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
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 2.3: what the delegate runner tells the telemetry about each ladder attempt.
|
|
3
|
+
*
|
|
4
|
+
* Every attempt of a `delegate` ladder (the first dispatch, a variant step, a plain retry, an escalation) is
|
|
5
|
+
* its own attempt in the 2.1 dispatch-facts registry, under the attempt's step label, so that
|
|
6
|
+
*
|
|
7
|
+
* - `session.step.ended` events, verdicts and false refusals land on the right `(class x agent x model#variant)`
|
|
8
|
+
* key (a resumed child that moved to a higher variant is a different key from the attempt before it);
|
|
9
|
+
* - the variant-refusal rule (QA-2.1-R2-3: a refusal that is a variant attempt's first terminal signal is a
|
|
10
|
+
* variant failure) and the ladder's `triedByModel` (A17a) name the same rungs;
|
|
11
|
+
* - the delegate ladder's last-step context (`lastStepContext`, D11) is tracked for the child.
|
|
12
|
+
*
|
|
13
|
+
* and, when `routing.engine` is not `static`, enqueues one `DecisionRow` per attempt (1.3 handoff to 2.3):
|
|
14
|
+
* `step` in `dispatch | variant | retry | escalate`, `resume` per D11/A5, `chosen` = the ladder's pick,
|
|
15
|
+
* `best` null, `switched` false. Rows go through the process-wide outcomes bundle (A3), never on a hot path:
|
|
16
|
+
* `enqueue` is an O(1) push and the flusher writes at idle/deleted or every 30 s (D15).
|
|
17
|
+
*
|
|
18
|
+
* Everything here is best effort: a failure is logged and never reaches the delegation.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { randomBytes } from "node:crypto";
|
|
22
|
+
import type { RouterConfig, RouterHost } from "../router/config";
|
|
23
|
+
import { resolveRouting } from "../router/config";
|
|
24
|
+
import { rememberDispatch, type DetectionDepth } from "../router/sessions";
|
|
25
|
+
import { classify, type TaskFacts } from "../routing/classify";
|
|
26
|
+
import { acquireOutcomes } from "../routing/outcomes";
|
|
27
|
+
import { ingestSettings, type IngestSettings } from "../routing/outcomes/ingest";
|
|
28
|
+
import type { AcquireOutcomesOptions } from "../routing/outcomes";
|
|
29
|
+
import {
|
|
30
|
+
LOG_ROW_VERSION,
|
|
31
|
+
classifyAgentOrigin,
|
|
32
|
+
makeKey,
|
|
33
|
+
normalizeVariant,
|
|
34
|
+
safeNow,
|
|
35
|
+
type Clock,
|
|
36
|
+
type DecisionFacts,
|
|
37
|
+
type DecisionRow,
|
|
38
|
+
type OutcomeLogger,
|
|
39
|
+
type OutcomesBundle,
|
|
40
|
+
} from "../routing/outcomes/types";
|
|
41
|
+
import type { AttemptPlan } from "./resume";
|
|
42
|
+
|
|
43
|
+
/** What one ladder attempt registers. */
|
|
44
|
+
export interface AttemptRecord {
|
|
45
|
+
/** The child the attempt runs on (created, or the resumed one). */
|
|
46
|
+
readonly childSessionID: string;
|
|
47
|
+
/** The orchestrator session that called `delegate`. */
|
|
48
|
+
readonly parentSessionID: string | null;
|
|
49
|
+
readonly plan: AttemptPlan;
|
|
50
|
+
/** The configuration this delegation runs under (the runner's hot-reloaded `activeCfg`); default: the recorder's own. */
|
|
51
|
+
readonly config?: RouterConfig;
|
|
52
|
+
/** Typed facts of the delegated task (rules classifier). */
|
|
53
|
+
readonly facts: DecisionFacts;
|
|
54
|
+
/** The delegation's verification depth, from its `[acceptance]` block. */
|
|
55
|
+
readonly acceptance: DetectionDepth | null;
|
|
56
|
+
/** True when the attempt runs on a child that already existed. */
|
|
57
|
+
readonly resumed: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface AttemptRecorder {
|
|
61
|
+
/** True when attempts are worth registering for the engine alone (any mode but `static`, on v2). */
|
|
62
|
+
engineLive(config?: RouterConfig): boolean;
|
|
63
|
+
/** Register now; optionally defer the row until the returned idempotent confirmation is called. Never throws. */
|
|
64
|
+
record(attempt: AttemptRecord, deferRow?: boolean): () => void;
|
|
65
|
+
/** Opportunistic maintenance: releases the outcomes bundle when the engine went back to `static`. */
|
|
66
|
+
sweep(): void;
|
|
67
|
+
/** Releases this recorder's reference to the outcomes bundle. Never rejects. */
|
|
68
|
+
dispose(): Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface AttemptRecorderDeps {
|
|
72
|
+
readonly host: RouterHost;
|
|
73
|
+
/** The live configuration (hot reload swaps the object). */
|
|
74
|
+
readonly config: () => RouterConfig;
|
|
75
|
+
readonly logger: OutcomeLogger;
|
|
76
|
+
readonly now?: Clock;
|
|
77
|
+
/** Seam over `acquireOutcomes` (tests). */
|
|
78
|
+
readonly acquire?: (options: AcquireOutcomesOptions) => OutcomesBundle;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const NONCE = randomBytes(4).toString("hex");
|
|
82
|
+
let decisionSeq = 0;
|
|
83
|
+
|
|
84
|
+
function describe(error: unknown): string {
|
|
85
|
+
return error instanceof Error ? error.message : String(error);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** `provider/model` of a plan, or null when the tier's model did not resolve. */
|
|
89
|
+
function modelOf(plan: AttemptPlan): string | null {
|
|
90
|
+
return plan.model === undefined ? null : `${plan.model.providerID}/${plan.model.modelID}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The decision row's `reason`: the ladder's step, the D11 verdict with both numbers, and any runner override. */
|
|
94
|
+
export function describeAttempt(attempt: AttemptRecord): string {
|
|
95
|
+
const { plan } = attempt;
|
|
96
|
+
const parts = [`ladder ${plan.step} on ${plan.tier}`, attempt.resumed ? "resumed child" : "fresh child"];
|
|
97
|
+
const basis = plan.resumeBasis;
|
|
98
|
+
if (basis !== undefined) {
|
|
99
|
+
const numbers = [
|
|
100
|
+
basis.tokens === null ? null : `tokens=${basis.tokens}`,
|
|
101
|
+
basis.budget === null ? null : `budget=${basis.budget}`,
|
|
102
|
+
basis.threshold === null ? null : `threshold=${basis.threshold}`,
|
|
103
|
+
].filter((part): part is string => part !== null);
|
|
104
|
+
parts.push(`D11 ${basis.reason}${numbers.length === 0 ? "" : ` (${numbers.join(" ")})`}`);
|
|
105
|
+
}
|
|
106
|
+
if (plan.fresh !== undefined) parts.push(`runner: fresh (${plan.fresh})`);
|
|
107
|
+
if (plan.costRatio !== undefined) parts.push(`rung costRatio ${plan.costRatio}`);
|
|
108
|
+
return parts.join("; ");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function createAttemptRecorder(deps: AttemptRecorderDeps): AttemptRecorder {
|
|
112
|
+
const now: Clock = deps.now ?? Date.now;
|
|
113
|
+
const acquire = deps.acquire ?? acquireOutcomes;
|
|
114
|
+
let held: { readonly dir: string; readonly bundle: OutcomesBundle } | null = null;
|
|
115
|
+
let disposed = false;
|
|
116
|
+
|
|
117
|
+
const settingsNow = (config?: RouterConfig): IngestSettings | null => ingestSettings(config ?? deps.config(), deps.host);
|
|
118
|
+
|
|
119
|
+
const release = (): void => {
|
|
120
|
+
const current = held;
|
|
121
|
+
held = null;
|
|
122
|
+
if (current !== null) void current.bundle.release().catch((error: unknown) => deps.logger.warn("[router] ladder attempts: releasing the outcomes bundle failed", { error: describe(error) }));
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const bundleFor = (settings: IngestSettings): OutcomesBundle | null => {
|
|
126
|
+
if (disposed) return null;
|
|
127
|
+
if (held !== null && held.dir === settings.outcomesDir) return held.bundle;
|
|
128
|
+
release();
|
|
129
|
+
const bundle = acquire({ dir: settings.outcomesDir, tuning: settings.tuning, logger: deps.logger });
|
|
130
|
+
held = { dir: settings.outcomesDir, bundle };
|
|
131
|
+
return bundle;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
engineLive: (config) => {
|
|
136
|
+
try {
|
|
137
|
+
return settingsNow(config) !== null;
|
|
138
|
+
} catch (error) {
|
|
139
|
+
deps.logger.warn("[router] ladder attempts: reading the routing settings failed", { error: describe(error) });
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
record(attempt, deferRow = false) {
|
|
145
|
+
try {
|
|
146
|
+
const { plan } = attempt;
|
|
147
|
+
const settings = settingsNow(attempt.config);
|
|
148
|
+
const model = modelOf(plan);
|
|
149
|
+
const decisionID = settings === null || model === null ? null : `ladder-${NONCE}-${++decisionSeq}`;
|
|
150
|
+
rememberDispatch(attempt.childSessionID, {
|
|
151
|
+
facts: attempt.facts,
|
|
152
|
+
agent: plan.agent,
|
|
153
|
+
model,
|
|
154
|
+
variant: plan.model?.variant ?? null,
|
|
155
|
+
tier: plan.tier,
|
|
156
|
+
acceptance: attempt.acceptance,
|
|
157
|
+
parentSessionID: attempt.parentSessionID,
|
|
158
|
+
decisionID,
|
|
159
|
+
step: plan.step,
|
|
160
|
+
// Registered with ingestion off (engine static): no instance may score it (QA-2.3-6).
|
|
161
|
+
outcomes: settings !== null,
|
|
162
|
+
}, safeNow(now));
|
|
163
|
+
if (settings === null || decisionID === null || plan.model === undefined) return () => undefined;
|
|
164
|
+
const bundle = bundleFor(settings);
|
|
165
|
+
if (bundle === null) return () => undefined;
|
|
166
|
+
const variant = normalizeVariant(plan.model.variant);
|
|
167
|
+
const origin = classifyAgentOrigin(plan.agent, settings.routerAgentIds);
|
|
168
|
+
const key = makeKey(attempt.facts.class, { origin, id: plan.agent }, plan.model.providerID, plan.model.modelID, variant);
|
|
169
|
+
const chosen = { key, agent: plan.agent, origin, model: `${plan.model.providerID}/${plan.model.modelID}`, variant };
|
|
170
|
+
const row: DecisionRow = {
|
|
171
|
+
v: LOG_ROW_VERSION,
|
|
172
|
+
ts: new Date(safeNow(now)).toISOString(),
|
|
173
|
+
sessionID: attempt.parentSessionID ?? "",
|
|
174
|
+
kind: "decision",
|
|
175
|
+
decisionID,
|
|
176
|
+
mode: settings.engine,
|
|
177
|
+
childSessionID: attempt.childSessionID,
|
|
178
|
+
facts: attempt.facts,
|
|
179
|
+
chosen,
|
|
180
|
+
best: null,
|
|
181
|
+
switched: false,
|
|
182
|
+
pinned: false,
|
|
183
|
+
unit: "ratio",
|
|
184
|
+
costs: {},
|
|
185
|
+
confidence: attempt.facts.confidence,
|
|
186
|
+
reason: describeAttempt(attempt),
|
|
187
|
+
step: plan.step,
|
|
188
|
+
resume: attempt.resumed,
|
|
189
|
+
};
|
|
190
|
+
let confirmed = false;
|
|
191
|
+
const confirm = (): void => {
|
|
192
|
+
if (confirmed) return;
|
|
193
|
+
confirmed = true;
|
|
194
|
+
try {
|
|
195
|
+
bundle.flusher.enqueue(row);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
deps.logger.warn("[router] ladder attempts: enqueueing an attempt failed", { error: describe(error), child: attempt.childSessionID });
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
if (!deferRow) confirm();
|
|
201
|
+
return confirm;
|
|
202
|
+
} catch (error) {
|
|
203
|
+
deps.logger.warn("[router] ladder attempts: registering an attempt failed", { error: describe(error), child: attempt.childSessionID });
|
|
204
|
+
return () => undefined;
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
sweep() {
|
|
209
|
+
try {
|
|
210
|
+
if (held !== null && settingsNow() === null) release();
|
|
211
|
+
} catch (error) {
|
|
212
|
+
deps.logger.warn("[router] ladder attempts: sweep failed", { error: describe(error) });
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
|
|
216
|
+
async dispose() {
|
|
217
|
+
disposed = true;
|
|
218
|
+
const current = held;
|
|
219
|
+
held = null;
|
|
220
|
+
if (current === null) return;
|
|
221
|
+
try {
|
|
222
|
+
await current.bundle.release();
|
|
223
|
+
} catch (error) {
|
|
224
|
+
deps.logger.warn("[router] ladder attempts: releasing the outcomes bundle failed", { error: describe(error) });
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** What the rules classifier says about a delegated task, for the registry and the decision rows. */
|
|
231
|
+
export interface DelegationFacts {
|
|
232
|
+
readonly facts: TaskFacts;
|
|
233
|
+
readonly acceptance: DetectionDepth | null;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Rules-only facts of a delegation (D3: no backend call, nothing leaves the machine, never throws). The
|
|
238
|
+
* delegate tool has no orchestrator-chosen class, so the class comes from the same rules `/annotate-plan` and
|
|
239
|
+
* the dispatch hook start from; a class below `routing.minClassConfidence` is simply not recorded by the ingest.
|
|
240
|
+
*/
|
|
241
|
+
export async function classifyDelegation(
|
|
242
|
+
cfg: RouterConfig,
|
|
243
|
+
host: RouterHost,
|
|
244
|
+
task: string,
|
|
245
|
+
acceptance: string | undefined,
|
|
246
|
+
logger: OutcomeLogger,
|
|
247
|
+
): Promise<DelegationFacts | null> {
|
|
248
|
+
try {
|
|
249
|
+
const routing = resolveRouting(cfg, host);
|
|
250
|
+
const prompt = acceptance === undefined || acceptance.trim() === "" ? task : `${task}\n\n${acceptance}`;
|
|
251
|
+
const result = await classify({ prompt }, {
|
|
252
|
+
cfg,
|
|
253
|
+
settings: { ...routing.classifier, backend: "rules" },
|
|
254
|
+
minClassConfidence: routing.minClassConfidence,
|
|
255
|
+
backend: null,
|
|
256
|
+
logger: { warn: (message: string) => logger.warn(message) },
|
|
257
|
+
});
|
|
258
|
+
return { facts: result.facts, acceptance: result.detection };
|
|
259
|
+
} catch (error) {
|
|
260
|
+
logger.warn("[router] ladder attempts: classifying the delegation failed", { error: describe(error) });
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
}
|