@alexeiled/claude-router 1.22.0 → 1.23.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 +30 -0
- package/README.md +7 -7
- package/docs/activity-routing.md +56 -14
- package/docs/architecture.md +40 -17
- package/docs/configuration.md +12 -4
- package/docs/evaluation.md +20 -5
- package/docs/native-router.md +2 -2
- package/docs/providers.md +1 -1
- package/docs/router-band.svg +11 -11
- package/docs/router-pane-now.svg +4 -4
- package/docs/router-pane-usage-whatif.svg +10 -9
- package/docs/router-pane-usage.svg +10 -9
- package/docs/routing-modes.md +13 -11
- package/docs/user-guide.md +46 -28
- package/hooks/native-router.mjs +127 -14
- package/lib/activity-stats.mjs +5 -3
- package/lib/activity.mjs +66 -23
- package/lib/band.mjs +11 -6
- package/lib/classifier-apis.mjs +79 -24
- package/lib/classifier-client.mjs +16 -5
- package/lib/classifier-contract.mjs +47 -11
- package/lib/config.mjs +16 -3
- package/lib/costs.mjs +2 -2
- package/lib/decisions.mjs +16 -1
- package/lib/display.mjs +15 -11
- package/lib/facts.mjs +32 -1
- package/lib/modes.mjs +12 -1
- package/lib/panel.mjs +22 -23
- package/lib/route.mjs +159 -36
- package/lib/subagents.mjs +3 -3
- package/lib/view.mjs +14 -1
- package/package.json +1 -1
- package/types/index.d.ts +5 -1
package/lib/modes.mjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// classifier and the configured routes stay as they are; the mode moves each tier's route by one fixed step. Pure.
|
|
3
3
|
import { ACTIVITIES, DEFAULTS, EFFORTS, ROUTING_MODES, rank, resolveRoute, sameRoute, TIERS } from './config.mjs';
|
|
4
4
|
import { clampEffort } from './cost.mjs';
|
|
5
|
+
import { canonicalModel } from './providers.mjs';
|
|
5
6
|
|
|
6
7
|
// The route's effort one level up (`step` 1) or down (-1), on what the model lists; null when it cannot move that
|
|
7
8
|
// way: a null effort (the session's), a model without efforts, or no listed level in that direction. The model's
|
|
@@ -31,6 +32,14 @@ export function modeRoute(config, mode, tier, activity = null) {
|
|
|
31
32
|
const effort = steppedEffort(config, route, -1);
|
|
32
33
|
return effort ? { model: route.model, effort } : route;
|
|
33
34
|
}
|
|
35
|
+
// Haiku's documented xhigh empty-answer risk is not a provider-wide effort limit. Preserve explicit higher
|
|
36
|
+
// efforts and session effort; only the automatic promotion prefers High, except medium bounded ops.
|
|
37
|
+
const model = config.models[route.model];
|
|
38
|
+
if (canonicalModel(model.id).replace(/-\d{8}$/, '') === 'claude-haiku-5-5' && route.effort) {
|
|
39
|
+
if (['xhigh', 'max'].includes(route.effort)) return route;
|
|
40
|
+
if (!(tier === 'medium' && activity === 'ops') && model.efforts.includes('high'))
|
|
41
|
+
return { model: route.model, effort: 'high' };
|
|
42
|
+
}
|
|
34
43
|
const effort = steppedEffort(config, route, 1);
|
|
35
44
|
return effort ? { model: route.model, effort } : routeAt(TIERS[Math.min(TIERS.length - 1, rank(tier) + 1)]);
|
|
36
45
|
}
|
|
@@ -50,7 +59,9 @@ export function applyMode(config, routingMode) {
|
|
|
50
59
|
const route = modeRoute(config, mode, tier, activity);
|
|
51
60
|
if (!sameRoute(route, routes[tier])) activities[activity] = { ...activities[activity], [tier]: route };
|
|
52
61
|
}
|
|
53
|
-
|
|
62
|
+
// The sparse effective table can erase an explicit use-tier cell or add an inherited mode preference.
|
|
63
|
+
// Preserve the original declarations so phase floors never override an explicit user cell.
|
|
64
|
+
return { ...config, routes, activities, configuredActivities: config.activities, policy, routingMode };
|
|
54
65
|
}
|
|
55
66
|
|
|
56
67
|
// The active mode and its source: a session override, then the project file, then the profile router.json.
|
package/lib/panel.mjs
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
import { effortSharesCache, routeEffort } from './cost.mjs';
|
|
18
18
|
import { periodCost, resetText } from './costs.mjs';
|
|
19
19
|
import {
|
|
20
|
+
API_ESTIMATE,
|
|
20
21
|
activityReading,
|
|
21
22
|
baselineName,
|
|
22
23
|
classifierLabel,
|
|
@@ -333,6 +334,7 @@ function nextLines(ui, config, view, actions, columns) {
|
|
|
333
334
|
ACTIVITIES.includes(reading.choice) ? ui.activity(reading.choice) : ui.dim(reading.choice),
|
|
334
335
|
reading.share ? ` ${reading.share}` : '',
|
|
335
336
|
reading.others.length ? ui.dim(` ${reading.others[0]}`) : null,
|
|
337
|
+
view.taskPhase ? ui.dim(` phase ${view.taskPhase} · route activity ${view.activity ?? 'base'}`) : null,
|
|
336
338
|
)
|
|
337
339
|
: null,
|
|
338
340
|
),
|
|
@@ -433,11 +435,11 @@ function planLines(ui, cost, now) {
|
|
|
433
435
|
];
|
|
434
436
|
}
|
|
435
437
|
|
|
436
|
-
//
|
|
438
|
+
// The estimate's billing product is context, never evidence of an invoice or measured quota use.
|
|
437
439
|
function billingWords(cost) {
|
|
438
|
-
if (cost.billing === 'plan') return
|
|
439
|
-
if (cost.billing === 'mixed') return
|
|
440
|
-
return
|
|
440
|
+
if (cost.billing === 'plan') return `${API_ESTIMATE} · plan quota, not a charge`;
|
|
441
|
+
if (cost.billing === 'mixed') return `${API_ESTIMATE} · credits ${costText(cost.creditsUsd, cost.billing)}`;
|
|
442
|
+
return `${API_ESTIMATE} · configured rates, not an invoice`;
|
|
441
443
|
}
|
|
442
444
|
|
|
443
445
|
// SESSION: one total with the subagents, the difference against no routing, the main thread and the subagents as bars
|
|
@@ -631,7 +633,7 @@ function readingDetail(ui, view, metrics) {
|
|
|
631
633
|
ui.head(`INPUT PER REPLY · last ${plural(history.length, 'reply', 'replies')} · lowest to highest`),
|
|
632
634
|
trend,
|
|
633
635
|
ui.text(' '),
|
|
634
|
-
ui.head('ESTIMATES · configured
|
|
636
|
+
ui.head('ESTIMATES · configured API rates'),
|
|
635
637
|
ui.text(`Cache hits saved ${dollars(metrics.cacheBenefit)}`),
|
|
636
638
|
Number.isFinite(view.estimate?.taxUsd) ? ui.text(`Switch cost ${dollars(view.estimate.taxUsd)}`) : null,
|
|
637
639
|
...(view.comparison
|
|
@@ -1200,7 +1202,7 @@ function classifierTab(ui, config, view, _usage, actions, _modelOptions, columns
|
|
|
1200
1202
|
ui.line(ui.head('CLASSIFIER'), ui.dim(' · asked once per new turn · a pick saves at once')),
|
|
1201
1203
|
ui.dim(`${''.padEnd(17)}${'prompt goes to'.padEnd(21)}${'probe'.padEnd(8)}${'p95'.padEnd(10)}status`),
|
|
1202
1204
|
...rows,
|
|
1203
|
-
...dimLines(ui, ' probe:
|
|
1205
|
+
...dimLines(ui, ' probe: historical turn-activity questions, not goal/phase accuracy; p95 from that run', columns),
|
|
1204
1206
|
ui.text(' '),
|
|
1205
1207
|
ui.line(ui.head(active.label.toUpperCase()), ui.dim(' · active')),
|
|
1206
1208
|
ui.line(
|
|
@@ -1213,6 +1215,7 @@ function classifierTab(ui, config, view, _usage, actions, _modelOptions, columns
|
|
|
1213
1215
|
),
|
|
1214
1216
|
ui.dim(`${''.padEnd(12)}past the deadline the turn keeps its model`),
|
|
1215
1217
|
ui.line(ui.cell(12, 'Sends'), `prompt + ${config.context.recentTurns} recent turns → ${where(active)}`),
|
|
1218
|
+
ui.dim(' the bounded active goal and current step also go to the classifier'),
|
|
1216
1219
|
ui.line(
|
|
1217
1220
|
ui.cell(12, 'Health'),
|
|
1218
1221
|
paused
|
|
@@ -1240,7 +1243,7 @@ function sessionAgreement(ui, view) {
|
|
|
1240
1243
|
return [
|
|
1241
1244
|
ui.text(' '),
|
|
1242
1245
|
ui.head('THIS SESSION'),
|
|
1243
|
-
ui.text(`
|
|
1246
|
+
ui.text(`Tool compatibility (not accuracy): ${matched} of ${total} turns (${percent(matched / total)})`),
|
|
1244
1247
|
];
|
|
1245
1248
|
}
|
|
1246
1249
|
|
|
@@ -1316,7 +1319,7 @@ function storeUsage(ui, store, metrics) {
|
|
|
1316
1319
|
out.push(ui.text(`Labelled ${summary.labelled} turns`));
|
|
1317
1320
|
const { matched, total } = summary.agreement;
|
|
1318
1321
|
if (total > 0)
|
|
1319
|
-
out.push(ui.text(`
|
|
1322
|
+
out.push(ui.text(`Tool compatibility (not accuracy): ${matched} of ${total} turns (${percent(matched / total)})`));
|
|
1320
1323
|
const { focus } = summary;
|
|
1321
1324
|
if (focus.total > 0)
|
|
1322
1325
|
out.push(
|
|
@@ -1343,8 +1346,8 @@ function storeUsage(ui, store, metrics) {
|
|
|
1343
1346
|
// The footer as [text, joiner] pieces, packed to the pane: two lines at full width, more in a narrow pane, never a
|
|
1344
1347
|
// line that ends in a joiner.
|
|
1345
1348
|
const SAVINGS_FOOTER = [
|
|
1346
|
-
['
|
|
1347
|
-
['
|
|
1349
|
+
['≈$ = tokens × configured API rates (list/custom)', ' · '],
|
|
1350
|
+
['not invoices or measured quota savings', ' · '],
|
|
1348
1351
|
['answer quality not measured', ''],
|
|
1349
1352
|
];
|
|
1350
1353
|
const FULL_WIDTH = 68;
|
|
@@ -1663,7 +1666,9 @@ function subagentUsage(ui, config, view, actions, period, now, columns, billing)
|
|
|
1663
1666
|
out.push(
|
|
1664
1667
|
ui.text(' '),
|
|
1665
1668
|
...dimLines(ui, ' ■ moved by the router · □ Claude Code’s pick · color: the model', columns),
|
|
1666
|
-
...(routed.length
|
|
1669
|
+
...(routed.length
|
|
1670
|
+
? dimLines(ui, ' vs CC: the same tokens on Claude Code’s pick, at configured API rates', columns)
|
|
1671
|
+
: []),
|
|
1667
1672
|
);
|
|
1668
1673
|
return out;
|
|
1669
1674
|
}
|
|
@@ -2061,7 +2066,7 @@ function costHeadline(ui, cost) {
|
|
|
2061
2066
|
);
|
|
2062
2067
|
return ui.dim(
|
|
2063
2068
|
cost.effortOnly
|
|
2064
|
-
? 'Effort routing on one model: no saving is estimated;
|
|
2069
|
+
? 'Effort routing on one model: no saving is estimated; cost uses configured API rates.'
|
|
2065
2070
|
: `${uncomparedText(cost).replace(/^./, (c) => c.toUpperCase())}: no saving is shown.`,
|
|
2066
2071
|
);
|
|
2067
2072
|
}
|
|
@@ -2072,7 +2077,7 @@ function costTable(ui, config, view, period, cost, legacy, actions, columns) {
|
|
|
2072
2077
|
if (!cost.replies && !cost.agents) return emptyCost(ui, config, view, period);
|
|
2073
2078
|
const compared = cost.diffUsd !== null;
|
|
2074
2079
|
const r = cost.readout;
|
|
2075
|
-
//
|
|
2080
|
+
// All products use configured-rate estimates, including the credits subtotal.
|
|
2076
2081
|
const row = (label, amount, diff, base, style = {}, billing = cost.billing) =>
|
|
2077
2082
|
ui.line(
|
|
2078
2083
|
ui.cell(COST_LABEL, ui.text(label, style)),
|
|
@@ -2130,14 +2135,8 @@ function costTable(ui, config, view, period, cost, legacy, actions, columns) {
|
|
|
2130
2135
|
)
|
|
2131
2136
|
: null,
|
|
2132
2137
|
ui.dim('─'.repeat(COST_LABEL + COST_AMOUNT + (compared ? COST_DIFF + COST_BASE : 0))),
|
|
2133
|
-
row(
|
|
2134
|
-
|
|
2135
|
-
cost.totalUsd,
|
|
2136
|
-
cost.diffUsd,
|
|
2137
|
-
cost.baseUsd,
|
|
2138
|
-
{ bold: true },
|
|
2139
|
-
),
|
|
2140
|
-
cost.billing === 'mixed' ? row(' billed as credits', cost.creditsUsd, null, null, {}, 'billed') : null,
|
|
2138
|
+
row(`${total} · ${API_ESTIMATE}`, cost.totalUsd, cost.diffUsd, cost.baseUsd, { bold: true }),
|
|
2139
|
+
cost.billing === 'mixed' ? row(' credits estimate', cost.creditsUsd, null, null, {}, 'billed') : null,
|
|
2141
2140
|
].filter(Boolean);
|
|
2142
2141
|
if (legacy?.replies)
|
|
2143
2142
|
out.push(
|
|
@@ -2159,8 +2158,8 @@ const COST_BASE = 12;
|
|
|
2159
2158
|
function helpLines(ui) {
|
|
2160
2159
|
return [
|
|
2161
2160
|
ui.head('ABOUT THESE NUMBERS'),
|
|
2162
|
-
ui.dim('
|
|
2163
|
-
ui.dim('
|
|
2161
|
+
ui.dim('≈$ is tokens × configured API rates in router.json (list/custom).'),
|
|
2162
|
+
ui.dim('These are estimates, not invoices or measured plan-quota savings.'),
|
|
2164
2163
|
ui.dim('No routing: your model on the main thread, Claude Code’s pick per agent,'),
|
|
2165
2164
|
ui.dim('same tokens and output length. Answer quality is not measured.'),
|
|
2166
2165
|
ui.dim('For the whole session cost, with side calls such as titles and compaction'),
|
package/lib/route.mjs
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
acceptActivity,
|
|
3
|
+
acceptPhase,
|
|
4
|
+
chooseActivity,
|
|
5
|
+
compareRoutes,
|
|
6
|
+
lateralMove,
|
|
7
|
+
opsWorkActivity,
|
|
8
|
+
routingActivity,
|
|
9
|
+
sameRequest,
|
|
10
|
+
} from './activity.mjs';
|
|
2
11
|
import { ACTIVITIES, rank, resolveRoute, routeSpec, sameRoute, TIERS } from './config.mjs';
|
|
3
12
|
import * as costs from './cost.mjs';
|
|
4
|
-
import { clip, completedSince, extractFacts } from './facts.mjs';
|
|
13
|
+
import { clip, completedSince, extractFacts, stripReminders } from './facts.mjs';
|
|
5
14
|
import { CONTEXT_FILL, decide, fitTier, initialState, sameTaskOf } from './policy.mjs';
|
|
6
15
|
import { canonicalModel } from './providers.mjs';
|
|
7
16
|
|
|
@@ -93,6 +102,7 @@ export function resetHistory(loop) {
|
|
|
93
102
|
resolutions: {},
|
|
94
103
|
historyMeasured: false,
|
|
95
104
|
state: freshState(loop.state),
|
|
105
|
+
...(loop.decision ? { decision: { ...loop.decision, floorActivity: null } } : {}),
|
|
96
106
|
// The active turn keeps its route, pin and fallback suspension; the next turn decides afresh.
|
|
97
107
|
ineligible: [],
|
|
98
108
|
yoursBy: {},
|
|
@@ -113,19 +123,21 @@ export function prepareLoop(loop, messageCount) {
|
|
|
113
123
|
};
|
|
114
124
|
}
|
|
115
125
|
|
|
116
|
-
export function nativeFacts(config, loop, { messages, prompt, effort, turnId, contextTokens }) {
|
|
126
|
+
export function nativeFacts(config, loop, { messages, prompt, effort, turnId, contextTokens, progress = false }) {
|
|
117
127
|
const facts = extractFacts({ messages, output_config: { effort } }, loop, config.context);
|
|
118
|
-
if (typeof prompt === 'string') facts.prompt = clip(prompt, config.context.maxTextChars);
|
|
128
|
+
if (typeof prompt === 'string') facts.prompt = clip(stripReminders(prompt), config.context.maxTextChars);
|
|
119
129
|
// A move down on the same task waits out the dwell and for a checkpoint since the last model change: a passed check,
|
|
120
130
|
// or the success the running route's activity can show (completedSince).
|
|
121
131
|
const since = loop.switchedAt ?? { requests: 0, messageCount: 0 };
|
|
122
132
|
const downAllowed =
|
|
133
|
+
!progress &&
|
|
123
134
|
(loop.requests ?? 0) - since.requests >= config.policy.minDwellRequests &&
|
|
124
135
|
completedSince(messages, since.messageCount, activityOf(config, loop));
|
|
125
136
|
// A loop saved without the field counts as measured, keeping the vote streak.
|
|
126
137
|
return {
|
|
127
138
|
...facts,
|
|
128
139
|
downAllowed,
|
|
140
|
+
...(progress ? { progress: true } : {}),
|
|
129
141
|
pin: null,
|
|
130
142
|
contextTokens,
|
|
131
143
|
turnKey: turnId,
|
|
@@ -162,6 +174,13 @@ function economicConfig(config, loop) {
|
|
|
162
174
|
export function chooseRoute(config, loop, input) {
|
|
163
175
|
const { pin, facts } = input;
|
|
164
176
|
const applied = routeTurn(config, loop, input);
|
|
177
|
+
const floor = opsFloor(config, applied.floorActivity);
|
|
178
|
+
if (
|
|
179
|
+
applied.activity === floor.activity &&
|
|
180
|
+
floor.retained.includes(applied.tier) &&
|
|
181
|
+
sameRoute(applied.route, resolveRoute(config, applied.tier, 'ops'))
|
|
182
|
+
)
|
|
183
|
+
applied.activity = 'ops';
|
|
165
184
|
const kept = pin || !applied.tier;
|
|
166
185
|
return {
|
|
167
186
|
...loop,
|
|
@@ -170,20 +189,51 @@ export function chooseRoute(config, loop, input) {
|
|
|
170
189
|
lastActivity: kept ? activityOf(config, loop) : applied.activity,
|
|
171
190
|
route: kept ? (loop.route ?? null) : applied.route,
|
|
172
191
|
turnId: facts.turnKey,
|
|
173
|
-
decision:
|
|
192
|
+
decision: {
|
|
193
|
+
...applied,
|
|
194
|
+
goal: !pin && config.activityRouting === 'on' ? acceptActivity(config, input.advice) : null,
|
|
195
|
+
taskPhase: !pin && config.activityRouting === 'on' ? acceptPhase(config, input.advice) : null,
|
|
196
|
+
},
|
|
174
197
|
suspended: false,
|
|
175
198
|
reviewedAt: loop.requests ?? 0,
|
|
176
199
|
};
|
|
177
200
|
}
|
|
178
201
|
|
|
202
|
+
// An absent engineering cell inherits the generic row, which is not necessarily a floor for a custom ops row.
|
|
203
|
+
// Protect only a declared weaker inherited preference; explicit cells (even use-tier) and unknown strength stay
|
|
204
|
+
// authoritative. Project per tier before any price/fit/gate calculation, then report a retained ops cell as ops.
|
|
205
|
+
function opsFloor(config, activity) {
|
|
206
|
+
const retained = [];
|
|
207
|
+
if (config.activityRouting !== 'on' || !activity) return { config, activity, retained };
|
|
208
|
+
const declared = config.configuredActivities ?? config.activities;
|
|
209
|
+
const cells = { ...config.activities[activity] };
|
|
210
|
+
for (const tier of TIERS) {
|
|
211
|
+
if (declared[activity]?.[tier]) continue;
|
|
212
|
+
const ops = resolveRoute(config, tier, 'ops');
|
|
213
|
+
if (!(compareRoutes(config, resolveRoute(config, tier, activity), ops) < 0)) continue;
|
|
214
|
+
cells[tier] = ops;
|
|
215
|
+
retained.push(tier);
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
config: retained.length
|
|
219
|
+
? { ...config, configuredActivities: declared, activities: { ...config.activities, [activity]: cells } }
|
|
220
|
+
: config,
|
|
221
|
+
activity,
|
|
222
|
+
retained,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
179
226
|
// The activity of the route running now. Outside `on` there is none, even one a turn in `on` left in the loop before
|
|
180
227
|
// the mode changed: `off` is the 1.5 decision.
|
|
181
228
|
const activityOf = (config, loop) => (config.activityRouting === 'on' ? (loop.lastActivity ?? null) : null);
|
|
182
229
|
|
|
183
230
|
// One decision (plan §5.2) for the cell `{ tier, activity }` it lands on. Outside `on` the activity is null throughout.
|
|
184
231
|
function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown, availableModels, now }) {
|
|
185
|
-
const
|
|
186
|
-
|
|
232
|
+
const last = {
|
|
233
|
+
tier: loop.lastRoute,
|
|
234
|
+
activity: activityOf(config, loop),
|
|
235
|
+
floorActivity: config.activityRouting === 'on' ? (loop.decision?.floorActivity ?? null) : null,
|
|
236
|
+
};
|
|
187
237
|
const tier0 = TIERS.includes(facts.lastRoute) ? facts.lastRoute : config.baselineTier;
|
|
188
238
|
// The incumbent runs the route in use, not the one config gives its cell now. It is rebuilt from config for a loop
|
|
189
239
|
// from before 1.15, a route whose model left the config or can no longer run, and a route kept for an activity that
|
|
@@ -198,8 +248,12 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
198
248
|
const incumbent = {
|
|
199
249
|
tier: tier0,
|
|
200
250
|
activity: last.activity,
|
|
251
|
+
floorActivity: last.floorActivity,
|
|
201
252
|
route: inUse ?? resolveRoute(config, tier0, last.activity),
|
|
202
253
|
};
|
|
254
|
+
let result = chooseCell(config, loop, { facts, advice, pin, now }, incumbent);
|
|
255
|
+
// Restoring last/native provenance can change the floor again, so each candidate resolves its own projection.
|
|
256
|
+
const preferredRoute = (cell) => resolveRoute(opsFloor(config, cell.floorActivity).config, cell.tier, cell.activity);
|
|
203
257
|
// A decision that stays on the incumbent's cell keeps running its route; only a taken lateral move or a pin runs the
|
|
204
258
|
// cell's route from config. So a mode change reaches the request only through the gates.
|
|
205
259
|
const stays = (cell) =>
|
|
@@ -207,21 +261,8 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
207
261
|
(cell.activity ?? null) === incumbent.activity &&
|
|
208
262
|
cell.lateral !== 'taken' &&
|
|
209
263
|
cell.reason !== 'pinned';
|
|
210
|
-
const routeOf = (cell) => (stays(cell) ? incumbent.route :
|
|
264
|
+
const routeOf = (cell) => (stays(cell) ? incumbent.route : preferredRoute(cell));
|
|
211
265
|
const specOf = (cell) => routeSpec(config, routeOf(cell));
|
|
212
|
-
const activity =
|
|
213
|
-
pin || config.activityRouting !== 'on' ? null : chooseActivity(config, advice, incumbent, facts.effort, facts);
|
|
214
|
-
let result =
|
|
215
|
-
pin && TIERS.includes(pin)
|
|
216
|
-
? {
|
|
217
|
-
tier: pin,
|
|
218
|
-
activity: null,
|
|
219
|
-
reason: 'pinned',
|
|
220
|
-
lateral: null,
|
|
221
|
-
escalated: false,
|
|
222
|
-
state: { ...loop.state, turn: loop.state.turn + 1 },
|
|
223
|
-
}
|
|
224
|
-
: decideCell({ config, projected, facts, advice, state: loop.state, now, incumbent, activity });
|
|
225
266
|
// Without a reliable reading of the context, a route with a smaller window than the last one might overflow. Before
|
|
226
267
|
// the first decision the session's own model ran, not the mode's route for its cell.
|
|
227
268
|
const lastSpec =
|
|
@@ -242,19 +283,27 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
242
283
|
const fits = (cell) => runs(cell) && roomFor(cell);
|
|
243
284
|
if (contextKnown) {
|
|
244
285
|
const cell = result;
|
|
245
|
-
const
|
|
286
|
+
const acceptedActivity = cell.floorActivity ?? cell.activity;
|
|
287
|
+
const routeFor = (tier) =>
|
|
288
|
+
tier === cell.tier ? routeOf(cell) : preferredRoute({ ...cell, tier, activity: acceptedActivity });
|
|
246
289
|
const fitted = fitTier(config, result.tier, costs.nextContextTokens(facts), routeFor);
|
|
247
|
-
if (fitted !== result.tier)
|
|
290
|
+
if (fitted !== result.tier)
|
|
291
|
+
result = { ...result, tier: fitted, activity: acceptedActivity, reason: 'context-fit', lateral: null };
|
|
248
292
|
}
|
|
249
293
|
// A lateral outcome describes the cell the lateral rule chose; a context or availability override replaces it.
|
|
250
294
|
if (shrinksUnmeasured(result)) result = { ...result, ...last, reason: 'context-unknown', lateral: null };
|
|
251
295
|
if (!fits(result)) {
|
|
252
296
|
// A cell labelled with another activity would run, and stick, a route this turn did not choose.
|
|
253
297
|
const native = cellForModel(config, nativeModel);
|
|
254
|
-
const
|
|
298
|
+
const acceptedActivity = result.floorActivity ?? result.activity;
|
|
299
|
+
const cells = [
|
|
300
|
+
last,
|
|
301
|
+
native && { ...native, floorActivity: null },
|
|
302
|
+
...TIERS.map((tier) => ({ tier, activity: acceptedActivity, floorActivity: result.floorActivity })),
|
|
303
|
+
];
|
|
255
304
|
const fallback = cells.find(
|
|
256
|
-
(cell) => cell && (cell.activity === null || cell.activity ===
|
|
257
|
-
) ?? { tier: null, activity: null };
|
|
305
|
+
(cell) => cell && (cell.activity === null || cell.activity === acceptedActivity) && fits(cell),
|
|
306
|
+
) ?? { tier: null, activity: null, floorActivity: null };
|
|
258
307
|
// Only the window failed: no route is withheld, the conversation is just too full for every one of them.
|
|
259
308
|
const reason = runs(result) ? 'context-full' : 'model-unavailable';
|
|
260
309
|
result = { ...result, ...fallback, reason, lateral: null };
|
|
@@ -265,7 +314,9 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
265
314
|
let comparison = null;
|
|
266
315
|
const candidate = pin ?? advice?.choice;
|
|
267
316
|
if (contextKnown && loop.lastRequest && TIERS.includes(candidate) && candidate !== loop.lastRoute) {
|
|
268
|
-
const
|
|
317
|
+
const to = preferredRoute({ ...result, tier: candidate, activity: result.floorActivity ?? result.activity });
|
|
318
|
+
const from = incumbent.route;
|
|
319
|
+
const projected = economicConfig(opsFloor(config, result.floorActivity).config, loop);
|
|
269
320
|
const forecast = costs.switchEconomics(projected, to, from, facts, now);
|
|
270
321
|
comparison = {
|
|
271
322
|
candidate,
|
|
@@ -278,10 +329,59 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
278
329
|
return { ...result, route, model, effort, comparison, pinned: Boolean(pin), requestedPin: pin ?? null };
|
|
279
330
|
}
|
|
280
331
|
|
|
332
|
+
// Nomination carries its source into the gates; only their accepted outcome owns future refit intent.
|
|
333
|
+
function chooseCell(config, loop, { facts, advice, pin, now }, incumbent) {
|
|
334
|
+
if (pin && TIERS.includes(pin))
|
|
335
|
+
return {
|
|
336
|
+
tier: pin,
|
|
337
|
+
activity: null,
|
|
338
|
+
floorActivity: null,
|
|
339
|
+
reason: 'pinned',
|
|
340
|
+
lateral: null,
|
|
341
|
+
escalated: false,
|
|
342
|
+
state: { ...loop.state, turn: loop.state.turn + 1 },
|
|
343
|
+
};
|
|
344
|
+
const work = !pin && config.activityRouting === 'on' ? opsWorkActivity(config, advice) : null;
|
|
345
|
+
const proposal = opsFloor(config, work);
|
|
346
|
+
const selection =
|
|
347
|
+
!pin && config.activityRouting === 'on'
|
|
348
|
+
? chooseActivity(proposal.config, advice, incumbent, facts.effort, facts)
|
|
349
|
+
: { activity: null, floorActivity: null };
|
|
350
|
+
const chosen = opsFloor(config, selection.floorActivity);
|
|
351
|
+
return decideCell({
|
|
352
|
+
config: chosen.config,
|
|
353
|
+
projected: economicConfig(chosen.config, loop),
|
|
354
|
+
facts,
|
|
355
|
+
advice,
|
|
356
|
+
state: loop.state,
|
|
357
|
+
now,
|
|
358
|
+
incumbent,
|
|
359
|
+
activity: selection.floorActivity ?? selection.activity,
|
|
360
|
+
floorActivity: selection.floorActivity,
|
|
361
|
+
phaseDerived: Boolean(
|
|
362
|
+
work &&
|
|
363
|
+
selection.floorActivity === work &&
|
|
364
|
+
incumbent.floorActivity !== work &&
|
|
365
|
+
chosen.retained.includes(incumbent.tier),
|
|
366
|
+
),
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
|
|
281
370
|
// Steps 3-6 of plan §5.2: the tier as `decide` picks it, every candidate priced at its route for `activity` and the
|
|
282
371
|
// incumbent at the route it runs; then, when the tier stays and the route would change, the lateral rule. Equal routes
|
|
283
372
|
// never switch: a label alone decides nothing.
|
|
284
|
-
function decideCell({
|
|
373
|
+
function decideCell({
|
|
374
|
+
config,
|
|
375
|
+
projected,
|
|
376
|
+
facts,
|
|
377
|
+
advice,
|
|
378
|
+
state,
|
|
379
|
+
now,
|
|
380
|
+
incumbent,
|
|
381
|
+
activity,
|
|
382
|
+
floorActivity,
|
|
383
|
+
phaseDerived,
|
|
384
|
+
}) {
|
|
285
385
|
const routeFor = (tier) => resolveRoute(config, tier, activity);
|
|
286
386
|
// Failures move to the lowest tier whose route is stronger than the one running, or another request that does not
|
|
287
387
|
// order against it: after a mode change the next tier's route can be the weaker one.
|
|
@@ -307,15 +407,23 @@ function decideCell({ config, projected, facts, advice, state, now, incumbent, a
|
|
|
307
407
|
const escalated = step.state.escalatedSignature !== state.escalatedSignature;
|
|
308
408
|
const route = routeFor(step.tier);
|
|
309
409
|
// Without a classifier answer the running route stays, whatever the mode's route for the cell, during a hold too.
|
|
310
|
-
if (!advice
|
|
311
|
-
|
|
410
|
+
if (!advice) return { ...step, activity: incumbent.activity, floorActivity, lateral: null, escalated };
|
|
411
|
+
if (step.tier !== incumbent.tier || sameRequest(config, route, incumbent.route, facts.effort))
|
|
412
|
+
return { ...step, activity, floorActivity, lateral: null, escalated };
|
|
312
413
|
const lateral = lateralMove({
|
|
313
414
|
config: projected,
|
|
314
415
|
from: incumbent.route,
|
|
315
416
|
to: route,
|
|
316
417
|
// Returning to the base route needs no evidence, nor does the incumbent's own cell whose configured route changed
|
|
317
|
-
// (a routing mode, a route edit)
|
|
318
|
-
|
|
418
|
+
// (a routing mode, a route edit). A new phase-derived physical projection is not an operator edit, even when
|
|
419
|
+
// its label is unchanged: that proposal still needs the lower of the actual goal/phase supports.
|
|
420
|
+
mass:
|
|
421
|
+
!phaseDerived &&
|
|
422
|
+
(activity === null ||
|
|
423
|
+
activity === incumbent.activity ||
|
|
424
|
+
(floorActivity && floorActivity === incumbent.floorActivity))
|
|
425
|
+
? 1
|
|
426
|
+
: (routingActivity(config, advice)?.mass ?? 0),
|
|
319
427
|
hold: step.reason === 'hold',
|
|
320
428
|
sameTask: sameTaskOf(config, advice, facts),
|
|
321
429
|
facts,
|
|
@@ -326,6 +434,7 @@ function decideCell({ config, projected, facts, advice, state, now, incumbent, a
|
|
|
326
434
|
return {
|
|
327
435
|
...step,
|
|
328
436
|
activity: activityOf,
|
|
437
|
+
floorActivity: lateral.move ? floorActivity : incumbent.floorActivity,
|
|
329
438
|
reason: lateral.reason,
|
|
330
439
|
estimate: lateral.estimate,
|
|
331
440
|
lateral: lateral.move ? 'taken' : 'refused',
|
|
@@ -377,6 +486,7 @@ export function observedMove(config, loop, { observed, facts, contextKnown, now
|
|
|
377
486
|
...decision,
|
|
378
487
|
tier,
|
|
379
488
|
activity: observed,
|
|
489
|
+
floorActivity: null,
|
|
380
490
|
route: to,
|
|
381
491
|
model: routeSpec(config, to).id,
|
|
382
492
|
effort: costs.routeEffort(config, to, facts.effort),
|
|
@@ -389,18 +499,27 @@ export function observedMove(config, loop, { observed, facts, contextKnown, now
|
|
|
389
499
|
// A tool continuation keeps the turn's route and activity; only the context, the model list, or the turn's own tool
|
|
390
500
|
// calls (observedMove) can move it.
|
|
391
501
|
export function continueRoute(
|
|
392
|
-
|
|
502
|
+
current,
|
|
393
503
|
loop,
|
|
394
504
|
{ nativeModel, contextTokens, contextKnown, availableModels, effort, hasImages = false },
|
|
395
505
|
) {
|
|
396
506
|
if (!loop.decision) return loop;
|
|
397
507
|
// A fit to another tier serves this turn only: `loop.route` stays the route of `lastRoute`, as the tier does.
|
|
398
508
|
let decision = loop.decision;
|
|
509
|
+
const work =
|
|
510
|
+
current.activityRouting === 'on' && !decision.pinned && ['debug', 'code'].includes(decision.floorActivity)
|
|
511
|
+
? decision.floorActivity
|
|
512
|
+
: null;
|
|
513
|
+
const floor = opsFloor(current, work);
|
|
514
|
+
const config = floor.config;
|
|
515
|
+
const activity = work ?? decision.activity ?? null;
|
|
516
|
+
const activityOf = (tier) => (floor.retained.includes(tier) ? 'ops' : activity);
|
|
517
|
+
if (!work && decision.floorActivity) decision = { ...decision, floorActivity: null };
|
|
399
518
|
if (contextKnown && decision.tier) {
|
|
400
519
|
// The route the turn runs, which a mode change or a held move can keep apart from config, stays while the context
|
|
401
520
|
// fits it; past that, the routes config gives each tier, its own tier's first.
|
|
402
521
|
const running = runningRoute(config, decision);
|
|
403
|
-
const routeOf = (tier) => resolveRoute(config, tier,
|
|
522
|
+
const routeOf = (tier) => resolveRoute(config, tier, activity);
|
|
404
523
|
const fits = contextTokens <= routeSpec(config, running).contextWindow * CONTEXT_FILL;
|
|
405
524
|
const fitted = fits ? decision.tier : fitTier(config, decision.tier, contextTokens, routeOf);
|
|
406
525
|
if (!fits && !sameRoute(routeOf(fitted), running)) {
|
|
@@ -408,6 +527,7 @@ export function continueRoute(
|
|
|
408
527
|
decision = {
|
|
409
528
|
...decision,
|
|
410
529
|
tier: fitted,
|
|
530
|
+
activity: activityOf(fitted),
|
|
411
531
|
route,
|
|
412
532
|
model: routeSpec(config, route).id,
|
|
413
533
|
effort: costs.routeEffort(config, route, effort),
|
|
@@ -418,7 +538,7 @@ export function continueRoute(
|
|
|
418
538
|
if (hasImages && decision.tier && routeSpec(config, runningRoute(config, decision))?.imageInput === false) {
|
|
419
539
|
const runningWindow = routeSpec(config, runningRoute(config, decision)).contextWindow;
|
|
420
540
|
const tier = [...TIERS].reverse().find((t) => {
|
|
421
|
-
const model = routeSpec(config, resolveRoute(config, t,
|
|
541
|
+
const model = routeSpec(config, resolveRoute(config, t, activity));
|
|
422
542
|
return (
|
|
423
543
|
model.imageInput !== false &&
|
|
424
544
|
isModelAllowed(model.id, availableModels, nativeModel) &&
|
|
@@ -426,10 +546,12 @@ export function continueRoute(
|
|
|
426
546
|
(contextKnown ? contextTokens <= model.contextWindow * CONTEXT_FILL : model.contextWindow >= runningWindow)
|
|
427
547
|
);
|
|
428
548
|
});
|
|
429
|
-
const route = tier ? resolveRoute(config, tier,
|
|
549
|
+
const route = tier ? resolveRoute(config, tier, activity) : null;
|
|
430
550
|
decision = {
|
|
431
551
|
...decision,
|
|
432
552
|
tier: tier ?? null,
|
|
553
|
+
activity: tier ? activityOf(tier) : null,
|
|
554
|
+
floorActivity: tier ? work : null,
|
|
433
555
|
route,
|
|
434
556
|
model: route ? routeSpec(config, route).id : nativeModel,
|
|
435
557
|
effort: route ? costs.routeEffort(config, route, effort) : effort,
|
|
@@ -441,6 +563,7 @@ export function continueRoute(
|
|
|
441
563
|
...decision,
|
|
442
564
|
tier: null,
|
|
443
565
|
activity: null,
|
|
566
|
+
floorActivity: null,
|
|
444
567
|
route: null,
|
|
445
568
|
model: nativeModel,
|
|
446
569
|
effort,
|
package/lib/subagents.mjs
CHANGED
|
@@ -137,11 +137,11 @@ export function coreModelOf(config, spawn, { envModel = null, force = false, def
|
|
|
137
137
|
return usesEnv ? idOf(envModel) : spawn.parentModel;
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
-
// How the router's model compares with core's: null when they are the same, `up` or `down` by
|
|
141
|
-
// when either
|
|
140
|
+
// How the router's model compares with core's: null when they are the same, `up` or `down` by declared strength,
|
|
141
|
+
// `moved` when either rank is unknown or they are equal. Tariffs do not establish capability.
|
|
142
142
|
export function moveOf(config, routedModel, coreModel) {
|
|
143
143
|
if (!routedModel || !coreModel || routedModel === coreModel) return null;
|
|
144
|
-
const [routed, core] = [routedModel, coreModel].map((id) => modelSpec(config, id)?.
|
|
144
|
+
const [routed, core] = [routedModel, coreModel].map((id) => modelSpec(config, id)?.strength);
|
|
145
145
|
if (!Number.isFinite(routed) || !Number.isFinite(core) || routed === core) return 'moved';
|
|
146
146
|
return routed > core ? 'up' : 'down';
|
|
147
147
|
}
|
package/lib/view.mjs
CHANGED
|
@@ -16,6 +16,9 @@ export function initialView(model) {
|
|
|
16
16
|
// The effort Claude Code sent on the turn's first request, before routing: with nativeModel, "your model".
|
|
17
17
|
nativeEffort: null,
|
|
18
18
|
selectedModel: null,
|
|
19
|
+
// Last recorded completed request handed to the engine, not an attestation of its serialized API request.
|
|
20
|
+
delegatedModel: null,
|
|
21
|
+
delegatedEffort: null,
|
|
19
22
|
actualModel: null,
|
|
20
23
|
tier: null,
|
|
21
24
|
effort: null,
|
|
@@ -36,6 +39,10 @@ export function initialView(model) {
|
|
|
36
39
|
comparison: null,
|
|
37
40
|
probabilities: null,
|
|
38
41
|
activity: null,
|
|
42
|
+
goal: null,
|
|
43
|
+
taskPhase: null,
|
|
44
|
+
phaseChoice: null,
|
|
45
|
+
phaseProbabilities: null,
|
|
39
46
|
activityChoice: null,
|
|
40
47
|
activityProbabilities: null,
|
|
41
48
|
history: [],
|
|
@@ -87,6 +94,10 @@ export const CLEARED_READINGS = {
|
|
|
87
94
|
estimate: null,
|
|
88
95
|
activityChoice: null,
|
|
89
96
|
activityProbabilities: null,
|
|
97
|
+
goal: null,
|
|
98
|
+
taskPhase: null,
|
|
99
|
+
phaseChoice: null,
|
|
100
|
+
phaseProbabilities: null,
|
|
90
101
|
};
|
|
91
102
|
|
|
92
103
|
// The view patch for a tool continuation's route. In `on` the activity label is the route's, which a fallback to the
|
|
@@ -99,7 +110,9 @@ export function continuationView(config, decision, context) {
|
|
|
99
110
|
reason: decision.reason,
|
|
100
111
|
contextTokens: context.tokens,
|
|
101
112
|
contextKnown: context.known,
|
|
102
|
-
...(config.activityRouting === 'on'
|
|
113
|
+
...(config.activityRouting === 'on'
|
|
114
|
+
? { activity: decision.activity ?? null, goal: decision.goal ?? null, taskPhase: decision.taskPhase ?? null }
|
|
115
|
+
: {}),
|
|
103
116
|
};
|
|
104
117
|
}
|
|
105
118
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexeiled/claude-router",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.23.0",
|
|
4
4
|
"description": "Native Claude Code model and effort routing with Anthropic, DeepSeek, z.ai, MiniMax, MiMo and Kimi defaults, custom provider profiles, and classifier advice.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alexei Ledenev",
|
package/types/index.d.ts
CHANGED
|
@@ -192,6 +192,9 @@ declare module 'claude-code' {
|
|
|
192
192
|
nativeEffort?: string | number | null;
|
|
193
193
|
activeTurnId?: string | null;
|
|
194
194
|
selectedModel?: string | null;
|
|
195
|
+
// Last recorded completed engine request, not verified API-transmitted settings.
|
|
196
|
+
delegatedModel?: string | null;
|
|
197
|
+
delegatedEffort?: string | number | null;
|
|
195
198
|
actualModel?: string | null;
|
|
196
199
|
tier?: RouterTier | null;
|
|
197
200
|
effort?: string | number | null;
|
|
@@ -298,8 +301,9 @@ declare module 'claude-code' {
|
|
|
298
301
|
effort: string | number | null;
|
|
299
302
|
estimate?: RouterEstimate | null;
|
|
300
303
|
comparison?: RouterComparison | null;
|
|
301
|
-
// The applied
|
|
304
|
+
// The applied physical cell; a protected ops cell may retain a separate accepted work preference.
|
|
302
305
|
activity?: RouterActivity | null;
|
|
306
|
+
floorActivity?: 'debug' | 'code' | null;
|
|
303
307
|
lateral?: RouterLateral;
|
|
304
308
|
// A failure asked for an escalation this turn, also one the cash gate held or context-fit replaced.
|
|
305
309
|
escalated?: boolean;
|