@alexeiled/claude-router 1.0.0 → 1.1.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/policy.mjs CHANGED
@@ -16,7 +16,9 @@ export function decide({ config, facts, advice, state, baseline, now, costs = na
16
16
  next.escalatedSignature = facts.failure.signature;
17
17
  next.holdUntilTurn = next.turn + config.policy.escalationHoldTurns;
18
18
  next.votes = [];
19
- return result(TIERS[rank(incumbent) + 1], 'escalation', next);
19
+ const target = TIERS[rank(incumbent) + 1];
20
+ const gated = cashGate(config, target, facts, now, costs);
21
+ return gated.blocked ? gatedResult(gated, incumbent, next) : result(target, 'escalation', next);
20
22
  }
21
23
  if (next.turn <= next.holdUntilTurn) return stay('hold');
22
24
  if (!advice) return stay('no-advice');
@@ -30,10 +32,7 @@ export function decide({ config, facts, advice, state, baseline, now, costs = na
30
32
  if (rank(choice) > rank(incumbent)) {
31
33
  const upgrade = massAbove(advice.probabilities, incumbent);
32
34
  const gated = cashGate(config, choice, facts, now, costs);
33
- if (gated.blocked) {
34
- if (!gated.fallback || rank(gated.fallback) <= rank(incumbent)) return stay('cash-gate', gated.estimate);
35
- return result(gated.fallback, 'cash-gate', next, gated.estimate);
36
- }
35
+ if (gated.blocked) return gatedResult(gated, incumbent, next);
37
36
  const tax = Math.max(0, costs.switchingTaxUsd(config, choice, incumbent, facts, now));
38
37
  const threshold =
39
38
  config.policy.upgradeBase + config.policy.upgradeSlope * (tax / (tax + config.policy.upgradePivotUsd));
@@ -57,8 +56,10 @@ export function decide({ config, facts, advice, state, baseline, now, costs = na
57
56
  const threshold =
58
57
  config.policy.downgradeMass + config.policy.downgradeSlope * (tax / (tax + config.policy.downgradePivotUsd));
59
58
  const estimate = { downgradeMass: support, streak, taxUsd: tax, threshold };
60
- if (support >= threshold && streak >= config.policy.downgradeVotes)
61
- return result(choice, 'downgrade', next, estimate);
59
+ if (support >= threshold && streak >= config.policy.downgradeVotes) {
60
+ const gated = cashGate(config, choice, facts, now, costs);
61
+ return gated.blocked ? gatedResult(gated, incumbent, next) : result(choice, 'downgrade', next, estimate);
62
+ }
62
63
  return stay('downgrade-pending', estimate);
63
64
  }
64
65
 
@@ -79,7 +80,7 @@ export function fitTier(config, tier, tokens) {
79
80
  );
80
81
  }
81
82
 
82
- // Cold-write guard: an automatic route to a credits-billed model whose cache is not warm must not start with a cache
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
83
84
  // write above `cashCapUsd`. It bounds that one estimated write, not the spend of the turn: a warm cache passes, and
84
85
  // output is not counted.
85
86
  function cashGate(config, tier, facts, now, costs) {
@@ -94,6 +95,12 @@ function cashGate(config, tier, facts, now, costs) {
94
95
  return { blocked: true, fallback, estimate };
95
96
  }
96
97
 
98
+ // A blocked switch lands on the strongest plan tier when that is above the incumbent, else stays.
99
+ function gatedResult(gated, incumbent, state) {
100
+ const tier = gated.fallback && rank(gated.fallback) > rank(incumbent) ? gated.fallback : incumbent;
101
+ return result(tier, 'cash-gate', state, gated.estimate);
102
+ }
103
+
97
104
  function cacheOf(config, tier, facts, now, costs) {
98
105
  return costs.cacheState(facts.models[costs.routeCacheKey(config, tier, facts.effort)], now, config.cache);
99
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexeiled/claude-router",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Native Claude Code Mod: Jev model and effort routing for the main conversation.",
5
5
  "license": "MIT",
6
6
  "author": "Alexei Ledenev",
@@ -31,7 +31,9 @@
31
31
  "test": "node --test --test-timeout=8000 test/*.test.mjs",
32
32
  "check": "biome check lib scripts test hooks types .claude-plugin package.json package-lock.json biome.json experiments/mod-router",
33
33
  "validate": "claude plugin validate .claude-plugin/plugin.json && claude plugin validate .claude-plugin/marketplace.json",
34
+ "test:plugin": "bash scripts/plugin-test.sh",
34
35
  "format": "biome format --write .",
36
+ "docs:images": "node scripts/render-doc-images.mjs",
35
37
  "pack:dry": "npm pack --dry-run",
36
38
  "prepublishOnly": "npm run check && npm test"
37
39
  },
package/types/index.d.ts CHANGED
@@ -19,6 +19,16 @@ declare module 'claude-code' {
19
19
  cap?: number;
20
20
  cache?: RouterCacheState | { candidate: RouterCacheState; incumbent: RouterCacheState };
21
21
  }
22
+ interface RouterTuning {
23
+ timeoutMs: number;
24
+ downgradeVotes: number;
25
+ horizon: number;
26
+ cashCapUsd: number;
27
+ }
28
+ interface RouterRoutes {
29
+ routes: Record<RouterTier, { model: string; effort?: string | null }>;
30
+ baselineTier: RouterTier;
31
+ }
22
32
  interface RouterView {
23
33
  phase: 'ready' | 'choosing' | 'routed' | 'manual' | 'unavailable';
24
34
  mode: 'auto' | 'manual';
@@ -43,9 +53,16 @@ declare module 'claude-code' {
43
53
  adviceChoice?: RouterTier | 'uncertain' | null;
44
54
  estimate?: RouterEstimate | null;
45
55
  comparison?: RouterComparison | null;
56
+ probabilities?: Partial<Record<RouterTier | 'uncertain', number>> | null;
46
57
  history?: number[];
58
+ tiers?: (RouterTier | null)[];
47
59
  configPath?: string | null;
48
- tuning?: { timeoutMs: number; downgradeVotes: number; horizon: number } | null;
60
+ tuning?: Partial<RouterTuning> | null;
61
+ tuningBase?: RouterTuning | null;
62
+ routeDraft?: (RouterRoutes & { base: RouterRoutes }) | null;
63
+ tab?: 'now' | 'tiers' | 'tuning' | 'usage';
64
+ help?: boolean;
65
+ bandDetail?: boolean;
49
66
  notice?: string | null;
50
67
  }
51
68
  interface RouterPolicyState {