@rt-tools/agent-kit 0.37.1 → 0.39.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.
- package/README.md +5 -5
- package/assets/checks/action-of.mjs +154 -0
- package/assets/checks/board-runs.github.mjs +40 -5
- package/assets/checks/check-board.github.mjs +38 -7
- package/assets/checks/check-state-next.mjs +22 -10
- package/assets/checks/check-turn-map.mjs +26 -6
- package/assets/checks/check-work-steps.mjs +4 -173
- package/assets/checks/decide.mjs +285 -0
- package/assets/checks/decision-table.json +21432 -0
- package/assets/checks/decision-table.mjs +495 -0
- package/assets/checks/epic-window.mjs +98 -0
- package/assets/checks/flow-condition.mjs +64 -0
- package/assets/checks/flow-data.mjs +135 -0
- package/assets/checks/flow-forecast.mjs +334 -0
- package/assets/checks/flow-guard-run.mjs +90 -0
- package/assets/checks/flow-guard.mjs +496 -0
- package/assets/checks/flow-model.mjs +271 -0
- package/assets/checks/flow-override.mjs +130 -0
- package/assets/checks/flow-plan.mjs +206 -0
- package/assets/checks/flow-scaffold.mjs +224 -0
- package/assets/checks/flow-step.mjs +495 -0
- package/assets/checks/flow-sync.mjs +296 -0
- package/assets/checks/flow-syntax.mjs +292 -0
- package/assets/checks/flow-verdict.mjs +227 -0
- package/assets/checks/merge-read.mjs +239 -0
- package/assets/checks/owner-nudges.mjs +389 -0
- package/assets/checks/push-inputs.mjs +59 -16
- package/assets/checks/record-count.mjs +437 -0
- package/assets/checks/record-recount.mjs +187 -0
- package/assets/checks/record-turn.mjs +155 -0
- package/assets/checks/task-flow-script.mjs +318 -0
- package/assets/checks/tree-assignment.mjs +26 -7
- package/assets/checks/work-result.mjs +362 -0
- package/assets/checks/work-steps-lib.mjs +205 -0
- package/assets/commands/night.md +6 -6
- package/assets/defaults/gate-map.sh +39 -12
- package/assets/defaults/merges.sh +48 -0
- package/assets/defaults/project.sh +6 -5
- package/assets/defaults/turn-map.md +23 -22
- package/assets/docs/GLOSSARY.md +5 -1
- package/assets/hooks/ask-judge.md +67 -0
- package/assets/hooks/browser-guard-device-id.sh +11 -9
- package/assets/hooks/browser-guard-no-asking.sh +12 -6
- package/assets/hooks/browser-guard-no-listing.sh +13 -7
- package/assets/hooks/browser-guard-no-other-drivers.sh +25 -17
- package/assets/hooks/browser-guard-require-select.sh +15 -15
- package/assets/hooks/commit-msg.sh +13 -3
- package/assets/hooks/conscience-guard.sh +9 -25
- package/assets/hooks/decide.sh +141 -0
- package/assets/hooks/deny-tail.sh +16 -11
- package/assets/hooks/dev-server-guard.sh +27 -20
- package/assets/hooks/dispatch.sh +47 -4
- package/assets/hooks/docs-guard.sh +31 -200
- package/assets/hooks/end-conversation-guard.sh +12 -21
- package/assets/hooks/epic-stop-guard.sh +54 -152
- package/assets/hooks/exam-guard.sh +32 -65
- package/assets/hooks/facts-docs.sh +221 -0
- package/assets/hooks/facts-flow.sh +173 -0
- package/assets/hooks/facts.sh +374 -0
- package/assets/hooks/flow-step-guard.sh +93 -0
- package/assets/hooks/git-guard-delivery-body.sh +4 -4
- package/assets/hooks/git-guard-delivery-conflict.sh +16 -10
- package/assets/hooks/git-guard-delivery-draft.sh +3 -3
- package/assets/hooks/git-guard-delivery-epic.sh +18 -9
- package/assets/hooks/git-guard-delivery-folder.sh +32 -7
- package/assets/hooks/git-guard-delivery-merge.sh +2 -2
- package/assets/hooks/git-guard-delivery-merged.sh +12 -1
- package/assets/hooks/git-guard-delivery-pipeline.sh +1 -1
- package/assets/hooks/git-guard-delivery-rerun.sh +44 -0
- package/assets/hooks/git-guard-delivery-signature.sh +82 -45
- package/assets/hooks/git-guard-delivery-task.sh +47 -0
- package/assets/hooks/git-guard-delivery.sh +112 -101
- package/assets/hooks/git-guard-discard.sh +8 -18
- package/assets/hooks/git-guard-main.sh +13 -19
- package/assets/hooks/git-guard-no-verify.sh +56 -16
- package/assets/hooks/git-guard-push-ahead.sh +39 -6
- package/assets/hooks/git-guard-push-marks.sh +262 -13
- package/assets/hooks/git-guard-push-tests.sh +58 -52
- package/assets/hooks/git-guard-tree-assignment.sh +7 -4
- package/assets/hooks/guard-note.sh +8 -6
- package/assets/hooks/handoff-entry-guard.sh +19 -38
- package/assets/hooks/handoff-write.sh +3 -1
- package/assets/hooks/hook-input.sh +0 -41
- package/assets/hooks/metric-marks.sh +19 -7
- package/assets/hooks/observe.sh +1 -1
- package/assets/hooks/override-write-guard.sh +27 -9
- package/assets/hooks/pre-push.sh +40 -12
- package/assets/hooks/prose-style-guard.sh +6 -19
- package/assets/hooks/pull-merges.sh +76 -0
- package/assets/hooks/push-ahead-call-guard.sh +447 -0
- package/assets/hooks/push-set.sh +38 -8
- package/assets/hooks/qa-dataid-guard.sh +7 -17
- package/assets/hooks/record-turn.sh +52 -0
- package/assets/hooks/rerun-guard.sh +11 -16
- package/assets/hooks/reuse-first-guard.sh +7 -21
- package/assets/hooks/role-calls.sh +25 -3
- package/assets/hooks/rule-source-guard.sh +54 -21
- package/assets/hooks/skill-gate-rearm.sh +18 -5
- package/assets/hooks/skill-gate.sh +156 -20
- package/assets/hooks/skill-loaded.sh +18 -3
- package/assets/hooks/sql-guard-target.sh +2 -2
- package/assets/hooks/sql-guard-write.sh +12 -12
- package/assets/hooks/sql-guard.sh +58 -16
- package/assets/hooks/task-context-load.sh +3 -1
- package/assets/hooks/task-flow-context.sh +5 -18
- package/assets/hooks/task-flow-draft-guard.sh +25 -74
- package/assets/hooks/task-flow-guard.sh +28 -130
- package/assets/hooks/tree-assignment-guard.sh +25 -14
- package/assets/hooks/turn-entry-load.sh +3 -1
- package/assets/hooks/turn-exit-guard.sh +92 -452
- package/assets/hooks/turn-exit-role.sh +3 -1
- package/assets/hooks/turn-judge.md +113 -0
- package/assets/hooks/window-fill-guard.sh +22 -41
- package/assets/patterns/doc-style-human.md +36 -76
- package/assets/patterns/git-workflow-commit.azure.md +9 -155
- package/assets/patterns/git-workflow-commit.github.md +11 -269
- package/assets/patterns/git-workflow-commit.gitlab.md +9 -161
- package/assets/patterns/git-workflow-docker.md +8 -231
- package/assets/patterns/git-workflow-freshness.md +8 -56
- package/assets/patterns/git-workflow-merge.md +6 -176
- package/assets/patterns/git-workflow-migration.md +5 -75
- package/assets/patterns/git-workflow-pr-body.github.md +6 -159
- package/assets/patterns/git-workflow-pr-ready.md +9 -70
- package/assets/patterns/git-workflow-pr.azure.md +13 -285
- package/assets/patterns/git-workflow-pr.github.md +13 -251
- package/assets/patterns/git-workflow-pr.gitlab.md +13 -286
- package/assets/patterns/git-workflow-restart.md +5 -42
- package/assets/patterns/git-workflow-secrets.md +10 -76
- package/assets/patterns/git-workflow-stack.md +7 -207
- package/assets/patterns/status-report-table.github.md +1 -1
- package/assets/patterns/task-flow-archive.md +12 -233
- package/assets/patterns/task-flow-close.md +16 -292
- package/assets/patterns/task-flow-cycle.md +13 -39
- package/assets/patterns/task-flow-graph.needs-graph.md +8 -62
- package/assets/patterns/task-flow-handoff.md +13 -161
- package/assets/patterns/task-flow-resume.md +18 -218
- package/assets/patterns/task-flow-start.md +9 -247
- package/assets/patterns/turn-entry-map.md +2 -2
- package/assets/pitfalls/agent-kit-extend.md +200 -0
- package/assets/pitfalls/agent-kit.2.md +323 -0
- package/assets/pitfalls/angular-patterns.md +98 -0
- package/assets/pitfalls/browser-verification.md +265 -0
- package/assets/pitfalls/component-structure.md +98 -0
- package/assets/pitfalls/dependencies.md +92 -0
- package/assets/pitfalls/deploy-flow.azure.md +171 -0
- package/assets/pitfalls/deploy-flow.github.md +211 -0
- package/assets/pitfalls/deploy-flow.gitlab.md +173 -0
- package/assets/pitfalls/doc-style-human.md +112 -0
- package/assets/pitfalls/doc-style.2.md +286 -0
- package/assets/pitfalls/entity-conventions.needs-admin.md +96 -0
- package/assets/pitfalls/entity-models.md +89 -0
- package/assets/pitfalls/epic-flow.md +92 -0
- package/assets/pitfalls/git-workflow-commit.azure.md +190 -0
- package/assets/pitfalls/git-workflow-commit.github.md +307 -0
- package/assets/pitfalls/git-workflow-commit.gitlab.md +196 -0
- package/assets/pitfalls/git-workflow-docker.md +275 -0
- package/assets/pitfalls/git-workflow-freshness.md +88 -0
- package/assets/pitfalls/git-workflow-hosting.azure.md +31 -0
- package/assets/pitfalls/git-workflow-hosting.github.md +32 -0
- package/assets/pitfalls/git-workflow-hosting.gitlab.md +31 -0
- package/assets/pitfalls/git-workflow-merge.md +220 -0
- package/assets/pitfalls/git-workflow-migration.md +103 -0
- package/assets/pitfalls/git-workflow-pr-body.github.md +185 -0
- package/assets/pitfalls/git-workflow-pr-ready.md +104 -0
- package/assets/pitfalls/git-workflow-pr.2.azure.md +172 -0
- package/assets/pitfalls/git-workflow-pr.2.github.md +184 -0
- package/assets/pitfalls/git-workflow-pr.2.gitlab.md +180 -0
- package/assets/pitfalls/git-workflow-pr.azure.md +159 -0
- package/assets/pitfalls/git-workflow-pr.github.md +107 -0
- package/assets/pitfalls/git-workflow-pr.gitlab.md +151 -0
- package/assets/pitfalls/git-workflow-push.azure.md +30 -0
- package/assets/pitfalls/git-workflow-push.github.md +30 -0
- package/assets/pitfalls/git-workflow-push.gitlab.md +30 -0
- package/assets/pitfalls/git-workflow-queue.azure.md +30 -0
- package/assets/pitfalls/git-workflow-queue.github.md +30 -0
- package/assets/pitfalls/git-workflow-queue.gitlab.md +30 -0
- package/assets/pitfalls/git-workflow-restart.md +72 -0
- package/assets/pitfalls/git-workflow-secrets.md +114 -0
- package/assets/pitfalls/git-workflow-stack.md +254 -0
- package/assets/pitfalls/git-workflow.2.azure.md +291 -0
- package/assets/pitfalls/git-workflow.2.github.md +294 -0
- package/assets/pitfalls/git-workflow.2.gitlab.md +278 -0
- package/assets/pitfalls/lib-layers.md +114 -0
- package/assets/pitfalls/lists.md +117 -0
- package/assets/pitfalls/mockups.needs-mockups.md +87 -0
- package/assets/pitfalls/navigation.md +94 -0
- package/assets/pitfalls/observability.needs-app.md +186 -0
- package/assets/pitfalls/platform-access.md +94 -0
- package/assets/pitfalls/queue-audit.github.md +78 -0
- package/assets/pitfalls/reuse-first.md +201 -0
- package/assets/pitfalls/spec-driven.2.md +318 -0
- package/assets/pitfalls/styling-bem.2.md +125 -0
- package/assets/pitfalls/task-flow-archive.md +265 -0
- package/assets/pitfalls/task-flow-close.2.md +166 -0
- package/assets/pitfalls/task-flow-close.md +167 -0
- package/assets/pitfalls/task-flow-cycle.md +73 -0
- package/assets/pitfalls/task-flow-graph.needs-graph.md +98 -0
- package/assets/pitfalls/task-flow-handoff.md +200 -0
- package/assets/pitfalls/task-flow-handover.md +30 -0
- package/assets/pitfalls/task-flow-owner.md +58 -0
- package/assets/pitfalls/task-flow-plan.md +29 -0
- package/assets/pitfalls/task-flow-resume.md +257 -0
- package/assets/pitfalls/task-flow-start.md +287 -0
- package/assets/pitfalls/task-flow.2.md +303 -0
- package/assets/pitfalls/task-flow.md +4 -4
- package/assets/pitfalls/testing.2.md +279 -0
- package/assets/pitfalls/turn-conduct.md +16 -17
- package/assets/pitfalls/typescript-conventions.md +161 -0
- package/assets/pitfalls/write-a-skill.md +128 -0
- package/assets/rules/angular-patterns.md +21 -86
- package/assets/rules/autonomous-work.md +2 -2
- package/assets/rules/browser-verification.md +28 -256
- package/assets/rules/component-structure.md +25 -87
- package/assets/rules/dependencies.md +20 -78
- package/assets/rules/deploy-flow.azure.md +26 -156
- package/assets/rules/deploy-flow.github.md +34 -200
- package/assets/rules/deploy-flow.gitlab.md +27 -159
- package/assets/rules/doc-style.md +32 -286
- package/assets/rules/entity-conventions.needs-admin.md +26 -82
- package/assets/rules/entity-models.md +14 -63
- package/assets/rules/epic-flow.md +24 -63
- package/assets/rules/git-workflow-hosting.azure.md +26 -0
- package/assets/rules/git-workflow-hosting.github.md +38 -0
- package/assets/rules/git-workflow-hosting.gitlab.md +27 -0
- package/assets/rules/git-workflow-push.azure.md +28 -0
- package/assets/rules/git-workflow-push.github.md +28 -0
- package/assets/rules/git-workflow-push.gitlab.md +28 -0
- package/assets/rules/git-workflow-queue.azure.md +23 -0
- package/assets/rules/git-workflow-queue.github.md +30 -0
- package/assets/rules/git-workflow-queue.gitlab.md +23 -0
- package/assets/rules/git-workflow.azure.md +21 -282
- package/assets/rules/git-workflow.github.md +24 -284
- package/assets/rules/git-workflow.gitlab.md +21 -269
- package/assets/rules/lib-layers.md +30 -103
- package/assets/rules/lists.md +21 -105
- package/assets/rules/mockups.needs-mockups.md +25 -64
- package/assets/rules/navigation.md +10 -69
- package/assets/rules/observability.needs-app.md +43 -172
- package/assets/rules/platform-access.md +28 -75
- package/assets/rules/queue-audit.github.md +25 -52
- package/assets/rules/reuse-first.md +20 -195
- package/assets/rules/spec-driven.md +35 -308
- package/assets/rules/styling-bem.md +20 -117
- package/assets/rules/task-flow-handover.md +24 -0
- package/assets/rules/task-flow-owner.md +34 -0
- package/assets/rules/task-flow-plan.md +38 -0
- package/assets/rules/task-flow.md +22 -300
- package/assets/rules/testing.md +32 -273
- package/assets/rules/turn-conduct.md +105 -106
- package/assets/rules/turn-entry.md +2 -2
- package/assets/rules/typescript-conventions.md +41 -147
- package/assets/samples/tasks/_template/plan.md +30 -6
- package/assets/samples/tasks/_template/progress.md +0 -1
- package/assets/skills/agent-kit-extend.md +25 -168
- package/assets/skills/agent-kit.md +27 -299
- package/assets/skills/write-a-skill.md +20 -106
- package/assets/task-flow/flow.js +184 -0
- package/assets/task-flow/flow.md +101 -0
- package/assets/task-flow/measure/agreement.mjs +24 -0
- package/assets/task-flow/measure/ci.mjs +184 -0
- package/assets/task-flow/measure/copy.mjs +22 -0
- package/assets/task-flow/measure/epic-of.mjs +50 -0
- package/assets/task-flow/measure/epic-text.mjs +35 -0
- package/assets/task-flow/measure/epic.mjs +114 -0
- package/assets/task-flow/measure/folder.mjs +16 -0
- package/assets/task-flow/measure/footprint.mjs +28 -0
- package/assets/task-flow/measure/grill.mjs +21 -0
- package/assets/task-flow/measure/host.mjs +8 -0
- package/assets/task-flow/measure/owner.mjs +27 -0
- package/assets/task-flow/measure/plan.mjs +27 -0
- package/assets/task-flow/measure/pr.mjs +20 -0
- package/assets/task-flow/measure/progress.mjs +50 -0
- package/assets/task-flow/measure/session.mjs +252 -0
- package/assets/task-flow/measure/task.mjs +23 -0
- package/assets/task-flow/measure/text.mjs +77 -0
- package/assets/task-flow/source/git.mjs +254 -0
- package/assets/task-flow/source/host.mjs +277 -0
- package/assets/task-flow/source/marks.mjs +56 -0
- package/assets/task-flow/source/owner.mjs +94 -0
- package/assets/task-flow/source/records.mjs +116 -0
- package/assets/task-flow/source/session.mjs +269 -0
- package/assets/task-flow/source/steps.mjs +28 -0
- package/assets/task-flow/source/tree.mjs +42 -0
- package/bin/agent-kit.d.ts.map +1 -1
- package/bin/agent-kit.js +30 -0
- package/bin/agent-kit.js.map +1 -1
- package/lib/agent-checks.d.ts +55 -0
- package/lib/agent-checks.d.ts.map +1 -0
- package/lib/agent-checks.js +221 -0
- package/lib/agent-checks.js.map +1 -0
- package/lib/assets.d.ts.map +1 -1
- package/lib/assets.js +7 -2
- package/lib/assets.js.map +1 -1
- package/lib/cargo.d.ts +29 -0
- package/lib/cargo.d.ts.map +1 -1
- package/lib/cargo.js +27 -1
- package/lib/cargo.js.map +1 -1
- package/lib/cascade.d.ts.map +1 -1
- package/lib/cascade.js +3 -1
- package/lib/cascade.js.map +1 -1
- package/lib/commands.d.ts.map +1 -1
- package/lib/commands.js +78 -2
- package/lib/commands.js.map +1 -1
- package/lib/config.d.ts +18 -0
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +18 -1
- package/lib/config.js.map +1 -1
- package/lib/epic-window-send.d.ts +74 -0
- package/lib/epic-window-send.d.ts.map +1 -0
- package/lib/epic-window-send.js +166 -0
- package/lib/epic-window-send.js.map +1 -0
- package/lib/hooks-map.d.ts +4 -0
- package/lib/hooks-map.d.ts.map +1 -1
- package/lib/hooks-map.js +2 -2
- package/lib/hooks-map.js.map +1 -1
- package/lib/integrity.d.ts +3 -0
- package/lib/integrity.d.ts.map +1 -1
- package/lib/integrity.js +7 -0
- package/lib/integrity.js.map +1 -1
- package/lib/metric-lines.d.ts +2 -1
- package/lib/metric-lines.d.ts.map +1 -1
- package/lib/metric-lines.js +49 -1
- package/lib/metric-lines.js.map +1 -1
- package/lib/metric-send-mark.d.ts +6 -3
- package/lib/metric-send-mark.d.ts.map +1 -1
- package/lib/metric-send-mark.js +15 -5
- package/lib/metric-send-mark.js.map +1 -1
- package/lib/metric-send.d.ts +6 -2
- package/lib/metric-send.d.ts.map +1 -1
- package/lib/metric-send.js +47 -13
- package/lib/metric-send.js.map +1 -1
- package/lib/metrics.d.ts +17 -0
- package/lib/metrics.d.ts.map +1 -1
- package/lib/metrics.js +26 -15
- package/lib/metrics.js.map +1 -1
- package/lib/observations.js +3 -3
- package/lib/observations.js.map +1 -1
- package/lib/outcomes.d.ts +5 -2
- package/lib/outcomes.d.ts.map +1 -1
- package/lib/outcomes.js +5 -4
- package/lib/outcomes.js.map +1 -1
- package/lib/push-gate.d.ts +13 -0
- package/lib/push-gate.d.ts.map +1 -1
- package/lib/push-gate.js +11 -7
- package/lib/push-gate.js.map +1 -1
- package/lib/push-set-usefulness.d.ts +78 -0
- package/lib/push-set-usefulness.d.ts.map +1 -0
- package/lib/push-set-usefulness.js +225 -0
- package/lib/push-set-usefulness.js.map +1 -0
- package/lib/sync.d.ts +10 -0
- package/lib/sync.d.ts.map +1 -1
- package/lib/sync.js +14 -1
- package/lib/sync.js.map +1 -1
- package/lib/work-result.d.ts +43 -0
- package/lib/work-result.d.ts.map +1 -0
- package/lib/work-result.js +39 -0
- package/lib/work-result.js.map +1 -0
- package/package.json +1 -1
- package/assets/hooks/claim-guard.sh +0 -212
- package/assets/hooks/grill-gate-ask.sh +0 -26
- package/assets/hooks/grill-gate-bypass.sh +0 -61
- package/assets/hooks/grill-gate-options.sh +0 -50
- package/assets/hooks/grill-gate.sh +0 -482
- package/assets/hooks/postmortem-guard.sh +0 -124
- package/assets/hooks/proposal-guard.sh +0 -154
- package/assets/hooks/stand-login-guard.sh +0 -107
- package/assets/hooks/turn-exit-epic.sh +0 -173
- package/assets/hooks/turn-exit-patterns.sh +0 -123
- package/assets/hooks/turn-exit-verdict.sh +0 -149
- package/assets/hooks/waiting-turn-guard.sh +0 -320
- 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]));
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Conditions of the task flow description: an exit of a state and a release case built from ready
|
|
4
|
+
* measurers without a line of code.
|
|
5
|
+
*
|
|
6
|
+
* A condition is a reference to a measurer capability — `measure.plan.approved` — or an object of
|
|
7
|
+
* exactly one key over other conditions:
|
|
8
|
+
*
|
|
9
|
+
* { all: [c1, c2, …] } — met when every part is met;
|
|
10
|
+
* { any: [c1, c2, …] } — met when one part is met;
|
|
11
|
+
* { not: c } — met when its part is not.
|
|
12
|
+
*
|
|
13
|
+
* A new fact is a new measurer; a new combination of facts is a line of the description. The
|
|
14
|
+
* condition is checked when the model is built — a reference to a measurer the description does not
|
|
15
|
+
* hold is refused there — and evaluated by the engine with the results of the measurers.
|
|
16
|
+
*
|
|
17
|
+
* The module has no command line of its own: the exit of a state is printed by
|
|
18
|
+
* `node flow-model.mjs <flow.js> --exit <state> <results.json>`.
|
|
19
|
+
*/
|
|
20
|
+
import { Call, Ref } from './flow-syntax.mjs';
|
|
21
|
+
|
|
22
|
+
/** The keys of a condition object. */
|
|
23
|
+
export const OPERATORS = ['all', 'any', 'not'];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Checks a condition. `measures` maps `measure.<entry>` to the set of its capabilities. Returns
|
|
27
|
+
* nothing; throws with `where` and the reason.
|
|
28
|
+
*/
|
|
29
|
+
export function validateCondition(value, measures, where, fail) {
|
|
30
|
+
if (value instanceof Ref) {
|
|
31
|
+
const [section, entry, cap, ...rest] = value.path;
|
|
32
|
+
const name = value.path.join('.');
|
|
33
|
+
if (section !== 'measure' || cap === undefined || rest.length > 0) {
|
|
34
|
+
fail(`${where}: «${name}» is not a measurer capability «measure.<entry>.<capability>»`);
|
|
35
|
+
}
|
|
36
|
+
const caps = measures.get(`measure.${entry}`);
|
|
37
|
+
if (!caps || !caps.has(cap)) fail(`${where}: «${name}» names no measurer of the description`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value) || value instanceof Call) {
|
|
41
|
+
fail(`${where}: a condition is a measurer reference or one of ${OPERATORS.join(', ')}`);
|
|
42
|
+
}
|
|
43
|
+
const keys = Object.keys(value);
|
|
44
|
+
if (keys.length !== 1 || !OPERATORS.includes(keys[0])) {
|
|
45
|
+
fail(`${where}: a condition object has exactly one key of ${OPERATORS.join(', ')}, it has «${keys.join('», «')}»`);
|
|
46
|
+
}
|
|
47
|
+
const [op] = keys;
|
|
48
|
+
const inner = value[op];
|
|
49
|
+
if (op === 'not') {
|
|
50
|
+
validateCondition(inner, measures, `${where}.not`, fail);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (!Array.isArray(inner) || inner.length === 0) fail(`${where}.${op}: a list of at least one condition`);
|
|
54
|
+
inner.forEach((part, index) => validateCondition(part, measures, `${where}.${op}[${index}]`, fail));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Evaluates a checked condition; `measured(name)` answers a measurer result by its reference. */
|
|
58
|
+
export function evaluateCondition(value, measured) {
|
|
59
|
+
if (value instanceof Ref) return measured(value.path.join('.')) === true;
|
|
60
|
+
if ('all' in value) return value.all.every((part) => evaluateCondition(part, measured));
|
|
61
|
+
if ('any' in value) return value.any.some((part) => evaluateCondition(part, measured));
|
|
62
|
+
return !evaluateCondition(value.not, measured);
|
|
63
|
+
}
|
|
64
|
+
|