@thincoder/core 0.11.3 → 0.11.5

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 (183) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/advisor/compaction.mjs +40 -17
  3. package/advisor/loop.mjs +14 -6
  4. package/advisor/notice.mjs +1 -1
  5. package/advisor/timeline.mjs +4 -4
  6. package/agent/completion.mjs +19 -0
  7. package/agent/dispatch-gates.mjs +2 -0
  8. package/agent/dispatch.mjs +2 -2
  9. package/agent/family-tools.mjs +13 -9
  10. package/agent/helpers.mjs +10 -6
  11. package/agent/planning-step.mjs +453 -0
  12. package/agent/post-turn.mjs +70 -2
  13. package/agent/run-stages.mjs +22 -50
  14. package/agent/run-start.mjs +17 -0
  15. package/agent/setup-reminders.mjs +71 -8
  16. package/agent/setup.mjs +8 -39
  17. package/agent/spawn-child.mjs +6 -2
  18. package/agent/task-lifecycle.mjs +146 -0
  19. package/agent/turn-loop.mjs +73 -11
  20. package/agent-tools/batch-closeout.mjs +21 -34
  21. package/agent-tools/batch-gate.mjs +7 -5
  22. package/agent-tools/batch-ledger-bind.mjs +21 -1
  23. package/agent-tools/batch-lifecycle.mjs +7 -7
  24. package/agent-tools/batch-record-write.mjs +237 -280
  25. package/agent-tools/batch-supersede.mjs +3 -3
  26. package/agent-tools/batch.mjs +42 -9
  27. package/agent-tools/context.mjs +0 -13
  28. package/agent-tools/escalate-async.mjs +7 -1
  29. package/agent-tools/parent-channel.mjs +7 -6
  30. package/agent-tools/plan.mjs +103 -67
  31. package/agent-tools/review-facts.mjs +3 -3
  32. package/agent-tools/settings.mjs +2 -0
  33. package/agent-tools/spawn-gates.mjs +37 -11
  34. package/agent-tools/ssh-session.mjs +68 -25
  35. package/agent-tools/ssh-transfer.mjs +7 -2
  36. package/agent-tools/ssh-transport.mjs +2 -2
  37. package/agent-tools/ssh-vault.mjs +2 -2
  38. package/agent-tools/step.mjs +462 -0
  39. package/agent-tools/subagent-actions.mjs +6 -1
  40. package/agent-tools/subagent-run.mjs +7 -0
  41. package/agent-tools/subagent-spawn.mjs +1 -1
  42. package/agent-tools/subagent.mjs +17 -11
  43. package/agent-tools/task.mjs +178 -26
  44. package/agent-tools.mjs +1 -0
  45. package/agent.mjs +2 -0
  46. package/config-defaults.mjs +17 -1
  47. package/config-migrate.mjs +8 -5
  48. package/config.mjs +7 -0
  49. package/context.mjs +5 -4
  50. package/declaration.mjs +4 -4
  51. package/generate-title.mjs +0 -6
  52. package/i18n.mjs +4 -0
  53. package/ledger-cmd.mjs +11 -1
  54. package/ledger-tools.mjs +15 -4
  55. package/manifest-schema.mjs +1 -0
  56. package/model-context.mjs +34 -11
  57. package/model-specs.mjs +5 -2
  58. package/package.json +1 -1
  59. package/prompt-files.mjs +48 -11
  60. package/prompt-overlays.mjs +79 -8
  61. package/prompts/advisor-design.md +253 -206
  62. package/prompts/advisor-round1.md +151 -145
  63. package/prompts/advisor-round2.md +136 -130
  64. package/prompts/advisor-round3.md +140 -134
  65. package/prompts/common.md +244 -96
  66. package/prompts/consult-base.md +29 -30
  67. package/prompts/discipline-engineering.md +73 -519
  68. package/prompts/discipline-normal.md +28 -136
  69. package/prompts/knowledge/batch-before-write.md +8 -0
  70. package/prompts/knowledge/batch-cycle-register.md +15 -0
  71. package/prompts/knowledge/batch-progress-ledger.md +29 -0
  72. package/prompts/knowledge/business-gates.md +24 -24
  73. package/prompts/knowledge/decision-quality.md +8 -19
  74. package/prompts/knowledge/doc-discipline-d-rules.md +63 -0
  75. package/prompts/knowledge/doc-lookup.md +25 -24
  76. package/prompts/knowledge/document-writing.md +11 -32
  77. package/prompts/knowledge/documentation-evaluation.md +42 -42
  78. package/prompts/knowledge/eng-dispatch-ops.md +34 -0
  79. package/prompts/knowledge/eng-ledger-ops.md +26 -0
  80. package/prompts/knowledge/evidence-discipline.md +10 -32
  81. package/prompts/knowledge/normal-batch-discipline.md +13 -0
  82. package/prompts/knowledge/normal-coding-workflow.md +97 -99
  83. package/prompts/knowledge/normal-delivery-collaboration.md +107 -112
  84. package/prompts/knowledge/normal-work-management.md +44 -44
  85. package/prompts/knowledge/product-security.md +7 -26
  86. package/prompts/knowledge/project-records.md +69 -64
  87. package/prompts/knowledge/tool-advisor.md +17 -17
  88. package/prompts/knowledge/tool-apply-patch.md +14 -14
  89. package/prompts/knowledge/tool-bash.md +32 -31
  90. package/prompts/knowledge/tool-batch-eng-coder.md +19 -13
  91. package/prompts/knowledge/tool-batch-eng-designer.md +21 -14
  92. package/prompts/knowledge/tool-batch-review.md +18 -12
  93. package/prompts/knowledge/tool-batch.md +93 -85
  94. package/prompts/knowledge/tool-browser.md +59 -59
  95. package/prompts/knowledge/tool-code-search.md +2 -2
  96. package/prompts/knowledge/tool-consult-start.md +6 -6
  97. package/prompts/knowledge/tool-consult-stop.md +3 -3
  98. package/prompts/knowledge/tool-context.md +4 -4
  99. package/prompts/knowledge/tool-delete.md +16 -16
  100. package/prompts/knowledge/tool-doc-check.md +39 -39
  101. package/prompts/knowledge/tool-doc-graph-read.md +11 -11
  102. package/prompts/knowledge/tool-doc-graph.md +48 -48
  103. package/prompts/knowledge/tool-doc-search.md +3 -3
  104. package/prompts/knowledge/tool-edit.md +30 -30
  105. package/prompts/knowledge/tool-eng.md +1 -1
  106. package/prompts/knowledge/tool-execute.md +16 -16
  107. package/prompts/knowledge/tool-fetch.md +15 -15
  108. package/prompts/knowledge/tool-file-ops.md +15 -15
  109. package/prompts/knowledge/tool-get-current-time.md +3 -3
  110. package/prompts/knowledge/tool-git-read.md +15 -15
  111. package/prompts/knowledge/tool-git.md +26 -26
  112. package/prompts/knowledge/tool-glob.md +14 -14
  113. package/prompts/knowledge/tool-goal.md +5 -5
  114. package/prompts/knowledge/tool-grep.md +19 -18
  115. package/prompts/knowledge/tool-hashline-edit.md +13 -13
  116. package/prompts/knowledge/tool-insert-after.md +14 -14
  117. package/prompts/knowledge/tool-instance-list.md +3 -3
  118. package/prompts/knowledge/tool-instance-send.md +5 -5
  119. package/prompts/knowledge/tool-knowledge.md +12 -12
  120. package/prompts/knowledge/tool-ledger-read.md +20 -20
  121. package/prompts/knowledge/tool-ledger.md +44 -44
  122. package/prompts/knowledge/tool-lint.md +10 -10
  123. package/prompts/knowledge/tool-ls.md +11 -11
  124. package/prompts/knowledge/tool-lsp.md +8 -8
  125. package/prompts/knowledge/tool-main-history.md +1 -1
  126. package/prompts/knowledge/tool-manifest-read.md +7 -7
  127. package/prompts/knowledge/tool-manifest.md +43 -32
  128. package/prompts/knowledge/tool-memory-read.md +12 -12
  129. package/prompts/knowledge/tool-memory.md +14 -14
  130. package/prompts/knowledge/tool-notify-parent.md +10 -10
  131. package/prompts/knowledge/tool-operations.md +88 -88
  132. package/prompts/knowledge/tool-peek.md +10 -0
  133. package/prompts/knowledge/tool-peer-instances.md +1 -1
  134. package/prompts/knowledge/tool-plan.md +6 -3
  135. package/prompts/knowledge/tool-process-read.md +8 -8
  136. package/prompts/knowledge/tool-process.md +11 -11
  137. package/prompts/knowledge/tool-question.md +11 -13
  138. package/prompts/knowledge/tool-read-history-batch.md +13 -13
  139. package/prompts/knowledge/tool-read-history.md +19 -19
  140. package/prompts/knowledge/tool-read-image.md +8 -8
  141. package/prompts/knowledge/tool-read.md +17 -17
  142. package/prompts/knowledge/tool-recent-changes.md +4 -4
  143. package/prompts/knowledge/tool-repo-outline.md +2 -2
  144. package/prompts/knowledge/tool-settings-read.md +5 -7
  145. package/prompts/knowledge/tool-settings.md +10 -10
  146. package/prompts/knowledge/tool-skill.md +4 -4
  147. package/prompts/knowledge/tool-ssh.md +66 -66
  148. package/prompts/knowledge/tool-step.md +26 -0
  149. package/prompts/knowledge/tool-subagent.md +52 -50
  150. package/prompts/knowledge/tool-task.md +14 -11
  151. package/prompts/knowledge/tool-team-ledger-read.md +7 -7
  152. package/prompts/knowledge/tool-team-ledger.md +13 -13
  153. package/prompts/knowledge/tool-timer.md +14 -14
  154. package/prompts/knowledge/tool-tree.md +11 -11
  155. package/prompts/knowledge/tool-verify.md +11 -11
  156. package/prompts/knowledge/tool-wait-for.md +20 -20
  157. package/prompts/knowledge/tool-websearch.md +16 -14
  158. package/prompts/knowledge/tool-write.md +10 -10
  159. package/prompts/knowledge/upstream-protocol.md +37 -38
  160. package/prompts/persona-coder.md +22 -22
  161. package/prompts/persona-eng-coder.md +87 -81
  162. package/prompts/persona-eng-designer.md +173 -153
  163. package/prompts/persona-engineering.md +53 -554
  164. package/prompts/persona-explore.md +18 -15
  165. package/prompts/persona-normal.md +62 -63
  166. package/prompts/persona-plan.md +36 -32
  167. package/prompts/persona-tester.md +39 -39
  168. package/prompts/planning-next-moves.md +9 -0
  169. package/prompts/subagent-base.md +4 -16
  170. package/provider/errors.mjs +0 -24
  171. package/provider/list-models.mjs +20 -2
  172. package/session-lifecycle.mjs +4 -3
  173. package/tool-args.mjs +25 -0
  174. package/tool-docs/peek.md +3 -0
  175. package/tool-docs/plan.md +1 -1
  176. package/tool-docs/question.md +1 -1
  177. package/tool-docs/step.md +3 -0
  178. package/tools/edit-diff.mjs +4 -4
  179. package/tools/index.mjs +3 -2
  180. package/tools/peek.mjs +447 -0
  181. package/tools/question.mjs +87 -9
  182. package/tools/ssh.mjs +1 -1
  183. package/window-policy.mjs +107 -80
