@lennaertds/cardine-lab 0.0.0-stage → 0.5.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/CHANGELOG.md +62 -0
- package/README.md +192 -2
- package/bin/cardine-lab +52 -0
- package/cardine-bwrap +215 -0
- package/cardine-secrets-guard/index.js +196 -0
- package/cardine-secrets-guard/index.test.js +188 -0
- package/cardine-secrets-guard/package.json +9 -0
- package/cardine-slice-check/index.js +74 -0
- package/cardine-slice-check/package.json +9 -0
- package/cardine-status.sh +541 -0
- package/docs/VERSIONING.md +86 -0
- package/docs/cardine-lab.md +94 -0
- package/install-lab.sh +655 -0
- package/package.json +31 -4
- package/scripts/check-bwrap-secrets.sh +188 -0
- package/scripts/check-links.sh +81 -0
- package/scripts/check-sandbox.sh +129 -0
- package/scripts/check-secrets-repo.sh +100 -0
- package/scripts/check-secrets.sh +124 -0
- package/scripts/deps-audit.sh +145 -0
- package/scripts/digest-check.sh +36 -0
- package/scripts/digest-fetch.sh +114 -0
- package/scripts/digest-propose.sh +94 -0
- package/scripts/digest-reminder.sh +27 -0
- package/scripts/digest-rss.py +223 -0
- package/scripts/digest-week.sh +184 -0
- package/scripts/install-digest-timers.sh +12 -0
- package/scripts/native-ffi.sh +113 -0
- package/scripts/perf-check.sh +31 -0
- package/scripts/polyglot-check.sh +32 -0
- package/scripts/radar-check.sh +44 -0
- package/scripts/radar-reminder.sh +46 -0
- package/scripts/secrets-apply.sh +144 -0
- package/scripts/secrets-init.sh +31 -0
- package/scripts/security-scan.sh +57 -0
- package/systemd/digest-fetch.service +13 -0
- package/systemd/digest-fetch.timer +9 -0
- package/systemd/digest-week.service +13 -0
- package/systemd/digest-week.timer +9 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// cardine-workspace-guard — de agent staat alleen in de workspace.
|
|
2
|
+
//
|
|
3
|
+
// Waarom een plugin en niet (alleen) bwrap: bash/grep draaien ín de
|
|
4
|
+
// bwrap-kooi (cardine-bwrap), maar de filesystem-tools draaien in het
|
|
5
|
+
// DSH-hostproces (dsh-fs-local: "cwd is a resolution default, not a
|
|
6
|
+
// containment boundary"). Alleen een tools/pre-execute-listener zit vóór
|
|
7
|
+
// die tools, binnen DSH, en overleeft DSH-updates.
|
|
8
|
+
//
|
|
9
|
+
// Dit is een ALLOWLIST, geen denylijst (ADR 0017, strikt profiel):
|
|
10
|
+
// * elk pad buiten de open sessie wordt geweigerd — niet gevraagd,
|
|
11
|
+
// want de approval-policy staat op `never`;
|
|
12
|
+
// * de cwd van de sessie wint van CARDINE_WORKSPACE. Die variabele is
|
|
13
|
+
// alleen de terugval als de sessie geen cwd heeft. Een pin op de repo
|
|
14
|
+
// waar het lab is geïnstalleerd zou elke andere map blokkeren;
|
|
15
|
+
// * binnen de workspace blijven .env/keys/certificaten dicht;
|
|
16
|
+
// * write/edit zijn in het strikte profiel sowieso niet beschikbaar
|
|
17
|
+
// (tool-fs met write/applyPatch uit); deze listener dekt ze toch, voor
|
|
18
|
+
// als een ander profiel ze wel mount.
|
|
19
|
+
//
|
|
20
|
+
// Deny-only: een guard kan een call alleen weigeren, nooit goedkeuren.
|
|
21
|
+
// Pad-resolutie (realpath + parent-walk) i.p.v. tekst-matching, zodat
|
|
22
|
+
// `..`, symlinks en relatieve paden de regel niet omzeilen. Een pad dat
|
|
23
|
+
// nog niet bestaat wordt met de bestaande voorouder opgelost; de naam
|
|
24
|
+
// van het (nog niet bestaande) stuk wordt alsnog per segment getoetst.
|
|
25
|
+
//
|
|
26
|
+
// Zie docs/architecture/security.md en ADR 0017.
|
|
27
|
+
|
|
28
|
+
import { realpath } from "node:fs/promises";
|
|
29
|
+
import { homedir } from "node:os";
|
|
30
|
+
import { basename, dirname, isAbsolute, resolve, sep } from "node:path";
|
|
31
|
+
|
|
32
|
+
export const name = "cardine-workspace-guard";
|
|
33
|
+
|
|
34
|
+
/** Codes in tool_result.error.info.code — de model-context en logs zien ze. */
|
|
35
|
+
export const WORKSPACE_GUARD_DENIED = "WORKSPACE_GUARD_DENIED";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Bestandsnamen die nooit gelezen of geschreven mogen worden, ook niet
|
|
39
|
+
* binnen de workspace. `*.example` valt er bewust buiten: dat is de
|
|
40
|
+
* documentatie zonder waarden.
|
|
41
|
+
*/
|
|
42
|
+
const SECRET_FILE = [
|
|
43
|
+
/(^|\/)\.env$/,
|
|
44
|
+
/(^|\/)\.env\.(?!example$)[^/]+$/,
|
|
45
|
+
/(^|\/)id_(rsa|ed25519|ecdsa|dsa)$/,
|
|
46
|
+
/\.pem$/,
|
|
47
|
+
/\.p12$/,
|
|
48
|
+
/\.pfx$/,
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Resolve een pad naar een canonieke absolute vorm. Bestaat het pad niet
|
|
53
|
+
* (bv. een write naar een nieuw bestand), dan wordt de eerst bestaande
|
|
54
|
+
* voorouder ge-realpath't en de rest er letterlijk aan geplakt; `..` en `.`
|
|
55
|
+
* daarin zijn al door `resolve` genormaliseerd.
|
|
56
|
+
*/
|
|
57
|
+
async function canonical(path, cwd) {
|
|
58
|
+
const absolute = isAbsolute(path) ? resolve(path) : resolve(cwd, path);
|
|
59
|
+
let probe = absolute;
|
|
60
|
+
const missing = [];
|
|
61
|
+
for (;;) {
|
|
62
|
+
try {
|
|
63
|
+
const real = await realpath(probe);
|
|
64
|
+
return missing.length === 0 ? real : resolve(real, ...missing.reverse());
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (error?.code !== "ENOENT" && error?.code !== "ENOTDIR") return absolute;
|
|
67
|
+
const parent = dirname(probe);
|
|
68
|
+
if (parent === probe) return absolute;
|
|
69
|
+
missing.push(basename(probe));
|
|
70
|
+
probe = parent;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* De workspace is de cwd van de open sessie. CARDINE_WORKSPACE wint daar
|
|
77
|
+
* niet van: die variabele stond in de Zed-server op de repo waar
|
|
78
|
+
* install-lab draaide, en blokkeerde daarna elke andere map (Caddie,
|
|
79
|
+
* 2026-11-23). Zonder sessie-cwd blijft de variabele de terugval.
|
|
80
|
+
*/
|
|
81
|
+
function workspaceRootOf(exec) {
|
|
82
|
+
const session = exec.agent?.session?.cwd;
|
|
83
|
+
if (typeof session === "string" && session.trim() !== "") {
|
|
84
|
+
return resolve(session);
|
|
85
|
+
}
|
|
86
|
+
const fromEnv = process.env.CARDINE_WORKSPACE;
|
|
87
|
+
if (typeof fromEnv === "string" && fromEnv.trim() !== "") {
|
|
88
|
+
return resolve(fromEnv);
|
|
89
|
+
}
|
|
90
|
+
return process.cwd();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Liggen `target` en `root` gelijk of onder elkaar? (`/a/b` ⊂ `/a`) */
|
|
94
|
+
function isUnder(target, root) {
|
|
95
|
+
return target === root || target.startsWith(`${root}${sep}`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Waarom een canoniek pad geweigerd is, of undefined als het mag.
|
|
100
|
+
* Volgorde: de workspace-allowlist eerst (dat is de grens), daarna de
|
|
101
|
+
* geheimen (die blijven dicht, óók binnen de workspace).
|
|
102
|
+
*/
|
|
103
|
+
function reasonFor(canonicalPath, root) {
|
|
104
|
+
if (!isUnder(canonicalPath, root)) {
|
|
105
|
+
return "ligt buiten de workspace — de agent werkt alleen binnen het project; " +
|
|
106
|
+
"vraag de gebruiker om dit zelf op het systeem uit te voeren";
|
|
107
|
+
}
|
|
108
|
+
// Binnen de workspace: *.example is documentatie, geen secret.
|
|
109
|
+
if (/\.example$/.test(canonicalPath)) return undefined;
|
|
110
|
+
for (const pattern of SECRET_FILE) {
|
|
111
|
+
if (pattern.test(canonicalPath)) return "is een geheim bestand (.env/keys) in de workspace";
|
|
112
|
+
}
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Paden die een tool-aanroep raakt, per toolnaam. */
|
|
117
|
+
function targetsOf(toolName, args) {
|
|
118
|
+
if (args === null || typeof args !== "object") return [];
|
|
119
|
+
const out = [];
|
|
120
|
+
const add = (value) => {
|
|
121
|
+
if (typeof value === "string" && value.trim() !== "") out.push(value);
|
|
122
|
+
};
|
|
123
|
+
switch (toolName) {
|
|
124
|
+
case "read":
|
|
125
|
+
case "read_image":
|
|
126
|
+
case "write":
|
|
127
|
+
case "edit":
|
|
128
|
+
case "list":
|
|
129
|
+
add(args.file_path ?? args.path ?? args.dir);
|
|
130
|
+
break;
|
|
131
|
+
case "glob":
|
|
132
|
+
case "grep":
|
|
133
|
+
add(args.path);
|
|
134
|
+
break;
|
|
135
|
+
case "apply_patch":
|
|
136
|
+
// apply_patch draagt freeform patchnotatie; lastig pad-voor-pad te
|
|
137
|
+
// parsen. Weiger de aanroep als een pad erin buiten de workspace valt.
|
|
138
|
+
add(args.input);
|
|
139
|
+
break;
|
|
140
|
+
default:
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function cwdOf(exec) {
|
|
147
|
+
try {
|
|
148
|
+
return exec.agent?.session?.cwd ?? process.cwd();
|
|
149
|
+
} catch {
|
|
150
|
+
return process.cwd();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function denial(toolName, path, reason, root) {
|
|
155
|
+
const canon = await canonical(isAbsolute(path) ? path : resolve(cwdOf({}), path), process.cwd()).catch(() => path);
|
|
156
|
+
return {
|
|
157
|
+
kind: "deny",
|
|
158
|
+
reason:
|
|
159
|
+
`cardine-workspace-guard: "${toolName}" op "${path}" geweigerd — ${reason}. ` +
|
|
160
|
+
`De workspace is "${root}". Er is geen goedkeuringspad: de gebruiker voert ` +
|
|
161
|
+
"wijzigingen buiten het project zelf uit. Doe een voorstel (pad + exacte inhoud) " +
|
|
162
|
+
"en vraag de gebruiker het toe te passen. Toon of lees nooit geheimen.",
|
|
163
|
+
info: { name: "WorkspaceGuardDenied", code: WORKSPACE_GUARD_DENIED, path: canon, workspace: root },
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Installeer de guard vóór alle andere pre-execute-listeners. */
|
|
168
|
+
export function apply(ctx) {
|
|
169
|
+
ctx.effect(function* () {
|
|
170
|
+
yield ctx.on(
|
|
171
|
+
"tools/pre-execute",
|
|
172
|
+
async (exec, next) => {
|
|
173
|
+
const paths = targetsOf(exec.name, exec.arguments);
|
|
174
|
+
if (paths.length === 0) return next();
|
|
175
|
+
const cwd = cwdOf(exec);
|
|
176
|
+
const root = await canonical(workspaceRootOf(exec), cwd);
|
|
177
|
+
for (const path of paths) {
|
|
178
|
+
// apply_patch: toets elk token dat een pad lijkt.
|
|
179
|
+
const candidates =
|
|
180
|
+
exec.name === "apply_patch"
|
|
181
|
+
? path.split(/\s+/).filter((t) => t.includes("/") || t.startsWith(".") || t.includes(".env"))
|
|
182
|
+
: [path];
|
|
183
|
+
for (const candidate of candidates) {
|
|
184
|
+
const canon = await canonical(candidate, cwd);
|
|
185
|
+
const reason = reasonFor(canon, root);
|
|
186
|
+
if (reason !== undefined) {
|
|
187
|
+
return denial(exec.name, candidate, reason, root);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return next();
|
|
192
|
+
},
|
|
193
|
+
{ prepend: true },
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// Tests voor cardine-workspace-guard: draai met `node --test`.
|
|
2
|
+
// Deckt de allowlist: alles buiten de workspace wordt geweigerd, geheimen
|
|
3
|
+
// binnen de workspace blijven dicht, *.example en gewone projectpaden gaan
|
|
4
|
+
// door, en `..`/symlinks/nieuwe paden omzeilen de grens niet.
|
|
5
|
+
|
|
6
|
+
import { test } from "node:test";
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { mkdtemp, mkdir, writeFile, symlink, realpath } from "node:fs/promises";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { apply, WORKSPACE_GUARD_DENIED } from "./index.js";
|
|
12
|
+
|
|
13
|
+
// Een tijdelijke HOME met de bekende secret-mappen; de guard hoeft hem niet
|
|
14
|
+
// meer te kennen voor de grens — die is de workspace — maar de uitkomst moet
|
|
15
|
+
// ook buiten de workspace geweigerd zijn, dus dit bewijst dat "onder HOME"
|
|
16
|
+
// geen uitzondering is.
|
|
17
|
+
const home = await mkdtemp(join(tmpdir(), "guard-home-"));
|
|
18
|
+
await mkdir(join(home, ".secrets", "repo"), { recursive: true });
|
|
19
|
+
await writeFile(join(home, ".secrets", "repo", ".env"), "SECRET=1\n");
|
|
20
|
+
await mkdir(join(home, ".dsh"), { recursive: true });
|
|
21
|
+
await writeFile(join(home, ".dsh", ".env"), "KEY=1\n");
|
|
22
|
+
|
|
23
|
+
const repo = await realpath(await mkdtemp(join(tmpdir(), "guard-repo-")));
|
|
24
|
+
await writeFile(join(repo, ".env"), "A=1\n");
|
|
25
|
+
await writeFile(join(repo, ".env.example"), "A=\n");
|
|
26
|
+
await writeFile(join(repo, ".env.production"), "A=prod\n");
|
|
27
|
+
await mkdir(join(repo, "apps", "api"), { recursive: true });
|
|
28
|
+
await writeFile(join(repo, "apps", "api", ".env.local"), "A=local\n");
|
|
29
|
+
await writeFile(join(repo, "key.pem"), "KEY\n");
|
|
30
|
+
await writeFile(join(repo, "readme.txt"), "ok\n");
|
|
31
|
+
// Een symlink binnen de repo die naar buiten wijst: de guard volgt hem.
|
|
32
|
+
await symlink(join(home, ".secrets"), join(repo, "link-out"));
|
|
33
|
+
|
|
34
|
+
// De suites hieronder bewijzen de grens via de sessie-cwd. Een
|
|
35
|
+
// CARDINE_WORKSPACE in de omgeving mag die cwd niet overrulen (Caddie,
|
|
36
|
+
// 2026-11-23). De variabele wordt per suite toch uit de omgeving gehaald,
|
|
37
|
+
// zodat een achtergebleven pin de cwd-suites niet alsnog kleurt.
|
|
38
|
+
function withEnvOff(body) {
|
|
39
|
+
return async () => {
|
|
40
|
+
const previous = process.env.CARDINE_WORKSPACE;
|
|
41
|
+
delete process.env.CARDINE_WORKSPACE;
|
|
42
|
+
try {
|
|
43
|
+
await body();
|
|
44
|
+
} finally {
|
|
45
|
+
if (previous === undefined) delete process.env.CARDINE_WORKSPACE;
|
|
46
|
+
else process.env.CARDINE_WORKSPACE = previous;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Mini-Cordis: vangt de listener op en laat ons er mee spelen.
|
|
52
|
+
function harness(workspace) {
|
|
53
|
+
let listener;
|
|
54
|
+
const ctx = {
|
|
55
|
+
effect: (fn) => {
|
|
56
|
+
fn().next();
|
|
57
|
+
},
|
|
58
|
+
on: (event, fn) => {
|
|
59
|
+
if (event === "tools/pre-execute") listener = fn;
|
|
60
|
+
return () => {};
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
apply(ctx);
|
|
64
|
+
return async (toolName, args) => {
|
|
65
|
+
const session = workspace ? { cwd: workspace } : {};
|
|
66
|
+
const exec = { name: toolName, arguments: args, agent: { session } };
|
|
67
|
+
return listener(exec, async () => ({ kind: "allow" }));
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const run = harness(repo);
|
|
72
|
+
|
|
73
|
+
const fsArgs = (tool, path) =>
|
|
74
|
+
tool === "grep" || tool === "glob" ? { pattern: "x", path } : { file_path: path };
|
|
75
|
+
|
|
76
|
+
test("weigert elk pad buiten de workspace, ook als het niet geheim is", withEnvOff(async () => {
|
|
77
|
+
const outside = [
|
|
78
|
+
"/etc/passwd",
|
|
79
|
+
"/etc/shadow",
|
|
80
|
+
join(home, ".dsh", ".env"),
|
|
81
|
+
join(home, ".ssh", "known_hosts"),
|
|
82
|
+
join(home, ".secrets", "repo", ".env"),
|
|
83
|
+
"/tmp/anything.txt",
|
|
84
|
+
join(repo, "..", "naast-de-repo.txt"),
|
|
85
|
+
];
|
|
86
|
+
for (const path of outside) {
|
|
87
|
+
const decision = await run("read", fsArgs("read", path));
|
|
88
|
+
assert.equal(decision.kind, "deny", `${path} hoort geweigerd te worden`);
|
|
89
|
+
assert.equal(decision.info.code, WORKSPACE_GUARD_DENIED);
|
|
90
|
+
assert.match(decision.reason, /buiten de workspace/);
|
|
91
|
+
}
|
|
92
|
+
}));
|
|
93
|
+
|
|
94
|
+
test("weigert een symlink die naar buiten wijst", withEnvOff(async () => {
|
|
95
|
+
const decision = await run("read", fsArgs("read", join(repo, "link-out", ".env")));
|
|
96
|
+
assert.equal(decision.kind, "deny");
|
|
97
|
+
assert.match(decision.reason, /buiten de workspace/);
|
|
98
|
+
}));
|
|
99
|
+
|
|
100
|
+
test("weigert binnen de workspace nog steeds .env/keys", withEnvOff(async () => {
|
|
101
|
+
const secrets = [
|
|
102
|
+
".env",
|
|
103
|
+
join(repo, ".env"),
|
|
104
|
+
".env.production",
|
|
105
|
+
join(repo, "apps/api/.env.local"),
|
|
106
|
+
join(repo, "key.pem"),
|
|
107
|
+
"apps/../.env",
|
|
108
|
+
join(repo, "apps", "nieuw", ".env"), // bestaat nog niet
|
|
109
|
+
];
|
|
110
|
+
for (const path of secrets) {
|
|
111
|
+
const decision = await run("read", fsArgs("read", path));
|
|
112
|
+
assert.equal(decision.kind, "deny", `${path} hoort geweigerd te worden`);
|
|
113
|
+
assert.equal(decision.info.code, WORKSPACE_GUARD_DENIED);
|
|
114
|
+
}
|
|
115
|
+
}));
|
|
116
|
+
|
|
117
|
+
test("laat gewone projectpaden en *.example door", withEnvOff(async () => {
|
|
118
|
+
for (const path of [
|
|
119
|
+
join(repo, "readme.txt"),
|
|
120
|
+
".env.example",
|
|
121
|
+
join(repo, "apps/api/x.ts"),
|
|
122
|
+
"nieuw/bestand.md",
|
|
123
|
+
".",
|
|
124
|
+
join(repo, "docs"),
|
|
125
|
+
]) {
|
|
126
|
+
const decision = await run("read", fsArgs("read", path));
|
|
127
|
+
assert.equal(decision.kind, "allow", `${path} hoort toegestaan te worden`);
|
|
128
|
+
}
|
|
129
|
+
}));
|
|
130
|
+
|
|
131
|
+
test("de open sessie wint van CARDINE_WORKSPACE", async () => {
|
|
132
|
+
const previous = process.env.CARDINE_WORKSPACE;
|
|
133
|
+
const pinned = await realpath(await mkdtemp(join(tmpdir(), "guard-pin-")));
|
|
134
|
+
await writeFile(join(pinned, "readme.txt"), "pin\n");
|
|
135
|
+
process.env.CARDINE_WORKSPACE = pinned;
|
|
136
|
+
try {
|
|
137
|
+
const allowed = await run("read", fsArgs("read", join(repo, "readme.txt")));
|
|
138
|
+
assert.equal(allowed.kind, "allow");
|
|
139
|
+
const denied = await run("read", fsArgs("read", join(pinned, "readme.txt")));
|
|
140
|
+
assert.equal(denied.kind, "deny");
|
|
141
|
+
assert.match(denied.reason, /buiten de workspace/);
|
|
142
|
+
} finally {
|
|
143
|
+
if (previous === undefined) delete process.env.CARDINE_WORKSPACE;
|
|
144
|
+
else process.env.CARDINE_WORKSPACE = previous;
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("zonder sessie-cwd geldt CARDINE_WORKSPACE", async () => {
|
|
149
|
+
const previous = process.env.CARDINE_WORKSPACE;
|
|
150
|
+
process.env.CARDINE_WORKSPACE = repo;
|
|
151
|
+
try {
|
|
152
|
+
const unscoped = harness(null);
|
|
153
|
+
const allowed = await unscoped("read", fsArgs("read", join(repo, "readme.txt")));
|
|
154
|
+
assert.equal(allowed.kind, "allow");
|
|
155
|
+
const denied = await unscoped("read", fsArgs("read", "/etc/hostname"));
|
|
156
|
+
assert.equal(denied.kind, "deny");
|
|
157
|
+
assert.match(denied.reason, /buiten de workspace/);
|
|
158
|
+
} finally {
|
|
159
|
+
if (previous === undefined) delete process.env.CARDINE_WORKSPACE;
|
|
160
|
+
else process.env.CARDINE_WORKSPACE = previous;
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("een CARDINE_WORKSPACE in de omgeving breekt de cwd-suites niet", async () => {
|
|
165
|
+
// Regressie 2026-11-23: in de DSH-shell stond CARDINE_WORKSPACE, waardoor
|
|
166
|
+
// de tests die de sessie-cwd bewijzen /tmp/guard-repo-* als "buiten de
|
|
167
|
+
// workspace" weigerden. withEnvOff haalt de variabele per suite weg.
|
|
168
|
+
const previous = process.env.CARDINE_WORKSPACE;
|
|
169
|
+
process.env.CARDINE_WORKSPACE = "/niet/bestaand";
|
|
170
|
+
try {
|
|
171
|
+
await withEnvOff(async () => {
|
|
172
|
+
const decision = await run("read", fsArgs("read", join(repo, "readme.txt")));
|
|
173
|
+
assert.equal(decision.kind, "allow");
|
|
174
|
+
})();
|
|
175
|
+
} finally {
|
|
176
|
+
if (previous === undefined) delete process.env.CARDINE_WORKSPACE;
|
|
177
|
+
else process.env.CARDINE_WORKSPACE = previous;
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("write/edit worden ook begrensd (voor profielen die ze mounten)", withEnvOff(async () => {
|
|
182
|
+
const ok = await run("write", { file_path: join(repo, "apps", "x.ts"), content: "x" });
|
|
183
|
+
assert.equal(ok.kind, "allow");
|
|
184
|
+
const bad = await run("write", { file_path: "/etc/hosts", content: "x" });
|
|
185
|
+
assert.equal(bad.kind, "deny");
|
|
186
|
+
const badEdit = await run("edit", { file_path: join(home, "notes.md") });
|
|
187
|
+
assert.equal(badEdit.kind, "deny");
|
|
188
|
+
}));
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { resolve, dirname } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
|
|
7
|
+
const name = "cardine-slice-check";
|
|
8
|
+
const steers = new WeakMap();
|
|
9
|
+
|
|
10
|
+
// De cardine-repo is ../../.. t.o.v. dit bestand (packages/lab/cardine-slice-check/index.js → cardine/)
|
|
11
|
+
const CARDINE_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../../..");
|
|
12
|
+
|
|
13
|
+
function findMakefile(dir) {
|
|
14
|
+
while (dir !== "/") {
|
|
15
|
+
if (existsSync(resolve(dir, "Makefile"))) return dir;
|
|
16
|
+
const parent = resolve(dir, "..");
|
|
17
|
+
if (parent === dir) return null;
|
|
18
|
+
dir = parent;
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function run(cwd) {
|
|
24
|
+
return new Promise((resolve) => {
|
|
25
|
+
const child = spawn("make", ["check"], {
|
|
26
|
+
cwd,
|
|
27
|
+
env: { ...process.env, PATH: process.env.PATH },
|
|
28
|
+
});
|
|
29
|
+
let stderr = "";
|
|
30
|
+
child.stderr.on("data", (chunk) => {
|
|
31
|
+
stderr += chunk.toString();
|
|
32
|
+
if (stderr.length > 4000) stderr = stderr.slice(-4000);
|
|
33
|
+
});
|
|
34
|
+
child.on("error", (error) => resolve({ code: 127, stderr: error.message }));
|
|
35
|
+
child.on("close", (code) => resolve({ code: code ?? 1, stderr }));
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function apply(ctx) {
|
|
40
|
+
ctx.on("agent/turn-stopping", async ({ agent }) => {
|
|
41
|
+
try {
|
|
42
|
+
// Bepaal CWD: agent's cwd, of fallback naar cardine root
|
|
43
|
+
let cwd = agent.session?.header?.cwd;
|
|
44
|
+
if (typeof cwd !== "string" || cwd.length === 0) cwd = CARDINE_ROOT;
|
|
45
|
+
|
|
46
|
+
// Zoek naar een Makefile — als de agent in een niet-cardine dir zit,
|
|
47
|
+
// gebruik dan de cardine root.
|
|
48
|
+
const mkDir = findMakefile(cwd);
|
|
49
|
+
if (!mkDir) cwd = CARDINE_ROOT;
|
|
50
|
+
else cwd = mkDir;
|
|
51
|
+
|
|
52
|
+
const result = await run(cwd);
|
|
53
|
+
if (result.code === 0) return;
|
|
54
|
+
|
|
55
|
+
const count = steers.get(agent) ?? 0;
|
|
56
|
+
if (count >= 2) return;
|
|
57
|
+
steers.set(agent, count + 1);
|
|
58
|
+
const tail = result.stderr.trim().split("\n").slice(-20).join("\n");
|
|
59
|
+
agent.steer({
|
|
60
|
+
role: "user",
|
|
61
|
+
id: randomUUID(),
|
|
62
|
+
content: [{
|
|
63
|
+
type: "text",
|
|
64
|
+
text: `make check is rood (code ${result.code}). De slice sluit niet. Fix, of zet de slice terug.\n${tail}`,
|
|
65
|
+
}],
|
|
66
|
+
source: { kind: "user" },
|
|
67
|
+
});
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.error(`cardine-slice-check: ${error instanceof Error ? error.message : String(error)}`);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { apply, name };
|