r/PiCodingAgent • • Sep 17 '26

Resource al-jaffee - simple cache-preserving fold-in compaction for pi agents

I've been working on a longer-term project and I have a lot of little side tangents and investigations as I work that I don't want the full context to get polluted by. I wrote a little pi plugin that lets agents create markers, then work on a task with their full context. When they're done, they can fold-in their full context to a summary, compacting the work in place.

It's less manual than forking the session and a little less to keep track of than subagents. I thought it was worth sharing. MIT license, 1k lines of code. Cost was maybe a dime worth of GLM 5.3 Flash.

https://github.com/gopherbone/al-jaffee

17 Upvotes

22 comments sorted by

View all comments

Show parent comment

2

u/porkyminch Sep 17 '26

It doesn't replace native compaction, it's a different set of tools. Agents are aware of the folds and know how/when to trigger them themselves, but if you ask them to fold in a task, they'll do it. They get a little summary of the work from the agent (plus file changes, tool calls counted programmatically) when they fold into the marker, which is appended onto their cache prefix so we don't do a total cache bust. To the agent they just look like tool calls, and the markers are all agent-created, so the agents kind of naturally know how to fold into them. It's an install and forget thing and also a thing you can recommend agents use. To the folded agent, the fold looks like this:

… main conversation …
assistant: set_fold_marker({ task: "archive inventory" })
toolResult: [al-jaffee] FOLD COMPLETE — tangent folded into this marker.
            Task: archive inventory
            Folded away: 9 conversation entries … you won't remember doing
            it, and that is expected, not an error. Treat the summary below
            as your own notes…
            Summary (yours, written at fold time): read a.md (kickoff, plan
            B), b.md (budget $12.4k), c.md (roadmap)…
assistant: (continues the main line from here — tangent tokens gone from context)

I'm not sure about forked subagents, but it'll probably work.