@@ -17,9 +17,17 @@ function pushAssistantText(agent, response) {
17
17
  }
18
18
  // 归属表(收口取材的判据一):中断分支的记账点与常路同笔——两处 `_touchedFiles.push` 都写归属。
19
19
  import { recordAttribution } from "./batch-attribution.mjs"
20
- import { injectPostTurn } from "./post-turn.mjs"
20
+ import { injectPostTurn, createWebSearchNudgeState } from "./post-turn.mjs"
21
+ import {
22
+ resolvePlanningConfig,
23
+ createPlanningState,
24
+ pickPlanningTrigger,
25
+ recordPlanningTurnOutcome,
26
+ runPlanningStep,
27
+ } from "./planning-step.mjs"
21
28
  import { clearTurnDomainIfEmpty, clearUpstreamDomainIfEmpty } from "./turn-domain-text.mjs"
22
29
  import { handleCompletion, isSubstantiveResponse } from "./completion.mjs"
30
+ import { clearCompletedTasks } from "./task-lifecycle.mjs"
23
31
  import { runCompactionCheck, enforceContextFit, preflightWindowFit, injectTurnReminders, injectResponseReminders } from "./run-stages.mjs"
24
32
  import { recordTokenSample, faceEstimate } from "../token-window.mjs"
25
33
  import { callModelTurn } from "./chat-call.mjs"
