@webpieces/agent-workflow-rules 0.0.0-stage → 0.4.846
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 +23 -2
- package/package.json +45 -4
- package/src/adapters/agent-response.d.ts +5 -0
- package/src/adapters/agent-response.js +86 -0
- package/src/adapters/agent-response.js.map +1 -0
- package/src/adapters/codex-subagent-guard.d.ts +45 -0
- package/src/adapters/codex-subagent-guard.js +88 -0
- package/src/adapters/codex-subagent-guard.js.map +1 -0
- package/src/adapters/guards-hook.d.ts +3 -0
- package/src/adapters/guards-hook.js +30 -0
- package/src/adapters/guards-hook.js.map +1 -0
- package/src/adapters/hook-app-fixtures.d.ts +68 -0
- package/src/adapters/hook-app-fixtures.js +170 -0
- package/src/adapters/hook-app-fixtures.js.map +1 -0
- package/src/adapters/hook-core.d.ts +22 -0
- package/src/adapters/hook-core.js +359 -0
- package/src/adapters/hook-core.js.map +1 -0
- package/src/adapters/hook-pipeline-evaluator.d.ts +5 -0
- package/src/adapters/hook-pipeline-evaluator.js +18 -0
- package/src/adapters/hook-pipeline-evaluator.js.map +1 -0
- package/src/adapters/review-identity-stamper.d.ts +20 -0
- package/src/adapters/review-identity-stamper.js +34 -0
- package/src/adapters/review-identity-stamper.js.map +1 -0
- package/src/bin/codex-trust.d.ts +105 -0
- package/src/bin/codex-trust.js +205 -0
- package/src/bin/codex-trust.js.map +1 -0
- package/src/bin/dev-hook-install.d.ts +1 -0
- package/src/bin/dev-hook-install.js +80 -0
- package/src/bin/dev-hook-install.js.map +1 -0
- package/src/bin/dev-hook-uninstall.d.ts +1 -0
- package/src/bin/dev-hook-uninstall.js +45 -0
- package/src/bin/dev-hook-uninstall.js.map +1 -0
- package/src/bin/hook-registration.d.ts +429 -0
- package/src/bin/hook-registration.js +661 -0
- package/src/bin/hook-registration.js.map +1 -0
- package/src/bin/install-entry.d.ts +19 -0
- package/src/bin/install-entry.js +128 -0
- package/src/bin/install-entry.js.map +1 -0
- package/src/bin/l0-allowlist.d.ts +46 -0
- package/src/bin/l0-allowlist.js +527 -0
- package/src/bin/l0-allowlist.js.map +1 -0
- package/src/bin/l0-codex-read.d.ts +20 -0
- package/src/bin/l0-codex-read.js +108 -0
- package/src/bin/l0-codex-read.js.map +1 -0
- package/src/bin/l0-decide.d.ts +49 -0
- package/src/bin/l0-decide.js +90 -0
- package/src/bin/l0-decide.js.map +1 -0
- package/src/bin/l0-entry.d.ts +103 -0
- package/src/bin/l0-entry.js +98 -0
- package/src/bin/l0-entry.js.map +1 -0
- package/src/bin/l0-ignored-tools.d.ts +38 -0
- package/src/bin/l0-ignored-tools.js +44 -0
- package/src/bin/l0-ignored-tools.js.map +1 -0
- package/src/bin/managed-env.d.ts +46 -0
- package/src/bin/managed-env.js +50 -0
- package/src/bin/managed-env.js.map +1 -0
- package/src/bin/neighbour-hooks.d.ts +42 -0
- package/src/bin/neighbour-hooks.js +177 -0
- package/src/bin/neighbour-hooks.js.map +1 -0
- package/src/bin/reviewer-agent.d.ts +19 -0
- package/src/bin/reviewer-agent.js +135 -0
- package/src/bin/reviewer-agent.js.map +1 -0
- package/src/bin/settings-shape.d.ts +37 -0
- package/src/bin/settings-shape.js +17 -0
- package/src/bin/settings-shape.js.map +1 -0
- package/src/bin/setup-config.d.ts +20 -0
- package/src/bin/setup-config.js +371 -0
- package/src/bin/setup-config.js.map +1 -0
- package/src/bin/setup.d.ts +78 -0
- package/src/bin/setup.js +411 -0
- package/src/bin/setup.js.map +1 -0
- package/src/bin/shim-audit-log.d.ts +119 -0
- package/src/bin/shim-audit-log.js +286 -0
- package/src/bin/shim-audit-log.js.map +1 -0
- package/src/bin/shim-deny-reason.d.ts +1 -0
- package/src/bin/shim-deny-reason.js +244 -0
- package/src/bin/shim-deny-reason.js.map +1 -0
- package/src/bin/shim-drift-fix.d.ts +1 -0
- package/src/bin/shim-drift-fix.js +73 -0
- package/src/bin/shim-drift-fix.js.map +1 -0
- package/src/bin/shim-testkit.d.ts +107 -0
- package/src/bin/shim-testkit.js +197 -0
- package/src/bin/shim-testkit.js.map +1 -0
- package/src/bin/shim-version-drift.d.ts +1 -0
- package/src/bin/shim-version-drift.js +164 -0
- package/src/bin/shim-version-drift.js.map +1 -0
- package/src/bin/shim.d.ts +16 -0
- package/src/bin/shim.js +641 -0
- package/src/bin/shim.js.map +1 -0
- package/src/bin/upgrade-shim.d.ts +18 -0
- package/src/bin/upgrade-shim.js +283 -0
- package/src/bin/upgrade-shim.js.map +1 -0
- package/src/configs/main-sync-guard-configs.d.ts +38 -0
- package/src/configs/main-sync-guard-configs.js +54 -0
- package/src/configs/main-sync-guard-configs.js.map +1 -0
- package/src/configs/rule-configs.d.ts +47 -0
- package/src/configs/rule-configs.js +96 -0
- package/src/configs/rule-configs.js.map +1 -0
- package/src/core/ai-type-context.d.ts +36 -0
- package/src/core/ai-type-context.js +45 -0
- package/src/core/ai-type-context.js.map +1 -0
- package/src/core/decision-log.d.ts +190 -0
- package/src/core/decision-log.js +409 -0
- package/src/core/decision-log.js.map +1 -0
- package/src/core/excluded-path-escape.d.ts +101 -0
- package/src/core/excluded-path-escape.js +236 -0
- package/src/core/excluded-path-escape.js.map +1 -0
- package/src/core/force-to-root.d.ts +32 -0
- package/src/core/force-to-root.js +60 -0
- package/src/core/force-to-root.js.map +1 -0
- package/src/core/guard-index-doc.d.ts +63 -0
- package/src/core/guard-index-doc.js +172 -0
- package/src/core/guard-index-doc.js.map +1 -0
- package/src/core/instruct-ai-writer.d.ts +1 -0
- package/src/core/instruct-ai-writer.js +6 -0
- package/src/core/instruct-ai-writer.js.map +1 -0
- package/src/core/l0-matrix.d.ts +102 -0
- package/src/core/l0-matrix.js +357 -0
- package/src/core/l0-matrix.js.map +1 -0
- package/src/core/l0-tooling-doc.d.ts +45 -0
- package/src/core/l0-tooling-doc.js +258 -0
- package/src/core/l0-tooling-doc.js.map +1 -0
- package/src/core/l1-doc.d.ts +7 -0
- package/src/core/l1-doc.js +340 -0
- package/src/core/l1-doc.js.map +1 -0
- package/src/core/l1-matrix-doc.d.ts +31 -0
- package/src/core/l1-matrix-doc.js +111 -0
- package/src/core/l1-matrix-doc.js.map +1 -0
- package/src/core/l1-rows.d.ts +167 -0
- package/src/core/l1-rows.js +262 -0
- package/src/core/l1-rows.js.map +1 -0
- package/src/core/l2-doc.d.ts +7 -0
- package/src/core/l2-doc.js +385 -0
- package/src/core/l2-doc.js.map +1 -0
- package/src/core/l2-matrix-doc.d.ts +23 -0
- package/src/core/l2-matrix-doc.js +72 -0
- package/src/core/l2-matrix-doc.js.map +1 -0
- package/src/core/l2-row.d.ts +54 -0
- package/src/core/l2-row.js +75 -0
- package/src/core/l2-row.js.map +1 -0
- package/src/core/l2-rows.d.ts +56 -0
- package/src/core/l2-rows.js +272 -0
- package/src/core/l2-rows.js.map +1 -0
- package/src/core/load-rules.d.ts +9 -0
- package/src/core/load-rules.js +65 -0
- package/src/core/load-rules.js.map +1 -0
- package/src/core/log-stream.d.ts +117 -0
- package/src/core/log-stream.js +156 -0
- package/src/core/log-stream.js.map +1 -0
- package/src/core/log-streams.d.ts +48 -0
- package/src/core/log-streams.js +55 -0
- package/src/core/log-streams.js.map +1 -0
- package/src/core/log-target.d.ts +21 -0
- package/src/core/log-target.js +29 -0
- package/src/core/log-target.js.map +1 -0
- package/src/core/main-sync-log.d.ts +16 -0
- package/src/core/main-sync-log.js +108 -0
- package/src/core/main-sync-log.js.map +1 -0
- package/src/core/main-sync-refresh.d.ts +39 -0
- package/src/core/main-sync-refresh.js +96 -0
- package/src/core/main-sync-refresh.js.map +1 -0
- package/src/core/main-sync-timeout.d.ts +33 -0
- package/src/core/main-sync-timeout.js +95 -0
- package/src/core/main-sync-timeout.js.map +1 -0
- package/src/core/matrix-cures.d.ts +12 -0
- package/src/core/matrix-cures.js +56 -0
- package/src/core/matrix-cures.js.map +1 -0
- package/src/core/missing-directory.d.ts +38 -0
- package/src/core/missing-directory.js +66 -0
- package/src/core/missing-directory.js.map +1 -0
- package/src/core/read-only-inspection.d.ts +34 -0
- package/src/core/read-only-inspection.js +88 -0
- package/src/core/read-only-inspection.js.map +1 -0
- package/src/core/rejection-log.d.ts +9 -0
- package/src/core/rejection-log.js +262 -0
- package/src/core/rejection-log.js.map +1 -0
- package/src/core/rules/branch-creation-guard.d.ts +113 -0
- package/src/core/rules/branch-creation-guard.js +425 -0
- package/src/core/rules/branch-creation-guard.js.map +1 -0
- package/src/core/rules/branch-switch-scan.d.ts +43 -0
- package/src/core/rules/branch-switch-scan.js +132 -0
- package/src/core/rules/branch-switch-scan.js.map +1 -0
- package/src/core/rules/build-output-pipe-guard.d.ts +56 -0
- package/src/core/rules/build-output-pipe-guard.js +120 -0
- package/src/core/rules/build-output-pipe-guard.js.map +1 -0
- package/src/core/rules/build-output-pipe-scan.d.ts +47 -0
- package/src/core/rules/build-output-pipe-scan.js +84 -0
- package/src/core/rules/build-output-pipe-scan.js.map +1 -0
- package/src/core/rules/cap-remedies.d.ts +39 -0
- package/src/core/rules/cap-remedies.js +59 -0
- package/src/core/rules/cap-remedies.js.map +1 -0
- package/src/core/rules/commit-message-substitution-guard.d.ts +72 -0
- package/src/core/rules/commit-message-substitution-guard.js +134 -0
- package/src/core/rules/commit-message-substitution-guard.js.map +1 -0
- package/src/core/rules/commit-message-substitution-scan.d.ts +42 -0
- package/src/core/rules/commit-message-substitution-scan.js +145 -0
- package/src/core/rules/commit-message-substitution-scan.js.map +1 -0
- package/src/core/rules/content-read-scan.d.ts +76 -0
- package/src/core/rules/content-read-scan.js +176 -0
- package/src/core/rules/content-read-scan.js.map +1 -0
- package/src/core/rules/cure-prefix-scan.d.ts +64 -0
- package/src/core/rules/cure-prefix-scan.js +133 -0
- package/src/core/rules/cure-prefix-scan.js.map +1 -0
- package/src/core/rules/feature-branch-guard.d.ts +121 -0
- package/src/core/rules/feature-branch-guard.js +327 -0
- package/src/core/rules/feature-branch-guard.js.map +1 -0
- package/src/core/rules/index.d.ts +1 -0
- package/src/core/rules/index.js +5 -0
- package/src/core/rules/index.js.map +1 -0
- package/src/core/rules/judged-tree.d.ts +66 -0
- package/src/core/rules/judged-tree.js +97 -0
- package/src/core/rules/judged-tree.js.map +1 -0
- package/src/core/rules/main-freshness.d.ts +37 -0
- package/src/core/rules/main-freshness.js +61 -0
- package/src/core/rules/main-freshness.js.map +1 -0
- package/src/core/rules/merge-in-progress-guard.d.ts +17 -0
- package/src/core/rules/merge-in-progress-guard.js +124 -0
- package/src/core/rules/merge-in-progress-guard.js.map +1 -0
- package/src/core/rules/merged-branch-bash-guard.d.ts +62 -0
- package/src/core/rules/merged-branch-bash-guard.js +171 -0
- package/src/core/rules/merged-branch-bash-guard.js.map +1 -0
- package/src/core/rules/merged-branch-message.d.ts +59 -0
- package/src/core/rules/merged-branch-message.js +137 -0
- package/src/core/rules/merged-branch-message.js.map +1 -0
- package/src/core/rules/pr-creation-or-push-guard.d.ts +26 -0
- package/src/core/rules/pr-creation-or-push-guard.js +150 -0
- package/src/core/rules/pr-creation-or-push-guard.js.map +1 -0
- package/src/core/rules/pr-merge-guard.d.ts +36 -0
- package/src/core/rules/pr-merge-guard.js +87 -0
- package/src/core/rules/pr-merge-guard.js.map +1 -0
- package/src/core/rules/read-stale-guard.d.ts +136 -0
- package/src/core/rules/read-stale-guard.js +328 -0
- package/src/core/rules/read-stale-guard.js.map +1 -0
- package/src/core/rules/recovery-allowlist.d.ts +49 -0
- package/src/core/rules/recovery-allowlist.js +187 -0
- package/src/core/rules/recovery-allowlist.js.map +1 -0
- package/src/core/rules/redirect-how-to-merge-main.d.ts +16 -0
- package/src/core/rules/redirect-how-to-merge-main.js +159 -0
- package/src/core/rules/redirect-how-to-merge-main.js.map +1 -0
- package/src/core/rules/stale-main-bash-guard.d.ts +198 -0
- package/src/core/rules/stale-main-bash-guard.js +363 -0
- package/src/core/rules/stale-main-bash-guard.js.map +1 -0
- package/src/core/rules/stale-main-message.d.ts +62 -0
- package/src/core/rules/stale-main-message.js +102 -0
- package/src/core/rules/stale-main-message.js.map +1 -0
- package/src/core/rules/tree-recovery.d.ts +92 -0
- package/src/core/rules/tree-recovery.js +165 -0
- package/src/core/rules/tree-recovery.js.map +1 -0
- package/src/core/rules/wait-spin-guard.d.ts +128 -0
- package/src/core/rules/wait-spin-guard.js +211 -0
- package/src/core/rules/wait-spin-guard.js.map +1 -0
- package/src/core/rules/wait-spin-scan.d.ts +118 -0
- package/src/core/rules/wait-spin-scan.js +203 -0
- package/src/core/rules/wait-spin-scan.js.map +1 -0
- package/src/core/rules/whole-repo-build-guard.d.ts +125 -0
- package/src/core/rules/whole-repo-build-guard.js +257 -0
- package/src/core/rules/whole-repo-build-guard.js.map +1 -0
- package/src/core/rules/whole-repo-build-scan.d.ts +28 -0
- package/src/core/rules/whole-repo-build-scan.js +198 -0
- package/src/core/rules/whole-repo-build-scan.js.map +1 -0
- package/src/core/runner.d.ts +19 -0
- package/src/core/runner.js +482 -0
- package/src/core/runner.js.map +1 -0
- package/src/core/session-call-history.d.ts +44 -0
- package/src/core/session-call-history.js +79 -0
- package/src/core/session-call-history.js.map +1 -0
- package/src/core/sync-main.d.ts +46 -0
- package/src/core/sync-main.js +176 -0
- package/src/core/sync-main.js.map +1 -0
- package/src/core/version-sync.d.ts +219 -0
- package/src/core/version-sync.js +558 -0
- package/src/core/version-sync.js.map +1 -0
- package/src/core/webpieces-versions.d.ts +98 -0
- package/src/core/webpieces-versions.js +206 -0
- package/src/core/webpieces-versions.js.map +1 -0
- package/src/core/workflow-hook-rules.d.ts +5 -0
- package/src/core/workflow-hook-rules.js +15 -0
- package/src/core/workflow-hook-rules.js.map +1 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +28 -0
- package/src/index.js.map +1 -0
- package/src/policy-keys.d.ts +2 -0
- package/src/policy-keys.js +6 -0
- package/src/policy-keys.js.map +1 -0
- package/src/rule-migrations.d.ts +3 -0
- package/src/rule-migrations.js +22 -0
- package/src/rule-migrations.js.map +1 -0
- package/src/rule-pack.d.ts +2 -0
- package/src/rule-pack.js +20 -0
- package/src/rule-pack.js.map +1 -0
- package/src/rule-settings.d.ts +5 -0
- package/src/rule-settings.js +31 -0
- package/src/rule-settings.js.map +1 -0
- package/src/safeguard-catalog.d.ts +3 -0
- package/src/safeguard-catalog.js +16 -0
- package/src/safeguard-catalog.js.map +1 -0
- package/templates/ai-hook.sh +497 -0
- package/templates/claude-settings-hook.json +28 -0
- package/templates/webpieces-reviewer.md +60 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { BashContext, Violation } from '@webpieces/hook-runtime';
|
|
2
|
+
import { BashRuleBase, EmptyRuleConfig } from '@webpieces/hook-runtime';
|
|
3
|
+
import { FixHint } from '@webpieces/hook-runtime';
|
|
4
|
+
/**
|
|
5
|
+
* Blocks `git commit` whose message is passed INLINE (`-m` / `--message` / `-am`) when that message
|
|
6
|
+
* contains a backtick, a `$(`, or a newline — and hands back `git commit -F <file>` instead.
|
|
7
|
+
*
|
|
8
|
+
* ─── The incident: a commit message that HUNG for ten minutes, twice ───────────────────────────────
|
|
9
|
+
* An agent ran `git commit -q -m "…"` with a multi-paragraph message that happened to contain the
|
|
10
|
+
* sentence: *"… `strings` on a .app built with --port 8084 contains no 8084."* The backticks are
|
|
11
|
+
* COMMAND SUBSTITUTION. The shell ran `strings` with no arguments, `strings` read stdin, and stdin
|
|
12
|
+
* never closed. The Bash tool SIGTERM'd it at its ten-minute cap. The retry kept the same sentence and
|
|
13
|
+
* did it again — twenty minutes for one commit.
|
|
14
|
+
*
|
|
15
|
+
* ─── Why this is a GUARD and not a doc line ────────────────────────────────────────────────────────
|
|
16
|
+
* The agent then diagnosed it as "a guard caught a commit message that quoting a blocked command",
|
|
17
|
+
* citing an unrelated deploy-guard note. Three facts settle that it was nothing of the kind, and they
|
|
18
|
+
* are worth writing down because they are the reason the cure has to arrive BEFORE execution:
|
|
19
|
+
*
|
|
20
|
+
* - a PreToolUse guard denies INSTANTLY, before the command runs. This ran for ten minutes.
|
|
21
|
+
* - `Exit code 143` is SIGTERM delivered to a RUNNING process, not a refusal.
|
|
22
|
+
* - the first attempt was `git add -A && git commit -m "…"`, and afterwards the 45 files were
|
|
23
|
+
* STAGED — so the command really executed, and then blocked while expanding its second word.
|
|
24
|
+
*
|
|
25
|
+
* A footnote in a doc could not have reached that agent at that moment; a refusal naming `-F` does.
|
|
26
|
+
*
|
|
27
|
+
* ─── The trigger is the METACHARACTER, never the words ─────────────────────────────────────────────
|
|
28
|
+
* A message is PROSE, and prose goes through the shell's expansion rules whether or not it is meant
|
|
29
|
+
* to. A blocklist of "dangerous commands" would have missed this entirely — `strings` is an ordinary
|
|
30
|
+
* word, and the next hang will name an ordinary word too. So the rule matches backtick, `$(` and a
|
|
31
|
+
* newline inside the message argument, and reads nothing at all into the surrounding sentence.
|
|
32
|
+
*
|
|
33
|
+
* ─── It fires on a SINGLE-QUOTED message too, and that is deliberate ───────────────────────────────
|
|
34
|
+
* `git commit -m 'a `backtick` here'` is, strictly, safe: single quotes suppress substitution, so the
|
|
35
|
+
* shell would pass those backticks through literally. This guard blocks it anyway, and the false
|
|
36
|
+
* positive is the point rather than an oversight — do not "fix" it. Two reasons:
|
|
37
|
+
*
|
|
38
|
+
* - single-quoting PROSE is fragile in the one way that matters. An English sentence eventually
|
|
39
|
+
* contains an apostrophe ("doesn't", "the agent's"), which CLOSES the quote mid-message and drops
|
|
40
|
+
* the rest of the sentence back into the shell's syntax — the exact state this guard exists to
|
|
41
|
+
* prevent, reached by a message that looked safe when it was written.
|
|
42
|
+
* - the cure is free. `-F` costs one Write tool call and works for every message, so being wrong
|
|
43
|
+
* here spends seconds, while being right saves twenty minutes. A guard chooses its errors by what
|
|
44
|
+
* each one COSTS, and these two costs are not close.
|
|
45
|
+
*
|
|
46
|
+
* `blocks a single-quoted message too` in the spec pins this, so a later reader meets the decision as
|
|
47
|
+
* a failing test rather than as a bug report.
|
|
48
|
+
*
|
|
49
|
+
* ─── It matches the RAW command, and that is load-bearing ──────────────────────────────────────────
|
|
50
|
+
* Every other bash guard here matches `ctx.commandCode`, which STRIPS heredoc bodies and quoted prose
|
|
51
|
+
* precisely so a commit message merely MENTIONING `git push` is not read as a push. That stripping
|
|
52
|
+
* deletes exactly the span this rule exists to inspect: on `commandCode` the message is already gone,
|
|
53
|
+
* and the guard would be permanently blind. So it reads `ctx.command`.
|
|
54
|
+
*
|
|
55
|
+
* That is safe here because the rule is still blocklist-shaped and its cure can never itself be
|
|
56
|
+
* blocked: `-F` writes the message to a file and passes a PATH, so nothing about the message text can
|
|
57
|
+
* make the replacement command match this guard. There is no input for which an agent is left with no
|
|
58
|
+
* accepted spelling.
|
|
59
|
+
*/
|
|
60
|
+
export declare class CommitMessageSubstitutionGuardRule extends BashRuleBase<EmptyRuleConfig> {
|
|
61
|
+
constructor();
|
|
62
|
+
private readonly scan;
|
|
63
|
+
readonly description: string;
|
|
64
|
+
get fixHint(): FixHint;
|
|
65
|
+
check(ctx: BashContext): readonly Violation[];
|
|
66
|
+
private allow;
|
|
67
|
+
private block;
|
|
68
|
+
private message;
|
|
69
|
+
private what;
|
|
70
|
+
private truncate;
|
|
71
|
+
private logDecision;
|
|
72
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CommitMessageSubstitutionGuardRule = void 0;
|
|
4
|
+
const rules_config_1 = require("@webpieces/rules-config");
|
|
5
|
+
const hook_runtime_1 = require("@webpieces/hook-runtime");
|
|
6
|
+
const hook_runtime_2 = require("@webpieces/hook-runtime");
|
|
7
|
+
const hook_runtime_3 = require("@webpieces/hook-runtime");
|
|
8
|
+
const rules_config_2 = require("@webpieces/rules-config");
|
|
9
|
+
const decision_log_1 = require("../decision-log");
|
|
10
|
+
const hook_runtime_4 = require("@webpieces/hook-runtime");
|
|
11
|
+
const commit_message_substitution_scan_1 = require("./commit-message-substitution-scan");
|
|
12
|
+
/**
|
|
13
|
+
* Blocks `git commit` whose message is passed INLINE (`-m` / `--message` / `-am`) when that message
|
|
14
|
+
* contains a backtick, a `$(`, or a newline — and hands back `git commit -F <file>` instead.
|
|
15
|
+
*
|
|
16
|
+
* ─── The incident: a commit message that HUNG for ten minutes, twice ───────────────────────────────
|
|
17
|
+
* An agent ran `git commit -q -m "…"` with a multi-paragraph message that happened to contain the
|
|
18
|
+
* sentence: *"… `strings` on a .app built with --port 8084 contains no 8084."* The backticks are
|
|
19
|
+
* COMMAND SUBSTITUTION. The shell ran `strings` with no arguments, `strings` read stdin, and stdin
|
|
20
|
+
* never closed. The Bash tool SIGTERM'd it at its ten-minute cap. The retry kept the same sentence and
|
|
21
|
+
* did it again — twenty minutes for one commit.
|
|
22
|
+
*
|
|
23
|
+
* ─── Why this is a GUARD and not a doc line ────────────────────────────────────────────────────────
|
|
24
|
+
* The agent then diagnosed it as "a guard caught a commit message that quoting a blocked command",
|
|
25
|
+
* citing an unrelated deploy-guard note. Three facts settle that it was nothing of the kind, and they
|
|
26
|
+
* are worth writing down because they are the reason the cure has to arrive BEFORE execution:
|
|
27
|
+
*
|
|
28
|
+
* - a PreToolUse guard denies INSTANTLY, before the command runs. This ran for ten minutes.
|
|
29
|
+
* - `Exit code 143` is SIGTERM delivered to a RUNNING process, not a refusal.
|
|
30
|
+
* - the first attempt was `git add -A && git commit -m "…"`, and afterwards the 45 files were
|
|
31
|
+
* STAGED — so the command really executed, and then blocked while expanding its second word.
|
|
32
|
+
*
|
|
33
|
+
* A footnote in a doc could not have reached that agent at that moment; a refusal naming `-F` does.
|
|
34
|
+
*
|
|
35
|
+
* ─── The trigger is the METACHARACTER, never the words ─────────────────────────────────────────────
|
|
36
|
+
* A message is PROSE, and prose goes through the shell's expansion rules whether or not it is meant
|
|
37
|
+
* to. A blocklist of "dangerous commands" would have missed this entirely — `strings` is an ordinary
|
|
38
|
+
* word, and the next hang will name an ordinary word too. So the rule matches backtick, `$(` and a
|
|
39
|
+
* newline inside the message argument, and reads nothing at all into the surrounding sentence.
|
|
40
|
+
*
|
|
41
|
+
* ─── It fires on a SINGLE-QUOTED message too, and that is deliberate ───────────────────────────────
|
|
42
|
+
* `git commit -m 'a `backtick` here'` is, strictly, safe: single quotes suppress substitution, so the
|
|
43
|
+
* shell would pass those backticks through literally. This guard blocks it anyway, and the false
|
|
44
|
+
* positive is the point rather than an oversight — do not "fix" it. Two reasons:
|
|
45
|
+
*
|
|
46
|
+
* - single-quoting PROSE is fragile in the one way that matters. An English sentence eventually
|
|
47
|
+
* contains an apostrophe ("doesn't", "the agent's"), which CLOSES the quote mid-message and drops
|
|
48
|
+
* the rest of the sentence back into the shell's syntax — the exact state this guard exists to
|
|
49
|
+
* prevent, reached by a message that looked safe when it was written.
|
|
50
|
+
* - the cure is free. `-F` costs one Write tool call and works for every message, so being wrong
|
|
51
|
+
* here spends seconds, while being right saves twenty minutes. A guard chooses its errors by what
|
|
52
|
+
* each one COSTS, and these two costs are not close.
|
|
53
|
+
*
|
|
54
|
+
* `blocks a single-quoted message too` in the spec pins this, so a later reader meets the decision as
|
|
55
|
+
* a failing test rather than as a bug report.
|
|
56
|
+
*
|
|
57
|
+
* ─── It matches the RAW command, and that is load-bearing ──────────────────────────────────────────
|
|
58
|
+
* Every other bash guard here matches `ctx.commandCode`, which STRIPS heredoc bodies and quoted prose
|
|
59
|
+
* precisely so a commit message merely MENTIONING `git push` is not read as a push. That stripping
|
|
60
|
+
* deletes exactly the span this rule exists to inspect: on `commandCode` the message is already gone,
|
|
61
|
+
* and the guard would be permanently blind. So it reads `ctx.command`.
|
|
62
|
+
*
|
|
63
|
+
* That is safe here because the rule is still blocklist-shaped and its cure can never itself be
|
|
64
|
+
* blocked: `-F` writes the message to a file and passes a PATH, so nothing about the message text can
|
|
65
|
+
* make the replacement command match this guard. There is no input for which an agent is left with no
|
|
66
|
+
* accepted spelling.
|
|
67
|
+
*/
|
|
68
|
+
class CommitMessageSubstitutionGuardRule extends hook_runtime_2.BashRuleBase {
|
|
69
|
+
constructor() {
|
|
70
|
+
// configKey === name and is DELIBERATELY not a real webpieces.config.json key. Like
|
|
71
|
+
// whole-repo-build-guard, this guard is loaded OUTSIDE the config-driven set, so the fault-Y
|
|
72
|
+
// config-sync check ("every built-in rule needs an entry, or every Bash call is blocked") can
|
|
73
|
+
// never see it — a new guard that every consumer must configure to avoid being blocked is a
|
|
74
|
+
// guard that ships an outage on upgrade, which has happened here once already.
|
|
75
|
+
super(new hook_runtime_2.EmptyRuleConfig(), 'commit-message-substitution-guard', 'commit-message-substitution-guard');
|
|
76
|
+
}
|
|
77
|
+
scan = new commit_message_substitution_scan_1.CommitMessageSubstitutionScan(new hook_runtime_4.CommandScanner());
|
|
78
|
+
description = 'Block `git commit -m` whose inline message contains a backtick, `$(` or a newline — the shell ' +
|
|
79
|
+
'expands those before git sees them — and name `git commit -F` instead.';
|
|
80
|
+
get fixHint() {
|
|
81
|
+
return new hook_runtime_3.FixHint('A commit message passed with -m goes through the shell. A backtick or $( in it is COMMAND ' +
|
|
82
|
+
'SUBSTITUTION, and a substituted command that reads stdin hangs until the tool times out.', 'Keep the message text out of the shell\'s expansion path — pass a PATH, not prose:', [
|
|
83
|
+
new rules_config_1.Option('Write the message to a file with the Write tool, then:\n' +
|
|
84
|
+
'git commit -F /tmp/commit-msg.txt', true),
|
|
85
|
+
new rules_config_1.Option('Feed it on stdin from a QUOTED heredoc:\n' +
|
|
86
|
+
'git commit -F - <<\'EOF\'\n' +
|
|
87
|
+
'<your message>\n' +
|
|
88
|
+
'EOF\n' +
|
|
89
|
+
'The quotes around EOF are what disable substitution. An unquoted <<EOF does NOT — ' +
|
|
90
|
+
'the body is still expanded, and you are back where you started.'),
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
check(ctx) {
|
|
94
|
+
// RAW, not commandCode — see the class docstring. commandCode strips the quoted prose this
|
|
95
|
+
// rule's entire subject is, so matching on it would make the guard permanently blind.
|
|
96
|
+
const hit = this.scan.firstHit(ctx.command);
|
|
97
|
+
if (hit === null)
|
|
98
|
+
return this.allow(ctx, 'no-inline-message-hazard');
|
|
99
|
+
return this.block(ctx, hit);
|
|
100
|
+
}
|
|
101
|
+
allow(ctx, reason) {
|
|
102
|
+
this.logDecision(ctx, 'ALLOW', reason);
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
block(ctx, hit) {
|
|
106
|
+
// BLOCK_AI_CURE: the cure is two commands the agent runs itself, both spelled out in fixHint.
|
|
107
|
+
this.logDecision(ctx, 'BLOCK_AI_CURE', `inline-message-${hit.kind}`);
|
|
108
|
+
return [new hook_runtime_1.Violation(1, this.truncate(ctx.command), this.message(hit))];
|
|
109
|
+
}
|
|
110
|
+
// Short on purpose: it is read mid-task by an agent that needs the ONE next move.
|
|
111
|
+
message(hit) {
|
|
112
|
+
return `Blocked: the ${hit.flag} message contains ${this.what(hit.kind)}, which the SHELL acts on `
|
|
113
|
+
+ 'before git ever sees the message.\n\n'
|
|
114
|
+
+ ` ${hit.excerpt}\n\n`
|
|
115
|
+
+ 'Write the message to a file with the Write tool and commit with `git commit -F <file>`, or '
|
|
116
|
+
+ 'pipe it in with a QUOTED heredoc: `git commit -F - <<\'EOF\'`.';
|
|
117
|
+
}
|
|
118
|
+
what(kind) {
|
|
119
|
+
if (kind === 'backtick')
|
|
120
|
+
return 'a BACKTICK (command substitution — `cmd` runs cmd)';
|
|
121
|
+
if (kind === 'command-substitution')
|
|
122
|
+
return 'a `$(` (command substitution)';
|
|
123
|
+
return 'a NEWLINE (a multi-paragraph message belongs in a file — that is where the backticks hide)';
|
|
124
|
+
}
|
|
125
|
+
truncate(s) {
|
|
126
|
+
const MAX = 120;
|
|
127
|
+
return s.length <= MAX ? s : s.slice(0, MAX) + '…';
|
|
128
|
+
}
|
|
129
|
+
logDecision(ctx, verdict, reason) {
|
|
130
|
+
(0, decision_log_1.logGuardDecision)(ctx.workspaceRoot, new decision_log_1.GuardDecision('commit-message-substitution-guard', 'Bash', ctx.command, '-', verdict, reason, '-', rules_config_2.L0_FAULT_NONE, decision_log_1.MATRIX_L2_UNROWED));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
exports.CommitMessageSubstitutionGuardRule = CommitMessageSubstitutionGuardRule;
|
|
134
|
+
//# sourceMappingURL=commit-message-substitution-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commit-message-substitution-guard.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/agent-workflow-rules/src/core/rules/commit-message-substitution-guard.ts"],"names":[],"mappings":"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","sourcesContent":["import { Option } from '@webpieces/rules-config';\n\nimport type { BashContext, Violation } from '@webpieces/hook-runtime';\nimport { Violation as V } from '@webpieces/hook-runtime';\nimport { BashRuleBase, EmptyRuleConfig } from '@webpieces/hook-runtime';\nimport { FixHint } from '@webpieces/hook-runtime';\nimport { L0_FAULT_NONE } from '@webpieces/rules-config';\nimport { logGuardDecision, GuardDecision, Verdict, MATRIX_L2_UNROWED } from '../decision-log';\nimport { CommandScanner } from '@webpieces/hook-runtime';\nimport { CommitMessageSubstitutionScan, MessageHazard, MessageHazardKind } from './commit-message-substitution-scan';\n\n/**\n * Blocks `git commit` whose message is passed INLINE (`-m` / `--message` / `-am`) when that message\n * contains a backtick, a `$(`, or a newline — and hands back `git commit -F <file>` instead.\n *\n * ─── The incident: a commit message that HUNG for ten minutes, twice ───────────────────────────────\n * An agent ran `git commit -q -m \"…\"` with a multi-paragraph message that happened to contain the\n * sentence: *\"… `strings` on a .app built with --port 8084 contains no 8084.\"* The backticks are\n * COMMAND SUBSTITUTION. The shell ran `strings` with no arguments, `strings` read stdin, and stdin\n * never closed. The Bash tool SIGTERM'd it at its ten-minute cap. The retry kept the same sentence and\n * did it again — twenty minutes for one commit.\n *\n * ─── Why this is a GUARD and not a doc line ────────────────────────────────────────────────────────\n * The agent then diagnosed it as \"a guard caught a commit message that quoting a blocked command\",\n * citing an unrelated deploy-guard note. Three facts settle that it was nothing of the kind, and they\n * are worth writing down because they are the reason the cure has to arrive BEFORE execution:\n *\n * - a PreToolUse guard denies INSTANTLY, before the command runs. This ran for ten minutes.\n * - `Exit code 143` is SIGTERM delivered to a RUNNING process, not a refusal.\n * - the first attempt was `git add -A && git commit -m \"…\"`, and afterwards the 45 files were\n * STAGED — so the command really executed, and then blocked while expanding its second word.\n *\n * A footnote in a doc could not have reached that agent at that moment; a refusal naming `-F` does.\n *\n * ─── The trigger is the METACHARACTER, never the words ─────────────────────────────────────────────\n * A message is PROSE, and prose goes through the shell's expansion rules whether or not it is meant\n * to. A blocklist of \"dangerous commands\" would have missed this entirely — `strings` is an ordinary\n * word, and the next hang will name an ordinary word too. So the rule matches backtick, `$(` and a\n * newline inside the message argument, and reads nothing at all into the surrounding sentence.\n *\n * ─── It fires on a SINGLE-QUOTED message too, and that is deliberate ───────────────────────────────\n * `git commit -m 'a `backtick` here'` is, strictly, safe: single quotes suppress substitution, so the\n * shell would pass those backticks through literally. This guard blocks it anyway, and the false\n * positive is the point rather than an oversight — do not \"fix\" it. Two reasons:\n *\n * - single-quoting PROSE is fragile in the one way that matters. An English sentence eventually\n * contains an apostrophe (\"doesn't\", \"the agent's\"), which CLOSES the quote mid-message and drops\n * the rest of the sentence back into the shell's syntax — the exact state this guard exists to\n * prevent, reached by a message that looked safe when it was written.\n * - the cure is free. `-F` costs one Write tool call and works for every message, so being wrong\n * here spends seconds, while being right saves twenty minutes. A guard chooses its errors by what\n * each one COSTS, and these two costs are not close.\n *\n * `blocks a single-quoted message too` in the spec pins this, so a later reader meets the decision as\n * a failing test rather than as a bug report.\n *\n * ─── It matches the RAW command, and that is load-bearing ──────────────────────────────────────────\n * Every other bash guard here matches `ctx.commandCode`, which STRIPS heredoc bodies and quoted prose\n * precisely so a commit message merely MENTIONING `git push` is not read as a push. That stripping\n * deletes exactly the span this rule exists to inspect: on `commandCode` the message is already gone,\n * and the guard would be permanently blind. So it reads `ctx.command`.\n *\n * That is safe here because the rule is still blocklist-shaped and its cure can never itself be\n * blocked: `-F` writes the message to a file and passes a PATH, so nothing about the message text can\n * make the replacement command match this guard. There is no input for which an agent is left with no\n * accepted spelling.\n */\nexport class CommitMessageSubstitutionGuardRule extends BashRuleBase<EmptyRuleConfig> {\n constructor() {\n // configKey === name and is DELIBERATELY not a real webpieces.config.json key. Like\n // whole-repo-build-guard, this guard is loaded OUTSIDE the config-driven set, so the fault-Y\n // config-sync check (\"every built-in rule needs an entry, or every Bash call is blocked\") can\n // never see it — a new guard that every consumer must configure to avoid being blocked is a\n // guard that ships an outage on upgrade, which has happened here once already.\n super(new EmptyRuleConfig(), 'commit-message-substitution-guard', 'commit-message-substitution-guard');\n }\n\n private readonly scan = new CommitMessageSubstitutionScan(new CommandScanner());\n\n readonly description =\n 'Block `git commit -m` whose inline message contains a backtick, `$(` or a newline — the shell ' +\n 'expands those before git sees them — and name `git commit -F` instead.';\n\n get fixHint(): FixHint {\n return new FixHint(\n 'A commit message passed with -m goes through the shell. A backtick or $( in it is COMMAND ' +\n 'SUBSTITUTION, and a substituted command that reads stdin hangs until the tool times out.',\n 'Keep the message text out of the shell\\'s expansion path — pass a PATH, not prose:',\n [\n new Option(\n 'Write the message to a file with the Write tool, then:\\n' +\n 'git commit -F /tmp/commit-msg.txt',\n true,\n ),\n new Option(\n 'Feed it on stdin from a QUOTED heredoc:\\n' +\n 'git commit -F - <<\\'EOF\\'\\n' +\n '<your message>\\n' +\n 'EOF\\n' +\n 'The quotes around EOF are what disable substitution. An unquoted <<EOF does NOT — ' +\n 'the body is still expanded, and you are back where you started.',\n ),\n ],\n );\n }\n\n check(ctx: BashContext): readonly Violation[] {\n // RAW, not commandCode — see the class docstring. commandCode strips the quoted prose this\n // rule's entire subject is, so matching on it would make the guard permanently blind.\n const hit = this.scan.firstHit(ctx.command);\n if (hit === null) return this.allow(ctx, 'no-inline-message-hazard');\n return this.block(ctx, hit);\n }\n\n private allow(ctx: BashContext, reason: string): readonly Violation[] {\n this.logDecision(ctx, 'ALLOW', reason);\n return [];\n }\n\n private block(ctx: BashContext, hit: MessageHazard): readonly Violation[] {\n // BLOCK_AI_CURE: the cure is two commands the agent runs itself, both spelled out in fixHint.\n this.logDecision(ctx, 'BLOCK_AI_CURE', `inline-message-${hit.kind}`);\n return [new V(1, this.truncate(ctx.command), this.message(hit))];\n }\n\n // Short on purpose: it is read mid-task by an agent that needs the ONE next move.\n private message(hit: MessageHazard): string {\n return `Blocked: the ${hit.flag} message contains ${this.what(hit.kind)}, which the SHELL acts on `\n + 'before git ever sees the message.\\n\\n'\n + ` ${hit.excerpt}\\n\\n`\n + 'Write the message to a file with the Write tool and commit with `git commit -F <file>`, or '\n + 'pipe it in with a QUOTED heredoc: `git commit -F - <<\\'EOF\\'`.';\n }\n\n private what(kind: MessageHazardKind): string {\n if (kind === 'backtick') return 'a BACKTICK (command substitution — `cmd` runs cmd)';\n if (kind === 'command-substitution') return 'a `$(` (command substitution)';\n return 'a NEWLINE (a multi-paragraph message belongs in a file — that is where the backticks hide)';\n }\n\n private truncate(s: string): string {\n const MAX = 120;\n return s.length <= MAX ? s : s.slice(0, MAX) + '…';\n }\n\n private logDecision(ctx: BashContext, verdict: Verdict, reason: string): void {\n logGuardDecision(\n ctx.workspaceRoot,\n new GuardDecision(\n 'commit-message-substitution-guard', 'Bash', ctx.command, '-', verdict, reason,\n '-', L0_FAULT_NONE, MATRIX_L2_UNROWED,\n ),\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { CommandScanner } from '@webpieces/hook-runtime';
|
|
2
|
+
/** Which shell construct was found inside an inline commit message. */
|
|
3
|
+
export type MessageHazardKind = 'backtick' | 'command-substitution' | 'newline';
|
|
4
|
+
/**
|
|
5
|
+
* One hazard found in one inline `-m` message: the flag that carried it, what it is, and the text
|
|
6
|
+
* around it. Data-only → a class, per CLAUDE.md.
|
|
7
|
+
*
|
|
8
|
+
* `excerpt` is a short window around the hit rather than the whole message: the refusal is read
|
|
9
|
+
* mid-task, and a 40-line commit message pasted back at the agent buries the one thing it must see.
|
|
10
|
+
*/
|
|
11
|
+
export declare class MessageHazard {
|
|
12
|
+
readonly flag: string;
|
|
13
|
+
readonly kind: MessageHazardKind;
|
|
14
|
+
readonly excerpt: string;
|
|
15
|
+
constructor(flag: string, kind: MessageHazardKind, excerpt: string);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Finds a commit message passed INLINE (`-m` / `--message` / `-am`) whose text carries a shell
|
|
19
|
+
* construct the shell will expand before git ever sees it.
|
|
20
|
+
*
|
|
21
|
+
* Split out from the guard for the same reason `WholeRepoBuildScan` is: WHAT counts as a hit is a
|
|
22
|
+
* tokenizer question with its own tests, while the guard owns the verdict, the log line and the words
|
|
23
|
+
* of the refusal.
|
|
24
|
+
*/
|
|
25
|
+
export declare class CommitMessageSubstitutionScan {
|
|
26
|
+
private readonly scanner;
|
|
27
|
+
constructor(scanner: CommandScanner);
|
|
28
|
+
/**
|
|
29
|
+
* The first hazard anywhere in the command, or null.
|
|
30
|
+
*
|
|
31
|
+
* `command` is the RAW command — see the guard's docstring for why `commandCode` cannot be used.
|
|
32
|
+
*/
|
|
33
|
+
firstHit(command: string): MessageHazard | null;
|
|
34
|
+
private hazardInArgs;
|
|
35
|
+
/** The message carried IN the flag token itself (`-mfix`, `--message=fix`), or null. */
|
|
36
|
+
private attachedMessage;
|
|
37
|
+
/** True when the flag's message is the FOLLOWING token (`-m msg`, `-am msg`, `--message msg`). */
|
|
38
|
+
private takesFollowingMessage;
|
|
39
|
+
/** The EARLIEST hazard in this text, so the excerpt points at what the shell hits first. */
|
|
40
|
+
private hazardIn;
|
|
41
|
+
private excerpt;
|
|
42
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CommitMessageSubstitutionScan = exports.MessageHazard = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* One hazard found in one inline `-m` message: the flag that carried it, what it is, and the text
|
|
6
|
+
* around it. Data-only → a class, per CLAUDE.md.
|
|
7
|
+
*
|
|
8
|
+
* `excerpt` is a short window around the hit rather than the whole message: the refusal is read
|
|
9
|
+
* mid-task, and a 40-line commit message pasted back at the agent buries the one thing it must see.
|
|
10
|
+
*/
|
|
11
|
+
class MessageHazard {
|
|
12
|
+
flag;
|
|
13
|
+
kind;
|
|
14
|
+
excerpt;
|
|
15
|
+
constructor(flag, kind, excerpt) {
|
|
16
|
+
this.flag = flag;
|
|
17
|
+
this.kind = kind;
|
|
18
|
+
this.excerpt = excerpt;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.MessageHazard = MessageHazard;
|
|
22
|
+
/** One hazard kind and the literal text that betrays it, in the order they are reported. */
|
|
23
|
+
class HazardNeedle {
|
|
24
|
+
kind;
|
|
25
|
+
needle;
|
|
26
|
+
constructor(kind, needle) {
|
|
27
|
+
this.kind = kind;
|
|
28
|
+
this.needle = needle;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The three constructs that make a commit message dangerous, and NOTHING about the words around them.
|
|
33
|
+
*
|
|
34
|
+
* A blocklist of "dangerous commands" would have missed the incident this exists for entirely — the
|
|
35
|
+
* word that hung was `strings`, which is as ordinary as a word gets. The hazard is the METACHARACTER.
|
|
36
|
+
*/
|
|
37
|
+
const HAZARDS = [
|
|
38
|
+
new HazardNeedle('backtick', '`'),
|
|
39
|
+
new HazardNeedle('command-substitution', '$('),
|
|
40
|
+
new HazardNeedle('newline', '\n'),
|
|
41
|
+
];
|
|
42
|
+
// A short cluster of git-commit boolean short flags that may legally precede `m` in one bundle
|
|
43
|
+
// (`-am`, `-qm`, `-asm`). Deliberately NOT `[A-Za-z]*`: `-Sabcmdef` (a gpg key id containing an `m`)
|
|
44
|
+
// would then read as a message flag with an attached message.
|
|
45
|
+
const SHORT_FLAG_CLUSTER = /^-([asevqnoup]*)m(.*)$/;
|
|
46
|
+
const LONG_MESSAGE_FLAG = '--message';
|
|
47
|
+
/**
|
|
48
|
+
* Finds a commit message passed INLINE (`-m` / `--message` / `-am`) whose text carries a shell
|
|
49
|
+
* construct the shell will expand before git ever sees it.
|
|
50
|
+
*
|
|
51
|
+
* Split out from the guard for the same reason `WholeRepoBuildScan` is: WHAT counts as a hit is a
|
|
52
|
+
* tokenizer question with its own tests, while the guard owns the verdict, the log line and the words
|
|
53
|
+
* of the refusal.
|
|
54
|
+
*/
|
|
55
|
+
class CommitMessageSubstitutionScan {
|
|
56
|
+
scanner;
|
|
57
|
+
constructor(scanner) {
|
|
58
|
+
this.scanner = scanner;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The first hazard anywhere in the command, or null.
|
|
62
|
+
*
|
|
63
|
+
* `command` is the RAW command — see the guard's docstring for why `commandCode` cannot be used.
|
|
64
|
+
*/
|
|
65
|
+
firstHit(command) {
|
|
66
|
+
for (const segment of this.scanner.commandSegments(command)) {
|
|
67
|
+
const args = this.scanner.gitSubcommandArgs(segment, 'commit');
|
|
68
|
+
if (args === null)
|
|
69
|
+
continue;
|
|
70
|
+
const hit = this.hazardInArgs(args);
|
|
71
|
+
if (hit !== null)
|
|
72
|
+
return hit;
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
hazardInArgs(args) {
|
|
77
|
+
for (let i = 0; i < args.length; i++) {
|
|
78
|
+
const flag = args[i];
|
|
79
|
+
const attached = this.attachedMessage(flag);
|
|
80
|
+
if (attached !== null) {
|
|
81
|
+
const hit = this.hazardIn(flag, attached);
|
|
82
|
+
if (hit !== null)
|
|
83
|
+
return hit;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (!this.takesFollowingMessage(flag))
|
|
87
|
+
continue;
|
|
88
|
+
// The value is the NEXT token, and it may be absent (`git commit -m` alone, which git
|
|
89
|
+
// itself rejects). Nothing to inspect then — a guard never fails on a malformed command.
|
|
90
|
+
const text = args[i + 1];
|
|
91
|
+
i++;
|
|
92
|
+
if (text === undefined)
|
|
93
|
+
continue;
|
|
94
|
+
const hit = this.hazardIn(flag, text);
|
|
95
|
+
if (hit !== null)
|
|
96
|
+
return hit;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
/** The message carried IN the flag token itself (`-mfix`, `--message=fix`), or null. */
|
|
101
|
+
attachedMessage(flag) {
|
|
102
|
+
if (flag.startsWith(`${LONG_MESSAGE_FLAG}=`))
|
|
103
|
+
return flag.slice(LONG_MESSAGE_FLAG.length + 1);
|
|
104
|
+
// `--amend`, `--no-edit`, `--fixup=…` — every other long flag, and never a message.
|
|
105
|
+
if (flag.startsWith('--'))
|
|
106
|
+
return null;
|
|
107
|
+
const cluster = SHORT_FLAG_CLUSTER.exec(flag);
|
|
108
|
+
if (cluster === null)
|
|
109
|
+
return null;
|
|
110
|
+
return cluster[2] === '' ? null : cluster[2];
|
|
111
|
+
}
|
|
112
|
+
/** True when the flag's message is the FOLLOWING token (`-m msg`, `-am msg`, `--message msg`). */
|
|
113
|
+
takesFollowingMessage(flag) {
|
|
114
|
+
if (flag === LONG_MESSAGE_FLAG)
|
|
115
|
+
return true;
|
|
116
|
+
if (flag.startsWith('--'))
|
|
117
|
+
return false;
|
|
118
|
+
const cluster = SHORT_FLAG_CLUSTER.exec(flag);
|
|
119
|
+
return cluster !== null && cluster[2] === '';
|
|
120
|
+
}
|
|
121
|
+
/** The EARLIEST hazard in this text, so the excerpt points at what the shell hits first. */
|
|
122
|
+
hazardIn(flag, text) {
|
|
123
|
+
let best = null;
|
|
124
|
+
let bestAt = text.length;
|
|
125
|
+
for (const hazard of HAZARDS) {
|
|
126
|
+
const at = text.indexOf(hazard.needle);
|
|
127
|
+
if (at === -1 || at >= bestAt)
|
|
128
|
+
continue;
|
|
129
|
+
bestAt = at;
|
|
130
|
+
best = new MessageHazard(flag, hazard.kind, this.excerpt(text, at));
|
|
131
|
+
}
|
|
132
|
+
return best;
|
|
133
|
+
}
|
|
134
|
+
// A window around the hit, newlines shown as `\n` so the excerpt stays one line in the report.
|
|
135
|
+
excerpt(text, at) {
|
|
136
|
+
const BEFORE = 30;
|
|
137
|
+
const AFTER = 40;
|
|
138
|
+
const start = Math.max(0, at - BEFORE);
|
|
139
|
+
const end = Math.min(text.length, at + AFTER);
|
|
140
|
+
const body = text.slice(start, end).replace(/\n/g, '\\n');
|
|
141
|
+
return `${start > 0 ? '…' : ''}${body}${end < text.length ? '…' : ''}`;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
exports.CommitMessageSubstitutionScan = CommitMessageSubstitutionScan;
|
|
145
|
+
//# sourceMappingURL=commit-message-substitution-scan.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commit-message-substitution-scan.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/agent-workflow-rules/src/core/rules/commit-message-substitution-scan.ts"],"names":[],"mappings":"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","sourcesContent":["import { CommandScanner } from '@webpieces/hook-runtime';\n\n/** Which shell construct was found inside an inline commit message. */\nexport type MessageHazardKind = 'backtick' | 'command-substitution' | 'newline';\n\n/**\n * One hazard found in one inline `-m` message: the flag that carried it, what it is, and the text\n * around it. Data-only → a class, per CLAUDE.md.\n *\n * `excerpt` is a short window around the hit rather than the whole message: the refusal is read\n * mid-task, and a 40-line commit message pasted back at the agent buries the one thing it must see.\n */\nexport class MessageHazard {\n constructor(\n readonly flag: string,\n readonly kind: MessageHazardKind,\n readonly excerpt: string,\n ) {}\n}\n\n/** One hazard kind and the literal text that betrays it, in the order they are reported. */\nclass HazardNeedle {\n constructor(readonly kind: MessageHazardKind, readonly needle: string) {}\n}\n\n/**\n * The three constructs that make a commit message dangerous, and NOTHING about the words around them.\n *\n * A blocklist of \"dangerous commands\" would have missed the incident this exists for entirely — the\n * word that hung was `strings`, which is as ordinary as a word gets. The hazard is the METACHARACTER.\n */\nconst HAZARDS: readonly HazardNeedle[] = [\n new HazardNeedle('backtick', '`'),\n new HazardNeedle('command-substitution', '$('),\n new HazardNeedle('newline', '\\n'),\n];\n\n// A short cluster of git-commit boolean short flags that may legally precede `m` in one bundle\n// (`-am`, `-qm`, `-asm`). Deliberately NOT `[A-Za-z]*`: `-Sabcmdef` (a gpg key id containing an `m`)\n// would then read as a message flag with an attached message.\nconst SHORT_FLAG_CLUSTER = /^-([asevqnoup]*)m(.*)$/;\n\nconst LONG_MESSAGE_FLAG = '--message';\n\n/**\n * Finds a commit message passed INLINE (`-m` / `--message` / `-am`) whose text carries a shell\n * construct the shell will expand before git ever sees it.\n *\n * Split out from the guard for the same reason `WholeRepoBuildScan` is: WHAT counts as a hit is a\n * tokenizer question with its own tests, while the guard owns the verdict, the log line and the words\n * of the refusal.\n */\nexport class CommitMessageSubstitutionScan {\n constructor(private readonly scanner: CommandScanner) {}\n\n /**\n * The first hazard anywhere in the command, or null.\n *\n * `command` is the RAW command — see the guard's docstring for why `commandCode` cannot be used.\n */\n firstHit(command: string): MessageHazard | null {\n for (const segment of this.scanner.commandSegments(command)) {\n const args = this.scanner.gitSubcommandArgs(segment, 'commit');\n if (args === null) continue;\n const hit = this.hazardInArgs(args);\n if (hit !== null) return hit;\n }\n return null;\n }\n\n private hazardInArgs(args: readonly string[]): MessageHazard | null {\n for (let i = 0; i < args.length; i++) {\n const flag = args[i];\n const attached = this.attachedMessage(flag);\n if (attached !== null) {\n const hit = this.hazardIn(flag, attached);\n if (hit !== null) return hit;\n continue;\n }\n if (!this.takesFollowingMessage(flag)) continue;\n // The value is the NEXT token, and it may be absent (`git commit -m` alone, which git\n // itself rejects). Nothing to inspect then — a guard never fails on a malformed command.\n const text = args[i + 1];\n i++;\n if (text === undefined) continue;\n const hit = this.hazardIn(flag, text);\n if (hit !== null) return hit;\n }\n return null;\n }\n\n /** The message carried IN the flag token itself (`-mfix`, `--message=fix`), or null. */\n private attachedMessage(flag: string): string | null {\n if (flag.startsWith(`${LONG_MESSAGE_FLAG}=`)) return flag.slice(LONG_MESSAGE_FLAG.length + 1);\n // `--amend`, `--no-edit`, `--fixup=…` — every other long flag, and never a message.\n if (flag.startsWith('--')) return null;\n const cluster = SHORT_FLAG_CLUSTER.exec(flag);\n if (cluster === null) return null;\n return cluster[2] === '' ? null : cluster[2];\n }\n\n /** True when the flag's message is the FOLLOWING token (`-m msg`, `-am msg`, `--message msg`). */\n private takesFollowingMessage(flag: string): boolean {\n if (flag === LONG_MESSAGE_FLAG) return true;\n if (flag.startsWith('--')) return false;\n const cluster = SHORT_FLAG_CLUSTER.exec(flag);\n return cluster !== null && cluster[2] === '';\n }\n\n /** The EARLIEST hazard in this text, so the excerpt points at what the shell hits first. */\n private hazardIn(flag: string, text: string): MessageHazard | null {\n let best: MessageHazard | null = null;\n let bestAt = text.length;\n for (const hazard of HAZARDS) {\n const at = text.indexOf(hazard.needle);\n if (at === -1 || at >= bestAt) continue;\n bestAt = at;\n best = new MessageHazard(flag, hazard.kind, this.excerpt(text, at));\n }\n return best;\n }\n\n // A window around the hit, newlines shown as `\\n` so the excerpt stays one line in the report.\n private excerpt(text: string, at: number): string {\n const BEFORE = 30;\n const AFTER = 40;\n const start = Math.max(0, at - BEFORE);\n const end = Math.min(text.length, at + AFTER);\n const body = text.slice(start, end).replace(/\\n/g, '\\\\n');\n return `${start > 0 ? '…' : ''}${body}${end < text.length ? '…' : ''}`;\n }\n}\n"]}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { CommandScanner, CommandSegment } from '@webpieces/hook-runtime';
|
|
2
|
+
/**
|
|
3
|
+
* Decides the one question stale-main-bash-guard asks of a command: does this segment put stale
|
|
4
|
+
* WORKSPACE FILE CONTENT into the agent's context?
|
|
5
|
+
*
|
|
6
|
+
* The distinction that matters is content vs metadata, not shell vs tool. `git log`, `git diff`,
|
|
7
|
+
* `git status`, builds, tests and the cure itself are all fine on a stale main — none of them hands
|
|
8
|
+
* you the text of a file that upstream has moved past. `cat src/x.ts` does, and so does
|
|
9
|
+
* `grep -r foo services/`, `ls .github/workflows/` (the incident's actual wrong answer: a listing
|
|
10
|
+
* missing a workflow that existed upstream) and `git show HEAD:file`.
|
|
11
|
+
*
|
|
12
|
+
* Three things keep this from over-blocking:
|
|
13
|
+
* 1. A piped consumer reads stdin, not the tree — `git log | grep fix` is metadata, so it passes.
|
|
14
|
+
* 2. A reader with no path operand that does not default to the cwd reads stdin — `cat` alone,
|
|
15
|
+
* `grep pattern` alone.
|
|
16
|
+
* 3. Only paths INSIDE the workspace count. `cat /etc/hosts`, `cat ~/.zshrc`, `cat /tmp/out.log`
|
|
17
|
+
* are nothing to do with this repo's staleness.
|
|
18
|
+
*/
|
|
19
|
+
export declare class ContentReadScan {
|
|
20
|
+
private readonly scanner;
|
|
21
|
+
private readonly workspaceRoot;
|
|
22
|
+
private readonly shell;
|
|
23
|
+
private readonly baseDir;
|
|
24
|
+
/**
|
|
25
|
+
* `effectiveCwd` is the directory the command really runs in — after its own leading `cd`, which
|
|
26
|
+
* is how an agent reaches a linked worktree, since the harness resets a cwd that left the workspace.
|
|
27
|
+
* RELATIVE operands are resolved against it, not against workspaceRoot: `cd /tmp/scratch && cat
|
|
28
|
+
* notes.md` reads `/tmp/scratch/notes.md`, which is nothing to do with this repo's staleness,
|
|
29
|
+
* while `cd /tmp/scratch && cat /repo/src/x.ts` still names repo content and is still caught.
|
|
30
|
+
* Defaults to workspaceRoot, which is exactly the old behaviour (relative = inside the repo).
|
|
31
|
+
*/
|
|
32
|
+
constructor(scanner: CommandScanner, workspaceRoot: string, effectiveCwd?: string);
|
|
33
|
+
/**
|
|
34
|
+
* True when this segment's ONLY job is reading content, and nothing it reads is in the workspace —
|
|
35
|
+
* `ls -la ~/.claude/projects/`, `cat /tmp/out.log`, `grep -r x /other/repo`.
|
|
36
|
+
*
|
|
37
|
+
* merged-branch-bash-guard needs this: it default-denies bash on a merged branch, and denied an
|
|
38
|
+
* `ls` of a directory outside every git repo on the grounds that the branch was merged. Nothing
|
|
39
|
+
* about a read that never touches the tree is affected by which branch the tree is on. The
|
|
40
|
+
* "content reader" restriction is what keeps this from becoming a general escape hatch: a build,
|
|
41
|
+
* a server or a git write is not a content reader and never qualifies, however its paths look.
|
|
42
|
+
*/
|
|
43
|
+
readsOnlyOutsideContent(segment: CommandSegment): boolean;
|
|
44
|
+
private isContentReader;
|
|
45
|
+
/**
|
|
46
|
+
* The command word that reads stale workspace content, or null when this segment does not.
|
|
47
|
+
* The returned string is only a log/diagnostic label.
|
|
48
|
+
*
|
|
49
|
+
* Judged on the segment's EFFECTIVE words: `for f in a b; do cat $f; done` splits into segments
|
|
50
|
+
* whose middle one is literally `do cat $f`, and taking `do` as the command name let every loop
|
|
51
|
+
* body read the stale tree unseen.
|
|
52
|
+
*/
|
|
53
|
+
readsStaleContent(segment: CommandSegment): string | null;
|
|
54
|
+
/**
|
|
55
|
+
* git's own content readers. `git grep` searches tracked CONTENT and `git show <rev>:<path>`
|
|
56
|
+
* prints a file — both stale when the rev is local. Against an `origin/…` rev they read the
|
|
57
|
+
* CURRENT upstream tree, which is exactly what we want the agent doing, so those pass.
|
|
58
|
+
*/
|
|
59
|
+
private gitContentRead;
|
|
60
|
+
private pathOperands;
|
|
61
|
+
/**
|
|
62
|
+
* Is this operand a path inside the workspace? A RELATIVE operand is resolved against the
|
|
63
|
+
* directory the command actually runs in (`baseDir`), so it counts only when that directory is
|
|
64
|
+
* itself in the tree — the old code assumed every relative path meant "inside the repo", which is
|
|
65
|
+
* how a command run in a `/private/tmp` scratchpad got judged as reading a stale repo. An
|
|
66
|
+
* absolute path counts only when it is genuinely under workspaceRoot, so `/etc/hosts`,
|
|
67
|
+
* `~/notes.md` and `/tmp/x` are not this repo's problem.
|
|
68
|
+
*
|
|
69
|
+
* Deliberately NOT filesystem-checked: whether the path exists says nothing about staleness, and
|
|
70
|
+
* a stat per operand on the blocking hook path is exactly the cost these guards avoid.
|
|
71
|
+
*/
|
|
72
|
+
private isWorkspacePath;
|
|
73
|
+
private isInWorkspace;
|
|
74
|
+
private isEscapeHatchPath;
|
|
75
|
+
private baseName;
|
|
76
|
+
}
|