@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.
Files changed (74) hide show
  1. package/README.md +4 -1
  2. package/dist/agents.d.ts.map +1 -1
  3. package/dist/{chunk-kcck20aw.js → chunk-ynxz0am9.js} +80 -9
  4. package/dist/chunk-ynxz0am9.js.map +11 -0
  5. package/dist/cluster.d.ts +19 -1
  6. package/dist/cluster.d.ts.map +1 -1
  7. package/dist/execution-config.d.ts +2 -1
  8. package/dist/execution-config.d.ts.map +1 -1
  9. package/dist/index.d.ts +2 -2
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +962 -268
  12. package/dist/index.js.map +21 -19
  13. package/dist/machines/contract.d.ts +138 -14
  14. package/dist/machines/contract.d.ts.map +1 -1
  15. package/dist/machines/sequencer.d.ts +33 -6
  16. package/dist/machines/sequencer.d.ts.map +1 -1
  17. package/dist/machines/simulation.d.ts +64 -0
  18. package/dist/machines/simulation.d.ts.map +1 -1
  19. package/dist/machines/work-machines.d.ts +40 -2
  20. package/dist/machines/work-machines.d.ts.map +1 -1
  21. package/dist/server.d.ts +11 -1
  22. package/dist/server.d.ts.map +1 -1
  23. package/dist/testing.d.ts +1 -1
  24. package/dist/testing.d.ts.map +1 -1
  25. package/dist/testing.js +208 -17
  26. package/dist/testing.js.map +4 -4
  27. package/dist/workers/cache-archive-routes.d.ts +24 -0
  28. package/dist/workers/cache-archive-routes.d.ts.map +1 -0
  29. package/dist/workers/disk-care.d.ts +36 -1
  30. package/dist/workers/disk-care.d.ts.map +1 -1
  31. package/dist/workers/harness-sign-ins.d.ts +32 -1
  32. package/dist/workers/harness-sign-ins.d.ts.map +1 -1
  33. package/dist/workers/machine-store.d.ts +37 -0
  34. package/dist/workers/machine-store.d.ts.map +1 -1
  35. package/dist/workers/member-machine-routes.d.ts +3 -0
  36. package/dist/workers/member-machine-routes.d.ts.map +1 -1
  37. package/dist/workers/member-machines.d.ts +52 -5
  38. package/dist/workers/member-machines.d.ts.map +1 -1
  39. package/dist/workers/placement.d.ts +10 -0
  40. package/dist/workers/placement.d.ts.map +1 -1
  41. package/dist/workers/sign-in-store.d.ts +67 -0
  42. package/dist/workers/sign-in-store.d.ts.map +1 -0
  43. package/dist/workers/sign-ins.d.ts +4 -6
  44. package/dist/workers/sign-ins.d.ts.map +1 -1
  45. package/dist/workers/worker-protocol.d.ts +9 -1
  46. package/dist/workers/worker-protocol.d.ts.map +1 -1
  47. package/dist/workers/worker-registry.d.ts +20 -6
  48. package/dist/workers/worker-registry.d.ts.map +1 -1
  49. package/dist/workers/worker-runtime.d.ts.map +1 -1
  50. package/package.json +18 -19
  51. package/src/agents.ts +5 -0
  52. package/src/cluster.ts +62 -7
  53. package/src/execution-config.ts +2 -1
  54. package/src/index.ts +7 -0
  55. package/src/machines/contract-kit.ts +1 -1
  56. package/src/machines/contract.ts +191 -19
  57. package/src/machines/sequencer.ts +106 -11
  58. package/src/machines/simulation.ts +370 -20
  59. package/src/machines/work-machines.ts +71 -3
  60. package/src/server.ts +120 -29
  61. package/src/testing.ts +1 -0
  62. package/src/workers/cache-archive-routes.ts +125 -0
  63. package/src/workers/disk-care.ts +157 -6
  64. package/src/workers/harness-sign-ins.ts +207 -22
  65. package/src/workers/machine-store.ts +341 -58
  66. package/src/workers/member-machine-routes.ts +58 -3
  67. package/src/workers/member-machines.ts +168 -14
  68. package/src/workers/placement.ts +7 -0
  69. package/src/workers/sign-in-store.ts +241 -0
  70. package/src/workers/sign-ins.ts +9 -33
  71. package/src/workers/worker-protocol.ts +9 -1
  72. package/src/workers/worker-registry.ts +47 -6
  73. package/src/workers/worker-runtime.ts +9 -3
  74. package/dist/chunk-kcck20aw.js.map +0 -11
