thincoder 0.12.59 → 0.12.61

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 (192) hide show
  1. package/CHANGELOG.md +62 -4
  2. package/README.md +10 -8
  3. package/bin/thincoder.mjs +99 -133
  4. package/package.json +6 -4
  5. package/src/abort-provenance.mjs +116 -0
  6. package/src/acp/bridge.mjs +45 -21
  7. package/src/acp.mjs +6 -1
  8. package/src/advisor/citations.mjs +83 -21
  9. package/src/advisor/compaction.mjs +174 -0
  10. package/src/advisor/loop.mjs +293 -0
  11. package/src/advisor/messages.mjs +59 -137
  12. package/src/advisor/project-context.mjs +194 -0
  13. package/src/advisor/repos.mjs +17 -40
  14. package/src/advisor/run.mjs +156 -359
  15. package/src/advisor/truncate.mjs +57 -0
  16. package/src/advisor.mjs +27 -7
  17. package/src/agent/completion.mjs +17 -11
  18. package/src/agent/dispatch.mjs +145 -27
  19. package/src/agent/helpers.mjs +107 -13
  20. package/src/agent/record-results.mjs +55 -11
  21. package/src/agent/relay-prefix.mjs +39 -0
  22. package/src/agent/run-stages.mjs +242 -0
  23. package/src/agent/setup-reminders.mjs +69 -0
  24. package/src/agent/setup.mjs +107 -127
  25. package/src/agent/spawn-child.mjs +55 -13
  26. package/src/agent-tools/advisor-async.mjs +346 -0
  27. package/src/agent-tools/advisor-settle.mjs +231 -0
  28. package/src/agent-tools/advisor.mjs +167 -116
  29. package/src/agent-tools/async-settle.mjs +191 -0
  30. package/src/agent-tools/batch-segment.mjs +195 -0
  31. package/src/agent-tools/consult.mjs +139 -107
  32. package/src/agent-tools/design-token.mjs +117 -0
  33. package/src/agent-tools/digest-budget.mjs +76 -0
  34. package/src/agent-tools/eng.mjs +24 -29
  35. package/src/agent-tools/escalate-async.mjs +289 -0
  36. package/src/agent-tools/read-history.mjs +166 -32
  37. package/src/agent-tools/recent-changes.mjs +2 -1
  38. package/src/agent-tools/review-streak.mjs +93 -0
  39. package/src/agent-tools/settings.mjs +137 -34
  40. package/src/agent-tools/subagent-actions.mjs +180 -133
  41. package/src/agent-tools/subagent-async.mjs +184 -177
  42. package/src/agent-tools/subagent-panel.mjs +160 -0
  43. package/src/agent-tools/subagent-run.mjs +205 -0
  44. package/src/agent-tools/subagent-scheduler.mjs +100 -27
  45. package/src/agent-tools/subagent-spawn.mjs +453 -0
  46. package/src/agent-tools/subagent.mjs +256 -578
  47. package/src/agent-tools/verify.mjs +119 -292
  48. package/src/agent-tools.mjs +1 -0
  49. package/src/agent.mjs +89 -205
  50. package/src/cli/distill-command.mjs +12 -6
  51. package/src/cli/make-agent.mjs +26 -8
  52. package/src/cli/memory-command.mjs +4 -3
  53. package/src/cli/permission.mjs +2 -2
  54. package/src/cli/setup-wizard.mjs +42 -17
  55. package/src/completions.mjs +114 -0
  56. package/src/config-migrate.mjs +70 -0
  57. package/src/config.mjs +180 -63
  58. package/src/context.mjs +5 -147
  59. package/src/conventions.mjs +223 -0
  60. package/src/crash-reports.mjs +128 -0
  61. package/src/distill.mjs +11 -11
  62. package/src/expand-home.mjs +16 -0
  63. package/src/explore-distill.mjs +155 -0
  64. package/src/generate-title.mjs +1 -1
  65. package/src/hooks.mjs +7 -3
  66. package/src/memory/code-index.mjs +9 -3
  67. package/src/memory/code-sync.mjs +72 -32
  68. package/src/memory/core.mjs +6 -193
  69. package/src/memory/delete.mjs +236 -0
  70. package/src/memory/docs.mjs +68 -54
  71. package/src/memory/file-walk.mjs +109 -0
  72. package/src/memory/schema.mjs +15 -3
  73. package/src/memory.mjs +3 -1
  74. package/src/model-ref.mjs +66 -0
  75. package/src/model-specs.mjs +42 -8
  76. package/src/peer-domains.mjs +265 -0
  77. package/src/peer-instances.mjs +231 -0
  78. package/src/prompt-overlays.mjs +82 -0
  79. package/src/prompts/advisor-design.md +18 -75
  80. package/src/prompts/advisor-round1.md +14 -67
  81. package/src/prompts/advisor-round2.md +15 -51
  82. package/src/prompts/advisor-round3.md +15 -51
  83. package/src/prompts/common.md +115 -0
  84. package/src/prompts/consult-base.md +5 -23
  85. package/src/prompts/discipline-engineering.md +217 -0
  86. package/src/prompts/discipline-normal.md +179 -0
  87. package/src/prompts/persona-coder.md +21 -0
  88. package/src/prompts/persona-eng-coder.md +37 -0
  89. package/src/prompts/persona-eng-designer.md +55 -0
  90. package/src/prompts/persona-engineering.md +54 -0
  91. package/src/prompts/persona-explore.md +15 -0
  92. package/src/prompts/persona-normal.md +27 -0
  93. package/src/prompts/persona-plan.md +26 -0
  94. package/src/provider/anthropic.mjs +4 -4
  95. package/src/provider/core.mjs +18 -98
  96. package/src/provider/errors.mjs +101 -0
  97. package/src/provider/google.mjs +5 -6
  98. package/src/provider/index.mjs +2 -1
  99. package/src/provider/list-models.mjs +93 -0
  100. package/src/provider/rate.mjs +2 -1
  101. package/src/provider/responses.mjs +5 -3
  102. package/src/provider/retry.mjs +8 -45
  103. package/src/provider/sse.mjs +3 -4
  104. package/src/proxy.mjs +9 -14
  105. package/src/session-gc.mjs +214 -0
  106. package/src/session-guard.mjs +47 -0
  107. package/src/session-rename.mjs +38 -0
  108. package/src/session-slots.mjs +188 -60
  109. package/src/session.mjs +104 -124
  110. package/src/token-ttl.mjs +274 -0
  111. package/src/tools/{system.mjs → bash.mjs} +19 -221
  112. package/src/tools/checklist-sync.mjs +181 -0
  113. package/src/tools/checklist.mjs +52 -39
  114. package/src/tools/edit-batch.mjs +109 -10
  115. package/src/tools/edit-diff.mjs +110 -27
  116. package/src/tools/edit.md +17 -12
  117. package/src/tools/execute.mjs +31 -4
  118. package/src/tools/file.mjs +41 -16
  119. package/src/tools/git.md +1 -1
  120. package/src/tools/git.mjs +23 -34
  121. package/src/tools/glob-dialect.mjs +130 -0
  122. package/src/tools/glob.md +3 -3
  123. package/src/tools/grep.md +1 -1
  124. package/src/tools/index.mjs +9 -8
  125. package/src/tools/ops.mjs +188 -3
  126. package/src/tools/patch.mjs +3 -3
  127. package/src/tools/question.md +4 -0
  128. package/src/tools/question.mjs +26 -0
  129. package/src/tools/read.md +1 -2
  130. package/src/tools/read_image.md +1 -1
  131. package/src/tools/search.mjs +236 -0
  132. package/src/tools/shared.mjs +14 -13
  133. package/src/tools/wait_for.md +22 -0
  134. package/src/tui/agent-turn.mjs +36 -228
  135. package/src/tui/ansi.mjs +2 -0
  136. package/src/tui/clipboard.mjs +7 -1
  137. package/src/tui/cmd-advisor.mjs +3 -2
  138. package/src/tui/cmd-config.mjs +142 -30
  139. package/src/tui/cmd-eng.mjs +28 -40
  140. package/src/tui/cmd-exit.mjs +6 -8
  141. package/src/tui/cmd-mcp.mjs +8 -2
  142. package/src/tui/cmd-model.mjs +14 -12
  143. package/src/tui/cmd-new.mjs +3 -2
  144. package/src/tui/cmd-reindex.mjs +7 -0
  145. package/src/tui/cmd-session.mjs +19 -4
  146. package/src/tui/cmd-submodel.mjs +8 -5
  147. package/src/tui/cmd-think.mjs +10 -10
  148. package/src/tui/cmd-undo.mjs +4 -3
  149. package/src/tui/cmd-upgrade.mjs +19 -4
  150. package/src/tui/config-helpers.mjs +28 -16
  151. package/src/tui/distill-cmd.mjs +1 -1
  152. package/src/tui/index.mjs +40 -38
  153. package/src/tui/interaction.mjs +3 -3
  154. package/src/tui/key-handler.mjs +61 -17
  155. package/src/tui/key-modes.mjs +86 -8
  156. package/src/tui/layout.mjs +18 -10
  157. package/src/tui/model-catalog.mjs +89 -0
  158. package/src/tui/model-picker.mjs +498 -0
  159. package/src/tui/mouse.mjs +52 -9
  160. package/src/tui/pickers.mjs +28 -392
  161. package/src/tui/render-frame.mjs +32 -16
  162. package/src/tui/render-loop.mjs +2 -0
  163. package/src/tui/render-segments.mjs +12 -9
  164. package/src/tui/render.mjs +37 -5
  165. package/src/tui/slash-commands.mjs +2 -2
  166. package/src/tui/startup.mjs +4 -0
  167. package/src/tui/subagent-blocks.mjs +106 -295
  168. package/src/tui/subagent-children.mjs +162 -0
  169. package/src/tui/subagent-freeze.mjs +169 -0
  170. package/src/tui/subagent-panel.mjs +24 -31
  171. package/src/tui/suspension-drive.mjs +297 -0
  172. package/src/tui/tool-args.mjs +7 -5
  173. package/src/tui/tool-display.mjs +143 -0
  174. package/src/tui/tool-events.mjs +56 -185
  175. package/src/tui/tui-lifecycle.mjs +46 -4
  176. package/src/tui/update-notice.mjs +4 -0
  177. package/src/tui/wizard.mjs +61 -21
  178. package/src/tui/wrapped-spawn.mjs +38 -0
  179. package/src/prompts/coder.md +0 -56
  180. package/src/prompts/discipline.md +0 -102
  181. package/src/prompts/eng-coder.md +0 -44
  182. package/src/prompts/engineering-sub.md +0 -34
  183. package/src/prompts/engineering.md +0 -340
  184. package/src/prompts/explore.md +0 -21
  185. package/src/prompts/main.md +0 -56
  186. package/src/prompts/methodology-template.md +0 -58
  187. package/src/prompts/plan.md +0 -16
  188. package/src/prompts/system.md +0 -63
  189. package/src/tools/pdf-parse-text.mjs +0 -497
  190. package/src/tools/pdf-parse-xref.mjs +0 -499
  191. package/src/tools/pdf.mjs +0 -155
  192. package/src/tools/read_pdf.md +0 -21
