@thincoder/core 0.10.13 → 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 (366) hide show
  1. package/CHANGELOG.md +74 -1
  2. package/advisor/convergence.mjs +34 -3
  3. package/advisor/loop.mjs +42 -10
  4. package/advisor/messages.mjs +46 -27
  5. package/advisor/notice.mjs +1 -1
  6. package/advisor/project-context.mjs +253 -14
  7. package/advisor/run.mjs +7 -4
  8. package/advisor.mjs +29 -43
  9. package/agent/batch-attribution.mjs +87 -0
  10. package/agent/batch-changed-surface.mjs +129 -0
  11. package/agent/completion.mjs +57 -4
  12. package/agent/digest-account.mjs +73 -46
  13. package/agent/dispatch-gates.mjs +6 -0
  14. package/agent/dispatch-run.mjs +1 -1
  15. package/agent/dispatch.mjs +35 -22
  16. package/agent/family-tools.mjs +36 -17
  17. package/agent/helpers.mjs +123 -95
  18. package/agent/offload.mjs +84 -0
  19. package/agent/post-turn.mjs +24 -10
  20. package/agent/record-results.mjs +19 -7
  21. package/agent/run-stages.mjs +127 -61
  22. package/agent/run-start.mjs +45 -8
  23. package/agent/setup-reminders.mjs +9 -2
  24. package/agent/setup.mjs +27 -22
  25. package/agent/spawn-child.mjs +38 -1
  26. package/agent/suspension.mjs +1 -1
  27. package/agent/timers.mjs +5 -3
  28. package/agent/turn-loop.mjs +60 -6
  29. package/agent/turn-projection.mjs +386 -0
  30. package/agent/turn-task-state.mjs +274 -0
  31. package/agent/turn-tool-process.mjs +168 -0
  32. package/agent/write-face.mjs +99 -0
  33. package/agent/write-gate-ledger.mjs +569 -0
  34. package/agent/write-gate.mjs +39 -13
  35. package/agent-tools/advisor-async.mjs +29 -169
  36. package/agent-tools/advisor-launch.mjs +178 -0
  37. package/agent-tools/advisor-settle.mjs +28 -1
  38. package/agent-tools/advisor.mjs +136 -31
  39. package/agent-tools/async-settle.mjs +2 -2
  40. package/agent-tools/audit-block.mjs +6 -6
  41. package/agent-tools/bash-async.mjs +8 -2
  42. package/agent-tools/batch-closeout.mjs +444 -0
  43. package/agent-tools/batch-gate.mjs +152 -138
  44. package/agent-tools/batch-ledger-bind.mjs +392 -0
  45. package/agent-tools/batch-ledger-link.mjs +73 -0
  46. package/agent-tools/batch-ledger-view.mjs +109 -0
  47. package/agent-tools/batch-lifecycle.mjs +245 -376
  48. package/agent-tools/batch-paths.mjs +73 -47
  49. package/agent-tools/batch-read.mjs +1 -1
  50. package/agent-tools/batch-record-write.mjs +417 -0
  51. package/agent-tools/batch-regions.mjs +215 -0
  52. package/agent-tools/batch-reopen.mjs +154 -0
  53. package/agent-tools/batch-segment-vocab.mjs +379 -0
  54. package/agent-tools/batch-skeleton.mjs +344 -196
  55. package/agent-tools/batch-supersede.mjs +232 -0
  56. package/agent-tools/batch-tier-gate.mjs +297 -0
  57. package/agent-tools/batch.mjs +189 -253
  58. package/agent-tools/consult.mjs +7 -4
  59. package/agent-tools/context.mjs +47 -17
  60. package/agent-tools/design-review-record.mjs +225 -0
  61. package/agent-tools/design-slots.mjs +24 -24
  62. package/agent-tools/design-token.mjs +318 -39
  63. package/agent-tools/doc-check.mjs +55 -29
  64. package/agent-tools/doc-graph.mjs +140 -0
  65. package/agent-tools/eng.mjs +10 -9
  66. package/agent-tools/escalate-async.mjs +5 -5
  67. package/agent-tools/manifest.mjs +34 -15
  68. package/agent-tools/read-history.mjs +115 -14
  69. package/agent-tools/review-facts.mjs +40 -15
  70. package/agent-tools/settings.mjs +23 -2
  71. package/agent-tools/spawn-gates.mjs +19 -22
  72. package/agent-tools/ssh-session.mjs +120 -111
  73. package/agent-tools/ssh-text.mjs +155 -0
  74. package/agent-tools/ssh-transfer.mjs +210 -0
  75. package/agent-tools/ssh-transport.mjs +183 -81
  76. package/agent-tools/subagent-actions-query.mjs +11 -7
  77. package/agent-tools/subagent-actions.mjs +1 -1
  78. package/agent-tools/subagent-async.mjs +11 -11
  79. package/agent-tools/subagent-brief.mjs +76 -0
  80. package/agent-tools/subagent-design-token.mjs +168 -0
  81. package/agent-tools/subagent-run.mjs +1 -1
  82. package/agent-tools/subagent-scheduler.mjs +45 -11
  83. package/agent-tools/subagent-spawn.mjs +143 -104
  84. package/agent-tools/subagent.mjs +26 -23
  85. package/agent-tools/taskbook-block.mjs +94 -0
  86. package/agent-tools/timer.mjs +12 -8
  87. package/agent-tools/verify.mjs +13 -4
  88. package/agent-tools.mjs +7 -3
  89. package/agent.mjs +20 -1
  90. package/attachments.mjs +3 -3
  91. package/browser/actions.mjs +9 -3
  92. package/browser/cdp.mjs +45 -17
  93. package/browser/launch.mjs +25 -43
  94. package/browser/pool.mjs +299 -0
  95. package/browser/queue.mjs +2 -0
  96. package/browser/session-state.mjs +450 -0
  97. package/browser/session.mjs +127 -151
  98. package/browser/snapshot.mjs +89 -0
  99. package/client-identity.mjs +22 -6
  100. package/config-defaults.mjs +2 -1
  101. package/config-migrate.mjs +40 -0
  102. package/config-normalize.mjs +28 -2
  103. package/config-presets.mjs +5 -3
  104. package/config-reload.mjs +59 -0
  105. package/config.mjs +62 -98
  106. package/context-degrade.mjs +38 -10
  107. package/context-echo.mjs +11 -0
  108. package/context.mjs +165 -68
  109. package/conventions.mjs +11 -9
  110. package/declaration.mjs +33 -7
  111. package/declared-repos.mjs +94 -0
  112. package/doc-check/doc-names.mjs +73 -0
  113. package/doc-check/doc-tokens.mjs +110 -0
  114. package/doc-check/existence.mjs +160 -0
  115. package/doc-check/index.mjs +312 -163
  116. package/doc-check/line-refs.mjs +38 -0
  117. package/doc-check/paragraph.mjs +220 -91
  118. package/doc-graph/anchors.mjs +474 -0
  119. package/doc-graph/doc-batch-face.mjs +59 -0
  120. package/doc-graph/doc-suffix.mjs +27 -0
  121. package/doc-graph/graph-tokens.mjs +160 -0
  122. package/doc-graph.mjs +778 -0
  123. package/explore-distill.mjs +12 -6
  124. package/i18n.mjs +2 -2
  125. package/index-discover.mjs +4 -1
  126. package/instance-inbox-watch.mjs +272 -0
  127. package/instance-inbox.mjs +218 -0
  128. package/instance-registry.mjs +179 -0
  129. package/knowledge/README.md +3 -0
  130. package/knowledge/prompt-resources.md +6 -0
  131. package/ledger-close-proof.mjs +95 -62
  132. package/ledger-close.mjs +188 -0
  133. package/ledger-cmd.mjs +76 -244
  134. package/ledger-consistency.mjs +113 -0
  135. package/ledger-db.mjs +70 -37
  136. package/ledger-executors.mjs +1 -1
  137. package/ledger-migrate.mjs +4 -4
  138. package/ledger-read.mjs +10 -8
  139. package/ledger-surface.mjs +1 -1
  140. package/ledger-tools.mjs +83 -24
  141. package/ledger-variant-notice.mjs +1 -1
  142. package/ledger-write-ops.mjs +462 -0
  143. package/ledger.mjs +2 -2
  144. package/manifest-discovery.mjs +5 -3
  145. package/manifest-schema.mjs +28 -2
  146. package/memory/memory-tool.mjs +5 -2
  147. package/memory/schema.mjs +6 -27
  148. package/package.json +3 -1
  149. package/path-resolve.mjs +498 -0
  150. package/permission.mjs +4 -0
  151. package/prompt-files.mjs +91 -1
  152. package/prompt-overlays.mjs +14 -6
  153. package/prompts/advisor-design.md +66 -22
  154. package/prompts/advisor-round1.md +37 -3
  155. package/prompts/advisor-round2.md +44 -10
  156. package/prompts/advisor-round3.md +43 -9
  157. package/prompts/common.md +15 -375
  158. package/prompts/consult-base.md +6 -1
  159. package/prompts/discipline-engineering.md +251 -95
  160. package/prompts/discipline-normal.md +4 -250
  161. package/prompts/knowledge/business-gates.md +22 -0
  162. package/prompts/knowledge/decision-quality.md +23 -0
  163. package/prompts/knowledge/doc-lookup.md +35 -0
  164. package/prompts/knowledge/document-writing.md +51 -0
  165. package/prompts/knowledge/documentation-evaluation.md +56 -0
  166. package/prompts/knowledge/evidence-discipline.md +57 -0
  167. package/prompts/knowledge/normal-coding-workflow.md +104 -0
  168. package/prompts/knowledge/normal-delivery-collaboration.md +108 -0
  169. package/prompts/knowledge/normal-work-management.md +57 -0
  170. package/prompts/knowledge/product-security.md +23 -0
  171. package/prompts/knowledge/project-records.md +62 -0
  172. package/prompts/knowledge/tool-advisor.md +29 -0
  173. package/prompts/knowledge/tool-apply-patch.md +18 -0
  174. package/prompts/knowledge/tool-bash.md +39 -0
  175. package/prompts/knowledge/tool-batch-eng-coder.md +12 -0
  176. package/prompts/knowledge/tool-batch-eng-designer.md +12 -0
  177. package/prompts/knowledge/tool-batch-review.md +12 -0
  178. package/prompts/knowledge/tool-batch.md +80 -0
  179. package/prompts/knowledge/tool-browser.md +57 -0
  180. package/prompts/knowledge/tool-code-search.md +4 -0
  181. package/prompts/knowledge/tool-consult-start.md +12 -0
  182. package/prompts/knowledge/tool-consult-stop.md +6 -0
  183. package/prompts/knowledge/tool-context.md +6 -0
  184. package/prompts/knowledge/tool-delete.md +17 -0
  185. package/prompts/knowledge/tool-doc-check.md +70 -0
  186. package/prompts/knowledge/tool-doc-graph-read.md +20 -0
  187. package/prompts/knowledge/tool-doc-graph.md +49 -0
  188. package/prompts/knowledge/tool-doc-search.md +6 -0
  189. package/prompts/knowledge/tool-edit.md +38 -0
  190. package/prompts/knowledge/tool-eng.md +2 -0
  191. package/prompts/knowledge/tool-execute.md +16 -0
  192. package/prompts/knowledge/tool-fetch.md +12 -0
  193. package/prompts/knowledge/tool-file-ops.md +16 -0
  194. package/prompts/knowledge/tool-get-current-time.md +7 -0
  195. package/prompts/knowledge/tool-git-read.md +19 -0
  196. package/prompts/knowledge/tool-git.md +29 -0
  197. package/prompts/knowledge/tool-glob.md +15 -0
  198. package/prompts/knowledge/tool-goal.md +8 -0
  199. package/prompts/knowledge/tool-grep.md +18 -0
  200. package/prompts/knowledge/tool-hashline-edit.md +14 -0
  201. package/prompts/knowledge/tool-insert-after.md +15 -0
  202. package/prompts/knowledge/tool-instance-list.md +6 -0
  203. package/prompts/knowledge/tool-instance-send.md +10 -0
  204. package/prompts/knowledge/tool-knowledge.md +18 -0
  205. package/prompts/knowledge/tool-ledger-read.md +35 -0
  206. package/prompts/knowledge/tool-ledger.md +42 -0
  207. package/prompts/knowledge/tool-lint.md +16 -0
  208. package/prompts/knowledge/tool-ls.md +13 -0
  209. package/prompts/knowledge/tool-lsp.md +11 -0
  210. package/prompts/knowledge/tool-main-history.md +2 -0
  211. package/prompts/knowledge/tool-manifest-read.md +14 -0
  212. package/prompts/knowledge/tool-manifest.md +31 -0
  213. package/prompts/knowledge/tool-memory-read.md +20 -0
  214. package/prompts/knowledge/tool-memory.md +15 -0
  215. package/prompts/knowledge/tool-notify-parent.md +12 -0
  216. package/prompts/knowledge/tool-operations.md +114 -0
  217. package/prompts/knowledge/tool-peer-instances.md +2 -0
  218. package/prompts/knowledge/tool-plan.md +6 -0
  219. package/prompts/knowledge/tool-process-read.md +13 -0
  220. package/prompts/knowledge/tool-process.md +15 -0
  221. package/prompts/knowledge/tool-question.md +16 -0
  222. package/prompts/knowledge/tool-read-history-batch.md +22 -0
  223. package/prompts/knowledge/tool-read-history.md +36 -0
  224. package/prompts/knowledge/tool-read-image.md +11 -0
  225. package/prompts/knowledge/tool-read.md +21 -0
  226. package/prompts/knowledge/tool-recent-changes.md +6 -0
  227. package/prompts/knowledge/tool-repo-outline.md +4 -0
  228. package/prompts/knowledge/tool-settings-read.md +12 -0
  229. package/prompts/knowledge/tool-settings.md +18 -0
  230. package/prompts/knowledge/tool-skill.md +6 -0
  231. package/prompts/knowledge/tool-ssh.md +52 -0
  232. package/prompts/knowledge/tool-subagent.md +59 -0
  233. package/prompts/knowledge/tool-task.md +12 -0
  234. package/prompts/knowledge/tool-team-ledger-read.md +11 -0
  235. package/prompts/knowledge/tool-team-ledger.md +11 -0
  236. package/prompts/knowledge/tool-timer.md +23 -0
  237. package/prompts/knowledge/tool-tree.md +14 -0
  238. package/prompts/knowledge/tool-verify.md +19 -0
  239. package/prompts/knowledge/tool-wait-for.md +25 -0
  240. package/prompts/knowledge/tool-websearch.md +19 -0
  241. package/prompts/knowledge/tool-write.md +9 -0
  242. package/prompts/knowledge/upstream-protocol.md +43 -0
  243. package/prompts/persona-eng-coder.md +29 -3
  244. package/prompts/persona-eng-designer.md +131 -159
  245. package/prompts/persona-engineering.md +202 -131
  246. package/prompts/persona-explore.md +1 -1
  247. package/prompts/persona-plan.md +6 -1
  248. package/prompts/persona-tester.md +55 -0
  249. package/prompts/subagent-base.md +2 -0
  250. package/provider/anthropic.mjs +6 -4
  251. package/provider/core.mjs +5 -4
  252. package/provider/errors.mjs +29 -4
  253. package/provider/google.mjs +39 -12
  254. package/provider/rate.mjs +11 -3
  255. package/provider/responses.mjs +3 -1
  256. package/provider/sse.mjs +15 -4
  257. package/provider-flows.mjs +2 -2
  258. package/session-identity.mjs +21 -0
  259. package/session-index-build.mjs +1 -1
  260. package/session-index-pass.mjs +1 -1
  261. package/session-index-query.mjs +1 -1
  262. package/session-index.mjs +1 -1
  263. package/session-lifecycle.mjs +79 -19
  264. package/session-slot-write.mjs +37 -5
  265. package/session-slots.mjs +9 -14
  266. package/session.mjs +29 -8
  267. package/skip-dirs.mjs +48 -0
  268. package/team-ledger.mjs +17 -13
  269. package/text-budget.mjs +4 -0
  270. package/token-ttl.mjs +73 -4
  271. package/token-window.mjs +244 -34
  272. package/tool-docs/advisor.md +2 -27
  273. package/tool-docs/apply_patch.md +1 -13
  274. package/tool-docs/bash.md +2 -37
  275. package/tool-docs/batch-eng-coder.md +3 -0
  276. package/tool-docs/batch-eng-designer.md +3 -0
  277. package/tool-docs/batch-review.md +3 -0
  278. package/tool-docs/batch.md +3 -1
  279. package/tool-docs/browser.md +2 -47
  280. package/tool-docs/code_search.md +3 -1
  281. package/tool-docs/consult_start.md +2 -4
  282. package/tool-docs/consult_stop.md +2 -2
  283. package/tool-docs/context.md +1 -5
  284. package/tool-docs/delete.md +2 -14
  285. package/tool-docs/doc_check.md +2 -72
  286. package/tool-docs/doc_graph-read.md +3 -0
  287. package/tool-docs/doc_graph.md +3 -0
  288. package/tool-docs/doc_search.md +3 -1
  289. package/tool-docs/edit.md +2 -18
  290. package/tool-docs/eng.md +3 -1
  291. package/tool-docs/execute.md +2 -12
  292. package/tool-docs/fetch.md +2 -12
  293. package/tool-docs/file_ops.md +2 -16
  294. package/tool-docs/get_current_time.md +1 -6
  295. package/tool-docs/git-read.md +3 -0
  296. package/tool-docs/git.md +2 -27
  297. package/tool-docs/glob.md +2 -10
  298. package/tool-docs/goal.md +2 -5
  299. package/tool-docs/grep.md +2 -18
  300. package/tool-docs/hashline_edit.md +2 -13
  301. package/tool-docs/insert_after.md +2 -14
  302. package/tool-docs/instance_list.md +3 -0
  303. package/tool-docs/instance_send.md +3 -0
  304. package/tool-docs/knowledge.md +3 -0
  305. package/tool-docs/ledger-read.md +3 -0
  306. package/tool-docs/ledger.md +3 -8
  307. package/tool-docs/lint.md +2 -9
  308. package/tool-docs/ls.md +2 -11
  309. package/tool-docs/lsp.md +2 -9
  310. package/tool-docs/main_history.md +3 -1
  311. package/tool-docs/manifest-read.md +2 -7
  312. package/tool-docs/manifest.md +2 -28
  313. package/tool-docs/memory-read.md +3 -0
  314. package/tool-docs/memory.md +3 -11
  315. package/tool-docs/notify_parent.md +2 -13
  316. package/tool-docs/peer_instances.md +3 -1
  317. package/tool-docs/plan.md +2 -4
  318. package/tool-docs/process-read.md +3 -0
  319. package/tool-docs/process.md +2 -13
  320. package/tool-docs/question.md +3 -17
  321. package/tool-docs/read.md +2 -19
  322. package/tool-docs/read_history-batch.md +3 -0
  323. package/tool-docs/read_history.md +2 -6
  324. package/tool-docs/read_image.md +2 -7
  325. package/tool-docs/recent_changes.md +3 -1
  326. package/tool-docs/repo_outline.md +3 -1
  327. package/tool-docs/settings-read.md +3 -0
  328. package/tool-docs/settings.md +2 -5
  329. package/tool-docs/skill.md +3 -5
  330. package/tool-docs/ssh.md +2 -50
  331. package/tool-docs/subagent.md +2 -63
  332. package/tool-docs/task.md +2 -6
  333. package/tool-docs/team_ledger-read.md +3 -0
  334. package/tool-docs/team_ledger.md +3 -9
  335. package/tool-docs/timer.md +3 -13
  336. package/tool-docs/tree.md +2 -13
  337. package/tool-docs/verify.md +3 -1
  338. package/tool-docs/wait_for.md +2 -32
  339. package/tool-docs/websearch.md +2 -17
  340. package/tool-docs/write.md +2 -10
  341. package/tools/bash.mjs +12 -2
  342. package/tools/browser.mjs +52 -7
  343. package/tools/doc-write-gate.mjs +73 -0
  344. package/tools/edit-batch.mjs +176 -91
  345. package/tools/edit-diff.mjs +129 -45
  346. package/tools/edit-receipt.mjs +39 -0
  347. package/tools/execute.mjs +11 -5
  348. package/tools/file.mjs +32 -34
  349. package/tools/git.mjs +43 -2
  350. package/tools/index.mjs +6 -0
  351. package/tools/knowledge.mjs +143 -0
  352. package/tools/linter.mjs +3 -3
  353. package/tools/ops.mjs +4 -1
  354. package/tools/patch.mjs +127 -38
  355. package/tools/process-doc-mark.mjs +139 -0
  356. package/tools/search.mjs +15 -6
  357. package/tools/shared.mjs +31 -7
  358. package/tools/ssh.mjs +48 -7
  359. package/tools/tree.mjs +4 -1
  360. package/tools/write-gate-params.mjs +31 -0
  361. package/tools/write-path.mjs +79 -6
  362. package/traces/trace-store.mjs +4 -3
  363. package/window-policy.mjs +203 -0
  364. package/agent/light-round.mjs +0 -63
  365. package/agent/unbooked-pen.mjs +0 -62
  366. package/doc-check/anchors.mjs +0 -135
