@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.
@@ -1,11 +1,15 @@
1
- // Activity statistics: counts only, never text. The session reducer feeds the Usage tab; the store reducer feeds the
2
- // counts kept across sessions under `activity:stats:v1`. Pure: state and a turn in, new state out.
1
+ // Activity statistics: counts only, never text. The session reducer feeds the Usage tab; the store reducers feed the
2
+ // counts kept across sessions under `activity:stats:v1` and, per session, under METRICS_PREFIX. Pure: state and a turn
3
+ // in, new state out.
3
4
 
4
5
  import { acceptActivity } from './activity.mjs';
5
6
  import { ACTIVITIES, ACTIVITY_VALUES } from './config.mjs';
6
7
 
7
8
  export const OBSERVED = ['code', 'docs', 'ops', 'read', 'talk'];
8
9
  export const STORE_KEY = 'activity:stats:v1';
10
+ // Each session keeps its own metrics under this prefix and its session id, as savings does (SAVINGS_PREFIX): sessions
11
+ // finishing turns at the same moment do not overwrite each other. The readout is their sum (mergeMetrics).
12
+ export const METRICS_PREFIX = 'activity:metrics:v1:';
9
13
 
10
14
  // The observed buckets each predicted activity allows. A weak oracle: it tells code from ops and explore, and cannot
11
15
  // separate plan from review. That is enough to catch a cheap route on a coding turn.
@@ -46,18 +50,35 @@ function addShadow(shadow, wouldDiffer, range) {
46
50
  };
47
51
  }
48
52
 
53
+ function addRoutes(before, added) {
54
+ const next = { ...before };
55
+ for (const [route, n] of Object.entries(added ?? {})) if (count(n)) next[route] = (next[route] ?? 0) + count(n);
56
+ return next;
57
+ }
58
+
59
+ // The route most of an activity's routed replies went out with, `model@effort`; ties go to the first in name order,
60
+ // so the readout is stable. Null without routed replies.
61
+ export function mostlyOn(counts) {
62
+ const [top] = Object.entries(counts?.routes ?? {}).sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1));
63
+ return top?.[0] ?? null;
64
+ }
65
+
49
66
  export function emptySession() {
50
67
  return {
51
68
  byActivity: {},
52
69
  switches: { tier: 0, activity: 0 },
70
+ lateral: { taken: 0, refused: 0 },
53
71
  agreement: { matched: 0, total: 0 },
54
72
  shadow: emptyShadow(),
55
73
  };
56
74
  }
57
75
 
58
76
  // One finished turn. `activity` is the turn's label (null: none), `answer` the classifier's accepted activity, which
