@alexeiled/claude-router 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +44 -0
- package/README.md +9 -9
- package/docs/architecture.md +22 -7
- package/docs/configuration.md +27 -26
- package/docs/user-guide.md +3 -3
- package/lib/config.mjs +15 -10
- package/lib/cost.mjs +13 -4
- package/lib/gateway.mjs +1 -1
- package/lib/policy.mjs +11 -3
- package/lib/router.mjs +2 -1
- package/package.json +1 -1
- package/scripts/git-hooks/pre-commit +17 -3
- package/skills/medium/SKILL.md +3 -3
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "router",
|
|
3
3
|
"displayName": "jev-router",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.8.0",
|
|
5
5
|
"description": "Auto-picks the right Claude model for each turn — small models for quick edits, mid-tier for code, frontier for hard problems. Uses Jev to classify each request.",
|
|
6
6
|
"author": { "name": "Alexei Ledenev", "url": "https://github.com/alexei-led" },
|
|
7
7
|
"repository": "https://github.com/alexei-led/claude-router",
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.8.0 (2026-10-01)
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
- **Sonnet 5.5 replaces Sonnet 5.** The `sonnet` model is now
|
|
8
|
+
`claude-sonnet-5-5`, at the same prices. A live probe through Claude Code
|
|
9
|
+
2.1.286 shows it takes all five effort levels and all three request
|
|
10
|
+
features, so the gateway no longer adapts its history.
|
|
11
|
+
- **`medium` runs on Sonnet 5.5 at `xhigh`**, not on Opus 5.5 at `high`.
|
|
12
|
+
`/router:medium` changes with it. `high` stays Opus 5.5 at `xhigh`. From
|
|
13
|
+
2026-09-23 to 2026-10-01, 15 of 16 escalations from Sonnet moved the
|
|
14
|
+
session onto Opus for good, at about $31.6 of cold cache writes; most were
|
|
15
|
+
triggered by environment or hook errors, not by the model. An escalation
|
|
16
|
+
from `low` now stays on Sonnet.
|
|
17
|
+
|
|
18
|
+
### Upgrade
|
|
19
|
+
|
|
20
|
+
- If your `router.json` sets `models.sonnet.id` to `claude-sonnet-5`, also
|
|
21
|
+
set `"features": ["mid-conversation-system"]`. The new default lists all
|
|
22
|
+
three features, and Sonnet 5 answers the other two with 400.
|
|
23
|
+
- To keep the old `medium`, set
|
|
24
|
+
`"routes": { "medium": { "model": "opus", "effort": "high" } }` and change
|
|
25
|
+
`skills/medium/SKILL.md` to match.
|
|
26
|
+
|
|
27
|
+
## 0.7.2 (2026-09-25)
|
|
28
|
+
|
|
29
|
+
### Fixed
|
|
30
|
+
|
|
31
|
+
- `router.json` accepted a `policy.downgradePivotUsd` of 0 and a
|
|
32
|
+
`policy.downgradeSlope` that is not a number. A zero pivot made the
|
|
33
|
+
downgrade bar `NaN` whenever a switch cost nothing, and then no downgrade
|
|
34
|
+
ever passed. Both values now stop a new gateway from starting, as the
|
|
35
|
+
upgrade keys already did; `/router:status` names the field.
|
|
36
|
+
|
|
37
|
+
### Changed
|
|
38
|
+
|
|
39
|
+
- The downgrade bar counts what a cheaper model saves, not only its cache
|
|
40
|
+
write. The tax used to count the input of the next request alone. It now
|
|
41
|
+
nets the output and cache-read savings of the next
|
|
42
|
+
`policy.downgradeHorizonTurns` turns (default 5). From a warm Opus at
|
|
43
|
+
`xhigh` to a cold Sonnet, with 104k of context and 4k of output, the bar
|
|
44
|
+
drops from 0.935 to 0.922 (1-hour TTL). It never goes below 0.90. A model
|
|
45
|
+
without an `output` price keeps the input-only tax.
|
|
46
|
+
|
|
3
47
|
## 0.7.1 (2026-09-24)
|
|
4
48
|
|
|
5
49
|
### Fixed
|
package/README.md
CHANGED
|
@@ -54,9 +54,9 @@ flowchart LR
|
|
|
54
54
|
|
|
55
55
|
A local gateway receives each request from Claude Code. It changes the model,
|
|
56
56
|
the effort and the thinking setting. For Haiku, it also limits the output
|
|
57
|
-
size. It does not change your prompt or your tools.
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
size. It does not change your prompt or your tools. Haiku does not accept all
|
|
58
|
+
the request features that Claude Code uses for Opus, so the gateway adapts
|
|
59
|
+
those parts of the history for it, as Claude Code itself does. Prompt
|
|
60
60
|
caching and a claude.ai login work as usual.
|
|
61
61
|
|
|
62
62
|
## How Jev selects a tier
|
|
@@ -76,12 +76,12 @@ flowchart LR
|
|
|
76
76
|
class R out
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
-
| Tier | Model and effort
|
|
80
|
-
| -------- |
|
|
81
|
-
| `micro` | Haiku 4.5, no thinking
|
|
82
|
-
| `low` | Sonnet 5, your effort
|
|
83
|
-
| `medium` |
|
|
84
|
-
| `high` | Opus 5.5, `xhigh` effort
|
|
79
|
+
| Tier | Model and effort | Jev selects it for |
|
|
80
|
+
| -------- | -------------------------- | --------------------------------------------------------------------- |
|
|
81
|
+
| `micro` | Haiku 4.5, no thinking | Lookups, trivial edits, one-step mechanical work |
|
|
82
|
+
| `low` | Sonnet 5.5, your effort | Clear, low-risk coding steps with one obvious approach |
|
|
83
|
+
| `medium` | Sonnet 5.5, `xhigh` effort | Features, bug fixes and refactors with interacting constraints |
|
|
84
|
+
| `high` | Opus 5.5, `xhigh` effort | Architecture, unclear bugs, security, work where correctness is vital |
|
|
85
85
|
|
|
86
86
|
The router tells Jev to put correctness before cost, and not to judge by prompt
|
|
87
87
|
length, language or single topic words. The router then keeps the choice
|
package/docs/architecture.md
CHANGED
|
@@ -61,9 +61,9 @@ flowchart TB
|
|
|
61
61
|
- Claude Code builds each request for the model that the alias behaves as
|
|
62
62
|
(Opus). A model without one of its [request features](configuration.md#models)
|
|
63
63
|
gets the request without it, the way Claude Code retries after a 400:
|
|
64
|
-
- no `mid-conversation-tool-changes` (
|
|
64
|
+
- no `mid-conversation-tool-changes` (Haiku): `tool_addition` and
|
|
65
65
|
`tool_removal` blocks go; their cache breakpoint moves to the block before;
|
|
66
|
-
- no `per-turn-control` (
|
|
66
|
+
- no `per-turn-control` (Haiku): the beta header and the
|
|
67
67
|
`output_config` on messages (the effort of each turn) go;
|
|
68
68
|
- no `mid-conversation-system` (Haiku): each `system` message becomes a
|
|
69
69
|
`<system-reminder>` text in the user message next to it.
|
|
@@ -208,6 +208,16 @@ only when that tier is above the current route.
|
|
|
208
208
|
- The downgrade bar grows the same way, from the other side: a candidate
|
|
209
209
|
colder than the incumbent pays a cache write, not just a smaller model.
|
|
210
210
|
`bar = 0.90 + 0.08 × tax / (tax + $0.50)`. It stays between 0.90 and 0.98.
|
|
211
|
+
- The downgrade tax nets the savings of the next `H` turns
|
|
212
|
+
(`policy.downgradeHorizonTurns`, default 5):
|
|
213
|
+
`tax = max(0, next + (H − 1) × later)`. `next` is the cost difference of
|
|
214
|
+
the next turn, input and output. `later` is the difference of each later
|
|
215
|
+
turn, with both caches warm. Both come from the
|
|
216
|
+
[shadow estimate](#cache-and-cost-model). Example: warm Opus at `xhigh` to
|
|
217
|
+
a cold Sonnet, 104k of context, 4k of output, 1-hour TTL. The input tax is
|
|
218
|
+
$0.40 (bar 0.935); net of five turns it is $0.20 (bar 0.922).
|
|
219
|
+
- When either model has no `output` price, or a zero one, the downgrade tax
|
|
220
|
+
is the input tax alone. A missing price is unknown, not free.
|
|
211
221
|
- A prompt continues the task when the Jev continuation answer is 0.7 or
|
|
212
222
|
more. A continuing prompt never votes for a downgrade.
|
|
213
223
|
- Upgrades have no cooldown. Plan, then execute, then hard work again is a
|
|
@@ -260,8 +270,8 @@ tax = max(0, input_cost(candidate) − input_cost(current))
|
|
|
260
270
|
`test/fixtures/list-prices.json`, which names its source and date.
|
|
261
271
|
- **Shadow estimate.** When the Jev choice differs from the current route, the
|
|
262
272
|
log records the cost of the next turn, the cost of each later turn with
|
|
263
|
-
output, and the turns until a switch repays its cost. The
|
|
264
|
-
|
|
273
|
+
output, and the turns until a switch repays its cost. The downgrade tax
|
|
274
|
+
uses the same arithmetic. The upgrade tax counts input only.
|
|
265
275
|
|
|
266
276
|
## Modules
|
|
267
277
|
|
|
@@ -417,7 +427,7 @@ To work on the code:
|
|
|
417
427
|
|
|
418
428
|
```sh
|
|
419
429
|
npm install
|
|
420
|
-
git config --local core.hooksPath scripts/git-hooks # Biome, tests and Gitleaks before commit and push
|
|
430
|
+
git config --local core.hooksPath scripts/git-hooks # Biome, ShellCheck, tests and Gitleaks before commit and push
|
|
421
431
|
npm test && npm run check # node:test, Biome
|
|
422
432
|
claude --plugin-dir . --model jev-router # with ANTHROPIC_BASE_URL and TYPESAFE_API_KEY
|
|
423
433
|
```
|
|
@@ -431,7 +441,12 @@ claude --plugin-dir . --model jev-router # with ANTHROPIC_BASE_URL
|
|
|
431
441
|
- Without the hint headers, the gateway guesses side requests. A missed side
|
|
432
442
|
request with a short history counts as a history break.
|
|
433
443
|
- `x-claude-code-agent-type` goes to the log only.
|
|
434
|
-
- The shadow estimate
|
|
435
|
-
changes how much a model writes.
|
|
444
|
+
- The shadow estimate and the downgrade tax use the last output size for
|
|
445
|
+
both routes, but effort changes how much a model writes.
|
|
446
|
+
- Opus 5.5 and Sonnet 5.5 bind thinking blocks to the model and the
|
|
447
|
+
conversation. Anthropic answers a replay after a prefix change with 400 for
|
|
448
|
+
accounts created on or after 2026-08-31. A switch between the two, with
|
|
449
|
+
thinking blocks in the history, passed on one older account (2026-10-01);
|
|
450
|
+
no newer account was tested.
|
|
436
451
|
|
|
437
452
|
A day of real usage is in [Evaluation](evaluation.md).
|
package/docs/configuration.md
CHANGED
|
@@ -95,12 +95,12 @@ For example, to run `low` on Sonnet at `high` effort and give Jev more time:
|
|
|
95
95
|
|
|
96
96
|
### routes
|
|
97
97
|
|
|
98
|
-
| Tier | Default
|
|
99
|
-
| -------- |
|
|
100
|
-
| `high` | `{ "model": "opus", "effort": "xhigh" }`
|
|
101
|
-
| `medium` | `{ "model": "
|
|
102
|
-
| `low` | `{ "model": "sonnet" }`
|
|
103
|
-
| `micro` | `{ "model": "haiku" }`
|
|
98
|
+
| Tier | Default |
|
|
99
|
+
| -------- | ------------------------------------------ |
|
|
100
|
+
| `high` | `{ "model": "opus", "effort": "xhigh" }` |
|
|
101
|
+
| `medium` | `{ "model": "sonnet", "effort": "xhigh" }` |
|
|
102
|
+
| `low` | `{ "model": "sonnet" }` |
|
|
103
|
+
| `micro` | `{ "model": "haiku" }` |
|
|
104
104
|
|
|
105
105
|
`model` is a key of `models`. `effort` is `low`, `medium`, `high`, `xhigh` or
|
|
106
106
|
`max`. Without `effort`, the gateway keeps the effort that Claude Code sent.
|
|
@@ -109,16 +109,16 @@ A test makes sure that they agree.
|
|
|
109
109
|
|
|
110
110
|
### models
|
|
111
111
|
|
|
112
|
-
| Alias | `id`
|
|
113
|
-
| -------- |
|
|
114
|
-
| `opus` | `claude-opus-5-5`
|
|
115
|
-
| `sonnet` | `claude-sonnet-5`
|
|
116
|
-
| `haiku` | `claude-haiku-4-5`
|
|
112
|
+
| Alias | `id` | `input` | `output` | `cacheRead` | `contextWindow` | `maxOutput` | `billing` | `efforts` | `features` |
|
|
113
|
+
| -------- | ------------------- | ------- | -------- | ----------- | --------------- | ----------- | --------- | --------- | ---------- |
|
|
114
|
+
| `opus` | `claude-opus-5-5` | 4 | 20 | 0.2 | 1,000,000 | — | `plan` | all five | all three |
|
|
115
|
+
| `sonnet` | `claude-sonnet-5-5` | 2 | 10 | 0.2 | 1,000,000 | — | `plan` | all five | all three |
|
|
116
|
+
| `haiku` | `claude-haiku-4-5` | 1 | 5 | 0.1 | 200,000 | 64,000 | `plan` | none | none |
|
|
117
117
|
|
|
118
118
|
| Field | Meaning |
|
|
119
119
|
| ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
120
120
|
| `id` | The model id that the gateway sends to Anthropic. |
|
|
121
|
-
| `input`, `output`, `cacheRead` | List prices in USD per million tokens. `output` is optional.
|
|
121
|
+
| `input`, `output`, `cacheRead` | List prices in USD per million tokens. `output` is optional. The `shadow` estimate and the downgrade tax use it. |
|
|
122
122
|
| `contextWindow` | The window in tokens. A turn goes only to a model that holds the context at 80% fill. |
|
|
123
123
|
| `maxOutput` | Optional limit for `max_tokens`. Haiku 4.5 rejects more than 64K. |
|
|
124
124
|
| `billing` | `plan` for the subscription limits. `credits` for models that bill usage credits. |
|
|
@@ -133,20 +133,21 @@ from a probe against the real API. A test fails when they differ.
|
|
|
133
133
|
|
|
134
134
|
The [switching policy](architecture.md#switching-policy) uses these values.
|
|
135
135
|
|
|
136
|
-
| Key
|
|
137
|
-
|
|
|
138
|
-
| `upgradeVotes`
|
|
139
|
-
| `upgradeBase`
|
|
140
|
-
| `upgradeSlope`
|
|
141
|
-
| `upgradePivotUsd`
|
|
142
|
-
| `jumpConfidence`
|
|
143
|
-
| `downgradeVotes`
|
|
144
|
-
| `downgradeMass`
|
|
145
|
-
| `downgradeSlope`
|
|
146
|
-
| `downgradePivotUsd`
|
|
147
|
-
| `
|
|
148
|
-
| `
|
|
149
|
-
| `
|
|
136
|
+
| Key | Default | Meaning |
|
|
137
|
+
| ----------------------- | ------- | ----------------------------------------------------------------------------------------------------- |
|
|
138
|
+
| `upgradeVotes` | `2` | Consecutive votes above the current tier before an upgrade. |
|
|
139
|
+
| `upgradeBase` | `0.75` | The upgrade bar when a switch costs nothing. |
|
|
140
|
+
| `upgradeSlope` | `0.15` | How much a switching cost can raise the bar: `base + slope × tax / (tax + pivot)`. |
|
|
141
|
+
| `upgradePivotUsd` | `0.5` | The switching cost that adds half of the slope. |
|
|
142
|
+
| `jumpConfidence` | `0.95` | The confidence for a jump of two tiers without the vote delay. |
|
|
143
|
+
| `downgradeVotes` | `2` | Consecutive votes for a lower tier before a downgrade. |
|
|
144
|
+
| `downgradeMass` | `0.9` | The downgrade bar when the candidate's cache is already warm. |
|
|
145
|
+
| `downgradeSlope` | `0.08` | How much a cold candidate's cache write can raise the bar, same formula as upgrade. |
|
|
146
|
+
| `downgradePivotUsd` | `0.5` | The switching cost that adds half of the slope. |
|
|
147
|
+
| `downgradeHorizonTurns` | `5` | The turns of output and read savings that a downgrade's tax nets. Input only without `output` prices. |
|
|
148
|
+
| `continuationMass` | `0.7` | The Jev probability for "continues the task" that keeps the route. |
|
|
149
|
+
| `escalationHoldTurns` | `2` | Turns that the route stays up after an escalation. |
|
|
150
|
+
| `cashCapUsd` | `2` | The cold-write guard: the largest first cache write for a switch to a `credits` model. |
|
|
150
151
|
|
|
151
152
|
### cache, jev, context, log
|
|
152
153
|
|
package/docs/user-guide.md
CHANGED
|
@@ -18,7 +18,7 @@ to read what it does, and how to control it.
|
|
|
18
18
|
|
|
19
19
|
1. Start Claude Code. `/model` shows `Jev Router (auto)` as the model.
|
|
20
20
|
2. Send a prompt. If setup added the status line, it shows the route, for
|
|
21
|
-
example `jev-router ▸ sonnet-5 · high · same-tier`.
|
|
21
|
+
example `jev-router ▸ sonnet-5-5 · high · same-tier`.
|
|
22
22
|
3. Run `/router:status`. It shows `Jev routing: active.`
|
|
23
23
|
|
|
24
24
|
If a step fails, read [Troubleshooting](#troubleshooting).
|
|
@@ -101,7 +101,7 @@ Router v0.7.0, gateway: http://127.0.0.1:43170, alias `jev-router`.
|
|
|
101
101
|
Jev routing: active.
|
|
102
102
|
Default tier: low.
|
|
103
103
|
…
|
|
104
|
-
Last turn: low → claude-sonnet-5 at high, reason upgrade-pending, context 84210 tokens, cache reads 83904.
|
|
104
|
+
Last turn: low → claude-sonnet-5-5 at high, reason upgrade-pending, context 84210 tokens, cache reads 83904.
|
|
105
105
|
Why: Jev asked for a higher tier; the route stays until the votes and the mass are enough.
|
|
106
106
|
Estimate: upgrade mass 0.78 against a bar of 0.84, switching tax $0.66 at list prices, 1 vote(s) in a row, cache: candidate unknown, current warm.
|
|
107
107
|
```
|
|
@@ -143,7 +143,7 @@ log has no prompt text.
|
|
|
143
143
|
| `reason` | The rule that decided. Tool calls show `tool-continuation`. `error`: routing failed and the default tier served. |
|
|
144
144
|
| `advice` | The Jev probabilities for each tier, and for "continues the task". |
|
|
145
145
|
| `estimate` | The numbers of a vote: confidence, bar, switching tax, cache state. |
|
|
146
|
-
| `shadow` | The cost of the Jev choice against the current route. The
|
|
146
|
+
| `shadow` | The cost of the Jev choice against the current route. The downgrade tax uses the same arithmetic. |
|
|
147
147
|
| `observed` | The usage of the response: model, effort, context tokens, cache reads, output. |
|
|
148
148
|
| `failed` | Anthropic answered a routed turn with an error: status, tier, model, effort. |
|
|
149
149
|
| `historyBreak` | A compaction or a rewind. The votes and the cache estimates reset. |
|
package/lib/config.mjs
CHANGED
|
@@ -14,17 +14,19 @@ export const DEFAULTS = {
|
|
|
14
14
|
// the hooks start it again before the next prompt. Two hours outlast /loop wakeups and Monitor waits (up to one
|
|
15
15
|
// hour), which reach the gateway without a prompt. 0 keeps it running.
|
|
16
16
|
gateway: { port: 43170, alias: 'jev-router', baselineTier: 'low', auxiliaryTier: 'low', idleShutdownMs: 7_200_000 },
|
|
17
|
-
//
|
|
18
|
-
//
|
|
17
|
+
// Sonnet 5.5 is the worker: `low` at the effort the user picked, `medium` at xhigh, so an escalation from `low`
|
|
18
|
+
// stays on Sonnet's cache instead of a cold write to Opus. `high` is Opus 5.5 at xhigh, the strongest setting this
|
|
19
|
+
// router asks for: Anthropic keeps Opus for open-ended, long-horizon work. xhigh on Sonnet is not yet measured
|
|
20
|
+
// against Opus at high; the `observed` lines in decisions.jsonl are the input for that.
|
|
19
21
|
routes: {
|
|
20
22
|
high: { model: 'opus', effort: 'xhigh' },
|
|
21
|
-
medium: { model: '
|
|
23
|
+
medium: { model: 'sonnet', effort: 'xhigh' },
|
|
22
24
|
low: { model: 'sonnet' },
|
|
23
25
|
micro: { model: 'haiku' },
|
|
24
26
|
},
|
|
25
27
|
// `id` is sent upstream verbatim. List prices in USD per million tokens; `cacheRead` is absolute,
|
|
26
28
|
// not a multiplier (Opus 5.5 reads at 0.05x input, the rest at the standard 0.1x). A price change must also change
|
|
27
|
-
// test/fixtures/list-prices.json, with its source and date. `output` feeds
|
|
29
|
+
// test/fixtures/list-prices.json, with its source and date. `output` feeds the shadow estimate and the downgrade tax.
|
|
28
30
|
// `efforts` lists what the model accepts; an empty list means no effort field and no adaptive thinking. A change
|
|
29
31
|
// must also change test/fixtures/effort-support.json, from a new probe against the real API.
|
|
30
32
|
// `features` lists the FEATURES the model accepts; the gateway removes the others from the request. A model
|
|
@@ -42,14 +44,14 @@ export const DEFAULTS = {
|
|
|
42
44
|
features: FEATURES,
|
|
43
45
|
},
|
|
44
46
|
sonnet: {
|
|
45
|
-
id: 'claude-sonnet-5',
|
|
47
|
+
id: 'claude-sonnet-5-5',
|
|
46
48
|
input: 2,
|
|
47
49
|
output: 10,
|
|
48
50
|
cacheRead: 0.2,
|
|
49
51
|
contextWindow: 1_000_000,
|
|
50
52
|
billing: 'plan',
|
|
51
53
|
efforts: EFFORTS,
|
|
52
|
-
features:
|
|
54
|
+
features: FEATURES,
|
|
53
55
|
},
|
|
54
56
|
// `maxOutput` caps `max_tokens`: Claude Code asks for 128K, Haiku 4.5 answers more than 64K with 400.
|
|
55
57
|
haiku: {
|
|
@@ -79,6 +81,7 @@ export const DEFAULTS = {
|
|
|
79
81
|
downgradeMass: 0.9,
|
|
80
82
|
downgradeSlope: 0.08, // a cold candidate raises the bar toward ~0.98, same shape as the upgrade bar
|
|
81
83
|
downgradePivotUsd: 0.5,
|
|
84
|
+
downgradeHorizonTurns: 5, // turns whose output and read savings offset a downgrade's cache write
|
|
82
85
|
continuationMass: 0.7,
|
|
83
86
|
escalationHoldTurns: 2,
|
|
84
87
|
// Ceiling on a cold cache write to a `credits` model. No default model bills credits, so this is
|
|
@@ -213,12 +216,14 @@ function validate(config) {
|
|
|
213
216
|
for (const field of ['upgradeBase', 'jumpConfidence', 'downgradeMass', 'continuationMass']) {
|
|
214
217
|
if (!(isNumber(p[field], 0) && p[field] <= 1)) throw new Error(`policy.${field} must be between 0 and 1`);
|
|
215
218
|
}
|
|
216
|
-
for (const field of ['upgradeVotes', 'downgradeVotes', 'escalationHoldTurns']) {
|
|
219
|
+
for (const field of ['upgradeVotes', 'downgradeVotes', 'downgradeHorizonTurns', 'escalationHoldTurns']) {
|
|
217
220
|
if (!isNumber(p[field], 1, { integer: true })) throw new Error(`policy.${field} must be a positive integer`);
|
|
218
221
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
+
// The pivot divides: tax / (tax + pivot). Zero makes a free switch NaN and silently stops that switch.
|
|
223
|
+
for (const side of ['upgrade', 'downgrade']) {
|
|
224
|
+
if (!isNumber(p[`${side}Slope`], 0)) throw new Error(`policy.${side}Slope must be a non-negative number`);
|
|
225
|
+
if (!isNumber(p[`${side}PivotUsd`], 0, { above: true })) throw new Error(`policy.${side}PivotUsd must be positive`);
|
|
226
|
+
}
|
|
222
227
|
if (!isNumber(p.cashCapUsd, 0)) throw new Error('policy.cashCapUsd must be a non-negative number');
|
|
223
228
|
const { cache, jev, context } = config;
|
|
224
229
|
for (const map of ['writeMultiplier', 'ttlMs']) {
|
package/lib/cost.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { clampEffort } from './rewrite.mjs';
|
|
3
3
|
|
|
4
4
|
// The prompt cache belongs to a model, and its messages part to the effort too: a top-level effort change rewrites
|
|
5
|
-
// the messages cache.
|
|
5
|
+
// the messages cache. Sonnet at `high` and Sonnet at `xhigh` are two caches, not one.
|
|
6
6
|
export function cacheKey(modelId, effort) {
|
|
7
7
|
return effort ? `${modelId}@${effort}` : modelId;
|
|
8
8
|
}
|
|
@@ -62,9 +62,9 @@ export function switchingTaxUsd(config, candidateTier, incumbentTier, facts, now
|
|
|
62
62
|
);
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
// Shadow estimate for the decision log; the
|
|
66
|
-
// list-price equivalent, not a charge. Output uses the last observed output size for both routes, although
|
|
67
|
-
// changes how much a model writes.
|
|
65
|
+
// Shadow estimate for the decision log; the downgrade tax nets it over a horizon. At list prices, so for `plan` models
|
|
66
|
+
// it is a list-price equivalent, not a charge. Output uses the last observed output size for both routes, although
|
|
67
|
+
// effort changes how much a model writes.
|
|
68
68
|
// - nextTurnUsd: candidate minus incumbent for the next request, input at the current cache state plus output.
|
|
69
69
|
// - laterTurnUsd: the same difference for each later turn, once both caches are warm.
|
|
70
70
|
// - paybackTurns: 0 when the switch is cheaper at once, n when later turns repay it after n turns, null when never.
|
|
@@ -82,3 +82,12 @@ export function shadowEconomics(config, candidateTier, incumbentTier, facts, now
|
|
|
82
82
|
else if (nextTurnUsd > 0 && laterTurnUsd < 0) paybackTurns = Math.ceil(nextTurnUsd / -laterTurnUsd);
|
|
83
83
|
return { nextTurnUsd, laterTurnUsd, paybackTurns, outputTokens };
|
|
84
84
|
}
|
|
85
|
+
|
|
86
|
+
// A downgrade's tax over `turns` turns: the next one plus the later ones, output included. A missing or zero output
|
|
87
|
+
// price is unknown, not free, so the tax then falls back to the next request's input alone.
|
|
88
|
+
export function downgradeTaxUsd(config, candidateTier, incumbentTier, facts, now, turns) {
|
|
89
|
+
const priced = [candidateTier, incumbentTier].every((tier) => modelOf(config, tier).output > 0);
|
|
90
|
+
if (!priced) return Math.max(0, switchingTaxUsd(config, candidateTier, incumbentTier, facts, now));
|
|
91
|
+
const { nextTurnUsd, laterTurnUsd } = shadowEconomics(config, candidateTier, incumbentTier, facts, now);
|
|
92
|
+
return Math.max(0, nextTurnUsd + (turns - 1) * laterTurnUsd);
|
|
93
|
+
}
|
package/lib/gateway.mjs
CHANGED
|
@@ -214,7 +214,7 @@ function errorBody(type, message) {
|
|
|
214
214
|
return { type: 'error', error: { type, message } };
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
-
// "Picks claude-opus-5-5 / claude-sonnet-5 / claude-haiku-4-5 and the effort for each turn"
|
|
217
|
+
// "Picks claude-opus-5-5 / claude-sonnet-5-5 / claude-haiku-4-5 and the effort for each turn"
|
|
218
218
|
export function routesDescription(config) {
|
|
219
219
|
const ids = [...new Set(Object.values(config.routes).map((r) => config.models[r.model].id))];
|
|
220
220
|
return `Picks ${ids.join(' / ')} and the effort for each turn`;
|
package/lib/policy.mjs
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
// Switching policy v0 (docs/architecture.md): stickiness, escalation floor, cost-gated votes.
|
|
2
2
|
import { rank, TIERS } from './config.mjs';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
cacheState,
|
|
5
|
+
coldWriteUsd,
|
|
6
|
+
downgradeTaxUsd,
|
|
7
|
+
isWarm,
|
|
8
|
+
nextContextTokens,
|
|
9
|
+
routeCacheKey,
|
|
10
|
+
switchingTaxUsd,
|
|
11
|
+
} from './cost.mjs';
|
|
4
12
|
|
|
5
13
|
export function initialState() {
|
|
6
14
|
return { turn: 0, votes: [], holdUntilTurn: 0, escalatedSignature: null };
|
|
@@ -49,8 +57,8 @@ export function decide({ config, facts, advice, state, baseline, now }) {
|
|
|
49
57
|
const support = massAtOrBelow(advice.probabilities, choice);
|
|
50
58
|
const streak = trailing(next.votes, (v) => rank(v.tier) <= rank(choice));
|
|
51
59
|
// A downgrade is not always cheaper this turn: a cold candidate next to a warm incumbent can pay a bigger cache
|
|
52
|
-
// write than it saves. The bar rises with that cost the
|
|
53
|
-
const tax =
|
|
60
|
+
// write than it saves. The bar rises with that cost, net of what the next turns save, the way the upgrade bar does.
|
|
61
|
+
const tax = downgradeTaxUsd(config, choice, incumbent, facts, now, config.policy.downgradeHorizonTurns);
|
|
54
62
|
const threshold =
|
|
55
63
|
config.policy.downgradeMass + config.policy.downgradeSlope * (tax / (tax + config.policy.downgradePivotUsd));
|
|
56
64
|
const estimate = { downgradeMass: support, streak, taxUsd: tax, threshold };
|
package/lib/router.mjs
CHANGED
|
@@ -185,7 +185,8 @@ export class Router {
|
|
|
185
185
|
}
|
|
186
186
|
const now = this.now();
|
|
187
187
|
const decision = decide({ config: this.config, facts, advice, state, baseline: gateway.baselineTier, now });
|
|
188
|
-
// Shadow only: what following Jev's choice instead of staying would cost at list prices. The policy
|
|
188
|
+
// Shadow only: what following Jev's choice instead of staying would cost at list prices. The policy does not read
|
|
189
|
+
// this record; the downgrade tax computes its own from the same function.
|
|
189
190
|
const incumbent = TIERS.includes(facts.lastRoute) ? facts.lastRoute : gateway.baselineTier;
|
|
190
191
|
const shadow =
|
|
191
192
|
TIERS.includes(advice?.choice) && advice.choice !== incumbent
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
|
-
# Staged-only checks: Biome on staged .mjs/.json,
|
|
3
|
-
# Full check/test/pack runs in pre-push.
|
|
2
|
+
# Staged-only checks: Biome on staged .mjs/.json, ShellCheck on staged shell
|
|
3
|
+
# scripts, Gitleaks on the staged diff. Full check/test/pack runs in pre-push.
|
|
4
4
|
set -euo pipefail
|
|
5
5
|
|
|
6
6
|
cd "$(git rev-parse --show-toplevel)"
|
|
@@ -16,12 +16,26 @@ command -v gitleaks >/dev/null || {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
files=()
|
|
19
|
+
shell_files=()
|
|
19
20
|
while IFS= read -r -d '' f; do
|
|
20
|
-
case "$f" in
|
|
21
|
+
case "$f" in
|
|
22
|
+
*.mjs | *.json) files+=("$f") ;;
|
|
23
|
+
*.sh | scripts/git-hooks/*) shell_files+=("$f") ;;
|
|
24
|
+
esac
|
|
21
25
|
done < <(git diff --cached --name-only -z --diff-filter=ACMR)
|
|
22
26
|
|
|
23
27
|
if [ "${#files[@]}" -gt 0 ]; then
|
|
24
28
|
"$biome" check --no-errors-on-unmatched -- "${files[@]}"
|
|
25
29
|
fi
|
|
26
30
|
|
|
31
|
+
# Checked only when shell files are staged, so contributors who never touch
|
|
32
|
+
# them don't need shellcheck installed.
|
|
33
|
+
if [ "${#shell_files[@]}" -gt 0 ]; then
|
|
34
|
+
command -v shellcheck >/dev/null || {
|
|
35
|
+
echo "pre-commit: shellcheck missing; run 'brew install shellcheck'" >&2
|
|
36
|
+
exit 1
|
|
37
|
+
}
|
|
38
|
+
shellcheck -- "${shell_files[@]}"
|
|
39
|
+
fi
|
|
40
|
+
|
|
27
41
|
gitleaks git --pre-commit --staged --redact --verbose --no-banner
|
package/skills/medium/SKILL.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: medium
|
|
3
3
|
disable-model-invocation: true
|
|
4
|
-
description: Manual pin: run this turn on
|
|
5
|
-
model:
|
|
6
|
-
effort:
|
|
4
|
+
description: Manual pin: run this turn on sonnet at xhigh effort. Use as /router:medium <prompt>.
|
|
5
|
+
model: sonnet
|
|
6
|
+
effort: xhigh
|
|
7
7
|
---
|
|
8
8
|
Routing tier applied. Continue with the user's request as written. Do not mention the router.
|