@@ -13,13 +13,19 @@
13
13
  * dimension it could no longer check (silent failure, the worst class: invisible
14
14
  * and unfixable by the user). Missing pieces now say so, and the reviewer is told
15
15
  * to state the limitation in its findings. The document map is derived from the
16
- * manifest's `docRoot` (the one place that says where the docs live); the standards
17
- * face has no derivable convention name and is therefore an explicit degradation.
16
+ * manifest's `docRoot` (the one place that says where the docs live); the standards face
17
+ * is the project's OWN declaration too (`docRoot.standards` — a document LAYER: a directory
18
+ * whose `.md` files, subdirectories included, are the standards) — the probe was retired, the declaration
19
+ * replaced guessing — and it degrades in three machine-distinguishable states:
20
+ * undeclared (no path), declared but unreadable (the declared path is named), and injected.
18
21
  */
19
- import { readFileSync, existsSync } from "node:fs"
22
+ import { readFileSync, readdirSync, existsSync, statSync } from "node:fs"
20
23
  import { resolve, join, relative, dirname, sep } from "node:path"
21
24
  import { providerSpec } from "../config.mjs"
22
- import { DEFAULT_MANIFEST, docRootPaths, readManifest } from "../manifest.mjs"
25
+ import { DEFAULT_MANIFEST, docRootPaths, manifestFilePath, projectRootView, readManifest } from "../manifest.mjs"
26
+ // 去重键的折大小写尺子(本档私有):同一目录的两种大小写写法在 win32 上是同一个目录,
27
+ // 不折就同一份档注入两次。
28
+ const pathKey = (p) => (process.platform === "win32" ? p.toLowerCase() : p)
23
29
 
