@alexeiled/claude-router 1.5.1 → 1.6.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,22 @@
1
- import { activeClassifier, effectiveRoutes, sameRoute, TIERS, tuningOf } from './config.mjs';
1
+ import { compareRoutes } from './activity.mjs';
2
+ import { readStore, storeSummary } from './activity-stats.mjs';
2
3
  import {
4
+ ACTIVITIES,
5
+ ACTIVITY_MODES,
6
+ activeClassifier,
7
+ DEFAULTS,
8
+ effectiveActivities,
9
+ effectiveRoutes,
10
+ resolveRoute,
11
+ sameCell,
12
+ sameRoute,
13
+ TIERS,
14
+ tuningOf,
15
+ } from './config.mjs';
16
+ import {
17
+ ACTIVITY_LETTER,
18
+ activityReading,
19
+ activityRouteText,
3
20
  bar,
4
21
  classifierLabel,
5
22
  classifierStatus,
@@ -13,6 +30,7 @@ import {
13
30
  percent,
14
31
  REASONS,
15
32
  routeLabel,
33
+ routeName,
16
34
  sparkline,
17
35
  switchCount,
18
36
  TIER_COLOR,
@@ -35,15 +53,22 @@ const TUNING_CHOICES = {
35
53
  const LADDER = [...TIERS].reverse();
36
54
  const SESSION_EFFORT = 'session';
37
55
 
38
- function routeName(config, route) {
39
- const model = config.models[route.model];
40
- return `${modelName(model.id)}${model.efforts.length ? ` · ${route.effort ?? SESSION_EFFORT}` : ''}`;
41
- }
56
+ 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',
60
+ };
61
+ const EFFORT_SHORT = { low: 'low', medium: 'med', high: 'high', xhigh: 'xh', max: 'max' };
42
62
 
43
- // The route draft the Routing tab edits: the saved routes until the person changes one.
63
+ // The route draft the Routing tab edits: the saved routes, activity overrides and mode until the person changes one.
44
64
  export function routeDraftOf(config, view) {
45
65
  if (view.routeDraft) return view.routeDraft;
46
- const base = structuredClone({ routes: config.routes, baselineTier: config.baselineTier });
66
+ const base = structuredClone({
67
+ routes: config.routes,
68
+ baselineTier: config.baselineTier,
69
+ activities: config.activities,
70
+ activityRouting: config.activityRouting,
71
+ });
47
72
  return { ...structuredClone(base), base };
48
73
  }
49
74
 
@@ -163,15 +188,25 @@ function nowTab(ui, config, view, usage, actions) {
163
188
  const label = classifierLabel(config);
164
189
  const missing = missingCredentials(config, view, config.classifier);
165
190
  const served = view.actualModel && current && !isSameModel(current, view.actualModel);
191
+ const reading = config.activityRouting === 'off' ? null : activityReading(view);
192
+ const route = activityRouteText(config, view);
166
193
  const out = [
167
- ui.line('Now ', view.tier ? ui.tier(view.tier, `▌${view.tier} `) : null, routeLabel(current, view.effort)),
168
- ui.line('Why ', REASONS[view.reason] ?? view.reason ?? 'ready'),
194
+ ui.line('Now ', view.tier ? ui.tier(view.tier, `▌${view.tier} `) : null, routeLabel(current, view.effort)),
195
+ reading
196
+ ? ui.line(
197
+ 'Activity ',
198
+ `${reading.choice}${reading.share ? ` ${reading.share}` : ''}`,
199
+ reading.others.length ? ui.dim(` ${reading.others.join(' · ')}`) : null,
200
+ )
201
+ : null,
202
+ ui.line('Why ', REASONS[view.reason] ?? view.reason ?? 'ready'),
203
+ route ? ui.line('Route ', route) : null,
169
204
  served
170
- ? ui.line(ui.text('Served ', { color: 'yellow' }), `${modelName(view.actualModel)} · native fallback`)
205
+ ? ui.line(ui.text('Served ', { color: 'yellow' }), `${modelName(view.actualModel)} · native fallback`)
171
206
  : null,
172
207
  view.pendingPin
173
208
  ? ui.line(
174
- ui.text('Pinned ', { color: 'yellow' }),
209
+ ui.text('Pinned ', { color: 'yellow' }),
175
210
  'next turn → ',
176
211
  ui.tier(view.pendingPin),
177
212
  ' ',
@@ -236,10 +271,13 @@ function nowTab(ui, config, view, usage, actions) {
236
271
  }
237
272
 
238
273
  // One cell per reply, colored by the tier that served it; a dot where routing did not choose (routing off, fallback).
274
+ // Under it, the reply's activity letter, aligned from the end: a view from before 1.6 has fewer activities.
239
275
  function replies(ui, view) {
240
276
  const tiers = view.tiers ?? [];
241
277
  if (!tiers.length) return [ui.head('REPLIES'), ui.dim(' no replies yet')];
242
278
  const switches = switchCount(tiers);
279
+ const activities = view.activities ?? [];
280
+ const letters = tiers.map((_, i) => activities[activities.length - tiers.length + i] ?? null);
243
281
  return [
244
282
  ui.head(`REPLIES · last ${tiers.length}`),
245
283
  ui.line(
@@ -247,7 +285,13 @@ function replies(ui, view) {
247
285
  ...tiers.map((tier) => (tier ? ui.tier(tier, '█') : ui.dim('·'))),
248
286
  ui.dim(` ${switches} switch${switches === 1 ? '' : 'es'}`),
249
287
  ),
288
+ ...(letters.some(Boolean)
289
+ ? [ui.line(' ', ...letters.map((activity) => (activity ? ui.text(ACTIVITY_LETTER[activity]) : ui.dim('·'))))]
290
+ : []),
250
291
  ui.line(' ', ...TIERS.flatMap((tier) => [ui.tier(tier, '■ '), `${tier} `])),
292
+ ...(letters.some(Boolean)
293
+ ? [ui.dim(` ${ACTIVITIES.map((activity) => `${ACTIVITY_LETTER[activity]} ${activity}`).join(' ')}`)]
294
+ : []),
251
295
  ];
252
296
  }
253
297
 
@@ -259,19 +303,27 @@ const POLICY_ROWS = [
259
303
  ['cashCapUsd', 'Credits cap', (v) => `$${v.toFixed(2)}`, 'max cold write on credits', 'policy.cashCapUsd'],
260
304
  ];
261
305
 
262
- // What the Routing tab changed and has not saved: tiers and baseline against the saved routes, policy values against
263
- // the values the draft started from.
306
+ // What the Routing tab changed and has not saved: tiers, baseline, activity cells and mode against the saved config,
307
+ // policy values against the values the draft started from. `cells` lists [activity, tier] pairs.
264
308
  export function routingChanges(config, view) {
265
- const draft = effectiveRoutes(routeDraftOf(config, view), config);
309
+ const raw = routeDraftOf(config, view);
310
+ const draft = { ...effectiveRoutes(raw, config), ...effectiveActivities(raw, config) };
266
311
  const saved = tuningOf(config);
267
312
  const base = view.tuningBase ?? saved;
268
313
  const tuning = { ...saved, ...view.tuning };
269
314
  const tiers = TIERS.filter((tier) => !sameRoute(draft.routes[tier], config.routes[tier]));
270
315
  const baseline = draft.baselineTier !== config.baselineTier;
316
+ const cells = ACTIVITIES.flatMap((activity) =>
317
+ TIERS.filter((tier) => !sameCell(draft.activities[activity]?.[tier], config.activities[activity]?.[tier])).map(
318
+ (tier) => [activity, tier],
319
+ ),
320
+ );
321
+ const mode = draft.activityRouting !== config.activityRouting;
271
322
  const policy = POLICY_ROWS.map(([key]) => key).filter(
272
323
  (key) => Object.hasOwn(view.tuning ?? {}, key) && view.tuning[key] !== base[key],
273
324
  );
274
- return { draft, saved, tuning, tiers, baseline, policy, count: tiers.length + Number(baseline) + policy.length };
325
+ const count = tiers.length + Number(baseline) + cells.length + Number(mode) + policy.length;
326
+ return { draft, saved, tuning, tiers, baseline, cells, mode, policy, count };
275
327
  }
276
328
 
277
329
  const marker = (ui, changed) => ui.cell(2, changed ? ui.text('●', { color: 'yellow' }) : '');
@@ -279,7 +331,8 @@ const marker = (ui, changed) => ui.cell(2, changed ? ui.text('●', { color: 'ye
279
331
  // Routes and policy are edited as one set: the switch-cost lines read the whole ladder, so nothing here is written
280
332
  // until Save in the status bar.
281
333
  function routingTab(ui, config, view, _usage, actions, modelOptions) {
282
- const { draft, tuning, tiers, baseline, policy } = routingChanges(config, view);
334
+ const changes = routingChanges(config, view);
335
+ const { draft, tuning, tiers, baseline, policy } = changes;
283
336
  const out = [
284
337
  ui.line(ui.head('ROUTES'), ui.dim(' · applies next turn · edit, then Save')),
285
338
  ui.dim(`${'tier'.padEnd(8)}${'model'.padEnd(14)}${'effort'.padEnd(14)}${'$/M in · out'.padEnd(14)}window`),
@@ -340,21 +393,12 @@ function routingTab(ui, config, view, _usage, actions, modelOptions) {
340
393
  ),
341
394
  ui.text(' '),
342
395
  ui.head('SWITCH COST · from these routes'),
343
- );
344
- for (let i = 0; i < LADDER.length - 1; i += 1) {
345
- const upper = draft.routes[LADDER[i]];
346
- const lower = draft.routes[LADDER[i + 1]];
347
- const pair = `${LADDER[i + 1]} → ${LADDER[i]}`.padEnd(16);
348
- if (sameRoute(upper, lower))
349
- out.push(ui.line(ui.text('! ', { color: 'yellow' }), pair, 'identical: this step changes nothing'));
350
- else if (upper.model === lower.model)
351
- out.push(ui.line(ui.dim('· '), pair, 'same model, new effort: priced as a new cache'));
352
- else out.push(ui.line(ui.dim('· '), pair, 'model change: priced as a cold cache write'));
353
- }
354
- out.push(
396
+ ...switchCost(ui, draft.routes),
355
397
  ui.Button({ key: 'reset-routes', label: 'Reset routes to defaults', hotkey: 'r', onPress: actions.resetRoutes }),
356
398
  ui.dim('A new model ID needs router.json → models: price, window, efforts.'),
357
399
  ui.text(' '),
400
+ ...activitySections(ui, config, changes, actions, modelOptions),
401
+ ui.text(' '),
358
402
  ui.line(ui.head('POLICY'), ui.dim(' · applies next turn · edit, then Save')),
359
403
  ...POLICY_ROWS.map(([key, label, format, hint]) =>
360
404
  ui.line(
@@ -379,6 +423,162 @@ function routingTab(ui, config, view, _usage, actions, modelOptions) {
379
423
  return out;
380
424
  }
381
425
 
426
+ function switchCost(ui, routes) {
427
+ return LADDER.slice(0, -1).map((upperTier, i) => {
428
+ const upper = routes[upperTier];
429
+ const lower = routes[LADDER[i + 1]];
430
+ const pair = `${LADDER[i + 1]} → ${upperTier}`.padEnd(16);
431
+ if (sameRoute(upper, lower))
432
+ return ui.line(ui.text('! ', { color: 'yellow' }), pair, 'identical: this step changes nothing');
433
+ if (upper.model === lower.model)
434
+ return ui.line(ui.dim('· '), pair, 'same model, new effort: priced as a new cache');
435
+ return ui.line(ui.dim('· '), pair, 'model change: priced as a cold cache write');
436
+ });
437
+ }
438
+
439
+ // "S·med": the model's initial and a short effort, for the matrix.
440
+ function shortRoute(config, route) {
441
+ const model = config.models[route.model];
442
+ const effort = model.efforts.length ? `·${EFFORT_SHORT[route.effort] ?? 'ses'}` : '';
443
+ return `${modelName(model.id)[0]}${effort}`;
444
+ }
445
+ const routeKey = (route) => `${route.model}@${route.effort ?? ''}`;
446
+
447
+ // Why an override may be a mistake: no effect, a route above the next tier's, or a pair no other cell runs.
448
+ function overrideWarnings(config, uses, activity, tier) {
449
+ const route = resolveRoute(config, tier, activity);
450
+ if (sameRoute(route, config.routes[tier])) return ['same as base: no effect'];
451
+ const above = config.routes[TIERS[TIERS.indexOf(tier) + 1]];
452
+ return [
453
+ above && compareRoutes(config, route, above) > 0 ? 'stronger than the tier above' : null,
454
+ uses.get(routeKey(route)) === 1 ? 'new (model, effort) pair: one more cache' : null,
455
+ ].filter(Boolean);
456
+ }
457
+
458
+ // 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) {
460
+ const draft = { ...config, ...changes.draft };
461
+ const mode = draft.activityRouting;
462
+ const cellRoutes = ACTIVITIES.flatMap((activity) => TIERS.map((tier) => resolveRoute(draft, tier, activity)));
463
+ const uses = new Map();
464
+ for (const route of [...TIERS.map((tier) => draft.routes[tier]), ...cellRoutes])
465
+ uses.set(routeKey(route), (uses.get(routeKey(route)) ?? 0) + 1);
466
+ // Activities with the same row share it, so the matrix stays as short as the overrides are few.
467
+ const rows = new Map();
468
+ for (const activity of ACTIVITIES) {
469
+ const cells = TIERS.map((tier) => {
470
+ const route = resolveRoute(draft, tier, activity);
471
+ return sameRoute(route, draft.routes[tier]) ? '·' : shortRoute(draft, route);
472
+ });
473
+ const row = rows.get(cells.join()) ?? { names: [], cells };
474
+ rows.set(cells.join(), { ...row, names: [...row.names, activity] });
475
+ }
476
+ const distinct = uses.size;
477
+ const matrixRow = (name, cells) => ui.text(` ${name.padEnd(30)}${cells.map((c) => c.padEnd(9)).join('')}`);
478
+ 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')),
491
+ ui.line(
492
+ ui.cell(
493
+ 26,
494
+ ui.Select
495
+ ? ui.Select({
496
+ key: 'activity-mode',
497
+ label: 'Activity routing',
498
+ value: mode,
499
+ options: ACTIVITY_MODES.map((m) => ({ value: m, label: m })),
500
+ onSelect: (m) => actions.activityMode(m),
501
+ })
502
+ : `Activity routing: ${mode}`,
503
+ ),
504
+ marker(ui, changes.mode),
505
+ ui.dim(ACTIVITY_MODE_HINT[mode]),
506
+ ),
507
+ ];
508
+ const free = [];
509
+ for (const activity of ACTIVITIES) {
510
+ for (const tier of TIERS) {
511
+ const edited = changes.cells.some(([a, t]) => a === activity && t === tier);
512
+ const route = resolveRoute(draft, tier, activity);
513
+ // A built-in override set to the tier's route is how a removal is saved: it lists as free, not as no effect.
514
+ const removed =
515
+ sameRoute(route, draft.routes[tier]) && DEFAULTS.activities[activity]?.[tier] !== undefined && !edited;
516
+ if (!draft.activities[activity]?.[tier] || removed) {
517
+ free.push([activity, tier]);
518
+ continue;
519
+ }
520
+ out.push(overrideRow(ui, draft, route, activity, tier, edited, actions, modelOptions, uses));
521
+ }
522
+ }
523
+ if (ui.Select && free.length)
524
+ out.push(
525
+ ui.Select({
526
+ key: 'activity-add',
527
+ value: '',
528
+ options: [
529
+ { value: '', label: 'add an override…' },
530
+ ...free.map(([activity, tier]) => ({ value: `${activity}.${tier}`, label: `${activity} at ${tier}` })),
531
+ ],
532
+ onSelect: (picked) => {
533
+ const [activity, tier] = picked.split('.');
534
+ return tier ? actions.addActivity(activity, tier) : undefined;
535
+ },
536
+ }),
537
+ );
538
+ return out;
539
+ }
540
+
541
+ function overrideRow(ui, draft, route, activity, tier, edited, actions, modelOptions, uses) {
542
+ const model = draft.models[route.model];
543
+ const aliases = modelOptions.includes(route.model) ? modelOptions : [route.model, ...modelOptions];
544
+ const warnings = overrideWarnings(draft, uses, activity, tier);
545
+ return ui.line(
546
+ ui.cell(10, activity),
547
+ ui.cell(8, ui.tier(tier)),
548
+ ui.cell(
549
+ 14,
550
+ ui.Select
551
+ ? ui.Select({
552
+ key: `activity-model-${activity}-${tier}`,
553
+ value: route.model,
554
+ options: aliases.map((alias) => ({ value: alias, label: modelName(draft.models[alias].id) })),
555
+ onSelect: (alias) => actions.activityModel(activity, tier, alias),
556
+ })
557
+ : modelName(model.id),
558
+ ),
559
+ ui.cell(
560
+ 12,
561
+ !model.efforts.length
562
+ ? ui.dim('none')
563
+ : ui.Select
564
+ ? ui.Select({
565
+ key: `activity-effort-${activity}-${tier}`,
566
+ value: route.effort ?? SESSION_EFFORT,
567
+ options: [SESSION_EFFORT, ...model.efforts].map((effort) => ({ value: effort, label: effort })),
568
+ onSelect: (effort) => actions.activityEffort(activity, tier, effort === SESSION_EFFORT ? null : effort),
569
+ })
570
+ : (route.effort ?? SESSION_EFFORT),
571
+ ),
572
+ marker(ui, edited),
573
+ ui.Button({
574
+ key: `activity-remove-${activity}-${tier}`,
575
+ label: 'remove',
576
+ onPress: () => actions.removeActivity(activity, tier),
577
+ }),
578
+ warnings.length ? ui.text(` ${warnings.join(' · ')}`, { color: 'yellow' }) : null,
579
+ );
580
+ }
581
+
382
582
  // A Select over TUNING_CHOICES[key], with a saved value outside them kept as the first option.
383
583
  function choice(ui, key, value, format, pick) {
384
584
  if (!ui.Select) return format(value);
@@ -467,6 +667,14 @@ function statusBar(ui, config, view, actions) {
467
667
  ),
468
668
  );
469
669
  if (changes.baseline) out.push(...diff('baselineTier', config.baselineTier, changes.draft.baselineTier));
670
+ const draft = { ...config, ...changes.draft };
671
+ const cellName = (c, activity, tier) =>
672
+ c.activities[activity]?.[tier] ? routeName(c, resolveRoute(c, tier, activity)) : 'no override';
673
+ for (const [activity, tier] of changes.cells)
674
+ out.push(
675
+ ...diff(`activities.${activity}.${tier}`, cellName(config, activity, tier), cellName(draft, activity, tier)),
676
+ );
677
+ if (changes.mode) out.push(...diff('activityRouting', config.activityRouting, changes.draft.activityRouting));
470
678
  for (const [key, , format, , field] of POLICY_ROWS.filter(([key]) => changes.policy.includes(key)))
471
679
  out.push(...diff(field, format(changes.saved[key]), format(changes.tuning[key])));
472
680
  out.push(
@@ -493,7 +701,91 @@ function statusBar(ui, config, view, actions) {
493
701
  return out;
494
702
  }
495
703
 
496
- function usageTab(ui, config, view, usage) {
704
+ // `on` would route N of M shadow turns differently, with the summed next-request range of the differing turns that had
705
+ // one, at configured list prices; `for K` says when only some had one.
706
+ function shadowLine(shadow) {
707
+ const counted = `Shadow on would route ${shadow.differs} of ${shadow.turns} turns differently`;
708
+ if (!(shadow.estimated > 0)) return counted;
709
+ const some = shadow.estimated < shadow.differs ? ` for ${shadow.estimated}` : '';
710
+ return `${counted} · est. ${difference(shadow.minUsd)} … ${difference(shadow.maxUsd)}${some} at list prices`;
711
+ }
712
+
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) {
716
+ const summary = storeSummary(readStore(store));
717
+ const out = [ui.head('ACROSS SESSIONS · since the last reset')];
718
+ if (summary.labelled === 0) return [...out, ui.dim(' no turns recorded yet')];
719
+ out.push(ui.text(`Labelled ${summary.labelled} turns`));
720
+ const { matched, total } = summary.agreement;
721
+ if (total > 0)
722
+ out.push(ui.text(`Agreement classifier vs tools: ${matched} of ${total} turns (${percent(matched / total)})`));
723
+ const { focus } = summary;
724
+ if (focus.total > 0)
725
+ out.push(
726
+ ui.text(
727
+ `Code/ops/explore classifier vs tools: ${focus.matched} of ${focus.total} turns (${percent(focus.matched / focus.total)})`,
728
+ ),
729
+ );
730
+ if (summary.disagreements.length)
731
+ out.push(
732
+ ui.text(`Mismatch ${summary.disagreements.map((d) => `${d.predicted} → ${d.observed} ${d.n}`).join(' · ')}`),
733
+ );
734
+ const { taken, refused } = summary.lateral;
735
+ if (taken + refused > 0) out.push(ui.text(`Lateral ${taken} taken · ${refused} refused`));
736
+ if (summary.shadow.turns > 0) out.push(ui.text(shadowLine(summary.shadow)));
737
+ return out;
738
+ }
739
+
740
+ // Session counts by activity, the switches by cause, the tool agreement and the shadow readout, then the counts kept
741
+ // 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) {
743
+ const stats = view.activityStats ?? null;
744
+ 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)];
751
+ if (mode === 'off' && !stats)
752
+ return [
753
+ ui.head('ACTIVITY · routing off'),
754
+ ui.dim(' Not asked. Set activity routing to shadow or on in the Routing tab.'),
755
+ ...(storeSummary(readStore(view.activityStore)).labelled > 0 ? across : []),
756
+ reset,
757
+ ];
758
+ const out = [ui.line(ui.head('ACTIVITY · this session'), ui.dim(` routing ${mode}`))];
759
+ const rows = Object.entries(stats?.byActivity ?? {}).filter(([, counts]) => counts?.turns > 0);
760
+ if (!rows.length) out.push(ui.dim(' no activity readings yet'));
761
+ else {
762
+ const total = rows.reduce((sum, [, counts]) => sum + counts.turns, 0);
763
+ out.push(
764
+ ui.dim(` ${''.padEnd(9)}${''.padEnd(10)}${'turns'.padStart(7)}${'requests'.padStart(10)}${'share'.padStart(7)}`),
765
+ );
766
+ for (const [name, counts] of rows.sort((a, b) => b[1].turns - a[1].turns)) {
767
+ const share = counts.turns / total;
768
+ out.push(
769
+ ui.line(
770
+ ` ${name.padEnd(9)}`,
771
+ ui.text(bar(share, 1, 10).text, { color: 'cyan' }),
772
+ `${String(counts.turns).padStart(7)}${String(counts.requests).padStart(10)}${percent(share).padStart(7)}`,
773
+ ),
774
+ );
775
+ }
776
+ }
777
+ if (stats) {
778
+ const { tier, activity } = stats.switches;
779
+ out.push(ui.text(`Switches ${tier + activity} · ${tier} by tier · ${activity} by activity`));
780
+ const { matched, total } = stats.agreement;
781
+ if (total > 0)
782
+ out.push(ui.text(`Agreement classifier vs tools: ${matched} of ${total} turns (${percent(matched / total)})`));
783
+ if (mode === 'shadow' || stats.shadow.turns > 0) out.push(ui.text(shadowLine(stats.shadow)));
784
+ }
785
+ return [...out, ...across, reset];
786
+ }
787
+
788
+ function usageTab(ui, config, view, usage, actions) {
497
789
  const metrics = usageMetrics(config, view, usage);
498
790
  const history = view.history ?? [];
499
791
  const tiers = view.tiers ?? [];
@@ -523,6 +815,8 @@ function usageTab(ui, config, view, usage) {
523
815
  .filter((limit) => Number.isFinite(limit.percentUsed))
524
816
  .map((limit) => ui.gauge(`${limit.kind.replaceAll('_', ' ').padEnd(11)}`, bar(limit.percentUsed, 100), 'used')),
525
817
  ui.text(' '),
818
+ ...activityUsage(ui, config, view, actions),
819
+ ui.text(' '),
526
820
  ui.head('ESTIMATES · configured list prices'),
527
821
  ui.text(`Cache read benefit ${dollars(metrics.cacheBenefit)}`),
528
822
  Number.isFinite(view.estimate?.taxUsd) ? ui.text(`Switch tax ${dollars(view.estimate.taxUsd)}`) : null,
package/lib/policy.mjs CHANGED
@@ -1,23 +1,38 @@
1
1
  // Switching policy v0 (docs/architecture.md): stickiness, escalation floor, cost-gated votes.
2
- import { rank, routeModel, TIERS } from './config.mjs';
2
+ import { rank, resolveRoute, routeSpec, TIERS } from './config.mjs';
3
3
  import * as nativeCosts from './cost.mjs';
4
4
 
5
5
  export function initialState() {
6
6
  return { turn: 0, votes: [], holdUntilTurn: 0, escalatedSignature: null };
7
7
  }
8
8
 
9
- // Returns { tier, reason, state, estimate }. `baseline` is the tier used when nothing argues otherwise.
10
- export function decide({ config, facts, advice, state, baseline, now, costs = nativeCosts }) {
9
+ // Returns { tier, reason, state, estimate }. `baseline` is the tier used when nothing argues otherwise. Tiers rank the
10
+ // choice; prices come from routes: each candidate tier at `routeFor(tier)`, the incumbent at the route it runs on.
11
+ // `escalateTo(incumbent)` names the tier repeated failures move to, or null when no tier helps: then the failure
12
+ // signature is not consumed.
13
+ export function decide({
14
+ config,
15
+ facts,
16
+ advice,
17
+ state,
18
+ baseline,
19
+ now,
20
+ costs = nativeCosts,
21
+ routeFor = (tier) => resolveRoute(config, tier),
22
+ incumbentRoute = null,
23
+ escalateTo = (tier) => TIERS[rank(tier) + 1] ?? null,
24
+ }) {
11
25
  const next = { ...state, turn: state.turn + 1, votes: [...state.votes] };
12
26
  const incumbent = facts.lastRoute && TIERS.includes(facts.lastRoute) ? facts.lastRoute : baseline;
27
+ const current = incumbentRoute ?? routeFor(incumbent);
13
28
  const stay = (reason, estimate = null) => result(incumbent, reason, next, estimate);
14
29
 
15
- if (facts.failure && facts.failure.signature !== next.escalatedSignature && rank(incumbent) < TIERS.length - 1) {
30
+ const target = facts.failure && facts.failure.signature !== next.escalatedSignature ? escalateTo(incumbent) : null;
31
+ if (target) {
16
32
  next.escalatedSignature = facts.failure.signature;
17
33
  next.holdUntilTurn = next.turn + config.policy.escalationHoldTurns;
18
34
  next.votes = [];
19
- const target = TIERS[rank(incumbent) + 1];
20
- const gated = cashGate(config, target, facts, now, costs);
35
+ const gated = cashGate(config, routeFor(target), facts, now, costs, routeFor);
21
36
  return gated.blocked ? gatedResult(gated, incumbent, next) : result(target, 'escalation', next);
22
37
  }
23
38
  if (next.turn <= next.holdUntilTurn) return stay('hold');
@@ -28,22 +43,27 @@ export function decide({ config, facts, advice, state, baseline, now, costs = na
28
43
  const choice = advice.choice;
29
44
  next.votes = [...next.votes.slice(-2), { tier: choice, turn: next.turn }];
30
45
  if (rank(choice) === rank(incumbent)) return stay('same-tier');
46
+ // Before the first measured reply of a history there is no cache it built, so staying protects nothing and one
47
+ // vote is enough. Facts without the field keep the streak.
48
+ const fresh = facts.historyMeasured === false;
49
+ const upgradeVotes = fresh ? 1 : config.policy.upgradeVotes;
50
+ const downgradeVotes = fresh ? 1 : config.policy.downgradeVotes;
31
51
 
32
52
  if (rank(choice) > rank(incumbent)) {
33
53
  const upgrade = massAbove(advice.probabilities, incumbent);
34
- const gated = cashGate(config, choice, facts, now, costs);
54
+ const gated = cashGate(config, routeFor(choice), facts, now, costs, routeFor);
35
55
  if (gated.blocked) return gatedResult(gated, incumbent, next);
36
- const tax = Math.max(0, costs.switchingTaxUsd(config, choice, incumbent, facts, now));
56
+ const tax = Math.max(0, costs.switchingTaxUsd(config, routeFor(choice), current, facts, now));
37
57
  const threshold =
38
58
  config.policy.upgradeBase + config.policy.upgradeSlope * (tax / (tax + config.policy.upgradePivotUsd));
39
59
  const jump = rank(choice) - rank(incumbent) >= 2 && upgrade >= config.policy.jumpConfidence;
40
60
  const streak = trailing(next.votes, (v) => rank(v.tier) > rank(incumbent));
41
61
  const cache = {
42
- candidate: cacheOf(config, choice, facts, now, costs),
43
- incumbent: cacheOf(config, incumbent, facts, now, costs),
62
+ candidate: cacheOf(config, routeFor(choice), facts, now, costs),
63
+ incumbent: cacheOf(config, current, facts, now, costs),
44
64
  };
45
65
  const estimate = { taxUsd: tax, threshold, upgradeMass: upgrade, streak, cache };
46
- if (jump || (streak >= config.policy.upgradeVotes && upgrade >= threshold))
66
+ if (jump || (streak >= upgradeVotes && upgrade >= threshold))
47
67
  return result(choice, jump ? 'jump' : 'upgrade', next, estimate);
48
68
  return stay('upgrade-pending', estimate);
49
69
  }
@@ -52,12 +72,12 @@ export function decide({ config, facts, advice, state, baseline, now, costs = na
52
72
  const streak = trailing(next.votes, (v) => rank(v.tier) <= rank(choice));
53
73
  // A downgrade is not always cheaper this turn: a cold candidate next to a warm incumbent can pay a bigger cache
54
74
  // write than it saves. The bar rises with that cost, net of what the next turns save, the way the upgrade bar does.
55
- const tax = costs.downgradeTaxUsd(config, choice, incumbent, facts, now, config.policy.downgradeHorizonTurns);
75
+ const tax = costs.downgradeTaxUsd(config, routeFor(choice), current, facts, now, config.policy.downgradeHorizonTurns);
56
76
  const threshold =
57
77
  config.policy.downgradeMass + config.policy.downgradeSlope * (tax / (tax + config.policy.downgradePivotUsd));
58
78
  const estimate = { downgradeMass: support, streak, taxUsd: tax, threshold };
59
- if (support >= threshold && streak >= config.policy.downgradeVotes) {
60
- const gated = cashGate(config, choice, facts, now, costs);
79
+ if (support >= threshold && streak >= downgradeVotes) {
80
+ const gated = cashGate(config, routeFor(choice), facts, now, costs, routeFor);
61
81
  return gated.blocked ? gatedResult(gated, incumbent, next) : result(choice, 'downgrade', next, estimate);
62
82
  }
63
83
  return stay('downgrade-pending', estimate);
@@ -69,8 +89,8 @@ export const CONTEXT_FILL = 0.8;
69
89
 
70
90
  // The lowest tier at or above `tier` whose model fits `tokens` of context. Routes need not grow in window
71
91
  // with rank, so the search falls back to any tier that fits, then to the largest window, keeping `tier` on a tie.
72
- export function fitTier(config, tier, tokens) {
73
- const windowOf = (t) => routeModel(config, t).contextWindow;
92
+ export function fitTier(config, tier, tokens, routeFor = (t) => resolveRoute(config, t)) {
93
+ const windowOf = (t) => routeSpec(config, routeFor(t)).contextWindow;
74
94
  const fits = (t) => tokens <= windowOf(t) * CONTEXT_FILL;
75
95
  if (fits(tier)) return tier;
76
96
  return (
@@ -80,18 +100,17 @@ export function fitTier(config, tier, tokens) {
80
100
  );
81
101
  }
82
102
 
83
- // Cold-write guard: an automatic route (upgrade, downgrade or escalation) to a credits-billed model whose cache is not warm must not start with a cache
103
+ // Cold-write guard: an automatic route (upgrade, downgrade, escalation or activity move) to a credits-billed model whose cache is not warm must not start with a cache
84
104
  // write above `cashCapUsd`. It bounds that one estimated write, not the spend of the turn: a warm cache passes, and
85
105
  // output is not counted.
86
- function cashGate(config, tier, facts, now, costs) {
87
- const alias = config.routes[tier].model;
88
- if (config.models[alias].billing !== 'credits') return { blocked: false };
89
- if (costs.isWarm(facts.models[costs.routeCacheKey(config, tier, facts.effort)], now, config.cache))
106
+ export function cashGate(config, route, facts, now, costs, routeFor = (tier) => resolveRoute(config, tier)) {
107
+ if (routeSpec(config, route).billing !== 'credits') return { blocked: false };
108
+ if (costs.isWarm(facts.models[costs.routeCacheKey(config, route, facts.effort)], now, config.cache))
90
109
  return { blocked: false };
91
- const cold = costs.coldWriteUsd(config, alias, costs.nextContextTokens(facts));
110
+ const cold = costs.coldWriteUsd(config, route.model, costs.nextContextTokens(facts));
92
111
  if (cold <= config.policy.cashCapUsd) return { blocked: false };
93
- const fallback = [...TIERS].reverse().find((t) => routeModel(config, t).billing === 'plan');
94
- const estimate = { coldUsd: cold, cap: config.policy.cashCapUsd, cache: cacheOf(config, tier, facts, now, costs) };
112
+ const fallback = [...TIERS].reverse().find((t) => routeSpec(config, routeFor(t)).billing === 'plan');
113
+ const estimate = { coldUsd: cold, cap: config.policy.cashCapUsd, cache: cacheOf(config, route, facts, now, costs) };
95
114
  return { blocked: true, fallback, estimate };
96
115
  }
97
116
 
@@ -101,8 +120,8 @@ function gatedResult(gated, incumbent, state) {
101
120
  return result(tier, 'cash-gate', state, gated.estimate);
102
121
  }
103
122
 
104
- function cacheOf(config, tier, facts, now, costs) {
105
- return costs.cacheState(facts.models[costs.routeCacheKey(config, tier, facts.effort)], now, config.cache);
123
+ function cacheOf(config, route, facts, now, costs) {
124
+ return costs.cacheState(facts.models[costs.routeCacheKey(config, route, facts.effort)], now, config.cache);
106
125
  }
107
126
 
108
127
  function trailing(votes, predicate) {