@catamorphic/work-server 0.1.0-alpha.43 → 0.1.0-alpha.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/dist/{chunk-kcck20aw.js → chunk-ynxz0am9.js} +80 -9
- package/dist/chunk-ynxz0am9.js.map +11 -0
- package/dist/cluster.d.ts +19 -1
- package/dist/cluster.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +784 -197
- package/dist/index.js.map +18 -16
- package/dist/machines/contract.d.ts +138 -14
- package/dist/machines/contract.d.ts.map +1 -1
- package/dist/machines/sequencer.d.ts +33 -6
- package/dist/machines/sequencer.d.ts.map +1 -1
- package/dist/machines/simulation.d.ts +64 -0
- package/dist/machines/simulation.d.ts.map +1 -1
- package/dist/machines/work-machines.d.ts +20 -2
- package/dist/machines/work-machines.d.ts.map +1 -1
- package/dist/server.d.ts +11 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +208 -17
- package/dist/testing.js.map +4 -4
- package/dist/workers/cache-archive-routes.d.ts +24 -0
- package/dist/workers/cache-archive-routes.d.ts.map +1 -0
- package/dist/workers/harness-sign-ins.d.ts +32 -1
- package/dist/workers/harness-sign-ins.d.ts.map +1 -1
- package/dist/workers/machine-store.d.ts +37 -0
- package/dist/workers/machine-store.d.ts.map +1 -1
- package/dist/workers/member-machine-routes.d.ts +3 -0
- package/dist/workers/member-machine-routes.d.ts.map +1 -1
- package/dist/workers/member-machines.d.ts +52 -5
- package/dist/workers/member-machines.d.ts.map +1 -1
- package/dist/workers/placement.d.ts +10 -0
- package/dist/workers/placement.d.ts.map +1 -1
- package/dist/workers/sign-in-store.d.ts +67 -0
- package/dist/workers/sign-in-store.d.ts.map +1 -0
- package/dist/workers/sign-ins.d.ts +4 -6
- package/dist/workers/sign-ins.d.ts.map +1 -1
- package/dist/workers/worker-protocol.d.ts +9 -1
- package/dist/workers/worker-protocol.d.ts.map +1 -1
- package/dist/workers/worker-registry.d.ts +20 -6
- package/dist/workers/worker-registry.d.ts.map +1 -1
- package/dist/workers/worker-runtime.d.ts.map +1 -1
- package/package.json +18 -19
- package/src/cluster.ts +62 -7
- package/src/index.ts +7 -0
- package/src/machines/contract-kit.ts +1 -1
- package/src/machines/contract.ts +191 -19
- package/src/machines/sequencer.ts +106 -11
- package/src/machines/simulation.ts +370 -20
- package/src/machines/work-machines.ts +33 -3
- package/src/server.ts +106 -9
- package/src/testing.ts +1 -0
- package/src/workers/cache-archive-routes.ts +125 -0
- package/src/workers/harness-sign-ins.ts +207 -22
- package/src/workers/machine-store.ts +337 -56
- package/src/workers/member-machine-routes.ts +58 -3
- package/src/workers/member-machines.ts +168 -14
- package/src/workers/placement.ts +7 -0
- package/src/workers/sign-in-store.ts +241 -0
- package/src/workers/sign-ins.ts +9 -33
- package/src/workers/worker-protocol.ts +9 -1
- package/src/workers/worker-registry.ts +47 -6
- package/src/workers/worker-runtime.ts +5 -1
- package/dist/chunk-kcck20aw.js.map +0 -11
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
3
|
import fs from "node:fs";
|
|
4
|
-
import
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import type { Readable, Writable } from "node:stream";
|
|
5
6
|
import {
|
|
7
|
+
SANDBOX_CAPABILITIES,
|
|
8
|
+
type SandboxEgress,
|
|
9
|
+
type SandboxImage,
|
|
10
|
+
type SandboxProvider,
|
|
6
11
|
SIGN_IN_SPECS,
|
|
7
12
|
type SignInHarness,
|
|
8
13
|
type SignInSpec,
|
|
14
|
+
signInHomePath,
|
|
15
|
+
spawnInSandbox,
|
|
9
16
|
} from "@catamorphic/sandbox";
|
|
10
17
|
import { z } from "zod";
|
|
11
18
|
import {
|
|
@@ -24,7 +31,12 @@ import {
|
|
|
24
31
|
* A member signs in to Claude Code or Codex on one of their machines from
|
|
25
32
|
* the Work app (ADRs 0213, 0342): this machine runs the harness's own
|
|
26
33
|
* unmodified login into a home of its own for them, the app shows the link
|
|
27
|
-
* it printed, and the member finishes in their own browser.
|
|
34
|
+
* it printed, and the member finishes in their own browser. On a machine
|
|
35
|
+
* whose sandboxes run images, the login runs in a sign-in sandbox of the
|
|
36
|
+
* image a turn in the member's Environment runs, with that home mounted
|
|
37
|
+
* where turns mount it, so the CLI that signs in is the one that runs
|
|
38
|
+
* (ADR 0363). The sandbox is nobody's workspace: no checkout, no secrets,
|
|
39
|
+
* nothing but the home, and it goes when the login ends. Codex prints a
|
|
28
40
|
* one-time code the member enters there (`codex login --device-auth`);
|
|
29
41
|
* Claude Code's page gives the member a code, which they paste into the
|
|
30
42
|
* app and this machine writes to the login as its one line of input
|
|
@@ -36,9 +48,23 @@ import {
|
|
|
36
48
|
* nowhere else. No answer, message, log or error here carries it.
|
|
37
49
|
*/
|
|
38
50
|
|
|
51
|
+
/**
|
|
52
|
+
* What a turn in the Environment a sign-in is for runs (ADR 0363): its
|
|
53
|
+
* image (absent: the machine's own) and its network.
|
|
54
|
+
*/
|
|
55
|
+
export interface SignInSandbox {
|
|
56
|
+
image?: SandboxImage;
|
|
57
|
+
egress?: SandboxEgress;
|
|
58
|
+
}
|
|
59
|
+
|
|
39
60
|
/** What a member does with a harness's sign-in on a machine. */
|
|
40
61
|
export type HarnessSignInRequest =
|
|
41
|
-
| {
|
|
62
|
+
| {
|
|
63
|
+
action: "begin";
|
|
64
|
+
harness: SignInHarness;
|
|
65
|
+
member: string;
|
|
66
|
+
sandbox?: SignInSandbox;
|
|
67
|
+
}
|
|
42
68
|
| {
|
|
43
69
|
action: "status";
|
|
44
70
|
harness: SignInHarness;
|
|
@@ -98,11 +124,35 @@ const DEFAULT_CODE_MINUTES = 15;
|
|
|
98
124
|
const EXIT_DRAIN_MS = 500;
|
|
99
125
|
/** Finished attempts are remembered this long for a late status read. */
|
|
100
126
|
const FINISHED_TTL_MS = 60 * 60_000;
|
|
127
|
+
/**
|
|
128
|
+
* Beside a pending home whose login runs in a sandbox: the sandbox's id, so
|
|
129
|
+
* a machine that restarted removes a sandbox its earlier process left.
|
|
130
|
+
*/
|
|
131
|
+
const SANDBOX_NOTE = ".sandbox";
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A login running, on this machine or in its sign-in sandbox: a Node child
|
|
135
|
+
* process, or a process in a sandbox read as one.
|
|
136
|
+
*/
|
|
137
|
+
interface LoginProcess {
|
|
138
|
+
readonly stdin: Writable | null;
|
|
139
|
+
readonly stdout: Readable | null;
|
|
140
|
+
readonly stderr: Readable | null;
|
|
141
|
+
readonly exitCode: number | null;
|
|
142
|
+
kill(signal?: NodeJS.Signals): boolean;
|
|
143
|
+
once(
|
|
144
|
+
event: "exit",
|
|
145
|
+
listener: (code: number | null, signal: NodeJS.Signals | null) => void,
|
|
146
|
+
): unknown;
|
|
147
|
+
once(event: "error", listener: (error: Error) => void): unknown;
|
|
148
|
+
}
|
|
101
149
|
|
|
102
150
|
interface Attempt {
|
|
103
151
|
harness: SignInHarness;
|
|
104
152
|
member: string;
|
|
105
|
-
child:
|
|
153
|
+
child: LoginProcess;
|
|
154
|
+
/** Ends the login's sign-in sandbox, if it has one; safe to call again. */
|
|
155
|
+
release: () => Promise<void>;
|
|
106
156
|
staging: string;
|
|
107
157
|
expiresAt: number;
|
|
108
158
|
state: HarnessSignInState;
|
|
@@ -247,9 +297,133 @@ export class HarnessSignIns {
|
|
|
247
297
|
* placement now rather than at its next scan.
|
|
248
298
|
*/
|
|
249
299
|
onChange?: () => void;
|
|
300
|
+
/**
|
|
301
|
+
* The machine's sandboxes (ADR 0363). Where they run images, every
|
|
302
|
+
* login runs in a sandbox of one: the Environment's, or the
|
|
303
|
+
* machine's own. Otherwise, and without it, a login runs on this
|
|
304
|
+
* machine with the CLI on its `PATH`.
|
|
305
|
+
*/
|
|
306
|
+
provider?: SandboxProvider;
|
|
250
307
|
},
|
|
251
308
|
) {}
|
|
252
309
|
|
|
310
|
+
private recovered?: Promise<void>;
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Remove the sign-in sandboxes, and their pending homes, that an earlier
|
|
314
|
+
* process of this machine left: it forgot their logins when it stopped.
|
|
315
|
+
*/
|
|
316
|
+
recover(): Promise<void> {
|
|
317
|
+
this.recovered ??= (async () => {
|
|
318
|
+
const pending = path.join(this.options.signInRoot, ".pending");
|
|
319
|
+
let names: string[];
|
|
320
|
+
try {
|
|
321
|
+
names = fs.readdirSync(pending);
|
|
322
|
+
} catch {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
for (const name of names) {
|
|
326
|
+
if (!name.endsWith(SANDBOX_NOTE)) continue;
|
|
327
|
+
const note = path.join(pending, name);
|
|
328
|
+
const id = fs.readFileSync(note, "utf8").trim();
|
|
329
|
+
if (id)
|
|
330
|
+
await this.options.provider
|
|
331
|
+
?.destroySandbox(id)
|
|
332
|
+
.catch(() => undefined);
|
|
333
|
+
fs.rmSync(note.slice(0, -SANDBOX_NOTE.length), {
|
|
334
|
+
recursive: true,
|
|
335
|
+
force: true,
|
|
336
|
+
});
|
|
337
|
+
fs.rmSync(note, { force: true });
|
|
338
|
+
}
|
|
339
|
+
})();
|
|
340
|
+
return this.recovered;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** The machine's sandboxes, where a login runs in an image of theirs. */
|
|
344
|
+
private imageSandboxes(): SandboxProvider | undefined {
|
|
345
|
+
const provider = this.options.provider;
|
|
346
|
+
return provider?.processes &&
|
|
347
|
+
provider.capabilities?.includes(SANDBOX_CAPABILITIES.images)
|
|
348
|
+
? provider
|
|
349
|
+
: undefined;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Start a harness's own login into `staging`: in a sign-in sandbox of
|
|
354
|
+
* the Environment's image on a machine whose sandboxes run images, else
|
|
355
|
+
* on this machine.
|
|
356
|
+
*/
|
|
357
|
+
private async startLogin(input: {
|
|
358
|
+
harness: SignInHarness;
|
|
359
|
+
staging: string;
|
|
360
|
+
sandbox?: SignInSandbox;
|
|
361
|
+
}): Promise<{ child: LoginProcess; release: () => Promise<void> }> {
|
|
362
|
+
const spec = SIGN_IN_SPECS[input.harness];
|
|
363
|
+
const provider = this.imageSandboxes();
|
|
364
|
+
if (!provider?.processes) {
|
|
365
|
+
const login = signInCommand({
|
|
366
|
+
harness: input.harness,
|
|
367
|
+
home: input.staging,
|
|
368
|
+
});
|
|
369
|
+
const child = spawn(login.command, login.args, {
|
|
370
|
+
env: signInEnvironment({
|
|
371
|
+
harness: input.harness,
|
|
372
|
+
base: this.options.env ?? process.env,
|
|
373
|
+
login: { ...login.env, NO_COLOR: "1" },
|
|
374
|
+
}),
|
|
375
|
+
// A login that waits for a pasted code reads it from a plain pipe.
|
|
376
|
+
stdio: [spec.expects ? "pipe" : "ignore", "pipe", "pipe"],
|
|
377
|
+
});
|
|
378
|
+
return { child, release: async () => {} };
|
|
379
|
+
}
|
|
380
|
+
const processes = provider.processes;
|
|
381
|
+
let handle: { providerId: string };
|
|
382
|
+
try {
|
|
383
|
+
handle = await provider.createSandbox({
|
|
384
|
+
...(input.sandbox?.image ? { image: input.sandbox.image } : {}),
|
|
385
|
+
...(input.sandbox?.egress ? { egress: input.sandbox.egress } : {}),
|
|
386
|
+
signInLogin: {
|
|
387
|
+
harness: input.harness,
|
|
388
|
+
pending: path.basename(input.staging),
|
|
389
|
+
},
|
|
390
|
+
labels: { purpose: "sign-in" },
|
|
391
|
+
});
|
|
392
|
+
} catch (error) {
|
|
393
|
+
throw new HarnessSignInRefusedError(
|
|
394
|
+
`Could not make a sandbox to sign in to ${spec.name} in: ${
|
|
395
|
+
error instanceof Error ? error.message : String(error)
|
|
396
|
+
}`,
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
const note = `${input.staging}${SANDBOX_NOTE}`;
|
|
400
|
+
fs.writeFileSync(note, handle.providerId, { mode: 0o600 });
|
|
401
|
+
let released: Promise<void> | undefined;
|
|
402
|
+
const release = () => {
|
|
403
|
+
released ??= provider.destroySandbox(handle.providerId).then(
|
|
404
|
+
() => fs.rmSync(note, { force: true }),
|
|
405
|
+
() => undefined,
|
|
406
|
+
);
|
|
407
|
+
return released;
|
|
408
|
+
};
|
|
409
|
+
const home = signInHomePath({
|
|
410
|
+
workspaceRoot: provider.workspaceRoot,
|
|
411
|
+
harness: input.harness,
|
|
412
|
+
});
|
|
413
|
+
const child = spawnInSandbox({
|
|
414
|
+
processes,
|
|
415
|
+
sandboxId: handle.providerId,
|
|
416
|
+
command: spec.command,
|
|
417
|
+
args: [...spec.login, ...spec.defaultArgs],
|
|
418
|
+
cwd: provider.workspaceRoot,
|
|
419
|
+
env: { [spec.homeVariable]: home, NO_COLOR: "1" },
|
|
420
|
+
name: `${spec.name} sign-in`,
|
|
421
|
+
});
|
|
422
|
+
// A login that prints nothing a person reads needs no input.
|
|
423
|
+
if (!spec.expects) child.stdin.end();
|
|
424
|
+
return { child, release };
|
|
425
|
+
}
|
|
426
|
+
|
|
253
427
|
/** One request from the control plane (or this server's own routes). */
|
|
254
428
|
async handle(
|
|
255
429
|
request: HarnessSignInRequest,
|
|
@@ -298,8 +472,10 @@ export class HarnessSignIns {
|
|
|
298
472
|
async begin(input: {
|
|
299
473
|
harness: SignInHarness;
|
|
300
474
|
member: string;
|
|
475
|
+
sandbox?: SignInSandbox;
|
|
301
476
|
}): Promise<HarnessSignInStarted> {
|
|
302
477
|
this.prune();
|
|
478
|
+
await this.recover();
|
|
303
479
|
const { harness, member } = input;
|
|
304
480
|
const spec = SIGN_IN_SPECS[harness];
|
|
305
481
|
try {
|
|
@@ -321,20 +497,18 @@ export class HarnessSignIns {
|
|
|
321
497
|
harness,
|
|
322
498
|
member,
|
|
323
499
|
});
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
env: this.options.env,
|
|
328
|
-
});
|
|
329
|
-
const child = spawn(login.command, login.args, {
|
|
330
|
-
env: signInEnvironment({
|
|
500
|
+
let started: { child: LoginProcess; release: () => Promise<void> };
|
|
501
|
+
try {
|
|
502
|
+
started = await this.startLogin({
|
|
331
503
|
harness,
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
})
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
504
|
+
staging,
|
|
505
|
+
...(input.sandbox ? { sandbox: input.sandbox } : {}),
|
|
506
|
+
});
|
|
507
|
+
} catch (error) {
|
|
508
|
+
fs.rmSync(staging, { recursive: true, force: true });
|
|
509
|
+
throw error;
|
|
510
|
+
}
|
|
511
|
+
const { child, release } = started;
|
|
338
512
|
// A login that ended takes no more input; writing then is no failure.
|
|
339
513
|
child.stdin?.on("error", () => undefined);
|
|
340
514
|
const id = randomUUID();
|
|
@@ -346,7 +520,9 @@ export class HarnessSignIns {
|
|
|
346
520
|
settled = true;
|
|
347
521
|
clearTimeout(timer);
|
|
348
522
|
child.kill("SIGTERM");
|
|
349
|
-
|
|
523
|
+
void release().then(() =>
|
|
524
|
+
fs.rmSync(staging, { recursive: true, force: true }),
|
|
525
|
+
);
|
|
350
526
|
reject(new HarnessSignInRefusedError(message));
|
|
351
527
|
};
|
|
352
528
|
const timer = setTimeout(
|
|
@@ -376,6 +552,7 @@ export class HarnessSignIns {
|
|
|
376
552
|
harness,
|
|
377
553
|
member,
|
|
378
554
|
child,
|
|
555
|
+
release,
|
|
379
556
|
staging,
|
|
380
557
|
expiresAt,
|
|
381
558
|
state: "waiting",
|
|
@@ -409,7 +586,7 @@ export class HarnessSignIns {
|
|
|
409
586
|
`Could not run ${spec.name} on this machine: ${error.message}. Install it there first`,
|
|
410
587
|
),
|
|
411
588
|
);
|
|
412
|
-
const ended = (code: number | null) => {
|
|
589
|
+
const ended = async (code: number | null) => {
|
|
413
590
|
if (!settled) {
|
|
414
591
|
fail(
|
|
415
592
|
lastWords(output) ??
|
|
@@ -419,6 +596,9 @@ export class HarnessSignIns {
|
|
|
419
596
|
}
|
|
420
597
|
const attempt = this.attempts.get(id);
|
|
421
598
|
if (attempt?.state !== "waiting") return;
|
|
599
|
+
// The sandbox goes before its home takes the member's place.
|
|
600
|
+
await release();
|
|
601
|
+
if (this.attempts.get(id)?.state !== "waiting") return;
|
|
422
602
|
if (code === 0 && signedIn({ harness, home: staging })) {
|
|
423
603
|
try {
|
|
424
604
|
// Someone else may have signed in here meanwhile.
|
|
@@ -459,6 +639,8 @@ export class HarnessSignIns {
|
|
|
459
639
|
Promise.all([drained(child.stdout), drained(child.stderr)]),
|
|
460
640
|
new Promise((resolve) => setTimeout(resolve, EXIT_DRAIN_MS)),
|
|
461
641
|
]).then(() => ended(code));
|
|
642
|
+
// The process ended: its sandbox goes, whatever happens next.
|
|
643
|
+
void release();
|
|
462
644
|
});
|
|
463
645
|
});
|
|
464
646
|
}
|
|
@@ -563,8 +745,11 @@ export class HarnessSignIns {
|
|
|
563
745
|
attempt.output = "";
|
|
564
746
|
clearTimeout(attempt.deadline);
|
|
565
747
|
if (attempt.child.exitCode === null) attempt.child.kill("SIGTERM");
|
|
566
|
-
if (state
|
|
567
|
-
|
|
748
|
+
if (state === "signed-in") return;
|
|
749
|
+
const { staging } = attempt;
|
|
750
|
+
void attempt
|
|
751
|
+
.release()
|
|
752
|
+
.then(() => fs.rmSync(staging, { recursive: true, force: true }));
|
|
568
753
|
}
|
|
569
754
|
|
|
570
755
|
private prune(): void {
|