Pre-Compaction Hooks Turn Memory Loss Into a Checkpoint

repoaicliagentsagent-loopsmemorypluginscoordinationtypescript

maps directly to joelclaw agent-loop resume points where task state, file reservations, and agent mail need to survive compaction

This opencode pull request adds an experimental.session.compacting plugin hook that fires before a session gets compacted. The hook lets a plugin append extra context to the compaction prompt instead of hoping the generic summary catches the important operational bits.

That’s the clever move: compaction stops being a dumb memory squish and becomes a checkpoint boundary. A coordination layer like swarm-tools can inject the active task, agent role, file reservations, recent mail, and instructions for re-syncing after the model resumes. That is exactly the stuff agents lose when they wake up in a fresh context and start confidently editing the wrong damn file.

The implementation is intentionally small: opencode triggers experimental.session.compacting with a sessionID, collects context: string[], and appends it to the default continuation prompt. Plugins fetch their own state. They can’t replace the base prompt. That append-only shape is good taste because it gives extensions power without turning compaction into prompt anarchy.

For joelclaw, this is a clean pattern for any long-running agent loop that crosses a context boundary. Before memory gets compressed, let the system of record speak: issue tracker, mailbox, reservations, current stage, acceptance criteria, and the next safe action.

Key Ideas

  • opencode now has an experimental.session.compacting hook that runs before the continuation summary is generated.
  • The hook appends domain-specific context to the compaction prompt through output.context.push(...).
  • swarm-tools can use the hook to preserve multi-agent coordination state across compaction.
  • Append-only plugin context is a safer extension point than letting plugins replace the core compaction prompt.
  • Context compression should include operational state, not just a conversational recap.
  • This maps to joelclaw agent-loop resumability: task state, file reservations, mail, and acceptance criteria should be reloaded after compaction.