@zhuxixi/pi-agent-board 0.13.1 → 0.14.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.
Files changed (80) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/README.md +15 -8
  3. package/docs/SECTION_FOLDING.md +53 -0
  4. package/docs/superpowers/plans/2026-10-23-issue-222-attach-ready.md +290 -0
  5. package/docs/superpowers/plans/2026-10-28-issue-257-legacy-pty-runner-endpoint-cleanup.md +256 -0
  6. package/docs/superpowers/plans/2026-11-03-cwd-favorites-full-pool.md +266 -0
  7. package/docs/superpowers/plans/2026-11-03-issue-266-alt-h-detach.md +467 -0
  8. package/docs/superpowers/plans/2026-11-04-issue-270-scenario-stdout-flush.md +584 -0
  9. package/docs/superpowers/plans/2026-11-05-issue-278-ctrl-g-external-editor.md +381 -0
  10. package/docs/superpowers/plans/2026-11-06-issue-280-frame-witness-delivery.md +430 -0
  11. package/docs/superpowers/plans/2026-11-07-issue-284-attach-recovery.md +115 -0
  12. package/docs/superpowers/plans/2026-11-07-issue-284-u1-u2-trial-guide.md +39 -0
  13. package/docs/superpowers/plans/2026-11-08-host-restart-model-override.md +894 -0
  14. package/docs/superpowers/plans/2026-11-08-issue-288-cap-floor.md +51 -0
  15. package/docs/superpowers/plans/2026-11-09-dashboard-fold-flake.md +166 -0
  16. package/docs/superpowers/plans/2026-11-09-issue-273-harness-console-log-exit.md +369 -0
  17. package/docs/superpowers/plans/2026-11-10-issue-258-board-selection-settle.md +760 -0
  18. package/docs/superpowers/plans/2026-11-13-issue-294-snapshot-resize-intent.md +590 -0
  19. package/docs/superpowers/plans/2026-11-16-greeting-title-poll.md +838 -0
  20. package/docs/superpowers/plans/2026-11-22-issue-297-compaction-mirror.md +797 -0
  21. package/docs/superpowers/plans/2026-11-23-issue-254-cursor-keep-position.md +663 -0
  22. package/docs/superpowers/plans/2026-11-23-issue-95-legacy-quarantine.md +320 -0
  23. package/docs/superpowers/specs/2026-10-23-issue-222-attach-ready-design.md +204 -0
  24. package/docs/superpowers/specs/2026-10-28-issue-257-legacy-pty-runner-endpoint-cleanup-design.md +197 -0
  25. package/docs/superpowers/specs/2026-11-03-cwd-favorites-full-pool-design.md +106 -0
  26. package/docs/superpowers/specs/2026-11-03-issue-266-alt-h-detach-design.md +185 -0
  27. package/docs/superpowers/specs/2026-11-04-issue-270-scenario-stdout-flush-design.md +176 -0
  28. package/docs/superpowers/specs/2026-11-05-issue-278-ctrl-g-external-editor-design.md +185 -0
  29. package/docs/superpowers/specs/2026-11-07-issue-284-attach-recovery-design.md +166 -0
  30. package/docs/superpowers/specs/2026-11-08-host-restart-model-override-design.md +115 -0
  31. package/docs/superpowers/specs/2026-11-08-issue-288-cap-floor-design.md +86 -0
  32. package/docs/superpowers/specs/2026-11-09-dashboard-fold-flake-design.md +109 -0
  33. package/docs/superpowers/specs/2026-11-09-issue-273-harness-console-log-exit-design.md +182 -0
  34. package/docs/superpowers/specs/2026-11-10-issue-258-board-selection-settle-design.md +109 -0
  35. package/docs/superpowers/specs/2026-11-13-issue-294-snapshot-resize-intent-design.md +141 -0
  36. package/docs/superpowers/specs/2026-11-16-greeting-title-poll-design.md +174 -0
  37. package/docs/superpowers/specs/2026-11-22-issue-297-compaction-mirror-design.md +158 -0
  38. package/docs/superpowers/specs/2026-11-22-issue-95-legacy-quarantine-design.md +209 -0
  39. package/docs/superpowers/specs/2026-11-23-issue-254-cursor-keep-position-design.md +201 -0
  40. package/package.json +1 -1
  41. package/runner/pty-runner.mjs +526 -63
  42. package/runner/title-runner.mjs +33 -24
  43. package/src/commands/agent-board.ts +5 -1
  44. package/src/core/attach-detach.mjs +27 -0
  45. package/src/core/attach-frame-proof.mjs +897 -0
  46. package/src/core/attach-frame-reporter.mjs +738 -0
  47. package/src/core/attach-frame-stream.mjs +266 -0
  48. package/src/core/attach-pr-label.mjs +12 -0
  49. package/src/core/attach-recovery.mjs +239 -0
  50. package/src/core/attach-screen-owner.mjs +55 -0
  51. package/src/core/attach-view-proof.mjs +709 -0
  52. package/src/core/auto-state.mjs +6 -1
  53. package/src/core/cwd-stats.mjs +3 -1
  54. package/src/core/dashboard-prefs.mjs +31 -0
  55. package/src/core/derive.mjs +8 -1
  56. package/src/core/events.mjs +127 -1
  57. package/src/core/foreground-projection-cache.mjs +1 -0
  58. package/src/core/frame-witness-update.mjs +35 -0
  59. package/src/core/fullscreen-selection.mjs +60 -0
  60. package/src/core/host-protocol.mjs +13 -2
  61. package/src/core/hosted-frame-witness.mjs +60 -0
  62. package/src/core/paths.mjs +10 -3
  63. package/src/core/pty-attach-render.mjs +239 -0
  64. package/src/core/pty-kitty-images.mjs +237 -0
  65. package/src/core/rows.mjs +43 -7
  66. package/src/core/session-entries.mjs +82 -0
  67. package/src/core/session-launch-prefs.mjs +45 -0
  68. package/src/core/session-view.mjs +3 -63
  69. package/src/core/store.mjs +1 -0
  70. package/src/core/terminal-attach-client.mjs +199 -20
  71. package/src/core/terminal-attach-protocol.mjs +221 -7
  72. package/src/core/terminal-model.mjs +24 -3
  73. package/src/core/terminal-snapshot.mjs +10 -0
  74. package/src/core/title-flow.mjs +66 -0
  75. package/src/core/title.mjs +101 -0
  76. package/src/core/types.mjs +17 -0
  77. package/src/index.ts +85 -0
  78. package/src/runtime/service.mjs +87 -18
  79. package/src/ui/dashboard.ts +198 -44
  80. package/src/ui/pty-attach.ts +1462 -117
