@thincoder/core 0.11.1 → 0.11.3

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 (382) hide show
  1. package/CHANGELOG.md +64 -2
  2. package/advisor/compaction.mjs +7 -5
  3. package/advisor/convergence.mjs +34 -3
  4. package/advisor/history.mjs +24 -11
  5. package/advisor/loop.mjs +49 -12
  6. package/advisor/messages.mjs +114 -33
  7. package/advisor/project-context.mjs +171 -42
  8. package/advisor/run.mjs +24 -10
  9. package/advisor.mjs +36 -46
  10. package/agent/batch-attribution.mjs +87 -0
  11. package/agent/batch-changed-surface.mjs +129 -0
  12. package/agent/chat-call.mjs +14 -9
  13. package/agent/completion.mjs +70 -41
  14. package/agent/continue-decision.mjs +14 -8
  15. package/agent/digest-account.mjs +133 -59
  16. package/agent/dispatch-gates.mjs +6 -0
  17. package/agent/dispatch-run.mjs +1 -1
  18. package/agent/dispatch.mjs +10 -4
  19. package/agent/family-tools.mjs +37 -23
  20. package/agent/helpers.mjs +139 -73
  21. package/agent/image-heal.mjs +18 -4
  22. package/agent/offload.mjs +25 -29
  23. package/agent/post-turn.mjs +36 -35
  24. package/agent/record-results.mjs +39 -7
  25. package/agent/run-stages.mjs +132 -52
  26. package/agent/run-start.mjs +150 -78
  27. package/agent/setup-reminders.mjs +194 -91
  28. package/agent/setup.mjs +219 -140
  29. package/agent/spawn-child.mjs +40 -3
  30. package/agent/suspension.mjs +43 -15
  31. package/agent/timers.mjs +9 -4
  32. package/agent/turn-domain-text.mjs +105 -0
  33. package/agent/turn-loop.mjs +84 -27
  34. package/agent/turn-projection.mjs +286 -0
  35. package/agent/turn-task-state.mjs +274 -0
  36. package/agent/turn-tool-process.mjs +163 -0
  37. package/agent/write-gate-ledger.mjs +569 -0
  38. package/agent/write-gate.mjs +29 -352
  39. package/agent-tools/advisor-async.mjs +35 -172
  40. package/agent-tools/advisor-launch.mjs +178 -0
  41. package/agent-tools/advisor-settle.mjs +28 -1
  42. package/agent-tools/advisor.mjs +37 -48
  43. package/agent-tools/async-discard.mjs +7 -2
  44. package/agent-tools/async-settle.mjs +18 -12
  45. package/agent-tools/audit-block.mjs +6 -6
  46. package/agent-tools/bash-async.mjs +5 -2
  47. package/agent-tools/batch-closeout.mjs +223 -35
  48. package/agent-tools/batch-gate.mjs +142 -342
  49. package/agent-tools/batch-ledger-bind.mjs +240 -20
  50. package/agent-tools/batch-ledger-view.mjs +109 -0
  51. package/agent-tools/batch-lifecycle.mjs +197 -48
  52. package/agent-tools/batch-paths.mjs +29 -8
  53. package/agent-tools/batch-read.mjs +151 -4
  54. package/agent-tools/batch-record-write.mjs +417 -0
  55. package/agent-tools/batch-regions.mjs +215 -0
  56. package/agent-tools/batch-reopen.mjs +23 -11
  57. package/agent-tools/batch-segment-vocab.mjs +459 -0
  58. package/agent-tools/batch-skeleton.mjs +196 -239
  59. package/agent-tools/batch-supersede.mjs +232 -0
  60. package/agent-tools/batch-tier-gate.mjs +214 -0
  61. package/agent-tools/batch.mjs +192 -439
  62. package/agent-tools/browser-async.mjs +5 -2
  63. package/agent-tools/consult.mjs +17 -11
  64. package/agent-tools/context.mjs +76 -33
  65. package/agent-tools/design-review-record.mjs +225 -0
  66. package/agent-tools/design-slots.mjs +1 -1
  67. package/agent-tools/design-token.mjs +279 -81
  68. package/agent-tools/digest-budget.mjs +10 -9
  69. package/agent-tools/doc-check.mjs +43 -33
  70. package/agent-tools/doc-graph.mjs +140 -0
  71. package/agent-tools/eng.mjs +12 -12
  72. package/agent-tools/escalate-async.mjs +5 -5
  73. package/agent-tools/goal.mjs +5 -2
  74. package/agent-tools/judge-domain.mjs +347 -0
  75. package/agent-tools/manifest.mjs +34 -18
  76. package/agent-tools/parent-channel.mjs +6 -3
  77. package/agent-tools/plan.mjs +11 -10
  78. package/agent-tools/read-history.mjs +112 -11
  79. package/agent-tools/review-facts.mjs +19 -6
  80. package/agent-tools/settings.mjs +23 -2
  81. package/agent-tools/skill.mjs +7 -8
  82. package/agent-tools/spawn-gates.mjs +14 -24
  83. package/agent-tools/ssh-session.mjs +8 -5
  84. package/agent-tools/ssh-text.mjs +155 -0
  85. package/agent-tools/ssh-transfer.mjs +1 -1
  86. package/agent-tools/ssh-transport.mjs +19 -148
  87. package/agent-tools/subagent-actions-query.mjs +10 -6
  88. package/agent-tools/subagent-actions.mjs +2 -2
  89. package/agent-tools/subagent-async.mjs +19 -17
  90. package/agent-tools/subagent-brief.mjs +76 -0
  91. package/agent-tools/subagent-design-token.mjs +168 -0
  92. package/agent-tools/subagent-run.mjs +1 -1
  93. package/agent-tools/subagent-scheduler.mjs +13 -13
  94. package/agent-tools/subagent-spawn.mjs +92 -112
  95. package/agent-tools/subagent.mjs +16 -28
  96. package/agent-tools/task.mjs +0 -8
  97. package/agent-tools/taskbook-block.mjs +94 -0
  98. package/agent-tools/timer.mjs +12 -8
  99. package/agent-tools/verify.mjs +13 -4
  100. package/agent-tools.mjs +7 -3
  101. package/agent.mjs +26 -7
  102. package/attachments.mjs +3 -3
  103. package/auto-think.mjs +5 -2
  104. package/browser/actions.mjs +9 -3
  105. package/browser/cdp.mjs +45 -17
  106. package/browser/launch.mjs +25 -43
  107. package/browser/pool.mjs +299 -0
  108. package/browser/queue.mjs +2 -0
  109. package/browser/session-state.mjs +450 -0
  110. package/browser/session.mjs +127 -151
  111. package/browser/snapshot.mjs +89 -0
  112. package/client-identity.mjs +1 -1
  113. package/compaction-work-state.mjs +230 -0
  114. package/compress-form.mjs +41 -6
  115. package/config-defaults.mjs +7 -3
  116. package/config-migrate.mjs +40 -0
  117. package/config-normalize.mjs +28 -2
  118. package/config-reload.mjs +59 -0
  119. package/config.mjs +110 -110
  120. package/context-degrade.mjs +93 -34
  121. package/context-echo.mjs +6 -15
  122. package/context-push.mjs +328 -31
  123. package/context.mjs +298 -170
  124. package/conventions.mjs +11 -9
  125. package/declaration.mjs +126 -22
  126. package/declared-repos.mjs +94 -0
  127. package/doc-check/doc-names.mjs +73 -0
  128. package/doc-check/doc-tokens.mjs +110 -0
  129. package/doc-check/existence.mjs +160 -0
  130. package/doc-check/index.mjs +309 -213
  131. package/doc-check/line-refs.mjs +27 -101
  132. package/doc-check/paragraph.mjs +218 -102
  133. package/doc-graph/anchors.mjs +474 -0
  134. package/doc-graph/doc-batch-face.mjs +59 -0
  135. package/doc-graph/doc-suffix.mjs +27 -0
  136. package/doc-graph/graph-tokens.mjs +160 -0
  137. package/doc-graph.mjs +778 -0
  138. package/escape.mjs +6 -5
  139. package/explore-distill.mjs +41 -8
  140. package/generate-title.mjs +10 -1
  141. package/i18n.mjs +1 -1
  142. package/instance-inbox-watch.mjs +272 -0
  143. package/instance-inbox.mjs +218 -0
  144. package/instance-registry.mjs +179 -0
  145. package/knowledge/README.md +3 -0
  146. package/knowledge/prompt-resources.md +10 -0
  147. package/ledger-close-proof.mjs +100 -49
  148. package/ledger-close.mjs +188 -0
  149. package/ledger-cmd.mjs +47 -91
  150. package/ledger-consistency.mjs +113 -0
  151. package/ledger-db.mjs +47 -15
  152. package/ledger-executors.mjs +1 -1
  153. package/ledger-tools.mjs +121 -62
  154. package/ledger-write-ops.mjs +162 -46
  155. package/ledger.mjs +1 -1
  156. package/manifest-discovery.mjs +3 -1
  157. package/manifest-schema.mjs +27 -5
  158. package/memory/docs.mjs +51 -15
  159. package/memory/file-list.mjs +6 -0
  160. package/memory/schema.mjs +2 -2
  161. package/model-specs.mjs +87 -21
  162. package/origin-guard.mjs +190 -0
  163. package/package.json +3 -1
  164. package/path-resolve.mjs +338 -57
  165. package/prompt-files.mjs +114 -2
  166. package/prompt-overlays.mjs +21 -11
  167. package/prompts/advisor-design.md +96 -36
  168. package/prompts/advisor-round1.md +48 -4
  169. package/prompts/advisor-round2.md +54 -10
  170. package/prompts/advisor-round3.md +53 -9
  171. package/prompts/common.md +99 -434
  172. package/prompts/consult-base.md +7 -4
  173. package/prompts/discipline-engineering.md +504 -295
  174. package/prompts/discipline-normal.md +106 -281
  175. package/prompts/knowledge/business-gates.md +30 -0
  176. package/prompts/knowledge/decision-quality.md +21 -0
  177. package/prompts/knowledge/doc-lookup.md +30 -0
  178. package/prompts/knowledge/document-writing.md +33 -0
  179. package/prompts/knowledge/documentation-evaluation.md +48 -0
  180. package/prompts/knowledge/evidence-discipline.md +38 -0
  181. package/prompts/knowledge/normal-coding-workflow.md +110 -0
  182. package/prompts/knowledge/normal-delivery-collaboration.md +124 -0
  183. package/prompts/knowledge/normal-work-management.md +60 -0
  184. package/prompts/knowledge/product-security.md +28 -0
  185. package/prompts/knowledge/project-records.md +73 -0
  186. package/prompts/knowledge/tool-advisor.md +29 -0
  187. package/prompts/knowledge/tool-apply-patch.md +17 -0
  188. package/prompts/knowledge/tool-bash.md +48 -0
  189. package/prompts/knowledge/tool-batch-eng-coder.md +19 -0
  190. package/prompts/knowledge/tool-batch-eng-designer.md +20 -0
  191. package/prompts/knowledge/tool-batch-review.md +18 -0
  192. package/prompts/knowledge/tool-batch.md +94 -0
  193. package/prompts/knowledge/tool-browser.md +68 -0
  194. package/prompts/knowledge/tool-code-search.md +4 -0
  195. package/prompts/knowledge/tool-consult-start.md +12 -0
  196. package/prompts/knowledge/tool-consult-stop.md +6 -0
  197. package/prompts/knowledge/tool-context.md +6 -0
  198. package/prompts/knowledge/tool-delete.md +21 -0
  199. package/prompts/knowledge/tool-doc-check.md +71 -0
  200. package/prompts/knowledge/tool-doc-graph-read.md +20 -0
  201. package/prompts/knowledge/tool-doc-graph.md +49 -0
  202. package/prompts/knowledge/tool-doc-search.md +6 -0
  203. package/prompts/knowledge/tool-edit.md +41 -0
  204. package/prompts/knowledge/tool-eng.md +2 -0
  205. package/prompts/knowledge/tool-execute.md +21 -0
  206. package/prompts/knowledge/tool-fetch.md +18 -0
  207. package/prompts/knowledge/tool-file-ops.md +21 -0
  208. package/prompts/knowledge/tool-get-current-time.md +7 -0
  209. package/prompts/knowledge/tool-git-read.md +21 -0
  210. package/prompts/knowledge/tool-git.md +29 -0
  211. package/prompts/knowledge/tool-glob.md +17 -0
  212. package/prompts/knowledge/tool-goal.md +8 -0
  213. package/prompts/knowledge/tool-grep.md +21 -0
  214. package/prompts/knowledge/tool-hashline-edit.md +16 -0
  215. package/prompts/knowledge/tool-insert-after.md +17 -0
  216. package/prompts/knowledge/tool-instance-list.md +6 -0
  217. package/prompts/knowledge/tool-instance-send.md +10 -0
  218. package/prompts/knowledge/tool-knowledge.md +18 -0
  219. package/prompts/knowledge/tool-ledger-read.md +28 -0
  220. package/prompts/knowledge/tool-ledger.md +46 -0
  221. package/prompts/knowledge/tool-lint.md +16 -0
  222. package/prompts/knowledge/tool-ls.md +15 -0
  223. package/prompts/knowledge/tool-lsp.md +11 -0
  224. package/prompts/knowledge/tool-main-history.md +2 -0
  225. package/prompts/knowledge/tool-manifest-read.md +14 -0
  226. package/prompts/knowledge/tool-manifest.md +34 -0
  227. package/prompts/knowledge/tool-memory-read.md +20 -0
  228. package/prompts/knowledge/tool-memory.md +19 -0
  229. package/prompts/knowledge/tool-notify-parent.md +12 -0
  230. package/prompts/knowledge/tool-operations.md +102 -0
  231. package/prompts/knowledge/tool-peer-instances.md +2 -0
  232. package/prompts/knowledge/tool-plan.md +5 -0
  233. package/prompts/knowledge/tool-process-read.md +13 -0
  234. package/prompts/knowledge/tool-process.md +15 -0
  235. package/prompts/knowledge/tool-question.md +16 -0
  236. package/prompts/knowledge/tool-read-history-batch.md +22 -0
  237. package/prompts/knowledge/tool-read-history.md +35 -0
  238. package/prompts/knowledge/tool-read-image.md +11 -0
  239. package/prompts/knowledge/tool-read.md +22 -0
  240. package/prompts/knowledge/tool-recent-changes.md +6 -0
  241. package/prompts/knowledge/tool-repo-outline.md +4 -0
  242. package/prompts/knowledge/tool-settings-read.md +12 -0
  243. package/prompts/knowledge/tool-settings.md +17 -0
  244. package/prompts/knowledge/tool-skill.md +6 -0
  245. package/prompts/knowledge/tool-ssh.md +74 -0
  246. package/prompts/knowledge/tool-subagent.md +57 -0
  247. package/prompts/knowledge/tool-task.md +14 -0
  248. package/prompts/knowledge/tool-team-ledger-read.md +11 -0
  249. package/prompts/knowledge/tool-team-ledger.md +15 -0
  250. package/prompts/knowledge/tool-timer.md +23 -0
  251. package/prompts/knowledge/tool-tree.md +16 -0
  252. package/prompts/knowledge/tool-verify.md +19 -0
  253. package/prompts/knowledge/tool-wait-for.md +25 -0
  254. package/prompts/knowledge/tool-websearch.md +19 -0
  255. package/prompts/knowledge/tool-write.md +12 -0
  256. package/prompts/knowledge/upstream-protocol.md +45 -0
  257. package/prompts/persona-coder.md +1 -3
  258. package/prompts/persona-eng-coder.md +24 -6
  259. package/prompts/persona-eng-designer.md +150 -171
  260. package/prompts/persona-engineering.md +531 -442
  261. package/prompts/persona-explore.md +1 -3
  262. package/prompts/persona-normal.md +17 -9
  263. package/prompts/persona-plan.md +5 -3
  264. package/prompts/persona-tester.md +0 -2
  265. package/prompts/subagent-base.md +2 -2
  266. package/provider/anthropic.mjs +10 -6
  267. package/provider/core.mjs +8 -5
  268. package/provider/errors.mjs +63 -4
  269. package/provider/google.mjs +29 -10
  270. package/provider/reasoning-echo.mjs +16 -0
  271. package/provider/responses.mjs +7 -3
  272. package/provider/sse.mjs +11 -6
  273. package/provider-flows.mjs +2 -2
  274. package/rules.mjs +11 -10
  275. package/session-index-build.mjs +1 -1
  276. package/session-index-pass.mjs +1 -1
  277. package/session-index-query.mjs +1 -1
  278. package/session-index.mjs +1 -1
  279. package/session-lifecycle.mjs +131 -42
  280. package/session-slot-write.mjs +38 -6
  281. package/session.mjs +30 -11
  282. package/shell-identity.mjs +129 -22
  283. package/skip-dirs.mjs +26 -2
  284. package/team-ledger.mjs +34 -30
  285. package/text-budget.mjs +1 -1
  286. package/think-off.mjs +27 -0
  287. package/token-ttl.mjs +74 -5
  288. package/token-window.mjs +371 -47
  289. package/tool-docs/advisor.md +2 -27
  290. package/tool-docs/apply_patch.md +1 -15
  291. package/tool-docs/bash.md +2 -43
  292. package/tool-docs/batch-eng-coder.md +3 -0
  293. package/tool-docs/batch-eng-designer.md +3 -0
  294. package/tool-docs/batch-review.md +3 -0
  295. package/tool-docs/batch.md +2 -84
  296. package/tool-docs/browser.md +2 -49
  297. package/tool-docs/code_search.md +2 -2
  298. package/tool-docs/consult_start.md +2 -8
  299. package/tool-docs/consult_stop.md +2 -4
  300. package/tool-docs/context.md +1 -5
  301. package/tool-docs/delete.md +2 -17
  302. package/tool-docs/doc_check.md +2 -107
  303. package/tool-docs/doc_graph-read.md +3 -0
  304. package/tool-docs/doc_graph.md +3 -0
  305. package/tool-docs/doc_search.md +2 -4
  306. package/tool-docs/edit.md +1 -38
  307. package/tool-docs/eng.md +3 -1
  308. package/tool-docs/execute.md +2 -18
  309. package/tool-docs/fetch.md +2 -14
  310. package/tool-docs/file_ops.md +2 -18
  311. package/tool-docs/get_current_time.md +1 -6
  312. package/tool-docs/git-read.md +3 -0
  313. package/tool-docs/git.md +2 -35
  314. package/tool-docs/glob.md +2 -10
  315. package/tool-docs/goal.md +1 -6
  316. package/tool-docs/grep.md +2 -18
  317. package/tool-docs/hashline_edit.md +2 -15
  318. package/tool-docs/insert_after.md +2 -16
  319. package/tool-docs/instance_list.md +3 -0
  320. package/tool-docs/instance_send.md +3 -0
  321. package/tool-docs/knowledge.md +3 -0
  322. package/tool-docs/ledger-read.md +3 -0
  323. package/tool-docs/ledger.md +3 -8
  324. package/tool-docs/lint.md +2 -10
  325. package/tool-docs/ls.md +2 -11
  326. package/tool-docs/lsp.md +2 -9
  327. package/tool-docs/main_history.md +3 -1
  328. package/tool-docs/manifest-read.md +2 -8
  329. package/tool-docs/manifest.md +2 -35
  330. package/tool-docs/memory-read.md +3 -0
  331. package/tool-docs/memory.md +2 -20
  332. package/tool-docs/notify_parent.md +2 -13
  333. package/tool-docs/peer_instances.md +3 -1
  334. package/tool-docs/plan.md +1 -5
  335. package/tool-docs/process-read.md +3 -0
  336. package/tool-docs/process.md +2 -13
  337. package/tool-docs/question.md +3 -17
  338. package/tool-docs/read.md +2 -19
  339. package/tool-docs/read_history-batch.md +3 -0
  340. package/tool-docs/read_history.md +2 -18
  341. package/tool-docs/read_image.md +2 -9
  342. package/tool-docs/recent_changes.md +2 -2
  343. package/tool-docs/repo_outline.md +2 -2
  344. package/tool-docs/settings-read.md +3 -0
  345. package/tool-docs/settings.md +1 -12
  346. package/tool-docs/skill.md +2 -6
  347. package/tool-docs/ssh.md +1 -62
  348. package/tool-docs/subagent.md +2 -100
  349. package/tool-docs/task.md +1 -7
  350. package/tool-docs/team_ledger-read.md +3 -0
  351. package/tool-docs/team_ledger.md +3 -9
  352. package/tool-docs/timer.md +2 -19
  353. package/tool-docs/tree.md +2 -13
  354. package/tool-docs/verify.md +2 -12
  355. package/tool-docs/wait_for.md +2 -32
  356. package/tool-docs/websearch.md +2 -19
  357. package/tool-docs/write.md +2 -12
  358. package/tools/browser.mjs +54 -9
  359. package/tools/doc-write-gate.mjs +73 -0
  360. package/tools/edit-batch.mjs +170 -92
  361. package/tools/edit-diff.mjs +78 -62
  362. package/tools/edit-receipt.mjs +39 -0
  363. package/tools/execute.mjs +3 -3
  364. package/tools/file.mjs +28 -14
  365. package/tools/git.mjs +36 -21
  366. package/tools/index.mjs +6 -0
  367. package/tools/knowledge.mjs +143 -0
  368. package/tools/linter.mjs +3 -3
  369. package/tools/ops.mjs +5 -3
  370. package/tools/patch.mjs +124 -44
  371. package/tools/process-doc-mark.mjs +33 -4
  372. package/tools/repomap.mjs +25 -3
  373. package/tools/search.mjs +74 -26
  374. package/tools/shared.mjs +29 -27
  375. package/tools/ssh.mjs +5 -5
  376. package/tools/tree.mjs +32 -10
  377. package/tools/web-deepseek.mjs +1 -1
  378. package/tools/web.mjs +2 -2
  379. package/tools/write-gate-params.mjs +31 -0
  380. package/tools/write-path.mjs +16 -4
  381. package/window-policy.mjs +243 -0
  382. package/doc-check/anchors.mjs +0 -235
