refactor: cache-aligned summarization for compaction requests#2752
Open
lloydzhou wants to merge 3 commits intocharmbracelet:mainfrom
Open
refactor: cache-aligned summarization for compaction requests#2752lloydzhou wants to merge 3 commits intocharmbracelet:mainfrom
lloydzhou wants to merge 3 commits intocharmbracelet:mainfrom
Conversation
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the Contributor License Agreement (CLA) and hereby sign the CLA. |
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 — Cache-Aligned Summarization
Align the summarization agent's prefix (system prompt, tools, cache-control markers) with the main agent so that providers supporting prompt caching reuse the already-cached prefix on compaction requests. Providers that don't support explicit cache-control markers silently ignore them — no behavior change.
将摘要 agent 的前缀(system prompt、tools、cache-control markers)与主 agent 对齐,使支持 prompt caching 的 provider 在压缩请求时复用已缓存的前缀。不支持显式 cache-control 标记的 provider 会静默忽略,行为无变化。
Cost Example (Claude Sonnet 4, $3.00/$0.30/$15.00 per MTok)
A compaction request with 100k input tokens: 5k prefix (system prompt + tools) + 94k old messages + 1k summary instruction.
What Changed
buildSystemPrompt()— shared system prompt builder (base prompt + MCP instructions), extracted from inline code inRun.buildAgent()— shared agent factory: callsbuildSystemPrompt(), copies tools (with cache control on the last tool), createsfantasy.Agent. BothRunandSummarizenow call this instead of constructing agents independently.applyCacheControl(messages)— shared cache marking: clears stale provider options, marks the system message boundary and last 2 messages with cache control. BothPrepareStepclosures now call this.buildSummaryPrompt— summary instructions are now embedded in the user prompt (viatemplates/summary.md) rather than as a separate system prompt, so the main agent's system prompt prefix is preserved.变更内容
buildSystemPrompt()— 共享 system prompt 构建器(基础提示 + MCP 指令),从Run内联代码中提取。buildAgent()— 共享 agent 工厂:调用buildSystemPrompt()、复制 tools(最后一个 tool 添加 cache control)、创建fantasy.Agent。Run和Summarize均通过此方法构建 agent,不再各自独立构造。applyCacheControl(messages)— 共享缓存标记:清除过期的 provider options,标记 system message 边界和最后 2 条消息的 cache control。两个PrepareStep闭包均调用此方法。buildSummaryPrompt— 摘要指令改为嵌入在 user prompt 中(通过templates/summary.md),而非作为独立的 system prompt,从而保留主 agent 的 system prompt 前缀用于缓存命中。