@fungi.computer/shed 0.9.1 → 0.10.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.
@@ -0,0 +1,586 @@
1
+ import assert from "node:assert/strict";
2
+ import { it, type TestContext } from "node:test";
3
+ import { DatabaseSync } from "node:sqlite";
4
+ import * as z from "zod";
5
+ import type { HostActor } from "@fungi.computer/puffball/app-method-runtime";
6
+ import {
7
+ mintComputerProcessId,
8
+ type ComputerProcess,
9
+ } from "@fungi.computer/computer-contract";
10
+ import { appBackend, type ShedMethodContext } from "../methods.js";
11
+ import { createShedRuns } from "../runs.js";
12
+
13
+ type Caller = Parameters<typeof appBackend.call>[3];
14
+ const keyOne: HostActor = { kind: "team-key", keyId: "key_one" };
15
+ const keyTwo: HostActor = { kind: "team-key", keyId: "key_two" };
16
+ const member: HostActor = { kind: "member", memberId: "member_one" };
17
+ const agent: HostActor = { kind: "agent", agentId: "agent_one" };
18
+ const scope = { kind: "standalone" as const, tenant: "default" };
19
+ const as = (actor: HostActor, idempotencyKey?: string): Caller =>
20
+ idempotencyKey === undefined
21
+ ? { scope, actor, computerId: "computer_one" }
22
+ : { scope, actor, computerId: "computer_one", idempotencyKey };
23
+ const started = z.looseObject({
24
+ process: z.looseObject({ id: z.string() }),
25
+ logPath: z.string(),
26
+ parentId: z.string().optional(),
27
+ });
28
+ const notFound = { error: "not_found" };
29
+
30
+ /** One Computer that records what Shed asks of it, with a log that always reads "ok". */
31
+ function fixture(t: TestContext, seed?: (database: DatabaseSync) => void) {
32
+ const database = new DatabaseSync(":memory:");
33
+ t.after(() => {
34
+ database.close();
35
+ });
36
+ seed?.(database);
37
+ const sql = {
38
+ exec(query: string, ...values: (string | number | null)[]) {
39
+ const rows = database.prepare(query).all(...values);
40
+ return { toArray: () => rows };
41
+ },
42
+ };
43
+ const processes = new Map<string, ComputerProcess>();
44
+ const starts: {
45
+ command: string;
46
+ cwd?: string;
47
+ port?: number;
48
+ requestId?: string;
49
+ }[] = [];
50
+ const kills: string[] = [];
51
+ const reads: string[] = [];
52
+ const behavior = { holdsOnStop: false };
53
+ const unexpected = () => Promise.reject(new Error("Unexpected binding call"));
54
+ const computer = {
55
+ ports: unexpected,
56
+ exec: unexpected,
57
+ preview: unexpected,
58
+ processStart: (input: {
59
+ command: string;
60
+ cwd?: string;
61
+ port?: number;
62
+ requestId?: string;
63
+ }) => {
64
+ starts.push(input);
65
+ const process: ComputerProcess = {
66
+ id: mintComputerProcessId(),
67
+ port: input.port ?? null,
68
+ command: input.command,
69
+ cwd: input.cwd ?? null,
70
+ state: "running",
71
+ exitCode: null,
72
+ sync: "synced",
73
+ startedAt: starts.length,
74
+ endedAt: null,
75
+ endedReason: null,
76
+ };
77
+ processes.set(process.id, process);
78
+ return Promise.resolve(process);
79
+ },
80
+ processes: () =>
81
+ Promise.resolve({ processes: [...processes.values()].reverse() }),
82
+ processKill: (input: { id: string }) => {
83
+ kills.push(input.id);
84
+ const process = processes.get(input.id);
85
+ if (process === undefined) return unexpected();
86
+ if (behavior.holdsOnStop)
87
+ return Promise.resolve({ process, signalled: true });
88
+ const ended: ComputerProcess = {
89
+ ...process,
90
+ state: "exited",
91
+ exitCode: 143,
92
+ endedAt: 99,
93
+ };
94
+ processes.set(input.id, ended);
95
+ return Promise.resolve({ process: ended, signalled: true });
96
+ },
97
+ read: (input: { path: string; byteOffset: number }) => {
98
+ reads.push(input.path);
99
+ return Promise.resolve({
100
+ path: input.path,
101
+ byteOffset: input.byteOffset,
102
+ content: "b2s=",
103
+ encoding: "base64",
104
+ truncated: true,
105
+ });
106
+ },
107
+ };
108
+ const open = (): ShedMethodContext => ({
109
+ runs: createShedRuns(sql),
110
+ env: { COMPUTER: computer },
111
+ });
112
+ return {
113
+ database,
114
+ open,
115
+ context: open(),
116
+ processes,
117
+ starts,
118
+ kills,
119
+ reads,
120
+ behavior,
121
+ };
122
+ }
123
+
124
+ void it("starts for a key with only a requestId, replays it, and refuses changed input", async (t) => {
125
+ const f = fixture(t);
126
+ const first = started.parse(
127
+ await appBackend.call(
128
+ "start",
129
+ { command: "pnpm dev", port: 3000, requestId: "req_one" },
130
+ f.context,
131
+ as(keyOne),
132
+ ),
133
+ );
134
+ const again = started.parse(
135
+ await appBackend.call(
136
+ "start",
137
+ { command: "pnpm dev", port: 3000, requestId: "req_one" },
138
+ f.open(),
139
+ as(keyOne),
140
+ ),
141
+ );
142
+ assert.deepEqual(again, first);
143
+ assert.equal(f.starts.length, 1);
144
+ assert.equal(f.starts[0]?.requestId, "req_one");
145
+ await assert.rejects(
146
+ appBackend.call(
147
+ "start",
148
+ { command: "pnpm build", port: 3000, requestId: "req_one" },
149
+ f.context,
150
+ as(keyOne),
151
+ ),
152
+ /Run identity conflicts/,
153
+ );
154
+ await assert.rejects(
155
+ appBackend.call("start", { command: "pnpm dev" }, f.context, as(keyOne)),
156
+ /request id/,
157
+ );
158
+ // The same requestId from another key is its own start.
159
+ await appBackend.call(
160
+ "start",
161
+ { command: "pnpm dev", port: 3000, requestId: "req_one" },
162
+ f.context,
163
+ as(keyTwo),
164
+ );
165
+ assert.equal(f.starts.length, 2);
166
+ });
167
+
168
+ void it("forwards no requestId when the host attested the key, whatever the caller also sent", async (t) => {
169
+ const f = fixture(t);
170
+ await appBackend.call(
171
+ "run",
172
+ { command: "sleep 9", requestId: "caller_chosen" },
173
+ f.context,
174
+ as(member, "host_key"),
175
+ );
176
+ assert.equal(f.starts.length, 1);
177
+ assert.equal(f.starts[0]?.requestId, undefined);
178
+ });
179
+
180
+ void it("runs for a key under a requestId it names, replays it, and returns the exit", async (t) => {
181
+ const f = fixture(t);
182
+ const call = (context: ShedMethodContext, command = "make test") =>
183
+ appBackend.call(
184
+ "run",
185
+ { command, requestId: "run_one" },
186
+ context,
187
+ as(keyOne),
188
+ );
189
+ assert.deepEqual(await call(f.context), {
190
+ kind: "pending",
191
+ retryAfterMs: 5000,
192
+ });
193
+ assert.deepEqual(await call(f.open()), {
194
+ kind: "pending",
195
+ retryAfterMs: 5000,
196
+ });
197
+ assert.equal(f.starts.length, 1);
198
+ assert.equal(f.starts[0]?.requestId, "run_one");
199
+ await assert.rejects(call(f.context, "make build"), /Run identity conflicts/);
200
+ const [id] = [...f.processes.keys()];
201
+ const running = id === undefined ? undefined : f.processes.get(id);
202
+ assert.ok(id !== undefined && running);
203
+ f.processes.set(id, { ...running, state: "exited", exitCode: 0, endedAt: 3 });
204
+ const done = z
205
+ .looseObject({ exitCode: z.number(), logPath: z.string() })
206
+ .parse(await call(f.open()));
207
+ assert.equal(done.exitCode, 0);
208
+ assert.equal(f.starts.length, 1);
209
+ // Another key's run under the same id is its own.
210
+ await appBackend.call(
211
+ "run",
212
+ { command: "make test", requestId: "run_one" },
213
+ f.context,
214
+ as(keyTwo),
215
+ );
216
+ assert.equal(f.starts.length, 2);
217
+ });
218
+
219
+ void it("forwards no requestId on start when the host attested the key, whatever the caller also sent", async (t) => {
220
+ const f = fixture(t);
221
+ await appBackend.call(
222
+ "start",
223
+ { command: "sleep 9", requestId: "caller_chosen" },
224
+ f.context,
225
+ as(member, "host_key"),
226
+ );
227
+ assert.equal(f.starts.length, 1);
228
+ assert.equal(f.starts[0]?.requestId, undefined);
229
+ await appBackend.call(
230
+ "start",
231
+ { command: "sleep 9", requestId: "caller_chosen" },
232
+ f.context,
233
+ as(member, "host_key"),
234
+ );
235
+ assert.equal(f.starts.length, 1);
236
+ });
237
+
238
+ void it("reads logs by process id for the actor that started it, and restarts and stops it", async (t) => {
239
+ const f = fixture(t);
240
+ const first = started.parse(
241
+ await appBackend.call(
242
+ "start",
243
+ {
244
+ command: "pnpm dev",
245
+ cwd: "/workspace/app",
246
+ port: 3000,
247
+ requestId: "r1",
248
+ },
249
+ f.context,
250
+ as(keyOne),
251
+ ),
252
+ );
253
+ const log = await appBackend.call(
254
+ "logs",
255
+ { id: first.process.id, offset: 2 },
256
+ f.context,
257
+ as(keyOne),
258
+ );
259
+ assert.deepEqual(log, {
260
+ id: first.process.id,
261
+ path: first.logPath,
262
+ byteOffset: 2,
263
+ content: "b2s=",
264
+ encoding: "base64",
265
+ truncated: true,
266
+ });
267
+ assert.deepEqual(f.reads, [first.logPath]);
268
+
269
+ const restarted = started.parse(
270
+ await appBackend.call(
271
+ "restart",
272
+ { id: first.process.id, requestId: "r2" },
273
+ f.context,
274
+ as(keyOne),
275
+ ),
276
+ );
277
+ assert.deepEqual(f.kills, [first.process.id]);
278
+ assert.equal(restarted.parentId, first.process.id);
279
+ assert.notEqual(restarted.process.id, first.process.id);
280
+ assert.notEqual(restarted.logPath, first.logPath);
281
+ const second = f.starts.at(1);
282
+ assert.ok(second);
283
+ assert.equal(second.cwd, "/workspace/app");
284
+ assert.equal(second.port, 3000);
285
+ assert.equal(second.requestId, "r2");
286
+ // The wrapper runs the recorded command, not a new one.
287
+ assert.match(second.command, /pnpm dev/);
288
+ assert.deepEqual(
289
+ started.parse(
290
+ await appBackend.call(
291
+ "restart",
292
+ { id: first.process.id, requestId: "r2" },
293
+ f.open(),
294
+ as(keyOne),
295
+ ),
296
+ ),
297
+ restarted,
298
+ );
299
+ assert.equal(f.starts.length, 2);
300
+ assert.equal(f.kills.length, 1);
301
+ await assert.rejects(
302
+ appBackend.call(
303
+ "restart",
304
+ { id: restarted.process.id, requestId: "r2" },
305
+ f.context,
306
+ as(keyOne),
307
+ ),
308
+ /Run identity conflicts/,
309
+ );
310
+
311
+ const newLog = z
312
+ .looseObject({ path: z.string() })
313
+ .parse(
314
+ await appBackend.call(
315
+ "logs",
316
+ { id: restarted.process.id },
317
+ f.context,
318
+ as(keyOne),
319
+ ),
320
+ );
321
+ assert.equal(newLog.path, restarted.logPath);
322
+ await appBackend.call(
323
+ "stop",
324
+ { id: restarted.process.id },
325
+ f.context,
326
+ as(keyOne),
327
+ );
328
+ assert.deepEqual(f.kills, [first.process.id, restarted.process.id]);
329
+ });
330
+
331
+ void it("waits for the replaced process to exit before starting its replacement", async (t) => {
332
+ const f = fixture(t);
333
+ const first = started.parse(
334
+ await appBackend.call(
335
+ "start",
336
+ { command: "pnpm dev", port: 3000, requestId: "r1" },
337
+ f.context,
338
+ as(keyOne),
339
+ ),
340
+ );
341
+ f.behavior.holdsOnStop = true;
342
+ assert.deepEqual(
343
+ await appBackend.call(
344
+ "restart",
345
+ { id: first.process.id, requestId: "r2" },
346
+ f.context,
347
+ as(keyOne),
348
+ ),
349
+ { kind: "pending", retryAfterMs: 1000 },
350
+ );
351
+ assert.equal(f.starts.length, 1);
352
+ const running = f.processes.get(first.process.id);
353
+ assert.ok(running);
354
+ f.processes.set(first.process.id, {
355
+ ...running,
356
+ state: "exited",
357
+ exitCode: 143,
358
+ endedAt: 5,
359
+ });
360
+ const restarted = started.parse(
361
+ await appBackend.call(
362
+ "restart",
363
+ { id: first.process.id, requestId: "r2" },
364
+ f.context,
365
+ as(keyOne),
366
+ ),
367
+ );
368
+ assert.equal(restarted.parentId, first.process.id);
369
+ assert.equal(f.starts.length, 2);
370
+ });
371
+
372
+ void it("keeps a key away from a process the UI started, and from another key's", async (t) => {
373
+ const f = fixture(t);
374
+ const ui = started.parse(
375
+ await appBackend.call(
376
+ "start",
377
+ { command: "pnpm dev" },
378
+ f.context,
379
+ as(member, "ui_start"),
380
+ ),
381
+ );
382
+ const mine = started.parse(
383
+ await appBackend.call(
384
+ "start",
385
+ { command: "pnpm test", requestId: "mine" },
386
+ f.context,
387
+ as(keyOne),
388
+ ),
389
+ );
390
+ assert.deepEqual(
391
+ await appBackend.call("stop", { id: ui.process.id }, f.context, as(keyOne)),
392
+ notFound,
393
+ );
394
+ assert.deepEqual(
395
+ await appBackend.call(
396
+ "restart",
397
+ { id: ui.process.id, requestId: "r" },
398
+ f.context,
399
+ as(keyOne),
400
+ ),
401
+ notFound,
402
+ );
403
+ assert.deepEqual(
404
+ await appBackend.call("logs", { id: ui.process.id }, f.context, as(keyOne)),
405
+ notFound,
406
+ );
407
+ assert.deepEqual(
408
+ await appBackend.call(
409
+ "stop",
410
+ { id: mine.process.id },
411
+ f.context,
412
+ as(keyTwo),
413
+ ),
414
+ notFound,
415
+ );
416
+ assert.deepEqual(
417
+ await appBackend.call(
418
+ "logs",
419
+ { id: mine.process.id },
420
+ f.context,
421
+ as(keyTwo),
422
+ ),
423
+ notFound,
424
+ );
425
+ assert.deepEqual(f.kills, []);
426
+ assert.deepEqual(f.reads, []);
427
+
428
+ const page = z.object({
429
+ processes: z.array(
430
+ z.looseObject({ id: z.string(), controllable: z.boolean() }),
431
+ ),
432
+ });
433
+ const marks = async (actor: HostActor) =>
434
+ page
435
+ .parse(await appBackend.call("listProcesses", {}, f.context, as(actor)))
436
+ .processes.map((row) => [row.id, row.controllable]);
437
+ assert.deepEqual(await marks(keyOne), [
438
+ [mine.process.id, true],
439
+ [ui.process.id, false],
440
+ ]);
441
+ assert.deepEqual(await marks(member), [
442
+ [mine.process.id, false],
443
+ [ui.process.id, true],
444
+ ]);
445
+ assert.deepEqual(await marks(agent), [
446
+ [mine.process.id, false],
447
+ [ui.process.id, false],
448
+ ]);
449
+ });
450
+
451
+ void it("refuses an Agent to restart and asks a key for a request id", async (t) => {
452
+ const f = fixture(t);
453
+ const own = started.parse(
454
+ await appBackend.call(
455
+ "start",
456
+ { command: "pnpm dev", requestId: "r1" },
457
+ f.context,
458
+ as(keyOne),
459
+ ),
460
+ );
461
+ assert.deepEqual(
462
+ await appBackend.call(
463
+ "restart",
464
+ { id: own.process.id, requestId: "r" },
465
+ f.context,
466
+ as(agent),
467
+ ),
468
+ { error: "not_granted" },
469
+ );
470
+ await assert.rejects(
471
+ appBackend.call("restart", { id: own.process.id }, f.context, as(keyOne)),
472
+ /request id/,
473
+ );
474
+ assert.deepEqual(f.kills, []);
475
+ });
476
+
477
+ void it("names a log by exactly one of run or id", async (t) => {
478
+ const f = fixture(t);
479
+ const own = started.parse(
480
+ await appBackend.call(
481
+ "start",
482
+ { command: "pnpm dev", requestId: "r1" },
483
+ f.context,
484
+ as(keyOne),
485
+ ),
486
+ );
487
+ const unnamed: Record<string, string>[] = [
488
+ {},
489
+ { run: "r1", id: own.process.id },
490
+ ];
491
+ for (const input of unnamed)
492
+ await assert.rejects(
493
+ appBackend.call("logs", input, f.context, as(keyOne)),
494
+ /run or by id/,
495
+ );
496
+ // The key's own call key still names its start.
497
+ const byRun = z
498
+ .looseObject({ run: z.string() })
499
+ .parse(await appBackend.call("logs", { run: "r1" }, f.context, as(keyOne)));
500
+ assert.equal(byRun.run, "r1");
501
+ });
502
+
503
+ const oldTable =
504
+ "CREATE TABLE shed_native_process_claims (requestKey TEXT PRIMARY KEY, computerId TEXT NOT NULL, command TEXT NOT NULL, cwd TEXT, processId TEXT, phase TEXT NOT NULL, startedAt INTEGER, exitCode INTEGER, logPath TEXT NOT NULL, gitAccess TEXT)";
505
+
506
+ void it("adds the new columns to an old ledger in place and refuses to restart an old row", async (t) => {
507
+ const oldProcessId = mintComputerProcessId();
508
+ let seededColumns: string[] = [];
509
+ const f = fixture(t, (database) => {
510
+ database.exec(oldTable);
511
+ seededColumns = database
512
+ .prepare("PRAGMA table_info(shed_native_process_claims)")
513
+ .all()
514
+ .map((row) => String(row.name));
515
+ database
516
+ .prepare(
517
+ "INSERT INTO shed_native_process_claims VALUES (?,?,?,?,?,?,?,?,?,?)",
518
+ )
519
+ .run(
520
+ JSON.stringify([member, "start", "old_call"]),
521
+ "computer_one",
522
+ "node old.mjs",
523
+ null,
524
+ oldProcessId,
525
+ "running",
526
+ 1,
527
+ null,
528
+ "/workspace/.fungi/runs/call-old/out.log",
529
+ null,
530
+ );
531
+ });
532
+ const names = () =>
533
+ f.database
534
+ .prepare("PRAGMA table_info(shed_native_process_claims)")
535
+ .all()
536
+ .map((row) => String(row.name));
537
+ assert.equal(seededColumns.includes("port"), false);
538
+ const runs = f.context.runs;
539
+ assert.ok(runs);
540
+ assert.deepEqual(names().slice(-3), ["port", "parentId", "stoppedAt"]);
541
+ // The old row loads untouched: it is still owned and its log still resolves.
542
+ assert.equal(runs.owns(oldProcessId, "computer_one", member), true);
543
+ assert.deepEqual(runs.logPathById(oldProcessId, "computer_one", member), {
544
+ path: "/workspace/.fungi/runs/call-old/out.log",
545
+ });
546
+ assert.deepEqual(
547
+ f.database
548
+ .prepare("SELECT port, parentId, command FROM shed_native_process_claims")
549
+ .all()
550
+ .map((row) => ({ ...row })),
551
+ [{ port: null, parentId: null, command: "node old.mjs" }],
552
+ );
553
+ // Reopening is a no-op.
554
+ f.open();
555
+ assert.equal(names().filter((name) => name === "port").length, 1);
556
+ // It cannot be restarted: no port was recorded. Nothing is signalled or started.
557
+ assert.deepEqual(
558
+ await appBackend.call(
559
+ "restart",
560
+ { id: oldProcessId, requestId: "r" },
561
+ f.context,
562
+ as(member),
563
+ ),
564
+ { error: "invalid" },
565
+ );
566
+ assert.deepEqual(f.kills, []);
567
+ assert.deepEqual(f.starts, []);
568
+ // A new start on the migrated ledger records its port, so it restarts.
569
+ const fresh = started.parse(
570
+ await appBackend.call(
571
+ "start",
572
+ { command: "sleep 1" },
573
+ f.open(),
574
+ as(member, "new_call"),
575
+ ),
576
+ );
577
+ const again = started.parse(
578
+ await appBackend.call(
579
+ "restart",
580
+ { id: fresh.process.id, requestId: "r" },
581
+ f.open(),
582
+ as(member),
583
+ ),
584
+ );
585
+ assert.equal(again.parentId, fresh.process.id);
586
+ });
@@ -3,12 +3,12 @@ import { createShedRuns } from "../runs.js";
3
3
  import {
4
4
  maximumStreamBytes,
5
5
  mintComputerProcessId,
6
- processListOutputSchema,
7
6
  } from "@fungi.computer/computer-contract";
