@magic6/dsh-compaction-policy-engine 0.5.10 → 0.5.11

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/docs/DESIGN.md CHANGED
@@ -85,9 +85,19 @@ the disable and the mount together.
85
85
  The loader key is `disabled`, not `disable`. The typo is dropped by the schema and the row
86
86
  stays enabled.
87
87
 
88
- The bundle deliberately ships the engine row `disabled`: turning it on without also disabling
89
- the stock engine would create a second engine, and disabling the stock engine without a
90
- replacement would leave no engine. Superseding the stock engine is a deployment decision.
88
+ **`ctx.compaction` is a single-slot service, not an agreement between engines.** A second
89
+ engine in the same context does not lose a threshold race — it throws
90
+ `service "compaction" has been registered at <BasicCompactionEngine>` while constructing and
91
+ never mounts (the host prints only `dsh: warning: 1 entry did not activate`). "Keep the stock
92
+ engine as a late backstop" is therefore not available.
93
+
94
+ Because of that, the bundle does both halves of the swap in one place: it sets
95
+ `compaction-basic` to `disabled: true` and enables the engine row. Uninstalling removes both,
96
+ so the stock engine comes back — a profile can never end up with no compaction at all.
97
+
98
+ ≤0.5.9 shipped the engine row `disabled`, on the theory that superseding the stock engine was
99
+ a deployment decision. That assumed the stock engine was running; on `dsh-web-app` presets it
100
+ is not, so both engines were off and the seam was unclaimed. See `exp.md` (2026-11-26).
91
101
 
92
102
  The companion row is host-plane and always enabled because the browser bundle is discovered
93
103
  from enabled loader entries; the engine row lives in a preset and is invisible to that scan.
@@ -0,0 +1,70 @@
1
+ # 压缩高可用方案
2
+
3
+ > 目标:**压缩永远不因"发不出去"而失败**。
4
+
5
+ ## 0. 已解决:全量重放(2026-11-26)
6
+
7
+ `summarize()` 曾把整段对话重放,让摘要请求成为 live state 的精确 token-前缀延伸
8
+ (为了本地引擎的 `--prompt-cache` 命中)。**该机制已删除**,用官方的 region-only。
9
+
10
+ 它不是"偶发过大",而是**结构上必然失败**。Win11 会话实测(`session-91ac1b04`,
11
+ 从未成功压缩过):
12
+
13
+ | 请求体 | tokens |
14
+ |:--|--:|
15
+ | 全量重放(带 reasoning) | 2,182,311 |
16
+ | 全量重放(剥掉 reasoning) | 1,247,719 |
17
+ | 网关实际请求峰值 | **217,373** |
18
+ | 声明窗口 | 1,000,000 |
19
+
20
+ 区别在于**正常请求只发当前窗口内的内容**,而全量重放试图把**累计历史**(1.25M)
21
+ 全部塞进一次请求。剥掉 reasoning 只能从 2.2M 降到 1.25M —— 仍然超窗。
22
+
23
+ **结论:需要压缩的会话,恰好就是全量重放装不下的会话。** 用来救它的请求,正是
24
+ 发不出去的那个。四次压缩全部 `context_length_exceeded`,会话永久不可恢复。
25
+
26
+ region-only 的区域由触发压缩的 gate 限定,**因此天然有界**。这是唯一正确的做法。
27
+
28
+ ## 1. 剩余风险
29
+
30
+ 回到 region-only 后,摘要请求体积 = 被压区间。该区间由 gate 选中,正常情况有界。
31
+ 但仍有一种情况会超:
32
+
33
+ **1M 会话切到 256K 模型。** gate 按 256K 计算,选中区间相当大,摘要请求可能装不进
34
+ 256K 窗口。
35
+
36
+ ## 2. 保底方案(`lib/summarize-budget.js`,已写好 19 个测试)
37
+
38
+ 显式给摘要请求设上限,超了就分级降级:
39
+
40
+ ```
41
+ ① region-only 父类 input 装得下 → 直接用 (默认路径)
42
+ ② 分批压缩 装不下 → 切成 N 批,每批带前批摘要
43
+ ③ 离线裁剪 分批仍不行 → 砍 tool 输出,不调模型
44
+ ```
45
+
46
+ **分批的接续方式**:
47
+
48
+ ```
49
+ batch 1: [最早的节点…塞满 80%] → S1
50
+ batch 2: [S1] + [后续节点…塞满 80%] → S2
51
+ batch 3: [S2] + [后续…] → S3
52
+ ```
53
+
54
+ 每批带上前一批摘要,信息不丢。批次数上限 12,超了就明说"开新会话"而不是无限烧钱。
55
+
56
+ **离线裁剪只动 tool 输出** —— 那是长会话的体积主体,也是唯一可重建的部分;
57
+ user/assistant 消息是语义主干,永不删除。
58
+
59
+ ## 3. 状态
60
+
61
+ - [x] 移除全量重放,回到 region-only(`780e74c`)
62
+ - [x] `lib/summarize-budget.js`:预算/切分/裁剪,19 测试
63
+ - [ ] 接线到 `summarize()`:按预算选择 ①②③
64
+ - [ ] e2e 场景 `shrink`:1M 会话 → 256K 模型
65
+
66
+ ## 4. 不做的事
67
+
68
+ - 不改父类 `BasicCompactionEngine` 的事务语义
69
+ - 不动 `compactRegion` 的持久化逻辑
70
+ - 不再为缓存命中牺牲可用性 —— 压不动比压得慢严重得多
package/lib/index.js CHANGED
@@ -246,55 +246,6 @@ function conversationTarget(agent) {
246
246
  return undefined;
247
247
  }
