dsh-context-truth 0.3.4 → 0.4.0
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 +53 -0
- package/README-en.md +19 -1
- package/README.md +25 -1
- package/lib/index.js +251 -12
- package/package.json +1 -1
- package/test/index.test.js +197 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,58 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
* **委派会话(子代理)也能写交接档了:`subagentHandoff`,默认关。** 动机是量出来的,不是猜的:
|
|
6
|
+
上游把"智能体团队"并进子代理之后,子代理成了可续、可寻址的同事(#7519 的身份与消息指引、
|
|
7
|
+
0.2.2 的命名、`continue its conversation after it finishes`),生命周期变长,折叠暴露面跟着上升。
|
|
8
|
+
本机 296 个会话里 116 个是子代理,**8 个(6.9%)至少折叠过一次**(14 次折叠事件),其中一个已经
|
|
9
|
+
到折叠点的 65%。而折叠对子会话的伤害和根会话一样大:它的父代理会继续用 `send_message` 找它,
|
|
10
|
+
好像它还记得折叠前的事。
|
|
11
|
+
* 形状是**提醒 + 写档 + 折叠后指回**,**不唤醒、不折叠**:
|
|
12
|
+
- 委派会话越过准备点(`handoffAt` × 折叠点,默认 85%)时,把 `[handoff fold · subagent prepare]`
|
|
13
|
+
发布到它自己的 `context-truth:handoff` runtime-context 块(与根会话同一条通道,仍然每轮最多一次、
|
|
14
|
+
每周期最多 `handoffReminders` 次,没有用户消息、没有 `inject`);
|
|
15
|
+
- **写文件就是登记**:子会话的工具目录里没有可用的 `handoff_ready`(它按设计拒答委派会话),
|
|
16
|
+
所以插件在下一个请求里检查 `<root>/<childSessionId>/<fold#>.handoff.md` 是否存在,存在即登记,
|
|
17
|
+
并记下当时读数作为折叠基线;开关打开时 `handoff_ready` 的拒答文案改成"写文件本身就是登记",
|
|
18
|
+
不再是一堵墙;
|
|
19
|
+
- 折叠由**宿主**在压力点执行,插件只观察:已登记 + 读数掉到登记时读数的 **90% 以下** = 折叠发生了
|
|
20
|
+
(读数只在窗口被重写时掉下来;掉几个百分点是工具目录进出之类的抖动,不算),于是把
|
|
21
|
+
`[handoff fold · subagent, after the fold]` 连同路径发布到下一次请求,`foldNo` 加一(下一份是
|
|
22
|
+
`002.handoff.md`),并保留被指回的那份档——与根会话"激活目标不能被下一轮准备回收"同一条规则;
|
|
23
|
+
- 掉档但没折叠(例如重启后才发现档、或读数在准备点以下且有旧档)也有归宿:`discoveredLate`
|
|
24
|
+
直接发指回提示,不让子会话困在"档在盘上、没人告诉它"的状态里。
|
|
25
|
+
* 默认关:它会改变每个子会话模型看到的东西,而多数子会话到不了准备点(116 个里只有 8 个被折叠过)。
|
|
26
|
+
* **实机演练通过(2026-11-20 20:00,重启后运行 0.4.0 + `subagentHandoff: true`)**:把 `thresholdRatio`
|
|
27
|
+
压到 0.02(折叠点 20,971、准备点 17,825)、`handoffReminders` 压到 1,用一个既有子会话跑完整条链路:
|
|
28
|
+
1. `delegate handoff prep 1/1 … (used=93604 of the 20971 fold point)` —— 提醒发布;
|
|
29
|
+
2. 子会话按提醒写 `handoffs/86942830…/001.handoff.md`(2318 字符),**没有**调 `handoff_ready`
|
|
30
|
+
(其拒答文案也确实换成了"写文件本身就是登记"那句);
|
|
31
|
+
3. `delegate … wrote its handoff note (2318 chars)` —— 文件即登记,`ready.used=100865` 基线入账;
|
|
32
|
+
4. 把基线改成 600,000 模拟宿主折叠(真折叠要 727K,演练压不到),下一次请求得到
|
|
33
|
+
`delegate … was folded since it wrote …`,`foldNo` 0→1、`ready` 清空、`reminders` 归零;
|
|
34
|
+
5. 子会话的下一次请求逐字报回 `[handoff fold · subagent, after the fold]` 首行与路径。
|
|
35
|
+
全程没有 `followup`、没有折叠(该子会话的每一次被唤醒都来自父代理的 `send_message`)。
|
|
36
|
+
* **真实宿主折叠也补上了(同日 20:13–20:14,生产会话而非演练)**:另一个会话里已有的子代理
|
|
37
|
+
`f664ed48`(当时 65%)拿到提醒是 20:10:10.816 `delegate handoff prep 1/3 (used=683623 of 727040)`,
|
|
38
|
+
它写了 5,594 字符的档(基线 `ready.used=691627`);读数在 20:13:36.158 越过折叠点
|
|
39
|
+
(`tight -> critical (used=727843 … compactionAt=727040)`,超出 803 tokens),宿主随即折叠
|
|
40
|
+
(`compaction/start` 20:13:36 → `compaction/end` 20:14:00,`compaction (step pressure): shadowed 427
|
|
41
|
+
surface nodes (seqs 12-1189, ~339405 tokens)`);20:14:00.241 插件判折叠并登记
|
|
42
|
+
`used=392118, registered at=691627, cause=reading dropped`;20:14:09 那条指回提示只投递一次,
|
|
43
|
+
**子会话折叠后的第一个动作就是 `read` 自己的档**(20:14:10),随后在同一轮里继续干活——没有唤醒。
|
|
44
|
+
这是该子会话的第一次折叠(此前 0 次),也是本能力唯一一次"提醒 → 写档 → 真实折叠 → 指回 → 读档"全真链路。
|
|
45
|
+
* **顺带纠正一条 0.3.2 写错的时序**:runtime context 快照是在该步的 `agent/request` 瀑布**之前**物化的,
|
|
46
|
+
所以"在这条瀑布里发布的提醒由同一个请求渲染"是错的——它由**下一个**请求渲染(证据:19:50:08.943 发布,
|
|
47
|
+
19:50:11 的快照才带上它)。多步轮次不受影响;对"一轮只有一步"的子会话意味着轮末发布的折叠后标记要等
|
|
48
|
+
父代理下一次叫它才读得到(演练里正是这样读到的)。代码注释已按实测改写。
|
|
49
|
+
* 测试:82(新增 8 条委派用例);六处变异分别失败 1 / 1 / 2 / 1 / 3 / 3 例,隔离副本按字节恢复后
|
|
50
|
+
与工作副本 sha256 一致。
|
|
51
|
+
* 顺带核实(写进交接档,不进代码):本机 kernel **0.2.2-rc.5 不含** #8348 的子代理汇报指引
|
|
52
|
+
(`dsh-tool-subagent-control` 的 `src/index.ts` 与 `lib/index.js` 都没有那句),它只在 master 上;
|
|
53
|
+
所以"子代理用完 send_message 汇报"这条在本机尚未生效,本机的复用压力来自 #7519 + 命名 + 本地
|
|
54
|
+
`dsh-srkit` 的 `subagent-reuse` skill。
|
|
55
|
+
|
|
3
56
|
## 0.3.4
|
|
4
57
|
|
|
5
58
|
* **唤醒消息瘦身:完整提醒改由唤醒轮的 runtime context 承载。** 以前轮末唤醒把整段
|
package/README-en.md
CHANGED
|
@@ -108,6 +108,23 @@ The chain is:
|
|
|
108
108
|
|
|
109
109
|
Guards: at most 3 reminders per cycle; if a round ends past the prep point with no note registered, the plugin **wakes** a round with `followup` (dynamic context is only read by requests that actually happen, and a round that ends issues no further request, so the reminder would otherwise wait for a human — measured, and that is why the wake exists). The wake message is a **single line** naming the note path, and `prepare` re-publishes the full checklist into the runtime-context handoff block of that same request — so the transcript never gets the whole reminder as a user message. If the reading has already dropped below the prep point when the wake round starts (for example the host folded first), only the one-liner remains and the log says why. Notes are temporary, recycled when the next handoff is created with a 7-day sweep; a registered note whose fold did not happen is kept and never activates a turn.
|
|
110
110
|
|
|
111
|
+
#### Notes in delegated sessions (`subagentHandoff`, off by default)
|
|
112
|
+
|
|
113
|
+
The root-session handoff leans on two things a child does not have: this plugin performs the fold, and this plugin opens the activation turn. A child therefore gets a reduced shape, not the same one:
|
|
114
|
+
|
|
115
|
+
| | Root session | Delegated session (`subagentHandoff: true`) |
|
|
116
|
+
|---|---|---|
|
|
117
|
+
| Reminder | ✅ runtime-context block | ✅ the same block and the same budget (once per turn, `handoffReminders` per cycle) |
|
|
118
|
+
| Registration | call `handoff_ready` | **writing the file is the registration** (`handoff_ready` refuses a delegated session by design) |
|
|
119
|
+
| Fold | the plugin runs it at the round end | ❌ the host runs it at the pressure point; the plugin only watches |
|
|
120
|
+
| After the fold | `followup` activates exactly one turn | ❌ a pointer to that path, delivered on the next request its parent causes |
|
|
121
|
+
|
|
122
|
+
Why reduced: a child's turns belong to whoever delegated it, and injecting one would break the parent's sequencing. Why still worth doing: upstream merged agent teams into subagents, so children are now resumable, addressable colleagues with longer lives; 8 of 116 delegated sessions on this host (6.9%) were folded at least once, and after such a fold the parent keeps addressing a child that only remembers a summary.
|
|
123
|
+
|
|
124
|
+
The fold is detected from the reading, not from a counter: a registered note plus a reading below **90% of the reading it was registered at**. A reading only drops when somebody rewrites the window; a few percent is a trim (a tool catalog coming and going, `defer_loading`) and is not treated as a fold — otherwise a child would be sent to read a note it does not need yet.
|
|
125
|
+
|
|
126
|
+
One timing detail: the reminder is published inside `agent/request`, and that step's runtime-context snapshot is already materialized by then, so the model reads it on its **next** step. That is invisible for a root session (rounds have several steps); a child whose turn is a single step reads the post-fold pointer when its parent next calls it — measured that way, and nothing is lost.
|
|
127
|
+
|
|
111
128
|
**Known limitation: session permissions can block the note write.** Notes live under `$DSH_HOME/handoffs/…`, and the host sandboxes model file writes per session permission (`checkedTarget` in `@deepseek-ai/dsh-fs-sandbox`, which checks the target against the writable roots of the resolved mode):
|
|
112
129
|
|
|
113
130
|
- **read-only**: every mutation is denied, so the model cannot write the note at all; handoff mode degrades to "remind, but never fold" and the host pressure fold takes over;
|
|
@@ -191,6 +208,7 @@ Capability 3 resolves its engine itself, at fold time, through a **ranked candid
|
|
|
191
208
|
| `handoffAt` | `0.85` | Prep point for the handoff reminder, as a fraction of the **fold point** (not the window) |
|
|
192
209
|
| `handoffDir` | `''` | Note directory; empty means `$DSH_HOME/handoffs`, a relative path also resolves under `$DSH_HOME`, never the workspace |
|
|
193
210
|
| `handoffReminders` | `3` | Reminders per cycle, wake included |
|
|
211
|
+
| `subagentHandoff` | `false` | Remind delegated sessions too: the file is the registration, no wake, no fold, and after the fold only a pointer back (see above) |
|
|
194
212
|
| `foldAtIdle` | `false` | Legacy switch, equivalent to `foldMode: idle`; an explicit `false` means `off`. Only matters for a config that never set `foldMode` |
|
|
195
213
|
| `foldAtIdleMarginPercent` | `5` | Fold once the headroom is within this percent of the window (50K on a 1M window) |
|
|
196
214
|
| `foldAtIdleMethod` | `auto` | Channel for the request: `auto` runs the session's own `/compact` first (realm view), then falls back to the ranked service chain; `command` / `service` force one. `service` is the most predictable of the two (this profile uses it) |
|
|
@@ -234,7 +252,7 @@ What you get for it: the model stops halting or handing off because it *thinks*
|
|
|
234
252
|
## Verify
|
|
235
253
|
|
|
236
254
|
```sh
|
|
237
|
-
npm test #
|
|
255
|
+
npm test # 82 unit tests: banding, hysteresis, quantization, free window, fold definition, idle fold and its guards, live settings changes, schema-default parity, pace estimate, degradation, directive patching, look-alike rejection, idempotence, handoff folding (reminder, registration, round-end fold, single activation, restart recovery, recycling, round-end wake), notes in delegated sessions (the file as the registration, fold detection, the pointer back, off by default), client card shape, the handoff reminder travelling in runtime context rather than as a message, and a live mode switch dropping it
|
|
238
256
|
```
|
|
239
257
|
|
|
240
258
|
After installing, open a new session and send one message: expanding that turn's runtime context block should show `Context occupancy (host-measured): …`, and the log carries `context-truth: none -> ample (used=… window=1000000 compactionAt=678464)`.
|
package/README.md
CHANGED
|
@@ -168,6 +168,29 @@ this in as few commands as possible, then a short answer."。数字它读对了
|
|
|
168
168
|
交接档是临时文件,下一次交接生成时回收旧的、7 天兜底清理;登记了档但没折成(引擎拒绝、被别的东西
|
|
169
169
|
先折了)时不会激活,档保留。
|
|
170
170
|
|
|
171
|
+
#### 委派会话的交接档(`subagentHandoff`,默认关)
|
|
172
|
+
|
|
173
|
+
根会话的 handoff 依赖两件子会话没有的东西:折叠是本插件做的,激活轮是本插件开的。所以给子会话的不是同一套,
|
|
174
|
+
而是缩小版:
|
|
175
|
+
|
|
176
|
+
| | 根会话 | 委派会话(`subagentHandoff: true`) |
|
|
177
|
+
|---|---|---|
|
|
178
|
+
| 提醒 | ✅ runtime context 块 | ✅ 同一个块、同一套预算(每轮一次、每周期 `handoffReminders` 次) |
|
|
179
|
+
| 登记 | 调 `handoff_ready` | **写文件即登记**(子会话调 `handoff_ready` 一律拒答) |
|
|
180
|
+
| 折叠 | 插件在轮末执行 | ❌ 宿主在压力点执行,插件只观察 |
|
|
181
|
+
| 折叠后 | `followup` 自动激活唯一一轮 | ❌ 只在**父代理下一次找它**的那个请求里发一条指回路径的提示 |
|
|
182
|
+
|
|
183
|
+
为什么缩小:子会话的轮次属于它的委派方,插件注入一轮会打乱父代理的编排。为什么仍然值得做:上游把团队并进
|
|
184
|
+
子代理后,子代理成了可续、可寻址的同事,生命周期变长;本机 116 个子代理里 8 个(6.9%)至少折叠过一次,
|
|
185
|
+
而折叠后父代理照样找它,它却只记得摘要。
|
|
186
|
+
|
|
187
|
+
折叠的判定用读数而不是计数器:已登记 + 读数掉到**登记时读数的 90% 以下**。读数只在窗口被重写时才会掉下来,
|
|
188
|
+
掉几个百分点属于工具目录进出(`defer_loading`)之类的抖动,不当折叠——否则子会话会去读一份它还不需要的档。
|
|
189
|
+
|
|
190
|
+
一个时序细节:提醒发布在 `agent/request` 里,而该步的 runtime-context 快照在它之前就物化了,所以模型**下一步**
|
|
191
|
+
才读到它。对根会话(一轮多步)没有影响;子会话若一轮只有一步,折叠后那条指回提示会等到父代理下一次叫它
|
|
192
|
+
才被读到——实测就是这样读到的,不算丢失。
|
|
193
|
+
|
|
171
194
|
**已知限制:会话权限能挡住交接档写入。** 交接档在 `$DSH_HOME/handoffs/…`,而宿主按会话权限给模型的文件写入设了沙箱
|
|
172
195
|
(`@deepseek-ai/dsh-fs-sandbox` 的 `checkedTarget`:按权限模式判断目标是否落在可写根内):
|
|
173
196
|
|
|
@@ -272,6 +295,7 @@ dsh plugin --profile web add dsh-context-truth
|
|
|
272
295
|
| `handoffAt` | `0.85` | 交接提醒的准备点,作为**折叠点**的比例(不是窗口比例) |
|
|
273
296
|
| `handoffDir` | `''` | 交接档目录;空=`$DSH_HOME/handoffs`,相对路径也落在 `$DSH_HOME` 下,绝不进工作区 |
|
|
274
297
|
| `handoffReminders` | `3` | 每个周期最多提醒几次(含轮末唤醒) |
|
|
298
|
+
| `subagentHandoff` | `false` | 委派会话也提醒写交接档:写文件即登记、不唤醒、不折叠,折叠后只指回路径(见上文) |
|
|
275
299
|
| `foldAtIdle` | `false` | 旧开关,等价于 `foldMode: idle`;显式 `false` 等价于 `off`,只对没写过 `foldMode` 的配置生效 |
|
|
276
300
|
| `foldAtIdleMarginPercent` | `5` | 余量 ≤ 窗口的百分之几时触发(1M 窗口 = 50K) |
|
|
277
301
|
| `foldAtIdleMethod` | `auto` | 折叠请求通道:`auto`=先走该会话的 `/compact`(realm 视图)再回退到服务候选链;`command`/`service` 强制其一。`service` 是最可预测的一档(本 profile 用它) |
|
|
@@ -336,7 +360,7 @@ runtime-context 快照**字节不变就不会重发**,所以读数按台阶量
|
|
|
336
360
|
## 验证
|
|
337
361
|
|
|
338
362
|
```sh
|
|
339
|
-
npm test #
|
|
363
|
+
npm test # 82 项单测:分档/迟滞/量化/自由窗口/折叠定义/空闲折叠与护栏/配置热更新/schema 默认值一致性/节奏估算/降级/摘要改写/形似请求排除/幂等/交接式折叠(提醒、登记、轮末折叠、单次激活、重启恢复、回收、轮末唤醒)/委派会话的交接档(写文件即登记、折叠判定、折叠后指回、默认关)/客户端设置卡结构/交接提醒走 runtime context 而非消息,以及实时切模式后提醒随模式消失
|
|
340
364
|
```
|
|
341
365
|
|
|
342
366
|
装好后开一个新会话发一条消息,展开该轮的 runtime context 块应看到
|
package/lib/index.js
CHANGED
|
@@ -285,6 +285,21 @@ const DEFAULT_OPTIONS = {
|
|
|
285
285
|
* which is the behaviour this mode exists to improve on — so keep it above 1.
|
|
286
286
|
*/
|
|
287
287
|
handoffReminders: 3,
|
|
288
|
+
/**
|
|
289
|
+
* Extend the handoff reminder to DELEGATED sessions (default off).
|
|
290
|
+
*
|
|
291
|
+
* A delegate gets no fold from this plugin — the host's pressure fold owns it —
|
|
292
|
+
* so this is deliberately the reduced shape: remind it past the prep point, let
|
|
293
|
+
* the plain file be the registration (a child never gets `handoff_ready`), and
|
|
294
|
+
* after the fold point hand it a pointer back to that note. No wake and no fold:
|
|
295
|
+
* a delegate's turns belong to whoever delegated it, and a `followup` injected
|
|
296
|
+
* into that arrangement would break the parent's sequencing.
|
|
297
|
+
*
|
|
298
|
+
* Off by default because it changes what every child session's model is told,
|
|
299
|
+
* and most children never get near the prep point (measured on this host: 8 of
|
|
300
|
+
* 116 delegated sessions were ever folded, 6.9%).
|
|
301
|
+
*/
|
|
302
|
+
subagentHandoff: false,
|
|
288
303
|
}
|
|
289
304
|
|
|
290
305
|
/**
|
|
@@ -356,6 +371,7 @@ export const Config = z.object({
|
|
|
356
371
|
handoffAt: z.number().min(0.5).max(0.95).default(0.85).volatile(),
|
|
357
372
|
handoffDir: z.string().default('').volatile(),
|
|
358
373
|
handoffReminders: z.number().step(1).min(0).default(3).volatile(),
|
|
374
|
+
subagentHandoff: z.boolean().default(false).volatile(),
|
|
359
375
|
})
|
|
360
376
|
|
|
361
377
|
/** Clamp a numeric option, falling back when it is not a finite number. */
|
|
@@ -468,6 +484,7 @@ function resolveOptions(config) {
|
|
|
468
484
|
0,
|
|
469
485
|
Math.round(num(merged.handoffReminders, DEFAULT_OPTIONS.handoffReminders)),
|
|
470
486
|
),
|
|
487
|
+
subagentHandoff: merged.subagentHandoff === true,
|
|
471
488
|
}
|
|
472
489
|
}
|
|
473
490
|
|
|
@@ -1207,13 +1224,15 @@ export function apply(ctx, config) {
|
|
|
1207
1224
|
|
|
1208
1225
|
// Handoff prep travels as runtime context, not as a message.
|
|
1209
1226
|
//
|
|
1210
|
-
//
|
|
1211
|
-
//
|
|
1212
|
-
//
|
|
1213
|
-
//
|
|
1214
|
-
//
|
|
1215
|
-
//
|
|
1216
|
-
//
|
|
1227
|
+
// A notice published here is rendered by the **next** request's snapshot, not by
|
|
1228
|
+
// the one that decided to remind. Measured on 0.2.2 (delegate drill, 2026-11-20):
|
|
1229
|
+
// publish at 19:50:08.943, and the block appears in the step whose snapshot is
|
|
1230
|
+
// materialized at 19:50:11 — the snapshot is taken before that step's
|
|
1231
|
+
// `agent/request` waterfall runs, so the same step cannot carry it. Consequences
|
|
1232
|
+
// worth keeping in mind: a round's LAST step can never render a notice (the next
|
|
1233
|
+
// round's first step does), and a single-step turn shows it one turn later.
|
|
1234
|
+
// `prepare` retires the session's previous notice before deciding anything, so
|
|
1235
|
+
// the paragraph is still rendered exactly once rather than on every step.
|
|
1217
1236
|
const handoffNotices = new Map()
|
|
1218
1237
|
|
|
1219
1238
|
if (resolve().enabled) {
|
|
@@ -1630,6 +1649,95 @@ function handoffPrepMessage({ path, trigger, recycled, options }) {
|
|
|
1630
1649
|
].join('\n')
|
|
1631
1650
|
}
|
|
1632
1651
|
|
|
1652
|
+
/**
|
|
1653
|
+
* Delegate variant of the prep reminder.
|
|
1654
|
+
*
|
|
1655
|
+
* Two things must NOT be copied from the root text: `handoff_ready` (a child's
|
|
1656
|
+
* call is refused on purpose, and telling it to call a refusing tool is worse than
|
|
1657
|
+
* saying nothing) and the promise that the fold runs at this round's end (nothing
|
|
1658
|
+
* here folds a delegate; the host does it at the pressure point, whenever that
|
|
1659
|
+
* lands). What survives is the part that matters: the one allowed path, the
|
|
1660
|
+
* section checklist, and the bar — a reader with no conversation history.
|
|
1661
|
+
*/
|
|
1662
|
+
function handoffDelegatePrepMessage({ path, trigger, recycled, options }) {
|
|
1663
|
+
const percent = foldPointPercent(trigger.used, trigger.threshold)
|
|
1664
|
+
const used = formatTokens(trigger.used)
|
|
1665
|
+
const point = formatTokens(trigger.threshold)
|
|
1666
|
+
const recycledNote =
|
|
1667
|
+
recycled > 0
|
|
1668
|
+
? options.language === 'zh'
|
|
1669
|
+
? `(按回收策略删掉了上一份 ${recycled} 个档文件。)`
|
|
1670
|
+
: ` (recycled ${recycled} older note file(s) per the recycling rule.)`
|
|
1671
|
+
: ''
|
|
1672
|
+
if (options.language === 'zh') {
|
|
1673
|
+
return [
|
|
1674
|
+
`[交接式折叠 · 子代理准备] 这个被委派的会话已用到折叠点的 ${percent}%(${used} / ${point})。折叠由宿主在折叠点执行,不看你的进度;它不是本插件的折叠,也不会给你开新轮。`,
|
|
1675
|
+
'',
|
|
1676
|
+
'现在写一份交接档给折叠后的你自己,然后继续手头的活(不要为写档中断任务):',
|
|
1677
|
+
`1) 路径必须是这个,不要改:${path}`,
|
|
1678
|
+
'2) 按这几节写,标准是「不看对话历史也能立刻接上」:',
|
|
1679
|
+
' - 当前目标(一句话)',
|
|
1680
|
+
' - 已定案(拍过板的决定,注明勿再复议)',
|
|
1681
|
+
' - 已验证的事实与证据(命令输出、file:line)',
|
|
1682
|
+
' - 待办(按序,标出下一步的第一个动作)',
|
|
1683
|
+
' - 关键路径',
|
|
1684
|
+
' - 坑(试过并失败的路线,避免重试)',
|
|
1685
|
+
'3) 写完就够了,不要调用工具登记:**文件本身就是登记**(`handoff_ready` 对子代理一律拒答)。',
|
|
1686
|
+
`4) 这是临时文件:下一次交接生成时它会被回收${recycledNote}。要长期保留的内容写进项目里,不要留在这里。`,
|
|
1687
|
+
'',
|
|
1688
|
+
'折叠之后,你下一次被唤醒的那个请求里会把这份档的路径再指给你一次。',
|
|
1689
|
+
].join('\n')
|
|
1690
|
+
}
|
|
1691
|
+
return [
|
|
1692
|
+
`[handoff fold · subagent prepare] This delegated session is at ${percent}% of its fold point (${used} / ${point}). The fold is the host's, at the fold point, regardless of where you are in your work; it is not this plugin's fold and it will not open a turn for you.`,
|
|
1693
|
+
'',
|
|
1694
|
+
'Write a handoff note for the post-fold you, then continue your work (do not interrupt the task to do this):',
|
|
1695
|
+
`1) The path must be exactly this one, do not change it: ${path}`,
|
|
1696
|
+
'2) Use these sections; the bar is "a reader with no conversation history can continue":',
|
|
1697
|
+
' - current goal (one line)',
|
|
1698
|
+
' - decisions already made (note that they are settled)',
|
|
1699
|
+
' - verified facts and their evidence (command output, file:line)',
|
|
1700
|
+
' - next steps in order, with the very next action marked',
|
|
1701
|
+
' - key paths',
|
|
1702
|
+
' - dead ends already tried, so they are not retried',
|
|
1703
|
+
'3) Writing the file is the whole registration; do not call a tool for it (`handoff_ready` refuses a delegated session on purpose).',
|
|
1704
|
+
`4) This is a temporary file: it is recycled when the next handoff is created${recycledNote}. Anything that must outlive it belongs in the project.`,
|
|
1705
|
+
'',
|
|
1706
|
+
'After the fold, the next request you serve will point you back at this note.',
|
|
1707
|
+
].join('\n')
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
/**
|
|
1711
|
+
* Delegate variant of the activation turn: a pointer, not a turn.
|
|
1712
|
+
*
|
|
1713
|
+
* A delegate cannot be woken without breaking whoever is driving it, so the
|
|
1714
|
+
* post-fold step is a runtime-context block on the request the parent causes
|
|
1715
|
+
* next. It therefore has to carry the "read it first" instruction itself — there
|
|
1716
|
+
* is no first user message of an activation round to lean on.
|
|
1717
|
+
*/
|
|
1718
|
+
function handoffDelegateFoldMessage({ path, options }) {
|
|
1719
|
+
if (options.language === 'zh') {
|
|
1720
|
+
return [
|
|
1721
|
+
'[交接式折叠 · 子代理折叠后] 你的窗口在上一次请求之后被折叠过了:细节已经不在上下文里,摘要是有损重建。',
|
|
1722
|
+
'折叠前你自己写过一份交接档,它还在 —— 先读它再动手:',
|
|
1723
|
+
path,
|
|
1724
|
+
'',
|
|
1725
|
+
'1) 先 read 这个文件。它比摘要可靠:那里是你折叠前的第一手记录。',
|
|
1726
|
+
'2) 从它的「待办」继续,不要重新推导档里已经写明的结论。',
|
|
1727
|
+
'3) 如果档里的描述和你现在看到的实际情况冲突,以实际情况为准,并在给委派方的汇报里说清分歧。',
|
|
1728
|
+
].join('\n')
|
|
1729
|
+
}
|
|
1730
|
+
return [
|
|
1731
|
+
'[handoff fold · subagent, after the fold] Your window was folded since your last request: the details are no longer in context and the summary is a lossy reconstruction.',
|
|
1732
|
+
'You wrote yourself a handoff note before that happened, and it is still there — read it before you act:',
|
|
1733
|
+
path,
|
|
1734
|
+
'',
|
|
1735
|
+
'1) Read that file first. It is your own first-hand record from before the fold, and it is more reliable than the summary.',
|
|
1736
|
+
'2) Continue from its "next steps"; do not re-derive conclusions it already states.',
|
|
1737
|
+
'3) Where it disagrees with what you observe now, trust the observation and say where they diverged when you report back.',
|
|
1738
|
+
].join('\n')
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1633
1741
|
/**
|
|
1634
1742
|
* Wake message: one line that opens the round.
|
|
1635
1743
|
*
|
|
@@ -1685,7 +1793,14 @@ function emptyHandoffState(sessionId) {
|
|
|
1685
1793
|
return { version: 1, sessionId, foldNo: 0, reminders: 0, reminderTurn: null, ready: null, pending: null, wokenAt: null }
|
|
1686
1794
|
}
|
|
1687
1795
|
|
|
1688
|
-
/**
|
|
1796
|
+
/**
|
|
1797
|
+
* A registered note, or `null` when the record is unusable.
|
|
1798
|
+
*
|
|
1799
|
+
* `used` is the occupancy at the moment the note was seen. The root path writes it
|
|
1800
|
+
* for the record and never reads it; the delegate path reads it to tell a real fold
|
|
1801
|
+
* (the reading halves) from a small trim (the reading wobbles), because a delegate's
|
|
1802
|
+
* fold is somebody else's decision and cannot be inferred from a fold count.
|
|
1803
|
+
*/
|
|
1689
1804
|
function normalizeReady(value) {
|
|
1690
1805
|
if (value === null || typeof value !== 'object') return null
|
|
1691
1806
|
if (typeof value.path !== 'string' || value.path === '') return null
|
|
@@ -1693,6 +1808,7 @@ function normalizeReady(value) {
|
|
|
1693
1808
|
path: value.path,
|
|
1694
1809
|
chars: Number.isFinite(value.chars) ? value.chars : 0,
|
|
1695
1810
|
at: Number.isFinite(value.at) ? value.at : 0,
|
|
1811
|
+
used: Number.isFinite(value.used) && value.used > 0 ? value.used : 0,
|
|
1696
1812
|
}
|
|
1697
1813
|
}
|
|
1698
1814
|
|
|
@@ -1954,6 +2070,115 @@ function installHandoffFold(ctx, resolve, services, notices) {
|
|
|
1954
2070
|
return { path, trigger, recycled }
|
|
1955
2071
|
}
|
|
1956
2072
|
|
|
2073
|
+
/**
|
|
2074
|
+
* Step 1c: the delegate variant of step 1 (`subagentHandoff`, opt-in).
|
|
2075
|
+
*
|
|
2076
|
+
* A delegated session has the same blind spot as a root one — the fold rewrites
|
|
2077
|
+
* the window and the first-hand record of what it was doing goes with it — but
|
|
2078
|
+
* none of the same levers: nothing here may fold it (the host owns that) and
|
|
2079
|
+
* nothing here may wake it (its parent owns its turns). So the variant is
|
|
2080
|
+
* exactly: remind, take the FILE as the registration, and after the fold hand
|
|
2081
|
+
* back the way to that note.
|
|
2082
|
+
*
|
|
2083
|
+
* The fold is detected from the reading, not from a fold counter: `used` only
|
|
2084
|
+
* drops when somebody rewrote the window, so a registered note plus a reading
|
|
2085
|
+
* below 90% of the reading it was registered at IS the fold. Anything gentler is
|
|
2086
|
+
* a trim (a tool catalog coming and going, `defer_loading`) and is ignored. The
|
|
2087
|
+
* 10% band is what keeps a 2K wobble from being reported as a fold.
|
|
2088
|
+
*/
|
|
2089
|
+
function prepareDelegate(options, session, turn) {
|
|
2090
|
+
const sessionId = sessionIdOf(session)
|
|
2091
|
+
if (sessionId === '') return
|
|
2092
|
+
// Same one-shot discipline as the root path: retire the previous request's
|
|
2093
|
+
// block before this request decides anything.
|
|
2094
|
+
notices.delete(sessionId)
|
|
2095
|
+
const state = stateFor(options, sessionId)
|
|
2096
|
+
const root = handoffRoot(options)
|
|
2097
|
+
const path = handoffPathFor(root, sessionId, state.foldNo + 1)
|
|
2098
|
+
const trigger = handoffTrigger(readOccupancy(ctx, session, options, services.meter), options)
|
|
2099
|
+
|
|
2100
|
+
// Discovery. A child never gets `handoff_ready` (the tool refuses delegates on
|
|
2101
|
+
// purpose), so requiring a registration call would make every reminder a dead
|
|
2102
|
+
// end: the file the model wrote is the registration.
|
|
2103
|
+
const chars = handoffFileChars(path)
|
|
2104
|
+
if (chars !== null && state.ready === null) {
|
|
2105
|
+
state.ready = {
|
|
2106
|
+
path,
|
|
2107
|
+
chars,
|
|
2108
|
+
at: Date.now(),
|
|
2109
|
+
used: Number.isFinite(trigger.used) && trigger.used > 0 ? trigger.used : 0,
|
|
2110
|
+
}
|
|
2111
|
+
persist(options, sessionId, state)
|
|
2112
|
+
logLine(ctx, 'info', 'context-truth: delegate %s wrote its handoff note (%s chars) -> %s', sessionId, chars, path)
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
if (state.ready !== null && Number.isFinite(trigger.used)) {
|
|
2116
|
+
const note = state.ready
|
|
2117
|
+
const folded = note.used > 0 && trigger.used < note.used * 0.9
|
|
2118
|
+
// A note found while already below the prep point was written before this
|
|
2119
|
+
// plugin started watching (a restart, or the option switched on mid-session),
|
|
2120
|
+
// so the fold it was written for has already happened.
|
|
2121
|
+
const discoveredLate = note.used === 0 && !trigger.prepare
|
|
2122
|
+
if (folded || discoveredLate) {
|
|
2123
|
+
state.foldNo += 1
|
|
2124
|
+
state.ready = null
|
|
2125
|
+
state.reminders = 0
|
|
2126
|
+
state.reminderTurn = turn === undefined ? null : turn
|
|
2127
|
+
persist(options, sessionId, state)
|
|
2128
|
+
notices.set(sessionId, handoffDelegateFoldMessage({ path: note.path, options }))
|
|
2129
|
+
logLine(
|
|
2130
|
+
ctx,
|
|
2131
|
+
'info',
|
|
2132
|
+
'context-truth: delegate %s was folded since it wrote %s (used=%s, registered at=%s, cause=%s); pointing it back',
|
|
2133
|
+
sessionId,
|
|
2134
|
+
note.path,
|
|
2135
|
+
trigger.used,
|
|
2136
|
+
note.used > 0 ? note.used : 'unknown',
|
|
2137
|
+
folded ? 'reading dropped' : 'note found below the prep point',
|
|
2138
|
+
)
|
|
2139
|
+
return
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
if (!trigger.prepare) return
|
|
2144
|
+
// A pending note owns the next reminder; nagging on top of it only costs turns.
|
|
2145
|
+
if (state.ready !== null) return
|
|
2146
|
+
if (state.reminders >= options.handoffReminders) {
|
|
2147
|
+
noteOnce(
|
|
2148
|
+
`delegate-budget:${sessionId}`,
|
|
2149
|
+
'info',
|
|
2150
|
+
'context-truth: delegate handoff reminder budget spent for %s (%s in this cycle); the host pressure fold takes this one',
|
|
2151
|
+
sessionId,
|
|
2152
|
+
options.handoffReminders,
|
|
2153
|
+
)
|
|
2154
|
+
return
|
|
2155
|
+
}
|
|
2156
|
+
if (turn !== undefined && state.reminderTurn === turn) return
|
|
2157
|
+
const doomed = handoffReapable(root, sessionId, [path, handoffPathFor(root, sessionId, state.foldNo)])
|
|
2158
|
+
// Runtime context, exactly like the root reminder, and with the same one-step
|
|
2159
|
+
// delay: this request's snapshot is already materialized, so the child reads it
|
|
2160
|
+
// on its next step (or next turn, if this was its last step). No user message
|
|
2161
|
+
// is fabricated on the parent's behalf.
|
|
2162
|
+
notices.set(sessionId, handoffDelegatePrepMessage({ path, trigger, recycled: doomed.length, options }))
|
|
2163
|
+
// Reap after the notice lands, so a failed delivery never costs the previous note.
|
|
2164
|
+
const recycled = deleteFiles(doomed)
|
|
2165
|
+
state.reminders += 1
|
|
2166
|
+
state.reminderTurn = turn === undefined ? null : turn
|
|
2167
|
+
persist(options, sessionId, state)
|
|
2168
|
+
logLine(
|
|
2169
|
+
ctx,
|
|
2170
|
+
'info',
|
|
2171
|
+
'context-truth: delegate handoff prep %s/%s for %s (used=%s of the %s fold point, recycled=%s) -> %s',
|
|
2172
|
+
state.reminders,
|
|
2173
|
+
options.handoffReminders,
|
|
2174
|
+
sessionId,
|
|
2175
|
+
trigger.used,
|
|
2176
|
+
trigger.threshold,
|
|
2177
|
+
recycled,
|
|
2178
|
+
path,
|
|
2179
|
+
)
|
|
2180
|
+
}
|
|
2181
|
+
|
|
1957
2182
|
/**
|
|
1958
2183
|
* Step 1: remind the model while it can still act. Runs inside the
|
|
1959
2184
|
* `agent/request` waterfall, so it stays synchronous and swallows everything —
|
|
@@ -1968,7 +2193,11 @@ function installHandoffFold(ctx, resolve, services, notices) {
|
|
|
1968
2193
|
if (options.foldMode !== 'handoff') return
|
|
1969
2194
|
const session = agent === null || agent === undefined ? undefined : agent.session
|
|
1970
2195
|
if (session === null || session === undefined) return
|
|
1971
|
-
if (isDelegate(session))
|
|
2196
|
+
if (isDelegate(session)) {
|
|
2197
|
+
// Reduced variant (opt-in): remind and point back, never wake and never fold.
|
|
2198
|
+
if (options.subagentHandoff) prepareDelegate(options, session, turn)
|
|
2199
|
+
return
|
|
2200
|
+
}
|
|
1972
2201
|
const sessionId = sessionIdOf(session)
|
|
1973
2202
|
if (sessionId === '') return
|
|
1974
2203
|
// Retire the previous request's notice before this request decides anything,
|
|
@@ -2018,9 +2247,9 @@ function installHandoffFold(ctx, resolve, services, notices) {
|
|
|
2018
2247
|
const keep = [path, handoffPathFor(root, sessionId, state.foldNo)]
|
|
2019
2248
|
const doomed = handoffReapable(root, sessionId, keep)
|
|
2020
2249
|
// Publish the reminder into runtime context instead of queueing a user
|
|
2021
|
-
// message:
|
|
2022
|
-
//
|
|
2023
|
-
//
|
|
2250
|
+
// message: it is as timely as `inject` and never looks like something the human
|
|
2251
|
+
// typed. It is rendered by the NEXT request's snapshot (see the note above
|
|
2252
|
+
// `handoffNotices`), which for a multi-step round is one step away.
|
|
2024
2253
|
notices.set(sessionId, handoffPrepMessage({ path, trigger, recycled: doomed.length, options }))
|
|
2025
2254
|
// Reap after the notice lands, so a failed delivery does not cost the model
|
|
2026
2255
|
// its previous note for nothing.
|
|
@@ -2314,6 +2543,14 @@ function installHandoffFold(ctx, resolve, services, notices) {
|
|
|
2314
2543
|
return zh ? '✗ handoff_ready 需要一个活着的会话,没有登记任何东西。' : '✗ handoff_ready needs a live session; nothing was registered.'
|
|
2315
2544
|
}
|
|
2316
2545
|
if (isDelegate(session)) {
|
|
2546
|
+
// With the delegate variant on, "refused" must still be actionable: the
|
|
2547
|
+
// child's registration is the file itself, and this call is the one step
|
|
2548
|
+
// it does NOT need.
|
|
2549
|
+
if (options.subagentHandoff) {
|
|
2550
|
+
return zh
|
|
2551
|
+
? '✗ 当前是委派会话:交接式折叠不在子会话上执行,**写文件本身就是登记**,不需要调用这个工具。'
|
|
2552
|
+
: '✗ this is a delegated session: handoff folding does not run on a child, and writing the note file IS the registration — this tool is not needed.'
|
|
2553
|
+
}
|
|
2317
2554
|
return zh
|
|
2318
2555
|
? '✗ 当前是委派会话,交接式折叠只在根会话上生效;没有登记任何东西。'
|
|
2319
2556
|
: '✗ this is a delegated session, and handoff folding only applies to a root session; nothing was registered.'
|
|
@@ -2650,6 +2887,8 @@ export const internals = {
|
|
|
2650
2887
|
handoffRoot,
|
|
2651
2888
|
handoffPathFor,
|
|
2652
2889
|
handoffPrepMessage,
|
|
2890
|
+
handoffDelegatePrepMessage,
|
|
2891
|
+
handoffDelegateFoldMessage,
|
|
2653
2892
|
handoffWakeMessage,
|
|
2654
2893
|
handoffActivationMessage,
|
|
2655
2894
|
handoffReapable,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-context-truth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "DeepSeek Harness plugin: tells the model its real, host-measured context occupancy every turn — and keeps compaction summaries from carrying false \"context is nearly exhausted\" claims.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"deepseek-harness",
|
package/test/index.test.js
CHANGED
|
@@ -8,7 +8,7 @@ import { apply, internals, name, inject, Config, CONTEXT_NAME, VARIABLE_NAME, HA
|
|
|
8
8
|
|
|
9
9
|
const { resolveOptions, readOccupancy, settleBand, quantize, quantizeHeadroom, headroomDisplayStep, render, patchCompactionInstruction, patchCompactionRequest, isCompactionRequest, isTaggedCompactionRequest, messageText, DEFAULT_OPTIONS, DEFAULT_COMPACTION_RULES, PACE_LADDER, median, nextPaceState, samplePace, paceSteps, paceRung, settlePace, paceSentence, tightClause, foldClause, shouldFoldAtIdle, backendIdentity } = internals
|
|
10
10
|
|
|
11
|
-
const { resolveFoldMode, handoffTrigger, handoffRoot, handoffPathFor, handoffPrepMessage, handoffWakeMessage, handoffActivationMessage, handoffReapable, sweepHandoffs, handoffFileChars, safeSegment, sessionIdOf, isDelegate, samePath, HANDOFF_TOOL_NAME, HANDOFF_MIN_CHARS, HANDOFF_MAX_AGE_MS, HANDOFF_STATE_FILE } = internals
|
|
11
|
+
const { resolveFoldMode, handoffTrigger, handoffRoot, handoffPathFor, handoffPrepMessage, handoffDelegatePrepMessage, handoffDelegateFoldMessage, handoffWakeMessage, handoffActivationMessage, handoffReapable, sweepHandoffs, handoffFileChars, safeSegment, sessionIdOf, isDelegate, samePath, HANDOFF_TOOL_NAME, HANDOFF_MIN_CHARS, HANDOFF_MAX_AGE_MS, HANDOFF_STATE_FILE } = internals
|
|
12
12
|
|
|
13
13
|
/** A 1M window reserving the DeepSeek adapter's default 256K completion tokens. */
|
|
14
14
|
const WINDOW = 1000000
|
|
@@ -1718,6 +1718,202 @@ test('a delegated session is never prepared, registered, or folded', async (t) =
|
|
|
1718
1718
|
assert.equal(isDelegate(handoffSession('root', { depth: 0 })), false)
|
|
1719
1719
|
})
|
|
1720
1720
|
|
|
1721
|
+
test('the delegate variant is off by default, and off means off', async (t) => {
|
|
1722
|
+
// 子代理变体是可选行为:默认配置下委派会话的提示词必须逐字节不变。
|
|
1723
|
+
const root = scratchRoot(t)
|
|
1724
|
+
const { meter } = movableMeter(630000)
|
|
1725
|
+
const agent = handoffAgent(handoffSession('session-child-off', { depth: 1 }))
|
|
1726
|
+
const { ctx, registered } = stubContext({ meter, compaction: { compactNow: () => Promise.resolve({}) } })
|
|
1727
|
+
|
|
1728
|
+
assert.equal(DEFAULT_OPTIONS.subagentHandoff, false, 'a new behaviour ships off')
|
|
1729
|
+
assert.equal(resolveOptions({}).subagentHandoff, false)
|
|
1730
|
+
assert.equal(resolveOptions(Config({ subagentHandoff: true })).subagentHandoff, true, 'the form can turn it on')
|
|
1731
|
+
|
|
1732
|
+
apply(ctx, { foldMode: 'handoff', handoffDir: root })
|
|
1733
|
+
await requestStep(registered, agent, 1)
|
|
1734
|
+
assert.equal(handoffBlock(registered, agent), '', 'the default config keeps a delegate silent')
|
|
1735
|
+
})
|
|
1736
|
+
|
|
1737
|
+
test('a delegate is reminded past the prep point, and its note file is the registration', async (t) => {
|
|
1738
|
+
const root = scratchRoot(t)
|
|
1739
|
+
const { box, meter } = movableMeter(620000)
|
|
1740
|
+
const folds = []
|
|
1741
|
+
const agent = handoffAgent(handoffSession('session-child-prep', { depth: 1 }))
|
|
1742
|
+
const { ctx, registered } = stubContext({
|
|
1743
|
+
meter,
|
|
1744
|
+
compaction: { compactNow: (...args) => (folds.push(args), Promise.resolve({})) },
|
|
1745
|
+
})
|
|
1746
|
+
apply(ctx, { foldMode: 'handoff', handoffDir: root, subagentHandoff: true })
|
|
1747
|
+
|
|
1748
|
+
const notePath = handoffPathFor(root, 'session-child-prep', 1)
|
|
1749
|
+
await requestStep(registered, agent, 1)
|
|
1750
|
+
const block = handoffBlock(registered, agent)
|
|
1751
|
+
assert.match(block, /handoff fold · subagent prepare/)
|
|
1752
|
+
assert.ok(block.includes(notePath), 'the one allowed path is spelled out')
|
|
1753
|
+
assert.match(block, /Writing the file is the whole registration/)
|
|
1754
|
+
assert.doesNotMatch(block, /follow-up turn is activated/, 'the root promise would be a lie for a child')
|
|
1755
|
+
assert.equal(agent.injected.length, 0, 'a delegate is never addressed as the human')
|
|
1756
|
+
assert.equal(agent.followed.length, 0, 'and never woken: its turns belong to whoever delegated it')
|
|
1757
|
+
|
|
1758
|
+
// 拒答也必须可执行:子会话没有 handed_ready 可用,要求调用等于把提醒变成死路。
|
|
1759
|
+
const tool = registered.tools.find((definition) => definition.name === HANDOFF_TOOL_NAME)
|
|
1760
|
+
assert.match(tool.execute({}, { agent }), /writing the note file IS the registration/, 'a refused call says what to do instead')
|
|
1761
|
+
|
|
1762
|
+
// 写文件 = 登记。
|
|
1763
|
+
mkdirSync(join(root, 'session-child-prep'), { recursive: true })
|
|
1764
|
+
writeFileSync(notePath, `# note\n${'x'.repeat(400)}`, 'utf8')
|
|
1765
|
+
await requestStep(registered, agent, 1)
|
|
1766
|
+
assert.equal(handoffBlock(registered, agent), '', 'the note on disk ends the reminder')
|
|
1767
|
+
const state = JSON.parse(readFileSync(join(root, 'session-child-prep', HANDOFF_STATE_FILE), 'utf8'))
|
|
1768
|
+
assert.equal(state.ready.path, notePath)
|
|
1769
|
+
assert.equal(state.ready.used, 620000, 'the reading is kept as the fold baseline')
|
|
1770
|
+
assert.equal(state.reminders, 1)
|
|
1771
|
+
|
|
1772
|
+
// 待写的档已经在盘上:下一轮即使仍在准备点以上,也不能再发第二条提醒。
|
|
1773
|
+
box.used = 630000
|
|
1774
|
+
await requestStep(registered, agent, 2)
|
|
1775
|
+
assert.equal(handoffBlock(registered, agent), '', 'a pending note owns the next reminder slot')
|
|
1776
|
+
|
|
1777
|
+
await idleBoundary(registered, agent)
|
|
1778
|
+
assert.equal(folds.length, 0, 'nothing here folds a delegate')
|
|
1779
|
+
})
|
|
1780
|
+
|
|
1781
|
+
test('after the host folds a delegate, the next request points it back at its note', async (t) => {
|
|
1782
|
+
const root = scratchRoot(t)
|
|
1783
|
+
const { box, meter } = movableMeter(620000)
|
|
1784
|
+
const agent = handoffAgent(handoffSession('session-child-fold', { depth: 1 }))
|
|
1785
|
+
const { ctx, registered } = stubContext({ meter, compaction: { compactNow: () => Promise.resolve({}) } })
|
|
1786
|
+
apply(ctx, { foldMode: 'handoff', handoffDir: root, subagentHandoff: true })
|
|
1787
|
+
|
|
1788
|
+
const notePath = handoffPathFor(root, 'session-child-fold', 1)
|
|
1789
|
+
await requestStep(registered, agent, 1)
|
|
1790
|
+
mkdirSync(join(root, 'session-child-fold'), { recursive: true })
|
|
1791
|
+
writeFileSync(notePath, `# note\n${'x'.repeat(400)}`, 'utf8')
|
|
1792
|
+
await requestStep(registered, agent, 1)
|
|
1793
|
+
|
|
1794
|
+
box.used = 210000
|
|
1795
|
+
await requestStep(registered, agent, 2)
|
|
1796
|
+
const block = handoffBlock(registered, agent)
|
|
1797
|
+
assert.match(block, /handoff fold · subagent, after the fold/)
|
|
1798
|
+
assert.ok(block.includes(notePath), 'the pointer names the file the child wrote')
|
|
1799
|
+
const state = JSON.parse(readFileSync(join(root, 'session-child-fold', HANDOFF_STATE_FILE), 'utf8'))
|
|
1800
|
+
assert.equal(state.foldNo, 1, 'the cycle is counted, so the next note is 002')
|
|
1801
|
+
assert.equal(state.ready, null)
|
|
1802
|
+
assert.equal(state.reminders, 0)
|
|
1803
|
+
assert.equal(agent.followed.length, 0, 'the pointer rides a turn the parent already caused; nothing is woken')
|
|
1804
|
+
|
|
1805
|
+
await requestStep(registered, agent, 2)
|
|
1806
|
+
assert.equal(handoffBlock(registered, agent), '', 'the pointer is one-shot, like every other block')
|
|
1807
|
+
})
|
|
1808
|
+
|
|
1809
|
+
test('a small drop is a trim, not a fold', async (t) => {
|
|
1810
|
+
// 只有当窗口被人重写过,reading 才会掉下来;掉 3% 是工具目录/defer 之类的小抖动,
|
|
1811
|
+
// 把它当折叠会让孩子去读一份它还不需要的档。
|
|
1812
|
+
const root = scratchRoot(t)
|
|
1813
|
+
const { box, meter } = movableMeter(620000)
|
|
1814
|
+
const agent = handoffAgent(handoffSession('session-child-trim', { depth: 1 }))
|
|
1815
|
+
const { ctx, registered } = stubContext({ meter, compaction: { compactNow: () => Promise.resolve({}) } })
|
|
1816
|
+
apply(ctx, { foldMode: 'handoff', handoffDir: root, subagentHandoff: true })
|
|
1817
|
+
|
|
1818
|
+
const notePath = handoffPathFor(root, 'session-child-trim', 1)
|
|
1819
|
+
await requestStep(registered, agent, 1)
|
|
1820
|
+
mkdirSync(join(root, 'session-child-trim'), { recursive: true })
|
|
1821
|
+
writeFileSync(notePath, `# note\n${'x'.repeat(400)}`, 'utf8')
|
|
1822
|
+
await requestStep(registered, agent, 1)
|
|
1823
|
+
|
|
1824
|
+
box.used = 600000
|
|
1825
|
+
await requestStep(registered, agent, 2)
|
|
1826
|
+
assert.equal(handoffBlock(registered, agent), '', 'a reading below the prep point but not halved is not a fold')
|
|
1827
|
+
const state = JSON.parse(readFileSync(join(root, 'session-child-trim', HANDOFF_STATE_FILE), 'utf8'))
|
|
1828
|
+
assert.equal(state.foldNo, 0)
|
|
1829
|
+
assert.equal(state.ready.path, notePath, 'the note stays registered and waiting')
|
|
1830
|
+
})
|
|
1831
|
+
|
|
1832
|
+
test('a note found below the prep point after a restart still gets its pointer', async (t) => {
|
|
1833
|
+
// 状态里没有读数基线(旧版本写的,或选项刚打开),而读数已经在准备点以下:
|
|
1834
|
+
// 唯一合理的解释是折叠已经发生。不能因为“没有基线”就把孩子丢在摘要里。
|
|
1835
|
+
const root = scratchRoot(t)
|
|
1836
|
+
const dir = join(root, 'session-child-late')
|
|
1837
|
+
mkdirSync(dir, { recursive: true })
|
|
1838
|
+
const notePath = handoffPathFor(root, 'session-child-late', 1)
|
|
1839
|
+
writeFileSync(notePath, 'late note', 'utf8')
|
|
1840
|
+
writeFileSync(
|
|
1841
|
+
join(dir, HANDOFF_STATE_FILE),
|
|
1842
|
+
JSON.stringify({ version: 1, sessionId: 'session-child-late', foldNo: 0, ready: { path: notePath, chars: 9, at: 1 } }),
|
|
1843
|
+
'utf8',
|
|
1844
|
+
)
|
|
1845
|
+
const { meter } = movableMeter(300000)
|
|
1846
|
+
const agent = handoffAgent(handoffSession('session-child-late', { depth: 1 }))
|
|
1847
|
+
const { ctx, registered } = stubContext({ meter, compaction: { compactNow: () => Promise.resolve({}) } })
|
|
1848
|
+
apply(ctx, { foldMode: 'handoff', handoffDir: root, subagentHandoff: true })
|
|
1849
|
+
|
|
1850
|
+
await requestStep(registered, agent, 1)
|
|
1851
|
+
const block = handoffBlock(registered, agent)
|
|
1852
|
+
assert.match(block, /handoff fold · subagent, after the fold/)
|
|
1853
|
+
assert.ok(block.includes(notePath))
|
|
1854
|
+
const state = JSON.parse(readFileSync(join(dir, HANDOFF_STATE_FILE), 'utf8'))
|
|
1855
|
+
assert.equal(state.foldNo, 1)
|
|
1856
|
+
assert.equal(state.ready, null)
|
|
1857
|
+
})
|
|
1858
|
+
|
|
1859
|
+
test('after the fold the next note is numbered up, and the pointed-at note survives', async (t) => {
|
|
1860
|
+
const root = scratchRoot(t)
|
|
1861
|
+
const { box, meter } = movableMeter(620000)
|
|
1862
|
+
const agent = handoffAgent(handoffSession('session-child-numbered', { depth: 1 }))
|
|
1863
|
+
const { ctx, registered } = stubContext({ meter, compaction: { compactNow: () => Promise.resolve({}) } })
|
|
1864
|
+
apply(ctx, { foldMode: 'handoff', handoffDir: root, subagentHandoff: true })
|
|
1865
|
+
|
|
1866
|
+
const first = handoffPathFor(root, 'session-child-numbered', 1)
|
|
1867
|
+
await requestStep(registered, agent, 1)
|
|
1868
|
+
mkdirSync(join(root, 'session-child-numbered'), { recursive: true })
|
|
1869
|
+
writeFileSync(first, `# note\n${'x'.repeat(400)}`, 'utf8')
|
|
1870
|
+
await requestStep(registered, agent, 1)
|
|
1871
|
+
box.used = 210000
|
|
1872
|
+
await requestStep(registered, agent, 2)
|
|
1873
|
+
|
|
1874
|
+
box.used = 630000
|
|
1875
|
+
await requestStep(registered, agent, 3)
|
|
1876
|
+
const block = handoffBlock(registered, agent)
|
|
1877
|
+
assert.ok(block.includes(handoffPathFor(root, 'session-child-numbered', 2)), 'the next note is 002')
|
|
1878
|
+
assert.equal(existsSync(first), true, 'the note the fold pointer aimed at is kept, exactly like an activation target')
|
|
1879
|
+
assert.equal(agent.followed.length, 0)
|
|
1880
|
+
})
|
|
1881
|
+
|
|
1882
|
+
test('delegate reminders are one per turn and capped per cycle, and never wake', async (t) => {
|
|
1883
|
+
const root = scratchRoot(t)
|
|
1884
|
+
const { meter } = movableMeter(640000)
|
|
1885
|
+
const logs = []
|
|
1886
|
+
const agent = handoffAgent(handoffSession('session-child-budget', { depth: 1 }))
|
|
1887
|
+
const { ctx, registered } = stubContext({ meter, logs, compaction: { compactNow: () => Promise.resolve({}) } })
|
|
1888
|
+
apply(ctx, { foldMode: 'handoff', handoffDir: root, subagentHandoff: true })
|
|
1889
|
+
|
|
1890
|
+
const reminded = []
|
|
1891
|
+
for (const turn of [1, 1, 2, 3, 4]) {
|
|
1892
|
+
await requestStep(registered, agent, turn)
|
|
1893
|
+
if (handoffBlock(registered, agent).includes('subagent prepare')) reminded.push(turn)
|
|
1894
|
+
}
|
|
1895
|
+
assert.deepEqual(reminded, [1, 2, 3], 'one per turn, three per cycle')
|
|
1896
|
+
assert.equal(agent.followed.length, 0, 'a delegate is never woken, whatever the budget says')
|
|
1897
|
+
assert.ok(
|
|
1898
|
+
logs.some(([, message]) => String(message).includes('delegate handoff reminder budget spent')),
|
|
1899
|
+
'the budget stop is logged',
|
|
1900
|
+
)
|
|
1901
|
+
})
|
|
1902
|
+
|
|
1903
|
+
test('the delegate messages exist in both languages', () => {
|
|
1904
|
+
const trigger = { prepare: true, used: 620000, threshold: 727040, point: 618000, headroom: 107040 }
|
|
1905
|
+
const zh = handoffDelegatePrepMessage({ path: 'C:/x/001.handoff.md', trigger, recycled: 0, options: { language: 'zh' } })
|
|
1906
|
+
assert.match(zh, /子代理准备/)
|
|
1907
|
+
assert.ok(zh.includes('C:/x/001.handoff.md'))
|
|
1908
|
+
assert.match(zh, /文件本身就是登记/)
|
|
1909
|
+
|
|
1910
|
+
const en = handoffDelegatePrepMessage({ path: 'C:/x/001.handoff.md', trigger, recycled: 2, options: { language: 'en' } })
|
|
1911
|
+
assert.match(en, /recycled 2 older note file/)
|
|
1912
|
+
|
|
1913
|
+
assert.match(handoffDelegateFoldMessage({ path: 'C:/x/001.handoff.md', options: { language: 'zh' } }), /子代理折叠后/)
|
|
1914
|
+
assert.match(handoffDelegateFoldMessage({ path: 'C:/x/001.handoff.md', options: { language: 'en' } }), /subagent, after the fold/)
|
|
1915
|
+
})
|
|
1916
|
+
|
|
1721
1917
|
test('the activation target survives the prep that hands out the next note', async (t) => {
|
|
1722
1918
|
const root = scratchRoot(t)
|
|
1723
1919
|
const dir = join(root, 'session-gc')
|