package/advisor/run.mjs CHANGED
@@ -6,11 +6,12 @@
6
6
  */
7
7
  import { findProvider } from "../config.mjs"
8
8
  import { prepareAdvisorMessages } from "../advisor.mjs"
9
- import { buildObjectDeclarationBlock, buildDesignApprovalBlock } from "./messages.mjs"
9
+ import { buildObjectDeclarationBlock, buildDesignApprovalBlock, reviewHoldsBatchWrite } from "./messages.mjs"
10
10
  import { appendCitationReport } from "./citations.mjs"
11
11
  import { runAdvisorToolLoop } from "./loop.mjs"
12
12
  import { estimateTokens } from "./compaction.mjs"
13
13
  import { batchDocForReview } from "../agent-tools/batch.mjs"
14
+ import { buildBatchBrief } from "../agent-tools/batch-read.mjs"
14
15
 
15
16
  // 拆分后 import 面(既有导出名逐一保面——re-export;谓词为本批新增)。
16
17
  export { ADVISOR_THINKING_PLACEHOLDER, MAX_RESULT_CHARS, renderTimeline as _renderTimeline } from "./compaction.mjs"
@@ -73,19 +74,22 @@ export function looksLikeReviewOutput(text) {
73
74
 
74
75
  /**
75
76
  * 压缩定锚简报(F13/`ADVISOR-GUARDS.md §2` #3)——**由评审参数构建**(非模型输出):对象声明 / 文档清单 /
76
- * Approval Signal 三锚;重内容(项目指南 / 方法论 / 文档地图)不入 pin(压缩的意义所在)。
77
+ * 批档简报(§2.5——与用户消息同一段文本)/ Approval Signal 三锚;重内容(项目指南 / 方法论 / 文档地图)不入 pin
78
+ * (压缩的意义所在)。
77
79
  * 形态逐字见 `ADVISOR-GUARDS.md §2` #3(首行为机械化重挂说明)。
80
+ * `batchBrief` 不给默认值:它与用户消息那一段必须逐字同值(同一次发起、一处算),默认值就是第二处。
78
81
  */
79
- function buildPinnedBrief(reviewType, documents, object, designToken, designId) {
82
+ function buildPinnedBrief(reviewType, documents, batchBrief, object, designToken, designId, writesSection3) {
80
83
  const docList = Array.isArray(documents) ? documents.filter((d) => typeof d === "string" && d.trim()) : []
81
84
  const parts = ["[review brief — re-attached after context compaction; the original review request is no longer in the context]"]
82
85
  const declaration = buildObjectDeclarationBlock(object)
83
86
  if (declaration) parts.push(declaration.trimEnd())
87
+ if (batchBrief) parts.push(batchBrief.trimEnd())
84
88
  if (docList.length > 0) {
85
89
  parts.push("## Documents to Review")
86
90
  parts.push(docList.map((d) => `- ${d} — Read this file in full`).join("\n"))
87
91
  }
88
- if (reviewType === "design" && designToken) parts.push(buildDesignApprovalBlock(designToken, designId))
92
+ if (reviewType === "design" && designToken) parts.push(buildDesignApprovalBlock(designToken, designId, writesSection3))
89
93
  return parts.join("\n\n")
90
94
  }
91
95
 
@@ -114,7 +118,20 @@ export async function runAdvisorReview(agent, reviewType, callbacks, designToken
114
118
  // Advisor always works in the agent's cwd — scope is defined by paths/documents.
115
119
  const advisorCwd = agent.cwd
116
120
 
117
- const messages = prepareAdvisorMessages(agent, reviewType, designToken, documents, paths, null, object, designId)
121
+ // BATCH-RECORD.md §4.3 批次档写通道的绑定——**与评审轨无关**:携了可读 `batchDoc` 即绑定
122
+ // (判据单源 = `docs/core/design/BATCH-LEDGER-REVIEW-UNIFY.md` §2.15)。同步路径 = 调用方(advisor
123
+ // 工具)传入的 callbacks.batchDoc(本笔发起时点的绑定值);异步路径 = 池条目 `entry.start()` 传入的
124
+ // 同一逐笔快照(条目字段 `batchDoc`,不读活的实例字段)。
125
+ // 工具面批走代码轨,评审员靠这一绑定才拿得到写 §3 的手;按轨截断 ⇒ 那一轨永远没有写通道,
126
+ // `§3` 只能编或永远挂着(用户 2026-11-01 11:43 否掉的那件事)。
127
+ // 绑定先于消息构建算一次:用户消息与定锚简报喂的是同一个判轨值,不是各算一次。
128
+ const boundBatchDoc = batchDocForReview(agent, documents, callbacks)
129
+ const writesSection3 = reviewHoldsBatchWrite(agent, reviewType, boundBatchDoc)
130
+ // 批档简报(`ADVISOR-GUARDS.md` §2.5):**一次算、两处消费**(与 `boundBatchDoc` ∥ `writesSection3` 同款)——
131
+ // 用户消息与压缩定锚简报喂同一个值;未绑定 ⇒ 空串(块不出现,这是调用方算出来的结果,不是签名替他选的默认)。
132
+ const batchBrief = buildBatchBrief(boundBatchDoc, agent.cwd)
133
+
134
+ const messages = prepareAdvisorMessages(agent, reviewType, designToken, documents, batchBrief, paths, null, object, designId, writesSection3)
118
135
 
119
136
  // B 启动断言(fail-closed——`ADVISOR-GUARDS.md §2` #2):设计评审请求内**必须**携带与本次签发 token 精确
120
137
  // 对应的 Approval Signal——构建面补不上就拒绝启动(不发"请回显一个不存在的 token"的请求)。
@@ -132,12 +149,9 @@ export async function runAdvisorReview(agent, reviewType, callbacks, designToken
132
149
  }
133
150
  }
134
151
 
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
152
+ // BATCH-RECORD.md §4.3 批次档写通道(上方的 `boundBatchDoc` ∥ `writesSection3`——本条与用户消息同值)。
139
153
  // F13/`ADVISOR-GUARDS.md §2` #3:压缩定锚简报(评审参数构建——压缩触发时由 compaction 重挂)。
140
- const pinned = buildPinnedBrief(reviewType, documents, object, designToken, designId)
154
+ const pinned = buildPinnedBrief(reviewType, documents, batchBrief, object, designToken, designId, writesSection3)
141
155
 
142
156
  try {
143
157
  // #934(零落笔看门狗——§6.32.6 触发接线②):评审族闸位钩子转发(callbacks 不供 `onStall` ⇒
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,33 +175,42 @@ 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)
186
185
  * @param {string[]|null} [documents] — design review only: explicit list of doc paths to review (passed through to buildAdvisorUserMessage)
186
+ * @param {string} batchBrief — 本次绑定批档的简报块(`buildBatchBrief` 的返回值;未绑定 ⇒ 空串);
187
+ * 紧跟在 `documents` 之后透传给 `buildAdvisorUserMessage`(`ADVISOR-GUARDS.md` §2.5)——**不给默认值**。
187
188
  * @param {string[]|null} [paths] — code review only: explicit list of file/dir paths to review
188
189
  * @param {Object|null} [object] — review-object declaration (AGENT-LOOP-ASYNC-POOL.md §6.18 D-OA1): passed through
189
190
  * to the user-message builders; mechanically injected at the start of every review
190
191
  * round (round 1 design/code + round 2+ follow-up). Absent → legacy behavior.
191
192
  * @param {string|null} [designId] — F2a: injected next to the design token in
192
193
  * the Approval Signal (round 1 + round 2+ — both values, verbatim anchor).
194
+ * @param {boolean} writesSection3 — 本评审实例是否持有 `§3` 写通道(`reviewHoldsBatchWrite` 的取值,由
195
+ * `advisor/run.mjs` 算一次传进来);透传给同档两处 `buildAdvisorUserMessage`。**不给默认值**。
193
196
  */
194
- export function prepareAdvisorMessages(agent, reviewType, designToken = null, documents = null, paths = null, priorParam = null, object = null, designId = null) {
197
+ export function prepareAdvisorMessages(agent, reviewType, designToken = null, documents = null, batchBrief, paths = null, priorParam = null, object = null, designId = null, writesSection3) {
195
198
  // Deterministic convergence state (decision 2026-08-08): round 2+ requires
196
199
  // _advisorRound > 0 AND a stored prior review output. No history parsing.
197
200
  // priorParam (direct callers) wins over the stored output — same derivation
198
201
  // as buildAdvisorSystemPrompt (single source of truth for round semantics).
199
202
  const prior = (agent._advisorRound || 0) > 0 ? (priorParam ?? agent._lastAdvisorOutput) : null
200
203
 
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) {
204
+ // Design review — EVERY round: the dedicated full-scope review, with the
205
+ // approval token re-injected (an async fix-round continuation must be able to
206
+ // re-approve) and the prior review carried as a REFERENCE block, never as the
207
+ // verification list (decision 2026-11-16: a design review has no convergence
208
+ // decay — each round re-reads the whole scope against the complete criteria).
209
+ // Fresh session, exactly like round 1.
210
+ if (reviewType === "design") {
205
211
  return [
206
212
  { role: "system", content: buildAdvisorSystemPrompt(agent, prior, reviewType) },
207
- { role: "user", content: escapeLiteralEscapes(buildAdvisorUserMessage(agent, prior, reviewType, designToken, documents, paths, object, designId)) },
213
+ { role: "user", content: escapeLiteralEscapes(buildAdvisorUserMessage(agent, prior, reviewType, designToken, documents, batchBrief, paths, object, designId, writesSection3)) },
208
214
  ]
209
215
  }
210
216
 
@@ -213,7 +219,7 @@ export function prepareAdvisorMessages(agent, reviewType, designToken = null, do
213
219
  // re-review false reports (the model quoted pre-fix file content instead of
214
220
  // re-reading) and a token sink. The agent response table (fix claims) is
215
221
  // injected through buildAdvisorFollowUp instead; the system prompt carries
216
- // the round (ROUND2/ROUND3) via buildAdvisorSystemPrompt.
222
+ // the round (ROUND2/ROUND3, code review only) via buildAdvisorSystemPrompt.
217
223
  // No prior review output: reset ONLY when this run made no code changes (user
218
224
  // decision 2026-08-05: any loop that modified code must NOT reset — the
219
225
  // advisor guard WILL push back, so the convergence round must keep advancing
@@ -230,7 +236,7 @@ export function prepareAdvisorMessages(agent, reviewType, designToken = null, do
230
236
  agent._advisorRound = 0
231
237
  }
232
238
  // Mutations exist → KEEP the round (attempts keep advancing through retries).
233
- const user = buildAdvisorUserMessage(agent, prior, reviewType, designToken, documents, paths, object, designId)
239
+ const user = buildAdvisorUserMessage(agent, prior, reviewType, designToken, documents, batchBrief, paths, object, designId, writesSection3)
234
240
  return [
235
241
  { role: "system", content: buildAdvisorSystemPrompt(agent, prior, reviewType) },
236
242
  {
@@ -260,22 +266,6 @@ export function prepareAdvisorMessages(agent, reviewType, designToken = null, do
260
266
  // review surface.
261
267
  const scopeFiles = resolveScopeFiles(agent, paths)
262
268
  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
269
  return [
280
270
  { role: "system", content: buildAdvisorSystemPrompt(agent, prior, reviewType) },
281
271
  { 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
+ }
@@ -0,0 +1,129 @@
1
+ /**
2
+ * agent/batch-changed-surface.mjs — 收口档位臂取材面的判据一族(从 `agent-tools/batch-closeout.mjs` 拆出):
3
+ * 比较键 `surfaceKey` ∥ 显示式 `displayRelOf` ∥ 三处判据 `selfAttributedOf` / `sessionClaimsOf` / `neighborListClaims`。
4
+ *
5
+ * 语义单源 = `docs/core/design/BATCH-RECORD.md` §4.18.1(本批改动面的取材:写时归属作判据一 ∥ 提交 diff 按认领收窄);
6
+ * 本档只实现它,不重述口径。
7
+ *
8
+ * 依赖单向:`agent-tools/batch-closeout.mjs` → 本档——入口 `changedSurface` 与回执段 `changedSurfaceReceiptLine`
9
+ * 留在那里;本档不 import 它,两档不成环。比较键随判据一族住在这里:三处判据都用它,留在入口侧就要反向 import。
10
+ *
11
+ * 本档不是无依赖那一形(同族的 `batch-attribution.mjs` 才是「零 fs、零 import」):认领的计划源本来就要读盘、
12
+ * 要问 git——`batchDirs` ∥ `machineListRowsOf` ∥ `baseShaOf` ∥ `committedDocs` 都在它的取材路径上。
13
+ */
14
+ import { readdirSync, readFileSync } from "node:fs"
15
+ import { isAbsolute, relative, resolve } from "node:path"
16
+
17
+ import { owningProject } from "../manifest.mjs"
18
+ import { batchDirs } from "../doc-check/existence.mjs"
19
+ import { baseShaOf } from "../agent-tools/batch-gate.mjs"
20
+ import { committedDocs, committedDocsUpto, lastCommitOf } from "../agent-tools/batch-regions.mjs"
21
+ import { isBatchRecord, machineListRowsOf, readBatchStatusLine } from "../agent-tools/batch-segment-vocab.mjs"
22
+ import { attributedTo } from "./batch-attribution.mjs"
23
+
24
+ /** 比较键(绝对路径归一:`/` 分隔符 + win32 折小写)——归属表、认领表与改动面条目三处共用一把尺。 */
25
+ export function surfaceKey(p) {
26
+ const s = resolve(String(p ?? "")).replace(/\\/g, "/")
27
+ return process.platform === "win32" ? s.toLowerCase() : s
28
+ }
29
+
30
+ /** 显示用的仓根相对路径(**不折大小写**:折小写是比较键的事,回执上印真名;仓外 ⇒ 照原值)。 */
31
+ export function displayRelOf(root, p) {
32
+ const raw = String(p ?? "").replace(/\\/g, "/")
33
+ if (typeof root !== "string" || root === "") return raw
34
+ const rel = relative(resolve(root), resolve(p)).replace(/\\/g, "/")
35
+ return rel === "" || rel.startsWith("../") || isAbsolute(rel) ? raw : rel
36
+ }
37
+
38
+ /** 归属表里归属**含本档**的路径集(比较键)。精确取件走叶档的 `attributedTo`,再按归一键补一遍:
39
+ * 表键与批档串在分隔符 ∥ 大小写上可能不同形,而判据一必须比认领更硬——少判的那一侧只许来自认领。 */
40
+ export function selfAttributedOf(agent, selfAbs) {
41
+ const mine = new Set(attributedTo(agent, selfAbs).map(surfaceKey))
42
+ const table = agent?._batchAttribution
43
+ if (!(table instanceof Map)) return mine
44
+ const self = surfaceKey(selfAbs)
45
+ for (const [batch, paths] of table) {
46
+ if (surfaceKey(batch) !== self) continue
47
+ for (const p of paths) mine.add(surfaceKey(p))
48
+ }
49
+ return mine
50
+ }
51
+
52
+ /** 本会话归属表里**别的批**的路径集(认领的观测源)。
53
+ * 不按批档状态筛:归属记的是「哪一批写了这一档」这件已发生的事,那一批收口之后这个事实不变——
54
+ * 同会话并行的两批,后收口的一方靠这一源减掉先收口那一方的档(正是「各自收口不为对方档要行」的实现面)。 */
55
+ export function sessionClaimsOf(agent, selfAbs) {
56
+ const table = agent?._batchAttribution
57
+ const claims = new Set()
58
+ if (!(table instanceof Map)) return claims
59
+ const self = surfaceKey(selfAbs)
60
+ for (const [batch, paths] of table) {
61
+ if (surfaceKey(batch) === self) continue
62
+ for (const p of paths) claims.add(surfaceKey(p))
63
+ }
64
+ return claims
65
+ }
66
+
67
+ /** 盘上**别的批档**已认领的路径集(认领的计划源),`Map<比较键, 批档绝对路径>`。
68
+ *
69
+ * 清单只是**计划面**——它声明打算改哪些档,不证明真改了;单凭计划认领,一条永不兑现的计划就能把一档从
70
+ * 每一批的判据里摘出去。故本源另加三条(缺一不认领):
71
+ * ① 该档确在**那份批档自己的**区间之内(`baseShaOf` 取那份批档的 sha,`committedDocs` ∥ `committedDocsUpto` 给档清单);
72
+ * ② 那份批档与本次收口**同项目根**——跨项目同名的相对路径不认领;
73
+ * ③ 那份批档**状态可读**(`readBatchStatusLine` 三值):收口不改变归属事实,所以已收口的批同样靠本源认领,
74
+ * 缺的只是上界——上界随状态定:`open` ⇒ `HEAD`(`committedDocs`,答「目前改到哪」),`closed` ⇒ 那份批档
75
+ * 自己的最后一次改动提交(`lastCommitOf` + `committedDocsUpto`),取不到上界就不认领。`unknown`(无状态行 ∥
76
+ * 两条以上 ∥ 两词皆不命中)与写锁同口按冻结处置:状态不明的一方不给认领权(不明不等于未收口)。
77
+ * 为什么 `closed` 不能也取 HEAD:收口批的区间会一直往后长,「该档在它的区间之内」对它几乎恒真 ⇒ 计划面变回
78
+ * 所有权,正是①要防的静默放过;钉死上界之后,认领面退回「那一批确实改过的档」。
79
+ * **失败面**:本函数逐条链批各算一次,那几笔读不出——`baseShaOf` 回 `null`、两函数回 `ok: false`、
80
+ * `lastCommitOf` 回 `null`——一律 `continue` 按「认领不成立」处置:那一档照本批判,本批收口照跑。
81
+ * 拒收口那一条只属 `changedSurface` 自己那一笔 diff(邻批的缺陷不得拦本批的收口)。
82
+ * 抽取面 = `machineListRowsOf`(机读清单读取)+ `batchDirs`(批档目录):按「哪一份批档列了哪一档」取,
83
+ * 不取跨批合并后的路径集(并集给不出①要的那份批档自己的批起 sha)。 */
84
+ export function neighborListClaims(root, selfAbs) {
85
+ const claims = new Map()
86
+ const self = surfaceKey(selfAbs)
87
+ const base = resolve(root)
88
+ for (const dir of batchDirs(base)) {
89
+ let names
90
+ try { names = readdirSync(dir) } catch { continue }
91
+ for (const name of names) {
92
+ if (!name.toLowerCase().endsWith(".md")) continue
93
+ const abs = resolve(dir, name)
94
+ if (surfaceKey(abs) === self) continue
95
+ let src
96
+ try { src = readFileSync(abs, "utf8") } catch { continue }
97
+ // 认领面只收**可读状态**的批档:收口批靠它自己的区间上界认领,`unknown`(无状态行 ∥ 两条以上 ∥ 两词皆不命中)
98
+ // 与写锁同口按冻结处置——状态不明的一方不给认领权(不明不等于未收口,认领是往「少判」方向倒的那一条)。
99
+ if (!isBatchRecord(src)) continue
100
+ const status = readBatchStatusLine(src)
101
+ if (status === "unknown") continue
102
+ const rows = machineListRowsOf(src)
103
+ if (rows.length === 0) continue
104
+ const owned = owningProject(abs, base)
105
+ if (!owned || surfaceKey(owned) !== surfaceKey(base)) continue // ②(取不到根 ⇒ 不认领)
106
+ const sha = baseShaOf(abs)
107
+ if (sha === null) continue // 没有它自己的批起 sha ⇒ 观测不成立
108
+ // 区间上界随状态定:未收口 ⇒ HEAD(右端继续往后长是对的,那一批还在写);
109
+ // 已收口 ⇒ 它自己的最后一次提交(钉死上界,否则「该档在它区间内」对它几乎恒真 ⇒ 计划面变所有权)。
110
+ let committed
111
+ if (status === "closed") {
112
+ const upper = lastCommitOf(base, abs)
113
+ if (upper === null) continue // 上界取不到 ⇒ 不认领(fail-closed:那一档照本批判)
114
+ committed = committedDocsUpto(base, sha, upper)
115
+ } else {
116
+ committed = committedDocs(base, sha)
117
+ }
118
+ if (!committed.ok) continue // 这一链读不出 ⇒ 不认领、本批照跑(拒收口只属本批自己的 diff 腿)
119
+ // 认领键与改动面条目同一坐标系(绝对路径归一)——两侧共用 `surfaceKey` 一把尺。
120
+ const changed = new Set()
121
+ for (const f of committed.files ?? []) changed.add(surfaceKey(resolve(committed.base, f)))
122
+ for (const row of rows) {
123
+ const key = surfaceKey(resolve(base, row.path))
124
+ if (changed.has(key)) claims.set(key, abs) // ①
125
+ }
126
+ }
127
+ }
128
+ return claims
129
+ }
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import { chat } from "../provider/index.mjs"
8
8
  import { healImageRejection, clearImageFailStreak } from "./image-heal.mjs"
9
+ import { assembleRequestMessages, pushInject, pushRecord } from "../context.mjs"
9
10
 
10
11
  /** 流式输出门(§2.5 #78 并入——VSC 三态门,纯函数可直驱测试):depth 0 恒通;
11
12
  * consult 子代理豁免(其输出进会诊面板);其余经 `streamOutput` 显式选择进入。 */
@@ -16,14 +17,16 @@ export function streamOutputAllowed(depth, role, streamOutput = false) {
16
17
  export async function callModelTurn(agent, {
17
18
  systemPrompt, toolSchemas, streamOutput, callbacks, signal, streamRuleFired, autoTurn, depth, turn,
18
19
  }) {
19
- const messages = [{ role: "system", content: systemPrompt }, ...agent.history]
20
- let response
21
-
22
20
  // Auto-think: classify difficulty and set reasoning effort on turn 0; silent on failure.
21
+ // 分类与应用必须先于首次正式请求装配:assembleRequestMessages 要读分类后的 reasoningEffort。
23
22
  if (agent.config?.agent?.autoThink && turn === 0) {
24
23
  const { classifyAndApply } = await import("../auto-think.mjs")
25
24
  await classifyAndApply(agent, turn).catch(() => {})
26
25
  }
26
+ const toolsDeclared = Array.isArray(toolSchemas) && toolSchemas.length > 0
27
+ // 首送与图片自愈重发共用同一请求构造形,工具声明判据也来自同一份 toolSchemas。
28
+ const messages = assembleRequestMessages(agent, { systemPrompt, toolsDeclared })
29
+ let response
27
30
 
28
31
  // 发送选项(首送 ∥ §6.26 自愈重发共用——两送除 messages 外逐字同参)
29
32
  const sendOpts = {
@@ -60,19 +63,21 @@ export async function callModelTurn(agent, {
60
63
  // Inject into history; the outer loop recreates the controller and resumes.
61
64
  if (e.name === "AbortError" && signal?.reason?.interrupt) {
62
65
  const msg = `[User interrupt: ${signal.reason.message}]`
63
- // Dedup: if already handled during tool execution (interrupt branch below),
64
- // don't push a duplicate — the outer loop still recreates the controller.
65
- if (agent.history.at(-1)?.content !== msg) {
66
- agent.history.push({ role: "user", content: msg })
66
+ // 去重:工具执行中断那一支已投过同一条时不再入队。判据读待发队尾条——
67
+ // 注入不在 `agent.history` 上(嵌套线上 `.content` 恒 `undefined`,读它去重恒不命中)。
68
+ const queue = Array.isArray(agent._pendingInject) ? agent._pendingInject : []
69
+ if (queue.at(-1)?.content !== msg) {
70
+ pushInject(agent, { role: "user", content: msg })
71
+ pushRecord(agent, { role: "user", content: msg })
67
72
  }
68
73
  }
69
74
  // §6.26 图片拒绝自愈(判定 ∥ 剥除 ∥ 注记 ∥ 记账 = agent/image-heal.mjs 单源):
70
75
  // 未愈 ⇒ 原错照抛(非图像错误零误伤);已愈 ⇒ 载荷已修(历史无图 + 机读注记)单次重发。
71
76
  const { healed } = healImageRejection(agent, e)
72
77
  if (!healed) throw e
73
- // 重建消息(§6.26 步 4——剥除后的 agent.history 含注记):再发一次(非重试——同请求
78
+ // 重建消息(§6.26 步 4——剥除后的机读线含自愈注记):再发一次(非重试——同请求
74
79
  // 重发对图像拒绝恒无效);再败 ⇒ 本行抛新错(不循环、不二次剥除,历史保持已剥除态)。
75
- response = await chat(agent.provider, { messages: [{ role: "system", content: systemPrompt }, ...agent.history], ...sendOpts })
80
+ response = await chat(agent.provider, { messages: assembleRequestMessages(agent, { systemPrompt, toolsDeclared }), ...sendOpts })
76
81
  }
77
82
  // §6.26 清零调用点(记账单源 = image-heal.mjs):任一次成功调用清零(含自愈重发成功)
78
83
  clearImageFailStreak(agent)