@rt-tools/agent-kit 0.37.1 → 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.
- 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 +21365 -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 +136 -0
- package/assets/checks/flow-forecast.mjs +330 -0
- package/assets/checks/flow-guard-run.mjs +90 -0
- package/assets/checks/flow-guard.mjs +294 -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 +365 -0
- package/assets/checks/flow-sync.mjs +296 -0
- package/assets/checks/flow-syntax.mjs +292 -0
- package/assets/checks/flow-verdict.mjs +112 -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 +410 -0
- package/assets/checks/record-recount.mjs +187 -0
- package/assets/checks/record-turn.mjs +155 -0
- package/assets/checks/task-flow-script.mjs +292 -0
- package/assets/checks/tree-assignment.mjs +26 -7
- package/assets/checks/work-result.mjs +281 -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 +21 -22
- package/assets/docs/GLOSSARY.md +5 -1
- package/assets/hooks/ask-judge.md +65 -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 +308 -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 +32 -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 +15 -4
- package/assets/hooks/git-guard-push-marks.sh +53 -1
- package/assets/hooks/git-guard-push-tests.sh +30 -51
- 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 +14 -10
- package/assets/hooks/prose-style-guard.sh +6 -19
- package/assets/hooks/pull-merges.sh +76 -0
- package/assets/hooks/push-set.sh +11 -4
- 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 +103 -0
- package/assets/hooks/window-fill-guard.sh +22 -41
- 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.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/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/navigation.md +94 -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 +264 -0
- package/assets/pitfalls/task-flow-close.2.md +165 -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 +29 -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/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/navigation.md +10 -69
- 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 +100 -104
- 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 +188 -0
- package/assets/task-flow/flow.md +102 -0
- package/assets/task-flow/measure/agreement.mjs +24 -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 +20 -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 +175 -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 +86 -0
- package/assets/task-flow/source/host.mjs +172 -0
- package/assets/task-flow/source/marks.mjs +56 -0
- package/assets/task-flow/source/owner.mjs +74 -0
- package/assets/task-flow/source/records.mjs +116 -0
- package/assets/task-flow/source/session.mjs +232 -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 +209 -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,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The guard of the course of work — the engine command `guard`, called by the hook
|
|
3
|
+
* `flow-step-guard.sh` before Edit, Write, MultiEdit and a shell command.
|
|
4
|
+
*
|
|
5
|
+
* It refuses what would move the scenario by a declaration instead of a measurement:
|
|
6
|
+
*
|
|
7
|
+
* - a step marked done in the progress without the green mark the engine wrote for it;
|
|
8
|
+
* - done and current marks out of order: done steps first, the current one right after them;
|
|
9
|
+
* - the state line of the progress ahead of the state the engine measured;
|
|
10
|
+
* - a shell write into the progress, the plan, the epic plan or the directory of the marks, and
|
|
11
|
+
* an edit of that directory by any tool other than the engine;
|
|
12
|
+
* - «отложено» set or lifted in the epic plan without the owner's last word naming the task;
|
|
13
|
+
* - an edit of the tree override of the description without the owner's last word naming the key;
|
|
14
|
+
* - an edit of the description, its measurers and sources or the engine outside a task whose
|
|
15
|
+
* approved plan names the path in a footprint row marked «(правится)».
|
|
16
|
+
*
|
|
17
|
+
* One edit it names as the owner's decision, and the decision table answers it with the window: a
|
|
18
|
+
* replacement line of the progress added or changed. The engine reads no quote after that: the
|
|
19
|
+
* allowed edit is the owner's word. «шаг владельца» set on a row of the epic plan is refused outright.
|
|
20
|
+
*
|
|
21
|
+
* A plan without a single check line, and the plan of a branch in `words.legacyBranches`, keep the
|
|
22
|
+
* done marks of the progress as they are: their steps were never measured by the engine.
|
|
23
|
+
*
|
|
24
|
+
* The decisions are pure: the context brings the files, the marks, the owner's last word and the
|
|
25
|
+
* measured state. A shell command is judged by its text, so a script that computes the path itself
|
|
26
|
+
* is not seen.
|
|
27
|
+
*/
|
|
28
|
+
import { stepsOfPlan, stepsOfProgress } from './work-steps-lib.mjs';
|
|
29
|
+
|
|
30
|
+
/** The word that defers a task in the epic plan. */
|
|
31
|
+
const DEFERRED = 'отложено';
|
|
32
|
+
|
|
33
|
+
/** The words an epic plan row carried for a task handed to the owner: a row is no longer marked so. */
|
|
34
|
+
const OWNER_STEP = 'шаг владельца';
|
|
35
|
+
|
|
36
|
+
/** The opening of a replacement line of the progress, with the number of the step. */
|
|
37
|
+
const REPLACEMENT_START = /^-\s+\*\*(?:Проверка шага|Check of step)\s+(\d+\.\d+)\s+(?:заменена|replaced):\*\*/u;
|
|
38
|
+
|
|
39
|
+
/** The mark of a footprint row the task edits. */
|
|
40
|
+
const EDITED_ROW = '(правится)';
|
|
41
|
+
|
|
42
|
+
/** The state line of the progress. */
|
|
43
|
+
const STATE_LINE = /^-\s+\*\*(?:State|Состояние):\*\*\s+`([^`]+)`/mu;
|
|
44
|
+
|
|
45
|
+
/** A shell command that writes: a redirect, a tee, an in-place edit, a copy, a move, a removal or an inline script. */
|
|
46
|
+
const WRITE_SIGNS = [
|
|
47
|
+
(path) => new RegExp(`>{1,2}\\s*["']?[^\\s;|&"']*${path}`),
|
|
48
|
+
(path) => new RegExp(`\\btee\\b[^;|&]*${path}`),
|
|
49
|
+
(path) => new RegExp(`\\b(?:sed|perl|awk|gawk)\\b[^;|&]*\\s-i[^;|&]*${path}`),
|
|
50
|
+
(path) => new RegExp(`\\b(?:cp|mv|rm|install|rsync|truncate|touch|dd|ln)\\b[^;|&]*${path}`),
|
|
51
|
+
(path) => new RegExp(`\\bgit\\s+(?:checkout|restore)\\b[^;|&]*${path}`),
|
|
52
|
+
(path) => new RegExp(`\\b(?:python3?|node|ruby|perl)\\b[^;|&]*(?:\\s-[ce]\\b|\\s--eval\\b|\\s-\\s*<<)[\\s\\S]*${path}`),
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
const escape = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
56
|
+
|
|
57
|
+
/** The shell command writes into a path matching the pattern (a regular expression source). */
|
|
58
|
+
export function shellWrites(command, pathSource) {
|
|
59
|
+
return WRITE_SIGNS.some((sign) => sign(pathSource).test(command));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The text an edit leaves: Write gives it whole, Edit and MultiEdit replace in the text before. */
|
|
63
|
+
export function textAfter(call, before) {
|
|
64
|
+
if (call.tool === 'Write') return call.content ?? '';
|
|
65
|
+
let text = before ?? '';
|
|
66
|
+
for (const edit of call.edits ?? []) {
|
|
67
|
+
if (edit.replaceAll) text = text.split(edit.oldString).join(edit.newString);
|
|
68
|
+
else text = text.replace(edit.oldString, () => edit.newString);
|
|
69
|
+
}
|
|
70
|
+
return text;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The done and current marks of the progress are in order: done steps first, the current right after. */
|
|
74
|
+
export function marksOutOfOrder(progress) {
|
|
75
|
+
const { steps } = stepsOfProgress(progress);
|
|
76
|
+
let open = false;
|
|
77
|
+
for (const step of steps) {
|
|
78
|
+
if (step.mark === 'x' && open) return `step ${step.number} is marked done after a step that is not done`;
|
|
79
|
+
if (step.mark === '>' && open) return `step ${step.number} is marked current after a step that is not done`;
|
|
80
|
+
if (step.mark !== 'x') open = true;
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The steps the edit marks done that were not done before. */
|
|
86
|
+
export function newlyDone(before, after) {
|
|
87
|
+
const was = new Set(stepsOfProgress(before ?? '').steps.filter((s) => s.mark === 'x').map((s) => s.number));
|
|
88
|
+
return stepsOfProgress(after).steps.filter((s) => s.mark === 'x' && !was.has(s.number)).map((s) => s.number);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The state the progress declares, or null. */
|
|
92
|
+
export const declaredState = (text) => text?.match(STATE_LINE)?.[1] ?? null;
|
|
93
|
+
|
|
94
|
+
/** The task numbers of the epic plan lines whose «отложено» the edit sets or lifts. */
|
|
95
|
+
export function deferChanges(before, after, key) {
|
|
96
|
+
const deferredTasks = (text) => {
|
|
97
|
+
const tasks = new Set();
|
|
98
|
+
for (const line of (text ?? '').split('\n')) {
|
|
99
|
+
if (!line.includes(DEFERRED)) continue;
|
|
100
|
+
for (const match of line.matchAll(new RegExp(`${escape(key)}-(\\d+)`, 'g'))) tasks.add(match[1]);
|
|
101
|
+
}
|
|
102
|
+
return tasks;
|
|
103
|
+
};
|
|
104
|
+
const was = deferredTasks(before);
|
|
105
|
+
const now = deferredTasks(after);
|
|
106
|
+
return [...new Set([...was, ...now])].filter((task) => was.has(task) !== now.has(task));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The lines of the text after the edit that match the sign and did not stand in the text before. */
|
|
110
|
+
const newLines = (before, after, sign) => {
|
|
111
|
+
const was = new Set((before ?? '').split('\n').map((line) => line.trim()));
|
|
112
|
+
return (after ?? '').split('\n').filter((line) => sign(line) && !was.has(line.trim()));
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The step numbers whose replacement line the edit adds or changes. A removed line is not counted:
|
|
117
|
+
* dropping a replacement gives the check of the plan back and needs no word of the owner.
|
|
118
|
+
*/
|
|
119
|
+
export function replacementChanges(before, after) {
|
|
120
|
+
return [...new Set(newLines(before, after, (line) => REPLACEMENT_START.test(line)).map((line) => line.match(REPLACEMENT_START)[1]))];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The task numbers of the epic plan rows the edit marks «шаг владельца». The mark is refused: the
|
|
125
|
+
* step is the executor's. A row that leaves the mark is not counted: the task goes back to the work.
|
|
126
|
+
*/
|
|
127
|
+
export function ownerRowChanges(before, after, key) {
|
|
128
|
+
const marked = (text) => new RegExp(`\\|\\s*${OWNER_STEP}[^|]*\\|?\\s*$`, 'u').test(text);
|
|
129
|
+
const numbers = (line) => [...line.matchAll(new RegExp(`${escape(key)}-(\\d+)`, 'g'))].map((match) => match[1]);
|
|
130
|
+
// A row that carried the mark before the edit is a legacy row: its other cells may be edited.
|
|
131
|
+
const legacy = new Set((before ?? '').split('\n').filter(marked).flatMap(numbers));
|
|
132
|
+
const tasks = new Set();
|
|
133
|
+
for (const line of newLines(before, after, marked)) {
|
|
134
|
+
for (const task of numbers(line)) if (!legacy.has(task)) tasks.add(task);
|
|
135
|
+
}
|
|
136
|
+
return [...tasks];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The keys an edit of the override changes: `key:` of every line that differs. */
|
|
140
|
+
export function changedKeys(before, after) {
|
|
141
|
+
const lines = (text) => new Set((text ?? '').split('\n').map((line) => line.trim()).filter(Boolean));
|
|
142
|
+
const was = lines(before);
|
|
143
|
+
const now = lines(after);
|
|
144
|
+
const changed = [...[...now].filter((line) => !was.has(line)), ...[...was].filter((line) => !now.has(line))];
|
|
145
|
+
const keys = new Set();
|
|
146
|
+
for (const line of changed) {
|
|
147
|
+
const key = line.match(/^['"]?([A-Za-z_][\w-]*)['"]?\s*:/)?.[1];
|
|
148
|
+
if (key && key !== 'reason') keys.add(key);
|
|
149
|
+
}
|
|
150
|
+
return { keys: [...keys], reasons: changed.filter((line) => now.has(line)).map((line) => line.match(/^reason\s*:\s*['"](.+)['"],?$/)?.[1]).filter(Boolean) };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The paths of the footprint rows marked «(правится)»: every path in backticks of such a row. */
|
|
154
|
+
export function editedPaths(plan) {
|
|
155
|
+
const paths = [];
|
|
156
|
+
for (const line of (plan ?? '').split('\n')) {
|
|
157
|
+
if (!line.startsWith('|') || !line.split('|')[1]?.includes(EDITED_ROW)) continue;
|
|
158
|
+
for (const match of line.matchAll(/`([^`\s]+)`/g)) paths.push(match[1]);
|
|
159
|
+
}
|
|
160
|
+
return paths;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The path is named by a footprint path: the same file, a directory that holds it, or a bare file
|
|
165
|
+
* name equal to its last part — a footprint row lists the files of one directory by name.
|
|
166
|
+
*/
|
|
167
|
+
export const namedBy = (path, paths) =>
|
|
168
|
+
paths.some((named) => path === named || (named.endsWith('/') && path.startsWith(named)) || (!named.includes('/') && path.split('/').pop() === named));
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The verdict on one call: the reason of a refusal, or null.
|
|
172
|
+
*
|
|
173
|
+
* `call` — { tool, path, content, edits, command } with `path` relative to the root of the tree.
|
|
174
|
+
* `ctx`:
|
|
175
|
+
* branch, key the checked-out branch and the task key of the tree
|
|
176
|
+
* tasksDir, plansDir where the task folders and the epic plans lie, relative to the root
|
|
177
|
+
* marksDir the directory of the marks relative to the root, or an absolute path
|
|
178
|
+
* protectedPaths patterns (regular expression sources) of the description and the engine
|
|
179
|
+
* overridePath the tree override of the description, relative to the root
|
|
180
|
+
* legacy `words.legacyBranches`
|
|
181
|
+
* read(path) the text of a file of the working copy, or null
|
|
182
|
+
* stepMark(number) the mark of a step of the branch, or null
|
|
183
|
+
* approved() the plan of the branch carries the approval mark
|
|
184
|
+
* ownerLast() the owner's last word: a typed message or the chosen options of a menu
|
|
185
|
+
* stateIndex(name) the place of a state in the scenario, or -1
|
|
186
|
+
* measured() the index of the measured state, or null when it cannot be measured
|
|
187
|
+
*/
|
|
188
|
+
export function judgeCall(call, ctx) {
|
|
189
|
+
const folder = `${ctx.tasksDir}/${ctx.branch}`;
|
|
190
|
+
const progressPath = `${folder}/progress.md`;
|
|
191
|
+
const planPath = `${folder}/plan.md`;
|
|
192
|
+
const marks = escape(ctx.marksDir);
|
|
193
|
+
|
|
194
|
+
if (call.tool === 'Bash') return judgeShell(call.command ?? '', ctx, marks);
|
|
195
|
+
|
|
196
|
+
const path = call.path ?? '';
|
|
197
|
+
if (path.startsWith(`${ctx.marksDir}/`) || path === ctx.marksDir) return 'the marks of the steps and of the approval are written only by the engine `task-flow-script.mjs`';
|
|
198
|
+
if (ctx.isRecord && ctx.isRecord(path)) return 'the session records are written only by the harness: an approval is read from them, and a hand write would forge it';
|
|
199
|
+
if (path === progressPath) return judgeProgress(call, ctx, planPath);
|
|
200
|
+
if (path.startsWith(`${ctx.plansDir}/`) && path.endsWith('.md')) return judgeDefer(call, ctx);
|
|
201
|
+
if (path === ctx.overridePath) return judgeOverride(call, ctx);
|
|
202
|
+
if (ctx.protectedPaths.some((source) => new RegExp(source).test(path))) return judgeProtected(path, ctx, planPath);
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function judgeShell(command, ctx, marks) {
|
|
207
|
+
const engine = /task-flow-script\.mjs/.test(command);
|
|
208
|
+
if (!engine && (shellWrites(command, marks) || shellWrites(command, 'rt-kit/(?:steps|approval)/'))) {
|
|
209
|
+
return 'the marks of the steps and of the approval are written only by the engine `task-flow-script.mjs`, never by a shell command';
|
|
210
|
+
}
|
|
211
|
+
if (shellWrites(command, '\\.claude/projects/[^\\s;|&"\']*\\.jsonl')) {
|
|
212
|
+
return 'the session records are written only by the harness, never by a shell command: an approval is read from them';
|
|
213
|
+
}
|
|
214
|
+
if (shellWrites(command, `${escape(ctx.tasksDir)}/[^\\s;|&]*(?:progress|plan)\\.md`) || shellWrites(command, '(?:^|[\\s/"\'])(?:progress|plan)\\.md')) {
|
|
215
|
+
return 'the progress and the plan are edited by the edit tool, not by a shell command: the guard judges an edit, and a shell write passes it unseen';
|
|
216
|
+
}
|
|
217
|
+
if (shellWrites(command, `${escape(ctx.plansDir)}/`)) {
|
|
218
|
+
return 'the epic plan is edited by the edit tool, not by a shell command: «отложено» in it needs the owner\'s word, and a shell write passes it unseen';
|
|
219
|
+
}
|
|
220
|
+
const written = command
|
|
221
|
+
.split(/[\s;|&<>'"]+/)
|
|
222
|
+
.map((token) => token.replace(/^\.\//, ''))
|
|
223
|
+
.find((token) => ctx.protectedPaths.some((source) => new RegExp(source).test(token)) && shellWrites(command, escape(token)));
|
|
224
|
+
if (written) return judgeProtected(written, ctx, `${ctx.tasksDir}/${ctx.branch}/plan.md`);
|
|
225
|
+
if (shellWrites(command, escape(ctx.overridePath))) return 'the override of the description is edited by the edit tool, and only by the owner\'s word naming the key';
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function judgeProgress(call, ctx, planPath) {
|
|
230
|
+
const before = ctx.read(call.path);
|
|
231
|
+
const after = textAfter(call, before);
|
|
232
|
+
const order = marksOutOfOrder(after);
|
|
233
|
+
if (order) return `${order}: done steps go first, and the one current step right after them`;
|
|
234
|
+
|
|
235
|
+
// The plan as the commit that added it wrote it: a plan stripped of its checks in the working copy
|
|
236
|
+
// lifts nothing. Before that commit there is no approved plan, and the working copy is all there is.
|
|
237
|
+
const plan = (ctx.committed ? ctx.committed(planPath) : null) ?? ctx.read(planPath);
|
|
238
|
+
const legacy = ctx.legacy.includes(ctx.branch) || stepsOfPlan(plan ?? '').every((step) => step.command === null);
|
|
239
|
+
if (!legacy) {
|
|
240
|
+
const marked = newlyDone(before, after);
|
|
241
|
+
if (marked.length > 0) {
|
|
242
|
+
return `step ${marked.join(', ')} is marked done after its check matched, by \`node tools/task-flow-script.mjs step ${marked[0]}\` itself: a hand mark is refused whatever mark lies on disk`;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const state = declaredState(after);
|
|
247
|
+
if (state && state !== declaredState(before)) {
|
|
248
|
+
const declared = ctx.stateIndex(state);
|
|
249
|
+
const measured = ctx.measured();
|
|
250
|
+
if (declared >= 0 && measured !== null && declared > measured) {
|
|
251
|
+
return `the state \`${state}\` is ahead of the measured one: the engine measures the branch below it — \`node tools/task-flow-script.mjs measure\` names the exit that is not met`;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const replaced = replacementChanges(before, after);
|
|
255
|
+
if (replaced.length > 0) {
|
|
256
|
+
return `the replacement of the check of step ${replaced.join(', ')} is the owner's decision: the owner allows or forbids this edit in the window`;
|
|
257
|
+
}
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function judgeDefer(call, ctx) {
|
|
262
|
+
const before = ctx.read(call.path);
|
|
263
|
+
const after = textAfter(call, before);
|
|
264
|
+
const changed = deferChanges(before, after, ctx.key);
|
|
265
|
+
if (changed.length === 0) {
|
|
266
|
+
const steps = ownerRowChanges(before, after, ctx.key);
|
|
267
|
+
if (steps.length === 0) return null;
|
|
268
|
+
return `«${OWNER_STEP}» for ${steps.map((task) => `${ctx.key}-${task}`).join(', ')} is not written into a row: the step is the executor's, and only the owner's word naming the task puts it off as «${DEFERRED}»`;
|
|
269
|
+
}
|
|
270
|
+
const word = ctx.ownerLast() ?? '';
|
|
271
|
+
const missing = changed.filter((task) => !new RegExp(`${escape(ctx.key)}-${task}(?!\\d)`).test(word));
|
|
272
|
+
if (missing.length === 0) return null;
|
|
273
|
+
return `«${DEFERRED}» for ${missing.map((task) => `${ctx.key}-${task}`).join(', ')} is set and lifted only by the owner's last word naming that task`;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function judgeOverride(call, ctx) {
|
|
277
|
+
const before = ctx.read(call.path);
|
|
278
|
+
const { keys, reasons } = changedKeys(before, textAfter(call, before));
|
|
279
|
+
const word = ctx.ownerLast() ?? '';
|
|
280
|
+
const unnamed = keys.filter((key) => !word.includes(key));
|
|
281
|
+
if (keys.length === 0 || unnamed.length > 0) {
|
|
282
|
+
return `the override of the description is edited only by the owner's last word naming the key it changes${unnamed.length ? `: ${unnamed.join(', ')}` : ''}`;
|
|
283
|
+
}
|
|
284
|
+
const unquoted = reasons.filter((reason) => !word.includes(reason));
|
|
285
|
+
if (unquoted.length > 0) return `the reason of a removed key is a quote of the owner's last word: «${unquoted[0]}» is not in it`;
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function judgeProtected(path, ctx, planPath) {
|
|
290
|
+
const fault = ctx.legacy.includes(ctx.branch) ? null : ctx.approvalFault ? ctx.approvalFault() : ctx.approved() ? null : 'there is no approval mark';
|
|
291
|
+
if (!fault && namedBy(path, editedPaths(ctx.read(planPath)))) return null;
|
|
292
|
+
const why = fault ? ` — ${fault}: ask the owner again and run \`node tools/task-flow-script.mjs plan-red\`` : '';
|
|
293
|
+
return `${path} belongs to the description of the task flow and its engine: it is edited only in a task whose approved plan names it in a footprint row marked «${EDITED_ROW}»${why}`;
|
|
294
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* The model of the task flow description: what the parsed description means, checked against the
|
|
4
|
+
* files it names, before any of them is run.
|
|
5
|
+
*
|
|
6
|
+
* The description has exactly six sections. `source` and `measure` name components: the key of an
|
|
7
|
+
* entry is a file — `measure.plan` is `measure/plan.mjs` next to the description — and every key
|
|
8
|
+
* under it is a capability, a named export of that file. The value under a capability is its ports:
|
|
9
|
+
* an object whose keys are the names the factory takes and whose values are references to other
|
|
10
|
+
* components. The ports equal the signature of the factory exactly — `()` or `({ a, b })` without
|
|
11
|
+
* defaults, renaming or nested patterns: a port the factory does not take is a wire to nowhere, and
|
|
12
|
+
* a default hides a missing wire. A missing capability is an error, never a substitute.
|
|
13
|
+
*
|
|
14
|
+
* A measurer gets its data only through its ports. So every file of `measure/` — the measurer and
|
|
15
|
+
* any neighbour it imports — imports only files of the same directory, by a path starting with
|
|
16
|
+
* `./`, and holds no `import()`, `require`, `createRequire`, `process` or `globalThis`. The files
|
|
17
|
+
* are read as text, not run: a measurer that reached for the disk would be refused before the
|
|
18
|
+
* engine ever loads it.
|
|
19
|
+
*
|
|
20
|
+
* `state` lists the states in the order they are written; a title is unique. The exit of a state
|
|
21
|
+
* and the `when` of a release case are conditions over the measurers of the description — a
|
|
22
|
+
* reference or `all`, `any`, `not` over them, see `flow-condition.mjs`. `words`, `expect` and the
|
|
23
|
+
* order of the release cases are checked by `flow-data.mjs`; `release` comes out as a list in its
|
|
24
|
+
* order. References between components form no cycle.
|
|
25
|
+
*
|
|
26
|
+
* A tree adds its own through an override in the same language, merged key by key before the model
|
|
27
|
+
* is built — see `flow-override.mjs`. A component the override names lies next to the override,
|
|
28
|
+
* its file path is relative to the description, and the same contract holds for it. The model
|
|
29
|
+
* names what the override changed in `changes`.
|
|
30
|
+
*
|
|
31
|
+
* Usage: node flow-model.mjs <flow.js> [--override <flow.js>] — prints the model as JSON, or
|
|
32
|
+
* `flow model: <what>` and exits 1. With `--exit <state> <results.json>` it prints `true` or
|
|
33
|
+
* `false` for the exit of that state, the results mapping `measure.<entry>.<capability>` to a
|
|
34
|
+
* boolean; a result that is absent or not `true` counts as unmet.
|
|
35
|
+
*/
|
|
36
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
37
|
+
import { dirname, join, relative } from 'node:path';
|
|
38
|
+
import { pathToFileURL } from 'node:url';
|
|
39
|
+
|
|
40
|
+
import { evaluateCondition, validateCondition } from './flow-condition.mjs';
|
|
41
|
+
import { checkExpect, checkWords, releaseOrder } from './flow-data.mjs';
|
|
42
|
+
import { mergeOverride } from './flow-override.mjs';
|
|
43
|
+
import { Call, FlowSyntaxError, Ref, SECTIONS, parse } from './flow-syntax.mjs';
|
|
44
|
+
|
|
45
|
+
/** A name of an entry, a capability or a port. */
|
|
46
|
+
const NAME = /^[A-Za-z][A-Za-z0-9]*$/;
|
|
47
|
+
/** The sections whose entries are files. */
|
|
48
|
+
const COMPONENT_SECTIONS = ['source', 'measure'];
|
|
49
|
+
/** What a file of `measure/` may not hold: a way to data past the ports. */
|
|
50
|
+
const MEASURE_BANS = [
|
|
51
|
+
[/\bimport\s*\(/, 'import()'],
|
|
52
|
+
[/\bcreateRequire\b/, 'createRequire'],
|
|
53
|
+
[/\brequire\s*\(/, 'require'],
|
|
54
|
+
[/\bprocess\b/, 'process'],
|
|
55
|
+
[/\bglobalThis\b/, 'globalThis'],
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
/** A refusal of the model: what is wrong, naming the component. */
|
|
59
|
+
export class FlowModelError extends Error {}
|
|
60
|
+
|
|
61
|
+
const fail = (what) => {
|
|
62
|
+
throw new FlowModelError(what);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** The text of a module without its comments: a ban word in an explanation is not code. */
|
|
66
|
+
function code(text) {
|
|
67
|
+
return text.replace(/\/\*[\s\S]*?\*\//g, '').replace(/(^|[^:])\/\/.*$/gm, '$1');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The ports a capability's factory takes, read from its declaration. `()` gives none, `({ a, b })`
|
|
72
|
+
* gives a and b; anything else — a default, a renaming, a nested pattern, a bare parameter — is
|
|
73
|
+
* refused, naming the component.
|
|
74
|
+
*/
|
|
75
|
+
export function signatureOf(text, cap, where) {
|
|
76
|
+
const body = code(text);
|
|
77
|
+
const fn = new RegExp(`export\\s+(?:async\\s+)?function\\s+${cap}\\s*\\(([^)]*)\\)`).exec(body);
|
|
78
|
+
const arrow = new RegExp(`export\\s+const\\s+${cap}\\s*=\\s*(?:async\\s*)?\\(([^)]*)\\)\\s*=>`).exec(body);
|
|
79
|
+
const match = fn ?? arrow;
|
|
80
|
+
if (!match) fail(`${where}: the file exports no capability «${cap}»`);
|
|
81
|
+
const params = match[1].trim();
|
|
82
|
+
if (params === '') return [];
|
|
83
|
+
const pattern = /^\{\s*([^{}]*?)\s*,?\s*\}$/.exec(params);
|
|
84
|
+
if (!pattern) fail(`${where}: the signature of «${cap}» is not «()» or «({ a, b })»: (${params})`);
|
|
85
|
+
const names = pattern[1].split(',').map((s) => s.trim()).filter(Boolean);
|
|
86
|
+
for (const name of names) {
|
|
87
|
+
if (!NAME.test(name)) fail(`${where}: the port «${name}» of «${cap}» carries a default, a renaming or a pattern`);
|
|
88
|
+
}
|
|
89
|
+
return names;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Every file of `measure/` imports only its neighbours and reaches no data past the ports. */
|
|
93
|
+
function checkMeasureDir(dir) {
|
|
94
|
+
if (!existsSync(dir)) return;
|
|
95
|
+
for (const name of readdirSync(dir).filter((f) => f.endsWith('.mjs') || f.endsWith('.js'))) {
|
|
96
|
+
const body = code(readFileSync(join(dir, name), 'utf8'));
|
|
97
|
+
for (const [ban, label] of MEASURE_BANS) {
|
|
98
|
+
if (ban.test(body)) fail(`measure/${name}: «${label}» is not allowed — a measurer gets its data through its ports`);
|
|
99
|
+
}
|
|
100
|
+
for (const m of body.matchAll(/\bimport\s+(?:[^'";]*?\s+from\s+)?['"]([^'"]+)['"]/g)) {
|
|
101
|
+
const spec = m[1];
|
|
102
|
+
if (!spec.startsWith('./') || spec.slice(2).includes('/')) {
|
|
103
|
+
fail(`measure/${name}: the import «${spec}» leaves the directory — only «./<file>» is allowed`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The component a reference points at, and the capability if it names one. */
|
|
110
|
+
function target(ref, components, where) {
|
|
111
|
+
const [section, entry, cap, ...rest] = ref.path;
|
|
112
|
+
if (!COMPONENT_SECTIONS.includes(section)) return null;
|
|
113
|
+
const component = components.get(`${section}.${entry}`);
|
|
114
|
+
if (!component) fail(`${where}: «${ref.path.join('.')}» points at no component`);
|
|
115
|
+
if (cap !== undefined && !(cap in component.caps)) fail(`${where}: «${ref.path.join('.')}» names no capability of ${section}.${entry}`);
|
|
116
|
+
if (rest.length > 0) fail(`${where}: «${ref.path.join('.')}» goes deeper than a capability`);
|
|
117
|
+
return component;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Builds the model of a description file, merged with the tree override when `override` names one.
|
|
122
|
+
* Throws `FlowModelError` or `FlowSyntaxError`.
|
|
123
|
+
*/
|
|
124
|
+
export function buildModel(path, { override } = {}) {
|
|
125
|
+
const base = dirname(path);
|
|
126
|
+
let description = parse(readFileSync(path, 'utf8'));
|
|
127
|
+
let origins = new Map();
|
|
128
|
+
let changes = [];
|
|
129
|
+
if (override) {
|
|
130
|
+
if (!existsSync(override)) fail(`the override ${override} is absent`);
|
|
131
|
+
({ description, origins, changes } = mergeOverride(description, parse(readFileSync(override, 'utf8')), dirname(override), fail));
|
|
132
|
+
checkMeasureDir(join(dirname(override), 'measure'));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const keys = Object.keys(description);
|
|
136
|
+
const extra = keys.filter((k) => !SECTIONS.includes(k));
|
|
137
|
+
const missing = SECTIONS.filter((k) => !keys.includes(k));
|
|
138
|
+
if (extra.length > 0) fail(`the description has exactly six sections — «${extra.join('», «')}» is not one of them`);
|
|
139
|
+
if (missing.length > 0) fail(`the description has exactly six sections — «${missing.join('», «')}» is missing`);
|
|
140
|
+
|
|
141
|
+
for (const section of SECTIONS) {
|
|
142
|
+
const value = description[section];
|
|
143
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value) || value instanceof Ref || value instanceof Call) {
|
|
144
|
+
fail(`the section «${section}» is an object of entries`);
|
|
145
|
+
}
|
|
146
|
+
for (const entry of Object.keys(value)) {
|
|
147
|
+
if (!NAME.test(entry)) fail(`${section}: the name «${entry}» is outside [A-Za-z][A-Za-z0-9]*`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
checkMeasureDir(join(base, 'measure'));
|
|
152
|
+
|
|
153
|
+
const components = new Map();
|
|
154
|
+
for (const section of COMPONENT_SECTIONS) {
|
|
155
|
+
for (const [entry, caps] of Object.entries(description[section])) {
|
|
156
|
+
const origin = origins.get(`${section}.${entry}`);
|
|
157
|
+
const where = origin ? `the override's ${section}.${entry}` : `${section}.${entry}`;
|
|
158
|
+
const file = join(origin ?? base, section, `${entry}.mjs`);
|
|
159
|
+
if (caps === null || typeof caps !== 'object' || Array.isArray(caps) || caps instanceof Ref || caps instanceof Call) {
|
|
160
|
+
fail(`${where}: an entry is an object of capabilities`);
|
|
161
|
+
}
|
|
162
|
+
if (!existsSync(file)) fail(`${where}: the file ${section}/${entry}.mjs is absent`);
|
|
163
|
+
const text = readFileSync(file, 'utf8');
|
|
164
|
+
const component = { section, entry, file: origin ? relative(base, file) : `${section}/${entry}.mjs`, caps: {} };
|
|
165
|
+
for (const [cap, ports] of Object.entries(caps)) {
|
|
166
|
+
if (!NAME.test(cap)) fail(`${where}: the name «${cap}» is outside [A-Za-z][A-Za-z0-9]*`);
|
|
167
|
+
if (ports === null || typeof ports !== 'object' || Array.isArray(ports) || ports instanceof Ref || ports instanceof Call) {
|
|
168
|
+
fail(`${where}.${cap}: the ports are an object, «{}» when there are none`);
|
|
169
|
+
}
|
|
170
|
+
const signature = signatureOf(text, cap, where);
|
|
171
|
+
const named = Object.keys(ports);
|
|
172
|
+
const lacking = signature.filter((p) => !named.includes(p));
|
|
173
|
+
const surplus = named.filter((p) => !signature.includes(p));
|
|
174
|
+
if (lacking.length > 0 || surplus.length > 0) {
|
|
175
|
+
fail(`${where}.${cap}: the ports {${named.join(', ')}} are not the signature ({ ${signature.join(', ')} })`);
|
|
176
|
+
}
|
|
177
|
+
component.caps[cap] = ports;
|
|
178
|
+
}
|
|
179
|
+
components.set(`${section}.${entry}`, component);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// The wires: every port points at an existing component, and the components form no cycle.
|
|
184
|
+
const edges = new Map();
|
|
185
|
+
for (const [where, component] of components) {
|
|
186
|
+
const to = new Set();
|
|
187
|
+
for (const [cap, ports] of Object.entries(component.caps)) {
|
|
188
|
+
for (const [port, value] of Object.entries(ports)) {
|
|
189
|
+
if (!(value instanceof Ref)) fail(`${where}.${cap}: the port «${port}» is a reference to a component`);
|
|
190
|
+
const t = target(value, components, `${where}.${cap}`);
|
|
191
|
+
if (!t) fail(`${where}.${cap}: the port «${port}» points outside source and measure`);
|
|
192
|
+
to.add(`${t.section}.${t.entry}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
edges.set(where, to);
|
|
196
|
+
}
|
|
197
|
+
const state = new Map();
|
|
198
|
+
const visit = (node, trail) => {
|
|
199
|
+
if (state.get(node) === 'done') return;
|
|
200
|
+
if (state.get(node) === 'open') fail(`the references form a cycle: ${[...trail, node].join(' → ')}`);
|
|
201
|
+
state.set(node, 'open');
|
|
202
|
+
for (const next of edges.get(node) ?? []) visit(next, [...trail, node]);
|
|
203
|
+
state.set(node, 'done');
|
|
204
|
+
};
|
|
205
|
+
for (const node of edges.keys()) visit(node, []);
|
|
206
|
+
|
|
207
|
+
const measures = new Map(
|
|
208
|
+
[...components.values()].filter((c) => c.section === 'measure').map((c) => [`measure.${c.entry}`, new Set(Object.keys(c.caps))]),
|
|
209
|
+
);
|
|
210
|
+
const titles = new Set();
|
|
211
|
+
const states = Object.entries(description.state).map(([key, s]) => {
|
|
212
|
+
if (s === null || typeof s !== 'object' || Array.isArray(s)) fail(`state.${key}: a state is an object`);
|
|
213
|
+
if (typeof s.title !== 'string' || s.title === '') fail(`state.${key}: the title is a string`);
|
|
214
|
+
if (titles.has(s.title)) fail(`state.${key}: the title «${s.title}» repeats`);
|
|
215
|
+
titles.add(s.title);
|
|
216
|
+
if (!('exit' in s)) fail(`state.${key}: the exit is missing — a measurer reference or a condition`);
|
|
217
|
+
validateCondition(s.exit, measures, `state.${key}.exit`, fail);
|
|
218
|
+
return { key, ...s };
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
for (const [key, r] of Object.entries(description.release)) {
|
|
222
|
+
if (r === null || typeof r !== 'object' || Array.isArray(r) || r instanceof Ref || r instanceof Call) {
|
|
223
|
+
fail(`release.${key}: a release case is an object`);
|
|
224
|
+
}
|
|
225
|
+
if (!('when' in r)) fail(`release.${key}: the when is missing — a measurer reference or a condition`);
|
|
226
|
+
validateCondition(r.when, measures, `release.${key}.when`, fail);
|
|
227
|
+
}
|
|
228
|
+
const release = releaseOrder(description.release, fail);
|
|
229
|
+
checkWords(description.words, fail);
|
|
230
|
+
checkExpect(description.expect, fail);
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
sources: [...components.values()].filter((c) => c.section === 'source'),
|
|
234
|
+
measures: [...components.values()].filter((c) => c.section === 'measure'),
|
|
235
|
+
states,
|
|
236
|
+
release,
|
|
237
|
+
words: description.words,
|
|
238
|
+
expect: description.expect,
|
|
239
|
+
changes,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
244
|
+
const args = process.argv.slice(2);
|
|
245
|
+
const at = args.indexOf('--override');
|
|
246
|
+
const override = at >= 0 ? args.splice(at, 2)[1] : undefined;
|
|
247
|
+
const [file, flag, key, results] = args;
|
|
248
|
+
if (!file || (at >= 0 && !override) || (flag !== undefined && (flag !== '--exit' || !key || !results))) {
|
|
249
|
+
console.error('usage: node flow-model.mjs <flow.js> [--override <flow.js>] [--exit <state> <results.json>]');
|
|
250
|
+
process.exit(2);
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
const model = buildModel(file, { override });
|
|
254
|
+
if (flag === undefined) {
|
|
255
|
+
console.log(JSON.stringify(model));
|
|
256
|
+
process.exit(0);
|
|
257
|
+
}
|
|
258
|
+
const state = model.states.find((s) => s.key === key);
|
|
259
|
+
if (!state) fail(`no state «${key}»`);
|
|
260
|
+
const values = JSON.parse(readFileSync(results, 'utf8'));
|
|
261
|
+
console.log(String(evaluateCondition(state.exit, (name) => values[name])));
|
|
262
|
+
} catch (error) {
|
|
263
|
+
if (error instanceof FlowSyntaxError) {
|
|
264
|
+
console.log(`flow syntax: ${error.message}`);
|
|
265
|
+
process.exit(1);
|
|
266
|
+
}
|
|
267
|
+
if (!(error instanceof FlowModelError)) throw error;
|
|
268
|
+
console.log(`flow model: ${error.message}`);
|
|
269
|
+
process.exit(1);
|
|
270
|
+
}
|
|
271
|
+
}
|