@@ -1,7 +1,8 @@
1
- import type { z } from "zod";
1
+ import { z } from "zod";
2
2
  import {
3
3
  DEFAULT_LOST_AFTER_MINUTES,
4
4
  type DesiredMachine,
5
+ handedOffKey,
5
6
  MachineCreateResultSchema,
6
7
  MachineDescribeResultSchema,
7
8
  MachineDestroyResultSchema,
@@ -9,6 +10,7 @@ import {
9
10
  type MachineEventKind,
10
11
  type MachineFacts,
11
12
  MachinePlanSchema,
13
+ type MachinePreparation,
12
14
  type MachinePrincipal,
13
15
  MachineStartResultSchema,
14
16
  type MachineState,
@@ -23,6 +25,7 @@ import {
23
25
  samePrincipals,
24
26
  servesNobody,
25
27
  servesOneOwner,
28
+ spareProject,
26
29
  type WaitingWork,
27
30
  } from "./contract.js";
28
31
 
@@ -72,12 +75,18 @@ export interface MachineRecord {
72
75
  lostAfterMinutes: number | null;
73
76
  /** An operator revoked its worker. */
74
77
  revoked: boolean;
78
+ /** Its worker connected, and the host's `onConnected` has not heard yet. */
79
+ connectedPending: boolean;
75
80
  }
76
81
 
77
82
  /** A desired machine with every default filled in. */
78
83
  export interface PlannedMachine {
79
84
  name: string;
80
85
  for: readonly MachinePrincipal[] | null;
86
+ /** A spare the first chat key placed on it takes (ADR 0360). */
87
+ spare: boolean;
88
+ /** The project that accounts for it (ADR 0364). */
89
+ project: string | null;
81
90
  labels: Readonly<Record<string, string>>;
82
91
  state: "running" | "stopped";
83
92
  options: Readonly<Record<string, unknown>>;
@@ -119,7 +128,21 @@ export interface MachineWorld {
119
128
  trusted: boolean;
120
129
  reason: string | null;
121
130
  lostAfterMinutes: number | null;
131
+ spare: boolean;
132
+ project: string | null;
122
133
  }): Promise<void>;
134
+ /**
135
+ * Prepare a machine for an Environment of a project (ADR 0360): join the
136
+ * preparation under way, start one, or start a failed one again.
137
+ */
138
+ prepare(args: {
139
+ name: string;
140
+ project: string;
141
+ environment: string;
142
+ commit?: string;
143
+ }): Promise<MachinePreparation>;
144
+ /** The host's `onConnected` heard this connection. */
145
+ heardConnected(args: { name: string }): Promise<void>;
123
146
  /** Whom it is for from now on; also ends a reset that is no longer wanted. */
124
147
  setFor(args: {
125
148
  name: string;
@@ -151,8 +174,9 @@ export interface MachineWorld {
151
174
  */
152
175
  declareLost(args: { name: string; unseenForMs: number }): Promise<boolean>;
153
176
  /**
154
- * Take no new work and become `stopping`, under its lock: only while it
155
- * holds nothing. False when it holds something.
177
+ * Stop its quiet workspaces, then take no new work and become
178
+ * `stopping`, under its lock (ADR 0361): only while every workspace it
179
+ * holds is stopped. False while something on it is in use.
156
180
  */
157
181
  beginStop(args: { name: string; at: string }): Promise<boolean>;
158
182
  stopped(args: { name: string }): Promise<void>;
@@ -281,6 +305,8 @@ export function plannedMachine(desired: DesiredMachine): PlannedMachine {
281
305
  return {
282
306
  name: desired.name,
283
307
  for: desired.for === undefined ? null : normalizePrincipals(desired.for),
308
+ spare: desired.spare ?? false,
309
+ project: desired.project ?? null,
284
310
  labels: desired.labels ?? {},
285
311
  state: desired.state ?? "running",
286
312
  options: desired.options ?? {},
@@ -374,6 +400,7 @@ export async function runMachinePass(args: {
374
400
  options: machine.options,
375
401
  labels: machine.labels,
376
402
  for: machine.for,
403
+ project: machine.project,
377
404
  reason: machine.reason,
378
405
  refused: null,
379
406
  failedBefore: false,
@@ -408,7 +435,7 @@ async function readPlan(args: {
408
435
  return {
409
436
  error: `plan returned something invalid${
410
437
  issue ? ` (${issue.path.join(".") || "result"}: ${issue.message})` : ""
411
- }; return an array of { name, for?, labels?, state?, options?, trusted?, lostAfterMinutes?, reason? }`,
438
+ }; return an array of { name, for?, spare?, labels?, state?, options?, trusted?, lostAfterMinutes?, reason? }`,
412
439
  };
413
440
  }
414
441
  return { machines: parsed.data.map((machine) => plannedMachine(machine)) };
@@ -515,6 +542,7 @@ async function stepManaged(
515
542
  options: record.facts.options,
516
543
  labels: record.facts.labels,
517
544
  for: record.facts.for,
545
+ project: record.facts.project,
518
546
  reason: record.facts.reason,
519
547
  refused: record.refused,
520
548
  failedBefore: record.facts.lastError?.verb === "create",
@@ -530,7 +558,7 @@ async function stepManaged(
530
558
  phase = "stopping";
531
559
  phaseSince = context.now;
532
560
  } else
533
- notice = `The plan wants it stopped. It stops once its ${workspaces(record)} given back.`;
561
+ notice = `The plan wants it stopped. It stops once its ${workspaces(record)} quiet: no turn, terminal, preview or setup in use.`;
534
562
  }
535
563
  }
536
564
  if (phase === "stopping") {
@@ -592,10 +620,43 @@ async function stepManaged(
592
620
  }
593
621
  }
594
622
  }
623
+ if (phase === "running") await connectedStep(context, record);
595
624
  await setNotice(context, record, notice ?? isolationNotice(record, wanted));
596
625
  return "kept";
597
626
  }
598
627
 
628
+ /**
629
+ * Tell the host's `onConnected` that a machine's worker connected (ADR
630
+ * 0360), once it serves, and again on later passes until it resolves.
631
+ */
632
+ async function connectedStep(
633
+ context: PassContext,
634
+ record: MachineRecord,
635
+ ): Promise<void> {
636
+ if (!record.connectedPending || record.facts.state !== "serving") return;
637
+ const { hook, world } = context;
638
+ const { name } = record.facts;
639
+ if (hook.onConnected) {
640
+ const heard = await callHook(context, {
641
+ name,
642
+ verb: "onConnected",
643
+ failedBefore: record.facts.lastError?.verb === "onConnected",
644
+ schema: z.literal(true),
645
+ expected: "nothing",
646
+ call: async () => {
647
+ await hook.onConnected?.({
648
+ machine: record.facts,
649
+ prepare: (args) => world.prepare({ name, ...args }),
650
+ });
651
+ return true;
652
+ },
653
+ });
654
+ if (!heard) return;
655
+ }
656
+ await context.fence();
657
+ await world.heardConnected({ name });
658
+ }
659
+
599
660
  /**
600
661
  * A machine an operator enrolled by hand: the plan may say whom it is for,
601
662
  * its labels and why, never create, stop or destroy it.
@@ -605,6 +666,7 @@ async function stepEnrolledByHand(
605
666
  record: MachineRecord,
606
667
  wanted: PlannedMachine | undefined,
607
668
  ): Promise<"kept"> {
669
+ await connectedStep(context, record);
608
670
  if (!wanted) return "kept";
609
671
  let notice = await applySettings(context, record, wanted);
610
672
  if (wanted.state === "stopped")
@@ -618,16 +680,32 @@ async function stepEnrolledByHand(
618
680
  async function applySettings(
619
681
  context: PassContext,
620
682
  record: MachineRecord,
621
- wanted: PlannedMachine,
683
+ planned: PlannedMachine,
622
684
  ): Promise<string | null> {
623
685
  const { world, summary } = context;
624
686
  const { facts } = record;
625
687
  const { name } = facts;
688
+ // A spare a chat key took since this plan read its facts (ADR 0360): it
689
+ // is that key's now, whatever the plan still says, and a plan that reads
690
+ // the new facts names it for its key. Never widened back: that would
691
+ // reset a machine a chat was just placed on.
692
+ const project = planned.spare ? spareProject(planned.for) : undefined;
693
+ const handedOff =
694
+ project !== undefined &&
695
+ !facts.spare &&
696
+ handedOffKey({ project, for: record.reset?.for ?? facts.for }) !==
697
+ undefined;
698
+ const wanted: PlannedMachine = handedOff
699
+ ? { ...planned, spare: false, for: record.reset?.for ?? facts.for }
700
+ : planned;
626
701
  let changed = false;
627
702
  if (
628
703
  canonical(facts.labels) !== canonical(wanted.labels) ||
629
704
  facts.trusted !== wanted.trusted ||
630
705
  facts.reason !== wanted.reason ||
706
+ facts.spare !== wanted.spare ||
707
+ // Names who accounts for it, not who may use it: no reset (ADR 0364).
708
+ facts.project !== wanted.project ||
631
709
  record.lostAfterMinutes !== wanted.lostAfterMinutes
632
710
  ) {
633
711
  await context.fence();
@@ -637,6 +715,8 @@ async function applySettings(
637
715
  trusted: wanted.trusted,
638
716
  reason: wanted.reason,
639
717
  lostAfterMinutes: wanted.lostAfterMinutes,
718
+ spare: wanted.spare,
719
+ project: wanted.project,
640
720
  });
641
721
  changed = true;
642
722
  }
@@ -678,6 +758,7 @@ async function createStep(
678
758
  options: Readonly<Record<string, unknown>>;
679
759
  labels: Readonly<Record<string, string>>;
680
760
  for: readonly MachinePrincipal[] | null;
761
+ project: string | null;
681
762
  reason: string | null;
682
763
  refused: MachineRecord["refused"];
683
764
  failedBefore: boolean;
@@ -744,6 +825,7 @@ async function createStep(
744
825
  managed: true,
745
826
  labels: machine.labels,
746
827
  for: machine.for,
828
+ project: machine.project,
747
829
  options: machine.options,
748
830
  reason: machine.reason,
749
831
  },
@@ -878,17 +960,29 @@ async function annotateWaits(args: {
878
960
 
879
961
  /**
880
962
  * Whether a machine would take a piece of waiting work once it serves: its
881
- * labels (and `plane: worker`) satisfy the work's pool, and it is for the
882
- * work's owner.
963
+ * labels (and `plane: worker`) satisfy the work's pool, it is for the
964
+ * work's owner, and it is the machine the work waits for by name, if any
965
+ * (its stopped workspace is there, ADR 0361, or a member's sign-in was
966
+ * begun on it, ADR 0363).
883
967
  */
884
968
  export function machineTakes(
885
- machine: Pick<MachineFacts, "labels" | "for">,
886
- work: Pick<WaitingWork, "pool" | "project" | "user" | "key">,
969
+ machine: Pick<MachineFacts, "name" | "labels" | "for"> & {
970
+ spare?: boolean;
971
+ },
972
+ work: Pick<
973
+ WaitingWork,
974
+ "pool" | "project" | "user" | "key" | "kind" | "machine"
975
+ >,
887
976
  ): boolean {
977
+ if (work.machine !== null && work.machine !== machine.name) return false;
888
978
  const labels: Record<string, string> = { ...machine.labels, plane: "worker" };
889
979
  return (
890
980
  Object.entries(work.pool).every(([key, value]) => labels[key] === value) &&
891
- principalsTake({ for: machine.for, work })
981
+ principalsTake({
982
+ for: machine.for,
983
+ ...(machine.spare ? { spare: true } : {}),
984
+ work,
985
+ })
892
986
  );
893
987
  }
894
988
 
@@ -917,6 +1011,7 @@ async function emit(
917
1011
  managed: facts.managed,
918
1012
  labels: facts.labels,
919
1013
  for: facts.for,
1014
+ project: facts.project,
920
1015
  options: facts.options,
921
1016
  reason: facts.reason,
922
1017
  },