@cratis/pi 7.170.11 → 7.170.12
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.
|
@@ -84,11 +84,12 @@ interface ScriptRun {
|
|
|
84
84
|
* writes it exists to refuse. Callers decide what to do with `failed`; this function only reports
|
|
85
85
|
* it honestly.
|
|
86
86
|
*/
|
|
87
|
-
function runScript(script: string, stdinJson: string, cwd: string, signal?: AbortSignal,
|
|
87
|
+
function runScript(script: string, stdinJson: string, cwd: string, signal?: AbortSignal, options: { args?: string[]; env?: NodeJS.ProcessEnv } = {}): Promise<ScriptRun> {
|
|
88
|
+
const { args = [], env } = options;
|
|
88
89
|
return new Promise<ScriptRun>((resolve) => {
|
|
89
90
|
let proc: ReturnType<typeof spawn>;
|
|
90
91
|
try {
|
|
91
|
-
proc = spawn("bash", [script, ...args], { cwd, stdio: ["pipe", "pipe", "pipe"] });
|
|
92
|
+
proc = spawn("bash", [script, ...args], { cwd, env, stdio: ["pipe", "pipe", "pipe"] });
|
|
92
93
|
} catch (error) {
|
|
93
94
|
resolve({ code: 127, stdout: "", stderr: String(error), failed: true });
|
|
94
95
|
return;
|
|
@@ -147,9 +148,10 @@ async function runBlockingGuard(
|
|
|
147
148
|
subject: string,
|
|
148
149
|
payload: object,
|
|
149
150
|
ctx: ToolCallContext,
|
|
150
|
-
fixTarget =
|
|
151
|
+
options: { fixTarget?: string; env?: NodeJS.ProcessEnv } = {},
|
|
151
152
|
): Promise<{ block: true; reason: string } | undefined> {
|
|
152
|
-
const
|
|
153
|
+
const { fixTarget = "this guard", env } = options;
|
|
154
|
+
const run = await runScript(script, JSON.stringify(payload), ctx.cwd, ctx.signal, { env });
|
|
153
155
|
if (run.failed) {
|
|
154
156
|
return {
|
|
155
157
|
block: true,
|
|
@@ -168,9 +170,10 @@ async function runCommandGuards(
|
|
|
168
170
|
guards: Array<[script: string, name: string]>,
|
|
169
171
|
payload: object,
|
|
170
172
|
ctx: ToolCallContext,
|
|
173
|
+
sessionEnv?: (script: string) => NodeJS.ProcessEnv | undefined,
|
|
171
174
|
): Promise<{ block: true; reason: string } | undefined> {
|
|
172
175
|
for (const [script, name] of guards.filter(([guard]) => isInstalled(guard))) {
|
|
173
|
-
const blocked = await runBlockingGuard(script, name, "command", payload, ctx);
|
|
176
|
+
const blocked = await runBlockingGuard(script, name, "command", payload, ctx, { env: sessionEnv?.(script) });
|
|
174
177
|
if (blocked) return blocked;
|
|
175
178
|
}
|
|
176
179
|
return undefined;
|
|
@@ -195,6 +198,7 @@ function isInstalled(script: string): boolean {
|
|
|
195
198
|
}
|
|
196
199
|
}
|
|
197
200
|
|
|
201
|
+
|
|
198
202
|
export default function (pi: ExtensionAPI) {
|
|
199
203
|
if (isPackagedExtension && fs.existsSync(path.join(process.cwd(), ".cratis", "ai.manifest.json"))) return;
|
|
200
204
|
const managedScriptsDir = path.join(process.cwd(), ".cratis", "ai", "hooks", "scripts");
|
|
@@ -203,6 +207,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
203
207
|
const guardStoreMutations = path.join(scriptsDir, "cratis-guard-store-mutations.sh");
|
|
204
208
|
const guardSecrets = path.join(scriptsDir, "cratis-guard-secrets.sh");
|
|
205
209
|
const guardShellWrites = path.join(scriptsDir, "cratis-guard-shell-writes.sh");
|
|
210
|
+
// The dependency-manifest policy belongs to the session, so the two write guards are told which repository the session
|
|
211
|
+
// is in. It is fixed here, at start-up, not read from ctx.cwd, which follows the model's own directory changes. The
|
|
212
|
+
// bundled-script fallback would otherwise resolve to the Pi package. The root is always passed, so a session directory
|
|
213
|
+
// that is no longer usable makes the guards fail closed instead of falling back to the script's checkout.
|
|
214
|
+
const sessionRoot = process.cwd();
|
|
215
|
+
const writeGuardEnv = (script: string): NodeJS.ProcessEnv | undefined =>
|
|
216
|
+
script === guardWrites || script === guardShellWrites ? { ...process.env, CLAUDE_PROJECT_DIR: sessionRoot } : undefined;
|
|
206
217
|
const guardPrBody = path.join(scriptsDir, "cratis-guard-pr-body.sh");
|
|
207
218
|
const patternScan = path.join(scriptsDir, "cratis-pattern-scan.sh");
|
|
208
219
|
const qualityGate = path.join(scriptsDir, "cratis-quality-gate.sh");
|
|
@@ -231,7 +242,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
231
242
|
const command = (event as any).input?.command;
|
|
232
243
|
if (typeof command !== "string" || !command.trim()) return;
|
|
233
244
|
const payload = { cwd: ctx.cwd, tool_name: "Bash", tool_input: { command } };
|
|
234
|
-
const blocked = await runCommandGuards([[guardStoreMutations, "cratis-guard-store-mutations"], [guardSecrets, "cratis-guard-secrets"], [guardShellWrites, "cratis-guard-shell-writes"]], payload, ctx);
|
|
245
|
+
const blocked = await runCommandGuards([[guardStoreMutations, "cratis-guard-store-mutations"], [guardSecrets, "cratis-guard-secrets"], [guardShellWrites, "cratis-guard-shell-writes"]], payload, ctx, writeGuardEnv);
|
|
235
246
|
if (blocked) return blocked;
|
|
236
247
|
if (isInstalled(guardPrBody)) {
|
|
237
248
|
const run = await runScript(guardPrBody, JSON.stringify(payload), ctx.cwd, ctx.signal);
|
|
@@ -245,9 +256,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
245
256
|
if (!filePath) return;
|
|
246
257
|
if (!isInstalled(guardWrites)) return; // no guard installed in this repository - nothing to enforce
|
|
247
258
|
const payload = { cwd: ctx.cwd, tool_input: { file_path: filePath, content, new_string: newString } };
|
|
248
|
-
// The shared script
|
|
249
|
-
// keep
|
|
250
|
-
return runBlockingGuard(guardWrites, "cratis-guard-writes", "write", payload, ctx, "write guards");
|
|
259
|
+
// The shared script takes the manifest policy (allowDependencyManifestWrites, else Git origin) from the session
|
|
260
|
+
// repository, never the write target; keep that and generated/.env protection identical to Claude Code.
|
|
261
|
+
return runBlockingGuard(guardWrites, "cratis-guard-writes", "write", payload, ctx, { fixTarget: "write guards", env: writeGuardEnv(guardWrites) });
|
|
251
262
|
});
|
|
252
263
|
|
|
253
264
|
// ── Cleanup of delivered worktrees and branches ──
|
|
@@ -257,7 +268,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
257
268
|
async function sweepDelivered(hookEvent: "PostToolUse" | "SessionStart", command: string | undefined, cwd: string, signal?: AbortSignal): Promise<string> {
|
|
258
269
|
if (!isInstalled(cleanupWorktrees)) return "";
|
|
259
270
|
const payload = JSON.stringify({ hook_event_name: hookEvent, cwd, tool_input: command ? { command } : undefined });
|
|
260
|
-
const run = await runScript(cleanupWorktrees, payload, cwd, signal, ["--hook"]);
|
|
271
|
+
const run = await runScript(cleanupWorktrees, payload, cwd, signal, { args: ["--hook"] });
|
|
261
272
|
if (run.failed) return "cratis-cleanup-worktrees is installed but could not be run, so delivered worktrees were not cleaned up.";
|
|
262
273
|
try {
|
|
263
274
|
return JSON.parse(run.stdout)?.hookSpecificOutput?.additionalContext?.trim() ?? run.stderr.trim();
|
|
@@ -36,9 +36,14 @@ payload omits cwd; glob probing does not. Filesystem check/use races remain: thi
|
|
|
36
36
|
|
|
37
37
|
The Bash write guard checks manifest and environment-file names against both logical and resolved physical basenames; run `cratis ai update` to receive this change.
|
|
38
38
|
|
|
39
|
-
A Pi session runs guards from its starting checkout
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
A Pi session runs guards from its starting checkout (or the bundled corpus when that checkout has none). A command or
|
|
40
|
+
file path that targets another checkout is judged by those guards; the bridge never runs the other checkout's scripts,
|
|
41
|
+
because a checkout the agent can write cannot be a trust anchor. The guards do read policy data about the write target's
|
|
42
|
+
repository for dependency-manifest writes, but only to tighten: the session's policy decides, and a target can block
|
|
43
|
+
but never unlock ([Cratis/AI#1256](https://github.com/Cratis/AI/issues/1256); see *Dependency-manifest writes* below). Refresh the corpus in the starting checkout with
|
|
44
|
+
`cratis ai update` (scripts are read per call, so no restart), or start a new session in the checkout you change.
|
|
45
|
+
Proposed in Cratis/AI#1114 and drafted as `decisions/153-pi-hook-bridge-runs-only-the-starting-checkouts-guards.md`
|
|
46
|
+
(status: proposed): do not select the guard corpus per call.
|
|
42
47
|
|
|
43
48
|
Reader glob probes skip directories only when decoded options establish a nonrecursive reader;
|
|
44
49
|
unknown or recursive grep options keep directory matches conservative. Simple sed print/substitution
|
|
@@ -464,14 +469,23 @@ Other protected classes still apply regardless of a generated header.
|
|
|
464
469
|
|
|
465
470
|
### Dependency-manifest writes
|
|
466
471
|
|
|
467
|
-
|
|
472
|
+
The policy comes from the **session**, not from the file being written. The session repository is
|
|
473
|
+
`CLAUDE_PROJECT_DIR` (else the checkout that holds the installed hook; in Pi, the working directory
|
|
474
|
+
Pi was started in). Its `.cratis/ai.json` `allowDependencyManifestWrites` decides; without a boolean, manifest
|
|
475
|
+
writes are allowed **silently by default** when that repository's Git `origin` points to
|
|
468
476
|
`github.com/Cratis/<repo>` over HTTPS or SSH (including `git@github.com:Cratis/<repo>.git`;
|
|
469
|
-
host and owner are case-insensitive).
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
477
|
+
host and owner are case-insensitive). A missing or non-Cratis origin keeps the block.
|
|
478
|
+
|
|
479
|
+
The repository that owns the write target is consulted only to **tighten** the decision. The hook
|
|
480
|
+
resolves it from the target, resolving relative paths against the tool's working directory, lexically
|
|
481
|
+
collapsing repeated separators, `.` and `..`, then walking up to the nearest existing directory for new
|
|
482
|
+
files; that directory is resolved physically before Git lookup, so symlinked directories use the
|
|
483
|
+
repository they actually point into. The write is allowed only if the session policy **and** the target
|
|
484
|
+
repository's own policy (same rule: its setting, else its Cratis origin) both allow it. A target's `true`
|
|
485
|
+
or Cratis origin can never unlock a session that blocks, so an agent-created repository cannot opt itself
|
|
486
|
+
in. A target outside any Git repository is blocked, and so is any session whose repository cannot be
|
|
487
|
+
determined. `.cratis/ai.json` is itself a protected control, so the agent cannot edit the session policy.
|
|
488
|
+
Installed corpus files and selected profiles do **not** prove organization
|
|
475
489
|
ownership: consumer applications can have exactly the same installation and profiles.
|
|
476
490
|
|
|
477
491
|
Set a repository-owned boolean in `.cratis/ai.json` to override that default in both Claude Code
|
|
@@ -482,7 +496,7 @@ and Pi:
|
|
|
482
496
|
```
|
|
483
497
|
|
|
484
498
|
`false` re-enables the manifest block in a Cratis repository; `true` allows manifest writes
|
|
485
|
-
in any repository. Merge this property into the existing configuration. An absent, malformed,
|
|
499
|
+
in any repository, for targets that do not themselves block (see above). Merge this property into the existing configuration. An absent, malformed,
|
|
486
500
|
or non-boolean setting uses the origin-based default. This setting never permits `.env` writes.
|
|
487
501
|
The protected basename list is unchanged; `package.json` is not part of this guard's manifest category.
|
|
488
502
|
|
|
@@ -9,8 +9,12 @@
|
|
|
9
9
|
#
|
|
10
10
|
# While jq is missing the guard cannot read the tool call, so it refuses every call.
|
|
11
11
|
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
12
|
+
# The manifest policy belongs to the session, never to the write target. The session repository is
|
|
13
|
+
# CLAUDE_PROJECT_DIR (else the checkout holding this script); its .cratis/ai.json
|
|
14
|
+
# allowDependencyManifestWrites (true allows, false blocks) decides, else its github.com/Cratis origin.
|
|
15
|
+
# The target repository can only tighten that: an explicit false, a non-Cratis origin without a setting,
|
|
16
|
+
# or no Git repository blocks; a target's true or Cratis origin never unlocks a blocking session.
|
|
17
|
+
# A session that cannot be determined blocks. Only manifests are affected; .env stays blocked.
|
|
14
18
|
#
|
|
15
19
|
# Escape hatch for the "unless explicitly asked" case — the user asks, you set it for the call:
|
|
16
20
|
# CRATIS_HOOKS_ALLOW_PROTECTED_WRITES=1
|
|
@@ -374,9 +378,43 @@ agent_control_target() {
|
|
|
374
378
|
}
|
|
375
379
|
|
|
376
380
|
# ── Dependency manifests, lockfiles, SDK and NuGet configuration ──────────────
|
|
381
|
+
# Policy of one repository root: a boolean allowDependencyManifestWrites wins, else a github.com/Cratis origin.
|
|
382
|
+
repository_manifest_policy() {
|
|
383
|
+
local setting="" root="$1"
|
|
384
|
+
if [ -f "$root/.cratis/ai.json" ]; then
|
|
385
|
+
# Only actual booleans override the default; jq's `// empty` would discard false.
|
|
386
|
+
setting="$(jq -r '.allowDependencyManifestWrites | select(type == "boolean") | tostring' "$root/.cratis/ai.json" 2>/dev/null)" || parser_failed
|
|
387
|
+
fi
|
|
388
|
+
case "$setting" in
|
|
389
|
+
true) return 0 ;;
|
|
390
|
+
false) return 1 ;;
|
|
391
|
+
esac
|
|
392
|
+
repository_origin "$root"
|
|
393
|
+
# Anchor host, owner and the single repository segment; never infer ownership from profiles.
|
|
394
|
+
origin_matches '^(https://github\.com/|git@github\.com:|ssh://git@github\.com(:[0-9]+)?/)cratis/[a-z0-9_.-]+/?$'
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
# The session's policy, computed once. The session is fixed by the harness (CLAUDE_PROJECT_DIR, else the checkout
|
|
398
|
+
# holding this script), never by the write target, the payload cwd or the shell's working directory.
|
|
399
|
+
# Anything that cannot be determined blocks.
|
|
400
|
+
session_manifest_policy=""
|
|
401
|
+
session_manifests_allowed() {
|
|
402
|
+
if [ -z "$session_manifest_policy" ]; then
|
|
403
|
+
local session_root
|
|
404
|
+
session_manifest_policy=1
|
|
405
|
+
# A session the harness named but that is not a directory is undeterminable, never the script's checkout.
|
|
406
|
+
if [ -n "${CLAUDE_PROJECT_DIR:-}" ] && [ ! -d "$CLAUDE_PROJECT_DIR" ]; then return 1; fi
|
|
407
|
+
if session_root="$(hook_repo_root 2>/dev/null)" && session_root="$(git -C "$session_root" rev-parse --show-toplevel 2>/dev/null)"; then
|
|
408
|
+
if repository_manifest_policy "$session_root"; then session_manifest_policy=0; fi
|
|
409
|
+
fi
|
|
410
|
+
fi
|
|
411
|
+
return "$session_manifest_policy"
|
|
412
|
+
}
|
|
413
|
+
|
|
377
414
|
manifest_writes_allowed() {
|
|
378
|
-
local
|
|
379
|
-
#
|
|
415
|
+
local directory policy_root start="$PWD"
|
|
416
|
+
# The session decides first; the target is only consulted to tighten it.
|
|
417
|
+
session_manifests_allowed || return 1
|
|
380
418
|
# New files can have nonexistent parents; find their nearest existing directory first.
|
|
381
419
|
dir_of "$file"
|
|
382
420
|
directory="$REPLY"
|
|
@@ -390,17 +428,7 @@ manifest_writes_allowed() {
|
|
|
390
428
|
directory="$PWD"
|
|
391
429
|
cd "$start" 2>/dev/null || true
|
|
392
430
|
policy_root="$(git -C "$directory" rev-parse --show-toplevel 2>/dev/null)" || return 1
|
|
393
|
-
|
|
394
|
-
# Only actual booleans override the default; jq's `// empty` would discard false.
|
|
395
|
-
setting="$(jq -r '.allowDependencyManifestWrites | select(type == "boolean") | tostring' "$policy_root/.cratis/ai.json" 2>/dev/null)" || parser_failed
|
|
396
|
-
fi
|
|
397
|
-
case "$setting" in
|
|
398
|
-
true) return 0 ;;
|
|
399
|
-
false) return 1 ;;
|
|
400
|
-
esac
|
|
401
|
-
repository_origin "$policy_root"
|
|
402
|
-
# Anchor host, owner and the single repository segment; never infer ownership from profiles.
|
|
403
|
-
origin_matches '^(https://github\.com/|git@github\.com:|ssh://git@github\.com(:[0-9]+)?/)cratis/[a-z0-9_.-]+/?$'
|
|
431
|
+
repository_manifest_policy "$policy_root"
|
|
404
432
|
}
|
|
405
433
|
|
|
406
434
|
# Return the category, leaving normal Write/Edit diagnostics unchanged below.
|