@code-yeongyu/senpi-codemode 2026.10.5 → 2026.10.7

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 (68) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/README.md +20 -2
  3. package/package.json +4 -4
  4. package/src/bridge/kernel-tools-protocol.ts +1 -1
  5. package/src/bridge/reserved.ts +10 -0
  6. package/src/bridges/eval-virtual-schemas.ts +79 -0
  7. package/src/bridges/handle-bridge.ts +72 -0
  8. package/src/bridges/reserved-dispatch.ts +20 -2
  9. package/src/bridges/schema-bridge.ts +4 -0
  10. package/src/completion/handle.ts +69 -0
  11. package/src/completion/tool-bridge.ts +31 -12
  12. package/src/config/feature-settings.ts +110 -0
  13. package/src/config/memory-settings.ts +8 -0
  14. package/src/config/settings-constants.ts +7 -0
  15. package/src/config/settings-overrides.ts +72 -0
  16. package/src/config/settings.ts +35 -78
  17. package/src/config/unknown-settings.ts +19 -0
  18. package/src/extension/bridge-dispatch.ts +41 -0
  19. package/src/extension/bridge-tool-call.ts +41 -0
  20. package/src/extension/host-facts.ts +18 -0
  21. package/src/extension/idle-parking-kernel.ts +272 -0
  22. package/src/extension/kernel-registration.ts +25 -0
  23. package/src/extension/kernel-tools-probe.ts +24 -0
  24. package/src/extension/removed-tool-hints.ts +15 -0
  25. package/src/extension/runtime-factory.ts +3 -0
  26. package/src/extension/session-manager-contract.ts +3 -0
  27. package/src/extension/session-manager.ts +58 -58
  28. package/src/extension/start-recovery.ts +111 -0
  29. package/src/handles/completion-handles.ts +202 -0
  30. package/src/handles/handle-args.ts +103 -0
  31. package/src/handles/handle-registry.ts +164 -0
  32. package/src/handles/wait.ts +214 -0
  33. package/src/handles/watch-queue.ts +102 -0
  34. package/src/index.ts +99 -94
  35. package/src/kernels/jl/handles.jl +124 -0
  36. package/src/kernels/jl/kernel.ts +3 -1
  37. package/src/kernels/jl/prelude.jl +9 -0
  38. package/src/kernels/jl/runner.jl +160 -1
  39. package/src/kernels/js/context-manager.ts +1 -1
  40. package/src/kernels/js/handles.js +93 -0
  41. package/src/kernels/js/kernel-tools-define.d.ts +6 -0
  42. package/src/kernels/js/kernel-tools-define.js +3 -1
  43. package/src/kernels/js/kernel-tools-registry.d.ts +2 -0
  44. package/src/kernels/js/kernel-tools-registry.js +18 -9
  45. package/src/kernels/js/kernel-tools-schema.js +6 -2
  46. package/src/kernels/js/kernel-tools-types.ts +2 -1
  47. package/src/kernels/js/worker-global-sizes.js +84 -14
  48. package/src/kernels/js/worker-runtime.js +10 -1
  49. package/src/kernels/py/host-cell.ts +42 -0
  50. package/src/kernels/py/kernel-contract.ts +2 -0
  51. package/src/kernels/py/kernel.ts +48 -4
  52. package/src/kernels/py/prelude.py +145 -4
  53. package/src/kernels/rb/handles.rb +97 -0
  54. package/src/kernels/rb/kernel.ts +3 -1
  55. package/src/kernels/rb/paths.rb +37 -0
  56. package/src/kernels/rb/prelude.rb +15 -38
  57. package/src/kernels/rb/runner.rb +141 -0
  58. package/src/kernels/shared/kernel-memory-host.ts +14 -6
  59. package/src/kernels/{js/kernel-tools-host.ts → shared/kernel-tools-pump.ts} +2 -2
  60. package/src/kernels/shared/process-group-cpu.ts +139 -0
  61. package/src/kernels/shared/subprocess-contract.ts +3 -0
  62. package/src/kernels/shared/subprocess-kernel.ts +33 -1
  63. package/src/kernels/shared/subprocess-startup.ts +90 -0
  64. package/src/tool/cell-handler.ts +21 -18
  65. package/src/tool/cell-reserved-dispatch.ts +51 -0
  66. package/src/tool/eval-tool-options.ts +3 -0
  67. package/src/tool/run-eval-cell.ts +7 -6
  68. package/src/tool/types.ts +15 -0