8
7
  import { Shed } from "../index.js";
9
8
  import * as z from "zod";
10
9
  import { expect, it, onTestFinished } from "vitest";
11
10
  import { appBackend, type ShedMethodContext } from "../methods.js";
11
+ import { processPageSchema } from "../process-page.js";
12
12
 
13
13
  function hostComputer(
14
14
  overrides: Partial<ShedMethodContext["env"]["COMPUTER"]>,
@@ -425,7 +425,22 @@ it("retains a process whose native cwd contains parent segments", async () => {
425
425
  computerId: "computer-a",
426
426
  },
427
427
  ),
428
- ).toEqual(reply);
428
+ ).toEqual({
429
+ processes: reply.processes.map((item) => ({
430
+ ...item,
431
+ controllable: false,
432
+ run: {
433
+ source: "shed.process",
434
+ id: item.id,
435
+ state: "running",
436
+ requestedAt: 1,
437
+ startedAt: 1,
438
+ log: null,
439
+ outputs: [],
440
+ cost: null,
441
+ },
442
+ })),
443
+ });
429
444
  });
430
445
 
431
446
  it("pages retained processes on the admitted Computer without starting work and refuses another Computer's cursor", async () => {
@@ -465,7 +480,7 @@ it("pages retained processes on the admitted Computer without starting work and
465
480
  actor: { kind: "member", memberId: "member-a" },
466
481
  computerId: "computer-a",
467
482
  };
468
- const page = processListOutputSchema.extend({ next: z.json().optional() });
483
+ const page = processPageSchema.extend({ next: z.json().optional() });
469
484
  const first = page.parse(
470
485
  await appBackend.call("listProcesses", { limit: 20 }, context, caller),
471
486
  );
@@ -480,7 +495,12 @@ it("pages retained processes on the admitted Computer without starting work and
480
495
  caller,
481
496
  ),
482
497
  );
483
- expect(next.processes).toEqual([processes[20]]);
498
+ expect(next.processes).toHaveLength(1);
499
+ const [row] = next.processes;
500
+ expect(row).toMatchObject({ ...processes[20], controllable: false });
501
+ expect(row?.run.id).toBe(processes[20]?.id);
502
+ expect(row?.run.log).toBeNull();
503
+ expect(row?.run.cost).toBeNull();
484
504
  expect(next.next).toBeUndefined();
485
505
  expect(reads).toBe(2);
486
506
  expect(