@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,12 +1,15 @@
|
|
|
1
1
|
import { createHash, createHmac, randomBytes, randomUUID } from "node:crypto";
|
|
2
|
+
import { ACTIVE_TURN_STATUSES } from "@catamorphic/agent-protocol";
|
|
2
3
|
import {
|
|
3
4
|
isProjectPrincipal,
|
|
5
|
+
PLACEMENT_FREE_JOB_KINDS,
|
|
4
6
|
PLACEMENT_WAIT_FRESH_SECONDS,
|
|
7
|
+
WORKSPACE_IN_USE_SECONDS,
|
|
5
8
|
type WorkerNode,
|
|
6
9
|
type WorkerNodesService,
|
|
7
10
|
} from "@catamorphic/core";
|
|
8
11
|
import type { DB } from "@catamorphic/db";
|
|
9
|
-
import { parseSignInCapability } from "@catamorphic/sandbox";
|
|
12
|
+
import { accessTier, parseSignInCapability } from "@catamorphic/sandbox";
|
|
10
13
|
import {
|
|
11
14
|
type Kysely,
|
|
12
15
|
type Selectable,
|
|
@@ -18,6 +21,7 @@ import { z } from "zod";
|
|
|
18
21
|
import {
|
|
19
22
|
DEFAULT_LOST_AFTER_MINUTES,
|
|
20
23
|
type MachineFacts,
|
|
24
|
+
type MachinePreparation,
|
|
21
25
|
type MachinePrincipal,
|
|
22
26
|
type MachineState,
|
|
23
27
|
type MachineVerb,
|
|
@@ -32,10 +36,12 @@ import {
|
|
|
32
36
|
type PlannedMachine,
|
|
33
37
|
} from "../machines/sequencer.js";
|
|
34
38
|
import {
|
|
39
|
+
nodeAccess,
|
|
35
40
|
type ScheduledPlacement,
|
|
36
41
|
storedPlacement,
|
|
37
42
|
type WorkerPlacement,
|
|
38
43
|
} from "./placement.js";
|
|
44
|
+
import { SignInStore } from "./sign-in-store.js";
|
|
39
45
|
|
|
40
46
|
export const WORKER_NODE_PREFIX = "worker.";
|
|
41
47
|
|
|
@@ -63,6 +69,7 @@ const VERBS: readonly MachineVerb[] = [
|
|
|
63
69
|
"stop",
|
|
64
70
|
"start",
|
|
65
71
|
"describe",
|
|
72
|
+
"onConnected",
|
|
66
73
|
];
|
|
67
74
|
|
|
68
75
|
const OptionsSchema = z.record(z.string(), z.unknown());
|
|
@@ -101,7 +108,12 @@ export class MachineStore {
|
|
|
101
108
|
/** Minutes unseen that make a machine lost when its plan gives none. */
|
|
102
109
|
lostMinutes?: number;
|
|
103
110
|
},
|
|
104
|
-
) {
|
|
111
|
+
) {
|
|
112
|
+
this.signIns = new SignInStore(deps);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Members' sign-ins waiting for their machine, and how each was made (ADR 0363). */
|
|
116
|
+
readonly signIns: SignInStore;
|
|
105
117
|
|
|
106
118
|
get defaultLostMinutes(): number {
|
|
107
119
|
return this.deps.lostMinutes ?? DEFAULT_LOST_AFTER_MINUTES;
|
|
@@ -165,6 +177,8 @@ export class MachineStore {
|
|
|
165
177
|
idempotency_key: key,
|
|
166
178
|
labels: JSON.stringify(machine.labels),
|
|
167
179
|
serves: machine.for === null ? null : JSON.stringify(machine.for),
|
|
180
|
+
spare: machine.spare,
|
|
181
|
+
project_id: machine.project,
|
|
168
182
|
trusted: machine.trusted,
|
|
169
183
|
options: JSON.stringify(machine.options),
|
|
170
184
|
lost_after_minutes: machine.lostAfterMinutes,
|
|
@@ -318,15 +332,101 @@ export class MachineStore {
|
|
|
318
332
|
trusted: boolean;
|
|
319
333
|
reason: string | null;
|
|
320
334
|
lostAfterMinutes: number | null;
|
|
335
|
+
/** Left out: it stays what it is. */
|
|
336
|
+
spare?: boolean;
|
|
337
|
+
/** Left out: it stays what it is. */
|
|
338
|
+
project?: string | null;
|
|
321
339
|
}): Promise<void> {
|
|
322
340
|
await this.updatePlacement(args.name, {
|
|
323
341
|
labels: JSON.stringify(args.labels),
|
|
324
342
|
trusted: args.trusted,
|
|
325
343
|
reason: args.reason,
|
|
326
344
|
lost_after_minutes: args.lostAfterMinutes,
|
|
345
|
+
...(args.spare !== undefined ? { spare: args.spare } : {}),
|
|
346
|
+
...(args.project !== undefined ? { project_id: args.project } : {}),
|
|
327
347
|
});
|
|
328
348
|
}
|
|
329
349
|
|
|
350
|
+
/**
|
|
351
|
+
* Hand a spare to the chat key being placed on it (ADR 0360), in the
|
|
352
|
+
* transaction that reserves the chat's capacity, which holds the worker
|
|
353
|
+
* node's row lock: whom the machine is for narrows to that key, and its
|
|
354
|
+
* admission revision changes, so a second key admitted against the spare
|
|
355
|
+
* at the same moment is refused when its own Allocation is made and is
|
|
356
|
+
* placed again elsewhere. Nothing is reset: the key could already place
|
|
357
|
+
* work there. Work with no key (a cache fill, a preparation) leaves a
|
|
358
|
+
* spare as it is.
|
|
359
|
+
*/
|
|
360
|
+
async claimSpare(args: {
|
|
361
|
+
transaction: Transaction<DB>;
|
|
362
|
+
nodeId: string;
|
|
363
|
+
projectId: string;
|
|
364
|
+
chatKey: string | undefined;
|
|
365
|
+
}): Promise<boolean> {
|
|
366
|
+
if (!args.chatKey || !args.nodeId.startsWith(WORKER_NODE_PREFIX))
|
|
367
|
+
return false;
|
|
368
|
+
const name = args.nodeId.slice(WORKER_NODE_PREFIX.length);
|
|
369
|
+
const claimed = await args.transaction
|
|
370
|
+
.updateTable("work_machines")
|
|
371
|
+
.set({
|
|
372
|
+
serves: JSON.stringify([
|
|
373
|
+
{ project: args.projectId, key: args.chatKey },
|
|
374
|
+
]),
|
|
375
|
+
spare: false,
|
|
376
|
+
updated_at: sql`now()`,
|
|
377
|
+
})
|
|
378
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
379
|
+
.where("name", "=", name)
|
|
380
|
+
.where("spare", "=", true)
|
|
381
|
+
// Only its own project's spare, whatever admission let through.
|
|
382
|
+
.where(
|
|
383
|
+
"serves",
|
|
384
|
+
"=",
|
|
385
|
+
sql<string>`${JSON.stringify([{ project: args.projectId }])}::jsonb`,
|
|
386
|
+
)
|
|
387
|
+
.returning("name")
|
|
388
|
+
.executeTakeFirst();
|
|
389
|
+
if (!claimed) return false;
|
|
390
|
+
await args.transaction
|
|
391
|
+
.updateTable("worker_nodes")
|
|
392
|
+
.set({ admission_revision: randomUUID() })
|
|
393
|
+
.where("id", "=", args.nodeId)
|
|
394
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
395
|
+
.where("admission_revision", "is not", null)
|
|
396
|
+
.execute();
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Whether a machine takes a project's own trusted work that has no chat
|
|
402
|
+
* key (a cache fill, a preparation), as placement decides it (ADR 0360),
|
|
403
|
+
* online or not: one for anyone, for that project, or for one of its chat
|
|
404
|
+
* keys. A node that is no worker is a server's own, open to anyone.
|
|
405
|
+
*/
|
|
406
|
+
async admitsProjectWork(args: {
|
|
407
|
+
nodeId: string;
|
|
408
|
+
projectId: string;
|
|
409
|
+
}): Promise<boolean> {
|
|
410
|
+
if (!args.nodeId.startsWith(WORKER_NODE_PREFIX)) return true;
|
|
411
|
+
const row = await this.row({
|
|
412
|
+
name: args.nodeId.slice(WORKER_NODE_PREFIX.length),
|
|
413
|
+
});
|
|
414
|
+
if (!row) return false;
|
|
415
|
+
return (
|
|
416
|
+
accessTier({
|
|
417
|
+
access: nodeAccess(storedPlacement(row).for),
|
|
418
|
+
owner: undefined,
|
|
419
|
+
projectId: args.projectId,
|
|
420
|
+
fill: true,
|
|
421
|
+
}) !== undefined
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** The host's `onConnected` heard this connection. */
|
|
426
|
+
async heardConnected(args: { name: string }): Promise<void> {
|
|
427
|
+
await this.update(args.name, { connect_pending: false });
|
|
428
|
+
}
|
|
429
|
+
|
|
330
430
|
/** Whom a machine is for from now on; ends a reset no longer wanted. */
|
|
331
431
|
async setFor(args: {
|
|
332
432
|
name: string;
|
|
@@ -390,7 +490,13 @@ export class MachineStore {
|
|
|
390
490
|
}
|
|
391
491
|
: {}),
|
|
392
492
|
...(args.lost ? { lost_at: sql<Date>`coalesce(lost_at, now())` } : {}),
|
|
393
|
-
|
|
493
|
+
// Its worker connected: the host's `onConnected` is owed a call.
|
|
494
|
+
...(args.phase === "running"
|
|
495
|
+
? {
|
|
496
|
+
lost_at: null,
|
|
497
|
+
connect_pending: sql<boolean>`CASE WHEN phase IN ('starting', 'resuming') THEN true ELSE connect_pending END`,
|
|
498
|
+
}
|
|
499
|
+
: {}),
|
|
394
500
|
})
|
|
395
501
|
.where("tenant_id", "=", this.deps.tenantId)
|
|
396
502
|
.where("name", "=", args.name)
|
|
@@ -429,6 +535,11 @@ export class MachineStore {
|
|
|
429
535
|
.where("tenant_id", "=", this.deps.tenantId)
|
|
430
536
|
.where("name", "=", args.name)
|
|
431
537
|
.where("used_at", "is", null)
|
|
538
|
+
.execute(); // Nothing prepared there is of use to anyone now (ADR 0360).
|
|
539
|
+
await db
|
|
540
|
+
.deleteFrom("machine_preparations")
|
|
541
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
542
|
+
.where("worker_node_id", "=", `${WORKER_NODE_PREFIX}${args.name}`)
|
|
432
543
|
.execute();
|
|
433
544
|
}
|
|
434
545
|
|
|
@@ -576,15 +687,32 @@ export class MachineStore {
|
|
|
576
687
|
"machine.phase",
|
|
577
688
|
"machine.dropped_at",
|
|
578
689
|
"machine.reset_pending",
|
|
690
|
+
"machine.spare",
|
|
579
691
|
"node.admission_revision",
|
|
580
692
|
])
|
|
581
693
|
.where("machine.tenant_id", "=", this.deps.tenantId)
|
|
582
694
|
.execute();
|
|
695
|
+
const prepared = await this.deps.db
|
|
696
|
+
.selectFrom("machine_preparations")
|
|
697
|
+
.select(["worker_node_id", "project_id", "environment_name"])
|
|
698
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
699
|
+
.where("status", "=", "succeeded")
|
|
700
|
+
.execute();
|
|
583
701
|
return new Map(
|
|
584
702
|
rows.map((row) => [
|
|
585
703
|
`${WORKER_NODE_PREFIX}${row.name}`,
|
|
586
704
|
{
|
|
587
705
|
...storedPlacement(row),
|
|
706
|
+
spare: row.spare,
|
|
707
|
+
prepared: prepared
|
|
708
|
+
.filter(
|
|
709
|
+
(entry) =>
|
|
710
|
+
entry.worker_node_id === `${WORKER_NODE_PREFIX}${row.name}`,
|
|
711
|
+
)
|
|
712
|
+
.map((entry) => ({
|
|
713
|
+
project: entry.project_id,
|
|
714
|
+
environment: entry.environment_name,
|
|
715
|
+
})),
|
|
588
716
|
admissionRevision: row.admission_revision,
|
|
589
717
|
accepting:
|
|
590
718
|
row.phase === "running" &&
|
|
@@ -603,51 +731,118 @@ export class MachineStore {
|
|
|
603
731
|
*/
|
|
604
732
|
async records(): Promise<MachineRecord[]> {
|
|
605
733
|
const { db, tenantId } = this.deps;
|
|
606
|
-
const [rows, workers, nodes, holdings, loads, clock] =
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
join
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
734
|
+
const [rows, workers, nodes, holdings, loads, clock, preparations] =
|
|
735
|
+
await Promise.all([
|
|
736
|
+
db
|
|
737
|
+
.selectFrom("work_machines")
|
|
738
|
+
.selectAll()
|
|
739
|
+
.where("tenant_id", "=", tenantId)
|
|
740
|
+
.orderBy("name")
|
|
741
|
+
.execute(),
|
|
742
|
+
db
|
|
743
|
+
.selectFrom("work_workers")
|
|
744
|
+
.select(["name", "enrolled_at", "last_seen_at", "revoked_at"])
|
|
745
|
+
.where("tenant_id", "=", tenantId)
|
|
746
|
+
.execute(),
|
|
747
|
+
this.deps.nodes.list({ tenantId, authorityId: this.deps.authorityId }),
|
|
748
|
+
db
|
|
749
|
+
.selectFrom("execution_allocations as allocation")
|
|
750
|
+
.leftJoin("agent_sessions as session", (join) =>
|
|
751
|
+
join
|
|
752
|
+
.onRef("session.id", "=", "allocation.root_workload_id")
|
|
753
|
+
.on("allocation.workload_kind", "=", "agent"),
|
|
754
|
+
)
|
|
755
|
+
.select([
|
|
756
|
+
"allocation.worker_node_id",
|
|
757
|
+
"allocation.project_id",
|
|
758
|
+
"allocation.status",
|
|
759
|
+
"allocation.workload_kind",
|
|
760
|
+
"allocation.created_at",
|
|
761
|
+
"allocation.sandbox_creation_started",
|
|
762
|
+
"allocation.sandbox_provider_id",
|
|
763
|
+
"session.external_user_id",
|
|
764
|
+
"session.chat_key",
|
|
765
|
+
])
|
|
766
|
+
// What keeps a workspace in use (ADR 0361): a turn queued, held
|
|
767
|
+
// or at work in any session working there, and its latest turn
|
|
768
|
+
// and use, across them all (ADR 0322).
|
|
769
|
+
.select((eb) => [
|
|
770
|
+
eb
|
|
771
|
+
.exists(
|
|
772
|
+
eb
|
|
773
|
+
.selectFrom("agent_turns")
|
|
774
|
+
.innerJoin(
|
|
775
|
+
"agent_sessions as sharer",
|
|
776
|
+
"sharer.id",
|
|
777
|
+
"agent_turns.session_id",
|
|
778
|
+
)
|
|
779
|
+
.select("agent_turns.id")
|
|
780
|
+
.whereRef("sharer.allocation_id", "=", "allocation.id")
|
|
781
|
+
.where("agent_turns.status", "in", [
|
|
782
|
+
"queued",
|
|
783
|
+
"held",
|
|
784
|
+
...ACTIVE_TURN_STATUSES,
|
|
785
|
+
]),
|
|
786
|
+
)
|
|
787
|
+
.as("turning"),
|
|
788
|
+
eb
|
|
789
|
+
.selectFrom("agent_turns")
|
|
790
|
+
.innerJoin(
|
|
791
|
+
"agent_sessions as sharer",
|
|
792
|
+
"sharer.id",
|
|
793
|
+
"agent_turns.session_id",
|
|
794
|
+
)
|
|
795
|
+
.select((turn) => turn.fn.max("agent_turns.updated_at").as("at"))
|
|
796
|
+
.whereRef("sharer.allocation_id", "=", "allocation.id")
|
|
797
|
+
.as("last_turn_at"),
|
|
798
|
+
eb
|
|
799
|
+
.selectFrom("session_workspace_use")
|
|
800
|
+
.innerJoin(
|
|
801
|
+
"agent_sessions as user",
|
|
802
|
+
"user.id",
|
|
803
|
+
"session_workspace_use.session_id",
|
|
804
|
+
)
|
|
805
|
+
.select((use) =>
|
|
806
|
+
use.fn.max("session_workspace_use.used_at").as("at"),
|
|
807
|
+
)
|
|
808
|
+
.whereRef("user.allocation_id", "=", "allocation.id")
|
|
809
|
+
.as("last_used_at"),
|
|
810
|
+
])
|
|
811
|
+
.where("allocation.tenant_id", "=", tenantId)
|
|
812
|
+
.where("allocation.status", "in", ["active", "stopped"])
|
|
813
|
+
.where("allocation.worker_node_id", "like", `${WORKER_NODE_PREFIX}%`)
|
|
814
|
+
.execute(),
|
|
815
|
+
db
|
|
816
|
+
.selectFrom("execution_allocations")
|
|
817
|
+
.select([
|
|
818
|
+
"worker_node_id",
|
|
819
|
+
sql<string>`count(*) FILTER (WHERE capacity_released_at IS NULL)`.as(
|
|
820
|
+
"holding",
|
|
821
|
+
),
|
|
822
|
+
sql<Date | null>`max(capacity_released_at)`.as("last_released"),
|
|
823
|
+
])
|
|
824
|
+
.where("tenant_id", "=", tenantId)
|
|
825
|
+
.where("worker_node_id", "like", `${WORKER_NODE_PREFIX}%`)
|
|
826
|
+
.groupBy("worker_node_id")
|
|
827
|
+
.execute(),
|
|
828
|
+
sql<{ now: Date }>`SELECT now() AS now`.execute(db),
|
|
829
|
+
db
|
|
830
|
+
.selectFrom("machine_preparations")
|
|
831
|
+
.select([
|
|
832
|
+
"worker_node_id",
|
|
833
|
+
"project_id",
|
|
834
|
+
"environment_name",
|
|
835
|
+
"status",
|
|
836
|
+
"commit_sha",
|
|
837
|
+
"error",
|
|
838
|
+
"queued_at",
|
|
839
|
+
"finished_at",
|
|
840
|
+
])
|
|
841
|
+
.where("tenant_id", "=", tenantId)
|
|
842
|
+
.orderBy("project_id")
|
|
843
|
+
.orderBy("environment_name")
|
|
844
|
+
.execute(),
|
|
845
|
+
]);
|
|
651
846
|
const now = clock.rows[0]?.now ?? new Date();
|
|
652
847
|
const workerOf = new Map(workers.map((worker) => [worker.name, worker]));
|
|
653
848
|
const nodeOf = new Map(nodes.map((node) => [node.id, node]));
|
|
@@ -657,7 +852,30 @@ export class MachineStore {
|
|
|
657
852
|
const worker = workerOf.get(row.name);
|
|
658
853
|
const node = nodeOf.get(nodeId);
|
|
659
854
|
const load = loadOf.get(nodeId);
|
|
660
|
-
const
|
|
855
|
+
const kept = holdings.filter((entry) => entry.worker_node_id === nodeId);
|
|
856
|
+
const held = kept.filter((entry) => entry.status === "active");
|
|
857
|
+
const stopped = kept.length - held.length;
|
|
858
|
+
// Quiet (ADR 0361): nothing waits to be given back, no preparation
|
|
859
|
+
// runs, and every live workspace is a chat's with no turn and nobody
|
|
860
|
+
// in it now.
|
|
861
|
+
const quiet =
|
|
862
|
+
Number(load?.holding ?? 0) === kept.length &&
|
|
863
|
+
!preparations.some(
|
|
864
|
+
(entry) =>
|
|
865
|
+
entry.worker_node_id === nodeId &&
|
|
866
|
+
(entry.status === "queued" || entry.status === "running"),
|
|
867
|
+
) &&
|
|
868
|
+
held.every(
|
|
869
|
+
(entry) =>
|
|
870
|
+
entry.workload_kind === "agent" &&
|
|
871
|
+
!entry.turning &&
|
|
872
|
+
!usedWithin({
|
|
873
|
+
at: dateOf(entry.last_used_at),
|
|
874
|
+
now,
|
|
875
|
+
seconds: WORKSPACE_IN_USE_SECONDS,
|
|
876
|
+
}) &&
|
|
877
|
+
!(entry.sandbox_creation_started && !entry.sandbox_provider_id),
|
|
878
|
+
);
|
|
661
879
|
const phase = phaseOf(row.phase);
|
|
662
880
|
const seen =
|
|
663
881
|
worker?.last_seen_at ?? worker?.enrolled_at ?? row.phase_since;
|
|
@@ -686,11 +904,31 @@ export class MachineStore {
|
|
|
686
904
|
since: sinceOf({ row, state, seen }).toISOString(),
|
|
687
905
|
labels: placement.labels,
|
|
688
906
|
for: placement.for,
|
|
907
|
+
spare: row.spare,
|
|
908
|
+
project: row.project_id,
|
|
909
|
+
preparations: preparations
|
|
910
|
+
.filter((entry) => entry.worker_node_id === nodeId)
|
|
911
|
+
.map(
|
|
912
|
+
(entry): MachinePreparation => ({
|
|
913
|
+
project: entry.project_id,
|
|
914
|
+
environment: entry.environment_name,
|
|
915
|
+
status:
|
|
916
|
+
entry.status === "succeeded"
|
|
917
|
+
? "prepared"
|
|
918
|
+
: entry.status === "failed"
|
|
919
|
+
? "failed"
|
|
920
|
+
: "preparing",
|
|
921
|
+
commit: entry.commit_sha,
|
|
922
|
+
error: entry.error,
|
|
923
|
+
since: (entry.finished_at ?? entry.queued_at).toISOString(),
|
|
924
|
+
}),
|
|
925
|
+
),
|
|
689
926
|
trusted: placement.trusted,
|
|
690
927
|
options: OptionsSchema.parse(row.options),
|
|
691
928
|
reason: row.reason,
|
|
692
929
|
holding: {
|
|
693
930
|
workspaces: held.length,
|
|
931
|
+
stopped,
|
|
694
932
|
for: normalizePrincipals(
|
|
695
933
|
held.map((entry) =>
|
|
696
934
|
entry.external_user_id &&
|
|
@@ -702,13 +940,17 @@ export class MachineStore {
|
|
|
702
940
|
),
|
|
703
941
|
),
|
|
704
942
|
},
|
|
705
|
-
|
|
706
|
-
(state === "serving" || state === "draining") &&
|
|
707
|
-
Number(load?.holding ?? 0) === 0
|
|
943
|
+
quietSince:
|
|
944
|
+
(state === "serving" || state === "draining") && quiet
|
|
708
945
|
? latest([
|
|
709
946
|
row.phase_since,
|
|
710
947
|
load?.last_released ?? null,
|
|
711
948
|
worker?.enrolled_at ?? null,
|
|
949
|
+
...held.flatMap((entry) => [
|
|
950
|
+
entry.created_at,
|
|
951
|
+
dateOf(entry.last_turn_at),
|
|
952
|
+
dateOf(entry.last_used_at),
|
|
953
|
+
]),
|
|
712
954
|
]).toISOString()
|
|
713
955
|
: null,
|
|
714
956
|
lastSeenAt: worker?.last_seen_at?.toISOString() ?? null,
|
|
@@ -746,6 +988,7 @@ export class MachineStore {
|
|
|
746
988
|
destroyRequestedAt: row.destroy_requested_at?.toISOString() ?? null,
|
|
747
989
|
lostAfterMinutes: row.lost_after_minutes,
|
|
748
990
|
revoked: Boolean(worker?.revoked_at),
|
|
991
|
+
connectedPending: row.connect_pending,
|
|
749
992
|
};
|
|
750
993
|
});
|
|
751
994
|
}
|
|
@@ -782,6 +1025,7 @@ export class MachineStore {
|
|
|
782
1025
|
"placement_waits.requirements",
|
|
783
1026
|
"placement_waits.reason",
|
|
784
1027
|
"placement_waits.waiting_since",
|
|
1028
|
+
"placement_waits.worker_node_id",
|
|
785
1029
|
"work_accounts.disabled_at",
|
|
786
1030
|
sql<boolean>`EXISTS (SELECT 1 FROM environment_cache_fills WHERE id = placement_waits.root_workload_id)`.as(
|
|
787
1031
|
"fill",
|
|
@@ -812,10 +1056,15 @@ export class MachineStore {
|
|
|
812
1056
|
pool: pool.data,
|
|
813
1057
|
requirements: requirements.data,
|
|
814
1058
|
reason: row.reason === "unavailable" ? "unavailable" : "capacity",
|
|
1059
|
+
// Only the machine keeping its stopped workspace takes it (ADR 0361).
|
|
1060
|
+
machine: row.worker_node_id?.startsWith(WORKER_NODE_PREFIX)
|
|
1061
|
+
? row.worker_node_id.slice(WORKER_NODE_PREFIX.length)
|
|
1062
|
+
: null,
|
|
815
1063
|
waitingSince: row.waiting_since.toISOString(),
|
|
816
1064
|
});
|
|
817
1065
|
}
|
|
818
|
-
|
|
1066
|
+
// A sign-in begun on a stopped machine waits for that machine (ADR 0363).
|
|
1067
|
+
return [...work, ...(await this.signIns.waiting())];
|
|
819
1068
|
}
|
|
820
1069
|
|
|
821
1070
|
/**
|
|
@@ -840,7 +1089,7 @@ export class MachineStore {
|
|
|
840
1089
|
eb
|
|
841
1090
|
.selectFrom("execution_allocations")
|
|
842
1091
|
.select(
|
|
843
|
-
sql<string>`count(*) FILTER (WHERE capacity_released_at IS NULL)::text || '/' || coalesce(max(capacity_released_at)::text, '')`.as(
|
|
1092
|
+
sql<string>`count(*) FILTER (WHERE capacity_released_at IS NULL)::text || '/' || count(*) FILTER (WHERE status = 'stopped')::text || '/' || coalesce(max(capacity_released_at)::text, '')`.as(
|
|
844
1093
|
"work",
|
|
845
1094
|
),
|
|
846
1095
|
)
|
|
@@ -850,12 +1099,22 @@ export class MachineStore {
|
|
|
850
1099
|
eb
|
|
851
1100
|
.selectFrom("work_machines")
|
|
852
1101
|
.select(
|
|
853
|
-
sql<string>`coalesce(string_agg(name || ':' || phase || ':' || (dropped_at IS NOT NULL)::text || ':' || (disabled_at IS NOT NULL)::text, ',' ORDER BY name), '')`.as(
|
|
1102
|
+
sql<string>`coalesce(string_agg(name || ':' || phase || ':' || (dropped_at IS NOT NULL)::text || ':' || (disabled_at IS NOT NULL)::text || ':' || spare::text || ':' || connect_pending::text, ',' ORDER BY name), '')`.as(
|
|
854
1103
|
"machines",
|
|
855
1104
|
),
|
|
856
1105
|
)
|
|
857
1106
|
.where("tenant_id", "=", this.deps.tenantId)
|
|
858
1107
|
.as("machines"),
|
|
1108
|
+
// A machine became prepared, or its preparation failed (ADR 0360).
|
|
1109
|
+
eb
|
|
1110
|
+
.selectFrom("machine_preparations")
|
|
1111
|
+
.select(
|
|
1112
|
+
sql<string>`coalesce(string_agg(id::text || ':' || status, ',' ORDER BY id), '')`.as(
|
|
1113
|
+
"preparations",
|
|
1114
|
+
),
|
|
1115
|
+
)
|
|
1116
|
+
.where("tenant_id", "=", this.deps.tenantId)
|
|
1117
|
+
.as("preparations"),
|
|
859
1118
|
eb
|
|
860
1119
|
.selectFrom("work_accounts")
|
|
861
1120
|
.select(
|
|
@@ -872,6 +1131,7 @@ export class MachineStore {
|
|
|
872
1131
|
),
|
|
873
1132
|
)
|
|
874
1133
|
.as("roles"),
|
|
1134
|
+
this.signIns.signature().as("signIns"),
|
|
875
1135
|
])
|
|
876
1136
|
.executeTakeFirst();
|
|
877
1137
|
const reach = [...(await this.reach())]
|
|
@@ -881,8 +1141,10 @@ export class MachineStore {
|
|
|
881
1141
|
row?.waits ?? "",
|
|
882
1142
|
row?.work ?? "",
|
|
883
1143
|
row?.machines ?? "",
|
|
1144
|
+
row?.preparations ?? "",
|
|
884
1145
|
row?.accounts ?? "",
|
|
885
1146
|
row?.roles ?? "",
|
|
1147
|
+
row?.signIns ?? "",
|
|
886
1148
|
reach,
|
|
887
1149
|
].join("|");
|
|
888
1150
|
}
|
|
@@ -973,6 +1235,23 @@ function principalsJson(
|
|
|
973
1235
|
: JSON.stringify(normalizePrincipals(principals));
|
|
974
1236
|
}
|
|
975
1237
|
|
|
1238
|
+
/** A timestamp a subquery answered: a Date from Postgres, or a string from PGlite. */
|
|
1239
|
+
function dateOf(value: Date | string | null): Date | null {
|
|
1240
|
+
return value === null ? null : new Date(value);
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
/** Whether a person used a workspace within `seconds` of `now`. */
|
|
1244
|
+
function usedWithin(args: {
|
|
1245
|
+
at: Date | null;
|
|
1246
|
+
now: Date;
|
|
1247
|
+
seconds: number;
|
|
1248
|
+
}): boolean {
|
|
1249
|
+
return (
|
|
1250
|
+
args.at !== null &&
|
|
1251
|
+
args.now.getTime() - args.at.getTime() < args.seconds * 1000
|
|
1252
|
+
);
|
|
1253
|
+
}
|
|
1254
|
+
|
|
976
1255
|
function latest(dates: readonly (Date | null)[]): Date {
|
|
977
1256
|
return dates.reduce<Date>(
|
|
978
1257
|
(best, date) => (date && date > best ? date : best),
|
|
@@ -1031,7 +1310,8 @@ function waitFresh() {
|
|
|
1031
1310
|
/**
|
|
1032
1311
|
* Chats and runs reuse placement demand, and a cache fill that waits for a
|
|
1033
1312
|
* machine with room for its size (ADR 0336); waiting journals alone are no
|
|
1034
|
-
* demand
|
|
1313
|
+
* demand, and neither is a job that needs no placement (a pause timeout, a
|
|
1314
|
+
* host call's follower): any replica runs it without a machine.
|
|
1035
1315
|
*/
|
|
1036
1316
|
function queuedPlacementWait(db: Kysely<DB>) {
|
|
1037
1317
|
const turns = db
|
|
@@ -1052,14 +1332,17 @@ function queuedPlacementWait(db: Kysely<DB>) {
|
|
|
1052
1332
|
.where("root.status", "in", ["pending", "running", "waiting"])
|
|
1053
1333
|
.where("run.status", "in", ["pending", "running", "waiting"])
|
|
1054
1334
|
.where("job.status", "=", "pending")
|
|
1055
|
-
.where("job.kind", "
|
|
1335
|
+
.where("job.kind", "not in", [...PLACEMENT_FREE_JOB_KINDS])
|
|
1056
1336
|
.where("job.available_at", "<=", sql<Date>`now()`);
|
|
1057
1337
|
const fills = db
|
|
1058
1338
|
.selectFrom("environment_cache_fills")
|
|
1059
1339
|
.select("id")
|
|
1060
1340
|
.where("id", "=", sql<string>`placement_waits.root_workload_id`)
|
|
1061
1341
|
.where("status", "=", "queued");
|
|
1062
|
-
|
|
1342
|
+
// Work for a stopped workspace (ADR 0361) is demand while it is tried:
|
|
1343
|
+
// a turn, or a person at a terminal or preview asking again.
|
|
1344
|
+
return sql<boolean>`(placement_waits.worker_node_id IS NOT NULL OR
|
|
1345
|
+
(placement_waits.workload_kind = 'agent' AND EXISTS (${turns})) OR
|
|
1063
1346
|
(placement_waits.workload_kind = 'workflow' AND EXISTS (${jobs})) OR
|
|
1064
1347
|
EXISTS (${fills}))`;
|
|
1065
1348
|
}
|