@@ -35,14 +43,6 @@ export async function runTurnLoop(agent, {
35
43
  depth, signal, autoTurn, upstreamTurn, suspDriven, streamOutput,
36
44
  consumeInjected, consumeQueuedInput, callbacks, distillSignal,
37
45
  }) {
38
- let guardPushbacks = 0
39
- let advisorPushbacks = 0
40
- let honestReminderInjected = false
41
- const recentCallSigs = []
42
- // "once" stream rules fire at most once per runAgent call; the set survives across
43
- // chat() calls (rule abort-retry, tool loop) within the turn.
44
- const streamRuleFired = new Set()
45
-
46
46
  // Compaction overhead for the pure-estimation path: system prompt + tools schema are
47
47
  // in every request but not in history — without them the first-turn/just-compacted
48
48
  // estimate under-counts and may never trigger. Measured path already includes both.
@@ -57,6 +57,52 @@ export async function runTurnLoop(agent, {
57
57
  // `context` 工具的 stats 报**同一口径**(不自行重算第二口径;VSC checkAndCompact 同款暂存)。
58
58
  agent._ctxBasis = { threshold, overhead: compactionOverhead }
59
59
 
60
+ // `step` 分叉微步上下文快照(`STEP-FORK.md` §2.1):在环运行期间暴露当轮字节级一致的
61
+ // `systemPrompt` / `toolSchemas` / `toolByName` / `threshold` 引用,环退出时无条件还原。
62
+ const prevStepCtx = agent._stepContext
63
+ agent._stepContext = { systemPrompt, toolSchemas, toolByName, threshold }
64
+ try {
65
+ return await runTurnLoopBody(agent, {
66
+ maxTurns, threshold, toolSchemas, toolByName, systemPrompt,
67
+ depth, signal, autoTurn, upstreamTurn, suspDriven, streamOutput,
68
+ consumeInjected, consumeQueuedInput, callbacks, distillSignal,
69
+ compactionOverhead,
70
+ })
71
+ } finally {
72
+ agent._stepContext = prevStepCtx
73
+ }
74
+ }
75
+
76
+ async function runTurnLoopBody(agent, {
77
+ maxTurns, threshold, toolSchemas, toolByName, systemPrompt,
78
+ depth, signal, autoTurn, upstreamTurn, suspDriven, streamOutput,
79
+ consumeInjected, consumeQueuedInput, callbacks, distillSignal,
80
+ compactionOverhead,
81
+ }) {
82
+ let guardPushbacks = 0
83
+ let advisorPushbacks = 0
84
+ let honestReminderInjected = false
85
+ const recentCallSigs = []
86
+ const streamRuleFired = new Set()
87
+ const searchNudgeState = createWebSearchNudgeState()
88
+ const deliveredAdvisorTypes = Array.isArray(agent._deliveredAdvisorReviews)
89
+ ? agent._deliveredAdvisorReviews.map((r) => (r?.reviewType === "design" || r?.reviewType === "code" ? r.reviewType : "unknown"))
90
+ : []
91
+ agent._deliveredAdvisorReviews = []
92
+ const planningCfg = resolvePlanningConfig(agent, {
93
+ depth, autoTurn, upstreamTurn, mode: agent.config?.agent?.mode ?? "normal",
94
+ hasAdvisorDelivery: deliveredAdvisorTypes.length > 0,
95
+ })
96
+ const planningState = createPlanningState({ advisorReviewTypes: deliveredAdvisorTypes })
97
+ const lastInitial = flattenHistory(agent.history).at(-1)
98
+ const initialUserInput = lastInitial?.role === "user"
99
+ ? (typeof lastInitial.content === "string"
100
+ ? lastInitial.content
101
+ : Array.isArray(lastInitial.content)
102
+ ? lastInitial.content.map((p) => (typeof p?.text === "string" ? p.text : "")).join("\n")
103
+ : "")
104
+ : ""
105
+
60
106
  // 切换预判(`CONTEXT-COMPACTION.md` §5.2):run 装配之后、第一个回合之前——模型切换 ∥ `providers[].context`
61
107
  // 调小 ∥ `reasoningEffort` 变更这三条来路都让 `limit` 变小,而 `agent.history` 一条没少。预判点因此就是
62
108
  // 装备点(参数按 run 现算),一处覆盖三条来路,不必为每条各立钩子。越线 ⇒ 跑与安全点同一条处置链。
@@ -165,12 +211,23 @@ export async function runTurnLoop(agent, {
165
211
  if (lastRole === "user" || lastRole === "tool") {
166
212
  // 2026-09-05 实践轮:压缩检查/降级计数提为 runCompactionCheck(agent/run-stages.mjs——
167
213
  // CLI 对位 VS run-stages)——循环骨架此处只剩检查调用(recentCallSigs 对象引用回流)。
168
- await runCompactionCheck(agent, { threshold, callbacks, compactionOverhead, signal, recentCallSigs })
214
+ const compacted = await runCompactionCheck(agent, { threshold, callbacks, compactionOverhead, signal, recentCallSigs })
215
+ if (compacted) planningState.justCompacted = true
169
216
  }
170
217
 
171
218
  // 2026-09-05 实践轮:plan cadence + eng 状态注入提为 injectTurnReminders(run-stages)
172
219
  await injectTurnReminders(agent, { depth })
173
220
 
221
+ // Harness 独立「Planning next moves」规划步(AGENT-LOOP.md §7.3):按触发节点插入 1 轮受控规划,
222
+ // 默认复用前缀缓存 + 不设 toolChoice:"none",输出流向三端 Planning next moves 块并经 B 段注入下轮执行。
223
+ const planTrigger = pickPlanningTrigger(planningState, planningCfg, { turn, input: initialUserInput })
224
+ if (planTrigger) {
225
+ await runPlanningStep(agent, {
226
+ trigger: planTrigger, cfg: planningCfg, state: planningState,
227
+ systemPrompt, toolSchemas, toolByName, callbacks, signal, depth, turn, input: initialUserInput,
228
+ })
229
+ }
230
+
174
231
  const response = await callTurn({
175
232
  systemPrompt, toolSchemas, streamOutput, callbacks, signal, streamRuleFired, autoTurn, depth, turn,
176
233
  })
@@ -276,6 +333,8 @@ export async function runTurnLoop(agent, {
276
333
  advisorPushbacks = cr.advisorPushbacks
277
334
  if (cr.action === "continue") continue
278
335
  if (depth === 0) {
336
+ // §7.4(AGENT-LOOP.md):正常收尾时全员 `done` 的任务清单自动清空并经 `_onTaskUpdate([])` 同步四端,不留陈旧面板。
337
+ clearCompletedTasks(agent)
279
338
  // End-of-run exploration distillation (CONTEXT-COMPACTION §5 + SEND-STALL-DISTILL
280
339
  // §2.1): async — the promise hangs on _pendingDistill, settling at the next run's
281
340
  // start or the TUI exit flush. Silent (N3): failure never blocks return/history.
@@ -361,7 +420,10 @@ export async function runTurnLoop(agent, {
361
420
  // Commit tool results (pairing, multimodal deferral, mutation accounting, reindex)
362
421
  await recordToolResults(agent, toolByName, results)
363
422
 
364
- injectPostTurn(agent, results, recentCallSigs, callbacks, turn, { suspDriven })
423
+ if (planningCfg) recordPlanningTurnOutcome(planningState, { results, toolByName })
424
+ injectPostTurn(agent, results, recentCallSigs, callbacks, turn, {
425
+ suspDriven, depth, toolByName, searchNudgeState: planningCfg ? null : searchNudgeState,
426
+ })
365
427
  }
366
428
 
367
429
  // #417(撞帽载荷「本段零落盘轮数」):收尾轮(撞帽轮)单结——与环内同一谓词(不另作 alert ∥ streak
@@ -6,23 +6,23 @@
6
6
  *
7
7
  * 本档承载三件(都不碰 fs 写、只读与判):
8
8
  * ① `assertCloseoutGates` —— 收口两路(`close` ∥ `status` 写 `§1 已收口`)共调的判序:
9
- * 链完整性门(§2/§3/§4/§6 各段须有内容——`不适用` 声明机制已退役)→ **`§3` 状态词臂**(无条件 = `评审通过`)
9
+ * 链完整性门(§2/§3/§4/§6 各段须有内容——`不适用` 声明机制已退役)→ **末轮块裁定臂**(末轮轮次块自己携 pass 裁定;无块即无 pass,照拒)
10
10
  * → 本批批内单测档门(`TESTING.md` §3.5)→ 文档机检门(判据 = `docs/core/design/DOC-CHECK.md` §1–§6;
11
11
  * 调用点 = 同档 §7「落点」三处之一「批档写入与收口」)→ **档位臂**
12
12
  * (`batch-tier-gate.mjs` 的 `assertTierPlan`——一支判据、一条拒文:本批改动面里实测越了收口线的在盘
13
13
  * 码档,§2 必须有它的正向形分拆方案(判法 ∥ 拒文 = `docs/core/design/DOC-CHECK.md` §8)。判的是
14
14
  * **记录缺件**(越线那一支拿收口时的当前长度当触发物,判的仍是 §2 那一格);语义单源 =
15
15
  * `docs/core/standards/CODE-TIER.md` §2.2,位次 = 本档的判序第 ⑤ 臂)。
16
- * ② `assertSectionFiveGate` —— §5 写入门(段序门):两条合取——① `§3` 状态行 = `评审通过`;② `§3` 最近一轮
17
- * 轮次块自己携 pass 裁定行(块 = `lastRoundBlock`;裁定读数 = 骨架档 `verdictGapOf`,`append §3` 写入闸与
18
- * 签发面读同一个函数)。旧的第一条旁路(`§1` 声明 §3 不适用)已随本批删除:实现只发生在评审之后,没有例外段。
16
+ * ② `assertSectionFiveGate` —— §5 写入门(段序门):一条判据 = 末轮轮次块携 pass 裁定(`lastRoundVerdictGap` =
17
+ * 骨架档 `verdictGapOf`;`append §3` 写入闸与签发面读同一个函数)。旧的第一条旁路(`§1` 声明 §3 不适用)
18
+ * 已随声明机制退役:实现只发生在评审之后,没有例外段。
19
19
  * ③ `reanchorReceiptLine` —— 收口重锚回执三行(§2.11 逐字)。
20
20
  * ④ **全归档补关面**(§4.18-C · `docs/core/design/LEDGER.md` §11.2):拒文 `repairBlockedError`、档面
21
21
  * 追记行 `repairTraceLine` 与回执段 `repairReceiptLine`。补关只跳过收口门的 ①②⑤ 三臂,③④ 与写序、
22
22
  * 凭证消费、完成清单预检全部保留——「行全归档」不是「环节全免」;分派判据住 `batch-ledger-view.mjs`。
23
23
  *
24
- * 为什么 §3 内容臂留着(不换成只判状态词):内容臂守的是「段里真跑过」,状态词臂守的是「跑的结论是
25
- * pass」——换掉内容臂 = 门被放宽(一段空着但状态词对就过),那正是本批要堵的洞。所以是**加**一条臂。
24
+ * 为什么 §3 内容臂留着:内容臂守的是「段里真跑过」,末轮块裁定臂守的是「跑的结论是 pass」——换掉内容臂 =
25
+ * 门被放宽(一段空着但凭据块对就过),那正是本批要堵的洞。
26
26
  *
27
27
  * 依赖单向(KD-4):`skeleton ← closeout ← bind ← reopen ← lifecycle`——本档不 import lifecycle。
28
28
  */
@@ -37,8 +37,7 @@ import {
37
37
  } from "../agent/batch-changed-surface.mjs"
38
38
 
39
39
  import {
40
- countRoundBlocks, inapplicableDeclarationError, lastRoundBlock, malformedInapplicableLine, readSectionStatusWord,
41
- sectionContentState, STATUS_WORDS, verdictGapOf,
40
+ countRoundBlocks, inapplicableDeclarationError, lastRoundBlock, malformedInapplicableLine, sectionContentState, verdictGapOf,
42
41
  } from "./batch-skeleton.mjs"
43
42
  // 裁定读数 = 骨架档 `verdictGapOf` 一个函数(设计档 §2.6.1「判据单源」):`§5` 门判据 2 与签发面判的是同一份
44
43
  // 回执文本,组合序各写一遍必然一处变一处不变。本档不再 import 两条裁定锚——锚与序都在那个函数里。
@@ -66,13 +65,6 @@ function closeoutChainError(missing) {
66
65
  "§6 must carry its verification. Nothing was written."
67
66
  }
68
67
 
69
- /** `§3` 状态词臂拒句(设计档 §2.6 `R-G1` 逐字形):链门已过、只剩评审结论没到时出这一条。
70
- * `value` = 读到的状态词(缺失 ⇒ `(无状态行)`)。本臂无条件——没有「声明不适用」这条旁路。 */
71
- function closeoutS3Error(value) {
72
- return "batch: 收口链未跑完(closeout chain gate)— close refuses: " +
73
- `§3 状态行 = ${value},须 = 评审通过(评审须真跑过并给 pass)。Missing: §3. Nothing was written.`
74
- }
75
-
76
68
  /**
77
69
  * 链完整性门(§4.18-A——**收口前置**):判据 = §2 / §3 / §4 / §6 每段须 `sectionContentState(src, seg) !== "empty"`
78
70
  * (有内容);不满足 ⇒ throw 并点名缺段(J1;§6 为唯一缺段时 = J2 旧串逐字)。门不判内容质量、
@@ -91,7 +83,7 @@ export function assertCloseoutChainComplete(src) {
91
83
  /**
92
84
  * 收口两路共调判序(设计档 §2.4–§2.6 · 2026-10-27 四道门 + 2026-11-11 第 ⑤ 臂):
93
85
  * ① 链完整性门(§2 / §3 / §4 / §6 各段须有内容)
94
- * ② **`§3` 状态词臂**:`§3` 状态行 = `评审通过`(无条件)
86
+ * ② 末轮块裁定臂:末轮轮次块携 pass 裁定;`repair` 边不跑本臂
95
87
  * ③ 本批批内单测档门(仍在盘 ⇒ 拒——零留存的结构兜底)
96
88
  * ④ 文档机检门(REFUSED ∥ ERROR ⇒ 拒)
97
89
  * ⑤ **档位臂**(`batch-tier-gate.mjs` 的 `assertTierPlan`):一支判据、一条拒文——
@@ -103,18 +95,22 @@ export function assertCloseoutChainComplete(src) {
103
95
  * `src` = 调用方已读的写盘前全文(判据与写盘读同一份字节,不二次读盘)。
104
96
  * `mode` = **哪一条收口边在跑**(三值闭集;跳过集由这一处决定):
105
97
  * `normal` = 正常收口(五臂全跑)∥ `repair` = 全归档补关(§4.18-C)∥ `supersede` = 被取代的收口(§4.18-D)。
106
- * `repair` ∥ `supersede` 两条都**只跳过 ①②⑤ 三臂**,③④ 照跑——补关与取代按定义没有可判的 §2,
98
+ * `repair` ∥ `supersede` 两条都只跳过 ① 链门、② 末轮块裁定臂与 ⑤ 档位臂,③④ 照跑——补关与取代按定义没有可判的 §2,
107
99
  * 而单测档门与机检门在那两条边上照旧生效;分派判据不在这里
108
100
  * (补关住 `batch-ledger-view.mjs` 的一致性预读,取代由 `batch-supersede.mjs` 自己持),本档只承接
109
101
  * 「已判定走哪条边」之后的判序。
110
102
  * @returns {object|null} 文档机检门结果
111
103
  */
112
104
  export function assertCloseoutGates({ abs, cwd, agent, src, action = "close", mode = "normal" }) {
105
+ // 非工程模式(普通 / tiny)不挂工程收口机械门(`BATCH-RECORD.md` §4.7 / §4.16 / §8:
106
+ // 普通模式不获得工程机械门——不强制六段链门 §2/§3/§4/§6、不强卡 §3 末轮 pass 块、不跑批内单测档门、
107
+ // 收口文档机检与 800 行档位臂;冻结门与台账完成清单结算 `assertCloseDisposition` 仍在调用侧照常执行)。
108
+ if (!agent?.config?.agent?.engineering) return null
113
109
  if (mode === "normal") {
114
110
  assertCloseoutChainComplete(src) // ① 链门(§2 / §3 / §4 / §6 各段须有内容)
115
- // ② §3 状态词臂(无条件):评审须真跑过并给 pass。旧旁路「§1 声明 §3 不适用」已随声明机制退役删除。
116
- const s3 = readSectionStatusWord(src, 3)
117
- if (s3 !== STATUS_WORDS[3].done) throw new Error(closeoutS3Error(s3 ?? "(无状态行)"))
111
+ // ② 末轮块裁定臂:末轮轮次块自己携 pass 裁定;无 pass 行、段内无块 ⇒ 照拒。
112
+ const gap = lastRoundVerdictGap(lastRoundBlock(src, 3))
113
+ if (gap !== null) throw new Error(sectionFiveVerdictBlockError(src, gap))
118
114
  }
119
115
  assertUnitTestFilesCleared(abs, action) // ③
120
116
  const gate = runBatchGate({ abs, cwd, agent, scope: "close" }) // ④
@@ -280,23 +276,14 @@ function sectionFiveVerdictBlockError(src, gap) {
280
276
 
281
277
  /**
282
278
  * §5 写入门(段序门 · 设计档 §2.6):实现发生在评审通过之后,序在这里才判得出真假。
283
- * 判据 = 两条合取(**没有第一条的旁路**:`§1` 声明 §3 不适用已随声明机制退役——实现只发生在评审之后),
284
- * 先判状态词、过了再判块(状态词不过时拒文已足以说明,不必再报块):
285
- * ① `§3` 状态行 = `评审通过`(拒文 `R-G2`);
286
- * ② `§3` **最近一轮轮次块自己携 pass 裁定行**(拒文 `R-G2b`)——评审写了一笔 `§3` 却不带任何裁定行时,
287
- * 上一轮的 `评审通过` 仍是字面字,只判 ① 就会放行。② 只问一个可读的事实「门所依赖的块自己有没有
288
- * 说结论」,不猜意图(补记 ∥ 隐性打回的处置是同一条);`§3` 无任何轮次块 ⇒ ② 不成立、照拒——
289
- * 真跑过的 pass 必落本轮块,无块即无 pass 可依。
290
- * 修正轮在 §3 已 pass 之后追加 §5,末块携 pass 裁定 ⇒ 两判据当场成立。`status` 单写不再足以开 `§5`(只置状态词、不落一块携 pass 裁定的
291
- * 轮次块 ⇒ 照拒)。
279
+ * 判据 = **一条**:`§3` **最近一轮轮次块自己携 pass 裁定行**(拒文 `R-G2b`,`lastRoundVerdictGap`)——
280
+ * 评审写了一笔 `§3` 却不带任何裁定行时,放行它就等于放行一笔没有结论的评审。② 只问一个可读的事实
281
+ * 「门所依赖的块自己有没有说结论」,不猜意图(补记 ∥ 隐性打回的处置是同是这条);`§3` 无任何轮次块 ⇒
282
+ * 不成立、照拒——真跑过的 pass 必落本轮块,无块即无 pass 可依。
283
+ * `status` 单写不开 `§5`:只置段头区状态词、不落一块携 pass 裁定的轮次块 ⇒ 照拒(攒词的旧旁路已随声明机制退役)。
292
284
  * @param {string} src — 写盘前的档全文
293
285
  */
294
286
  export function assertSectionFiveGate(src) {
295
- const s3 = readSectionStatusWord(src, 3)
296
- if (s3 !== STATUS_WORDS[3].done) {
297
- throw new Error("batch: §5 append refused — the implementation section opens only after the design review passed " +
298
- `(§3 状态行 = ${s3 ?? "(无状态行)"},须 = 评审通过). Nothing was written.`)
299
- }
300
287
  const gap = lastRoundVerdictGap(lastRoundBlock(src, 3))
301
288
  if (gap !== null) throw new Error(sectionFiveVerdictBlockError(src, gap))
302
289
  }
@@ -87,15 +87,17 @@ export function gateReceiptLine(gate) {
87
87
  * 处置面不在这里:命中行说的是哪一处、为什么,改法归作者;本门只拒、不代改。
88
88
  */
89
89
  export function gateRefusalMessage(gate, action = "append") {
90
- const writeFace = action === "append" || action === "edit"
90
+ const writeFace = action === "append" || action === "edit" || action === "update"
91
91
  const lead = action === "edit"
92
92
  ? "batch: 文档机检门拒写(doc-check gate)—— 本次段内就地改未落盘,档字节不变。"
93
- : writeFace
94
- ? "batch: 文档机检门拒写(doc-check gate)—— 本次段写入未落盘,档字节不变。"
95
- : `batch: 文档机检门拒收口(doc-check gate)—— ${action === "close" ? "close" : "status → 已收口"} 被拒绝,§1 状态行字节不变。`
93
+ : action === "update"
94
+ ? "batch: 文档机检门拒写(doc-check gate)—— 本次批量段更新未落盘,档字节不变。"
95
+ : writeFace
96
+ ? "batch: 文档机检门拒写(doc-check gate)—— 本次段写入未落盘,档字节不变。"
97
+ : `batch: 文档机检门拒收口(doc-check gate)—— ${action === "close" ? "close" : "status → 已收口"} 被拒绝,§1 状态行字节不变。`
96
98
  const detail = gate.state === "error" ? String(gate.report ?? "").split("\n").slice(1) : gate.hitLines
97
99
  const noBytes = writeFace
98
- ? action === "edit"
100
+ ? (action === "edit" || action === "update")
99
101
  ? "Nothing was written — the target range and every other byte of the record are unchanged."
100
102
  : "Nothing was written. Do NOT close the record after a refused append — nothing landed, and closing freezes the missing section for good (失败 ⇒ 勿 close)."
101
103
  : "Nothing was written."
@@ -35,6 +35,7 @@ import { basename, dirname, join, relative, resolve } from "node:path"
35
35
 
36
36
  import { appendLedgerNoToHeader, batchSkeleton, goAheadReceiptLine, readBatchStatusLine } from "./batch-skeleton.mjs"
37
37
  import { resolveBatchCreatePath } from "./batch-paths.mjs"
38
+ import { readManifest } from "../manifest.mjs"
38
39
  // B8(TOOLS.md §6.29 ④):父会话历史窗口起点(建批成功即记一次)
39
40
  import { recordBatchHistoryStart } from "./read-history.mjs"
40
41
 
@@ -271,7 +272,26 @@ export async function createBatchRecord({ args, ctx, review, cwd, bases, onWritt
271
272
  // 旧批没有起点时由首次绑定 spawn 记一次(`subagent-spawn.mjs`)——那里不回填更早的消息。
272
273
  recordBatchHistoryStart(ctx?.agent, abs, Date.now())
273
274
  // 建批这一笔也是 §1 写入(§4.24 回执面):回执带当前授权行 ⇒ 新批的默认态 `未授权` 当场读得出来。
274
- return `batch: created ${abs} — six-section skeleton written (§1 status line carries the gate-legal 「进行中」 placeholder); ledger row #${result.id} ${took} in the same call (row status = 在途 · task_book = ${taskBook} · trigger = 归批). header 台账 line filled (#${result.id} · ${board ?? "—"}).${goAheadReceiptLine(readFileSync(abs, "utf8"))}${sameFaceReceiptLine(result.sameFace)}`
275
+ return `batch: created ${abs} — six-section skeleton written (§1 status line carries the gate-legal 「进行中」 placeholder); ledger row #${result.id} ${took} in the same call (row status = 在途 · task_book = ${taskBook} · trigger = 归批). header 台账 line filled (#${result.id} · ${board ?? "—"}).${goAheadReceiptLine(readFileSync(abs, "utf8"))}${sameFaceReceiptLine(result.sameFace)}${docRootHintLine(root)}`
276
+ }
277
+
278
+ /** 建批回执的权威文档层即时引导(非门、只读、失败静默):把项目的需求与设计层根路径送到动手那一帧。 */
279
+ function docRootHintLine(root) {
280
+ try {
281
+ const m = readManifest(root)
282
+ if (!m?.ok || !m.manifest?.docRoot) return ""
283
+ const fmt = (v) => (typeof v === "string" && v.trim() ? v.trim()
284
+ : Array.isArray(v) ? v.map((x) => (typeof x === "string" ? x.trim() : "")).filter(Boolean).join(", ") : "")
285
+ const parts = []
286
+ const req = fmt(m.manifest.docRoot.requirements)
287
+ const des = fmt(m.manifest.docRoot.design)
288
+ if (req) parts.push(`requirements=${req}`)
289
+ if (des) parts.push(`design=${des}`)
290
+ if (!parts.length) return ""
291
+ return `\n [authority docs] ${parts.join(" · ")} — read the owning requirement/design doc before editing (do not treat this batch record as the doc lookup).`
292
+ } catch {
293
+ return ""
294
+ }
275
295
  }
276
296
 
277
297
  /** 本地今天(YYYY-MM-DD)——create 的缺省 date。 */
@@ -20,7 +20,7 @@
20
20
  * eng-coder → §5 · coder → §5【普通模式分支增表】· 主 agent → 工程 §1 ∥ 普通 §1/§2/§5【取段——§4.16】)。
21
21
  *
22
22
  * 四道门(2026-10-27 · 设计档 §2.6):写锁 = `agent/write-gate.mjs`(不在本档);§5 写入门 =
23
- * `batch.mjs` append 面;**收口门** = `batch-closeout.mjs`(链门 + §3 状态词臂 + 单测档门 + 机检门);
23
+ * `batch.mjs` append 面;**收口门** = `batch-closeout.mjs`(链门 + 末轮块裁定门 + 单测档门 + 机检门);
24
24
  * 核销工作证据门 = `ledger-close-proof.mjs`(只判应用本笔后的 evidence 非空白)。
25
25
  *
26
26
  * 收口重锚(§6.2 · C · AC-M2-30):收口有两条通路——`close` ∥ `status` 写 §1「已收口」——两路都冻结档、
@@ -31,7 +31,7 @@
31
31
  * 回滚本身也失败时出第二条串(点名半状态与 `batch({action:"check"})`)。
32
32
  *
33
33
  * **全归档补关**(§4.18-C · `docs/core/design/LEDGER.md` §11.2):两路在链门前读 `batchLedgerConsistency`——
34
- * 行集非空、每行均已归档、且 `halfStates` 恰为唯一 `open-but-settled` ⇒ 补关(只跳过链内容门与 §3 pass 臂,
34
+ * 行集非空、每行均已归档、且 `halfStates` 恰为唯一 `open-but-settled` ⇒ 补关(只跳过 ① 链门、② 末轮块裁定臂与 ⑤ 档位臂,③④ 照跑,
35
35
  * 不补写 §3/§4/§6);补关成功在 §1 段尾追加一行 `**补关记录**`、回执另出 `[ledger repair]`,台账行零改写。
36
36
  *
37
37
  * 重开回收(§2.13 · E13):`status` 写 §1 `进行中` 而前态 `closed` ⇒ 重开;写盘后同调 `reclaimOnReopen`
@@ -237,7 +237,7 @@ function STATUS_LINE_AMBIGUOUS_ERROR(seg, count) {
237
237
  * **收口第二通路**:值余核 = §1「已收口」时本 action 同样收口(收口门 + 单测档门 + 机检门 + 冻结与
238
238
  * close 同序)⇒ 状态行写盘后同调 `reanchorOnClose`,回执携同一 `[ledger reanchor]` 段;`completed` 与
239
239
  * `closeEvidence` 与 `close` 同两条必填,台账失败同样回滚档并抛拒。与 close 同门同分派:全归档批走补关
240
- * (§4.18-C,只绕开内容门与 §3 pass 臂,§1 段尾落一行 `**补关记录**`)。
240
+ * (§4.18-C,只跳过 ① 链门、② 末轮块裁定臂与 ⑤ 档位臂,③④ 照跑,§1 段尾落一行 `**补关记录**`)。
241
241
  * **重开径**(§4.18-B · §2.13):前态 closed ∧ 值 = 进行中 ∧ note 非空 ⇒ 重开;写盘后同调
242
242
  * `reclaimOnReopen`(`待核销 → 在途`),回执携 `[ledger reopen]` 段;回收失败 ⇒ 档回滚到冻结态并抛拒。
243
243
  * **其余一切用法零台账写**;本 action 的回执末尾恒带一行 `[ledger consistency]`。
@@ -319,7 +319,7 @@ export async function statusBatchRecord({ args, ctx, review, reviewTrack, pickTa
319
319
  // 回执附带当前授权行(§4.24):重开径不改授权行(它只改写状态行 + 追记行)⇒ 读写前全文同值。
320
320
  return `batch: §1 reopened — status line → 「进行中(重开 ${todayLocal()})」 + reopen record appended to §1 (reason lands verbatim in the record line). Append/status are writable again (BR-64); the closeout chain gate still applies at the next close.${goAheadReceiptLine(src)}${reopenReceiptLine(reclaim)}${consistencyReceiptLine(await consistencyOf(abs, ctx?.agent?.cwd))}`
321
321
  }
322
- // 收口门(§2.6——链门 + §3 状态词臂 + 单测档门 + 机检门):本笔把 §1 写成「已收口」⇒ 四道同判。
322
+ // 收口门(§2.6——链门 + 末轮块裁定门 + 单测档门 + 机检门):本笔把 §1 写成「已收口」⇒ 四道同判。
323
323
  // 段号守卫只写这一次(`STATUS_WORDS[1].closed` 只属 §1 项,`assertStatusValue` 亦拒他段该值)。
324
324
  const closingValue = seg === 1 && stripDecorations(value) === STATUS_WORDS[1].closed
325
325
  if (!closingValue && args?.completed !== undefined) {
@@ -362,7 +362,7 @@ export async function statusBatchRecord({ args, ctx, review, reviewTrack, pickTa
362
362
  * close——收口冻结(§4.13,仅 depth-0)。§1 状态行 →「已收口 <YYYY-MM-DD>」;此后该档 append/status 全拒
363
363
  * (冻结判据唯一真值 = §1 行——§4.9 零语义变;唯一例外 = 重开合取 §4.18-B)。对已收口档再 close ⇒ 拒。
364
364
  * 不代写 §6 内容、不替代台账核销事务。
365
- * 前提规则(#422 + §4.18-A + §2.6 四道门):① 收口门(链门 ∧ §3 状态词臂;§6 唯一缺段时拒串 = 旧 §6 串
365
+ * 前提规则(#422 + §4.18-A + §2.6 四道门):① 收口门(链门 ∧ 末轮块裁定臂;§6 唯一缺段时拒串 = 旧 §6 串
366
366
  * 逐字零回归);② 本批批内单测档门;③ 文档机检门;④ 无状态行档 ⇒ 自建机读位(「有行但不可解析」仍
367
367
  * fail-closed)。**全归档补关**(§4.18-C)只绕开 ① 的两臂,②③ 与完成清单预检、凭证消费、重锚全保留。
368
368
  * @returns {Promise<string>} 成功消息
@@ -376,8 +376,8 @@ export async function closeBatchRecord({ args, ctx, review, pickTarget, onWritte
376
376
  if (readBatchStatusLine(src) !== "open" && sectionHasStatusLine(src, 1)) assertGateOpen(src)
377
377
  // 收口分流(§4.18-C):链门之前读一致性——全归档批走补关,其余走完整链门。
378
378
  const repair = await repairDecisionOf(abs, ctx?.agent?.cwd)
379
- // 收口门(§2.6):链门 → §3 状态词臂 → 单测档门 → 机检门(位次 = 既有拒因之后、写盘之前);
380
- // 补关分派 ⇒ 只跳过前两臂(repair.repair)。
379
+ // 收口门(§2.6):链门 → 末轮块裁定门 → 单测档门 → 机检门(位次 = 既有拒因之后、写盘之前);
380
+ // 补关分派 ⇒ 只跳过 ① 链门、② 末轮块裁定臂与 ⑤ 档位臂(③④ 照跑)。
381
381
  const gate = assertCloseoutGates({ abs, cwd: ctx?.agent?.cwd, agent: ctx?.agent ?? {}, src, action: "close", mode: repair.repair ? "repair" : "normal" })
382
382
  // 完成清单预检位于既有收口门之后、凭证消费与批档写盘之前;拒绝时令牌、档、账均不变。
383
383
  // `closeEvidence` 与 `completed` 同一格(收口即结算:证据由本笔给,机器不代写——代写一句就把核销门