@alexeiled/claude-router 1.3.1 → 1.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.
@@ -1,24 +1,24 @@
1
1
  import { activeClassifier, TIERS } from './config.mjs';
2
- import { classifierLabel, classifierStatus, GATEWAY_SETTINGS, SHORT_REASONS, usageMetrics } from './native-display.mjs';
3
- import { modelName, TIER_COLOR } from './native-panel.mjs';
2
+ import {
3
+ classifierLabel,
4
+ classifierStatus,
5
+ classifierWarns,
6
+ GATEWAY_SETTINGS,
7
+ modelName,
8
+ percent,
9
+ routeLabel,
10
+ SHORT_REASONS,
11
+ switchCount,
12
+ TIER_COLOR,
13
+ usageMetrics,
14
+ } from './display.mjs';
15
+ import { isSameModel } from './route.mjs';
4
16
 
5
17
  const BARS = '▂▄▆█';
6
18
  const SEPARATOR = ' · ';
7
19
  // Space the band keeps for the trailing Router button.
8
20
  const ROUTER_BUTTON = 8;
9
21
  const STRIP = 20;
10
- // Classifier failures worth a warning; busy and cancelled are routine and pass silently.
11
- const WARNED = new Set([
12
- 'missing-key',
13
- 'missing-account',
14
- 'paused',
15
- 'timeout',
16
- 'auth',
17
- 'http',
18
- 'unreachable',
19
- 'policy',
20
- 'malformed',
21
- ]);
22
22
 
23
23
  const part = (text, style = {}) => ({ text, style });
24
24
  const button = (props) => ({ button: props });
@@ -56,23 +56,18 @@ function meter(tier, dim = false) {
56
56
  return [...BARS].map((bar, i) => part(bar, i <= lit ? { color: TIER_COLOR[tier] } : { dimColor: true }));
57
57
  }
58
58
 
59
- function route(view) {
60
- const id = view.actualModel ?? view.selectedModel ?? view.nativeModel;
61
- const effort = view.effort === null || view.effort === undefined ? '' : ` · ${view.effort}`;
62
- return `${modelName(id)}${effort}`;
63
- }
59
+ const route = (view) => routeLabel(view.actualModel ?? view.selectedModel ?? view.nativeModel, view.effort);
64
60
 
65
61
  function served(view) {
66
62
  const { actualModel: actual, selectedModel: selected } = view;
67
- return Boolean(actual && selected && actual !== selected && !actual.startsWith(`${selected}-`));
63
+ return Boolean(actual && selected && !isSameModel(selected, actual));
68
64
  }
69
65
 
70
66
  function supportText(config, estimate) {
71
67
  if (!Number.isFinite(estimate?.threshold)) return null;
72
68
  const mass = Number.isFinite(estimate.upgradeMass) ? estimate.upgradeMass : estimate.downgradeMass;
73
69
  if (!Number.isFinite(mass)) return null;
74
- const pct = (n) => `${Math.round(n * 100)}%`;
75
- return `${classifierLabel(config)} ${pct(mass)} ${mass >= estimate.threshold ? '≥' : '<'} ${pct(estimate.threshold)}`;
70
+ return `${classifierLabel(config)} ${percent(mass)} ${mass >= estimate.threshold ? '≥' : '<'} ${percent(estimate.threshold)}`;
76
71
  }
77
72
 
