feat: enhance compaction prompts for higher-fidelity context restoration#14662
feat: enhance compaction prompts for higher-fidelity context restoration#14662ryanwyler wants to merge 2 commits intoanomalyco:devfrom
Conversation
Rewrite both the system prompt (compaction.txt) and the inline user message template (compaction.ts defaultPrompt) to produce compaction output that enables an AI agent to resume complex work after a full context wipe. ## System prompt (compaction.txt) Reframed from summarization to memory transfer. The model is told its context is about to be wiped and everything not written down will be lost. Preservation rules are direct commands, not conditionals: - PRESERVE resolved code verbatim — query patterns, state machines, auth flows, data model behaviors, correct field names, API response shapes. Do not describe code that you can show. - INCLUDE failed approaches numbered for cross-cycle reference - PRESERVE user directives as sacred, accumulating across cycles - PRESERVE credentials and operational details - RESOLVE implementation contradictions to settled state - DISCARD debugging noise, narration, superseded work, trivial code ## Inline template (compaction.ts defaultPrompt) Expanded from 5 sections to 8, structured around what an agent needs most when told to continue with no prior context: 1. Objective — immediate next action, current blockers 2. User Directives — separated from task instructions, accumulate 3. Plan — remaining steps, enough detail to pick up mid-step 4. Failed Approaches — exhaustive, numbered, highest-value section 5. Resolved Code & Discoveries — verbatim code blocks and patterns that would be painful to rediscover 6. How Things Work Now — settled system state as direct instructions 7. Accomplished — completed, in-progress, remaining as direct facts 8. Relevant files / environment — modification status, credentials, credential sync locations, endpoints, service ports ## Approach Developed through iterative testing against a long-running multi- compaction session, comparing output quality at each step. Key phrasings were refined based on measurable differences in what the model chose to preserve or discard — particularly around verbatim code inclusion, failed approach depth, and credential capture.
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
|
Thanks for the PR @ryanwyler ! Here is my take:
In any case, compaction is one of the core problems when using opencode day to day. I hope the core team review this and prioritize working on it. |
|
I would suggest that instead of making the compaction coding specific, you make it focused on general task performance. A wider umbrella will improve the longevity and overall effectiveness of the solution. A lot of coding work isn't coding work, but knowledge-work (e.g. research and design). The non-coding work is arguably negatively affected far more by compaction than coding - harder to recover the context. That said, the first thing I replaced in OpenCode was the compaction prompt. I'd be very interested in your ideas and approach to the higher-level problem of making compaction work for general task performance. |
Issue for this PR
Closes #3031
Also addresses #14368 and #2945
Type of change
What does this PR do?
Provides SIGNIFICANT improvements for maintaining a session after compaction. This rewrites the compaction system prompt and inline template so the agent retains significant working context to continue after compaction. Re-frames compaction as a memory transfer rather than a summary, expands the inline template from 5 sections to 8 (adding separated User Directives, Failed Approaches, Resolved Code and Discoveries, and How Things Work Now), and adds imperative preservation rules for resolved code, credentials, and user corrections.
Good to use paired with this PR to be able to use different models during compaction controlled via TUI: #14665
How did you verify your code works?
Tested against a real multi-hour session export, comparing compaction output across 5 prompt iterations. Final version benchmarked against a 22-cycle reference compaction from the same session.
Checklist