@thincoder/core 0.11.1 → 0.11.2

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 (333) hide show
  1. package/CHANGELOG.md +46 -2
  2. package/advisor/convergence.mjs +34 -3
  3. package/advisor/loop.mjs +42 -10
  4. package/advisor/messages.mjs +42 -25
  5. package/advisor/project-context.mjs +171 -42
  6. package/advisor/run.mjs +7 -4
  7. package/advisor.mjs +29 -43
  8. package/agent/batch-attribution.mjs +87 -0
  9. package/agent/batch-changed-surface.mjs +129 -0
  10. package/agent/completion.mjs +44 -3
  11. package/agent/dispatch-gates.mjs +6 -0
  12. package/agent/dispatch.mjs +8 -3
  13. package/agent/family-tools.mjs +27 -12
  14. package/agent/helpers.mjs +122 -1
  15. package/agent/offload.mjs +25 -29
  16. package/agent/post-turn.mjs +24 -10
  17. package/agent/record-results.mjs +18 -1
  18. package/agent/run-stages.mjs +77 -32
  19. package/agent/run-start.mjs +37 -0
  20. package/agent/setup-reminders.mjs +9 -2
  21. package/agent/setup.mjs +24 -11
  22. package/agent/spawn-child.mjs +38 -1
  23. package/agent/timers.mjs +5 -3
  24. package/agent/turn-loop.mjs +30 -5
  25. package/agent/turn-projection.mjs +386 -0
  26. package/agent/turn-task-state.mjs +274 -0
  27. package/agent/turn-tool-process.mjs +168 -0
  28. package/agent/write-gate-ledger.mjs +569 -0
  29. package/agent/write-gate.mjs +29 -352
  30. package/agent-tools/advisor-async.mjs +29 -169
  31. package/agent-tools/advisor-launch.mjs +178 -0
  32. package/agent-tools/advisor-settle.mjs +28 -1
  33. package/agent-tools/advisor.mjs +36 -47
  34. package/agent-tools/audit-block.mjs +6 -6
  35. package/agent-tools/batch-closeout.mjs +273 -35
  36. package/agent-tools/batch-gate.mjs +142 -342
  37. package/agent-tools/batch-ledger-bind.mjs +226 -17
  38. package/agent-tools/batch-ledger-view.mjs +109 -0
  39. package/agent-tools/batch-lifecycle.mjs +169 -46
  40. package/agent-tools/batch-paths.mjs +29 -8
  41. package/agent-tools/batch-record-write.mjs +417 -0
  42. package/agent-tools/batch-regions.mjs +215 -0
  43. package/agent-tools/batch-reopen.mjs +23 -11
  44. package/agent-tools/batch-segment-vocab.mjs +379 -0
  45. package/agent-tools/batch-skeleton.mjs +192 -239
  46. package/agent-tools/batch-supersede.mjs +232 -0
  47. package/agent-tools/batch-tier-gate.mjs +297 -0
  48. package/agent-tools/batch.mjs +182 -439
  49. package/agent-tools/consult.mjs +7 -4
  50. package/agent-tools/context.mjs +44 -17
  51. package/agent-tools/design-review-record.mjs +225 -0
  52. package/agent-tools/design-slots.mjs +1 -1
  53. package/agent-tools/design-token.mjs +279 -81
  54. package/agent-tools/doc-check.mjs +43 -33
  55. package/agent-tools/doc-graph.mjs +140 -0
  56. package/agent-tools/eng.mjs +9 -8
  57. package/agent-tools/escalate-async.mjs +2 -2
  58. package/agent-tools/manifest.mjs +34 -18
  59. package/agent-tools/read-history.mjs +112 -11
  60. package/agent-tools/review-facts.mjs +10 -3
  61. package/agent-tools/settings.mjs +23 -2
  62. package/agent-tools/spawn-gates.mjs +9 -8
  63. package/agent-tools/ssh-session.mjs +2 -2
  64. package/agent-tools/ssh-text.mjs +155 -0
  65. package/agent-tools/ssh-transport.mjs +19 -148
  66. package/agent-tools/subagent-actions-query.mjs +10 -6
  67. package/agent-tools/subagent-async.mjs +8 -9
  68. package/agent-tools/subagent-brief.mjs +76 -0
  69. package/agent-tools/subagent-design-token.mjs +168 -0
  70. package/agent-tools/subagent-run.mjs +1 -1
  71. package/agent-tools/subagent-scheduler.mjs +4 -4
  72. package/agent-tools/subagent-spawn.mjs +90 -110
  73. package/agent-tools/subagent.mjs +11 -10
  74. package/agent-tools/taskbook-block.mjs +94 -0
  75. package/agent-tools/timer.mjs +12 -8
  76. package/agent-tools/verify.mjs +13 -4
  77. package/agent-tools.mjs +7 -3
  78. package/agent.mjs +20 -1
  79. package/attachments.mjs +3 -3
  80. package/browser/actions.mjs +9 -3
  81. package/browser/cdp.mjs +45 -17
  82. package/browser/launch.mjs +25 -43
  83. package/browser/pool.mjs +299 -0
  84. package/browser/queue.mjs +2 -0
  85. package/browser/session-state.mjs +450 -0
  86. package/browser/session.mjs +127 -151
  87. package/browser/snapshot.mjs +89 -0
  88. package/client-identity.mjs +1 -1
  89. package/config-defaults.mjs +2 -1
  90. package/config-migrate.mjs +40 -0
  91. package/config-normalize.mjs +28 -2
  92. package/config-reload.mjs +59 -0
  93. package/config.mjs +62 -98
  94. package/context-degrade.mjs +38 -10
  95. package/context-echo.mjs +11 -0
  96. package/context.mjs +149 -83
  97. package/conventions.mjs +11 -9
  98. package/declaration.mjs +31 -5
  99. package/declared-repos.mjs +94 -0
  100. package/doc-check/doc-names.mjs +73 -0
  101. package/doc-check/doc-tokens.mjs +110 -0
  102. package/doc-check/existence.mjs +160 -0
  103. package/doc-check/index.mjs +309 -213
  104. package/doc-check/line-refs.mjs +27 -101
  105. package/doc-check/paragraph.mjs +218 -102
  106. package/doc-graph/anchors.mjs +474 -0
  107. package/doc-graph/doc-batch-face.mjs +59 -0
  108. package/doc-graph/doc-suffix.mjs +27 -0
  109. package/doc-graph/graph-tokens.mjs +160 -0
  110. package/doc-graph.mjs +778 -0
  111. package/explore-distill.mjs +11 -5
  112. package/i18n.mjs +1 -1
  113. package/instance-inbox-watch.mjs +272 -0
  114. package/instance-inbox.mjs +218 -0
  115. package/instance-registry.mjs +179 -0
  116. package/knowledge/README.md +3 -0
  117. package/knowledge/prompt-resources.md +6 -0
  118. package/ledger-close-proof.mjs +100 -49
  119. package/ledger-close.mjs +188 -0
  120. package/ledger-cmd.mjs +47 -91
  121. package/ledger-consistency.mjs +113 -0
  122. package/ledger-db.mjs +47 -15
  123. package/ledger-executors.mjs +1 -1
  124. package/ledger-tools.mjs +78 -19
  125. package/ledger-write-ops.mjs +130 -41
  126. package/ledger.mjs +1 -1
  127. package/manifest-discovery.mjs +3 -1
  128. package/manifest-schema.mjs +27 -5
  129. package/memory/schema.mjs +2 -2
  130. package/package.json +3 -1
  131. package/path-resolve.mjs +338 -57
  132. package/prompt-files.mjs +91 -1
  133. package/prompt-overlays.mjs +9 -6
  134. package/prompts/advisor-design.md +62 -21
  135. package/prompts/advisor-round1.md +34 -1
  136. package/prompts/advisor-round2.md +40 -7
  137. package/prompts/advisor-round3.md +39 -6
  138. package/prompts/common.md +14 -387
  139. package/prompts/consult-base.md +5 -1
  140. package/prompts/discipline-engineering.md +226 -51
  141. package/prompts/discipline-normal.md +4 -255
  142. package/prompts/knowledge/business-gates.md +22 -0
  143. package/prompts/knowledge/decision-quality.md +23 -0
  144. package/prompts/knowledge/doc-lookup.md +35 -0
  145. package/prompts/knowledge/document-writing.md +51 -0
  146. package/prompts/knowledge/documentation-evaluation.md +56 -0
  147. package/prompts/knowledge/evidence-discipline.md +57 -0
  148. package/prompts/knowledge/normal-coding-workflow.md +104 -0
  149. package/prompts/knowledge/normal-delivery-collaboration.md +108 -0
  150. package/prompts/knowledge/normal-work-management.md +57 -0
  151. package/prompts/knowledge/product-security.md +23 -0
  152. package/prompts/knowledge/project-records.md +62 -0
  153. package/prompts/knowledge/tool-advisor.md +29 -0
  154. package/prompts/knowledge/tool-apply-patch.md +18 -0
  155. package/prompts/knowledge/tool-bash.md +39 -0
  156. package/prompts/knowledge/tool-batch-eng-coder.md +12 -0
  157. package/prompts/knowledge/tool-batch-eng-designer.md +12 -0
  158. package/prompts/knowledge/tool-batch-review.md +12 -0
  159. package/prompts/knowledge/tool-batch.md +80 -0
  160. package/prompts/knowledge/tool-browser.md +57 -0
  161. package/prompts/knowledge/tool-code-search.md +4 -0
  162. package/prompts/knowledge/tool-consult-start.md +12 -0
  163. package/prompts/knowledge/tool-consult-stop.md +6 -0
  164. package/prompts/knowledge/tool-context.md +6 -0
  165. package/prompts/knowledge/tool-delete.md +17 -0
  166. package/prompts/knowledge/tool-doc-check.md +70 -0
  167. package/prompts/knowledge/tool-doc-graph-read.md +20 -0
  168. package/prompts/knowledge/tool-doc-graph.md +49 -0
  169. package/prompts/knowledge/tool-doc-search.md +6 -0
  170. package/prompts/knowledge/tool-edit.md +38 -0
  171. package/prompts/knowledge/tool-eng.md +2 -0
  172. package/prompts/knowledge/tool-execute.md +16 -0
  173. package/prompts/knowledge/tool-fetch.md +12 -0
  174. package/prompts/knowledge/tool-file-ops.md +16 -0
  175. package/prompts/knowledge/tool-get-current-time.md +7 -0
  176. package/prompts/knowledge/tool-git-read.md +19 -0
  177. package/prompts/knowledge/tool-git.md +29 -0
  178. package/prompts/knowledge/tool-glob.md +15 -0
  179. package/prompts/knowledge/tool-goal.md +8 -0
  180. package/prompts/knowledge/tool-grep.md +18 -0
  181. package/prompts/knowledge/tool-hashline-edit.md +14 -0
  182. package/prompts/knowledge/tool-insert-after.md +15 -0
  183. package/prompts/knowledge/tool-instance-list.md +6 -0
  184. package/prompts/knowledge/tool-instance-send.md +10 -0
  185. package/prompts/knowledge/tool-knowledge.md +18 -0
  186. package/prompts/knowledge/tool-ledger-read.md +35 -0
  187. package/prompts/knowledge/tool-ledger.md +42 -0
  188. package/prompts/knowledge/tool-lint.md +16 -0
  189. package/prompts/knowledge/tool-ls.md +13 -0
  190. package/prompts/knowledge/tool-lsp.md +11 -0
  191. package/prompts/knowledge/tool-main-history.md +2 -0
  192. package/prompts/knowledge/tool-manifest-read.md +14 -0
  193. package/prompts/knowledge/tool-manifest.md +31 -0
  194. package/prompts/knowledge/tool-memory-read.md +20 -0
  195. package/prompts/knowledge/tool-memory.md +15 -0
  196. package/prompts/knowledge/tool-notify-parent.md +12 -0
  197. package/prompts/knowledge/tool-operations.md +114 -0
  198. package/prompts/knowledge/tool-peer-instances.md +2 -0
  199. package/prompts/knowledge/tool-plan.md +6 -0
  200. package/prompts/knowledge/tool-process-read.md +13 -0
  201. package/prompts/knowledge/tool-process.md +15 -0
  202. package/prompts/knowledge/tool-question.md +16 -0
  203. package/prompts/knowledge/tool-read-history-batch.md +22 -0
  204. package/prompts/knowledge/tool-read-history.md +36 -0
  205. package/prompts/knowledge/tool-read-image.md +11 -0
  206. package/prompts/knowledge/tool-read.md +21 -0
  207. package/prompts/knowledge/tool-recent-changes.md +6 -0
  208. package/prompts/knowledge/tool-repo-outline.md +4 -0
  209. package/prompts/knowledge/tool-settings-read.md +12 -0
  210. package/prompts/knowledge/tool-settings.md +18 -0
  211. package/prompts/knowledge/tool-skill.md +6 -0
  212. package/prompts/knowledge/tool-ssh.md +52 -0
  213. package/prompts/knowledge/tool-subagent.md +59 -0
  214. package/prompts/knowledge/tool-task.md +12 -0
  215. package/prompts/knowledge/tool-team-ledger-read.md +11 -0
  216. package/prompts/knowledge/tool-team-ledger.md +11 -0
  217. package/prompts/knowledge/tool-timer.md +23 -0
  218. package/prompts/knowledge/tool-tree.md +14 -0
  219. package/prompts/knowledge/tool-verify.md +19 -0
  220. package/prompts/knowledge/tool-wait-for.md +25 -0
  221. package/prompts/knowledge/tool-websearch.md +19 -0
  222. package/prompts/knowledge/tool-write.md +9 -0
  223. package/prompts/knowledge/upstream-protocol.md +43 -0
  224. package/prompts/persona-eng-coder.md +21 -3
  225. package/prompts/persona-eng-designer.md +131 -169
  226. package/prompts/persona-engineering.md +142 -84
  227. package/prompts/persona-explore.md +1 -1
  228. package/prompts/persona-plan.md +5 -1
  229. package/prompts/subagent-base.md +2 -0
  230. package/provider/anthropic.mjs +6 -4
  231. package/provider/core.mjs +3 -2
  232. package/provider/errors.mjs +29 -4
  233. package/provider/google.mjs +24 -7
  234. package/provider/responses.mjs +3 -1
  235. package/provider-flows.mjs +2 -2
  236. package/session-index-build.mjs +1 -1
  237. package/session-index-pass.mjs +1 -1
  238. package/session-index-query.mjs +1 -1
  239. package/session-index.mjs +1 -1
  240. package/session-lifecycle.mjs +72 -17
  241. package/session-slot-write.mjs +37 -5
  242. package/session.mjs +24 -8
  243. package/skip-dirs.mjs +2 -2
  244. package/team-ledger.mjs +13 -9
  245. package/token-ttl.mjs +73 -4
  246. package/token-window.mjs +167 -33
  247. package/tool-docs/advisor.md +2 -27
  248. package/tool-docs/apply_patch.md +1 -15
  249. package/tool-docs/bash.md +2 -41
  250. package/tool-docs/batch-eng-coder.md +3 -0
  251. package/tool-docs/batch-eng-designer.md +3 -0
  252. package/tool-docs/batch-review.md +3 -0
  253. package/tool-docs/batch.md +2 -84
  254. package/tool-docs/browser.md +2 -49
  255. package/tool-docs/code_search.md +2 -2
  256. package/tool-docs/consult_start.md +2 -8
  257. package/tool-docs/consult_stop.md +2 -4
  258. package/tool-docs/context.md +1 -5
  259. package/tool-docs/delete.md +2 -17
  260. package/tool-docs/doc_check.md +2 -107
  261. package/tool-docs/doc_graph-read.md +3 -0
  262. package/tool-docs/doc_graph.md +3 -0
  263. package/tool-docs/doc_search.md +2 -4
  264. package/tool-docs/edit.md +1 -38
  265. package/tool-docs/eng.md +3 -1
  266. package/tool-docs/execute.md +2 -18
  267. package/tool-docs/fetch.md +2 -14
  268. package/tool-docs/file_ops.md +2 -18
  269. package/tool-docs/get_current_time.md +1 -6
  270. package/tool-docs/git-read.md +3 -0
  271. package/tool-docs/git.md +2 -35
  272. package/tool-docs/glob.md +2 -10
  273. package/tool-docs/goal.md +1 -6
  274. package/tool-docs/grep.md +2 -18
  275. package/tool-docs/hashline_edit.md +2 -15
  276. package/tool-docs/insert_after.md +2 -16
  277. package/tool-docs/instance_list.md +3 -0
  278. package/tool-docs/instance_send.md +3 -0
  279. package/tool-docs/knowledge.md +3 -0
  280. package/tool-docs/ledger-read.md +3 -0
  281. package/tool-docs/ledger.md +3 -8
  282. package/tool-docs/lint.md +2 -10
  283. package/tool-docs/ls.md +2 -11
  284. package/tool-docs/lsp.md +2 -9
  285. package/tool-docs/main_history.md +3 -1
  286. package/tool-docs/manifest-read.md +2 -8
  287. package/tool-docs/manifest.md +2 -35
  288. package/tool-docs/memory-read.md +3 -0
  289. package/tool-docs/memory.md +2 -20
  290. package/tool-docs/notify_parent.md +2 -13
  291. package/tool-docs/peer_instances.md +3 -1
  292. package/tool-docs/plan.md +1 -5
  293. package/tool-docs/process-read.md +3 -0
  294. package/tool-docs/process.md +2 -13
  295. package/tool-docs/question.md +3 -17
  296. package/tool-docs/read.md +2 -19
  297. package/tool-docs/read_history-batch.md +3 -0
  298. package/tool-docs/read_history.md +2 -18
  299. package/tool-docs/read_image.md +2 -9
  300. package/tool-docs/recent_changes.md +2 -2
  301. package/tool-docs/repo_outline.md +2 -2
  302. package/tool-docs/settings-read.md +3 -0
  303. package/tool-docs/settings.md +1 -12
  304. package/tool-docs/skill.md +2 -6
  305. package/tool-docs/ssh.md +1 -62
  306. package/tool-docs/subagent.md +2 -100
  307. package/tool-docs/task.md +1 -7
  308. package/tool-docs/team_ledger-read.md +3 -0
  309. package/tool-docs/team_ledger.md +3 -9
  310. package/tool-docs/timer.md +2 -19
  311. package/tool-docs/tree.md +2 -13
  312. package/tool-docs/verify.md +2 -12
  313. package/tool-docs/wait_for.md +2 -32
  314. package/tool-docs/websearch.md +2 -19
  315. package/tool-docs/write.md +2 -12
  316. package/tools/browser.mjs +52 -7
  317. package/tools/doc-write-gate.mjs +73 -0
  318. package/tools/edit-batch.mjs +170 -92
  319. package/tools/edit-diff.mjs +78 -62
  320. package/tools/edit-receipt.mjs +39 -0
  321. package/tools/execute.mjs +1 -1
  322. package/tools/file.mjs +16 -8
  323. package/tools/git.mjs +24 -9
  324. package/tools/index.mjs +6 -0
  325. package/tools/knowledge.mjs +143 -0
  326. package/tools/linter.mjs +3 -3
  327. package/tools/ops.mjs +3 -1
  328. package/tools/patch.mjs +123 -43
  329. package/tools/shared.mjs +28 -27
  330. package/tools/write-gate-params.mjs +31 -0
  331. package/tools/write-path.mjs +16 -4
  332. package/window-policy.mjs +203 -0
  333. package/doc-check/anchors.mjs +0 -235
