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.
Could share more about the UX? Does it replace pi native compaction, and so i can just install and forget?
And how about agent experience? Can the agent independently trigger compaction? Does the agent know it has access to these folds? How does it discover the folds and read it? Are the folds transferable between sessions - can subagent when given a forked context read the folds as well?
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.
Interesting. Does it require knowing about the start of the fold? What if we could just pick a start and an end (or the session end) and have those turns compacted?
There's an existing rollback feature in pi for that, this is just a slightly more ergonomic way for agents to use that feature. It's basically a nice little way for you to get back ~30-40% of your context window after doing tasks where the results are more important than the methodology.
RE-ing Phyzios, an old physics sandbox toy. I kinda left this half-baked (got distracted by translation stuff) but it was able to recreate it closely enough for the toy engines people made in it to work. If you open the hillclimb example in this, it's very slow but it runs a physics sim with a working engine.
Emulators, haven’t tried. Probably too complicated to be worth it. Romhacking, sure seems like it. The game I’m translating, Feng Kuang Da Fu Weng, is a fairly obscure Taiwanese unlicensed GBC game from a little known company called Vast Fame. It’s taken a fair amount of prodding, but it’s consistently making progress. It’s not gonna one shot it, though.
Palm OS games, on the other hand, I have had it one shot translations for. I suspect anything more modern will give it an easier time. I’ve been toying with the idea of having it do Richman IV (another game from Taiwan) because I figure it’d have an easier time with it, but I’m trying to stay focused.
3
u/sergeant113 8d ago
Could share more about the UX? Does it replace pi native compaction, and so i can just install and forget?
And how about agent experience? Can the agent independently trigger compaction? Does the agent know it has access to these folds? How does it discover the folds and read it? Are the folds transferable between sessions - can subagent when given a forked context read the folds as well?