opencode-model-router 2.0.0 → 2.1.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 +68 -0
- package/README.md +70 -4
- package/package.json +2 -2
- package/src/compat/child-session.ts +5 -0
- package/src/compat/v2-hooks.ts +47 -14
- package/src/escalate/effort-override.ts +165 -0
- package/src/escalate/ladder.ts +57 -7
- package/src/index.ts +180 -14
- package/src/router/agent-options.ts +43 -12
- package/src/router/config.ts +131 -20
- package/src/router/depth-guard.ts +152 -0
- package/src/router/depth.ts +826 -0
- package/src/v2.ts +1 -0
- package/src/verify/wiring.ts +16 -2
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Plugin } from "@opencode-ai/plugin";
|
|
2
2
|
import type { RouterPluginInput } from "./compat/child-session";
|
|
3
|
-
import { TASK_VERIFICATION } from "./compat/child-session";
|
|
3
|
+
import { DEPTH_BANNER, TASK_VERIFICATION } from "./compat/child-session";
|
|
4
4
|
|
|
5
5
|
// Imports for internal use within this module
|
|
6
6
|
import {
|
|
@@ -12,9 +12,10 @@ import {
|
|
|
12
12
|
localOverridePath,
|
|
13
13
|
findProjectOverride,
|
|
14
14
|
resolveVerifyBudget,
|
|
15
|
+
resolveDepthLimit,
|
|
15
16
|
warnDeprecatedVerifyKeys,
|
|
16
17
|
} from "./router/config";
|
|
17
|
-
import type { RouterConfig, TierConfig, Preset, ModeConfig } from "./router/config";
|
|
18
|
+
import type { RouterConfig, TierConfig, Preset, ModeConfig, EffortLevel } from "./router/config";
|
|
18
19
|
import { buildAgentOptions, warnAgentOptionsEffortOnce } from "./router/agent-options";
|
|
19
20
|
import { selectTierPrompt, TOOL_AUTHORITY_CLAUSE } from "./router/prompts";
|
|
20
21
|
import { stripDelegateInstructions } from "./router/instructions";
|
|
@@ -99,6 +100,9 @@ import {
|
|
|
99
100
|
buildAcceptedSuffix,
|
|
100
101
|
} from "./verify/dispatch";
|
|
101
102
|
import { newLadderState, recordAttempt, nextAction, advance, buildEscalatePolicy, formatLadderScorecard } from "./escalate/ladder";
|
|
103
|
+
import { createDepthTracker, DEPTH_LOOKUP_RETRY_MS } from "./router/depth";
|
|
104
|
+
import { createDepthGuard } from "./router/depth-guard";
|
|
105
|
+
import { applyEffortOverride, createEffortOverrideStore } from "./escalate/effort-override";
|
|
102
106
|
|
|
103
107
|
// ---------------------------------------------------------------------------
|
|
104
108
|
// Re-exports — type-only re-exports for IDE/test consumers.
|
|
@@ -114,6 +118,15 @@ export type { TrajectoryState, TrajectoryToolEvent } from "./telemetry/trajector
|
|
|
114
118
|
export type { EnforcementMode } from "./router/enforcement";
|
|
115
119
|
export type { GuardPolicy, GuardState, GuardCall, GuardDecision } from "./guard/guards";
|
|
116
120
|
|
|
121
|
+
/** Diagnostics must also tolerate thrown values without primitive conversion. */
|
|
122
|
+
function describeError(error: unknown): string {
|
|
123
|
+
try {
|
|
124
|
+
return scrubText(String(error));
|
|
125
|
+
} catch {
|
|
126
|
+
return "unprintable error";
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
117
130
|
function saveActivePreset(presetName: string): void {
|
|
118
131
|
const cfg = loadConfig();
|
|
119
132
|
const resolved = resolvePresetName(cfg, presetName);
|
|
@@ -238,7 +251,7 @@ function warnSessionLookupFailedOnce(): void {
|
|
|
238
251
|
}
|
|
239
252
|
|
|
240
253
|
const SESSION_ROOT_MEMO_MAX = 500;
|
|
241
|
-
const SESSION_LOOKUP_RETRY_MS =
|
|
254
|
+
const SESSION_LOOKUP_RETRY_MS = DEPTH_LOOKUP_RETRY_MS;
|
|
242
255
|
|
|
243
256
|
const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
244
257
|
let cfg = loadConfig();
|
|
@@ -290,6 +303,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
290
303
|
const memo = sessionRootMemo.get(sessionID);
|
|
291
304
|
if (memo !== undefined) {
|
|
292
305
|
if (!memo) sessionStore.markChildSession(sessionID);
|
|
306
|
+
else depthTracker.recordRoot(sessionID);
|
|
293
307
|
return memo;
|
|
294
308
|
}
|
|
295
309
|
const failedAt = sessionLookupFailedAt.get(sessionID);
|
|
@@ -311,6 +325,8 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
311
325
|
sessionRootMemo.delete(oldest);
|
|
312
326
|
}
|
|
313
327
|
if (!isRoot) sessionStore.markChildSession(sessionID);
|
|
328
|
+
if (isRoot) depthTracker.recordRoot(sessionID);
|
|
329
|
+
else depthTracker.recordCreated(sessionID, parentID as string);
|
|
314
330
|
return isRoot;
|
|
315
331
|
} catch {
|
|
316
332
|
sessionLookupFailedAt.set(sessionID, Date.now());
|
|
@@ -346,6 +362,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
346
362
|
() => guardStore.sweep(),
|
|
347
363
|
() => trajectoryStore.sweep(),
|
|
348
364
|
() => changedFileStore.sweep(),
|
|
365
|
+
() => depthTracker.sweep(),
|
|
349
366
|
// 2.2.3: the S5 batch coordinator's defensive eviction (batch.ts B11). Declared below; the
|
|
350
367
|
// sweeper only runs from chat.message, long after this factory has returned.
|
|
351
368
|
() => { sweepVerification(); },
|
|
@@ -360,6 +377,60 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
360
377
|
// from a plugin paints over the TUI. Falls back to console when the server
|
|
361
378
|
// has no /log endpoint. See src/router/logger.ts.
|
|
362
379
|
const logger = createPluginLogger(ctx.client);
|
|
380
|
+
const routerWarn = { warn: (message: string) => logger.warn(message) };
|
|
381
|
+
const depthTracker = createDepthTracker({
|
|
382
|
+
async getParent(id) {
|
|
383
|
+
if (sessionRootMemo.get(id) === true) return null;
|
|
384
|
+
const res = await ctx.client.session.get({ path: { id } });
|
|
385
|
+
if (!res || (res as { error?: unknown }).error || !res.data) {
|
|
386
|
+
throw new Error("session.get returned no session data");
|
|
387
|
+
}
|
|
388
|
+
const parentID = res.data.parentID;
|
|
389
|
+
return typeof parentID === "string" && parentID !== "" ? parentID : null;
|
|
390
|
+
},
|
|
391
|
+
now: () => Date.now(),
|
|
392
|
+
logger: routerWarn,
|
|
393
|
+
});
|
|
394
|
+
const depthGuard = createDepthGuard({
|
|
395
|
+
tracker: depthTracker,
|
|
396
|
+
limit: () => resolveDepthLimit(cfg),
|
|
397
|
+
mode: (sid) => resolveEnforcementMode({
|
|
398
|
+
config: cfg,
|
|
399
|
+
tier: (typeof sid === "string" ? sessionStore.getTier(sid) : null) ?? undefined,
|
|
400
|
+
env: process.env,
|
|
401
|
+
}).mode,
|
|
402
|
+
logger: routerWarn,
|
|
403
|
+
});
|
|
404
|
+
const effortOverrides = createEffortOverrideStore({ logger: routerWarn });
|
|
405
|
+
let warnedGraderParams = false;
|
|
406
|
+
const depthBanners = new Map<string, string>();
|
|
407
|
+
const warnedNoCallID = new Set<string>();
|
|
408
|
+
const stashDepthBanner = (
|
|
409
|
+
input: { sessionID?: string; callID?: string },
|
|
410
|
+
output: Record<PropertyKey, unknown>,
|
|
411
|
+
banner: string,
|
|
412
|
+
): void => {
|
|
413
|
+
try {
|
|
414
|
+
if (ctx.routerHost === "v2") {
|
|
415
|
+
output[DEPTH_BANNER] = banner;
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
if (typeof input.callID === "string" && input.callID !== "") {
|
|
419
|
+
const key = `${input.sessionID}:${input.callID}`;
|
|
420
|
+
depthBanners.delete(key);
|
|
421
|
+
depthBanners.set(key, banner);
|
|
422
|
+
while (depthBanners.size > 1000) depthBanners.delete(depthBanners.keys().next().value!);
|
|
423
|
+
} else {
|
|
424
|
+
const sid = String(input.sessionID);
|
|
425
|
+
if (warnedNoCallID.has(sid)) return;
|
|
426
|
+
logger.warn(`[router] delegation depth: task call without callID in session ${sid}; advisory banner not delivered`);
|
|
427
|
+
warnedNoCallID.add(sid);
|
|
428
|
+
while (warnedNoCallID.size > 1000) warnedNoCallID.delete(warnedNoCallID.values().next().value!);
|
|
429
|
+
}
|
|
430
|
+
} catch (error) {
|
|
431
|
+
logger.warn("[router] delegation depth: advisory banner not stored", { error: describeError(error) });
|
|
432
|
+
}
|
|
433
|
+
};
|
|
363
434
|
|
|
364
435
|
const {
|
|
365
436
|
graderSessions, dispatchGrader, buildGateDeps, disposeChildSession,
|
|
@@ -372,6 +443,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
372
443
|
} = createVerificationWiring({
|
|
373
444
|
client: ctx.client,
|
|
374
445
|
childRunner: ctx.routerChildRunner,
|
|
446
|
+
onChildSessionCreated: (sid, creator) => depthTracker.recordPluginChild(sid, creator),
|
|
375
447
|
directory: ctx.directory,
|
|
376
448
|
getConfig: () => cfg,
|
|
377
449
|
logger,
|
|
@@ -545,6 +617,24 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
545
617
|
* settled; clearing it in the finally below would abort a capture still in flight.
|
|
546
618
|
*/
|
|
547
619
|
let deferredOwnsBaseline = false;
|
|
620
|
+
const depth = bypassed ? undefined : await depthGuard.checkDispatch(toolCtx?.sessionID);
|
|
621
|
+
if (depth?.block) {
|
|
622
|
+
try {
|
|
623
|
+
// Known subagents are counted by the normal after-hook: delegate
|
|
624
|
+
// returns a refusal rather than throwing (unlike native task).
|
|
625
|
+
if (typeof toolCtx?.sessionID === "string" && !sessionStore.isSubagent(toolCtx.sessionID)) {
|
|
626
|
+
trajectoryStore.recordToolEvent(toolCtx.sessionID, { tool: "delegate", readOnly: false, blocked: true });
|
|
627
|
+
}
|
|
628
|
+
} catch {
|
|
629
|
+
// Best-effort observation must never lose the depth refusal.
|
|
630
|
+
}
|
|
631
|
+
return depth.message!;
|
|
632
|
+
}
|
|
633
|
+
const withDepthBanner = (text: string): string => {
|
|
634
|
+
if (!depth?.banner) return text;
|
|
635
|
+
const trimmed = text.trimEnd();
|
|
636
|
+
return trimmed ? `${trimmed}\n\n${depth.banner}` : depth.banner;
|
|
637
|
+
};
|
|
548
638
|
try {
|
|
549
639
|
let activeCfg = cfg;
|
|
550
640
|
try {
|
|
@@ -577,6 +667,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
577
667
|
|
|
578
668
|
let producerText = "";
|
|
579
669
|
let forcing: string | null = null;
|
|
670
|
+
let effort: EffortLevel | undefined;
|
|
580
671
|
|
|
581
672
|
/**
|
|
582
673
|
* One turn of the escalation ladder: create a producer session, run
|
|
@@ -591,6 +682,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
591
682
|
const runProducerAttempt = async (
|
|
592
683
|
tier: string,
|
|
593
684
|
forcingNote: string | null,
|
|
685
|
+
effort?: EffortLevel,
|
|
594
686
|
): Promise<{
|
|
595
687
|
sessionID: string;
|
|
596
688
|
text: string;
|
|
@@ -609,6 +701,11 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
609
701
|
const registerProducer = async (sid: string) => {
|
|
610
702
|
producerSid = sid;
|
|
611
703
|
producerSessions.push(sid);
|
|
704
|
+
depthTracker.recordPluginChild(sid, toolCtx?.sessionID ?? null);
|
|
705
|
+
if (effort !== undefined) {
|
|
706
|
+
const tierCfg = getActiveTiers(activeCfg)[tier];
|
|
707
|
+
if (tierCfg) effortOverrides.set(sid, tier, tierCfg, effort);
|
|
708
|
+
}
|
|
612
709
|
// Keep the ORIGINAL dispatch reference across retries/escalations:
|
|
613
710
|
// recapturing after a failed attempt would excuse its regression.
|
|
614
711
|
if (!baselineID) {
|
|
@@ -717,6 +814,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
717
814
|
} catch {
|
|
718
815
|
// non-fatal
|
|
719
816
|
}
|
|
817
|
+
effortOverrides.clear(producerSid);
|
|
720
818
|
await disposeChildSession(producerSid);
|
|
721
819
|
return { sessionID: producerSid, text: producerText, deferredFooter: finish.footer };
|
|
722
820
|
}
|
|
@@ -845,6 +943,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
845
943
|
}
|
|
846
944
|
// Dispose this attempt's backend session before the next iteration
|
|
847
945
|
// so a long ladder never accumulates live sessions.
|
|
946
|
+
effortOverrides.clear(producerSid);
|
|
848
947
|
await disposeChildSession(producerSid);
|
|
849
948
|
|
|
850
949
|
return { sessionID: producerSid, text: producerText, gateRes };
|
|
@@ -852,21 +951,21 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
852
951
|
|
|
853
952
|
while (true) {
|
|
854
953
|
if (safety++ > safetyMax) {
|
|
855
|
-
return (
|
|
954
|
+
return withDepthBanner(
|
|
856
955
|
`[router status: unmet] delegation stopped by the safety net after ` +
|
|
857
956
|
`${state.totalAttempts} attempt(s).\n\n${scrubText(producerText)}`
|
|
858
957
|
);
|
|
859
958
|
}
|
|
860
959
|
const tier = state.currentTier;
|
|
861
|
-
const attempt = await runProducerAttempt(tier, forcing);
|
|
960
|
+
const attempt = await runProducerAttempt(tier, forcing, effort);
|
|
862
961
|
if (!attempt) {
|
|
863
|
-
return "[router] delegate failed: could not create a producer session.";
|
|
962
|
+
return withDepthBanner("[router] delegate failed: could not create a producer session.");
|
|
864
963
|
}
|
|
865
964
|
producerText = attempt.text;
|
|
866
965
|
if ("deferredFooter" in attempt) {
|
|
867
966
|
// Section 1.5-16: the result unchanged plus the footer, appended last. Never
|
|
868
967
|
// labelled accepted or verified, and never retried or escalated.
|
|
869
|
-
return appendRouterFooter(producerText, attempt.deferredFooter);
|
|
968
|
+
return appendRouterFooter(withDepthBanner(producerText), attempt.deferredFooter);
|
|
870
969
|
}
|
|
871
970
|
const producerSid = attempt.sessionID;
|
|
872
971
|
const gateRes = attempt.gateRes;
|
|
@@ -890,7 +989,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
890
989
|
true,
|
|
891
990
|
gateRes.verdict.method,
|
|
892
991
|
);
|
|
893
|
-
return producerText + buildAcceptedSuffix(gateRes.verdict.method, gateRes.verdict.outcome, gateRes.verdict.caveats, gateRes.verdict.notes);
|
|
992
|
+
return withDepthBanner(producerText) + buildAcceptedSuffix(gateRes.verdict.method, gateRes.verdict.outcome, gateRes.verdict.caveats, gateRes.verdict.notes);
|
|
894
993
|
}
|
|
895
994
|
if (action.action === "give_up") {
|
|
896
995
|
dumpDelegateScorecard(
|
|
@@ -900,7 +999,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
900
999
|
gateRes.verdict.method,
|
|
901
1000
|
);
|
|
902
1001
|
const note = scrubText(buildForcingNote(gateRes.verdict.reasons));
|
|
903
|
-
return (
|
|
1002
|
+
return withDepthBanner(
|
|
904
1003
|
`[router status: unmet] The delegated result was not accepted after ` +
|
|
905
1004
|
`${state.totalAttempts} attempt(s) across ${state.escalations} escalation(s) ` +
|
|
906
1005
|
`(final tier ${state.currentTier}; ${action.reason ?? "verification failed"}).\n\n` +
|
|
@@ -909,10 +1008,11 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
909
1008
|
}
|
|
910
1009
|
// retry or escalate
|
|
911
1010
|
forcing = action.forcingMessage ?? null;
|
|
1011
|
+
effort = action.effort;
|
|
912
1012
|
state = advance(state, action);
|
|
913
1013
|
}
|
|
914
1014
|
} catch {
|
|
915
|
-
return "[router] delegate failed (fail-closed): the delegation or verification could not complete.";
|
|
1015
|
+
return withDepthBanner("[router] delegate failed (fail-closed): the delegation or verification could not complete.");
|
|
916
1016
|
} finally {
|
|
917
1017
|
// Safety net for every exit path an end-of-iteration dispose cannot
|
|
918
1018
|
// reach: accept/give-up returns, the safety-net return, and throws.
|
|
@@ -920,6 +1020,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
920
1020
|
// disposed session is harmless.
|
|
921
1021
|
for (const sid of producerSessions) {
|
|
922
1022
|
if (!(deferredOwnsBaseline && sid === baselineID)) changedFileStore.clear(sid);
|
|
1023
|
+
effortOverrides.clear(sid);
|
|
923
1024
|
await disposeChildSession(sid);
|
|
924
1025
|
}
|
|
925
1026
|
}
|
|
@@ -987,8 +1088,26 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
987
1088
|
output.temperature = graderTemperature;
|
|
988
1089
|
}
|
|
989
1090
|
}
|
|
990
|
-
} catch {
|
|
991
|
-
|
|
1091
|
+
} catch (error) {
|
|
1092
|
+
if (!warnedGraderParams) {
|
|
1093
|
+
warnedGraderParams = true;
|
|
1094
|
+
try {
|
|
1095
|
+
logger.warn("[verify] grader temperature not applied", { error: describeError(error) });
|
|
1096
|
+
} catch {
|
|
1097
|
+
// Even a failed diagnostic sink must not interrupt chat.params.
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
try {
|
|
1102
|
+
if (input && typeof input === "object") {
|
|
1103
|
+
applyEffortOverride(effortOverrides, input, ctx.routerHost === "v2" ? output : output?.options, routerWarn);
|
|
1104
|
+
}
|
|
1105
|
+
} catch (error) {
|
|
1106
|
+
try {
|
|
1107
|
+
logger.warn("[router] effort override not applied", { error: describeError(error) });
|
|
1108
|
+
} catch {
|
|
1109
|
+
// Host accessors and diagnostic sinks are both best-effort here.
|
|
1110
|
+
}
|
|
992
1111
|
}
|
|
993
1112
|
},
|
|
994
1113
|
|
|
@@ -1098,6 +1217,22 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
1098
1217
|
// -----------------------------------------------------------------------
|
|
1099
1218
|
"tool.execute.before": async (input: any, output: any) => {
|
|
1100
1219
|
if (bypassed) return;
|
|
1220
|
+
if (input?.tool === "task") {
|
|
1221
|
+
const depth = await depthGuard.checkDispatch(input.sessionID);
|
|
1222
|
+
if (depth.block) {
|
|
1223
|
+
try {
|
|
1224
|
+
if (typeof input.sessionID === "string") {
|
|
1225
|
+
trajectoryStore.recordToolEvent(input.sessionID, {
|
|
1226
|
+
tool: input.tool, readOnly: READ_ONLY_TOOLS.has(input.tool), blocked: true,
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1229
|
+
} catch (error) {
|
|
1230
|
+
logger.warn("[router] delegation depth: refusal not recorded", { error: describeError(error) });
|
|
1231
|
+
}
|
|
1232
|
+
throw new Error(depth.message);
|
|
1233
|
+
}
|
|
1234
|
+
if (depth.banner) stashDepthBanner(input, output, depth.banner);
|
|
1235
|
+
}
|
|
1101
1236
|
// Observe before execution too: an in-flight edit must contaminate a
|
|
1102
1237
|
// capture even if the test finishes before the edit's after-hook fires.
|
|
1103
1238
|
if (typeof input?.tool === "string") changedFileStore.observeEdit(input.tool,
|
|
@@ -1259,6 +1394,22 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
1259
1394
|
changedFileStore.record(sid, input.tool, input?.args);
|
|
1260
1395
|
}
|
|
1261
1396
|
|
|
1397
|
+
let unbannered: { output: unknown } | undefined;
|
|
1398
|
+
if (input?.tool === "task") {
|
|
1399
|
+
const key = `${sid}:${input.callID}`;
|
|
1400
|
+
const banner = depthBanners.get(key);
|
|
1401
|
+
depthBanners.delete(key);
|
|
1402
|
+
if (banner !== undefined) {
|
|
1403
|
+
try {
|
|
1404
|
+
unbannered = { output: output.output };
|
|
1405
|
+
const text = typeof output.output === "string" ? output.output.trimEnd() : "";
|
|
1406
|
+
output.output = text ? `${text}\n\n${banner}` : banner;
|
|
1407
|
+
} catch (error) {
|
|
1408
|
+
logger.warn("[router] delegation depth: advisory banner not delivered", { error: describeError(error) });
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1262
1413
|
if (sid && sessionStore.isSubagent(sid) && typeof input?.tool === "string") {
|
|
1263
1414
|
trajectoryStore.recordToolEvent(sid, {
|
|
1264
1415
|
tool: input.tool,
|
|
@@ -1294,7 +1445,7 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
1294
1445
|
try {
|
|
1295
1446
|
// pending.ts R11: the producer's changes landed by now.
|
|
1296
1447
|
const returnedAt = Date.now();
|
|
1297
|
-
const { finalReturnText, childSessionID } = parseTaskResult(output);
|
|
1448
|
+
const { finalReturnText, childSessionID } = parseTaskResult(unbannered ? { ...output, output: unbannered.output } : output);
|
|
1298
1449
|
const producerTier =
|
|
1299
1450
|
typeof input?.args?.subagent_type === "string"
|
|
1300
1451
|
? input.args.subagent_type
|
|
@@ -1379,7 +1530,8 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
1379
1530
|
|
|
1380
1531
|
const gateGraderSessions = new Set<string>();
|
|
1381
1532
|
const completedFailures: string[] = [];
|
|
1382
|
-
|
|
1533
|
+
// Keep the native grader unparented on the backend; only depth tracking uses its caller.
|
|
1534
|
+
const gateDeps = buildGateDeps(undefined, gateGraderSessions, verification, gateDeadline, false, orchestratorSessionID || null);
|
|
1383
1535
|
gateDeps.deterministic.onFailure = reason => completedFailures.push(reason);
|
|
1384
1536
|
let res;
|
|
1385
1537
|
try {
|
|
@@ -1497,6 +1649,12 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
1497
1649
|
sessionRootMemo.delete(id);
|
|
1498
1650
|
sessionLookupFailedAt.delete(id);
|
|
1499
1651
|
sessionStore.unregister(id);
|
|
1652
|
+
effortOverrides.clear(id);
|
|
1653
|
+
depthTracker.forget(id);
|
|
1654
|
+
for (const key of depthBanners.keys()) {
|
|
1655
|
+
if (key.startsWith(`${id}:`)) depthBanners.delete(key);
|
|
1656
|
+
}
|
|
1657
|
+
warnedNoCallID.delete(id);
|
|
1500
1658
|
// 2.4.2b: a deleted orchestrator's handles, tombstones and lineage records go with it.
|
|
1501
1659
|
// 2.4.5: so do its background requests and late notices; its run in flight is aborted.
|
|
1502
1660
|
background?.forgetSession(id);
|
|
@@ -1518,6 +1676,14 @@ const ModelRouterPlugin: Plugin = async (ctx: RouterPluginInput) => {
|
|
|
1518
1676
|
// subagent that has no task tool, and the subagent refused the work.
|
|
1519
1677
|
if (event?.type === "session.created") {
|
|
1520
1678
|
const info = event?.properties?.info;
|
|
1679
|
+
try {
|
|
1680
|
+
if (typeof info?.id === "string") {
|
|
1681
|
+
depthTracker.recordCreated(info.id,
|
|
1682
|
+
typeof info.parentID === "string" && info.parentID !== "" ? info.parentID : null);
|
|
1683
|
+
}
|
|
1684
|
+
} catch (error) {
|
|
1685
|
+
logger.warn("[router] delegation depth: session creation not recorded", { error: describeError(error) });
|
|
1686
|
+
}
|
|
1521
1687
|
if (
|
|
1522
1688
|
typeof info?.id === "string" &&
|
|
1523
1689
|
typeof info?.parentID === "string" &&
|
|
@@ -5,14 +5,14 @@ import type { PluginLogger } from "./logger";
|
|
|
5
5
|
* `src/index.ts` turns each tier of the active preset into an opencode agent
|
|
6
6
|
* definition; this module owns the `options` bag on that definition. It is the
|
|
7
7
|
* only place that knows how a tier's provider-agnostic `effort` maps onto the
|
|
8
|
-
* provider-specific knob (`effort` for Anthropic, `
|
|
8
|
+
* provider-specific knob (`effort` for Anthropic, `reasoningEffort` for
|
|
9
9
|
* OpenAI) and what to do when a tier asks for something the provider cannot do.
|
|
10
10
|
*
|
|
11
11
|
* The display-path builder in `src/commands/output.ts` renders what a tier is
|
|
12
12
|
* configured with; this one decides what is actually sent. They are kept apart
|
|
13
13
|
* on purpose: `/tiers` should not warn, downgrade or drop keys.
|
|
14
14
|
*/
|
|
15
|
-
import type { TierConfig } from "./config";
|
|
15
|
+
import type { EffortLevel, TierConfig } from "./config";
|
|
16
16
|
import { EFFORT_LEVELS } from "./config";
|
|
17
17
|
import { isAdaptiveOnlyClaudeModel, isClaudeModel } from "./protocol";
|
|
18
18
|
|
|
@@ -55,6 +55,33 @@ function isOpenAIModel(model: string): boolean {
|
|
|
55
55
|
return s.startsWith("openai/") || /\bgpt-/.test(s) || /(^|[/\-_])o[134]([/\-_]|$)/.test(s);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
export function effortRank(level: EffortLevel): number {
|
|
59
|
+
return EFFORT_LEVELS.indexOf(level);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function nextEffort(current: EffortLevel, bound: EffortLevel): EffortLevel | null {
|
|
63
|
+
return effortRank(current) < effortRank(bound)
|
|
64
|
+
? EFFORT_LEVELS[effortRank(current) + 1]!
|
|
65
|
+
: null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function minEffort(a: EffortLevel, b: EffortLevel): EffortLevel {
|
|
69
|
+
return effortRank(a) <= effortRank(b) ? a : b;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function effortCeilingFor(tier: TierConfig): EffortLevel | null {
|
|
73
|
+
if (tier.variant || tier.effort === undefined || !isEffortLevel(tier.effort)) return null;
|
|
74
|
+
if (isClaudeModel(tier.model)) {
|
|
75
|
+
if (Boolean(tier.thinking?.budgetTokens) && !isAdaptiveOnlyClaudeModel(tier.model)) return null;
|
|
76
|
+
return "max";
|
|
77
|
+
}
|
|
78
|
+
if (isOpenAIModel(tier.model)) {
|
|
79
|
+
if (tier.reasoning?.effort) return null;
|
|
80
|
+
return "high";
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
58
85
|
/**
|
|
59
86
|
* Provider-specific agent options for a tier, as registered with opencode.
|
|
60
87
|
*
|
|
@@ -64,8 +91,8 @@ function isOpenAIModel(model: string): boolean {
|
|
|
64
91
|
* loses to `effort`, with a one-time notice per tier.
|
|
65
92
|
*
|
|
66
93
|
* Explicit fields are gated by model: a Claude model never receives
|
|
67
|
-
* `
|
|
68
|
-
* (`isAdaptiveOnlyClaudeModel`) never receives `
|
|
94
|
+
* `reasoningEffort` / `reasoningSummary`, and an adaptive-only Claude model
|
|
95
|
+
* (`isAdaptiveOnlyClaudeModel`) never receives `thinking` — the budget is
|
|
69
96
|
* then ignored as if unset, so `effort` still applies. Each drop warns once
|
|
70
97
|
* per tier. A key is
|
|
71
98
|
* only ever present when something asked for it — an unset `effort` leaves no
|
|
@@ -94,8 +121,6 @@ export function buildAgentOptions(
|
|
|
94
121
|
`tier ${tierName}: model '${tier.model}' only accepts adaptive thinking and rejects a manual budget, so thinking.budgetTokens is ignored; use effort instead`,
|
|
95
122
|
logger,
|
|
96
123
|
);
|
|
97
|
-
} else if (hasThinkingBudget) {
|
|
98
|
-
opts.budget_tokens = tier.thinking?.budgetTokens;
|
|
99
124
|
} else if (tier.thinking?.budgetTokens === 0) {
|
|
100
125
|
warnAgentOptionsEffortOnce(
|
|
101
126
|
`thinking-zero:${tierName}`,
|
|
@@ -116,10 +141,7 @@ export function buildAgentOptions(
|
|
|
116
141
|
}
|
|
117
142
|
} else if (tier.reasoning) {
|
|
118
143
|
if (tier.reasoning.effort) {
|
|
119
|
-
opts.
|
|
120
|
-
}
|
|
121
|
-
if (tier.reasoning.summary) {
|
|
122
|
-
opts.reasoning_summary = tier.reasoning.summary;
|
|
144
|
+
opts.reasoningEffort = tier.reasoning.effort;
|
|
123
145
|
}
|
|
124
146
|
}
|
|
125
147
|
|
|
@@ -157,9 +179,9 @@ export function buildAgentOptions(
|
|
|
157
179
|
`tier ${tierName}: downgrading effort '${effort}' to 'high' because OpenAI reasoning_effort only supports low, medium, or high`,
|
|
158
180
|
logger,
|
|
159
181
|
);
|
|
160
|
-
opts.
|
|
182
|
+
opts.reasoningEffort = "high";
|
|
161
183
|
} else {
|
|
162
|
-
opts.
|
|
184
|
+
opts.reasoningEffort = effort;
|
|
163
185
|
}
|
|
164
186
|
} else {
|
|
165
187
|
warnAgentOptionsEffortOnce(
|
|
@@ -170,5 +192,14 @@ export function buildAgentOptions(
|
|
|
170
192
|
}
|
|
171
193
|
}
|
|
172
194
|
|
|
195
|
+
// Preserve the v2.0.0 bridge's insertion order: ordinary options, then
|
|
196
|
+
// reasoningEffort, reasoningSummary, thinking. Only emission moves; the
|
|
197
|
+
// family checks, warning order and explicit-config precedence stay intact.
|
|
198
|
+
if (!isClaude && tier.reasoning?.summary) {
|
|
199
|
+
opts.reasoningSummary = tier.reasoning.summary;
|
|
200
|
+
}
|
|
201
|
+
if (hasThinkingBudget) {
|
|
202
|
+
opts.thinking = { type: "enabled", budgetTokens: tier.thinking?.budgetTokens };
|
|
203
|
+
}
|
|
173
204
|
return opts;
|
|
174
205
|
}
|