@@ -7,17 +7,14 @@
7
7
  * delivers the report (AGENT-LOOP-ASYNC-POOL.md §6.8 consumption machinery — role-agnostic; entries carry
8
8
  * role "advisor" and ride the subagent panel as a pseudo-role, ruling ②-4 A).
9
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}>) — rounds/prior are PER REVIEW (fix #4), no longer a run-global
14
- * shared counter (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,
10
+ * This module owns the WRITE half — launch ∥ queue ∥ cancel ∥ close. The READ half
11
+ * (which instance a launch continues, and whether the pool admits a start: the
12
+ * per-review INSTANCE registry agent._advisorRuns + launch resolution + pool limits
13
+ * + the same-scope guard) lives in `advisor-launch.mjs`, split out 2026-11-12 when
14
+ * this file crossed the 500-line design line; its exports are re-exported below, so
15
+ * the existing import face is unchanged.
16
+ *
17
+ * 1. the async pool (agent._asyncAdvisors — ADVISOR_POOL_LIMIT = 4 default,
21
18
  * agent.poolLimits.advisor configurable — POOL-CONFIG-UNIFIED; same-scope
22
19
  * running launches are refused at once — never queued (F-5); over-limit +
23
20
  * DIFFERENT scope launches QUEUE (ED-4 2026-09-16 — AGENT-LOOP-ASYNC-POOL.md §6.10:
@@ -25,7 +22,7 @@
25
22
  * queued cancel dequeue+renumber — the former over-limit refusal ②-6a is
26
23
  * retired); cancel (ruling ②-6b — directed abort → cancelled settle: no
27
24
  * pending entry, no token, a "评审已取消——token 未签发" reminder);
28
- * 4. SETTLE accounting (fixes #2/#4 — moved from the tool-result commit): a
25
+ * 2. SETTLE accounting (fixes #2/#4 — moved from the tool-result commit): a
29
26
  * review whose targets mutated after launch settles STALE — it marks no
30
27
  * _calledAdvisorThisRun and issues no token (the guard pushes back); a
31
28
  * non-stale code review marks _calledAdvisorThisRun; a passing design review
@@ -37,7 +34,15 @@
37
34
  * the same instance resolution (rounds/prior/cap) — its accounting still lands
38
35
  * in recordToolResults (agent.mjs run loop), marker-keyed by toolCall id.
39
36
  */
40
- import { randomUUID } from "node:crypto"
37
+ // 实例与池判定一族(只读:哪条实例接这一轮、池让不让起)2026-11-12 迁 advisor-launch.mjs——
38
+ // 本档过 500 设计器硬线,分拆方案 = `docs/core/design/DESIGN-TOKEN-SETTLEMENT.md` §10(本批内实施)。
39
+ // 既有 import 面(completion.mjs ∥ advisor.mjs ∥ design-slots.mjs ∥ record-results.mjs ∥ 测试)
40
+ // 经此 re-export 不变。
41
+ import { advisorRunsRead, advisorPoolLimitFor, runningAdvisorOfScope, runningAdvisorCount } from "./advisor-launch.mjs"
42
+ export {
43
+ ADVISOR_POOL_LIMIT, advisorRuns, advisorReviewPending, advisorPoolLimitFor, effectiveAdvisorRound,
44
+ openCodeRun, resolveAdvisorLaunch, resolveAdvisorPoolLimit, runningAdvisorCount, runningAdvisorOfScope,
45
+ } from "./advisor-launch.mjs"
41
46
  // design-token 工具组(2026-09-08 自本文件迁至 design-token.mjs——再越 500 行硬限)——
42
47
  // 既有 import 面(advisor.mjs / 测试)不变:原导出全部经此 re-export 保留。
43
48
  export {
@@ -52,8 +57,8 @@ export {
52
57
  mutationSeqOf, noteMutations, reviewIsStale, settleAdvisorRun, inflightDesignReviewConflict,
53
58
  } from "./advisor-settle.mjs"
54
59
  // 第 33 批(评审失败护栏):doc-set 键迁事实面档(实例续跑与陈旧判定同锚单源;原为私有——零 import 面)。
55
- // 本文件继续在实例解析 / 池 entries 上消费它。
56
- import { docSetKey, resolveReviewDocPaths, scopeAdvisorMirror } from "./review-facts.mjs"
60
+ // 实例解析随 2026-11-12 拆分迁 advisor-launch.mjs;本文件只剩池 entries 的 docAbs 产出面消费它。
61
+ import { resolveReviewDocPaths, scopeAdvisorMirror } from "./review-facts.mjs"
57
62
  // F31(2026-09-18 顾问面治理批):拒回文案对象标识行单源。
58
63
  import { withIdentityLine, scopeSummary } from "../advisor/notice.mjs"
59
64
  import { runAdvisorReview, resolveAdvisorProvider, ADVISOR_THINKING_PLACEHOLDER } from "../advisor/run.mjs"
@@ -68,159 +73,6 @@ import { nextSubagentId, consumeSubagentToken, assertPoolKeyFree, entryTerminal
68
73
  // #934(零落笔看门狗——AGENT-LOOP-UPSTREAM.md §6.32.6 触发接线②):评审族推送单源(叶档——恒零抛)。
69
74
  import { pushAdvisorAlert } from "./zero-write-watch.mjs"
70
75
 
71
- // ─────────────────────────────────────────────────────────────────────────────
72
- // Review-instance registry (_advisorRuns — per-review rounds/prior/cap)
73
- // ─────────────────────────────────────────────────────────────────────────────
74
-
75
- /** 载体吸收后的只读读取(不创建):父对象字段优先、缺字段回退跨 run 载体
76
- * (`history` 数组——agent 逐 run 重建的形态靠它跨 run 存活)。无 ⇒ null。 */
77
- function advisorRunsRead(agent) {
78
- const m = carrierField(agent, "_advisorRuns")
79
- return m instanceof Map ? m : null
80
- }
81
-
82
- export function advisorRuns(agent) {
83
- // 实例注册表读取吸收(与 #94 载体口径同源):既有形态(字段挂 agent)零变;
84
- // 注册表已挂跨 run 载体的形态直接沿用(不另建分叉)。
85
- const existing = advisorRunsRead(agent)
86
- if (existing) return existing
87
- // 缺省创建:落在跨 run 载体上(有 history 时)——与其余载体字段同口径。
88
- const holder = agent?.history ?? agent
89
- if (!holder) return new Map()
90
- if (!(holder._advisorRuns instanceof Map)) holder._advisorRuns = new Map()
91
- return holder._advisorRuns
92
- }
93
-
94
- /** The newest OPEN code instance (the thread a fix-round launch continues), or null. */
95
- export function openCodeRun(agent) {
96
- const runs = advisorRunsRead(agent)
97
- if (!(runs instanceof Map) || runs.size === 0) return null
98
- for (const r of [...runs.values()].reverse()) {
99
- if (r.reviewType === "code" && r.open) return r
100
- }
101
- return null
102
- }
103
-
104
- /** The newest OPEN design instance for the given document set, or null. */
105
- function openDesignRun(agent, key) {
106
- const runs = advisorRunsRead(agent)
107
- if (!(runs instanceof Map) || runs.size === 0) return null
108
- for (const r of [...runs.values()].reverse()) {
109
- if (r.reviewType === "design" && r.open && r.docSetKey === key) return r
110
- }
111
- return null
112
- }
113
-
114
- /**
115
- * Resolve the review instance a launch continues (AGENT-LOOP-ASYNC-POOL.md §6.10 ③ — per-review
116
- * rounds/prior; reviewId = designId for design reviews, a random id for code).
117
- * - design: continue the OPEN instance of the same document set (fix rounds of
118
- * a review thread keep its designId and its round/prior); none → new instance.
119
- * - code: continue the newest OPEN code instance (guard-driven fix loops);
120
- * none → new instance.
121
- * The legacy agent._advisorRound/_lastAdvisorOutput fields are SCOPED to the
122
- * instance before the launch (message building + the run.mjs cap read them) —
123
- * prior-less continuation degrades to round 1 semantics (fresh full review).
124
- * @returns {{run: object, isNew: boolean, reviewId: string, designId: string|null}}
125
- */
126
- export function resolveAdvisorLaunch(agent, reviewType, { documents = null } = {}) {
127
- const runs = advisorRuns(agent)
128
- let run = null
129
- if (reviewType === "design") {
130
- const key = docSetKey(documents, agent.cwd)
131
- run = openDesignRun(agent, key)
132
- if (!run) {
133
- const reviewId = randomUUID()
134
- // F2h (§29.1 2026-09-07): a same-scope re-review reuses the session's
135
- // designId for this doc-set (any prior instance of the scope — open OR
136
- // closed — the newest wins). A passed re-review overwrites the slot under
137
- // the same id (no slot residue — T14: the old token is then rejected at
138
- // the gate); old slots die at TTL only, kept as the fail-safe fallback.
139
- const prior = [...runs.values()].reverse().find((r) => r.reviewType === "design" && r.docSetKey === key)
140
- // #863 实例回收(§6.10):同 docSetKey 保最新一条 closed(= prior——F2h 只取 newest,语义零变);
141
- // 余者去重回收(实例随删释放——closed 零消费面)。
142
- for (const r of [...runs.values()]) {
143
- if (r !== prior && r.reviewType === "design" && !r.open && r.docSetKey === key) runs.delete(r.reviewId)
144
- }
145
- run = {
146
- reviewId, reviewType, designId: prior?.designId ?? reviewId,
147
- round: 0, priorOutput: null, stale: false, open: true, docSetKey: key,
148
- }
149
- runs.set(reviewId, run)
150
- }
151
- } else {
152
- run = openCodeRun(agent)
153
- if (!run) {
154
- // #863 实例回收(§6.10):新 code 实例创建 ⇒ closed 实例清除(openCodeRun 仅取 open——零消费)。
155
- for (const r of [...runs.values()]) {
156
- if (r.reviewType === "code" && !r.open) runs.delete(r.reviewId)
157
- }
158
- const reviewId = randomUUID()
159
- run = {
160
- reviewId, reviewType, designId: null,
161
- round: 0, priorOutput: null, stale: false, open: true, docSetKey: null,
162
- }
163
- runs.set(reviewId, run)
164
- }
165
- }
166
- // Scope the mirror fields to THIS instance (message builder + run.mjs cap +
167
- // displays; #949 ① — the queued-launch window is re-secured at entry.start).
168
- scopeAdvisorMirror(agent, run)
169
- return { run, isNew: run.round === 0 && !run.priorOutput, reviewId: run.reviewId, designId: run.designId }
170
- }
171
-
172
- /** Guard round — the OPEN CODE instance only (2026-09-07 §8 F2): the design-written
173
- * mirror never gates the code guard — no open code instance → 0. */
174
- export function effectiveAdvisorRound(agent) {
175
- return openCodeRun(agent)?.round ?? 0
176
- }
177
-
178
- // ─────────────────────────────────────────────────────────────────────────────
179
- // Async pool (agent._asyncAdvisors — pool 上限 F-1/F-2:常量 = 运行时回退权威(2 → 4),
180
- // config.mjs DEFAULTS.agent.poolLimits.advisor = config 层镜像——耦合锁 config-pool
181
- // .test.mjs 逐键断言——勿单侧改默认)
182
- // ─────────────────────────────────────────────────────────────────────────────
183
-
184
- export const ADVISOR_POOL_LIMIT = 4
185
-
186
- /** AGENT-LOOP-ASYNC-POOL.md §6.10 advisor 池生效上限(纯——读 agent.poolLimits.advisor——合法 ≥1 整数生效——
187
- * 非法/缺省回退 ADVISOR_POOL_LIMIT——与 subagent 域 resolvePoolLimits 独立不共享
188
- * (subagent 两键表不含本键——键表语义不同——POOL-CONFIG-UNIFIED F-2))。 */
189
- export function resolveAdvisorPoolLimit(raw) {
190
- const v = raw?.advisor
191
- return Number.isInteger(v) && v >= 1 ? v : ADVISOR_POOL_LIMIT
192
- }
193
-
194
- /** launch 判定点每次读 agent.config(/config 热应用——变更即生效下个 launch——文案
195
- * 如实报本值)。 */
196
- export function advisorPoolLimitFor(agent) {
197
- return resolveAdvisorPoolLimit(agent?.config?.agent?.poolLimits)
198
- }
199
- /** §6.10 同 scope 守卫(F-5——用户裁①):同 reviewType+scope 有 running 评审 → true
200
- * (拒——running 并行多实例歧义放大——settled 续跑语义不变)。design scope = 文档集键
201
- * (docSetKey);code = 单 code 线程(记录不记路径键——与 openCodeRun 语义一致)。
202
- * 与池容量守卫独立:容量 = 全局 ≤N——scope = 同 scope ≤1——两关都过才启动。 */
203
- export function runningAdvisorOfScope(agent, reviewType, docSetKey) {
204
- const pool = getAsyncPool(agent, "advisor")
205
- if (!(pool instanceof Map)) return false
206
- for (const e of pool.values()) {
207
- if (e.status !== "running" || e.reviewType !== reviewType) continue
208
- if (reviewType === "design" && e.run?.docSetKey !== docSetKey) continue
209
- return true
210
- }
211
- return false
212
- }
213
-
214
- export function runningAdvisorCount(agent) {
215
- return [...(getAsyncPool(agent, "advisor")?.values() ?? [])].filter((e) => e.status === "running").length
216
- }
217
-
218
- /** Any in-flight (non-done) async advisor review? — the completion guard skips
219
- * its push-back while a review is pending (T-24b4: 未决不算未评审). */
220
- export function advisorReviewPending(agent) {
221
- return [...(getAsyncPool(agent, "advisor")?.values() ?? [])].some((e) => !e.done)
222
- }
223
-
224
76
  /** Directed cancel (ruling ②-6b — ⏹/cancel): abort the running review's
225
77
  * controller (run.mjs signal chain) → the settle callback runs the cancelled
226
78
  * branch (no pending entry / no token / "评审已取消——token 未签发" reminder).
@@ -367,7 +219,7 @@ export function refillAdvisorQueue(parent, onToken) {
367
219
  * or invalid ctx.
368
220
  */
369
221
  export function launchAsyncAdvisor(parent, ctx, launch) {
370
- const { reviewType, documents, paths, object, designToken, designId, run } = launch
222
+ const { reviewType, documents, paths, object, designToken, designId, run, batchDoc = null } = launch
371
223
  const limit = advisorPoolLimitFor(parent)
372
224
  // §6.10 same-scope guard(F-5——用户裁①):同 type+scope 有 running → 拒——与池容量
373
225
  // 守卫独立两关都过才启动——同 scope 不等不排(settled 续跑不变;排队仅异 scope 面)。
@@ -391,6 +243,10 @@ export function launchAsyncAdvisor(parent, ctx, launch) {
391
243
  const entry = {
392
244
  id, role: "advisor", reviewType, run,
393
245
  reviewId: run.reviewId, designId, designToken, documents, paths, object,
246
+ // 批次档绑定 = 逐笔快照(发起时点的值,与 reviewId / designToken 同族):排队条目若在启动时读活的
247
+ // `run.batchDoc`,读到的是后一笔发起覆盖后的值 ⇒ 评审把裁定写进别人的档(判据单源 =
248
+ // `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.15)。
249
+ batchDoc,
394
250
  launchSeq: mutationSeqOf(parent),
395
251
  // docAbs 产出面同源归一(2026-10-04 · #921 · A 随件):四分支单源 resolved(裸形/前缀形
396
252
  // 命中候选项目根即归一为绝对路径)——冻结窗/陈旧判定对**裸形受理档**照护成立(否则 A
@@ -431,6 +287,10 @@ export function launchAsyncAdvisor(parent, ctx, launch) {
431
287
  onOutput: (chunk) => relayAdvisorOutput(ctx?.callbacks, entry.relayPrefix, chunk),
432
288
  signal: entry.controller.signal,
433
289
  onStall: (rounds) => pushAdvisorAlert(parent, `advisor#${id}`, rounds), // #934:连续无产出越阈 ⇒ 自动上行提醒(只推——§6.32)
290
+ // 本笔发起时点的绑定快照(入口条目字段),与评审轨无关(判据单源 =
291
+ // `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.15)。不读活的 `run.batchDoc`:
292
+ // 排队条目启动时那个字段已被后一笔发起覆盖。
293
+ batchDoc: entry.batchDoc,
434
294
  }, designToken, documents, paths, object, designId)
435
295
  .then((report) => { entry.report = report })
436
296
  // §6.12 第 3 条合成器(第 24 批):原 message 前缀逐字保留 + 来源后缀
@@ -0,0 +1,178 @@
1
+ /**
2
+ * advisor-launch.mjs — 实例与池判定一族:哪条评审实例接这一轮、池让不让起。
3
+ *
4
+ * 2026-11-12 自 advisor-async.mjs 拆出(分拆方案 = `docs/core/design/DESIGN-TOKEN-SETTLEMENT.md`
5
+ * §10「分拆方案(`advisor-async.mjs`,本批内实施)」——该档过 500 设计器硬线,规则要求本批内拆完)。
6
+ * 切点 = 原档「Review-instance registry」分节起、`cancelAsyncAdvisor` 之前整段。
7
+ * 本档对**池与队列**只读(不碰队列、不碰控制器、不写池);实例注册表按既有语义懒建与回收,故
8
+ * `advisorRuns` ∥ `resolveAdvisorLaunch` 会写 `_advisorRuns` 与镜像字段。留下的族是「起 ∥ 停 ∥ 排 ∥ 关」写动作。
9
+ * 依赖方向单一:本档只依赖 `carrierField` ∥ `getAsyncPool`(async-settle.mjs)∥ `docSetKey` ∥
10
+ * `resolveReviewDocPaths` ∥ `scopeAdvisorMirror`(review-facts.mjs)∥ `randomUUID`(node:crypto);
11
+ * advisor-async.mjs 反向 import 本档的判定函数——两档之间是单向边。与 async-settle.mjs 之间
12
+ * 存在**既有的**一环(该档静态 import `refillAdvisorQueue`,本档 import `carrierField` ∥ `getAsyncPool`):
13
+ * 拆分前是 advisor-async ↔ async-settle 的一环,拆分后同一环多经本档一格——环数不变、本笔未新增环,
14
+ * 环上无模块级求值(两侧都只在函数体内调用)。
15
+ * 既有 import 面零改:原档 re-export 本档的全部既有导出(同 design-token.mjs ∥ advisor-settle.mjs 的先例)。
16
+ */
17
+ import { randomUUID } from "node:crypto"
18
+ import { docSetKey, resolveReviewDocPaths, scopeAdvisorMirror } from "./review-facts.mjs"
19
+ import { carrierField, getAsyncPool } from "./async-settle.mjs"
20
+
21
+ // ─────────────────────────────────────────────────────────────────────────────
22
+ // Review-instance registry (_advisorRuns — per-review rounds/prior/cap)
23
+ // ─────────────────────────────────────────────────────────────────────────────
24
+
25
+ /** 载体吸收后的只读读取(不创建):父对象字段优先、缺字段回退跨 run 载体
26
+ * (`history` 数组——agent 逐 run 重建的形态靠它跨 run 存活)。无 ⇒ null。
27
+ * 导出面:写动作一族(`closeOpenCodeAdvisorRuns`)要用它,故跨档可见;它不是原档的既有导出,
28
+ * 故原档的 re-export 名单**不含**它(import 面与拆分前逐字同)。 */
29
+ export function advisorRunsRead(agent) {
30
+ const m = carrierField(agent, "_advisorRuns")
31
+ return m instanceof Map ? m : null
32
+ }
33
+
34
+ export function advisorRuns(agent) {
35
+ // 实例注册表读取吸收(与 #94 载体口径同源):既有形态(字段挂 agent)零变;
36
+ // 注册表已挂跨 run 载体的形态直接沿用(不另建分叉)。
37
+ const existing = advisorRunsRead(agent)
38
+ if (existing) return existing
39
+ // 缺省创建:落在跨 run 载体上(有 history 时)——与其余载体字段同口径。
40
+ const holder = agent?.history ?? agent
41
+ if (!holder) return new Map()
42
+ if (!(holder._advisorRuns instanceof Map)) holder._advisorRuns = new Map()
43
+ return holder._advisorRuns
44
+ }
45
+
46
+ /** The newest OPEN code instance (the thread a fix-round launch continues), or null. */
47
+ export function openCodeRun(agent) {
48
+ const runs = advisorRunsRead(agent)
49
+ if (!(runs instanceof Map) || runs.size === 0) return null
50
+ for (const r of [...runs.values()].reverse()) {
51
+ if (r.reviewType === "code" && r.open) return r
52
+ }
53
+ return null
54
+ }
55
+
56
+ /** The newest OPEN design instance for the given document set, or null. */
57
+ function openDesignRun(agent, key) {
58
+ const runs = advisorRunsRead(agent)
59
+ if (!(runs instanceof Map) || runs.size === 0) return null
60
+ for (const r of [...runs.values()].reverse()) {
61
+ if (r.reviewType === "design" && r.open && r.docSetKey === key) return r
62
+ }
63
+ return null
64
+ }
65
+
66
+ /**
67
+ * Resolve the review instance a launch continues (AGENT-LOOP-ASYNC-POOL.md §6.10 ③ — per-review
68
+ * rounds/prior; reviewId = designId for design reviews, a random id for code).
69
+ * - design: continue the OPEN instance of the same document set (fix rounds of
70
+ * a review thread keep its designId and its round/prior); none → new instance.
71
+ * - code: continue the newest OPEN code instance (guard-driven fix loops);
72
+ * none → new instance.
73
+ * The legacy agent._advisorRound/_lastAdvisorOutput fields are SCOPED to the
74
+ * instance before the launch (message building + the run.mjs cap read them) —
75
+ * prior-less continuation degrades to round 1 semantics (fresh full review).
76
+ * @returns {{run: object, isNew: boolean, reviewId: string, designId: string|null}}
77
+ */
78
+ export function resolveAdvisorLaunch(agent, reviewType, { documents = null } = {}) {
79
+ const runs = advisorRuns(agent)
80
+ let run = null
81
+ if (reviewType === "design") {
82
+ const key = docSetKey(documents, agent.cwd)
83
+ run = openDesignRun(agent, key)
84
+ if (!run) {
85
+ const reviewId = randomUUID()
86
+ // F2h (§29.1 2026-09-07): a same-scope re-review reuses the session's
87
+ // designId for this doc-set (any prior instance of the scope — open OR
88
+ // closed — the newest wins). A passed re-review overwrites the slot under
89
+ // the same id (no slot residue — T14: the old token is then rejected at
90
+ // the gate); old slots die at TTL only, kept as the fail-safe fallback.
91
+ const prior = [...runs.values()].reverse().find((r) => r.reviewType === "design" && r.docSetKey === key)
92
+ // #863 实例回收(§6.10):同 docSetKey 保最新一条 closed(= prior——F2h 只取 newest,语义零变);
93
+ // 余者去重回收(实例随删释放——closed 零消费面)。
94
+ for (const r of [...runs.values()]) {
95
+ if (r !== prior && r.reviewType === "design" && !r.open && r.docSetKey === key) runs.delete(r.reviewId)
96
+ }
97
+ run = {
98
+ reviewId, reviewType, designId: prior?.designId ?? reviewId,
99
+ round: 0, priorOutput: null, stale: false, open: true, docSetKey: key,
100
+ // 被审文档集的**受理绝对路径**(单源 = `resolveReviewDocPaths`——与受理面同一把尺):
101
+ // §10.2 封印按这一组算。`docSetKey` 是实例键(`normAbs` 的 cwd 拼法),cwd 非项目根时
102
+ // 它与受理路径不是同一组——拿实例键当封印清单会把封印盖到另一条路径上。
103
+ docAbs: [...resolveReviewDocPaths(documents, agent.cwd).resolved.values()],
104
+ }
105
+ runs.set(reviewId, run)
106
+ }
107
+ } else {
108
+ run = openCodeRun(agent)
109
+ if (!run) {
110
+ // #863 实例回收(§6.10):新 code 实例创建 ⇒ closed 实例清除(openCodeRun 仅取 open——零消费)。
111
+ for (const r of [...runs.values()]) {
112
+ if (r.reviewType === "code" && !r.open) runs.delete(r.reviewId)
113
+ }
114
+ const reviewId = randomUUID()
115
+ run = {
116
+ reviewId, reviewType, designId: null,
117
+ round: 0, priorOutput: null, stale: false, open: true, docSetKey: null,
118
+ }
119
+ runs.set(reviewId, run)
120
+ }
121
+ }
122
+ // Scope the mirror fields to THIS instance (message builder + run.mjs cap +
123
+ // displays; #949 ① — the queued-launch window is re-secured at entry.start).
124
+ scopeAdvisorMirror(agent, run)
125
+ return { run, isNew: run.round === 0 && !run.priorOutput, reviewId: run.reviewId, designId: run.designId }
126
+ }
127
+
128
+ /** Guard round — the OPEN CODE instance only (2026-09-07 §8 F2): the design-written
129
+ * mirror never gates the code guard — no open code instance → 0. */
130
+ export function effectiveAdvisorRound(agent) {
131
+ return openCodeRun(agent)?.round ?? 0
132
+ }
133
+
134
+ // ─────────────────────────────────────────────────────────────────────────────
135
+ // Async pool (agent._asyncAdvisors — pool 上限 F-1/F-2:常量 = 运行时回退权威(2 → 4),
136
+ // config.mjs DEFAULTS.agent.poolLimits.advisor = config 层镜像——耦合锁 config-pool
137
+ // .test.mjs 逐键断言——勿单侧改默认)
138
+ // ─────────────────────────────────────────────────────────────────────────────
139
+
140
+ export const ADVISOR_POOL_LIMIT = 4
141
+
142
+ /** AGENT-LOOP-ASYNC-POOL.md §6.10 advisor 池生效上限(纯——读 agent.poolLimits.advisor——合法 ≥1 整数生效——
143
+ * 非法/缺省回退 ADVISOR_POOL_LIMIT——与 subagent 域 resolvePoolLimits 独立不共享
144
+ * (subagent 两键表不含本键——键表语义不同——POOL-CONFIG-UNIFIED F-2))。 */
145
+ export function resolveAdvisorPoolLimit(raw) {
146
+ const v = raw?.advisor
147
+ return Number.isInteger(v) && v >= 1 ? v : ADVISOR_POOL_LIMIT
148
+ }
149
+
150
+ /** launch 判定点每次读 agent.config(/config 热应用——变更即生效下个 launch——文案
151
+ * 如实报本值)。 */
152
+ export function advisorPoolLimitFor(agent) {
153
+ return resolveAdvisorPoolLimit(agent?.config?.agent?.poolLimits)
154
+ }
155
+ /** §6.10 同 scope 守卫(F-5——用户裁①):同 reviewType+scope 有 running 评审 → true
156
+ * (拒——running 并行多实例歧义放大——settled 续跑语义不变)。design scope = 文档集键
157
+ * (docSetKey);code = 单 code 线程(记录不记路径键——与 openCodeRun 语义一致)。
158
+ * 与池容量守卫独立:容量 = 全局 ≤N——scope = 同 scope ≤1——两关都过才启动。 */
159
+ export function runningAdvisorOfScope(agent, reviewType, docSetKey) {
160
+ const pool = getAsyncPool(agent, "advisor")
161
+ if (!(pool instanceof Map)) return false
162
+ for (const e of pool.values()) {
163
+ if (e.status !== "running" || e.reviewType !== reviewType) continue
164
+ if (reviewType === "design" && e.run?.docSetKey !== docSetKey) continue
165
+ return true
166
+ }
167
+ return false
168
+ }
169
+
170
+ export function runningAdvisorCount(agent) {
171
+ return [...(getAsyncPool(agent, "advisor")?.values() ?? [])].filter((e) => e.status === "running").length
172
+ }
173
+
174
+ /** Any in-flight (non-done) async advisor review? — the completion guard skips
175
+ * its push-back while a review is pending (T-24b4: 未决不算未评审). */
176
+ export function advisorReviewPending(agent) {
177
+ return [...(getAsyncPool(agent, "advisor")?.values() ?? [])].some((e) => !e.done)
178
+ }
@@ -112,6 +112,9 @@ export function inflightDesignReviewConflict(agent, absPaths) {
112
112
  * mirror retired per DESIGN-TOKEN-SETTLEMENT D3) + D1 settle-time slot persist
113
113
  * (persistEngTokens — write failure = settle failure: no registration, no
114
114
  * Approved echo, re-review — 评审 #1);
115
+ * **轮次块与状态行同成一笔,再与落盘同成败**(先写块、后落盘;落盘失败 ⇒ 档字节回写写前原文
116
+ * ⇒ 块与状态行一并撤销 + 槽 Map 回 `preMap`)
117
+ * —— 一笔的范围、顺序与回退 = `docs/core/design/BATCH-RECORD.md` §4.23。
115
118
  * 4. fix B — branch-shaped report output (the caller writes it back to
116
119
  * entry.report; digest injects the CLEANED form): pass → stripped +
117
120
  * Approved/designId suffix (sync 参照形态); persist-failed → stripped +
@@ -147,6 +150,10 @@ export function settleAdvisorRun(agent, entry) {
147
150
  const launchRefused = result != null && String(result).startsWith(ADVISOR_LAUNCH_REFUSAL_PREFIX)
148
151
  if (!stale) {
149
152
  if (run.reviewType === "design" && entry.designToken && result) {
153
+ // 结算面只认本笔快照(判据单源 = `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.15):`run` 是
154
+ // 实例级对象,排队窗口里 `batchDoc` 被同一实例的后一笔发起覆盖过 ⇒ 不写回的话,本轮块会落进别人的档、
155
+ // token 归属也落到别人的档。
156
+ run.batchDoc = entry.batchDoc
150
157
  // settle 前 Map 快照——落盘失败时回滚用(settle 失败 = 结算未发生,不留半结算态:
151
158
  // 重评覆盖旧槽的边角(F2h 复用 designId)也原样恢复旧 token——内存与盘一致)。
152
159
  const preMap = agent._engDesignTokens instanceof Map
@@ -175,9 +182,25 @@ export function settleAdvisorRun(agent, entry) {
175
182
  else delete agent._engDesignTokens
176
183
  run.approvedSuffix = null
177
184
  persistFailed = true
185
+ // §4.23 ③(回退两处):档字节写回**写前原文**——只回退 Map 会在档上留一块没有 token 的轮次块,
186
+ // 下一轮读 §3 以为本轮已评过(轮次计数也虚增一块)。回退写也失败 ⇒ 两处并报、不静默
187
+ // (token 才是 spawn 的凭据,残留的块不授予假授权)。
188
+ // 范围如实 = **两处**(档字节 ∥ 槽 Map):`settleDesignReview` 同拍翻的实例态(`run.open` ∥
189
+ // `priorOutput`)不回退 ⇒ 失败后重跑是全新一轮评审,不是同一实例重评(同档 §4.23 ③ 的两处口径)。
190
+ let rollbackErr = null
191
+ if (settled.sign) {
192
+ try {
193
+ settled.sign.rollback()
194
+ } catch (e) {
195
+ rollbackErr = e?.message ?? String(e)
196
+ }
197
+ }
178
198
  logEvent("advisor:error", { id: `advisor#${entry.id}`, err: "engDesignTokens slot persist failed — settle failed (re-review)" })
179
199
  const stripped = stripDesignTokenEcho(String(result), entry.designToken).trim()
180
- 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()
200
+ const rollbackNote = rollbackErr
201
+ ? `\n\n并报:块回写也失败了——批档 §3 上留着一条没有 token 的评审记录 (the block rollback failed too; the record keeps a round block that grants no authorization; reason: ${rollbackErr})。直接重跑评审即可。`
202
+ : ""
203
+ 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 未能持久化——需重评;整笔未签发)。${rollbackNote}`.trim()
181
204
  }
182
205
  } else {
183
206
  report = settled.output
@@ -205,6 +228,10 @@ export function settleAdvisorRun(agent, entry) {
205
228
  )) || launchRefused
206
229
  if (!failureVerdict) {
207
230
  agent._calledAdvisorThisRun = true
231
+ // ADVISOR-CONVERGENCE.md §6.4 B 复位点 2(异步面):非陈旧且非失败结算 ⇒ 评审真的覆盖了这份代码
232
+ // ⇒ 本未评审周期结束,下一周期起于完整预算。
233
+ agent._advisorReminders = 0
234
+ agent._advisorHonestySent = false
208
235
  }
209
236
  } else if (run.reviewType === "design" && entry.designToken && result != null) {
210
237
  // fix B(stale 分支):陈旧评审不签发——digest 不得展示未注册 token——先剥