@jr2/orchestrator 0.3.0 → 0.4.1

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 (73) hide show
  1. package/bin/server.ts +7 -4
  2. package/console/canvas.ts +27 -14
  3. package/console/components/app.ts +10 -1
  4. package/console/components/fleet.ts +39 -6
  5. package/console/components/machine-pane.ts +89 -2
  6. package/console/components/nav.ts +1 -1
  7. package/console/components/schema-form.ts +12 -4
  8. package/console/json-field.ts +23 -0
  9. package/console/main.ts +110 -39
  10. package/console/store.ts +221 -17
  11. package/console/style.css +90 -0
  12. package/package.json +4 -4
  13. package/src/actor.ts +502 -49
  14. package/src/ambient.ts +2 -0
  15. package/src/assembly-checks.ts +165 -0
  16. package/src/assembly-tar.ts +494 -0
  17. package/src/assembly.ts +721 -0
  18. package/src/chart.ts +2050 -0
  19. package/src/charts.ts +720 -0
  20. package/src/config.ts +152 -46
  21. package/src/content-hash.ts +105 -0
  22. package/src/custodian.lua +6 -5
  23. package/src/custodian.ts +71 -19
  24. package/src/customize.ts +216 -25
  25. package/src/effect-checks.ts +557 -0
  26. package/src/effect.ts +905 -0
  27. package/src/effector-wire.ts +525 -0
  28. package/src/fingerprint.ts +4 -3
  29. package/src/gate.ts +8 -0
  30. package/src/git-clone.ts +65 -0
  31. package/src/git-push.ts +336 -0
  32. package/src/harness-client.ts +245 -23
  33. package/src/held-secrets.ts +82 -26
  34. package/src/holds.ts +156 -0
  35. package/src/http-effect.ts +981 -0
  36. package/src/http.ts +496 -104
  37. package/src/images.ts +47 -21
  38. package/src/index.ts +43 -2
  39. package/src/instance.ts +444 -58
  40. package/src/kube-client.ts +333 -0
  41. package/src/machine-doc.ts +88 -6
  42. package/src/mount-digest.ts +53 -0
  43. package/src/names.ts +66 -8
  44. package/src/notices.ts +88 -0
  45. package/src/open-body.ts +1444 -0
  46. package/src/open-effect.ts +144 -0
  47. package/src/open.ts +4 -4
  48. package/src/parts.ts +530 -33
  49. package/src/party.ts +468 -0
  50. package/src/pick.ts +309 -0
  51. package/src/pool.ts +1 -1
  52. package/src/registration.ts +301 -17
  53. package/src/repo-fetch.ts +60 -85
  54. package/src/repos.ts +63 -74
  55. package/src/rest.ts +133 -0
  56. package/src/run-host.ts +1832 -98
  57. package/src/sandbox-effect.ts +891 -0
  58. package/src/sandbox-effector.ts +111 -0
  59. package/src/sandbox-kube.ts +1719 -0
  60. package/src/sandbox-watch.ts +260 -0
  61. package/src/secret-placement.ts +494 -0
  62. package/src/server.ts +281 -84
  63. package/src/setup.ts +589 -70
  64. package/src/size.ts +342 -0
  65. package/src/snapshot-store.ts +64 -13
  66. package/src/stub-harness.ts +5 -0
  67. package/src/sync-client.ts +54 -0
  68. package/src/timers.ts +178 -0
  69. package/src/tokens.ts +35 -4
  70. package/src/vocabulary.ts +66 -1
  71. package/src/wire.ts +156 -2
  72. package/src/workspace.ts +529 -194
  73. package/src/sandbox-kubectl.ts +0 -1108
