@intentius/chant 0.124.76 → 0.124.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/dist/__fixtures__/agent-guard/plywood.d.ts +8 -0
  2. package/dist/__fixtures__/agent-guard/plywood.d.ts.map +1 -0
  3. package/dist/__fixtures__/agent-guard/plywood.js +19 -0
  4. package/dist/__fixtures__/agent-guard/terragucci.d.ts +8 -0
  5. package/dist/__fixtures__/agent-guard/terragucci.d.ts.map +1 -0
  6. package/dist/__fixtures__/agent-guard/terragucci.js +30 -0
  7. package/dist/__fixtures__/gated-ledger.d.ts +10 -0
  8. package/dist/__fixtures__/gated-ledger.d.ts.map +1 -0
  9. package/dist/__fixtures__/gated-ledger.js +51 -0
  10. package/dist/__fixtures__/notify-messages/events.d.ts +9 -0
  11. package/dist/__fixtures__/notify-messages/events.d.ts.map +1 -0
  12. package/dist/__fixtures__/notify-messages/events.js +21 -0
  13. package/dist/__fixtures__/product-words.d.ts +14 -0
  14. package/dist/__fixtures__/product-words.d.ts.map +1 -1
  15. package/dist/__fixtures__/product-words.js +11 -0
  16. package/dist/__fixtures__/terragucci-notify.d.ts +66 -0
  17. package/dist/__fixtures__/terragucci-notify.d.ts.map +1 -0
  18. package/dist/__fixtures__/terragucci-notify.js +133 -0
  19. package/dist/action-pins.d.ts +8 -0
  20. package/dist/action-pins.d.ts.map +1 -1
  21. package/dist/action-pins.js +9 -0
  22. package/dist/agent-guard.d.ts +143 -0
  23. package/dist/agent-guard.d.ts.map +1 -0
  24. package/dist/agent-guard.js +455 -0
  25. package/dist/ci-commit.d.ts +120 -5
  26. package/dist/ci-commit.d.ts.map +1 -1
  27. package/dist/ci-commit.js +214 -23
  28. package/dist/ci-own-jobs.d.ts +81 -0
  29. package/dist/ci-own-jobs.d.ts.map +1 -0
  30. package/dist/ci-own-jobs.js +233 -0
  31. package/dist/ci-product-words.d.ts +18 -4
  32. package/dist/ci-product-words.d.ts.map +1 -1
  33. package/dist/ci-product-words.js +26 -6
  34. package/dist/ci-product.d.ts +40 -1
  35. package/dist/ci-product.d.ts.map +1 -1
  36. package/dist/ci-product.js +40 -1
  37. package/dist/cli/handlers/approve.d.ts +5 -1
  38. package/dist/cli/handlers/approve.d.ts.map +1 -1
  39. package/dist/cli/handlers/approve.js +17 -11
  40. package/dist/cli/handlers/build.d.ts +1 -1
  41. package/dist/cli/handlers/build.d.ts.map +1 -1
  42. package/dist/cli/handlers/build.js +88 -2
  43. package/dist/cli/handlers/commit.d.ts.map +1 -1
  44. package/dist/cli/handlers/commit.js +3 -1
  45. package/dist/cli/handlers/notify-relay.d.ts +18 -1
  46. package/dist/cli/handlers/notify-relay.d.ts.map +1 -1
  47. package/dist/cli/handlers/notify-relay.js +90 -14
  48. package/dist/cli/handlers/pr.d.ts.map +1 -1
  49. package/dist/cli/handlers/pr.js +3 -1
  50. package/dist/cli/handlers/release.d.ts +5 -1
  51. package/dist/cli/handlers/release.d.ts.map +1 -1
  52. package/dist/cli/handlers/release.js +21 -9
  53. package/dist/cli/handlers/run-generate.d.ts.map +1 -1
  54. package/dist/cli/handlers/run-generate.js +2 -1
  55. package/dist/cli/main.d.ts.map +1 -1
  56. package/dist/cli/main.js +21 -1
  57. package/dist/cli/parse-args.d.ts.map +1 -1
  58. package/dist/cli/parse-args.js +60 -4
  59. package/dist/cli/registry.d.ts +16 -0
  60. package/dist/cli/registry.d.ts.map +1 -1
  61. package/dist/components/pr-env-components.d.ts +65 -0
  62. package/dist/components/pr-env-components.d.ts.map +1 -0
  63. package/dist/components/pr-env-components.js +162 -0
  64. package/dist/components/pr-policy.d.ts +15 -0
  65. package/dist/components/pr-policy.d.ts.map +1 -1
  66. package/dist/components/pr-policy.js +24 -1
  67. package/dist/components/release-compile.d.ts +15 -3
  68. package/dist/components/release-compile.d.ts.map +1 -1
  69. package/dist/components/release-compile.js +66 -15
  70. package/dist/config.d.ts +14 -8
  71. package/dist/config.d.ts.map +1 -1
  72. package/dist/config.js +16 -1
  73. package/dist/gate-policy.d.ts +14 -1
  74. package/dist/gate-policy.d.ts.map +1 -1
  75. package/dist/gate-policy.js +16 -6
  76. package/dist/lexicon.d.ts +25 -1
  77. package/dist/lexicon.d.ts.map +1 -1
  78. package/dist/lexicon.js +10 -0
  79. package/dist/lifecycle/gated-op.d.ts +95 -0
  80. package/dist/lifecycle/gated-op.d.ts.map +1 -0
  81. package/dist/lifecycle/gated-op.js +96 -0
  82. package/dist/lifecycle/git.d.ts.map +1 -1
  83. package/dist/lifecycle/git.js +83 -2
  84. package/dist/lifecycle/release-images.d.ts +77 -3
  85. package/dist/lifecycle/release-images.d.ts.map +1 -1
  86. package/dist/lifecycle/release-images.js +132 -6
  87. package/dist/notify-product.d.ts +11 -2
  88. package/dist/notify-product.d.ts.map +1 -1
  89. package/dist/notify-product.js +16 -4
  90. package/dist/notify-relay-repo.d.ts +131 -0
  91. package/dist/notify-relay-repo.d.ts.map +1 -0
  92. package/dist/notify-relay-repo.js +202 -0
  93. package/dist/notify-relay.d.ts +25 -1
  94. package/dist/notify-relay.d.ts.map +1 -1
  95. package/dist/notify-relay.js +27 -2
  96. package/dist/notify.d.ts +36 -4
  97. package/dist/notify.d.ts.map +1 -1
  98. package/dist/notify.js +31 -21
  99. package/dist/notify.schema.json +1 -0
  100. package/dist/op/activities/review-product.d.ts +111 -0
  101. package/dist/op/activities/review-product.d.ts.map +1 -0
  102. package/dist/op/activities/review-product.js +116 -0
  103. package/dist/op/op-waves-capability.js +2 -2
  104. package/dist/policy-engine.d.ts +95 -0
  105. package/dist/policy-engine.d.ts.map +1 -0
  106. package/dist/policy-engine.js +157 -0
  107. package/dist/pr-forge-reads.d.ts +124 -0
  108. package/dist/pr-forge-reads.d.ts.map +1 -0
  109. package/dist/pr-forge-reads.js +322 -0
  110. package/dist/pr-forge.d.ts +14 -0
  111. package/dist/pr-forge.d.ts.map +1 -1
  112. package/dist/pr-forge.js +21 -13
  113. package/dist/pr-plan-note.d.ts +293 -0
  114. package/dist/pr-plan-note.d.ts.map +1 -0
  115. package/dist/pr-plan-note.js +362 -0
  116. package/dist/release-floors.d.ts +9 -1
  117. package/dist/release-floors.d.ts.map +1 -1
  118. package/dist/release-floors.js +9 -1
  119. package/dist/state-export.d.ts +126 -0
  120. package/dist/state-export.d.ts.map +1 -0
  121. package/dist/state-export.js +156 -0
  122. package/dist/state-migration.d.ts +148 -0
  123. package/dist/state-migration.d.ts.map +1 -0
  124. package/dist/state-migration.js +146 -0
  125. package/dist/state-unlock.d.ts +167 -0
  126. package/dist/state-unlock.d.ts.map +1 -0
  127. package/dist/state-unlock.js +196 -0
  128. package/dist/wave-runtime.mjs +332 -332
  129. package/dist/workspace/declaration.schema.json +1 -1
  130. package/dist/workspace/release-workflow.d.ts +10 -0
  131. package/dist/workspace/release-workflow.d.ts.map +1 -1
  132. package/dist/workspace/release-workflow.js +10 -4
  133. package/package.json +1 -1
  134. package/src/__fixtures__/agent-guard/plywood.ts +27 -0
  135. package/src/__fixtures__/agent-guard/terragucci.ts +39 -0
  136. package/src/__fixtures__/gated-ledger.ts +58 -0
  137. package/src/__fixtures__/notify-messages/events.ts +23 -0
  138. package/src/__fixtures__/notify-messages/messages.json +50 -0
  139. package/src/__fixtures__/pr-loop/terragucci-plan.note.golden.md +53 -0
  140. package/src/__fixtures__/pr-loop/terragucci-plan.note.json +83 -0
  141. package/src/__fixtures__/product-words.ts +12 -0
  142. package/src/__fixtures__/review-agent/terragucci-review.json +94 -0
  143. package/src/__fixtures__/terragucci-notify.ts +170 -0
  144. package/src/action-pins.test.ts +12 -1
  145. package/src/action-pins.ts +11 -0
  146. package/src/agent-guard.test.ts +319 -0
  147. package/src/agent-guard.ts +547 -0
  148. package/src/ci-commit.test.ts +213 -2
  149. package/src/ci-commit.ts +258 -27
  150. package/src/ci-own-jobs.test.ts +108 -0
  151. package/src/ci-own-jobs.ts +252 -0
  152. package/src/ci-product-words.ts +35 -7
  153. package/src/ci-product.ts +81 -1
  154. package/src/cli/handlers/approve-cwd.test.ts +112 -0
  155. package/src/cli/handlers/approve.ts +22 -11
  156. package/src/cli/handlers/build-own-jobs.test.ts +80 -0
  157. package/src/cli/handlers/build-pr-loop-control.test.ts +87 -0
  158. package/src/cli/handlers/build-run-group.test.ts +59 -0
  159. package/src/cli/handlers/build.ts +83 -3
  160. package/src/cli/handlers/commit.ts +3 -0
  161. package/src/cli/handlers/notify-relay.ts +99 -17
  162. package/src/cli/handlers/pr.ts +3 -1
  163. package/src/cli/handlers/release.ts +20 -10
  164. package/src/cli/handlers/run-generate.ts +2 -1
  165. package/src/cli/main.ts +21 -1
  166. package/src/cli/parse-args.ts +45 -3
  167. package/src/cli/registry.ts +16 -0
  168. package/src/components/pr-env-components.test.ts +137 -0
  169. package/src/components/pr-env-components.ts +213 -0
  170. package/src/components/pr-policy.test.ts +41 -1
  171. package/src/components/pr-policy.ts +25 -1
  172. package/src/components/release-compile.test.ts +47 -0
  173. package/src/components/release-compile.ts +70 -13
  174. package/src/config.ts +23 -3
  175. package/src/gate-policy-bundle.test.ts +14 -0
  176. package/src/gate-policy.test.ts +7 -0
  177. package/src/gate-policy.ts +22 -7
  178. package/src/lexicon.ts +28 -1
  179. package/src/lifecycle/gated-op.ts +158 -0
  180. package/src/lifecycle/git.test.ts +92 -0
  181. package/src/lifecycle/git.ts +71 -2
  182. package/src/lifecycle/release-images-dockerfile.test.ts +221 -0
  183. package/src/lifecycle/release-images.ts +167 -8
  184. package/src/notify-message-words.test.ts +186 -0
  185. package/src/notify-product.ts +17 -4
  186. package/src/notify-relay-repo.test.ts +284 -0
  187. package/src/notify-relay-repo.ts +234 -0
  188. package/src/notify-relay.test.ts +12 -0
  189. package/src/notify-relay.ts +29 -3
  190. package/src/notify.schema.json +1 -0
  191. package/src/notify.ts +59 -19
  192. package/src/op/activities/review-product.test.ts +116 -0
  193. package/src/op/activities/review-product.ts +200 -0
  194. package/src/op/op-waves-capability.ts +2 -2
  195. package/src/op/schedule-overdue.test.ts +26 -0
  196. package/src/policy-engine.test.ts +162 -0
  197. package/src/policy-engine.ts +200 -0
  198. package/src/pr-forge-reads.test.ts +285 -0
  199. package/src/pr-forge-reads.ts +424 -0
  200. package/src/pr-forge.ts +30 -16
  201. package/src/pr-plan-note.test.ts +105 -0
  202. package/src/pr-plan-note.ts +521 -0
  203. package/src/release-floors.ts +13 -1
  204. package/src/state-export.test.ts +124 -0
  205. package/src/state-export.ts +227 -0
  206. package/src/state-migration.test.ts +133 -0
  207. package/src/state-migration.ts +236 -0
  208. package/src/state-unlock.test.ts +165 -0
  209. package/src/state-unlock.ts +293 -0
  210. package/src/workspace/declaration.schema.json +1 -1
  211. package/src/workspace/release-workflow.test.ts +34 -0
  212. package/src/workspace/release-workflow.ts +19 -4
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-guard.d.ts","sourceRoot":"","sources":["../src/agent-guard.ts"],"names":[],"mappings":"AAoCA,qHAAqH;AACrH,eAAO,MAAM,iBAAiB,aAAa,CAAC;AAE5C,wHAAwH;AACxH,MAAM,WAAW,gBAAgB;IAC/B,0CAA0C;IAC1C,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,sEAAsE;IACtE,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IACrC,yGAAyG;IACzG,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,0GAA0G;IAC1G,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,+HAA+H;IAC/H,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,0FAA0F;IAC1F,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,+IAA+I;IAC/I,QAAQ,CAAC,EAAE,KAAK,CAAC;QAAE,KAAK,EAAE,MAAM,EAAE,CAAC;QAAC,GAAG,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CACtD;AAED,2BAA2B;AAC3B,MAAM,WAAW,eAAe;IAC9B,+DAA+D;IAC/D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,MAAM,qBAAqB,gCAAgC,CAAC;AAEnE;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,GAAE,SAAS,MAAM,EAAO,EAAE,IAAI,GAAE,CAAC,OAAO,EAAE,MAAM,KAAK,KAA2B,GAAG,MAAM,CAkBzL;AAED,MAAM,WAAW,wBAAyB,SAAQ,cAAc;IAC9D,oCAAoC;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,sGAAsG;IACtG,OAAO,EAAE,OAAO,GAAG,SAAS,CAAC;IAC7B,kGAAkG;IAClG,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,mGAAmG;IACnG,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,sGAAsG;IACtG,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,0CAA0C;IAC1C,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,yFAAyF;IACzF,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,KAAK,CAAC;CACnC;AAED,MAAM,WAAW,iBAAiB;IAChC,qDAAqD;IACrD,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,yDAAyD;IACzD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,iDAAiD;IACjD,WAAW,EAAE,MAAM,CAAC;IACpB,8DAA8D;IAC9D,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,iFAAiF;IACjF,IAAI,EAAE,MAAM,CAAC;CACd;AAED,uEAAuE;AACvE,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,wBAAwB,GAAG,iBAAiB,CA4DhF"}
@@ -0,0 +1,455 @@
1
+ /**
2
+ * The agent guard (#4770): a speed bump that keeps a coding agent in a
3
+ * checkout from running what applies, approves or overrides, so the change
4
+ * goes through a pull request and a person's approval. terragucci and
5
+ * plywood each had one; a product now passes its words and rules here.
6
+ *
7
+ * Two layers, both written by {@link installAgentGuard}:
8
+ *
9
+ * - a Claude Code PreToolUse hook on Bash: one Node script that reads the
10
+ * tool call on stdin and blocks (exit 2, Claude Code's block code) a
11
+ * command line that would run a refused command;
12
+ * - a wrapper per engine binary, for the agent's shell to put ahead of the
13
+ * real binary on PATH: it refuses the same commands and runs the next
14
+ * binary of that name on PATH for everything else.
15
+ *
16
+ * The rule lives once, in {@link makeGuard}. The script carries its source
17
+ * (`makeGuard.toString()`) and its config as JSON, and imports Node's modules
18
+ * only; {@link agentGuard} runs the same function in process, so a product's
19
+ * `guard check` and the hook give the same answers.
20
+ *
21
+ * Two installs: `local` writes the script and wrappers under the checkout's
22
+ * git directory, with absolute paths, and the hook into
23
+ * `.claude/settings.local.json`, listed in `.git/info/exclude`; `project`
24
+ * writes the script to `.claude/hooks/<product>-guard.mjs` and the hook into
25
+ * `.claude/settings.json` as `$CLAUDE_PROJECT_DIR/...`, meant to be
26
+ * committed, and the wrappers to `.<product>/agent-bin`.
27
+ *
28
+ * An agent that calls the real binary by its path, through a script, or with
29
+ * credentials of its own gets past it; the control is the credentials. Roots
30
+ * are written into the script, so the guard is written again after they
31
+ * change.
32
+ */
33
+ import { spawnSync } from "node:child_process";
34
+ import { chmodSync, existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
35
+ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
36
+ /** The first chant with the shared agent guard (#4770). Products feature-detect on it, or on `installAgentGuard`. */
37
+ export const AGENT_GUARD_FLOOR = "0.124.77";
38
+ /**
39
+ * The rule. It references nothing outside itself: the guard's script carries
40
+ * its source text, so keep it to its parameters and ES2020.
41
+ */
42
+ export function makeGuard(config, deps) {
43
+ const { realpathSync, resolve, basename, join, relative, sep } = deps;
44
+ const real = (p) => {
45
+ try {
46
+ return realpathSync(p);
47
+ }
48
+ catch {
49
+ return resolve(p);
50
+ }
51
+ };
52
+ const repo = real(config.repo !== undefined ? config.repo : resolve(deps.scriptDir || ".", config.repoFromScript || "."));
53
+ const roots = config.roots === undefined ? undefined : config.roots.map((r) => real(join(repo, r)));
54
+ const words = config.words;
55
+ const rules = config.rules;
56
+ const inside = (at, dir) => at === dir || at.startsWith(dir === sep ? dir : dir + sep);
57
+ const bare = (f) => f.replace(/^--?/, "");
58
+ const flagIs = (a, names) => {
59
+ if (!names || !/^--?[^-]/.test(a))
60
+ return false;
61
+ const n = bare(a).replace(/=.*$/, "");
62
+ return names.some((f) => bare(f) === n);
63
+ };
64
+ const message = (what, where) => {
65
+ const at = where === undefined ? "" : (words.where === undefined ? " in {root}" : words.where).split("{root}").join(where);
66
+ return words.product + " guard: " + words.refusal.split("{what}").join(what).split("{where}").join(at);
67
+ };
68
+ function verdict(bin, args, cwd) {
69
+ const e = rules.engines[bin];
70
+ if (!e)
71
+ return undefined;
72
+ let dir = cwd;
73
+ let all = false;
74
+ let refusedFlag;
75
+ const ws = [];
76
+ for (let i = 0; i < args.length; i++) {
77
+ const a = args[i];
78
+ if (flagIs(a, e.dirFlags)) {
79
+ const eq = a.indexOf("=");
80
+ if (eq >= 0)
81
+ dir = resolve(cwd, a.slice(eq + 1));
82
+ else if (i + 1 < args.length)
83
+ dir = resolve(cwd, args[++i]);
84
+ }
85
+ else if (e.allFlags && e.allFlags.indexOf(a) >= 0)
86
+ all = true;
87
+ else if (flagIs(a, e.refuseFlags))
88
+ refusedFlag = refusedFlag || a.replace(/=.*$/, "");
89
+ else if (e.valuedFlags && e.valuedFlags.indexOf(a) >= 0)
90
+ i++;
91
+ else if (a !== "--" && !a.startsWith("-"))
92
+ ws.push(a);
93
+ }
94
+ for (const p of e.prefixes || []) {
95
+ if (p.words.every((w, i) => ws[i] === w)) {
96
+ ws.splice(0, p.words.length);
97
+ if (p.all)
98
+ all = true;
99
+ break;
100
+ }
101
+ }
102
+ const cmd = ws[0];
103
+ const sub = cmd !== undefined && e.refuseSub && Object.prototype.hasOwnProperty.call(e.refuseSub, cmd) ? e.refuseSub[cmd] : undefined;
104
+ const command = cmd !== undefined && e.refuse.indexOf(cmd) >= 0 ? cmd : sub && ws[1] !== undefined && sub.indexOf(ws[1]) >= 0 ? cmd + " " + ws[1] : refusedFlag;
105
+ if (command === undefined)
106
+ return undefined;
107
+ let where;
108
+ if (roots !== undefined) {
109
+ const at = real(dir);
110
+ const hit = roots.find((r) => r === at || (all && inside(r, at)));
111
+ if (!hit)
112
+ return undefined;
113
+ where = relative(repo, hit) || ".";
114
+ }
115
+ const what = bin + " " + command;
116
+ return { bin, command, what, ...(where === undefined ? {} : { where }), message: message(what, where) };
117
+ }
118
+ function productVerdict(args, cwd) {
119
+ const p = rules.product;
120
+ if (!p || !inside(real(cwd), repo))
121
+ return undefined;
122
+ const ws = args.filter((a) => !a.startsWith("-"));
123
+ const bin = words.product;
124
+ const found = (command) => ({ bin, command, what: bin + " " + command, message: message(bin + " " + command, undefined) });
125
+ for (const c of p.commands)
126
+ if (c.every((w, i) => ws[i] === w))
127
+ return found(c.join(" "));
128
+ for (const [flag, value] of p.flagValues || []) {
129
+ const i = args.findIndex((a) => a === flag || a.startsWith(flag + "="));
130
+ if (i >= 0 && (args[i] === flag + "=" + value || (args[i] === flag && args[i + 1] === value)))
131
+ return found(((ws[0] === undefined ? "" : ws[0] + " ") + flag + " " + value).trim());
132
+ }
133
+ return undefined;
134
+ }
135
+ /** A shell command line's words and operators, quotes taken off. Enough for a guard, not a shell. */
136
+ function lex(text) {
137
+ const out = [];
138
+ let cur = null;
139
+ const end = () => {
140
+ if (cur !== null)
141
+ out.push({ word: cur });
142
+ cur = null;
143
+ };
144
+ for (let i = 0; i < text.length; i++) {
145
+ const c = text[i];
146
+ if (c === "'") {
147
+ const j = text.indexOf("'", i + 1);
148
+ cur = (cur === null ? "" : cur) + (j < 0 ? text.slice(i + 1) : text.slice(i + 1, j));
149
+ i = j < 0 ? text.length : j;
150
+ }
151
+ else if (c === '"') {
152
+ let s = "";
153
+ for (i++; i < text.length && text[i] !== '"'; i++) {
154
+ if (text[i] === "\\" && i + 1 < text.length)
155
+ i++;
156
+ s += text[i];
157
+ }
158
+ cur = (cur === null ? "" : cur) + s;
159
+ }
160
+ else if (c === "\\" && i + 1 < text.length)
161
+ cur = (cur === null ? "" : cur) + text[++i];
162
+ else if (c === "$" && text[i + 1] === "(") {
163
+ end();
164
+ out.push({ op: "$(" });
165
+ i++;
166
+ }
167
+ else if (";&|\n()`".indexOf(c) >= 0) {
168
+ end();
169
+ out.push({ op: c });
170
+ }
171
+ else if (/\s/.test(c))
172
+ end();
173
+ else
174
+ cur = (cur === null ? "" : cur) + c;
175
+ }
176
+ end();
177
+ return out;
178
+ }
179
+ /** Words that run the words after them as the command, with their flags that take a value. */
180
+ const LAUNCHERS = {
181
+ command: [],
182
+ builtin: [],
183
+ exec: ["-a"],
184
+ env: ["-u", "--unset", "-C", "--chdir", "-S", "--split-string"],
185
+ sudo: ["-u", "-g", "-h", "-p", "-C", "-D", "-r", "-t", "-U"],
186
+ doas: ["-u", "-C"],
187
+ time: ["-f", "-o", "--format", "--output"],
188
+ nohup: [],
189
+ nice: ["-n", "--adjustment"],
190
+ xargs: ["-a", "-d", "-E", "-e", "-I", "-i", "-L", "-l", "-n", "-P", "-s", "--arg-file", "--delimiter", "--max-args", "--max-procs"],
191
+ };
192
+ const SHELLS = ["sh", "bash", "zsh", "dash", "ksh"];
193
+ const RUNNERS = ["npx", "bunx", "pnpx"];
194
+ const RUNNER_VALUED = ["-p", "--package", "-c", "--call"];
195
+ function checkCommand(text, cwd, depth = 0) {
196
+ if (depth > 3)
197
+ return undefined;
198
+ let dir = cwd;
199
+ const segments = [[]];
200
+ for (const t of lex(text)) {
201
+ if (t.op)
202
+ segments.push([]);
203
+ else
204
+ segments[segments.length - 1].push(t.word);
205
+ }
206
+ for (let seg of segments) {
207
+ for (;;) {
208
+ if (seg.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(seg[0]))
209
+ seg = seg.slice(1);
210
+ else if (seg.length && Object.prototype.hasOwnProperty.call(LAUNCHERS, basename(seg[0]))) {
211
+ const valued = LAUNCHERS[basename(seg[0])];
212
+ seg = seg.slice(1);
213
+ while (seg.length && seg[0].startsWith("-") && seg[0] !== "-") {
214
+ if (seg[0] === "--") {
215
+ seg = seg.slice(1);
216
+ break;
217
+ }
218
+ seg = seg.slice(valued.indexOf(seg[0]) >= 0 ? 2 : 1);
219
+ }
220
+ }
221
+ else
222
+ break;
223
+ }
224
+ if (!seg.length)
225
+ continue;
226
+ const head = basename(seg[0]);
227
+ if (head === "cd") {
228
+ const home = deps.home === undefined ? dir : deps.home;
229
+ const to = seg[1] === undefined || seg[1] === "~" ? home : seg[1].startsWith("~/") ? join(home, seg[1].slice(2)) : seg[1];
230
+ if (to !== "-")
231
+ dir = resolve(dir, to);
232
+ continue;
233
+ }
234
+ if (SHELLS.indexOf(head) >= 0) {
235
+ const c = seg.findIndex((a, i) => i > 0 && /^-[A-Za-z]*c[A-Za-z]*$/.test(a));
236
+ if (c >= 0 && seg[c + 1] !== undefined) {
237
+ const v = checkCommand(seg[c + 1], dir, depth + 1);
238
+ if (v)
239
+ return v;
240
+ }
241
+ continue;
242
+ }
243
+ if (head === "eval") {
244
+ const v = checkCommand(seg.slice(1).join(" "), dir, depth + 1);
245
+ if (v)
246
+ return v;
247
+ continue;
248
+ }
249
+ let rest = seg;
250
+ if (RUNNERS.indexOf(head) >= 0) {
251
+ rest = rest.slice(1);
252
+ while (rest.length && rest[0].startsWith("-"))
253
+ rest = rest.slice(RUNNER_VALUED.indexOf(rest[0]) >= 0 ? 2 : 1);
254
+ if (!rest.length)
255
+ continue;
256
+ }
257
+ // npx names a package as @scope/name, with a version after an @.
258
+ const name = basename(rest[0]).replace(/@.*$/, "");
259
+ const v = Object.prototype.hasOwnProperty.call(rules.engines, name) ? verdict(name, rest.slice(1), dir) : name === words.product ? productVerdict(rest.slice(1), dir) : undefined;
260
+ if (v)
261
+ return v;
262
+ }
263
+ return undefined;
264
+ }
265
+ return { checkCommand: (text, cwd) => checkCommand(text, cwd), verdict, productVerdict, repo };
266
+ }
267
+ /** The guard in process, for a product's `guard check`: the rule the hook runs. */
268
+ export function agentGuard(spec, at) {
269
+ return makeGuard({ ...spec, repo: at.repo, ...(at.roots ? { roots: at.roots } : {}) }, nodeDeps());
270
+ }
271
+ function nodeDeps(scriptDir) {
272
+ return { realpathSync, resolve, basename, join, relative, sep, home: process.env.HOME, ...(scriptDir ? { scriptDir } : {}) };
273
+ }
274
+ /** The guard's script: {@link makeGuard}'s source, the config, and the hook and wrapper entry points. */
275
+ export function agentGuardScript(config) {
276
+ const p = JSON.stringify(config.words.product);
277
+ return `// Written by ${config.words.product} guard. Run it again after the pipeline's roots change.
278
+ import { spawnSync } from "node:child_process";
279
+ import { accessSync, constants, realpathSync, statSync } from "node:fs";
280
+ import { basename, delimiter, dirname, join, relative, resolve, sep } from "node:path";
281
+ import { fileURLToPath } from "node:url";
282
+
283
+ const CONFIG = ${JSON.stringify(config)};
284
+ const PRODUCT = ${p};
285
+ // esbuild with keepNames (tsx) wraps the function's inner functions in __name.
286
+ const __name = (f) => f;
287
+ ${makeGuard.toString()}
288
+ const guard = makeGuard(CONFIG, { realpathSync, resolve, basename, join, relative, sep, home: process.env.HOME, scriptDir: dirname(fileURLToPath(import.meta.url)) });
289
+ export const checkCommand = guard.checkCommand;
290
+ export const verdict = guard.verdict;
291
+
292
+ const real = (p) => { try { return realpathSync(p); } catch { return resolve(p); } };
293
+
294
+ /** The next binary of this name on PATH, past the wrappers' own directory. */
295
+ function realBinary(bin, self) {
296
+ const skip = real(self);
297
+ for (const d of (process.env.PATH ?? "").split(delimiter)) {
298
+ if (!d || real(d) === skip) continue;
299
+ const p = join(d, bin);
300
+ try {
301
+ accessSync(p, constants.X_OK);
302
+ if (statSync(p).isFile()) return p;
303
+ } catch {}
304
+ }
305
+ return undefined;
306
+ }
307
+
308
+ if (process.argv[1] && real(process.argv[1]) === real(fileURLToPath(import.meta.url))) {
309
+ const [mode, ...rest] = process.argv.slice(2);
310
+ if (mode === "wrap") {
311
+ const [bin, self, ...args] = rest;
312
+ const v = verdict(bin, args, process.cwd());
313
+ if (v) {
314
+ process.stderr.write(v.message + "\\n");
315
+ process.exit(${config.rules.wrapperExit ?? 3});
316
+ }
317
+ const exe = realBinary(bin, self);
318
+ if (!exe) {
319
+ process.stderr.write(PRODUCT + " guard: no " + bin + " on PATH besides the guard's own\\n");
320
+ process.exit(127);
321
+ }
322
+ for (const s of ["SIGINT", "SIGTERM", "SIGHUP"]) process.on(s, () => {});
323
+ const r = spawnSync(exe, args, { stdio: "inherit" });
324
+ if (r.error) {
325
+ process.stderr.write(PRODUCT + " guard: " + r.error.message + "\\n");
326
+ process.exit(127);
327
+ }
328
+ if (r.signal) {
329
+ process.removeAllListeners(r.signal);
330
+ process.kill(process.pid, r.signal);
331
+ }
332
+ process.exit(r.status ?? 1);
333
+ } else if (mode === "hook" || mode === undefined) {
334
+ let input = "";
335
+ for await (const chunk of process.stdin) input += chunk;
336
+ let event;
337
+ try { event = JSON.parse(input); } catch { process.exit(0); }
338
+ const command = event?.tool_input?.command;
339
+ if (typeof command !== "string") process.exit(0);
340
+ const v = checkCommand(command, typeof event.cwd === "string" && event.cwd ? event.cwd : process.cwd());
341
+ if (v) {
342
+ process.stderr.write(v.message + "\\n");
343
+ process.exit(2);
344
+ }
345
+ process.exit(0);
346
+ }
347
+ }
348
+ `;
349
+ }
350
+ const shq = (s) => `'${s.split("'").join(`'\\''`)}'`;
351
+ /** A wrapper on PATH: the guard's script for one binary. `node` absolute (with `node` on PATH as the fallback) or undefined for `node`; `script` absolute, or relative to the wrapper's directory. */
352
+ export function agentGuardWrapper(product, bin, script, node) {
353
+ const at = isAbsolute(script) ? shq(script) : `"$here/"${shq(script)}`;
354
+ return `#!/bin/sh
355
+ # Written by ${product} guard: refuses what the guard refuses of ${bin}, and runs the next ${bin} on PATH for the rest.
356
+ here=$(cd "$(dirname "$0")" && pwd)
357
+ NODE=${node ? shq(node) : "node"}
358
+ ${node ? `[ -x "$NODE" ] || NODE=node\n` : ""}exec "$NODE" ${at} wrap ${bin} "$here" "$@"
359
+ `;
360
+ }
361
+ /** Claude Code's settings files: the shared one, and the local one it keeps out of commits. */
362
+ export const CLAUDE_SETTINGS_SHARED = ".claude/settings.json";
363
+ export const CLAUDE_SETTINGS_LOCAL = ".claude/settings.local.json";
364
+ /**
365
+ * A Claude Code settings file's text with the guard's PreToolUse hook on
366
+ * Bash, in place of an earlier one (an entry naming `command` or any of
367
+ * `markers`). Everything else in the file is kept. Throws when the file is
368
+ * not a JSON object.
369
+ */
370
+ export function withAgentGuardHook(text, command, at, markers = [], fail = (m) => new Error(m)) {
371
+ let doc = {};
372
+ if (text !== undefined && text.trim() !== "") {
373
+ let parsed;
374
+ try {
375
+ parsed = JSON.parse(text);
376
+ }
377
+ catch (e) {
378
+ throw fail(`${at} is not JSON (${e.message}); fix it, then write the guard again`);
379
+ }
380
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
381
+ throw fail(`${at} is not a JSON object`);
382
+ doc = parsed;
383
+ }
384
+ const hooks = (doc.hooks && typeof doc.hooks === "object" && !Array.isArray(doc.hooks) ? doc.hooks : {});
385
+ const pre = Array.isArray(hooks.PreToolUse) ? hooks.PreToolUse : [];
386
+ const needles = [JSON.stringify(command).slice(1, -1), ...markers.map((m) => JSON.stringify(m).slice(1, -1))];
387
+ const kept = pre.filter((e) => !needles.some((n) => JSON.stringify(e).includes(n)));
388
+ doc.hooks = { ...hooks, PreToolUse: [...kept, { matcher: "Bash", hooks: [{ type: "command", command }] }] };
389
+ return `${JSON.stringify(doc, null, 2)}\n`;
390
+ }
391
+ /** Write the guard: the script, a wrapper per engine, and the hook. */
392
+ export function installAgentGuard(o) {
393
+ const fail = o.fail ?? ((m) => new Error(m));
394
+ const product = o.words.product;
395
+ const repo = resolve(o.repo);
396
+ const git = (args) => {
397
+ const r = spawnSync("git", ["-C", repo, ...args], { encoding: "utf-8" });
398
+ if (r.status !== 0)
399
+ throw fail(`${product} guard runs in a git checkout (git ${args.join(" ")}: ${(r.stderr || "").trim()})`);
400
+ return r.stdout.trim();
401
+ };
402
+ const spec = { words: o.words, rules: o.rules };
403
+ const roots = o.roots ? { roots: o.roots } : {};
404
+ const shown = (p) => (p.startsWith(repo + sep) ? relative(repo, p) : p);
405
+ const written = [];
406
+ const put = (p, text, mode) => {
407
+ mkdirSync(dirname(p), { recursive: true });
408
+ writeFileSync(p, text);
409
+ if (mode !== undefined)
410
+ chmodSync(p, mode);
411
+ written.push(shown(p));
412
+ };
413
+ let script;
414
+ let bin;
415
+ let settings;
416
+ let hookCommand;
417
+ let markers;
418
+ if (o.install === "local") {
419
+ const dir = o.dir ? resolve(repo, o.dir) : join(git(["rev-parse", "--absolute-git-dir"]), product, "guard");
420
+ const node = o.node ?? process.execPath;
421
+ script = join(dir, "guard.mjs");
422
+ bin = join(dir, "bin");
423
+ put(script, agentGuardScript({ ...spec, repo, ...roots }));
424
+ for (const b of Object.keys(o.rules.engines))
425
+ put(join(bin, b), agentGuardWrapper(product, b, script, node), 0o755);
426
+ settings = CLAUDE_SETTINGS_LOCAL;
427
+ hookCommand = `"${node}" "${script}" hook`;
428
+ markers = [`${product}/guard/guard.mjs`];
429
+ }
430
+ else {
431
+ const rel = `.claude/hooks/${product}-guard.mjs`;
432
+ script = join(repo, rel);
433
+ bin = join(repo, `.${product}`, "agent-bin");
434
+ put(script, agentGuardScript({ ...spec, repoFromScript: "../..", ...roots }));
435
+ for (const b of Object.keys(o.rules.engines))
436
+ put(join(bin, b), agentGuardWrapper(product, b, relative(bin, script)), 0o755);
437
+ settings = CLAUDE_SETTINGS_SHARED;
438
+ hookCommand = `node "$CLAUDE_PROJECT_DIR/${rel}" hook`;
439
+ markers = [`${product}-guard.mjs`];
440
+ }
441
+ let hook;
442
+ if (!o.noHook) {
443
+ hook = join(repo, settings);
444
+ put(hook, withAgentGuardHook(existsSync(hook) ? readFileSync(hook, "utf-8") : undefined, hookCommand, settings, markers, fail));
445
+ // The local hook names paths on this machine: keep the file out of commits unless git already ignores it.
446
+ if (o.install === "local" && spawnSync("git", ["-C", repo, "check-ignore", "-q", settings]).status !== 0) {
447
+ const common = git(["rev-parse", "--git-common-dir"]);
448
+ const exclude = join(isAbsolute(common) ? common : resolve(repo, common), "info", "exclude");
449
+ mkdirSync(dirname(exclude), { recursive: true });
450
+ const now = existsSync(exclude) ? readFileSync(exclude, "utf-8") : "";
451
+ writeFileSync(exclude, `${now}${now === "" || now.endsWith("\n") ? "" : "\n"}/${settings}\n`);
452
+ }
453
+ }
454
+ return { script, bin, ...(hook ? { hook } : {}), hookCommand, written, path: `export PATH="${bin}:$PATH"` };
455
+ }