seisin 0.5.0 → 0.6.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.
Files changed (76) hide show
  1. package/README.md +385 -129
  2. package/package.json +13 -5
  3. package/src/browsers.js +52 -0
  4. package/src/canary.js +149 -0
  5. package/src/cli.js +47 -23
  6. package/src/commands/check.js +154 -3
  7. package/src/commands/explain.js +2 -5
  8. package/src/commands/hook.js +7 -3
  9. package/src/commands/init.js +57 -50
  10. package/src/commands/requests.js +31 -56
  11. package/src/commands/review.js +33 -6
  12. package/src/commands/run.js +332 -110
  13. package/src/commands/walls.js +39 -7
  14. package/src/commands/whose.js +2 -1
  15. package/src/config.js +321 -15
  16. package/src/controls.js +5 -5
  17. package/src/diagnose.js +67 -16
  18. package/src/env.js +3 -0
  19. package/src/grants.js +107 -2
  20. package/src/handoff.js +285 -10
  21. package/src/help.js +39 -19
  22. package/src/hook.js +59 -30
  23. package/src/index.d.ts +217 -0
  24. package/src/index.js +20 -12
  25. package/src/inspect/codex.js +64 -0
  26. package/src/inspect/credentials.js +48 -0
  27. package/src/inspect/enforce.js +24 -0
  28. package/src/inspect/files.js +71 -0
  29. package/src/inspect/home.js +43 -0
  30. package/src/inspect/inject.js +68 -0
  31. package/src/inspect/keys.js +74 -0
  32. package/src/inspect/layout.js +258 -0
  33. package/src/inspect/limits.js +98 -0
  34. package/src/inspect/machine.js +107 -0
  35. package/src/inspect/network.js +37 -0
  36. package/src/inspect/providers.js +71 -0
  37. package/src/inspect/read-territory.js +105 -0
  38. package/src/inspect/role-keys.js +136 -0
  39. package/src/inspect/shared.js +37 -0
  40. package/src/inspect/warnings.js +58 -0
  41. package/src/inspect.js +13 -815
  42. package/src/intake.js +131 -21
  43. package/src/keys.js +223 -67
  44. package/src/kinds.js +42 -0
  45. package/src/lines.js +26 -0
  46. package/src/log.js +7 -2
  47. package/src/mcp.js +28 -43
  48. package/src/nested.js +12 -3
  49. package/src/notify.js +53 -12
  50. package/src/owners.js +165 -3
  51. package/src/paths.js +17 -1
  52. package/src/provider-run.js +81 -0
  53. package/src/redact.js +34 -15
  54. package/src/render.js +35 -9
  55. package/src/requests.js +363 -45
  56. package/src/review.js +21 -9
  57. package/src/rundir.js +29 -8
  58. package/src/sandboxes/command.js +93 -0
  59. package/src/sandboxes/index.js +113 -0
  60. package/src/sandboxes/mach-profile.js +31 -0
  61. package/src/sandboxes/srt-mach.mjs +43 -0
  62. package/src/sandboxes/srt.js +163 -0
  63. package/src/sandboxes/tartaros.js +80 -0
  64. package/src/scan.js +7 -3
  65. package/src/serve.js +35 -17
  66. package/src/spool.js +8 -17
  67. package/src/srt.js +263 -33
  68. package/src/surface.js +129 -16
  69. package/src/territory.js +261 -0
  70. package/src/trace.js +33 -0
  71. package/src/uilink.js +15 -6
  72. package/src/views.js +40 -14
  73. package/src/violations.js +143 -7
  74. package/src/walls.js +62 -9
  75. package/ui/index.html +85 -13
  76. package/ui/favicon.svg +0 -1
package/src/inspect.js CHANGED
@@ -9,16 +9,14 @@
9
9
  * where they can be exercised directly.
10
10
  */
11
11
  import { unknownRole } from "./suggest.js";
12
- import { resolve, dirname, basename, relative, join, delimiter } from "node:path";
13
- import { lstatSync, readdirSync, existsSync, readFileSync, statSync } from "node:fs";
14
- import { ownersOf, covers, enforcedNeverWrites } from "./owners.js";
15
- import { ROLE_KEYS, closest } from "./config.js";
16
- import { entriesOf } from "./keys.js";
17
- import { SHAPES } from "./scan.js";
18
- import { wired, broadlyWired } from "./commands/wire.js";
19
- import { RUNTIME_WRITES, CREDENTIAL_HOMES, expand, settingsFor } from "./srt.js";
20
- import { realOrSelf } from "./grants.js";
21
- import { protections, resolveExecutable } from "./surface.js";
12
+ import { protections } from "./surface.js";
13
+ import { LIMITS } from "./inspect/limits.js";
14
+ import { sharedPaths } from "./inspect/shared.js";
15
+ import { codexSignIn } from "./inspect/codex.js";
16
+ import { warningsFor } from "./inspect/warnings.js";
17
+
18
+ // The warnings live in src/inspect/, one family per file; this is the entry point.
19
+ export { sharedPaths, codexSignIn };
22
20
 
23
21
  /**
24
22
  * A report on one config: the roles, and every way the map lies.
@@ -36,7 +34,10 @@ export function inspect(config, only = null, where = config.path) {
36
34
 
37
35
  return {
38
36
  where,
39
- roles: roles.map((r) => ({ name: r.name, writes: r.writes, keys: r.keys, neverWrites: r.neverWrites ?? [], localBinding: r.localBinding === true, localPorts: r.localPorts ?? [], mcp: r.mcp ?? null, trustd: r.trustd === true })),
37
+ roles: roles.map((r) => ({ name: r.name, writes: r.writes, keys: r.keys,
38
+ reads: r.reads ?? [], toolchain: r.toolchain ?? [], verify: r.verify,
39
+ neverWrites: r.neverWrites ?? [], private: r.private ?? [], localBinding: r.localBinding === true,
40
+ localPorts: r.localPorts ?? [], mcp: r.mcp ?? null, trustd: r.trustd === true })),
40
41
  /**
41
42
  * The provider commands this config would run, listed because they are the
42
43
  * one thing in a `seisin.toml` that **executes**, and it executes in the
@@ -57,810 +58,7 @@ export function inspect(config, only = null, where = config.path) {
57
58
  * quietly. See surface.js for the family and the measurements.
58
59
  */
