@xeonr/pipelines-sdk 0.0.0-stage → 0.2.0

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 (102) hide show
  1. package/README.md +334 -2
  2. package/dist/auth.d.ts +72 -0
  3. package/dist/auth.d.ts.map +1 -0
  4. package/dist/auth.js +139 -0
  5. package/dist/auth.js.map +1 -0
  6. package/dist/client.d.ts +129 -0
  7. package/dist/client.d.ts.map +1 -0
  8. package/dist/client.js +106 -0
  9. package/dist/client.js.map +1 -0
  10. package/dist/errors.d.ts +113 -0
  11. package/dist/errors.d.ts.map +1 -0
  12. package/dist/errors.js +115 -0
  13. package/dist/errors.js.map +1 -0
  14. package/dist/gen/buf/validate/validate_pb.d.ts +4828 -0
  15. package/dist/gen/buf/validate/validate_pb.d.ts.map +1 -0
  16. package/dist/gen/buf/validate/validate_pb.js +357 -0
  17. package/dist/gen/buf/validate/validate_pb.js.map +1 -0
  18. package/dist/gen/gnostic/openapi/v3/annotations_pb.d.ts +24 -0
  19. package/dist/gen/gnostic/openapi/v3/annotations_pb.d.ts.map +1 -0
  20. package/dist/gen/gnostic/openapi/v3/annotations_pb.js +37 -0
  21. package/dist/gen/gnostic/openapi/v3/annotations_pb.js.map +1 -0
  22. package/dist/gen/gnostic/openapi/v3/openapiv3_pb.d.ts +2245 -0
  23. package/dist/gen/gnostic/openapi/v3/openapiv3_pb.d.ts.map +1 -0
  24. package/dist/gen/gnostic/openapi/v3/openapiv3_pb.js +410 -0
  25. package/dist/gen/gnostic/openapi/v3/openapiv3_pb.js.map +1 -0
  26. package/dist/gen/xeonr/pipelines/private/v1/attempt_pb.d.ts +1031 -0
  27. package/dist/gen/xeonr/pipelines/private/v1/attempt_pb.d.ts.map +1 -0
  28. package/dist/gen/xeonr/pipelines/private/v1/attempt_pb.js +219 -0
  29. package/dist/gen/xeonr/pipelines/private/v1/attempt_pb.js.map +1 -0
  30. package/dist/gen/xeonr/pipelines/private/v1/runners_pb.d.ts +190 -0
  31. package/dist/gen/xeonr/pipelines/private/v1/runners_pb.d.ts.map +1 -0
  32. package/dist/gen/xeonr/pipelines/private/v1/runners_pb.js +53 -0
  33. package/dist/gen/xeonr/pipelines/private/v1/runners_pb.js.map +1 -0
  34. package/dist/gen/xeonr/pipelines/private/v1/step_local_pb.d.ts +1161 -0
  35. package/dist/gen/xeonr/pipelines/private/v1/step_local_pb.d.ts.map +1 -0
  36. package/dist/gen/xeonr/pipelines/private/v1/step_local_pb.js +269 -0
  37. package/dist/gen/xeonr/pipelines/private/v1/step_local_pb.js.map +1 -0
  38. package/dist/gen/xeonr/pipelines/private/v1/worker_pb.d.ts +1061 -0
  39. package/dist/gen/xeonr/pipelines/private/v1/worker_pb.d.ts.map +1 -0
  40. package/dist/gen/xeonr/pipelines/private/v1/worker_pb.js +221 -0
  41. package/dist/gen/xeonr/pipelines/private/v1/worker_pb.js.map +1 -0
  42. package/dist/gen/xeonr/pipelines/v1/common_pb.d.ts +580 -0
  43. package/dist/gen/xeonr/pipelines/v1/common_pb.d.ts.map +1 -0
  44. package/dist/gen/xeonr/pipelines/v1/common_pb.js +483 -0
  45. package/dist/gen/xeonr/pipelines/v1/common_pb.js.map +1 -0
  46. package/dist/gen/xeonr/pipelines/v1/definition_pb.d.ts +762 -0
  47. package/dist/gen/xeonr/pipelines/v1/definition_pb.d.ts.map +1 -0
  48. package/dist/gen/xeonr/pipelines/v1/definition_pb.js +190 -0
  49. package/dist/gen/xeonr/pipelines/v1/definition_pb.js.map +1 -0
  50. package/dist/gen/xeonr/pipelines/v1/pools_pb.d.ts +919 -0
  51. package/dist/gen/xeonr/pipelines/v1/pools_pb.d.ts.map +1 -0
  52. package/dist/gen/xeonr/pipelines/v1/pools_pb.js +272 -0
  53. package/dist/gen/xeonr/pipelines/v1/pools_pb.js.map +1 -0
  54. package/dist/gen/xeonr/pipelines/v1/registry_pb.d.ts +895 -0
  55. package/dist/gen/xeonr/pipelines/v1/registry_pb.d.ts.map +1 -0
  56. package/dist/gen/xeonr/pipelines/v1/registry_pb.js +277 -0
  57. package/dist/gen/xeonr/pipelines/v1/registry_pb.js.map +1 -0
  58. package/dist/gen/xeonr/pipelines/v1/runs_pb.d.ts +1237 -0
  59. package/dist/gen/xeonr/pipelines/v1/runs_pb.d.ts.map +1 -0
  60. package/dist/gen/xeonr/pipelines/v1/runs_pb.js +204 -0
  61. package/dist/gen/xeonr/pipelines/v1/runs_pb.js.map +1 -0
  62. package/dist/gen/xeonr/pipelines/v1/secrets_pb.d.ts +647 -0
  63. package/dist/gen/xeonr/pipelines/v1/secrets_pb.d.ts.map +1 -0
  64. package/dist/gen/xeonr/pipelines/v1/secrets_pb.js +181 -0
  65. package/dist/gen/xeonr/pipelines/v1/secrets_pb.js.map +1 -0
  66. package/dist/gen/xeonr/pipelines/v1/sinks_pb.d.ts +381 -0
  67. package/dist/gen/xeonr/pipelines/v1/sinks_pb.d.ts.map +1 -0
  68. package/dist/gen/xeonr/pipelines/v1/sinks_pb.js +145 -0
  69. package/dist/gen/xeonr/pipelines/v1/sinks_pb.js.map +1 -0
  70. package/dist/gen/xeonr/platform/v1/options_pb.d.ts +313 -0
  71. package/dist/gen/xeonr/platform/v1/options_pb.d.ts.map +1 -0
  72. package/dist/gen/xeonr/platform/v1/options_pb.js +235 -0
  73. package/dist/gen/xeonr/platform/v1/options_pb.js.map +1 -0
  74. package/dist/handler.d.ts +94 -0
  75. package/dist/handler.d.ts.map +1 -0
  76. package/dist/handler.js +18 -0
  77. package/dist/handler.js.map +1 -0
  78. package/dist/index.d.ts +30 -0
  79. package/dist/index.d.ts.map +1 -0
  80. package/dist/index.js +24 -0
  81. package/dist/index.js.map +1 -0
  82. package/dist/internal/time.d.ts +8 -0
  83. package/dist/internal/time.d.ts.map +1 -0
  84. package/dist/internal/time.js +25 -0
  85. package/dist/internal/time.js.map +1 -0
  86. package/dist/logship.d.ts +117 -0
  87. package/dist/logship.d.ts.map +1 -0
  88. package/dist/logship.js +319 -0
  89. package/dist/logship.js.map +1 -0
  90. package/dist/testing/index.d.ts +215 -0
  91. package/dist/testing/index.d.ts.map +1 -0
  92. package/dist/testing/index.js +1015 -0
  93. package/dist/testing/index.js.map +1 -0
  94. package/dist/transport.d.ts +43 -0
  95. package/dist/transport.d.ts.map +1 -0
  96. package/dist/transport.js +44 -0
  97. package/dist/transport.js.map +1 -0
  98. package/dist/worker.d.ts +236 -0
  99. package/dist/worker.d.ts.map +1 -0
  100. package/dist/worker.js +733 -0
  101. package/dist/worker.js.map +1 -0
  102. package/package.json +70 -3
