opencode-model-router 2.2.0 → 2.4.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/CHANGELOG.md +214 -0
- package/README.md +90 -12
- package/package.json +4 -1
- package/src/commands/output.ts +24 -1
- package/src/compat/child-session.ts +43 -0
- package/src/compat/v2-client.ts +107 -7
- package/src/compat/v2-hooks.ts +1435 -44
- package/src/escalate/attempt-recorder.ts +263 -0
- package/src/escalate/ladder.ts +496 -10
- package/src/escalate/resume.ts +208 -0
- package/src/escalate/variants.ts +391 -0
- package/src/guard/enforce.ts +186 -22
- package/src/guard/guards.ts +132 -6
- package/src/guard/store.ts +5 -0
- package/src/index.ts +1193 -56
- package/src/router/build-info.ts +151 -0
- package/src/router/config.ts +1611 -19
- package/src/router/dispatch-header.ts +46 -4
- package/src/router/git-tools.ts +1152 -0
- package/src/router/guard-profile.ts +100 -0
- package/src/router/plugin-agents.ts +433 -0
- package/src/router/prompts.ts +26 -1
- package/src/router/protocol.ts +180 -0
- package/src/router/read-only.ts +149 -0
- package/src/router/role-agents.ts +409 -0
- package/src/router/roles-config.ts +429 -0
- package/src/router/roles.ts +600 -0
- package/src/router/run-tools.ts +913 -0
- package/src/router/sensitive-paths.ts +101 -0
- package/src/router/sessions.ts +533 -9
- package/src/router/subagents.ts +28 -1
- package/src/routing/advisor/findings.ts +898 -0
- package/src/routing/advisor/index.ts +614 -0
- package/src/routing/classify/backends/host.ts +163 -0
- package/src/routing/classify/backends/openai-compatible.ts +275 -0
- package/src/routing/classify/backends/shared.ts +741 -0
- package/src/routing/classify/backends/typesafe.ts +315 -0
- package/src/routing/classify/fences.ts +96 -0
- package/src/routing/classify/index.ts +594 -0
- package/src/routing/classify/route-line.ts +362 -0
- package/src/routing/classify/rules.ts +667 -0
- package/src/routing/classify/scrub.ts +214 -0
- package/src/routing/classify/state.ts +148 -0
- package/src/routing/classify/text.ts +20 -0
- package/src/routing/classify/types.ts +1310 -0
- package/src/routing/commands/annotate-plan.ts +652 -0
- package/src/routing/commands/stats.ts +134 -0
- package/src/routing/commands/v1-roles.ts +64 -0
- package/src/routing/engine/index.ts +32 -0
- package/src/routing/engine/kernel.ts +760 -0
- package/src/routing/engine/ladders.ts +802 -0
- package/src/routing/engine/plan.ts +404 -0
- package/src/routing/engine/protocol-line.ts +150 -0
- package/src/routing/engine/roles-taxonomy.ts +52 -0
- package/src/routing/engine/simulate.ts +167 -0
- package/src/routing/engine/types.ts +368 -0
- package/src/routing/file-lock.ts +53 -0
- package/src/routing/outcomes/beta.ts +147 -0
- package/src/routing/outcomes/cost.ts +313 -0
- package/src/routing/outcomes/index.ts +215 -0
- package/src/routing/outcomes/ingest.ts +960 -0
- package/src/routing/outcomes/persist.ts +1080 -0
- package/src/routing/outcomes/signals.ts +650 -0
- package/src/routing/outcomes/stats.ts +873 -0
- package/src/routing/outcomes/store.ts +673 -0
- package/src/routing/outcomes/types.ts +1093 -0
- package/src/routing/roles/authority.ts +495 -0
- package/src/routing/roles/binding.ts +543 -0
- package/src/routing/roles/policy.ts +416 -0
- package/src/routing/roles/work-root.ts +132 -0
- package/src/routing/wire/dispatch.ts +2088 -0
- package/src/routing/wire/hint.ts +222 -0
- package/src/routing/wire/host-info.ts +256 -0
- package/src/routing/wire/runtime.ts +348 -0
- package/src/v2.ts +40 -1
- package/src/verify/checker.ts +249 -7
- package/src/verify/deterministic.ts +10 -1
- package/src/verify/dispatch.ts +63 -6
- package/src/verify/dod.ts +127 -10
- package/src/verify/gate.ts +75 -7
- package/src/verify/types.ts +12 -0
- package/src/verify/wiring.ts +55 -10
- package/tiers.json +13 -5
package/src/compat/v2-client.ts
CHANGED
|
@@ -2,10 +2,13 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
2
2
|
import type { Plugin } from "@opencode/plugin";
|
|
3
3
|
import type { ToolContext } from "@opencode/plugin/promise/tool";
|
|
4
4
|
import type { SessionContext } from "@opencode/plugin/promise/session";
|
|
5
|
-
import type
|
|
5
|
+
import { ResumeRejectedError, type ChildSessionRunner } from "./child-session";
|
|
6
|
+
import { markRunnerDispatch, runnerDescription } from "../router/sessions";
|
|
6
7
|
|
|
7
8
|
export const V2_GRADER_AGENT = "model-router-grader";
|
|
8
9
|
const RETAINED_CONTEXT_LIMIT = 500;
|
|
10
|
+
/** How long a delegation waits for the host to remove a child it started by mistake before it reports the error. */
|
|
11
|
+
export const STRAY_CLEANUP_TIMEOUT_MS = 2_000;
|
|
9
12
|
|
|
10
13
|
/** The small v1 client surface still used by the shared hooks. */
|
|
11
14
|
export function createV2Runtime(ctx: Plugin.Context) {
|
|
@@ -45,7 +48,18 @@ export function createV2Runtime(ctx: Plugin.Context) {
|
|
|
45
48
|
name: provider.name,
|
|
46
49
|
models: Object.fromEntries(models.data
|
|
47
50
|
.filter(model => model.providerID === provider.id && model.enabled)
|
|
48
|
-
|
|
51
|
+
// `id` and `status` are what the v1 shape had; the rest lets the cost doctor (Phase 2.4) read the SAME call instead of a second
|
|
52
|
+
// `model.list` (QA-2.4-3). v1 consumers (`normalizeCatalog`) ignore the extra fields.
|
|
53
|
+
.map(model => [model.id, {
|
|
54
|
+
id: model.id,
|
|
55
|
+
status: model.status,
|
|
56
|
+
enabled: model.enabled,
|
|
57
|
+
family: model.family,
|
|
58
|
+
capabilities: model.capabilities,
|
|
59
|
+
cost: model.cost,
|
|
60
|
+
variants: model.variants,
|
|
61
|
+
limit: model.limit,
|
|
62
|
+
}])),
|
|
49
63
|
})),
|
|
50
64
|
default: defaultModel.data
|
|
51
65
|
? { [defaultModel.data.providerID]: defaultModel.data.id }
|
|
@@ -57,6 +71,28 @@ export function createV2Runtime(ctx: Plugin.Context) {
|
|
|
57
71
|
// v2 has no app.log endpoint. createPluginLogger uses its console fallback.
|
|
58
72
|
};
|
|
59
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Stop and remove a child the host started that no caller knows (QA-2.3-5). Bounded (QA-2.3-R2-3): resolves with ""
|
|
76
|
+
* when it is gone, otherwise with a sentence for the error (a failure, or a removal still running after
|
|
77
|
+
* STRAY_CLEANUP_TIMEOUT_MS, which then finishes or fails in the background without anyone waiting for it). Never rejects.
|
|
78
|
+
*/
|
|
79
|
+
const removeStray = async (sessionID: string): Promise<string> => {
|
|
80
|
+
const removal = childRunner.dispose(sessionID).then(
|
|
81
|
+
() => "",
|
|
82
|
+
(error: unknown) => `; removing it failed (${error instanceof Error ? error.message : String(error)})`,
|
|
83
|
+
);
|
|
84
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
85
|
+
const timeout = new Promise<string>((resolve) => {
|
|
86
|
+
timer = setTimeout(() => resolve(`; removing it did not finish within ${STRAY_CLEANUP_TIMEOUT_MS} ms and may still be running`), STRAY_CLEANUP_TIMEOUT_MS);
|
|
87
|
+
if (typeof timer === "object" && typeof timer.unref === "function") timer.unref();
|
|
88
|
+
});
|
|
89
|
+
try {
|
|
90
|
+
return await Promise.race([removal, timeout]);
|
|
91
|
+
} finally {
|
|
92
|
+
clearTimeout(timer);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
60
96
|
const childRunner: ChildSessionRunner = {
|
|
61
97
|
async run(request) {
|
|
62
98
|
lifetime.signal.throwIfAborted();
|
|
@@ -82,14 +118,54 @@ export function createV2Runtime(ctx: Plugin.Context) {
|
|
|
82
118
|
...(request.signal ? [request.signal] : []),
|
|
83
119
|
]);
|
|
84
120
|
signal.throwIfAborted();
|
|
85
|
-
|
|
121
|
+
const resumeID = request.resumeSessionID;
|
|
122
|
+
if (resumeID !== undefined) {
|
|
123
|
+
// The host resumes any session id it is given, so ours checks that the child exists and belongs to the
|
|
124
|
+
// calling session before anything is sent. Whatever the lookup says other than "my child", the caller
|
|
125
|
+
// falls back to a fresh child.
|
|
126
|
+
const parent = scope.context.sessionID;
|
|
127
|
+
let info: { parentID?: unknown } | undefined;
|
|
128
|
+
try {
|
|
129
|
+
info = await ctx.session.get({ sessionID: resumeID }, { signal });
|
|
130
|
+
} catch (error) {
|
|
131
|
+
signal.throwIfAborted();
|
|
132
|
+
throw new ResumeRejectedError(resumeID, `session lookup failed (${error instanceof Error ? error.message : String(error)})`);
|
|
133
|
+
}
|
|
134
|
+
if (info?.parentID !== parent) throw new ResumeRejectedError(resumeID, `it is not a child of session ${parent}`);
|
|
135
|
+
}
|
|
136
|
+
// A resumed child is known up front: the host does not create one, and a progress event for another id is an error.
|
|
137
|
+
let childID: string | undefined = resumeID;
|
|
138
|
+
let confirmed = false;
|
|
139
|
+
const confirmChild = async (sessionID: string): Promise<void> => {
|
|
140
|
+
if (confirmed) return;
|
|
141
|
+
confirmed = true;
|
|
142
|
+
await request.onConfirmed?.(sessionID);
|
|
143
|
+
};
|
|
144
|
+
/** Set when the host answered a resume with another child; thrown from `run` whatever the host made of the progress error. */
|
|
145
|
+
let strayResume: ResumeRejectedError | undefined;
|
|
146
|
+
if (resumeID !== undefined) {
|
|
147
|
+
activeChildren.set(resumeID, controller);
|
|
148
|
+
if (request.system) childSystems.set(resumeID, request.system);
|
|
149
|
+
}
|
|
86
150
|
const model = request.model;
|
|
151
|
+
// 2.2 / 2.3 single writer (QA-2.2-1): announce the native call to the 2.2 dispatch router, which must leave it alone
|
|
152
|
+
// (this runner has chosen the agent and model#variant, writes the attempt's row and registers the child itself). Measured on 2.0.22
|
|
153
|
+
// (Phase 3.2, H4): the host runs no plugin tool hook for a call made through `ctx.tool.list()`, so the router never sees it and the
|
|
154
|
+
// mark is withdrawn below unconsumed; it is defensive, for a host that does hook such calls.
|
|
155
|
+
let withdrawMark: (() => void) | undefined;
|
|
87
156
|
try {
|
|
157
|
+
// A resumed child is registered here, before the host can run it, exactly like a created one is in `progress`.
|
|
158
|
+
if (resumeID !== undefined) {
|
|
159
|
+
await request.onCreated(resumeID);
|
|
160
|
+
signal.throwIfAborted();
|
|
161
|
+
}
|
|
162
|
+
withdrawMark = markRunnerDispatch({ parentSessionID: toolContext.sessionID, agent: request.agent ?? V2_GRADER_AGENT, prompt: request.prompt });
|
|
88
163
|
const result = await native.execute({
|
|
89
164
|
agent: request.agent ?? V2_GRADER_AGENT,
|
|
90
|
-
description: request.agent
|
|
165
|
+
description: runnerDescription(request.agent),
|
|
91
166
|
prompt: request.prompt,
|
|
92
167
|
...(model ? { model: `${model.providerID}/${model.modelID}${model.variant ? `#${model.variant}` : ""}` } : {}),
|
|
168
|
+
...(resumeID !== undefined ? { sessionID: resumeID } : {}),
|
|
93
169
|
background: false,
|
|
94
170
|
}, {
|
|
95
171
|
...toolContext,
|
|
@@ -97,7 +173,22 @@ export function createV2Runtime(ctx: Plugin.Context) {
|
|
|
97
173
|
async progress(metadata) {
|
|
98
174
|
const sessionID = metadata.sessionID;
|
|
99
175
|
if (typeof sessionID === "string" && sessionID !== childID) {
|
|
100
|
-
if (childID)
|
|
176
|
+
if (childID) {
|
|
177
|
+
// The host started another child than the one this call is about (a resume that did not resume, or a
|
|
178
|
+
// second child): no caller knows its id, so it is stopped and removed here before the error, or it
|
|
179
|
+
// would live until the host is restarted (QA-2.3-5). The original child is interrupted below.
|
|
180
|
+
// The removal is bounded (QA-2.3-R2-3): a host that hangs on it must not hang the delegation, and the error
|
|
181
|
+
// says so instead of hiding it.
|
|
182
|
+
const cleanup = await removeStray(sessionID);
|
|
183
|
+
if (resumeID !== undefined && childID === resumeID) {
|
|
184
|
+
// A resume that did not resume: the caller starts a fresh child for the same attempt (D11 fallback)
|
|
185
|
+
// instead of counting a failed attempt. Kept aside so the host's own wrapping of a progress error
|
|
186
|
+
// cannot hide the type.
|
|
187
|
+
strayResume = new ResumeRejectedError(resumeID, `the host started another child (${sessionID}) instead${cleanup}`);
|
|
188
|
+
throw strayResume;
|
|
189
|
+
}
|
|
190
|
+
throw new Error(`[model-router] native subagent changed its child session ID (${childID} -> ${sessionID}${cleanup})`);
|
|
191
|
+
}
|
|
101
192
|
childID = sessionID;
|
|
102
193
|
activeChildren.set(sessionID, controller);
|
|
103
194
|
if (request.system) childSystems.set(sessionID, request.system);
|
|
@@ -107,6 +198,7 @@ export function createV2Runtime(ctx: Plugin.Context) {
|
|
|
107
198
|
if (request.cwd) await ctx.session.move({ sessionID, directory: request.cwd }, { signal });
|
|
108
199
|
signal.throwIfAborted();
|
|
109
200
|
}
|
|
201
|
+
if (typeof sessionID === "string" && sessionID === childID) await confirmChild(sessionID);
|
|
110
202
|
// V2 rejects progress outside a running call. Deferred/queued graders
|
|
111
203
|
// retain settled parent contexts; also tolerate settlement during progress.
|
|
112
204
|
if (scope.active) {
|
|
@@ -115,19 +207,27 @@ export function createV2Runtime(ctx: Plugin.Context) {
|
|
|
115
207
|
}
|
|
116
208
|
},
|
|
117
209
|
});
|
|
118
|
-
signal.throwIfAborted();
|
|
119
210
|
const output = result.output as { sessionID?: unknown; status?: unknown; output?: unknown } | undefined;
|
|
211
|
+
// A named child confirms an attempted resume even when its result failed or was aborted.
|
|
212
|
+
if (childID && output?.sessionID === childID) await confirmChild(childID);
|
|
213
|
+
signal.throwIfAborted();
|
|
214
|
+
if (resumeID !== undefined && typeof output?.sessionID === "string" && output.sessionID !== resumeID) {
|
|
215
|
+
const cleanup = await removeStray(output.sessionID);
|
|
216
|
+
throw new ResumeRejectedError(resumeID, `the host started another child (${output.sessionID}) instead${cleanup}`);
|
|
217
|
+
}
|
|
120
218
|
if (!childID || output?.sessionID !== childID || output.status !== "completed" || typeof output.output !== "string") {
|
|
121
219
|
throw new Error("[model-router] native subagent did not return a completed child result; verification cannot use a pending result");
|
|
122
220
|
}
|
|
221
|
+
await confirmChild(childID);
|
|
123
222
|
return { sessionID: childID, text: output.output };
|
|
124
223
|
} catch (error) {
|
|
125
224
|
controller.abort();
|
|
126
225
|
if (childID) {
|
|
127
226
|
try { await ctx.session.interrupt({ sessionID: childID }); } catch { /* keep the original dispatch failure */ }
|
|
128
227
|
}
|
|
129
|
-
throw error;
|
|
228
|
+
throw strayResume ?? error;
|
|
130
229
|
} finally {
|
|
230
|
+
withdrawMark?.(); // a hook that never fired must not leave a mark behind
|
|
131
231
|
if (childID) {
|
|
132
232
|
childSystems.delete(childID);
|
|
133
233
|
activeChildren.delete(childID);
|