description: "面向部署方的自动会话压缩:选择、调优或排查随 token 压力上升如何把较早历史摘要为总结。"
English | 中文
dsh-compaction-basic 让长时 agent 会话在接近模型上下文上限时仍能正常工作。token 压力上升时,它会自动把对话最旧的部分压缩为摘要,并保持近期部分完整;上下文溢出错误发生后,它会压缩并重试。你也可以通过 dsh-command-compact 的 /compact 按需压缩,并挂载 dsh-compaction-tool-result-pruner 先修剪超大工具输出。压缩的代价是一次额外的模型请求,它读取所选历史并写出摘要;只有摘要文本会被保留。它只压缩派生历史——无法缩减系统提示词、工具或会话前缀,也无法拆分单个不可分单元(例如一次超大工具调用)。
在已提供 LLM(大语言模型)、会话存储与 token 测量的组合中挂载本包,即可获得自动会话压缩。随附 dsh 基础配置默认启用它;需要控制压缩发生的时机时请显式挂载。
默认设置下你会获得四种行为:会话向模型上下文上限增长时自动压缩;提供方确认上下文溢出错误后的恢复(先压缩再重试该请求);通过 /compact 命令按需压缩;以及——挂载修剪器时——压缩前对超大工具输出的修剪。
挂载会话存储、token 测量、可选修剪器、本后端,以及可选的按需命令:
- name: '@deepseek-ai/dsh-session'
- name: '@deepseek-ai/dsh-token-meter'
- name: '@deepseek-ai/dsh-compaction-tool-result-pruner'
- name: '@deepseek-ai/dsh-compaction-basic'
- name: '@deepseek-ai/dsh-command-compact'
你可以通过观察会话越过本来会溢出的位置继续工作、以及运行 /compact 立即压缩一次来确认成功。如果组合缺少 LLM、会话存储或 token 测量,插件会加载失败。同一个后端可以服务上下文大小不同的模型;用按模型覆盖为每条路由设置各自的阈值与保留:
- name: '@deepseek-ai/dsh-compaction-basic'
config:
thresholdRatio: 0.8
retainRatio: 0.16
modelPolicies:
- provider: local
model: small-context
thresholdRatio: 0.7
retainTokens: 2048
所有设置都可选。默认在已路由模型上下文窗口的 80% 处开始压缩,并逐字保留最新的 16%;下表是完整的策略面,生成的配置目录是穷尽式真源。
| 字段 | 默认值 | 含义 |
|---|---|---|
thresholdRatio |
0.8 |
在 floor(routedContextWindow × ratio) 处开始压缩。 |
retainRatio |
0.16 |
以已路由上下文窗口的一部分表示逐字保留的近期对话;与 retainTokens 互斥。 |
retainTokens |
— | 逐字保留的近期对话绝对预算;与 retainRatio 互斥,并且必须低于已解析阈值。 |
summarizationProvider |
'' |
与 summarizationModel 一起设置;空对使用最新已路由请求目标,再回退到 AgentOptions 对。 |
summarizationModel |
'' |
与 summarizationProvider 一起设置;空对使用最新已路由请求目标,再回退到 AgentOptions 对。 |
maxTokens |
8192 |
摘要请求的输出上限;可包含推理 token。 |
compactionRetries |
1 |
压力仍高于阈值时,在首次压缩后进行的额外尝试次数。 |
maxOverflowRetries |
1 |
已确认上下文窗口溢出后的最大重试次数;0 只禁用恢复。 |
modelPolicies |
[] |
针对个别模型路由的精确 { provider, model, ...partialPolicy } 覆盖。 |
auto |
true |
启用自动压缩与溢出恢复;设为 false 则仅手动执行。 |
配置错误会快速失败:未知设置、重复的按模型覆盖、两种保留形式同时出现,或比例保留量不低于阈值,都会在加载时拒绝插件。任何绝对 retainTokens 预算——顶层或按模型——不低于其阈值时,都会在该模型首次使用时失败,因为该比较需要模型的上下文大小。
最旧的平衡范围会被替换为一条摘要消息,近期尾部保持逐字不变;对话从摘要继续。操作会报告压缩了多少历史项以及估算释放的 token 数。如果没有任何内容可以安全压缩——例如整个对话就是一个不可分单元——则不会有任何改变,也不会向会话日志写入任何内容。如果没有模型可以撰写摘要(既未配置目标,也还没有已路由请求),压缩会失败并给出清晰错误,提示你配置摘要提供方与模型,或先路由一次请求。
挂载 dsh-command-compact 后,在聊天 UI 中输入 /compact 即可立即压缩,即使未达到压力阈值。命令会报告压缩了多少历史项以及估算节省的 token 数。当 agent 正在轮次中或压缩已在运行时,/compact 会报告压缩暂不可用;运行期间你发送的提示词会被接受,并在压缩结束后才开始。
在本包之前挂载 dsh-compaction-tool-result-pruner,即可在压缩过程中修剪超大工具结果。修剪不发起模型调用,并可能完全省去摘要:当修剪后的对话在阈值之内时,压缩会跳过摘要。修剪只在压缩触发条件满足后运行——低于压力的对话绝不会被触碰。
当包级约定不够用时阅读以下页面;它们从共享 seam 逐步进入可选配套工具与决策证据。
成功步骤越过阈值后,如果已加载可选修剪器,超大工具结果会先被改写。如果仍需摘要,下一个请求会收到下方检查点前导、一个空行、<compacted-summary>、根据数据生成的摘要以及 </compacted-summary>。溢出恢复会根据使表层前进的任何替换重建立即重试。检查点会替换已选较早范围,后面跟随已保留的近期单元。
This is an automatically generated checkpoint condensing an earlier span of the conversation to free up context. Treat the captured context as established background and build on it without restating it. Continue the task directly from the messages that follow, without acknowledging this checkpoint.
不依赖模型的剪枝可以完全避免辅助调用;否则它会在摘要替换较早范围之前缩减该调用的 transcript(文本记录)。替换会缩减未来输入历史,而非追加第二份副本。摘要会保留到后续压缩将其替换,但不可分的非工具单元仍可能超出预算。
它是替换,而非仅追加。每个检查点都会使从第一个已替换历史 token 起的复用失效;该范围之前未更改的请求前缀仍可复用。
摘要模型会接收逐字回放的会话:与上次已路由请求为已遮蔽区域发送的相同系统提示词、工具 schema 与消息,后面跟随一条最终 user 消息,即下方压缩指令。会话模型绝不会看到该私有请求或其推理;只有返回文本会被存储。
You are now acting as a compaction engine for this AI coding assistant. Condense the conversation ABOVE into a structured checkpoint that lets another model resume the work with no loss of essential context.
Output EXACTLY the Markdown structure below: keep every section, in order. Use terse bullets, not prose paragraphs. Write "(none)" for an empty section — never drop a section.
## Primary Request and Intent
- [the user's original and evolving goals; quote verbatim where the exact wording matters]
## Key Technical Concepts
- [technologies, frameworks, patterns, and conventions in play]
## Files and Code
- [exact path: why it matters, key changes or snippets]
## Errors and Fixes
- [error: how it was resolved, plus any related user feedback]
## Pending Jobs
- [explicitly requested work not yet completed]
## Current Work
- [precisely what was in progress at this checkpoint]
## Next Step
- [the single next action, directly in line with the most recent request, or "(none)"]
## Critical Context
- [decisions and their rationale, constraints, user preferences, open questions, data needed to continue]
Rules:
- Write concise English engineering prose. Preserve exact file paths, commands, error strings, identifiers, numeric values, function signatures, and syntax fragments.
- Capture user feedback and explicit instructions faithfully, especially corrections.
- Do NOT mention this summarization request or that the context was compacted.
- Output only the checkpoint text: do not call any tool or take any other action.
- If the conversation already contains a <compacted-summary> block, it is a PRIOR checkpoint. Do not copy it forward verbatim: preserve still-true facts, drop stale ones, and merge newer information into a single consolidated summary under the same structure.
这是一次独立模型调用:输入是已回放会话前缀加固定指令,输出受 maxTokens 限制。收敛重试可能多次支付这项成本。
已回放系统提示词、工具与已遮蔽区域消息与会话最后一个已路由请求逐字匹配,因此提供方的热前缀 cache 可复用至尾随指令之前;只有该指令与摘要输出未缓存。将摘要器路由到不同提供方/模型,或压缩非头部范围,都会放弃该复用。
这些限制说明自动压缩何时不合适,或何时需要特别注意;它们是当前包约束。
CONTEXT_WINDOW_EXCEEDED。compactRegion 要求存在未结束的轮次——在完全关闭的会话上手动调用会抛出异常(「no open turn」),而不是执行压缩。maxTokens 而发生的摘要截断(隐藏推理 token 可能会耗尽该额度)遵循同一规则。