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.
@@ -1,15 +1,22 @@
1
- import type { RouterConfig } from "../router/config";
1
+ import { effortCeilingFor, effortRank, minEffort, nextEffort } from "../router/agent-options";
2
+ import { resolveEffortBump, type EffortLevel, type RouterConfig } from "../router/config";
3
+ import { getActiveTiers } from "../router/protocol";
2
4
 
3
5
  // ---------------------------------------------------------------------------
4
6
  // Types
5
7
  // ---------------------------------------------------------------------------
6
8
 
9
+ export interface EffortBumpPolicy {
10
+ perTier: Record<string, { base: EffortLevel; bound: EffortLevel }>;
11
+ }
12
+
7
13
  export interface EscalatePolicy {
8
14
  ladder: string[];
9
15
  floorTier?: string | null;
10
16
  maxAttemptsPerTier: number;
11
17
  maxTotalAttempts: number;
12
18
  costMultiple?: number | null;
19
+ effortBump?: EffortBumpPolicy | null;
13
20
  }
14
21
 
15
22
  export interface LadderState {
@@ -19,6 +26,7 @@ export interface LadderState {
19
26
  escalations: number;
20
27
  firstAttemptCost: number | null;
21
28
  cumulativeCost: number;
29
+ currentEffort?: EffortLevel | null;
22
30
  }
23
31
 
24
32
  export type LadderActionKind = "accept" | "retry" | "escalate" | "give_up";
@@ -28,6 +36,7 @@ export interface LadderAction {
28
36
  tier?: string;
29
37
  forcingMessage?: string;
30
38
  reason?: string;
39
+ effort?: EffortLevel;
31
40
  }
32
41
 
33
42
  export interface LadderVerdict {
@@ -59,7 +68,7 @@ export function newLadderState(
59
68
  producerTier: string,
60
69
  policy: EscalatePolicy,
61
70
  ): LadderState {
62
- return {
71
+ const state: LadderState = {
63
72
  currentTier: resolveStartTier(producerTier, policy),
64
73
  attemptsThisTier: 0,
65
74
  totalAttempts: 0,
@@ -67,6 +76,8 @@ export function newLadderState(
67
76
  firstAttemptCost: null,
68
77
  cumulativeCost: 0,
69
78
  };
79
+ if (policy.effortBump) state.currentEffort = null;
80
+ return state;
70
81
  }
71
82
 
72
83
  export function recordAttempt(
@@ -141,11 +152,25 @@ export function nextAction(
141
152
 
142
153
  // (5) retry within tier
143
154
  if (state.attemptsThisTier < policy.maxAttemptsPerTier) {
144
- return {
155
+ const action: LadderAction = {
145
156
  action: "retry",
146
157
  tier: state.currentTier,
147
158
  forcingMessage: buildLadderForcingMessage(verdict?.reasons ?? []),
148
159
  };
160
+ const perTier = policy.effortBump?.perTier;
161
+ const bump = perTier && Object.prototype.hasOwnProperty.call(perTier, state.currentTier)
162
+ ? perTier[state.currentTier] : undefined;
163
+ if (bump) {
164
+ const current = state.currentEffort ?? bump.base;
165
+ const startingEffort = minEffort(
166
+ effortRank(current) < effortRank(bump.base) ? bump.base : current,
167
+ bump.bound,
168
+ );
169
+ const effort = nextEffort(startingEffort, bump.bound)
170
+ ?? (state.currentEffort == null ? undefined : startingEffort);
171
+ if (effort !== undefined) action.effort = effort;
172
+ }
173
+ return action;
149
174
  }
150
175
 
151
176
  // (6) escalate or give_up
@@ -165,15 +190,19 @@ export function nextAction(
165
190
 
166
191
  export function advance(state: LadderState, action: LadderAction): LadderState {
167
192
  if (action.action === "retry") {
168
- return { ...state, attemptsThisTier: state.attemptsThisTier + 1 };
193
+ const next = { ...state, attemptsThisTier: state.attemptsThisTier + 1 };
194
+ if (action.effort !== undefined) next.currentEffort = action.effort;
195
+ return next;
169
196
  }
170
197
  if (action.action === "escalate") {
171
- return {
198
+ const next = {
172
199
  ...state,
173
200
  currentTier: action.tier!,
174
201
  attemptsThisTier: 0,
175
202
  escalations: state.escalations + 1,
176
203
  };
204
+ if (next.currentEffort !== undefined) next.currentEffort = null;
205
+ return next;
177
206
  }
178
207
  // accept / give_up — terminal, return unchanged
179
208
  return state;
@@ -181,13 +210,34 @@ export function advance(state: LadderState, action: LadderAction): LadderState {
181
210
 
182
211
  export function buildEscalatePolicy(cfg: RouterConfig): EscalatePolicy {
183
212
  const esc = cfg.enforcement?.escalate;
184
- return {
213
+ const policy: EscalatePolicy = {
185
214
  ladder: esc?.ladder ?? ["fast", "medium", "heavy"],
186
215
  floorTier: esc?.floorTier ?? null,
187
216
  maxAttemptsPerTier: esc?.maxAttemptsPerTier ?? 1,
188
217
  maxTotalAttempts: esc?.maxTotalAttempts ?? 4,
189
218
  costMultiple: esc?.costCeiling?.multiple ?? 4,
190
219
  };
220
+ const effortBump = buildEffortBump(cfg);
221
+ // Keep bump-off policies byte-identical to the original golden fixture.
222
+ if (effortBump) policy.effortBump = effortBump;
223
+ return policy;
224
+ }
225
+
226
+ function buildEffortBump(cfg: RouterConfig): EffortBumpPolicy | null {
227
+ const { enabled, max } = resolveEffortBump(cfg);
228
+ if (!enabled) return null;
229
+ const entries: Array<[string, { base: EffortLevel; bound: EffortLevel }]> = [];
230
+ for (const [name, tier] of Object.entries(getActiveTiers(cfg) ?? {})) {
231
+ if (typeof tier?.model !== "string") continue;
232
+ const ceiling = effortCeilingFor(tier);
233
+ // A non-null ceiling also validates the configured effort (D7).
234
+ if (ceiling === null || tier.effort === undefined) continue;
235
+ const base = tier.effort;
236
+ const bound = minEffort(ceiling, max);
237
+ if (effortRank(bound) <= effortRank(base)) continue;
238
+ entries.push([name, { base, bound }]);
239
+ }
240
+ return entries.length > 0 ? { perTier: Object.fromEntries(entries) } : null;
191
241
  }
192
242
 
193
243
  /**
@@ -199,7 +249,7 @@ export function formatLadderScorecard(
199
249
  method: string,
200
250
  ): string {
201
251
  return (
202
- `[router delegate scorecard | final_tier=${state.currentTier} | ` +
252
+ `[router delegate scorecard | final_tier=${state.currentTier}${state.currentEffort ? `@${state.currentEffort}` : ""} | ` +
203
253
  `attempts=${state.totalAttempts} | escalations=${state.escalations} | ` +
204
254
  `cost=${state.cumulativeCost} | verdict=${accepted ? "PASS" : "UNMET"} | ` +
205
255
  `method=${method}]`