@catamorphic/work-server 0.1.0-alpha.42 → 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/agents.d.ts.map +1 -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/execution-config.d.ts +2 -1
- package/dist/execution-config.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +962 -268
- package/dist/index.js.map +21 -19
- 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 +40 -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/disk-care.d.ts +36 -1
- package/dist/workers/disk-care.d.ts.map +1 -1
- 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/agents.ts +5 -0
- package/src/cluster.ts +62 -7
- package/src/execution-config.ts +2 -1
- 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 +71 -3
- package/src/server.ts +120 -29
- package/src/testing.ts +1 -0
- package/src/workers/cache-archive-routes.ts +125 -0
- package/src/workers/disk-care.ts +157 -6
- package/src/workers/harness-sign-ins.ts +207 -22
- package/src/workers/machine-store.ts +341 -58
- 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 +9 -3
- package/dist/chunk-kcck20aw.js.map +0 -11
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
AccessDeniedError,
|
|
3
|
+
EnvironmentAccessDeniedError,
|
|
4
|
+
EnvironmentIncompatibleError,
|
|
5
|
+
EnvironmentNotFoundError,
|
|
6
|
+
EnvironmentPolicyInvalidError,
|
|
7
|
+
ExecutorNotConnectedError,
|
|
8
|
+
type Identity,
|
|
9
|
+
ProjectNotFoundError,
|
|
10
|
+
} from "@catamorphic/core";
|
|
2
11
|
import { type SignInHarness, signInHarness } from "@catamorphic/sandbox";
|
|
3
12
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
4
13
|
import { z } from "zod";
|
|
@@ -14,6 +23,9 @@ import { type MemberMachines, NotYourMachineError } from "./member-machines.js";
|
|
|
14
23
|
* machine of the caller's own; the sign-in itself happens in the member's
|
|
15
24
|
* browser and on the machine. The code a member pastes for a login that
|
|
16
25
|
* waits for one passes through to the machine: no route logs or repeats it.
|
|
26
|
+
* A sign-in names the project Environment it is for, whose image makes it
|
|
27
|
+
* (ADR 0363); one begun on a stopped machine answers 202 `starting` while
|
|
28
|
+
* the machine starts, and the app begins it again once it serves.
|
|
17
29
|
*/
|
|
18
30
|
export function registerMemberMachineRoutes(
|
|
19
31
|
app: FastifyInstance,
|
|
@@ -30,6 +42,11 @@ export function registerMemberMachineRoutes(
|
|
|
30
42
|
const inputBody = z.strictObject({
|
|
31
43
|
text: z.string().min(1).max(SIGN_IN_INPUT_MAX),
|
|
32
44
|
});
|
|
45
|
+
const beginBody = z.strictObject({
|
|
46
|
+
project: z.string().uuid(),
|
|
47
|
+
environment: z.string().min(1).max(200),
|
|
48
|
+
});
|
|
49
|
+
const listQuery = z.object({ project: z.string().uuid().optional() });
|
|
33
50
|
|
|
34
51
|
/** The caller, or a 401 already sent. */
|
|
35
52
|
const member = async (
|
|
@@ -59,8 +76,24 @@ export function registerMemberMachineRoutes(
|
|
|
59
76
|
|
|
60
77
|
/** What stopped a request, as the app shows it. */
|
|
61
78
|
const refusal = (reply: FastifyReply, error: unknown) => {
|
|
62
|
-
if (
|
|
79
|
+
if (
|
|
80
|
+
error instanceof NotYourMachineError ||
|
|
81
|
+
error instanceof AccessDeniedError ||
|
|
82
|
+
error instanceof EnvironmentAccessDeniedError
|
|
83
|
+
)
|
|
63
84
|
return reply.status(403).send({ error: error.message });
|
|
85
|
+
if (
|
|
86
|
+
error instanceof EnvironmentNotFoundError ||
|
|
87
|
+
error instanceof ProjectNotFoundError
|
|
88
|
+
)
|
|
89
|
+
return reply.status(404).send({ error: error.message });
|
|
90
|
+
if (
|
|
91
|
+
error instanceof EnvironmentIncompatibleError ||
|
|
92
|
+
error instanceof EnvironmentPolicyInvalidError
|
|
93
|
+
)
|
|
94
|
+
return reply
|
|
95
|
+
.status(409)
|
|
96
|
+
.send({ error: error.message, code: "environment_refused" });
|
|
64
97
|
if (error instanceof HarnessSignInRefusedError)
|
|
65
98
|
return reply
|
|
66
99
|
.status(409)
|
|
@@ -80,7 +113,19 @@ export function registerMemberMachineRoutes(
|
|
|
80
113
|
app.get("/api/work/me/machines", async (request, reply) => {
|
|
81
114
|
const identity = await member(request, reply);
|
|
82
115
|
if (!identity) return reply;
|
|
83
|
-
|
|
116
|
+
const query = listQuery.safeParse(request.query);
|
|
117
|
+
if (!query.success)
|
|
118
|
+
return reply.status(400).send({ error: "project is a project's id" });
|
|
119
|
+
try {
|
|
120
|
+
return {
|
|
121
|
+
machines: await options.machines.list(
|
|
122
|
+
identity,
|
|
123
|
+
query.data.project ? { projectId: query.data.project } : {},
|
|
124
|
+
),
|
|
125
|
+
};
|
|
126
|
+
} catch (error) {
|
|
127
|
+
return refusal(reply, error);
|
|
128
|
+
}
|
|
84
129
|
});
|
|
85
130
|
|
|
86
131
|
app.post(
|
|
@@ -91,12 +136,22 @@ export function registerMemberMachineRoutes(
|
|
|
91
136
|
const params = harnessParams.parse(request.params);
|
|
92
137
|
const harness = await harnessOf(reply, params.harness);
|
|
93
138
|
if (!harness) return reply;
|
|
139
|
+
const body = beginBody.safeParse(request.body);
|
|
140
|
+
if (!body.success)
|
|
141
|
+
return reply.status(400).send({
|
|
142
|
+
error:
|
|
143
|
+
"Name the project and the Environment the sign-in is for as { project, environment }",
|
|
144
|
+
});
|
|
94
145
|
try {
|
|
95
146
|
const started = await options.machines.begin({
|
|
96
147
|
identity,
|
|
97
148
|
machineId: params.id,
|
|
98
149
|
harness,
|
|
150
|
+
projectId: body.data.project,
|
|
151
|
+
environment: body.data.environment,
|
|
99
152
|
});
|
|
153
|
+
// The machine is stopped: it starts, and the sign-in begins again.
|
|
154
|
+
if ("state" in started) return reply.status(202).send(started);
|
|
100
155
|
return reply.status(201).send(started);
|
|
101
156
|
} catch (error) {
|
|
102
157
|
return refusal(reply, error);
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
Identity,
|
|
3
|
+
SignInEnvironment,
|
|
4
|
+
WorkerNode,
|
|
5
|
+
} from "@catamorphic/core";
|
|
2
6
|
import {
|
|
3
7
|
accessTier,
|
|
4
8
|
MACHINE_CAPABILITIES,
|
|
5
9
|
parseSignInCapability,
|
|
10
|
+
SIGN_IN_HARNESSES,
|
|
6
11
|
type SignInHarness,
|
|
7
12
|
signInCapability,
|
|
8
13
|
} from "@catamorphic/sandbox";
|
|
@@ -18,9 +23,11 @@ import {
|
|
|
18
23
|
type HarnessSignInStatus,
|
|
19
24
|
HarnessSignInStatusSchema,
|
|
20
25
|
type HarnessSignIns,
|
|
26
|
+
type SignInSandbox,
|
|
21
27
|
} from "./harness-sign-ins.js";
|
|
22
28
|
import { WORKER_NODE_PREFIX } from "./machine-store.js";
|
|
23
29
|
import { nodeAccess } from "./placement.js";
|
|
30
|
+
import type { SignInMade, SignInStore } from "./sign-in-store.js";
|
|
24
31
|
|
|
25
32
|
/*
|
|
26
33
|
* A member's own machines, as the app shows them for Claude Code and Codex
|
|
@@ -49,8 +56,36 @@ export interface MemberMachine {
|
|
|
49
56
|
reason: string | null;
|
|
50
57
|
/** Whether the member is signed in to each harness on this machine. */
|
|
51
58
|
signIns: Record<SignInHarness, MemberSignInState>;
|
|
59
|
+
/**
|
|
60
|
+
* How each sign-in there was made (ADR 0363): the Environment and image
|
|
61
|
+
* it was begun with. A turn in any Environment on the machine uses it.
|
|
62
|
+
*/
|
|
63
|
+
made: Partial<Record<SignInHarness, SignInMade>>;
|
|
64
|
+
/**
|
|
65
|
+
* The project's Environments a sign-in here may be made with, when the
|
|
66
|
+
* list was asked for a project: ones that allow personal credentials,
|
|
67
|
+
* that the member may use, and that this machine serves.
|
|
68
|
+
*/
|
|
69
|
+
environments?: Array<{ name: string; image: string | null }>;
|
|
52
70
|
}
|
|
53
71
|
|
|
72
|
+
/** A sign-in begun on a machine that was stopped: it is starting (ADR 0363). */
|
|
73
|
+
export interface MachineStarting {
|
|
74
|
+
state: "starting";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Where a machine of the member's own does not serve yet, so a sign-in
|
|
79
|
+
* there waits for it to start instead of failing (ADR 0363).
|
|
80
|
+
* Replica memory (c): a constant, the same on every replica.
|
|
81
|
+
*/
|
|
82
|
+
const NOT_YET_SERVING: ReadonlySet<MachineState> = new Set([
|
|
83
|
+
"starting",
|
|
84
|
+
"stopping",
|
|
85
|
+
"stopped",
|
|
86
|
+
"resuming",
|
|
87
|
+
]);
|
|
88
|
+
|
|
54
89
|
/** The machine is not one the member may sign in on. */
|
|
55
90
|
export class NotYourMachineError extends Error {
|
|
56
91
|
constructor() {
|
|
@@ -82,17 +117,51 @@ export class MemberMachines {
|
|
|
82
117
|
nodeId: string;
|
|
83
118
|
request: HarnessSignInRequest;
|
|
84
119
|
}) => Promise<unknown>;
|
|
120
|
+
/** A project's Environments as a sign-in reads them (ADR 0363). */
|
|
121
|
+
environments: {
|
|
122
|
+
signInEnvironments(args: {
|
|
123
|
+
identity: Identity;
|
|
124
|
+
projectId: string;
|
|
125
|
+
}): Promise<SignInEnvironment[]>;
|
|
126
|
+
signInSandbox(args: {
|
|
127
|
+
identity: Identity;
|
|
128
|
+
projectId: string;
|
|
129
|
+
environment: string;
|
|
130
|
+
}): Promise<SignInEnvironment & { sandbox: SignInSandbox }>;
|
|
131
|
+
};
|
|
132
|
+
/** Sign-ins waiting for their machine, and how each was made. */
|
|
133
|
+
signIns: Pick<
|
|
134
|
+
SignInStore,
|
|
135
|
+
"wait" | "began" | "begun" | "signedIn" | "signedOut" | "made"
|
|
136
|
+
>;
|
|
85
137
|
},
|
|
86
138
|
) {}
|
|
87
139
|
|
|
88
|
-
async list(
|
|
140
|
+
async list(
|
|
141
|
+
identity: Identity,
|
|
142
|
+
options: { projectId?: string } = {},
|
|
143
|
+
): Promise<MemberMachine[]> {
|
|
89
144
|
const own = await this.own(identity);
|
|
90
145
|
const nodes = new Map(
|
|
91
146
|
(await this.deps.nodes()).map((node) => [node.id, node]),
|
|
92
147
|
);
|
|
93
148
|
const member = identity.externalUserId;
|
|
149
|
+
const made = await this.deps.signIns.made({ member });
|
|
150
|
+
const environments = options.projectId
|
|
151
|
+
? await this.deps.environments.signInEnvironments({
|
|
152
|
+
identity,
|
|
153
|
+
projectId: options.projectId,
|
|
154
|
+
})
|
|
155
|
+
: undefined;
|
|
94
156
|
return [...own].map(([id, machine]) => {
|
|
95
157
|
const node = nodes.get(id);
|
|
158
|
+
const signIns = signInStates((harness) =>
|
|
159
|
+
node?.descriptor.capabilities.includes(
|
|
160
|
+
signInCapability({ harness, member }),
|
|
161
|
+
)
|
|
162
|
+
? "signed-in"
|
|
163
|
+
: "signed-out",
|
|
164
|
+
);
|
|
96
165
|
return {
|
|
97
166
|
id,
|
|
98
167
|
name: machine?.name ?? node?.descriptor.label ?? id,
|
|
@@ -101,30 +170,87 @@ export class MemberMachines {
|
|
|
101
170
|
: Boolean(node?.available),
|
|
102
171
|
state: machine?.state ?? "serving",
|
|
103
172
|
reason: machine?.reason ?? null,
|
|
104
|
-
signIns
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
)
|
|
108
|
-
|
|
109
|
-
|
|
173
|
+
signIns,
|
|
174
|
+
// Only for a sign-in the machine still holds.
|
|
175
|
+
made: Object.fromEntries(
|
|
176
|
+
SIGN_IN_HARNESSES.flatMap((harness) => {
|
|
177
|
+
const entry = made.get(id)?.[harness];
|
|
178
|
+
return entry && signIns[harness] === "signed-in"
|
|
179
|
+
? [[harness, entry]]
|
|
180
|
+
: [];
|
|
181
|
+
}),
|
|
110
182
|
),
|
|
183
|
+
...(environments
|
|
184
|
+
? {
|
|
185
|
+
environments: environments
|
|
186
|
+
.filter((environment) => serves(machine, environment))
|
|
187
|
+
.map(({ name, image }) => ({ name, image })),
|
|
188
|
+
}
|
|
189
|
+
: {}),
|
|
111
190
|
};
|
|
112
191
|
});
|
|
113
192
|
}
|
|
114
193
|
|
|
115
|
-
/**
|
|
116
|
-
|
|
194
|
+
/**
|
|
195
|
+
* Begin a harness's sign-in on one of the member's machines, made with
|
|
196
|
+
* the image of the Environment named (ADR 0363). On a machine that is
|
|
197
|
+
* stopped or on its way, the sign-in waits for it and the plan starts it:
|
|
198
|
+
* the answer is {@link MachineStarting}, and the app begins again once
|
|
199
|
+
* the machine serves. Throws {@link HarnessSignInRefusedError} with the
|
|
200
|
+
* machine's reason.
|
|
201
|
+
*/
|
|
202
|
+
async begin(
|
|
203
|
+
args: Target & { projectId: string; environment: string },
|
|
204
|
+
): Promise<HarnessSignInStarted | MachineStarting> {
|
|
205
|
+
const own = await this.own(args.identity);
|
|
206
|
+
if (!own.has(args.machineId)) throw new NotYourMachineError();
|
|
207
|
+
const machine = own.get(args.machineId);
|
|
208
|
+
const resolved = await this.deps.environments.signInSandbox({
|
|
209
|
+
identity: args.identity,
|
|
210
|
+
projectId: args.projectId,
|
|
211
|
+
environment: args.environment,
|
|
212
|
+
});
|
|
213
|
+
if (!serves(machine, resolved))
|
|
214
|
+
throw new HarnessSignInRefusedError(
|
|
215
|
+
`Environment '${args.environment}' does not run on this machine. Choose one that does`,
|
|
216
|
+
);
|
|
217
|
+
const member = args.identity.externalUserId;
|
|
218
|
+
if (machine && NOT_YET_SERVING.has(machine.state)) {
|
|
219
|
+
await this.deps.signIns.wait({
|
|
220
|
+
machine: machine.name,
|
|
221
|
+
member,
|
|
222
|
+
harness: args.harness,
|
|
223
|
+
project: args.projectId,
|
|
224
|
+
environment: args.environment,
|
|
225
|
+
});
|
|
226
|
+
return { state: "starting" };
|
|
227
|
+
}
|
|
117
228
|
const answer = z
|
|
118
229
|
.union([HarnessSignInStartedSchema, HarnessSignInRefusedSchema])
|
|
119
230
|
.parse(
|
|
120
231
|
await this.send(args, {
|
|
121
232
|
action: "begin",
|
|
122
233
|
harness: args.harness,
|
|
123
|
-
member
|
|
234
|
+
member,
|
|
235
|
+
sandbox: resolved.sandbox,
|
|
124
236
|
}),
|
|
125
237
|
);
|
|
238
|
+
// It reached the machine: it waits for it no more, begun or refused.
|
|
239
|
+
if (machine)
|
|
240
|
+
await this.deps.signIns.began({ machine: machine.name, member });
|
|
126
241
|
if ("refused" in answer)
|
|
127
242
|
throw new HarnessSignInRefusedError(answer.refused);
|
|
243
|
+
await this.deps.signIns.begun({
|
|
244
|
+
attempt: answer.attempt,
|
|
245
|
+
nodeId: args.machineId,
|
|
246
|
+
member,
|
|
247
|
+
harness: args.harness,
|
|
248
|
+
made: {
|
|
249
|
+
project: args.projectId,
|
|
250
|
+
environment: resolved.name,
|
|
251
|
+
image: resolved.image,
|
|
252
|
+
},
|
|
253
|
+
});
|
|
128
254
|
return answer;
|
|
129
255
|
}
|
|
130
256
|
|
|
@@ -182,18 +308,24 @@ export class MemberMachines {
|
|
|
182
308
|
}
|
|
183
309
|
|
|
184
310
|
async signOut(args: Target): Promise<{ signedOut: boolean }> {
|
|
185
|
-
|
|
311
|
+
const answer = HarnessSignedOutSchema.parse(
|
|
186
312
|
await this.send(args, {
|
|
187
313
|
action: "signOut",
|
|
188
314
|
harness: args.harness,
|
|
189
315
|
member: args.identity.externalUserId,
|
|
190
316
|
}),
|
|
191
317
|
);
|
|
318
|
+
await this.deps.signIns.signedOut({
|
|
319
|
+
nodeId: args.machineId,
|
|
320
|
+
member: args.identity.externalUserId,
|
|
321
|
+
harness: args.harness,
|
|
322
|
+
});
|
|
323
|
+
return answer;
|
|
192
324
|
}
|
|
193
325
|
|
|
194
326
|
/** A machine's "signed in" counts once placement sees the sign-in too. */
|
|
195
327
|
private async usable(
|
|
196
|
-
args: Target,
|
|
328
|
+
args: Target & { attempt: string },
|
|
197
329
|
status: HarnessSignInStatus,
|
|
198
330
|
): Promise<HarnessSignInStatus> {
|
|
199
331
|
if (status.state !== "signed-in") return status;
|
|
@@ -206,7 +338,14 @@ export class MemberMachines {
|
|
|
206
338
|
member: args.identity.externalUserId,
|
|
207
339
|
}),
|
|
208
340
|
);
|
|
209
|
-
|
|
341
|
+
if (!reported) return { state: "waiting" };
|
|
342
|
+
await this.deps.signIns.signedIn({
|
|
343
|
+
attempt: args.attempt,
|
|
344
|
+
nodeId: args.machineId,
|
|
345
|
+
member: args.identity.externalUserId,
|
|
346
|
+
harness: args.harness,
|
|
347
|
+
});
|
|
348
|
+
return status;
|
|
210
349
|
}
|
|
211
350
|
|
|
212
351
|
/** The member's own machine gets the request, and only then. */
|
|
@@ -259,6 +398,21 @@ export class MemberMachines {
|
|
|
259
398
|
}
|
|
260
399
|
}
|
|
261
400
|
|
|
401
|
+
/**
|
|
402
|
+
* Whether a machine serves an Environment: it carries the Environment's
|
|
403
|
+
* pool labels. A single server's own machine serves every one.
|
|
404
|
+
*/
|
|
405
|
+
function serves(
|
|
406
|
+
machine: MachineFacts | undefined,
|
|
407
|
+
environment: Pick<SignInEnvironment, "pool">,
|
|
408
|
+
): boolean {
|
|
409
|
+
if (!machine) return true;
|
|
410
|
+
const labels: Record<string, string> = { ...machine.labels, plane: "worker" };
|
|
411
|
+
return Object.entries(environment.pool).every(
|
|
412
|
+
([key, value]) => labels[key] === value,
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
262
416
|
/** One state per harness a member signs in to. */
|
|
263
417
|
function signInStates(
|
|
264
418
|
state: (harness: SignInHarness) => MemberSignInState,
|
package/src/workers/placement.ts
CHANGED
|
@@ -59,6 +59,13 @@ export function workerPlacement(
|
|
|
59
59
|
export type ScheduledPlacement = WorkerPlacement & {
|
|
60
60
|
accepting: boolean;
|
|
61
61
|
admissionRevision: string | null;
|
|
62
|
+
/**
|
|
63
|
+
* A spare no chat key has taken yet (ADR 0360): it takes its project's
|
|
64
|
+
* chats that have a key, and its cache fills.
|
|
65
|
+
*/
|
|
66
|
+
spare?: boolean;
|
|
67
|
+
/** The Environments it is prepared for; such a machine is preferred. */
|
|
68
|
+
prepared?: readonly { project: string; environment: string }[];
|
|
62
69
|
};
|
|
63
70
|
|
|
64
71
|
export function nodeAccess(
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import type { DB } from "@catamorphic/db";
|
|
2
|
+
import { SIGN_IN_HARNESSES, type SignInHarness } from "@catamorphic/sandbox";
|
|
3
|
+
import { type Kysely, sql } from "kysely";
|
|
4
|
+
import {
|
|
5
|
+
SIGN_IN_WAIT_MINUTES,
|
|
6
|
+
type WaitingWork,
|
|
7
|
+
} from "../machines/contract.js";
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* What the control plane keeps about members' sign-ins (ADR 0363): a
|
|
11
|
+
* sign-in waiting for its stopped machine to start, and which Environment
|
|
12
|
+
* and image each sign-in was made with. The sign-in itself stays on its
|
|
13
|
+
* machine; nothing here is a credential.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** How a member's sign-in on a machine was made, for display. */
|
|
17
|
+
export interface SignInMade {
|
|
18
|
+
project: string | null;
|
|
19
|
+
environment: string;
|
|
20
|
+
/** The image reference or Dockerfile path; null for the machine's own. */
|
|
21
|
+
image: string | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class SignInStore {
|
|
25
|
+
constructor(
|
|
26
|
+
private readonly deps: {
|
|
27
|
+
db: Kysely<DB>;
|
|
28
|
+
tenantId: string;
|
|
29
|
+
},
|
|
30
|
+
) {}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A member began a sign-in on a stopped machine of their own: work
|
|
34
|
+
* waiting for exactly that machine, so the plan starts it, until the
|
|
35
|
+
* sign-in begins there or {@link SIGN_IN_WAIT_MINUTES} pass.
|
|
36
|
+
*/
|
|
37
|
+
async wait(args: {
|
|
38
|
+
machine: string;
|
|
39
|
+
member: string;
|
|
40
|
+
harness: SignInHarness;
|
|
41
|
+
project: string;
|
|
42
|
+
environment: string;
|
|
43
|
+
}): Promise<void> {
|
|
44
|
+
const { db, tenantId } = this.deps;
|
|
45
|
+
const expires = sql<Date>`now() + make_interval(mins => ${SIGN_IN_WAIT_MINUTES})`;
|
|
46
|
+
await db
|
|
47
|
+
.deleteFrom("sign_in_waits")
|
|
48
|
+
.where("tenant_id", "=", tenantId)
|
|
49
|
+
.where("expires_at", "<=", sql<Date>`now()`)
|
|
50
|
+
.execute();
|
|
51
|
+
await db
|
|
52
|
+
.insertInto("sign_in_waits")
|
|
53
|
+
.values({
|
|
54
|
+
tenant_id: tenantId,
|
|
55
|
+
machine: args.machine,
|
|
56
|
+
member: args.member,
|
|
57
|
+
harness: args.harness,
|
|
58
|
+
project_id: args.project,
|
|
59
|
+
environment_name: args.environment,
|
|
60
|
+
expires_at: expires,
|
|
61
|
+
})
|
|
62
|
+
.onConflict((conflict) =>
|
|
63
|
+
conflict.columns(["tenant_id", "machine", "member"]).doUpdateSet({
|
|
64
|
+
harness: args.harness,
|
|
65
|
+
project_id: args.project,
|
|
66
|
+
environment_name: args.environment,
|
|
67
|
+
expires_at: expires,
|
|
68
|
+
}),
|
|
69
|
+
)
|
|
70
|
+
.execute();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The sign-in began on its machine (or was given up): it waits no more. */
|
|
74
|
+
async began(args: { machine: string; member: string }): Promise<void> {
|
|
75
|
+
await this.deps.db
|
|
76
|
+
.deleteFrom("sign_in_waits")
|
|
77
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
78
|
+
.where("machine", "=", args.machine)
|
|
79
|
+
.where("member", "=", args.member)
|
|
80
|
+
.execute();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Sign-ins waiting for their machine, oldest first, as the plan reads them. */
|
|
84
|
+
async waiting(): Promise<WaitingWork[]> {
|
|
85
|
+
const rows = await this.deps.db
|
|
86
|
+
.selectFrom("sign_in_waits")
|
|
87
|
+
.leftJoin(
|
|
88
|
+
"work_accounts",
|
|
89
|
+
"work_accounts.user_id",
|
|
90
|
+
"sign_in_waits.member",
|
|
91
|
+
)
|
|
92
|
+
.select([
|
|
93
|
+
"sign_in_waits.id",
|
|
94
|
+
"sign_in_waits.machine",
|
|
95
|
+
"sign_in_waits.member",
|
|
96
|
+
"sign_in_waits.project_id",
|
|
97
|
+
"sign_in_waits.environment_name",
|
|
98
|
+
"sign_in_waits.waiting_since",
|
|
99
|
+
"work_accounts.disabled_at",
|
|
100
|
+
])
|
|
101
|
+
.where("sign_in_waits.tenant_id", "=", this.deps.tenantId)
|
|
102
|
+
.where("sign_in_waits.expires_at", ">", sql<Date>`now()`)
|
|
103
|
+
.orderBy("sign_in_waits.waiting_since")
|
|
104
|
+
.execute();
|
|
105
|
+
// A disabled member's sign-in is no reason for a machine.
|
|
106
|
+
return rows
|
|
107
|
+
.filter((row) => row.disabled_at === null)
|
|
108
|
+
.map((row) => ({
|
|
109
|
+
id: row.id,
|
|
110
|
+
kind: "sign-in",
|
|
111
|
+
project: row.project_id,
|
|
112
|
+
user: row.member,
|
|
113
|
+
key: null,
|
|
114
|
+
environment: row.environment_name,
|
|
115
|
+
pool: {},
|
|
116
|
+
requirements: { resources: {}, capabilities: [] },
|
|
117
|
+
reason: "unavailable",
|
|
118
|
+
waitingSince: row.waiting_since.toISOString(),
|
|
119
|
+
machine: row.machine,
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** What changes the plan's facts: how many wait, and the newest. */
|
|
124
|
+
signature() {
|
|
125
|
+
return this.deps.db
|
|
126
|
+
.selectFrom("sign_in_waits")
|
|
127
|
+
.select(
|
|
128
|
+
sql<string>`count(*)::text || '/' || coalesce(max(waiting_since)::text, '') || '/' || coalesce(max(expires_at)::text, '')`.as(
|
|
129
|
+
"sign_ins",
|
|
130
|
+
),
|
|
131
|
+
)
|
|
132
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
133
|
+
.where("expires_at", ">", sql<Date>`now()`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** A sign-in began on a machine, in this Environment and image. */
|
|
137
|
+
async begun(args: {
|
|
138
|
+
attempt: string;
|
|
139
|
+
nodeId: string;
|
|
140
|
+
member: string;
|
|
141
|
+
harness: SignInHarness;
|
|
142
|
+
made: SignInMade;
|
|
143
|
+
}): Promise<void> {
|
|
144
|
+
const { db, tenantId } = this.deps;
|
|
145
|
+
// Attempts that never completed are not kept for long.
|
|
146
|
+
await db
|
|
147
|
+
.deleteFrom("machine_sign_ins")
|
|
148
|
+
.where("tenant_id", "=", tenantId)
|
|
149
|
+
.where("signed_in_at", "is", null)
|
|
150
|
+
.where("begun_at", "<", sql<Date>`now() - interval '1 day'`)
|
|
151
|
+
.execute();
|
|
152
|
+
await db
|
|
153
|
+
.insertInto("machine_sign_ins")
|
|
154
|
+
.values({
|
|
155
|
+
attempt: args.attempt,
|
|
156
|
+
tenant_id: tenantId,
|
|
157
|
+
node_id: args.nodeId,
|
|
158
|
+
member: args.member,
|
|
159
|
+
harness: args.harness,
|
|
160
|
+
project_id: args.made.project,
|
|
161
|
+
environment_name: args.made.environment,
|
|
162
|
+
image: args.made.image,
|
|
163
|
+
})
|
|
164
|
+
.onConflict((conflict) => conflict.column("attempt").doNothing())
|
|
165
|
+
.execute();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The attempt completed: it is how the member's sign-in there was made. */
|
|
169
|
+
async signedIn(args: {
|
|
170
|
+
attempt: string;
|
|
171
|
+
nodeId: string;
|
|
172
|
+
member: string;
|
|
173
|
+
harness: SignInHarness;
|
|
174
|
+
}): Promise<void> {
|
|
175
|
+
await this.deps.db.transaction().execute(async (trx) => {
|
|
176
|
+
const marked = await trx
|
|
177
|
+
.updateTable("machine_sign_ins")
|
|
178
|
+
.set({ signed_in_at: sql<Date>`now()` })
|
|
179
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
180
|
+
.where("attempt", "=", args.attempt)
|
|
181
|
+
.where("node_id", "=", args.nodeId)
|
|
182
|
+
.where("member", "=", args.member)
|
|
183
|
+
.where("harness", "=", args.harness)
|
|
184
|
+
.where("signed_in_at", "is", null)
|
|
185
|
+
.executeTakeFirst();
|
|
186
|
+
if (!marked.numUpdatedRows) return;
|
|
187
|
+
await trx
|
|
188
|
+
.deleteFrom("machine_sign_ins")
|
|
189
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
190
|
+
.where("node_id", "=", args.nodeId)
|
|
191
|
+
.where("member", "=", args.member)
|
|
192
|
+
.where("harness", "=", args.harness)
|
|
193
|
+
.where("attempt", "!=", args.attempt)
|
|
194
|
+
.where("signed_in_at", "is not", null)
|
|
195
|
+
.execute();
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The member signed out of a harness on a machine. */
|
|
200
|
+
async signedOut(args: {
|
|
201
|
+
nodeId: string;
|
|
202
|
+
member: string;
|
|
203
|
+
harness: SignInHarness;
|
|
204
|
+
}): Promise<void> {
|
|
205
|
+
await this.deps.db
|
|
206
|
+
.deleteFrom("machine_sign_ins")
|
|
207
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
208
|
+
.where("node_id", "=", args.nodeId)
|
|
209
|
+
.where("member", "=", args.member)
|
|
210
|
+
.where("harness", "=", args.harness)
|
|
211
|
+
.execute();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** How each of a member's completed sign-ins was made, by node id. */
|
|
215
|
+
async made(args: {
|
|
216
|
+
member: string;
|
|
217
|
+
}): Promise<Map<string, Partial<Record<SignInHarness, SignInMade>>>> {
|
|
218
|
+
const rows = await this.deps.db
|
|
219
|
+
.selectFrom("machine_sign_ins")
|
|
220
|
+
.select(["node_id", "harness", "project_id", "environment_name", "image"])
|
|
221
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
222
|
+
.where("member", "=", args.member)
|
|
223
|
+
.where("signed_in_at", "is not", null)
|
|
224
|
+
.orderBy("signed_in_at")
|
|
225
|
+
.execute();
|
|
226
|
+
const made = new Map<string, Partial<Record<SignInHarness, SignInMade>>>();
|
|
227
|
+
for (const row of rows) {
|
|
228
|
+
const harness = SIGN_IN_HARNESSES.find((name) => name === row.harness);
|
|
229
|
+
if (!harness) continue;
|
|
230
|
+
const entry = made.get(row.node_id) ?? {};
|
|
231
|
+
// Ordered by completion: the newest is the home in place.
|
|
232
|
+
entry[harness] = {
|
|
233
|
+
project: row.project_id,
|
|
234
|
+
environment: row.environment_name,
|
|
235
|
+
image: row.image,
|
|
236
|
+
};
|
|
237
|
+
made.set(row.node_id, entry);
|
|
238
|
+
}
|
|
239
|
+
return made;
|
|
240
|
+
}
|
|
241
|
+
}
|