59
- // agreement compares with the tools (null: not counted), `shadow` marks a turn in shadow mode, `wouldDiffer` says
60
- // whether 'on' would have routed it differently (null: no would-route) and `wouldUsd` prices that difference.
77
+ // agreement compares with the tools (null: not counted), `lateral` the turn's lateral outcome (turnActivity, counted
78
+ // outside shadow only), `shadow` marks a turn in shadow mode, `wouldDiffer` says whether 'on' would have routed it
79
+ // differently (null: no would-route) and `wouldUsd` prices that difference. `routedUsd` is the list price of the
80
+ // turn's priced replies (null: none had a price), `pricedRequests` how many of its replies had one, and `routes` counts
81
+ // its routed replies by the `model@effort` they went out with.
61
82
  export function recordTurn(
62
83
  session,
63
84
  {
@@ -67,8 +88,12 @@ export function recordTurn(
67
88
  requests,
68
89
  inputTokens,
69
90
  outputTokens,
91
+ routedUsd = null,
92
+ pricedRequests = 0,
93
+ routes = {},
70
94
  tierSwitches,
71
95
  activitySwitches,
96
+ lateral,
72
97
  wouldDiffer,
73
98
  wouldUsd,
74
99
  shadow,
@@ -76,6 +101,8 @@ export function recordTurn(
76
101
  ) {
77
102
  const key = activity ?? 'none';
78
103
  const before = session.byActivity[key] ?? { turns: 0, requests: 0, inputTokens: 0, outputTokens: 0 };
104
+ // A view saved by 1.7.0 has no lateral counts.
105
+ const moves = { taken: 0, refused: 0, ...session.lateral };
79
106
  return {
80
107
  byActivity: {
81
108
  ...session.byActivity,
@@ -84,12 +111,19 @@ export function recordTurn(
84
111
  requests: before.requests + count(requests),
85
112
  inputTokens: before.inputTokens + count(inputTokens),
86
113
  outputTokens: before.outputTokens + count(outputTokens),
114
+ // Counts saved by 1.7.0 have no cost, priced replies or routes: their replies stay unpriced.
115
+ routedUsd: Number.isFinite(routedUsd) ? (before.routedUsd ?? 0) + routedUsd : (before.routedUsd ?? null),
116
+ pricedRequests: (before.pricedRequests ?? 0) + count(pricedRequests),
117
+ routes: addRoutes(before.routes ?? {}, routes),
87
118
  },
88
119
  },
89
120
  switches: {
90
121
  tier: session.switches.tier + count(tierSwitches),
91
122
  activity: session.switches.activity + count(activitySwitches),
92
123
  },
124
+ // In shadow the lateral outcome is the would-route's, which no route followed; the Shadow line covers it.
125
+ lateral:
126
+ !shadow && (lateral === 'taken' || lateral === 'refused') ? { ...moves, [lateral]: moves[lateral] + 1 } : moves,
93
127
  agreement: answer
94
128
  ? {
95
129
  matched: session.agreement.matched + (agrees(answer, observed) ? 1 : 0),
@@ -103,10 +137,33 @@ export function recordTurn(
103
137
  };
104
138
  }
105
139
 
140
+ // Classifier wait per turn, bucketed by these upper bounds in ms, plus one bucket above the last; kept per classifier
141
+ // id for at most LATENCY_CLASSIFIERS ids, so the store stays bounded.
142
+ export const LATENCY_BOUNDS = [
143
+ 100, 150, 200, 250, 300, 350, 400, 450, 500, 600, 700, 800, 900, 1000, 1250, 1500, 2000, 2500, 3000, 4000, 5000,
144
+ ];
145
+ const LATENCY_BUCKETS = LATENCY_BOUNDS.length + 1;
146
+ const LATENCY_CLASSIFIERS = 8;
147
+
106
148
  export function emptyStore() {
107
149
  return { version: 1, confusion: {}, runs: {}, lateral: { taken: 0, refused: 0 }, shadow: emptyShadow() };
108
150
  }
109
151
 
152
+ // The p95 of a latency histogram: the upper bound of its bucket in ms, `over` when it lies above the last bound, and
153
+ // the turns counted. Null without turns.
154
+ export function latencyP95(buckets) {
155
+ const turns = (buckets ?? []).reduce((sum, n) => sum + n, 0);
156
+ if (!turns) return null;
157
+ let seen = 0;
158
+ const at = buckets.findIndex((n) => {
159
+ seen += n;
160
+ return seen >= 0.95 * turns;
161
+ });
162
+ return at < LATENCY_BOUNDS.length
163
+ ? { ms: LATENCY_BOUNDS[at], over: false, turns }
164
+ : { ms: LATENCY_BOUNDS.at(-1), over: true, turns };
165
+ }
166
+
110
167
  const isCount = (n) => Number.isSafeInteger(n) && n >= 0;
111
168
  const isRecord = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
112
169
  const onlyKeys = (v, keys) => isRecord(v) && Object.keys(v).every((k) => keys.includes(k));
@@ -127,7 +184,8 @@ const isShadow = (v) =>
127
184
  Number.isFinite(v.maxUsd));
128
185
 
129
186
  // The stored value if it has a known shape, filled to the current one, else an empty store: a hand-edited or
130
- // unknown file must not break stats.
187
+ // unknown file must not break stats. The shape is 1.7.0's, which rejects any other key: a new count goes under its own
188
+ // key, such as METRICS_KEY, so a downgrade keeps these.
131
189
  export function readStore(value) {
132
190
  const ok =
133
191
  onlyKeys(value, ['version', 'confusion', 'runs', 'lateral', 'shadow']) &&
@@ -167,6 +225,101 @@ export function recordStore(store, { predicted, observed, runEnded, lateral, wou
167
225
  return next;
168
226
  }
169
227
 
228
+ // The readings kept across sessions beside the stats store, one session's or their sum: the classifier wait per
229
+ // classifier id, and the cheaper activity moves taken in `on` with the escalations that followed one, since `since`
230
+ // (ms), the first reading after a reset.
231
+ export function emptyMetrics() {
232
+ return { version: 1, since: null, latency: {}, downMoves: { moves: 0, escalations: 0 } };
233
+ }
234
+
235
+ const isLatency = (v) =>
236
+ isRecord(v) &&
237
+ Object.keys(v).length <= LATENCY_CLASSIFIERS &&
238
+ Object.values(v).every((b) => Array.isArray(b) && b.length === LATENCY_BUCKETS && b.every(isCount));
239
+
240
+ // Each part read on its own: a bad or outdated one, such as latency after LATENCY_BOUNDS changes, reads as empty and
241
+ // leaves the others.
242
+ export function readMetrics(value) {
243
+ const empty = emptyMetrics();
244
+ if (!isRecord(value) || value.version !== 1) return empty;
245
+ return {
246
+ version: 1,
247
+ since: Number.isFinite(value.since) ? value.since : null,
248
+ latency: isLatency(value.latency) ? value.latency : empty.latency,
249
+ downMoves: exactCountsOf(value.downMoves, ['moves', 'escalations']) ? value.downMoves : empty.downMoves,
250
+ };
251
+ }
252
+
253
+ // One finished turn at `now`: `adviceMs` how long `classifier` took (null: not timed), and `downMove` 'moved' or
254
+ // 'escalated' (advanceDown). A turn that adds nothing returns `metrics` itself.
255
+ export function recordMetrics(metrics, { classifier = null, adviceMs = null, downMove = null, now = null }) {
256
+ const next = {
257
+ version: 1,
258
+ since: metrics.since ?? now,
259
+ latency: { ...metrics.latency },
260
+ downMoves: { ...metrics.downMoves },
261
+ };
262
+ let counted = downMove === 'moved' || downMove === 'escalated';
263
+ const known = Object.hasOwn(next.latency, classifier);
264
+ if (
265
+ typeof classifier === 'string' &&
266
+ Number.isFinite(adviceMs) &&
267
+ adviceMs >= 0 &&
268
+ (known || Object.keys(next.latency).length < LATENCY_CLASSIFIERS)
269
+ ) {
270
+ const buckets = known ? [...next.latency[classifier]] : Array(LATENCY_BUCKETS).fill(0);
271
+ const at = LATENCY_BOUNDS.findIndex((bound) => adviceMs <= bound);
272
+ buckets[at === -1 ? LATENCY_BOUNDS.length : at] += 1;
273
+ next.latency[classifier] = buckets;
274
+ counted = true;
275
+ }
276
+ if (!counted) return metrics;
277
+ if (downMove === 'moved') next.downMoves.moves += 1;
278
+ if (downMove === 'escalated') next.downMoves.escalations += 1;
279
+ return next;
280
+ }
281
+
282
+ // A record whose first reading came before the last reset at `resetAt` (ms) reads as empty, as savings' afterReset.
283
+ export function metricsAfterReset(record, resetAt) {
284
+ return Number.isFinite(resetAt) && record.since !== null && record.since < resetAt ? emptyMetrics() : record;
285
+ }
286
+
287
+ // The metrics kept across sessions: every session's record since the last reset added up, since the earliest. Latency
288
+ // keeps the first LATENCY_CLASSIFIERS classifier ids in the order given, so the sum still reads back (readMetrics).
289
+ export function mergeMetrics(values, resetAt = null) {
290
+ const merged = emptyMetrics();
291
+ for (const one of values.map((value) => metricsAfterReset(readMetrics(value), resetAt))) {
292
+ const since = [merged.since, one.since].filter(Number.isFinite);
293
+ merged.since = since.length ? Math.min(...since) : null;
294
+ for (const [id, buckets] of Object.entries(one.latency)) {
295
+ // Own keys only: a classifier id such as `toString` must not read Object.prototype.
296
+ const known = Object.hasOwn(merged.latency, id) ? merged.latency[id] : null;
297
+ if (known) merged.latency[id] = known.map((n, i) => n + buckets[i]);
298
+ else if (Object.keys(merged.latency).length < LATENCY_CLASSIFIERS) merged.latency[id] = [...buckets];
299
+ }
300
+ merged.downMoves = {
301
+ moves: merged.downMoves.moves + one.downMoves.moves,
302
+ escalations: merged.downMoves.escalations + one.downMoves.escalations,
303
+ };
304
+ }
305
+ return merged;
306
+ }
307
+
308
+ // Escalations after a cheaper activity move. `after` is the `model@effort` route a cheaper move went to while the
309
+ // watch lasts, else null; `turn` is the turn's mode, decision reason and lateral outcome, whether a failure asked for
310
+ // an escalation (also one the cash gate held or context-fit replaced), whether it was pinned, and its route. A taken
311
+ // cheaper move in `on` starts the watch; an escalation while it lasts counts once and ends it, as does a turn on
312
+ // another route or a turn in another mode (an `off` turn too, which records nothing else); the hook also ends it when
313
+ // routing is turned off. A pin is the person's choice, not a route change: the watch goes on if the turn after it returns.
314
+ // Returns the watch now and the turn's event: 'moved', 'escalated' or null.
315
+ export function advanceDown(after, { mode, reason, lateral, escalated, pinned, route }) {
316
+ if (mode !== 'on') return { after: null, event: null };
317
+ if (reason === 'activity-down' && lateral === 'taken') return { after: route, event: 'moved' };
318
+ if (!after || pinned) return { after, event: null };
319
+ if (escalated) return { after: null, event: 'escalated' };
320
+ return { after: route === after ? after : null, event: null };
321
+ }
322
+
170
323
  // A run is consecutive turns with the same predicted activity. Given the run so far ({ activity, length } or null)
171
324
  // and this turn's predicted activity (null or 'uncertain' end it), returns the run now and the run that just ended.
172
325
  export function advanceRun(run, predicted) {
@@ -175,21 +328,22 @@ export function advanceRun(run, predicted) {
175
328
  return { run: activity ? { activity, length: 1 } : null, ended: run };
176
329
  }
177
330
 
178
- const LATERAL = { 'activity-up': 'taken', 'activity-down': 'taken', 'activity-pending': 'refused' };
179
331
  const differs = (a, b) => a.model !== b.model || (a.effort ?? null) !== (b.effort ?? null);
332
+ // A decision's lateral outcome: 'taken', 'refused' (not worth it, a hold, the cash gate), or null for none.
333
+ const lateralOf = (decision) =>
334
+ decision?.lateral === 'taken' || decision?.lateral === 'refused' ? decision.lateral : null;
180
335
 
181
336
  // What one routed turn adds to the stats, from its config, the classifier's advice, and the decisions before and
182
337
  // after it. `label` is the turn's activity: the applied one in `on`, the accepted answer in `shadow`; `answer` is the
183
338
  // accepted answer in both, which the tools are checked against and runs follow. `switched` names
184
339
  // what moved the route from the previous turn's: 'activity' for a lateral move, 'tier' for any other, null for none
185
- // or a pin. `lateral` reads the decision that carries an activity, the would-route in `shadow`; a lateral move refused
186
- // by a hold or the cash gate shares its reason with tier decisions and is not counted.
340
+ // or a pin. `lateral` reads the decision that carries an activity, the would-route in `shadow`.
187
341
  export function turnActivity(config, advice, previous, decision) {
188
342
  const mode = config.activityRouting;
189
343
  const would = decision.wouldRoute ?? null;
190
344
  const switched =
191
345
  previous?.model && !decision.pinned && differs(previous, decision)
192
- ? LATERAL[decision.reason] === 'taken'
346
+ ? lateralOf(decision) === 'taken'
193
347
  ? 'activity'
194
348
  : 'tier'
195
349
  : null;
@@ -201,14 +355,14 @@ export function turnActivity(config, advice, previous, decision) {
201
355
  label: mode === 'on' ? (decision.activity ?? null) : mode === 'shadow' ? answer : null,
202
356
  predicted: advice?.activity?.choice ?? null,
203
357
  switched,
204
- lateral: LATERAL[(mode === 'shadow' ? would : mode === 'on' ? decision : null)?.reason] ?? null,
358
+ lateral: lateralOf(mode === 'shadow' ? would : mode === 'on' ? decision : null),
205
359
  // A shadow turn always counts, so the session and store denominators agree.
206
360
  wouldDiffer,
207
361
  wouldUsd: wouldDiffer ? (would.difference ?? null) : null,
208
362
  };
209
363
  }
210
364
 
211
- // The activities plan §11 checks agreement on before activity routing turns on by default.
365
+ // The activities whose agreement plan §11 tracks: the costly mistake is a cheap route on a coding turn.
212
366
  const FOCUS = ['code', 'ops', 'explore'];
213
367
 
214
368
  // The counts kept across sessions as the Usage tab and /router show them. Agreement covers the rows that name an
package/lib/band.mjs CHANGED
@@ -3,21 +3,27 @@ import {
3
3
  classifierLabel,
4
4
  classifierStatus,
5
5
  classifierWarns,
6
- GATEWAY_SETTINGS,
6
+ differenceShare,
7
7
  modelName,
8
8
  NOT_A_TIER_REASON,
9
9
  percent,
10
10
  reasonActivity,
11
11
  routeLabel,
12
+ SAVINGS_COLOR,
12
13
  shortReason,
14
+ signedUsd,
13
15
  switchCount,
14
16
  TIER_COLOR,
17
+ unavailableReason,
15
18
  usageMetrics,
16
19
  } from './display.mjs';
17
20
  import { isSameModel } from './route.mjs';
21
+ import { emptyTotals, readout } from './savings.mjs';
18
22
 
19
23
  const BARS = '▂▄▆█';
20
24
  const SEPARATOR = ' · ';
25
+ // The hover row's controls sit apart without the dot: they are buttons, not readings.
26
+ const GAP = ' ';
21
27
  // Space the band keeps for the trailing Router button.
22
28
  const ROUTER_BUTTON = 8;
23
29
  const STRIP = 20;
@@ -33,15 +39,15 @@ const button = (props) => ({ button: props });
33
39
  // drop from inside a segment it shares no separator with.
34
40
  const segment = (priority, ...parts) => ({ priority, parts: parts.flat().filter(Boolean) });
35
41
  const partWidth = (p) => (p.button ? p.button.label.length + (p.button.plain ? 0 : 4) : p.text.length);
36
- const lineWidth = (segments) =>
37
- segments.reduce((sum, s, i) => sum + (i ? SEPARATOR.length : 0) + s.parts.reduce((n, p) => n + partWidth(p), 0), 0);
42
+ const lineWidth = (segments, separator) =>
43
+ segments.reduce((sum, s, i) => sum + (i ? separator.length : 0) + s.parts.reduce((n, p) => n + partWidth(p), 0), 0);
38
44
 
39
45
  // Drops the highest-priority segments and parts, later ones first on a tie, until the line fits `columns`. If the
40
46
  // priority-0 segments alone are too wide, the later ones go too, and the first is cut with an ellipsis: Ink would
41
47
  // otherwise shrink every Text in the row and garble it.
42
- export function fitSegments(segments, columns) {
48
+ export function fitSegments(segments, columns, separator = SEPARATOR) {
43
49
  const kept = [...segments];
44
- while (lineWidth(kept) > columns) {
50
+ while (lineWidth(kept, separator) > columns) {
45
51
  let drop = null;
46
52
  kept.forEach((s, i) => {
47
53
  s.parts.forEach((p, j) => {
@@ -56,7 +62,7 @@ export function fitSegments(segments, columns) {
56
62
  else if (kept.length > 1) kept.pop();
57
63
  else break;
58
64
  }
59
- const overflow = lineWidth(kept) - columns;
65
+ const overflow = lineWidth(kept, separator) - columns;
60
66
  if (overflow > 0 && kept.length) {
61
67
  const parts = [...kept[0].parts];
62
68
  const last = parts.findLastIndex((p) => !p.button && p.text.length > overflow);
@@ -75,10 +81,6 @@ function meter(tier, dim = false) {
75
81
  // The classifier is working: every bar in the accent colour.
76
82
  const pendingMeter = () => [...BARS].map((bar) => part(bar, { color: COLOR.accent }));
77
83
 
78
- // Turns routing off: the one control in Auto that pauses it.
79
- const routingOffButton = (actions, variant) =>
80
- button({ key: 'band-manual-main', label: 'Routing off', variant, onPress: () => actions.mode('manual') });
81
-
82
84
  const route = (view) => routeLabel(view.actualModel ?? view.selectedModel ?? view.nativeModel, view.effort);
83
85
 
84
86
  // The turn's activity between the tier and the route, in neutral ink so the tier keeps its color; dim and marked in
@@ -105,31 +107,30 @@ export function bandSegments(config, view, usage, actions) {
105
107
  if (view.phase === 'unavailable')
106
108
  return [
107
109
  segment(0, part('✕ Routing unavailable', { color: 'red' })),
108
- segment(
109
- 2,
110
- part(view.error === GATEWAY_SETTINGS ? 'v0.8 gateway settings remain' : (view.error ?? 'unknown error'), {
111
- color: 'red',
112
- }),
113
- ),
110
+ segment(2, part(unavailableReason(view), { color: 'red' })),
114
111
  segment(1, button({ key: 'band-fix', label: 'Fix', onPress: actions.open })),
115
112
  ];
113
+ // The state and Turn on lead in the first segment, which never drops, and a cut keeps buttons whole: the band's one
114
+ // way back on stays at any width. The model and why go first, then the word Routing.
116
115
  if (view.mode === 'manual')
117
116
  return [
118
117
  segment(
119
118
  0,
120
119
  part('○ ', { dimColor: true }),
121
- part('Routing '),
120
+ { ...part('Routing '), priority: 1 },
122
121
  part('off', { backgroundColor: COLOR.off, color: COLOR.muted }),
123
- part(` · every turn uses ${modelName(view.nativeModel)}`),
124
- view.reason === 'model selected manually' ? part(' (/model)', { dimColor: true }) : null,
122
+ part(' '),
123
+ button({ key: 'band-auto', label: 'Turn on', variant: 'primary', onPress: () => actions.mode('auto') }),
125
124
  ),
126
- view.reason === NOT_A_TIER_REASON
127
- ? segment(2, part(`${modelName(view.nativeModel)} is not a routing tier`, { dimColor: true }))
128
- : null,
129
125
  segment(
130
126
  1,
131
- button({ key: 'band-auto', label: 'Routing on', variant: 'primary', onPress: () => actions.mode('auto') }),
127
+ { ...part('every turn uses '), priority: 3 },
128
+ part(modelName(view.nativeModel)),
129
+ view.reason === 'model selected manually' ? { ...part(' (/model)', { dimColor: true }), priority: 2 } : null,
132
130
  ),
131
+ view.reason === NOT_A_TIER_REASON
132
+ ? segment(2, part(`${modelName(view.nativeModel)} is not a routing tier`, { dimColor: true }))
133
+ : null,
133
134
  ].filter(Boolean);
134
135
  if (view.phase === 'choosing')
135
136
  return [
@@ -151,11 +152,7 @@ export function bandSegments(config, view, usage, actions) {
151
152
  )
152
153
  : null;
153
154
  if (!view.tier && view.phase === 'ready' && !view.actualModel)
154
- return [
155
- segment(0, meter(null, true), part(' Routing on · ready', { dimColor: true })),
156
- segment(1, routingOffButton(actions, 'secondary')),
157
- pin,
158
- ].filter(Boolean);
155
+ return [segment(0, meter(null, true), part(' Routing on · ready', { dimColor: true })), pin].filter(Boolean);
159
156
  const main = segment(
160
157
  0,
161
158
  view.tier ? [...meter(view.tier), pill(view.tier, TIER_COLOR[view.tier])] : part('○ '),
@@ -180,7 +177,6 @@ export function bandSegments(config, view, usage, actions) {
180
177
  const support = supportText(config, view.estimate);
181
178
  return [
182
179
  main,
183
- segment(1, routingOffButton(actions, 'secondary')),
184
180
  pin,
185
181
  segment(1, part(shortReason(view.reason, reasonActivity(config, view)), { dimColor: true })),
186
182
  support ? segment(4, part(support, { dimColor: true })) : null,
@@ -190,13 +186,34 @@ export function bandSegments(config, view, usage, actions) {
190
186
  ].filter(Boolean);
191
187
  }
192
188
 
193
- // The second row of the detailed band: recent replies by tier and the session's switch economics.
194
- function detailRow(config, view, usage) {
189
+ // The session against your model, once it is past "too early": "vs your model −22% (−$1.84, list prices)", or the
190
+ // extra with what bought it, "+$2.48 (stronger models, list prices)". Null before that.
191
+ export function savingsParts(view) {
192
+ const r = readout(view.savings ?? emptyTotals());
193
+ if (r.early) return null;
194
+ const share = differenceShare(r);
195
+ const [shown, note] =
196
+ r.differenceUsd < 0
197
+ ? [share ?? signedUsd(r.differenceUsd), `${signedUsd(r.differenceUsd)}, list prices`]
198
+ : r.differenceUsd > 0
199
+ ? [
200
+ signedUsd(r.differenceUsd),
201
+ `${r.strongerUsd >= r.switchUsd ? 'stronger models' : 'switch cache writes'}, list prices`,
202
+ ]
203
+ : ['$0.00', 'list prices'];
204
+ const color = r.differenceUsd < 0 ? SAVINGS_COLOR.less : r.differenceUsd > 0 ? SAVINGS_COLOR.more : undefined;
205
+ return [part('vs your model '), part(shown, color ? { color } : {}), part(` (${note})`, { dimColor: true })];
206
+ }
207
+
208
+ // The second row of the detailed band: recent replies by tier, the session's switch economics, and the session
209
+ // against your model when the row has room for it.
210
+ function detailRow(config, view, usage, columns) {
195
211
  const tiers = (view.tiers ?? []).slice(-STRIP);
196
212
  if (!tiers.length) return [part('no replies yet', { dimColor: true })];
197
- const switches = switchCount(tiers);
213
+ const switches = switchCount((view.routes ?? []).slice(-tiers.length));
198
214
  const metrics = usageMetrics(config, view, usage);
199
- return [
215
+ const savings = savingsParts(view);
216
+ const row = [
200
217
  part('replies ', { dimColor: true }),
201
218
  ...tiers.map((tier) => (tier ? part('█', { color: TIER_COLOR[tier] }) : part('·', { dimColor: true }))),
202
219
  part(
@@ -206,6 +223,35 @@ function detailRow(config, view, usage) {
206
223
  { dimColor: true },
207
224
  ),
208
225
  ];
226
+ const tail = savings ? [part(SEPARATOR, { dimColor: true }), ...savings] : [];
227
+ const width = (parts) => parts.reduce((n, p) => n + partWidth(p), 0);
228
+ return width(row) + width(tail) <= columns ? [...row, ...tail] : row;
229
+ }
230
+
231
+ // The row revealed under the pointer: pins and the mode control while routing is on, and the one- or two-row choice.
232
+ // No digit hotkeys, since a bare digit in an empty prompt would press them. When the row is narrow, the routing on
233
+ // label goes first, then 2 rows, then the pin label, then the pins. Turn off stays, the band's one way to turn
234
+ // routing off, and so does 1 row, the only way back from a second row too wide for the band.
235
+ function hoverSegments(view, actions, routing) {
236
+ const pins = TIERS.flatMap((tier, i) => [
237
+ i ? part(' ') : null,
238
+ button({ key: `band-pin-${tier}`, label: tier, onPress: () => actions.pin(tier) }),
239
+ ]);
240
+ return [
241
+ routing ? segment(1, { ...part('pin next turn ', { dimColor: true }), priority: 2 }, pins) : null,
242
+ // The state, then what a press does. The state goes first: the meter on the row above already says routing is on.
243
+ routing
244
+ ? segment(
245
+ 0,
246
+ { ...part('routing on ', { dimColor: true }), priority: 4 },
247
+ button({ key: 'band-manual', label: 'Turn off', onPress: () => actions.mode('manual') }),
248
+ )
249
+ : null,
250
+ segment(
251
+ view.bandDetail ? 0 : 3,
252
+ button({ key: 'band-detail', label: view.bandDetail ? '1 row' : '2 rows', onPress: actions.toggleDetail }),
253
+ ),
254
+ ].filter(Boolean);
209
255
  }
210
256
 
211
257
  export function renderBand({ Box, Text, Button }, config, view, usage, { columns, agentId }, actions) {
@@ -216,36 +262,17 @@ export function renderBand({ Box, Text, Button }, config, view, usage, { columns
216
262
  const line = kept.flatMap((s, i) => (i ? [part(SEPARATOR, { dimColor: true }), ...s.parts] : s.parts));
217
263
  line.push(part(' '), button({ key: 'details', label: 'Router', plain: true, onPress: actions.open }));
218
264
  const routing = view.mode === 'auto' && view.phase !== 'unavailable';
265
+ const hover = fitSegments(hoverSegments(view, actions, routing), columns, GAP);
219
266
  return Box({
220
267
  key: 'router-band',
221
268
  flexDirection: 'column',
222
269
  children: [
223
270
  row(line),
224
- view.bandDetail && routing ? row(detailRow(config, view, usage)) : null,
225
- // Revealed while the pointer is over the band; no digit hotkeys, since a bare digit in an
226
- // empty prompt would press them.
271
+ view.bandDetail && routing ? row(detailRow(config, view, usage, columns)) : null,
227
272
  Box({
228
273
  display: 'none',
229
274
  hover: { display: 'flex' },
230
- children: [
231
- Text({ dimColor: true, children: routing ? 'pin next turn ' : '' }),
232
- ...(routing
233
- ? TIERS.flatMap((tier) => [
234
- Button({ key: `band-pin-${tier}`, label: tier, onPress: () => actions.pin(tier) }),
235
- Text({ children: ' ' }),
236
- ])
237
- : []),
238
- Text({ children: routing ? ' ' : '' }),
239
- routing
240
- ? Button({ key: 'band-manual', label: 'Routing off', onPress: () => actions.mode('manual') })
241
- : Button({ key: 'band-auto-row', label: 'Routing on', onPress: () => actions.mode('auto') }),
242
- Text({ children: ' ' }),
243
- Button({
244
- key: 'band-detail',
245
- label: view.bandDetail ? '1 row' : '2 rows',
246
- onPress: actions.toggleDetail,
247
- }),
248
- ],
275
+ children: hover.flatMap((s, i) => (i ? [part(GAP), ...s.parts] : s.parts)).map(draw),
249
276
  }),
250
277
  ],
251
278
  });