@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,496 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The guard of the course of work — the engine command `guard`, called by the hook `flow-step-guard.sh`
|
|
3
|
+
* before Edit, Write, MultiEdit and a shell command. It refuses what would move the scenario by a
|
|
4
|
+
* declaration instead of a measurement: a step marked done without the engine's green mark; marks out
|
|
5
|
+
* of order; the state line ahead of the measured state; a shell write into the progress, the plan, the
|
|
6
|
+
* epic plan, the marks, the session records, the description or the engine; «отложено» set or lifted,
|
|
7
|
+
* or an edit of the tree override, without the owner's last word naming the task or the key; an edit
|
|
8
|
+
* of the description or the engine outside a task whose approved plan names the path in a footprint
|
|
9
|
+
* row marked «(правится)». A replacement line of the progress is the owner's decision; «шаг владельца»
|
|
10
|
+
* on a row is refused. A plan with no check line, and a legacy branch, keep their done marks. The
|
|
11
|
+
* decisions are pure: the context brings the files, the marks, the owner's last word and the state.
|
|
12
|
+
*/
|
|
13
|
+
import { posix } from 'node:path';
|
|
14
|
+
|
|
15
|
+
import { stepsOfPlan, stepsOfProgress } from './work-steps-lib.mjs';
|
|
16
|
+
|
|
17
|
+
/** The word that defers a task in the epic plan; the words a row carried for a task handed to the owner. */
|
|
18
|
+
const [DEFERRED, OWNER_STEP] = ['отложено', 'шаг владельца'];
|
|
19
|
+
/** The opening of a replacement line of the progress; the mark of an edited footprint row; the state line. */
|
|
20
|
+
const REPLACEMENT_START = /^-\s+\*\*(?:Проверка шага|Check of step)\s+(\d+\.\d+)\s+(?:заменена|replaced):\*\*/u;
|
|
21
|
+
const EDITED_ROW = '(правится)';
|
|
22
|
+
const STATE_LINE = /^-\s+\*\*(?:State|Состояние):\*\*\s+`([^`]+)`/mu;
|
|
23
|
+
const escape = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
24
|
+
|
|
25
|
+
/** A path under the temporary directories or a device; a word the shell still expands. */
|
|
26
|
+
const TEMP = /^\/(?:private\/)?(?:tmp|var\/folders)(?:\/|$)|^\/dev\//;
|
|
27
|
+
const DYNAMIC = /[$`*?[]|\{[^}"':\s]*(?:,|\.\.)[^}"':\s]*\}/;
|
|
28
|
+
/** The commands that only read what they name; echo and printf name nothing but their redirects. */
|
|
29
|
+
const READERS = new Set('cat head tail less wc grep rg egrep fgrep jq diff cmp ls stat file test [ [[ echo printf awk gawk sed sort cut tr nl uniq column find gh mkdir cd pushd popd export local declare readonly for read true false : basename dirname realpath readlink pwd shasum sha256sum md5 md5sum xxd od cspell du tree chmod pgrep sleep which type date tsc'.split(' '));
|
|
30
|
+
const GIT_READERS = new Set('diff log show status add blame ls-files ls-tree grep rev-parse rev-list cat-file commit merge-base check-ignore diff-tree for-each-ref show-ref branch reflog shortlog describe'.split(' '));
|
|
31
|
+
const COPIES = new Set(['cp', 'mv', 'install', 'ln', 'rsync', 'tar', 'unzip']);
|
|
32
|
+
/** The words before a command that only run it, with their options that take a value. */
|
|
33
|
+
const PREFIXES = { nice: ['-n'], sudo: ['-u', '-g'], timeout: ['-s', '-k'], env: ['-u'], stdbuf: ['-o', '-e', '-i'], xargs: ['-n', '-I', '-L', '-P', '-d', '-s'], npx: ['-p'] };
|
|
34
|
+
for (const word of 'command exec time nohup builtin if then else elif do while until ! {'.split(' ')) PREFIXES[word] = [];
|
|
35
|
+
/** The inside of the string literals of a script: not code. */
|
|
36
|
+
const STRINGS = /'''[\s\S]*?'''|"""[\s\S]*?"""|`(?:\\[\s\S]|[^\\`])*`|'(?:\\.|[^\\'\n])*'|"(?:\\.|[^\\"\n])*"/g;
|
|
37
|
+
/** A call of a script that runs a command: then all its text is read, not only its paths. */
|
|
38
|
+
const RUNS = /(?<!\.)\bexec\w*\s*\(|\b(?:system|popen|spawn\w*)\s*\(|child_process|subprocess/;
|
|
39
|
+
/** A write or a command run in the code of a script; not a write to stdout, nor `.copy()` of a list. */
|
|
40
|
+
const WIDE = new RegExp(
|
|
41
|
+
`${RUNS.source}|\\b(?:writeFile|appendFile|createWriteStream)\\w*\\b|(?<!read(?:File|dir|link)?|exists|stat|lstat|realpath|access)Sync\\s*\\(|(?<!std(?:out|err)\\.)\\bwrite\\w*\\s*\\(|(?:\\b(?:os|shutil|fs|fsp|promises)\\.|(?<![.\\w]))(?:rename|unlink|remove|copy|move|truncate|rmdir|rm|cp|symlink|link|replace)\\w*\\s*\\(|\\bPath\\([^)]*\\)\\.(?:replace|rename|unlink|write_\\w+|\\w+_to)\\s*\\(|\\.touch\\s*\\(|\\bgetattr\\s*\\(|\\bopen\\s+(?:my\\s+)?[$\\w]+\\s*,`
|
|
42
|
+
);
|
|
43
|
+
/** A write call with a literal first argument under a temporary directory, and its handle: no file of the tree. */
|
|
44
|
+
const TEMP_PATH = `\\s*\\(\\s*(?<q>['"])\\/(?:private\\/)?(?:tmp|var\\/folders)\\/[^'"]*\\k<q>`;
|
|
45
|
+
const TEMP_WRITE = new RegExp(`\\b(?:open|writeFile\\w*|appendFile\\w*|createWriteStream)${TEMP_PATH}[^()]*(?:\\((?:[^()]|\\([^()]*\\))*\\)[^()]*)*\\)(?:\\.write\\w*)?`, 'g');
|
|
46
|
+
const TEMP_HANDLE = new RegExp(`(?:(\\w+)\\s*=\\s*|with\\s+)open${TEMP_PATH}[^)]*\\)(?:\\s+as\\s+(\\w+))?`, 'g');
|
|
47
|
+
/** A call of a script that copies, moves or removes a whole directory. */
|
|
48
|
+
const TREE_CALL = /\b(?:cp|copy|rename|rm|move|rmdir)\w*\s*\(|copytree|rmtree/;
|
|
49
|
+
|
|
50
|
+
/** The index of the bracket that closes the one at `at`, quotes skipped. */
|
|
51
|
+
function closing(text, at) {
|
|
52
|
+
let depth = 0;
|
|
53
|
+
for (let i = at; i < text.length; i++) {
|
|
54
|
+
if (text[i] === '\\') i++;
|
|
55
|
+
else if ("'\"`".includes(text[i])) i = text.indexOf(text[i], i + 1) < 0 ? text.length : text.indexOf(text[i], i + 1);
|
|
56
|
+
else if (text[i] === '(') depth++;
|
|
57
|
+
else if (text[i] === ')' && --depth === 0) return i;
|
|
58
|
+
}
|
|
59
|
+
return text.length;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The simple commands of a shell text, quotes taken off: { words, redirects, stdin, subs, piped,
|
|
64
|
+
* depth }. A here-document and a here-string are the stdin; `<(…)`, `>(…)`, `$(…)` and backticks the subs.
|
|
65
|
+
*/
|
|
66
|
+
export function shellCommands(text) {
|
|
67
|
+
const [commands, docs] = [[], []];
|
|
68
|
+
let [cmd, word, pending, piped, depth] = [null, null, null, false, 0];
|
|
69
|
+
const fresh = () => (cmd = { words: [], redirects: [], stdin: null, subs: [], piped, depth });
|
|
70
|
+
const endWord = () => {
|
|
71
|
+
if (word === null) return;
|
|
72
|
+
if (pending === '<<' || pending === '<<-') docs.push({ delim: word, strip: pending === '<<-', cmd });
|
|
73
|
+
else if (pending === '<<<') cmd.stdin = word;
|
|
74
|
+
else if (pending === '>') cmd.redirects.push(word);
|
|
75
|
+
else if (pending !== '<') cmd.words.push(word);
|
|
76
|
+
[pending, word] = [null, null];
|
|
77
|
+
};
|
|
78
|
+
const endCommand = (pipe) => {
|
|
79
|
+
endWord();
|
|
80
|
+
if (cmd.words.length || cmd.redirects.length || cmd.subs.length) commands.push(cmd);
|
|
81
|
+
piped = pipe;
|
|
82
|
+
fresh();
|
|
83
|
+
};
|
|
84
|
+
fresh();
|
|
85
|
+
for (let i = 0; i < text.length; ) {
|
|
86
|
+
const c = text[i];
|
|
87
|
+
if (c === '\\') {
|
|
88
|
+
if (text[i + 1] !== '\n') word = (word ?? '') + (text[i + 1] ?? '');
|
|
89
|
+
i += 2;
|
|
90
|
+
} else if (c === "'" || c === '"' || (c === '$' && text[i + 1] === "'")) {
|
|
91
|
+
// A double-quoted `$(…)` is kept whole: its own quotes do not end the word.
|
|
92
|
+
const from = c === '$' ? i + 2 : i + 1;
|
|
93
|
+
// A substitution in double quotes is a command of its own: it goes to the subs.
|
|
94
|
+
let j = from;
|
|
95
|
+
while (j < text.length && text[j] !== text[from - 1]) {
|
|
96
|
+
const end = c !== '"' ? -1 : text.startsWith('$(', j) ? closing(text, j + 1) : text[j] === '`' ? text.indexOf('`', j + 1) : -1;
|
|
97
|
+
if (end > j) cmd.subs.push(text.slice(j + (text[j] === '`' ? 1 : 2), end));
|
|
98
|
+
j = c === '"' && text[j] === '\\' ? j + 2 : end > j ? end + 1 : j + 1;
|
|
99
|
+
}
|
|
100
|
+
word = (word ?? '') + (c === '"' ? text.slice(from, j).replace(/\\(["\\$`])/g, '$1') : text.slice(from, j));
|
|
101
|
+
i = j + 1;
|
|
102
|
+
} else if (text.startsWith('$(', i) || c === '`') {
|
|
103
|
+
const end = c === '`' ? text.indexOf('`', i + 1) : closing(text, i + 1);
|
|
104
|
+
if (end > i) cmd.subs.push(text.slice(i + (c === '`' ? 1 : 2), end));
|
|
105
|
+
word = (word ?? '') + text.slice(i, end < 0 ? text.length : end + 1);
|
|
106
|
+
i = end < 0 ? text.length : end + 1;
|
|
107
|
+
} else if (c === '#' && word === null) while (i < text.length && text[i] !== '\n') i++;
|
|
108
|
+
else if (c === '\n') {
|
|
109
|
+
endCommand(false);
|
|
110
|
+
i++;
|
|
111
|
+
for (const doc of docs.splice(0)) {
|
|
112
|
+
const end = new RegExp(`^${doc.strip ? '\\t*' : ''}${escape(doc.delim)}$`, 'm').exec(text.slice(i)) ?? { index: text.length - i, 0: '' };
|
|
113
|
+
doc.cmd.stdin = text.slice(i, i + end.index).replace(/\n$/, '');
|
|
114
|
+
i += end.index + end[0].length + 1;
|
|
115
|
+
}
|
|
116
|
+
} else if (c === ' ' || c === '\t') {
|
|
117
|
+
endWord();
|
|
118
|
+
i++;
|
|
119
|
+
} else if (';|&()'.includes(c) && !(c === '&' && text[i + 1] === '>')) {
|
|
120
|
+
const pair = text[i + 1] === c && c !== '(' && c !== ')';
|
|
121
|
+
endCommand(c === '|' && !pair);
|
|
122
|
+
depth = Math.max(0, depth + (c === '(' ? 1 : c === ')' ? -1 : 0));
|
|
123
|
+
cmd.depth = depth;
|
|
124
|
+
i += pair ? 2 : 1;
|
|
125
|
+
} else if (c === '>' || c === '<' || c === '&') {
|
|
126
|
+
if (word !== null && /^\d+$/.test(word)) word = null;
|
|
127
|
+
endWord();
|
|
128
|
+
const op = text.slice(i).match(/^(?:<<<|<<-|<<|<>|<|&?>>|&?>\|?)/)[0];
|
|
129
|
+
i += op.length;
|
|
130
|
+
if (text[i] === '(') {
|
|
131
|
+
cmd.subs.push(text.slice(i + 1, closing(text, i)));
|
|
132
|
+
i = closing(text, i) + 1;
|
|
133
|
+
} else if (op.endsWith('>') && text[i] === '&' && /[\d-]/.test(text[i + 1] ?? '')) for (i++; /[\d-]/.test(text[i] ?? ''); ) i++;
|
|
134
|
+
else {
|
|
135
|
+
if (text[i] === '&' || text[i] === '!') i++;
|
|
136
|
+
pending = op.includes('>') ? '>' : op;
|
|
137
|
+
}
|
|
138
|
+
} else word = (word ?? '') + text[i++];
|
|
139
|
+
}
|
|
140
|
+
endCommand(false);
|
|
141
|
+
return commands;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** A script writes: a call of a write or of a command in its code, or `open` in a mode other than reading. */
|
|
145
|
+
function scriptWrites(text) {
|
|
146
|
+
const handles = [...text.matchAll(TEMP_HANDLE)].map((match) => match[1] ?? match[3]).filter(Boolean);
|
|
147
|
+
const script = text.replace(TEMP_WRITE, 'read()').replace(handles.length ? new RegExp(`\\b(?:${handles.join('|')})\\.write\\w*\\s*\\(`, 'g') : /^\b$/, 'read(');
|
|
148
|
+
const code = script.replace(STRINGS, (literal) => literal.replace(/[^\n]/g, ' '));
|
|
149
|
+
if (WIDE.test(code) || RUNS.test(script) || /shutil|\bIO\.|\bFile\.(?:open|write)|\[\s*['"]\w*(?:write|append|rename|unlink|rm|cp|copy|truncate|link)\w*['"]\s*\]/i.test(script)) return true;
|
|
150
|
+
return [...code.matchAll(/(\.?)\bopen\s*\(/g)].some((call) => {
|
|
151
|
+
const from = call.index + call[0].length;
|
|
152
|
+
const end = closing(code, from - 1);
|
|
153
|
+
let [k, depth] = [from, 0];
|
|
154
|
+
for (; k < end && !(code[k] === ',' && depth === 0); k++) depth += '([{'.includes(code[k]) ? 1 : ')]}'.includes(code[k]) ? -1 : 0;
|
|
155
|
+
// A method takes the mode first: `Path(p).open('w')`; a function second: `open(p, 'w')`.
|
|
156
|
+
const mode = (call[1] ? script.slice(from, k) : script.slice(k + 1, end)).trim();
|
|
157
|
+
return !(mode === '' || /^(?:['"][rbtU]*['"]|encoding\s*=|errors\s*=|newline\s*=)/.test(mode)) || /mode\s*=\s*['"][^'"]*[wax+]/.test(script.slice(from, end));
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The word with the variables of the command put in; `mktemp` and `$TMPDIR` are a temporary directory. */
|
|
162
|
+
const expand = (word, vars) =>
|
|
163
|
+
word.replace(/\$\(mktemp\b[^)]*\)/g, '/tmp/mktemp').replace(/^~(?=\/|$)/, '/HOME').replace(/\$(?:\{(\w+)[^}]*\}|(\w+))/g, (all, braced, bare) => ({ TMPDIR: '/tmp', HOME: '/HOME' })[braced ?? bare] ?? vars.get(braced ?? bare) ?? all);
|
|
164
|
+
|
|
165
|
+
/** Every tail of a path after a slash: a path of the tree may be written from any directory above it. */
|
|
166
|
+
const tails = (path) => path.split('/').map((_, k, parts) => parts.slice(k).join('/')).filter(Boolean);
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* A path lies in a guarded directory: in it, below it when the area ends with a slash, above it when
|
|
170
|
+
* `up`. A relative path counts from the root; an absolute one by a tail, only on an area of two parts.
|
|
171
|
+
*/
|
|
172
|
+
const inArea = (path, area, up) => {
|
|
173
|
+
const [dir, deep] = [area.replace(/\/$/, ''), area.endsWith('/')];
|
|
174
|
+
const here = (tail) => (tail === dir && (deep || up)) || (deep && tail.startsWith(`${dir}/`));
|
|
175
|
+
return path.startsWith('/') ? dir.includes('/') && tails(path).some(here) : here(path) || (up && (path === '.' || dir.startsWith(`${path}/`)));
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/** The guarded files of a shell command: the sign of their name in a text, their directories, the reason. */
|
|
179
|
+
function shellGuards(ctx) {
|
|
180
|
+
const marks = posix.normalize(ctx.marksDir);
|
|
181
|
+
const engine = (tail) => ctx.protectedPaths.some((source) => new RegExp(source).test(tail));
|
|
182
|
+
const shell = 'by the edit tool, not by a shell command';
|
|
183
|
+
const name = (text) => text.split(/[\s;|&<>'"()=,`{}]+/).flatMap((token) => tails(token.replace(/^\.\//, ''))).find(engine);
|
|
184
|
+
// The directory of a pattern: all of it when the pattern ends with a slash, else only its own files.
|
|
185
|
+
const head = (source) => source.replace(/^\^|^\(\?:\^\|\/\)/, '').match(/^[\w./-]*/)[0];
|
|
186
|
+
const area = (source) => (head(source).endsWith('/') ? head(source) : head(source).replace(/\/?[^/]*$/, ''));
|
|
187
|
+
return [
|
|
188
|
+
[new RegExp(`rt-kit/(?:steps|approval)|${escape(marks)}`), [`${marks}/`, 'rt-kit/steps/', 'rt-kit/approval/'], () => 'the marks of the steps and of the approval are written only by the engine `task-flow-script.mjs`, never by a shell command'],
|
|
189
|
+
[/\.claude\/projects\/\S*(?:\.jsonl\b|\*)/, ['.claude/projects/'], () => 'the session records are written only by the harness, never by a shell command: an approval is read from them'],
|
|
190
|
+
[new RegExp(`(?:^|[^\\w.-])(?:progress|plan)\\.md(?![\\w.-])|${escape(ctx.tasksDir)}/\\S*[*?[]`, 'i'),[`${ctx.tasksDir}/`], () => `the progress and the plan are edited ${shell}: the guard judges an edit, and a shell write passes it unseen`],
|
|
191
|
+
[new RegExp(`(?:^|[^\\w.-])${escape(ctx.plansDir)}/`), [`${ctx.plansDir}/`], () => `the epic plan is edited ${shell}: «отложено» in it needs the owner's word, and a shell write passes it unseen`],
|
|
192
|
+
[{ exec: (text) => (name(text) ? [name(text)] : null) }, ctx.protectedPaths.map(area).filter(Boolean), (path) => judgeProtected(path, ctx, `${ctx.tasksDir}/${ctx.branch}/plan.md`)],
|
|
193
|
+
[new RegExp(`${escape(posix.dirname(ctx.overridePath))}/`), [`${posix.dirname(ctx.overridePath)}/`], () => `the override of the description is edited ${shell}, and only by the owner's word naming the key`],
|
|
194
|
+
];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The paths a script names: its literals with no space in them but what it imports, its globs when it globs. */
|
|
198
|
+
const literals = (script) =>
|
|
199
|
+
(script.replace(/\b(?:from|import\s*\(|require\s*\()\s*(['"`])[^'"`]*\1/g, '').match(STRINGS) ?? []).map((literal) => literal.replace(/^('''|"""|['"`])([\s\S]*)\1$/, '$2')).filter((literal) => literal && !/\s/.test(literal) && (!/[*?[]/.test(literal) || /glob|readdir|listdir|walk|scandir|Dir\[/.test(script))).join(' ');
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* The verdict on a shell command, simple command by simple command, variables of the command put in; a
|
|
203
|
+
* script the command wrote is judged, a script it did not write is code of the tree and reads.
|
|
204
|
+
*/
|
|
205
|
+
function judgeShell(command, ctx, level = 0, vars = new Map(), cwd = '.') {
|
|
206
|
+
const guards = shellGuards(ctx);
|
|
207
|
+
const written = new Map();
|
|
208
|
+
// A function the command defines runs its body, and the body is judged as commands of its own.
|
|
209
|
+
const defined = new Set([...command.matchAll(/(?:^|[\s;&|({])(?:function\s+)?([\w-]+)\s*\(\)\s*\{/g)].map((match) => match[1]));
|
|
210
|
+
const cwds = [cwd];
|
|
211
|
+
let pipeline = '';
|
|
212
|
+
for (const unit of shellCommands(command)) {
|
|
213
|
+
cwds.length = Math.min(cwds.length, unit.depth + 1);
|
|
214
|
+
while (cwds.length <= unit.depth) cwds.push(cwds.at(-1));
|
|
215
|
+
// The path of a word; of a word the shell expands, the directory before it — none after an unknown variable.
|
|
216
|
+
let base = cwds[unit.depth];
|
|
217
|
+
const at = (word) => {
|
|
218
|
+
const text = expand(word, vars);
|
|
219
|
+
const cut = text.search(DYNAMIC);
|
|
220
|
+
const slash = text.lastIndexOf('/', cut);
|
|
221
|
+
if (cut >= 0 && (text.includes('..') || (slash < 0 && /[$`]/.test(text[cut])))) return null;
|
|
222
|
+
const path = cut < 0 ? text : text.slice(0, slash + 1);
|
|
223
|
+
return path.startsWith('/') || base !== null ? posix.normalize(posix.join(path.startsWith('/') ? '/' : base, path)).replace(/(.)\/$/, '$1') : null;
|
|
224
|
+
};
|
|
225
|
+
// A temporary path names nothing, save a session record: the records of a session may lie there.
|
|
226
|
+
const spared = (word) => at(word) !== null && TEMP.test(at(word)) && !guards[1][0].test(expand(word, vars));
|
|
227
|
+
let words = unit.words.map((word) => expand(word, vars));
|
|
228
|
+
for (const word of words) {
|
|
229
|
+
const set = word.match(/^(\w+)=([\s\S]*)$/);
|
|
230
|
+
if (set) vars.set(set[1], set[2]);
|
|
231
|
+
else if (!['export', 'local', 'declare', 'readonly'].includes(word)) break;
|
|
232
|
+
}
|
|
233
|
+
if (words[0] === 'for' && words[2] === 'in') vars.set(words[1], words.slice(3).join(' '));
|
|
234
|
+
let fed = false;
|
|
235
|
+
while (words.length > 0 && (Object.hasOwn(PREFIXES, posix.basename(words[0])) || /^\w+=/.test(words[0]) || (/^p?npm$/.test(words[0]) && /^(?:exec|dlx)$/.test(words[1])))) {
|
|
236
|
+
const head = posix.basename(words.shift());
|
|
237
|
+
if (/^p?npm$/.test(head)) words.shift();
|
|
238
|
+
while (words[0]?.startsWith('-')) if ((PREFIXES[head] ?? []).includes(words.shift())) words.shift();
|
|
239
|
+
if (head === 'timeout') words.shift();
|
|
240
|
+
fed ||= head === 'xargs';
|
|
241
|
+
}
|
|
242
|
+
const name = posix.basename(words[0] ?? '');
|
|
243
|
+
const args = words.slice(1);
|
|
244
|
+
const operands = args.filter((arg) => !arg.startsWith('-'));
|
|
245
|
+
const fromPipe = unit.piped ? pipeline : '';
|
|
246
|
+
pipeline = `${unit.piped ? `${pipeline} ` : ''}${args.join(' ')} ${unit.stdin ?? ''}`;
|
|
247
|
+
if (name === 'cd' || name === 'pushd' || name === 'popd') cwds[unit.depth] = name === 'popd' || operands[0] === '-' ? null : at(operands[0] ?? '~');
|
|
248
|
+
if (name === 'git' && args.includes('-C')) base = at(args[args.indexOf('-C') + 1] ?? '');
|
|
249
|
+
const flag = (sign) => args.some((arg) => sign.test(arg));
|
|
250
|
+
// A runner: the script inline, from a here-document, a pipe or a file this command wrote.
|
|
251
|
+
const runsWritten = written.has(at(words[0] ?? '') ?? '');
|
|
252
|
+
const shellRunner = /^(?:bash|sh|zsh|source|\.|eval)$/.test(name) || runsWritten;
|
|
253
|
+
const interpreter = /^(?:python[\d.]*|node|nodejs|ruby|perl|deno|bun)$/.test(name);
|
|
254
|
+
const argv = [...args];
|
|
255
|
+
let [script, trusted, params] = [null, false, []];
|
|
256
|
+
if (shellRunner || interpreter) {
|
|
257
|
+
const inline = args.findIndex((arg) => /^-[a-zA-Z]*[ce]$/.test(arg) || ['--eval', '-p', '--print'].includes(arg));
|
|
258
|
+
const file = runsWritten ? words[0] : operands[0];
|
|
259
|
+
if (name === 'eval') script = args.join(' ');
|
|
260
|
+
else if (inline >= 0) [script, params] = [(argv.splice(inline + 1, 1)[0] ?? '').replaceAll('{}', fed ? fromPipe.trim() : '{}'),shellRunner ? args.slice(inline + 2) : []];
|
|
261
|
+
else if (file && file !== '-') [script, trusted, params] = [written.get(at(file)) ?? null, !written.has(at(file)) && !flag(/^-m$/), runsWritten ? [words[0], ...operands] : operands];
|
|
262
|
+
else script = unit.stdin ?? (unit.piped ? fromPipe : null);
|
|
263
|
+
}
|
|
264
|
+
// A shell script is a command of its own, its positional parameters put in; so is what find runs.
|
|
265
|
+
const [option, exec] = [args.findIndex((arg) => /^[-(!]/.test(arg)), name === 'find' ? args.findIndex((arg) => /^-(?:exec|execdir|ok|okdir)$/.test(arg)) : -1];
|
|
266
|
+
const [starts, quote] = [option < 0 ? args : args.slice(0, option), (arg) => (/^[\w./=@%+:,-]*$/.test(arg) ? arg : `'${arg.replaceAll("'", `'\\''`)}'`)];
|
|
267
|
+
const found = exec < 0 ? [] : [args.slice(exec + 1).filter((arg, k, list) => !list.slice(0, k + 1).some((one) => one === ';' || one === '+')).map((arg) => quote(arg.replaceAll('{}', starts.map((start) => `${start}/*`).join(' ')))).join(' ')];
|
|
268
|
+
for (const inner of [...unit.subs, ...found, ...(shellRunner && script !== null && level < 3 ? [script] : [])]) {
|
|
269
|
+
const verdict = judgeShell(inner, ctx, level + 1, new Map([...vars, ...params.map((value, k) => [String(k), value])]), base);
|
|
270
|
+
if (verdict) return verdict;
|
|
271
|
+
}
|
|
272
|
+
const data = name === 'echo' || name === 'printf';
|
|
273
|
+
const content = unit.stdin ?? (data ? args.join(' ') : null);
|
|
274
|
+
for (const target of [...unit.redirects, ...(name === 'tee' ? operands : [])]) if (at(target) !== null) written.set(at(target), content ?? fromPipe);
|
|
275
|
+
// The names: the words, the redirects, the names a copy lands in its target, the script — of a
|
|
276
|
+
// script that runs no command only the paths it names — and the words piped into xargs. A
|
|
277
|
+
// here-document of a command that is not a runner is the text it writes, not a name; so are
|
|
278
|
+
// the words of echo and printf.
|
|
279
|
+
const shown = interpreter && script !== null && !RUNS.test(script) ? literals(script) : script;
|
|
280
|
+
const target = args.find((arg, k) => ['-t', '-C', '-d', '--target-directory'].includes(args[k - 1])) ?? args.find((arg) => arg.startsWith('--target-directory='))?.slice(19);
|
|
281
|
+
const into = target ?? (operands.length > 1 && !['tar', 'unzip'].includes(name) ? operands.at(-1) : null);
|
|
282
|
+
const landed = COPIES.has(name) && into ? operands.filter((word) => word !== into).map((word) => posix.join(into, posix.basename(word))) : [];
|
|
283
|
+
const pieces = [...(data ? [] : argv), ...landed, ...unit.redirects.map((word) => expand(word, vars)), trusted ? '' : (shown ?? ''), fed ? fromPipe : ''];
|
|
284
|
+
const tokens = pieces.join(' ').split(/\s+/).filter(Boolean);
|
|
285
|
+
const text = tokens.flatMap((token) => (spared(token) ? [] : at(token) !== null && at(token) !== token && !token.startsWith('/') ? [token, at(token)] : [token])).join(' ');
|
|
286
|
+
const named = guards.filter(([sign]) => sign.exec(text));
|
|
287
|
+
// A write whose target is not a literal path is named in the refusal: the target first, then the rest.
|
|
288
|
+
const loose = [...unit.redirects, into ?? '', ...argv.filter((arg) => !arg.startsWith('-'))].map((word) => expand(word, vars)).find((word) => DYNAMIC.test(word));
|
|
289
|
+
const refuse = (by, path) => {
|
|
290
|
+
const reason = by[2](path ?? by[0].exec(text)?.[0]);
|
|
291
|
+
return reason && loose ? `${reason} — \`${loose}\` is not a literal path: write the path as a literal` : reason;
|
|
292
|
+
};
|
|
293
|
+
// The directories a copy, a move, a removal or a checkout writes into, and whether it reaches below.
|
|
294
|
+
// A copy of named files is judged by the names it lands; a tree, or a source the shell expands, by its target.
|
|
295
|
+
// A whole task folder is removed or moved away as the taking-apart does, and made from the template
|
|
296
|
+
// when it does not stand yet; a write into a standing folder from anything else is judged below.
|
|
297
|
+
const [taskFolder, template] = [`(?!_template$)[^/]+$`, '_template(?:/|$)'].map((tail) => (word) => new RegExp(`(?:^|/)${escape(ctx.tasksDir)}/${tail}`).test((DYNAMIC.test(expand(word, vars)) ? expand(word, vars) : at(word)) ?? ''));
|
|
298
|
+
const sources = operands.filter((word) => word !== into);
|
|
299
|
+
if (name === 'cp' && into && taskFolder(into) && sources.every(template) && !ctx.read?.(`${at(into)}/progress.md`)) continue;
|
|
300
|
+
const sub = name === 'git' ? args.find((arg, k) => !arg.startsWith('-') && !['-C', '-c'].includes(args[k - 1])) : null;
|
|
301
|
+
const staged = flag(/^(?:--staged|-S)$/) && !flag(/^(?:--worktree|-W)$/);
|
|
302
|
+
const recursive = flag(/^-[a-zA-Z]*[rRa]|^--(?:recursive|archive)$/) || ['rsync', 'tar', 'unzip'].includes(name);
|
|
303
|
+
const mode = name === 'tar' ? args.filter((arg, k) => /^-[a-zA-Z]+$/.test(arg) || (k === 0 && /^[a-zA-Z]+$/.test(arg))).join('') : '';
|
|
304
|
+
const listing = (name === 'tar' && /[tO]/.test(mode)) || (name === 'unzip' && flag(/^-[lpvZ]/));
|
|
305
|
+
const dirs = [];
|
|
306
|
+
const folder = (word) => !/\.\w+$/.test(word);
|
|
307
|
+
const unnamed = recursive || sources.some((word) => DYNAMIC.test(word) || (name === 'mv' && folder(word))) || into === null;
|
|
308
|
+
const moved = name === 'mv' && sources.length > 0 && sources.every(taskFolder);
|
|
309
|
+
if (COPIES.has(name) && unnamed && !listing && !moved && (name !== 'tar' || /x/.test(mode))) dirs.push([into ?? (['tar', 'unzip'].includes(name) ? '.' : null), recursive]);
|
|
310
|
+
if (name === 'mv') dirs.push(...sources.filter((word) => folder(word) && !taskFolder(word)).map((word) => [word, true]));
|
|
311
|
+
if (name === 'rm' && recursive) dirs.push(...operands.filter((word) => !taskFolder(word)).map((word) => [word, true]));
|
|
312
|
+
if (name === 'find' && flag(/^-(?:delete|fprint)/)) dirs.push(...starts.map((word) => [word, true]));
|
|
313
|
+
// A git path: every word after `--`; before it, a word with a slash or one that holds a guarded directory.
|
|
314
|
+
const dash = args.indexOf('--');
|
|
315
|
+
const gitPaths = dash >= 0 ? args.slice(dash + 1) : operands.slice(operands.indexOf(sub) + 1).filter((arg) => !/^[\w.~^@{}-]+$/.test(arg) || arg === '.' || guards.some(([, list]) => list.some((one) => inArea(at(arg) ?? '', one, true))));
|
|
316
|
+
if (sub === 'checkout' || (sub === 'restore' && !staged) || sub === 'clean') dirs.push(...(gitPaths.length || sub !== 'clean' ? gitPaths : ['.']).filter(folder).map((word) => [word, true]));
|
|
317
|
+
// A script that copies or removes a directory and names a task folder itself writes into the folder.
|
|
318
|
+
if (interpreter && script !== null && TREE_CALL.test(script.replace(STRINGS, '""'))) dirs.push(...(text.match(new RegExp(`${escape(ctx.tasksDir)}/[^/\\s'"]+(?=/?['"\\s]|/?$)`, 'g')) ?? []).map((word) => [word, false]));
|
|
319
|
+
// An in-place edit of sed, awk, perl or ruby writes the files it names, not its expression.
|
|
320
|
+
const inPlace = /^(?:g?sed|g?awk|perl|ruby)$/.test(name) && flag(/^-[a-zA-Z0-9]*i|^--in-place|^inplace$/);
|
|
321
|
+
const expressions = args.filter((arg, k) => /^-[a-zA-Z]*e$|^--expression$/.test(args[k - 1] ?? ''));
|
|
322
|
+
const edited = [...operands.filter((arg) => arg && arg !== 'inplace' && !expressions.includes(arg)).slice(expressions.length || interpreter ? 0 : 1), ...(fed ? fromPipe.split(/\s+/).filter(Boolean) : [])];
|
|
323
|
+
// A write target the shell still expands is judged by the directory it lies in.
|
|
324
|
+
dirs.push(...[...unit.redirects, ...(inPlace ? edited : []), ...(name === 'rm' ? operands : [])].filter((word) => DYNAMIC.test(expand(word, vars))).map((word) => [word, false]));
|
|
325
|
+
// awk that prints into a file or runs a command, and sed with a `w` command, write.
|
|
326
|
+
const scripted = (/^g?awk$/.test(name) && flag(/printf?[^;}]*>|system\s*\(|\|\s*"/)) || (/^g?sed$/.test(name) && flag(/(?:^|[;{}\n]|\/[gpiIme0-9]*)\s*w\s+\S/));
|
|
327
|
+
const archive = args[args.findIndex((arg) => /^-?[a-zA-Z]*f$/.test(arg)) + 1] ?? '';
|
|
328
|
+
const reads = READERS.has(name) ? !scripted && !(name === 'sort' && flag(/^-o|^--output/)) && !(name === 'find' && flag(/^-(?:delete|fprint)/))
|
|
329
|
+
: name === 'git' ? GIT_READERS.has(sub) || (sub === 'restore' && staged) || (sub === 'reset' && !flag(/^--(?:hard|merge|keep)$/))
|
|
330
|
+
: ['cp', 'install', 'ln', 'rsync'].includes(name) ? into !== null && spared(into)
|
|
331
|
+
: name === 'tar' || name === 'unzip' ? listing || (/c/.test(mode) && spared(archive))
|
|
332
|
+
: ['prettier', 'eslint', 'markdownlint', 'markdownlint-cli2', 'iconv'].includes(name) ? !flag(/^(?:-w|--write|--fix\b.*|-o|--output\b.*)$/)
|
|
333
|
+
: shellRunner || interpreter ? trusted || (script !== null && (shellRunner || !scriptWrites(script)))
|
|
334
|
+
: name === '' || defined.has(name);
|
|
335
|
+
const reader = unit.redirects.every(spared) && (inPlace ? edited.every(spared) : reads);
|
|
336
|
+
if (fed && !reader) dirs.push(...fromPipe.split(/\s+/).filter(Boolean).map((word) => [word, true]));
|
|
337
|
+
const why = reader ? null : named.map((by) => refuse(by)).find(Boolean);
|
|
338
|
+
if (why) return why;
|
|
339
|
+
for (const [dir, up] of dirs) {
|
|
340
|
+
const path = dir === null ? null : at(dir);
|
|
341
|
+
const areas = path === null ? named : TEMP.test(path) ? [] : guards.filter(([, list]) => list.some((one) => inArea(path, one, up)));
|
|
342
|
+
const refused = areas.map((by) => refuse(by, path ?? undefined)).find(Boolean);
|
|
343
|
+
if (refused) return refused;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** The text an edit leaves: Write gives it whole, Edit and MultiEdit replace in the text before. */
|
|
350
|
+
export function textAfter(call, before) {
|
|
351
|
+
if (call.tool === 'Write') return call.content ?? '';
|
|
352
|
+
const apply = (text, edit) => (edit.replaceAll ? text.split(edit.oldString).join(edit.newString) : text.replace(edit.oldString, () => edit.newString));
|
|
353
|
+
return (call.edits ?? []).reduce(apply, before ?? '');
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** The done and current marks of the progress are in order: done steps first, the current right after. */
|
|
357
|
+
export function marksOutOfOrder(progress) {
|
|
358
|
+
const { steps } = stepsOfProgress(progress);
|
|
359
|
+
let open = false;
|
|
360
|
+
for (const step of steps) {
|
|
361
|
+
if (open && (step.mark === 'x' || step.mark === '>')) return `step ${step.number} is marked ${step.mark === 'x' ? 'done' : 'current'} after a step that is not done`;
|
|
362
|
+
if (step.mark !== 'x') open = true;
|
|
363
|
+
}
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** The steps the edit marks done that were not done before. */
|
|
368
|
+
export function newlyDone(before, after) {
|
|
369
|
+
const was = new Set(stepsOfProgress(before ?? '').steps.filter((s) => s.mark === 'x').map((s) => s.number));
|
|
370
|
+
return stepsOfProgress(after).steps.filter((s) => s.mark === 'x' && !was.has(s.number)).map((s) => s.number);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** The state the progress declares, or null. */
|
|
374
|
+
export const declaredState = (text) => text?.match(STATE_LINE)?.[1] ?? null;
|
|
375
|
+
|
|
376
|
+
/** The task numbers of the epic plan lines whose «отложено» the edit sets or lifts. */
|
|
377
|
+
export function deferChanges(before, after, key) {
|
|
378
|
+
const numbers = (line) => [...line.matchAll(new RegExp(`${escape(key)}-(\\d+)`, 'g'))].map((match) => match[1]);
|
|
379
|
+
const deferredTasks = (text) => new Set((text ?? '').split('\n').filter((line) => line.includes(DEFERRED)).flatMap(numbers));
|
|
380
|
+
const [was, now] = [deferredTasks(before), deferredTasks(after)];
|
|
381
|
+
return [...new Set([...was, ...now])].filter((task) => was.has(task) !== now.has(task));
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** The lines of the text after the edit that match the sign and did not stand in the text before. */
|
|
385
|
+
const newLines = (before, after, sign) => ((was) => (after ?? '').split('\n').filter((line) => sign(line) && !was.has(line.trim())))(new Set((before ?? '').split('\n').map((line) => line.trim())));
|
|
386
|
+
|
|
387
|
+
/** The step numbers whose replacement line the edit adds or changes; dropping one needs no owner's word. */
|
|
388
|
+
export const replacementChanges = (before, after) => [...new Set(newLines(before, after, (line) => REPLACEMENT_START.test(line)).map((line) => line.match(REPLACEMENT_START)[1]))];
|
|
389
|
+
|
|
390
|
+
/** The tasks of the epic plan rows the edit marks «шаг владельца» (refused: the step is the executor's). */
|
|
391
|
+
export function ownerRowChanges(before, after, key) {
|
|
392
|
+
const marked = (text) => new RegExp(`\\|\\s*${OWNER_STEP}[^|]*\\|?\\s*$`, 'u').test(text);
|
|
393
|
+
const numbers = (line) => [...line.matchAll(new RegExp(`${escape(key)}-(\\d+)`, 'g'))].map((match) => match[1]);
|
|
394
|
+
// A row that carried the mark before the edit is a legacy row: its other cells may be edited.
|
|
395
|
+
const legacy = new Set((before ?? '').split('\n').filter(marked).flatMap(numbers));
|
|
396
|
+
return [...new Set(newLines(before, after, marked).flatMap(numbers))].filter((task) => !legacy.has(task));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** The keys an edit of the override changes: `key:` of every line that differs. */
|
|
400
|
+
export function changedKeys(before, after) {
|
|
401
|
+
const lines = (text) => new Set((text ?? '').split('\n').map((line) => line.trim()).filter(Boolean));
|
|
402
|
+
const [was, now] = [lines(before), lines(after)];
|
|
403
|
+
const changed = [...[...now].filter((line) => !was.has(line)), ...[...was].filter((line) => !now.has(line))];
|
|
404
|
+
const keys = new Set(changed.map((line) => line.match(/^['"]?([A-Za-z_][\w-]*)['"]?\s*:/)?.[1]).filter((key) => key && key !== 'reason'));
|
|
405
|
+
return { keys: [...keys], reasons: changed.filter((line) => now.has(line)).map((line) => line.match(/^reason\s*:\s*['"](.+)['"],?$/)?.[1]).filter(Boolean) };
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** The paths of the footprint rows marked «(правится)»: every path in backticks of such a row. */
|
|
409
|
+
export function editedPaths(plan) {
|
|
410
|
+
const rows = (plan ?? '').split('\n').filter((line) => line.startsWith('|') && line.split('|')[1]?.includes(EDITED_ROW));
|
|
411
|
+
return rows.flatMap((line) => [...line.matchAll(/`([^`\s]+)`/g)].map((match) => match[1]));
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** The path is named by a footprint path: the same file, a directory that holds it, or its bare file name. */
|
|
415
|
+
export const namedBy = (path, paths) =>
|
|
416
|
+
paths.some((named) => path === named || (named.endsWith('/') && path.startsWith(named)) || (!named.includes('/') && path.split('/').pop() === named));
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* The verdict on one call: the reason of a refusal, or null. `call` — { tool, path, content, edits,
|
|
420
|
+
* command }, `path` relative to the root of the tree. `ctx`: `branch` and `key` of the tree;
|
|
421
|
+
* `tasksDir` and `plansDir` relative to the root; `marksDir` relative or absolute; `protectedPaths`,
|
|
422
|
+
* the patterns of the description and the engine; `overridePath`; `legacy`, `words.legacyBranches`;
|
|
423
|
+
* `read(path)`, a file of the working copy or null; `stepMark(number)`; `approved()`, the approval
|
|
424
|
+
* mark of the plan; `ownerLast()`, the owner's typed message or chosen options; `stateIndex(name)`,
|
|
425
|
+
* the place of a state or -1; `measured()`, the index of the measured state or null.
|
|
426
|
+
*/
|
|
427
|
+
export function judgeCall(call, ctx) {
|
|
428
|
+
const [progressPath, planPath] = ['progress', 'plan'].map((name) => `${ctx.tasksDir}/${ctx.branch}/${name}.md`);
|
|
429
|
+
if (call.tool === 'Bash') return judgeShell(call.command ?? '', ctx);
|
|
430
|
+
const path = call.path ?? '';
|
|
431
|
+
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`';
|
|
432
|
+
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';
|
|
433
|
+
if (path === progressPath) return judgeProgress(call, ctx, planPath);
|
|
434
|
+
if (path.startsWith(`${ctx.plansDir}/`) && path.endsWith('.md')) return judgeDefer(call, ctx);
|
|
435
|
+
if (path === ctx.overridePath) return judgeOverride(call, ctx);
|
|
436
|
+
if (ctx.protectedPaths.some((source) => new RegExp(source).test(path))) return judgeProtected(path, ctx, planPath);
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function judgeProgress(call, ctx, planPath) {
|
|
441
|
+
const before = ctx.read(call.path);
|
|
442
|
+
const after = textAfter(call, before);
|
|
443
|
+
const order = marksOutOfOrder(after);
|
|
444
|
+
if (order) return `${order}: done steps go first, and the one current step right after them`;
|
|
445
|
+
// The plan as the commit that added it wrote it: a plan stripped of its checks in the working copy
|
|
446
|
+
// lifts nothing. Before that commit there is no approved plan, and the working copy is all there is.
|
|
447
|
+
const plan = (ctx.committed ? ctx.committed(planPath) : null) ?? ctx.read(planPath);
|
|
448
|
+
const legacy = ctx.legacy.includes(ctx.branch) || stepsOfPlan(plan ?? '').every((step) => step.command === null);
|
|
449
|
+
const marked = legacy ? [] : newlyDone(before, after);
|
|
450
|
+
if (marked.length > 0) 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`;
|
|
451
|
+
const state = declaredState(after);
|
|
452
|
+
const [declared, measured] = state && state !== declaredState(before) ? [ctx.stateIndex(state), ctx.measured()] : [-1, null];
|
|
453
|
+
if (declared >= 0 && measured !== null && declared > measured) {
|
|
454
|
+
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`;
|
|
455
|
+
}
|
|
456
|
+
const replaced = replacementChanges(before, after);
|
|
457
|
+
if (replaced.length > 0) {
|
|
458
|
+
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`;
|
|
459
|
+
}
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function judgeDefer(call, ctx) {
|
|
464
|
+
const before = ctx.read(call.path);
|
|
465
|
+
const after = textAfter(call, before);
|
|
466
|
+
const changed = deferChanges(before, after, ctx.key);
|
|
467
|
+
if (changed.length === 0) {
|
|
468
|
+
const steps = ownerRowChanges(before, after, ctx.key);
|
|
469
|
+
if (steps.length === 0) return null;
|
|
470
|
+
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}»`;
|
|
471
|
+
}
|
|
472
|
+
const word = ctx.ownerLast() ?? '';
|
|
473
|
+
const missing = changed.filter((task) => !new RegExp(`${escape(ctx.key)}-${task}(?!\\d)`).test(word));
|
|
474
|
+
if (missing.length === 0) return null;
|
|
475
|
+
return `«${DEFERRED}» for ${missing.map((task) => `${ctx.key}-${task}`).join(', ')} is set and lifted only by the owner's last word naming that task`;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function judgeOverride(call, ctx) {
|
|
479
|
+
const before = ctx.read(call.path);
|
|
480
|
+
const { keys, reasons } = changedKeys(before, textAfter(call, before));
|
|
481
|
+
const word = ctx.ownerLast() ?? '';
|
|
482
|
+
const unnamed = keys.filter((key) => !word.includes(key));
|
|
483
|
+
if (keys.length === 0 || unnamed.length > 0) {
|
|
484
|
+
return `the override of the description is edited only by the owner's last word naming the key it changes${unnamed.length ? `: ${unnamed.join(', ')}` : ''}`;
|
|
485
|
+
}
|
|
486
|
+
const unquoted = reasons.filter((reason) => !word.includes(reason));
|
|
487
|
+
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`;
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function judgeProtected(path, ctx, planPath) {
|
|
492
|
+
const fault = ctx.legacy.includes(ctx.branch) ? null : ctx.approvalFault ? ctx.approvalFault() : ctx.approved() ? null : 'there is no approval mark';
|
|
493
|
+
if (!fault && namedBy(path, editedPaths(ctx.read(planPath)))) return null;
|
|
494
|
+
const why = fault ? ` — ${fault}: ask the owner again and run \`node tools/task-flow-script.mjs plan-red\`` : '';
|
|
495
|
+
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}`;
|
|
496
|
+
}
|