78
73
  export function bandSegments(config, view, usage, actions) {
@@ -130,7 +125,7 @@ export function bandSegments(config, view, usage, actions) {
130
125
  );
131
126
  // The warning leads and the route yields: when the band is narrow, what to fix matters more than the model kept,
132
127
  // and a long classifier label ("Clef Flash: no account ID") must not push the warning out.
133
- if (WARNED.has(view.error))
128
+ if (classifierWarns(view.error))
134
129
  return [
135
130
  segment(0, part(`⚠ ${classifierStatus(config, view.error)}`, { color: 'yellow' })),
136
131
  view.error === 'missing-key' || view.error === 'missing-account'
@@ -141,7 +136,7 @@ export function bandSegments(config, view, usage, actions) {
141
136
  segment(3, part('keeping model', { color: 'yellow' })),
142
137
  ].filter(Boolean);
143
138
  const metrics = usageMetrics(config, view, usage);
144
- const percent = (bar) => part(bar.percent === null ? '?' : `${bar.percent}%`, { color: bar.color });
139
+ const reading = (bar) => part(bar.percent === null ? '?' : `${bar.percent}%`, { color: bar.color });
145
140
  const support = supportText(config, view.estimate);
146
141
  return [
147
142
  main,
@@ -150,7 +145,7 @@ export function bandSegments(config, view, usage, actions) {
150
145
  support ? segment(4, part(support, { dimColor: true })) : null,
151
146
  metrics.contextBar.percent === null && metrics.cacheBar.percent === null
152
147
  ? null
153
- : segment(3, part('ctx '), percent(metrics.contextBar), part(' · cache '), percent(metrics.cacheBar)),
148
+ : segment(3, part('ctx '), reading(metrics.contextBar), part(' · cache '), reading(metrics.cacheBar)),
154
149
  ].filter(Boolean);
155
150
  }
156
151
 
@@ -158,7 +153,7 @@ export function bandSegments(config, view, usage, actions) {
158
153
  function detailRow(config, view, usage) {
159
154
  const tiers = (view.tiers ?? []).slice(-STRIP);
160
155
  if (!tiers.length) return [part('no replies yet', { dimColor: true })];
161
- const switches = tiers.slice(1).filter((tier, i) => tier && tiers[i] && tier !== tiers[i]).length;
156
+ const switches = switchCount(tiers);
162
157
  const metrics = usageMetrics(config, view, usage);
163
158
  return [
164
159
  part('replies ', { dimColor: true }),
@@ -216,8 +211,7 @@ export function renderBand({ Box, Text, Button }, config, view, usage, { columns
216
211
  }
217
212
 
218
213
  export function switchToast(config, previous, next, estimate) {
219
- const label = (decision) =>
220
- `${modelName(decision.model)}${decision.effort === null || decision.effort === undefined ? '' : ` · ${decision.effort}`}`;
214
+ const label = (decision) => routeLabel(decision.model, decision.effort);
221
215
  const support = supportText(config, estimate);
222
216
  return `${label(previous)} → ${label(next)} — ${SHORT_REASONS[next.reason]?.replace(/^\W+\s*/, '') ?? next.reason}${support ? ` (${support})` : ''}`;
223
217
  }
@@ -1,13 +1,13 @@
1
- import { activeClassifier } from './config.mjs';
2
1
  import {
3
2
  buildRequest,
3
+ FAILURES_TO_PAUSE,
4
4
  isTransientStatus,
5
- JEV_FAILURES_TO_PAUSE,
6
- JEV_PAUSE_MS,
5
+ PAUSE_MS,
7
6
  parseAnswers,
8
7
  RETRY_DELAY_MS,
9
8
  retryDelayMs,
10
- } from './jev-contract.mjs';
9
+ } from './classifier-contract.mjs';
10
+ import { activeClassifier } from './config.mjs';
11
11
 
12
12
  class AdviceError extends Error {
13
13
  constructor(code) {
@@ -17,7 +17,7 @@ class AdviceError extends Error {
17
17
  }
18
18
 
19
19
  // Owned by one Mod activation. It survives history resets; the host cancels its work on unload.
20
- export class NativeJev {
20
+ export class ClassifierClient {
21
21
  constructor({ now = Date.now, health = null } = {}) {
22
22
  this.now = now;
23
23
  this.pending = null;
@@ -27,7 +27,7 @@ export class NativeJev {
27
27
 
28
28
  restore(health) {
29
29
  this.failures =
30
- Number.isInteger(health?.failures) && health.failures >= 0 ? Math.min(health.failures, JEV_FAILURES_TO_PAUSE) : 0;
30
+ Number.isInteger(health?.failures) && health.failures >= 0 ? Math.min(health.failures, FAILURES_TO_PAUSE) : 0;
31
31
  this.pausedUntil = Number.isFinite(health?.pausedUntil) ? health.pausedUntil : 0;
32
32
  }
33
33
 
@@ -50,8 +50,8 @@ export class NativeJev {
50
50
  } catch (error) {
51
51
  const code = signal?.aborted ? 'cancelled' : error instanceof AdviceError ? error.code : 'unreachable';
52
52
  if (code !== 'cancelled' && code !== 'policy') {
53
- this.failures = Math.min(this.failures + 1, JEV_FAILURES_TO_PAUSE);
54
- if (this.failures >= JEV_FAILURES_TO_PAUSE) this.pausedUntil = this.now() + JEV_PAUSE_MS;
53
+ this.failures = Math.min(this.failures + 1, FAILURES_TO_PAUSE);
54
+ if (this.failures >= FAILURES_TO_PAUSE) this.pausedUntil = this.now() + PAUSE_MS;
55
55
  }
56
56
  return { advice: null, error: code };
57
57
  } finally {
@@ -34,8 +34,8 @@ const ROUTE_INSTRUCTIONS = {
34
34
  const CONTINUATION_INSTRUCTIONS =
35
35
  'Is `currentRequest.text` a continuation of the task in `recentDialogue` (for example "continue", "yes", "now fix the tests"), rather than a new task?';
36
36
  export const RETRY_DELAY_MS = 100;
37
- export const JEV_FAILURES_TO_PAUSE = 3;
38
- export const JEV_PAUSE_MS = 60_000;
37
+ export const FAILURES_TO_PAUSE = 3;
38
+ export const PAUSE_MS = 60_000;
39
39
  const TRANSIENT = new Set([408, 429, 500, 502, 503, 504]);
40
40
 
41
41
  export function isTransientStatus(status) {
@@ -0,0 +1,62 @@
1
+ // router.json edits the pane writes: the rewrite, its validation and what Undo restores. Pure: the hook reads and
2
+ // writes the file.
3
+ import { loadConfig } from './config.mjs';
4
+
5
+ const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
6
+
7
+ // The router.json text after `change(previousFile)` and the config it loads, from the file's text or null when there
8
+ // is none. Throws when the old text is not JSON or the result does not validate.
9
+ export function rewrittenConfig(text, change) {
10
+ const file = change(text === null ? {} : JSON.parse(text));
11
+ return { config: loadConfig({ userFile: file }), text: `${JSON.stringify(file, null, 2)}\n` };
12
+ }
13
+
14
+ // The pane line for a write that failed.
15
+ export function notSaved(error) {
16
+ // A JSON parse message quotes the file; the loadConfig messages name the setting only.
17
+ const reason = error instanceof SyntaxError ? 'router.json is not valid JSON' : error.message;
18
+ return `Not saved: ${reason}. router.json is unchanged.`;
19
+ }
20
+
21
+ // The leaves where router.json `after` differs from `before`, each with its value in `before`, or no `value` where it
22
+ // had none. An object present on one side only is walked as empty on the other, so a sibling stays out of it.
23
+ export function changedLeaves(before, after, path = []) {
24
+ const out = [];
25
+ for (const key of new Set([...Object.keys(before), ...Object.keys(after)])) {
26
+ const [a, b] = [before[key], after[key]];
27
+ if ((isRecord(a) || a === undefined) && (isRecord(b) || b === undefined) && (isRecord(a) || isRecord(b)))
28
+ out.push(...changedLeaves(a ?? {}, b ?? {}, [...path, key]));
29
+ else if (JSON.stringify(a) !== JSON.stringify(b))
30
+ out.push(Object.hasOwn(before, key) ? { path: [...path, key], value: a } : { path: [...path, key] });
31
+ }
32
+ return out;
33
+ }
34
+
35
+ // The router.json `file` with each leaf of one pane write put back as it was before it: a value written again
36
+ // verbatim, a leaf that was absent deleted along with any object that leaves empty. Leaves the write did not touch,
37
+ // such as an edit made on disk since, stay as they are.
38
+ export function restored(file, leaves) {
39
+ const next = structuredClone(file);
40
+ for (const { path, ...leaf } of leaves) {
41
+ const parents = path.slice(0, -1);
42
+ const key = path.at(-1);
43
+ if (Object.hasOwn(leaf, 'value')) {
44
+ let node = next;
45
+ for (const name of parents) {
46
+ if (!isRecord(node[name])) node[name] = {};
47
+ node = node[name];
48
+ }
49
+ node[key] = leaf.value;
50
+ continue;
51
+ }
52
+ const chain = [next];
53
+ for (const name of parents) {
54
+ if (!isRecord(chain.at(-1)[name])) break;
55
+ chain.push(chain.at(-1)[name]);
56
+ }
57
+ if (chain.length !== path.length) continue;
58
+ delete chain.at(-1)[key];
59
+ for (let i = chain.length - 1; i > 0 && !Object.keys(chain[i]).length; i -= 1) delete chain[i - 1][path[i - 1]];
60
+ }
61
+ return next;
62
+ }
package/lib/config.mjs CHANGED
@@ -4,15 +4,16 @@ export const TIERS = ['micro', 'low', 'medium', 'high'];
4
4
  export const EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'];
5
5
  export const DEFAULTS = {
6
6
  baselineTier: 'low',
7
- // Sonnet 5.5 is the worker at `low`, at the effort the user picked. `medium` is Opus 5.5 at medium effort, a balance
8
- // of speed and cost per task; `high` is Opus 5.5 at xhigh, the strongest setting this router asks for.
9
- // `micro` is Haiku 5.5 at low effort: Claude Code owns the thinking field, so low effort is the least thinking the router can ask for.
10
- // These are routing defaults, not a claim of equal model quality.
7
+ // Haiku 5.5 is the worker at `low` and `micro`: at high effort it scored above Sonnet 5.5 at low, and at xhigh near
8
+ // Sonnet at medium, for a fraction of the cost per attempt (Anthropic's OSWorld 2.1 effort chart: computer use, not
9
+ // coding). Opus 5.5 at medium matched or beat Sonnet 5.5 at xhigh at 25-50% lower cost per task on Terminal-Bench
10
+ // 4.0, FrontierCode v1.1 and CursorBench 4.0 (anthropic.com/claude-sonnet-5-5). `high` is Opus 5.5 at xhigh, the
11
+ // strongest setting this router asks for. These are routing defaults, not a claim of equal model quality.
11
12
  routes: {
12
13
  high: { model: 'opus', effort: 'xhigh' },
13
14
  medium: { model: 'opus', effort: 'medium' },
14
- low: { model: 'sonnet' },
15
- micro: { model: 'haiku', effort: 'low' },
15
+ low: { model: 'haiku', effort: 'high' },
16
+ micro: { model: 'haiku', effort: 'medium' },
16
17
  },
17
18
  // `id` selects the native model. List prices in USD per million tokens; `cacheRead` is absolute,
18
19
  // not a multiplier (Opus 5.5 reads at 0.05x input, the rest at the standard 0.1x). Haiku 5.5 lists the rates for
@@ -176,7 +177,7 @@ export function withRoutes(file, draft) {
176
177
  const { model, effort = null } = draft.routes[tier];
177
178
  const preset = DEFAULTS.routes[tier];
178
179
  if (model === preset.model && effort === (preset.effort ?? null)) continue;
179
- routes[tier] = effort !== null || preset.effort ? { model, effort } : { model };
180
+ routes[tier] = { model, effort };
180
181
  }
181
182
  const baseline =
182
183
  draft.baselineTier === base.baselineTier
@@ -234,6 +235,17 @@ export function rank(tier) {
234
235
  return TIERS.indexOf(tier);
235
236
  }
236
237
 
238
+ // The model entry a tier routes to.
239
+ export const routeModel = (config, tier) => config.models[config.routes[tier].model];
240
+
241
+ // Whether a Claude Code version, such as 2.1.289 or a 2.1.290-beta build, runs the router: 2.1.289 or newer.
242
+ export function supportedVersion(version) {
243
+ const parts = /^(\d+)\.(\d+)\.(\d+)(?:$|-)/.exec(version ?? '');
244
+ if (!parts) return false;
245
+ const [major, minor, patch] = parts.slice(1).map(Number);
246
+ return major > 2 || (major === 2 && (minor > 1 || (minor === 1 && patch >= 289)));
247
+ }
248
+
237
249
  function merge(base, override) {
238
250
  if (Array.isArray(base) || typeof base !== 'object' || base === null) return override;
239
251
  const out = { ...base };
@@ -1,4 +1,4 @@
1
- import { EFFORTS } from './config.mjs';
1
+ import { EFFORTS, routeModel } from './config.mjs';
2
2
 
3
3
  export function cacheKey(modelId, effort) {
4
4
  return effort ? `${modelId}@${effort}` : modelId;
@@ -18,7 +18,7 @@ export function nextContextTokens(facts) {
18
18
 
19
19
  export function routeEffort(config, tier, sentEffort) {
20
20
  const route = config.routes[tier];
21
- const model = config.models[route.model];
21
+ const model = routeModel(config, tier);
22
22
  if (model.efforts.length === 0) return null;
23
23
  if (route.effort) return clampEffort(route.effort, model.efforts);
24
24
  if (typeof sentEffort === 'number' && Number.isFinite(sentEffort)) return sentEffort;
@@ -26,7 +26,7 @@ export function routeEffort(config, tier, sentEffort) {
26
26
  }
27
27
 
28
28
  export function routeCacheKey(config, tier, sentEffort) {
29
- return cacheKey(config.models[config.routes[tier].model].id, routeEffort(config, tier, sentEffort));
29
+ return cacheKey(routeModel(config, tier).id, routeEffort(config, tier, sentEffort));
30
30
  }
31
31
 
32
32
  export function isWarm(state, now, cache) {
@@ -41,7 +41,7 @@ export function cacheState(state, now, cache) {
41
41
 
42
42
  // Bounds are scenarios, never an independent ledger. Unknown incumbent cache can still be served.
43
43
  export function inputBounds(config, tier, tokens, facts, now) {
44
- const model = config.models[config.routes[tier].model];
44
+ const model = routeModel(config, tier);
45
45
  const state = facts.models[routeCacheKey(config, tier, facts.effort)];
46
46
  const prefix = Math.min(state?.prefixTokens ?? tokens, tokens);
47
47
  const fresh = isWarm(state, now, config.cache);
@@ -64,8 +64,8 @@ export function switchingTaxUsd(config, candidate, incumbent, facts, now) {
64
64
  }
65
65
 
66
66
  export function shadowEconomics(config, candidate, incumbent, facts, now) {
67
- const proposed = config.models[config.routes[candidate].model];
68
- const current = config.models[config.routes[incumbent].model];
67
+ const proposed = routeModel(config, candidate);
68
+ const current = routeModel(config, incumbent);
69
69
  if (!(proposed.output > 0) || !(current.output > 0)) return null;
70
70
  const tokens = nextContextTokens(facts);
71
71
  const outputTokens = facts.lastRequest?.outputTokens ?? 0;
@@ -1,5 +1,25 @@
1
- import { activeClassifier, CLASSIFIER_OPTIONS, endpointSettings } from './config.mjs';
2
- import { tierForModel } from './native-router.mjs';
1
+ import { activeClassifier, CLASSIFIER_OPTIONS, endpointSettings, routeModel } from './config.mjs';
2
+ import { tierForModel } from './route.mjs';
3
+
4
+ // Cost order, cheapest first, so the colors read as a scale. Hex values: every surface draws them.
5
+ export const TIER_COLOR = { micro: '#5fd7af', low: '#5f9fe0', medium: '#d7af5f', high: '#e0708a' };
6
+
7
+ export function modelName(id) {
8
+ if (!id) return 'no response yet';
9
+ return id
10
+ .replace(/^claude-/, '')
11
+ .replace(/-([0-9])-([0-9])(-\d{8})?$/, ' $1.$2')
12
+ .replace(/^./, (c) => c.toUpperCase());
13
+ }
14
+
15
+ // "Opus 5.5 · high"; a model with no effort reading shows its name alone.
16
+ export const routeLabel = (model, effort) =>
17
+ `${modelName(model)}${effort === null || effort === undefined ? '' : ` · ${effort}`}`;
18
+
19
+ // Changes of tier between consecutive replies; a reply routing did not choose breaks no run.
20
+ export const switchCount = (tiers) => tiers.slice(1).filter((tier, i) => tier && tiers[i] && tier !== tiers[i]).length;
21
+
22
+ export const percent = (value) => `${Math.round(value * 100)}%`;
3
23
 
4
24
  export const REASONS = {
5
25
  ready: 'ready for the next turn',
@@ -51,20 +71,28 @@ export function classifierStatus(config, error) {
51
71
  const label = activeClassifier(config).label;
52
72
  return error === 'missing-key' || error === 'missing-account'
53
73
  ? `${label}: ${missingText(config, config.classifier, error)}`
54
- : `${label} ${CLASSIFIER_ERRORS[error] ?? error}`;
74
+ : `${label} ${CLASSIFIER_ERRORS[error]?.text ?? error}`;
55
75
  }
56
76
 
57
- export const CLASSIFIER_ERRORS = {
58
- paused: 'paused after failures',
59
- timeout: 'timed out',
60
- auth: 'rejected the key',
61
- http: 'request failed',
62
- unreachable: 'unreachable',
63
- policy: 'blocked by network policy',
64
- malformed: 'sent a bad answer',
65
- busy: 'busy',
66
- cancelled: 'cancelled',
77
+ // Every classifier error: its words after the label (missing credentials name the setting instead), whether the band
78
+ // warns (busy and cancelled are routine and pass silently), and whether the wait counts as a timed reading (a request
79
+ // that was never sent does not).
80
+ const CLASSIFIER_ERRORS = {
81
+ 'missing-key': { text: null, warn: true, timed: false },
82
+ 'missing-account': { text: null, warn: true, timed: false },
83
+ paused: { text: 'paused after failures', warn: true, timed: false },
84
+ busy: { text: 'busy', warn: false, timed: false },
85
+ timeout: { text: 'timed out', warn: true, timed: true },
86
+ auth: { text: 'rejected the key', warn: true, timed: true },
87
+ http: { text: 'request failed', warn: true, timed: true },
88
+ unreachable: { text: 'unreachable', warn: true, timed: true },
89
+ policy: { text: 'blocked by network policy', warn: true, timed: true },
90
+ malformed: { text: 'sent a bad answer', warn: true, timed: true },
91
+ cancelled: { text: 'cancelled', warn: false, timed: true },
67
92
  };
93
+ export const classifierWarns = (error) => CLASSIFIER_ERRORS[error]?.warn === true;
94
+ // No error, or one outside the table, times the wait.
95
+ export const classifierTimed = (error) => CLASSIFIER_ERRORS[error]?.timed !== false;
68
96
  // One or two words for the band; REASONS holds the sentence the pane shows.
69
97
  export const SHORT_REASONS = {
70
98
  ready: 'ready',
@@ -116,7 +144,7 @@ export function bar(value, maximum, width = 16) {
116
144
  export function usageMetrics(config, view, usage) {
117
145
  const model = view.actualModel ?? view.selectedModel ?? view.nativeModel;
118
146
  const tier = tierForModel(config, model);
119
- const spec = tier ? config.models[config.routes[tier].model] : null;
147
+ const spec = tier ? routeModel(config, tier) : null;
120
148
  const window = spec?.contextWindow ?? null;
121
149
  const observed = Number.isFinite(usage?.context?.tokens) ? usage.context.tokens : null;
122
150
  const candidates = [view.contextTokens, observed === null ? null : observed + (view.outputTokens ?? 0)].filter(
@@ -9,37 +9,33 @@ import {
9
9
  GATEWAY_SETTINGS,
10
10
  missingCredentials,
11
11
  missingText,
12
+ modelName,
13
+ percent,
12
14
  REASONS,
15
+ routeLabel,
13
16
  sparkline,
17
+ switchCount,
18
+ TIER_COLOR,
14
19
  usageMetrics,
15
- } from './native-display.mjs';
20
+ } from './display.mjs';
21
+ import { isSameModel } from './route.mjs';
16
22
 
17
- export const TABS = [
23
+ const TABS = [
18
24
  ['now', '1', 'Now'],
19
25
  ['routing', '2', 'Routing'],
20
26
  ['classifier', '3', 'Classifier'],
21
27
  ['usage', '4', 'Usage'],
22
28
  ];
23
- export const TUNING_CHOICES = {
29
+ const TUNING_CHOICES = {
24
30
  timeoutMs: [500, 1000, 1500, 3000],
25
31
  downgradeVotes: [1, 2, 3],
26
32
  horizon: [1, 3, 5, 10],
27
33
  cashCapUsd: [0.5, 1, 2, 5],
28
34
  };
29
- // Cost order, cheapest first, so the colors read as a scale. Hex values: every surface draws them.
30
- export const TIER_COLOR = { micro: '#5fd7af', low: '#5f9fe0', medium: '#d7af5f', high: '#e0708a' };
31
35
  const LADDER = [...TIERS].reverse();
32
36
  const SESSION_EFFORT = 'session';
33
37
 
34
- export function modelName(id) {
35
- if (!id) return 'no response yet';
36
- return id
37
- .replace(/^claude-/, '')
38
- .replace(/-([0-9])-([0-9])(-\d{8})?$/, ' $1.$2')
39
- .replace(/^./, (c) => c.toUpperCase());
40
- }
41
-
42
- export function routeName(config, route) {
38
+ function routeName(config, route) {
43
39
  const model = config.models[route.model];
44
40
  return `${modelName(model.id)}${model.efforts.length ? ` · ${route.effort ?? SESSION_EFFORT}` : ''}`;
45
41
  }
@@ -53,7 +49,6 @@ export function routeDraftOf(config, view) {
53
49
 
54
50
  const dollars = (value) => (Number.isFinite(value) ? `$${value.toFixed(3)}` : 'not reported');
55
51
  const difference = (value) => `${value < 0 ? '−' : '+'}$${Math.abs(value).toFixed(3)}`;
56
- const percent = (value) => `${Math.round(value * 100)}%`;
57
52
  const hostOf = (endpoint) => endpoint.replace(/^https?:\/\//, '').split('/')[0];
58
53
  const windowSize = (tokens) => (tokens >= 1e6 ? `${tokens / 1e6}M` : `${Math.round(tokens / 1e3)}K`);
59
54
  const contextSuffix = (metrics) =>
@@ -167,14 +162,9 @@ function nowTab(ui, config, view, usage, actions) {
167
162
  const current = view.selectedModel ?? view.nativeModel;
168
163
  const label = classifierLabel(config);
169
164
  const missing = missingCredentials(view, config.classifier);
170
- const served =
171
- view.actualModel && current && view.actualModel !== current && !view.actualModel.startsWith(`${current}-`);
165
+ const served = view.actualModel && current && !isSameModel(current, view.actualModel);
172
166
  const out = [
173
- ui.line(
174
- 'Now ',
175
- view.tier ? ui.tier(view.tier, `▌${view.tier} `) : null,
176
- `${modelName(current)}${view.effort === null || view.effort === undefined ? '' : ` · ${view.effort}`}`,
177
- ),
167
+ ui.line('Now ', view.tier ? ui.tier(view.tier, `▌${view.tier} `) : null, routeLabel(current, view.effort)),
178
168
  ui.line('Why ', REASONS[view.reason] ?? view.reason ?? 'ready'),
179
169
  served
180
170
  ? ui.line(ui.text('Served ', { color: 'yellow' }), `${modelName(view.actualModel)} · native fallback`)
@@ -247,7 +237,7 @@ function nowTab(ui, config, view, usage, actions) {
247
237
  function replies(ui, view) {
248
238
  const tiers = view.tiers ?? [];
249
239
  if (!tiers.length) return [ui.head('REPLIES'), ui.dim(' no replies yet')];
250
- const switches = tiers.slice(1).filter((tier, i) => tier && tiers[i] && tier !== tiers[i]).length;
240
+ const switches = switchCount(tiers);
251
241
  return [
252
242
  ui.head(`REPLIES · last ${tiers.length}`),
253
243
  ui.line(
@@ -259,6 +249,8 @@ function replies(ui, view) {
259
249
  ];
260
250
  }
261
251
 
252
+ // Rows: [key, label, format, hint, router.json field]. Typed as tuples so tsc keeps each position's type.
253
+ /** @type {[string, string, (value: number) => string, string, string][]} */
262
254
  const POLICY_ROWS = [
263
255
  ['downgradeVotes', 'Votes to go down', String, 'agreeing turns before cheaper', 'policy.downgradeVotes'],
264
256
  ['horizon', 'Payback horizon', (v) => `${v} turns`, 'a switch repays its cache', 'policy.downgradeHorizonTurns'],
package/lib/policy.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // Switching policy v0 (docs/architecture.md): stickiness, escalation floor, cost-gated votes.
2
- import { rank, TIERS } from './config.mjs';
3
- import * as nativeCosts from './native-cost.mjs';
2
+ import { rank, routeModel, TIERS } from './config.mjs';
3
+ import * as nativeCosts from './cost.mjs';
4
4
 
5
5
  export function initialState() {
6
6
  return { turn: 0, votes: [], holdUntilTurn: 0, escalatedSignature: null };
@@ -65,12 +65,12 @@ export function decide({ config, facts, advice, state, baseline, now, costs = na
65
65
 
66
66
  // Share of a model's window the next request may fill. The estimate leaves out the new prompt and tool
67
67
  // results, so a model drops out before the conversation reaches its limit.
68
- const CONTEXT_FILL = 0.8;
68
+ export const CONTEXT_FILL = 0.8;
69
69
 
70
70
  // The lowest tier at or above `tier` whose model fits `tokens` of context. Routes need not grow in window
71
71
  // with rank, so the search falls back to any tier that fits, then to the largest window, keeping `tier` on a tie.
72
72
  export function fitTier(config, tier, tokens) {
73
- const windowOf = (t) => config.models[config.routes[t].model].contextWindow;
73
+ const windowOf = (t) => routeModel(config, t).contextWindow;
74
74
  const fits = (t) => tokens <= windowOf(t) * CONTEXT_FILL;
75
75
  if (fits(tier)) return tier;
76
76
  return (
@@ -88,9 +88,9 @@ function cashGate(config, tier, facts, now, costs) {
88
88
  if (config.models[alias].billing !== 'credits') return { blocked: false };
89
89
  if (costs.isWarm(facts.models[costs.routeCacheKey(config, tier, facts.effort)], now, config.cache))
90
90
  return { blocked: false };
91
- const cold = costs.coldWriteUsd(config, alias, costs.nextContextTokens(facts), facts);
91
+ const cold = costs.coldWriteUsd(config, alias, costs.nextContextTokens(facts));
92
92
  if (cold <= config.policy.cashCapUsd) return { blocked: false };
93
- const fallback = [...TIERS].reverse().find((t) => config.models[config.routes[t].model].billing === 'plan');
93
+ const fallback = [...TIERS].reverse().find((t) => routeModel(config, t).billing === 'plan');
94
94
  const estimate = { coldUsd: cold, cap: config.policy.cashCapUsd, cache: cacheOf(config, tier, facts, now, costs) };
95
95
  return { blocked: true, fallback, estimate };
96
96
  }
@@ -1,24 +1,30 @@
1
- import { TIERS } from './config.mjs';
2
- import { clip, extractFacts } from './facts-pure.mjs';
3
- import * as costs from './native-cost.mjs';
4
- import { decide, fitTier, initialState } from './policy.mjs';
1
+ import { routeModel, TIERS } from './config.mjs';
2
+ import * as costs from './cost.mjs';
3
+ import { clip, extractFacts } from './facts.mjs';
4
+ import { CONTEXT_FILL, decide, fitTier, initialState } from './policy.mjs';
5
5
 
6
6
  // Exact observed API snapshot, not a rule that merges different dated versions.
7
- export const SNAPSHOTS = { 'claude-haiku-4-5': 'claude-haiku-4-5-20251001' };
7
+ const SNAPSHOTS = { 'claude-haiku-4-5': 'claude-haiku-4-5-20251001' };
8
+
9
+ // A request this much smaller than the last means the history was compacted or rewound: the cached prefixes the
10
+ // observations describe are gone, so they are dropped rather than priced as warm.
11
+ const HISTORY_SHRINK = 0.8;
8
12
 
9
13
  // Whether the engine served the requested model, allowing for a dated snapshot of it.
10
14
  export function isSameModel(requested, served) {
11
15
  return requested === served || SNAPSHOTS[requested] === served;
12
16
  }
13
17
 
18
+ // The engine may echo our own routed model on a continuation; any third model is its fallback.
19
+ export function isNativeFallback(loop, model) {
20
+ if (loop.suspended) return true;
21
+ const known = [loop.engineModel, loop.decision?.model].filter(Boolean);
22
+ return known.length > 0 && !known.some((id) => isSameModel(id, model));
23
+ }
24
+
14
25
  export function tierForModel(config, id) {
15
26
  const order = [config.baselineTier, ...TIERS.filter((tier) => tier !== config.baselineTier)];
16
- return (
17
- order.find((tier) => {
18
- const model = config.models[config.routes[tier].model];
19
- return isSameModel(model.id, id);
20
- }) ?? null
21
- );
27
+ return order.find((tier) => isSameModel(routeModel(config, tier).id, id)) ?? null;
22
28
  }
23
29
 
24
30
  export function emptyLoop(config, model) {
@@ -95,35 +101,29 @@ export function chooseRoute(config, loop, { facts, advice, pin, nativeModel, con
95
101
  now,
96
102
  costs,
97
103
  });
104
+ // Without a reliable reading of the context, a tier with a smaller window than the last route's might overflow.
105
+ const shrinksUnmeasured = (tier) =>
106
+ (!contextKnown || !loop.historyMeasured) &&
107
+ routeModel(config, tier).contextWindow < routeModel(config, loop.lastRoute).contextWindow;
98
108
  const fits = (tier) => {
99
- const model = config.models[config.routes[tier].model];
100
- if (
101
- (!contextKnown || !loop.historyMeasured) &&
102
- model.contextWindow < config.models[config.routes[loop.lastRoute].model].contextWindow
103
- )
104
- return false;
109
+ const model = routeModel(config, tier);
105
110
  return (
111
+ !shrinksUnmeasured(tier) &&
106
112
  isModelAllowed(model.id, availableModels, nativeModel) &&
107
113
  !loop.ineligible.includes(model.id) &&
108
- (!contextKnown || costs.nextContextTokens(facts) <= model.contextWindow * 0.8)
114
+ (!contextKnown || costs.nextContextTokens(facts) <= model.contextWindow * CONTEXT_FILL)
109
115
  );
110
116
  };
111
117
  if (contextKnown) {
112
118
  const fitted = fitTier(config, result.tier, costs.nextContextTokens(facts));
113
119
  if (fitted !== result.tier) result = { ...result, tier: fitted, reason: 'context-fit' };
114
120
  }
115
- if (
116
- (!contextKnown || !loop.historyMeasured) &&
117
- config.models[config.routes[result.tier].model].contextWindow <
118
- config.models[config.routes[loop.lastRoute].model].contextWindow
119
- ) {
120
- result = { ...result, tier: loop.lastRoute, reason: 'context-unknown' };
121
- }
121
+ if (shrinksUnmeasured(result.tier)) result = { ...result, tier: loop.lastRoute, reason: 'context-unknown' };
122
122
  if (!fits(result.tier)) {
123
123
  const fallback = [loop.lastRoute, tierForModel(config, nativeModel), ...TIERS].find((tier) => tier && fits(tier));
124
124
  result = { ...result, tier: fallback ?? null, reason: 'model-unavailable' };
125
125
  }
126
- const model = result.tier ? config.models[config.routes[result.tier].model].id : nativeModel;
126
+ const model = result.tier ? routeModel(config, result.tier).id : nativeModel;
127
127
  const effort = result.tier ? costs.routeEffort(config, result.tier, facts.effort) : facts.effort;
128
128
  let comparison = null;
129
129
  const candidate = pin ?? advice?.choice;
@@ -134,8 +134,7 @@ export function chooseRoute(config, loop, { facts, advice, pin, nativeModel, con
134
134
  const forecast = costs.shadowEconomics(projected, candidate, loop.lastRoute, facts, now);
135
135
  // Without both output prices, compare input only, as shadowEconomics does.
136
136
  const output = forecast
137
- ? ((projected.models[projected.routes[candidate].model].output -
138
- projected.models[projected.routes[loop.lastRoute].model].output) *
137
+ ? ((routeModel(projected, candidate).output - routeModel(projected, loop.lastRoute).output) *
139
138
  loop.lastRequest.outputTokens) /
140
139
  1e6
141
140
  : 0;
@@ -168,7 +167,7 @@ export function continueRoute(config, loop, { nativeModel, contextTokens, contex
168
167
  decision = {
169
168
  ...decision,
170
169
  tier: fitted,
171
- model: config.models[config.routes[fitted].model].id,
170
+ model: routeModel(config, fitted).id,
172
171
  effort: costs.routeEffort(config, fitted, effort),
173
172
  reason: 'context-fit',
174
173
  };
@@ -179,7 +178,7 @@ export function continueRoute(config, loop, { nativeModel, contextTokens, contex
179
178
  return { ...loop, decision };
180
179
  }
181
180
 
182
- export function observeResponse(_config, loop, { usage, requestedModel, effort, stopReason, now }) {
181
+ export function observeResponse(loop, { usage, requestedModel, effort, stopReason, now }) {
183
182
  let next = loop;
184
183
  if (stopReason === 'model_context_window_exceeded') {
185
184
  next = { ...next, ineligible: [...new Set([...next.ineligible, requestedModel])] };
@@ -194,7 +193,7 @@ export function observeResponse(_config, loop, { usage, requestedModel, effort,
194
193
  const resolutions = sameModel ? { ...next.resolutions, [requestedModel]: usage.model } : { ...next.resolutions };
195
194
  if (!sameModel) delete resolutions[requestedModel];
196
195
  let models = next.models;
197
- if (next.lastRequest && tokens < next.lastRequest.tokens * 0.8) models = {};
196
+ if (next.lastRequest && tokens < next.lastRequest.tokens * HISTORY_SHRINK) models = {};
198
197
  // A substituted model's effort is not reported, so do not credit its estimated cache.
199
198
  if (sameModel)
200
199
  models = {