@@ -0,0 +1,1015 @@
1
+ /**
2
+ * @xeonr/pipelines-sdk/testing: an in-memory fake of pipelines-api's worker
3
+ * and jobs surface, so a product can unit-test its handlers, and the code
4
+ * that enqueues them, with no server.
5
+ *
6
+ * It is a real Connect router behind an in-memory transport, so the SDK's
7
+ * `PipelinesClient` and `Worker` talk to it exactly as they talk to
8
+ * pipelines-api: the same generated clients, the same error codes.
9
+ *
10
+ * ```ts
11
+ * const fake = new FakePipelines();
12
+ * const worker = new Worker({ transport: fake.transport(), tenant: FAKE_TENANT, pool: { name: "test" } });
13
+ * worker.handle(sendEmail);
14
+ * await worker.start();
15
+ * const client = new PipelinesClient({ transport: fake.transport() });
16
+ * const { run } = await client.createJob({ tenant: FAKE_TENANT, handler: "acme/send-email", idempotencyKey: "k", payload: { to: "a@b" } });
17
+ * const done = await fake.waitForRun(run.id);
18
+ * ```
19
+ *
20
+ * What it models: auth by bearer token (accounts with a tenant and scopes),
21
+ * namespace and pool ownership, `PublishVersion`, `SetPool`, `CreateJob`
22
+ * (with delay, idempotency, retry and timeout), `CreateRun` with handler
23
+ * nodes and external (image) nodes that a test settles with
24
+ * {@link FakePipelines.completeNode}, `${{ steps.<node>.outputs.kv.<slot> }}`
25
+ * references (only to a node in the referrer's needs closure), the claim
26
+ * long-poll, the worker id on every ref, leases and their lapse,
27
+ * `ReleaseAttempt`, heartbeats with cancel, logs (idempotent on seq,
28
+ * `out_of_range` naming the expected seq for a gap, and
29
+ * {@link FakePipelines.loseLogChunks} to play a server that lost chunks),
30
+ * completion with the server's termination to failure-class mapping,
31
+ * `handler_failure_kind`, retries by class, free interruptions on an
32
+ * `interruptible` node, cancel, and error injection.
33
+ *
34
+ * What it does not: secrets, blobs, tiers, routing between pools, the
35
+ * outbox, and the platform's own deadline enforcement.
36
+ */
37
+ import { randomUUID } from "node:crypto";
38
+ import { create, fromJson, toJson } from "@bufbuild/protobuf";
39
+ import { durationFromMs, durationMs, timestampFromMs, ValueSchema } from "@bufbuild/protobuf/wkt";
40
+ import { Code, ConnectError, createRouterTransport } from "@connectrpc/connect";
41
+ import { tokenSource } from "../auth.js";
42
+ import { authInterceptor } from "../transport.js";
43
+ import { AttemptState, FailureClass, NodeState, RunState, Termination } from "../gen/xeonr/pipelines/v1/common_pb.js";
44
+ import { PoolKind, PoolOwnerKind, PoolSchema, PoolsService } from "../gen/xeonr/pipelines/v1/pools_pb.js";
45
+ import { PackageKind, RegistryService, VersionState } from "../gen/xeonr/pipelines/v1/registry_pb.js";
46
+ import { AttemptSchema, NodeRunSchema, RunSchema, RunsService } from "../gen/xeonr/pipelines/v1/runs_pb.js";
47
+ import { ClaimedAttemptSchema, HandlerFailureKind, WorkerService } from "../gen/xeonr/pipelines/private/v1/worker_pb.js";
48
+ /** The tenant the default account acts for. */
49
+ export const FAKE_TENANT = "urn:xeonr:auth:team:00000000-0000-4000-8000-000000000001";
50
+ /** The default account's token. */
51
+ export const FAKE_TOKEN = "fake-token";
52
+ /** Every scope pipelines' RPCs use. */
53
+ export const ALL_SCOPES = [
54
+ "pipelines:run:read",
55
+ "pipelines:run:edit",
56
+ "pipelines:package:read",
57
+ "pipelines:package:edit",
58
+ "pipelines:pool:read",
59
+ "pipelines:pool:edit",
60
+ "pipelines:worker:edit",
61
+ ];
62
+ const TRANSIENT = [
63
+ FailureClass.ABANDONED,
64
+ FailureClass.RUNNER_CRASHED,
65
+ FailureClass.IMAGE_PULL,
66
+ FailureClass.STAGING_FAILED,
67
+ FailureClass.INTERRUPTED,
68
+ FailureClass.INTERNAL,
69
+ ];
70
+ const KV_KEY = /^[A-Za-z_][A-Za-z0-9_-]*$/;
71
+ /** How many interruptions an interruptible node gets without spending a retry. */
72
+ const FREE_INTERRUPTIONS = 10;
73
+ const REF = /^\$\{\{\s*([^}]+?)\s*\}\}$/;
74
+ /**
75
+ * An in-memory pipelines-api for tests. Call {@link close} when done (it
76
+ * stops the timer that looks at leases and delays).
77
+ */
78
+ export class FakePipelines {
79
+ /** Runs by id. */
80
+ runs = new Map();
81
+ /** Every call that reached the fake, in order. */
82
+ calls = [];
83
+ accounts;
84
+ opts;
85
+ packages = new Map();
86
+ namespaces = new Map();
87
+ pools = new Map();
88
+ attemptsById = new Map();
89
+ idempotency = new Map();
90
+ injected = new Map();
91
+ timer;
92
+ changed;
93
+ constructor(opts = {}) {
94
+ this.accounts = new Map(Object.entries(opts.accounts ?? { [FAKE_TOKEN]: { tenant: FAKE_TENANT, scopes: ALL_SCOPES } }));
95
+ const leaseMs = opts.leaseMs ?? 60_000;
96
+ this.opts = {
97
+ leaseMs,
98
+ heartbeatIntervalMs: opts.heartbeatIntervalMs ?? leaseMs / 4,
99
+ cancelGraceMs: opts.cancelGraceMs ?? 10_000,
100
+ defaultTimeoutMs: opts.defaultTimeoutMs ?? 0,
101
+ defaultRetries: opts.defaultRetries ?? 2,
102
+ retryBackoffMs: opts.retryBackoffMs ?? 0,
103
+ logMaxBytes: opts.logMaxBytes ?? 0,
104
+ tickMs: opts.tickMs ?? 10,
105
+ };
106
+ this.changed = signal();
107
+ this.timer = setInterval(() => this.tick(), this.opts.tickMs);
108
+ this.timer.unref();
109
+ }
110
+ /**
111
+ * A transport to the fake that authenticates with `token` (default
112
+ * {@link FAKE_TOKEN}) through the SDK's own auth interceptor, so token
113
+ * refresh behaves as it does against the real API.
114
+ */
115
+ transport(token = FAKE_TOKEN, interceptors = []) {
116
+ const provider = typeof token === "string" ? () => token : token;
117
+ return this.buildRouter([authInterceptor(tokenSource(provider)), ...interceptors]);
118
+ }
119
+ /** Adds or replaces an account. */
120
+ setAccount(token, account) {
121
+ this.accounts.set(token, account);
122
+ }
123
+ /** Forgets a token: calls bearing it answer `unauthenticated`. */
124
+ revoke(token) {
125
+ this.accounts.delete(token);
126
+ }
127
+ /** Makes the next `times` calls of `method` fail with `code`. */
128
+ inject(method, code, times = 1, message = "injected by the fake") {
129
+ const list = this.injected.get(method) ?? [];
130
+ for (let i = 0; i < times; i++)
131
+ list.push(new ConnectError(message, code));
132
+ this.injected.set(method, list);
133
+ }
134
+ /** How many calls of `method` reached the fake. */
135
+ count(method) {
136
+ return this.calls.filter((c) => c.method === method).length;
137
+ }
138
+ /** One run, or throws. */
139
+ run(runId) {
140
+ const r = this.runs.get(runId);
141
+ if (!r)
142
+ throw new Error(`FakePipelines: no run ${runId}`);
143
+ return r;
144
+ }
145
+ /** One attempt, or throws. */
146
+ attempt(attemptId) {
147
+ const a = this.attemptsById.get(attemptId);
148
+ if (!a)
149
+ throw new Error(`FakePipelines: no attempt ${attemptId}`);
150
+ return a;
151
+ }
152
+ /** Every attempt of a run, in creation order. */
153
+ attemptsOf(runId) {
154
+ return [...this.run(runId).nodes.values()].flatMap((n) => n.attempts);
155
+ }
156
+ /** An attempt's log as text. */
157
+ logOf(attemptId) {
158
+ const dec = new TextDecoder();
159
+ return this.attempt(attemptId)
160
+ .chunks.map((c) => dec.decode(c))
161
+ .join("");
162
+ }
163
+ /** A registered pool, by name. */
164
+ pool(name) {
165
+ return [...this.pools.values()].find((p) => p.name === name);
166
+ }
167
+ /** Whether `name@version` was published. */
168
+ published(name, version) {
169
+ return this.packages.get(name)?.versions.get(version);
170
+ }
171
+ /** Waits until a run is terminal and returns it. */
172
+ async waitForRun(runId, timeoutMs = 10_000) {
173
+ return this.waitFor(() => {
174
+ const r = this.run(runId);
175
+ return terminal(r.state) ? r : undefined;
176
+ }, timeoutMs);
177
+ }
178
+ /** Waits until `check` returns something other than undefined. */
179
+ async waitFor(check, timeoutMs = 10_000) {
180
+ const until = Date.now() + timeoutMs;
181
+ for (;;) {
182
+ const v = check();
183
+ if (v !== undefined)
184
+ return v;
185
+ if (Date.now() > until)
186
+ throw new Error("FakePipelines.waitFor: timed out");
187
+ await new Promise((r) => setTimeout(r, this.opts.tickMs));
188
+ }
189
+ }
190
+ /** Lapses an attempt's lease now, as the reaper would at its expiry. */
191
+ /**
192
+ * Forgets an attempt's log chunks from `fromSeq` on, as a server that lost
193
+ * chunks it had acknowledged would: the worker's next chunk is refused as
194
+ * ahead (`out_of_range` naming `fromSeq`).
195
+ */
196
+ loseLogChunks(attemptId, fromSeq) {
197
+ const a = this.attempt(attemptId);
198
+ a.chunks.length = Math.min(a.chunks.length, fromSeq);
199
+ }
200
+ expireLease(attemptId) {
201
+ this.attempt(attemptId).leaseExpiresAt = 0;
202
+ this.tick();
203
+ }
204
+ /**
205
+ * Settles an external (image) node, standing in for a runner: succeeded
206
+ * with `kv`, or failed with `failureClass`.
207
+ */
208
+ completeNode(runId, nodeId, result = {}) {
209
+ const run = this.run(runId);
210
+ const node = run.nodes.get(nodeId);
211
+ if (!node || node.handler)
212
+ throw new Error(`FakePipelines.completeNode: ${nodeId} is not an external node of ${runId}`);
213
+ if (node.state !== NodeState.READY)
214
+ throw new Error(`FakePipelines.completeNode: ${nodeId} is not ready`);
215
+ if (result.failureClass) {
216
+ node.state = NodeState.FAILED;
217
+ node.failureClass = result.failureClass;
218
+ }
219
+ else {
220
+ node.state = NodeState.SUCCEEDED;
221
+ node.kv = result.kv ?? {};
222
+ }
223
+ this.advance(run);
224
+ }
225
+ /** Stops the fake's timer. */
226
+ close() {
227
+ clearInterval(this.timer);
228
+ this.changed.resolve();
229
+ }
230
+ // ---- the router -------------------------------------------------------
231
+ buildRouter(interceptors) {
232
+ return createRouterTransport((router) => {
233
+ router.service(WorkerService, {
234
+ claimAttempts: async (req, ctx) => {
235
+ const who = this.enter("ClaimAttempts", ctx, "pipelines:worker:edit");
236
+ if (req.selector.case !== "handlers")
237
+ throw new ConnectError("an SDK worker claims by handler", Code.PermissionDenied);
238
+ const sel = req.selector.value;
239
+ const pool = this.pools.get(sel.poolId);
240
+ const denied = new ConnectError("the caller's owner does not own that sdk pool or those handlers", Code.PermissionDenied);
241
+ if (!pool || pool.kind !== PoolKind.SDK || pool.owner?.tenant !== who.tenant)
242
+ throw denied;
243
+ for (const h of sel.handlers) {
244
+ if (this.namespaces.get(h.split("/")[0]) !== who.tenant || !pool.handlers.includes(h))
245
+ throw denied;
246
+ }
247
+ if (req.freeSlots < 1 || req.freeSlots > 256)
248
+ throw new ConnectError("free_slots: 1 to 256", Code.InvalidArgument);
249
+ const until = Date.now() + Math.min(req.wait ? durationMs(req.wait) : 30_000, 30_000);
250
+ // One timer and one abort listener for the whole hold.
251
+ const stop = Promise.race([sleepUntil(until), aborted(ctx.signal)]);
252
+ for (;;) {
253
+ const claimed = this.claim(pool, sel.handlers, sel.workerId, req.freeSlots);
254
+ if (claimed.length > 0 || Date.now() >= until || ctx.signal.aborted) {
255
+ return { attempts: claimed.map((a) => this.describe(a)) };
256
+ }
257
+ await Promise.race([this.changed.promise, stop]);
258
+ }
259
+ },
260
+ startAttempt: (req, ctx) => {
261
+ const who = this.enter("StartAttempt", ctx, "pipelines:worker:edit");
262
+ const a = this.held(who, req.ref);
263
+ const run = this.run(a.runId);
264
+ if (a.state === AttemptState.RUNNING)
265
+ return this.started(a);
266
+ if (a.state !== AttemptState.CLAIMED)
267
+ throw stale();
268
+ if (run.cancelRequestedAt !== undefined) {
269
+ throw new ConnectError("a cancel was requested: do not start the step", Code.FailedPrecondition);
270
+ }
271
+ a.state = AttemptState.RUNNING;
272
+ a.leaseExpiresAt = Date.now() + this.opts.leaseMs;
273
+ const node = run.nodes.get(a.nodeId);
274
+ node.state = NodeState.RUNNING;
275
+ a.startedAt = Date.now();
276
+ return this.started(a);
277
+ },
278
+ heartbeat: (req, ctx) => {
279
+ const who = this.enter("Heartbeat", ctx, "pipelines:worker:edit");
280
+ const a = this.held(who, req.ref);
281
+ if (a.state !== AttemptState.CLAIMED && a.state !== AttemptState.RUNNING)
282
+ throw stale();
283
+ a.leaseExpiresAt = Date.now() + this.opts.leaseMs;
284
+ const run = this.run(a.runId);
285
+ const cancel = run.cancelRequestedAt !== undefined;
286
+ return {
287
+ cancel,
288
+ cancelReason: cancel ? run.cancelReason || "the run was cancelled" : "",
289
+ leaseExpiresAt: timestampFromMs(a.leaseExpiresAt),
290
+ };
291
+ },
292
+ appendLogs: (req, ctx) => {
293
+ const who = this.enter("AppendLogs", ctx, "pipelines:worker:edit");
294
+ const a = this.held(who, req.ref);
295
+ if (a.state !== AttemptState.CLAIMED && a.state !== AttemptState.RUNNING)
296
+ throw stale();
297
+ const next = BigInt(a.chunks.length);
298
+ if (req.seq > next)
299
+ throw new ConnectError(`seq: the next expected seq is ${next}`, Code.OutOfRange);
300
+ if (req.data.length < 1 || req.data.length > 1 << 20)
301
+ throw new ConnectError("data: 1 byte to 1 MiB", Code.InvalidArgument);
302
+ if (req.seq === next && !a.logTruncated) {
303
+ a.chunks.push(req.data);
304
+ a.logFinal ||= req.final;
305
+ const size = a.chunks.reduce((n, c) => n + c.length, 0);
306
+ if (this.opts.logMaxBytes > 0 && size >= this.opts.logMaxBytes)
307
+ a.logTruncated = true;
308
+ }
309
+ return { nextSeq: BigInt(a.chunks.length), truncated: a.logTruncated };
310
+ },
311
+ completeAttempt: (req, ctx) => {
312
+ const who = this.enter("CompleteAttempt", ctx, "pipelines:worker:edit");
313
+ const a = this.held(who, req.ref);
314
+ if (a.completedAt !== undefined)
315
+ return { state: a.state };
316
+ if (a.state !== AttemptState.RUNNING)
317
+ throw stale();
318
+ if (req.termination === Termination.UNSPECIFIED)
319
+ throw new ConnectError("termination: required", Code.InvalidArgument);
320
+ if (req.message.length > 2048)
321
+ throw new ConnectError("message: at most 2048", Code.InvalidArgument);
322
+ if (req.handlerFailureKind !== HandlerFailureKind.UNSPECIFIED && req.termination !== Termination.HANDLER_ERROR) {
323
+ throw new ConnectError("handler_failure_kind is only for termination TERMINATION_HANDLER_ERROR", Code.InvalidArgument);
324
+ }
325
+ const kv = {};
326
+ const entries = Object.entries(req.kv);
327
+ if (entries.length > 100)
328
+ throw new ConnectError("kv: at most 100 pairs", Code.InvalidArgument);
329
+ for (const [k, v] of entries) {
330
+ if (k.length > 64 || !KV_KEY.test(k))
331
+ throw new ConnectError(`kv: ${k} is not a valid key`, Code.InvalidArgument);
332
+ kv[k] = toJson(ValueSchema, v);
333
+ }
334
+ const [state, failureClass] = outcome(req.termination, req.exitCode);
335
+ a.state = state;
336
+ a.failureClass = failureClass;
337
+ a.termination = req.termination;
338
+ a.exitCode = req.exitCode;
339
+ a.message = req.message;
340
+ a.handlerFailureKind = req.handlerFailureKind;
341
+ a.kv = state === AttemptState.SUCCEEDED ? kv : {};
342
+ a.completedAt = Date.now();
343
+ this.settle(a);
344
+ return { state: a.state };
345
+ },
346
+ releaseAttempt: (req, ctx) => {
347
+ const who = this.enter("ReleaseAttempt", ctx, "pipelines:worker:edit");
348
+ if (req.reason.length > 256)
349
+ throw new ConnectError("reason: at most 256", Code.InvalidArgument);
350
+ const a = this.held(who, req.ref);
351
+ if (a.state === AttemptState.RUNNING)
352
+ return { state: a.state };
353
+ if (a.state !== AttemptState.CLAIMED)
354
+ throw stale();
355
+ // CLAIMED -> OFFERED at once: the epoch is bumped, nothing spent.
356
+ a.epoch += 1n;
357
+ a.workerId = "";
358
+ a.poolId = "";
359
+ a.leaseExpiresAt = 0;
360
+ a.releases++;
361
+ if (this.run(a.runId).cancelRequestedAt !== undefined) {
362
+ a.state = AttemptState.CANCELLED;
363
+ a.failureClass = FailureClass.CANCELLED;
364
+ a.completedAt = Date.now();
365
+ this.settle(a);
366
+ }
367
+ else {
368
+ a.state = AttemptState.OFFERED;
369
+ a.notBefore = 0;
370
+ }
371
+ this.notify();
372
+ return { state: a.state };
373
+ },
374
+ });
375
+ router.service(RunsService, {
376
+ createJob: (req, ctx) => {
377
+ const who = this.enter("CreateJob", ctx, "pipelines:run:edit");
378
+ this.actsFor(who, req.tenant);
379
+ const existing = this.idempotency.get(`${who.product ?? who.tenant}\u0000${req.idempotencyKey}`);
380
+ if (existing)
381
+ return { run: this.runMessage(this.run(existing)), deduplicated: true };
382
+ this.requireHandler(req.handler);
383
+ const run = this.newRun(who, req.tenant, req.idempotencyKey, req.resource, {}, req.delay ? durationMs(req.delay) : 0);
384
+ if (req.retry && req.retry.max > 10)
385
+ throw new ConnectError("retry.max: at most 10", Code.InvalidArgument);
386
+ if (req.retry?.when.includes(FailureClass.CANCELLED))
387
+ throw new ConnectError("retry.when: CANCELLED is never retried", Code.InvalidArgument);
388
+ this.addNode(run, {
389
+ id: "job",
390
+ handler: req.handler,
391
+ uses: "",
392
+ with: req.payload ?? {},
393
+ needs: [],
394
+ retry: req.retry ? { max: req.retry.max, when: req.retry.when } : undefined,
395
+ timeoutMs: req.timeout ? durationMs(req.timeout) : 0,
396
+ interruptible: false,
397
+ });
398
+ this.advance(run);
399
+ return { run: this.runMessage(run), deduplicated: false };
400
+ },
401
+ createRun: (req, ctx) => {
402
+ const who = this.enter("CreateRun", ctx, "pipelines:run:edit");
403
+ this.actsFor(who, req.tenant);
404
+ const existing = this.idempotency.get(`${who.product ?? who.tenant}\u0000${req.idempotencyKey}`);
405
+ if (existing)
406
+ return { run: this.runMessage(this.run(existing)), deduplicated: true };
407
+ if (req.source.case !== "inlineDefinition")
408
+ throw new ConnectError("only inline definitions", Code.Unimplemented);
409
+ const def = req.source.value;
410
+ if (def.nodes.length === 0)
411
+ throw new ConnectError("definition: no nodes", Code.InvalidArgument);
412
+ const specs = def.nodes.map((n) => nodeSpec(n, (h) => this.requireHandler(h)));
413
+ checkGraph(specs);
414
+ const run = this.newRun(who, req.tenant, req.idempotencyKey, req.resource, req.variables ?? {}, 0);
415
+ for (const spec of specs)
416
+ this.addNode(run, spec);
417
+ this.advance(run);
418
+ return { run: this.runMessage(run), deduplicated: false };
419
+ },
420
+ getRun: (req, ctx) => {
421
+ const who = this.enter("GetRun", ctx, "pipelines:run:read");
422
+ return { run: this.runMessage(this.visible(who, req.runId)) };
423
+ },
424
+ getRunGraph: (req, ctx) => {
425
+ const who = this.enter("GetRun", ctx, "pipelines:run:read");
426
+ const run = this.visible(who, req.runId);
427
+ return { run: this.runMessage(run), nodes: [...run.nodes.values()].map((n) => this.nodeMessage(n)) };
428
+ },
429
+ listRuns: (req, ctx) => {
430
+ const who = this.enter("GetRun", ctx, "pipelines:run:read");
431
+ this.actsFor(who, req.tenant);
432
+ const runs = [...this.runs.values()]
433
+ .filter((r) => r.tenant === req.tenant && (req.states.length === 0 || req.states.includes(r.state)))
434
+ .filter((r) => !req.resource || (r.resource?.kind === req.resource.kind && r.resource.id === req.resource.id))
435
+ .reverse();
436
+ return { runs: runs.map((r) => this.runMessage(r)), nextCursor: "" };
437
+ },
438
+ cancelRun: (req, ctx) => {
439
+ const who = this.enter("CancelRun", ctx, "pipelines:run:edit");
440
+ const run = this.visible(who, req.runId);
441
+ this.cancel(run, req.reason);
442
+ return { run: this.runMessage(run) };
443
+ },
444
+ });
445
+ router.service(RegistryService, {
446
+ publishVersion: (req, ctx) => {
447
+ const who = this.enter("PublishVersion", ctx, "pipelines:package:edit");
448
+ const namespace = req.name.split("/")[0];
449
+ const owner = this.namespaces.get(namespace);
450
+ if (owner !== undefined && owner !== who.tenant) {
451
+ throw new ConnectError(`the ${namespace} namespace is not this team's`, Code.PermissionDenied);
452
+ }
453
+ if (req.tenant !== who.tenant)
454
+ throw new ConnectError("tenant: the caller does not act for it", Code.PermissionDenied);
455
+ if (!req.manifest)
456
+ throw new ConnectError("manifest: required", Code.InvalidArgument);
457
+ this.namespaces.set(namespace, who.tenant);
458
+ const pkg = this.packages.get(req.name) ?? { tenant: who.tenant, versions: new Map() };
459
+ const prior = pkg.versions.get(req.version);
460
+ if (prior && JSON.stringify(prior) !== JSON.stringify(req.manifest)) {
461
+ throw new ConnectError(`${req.name}@${req.version} exists with different content`, Code.AlreadyExists);
462
+ }
463
+ pkg.versions.set(req.version, req.manifest);
464
+ this.packages.set(req.name, pkg);
465
+ return { version: { name: req.name, version: req.version, manifest: req.manifest, state: VersionState.READY } };
466
+ },
467
+ });
468
+ router.service(PoolsService, {
469
+ setPool: (req, ctx) => {
470
+ const who = this.enter("SetPool", ctx, "pipelines:pool:edit");
471
+ const p = req.pool;
472
+ if (!p)
473
+ throw new ConnectError("pool: required", Code.InvalidArgument);
474
+ if (p.owner?.kind !== PoolOwnerKind.TENANT || p.owner.tenant !== who.tenant) {
475
+ throw new ConnectError("pool.owner: the caller does not act for it", Code.PermissionDenied);
476
+ }
477
+ if (p.kind === PoolKind.SDK && p.handlers.length === 0) {
478
+ throw new ConnectError("pool.handlers: an sdk pool names the handlers it serves", Code.InvalidArgument);
479
+ }
480
+ for (const h of p.handlers) {
481
+ if (this.namespaces.get(h.split("/")[0]) !== who.tenant) {
482
+ throw new ConnectError(`pool.handlers: the namespace of ${h} is not this team's`, Code.PermissionDenied);
483
+ }
484
+ }
485
+ const existing = [...this.pools.values()].find((x) => x.owner?.tenant === p.owner?.tenant && x.name === p.name);
486
+ const pool = create(PoolSchema, { ...p, id: existing?.id ?? randomUUID() });
487
+ this.pools.set(pool.id, pool);
488
+ this.notify();
489
+ return { pool, created: !existing };
490
+ },
491
+ });
492
+ }, { transport: { interceptors } });
493
+ }
494
+ // ---- auth -------------------------------------------------------------
495
+ enter(method, ctx, scope) {
496
+ const header = ctx.requestHeader.get("Authorization") ?? "";
497
+ const token = header.startsWith("Bearer ") ? header.slice(7) : "";
498
+ this.calls.push({ method, token });
499
+ const queue = this.injected.get(method);
500
+ const injected = queue?.shift();
501
+ if (injected)
502
+ throw injected;
503
+ const who = this.accounts.get(token);
504
+ if (!who)
505
+ throw new ConnectError("a valid token is required", Code.Unauthenticated);
506
+ if (!who.scopes.includes(scope))
507
+ throw new ConnectError(`the token lacks ${scope}`, Code.PermissionDenied);
508
+ return who;
509
+ }
510
+ actsFor(who, tenant) {
511
+ if (who.tenant !== tenant)
512
+ throw new ConnectError("the caller may not act for that tenant", Code.PermissionDenied);
513
+ }
514
+ visible(who, runId) {
515
+ const run = this.runs.get(runId);
516
+ if (!run || run.tenant !== who.tenant)
517
+ throw new ConnectError("no such run", Code.NotFound);
518
+ return run;
519
+ }
520
+ /** The server's held(): the attempt at this epoch, held by a pool of the caller's team. */
521
+ held(who, ref) {
522
+ if (!ref)
523
+ throw new ConnectError("ref: required", Code.InvalidArgument);
524
+ const a = this.attemptsById.get(ref.attemptId);
525
+ if (!a || a.epoch !== ref.claimEpoch || !a.poolId)
526
+ throw stale();
527
+ // Claimed by handler: the ref must name the worker recorded at the claim.
528
+ if (!ref.workerId || ref.workerId !== a.workerId)
529
+ throw stale();
530
+ if (this.pools.get(a.poolId)?.owner?.tenant !== who.tenant)
531
+ throw stale();
532
+ return a;
533
+ }
534
+ requireHandler(handler) {
535
+ const pkg = this.packages.get(handler);
536
+ if (!pkg || pkg.versions.size === 0 || [...pkg.versions.values()].some((m) => m.kind !== PackageKind.WORKER)) {
537
+ throw new ConnectError(`handler ${handler} is not a published worker package`, Code.InvalidArgument);
538
+ }
539
+ }
540
+ // ---- the scheduler ----------------------------------------------------
541
+ newRun(who, tenant, key, resource, variables, delayMs) {
542
+ const now = Date.now();
543
+ const run = {
544
+ id: randomUUID(),
545
+ tenant,
546
+ product: who.product ?? who.tenant,
547
+ idempotencyKey: key,
548
+ createdBy: `urn:fake:sa:${who.tenant}`,
549
+ resource: resource ? { kind: resource.kind, id: resource.id, ...(resource.json ? { json: resource.json } : {}) } : undefined,
550
+ variables,
551
+ state: RunState.QUEUED,
552
+ failureClass: FailureClass.UNSPECIFIED,
553
+ failedNodeId: "",
554
+ notBefore: now + delayMs,
555
+ createdAt: now,
556
+ startedAt: undefined,
557
+ finishedAt: undefined,
558
+ cancelRequestedAt: undefined,
559
+ cancelReason: "",
560
+ nodes: new Map(),
561
+ };
562
+ this.runs.set(run.id, run);
563
+ this.idempotency.set(`${run.product}\u0000${key}`, run.id);
564
+ return run;
565
+ }
566
+ addNode(run, spec) {
567
+ run.nodes.set(spec.id, {
568
+ id: spec.id,
569
+ handler: spec.handler,
570
+ uses: spec.uses,
571
+ state: NodeState.PENDING,
572
+ failureClass: FailureClass.UNSPECIFIED,
573
+ kv: {},
574
+ attempts: [],
575
+ needs: spec.needs,
576
+ with: spec.with,
577
+ retry: spec.retry ?? { max: this.opts.defaultRetries, when: [] },
578
+ timeoutMs: spec.timeoutMs || this.opts.defaultTimeoutMs,
579
+ interruptible: spec.interruptible,
580
+ });
581
+ }
582
+ /** Makes ready what can be, skips what cannot run, and settles the run. */
583
+ advance(run) {
584
+ if (terminal(run.state))
585
+ return;
586
+ if (run.notBefore > Date.now() && run.cancelRequestedAt === undefined)
587
+ return;
588
+ for (let changed = true; changed;) {
589
+ changed = false;
590
+ for (const n of run.nodes.values()) {
591
+ if (n.state !== NodeState.PENDING)
592
+ continue;
593
+ const needs = n.needs.map((id) => run.nodes.get(id));
594
+ if (run.cancelRequestedAt !== undefined) {
595
+ n.state = NodeState.SKIPPED;
596
+ changed = true;
597
+ }
598
+ else if (needs.some((x) => x.state === NodeState.FAILED || x.state === NodeState.SKIPPED || x.state === NodeState.CANCELLED)) {
599
+ n.state = NodeState.SKIPPED;
600
+ changed = true;
601
+ }
602
+ else if (needs.every((x) => x.state === NodeState.SUCCEEDED)) {
603
+ n.state = NodeState.READY;
604
+ run.startedAt ??= Date.now();
605
+ if (run.state === RunState.QUEUED)
606
+ run.state = RunState.RUNNING;
607
+ if (n.handler) {
608
+ const inputs = this.resolve(run, n);
609
+ if (typeof inputs === "string") {
610
+ n.state = NodeState.FAILED;
611
+ n.failureClass = FailureClass.INVALID_IO;
612
+ }
613
+ else {
614
+ this.offer(run, n, inputs, 0);
615
+ }
616
+ }
617
+ changed = true;
618
+ }
619
+ }
620
+ }
621
+ const nodes = [...run.nodes.values()];
622
+ if (nodes.every((n) => terminalNode(n.state))) {
623
+ run.finishedAt = Date.now();
624
+ const failed = nodes.find((n) => n.state === NodeState.FAILED);
625
+ if (run.cancelRequestedAt !== undefined) {
626
+ run.state = RunState.CANCELLED;
627
+ }
628
+ else if (failed) {
629
+ run.state = RunState.FAILED;
630
+ run.failureClass = failed.failureClass;
631
+ run.failedNodeId = failed.id;
632
+ }
633
+ else {
634
+ run.state = RunState.SUCCEEDED;
635
+ }
636
+ }
637
+ }
638
+ resolve(run, n) {
639
+ const out = {};
640
+ for (const [k, v] of Object.entries(n.with)) {
641
+ const m = typeof v === "string" ? REF.exec(v) : null;
642
+ if (!m) {
643
+ out[k] = v;
644
+ continue;
645
+ }
646
+ const path = m[1].split(".");
647
+ if ((path[0] === "steps" || path[0] === "needs") && path[2] === "outputs" && path[3] === "kv" && path[4]) {
648
+ const value = run.nodes.get(path[1])?.kv[path[4]];
649
+ if (value === undefined)
650
+ return `${m[1]} does not resolve`;
651
+ out[k] = value;
652
+ }
653
+ else if (path[0] === "variables" && path[1] && run.variables[path[1]] !== undefined) {
654
+ out[k] = run.variables[path[1]];
655
+ }
656
+ else {
657
+ return `${m[1]} does not resolve`;
658
+ }
659
+ }
660
+ return out;
661
+ }
662
+ offer(run, n, inputs, delayMs) {
663
+ const a = {
664
+ id: randomUUID(),
665
+ runId: run.id,
666
+ nodeId: n.id,
667
+ number: n.attempts.length + 1,
668
+ state: AttemptState.OFFERED,
669
+ epoch: 0n,
670
+ workerId: "",
671
+ poolId: "",
672
+ leaseExpiresAt: 0,
673
+ notBefore: Date.now() + delayMs,
674
+ inputs,
675
+ termination: Termination.UNSPECIFIED,
676
+ failureClass: FailureClass.UNSPECIFIED,
677
+ exitCode: undefined,
678
+ message: "",
679
+ kv: {},
680
+ chunks: [],
681
+ logFinal: false,
682
+ logTruncated: false,
683
+ completedAt: undefined,
684
+ handlerFailureKind: HandlerFailureKind.UNSPECIFIED,
685
+ releases: 0,
686
+ };
687
+ n.attempts.push(a);
688
+ this.attemptsById.set(a.id, a);
689
+ this.notify();
690
+ }
691
+ claim(pool, handlers, workerId, max) {
692
+ const now = Date.now();
693
+ const out = [];
694
+ for (const run of this.runs.values()) {
695
+ if (run.tenant !== pool.owner?.tenant || run.cancelRequestedAt !== undefined || run.notBefore > now)
696
+ continue;
697
+ for (const n of run.nodes.values()) {
698
+ if (!handlers.includes(n.handler))
699
+ continue;
700
+ for (const a of n.attempts) {
701
+ if (out.length >= max)
702
+ return out;
703
+ if (a.state !== AttemptState.OFFERED || a.notBefore > now)
704
+ continue;
705
+ a.state = AttemptState.CLAIMED;
706
+ a.epoch += 1n;
707
+ a.workerId = workerId;
708
+ a.poolId = pool.id;
709
+ a.leaseExpiresAt = now + this.opts.leaseMs;
710
+ out.push(a);
711
+ }
712
+ }
713
+ }
714
+ return out;
715
+ }
716
+ describe(a) {
717
+ const run = this.run(a.runId);
718
+ const n = run.nodes.get(a.nodeId);
719
+ const pkg = this.packages.get(n.handler);
720
+ const version = pkg ? [...pkg.versions.keys()].at(-1) : "0.0.0";
721
+ return create(ClaimedAttemptSchema, {
722
+ ref: { attemptId: a.id, claimEpoch: a.epoch, workerId: a.workerId },
723
+ runId: run.id,
724
+ nodeId: n.id,
725
+ number: a.number,
726
+ tenant: run.tenant,
727
+ leaseExpiresAt: timestampFromMs(a.leaseExpiresAt),
728
+ heartbeatInterval: durationFromMs(this.opts.heartbeatIntervalMs),
729
+ package: { name: n.handler, version },
730
+ ...(pkg?.versions.get(version) ? { manifest: pkg.versions.get(version) } : {}),
731
+ inputs: a.inputs,
732
+ ...(n.timeoutMs > 0 ? { timeout: durationFromMs(n.timeoutMs) } : {}),
733
+ cancelGrace: durationFromMs(this.opts.cancelGraceMs),
734
+ ...(run.resource ? { resource: run.resource } : {}),
735
+ variables: run.variables,
736
+ runStartedAt: timestampFromMs(run.startedAt ?? Date.now()),
737
+ logMaxBytes: BigInt(this.opts.logMaxBytes),
738
+ });
739
+ }
740
+ started(a) {
741
+ const n = this.run(a.runId).nodes.get(a.nodeId);
742
+ const startedAt = a.startedAt ?? Date.now();
743
+ return {
744
+ attemptTokenSecret: `fake-attempt-token.${a.id}.${a.epoch}`,
745
+ ...(n.timeoutMs > 0
746
+ ? {
747
+ deadline: timestampFromMs(startedAt + n.timeoutMs),
748
+ tokenExpiresAt: timestampFromMs(startedAt + n.timeoutMs + this.opts.cancelGraceMs),
749
+ }
750
+ : { tokenExpiresAt: timestampFromMs(Date.now() + 86_400_000) }),
751
+ };
752
+ }
753
+ /** After an attempt ends: settle the node, or offer a retry. */
754
+ settle(a) {
755
+ const run = this.run(a.runId);
756
+ const n = run.nodes.get(a.nodeId);
757
+ if (a.state === AttemptState.SUCCEEDED) {
758
+ n.state = NodeState.SUCCEEDED;
759
+ n.kv = a.kv;
760
+ }
761
+ else if (a.state === AttemptState.CANCELLED || run.cancelRequestedAt !== undefined) {
762
+ n.state = NodeState.CANCELLED;
763
+ n.failureClass = FailureClass.CANCELLED;
764
+ }
765
+ else {
766
+ const permanent = a.failureClass === FailureClass.HANDLER_ERROR && a.handlerFailureKind === HandlerFailureKind.PERMANENT;
767
+ const transientHandler = a.failureClass === FailureClass.HANDLER_ERROR && a.handlerFailureKind === HandlerFailureKind.TRANSIENT;
768
+ const when = n.retry.when.length > 0 ? n.retry.when : transientHandler ? [...TRANSIENT, FailureClass.HANDLER_ERROR] : TRANSIENT;
769
+ // On an interruptible node an interruption is free, up to a bound.
770
+ const free = (x) => n.interruptible && x.failureClass === FailureClass.INTERRUPTED;
771
+ const freeUsed = n.attempts.filter(free).length;
772
+ if (free(a) && freeUsed <= FREE_INTERRUPTIONS) {
773
+ this.offer(run, n, a.inputs, 0);
774
+ return;
775
+ }
776
+ const retries = n.attempts.slice(0, -1).filter((x) => !free(x)).length;
777
+ if (!permanent && when.includes(a.failureClass) && a.failureClass !== FailureClass.CANCELLED && retries < n.retry.max) {
778
+ this.offer(run, n, a.inputs, this.opts.retryBackoffMs);
779
+ return;
780
+ }
781
+ n.state = NodeState.FAILED;
782
+ n.failureClass = a.failureClass;
783
+ }
784
+ this.advance(run);
785
+ this.notify();
786
+ }
787
+ cancel(run, reason) {
788
+ if (terminal(run.state) || run.cancelRequestedAt !== undefined)
789
+ return;
790
+ run.cancelRequestedAt = Date.now();
791
+ run.cancelReason = reason;
792
+ run.state = RunState.CANCELLING;
793
+ for (const n of run.nodes.values()) {
794
+ for (const a of n.attempts) {
795
+ if (a.state === AttemptState.OFFERED) {
796
+ a.state = AttemptState.CANCELLED;
797
+ a.failureClass = FailureClass.CANCELLED;
798
+ a.completedAt = Date.now();
799
+ }
800
+ }
801
+ const live = n.attempts.some((a) => a.state === AttemptState.CLAIMED || a.state === AttemptState.RUNNING);
802
+ if (!terminalNode(n.state) && !live) {
803
+ n.state = n.state === NodeState.PENDING ? NodeState.SKIPPED : NodeState.CANCELLED;
804
+ }
805
+ }
806
+ this.advance(run);
807
+ this.notify();
808
+ }
809
+ /** The reaper and the delay clock. */
810
+ tick() {
811
+ const now = Date.now();
812
+ for (const run of this.runs.values()) {
813
+ if (run.state === RunState.QUEUED && run.notBefore <= now && run.cancelRequestedAt === undefined)
814
+ this.advance(run);
815
+ for (const n of run.nodes.values()) {
816
+ for (const a of n.attempts) {
817
+ if (a.leaseExpiresAt > now)
818
+ continue;
819
+ if (a.state === AttemptState.CLAIMED) {
820
+ // Nothing ran: offered again, epoch bumped, no retry spent.
821
+ a.state = run.cancelRequestedAt !== undefined ? AttemptState.CANCELLED : AttemptState.OFFERED;
822
+ a.epoch += 1n;
823
+ a.workerId = "";
824
+ a.poolId = "";
825
+ if (a.state === AttemptState.CANCELLED) {
826
+ a.completedAt = now;
827
+ this.settle(a);
828
+ }
829
+ }
830
+ else if (a.state === AttemptState.RUNNING) {
831
+ a.state = AttemptState.ABANDONED;
832
+ a.failureClass = FailureClass.ABANDONED;
833
+ a.completedAt = now;
834
+ this.settle(a);
835
+ }
836
+ }
837
+ }
838
+ }
839
+ this.notify();
840
+ }
841
+ notify() {
842
+ const prev = this.changed;
843
+ this.changed = signal();
844
+ prev.resolve();
845
+ }
846
+ // ---- messages ---------------------------------------------------------
847
+ runMessage(r) {
848
+ return create(RunSchema, {
849
+ id: r.id,
850
+ tenant: r.tenant,
851
+ product: r.product,
852
+ idempotencyKey: r.idempotencyKey,
853
+ ...(r.resource ? { resource: r.resource } : {}),
854
+ variables: r.variables,
855
+ state: r.state,
856
+ failureClass: r.failureClass,
857
+ failedNodeId: r.failedNodeId,
858
+ createdBy: r.createdBy,
859
+ createdAt: timestampFromMs(r.createdAt),
860
+ ...(r.notBefore > r.createdAt ? { notBefore: timestampFromMs(r.notBefore) } : {}),
861
+ ...(r.startedAt ? { startedAt: timestampFromMs(r.startedAt) } : {}),
862
+ ...(r.finishedAt ? { finishedAt: timestampFromMs(r.finishedAt) } : {}),
863
+ ...(r.cancelRequestedAt ? { cancelRequestedAt: timestampFromMs(r.cancelRequestedAt) } : {}),
864
+ cancelReason: r.cancelReason,
865
+ });
866
+ }
867
+ nodeMessage(n) {
868
+ const settled = n.attempts.find((a) => a.state === AttemptState.SUCCEEDED) ?? n.attempts.at(-1);
869
+ return create(NodeRunSchema, {
870
+ nodeId: n.id,
871
+ state: n.state,
872
+ failureClass: n.failureClass,
873
+ settledAttemptId: terminalNode(n.state) ? (settled?.id ?? "") : "",
874
+ kv: Object.fromEntries(Object.entries(n.kv).map(([k, v]) => [k, jsonValue(v)])),
875
+ attempts: n.attempts.map((a) => create(AttemptSchema, {
876
+ id: a.id,
877
+ runId: a.runId,
878
+ nodeId: a.nodeId,
879
+ number: a.number,
880
+ state: a.state,
881
+ poolId: a.poolId,
882
+ runnerId: a.workerId,
883
+ failureClass: a.failureClass,
884
+ termination: a.termination,
885
+ ...(a.exitCode !== undefined ? { exitCode: a.exitCode } : {}),
886
+ message: a.message,
887
+ logBytes: BigInt(a.chunks.reduce((s, c) => s + c.length, 0)),
888
+ logTruncated: a.logTruncated,
889
+ })),
890
+ });
891
+ }
892
+ }
893
+ /**
894
+ * Holds a definition to the server's rules the fake models: unique ids,
895
+ * known needs, no cycle, and a reference names only a node in the referring
896
+ * node's dependency closure (a reference adds no edge).
897
+ */
898
+ function checkGraph(specs) {
899
+ const byId = new Map();
900
+ for (const s of specs) {
901
+ if (byId.has(s.id))
902
+ throw new ConnectError(`node ${s.id}: duplicate id`, Code.InvalidArgument);
903
+ byId.set(s.id, s);
904
+ }
905
+ const closure = (id, seen = new Set(), path = new Set()) => {
906
+ for (const need of byId.get(id).needs) {
907
+ if (!byId.has(need))
908
+ throw new ConnectError(`node ${id}: unknown need ${need}`, Code.InvalidArgument);
909
+ if (path.has(need) || need === id)
910
+ throw new ConnectError(`node ${id}: the needs form a cycle`, Code.InvalidArgument);
911
+ if (seen.has(need))
912
+ continue;
913
+ seen.add(need);
914
+ closure(need, seen, new Set([...path, id]));
915
+ }
916
+ return seen;
917
+ };
918
+ for (const s of specs) {
919
+ const reachable = closure(s.id);
920
+ for (const v of Object.values(s.with)) {
921
+ const m = typeof v === "string" ? REF.exec(v) : null;
922
+ const node = m?.[1]?.match(/^(?:steps|needs)\.([^.]+)\./)?.[1];
923
+ if (node && !reachable.has(node)) {
924
+ throw new ConnectError(`node ${s.id}: ${m[1]} names a node outside its needs`, Code.InvalidArgument);
925
+ }
926
+ }
927
+ }
928
+ }
929
+ function nodeSpec(n, requireHandler) {
930
+ let handler = "";
931
+ let uses = "";
932
+ if (n.action.case === "handler") {
933
+ handler = n.action.value;
934
+ requireHandler(handler);
935
+ }
936
+ else if (n.action.case === "uses") {
937
+ uses = `${n.action.value.name}@${n.action.value.version}`;
938
+ }
939
+ else {
940
+ throw new ConnectError(`node ${n.id}: only handler and uses nodes`, Code.Unimplemented);
941
+ }
942
+ return {
943
+ id: n.id,
944
+ handler,
945
+ uses,
946
+ with: Object.fromEntries(Object.entries(n.with).map(([k, v]) => [k, toJson(ValueSchema, v)])),
947
+ needs: n.needs.map((x) => x.node),
948
+ retry: n.retry ? { max: n.retry.max, when: n.retry.when } : undefined,
949
+ timeoutMs: n.timeout ? durationMs(n.timeout) : 0,
950
+ interruptible: n.interruptible,
951
+ };
952
+ }
953
+ /** The server's dispatch.Outcome. */
954
+ function outcome(t, exitCode) {
955
+ switch (t) {
956
+ case Termination.EXITED:
957
+ return exitCode === undefined || exitCode === 0
958
+ ? [AttemptState.SUCCEEDED, FailureClass.UNSPECIFIED]
959
+ : [AttemptState.FAILED, FailureClass.EXIT_CODE];
960
+ case Termination.OOM_KILLED:
961
+ return [AttemptState.FAILED, FailureClass.OOM];
962
+ case Termination.TIMED_OUT:
963
+ return [AttemptState.FAILED, FailureClass.TIMEOUT];
964
+ case Termination.CANCELLED:
965
+ return [AttemptState.CANCELLED, FailureClass.CANCELLED];
966
+ case Termination.IMAGE_PULL_FAILED:
967
+ return [AttemptState.FAILED, FailureClass.IMAGE_PULL];
968
+ case Termination.STAGING_FAILED:
969
+ return [AttemptState.FAILED, FailureClass.STAGING_FAILED];
970
+ case Termination.SECRETS_FAILED:
971
+ return [AttemptState.FAILED, FailureClass.SECRETS_UNAVAILABLE];
972
+ case Termination.HANDLER_ERROR:
973
+ return [AttemptState.FAILED, FailureClass.HANDLER_ERROR];
974
+ case Termination.INTERRUPTED:
975
+ return [AttemptState.FAILED, FailureClass.INTERRUPTED];
976
+ default:
977
+ return [AttemptState.FAILED, FailureClass.RUNNER_CRASHED];
978
+ }
979
+ }
980
+ function stale() {
981
+ return new ConnectError("the attempt is not held by this caller at this claim epoch: stop it and report nothing more", Code.FailedPrecondition);
982
+ }
983
+ function terminal(s) {
984
+ return s === RunState.SUCCEEDED || s === RunState.FAILED || s === RunState.CANCELLED;
985
+ }
986
+ function terminalNode(s) {
987
+ return (s === NodeState.SUCCEEDED ||
988
+ s === NodeState.FAILED ||
989
+ s === NodeState.FAILED_ALLOWED ||
990
+ s === NodeState.SKIPPED ||
991
+ s === NodeState.CANCELLED);
992
+ }
993
+ function jsonValue(v) {
994
+ return fromJson(ValueSchema, v);
995
+ }
996
+ function signal() {
997
+ let resolve;
998
+ const promise = new Promise((r) => (resolve = r));
999
+ return { promise, resolve };
1000
+ }
1001
+ function sleepUntil(at) {
1002
+ return new Promise((r) => {
1003
+ const t = setTimeout(r, Math.max(0, at - Date.now()));
1004
+ t.unref();
1005
+ });
1006
+ }
1007
+ function aborted(s) {
1008
+ return new Promise((r) => {
1009
+ if (s.aborted)
1010
+ r();
1011
+ else
1012
+ s.addEventListener("abort", () => r(), { once: true });
1013
+ });
1014
+ }
1015
+ //# sourceMappingURL=index.js.map