@rt-tools/agent-kit 0.37.0 → 0.38.0

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 (436) hide show
  1. package/README.md +5 -5
  2. package/assets/checks/action-of.mjs +154 -0
  3. package/assets/checks/board-pull-state.github.mjs +82 -13
  4. package/assets/checks/board-runs.github.mjs +40 -5
  5. package/assets/checks/board.github.mjs +1 -1
  6. package/assets/checks/check-board.github.mjs +58 -19
  7. package/assets/checks/check-specs.mjs +2 -2
  8. package/assets/checks/check-state-next.mjs +22 -10
  9. package/assets/checks/check-turn-map.mjs +26 -6
  10. package/assets/checks/check-work-steps.mjs +4 -173
  11. package/assets/checks/decide.mjs +285 -0
  12. package/assets/checks/decision-table.json +21365 -0
  13. package/assets/checks/decision-table.mjs +495 -0
  14. package/assets/checks/epic-table.github.mjs +4 -3
  15. package/assets/checks/epic-window.mjs +98 -0
  16. package/assets/checks/flow-condition.mjs +64 -0
  17. package/assets/checks/flow-data.mjs +136 -0
  18. package/assets/checks/flow-forecast.mjs +330 -0
  19. package/assets/checks/flow-guard-run.mjs +90 -0
  20. package/assets/checks/flow-guard.mjs +294 -0
  21. package/assets/checks/flow-model.mjs +271 -0
  22. package/assets/checks/flow-override.mjs +130 -0
  23. package/assets/checks/flow-plan.mjs +206 -0
  24. package/assets/checks/flow-scaffold.mjs +224 -0
  25. package/assets/checks/flow-step.mjs +365 -0
  26. package/assets/checks/flow-sync.mjs +296 -0
  27. package/assets/checks/flow-syntax.mjs +292 -0
  28. package/assets/checks/flow-verdict.mjs +112 -0
  29. package/assets/checks/lib-common.mjs +10 -2
  30. package/assets/checks/lib-domains.mjs +1 -1
  31. package/assets/checks/lib-manifests.mjs +34 -2
  32. package/assets/checks/merge-read.mjs +239 -0
  33. package/assets/checks/owner-nudges.mjs +389 -0
  34. package/assets/checks/push-inputs.mjs +444 -0
  35. package/assets/checks/push-trace.mjs +108 -0
  36. package/assets/checks/record-count.mjs +410 -0
  37. package/assets/checks/record-recount.mjs +187 -0
  38. package/assets/checks/record-turn.mjs +155 -0
  39. package/assets/checks/rt-kit-checks.config.mjs +17 -2
  40. package/assets/checks/spec-anchors.mjs +4 -4
  41. package/assets/checks/spec-contract.mjs +5 -5
  42. package/assets/checks/task-flow-script.mjs +292 -0
  43. package/assets/checks/tree-assignment.mjs +26 -7
  44. package/assets/checks/work-result.mjs +281 -0
  45. package/assets/checks/work-steps-lib.mjs +205 -0
  46. package/assets/commands/next-session.md +3 -1
  47. package/assets/commands/night.md +28 -18
  48. package/assets/defaults/gate-map.sh +39 -12
  49. package/assets/defaults/merges.sh +48 -0
  50. package/assets/defaults/project.sh +43 -13
  51. package/assets/defaults/push.sh +23 -0
  52. package/assets/defaults/turn-map.md +24 -21
  53. package/assets/docs/GLOSSARY.md +6 -2
  54. package/assets/hooks/ask-judge.md +65 -0
  55. package/assets/hooks/browser-guard-device-id.sh +11 -9
  56. package/assets/hooks/browser-guard-no-asking.sh +12 -6
  57. package/assets/hooks/browser-guard-no-listing.sh +13 -7
  58. package/assets/hooks/browser-guard-no-other-drivers.sh +25 -17
  59. package/assets/hooks/browser-guard-require-select.sh +15 -15
  60. package/assets/hooks/commit-msg.sh +13 -3
  61. package/assets/hooks/conscience-guard.sh +9 -25
  62. package/assets/hooks/decide.sh +141 -0
  63. package/assets/hooks/deny-tail.sh +16 -11
  64. package/assets/hooks/dev-server-guard.sh +27 -20
  65. package/assets/hooks/dispatch.sh +47 -4
  66. package/assets/hooks/docs-guard.sh +31 -200
  67. package/assets/hooks/end-conversation-guard.sh +12 -21
  68. package/assets/hooks/epic-stop-guard.sh +54 -105
  69. package/assets/hooks/exam-guard.sh +32 -65
  70. package/assets/hooks/facts-docs.sh +221 -0
  71. package/assets/hooks/facts-flow.sh +173 -0
  72. package/assets/hooks/facts.sh +308 -0
  73. package/assets/hooks/flow-step-guard.sh +93 -0
  74. package/assets/hooks/git-guard-delivery-body.sh +4 -4
  75. package/assets/hooks/git-guard-delivery-conflict.sh +16 -10
  76. package/assets/hooks/git-guard-delivery-draft.sh +3 -3
  77. package/assets/hooks/git-guard-delivery-epic.sh +18 -9
  78. package/assets/hooks/git-guard-delivery-folder.sh +32 -7
  79. package/assets/hooks/git-guard-delivery-merge.sh +2 -2
  80. package/assets/hooks/git-guard-delivery-merged.sh +12 -1
  81. package/assets/hooks/git-guard-delivery-pipeline.sh +1 -1
  82. package/assets/hooks/git-guard-delivery-rerun.sh +32 -0
  83. package/assets/hooks/git-guard-delivery-signature.sh +82 -45
  84. package/assets/hooks/git-guard-delivery-task.sh +47 -0
  85. package/assets/hooks/git-guard-delivery.sh +112 -101
  86. package/assets/hooks/git-guard-discard.sh +8 -18
  87. package/assets/hooks/git-guard-main.sh +13 -19
  88. package/assets/hooks/git-guard-no-verify.sh +56 -16
  89. package/assets/hooks/git-guard-push-ahead.sh +46 -5
  90. package/assets/hooks/git-guard-push-marks.sh +227 -31
  91. package/assets/hooks/git-guard-push-tests.sh +51 -53
  92. package/assets/hooks/git-guard-tree-assignment.sh +7 -4
  93. package/assets/hooks/guard-note.sh +8 -6
  94. package/assets/hooks/handoff-entry-guard.sh +20 -39
  95. package/assets/hooks/handoff-write.sh +3 -1
  96. package/assets/hooks/hook-input.sh +0 -41
  97. package/assets/hooks/metric-marks.sh +19 -7
  98. package/assets/hooks/observe.sh +1 -1
  99. package/assets/hooks/override-write-guard.sh +27 -9
  100. package/assets/hooks/pre-push.sh +20 -10
  101. package/assets/hooks/prose-style-guard.sh +6 -19
  102. package/assets/hooks/pull-merges.sh +76 -0
  103. package/assets/hooks/push-set.sh +39 -7
  104. package/assets/hooks/qa-dataid-guard.sh +7 -17
  105. package/assets/hooks/record-turn.sh +52 -0
  106. package/assets/hooks/rerun-guard.sh +11 -16
  107. package/assets/hooks/reuse-first-guard.sh +7 -21
  108. package/assets/hooks/role-calls.sh +25 -3
  109. package/assets/hooks/rule-source-guard.sh +54 -21
  110. package/assets/hooks/skill-gate-rearm.sh +18 -5
  111. package/assets/hooks/skill-gate.sh +156 -20
  112. package/assets/hooks/skill-loaded.sh +18 -3
  113. package/assets/hooks/sql-guard-target.sh +2 -2
  114. package/assets/hooks/sql-guard-write.sh +12 -12
  115. package/assets/hooks/sql-guard.sh +58 -16
  116. package/assets/hooks/task-context-load.sh +3 -1
  117. package/assets/hooks/task-flow-context.sh +20 -22
  118. package/assets/hooks/task-flow-draft-guard.sh +25 -74
  119. package/assets/hooks/task-flow-guard.sh +28 -129
  120. package/assets/hooks/tree-assignment-guard.sh +25 -14
  121. package/assets/hooks/turn-entry-load.sh +3 -1
  122. package/assets/hooks/turn-exit-guard.sh +92 -452
  123. package/assets/hooks/turn-exit-role.sh +47 -0
  124. package/assets/hooks/turn-judge.md +103 -0
  125. package/assets/hooks/window-fill-guard.sh +22 -41
  126. package/assets/laws/work-conduct.md +9 -8
  127. package/assets/patterns/autonomous-work-run.md +35 -10
  128. package/assets/patterns/browser-verification-measure.md +10 -20
  129. package/assets/patterns/browser-verification-stand.md +30 -42
  130. package/assets/patterns/doc-style-human.md +0 -6
  131. package/assets/patterns/doc-style-sweep.md +7 -7
  132. package/assets/patterns/doc-style-trace.md +5 -4
  133. package/assets/patterns/doc-style-write.md +7 -7
  134. package/assets/patterns/epic-flow-declare.md +15 -8
  135. package/assets/patterns/git-workflow-commit.azure.md +9 -155
  136. package/assets/patterns/git-workflow-commit.github.md +11 -269
  137. package/assets/patterns/git-workflow-commit.gitlab.md +9 -161
  138. package/assets/patterns/git-workflow-docker.md +8 -231
  139. package/assets/patterns/git-workflow-freshness.md +9 -56
  140. package/assets/patterns/git-workflow-merge.md +7 -174
  141. package/assets/patterns/git-workflow-migration.md +5 -73
  142. package/assets/patterns/git-workflow-pr-body.github.md +6 -159
  143. package/assets/patterns/git-workflow-pr-ready.md +9 -69
  144. package/assets/patterns/git-workflow-pr.azure.md +14 -220
  145. package/assets/patterns/git-workflow-pr.github.md +13 -253
  146. package/assets/patterns/git-workflow-pr.gitlab.md +14 -241
  147. package/assets/patterns/git-workflow-restart.md +5 -41
  148. package/assets/patterns/git-workflow-secrets.md +10 -76
  149. package/assets/patterns/git-workflow-stack.md +9 -205
  150. package/assets/patterns/lib-layers-new.md +7 -3
  151. package/assets/patterns/queue-audit-read.github.md +9 -3
  152. package/assets/patterns/spec-driven-domain.md +50 -48
  153. package/assets/patterns/spec-driven-rule.md +18 -19
  154. package/assets/patterns/status-report-table.github.md +2 -2
  155. package/assets/patterns/task-flow-archive.md +15 -256
  156. package/assets/patterns/task-flow-close.md +16 -291
  157. package/assets/patterns/task-flow-cycle.md +13 -39
  158. package/assets/patterns/task-flow-graph.needs-graph.md +8 -58
  159. package/assets/patterns/task-flow-handoff.md +13 -161
  160. package/assets/patterns/task-flow-resume.md +19 -183
  161. package/assets/patterns/task-flow-start.md +9 -248
  162. package/assets/patterns/testing-e2e.md +14 -18
  163. package/assets/patterns/testing-unit.md +9 -13
  164. package/assets/patterns/turn-entry-map.md +15 -5
  165. package/assets/pitfalls/agent-kit-extend.md +200 -0
  166. package/assets/pitfalls/agent-kit.2.md +323 -0
  167. package/assets/pitfalls/angular-patterns.md +98 -0
  168. package/assets/pitfalls/autonomous-work.md +15 -0
  169. package/assets/pitfalls/browser-verification.md +265 -0
  170. package/assets/pitfalls/component-structure.md +98 -0
  171. package/assets/pitfalls/dependencies.md +92 -0
  172. package/assets/pitfalls/deploy-flow.azure.md +171 -0
  173. package/assets/pitfalls/deploy-flow.github.md +211 -0
  174. package/assets/pitfalls/deploy-flow.gitlab.md +173 -0
  175. package/assets/pitfalls/doc-style.2.md +286 -0
  176. package/assets/pitfalls/doc-style.md +3 -3
  177. package/assets/pitfalls/entity-conventions.needs-admin.md +96 -0
  178. package/assets/pitfalls/entity-models.md +89 -0
  179. package/assets/pitfalls/git-workflow-commit.azure.md +190 -0
  180. package/assets/pitfalls/git-workflow-commit.github.md +307 -0
  181. package/assets/pitfalls/git-workflow-commit.gitlab.md +196 -0
  182. package/assets/pitfalls/git-workflow-docker.md +275 -0
  183. package/assets/pitfalls/git-workflow-freshness.md +88 -0
  184. package/assets/pitfalls/git-workflow-hosting.azure.md +31 -0
  185. package/assets/pitfalls/git-workflow-hosting.github.md +32 -0
  186. package/assets/pitfalls/git-workflow-hosting.gitlab.md +31 -0
  187. package/assets/pitfalls/git-workflow-merge.md +220 -0
  188. package/assets/pitfalls/git-workflow-migration.md +103 -0
  189. package/assets/pitfalls/git-workflow-pr-body.github.md +185 -0
  190. package/assets/pitfalls/git-workflow-pr-ready.md +104 -0
  191. package/assets/pitfalls/git-workflow-pr.2.azure.md +172 -0
  192. package/assets/pitfalls/git-workflow-pr.2.github.md +184 -0
  193. package/assets/pitfalls/git-workflow-pr.2.gitlab.md +180 -0
  194. package/assets/pitfalls/git-workflow-pr.azure.md +159 -0
  195. package/assets/pitfalls/git-workflow-pr.github.md +107 -0
  196. package/assets/pitfalls/git-workflow-pr.gitlab.md +151 -0
  197. package/assets/pitfalls/git-workflow-push.azure.md +30 -0
  198. package/assets/pitfalls/git-workflow-push.github.md +30 -0
  199. package/assets/pitfalls/git-workflow-push.gitlab.md +30 -0
  200. package/assets/pitfalls/git-workflow-queue.azure.md +30 -0
  201. package/assets/pitfalls/git-workflow-queue.github.md +30 -0
  202. package/assets/pitfalls/git-workflow-queue.gitlab.md +30 -0
  203. package/assets/pitfalls/git-workflow-restart.md +72 -0
  204. package/assets/pitfalls/git-workflow-secrets.md +114 -0
  205. package/assets/pitfalls/git-workflow-stack.md +254 -0
  206. package/assets/pitfalls/git-workflow.2.azure.md +291 -0
  207. package/assets/pitfalls/git-workflow.2.github.md +294 -0
  208. package/assets/pitfalls/git-workflow.2.gitlab.md +278 -0
  209. package/assets/pitfalls/git-workflow.github.md +5 -9
  210. package/assets/pitfalls/lib-layers.md +114 -0
  211. package/assets/pitfalls/lists.md +117 -0
  212. package/assets/pitfalls/navigation.md +94 -0
  213. package/assets/pitfalls/reuse-first.md +201 -0
  214. package/assets/pitfalls/spec-driven.2.md +318 -0
  215. package/assets/pitfalls/spec-driven.md +9 -7
  216. package/assets/pitfalls/styling-bem.2.md +125 -0
  217. package/assets/pitfalls/task-flow-archive.md +264 -0
  218. package/assets/pitfalls/task-flow-close.2.md +165 -0
  219. package/assets/pitfalls/task-flow-close.md +167 -0
  220. package/assets/pitfalls/task-flow-cycle.md +73 -0
  221. package/assets/pitfalls/task-flow-graph.needs-graph.md +98 -0
  222. package/assets/pitfalls/task-flow-handoff.md +200 -0
  223. package/assets/pitfalls/task-flow-handover.md +30 -0
  224. package/assets/pitfalls/task-flow-owner.md +29 -0
  225. package/assets/pitfalls/task-flow-plan.md +29 -0
  226. package/assets/pitfalls/task-flow-resume.md +257 -0
  227. package/assets/pitfalls/task-flow-start.md +287 -0
  228. package/assets/pitfalls/task-flow.2.md +303 -0
  229. package/assets/pitfalls/task-flow.md +18 -29
  230. package/assets/pitfalls/testing.2.md +279 -0
  231. package/assets/pitfalls/testing.md +11 -12
  232. package/assets/pitfalls/turn-conduct.md +17 -18
  233. package/assets/pitfalls/typescript-conventions.md +161 -0
  234. package/assets/pitfalls/write-a-skill.md +128 -0
  235. package/assets/rules/angular-patterns.md +21 -86
  236. package/assets/rules/autonomous-work.md +37 -29
  237. package/assets/rules/browser-verification.md +28 -251
  238. package/assets/rules/component-structure.md +25 -87
  239. package/assets/rules/dependencies.md +20 -78
  240. package/assets/rules/deploy-flow.azure.md +27 -134
  241. package/assets/rules/deploy-flow.github.md +34 -196
  242. package/assets/rules/deploy-flow.gitlab.md +28 -137
  243. package/assets/rules/doc-style.md +32 -282
  244. package/assets/rules/entity-conventions.needs-admin.md +26 -82
  245. package/assets/rules/entity-models.md +14 -63
  246. package/assets/rules/epic-flow.md +5 -3
  247. package/assets/rules/git-workflow-hosting.azure.md +26 -0
  248. package/assets/rules/git-workflow-hosting.github.md +38 -0
  249. package/assets/rules/git-workflow-hosting.gitlab.md +27 -0
  250. package/assets/rules/git-workflow-push.azure.md +28 -0
  251. package/assets/rules/git-workflow-push.github.md +28 -0
  252. package/assets/rules/git-workflow-push.gitlab.md +28 -0
  253. package/assets/rules/git-workflow-queue.azure.md +23 -0
  254. package/assets/rules/git-workflow-queue.github.md +30 -0
  255. package/assets/rules/git-workflow-queue.gitlab.md +23 -0
  256. package/assets/rules/git-workflow.azure.md +21 -264
  257. package/assets/rules/git-workflow.github.md +24 -285
  258. package/assets/rules/git-workflow.gitlab.md +21 -258
  259. package/assets/rules/lib-layers.md +30 -103
  260. package/assets/rules/lists.md +21 -105
  261. package/assets/rules/navigation.md +10 -69
  262. package/assets/rules/queue-audit.github.md +5 -3
  263. package/assets/rules/reuse-first.md +20 -195
  264. package/assets/rules/spec-driven.md +35 -316
  265. package/assets/rules/status-report.md +5 -3
  266. package/assets/rules/styling-bem.md +20 -117
  267. package/assets/rules/task-flow-handover.md +24 -0
  268. package/assets/rules/task-flow-owner.md +34 -0
  269. package/assets/rules/task-flow-plan.md +38 -0
  270. package/assets/rules/task-flow.md +22 -289
  271. package/assets/rules/testing.md +32 -270
  272. package/assets/rules/turn-conduct.md +109 -111
  273. package/assets/rules/turn-entry.md +2 -2
  274. package/assets/rules/typescript-conventions.md +41 -147
  275. package/assets/samples/tasks/_template/plan.md +30 -6
  276. package/assets/samples/tasks/_template/progress.md +0 -1
  277. package/assets/skills/agent-kit-extend.md +25 -168
  278. package/assets/skills/agent-kit.md +27 -299
  279. package/assets/skills/write-a-skill.md +20 -106
  280. package/assets/task-flow/flow.js +188 -0
  281. package/assets/task-flow/flow.md +102 -0
  282. package/assets/task-flow/measure/agreement.mjs +24 -0
  283. package/assets/task-flow/measure/copy.mjs +22 -0
  284. package/assets/task-flow/measure/epic-of.mjs +50 -0
  285. package/assets/task-flow/measure/epic-text.mjs +35 -0
  286. package/assets/task-flow/measure/epic.mjs +114 -0
  287. package/assets/task-flow/measure/folder.mjs +16 -0
  288. package/assets/task-flow/measure/footprint.mjs +28 -0
  289. package/assets/task-flow/measure/grill.mjs +21 -0
  290. package/assets/task-flow/measure/host.mjs +8 -0
  291. package/assets/task-flow/measure/owner.mjs +20 -0
  292. package/assets/task-flow/measure/plan.mjs +27 -0
  293. package/assets/task-flow/measure/pr.mjs +20 -0
  294. package/assets/task-flow/measure/progress.mjs +50 -0
  295. package/assets/task-flow/measure/session.mjs +175 -0
  296. package/assets/task-flow/measure/task.mjs +23 -0
  297. package/assets/task-flow/measure/text.mjs +77 -0
  298. package/assets/task-flow/source/git.mjs +86 -0
  299. package/assets/task-flow/source/host.mjs +172 -0
  300. package/assets/task-flow/source/marks.mjs +56 -0
  301. package/assets/task-flow/source/owner.mjs +74 -0
  302. package/assets/task-flow/source/records.mjs +116 -0
  303. package/assets/task-flow/source/session.mjs +232 -0
  304. package/assets/task-flow/source/steps.mjs +28 -0
  305. package/assets/task-flow/source/tree.mjs +42 -0
  306. package/assets/templates/rule.md +2 -2
  307. package/bin/agent-kit.d.ts.map +1 -1
  308. package/bin/agent-kit.js +30 -0
  309. package/bin/agent-kit.js.map +1 -1
  310. package/lib/agent-checks.d.ts +55 -0
  311. package/lib/agent-checks.d.ts.map +1 -0
  312. package/lib/agent-checks.js +209 -0
  313. package/lib/agent-checks.js.map +1 -0
  314. package/lib/assets.d.ts.map +1 -1
  315. package/lib/assets.js +7 -2
  316. package/lib/assets.js.map +1 -1
  317. package/lib/cargo-prose.d.ts +2 -1
  318. package/lib/cargo-prose.d.ts.map +1 -1
  319. package/lib/cargo-prose.js.map +1 -1
  320. package/lib/cargo.d.ts +29 -0
  321. package/lib/cargo.d.ts.map +1 -1
  322. package/lib/cargo.js +27 -1
  323. package/lib/cargo.js.map +1 -1
  324. package/lib/cascade.d.ts.map +1 -1
  325. package/lib/cascade.js +3 -1
  326. package/lib/cascade.js.map +1 -1
  327. package/lib/commands.d.ts.map +1 -1
  328. package/lib/commands.js +78 -2
  329. package/lib/commands.js.map +1 -1
  330. package/lib/companion.d.ts +0 -5
  331. package/lib/companion.d.ts.map +1 -1
  332. package/lib/companion.js +1 -1
  333. package/lib/companion.js.map +1 -1
  334. package/lib/config.d.ts +18 -0
  335. package/lib/config.d.ts.map +1 -1
  336. package/lib/config.js +18 -1
  337. package/lib/config.js.map +1 -1
  338. package/lib/cost.d.ts +0 -8
  339. package/lib/cost.d.ts.map +1 -1
  340. package/lib/cost.js +4 -4
  341. package/lib/cost.js.map +1 -1
  342. package/lib/epic-window-send.d.ts +74 -0
  343. package/lib/epic-window-send.d.ts.map +1 -0
  344. package/lib/epic-window-send.js +166 -0
  345. package/lib/epic-window-send.js.map +1 -0
  346. package/lib/hooks-map.d.ts +4 -12
  347. package/lib/hooks-map.d.ts.map +1 -1
  348. package/lib/hooks-map.js +4 -27
  349. package/lib/hooks-map.js.map +1 -1
  350. package/lib/integrity.d.ts +3 -0
  351. package/lib/integrity.d.ts.map +1 -1
  352. package/lib/integrity.js +7 -0
  353. package/lib/integrity.js.map +1 -1
  354. package/lib/metric-lines.d.ts +2 -1
  355. package/lib/metric-lines.d.ts.map +1 -1
  356. package/lib/metric-lines.js +49 -1
  357. package/lib/metric-lines.js.map +1 -1
  358. package/lib/metric-send-mark.d.ts +6 -3
  359. package/lib/metric-send-mark.d.ts.map +1 -1
  360. package/lib/metric-send-mark.js +15 -5
  361. package/lib/metric-send-mark.js.map +1 -1
  362. package/lib/metric-send.d.ts +6 -2
  363. package/lib/metric-send.d.ts.map +1 -1
  364. package/lib/metric-send.js +47 -13
  365. package/lib/metric-send.js.map +1 -1
  366. package/lib/metrics.d.ts +17 -0
  367. package/lib/metrics.d.ts.map +1 -1
  368. package/lib/metrics.js +26 -15
  369. package/lib/metrics.js.map +1 -1
  370. package/lib/observations-cargo.d.ts +1 -5
  371. package/lib/observations-cargo.d.ts.map +1 -1
  372. package/lib/observations-cargo.js +2 -2
  373. package/lib/observations-cargo.js.map +1 -1
  374. package/lib/observations.d.ts +0 -2
  375. package/lib/observations.d.ts.map +1 -1
  376. package/lib/observations.js +3 -3
  377. package/lib/observations.js.map +1 -1
  378. package/lib/order.d.ts +0 -2
  379. package/lib/order.d.ts.map +1 -1
  380. package/lib/order.js +0 -4
  381. package/lib/order.js.map +1 -1
  382. package/lib/outcomes.d.ts +7 -9
  383. package/lib/outcomes.d.ts.map +1 -1
  384. package/lib/outcomes.js +8 -7
  385. package/lib/outcomes.js.map +1 -1
  386. package/lib/proposals.d.ts +0 -1
  387. package/lib/proposals.d.ts.map +1 -1
  388. package/lib/proposals.js +1 -1
  389. package/lib/proposals.js.map +1 -1
  390. package/lib/push-gate.d.ts +13 -0
  391. package/lib/push-gate.d.ts.map +1 -1
  392. package/lib/push-gate.js +11 -7
  393. package/lib/push-gate.js.map +1 -1
  394. package/lib/push-set-usefulness.d.ts +78 -0
  395. package/lib/push-set-usefulness.d.ts.map +1 -0
  396. package/lib/push-set-usefulness.js +225 -0
  397. package/lib/push-set-usefulness.js.map +1 -0
  398. package/lib/shipment.d.ts +2 -11
  399. package/lib/shipment.d.ts.map +1 -1
  400. package/lib/shipment.fixture.d.ts +0 -3
  401. package/lib/shipment.fixture.d.ts.map +1 -1
  402. package/lib/shipment.fixture.js +3 -3
  403. package/lib/shipment.fixture.js.map +1 -1
  404. package/lib/shipment.js +2 -2
  405. package/lib/shipment.js.map +1 -1
  406. package/lib/sync.d.ts +10 -0
  407. package/lib/sync.d.ts.map +1 -1
  408. package/lib/sync.js +14 -1
  409. package/lib/sync.js.map +1 -1
  410. package/lib/traits.d.ts +0 -3
  411. package/lib/traits.d.ts.map +1 -1
  412. package/lib/traits.js +2 -2
  413. package/lib/traits.js.map +1 -1
  414. package/lib/variants.d.ts +0 -1
  415. package/lib/variants.d.ts.map +1 -1
  416. package/lib/variants.js +1 -1
  417. package/lib/variants.js.map +1 -1
  418. package/lib/work-result.d.ts +43 -0
  419. package/lib/work-result.d.ts.map +1 -0
  420. package/lib/work-result.js +39 -0
  421. package/lib/work-result.js.map +1 -0
  422. package/package.json +1 -1
  423. package/assets/checks/signals/ui-kit.json +0 -11
  424. package/assets/hooks/claim-guard.sh +0 -212
  425. package/assets/hooks/grill-gate-ask.sh +0 -26
  426. package/assets/hooks/grill-gate-bypass.sh +0 -61
  427. package/assets/hooks/grill-gate-options.sh +0 -50
  428. package/assets/hooks/grill-gate.sh +0 -482
  429. package/assets/hooks/postmortem-guard.sh +0 -124
  430. package/assets/hooks/proposal-guard.sh +0 -154
  431. package/assets/hooks/stand-login-guard.sh +0 -107
  432. package/assets/hooks/turn-exit-epic.sh +0 -173
  433. package/assets/hooks/turn-exit-patterns.sh +0 -123
  434. package/assets/hooks/turn-exit-verdict.sh +0 -149
  435. package/assets/hooks/waiting-turn-guard.sh +0 -320
  436. package/assets/hooks/work-start-guard.sh +0 -175