59
60
  protected: protections(config, roles),
60
- warnings: warningsFor(config, roles, shared),
61
+ warnings: warningsFor(config, roles, shared, { scoped: !!only }),
61
62
  limits: LIMITS,
62
63
  };
63
64
  }
64
-
65
- /**
66
- * What the tool cannot do at all — as opposed to `warnings`, which are things
67
- * wrong with *this* config.
68
- *
69
- * Kept apart on purpose. A limit printed as a warning reads as something a
70
- * person could fix, and after the third run it reads as nothing at all.
71
- *
72
- * These are the sentences someone needs before they trust the map on screen
73
- * more than it deserves, so `check` prints them every time rather than behind a
74
- * flag: the moment to learn that deletes are not covered is while you are
75
- * looking at the policy, not afterwards.
76
- */
77
- const LIMITS = [
78
- ...(process.platform === "darwin" ? [] : [{
79
- kind: "kernel-denials-unreadable",
80
- headline: "denials by the kernel are not recorded on this platform",
81
- detail:
82
- "on macOS `seisin run` reads them out of the system log, so a denial the hook never " +
83
- "saw still lands in `seisin log` with its owner and still queues a request. There is no " +
84
- "equivalent to read here — bubblewrap does not log denials, and the runtime's substitute " +
85
- "is not reachable from outside it. The boundary holds exactly as well; what you lose is " +
86
- "seeing it work. Run `seisin wire` so the hook records what it can. The ask is upstream: " +
87
- "github.com/carlostapiacl/seisin/blob/main/docs/upstream/cli-violations.md",
88
- }]),
89
- {
90
- kind: "keys-only-what-you-declared",
91
- headline: "key isolation covers the directory you declared, and nothing else",
92
- detail:
93
- "a credential sitting in the repo outside [keys] dir is ordinary readable " +
94
- "content to every role. Run `seisin scan` to find them — check does not, " +
95
- "because reading the whole tree on every invocation is how a command stops " +
96
- "being run.",
97
- },
98
- {
99
- kind: "observe-cannot-see-the-network",
100
- headline: "`--observe` opens the filesystem, never the network",
101
- detail:
102
- "so it cannot tell you which domains an agent needs. The runtime does record every " +
103
- "denial the proxy makes — host, port and reason — but only a caller that embeds the " +
104
- "library can read them; nothing reaches a caller that runs the binary, which is what " +
105
- "seisin does. So for an agent whose endpoints are not published, the options are to " +
106
- "find them another way or drop the network restriction for it. The ask is upstream: " +
107
- "github.com/carlostapiacl/seisin/blob/main/docs/upstream/cli-violations.md",
108
- },
109
- ...(process.platform === "linux" ? [{
110
- kind: "control-files-linux",
111
- headline: "a project's hooks and settings are protected only where they already exist",
112
- detail:
113
- "bubblewrap denies a path by mounting over it, and for a path that is not there it would " +
114
- "create it on your disk. So on Linux a role can still create a .git/hooks entry or a " +
115
- ".claude/settings.json in a project of its territory that had none. On macOS the kernel " +
116
- "denies creating them too.",
117
- }] : []),
118
- {
119
- kind: "shared-scratch",
120
- headline: "roles share ~/.cache, ~/.local/share and the temp dir unless isolate = \"home\"",
121
- detail:
122
- "programs installed there that are on your PATH are protected, and so are Claude Code's " +
123
- "settings and plugins. Anything else a program outside the sandbox reads from those places — a " +
124
- "cache it executes, a tool not on PATH — is writable by every role. isolate = \"home\" " +
125
- "gives each role its own.",
126
- },
127
- {
128
- kind: "unlink-uncovered",
129
- headline: "a role can delete inside its own territory",
130
- detail:
131
- // The path that used to be here ships with the repo and not with the npm
132
- // package, so for most readers it named a file they do not have.
133
- "writes and deletes are one permission to the kernel, so `rm` inside a " +
134
- "role's own folders succeeds. Closing it needs denyUnlink in the sandbox " +
135
- "runtime: github.com/carlostapiacl/seisin#how-it-holds",
136
- },
137
- ];
138
-
139
- /**
140
- * What is wrong with a role table's keys, as opposed to its paths.
141
- *
142
- * Two things, both about `never_writes` and both ways a subtraction can look
143
- * written and not be:
144
- *
145
- * - a setting seisin does not know. It used to be dropped without a word, which
146
- * for `writes` is a role with less than it thinks, and for a misspelt
147
- * `never_writes` is a role with MORE than its owner thinks — the one
148
- * direction a policy must not fail in. Named with the nearest
149
- * known setting when there is one close enough to be the intended spelling.
150
- * - a `never_writes` entry that no `writes` of the same role covers. It
151
- * subtracts from nothing, so it is either a typo in the path or a rule
152
- * that stopped applying when the territory moved. Either way it reads as
153
- * protection and is not.
154
- *
155
- * Silent when `never_writes` is absent. Absent is valid; a check that asks for
156
- * the setting would turn an optional subtraction into a required migration.
157
- */
158
- function roleKeyWarnings(roles, config = null) {
159
- const warnings = [];
160
- for (const r of roles) {
161
- const enforced = new Set(enforcedNeverWrites(config, r));
162
- const skipped = (r.neverWritesDeclared ?? r.neverWrites ?? []).filter((g) => !enforced.has(g));
163
- if (skipped.length)
164
- warnings.push({
165
- kind: "never-writes-not-enforced-here",
166
- headline: `${r.name}: never_writes ${skipped.join(" ")} — not enforced on Linux until it exists`,
167
- detail: "bubblewrap denies a path by mounting over it, and would create a missing one on the host " +
168
- "for the whole run — for a lock file, locking everyone else out, and for good if the role is " +
169
- "killed. seisin skips those, and explain says allowed for them, so the sentence matches the kernel.",
170
- });
171
- for (const k of r.unknownKeys ?? []) {
172
- const near = nearest(k, ROLE_KEYS);
173
- warnings.push({
174
- kind: "unknown-role-key",
175
- headline: `${r.name}: unknown setting "${k}" in [roles.${r.name}] — ignored` +
176
- (near ? `. Did you mean "${near}"?` : ""),
177
- detail: `Known settings: ${ROLE_KEYS.join(", ")}. ` +
178
- (near === "never_writes"
179
- ? "As written this subtracts nothing: the role can still write every path it lists."
180
- : "Nothing in it takes effect."),
181
- });
182
- }
183
- // A family handed to a role is a hole in the protection on purpose, and
184
- // said on every check so it stays a decision somebody can see.
185
- for (const f of r.controlFiles ?? [])
186
- warnings.push({
187
- kind: "control-files-handed",
188
- headline: f === "ide"
189
- ? `${r.name}: control_files = ["ide"] — may edit .vscode/, .cursor/ and .windsurf/ in its territory`
190
- : `${r.name}: control_files = ["instructions"] — may edit CLAUDE.md, AGENTS.md and their kin in its territory`,
191
- detail: f === "ide"
192
- ? "Your editor applies what it writes there — tasks, settings, auto-approval, MCP servers — outside the sandbox. " +
193
- "Hand it only to a role that maintains those files, and review its changes to them."
194
- : "The next agent session reads what it writes there as its instructions, whichever role that session is.",
195
- });
196
- // macOS only, and not by omission: on Linux the runtime removes the network
197
- // namespace, so every role already has a loopback of its own — it can serve
198
- // and reach its own server with or without the key, and reaches nothing on
199
- // the host either way (measured in Docker, Debian 12, bwrap 0.8.0).
200
- if (r.localBinding && process.platform === "darwin")
201
- warnings.push({
202
- kind: "local-binding-reaches-localhost",
203
- headline: `${r.name}: local_binding lets it connect to every port on localhost, not only listen`,
204
- detail: "The runtime grants bind on any interface, inbound, and outbound to localhost:*. " +
205
- "Anything listening on this machine without authentication — a local control API, a " +
206
- "database, a dev server — is within this role's reach, outside its territory.",
207
- });
208
- // Both at once is a config that says "these ports" and grants "every port".
209
- // macOS only: on Linux local_binding reaches nothing on the host, so there
210
- // local_ports is the only one of the two that does anything.
211
- if (r.localBinding && r.localPorts?.length && process.platform === "darwin")
212
- warnings.push({
213
- kind: "local-ports-moot",
214
- headline: `${r.name}: local_ports names ${r.localPorts.join(", ")}, but local_binding already opens every localhost port`,
215
- detail: "local_binding grants outbound to localhost:* in the kernel profile, so the port list " +
216
- "restricts nothing for this role. Drop local_binding if the role only needs to reach " +
217
- "those ports; keep it only if the role has to listen.",
218
- });
219
- // trustd is a macOS service; on Linux the key changes nothing.
220
- if (r.trustd && process.platform === "darwin")
221
- warnings.push({
222
- kind: "trustd-open",
223
- headline: `${r.name}: trustd = true opens com.apple.trustd.agent, a path out that the domain list does not see`,
224
- detail: "trustd verifies certificates for Go (before 1.27) and Dart on macOS, and to do it fetches, " +
225
- "outside the sandbox, the URLs a certificate carries. The runtime calls it an exfiltration vector; " +
226
- "no public demonstration exists. Go 1.27+ does not need it: seisin sets SSL_CERT_FILE.",
227
- });
228
- for (const g of r.neverWrites ?? []) {
229
- if (r.writes.some((w) => covers(w, g) || covers(w, g.replace(/\/\*\*$/, ""))))
230
- continue;
231
- warnings.push({
232
- kind: "never-writes-subtracts-nothing",
233
- headline: `${r.name}: never_writes "${g}" is not inside any of its writes`,
234
- detail: "It denies something that was already denied, so it protects nothing. " +
235
- "Check the path, or drop the entry if the territory moved.",
236
- });
237
- }
238
- }
239
- return warnings;
240
- }
241
-
242
- /** The candidate within edit distance 3 of `word`, or null. */
243
- function nearest(word, candidates) {
244
- // Spellings of the same idea in another vocabulary, checked first because
245
- // letters mislead here: `no_writes` is three edits from `writes`, and
246
- // suggesting the grant for a misspelt subtraction is the worst answer.
247
- if (/deny|never|no_?write/i.test(word)) return "never_writes";
248
- return closest(word, candidates);
249
- }
250
-
251
- /**
252
- * Paths more than one role claims.
253
- *
254
- * Overlap is allowed and sometimes right — a hotfix role deliberately reaches
255
- * into the territory of the role that normally owns a repo. It is reported so
256
- * that it stays a decision somebody made rather than a thing that drifted.
257
- */
258
- export function sharedPaths(config, roles = Object.values(config.roles)) {
259
- // Asked once per distinct glob: a real policy repeats them (1,332 writes,
260
- // 303 distinct in a multi-repo workspace), and each question is every glob of every
261
- // role again.
262
- const seen = new Set();
263
- const asked = new Map();
264
- for (const r of roles)
265
- for (const glob of r.writes) {
266
- let many = asked.get(glob);
267
- if (many === undefined) asked.set(glob, (many = ownersOf(config, glob.replace(/\/\*\*$/, "")).length > 1));
268
- if (many) seen.add(glob);
269
- }
270
- /**
271
- * A shared database counts once, not four times.
272
- *
273
- * `writes` carries the sidecar expansion so that the grant and the sentence
274
- * agree (see config.js), and this list read straight off it turned nine
275
- * shared things into thirty-three — the same name with three suffixes,
276
- * announced as if it were three more decisions to make. The expansion exists
277
- * to stop a policy saying one thing four times; a warning that undoes that
278
- * is the feature leaking into the place it was meant to clean up.
279
- *
280
- * A sidecar shared WITHOUT its database still counts on its own, because
281
- * that is a real and odd thing to have.
282
- */
283
- const todas = new Set(seen);
284
- return [...seen].filter((p) => !(/-(wal|shm|journal)$/.test(p) && todas.has(p.replace(/-(wal|shm|journal)$/, ""))));
285
- }
286
-
287
- /**
288
- * What a role can still read in YOUR home, which is the limit people are most
289
- * surprised by.
290
- *
291
- * `~/.ssh`, `~/.aws/credentials`, `~/.npmrc` and `~/.config/gh/hosts.yml` are
292
- * ordinary readable files to every role unless `[runtime] isolate` says
293
- * otherwise, and nothing on screen said so. Reads outside the key directories
294
- * being open is a deliberate trade - an agent that cannot read the machine
295
- * cannot work - but a trade nobody was shown is not a trade they made.
296
- *
297
- * It reports the level rather than pushing one. `credentials` closes those four
298
- * and leaves the agent logged in; `home` closes the agent's own directories too
299
- * and costs the session. Which of the two you want is the threat model, which
300
- * is why there is no default beyond off.
301
- */
302
- function homeReachWarning(config) {
303
- const level = config.isolate === true ? "home" : config.isolate;
304
- if (level === "home") return null; // nothing left to say
305
-
306
- const where = CREDENTIAL_HOMES.slice(0, 4).join(" ");
307
- return level === "credentials"
308
- ? {
309
- kind: "home-partly-open",
310
- headline: "your credentials are closed to every role; the agent's own directories are not",
311
- detail:
312
- `isolate = "credentials" denies ${where} and the rest. What stays open is ` +
313
- "~/.claude and ~/.codex, which is what keeps the agent logged in, so the roles " +
314
- 'share one session and can read each other. `isolate = "home"` closes those ' +
315
- "too, and then every CLI in the sandbox will ask you to log in again.",
316
- }
317
- : {
318
- kind: "home-open",
319
- headline: `every role can read ${where} and the rest of your home`,
320
- detail:
321
- "reads outside the [keys] dir are open on purpose - an agent that cannot read the " +
322
- "machine cannot work - but that includes the places credentials live. " +
323
- '`[runtime] isolate = "credentials"` closes them, and the agent stays logged in.',
324
- };
325
- }
326
-
327
-
328
- /**
329
- * Credential-shaped names at the top of the repository, and nothing deeper.
330
- *
331
- * One `readdir`, no recursion, conventional names only. It exists to answer
332
- * "does this repo plausibly have secrets" fast enough to run on every `check`,
333
- * not to find them — `seisin scan` is the one that looks properly.
334
- */
335
- const CREDENTIAL_NAMES = /^(\.env(\..+)?|\.secrets|secrets|\.netrc|.*\.(pem|key|p12|pfx))$/i;
336
-
337
- function credentialish(root) {
338
- let entries;
339
- try {
340
- entries = readdirSync(root, { withFileTypes: true });
341
- } catch {
342
- return [];
343
- }
344
- const found = new Set(entries.filter((e) => CREDENTIAL_NAMES.test(e.name)).map((e) => e.name));
345
-
346
- /**
347
- * And the ones whose name gives nothing away.
348
- *
349
- * The name test misses `credentials.txt`, `tokens.conf`, `config.local` —
350
- * which a field review found by writing a test file this did not catch, and
351
- * then blaming the test. The test was fine; the detector was name-only.
352
- *
353
- * So the root's small text files are also read, with the same shapes
354
- * `seisin scan` uses. Root only, small only, and it stops at the first hit
355
- * per file: this runs on every `check` and the full walk is `scan`'s job.
356
- */
357
- const CERTAIN = SHAPES.filter(([c]) => c === "certain").map(([, , re]) => re);
358
- for (const e of entries) {
359
- if (found.size >= 4) break;
360
- if (!e.isFile() || found.has(e.name)) continue;
361
- try {
362
- const p = join(root, e.name);
363
- if (statSync(p).size > 64 * 1024) continue;
364
- const text = readFileSync(p, "utf8");
365
- if (CERTAIN.some((re) => re.test(text))) found.add(e.name);
366
- } catch {
367
- /* unreadable, or not text: not our business here */
368
- }
369
- }
370
- return [...found].sort().slice(0, 4);
371
- }
372
-
373
- /**
374
- * Would the parent find this command? The same lookup `run` makes
375
- * (resolveExecutable), so check does not call "missing" a program that is
376
- * only in a directory a role writes — that has its own warning, above.
377
- */
378
- function onPath(cmd, config) {
379
- try {
380
- // A path is resolved against the policy and not looked up, so it is there
381
- // only if it exists.
382
- return existsSync(resolveExecutable(cmd, config));
383
- } catch (e) {
384
- return /a role can write/.test(e.message);
385
- }
386
- }
387
-
388
- /** Every way this policy does not hold, each with what to do about it. */
389
- function warningsFor(config, roles, shared = sharedPaths(config, roles)) {
390
- const warnings = [];
391
-
392
- // A pattern the kernel cannot be given is not a policy, it is a sentence that
393
- // reads like one. `run` refuses it; `check` exists precisely so you find that
394
- // out while reading the map rather than mid-run.
395
- for (const r of roles) {
396
- try {
397
- settingsFor(config, r.name);
398
- } catch (e) {
399
- // Whatever settingsFor refuses, `run` will refuse too. The label used to
400
- // say "unenforceable-glob" for every one of them, which mislabelled the
401
- // key-outside-its-directory error as a glob problem — a warning that
402
- // names the wrong cause sends the reader to the wrong line.
403
- warnings.push({
404
- kind: "cannot-be-enforced",
405
- headline: `${r.name}: ${e.message.split("\n")[0]}`,
406
- detail: e.message.split("\n").slice(1).map((s) => s.trim()).filter(Boolean).join(" "),
407
- });
408
- }
409
- }
410
- warnings.push(...roleKeyWarnings(roles, config));
411
-
412
- // A provider the parent could not find where no role writes. `run` refuses
413
- // it with the same sentence; here it is found before anybody runs anything.
414
- for (const [scheme, p] of Object.entries(config.keyProviders ?? {})) {
415
- const cmd = p.command?.[0];
416
- if (!cmd || cmd.includes("/")) continue;
417
- try { resolveExecutable(cmd, config); } catch (e) {
418
- if (!/a role can write/.test(e.message)) continue;
419
- warnings.push({
420
- kind: "provider-in-territory",
421
- headline: `the ${scheme}:// provider "${cmd}" is only found where a role can write`,
422
- detail: e.message.split("\n").slice(1).map((l) => l.trim()).join(" "),
423
- });
424
- }
425
- }
426
-
427
- // PATH entries that are not absolute resolve against wherever the parent is
428
- // standing, which is the repo. They are ignored for every lookup seisin makes
429
- // outside the sandbox; said here because a lookup that skips them can find a
430
- // different program than your shell does.
431
- const unanchored = (process.env.PATH ?? "").split(delimiter).filter((d) => !d || !d.startsWith("/"));
432
- if (unanchored.length)
433
- warnings.push({
434
- kind: "relative-path",
435
- headline: `PATH has ${unanchored.length} relative entr${unanchored.length === 1 ? "y" : "ies"} (${unanchored.map((d) => JSON.stringify(d)).join(", ")})`,
436
- detail: "They resolve inside the repo, where roles write, so seisin skips them when it looks for a program to run outside the sandbox.",
437
- });
438
- if (shared.length)
439
- warnings.push({
440
- kind: "shared",
441
- headline: `${shared.length} path(s) claimed by more than one role: ${shared.join(" ")}`,
442
- detail: "Overlap is allowed — seisin will name every owner. It is listed so it stays a decision.",
443
- });
444
-
445
- // Scratch is granted to every role, so a repo sitting inside it is writable
446
- // by all of them no matter what the territory says. Saying so is the
447
- // difference between a limitation and a trap — and it is not hypothetical:
448
- // the test fixture lived in the temp dir and made a boundary test pass for
449
- // the wrong reason.
450
- // Both sides resolved, or the comparison is a coin flip. `expand()` follows
451
- // symlinks — it has to, because on macOS /tmp is a link to /private/tmp and a
452
- // grant written as /tmp grants nothing — so the root has to be followed too.
453
- // Without this a repo at /tmp/x is never reported as sitting in scratch,
454
- // which is the one case where the warning matters most.
455
- const scratch = (config.runtimeWrites ?? RUNTIME_WRITES).map(expand);
456
- const root = realOrSelf(resolve(config.root));
457
- const inside = scratch.filter((s) => root === s || root.startsWith(s + "/"));
458
- if (inside.length)
459
- warnings.push({
460
- kind: "scratch",
461
- headline: `this repo lives inside shared scratch space (${inside[0]})`,
462
- detail: "Every role can write scratch, so territory does not hold here. Move the repo, or set [runtime] writes = [].",
463
- });
464
-
465
- /**
466
- * A role that can write code and cannot reach any inference API.
467
- *
468
- * Reported three separate times, by three different people, with the same
469
- * shape each time: a list of what agents need, written somewhere that does
470
- * not know which agent will run. The last instance cost six bench runs that
471
- * finished in eight seconds with `403 Connection blocked by network
472
- * allowlist` — which in a results table reads as "this model cannot do the
473
- * task", not as a policy error.
474
- *
475
- * What is deliberately NOT here: a registry of agents and their endpoints.
476
- * That is the same defect in a new place, and the reporter said so before I
477
- * could. The list below is only ever consulted to decide whether to print a
478
- * sentence, so being wrong about it costs a false warning — never a broken
479
- * run. That asymmetry is the whole reason it is allowed to exist, and the
480
- * warning says out loud that the list is ours and may not know your agent.
481
- */
482
- const INFERENCE = /(anthropic|openai|googleapis|generativelanguage|bedrock|azure|openrouter|mistral|groq|together|deepseek|x\.ai|cohere|ollama|localhost|127\.0\.0\.1)/i;
483
- for (const r of roles) {
484
- const domains = r.network ?? config.allowedDomains ?? [];
485
- if (domains.some((d) => INFERENCE.test(d))) continue;
486
- warnings.push({
487
- kind: "no-model-endpoint",
488
- headline: domains.length
489
- ? `${r.name} has ${domains.length} allowed domain(s) and none looks like a model API`
490
- : `${r.name} may reach no network at all`,
491
- detail:
492
- "an agent that cannot reach its own model fails at startup with a 403 from the " +
493
- "egress proxy, which reads as a broken install or a bad model rather than a policy. " +
494
- "This check knows a handful of providers and will not know yours — it is a question, " +
495
- "not a verdict.",
496
- });
497
- }
498
-
499
- // Without the hook, half the record is missing rather than all of it: on macOS
500
- // `seisin run` reads the kernel's own denials, so they still land and
501
- // still queue a request. What goes unrecorded is everything the kernel
502
- // allowed — which is the half `init --from-observations` is built out of, and
503
- // the half that tells "nothing was denied" apart from "nobody was watching".
504
- // The one limit that is about YOUR machine rather than about the repo.
505
- const reach = homeReachWarning(config);
506
- if (reach) warnings.push(reach);
507
-
508
- if (!wired(config.root))
509
- warnings.push({
510
- kind: "hook-not-wired",
511
- headline: "only denials are being recorded: the agent has not been told to run the hook",
512
- detail:
513
- process.platform === "darwin"
514
- ? "the boundary holds either way, and denials are still logged and still queue a " +
515
- "request — `seisin run` reads them from the kernel. What is missing is every " +
516
- "action that was allowed, so `review` cannot tell you which grants are unused and " +
517
- "`init --from-observations` has nothing to build from. Run `seisin wire` once in " +
518
- "this repo."
519
- : "the boundary holds either way — but on this platform the kernel's denials are " +
520
- "not readable either, so log, watch, requests, grant and review all read a record " +
521
- "that nobody is writing. Run `seisin wire` once in this repo.",
522
- });
523
-
524
- else if (broadlyWired(config.root))
525
- warnings.push({
526
- kind: "hook-matcher-broad",
527
- headline: "the hook runs on every tool call: it was wired before 0.5.0 with the matcher \"*\"",
528
- detail:
529
- "nothing is lost — every tool the hook reads is still covered — but each Read, Glob " +
530
- "and TodoWrite starts a process that has nothing to say. Run `seisin wire` again: it " +
531
- "narrows seisin's own entry in place and leaves everyone else's hooks alone.",
532
- });
533
-
534
- // A territory that leaves the repo is supported — a role can own its handover
535
- // note one level up — but it is a different promise from "this repo, divided",
536
- // and it should be a sentence somebody chose rather than a line that drifted.
537
- const loose = []; // roles granting individual files
538
- const listed = []; // role + folder pairs that enumerate
539
-
540
- for (const r of roles) {
541
- const out = r.writes.filter((w) => w.startsWith("/") || w.startsWith("../"));
542
- if (out.length)
543
- warnings.push({
544
- kind: "territory-outside-repo",
545
- headline: `${r.name} writes outside this repo: ${out.join(" ")}`,
546
- detail: "the sandbox will grant it. Nothing here checks what lives there.",
547
- });
548
- }
549
-
550
- /**
551
- * A territory that is not on disk.
552
- *
553
- * A proposal written for another repo — or one typo — reads as a working
554
- * policy: `check` printed the map and the first sign of anything wrong was a
555
- * denial inside the agent. Said per glob, as a warning and never a failure:
556
- * a role can own a folder it is about to create.
557
- */
558
- for (const r of roles) {
559
- for (const g of r.writes) {
560
- if (g === "**" || g.startsWith("/") || g.startsWith("../")) continue;
561
- const fixed = [];
562
- for (const part of g.split("/")) {
563
- if (/[*?[\]]/.test(part)) break;
564
- fixed.push(part);
565
- }
566
- if (!fixed.length || existsSync(join(config.root, ...fixed))) continue;
567
- warnings.push({
568
- kind: "writes-match-nothing",
569
- headline: `${r.name} writes ${g} — matches nothing in this repo`,
570
- detail: "fine if the role is about to create it; otherwise the territory points at a folder that is not there.",
571
- });
572
- }
573
- }
574
-
575
- /**
576
- * A territory inside a key directory.
577
- *
578
- * Seen in use: a role's whole territory sat under the folder declared as
579
- * `[keys] dir`, which is closed to every role, so the role could write
580
- * nothing at all — and `check` printed its territory as if it held. A key
581
- * directory is read by the roles that declare a key in it and written by
582
- * none; a folder a role must write belongs somewhere else.
583
- */
584
- const keyRoots = (config.keyDirs ?? []).map((d) => d.replace(/\/+$/, "")).filter(Boolean);
585
- for (const r of roles) {
586
- for (const g of r.writes) {
587
- const fixed = [];
588
- for (const part of g.split("/")) {
589
- if (/[*?[\]]/.test(part)) break;
590
- fixed.push(part);
591
- }
592
- const at = fixed.join("/");
593
- const dir = at && keyRoots.find((d) => at === d || at.startsWith(d + "/"));
594
- if (!dir) continue;
595
- warnings.push({
596
- kind: "territory-in-key-dir",
597
- headline: `${r.name} writes ${g} — inside the key directory ${dir}, which no role can write`,
598
- detail: "a key directory is read by the roles that declare its keys and written by none. Move what this role writes out of it, or drop the territory.",
599
- });
600
- }
601
- }
602
-
603
- /**
604
- * A file granted by name does not come with its neighbours.
605
- *
606
- * Measured, in production, and it survived two reviews of the policy by people:
607
- * a role was granted its database file and still could not write the
608
- * database. The database opens a second file beside the first — a journal —
609
- * and the sibling was outside the grant, so the write failed in the
610
- * database's own words ("readonly") rather than as a permission error.
611
- *
612
- * What is deliberately NOT here: a list of which programs write which
613
- * siblings. That would ask seisin to know about databases, and the next
614
- * question is why it does not know about a lock file, the temporary file of
615
- * an atomic write, or an editor's backup. The list has no end and every entry
616
- * is a guess about a repo nobody foresaw. The general form needs no list: the
617
- * kernel grants a path and everything under it, so a grant on a file covers
618
- * the file and nothing else. Whether a tool will want a neighbour is the
619
- * reader's to know; that the neighbour is not granted is arithmetic.
620
- */
621
- for (const r of roles) {
622
- const { files, subtrees } = territoryOf(config, r);
623
- // A file inside one of the role's own subtrees has its siblings covered by
624
- // that subtree, so naming it changes nothing and warns about nothing.
625
- const alone = files.filter((f) => !subtrees.some((s) => under(dirname(f.abs), s)));
626
- if (alone.length)
627
- loose.push({ role: r.name, n: alone.length, of: r.writes.length, globs: alone.map((f) => f.glob) });
628
-
629
-
630
- /**
631
- * A territory written as a list of files is a photograph of the folder.
632
- *
633
- * A sandbox cannot say "the source files in this folder" — one level down
634
- * is not a prefix, and `src/*` is refused above for exactly that reason,
635
- * correctly. So a territory that means that has to be written as the files
636
- * that exist today, and the day a file is added the policy is one behind:
637
- * the owning role cannot write its own new file, and nothing fails loudly.
638
- *
639
- * Reported as a count, never as a verdict. "Comparable" here means only
640
- * "a regular file in the same folder" — not the same extension, not the
641
- * same shape of name, because either of those is a guess about what the
642
- * territory *meant*, and a guess about intent is the thing this check
643
- * exists to avoid. The unnamed files are listed by name instead, so the
644
- * reader can see in one glance whether the gap is an oversight or a
645
- * decision. seisin cannot tell, and does not pretend to.
646
- *
647
- * Silent when a folder has fewer than two named files (one file is a
648
- * grant, not an enumeration) and when the folder is also granted as a
649
- * subtree (then the list is decorative).
650
- */
651
- const byDir = new Map();
652
- for (const f of alone) {
653
- const dir = dirname(f.abs);
654
- if (!byDir.has(dir)) byDir.set(dir, []);
655
- byDir.get(dir).push(basename(f.abs));
656
- }
657
- for (const [dir, named] of byDir) {
658
- if (named.length < 2) continue;
659
- const onDisk = regularFilesIn(dir);
660
- const unnamed = onDisk.filter((n) => !named.includes(n));
661
- if (!unnamed.length) continue;
662
- const shown = relative(resolve(config.root), dir);
663
- listed.push({ role: r.name, named: named.length, total: onDisk.length,
664
- where: shown ? shown + "/" : "the repo root", unnamed });
665
-
666
- }
667
- }
668
-
669
- /**
670
- * One warning per finding, not one per role.
671
- *
672
- * These were emitted inside the role loop, which reads fine on the two-role
673
- * fixture they were written against and falls apart on a real policy: 30
674
- * roles produced 39 of these lines and took `seisin check` from 30 lines to
675
- * **297**. That is the defect the detail text below is about — a check nobody
676
- * finishes reading protects nobody — reached from the other side.
677
- *
678
- * So the count leads and a few examples follow. Whoever wants the full list
679
- * has `seisin check <role>`, which is the question they were asking anyway.
680
- */
681
- if (loose.length) {
682
- const shown = loose.slice(0, 3).map((l) => `${l.role} (${l.n} of ${l.of})`).join(", ");
683
- const more = loose.length > 3 ? `, and ${loose.length - 3} more` : "";
684
- warnings.push({
685
- kind: "siblings-uncovered",
686
- headline:
687
- `${loose.length} role(s) grant individual files rather than folders: ${shown}${more}`,
688
- detail:
689
- "the kernel grants exactly that path. Anything a tool creates beside it — a " +
690
- "database's journal, a lock file, the temporary file of an atomic write — is a " +
691
- "sibling, and a sibling is outside the grant; the failure arrives in the tool's " +
692
- "own words, not as a permission error. Grant the folder where the tool needs " +
693
- "neighbours. `seisin check <role>` names the paths for one role.",
694
- });
695
- }
696
-
697
- if (listed.length) {
698
- const shown = listed.slice(0, 3)
699
- .map((l) => `${l.role} names ${l.named} of ${l.total} in ${l.where}`).join("; ");
700
- const more = listed.length > 3 ? `; and ${listed.length - 3} more` : "";
701
- warnings.push({
702
- kind: "enumerated-territory",
703
- headline: `${listed.length} territory(ies) list some files of a folder: ${shown}${more}`,
704
- detail:
705
- `not named, in the first of them: ${listed[0].unnamed.slice(0, 8).join(" ")}` +
706
- `${listed[0].unnamed.length > 8 ? ` … and ${listed[0].unnamed.length - 8} more` : ""}` +
707
- `. A sandbox grants a path, ` +
708
- `not "the files of this kind here", so a territory that means that is the list of files ` +
709
- `that existed when it was written — the next one added lands outside it, and nothing ` +
710
- `fails loudly. Whether these are left out on purpose is not something a count can tell.`,
711
- });
712
- }
713
-
714
- // `env` is a hole in the [keys] model, on purpose: some tools only take a
715
- // credential through the environment. It should still be loud, because it is
716
- // the one place a secret reaches a role without being a declared key.
717
- const SECRETISH = /(TOKEN|KEY|SECRET|PASSWORD|PASSWD|AUTH|COOKIE|SESSION|PRIVATE|CREDENTIAL)/i;
718
- for (const r of roles) {
719
- const risky = (r.env ?? []).filter((n) => SECRETISH.test(n));
720
- if (risky.length)
721
- warnings.push({
722
- kind: "secret-through-env",
723
- headline: `${r.name} receives ${risky.join(", ")} through the environment`,
724
- detail:
725
- "that bypasses [keys] entirely: it is not scoped, not redacted by name, and not " +
726
- "visible in the key map. Declare it as a key file where the tool allows one.",
727
- });
728
- }
729
-
730
- /**
731
- * A policy with no credential floor at all, in a repo that has credentials.
732
- *
733
- * `[keys] dir` is what produces `denyRead`. Without it the emitted settings
734
- * carry an empty deny list, so every role reads every secret in the
735
- * repository — and until this warning, nothing said so. The existing
736
- * `keys-unscoped` only fires when a role *lists* keys, which is the case
737
- * where somebody already thought about it.
738
- *
739
- * The dangerous shape is the other one, and it is not hypothetical: a policy
740
- * generated by a script. The deployment this was built against generates its
741
- * `seisin.toml` from a Python program, and declares `[keys] dir` mostly so
742
- * `scan` skips those directories. A generator that dropped that one line
743
- * would hand every role the credential tree and produce a clean `check`.
744
- *
745
- * Credit where it is due: this is `L-01` of a code review of another
746
- * project's contract type, which makes its credential floor impossible to
747
- * construct empty. That is the right shape and it is a bigger change than a
748
- * warning. This is the cheap half — and the review's claim that the file
749
- * path was safe turned out to be generous: it was not.
750
- *
751
- * Deliberately NOT a full `seisin scan`: that walks the tree and `check` has
752
- * to stay instant. A shallow look at the repository root catches the
753
- * conventional places and costs one `readdir`.
754
- */
755
- if ((config.keyDirs ?? []).length === 0) {
756
- const found = credentialish(config.root);
757
- if (found.length)
758
- warnings.push({
759
- kind: "no-key-floor",
760
- headline: `no [keys] dir is declared, and this repo has ${found.join(", ")} — every role can read them`,
761
- detail:
762
- "Without a key directory the emitted policy denies no reads at all. Declare the " +
763
- "directory your secrets live in, and the roles that may read each one.",
764
- });
765
- }
766
-
767
- // Only a key that is a PATH needs a key directory. A config whose secrets all
768
- // live in a vault has no `[keys] dir` and nothing wrong with it, and warning
769
- // there would be the tool insisting on the arrangement it was built for.
770
- if (config.keyDirs.length === 0 &&
771
- Object.values(config.roles).some((r) => entriesOf(r).some((k) => k.kind === "file")))
772
- warnings.push({
773
- kind: "keys-unscoped",
774
- headline: "keys are listed but [keys] dir is unset — nothing will be scoped",
775
- detail: null,
776
- });
777
-
778
- /**
779
- * A provider whose command is not on PATH.
780
- *
781
- * This is the half of validating references that can be done without
782
- * resolving one — which is the whole requirement: a broken policy has to be
783
- * visible in `check`, not on the first run, and certainly not by asking
784
- * somebody's keychain for a password to find out. The scheme and the
785
- * provider are checked when the config loads, so by the time we are here
786
- * they exist; what is left is whether the command does.
787
- *
788
- * A warning and not an error: the binary can legitimately be missing on the
789
- * machine reading the policy and present on the one that runs it, which is
790
- * every CI checkout.
791
- */
792
- const used = new Set();
793
- for (const r of Object.values(config.roles))
794
- for (const k of entriesOf(r)) if (k.kind === "ref") used.add(k.scheme);
795
- for (const scheme of [...used].sort()) {
796
- const provider = config.keyProviders?.[scheme];
797
- if (!provider || onPath(provider.command[0], config)) continue;
798
- warnings.push({
799
- kind: "provider-missing",
800
- headline: `the ${scheme} provider runs "${provider.command[0]}", which is not on PATH`,
801
- detail:
802
- "every key of that scheme will stop the run rather than resolve. Nothing is " +
803
- "substituted for a key that did not resolve, so this fails closed — but it fails " +
804
- "at the start of a run, not here.",
805
- });
806
- }
807
-
808
- return warnings;
809
- }
810
-
811
- /**
812
- * A role's territory, sorted by what the kernel will do with each line.
813
- *
814
- * Three shapes get through `settingsFor` (see EXPRESSIBLE in srt.js): the whole
815
- * repo, a subtree `x/**`, and a literal path. The literal path is the one the
816
- * text cannot settle on its own — `data/base.sqlite` and `data` are both
817
- * literal, and to the kernel the second is a subtree because it is a directory.
818
- * So the disk is asked, once per line: a regular file is a file, a directory is
819
- * a subtree, and anything else — absent, a symlink, a socket — goes in neither
820
- * count rather than being guessed at. That is one `lstat` per declared path,
821
- * bounded by the length of the policy and not the size of the repo, which is
822
- * the line `check` stays on.
823
- *
824
- * Subtrees come back as the absolute prefix the kernel will be handed, so that
825
- * "is this file's folder inside that grant" is the same comparison the kernel
826
- * makes — a prefix — and not a second reading of the glob.
827
- */
828
- function territoryOf(config, role) {
829
- const root = resolve(config.root);
830
- const abs = (p) => resolve(root, p);
831
- const files = [];
832
- const subtrees = [];
833
- for (const glob of role.writes) {
834
- if (glob === "**" || glob.endsWith("/**")) {
835
- subtrees.push(abs(glob === "**" ? "." : glob.slice(0, -3)));
836
- continue;
837
- }
838
- if (/[*?[\]]/.test(glob)) continue; // already refused above as cannot-be-enforced
839
- const s = lstatOrNull(abs(glob));
840
- if (s?.isFile()) files.push({ glob, abs: abs(glob) });
841
- else if (s?.isDirectory()) subtrees.push(abs(glob));
842
- }
843
- return { files, subtrees };
844
- }
845
-
846
- /** Is `p` the prefix, or somewhere beneath it. The kernel's own test, in one line. */
847
- function under(p, prefix) {
848
- return p === prefix || p.startsWith(prefix + "/");
849
- }
850
-
851
- /** The regular files directly inside `dir`, by name. Not folders, not links, not deeper. */
852
- function regularFilesIn(dir) {
853
- try {
854
- return readdirSync(dir, { withFileTypes: true }).filter((d) => d.isFile()).map((d) => d.name).sort();
855
- } catch {
856
- return [];
857
- }
858
- }
859
-
860
- function lstatOrNull(p) {
861
- try {
862
- return lstatSync(p);
863
- } catch {
864
- return null;
865
- }
866
- }