@openparachute/agent 0.2.3-rc.2 → 0.2.3-rc.3

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 (54) hide show
  1. package/package.json +4 -1
  2. package/src/transports/vault.ts +19 -1
  3. package/src/_parked/interactive-spawn.test.ts +0 -324
  4. package/src/_parked/interactive-spawn.ts +0 -701
  5. package/src/agent-defs.test.ts +0 -1504
  6. package/src/agent-mcp-config.test.ts +0 -115
  7. package/src/agents.test.ts +0 -360
  8. package/src/auth.test.ts +0 -46
  9. package/src/backends/attached-queue.test.ts +0 -376
  10. package/src/backends/programmatic.test.ts +0 -1715
  11. package/src/backends/registry.test.ts +0 -1494
  12. package/src/backends/stream-json.test.ts +0 -570
  13. package/src/channel-backend-wiring.test.ts +0 -237
  14. package/src/credentials.test.ts +0 -274
  15. package/src/cron.test.ts +0 -342
  16. package/src/daemon-agent-def-api.test.ts +0 -166
  17. package/src/daemon-agent-defs-api.test.ts +0 -953
  18. package/src/daemon-agent-env-api.test.ts +0 -338
  19. package/src/daemon-attached-queue-store.test.ts +0 -65
  20. package/src/daemon-config-api.test.ts +0 -962
  21. package/src/daemon-jobs-api.test.ts +0 -271
  22. package/src/daemon-vault-chat.test.ts +0 -250
  23. package/src/daemon.test.ts +0 -746
  24. package/src/def-vaults.test.ts +0 -136
  25. package/src/delivery-state.test.ts +0 -110
  26. package/src/effective-env.test.ts +0 -114
  27. package/src/grants.test.ts +0 -638
  28. package/src/hub-jwt.test.ts +0 -161
  29. package/src/jobs.test.ts +0 -245
  30. package/src/mcp-http.test.ts +0 -265
  31. package/src/mint-token.test.ts +0 -152
  32. package/src/module-manifest.test.ts +0 -158
  33. package/src/programmatic-wiring.test.ts +0 -838
  34. package/src/registry.test.ts +0 -227
  35. package/src/resolve-port.test.ts +0 -64
  36. package/src/routing.test.ts +0 -184
  37. package/src/runner.test.ts +0 -506
  38. package/src/sandbox/config.test.ts +0 -150
  39. package/src/sandbox/egress.test.ts +0 -113
  40. package/src/sandbox/live-seatbelt.test.ts +0 -277
  41. package/src/sandbox/mounts.test.ts +0 -154
  42. package/src/sandbox/sandbox.test.ts +0 -168
  43. package/src/services-manifest.test.ts +0 -106
  44. package/src/spa-serve.test.ts +0 -116
  45. package/src/spawn-agent-cli.test.ts +0 -172
  46. package/src/spawn-agent.test.ts +0 -1218
  47. package/src/spawn-deps.test.ts +0 -54
  48. package/src/terminal-assets.test.ts +0 -50
  49. package/src/terminal.test.ts +0 -530
  50. package/src/transports/http-ui.test.ts +0 -455
  51. package/src/transports/telegram.test.ts +0 -174
  52. package/src/transports/vault.test.ts +0 -2012
  53. package/src/ui-kit.test.ts +0 -178
  54. package/web/ui/tsconfig.json +0 -21
