@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/ledger-tools.mjs CHANGED
@@ -13,7 +13,7 @@
13
13
  import { existsSync } from "node:fs"
14
14
  import { isAbsolute, dirname, join, resolve } from "node:path"
15
15
  import { ledgerAdd, ledgerClose, ledgerCloseBatch, ledgerCount, ledgerQuery, ledgerUpdate, ledgerUpdateBatch } from "./ledger-cmd.mjs"
16
- import { noRootMessage, resolveLedgerRoot } from "./ledger-db.mjs"
16
+ import { QUERY_PAGE_SIZE, noRootMessage, resolveLedgerRoot } from "./ledger-db.mjs"
17
17
  import { DESC } from "./tools/shared.mjs" // #15 描述外置:文本单点 = tool-docs/ledger.md(DESC 单解析面)
18
18
 
19
19
  /** 指针值 → 文件路径(剥 `§N` 段号;空值跳过)。 */
@@ -29,7 +29,7 @@ const ptrPath = (v) => {
29
29
  * 宿主下两者不同:会话锚才是「不能跑到工作目录以外去」的那道界)。 */
30
30
  const anchorOf = (ctx) => ctx?.agent?.cwd ?? ctx?.cwd
31
31
 
32
- /** 工具 cwd 供值面:**起点 = 动作目标路径**(`docs/core/requirements/ENGINEERING-MODE-V2-SPEC-MANIFEST.md:72`
32
+ /** 工具 cwd 供值面:**起点 = 动作目标路径**(`docs/core/requirements/ENGINEERING-MODE-V2-SPEC-MANIFEST.md` 的「解析模型与作用定位」节
33
33
  * 「参数按用点解析——动作作用于哪个项目的路径,就取那个项目的档」)。
34
34
  * 指针(`task_book` ∥ `req_doc` ∥ `batch`)在场 ⇒ 取该文件所在目录为起点(文件 = 其所在目录);
35
35
  * 无指针的动作(`query` ∥ `count` ∥ 首次建行)才回退会话锚。项目根由核 `resolveLedgerRoot` 推导。 */
@@ -56,6 +56,53 @@ function assertLedgerStart(start, boundary) {
56
56
  if (!resolveLedgerRoot(start, boundary)) throw new Error(noRootMessage(start, boundary))
57
57
  }
58
58
 