@@ -0,0 +1,202 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import {
3
+ type CancelReceipt,
4
+ EvalHandleError,
5
+ type HandleError,
6
+ type HandleOutcome,
7
+ type HandlePhase,
8
+ type HandleRef,
9
+ type HandleSnapshot,
10
+ type HandleWatch,
11
+ type OutputRequest,
12
+ type OutputSnapshot,
13
+ } from "@code-yeongyu/senpi";
14
+ import { refKey } from "./handle-args.ts";
15
+ import { WatchQueue } from "./watch-queue.ts";
16
+
17
+ export interface CompletionStart {
18
+ /** Runs the host completion; the signal aborts on cancel, on the deadline, and when the session generation drops. */
19
+ readonly run: (signal: AbortSignal) => Promise<unknown>;
20
+ /** Absolute wall-clock deadline (ms), derived from the creating cell's hard deadline. */
21
+ readonly deadlineMs: number;
22
+ }
23
+
24
+ interface CompletionEntry {
25
+ readonly ref: HandleRef;
26
+ phase: HandlePhase;
27
+ hostStatus: string;
28
+ revision: number;
29
+ value?: unknown;
30
+ error?: HandleError;
31
+ readonly controller: AbortController;
32
+ timer: ReturnType<typeof setTimeout> | undefined;
33
+ deadlineHit: boolean;
34
+ }
35
+
36
+ const TERMINAL: ReadonlySet<HandlePhase> = new Set(["succeeded", "failed", "cancelled", "lost"]);
37
+ const DEADLINE_MESSAGE = "completion handle exceeded its cell's hard deadline";
38
+ const MAX_TIMER_MS = 2 ** 31 - 1;
39
+
40
+ /**
41
+ * codemode-owned completion handles (`completion(prompt, {handle: true})`). They live in the session's
42
+ * registry, so they survive a kernel restart and need no host capability; once the session generation
43
+ * is dropped every ref fails closed with `eval_handle_stale`.
44
+ */
45
+ export class CompletionHandles {
46
+ readonly #entries = new Map<string, CompletionEntry>();
47
+ readonly #watches = new Set<WatchQueue>();
48
+ readonly #now: () => number;
49
+ #disposed = false;
50
+
51
+ constructor(now: () => number = Date.now) {
52
+ this.#now = now;
53
+ }
54
+
55
+ start(input: CompletionStart): HandleRef {
56
+ this.#assertLive();
57
+ const ref: HandleRef = { kind: "completion", id: `cp_${randomBytes(16).toString("hex")}`, run_epoch: 0 };
58
+ const controller = new AbortController();
59
+ const entry: CompletionEntry = {
60
+ ref,
61
+ phase: "pending",
62
+ hostStatus: "running",
63
+ revision: 1,
64
+ controller,
65
+ timer: undefined,
66
+ deadlineHit: false,
67
+ };
68
+ this.#entries.set(ref.id, entry);
69
+ const remainingMs = Math.min(MAX_TIMER_MS, Math.max(0, input.deadlineMs - this.#now()));
70
+ if (Number.isFinite(remainingMs)) {
71
+ entry.timer = setTimeout(() => {
72
+ entry.deadlineHit = true;
73
+ controller.abort(new Error(DEADLINE_MESSAGE));
74
+ }, remainingMs);
75
+ entry.timer.unref?.();
76
+ }
77
+ input.run(controller.signal).then(
78
+ (value) => this.#settle(entry, { phase: "succeeded", hostStatus: "completed", value }),
79
+ (error: unknown) => {
80
+ // cancel() and dispose() settle the entry before aborting; their rejection is not an outcome.
81
+ if (TERMINAL.has(entry.phase)) return;
82
+ const hostStatus = entry.deadlineHit ? "deadline" : "failed";
83
+ this.#settle(entry, { phase: "failed", hostStatus, error: toHandleError(error, entry.deadlineHit) });
84
+ },
85
+ );
86
+ return ref;
87
+ }
88
+
89
+ get openWatches(): number {
90
+ return this.#watches.size;
91
+ }
92
+
93
+ owns(ref: HandleRef): boolean {
94
+ return ref.kind === "completion";
95
+ }
96
+
97
+ watch(refs: readonly HandleRef[]): HandleWatch {
98
+ const entries = refs.map((ref) => this.#fence(ref));
99
+ const queue = new WatchQueue(refs.map(refKey));
100
+ // Subscribe first, then snapshot: a settle between the two is queued and arrives in updates once.
101
+ this.#watches.add(queue);
102
+ const initial = entries.map((entry) => snapshotOf(entry));
103
+ return queue.toWatch(initial, () => this.#watches.delete(queue));
104
+ }
105
+
106
+ result(ref: HandleRef): HandleOutcome {
107
+ const entry = this.#fence(ref);
108
+ if (!TERMINAL.has(entry.phase)) throw new EvalHandleError("eval_handle_pending", `${ref.id} is still running`);
109
+ if (entry.phase === "succeeded") return { status: "fulfilled", ref: entry.ref, value: entry.value };
110
+ return {
111
+ status: "rejected",
112
+ ref: entry.ref,
113
+ error: entry.error ?? { code: "eval_handle_lost", message: `${ref.id} ended without an outcome` },
114
+ };
115
+ }
116
+
117
+ cancel(ref: HandleRef): CancelReceipt {
118
+ const entry = this.#fence(ref);
119
+ if (TERMINAL.has(entry.phase)) return { ref: entry.ref, cancelled: false, phase: entry.phase };
120
+ const error = { code: "eval_handle_cancelled", message: `${ref.id} was cancelled` };
121
+ this.#settle(entry, { phase: "cancelled", hostStatus: "cancelled", error });
122
+ entry.controller.abort(new Error(error.message));
123
+ return { ref: entry.ref, cancelled: true, phase: "cancelled" };
124
+ }
125
+
126
+ output(ref: HandleRef, request: OutputRequest): OutputSnapshot {
127
+ const entry = this.#fence(ref);
128
+ const text =
129
+ entry.phase === "succeeded"
130
+ ? typeof entry.value === "string"
131
+ ? entry.value
132
+ : JSON.stringify(entry.value)
133
+ : (entry.error?.message ?? "");
134
+ const lines = text.length === 0 ? [] : text.split("\n");
135
+ const total = lines.length;
136
+ const window =
137
+ request.format === "tail"
138
+ ? lines.slice(-(request.limit ?? 20))
139
+ : lines.slice(request.offset ?? 0, (request.offset ?? 0) + (request.limit ?? total));
140
+ const offset = request.format === "tail" ? total - window.length : (request.offset ?? 0);
141
+ return { ref: entry.ref, text: window.join("\n"), offset, total, truncated: window.length < total };
142
+ }
143
+
144
+ /** Aborts every in-flight completion and closes every watch; afterwards every ref is stale. */
145
+ dispose(): void {
146
+ if (this.#disposed) return;
147
+ this.#disposed = true;
148
+ for (const entry of this.#entries.values()) {
149
+ if (entry.timer !== undefined) clearTimeout(entry.timer);
150
+ if (!TERMINAL.has(entry.phase)) {
151
+ entry.phase = "lost";
152
+ entry.hostStatus = "session-dropped";
153
+ entry.controller.abort(new Error("session generation dropped"));
154
+ }
155
+ }
156
+ for (const queue of this.#watches) queue.close();
157
+ this.#watches.clear();
158
+ }
159
+
160
+ #assertLive(): void {
161
+ if (this.#disposed) throw new EvalHandleError("eval_handle_stale", "this session generation was dropped");
162
+ }
163
+
164
+ #fence(ref: HandleRef): CompletionEntry {
165
+ this.#assertLive();
166
+ const entry = this.#entries.get(ref.id);
167
+ if (ref.kind !== "completion" || !entry)
168
+ throw new EvalHandleError("eval_handle_not_found", `no completion handle ${ref.id}`);
169
+ if (ref.run_epoch !== entry.ref.run_epoch)
170
+ throw new EvalHandleError("eval_handle_stale", `${ref.id} run epoch ${ref.run_epoch} is not live`);
171
+ return entry;
172
+ }
173
+
174
+ #settle(
175
+ entry: CompletionEntry,
176
+ next: { phase: HandlePhase; hostStatus: string; value?: unknown; error?: HandleError },
177
+ ): void {
178
+ if (TERMINAL.has(entry.phase)) return;
179
+ if (entry.timer !== undefined) clearTimeout(entry.timer);
180
+ entry.timer = undefined;
181
+ entry.phase = next.phase;
182
+ entry.hostStatus = next.hostStatus;
183
+ if ("value" in next) entry.value = next.value;
184
+ if (next.error) entry.error = next.error;
185
+ entry.revision += 1;
186
+ const snapshot = snapshotOf(entry);
187
+ for (const queue of this.#watches) queue.push(snapshot);
188
+ }
189
+ }
190
+
191
+ function snapshotOf(entry: CompletionEntry): HandleSnapshot {
192
+ return { ref: entry.ref, phase: entry.phase, host_status: entry.hostStatus, revision: entry.revision };
193
+ }
194
+
195
+ function toHandleError(error: unknown, timedOut: boolean): HandleError {
196
+ if (timedOut) return { code: "completion_deadline", message: DEADLINE_MESSAGE };
197
+ if (error instanceof Error) {
198
+ const code = "code" in error && typeof error.code === "string" ? error.code : "completion_failed";
199
+ return { code, message: error.message };
200
+ }
201
+ return { code: "completion_failed", message: String(error) };
202
+ }
@@ -0,0 +1,103 @@
1
+ import type { HandleRef, OutputRequest } from "@code-yeongyu/senpi";
2
+ import { type Static, type TSchema, Type } from "typebox";
3
+ import { Check, Errors } from "typebox/value";
4
+
5
+ /** Wire shapes of the five reserved handle tools; every ref carries kind, id and run epoch so the host can fence it. */
6
+ const handleRefSchema = Type.Object(
7
+ {
8
+ kind: Type.Union([Type.Literal("agent"), Type.Literal("completion"), Type.Literal("workpool")]),
9
+ id: Type.String({ minLength: 1 }),
10
+ run_epoch: Type.Integer({ minimum: 0 }),
11
+ },
12
+ { additionalProperties: true },
13
+ );
14
+
15
+ export const WAIT_MODES = ["all", "any", "settled"] as const;
16
+ export type WaitMode = (typeof WAIT_MODES)[number];
17
+
18
+ const waitArgsSchema = Type.Object(
19
+ {
20
+ refs: Type.Array(handleRefSchema),
21
+ timeout: Type.Optional(Type.Number({ minimum: 0 })),
22
+ mode: Type.Optional(Type.Union(WAIT_MODES.map((mode) => Type.Literal(mode)))),
23
+ },
24
+ { additionalProperties: false },
25
+ );
26
+ const refArgsSchema = Type.Object({ ref: handleRefSchema }, { additionalProperties: false });
27
+ const outputArgsSchema = Type.Object(
28
+ {
29
+ ref: handleRefSchema,
30
+ format: Type.Optional(Type.Union([Type.Literal("raw"), Type.Literal("tail")])),
31
+ offset: Type.Optional(Type.Integer({ minimum: 0 })),
32
+ limit: Type.Optional(Type.Integer({ minimum: 1 })),
33
+ },
34
+ { additionalProperties: false },
35
+ );
36
+ const sendArgsSchema = Type.Object(
37
+ { ref: handleRefSchema, message: Type.String({ minLength: 1 }) },
38
+ { additionalProperties: false },
39
+ );
40
+
41
+ export interface WaitRequest {
42
+ readonly refs: readonly HandleRef[];
43
+ readonly mode: WaitMode;
44
+ readonly timeoutSeconds?: number;
45
+ }
46
+
47
+ export class HandleArgumentsError extends Error {
48
+ readonly name = "HandleArgumentsError";
49
+ readonly code = "eval_handle_invalid_arguments";
50
+
51
+ constructor(helper: string, summary: string) {
52
+ super(`eval_handle_invalid_arguments: ${helper} received invalid arguments: ${summary}`);
53
+ }
54
+ }
55
+
56
+ export function parseWaitArgs(value: unknown): WaitRequest {
57
+ const parsed = parseWith(waitArgsSchema, value, "wait()");
58
+ if (parsed.timeout !== undefined && !Number.isFinite(parsed.timeout)) {
59
+ throw new HandleArgumentsError("wait()", "timeout must be a finite number of seconds >= 0");
60
+ }
61
+ return {
62
+ refs: parsed.refs.map(toRef),
63
+ mode: parsed.mode ?? "all",
64
+ ...(parsed.timeout === undefined ? {} : { timeoutSeconds: parsed.timeout }),
65
+ };
66
+ }
67
+
68
+ export function parseRefArgs(value: unknown, helper: string): HandleRef {
69
+ return toRef(parseWith(refArgsSchema, value, helper).ref);
70
+ }
71
+
72
+ export function parseOutputArgs(value: unknown): { readonly ref: HandleRef; readonly request: OutputRequest } {
73
+ const parsed = parseWith(outputArgsSchema, value, "control.output()");
74
+ return {
75
+ ref: toRef(parsed.ref),
76
+ request: {
77
+ ...(parsed.format === undefined ? {} : { format: parsed.format }),
78
+ ...(parsed.offset === undefined ? {} : { offset: parsed.offset }),
79
+ ...(parsed.limit === undefined ? {} : { limit: parsed.limit }),
80
+ },
81
+ };
82
+ }
83
+
84
+ export function parseSendArgs(value: unknown): { readonly ref: HandleRef; readonly message: string } {
85
+ const parsed = parseWith(sendArgsSchema, value, "control.send()");
86
+ return { ref: toRef(parsed.ref), message: parsed.message };
87
+ }
88
+
89
+ export function refKey(ref: HandleRef): string {
90
+ return `${ref.kind}:${ref.id}:${ref.run_epoch}`;
91
+ }
92
+
93
+ function toRef(value: Static<typeof handleRefSchema>): HandleRef {
94
+ return { kind: value.kind, id: value.id, run_epoch: value.run_epoch };
95
+ }
96
+
97
+ function parseWith<T extends TSchema>(schema: T, value: unknown, helper: string): Static<T> {
98
+ if (Check(schema, value)) return value;
99
+ const summary = Errors(schema, value)
100
+ .map((error) => `${error.instancePath || "/"} ${error.message}`)
101
+ .join("; ");
102
+ throw new HandleArgumentsError(helper, summary || "invalid value");
103
+ }
@@ -0,0 +1,164 @@
1
+ import {
2
+ type CancelReceipt,
3
+ EvalHandleError,
4
+ type EvalHandleHost,
5
+ type HandleCallContext,
6
+ type HandleOutcome,
7
+ type HandleRef,
8
+ type HandleSnapshot,
9
+ type HandleWatch,
10
+ type OutputRequest,
11
+ type OutputSnapshot,
12
+ } from "@code-yeongyu/senpi";
13
+ import { CompletionHandles, type CompletionStart } from "./completion-handles.ts";
14
+ import { refKey } from "./handle-args.ts";
15
+ import { mergeWatches } from "./watch-queue.ts";
16
+
17
+ /** What a bridge call brings along: the session's host capability (if any) and the calling cell's signal. */
18
+ export interface HandleBackend {
19
+ readonly host: EvalHandleHost | undefined;
20
+ readonly signal: AbortSignal | undefined;
21
+ }
22
+
23
+ export interface HandleRegistryOptions {
24
+ /** The agent session that owns this registry; every host call is fenced with it. */
25
+ readonly ownerSessionId: string;
26
+ readonly now?: () => number;
27
+ }
28
+
29
+ /**
30
+ * One registry per session generation (created at `session_start`, disposed in `dropRuntime`). Agent and
31
+ * workpool refs are routed to the session's `EvalHandleHost`; completion refs are codemode's own in-flight
32
+ * completions. After disposal every ref fails closed with `eval_handle_stale` before any host is touched.
33
+ */
34
+ export class HandleRegistry {
35
+ readonly ownerSessionId: string;
36
+ readonly #completions: CompletionHandles;
37
+ readonly #open = new Set<HandleWatch>();
38
+ #disposed = false;
39
+
40
+ constructor(options: HandleRegistryOptions) {
41
+ this.ownerSessionId = options.ownerSessionId;
42
+ this.#completions = new CompletionHandles(options.now);
43
+ }
44
+
45
+ get disposed(): boolean {
46
+ return this.#disposed;
47
+ }
48
+
49
+ get openWatches(): number {
50
+ return this.#open.size;
51
+ }
52
+
53
+ get openCompletionWatches(): number {
54
+ return this.#completions.openWatches;
55
+ }
56
+
57
+ startCompletion(input: CompletionStart): HandleRef {
58
+ this.#assertLive();
59
+ return this.#completions.start(input);
60
+ }
61
+
62
+ async watch(refs: readonly HandleRef[], backend: HandleBackend): Promise<HandleWatch> {
63
+ this.#assertLive();
64
+ const local = refs.filter((ref) => this.#completions.owns(ref));
65
+ const remote = refs.filter((ref) => !this.#completions.owns(ref));
66
+ const host = remote.length > 0 ? this.#requireHost(backend, remote) : undefined;
67
+ const parts: HandleWatch[] = [];
68
+ if (local.length > 0) parts.push(this.#completions.watch(local));
69
+ if (host !== undefined) {
70
+ try {
71
+ parts.push(await host.watch(remote, this.#callContext(backend)));
72
+ } catch (error) {
73
+ for (const part of parts) part.close();
74
+ throw error;
75
+ }
76
+ }
77
+ const byKey = new Map(parts.flatMap((part) => part.initial.map((snapshot) => [refKey(snapshot.ref), snapshot])));
78
+ const ordered = refs.map((ref) => {
79
+ const snapshot = byKey.get(refKey(ref));
80
+ if (snapshot === undefined) throw new EvalHandleError("eval_handle_not_found", `no snapshot for ${ref.id}`);
81
+ return snapshot;
82
+ });
83
+ const merged = mergeWatches(ordered, parts);
84
+ const tracked: HandleWatch = {
85
+ initial: merged.initial,
86
+ updates: merged.updates,
87
+ close: () => {
88
+ merged.close();
89
+ this.#open.delete(tracked);
90
+ },
91
+ };
92
+ this.#open.add(tracked);
93
+ if (this.#disposed) tracked.close();
94
+ return tracked;
95
+ }
96
+
97
+ async status(ref: HandleRef, backend: HandleBackend): Promise<HandleSnapshot> {
98
+ const watch = await this.watch([ref], backend);
99
+ try {
100
+ const snapshot = watch.initial[0];
101
+ if (snapshot === undefined) throw new EvalHandleError("eval_handle_not_found", `no snapshot for ${ref.id}`);
102
+ return snapshot;
103
+ } finally {
104
+ watch.close();
105
+ }
106
+ }
107
+
108
+ async result(ref: HandleRef, backend: HandleBackend): Promise<HandleOutcome> {
109
+ this.#assertLive();
110
+ if (this.#completions.owns(ref)) return this.#completions.result(ref);
111
+ return await this.#requireHost(backend, [ref]).result(ref, this.#callContext(backend));
112
+ }
113
+
114
+ async send(ref: HandleRef, message: string, backend: HandleBackend): Promise<HandleSnapshot> {
115
+ this.#assertLive();
116
+ if (this.#completions.owns(ref)) {
117
+ throw new EvalHandleError("eval_handle_operation_unsupported", "send() is for agent handles only");
118
+ }
119
+ return await this.#requireHost(backend, [ref]).send(ref, message, this.#callContext(backend));
120
+ }
121
+
122
+ async cancel(ref: HandleRef, backend: HandleBackend): Promise<CancelReceipt> {
123
+ this.#assertLive();
124
+ if (this.#completions.owns(ref)) return this.#completions.cancel(ref);
125
+ return await this.#requireHost(backend, [ref]).cancel(ref, this.#callContext(backend));
126
+ }
127
+
128
+ async output(ref: HandleRef, request: OutputRequest, backend: HandleBackend): Promise<OutputSnapshot> {
129
+ this.#assertLive();
130
+ if (this.#completions.owns(ref)) return this.#completions.output(ref, request);
131
+ return await this.#requireHost(backend, [ref]).output(ref, request, this.#callContext(backend));
132
+ }
133
+
134
+ /** Closes every open watch and aborts every in-flight completion; later calls fail with `eval_handle_stale`. */
135
+ dispose(): void {
136
+ if (this.#disposed) return;
137
+ this.#disposed = true;
138
+ for (const watch of [...this.#open]) watch.close();
139
+ this.#open.clear();
140
+ this.#completions.dispose();
141
+ }
142
+
143
+ #assertLive(): void {
144
+ if (this.#disposed) {
145
+ throw new EvalHandleError("eval_handle_stale", "this session generation was dropped; its handles are gone");
146
+ }
147
+ }
148
+
149
+ #requireHost(backend: HandleBackend, refs: readonly HandleRef[]): EvalHandleHost {
150
+ if (backend.host !== undefined) return backend.host;
151
+ const kinds = [...new Set(refs.map((ref) => ref.kind))].join("/");
152
+ throw new EvalHandleError(
153
+ "eval_wait_unavailable",
154
+ `wait()/handle() over ${kinds} work is not supported by this runtime: no EvalHandleHost is provided for this session`,
155
+ );
156
+ }
157
+
158
+ #callContext(backend: HandleBackend): HandleCallContext {
159
+ return {
160
+ ownerSessionId: this.ownerSessionId,
161
+ ...(backend.signal === undefined ? {} : { signal: backend.signal }),
162
+ };
163
+ }
164
+ }
@@ -0,0 +1,214 @@
1
+ import {
2
+ EvalHandleError,
3
+ type HandleOutcome,
4
+ type HandlePhase,
5
+ type HandleRef,
6
+ type HandleSnapshot,
7
+ type HandleWatch,
8
+ } from "@code-yeongyu/senpi";
9
+ import { refKey, type WaitRequest } from "./handle-args.ts";
10
+
11
+ const MAX_TIMER_MS = 2 ** 31 - 1;
12
+
13
+ export interface WaitBackend {
14
+ watch(refs: readonly HandleRef[]): Promise<HandleWatch>;
15
+ result(ref: HandleRef): Promise<HandleOutcome>;
16
+ readonly signal?: AbortSignal;
17
+ }
18
+
19
+ export type WaitAnyResult = { readonly index: number; readonly ref: HandleRef; readonly value: unknown };
20
+
21
+ /** A handle's own failure, raised by `wait(..., {mode: "all"})` and `control.wait()`; `code` is in the text. */
22
+ export class HandleOutcomeError extends Error {
23
+ readonly name = "HandleOutcomeError";
24
+ readonly code: string;
25
+ readonly ref: HandleRef;
26
+ readonly details: unknown;
27
+
28
+ constructor(outcome: Extract<HandleOutcome, { status: "rejected" }>) {
29
+ super(`${outcome.error.code}: ${outcome.error.message}`);
30
+ this.code = outcome.error.code;
31
+ this.ref = outcome.ref;
32
+ this.details = outcome.error.details;
33
+ }
34
+ }
35
+
36
+ export class WaitAllRejectedError extends Error {
37
+ readonly name = "WaitAllRejectedError";
38
+ readonly code = "eval_wait_all_rejected";
39
+ readonly outcomes: readonly HandleOutcome[];
40
+
41
+ constructor(outcomes: readonly HandleOutcome[]) {
42
+ const summary = outcomes
43
+ .map((outcome) =>
44
+ outcome.status === "rejected" ? `${outcome.ref.id}: ${outcome.error.code}` : outcome.ref.id,
45
+ )
46
+ .join("; ");
47
+ super(`eval_wait_all_rejected: wait(any) found no successful handle among ${outcomes.length}; ${summary}`);
48
+ this.outcomes = outcomes;
49
+ }
50
+ }
51
+
52
+ const TERMINAL: ReadonlySet<HandlePhase> = new Set(["succeeded", "failed", "cancelled", "lost"]);
53
+
54
+ /**
55
+ * The in-cell barrier. Subscribes once per distinct ref, keeps duplicate slots, settles according to the
56
+ * mode, and never cancels work: a timeout only closes the subscription. The timeout runs from entry and,
57
+ * like cancelling the cell, also ends a wait that is still subscribing or fetching a result. Parked time
58
+ * is the caller's concern (the bridge-call path pauses the run budget around this call).
59
+ */
60
+ export async function waitForHandles(request: WaitRequest, backend: WaitBackend): Promise<unknown> {
61
+ const { refs, mode } = request;
62
+ if (refs.length === 0) {
63
+ if (mode === "any") throw new EvalHandleError("eval_wait_empty", "wait(any) needs at least one handle");
64
+ return [];
65
+ }
66
+ const unique = [...new Map(refs.map((ref) => [refKey(ref), ref])).values()];
67
+ const outcomes = new Map<string, HandleOutcome>();
68
+ const revisions = new Map<string, number>();
69
+ const stop = interruption(request.timeoutSeconds, backend.signal, refs, outcomes);
70
+ let watch: HandleWatch | undefined;
71
+ const finish = (): unknown => {
72
+ switch (mode) {
73
+ case "all":
74
+ return refs.map((ref) => settledValue(outcomes, ref));
75
+ case "settled":
76
+ return refs.map((ref) => outcomeOf(outcomes, ref));
77
+ case "any":
78
+ return firstSuccess(refs, outcomes) ?? rejectAll(refs, outcomes);
79
+ default:
80
+ return assertNever(mode);
81
+ }
82
+ };
83
+ // Returns true once the mode can settle without waiting for the remaining handles.
84
+ const absorb = async (snapshot: HandleSnapshot): Promise<boolean> => {
85
+ const key = refKey(snapshot.ref);
86
+ const known = revisions.get(key);
87
+ if (known !== undefined && snapshot.revision <= known) return false;
88
+ revisions.set(key, snapshot.revision);
89
+ if (snapshot.ref.kind === "workpool" && snapshot.phase === "pending" && snapshot.host_status === "open") {
90
+ throw new EvalHandleError(
91
+ "eval_pool_open",
92
+ `workpool ${snapshot.ref.id} is still open; close() it before waiting`,
93
+ );
94
+ }
95
+ if (!TERMINAL.has(snapshot.phase) || outcomes.has(key)) return false;
96
+ const outcome = await stop.race(backend.result(snapshot.ref));
97
+ outcomes.set(key, outcome);
98
+ if (mode === "all" && outcome.status === "rejected") throw new HandleOutcomeError(outcome);
99
+ if (mode === "any" && outcome.status === "fulfilled") return true;
100
+ return outcomes.size === unique.length;
101
+ };
102
+ try {
103
+ const subscribing = backend.watch(unique);
104
+ // A watch that resolves after the wait already ended is closed, so its subscription never leaks.
105
+ void subscribing.then(
106
+ (late) => {
107
+ if (stop.ended) late.close();
108
+ },
109
+ () => undefined,
110
+ );
111
+ watch = await stop.race(subscribing);
112
+ for (const snapshot of watch.initial) {
113
+ if (await absorb(snapshot)) return finish();
114
+ }
115
+ if (outcomes.size === unique.length) return finish();
116
+ if (request.timeoutSeconds === 0) throw timeoutError(0, refs, outcomes);
117
+ const iterator = watch.updates[Symbol.asyncIterator]();
118
+ try {
119
+ while (true) {
120
+ const step = await stop.race(iterator.next());
121
+ if (step.done) return finish();
122
+ if (await absorb(step.value)) return finish();
123
+ }
124
+ } finally {
125
+ await iterator.return?.();
126
+ }
127
+ } finally {
128
+ stop.end();
129
+ watch?.close();
130
+ }
131
+ }
132
+
133
+ /** One deadline armed at entry plus the cell's abort; `race` rejects any pending step once either fires. */
134
+ function interruption(
135
+ timeoutSeconds: number | undefined,
136
+ signal: AbortSignal | undefined,
137
+ refs: readonly HandleRef[],
138
+ outcomes: ReadonlyMap<string, HandleOutcome>,
139
+ ) {
140
+ let timer: ReturnType<typeof setTimeout> | undefined;
141
+ let onAbort: (() => void) | undefined;
142
+ let ended = false;
143
+ const fired = new Promise<never>((_resolve, reject) => {
144
+ if (timeoutSeconds !== undefined && timeoutSeconds > 0) {
145
+ // A timer longer than the platform maximum fires at once, so a long deadline is armed in chunks.
146
+ const deadline = Date.now() + timeoutSeconds * 1_000;
147
+ const arm = (): void => {
148
+ const remainingMs = deadline - Date.now();
149
+ if (remainingMs <= 0) {
150
+ reject(timeoutError(timeoutSeconds, refs, outcomes));
151
+ return;
152
+ }
153
+ timer = setTimeout(arm, Math.min(remainingMs, MAX_TIMER_MS));
154
+ };
155
+ arm();
156
+ }
157
+ if (signal !== undefined) {
158
+ onAbort = () =>
159
+ reject(signal.reason instanceof Error ? signal.reason : new Error("wait() cancelled with its cell"));
160
+ if (signal.aborted) onAbort();
161
+ else signal.addEventListener("abort", onAbort, { once: true });
162
+ }
163
+ });
164
+ fired.catch(() => undefined);
165
+ return {
166
+ get ended(): boolean {
167
+ return ended;
168
+ },
169
+ race<T>(step: Promise<T>): Promise<T> {
170
+ return Promise.race([step, fired]);
171
+ },
172
+ end(): void {
173
+ ended = true;
174
+ if (timer !== undefined) clearTimeout(timer);
175
+ if (signal !== undefined && onAbort !== undefined) signal.removeEventListener("abort", onAbort);
176
+ },
177
+ };
178
+ }
179
+
180
+ function timeoutError(seconds: number, refs: readonly HandleRef[], outcomes: ReadonlyMap<string, HandleOutcome>) {
181
+ const settled = refs.filter((ref) => outcomes.has(refKey(ref))).length;
182
+ return new EvalHandleError(
183
+ "eval_wait_timeout",
184
+ `wait() timed out after ${seconds}s; ${settled}/${refs.length} handles settled; work was not cancelled`,
185
+ );
186
+ }
187
+
188
+ function outcomeOf(outcomes: ReadonlyMap<string, HandleOutcome>, ref: HandleRef): HandleOutcome {
189
+ const outcome = outcomes.get(refKey(ref));
190
+ if (outcome === undefined) throw new EvalHandleError("eval_handle_pending", `${ref.id} did not settle`);
191
+ return outcome;
192
+ }
193
+
194
+ function settledValue(outcomes: ReadonlyMap<string, HandleOutcome>, ref: HandleRef): unknown {
195
+ const outcome = outcomeOf(outcomes, ref);
196
+ if (outcome.status === "rejected") throw new HandleOutcomeError(outcome);
197
+ return outcome.value;
198
+ }
199
+
200
+ function firstSuccess(refs: readonly HandleRef[], outcomes: ReadonlyMap<string, HandleOutcome>) {
201
+ for (const [index, ref] of refs.entries()) {
202
+ const outcome = outcomes.get(refKey(ref));
203
+ if (outcome?.status === "fulfilled") return { index, ref, value: outcome.value } satisfies WaitAnyResult;
204
+ }
205
+ return undefined;
206
+ }
207
+
208
+ function rejectAll(refs: readonly HandleRef[], outcomes: ReadonlyMap<string, HandleOutcome>): never {
209
+ throw new WaitAllRejectedError(refs.map((ref) => outcomeOf(outcomes, ref)));
210
+ }
211
+
212
+ function assertNever(value: never): never {
213
+ throw new TypeError(`Unsupported wait mode: ${String(value)}`);
214
+ }