@scopebond/hook 0.5.0 → 0.7.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 +139 -12
- package/dist/cli.d.ts +2 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +667 -69
- package/dist/cli.js.map +1 -1
- package/dist/cloud.d.ts +2 -0
- package/dist/cloud.d.ts.map +1 -1
- package/dist/cloud.js +1 -1
- package/dist/cloud.js.map +1 -1
- package/dist/explain.d.ts +44 -0
- package/dist/explain.d.ts.map +1 -0
- package/dist/explain.js +75 -0
- package/dist/explain.js.map +1 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +9 -3
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +36 -28
- package/dist/init.js.map +1 -1
- package/dist/install.d.ts +63 -3
- package/dist/install.d.ts.map +1 -1
- package/dist/install.js +242 -28
- package/dist/install.js.map +1 -1
- package/dist/map.d.ts +12 -3
- package/dist/map.d.ts.map +1 -1
- package/dist/map.js +765 -60
- package/dist/map.js.map +1 -1
- package/dist/minimize.d.ts +6 -3
- package/dist/minimize.d.ts.map +1 -1
- package/dist/minimize.js +24 -4
- package/dist/minimize.js.map +1 -1
- package/dist/rules.d.ts +51 -0
- package/dist/rules.d.ts.map +1 -0
- package/dist/rules.js +216 -0
- package/dist/rules.js.map +1 -0
- package/dist/runtime-install.d.ts +25 -0
- package/dist/runtime-install.d.ts.map +1 -0
- package/dist/runtime-install.js +106 -0
- package/dist/runtime-install.js.map +1 -0
- package/dist/runtime.d.ts +17 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +122 -11
- package/dist/runtime.js.map +1 -1
- package/dist/shell.d.ts +53 -4
- package/dist/shell.d.ts.map +1 -1
- package/dist/shell.js +737 -81
- package/dist/shell.js.map +1 -1
- package/dist/version.d.ts +5 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +6 -2
- package/dist/version.js.map +1 -1
- package/package.json +3 -3
package/dist/rules.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// Rules you can actually read and change.
|
|
2
|
+
//
|
|
3
|
+
// The site says "set rules in plain terms" and "it is a plain JSON file — edit the
|
|
4
|
+
// limits". What `init` wrote was 6.7 KB of generated regular expression: the `safe-shell`
|
|
5
|
+
// clause alone is a ~700-character case-folded negative lookahead. Nobody edits that, so
|
|
6
|
+
// in practice the starter policy was the *only* policy and "set your own rules" was not
|
|
7
|
+
// true.
|
|
8
|
+
//
|
|
9
|
+
// The fix is not to weaken the patterns — they are careful, and the canonicalization
|
|
10
|
+
// tests exist because bypasses are subtle. It is to stop making the pattern the
|
|
11
|
+
// interface. The lists the patterns are built from live in `.scopebond/rules.json`, a
|
|
12
|
+
// short readable file, and `policy.json` is compiled from it. The regex stays as the
|
|
13
|
+
// compiled form.
|
|
14
|
+
//
|
|
15
|
+
// `compile()` reproduces the shipped starter policy's enforcement patterns exactly —
|
|
16
|
+
// `rules.test.mjs` asserts that against `starterPolicy()`, so a change here cannot
|
|
17
|
+
// quietly alter what is enforced.
|
|
18
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
import { ci, under, named, dir, DESTRUCTIVE } from "./runtime.js";
|
|
21
|
+
export const RULES_FILE = "rules.json";
|
|
22
|
+
const pathPattern = (rule) => rule.kind === "under" ? under(rule.value)
|
|
23
|
+
: rule.kind === "named" ? named(rule.value)
|
|
24
|
+
: rule.kind === "dir" ? dir(rule.value)
|
|
25
|
+
: rule.pattern;
|
|
26
|
+
/** Escape every regular-expression metacharacter, backslash included.
|
|
27
|
+
*
|
|
28
|
+
* A path the user typed is literal text, and this value goes straight into a rule that
|
|
29
|
+
* decides what the agent may touch: if one metacharacter escapes escaping, the rule
|
|
30
|
+
* silently protects something other than what they asked for. The class is therefore
|
|
31
|
+
* complete on its own rather than relying on an earlier step having removed backslashes
|
|
32
|
+
* (CodeQL js/incomplete-sanitization). */
|
|
33
|
+
const escapeRegex = (text) => text.replace(/[\\^$.*+?()[\]{}|/]/g, "\\$&");
|
|
34
|
+
/** A protected-path rule from a plain path the user typed. A trailing `/` (or a path with
|
|
35
|
+
* no dot in its last segment) reads as a directory; anything else as a file name.
|
|
36
|
+
*
|
|
37
|
+
* Normalisation is one obvious step at a time. The previous version folded separator
|
|
38
|
+
* conversion and leading-`./` stripping into a single `^[./\\]+` replace with a callback,
|
|
39
|
+
* which was both hard to read and quadratic on a path of many leading dots
|
|
40
|
+
* (CodeQL js/polynomial-redos). */
|
|
41
|
+
export function pathRuleFor(input) {
|
|
42
|
+
const trimmed = input.trim();
|
|
43
|
+
const withForwardSlashes = trimmed.split("\\").join("/");
|
|
44
|
+
const withoutDotSlash = withForwardSlashes.startsWith("./") ? withForwardSlashes.slice(2) : withForwardSlashes;
|
|
45
|
+
// Drop trailing separators without a quantifier, so the cost is plainly linear.
|
|
46
|
+
let clean = withoutDotSlash;
|
|
47
|
+
while (clean.endsWith("/"))
|
|
48
|
+
clean = clean.slice(0, -1);
|
|
49
|
+
// A trailing slash is an explicit directory; so is a last segment with no extension.
|
|
50
|
+
// Checked by string, not by `/\.[^/]*$/`: that pattern is unanchored at the front, so it
|
|
51
|
+
// retries at every position and goes quadratic on a long path.
|
|
52
|
+
const lastSegment = clean.slice(clean.lastIndexOf("/") + 1);
|
|
53
|
+
const isDirectory = trimmed.endsWith("/") || !lastSegment.includes(".");
|
|
54
|
+
return isDirectory
|
|
55
|
+
? { kind: "under", value: escapeRegex(clean), label: `${clean}/ and everything in it` }
|
|
56
|
+
: { kind: "named", value: escapeRegex(clean), label: clean };
|
|
57
|
+
}
|
|
58
|
+
/** The branch pattern: deny the listed refs, plus the "every branch at once" flags and a
|
|
59
|
+
* push whose destination could not be read. `--tags` alone is allowed. */
|
|
60
|
+
function branchPattern(branches) {
|
|
61
|
+
const exact = branches.filter((b) => !b.endsWith("/*"));
|
|
62
|
+
const prefixes = branches.filter((b) => b.endsWith("/*")).map((b) => b.slice(0, -1));
|
|
63
|
+
const parts = [
|
|
64
|
+
exact.length ? `(?!(?:${exact.map(ci).join("|")})$)` : "",
|
|
65
|
+
...prefixes.map((p) => `(?!${ci(p)})`),
|
|
66
|
+
`(?!-(?!-${ci("tags")}$))`,
|
|
67
|
+
];
|
|
68
|
+
return `^${parts.join("")}.+`;
|
|
69
|
+
}
|
|
70
|
+
const programPattern = (programs) => `^(?!(?:${programs.map(ci).join("|")})(?:${ci("\\.(?:exe|cmd|bat|com|ps1)")})?$).+`;
|
|
71
|
+
const pathsPattern = (rules) => `^${rules.map(pathPattern).join("")}.+`;
|
|
72
|
+
/** A sentence listing what a clause covers, generated from the list rather than written by
|
|
73
|
+
* hand — so it stays true after an edit. The block message quotes this, so a stale
|
|
74
|
+
* description would be a lie told at the worst moment. */
|
|
75
|
+
function sentence(items, limit = 12) {
|
|
76
|
+
const shown = items.slice(0, limit);
|
|
77
|
+
const rest = items.length - shown.length;
|
|
78
|
+
return shown.join(", ") + (rest > 0 ? `, and ${rest} more` : "");
|
|
79
|
+
}
|
|
80
|
+
/** Compile a rule set into the policy the gateway evaluates. */
|
|
81
|
+
export function compile(rules, agentKid) {
|
|
82
|
+
return {
|
|
83
|
+
vocabulary_version: "1.0", policy_id: "coding-agent", version: 1,
|
|
84
|
+
clauses: [
|
|
85
|
+
{
|
|
86
|
+
id: "protect-branches", type: "action_allowlist", mode: "enforce", action_types: ["git.push"],
|
|
87
|
+
param_bounds: { ref: { pattern: branchPattern(rules.protected_branches) } },
|
|
88
|
+
description: `Deny pushes to ${sentence(rules.protected_branches)} (any case, any refspec spelling), pushes of every branch at once (--all, --mirror) and pushes whose destination cannot be read from the command (a git alias, a configured push refspec, send-pack). A tags-only push (--tags) is allowed. Change these in .scopebond/${RULES_FILE}.`,
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
id: "safe-shell", type: "action_allowlist", mode: "enforce", action_types: ["shell.exec"],
|
|
92
|
+
param_bounds: { program: { pattern: programPattern(rules.destructive_programs) } },
|
|
93
|
+
description: `Deny destructive programs (${sentence(rules.destructive_programs, 10)}) in any case and with or without .exe. An empty program — a command that could not be parsed, or whose program is only known at run time ($VAR, $(…), eval of a variable) — is denied. Argument-shaped deletion (find -delete, git clean) is not a program name and is not covered here. Change this list in .scopebond/${RULES_FILE}.`,
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
id: "protect-write", type: "action_allowlist", mode: "enforce", action_types: ["file.write"],
|
|
97
|
+
param_bounds: { path: { pattern: pathsPattern(rules.protected_write) } },
|
|
98
|
+
description: `Allow workspace writes, but never to ${sentence(rules.protected_write.map((r) => r.label))}. Case-insensitive. Change this list in .scopebond/${RULES_FILE}.`,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
id: "protect-read", type: "action_allowlist", mode: "enforce", action_types: ["file.read"],
|
|
102
|
+
param_bounds: { path: { pattern: pathsPattern(rules.protected_read) } },
|
|
103
|
+
description: `Allow workspace reads, but never ${sentence(rules.protected_read.map((r) => r.label))}. Case-insensitive. Change this list in .scopebond/${RULES_FILE}.`,
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
id: "observe-net-mcp", type: "action_allowlist", mode: "monitor",
|
|
107
|
+
action_types: rules.observe,
|
|
108
|
+
description: `Observe ${sentence(rules.observe)} — recorded, not blocked. Add bounds to enforce. Change this list in .scopebond/${RULES_FILE}.`,
|
|
109
|
+
},
|
|
110
|
+
{ id: "keys", type: "key_policy", active_keys: [agentKid], description: "Only the enrolled machine key may sign." },
|
|
111
|
+
],
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** The shipped defaults. Every entry here reproduces one piece of the starter policy's
|
|
115
|
+
* patterns; `rules.test.mjs` pins that equivalence. */
|
|
116
|
+
export function defaultRules() {
|
|
117
|
+
return {
|
|
118
|
+
version: 1,
|
|
119
|
+
protected_branches: ["main", "master", "release/*"],
|
|
120
|
+
destructive_programs: [...DESTRUCTIVE],
|
|
121
|
+
protected_write: [
|
|
122
|
+
{ kind: "under", value: "\\.scopebond", label: "the hook's own policy and keys (.scopebond)" },
|
|
123
|
+
{ kind: "raw", pattern: `(?!(?:.*/)?${ci("\\.claude/settings")})`, label: "Claude Code settings" },
|
|
124
|
+
{ kind: "under", value: "\\.claude/hooks", label: "Claude Code hooks" },
|
|
125
|
+
{ kind: "under", value: "\\.claude/agents", label: "Claude Code agents" },
|
|
126
|
+
{ kind: "raw", pattern: `(?!(?:.*/)?${ci("\\.cursor/hooks")})`, label: "Cursor hook settings" },
|
|
127
|
+
{ kind: "named", value: "\\.codex/hooks\\.json", label: "Codex hook settings" },
|
|
128
|
+
{ kind: "named", value: "\\.codex/config\\.toml", label: "Codex config" },
|
|
129
|
+
{ kind: "named", value: "\\.mcp\\.json", label: ".mcp.json" },
|
|
130
|
+
{ kind: "under", value: "\\.git/hooks", label: "git hooks" },
|
|
131
|
+
{ kind: "named", value: "\\.git/config", label: "git config" },
|
|
132
|
+
{ kind: "under", value: "\\.husky", label: "Husky hooks" },
|
|
133
|
+
{ kind: "under", value: "\\.github/workflows", label: "GitHub workflows" },
|
|
134
|
+
{ kind: "under", value: "\\.github/actions", label: "GitHub actions" },
|
|
135
|
+
{ kind: "named", value: "\\.gitlab-ci\\.yml", label: ".gitlab-ci.yml" },
|
|
136
|
+
{ kind: "named", value: "\\.gitlab-ci\\.yaml", label: ".gitlab-ci.yaml" },
|
|
137
|
+
{ kind: "under", value: "\\.circleci", label: "CircleCI config" },
|
|
138
|
+
{ kind: "named", value: "azure-pipelines\\.yml", label: "azure-pipelines.yml" },
|
|
139
|
+
{ kind: "named", value: "Jenkinsfile", label: "Jenkinsfile" },
|
|
140
|
+
],
|
|
141
|
+
protected_read: [
|
|
142
|
+
{ kind: "under", value: "\\.scopebond", label: "the hook's own policy and keys (.scopebond)" },
|
|
143
|
+
{ kind: "raw", pattern: `(?!.*${ci("\\.(?:key|pem|p12|pfx|jks|keystore)")}$)`, label: "signing keys and key containers (*.key, *.pem, *.p12, *.pfx, *.jks)" },
|
|
144
|
+
{
|
|
145
|
+
kind: "raw",
|
|
146
|
+
pattern: `(?!(?:.*/)?${ci("\\.env")}(?!(?:\\.[^/]*)?\\.(?:${ci("example")}|${ci("sample")}|${ci("template")}|${ci("dist")})$)(?:\\.[^/]*)?$)`,
|
|
147
|
+
label: "environment secret files (.env, .env.* — except .example/.sample/.template/.dist)",
|
|
148
|
+
},
|
|
149
|
+
{ kind: "named", value: "\\.envrc", label: ".envrc" },
|
|
150
|
+
{
|
|
151
|
+
kind: "raw",
|
|
152
|
+
pattern: `(?!(?:.*/)?${ci("\\.ssh")}(?:$|/(?!.*${ci("\\.pub")}$)(?!${ci("known_hosts")}$)(?!${ci("config")}$)))`,
|
|
153
|
+
label: "SSH private keys and the .ssh directory (public keys, known_hosts and config are allowed)",
|
|
154
|
+
},
|
|
155
|
+
{ kind: "raw", pattern: `(?!(?:.*/)?${ci("\\.aws")}(?:$|/(?!${ci("config")}$)))`, label: "AWS credentials (.aws, except .aws/config)" },
|
|
156
|
+
{ kind: "named", value: "\\.npmrc", label: ".npmrc" },
|
|
157
|
+
{ kind: "named", value: "\\.pypirc", label: ".pypirc" },
|
|
158
|
+
{ kind: "named", value: "\\.netrc", label: ".netrc" },
|
|
159
|
+
{ kind: "named", value: "_netrc", label: "_netrc" },
|
|
160
|
+
{ kind: "named", value: "\\.git-credentials", label: ".git-credentials" },
|
|
161
|
+
{ kind: "dir", value: "\\.kube", label: "the .kube directory" },
|
|
162
|
+
{ kind: "named", value: "\\.kube/config", label: ".kube/config" },
|
|
163
|
+
{ kind: "dir", value: "\\.docker", label: "the .docker directory" },
|
|
164
|
+
{ kind: "named", value: "\\.docker/config\\.json", label: ".docker/config.json" },
|
|
165
|
+
{ kind: "under", value: "\\.config/gcloud", label: "gcloud credentials" },
|
|
166
|
+
{ kind: "under", value: "\\.azure", label: "Azure credentials" },
|
|
167
|
+
{ kind: "under", value: "\\.gnupg", label: "GnuPG keys" },
|
|
168
|
+
{ kind: "dir", value: "\\.config/gh", label: "the GitHub CLI config directory" },
|
|
169
|
+
{ kind: "named", value: "\\.config/gh/hosts\\.yml", label: "the GitHub CLI's hosts.yml" },
|
|
170
|
+
{ kind: "named", value: "\\.claude/\\.credentials\\.json", label: "Claude Code's .credentials.json" },
|
|
171
|
+
],
|
|
172
|
+
observe: ["net.fetch", "mcp.tool.call"],
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
export const rulesPath = (dir) => join(dir, RULES_FILE);
|
|
176
|
+
export function loadRules(configDir) {
|
|
177
|
+
const file = rulesPath(configDir);
|
|
178
|
+
if (!existsSync(file))
|
|
179
|
+
return null;
|
|
180
|
+
try {
|
|
181
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
182
|
+
if (parsed?.version !== 1 || !Array.isArray(parsed.protected_branches))
|
|
183
|
+
return null;
|
|
184
|
+
return parsed;
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
export function saveRules(configDir, rules) {
|
|
191
|
+
const file = rulesPath(configDir);
|
|
192
|
+
writeFileSync(file, `${JSON.stringify(rules, null, 2)}\n`);
|
|
193
|
+
return file;
|
|
194
|
+
}
|
|
195
|
+
/** Plain English, for `scopebond-hook rules`. */
|
|
196
|
+
export function describeRules(rules) {
|
|
197
|
+
const lines = [];
|
|
198
|
+
lines.push("Blocked before it runs:");
|
|
199
|
+
lines.push("");
|
|
200
|
+
lines.push(` pushes to ${rules.protected_branches.join(", ")}`);
|
|
201
|
+
lines.push(` and any push whose destination cannot be read`);
|
|
202
|
+
lines.push(` programs ${rules.destructive_programs.join(", ")}`);
|
|
203
|
+
lines.push("");
|
|
204
|
+
lines.push(` writes to ${rules.protected_write.length} protected location(s):`);
|
|
205
|
+
for (const rule of rules.protected_write)
|
|
206
|
+
lines.push(` ${rule.label}`);
|
|
207
|
+
lines.push("");
|
|
208
|
+
lines.push(` reads of ${rules.protected_read.length} protected location(s):`);
|
|
209
|
+
for (const rule of rules.protected_read)
|
|
210
|
+
lines.push(` ${rule.label}`);
|
|
211
|
+
lines.push("");
|
|
212
|
+
lines.push("Recorded, not blocked:");
|
|
213
|
+
lines.push(` ${rules.observe.join(", ")}`);
|
|
214
|
+
return lines.join("\n");
|
|
215
|
+
}
|
|
216
|
+
//# sourceMappingURL=rules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules.js","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,EAAE;AACF,mFAAmF;AACnF,0FAA0F;AAC1F,yFAAyF;AACzF,wFAAwF;AACxF,QAAQ;AACR,EAAE;AACF,qFAAqF;AACrF,gFAAgF;AAChF,sFAAsF;AACtF,qFAAqF;AACrF,iBAAiB;AACjB,EAAE;AACF,qFAAqF;AACrF,mFAAmF;AACnF,kCAAkC;AAElC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAuBlE,MAAM,CAAC,MAAM,UAAU,GAAG,YAAY,CAAC;AAEvC,MAAM,WAAW,GAAG,CAAC,IAAc,EAAU,EAAE,CAC7C,IAAI,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;IACvC,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;QAC3C,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,KAA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3B,KAAK,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;IACtC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACf,KAAK,CAAC,IAAI,CAAC,wBAAwB,KAAK,CAAC,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC1E,KAAK,CAAC,IAAI,CAAC,oEAAoE,CAAC,CAAC;IACjF,KAAK,CAAC,IAAI,CAAC,wBAAwB,KAAK,CAAC,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC5E,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACf,KAAK,CAAC,IAAI,CAAC,wBAAwB,KAAK,CAAC,eAAe,CAAC,MAAM,yBAAyB,CAAC,CAAC;IAC1F,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,eAAe;QAAE,KAAK,CAAC,IAAI,CAAC,0BAA0B,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;IAC7F,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACf,KAAK,CAAC,IAAI,CAAC,wBAAwB,KAAK,CAAC,cAAc,CAAC,MAAM,yBAAyB,CAAC,CAAC;IACzF,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,cAAc;QAAE,KAAK,CAAC,IAAI,CAAC,0BAA0B,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;IAC5F,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACf,KAAK,CAAC,IAAI,CAAC,wBAAwB,CAAC,CAAC;IACrC,KAAK,CAAC,IAAI,CAAC,KAAK,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC5C,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC","sourcesContent":["// Rules you can actually read and change.\n//\n// The site says \"set rules in plain terms\" and \"it is a plain JSON file — edit the\n// limits\". What `init` wrote was 6.7 KB of generated regular expression: the `safe-shell`\n// clause alone is a ~700-character case-folded negative lookahead. Nobody edits that, so\n// in practice the starter policy was the *only* policy and \"set your own rules\" was not\n// true.\n//\n// The fix is not to weaken the patterns — they are careful, and the canonicalization\n// tests exist because bypasses are subtle. It is to stop making the pattern the\n// interface. The lists the patterns are built from live in `.scopebond/rules.json`, a\n// short readable file, and `policy.json` is compiled from it. The regex stays as the\n// compiled form.\n//\n// `compile()` reproduces the shipped starter policy's enforcement patterns exactly —\n// `rules.test.mjs` asserts that against `starterPolicy()`, so a change here cannot\n// quietly alter what is enforced.\n\nimport { existsSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { ci, under, named, dir, DESTRUCTIVE } from \"./runtime.js\";\n\n/** One protected location. `under`/`named`/`dir` are the readable, editable shapes;\n * `raw` carries the few patterns with bespoke exceptions (`.env` templates are allowed,\n * `.ssh/config` and `*.pub` are allowed) that no simple shape expresses. */\nexport type PathRule =\n | { kind: \"under\"; value: string; label: string }\n | { kind: \"named\"; value: string; label: string }\n | { kind: \"dir\"; value: string; label: string }\n | { kind: \"raw\"; pattern: string; label: string };\n\nexport interface RuleSet {\n version: 1;\n /** Refs an agent may not push to. `release/*` means the prefix `release/`. */\n protected_branches: string[];\n /** Programs an agent may not run, in any case and with or without .exe. */\n destructive_programs: string[];\n protected_write: PathRule[];\n protected_read: PathRule[];\n /** Action types recorded but not blocked. */\n observe: string[];\n}\n\nexport const RULES_FILE = \"rules.json\";\n\nconst pathPattern = (rule: PathRule): string =>\n rule.kind === \"under\" ? under(rule.value)\n : rule.kind === \"named\" ? named(rule.value)\n : rule.kind === \"dir\" ? dir(rule.value)\n : rule.pattern;\n\n/** Escape every regular-expression metacharacter, backslash included.\n *\n * A path the user typed is literal text, and this value goes straight into a rule that\n * decides what the agent may touch: if one metacharacter escapes escaping, the rule\n * silently protects something other than what they asked for. The class is therefore\n * complete on its own rather than relying on an earlier step having removed backslashes\n * (CodeQL js/incomplete-sanitization). */\nconst escapeRegex = (text: string): string => text.replace(/[\\\\^$.*+?()[\\]{}|/]/g, \"\\\\$&\");\n\n/** A protected-path rule from a plain path the user typed. A trailing `/` (or a path with\n * no dot in its last segment) reads as a directory; anything else as a file name.\n *\n * Normalisation is one obvious step at a time. The previous version folded separator\n * conversion and leading-`./` stripping into a single `^[./\\\\]+` replace with a callback,\n * which was both hard to read and quadratic on a path of many leading dots\n * (CodeQL js/polynomial-redos). */\nexport function pathRuleFor(input: string): PathRule {\n const trimmed = input.trim();\n const withForwardSlashes = trimmed.split(\"\\\\\").join(\"/\");\n const withoutDotSlash = withForwardSlashes.startsWith(\"./\") ? withForwardSlashes.slice(2) : withForwardSlashes;\n // Drop trailing separators without a quantifier, so the cost is plainly linear.\n let clean = withoutDotSlash;\n while (clean.endsWith(\"/\")) clean = clean.slice(0, -1);\n // A trailing slash is an explicit directory; so is a last segment with no extension.\n // Checked by string, not by `/\\.[^/]*$/`: that pattern is unanchored at the front, so it\n // retries at every position and goes quadratic on a long path.\n const lastSegment = clean.slice(clean.lastIndexOf(\"/\") + 1);\n const isDirectory = trimmed.endsWith(\"/\") || !lastSegment.includes(\".\");\n return isDirectory\n ? { kind: \"under\", value: escapeRegex(clean), label: `${clean}/ and everything in it` }\n : { kind: \"named\", value: escapeRegex(clean), label: clean };\n}\n\n/** The branch pattern: deny the listed refs, plus the \"every branch at once\" flags and a\n * push whose destination could not be read. `--tags` alone is allowed. */\nfunction branchPattern(branches: string[]): string {\n const exact = branches.filter((b) => !b.endsWith(\"/*\"));\n const prefixes = branches.filter((b) => b.endsWith(\"/*\")).map((b) => b.slice(0, -1));\n const parts = [\n exact.length ? `(?!(?:${exact.map(ci).join(\"|\")})$)` : \"\",\n ...prefixes.map((p) => `(?!${ci(p)})`),\n `(?!-(?!-${ci(\"tags\")}$))`,\n ];\n return `^${parts.join(\"\")}.+`;\n}\n\nconst programPattern = (programs: string[]): string =>\n `^(?!(?:${programs.map(ci).join(\"|\")})(?:${ci(\"\\\\.(?:exe|cmd|bat|com|ps1)\")})?$).+`;\n\nconst pathsPattern = (rules: PathRule[]): string => `^${rules.map(pathPattern).join(\"\")}.+`;\n\n/** A sentence listing what a clause covers, generated from the list rather than written by\n * hand — so it stays true after an edit. The block message quotes this, so a stale\n * description would be a lie told at the worst moment. */\nfunction sentence(items: string[], limit = 12): string {\n const shown = items.slice(0, limit);\n const rest = items.length - shown.length;\n return shown.join(\", \") + (rest > 0 ? `, and ${rest} more` : \"\");\n}\n\n/** Compile a rule set into the policy the gateway evaluates. */\nexport function compile(rules: RuleSet, agentKid: string): Record<string, unknown> {\n return {\n vocabulary_version: \"1.0\", policy_id: \"coding-agent\", version: 1,\n clauses: [\n {\n id: \"protect-branches\", type: \"action_allowlist\", mode: \"enforce\", action_types: [\"git.push\"],\n param_bounds: { ref: { pattern: branchPattern(rules.protected_branches) } },\n description: `Deny pushes to ${sentence(rules.protected_branches)} (any case, any refspec spelling), pushes of every branch at once (--all, --mirror) and pushes whose destination cannot be read from the command (a git alias, a configured push refspec, send-pack). A tags-only push (--tags) is allowed. Change these in .scopebond/${RULES_FILE}.`,\n },\n {\n id: \"safe-shell\", type: \"action_allowlist\", mode: \"enforce\", action_types: [\"shell.exec\"],\n param_bounds: { program: { pattern: programPattern(rules.destructive_programs) } },\n description: `Deny destructive programs (${sentence(rules.destructive_programs, 10)}) in any case and with or without .exe. An empty program — a command that could not be parsed, or whose program is only known at run time ($VAR, $(…), eval of a variable) — is denied. Argument-shaped deletion (find -delete, git clean) is not a program name and is not covered here. Change this list in .scopebond/${RULES_FILE}.`,\n },\n {\n id: \"protect-write\", type: \"action_allowlist\", mode: \"enforce\", action_types: [\"file.write\"],\n param_bounds: { path: { pattern: pathsPattern(rules.protected_write) } },\n description: `Allow workspace writes, but never to ${sentence(rules.protected_write.map((r) => r.label))}. Case-insensitive. Change this list in .scopebond/${RULES_FILE}.`,\n },\n {\n id: \"protect-read\", type: \"action_allowlist\", mode: \"enforce\", action_types: [\"file.read\"],\n param_bounds: { path: { pattern: pathsPattern(rules.protected_read) } },\n description: `Allow workspace reads, but never ${sentence(rules.protected_read.map((r) => r.label))}. Case-insensitive. Change this list in .scopebond/${RULES_FILE}.`,\n },\n {\n id: \"observe-net-mcp\", type: \"action_allowlist\", mode: \"monitor\",\n action_types: rules.observe,\n description: `Observe ${sentence(rules.observe)} — recorded, not blocked. Add bounds to enforce. Change this list in .scopebond/${RULES_FILE}.`,\n },\n { id: \"keys\", type: \"key_policy\", active_keys: [agentKid], description: \"Only the enrolled machine key may sign.\" },\n ],\n };\n}\n\n/** The shipped defaults. Every entry here reproduces one piece of the starter policy's\n * patterns; `rules.test.mjs` pins that equivalence. */\nexport function defaultRules(): RuleSet {\n return {\n version: 1,\n protected_branches: [\"main\", \"master\", \"release/*\"],\n destructive_programs: [...DESTRUCTIVE],\n protected_write: [\n { kind: \"under\", value: \"\\\\.scopebond\", label: \"the hook's own policy and keys (.scopebond)\" },\n { kind: \"raw\", pattern: `(?!(?:.*/)?${ci(\"\\\\.claude/settings\")})`, label: \"Claude Code settings\" },\n { kind: \"under\", value: \"\\\\.claude/hooks\", label: \"Claude Code hooks\" },\n { kind: \"under\", value: \"\\\\.claude/agents\", label: \"Claude Code agents\" },\n { kind: \"raw\", pattern: `(?!(?:.*/)?${ci(\"\\\\.cursor/hooks\")})`, label: \"Cursor hook settings\" },\n { kind: \"named\", value: \"\\\\.codex/hooks\\\\.json\", label: \"Codex hook settings\" },\n { kind: \"named\", value: \"\\\\.codex/config\\\\.toml\", label: \"Codex config\" },\n { kind: \"named\", value: \"\\\\.mcp\\\\.json\", label: \".mcp.json\" },\n { kind: \"under\", value: \"\\\\.git/hooks\", label: \"git hooks\" },\n { kind: \"named\", value: \"\\\\.git/config\", label: \"git config\" },\n { kind: \"under\", value: \"\\\\.husky\", label: \"Husky hooks\" },\n { kind: \"under\", value: \"\\\\.github/workflows\", label: \"GitHub workflows\" },\n { kind: \"under\", value: \"\\\\.github/actions\", label: \"GitHub actions\" },\n { kind: \"named\", value: \"\\\\.gitlab-ci\\\\.yml\", label: \".gitlab-ci.yml\" },\n { kind: \"named\", value: \"\\\\.gitlab-ci\\\\.yaml\", label: \".gitlab-ci.yaml\" },\n { kind: \"under\", value: \"\\\\.circleci\", label: \"CircleCI config\" },\n { kind: \"named\", value: \"azure-pipelines\\\\.yml\", label: \"azure-pipelines.yml\" },\n { kind: \"named\", value: \"Jenkinsfile\", label: \"Jenkinsfile\" },\n ],\n protected_read: [\n { kind: \"under\", value: \"\\\\.scopebond\", label: \"the hook's own policy and keys (.scopebond)\" },\n { kind: \"raw\", pattern: `(?!.*${ci(\"\\\\.(?:key|pem|p12|pfx|jks|keystore)\")}$)`, label: \"signing keys and key containers (*.key, *.pem, *.p12, *.pfx, *.jks)\" },\n {\n kind: \"raw\",\n pattern: `(?!(?:.*/)?${ci(\"\\\\.env\")}(?!(?:\\\\.[^/]*)?\\\\.(?:${ci(\"example\")}|${ci(\"sample\")}|${ci(\"template\")}|${ci(\"dist\")})$)(?:\\\\.[^/]*)?$)`,\n label: \"environment secret files (.env, .env.* — except .example/.sample/.template/.dist)\",\n },\n { kind: \"named\", value: \"\\\\.envrc\", label: \".envrc\" },\n {\n kind: \"raw\",\n pattern: `(?!(?:.*/)?${ci(\"\\\\.ssh\")}(?:$|/(?!.*${ci(\"\\\\.pub\")}$)(?!${ci(\"known_hosts\")}$)(?!${ci(\"config\")}$)))`,\n label: \"SSH private keys and the .ssh directory (public keys, known_hosts and config are allowed)\",\n },\n { kind: \"raw\", pattern: `(?!(?:.*/)?${ci(\"\\\\.aws\")}(?:$|/(?!${ci(\"config\")}$)))`, label: \"AWS credentials (.aws, except .aws/config)\" },\n { kind: \"named\", value: \"\\\\.npmrc\", label: \".npmrc\" },\n { kind: \"named\", value: \"\\\\.pypirc\", label: \".pypirc\" },\n { kind: \"named\", value: \"\\\\.netrc\", label: \".netrc\" },\n { kind: \"named\", value: \"_netrc\", label: \"_netrc\" },\n { kind: \"named\", value: \"\\\\.git-credentials\", label: \".git-credentials\" },\n { kind: \"dir\", value: \"\\\\.kube\", label: \"the .kube directory\" },\n { kind: \"named\", value: \"\\\\.kube/config\", label: \".kube/config\" },\n { kind: \"dir\", value: \"\\\\.docker\", label: \"the .docker directory\" },\n { kind: \"named\", value: \"\\\\.docker/config\\\\.json\", label: \".docker/config.json\" },\n { kind: \"under\", value: \"\\\\.config/gcloud\", label: \"gcloud credentials\" },\n { kind: \"under\", value: \"\\\\.azure\", label: \"Azure credentials\" },\n { kind: \"under\", value: \"\\\\.gnupg\", label: \"GnuPG keys\" },\n { kind: \"dir\", value: \"\\\\.config/gh\", label: \"the GitHub CLI config directory\" },\n { kind: \"named\", value: \"\\\\.config/gh/hosts\\\\.yml\", label: \"the GitHub CLI's hosts.yml\" },\n { kind: \"named\", value: \"\\\\.claude/\\\\.credentials\\\\.json\", label: \"Claude Code's .credentials.json\" },\n ],\n observe: [\"net.fetch\", \"mcp.tool.call\"],\n };\n}\n\nexport const rulesPath = (dir: string): string => join(dir, RULES_FILE);\n\nexport function loadRules(configDir: string): RuleSet | null {\n const file = rulesPath(configDir);\n if (!existsSync(file)) return null;\n try {\n const parsed = JSON.parse(readFileSync(file, \"utf8\")) as RuleSet;\n if (parsed?.version !== 1 || !Array.isArray(parsed.protected_branches)) return null;\n return parsed;\n } catch { return null; }\n}\n\nexport function saveRules(configDir: string, rules: RuleSet): string {\n const file = rulesPath(configDir);\n writeFileSync(file, `${JSON.stringify(rules, null, 2)}\\n`);\n return file;\n}\n\n/** Plain English, for `scopebond-hook rules`. */\nexport function describeRules(rules: RuleSet): string {\n const lines: string[] = [];\n lines.push(\"Blocked before it runs:\");\n lines.push(\"\");\n lines.push(` pushes to ${rules.protected_branches.join(\", \")}`);\n lines.push(` and any push whose destination cannot be read`);\n lines.push(` programs ${rules.destructive_programs.join(\", \")}`);\n lines.push(\"\");\n lines.push(` writes to ${rules.protected_write.length} protected location(s):`);\n for (const rule of rules.protected_write) lines.push(` ${rule.label}`);\n lines.push(\"\");\n lines.push(` reads of ${rules.protected_read.length} protected location(s):`);\n for (const rule of rules.protected_read) lines.push(` ${rule.label}`);\n lines.push(\"\");\n lines.push(\"Recorded, not blocked:\");\n lines.push(` ${rules.observe.join(\", \")}`);\n return lines.join(\"\\n\");\n}\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Where pinned copies live: one directory per hook version. */
|
|
2
|
+
export declare function runtimeRoot(): string;
|
|
3
|
+
export declare function runtimeDirFor(version: string): string;
|
|
4
|
+
/** The CLI inside a pinned copy. */
|
|
5
|
+
export declare function pinnedCliPath(version: string): string;
|
|
6
|
+
/** True when a path sits in npm's throwaway `npx` cache, which npm may clear at any
|
|
7
|
+
* time. Anything else — a project `node_modules`, a global install, a pinned copy —
|
|
8
|
+
* is durable enough to reference from a config file. */
|
|
9
|
+
export declare function isEphemeralPath(file: string): boolean;
|
|
10
|
+
/** Walk up from `dist/cli.js` to the `node_modules` directory that contains the whole
|
|
11
|
+
* materialised dependency tree (`node_modules/@scopebond/hook/dist/cli.js` → four up).
|
|
12
|
+
* Returns null if the layout is not what we expect, rather than copying the wrong thing. */
|
|
13
|
+
export declare function nodeModulesRootOf(cliFile: string): string | null;
|
|
14
|
+
export interface PinResult {
|
|
15
|
+
/** The CLI path to pin, or null to keep the `npx` form. */
|
|
16
|
+
cli: string | null;
|
|
17
|
+
/** Why, in one phrase, for the line `init` prints. */
|
|
18
|
+
how: "already-durable" | "pinned" | "reused" | "unavailable";
|
|
19
|
+
}
|
|
20
|
+
/** Make sure a durable copy of this CLI exists and say which path to pin.
|
|
21
|
+
*
|
|
22
|
+
* Never throws: every failure degrades to `{ cli: null }` so the caller falls back
|
|
23
|
+
* to `npx`, which is slower but always starts. */
|
|
24
|
+
export declare function ensureDurableRuntime(cliFile: string, version: string): PinResult;
|
|
25
|
+
//# sourceMappingURL=runtime-install.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime-install.d.ts","sourceRoot":"","sources":["../src/runtime-install.ts"],"names":[],"mappings":"AAuBA,gEAAgE;AAChE,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,oCAAoC;AACpC,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAErD;AAED;;yDAEyD;AACzD,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAErD;AAED;;6FAE6F;AAC7F,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAShE;AAeD,MAAM,WAAW,SAAS;IACxB,2DAA2D;IAC3D,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,sDAAsD;IACtD,GAAG,EAAE,iBAAiB,GAAG,QAAQ,GAAG,QAAQ,GAAG,aAAa,CAAC;CAC9D;AAED;;;mDAGmD;AACnD,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,CA0BhF"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Pinning the hook to a stable path, so the harness does not pay `npx` on every
|
|
2
|
+
// tool call.
|
|
3
|
+
//
|
|
4
|
+
// `init` installs a hook command into the agent's config, and that command runs
|
|
5
|
+
// once per tool call — hundreds of times an hour. The obvious command,
|
|
6
|
+
// `npx -y @scopebond/hook@<version> claude`, costs about 830 ms a call on a warm
|
|
7
|
+
// cache; running the same CLI directly costs about 110 ms. The ~720 ms difference
|
|
8
|
+
// is npm resolving a package that is already on disk.
|
|
9
|
+
//
|
|
10
|
+
// The reason `init` used `npx` anyway is sound: a hook entry that cannot start is
|
|
11
|
+
// worse than no hook, because a harness can read the failure as "no hook", and an
|
|
12
|
+
// absolute path into npm's `_npx` cache is not durable — npm may clear it.
|
|
13
|
+
//
|
|
14
|
+
// So: copy the tree npx already materialised into the Scopebond home once per
|
|
15
|
+
// machine and per version, and pin *that*. It is durable, self-contained, needs no
|
|
16
|
+
// network, lives under the home the governed agent's own policy protects, and
|
|
17
|
+
// `doctor` re-checks that the path still resolves. If anything here fails, the
|
|
18
|
+
// caller keeps the `npx` form — slow beats broken.
|
|
19
|
+
import { existsSync, mkdirSync, cpSync, rmSync, renameSync, readdirSync } from "node:fs";
|
|
20
|
+
import { dirname, join, sep } from "node:path";
|
|
21
|
+
import { userHome } from "./install.js";
|
|
22
|
+
/** Where pinned copies live: one directory per hook version. */
|
|
23
|
+
export function runtimeRoot() {
|
|
24
|
+
return join(userHome(), "runtime");
|
|
25
|
+
}
|
|
26
|
+
export function runtimeDirFor(version) {
|
|
27
|
+
return join(runtimeRoot(), version);
|
|
28
|
+
}
|
|
29
|
+
/** The CLI inside a pinned copy. */
|
|
30
|
+
export function pinnedCliPath(version) {
|
|
31
|
+
return join(runtimeDirFor(version), "node_modules", "@scopebond", "hook", "dist", "cli.js");
|
|
32
|
+
}
|
|
33
|
+
/** True when a path sits in npm's throwaway `npx` cache, which npm may clear at any
|
|
34
|
+
* time. Anything else — a project `node_modules`, a global install, a pinned copy —
|
|
35
|
+
* is durable enough to reference from a config file. */
|
|
36
|
+
export function isEphemeralPath(file) {
|
|
37
|
+
return `${sep}${file.replace(/[\\/]/g, sep)}${sep}`.includes(`${sep}_npx${sep}`);
|
|
38
|
+
}
|
|
39
|
+
/** Walk up from `dist/cli.js` to the `node_modules` directory that contains the whole
|
|
40
|
+
* materialised dependency tree (`node_modules/@scopebond/hook/dist/cli.js` → four up).
|
|
41
|
+
* Returns null if the layout is not what we expect, rather than copying the wrong thing. */
|
|
42
|
+
export function nodeModulesRootOf(cliFile) {
|
|
43
|
+
let dir = dirname(cliFile); // .../dist
|
|
44
|
+
for (let i = 0; i < 6; i += 1) {
|
|
45
|
+
const parent = dirname(dir);
|
|
46
|
+
if (parent === dir)
|
|
47
|
+
return null;
|
|
48
|
+
if (parent.endsWith(`${sep}node_modules`) || parent.endsWith("/node_modules"))
|
|
49
|
+
return parent;
|
|
50
|
+
dir = parent;
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
/** A pinned copy is usable only if the CLI and its sibling packages are all there —
|
|
55
|
+
* a half-copied tree would fail closed on every tool call. */
|
|
56
|
+
function pinnedCopyIsComplete(version) {
|
|
57
|
+
const cli = pinnedCliPath(version);
|
|
58
|
+
if (!existsSync(cli))
|
|
59
|
+
return false;
|
|
60
|
+
const scope = join(runtimeDirFor(version), "node_modules", "@scopebond");
|
|
61
|
+
try {
|
|
62
|
+
// The hook cannot run without the gateway and sdk it imports at startup.
|
|
63
|
+
const names = new Set(readdirSync(scope));
|
|
64
|
+
return names.has("hook") && names.has("gateway") && names.has("sdk");
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** Make sure a durable copy of this CLI exists and say which path to pin.
|
|
71
|
+
*
|
|
72
|
+
* Never throws: every failure degrades to `{ cli: null }` so the caller falls back
|
|
73
|
+
* to `npx`, which is slower but always starts. */
|
|
74
|
+
export function ensureDurableRuntime(cliFile, version) {
|
|
75
|
+
try {
|
|
76
|
+
if (!existsSync(cliFile))
|
|
77
|
+
return { cli: null, how: "unavailable" };
|
|
78
|
+
// Already installed somewhere npm will not delete — pin it as it stands.
|
|
79
|
+
if (!isEphemeralPath(cliFile))
|
|
80
|
+
return { cli: cliFile, how: "already-durable" };
|
|
81
|
+
if (pinnedCopyIsComplete(version))
|
|
82
|
+
return { cli: pinnedCliPath(version), how: "reused" };
|
|
83
|
+
const source = nodeModulesRootOf(cliFile);
|
|
84
|
+
if (!source || !existsSync(source))
|
|
85
|
+
return { cli: null, how: "unavailable" };
|
|
86
|
+
const target = runtimeDirFor(version);
|
|
87
|
+
// Copy to a staging directory and move it into place, so an interrupted copy
|
|
88
|
+
// never leaves a partial tree that looks complete.
|
|
89
|
+
const staging = `${target}.incoming-${process.pid}`;
|
|
90
|
+
rmSync(staging, { recursive: true, force: true });
|
|
91
|
+
mkdirSync(staging, { recursive: true });
|
|
92
|
+
cpSync(source, join(staging, "node_modules"), { recursive: true, dereference: true });
|
|
93
|
+
rmSync(target, { recursive: true, force: true });
|
|
94
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
95
|
+
renameSync(staging, target);
|
|
96
|
+
if (!pinnedCopyIsComplete(version)) {
|
|
97
|
+
rmSync(target, { recursive: true, force: true });
|
|
98
|
+
return { cli: null, how: "unavailable" };
|
|
99
|
+
}
|
|
100
|
+
return { cli: pinnedCliPath(version), how: "pinned" };
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return { cli: null, how: "unavailable" };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=runtime-install.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime-install.js","sourceRoot":"","sources":["../src/runtime-install.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,aAAa;AACb,EAAE;AACF,gFAAgF;AAChF,uEAAuE;AACvE,iFAAiF;AACjF,kFAAkF;AAClF,sDAAsD;AACtD,EAAE;AACF,kFAAkF;AAClF,kFAAkF;AAClF,2EAA2E;AAC3E,EAAE;AACF,8EAA8E;AAC9E,mFAAmF;AACnF,8EAA8E;AAC9E,+EAA+E;AAC/E,mDAAmD;AAEnD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACzF,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,gEAAgE;AAChE,MAAM,UAAU,WAAW;IACzB,OAAO,IAAI,CAAC,QAAQ,EAAE,EAAE,SAAS,CAAC,CAAC;AACrC,CAAC;AAED,MAAM,UAAU,aAAa,CAA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,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QAClD,SAAS,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACxC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,EAAE,cAAc,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC;QACtF,MAAM,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACjD,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAChD,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC5B,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC,EAAE,CAAC;YACnC,MAAM,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;YACjD,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,aAAa,EAAE,CAAC;QAC3C,CAAC;QACD,OAAO,EAAE,GAAG,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC;IACxD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,aAAa,EAAE,CAAC;IAC3C,CAAC;AACH,CAAC","sourcesContent":["// Pinning the hook to a stable path, so the harness does not pay `npx` on every\n// tool call.\n//\n// `init` installs a hook command into the agent's config, and that command runs\n// once per tool call — hundreds of times an hour. The obvious command,\n// `npx -y @scopebond/hook@<version> claude`, costs about 830 ms a call on a warm\n// cache; running the same CLI directly costs about 110 ms. The ~720 ms difference\n// is npm resolving a package that is already on disk.\n//\n// The reason `init` used `npx` anyway is sound: a hook entry that cannot start is\n// worse than no hook, because a harness can read the failure as \"no hook\", and an\n// absolute path into npm's `_npx` cache is not durable — npm may clear it.\n//\n// So: copy the tree npx already materialised into the Scopebond home once per\n// machine and per version, and pin *that*. It is durable, self-contained, needs no\n// network, lives under the home the governed agent's own policy protects, and\n// `doctor` re-checks that the path still resolves. If anything here fails, the\n// caller keeps the `npx` form — slow beats broken.\n\nimport { existsSync, mkdirSync, cpSync, rmSync, renameSync, readdirSync } from \"node:fs\";\nimport { dirname, join, sep } from \"node:path\";\nimport { userHome } from \"./install.js\";\n\n/** Where pinned copies live: one directory per hook version. */\nexport function runtimeRoot(): string {\n return join(userHome(), \"runtime\");\n}\n\nexport function runtimeDirFor(version: string): string {\n return join(runtimeRoot(), version);\n}\n\n/** The CLI inside a pinned copy. */\nexport function pinnedCliPath(version: string): string {\n return join(runtimeDirFor(version), \"node_modules\", \"@scopebond\", \"hook\", \"dist\", \"cli.js\");\n}\n\n/** True when a path sits in npm's throwaway `npx` cache, which npm may clear at any\n * time. Anything else — a project `node_modules`, a global install, a pinned copy —\n * is durable enough to reference from a config file. */\nexport function isEphemeralPath(file: string): boolean {\n return `${sep}${file.replace(/[\\\\/]/g, sep)}${sep}`.includes(`${sep}_npx${sep}`);\n}\n\n/** Walk up from `dist/cli.js` to the `node_modules` directory that contains the whole\n * materialised dependency tree (`node_modules/@scopebond/hook/dist/cli.js` → four up).\n * Returns null if the layout is not what we expect, rather than copying the wrong thing. */\nexport function nodeModulesRootOf(cliFile: string): string | null {\n let dir = dirname(cliFile); // .../dist\n for (let i = 0; i < 6; i += 1) {\n const parent = dirname(dir);\n if (parent === dir) return null;\n if (parent.endsWith(`${sep}node_modules`) || parent.endsWith(\"/node_modules\")) return parent;\n dir = parent;\n }\n return null;\n}\n\n/** A pinned copy is usable only if the CLI and its sibling packages are all there —\n * a half-copied tree would fail closed on every tool call. */\nfunction pinnedCopyIsComplete(version: string): boolean {\n const cli = pinnedCliPath(version);\n if (!existsSync(cli)) return false;\n const scope = join(runtimeDirFor(version), \"node_modules\", \"@scopebond\");\n try {\n // The hook cannot run without the gateway and sdk it imports at startup.\n const names = new Set(readdirSync(scope));\n return names.has(\"hook\") && names.has(\"gateway\") && names.has(\"sdk\");\n } catch { return false; }\n}\n\nexport interface PinResult {\n /** The CLI path to pin, or null to keep the `npx` form. */\n cli: string | null;\n /** Why, in one phrase, for the line `init` prints. */\n how: \"already-durable\" | \"pinned\" | \"reused\" | \"unavailable\";\n}\n\n/** Make sure a durable copy of this CLI exists and say which path to pin.\n *\n * Never throws: every failure degrades to `{ cli: null }` so the caller falls back\n * to `npx`, which is slower but always starts. */\nexport function ensureDurableRuntime(cliFile: string, version: string): PinResult {\n try {\n if (!existsSync(cliFile)) return { cli: null, how: \"unavailable\" };\n // Already installed somewhere npm will not delete — pin it as it stands.\n if (!isEphemeralPath(cliFile)) return { cli: cliFile, how: \"already-durable\" };\n if (pinnedCopyIsComplete(version)) return { cli: pinnedCliPath(version), how: \"reused\" };\n const source = nodeModulesRootOf(cliFile);\n if (!source || !existsSync(source)) return { cli: null, how: \"unavailable\" };\n const target = runtimeDirFor(version);\n // Copy to a staging directory and move it into place, so an interrupted copy\n // never leaves a partial tree that looks complete.\n const staging = `${target}.incoming-${process.pid}`;\n rmSync(staging, { recursive: true, force: true });\n mkdirSync(staging, { recursive: true });\n cpSync(source, join(staging, \"node_modules\"), { recursive: true, dereference: true });\n rmSync(target, { recursive: true, force: true });\n mkdirSync(dirname(target), { recursive: true });\n renameSync(staging, target);\n if (!pinnedCopyIsComplete(version)) {\n rmSync(target, { recursive: true, force: true });\n return { cli: null, how: \"unavailable\" };\n }\n return { cli: pinnedCliPath(version), how: \"pinned\" };\n } catch {\n return { cli: null, how: \"unavailable\" };\n }\n}\n"]}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -21,11 +21,22 @@ export interface RuntimeConfig {
|
|
|
21
21
|
export interface Decision {
|
|
22
22
|
decision: "allow" | "deny" | "not_evaluated";
|
|
23
23
|
reason: string;
|
|
24
|
+
/** The clause that decided a deny, when the verdict named one. */
|
|
25
|
+
clauseId?: string | null;
|
|
24
26
|
/** The deciding receipt (the denied one, or the first allow). */
|
|
25
27
|
receipt?: unknown;
|
|
26
28
|
/** Every receipt produced — one per simple command in a decomposed shell call. */
|
|
27
29
|
receipts?: unknown[];
|
|
28
30
|
}
|
|
31
|
+
export declare const ci: (s: string) => string;
|
|
32
|
+
export declare const under: (dir: string) => string;
|
|
33
|
+
export declare const named: (file: string) => string;
|
|
34
|
+
export declare const dir: (d: string) => string;
|
|
35
|
+
export declare const DESTRUCTIVE: string[];
|
|
36
|
+
/** Upgrade the starter-policy patterns an older hook wrote, in memory, so a user who
|
|
37
|
+
* installed an earlier version gets the current protections without re-running init.
|
|
38
|
+
* Only exact legacy strings are replaced; any other pattern is the operator's own. */
|
|
39
|
+
export declare function upgradeStarterPolicy<T>(policy: T): T;
|
|
29
40
|
/** The default starter policy for a coding agent: protect release branches, deny
|
|
30
41
|
* destructive programs, allow workspace file access except the hook's own config
|
|
31
42
|
* and keys, and trust only the enrolled machine key. Every threshold is the
|
|
@@ -40,6 +51,12 @@ export declare function createHookRuntime(config: RuntimeConfig): {
|
|
|
40
51
|
/** Deliver queued receipts to Cloud with a bounded timeout, then it is safe to
|
|
41
52
|
* exit. Undelivered receipts persist in the durable outbox for the next run. */
|
|
42
53
|
flush(): Promise<void>;
|
|
54
|
+
/** Release the SQLite handles. The hook is a per-tool-call process, and a writer that
|
|
55
|
+
* exits without closing leaves its write-ahead log on disk for the next process to
|
|
56
|
+
* extend — measured at ~11 KiB of WAL per receipt against ~1.7 KiB when closed, so a
|
|
57
|
+
* busy session was writing tens of megabytes of pure overhead into the user's project.
|
|
58
|
+
* Always safe to call, and every exit path should. */
|
|
59
|
+
close(): void;
|
|
43
60
|
/** Decide one mapped action, recording a receipt either way. */
|
|
44
61
|
evaluateOne(mapped: Mapped): Promise<Decision>;
|
|
45
62
|
/** Decide a whole tool call. A shell call decomposes into several simple
|
package/dist/runtime.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAOA,OAAO,EAA6C,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAGnG,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACvC,OAAO,EAAgC,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAK/E,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf;;uFAEmF;IACnF,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;sEACkE;IAClE,KAAK,CAAC,EAAE;QAAE,UAAU,EAAE,cAAc,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACvF;AAED,MAAM,WAAW,QAAQ;IACvB,QAAQ,EAAE,OAAO,GAAG,MAAM,GAAG,eAAe,CAAC;IAC7C,MAAM,EAAE,MAAM,CAAC;IACf,kEAAkE;IAClE,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,iEAAiE;IACjE,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,kFAAkF;IAClF,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC;CACtB;AAWD,eAAO,MAAM,EAAE,GAAI,GAAG,MAAM,KAAG,MAAiF,CAAC;AACjH,eAAO,MAAM,KAAK,GAAI,KAAK,MAAM,KAAG,MAAyC,CAAC;AAC9E,eAAO,MAAM,KAAK,GAAI,MAAM,MAAM,KAAG,MAAoC,CAAC;AAC1E,eAAO,MAAM,GAAG,GAAI,GAAG,MAAM,KAAG,MAAmC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