@@ -0,0 +1,158 @@
1
+ # Issue #297 根因报告与修复 Spec — 看板镜像 compaction 窗口
2
+
3
+ Drafted: deepseek/deepseek-flash (selected) · deepseek/deepseek-flash (physical) @ 2026-11-16T22:24:08+08:00
4
+ Revised: deepseek/deepseek-flash (selected) · deepseek/deepseek-flash (physical) @ 2026-11-16T22:43:40+08:00(round-1:接受 R1-G1/G2/G3 修订与 R1-D1–D6 细化,spec 升 v2)
5
+ Revised: deepseek/deepseek-flash (selected) · deepseek/deepseek-flash (physical) @ 2026-11-16T23:12:00+08:00(round-2:0 真缺口;吸收 R2-D1,U1 前置去掉不存在的 `npm run build`)
6
+ Base: main @ 608a22a(0.13.1)
7
+ Status: v2-final(round-2 收敛:0 真缺口、1 细化已吸收)
8
+
9
+ ## 0. 一句话结论
10
+
11
+ 给两条事件通路(interactive/hosted 扩展钩子、JSON runner 流)各加一个 compaction 窗口的**状态标记**:窗口内把行置为 `working` 并显示 `Compacting context…`,窗口结束时按「压缩前状态」恢复;标记里持久化压缩前快照(含 `processState`),因此两条通路共用同一套 reducer 语义,且 `isAgentBusy` 派生出的全部忙守卫自动恢复。
12
+
13
+ ## 1. 根因
14
+
15
+ 看板没有订阅 compaction 事件,不是镜像逻辑写错(证据与代码坐标见 issue #297 与本目录 `research/root-cause.md`):
16
+
17
+ - 扩展事件通路只注册了 8 个事件(`src/index.ts:238-244`),compaction 的扩展钩子 `session_before_compact` / `session_compact` / `session_compact_failed` 未被注册。
18
+ - JSON 通路 `reduceEvent`(`src/core/events.mjs:144`)没有 `compaction_start` / `compaction_end` 分支。
19
+ - `syncRowEvent`(`src/runtime/service.mjs:1849`)只在 `input | before_agent_start | agent_start` 时置 `working`。
20
+
21
+ 后果不只是标签:`isAgentBusy()`(`src/core/warm-host-sweeper.mjs:23`)只认 `queued/working`,压缩期间 mark-done / hold / follow-up drain / warm-host 驱逐保护 / header Running 计数全部把行当作空闲。
22
+
23
+ pi 侧事件时序(1.5.2 `dist/core/agent-session.js`):
24
+
25
+ - interactive 手动 `/compact`:`compaction_start` → `session_before_compact` → 摘要生成 → `session_compact`;失败时触发 `session_compact_failed`(`errorMessage` 已带 `Compaction failed: ` 前缀;`aborted` 表示取消)。
26
+ - interactive 自动压缩(threshold/overflow):同样先 `session_before_compact`,成功 `session_compact`,失败 `session_compact_failed`;`willRetry=true` 表示压缩后要重试被中断的 turn。
27
+ - JSON/RPC:只发 `compaction_start` / `compaction_end`(`{reason, result|undefined, aborted, willRetry, errorMessage?}`)。
28
+ - 手动压缩在 `prepareCompaction` 失败(如「Nothing to compact」)时**仍会**走 catch 分支发 `compaction_end` + `session_compact_failed`,因此不会留下「永远 Compacting」的悬空窗口——但这也意味着 begin 与 end 之间可能只隔毫秒级(R1-G2 的竞态来源)。
29
+
30
+ ## 2. 修复设计
31
+
32
+ ### D1 — 新增 compaction 窗口字段(ViewState + RunStatus)
33
+
34
+ ```js
35
+ /**
36
+ * @typedef {Object} CompactionWindow
37
+ * @property {"running"|"failed"} phase
38
+ * @property {"manual"|"threshold"|"overflow"} reason
39
+ * @property {number} startedAt
40
+ * @property {{semanticState:SemanticState, processState:ProcessState, summary:string,
41
+ * question:string|null, error:string|null}|null} previous
42
+ * 压缩前快照(含 processState,R1-G3)。failed 标记也保留它:重试成功后恢复压缩前的 error,
43
+ * 而不是把 `Compaction failed: …` 留在行上(CR round-1 advisory)。
44
+ * @property {string|null} error 仅 phase "failed":pi 的 errorMessage 原文。
45
+ */
46
+ ```
47
+
48
+ - `ViewState` 新增可选 `compaction`;`RunStatus` 新增 `compaction`。
49
+ - `createRunStatus` / `blankState` 初始化为 `null`;**`statusFromRow` 读穿现有值** `compaction: s.compaction ?? null`(R1-D1:这里不是「初始化 null」,否则 begin 落盘的 marker 在下一个事件重建时丢失)。
50
+ - `projectViewState` 透传 `compaction: status.compaction ?? null`。
51
+ - `src/core/foreground-projection-cache.mjs` 的 `CACHED_FIELDS` 白名单**必须加入 `"compaction"`**(R1-G2):前台通路 `syncRowEvent` 入口会 `adopt` 缓存覆盖白名单字段做 read-your-writes 纠正(issue #225 机制),白名单漏掉该字段会让「begin 写盘在途时 end 到达」的毫秒级失败路径丢掉 marker,随后落入 previous 缺失分支,idle 行卡在 working + `Compacting context…`。
52
+ - 旧行无该字段:读取时按 `?? null` 容忍,**不做迁移、不写版本号**。
53
+
54
+ ### D2 — 纯函数语义(events.mjs,两条通路共用)
55
+
56
+ `beginCompaction(status, { reason }, now)`:
57
+
58
+ - 已有 marker 且 `phase === "running"`(重复/嵌套 before_compact):**不覆盖** `previous`(I3),只刷新 `lastActivityAt`。
59
+ - 已有 marker 且 `phase === "failed"`(失败后用户立刻重试 `/compact`,或自动压缩失败后 pi 再次触发):**视为新窗口**——用当前(已恢复的)状态重拍快照,`phase` 置 `"running"`(R1-D2)。
60
+ - 无 marker:快照 `previous = { semanticState, processState, summary, question, error }`,`phase="running"`,`startedAt=now`。
61
+ - 统一置 `semanticState="working"`、`processState="alive"`、`lastActivityAt=now`,`summary = deriveSummary(status)`。
62
+ - 不动 `pendingQuestions`、`latestAssistantPreview`、`latestTool`、evidence。
63
+
64
+ `endCompaction(status, { willRetry, aborted, errorMessage }, now)`:
65
+
66
+ - `previous` 缺失(没等到 begin 落盘 / 迟到 end):清标记,保持 `working`,交给后续 `agent_end` 或 reconcile 收敛(这是唯一无法从快照恢复的分支,见 R1-G2 的缓存白名单消除主要来源)。
67
+ - `willRetry === true`:清标记、保持 `working`(run 会重试)。**有意丢弃 `errorMessage`**:重试会把该错误盖掉,写进行里只会误导(R1-D2,A2 有断言)。
68
+ - 否则 `previous.semanticState ∈ {queued, working}`:清标记、`semanticState="working"`、`processState="alive"`,summary 用 `deriveSummary`。
69
+ - 否则(resting 态):恢复 `semanticState / processState / question / error`,`summary = previous.summary || deriveSummary(status)`(R1-G3:`processState` 必须回到快照值,否则 idle 行会留下假 `alive`,导致 `row.alive`、reconcile 的 `looksActive`、dashboard stop 守卫全部误判)。
70
+ - `aborted === true`:按上面的恢复规则收尾,**不**写 `error`、**不**留 failed 标记(pi 的语义是「Compaction cancelled」)。
71
+ - `!aborted && errorMessage`:先按恢复规则回落,再写 `status.error = errorMessage`,留 `compaction = { phase:"failed", reason, startedAt:now, previous:null, error:errorMessage }`,**并在同一函数内 `status.summary = deriveSummary(status)`**(R1-G1:前台薄壳不经过 `reduceEvent` 的尾巴,失败文案只能由这里产出)。
72
+ - 所有分支清 `lastActivityAt = now`。
73
+
74
+ `finalizeRun`:清 `compaction = null`(终态不可能仍在压缩)。
75
+
76
+ ### D3 — summary 派生与归属(derive.mjs + events.mjs)
77
+
78
+ `deriveSummary` 的首个分支(**先于** currentTool / preview):
79
+
80
+ - `phase === "running"` → `"Compacting context…"`(对齐 pi 自己的状态文案)。
81
+ - `phase === "failed"` → `truncate(error, max)`(pi 的 `errorMessage` 已自带 `Compaction failed: ` 前缀,不再二次拼接)。
82
+
83
+ **summary 归属(R1-D3)**:compaction 窗口的两个 helper 是 summary 的唯一生产者。`reduceEvent` 末尾的 `if (meaningful) status.summary = deriveSummary(status)` 对 `compaction_start` / `compaction_end` 两个事件**跳过**(用局部标记),否则 JSON 通路会重派生、吃掉 `endCompaction` 刚恢复的 `previous.summary`,与前台通路行为分裂。
84
+
85
+ failed 标记的清除:`reduceEvent` 末尾计算 summary 之前(非 compaction 事件且 `meaningful`)清标记再派生;前台通路在 `syncRowEvent` 入口清除非 compaction 事件带来的残留标记。失败提示只活到下一次真实活动为止。
86
+
87
+ ### D4 — 扩展事件通路(src/index.ts + service.mjs)
88
+
89
+ - `src/index.ts` 追加三个注册,全部复用现有 `syncForeground`(hosted 走 `syncHostedEvent`,普通会话走 `syncForegroundEvent`):
90
+ `session_before_compact` / `session_compact` / `session_compact_failed`。
91
+ 处理器**不返回**任何值(不 cancel、不提供自定义 compaction)。
92
+ - `syncRowEvent` 追加三个分支,各 3–6 行薄壳:构造 facts → 调 D2 的纯函数 → `writeForegroundState` → `return true`:
93
+ - `session_before_compact` → `beginCompaction(status, { reason: event.reason }, now)`
94
+ - `session_compact` → `endCompaction(status, { willRetry: event.willRetry }, now)`
95
+ - `session_compact_failed` → `endCompaction(status, { willRetry: event.willRetry, aborted: event.aborted, errorMessage: event.errorMessage ?? null }, now)`
96
+ - 入口清理规则:`if (status.compaction && !COMPACTION_EVENTS.has(event.type)) status.compaction = null;`(COMPACTION_EVENTS = 上述三个),保证 abandon 的 running 标记/残留 failed 标记不会跨真实事件存活。
97
+ - `index.ts` 的注册名由 A6 静态断言覆盖(R1-D4:注册层不进 service harness,拼写错误不能只等用户实测发现)。
98
+
99
+ ### D5 — JSON 通路(events.mjs reduceEvent)
100
+
101
+ - `case "compaction_start"`:`beginCompaction(status, { reason: event.reason }, now)`,`meaningful = true`,标记「本次 summary 已由 helper 产出」。
102
+ - `case "compaction_end"`:`endCompaction(status, { willRetry, aborted, errorMessage }, now)`,`meaningful = true`,同样跳过尾巴重派生(D3)。
103
+
104
+ D2/D3 的语义在两条通路上完全相同;`runner/job-runner.mjs` 不需要改(beat 把整份 status 当作 patch,`applyStatusProjection` 泛型合并后由 `projectViewState` 投影)。
105
+
106
+ ### D6 — 不改动的层(明确排除)
107
+
108
+ - `isAgentBusy` 及其全部调用点:**读的是 semanticState,不是 compaction 字段**;本修复只通过置 `working` 生效。
109
+ - coordinator 决策层与 `PATCHABLE_FIELDS` 白名单:`sync_foreground` 是全量投影(`state-commands.mjs:612-618`),`run_progress` 是泛型 `diffFields` 合并,新字段无需白名单(R1-P1 已逐条核对)。
110
+ - evidence / session view / attach 通路:compaction 不产生 evidence 变化。
111
+
112
+ ## 3. 不变量
113
+
114
+ - **I1(窗口只动展示态)**:compaction 窗口内只允许改 `semanticState / processState / summary / lastActivityAt / compaction`;`pendingQuestions`、`latestAssistantPreview`、`latestTool`、evidence 保持原样。
115
+ - **I2(忙判据不扩语义)**:忙守卫仍只读 `semanticState ∈ {queued, working}`;`compaction` 字段不参与任何守卫判断。
116
+ - **I3(快照一次性)**:同一 running 窗口内重复 `before_compact`/`compaction_start` 不覆盖 `previous`;只有 failed 残留会被视为新窗口。
117
+ - **I4(恢复有条件)**:压缩前是 resting 态(`idle/needs_input/completed/failed/stopped/holding`)→ 恢复该态及其 `processState`;压缩前是 `queued/working` 或 `willRetry` → 保持 `working/alive`,交由后续 `agent_end` 收尾。
118
+ - **I5(失败不伪造终态)**:压缩失败只写 `error` 与失败 summary,不把行改成 `failed` 语义态(会话本身仍可用);`willRetry` 时连 error 也不写。
119
+ - **I6(终态不残留)**:`finalizeRun` 必清 `compaction`;任何真实活动事件都会清掉 failed 标记。
120
+ - **I7(旧行兼容)**:缺失字段按 `null` 解释,不迁移、不改 version。
121
+ - **I8(summary 单一生产者)**:窗口内 summary 只由 D2 helper 产出,两条通路的尾巴都不得重派生。
122
+
123
+ ## 4. 非目标
124
+
125
+ - 不显示压缩进度百分比、token 数、耗时、成本。
126
+ - 不处理 `summarization_retry_*`(pi 的重试提示不进看板),也不把压缩尝试渲染成独立行。
127
+ - 不改 mark-done / hold / follow-up / attach 的任何语义或文案。
128
+ - 不为 JSON runner 增加手动 `/compact` 能力(该通路本就没有斜杠命令)。
129
+ - 不改 dashboard 分组规则、header 计数公式、evidence 聚合。
130
+
131
+ ## 5. 验收矩阵
132
+
133
+ | ID | 功能点 | 验收方式 | 具体验证 | 通过标准 |
134
+ |----|--------|----------|----------|----------|
135
+ | A1 | `deriveSummary` 的 compaction 文案与优先级 | 自动化验证(unit) | `node --test test/derive.test.mjs` | running→`Compacting context…`;failed→`error` 原文(不二次加前缀);无标记时既有断言全过;优先级高于 currentTool/preview |
136
+ | A2 | D2/D3 reducer 语义(两条通路共用的纯函数) | 自动化验证(unit) | `node --test test/events.test.mjs` | `beginCompaction`:快照含 processState、running 幂等、failed 残留→新窗口;`endCompaction`:resting 恢复(含 `processState` 回到快照值)/ mid-run 保持 working+alive / `willRetry` 保持并**丢弃 errorMessage** / `aborted` 恢复且无 error / `errorMessage`→failed 标记 + `error` + summary=error 原文 / previous 缺失→保持 working;`finalizeRun` 清标记;`projectViewState` 透传、缺字段→null;`reduceEvent` 对 `compaction_start/end` 返回 meaningful、summary 不被尾巴重派生覆盖;下一个真实事件清掉 failed 标记 |
137
+ | A3 | 前台/hosted 镜像(`syncRowEvent` 三分支 + 忙守卫 + 在途竞态) | 自动化验证(integration,进程内) | `node --test test/service.test.mjs`(主断言用 coordinator off;另加一条 `startTrackedCoordinator` 的在途用例) | coordinator off:`session_before_compact`→`state.json`: working/alive/summary=`Compacting context…`/`compaction.previous.semanticState===idle`. 期间 `isAgentBusy(row)===true`、`markCompleted`/`holdView` 返回 busy 拒绝;`session_compact`→恢复 idle + 原 summary + `processState==="exited"` + `compaction===null`;failed(aborted)→恢复且无 error;failed(errorMessage)→恢复态 + `error` 原文 + **summary 为 `Compaction failed: …`**;重复 before 不覆盖 previous。tracked coordinator:back-to-back 触发 begin→end(不等待在途写)后轮询到恢复态,**不得**停在 working + Compacting |
138
+ | A4 | JSON runner 通路的端到端镜像 | 自动化验证(integration) | `node --test test/runner.integration.test.mjs`(fake-pi 新增 `compact` 模式,sleep 窗口 ≥1s) | 压缩窗口内用 `waitFor` 轮询到 `state.json` 为 working + `Compacting context…` 且 `status.json.compaction.phase==="running"`;`agent_end` 后行终态为 idle 且 `compaction===null`、`status.json` 同步清空 |
139
+ | A5 | 全量回归 | 自动化验证(build+static+test) | `npm run verify`(typecheck + perf gate + tests + coverage + pack:dry) | 全绿,覆盖率阈值(lines 85 / funcs 80 / branches 70)不下降 |
140
+ | A6 | `src/index.ts` 的三个钩子注册名(注册层不进 service harness) | 自动化验证(static) | 新增 `test/index-compaction-events.test.mjs`:读 `src/index.ts` 文本,断言包含 `pi.on("session_before_compact"`、`pi.on("session_compact"`、`pi.on("session_compact_failed"` 三处 | 三条注册都在;文件被改名/删除时测试失败(fail-closed) |
141
+ | U1 | 真实 `/compact` 观感与守卫 | 用户实测 | **前置**:切到 PR 分支并 `npm install`——**没有构建步骤**(`package.json` 的 `main`/`exports` 直接指向 `./index.ts`,pi 加载 TS 源;不存在 `build` script),或用隔离启动 `--extension <worktree 绝对 index.ts>`;随后重启 pi 或 `/reload` 扩展;JSON 通路需重新 dispatch 一个后台会话才会用新 runner。**时机**:A1–A6 全绿后、合并前。**步骤**:在 board 里对一个有历史的后台会话 attach → 执行 `/compact`(长历史会话最直观)。**观察**:(1) 行进入 Running 且 summary 为 `Compacting context…`;(2) 压缩期间在 dashboard 按 `d` 标记完成被拒(提示等待);(3) 压缩结束行回到压缩前状态(通常 Needs instructions),summary 不再是 Compacting;(4) 阈值压缩:用一个小上下文窗口模型(或超长历史)跑一个后台会话,压缩发生时行不显示为空闲 | 四项均符合;(3) 若压缩失败(用 `Nothing to compact` 的短历史触发),行显示 `Compaction failed: ...` 且不残留 Compacting |
142
+
143
+ ## 6. 可测性拆分设计(实现硬约束)
144
+
145
+ - **纯函数层(events.mjs)**:`beginCompaction` / `endCompaction` 不读盘、不取时钟(`now` 注入);`endCompaction` 的六种分支全部可由 A2 单测覆盖,`endCompaction` 是 summary 的唯一生产者(I8)。
146
+ - **派生层(derive.mjs)**:`deriveSummary` 的 compaction 分支是纯字符串决策,A1 直接断言;不引入新的 I/O。
147
+ - **薄壳层(service.mjs)**:`syncRowEvent` 三个分支只做「取字段 → 调纯函数 → 写盘」,不做判断;语义断言放在 A2,落盘断言放在 A3。**禁止**把 restore 规则复制进 service(否则两条通路会漂移)。
148
+ - **缓存层(foreground-projection-cache.mjs)**:只加白名单项,不改机制;A3 的在途用例是这个白名单的回归保护。
149
+ - **驱动层(test-support/fake-pi.mjs)**:新增 `compact` 模式(`agent_start` → `compaction_start` → sleep ≥1000ms → `compaction_end` → `message_end` → `agent_end`),只负责发事件。**sleep 下限的依据**:`runner/job-runner.mjs:37` 的 `WRITE_THROTTLE_MS = 250`,beat 节流后还要经协调器物化,窗口 <1s 会间歇性错过 running 相位(R1-D5);A4 用 `waitFor` 轮询,不做一次性读取。
150
+ - **静态层(A6)**:只读文本断言注册名,不 import TS(`src/index.ts` 不是可运行 ESM)。
151
+ - **测试边界**:A1 只碰 derive;A2 只碰 events;A3 只碰 service + 写盘(off / tracked coordinator 两种);A4 只碰 runner 子进程与 JSON 流;A6 只碰源文本。任何一层都不需要真实模型调用或真实 `/compact`(那是 U1 的职责)。
152
+
153
+ ## 7. 风险与回滚
154
+
155
+ - **风险 1(字段进入投影)**:`compaction` 进入 `state.json` 后,若有外部消费者做严格 schema 校验会报未知字段。仓内证据:`readState` 只做 JSON 解析、dashboard 按已知字段读取,无严格 schema;`patch_fields` 白名单只约束命令来源,不约束投影字段。
156
+ - **风险 2(typecheck 覆盖面,R1-D6)**:`tsconfig.json:9` 是 `checkJs: false`,本改动主体是 `.mjs`,`npm run verify` 的 typecheck 几乎不覆盖其函数体——**真正的把关者是 A1–A4 的测试与覆盖率阈值**,不要把 A5 当成类型级保护。
157
+ - **风险 3(summary 覆盖策略)**:写错会让行短暂显示错误文案。A1/A2 钉住 running/failed 两相位、恢复取值与清除时机;U1 覆盖真实观感。
158
+ - **回滚**:按层列文件——运行时 `src/index.ts`、`src/runtime/service.mjs`、`src/core/events.mjs`、`src/core/derive.mjs`、`src/core/types.mjs`(三个 typedef)、`src/core/foreground-projection-cache.mjs`(白名单);测试 `test/derive.test.mjs`、`test/events.test.mjs`、`test/service.test.mjs`、`test/runner.integration.test.mjs`、新增 `test/index-compaction-events.test.mjs`;夹具 `test-support/fake-pi.mjs`。revert 该 PR 即可;`state.json` 中的 `compaction` 字段对旧代码是未知字段(被忽略),无迁移负担。
@@ -0,0 +1,209 @@
1
+ # Spec — Issue #95 切片:临时隔离 `internal marker failure … (legacy main)` 并补 F3 现场
2
+
3
+ > 状态:**DRAFT 待用户确认**(`stepwise` 档,步 4 收敛后 ⏸)
4
+ > 认领:zhuxixi(2026-11-22)
5
+ > 调研:同目录 `research/01-red-state-and-scope.md`、`research/02-mechanism-evidence.md`(2026-09-25)、
6
+ > `research/03-legacy-quarantine-slice.md`(2026-11-22,本切片专属)
7
+ > 上游:issue #95(umbrella,本切片**不关闭**它)
8
+ > 基线 HEAD:`c1e39cd`(2026-11-23 main;目标相关文件与 `df083a5` 无差异;已完成的 run `43781526496` Node 22 Coverage 红在本切片目标用例)
9
+ > Drafted: kimi-coding/k3.1-pro (selected) · kimi-coding/k3.1-pro (physical) @ 2026-11-22T21:57:44+08:00
10
+ > Revised: kimi-coding/k3.1-pro (selected) · kimi-coding/k3.1-pro (physical) @ 2026-11-22T22:14:46+08:00(R1:接受 R1-G1 + R1-D1…D5 全部六条)
11
+
12
+ > Revised: kimi-coding/k3.1-pro (selected) · kimi-coding/k3.1-pro (physical) @ 2026-11-22T22:30:23+08:00(R2:收敛,接受 R2-D1…D3 三条细化;A7 预算改为有实测依据的 240s)
13
+
14
+ ## 1. 问题陈述
15
+
16
+ `test/attach-frame-witness.integration.test.mjs` 的
17
+ `internal marker failure revokes capability via onUnavailable (legacy main)` 是 main CI 的反复红源:
18
+ 11-16 run `43781526496` 上它烧满 15s 等待(`duration_ms 15263.9`),失败信息只有一句 `timed out waiting`;
19
+ 同一 run 的 owned 半边与 Node 24 双 job 全绿。issue 台账记录该签名近 6 次 main CI 有 5 次命中。
20
+
21
+ 这条红属于 #95 已定论的 B 类:**事件产生了,但没在墙钟窗口内到达**(并行争抢 + 默认文件级并发)。
22
+ 它**不是** A 类(事件根本没产生的生产缺陷)——没有证据指向 legacy runner 的降级链路本身坏了。
23
+
24
+ **本切片的定位是临时隔离,不是修复。** #95 的结构性病因(无串行化、墙钟预算)原样保留,
25
+ 恢复条件写死为「process 相位串行化落地后 revert」。
26
+
27
+ ## 2. 设计决策
28
+
29
+ ### D1:只跳 legacy 半边,owned 半边原样保留并继续跑
30
+
31
+ `for (const legacy of [false, true])` 生成的两个用例共享**同一段参数化 test 体**,因此隔离只能写成条件式:
32
+
33
+ ```js
34
+ { skip: legacy ? "issue #95: temporary quarantine … restore after process-phase serialization" : undefined }
35
+ ```
36
+
37
+ `legacy === false`(owned)时 `skip` 为 `undefined`,用例照常执行;`legacy === true` 时跳过并输出理由。
38
+ skip 理由是字符串而非 `true`,会原样出现在 TAP 输出里,让每次跳过都自证原因。
39
+
40
+ skip 文案须包含三要素:`issue #95`、临时性质(temporary quarantine)、恢复条件(restore after process-phase serialization)。
41
+
42
+ - **为什么不整条删**:它保护的跨进程降级链路(marker 写失败 → `onUnavailable` → `socket.destroy`
43
+ → runner 清理 → 广播 `enabled:false` → host.json 改写)只有本用例在测;
44
+ `attach-frame-reporter.test.mjs:557` 的单测只覆盖第一步。整条删 = 撤掉兜底保护。
45
+ - **为什么跳 legacy 而不是 owned**:owned 是现代默认路径(`attachMode==="protocol" && frameWitnessAvailable`),
46
+ 保护价值更高,且当前不是红源。
47
+ - **为什么不用 `--test-name-pattern` 隔离**:#178 D6 已实证 Node 24 下负向前瞻排除不掉测试(root 聚合条目名称为空、恒匹配)。
48
+
49
+ ### D2:保留的 owned 半边必须补 F3 现场快照
50
+
51
+ 本文件是 F3 纪律的漏网文件(同族 6 个文件已接 `test-support/flake-postmortem.mjs`,本文件零命中)。
52
+ 给 owned 半边的两处等待接上 capture thunk,使超时错误自带 **host / state / diagnosticsTail / journal / screenLog** 现场。
53
+ **`status` 恒为 `<absent>`,不是「可能缺失」**:`capturePostmortem` 只在 `runId` 非空时才读 status
54
+ (`test-support/flake-postmortem.mjs:87`),而本文件 fixture 直接 spawn `pty-runner`、不经协调器建 run,全文件无 `runId`。
55
+ 本切片不改这个行为——为拿到 status 而引入协调器属于范围扩张。
56
+
57
+ 实现走**本文件本地 `waitFor` 的可选参数**,与同族 `runner.integration.test.mjs:71` 的既有写法一致:
58
+
59
+ ```js
60
+ async function waitFor(predicate, timeoutMs = 15000, capture = null) {
61
+ return waitForWithPostmortem(predicate, { timeoutMs, intervalMs: 25, capture });
62
+ }
63
+ ```
64
+
65
+ - **默认值与轮询间隔不变**(15000 / 25ms),未传 capture 的既有 74 处 `await waitFor(` 调用行为字节级不变。
66
+ - 只在 owned 半边传 capture;legacy 半边已 skip,不接。
67
+ - **不做全文件迁移**:其余 18 个用例的等待不在本切片范围,避免 diff 膨胀。
68
+
69
+ ### D3:守卫必须钉「条件式 skip」这一具体形态,并给 TAP 面一个真归属
70
+
71
+ **前置事实(决定守卫怎么写)**:两个「半边」不是两段独立源码,而是**同一段参数化 test 体**——
72
+ `for (const legacy of [false, true])` + 模板串名(`test/attach-frame-witness.integration.test.mjs:1320-1321`)。
73
+ 所以「owned 段无 skip / legacy 段有 skip」在源码文本上互斥,**唯一可实现且防得住扩大化的守卫是钉住条件式形态**:
74
+
75
+ ```js
76
+ { skip: legacy ? "issue #95: temporary quarantine …" : undefined }
77
+ ```
78
+
79
+ 沿用 `test/wait-capture-wiring.test.mjs`(#178 A3/A7)的源码级手法,新增守卫断言五件事:
80
+
81
+ 1. 用例体匹配 `skip:\s*legacy\s*\?\s*"issue #95[^"]*"`,且 false 分支为 `undefined`
82
+ ——拍平成 `skip: "…"`(全跳)或删掉 skip(隔离失效)都会红;
83
+ 2. skip 文案含「temporary quarantine」与恢复条件关键词;
84
+ 3. 两条核心断言仍在,**以磁盘源文本为准**(`:1327` 与 `:1330`,含 `msg.` 前缀与可选链):
85
+ 广播断言 `msg.status?.frameWitness?.enabled === false`、host 断言 `readHost(root, viewId)?.frameWitness?.enabled === false`
86
+ ——删断言不是转绿的方式;守卫按真实文本写正则,不得按转述写;
87
+ 4. 用例体内每个 `await waitFor(` 都以裸 `capture` 收尾(复用既有正则 `/,\s*capture\s*$/`);
88
+ 5. 本地 `waitFor` 委托层的默认值文本仍为 `timeoutMs = 15000` 与 `intervalMs: 25`。
89
+
90
+ **TAP 面(「owned 半边真的在跑」)必须有自动归属,不能靠人眼扫**:新增一条用例 spawn
91
+ `node --test test/attach-frame-witness.integration.test.mjs`(预算见下),
92
+ 解析其 TAP 输出,断言:legacy 条目带 `# SKIP` 且理由含 `issue #95`;owned 条目为 `ok` 且**不含** `SKIP`。
93
+ 这条是本切片唯一会 spawn 进程的守卫,与纯文本守卫分文件存放(见 §3.2)。
94
+
95
+ **预算依据(实测,非拍脑袋)**:本机(8 核、Node v24.22.1)跑目标文件全量 29 条用例,墙钟 **45.08s**、
96
+ `duration_ms 45058`,29 pass / 0 fail / 0 skipped;目标用例单条约 0.51s(基线 TAP 留档见下)。
97
+ 取 `{ timeout: 240_000 }`(4 分钟 ≈ 5.3× 本机实测)以容纳 CI 慢机与 c8 插桩开销,
98
+ 并按 `scripts/budget-audit.mjs` 规则 (b) 附 `// budget: spawns the whole 29-case file; 4 min = 5.3x the 45.08s local measurement` 豁免。
99
+ 若实测 CI 上仍不够,改预算属于后续 PR 的事,不在本切片里反复加宽。
100
+
101
+ ### D4:范围与硬边界
102
+
103
+ - **只动测试**:不改 `runner/`、不改 `src/`、**`ci.yml` 零改动**、不加依赖、不改任何测试用例名称。
104
+ - **不改预算数值**:15s / 5s 等待与文件内既有的 `{ timeout: 20000 }`(`spawnSync` 选项,非 test 超时)原样保留。
105
+ 本切片不是预算调整,预算纪律由 #95 F2 的 `budget-audit` 门禁继续把守。
106
+ - **不动 #95 的其它签名**:`subscribe_terminal`、`A5 burst`、`attach-entry-22` B/Q、reporter 系列等一律不碰。
107
+ - **不关闭 #95**:PR 描述用 `Refs #95`,不用 `Fixes`。
108
+
109
+ ## 3. 组件契约
110
+
111
+ ### 3.1 `test/attach-frame-witness.integration.test.mjs`(改)
112
+
113
+ | 点 | 改动 |
114
+ |---|---|
115
+ | import | 新增 `capturePostmortem, formatPostmortem, waitForWithPostmortem`(来自 `../test-support/flake-postmortem.mjs`) |
116
+ | `waitFor` | 签名加第三个可选参数 `capture = null`,函数体改为委托 `waitForWithPostmortem`,默认 `timeoutMs = 15000`、`intervalMs = 25` 不变 |
117
+ | legacy 半边 | test 选项加 `{ skip: legacy ? "issue #95: temporary quarantine …" : undefined }`(条件式,见 D1) |
118
+ | owned 半边 | 用例体内先建命名绑定 `const capture = () => formatPostmortem(capturePostmortem(root, viewId, null));`,两处 `await waitFor(...)` 以裸 `capture` 收尾 |
119
+
120
+ capture 使用 `runId = null`:本文件的 fixture 直接 spawn `pty-runner`、不经协调器创建 run,因此现场里 `status` 恒为 `<absent>`
121
+ (见 D2);`capturePostmortem` 对其余缺失文件一律安全返回 `null`(`safe()` 包裹),不会二次抛错。
122
+
123
+ **形态必须与兄弟文件先例一致**:`control-reconcile.integration.test.mjs:343` 与 `pty-runner.integration.test.mjs:521`
124
+ 都是「命名 `capture` 绑定 + 裸标识符传参」,既有守卫正则 `/,\s*capture\s*$/`
125
+ (`test/wait-capture-wiring.test.mjs:120,127`)也只认这个形态。写内联 thunk 会让复用的守卫必红。
126
+
127
+ ### 3.2 `test-support/test-source-scan.mjs`(新,纯函数模块)
128
+
129
+ **为什么放 `test-support/` 而不是从守卫文件 import**:`test/wait-capture-wiring.test.mjs` 在 import 时即注册 5 个顶层 `test()`,
130
+ 跨测试文件 import 会在新守卫文件的 `node --test` 进程里把这 5 个用例**再跑一遍**(`node --test` 对每个测试文件各起一个子进程;`scripts/run-tests.mjs:331` 把全部文件交给单个 `node --test` 调用),
131
+ 红时失败归因翻倍。故把纯函数移到 `test-support/`,两个守卫文件都从那里 import——符合仓约定的「纯函数 + 薄 I/O 壳」分层。
132
+
133
+ 迁移内容(实现阶段不改其逻辑,只搬家):
134
+
135
+ - `testCaseBody(source, marker)` —— 从 `test/wait-capture-wiring.test.mjs:40` 迁入并 re-export,原文件改为 import;
136
+ - `waitForCalls(body)` —— 从 `:55` 迁入,同上;
137
+ - `indentedTestCaseBody(source, marker)` —— **新增**,处理本切片目标用例位于 `for` 循环内、**缩进一级**的情形:
138
+ 起始为行首 `\ttest(`,收尾为该缩进层级的 `\n\t});`;marker 不存在或找不到收尾都显式抛错(防空跑成绿)。
139
+
140
+ ### 3.3 `test/frame-witness-quarantine.test.mjs`(新,unit/static)
141
+
142
+ 纯文本守卫(D3 第 1/2/3/4/5 条)+ `indentedTestCaseBody` 的真值表单测。不 spawn 进程。
143
+
144
+ ### 3.4 `test/frame-witness-quarantine-tap.test.mjs`(新,integration)
145
+
146
+ 唯一 spawn 子进程的守卫:跑目标文件、解析 TAP、断言 legacy SKIP / owned 非 SKIP(D3 的 TAP 面)。
147
+ 与纯文本守卫分文件,避免「只想跑静态检查」时被迫跑一遍真实 runner 套件。
148
+
149
+ ### 3.5 非组件
150
+
151
+ 不新增 `src/` 模块、不改 `scripts/run-tests.mjs`、不动 `package.json` 脚本。
152
+ `test/wait-capture-wiring.test.mjs` 只改 import 来源,其 5 个用例的断言与行为不变。
153
+
154
+ ## 4. 可测性拆分设计(实现阶段硬约束)
155
+
156
+ | 单元 | 性质 | 怎么测 | 验收 |
157
+ |---|---|---|---|
158
+ | `indentedTestCaseBody(source, marker)` | **纯函数**(字符串处理,无 I/O) | 直接传字符串样本,断言:正确截取、不吞掉后续用例、marker 缺失抛错、收尾缺失抛错 | A1 |
159
+ | `waitFor` 委托层 | 薄壳,决策全在已测的 `waitForWithPostmortem` | 不新增单测;由 `test/flake-postmortem.test.mjs` 既有 11 个顶层用例覆盖核心逻辑;默认值由静态守卫钉住(A2) | A2 |
160
+ | capture thunk | 纯 I/O 读取 + 已测的 `formatPostmortem` | 由 `test/flake-postmortem.test.mjs` 覆盖渲染;本切片只保证接线形态 | A3 |
161
+ | 条件式 skip 接线 | 声明式 test 选项 | 静态守卫钉形态(A4)+ TAP 实证(A7) | A4 / A7 |
162
+ | 纯函数迁移 | 搬家不改逻辑 | `test/wait-capture-wiring.test.mjs` 既有 5 个用例原样通过即回归钉 | A6 |
163
+
164
+ 边界声明:**解析(纯)↔ 等待(已测共享件)↔ 断言(测试)三层不得合并**;
165
+ 不允许把 `indentedTestCaseBody` 内联进断言里,也不允许在目标文件里重写一份 postmortem 渲染。
166
+
167
+ ## 5. 验收矩阵
168
+
169
+ ### 自动化验证
170
+
171
+ | ID | 功能点 | 验收方式 | 具体验证 | 通过标准 |
172
+ |----|--------|----------|----------|----------|
173
+ | A1 | 缩进用例段解析 + 纯函数迁移 | 自动化(unit,`test/frame-witness-quarantine.test.mjs`) | `node --test test/frame-witness-quarantine.test.mjs` | 真值表通过;marker 缺失 / 收尾缺失均抛错(防空跑成绿);不吞后续用例 |
174
+ | A2 | 默认等待行为不变 | 自动化(static,同上) | 静态守卫断言委托层文本含 `timeoutMs = 15000` 与 `intervalMs: 25`;`node --test test/attach-frame-witness.integration.test.mjs` 退出码 0 | 默认值被改即红;`git diff` 复核为评审注记,不计入自动化 |
175
+ | A3 | owned 半边现场接线 | 自动化(static,同上) | 静态守卫断言用例体内每个 `await waitFor(` 以裸 `capture` 收尾(复用 `/,\s*capture\s*$/`) | 全部命中;漏接即红 |
176
+ | A4 | skip 是条件式且只落 legacy | 自动化(static,同上) | 断言用例体匹配 `skip:\s*legacy\s*\?\s*"issue #95[^"]*"` 且 false 分支为 `undefined` | 拍平成全跳、或删 skip,均红 |
177
+ | A5 | 降级断言未被削弱 | 自动化(static,同上) | 静态守卫按磁盘真实文本断言两条核心断言仍在用例体(`msg.status?.frameWitness?.enabled === false`、`readHost(root, viewId)?.frameWitness?.enabled === false`) | 均在;任一被删即红 |
178
+ | A6 | 既有套件与迁移回归 | 自动化(unit + integration + build) | `node --test test/wait-capture-wiring.test.mjs`;`npm run verify` | 既有 5 个守卫用例原样通过;verify exit 0(typecheck + perf gate + 全量测试 + 覆盖率阈值 85/80/70 + pack:dry) |
179
+ | A7 | TAP 面:owned 真的在跑、legacy 真的被跳 | 自动化(integration,`test/frame-witness-quarantine-tap.test.mjs`) | `node --test test/frame-witness-quarantine-tap.test.mjs`(该守卫内部 spawn `node --test test/attach-frame-witness.integration.test.mjs` 并解析 TAP) | legacy 条目带 `# SKIP` 且理由含 `issue #95`;owned 条目为 `ok` 且不含 `SKIP`。两版 Node 由 CI 矩阵各跑一次(A8) |
180
+ | A8 | CI 双矩阵 | 自动化(CI) | PR 的 `Test (Node 22)` / `Test (Node 24)` | 两个必需检查均绿;A7 的 TAP 守卫在两版 Node 上都执行并通过 |
181
+
182
+ ### 用户实测 / 合并后观测
183
+
184
+ | ID | 功能点 | 验收方式 | 步骤与观察 | 通过标准 |
185
+ |----|--------|----------|------------|----------|
186
+ | U1 | 该签名不再阻塞 main | 用户实测(合并后 CI 观测) | `gh run list --branch main --limit 8`,逐 run 记录失败点 | 合并后 ≥5 次 main run 中,`internal marker failure … (legacy main)` 零命中(它已 SKIP);若 owned 半边转红,失败信息**必须**自带 postmortem 现场 |
187
+ | U2 | 隔离可回退 | 用户实测(#95 串行化落地时) | revert 本切片 commit 后跑 `node --test test/attach-frame-witness.integration.test.mjs` | 静态守卫同步更新后,legacy 半边恢复执行;本条是恢复动作的 checklist,不在本 PR 执行 |
188
+
189
+ **诚实声明**:A6/A7 全绿不能证明 #95 痊愈。本切片只移除一条签名,
190
+ 同文件其余等待与其它 32 个真实进程文件的 flake 风险原样存在,U1 只对该签名负责。
191
+
192
+ ## 6. 非目标
193
+
194
+ - 不实现 #95 的 process 相位串行化(F5/D1),不新增 CI job,不改分支保护。
195
+ - 不做 `attach-entry-22` B/Q 的 c8 相位剔除(属另一条线)。
196
+ - 不迁移本文件其它 18 个用例的 postmortem 接线。
197
+ - 不清理 `/tmp/agentview-*` 夹具残留(#95 F6 / #211)。
198
+ - 不改 `enforce_admins`、不使用 `--admin` 绕过必需检查。
199
+
200
+ ## 7. 交付物清单
201
+
202
+ 1. `test/attach-frame-witness.integration.test.mjs`:skip + F3 接线(预计 ~20 行)
203
+ 2. `test-support/test-source-scan.mjs`:新纯函数模块(迁移 `testCaseBody` / `waitForCalls` + 新增 `indentedTestCaseBody`)
204
+ 3. `test/wait-capture-wiring.test.mjs`:改为从 `test-support/test-source-scan.mjs` import,断言不变
205
+ 4. `test/frame-witness-quarantine.test.mjs`:新纯文本守卫 + 解析真值表(预计 ~110 行)
206
+ 5. `test/frame-witness-quarantine-tap.test.mjs`:新 TAP 实证守卫(预计 ~60 行)
207
+ 6. spec 文档落 worktree `docs/superpowers/specs/2026-11-22-issue-95-legacy-quarantine-design.md`(首个 commit)
208
+ 7. plan 落 worktree `docs/superpowers/plans/2026-11-23-issue-95-legacy-quarantine.md`
209
+ 8. issue #95 评论:切片交付对账 + 「未结案」声明 + 恢复条件
@@ -0,0 +1,201 @@
1
+ # Spec: 降权操作保持看板光标原位(issue #254)
2
+
3
+ Drafted: deepseek/deepseek-flash (selected) · deepseek/deepseek-flash (physical) @ 2026-11-23T22:52:00+08:00
4
+ Revised: deepseek/deepseek-flash (selected) · deepseek/deepseek-flash (physical) @ 2026-11-23T23:20:00+08:00(第 1 轮复核处置:R1-D1/D2/D3/D4/D5/D6 全部接受)
5
+
6
+ > 依据:issue #254 正文 + 两轮调研(`research/round1-root-cause.md`、`research/round2-precedent-testability.md`)
7
+ > + JFox permanent `202610290000456264`(ID 选中→光标下沉)、`202610290000473827`(邻位助手)、`202609302208475929`(探针模式三坑)
8
+ > **行号基准:`b003dd1`(0.13.1)**
9
+
10
+ ## 1. 目标与非目标
11
+
12
+ ### 1.1 目标
13
+
14
+ 1. 用户用 `h`(hold / unhold)或 `d`+`y`(done)主动降权一行后,光标留在原视觉位置——由原位置补位的那一行被选中,而不是跟着降到 `holding`/`completed` 分组。
15
+ 2. 批量 done(SELECT 模式 `d`+`y`)同样保持位置:选中「被移走的区块下方那一行」,区块在末尾时取上方一行;不再跳到 `completedIds[0]`。
16
+ 3. 后台驱动的状态迁移(如会话开始运行时 `idle` → `working`,行移到更靠前的分组)保持既有跟随行为,不受影响。
17
+ 4. 降权失败(协调器拒绝、busy、超时)或以 0 条完成结束时,光标完全不动。
18
+
19
+ ### 1.2 非目标
20
+
21
+ - 不改面板内 `x`→`y` 删除目前的「删除后落到 `orderedIds[0]`」行为(与 attach 内删除的邻位语义不一致,属既有分歧,另议)。
22
+ - 不改 `service.holdView` / `clearHoldView` / `markCompleted` / `markCompletedMany` 的语义与落库。
23
+ - 不改 `nextVisibleIdAfterRemoval`(issue #147)的签名与行为。
24
+ - 不清理批量 done 后多选集合里残留的 completed ID。
25
+ - 不改分组顺序、折叠、过滤、排序规则。
26
+ - 不做键位可配置化。
27
+
28
+ ## 2. 现状审计(实现时按此定位)
29
+
30
+ | 事实 | 位置 |
31
+ | --- | --- |
32
+ | 选中状态按 ID 持有 | `src/ui/dashboard.ts:190` |
33
+ | `refresh()` 只在选中 ID 从 `orderedIds` 消失时重锚到 `orderedIds[0]`,仍可见则原样保留 | `src/ui/dashboard.ts:211-246`(重锚分支 222-236) |
34
+ | `toggleHold()`(`h`,hold / unhold 同一入口) | `src/ui/dashboard.ts:1076-1087` |
35
+ | `confirmDone()` 的 `onYes`(`d`+`y`) | `src/ui/dashboard.ts:1056-1072` |
36
+ | `confirmDoneSelection()` 的 `onYes`,含主动跟随 `selectedId = res.completedIds[0]` | `src/ui/dashboard.ts:1089-1111`(主动跟随 1102) |
37
+ | 确认键处理:`mode` 先复位到 `returnMode` 再调 `onYes` | `src/ui/dashboard.ts:592-610` |
38
+ | hold 落库 `semanticState="holding"` / unhold 落库 `"idle"` | `src/runtime/service.mjs:780-825` |
39
+ | 分组顺序:`holding`、`completed` 在底部 | `src/core/types.mjs:54-63`;`src/core/rows.mjs:233-258` |
40
+ | `d`/`h` 在 peek 与 session 模式也可达;`reply` 是文本模式,不可达 | `src/ui/dashboard.ts:520-521`、`:558-559`、`:398-399` |
41
+ | 既有邻位助手(按 rows 重算顺序) | `src/core/rows.mjs:270-277` |
42
+ | 轮询每 `POLL_MS` 调 `refresh()` + 渲染 | `src/commands/agent-board.ts:113-117` |
43
+ | 探针模板 | `test-support/dashboard-delete-key-probe.ts`;`test/dashboard-delete-key.test.mjs` |
44
+
45
+ ## 3. 设计
46
+
47
+ ### 3.1 决策表
48
+
49
+ | 决策点 | 选择 | 理由 | 被拒备选 |
50
+ | --- | --- | --- | --- |
51
+ | 邻位计算落点 | 新增**纯函数** `neighborAfterRemoval(orderedIds, removedIds, anchorId)` 放 `src/core/rows.mjs` | 输入只需看板顺序 ID 数组,不依赖 UI 状态,可在 `test/rows.test.mjs` 用 unit 覆盖全部边界 | 复用 `nextVisibleIdAfterRemoval`(它吃 `rows` 并自己调 `groupRowsByFolder`,不知道 filter/折叠,且无法表达「整块移除」) |
52
+ | 锚点计算时点 | mutation **之前**用当时的 `orderedIds` 算 | 状态命令是异步的,落地后顺序已变,只剩「新位置」没有「原位置」 | mutation 后按 `res.completedIds` 反推(拿不到原视觉位置) |
53
+ | 锚点应用时点 | 成功分支里、`refresh()` **之后**同步应用 | `refresh()` 会先把选中留在被降权行(或重锚到顶部),必须在它之后覆盖 | 在 `refresh()` 之前设置(会被 `refresh()` 的可见性判定覆盖) |
54
+ | 失败时 | 不应用锚点 | 行没动,应用锚点等于把光标从仍在原位的行上挪走 | 无条件应用(会产生「操作失败但光标跳走」) |
55
+ | 邻居失效兜底 | 邻居不在 `orderedIds` 时按**原下标 clamp** 取行 | 直接赋不可见 ID 会被 `refresh()` 判为「选中行消失」而回落到顶部第 1 行 | 赋 null / 交给 `refresh()` 兜底 |
56
+ | unhold(holding 行按 `h`) | 同样保持位置 | 「某行移走后下一行补位」与移动方向无关,同一套逻辑零成本;「h 不移动光标」心智一致 | 跟随回 idle 组(需要第二套逻辑,且与 hold 不对称) |
57
+ | 模式处理 | 所有模式统一应用锚点,不加模式判断 | 一条规则覆盖全部可达入口;`peekId` 独立于 `selectedId`,peek 面板仍停在被降权的会话上,不受影响,退出 peek 后光标已在原位置。`d`/`h` 实际可达的模式是 `list`、`select`(仅 `d`)、`peek`、`session`;`reply` 是文本输入模式,`h`/`d` 只是字面字符,不可能触发降权,无需特判 | 按模式特判(`d`+`y` 会先把 mode 复位成 `list` 再执行、`h` 则留在 `peek`,特判会让两条命令在 peek 下行为不一致) |
58
+ | 批量 done 的锚点 | 取「`doneIds` 在看板顺序中最靠前的那一行」,其后的非 `doneIds` 行优先 | `selectedBatchRows()` 遍历 Set 插入序,与看板顺序无关,不能取 `[0]` | 取 `selectedIds` 首个(随选择顺序抖动) |
59
+ | 批量 done 的补位 | 扫描时跳过全部 `doneIds` | 连续区块整体下移时,区块内下一行同样会沉底,不能当选 | 只排除锚点自身(连续区块会选到另一个沉底行) |
60
+
61
+ ### 3.2 纯函数契约(`src/core/rows.mjs`)
62
+
63
+ ```js
64
+ /**
65
+ * Issue #254: the row the board should select after `removedIds` leave their
66
+ * current positions. Mirrors issue #147 delete semantics — the next visible row
67
+ * after the anchor's slot, else the previous one, else null — but works on the
68
+ * dashboard's already-filtered, already-folded `orderedIds`.
69
+ *
70
+ * @param {readonly string[]} orderedIds visible row ids in board order
71
+ * @param {Iterable<string>} removedIds ids that will leave their positions
72
+ * @param {string} anchorId the id whose slot must be kept
73
+ * @returns {{ neighborId: string | null, anchorIndex: number } | null}
74
+ * null when anchorId is not visible
75
+ */
76
+ export function neighborAfterRemoval(orderedIds, removedIds, anchorId)
77
+ ```
78
+
79
+ 语义细则:
80
+
81
+ 1. `anchorId` 不在 `orderedIds` → 返回 `null`(调用方不动光标)。
82
+ 2. `anchorIndex = orderedIds.indexOf(anchorId)`。
83
+ 3. 向后扫描 `anchorIndex+1 .. len-1`,返回第一个不在 `removedIds` 里的 ID;没有则向前扫描 `anchorIndex-1 .. 0`,返回第一个不在 `removedIds` 里的 ID。
84
+ 4. 两向都没有 → `{ neighborId: null, anchorIndex }`。
85
+ 5. 纯函数:不读时钟、不碰文件、不改入参。
86
+
87
+ ### 3.3 dashboard 薄层(`src/ui/dashboard.ts`)
88
+
89
+ 两个私有方法,只做状态读写,不含排序逻辑:
90
+
91
+ ```ts
92
+ private capturePositionAnchor(id: string, removedIds: string[] = [id]) {
93
+ return neighborAfterRemoval(this.orderedIds, removedIds, id);
94
+ }
95
+
96
+ private applyPositionAnchor(anchor: { neighborId: string | null; anchorIndex: number } | null): void {
97
+ if (!anchor) return;
98
+ if (anchor.neighborId && this.orderedIds.includes(anchor.neighborId)) {
99
+ this.selectedId = anchor.neighborId;
100
+ this.selectedSection = null;
101
+ this.prewarmSelected();
102
+ return;
103
+ }
104
+ const last = this.orderedIds.length - 1;
105
+ if (last < 0) return;
106
+ const fallback = this.orderedIds[Math.min(anchor.anchorIndex, last)] ?? null;
107
+ if (!fallback) return;
108
+ this.selectedId = fallback;
109
+ this.selectedSection = null;
110
+ this.prewarmSelected();
111
+ }
112
+ ```
113
+
114
+ - `selectedSection` 必须清空:降权行可能迁入折叠分组,`refresh()` 此时已把焦点转成分区标题(`selectedId=null`、`selectedSection=state`),再设置 `selectedId` 而不清 `selectedSection` 会形成「两者同时有值」的非法态。
115
+ - `prewarmSelected()` 补齐预热:`refresh()` 只在 `selectedId` 变化时排程,锚点是在它之后改的。
116
+
117
+ ### 3.4 三个调用点
118
+
119
+ | 入口 | 捕获 | 应用 |
120
+ | --- | --- | --- |
121
+ | `toggleHold()` | 调 `service.holdView` / `clearHoldView` **之前**:`const anchor = this.capturePositionAnchor(row.meta.id)` | `.then` 里 `res.ok` 分支、`this.refresh()` **之后**:`this.applyPositionAnchor(anchor)` |
122
+ | `confirmDone()` | `onYes` 内、`this.markCompleted(row)` **之前** | `.then` 里 `res.ok` 分支、`this.refresh()` **之后** |
123
+ | `confirmDoneSelection()` | `onYes` 内、`markCompletedMany` **之前**;锚点 ID 取 `doneIds` 中 `orderedIds` 下标最小者,`removedIds = doneIds` | `.then` 里 `res.ok && res.completed > 0` 时、`this.refresh()` **之后**;删除 `selectedId = res.completedIds[0]`。`completed === 0`(全部被跳过)时不应用锚点,光标不动 |
124
+
125
+ 时序(以 `d`+`y` 为例):
126
+
127
+ ```text
128
+ key y → mode 复位 → onYes
129
+ anchor = capturePositionAnchor(row.id) # 用 mutation 前的 orderedIds
130
+ await markCompleted(row) # 协调器写状态(可能耗时)
131
+ if ok:
132
+ refresh() # 重排,选中暂时仍在被降权行
133
+ applyPositionAnchor(anchor) # 覆盖为补位行(或 clamp 兜底)
134
+ else:
135
+ refresh() # 忽略锚点;光标留在原行
136
+ ```
137
+
138
+ `refresh()` 与 `applyPositionAnchor()` 在同一个 `.then` 回调里同步执行,两者之间不可能插入渲染或轮询,因此不会出现「先画在旧位置再跳」的闪烁帧;无需额外 `requestRender`(轮询 `POLL_MS` 与输入驱动渲染已覆盖)。
139
+
140
+ ## 4. 边界与降级
141
+
142
+ | 场景 | 行为 |
143
+ | --- | --- |
144
+ | 被降权行是列表中唯一的一行 | `neighborId=null`;clamp 后仍取到该行(行仍在列表里),保持选中 |
145
+ | 被降权行是末行 | 向前扫描取上一行 |
146
+ | 批量 done 覆盖末尾连续区块 | 向后无可用行,向前取区块上一行 |
147
+ | 批量 done 覆盖全部行 | 行迁入 `DONE` 分组后仍然可见(`completed` 在 `GROUP_ORDER` 内),`neighborId=null`,clamp 到原下标处的已完成行;只有 `DONE` 分区被折叠时 `orderedIds` 才可能为空,此时 `applyPositionAnchor` 空操作 |
148
+ | 批量 done 全部被服务跳过(`completed=0`) | 不应用锚点(`res.completed > 0` 才应用),光标留在原行 |
149
+ | 过滤生效时降权 | 锚点按过滤后的 `orderedIds` 算;若邻居也被过滤掉,clamp 到原下标,下标越界取末行 |
150
+ | 邻居在 mutation 落地前被轮询移走 | `orderedIds.includes(neighborId)` 为假 → clamp 兜底 |
151
+ | 被降权行迁入折叠分组 | `refresh()` 把焦点转成分区标题;锚点设置 `selectedId` 并清空 `selectedSection`,光标回到行列表 |
152
+ | 批量 done 中有行被服务跳过(busy) | 该行仍在原位但被当作已移除而跳过,锚点可能落到更远一行——可接受的视觉容差,不做二次修正 |
153
+ | 协调器拒绝(busy / timeout) | 不应用锚点,光标留在原行 |
154
+ | peek 模式下降权 | 应用锚点;`peekId` 不变,peek 面板仍指向该会话 |
155
+
156
+ 无降级路径:本改动不涉及外部服务、网络或配置,没有需要降级的能力。
157
+
158
+ ## 5. 验收矩阵
159
+
160
+ | ID | 功能点 | 验收方式 | 具体验证 | 通过标准 |
161
+ | --- | --- | --- | --- | --- |
162
+ | A1 | 纯函数邻位语义:中间行 / 末行 / 单行 / 未知 ID / 整块移除 / 全部移除 | 自动化(unit) | `node --test test/rows.test.mjs`(新增 `neighborAfterRemoval` 用例) | 7 类断言全过 |
163
+ | A2 | `h` 降权中间行后光标留在原槽位 | 自动化(integration,探针) | `node --test test/dashboard-position-keep.test.mjs` | `selectedId` == 原位置下方的行,不是被降权行 |
164
+ | A3 | `h` 降权末行后选中上一行 | 自动化(integration,探针) | 同上 | `selectedId` == 上一行 |
165
+ | A4 | `h` 降权唯一行后仍选中该行 | 自动化(integration,探针) | 同上 | `selectedId` == 该行 |
166
+ | A5 | `d`+`y` 降权中间行后光标留在原槽位 | 自动化(integration,探针) | 同上 | `selectedId` == 补位行 |
167
+ | A6 | unhold(holding 行按 `h`)同样保持位置 | 自动化(integration,探针) | 同上 | `selectedId` == 原槽位补位行 |
168
+ | A7 | 批量 done 选中补位行,而非 `completedIds[0]` | 自动化(integration,探针) | 同上 | `selectedId` == 区块下方第一个非选中行;区块在末尾时为区块上一行 |
169
+ | A8 | 单条降权失败时光标不动(`h` 与 `d`+`y` 两条入口) | 自动化(integration,探针) | 同上(分别 patch `service.markCompleted` 与 `service.holdView` 返回 `{ok:false}`) | 两次的 `selectedId` 都等于操作前行 |
170
+ | A9 | 过滤生效时降权仍保持位置 | 自动化(integration,探针) | 同上(设置 `filterQuery` 后驱动 `h`) | `selectedId` == 过滤结果中的补位行 |
171
+ | A10 | peek 模式下降权不改变 peek 目标 | 自动化(integration,探针) | 同上 | `peekId` 不变;`selectedId` == 补位行 |
172
+ | A11 | 后台状态迁移(非用户操作)仍跟随 | 自动化(integration,探针) | 同上(改行状态后 `refresh()`) | `selectedId` 仍是被迁移的行 |
173
+ | A12 | 类型与既有测试不被破坏 | 自动化(static + 回归) | `npm run typecheck`;`npm test` | 无类型错误;既有测试全绿(含 `test/dashboard-delete-key.test.mjs` 对删除回落 `orderedIds[0]` 的断言) |
174
+ | A13 | 批量 done 全部被跳过时不应用锚点 | 自动化(integration,探针) | 同上(patch `service.markCompletedMany` 返回 `{ok:true, completed:0, skipped:N, completedIds:[]}`) | `selectedId` == 操作前行 |
175
+ | U1 | 真终端手感(可选实测) | 用户实测 | 时机:A1–A13 全绿之后、合并之前。前置:在本仓 PR 的 worktree 上 `pi install <worktree 绝对路径>`(或把 `~/.pi/agent/extensions/agent-board` 的 symlink 指向该 worktree)并重启 pi,确认加载的是改动后的代码;然后打开 board,选中中部行依次按 `h`、`d`+`y`,观察光标与滚动位置 | 光标不跳到 `ON HOLD`/`DONE` 分组,滚动不动 |
176
+
177
+ 说明:批量 done 的 `!res.ok` 分支在现有 `markCompletedMany` 实现下不可达(恒返回 `ok:true`,`src/runtime/service.mjs:2488`),不设独立验收项;对应场景由 A13(`completed=0`)覆盖。
178
+
179
+ ## 6. 可测性拆分设计
180
+
181
+ ### 6.1 纯函数层(unit)
182
+
183
+ - `neighborAfterRemoval(orderedIds, removedIds, anchorId)`:输入全是字符串数组/可迭代集合,无 I/O、无时钟。测试放 `test/rows.test.mjs`,与既有 `nextVisibleIdAfterRemoval` 用例相邻。
184
+ - 用例矩阵:`[a,b,c]` 锚 `b` → `c`;锚 `c` → `b`;`[a]` 锚 `a` → `null` 邻居、`anchorIndex=0`;未知锚 → `null`;`[a,b,c,d]` 移除 `{b,c}` 锚 `b` → `d`;移除全部 → `null` 邻居;移除末行锚末行 → 前驱。
185
+
186
+ ### 6.2 UI 薄层(integration,探针)
187
+
188
+ - 探针 `test-support/dashboard-position-keep-probe.ts`:照 `dashboard-delete-key-probe.ts` 的模式,构造真实 service + 真实 `DashboardComponent`,用 `handleInput()` 驱动,白盒读 `selectedId` / `peekId` / `orderedIds`,末尾 `console.log(JSON.stringify(report))`。
189
+ - 测试 `test/dashboard-position-keep.test.mjs`:`execFileSync` 跑一次探针,对同一份报告做 A2–A13 的逐条断言。
190
+ - 边界放在同一份报告里(不同的小节),避免多次起进程。
191
+ - 三处已知坑按 JFox `202609302208475929` 处理:`keybindings = { matches: () => false }`;行补丁同时包 `service.rows()` 与 `service.row(id)`;断言宽度 240。
192
+
193
+ ### 6.3 由此形成的测试边界
194
+
195
+ - 纯函数测「选谁」的全部数学;探针测「三个入口是否在正确的时点捕获/应用、失败分支是否不动、各模式是否一致」。两层不重叠:探针不重复枚举邻位组合,unit 不碰组件状态。
196
+
197
+ ## 7. 风险
198
+
199
+ 1. **异步窗口内的顺序假设**:锚点在 mutation 前捕获,若 mutation 期间用户又按了导航键(`j`/`k`),应用锚点会覆盖用户刚刚的移动。缓解:概率低(确认流程需要连按),且宽限期只有一次状态命令;接受该行为并在实现注释中写明。
200
+ 2. **批量 done 的锚点与实际不一致**:服务可能跳过部分行(busy / 已 completed),跳过行仍在原位但被当作已移除。缓解:可接受容差,A7 只断言「等价于整块移除」的主路径;全部被跳过(`completed=0`)时不应用锚点(A13),避免「什么都没动、光标却跳走」。
201
+ 3. **`prewarmSelected()` 的额外调用**可能导致一次多余的预热请求(指向补位行而非降权行)。缓解:预热本身是幂等缓存,代价可忽略。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhuxixi/pi-agent-board",
3
- "version": "0.13.1",
3
+ "version": "0.14.0",
4
4
  "description": "Agent-board dashboard for Pi: dispatch, monitor, peek/reply, and attach to background Pi sessions.",
5
5
  "type": "module",
6
6
  "main": "./index.ts",