opencode-model-router 2.0.0 → 2.2.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 +89 -0
- package/README.md +79 -6
- package/package.json +2 -2
- package/src/compat/child-session.ts +5 -0
- package/src/compat/v2-hooks.ts +98 -25
- package/src/escalate/effort-override.ts +165 -0
- package/src/escalate/ladder.ts +57 -7
- package/src/index.ts +251 -37
- package/src/router/agent-options.ts +43 -12
- package/src/router/config.ts +463 -74
- 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/tiers.json +43 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { BeforeResult } from "../guard/enforce";
|
|
2
|
+
import { MAX_DELEGATION_DEPTH_LIMIT } from "./config";
|
|
3
|
+
import { MAX_DEPTH_HOPS } from "./depth";
|
|
4
|
+
import type { DepthTracker } from "./depth";
|
|
5
|
+
import type { EnforcementMode } from "./enforcement";
|
|
6
|
+
|
|
7
|
+
export const DELEGATION_DEPTH_GUARD = "delegation_depth";
|
|
8
|
+
export type DepthGuardResult = BeforeResult & { banner?: string };
|
|
9
|
+
|
|
10
|
+
const MAX_WARNED_CALLERS = 1_000;
|
|
11
|
+
const MAX_WARNED_CAUSES = 100;
|
|
12
|
+
|
|
13
|
+
function describe(value: unknown): string {
|
|
14
|
+
try {
|
|
15
|
+
return value instanceof Error ? `${value.name}: ${value.message}` : `${typeof value}: ${String(value)}`;
|
|
16
|
+
} catch {
|
|
17
|
+
return "unprintable value";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Runtime seam values are keyed by their bounded printed form, never identity. */
|
|
22
|
+
function describeValue(value: unknown): string {
|
|
23
|
+
let printed: string;
|
|
24
|
+
try {
|
|
25
|
+
printed = String(value);
|
|
26
|
+
} catch {
|
|
27
|
+
printed = "unprintable value";
|
|
28
|
+
}
|
|
29
|
+
return `${typeof value}: ${printed}`.slice(0, 80);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function depthLimitMessage(depth: number, max: number): string {
|
|
33
|
+
return `[router] DELEGATION DEPTH LIMIT — this session is at delegation depth ${depth}; enforcement.maxDelegationDepth is ${max}, so it cannot dispatch another subagent. Do this part of the work yourself and report the result; do not retry the dispatch.`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function depthAdvisoryBanner(depth: number, max: number): string {
|
|
37
|
+
return `[⚠ GUARD:delegation_depth] this session is at delegation depth ${depth}; enforcement.maxDelegationDepth is ${max}. In enforced mode this dispatch would have been refused. Do not dispatch further subagents from this session; do that work yourself.`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Pure dispatch decision; the caller owns refusal and advisory-banner delivery. */
|
|
41
|
+
export function createDepthGuard(deps: {
|
|
42
|
+
tracker: Pick<DepthTracker, "depthOf">;
|
|
43
|
+
limit: () => number | null;
|
|
44
|
+
mode: (callerSessionID: string | undefined) => EnforcementMode;
|
|
45
|
+
logger: { warn(msg: string): void };
|
|
46
|
+
}): { checkDispatch(callerSessionID: string | undefined): Promise<DepthGuardResult> } {
|
|
47
|
+
const warnedCallers = new Set<string>();
|
|
48
|
+
const warnedModes = new Set<unknown>();
|
|
49
|
+
const warnedModeErrors = new Set<unknown>();
|
|
50
|
+
const warnedLimitErrors = new Set<unknown>();
|
|
51
|
+
const warnedDepths = new Set<unknown>();
|
|
52
|
+
const warnedLimits = new Set<unknown>();
|
|
53
|
+
let warnedCaller = false;
|
|
54
|
+
|
|
55
|
+
function warn(message: string): boolean {
|
|
56
|
+
try {
|
|
57
|
+
deps.logger.warn(`[router] delegation depth: ${message}`);
|
|
58
|
+
return true;
|
|
59
|
+
} catch {
|
|
60
|
+
// A broken logger must not change an admission decision or reject a call.
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function warnUnknown(caller: string): void {
|
|
66
|
+
if (warnedCallers.has(caller)) return;
|
|
67
|
+
if (!warn(`cannot resolve depth for session ${caller}; allowing dispatch.`)) return;
|
|
68
|
+
warnedCallers.add(caller);
|
|
69
|
+
if (warnedCallers.size > MAX_WARNED_CALLERS) {
|
|
70
|
+
warnedCallers.delete(warnedCallers.values().next().value!);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function warnOnce(causes: Set<unknown>, cause: unknown, message: string): void {
|
|
75
|
+
if (causes.has(cause)) return;
|
|
76
|
+
// Remember only delivered warnings; a broken logger is retried next time.
|
|
77
|
+
if (!warn(message)) return;
|
|
78
|
+
causes.add(cause);
|
|
79
|
+
if (causes.size > MAX_WARNED_CAUSES) causes.delete(causes.values().next().value);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
async checkDispatch(callerSessionID) {
|
|
84
|
+
// Read both seams on every call so live config and caller-tier changes apply.
|
|
85
|
+
let mode: EnforcementMode = "advisory";
|
|
86
|
+
try {
|
|
87
|
+
const resolved = deps.mode(callerSessionID);
|
|
88
|
+
if (resolved === "off" || resolved === "advisory" || resolved === "enforced") {
|
|
89
|
+
mode = resolved;
|
|
90
|
+
} else {
|
|
91
|
+
const cause = describeValue(resolved);
|
|
92
|
+
warnOnce(warnedModes, cause, `invalid enforcement mode ${cause}; using advisory.`);
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
const cause = describe(error);
|
|
96
|
+
warnOnce(warnedModeErrors, cause, `cannot resolve enforcement mode (${cause}); using advisory.`);
|
|
97
|
+
}
|
|
98
|
+
if (mode === "off") return { block: false, mode };
|
|
99
|
+
|
|
100
|
+
let max: number | null;
|
|
101
|
+
try {
|
|
102
|
+
max = deps.limit();
|
|
103
|
+
} catch (error) {
|
|
104
|
+
const cause = describe(error);
|
|
105
|
+
warnOnce(warnedLimitErrors, cause, `cannot resolve delegation depth limit (${cause}); using 1.`);
|
|
106
|
+
max = 1;
|
|
107
|
+
}
|
|
108
|
+
if (max === null) return { block: false, mode };
|
|
109
|
+
if (!Number.isInteger(max) || max < 1 || max > MAX_DELEGATION_DEPTH_LIMIT) {
|
|
110
|
+
const cause = describeValue(max);
|
|
111
|
+
warnOnce(warnedLimits, cause, `invalid delegation depth limit ${cause}; using effective limit 1.`);
|
|
112
|
+
max = 1;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (typeof callerSessionID !== "string" || callerSessionID === "") {
|
|
116
|
+
if (!warnedCaller) {
|
|
117
|
+
warnedCaller = warn("missing or invalid caller session; allowing dispatch.");
|
|
118
|
+
}
|
|
119
|
+
return { block: false, mode, guard: null };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let depth: number | undefined;
|
|
123
|
+
try {
|
|
124
|
+
depth = await deps.tracker.depthOf(callerSessionID);
|
|
125
|
+
} catch {
|
|
126
|
+
warnUnknown(callerSessionID);
|
|
127
|
+
return { block: false, mode, guard: null };
|
|
128
|
+
}
|
|
129
|
+
if (depth === undefined) {
|
|
130
|
+
// The tracker owns unknown-depth warnings (including failure/timeout).
|
|
131
|
+
// Phase 2.3 must pass the same plugin logger to tracker and guard: D2
|
|
132
|
+
// requires one warning, not one per layer, including forgotten callers.
|
|
133
|
+
// Only invalid callers and tracker contract violations are ours to log.
|
|
134
|
+
return { block: false, mode, guard: null };
|
|
135
|
+
}
|
|
136
|
+
if (!Number.isInteger(depth) || depth < 0) {
|
|
137
|
+
const cause = describe(depth);
|
|
138
|
+
warnOnce(warnedDepths, JSON.stringify([callerSessionID, cause]),
|
|
139
|
+
`invalid depth ${cause} for session ${callerSessionID}; using ${MAX_DEPTH_HOPS}.`);
|
|
140
|
+
depth = MAX_DEPTH_HOPS;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (depth + 1 > max) {
|
|
144
|
+
if (mode === "enforced") {
|
|
145
|
+
return { block: true, mode, message: depthLimitMessage(depth, max), guard: DELEGATION_DEPTH_GUARD };
|
|
146
|
+
}
|
|
147
|
+
return { block: false, mode, guard: DELEGATION_DEPTH_GUARD, banner: depthAdvisoryBanner(depth, max) };
|
|
148
|
+
}
|
|
149
|
+
return { block: false, mode, guard: null };
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|