@alexeiled/claude-router 1.6.1 → 1.7.1

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/lib/panel.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { compareRoutes } from './activity.mjs';
2
- import { readStore, storeSummary } from './activity-stats.mjs';
2
+ import { latencyP95, mostlyOn, readMetrics, readStore, storeSummary } from './activity-stats.mjs';
3
3
  import {
4
4
  ACTIVITIES,
5
5
  ACTIVITY_MODES,
@@ -13,6 +13,7 @@ import {
13
13
  TIERS,
14
14
  tuningOf,
15
15
  } from './config.mjs';
16
+ import { routeEffort } from './cost.mjs';
16
17
  import {
17
18
  ACTIVITY_LETTER,
18
19
  activityReading,
@@ -21,6 +22,7 @@ import {
21
22
  classifierLabel,
22
23
  classifierStatus,
23
24
  credentialsNeeded,
25
+ differenceShare,
24
26
  formatTokens,
25
27
  GATEWAY_CLEANUP,
26
28
  GATEWAY_SETTINGS,
@@ -28,15 +30,24 @@ import {
28
30
  missingText,
29
31
  modelName,
30
32
  percent,
31
- REASONS,
33
+ probeText,
32
34
  routeLabel,
33
35
  routeName,
36
+ savingsColor,
37
+ signedUsd,
38
+ sinceDate,
34
39
  sparkline,
40
+ strongerReplies,
35
41
  switchCount,
36
42
  TIER_COLOR,
43
+ unavailableReason,
37
44
  usageMetrics,
45
+ usd,
46
+ whyText,
47
+ yourModel,
38
48
  } from './display.mjs';
39
- import { isSameModel } from './route.mjs';
49
+ import { cellForModel, isSameModel } from './route.mjs';
50
+ import { EARLY_REPLIES, emptyTotals, modelSpec, readout, readSavingsStore } from './savings.mjs';
40
51
 
41
52
  const TABS = [
42
53
  ['now', '1', 'Now'],
@@ -49,14 +60,15 @@ const TUNING_CHOICES = {
49
60
  downgradeVotes: [1, 2, 3],
50
61
  horizon: [1, 3, 5, 10],
51
62
  cashCapUsd: [0.5, 1, 2, 5],
63
+ activityMass: [0.5, 0.6, 0.7, 0.8],
52
64
  };
53
65
  const LADDER = [...TIERS].reverse();
54
66
  const SESSION_EFFORT = 'session';
55
67
 
56
68
  const ACTIVITY_MODE_HINT = {
57
- off: 'asks nothing new; tier routes only',
58
- shadow: 'asks and shows what on would do; tier routes run',
59
- on: 'activity overrides run',
69
+ off: 'the activity is not asked; base routes run',
70
+ shadow: 'shows what on would do; base routes run',
71
+ on: 'an override runs when the activity has one',
60
72
  };
61
73
  const EFFORT_SHORT = { low: 'low', medium: 'med', high: 'high', xhigh: 'xh', max: 'max' };
62
74
 
@@ -113,7 +125,7 @@ export function renderPanel(
113
125
  view,
114
126
  usage,
115
127
  actions,
116
- { modelOptions = Object.keys(config.models) } = {},
128
+ { modelOptions = Object.keys(config.models), columns = null } = {},
117
129
  ) {
118
130
  const ui = kit(elements);
119
131
  const tab = TABS.some(([id]) => id === view.tab) ? view.tab : 'now';
@@ -122,7 +134,7 @@ export function renderPanel(
122
134
  flexDirection: 'column',
123
135
  children: [
124
136
  ...header(ui, config, view, tab, actions),
125
- ...body(ui, config, view, usage, actions, modelOptions),
137
+ ...body(ui, config, view, usage, actions, modelOptions, columns),
126
138
  ...(view.help ? helpLines(ui) : []),
127
139
  ...statusBar(ui, config, view, actions),
128
140
  ui.Button({ key: 'close', label: 'Close', hotkey: 'q', role: 'dismiss', onPress: actions.close }),
@@ -130,40 +142,49 @@ export function renderPanel(
130
142
  });
131
143
  }
132
144
 
133
- function header(ui, config, view, tab, actions) {
134
- const auto = view.mode === 'auto';
145
+ // Routing's state and its one mode control. While routing is unavailable, on and off would change nothing, so the
146
+ // header says why instead of offering them.
147
+ function routingState(ui, config, view, actions) {
148
+ if (view.phase === 'unavailable')
149
+ return [
150
+ ui.line(
151
+ ui.head('ROUTING'),
152
+ ' ',
153
+ ui.text('unavailable', { color: 'yellow' }),
154
+ ui.dim(' Claude’s model is kept'),
155
+ ),
156
+ ui.text(unavailableReason(view), { color: 'yellow' }),
157
+ ...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP.map((line) => ui.text(line, { color: 'yellow' })) : []),
158
+ ];
135
159
  const label = classifierLabel(config);
136
160
  const error = view.error ?? missingCredentials(config, view, config.classifier);
137
- const status =
138
- view.phase === 'unavailable'
139
- ? ui.text('Routing unavailable', { color: 'yellow' })
140
- : error
141
- ? ui.text(classifierStatus(config, error), { color: 'yellow' })
142
- : ui.text(`${label} ready${Number.isFinite(view.adviceMs) ? ` · ${view.adviceMs} ms` : ''}`, {
143
- color: 'green',
144
- });
161
+ // A radio pair, marked as the Classifier tab marks its active row.
162
+ const option = (mode, label, hotkey) =>
163
+ ui.Button({
164
+ key: mode,
165
+ label: `${view.mode === mode ? '◉' : '○'} ${label}`,
166
+ hotkey,
167
+ variant: view.mode === mode ? 'primary' : 'secondary',
168
+ onPress: () => actions.mode(mode),
169
+ });
145
170
  return [
146
- ui.line(ui.head(`ROUTING · ${auto ? 'on' : 'off'}`), ' ', status),
147
- view.phase === 'unavailable' && view.error ? ui.text(view.error, { color: 'yellow' }) : null,
148
- ...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP.map((line) => ui.text(line, { color: 'yellow' })) : []),
149
171
  ui.line(
150
- ui.Button({
151
- key: 'auto',
152
- label: 'Routing on',
153
- hotkey: 'o',
154
- variant: auto ? 'primary' : 'secondary',
155
- onPress: () => actions.mode('auto'),
156
- }),
172
+ ui.head('ROUTING'),
173
+ ' ',
174
+ option('auto', 'on', 'o'),
157
175
  ' ',
158
- ui.Button({
159
- key: 'manual',
160
- label: 'Routing off',
161
- hotkey: 'f',
162
- variant: auto ? 'secondary' : 'primary',
163
- onPress: () => actions.mode('manual'),
164
- }),
165
- ui.dim(' Routing off keeps the /model choice'),
176
+ option('manual', 'off', 'f'),
177
+ ui.dim(' off keeps the /model choice'),
166
178
  ),
179
+ error
180
+ ? ui.text(classifierStatus(config, error), { color: 'yellow' })
181
+ : ui.text(`${label} ready${Number.isFinite(view.adviceMs) ? ` · ${view.adviceMs} ms` : ''}`, { color: 'green' }),
182
+ ];
183
+ }
184
+
185
+ function header(ui, config, view, tab, actions) {
186
+ return [
187
+ ...routingState(ui, config, view, actions),
167
188
  ui.line(
168
189
  ...TABS.flatMap(([id, hotkey, label]) => [
169
190
  ui.Button({
@@ -183,10 +204,11 @@ function header(ui, config, view, tab, actions) {
183
204
 
184
205
  function nowTab(ui, config, view, usage, actions) {
185
206
  const metrics = usageMetrics(config, view, usage);
186
- const auto = view.mode === 'auto';
207
+ const unavailable = view.phase === 'unavailable';
208
+ const auto = view.mode === 'auto' && !unavailable;
187
209
  const current = view.selectedModel ?? view.nativeModel;
188
210
  const label = classifierLabel(config);
189
- const missing = missingCredentials(config, view, config.classifier);
211
+ const missing = unavailable ? null : missingCredentials(config, view, config.classifier);
190
212
  const served = view.actualModel && current && !isSameModel(current, view.actualModel);
191
213
  const reading = config.activityRouting === 'off' ? null : activityReading(view);
192
214
  const route = activityRouteText(config, view);
@@ -199,7 +221,7 @@ function nowTab(ui, config, view, usage, actions) {
199
221
  reading.others.length ? ui.dim(` ${reading.others.join(' · ')}`) : null,
200
222
  )
201
223
  : null,
202
- ui.line('Why ', REASONS[view.reason] ?? view.reason ?? 'ready'),
224
+ ui.line('Why ', whyText(config, view)),
203
225
  route ? ui.line('Route ', route) : null,
204
226
  served
205
227
  ? ui.line(ui.text('Served ', { color: 'yellow' }), `${modelName(view.actualModel)} · native fallback`)
@@ -250,7 +272,13 @@ function nowTab(ui, config, view, usage, actions) {
250
272
  );
251
273
  }
252
274
  out.push(
253
- ui.dim(auto ? ' Pins serve the next turn only.' : ' Pins need routing on. Routing off keeps the /model choice.'),
275
+ ui.dim(
276
+ unavailable
277
+ ? ' Pins need routing, which is unavailable.'
278
+ : auto
279
+ ? ' Pins serve the next turn only.'
280
+ : ' Pins need routing on. Routing off keeps the /model choice.',
281
+ ),
254
282
  );
255
283
  out.push(ui.text(' '), ...replies(ui, view));
256
284
  out.push(
@@ -275,8 +303,8 @@ function nowTab(ui, config, view, usage, actions) {
275
303
  function replies(ui, view) {
276
304
  const tiers = view.tiers ?? [];
277
305
  if (!tiers.length) return [ui.head('REPLIES'), ui.dim(' no replies yet')];
278
- const switches = switchCount(tiers);
279
306
  const activities = view.activities ?? [];
307
+ const switches = switchCount((view.routes ?? []).slice(-tiers.length));
280
308
  const letters = tiers.map((_, i) => activities[activities.length - tiers.length + i] ?? null);
281
309
  return [
282
310
  ui.head(`REPLIES · last ${tiers.length}`),
@@ -301,6 +329,13 @@ const POLICY_ROWS = [
301
329
  ['downgradeVotes', 'Votes to go down', String, 'agreeing turns before cheaper', 'policy.downgradeVotes'],
302
330
  ['horizon', 'Payback horizon', (v) => `${v} turns`, 'a switch repays its cache', 'policy.downgradeHorizonTurns'],
303
331
  ['cashCapUsd', 'Credits cap', (v) => `$${v.toFixed(2)}`, 'max cold write on credits', 'policy.cashCapUsd'],
332
+ [
333
+ 'activityMass',
334
+ 'Activity threshold',
335
+ percent,
336
+ 'a weaker reading keeps the running activity or uses the base route',
337
+ 'policy.activityMass',
338
+ ],
304
339
  ];
305
340
 
306
341
  // What the Routing tab changed and has not saved: tiers, baseline, activity cells and mode against the saved config,
@@ -397,7 +432,7 @@ function routingTab(ui, config, view, _usage, actions, modelOptions) {
397
432
  ui.Button({ key: 'reset-routes', label: 'Reset routes to defaults', hotkey: 'r', onPress: actions.resetRoutes }),
398
433
  ui.dim('A new model ID needs router.json → models: price, window, efforts.'),
399
434
  ui.text(' '),
400
- ...activitySections(ui, config, changes, actions, modelOptions),
435
+ ...activitySections(ui, config, view, changes, actions, modelOptions),
401
436
  ui.text(' '),
402
437
  ui.line(ui.head('POLICY'), ui.dim(' · applies next turn · edit, then Save')),
403
438
  ...POLICY_ROWS.map(([key, label, format, hint]) =>
@@ -456,7 +491,7 @@ function overrideWarnings(config, uses, activity, tier) {
456
491
  }
457
492
 
458
493
  // ACTIVITIES, the effective matrix of the draft, and OVERRIDES, its editable cells. Both join the routing draft.
459
- function activitySections(ui, config, changes, actions, modelOptions) {
494
+ function activitySections(ui, config, view, changes, actions, modelOptions) {
460
495
  const draft = { ...config, ...changes.draft };
461
496
  const mode = draft.activityRouting;
462
497
  const cellRoutes = ACTIVITIES.flatMap((activity) => TIERS.map((tier) => resolveRoute(draft, tier, activity)));
@@ -475,19 +510,9 @@ function activitySections(ui, config, changes, actions, modelOptions) {
475
510
  }
476
511
  const distinct = uses.size;
477
512
  const matrixRow = (name, cells) => ui.text(` ${name.padEnd(30)}${cells.map((c) => c.padEnd(9)).join('')}`);
513
+ // The mode leads the section: it decides whether the matrix below runs or is only shown.
478
514
  const out = [
479
- ui.line(ui.cell(32, ui.head(`ACTIVITIES · routing ${mode}`)), ui.dim(TIERS.map((t) => t.padEnd(9)).join(''))),
480
- matrixRow(
481
- 'base',
482
- TIERS.map((tier) => shortRoute(draft, draft.routes[tier])),
483
- ),
484
- ...[...rows.values()].map(({ names, cells }) => matrixRow(names.join(' '), cells)),
485
- ui.dim(
486
- ` ${distinct} distinct route${distinct === 1 ? '' : 's'} = ${distinct} cache${distinct === 1 ? '' : 's'}` +
487
- `${mode === 'on' ? '' : ' once activity routing is on'}`,
488
- ),
489
- ui.text(' '),
490
- ui.line(ui.head('OVERRIDES'), ui.dim(' · applies next turn · edit, then Save')),
515
+ ui.line(ui.head('ACTIVITIES'), ui.dim(' · applies next turn · edit, then Save')),
491
516
  ui.line(
492
517
  ui.cell(
493
518
  26,
@@ -504,6 +529,18 @@ function activitySections(ui, config, changes, actions, modelOptions) {
504
529
  marker(ui, changes.mode),
505
530
  ui.dim(ACTIVITY_MODE_HINT[mode]),
506
531
  ),
532
+ ui.line(ui.cell(32, ''), ui.dim(TIERS.map((t) => t.padEnd(9)).join(''))),
533
+ matrixRow(
534
+ 'base',
535
+ TIERS.map((tier) => shortRoute(draft, draft.routes[tier])),
536
+ ),
537
+ ...[...rows.values()].map(({ names, cells }) => matrixRow(names.join(' '), cells)),
538
+ ui.dim(
539
+ ` ${distinct} distinct route${distinct === 1 ? '' : 's'} = ${distinct} cache${distinct === 1 ? '' : 's'}` +
540
+ `${mode === 'on' ? '' : ' once activity routing is on'}`,
541
+ ),
542
+ ui.text(' '),
543
+ ui.line(ui.head('OVERRIDES'), ...overridesState(ui, view, config.activityRouting, mode)),
507
544
  ];
508
545
  const free = [];
509
546
  for (const activity of ACTIVITIES) {
@@ -538,6 +575,24 @@ function activitySections(ui, config, changes, actions, modelOptions) {
538
575
  return out;
539
576
  }
540
577
 
578
+ // Whether the overrides run now: activity routing on is not enough while routing itself is off or unavailable.
579
+ // Whether overrides run now: the saved mode decides, since a draft runs nothing until Save. A different draft mode
580
+ // says what Save will change.
581
+ function overridesState(ui, view, saved, drafted) {
582
+ const why = (mode) =>
583
+ view.phase === 'unavailable'
584
+ ? 'routing is unavailable'
585
+ : view.mode === 'manual'
586
+ ? 'routing is off'
587
+ : mode === 'on'
588
+ ? null
589
+ : `activity routing is ${mode}`;
590
+ const now = why(saved);
591
+ const state = now ? ui.text(` · not in use: ${now}`, { color: 'yellow' }) : ui.dim(' · in use');
592
+ if (drafted === saved || Boolean(why(drafted)) === Boolean(now)) return [state];
593
+ return [state, ui.dim(why(drafted) ? ' · not in use after Save' : ' · in use after Save')];
594
+ }
595
+
541
596
  function overrideRow(ui, draft, route, activity, tier, edited, actions, modelOptions, uses) {
542
597
  const model = draft.models[route.model];
543
598
  const aliases = modelOptions.includes(route.model) ? modelOptions : [route.model, ...modelOptions];
@@ -596,10 +651,11 @@ function choice(ui, key, value, format, pick) {
596
651
  function classifierTab(ui, config, view, _usage, actions) {
597
652
  const active = activeClassifier(config);
598
653
  const health = view.health ?? { failures: 0, pausedUntil: 0 };
599
- const rows = Object.entries(config.classifiers).map(([id, entry]) => {
654
+ const rows = Object.entries(config.classifiers).flatMap(([id, entry]) => {
600
655
  const isActive = id === config.classifier;
601
656
  const missing = missingCredentials(config, view, id);
602
- return ui.line(
657
+ const probe = probeText(config, id);
658
+ const row = ui.line(
603
659
  ui.Button({
604
660
  key: `classifier-${id}`,
605
661
  label: `${isActive ? '◉' : '○'} ${entry.label.padEnd(11)}`,
@@ -617,6 +673,7 @@ function classifierTab(ui, config, view, _usage, actions) {
617
673
  ? ui.dim(` ${view.adviceMs} ms`)
618
674
  : null,
619
675
  );
676
+ return probe ? [row, ui.dim(` ${probe}`)] : [row];
620
677
  });
621
678
  return [
622
679
  ui.line(ui.head('CLASSIFIER'), ui.dim(' · applies next turn · saves at once')),
@@ -710,10 +767,11 @@ function shadowLine(shadow) {
710
767
  return `${counted} · est. ${difference(shadow.minUsd)} … ${difference(shadow.maxUsd)}${some} at list prices`;
711
768
  }
712
769
 
713
- // The counts kept across sessions, read from the store as last loaded: what the decision to turn activity routing on
714
- // by default reads. Agreement uses the classifier's raw answer, so it can differ from the session's.
715
- function storeUsage(ui, store) {
770
+ // The counts kept across sessions, read from the stores as last loaded. Agreement uses the classifier's raw answer, so
771
+ // it can differ from the session's.
772
+ function storeUsage(ui, config, store, metrics) {
716
773
  const summary = storeSummary(readStore(store));
774
+ const { latency, downMoves } = readMetrics(metrics);
717
775
  const out = [ui.head('ACROSS SESSIONS · since the last reset')];
718
776
  if (summary.labelled === 0) return [...out, ui.dim(' no turns recorded yet')];
719
777
  out.push(ui.text(`Labelled ${summary.labelled} turns`));
@@ -732,36 +790,70 @@ function storeUsage(ui, store) {
732
790
  ui.text(`Mismatch ${summary.disagreements.map((d) => `${d.predicted} → ${d.observed} ${d.n}`).join(' · ')}`),
733
791
  );
734
792
  const { taken, refused } = summary.lateral;
735
- if (taken + refused > 0) out.push(ui.text(`Lateral ${taken} taken · ${refused} refused`));
793
+ if (taken + refused > 0) out.push(ui.text(`Activity moves ${taken} taken · ${refused} refused`));
794
+ const { moves, escalations } = downMoves;
795
+ if (moves > 0)
796
+ out.push(
797
+ ui.text(`Escalations after a cheaper activity move: ${escalations} in ${moves} move${moves === 1 ? '' : 's'}`),
798
+ );
799
+ const p95 = latencyP95(Object.hasOwn(latency, config.classifier) ? latency[config.classifier] : null);
800
+ if (p95)
801
+ out.push(
802
+ ui.text(
803
+ `Latency ${classifierLabel(config)} p95 ${p95.over ? '>' : '≤'} ${p95.ms} ms · ${p95.turns} turn${p95.turns === 1 ? '' : 's'}`,
804
+ ),
805
+ );
736
806
  if (summary.shadow.turns > 0) out.push(ui.text(shadowLine(summary.shadow)));
737
807
  return out;
738
808
  }
739
809
 
740
- // Session counts by activity, the switches by cause, the tool agreement and the shadow readout, then the counts kept
810
+ // The ACTIVITY table's columns before the cost, and the cost column.
811
+ const ACTIVITY_TABLE = 45;
812
+ const ACTIVITY_COST = 11;
813
+
814
+ // Session counts by activity: turns, requests, share, the estimated list price of their replies and the route most of
815
+ // them ran on; the switches by cause, the tool agreement and the shadow readout, then the counts kept
741
816
  // across sessions. Reset is always offered: the counts kept across sessions may exist before this session has any.
742
- function activityUsage(ui, config, view, actions) {
817
+ function activityUsage(ui, config, view, actions, columns) {
743
818
  const stats = view.activityStats ?? null;
744
819
  const mode = config.activityRouting;
745
- const reset = ui.Button({
746
- key: 'reset-activity-stats',
747
- label: 'Reset activity stats',
748
- onPress: actions.resetActivityStats,
749
- });
750
- const across = [ui.text(' '), ...storeUsage(ui, view.activityStore)];
820
+ const reset = ui.line(
821
+ ui.Button({ key: 'reset-stats', label: 'Reset stats', onPress: actions.resetStats }),
822
+ ui.dim(' clears activity stats and routing vs your model, all sessions'),
823
+ );
824
+ const across = [ui.text(' '), ...storeUsage(ui, config, view.activityStore, view.activityMetrics)];
751
825
  if (mode === 'off' && !stats)
752
826
  return [
753
- ui.head('ACTIVITY · routing off'),
827
+ ui.head('ACTIVITY · activity routing off'),
754
828
  ui.dim(' Not asked. Set activity routing to shadow or on in the Routing tab.'),
755
829
  ...(storeSummary(readStore(view.activityStore)).labelled > 0 ? across : []),
756
830
  reset,
757
831
  ];
758
- const out = [ui.line(ui.head('ACTIVITY · this session'), ui.dim(` routing ${mode}`))];
832
+ const out = [ui.line(ui.head('ACTIVITY · this session'), ui.dim(` activity routing ${mode}`))];
759
833
  const rows = Object.entries(stats?.byActivity ?? {}).filter(([, counts]) => counts?.turns > 0);
760
834
  if (!rows.length) out.push(ui.dim(' no activity readings yet'));
761
835
  else {
762
836
  const total = rows.reduce((sum, [, counts]) => sum + counts.turns, 0);
837
+ // A partial sum would read as the whole: a cost shows only when every reply had a price.
838
+ const cost = (counts) =>
839
+ Number.isFinite(counts.routedUsd) && counts.pricedRequests === counts.requests ? usd(counts.routedUsd) : '—';
840
+ const route = (counts) => {
841
+ const key = mostlyOn(counts);
842
+ if (!key) return '—';
843
+ const at = key.lastIndexOf('@');
844
+ const effort = key.slice(at + 1);
845
+ return routeLabel(key.slice(0, at), effort === 'session' ? null : effort);
846
+ };
847
+ // A narrow pane drops "mostly on", then the cost, before any column it had before them.
848
+ const widest = Math.max('mostly on'.length, ...rows.map(([, counts]) => route(counts).length));
849
+ const fits = (width) => !Number.isFinite(columns) || width <= columns;
850
+ const showCost = fits(ACTIVITY_TABLE + ACTIVITY_COST);
851
+ const showRoute = showCost && fits(ACTIVITY_TABLE + ACTIVITY_COST + 3 + widest);
763
852
  out.push(
764
- ui.dim(` ${''.padEnd(9)}${''.padEnd(10)}${'turns'.padStart(7)}${'requests'.padStart(10)}${'share'.padStart(7)}`),
853
+ ui.dim(
854
+ ` ${''.padEnd(9)}${''.padEnd(10)}${'turns'.padStart(7)}${'requests'.padStart(10)}${'share'.padStart(7)}` +
855
+ `${showCost ? 'est. cost'.padStart(ACTIVITY_COST) : ''}${showRoute ? ' mostly on' : ''}`,
856
+ ),
765
857
  );
766
858
  for (const [name, counts] of rows.sort((a, b) => b[1].turns - a[1].turns)) {
767
859
  const share = counts.turns / total;
@@ -769,14 +861,21 @@ function activityUsage(ui, config, view, actions) {
769
861
  ui.line(
770
862
  ` ${name.padEnd(9)}`,
771
863
  ui.text(bar(share, 1, 10).text, { color: 'cyan' }),
772
- `${String(counts.turns).padStart(7)}${String(counts.requests).padStart(10)}${percent(share).padStart(7)}`,
864
+ `${String(counts.turns).padStart(7)}${String(counts.requests).padStart(10)}${percent(share).padStart(7)}` +
865
+ `${showCost ? cost(counts).padStart(ACTIVITY_COST) : ''}${showRoute ? ` ${route(counts)}` : ''}`,
773
866
  ),
774
867
  );
775
868
  }
776
869
  }
777
870
  if (stats) {
778
871
  const { tier, activity } = stats.switches;
779
- out.push(ui.text(`Switches ${tier + activity} · ${tier} by tier · ${activity} by activity`));
872
+ // Refused moves change no route. The session counts them in `on` only, so moves from before a switch to shadow stay.
873
+ const refused = stats.lateral?.refused ?? 0;
874
+ out.push(
875
+ ui.text(
876
+ `Switches ${tier + activity} · ${tier} by tier · ${activity} by activity${refused ? ` · ${refused} refused` : ''}`,
877
+ ),
878
+ );
780
879
  const { matched, total } = stats.agreement;
781
880
  if (total > 0)
782
881
  out.push(ui.text(`Agreement classifier vs tools: ${matched} of ${total} turns (${percent(matched / total)})`));
@@ -785,7 +884,237 @@ function activityUsage(ui, config, view, actions) {
785
884
  return [...out, ...across, reset];
786
885
  }
787
886
 
788
- function usageTab(ui, config, view, usage, actions) {
887
+ // Bars are this wide at the larger of routed and your model; the section drops them before any number when the pane is
888
+ // narrower than the table.
889
+ const SAVINGS_BAR = 22;
890
+ const SAVINGS_COLUMN = 15;
891
+ const SAVINGS_PARTS = [' cheaper models', ' stronger than yours', ' cache writes from switches'];
892
+ // The footer as [text, joiner] pieces, packed to the pane: two lines at full width, more in a narrow pane, never a
893
+ // line that ends in a joiner.
894
+ const SAVINGS_FOOTER = [
895
+ ['API list prices', ' · '],
896
+ ['same tokens and output length', ' · '],
897
+ ['not a bill;', ' '],
898
+ ['on a Claude plan it stands for quota', ' · '],
899
+ ['answer quality not measured', ''],
900
+ ];
901
+ const FULL_WIDTH = 68;
902
+
903
+ // `pieces` packed into lines of at most `width` cells after `indent`.
904
+ function pack(pieces, width, indent) {
905
+ const lines = [];
906
+ let line = '';
907
+ let joiner = '';
908
+ for (const [text, next] of pieces) {
909
+ if (line && indent.length + line.length + joiner.length + text.length > width) {
910
+ lines.push(indent + line);
911
+ line = text;
912
+ } else line += (line ? joiner : '') + text;
913
+ joiner = next;
914
+ }
915
+ return line ? [...lines, indent + line] : lines;
916
+ }
917
+
918
+ // A dim sentence wrapped by words to a narrow pane, keeping its indent, which Ink's own wrap would drop.
919
+ const dimLines = (ui, text, columns) => {
920
+ const indent = /^ */.exec(text)[0];
921
+ const words = text
922
+ .trim()
923
+ .split(' ')
924
+ .map((word) => [word, ' ']);
925
+ return (Number.isFinite(columns) ? pack(words, columns, indent) : [text]).map((line) => ui.dim(line));
926
+ };
927
+ const footer = (ui, columns) =>
928
+ pack(SAVINGS_FOOTER, Math.min(FULL_WIDTH, columns ?? FULL_WIDTH), ' ').map((line) => ui.dim(line));
929
+
930
+ // One readout cell of the Difference row: "too early" below EARLY_REPLIES replies, else the signed amount and share.
931
+ function differenceCell(ui, r) {
932
+ if (!r) return '';
933
+ if (r.early) return ui.dim('too early');
934
+ const share = differenceShare(r);
935
+ return ui.text(`${signedUsd(r.differenceUsd)}${share ? ` ${share}` : ''}`, {
936
+ bold: true,
937
+ color: savingsColor(r.differenceUsd),
938
+ });
939
+ }
940
+
941
+ const partCell = (ui, value) => (value === null ? '' : ui.text(signedUsd(value), { color: savingsColor(value) }));
942
+
943
+ // A bar of `parts` ([usd, color]) at `scale` cells per dollar, filled to SAVINGS_BAR with dim cells. Each part ends at
944
+ // its rounded running total, so the parts add up to the bar.
945
+ function moneyBar(ui, parts, scale) {
946
+ let at = 0;
947
+ let sum = 0;
948
+ const out = [];
949
+ for (const [value, color] of parts) {
950
+ sum += value;
951
+ const end = Math.round(sum * scale);
952
+ if (end > at) out.push(ui.text('█'.repeat(end - at), color ? { color } : {}));
953
+ at = Math.max(at, end);
954
+ }
955
+ if (at < SAVINGS_BAR) out.push(ui.dim('░'.repeat(SAVINGS_BAR - at)));
956
+ return out;
957
+ }
958
+
959
+ // The parts of a session readout past "too early": each part, the stronger replies, the two costs as bars (not in a
960
+ // narrow pane), the replies by tier, why routing spent more, and the shadow estimate.
961
+ function savingsDetail(ui, config, view, { totals, session, across, row, tier, narrow, columns }) {
962
+ const out = [];
963
+ const savedPart = (key) => (across && !across.early ? partCell(ui, across[key]) : null);
964
+ const stronger = strongerReplies(totals);
965
+ out.push(
966
+ row(ui.dim(SAVINGS_PARTS[0]), partCell(ui, session.cheaperUsd), savedPart('cheaperUsd')),
967
+ row(ui.dim(SAVINGS_PARTS[1]), partCell(ui, session.strongerUsd), savedPart('strongerUsd')),
968
+ ...(stronger.count
969
+ ? dimLines(
970
+ ui,
971
+ ` ${stronger.count} repl${stronger.count === 1 ? 'y' : 'ies'} on ${stronger.several ? 'stronger models' : modelName(stronger.model)}`,
972
+ columns,
973
+ )
974
+ : []),
975
+ row(ui.dim(SAVINGS_PARTS[2]), partCell(ui, session.switchUsd), savedPart('switchUsd')),
976
+ ui.text(' '),
977
+ );
978
+ const largest = Math.max(totals.routedUsd, totals.yoursUsd);
979
+ if (!narrow && largest > 0) {
980
+ const scale = SAVINGS_BAR / largest;
981
+ const tiers = [...TIERS, 'none'];
982
+ const routedParts = tiers.map((t) => [totals.tierUsd[t] ?? 0, TIER_COLOR[t]]);
983
+ out.push(
984
+ ui.line(ui.dim(' routed '), ...moneyBar(ui, routedParts, scale), ` ${usd(session.routedUsd)}`),
985
+ ui.line(
986
+ ui.dim(' your model '),
987
+ ...moneyBar(ui, [[totals.yoursUsd, TIER_COLOR[tier]]], scale),
988
+ ` ${usd(session.yoursUsd)}`,
989
+ ),
990
+ );
991
+ }
992
+ const shares = [...TIERS, 'none'].filter((t) => totals.tiers[t] > 0);
993
+ out.push(
994
+ ui.line(
995
+ ' ',
996
+ ...shares.flatMap((t, i) => [
997
+ t === 'none' ? ui.dim('■ ') : ui.tier(t, '■ '),
998
+ ui.dim(
999
+ `${t === 'none' ? 'not routed' : t} ${percent(totals.tiers[t] / totals.replies)}${i === shares.length - 1 ? ' of replies' : ' '}`,
1000
+ ),
1001
+ ]),
1002
+ ),
1003
+ );
1004
+ if (session.differenceUsd > 0 && stronger.count && session.strongerUsd >= session.switchUsd) {
1005
+ const models = stronger.several ? 'stronger models' : modelName(stronger.model);
1006
+ out.push(
1007
+ ...dimLines(
1008
+ ui,
1009
+ ` Routing spent more than ${modelName(view.nativeModel)} alone because upper tiers ran on ${models}.`,
1010
+ columns,
1011
+ ),
1012
+ ...dimLines(ui, ' To spend less, pick cheaper models for those tiers in the Routing tab.', columns),
1013
+ );
1014
+ }
1015
+ const shadow = view.activityStats?.shadow;
1016
+ if (config.activityRouting === 'shadow' && shadow?.estimated > 0)
1017
+ out.push(
1018
+ ui.line(
1019
+ ui.dim(' if activity routing were on: '),
1020
+ ui.text(`est. ${signedUsd(shadow.minUsd)} … ${signedUsd(shadow.maxUsd)}`, {
1021
+ color: savingsColor((shadow.minUsd + shadow.maxUsd) / 2),
1022
+ }),
1023
+ ui.dim(' this session'),
1024
+ ),
1025
+ );
1026
+ return out;
1027
+ }
1028
+
1029
+ // The tier whose route is your model at your effort, for its color; else any tier on your model.
1030
+ function yourTier(config, view) {
1031
+ const runs = (tier) => {
1032
+ const route = resolveRoute(config, tier);
1033
+ return (
1034
+ isSameModel(config.models[route.model].id, view.nativeModel) &&
1035
+ routeEffort(config, route, view.nativeEffort) === (view.nativeEffort ?? null)
1036
+ );
1037
+ };
1038
+ return TIERS.find(runs) ?? cellForModel(config, view.nativeModel)?.tier ?? null;
1039
+ }
1040
+
1041
+ // ROUTING VS YOUR MODEL: what routed replies cost at list prices against the same tokens on the session's own model
1042
+ // and effort, this session and since the last reset, split into its three parts; then the two costs as bars and the
1043
+ // replies by tier. Below EARLY_REPLIES replies a column reads "too early"; with routing off or unavailable and no
1044
+ // routed reply, one sentence says so.
1045
+ function savingsSection(ui, config, view, columns) {
1046
+ const name = yourModel(view);
1047
+ const tier = yourTier(config, view);
1048
+ const title = [ui.head('ROUTING VS YOUR MODEL'), ui.dim(' · '), tier ? ui.tier(tier, name) : ui.text(name)];
1049
+ if (!modelSpec(config, view.nativeModel))
1050
+ return [ui.line(...title), ui.dim(` ${modelName(view.nativeModel)} has no list price in router.json models.`)];
1051
+ const totals = view.savings ?? emptyTotals();
1052
+ const session = readout(totals);
1053
+ const store = readSavingsStore(view.savingsStore);
1054
+ let across = store.replies ? readout(store) : null;
1055
+ const same = 'Same tokens on your model';
1056
+ const unavailable = view.phase === 'unavailable';
1057
+ const quiet = (unavailable || view.mode === 'manual') && !session.replies;
1058
+ // The label column fits the longest label shown, two spaces before the amounts.
1059
+ const labels = [` ${same}`, ...(quiet || session.early ? [] : SAVINGS_PARTS)];
1060
+ const widest = Math.max(...labels.map((text) => text.length)) + 2;
1061
+ const titleWidth = 'ROUTING VS YOUR MODEL · '.length + name.length;
1062
+ // The last column needs only its widest value, "−$11.42 −22%".
1063
+ const table = (label) => label + SAVINGS_COLUMN + (across ? 13 : 0);
1064
+ // The column heads share the title's line when it fits; a narrow pane puts them on their own and drops the bars,
1065
+ // and a narrower one the saved column too.
1066
+ let label = Math.max(titleWidth + 3, widest);
1067
+ const narrow = Number.isFinite(columns) && table(label) > columns;
1068
+ if (narrow) label = widest;
1069
+ if (narrow && table(label) > columns) across = null;
1070
+ const heads = [
1071
+ ui.cell(SAVINGS_COLUMN, ui.dim('this session')),
1072
+ across ? ui.dim(`since ${sinceDate(store.since)}`) : null,
1073
+ ];
1074
+ const row = (name, mine, saved = null) =>
1075
+ ui.line(ui.cell(label, name), ui.cell(SAVINGS_COLUMN, mine), across && saved !== null ? saved : null);
1076
+ // The saved column adds up sessions that each had their own model.
1077
+ const notes = () => [
1078
+ ui.text(' '),
1079
+ ...(across
1080
+ ? dimLines(ui, ` since ${sinceDate(store.since)}: each session compared with its own model`, columns)
1081
+ : []),
1082
+ ...footer(ui, columns),
1083
+ ];
1084
+ const out = narrow
1085
+ ? [ui.line(...title), ui.line(ui.cell(label, ''), ...heads)]
1086
+ : [ui.line(ui.cell(label, ui.line(...title)), ...heads)];
1087
+ if (quiet) {
1088
+ out.push(
1089
+ ...dimLines(
1090
+ ui,
1091
+ ` Routing is ${unavailable ? 'unavailable' : 'off'}: every reply this session used your model.`,
1092
+ columns,
1093
+ ),
1094
+ row(' Difference', '$0.00', differenceCell(ui, across)),
1095
+ );
1096
+ return [...out, ...notes()];
1097
+ }
1098
+ out.push(
1099
+ row(' Routed replies', usd(session.routedUsd), across ? usd(across.routedUsd) : null),
1100
+ row(` ${same}`, usd(session.yoursUsd), across ? usd(across.yoursUsd) : null),
1101
+ row(ui.text(' Difference', { bold: true }), differenceCell(ui, session), differenceCell(ui, across)),
1102
+ );
1103
+ if (session.early) {
1104
+ out.push(
1105
+ ...dimLines(
1106
+ ui,
1107
+ ` ${session.replies} routed repl${session.replies === 1 ? 'y' : 'ies'} so far; the session figure appears after ${EARLY_REPLIES}`,
1108
+ columns,
1109
+ ),
1110
+ );
1111
+ return [...out, ...notes()];
1112
+ }
1113
+ out.push(...savingsDetail(ui, config, view, { totals, session, across, row, tier, narrow, columns }));
1114
+ return [...out, ...notes()];
1115
+ }
1116
+
1117
+ function usageTab(ui, config, view, usage, actions, _modelOptions, columns) {
789
1118
  const metrics = usageMetrics(config, view, usage);
790
1119
  const history = view.history ?? [];
791
1120
  const tiers = view.tiers ?? [];
@@ -801,6 +1130,8 @@ function usageTab(ui, config, view, usage, actions) {
801
1130
  )
802
1131
  : ui.dim(' no replies yet');
803
1132
  return [
1133
+ ...savingsSection(ui, config, view, columns),
1134
+ ui.text(' '),
804
1135
  ui.head('USAGE · Claude readings'),
805
1136
  ui.line(`Cost ${dollars(metrics.cost)} `, ui.dim('reported by Claude')),
806
1137
  ui.gauge('Context ', metrics.contextBar, contextSuffix(metrics)),
@@ -815,7 +1146,7 @@ function usageTab(ui, config, view, usage, actions) {
815
1146
  .filter((limit) => Number.isFinite(limit.percentUsed))
816
1147
  .map((limit) => ui.gauge(`${limit.kind.replaceAll('_', ' ').padEnd(11)}`, bar(limit.percentUsed, 100), 'used')),
817
1148
  ui.text(' '),
818
- ...activityUsage(ui, config, view, actions),
1149
+ ...activityUsage(ui, config, view, actions, columns),
819
1150
  ui.text(' '),
820
1151
  ui.head('ESTIMATES · configured list prices'),
821
1152
  ui.text(`Cache read benefit ${dollars(metrics.cacheBenefit)}`),
@@ -841,7 +1172,8 @@ function helpLines(ui) {
841
1172
  ui.head('ABOUT THESE NUMBERS'),
842
1173
  ui.dim('Cost, context and cache are Claude readings. $ estimates use the'),
843
1174
  ui.dim('list prices in router.json; plan prices are equivalents, not cash.'),
844
- ui.dim('Routing savings are not measured. Cache benefit is before writes.'),
1175
+ ui.dim('Routing vs your model holds tokens and output length the same'),
1176
+ ui.dim('and does not measure answer quality. Cache benefit is before writes.'),
845
1177
  ui.dim('The context bar uses the routed model’s window, 20% in reserve.'),
846
1178
  ui.dim('A switch estimate prices 5m–1h cache writes; minus means cheaper.'),
847
1179
  ];