switchback-core 2026.11.5 → 2026.11.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "switchback-core",
3
- "version": "2026.11.5",
3
+ "version": "2026.11.6",
4
4
  "type": "module",
5
5
  "description": "Host-agnostic core for switchback: fallback routing, error classification ports, block state and configuration.",
6
6
  "license": "MIT",
@@ -35,14 +35,18 @@ export function splitDecisionModelAddress(address: string): { provider: string;
35
35
  /** The connection details an address resolves to: its own entry first, then its provider's. */
36
36
  export function decisionModelConnection(config: ConnectionSource, address: string): { baseUrl?: string } | undefined {
37
37
  const parts = splitDecisionModelAddress(address);
38
- return config.decisionModels?.[address] ?? (parts === undefined ? undefined : config.decisionProviders?.[parts.provider]);
38
+ return (
39
+ config.decisionModels?.[address] ??
40
+ (parts === undefined ? undefined : config.decisionProviders?.[parts.provider])
41
+ );
39
42
  }
40
43
 
41
44
  /** Every decision-model address the config references, de-duplicated, in model order. */
42
45
  export function referencedDecisionModelAddresses(config: ResolvedSwitchbackFileConfig): string[] {
43
46
  const addresses = new Set<string>();
44
47
  for (const model of config.models) {
45
- if (model.decisionModel !== undefined) addresses.add(`${model.decisionModel.provider}/${model.decisionModel.id}`);
48
+ if (model.decisionModel !== undefined)
49
+ addresses.add(`${model.decisionModel.provider}/${model.decisionModel.id}`);
46
50
  }
47
51
  return [...addresses];
48
52
  }
package/src/config.ts CHANGED
@@ -521,7 +521,12 @@ function resolveFile(
521
521
  : Object.fromEntries(
522
522
  Object.entries(file.decisionModels).map(([address, entry]) => {
523
523
  const { provider, id } = splitAddress(address, `decisionModels.${address}`);
524
- const apiKey = resolveSecretApiKey(entry.apiKey, secrets, warnings, `decisionModels.${address}`);
524
+ const apiKey = resolveSecretApiKey(
525
+ entry.apiKey,
526
+ secrets,
527
+ warnings,
528
+ `decisionModels.${address}`,
529
+ );
525
530
  const descriptor: DecisionModelConfig = {
526
531
  provider,
527
532
  id,
package/src/crashes.ts CHANGED
@@ -38,7 +38,7 @@ import type { ClassifiedError, ErrorClass, ErrorScope } from "./types.ts";
38
38
  import type { NoClassifierReason } from "./classify.ts";
39
39
 
40
40
  /** What the router did about a particular failure. */
41
- export type CrashAction = "blind-cycle" | "blocked+advanced" | "stuck-stayed";
41
+ export type CrashAction = "blind-cycle" | "blocked+advanced" | "stuck-stayed" | "overflow-advanced";
42
42
 
43
43
  /** A classifier verdict (with the prompt version that produced it). */
44
44
  export interface CrashVerdict {
package/src/failure.ts CHANGED
@@ -180,7 +180,11 @@ export async function assessFailure(opts: {
180
180
  * This is the fallback for that case. It reuses `assessFailure`, so the
181
181
  * verdict and the block are produced by exactly the same code as the retry
182
182
  * path; only the routing decision is absent, because there is no retry left to
183
- * re-route. Returns true when it acted, so the caller can skip duplicates.
183
+ * re-route. Returns what the failure became, so the caller can act on the
184
+ * verdict class: `acted` mirrors the old boolean (the assessment touched the
185
+ * block map), and `verdict` lets pi's `agent_end` record an overflow give-up -
186
+ * the one verdict that needs a routing consequence on the *next* turn, because
187
+ * overflow writes no block for stickiness to trip over.
184
188
  */
185
189
  export async function observeUnretriedFailure(opts: {
186
190
  failedId: string | undefined;
@@ -191,7 +195,7 @@ export async function observeUnretriedFailure(opts: {
191
195
  now: number;
192
196
  blocked: BlockedMap;
193
197
  notify?: NotifyPort;
194
- }): Promise<boolean> {
198
+ }): Promise<{ acted: boolean; verdict: ClassifiedError | undefined }> {
195
199
  const { failedId, errorMessage, now } = opts;
196
200
  const assessed = await assessFailure({
197
201
  failedId,
@@ -211,7 +215,7 @@ export async function observeUnretriedFailure(opts: {
211
215
  // No verdict means nothing to justify a block with; record the sighting so
212
216
  // the corpus shows the gap, and leave routing untouched.
213
217
  recordDecideCrash(failedId, opts.registry, errorMessage, now, null, assessed.noClassifierReason, "blind-cycle");
214
- return false;
218
+ return { acted: false, verdict: undefined };
215
219
  }
216
220
  const classified = assessed.verdict;
217
221
  const action: CrashAction = assessed.touchedBlocks ? "blocked+advanced" : "stuck-stayed";
@@ -224,5 +228,5 @@ export async function observeUnretriedFailure(opts: {
224
228
  "warning",
225
229
  );
226
230
  }
227
- return assessed.touchedBlocks;
231
+ return { acted: assessed.touchedBlocks, verdict: classified };
228
232
  }
package/src/routing.ts CHANGED
@@ -101,6 +101,14 @@ export interface RouteInputs {
101
101
  * never to make a model ineligible. See `src/context-fit.ts`.
102
102
  */
103
103
  contextTokens?: number;
104
+ /**
105
+ * The model this session just gave up on with a context overflow (pi's
106
+ * `agent_end` fallback records it; there is no retry left to re-route). On the
107
+ * `user` path the router advances off this model instead of sticking to it,
108
+ * because nothing else will move the session - overflow writes no block.
109
+ * `undefined` = no recent overflow for this session.
110
+ */
111
+ overflowSkip?: ModelId;
104
112
  }
105
113
 
106
114
  function buildStateAfterSwitch(current: ModelId, prior: SwitchbackState | undefined): SwitchbackState {
@@ -352,6 +360,43 @@ export async function decide(
352
360
 
353
361
  const current = request.state?.current;
354
362
 
363
+ // Overflow advance: this session just gave up on `current` with a context
364
+ // overflow (pi's give-up point; see `observeUnretriedFailure`). Overflow writes
365
+ // no block, so stickiness would walk straight back into the model that cannot
366
+ // hold the payload - the one signal that must outrank stickiness on the user
367
+ // path. Advance forward from it, preferring a target that can hold the context;
368
+ // when nothing else is usable, fall through to the normal rules (sticking is
369
+ // then the only option).
370
+ const overflowSkip =
371
+ inputs.overflowSkip !== undefined && inputs.overflowSkip === current ? inputs.overflowSkip : undefined;
372
+ if (overflowSkip !== undefined) {
373
+ const pick = await pickWithContextFit({
374
+ resolved,
375
+ skip: overflowSkip,
376
+ blocked,
377
+ disabled: inputs.disabled,
378
+ now,
379
+ startAfter: current,
380
+ registry,
381
+ decisionModel: modelConfig.decisionModel,
382
+ contextTokens: inputs.contextTokens,
383
+ });
384
+ if (pick) {
385
+ inputs.notify?.(`switchback: ${overflowSkip} context overflow - advancing to ${pick.modelId}`, "warning");
386
+ return resolveDispatchLevel(
387
+ {
388
+ decision: { kind: "switch", modelId: pick.modelId, reason: "overflow-advance" },
389
+ nextState: buildStateAfterSwitch(pick.modelId, request.state),
390
+ thinkingLevel: request.thinkingLevel,
391
+ },
392
+ request,
393
+ modelConfig,
394
+ registry,
395
+ inputs,
396
+ );
397
+ }
398
+ }
399
+
355
400
  // Idle reset ("switch to initial"): after a long idle the provider's prompt cache
356
401
  // is cold, so staying buys nothing, and a model with a short quota window and no
357
402
  // weekly cap is worth preferring again. Configured per virtual model; the pin
@@ -585,18 +630,66 @@ async function decideRetry(
585
630
  };
586
631
  }
587
632
 
588
- // Overflow: pi already compacted; the route stays as the router chose it. Stick
589
- // to the failed model without blocking or counting as a transient retry.
633
+ // Overflow: a property of the request (the payload did not fit), not of the
634
+ // model's health - it never writes a block. But staying put forever dead-ends
635
+ // the session: pi has already compacted for this retry, and OpenCode re-sends
636
+ // the identical payload with nothing in between. So the verdict buys exactly
637
+ // ONE stick - the fair retry on the same model - recorded in the state; a
638
+ // second overflow verdict in the same cascade advances instead, still without
639
+ // blocking. No other entry at all: exhausted, so the host surfaces the failure
640
+ // rather than looping on a model that cannot take the request.
590
641
  if (classified?.class === "overflow") {
642
+ const stayId = failedId ?? priorState?.current ?? modelConfig.fallbacks[0]!;
643
+ if (priorState?.overflowRetried === true) {
644
+ const pick = await pickWithContextFit({
645
+ resolved,
646
+ skip: failedId,
647
+ excluded: retryExcluded,
648
+ failedScope: classified.scope,
649
+ blocked,
650
+ disabled: inputs.disabled,
651
+ now,
652
+ startAfter: advanceFrom,
653
+ registry,
654
+ decisionModel: modelConfig.decisionModel,
655
+ contextTokens: inputs.contextTokens,
656
+ });
657
+ recordDecideCrash(
658
+ failedId,
659
+ registry,
660
+ errorMessage,
661
+ now,
662
+ classified,
663
+ undefined,
664
+ pick ? "overflow-advanced" : "stuck-stayed",
665
+ );
666
+ if (pick) {
667
+ inputs.notify?.(`switchback: ${failedId} context overflow - advancing to ${pick.modelId}`, "warning");
668
+ return resolveDispatchLevel(
669
+ {
670
+ decision: { kind: "switch", modelId: pick.modelId, reason: "overflow-advance" },
671
+ nextState: buildStateAfterSwitch(pick.modelId, priorState),
672
+ thinkingLevel: request.thinkingLevel,
673
+ },
674
+ request,
675
+ modelConfig,
676
+ registry,
677
+ inputs,
678
+ );
679
+ }
680
+ return {
681
+ decision: { kind: "exhausted", reason: "overflow-no-fallback" },
682
+ thinkingLevel: request.thinkingLevel,
683
+ };
684
+ }
591
685
  const modelId = failedId ?? priorState?.current ?? "(unknown)";
592
686
  recordDecideCrash(failedId, registry, errorMessage, now, classified, undefined, "stuck-stayed");
593
687
  return {
594
688
  decision: { kind: "stick", modelId, reason: "overflow-stick" },
595
- nextState: keepState(
596
- failedId ?? priorState?.current ?? modelConfig.fallbacks[0]!,
597
- priorState,
598
- priorState?.transientRetries ?? 0,
599
- ),
689
+ nextState: {
690
+ ...keepState(stayId, priorState, priorState?.transientRetries ?? 0),
691
+ overflowRetried: true,
692
+ },
600
693
  thinkingLevel: request.thinkingLevel,
601
694
  };
602
695
  }
package/src/types.ts CHANGED
@@ -192,6 +192,14 @@ export interface SwitchbackState {
192
192
  * cascade; see `chainWith()` in routing.ts.
193
193
  */
194
194
  chainFailed?: ModelId[];
195
+ /**
196
+ * True when an overflow verdict already stuck once on `current` in this retry
197
+ * cascade. The first overflow gets one fair retry on the same model (pi has
198
+ * just compacted for it); a second one advances instead of re-dispatching the
199
+ * same doomed payload. Cleared by every switch and by every non-overflow stick,
200
+ * so a stale marker can never pre-empt a later run's fair retry.
201
+ */
202
+ overflowRetried?: boolean;
195
203
  }
196
204
 
197
205
  /** Cross-session blocked-until map. Keyed by "provider/id", value is epoch ms. */