@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.
Files changed (114) hide show
  1. package/NOTICE.txt +7 -0
  2. package/README.md +9 -9
  3. package/dist/admission/routes.d.ts +1 -1
  4. package/dist/admission/routes.d.ts.map +1 -1
  5. package/dist/agent-capabilities.d.ts.map +1 -1
  6. package/dist/agents.d.ts +5 -6
  7. package/dist/agents.d.ts.map +1 -1
  8. package/dist/auth/fastify-auth.d.ts.map +1 -1
  9. package/dist/auth/work-auth-types.d.ts +2 -0
  10. package/dist/auth/work-auth-types.d.ts.map +1 -1
  11. package/dist/auth/work-auth.d.ts.map +1 -1
  12. package/dist/chunk-kcck20aw.js +676 -0
  13. package/dist/chunk-kcck20aw.js.map +11 -0
  14. package/dist/cluster.d.ts +8 -7
  15. package/dist/cluster.d.ts.map +1 -1
  16. package/dist/config.d.ts +5 -15
  17. package/dist/config.d.ts.map +1 -1
  18. package/dist/execution-config.d.ts +22 -0
  19. package/dist/execution-config.d.ts.map +1 -1
  20. package/dist/fake-agent.d.ts +1 -0
  21. package/dist/fake-agent.d.ts.map +1 -1
  22. package/dist/index.d.ts +3 -2
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +2730 -2851
  25. package/dist/index.js.map +28 -28
  26. package/dist/machines/contract-kit.d.ts +91 -0
  27. package/dist/machines/contract-kit.d.ts.map +1 -0
  28. package/dist/machines/contract.d.ts +383 -0
  29. package/dist/machines/contract.d.ts.map +1 -0
  30. package/dist/machines/sequencer.d.ts +267 -0
  31. package/dist/machines/sequencer.d.ts.map +1 -0
  32. package/dist/machines/simulation.d.ts +124 -0
  33. package/dist/machines/simulation.d.ts.map +1 -0
  34. package/dist/machines/work-machines.d.ts +61 -0
  35. package/dist/machines/work-machines.d.ts.map +1 -0
  36. package/dist/server.d.ts +15 -11
  37. package/dist/server.d.ts.map +1 -1
  38. package/dist/setup/machines.d.ts +9 -17
  39. package/dist/setup/machines.d.ts.map +1 -1
  40. package/dist/shares/share-routes.d.ts +1 -1
  41. package/dist/shares/share-routes.d.ts.map +1 -1
  42. package/dist/testing.d.ts +8 -0
  43. package/dist/testing.d.ts.map +1 -0
  44. package/dist/testing.js +1013 -0
  45. package/dist/testing.js.map +11 -0
  46. package/dist/workers/disk-care.d.ts +41 -0
  47. package/dist/workers/disk-care.d.ts.map +1 -0
  48. package/dist/workers/harness-sign-ins.d.ts +146 -0
  49. package/dist/workers/harness-sign-ins.d.ts.map +1 -0
  50. package/dist/workers/machine-store.d.ts +196 -0
  51. package/dist/workers/machine-store.d.ts.map +1 -0
  52. package/dist/workers/member-machine-routes.d.ts +5 -3
  53. package/dist/workers/member-machine-routes.d.ts.map +1 -1
  54. package/dist/workers/member-machines.d.ts +47 -28
  55. package/dist/workers/member-machines.d.ts.map +1 -1
  56. package/dist/workers/placement.d.ts +23 -40
  57. package/dist/workers/placement.d.ts.map +1 -1
  58. package/dist/workers/sign-ins.d.ts +44 -17
  59. package/dist/workers/sign-ins.d.ts.map +1 -1
  60. package/dist/workers/worker-protocol.d.ts +5 -2
  61. package/dist/workers/worker-protocol.d.ts.map +1 -1
  62. package/dist/workers/worker-registry.d.ts +174 -223
  63. package/dist/workers/worker-registry.d.ts.map +1 -1
  64. package/dist/workers/worker-routes.d.ts.map +1 -1
  65. package/dist/workers/worker-runtime.d.ts.map +1 -1
  66. package/package.json +44 -34
  67. package/src/admission/routes.ts +5 -2
  68. package/src/agent-capabilities.ts +22 -8
  69. package/src/agents.ts +52 -23
  70. package/src/auth/fastify-auth.ts +149 -17
  71. package/src/auth/work-auth-types.ts +2 -0
  72. package/src/auth/work-auth.ts +27 -0
  73. package/src/cluster.ts +115 -42
  74. package/src/config.ts +15 -41
  75. package/src/execution-config.ts +40 -0
  76. package/src/fake-agent.ts +21 -4
  77. package/src/index.ts +26 -12
  78. package/src/machines/contract-kit.ts +648 -0
  79. package/src/machines/contract.ts +522 -0
  80. package/src/machines/sequencer.ts +1010 -0
  81. package/src/machines/simulation.ts +833 -0
  82. package/src/machines/work-machines.ts +231 -0
  83. package/src/server.ts +235 -150
  84. package/src/setup/machines.ts +128 -121
  85. package/src/shares/share-routes.ts +17 -1
  86. package/src/testing.ts +22 -0
  87. package/src/workers/disk-care.ts +121 -0
  88. package/src/workers/fake-claude-cli.ts +95 -0
  89. package/src/workers/fake-codex-cli.ts +11 -4
  90. package/src/workers/harness-sign-ins.ts +580 -0
  91. package/src/workers/machine-store.ts +1065 -0
  92. package/src/workers/member-machine-routes.ts +92 -20
  93. package/src/workers/member-machines.ts +163 -99
  94. package/src/workers/placement.ts +59 -70
  95. package/src/workers/sign-ins.ts +109 -52
  96. package/src/workers/worker-protocol.ts +5 -2
  97. package/src/workers/worker-registry.ts +894 -754
  98. package/src/workers/worker-routes.ts +4 -1
  99. package/src/workers/worker-runtime.ts +55 -23
  100. package/dist/test-support.d.ts +0 -133
  101. package/dist/test-support.d.ts.map +0 -1
  102. package/dist/workers/codex-sign-ins.d.ts +0 -107
  103. package/dist/workers/codex-sign-ins.d.ts.map +0 -1
  104. package/dist/workers/install-script.d.ts +0 -71
  105. package/dist/workers/install-script.d.ts.map +0 -1
  106. package/dist/workers/machine-classes.d.ts +0 -115
  107. package/dist/workers/machine-classes.d.ts.map +0 -1
  108. package/dist/workers/machine-rules.d.ts +0 -402
  109. package/dist/workers/machine-rules.d.ts.map +0 -1
  110. package/src/test-support.ts +0 -404
  111. package/src/workers/codex-sign-ins.ts +0 -360
  112. package/src/workers/install-script.ts +0 -394
  113. package/src/workers/machine-classes.ts +0 -152
  114. 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 { CodexSignInRefusedError } from "./codex-sign-ins.js";
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 the Work app (ADR
9
- * 0213). Every route acts as the caller, on a machine of the caller's own;
10
- * the sign-in itself happens in the member's browser and on the machine.
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 machineParams = z.object({ id: z.string().min(1).max(200) });
20
- const attemptParams = machineParams.extend({ attempt: z.string().uuid() });
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 CodexSignInRefusedError)
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/codex/sign-in",
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 { id } = machineParams.parse(request.params);
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/codex/sign-in/:attempt",
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 { id, attempt } = attemptParams.parse(request.params);
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
- attempt,
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/codex/sign-in/:attempt",
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 { id, attempt } = attemptParams.parse(request.params);
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({ identity, machineId: id, attempt });
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/codex", async (request, reply) => {
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 { id } = machineParams.parse(request.params);
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({ identity, machineId: id });
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
- CodexSignedOutSchema,
11
- CodexSignInRefusedError,
12
- CodexSignInRefusedSchema,
13
- type CodexSignInRequest,
14
- type CodexSignInStarted,
15
- CodexSignInStartedSchema,
16
- type CodexSignInStatus,
17
- CodexSignInStatusSchema,
18
- type CodexSignIns,
19
- } from "./codex-sign-ins.js";
20
- import { nodeAccess, type ScheduledPlacement } from "./placement.js";
21
- import { isWorkerNode, WORKER_NODE_PREFIX } from "./worker-registry.js";
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 Codex sign-ins (ADR
25
- * 0213): workers whose access names only that member, and a single
26
- * server's own machine when its operator accepted personal credentials
27
- * there and no one else signed in on it first. A sign-in on any other
28
- * machine would show the provider several people's accounts from one
29
- * machine, so it is never offered, and placement would not run on it.
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
- codex: "signed-in" | "signed-out";
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
- placements: () => Promise<ReadonlyMap<string, ScheduledPlacement>>;
54
- /** The member's placement identity: their email and groups. */
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: CodexSignIns };
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: CodexSignInRequest;
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 = await this.deps.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 nodes
76
- .filter((node) => own.has(node.id))
77
- .map((node) => ({
78
- id: node.id,
79
- name: node.id.startsWith(WORKER_NODE_PREFIX)
80
- ? node.id.slice(WORKER_NODE_PREFIX.length)
81
- : node.descriptor.label,
82
- available: node.available,
83
- codex: node.descriptor.capabilities.includes(
84
- signInCapability({ harness: "codex", member }),
85
- )
86
- ? "signed-in"
87
- : "signed-out",
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 CodexSignInRefusedError} with the machine's reason. */
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([CodexSignInStartedSchema, CodexSignInRefusedSchema])
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) throw new CodexSignInRefusedError(answer.refused);
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(args: {
113
- identity: Identity;
114
- machineId: string;
115
- attempt: string;
116
- }): Promise<CodexSignInStatus> {
117
- const status = CodexSignInStatusSchema.parse(
118
- await this.send(args, {
119
- action: "status",
120
- member: args.identity.externalUserId,
121
- attempt: args.attempt,
122
- }),
123
- );
124
- if (status.state !== "signed-in") return status;
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
- async cancel(args: {
138
- identity: Identity;
139
- machineId: string;
140
- attempt: string;
141
- }): Promise<void> {
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
- identity: Identity;
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: CodexSignInRequest,
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
- /** The ids of the machines that are the member's own. */
175
- private async own(identity: Identity): Promise<Set<string>> {
176
- const [placements, owner] = await Promise.all([
177
- this.deps.placements(),
178
- this.deps.owner(identity.externalUserId),
179
- ]);
180
- const ids = new Set<string>();
181
- if (owner)
182
- for (const [nodeId, placement] of placements) {
183
- if (!isWorkerNode(nodeId) || placement.released) continue;
184
- if (accessTier({ access: nodeAccess(placement.access), owner }) === 0)
185
- ids.add(nodeId);
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
- ids.add(ownMachine.nodeId);
256
+ own.set(ownMachine.nodeId, undefined);
203
257
  }
204
- return ids;
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
+ }