@operad-io/harness-process 0.1.0 → 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.
package/src/keep.ts ADDED
@@ -0,0 +1,646 @@
1
+ /**
2
+ * The line channel of a harness child and the part of its life that serves
3
+ * more than one reply.
4
+ *
5
+ * Every harness worker reads one JSON object from stdin, answers with JSON
6
+ * lines, and may be kept after a done reply. The socket, the record, the idle
7
+ * timer and the stop request are the same for every harness; only the program
8
+ * that runs one prompt and the shape of its Result differ, so those are the
9
+ * adapter this module calls.
10
+ */
11
+
12
+ import { messageOf } from "@operad-io/engine";
13
+ import { createHash } from "node:crypto";
14
+ import { chmodSync, rmSync } from "node:fs";
15
+ import { createServer, type Socket } from "node:net";
16
+ import {
17
+ removeKeptWorker,
18
+ writeKeptWorker,
19
+ } from "@operad-io/engine/platform";
20
+ import { messageText } from "./inbox.ts";
21
+ import type { AskQuestion, AskQuestionAnswer, OutwardWrite } from "@operad-io/engine";
22
+ import type { CallAnswer, FormAnswer } from "./ask-call.ts";
23
+
24
+ export const EXIT_FAILED = 1;
25
+ export const EXIT_INVALID_INPUT = 2;
26
+ export const EXIT_CANCELLED = 143;
27
+
28
+
29
+ /** A stable JSON value: object keys sorted, arrays in order. */
30
+ function canonical(value: unknown): unknown {
31
+ if (Array.isArray(value)) return value.map(canonical);
32
+ if (typeof value === "object" && value !== null) {
33
+ return Object.fromEntries(
34
+ Object.keys(value)
35
+ .sort()
36
+ .map((key) => [key, canonical(Reflect.get(value, key))]),
37
+ );
38
+ }
39
+ return value;
40
+ }
41
+
42
+ /**
43
+ * What a worker is started with, apart from one reply: a worker started with
44
+ * another model, effort, directory, access profile, skills or MCP servers is
45
+ * not the same worker. One-time grants and the prompt belong to a reply, so
46
+ * they do not name the worker.
47
+ */
48
+ export function keepKeyOf(config: Readonly<Record<string, unknown>>): string {
49
+ const { prompt: _prompt, images: _images, resumeSession: _resume, ...rest } = config;
50
+ const accessValue = rest["access"];
51
+ const access =
52
+ typeof accessValue === "object" && accessValue !== null
53
+ ? { ...(accessValue as Record<string, unknown>) }
54
+ : accessValue;
55
+ if (typeof access === "object" && access !== null) {
56
+ delete (access as Record<string, unknown>)["grants"];
57
+ }
58
+ return createHash("sha256")
59
+ .update(JSON.stringify(canonical({ ...rest, access })))
60
+ .digest("hex");
61
+ }
62
+
63
+ /** How a chat worker stays reachable for its next prompt. */
64
+ export interface WorkerKeep {
65
+ readonly socket: string;
66
+ readonly meta: string;
67
+ readonly token: string;
68
+ readonly scope: string;
69
+ readonly key: string;
70
+ /** How long the worker stays after a reply before it ends itself. */
71
+ readonly timeoutMs: number;
72
+ }
73
+
74
+ /** Where the program talks to the world; the process entry points them at stdout and stderr. */
75
+ export interface WorkerPorts {
76
+ write(text: string): void;
77
+ log(text: string): void;
78
+ }
79
+
80
+ /** Where the program writes. Lines are written whole, in order. */
81
+ export interface WorkerIo<TLine = unknown> {
82
+ line(line: TLine): void;
83
+ /** Free text for humans; never parsed. */
84
+ log(text: string): void;
85
+ /**
86
+ * Asks the host to use up a one-time permission. True when it did and the
87
+ * call may run. Absent when the host cannot answer; no grant then applies.
88
+ */
89
+ useGrant?(waitId: string): Promise<boolean>;
90
+ /**
91
+ * Asks the host to ask a person whether one call may run, and resolves with
92
+ * the answer. Absent when the host cannot answer; a call that must ask is then
93
+ * refused.
94
+ */
95
+ ask?(call: {
96
+ readonly kind: "command" | "path";
97
+ readonly value: string;
98
+ readonly tool?: string;
99
+ readonly shown: string;
100
+ readonly session: string | null;
101
+ readonly outward?: OutwardWrite | undefined;
102
+ }): Promise<CallAnswer>;
103
+ /**
104
+ * Asks the host to put one form to a person (an agent's own question) and
105
+ * resolves with the answers. Absent when the host cannot ask a form.
106
+ */
107
+ askForm?(form: {
108
+ readonly questions: readonly AskQuestion[];
109
+ readonly session: string | null;
110
+ }): Promise<FormAnswer>;
111
+ /**
112
+ * Hands each message the host sends mid-run to `handler`, those that came
113
+ * before the call first. Absent when the host cannot send any.
114
+ */
115
+ onMessage?(handler: (text: string) => void): void;
116
+ }
117
+
118
+ export type ParsedInput<TInput> =
119
+ | { readonly ok: true; readonly input: TInput }
120
+ | { readonly ok: false; readonly message: string };
121
+
122
+ /** What a harness supplies; everything else is the same for every worker. */
123
+ export interface WorkerAdapter<TInput> {
124
+ /** Reads the program's input shape; the message names the field at fault. */
125
+ parse(text: string): ParsedInput<TInput>;
126
+ /** The line an invalid input writes. */
127
+ invalidInput(message: string): unknown;
128
+ /** The line the program writes when the harness itself failed. */
129
+ error(message: string): unknown;
130
+ /** The keep request in an input, if it asks for one. */
131
+ keep(input: TInput): WorkerKeep | undefined;
132
+ /** Runs one reply and writes its lines; returns the exit code. */
133
+ run(input: TInput, io: WorkerIo<unknown>, cancel: AbortSignal): Promise<number>;
134
+ /** Releases what outlives one reply, when this worker process ends. */
135
+ close?(): void;
136
+ /** The session the next reply continues, as the kept record reports it. */
137
+ session?(): string | null;
138
+ /** Remembers the session a done reply reports. */
139
+ setSession?(session: string | null): void;
140
+ /** Called when the kept session ends by itself, so the worker can leave too. */
141
+ onEnded?(handler: (code: number) => void): void;
142
+ }
143
+
144
+ /** One Result line as far as keeping cares: its name and session. */
145
+ interface KeptResult {
146
+ readonly result: "done" | "failed" | "cancelled";
147
+ readonly sessionId: string | null;
148
+ }
149
+
150
+ function keptResultOf(line: unknown): KeptResult | undefined {
151
+ if (typeof line !== "object" || line === null) return undefined;
152
+ if (Reflect.get(line, "type") !== "result") return undefined;
153
+ const name: unknown = Reflect.get(line, "result");
154
+ if (name !== "done" && name !== "failed" && name !== "cancelled") return undefined;
155
+ const session: unknown = Reflect.get(line, "sessionId");
156
+ return { result: name, sessionId: typeof session === "string" ? session : null };
157
+ }
158
+
159
+ /** Wraps an io and remembers the last Result written through it. */
160
+ class Capture implements WorkerIo<unknown> {
161
+ private last: KeptResult | undefined;
162
+
163
+ constructor(private readonly inner: WorkerIo<unknown>) {}
164
+
165
+ get result(): KeptResult | undefined {
166
+ return this.last;
167
+ }
168
+
169
+ line(line: unknown): void {
170
+ const result = keptResultOf(line);
171
+ if (result !== undefined) this.last = result;
172
+ this.inner.line(line);
173
+ }
174
+
175
+ log(text: string): void {
176
+ this.inner.log(text);
177
+ }
178
+
179
+ useGrant = (waitId: string): Promise<boolean> => {
180
+ return this.inner.useGrant?.(waitId) ?? Promise.resolve(false);
181
+ };
182
+
183
+ ask = (call: Parameters<NonNullable<WorkerIo["ask"]>>[0]): Promise<CallAnswer> => {
184
+ return this.inner.ask?.(call) ?? Promise.resolve({ decision: "deny", reason: "the host cannot ask" });
185
+ };
186
+
187
+ askForm = (form: Parameters<NonNullable<WorkerIo["askForm"]>>[0]): Promise<FormAnswer> => {
188
+ return this.inner.askForm?.(form) ?? Promise.resolve({ decision: "rejected" });
189
+ };
190
+
191
+ onMessage = (handler: (text: string) => void): void => {
192
+ this.inner.onMessage?.(handler);
193
+ };
194
+ }
195
+
196
+ /**
197
+ * Stdin or a socket as lines. The first line is the input; later lines are the
198
+ * host's answers.
199
+ */
200
+ class StdinLines {
201
+ readonly #reader: AsyncIterator<Uint8Array>;
202
+ readonly #decoder = new TextDecoder();
203
+ #buffer = "";
204
+ #done = false;
205
+
206
+ constructor(stream: AsyncIterable<Uint8Array>) {
207
+ this.#reader = stream[Symbol.asyncIterator]();
208
+ }
209
+
210
+ /** Stops reading; a read that waits ends as if stdin had ended. */
211
+ close(): void {
212
+ void this.#reader.return?.()?.catch(() => undefined);
213
+ }
214
+
215
+ async #more(): Promise<boolean> {
216
+ if (this.#done) return false;
217
+ const chunk = await this.#reader.next();
218
+ if (chunk.done) {
219
+ this.#done = true;
220
+ this.#buffer += this.#decoder.decode();
221
+ return false;
222
+ }
223
+ this.#buffer += this.#decoder.decode(chunk.value, { stream: true });
224
+ return true;
225
+ }
226
+
227
+ /** The next line, or what is left at the end of the stream; undefined when nothing is. */
228
+ async next(): Promise<string | undefined> {
229
+ for (;;) {
230
+ const newline = this.#buffer.indexOf("\n");
231
+ if (newline !== -1) {
232
+ const line = this.#buffer.slice(0, newline);
233
+ this.#buffer = this.#buffer.slice(newline + 1);
234
+ if (line.trim().length === 0) continue;
235
+ return line;
236
+ }
237
+ if (!(await this.#more())) {
238
+ const rest = this.#buffer;
239
+ this.#buffer = "";
240
+ return rest.trim().length === 0 ? undefined : rest;
241
+ }
242
+ }
243
+ }
244
+
245
+ async first(): Promise<string> {
246
+ return (await this.next()) ?? "";
247
+ }
248
+ }
249
+
250
+ /** One line channel of a worker: stdin for the first reply, a socket for a kept one. */
251
+ export class Channel<TLine = unknown> {
252
+ readonly io: WorkerIo<TLine>;
253
+ private readonly lines: StdinLines;
254
+ private readonly waiting = new Map<number, (used: boolean) => void>();
255
+ private readonly questions = new Map<number, (answer: CallAnswer) => void>();
256
+ private readonly forms = new Map<number, (answer: FormAnswer) => void>();
257
+ private readonly early: string[] = [];
258
+ private readonly onGone: (() => void) | undefined;
259
+ private onMessage: ((text: string) => void) | undefined;
260
+ private nextAsk = 1;
261
+ private nextGrant = 1;
262
+ private hostGone = false;
263
+ private started = false;
264
+
265
+ constructor(
266
+ stream: AsyncIterable<Uint8Array>,
267
+ ports: WorkerPorts,
268
+ onGone?: (() => void) | undefined,
269
+ ) {
270
+ this.lines = new StdinLines(stream);
271
+ this.onGone = onGone;
272
+ const write = ports.write;
273
+ this.io = {
274
+ line: (line) => write(`${JSON.stringify(line)}\n`),
275
+ onMessage: (handler) => {
276
+ this.onMessage = handler;
277
+ for (const text of this.early.splice(0)) handler(text);
278
+ },
279
+ log: (text) => ports.log(text),
280
+ useGrant: (waitId) =>
281
+ new Promise<boolean>((resolve) => {
282
+ if (this.hostGone) {
283
+ resolve(false);
284
+ return;
285
+ }
286
+ const id = this.nextGrant;
287
+ this.nextGrant += 1;
288
+ this.waiting.set(id, resolve);
289
+ write(`${JSON.stringify({ type: "grant_use", id, waitId })}\n`);
290
+ }),
291
+ ask: (call) =>
292
+ new Promise<CallAnswer>((resolve) => {
293
+ if (this.hostGone) {
294
+ resolve({ decision: "deny", reason: "the host is gone" });
295
+ return;
296
+ }
297
+ const id = this.nextAsk;
298
+ this.nextAsk += 1;
299
+ this.questions.set(id, (answer) => {
300
+ // The line that closes the question, so the host's stall timer runs again.
301
+ write(`${JSON.stringify({ type: "ask_resolved", id })}\n`);
302
+ resolve(answer);
303
+ });
304
+ write(`${JSON.stringify({ type: "ask_call", id, ...call })}\n`);
305
+ }),
306
+ askForm: (form) =>
307
+ new Promise<FormAnswer>((resolve) => {
308
+ if (this.hostGone) {
309
+ resolve({ decision: "rejected" });
310
+ return;
311
+ }
312
+ const id = this.nextAsk;
313
+ this.nextAsk += 1;
314
+ this.forms.set(id, (answer) => {
315
+ write(`${JSON.stringify({ type: "ask_resolved", id })}\n`);
316
+ resolve(answer);
317
+ });
318
+ write(`${JSON.stringify({ type: "ask_form", id, questions: form.questions, session: form.session })}\n`);
319
+ }),
320
+ };
321
+ }
322
+
323
+ /** The first line, read before the host may send answers or messages. */
324
+ next(): Promise<string | undefined> {
325
+ return this.lines.next();
326
+ }
327
+
328
+ /** Starts taking the host's later lines. */
329
+ start(): void {
330
+ if (this.started) return;
331
+ this.started = true;
332
+ void this.#read();
333
+ }
334
+
335
+ close(): void {
336
+ this.lines.close();
337
+ this.#gone();
338
+ }
339
+
340
+ async #read(): Promise<void> {
341
+ for (;;) {
342
+ const line = await this.lines.next();
343
+ if (line === undefined) break;
344
+ const said = messageText(line);
345
+ if (said !== undefined) {
346
+ if (this.onMessage === undefined) this.early.push(said);
347
+ else this.onMessage(said);
348
+ continue;
349
+ }
350
+ try {
351
+ const ack: unknown = JSON.parse(line);
352
+ if (typeof ack === "object" && ack !== null && Reflect.get(ack, "type") === "ask_ack") {
353
+ const id: unknown = Reflect.get(ack, "id");
354
+ const decision: unknown = Reflect.get(ack, "decision");
355
+ const reason: unknown = Reflect.get(ack, "reason");
356
+ if (typeof id === "number") {
357
+ this.questions.get(id)?.({
358
+ decision: decision === "allow" || decision === "stop" ? decision : "deny",
359
+ ...(typeof reason === "string" ? { reason } : {}),
360
+ });
361
+ this.questions.delete(id);
362
+ }
363
+ }
364
+ if (typeof ack === "object" && ack !== null && Reflect.get(ack, "type") === "ask_form_ack") {
365
+ const id: unknown = Reflect.get(ack, "id");
366
+ const decision: unknown = Reflect.get(ack, "decision");
367
+ const answers: unknown = Reflect.get(ack, "answers");
368
+ const note: unknown = Reflect.get(ack, "note");
369
+ if (typeof id === "number") {
370
+ this.forms.get(id)?.({
371
+ decision: decision === "answered" || decision === "stop" ? decision : "rejected",
372
+ ...(Array.isArray(answers)
373
+ ? { answers: answers as readonly AskQuestionAnswer[] }
374
+ : {}),
375
+ ...(typeof note === "string" ? { note } : {}),
376
+ });
377
+ this.forms.delete(id);
378
+ }
379
+ }
380
+ if (typeof ack === "object" && ack !== null && Reflect.get(ack, "type") === "grant_ack") {
381
+ const id: unknown = Reflect.get(ack, "id");
382
+ const used: unknown = Reflect.get(ack, "used");
383
+ if (typeof id === "number") {
384
+ this.waiting.get(id)?.(used === true);
385
+ this.waiting.delete(id);
386
+ }
387
+ }
388
+ } catch {
389
+ // Not a line the program understands; it is ignored.
390
+ }
391
+ }
392
+ this.#gone();
393
+ }
394
+
395
+ #gone(): void {
396
+ if (this.hostGone) return;
397
+ this.hostGone = true;
398
+ for (const resolve of this.waiting.values()) resolve(false);
399
+ this.waiting.clear();
400
+ for (const resolve of this.questions.values()) resolve({ decision: "deny", reason: "the host is gone" });
401
+ this.questions.clear();
402
+ for (const resolve of this.forms.values()) resolve({ decision: "rejected" });
403
+ this.forms.clear();
404
+ this.onGone?.();
405
+ }
406
+ }
407
+
408
+ /** Runs one reply from stdin and stops; the program is not kept. */
409
+ export async function serveOne<TInput>(
410
+ stdinStream: AsyncIterable<Uint8Array>,
411
+ ports: WorkerPorts,
412
+ cancel: AbortSignal,
413
+ adapter: WorkerAdapter<TInput>,
414
+ ): Promise<number> {
415
+ const channel = new Channel(stdinStream, ports);
416
+ let code: number;
417
+ try {
418
+ const text = await channel.next();
419
+ const parsed = adapter.parse(text ?? "");
420
+ if (!parsed.ok) {
421
+ channel.io.line(adapter.invalidInput(parsed.message));
422
+ code = EXIT_INVALID_INPUT;
423
+ } else {
424
+ channel.start();
425
+ code = await adapter.run(parsed.input, channel.io, cancel);
426
+ }
427
+ } catch (error) {
428
+ channel.io.log(error instanceof Error ? (error.stack ?? error.message) : String(error));
429
+ channel.io.line(adapter.error(messageOf(error)));
430
+ code = EXIT_FAILED;
431
+ }
432
+ channel.close();
433
+ adapter.close?.();
434
+ return code;
435
+ }
436
+
437
+ /**
438
+ * Runs the first reply from stdin and, when the reply was done and asked for
439
+ * keeping, serves later replies from a socket in the operad home. The worker's
440
+ * own timer ends it after the kept time; a failed, stopped or interrupted reply
441
+ * leaves no kept worker.
442
+ */
443
+ export async function serveKeptWorker<TInput>(
444
+ stdinStream: AsyncIterable<Uint8Array>,
445
+ ports: WorkerPorts,
446
+ cancel: AbortSignal,
447
+ adapter: WorkerAdapter<TInput>,
448
+ ): Promise<number> {
449
+ const channel = new Channel(stdinStream, ports);
450
+ let code: number;
451
+ try {
452
+ const text = await channel.next();
453
+ const parsed = adapter.parse(text ?? "");
454
+ if (!parsed.ok) {
455
+ channel.io.line(adapter.invalidInput(parsed.message));
456
+ code = EXIT_INVALID_INPUT;
457
+ } else {
458
+ channel.start();
459
+ const capture = new Capture(channel.io);
460
+ code = await adapter.run(parsed.input, capture, cancel);
461
+ channel.close();
462
+ const keep = adapter.keep(parsed.input);
463
+ const result = capture.result;
464
+ if (keep !== undefined && result?.result === "done") {
465
+ adapter.setSession?.(result.sessionId);
466
+ return await serveKept(keep, ports, cancel, adapter);
467
+ }
468
+ }
469
+ } catch (error) {
470
+ channel.io.log(error instanceof Error ? (error.stack ?? error.message) : String(error));
471
+ channel.io.line(adapter.error(messageOf(error)));
472
+ code = EXIT_FAILED;
473
+ }
474
+ channel.close();
475
+ adapter.close?.();
476
+ return code;
477
+ }
478
+
479
+ /** The kept part of a worker's life: one socket, one reply at a time, its own timer. */
480
+ async function serveKept<TInput>(
481
+ keep: WorkerKeep,
482
+ ports: WorkerPorts,
483
+ cancel: AbortSignal,
484
+ adapter: WorkerAdapter<TInput>,
485
+ ): Promise<number> {
486
+ const startedAt = Date.now();
487
+ let session = adapter.session?.() ?? null;
488
+ let lastUsedAt = startedAt;
489
+ let busy = false;
490
+ let stopping = false;
491
+ let timer: ReturnType<typeof setTimeout> | undefined;
492
+ let server: ReturnType<typeof createServer> | undefined;
493
+
494
+ const record = () => ({
495
+ pid: process.pid,
496
+ token: keep.token,
497
+ scope: keep.scope,
498
+ key: keep.key,
499
+ socket: keep.socket,
500
+ meta: keep.meta,
501
+ session,
502
+ startedAt,
503
+ lastUsedAt,
504
+ ...(busy ? { busy: true } : {}),
505
+ });
506
+ const save = (): void => {
507
+ try {
508
+ writeKeptWorker(record());
509
+ } catch {
510
+ // A record that cannot be written means the next reply starts a new worker.
511
+ }
512
+ };
513
+ const shutdown = (code: number): void => {
514
+ if (stopping) return;
515
+ stopping = true;
516
+ if (timer !== undefined) clearTimeout(timer);
517
+ timer = undefined;
518
+ server?.close();
519
+ removeKeptWorker({ meta: keep.meta, socket: keep.socket });
520
+ adapter.close?.();
521
+ process.exit(code);
522
+ };
523
+ const idle = (): void => {
524
+ if (stopping) return;
525
+ if (timer !== undefined) clearTimeout(timer);
526
+ timer = setTimeout(() => shutdown(0), keep.timeoutMs);
527
+ };
528
+
529
+ rmSync(keep.meta, { force: true });
530
+ rmSync(keep.socket, { force: true });
531
+
532
+ const handle = async (socket: Socket): Promise<void> => {
533
+ if (stopping) {
534
+ socket.destroy();
535
+ return;
536
+ }
537
+ if (timer !== undefined) {
538
+ clearTimeout(timer);
539
+ timer = undefined;
540
+ }
541
+ const connection = new AbortController();
542
+ const channel = new Channel(
543
+ socket,
544
+ {
545
+ write: (text) => {
546
+ if (!socket.destroyed) socket.write(text);
547
+ },
548
+ log: ports.log,
549
+ },
550
+ () => connection.abort(),
551
+ );
552
+ try {
553
+ const first = await channel.next();
554
+ if (first === undefined) {
555
+ channel.close();
556
+ idle();
557
+ return;
558
+ }
559
+ let command: unknown;
560
+ try {
561
+ command = JSON.parse(first);
562
+ } catch {
563
+ command = undefined;
564
+ }
565
+ if (
566
+ typeof command === "object" &&
567
+ command !== null &&
568
+ Reflect.get(command, "type") === "stop"
569
+ ) {
570
+ channel.close();
571
+ socket.end();
572
+ shutdown(0);
573
+ return;
574
+ }
575
+ const parsed = adapter.parse(first);
576
+ if (!parsed.ok) {
577
+ channel.io.line(adapter.invalidInput(parsed.message));
578
+ channel.close();
579
+ socket.end();
580
+ idle();
581
+ return;
582
+ }
583
+ busy = true;
584
+ save();
585
+ channel.start();
586
+ const capture = new Capture(channel.io);
587
+ let code = EXIT_FAILED;
588
+ try {
589
+ code = await adapter.run(
590
+ parsed.input,
591
+ capture,
592
+ AbortSignal.any([cancel, connection.signal]),
593
+ );
594
+ } catch (error) {
595
+ channel.io.log(error instanceof Error ? (error.stack ?? error.message) : String(error));
596
+ channel.io.line(adapter.error(messageOf(error)));
597
+ }
598
+ channel.close();
599
+ if (!socket.destroyed) socket.end();
600
+ busy = false;
601
+ lastUsedAt = Date.now();
602
+ const result = capture.result;
603
+ if (result?.result === "done") {
604
+ session = result.sessionId ?? session;
605
+ adapter.setSession?.(session);
606
+ save();
607
+ idle();
608
+ } else {
609
+ shutdown(code);
610
+ }
611
+ } catch (error) {
612
+ ports.log(error instanceof Error ? (error.stack ?? error.message) : String(error));
613
+ channel.close();
614
+ socket.destroy();
615
+ idle();
616
+ }
617
+ };
618
+
619
+ let chain: Promise<void> = Promise.resolve();
620
+ server = createServer((socket) => {
621
+ chain = chain.then(
622
+ () => handle(socket),
623
+ () => handle(socket),
624
+ );
625
+ });
626
+ await new Promise<void>((resolve, reject) => {
627
+ if (server === undefined) {
628
+ reject(new Error("the kept worker has no server"));
629
+ return;
630
+ }
631
+ server.once("error", reject);
632
+ server.listen(keep.socket, () => resolve());
633
+ });
634
+ chmodSync(keep.socket, 0o600);
635
+ save();
636
+ idle();
637
+ adapter.onEnded?.((code) => shutdown(code));
638
+ cancel.addEventListener(
639
+ "abort",
640
+ () => {
641
+ if (!busy) shutdown(EXIT_CANCELLED);
642
+ },
643
+ { once: true },
644
+ );
645
+ return await new Promise<number>(() => undefined);
646
+ }
package/src/limit.ts CHANGED
@@ -11,6 +11,7 @@ import { isRecord } from "@operad-io/engine";
11
11
  /** The settings schema property both harnesses declare for `concurrency`. */
12
12
  export const CONCURRENCY_SETTING = {
13
13
  type: "object" as const,
14
+ title: "Runs at once",
14
15
  additionalProperties: {
15
16
  type: "integer" as const,
16
17
  minimum: 1,