@@ -0,0 +1,346 @@
1
+ /**
2
+ * advisor-async.mjs — async advisor reviews (AGENT-LOOP.md §11.2 — R13, 2026-09-06).
3
+ *
4
+ * The advisor tool runs reviews in a background pool at depth 0 (default async —
5
+ * ruling ②-3 A): the launch returns an ack, the turn ends naturally, the session
6
+ * suspends, the review settles into _pendingAsyncResults and a digest turn
7
+ * delivers the report (§17 consumption machinery — role-agnostic; entries carry
8
+ * role "advisor" and ride the subagent panel as a pseudo-role, ruling ②-4 A).
9
+ *
10
+ * This module owns:
11
+ * 1. the per-review INSTANCE registry (agent._advisorRuns — ruling ②-5 A:
12
+ * Map<reviewId, {reviewType, round, priorOutput, stale, open, docSetKey,
13
+ * designId}>) — the 5-round convergence cap is PER REVIEW (fix #4), no longer
14
+ * a run-global shared budget (design + code used to share _advisorRound);
15
+ * 2. launch resolution: design reviews continue the instance of the same
16
+ * document set (reviewId = the designId minted at the instance's first
17
+ * launch); code reviews continue the newest OPEN code instance (round 2+ of
18
+ * a fix loop) and CLOSE at normal user-run ends once no review is in flight
19
+ * (a converged/abandoned thread must not burn the cap of later tasks);
20
+ * 3. the async pool (agent._asyncAdvisors — ADVISOR_POOL_LIMIT = 4 default,
21
+ * agent.poolLimits.advisor configurable — POOL-CONFIG-UNIFIED; over-limit and
22
+ * same-scope-running launches are refused at once — never queued (②-6a; F-5);
23
+ * cancel (ruling ②-6b — directed abort → cancelled settle: no
24
+ * pending entry, no token, a "评审已取消——token 未签发" reminder);
25
+ * 4. SETTLE accounting (fixes #2/#4 — moved from the tool-result commit): a
26
+ * review whose targets mutated after launch settles STALE — it marks no
27
+ * _calledAdvisorThisRun and issues no token (the guard pushes back); a
28
+ * non-stale code review marks _calledAdvisorThisRun; a passing design review
29
+ * issues its token under the designId slot at settle time.
30
+ * 2026-09-11 第 11 批拆分:settle 记账 + 变更日志 + 陈旧/冻结判定迁 `advisor-settle.mjs`
31
+ * (本文件 500 行 = 硬帽在册);既有 import 面经本文件 re-export 保持不变。
32
+ *
33
+ * The sync path (depth>0 eng-coder self-review / explicit async:false) reuses
34
+ * the same instance resolution (rounds/prior/cap) — its accounting still lands
35
+ * in recordToolResults (agent.mjs run loop), marker-keyed by toolCall id.
36
+ */
37
+ import { randomUUID } from "node:crypto"
38
+ // design-token 工具组(2026-09-08 自本文件迁至 design-token.mjs——再越 500 行硬限)——
39
+ // 既有 import 面(advisor.mjs / 测试)不变:原导出全部经此 re-export 保留。
40
+ export {
41
+ buildApprovedSuffix, stripApprovedSuffix, effectiveTokenTtlMs, generateDesignToken,
42
+ validateDesignToken, makeDesignTokenRegex, settleDesignReview,
43
+ } from "./design-token.mjs"
44
+ // 第 11 批拆分(2026-09-11):settle 记账 + 变更日志(noteMutations)+ 陈旧判定
45
+ // (reviewIsStale)+ 冻结冲突(inflightDesignReviewConflict)迁 advisor-settle.mjs——
46
+ // 既有 import 面(dispatch / subagent-async / escalate-async / 测试)经此 re-export 不变。
47
+ import { normAbs, mutationSeqOf, settleAdvisorRun } from "./advisor-settle.mjs"
48
+ export {
49
+ mutationSeqOf, noteMutations, reviewIsStale, settleAdvisorRun, inflightDesignReviewConflict,
50
+ } from "./advisor-settle.mjs"
51
+ // 第 33 批(§17.5):doc-set 键迁 `review-streak.mjs`(护栏与实例续跑同锚单源;原为私有
52
+ // ——零 import 面)。本文件继续在实例解析 / 池 entries 上消费它。
53
+ import { docSetKey } from "./review-streak.mjs"
54
+ import { runAdvisorReview, resolveAdvisorProvider, ADVISOR_THINKING_PLACEHOLDER } from "../advisor/run.mjs"
55
+ import { stripEventToken } from "../agent/spawn-child.mjs"
56
+ import { logEvent } from "../log.mjs"
57
+ import { deathLine } from "../abort-provenance.mjs"
58
+ // ASYNC-RESULT-CONTAINER.md D3/D6:settle 公共收尾单点 + child signal 构建单点
59
+ import { buildChildSignal, settleAsyncEntry } from "./async-settle.mjs"
60
+ import { nextSubagentId } from "./subagent-scheduler.mjs"
61
+
62
+ // ─────────────────────────────────────────────────────────────────────────────
63
+ // Review-instance registry (agent._advisorRuns — per-review rounds/prior/cap)
64
+ // ─────────────────────────────────────────────────────────────────────────────
65
+
66
+ export function advisorRuns(agent) {
67
+ if (!(agent._advisorRuns instanceof Map)) agent._advisorRuns = new Map()
68
+ return agent._advisorRuns
69
+ }
70
+
71
+ /** The newest OPEN code instance (the thread a fix-round launch continues), or null. */
72
+ export function openCodeRun(agent) {
73
+ const runs = agent?._advisorRuns
74
+ if (!(runs instanceof Map) || runs.size === 0) return null
75
+ for (const r of [...runs.values()].reverse()) {
76
+ if (r.reviewType === "code" && r.open) return r
77
+ }
78
+ return null
79
+ }
80
+
81
+ /** The newest OPEN design instance for the given document set, or null. */
82
+ function openDesignRun(agent, key) {
83
+ const runs = agent?._advisorRuns
84
+ if (!(runs instanceof Map) || runs.size === 0) return null
85
+ for (const r of [...runs.values()].reverse()) {
86
+ if (r.reviewType === "design" && r.open && r.docSetKey === key) return r
87
+ }
88
+ return null
89
+ }
90
+
91
+ /**
92
+ * Resolve the review instance a launch continues (§11.2 ③ — per-review
93
+ * rounds/prior; reviewId = designId for design reviews, a random id for code).
94
+ * - design: continue the OPEN instance of the same document set (fix rounds of
95
+ * a review thread keep its designId and its round/prior); none → new instance.
96
+ * - code: continue the newest OPEN code instance (guard-driven fix loops);
97
+ * none → new instance.
98
+ * The legacy agent._advisorRound/_lastAdvisorOutput fields are SCOPED to the
99
+ * instance before the launch (message building + the run.mjs cap read them) —
100
+ * prior-less continuation degrades to round 1 semantics (fresh full review).
101
+ * @returns {{run: object, isNew: boolean, reviewId: string, designId: string|null}}
102
+ */
103
+ export function resolveAdvisorLaunch(agent, reviewType, { documents = null } = {}) {
104
+ const runs = advisorRuns(agent)
105
+ let run = null
106
+ if (reviewType === "design") {
107
+ const key = docSetKey(documents, agent.cwd)
108
+ run = openDesignRun(agent, key)
109
+ if (!run) {
110
+ const reviewId = randomUUID()
111
+ // F2h (§29.1 2026-09-07): a same-scope re-review reuses the session's
112
+ // designId for this doc-set (any prior instance of the scope — open OR
113
+ // closed — the newest wins). A passed re-review overwrites the slot under
114
+ // the same id (no slot residue — T14: the old token is then rejected at
115
+ // the gate); old slots die at TTL only, kept as the fail-safe fallback.
116
+ const prior = [...runs.values()].reverse().find((r) => r.reviewType === "design" && r.docSetKey === key)
117
+ run = {
118
+ reviewId, reviewType, designId: prior?.designId ?? reviewId,
119
+ round: 0, priorOutput: null, stale: false, open: true, docSetKey: key,
120
+ }
121
+ runs.set(reviewId, run)
122
+ }
123
+ } else {
124
+ run = openCodeRun(agent)
125
+ if (!run) {
126
+ const reviewId = randomUUID()
127
+ run = {
128
+ reviewId, reviewType, designId: null,
129
+ round: 0, priorOutput: null, stale: false, open: true, docSetKey: null,
130
+ }
131
+ runs.set(reviewId, run)
132
+ }
133
+ }
134
+ // Scope the legacy mirror fields (message builder + run.mjs cap + displays).
135
+ agent._advisorRound = run.priorOutput ? run.round : 0
136
+ agent._lastAdvisorOutput = run.priorOutput
137
+ return { run, isNew: run.round === 0 && !run.priorOutput, reviewId: run.reviewId, designId: run.designId }
138
+ }
139
+
140
+ /** Guard round — the OPEN CODE instance only (2026-09-07 §8 F2): the design-written
141
+ * mirror never gates the code guard — no open code instance → 0. */
142
+ export function effectiveAdvisorRound(agent) {
143
+ return openCodeRun(agent)?.round ?? 0
144
+ }
145
+
146
+ // ─────────────────────────────────────────────────────────────────────────────
147
+ // Async pool (agent._asyncAdvisors — pool 上限 F-1/F-2:常量 = 运行时回退权威(2 → 4),
148
+ // config.mjs DEFAULTS.agent.poolLimits.advisor = config 层镜像——耦合锁 config-pool
149
+ // .test.mjs 逐键断言——勿单侧改默认)
150
+ // ─────────────────────────────────────────────────────────────────────────────
151
+
152
+ export const ADVISOR_POOL_LIMIT = 4
153
+
154
+ /** §11.2 advisor 池生效上限(纯——读 agent.poolLimits.advisor——合法 ≥1 整数生效——
155
+ * 非法/缺省回退 ADVISOR_POOL_LIMIT——与 subagent 域 resolvePoolLimits 独立不共享
156
+ * (subagent 两键表不含本键——键表语义不同——POOL-CONFIG-UNIFIED F-2))。 */
157
+ export function resolveAdvisorPoolLimit(raw) {
158
+ const v = raw?.advisor
159
+ return Number.isInteger(v) && v >= 1 ? v : ADVISOR_POOL_LIMIT
160
+ }
161
+
162
+ /** launch 判定点每次读 agent.config(/config 热应用——变更即生效下个 launch——文案
163
+ * 如实报本值)。 */
164
+ export function advisorPoolLimitFor(agent) {
165
+ return resolveAdvisorPoolLimit(agent?.config?.agent?.poolLimits)
166
+ }
167
+ /** §11.2 同 scope 守卫(F-5——用户裁①):同 reviewType+scope 有 running 评审 → true
168
+ * (拒——running 并行多实例歧义放大——settled 续跑语义不变)。design scope = 文档集键
169
+ * (docSetKey);code = 单 code 线程(记录不记路径键——与 openCodeRun 语义一致)。
170
+ * 与池容量守卫独立:容量 = 全局 ≤N——scope = 同 scope ≤1——两关都过才启动。 */
171
+ export function runningAdvisorOfScope(agent, reviewType, docSetKey) {
172
+ const pool = agent?._asyncAdvisors
173
+ if (!(pool instanceof Map)) return false
174
+ for (const e of pool.values()) {
175
+ if (e.status !== "running" || e.reviewType !== reviewType) continue
176
+ if (reviewType === "design" && e.run?.docSetKey !== docSetKey) continue
177
+ return true
178
+ }
179
+ return false
180
+ }
181
+
182
+ export function runningAdvisorCount(agent) {
183
+ return [...(agent?._asyncAdvisors?.values() ?? [])].filter((e) => e.status === "running").length
184
+ }
185
+
186
+ /** Any in-flight (non-done) async advisor review? — the completion guard skips
187
+ * its push-back while a review is pending (T-24b4: 未决不算未评审). */
188
+ export function advisorReviewPending(agent) {
189
+ return [...(agent?._asyncAdvisors?.values() ?? [])].some((e) => !e.done)
190
+ }
191
+
192
+ /** Directed cancel (ruling ②-6b — ⏹/cancel): abort the running review's
193
+ * controller (run.mjs signal chain) → the settle callback runs the cancelled
194
+ * branch (no pending entry / no token / "评审已取消——token 未签发" reminder). */
195
+ export function cancelAsyncAdvisor(agent, id) {
196
+ const key = String(id)
197
+ const map = agent?._asyncAdvisors ?? new Map()
198
+ const entry = map.get(key)
199
+ if (!entry) {
200
+ return { id: key, status: "error", error: `unknown async advisor review id: ${key}` }
201
+ }
202
+ if (entry.done) {
203
+ return { id: key, status: "error", error: `async advisor review #${key} has already finished — nothing to cancel` }
204
+ }
205
+ if (entry.cancelled) return { id: key, status: "cancelled" } // abort already in flight — idempotent
206
+ entry.cancelled = true
207
+ // §20.3 站点 #11(第 24 批):定向中止 = cancel(reason 载荷)
208
+ entry.controller?.abort?.({ abortTrigger: "cancel", abortDetail: "advisor-cancel" })
209
+ return { id: key, status: "cancelled" }
210
+ }
211
+
212
+ /** Relay an advisor onOutput chunk into the subagent block channel (`advisor#N/`
213
+ * prefix — kind-preserving: think via onReasoning, tool via onToolOutput, text
214
+ * via onToken; the wait placeholder is stripped — it is a live indicator, not
215
+ * content, and the sync path strips it at freeze too). */
216
+ function relayAdvisorOutput(callbacks, prefix, chunk) {
217
+ if (!callbacks) return
218
+ const kind = typeof chunk === "string" ? "text" : (chunk?.kind ?? "text")
219
+ const text = typeof chunk === "string" ? chunk : String(chunk?.text ?? "")
220
+ if (!text) return
221
+ if (kind === "think") {
222
+ callbacks.onReasoning?.(prefix + text.replaceAll(ADVISOR_THINKING_PLACEHOLDER, ""))
223
+ } else if (kind === "tool") {
224
+ callbacks.onToolOutput?.(prefix + "tool", { kind: "tool", text })
225
+ } else {
226
+ callbacks.onToken?.(prefix + stripEventToken(text))
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Launch an async advisor review (pool entry + background runner + settle
232
+ * wiring — mirrors subagent-run.mjs's lifecycle, minus queueing/scheduler).
233
+ * The runner wraps runAdvisorReview (promise → report/error) — the subagent
234
+ * pipeline is untouched (ruling ②-2 A). The entry carries role "advisor" so the
235
+ * digest/panel/freeze consumers route it like any other settled block.
236
+ * @returns {{ok: true, id: string}} — ack; or {{error: string}} — scope guard
237
+ * (same reviewType+scope running — §11.2 F-5) or pool full (the effective
238
+ * agent.poolLimits.advisor limit, never queued) or invalid ctx.
239
+ */
240
+ export function launchAsyncAdvisor(parent, ctx, launch) {
241
+ const { reviewType, documents, paths, object, designToken, designId, run } = launch
242
+ const limit = advisorPoolLimitFor(parent)
243
+ // §11.2 same-scope guard(F-5——用户裁①):同 type+scope 有 running → 拒——与池容量
244
+ // 守卫独立两关都过才启动——不等不排(②-6a 无排队语义保持——settled 续跑不变)。
245
+ if (runningAdvisorOfScope(parent, reviewType, run?.docSetKey ?? null)) {
246
+ const scopeNote = reviewType === "design"
247
+ ? "a design review of this document set is still running"
248
+ : "a code review is still running (code reviews are a single thread — launch the next one after it settles)"
249
+ return { error: `Advisor: 此 scope 已有评审在跑——settle 后逐个发起 — ${scopeNote}; round/prior continuation would be ambiguous while it is in flight — wait for it to settle, then launch the next review (AGENT-LOOP.md §11.2).` }
250
+ }
251
+ if (runningAdvisorCount(parent) >= limit) {
252
+ return { error: `Advisor: 另有一评审在跑——逐个发起 — another review is already running in the background pool (${limit} reviews at most — agent.poolLimits.advisor, default ${ADVISOR_POOL_LIMIT}); launch them one at a time (AGENT-LOOP.md §11.2 ruling ②-6a).` }
253
+ }
254
+ parent._asyncAdvisors ??= new Map()
255
+ // SUBAGENT-ID-COUNTER-AGENT(2026-09-09):取号统一走 nextSubagentId——池活续号兜底 + 跨池共号(§11.2)。
256
+ const id = nextSubagentId(parent)
257
+ const entry = {
258
+ id, role: "advisor", reviewType, run,
259
+ reviewId: run.reviewId, designId, designToken, documents, paths, object,
260
+ launchSeq: mutationSeqOf(parent),
261
+ docAbs: reviewType === "design" && Array.isArray(documents)
262
+ ? documents.filter((d) => typeof d === "string").map((d) => normAbs(d, parent.cwd))
263
+ : [],
264
+ relayPrefix: `advisor#${id}/`,
265
+ status: "running", position: undefined,
266
+ report: null, error: null, done: false, cancelled: false,
267
+ promise: null, _settle: null,
268
+ model: (() => { try { return resolveAdvisorProvider(parent).model ?? null } catch { return null } })(),
269
+ startedAt: Date.now(), turn: 0, maxTurns: 0,
270
+ controller: null,
271
+ }
272
+ // Entry controller chained to the session/run base signal (subagent-run parity):
273
+ // Ctrl+C / session abort propagates into the review's chat; a digest's own
274
+ // Ctrl+I must not orphan it (children hold the session signal while suspended).
275
+ // D6 buildChildSignal 单点(ASYNC-RESULT-CONTAINER.md)。
276
+ const ctrl = new AbortController()
277
+ entry.controller = ctrl
278
+ const baseSignal = buildChildSignal(parent, ctx)
279
+ if (baseSignal) {
280
+ // §20.3 站点 #10(第 24 批):hop 逐跳保 reason
281
+ if (baseSignal.aborted) ctrl.abort(baseSignal.reason)
282
+ else baseSignal.addEventListener("abort", () => ctrl.abort(baseSignal.reason), { once: true })
283
+ }
284
+ entry.promise = new Promise((res) => { entry._settle = res })
285
+ entry.start = () => {
286
+ entry.startedAt = Date.now()
287
+ // Async mark + [model] — the TUI block opens at ACTUAL start (⏹ gating reads it).
288
+ ctx?.callbacks?.onToken?.(entry.relayPrefix + "⟦ev⟧async\x1e")
289
+ ctx?.callbacks?.onToken?.(entry.relayPrefix + "[model]" + (entry.model ?? ""))
290
+ runAdvisorReview(parent, reviewType, {
291
+ onOutput: (chunk) => relayAdvisorOutput(ctx?.callbacks, entry.relayPrefix, chunk),
292
+ signal: entry.controller.signal,
293
+ }, designToken, documents, paths, object, designId)
294
+ .then((report) => { entry.report = report })
295
+ // §20.3 第 3 条合成器(第 24 批):原 message 前缀逐字保留 + 来源后缀
296
+ .catch((err) => { entry.error = deathLine(err, entry.controller?.signal) })
297
+ .finally(() => {
298
+ // settle 公共收尾单点(ASYNC-RESULT-CONTAINER.md D3——settleAsyncEntry):日志三连
299
+ // /cancelled 分支(出池+墓碑+⟦ev⟧stopped+"评审已取消——token 未签发"提醒)/挂起分流
300
+ // (pending 单容器+出池——统一守卫 !parentAborted——D4)/公共尾部(settleSeq/_settle/
301
+ // 唤醒 waiter)统一走共享 helper。族特有 hook = settleAdvisorRun 记账(fix #2/#4——
302
+ // 陈旧判定/轮次/token D1 落盘——settle 分支输出写回 entry.report,digest 原样进);
303
+ // cancelled/parent-aborted 不调(不消费预算——settleAdvisorRun 自己的早退语义等价)。
304
+ settleAsyncEntry(parent, entry, {
305
+ pool: parent._asyncAdvisors,
306
+ ctx,
307
+ onAccounting: () => {
308
+ const settled = settleAdvisorRun(parent, entry)
309
+ // §29 fix B:settle 分支输出(清洗/未签发提示)写回 entry.report——digest 原样进。
310
+ if (settled.report != null) entry.report = settled.report
311
+ },
312
+ })
313
+ })
314
+ }
315
+ parent._asyncAdvisors.set(String(id), entry)
316
+ logEvent("child:spawn", { role: "advisor", id: `advisor#${id}`, kind: "async", status: "running", ms: 0 })
317
+ entry.start()
318
+ return { ok: true, id: String(id) }
319
+ }
320
+
321
+ /**
322
+ * Close every OPEN code instance at a normal non-auto run end (finalizeAgentTurn)
323
+ * when no review/child activity remains: a code thread that settled and whose
324
+ * digest was consumed must not burn the 5-round cap of a later task — the next
325
+ * code review starts a fresh full review (round 1). Runs that END with a review
326
+ * or an eng-coder child in flight keep the thread open (fix-round continuation).
327
+ * @returns {boolean} whether any instance was closed
328
+ */
329
+ export function closeOpenCodeAdvisorRuns(agent) {
330
+ const advisors = agent?._asyncAdvisors
331
+ // Any pooled advisor entry — running OR settled-not-consumed — keeps the
332
+ // thread open: an in-run settle whose report has not reached the model (the
333
+ // suspension sweep / digest consumes it next) must still be continuable by
334
+ // the fix round that follows the disposition.
335
+ if ((advisors?.size ?? 0) > 0) return false
336
+ if ((agent?._asyncSubagents?.size ?? 0) > 0) return false
337
+ if ((agent?._pendingAsyncResults ?? []).some((e) => e.role === "advisor")) return false
338
+ let closed = false
339
+ const runs = agent?._advisorRuns
340
+ if (runs instanceof Map) {
341
+ for (const r of runs.values()) {
342
+ if (r.reviewType === "code" && r.open) { r.open = false; closed = true }
343
+ }
344
+ }
345
+ return closed
346
+ }
@@ -0,0 +1,231 @@
1
+ /**
2
+ * advisor-settle.mjs — advisor settle accounting + mutation log + stale / freeze
3
+ * determination(2026-09-11 第 11 批自 advisor-async.mjs 拆出——500 行 = 硬帽在册,
4
+ * 任何新增必越;既有 import 面经 advisor-async.mjs **re-export 保持不变**
5
+ * (noteMutations / mutationSeqOf / reviewIsStale / settleAdvisorRun)。
6
+ *
7
+ * 内容 = 迁入(逐字搬移含注释)+ 本批两处接线:
8
+ * - `settleAdvisorRun` —— 记账本体零改(轮次 / 陈旧 / token D1 落盘 / prior);A 族
9
+ * 未完成判定改用单谓词 `advisorIncompleteMarker`(§14.3 消费点 1/2):design 结算经
10
+ * `settleDesignReview(…, { incomplete })` 未完成即不签发;code 完成守卫同谓词
11
+ * (旧 `^` 锚首行失败正则退役——六形态语义零丢,含 review_failed 字符串
12
+ * resolve 形态);
13
+ * - `inflightDesignReviewConflict` —— E 族(F17/§14.14)D5 冻结窗口冲突检测:与
14
+ * `reviewIsStale` 同族(同 docAbs / 同 normAbs)、仅扫 running 且未取消的设计条目
15
+ * (dispatch Phase-1 预闸消费)。
16
+ *
17
+ * 第 33 批(2026-09-11——评审失败护栏 §17.5):`normAbs` 本体迁 `review-streak.mjs`
18
+ * (原处 re-export 保面)+ design 结算计数接线(分类单源 `designReviewOutcome` →
19
+ * `noteDesignReviewOutcome`,三出口)——记账本体 / 陈旧判定 / 轮次 / token 落盘零改。
20
+ */
21
+ import { persistEngTokens } from "../token-ttl.mjs"
22
+ import { settleDesignReview, makeDesignTokenRegex, stripApprovedSuffix } from "./design-token.mjs"
23
+ import { looksLikeReviewOutput, advisorIncompleteMarker, ADVISOR_LAUNCH_REFUSAL_PREFIX } from "../advisor/run.mjs"
24
+ import { isCodePath, loadConventions } from "../conventions.mjs"
25
+ import { logEvent } from "../log.mjs"
26
+ // 第 33 批(§17.5):`normAbs` 迁 `review-streak.mjs`(中立模块——护栏与陈旧判定共用同一归一)
27
+ // + 结算分类 / 计数落账(异步结算计数点)。
28
+ import { normAbs, designReviewOutcome, noteDesignReviewOutcome } from "./review-streak.mjs"
29
+
30
+ // ─────────────────────────────────────────────────────────────────────────────
31
+ // Mutation log (stale-review determination — fix #2: FILE_MUTATORS after the
32
+ // review launch make the settle stale: no called-mark, no token, guard re-pushes)
33
+ // ─────────────────────────────────────────────────────────────────────────────
34
+
35
+ export function mutationSeqOf(agent) {
36
+ return agent._mutationSeq ?? 0
37
+ }
38
+
39
+ /** Record a file-mutation commit — bounded ring feeding the stale scan. paths = ABSOLUTE.
40
+ * 唯一记账点(§29 fix A):dispatch runOne 写执行成功即刻调用(取代批后段 + 中断分支——
41
+ * 不双计);mergeChildMutations(子代理合入)是独立事件面。 */
42
+ export function noteMutations(agent, paths) {
43
+ const list = (paths ?? []).filter((p) => typeof p === "string" && p.length > 0)
44
+ if (list.length === 0) return
45
+ agent._mutationSeq = (agent._mutationSeq ?? 0) + 1
46
+ const log = agent._mutLog ??= []
47
+ log.push({ seq: agent._mutationSeq, paths: [...new Set(list)] })
48
+ if (log.length > 200) log.splice(0, log.length - 200)
49
+ }
50
+
51
+ // 第 33 批(§17.5):`normAbs` 本体迁 `review-streak.mjs`;原处 re-export 保既有 import 面
52
+ // (advisor-async / 测试)零变。
53
+ export { normAbs } from "./review-streak.mjs"
54
+
55
+ /** Stale = a mutation committed after the launch touched the review's face:
56
+ * code reviews judge the CODE face; design reviews judge their OWN documents. */
57
+ export function reviewIsStale(agent, entry) {
58
+ const log = agent?._mutLog
59
+ if (!Array.isArray(log) || log.length === 0) return false
60
+ const since = log.filter((m) => m.seq > (entry.launchSeq ?? -1))
61
+ if (since.length === 0) return false
62
+ if (entry.reviewType === "design") {
63
+ const scope = (entry.docAbs ?? []).map((p) => normAbs(p, agent?.cwd))
64
+ if (scope.length === 0) return false // no explicit doc scope — nothing to judge stale
65
+ const set = new Set(scope)
66
+ return since.some((m) => (m.paths ?? []).some((p) => set.has(normAbs(p, agent?.cwd))))
67
+ }
68
+ // Code face = the shared classifier (src/conventions.mjs) — one authority with
69
+ // the design gate and the mutation guard (PORTABILITY FR12 / PO-10).
70
+ const conv = loadConventions(agent?.cwd)
71
+ return since.some((m) => (m.paths ?? []).some((p) => isCodePath(normAbs(p, agent?.cwd), conv)))
72
+ }
73
+
74
+ /**
75
+ * E 族(F17/§14.14 E-3d)——D5 冻结窗口写前拦截的冲突检测:设计评审**在途**(点火 → 结算)
76
+ * 期间,父侧对被审文件集(`docAbs` = 声明文档集,含批次档)的写入会被 dispatch 预闸拒绝。
77
+ * 判据与 `reviewIsStale` **同源**(同一 docAbs / 同一 normAbs);仅扫 **running 且未取消**
78
+ * 的设计条目(已结算 / 已取消条目不拦——结算后写不再致 stale、取消的结算早退不判)。
79
+ * 保守残余(如实注):回合中止后池清前的窗口可能拒一笔不致 stale 的写(保守方向)。
80
+ * @param {Object} agent
81
+ * @param {string[]} absPaths — 本次写将触碰的路径(ABS——调用方 resolve(agent.cwd, p))
82
+ * @returns {{id: string, path: string}|null} 命中评审 id + 冲突路径(ABS),无冲突 null
83
+ */
84
+ export function inflightDesignReviewConflict(agent, absPaths) {
85
+ const pool = agent?._asyncAdvisors
86
+ if (!(pool instanceof Map) || pool.size === 0) return null
87
+ const wanted = (absPaths ?? [])
88
+ .filter((p) => typeof p === "string" && p.length > 0)
89
+ .map((p) => normAbs(p, agent?.cwd))
90
+ if (wanted.length === 0) return null
91
+ for (const entry of pool.values()) {
92
+ if (!entry || entry.reviewType !== "design") continue
93
+ if (entry.status !== "running" || entry.cancelled || entry.done) continue
94
+ const scope = (entry.docAbs ?? []).map((p) => normAbs(p, agent?.cwd))
95
+ if (scope.length === 0) continue
96
+ const set = new Set(scope)
97
+ for (const p of wanted) if (set.has(p)) return { id: String(entry.id), path: p }
98
+ }
99
+ return null
100
+ }
101
+
102
+ /**
103
+ * Settle accounting (fix #2/#4 — runs when a non-cancelled review settles,
104
+ * BEFORE the pending transfer / digest injection):
105
+ * 1. round++ (attempts count — parity with the legacy _advisorRound++ budget);
106
+ * 2. stale determination — mutated targets since launch → no called-mark, no token;
107
+ * 3. non-stale: code review → _calledAdvisorThisRun = true; design review →
108
+ * token echo check (settleDesignReview: slot + instance close; single-value
109
+ * mirror retired per DESIGN-TOKEN-SETTLEMENT D3) + D1 settle-time slot persist
110
+ * (persistEngTokens — write failure = settle failure: no registration, no
111
+ * Approved echo, re-review — 评审 #1);
112
+ * 4. §29 fix B — branch-shaped report output (the caller writes it back to
113
+ * entry.report; digest injects the CLEANED form): pass → stripped +
114
+ * Approved/designId suffix (sync 参照形态); persist-failed → stripped +
115
+ * "D1: …token could NOT be durably written…" re-review notice (no Approved
116
+ * suffix — never an unregistered token); stale → echo stripped +
117
+ * "评审目标已变更——token 未签发" prefix — never an unregistered token.
118
+ * 第 11 批增补(A/F11):`incomplete` = 宿主尾族判定(单谓词)——design 结算未完成即不
119
+ * 签发(`settleDesignReview` 的 `opts.incomplete`);code 完成守卫的失败判定改用同谓词
120
+ * (六形态语义零丢——旧 `^` 锚正则退役)。
121
+ * 第 33 批增补(§17.5 计数点 1):design 结算按分类单源(`designReviewOutcome`)落账到
122
+ * 会话级护栏 `agent._designReviewStreaks`(三出口:非陈旧 / 陈旧 / 无报告)——cancel 早退
123
+ * 与无实例路径不计(neutral 语义 = 无尝试发生)。
124
+ * Cancelled / parent-aborted reviews consume nothing (the user dropped the
125
+ * attempt — the retry must not lose budget).
126
+ * @returns {{cancelled: boolean, stale: boolean, passed: boolean, report: string|null}}
127
+ */
128
+ export function settleAdvisorRun(agent, entry) {
129
+ if (entry.cancelled) return { cancelled: true, stale: false, passed: false, report: null }
130
+ const run = entry.run
131
+ if (!run) return { cancelled: false, stale: false, passed: false, report: entry.report ?? null }
132
+ const result = entry.report ?? null
133
+ run.round++
134
+ agent._advisorRound = run.round
135
+ const stale = reviewIsStale(agent, entry)
136
+ run.stale = stale
137
+ let report = result
138
+ let passed = false
139
+ let persistFailed = false
140
+ // 宿主尾族判定(单谓词——design 结算与 code 守卫共用;§14.3 消费点 1/2)。
141
+ const incomplete = result != null ? advisorIncompleteMarker(String(result)) : null
142
+ // 启动拒绝报告(未发起请求——无评审产出):第 33 批上移为本结算段**单点**——消费 = ① design
143
+ // 失败分类(neutral——无尝试发生,§17.3 #1)② code 守卫 failureVerdict(既有语义零变)。
144
+ const launchRefused = result != null && String(result).startsWith(ADVISOR_LAUNCH_REFUSAL_PREFIX)
145
+ if (!stale) {
146
+ if (run.reviewType === "design" && entry.designToken && result) {
147
+ // settle 前 Map 快照——落盘失败时回滚用(settle 失败 = 结算未发生,不留半结算态:
148
+ // 重评覆盖旧槽的边角(F2h 复用 designId)也原样恢复旧 token——内存与盘一致)。
149
+ const preMap = agent._engDesignTokens instanceof Map
150
+ ? new Map(agent._engDesignTokens)
151
+ : null
152
+ const settled = settleDesignReview(agent, run, entry.designToken, result, { incomplete })
153
+ if (settled.passed) {
154
+ // DESIGN-TOKEN-SETTLEMENT D1(2026-09-08):settle 是唯一结算点——settle 当场
155
+ // 同步落盘 token 字段到槽文件(persistEngTokens = engTokenSlotFields 序列化 +
156
+ // session 安全写/轮转——勿裸写文件),不等下个回合尾 saveSession(消除"settle→
157
+ // 下个 saveSession"间的重启丢 token 窗口)。
158
+ // 写失败即 settle 失败(评审 #1):token 不注册(Map 回滚到 settle 前快照)、无
159
+ // Approved 回显、可重评——不静默吞错、不产生"内存有盘上无"态(宁可结算失败
160
+ // 可重评,不留半结算态)。
161
+ let durable = false
162
+ try {
163
+ durable = persistEngTokens(agent)
164
+ } catch (e) {
165
+ logEvent("advisor:error", { id: `advisor#${entry.id}`, err: `engDesignTokens slot persist threw: ${e?.message ?? String(e)}` })
166
+ }
167
+ if (durable) {
168
+ passed = true
169
+ report = settled.output
170
+ } else {
171
+ if (preMap) agent._engDesignTokens = preMap
172
+ else delete agent._engDesignTokens
173
+ run.approvedSuffix = null
174
+ persistFailed = true
175
+ logEvent("advisor:error", { id: `advisor#${entry.id}`, err: "engDesignTokens slot persist failed — settle failed (re-review)" })
176
+ const stripped = String(result)
177
+ .replace(makeDesignTokenRegex(entry.designToken, "g"), "")
178
+ .trim()
179
+ report = `${stripped}\n\nD1: the design review passed but the token could NOT be durably written to the session ledger (slot persist failed) — re-run advisor(type='design') to re-issue; no eng-coder spawn is authorized for this review (评审通过但 token 未能持久化——需重评).`.trim()
180
+ }
181
+ } else {
182
+ report = settled.output
183
+ }
184
+ }
185
+ // A completed review covers the code face ONLY when it actually produced a
186
+ // verdict: a mechanical-failure settle ("Advisor: review failed/timeout/…" —
187
+ // run.mjs resolves with the failure text, it never throws) must not satisfy
188
+ // the guard silently — the digest shows the failure and the guard pushes
189
+ // back for a retry (fix #2 anti-silent-skip intent; attempts still consume
190
+ // the per-review round budget, so repeated failures stay bounded by the
191
+ // cap). An error settle (rejection path — report null with an error) has no
192
+ // verdict either — same exclusion (advisor 复评补边). Design reviews keep
193
+ // the mark on any completed verdict (parity with the sync recordToolResults
194
+ // mark — they have no code face to cover) — EXCEPT a launch-refusal report
195
+ // (nothing was sent, no verdict at all: see launchRefused below).
196
+ // 第 11 批(F16/A3):失败判定 = 单谓词六 kind(含 review_failed 字符串 resolve 形态)
197
+ // ——旧 `^` 锚正则只认首行形态,漏「时间线 + 尾」;语义零丢(六形态全覆盖)。
198
+ // 另:设计评审的**启动拒绝报告**(未发起请求——无评审产出)同样不得计为评审覆盖
199
+ // (§14.4 异步结算面判据;与同步工具面 _advisorRefusals 同源前缀;正常链不可达=防御纵深)
200
+ // ——判定已上移为本结算段单点(launchRefused,上方)。
201
+ const failureVerdict = (run.reviewType !== "design" && (
202
+ incomplete !== null ||
203
+ (result == null && entry.error != null)
204
+ )) || launchRefused
205
+ if (!failureVerdict) {
206
+ agent._calledAdvisorThisRun = true
207
+ }
208
+ } else if (run.reviewType === "design" && entry.designToken && result != null) {
209
+ // §29 fix B(stale 分支):陈旧评审不签发——digest 不得展示未注册 token——先剥
210
+ // 方括号回显 + 前置 "评审目标已变更——token 未签发"(不变式——两分支都清洗)。
211
+ const stripped = String(result)
212
+ .replace(makeDesignTokenRegex(entry.designToken, "g"), "")
213
+ .trim()
214
+ report = `评审目标已变更——token 未签发 (review target changed after launch — this review judged a stale state; no design token was issued — re-run the review on the current state)\n\n${stripped}`.trim()
215
+ }
216
+ // 第 33 批(§17.5 计数点 1——异步结算):design 三出口(非陈旧 / 陈旧 / design 无报告)统一
217
+ // 按分类单源落账——count/stale/no_credential/no_report 增计数,可用判决复位,取消 / 中断 /
218
+ // 拒发 neutral(分类表见 §17.3)。cancel 早退 / 无实例在函数头已返回(不计)。
219
+ if (run.reviewType === "design") {
220
+ noteDesignReviewOutcome(agent, run.docSetKey, designReviewOutcome({
221
+ launchRefused, stale, hasResult: result != null, incomplete, persistFailed,
222
+ }))
223
+ }
224
+ // Prior of round 2+ = the last REVIEW-LOOKING output (mirror of run.mjs's guard).
225
+ // F2e (§29.1): strip the engine-approved suffix FIRST — the prior must never
226
+ // carry the raw token / designId (exact truncation — zero collateral).
227
+ if (report && looksLikeReviewOutput(report)) {
228
+ run.priorOutput = stripApprovedSuffix(report, run.approvedSuffix)
229
+ }
230
+ return { cancelled: false, stale, passed, report }
231
+ }