248
248
 
249
- /**
250
- * The message list the last routed request actually sent: the system head plus
251
- * every surface node's projected message, in surface order.
252
- *
253
- * Why the summary call must replay all of it rather than only the shadowed
254
- * region (measured against a local inference engine): that engine
255
- * resumes only from a stored state that is an exact token-prefix of the new
256
- * prompt, and it keeps at most `--prompt-cache` (6) checkpoints, taken at
257
- * assistant-turn boundaries and every `--prompt-cache-every` (16384) fresh
258
- * tokens. Those six slots hold the *recent* turn boundaries, so the shadowed
259
- * region's own checkpoints are long evicted: a region-only replay matches
260
- * nothing but the pinned system-prompt root and re-reads the whole region.
261
- * Measured: a 403,413-token prompt read over 311 s, then killed by provider-d's
262
- * 300 s idle watchdog, because a prefill emits no tokens and a slow engine
263
- * therefore looks idle. Appending the instruction after the FULL conversation
264
- * instead makes the call an exact extension of the live state, so the engine
265
- * resumes at the live end and prefills only the instruction.
266
- *
267
- * The trade-off is deliberate: the summary now condenses the whole
268
- * conversation, so the checkpoint and the verbatim-retained tail overlap. That
269
- * costs the tail's tokens once per compaction, against re-reading the entire
270
- * context on every one.
271
- *
272
- * @param session - the session whose surface to project.
273
- * @returns the projected messages, or `undefined` when the surface is unreadable.
274
- */
275
- function fullConversationMessages(session) {
276
- const nodes = session?.surface?.nodes;
277
- if (!Array.isArray(nodes) || nodes.length === 0) return undefined;
278
- const messages = [];
279
- for (const seq of nodes) {
280
- const event = session.eventAt?.(seq);
281
- if (event === undefined || event === null) continue;
282
- const message = session.deriveEventMessage?.(event);
283
- if (message === null || message === undefined) continue;
284
- messages.push(message);
285
- }
286
- return messages.length === 0 ? undefined : messages;
287
- }
288
-
289
- /**
290
- * The summary request's message list: the full conversation when the session
291
- * can project it, otherwise the caller's own region-only input unchanged.
292
- */
293
- function summarizationInput(input, agent) {
294
- const messages = fullConversationMessages(agent?.session);
295
- return messages === undefined ? input : { ...input, messages };
296
- }
297
-
298
249
  /**
299
250
  * Say once when the pressure gate falls back to the engine's constructor baseline
300
251
  * because no layer states a threshold for this target.
@@ -1052,14 +1003,18 @@ export class CompactionControlEngine extends BasicCompactionEngine {
1052
1003
  * fall back to the latest routed request, which preserves the provider's warm
1053
1004
  * prefix cache.
1054
1005
  *
1055
- * The message list is rewritten to the whole conversation first (see
1056
- * `summarizationInput`): upstream replays only the shadowed region, and a local
1057
- * engine whose checkpoints for that region have been evicted can only answer
1058
- * that by re-reading all of it. On `auto` the target is therefore the very
1059
- * route whose live state this request now extends.
1006
+ * The message list is the caller's own: upstream replays only the shadowed
1007
+ * region, and that region is bounded by the gate that selected it, so the
1008
+ * request is bounded too.
1060
1009
  */
1061
1010
  async summarize(input, agent, signal) {
1062
- const effective = summarizationInput(input, agent);
1011
+ // The parent's own region-only input is used verbatim. A full-conversation
1012
+ // replay was tried to reuse a local engine's prefix cache and removed on
1013
+ // 2026-11-26: it made the summary request carry the whole session, which is
1014
+ // the one payload that must stay bounded — a conversation large enough to
1015
+ // need compaction is one whose replay no longer fits, so the request that
1016
+ // was supposed to rescue the session could not be sent at all.
1017
+ const effective = input;
1063
1018
  const target = conversationTarget(agent);
1064
1019
  // The window goes with the policy for the same reason it does in the gate:
1065
1020
  // an absolute retention has to be converted before the parent can compare
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic6/dsh-compaction-policy-engine",
3
- "version": "0.5.10",
3
+ "version": "0.5.11",
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",