feat(opencode): cache-friendly compaction via primary loop request path - #42506
Open
rekram1-node wants to merge 1 commit into
Open
feat(opencode): cache-friendly compaction via primary loop request path#42506rekram1-node wants to merge 1 commit into
rekram1-node wants to merge 1 commit into
Conversation
Default compaction no longer uses a dedicated compaction agent. It inherits the session's active agent and delegates request assembly to the primary loop's shared envelope (same system prompt, instructions, skills, MCP context, and tool definitions), replaying the typed message head and appending the summary instruction as the final user message. This keeps the compaction request prefix-identical to the preceding turn so provider prompt/KV caches are reused instead of fully re-prefilled. An explicitly configured agent.compaction preserves the legacy behavior exactly: dedicated hidden agent, optional model override, and the serialized-transcript summary request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Default compaction currently sends a request that shares no prefix with the conversation it is compacting: no system prompt, no tool definitions, and the history flattened into a serialized
[User]: ...transcript inside a single user message. Every compaction pays full prompt-processing cost for the entire session at exactly the moment the context is largest, and providers cannot reuse any of the warm prompt/KV cache from the preceding turn.This PR makes default compaction a prefix-extension of the previous request instead:
Everything before the trailing instruction is byte-identical to the prior turn's request, so the provider reuses the cached prefix and only the instruction plus the summary output are uncached.
Changes
agent.compactionconfig seeds the old hidden deny-all agent and preserves the legacy behavior exactly (dedicated agent, optional model override, serialized-transcript summary request), so existing customizations are unaffected.SessionPromptgains a single sharedpreparehelper (tools + system + model messages) used by both normal turns and compaction.SessionCompaction.processaccepts an optionalruncallback; when present (and no custom compaction agent is configured), the loop executes the summary request through the shared envelope.SessionCompactionkeeps owning selection, prompt construction, and persistence — no new service dependencies.buildReplayPromptin core builds the trailing instruction for the replayed-history path, including prior-summary merge instructions for repeated compactions.experimental.session.compacting,experimental.chat.messages.transform) run unconditionally at their original sites.Not in scope (follow-ups)
toolChoice: "none"is not usable because some providers omit the definitions entirely).Tests
[User]:flattening), and ends with the summary instruction.agent.compaction.compaction.test.ts,prompt.test.ts, andagent.test.tssuites pass; repo-wide typecheck passes.