@@ -1,506 +0,0 @@
1
- import { describe, test, expect } from "bun:test";
2
- import { Runner, type TickDriver } from "./runner.ts";
3
- import type { Job } from "./jobs.ts";
4
- import {
5
- ProgrammaticAgentRegistry,
6
- type WriteOutbound,
7
- type WriteThread,
8
- type ThreadNote,
9
- } from "./backends/registry.ts";
10
- import type {
11
- AgentBackend,
12
- AgentHandle,
13
- AgentStatus,
14
- DeliverResult,
15
- InterimSink,
16
- TurnSession,
17
- } from "./backends/types.ts";
18
- import type { AgentSpec } from "./sandbox/types.ts";
19
-
20
- /** A controllable clock — tests step time by setting `current`. */
21
- function fakeClock(startIso: string) {
22
- let current = new Date(startIso);
23
- return {
24
- now: () => new Date(current.getTime()),
25
- set: (iso: string) => {
26
- current = new Date(iso);
27
- },
28
- };
29
- }
30
-
31
- /** A manual tick driver — the test calls `runScheduled()` to fire the scheduled fn. */
32
- function manualDriver(): TickDriver & { runScheduled: () => void; scheduledMs: number } {
33
- let fn: (() => void) | null = null;
34
- let ms = 0;
35
- return {
36
- schedule(f, intervalMs) {
37
- fn = f;
38
- ms = intervalMs;
39
- return { cancel: () => { fn = null; } };
40
- },
41
- runScheduled() {
42
- if (fn) fn();
43
- },
44
- get scheduledMs() {
45
- return ms;
46
- },
47
- };
48
- }
49
-
50
- function job(over: Partial<Job> = {}): Job {
51
- return {
52
- id: "j",
53
- channel: "uni-dev",
54
- message: "go",
55
- schedule: { cron: "0 * * * *", tz: "UTC" }, // hourly, top of hour
56
- enabled: true,
57
- createdAt: "2026-06-17T00:00:00.000Z",
58
- ...over,
59
- };
60
- }
61
-
62
- const silent = { warn: () => {}, error: () => {} };
63
-
64
- /** A store stub: jobs the runner loads + a record of persisted bookkeeping. */
65
- function store(jobs: Job[]) {
66
- const persisted: Array<{ id: string; lastStatus?: string; lastRunAt?: string }> = [];
67
- return {
68
- jobs,
69
- persisted,
70
- loadJobs: async () => jobs.map((j) => ({ ...j })), // fresh copies each tick (vault-like)
71
- persistFire: async (j: Job) => {
72
- persisted.push({ id: j.id, lastStatus: j.lastStatus, lastRunAt: j.lastRunAt });
73
- },
74
- };
75
- }
76
-
77
- describe("Runner.tick — horizon seeding + due detection", () => {
78
- test("a job seen for the first time gets a future horizon and does NOT fire", async () => {
79
- const clock = fakeClock("2026-06-17T10:30:00Z");
80
- const fired: string[] = [];
81
- const s = store([job()]);
82
- const r = new Runner({
83
- loadJobs: s.loadJobs,
84
- fire: async (j) => { fired.push(j.id); },
85
- persistFire: s.persistFire,
86
- now: clock.now,
87
- log: silent,
88
- });
89
- await r.tick();
90
- expect(fired).toEqual([]); // seeded horizon is 11:00, not due at 10:30.
91
- });
92
-
93
- test("fires exactly when the horizon is due, once per slot", async () => {
94
- const clock = fakeClock("2026-06-17T10:30:00Z");
95
- const fired: string[] = [];
96
- const s = store([job()]);
97
- const r = new Runner({
98
- loadJobs: s.loadJobs,
99
- fire: async (j) => { fired.push(j.id); },
100
- persistFire: s.persistFire,
101
- now: clock.now,
102
- log: silent,
103
- });
104
- await r.tick(); // seeds horizon 11:00
105
- expect(fired).toEqual([]);
106
-
107
- clock.set("2026-06-17T11:00:00Z");
108
- await r.tick(); // due → fires
109
- expect(fired).toEqual(["j"]);
110
- expect(s.persisted.at(-1)).toMatchObject({ id: "j", lastStatus: "ok", lastRunAt: "2026-06-17T11:00:00.000Z" });
111
-
112
- clock.set("2026-06-17T11:05:00Z");
113
- await r.tick(); // next horizon is 12:00 → not due
114
- expect(fired).toEqual(["j"]);
115
- });
116
-
117
- test("a disabled job never fires even when its horizon would be due", async () => {
118
- const clock = fakeClock("2026-06-17T11:00:00Z");
119
- const fired: string[] = [];
120
- const s = store([job({ enabled: false })]);
121
- const r = new Runner({
122
- loadJobs: s.loadJobs,
123
- fire: async (j) => { fired.push(j.id); },
124
- persistFire: s.persistFire,
125
- now: clock.now,
126
- log: silent,
127
- });
128
- await r.tick();
129
- clock.set("2026-06-17T12:00:00Z");
130
- await r.tick();
131
- expect(fired).toEqual([]);
132
- });
133
- });
134
-
135
- describe("Runner.tick — fire-once-on-miss (no stampede)", () => {
136
- test("a job whose horizon was seeded then time jumps far ahead fires ONCE", async () => {
137
- const clock = fakeClock("2026-06-17T05:30:00Z");
138
- let fireCount = 0;
139
- const s = store([job()]);
140
- const r = new Runner({
141
- loadJobs: s.loadJobs,
142
- fire: async () => { fireCount++; },
143
- persistFire: s.persistFire,
144
- now: clock.now,
145
- log: silent,
146
- });
147
- await r.tick(); // seeds horizon 06:00
148
- // Daemon "down" — next tick is at 11:30, well past 06:00 and several slots.
149
- clock.set("2026-06-17T11:30:00Z");
150
- await r.tick();
151
- expect(fireCount).toBe(1); // exactly once despite 5+ missed slots
152
- // Horizon recomputed forward from 11:30 → 12:00 (not 07:00).
153
- expect(s.jobs[0]!.nextRunAt).toBeUndefined(); // store copy untouched; check via next tick
154
- clock.set("2026-06-17T11:45:00Z");
155
- await r.tick();
156
- expect(fireCount).toBe(1); // still not due (next is 12:00)
157
- clock.set("2026-06-17T12:00:00Z");
158
- await r.tick();
159
- expect(fireCount).toBe(2);
160
- });
161
- });
162
-
163
- describe("Runner.tick — overlap guard (idempotent under slow fire)", () => {
164
- test("a job mid-fire is skipped by an interleaving tick", async () => {
165
- const clock = fakeClock("2026-06-17T11:00:00Z");
166
- let resolveFire!: () => void;
167
- let fireCount = 0;
168
- const s = store([job()]);
169
- const r = new Runner({
170
- loadJobs: s.loadJobs,
171
- fire: () => {
172
- fireCount++;
173
- return new Promise<void>((res) => { resolveFire = res; });
174
- },
175
- persistFire: s.persistFire,
176
- now: clock.now,
177
- log: silent,
178
- });
179
- await r.tick(); // seeds horizon 12:00 — but set clock so it's due:
180
- clock.set("2026-06-17T12:00:00Z");
181
-
182
- const t1 = r.tick(); // starts a fire that hasn't resolved
183
- // Let the tick reach the fire (it awaits loadJobs first).
184
- await new Promise((res) => setTimeout(res, 0));
185
- expect(fireCount).toBe(1);
186
-
187
- await r.tick(); // interleaving tick — job in-flight → skipped
188
- expect(fireCount).toBe(1);
189
-
190
- resolveFire();
191
- await t1;
192
- });
193
- });
194
-
195
- describe("Runner.tick — fire failure recorded, never thrown", () => {
196
- test("a throwing fire records error status + advances the horizon", async () => {
197
- const clock = fakeClock("2026-06-17T11:00:00Z");
198
- const s = store([job()]);
199
- const r = new Runner({
200
- loadJobs: s.loadJobs,
201
- fire: async () => { throw new Error("vault down"); },
202
- persistFire: s.persistFire,
203
- now: clock.now,
204
- log: silent,
205
- });
206
- await r.tick(); // seed
207
- clock.set("2026-06-17T12:00:00Z");
208
- await r.tick(); // due → fire throws, recorded
209
- expect(s.persisted.at(-1)).toMatchObject({ id: "j", lastStatus: "error: vault down" });
210
- });
211
-
212
- test("one bad job does not abort the pass for a good one", async () => {
213
- const clock = fakeClock("2026-06-17T11:00:00Z");
214
- const fired: string[] = [];
215
- const s = store([job({ id: "bad" }), job({ id: "good" })]);
216
- const r = new Runner({
217
- loadJobs: s.loadJobs,
218
- fire: async (j) => {
219
- if (j.id === "bad") throw new Error("boom");
220
- fired.push(j.id);
221
- },
222
- persistFire: s.persistFire,
223
- now: clock.now,
224
- log: silent,
225
- });
226
- await r.tick(); // seed both
227
- clock.set("2026-06-17T12:00:00Z");
228
- await r.tick(); // both due
229
- expect(fired).toEqual(["good"]);
230
- const byId = Object.fromEntries(s.persisted.map((p) => [p.id, p.lastStatus]));
231
- expect(byId.bad).toMatch(/error/);
232
- expect(byId.good).toBe("ok");
233
- });
234
- });
235
-
236
- describe("Runner.tick — load failure is a no-op tick", () => {
237
- test("a loadJobs rejection does not throw out of tick", async () => {
238
- const r = new Runner({
239
- loadJobs: async () => { throw new Error("vault unreachable"); },
240
- fire: async () => {},
241
- persistFire: async () => {},
242
- log: silent,
243
- });
244
- await r.tick(); // must resolve, not reject
245
- expect(true).toBe(true);
246
- });
247
- });
248
-
249
- describe("Runner.tick — deleted jobs prune their horizon", () => {
250
- test("a job removed from the store stops being tracked", async () => {
251
- const clock = fakeClock("2026-06-17T10:30:00Z");
252
- let current: Job[] = [job()];
253
- const r = new Runner({
254
- loadJobs: async () => current.map((j) => ({ ...j })),
255
- fire: async () => {},
256
- persistFire: async () => {},
257
- now: clock.now,
258
- log: silent,
259
- });
260
- await r.tick(); // seeds horizon for "j"
261
- current = []; // job deleted from the vault
262
- await r.tick(); // prunes — no throw, nothing to fire
263
- expect(true).toBe(true);
264
- });
265
- });
266
-
267
- describe("Runner.runNow — fire on demand", () => {
268
- test("fires immediately regardless of schedule + persists bookkeeping", async () => {
269
- const clock = fakeClock("2026-06-17T10:30:00Z");
270
- const fired: string[] = [];
271
- const s = store([job()]);
272
- const r = new Runner({
273
- loadJobs: s.loadJobs,
274
- fire: async (j) => { fired.push(j.id); },
275
- persistFire: s.persistFire,
276
- now: clock.now,
277
- log: silent,
278
- });
279
- const status = await r.runNow("j");
280
- expect(status).toBe("ok");
281
- expect(fired).toEqual(["j"]);
282
- expect(s.persisted.at(-1)).toMatchObject({ id: "j", lastRunAt: "2026-06-17T10:30:00.000Z" });
283
- });
284
-
285
- test("runNow on an unknown id throws", async () => {
286
- const r = new Runner({
287
- loadJobs: async () => [],
288
- fire: async () => {},
289
- persistFire: async () => {},
290
- log: silent,
291
- });
292
- await expect(r.runNow("nope")).rejects.toThrow(/no job/);
293
- });
294
-
295
- test("runNow records an error status without throwing on a fire failure", async () => {
296
- const s = store([job()]);
297
- const r = new Runner({
298
- loadJobs: s.loadJobs,
299
- fire: async () => { throw new Error("nope"); },
300
- persistFire: s.persistFire,
301
- log: silent,
302
- });
303
- const status = await r.runNow("j");
304
- expect(status).toMatch(/error: nope/);
305
- });
306
- });
307
-
308
- describe("Runner — driver wiring (start/stop)", () => {
309
- test("start schedules the tick on the injected driver; stop cancels", async () => {
310
- const clock = fakeClock("2026-06-17T12:00:00Z");
311
- const driver = manualDriver();
312
- const fired: string[] = [];
313
- // Pre-seed via a horizon that's already due: a tick first seeds (future), so
314
- // to observe a fire through the driver we drive twice with time advanced.
315
- const s = store([job()]);
316
- const r = new Runner({
317
- loadJobs: s.loadJobs,
318
- fire: async (j) => { fired.push(j.id); },
319
- persistFire: s.persistFire,
320
- now: clock.now,
321
- driver,
322
- intervalMs: 30_000,
323
- log: silent,
324
- });
325
- r.start();
326
- expect(driver.scheduledMs).toBe(30_000);
327
-
328
- driver.runScheduled(); // tick 1 — seeds horizon 13:00
329
- await new Promise((res) => setTimeout(res, 0));
330
- expect(fired).toEqual([]);
331
-
332
- clock.set("2026-06-17T13:00:00Z");
333
- driver.runScheduled(); // tick 2 — due → fires
334
- await new Promise((res) => setTimeout(res, 0));
335
- expect(fired).toEqual(["j"]);
336
-
337
- r.stop();
338
- fired.length = 0;
339
- clock.set("2026-06-17T14:00:00Z");
340
- driver.runScheduled(); // cancelled — no-op
341
- await new Promise((res) => setTimeout(res, 0));
342
- expect(fired).toEqual([]);
343
- });
344
-
345
- test("start is idempotent (a second start does not double-schedule)", () => {
346
- let scheduleCalls = 0;
347
- const driver: TickDriver = {
348
- schedule(_fn, _ms) {
349
- scheduleCalls++;
350
- return { cancel: () => {} };
351
- },
352
- };
353
- const r = new Runner({
354
- loadJobs: async () => [],
355
- fire: async () => {},
356
- persistFire: async () => {},
357
- driver,
358
- log: silent,
359
- });
360
- r.start();
361
- r.start();
362
- expect(scheduleCalls).toBe(1);
363
- });
364
- });
365
-
366
- // ---------------------------------------------------------------------------
367
- // Runner ↔ mode-aware deliver — a scheduled fire honors the DEF's mode.
368
- //
369
- // The runner is mode-AGNOSTIC: `fire(job)` just authors a synthetic inbound onto the
370
- // job's channel. The def's `mode` governs downstream at the deliver chokepoint. This
371
- // is what fixes "a scheduled job silently resumes the chat thread" — the operator
372
- // expresses ephemerality via `mode: multi-threaded` on the DEF, and the runner's fire
373
- // then runs a fresh turn + materializes a per-fire thread note (a single-threaded def's
374
- // fire resumes the thread as today + upserts its one thread note). We wire the runner's
375
- // `fire` to the REAL registry enqueue path
376
- // (fire → enqueue → mode-aware deliver) so the end-to-end behavior is asserted, not
377
- // just the runner-in-isolation contract.
378
- // ---------------------------------------------------------------------------
379
-
380
- /**
381
- * A fake backend that records whether each turn RESUMED — read off the {@link TurnSession}
382
- * the REGISTRY hands it (the daemon now owns the session uuid; the backend reads no store).
383
- * The registry resolves resume-vs-create from the thread note's persisted session (the
384
- * test wires `readSession` to simulate a prior session), so a faithful assertion is "what
385
- * did the registry decide?", surfaced via `session.resume`.
386
- */
387
- class ModeFakeBackend implements AgentBackend {
388
- readonly kind = "programmatic";
389
- readonly resumed = new Map<string, boolean>(); // channel → did this turn resume?
390
-
391
- async start(spec: AgentSpec): Promise<AgentHandle> {
392
- return { backend: this.kind, channel: spec.channels[0] as string, name: spec.name, spec };
393
- }
394
- async deliver(
395
- handle: AgentHandle,
396
- message: string,
397
- session: TurnSession,
398
- _onInterim?: InterimSink,
399
- ): Promise<DeliverResult> {
400
- // The registry resolved the session (resume an existing one vs create a fresh uuid);
401
- // the backend just records what it was handed. Echo the id back so the registry persists
402
- // it onto the thread note.
403
- this.resumed.set(handle.channel, session.resume);
404
- return { ok: true, reply: "did: " + message, sessionId: session.id };
405
- }
406
- async stop(_handle: AgentHandle): Promise<void> {}
407
- async status(_handle: AgentHandle): Promise<AgentStatus> {
408
- return { live: true };
409
- }
410
- }
411
-
412
- const noopOutbound: WriteOutbound = async () => {};
413
- // A thread recorder that splits the thread-as-container start-ensure (phase:start) from the
414
- // final record (phase:end), so a test asserts the FINAL turn record without counting the
415
- // working-ensure the registry now writes before every turn.
416
- function threadRec(): {
417
- threads: ThreadNote[];
418
- ends: () => ThreadNote[];
419
- starts: () => ThreadNote[];
420
- fn: WriteThread;
421
- } {
422
- const threads: ThreadNote[] = [];
423
- return {
424
- threads,
425
- ends: () => threads.filter((t) => t.phase !== "start"),
426
- starts: () => threads.filter((t) => t.phase === "start"),
427
- fn: async (t) => void threads.push(t),
428
- };
429
- }
430
- async function flushTurns(pred: () => boolean, tries = 200): Promise<void> {
431
- for (let i = 0; i < tries && !pred(); i++) await new Promise<void>((r) => setTimeout(r, 1));
432
- }
433
-
434
- /** Wire a runner whose `fire` enqueues onto the registry (the real fire→deliver path). */
435
- function wireRunner(reg: ProgrammaticAgentRegistry, clock = fakeClock("2026-06-17T10:30:00Z")) {
436
- return new Runner({
437
- loadJobs: async () => [],
438
- fire: async (j: Job) => {
439
- reg.enqueue(j.channel, { content: j.message });
440
- },
441
- persistFire: async () => {},
442
- now: clock.now,
443
- log: silent,
444
- });
445
- }
446
-
447
- describe("Runner — a scheduled fire honors the def's mode", () => {
448
- test("a MULTI-THREADED def's scheduled fire is fresh (no resume) + materializes a thread note", async () => {
449
- const backend = new ModeFakeBackend();
450
- const threads = threadRec();
451
- // Even with a prior session AVAILABLE on the note, a multi-threaded fire must NOT
452
- // consult readSession (each fire is a fresh thread) — wire one that WOULD return a
453
- // prior to prove it's ignored.
454
- const reg = new ProgrammaticAgentRegistry({
455
- backend,
456
- writeOutbound: noopOutbound,
457
- writeThread: threads.fn,
458
- readSession: async () => "sess-OLD",
459
- });
460
- await reg.register({ name: "digest", channels: ["digest"], mode: "multi-threaded", definition: "Agents/digest" });
461
-
462
- const r = wireRunner(reg);
463
- // runNow needs the job in the store; drive `fire` directly (the runner's fire is
464
- // what we're testing routes through the mode-aware deliver).
465
- await r["fire"](job({ id: "digest-job", channel: "digest", message: "run the digest" }));
466
- await flushTurns(() => threads.ends().length === 1);
467
-
468
- // Fresh-per-fire: the scheduled fire did NOT resume the chat thread.
469
- expect(backend.resumed.get("digest")).toBe(false);
470
- // …and it materialized one FINAL thread note per fire (the multi-threaded record), after a
471
- // working-ensure (thread-as-container — the thread is visible the moment the fire starts).
472
- expect(threads.starts()).toHaveLength(1);
473
- expect(threads.ends()).toHaveLength(1);
474
- expect(threads.ends()[0]!.mode).toBe("multi-threaded");
475
- expect(threads.ends()[0]!.status).toBe("ok");
476
- expect(threads.ends()[0]!.input).toBe("run the digest");
477
- });
478
-
479
- test("REGRESSION: a SINGLE-THREADED def's scheduled fire RESUMES the thread + materializes ONE thread note", async () => {
480
- const backend = new ModeFakeBackend();
481
- const threads = threadRec();
482
- // A prior turn established the thread → its session lives on the thread note; the
483
- // registry reads it back (readSession) and resumes. Wire a reader returning a prior.
484
- const reg = new ProgrammaticAgentRegistry({
485
- backend,
486
- writeOutbound: noopOutbound,
487
- writeThread: threads.fn,
488
- readSession: async () => "sess-EXISTING",
489
- });
490
- // No mode → single-threaded (the default = today's behavior).
491
- await reg.register({ name: "uni-dev", channels: ["uni-dev"] });
492
-
493
- const r = wireRunner(reg);
494
- await r["fire"](job({ id: "uni-job", channel: "uni-dev", message: "daily check-in" }));
495
- await flushTurns(() => threads.ends().length === 1);
496
-
497
- // A single-threaded def's scheduled fire RESUMES the existing chat thread (today's behavior).
498
- expect(backend.resumed.get("uni-dev")).toBe(true);
499
- // …and materializes ONE FINAL thread note (the unified model — single-threaded now writes a
500
- // thread note too, named after the def, holding a rolling summary), after a working-ensure.
501
- expect(threads.starts()).toHaveLength(1);
502
- expect(threads.ends()).toHaveLength(1);
503
- expect(threads.ends()[0]!.mode).toBe("single-threaded");
504
- expect(threads.ends()[0]!.name).toBe("uni-dev");
505
- });
506
- });
@@ -1,150 +0,0 @@
1
- import { describe, test, expect } from "bun:test";
2
- import { buildSandboxConfig } from "./config.ts";
3
- import type { AgentSpec, BaseBinds } from "./types.ts";
4
- import type { EgressBaseInput } from "./egress.ts";
5
-
6
- const BASE_BINDS: BaseBinds = {
7
- workspace: "/state/sessions/arm",
8
- runtimeReadOnly: ["/home/op/.claude"],
9
- };
10
- const EGRESS_BASE: EgressBaseInput = { hubOrigin: "https://hub.example.com" };
11
-
12
- // Most cases exercise the egress floor, which needs network "restricted". Scoped
13
- // reads are the DEFAULT (filesystem "workspace"), so the helper only sets the
14
- // network and leaves filesystem at its default. A spread `p` overrides (e.g.
15
- // `filesystem: "full"` to test broad reads).
16
- function specOf(p: Partial<AgentSpec> = {}): AgentSpec {
17
- return { name: "arm", channels: ["ch"], network: "restricted", ...p };
18
- }
19
-
20
- describe("buildSandboxConfig — defaults (scoped reads + open network)", () => {
21
- test("DEFAULT: scoped reads (home tree denied) + open network (no allowedDomains), writes confined", () => {
22
- const cfg = buildSandboxConfig({
23
- spec: { name: "arm", channels: ["ch"] }, // no filesystem/network → both defaults
24
- baseBinds: BASE_BINDS,
25
- egressBase: EGRESS_BASE,
26
- platform: "darwin",
27
- });
28
- // Scoped reads by default: the home tree is DENIED — this is what keeps the
29
- // operator's secrets (~/.parachute/operator.token, SSH keys) unreadable.
30
- expect(cfg.filesystem.denyRead).toContain("/Users");
31
- // Open network by default: allowedDomains omitted entirely (runtime = no restriction).
32
- expect((cfg.network as { allowedDomains?: string[] }).allowedDomains).toBeUndefined();
33
- // Writes confined to the workspace.
34
- expect(cfg.filesystem.allowWrite).toContain("/state/sessions/arm");
35
- });
36
-
37
- test("filesystem 'full': broad reads (no home-tree deny), writes still confined", () => {
38
- const cfg = buildSandboxConfig({
39
- spec: { name: "arm", channels: ["ch"], filesystem: "full" },
40
- baseBinds: BASE_BINDS,
41
- egressBase: EGRESS_BASE,
42
- platform: "darwin",
43
- });
44
- expect(cfg.filesystem.denyRead).toEqual([]);
45
- expect(cfg.filesystem.allowWrite).toContain("/state/sessions/arm");
46
- });
47
- });
48
-
49
- describe("buildSandboxConfig — spec → SandboxRuntimeConfig", () => {
50
- test("network: deny-by-default + base floor present, deniedDomains empty", () => {
51
- const cfg = buildSandboxConfig({
52
- spec: specOf({ egress: [] }),
53
- baseBinds: BASE_BINDS,
54
- egressBase: EGRESS_BASE,
55
- platform: "darwin",
56
- });
57
- expect(cfg.network.allowedDomains).toContain("api.anthropic.com");
58
- expect(cfg.network.allowedDomains).toContain("hub.example.com");
59
- expect(cfg.network.deniedDomains).toEqual([]);
60
- });
61
-
62
- test("SECURITY: a spec with foreign egress still carries the base floor", () => {
63
- const cfg = buildSandboxConfig({
64
- spec: specOf({ egress: ["registry.npmjs.org"] }),
65
- baseBinds: BASE_BINDS,
66
- egressBase: EGRESS_BASE,
67
- platform: "darwin",
68
- });
69
- expect(cfg.network.allowedDomains).toContain("api.anthropic.com");
70
- expect(cfg.network.allowedDomains).toContain("hub.example.com");
71
- expect(cfg.network.allowedDomains).toContain("registry.npmjs.org");
72
- });
73
-
74
- test("filesystem: scoped reads (deny home tree, re-allow binds) + write confinement", () => {
75
- const cfg = buildSandboxConfig({
76
- spec: specOf({ mounts: [{ hostPath: "/proj", mountPath: "/work", mode: "rw" }] }),
77
- baseBinds: BASE_BINDS,
78
- egressBase: EGRESS_BASE,
79
- platform: "darwin",
80
- });
81
- expect(cfg.filesystem.denyRead).toContain("/Users");
82
- expect(cfg.filesystem.allowRead).toContain("/state/sessions/arm");
83
- expect(cfg.filesystem.allowRead).toContain("/home/op/.claude");
84
- expect(cfg.filesystem.allowRead).toContain("/proj");
85
- expect(cfg.filesystem.allowWrite).toContain("/state/sessions/arm");
86
- expect(cfg.filesystem.allowWrite).toContain("/proj");
87
- });
88
-
89
- test("Linux platform denies /home instead of /Users", () => {
90
- const cfg = buildSandboxConfig({
91
- spec: specOf(),
92
- baseBinds: BASE_BINDS,
93
- egressBase: EGRESS_BASE,
94
- platform: "linux",
95
- });
96
- expect(cfg.filesystem.denyRead).toContain("/home");
97
- expect(cfg.filesystem.denyRead).not.toContain("/Users");
98
- });
99
-
100
- test("allowPty defaults true (interactive claude needs a pty)", () => {
101
- const cfg = buildSandboxConfig({
102
- spec: specOf(),
103
- baseBinds: BASE_BINDS,
104
- egressBase: EGRESS_BASE,
105
- platform: "darwin",
106
- });
107
- expect(cfg.allowPty).toBe(true);
108
- });
109
-
110
- test("ripgrep override threads through when provided", () => {
111
- const cfg = buildSandboxConfig({
112
- spec: specOf(),
113
- baseBinds: BASE_BINDS,
114
- egressBase: EGRESS_BASE,
115
- platform: "darwin",
116
- ripgrep: { command: "/abs/rg" },
117
- });
118
- expect(cfg.ripgrep).toEqual({ command: "/abs/rg" });
119
- });
120
-
121
- test("a restricted-network config carries the full runtime shape (allowedDomains present)", () => {
122
- const cfg = buildSandboxConfig({
123
- spec: specOf(), // network "restricted" → allowedDomains present
124
- baseBinds: BASE_BINDS,
125
- egressBase: EGRESS_BASE,
126
- platform: "darwin",
127
- });
128
- expect(cfg.network).toHaveProperty("allowedDomains");
129
- expect(cfg.network).toHaveProperty("deniedDomains");
130
- expect(cfg.filesystem).toHaveProperty("denyRead");
131
- expect(cfg.filesystem).toHaveProperty("allowRead");
132
- expect(cfg.filesystem).toHaveProperty("allowWrite");
133
- expect(cfg.filesystem).toHaveProperty("denyWrite");
134
- });
135
-
136
- test("an open-network config OMITS allowedDomains but keeps the rest of the shape", () => {
137
- const cfg = buildSandboxConfig({
138
- spec: { name: "arm", channels: ["ch"] }, // default → network open
139
- baseBinds: BASE_BINDS,
140
- egressBase: EGRESS_BASE,
141
- platform: "darwin",
142
- });
143
- // allowedDomains is deliberately ABSENT on open (the runtime's allow-all shape);
144
- // this is NOT a protocol guarantee that allowedDomains is always present.
145
- expect(cfg.network).not.toHaveProperty("allowedDomains");
146
- expect(cfg.network).toHaveProperty("deniedDomains");
147
- expect(cfg.filesystem).toHaveProperty("denyRead");
148
- expect(cfg.filesystem).toHaveProperty("allowWrite");
149
- });
150
- });