@crazx/dsh-compaction-basic 0.1.1-rc.1.zw.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.i18n.yaml +6 -0
- package/README.md +172 -0
- package/README.zh.md +172 -0
- package/lib/index.js +1580 -0
- package/lib/invariant.js +23 -0
- package/lib/types/config.d.ts +37 -0
- package/lib/types/hierarchical-planner.d.ts +40 -0
- package/lib/types/hierarchical-prompts.d.ts +40 -0
- package/lib/types/hierarchical.d.ts +27 -0
- package/lib/types/index.d.ts +83 -0
- package/lib/types/invariant.d.ts +16 -0
- package/lib/types/region.d.ts +63 -0
- package/lib/types/summarizer.d.ts +74 -0
- package/lib/types/types.d.ts +90 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.i18n.yaml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each
|
|
2
|
+
# side as of the last confirmed-consistent state. Both languages carry equal authority;
|
|
3
|
+
# after editing either side, bring the other along and re-record with:
|
|
4
|
+
# pnpm run verify-translation-pairing --write packages/compaction/compaction-basic/README.md
|
|
5
|
+
README.md: 9dc6ba5e77856d762b002fcc4606a45d82fbd324
|
|
6
|
+
README.zh.md: 16f95f140f8d08896bacbe9d498a5167c9143a80
|
package/README.md
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# @deepseek-ai/dsh-compaction-basic
|
|
2
|
+
|
|
3
|
+
English | [中文](README.zh.md)
|
|
4
|
+
|
|
5
|
+
The **basic compaction backend**: a `BasicCompactionEngine` implementing the `@deepseek-ai/dsh-compaction` Service Definition with reusable `ctx.tokenMeter` pressure, token-budget retention, and bounded summarization. Inputs that fit use the direct one-shot `ctx.llm.stream()` path and replay the conversation prefix for provider KV-cache reuse; oversized or Provider-rejected inputs automatically use chronological map-reduce calls (all interceptable at `llm/stream`).
|
|
6
|
+
|
|
7
|
+
This package owns the Service Provider role of the compaction capability — see the [Service Definition package](../compaction/README.md) for its contract and the [capability-seam Agent Note](../../../.agents/notes/implemented/feature/2026-06-18-compaction-capability-seam.md) for the design.
|
|
8
|
+
|
|
9
|
+
## What it owns
|
|
10
|
+
|
|
11
|
+
This backend owns the compaction policy:
|
|
12
|
+
|
|
13
|
+
- **Measurement** — the singleton `ctx.tokenMeter` prices the latest canonical logged envelope and current surface at one consumed-log revision. Step-boundary pressure therefore includes the actual system prompt, tools, routing, assistant completion, tool results, buffered context, and steering.
|
|
14
|
+
- **Routed policy** — proactive pressure resolves capacity from the adapter that owns the latest durable provider/model route, then scales the default policy plus an optional exact-target override into concrete token budgets. Model discovery remains advisory and is not consulted.
|
|
15
|
+
- **Model-free pruning** — after pressure or canonical overflow qualifies, the optional [`ctx.toolResultPruner`](../compaction-tool-result-pruner/README.md) service rewrites oversized tool results before range selection. Compact-basic remeasures through `ctx.tokenMeter`, skips summarization when pressure becomes safe, and otherwise summarizes the pruned surface. Below-pressure step checks never prune.
|
|
16
|
+
- **Retention** — compact the oldest whole surface units while preserving a recent tail and balanced tool-call/result cuts through the [`dsh-compaction` boundary helpers](../compaction/README.md#tool-pairing-boundaries). Turn boundaries do not protect old steps inside a runaway turn. An open indivisible tail declines until it closes. The optional pruner can repair an oversized closed tool unit when its text-bearing result is the removable bulk; indivisible non-tool units and non-prunable tool remainders remain out of scope.
|
|
17
|
+
- **Convergence** — retry head-checkpoint compaction up to `compactionRetries`; reject a summary that does not shrink its source, and throw if retries cannot return below threshold.
|
|
18
|
+
- **Summarization** — the configured provider/model pair, falling back to the latest logged request target and then the agent target, handles every auxiliary `llm/stream` call without running the loop-only `agent/request` extension point. When estimated input plus `maxTokens` fits the declared summary-model window, the unchanged one-shot path replays the conversation's system prompt, tools, and shadowed messages verbatim and appends the compaction instruction, preserving warm-prefix KV-cache reuse. If that envelope cannot fit, or the Provider returns canonical `CONTEXT_WINDOW_EXCEEDED`, compaction maps bounded chronological, tool-balanced spans and recursively reduces structured partial checkpoints. Provider-confirmed overflow bisects only the failed span and retains successful siblings. Hierarchy stages set `purpose: 'compaction'`, omit tool schemas by default, reject truncated, visual, or structurally incomplete output, and make no durable replacement until the complete hierarchy succeeds.
|
|
19
|
+
- **Summary provenance** — a successful one-shot keeps `llmStreamCall: true` and its reported usage. A single successful hierarchy stage may also carry that marker; multi-call or failed-attempt recovery leaves it unset. Multi-stage usage is summed only when every successful call reports usage and no failed model attempt occurred, preventing partial accounting from appearing complete.
|
|
20
|
+
- **Framing** — the replacement user message marks established checkpoint context with `<compacted-summary>` tags. The raw summary remains on the `compaction/summary` event, and later automatic cycles merge the prior checkpoint.
|
|
21
|
+
- **Lifecycle** — all entry points share one bracket-first region transaction. It validates the range and live lock, appends `compaction/start` synchronously, prepares and awaits the summary, revalidates, appends `compaction/summary` plus the replacement, and makes exactly one closing attempt. Automatic and explicit-region calls require a numeric open-turn owner and whole-surface stability; the serial `agent/pre-step` listener checks pressure before request derivation, while canonical provider overflow enters through `agent/request-error` and authorizes retry only after durable surface progress. `compactNow()` reserves idle admission, uses `turn: null`, accepts append-only context outside its selected span, flushes every closed attempt, and releases admission in `finally`.
|
|
22
|
+
- **Overflow recovery** — provider-confirmed overflow needs no capacity metadata: it bypasses normal pressure and retention, prunes, then attempts one maximal balanced head reduction while leaving the newest indivisible unit. Retry is authorized whenever `surface.replaceGeneration` advances, including when pruning lands before later summary work throws. No replacement, an exhausted target-specific cap, cancellation, or an unknown/noncanonical error preserves the original provider failure.
|
|
23
|
+
- **Failure handling** — a live unmatched `compaction/start` is the durable lock. An unmatched marker before a newer `session/end-seed` is stale evidence from a prior lifecycle and does not block; one after that boundary reports `busy`. Summary and changed-span failures close with an error and leave the conversation surface untouched, though the attempt remains in the log. A failed close deliberately leaves a blocking orphan. Operational pressure failures warn and continue, while overflow-recovery failure preserves the original provider error only when no earlier replacement advanced the surface. Cancellation remains authoritative after cleanup and durability.
|
|
24
|
+
|
|
25
|
+
The protected `summarize()` method is the sole subclass hook. A template- or remote-summarizer subclass can override it while pressure, retention, cited source events, shrink validation, and shadowed-token accounting stay on `ctx.tokenMeter`. The hook returns the safe summary plus the complete provider output, call envelope, and usage when available (`{ summary, rawOutput?, llmStreamCall?, provider, model, maxTokens?, usage? }`); `llmStreamCall: true` means producing that result consumed exactly one call through this context's `ctx.llm.stream()` and requires complete `rawOutput`, while unmarked `rawOutput` does not identify the call path. The transaction preserves those fields on `compaction/summary`.
|
|
26
|
+
|
|
27
|
+
## Config (`BasicCompactionConfig`)
|
|
28
|
+
|
|
29
|
+
Every setting is optional. Top-level policy fields are defaults for every routed model; `modelPolicies` applies partial overrides to exact provider/model pairs. At pressure time, compaction-basic asks the owning LLM adapter for that route's context capacity and resolves absolute budgets. Unrecognized keys, duplicate targets, mutually exclusive retention forms, and a merged `retainRatio` that is not below `thresholdRatio` fail plugin load. An absolute `retainTokens` budget that is not below its scaled threshold fails on the first resolvable target because that comparison requires model capacity.
|
|
30
|
+
|
|
31
|
+
| Key | Required | Meaning |
|
|
32
|
+
|---|---|---|
|
|
33
|
+
| `thresholdRatio` | no (default `0.8`) | Compact at `floor(routedContextWindow × ratio)`. |
|
|
34
|
+
| `retainRatio` | no (default `0.16`) | Recent surface budget kept verbatim as a fraction of the routed context window; mutually exclusive with `retainTokens`. |
|
|
35
|
+
| `retainTokens` | no | Absolute recent surface budget kept verbatim; mutually exclusive with `retainRatio` and must be below the resolved threshold. |
|
|
36
|
+
| `summarizationProvider` | no (default `''`) | Set together with `summarizationModel`; an empty pair resolves the latest logged request target, then the `AgentOptions` pair. |
|
|
37
|
+
| `summarizationModel` | no (default `''`) | Set together with `summarizationProvider`; an empty pair resolves the latest logged request target, then the `AgentOptions` pair. |
|
|
38
|
+
| `maxTokens` | no (default `8192`) | Provider generation cap for the summarization call; may include reasoning tokens. |
|
|
39
|
+
| `compactionRetries` | no (default `1`) | Extra attempts after the first when pressure remains above threshold. |
|
|
40
|
+
| `maxOverflowRetries` | no (default `1`) | Maximum retries after canonical context-window overflow; `0` disables recovery only. |
|
|
41
|
+
| `chunkInputRatio` | no (default `0.6`) | Fraction of the summary model window available to each hierarchy stage input; valid range `[0.1, 0.9]`. |
|
|
42
|
+
| `mapMaxTokens` | no (default `4096`) | Provider generation cap for one hierarchy map call. |
|
|
43
|
+
| `reduceMaxTokens` | no (default `8192`) | Provider generation cap for one hierarchy reduce call. |
|
|
44
|
+
| `maxDepth` | no (default `4`) | Maximum recursive reduce rounds; valid range `1..8`. |
|
|
45
|
+
| `replayTools` | no (default `false`) | Replay tool schemas in hierarchy stages. Enabling this may be required by strict Providers but consumes chunk input and reduces prefix reuse. |
|
|
46
|
+
| `modelPolicies` | no (default `[]`) | Exact `{ provider, model, ...partialPolicy }` overrides; matching uses both fields and does not depend on `listModels()`. |
|
|
47
|
+
| `auto` | no (default `true`) | Register step-boundary pressure and overflow-recovery listeners. Set `false` for manual-only. |
|
|
48
|
+
|
|
49
|
+
Every `modelPolicies` entry accepts the policy fields above except `auto` and `modelPolicies` itself. If an entry supplies either retention field, it replaces the default policy's retention choice; otherwise retention is inherited. Summarization provider/model remain a pair inside each entry.
|
|
50
|
+
|
|
51
|
+
An adapter may return no capacity for a valid dynamic route, and resolved capacity may expose an invalid absolute retention budget. Manual pressure checks then throw a target-specific configuration error; the automatic listener warns once for that exact target and continues with full history. Unrelated operational failures remain independently visible. Canonical provider overflow still attempts recovery because the provider has already established that compaction is necessary.
|
|
52
|
+
|
|
53
|
+
## Usage
|
|
54
|
+
|
|
55
|
+
`BasicCompactionEngine` requires `ctx.llm`, `ctx.tokenMeter`, and `ctx.sessions`. The composition below receives `ctx.llm` from its host and installs the other two services:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
59
|
+
import { BasicCompactionEngine } from '@deepseek-ai/dsh-compaction-basic'
|
|
60
|
+
import SessionStore from '@deepseek-ai/dsh-session'
|
|
61
|
+
import TokenMeter from '@deepseek-ai/dsh-token-meter'
|
|
62
|
+
|
|
63
|
+
export const name = 'compaction-basic'
|
|
64
|
+
export const inject = ['llm']
|
|
65
|
+
|
|
66
|
+
export function apply(ctx: Context): void {
|
|
67
|
+
ctx.plugin(SessionStore)
|
|
68
|
+
ctx.plugin(TokenMeter)
|
|
69
|
+
ctx.plugin(BasicCompactionEngine)
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Loading the plugin registers `ctx.compaction`. Add [`dsh-compaction-tool-result-pruner`](../compaction-tool-result-pruner/README.md) as a sibling before this plugin to enable the optional model-free pass. With `auto: true` (the default) it compacts automatically under token pressure. The sibling [`dsh-command-compact`](../command-compact/README.md) calls `ctx.compaction.compactNow(...)`; programmatic callers may also use any seam operation directly.
|
|
74
|
+
|
|
75
|
+
For example, the same compact plugin can safely serve models with different capacities and one target-specific policy:
|
|
76
|
+
|
|
77
|
+
```yaml
|
|
78
|
+
- name: '@deepseek-ai/dsh-compaction-basic'
|
|
79
|
+
config:
|
|
80
|
+
thresholdRatio: 0.8
|
|
81
|
+
retainRatio: 0.16
|
|
82
|
+
modelPolicies:
|
|
83
|
+
- provider: local
|
|
84
|
+
model: small-context
|
|
85
|
+
thresholdRatio: 0.7
|
|
86
|
+
retainTokens: 2048
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Model Experience
|
|
90
|
+
|
|
91
|
+
### Conversation history
|
|
92
|
+
|
|
93
|
+
#### What the model sees
|
|
94
|
+
|
|
95
|
+
After a successful step crosses the threshold, oversized tool results are first rewritten when the optional pruner is loaded. If summarization remains necessary, the next request receives the checkpoint preamble below, a blank line, `<compacted-summary>`, the data-dependent summary, and `</compacted-summary>`. Overflow recovery rebuilds the immediate retry from whatever replacement advanced the surface. A checkpoint replaces the selected older range and is followed by the retained recent units.
|
|
96
|
+
|
|
97
|
+
##### Conversation checkpoint preamble
|
|
98
|
+
|
|
99
|
+
```markdown
|
|
100
|
+
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.
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### Token effect
|
|
104
|
+
|
|
105
|
+
Model-free pruning can avoid the auxiliary call entirely; otherwise it reduces that call's transcript before the summary replaces an older range. The replacement reduces future input history rather than appending a second copy. A summary remains until a later compaction replaces it, while an indivisible non-tool unit can still exceed the budget.
|
|
106
|
+
|
|
107
|
+
#### KV Cache effect
|
|
108
|
+
|
|
109
|
+
Replacing rather than append-only. Each checkpoint invalidates reuse from the first replaced history token; the unchanged request prefix before that range remains reusable.
|
|
110
|
+
|
|
111
|
+
### Auxiliary summarizer request
|
|
112
|
+
|
|
113
|
+
#### What the model sees
|
|
114
|
+
|
|
115
|
+
When the complete request fits, the summarization model receives the conversation replayed verbatim — the same system prompt, tool schemas, and messages the last routed request sent for the shadowed region — followed by one final user message: the compaction instruction below. For hierarchy, each map request receives the same system prompt, an ordered tool-balanced source span, and a structured map instruction; reduce requests receive ordered `<partial-summary>` frames and a structured reduce instruction. Tool schemas accompany hierarchy calls only when `replayTools: true`. The conversation model never sees these private requests or their reasoning; only the final text is stored.
|
|
116
|
+
|
|
117
|
+
##### Compaction instruction (final user message)
|
|
118
|
+
|
|
119
|
+
```markdown
|
|
120
|
+
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.
|
|
121
|
+
|
|
122
|
+
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.
|
|
123
|
+
|
|
124
|
+
## Primary Request and Intent
|
|
125
|
+
- [the user's original and evolving goals; quote verbatim where the exact wording matters]
|
|
126
|
+
|
|
127
|
+
## Key Technical Concepts
|
|
128
|
+
- [technologies, frameworks, patterns, and conventions in play]
|
|
129
|
+
|
|
130
|
+
## Files and Code
|
|
131
|
+
- [exact path: why it matters, key changes or snippets]
|
|
132
|
+
|
|
133
|
+
## Errors and Fixes
|
|
134
|
+
- [error: how it was resolved, plus any related user feedback]
|
|
135
|
+
|
|
136
|
+
## Pending Jobs
|
|
137
|
+
- [explicitly requested work not yet completed]
|
|
138
|
+
|
|
139
|
+
## Current Work
|
|
140
|
+
- [precisely what was in progress at this checkpoint]
|
|
141
|
+
|
|
142
|
+
## Next Step
|
|
143
|
+
- [the single next action, directly in line with the most recent request, or "(none)"]
|
|
144
|
+
|
|
145
|
+
## Critical Context
|
|
146
|
+
- [decisions and their rationale, constraints, user preferences, open questions, data needed to continue]
|
|
147
|
+
|
|
148
|
+
Rules:
|
|
149
|
+
- Write concise English engineering prose. Preserve exact file paths, commands, error strings, identifiers, numeric values, function signatures, and syntax fragments.
|
|
150
|
+
- Capture user feedback and explicit instructions faithfully, especially corrections.
|
|
151
|
+
- Do NOT mention this summarization request or that the context was compacted.
|
|
152
|
+
- Output only the checkpoint text: do not call any tool or take any other action.
|
|
153
|
+
- 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.
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### Token effect
|
|
157
|
+
|
|
158
|
+
A fitting input costs one separate model call: the replayed conversation prefix plus the fixed instruction, with `maxTokens`-capped output. Hierarchy costs one call per map span plus one or more reduction calls capped by `mapMaxTokens` and `reduceMaxTokens`; Provider-confirmed overflow may add failed attempts before local bisection. Convergence retries can pay either cost more than once.
|
|
159
|
+
|
|
160
|
+
#### KV Cache effect
|
|
161
|
+
|
|
162
|
+
The fitting one-shot request matches the conversation's replayed system prompt, tools, and shadowed-region messages byte-for-byte, so the provider's warm prefix cache is reused up to the trailing instruction. Routing to another model or compacting a non-head range forgoes that reuse. Hierarchy intentionally bounds each call and therefore cannot preserve one full warm prefix: map calls may reuse their leading system/message prefix where the Provider permits, while reduce calls operate on newly generated partials. `replayTools: false` also omits the tool-schema prefix to leave more room for source messages.
|
|
163
|
+
|
|
164
|
+
## Known Limitations and Deferred Work
|
|
165
|
+
|
|
166
|
+
- **Meter accuracy follows the fixed heuristic** — missing reusable provider usage falls back to character count plus structural overhead rather than exact tokenization.
|
|
167
|
+
- **Overflow classification is adapter-maintained** — provider wording can change; both DeepSeek adapters normalize currently recognized context-limit failures to `CONTEXT_WINDOW_EXCEEDED`.
|
|
168
|
+
- **Bounded recovery requires summary-model capacity metadata** — an adapter that omits `contextWindow` keeps the legacy one-shot path. If that request succeeds, behavior is unchanged; if it overflows, hierarchy cannot derive safe chunk budgets and fails with an actionable capacity error.
|
|
169
|
+
- **Hierarchy output is a strict checkpoint protocol** — every map and reduce stage must return all required headings. Truncation, visual output, malformed structure, exhausted `maxDepth`, or an indivisible source/partial that still overflows fails the complete compaction transaction without installing a partial checkpoint.
|
|
170
|
+
- **Some indivisible-unit and envelope-only overflow remains outside surface compaction** — recovery cannot shrink system/tools/prefix, split an indivisible non-tool node, or repair a tool unit whose non-prunable remainder still exceeds the window. The optional pruner can shrink text-bearing tool-result bulk inside an otherwise indivisible pair.
|
|
171
|
+
- **`compactRegion` requires an open turn** — a manual call on a fully-closed session throws ("no open turn") rather than compacting.
|
|
172
|
+
- **Summarization failure preserves the latest durable surface** — before any replacement, the auto path logs a warning and proceeds with full over-budget history. If pruning already landed, a later summarization failure proceeds from that durable pruned surface. Summarization truncation at `maxTokens`, which hidden reasoning tokens can consume, follows the same rule.
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# @deepseek-ai/dsh-compaction-basic
|
|
2
|
+
|
|
3
|
+
[English](README.md) | 中文
|
|
4
|
+
|
|
5
|
+
**基础压缩(compaction)后端**:`BasicCompactionEngine` 实现 `@deepseek-ai/dsh-compaction` Service Definition,使用可复用的 `ctx.tokenMeter` 压力、token 预算保留与有界摘要。能够装入窗口的输入继续走直接的一次性 `ctx.llm.stream()` 路径,并回放会话前缀以复用提供方 KV Cache;超大输入或被 Provider 拒绝的输入会自动改用按时间顺序执行的 map-reduce 调用(均可在 `llm/stream` 处拦截)。
|
|
6
|
+
|
|
7
|
+
本包承担压缩能力的 Service Provider 角色;其约定见 [Service Definition 包](../compaction/README.zh.md),设计见 [能力 seam Agent Note](../../../.agents/notes/implemented/feature/2026-06-18-compaction-capability-seam.zh.md)。
|
|
8
|
+
|
|
9
|
+
## 拥有的职责
|
|
10
|
+
|
|
11
|
+
该后端拥有压缩策略:
|
|
12
|
+
|
|
13
|
+
- **测量**:单例 `ctx.tokenMeter` 会在同一个已消费日志 revision 上,计量最新一份规范化已记录 envelope 与当前表层的 token 用量。因此,步骤边界的压力计量会包含实际系统提示词、工具、路由、assistant 完成、工具结果、缓冲上下文与 steering(中途引导)。
|
|
14
|
+
- **路由策略**:主动压力从拥有最新持久提供方/模型路由的适配器解析容量,再将默认策略与可选的精确目标覆盖缩放为具体 token 预算。模型发现仍仅供参考,不参与此处的策略解析。
|
|
15
|
+
- **不依赖模型的剪枝**:在压力或规范溢出符合条件后,可选的 [`ctx.toolResultPruner`](../compaction-tool-result-pruner/README.zh.md) 服务会在选择范围之前改写超大工具结果。Compact-basic 通过 `ctx.tokenMeter` 重新测量;如果压力已回到安全范围,就跳过摘要,否则对已剪枝的表层进行摘要。低于压力的步骤检查绝不剪枝。
|
|
16
|
+
- **保留**:压缩最旧的完整表层单元,同时保留近期尾部,并通过 [`dsh-compaction` 边界 helper](../compaction/README.zh.md#tool-pairing-boundaries) 将切分点调整到工具调用/结果配对平衡的位置。轮次边界不会保护失控轮次内的旧步骤。尚未闭合且不可分的尾部会在闭合前拒绝压缩。当闭合的超大工具单元以文本型结果为可移除主体时,可选 pruner 可以修复它;不可分的非工具单元与不可剪枝的工具剩余部分不在范围内。
|
|
17
|
+
- **收敛**:最多按 `compactionRetries` 重试头部检查点压缩;拒绝不能缩小源内容的摘要,如果重试仍无法回到阈值以下,则抛出异常。
|
|
18
|
+
- **摘要**:已配置的提供方/模型对会处理所有辅助 `llm/stream` 调用;未配置时先回退到最新已记录请求目标,再回退到 agent(智能体)目标,并且不运行仅用于 agent loop 的 `agent/request` 扩展点。当估算输入与 `maxTokens` 能装入摘要模型声明的窗口时,原有 one-shot 路径保持不变:逐字回放会话系统提示词、工具与已遮蔽消息,再追加压缩指令,从而复用热前缀 KV Cache。如果该 envelope 无法装入,或 Provider 返回规范的 `CONTEXT_WINDOW_EXCEEDED`,压缩会映射有界、按时间排序且工具配对平衡的 span,再递归归并结构化部分检查点。Provider 已确认的溢出只会二分失败 span,并保留成功 sibling。Hierarchy stage 均设置 `purpose: 'compaction'`,默认省略工具 schema,拒绝截断、视觉或结构不完整的输出,并且只有整个层级成功后才执行持久替换。
|
|
19
|
+
- **摘要来源与 usage**:成功 one-shot 保留 `llmStreamCall: true` 及 Provider 上报的 usage。只有一个成功 stage 的 hierarchy 也可以携带该标记;多调用或曾有失败尝试的恢复不会设置它。只有所有成功调用都报告 usage 且不存在失败模型尝试时,才汇总多 stage usage,避免把不完整计量伪装成完整值。
|
|
20
|
+
- **框定**:替换 user 消息使用 `<compacted-summary>` 标签标记已建立的检查点上下文。原始摘要保留在 `compaction/summary` 事件上,后续自动周期会合并之前的检查点。
|
|
21
|
+
- **生命周期**:所有入口点共享一个先记录标记的区域事务。它会验证范围与活动锁,同步追加 `compaction/start`,准备并等待摘要,重新验证,再追加 `compaction/summary` 和替换,最后恰好进行一次闭合尝试。自动调用和显式范围调用要求数字标识的开放轮次归属,并要求整个表层保持稳定;串行 `agent/pre-step` listener 会在派生请求之前检查压力,而规范提供方溢出则经由 `agent/request-error` 进入,并且只在表层取得持久进展后才允许重试。`compactNow()` 会预留空闲接纳,使用 `turn: null`,允许所选 span 之外追加仅追加上下文,flush 每次已闭合尝试,并在 `finally` 中释放接纳预留。
|
|
22
|
+
- **溢出恢复**:提供方已确认的溢出不需容量元数据。它会绕过常规压力与保留,执行剪枝,再尝试一次最大平衡头部缩减,并留下最新不可分单元。只要 `surface.replaceGeneration` 前进,就允许重试,包括剪枝在后续摘要工作抛出异常前已落地的情况。如果没有替换、目标特定上限已耗尽、已取消,或遇到未知/非规范错误,则保留原始提供方失败。
|
|
23
|
+
- **失败处理**:活动的未匹配 `compaction/start` 是持久锁。位于较新 `session/end-seed` 之前的未匹配标记,是先前生命周期留下的陈旧证据,不会阻塞;位于该边界之后的标记报告 `busy`。摘要和 span 变更失败会以错误闭合,并保持会话表层不变,但日志中仍保留该尝试。闭合失败会有意留下阻塞性的未匹配标记。压力检查中的运行故障会发出警告并继续;只有此前没有替换推进表层时,溢出恢复失败才保留原始提供方错误。完成清理与持久化后,取消仍具有最终决定权。
|
|
24
|
+
|
|
25
|
+
受保护的 `summarize()` 方法是唯一的子类钩子。基于模板或远程摘要器的子类可以覆盖该方法,同时压力、保留、被引用的源事件、缩减验证与已遮蔽 token 计量仍由 `ctx.tokenMeter` 负责。钩子返回安全摘要,以及完整提供方输出、调用 envelope 和可用时的 usage(`{ summary, rawOutput?, llmStreamCall?, provider, model, maxTokens?, usage? }`);`llmStreamCall: true` 表示生成该结果时恰好通过此上下文的 `ctx.llm.stream()` 发起了一次调用,且必须提供完整的 `rawOutput`;未带标记的 `rawOutput` 并不能判定调用路径。事务会在 `compaction/summary` 上保留这些字段。
|
|
26
|
+
|
|
27
|
+
## 配置(`BasicCompactionConfig`)
|
|
28
|
+
|
|
29
|
+
所有设置都可选。顶层策略字段是每个已路由模型的默认值;`modelPolicies` 对精确提供方/模型对应用部分覆盖。出现压力时,compaction-basic 会请求所属 LLM(大语言模型)适配器提供该路由的上下文容量,并解析绝对预算。无法识别的配置键、重复目标、互斥保留形式,以及合并后的 `retainRatio` 不低于 `thresholdRatio`,都会使插件加载失败。不低于缩放后阈值的绝对 `retainTokens` 预算会在首次解析出目标时导致失败,因为该比较需要模型容量。
|
|
30
|
+
|
|
31
|
+
| Key | 必填 | 含义 |
|
|
32
|
+
|---|---|---|
|
|
33
|
+
| `thresholdRatio` | 否(默认 `0.8`) | 在 `floor(routedContextWindow × ratio)` 处压缩。 |
|
|
34
|
+
| `retainRatio` | 否(默认 `0.16`) | 以已路由上下文窗口的一部分表示逐字保留的近期表层预算;与 `retainTokens` 互斥。 |
|
|
35
|
+
| `retainTokens` | 否 | 逐字保留的近期表层绝对预算;与 `retainRatio` 互斥,并且必须低于已解析阈值。 |
|
|
36
|
+
| `summarizationProvider` | 否(默认 `''`) | 与 `summarizationModel` 一起设置;空对会解析为最新已记录请求目标,再回退到 `AgentOptions` 对。 |
|
|
37
|
+
| `summarizationModel` | 否(默认 `''`) | 与 `summarizationProvider` 一起设置;空对会解析为最新已记录请求目标,再回退到 `AgentOptions` 对。 |
|
|
38
|
+
| `maxTokens` | 否(默认 `8192`) | 摘要调用的提供方生成上限;可包含推理 token。 |
|
|
39
|
+
| `compactionRetries` | 否(默认 `1`) | 压力仍高于阈值时,在首次尝试后进行的额外尝试次数。 |
|
|
40
|
+
| `maxOverflowRetries` | 否(默认 `1`) | 规范上下文窗口溢出后的最大重试次数;`0` 只禁用恢复。 |
|
|
41
|
+
| `chunkInputRatio` | 否(默认 `0.6`) | 每个 hierarchy stage 输入可使用的摘要模型窗口比例;有效范围为 `[0.1, 0.9]`。 |
|
|
42
|
+
| `mapMaxTokens` | 否(默认 `4096`) | 单次 hierarchy map 调用的 Provider 生成上限。 |
|
|
43
|
+
| `reduceMaxTokens` | 否(默认 `8192`) | 单次 hierarchy reduce 调用的 Provider 生成上限。 |
|
|
44
|
+
| `maxDepth` | 否(默认 `4`) | 最大递归 reduce 轮数;有效范围为 `1..8`。 |
|
|
45
|
+
| `replayTools` | 否(默认 `false`) | 在 hierarchy stage 中回放工具 schema。严格 Provider 可能要求开启,但会占用 chunk 输入并降低前缀复用。 |
|
|
46
|
+
| `modelPolicies` | 否(默认 `[]`) | 精确的 `{ provider, model, ...partialPolicy }` 覆盖;匹配使用两个字段,不依赖 `listModels()`。 |
|
|
47
|
+
| `auto` | 否(默认 `true`) | 注册步骤边界压力与溢出恢复 listener。设为 `false` 则仅手动执行。 |
|
|
48
|
+
|
|
49
|
+
每个 `modelPolicies` 配置项都接受上述策略字段,但不接受 `auto` 和 `modelPolicies` 自身。如果配置项提供任意一个保留字段,就替换默认策略的保留选择;否则继承保留设置。摘要提供方/模型在每个配置项内仍然成对。
|
|
50
|
+
|
|
51
|
+
适配器可能无法为有效动态路由返回容量,已解析容量也可能暴露无效的绝对保留预算。此时手动压力检查会抛出目标特定配置错误;自动 listener 会对该精确目标警告一次,并携带完整历史继续。不相关的操作性失败仍会独立可见。规范提供方溢出仍会尝试恢复,因为提供方已确立压缩的必要性。
|
|
52
|
+
|
|
53
|
+
## 用法
|
|
54
|
+
|
|
55
|
+
`BasicCompactionEngine` 需要 `ctx.llm`、`ctx.tokenMeter` 和 `ctx.sessions`。以下组合从其宿主接收 `ctx.llm`,并安装另外两项服务:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
59
|
+
import { BasicCompactionEngine } from '@deepseek-ai/dsh-compaction-basic'
|
|
60
|
+
import SessionStore from '@deepseek-ai/dsh-session'
|
|
61
|
+
import TokenMeter from '@deepseek-ai/dsh-token-meter'
|
|
62
|
+
|
|
63
|
+
export const name = 'compaction-basic'
|
|
64
|
+
export const inject = ['llm']
|
|
65
|
+
|
|
66
|
+
export function apply(ctx: Context): void {
|
|
67
|
+
ctx.plugin(SessionStore)
|
|
68
|
+
ctx.plugin(TokenMeter)
|
|
69
|
+
ctx.plugin(BasicCompactionEngine)
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
加载插件会注册 `ctx.compaction`。在该插件之前添加同级 [`dsh-compaction-tool-result-pruner`](../compaction-tool-result-pruner/README.zh.md) 以启用可选的不依赖模型的处理阶段。当 `auto: true`(默认)时,它会在 token 压力下自动压缩。同级 [`dsh-command-compact`](../command-compact/README.zh.md) 调用 `ctx.compaction.compactNow(...)`;编程调用方也可以直接使用任一 seam 操作。
|
|
74
|
+
|
|
75
|
+
例如,同一个压缩插件可以安全服务于容量不同的模型,并应用一项目标特定策略:
|
|
76
|
+
|
|
77
|
+
```yaml
|
|
78
|
+
- name: '@deepseek-ai/dsh-compaction-basic'
|
|
79
|
+
config:
|
|
80
|
+
thresholdRatio: 0.8
|
|
81
|
+
retainRatio: 0.16
|
|
82
|
+
modelPolicies:
|
|
83
|
+
- provider: local
|
|
84
|
+
model: small-context
|
|
85
|
+
thresholdRatio: 0.7
|
|
86
|
+
retainTokens: 2048
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## 模型体验
|
|
90
|
+
|
|
91
|
+
### 会话历史
|
|
92
|
+
|
|
93
|
+
#### 模型看到的内容
|
|
94
|
+
|
|
95
|
+
成功步骤越过阈值后,如果已加载可选 pruner,超大工具结果会先被改写。如果仍需摘要,下一个请求会收到下方检查点前导、一个空行、`<compacted-summary>`、根据数据生成的摘要以及 `</compacted-summary>`。溢出恢复会根据使表层前进的任何替换重建立即重试。检查点会替换已选较早范围,后面跟随已保留的近期单元。
|
|
96
|
+
|
|
97
|
+
##### 会话检查点前导
|
|
98
|
+
|
|
99
|
+
```markdown
|
|
100
|
+
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.
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### Token 影响
|
|
104
|
+
|
|
105
|
+
不依赖模型的剪枝可以完全避免辅助调用;否则它会在摘要替换较早范围之前缩减该调用的 transcript(文本记录)。替换会缩减未来输入历史,而非追加第二份副本。摘要会保留到后续压缩将其替换,但不可分的非工具单元仍可能超出预算。
|
|
106
|
+
|
|
107
|
+
#### KV Cache 影响
|
|
108
|
+
|
|
109
|
+
它是替换,而非仅追加。每个检查点都会使从第一个已替换历史 token 起的复用失效;该范围之前未更改的请求前缀仍可复用。
|
|
110
|
+
|
|
111
|
+
### 辅助摘要器请求
|
|
112
|
+
|
|
113
|
+
#### 模型看到的内容
|
|
114
|
+
|
|
115
|
+
完整请求能够装入时,摘要模型会接收逐字回放的会话:与上次已路由请求为已遮蔽区域发送的相同系统提示词、工具 schema 与消息,后面跟随一条最终 user 消息,即下方压缩指令。Hierarchy 中,每个 map 请求接收相同系统提示词、一个按顺序且工具配对平衡的源 span 与结构化 map 指令;reduce 请求接收按顺序排列的 `<partial-summary>` frame 和结构化 reduce 指令。只有 `replayTools: true` 时,工具 schema 才会随 hierarchy 调用发送。会话模型绝不会看到这些私有请求或其推理;只有最终文本会被存储。
|
|
116
|
+
|
|
117
|
+
##### 压缩指令(最终 user 消息)
|
|
118
|
+
|
|
119
|
+
```markdown
|
|
120
|
+
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.
|
|
121
|
+
|
|
122
|
+
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.
|
|
123
|
+
|
|
124
|
+
## Primary Request and Intent
|
|
125
|
+
- [the user's original and evolving goals; quote verbatim where the exact wording matters]
|
|
126
|
+
|
|
127
|
+
## Key Technical Concepts
|
|
128
|
+
- [technologies, frameworks, patterns, and conventions in play]
|
|
129
|
+
|
|
130
|
+
## Files and Code
|
|
131
|
+
- [exact path: why it matters, key changes or snippets]
|
|
132
|
+
|
|
133
|
+
## Errors and Fixes
|
|
134
|
+
- [error: how it was resolved, plus any related user feedback]
|
|
135
|
+
|
|
136
|
+
## Pending Jobs
|
|
137
|
+
- [explicitly requested work not yet completed]
|
|
138
|
+
|
|
139
|
+
## Current Work
|
|
140
|
+
- [precisely what was in progress at this checkpoint]
|
|
141
|
+
|
|
142
|
+
## Next Step
|
|
143
|
+
- [the single next action, directly in line with the most recent request, or "(none)"]
|
|
144
|
+
|
|
145
|
+
## Critical Context
|
|
146
|
+
- [decisions and their rationale, constraints, user preferences, open questions, data needed to continue]
|
|
147
|
+
|
|
148
|
+
Rules:
|
|
149
|
+
- Write concise English engineering prose. Preserve exact file paths, commands, error strings, identifiers, numeric values, function signatures, and syntax fragments.
|
|
150
|
+
- Capture user feedback and explicit instructions faithfully, especially corrections.
|
|
151
|
+
- Do NOT mention this summarization request or that the context was compacted.
|
|
152
|
+
- Output only the checkpoint text: do not call any tool or take any other action.
|
|
153
|
+
- 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.
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### Token 影响
|
|
157
|
+
|
|
158
|
+
能够装入的输入会产生一次独立模型调用:输入是已回放会话前缀加固定指令,输出受 `maxTokens` 限制。Hierarchy 每个 map span 产生一次调用,并再产生一次或多次 reduction 调用,分别受 `mapMaxTokens` 与 `reduceMaxTokens` 限制;Provider 已确认的溢出在局部二分前还可能增加失败尝试。收敛重试可能多次支付任一种成本。
|
|
159
|
+
|
|
160
|
+
#### KV Cache 影响
|
|
161
|
+
|
|
162
|
+
能够装入的 one-shot 请求与会话已回放系统提示词、工具和已遮蔽区域消息逐字匹配,因此提供方的热前缀 cache 可复用至尾随指令之前。路由到另一个模型或压缩非头部范围会放弃该复用。Hierarchy 为保证每次调用有界,无法保留一个完整热前缀:Provider 允许时,map 调用仍可复用其前导系统/消息前缀,而 reduce 调用处理新生成的 partial。`replayTools: false` 还会省略工具 schema 前缀,为源消息留出更多空间。
|
|
163
|
+
|
|
164
|
+
## 已知限制与暂缓事项
|
|
165
|
+
|
|
166
|
+
- **计量准确度取决于固定启发式规则**:可复用提供方用量缺失时,会回退到字符数加结构开销,而非精确的 token 化。
|
|
167
|
+
- **溢出分类由适配器维护**:提供方措辞可能改变;两个 DeepSeek 适配器将当前可识别的上下文限制失败规范化为 `CONTEXT_WINDOW_EXCEEDED`。
|
|
168
|
+
- **有界恢复需要摘要模型容量元数据**:省略 `contextWindow` 的适配器会保留旧 one-shot 路径。如果该请求成功,行为不变;如果它溢出,hierarchy 无法推导安全 chunk 预算,并会以可操作的容量错误失败。
|
|
169
|
+
- **Hierarchy 输出是严格检查点协议**:每个 map 和 reduce stage 都必须返回所有必需标题。截断、视觉输出、结构错误、耗尽 `maxDepth`,或仍然溢出的不可分源/partial,都会使完整压缩事务失败,不会安装部分检查点。
|
|
170
|
+
- **部分不可分单元与仅 envelope 溢出仍不在表层压缩范围内**:恢复无法缩减系统/工具/前缀、拆分不可分的非工具节点,或修复不可剪枝剩余部分仍超出窗口的工具单元。可选 pruner 可以缩减原本不可分工具对内的文本型工具结果主体。
|
|
171
|
+
- **`compactRegion` 要求存在未结束的轮次**:在完全关闭的会话上手动调用会抛出异常(「no open turn」),而不是执行压缩。
|
|
172
|
+
- **摘要失败会保留最新持久表层**:任何替换前,自动路径会记录警告,并携带完整超预算历史继续。如果剪枝已落地,后续摘要失败会从该持久剪枝表层继续。因达到 `maxTokens` 而发生的摘要截断(隐藏推理 token 可能会耗尽该额度)遵循同一规则。
|