@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
@@ -0,0 +1,160 @@
1
+ /**
2
+ * doc-check/existence.mjs — 判据 ③「悬空指针」:仓内档名在项目根下找不到,就报。
3
+ *
4
+ * 判据句单源 = `docs/core/design/DOC-CHECK.md` §3:**在项目根下找一次**——找到不报,找不到就报,路径原样带上。
5
+ * 没有「腿序」、没有声明仓、没有豁免表:一句「仓内找一次」就够(§3 末段)。
6
+ *
7
+ * 「找到」认两形,有其一就算在:项目根下这个相对路径在盘;**或**仓内有**任一**档以这个 token 结尾。
8
+ * **不判唯一**(用户 2026-11-12:「有就行,不要判唯一」):仓里两份、三份同名档都算在——那是仓库的状态,
9
+ * 不是引用的对错。判唯一会让同一条引用头一天通、次日仓里多一个同名档就变死,变的不是引用。
10
+ *
11
+ * 四条不报(每一条都是「不该在这里报」,不是宽容):
12
+ * - **仓外一律不看**(§4):`../` 出仓 ∥ 绝对路径出仓 ∥ 别仓的档 ⇒ 跳过,一个字都不出。门不知道别的仓长什么样,
13
+ * 报出来的都是噪音;真写错了是路径没写清,责任在人。
14
+ * - **运行期状态目录不判**(§3 第三个例外):`.thincoder/` 开头的 token ⇒ 跳过,一个字都不出。那是会话、记忆、
15
+ * 临时件、运行期操作数的家,不是文档区;判它等于把工具自己的家当当成读者要翻的资料。
16
+ * 这条跳过必须坐在**判据**这一侧:`pathIndexOf` 本来就不收 `.thincoder` 目录,索引里没有它,判据不跳就只有
17
+ * 「找不到」这一条路可走(盘上真有的那几份由 `existsSync` 先认下),`.thincoder/advisor.md` 会被判成档缺。
18
+ * 比对走 `pathKey`(与同档其余路径同一把尺):`./` 前缀先剥、win32 折小写——一条前缀,不是字面串,
19
+ * 更不是豁免表(没有文件名清单、没有逐条登记)。
20
+ * - **在建的档不报**(§3 末段):档名出现在某份在途批档 §2 机读清单里 ⇒ 不报——设计档写「这里要建一个档」时,
21
+ * 那个档本来就还不存在;批档收口后清单跟着失效,同一路径立刻回到「找不到就报」。
22
+ * - **批档整份不判悬空**(§3):批档是**可整份删掉的执行记录**,不是导航——读者不会照着 §1–§6 去找档。
23
+ * 它记的是「这一轮做了什么、发现了什么」,所以记录里出现一个不存在的档名是**发现**,不是指向空气;
24
+ * 判它等于把「某份档不存在」这条发现本身拦在门外。设计档 ∥ 需求档 ∥ 标准档是给人查的,照判。
25
+ */
26
+ import { existsSync, readFileSync, readdirSync } from "node:fs"
27
+ import { join, resolve } from "node:path"
28
+
29
+ import { machineListRowsOf, isBatchRecord, readBatchStatusLine } from "../agent-tools/batch-segment-vocab.mjs"
30
+ import { docRootPaths, readManifest } from "../manifest.mjs"
31
+ import { NAME_RE, isDocToken } from "./doc-tokens.mjs"
32
+ import { bodyLines } from "./paragraph.mjs"
33
+
34
+ /** 比较用的路径键:分隔符统一成 `/`,win32 折小写(盘上大小写不敏感)。 */
35
+ const pathKey = (p) => {
36
+ const s = String(p ?? "").replace(/\\/g, "/").replace(/^\.\//, "")
37
+ return process.platform === "win32" ? s.toLowerCase() : s
38
+ }
39
+
40
+ /** 仓内路径索引(**一次建成,逐 token 查表**):元素 = 任一尾部路径段;在表里就是「在」,不记命中几处
41
+ * (不判唯一——见档头)。「找一次」就是这一张表——不是腿序:没有基根顺序、没有声明仓、
42
+ * 没有引用方目录那一级。
43
+ * 入参 `root` 须是已 `resolve` 的根(不带尾分隔符——本函数按它的长度切仓根相对路径)。
44
+ * 读不到的子树不进索引 ⇒ 那里的档会被判档缺(误报方向,与「宁拦不放」一致):今天不记未核标记。 */
45
+ export function pathIndexOf(root) {
46
+ const seen = new Set()
47
+ const walk = (dir) => {
48
+ let entries
49
+ try { entries = readdirSync(dir, { withFileTypes: true }) } catch { return }
50
+ for (const e of entries) {
51
+ if (e.name === "node_modules" || e.name === ".git" || e.name === ".thincoder") continue
52
+ const p = join(dir, e.name)
53
+ if (e.isDirectory()) { walk(p); continue }
54
+ const rel = pathKey(p.slice(root.length + 1))
55
+ const parts = rel.split("/")
56
+ for (let i = 0; i < parts.length; i++) {
57
+ const suf = parts.slice(i).join("/")
58
+ seen.add(suf)
59
+ }
60
+ }
61
+ }
62
+ walk(root)
63
+ return seen
64
+ }
65
+
66
+ /** 批档目录(在途批档的家与批档面):manifest 声明了 `docRoot.batches` 就用它;显式 `null` = 本面无 ⇒ 空集;其余(缺键 ∥ 读不到 ∥ 空数组)按本仓约定 `docs/batches`。 */
67
+ export function batchDirs(root) {
68
+ try {
69
+ const man = readManifest(root)
70
+ if (man?.ok) {
71
+ const declared = man.manifest?.docRoot?.batches
72
+ // 显式 `null` = 「本面无」(KD-M1-37 的合法哨兵)⇒ 本项目没有批档面,也不回落到约定目录。
73
+ if (declared === null) return []
74
+ const paths = docRootPaths(declared, root)
75
+ if (Array.isArray(paths) && paths.length) return paths
76
+ }
77
+ } catch { /* 读不到声明面 ⇒ 走约定目录:判据不因声明面坏掉而停机 */ }
78
+ return [join(root, "docs", "batches")]
79
+ }
80
+
81
+ /**
82
+ * 这份档是不是**批档面**(落在 `batchDirs` 的任一根之下,子目录也算):是 ⇒ 整份不判悬空(§3)。
83
+ * 「批档根」与「在途批档的家」同取 `batchDirs` 一处——两个面各自的根若各算一份,同一个目录会在
84
+ * 判据面与非判据面之间摇摆(多仓 / 自定义 `docRoot.batches` 时先摆)。
85
+ * 面别只决定悬空判不判:段长 ∥ 行号坐标两判据对批档照旧生效。
86
+ * @param {string} abs 被判档的绝对路径
87
+ * @param {string} root 项目根
88
+ * @returns {boolean}
89
+ */
90
+ export function isBatchFace(abs, root) {
91
+ const key = pathKey(resolve(abs))
92
+ return batchDirs(resolve(root)).some((dir) => {
93
+ const d = pathKey(dir)
94
+ return key === d || key.startsWith(`${d}/`)
95
+ })
96
+ }
97
+
98
+ /**
99
+ * 在途批档 §2 机读清单的并集(**批量一次算,逐 token 查表**——不按 token 去扫目录)。
100
+ * 「在途」= 批档 §1 状态行不是 `已收口`:豁免期正好等于那一批的生命期,不需要谁去维护它。
101
+ * @param {string} root 项目根
102
+ * @returns {Set<string>} 归一后的仓根相对路径
103
+ */
104
+ export function plannedPathsOf(root) {
105
+ const out = new Set()
106
+ for (const dir of batchDirs(root)) {
107
+ let names
108
+ try { names = readdirSync(dir) } catch { continue }
109
+ for (const name of names) {
110
+ if (!name.toLowerCase().endsWith(".md")) continue
111
+ let src
112
+ try { src = readFileSync(join(dir, name), "utf8") } catch { continue }
113
+ if (!isBatchRecord(src)) continue
114
+ if (readBatchStatusLine(src) === "closed") continue
115
+ for (const row of machineListRowsOf(src)) out.add(pathKey(row.path))
116
+ }
117
+ }
118
+ return out
119
+ }
120
+
121
+ /** 该 token 是不是「别仓的档」:首段在项目根的**同级**下,且那里有 `PROJECT-MANIFEST.json`(另一个项目)。 */
122
+ function siblingProjectForm(token, root) {
123
+ const first = String(token).split("/")[0]
124
+ if (!first || first === "." || first === "..") return false
125
+ return existsSync(join(root, "..", first, "PROJECT-MANIFEST.json"))
126
+ }
127
+
128
+ /**
129
+ * 判据 ③:逐行找仓内档名,在项目根下找一次。
130
+ * 候选 = **带目录段的档名**(`docs/x/y.md` 这种写法):裸名没有可找的位置,判它只会把
131
+ * 「本文档的邻居」全算成悬空(本仓设计档互引就是这么写的)。候选恒带目录段 ⇒ 档名豁免谓词在这一面
132
+ * 不适用(`docNames` 对含 `/` 的 token 恒返回 null,豁免只免裸名)。
133
+ * @param {string} text 档全文(调用方读好的,本档不读盘)
134
+ * @param {{root:string, planned?:Set<string>, indexPath?:Set<string>}} o `root` = 项目根;
135
+ * `planned` = 在途批档清单并集;`indexPath` = 已建好的仓内路径索引(同一根跑多档时复用)
136
+ * @returns {{line:number, token:string}[]}
137
+ */
138
+ export function existenceHits(text, { root, planned = new Set(), indexPath = null } = {}) {
139
+ const base = resolve(root)
140
+ let index = indexPath
141
+ const out = []
142
+ for (const { no, text: l } of bodyLines(text)) {
143
+ const re = new RegExp(NAME_RE.source, "gu")
144
+ let m
145
+ while ((m = re.exec(l)) !== null) {
146
+ const token = m[1]
147
+ if (!token.includes("/")) continue // 裸名:没有可找的位置,不判
148
+ if (!isDocToken(token)) continue // 后缀不在档名清单内 ⇒ 不是档
149
+ if (token.startsWith("../") || token.startsWith("/")) continue // 出仓形:不看
150
+ if (pathKey(token).startsWith(".thincoder/")) continue // 运行期状态目录:不是文档区,一个字都不出(判据侧跳,不靠索引侧;`./` 与 win32 大小写同 `pathKey` 归一)
151
+ if (existsSync(join(base, token))) continue // 找到 ⇒ 不报
152
+ if (planned.has(pathKey(token))) continue // 在途批档 §2 清单上 ⇒ 在建,不报
153
+ if (index === null) index = pathIndexOf(base)
154
+ if (index.has(pathKey(token))) continue // 仓内有任一档以它结尾 ⇒ 在,不报(不判唯一)
155
+ if (siblingProjectForm(token, base)) continue // 别仓的档:不看
156
+ out.push({ line: no, token })
157
+ }
158
+ }
159
+ return out
160
+ }