package/src/pick.ts ADDED
@@ -0,0 +1,309 @@
1
+ // What an author writes between a pick and the work it starts (ADR-0067). All three are optional:
2
+ // plain xstate — an input mapper, no answer — is legal.
3
+ //
4
+ // - `fromPick(def, fill)` — the `input` of a state a pick enters. The pick's params flow into the
5
+ // invoked actor's input BY NAME; `fill` returns only what the MACHINE adds. So the security
6
+ // boundary reads in one expression: the def is what the Agent supplies, `fill` is what the
7
+ // Machine adds, and the sum is checked against the invoked actor's input type — for a missing or
8
+ // mistyped key only: TypeScript accepts an extra key on a spread. The load walk refuses the rest
9
+ // (effect-checks.ts): a param that is no key of an Effect's input and that `fill` does not read,
10
+ // and a param `fill` overwrites.
11
+ // - `paramsOf(event, def)` — the same params, for a plain mapper that wants them.
12
+ // - `answer(fn)` — an xstate action that sets what the Agent reads when its pick is answered
13
+ // (ADR-0024). Absent, it reads the last reply since the pick. `fn` gets `{ context, event, chain }`:
14
+ // `chain` is every reply since the pick, oldest first, each typed by its slot and its kind. Its
15
+ // order and length are run-time facts, not typed. Last call wins. It runs where xstate runs it —
16
+ // a `rest`-entry `answer` runs only when a route enters `rest` again.
17
+
18
+ import type {
19
+ ActionFunction,
20
+ AnyActorRef,
21
+ AnyMachineSnapshot,
22
+ AnyStateMachine,
23
+ EventObject,
24
+ MachineContext,
25
+ OutputFrom,
26
+ ParameterizedObject,
27
+ ProvidedActor,
28
+ } from "xstate";
29
+ import { enqueueActions } from "xstate";
30
+ import type { z } from "zod";
31
+ import { SENDER_KEY, type EventDef, type Sender } from "@jr2/agent-protocol";
32
+ import { isAgent } from "./agent.ts";
33
+ import { boundRun } from "./registration.ts";
34
+ import type { PickAnswer } from "./wire.ts";
35
+
36
+ /** The pick's params, read off the event that entered the state. Throws — a jr2 or author defect
37
+ * the load walk refuses first — when the event is not this pick. */
38
+ export function paramsOf<N extends string, I extends z.ZodObject>(event: EventObject, def: EventDef<N, I>): z.infer<I> {
39
+ if (event.type !== def.name) {
40
+ throw new Error(`state entered by "${event.type}", but its input reads the params of pick "${def.name}"`);
41
+ }
42
+ const { type: _type, [SENDER_KEY]: _sender, ...payload } = event as EventObject & Record<string, unknown>;
43
+ return def.input.parse(payload);
44
+ }
45
+
46
+ /** The brand `fromPick` puts on the mapper it returns: the pick whose params it reads, and the
47
+ * `fill` the load walk probes. */
48
+ export type PickMapper = {
49
+ readonly pick: EventDef;
50
+ readonly fill?: (args: { context: unknown; params: unknown }) => unknown;
51
+ };
52
+
53
+ /** Is this invoke input a `fromPick` mapper? */
54
+ export function isPickMapper(value: unknown): value is PickMapper & ((args: unknown) => unknown) {
55
+ return typeof value === "function" && "pick" in value;
56
+ }
57
+
58
+ export function fromPick<N extends string, I extends z.ZodObject, C, E extends EventObject>(
59
+ def: EventDef<N, I>,
60
+ ): (args: { context: C; event: E; self: AnyActorRef }) => z.infer<I>;
61
+ export function fromPick<N extends string, I extends z.ZodObject, C, E extends EventObject, R extends object>(
62
+ def: EventDef<N, I>,
63
+ fill: (args: { context: C; params: z.infer<I> }) => R,
64
+ ): (args: { context: C; event: E; self: AnyActorRef }) => z.infer<I> & R;
65
+ export function fromPick<N extends string, I extends z.ZodObject, C, E extends EventObject, R extends object>(
66
+ def: EventDef<N, I>,
67
+ fill?: (args: { context: C; params: z.infer<I> }) => R,
68
+ ) {
69
+ const mapper = ({ context, event }: { context: C; event: E; self: AnyActorRef }) => {
70
+ const params = paramsOf(event, def);
71
+ return fill ? { ...params, ...fill({ context, params }) } : params;
72
+ };
73
+ return Object.assign(mapper, { pick: def, ...(fill ? { fill } : {}) });
74
+ }
75
+
76
+ /**
77
+ * One reply since the pick, as the author reads it: an invoked actor's output, typed by its slot
78
+ * (`src`); or an event that answered a state below the Agent's — a Gate's answer, a nested Agent's
79
+ * pick — typed by the Machine's events. A reply is from outside the Machine: the events it sends
80
+ * itself are not links.
81
+ */
82
+ export type ChainLink<TActor extends ProvidedActor, TEvent extends EventObject> =
83
+ | (TActor extends unknown ? { kind: "output"; src: TActor["src"]; output: OutputFrom<TActor["logic"]> } : never)
84
+ | { kind: "event"; event: Exclude<TEvent, { type: `${string}.${string}` }> };
85
+
86
+ /** The chain as the author reads it, from the replies the run host recorded. */
87
+ function linksOf(chain: readonly PickAnswer[]): unknown[] {
88
+ return chain.flatMap((link): unknown[] => {
89
+ if (link.kind === "output") return [{ kind: "output", src: link.actor, output: link.output }];
90
+ if (link.kind === "event") {
91
+ const sender: Sender | undefined = link.sender;
92
+ return [
93
+ { kind: "event", event: { type: link.event, ...link.payload, ...(sender ? { [SENDER_KEY]: sender } : {}) } },
94
+ ];
95
+ }
96
+ return [];
97
+ });
98
+ }
99
+
100
+ type Node = AnyStateMachine["root"];
101
+
102
+ /**
103
+ * Where an `answer` is written, as the Agents' states it is below: for each place the action is
104
+ * written (a state's entry or exit, a transition on a state, a state's initial transition), the
105
+ * nearest state at or above it that invokes an Agent — `null` for a transition where none does; an
106
+ * entry or exit below no Agent's state seats nowhere. Read once per Machine, by the action's
107
+ * identity: one `answer` object written in two places carries both. At run time each place runs as
108
+ * its own seat (`placeAnswers`): an `answer` cannot tell where it was written, so the place is
109
+ * decided before the Machine runs.
110
+ */
111
+ export type AnswerSeats = ReadonlySet<Node | null>;
112
+
113
+ /**
114
+ * The one place an `answer` runs from (ADR-0067): the Agent's state it is written below, or `null`
115
+ * on a transition below no Agent's state. At a state, it shapes only a pick of the Agent seated
116
+ * there. At `null`, the event that took the transition names the Agent whose pick it shapes (the run
117
+ * host reads it off the event).
118
+ */
119
+ export type AnswerSeat = Node | null;
120
+
121
+ const seatsCache = new WeakMap<AnyStateMachine, Map<unknown, Set<Node | null>>>();
122
+
123
+ /** How an `answer` makes the copy of itself that runs from one seat. */
124
+ const PLACE = Symbol("jr2.answer.place");
125
+ type Placeable = { [PLACE]: (seat: AnswerSeat) => unknown; readonly jr2Answer: true };
126
+
127
+ /** An action of `machine` as it is written — inline, or by name — resolved to its implementation. */
128
+ function resolverOf(machine: AnyStateMachine): (a: unknown) => unknown {
129
+ const named = (machine.implementations?.actions ?? {}) as Record<string, unknown>;
130
+ return (a) => {
131
+ const type = typeof a === "string" ? a : typeof a === "object" && a !== null ? (a as { type?: unknown }).type : a;
132
+ return typeof type === "string" ? named[type] : a;
133
+ };
134
+ }
135
+
136
+ /** The nearest state at or above `n` that invokes one of `machine`'s Agents, or `null`. */
137
+ function seatFinderOf(machine: AnyStateMachine): (n: Node) => Node | null {
138
+ const actors = (machine.implementations?.actors ?? {}) as Record<string, unknown>;
139
+ return (n) => {
140
+ for (let p: Node | undefined = n; p; p = p.parent)
141
+ if (p.invoke.some((inv) => typeof inv.src === "string" && isAgent(actors[inv.src]))) return p;
142
+ return null;
143
+ };
144
+ }
145
+
146
+ /** The `answer` a placed copy was made from: the identity `seatsOfAnswer` groups places by. */
147
+ const originals = new WeakMap<object, unknown>();
148
+ const originalOf = (fn: unknown): unknown => (typeof fn === "function" ? (originals.get(fn) ?? fn) : fn);
149
+
150
+ /** What stands where `original` was written on an entry or an exit below no Agent's state: an
151
+ * `answer` that runs nothing. Still an `answer` to the load walk, which warns on it. */
152
+ function unseatedAnswer(original: unknown): AnswerAction & (() => void) {
153
+ const copy = Object.assign(function unseatedAnswer(): void {}, { jr2Answer: true as const });
154
+ originals.set(copy, original);
155
+ return copy;
156
+ }
157
+
158
+ const placedMachines = new WeakSet<AnyStateMachine>();
159
+
160
+ /** xstate's `enqueueActions` as it is written: its `collect` runs at the place the action stands,
161
+ * and the actions it enqueues run there too. */
162
+ type Enqueuing = { type: "xstate.enqueueActions"; collect: (args: EnqueueArgs, params: unknown) => void };
163
+ type EnqueueArgs = { context: unknown; event: unknown; enqueue: ((action: unknown) => void) & object };
164
+ const isEnqueuing = (action: unknown): action is Enqueuing =>
165
+ typeof action === "function" &&
166
+ (action as Partial<Enqueuing>).type === "xstate.enqueueActions" &&
167
+ typeof (action as Partial<Enqueuing>).collect === "function";
168
+
169
+ /** The params an action written as `{ type, params }` is run with, as xstate reads them. */
170
+ function paramsAt(written: unknown, args: EnqueueArgs): unknown {
171
+ if (typeof written !== "object" || written === null || !("params" in written)) return undefined;
172
+ const params = (written as { params?: unknown }).params;
173
+ return typeof params === "function" ? params({ context: args.context, event: args.event }) : params;
174
+ }
175
+
176
+ /**
177
+ * Give every place an `answer` is written in `machine` a copy of its own, bound to that place's
178
+ * seat (ADR-0067): one action object written in two places then shapes, from each, by that place's
179
+ * rule alone — below an Agent's state, only that Agent's pick; on a transition below none, the pick
180
+ * of the Agent the event names. An entry or an exit below no Agent's state is given an `answer` that
181
+ * sets nothing. At run time an action cannot tell where it was written, so the place is decided
182
+ * here. Called for each Machine of a run's tree as its actor is created, before its first state is
183
+ * entered. Each list is replaced, never written in place: a list the author shared between two
184
+ * places stays the author's.
185
+ */
186
+ export function placeAnswers(machine: AnyStateMachine): void {
187
+ if (placedMachines.has(machine) || !machine?.root) return;
188
+ placedMachines.add(machine);
189
+ const resolve = resolverOf(machine);
190
+ const seatAbove = seatFinderOf(machine);
191
+ const placeOne = (a: unknown, seat: AnswerSeat | "nowhere"): unknown => {
192
+ const fn = resolve(a);
193
+ // An `enqueueActions` runs what it enqueues at its own place: each `answer` it enqueues, at any
194
+ // depth, runs from the seat the enqueuing action is written at (ADR-0067). The load walk cannot
195
+ // see inside one, so the copy places what it enqueues as it is enqueued.
196
+ if (isEnqueuing(fn)) {
197
+ const collect = fn.collect;
198
+ return enqueueActions(((args: EnqueueArgs) => {
199
+ const enqueue = Object.assign((action: unknown) => args.enqueue(placeOne(action, seat)), args.enqueue);
200
+ collect({ ...args, enqueue }, paramsAt(a, args));
201
+ }) as never);
202
+ }
203
+ if (!isAnswer(fn)) return a;
204
+ if (seat === "nowhere") return unseatedAnswer(originalOf(fn));
205
+ const original = originalOf(fn) as Partial<Placeable>;
206
+ return typeof original[PLACE] === "function" ? original[PLACE](seat) : fn;
207
+ };
208
+ const placed = (list: readonly unknown[], seat: AnswerSeat | "nowhere"): unknown[] =>
209
+ list.map((a) => placeOne(a, seat));
210
+ const visit = (n: Node): void => {
211
+ const seat = seatAbove(n);
212
+ const writable = n as unknown as { entry: unknown[]; exit: unknown[] };
213
+ writable.entry = placed(n.entry, seat ?? "nowhere");
214
+ writable.exit = placed(n.exit, seat ?? "nowhere");
215
+ const transitions = [
216
+ ...[...n.transitions.values()].flat(),
217
+ ...(n.always ?? []),
218
+ ...(n.initial ? [n.initial] : []),
219
+ ] as unknown as { actions: readonly unknown[] }[];
220
+ for (const t of new Set(transitions)) t.actions = placed(t.actions, seat);
221
+ for (const child of Object.values(n.states)) visit(child);
222
+ };
223
+ visit(machine.root);
224
+ }
225
+
226
+ export function seatsOfAnswer(machine: AnyStateMachine, action: unknown): AnswerSeats {
227
+ let byAction = seatsCache.get(machine);
228
+ if (byAction === undefined) {
229
+ byAction = new Map();
230
+ seatsCache.set(machine, byAction);
231
+ const resolve = resolverOf(machine);
232
+ const seatAbove = seatFinderOf(machine);
233
+ const visit = (n: Node): void => {
234
+ const seat = seatAbove(n);
235
+ const onEntryOrExit = [...n.entry, ...n.exit];
236
+ const onTransition = [
237
+ ...[...n.transitions.values()].flat().flatMap((t) => t.actions),
238
+ ...(n.always ?? []).flatMap((t) => t.actions),
239
+ ...(n.initial?.actions ?? []),
240
+ ];
241
+ const note = (a: unknown, seated: boolean): void => {
242
+ const fn = resolve(a);
243
+ if (!isAnswer(fn)) return;
244
+ const key = originalOf(fn);
245
+ const seats = byAction!.get(key) ?? new Set<Node | null>();
246
+ if (seated) seats.add(seat);
247
+ byAction!.set(key, seats);
248
+ };
249
+ // An entry or exit below no Agent's state is no transition an Agent's event takes: it seats
250
+ // nowhere, and runs nothing (`placeAnswers`, ADR-0067).
251
+ for (const a of onEntryOrExit) note(a, seat !== null);
252
+ for (const a of onTransition) note(a, true);
253
+ for (const child of Object.values(n.states)) visit(child);
254
+ };
255
+ if (machine?.root) visit(machine.root);
256
+ }
257
+ return byAction.get(originalOf(action)) ?? new Set<Node | null>([null]);
258
+ }
259
+
260
+ /** The brand the load walk reads: this action is an `answer`. */
261
+ export type AnswerAction = { readonly jr2Answer: true };
262
+
263
+ /** Is this action an `answer`? */
264
+ export const isAnswer = (action: unknown): boolean =>
265
+ typeof action === "function" && (action as Partial<AnswerAction>).jr2Answer === true;
266
+
267
+ export function answer<
268
+ TContext extends MachineContext,
269
+ TExpressionEvent extends EventObject,
270
+ TParams extends ParameterizedObject["params"] | undefined,
271
+ TEvent extends EventObject,
272
+ TActor extends ProvidedActor,
273
+ >(
274
+ fn: (args: { context: TContext; event: TExpressionEvent; chain: readonly ChainLink<TActor, TEvent>[] }) => unknown,
275
+ ): ActionFunction<TContext, TExpressionEvent, TEvent, TParams, TActor, never, never, never, never> & AnswerAction {
276
+ // The action as it runs from one seat (`placeAnswers`): the Agent's state it is written below, or
277
+ // `null` on a transition below none. The object the author holds runs as `null`: it runs only from
278
+ // a Machine `placeAnswers` never placed.
279
+ const at =
280
+ (seat: AnswerSeat) =>
281
+ ({ context, event, self }: { context: TContext; event: TExpressionEvent; self: AnyActorRef }) => {
282
+ const binding = boundRun(self.system);
283
+ // No pick of an Agent this seat reads is being answered (the state's first entry, a later
284
+ // Turn's, another Agent's): there is nothing to shape, and nothing runs.
285
+ const chain = binding?.chainOf?.(self, seat, event);
286
+ if (chain === undefined) return;
287
+ // Kit-internal: the host records links by slot name, which is what ChainLink states.
288
+ const links = linksOf(chain) as unknown as readonly ChainLink<TActor, TEvent>[];
289
+ binding?.setAnswer?.(self, seat, fn({ context, event, chain: links }), event);
290
+ };
291
+ const action = at(null);
292
+ const place = (seat: AnswerSeat): unknown => {
293
+ const copy = Object.assign(at(seat), { jr2Answer: true as const });
294
+ originals.set(copy, action);
295
+ return copy;
296
+ };
297
+ return Object.assign(action, { jr2Answer: true as const, [PLACE]: place }) as unknown as ActionFunction<
298
+ TContext,
299
+ TExpressionEvent,
300
+ TEvent,
301
+ TParams,
302
+ TActor,
303
+ never,
304
+ never,
305
+ never,
306
+ never
307
+ > &
308
+ AnswerAction;
309
+ }
package/src/pool.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  // The Source port is generalized — `next(active) → item | null`, plus an optional `wake` event
11
11
  // def (a webhook / `jr2 send` push seam, served as a standing gate named "source") and
12
12
  // `pollEvery` (the set mutates underneath us — re-query). A queue, a generator, or a re-queried
13
- // set; tk's claim actor is one adapter (CONTEXT.md: Work Source is the ticket-flavored Source).
13
+ // set; tk's claim actor is one adapter (GLOSSARY.md: Work Source is the Source adapter for tickets).
14
14
  //
15
15
  // Terminal triage is three-valued (ADR-0017, forced by parking-is-retention):
16
16
  // - drained — source empty and all children settled → final (jr exit 0);