59
+ // ── 分页(设计档 `LEDGER.md` §7.2「分页」条——`query` 工具面;核仍全量) ───────────────
60
+
61
+ /** 排序键(§7.2「无时间戳行的位置」):`updated_at ?? created_at ?? ""`(两列皆空的存量行键 = 空串)。
62
+ * 写入侧 `ledgerAdd` 恒置两列 ⇒ 空串形只来自迁移进来的存量行。定宽 UTC ISO 串(`nowIso`)⇒ 字典序 =
63
+ * 时序,游标按串比较即成立;空串字典序最小 ⇒ 倒序里恒居末位(当最旧处理)。 */
64
+ const pageSortKeyOf = (r) => r.updated_at ?? r.created_at ?? ""
65
+
66
+ /** 倒序比较:排序键倒序,同键按 `id` 倒序(兜底键——同刻多行才有确定次序)。 */
67
+ const pageSortDesc = (a, b) => {
68
+ const ka = pageSortKeyOf(a), kb = pageSortKeyOf(b)
69
+ return ka === kb ? b.id - a.id : ka < kb ? 1 : -1
70
+ }
71
+
72
+ /** 游标形判(§7.2「游标形」):`"<排序键>|<id>"`——首个 `|` 之前是排序键(任意串、可为空),之后是十进制
73
+ * 正整数;不合 ⇒ 拒,**不静默回首页**(静默回退把「游标打错」伪装成「已翻到头」,读的人不会再核第二遍)。
74
+ * 判于 `ledgerQuery` 之前 ⇒ 非法游标零库动作。 */
75
+ function parseQueryCursor(raw) {
76
+ const bad = () => new Error(`ledger(query):cursor 非法:${argPreview(raw)}(应为「<updated_at>|<id>」,照上一页尾行原样传入)`)
77
+ if (typeof raw !== "string") throw bad()
78
+ const sep = raw.indexOf("|")
79
+ const idText = sep < 0 ? "" : raw.slice(sep + 1)
80
+ if (!/^[0-9]+$/.test(idText) || Number.parseInt(idText, 10) <= 0) throw bad()
81
+ return { key: raw.slice(0, sep), id: Number.parseInt(idText, 10) }
82
+ }
83
+
84
+ /** 回显段(§7.2「回显段」):本页生效的过滤原样带上,形如 `, status:"在途"`;一枚没给 ⇒ 空串(`cursor`
85
+ * 前不留逗号)。**回显是「照抄尾行就翻同一本账」的前提**——不回显则第二页按未过滤全集取行、`<N>` 从过滤集
86
+ * 换成全库,且零报错。`cwd` 同一道理:换锚就换了库。 */
87
+ const QUERY_ECHO_KEYS = ["status", "kind", "board", "trigger", "task_book", "cwd"]
88
+ const queryEchoOf = (args) => QUERY_ECHO_KEYS.filter((k) => args[k] != null).map((k) => `, ${k}:${JSON.stringify(args[k])}`).join("")
89
+
90
+ /** 分页切片 + 尾行(§7.2「回执逐字」四态)。`rows` = 核返回的**全量**行集(本函数不读库;排序在副本上
91
+ * 就地做,核的 `ORDER BY id` 零改):`<N>` = 同一行集的长度(不另跑 `count`——两数出自同一次读,不会互相
92
+ * 打架);`<P>` / `<A>–<B>` = 同一行集的序号;`cursor` 越过末行 ⇒ 本页空(与「候选集空」同一行)。 */
93
+ function queryPageOf(rows, cursor, echo) {
94
+ const ordered = [...rows].sort(pageSortDesc)
95
+ const total = ordered.length
96
+ const hit = cursor ? ordered.findIndex((r) => { const k = pageSortKeyOf(r); return k < cursor.key || (k === cursor.key && r.id < cursor.id) }) : 0
97
+ const from = hit < 0 ? total : hit
98
+ const page = ordered.slice(from, from + QUERY_PAGE_SIZE)
99
+ if (page.length === 0) return { page, tail: `共 ${total} 行 · 本页无行` }
100
+ const span = `第 ${Math.floor(from / QUERY_PAGE_SIZE) + 1} 页(第 ${from + 1}–${from + page.length} 行,按 updated_at 倒序)`
101
+ if (from + page.length >= total) return { page, tail: `共 ${total} 行 · ${span}· 已到末页` }
102
+ const last = page[page.length - 1]
103
+ return { page, tail: `共 ${total} 行 · ${span}· 下一页:ledger({action:"query"${echo}, cursor:"${pageSortKeyOf(last)}|${last.id}"})` }
104
+ }
105
+
59
106
  // ── 工具层参数守卫(设计档 §3.2 · 2026-09-27 快车道修复 · Gitee #IKIQGK / 台账 #472) ──
60
107
 
61
108
  /** 值预览(P1–P5 文案的 `<预览>`——设计档 §3.2):`JSON.stringify`,超 80 字符截断加 `…`
@@ -136,7 +183,7 @@ const FULL_ACTIONS = ["add", "update", "close", "query", "count"]
136
183
  const READ_ACTIONS = ["query", "count"]
137
184
 
138
185
  /** cwd 参数描述(五前身声明同文——文本单点)。 */