24
30
  /** Project guide (AGENTS.md) injection budget — decision 2026-08-08:
25
31
  * NO fixed truncation; long-context models (1M+) get up to 5% of their context
@@ -37,6 +43,31 @@ export const NO_DOC_MAP_NOTICE = "(No document map found — no README.md under
37
43
  export const NO_STANDARDS_NOTICE = "(No project standards document — nothing in the project declares one — judge methodology compliance from the Project Guide (when present) and the review criteria above; state the limitation in your findings.)"
38
44
  export const NO_GIT_NOTICE = "(No git repository detected — change-set context is unavailable; read the review-scope files directly.)"
39
45
 
46
+ /**
47
+ * 态 2 —— 声明了标准层,但一份都读不到(文案单源 = `docs/core/design/MANIFEST.md` §2.12)。
48
+ * 与 `NO_STANDARDS_NOTICE` **不得同形**:那句说的是「没有任何东西声明」,而这里恰好是「声明了但
49
+ * 读不到」。同形会让作者以为自己没声明过,回头去补一个其实已经有的声明——所以本句必带声明路径;
50
+ * `reason` 五值分列,对应三类可修面(KD-M1-65):`no such directory` 改路径 ∥ `not a directory`
51
+ * 改形态 ∥ `no .md file` 往层里放档;`read failed: <code>` ∥ `manifest read failed: <code>` 修权限 / 修档。
52
+ * @param {{path: string, reason: string}} p — 声明路径(绝对形)+ 读不到的原因
53
+ * @returns {string}
54
+ */
55
+ export const STANDARDS_UNREADABLE_NOTICE = ({ path, reason }) =>
56
+ `(Project standards declared at ${path} in the manifest, but unreadable — ${reason};\n` +
57
+ "the methodology-compliance criterion is unchecked.\n" +
58
+ "Fix the path or the file — do NOT declare it again. State the limitation in your findings.)"
59
+
60
+ /**
61
+ * 半读句 —— 一个层里(或声明的多个目录里)有几份读不到(态 3 的下半——零静默:余项既不注入也不报
62
+ * = 静默丢声明,与需求档 §5.1 验收 ③「任何一面都不得静默跳过」相抵;设计 = `MANIFEST.md` §2.12
63
+ * KD-M1-65 的原因分列)。
64
+ * @param {{unreadable: string[]}} p — 逐份 `"<路径>: <原因>"`(路径绝对形)
65
+ * @returns {string}
66
+ */
67
+ export const STANDARDS_PARTIAL_NOTICE = ({ unreadable }) =>
68
+ `(Of the standards declared in the manifest, these could not be read — ${unreadable.join("; ")};\n` +
69
+ "the standard set above is INCOMPLETE. Fix them — do NOT declare them again. State the limitation in your findings.)"
70
+
40
71
  /**
41
72
  * Discover the project root for the review — user decision 2026-08-08:
42
73
  * the project root is a SUBDIRECTORY of the working directory, never an
@@ -75,6 +106,50 @@ export function findGuideRoot(cwd, scopeFiles) {
75
106
  return null
76
107
  }
77
108
 
109
+ /**
110
+ * 评审注入面的**项目根**推导(标准层与文档地图共用;判据 = `docs/core/design/MANIFEST.md`
111
+ * §2.12「判据」块的 `base` 行)。`findGuideRoot` 答的是另一问(最近的 `AGENTS.md`),不在此函数的
112
+ * 射程内。三段,按序、任一命中即止:
113
+ * ① 评审范围项的归属项目根 —— 逐项 `resolve(cwd, item)` → `projectRootView(item)`,首个 `state === "ok"` 的
114
+ * `root`;序 = 喂给 `findGuideRoot` 的同一个序(`[...pathList, ...docList]`)。判据用「数据档归属」而
115
+ * 不用「最近的 `AGENTS.md`」——本键的基数必须是项目根(`docRoot` 的值一律相对项目根解析)。
116
+ * ② 会话锚自身的归属项目根 —— `projectRootView(cwd).root`,仅 `state === "ok"` 时;`workspace` 不向下取
117
+ * 子项目(KD-M1-30),故工作区锚不会被猜成某个子项目。
118
+ * ③ 两腿皆空 ⇒ `null` —— 调用方落 `agent.cwd`(既有缺省;本函数不给项目指南的根,那仍归 `findGuideRoot`)。
119
+ * **护栏**:①② 的每一次 `projectRootView` 调用各自包 `try`(抛 ⇒ 跳过该项 / 该腿,其余照走;两腿全空 ⇒ ③)。
120
+ * 依据 `thincoder-core/manifest-discovery.mjs` 的契约行「I/O 异常照原样上抛」——`owningProject` 取根那一环
121
+ * 无护栏,同档明写调用点要自己包护栏;而注入器自己的 `try` 只包 `readManifest`,腿 ② 一抛就穿过去。
122
+ * **第三参零缺省**:`scopeFiles` 不是数组即当场拒(`undefined` 与 `null` 同拒),拒因点名该参数与收到的值;
123
+ * **不归一成 `[]`**——「忘了传」与「范围为空」语义相反(前者是调用方写错,后者是一次真空范围的评审)。
124
+ * @param {string} cwd — 会话锚(绝对路径)
125
+ * @param {string[]} scopeFiles — 评审范围路径(cwd 相对或绝对;**无缺省**,空评审传 `[]`)
126
+ * @returns {string|null} 被评审范围的归属项目根(绝对路径);无项目可指 ⇒ null
127
+ */
128
+ export function findProjectContextRoot(cwd, scopeFiles) {
129
+ if (!Array.isArray(scopeFiles)) {
130
+ const got = scopeFiles === undefined ? "undefined" : scopeFiles === null ? "null" : typeof scopeFiles
131
+ throw new TypeError(`findProjectContextRoot: scopeFiles must be an array (got ${got}) — pass the review scope as a list of paths, or [] for an empty one`)
132
+ }
133
+ for (const f of scopeFiles) {
134
+ try {
135
+ const view = projectRootView(resolve(cwd, f))
136
+ if (view.state === "ok") return view.root
137
+ } catch (e) {
138
+ // 该项解析抛(参数值异常 ∥ 权限 ∥ 链接环)⇒ 只跳过这一项,其余照走。
139
+ // 留一行告警:空吞会让「走了 ③」与「范围项本就无项目」在盘上不可分(同档另两处降级同形)。
140
+ console.warn(`[advisor] project-context root probe failed at ${String(f)} (cwd ${cwd}): ${e?.code ?? e?.message ?? "unknown"}`)
141
+ }
142
+ }
143
+ try {
144
+ const view = projectRootView(cwd)
145
+ if (view.state === "ok") return view.root
146
+ } catch (e) {
147
+ // 同 ①:护栏住推导函数,不中断评审消息组装;告警让这一步可追。
148
+ console.warn(`[advisor] project-context root probe failed at cwd ${cwd}: ${e?.code ?? e?.message ?? "unknown"}`)
149
+ }
150
+ return null
151
+ }
152
+
78
153
  /**
79
154
  * Inject the project guide (AGENTS.md) into the review message. AGENTS.md is the
80
155
  * project's doc map — it defines the structure and where requirements/design
@@ -182,10 +257,14 @@ export function findDocumentMap(base) {
182
257
  /**
183
258
  * Inject the project document map (PO-1), derived from `docRoot`. Degrades visibly: the reviewer
184
259
  * must know the Document-ownership criterion is unchecked — never a silent skip.
260
+ * @param {Object} agent — the parent agent
261
+ * @param {string[]} parts — message parts (mutated)
262
+ * @param {string[]} scopeFiles — 评审范围路径;base = 它们的归属项目根(`findProjectContextRoot`,与标准层同一条
263
+ * 推导;无根 ⇒ `agent.cwd`)。**无缺省**——非数组由该函数当场拒,拒因点名参数与收到的值。
185
264
  * @returns {string|null} the injected map path, or null when degraded
186
265
  */
