@catamorphic/work-server 0.1.0-alpha.38 → 0.1.0-alpha.41
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/NOTICE.txt +7 -0
- package/README.md +9 -9
- package/dist/admission/routes.d.ts +1 -1
- package/dist/admission/routes.d.ts.map +1 -1
- package/dist/agent-capabilities.d.ts.map +1 -1
- package/dist/agents.d.ts +5 -6
- package/dist/agents.d.ts.map +1 -1
- package/dist/auth/fastify-auth.d.ts.map +1 -1
- package/dist/auth/work-auth-types.d.ts +2 -0
- package/dist/auth/work-auth-types.d.ts.map +1 -1
- package/dist/auth/work-auth.d.ts.map +1 -1
- package/dist/chunk-kcck20aw.js +676 -0
- package/dist/chunk-kcck20aw.js.map +11 -0
- package/dist/cluster.d.ts +8 -7
- package/dist/cluster.d.ts.map +1 -1
- package/dist/config.d.ts +5 -15
- package/dist/config.d.ts.map +1 -1
- package/dist/execution-config.d.ts +22 -0
- package/dist/execution-config.d.ts.map +1 -1
- package/dist/fake-agent.d.ts +1 -0
- package/dist/fake-agent.d.ts.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2730 -2851
- package/dist/index.js.map +28 -28
- package/dist/machines/contract-kit.d.ts +91 -0
- package/dist/machines/contract-kit.d.ts.map +1 -0
- package/dist/machines/contract.d.ts +383 -0
- package/dist/machines/contract.d.ts.map +1 -0
- package/dist/machines/sequencer.d.ts +267 -0
- package/dist/machines/sequencer.d.ts.map +1 -0
- package/dist/machines/simulation.d.ts +124 -0
- package/dist/machines/simulation.d.ts.map +1 -0
- package/dist/machines/work-machines.d.ts +61 -0
- package/dist/machines/work-machines.d.ts.map +1 -0
- package/dist/server.d.ts +15 -11
- package/dist/server.d.ts.map +1 -1
- package/dist/setup/machines.d.ts +9 -17
- package/dist/setup/machines.d.ts.map +1 -1
- package/dist/shares/share-routes.d.ts +1 -1
- package/dist/shares/share-routes.d.ts.map +1 -1
- package/dist/testing.d.ts +8 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +1013 -0
- package/dist/testing.js.map +11 -0
- package/dist/workers/disk-care.d.ts +41 -0
- package/dist/workers/disk-care.d.ts.map +1 -0
- package/dist/workers/harness-sign-ins.d.ts +146 -0
- package/dist/workers/harness-sign-ins.d.ts.map +1 -0
- package/dist/workers/machine-store.d.ts +196 -0
- package/dist/workers/machine-store.d.ts.map +1 -0
- package/dist/workers/member-machine-routes.d.ts +5 -3
- package/dist/workers/member-machine-routes.d.ts.map +1 -1
- package/dist/workers/member-machines.d.ts +47 -28
- package/dist/workers/member-machines.d.ts.map +1 -1
- package/dist/workers/placement.d.ts +23 -40
- package/dist/workers/placement.d.ts.map +1 -1
- package/dist/workers/sign-ins.d.ts +44 -17
- package/dist/workers/sign-ins.d.ts.map +1 -1
- package/dist/workers/worker-protocol.d.ts +5 -2
- package/dist/workers/worker-protocol.d.ts.map +1 -1
- package/dist/workers/worker-registry.d.ts +174 -223
- package/dist/workers/worker-registry.d.ts.map +1 -1
- package/dist/workers/worker-routes.d.ts.map +1 -1
- package/dist/workers/worker-runtime.d.ts.map +1 -1
- package/package.json +44 -34
- package/src/admission/routes.ts +5 -2
- package/src/agent-capabilities.ts +22 -8
- package/src/agents.ts +52 -23
- package/src/auth/fastify-auth.ts +149 -17
- package/src/auth/work-auth-types.ts +2 -0
- package/src/auth/work-auth.ts +27 -0
- package/src/cluster.ts +115 -42
- package/src/config.ts +15 -41
- package/src/execution-config.ts +40 -0
- package/src/fake-agent.ts +21 -4
- package/src/index.ts +26 -12
- package/src/machines/contract-kit.ts +648 -0
- package/src/machines/contract.ts +522 -0
- package/src/machines/sequencer.ts +1010 -0
- package/src/machines/simulation.ts +833 -0
- package/src/machines/work-machines.ts +231 -0
- package/src/server.ts +235 -150
- package/src/setup/machines.ts +128 -121
- package/src/shares/share-routes.ts +17 -1
- package/src/testing.ts +22 -0
- package/src/workers/disk-care.ts +121 -0
- package/src/workers/fake-claude-cli.ts +95 -0
- package/src/workers/fake-codex-cli.ts +11 -4
- package/src/workers/harness-sign-ins.ts +580 -0
- package/src/workers/machine-store.ts +1065 -0
- package/src/workers/member-machine-routes.ts +92 -20
- package/src/workers/member-machines.ts +163 -99
- package/src/workers/placement.ts +59 -70
- package/src/workers/sign-ins.ts +109 -52
- package/src/workers/worker-protocol.ts +5 -2
- package/src/workers/worker-registry.ts +894 -754
- package/src/workers/worker-routes.ts +4 -1
- package/src/workers/worker-runtime.ts +55 -23
- package/dist/test-support.d.ts +0 -133
- package/dist/test-support.d.ts.map +0 -1
- package/dist/workers/codex-sign-ins.d.ts +0 -107
- package/dist/workers/codex-sign-ins.d.ts.map +0 -1
- package/dist/workers/install-script.d.ts +0 -71
- package/dist/workers/install-script.d.ts.map +0 -1
- package/dist/workers/machine-classes.d.ts +0 -115
- package/dist/workers/machine-classes.d.ts.map +0 -1
- package/dist/workers/machine-rules.d.ts +0 -402
- package/dist/workers/machine-rules.d.ts.map +0 -1
- package/src/test-support.ts +0 -404
- package/src/workers/codex-sign-ins.ts +0 -360
- package/src/workers/install-script.ts +0 -394
- package/src/workers/machine-classes.ts +0 -152
- package/src/workers/machine-rules.ts +0 -1954
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { ExecutorNotConnectedError, type Identity } from "@catamorphic/core";
|
|
2
|
+
import { type SignInHarness, signInHarness } from "@catamorphic/sandbox";
|
|
2
3
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
3
4
|
import { z } from "zod";
|
|
4
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
HarnessSignInRefusedError,
|
|
7
|
+
SIGN_IN_INPUT_MAX,
|
|
8
|
+
} from "./harness-sign-ins.js";
|
|
5
9
|
import { type MemberMachines, NotYourMachineError } from "./member-machines.js";
|
|
6
10
|
|
|
7
11
|
/**
|
|
8
|
-
* A member's own machines and their Codex sign-ins, for
|
|
9
|
-
* 0213). Every route acts as the caller, on a
|
|
10
|
-
* the sign-in itself happens in the member's
|
|
12
|
+
* A member's own machines and their Claude Code and Codex sign-ins, for
|
|
13
|
+
* the Work app (ADRs 0213, 0342). Every route acts as the caller, on a
|
|
14
|
+
* machine of the caller's own; the sign-in itself happens in the member's
|
|
15
|
+
* browser and on the machine. The code a member pastes for a login that
|
|
16
|
+
* waits for one passes through to the machine: no route logs or repeats it.
|
|
11
17
|
*/
|
|
12
18
|
export function registerMemberMachineRoutes(
|
|
13
19
|
app: FastifyInstance,
|
|
@@ -16,8 +22,14 @@ export function registerMemberMachineRoutes(
|
|
|
16
22
|
machines: MemberMachines;
|
|
17
23
|
},
|
|
18
24
|
): void {
|
|
19
|
-
const
|
|
20
|
-
|
|
25
|
+
const harnessParams = z.object({
|
|
26
|
+
id: z.string().min(1).max(200),
|
|
27
|
+
harness: z.string().min(1).max(40),
|
|
28
|
+
});
|
|
29
|
+
const attemptParams = harnessParams.extend({ attempt: z.string().uuid() });
|
|
30
|
+
const inputBody = z.strictObject({
|
|
31
|
+
text: z.string().min(1).max(SIGN_IN_INPUT_MAX),
|
|
32
|
+
});
|
|
21
33
|
|
|
22
34
|
/** The caller, or a 401 already sent. */
|
|
23
35
|
const member = async (
|
|
@@ -32,11 +44,24 @@ export function registerMemberMachineRoutes(
|
|
|
32
44
|
return identity;
|
|
33
45
|
};
|
|
34
46
|
|
|
47
|
+
/** The harness a route names, or a 404 already sent. */
|
|
48
|
+
const harnessOf = async (
|
|
49
|
+
reply: FastifyReply,
|
|
50
|
+
name: string,
|
|
51
|
+
): Promise<SignInHarness | undefined> => {
|
|
52
|
+
const harness = signInHarness(name);
|
|
53
|
+
if (!harness)
|
|
54
|
+
await reply
|
|
55
|
+
.status(404)
|
|
56
|
+
.send({ error: `'${name}' is not a harness members sign in to` });
|
|
57
|
+
return harness;
|
|
58
|
+
};
|
|
59
|
+
|
|
35
60
|
/** What stopped a request, as the app shows it. */
|
|
36
61
|
const refusal = (reply: FastifyReply, error: unknown) => {
|
|
37
62
|
if (error instanceof NotYourMachineError)
|
|
38
63
|
return reply.status(403).send({ error: error.message });
|
|
39
|
-
if (error instanceof
|
|
64
|
+
if (error instanceof HarnessSignInRefusedError)
|
|
40
65
|
return reply
|
|
41
66
|
.status(409)
|
|
42
67
|
.send({ error: error.message, code: "sign_in_refused" });
|
|
@@ -59,15 +84,18 @@ export function registerMemberMachineRoutes(
|
|
|
59
84
|
});
|
|
60
85
|
|
|
61
86
|
app.post(
|
|
62
|
-
"/api/work/me/machines/:id/
|
|
87
|
+
"/api/work/me/machines/:id/:harness/sign-in",
|
|
63
88
|
async (request, reply) => {
|
|
64
89
|
const identity = await member(request, reply);
|
|
65
90
|
if (!identity) return reply;
|
|
66
|
-
const
|
|
91
|
+
const params = harnessParams.parse(request.params);
|
|
92
|
+
const harness = await harnessOf(reply, params.harness);
|
|
93
|
+
if (!harness) return reply;
|
|
67
94
|
try {
|
|
68
95
|
const started = await options.machines.begin({
|
|
69
96
|
identity,
|
|
70
|
-
machineId: id,
|
|
97
|
+
machineId: params.id,
|
|
98
|
+
harness,
|
|
71
99
|
});
|
|
72
100
|
return reply.status(201).send(started);
|
|
73
101
|
} catch (error) {
|
|
@@ -77,16 +105,47 @@ export function registerMemberMachineRoutes(
|
|
|
77
105
|
);
|
|
78
106
|
|
|
79
107
|
app.get(
|
|
80
|
-
"/api/work/me/machines/:id/
|
|
108
|
+
"/api/work/me/machines/:id/:harness/sign-in/:attempt",
|
|
81
109
|
async (request, reply) => {
|
|
82
110
|
const identity = await member(request, reply);
|
|
83
111
|
if (!identity) return reply;
|
|
84
|
-
const
|
|
112
|
+
const params = attemptParams.parse(request.params);
|
|
113
|
+
const harness = await harnessOf(reply, params.harness);
|
|
114
|
+
if (!harness) return reply;
|
|
85
115
|
try {
|
|
86
116
|
return await options.machines.status({
|
|
87
117
|
identity,
|
|
88
|
-
machineId: id,
|
|
89
|
-
|
|
118
|
+
machineId: params.id,
|
|
119
|
+
harness,
|
|
120
|
+
attempt: params.attempt,
|
|
121
|
+
});
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return refusal(reply, error);
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
app.post(
|
|
129
|
+
"/api/work/me/machines/:id/:harness/sign-in/:attempt/input",
|
|
130
|
+
async (request, reply) => {
|
|
131
|
+
const identity = await member(request, reply);
|
|
132
|
+
if (!identity) return reply;
|
|
133
|
+
const params = attemptParams.parse(request.params);
|
|
134
|
+
const harness = await harnessOf(reply, params.harness);
|
|
135
|
+
if (!harness) return reply;
|
|
136
|
+
// What was sent is never repeated, in an answer or a log.
|
|
137
|
+
const body = inputBody.safeParse(request.body);
|
|
138
|
+
if (!body.success)
|
|
139
|
+
return reply.status(400).send({
|
|
140
|
+
error: "Send the code the sign-in page gave you as { text }",
|
|
141
|
+
});
|
|
142
|
+
try {
|
|
143
|
+
return await options.machines.input({
|
|
144
|
+
identity,
|
|
145
|
+
machineId: params.id,
|
|
146
|
+
harness,
|
|
147
|
+
attempt: params.attempt,
|
|
148
|
+
text: body.data.text,
|
|
90
149
|
});
|
|
91
150
|
} catch (error) {
|
|
92
151
|
return refusal(reply, error);
|
|
@@ -95,13 +154,20 @@ export function registerMemberMachineRoutes(
|
|
|
95
154
|
);
|
|
96
155
|
|
|
97
156
|
app.delete(
|
|
98
|
-
"/api/work/me/machines/:id/
|
|
157
|
+
"/api/work/me/machines/:id/:harness/sign-in/:attempt",
|
|
99
158
|
async (request, reply) => {
|
|
100
159
|
const identity = await member(request, reply);
|
|
101
160
|
if (!identity) return reply;
|
|
102
|
-
const
|
|
161
|
+
const params = attemptParams.parse(request.params);
|
|
162
|
+
const harness = await harnessOf(reply, params.harness);
|
|
163
|
+
if (!harness) return reply;
|
|
103
164
|
try {
|
|
104
|
-
await options.machines.cancel({
|
|
165
|
+
await options.machines.cancel({
|
|
166
|
+
identity,
|
|
167
|
+
machineId: params.id,
|
|
168
|
+
harness,
|
|
169
|
+
attempt: params.attempt,
|
|
170
|
+
});
|
|
105
171
|
return { ok: true };
|
|
106
172
|
} catch (error) {
|
|
107
173
|
return refusal(reply, error);
|
|
@@ -109,12 +175,18 @@ export function registerMemberMachineRoutes(
|
|
|
109
175
|
},
|
|
110
176
|
);
|
|
111
177
|
|
|
112
|
-
app.delete("/api/work/me/machines/:id
|
|
178
|
+
app.delete("/api/work/me/machines/:id/:harness", async (request, reply) => {
|
|
113
179
|
const identity = await member(request, reply);
|
|
114
180
|
if (!identity) return reply;
|
|
115
|
-
const
|
|
181
|
+
const params = harnessParams.parse(request.params);
|
|
182
|
+
const harness = await harnessOf(reply, params.harness);
|
|
183
|
+
if (!harness) return reply;
|
|
116
184
|
try {
|
|
117
|
-
return await options.machines.signOut({
|
|
185
|
+
return await options.machines.signOut({
|
|
186
|
+
identity,
|
|
187
|
+
machineId: params.id,
|
|
188
|
+
harness,
|
|
189
|
+
});
|
|
118
190
|
} catch (error) {
|
|
119
191
|
return refusal(reply, error);
|
|
120
192
|
}
|
|
@@ -3,105 +3,128 @@ import {
|
|
|
3
3
|
accessTier,
|
|
4
4
|
MACHINE_CAPABILITIES,
|
|
5
5
|
parseSignInCapability,
|
|
6
|
+
type SignInHarness,
|
|
6
7
|
signInCapability,
|
|
7
8
|
} from "@catamorphic/sandbox";
|
|
8
9
|
import { z } from "zod";
|
|
10
|
+
import type { MachineFacts, MachineState } from "../machines/contract.js";
|
|
9
11
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
type
|
|
14
|
-
type
|
|
15
|
-
|
|
16
|
-
type
|
|
17
|
-
|
|
18
|
-
type
|
|
19
|
-
} from "./
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
12
|
+
HarnessSignedOutSchema,
|
|
13
|
+
HarnessSignInRefusedError,
|
|
14
|
+
HarnessSignInRefusedSchema,
|
|
15
|
+
type HarnessSignInRequest,
|
|
16
|
+
type HarnessSignInStarted,
|
|
17
|
+
HarnessSignInStartedSchema,
|
|
18
|
+
type HarnessSignInStatus,
|
|
19
|
+
HarnessSignInStatusSchema,
|
|
20
|
+
type HarnessSignIns,
|
|
21
|
+
} from "./harness-sign-ins.js";
|
|
22
|
+
import { WORKER_NODE_PREFIX } from "./machine-store.js";
|
|
23
|
+
import { nodeAccess } from "./placement.js";
|
|
22
24
|
|
|
23
25
|
/*
|
|
24
|
-
* A member's own machines, as the app shows them for
|
|
25
|
-
* 0213): workers whose access names only that member,
|
|
26
|
-
* server's own machine when its operator accepted personal
|
|
27
|
-
* there and no one else signed in on it first. A sign-in on
|
|
28
|
-
* machine would show the provider several people's accounts
|
|
29
|
-
* machine, so it is never offered, and placement would not run
|
|
26
|
+
* A member's own machines, as the app shows them for Claude Code and Codex
|
|
27
|
+
* sign-ins (ADRs 0213, 0342): workers whose access names only that member,
|
|
28
|
+
* and a single server's own machine when its operator accepted personal
|
|
29
|
+
* credentials there and no one else signed in on it first. A sign-in on
|
|
30
|
+
* any other machine would show the provider several people's accounts
|
|
31
|
+
* from one machine, so it is never offered, and placement would not run
|
|
32
|
+
* on it.
|
|
30
33
|
*/
|
|
31
34
|
|
|
35
|
+
export type MemberSignInState = "signed-in" | "signed-out";
|
|
36
|
+
|
|
32
37
|
export interface MemberMachine {
|
|
33
38
|
id: string;
|
|
34
39
|
name: string;
|
|
40
|
+
/** Connected and taking work now. */
|
|
35
41
|
available: boolean;
|
|
36
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Where the machine stands (ADRs 0349, 0350), so its owner sees that it
|
|
44
|
+
* is starting, stopped or coming back rather than only "unavailable".
|
|
45
|
+
* `serving` for a single server's own machine.
|
|
46
|
+
*/
|
|
47
|
+
state: MachineState;
|
|
48
|
+
/** Why it exists or was stopped, when its host says. */
|
|
49
|
+
reason: string | null;
|
|
50
|
+
/** Whether the member is signed in to each harness on this machine. */
|
|
51
|
+
signIns: Record<SignInHarness, MemberSignInState>;
|
|
37
52
|
}
|
|
38
53
|
|
|
39
54
|
/** The machine is not one the member may sign in on. */
|
|
40
55
|
export class NotYourMachineError extends Error {
|
|
41
56
|
constructor() {
|
|
42
|
-
super(
|
|
43
|
-
"This is not a machine of your own, so your Codex sign-in cannot go on it",
|
|
44
|
-
);
|
|
57
|
+
super("This is not a machine of your own, so your sign-in cannot go on it");
|
|
45
58
|
this.name = "NotYourMachineError";
|
|
46
59
|
}
|
|
47
60
|
}
|
|
48
61
|
|
|
62
|
+
/** One of the member's machines and a harness on it. */
|
|
63
|
+
interface Target {
|
|
64
|
+
identity: Identity;
|
|
65
|
+
machineId: string;
|
|
66
|
+
harness: SignInHarness;
|
|
67
|
+
}
|
|
68
|
+
|
|
49
69
|
export class MemberMachines {
|
|
50
70
|
constructor(
|
|
51
71
|
private readonly deps: {
|
|
52
72
|
nodes: () => Promise<readonly WorkerNode[]>;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
owner: (
|
|
56
|
-
userId: string,
|
|
57
|
-
) => Promise<{ userId: string; groups: string[] } | undefined>;
|
|
73
|
+
/** Every machine Work knows, with whom it is for and where it stands. */
|
|
74
|
+
machines: () => Promise<readonly MachineFacts[]>;
|
|
58
75
|
/**
|
|
59
76
|
* The server's own machine, when it may hold a member's sign-in: a
|
|
60
77
|
* single server whose operator accepted personal credentials.
|
|
61
78
|
*/
|
|
62
|
-
ownMachine?: { nodeId: string; signIns:
|
|
79
|
+
ownMachine?: { nodeId: string; signIns: HarnessSignIns };
|
|
63
80
|
/** A request to a worker through the operation queue. */
|
|
64
81
|
worker: (args: {
|
|
65
82
|
nodeId: string;
|
|
66
|
-
request:
|
|
83
|
+
request: HarnessSignInRequest;
|
|
67
84
|
}) => Promise<unknown>;
|
|
68
85
|
},
|
|
69
86
|
) {}
|
|
70
87
|
|
|
71
88
|
async list(identity: Identity): Promise<MemberMachine[]> {
|
|
72
89
|
const own = await this.own(identity);
|
|
73
|
-
const nodes =
|
|
90
|
+
const nodes = new Map(
|
|
91
|
+
(await this.deps.nodes()).map((node) => [node.id, node]),
|
|
92
|
+
);
|
|
74
93
|
const member = identity.externalUserId;
|
|
75
|
-
return
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
id
|
|
79
|
-
name: node.id
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
94
|
+
return [...own].map(([id, machine]) => {
|
|
95
|
+
const node = nodes.get(id);
|
|
96
|
+
return {
|
|
97
|
+
id,
|
|
98
|
+
name: machine?.name ?? node?.descriptor.label ?? id,
|
|
99
|
+
available: machine
|
|
100
|
+
? machine.state === "serving"
|
|
101
|
+
: Boolean(node?.available),
|
|
102
|
+
state: machine?.state ?? "serving",
|
|
103
|
+
reason: machine?.reason ?? null,
|
|
104
|
+
signIns: signInStates((harness) =>
|
|
105
|
+
node?.descriptor.capabilities.includes(
|
|
106
|
+
signInCapability({ harness, member }),
|
|
107
|
+
)
|
|
108
|
+
? "signed-in"
|
|
109
|
+
: "signed-out",
|
|
110
|
+
),
|
|
111
|
+
};
|
|
112
|
+
});
|
|
89
113
|
}
|
|
90
114
|
|
|
91
|
-
/** Throws {@link
|
|
92
|
-
async begin(args: {
|
|
93
|
-
identity: Identity;
|
|
94
|
-
machineId: string;
|
|
95
|
-
}): Promise<CodexSignInStarted> {
|
|
115
|
+
/** Throws {@link HarnessSignInRefusedError} with the machine's reason. */
|
|
116
|
+
async begin(args: Target): Promise<HarnessSignInStarted> {
|
|
96
117
|
const answer = z
|
|
97
|
-
.union([
|
|
118
|
+
.union([HarnessSignInStartedSchema, HarnessSignInRefusedSchema])
|
|
98
119
|
.parse(
|
|
99
120
|
await this.send(args, {
|
|
100
121
|
action: "begin",
|
|
122
|
+
harness: args.harness,
|
|
101
123
|
member: args.identity.externalUserId,
|
|
102
124
|
}),
|
|
103
125
|
);
|
|
104
|
-
if ("refused" in answer)
|
|
126
|
+
if ("refused" in answer)
|
|
127
|
+
throw new HarnessSignInRefusedError(answer.refused);
|
|
105
128
|
return answer;
|
|
106
129
|
}
|
|
107
130
|
|
|
@@ -109,59 +132,87 @@ export class MemberMachines {
|
|
|
109
132
|
* Where a sign-in stands. Signed in means usable: the machine placed the
|
|
110
133
|
* login and placement sees it, so a chat started at once finds it.
|
|
111
134
|
*/
|
|
112
|
-
async status(
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const node = (await this.deps.nodes()).find(
|
|
126
|
-
(entry) => entry.id === args.machineId,
|
|
127
|
-
);
|
|
128
|
-
const reported = node?.descriptor.capabilities.includes(
|
|
129
|
-
signInCapability({
|
|
130
|
-
harness: "codex",
|
|
131
|
-
member: args.identity.externalUserId,
|
|
132
|
-
}),
|
|
135
|
+
async status(
|
|
136
|
+
args: Target & { attempt: string },
|
|
137
|
+
): Promise<HarnessSignInStatus> {
|
|
138
|
+
return this.usable(
|
|
139
|
+
args,
|
|
140
|
+
HarnessSignInStatusSchema.parse(
|
|
141
|
+
await this.send(args, {
|
|
142
|
+
action: "status",
|
|
143
|
+
harness: args.harness,
|
|
144
|
+
member: args.identity.externalUserId,
|
|
145
|
+
attempt: args.attempt,
|
|
146
|
+
}),
|
|
147
|
+
),
|
|
133
148
|
);
|
|
134
|
-
return reported ? status : { state: "waiting" };
|
|
135
149
|
}
|
|
136
150
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
151
|
+
/**
|
|
152
|
+
* Hand a waiting login the code the member copied from their browser.
|
|
153
|
+
* Answers where the sign-in stands, never the code. Throws
|
|
154
|
+
* {@link HarnessSignInRefusedError} when the machine would not take it.
|
|
155
|
+
*/
|
|
156
|
+
async input(
|
|
157
|
+
args: Target & { attempt: string; text: string },
|
|
158
|
+
): Promise<HarnessSignInStatus> {
|
|
159
|
+
const answer = z
|
|
160
|
+
.union([HarnessSignInStatusSchema, HarnessSignInRefusedSchema])
|
|
161
|
+
.parse(
|
|
162
|
+
await this.send(args, {
|
|
163
|
+
action: "input",
|
|
164
|
+
harness: args.harness,
|
|
165
|
+
member: args.identity.externalUserId,
|
|
166
|
+
attempt: args.attempt,
|
|
167
|
+
text: args.text,
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
if ("refused" in answer)
|
|
171
|
+
throw new HarnessSignInRefusedError(answer.refused);
|
|
172
|
+
return this.usable(args, answer);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async cancel(args: Target & { attempt: string }): Promise<void> {
|
|
142
176
|
await this.send(args, {
|
|
143
177
|
action: "cancel",
|
|
178
|
+
harness: args.harness,
|
|
144
179
|
member: args.identity.externalUserId,
|
|
145
180
|
attempt: args.attempt,
|
|
146
181
|
});
|
|
147
182
|
}
|
|
148
183
|
|
|
149
|
-
async signOut(args: {
|
|
150
|
-
|
|
151
|
-
machineId: string;
|
|
152
|
-
}): Promise<{ signedOut: boolean }> {
|
|
153
|
-
return CodexSignedOutSchema.parse(
|
|
184
|
+
async signOut(args: Target): Promise<{ signedOut: boolean }> {
|
|
185
|
+
return HarnessSignedOutSchema.parse(
|
|
154
186
|
await this.send(args, {
|
|
155
187
|
action: "signOut",
|
|
188
|
+
harness: args.harness,
|
|
156
189
|
member: args.identity.externalUserId,
|
|
157
190
|
}),
|
|
158
191
|
);
|
|
159
192
|
}
|
|
160
193
|
|
|
194
|
+
/** A machine's "signed in" counts once placement sees the sign-in too. */
|
|
195
|
+
private async usable(
|
|
196
|
+
args: Target,
|
|
197
|
+
status: HarnessSignInStatus,
|
|
198
|
+
): Promise<HarnessSignInStatus> {
|
|
199
|
+
if (status.state !== "signed-in") return status;
|
|
200
|
+
const node = (await this.deps.nodes()).find(
|
|
201
|
+
(entry) => entry.id === args.machineId,
|
|
202
|
+
);
|
|
203
|
+
const reported = node?.descriptor.capabilities.includes(
|
|
204
|
+
signInCapability({
|
|
205
|
+
harness: args.harness,
|
|
206
|
+
member: args.identity.externalUserId,
|
|
207
|
+
}),
|
|
208
|
+
);
|
|
209
|
+
return reported ? status : { state: "waiting" };
|
|
210
|
+
}
|
|
211
|
+
|
|
161
212
|
/** The member's own machine gets the request, and only then. */
|
|
162
213
|
private async send(
|
|
163
214
|
args: { identity: Identity; machineId: string },
|
|
164
|
-
request:
|
|
215
|
+
request: HarnessSignInRequest,
|
|
165
216
|
): Promise<unknown> {
|
|
166
217
|
const own = await this.own(args.identity);
|
|
167
218
|
if (!own.has(args.machineId)) throw new NotYourMachineError();
|
|
@@ -171,19 +222,22 @@ export class MemberMachines {
|
|
|
171
222
|
return this.deps.worker({ nodeId: args.machineId, request });
|
|
172
223
|
}
|
|
173
224
|
|
|
174
|
-
/**
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
225
|
+
/**
|
|
226
|
+
* The machines that are the member's own, by node id: workers for that
|
|
227
|
+
* member alone (with their facts), and a single server's own machine.
|
|
228
|
+
*/
|
|
229
|
+
private async own(
|
|
230
|
+
identity: Identity,
|
|
231
|
+
): Promise<Map<string, MachineFacts | undefined>> {
|
|
232
|
+
const owner = { userId: identity.externalUserId };
|
|
233
|
+
const own = new Map<string, MachineFacts | undefined>();
|
|
234
|
+
for (const machine of await this.deps.machines()) {
|
|
235
|
+
// One being destroyed, or dropped by the plan, is theirs no more.
|
|
236
|
+
if (machine.state === "destroying" || machine.state === "draining")
|
|
237
|
+
continue;
|
|
238
|
+
if (accessTier({ access: nodeAccess(machine.for), owner }) === 1)
|
|
239
|
+
own.set(`${WORKER_NODE_PREFIX}${machine.name}`, machine);
|
|
240
|
+
}
|
|
187
241
|
const ownMachine = this.deps.ownMachine;
|
|
188
242
|
if (ownMachine) {
|
|
189
243
|
const node = (await this.deps.nodes()).find(
|
|
@@ -199,8 +253,18 @@ export class MemberMachines {
|
|
|
199
253
|
capabilities.includes(MACHINE_CAPABILITIES.personalCredentials) &&
|
|
200
254
|
!heldByOther
|
|
201
255
|
)
|
|
202
|
-
|
|
256
|
+
own.set(ownMachine.nodeId, undefined);
|
|
203
257
|
}
|
|
204
|
-
return
|
|
258
|
+
return own;
|
|
205
259
|
}
|
|
206
260
|
}
|
|
261
|
+
|
|
262
|
+
/** One state per harness a member signs in to. */
|
|
263
|
+
function signInStates(
|
|
264
|
+
state: (harness: SignInHarness) => MemberSignInState,
|
|
265
|
+
): Record<SignInHarness, MemberSignInState> {
|
|
266
|
+
return {
|
|
267
|
+
"claude-code": state("claude-code"),
|
|
268
|
+
codex: state("codex"),
|
|
269
|
+
};
|
|
270
|
+
}
|