139
- const CWD_DESC = "本次动作的目标路径——项目根由它推导:沿祖先链取最近的项目目录(带 PROJECT-MANIFEST.json 的那一层;库在盘不算项目)。多根工作区 / 无项目目录 ⇒ 报明并给候选,不向下取子项目当根。台账按项目根关联存储于用户数据目录(工作树外、不进 git)。相对路径按当前会话锚解析;项目根按上溯界取——界外不挂项目。缺省 = 当前会话锚"
186
+ const CWD_DESC = "The action's target path. The project root is the nearest ancestor directory holding a `PROJECT-MANIFEST.json`. A ledger file on disk does not make a project. A workspace with several roots and no project directory is reported with the candidates. The search never descends into a sub-project. The ledger is stored per project root in the user data directory, outside the work tree. A relative path resolves against the session anchor; no project is attached outside that boundary. Default: the session anchor"
140
187
 
141
188
  /** 各 action 声明条目(= 前身旧工具 `parameters` 逐字;伪工具对象守卫 + 弃用壳共用单源)。
142
189
  * `parameters` 内的键 = 交模型 / provider 的那一份声明;**组判据两键(`oneOfRequired` / `whenPresent`)
@@ -148,14 +195,14 @@ const ACTION_SPECS = {
148
195
  required: ["kind", "title"],
149
196
  properties: {
150
197
  cwd: { type: "string", description: CWD_DESC },
151
- kind: { type: "string", enum: ["requirement", "tech_todo"], description: "类别" },
152
- title: { type: "string", description: "条目标题" },
153
- board: { type: "string", description: "归属板块(需求档文档名,可空)" },
154
- req_doc: { type: "string", description: "需求档指针(可空)" },
155
- task_book: { type: "string", description: "任务书指针(可空;在途 / 待核销必填——咬合 CHECK)" },
156
- evidence: { type: "string", description: "最小证据行(可空)" },
157
- trigger: { type: "string", enum: ["归批", "条件", "认账不排期"], description: "技术待办触发(可空)" },
158
- resume_when: { type: "string", description: "复启条件句(可空;`trigger=条件` 行的一句话条件 + 可选锚 `@YYYY-MM-DD` ∥ `@事件件名`)——值非空:空串 / 全空白 ⇒ 拒" },
198
+ kind: { type: "string", enum: ["requirement", "tech_todo"], description: "What kind of row to open — required on add." },
199
+ title: { type: "string", description: "The row's title — required on add." },
200
+ board: { type: "string", description: "The owning board — a requirements document name (optional)." },
201
+ req_doc: { type: "string", description: "Requirements-document pointer (optional)." },
202
+ task_book: { type: "string", description: "Task-book pointer (optional). Required while the row is 在途 or 待核销 — the ledger CHECK enforces it." },
203
+ evidence: { type: "string", description: "Minimal evidence line (optional)." },
204
+ trigger: { type: "string", enum: ["归批", "条件", "认账不排期"], description: "Tech-todo trigger — one of the `enum` values (optional)." },
205
+ resume_when: { type: "string", description: "Resume condition sentence (optional): one sentence for rows whose trigger is the conditional-resume kind, plus an optional anchor `@YYYY-MM-DD` or `@<event name>`. A blank or whitespace-only value is refused." },
159
206
  },
160
207
  additionalProperties: false,
161
208
  },
@@ -165,18 +212,18 @@ const ACTION_SPECS = {
165
212
  type: "object",
166
213
  properties: {
167
214
  cwd: { type: "string", description: CWD_DESC },
168
- id: { type: "number", description: "条目 id(与 batch 恰携其一)" },
169
- batch: { type: "string", description: "批档指针(与 id 恰携其一)——批级批量:一次结算该批的行;携 status ⇒ 逐行按迁移表重判,不携 status ⇒ 纯字段补丁(status 零写)" },
170
- status: { type: "string", enum: ["待讨论", "待设计", "在途", "待核销", "已核销", "已废弃"], description: "目标状态(六态之一,缺省 = 不变)" },
171
- title: { type: "string", description: "标题(缺省 = 不变)" },
172
- board: { type: "string", description: "归属板块(缺省 = 不变)" },
173
- req_doc: { type: "string", description: "需求档指针(缺省 = 不变)" },
174
- task_book: { type: "string", description: "任务书指针(缺省 = 不变)" },
175
- evidence: { type: "string", description: "证据(缺省 = 不变;传入 = 追加——旧值非空 ⇒ 拼 `旧|新`(「|」零空格)、旧空直写;新值全空白 ⇒ 拒;覆盖 = 与 `evidenceReplace: true` 同传)" },
176
- evidenceReplace: { type: "boolean", description: "覆盖旗(布尔):与 evidence 同传 = 整段替换(旧文零保留);缺省 / false = 追加;旗独传(无新文)⇒ 拒" },
177
- trigger: { type: "string", enum: ["归批", "条件", "认账不排期"], description: "触发(缺省 = 不变)" },
178
- resume_when: { type: "string", description: "复启条件句(缺省 = 不变;值非空——空串 / 全空白 ⇒ 拒)" },
179
- executor: { type: "string", description: "执行者 sessionId(可选——接手改写归属用;缺省 = 按状态迁移语义:进入待设计 / 在途自动写本会话;出在途 / 撤回清空)" },
215
+ id: { type: "number", description: "Row id — pass exactly one of id and batch." },
216
+ batch: { type: "string", description: "Batch-record pointer — pass exactly one of id and batch. Batch-level bulk: settles that batch's rows in one call. With `status`, every row is re-judged against the migration table; without it this is a plain field patch (status is not written)." },
217
+ status: { type: "string", enum: ["待讨论", "待设计", "在途", "待核销", "已核销", "已废弃"], description: "Target state — one of the six; omitted ⇒ unchanged." },
218
+ title: { type: "string", description: "Title; omitted ⇒ unchanged." },
219
+ board: { type: "string", description: "Owning board; omitted ⇒ unchanged." },
220
+ req_doc: { type: "string", description: "Requirements-document pointer; omitted ⇒ unchanged." },
221
+ task_book: { type: "string", description: "Task-book pointer; omitted ⇒ unchanged." },
222
+ evidence: { type: "string", description: "Evidence line; omitted ⇒ unchanged, given ⇒ appended: a non-empty old value is joined as `旧|新` (no spaces around the bar), an empty one is replaced outright. A whitespace-only value is refused. Pass `evidenceReplace: true` to replace the whole range. Required when moving 在途 or 待核销 back to 待讨论 — it is the return reason. The receipt returns it as `reason`." },
223
+ evidenceReplace: { type: "boolean", description: "Replace flag: pass it with `evidence` to replace the whole range (no part of the old text survives). Omitted or false ⇒ append. Passed alone, with no new text ⇒ refused." },
224
+ trigger: { type: "string", enum: ["归批", "条件", "认账不排期"], description: "Tech-todo trigger; omitted ⇒ unchanged." },
225
+ resume_when: { type: "string", description: "Resume condition sentence; omitted ⇒ unchanged. A blank or whitespace-only value is refused." },
226
+ executor: { type: "string", description: "Executor session id (optional — override the owner when taking a row over). The default follows the state transition: entering 待设计 or 在途 writes this session automatically. Leaving 在途, withdrawing or returning to 待讨论 clears it. 待核销 → 在途 always writes this session, and an explicit value does not override it." },
180
227
  },
181
228
  additionalProperties: false,
182
229
  },
@@ -188,11 +235,11 @@ const ACTION_SPECS = {
188
235
  required: ["status"],
189
236
  properties: {
190
237
  cwd: { type: "string", description: CWD_DESC },
191
- id: { type: "number", description: "条目 id(与 batch 恰携其一)" },
192
- batch: { type: "string", description: "批档指针(与 id 恰携其一)——批级批量结算:只销已到 待核销 的行;待讨论 / 待设计 点名跳过(须先 update 到 在途 认领);在途 点名跳过(不可跳)" },
193
- status: { type: "string", enum: ["已核销", "已废弃"], description: "目标态(勾销 / 撤回)——携 batch 时可不给(缺省 = 勾销单值),给了只能是 已核销" },
194
- evidence: { type: "string", description: "最小证据行(可选;传入 = 追加缺省——旧值非空 ⇒ 拼 `旧|新`(「|」零空格)、旧空直写;新值全空白 ⇒ 拒;覆盖 = 与 `evidenceReplace: true` 同传)" },
195
- evidenceReplace: { type: "boolean", description: "覆盖旗(布尔):与 evidence 同传 = 整段替换(旧文零保留);缺省 / false = 追加;旗独传(无新文)⇒ 拒" },
238
+ id: { type: "number", description: "Row id — pass exactly one of id and batch." },
239
+ batch: { type: "string", description: "Batch-record pointer — pass exactly one of id and batch. Batch-level settlement: only rows already 待核销 are settled. Rows in 待讨论 or 待设计 are skipped and named (claim them first with update, which moves them to 在途). A 在途 row named here is skipped as well — the transition cannot jump over 待核销." },
240
+ status: { type: "string", enum: ["已核销", "已废弃"], description: "Target state: settle or withdraw. With `batch` it may be omitted (the settle value); given, it must be 已核销." },
241
+ evidence: { type: "string", description: "Minimal work-evidence line (optional); given ⇒ appended: a non-empty old value is joined as `旧|新` (no spaces around the bar), an empty one is replaced outright. A whitespace-only value is refused. Pass `evidenceReplace: true` to replace the whole range. The settle gate only asks whether the evidence is non-blank after this write — it never reads the anchoring batch's closeout or review state." },
242
+ evidenceReplace: { type: "boolean", description: "Replace flag: pass it with `evidence` to replace the whole range (no part of the old text survives). Omitted or false ⇒ append. Passed alone, with no new text ⇒ refused." },
196
243
  },
197
244
  additionalProperties: false,
198
245
  },
@@ -204,11 +251,12 @@ const ACTION_SPECS = {
204
251
  type: "object",
205
252
  properties: {
206
253
  cwd: { type: "string", description: CWD_DESC },
207
- status: { type: "string", enum: ["待讨论", "待设计", "在途", "待核销", "已核销", "已废弃"], description: "按状态过滤(六态之一,缺省 = 不过滤)" },
208
- kind: { type: "string", enum: ["requirement", "tech_todo"], description: "按类别过滤" },
209
- board: { type: "string", description: "按归属板块过滤(需求档文档名)" },
210
- trigger: { type: "string", enum: ["归批", "条件", "认账不排期"], description: "按技术待办触发过滤(三枚举,缺省 = 不过滤——无触发(NULL)不过滤)" },
211
- task_book: { type: "string", description: "按任务书指针过滤(原值等值——批→行反查;缺省 = 不过滤)" },
254
+ status: { type: "string", enum: ["待讨论", "待设计", "在途", "待核销", "已核销", "已废弃"], description: "Filter by state — one of the six; omitted ⇒ no filter." },
255
+ kind: { type: "string", enum: ["requirement", "tech_todo"], description: "Filter by kind." },
256
+ board: { type: "string", description: "Filter by owning board (a requirements document name)." },
257
+ trigger: { type: "string", enum: ["归批", "条件", "认账不排期"], description: "Filter by tech-todo trigger — one of the three `enum` values. Omitted ⇒ no filter, and rows carrying no trigger (NULL) are left in." },
258
+ task_book: { type: "string", description: "Filter by task-book pointer (exact value — the batch→row lookup). Omitted ⇒ no filter." },
259
+ cursor: { type: "string", description: "Page cursor (optional): copy the previous page's tail line verbatim (form `<updated_at>|<id>`). Omitted ⇒ the first page, newest row first. A malformed cursor is refused — never silently treated as page one." },
212
260
  },
213
261
  additionalProperties: false,
214
262
  },
@@ -218,7 +266,7 @@ const ACTION_SPECS = {
218
266
  type: "object",
219
267
  properties: {
220
268
  cwd: { type: "string", description: CWD_DESC },
221
- task_book: { type: "string", description: "按任务书指针过滤(原值等值)" },
269
+ task_book: { type: "string", description: "Filter by task-book pointer (exact value)." },
222
270
  },
223
271
  additionalProperties: false,
224
272
  },
@@ -232,19 +280,20 @@ const UNION_PARAMETERS = {
232
280
  properties: {
233
281
  action: { type: "string", enum: FULL_ACTIONS, description: "" }, // 变体构造时填描述
234
282
  cwd: { type: "string", description: CWD_DESC },
235
- kind: { type: "string", enum: ["requirement", "tech_todo"], description: "类别(add 必填 / query 过滤;缺省 = 不过滤)" },
236
- title: { type: "string", description: "条目标题(add 必填;update 可改——空串 / 全空白 ⇒ 拒)" },
237
- board: { type: "string", description: "归属板块(需求档文档名;add 可空 / update 缺省 = 不变 / query 过滤)" },
238
- req_doc: { type: "string", description: "需求档指针(add 可空 / update 缺省 = 不变)" },
239
- task_book: { type: "string", description: "任务书指针(add 可空 / update 缺省 = 不变;在途 / 待核销必填——咬合 CHECK)" },
240
- evidence: { type: "string", description: "最小证据行(add 可空;update / close 传入 = 追加缺省——旧空直写、旧非空拼 `旧|新`(「|」零空格)、新值全空白 ⇒ 拒;覆盖 = 与 `evidenceReplace: true` 同传;update 缺省 = 不变 / close 缺省 = 判行值)" },
241
- evidenceReplace: { type: "boolean", description: "覆盖旗(布尔):与 evidence 同传 = 整段替换;缺省 / false = 追加;旗独传(无新文)⇒ 拒(update / close)" },
242
- trigger: { type: "string", enum: ["归批", "条件", "认账不排期"], description: "技术待办触发(add / update 可空 / query 过滤)" },
243
- resume_when: { type: "string", description: "复启条件句(add / update 可空;值非空——空串 / 全空白 ⇒ 拒;`trigger=条件` 行的一句话条件 + 可选锚 `@YYYY-MM-DD` ∥ `@事件件名`)" },
244
- id: { type: "number", description: "条目 id(update / close:与 batch 恰携其一)" },
245
- batch: { type: "string", description: "批档指针(update / close:与 id 恰携其一)——批级批量:一次结算该批的行(update 携 status ⇒ 逐行迁移表重判;不携 status ⇒ 纯字段补丁;close 只销 待核销 行、status 可空)" },
246
- status: { type: "string", enum: ["待讨论", "待设计", "在途", "待核销", "已核销", "已废弃"], description: "状态(单行 close 必填 ∈ {已核销, 已废弃};close 携 batch 时可空、给了只能是 已核销;update 迁移 / query 过滤——六态之一)" },
247
- executor: { type: "string", description: "执行者 sessionId(update 可选——接手改写归属;缺省 = 按状态迁移语义:进入待设计 / 在途自动写本会话;出在途 / 撤回清空)" },
283
+ kind: { type: "string", enum: ["requirement", "tech_todo"], description: "Kind — required on add, a filter on query; omitted ⇒ no filter." },
284
+ title: { type: "string", description: "Row title — required on add, changeable on update. Empty or whitespace-only is refused." },
285
+ board: { type: "string", description: "Owning board — a requirements document name: optional on add, unchanged when omitted on update, a filter on query." },
286
+ req_doc: { type: "string", description: "Requirements-document pointer: optional on add, unchanged when omitted on update." },
287
+ task_book: { type: "string", description: "Task-book pointer: optional on add, unchanged when omitted on update. Required while the row is 在途 or 待核销 — the ledger CHECK enforces it." },
288
+ evidence: { type: "string", description: "Evidence line: optional on add. On update and close, given ⇒ appended: an empty old value is replaced outright, a non-empty one is joined as `旧|新` (no spaces around the bar). A whitespace-only value is refused. Pass `evidenceReplace: true` to replace the whole range. Omitted on update ⇒ unchanged; omitted on close ⇒ the row's own value is judged." },
289
+ evidenceReplace: { type: "boolean", description: "Replace flag: pass it with `evidence` to replace the whole range. Omitted or false ⇒ append. Passed alone, with no new text ⇒ refused (update / close)." },
290
+ trigger: { type: "string", enum: ["归批", "条件", "认账不排期"], description: "Tech-todo trigger — one of the `enum` values: optional on add and update, a filter on query." },
291
+ resume_when: { type: "string", description: "Resume condition sentence: optional on add and update, for rows whose trigger is the conditional-resume kind. One sentence plus an optional anchor `@YYYY-MM-DD` or `@<event name>`. A blank or whitespace-only value is refused." },
292
+ id: { type: "number", description: "Row id — on update and close, pass exactly one of id and batch." },
293
+ batch: { type: "string", description: "Batch-record pointer — on update and close, pass exactly one of id and batch. Batch-level bulk: settles that batch's rows in one call. On update, with `status` every row is re-judged against the migration table; without it this is a plain field patch. On close only 待核销 rows are settled, and `status` may be omitted." },
294
+ status: { type: "string", enum: ["待讨论", "待设计", "在途", "待核销", "已核销", "已废弃"], description: "Status — one of the six. Single-row close requires it and accepts `已核销` or `已废弃`. With `batch`, close may omit it; given, it must be `已核销`. update migrates the row; query filters by it." },
295
+ executor: { type: "string", description: "Executor session id (update, optional — override the owner when taking a row over). The default follows the state transition: entering 待设计 or 在途 writes this session automatically. Leaving 在途, withdrawing or returning to 待讨论 clears it. 待核销 → 在途 always writes this session, and an explicit value does not override it." },
296
+ cursor: { type: "string", description: "Page cursor (query, optional): copy the previous page's tail line verbatim (form `<updated_at>|<id>`). A malformed cursor is refused — never silently treated as page one." },
248
297
  },
249
298
  additionalProperties: false,
250
299
  }
@@ -252,26 +301,32 @@ const UNION_PARAMETERS = {
252
301
  /** 变体构造(同名单对象、schema 窄化、两形永不同装配——先例 = `subagent` 工具 depth 面变体):
253
302
  * 全量(depth-0 五 action;`writeFace:true` —— `readonly:false` + 动作级分类钩子,写面保持侧效门,
254
303
  * query / count 归只读类 = 旧读二 `readonly:true` 同行为:planMode 放行 / 免审批)∥ 只读(depth>0
255
- * 二 action;`readonly:true`,写三 schema 级不可达)。形态显式声明(不按 actions 长度推导——增量
256
- * 变体不改错标文案)。 */
304
+ * 二 action;`readonly:true`,写三 schema 级不可达、写专用参数与写说明同样不出现)。
305
+ * 形态显式声明(不按 actions 长度推导——增量变体不改错标文案)。
306
+ *
307
+ * **只读变体的参数面 = 本变体各 action 的 `ACTION_SPECS` 并集**(TOOLS.md §6.29 B6:按角色生成 schema):
308
+ * 它拿不到 `title` / `evidence` / `id` 这类只有写动作消费的参数,也拿不到它们的说明;描述取自己的档
309
+ * (`ledger-read.md`)——全量档 `ledger.md` 通篇讲五动作,只读角色读到它就是读到做不到的事。 */
257
310
  function makeLedgerTool(actions, { readonly = false, writeFace = false } = {}) {
258
311
  const entryTool = { name: "ledger", parameters: { type: "object", required: ["action"], properties: { action: { type: "string", enum: actions } } } }
259
- return {
260
- name: "ledger",
261
- description: DESC("ledger"), // 文本单点 = tool-docs/ledger.md(五档要点合一)
262
- parameters: {
263
- ...UNION_PARAMETERS,
264
- properties: {
312
+ const readProps = {}
313
+ if (readonly) for (const a of actions) Object.assign(readProps, ACTION_SPECS[a].parameters.properties)
314
+ const properties = readonly
315
+ ? { action: { type: "string", enum: actions, description: "Which operation to run — read-only: `query` or `count`." }, ...readProps }
316
+ : {
265
317
  ...UNION_PARAMETERS.properties,
266
318
  action: {
267
319
  ...UNION_PARAMETERS.properties.action,
268
320
  enum: actions,
269
321
  description: writeFace
270
- ? "操作(五值):add 新增 / update 更新 / close 收口 / query 查询 / count 计数——写三仅主 agent 装配面"
271
- : "操作(只读二值):query 查询 / count 计数",
322
+ ? "Which operation to run. The three write actions are assembled for the main agent only."
323
+ : "Which operation to run — read-only: `query` or `count`.",
272
324
  },
273
- },
274
- },
325
+ }
326
+ return {
327
+ name: "ledger",
328
+ description: readonly ? DESC("ledger-read") : DESC("ledger"), // 文本单点 = tool-docs/{ledger,ledger-read}.md
329
+ parameters: { type: "object", required: ["action"], properties, additionalProperties: false },
275
330
  readonly,
276
331
  isReadonlyAction(args) { return READ_ACTIONS.includes(args?.action) },
277
332
  async execute(args, ctx) {
@@ -299,8 +354,8 @@ function makeLedgerTool(actions, { readonly = false, writeFace = false } = {}) {
299
354
  const { cwd, id, batch, ...patch } = rest
300
355
  const target = startOf(ctx, anchor, cwd, batch, patch.task_book, patch.req_doc)
301
356
  let executorSessionId
302
- if (patch.executor == null) {
303
- // 显式 `null` ≡ 略去(§3.1 `null` 口径 · #474——与缺省同分支);动态 import——零新静态边(K-LX1)
357
+ if (patch.executor == null || patch.status === "在途") {
358
+ // 回退到在途也必须供当前会话:显式旧 executor 不得覆盖机器语义。
304
359
  const { getSessionId } = await import("./session-slots.mjs")
305
360
  executorSessionId = getSessionId()
306
361
  }
@@ -315,9 +370,13 @@ function makeLedgerTool(actions, { readonly = false, writeFace = false } = {}) {
315
370
  return JSON.stringify({ ...ledgerClose({ cwd: target, id: rest.id, status: rest.status, evidence: rest.evidence, evidenceReplace: rest.evidenceReplace, boundary: anchor, agent: ctx?.agent }) })
316
371
  }
317
372
  case "query": {
373
+ // 分页(§7.2):游标形判先落(非法游标零库动作)→ 核取全量行 → 工具面排序切片 + 尾行。
318
374
  const target = startOf(ctx, anchor, rest.cwd, rest.task_book)
375
+ const cursor = rest.cursor == null ? null : parseQueryCursor(rest.cursor)
319
376
  assertLedgerStart(target, anchor)
320
- return JSON.stringify(ledgerQuery({ cwd: target, boundary: anchor, status: rest.status, kind: rest.kind, board: rest.board, trigger: rest.trigger, task_book: rest.task_book }), null, 2)
377
+ const rows = ledgerQuery({ cwd: target, boundary: anchor, status: rest.status, kind: rest.kind, board: rest.board, trigger: rest.trigger, task_book: rest.task_book })
378
+ const { page, tail } = queryPageOf(rows, cursor, queryEchoOf(rest))
379
+ return `${JSON.stringify(page, null, 2)}\n\n${tail}`
321
380
  }
322
381
  case "count": {
323
382
  const target = startOf(ctx, anchor, rest.cwd, rest.task_book)