187
- export function injectDocumentMap(agent, parts, root) {
188
- const base = root ?? agent.cwd
266
+ export function injectDocumentMap(agent, parts, scopeFiles) {
267
+ const base = findProjectContextRoot(agent.cwd, scopeFiles) ?? agent.cwd
189
268
  const mapPath = findDocumentMap(base)
190
269
  parts.push("## Document Map")
191
270
  let text = null
@@ -207,16 +286,176 @@ export function injectDocumentMap(agent, parts, root) {
207
286
  return mapPath
208
287
  }
209
288
 
289
+ /** 消息里的路径书写(\ → /):与 `injectProjectGuide` 的根注记同款(`relative(...).split(sep).join("/")`),
290
+ * 且让「声明路径字面」在消息里可 grep(`docs/nope` 而非 `docs\nope`)。读盘仍用原样路径。 */
291
+ const displayPath = (p) => String(p).replaceAll("\\", "/")
210
292
  /**
211
- * Inject the project standards section (PO-2). The declaration face is gone with the manifest
212
- * `advisor` key, and no standards file name is derivable from what the project does declare (the old
213
- * `METHODOLOGY.md` probe was deleted exactly because most projects have none). The section stays so the
214
- * reviewer is TOLD the dimension is unchecked — visible degradation, not a silent skip.
215
- * @returns {null} always degraded
293
+ * 标准层成员收集(模块私有——遍历 ∥ 序 ∥ 去重的单源;判据 ∥ 序 = `docs/core/design/MANIFEST.md` §2.12「接口契约」)。
294
+ *
295
+ * 递归:只下潜 `ent.isDirectory()`;**符号链接一律不跟**(`ent.isSymbolicLink()` 跳过,既不判文件也不下潜)——
296
+ * 环保险只落在这条上:无深度帽(会静默漏档)、无跳过表 ∥ 无条数帽(那两者属索引面策略,搬进来同样静默漏档)。
297
+ * 成员按**名字**认 `.md`(大小写不敏);**文件侧**不按 dirent 类型预筛(目录必下潜、链接必跳):读得成 = 可读,读不成 = 不可读(带该档自己的原因)。
298
+ * 序 = 成员**收完后对整集排一次**:目录序(声明序)→ 层根相对路径(`/` 归一)码点序——不是逐层下潜各按名序
299
+ * (递归后裸文件名不再唯一;两种读法只在「子目录名是层根档名的前缀」时给出相反的序)。
300
+ * 嵌套根(一根本身在另一根之内)⇒ 同一份档按**解析后的绝对档路径**去重、保留首次出现(首次出现的层根定排序键);
301
+ * 去重键在 **win32 折小写**(尺子 = `pathKey`——同一目录的两种写法是同一个目录);折小写**只作用于去重键**,
302
+ * 排序键与定位注仍用盘上的原字符串。
303
+ * 不去重则同一份档进两次正文、吃两份预算。
304
+ * @param {string[]} dirs — 声明目录的绝对路径(声明序;`docRootPaths` 已去重)
305
+ * @returns {{readable: {path: string, text: string}[], unreadable: {path: string, reason: string}[]}}
216
306
  */
217
- export function injectProjectStandards(_agent, parts, _root) {
307
+ function collectStandardDocs(dirs) {
308
+ const members = [] // { path, dirIndex, rel, text }——收完统一排序
309
+ const unreadable = []
310
+ dirs.forEach((dir, dirIndex) => {
311
+ // 逐目录四臂(判据 = §2.12「判据」块,逐臂对应):路径不存在 ∥ stat 抛 ∥ 不是目录 ∥ 列不出来。
312
+ if (!existsSync(dir)) { unreadable.push({ path: dir, reason: "no such directory" }); return }
313
+ let st
314
+ try {
315
+ st = statSync(dir)
316
+ } catch (e) {
317
+ unreadable.push({ path: dir, reason: `read failed: ${e?.code ?? "unknown"}` })
318
+ return
319
+ }
320
+ if (!st.isDirectory()) { unreadable.push({ path: dir, reason: "not a directory" }); return }
321
+ let mdSeen = 0 // 整棵子树见过的 `.md` 名字数(零 ⇒ `no .md file`;被去重的那份也算见过——子树不是空的)
322
+ let listFailed = false // 有目录列不出来 ⇒ 子树内容未观察完,"零 `.md`" 就是一句没人观察到的事实
323
+ const stack = [[dir, ""]] // 显式栈:深处照样走(不限深度);项 = [绝对路径, 层根相对前缀]
324
+ while (stack.length > 0) {
325
+ const [here, prefix] = stack.pop()
326
+ let entries
327
+ try {
328
+ entries = readdirSync(here, { withFileTypes: true })
329
+ } catch (e) {
330
+ // 任一下潜目录列不出来 ⇒ 该目录进不可读集、其余照走(与层根同一臂,不另立原因词)。
331
+ unreadable.push({ path: here, reason: `read failed: ${e?.code ?? "unknown"}` })
332
+ listFailed = true
333
+ continue
334
+ }
335
+ for (const ent of entries) {
336
+ if (ent.isSymbolicLink()) continue // 一律不跟:既不判文件也不下潜(环保险)
337
+ const rel = prefix ? `${prefix}/${ent.name}` : ent.name
338
+ if (ent.isDirectory()) { stack.push([join(here, ent.name), rel]); continue }
339
+ if (!/\.md$/i.test(ent.name)) continue
340
+ mdSeen++
341
+ const p = join(here, ent.name)
342
+ try {
343
+ members.push({ path: p, dirIndex, rel, text: readFileSync(p, "utf8") })
344
+ } catch (e) {
345
+ unreadable.push({ path: p, reason: `read failed: ${e?.code ?? "unknown"}` })
346
+ }
347
+ }
348
+ }
349
+ // 全子树零 `.md` ⇒ 该层根不可读(态 2 的第三因:声明对了、层里没档)。
350
+ // 但「列不出来」与「层里没档」是两类可修面(KD-M1-65):目录列不完时子树内容未知,
351
+ // 再补一句 `no .md file` 就是拿没人观察到的事实指错方向(作者会去放档,该修的是权限)。
352
+ if (mdSeen === 0 && !listFailed) unreadable.push({ path: dir, reason: "no .md file" })
353
+ })
354
+ // 整集排一次:目录序 → 层根相对路径码点序(`a < b` 比较,不用 localeCompare——跨机器同序)
355
+ members.sort((a, b) => (a.dirIndex !== b.dirIndex ? a.dirIndex - b.dirIndex : a.rel < b.rel ? -1 : a.rel > b.rel ? 1 : 0))
356
+ const seen = new Set()
357
+ const readable = []
358
+ for (const m of members) {
359
+ const key = pathKey(resolve(m.path).replaceAll("\\", "/")) // 嵌套根 ∥ 同目录大小写两种写法:同一份档(win32 折小写)
360
+ if (seen.has(key)) continue // 保留首次出现(先出现的层根排在前面 ⇒ 排序键取它的层根)
361
+ seen.add(key)
362
+ readable.push({ path: m.path, text: m.text })
363
+ }
364
+ return { readable, unreadable }
365
+ }
366
+
367
+ /**
368
+ * Inject the project standards section (PO-2 · 判据 ∥ 文案单源 = `docs/core/design/MANIFEST.md` §2.12).
369
+ *
370
+ * 标准层(`docRoot.standards`)= 一个**文档层(目录)**,不是一份档:层内成员 = 该目录**及其全部子目录**下的 `.md`
371
+ * (大小写不敏感),按**层根相对路径**码点序;符号链接一律不跟(KD-M1-63)。
372
+ *
373
+ * 三态(**可机判互异**,本面立身之本 = 用声明、不猜固定名):
374
+ * 态 1 未声明 —— `docRoot.standards` 缺键 / `null` / 一个目录都没得来 ⇒ `NO_STANDARDS_NOTICE`
375
+ * (零路径字面、零 `declared`)。
376
+ * 态 2 声明了但读不到 —— 逐目录 / 逐档都不可读 ⇒ `STANDARDS_UNREADABLE_NOTICE`(点名该**目录或该档**的
377
+ * 绝对路径 + 原因)。绝不与态 1 同形。原因取值域 = `no such directory` ∥ `not a directory` ∥
378
+ * `no .md file` ∥ `read failed: <code>` ∥ `manifest read failed: <code>`——三类可修面不同
379
+ * (改路径 ∥ 改形态 ∥ 往层里放档;KD-M1-65)。
380
+ * 态 3 读到 —— 逐档「定位注 + 正文」(目录序 + 层根相对路径码点序);部分读不到时正文在前、半读句逐项点名不可读者。
381
+ *
382
+ * 取值源 = `read.ok && read.manifest ? read.manifest : DEFAULT_MANIFEST`(同 `declaredDocRoots` 先例,
383
+ * 不另立第二套判定)⇒ 整档缺失 / 非法一律 `std === null` ⇒ 态 1;`dirs.length === 0 ⇒ 态 1` 是硬不
384
+ * 变量——态 2 句永不含 `undefined` 路径。数据档本身读抛(非 ENOENT)收为态 2 族,不中断消息组装。
385
+ *
386
+ * @param {Object} agent — the parent agent(`cwd` / provider 取上下文窗口)
387
+ * @param {string[]} parts — message parts (mutated)
388
+ * @param {string[]} scopeFiles — 评审范围路径;base = 它们的归属项目根(`findProjectContextRoot`;无根 ⇒
389
+ * `agent.cwd`)。与 `injectDocumentMap` 同一 base、同一条推导。**无缺省**——非数组由该函数当场拒,拒因点名
390
+ * 参数与收到的值。
391
+ * @returns {string|string[]|null} 注入的标准档绝对路径(单份 = 串,多份 = 数组);降级 ⇒ null
392
+ */
393
+ export function injectProjectStandards(agent, parts, scopeFiles) {
394
+ // base 先算:第三参不合契约时在改动 parts 之前就抛(消息里不留半段)。
395
+ const base = findProjectContextRoot(agent.cwd, scopeFiles) ?? agent.cwd
218
396
  parts.push("## Project Standards")
219
- parts.push(NO_STANDARDS_NOTICE) // degraded — visible by contract
397
+
398
+ // 数据档本身读抛(权限 / 目录等——`readManifest` fail-closed 上抛):收为态 2 族,不冒 `undefined`。
399
+ let read
400
+ try {
401
+ read = readManifest(base)
402
+ } catch (e) {
403
+ parts.push(STANDARDS_UNREADABLE_NOTICE({ path: displayPath(manifestFilePath(base)), reason: `manifest read failed: ${e?.code ?? "unknown"}` }))
404
+ parts.push("")
405
+ return null
406
+ }
407
+
408
+ const std = (read.ok && read.manifest ? read.manifest : DEFAULT_MANIFEST).docRoot.standards
409
+ // 态 1(两入口):未声明(缺键 / null / undefined)∥ 声明了但一个合法目录都没得来。
410
+ // 后者是硬不变量:宁可判「未声明」,也绝不发一句带 `undefined` 的「declared at …」。
411
+ const dirs = std == null ? [] : docRootPaths(std, base)
412
+ if (dirs.length === 0) {
413
+ parts.push(NO_STANDARDS_NOTICE) // degraded — visible by contract
414
+ parts.push("")
415
+ return null
416
+ }
417
+
418
+ // 遍历 ∥ 排序 ∥ 嵌套去重全在 `collectStandardDocs`(本函数只留三态组装与预算——可读单元不变)。
419
+ const { readable, unreadable } = collectStandardDocs(dirs)
420
+ // 态 2:声明了,一份都读不到(句中路径 = 不可读集第一项——目录项与文件项同形)。
421
+ if (readable.length === 0) {
422
+ parts.push(STANDARDS_UNREADABLE_NOTICE({ path: displayPath(unreadable[0].path), reason: unreadable[0].reason }))
423
+ parts.push("")
424
+ return null
425
+ }
426
+
427
+ // 态 3:正文(一个层的全部档共用一次注入的同一预算——按**注入序**(目录序 → 层根相对路径码点序)拼接后一次截断)。
428
+ // 逐档定位注先行(评审要知道读的是哪一份),多档多行、与正文同序。
429
+ parts.push("The documents below are the project's standards — the `.md` files of the declared standards layer. Judge methodology compliance against them.")
220
430
  parts.push("")
221
- return null
431
+ const blocks = readable.map((r) => ({ path: r.path, text: `<!-- standards file: ${displayPath(r.path)} -->\n${r.text}` }))
432
+ const body = blocks.map((b) => b.text).join("\n\n")
433
+ // 预算算式复用同两条常量、**逐注入独立计算**(不与指南共享池——两面语义不同,不该互相记账)。
434
+ // providerSpec 取值在 body 之外算:上下文规格查不到也不得伪装成「没有标准档」。
435
+ const resolvedProvider = agent.provider ?? agent._provider
436
+ const ctx = providerSpec(resolvedProvider).context
437
+ const cap = Math.max(PROJECT_GUIDE_MIN, Math.floor(ctx * PROJECT_GUIDE_FRACTION))
438
+ let shown = body
439
+ // 计量与切片**同一单位**(码点):两条口径混用会让含星平面字符(emoji / 生僻字)的标准档在
440
+ // 「其实全给了」时也报 PARTIAL、并给出 truncated at N of M 的假读数(M 是 UTF-16 长度)。
441
+ const chars = [...body]
442
+ if (chars.length > cap) {
443
+ // 被截断的那一份(拼接期已有逐份偏移;偏移同以码点计)——恢复指针必须指向它,否则评审拿着残缺规矩判。
444
+ let offset = 0
445
+ let cut = blocks[blocks.length - 1].path
446
+ for (const b of blocks) {
447
+ const len = [...b.text].length
448
+ if (cap < offset + len) { cut = b.path; break }
449
+ offset += len + 2 // +2 = 拼接分隔符 "\n\n"
450
+ }
451
+ // 截断的风险不在大小,在**静默**:指南截断丢的是指针,标准层截断丢的是判据本身。
452
+ shown = chars.slice(0, cap).join("") + `\n\n…(truncated at ${cap} of ${chars.length} chars — the standard set is PARTIAL;\nread the full file — ${displayPath(cut)} — for the rest)`
453
+ }
454
+ parts.push(shown)
455
+ // 半读句:正文在前,缺口逐项点名(零静默——KD-M1-65)。
456
+ if (unreadable.length > 0) {
457
+ parts.push(STANDARDS_PARTIAL_NOTICE({ unreadable: unreadable.map((u) => `${displayPath(u.path)}: ${u.reason}`) }))
458
+ }
459
+ parts.push("")
460
+ return readable.length === 1 ? readable[0].path : readable.map((r) => r.path)
222
461
  }
package/advisor/run.mjs CHANGED
@@ -132,10 +132,13 @@ export async function runAdvisorReview(agent, reviewType, callbacks, designToken
132
132
  }
133
133
  }
134
134
 
135
- // BATCH-RECORD.md §4.3 批次档写通道的绑定(仅设计评审):同步路径 = 调用方(advisor 工具)传入的
136
- // callbacks.batchDoc(即 resolved.run 的实例绑定);异步路径 = 本实例在跑池条目的
137
- // run.batchDoc(同文档集实例键——各评审各取各档,不用单值会话态)。
138
- const boundBatchDoc = reviewType === "design" ? batchDocForReview(agent, documents, callbacks) : null
135
+ // BATCH-RECORD.md §4.3 批次档写通道的绑定——**与评审轨无关**:携了可读 `batchDoc` 即绑定
136
+ // (判据单源 = `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.15)。同步路径 = 调用方(advisor
137
+ // 工具)传入的 callbacks.batchDoc(本笔发起时点的绑定值);异步路径 = 池条目 `entry.start()` 传入的
138
+ // 同一逐笔快照(条目字段 `batchDoc`,不读活的实例字段)。
139
+ // 工具面批走代码轨,评审员靠这一绑定才拿得到写 §3 的手;按轨截断 ⇒ 那一轨永远没有写通道,
140
+ // `§3` 只能编或永远挂着(用户 2026-11-01 11:43 否掉的那件事)。
141
+ const boundBatchDoc = batchDocForReview(agent, documents, callbacks)
139
142
  // F13/`ADVISOR-GUARDS.md §2` #3:压缩定锚简报(评审参数构建——压缩触发时由 compaction 重挂)。
140
143
  const pinned = buildPinnedBrief(reviewType, documents, object, designToken, designId)
141
144
 
package/advisor.mjs CHANGED
@@ -14,7 +14,8 @@
14
14
  * { advisor: { enabled: true, provider: "deepseek", model: "deepseek-chat" } }
15
15
  * provider + model are optional — defaults to the main agent's provider/model.
16
16
  *
17
- * Convergence protocol:
17
+ * Convergence protocol (CODE REVIEW — a design review has none: it takes
18
+ * `advisor-design.md` every round and never decays):
18
19
  * Round 1: full review → produces a numbered issue table.
19
20
  * Agent responds with a response table per issue (fix claims).
20
21
  * Round 2: semi-convergence — verifies the prior table + can flag obvious new issues.
@@ -32,7 +33,7 @@
32
33
  * counter only drives prompt decay and display; failure paths end in a
33
34
  * settlement conclusion block, and repeated failures are reported to the user.
34
35
  * Rounds 2+ also declare all earlier diffs STALE and require read-verified
35
- * file:line evidence for any unfixed/new finding — see docs/design/ADVISOR-CONVERGENCE.md.
36
+ * file:line evidence for any unfixed/new finding — see docs/core/design/ADVISOR-CONVERGENCE.md.
36
37
  *
37
38
  * Session memory (agent._advisorSession):
38
39
  * RETAINED for initialization compatibility but NEVER read (decision d698434):
@@ -45,7 +46,7 @@
45
46
  * Project customisation: .thincoder/advisor.md in the project root.
46
47
  */
47
48
  import { extractAgentResponseTable } from "./advisor/history.mjs"
48
- import { buildAdvisorUserMessage, resolveScopeFiles, buildObjectDeclarationBlock, buildDesignApprovalBlock } from "./advisor/messages.mjs"
49
+ import { buildAdvisorUserMessage, resolveScopeFiles, buildObjectDeclarationBlock } from "./advisor/messages.mjs"
49
50
  import { buildConvergenceBody } from "./advisor/convergence.mjs"
50
51
  import { escapeLiteralEscapes } from "./escape.mjs"
51
52
  import { applyPromptInjections, loadAdvisorPrompt } from "./prompt-files.mjs"
@@ -61,10 +62,11 @@ export { buildObjectDeclarationBlock, buildDesignApprovalBlock } from "./advisor
61
62
  // 改经核内单一解析面 `loadAdvisorPrompt`(契约 8 / D-C13);缺档语义不变 = 抛错(契约 9)。
62
63
 
63
64
  const ADVISOR_ROUND1 = loadAdvisorPrompt("advisor-round1.md")
64
- // ROUND2/3 are used whenever a convergence round (round 2+) is being built:
65
- // in-run session continuation replaces the system prompt with them, and a
65
+ // ROUND2/3 are used whenever a CODE-REVIEW convergence round (round 2+) is being
66
+ // built: in-run session continuation replaces the system prompt with them, and a
66
67
  // rebuilt fresh session (e.g. after a failed review) also selects them via
67
- // buildAdvisorSystemPrompt when _advisorRound > 0.
68
+ // buildAdvisorSystemPrompt when _advisorRound > 0. A design review never takes
69
+ // them — it re-applies the full criteria every round (ADVISOR_DESIGN).
68
70
  const ADVISOR_ROUND2 = loadAdvisorPrompt("advisor-round2.md")
69
71
  const ADVISOR_ROUND3 = loadAdvisorPrompt("advisor-round3.md")
70
72
  // Design-review prompt — hard-loaded like the round prompts (decision
@@ -78,16 +80,16 @@ const ADVISOR_DESIGN = loadAdvisorPrompt("advisor-design.md")
78
80
  // ────────────────────────────────────────
79
81
 
80
82
  /**
81
- /** Build the system prompt for an advisor review session.
83
+ * Build the system prompt for an advisor review session.
82
84
  * @param {Object} agent — the parent agent
83
85
  * @param {Object|null} [prior] — prior review output (full text; decision 2026-08-08)
84
86
  * @param {string} [reviewType] — "design" for design review, undefined/"code" for code review
85
87
  * @returns {string} the system prompt
86
88
  */
87
89
 
88
- // 锚替换缝(CORE-UNIFICATION §2.13.8——U0):本函数**六个 return 逐条**经 `applyPromptInjections`
89
- // (未配置 = 恒等,现行行为零变);读取径(模块级常量缓存)不动。结构机检
90
- // (`test/prompt-injections.test.mjs`)按 return 分支逐条守——新增分支未挂即红。
90
+ // 锚替换缝(CORE-UNIFICATION §2.13.8——U0):本函数**每个 return 分支**都经 `applyPromptInjections`
91
+ // (未配置 = 恒等,现行行为零变);读取径(模块级常量缓存)不动。必须保持的不变量:每个 return
92
+ // 分支都经 `applyPromptInjections`,新增分支未挂即错。
91
93
  export function buildAdvisorSystemPrompt(agent, prior, reviewType) {
92
94
  // Round decision is DETERMINISTIC (decision 2026-08-08): _advisorRound > 0
93
95
  // with a stored review output means convergence (round 2+); 0 means round 1.
@@ -95,16 +97,11 @@ export function buildAdvisorSystemPrompt(agent, prior, reviewType) {
95
97
  // and the stored output are the only inputs. A restarted process has
96
98
  // _advisorRound 0 → conservative full re-review.
97
99
  const hasPrior = (agent._advisorRound || 0) > 0 && (prior ?? agent._lastAdvisorOutput)
98
- // Design review: round 1 uses the dedicated design-review prompt (full scope +
99
- // approval token); rounds 2+ converge like code reviews (verify agent fix claims).
100
- if (reviewType === "design") {
101
- if (!hasPrior) {
102
- return applyPromptInjections(ADVISOR_DESIGN)
103
- }
104
- const round = (agent._advisorRound || 0) + 1
105
- if (round === 2) return applyPromptInjections(ADVISOR_ROUND2)
106
- return applyPromptInjections(ADVISOR_ROUND3)
107
- }
100
+ // Design review: EVERY round takes the dedicated design-review prompt — the
101
+ // complete criteria, applied to a full re-read (decision 2026-11-16). Design
102
+ // review has no convergence decay: the round counter drives display and audit
103
+ // only, never which criteria are in force.
104
+ if (reviewType === "design") return applyPromptInjections(ADVISOR_DESIGN)
108
105
  if (!hasPrior) return applyPromptInjections(ADVISOR_ROUND1)
109
106
  const round = (agent._advisorRound || 0) + 1
110
107
  if (round === 2) return applyPromptInjections(ADVISOR_ROUND2)
@@ -178,8 +175,10 @@ export { escapeLiteralEscapes }
178
175
  /**
179
176
  * Build the advisor conversation for this run.
180
177
  * EVERY call builds a fresh [system, user] session (decision d698434) — no
181
- * session reuse across rounds: round 1 = full scope (ROUND1 prompt), rounds
182
- * 2+ = convergence (ROUND2/ROUND3 prompt + fix-claims follow-up).
178
+ * session reuse across rounds. CODE REVIEW: round 1 = full scope (ROUND1
179
+ * prompt), rounds 2+ = convergence (ROUND2/ROUND3 prompt + fix-claims
180
+ * follow-up). DESIGN REVIEW: every round takes ADVISOR_DESIGN and the full
181
+ * review scope, with the prior output carried as a reference block (no decay).
183
182
  * @param {Object} agent — the parent agent
184
183
  * @param {string} [reviewType] — "design" or "code" (已由工具层类型门定轨——此处无缺省;F30)
185
184
  * @param {string|null} [designToken] — design-review approval token (design only)
@@ -198,10 +197,13 @@ export function prepareAdvisorMessages(agent, reviewType, designToken = null, do
198
197
  // as buildAdvisorSystemPrompt (single source of truth for round semantics).
199
198
  const prior = (agent._advisorRound || 0) > 0 ? (priorParam ?? agent._lastAdvisorOutput) : null
200
199
 
201
- // Design review round 1: the dedicated full-scope review with the approval
202
- // token (an independent gate — it runs even when a prior review exists, e.g.
203
- // after a failed design review). Fresh session.
204
- if (reviewType === "design" && (agent._advisorRound || 0) === 0) {
200
+ // Design review — EVERY round: the dedicated full-scope review, with the
201
+ // approval token re-injected (an async fix-round continuation must be able to
202
+ // re-approve) and the prior review carried as a REFERENCE block, never as the
203
+ // verification list (decision 2026-11-16: a design review has no convergence
204
+ // decay — each round re-reads the whole scope against the complete criteria).
205
+ // Fresh session, exactly like round 1.
206
+ if (reviewType === "design") {
205
207
  return [
206
208
  { role: "system", content: buildAdvisorSystemPrompt(agent, prior, reviewType) },
207
209
  { role: "user", content: escapeLiteralEscapes(buildAdvisorUserMessage(agent, prior, reviewType, designToken, documents, paths, object, designId)) },
@@ -213,7 +215,7 @@ export function prepareAdvisorMessages(agent, reviewType, designToken = null, do
213
215
  // re-review false reports (the model quoted pre-fix file content instead of
214
216
  // re-reading) and a token sink. The agent response table (fix claims) is
215
217
  // injected through buildAdvisorFollowUp instead; the system prompt carries
216
- // the round (ROUND2/ROUND3) via buildAdvisorSystemPrompt.
218
+ // the round (ROUND2/ROUND3, code review only) via buildAdvisorSystemPrompt.
217
219
  // No prior review output: reset ONLY when this run made no code changes (user
218
220
  // decision 2026-08-05: any loop that modified code must NOT reset — the
219
221
  // advisor guard WILL push back, so the convergence round must keep advancing
@@ -260,22 +262,6 @@ export function prepareAdvisorMessages(agent, reviewType, designToken = null, do
260
262
  // review surface.
261
263
  const scopeFiles = resolveScopeFiles(agent, paths)
262
264
  const followUp = buildAdvisorFollowUp(agent, prior, scopeFiles, object)
263
- // AGENT-LOOP-ASYNC-POOL.md §6.10 D-24b (design round 2+ — async fix-round continuations must be able to
264
- // re-approve): re-anchor the review scope (the convergence follow-up carries no
265
- // document list) and inject the round's design token with the approval signal.
266
- if (reviewType === "design") {
267
- const docList = Array.isArray(documents)
268
- ? documents.filter((d) => typeof d === "string" && d.trim())
269
- : []
270
- const scopeBlock = docList.length > 0
271
- ? `\n\n## Documents to Review\nThe documents below are the review scope. Review ONLY these files — do not scan git diff or read any other files.\n${docList.map((d) => `- ${d} — Read this file in full`).join("\n")}`
272
- : ""
273
- const tokenBlock = designToken ? `\n\n${buildDesignApprovalBlock(designToken, designId)}` : ""
274
- return [
275
- { role: "system", content: buildAdvisorSystemPrompt(agent, prior, reviewType) },
276
- { role: "user", content: escapeLiteralEscapes(followUp + scopeBlock + tokenBlock) },
277
- ]
278
- }
279
265
  return [
280
266
  { role: "system", content: buildAdvisorSystemPrompt(agent, prior, reviewType) },
281
267
  { role: "user", content: escapeLiteralEscapes(followUp) },
@@ -0,0 +1,87 @@
1
+ /**
2
+ * agent/batch-attribution.mjs — 归属表:记录 / 查询 / 合入三个纯函数(本档零 fs、零 import)。
3
+ *
4
+ * 承载的东西 = 档位臂取材面的**判据一**(语义单源 = `docs/core/design/BATCH-RECORD.md` §4.18.1):
5
+ * 写的那一刻「这笔写服务哪一批」已知(`agent._batchDoc`——子代理由 spawn 绑定,见 `subagent-spawn.mjs`),
6
+ * 记账时把它连同路径一起写下;收口取「归属含本档」的那些路径。
7
+ *
8
+ * 为什么不加进 `agent._touchedFiles`:那个数组的读者有 `recent_changes`、`verify`、子代理调度、审计块四处,
9
+ * 改它的形状要同轮改四处;而归属只是多一列事实,于是另记一张表、`_touchedFiles` 的形状一字不动。
10
+ *
11
+ * 键 = 批档绝对路径(收口比对的正是批档本身;号→档还要多一次映射,而一笔写只知道它服务的批)。
12
+ * 值 = 该批改过的绝对路径集。
13
+ *
14
+ * 路径只做**存放**,不在这里归一:比较口径(win32 折小写、分隔符归一)归收口面那一次比对,本档不持第二把尺。
15
+ */
16
+
17
+ /** 表的取法:`agent._batchAttribution` 是 Map 就用它,否则当场建一张(首笔记账与后续读取同一形)。 */
18
+ function tableOf(agent) {
19
+ if (!agent || typeof agent !== "object") return null
20
+ if (agent._batchAttribution instanceof Map) return agent._batchAttribution
21
+ const table = new Map()
22
+ agent._batchAttribution = table
23
+ return table
24
+ }
25
+
26
+ /** 批档键(只有非空串才算一个批;`null` ∥ 空串 = 这笔写没有绑定批档)。 */
27
+ const batchKeyOf = (batchDoc) => (typeof batchDoc === "string" && batchDoc.trim() !== "" ? batchDoc.trim() : null)
28
+
29
+ /**
30
+ * 记账:把 `paths` 记到 `batchDoc` 名下(一档一批一条,重复记账幂等)。
31
+ * 没有绑定批档的写**不入表**——收口面对没有归属的路径按本批判(fail-closed),这里不替它猜一个批。
32
+ * @param {object} agent — 记账的载体(`agent._batchAttribution` 挂在它上面)
33
+ * @param {string[]} paths — 这笔写落下的绝对路径
34
+ * @param {string|null} batchDoc — 这笔写服务的批档(`agent._batchDoc`)
35
+ * @returns {boolean} 真有新路径入表 ⇒ `true`
36
+ */
37
+ export function recordAttribution(agent, paths, batchDoc) {
38
+ const batch = batchKeyOf(batchDoc)
39
+ if (batch === null) return false
40
+ const list = Array.isArray(paths) ? paths : []
41
+ if (list.length === 0) return false
42
+ const table = tableOf(agent)
43
+ if (!table) return false
44
+ let set = table.get(batch)
45
+ if (!(set instanceof Set)) {
46
+ set = new Set()
47
+ table.set(batch, set)
48
+ }
49
+ let added = false
50
+ for (const p of list) {
51
+ if (typeof p !== "string" || p === "" || set.has(p)) continue
52
+ set.add(p)
53
+ added = true
54
+ }
55
+ return added
56
+ }
57
+
58
+ /**
59
+ * 查询:`batchDoc` 这一批的路径集(**副本**——调用方排序 ∥ 过滤都不改表本体)。
60
+ * @param {object} agent — 载体
61
+ * @param {string} batchDoc — 批档(绝对路径)
62
+ * @returns {string[]} 该批的路径(无记录 ⇒ 空集,不是 `null`:空集与「这一批一笔没记」同判)
63
+ */
64
+ export function attributedTo(agent, batchDoc) {
65
+ const batch = batchKeyOf(batchDoc)
66
+ const table = agent?._batchAttribution
67
+ if (batch === null || !(table instanceof Map)) return []
68
+ const set = table.get(batch)
69
+ return set instanceof Set ? [...set] : []
70
+ }
71
+
72
+ /**
73
+ * 合入:子代理的归属表并回父侧(并集;子代理记在它自己的 `agent` 上,父侧不靠路径再猜)。
74
+ * @param {object} parent — 父侧载体(并进去的一方)
75
+ * @param {object} child — 子代理载体(被并的一方;没有表 ⇒ 什么都不做)
76
+ * @returns {boolean} 真并入了新路径 ⇒ `true`
77
+ */
78
+ export function mergeAttribution(parent, child) {
79
+ const src = child?._batchAttribution
80
+ if (!(src instanceof Map) || src.size === 0) return false
81
+ let merged = false
82
+ for (const [batch, paths] of src) {
83
+ const list = paths instanceof Set ? [...paths] : []
84
+ if (recordAttribution(parent, list, batch)) merged = true
85
+ }
86
+ return merged
87
+ }