opencode-model-router 2.8.0 → 2.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +372 -0
- package/README.md +22 -2
- package/package.json +5 -1
- package/src/commands/output.ts +23 -1
- package/src/compat/background.ts +2439 -0
- package/src/compat/child-session.ts +6 -0
- package/src/compat/turn-state.ts +215 -0
- package/src/compat/v2-client.ts +136 -17
- package/src/compat/v2-hooks.ts +2656 -114
- package/src/compat/v2-observations.ts +328 -0
- package/src/executor/capacity.ts +362 -0
- package/src/executor/child-guards.ts +40 -0
- package/src/executor/command.ts +265 -0
- package/src/executor/compile/edges.ts +809 -0
- package/src/executor/compile/estimate.ts +158 -0
- package/src/executor/compile/footprint.ts +829 -0
- package/src/executor/compile/index.ts +1080 -0
- package/src/executor/compile/parse.ts +1492 -0
- package/src/executor/compile/residue.ts +706 -0
- package/src/executor/compile/validate.ts +280 -0
- package/src/executor/create.ts +672 -0
- package/src/executor/expand.ts +551 -0
- package/src/executor/git.ts +1214 -0
- package/src/executor/host-preamble.ts +17 -0
- package/src/executor/instructions.ts +35 -0
- package/src/executor/integrate/bisect.ts +73 -0
- package/src/executor/integrate/checks.ts +1086 -0
- package/src/executor/integrate/land.ts +177 -0
- package/src/executor/integrate/phase-suite.ts +407 -0
- package/src/executor/integrate/queue.ts +3083 -0
- package/src/executor/integrate/run-resolver.ts +19 -0
- package/src/executor/launch.ts +652 -0
- package/src/executor/ledger.ts +2208 -0
- package/src/executor/observations.ts +212 -0
- package/src/executor/paths.ts +159 -0
- package/src/executor/platform.ts +39 -0
- package/src/executor/process-identity.ts +19 -0
- package/src/executor/qa-loop.ts +470 -0
- package/src/executor/recovery.ts +403 -0
- package/src/executor/runtime.ts +6233 -0
- package/src/executor/schedule.ts +246 -0
- package/src/executor/slots/env.ts +145 -0
- package/src/executor/slots/pool.ts +1564 -0
- package/src/executor/slots/provision.ts +853 -0
- package/src/executor/tool.ts +534 -0
- package/src/executor/types.ts +2064 -0
- package/src/executor/unattended.ts +488 -0
- package/src/guard/store.ts +3 -1
- package/src/index.ts +1498 -288
- package/src/router/config.ts +776 -25
- package/src/router/git-tools.ts +106 -15
- package/src/router/leases.ts +1469 -0
- package/src/router/logger.ts +44 -32
- package/src/router/protocol.ts +40 -10
- package/src/router/role-agents.ts +224 -17
- package/src/router/roles-config.ts +10 -1
- package/src/router/roles.ts +1 -1
- package/src/router/run-tools.ts +56 -4
- package/src/router/tool-paths.ts +308 -0
- package/src/router/work-roots.ts +86 -0
- package/src/routing/advisor/findings.ts +25 -5
- package/src/routing/classify/index.ts +7 -1
- package/src/routing/classify/rules.ts +11 -0
- package/src/routing/commands/stats.ts +2 -2
- package/src/routing/outcomes/durations.ts +225 -0
- package/src/routing/outcomes/index.ts +59 -2
- package/src/routing/outcomes/persist.ts +409 -0
- package/src/routing/outcomes/stats.ts +170 -11
- package/src/routing/outcomes/timings.ts +836 -0
- package/src/routing/outcomes/types.ts +98 -0
- package/src/routing/roles/approvals.ts +86 -5
- package/src/routing/roles/binding.ts +99 -9
- package/src/routing/roles/orphans.ts +68 -0
- package/src/routing/roles/policy.ts +21 -3
- package/src/routing/roles/work-root.ts +3 -1
- package/src/routing/wire/dispatch.ts +86 -12
- package/src/routing/wire/hint.ts +56 -8
- package/src/v2.ts +94 -49
- package/src/verify/branch.ts +8 -2
- package/src/verify/canonical-path.ts +35 -0
- package/src/verify/deterministic.ts +38 -9
- package/src/verify/dispatch.ts +64 -28
- package/src/verify/exec.ts +52 -9
- package/src/verify/pending.ts +1691 -10
- package/src/verify/perimeter.ts +12 -6
- package/src/verify/plan-task.ts +729 -0
- package/src/verify/reference.ts +216 -3
- package/src/verify/tree.ts +30 -13
- package/src/verify/types.ts +9 -2
- package/src/verify/wiring.ts +290 -31
- package/tiers.json +5 -5
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { RunConfig } from "../../router/roles";
|
|
2
|
+
import { planRun, type RunHost } from "../../router/run-tools";
|
|
3
|
+
import type { RunResolver } from "./checks";
|
|
4
|
+
|
|
5
|
+
/** Router authorization and slot tool lookup, repeated against each prepared execution checkout by checks. */
|
|
6
|
+
export function createRunResolver(config: () => RunConfig, host: RunHost = { platform: process.platform, env: process.env }): RunResolver {
|
|
7
|
+
return {
|
|
8
|
+
async resolve(ref, cwd) {
|
|
9
|
+
try {
|
|
10
|
+
const plan = planRun({ script: ref.script, args: ref.args, cwd }, cwd, config(), host);
|
|
11
|
+
return { ok: true, command: { label: ref.script, file: plan.executable, args: plan.argv,
|
|
12
|
+
source: plan.kind === "script" ? "package-script" : "run-command", ref, resolution: { cwd: plan.cwd,
|
|
13
|
+
env: Object.fromEntries(Object.entries(plan.env).filter((entry): entry is [string, string] => entry[1] !== undefined)) } } };
|
|
14
|
+
} catch (error) {
|
|
15
|
+
return { ok: false, reason: error instanceof Error ? error.message : String(error) };
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Launcher A (3.1.5): starts plan children through the child runner with the context retained from the bound
|
|
3
|
+
* orchestrator's latest router tool call. Memo 3.1.1 §4-§5, ADR 0007 D14.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { ResumeRejectedError } from "../compat/child-session";
|
|
7
|
+
import {
|
|
8
|
+
ABSOLUTE_PATH_PATTERN,
|
|
9
|
+
CHILD_END_GRACE_MS,
|
|
10
|
+
ExecutorError,
|
|
11
|
+
LAUNCH_CONFIRM_TIMEOUT_MS,
|
|
12
|
+
LAUNCH_HEADER_PATTERN,
|
|
13
|
+
executionFailureOutcome,
|
|
14
|
+
formatLaunchHeader,
|
|
15
|
+
isContextSupersededError,
|
|
16
|
+
isTaskId,
|
|
17
|
+
type CancelReason,
|
|
18
|
+
type ChildEnd,
|
|
19
|
+
type ChildRunner,
|
|
20
|
+
type Clock,
|
|
21
|
+
type DispatchBookkeeping,
|
|
22
|
+
type HostError,
|
|
23
|
+
type HostSessionEvent,
|
|
24
|
+
type HostSessions,
|
|
25
|
+
type IsoTime,
|
|
26
|
+
type LaunchConfirmation,
|
|
27
|
+
type LaunchGate,
|
|
28
|
+
type LaunchHandle,
|
|
29
|
+
type LaunchHooks,
|
|
30
|
+
type LaunchRequest,
|
|
31
|
+
type MetricPayload,
|
|
32
|
+
type PlanId,
|
|
33
|
+
type RetainedContextInfo,
|
|
34
|
+
type RoleBindings,
|
|
35
|
+
type TaskId,
|
|
36
|
+
type TaskLauncher,
|
|
37
|
+
} from "./types";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* QA-PDF21-2-20: `budget` is the route's `budget=` (the guard's per-dispatch budget of the child, as a routed dispatch's;
|
|
41
|
+
* absent: none named); `prompt` is the launch prompt, whose cap line sets the child's read cap (a resume's names none).
|
|
42
|
+
*/
|
|
43
|
+
export type PlanBindInput = Parameters<RoleBindings["bind"]>[0] & {
|
|
44
|
+
planId: PlanId; taskId: TaskId; attempt: number; budget?: number | null; prompt?: string; tier?: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Binding of a plan child; the production adapter maps `bind` to `bindPlanChild` and `evict` to binding.ts `evict`
|
|
49
|
+
* (which tombstones the id: nothing binds it again). pdf2-1 (a): a completed child whose launch holds its binding
|
|
50
|
+
* (`LaunchHooks.holdCompletedBinding`) is evicted only once the runtime decided not to resume it; a budget resume of the
|
|
51
|
+
* same child under the same plan, task and attempt takes the live binding over instead of binding it again.
|
|
52
|
+
* QA-PDF21-2-20: `bind` also carries the launch prompt (the adapter registers the child's read cap from the executor's cap
|
|
53
|
+
* line) and the route `budget=` (the binding's call budget); QA-PDF21-2-11: a resume's new guard round is the bookkeeping's.
|
|
54
|
+
*/
|
|
55
|
+
export interface PlanRoleBindings {
|
|
56
|
+
bind(i: PlanBindInput): void;
|
|
57
|
+
evict(childSessionID: string): void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface TaskLauncherDeps {
|
|
61
|
+
runner: ChildRunner;
|
|
62
|
+
hosts: HostSessions;
|
|
63
|
+
bindings: PlanRoleBindings;
|
|
64
|
+
bookkeeping: DispatchBookkeeping;
|
|
65
|
+
clock: Clock;
|
|
66
|
+
/** Plugin lifetime; combined with the per-launch controller. */
|
|
67
|
+
lifetime: AbortSignal;
|
|
68
|
+
knownAgent(agent: string): boolean;
|
|
69
|
+
onMetric(planId: PlanId, m: Extract<MetricPayload, { name: "context-age" }>): void;
|
|
70
|
+
/** Default {@link LAUNCH_CONFIRM_TIMEOUT_MS}. */
|
|
71
|
+
confirmTimeoutMs?: number;
|
|
72
|
+
/** Default {@link CHILD_END_GRACE_MS}. */
|
|
73
|
+
endGraceMs?: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Wake key of a gate that is `context-stale` (A1-6): one per capture epoch. */
|
|
77
|
+
export function staleContextKey(bootID: string, epoch: number): string {
|
|
78
|
+
return `stale-context:${bootID}:${epoch}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Wake key of a gate with `no-context`: a capture of the session existed in this process and is gone. */
|
|
82
|
+
export function noContextKey(bootID: string, sessionID: string, lastEpoch: number): string {
|
|
83
|
+
return `no-context:${bootID}:${sessionID}:${lastEpoch}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Wake key of guard 6: nothing captured yet in this process (a restart yields a new bootID). */
|
|
87
|
+
export function awaitRouterCallKey(bootID: string, sessionID: string): string {
|
|
88
|
+
return `await-router-call:${bootID}:${sessionID}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
type TerminalEvent =
|
|
92
|
+
| { type: "execution.failed"; sessionID: string; error: HostError; at: IsoTime }
|
|
93
|
+
| { type: "execution.interrupted" | "execution.completed"; sessionID: string; at: IsoTime };
|
|
94
|
+
type ContextAgeResult = Extract<MetricPayload, { name: "context-age" }>["result"];
|
|
95
|
+
|
|
96
|
+
function isTerminal(e: HostSessionEvent): e is HostSessionEvent & TerminalEvent {
|
|
97
|
+
return e.type === "execution.failed" || e.type === "execution.interrupted" || e.type === "execution.completed";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function messageOf(error: unknown): string {
|
|
101
|
+
return error instanceof Error ? error.message : String(error);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** QA-3.1-15: per-session maps (`lastEpoch`, `staleEpoch`) keep at most this many sessions; the least recently touched go first. */
|
|
105
|
+
export const MAX_TRACKED_SESSIONS = 256;
|
|
106
|
+
|
|
107
|
+
const CONTEXT_ERROR_TEXT =
|
|
108
|
+
/session[ _-]?not[ _-]?found|(?:context|capture)[ _-]?(?:superseded|expired|aborted|stale|invalid|gone|missing|not[ _-]?found)|(?:expired|aborted|stale|superseded)[ _-]?(?:context|capture)/i;
|
|
109
|
+
|
|
110
|
+
/** Must match runtime.ts RESUME_PROMPT; resumes continue the existing child without a fresh plan header. */
|
|
111
|
+
const RESUME_PROMPT = "continue and finish";
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* QA-3.1-4: whether a runner error before confirmation says the retained context itself is unusable (so the capture is
|
|
115
|
+
* marked stale), as opposed to a deterministic runner failure that a fresh capture would not change (native subagent tool
|
|
116
|
+
* unavailable, unknown agent, validation errors, ...). Context-related: `ContextSupersededError`, a host error whose
|
|
117
|
+
* `type`/`code` is `context` or whose name/type/code/message names a superseded, expired, aborted, stale or missing
|
|
118
|
+
* context/capture, or a "session not found" host error.
|
|
119
|
+
*/
|
|
120
|
+
export function isContextRelatedRunnerError(error: unknown): boolean {
|
|
121
|
+
if (isContextSupersededError(error)) return true;
|
|
122
|
+
if (typeof error !== "object" || error === null) return false;
|
|
123
|
+
const e = error as { name?: unknown; type?: unknown; code?: unknown; message?: unknown };
|
|
124
|
+
const tag = (v: unknown): string => (typeof v === "string" ? v : "");
|
|
125
|
+
if (tag(e.type).toLowerCase() === "context" || tag(e.code).toLowerCase() === "context") return true;
|
|
126
|
+
return CONTEXT_ERROR_TEXT.test(`${tag(e.name)} ${tag(e.type)} ${tag(e.code)} ${tag(e.message)}`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** One launch: the executor's view of a child from `launch()` until both its completion and its runner have settled. */
|
|
130
|
+
interface Launch {
|
|
131
|
+
readonly req: LaunchRequest;
|
|
132
|
+
readonly hooks: LaunchHooks;
|
|
133
|
+
readonly key: string;
|
|
134
|
+
readonly controller: AbortController;
|
|
135
|
+
readonly contextInfo: RetainedContextInfo;
|
|
136
|
+
readonly ageMs: number;
|
|
137
|
+
readonly startedAt: IsoTime;
|
|
138
|
+
childId: string | null;
|
|
139
|
+
cancelReason: CancelReason | null;
|
|
140
|
+
/** `onCreated` (fresh child) or the first step (resume) entered before the confirmation deadline. */
|
|
141
|
+
hookEntered: boolean;
|
|
142
|
+
timedOut: boolean;
|
|
143
|
+
confirmSettled: boolean;
|
|
144
|
+
confirmed: boolean;
|
|
145
|
+
/** Why the confirmation was not given (when it was not). */
|
|
146
|
+
unconfirmed: Extract<LaunchConfirmation, { kind: "unconfirmed" }>["reason"] | null;
|
|
147
|
+
completionSettled: boolean;
|
|
148
|
+
runnerDone: boolean;
|
|
149
|
+
/** The runner rejected after the confirmation; the child's terminal event decides. */
|
|
150
|
+
runnerFailed: boolean;
|
|
151
|
+
terminal: TerminalEvent | null;
|
|
152
|
+
inFlightRegistered: boolean;
|
|
153
|
+
bound: boolean;
|
|
154
|
+
confirmTimer: { readonly id: number } | null;
|
|
155
|
+
graceTimer: { readonly id: number } | null;
|
|
156
|
+
resolveConfirmation(c: LaunchConfirmation): void;
|
|
157
|
+
rejectConfirmation(error: unknown): void;
|
|
158
|
+
resolveCompletion(e: ChildEnd): void;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Launcher A (3.1.5): see {@link TaskLauncher}. */
|
|
162
|
+
export function createTaskLauncher(d: TaskLauncherDeps): TaskLauncher {
|
|
163
|
+
const confirmTimeoutMs = d.confirmTimeoutMs ?? LAUNCH_CONFIRM_TIMEOUT_MS;
|
|
164
|
+
const endGraceMs = d.endGraceMs ?? CHILD_END_GRACE_MS;
|
|
165
|
+
let disposed = false;
|
|
166
|
+
const launches = new Map<string, Launch>();
|
|
167
|
+
const byChild = new Map<string, Launch>();
|
|
168
|
+
/** Newest capture epoch seen per session (for the `no-context` key). */
|
|
169
|
+
const lastEpoch = new Map<string, number>();
|
|
170
|
+
/** Captures of a session up to this epoch no longer launch anything (D14 guard 2). */
|
|
171
|
+
const staleEpoch = new Map<string, number>();
|
|
172
|
+
/**
|
|
173
|
+
* pdf2-1 (a): live bindings of completed children kept for a budget resume (`LaunchHooks.holdCompletedBinding`), by
|
|
174
|
+
* child session, with the plan, task and attempt they were bound for. Dropped by `releaseBinding`, taken over by a
|
|
175
|
+
* resume of the same child under the same plan, task and attempt, evicted on dispose.
|
|
176
|
+
*/
|
|
177
|
+
const held = new Map<string, { planId: PlanId; taskId: TaskId; attempt: number }>();
|
|
178
|
+
|
|
179
|
+
const launchKey = (taskId: TaskId, attempt: number): string => `${taskId}#${attempt}`;
|
|
180
|
+
|
|
181
|
+
const offCapture = d.runner.onContextCaptured((c) => {
|
|
182
|
+
setBounded(lastEpoch, c.sessionID, Math.max(lastEpoch.get(c.sessionID) ?? 0, c.epoch));
|
|
183
|
+
});
|
|
184
|
+
const offEvents = d.hosts.onEvent((e) => {
|
|
185
|
+
if (e.type === "step.started") {
|
|
186
|
+
const l = byChild.get(e.sessionID);
|
|
187
|
+
if (l !== undefined && l.req.resume !== undefined && !l.confirmSettled && l.cancelReason === null && !disposed) {
|
|
188
|
+
l.hookEntered = true;
|
|
189
|
+
if (l.confirmTimer !== null) d.clock.clearTimer(l.confirmTimer);
|
|
190
|
+
l.confirmTimer = null;
|
|
191
|
+
settleConfirmation(l, {
|
|
192
|
+
kind: "confirmed", childSessionID: e.sessionID, latencyMs: Math.max(0, d.clock.now() - Date.parse(l.startedAt)),
|
|
193
|
+
}, "confirmed");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (isTerminal(e)) onTerminal(e);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
/** Sets `key` as the most recent entry and drops the oldest ones beyond {@link MAX_TRACKED_SESSIONS} (never a session with a live launch). */
|
|
200
|
+
const setBounded = (map: Map<string, number>, key: string, value: number): void => {
|
|
201
|
+
map.delete(key);
|
|
202
|
+
map.set(key, value);
|
|
203
|
+
if (map.size <= MAX_TRACKED_SESSIONS) return;
|
|
204
|
+
const live = new Set<string>();
|
|
205
|
+
for (const l of launches.values()) live.add(l.req.orchestratorSessionID);
|
|
206
|
+
for (const k of map.keys()) {
|
|
207
|
+
if (map.size <= MAX_TRACKED_SESSIONS) break;
|
|
208
|
+
if (k !== key && !live.has(k)) map.delete(k);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const markStale = (sessionID: string, epoch: number): void => {
|
|
213
|
+
setBounded(staleEpoch, sessionID, Math.max(staleEpoch.get(sessionID) ?? 0, epoch));
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const gate = (orchestratorSessionID: string): LaunchGate => {
|
|
217
|
+
if (disposed) return { ok: false, reason: "disposed", wake: null };
|
|
218
|
+
const cap = d.runner.retainedContext(orchestratorSessionID);
|
|
219
|
+
if (cap === null) {
|
|
220
|
+
const seen = lastEpoch.get(orchestratorSessionID);
|
|
221
|
+
if (seen !== undefined) {
|
|
222
|
+
return {
|
|
223
|
+
ok: false, reason: "no-context",
|
|
224
|
+
wake: { reason: "stale-context", stateKey: noContextKey(d.runner.bootID, orchestratorSessionID, seen) },
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
ok: false, reason: "awaiting-first-router-call",
|
|
229
|
+
wake: { reason: "stale-context", stateKey: awaitRouterCallKey(d.runner.bootID, orchestratorSessionID) },
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
setBounded(lastEpoch, orchestratorSessionID, Math.max(lastEpoch.get(orchestratorSessionID) ?? 0, cap.epoch));
|
|
233
|
+
if (cap.epoch <= (staleEpoch.get(orchestratorSessionID) ?? 0)) {
|
|
234
|
+
return {
|
|
235
|
+
ok: false, reason: "context-stale",
|
|
236
|
+
wake: { reason: "stale-context", stateKey: staleContextKey(cap.bootID, cap.epoch) },
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
return { ok: true, context: cap, ageMs: Math.max(0, d.clock.now() - Date.parse(cap.capturedAt)) };
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const clearTimers = (l: Launch): void => {
|
|
243
|
+
if (l.confirmTimer !== null) d.clock.clearTimer(l.confirmTimer);
|
|
244
|
+
if (l.graceTimer !== null) d.clock.clearTimer(l.graceTimer);
|
|
245
|
+
l.confirmTimer = null;
|
|
246
|
+
l.graceTimer = null;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const evictBinding = (childId: string): void => {
|
|
250
|
+
try { d.bindings.evict(childId); } catch { /* the binding is dropped with the session anyway */ }
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const release = (l: Launch, status?: ChildEnd["kind"]): void => {
|
|
254
|
+
if (l.inFlightRegistered) {
|
|
255
|
+
l.inFlightRegistered = false;
|
|
256
|
+
// QA-3.1-24: the settled end kind, when known, so the bookkeeping does not guess it
|
|
257
|
+
try { d.bookkeeping.unregisterInFlight(l.req.taskId, l.req.attempt, status); } catch { /* bookkeeping never fails a launch */ }
|
|
258
|
+
}
|
|
259
|
+
if (l.bound && l.childId !== null) {
|
|
260
|
+
l.bound = false;
|
|
261
|
+
// pdf2-1 (a): a completed child may be resumed for its budget; its binding waits for the caller's decision
|
|
262
|
+
// (`releaseBinding`, or a resume of the same child under the same task and attempt). Every other end evicts here.
|
|
263
|
+
if (status === "completed" && l.hooks.holdCompletedBinding === true && !disposed) {
|
|
264
|
+
held.set(l.childId, { planId: l.req.planId, taskId: l.req.taskId, attempt: l.req.attempt });
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
evictBinding(l.childId);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const forget = (l: Launch): void => {
|
|
272
|
+
if (!l.completionSettled || !l.runnerDone) return;
|
|
273
|
+
if (launches.get(l.key) === l) launches.delete(l.key);
|
|
274
|
+
if (l.childId !== null && byChild.get(l.childId) === l) byChild.delete(l.childId);
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const settleCompletion = (l: Launch, end: ChildEnd): void => {
|
|
278
|
+
if (l.completionSettled) return;
|
|
279
|
+
l.completionSettled = true;
|
|
280
|
+
clearTimers(l);
|
|
281
|
+
release(l, end.kind);
|
|
282
|
+
l.resolveCompletion(end);
|
|
283
|
+
forget(l);
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const settleConfirmation = (l: Launch, c: LaunchConfirmation, result: ContextAgeResult, rejection?: unknown): void => {
|
|
287
|
+
if (l.confirmSettled) return;
|
|
288
|
+
l.confirmSettled = true;
|
|
289
|
+
if (c.kind === "confirmed") l.confirmed = true;
|
|
290
|
+
else l.unconfirmed = c.reason;
|
|
291
|
+
// Guard 4: the age of the context is recorded with every result, never gated.
|
|
292
|
+
try {
|
|
293
|
+
d.onMetric(l.req.planId, {
|
|
294
|
+
name: "context-age", taskId: l.req.taskId, attempt: l.req.attempt,
|
|
295
|
+
bootID: l.contextInfo.bootID, epoch: l.contextInfo.epoch, callID: l.contextInfo.callID,
|
|
296
|
+
capturedAt: l.contextInfo.capturedAt, ageMs: l.ageMs, result,
|
|
297
|
+
});
|
|
298
|
+
} catch { /* a metric sink never fails a launch */ }
|
|
299
|
+
if (rejection !== undefined) l.rejectConfirmation(rejection);
|
|
300
|
+
else l.resolveConfirmation(c);
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const unconfirm = (
|
|
304
|
+
l: Launch, reason: Extract<LaunchConfirmation, { kind: "unconfirmed" }>["reason"], error: unknown,
|
|
305
|
+
): void => {
|
|
306
|
+
settleConfirmation(l, { kind: "unconfirmed", reason, error: error === null ? null : messageOf(error) }, reason);
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const lastAssistantText = async (child: string): Promise<string> => {
|
|
310
|
+
try {
|
|
311
|
+
const { messages } = await d.hosts.context(child);
|
|
312
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
313
|
+
const m = messages[i];
|
|
314
|
+
if (m !== undefined && m.role === "assistant" && m.text !== null) return m.text;
|
|
315
|
+
}
|
|
316
|
+
} catch { /* an unreadable history gives an empty result text */ }
|
|
317
|
+
return "";
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
/** Classifies a launch from the terminal `session.execution.*` event of its child (D2 fallback, guard 5). */
|
|
321
|
+
const classifyTerminal = async (l: Launch, e: TerminalEvent): Promise<void> => {
|
|
322
|
+
const child = l.childId;
|
|
323
|
+
if (child === null || l.completionSettled) return;
|
|
324
|
+
const endedAt = d.clock.iso();
|
|
325
|
+
if (e.type === "execution.failed") {
|
|
326
|
+
settleCompletion(l, { kind: executionFailureOutcome(e.error), childSessionID: child, error: e.error, endedAt });
|
|
327
|
+
l.controller.abort();
|
|
328
|
+
} else if (e.type === "execution.interrupted") {
|
|
329
|
+
settleCompletion(l, l.cancelReason !== null
|
|
330
|
+
? { kind: "interrupted", childSessionID: child, reason: l.cancelReason, endedAt }
|
|
331
|
+
: { kind: "lost", childSessionID: child, reason: "unrequested-interrupt", endedAt });
|
|
332
|
+
l.controller.abort();
|
|
333
|
+
} else {
|
|
334
|
+
const text = await lastAssistantText(child);
|
|
335
|
+
settleCompletion(l, { kind: "completed", childSessionID: child, text, endedAt: d.clock.iso() });
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
function onTerminal(e: TerminalEvent): void {
|
|
340
|
+
const l = byChild.get(e.sessionID);
|
|
341
|
+
if (l === undefined || l.terminal !== null) return;
|
|
342
|
+
l.terminal = e;
|
|
343
|
+
if (l.completionSettled || !l.confirmed) return;
|
|
344
|
+
if (l.runnerFailed || e.type !== "execution.completed") {
|
|
345
|
+
void classifyTerminal(l, e);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
// `execution.completed` before the runner returned: wait up to the grace for the runner's text, then read the history.
|
|
349
|
+
if (l.graceTimer === null) {
|
|
350
|
+
l.graceTimer = d.clock.setTimer(endGraceMs, () => {
|
|
351
|
+
l.graceTimer = null;
|
|
352
|
+
void classifyTerminal(l, e);
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const onRunnerResult = (l: Launch, result: { sessionID: string; text: string }): void => {
|
|
358
|
+
l.runnerDone = true;
|
|
359
|
+
if (!l.confirmed) {
|
|
360
|
+
if (!l.confirmSettled) {
|
|
361
|
+
// No error to classify: a runner that returns without ever creating the child leaves the context in doubt.
|
|
362
|
+
markStale(l.req.orchestratorSessionID, l.req.context.epoch);
|
|
363
|
+
unconfirm(l, "threw", l.req.resume !== undefined
|
|
364
|
+
? "the runner returned without a first step from the resumed child"
|
|
365
|
+
: "the runner returned without creating a confirmed child");
|
|
366
|
+
}
|
|
367
|
+
settleCompletion(l, { kind: "lost", childSessionID: l.childId, reason: "runner-error", endedAt: d.clock.iso() });
|
|
368
|
+
release(l);
|
|
369
|
+
forget(l);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
settleCompletion(l, { kind: "completed", childSessionID: l.childId ?? result.sessionID, text: result.text, endedAt: d.clock.iso() });
|
|
373
|
+
forget(l);
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const onRunnerError = (l: Launch, error: unknown): void => {
|
|
377
|
+
if (l.req.resume !== undefined && error instanceof ResumeRejectedError) {
|
|
378
|
+
error = new ExecutorError("E_NO_CONTEXT", error.message, { childSessionID: error.sessionID });
|
|
379
|
+
}
|
|
380
|
+
l.runnerDone = true;
|
|
381
|
+
if (l.completionSettled) {
|
|
382
|
+
// Already settled (a confirmation timeout or an event): only the leftovers of a late-adopted child remain.
|
|
383
|
+
release(l);
|
|
384
|
+
forget(l);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const at = d.clock.iso();
|
|
388
|
+
if (!l.confirmed) {
|
|
389
|
+
if (l.req.resume !== undefined && !l.confirmSettled && l.cancelReason === null
|
|
390
|
+
&& typeof error === "object" && error !== null && "code" in error && error.code === "E_NO_CONTEXT") {
|
|
391
|
+
// A missing resume session is not a stale orchestrator capture. Keep the typed error for Rt to park this attempt.
|
|
392
|
+
settleConfirmation(l, { kind: "unconfirmed", reason: "threw", error: messageOf(error) }, "threw", error);
|
|
393
|
+
settleCompletion(l, { kind: "lost", childSessionID: l.childId, reason: "runner-error", endedAt: at });
|
|
394
|
+
forget(l);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (l.cancelReason !== null && (l.childId === null || l.req.resume !== undefined)) {
|
|
398
|
+
unconfirm(l, "threw", error);
|
|
399
|
+
settleCompletion(l, { kind: "interrupted", childSessionID: l.childId, reason: l.cancelReason, endedAt: at });
|
|
400
|
+
} else if (isContextSupersededError(error)) {
|
|
401
|
+
unconfirm(l, "superseded", error);
|
|
402
|
+
settleCompletion(l, { kind: "lost", childSessionID: null, reason: "context-superseded", endedAt: at });
|
|
403
|
+
} else if (l.confirmSettled) {
|
|
404
|
+
// task-error / hook-failed / timeout: the child was never prompted.
|
|
405
|
+
settleCompletion(l, { kind: "lost", childSessionID: l.childId, reason: "unconfirmed", endedAt: at });
|
|
406
|
+
} else {
|
|
407
|
+
// QA-3.1-4: a deterministic runner error leaves the retained context usable.
|
|
408
|
+
if (isContextRelatedRunnerError(error)) markStale(l.req.orchestratorSessionID, l.req.context.epoch);
|
|
409
|
+
unconfirm(l, "threw", error);
|
|
410
|
+
settleCompletion(l, { kind: "lost", childSessionID: l.childId, reason: "runner-error", endedAt: at });
|
|
411
|
+
}
|
|
412
|
+
forget(l);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
l.runnerFailed = true;
|
|
416
|
+
if (l.terminal !== null) {
|
|
417
|
+
void classifyTerminal(l, l.terminal);
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (l.graceTimer !== null) return; // an `execution.completed` fallback is already pending
|
|
421
|
+
l.graceTimer = d.clock.setTimer(endGraceMs, () => {
|
|
422
|
+
l.graceTimer = null;
|
|
423
|
+
const end: ChildEnd = l.cancelReason !== null
|
|
424
|
+
? { kind: "interrupted", childSessionID: l.childId, reason: l.cancelReason, endedAt: d.clock.iso() }
|
|
425
|
+
: { kind: "lost", childSessionID: l.childId, reason: "runner-error", endedAt: d.clock.iso() };
|
|
426
|
+
settleCompletion(l, end);
|
|
427
|
+
});
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
const bindChild = (l: Launch, child: string): void => {
|
|
431
|
+
const { req } = l;
|
|
432
|
+
d.bindings.bind({
|
|
433
|
+
childSessionID: child, role: req.role, agent: req.route.agent, workRoot: req.workRoot, grant: [...req.grant],
|
|
434
|
+
planId: req.planId, taskId: req.taskId, attempt: req.attempt, prompt: req.prompt,
|
|
435
|
+
...(req.route.tier === undefined ? {} : { tier: req.route.tier }),
|
|
436
|
+
// QA-PDF21-2-20: the route `budget=` guards the plan child as it guards a routed role child (absent: kept on a re-bind).
|
|
437
|
+
...(req.routeBudget !== undefined ? { budget: req.routeBudget } : {}),
|
|
438
|
+
});
|
|
439
|
+
l.bound = true;
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
const adopt = (l: Launch, child: string): void => {
|
|
443
|
+
l.childId = child;
|
|
444
|
+
byChild.set(child, l);
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
/** The runner's hook: awaited before the child is moved and prompted. */
|
|
448
|
+
const onCreated = async (l: Launch, child: string): Promise<void> => {
|
|
449
|
+
if (disposed || l.cancelReason !== null) throw new Error("[model-router] launch cancelled before the child was prompted");
|
|
450
|
+
if (l.req.resume !== undefined) {
|
|
451
|
+
// The runner registers a resumed session through this hook too, but it creates no child and journals no AC/AA.
|
|
452
|
+
if (child !== l.req.resume.childSessionID) throw new ExecutorError("E_NO_CONTEXT", "the runner did not resume the requested child");
|
|
453
|
+
if (l.confirmSettled && !l.confirmed) throw new Error("[model-router] resume was not confirmed in time");
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
if (!l.timedOut) {
|
|
457
|
+
// In time (guard 1): from here the confirmation waits for this hook, however long it takes.
|
|
458
|
+
l.hookEntered = true;
|
|
459
|
+
if (l.confirmTimer !== null) d.clock.clearTimer(l.confirmTimer);
|
|
460
|
+
l.confirmTimer = null;
|
|
461
|
+
adopt(l, child);
|
|
462
|
+
try {
|
|
463
|
+
bindChild(l, child);
|
|
464
|
+
} catch (error) {
|
|
465
|
+
unconfirm(l, "task-error", error);
|
|
466
|
+
throw error;
|
|
467
|
+
}
|
|
468
|
+
d.bookkeeping.registerInFlight(l.req.taskId, l.req.attempt, child);
|
|
469
|
+
l.inFlightRegistered = true;
|
|
470
|
+
try {
|
|
471
|
+
await l.hooks.onChildCreated(child);
|
|
472
|
+
} catch (error) {
|
|
473
|
+
unconfirm(l, "hook-failed", error);
|
|
474
|
+
throw error;
|
|
475
|
+
}
|
|
476
|
+
settleConfirmation(l, { kind: "confirmed", childSessionID: child, latencyMs: Math.max(0, d.clock.now() - Date.parse(l.startedAt)) }, "confirmed");
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
// Late: the confirmation already failed; Rt decides whether the child still belongs to the task.
|
|
480
|
+
let decision: "adopt" | "refuse";
|
|
481
|
+
try {
|
|
482
|
+
decision = await l.hooks.onLateChild(child);
|
|
483
|
+
} catch {
|
|
484
|
+
decision = "refuse";
|
|
485
|
+
}
|
|
486
|
+
if (decision === "refuse") throw new Error("[model-router] late child refused");
|
|
487
|
+
adopt(l, child);
|
|
488
|
+
bindChild(l, child);
|
|
489
|
+
d.bookkeeping.registerInFlight(l.req.taskId, l.req.attempt, child);
|
|
490
|
+
l.inFlightRegistered = true;
|
|
491
|
+
await l.hooks.onChildCreated(child);
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
const launch = (req: LaunchRequest, hooks: LaunchHooks): LaunchHandle => {
|
|
495
|
+
if (disposed) throw new ExecutorError("E_DISPOSED", "the launcher is disposed");
|
|
496
|
+
const g = gate(req.orchestratorSessionID);
|
|
497
|
+
const current = d.runner.retainedContext(req.orchestratorSessionID);
|
|
498
|
+
if (!g.ok || current === null || current.bootID !== req.context.bootID || current.epoch !== req.context.epoch
|
|
499
|
+
|| current.callID !== req.context.callID) {
|
|
500
|
+
throw new ExecutorError("E_NO_CONTEXT", "the launch context is not the current capture of the orchestrator", {
|
|
501
|
+
reason: g.ok ? "superseded" : g.reason,
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
const bad = (message: string): never => {
|
|
505
|
+
throw new ExecutorError("E_LAUNCH_TASK", message, { taskId: req.taskId, attempt: req.attempt });
|
|
506
|
+
};
|
|
507
|
+
if (req.resume !== undefined) {
|
|
508
|
+
if (req.prompt !== RESUME_PROMPT) bad("the resume prompt does not match the runtime resume prompt");
|
|
509
|
+
if (typeof req.planId !== "string" || typeof req.nonce !== "string" || !isTaskId(req.taskId)) {
|
|
510
|
+
bad("the resume plan, task or nonce is invalid");
|
|
511
|
+
}
|
|
512
|
+
formatLaunchHeader(req); // Validate the plan, task, attempt and nonce even though no header is sent.
|
|
513
|
+
if (typeof req.resume.childSessionID !== "string" || req.resume.childSessionID.trim() === "") {
|
|
514
|
+
bad("the resume child session id is empty");
|
|
515
|
+
}
|
|
516
|
+
// pdf2-1 (a): a held binding is taken over only by the identical plan child; never re-bound to another identity.
|
|
517
|
+
const kept = held.get(req.resume.childSessionID);
|
|
518
|
+
if (kept !== undefined && (kept.planId !== req.planId || kept.taskId !== req.taskId || kept.attempt !== req.attempt)) {
|
|
519
|
+
bad("the resume child is bound to another plan, task or attempt");
|
|
520
|
+
}
|
|
521
|
+
} else {
|
|
522
|
+
const header = LAUNCH_HEADER_PATTERN.exec(req.prompt);
|
|
523
|
+
// An editing task's read-cap line follows this opening line (DF-2-9), so the opening check holds for every prompt.
|
|
524
|
+
if (!req.prompt.startsWith("PLAN CONTEXT\n")) bad("the prompt does not start with PLAN CONTEXT");
|
|
525
|
+
if (header === null || header[1] !== req.planId || header[2] !== req.taskId || header[3] !== String(req.attempt)
|
|
526
|
+
|| header[4] !== req.nonce) bad("the prompt header does not match the launch request");
|
|
527
|
+
}
|
|
528
|
+
if (!d.knownAgent(req.route.agent)) bad(`unknown agent ${req.route.agent}`);
|
|
529
|
+
if (!ABSOLUTE_PATH_PATTERN.test(req.workRoot)) bad("the work root is not an absolute path");
|
|
530
|
+
const key = launchKey(req.taskId, req.attempt);
|
|
531
|
+
for (const other of launches.values()) {
|
|
532
|
+
// QA-PDF21-2-17: a resume may follow its own attempt's run whose completion already settled (the child's terminal event
|
|
533
|
+
// after the grace) while that run's runner has not returned yet: the same plan, task, attempt and child. That record is
|
|
534
|
+
// no launch in flight; the identity checks of `forget` keep its late runner result from touching the resume's records.
|
|
535
|
+
if (req.resume !== undefined && other.key === key && other.completionSettled && other.req.planId === req.planId
|
|
536
|
+
&& other.childId === req.resume.childSessionID) continue;
|
|
537
|
+
if (other.key === key || (other.req.taskId === req.taskId && !other.completionSettled)) {
|
|
538
|
+
bad("the task already has a launch in flight");
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
let resolveConfirmation: (c: LaunchConfirmation) => void = () => undefined;
|
|
543
|
+
let rejectConfirmation: (error: unknown) => void = () => undefined;
|
|
544
|
+
let resolveCompletion: (e: ChildEnd) => void = () => undefined;
|
|
545
|
+
const confirmation = new Promise<LaunchConfirmation>((resolve, reject) => { resolveConfirmation = resolve; rejectConfirmation = reject; });
|
|
546
|
+
const completion = new Promise<ChildEnd>((resolve) => { resolveCompletion = resolve; });
|
|
547
|
+
const startedAt = d.clock.iso();
|
|
548
|
+
const l: Launch = {
|
|
549
|
+
req, key, hooks, controller: new AbortController(), contextInfo: current, ageMs: g.ageMs, startedAt,
|
|
550
|
+
childId: null, cancelReason: null, hookEntered: false, timedOut: false, confirmSettled: false, confirmed: false,
|
|
551
|
+
unconfirmed: null, completionSettled: false, runnerDone: false, runnerFailed: false, terminal: null,
|
|
552
|
+
inFlightRegistered: false, bound: false, confirmTimer: null, graceTimer: null,
|
|
553
|
+
resolveConfirmation, rejectConfirmation, resolveCompletion,
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
// A retained launch fires no `subagent` hook, so the executor writes the dispatch-time rows itself.
|
|
557
|
+
d.bookkeeping.decisionRow(req);
|
|
558
|
+
d.bookkeeping.recordDispatchStart(req, startedAt);
|
|
559
|
+
d.bookkeeping.registerInFlight(req.taskId, req.attempt, req.resume?.childSessionID ?? null);
|
|
560
|
+
l.inFlightRegistered = true;
|
|
561
|
+
launches.set(key, l);
|
|
562
|
+
|
|
563
|
+
l.confirmTimer = d.clock.setTimer(confirmTimeoutMs, () => {
|
|
564
|
+
l.confirmTimer = null;
|
|
565
|
+
if (l.hookEntered || l.confirmSettled) return; // 30 s bounds child creation / the resumed child's first step, not our own hook
|
|
566
|
+
l.timedOut = true;
|
|
567
|
+
markStale(req.orchestratorSessionID, req.context.epoch);
|
|
568
|
+
unconfirm(l, "timeout", null);
|
|
569
|
+
settleCompletion(l, { kind: "lost", childSessionID: req.resume?.childSessionID ?? null, reason: "unconfirmed", endedAt: d.clock.iso() });
|
|
570
|
+
if (req.resume !== undefined) l.controller.abort(); // There is no late child to adopt on a resume.
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
const signal = AbortSignal.any([l.controller.signal, d.lifetime]);
|
|
574
|
+
let running: Promise<{ sessionID: string; text: string }>;
|
|
575
|
+
try {
|
|
576
|
+
if (req.resume !== undefined) {
|
|
577
|
+
const child = req.resume.childSessionID;
|
|
578
|
+
adopt(l, child);
|
|
579
|
+
const kept = held.get(child);
|
|
580
|
+
if (kept !== undefined && kept.planId === req.planId && kept.taskId === req.taskId && kept.attempt === req.attempt) {
|
|
581
|
+
// pdf2-1 (a): the binding of the budget end is still live for this very attempt: the resume takes it over.
|
|
582
|
+
held.delete(child);
|
|
583
|
+
l.bound = true;
|
|
584
|
+
} else {
|
|
585
|
+
bindChild(l, child);
|
|
586
|
+
}
|
|
587
|
+
// QA-PDF21-2-11: the resume is a NEW guard round and read-cap round for its child, as a routed resume is; without it the
|
|
588
|
+
// child keeps the exhausted per-dispatch budget of the round that ended NEED MORE: budget (the cumulative ceiling stays).
|
|
589
|
+
try { d.bookkeeping.beginResumeRound?.(child, req.route.agent, req.prompt); } catch { /* bookkeeping never fails a launch */ }
|
|
590
|
+
}
|
|
591
|
+
running = d.runner.run({
|
|
592
|
+
parentSessionID: req.orchestratorSessionID,
|
|
593
|
+
...(req.resume !== undefined ? { resumeSessionID: req.resume.childSessionID } : {}),
|
|
594
|
+
prompt: req.prompt,
|
|
595
|
+
agent: req.route.agent,
|
|
596
|
+
model: req.route.model,
|
|
597
|
+
cwd: req.workRoot,
|
|
598
|
+
signal,
|
|
599
|
+
detached: true,
|
|
600
|
+
context: req.context,
|
|
601
|
+
onCreated: (child) => onCreated(l, child),
|
|
602
|
+
});
|
|
603
|
+
} catch (error) {
|
|
604
|
+
running = Promise.reject(error);
|
|
605
|
+
}
|
|
606
|
+
running.then((result) => onRunnerResult(l, result), (error: unknown) => onRunnerError(l, error));
|
|
607
|
+
|
|
608
|
+
return {
|
|
609
|
+
taskId: req.taskId, attempt: req.attempt, nonce: req.nonce, context: req.context,
|
|
610
|
+
contextAgeMs: g.ageMs, startedAt, confirmation, completion,
|
|
611
|
+
};
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
const cancel = async (taskId: TaskId, attempt: number, reason: CancelReason): Promise<void> => {
|
|
615
|
+
const l = launches.get(launchKey(taskId, attempt));
|
|
616
|
+
if (l === undefined || l.cancelReason !== null) return;
|
|
617
|
+
l.cancelReason = reason;
|
|
618
|
+
l.controller.abort();
|
|
619
|
+
if (l.childId !== null) {
|
|
620
|
+
try { await d.hosts.interrupt(l.childId); } catch { /* the runner interrupts on abort as well */ }
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
return {
|
|
625
|
+
gate,
|
|
626
|
+
launch,
|
|
627
|
+
cancel,
|
|
628
|
+
inFlight() {
|
|
629
|
+
return [...launches.values()].map((l) => ({
|
|
630
|
+
taskId: l.req.taskId, attempt: l.req.attempt, childSessionID: l.childId, startedAt: l.startedAt,
|
|
631
|
+
}));
|
|
632
|
+
},
|
|
633
|
+
releaseBinding(childSessionID) {
|
|
634
|
+
if (!held.delete(childSessionID)) return;
|
|
635
|
+
evictBinding(childSessionID);
|
|
636
|
+
},
|
|
637
|
+
async dispose() {
|
|
638
|
+
if (disposed) return;
|
|
639
|
+
disposed = true;
|
|
640
|
+
offCapture();
|
|
641
|
+
offEvents();
|
|
642
|
+
for (const child of [...held.keys()]) {
|
|
643
|
+
held.delete(child);
|
|
644
|
+
evictBinding(child);
|
|
645
|
+
}
|
|
646
|
+
for (const l of launches.values()) {
|
|
647
|
+
if (l.cancelReason === null) l.cancelReason = "dispose";
|
|
648
|
+
l.controller.abort();
|
|
649
|
+
}
|
|
650
|
+
},
|
|
651
|
+
};
|
|
652
|
+
}
|