@magic6/dsh-compaction-policy-engine 0.5.4 → 0.5.6

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/CHANGELOG.md CHANGED
@@ -4,6 +4,26 @@ All notable changes to this project are documented here. The format follows
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
5
5
  [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## [0.5.6] - 2026-11-25
8
+
9
+ ### Fixed
10
+
11
+ - Two tests asserted POSIX path literals against `path.join` output and failed on
12
+ every Windows run (`\tmp\cc-home-probe\…` never starts with `/tmp/cc-home-probe/`).
13
+ The product code was correct; the assertions were the platform-specific part.
14
+ They now build the expected path with `join`/`sep`, and the suite passes on
15
+ Windows for the first time (198/198). CI is `ubuntu-latest` only, which is why
16
+ this went unnoticed while the plugin was being used on Windows.
17
+
18
+ ## [0.5.5] - 2026-11-22
19
+
20
+ ### Fixed
21
+
22
+ - Host compatibility range now includes DSH `0.2.1-rc.x` as well as `0.2.2-rc.x`.
23
+ DSH 0.2.1 rejects `engines.dsh >=0.2.2-0`, so the previous manifest blocked
24
+ installation even though the plugin runs on 0.2.1.
25
+ - README: state the supported DSH versions.
26
+
7
27
  ## [0.5.4] - 2026-11-16
8
28
 
9
29
  ### Fixed
package/README.en.md ADDED
@@ -0,0 +1,354 @@
1
+ # dsh-compaction-policy-engine
2
+
3
+ **English** | [简体中文](https://github.com/T-splines/dsh-compaction-policy-engine/blob/main/README.md)
4
+
5
+ **Per-model context compaction policy for DeepSeek Harness — three-layer thresholds, live reload and summarization-model selection, on top of the official compaction engine.**
6
+
7
+ The official `@deepseek-ai/dsh-compaction-basic` already supports a threshold, a
8
+ summarization model, and exact per-`(provider, model)` overrides through `modelPolicies`.
9
+ This plugin subclasses that engine and adds the layer above it:
10
+
11
+ - **provider-level layers** — one rule for every model behind a provider, instead of
12
+ enumerating each model id;
13
+ - **field-by-field inheritance** — a model can override `threshold` while inheriting
14
+ `retain`, `maxTokens` and the summarizer from its provider or the global layer;
15
+ - **ratio or absolute-token units**, converted against the route's declared window;
16
+ - **live reload plus a settings panel and session badge**;
17
+ - **a summarization fallback chain** and a context-policy prompt.
18
+
19
+ Region selection, tool-pairing balance, summary framing and the durable compaction
20
+ transaction remain the official implementation. If you only need a few exact
21
+ `(provider, model)` overrides, the official `modelPolicies` already covers you; this plugin
22
+ is for layered policy across many routes.
23
+
24
+ - [Why this exists](#why-this-exists)
25
+ - [What makes it different](#what-makes-it-different)
26
+ - [Install and mount](#install-and-mount)
27
+ - [Configuration](#configuration)
28
+ - [How policy is resolved](#how-policy-is-resolved)
29
+ - [Settings panel](#settings-panel)
30
+ - [Behavior guarantees](#behavior-guarantees)
31
+ - [Tests](#tests)
32
+ - [Compatibility and migration](#compatibility-and-migration)
33
+
34
+ ## Why this exists
35
+
36
+ The official engine's `modelPolicies` are exact `(provider, model)` pairs. When one gateway
37
+ exposes many model ids, or when several routes share the same policy, enumerating every pair
38
+ is brittle: a newly added model silently falls back to the global threshold until someone
39
+ adds it. This plugin lets one provider-level row cover every model behind that provider, and
40
+ resolves `models[] → providers{} → default` field by field.
41
+
42
+ ```
43
+ models[] → providers{} → default
44
+ ```
45
+
46
+ A model can override `threshold` while still inheriting `retain`, `maxTokens`,
47
+ `headroomTokens` and the summarization route from the provider or global layer.
48
+
49
+ ## What makes it different
50
+
51
+ - **Three-layer policy, per-field.** `models[]` → `providers{}` → `default`. Changing one
52
+ field does not require copying the whole layer, and one provider row covers every model
53
+ behind that provider.
54
+ - **Ratio or absolute tokens.** `threshold` / `retain` are ratios of the route's declared
55
+ window; `thresholdTokens` / `retainTokens` are absolute budgets. The plugin converts
56
+ exactly back into the parent engine's ratio (`(tokens + 0.5) / window`) and **refuses to
57
+ guess** when a route declares no window, naming the route in the error.
58
+ - **Live reload.** The engine polls `settings.yaml` and the `compaction-control-settings`
59
+ row every second; a policy edit applies to the next compaction without restarting DSH.
60
+ The panel writes the same document. The engine mount row's own `config:` is read at load
61
+ (see [Install and mount](#install-and-mount)).
62
+ - **Partial settings document.** The `compaction-control:` section in `settings.yaml` is
63
+ merged **per field** with the mount config (maps recurse, arrays replace wholesale), so a
64
+ panel save cannot silently erase a `models[]` layer written in the profile.
65
+ - **Settings panel.** Edit the global layer or a provider layer, switch units without
66
+ leaving both keys behind, see warnings for an un-compactable gap, and see the session
67
+ badge's current trigger point and the route that actually wrote the last summary.
68
+ - **Summarization fallback chain.** `summarizationFallback` lists routes to try when the
69
+ primary summarizer fails, so one broken summarizer route does not disable compaction.
70
+ - **Context-policy prompt.** The companion registers a prompt section telling the model that
71
+ context management is automatic and that it cannot measure its own usage — preventing the
72
+ "I think my context is nearly full" refusal loop observed in long sessions.
73
+ - **Manual `/compact` is durable.** A client disconnect or reconnect does not kill a 20–40 s
74
+ compaction transaction; the command reports the real reason when it cannot start.
75
+ - **Tested.** 198 `node --test` cases across 13 files, including a real-bundle client
76
+ evaluation, policy resolution, unit conversion, patch-file parsing and manual-compaction
77
+ retention.
78
+
79
+ ## Install and mount
80
+
81
+ Requirements: DSH `0.2.1-rc.x` / `0.2.2-rc.x`, Node.js 20+.
82
+
83
+ ### From npm
84
+
85
+ ```bash
86
+ dsh plugin --profile web add @magic6/dsh-compaction-policy-engine
87
+ ```
88
+
89
+ The npm package includes `examples/standard-with-policy` for reference; the preset override
90
+ rules below still apply. The `$PWD` commands in the web section assume a source checkout.
91
+
92
+ ### From source
93
+
94
+ ```bash
95
+ git clone https://github.com/T-splines/dsh-compaction-policy-engine.git
96
+ cd dsh-compaction-policy-engine
97
+ dsh plugin --profile web add "$PWD"
98
+ ```
99
+
100
+ The package is a bundle. It mounts:
101
+
102
+ - an **engine row** (`compaction-control`), **disabled by default**, and
103
+ - an **always-enabled companion row** (`compaction-control-settings`) that serves the
104
+ settings card and the context-policy prompt. It deliberately does **not** claim
105
+ `ctx.compaction`.
106
+
107
+ ### Choose the realm first
108
+
109
+ `ctx.compaction` is a **per-realm single-slot seam**. The engine must live in the same realm
110
+ the agent resolves `ctx.compaction` from. Mounting it elsewhere creates a second engine; both
111
+ subscribe to the bubbling `agent/pre-step`, and the lower threshold wins — the symptom is
112
+ "compaction became unexpectedly early after installing the plugin".
113
+
114
+ | profile | where the agent's engine lives | how to mount |
115
+ |:--|:--|:--|
116
+ | `headless` | the host plane (no agent preset) | the profile's `cordis.patch.yml`, overriding the bundle's engine row |
117
+ | `web` | the **agent preset** (`dsh-web-app` disables the host-plane `compaction-basic`) | a preset declaration override; this repo ships an example bundle |
118
+
119
+ #### Headless (host plane)
120
+
121
+ The bundle already inserts `compaction-control` (disabled). Override that row; do **not**
122
+ `insert` a second one:
123
+
124
+ ```yaml
125
+ # $DSH_HOME/profiles/<profile>/cordis.patch.yml
126
+ - id: compaction-basic
127
+ disabled: true
128
+
129
+ - id: compaction-control
130
+ disabled: false
131
+ config:
132
+ default:
133
+ threshold: 0.8
134
+ retain: 0.16
135
+ ```
136
+
137
+ The loader key is **`disabled`**, not `disable`; the typo is silently dropped by the static
138
+ config and looks like "I disabled it, but it is still enabled".
139
+
140
+ #### Web (agent preset)
141
+
142
+ DSH 0.2.2 presets are `@deepseek-ai/dsh-agent-preset` declarations carried by bundle
143
+ patches. The legacy `$DSH_HOME/.agent-presets/<id>/agent.cordis.yml` directory is no longer
144
+ read. The web agent realm isolates `ctx.compaction`, so a host-plane engine is not seen by
145
+ the agent.
146
+
147
+ This repo ships an example bundle that overrides the shipped `preset-standard` and replaces
148
+ `compaction-basic` inside its `compaction` group with `compaction-control` (keeping
149
+ `command-compact` and `tool-result-pruner`):
150
+
151
+ ```bash
152
+ dsh plugin --profile web add "$PWD"
153
+ dsh plugin --profile web add "$PWD/examples/standard-with-policy"
154
+ ```
155
+
156
+ Restart the web app and open a new session. The example is a snapshot of the standard preset
157
+ from DSH 0.2.2. If you have customized the standard preset, merge the `compaction` group into
158
+ your own preset declaration instead of installing the example blindly (Creator mode can
159
+ author that override). The file is
160
+ [`examples/standard-with-policy/cordis.patch.yml`](examples/standard-with-policy/cordis.patch.yml);
161
+ its header says the same.
162
+
163
+ Two silent failure modes are easy to create:
164
+
165
+ - engine on the host plane **and** the stock `compaction-basic` still enabled in the preset
166
+ → two engines, lower threshold wins;
167
+ - stock engine disabled **without** mounting a replacement → the seam is unclaimed and the
168
+ session silently stops compacting until the provider rejects it.
169
+
170
+ Disable the old engine and enable the new one in the **same composition**, reviewed together.
171
+
172
+ ### Live reload scope
173
+
174
+ The settings panel writes `$DSH_HOME/settings.yaml`. The engine polls that file **and** the
175
+ `compaction-control-settings` row every second, so panel edits and edits to that row apply to
176
+ the next compaction without a restart. The **engine mount row** (`compaction-control`) is
177
+ read at plugin load; editing its `config:` requires a restart.
178
+
179
+ ### Threshold semantics
180
+
181
+ The official engine's trigger is
182
+
183
+ ```
184
+ floor(min(W × threshold, W − O − headroomTokens))
185
+ ```
186
+
187
+ where `W` is the route's declared window, `O` the output reservation, and `headroomTokens`
188
+ defaults to 65,536. `measured` **includes the fixed system prompt and tool catalogue**. On a
189
+ deployment with a large tool set, a threshold of 0.5 can mean "compact after roughly 23K
190
+ tokens of conversation", not "compact when the conversation is half full".
191
+
192
+ The plugin's `threshold` / `retain` map to the official `thresholdRatio` / `retainRatio`;
193
+ `headroomTokens` and `maxTokens` are per-layer fields. The default is 0.8, the official
194
+ engine's default, so the plugin never triggers earlier than the stock engine unless a
195
+ headroom cap does. The session badge shows `window × threshold` as a quick preview; because
196
+ `W − O − headroomTokens` can be lower, treat that number as an **upper bound** on when
197
+ compaction fires.
198
+
199
+ ## Configuration
200
+
201
+ The policy can live in the mount row's `config:` or in `$DSH_HOME/settings.yaml` under the
202
+ `compaction-control:` section (the panel edits the latter).
203
+
204
+ ```yaml
205
+ compaction-control:
206
+ # global layer
207
+ default:
208
+ threshold: 0.8 # compact at this fraction of the window
209
+ retain: 0.16 # keep this tail fraction; must be < threshold
210
+ summarizationModel: auto # auto = the session's current route, or "provider/model"
211
+ summarizationFallback: [] # ordered "provider/model" routes to try after the primary
212
+ headroomTokens: 65536 # keep this much free below the threshold
213
+ maxTokens: 8192 # summary length cap
214
+ compactionRetries: 1
215
+ maxOverflowRetries: 1
216
+
217
+ # per-provider override (provider names come from the llm config)
218
+ providers:
219
+ some-provider:
220
+ threshold: 0.5
221
+
222
+ # exact (provider, model) override; highest priority
223
+ models:
224
+ - provider: local
225
+ model: model-262k
226
+ threshold: 0.9
227
+
228
+ # false = disable automatic compaction (manual /compact still works)
229
+ auto: true
230
+ ```
231
+
232
+ ### Two units per budget field
233
+
234
+ Each budget field has a ratio form and an absolute-token form. Use **one form per layer**:
235
+
236
+ | ratio (of the window) | absolute tokens | meaning |
237
+ |:--|:--|:--|
238
+ | `threshold` | `thresholdTokens` | compact when reached |
239
+ | `retain` | `retainTokens` | keep this much recent content |
240
+
241
+ If both forms appear in the same layer, the token form wins and a `[compaction] WARNING`
242
+ names the shadowed key. The whole section is not rejected, because the panel naturally
243
+ stores a token value next to a ratio baseline and silently ignoring it would be worse.
244
+
245
+ ### Other policy fields
246
+
247
+ Every field below is valid in `default`, a `providers{}` layer, or a `models[]` layer:
248
+
249
+ | field | default | meaning |
250
+ |:--|:--|:--|
251
+ | `summarizationModel` | `auto` | `auto` reuses the session's routed model; or an explicit `provider/model` |
252
+ | `summarizationFallback` | `[]` | ordered `provider/model` routes tried when the primary summarizer fails |
253
+ | `headroomTokens` | `65536` (official default) | tokens kept free below the threshold so the summary has room to be smaller |
254
+ | `maxTokens` | official default | cap on the generated summary length |
255
+ | `compactionRetries` | `1` | extra summarization attempts when pressure remains |
256
+ | `maxOverflowRetries` | `1` | retries after a confirmed context-window overflow; `0` disables recovery |
257
+
258
+ `auto` is a root-level switch, not a layer field: `false` disables automatic compaction while
259
+ manual `/compact` still works.
260
+
261
+ **The gap must be large enough.** The summary is a structured summary with a practical lower
262
+ bound (about 3.2K tokens). When `threshold − retain < 8192`, the summary cannot be smaller
263
+ than the content it replaces and the parent engine rejects it. The panel shows this as
264
+ "compactable space" with a warning; small windows may make a small gap unavoidable, so it is
265
+ a warning, not a load failure.
266
+
267
+ ## How policy is resolved
268
+
269
+ - `models[]` → `providers{}` → `default`, **per field**, resolved by
270
+ `(provider, model, window)` and cached. A session that switches models picks up the new
271
+ policy on the next threshold check.
272
+ - The engine reads the route **at the time of the check**, not at session start.
273
+ - A route that declares no context window cannot honor an absolute token budget: the field
274
+ is deferred and the error names the route when the policy is actually needed.
275
+ - Configuration errors (out-of-range ratio, `retain >= threshold`, unknown field, duplicate
276
+ `models[]` target, malformed `summarizationModel`) fail at plugin load with a message
277
+ pointing at the field.
278
+
279
+ ## Settings panel
280
+
281
+ Models page footer → **Compaction policy** card:
282
+
283
+ - choose the layer: global default or a provider route;
284
+ - edit each budget field; the number's shape is the unit (`0.2` ratio, `200000` tokens);
285
+ mixed/invalid values are marked and saving is disabled;
286
+ - switch units without leaving both keys in the document (the replaced key is `unset` in the
287
+ same operation);
288
+ - add/remove a provider override; removal unsets every field in the user layer, including
289
+ fields the card does not render;
290
+ - the session header badge shows the current trigger point (`ratio · tokens`), the layer
291
+ that supplied it (`models[]` / `providers{}` / `default` / engine default), the estimated
292
+ compactable space, and the route that wrote the last summary.
293
+
294
+ `models[]` remains hand-edited in `settings.yaml`: the settings writer treats arrays as
295
+ whole-value replacements, so a list editor would rewrite the whole table on every keystroke.
296
+ The badge still reports when a session hit a `models[]` entry.
297
+
298
+ Non-loopback browsers get DSH's memory-state view (`status: 'unavailable'`,
299
+ `writable: false`); the card says so instead of rendering plugin defaults as if they were
300
+ the deployed policy.
301
+
302
+ ## Behavior guarantees
303
+
304
+ - **Configuration errors fail at startup, not at the first compaction.** The exception is a
305
+ policy that needs the route's window; that fails at the step that actually needs it and
306
+ names the route.
307
+ - **`retain >= threshold` after an override is an error**, not a silent shrink of `retain`.
308
+ - **No semantic hijack.** Region selection, tool pairing and the durable transaction are
309
+ untouched from the official engine.
310
+ - **Manual compaction is persistent work.** `/compact` is not cancelled by a client
311
+ disconnect; only the agent's own maintenance signal can cancel it. Stop still works.
312
+ - **Failures are not misreported.** A busy gate is retried once with a bounded wait; if it
313
+ never releases, the message says the previous activity is still finishing, rather than
314
+ claiming a lock is held.
315
+ - **Context policy.** The companion says: context management is automatic; you cannot
316
+ measure your own usage; never stop or shorten work because you believe context is nearly
317
+ full. The rule is registered only when the profile has a `systemPrompt` service.
318
+
319
+ ## Tests
320
+
321
+ ```bash
322
+ npm test
323
+ ```
324
+
325
+ 198 cases across 13 files, including:
326
+
327
+ - `manual-compaction.test.js` — gate retry, UI signal abort, real failures, honest messages,
328
+ and a contract assertion against the installed official package;
329
+ - `context-policy.test.js` — prompt registration and absence without a `systemPrompt` service;
330
+ - `policy.test.js` — three-layer resolution, per-field merge, both units, conversion,
331
+ warnings, overlay document rules;
332
+ - `engine.test.js` — seam registration, `inject` inheritance, `Config` ownership, per-target
333
+ switching, window conversion, no-window refusal, profile `models[]` surviving a
334
+ `settings.yaml` section;
335
+ - `settings-file.test.js` — patch/settings parsing and immediate edits;
336
+ - `fallback.test.js`, `patch-file.test.js`, `settings.test.js`, `summary-prefix.test.js`,
337
+ `unstated-threshold.test.js` — summarizer fallback, patch entry shapes, settings merge,
338
+ summary framing and the unstated-threshold warning;
339
+ - `client-policy.test.js` / `client.test.js` — panel calculations, unit switching, provider
340
+ layers, session badge, illegal value blocking, inline bundle consistency.
341
+
342
+ ## Compatibility and migration
343
+
344
+ - DSH `0.2.1-rc.x` / `0.2.2-rc.x` (`engines.dsh`: `>=0.2.1-0 <0.3.0-0`), Node.js 20+.
345
+ - **Renamed from `dsh-compaction-control` in 0.5.0.** Entry ids are unchanged:
346
+ `compaction-control` (engine), `compaction-control-settings` (companion), and the
347
+ `compaction-control:` settings namespace. Update the `name:` fields in your profile/preset
348
+ to `@magic6/dsh-compaction-policy-engine` (and `.../engine` for the engine row).
349
+ - The engine is still a subclass of the official `BasicCompactionEngine`; no `node_modules`
350
+ patch is required.
351
+
352
+ ## License
353
+
354
+ MIT. See [LICENSE](LICENSE).
package/README.md CHANGED
@@ -1,95 +1,84 @@
1
1
  # dsh-compaction-policy-engine
2
2
 
3
- **English** | [简体中文](https://github.com/T-splines/dsh-compaction-policy-engine/blob/main/README.zh-CN.md)
4
-
5
- **Per-model context compaction policy for DeepSeek Harness — three-layer thresholds, live reload and summarization-model selection, on top of the official compaction engine.**
6
-
7
- The official `@deepseek-ai/dsh-compaction-basic` already supports a threshold, a
8
- summarization model, and exact per-`(provider, model)` overrides through `modelPolicies`.
9
- This plugin subclasses that engine and adds the layer above it:
10
-
11
- - **provider-level layers** — one rule for every model behind a provider, instead of
12
- enumerating each model id;
13
- - **field-by-field inheritance** — a model can override `threshold` while inheriting
14
- `retain`, `maxTokens` and the summarizer from its provider or the global layer;
15
- - **ratio or absolute-token units**, converted against the route's declared window;
16
- - **live reload plus a settings panel and session badge**;
17
- - **a summarization fallback chain** and a context-policy prompt.
18
-
19
- Region selection, tool-pairing balance, summary framing and the durable compaction
20
- transaction remain the official implementation. If you only need a few exact
21
- `(provider, model)` overrides, the official `modelPolicies` already covers you; this plugin
22
- is for layered policy across many routes.
23
-
24
- - [Why this exists](#why-this-exists)
25
- - [What makes it different](#what-makes-it-different)
26
- - [Install and mount](#install-and-mount)
27
- - [Configuration](#configuration)
28
- - [How policy is resolved](#how-policy-is-resolved)
29
- - [Settings panel](#settings-panel)
30
- - [Behavior guarantees](#behavior-guarantees)
31
- - [Tests](#tests)
32
- - [Compatibility and migration](#compatibility-and-migration)
33
-
34
- ## Why this exists
35
-
36
- The official engine's `modelPolicies` are exact `(provider, model)` pairs. When one gateway
37
- exposes many model ids, or when several routes share the same policy, enumerating every pair
38
- is brittle: a newly added model silently falls back to the global threshold until someone
39
- adds it. This plugin lets one provider-level row cover every model behind that provider, and
40
- resolves `models[] → providers{} → default` field by field.
3
+ [English](https://github.com/T-splines/dsh-compaction-policy-engine/blob/main/README.en.md) | **简体中文**
4
+
5
+ **DeepSeek Harness 的按模型上下文压缩策略——三层阈值、热加载与摘要模型选择,构建在官方压缩引擎之上。**
6
+
7
+ 官方 `@deepseek-ai/dsh-compaction-basic` 已经支持阈值、摘要模型,以及通过 `modelPolicies`
8
+ 给精确 `(provider, model)` 覆盖。本插件是它的子类,在它之上再加一层:
9
+
10
+ - **供应商层**——一条规则覆盖该 provider 后面的所有模型,不必逐个枚举模型 id;
11
+ - **逐字段继承**——模型层只覆盖 `threshold`,`retain`、`maxTokens`、摘要路由仍从供应商层或
12
+ 全局层继承;
13
+ - **比例与绝对 token 双单位**,按路由声明的窗口换算;
14
+ - **热加载 + 设置面板 + 会话徽标**;
15
+ - **摘要回退链**与上下文策略提示词。
16
+
17
+ 选区、工具配对平衡、摘要框架、落盘事务全部沿用官方实现。如果你只需要少量精确
18
+ `(provider, model)` 覆盖,官方 `modelPolicies` 已经够用;本插件面向的是跨很多路由的分层策略。
19
+
20
+ - [为什么需要它](#为什么需要它)
21
+ - [差异点](#差异点)
22
+ - [安装与挂载](#安装与挂载)
23
+ - [配置](#配置)
24
+ - [策略如何解析](#策略如何解析)
25
+ - [设置面板](#设置面板)
26
+ - [行为保证](#行为保证)
27
+ - [测试](#测试)
28
+ - [兼容与迁移](#兼容与迁移)
29
+
30
+ ## 为什么需要它
31
+
32
+ 官方引擎的 `modelPolicies` 是精确 `(provider, model)` 对。当一个网关暴露很多模型 id,或者
33
+ 多条路由共享同一套策略时,逐个枚举既啰嗦又脆弱:新加的模型会静默落回全局阈值,直到有人补上
34
+ 一行。本插件允许一条供应商层规则覆盖该 provider 的所有模型,并按
35
+ `models[] → providers{} → default` 逐字段解析。
41
36
 
42
37
  ```
43
38
  models[] → providers{} → default
44
39
  ```
45
40
 
46
- A model can override `threshold` while still inheriting `retain`, `maxTokens`,
47
- `headroomTokens` and the summarization route from the provider or global layer.
48
-
49
- ## What makes it different
50
-
51
- - **Three-layer policy, per-field.** `models[]` → `providers{}` → `default`. Changing one
52
- field does not require copying the whole layer, and one provider row covers every model
53
- behind that provider.
54
- - **Ratio or absolute tokens.** `threshold` / `retain` are ratios of the route's declared
55
- window; `thresholdTokens` / `retainTokens` are absolute budgets. The plugin converts
56
- exactly back into the parent engine's ratio (`(tokens + 0.5) / window`) and **refuses to
57
- guess** when a route declares no window, naming the route in the error.
58
- - **Live reload.** The engine polls `settings.yaml` and the `compaction-control-settings`
59
- row every second; a policy edit applies to the next compaction without restarting DSH.
60
- The panel writes the same document. The engine mount row's own `config:` is read at load
61
- (see [Install and mount](#install-and-mount)).
62
- - **Partial settings document.** The `compaction-control:` section in `settings.yaml` is
63
- merged **per field** with the mount config (maps recurse, arrays replace wholesale), so a
64
- panel save cannot silently erase a `models[]` layer written in the profile.
65
- - **Settings panel.** Edit the global layer or a provider layer, switch units without
66
- leaving both keys behind, see warnings for an un-compactable gap, and see the session
67
- badge's current trigger point and the route that actually wrote the last summary.
68
- - **Summarization fallback chain.** `summarizationFallback` lists routes to try when the
69
- primary summarizer fails, so one broken summarizer route does not disable compaction.
70
- - **Context-policy prompt.** The companion registers a prompt section telling the model that
71
- context management is automatic and that it cannot measure its own usage — preventing the
72
- "I think my context is nearly full" refusal loop observed in long sessions.
73
- - **Manual `/compact` is durable.** A client disconnect or reconnect does not kill a 20–40 s
74
- compaction transaction; the command reports the real reason when it cannot start.
75
- - **Tested.** 198 `node --test` cases across 13 files, including a real-bundle client
76
- evaluation, policy resolution, unit conversion, patch-file parsing and manual-compaction
77
- retention.
78
-
79
- ## Install and mount
80
-
81
- Requirements: DSH `0.2.2-rc.x`, Node.js 20+.
82
-
83
- ### From npm
41
+ 模型层可以只覆盖 `threshold`,`retain`、`maxTokens`、`headroomTokens` 和摘要路由继续从供应商
42
+ 层或全局层继承。
43
+
44
+ ## 差异点
45
+
46
+ - **三层策略、逐字段。** `models[]` → `providers{}` → `default`;改一个字段不需要抄整层,
47
+ 一条供应商行覆盖该 provider 后面的所有模型。
48
+ - **比例或绝对 token 两种单位。** `threshold` / `retain` 按路由声明的窗口换算;
49
+ `thresholdTokens` / `retainTokens` 是绝对预算。换算写回父类时使用
50
+ `(tokens + 0.5) / window`,保证精确回到声明的数;路由没有声明窗口时**拒绝猜**,
51
+ 在真正需要策略的那一步报错并点名路由。
52
+ - **热加载。** 引擎每秒轮询 `settings.yaml` 与 `compaction-control-settings` 行;策略修改在
53
+ 下一次压缩生效,不需要重启。面板写的是同一份文档。引擎挂载行自己的 `config:` 在加载时读取
54
+ (见[安装与挂载](#安装与挂载))。
55
+ - **部分文档合并。** `settings.yaml` 的 `compaction-control:` 段与挂载行 `config:` **逐字段**
56
+ 合并(映射递归、数组整体替换),面板保存不会悄悄抹掉 profile 里的 `models[]` 层。
57
+ - **设置面板。** 编辑全局层或某个供应商层,切换单位时不留下两个键,空隙过小时给出红字提示;
58
+ 会话头徽标显示当前触发点和「上一次摘要实际由哪条路由写」。
59
+ - **摘要回退链。** `summarizationFallback` 在主摘要路由失败时按序尝试其它路由,避免一条坏
60
+ 路由让整个压缩停摆。
61
+ - **上下文策略提示词。** companion 注册一段系统提示:上下文管理是自动的,模型无法测量自己的
62
+ 用量,不得因为自认为「上下文快满了」而停止、缩短或移交工作。
63
+ - **手动 `/compact` 是持久工作。** 客户端断线/重连不会杀死一次 20–40 秒的压缩事务;无法开始
64
+ 时如实报告原因,不把空锁说成「已有压缩在跑」。
65
+ - **有测试。** 198 个 `node --test` 用例,分布在 13 个文件,覆盖真实 bundle 渲染、三层解析、
66
+ 单位换算、补丁文件解析和手动压缩保留量。
67
+
68
+ ## 安装与挂载
69
+
70
+ 要求:DSH `0.2.1-rc.x` / `0.2.2-rc.x`、Node.js 20+。
71
+
72
+ ### 从 npm 安装
84
73
 
85
74
  ```bash
86
75
  dsh plugin --profile web add @magic6/dsh-compaction-policy-engine
87
76
  ```
88
77
 
89
- The npm package includes `examples/standard-with-policy` for reference; the preset override
90
- rules below still apply. The `$PWD` commands in the web section assume a source checkout.
78
+ npm 包里包含 `examples/standard-with-policy` 供参考;下面的 preset 覆盖规则仍然适用。
79
+ web 小节里的 `$PWD` 命令假设使用源码检出。
91
80
 
92
- ### From source
81
+ ### 从源码安装
93
82
 
94
83
  ```bash
95
84
  git clone https://github.com/T-splines/dsh-compaction-policy-engine.git
@@ -97,29 +86,26 @@ cd dsh-compaction-policy-engine
97
86
  dsh plugin --profile web add "$PWD"
98
87
  ```
99
88
 
100
- The package is a bundle. It mounts:
89
+ 包本身是 bundle,会挂两行:
101
90
 
102
- - an **engine row** (`compaction-control`), **disabled by default**, and
103
- - an **always-enabled companion row** (`compaction-control-settings`) that serves the
104
- settings card and the context-policy prompt. It deliberately does **not** claim
105
- `ctx.compaction`.
91
+ - **引擎行**(`compaction-control`),**默认 disabled**;
92
+ - **companion 行**(`compaction-control-settings`),始终启用,负责设置卡片与上下文提示词;
93
+ 它**不**认领 `ctx.compaction`。
106
94
 
107
- ### Choose the realm first
95
+ ### 先确定挂载平面
108
96
 
109
- `ctx.compaction` is a **per-realm single-slot seam**. The engine must live in the same realm
110
- the agent resolves `ctx.compaction` from. Mounting it elsewhere creates a second engine; both
111
- subscribe to the bubbling `agent/pre-step`, and the lower threshold wins — the symptom is
112
- "compaction became unexpectedly early after installing the plugin".
97
+ `ctx.compaction` 是**按 realm 解析的单槽接缝**。必须挂在 agent 实际解析到的作用域;挂在别处
98
+ 不是「覆盖」而是「并存」——两个引擎都订阅冒泡的 `agent/pre-step`,触发点变成两者中较低的那个,
99
+ 表现为「装完插件后压缩异常地早」。
113
100
 
114
- | profile | where the agent's engine lives | how to mount |
101
+ | profile | agent 的压缩引擎属于谁 | 怎么挂 |
115
102
  |:--|:--|:--|
116
- | `headless` | the host plane (no agent preset) | the profile's `cordis.patch.yml`, overriding the bundle's engine row |
117
- | `web` | the **agent preset** (`dsh-web-app` disables the host-plane `compaction-basic`) | a preset declaration override; this repo ships an example bundle |
103
+ | `headless` | 宿主平面(没有 preset) | profile 的 `cordis.patch.yml`,覆盖 bundle 已插入的引擎行 |
104
+ | `web` | **agent preset**(`dsh-web-app` 停用了宿主平面的 `compaction-basic`) | 覆盖 preset 声明;本仓库带一个示例 bundle |
118
105
 
119
- #### Headless (host plane)
106
+ #### headless(宿主平面)
120
107
 
121
- The bundle already inserts `compaction-control` (disabled). Override that row; do **not**
122
- `insert` a second one:
108
+ bundle 已经插入 `compaction-control`(disabled)。**覆盖这一行**,不要再 `insert` 一份:
123
109
 
124
110
  ```yaml
125
111
  # $DSH_HOME/profiles/<profile>/cordis.patch.yml
@@ -134,221 +120,189 @@ The bundle already inserts `compaction-control` (disabled). Override that row; d
134
120
  retain: 0.16
135
121
  ```
136
122
 
137
- The loader key is **`disabled`**, not `disable`; the typo is silently dropped by the static
138
- config and looks like "I disabled it, but it is still enabled".
123
+ loader 认的键是 **`disabled`**,不是 `disable`;写错不会报错,会被静态配置静默丢掉
124
+ (看起来停用了,其实还启用)。
139
125
 
140
- #### Web (agent preset)
126
+ #### web(agent preset)
141
127
 
142
- DSH 0.2.2 presets are `@deepseek-ai/dsh-agent-preset` declarations carried by bundle
143
- patches. The legacy `$DSH_HOME/.agent-presets/<id>/agent.cordis.yml` directory is no longer
144
- read. The web agent realm isolates `ctx.compaction`, so a host-plane engine is not seen by
145
- the agent.
128
+ DSH 0.2.2 的 preset 是 bundle patch 里的 `@deepseek-ai/dsh-agent-preset` 声明。旧目录
129
+ `$DSH_HOME/.agent-presets/<id>/agent.cordis.yml` 已经不再被读取。web 的 agent realm 隔离了
130
+ `ctx.compaction`,宿主平面的引擎它看不到。
146
131
 
147
- This repo ships an example bundle that overrides the shipped `preset-standard` and replaces
148
- `compaction-basic` inside its `compaction` group with `compaction-control` (keeping
149
- `command-compact` and `tool-result-pruner`):
132
+ 本仓库带一个示例 bundle:覆盖 `preset-standard`,把 `compaction` 组里的 `compaction-basic`
133
+ 换成 `compaction-control`,并保留 `command-compact` 与 `tool-result-pruner`:
150
134
 
151
135
  ```bash
152
136
  dsh plugin --profile web add "$PWD"
153
137
  dsh plugin --profile web add "$PWD/examples/standard-with-policy"
154
138
  ```
155
139
 
156
- Restart the web app and open a new session. The example is a snapshot of the standard preset
157
- from DSH 0.2.2. If you have customized the standard preset, merge the `compaction` group into
158
- your own preset declaration instead of installing the example blindly (Creator mode can
159
- author that override). The file is
160
- [`examples/standard-with-policy/cordis.patch.yml`](examples/standard-with-policy/cordis.patch.yml);
161
- its header says the same.
140
+ 重启 web app,新开会话验证。示例是 DSH 0.2.2 标准 preset 的快照;如果你改过标准 preset,请把
141
+ `compaction` 组的手动合并进你自己的 preset 声明,而不是盲目安装示例(Creator 模式可以代写
142
+ 这个覆盖)。文件在
143
+ [`examples/standard-with-policy/cordis.patch.yml`](examples/standard-with-policy/cordis.patch.yml),
144
+ 头部也写了同样的提醒。
162
145
 
163
- Two silent failure modes are easy to create:
146
+ 两种静默坏组合:
164
147
 
165
- - engine on the host plane **and** the stock `compaction-basic` still enabled in the preset
166
- → two engines, lower threshold wins;
167
- - stock engine disabled **without** mounting a replacement → the seam is unclaimed and the
168
- session silently stops compacting until the provider rejects it.
148
+ - 宿主平面挂了引擎,preset 里原版 `compaction-basic` 还在 → 两个引擎,阈值低的赢;
149
+ - 原版停用了却没有挂任何替代引擎 → 接缝无人认领,会话静默停止压缩,一路涨到被上游拒绝。
169
150
 
170
- Disable the old engine and enable the new one in the **same composition**, reviewed together.
151
+ 「停用旧的」和「启用新的」必须写在同一个组合里,一起 review。
171
152
 
172
- ### Live reload scope
153
+ ### 热加载范围
173
154
 
174
- The settings panel writes `$DSH_HOME/settings.yaml`. The engine polls that file **and** the
175
- `compaction-control-settings` row every second, so panel edits and edits to that row apply to
176
- the next compaction without a restart. The **engine mount row** (`compaction-control`) is
177
- read at plugin load; editing its `config:` requires a restart.
155
+ 设置面板写 `$DSH_HOME/settings.yaml`。引擎每秒轮询该文件**以及** `compaction-control-settings`
156
+ 行,所以面板修改和这一行的修改会在下一次压缩生效,不需要重启。**引擎挂载行**
157
+ (`compaction-control`)的 `config:` 在插件加载时读取,改它需要重启。
178
158
 
179
- ### Threshold semantics
159
+ ### 阈值语义
180
160
 
181
- The official engine's trigger is
161
+ 官方引擎的触发点是
182
162
 
183
163
  ```
184
164
  floor(min(W × threshold, W − O − headroomTokens))
185
165
  ```
186
166
 
187
- where `W` is the route's declared window, `O` the output reservation, and `headroomTokens`
188
- defaults to 65,536. `measured` **includes the fixed system prompt and tool catalogue**. On a
189
- deployment with a large tool set, a threshold of 0.5 can mean "compact after roughly 23K
190
- tokens of conversation", not "compact when the conversation is half full".
167
+ 其中 `W` 是路由声明的窗口,`O` 是输出预留,`headroomTokens` 默认 65,536。`measured`
168
+ **包含固定的系统提示词和工具清单**。在工具很多的部署里,`threshold: 0.5` 可能意味着
169
+ 「系统提示词之外再攒约 23K 就压」,不是「对话到一半才压」。
191
170
 
192
- The plugin's `threshold` / `retain` map to the official `thresholdRatio` / `retainRatio`;
193
- `headroomTokens` and `maxTokens` are per-layer fields. The default is 0.8, the official
194
- engine's default, so the plugin never triggers earlier than the stock engine unless a
195
- headroom cap does. The session badge shows `window × threshold` as a quick preview; because
196
- `W − O − headroomTokens` can be lower, treat that number as an **upper bound** on when
197
- compaction fires.
171
+ 本插件的 `threshold` / `retain` 映射到官方的 `thresholdRatio` / `retainRatio`;
172
+ `headroomTokens` 与 `maxTokens` 是逐层字段。默认 0.8 是官方引擎默认值,除非 headroom 上限
173
+ 先到,否则不会比原版更早触发。会话头徽标显示的是 `窗口 × threshold` 的快速预览;因为
174
+ `W − O − headroomTokens` 可能更低,把它当作触发点的**上界**。
198
175
 
199
- ## Configuration
176
+ ## 配置
200
177
 
201
- The policy can live in the mount row's `config:` or in `$DSH_HOME/settings.yaml` under the
202
- `compaction-control:` section (the panel edits the latter).
178
+ 策略可以写在挂载行的 `config:`,也可以写在 `$DSH_HOME/settings.yaml` 的
179
+ `compaction-control:` 段(面板编辑后者)。
203
180
 
204
181
  ```yaml
205
182
  compaction-control:
206
- # global layer
207
183
  default:
208
- threshold: 0.8 # compact at this fraction of the window
209
- retain: 0.16 # keep this tail fraction; must be < threshold
210
- summarizationModel: auto # auto = the session's current route, or "provider/model"
211
- summarizationFallback: [] # ordered "provider/model" routes to try after the primary
212
- headroomTokens: 65536 # keep this much free below the threshold
213
- maxTokens: 8192 # summary length cap
184
+ threshold: 0.8 # 到达窗口的这个比例就压
185
+ retain: 0.16 # 原样保留的尾部比例,必须小于 threshold
186
+ summarizationModel: auto # auto = 用会话当前路由的模型,或 "provider/model"
187
+ summarizationFallback: [] # 主摘要失败后按序尝试的 "provider/model" 路由
188
+ headroomTokens: 65536 # 阈值下方预留的空间
189
+ maxTokens: 8192 # 摘要长度上限
214
190
  compactionRetries: 1
215
191
  maxOverflowRetries: 1
216
192
 
217
- # per-provider override (provider names come from the llm config)
218
193
  providers:
219
194
  some-provider:
220
195
  threshold: 0.5
221
196
 
222
- # exact (provider, model) override; highest priority
223
197
  models:
224
198
  - provider: local
225
199
  model: model-262k
226
200
  threshold: 0.9
227
201
 
228
- # false = disable automatic compaction (manual /compact still works)
229
202
  auto: true
230
203
  ```
231
204
 
232
- ### Two units per budget field
205
+ ### 两种单位
233
206
 
234
- Each budget field has a ratio form and an absolute-token form. Use **one form per layer**:
207
+ 每个预算字段都有比例与绝对 token 两种写法,**同一层只能写一种**:
235
208
 
236
- | ratio (of the window) | absolute tokens | meaning |
209
+ | 比例 | 绝对 token | 语义 |
237
210
  |:--|:--|:--|
238
- | `threshold` | `thresholdTokens` | compact when reached |
239
- | `retain` | `retainTokens` | keep this much recent content |
211
+ | `threshold` | `thresholdTokens` | 到达这个量就压 |
212
+ | `retain` | `retainTokens` | 保留最近这么多内容 |
240
213
 
241
- If both forms appear in the same layer, the token form wins and a `[compaction] WARNING`
242
- names the shadowed key. The whole section is not rejected, because the panel naturally
243
- stores a token value next to a ratio baseline and silently ignoring it would be worse.
214
+ 同一层两种都写时 token 形式生效,并打一行 `[compaction] WARNING` 点名被遮蔽的键。整段拒绝
215
+ 会让面板刚保存的值看起来被静默忽略,所以选择「生效 + 告警」。
244
216
 
245
- ### Other policy fields
217
+ ### 其它策略字段
246
218
 
247
- Every field below is valid in `default`, a `providers{}` layer, or a `models[]` layer:
219
+ 以下字段在 `default`、`providers{}` 层或 `models[]` 层都有效:
248
220
 
249
- | field | default | meaning |
221
+ | 字段 | 默认值 | 含义 |
250
222
  |:--|:--|:--|
251
- | `summarizationModel` | `auto` | `auto` reuses the session's routed model; or an explicit `provider/model` |
252
- | `summarizationFallback` | `[]` | ordered `provider/model` routes tried when the primary summarizer fails |
253
- | `headroomTokens` | `65536` (official default) | tokens kept free below the threshold so the summary has room to be smaller |
254
- | `maxTokens` | official default | cap on the generated summary length |
255
- | `compactionRetries` | `1` | extra summarization attempts when pressure remains |
256
- | `maxOverflowRetries` | `1` | retries after a confirmed context-window overflow; `0` disables recovery |
257
-
258
- `auto` is a root-level switch, not a layer field: `false` disables automatic compaction while
259
- manual `/compact` still works.
260
-
261
- **The gap must be large enough.** The summary is a structured summary with a practical lower
262
- bound (about 3.2K tokens). When `threshold − retain < 8192`, the summary cannot be smaller
263
- than the content it replaces and the parent engine rejects it. The panel shows this as
264
- "compactable space" with a warning; small windows may make a small gap unavoidable, so it is
265
- a warning, not a load failure.
266
-
267
- ## How policy is resolved
268
-
269
- - `models[]` → `providers{}` → `default`, **per field**, resolved by
270
- `(provider, model, window)` and cached. A session that switches models picks up the new
271
- policy on the next threshold check.
272
- - The engine reads the route **at the time of the check**, not at session start.
273
- - A route that declares no context window cannot honor an absolute token budget: the field
274
- is deferred and the error names the route when the policy is actually needed.
275
- - Configuration errors (out-of-range ratio, `retain >= threshold`, unknown field, duplicate
276
- `models[]` target, malformed `summarizationModel`) fail at plugin load with a message
277
- pointing at the field.
278
-
279
- ## Settings panel
280
-
281
- Models page footer → **Compaction policy** card:
282
-
283
- - choose the layer: global default or a provider route;
284
- - edit each budget field; the number's shape is the unit (`0.2` ratio, `200000` tokens);
285
- mixed/invalid values are marked and saving is disabled;
286
- - switch units without leaving both keys in the document (the replaced key is `unset` in the
287
- same operation);
288
- - add/remove a provider override; removal unsets every field in the user layer, including
289
- fields the card does not render;
290
- - the session header badge shows the current trigger point (`ratio · tokens`), the layer
291
- that supplied it (`models[]` / `providers{}` / `default` / engine default), the estimated
292
- compactable space, and the route that wrote the last summary.
293
-
294
- `models[]` remains hand-edited in `settings.yaml`: the settings writer treats arrays as
295
- whole-value replacements, so a list editor would rewrite the whole table on every keystroke.
296
- The badge still reports when a session hit a `models[]` entry.
297
-
298
- Non-loopback browsers get DSH's memory-state view (`status: 'unavailable'`,
299
- `writable: false`); the card says so instead of rendering plugin defaults as if they were
300
- the deployed policy.
301
-
302
- ## Behavior guarantees
303
-
304
- - **Configuration errors fail at startup, not at the first compaction.** The exception is a
305
- policy that needs the route's window; that fails at the step that actually needs it and
306
- names the route.
307
- - **`retain >= threshold` after an override is an error**, not a silent shrink of `retain`.
308
- - **No semantic hijack.** Region selection, tool pairing and the durable transaction are
309
- untouched from the official engine.
310
- - **Manual compaction is persistent work.** `/compact` is not cancelled by a client
311
- disconnect; only the agent's own maintenance signal can cancel it. Stop still works.
312
- - **Failures are not misreported.** A busy gate is retried once with a bounded wait; if it
313
- never releases, the message says the previous activity is still finishing, rather than
314
- claiming a lock is held.
315
- - **Context policy.** The companion says: context management is automatic; you cannot
316
- measure your own usage; never stop or shorten work because you believe context is nearly
317
- full. The rule is registered only when the profile has a `systemPrompt` service.
318
-
319
- ## Tests
223
+ | `summarizationModel` | `auto` | `auto` 复用语会话当前路由;也可写明确的 `provider/model` |
224
+ | `summarizationFallback` | `[]` | 主摘要失败后按序尝试的 `provider/model` 路由 |
225
+ | `headroomTokens` | `65536`(官方默认) | 阈值下方预留的 token,给摘要留出比被压区间更小的空间 |
226
+ | `maxTokens` | 官方默认 | 生成摘要的长度上限 |
227
+ | `compactionRetries` | `1` | 压力仍高于阈值时的额外摘要尝试 |
228
+ | `maxOverflowRetries` | `1` | 确认上下文溢出后的重试次数;`0` 关闭恢复 |
229
+
230
+ `auto` 是根级开关,不是层字段:设为 `false` 只关闭自动压缩,手动 `/compact` 仍然可用。
231
+
232
+ **空隙要够大。** 摘要是结构化总结,实测下限约 3.2K token;`threshold − retain < 8192` 时摘要
233
+ 不可能比被压缩的区间更小,父类会拒绝。面板把「可压缩空间」标红提示;小窗口下小空隙可能无法
234
+ 避免,所以是告警而不是加载失败。
235
+
236
+ ## 策略如何解析
237
+
238
+ - `models[]` → `providers{}` → `default`,**逐字段**解析,按 `(provider, model, window)` 缓存。
239
+ 会话中途换模型,下一次门限判断立刻使用新策略。
240
+ - 引擎在门限检查时读取**当时**的路由,而不是会话启动时的路由。
241
+ - 路由没声明窗口时,绝对 token 预算无法兑现:字段进入 `deferred`,真正需要策略的那一步报错
242
+ 并点名路由。
243
+ - 配置错误(比例越界、`retain >= threshold`、未知字段、`models[]` 重复目标、
244
+ `summarizationModel` 残缺)在插件加载期失败,消息定位到字段。
245
+
246
+ ## 设置面板
247
+
248
+ Models 页脚 → **Compaction policy** 卡片:
249
+
250
+ - 选择层:全局默认或某个供应商路由;
251
+ - 编辑每个预算字段;**数值形状即单位**(`0.2` 比例,`200000` token),非法值标红并禁用保存;
252
+ - 切换单位时在同一批操作里 `unset` 被替换的键,文档不会同时留下两种单位;
253
+ - 新增/移除供应商覆盖;移除会把用户层里的所有字段 unset,包括卡片不渲染的字段;
254
+ - 会话头徽标显示当前触发点(`比例 · token`)、来源层(`models[]` / `providers{}` / `default` /
255
+ 引擎默认)、可压缩空间,以及上一次摘要实际使用的路由。
256
+
257
+ `models[]` 仍然只能手写 `settings.yaml`:settings 写入器对数组是整体替换,列表编辑器会在每次
258
+ 输入时重写整张表。徽标仍会报告会话命中了 `models[]`。
259
+
260
+ 非环回浏览器拿到的是 DSH 的内存态视图(`status: 'unavailable'`、`writable: false`),卡片会
261
+ 明说,而不是把插件默认值当成部署策略。
262
+
263
+ ## 行为保证
264
+
265
+ - **配置错误在启动时炸,不在第一次压缩时炸。** 例外是需要路由窗口的绝对预算:在真正需要策略
266
+ 的那一步报错并点名路由。
267
+ - **覆盖导致 `retain >= threshold` 时报错**,不悄悄缩小 retain。
268
+ - **不劫持压缩语义。** 选区、工具配对、事务落盘一行没改。
269
+ - **手动压缩是持久工作。** `/compact` 不因客户端断线而取消;只有 agent 自己的维护信号能取消。
270
+ Stop 仍然有效。
271
+ - **失败原因不谎报。** 闸门被占时先有界重试一次;始终不释放时明说「上一次活动还在收尾,
272
+ 锁是空的」。
273
+ - **上下文策略。** companion 声明:上下文由 harness 自动管理,模型无法测量自身用量,不得因为
274
+ 自认为快满而停止或缩短工作。profile 没有 `systemPrompt` 服务时不注册。
275
+
276
+ ## 测试
320
277
 
321
278
  ```bash
322
279
  npm test
323
280
  ```
324
281
 
325
- 198 cases across 13 files, including:
326
-
327
- - `manual-compaction.test.js` — gate retry, UI signal abort, real failures, honest messages,
328
- and a contract assertion against the installed official package;
329
- - `context-policy.test.js` — prompt registration and absence without a `systemPrompt` service;
330
- - `policy.test.js` — three-layer resolution, per-field merge, both units, conversion,
331
- warnings, overlay document rules;
332
- - `engine.test.js` — seam registration, `inject` inheritance, `Config` ownership, per-target
333
- switching, window conversion, no-window refusal, profile `models[]` surviving a
334
- `settings.yaml` section;
335
- - `settings-file.test.js` — patch/settings parsing and immediate edits;
336
- - `fallback.test.js`, `patch-file.test.js`, `settings.test.js`, `summary-prefix.test.js`,
337
- `unstated-threshold.test.js` — summarizer fallback, patch entry shapes, settings merge,
338
- summary framing and the unstated-threshold warning;
339
- - `client-policy.test.js` / `client.test.js` — panel calculations, unit switching, provider
340
- layers, session badge, illegal value blocking, inline bundle consistency.
341
-
342
- ## Compatibility and migration
343
-
344
- - DSH `0.2.2-rc.x` (`engines.dsh`: `>=0.2.2-0 <0.3.0-0`), Node.js 20+.
345
- - **Renamed from `dsh-compaction-control` in 0.5.0.** Entry ids are unchanged:
346
- `compaction-control` (engine), `compaction-control-settings` (companion), and the
347
- `compaction-control:` settings namespace. Update the `name:` fields in your profile/preset
348
- to `@magic6/dsh-compaction-policy-engine` (and `.../engine` for the engine row).
349
- - The engine is still a subclass of the official `BasicCompactionEngine`; no `node_modules`
350
- patch is required.
351
-
352
- ## License
353
-
354
- MIT. See [LICENSE](LICENSE).
282
+ 198 个用例,分布在 13 个文件,包括:
283
+
284
+ - `manual-compaction.test.js` — 闸门重试、UI 信号中断、真实失败、诚实消息,以及对已安装官方包
285
+ 的契约断言;
286
+ - `context-policy.test.js` — 提示词段落注册与无服务时的降级;
287
+ - `policy.test.js` — 三层解析、逐字段合并、两种单位、换算、告警、overlay 文档规则;
288
+ - `engine.test.js` — 接缝注册、`inject` 继承、`Config` 必须自有、按目标切换、窗口换算、
289
+ 无窗口拒绝、`settings.yaml` 段存在时 profile 的 `models[]` 仍生效;
290
+ - `settings-file.test.js` — 补丁/settings 解析与即时编辑;
291
+ - `fallback.test.js`、`patch-file.test.js`、`settings.test.js`、`summary-prefix.test.js`、
292
+ `unstated-threshold.test.js` — 摘要回退、补丁 entry 形状、settings 合并、摘要框架与
293
+ 未声明阈值告警;
294
+ - `client-policy.test.js` / `client.test.js` — 面板计算、单位切换、供应商层、会话徽标、
295
+ 非法值拦截、内联 bundle 一致性。
296
+
297
+ ## 兼容与迁移
298
+
299
+ - DSH `0.2.1-rc.x` / `0.2.2-rc.x`(`engines.dsh`:`>=0.2.1-0 <0.3.0-0`)、Node.js 20+。
300
+ - **0.5.0 从 `dsh-compaction-control` 改名。** entry id 保持不变:
301
+ `compaction-control`(引擎)、`compaction-control-settings`(companion),以及
302
+ `compaction-control:` 设置命名空间。把 profile/preset 里的 `name:` 改成
303
+ `@magic6/dsh-compaction-policy-engine`(引擎行加 `/engine`)即可。
304
+ - 仍然是官方 `BasicCompactionEngine` 的子类,不需要改 `node_modules`。
305
+
306
+ ## 许可
307
+
308
+ MIT,见 [LICENSE](LICENSE)。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic6/dsh-compaction-policy-engine",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "description": "Layered, live-reloading context compaction policy for DeepSeek Harness: provider-level rules, field-by-field inheritance, ratio/token units and a settings panel on top of the official engine.",
5
5
  "type": "module",
6
6
  "main": "lib/companion.js",
@@ -17,7 +17,7 @@
17
17
  "examples",
18
18
  "cordis.patch.yml",
19
19
  "README.md",
20
- "README.zh-CN.md",
20
+ "README.en.md",
21
21
  "CHANGELOG.md",
22
22
  "LICENSE"
23
23
  ],
@@ -46,7 +46,7 @@
46
46
  "url": "https://github.com/T-splines/dsh-compaction-policy-engine/issues"
47
47
  },
48
48
  "peerDependencies": {
49
- "@deepseek-ai/dsh-compaction-basic": "^0.2.2-rc.1",
49
+ "@deepseek-ai/dsh-compaction-basic": ">=0.2.1-0 <0.3.0-0",
50
50
  "@deepseek-ai/schemastery": "^3.18.2",
51
51
  "@deepseek-ai/cosmokit": "^1.8.3"
52
52
  },
@@ -67,6 +67,6 @@
67
67
  },
68
68
  "engines": {
69
69
  "node": ">=20.0.0",
70
- "dsh": ">=0.2.2-0 <0.3.0-0"
70
+ "dsh": ">=0.2.1-0 <0.3.0-0"
71
71
  }
72
72
  }
package/README.zh-CN.md DELETED
@@ -1,308 +0,0 @@
1
- # dsh-compaction-policy-engine
2
-
3
- [English](https://github.com/T-splines/dsh-compaction-policy-engine/blob/main/README.md) | **简体中文**
4
-
5
- **DeepSeek Harness 的按模型上下文压缩策略——三层阈值、热加载与摘要模型选择,构建在官方压缩引擎之上。**
6
-
7
- 官方 `@deepseek-ai/dsh-compaction-basic` 已经支持阈值、摘要模型,以及通过 `modelPolicies`
8
- 给精确 `(provider, model)` 覆盖。本插件是它的子类,在它之上再加一层:
9
-
10
- - **供应商层**——一条规则覆盖该 provider 后面的所有模型,不必逐个枚举模型 id;
11
- - **逐字段继承**——模型层只覆盖 `threshold`,`retain`、`maxTokens`、摘要路由仍从供应商层或
12
- 全局层继承;
13
- - **比例与绝对 token 双单位**,按路由声明的窗口换算;
14
- - **热加载 + 设置面板 + 会话徽标**;
15
- - **摘要回退链**与上下文策略提示词。
16
-
17
- 选区、工具配对平衡、摘要框架、落盘事务全部沿用官方实现。如果你只需要少量精确
18
- `(provider, model)` 覆盖,官方 `modelPolicies` 已经够用;本插件面向的是跨很多路由的分层策略。
19
-
20
- - [为什么需要它](#为什么需要它)
21
- - [差异点](#差异点)
22
- - [安装与挂载](#安装与挂载)
23
- - [配置](#配置)
24
- - [策略如何解析](#策略如何解析)
25
- - [设置面板](#设置面板)
26
- - [行为保证](#行为保证)
27
- - [测试](#测试)
28
- - [兼容与迁移](#兼容与迁移)
29
-
30
- ## 为什么需要它
31
-
32
- 官方引擎的 `modelPolicies` 是精确 `(provider, model)` 对。当一个网关暴露很多模型 id,或者
33
- 多条路由共享同一套策略时,逐个枚举既啰嗦又脆弱:新加的模型会静默落回全局阈值,直到有人补上
34
- 一行。本插件允许一条供应商层规则覆盖该 provider 的所有模型,并按
35
- `models[] → providers{} → default` 逐字段解析。
36
-
37
- ```
38
- models[] → providers{} → default
39
- ```
40
-
41
- 模型层可以只覆盖 `threshold`,`retain`、`maxTokens`、`headroomTokens` 和摘要路由继续从供应商
42
- 层或全局层继承。
43
-
44
- ## 差异点
45
-
46
- - **三层策略、逐字段。** `models[]` → `providers{}` → `default`;改一个字段不需要抄整层,
47
- 一条供应商行覆盖该 provider 后面的所有模型。
48
- - **比例或绝对 token 两种单位。** `threshold` / `retain` 按路由声明的窗口换算;
49
- `thresholdTokens` / `retainTokens` 是绝对预算。换算写回父类时使用
50
- `(tokens + 0.5) / window`,保证精确回到声明的数;路由没有声明窗口时**拒绝猜**,
51
- 在真正需要策略的那一步报错并点名路由。
52
- - **热加载。** 引擎每秒轮询 `settings.yaml` 与 `compaction-control-settings` 行;策略修改在
53
- 下一次压缩生效,不需要重启。面板写的是同一份文档。引擎挂载行自己的 `config:` 在加载时读取
54
- (见[安装与挂载](#安装与挂载))。
55
- - **部分文档合并。** `settings.yaml` 的 `compaction-control:` 段与挂载行 `config:` **逐字段**
56
- 合并(映射递归、数组整体替换),面板保存不会悄悄抹掉 profile 里的 `models[]` 层。
57
- - **设置面板。** 编辑全局层或某个供应商层,切换单位时不留下两个键,空隙过小时给出红字提示;
58
- 会话头徽标显示当前触发点和「上一次摘要实际由哪条路由写」。
59
- - **摘要回退链。** `summarizationFallback` 在主摘要路由失败时按序尝试其它路由,避免一条坏
60
- 路由让整个压缩停摆。
61
- - **上下文策略提示词。** companion 注册一段系统提示:上下文管理是自动的,模型无法测量自己的
62
- 用量,不得因为自认为「上下文快满了」而停止、缩短或移交工作。
63
- - **手动 `/compact` 是持久工作。** 客户端断线/重连不会杀死一次 20–40 秒的压缩事务;无法开始
64
- 时如实报告原因,不把空锁说成「已有压缩在跑」。
65
- - **有测试。** 198 个 `node --test` 用例,分布在 13 个文件,覆盖真实 bundle 渲染、三层解析、
66
- 单位换算、补丁文件解析和手动压缩保留量。
67
-
68
- ## 安装与挂载
69
-
70
- 要求:DSH `0.2.2-rc.x`、Node.js 20+。
71
-
72
- ### 从 npm 安装
73
-
74
- ```bash
75
- dsh plugin --profile web add @magic6/dsh-compaction-policy-engine
76
- ```
77
-
78
- npm 包里包含 `examples/standard-with-policy` 供参考;下面的 preset 覆盖规则仍然适用。
79
- web 小节里的 `$PWD` 命令假设使用源码检出。
80
-
81
- ### 从源码安装
82
-
83
- ```bash
84
- git clone https://github.com/T-splines/dsh-compaction-policy-engine.git
85
- cd dsh-compaction-policy-engine
86
- dsh plugin --profile web add "$PWD"
87
- ```
88
-
89
- 包本身是 bundle,会挂两行:
90
-
91
- - **引擎行**(`compaction-control`),**默认 disabled**;
92
- - **companion 行**(`compaction-control-settings`),始终启用,负责设置卡片与上下文提示词;
93
- 它**不**认领 `ctx.compaction`。
94
-
95
- ### 先确定挂载平面
96
-
97
- `ctx.compaction` 是**按 realm 解析的单槽接缝**。必须挂在 agent 实际解析到的作用域;挂在别处
98
- 不是「覆盖」而是「并存」——两个引擎都订阅冒泡的 `agent/pre-step`,触发点变成两者中较低的那个,
99
- 表现为「装完插件后压缩异常地早」。
100
-
101
- | profile | agent 的压缩引擎属于谁 | 怎么挂 |
102
- |:--|:--|:--|
103
- | `headless` | 宿主平面(没有 preset) | profile 的 `cordis.patch.yml`,覆盖 bundle 已插入的引擎行 |
104
- | `web` | **agent preset**(`dsh-web-app` 停用了宿主平面的 `compaction-basic`) | 覆盖 preset 声明;本仓库带一个示例 bundle |
105
-
106
- #### headless(宿主平面)
107
-
108
- bundle 已经插入 `compaction-control`(disabled)。**覆盖这一行**,不要再 `insert` 一份:
109
-
110
- ```yaml
111
- # $DSH_HOME/profiles/<profile>/cordis.patch.yml
112
- - id: compaction-basic
113
- disabled: true
114
-
115
- - id: compaction-control
116
- disabled: false
117
- config:
118
- default:
119
- threshold: 0.8
120
- retain: 0.16
121
- ```
122
-
123
- loader 认的键是 **`disabled`**,不是 `disable`;写错不会报错,会被静态配置静默丢掉
124
- (看起来停用了,其实还启用)。
125
-
126
- #### web(agent preset)
127
-
128
- DSH 0.2.2 的 preset 是 bundle patch 里的 `@deepseek-ai/dsh-agent-preset` 声明。旧目录
129
- `$DSH_HOME/.agent-presets/<id>/agent.cordis.yml` 已经不再被读取。web 的 agent realm 隔离了
130
- `ctx.compaction`,宿主平面的引擎它看不到。
131
-
132
- 本仓库带一个示例 bundle:覆盖 `preset-standard`,把 `compaction` 组里的 `compaction-basic`
133
- 换成 `compaction-control`,并保留 `command-compact` 与 `tool-result-pruner`:
134
-
135
- ```bash
136
- dsh plugin --profile web add "$PWD"
137
- dsh plugin --profile web add "$PWD/examples/standard-with-policy"
138
- ```
139
-
140
- 重启 web app,新开会话验证。示例是 DSH 0.2.2 标准 preset 的快照;如果你改过标准 preset,请把
141
- `compaction` 组的手动合并进你自己的 preset 声明,而不是盲目安装示例(Creator 模式可以代写
142
- 这个覆盖)。文件在
143
- [`examples/standard-with-policy/cordis.patch.yml`](examples/standard-with-policy/cordis.patch.yml),
144
- 头部也写了同样的提醒。
145
-
146
- 两种静默坏组合:
147
-
148
- - 宿主平面挂了引擎,preset 里原版 `compaction-basic` 还在 → 两个引擎,阈值低的赢;
149
- - 原版停用了却没有挂任何替代引擎 → 接缝无人认领,会话静默停止压缩,一路涨到被上游拒绝。
150
-
151
- 「停用旧的」和「启用新的」必须写在同一个组合里,一起 review。
152
-
153
- ### 热加载范围
154
-
155
- 设置面板写 `$DSH_HOME/settings.yaml`。引擎每秒轮询该文件**以及** `compaction-control-settings`
156
- 行,所以面板修改和这一行的修改会在下一次压缩生效,不需要重启。**引擎挂载行**
157
- (`compaction-control`)的 `config:` 在插件加载时读取,改它需要重启。
158
-
159
- ### 阈值语义
160
-
161
- 官方引擎的触发点是
162
-
163
- ```
164
- floor(min(W × threshold, W − O − headroomTokens))
165
- ```
166
-
167
- 其中 `W` 是路由声明的窗口,`O` 是输出预留,`headroomTokens` 默认 65,536。`measured`
168
- **包含固定的系统提示词和工具清单**。在工具很多的部署里,`threshold: 0.5` 可能意味着
169
- 「系统提示词之外再攒约 23K 就压」,不是「对话到一半才压」。
170
-
171
- 本插件的 `threshold` / `retain` 映射到官方的 `thresholdRatio` / `retainRatio`;
172
- `headroomTokens` 与 `maxTokens` 是逐层字段。默认 0.8 是官方引擎默认值,除非 headroom 上限
173
- 先到,否则不会比原版更早触发。会话头徽标显示的是 `窗口 × threshold` 的快速预览;因为
174
- `W − O − headroomTokens` 可能更低,把它当作触发点的**上界**。
175
-
176
- ## 配置
177
-
178
- 策略可以写在挂载行的 `config:`,也可以写在 `$DSH_HOME/settings.yaml` 的
179
- `compaction-control:` 段(面板编辑后者)。
180
-
181
- ```yaml
182
- compaction-control:
183
- default:
184
- threshold: 0.8 # 到达窗口的这个比例就压
185
- retain: 0.16 # 原样保留的尾部比例,必须小于 threshold
186
- summarizationModel: auto # auto = 用会话当前路由的模型,或 "provider/model"
187
- summarizationFallback: [] # 主摘要失败后按序尝试的 "provider/model" 路由
188
- headroomTokens: 65536 # 阈值下方预留的空间
189
- maxTokens: 8192 # 摘要长度上限
190
- compactionRetries: 1
191
- maxOverflowRetries: 1
192
-
193
- providers:
194
- some-provider:
195
- threshold: 0.5
196
-
197
- models:
198
- - provider: local
199
- model: model-262k
200
- threshold: 0.9
201
-
202
- auto: true
203
- ```
204
-
205
- ### 两种单位
206
-
207
- 每个预算字段都有比例与绝对 token 两种写法,**同一层只能写一种**:
208
-
209
- | 比例 | 绝对 token | 语义 |
210
- |:--|:--|:--|
211
- | `threshold` | `thresholdTokens` | 到达这个量就压 |
212
- | `retain` | `retainTokens` | 保留最近这么多内容 |
213
-
214
- 同一层两种都写时 token 形式生效,并打一行 `[compaction] WARNING` 点名被遮蔽的键。整段拒绝
215
- 会让面板刚保存的值看起来被静默忽略,所以选择「生效 + 告警」。
216
-
217
- ### 其它策略字段
218
-
219
- 以下字段在 `default`、`providers{}` 层或 `models[]` 层都有效:
220
-
221
- | 字段 | 默认值 | 含义 |
222
- |:--|:--|:--|
223
- | `summarizationModel` | `auto` | `auto` 复用语会话当前路由;也可写明确的 `provider/model` |
224
- | `summarizationFallback` | `[]` | 主摘要失败后按序尝试的 `provider/model` 路由 |
225
- | `headroomTokens` | `65536`(官方默认) | 阈值下方预留的 token,给摘要留出比被压区间更小的空间 |
226
- | `maxTokens` | 官方默认 | 生成摘要的长度上限 |
227
- | `compactionRetries` | `1` | 压力仍高于阈值时的额外摘要尝试 |
228
- | `maxOverflowRetries` | `1` | 确认上下文溢出后的重试次数;`0` 关闭恢复 |
229
-
230
- `auto` 是根级开关,不是层字段:设为 `false` 只关闭自动压缩,手动 `/compact` 仍然可用。
231
-
232
- **空隙要够大。** 摘要是结构化总结,实测下限约 3.2K token;`threshold − retain < 8192` 时摘要
233
- 不可能比被压缩的区间更小,父类会拒绝。面板把「可压缩空间」标红提示;小窗口下小空隙可能无法
234
- 避免,所以是告警而不是加载失败。
235
-
236
- ## 策略如何解析
237
-
238
- - `models[]` → `providers{}` → `default`,**逐字段**解析,按 `(provider, model, window)` 缓存。
239
- 会话中途换模型,下一次门限判断立刻使用新策略。
240
- - 引擎在门限检查时读取**当时**的路由,而不是会话启动时的路由。
241
- - 路由没声明窗口时,绝对 token 预算无法兑现:字段进入 `deferred`,真正需要策略的那一步报错
242
- 并点名路由。
243
- - 配置错误(比例越界、`retain >= threshold`、未知字段、`models[]` 重复目标、
244
- `summarizationModel` 残缺)在插件加载期失败,消息定位到字段。
245
-
246
- ## 设置面板
247
-
248
- Models 页脚 → **Compaction policy** 卡片:
249
-
250
- - 选择层:全局默认或某个供应商路由;
251
- - 编辑每个预算字段;**数值形状即单位**(`0.2` 比例,`200000` token),非法值标红并禁用保存;
252
- - 切换单位时在同一批操作里 `unset` 被替换的键,文档不会同时留下两种单位;
253
- - 新增/移除供应商覆盖;移除会把用户层里的所有字段 unset,包括卡片不渲染的字段;
254
- - 会话头徽标显示当前触发点(`比例 · token`)、来源层(`models[]` / `providers{}` / `default` /
255
- 引擎默认)、可压缩空间,以及上一次摘要实际使用的路由。
256
-
257
- `models[]` 仍然只能手写 `settings.yaml`:settings 写入器对数组是整体替换,列表编辑器会在每次
258
- 输入时重写整张表。徽标仍会报告会话命中了 `models[]`。
259
-
260
- 非环回浏览器拿到的是 DSH 的内存态视图(`status: 'unavailable'`、`writable: false`),卡片会
261
- 明说,而不是把插件默认值当成部署策略。
262
-
263
- ## 行为保证
264
-
265
- - **配置错误在启动时炸,不在第一次压缩时炸。** 例外是需要路由窗口的绝对预算:在真正需要策略
266
- 的那一步报错并点名路由。
267
- - **覆盖导致 `retain >= threshold` 时报错**,不悄悄缩小 retain。
268
- - **不劫持压缩语义。** 选区、工具配对、事务落盘一行没改。
269
- - **手动压缩是持久工作。** `/compact` 不因客户端断线而取消;只有 agent 自己的维护信号能取消。
270
- Stop 仍然有效。
271
- - **失败原因不谎报。** 闸门被占时先有界重试一次;始终不释放时明说「上一次活动还在收尾,
272
- 锁是空的」。
273
- - **上下文策略。** companion 声明:上下文由 harness 自动管理,模型无法测量自身用量,不得因为
274
- 自认为快满而停止或缩短工作。profile 没有 `systemPrompt` 服务时不注册。
275
-
276
- ## 测试
277
-
278
- ```bash
279
- npm test
280
- ```
281
-
282
- 198 个用例,分布在 13 个文件,包括:
283
-
284
- - `manual-compaction.test.js` — 闸门重试、UI 信号中断、真实失败、诚实消息,以及对已安装官方包
285
- 的契约断言;
286
- - `context-policy.test.js` — 提示词段落注册与无服务时的降级;
287
- - `policy.test.js` — 三层解析、逐字段合并、两种单位、换算、告警、overlay 文档规则;
288
- - `engine.test.js` — 接缝注册、`inject` 继承、`Config` 必须自有、按目标切换、窗口换算、
289
- 无窗口拒绝、`settings.yaml` 段存在时 profile 的 `models[]` 仍生效;
290
- - `settings-file.test.js` — 补丁/settings 解析与即时编辑;
291
- - `fallback.test.js`、`patch-file.test.js`、`settings.test.js`、`summary-prefix.test.js`、
292
- `unstated-threshold.test.js` — 摘要回退、补丁 entry 形状、settings 合并、摘要框架与
293
- 未声明阈值告警;
294
- - `client-policy.test.js` / `client.test.js` — 面板计算、单位切换、供应商层、会话徽标、
295
- 非法值拦截、内联 bundle 一致性。
296
-
297
- ## 兼容与迁移
298
-
299
- - DSH `0.2.2-rc.x`(`engines.dsh`:`>=0.2.2-0 <0.3.0-0`)、Node.js 20+。
300
- - **0.5.0 从 `dsh-compaction-control` 改名。** entry id 保持不变:
301
- `compaction-control`(引擎)、`compaction-control-settings`(companion),以及
302
- `compaction-control:` 设置命名空间。把 profile/preset 里的 `name:` 改成
303
- `@magic6/dsh-compaction-policy-engine`(引擎行加 `/engine`)即可。
304
- - 仍然是官方 `BasicCompactionEngine` 的子类,不需要改 `node_modules`。
305
-
306
- ## 许可
307
-
308
- MIT,见 [LICENSE](LICENSE)。