@@ -0,0 +1,495 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * The decision table of the package hooks: the facts of the tree and an action give «allow»,
4
+ * «deny» or «ask». Every hook is described here by rows or by a mark, and five modes read it.
5
+ *
6
+ * --validate the table parses: every row names a known area, facts, values and action
7
+ * --coverage every file of the hooks directory has a row or a mark: `deciding K of K, helpers M`
8
+ * --conform every row with a probe matches the hook run on that probe: `rows N, mismatch M`
9
+ * --walk every state of every area keeps an allowed action, and every refusal names an
10
+ * action allowed in the same state: `states N, ask-only A`, then `dead D, unnamed U`
11
+ * --chains the pairs where the named move of one row is an action another row may deny, each
12
+ * with its scenario: `derived N, scenarios S, uncovered U`
13
+ * --mutate each refusal row turned to «allow» for one probe run; a hook that decides by the
14
+ * table allows: `rows N, followed F`
15
+ * --reverse the named move of each refusal asked of every hook that judges that action, in the
16
+ * state of the refusal: none may refuse it — `moves N, refused R`
17
+ * --snapshot-save <file>, --snapshot-compare <file>
18
+ * the answers of all probes saved before a change of the hooks and compared after
19
+ * it: `saved N`, then `rows N, changed C`
20
+ *
21
+ * A state is one combination of the values of the facts of one area. Areas judge disjoint facts,
22
+ * so the walk goes area by area: the product over all hooks has no end, the product inside an area
23
+ * does.
24
+ *
25
+ * The decision of an action in a state: «deny» when any row denies it, otherwise «ask» when any
26
+ * row asks, otherwise «allow». An action decided «ask» is lawful — the owner decides in the window.
27
+ * A state whose only lawful action is the question to the owner is ask-only, named apart.
28
+ *
29
+ * FAIL-OPEN: no hooks directory — coverage has nothing to count and says so; no test harness next
30
+ * to the package — conformance has nothing to run with and says so. Both exit zero.
31
+ *
32
+ * Exit: 0 when the mode found nothing, 1 when it found a defect, 2 when the table does not parse.
33
+ */
34
+ import { spawn } from 'node:child_process';
35
+ import { availableParallelism } from 'node:os';
36
+ import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
37
+ import { dirname, join } from 'node:path';
38
+ import { fileURLToPath } from 'node:url';
39
+
40
+ import { decide } from './decide.mjs';
41
+
42
+ const HERE = dirname(fileURLToPath(import.meta.url));
43
+ const TABLE = process.env.RT_DECISION_TABLE ?? join(HERE, 'decision-table.json');
44
+ const HOOKS = process.env.RT_DECISION_HOOKS ?? [join(HERE, '../hooks'), join(process.cwd(), '.claude/hooks')].find(existsSync);
45
+ const HARNESS = join(HERE, '../../tests/lib.sh');
46
+ const SCENARIOS = process.env.RT_DECISION_SCENARIOS ?? join(process.cwd(), 'docs/specs/agent-kit/decision-table/scenarios.md');
47
+
48
+ const DECISIONS = new Set(['allow', 'deny', 'ask']);
49
+ const KINDS = new Set(['guard', 'helper', 'model']);
50
+
51
+ function load() {
52
+ try {
53
+ // The copy laid out in a tree carries the layout header on its first line.
54
+ return JSON.parse(readFileSync(TABLE, 'utf8').replace(/^#[^\n]*\n/, ''));
55
+ } catch (error) {
56
+ console.error(`decision-table: the table does not parse — ${error.message}`);
57
+ process.exit(2);
58
+ }
59
+ }
60
+
61
+ /** Every problem of the table's shape, one line each. */
62
+ export function problemsOf(table) {
63
+ const problems = [];
64
+ const areas = table.areas ?? {};
65
+ const hooks = new Map((table.hooks ?? []).map((h) => [h.file, h]));
66
+ for (const [file, hook] of hooks) {
67
+ if (!KINDS.has(hook.kind)) problems.push(`${file}: unknown kind «${hook.kind}»`);
68
+ if (hook.kind !== 'guard' && !hook.reason) problems.push(`${file}: a ${hook.kind} mark carries no reason`);
69
+ }
70
+ for (const [name, area] of Object.entries(areas)) {
71
+ if (area.ask && !area.actions.includes(area.ask)) problems.push(`area ${name}: the question «${area.ask}» is not its action`);
72
+ }
73
+ (table.rows ?? []).forEach((row, index) => {
74
+ const at = `row ${index + 1} (${row.hook ?? '?'})`;
75
+ const area = areas[row.area];
76
+ if (!hooks.has(row.hook)) problems.push(`${at}: the hook has no entry`);
77
+ else if (hooks.get(row.hook).kind !== 'guard') problems.push(`${at}: a ${hooks.get(row.hook).kind} has a row`);
78
+ if (!area) return problems.push(`${at}: unknown area «${row.area}»`);
79
+ if (!area.actions.includes(row.action)) problems.push(`${at}: the area has no action «${row.action}»`);
80
+ if (!DECISIONS.has(row.decision)) problems.push(`${at}: unknown decision «${row.decision}»`);
81
+ // A refusal may name a move of another area: the action names are one across the table.
82
+ if (row.decision === 'deny' && !Object.values(table.areas).some((a) => a.actions.includes(row.move))) {
83
+ problems.push(`${at}: a refusal names no action of any area`);
84
+ }
85
+ for (const [fact, value] of Object.entries(row.when ?? {})) {
86
+ const values = area.facts[fact];
87
+ if (!values) problems.push(`${at}: the area has no fact «${fact}»`);
88
+ else if ([value].flat().some((v) => !values.includes(v))) problems.push(`${at}: «${fact}» has no value «${value}»`);
89
+ }
90
+ });
91
+ (table.states ?? []).forEach((state, index) => {
92
+ const at = `state ${index + 1} (${state.hook ?? '?'})`;
93
+ const area = areas[state.area];
94
+ if (!hooks.has(state.hook)) problems.push(`${at}: the hook has no entry`);
95
+ else if (!hooks.get(state.hook).asks) problems.push(`${at}: the hook asks no neighbour, so it composes nothing`);
96
+ if (!area) return problems.push(`${at}: unknown area «${state.area}»`);
97
+ if (!area.actions.includes(state.action)) problems.push(`${at}: the area has no action «${state.action}»`);
98
+ if (!state.probe) problems.push(`${at}: a state carries no probe`);
99
+ for (const [fact, value] of Object.entries(state.when ?? {})) {
100
+ const values = area.facts[fact];
101
+ if (!values) problems.push(`${at}: the area has no fact «${fact}»`);
102
+ else if ([value].flat().some((v) => !values.includes(v))) problems.push(`${at}: «${fact}» has no value «${value}»`);
103
+ }
104
+ });
105
+ return problems;
106
+ }
107
+
108
+ /** The condition of a row as a map of fact to the set of values it admits. */
109
+ const condition = (row) => Object.fromEntries(Object.entries(row.when ?? {}).map(([fact, value]) => [fact, [value].flat()]));
110
+
111
+ /** Two conditions meet in some state: every fact they share admits a common value. */
112
+ export function meet(a, b) {
113
+ const merged = { ...a };
114
+ for (const [fact, values] of Object.entries(b)) {
115
+ if (!merged[fact]) merged[fact] = values;
116
+ else {
117
+ merged[fact] = merged[fact].filter((v) => values.includes(v));
118
+ if (merged[fact].length === 0) return null;
119
+ }
120
+ }
121
+ return merged;
122
+ }
123
+
124
+ /**
125
+ * A state inside a condition where every listed action is denied, or null. Search with backtracking;
126
+ * the actions with the fewest deny rows go first, so an action nothing denies ends the search at once
127
+ * instead of after every combination of the others.
128
+ */
129
+ function cover(denyRows, actions, start) {
130
+ const count = (action) => denyRows.filter((r) => r.action === action).length;
131
+ return coverOrdered(denyRows, [...actions].sort((a, b) => count(a) - count(b)), start);
132
+ }
133
+
134
+ function coverOrdered(denyRows, actions, start) {
135
+ if (actions.length === 0) return start;
136
+ const [action, ...rest] = actions;
137
+ for (const row of denyRows.filter((r) => r.action === action)) {
138
+ const merged = meet(start, condition(row));
139
+ if (!merged) continue;
140
+ const found = coverOrdered(denyRows, rest, merged);
141
+ if (found) return found;
142
+ }
143
+ return null;
144
+ }
145
+
146
+ /**
147
+ * The walk of one table. The states of an area are counted, not enumerated: an area of forty-eight
148
+ * facts has no end. A refusal is a defect when another row of the area may deny its named move in
149
+ * a state where the refusal itself fires — their conditions meet. A dead state is a condition where
150
+ * every action is denied; an ask-only one is where every action but the question is denied and no
151
+ * row may deny the question.
152
+ */
153
+ export function walk(table) {
154
+ const result = { states: 0n, askOnly: 0, dead: [], unnamed: [] };
155
+ const allDeny = table.rows.filter((row) => row.decision === 'deny');
156
+ for (const [name, area] of Object.entries(table.areas)) {
157
+ result.states += Object.values(area.facts).reduce((n, values) => n * BigInt(values.length), 1n);
158
+ const deny = table.rows.filter((row) => row.area === name && row.decision === 'deny');
159
+ const dead = cover(deny, area.actions, {});
160
+ if (dead) result.dead.push({ area: name, state: dead });
161
+ if (area.ask) {
162
+ const others = area.actions.filter((action) => action !== area.ask);
163
+ const state = cover(deny, others, {});
164
+ if (state && !deny.some((row) => row.action === area.ask && meet(state, condition(row)))) result.askOnly += 1;
165
+ }
166
+ // The named move is looked for in every area, not only in the refusal's own: an action
167
+ // name and a fact name mean one thing across the table, and a refusal of one area may name
168
+ // a move a hook of another area denies.
169
+ for (const row of deny) {
170
+ const by = allDeny.filter((other) => other !== row && other.action === row.move && meet(condition(row), condition(other)));
171
+ if (by.length > 0) result.unnamed.push({ area: name, state: meet(condition(row), condition(by[0])), row, by });
172
+ }
173
+ }
174
+ return result;
175
+ }
176
+
177
+ export const rowId = (row) => `${row.hook}:${row.action}:${row.decision}`;
178
+
179
+ /**
180
+ * The pairs where the named move of a refusal is an action another row of any area may deny. Two
181
+ * rows of one hook and action differ by their conditions, so every pair of rows is its own chain,
182
+ * even when its id repeats another's.
183
+ */
184
+ export function chainsOf(table) {
185
+ const pairs = [];
186
+ for (const row of table.rows.filter((r) => r.decision === 'deny')) {
187
+ for (const other of table.rows) {
188
+ if (other === row || other.decision !== 'deny' || other.action !== row.move) continue;
189
+ pairs.push({ id: `${rowId(row)} → ${rowId(other)}`, row, other });
190
+ }
191
+ }
192
+ return pairs;
193
+ }
194
+
195
+ /**
196
+ * The items of a scenarios file: a line starting with «- » and the lines indented under it. A chain
197
+ * stands in an item that starts with its refusal and names the row denying its move.
198
+ */
199
+ export function chainItems(text) {
200
+ return text.split(/\n(?! {2})/).filter((item) => item.startsWith('- `'));
201
+ }
202
+
203
+ export const chainWritten = (items, chain) =>
204
+ items.some((item) => item.startsWith(`- \`${rowId(chain.row)}\``) && item.includes(`\`${rowId(chain.other)}\``));
205
+
206
+ function hookFiles() {
207
+ if (!HOOKS) return null;
208
+ return readdirSync(HOOKS).filter((file) => /\.(sh|md)$/.test(file)).sort();
209
+ }
210
+
211
+ /**
212
+ * One probe run through the test harness: the decision — «allow», «ask» or «deny» — and the text
213
+ * the hook gave with it. A refusal comes two ways: exit 2 with the reason on stderr, or JSON with a
214
+ * decision and its reason.
215
+ */
216
+ /**
217
+ * Runs the hook of a row on the fixture of its facts. Each probe keeps its stderr in a file of its
218
+ * own: the probes run side by side, and a shared file would hand one probe the text of another.
219
+ */
220
+ function runProbe(row, extraEnv = {}) {
221
+ const script = [
222
+ `. "${HARNESS}"`,
223
+ // The harness drops every RT_ variable of the environment; the flipped row comes under another name.
224
+ '[ -n "${PROBE_FLIP:-}" ] && export RT_DECISION_FLIP="$PROBE_FLIP"',
225
+ // A rule within the host limit the gate puts into the session itself, and the fact of a
226
+ // loaded rule counts it. The table speaks of the rule the session has not got: the probes
227
+ // run under a limit nothing fits, and the delivery is held by the suites of the gate.
228
+ 'export RT_GATE_CONTEXT_LIMIT=1',
229
+ 'probe_err="$(mktemp)"',
230
+ row.probe.setup ?? '',
231
+ `json="$(${row.probe.input})"`,
232
+ `out="$(printf '%s' "$json" | "$HOOKS/${row.hook}" 2>"$probe_err")"; code=$?`,
233
+ `if [ "$code" = 2 ]; then echo deny; elif [ -z "$out" ]; then echo allow; else printf '%s' "$out" | jq -r '.hookSpecificOutput.permissionDecision // .decision // "allow"' | sed 's/^block$/deny/'; fi`,
234
+ `printf '%s' "$out" | jq -r '.hookSpecificOutput.permissionDecisionReason // .reason // empty' 2>/dev/null | tr '\\n' ' '`,
235
+ `tr '\\n' ' ' < "$probe_err"; rm -f "$probe_err"`,
236
+ ].join('\n');
237
+ const env = { ...process.env, HOOKS_OVERRIDE: HOOKS, TMPDIR: process.env.TMPDIR ?? '/tmp', ...extraEnv };
238
+ // The table speaks of the session's calls. A hook that passes everything on the pipeline would
239
+ // answer a sweep run there otherwise than it answers the session.
240
+ delete env.CI;
241
+ delete env.GITHUB_ACTIONS;
242
+ return new Promise((resolve) => {
243
+ const child = spawn('bash', ['-c', script], { env, stdio: ['ignore', 'pipe', 'ignore'] });
244
+ let stdout = '';
245
+ child.stdout.on('data', (chunk) => (stdout += chunk));
246
+ child.on('close', () => {
247
+ const lines = stdout.split('\n');
248
+ resolve({ decision: lines[0]?.trim() || 'allow', text: lines.slice(1).join(' ') });
249
+ });
250
+ });
251
+ }
252
+
253
+ /** Maps the items through an async function, as many at once as the machine has cores. */
254
+ async function pool(items, fn) {
255
+ const results = new Array(items.length);
256
+ let next = 0;
257
+ const worker = async () => {
258
+ while (next < items.length) {
259
+ const index = next++;
260
+ results[index] = await fn(items[index]);
261
+ }
262
+ };
263
+ await Promise.all(Array.from({ length: Math.max(1, availableParallelism()) }, worker));
264
+ return results;
265
+ }
266
+
267
+ function coverage(table) {
268
+ const files = hookFiles();
269
+ if (!files) return console.log('no hooks directory: nothing to count'), 0;
270
+ const known = new Map(table.hooks.map((h) => [h.file, h]));
271
+ const missing = files.filter((f) => !known.has(f));
272
+ // In the package an entry with no file is a dead entry; in a tree that installs the package it
273
+ // is a hook the tree dropped by its skip list, and that is lawful.
274
+ const source = HOOKS === join(HERE, '../hooks') && !process.env.RT_DECISION_TREE;
275
+ const absent = [...known.keys()].filter((f) => !files.includes(f));
276
+ const stale = source ? absent : [];
277
+ missing.forEach((f) => console.log(`no row or mark: ${f}`));
278
+ stale.forEach((f) => console.log(`an entry with no file: ${f}`));
279
+ if (!source && absent.length > 0) console.log(`skipped by the tree: ${absent.length}`);
280
+ const guards = files.filter((f) => known.get(f)?.kind === 'guard');
281
+ const helpers = files.filter((f) => known.has(f) && known.get(f).kind !== 'guard');
282
+ const deciding = guards.filter((f) => table.rows.some((r) => r.hook === f));
283
+ guards.filter((f) => !deciding.includes(f)).forEach((f) => console.log(`a guard with no row: ${f}`));
284
+ console.log(`deciding ${deciding.length} of ${guards.length}, helpers ${helpers.length}`);
285
+ return missing.length + stale.length + guards.length - deciding.length === 0 ? 0 : 1;
286
+ }
287
+
288
+ async function conform(table) {
289
+ if (!existsSync(HARNESS)) return console.log('no test harness next to the package: nothing to run with'), 0;
290
+ const probed = table.rows.filter((row) => row.probe);
291
+ const results = await pool(probed, runProbe);
292
+ // A refusal row names its move by `says` — a pattern its text must carry; a decision alone
293
+ // would let the hook name any move at all.
294
+ const mismatch = probed.filter((row, index) => {
295
+ const got = results[index];
296
+ // The hook's own text goes with a mismatch: the row id alone does not say which fact the hook read otherwise.
297
+ const said = got.text.trim().slice(0, 300);
298
+ if (got.decision !== row.decision) return console.log(`${rowId(row)}: the hook decided «${got.decision}»: ${said}`), true;
299
+ if (row.says && !new RegExp(row.says).test(got.text)) return console.log(`${rowId(row)}: the refusal does not say /${row.says}/: ${said}`), true;
300
+ if (row.decision === 'deny' && !row.says) return console.log(`${rowId(row)}: a refusal row carries no «says»`), true;
301
+ return false;
302
+ });
303
+ const unprobed = table.rows.filter((row) => !row.probe);
304
+ unprobed.forEach((row) => console.log(`no probe: ${rowId(row)}`));
305
+ // A helper mark claims the file refuses nothing: one subscribed to an agent event is run on a
306
+ // plain command in a bare tree. A sourced helper is not run: some of them run whole check sets
307
+ // when called by hand.
308
+ const plain = { setup: 'T=$(fixture_tree); export CLAUDE_PROJECT_DIR="$T" TMPDIR="$T"', input: `jq -n '{session_id:"p",hook_event_name:"PreToolUse",tool_name:"Bash",tool_input:{command:"true"},cwd:"/"}'` };
309
+ const helpers = table.hooks.filter((h) => h.kind === 'helper' && h.event && existsSync(join(HOOKS, h.file)));
310
+ const helperRuns = await pool(helpers, (h) => runProbe({ hook: h.file, probe: plain }));
311
+ const refusing = helpers.filter((h, index) => helperRuns[index].decision === 'deny');
312
+ refusing.forEach((h) => console.log(`${h.file}: a helper refused a plain command`));
313
+ const total = mismatch.length + unprobed.length + refusing.length;
314
+ console.log(`rows ${probed.length}, mismatch ${total}`);
315
+ return total === 0 ? 0 : 1;
316
+ }
317
+
318
+ /**
319
+ * The key of a row: its area, hook, action and conditions. Unique across the table — two rows with
320
+ * one key would be one decision written twice.
321
+ */
322
+ export const rowKey = (row) =>
323
+ `${row.area}|${row.hook}|${row.action}|${JSON.stringify(Object.fromEntries(Object.entries(row.when ?? {}).sort()))}`;
324
+
325
+ /**
326
+ * The text of a hook as the snapshot keeps it: the paths, the commit hashes and the ports of the
327
+ * fixture differ from run to run, and a refusal that names one would read as changed on every run.
328
+ */
329
+ export const steadyText = (text) =>
330
+ text
331
+ .replace(/\/(?:private\/)?(?:var\/folders|tmp)\/[^\s'"»),;]*/g, '<tmp>')
332
+ .replace(/\b(?=[0-9a-f]*\d)[0-9a-f]{7,40}\b/g, '<sha>')
333
+ // A hash of letters only is told from a word by its place: after «commit» or a dash, before «<».
334
+ .replace(/(\bcommit\s+|—\s)[a-f]{7,40}(?=\s<|\s|$)/g, '$1<sha>')
335
+ .replace(/\b(port|:)\s?\d{4,5}\b/g, '$1<port>')
336
+ // A message of the shell names the line of the hook, and every edit of the hook moves it.
337
+ .replace(/\bline \d+:/g, 'line <n>:')
338
+ .replace(/\s+/g, ' ')
339
+ .trim();
340
+
341
+ /** The answer of every probe: the decision and the steady text, by the key of its row. */
342
+ async function probeAnswers(table) {
343
+ const probed = table.rows.filter((row) => row.probe);
344
+ const results = await pool(probed, (row) => runProbe(row));
345
+ return Object.fromEntries(probed.map((row, index) => [rowKey(row), { decision: results[index].decision, text: steadyText(results[index].text) }]));
346
+ }
347
+
348
+ /**
349
+ * `--snapshot-save <file>`: the answers of all probes before a change of the hooks, to compare with
350
+ * after it. The snapshot judges the hooks apart from the table: a hook that prints its text from the
351
+ * table is compared with what it printed before, not with the table itself.
352
+ */
353
+ async function snapshotSave(table, file) {
354
+ const answers = await probeAnswers(table);
355
+ writeFileSync(file, `${JSON.stringify(answers, null, 1)}\n`);
356
+ console.log(`saved ${Object.keys(answers).length}`);
357
+ return 0;
358
+ }
359
+
360
+ /** `--snapshot-compare <file>`: every probe answers as the snapshot says. */
361
+ async function snapshotCompare(table, file) {
362
+ const before = JSON.parse(readFileSync(file, 'utf8'));
363
+ const now = await probeAnswers(table);
364
+ const changed = Object.keys(before).filter((key) => {
365
+ // The snapshot is read through the same steadying as the run: a rule added to it later applies
366
+ // to a snapshot taken before the rule.
367
+ const was = { ...before[key], text: steadyText(before[key].text ?? '') };
368
+ const is = now[key];
369
+ if (is && is.decision === was.decision && is.text === was.text) return false;
370
+ // The first place the two texts part, so the reader sees what moved without a diff.
371
+ const at = [...(was.text ?? '')].findIndex((ch, i) => ch !== (is?.text ?? '')[i]);
372
+ const near = (text) => (text ?? '').slice(Math.max(0, at - 40), at + 60);
373
+ console.log(`changed: ${key}: ${was.decision} → ${is?.decision ?? 'no row'}`);
374
+ if (is && at >= 0) console.log(` was «${near(was.text)}»\n now «${near(is.text)}»`);
375
+ return true;
376
+ });
377
+ console.log(`rows ${Object.keys(before).length}, changed ${changed.length}`);
378
+ return changed.length === 0 ? 0 : 1;
379
+ }
380
+
381
+ /**
382
+ * `--mutate`: each refusal row is turned to «allow» for one run of its probe, by the key in
383
+ * `RT_DECISION_FLIP`; a hook that takes its decision from the table then allows. The decider honours
384
+ * the key only under the test harness, which marks its runs with `RT_KIT_TEST`.
385
+ */
386
+ async function mutate(table) {
387
+ const rows = table.rows.filter((row) => row.probe && row.decision !== 'allow');
388
+ const results = await pool(rows, (row) => runProbe(row, { PROBE_FLIP: rowKey(row) }));
389
+ const followed = rows.filter((row, index) => results[index].decision === 'allow');
390
+ // The rows a hook did not follow, named by hook: a long list is cut, the count stays whole.
391
+ const missed = rows.filter((row, index) => results[index].decision !== 'allow');
392
+ if (process.argv.includes('--verbose')) missed.forEach((row) => console.log(`not followed: ${rowId(row)} ${JSON.stringify(row.when ?? {})}`));
393
+ console.log(`rows ${rows.length}, followed ${followed.length}`);
394
+ return followed.length === rows.length ? 0 : 1;
395
+ }
396
+
397
+ /**
398
+ * `--reverse`: a refusal is lawful only when its named move goes through in the same state. Each
399
+ * refusal row — stored or computed by the decider — names a move; every hook that has rows for that
400
+ * action is asked in the facts of the refusal. The hooks take their decision from the table, and the
401
+ * sweep `--mutate` proves it row by row, so the hook's answer is the decider's answer: asking the
402
+ * decider is asking the hook without a probe for each pair. A move no hook judges is free.
403
+ */
404
+ function reverse(table) {
405
+ const stored = load();
406
+ const judges = new Map();
407
+ for (const row of stored.rows ?? []) {
408
+ if (!judges.has(row.action)) judges.set(row.action, new Set());
409
+ judges.get(row.action).add(row.hook);
410
+ }
411
+ let moves = 0;
412
+ let refused = 0;
413
+ for (const row of table.rows) {
414
+ if (row.decision !== 'deny' || !row.move || !judges.has(row.move)) continue;
415
+ for (const hook of judges.get(row.move)) {
416
+ moves += 1;
417
+ const answer = decide(stored, { hook, action: row.move, facts: row.when ?? {} });
418
+ if (answer?.decision !== 'deny') continue;
419
+ refused += 1;
420
+ console.log(`refused: ${rowId(row)} names ${row.move}, and ${hook} denies it in ${JSON.stringify(row.when ?? {})}`);
421
+ }
422
+ }
423
+ console.log(`moves ${moves}, refused ${refused}`);
424
+ return refused === 0 ? 0 : 1;
425
+ }
426
+
427
+ function walkMode(table) {
428
+ const result = walk(table);
429
+ for (const { area, state } of result.dead.slice(0, 20)) console.log(`dead: ${area} ${JSON.stringify(state)}`);
430
+ for (const { area, state, row, by } of result.unnamed.slice(0, 20)) {
431
+ console.log(`unnamed: ${area} ${JSON.stringify(state)} ${rowId(row)} names «${row.move}», denied by ${by.map(rowId).join(', ')}`);
432
+ }
433
+ console.log(`states ${result.states.toString()}, ask-only ${result.askOnly}`);
434
+ console.log(`dead ${result.dead.length}, unnamed ${result.unnamed.length}`);
435
+ return result.dead.length + result.unnamed.length === 0 ? 0 : 1;
436
+ }
437
+
438
+ function chainsMode(table) {
439
+ const chains = chainsOf(table);
440
+ const text = existsSync(SCENARIOS) ? readFileSync(SCENARIOS, 'utf8') : '';
441
+ const items = chainItems(text);
442
+ const uncovered = chains.filter((chain) => !chainWritten(items, chain));
443
+ uncovered.forEach((chain) => console.log(`no scenario: ${chain.id}`));
444
+ // `--decided` names in the line that the pairs were counted over the rows the decider computed.
445
+ const decided = process.argv.includes('--decided') ? ' decided' : '';
446
+ console.log(`derived ${chains.length}${decided}, scenarios ${chains.length - uncovered.length}, uncovered ${uncovered.length}`);
447
+ return uncovered.length === 0 ? 0 : 1;
448
+ }
449
+
450
+ const MODES = {
451
+ '--coverage': coverage,
452
+ '--conform': conform,
453
+ '--walk': walkMode,
454
+ '--chains': chainsMode,
455
+ '--mutate': mutate,
456
+ '--reverse': reverse,
457
+ '--snapshot-save': (table) => snapshotSave(table, process.argv[3]),
458
+ '--snapshot-compare': (table) => snapshotCompare(table, process.argv[3]),
459
+ };
460
+
461
+ /**
462
+ * The table as the hooks meet it: the stored rows, and a row the decider computes for every state of
463
+ * `states`. A state is a combination of facts where a refusal is composed with the refusal of a
464
+ * neighbour; it keeps its probe and the pattern its text must carry, and nothing else — the
465
+ * decision, the move and the text come from the rows of the hook and its neighbours.
466
+ */
467
+ export function withDecided(table) {
468
+ const decided = (table.states ?? []).map((state) => {
469
+ const answer = decide(table, { hook: state.hook, action: state.action, facts: state.when ?? {} });
470
+ return { ...state, decision: answer?.decision ?? 'allow', move: answer?.move ?? undefined, text: answer?.text ?? '', decided: true };
471
+ });
472
+ return { ...table, rows: [...(table.rows ?? []), ...decided] };
473
+ }
474
+
475
+ async function main(mode) {
476
+ const stored = load();
477
+ const problems = problemsOf(stored);
478
+ if (mode === '--validate') {
479
+ problems.forEach((p) => console.log(p));
480
+ console.log(`rows ${(stored.rows ?? []).length}, problems ${problems.length}`);
481
+ return problems.length === 0 ? 0 : 1;
482
+ }
483
+ const table = withDecided(stored);
484
+ if (!MODES[mode]) {
485
+ console.error('decision-table: name a mode — --validate, --coverage, --conform, --walk, --chains, --mutate, --reverse, --snapshot-save <file> or --snapshot-compare <file>');
486
+ return 2;
487
+ }
488
+ if (problems.length > 0) {
489
+ console.error(`decision-table: ${problems.length} problems — run --validate`);
490
+ return 2;
491
+ }
492
+ return MODES[mode](table);
493
+ }
494
+
495
+ if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) process.exit(await main(process.argv[2]));
@@ -163,11 +163,12 @@ function rowOf(number, board, pulls) {
163
163
  /**
164
164
  * Why the epic exists, in one sentence — the first phrase of the section the plan opens with.
165
165
  *
166
- * A plan whose first section is worded otherwise loses the phrase, and the paragraph then names
167
- * only the count of the tasks: that is the open question `Q-WC-9` of the subdomain.
166
+ * The draft of the plan opens with «Что даёт»; plans written before it open with «Зачем». A plan
167
+ * whose first section is worded otherwise loses the phrase, and the paragraph then names only the
168
+ * count of the tasks.
168
169
  */
169
170
  function whyOf(plan) {
170
- const section = /^##\s+Зачем\s*$/m.exec(plan);
171
+ const section = /^##\s+(?:Что даёт|Зачем)\s*$/m.exec(plan);
171
172
  if (!section) {
172
173
  return '';
173
174
  }
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * The windows of an epic, built from the observations of the tree: the hook at the end of a turn
4
+ * writes the events of the session records, the reading of merges writes the merges.
5
+ *
6
+ * A window opens at a merge of a task request into the epic branch (`task-merged` with `opens`). It
7
+ * closes at the first of three events after the opening:
8
+ * - the next such merge into the same epic branch;
9
+ * - the merge of the epic request into the main branch (`epic-merged`);
10
+ * - the owner message that opens a turn with an incident analysis, written after the session left
11
+ * the epic branch itself. An analysis written while the session stands on the epic branch is a
12
+ * part of the work on the epic: on 13 November one was written at 01:56 in the middle of it.
13
+ * A window none of them closed by the end of the report day is open up to that end.
14
+ *
15
+ * A window counts the sessions that stood on the epic branch or on the branch of a task of the epic
16
+ * within it, each whole inside the window, and only the moments inside it: a turn that runs past the
17
+ * end is cut at the end.
18
+ */
19
+ import { readdirSync, readFileSync } from 'node:fs';
20
+ import { join } from 'node:path';
21
+
22
+ const endOf = (day) => `${day}T23:59:59.999Z`;
23
+
24
+ /** The tasks of an epic: the epic itself and every task merged into its branch. */
25
+ export function epicTasksOf(observations, epic) {
26
+ return new Set([epic, ...observations.filter((o) => o.ev === 'task-merged' && o.epic === epic).map((o) => o.task)]);
27
+ }
28
+
29
+ /**
30
+ * The moments that close a window by an analysis: per analysis change, the last typed owner word of
31
+ * the same session before it, when the session stood then on a task of the epic but not on the epic
32
+ * branch.
33
+ */
34
+ export function analysisStartsOf(observations, epic, tasks) {
35
+ const words = observations.filter((o) => o.ev === 'record-word' && o.kind === 'typed').sort((a, b) => String(a.t).localeCompare(String(b.t)));
36
+ const out = [];
37
+ for (const change of observations.filter((o) => o.ev === 'record-change' && o.analysis)) {
38
+ const opening = words.filter((w) => w.sid === change.sid && w.t <= change.t).at(-1);
39
+ if (opening && opening.task !== epic && tasks.has(opening.task)) out.push(opening.t);
40
+ }
41
+ return [...new Set(out)].sort();
42
+ }
43
+
44
+ /** The windows of every epic opened by the end of the day, each with its counts. */
45
+ export function epicWindows(observations, day) {
46
+ const end = endOf(day);
47
+ const seen = observations.filter((o) => String(o.t) <= end);
48
+ // A merge into the branch of an epic already merged into the main branch opens nothing: the epic is over.
49
+ const epicMergedAt = new Map(seen.filter((o) => o.ev === 'epic-merged').map((o) => [o.task, o.t]));
50
+ const openings = seen
51
+ .filter((o) => o.ev === 'task-merged' && o.opens && o.epic && !(epicMergedAt.get(o.epic) < o.t))
52
+ .sort((a, b) => String(a.t).localeCompare(String(b.t)));
53
+ const out = [];
54
+ for (const opening of openings) {
55
+ const epic = opening.epic;
56
+ const tasks = epicTasksOf(seen, epic);
57
+ const closers = [
58
+ ...openings.filter((o) => o.epic === epic && o.t > opening.t).map((o) => ({ t: o.t, by: 'task-merged' })),
59
+ ...seen.filter((o) => o.ev === 'epic-merged' && o.task === epic && o.t > opening.t).map((o) => ({ t: o.t, by: 'epic-merged' })),
60
+ ...analysisStartsOf(seen, epic, tasks).filter((t) => t > opening.t).map((t) => ({ t, by: 'analysis' })),
61
+ ].sort((a, b) => String(a.t).localeCompare(String(b.t)));
62
+ const close = closers[0] ?? null;
63
+ const to = close ? close.t : end;
64
+ const inside = seen.filter((o) => o.t >= opening.t && o.t < to);
65
+ const sessions = new Set(inside.filter((o) => String(o.ev).startsWith('record-') && tasks.has(o.task)).map((o) => o.sid));
66
+ const ofWindow = inside.filter((o) => sessions.has(o.sid));
67
+ const refusals = ofWindow.filter((o) => o.ev === 'record-refusal');
68
+ const byKind = {};
69
+ for (const r of refusals) byKind[r.kind] = (byKind[r.kind] ?? 0) + 1;
70
+ out.push({
71
+ epic,
72
+ from: opening.t,
73
+ to: close ? close.t : null,
74
+ closedBy: close ? close.by : 'open',
75
+ calls: ofWindow.filter((o) => o.ev === 'record-call').length,
76
+ refusals: refusals.length,
77
+ byKind,
78
+ reasks: ofWindow.filter((o) => o.ev === 'record-ask' && o.reask).length,
79
+ });
80
+ }
81
+ return out;
82
+ }
83
+
84
+ /** Every observation line under a directory of day files. */
85
+ export function readObservations(dir) {
86
+ const out = [];
87
+ for (const file of readdirSync(dir).filter((f) => f.endsWith('.jsonl'))) {
88
+ for (const line of readFileSync(join(dir, file), 'utf8').split('\n')) {
89
+ if (!line.trim()) continue;
90
+ try {
91
+ out.push(JSON.parse(line));
92
+ } catch {
93
+ // A broken line is no observation.
94
+ }
95
+ }
96
+ }
97
+ return out;
98
+ }