@alexeiled/claude-router 1.13.0 → 1.15.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 +46 -0
- package/README.md +3 -0
- package/docs/architecture.md +11 -1
- package/docs/configuration.md +6 -2
- package/docs/evaluation.md +8 -7
- package/docs/native-router.md +2 -1
- package/docs/router-band.svg +30 -8
- package/docs/router-pane-now.svg +134 -111
- package/docs/router-pane-policy.svg +47 -43
- package/docs/router-pane-usage-whatif.svg +75 -12
- package/docs/router-pane-usage.svg +79 -13
- package/docs/routing-modes.md +139 -0
- package/docs/user-guide.md +11 -5
- package/hooks/native-router.mjs +170 -23
- package/lib/activity.mjs +8 -2
- package/lib/band.mjs +43 -11
- package/lib/config.mjs +69 -17
- package/lib/cost.mjs +3 -0
- package/lib/display.mjs +21 -2
- package/lib/modes.mjs +98 -0
- package/lib/panel.mjs +338 -45
- package/lib/route.mjs +80 -23
- package/lib/subagents.mjs +182 -64
- package/lib/usage.mjs +32 -20
- package/lib/view.mjs +3 -0
- package/package.json +2 -1
- package/types/index.d.ts +34 -10
package/lib/route.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { chooseActivity, compareRoutes, lateralMove, sameRequest } from './activity.mjs';
|
|
2
|
-
import { ACTIVITIES, rank, resolveRoute, routeSpec, TIERS } from './config.mjs';
|
|
2
|
+
import { ACTIVITIES, rank, resolveRoute, routeSpec, sameRoute, TIERS } from './config.mjs';
|
|
3
3
|
import * as costs from './cost.mjs';
|
|
4
4
|
import { clip, extractFacts, verifiedSince } from './facts.mjs';
|
|
5
|
-
import { CONTEXT_FILL, decide, fitTier, initialState } from './policy.mjs';
|
|
5
|
+
import { CONTEXT_FILL, decide, fitTier, initialState, sameTaskOf } from './policy.mjs';
|
|
6
6
|
|
|
7
7
|
// Exact observed API snapshot, not a rule that merges different dated versions.
|
|
8
8
|
const SNAPSHOTS = { 'claude-haiku-4-5': 'claude-haiku-4-5-20251001' };
|
|
@@ -42,6 +42,9 @@ export function emptyLoop(config, model) {
|
|
|
42
42
|
return {
|
|
43
43
|
lastRoute: cell?.tier ?? config.baselineTier,
|
|
44
44
|
lastActivity: cell?.activity ?? null,
|
|
45
|
+
// The route (models key, effort) the last routed decision ran for `lastRoute`, kept as it ran: a routing mode or
|
|
46
|
+
// a route edit changes the config, never the route in use. Null until the first decision.
|
|
47
|
+
route: null,
|
|
45
48
|
state: initialState(),
|
|
46
49
|
models: {},
|
|
47
50
|
resolutions: {},
|
|
@@ -146,6 +149,7 @@ export function chooseRoute(config, loop, input) {
|
|
|
146
149
|
state: applied.state,
|
|
147
150
|
lastRoute: kept ? loop.lastRoute : applied.tier,
|
|
148
151
|
lastActivity: kept ? activityOf(config, loop) : applied.activity,
|
|
152
|
+
route: kept ? (loop.route ?? null) : applied.route,
|
|
149
153
|
turnId: facts.turnKey,
|
|
150
154
|
decision: applied,
|
|
151
155
|
suspended: false,
|
|
@@ -160,11 +164,32 @@ const activityOf = (config, loop) => (config.activityRouting === 'on' ? (loop.la
|
|
|
160
164
|
// One decision (plan §5.2) for the cell `{ tier, activity }` it lands on. Outside `on` the activity is null throughout.
|
|
161
165
|
function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown, availableModels, now }) {
|
|
162
166
|
const projected = economicConfig(config, loop);
|
|
163
|
-
const routeOf = (cell) => resolveRoute(config, cell.tier, cell.activity);
|
|
164
|
-
const specOf = (cell) => routeSpec(config, routeOf(cell));
|
|
165
167
|
const last = { tier: loop.lastRoute, activity: activityOf(config, loop) };
|
|
166
168
|
const tier0 = TIERS.includes(facts.lastRoute) ? facts.lastRoute : config.baselineTier;
|
|
167
|
-
|
|
169
|
+
// The incumbent runs the route in use, not the one config gives its cell now. It is rebuilt from config for a loop
|
|
170
|
+
// from before 1.15, a route whose model left the config or can no longer run, and a route kept for an activity that
|
|
171
|
+
// no longer applies (activity routing turned off).
|
|
172
|
+
const usable = (route) => {
|
|
173
|
+
const model = Object.hasOwn(config.models, route.model) ? config.models[route.model] : null;
|
|
174
|
+
return Boolean(
|
|
175
|
+
model && isModelAllowed(model.id, availableModels, nativeModel) && !loop.ineligible.includes(model.id),
|
|
176
|
+
);
|
|
177
|
+
};
|
|
178
|
+
const inUse = loop.route && usable(loop.route) && (loop.lastActivity ?? null) === last.activity ? loop.route : null;
|
|
179
|
+
const incumbent = {
|
|
180
|
+
tier: tier0,
|
|
181
|
+
activity: last.activity,
|
|
182
|
+
route: inUse ?? resolveRoute(config, tier0, last.activity),
|
|
183
|
+
};
|
|
184
|
+
// A decision that stays on the incumbent's cell keeps running its route; only a taken lateral move or a pin runs the
|
|
185
|
+
// cell's route from config. So a mode change reaches the request only through the gates.
|
|
186
|
+
const stays = (cell) =>
|
|
187
|
+
cell.tier === incumbent.tier &&
|
|
188
|
+
(cell.activity ?? null) === incumbent.activity &&
|
|
189
|
+
cell.lateral !== 'taken' &&
|
|
190
|
+
cell.reason !== 'pinned';
|
|
191
|
+
const routeOf = (cell) => (stays(cell) ? incumbent.route : resolveRoute(config, cell.tier, cell.activity));
|
|
192
|
+
const specOf = (cell) => routeSpec(config, routeOf(cell));
|
|
168
193
|
const activity =
|
|
169
194
|
pin || config.activityRouting !== 'on' ? null : chooseActivity(config, advice, incumbent, facts.effort, facts);
|
|
170
195
|
let result =
|
|
@@ -178,9 +203,12 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
178
203
|
state: { ...loop.state, turn: loop.state.turn + 1 },
|
|
179
204
|
}
|
|
180
205
|
: decideCell({ config, projected, facts, advice, state: loop.state, now, incumbent, activity });
|
|
181
|
-
// Without a reliable reading of the context, a route with a smaller window than the last one might overflow.
|
|
206
|
+
// Without a reliable reading of the context, a route with a smaller window than the last one might overflow. Before
|
|
207
|
+
// the first decision the session's own model ran, not the mode's route for its cell.
|
|
208
|
+
const lastSpec =
|
|
209
|
+
(!loop.decision && Object.values(config.models).find((m) => isSameModel(m.id, nativeModel))) || specOf(last);
|
|
182
210
|
const shrinksUnmeasured = (cell) =>
|
|
183
|
-
(!contextKnown || !loop.historyMeasured) && specOf(cell).contextWindow <
|
|
211
|
+
(!contextKnown || !loop.historyMeasured) && specOf(cell).contextWindow < lastSpec.contextWindow;
|
|
184
212
|
const fits = (cell) => {
|
|
185
213
|
const model = specOf(cell);
|
|
186
214
|
return (
|
|
@@ -191,7 +219,8 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
191
219
|
);
|
|
192
220
|
};
|
|
193
221
|
if (contextKnown) {
|
|
194
|
-
const
|
|
222
|
+
const cell = result;
|
|
223
|
+
const routeFor = (tier) => (tier === cell.tier ? routeOf(cell) : resolveRoute(config, tier, cell.activity));
|
|
195
224
|
const fitted = fitTier(config, result.tier, costs.nextContextTokens(facts), routeFor);
|
|
196
225
|
if (fitted !== result.tier) result = { ...result, tier: fitted, reason: 'context-fit', lateral: null };
|
|
197
226
|
}
|
|
@@ -212,7 +241,7 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
212
241
|
let comparison = null;
|
|
213
242
|
const candidate = pin ?? advice?.choice;
|
|
214
243
|
if (contextKnown && loop.lastRequest && TIERS.includes(candidate) && candidate !== loop.lastRoute) {
|
|
215
|
-
const [to, from] = [resolveRoute(config, candidate, activity),
|
|
244
|
+
const [to, from] = [resolveRoute(config, candidate, activity), incumbent.route];
|
|
216
245
|
const forecast = costs.switchEconomics(projected, to, from, facts, now);
|
|
217
246
|
comparison = {
|
|
218
247
|
candidate,
|
|
@@ -222,7 +251,7 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
222
251
|
outputTokens: loop.lastRequest.outputTokens,
|
|
223
252
|
};
|
|
224
253
|
}
|
|
225
|
-
return { ...result, model, effort, comparison, pinned: Boolean(pin), requestedPin: pin ?? null };
|
|
254
|
+
return { ...result, route, model, effort, comparison, pinned: Boolean(pin), requestedPin: pin ?? null };
|
|
226
255
|
}
|
|
227
256
|
|
|
228
257
|
// Steps 3-6 of plan §5.2: the tier as `decide` picks it, every candidate priced at its route for `activity` and the
|
|
@@ -230,13 +259,13 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
230
259
|
// never switch: a label alone decides nothing.
|
|
231
260
|
function decideCell({ config, projected, facts, advice, state, now, incumbent, activity }) {
|
|
232
261
|
const routeFor = (tier) => resolveRoute(config, tier, activity);
|
|
233
|
-
//
|
|
234
|
-
// order against it
|
|
262
|
+
// Failures move to the lowest tier whose route is stronger than the one running, or another request that does not
|
|
263
|
+
// order against it: after a mode change the next tier's route can be the weaker one.
|
|
235
264
|
const escalates = (t) => {
|
|
236
265
|
const order = compareRoutes(config, routeFor(t), incumbent.route, facts.effort);
|
|
237
266
|
return order > 0 || (order === null && !sameRequest(config, routeFor(t), incumbent.route, facts.effort));
|
|
238
267
|
};
|
|
239
|
-
const escalateTo =
|
|
268
|
+
const escalateTo = (tier) => TIERS.slice(rank(tier) + 1).find(escalates) ?? null;
|
|
240
269
|
const step = decide({
|
|
241
270
|
config: projected,
|
|
242
271
|
facts,
|
|
@@ -253,15 +282,18 @@ function decideCell({ config, projected, facts, advice, state, now, incumbent, a
|
|
|
253
282
|
// context or availability override keeps the flag.
|
|
254
283
|
const escalated = step.state.escalatedSignature !== state.escalatedSignature;
|
|
255
284
|
const route = routeFor(step.tier);
|
|
256
|
-
|
|
285
|
+
// Without a classifier answer the running route stays, whatever the mode's route for the cell, during a hold too.
|
|
286
|
+
if (!advice || step.tier !== incumbent.tier || sameRequest(config, route, incumbent.route, facts.effort))
|
|
257
287
|
return { ...step, activity, lateral: null, escalated };
|
|
258
288
|
const lateral = lateralMove({
|
|
259
289
|
config: projected,
|
|
260
290
|
from: incumbent.route,
|
|
261
291
|
to: route,
|
|
262
|
-
// Returning to the base route needs no evidence
|
|
263
|
-
|
|
292
|
+
// Returning to the base route needs no evidence, nor does the incumbent's own cell whose configured route changed
|
|
293
|
+
// (a routing mode, a route edit): the label is not what moves.
|
|
294
|
+
mass: activity === null || activity === incumbent.activity ? 1 : (advice?.activity?.probabilities?.[activity] ?? 0),
|
|
264
295
|
hold: step.reason === 'hold',
|
|
296
|
+
sameTask: sameTaskOf(config, advice, facts),
|
|
265
297
|
facts,
|
|
266
298
|
now,
|
|
267
299
|
costs,
|
|
@@ -277,11 +309,17 @@ function decideCell({ config, projected, facts, advice, state, now, incumbent, a
|
|
|
277
309
|
};
|
|
278
310
|
}
|
|
279
311
|
|
|
312
|
+
// The route a decision runs: the one it recorded, or for a decision from before 1.15 its cell's route in config.
|
|
313
|
+
const runningRoute = (config, decision) =>
|
|
314
|
+
decision.route && Object.hasOwn(config.models, decision.route.model)
|
|
315
|
+
? decision.route
|
|
316
|
+
: resolveRoute(config, decision.tier, decision.activity ?? null);
|
|
317
|
+
|
|
280
318
|
// Whether some activity's route at the decision's tier is stronger than the one running: only then can the turn's own
|
|
281
319
|
// tool calls move it (observedMove), so a step on the strongest route skips reading the transcript.
|
|
282
320
|
export function canMoveUp(config, decision, effort) {
|
|
283
321
|
if (config.activityRouting !== 'on' || !decision?.tier || decision.pinned) return false;
|
|
284
|
-
const from =
|
|
322
|
+
const from = runningRoute(config, decision);
|
|
285
323
|
return ACTIVITIES.some((a) => compareRoutes(config, resolveRoute(config, decision.tier, a), from, effort) > 0);
|
|
286
324
|
}
|
|
287
325
|
|
|
@@ -294,7 +332,7 @@ export function canMoveUp(config, decision, effort) {
|
|
|
294
332
|
export function observedMove(config, loop, { observed, facts, contextKnown, now }) {
|
|
295
333
|
const decision = loop.decision;
|
|
296
334
|
if (!canMoveUp(config, decision, facts.effort) || !ACTIVITIES.includes(observed)) return loop;
|
|
297
|
-
const from =
|
|
335
|
+
const from = runningRoute(config, decision);
|
|
298
336
|
// The gates price the route that will run: the one the context fits, not the tier's first choice.
|
|
299
337
|
const routeFor = (tier) => resolveRoute(config, tier, observed);
|
|
300
338
|
const tier = fitTier(config, decision.tier, costs.nextContextTokens(facts), routeFor);
|
|
@@ -309,10 +347,13 @@ export function observedMove(config, loop, { observed, facts, contextKnown, now
|
|
|
309
347
|
return {
|
|
310
348
|
...loop,
|
|
311
349
|
lastActivity: observed,
|
|
350
|
+
// The route in use belongs to `lastRoute`: a move the context fitted to another tier leaves it to be rebuilt.
|
|
351
|
+
route: tier === loop.lastRoute ? to : null,
|
|
312
352
|
decision: {
|
|
313
353
|
...decision,
|
|
314
354
|
tier,
|
|
315
355
|
activity: observed,
|
|
356
|
+
route: to,
|
|
316
357
|
model: routeSpec(config, to).id,
|
|
317
358
|
effort: costs.routeEffort(config, to, facts.effort),
|
|
318
359
|
reason: lateral.reason,
|
|
@@ -325,21 +366,37 @@ export function observedMove(config, loop, { observed, facts, contextKnown, now
|
|
|
325
366
|
// calls (observedMove) can move it.
|
|
326
367
|
export function continueRoute(config, loop, { nativeModel, contextTokens, contextKnown, availableModels, effort }) {
|
|
327
368
|
if (!loop.decision) return loop;
|
|
369
|
+
// A fit to another tier serves this turn only: `loop.route` stays the route of `lastRoute`, as the tier does.
|
|
328
370
|
let decision = loop.decision;
|
|
329
371
|
if (contextKnown && decision.tier) {
|
|
372
|
+
// The route the turn runs, which a mode change or a held move can keep apart from config, stays while the context
|
|
373
|
+
// fits it; past that, the routes config gives each tier, its own tier's first.
|
|
374
|
+
const running = runningRoute(config, decision);
|
|
330
375
|
const routeOf = (tier) => resolveRoute(config, tier, decision.activity ?? null);
|
|
331
|
-
const
|
|
332
|
-
|
|
376
|
+
const fits = contextTokens <= routeSpec(config, running).contextWindow * CONTEXT_FILL;
|
|
377
|
+
const fitted = fits ? decision.tier : fitTier(config, decision.tier, contextTokens, routeOf);
|
|
378
|
+
if (!fits && !sameRoute(routeOf(fitted), running)) {
|
|
379
|
+
const route = routeOf(fitted);
|
|
333
380
|
decision = {
|
|
334
381
|
...decision,
|
|
335
382
|
tier: fitted,
|
|
336
|
-
|
|
337
|
-
|
|
383
|
+
route,
|
|
384
|
+
model: routeSpec(config, route).id,
|
|
385
|
+
effort: costs.routeEffort(config, route, effort),
|
|
338
386
|
reason: 'context-fit',
|
|
339
387
|
};
|
|
388
|
+
}
|
|
340
389
|
}
|
|
341
390
|
if (!isModelAllowed(decision.model, availableModels, nativeModel) || loop.ineligible.includes(decision.model)) {
|
|
342
|
-
decision = {
|
|
391
|
+
decision = {
|
|
392
|
+
...decision,
|
|
393
|
+
tier: null,
|
|
394
|
+
activity: null,
|
|
395
|
+
route: null,
|
|
396
|
+
model: nativeModel,
|
|
397
|
+
effort,
|
|
398
|
+
reason: 'model-unavailable',
|
|
399
|
+
};
|
|
343
400
|
}
|
|
344
401
|
return { ...loop, decision };
|
|
345
402
|
}
|
package/lib/subagents.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import { CLASSIFY } from './config.mjs';
|
|
|
6
6
|
import { clip } from './facts.mjs';
|
|
7
7
|
import { afterResetOf, earlierSince, isRecord } from './records.mjs';
|
|
8
8
|
import { modelSpec, requestUsd } from './savings.mjs';
|
|
9
|
+
import { DAY, dayKey, daysBefore, RETAIN_DAYS } from './usage.mjs';
|
|
9
10
|
|
|
10
11
|
export const SUBAGENT_PREFIX = 'subagents:v1:';
|
|
11
12
|
// The longest a spawn waits for the classifier. The wait delays the subagent's start, and the client's deadline race
|
|
@@ -17,6 +18,26 @@ export const RESPAWN_WINDOW_MS = 600_000;
|
|
|
17
18
|
// `unlisted`: a built-in router.json does not list, or a type it lists as null. `pinned`: the agent's definition names a
|
|
18
19
|
// model and does not set `modelRouting: auto`. `unknown`: its definition could not be found or read.
|
|
19
20
|
export const PASS_REASONS = ['fork', 'workflow', 'explicit', 'teammate', 'unlisted', 'pinned', 'unknown'];
|
|
21
|
+
// Each reason as the pane and the band show it.
|
|
22
|
+
export const PASS_LABEL = {
|
|
23
|
+
fork: 'fork',
|
|
24
|
+
workflow: 'workflow agent',
|
|
25
|
+
explicit: 'explicit model',
|
|
26
|
+
teammate: 'teammate',
|
|
27
|
+
unlisted: 'type not routed',
|
|
28
|
+
pinned: 'pinned model',
|
|
29
|
+
unknown: 'no definition',
|
|
30
|
+
};
|
|
31
|
+
// Why Claude Code picked the model for each reason.
|
|
32
|
+
export const PASS_WHY = {
|
|
33
|
+
fork: 'your model',
|
|
34
|
+
workflow: 'the workflow sets it',
|
|
35
|
+
explicit: 'the Agent call sets it',
|
|
36
|
+
teammate: 'teammates keep theirs',
|
|
37
|
+
unlisted: 'not in subagents.types',
|
|
38
|
+
pinned: 'its agent file sets it',
|
|
39
|
+
unknown: 'agent file not found',
|
|
40
|
+
};
|
|
20
41
|
// Built-ins that ignore CLAUDE_CODE_SUBAGENT_MODEL unless CLAUDE_CODE_SUBAGENT_MODEL_FORCE is set.
|
|
21
42
|
const OWN_MODEL_BUILTINS = ['Explore', 'Plan'];
|
|
22
43
|
|
|
@@ -116,11 +137,20 @@ export function coreModelOf(config, spawn, { envModel = null, force = false, def
|
|
|
116
137
|
return usesEnv ? idOf(envModel) : spawn.parentModel;
|
|
117
138
|
}
|
|
118
139
|
|
|
140
|
+
// How the router's model compares with core's: null when they are the same, `up` or `down` by output price, `moved`
|
|
141
|
+
// when either has no price.
|
|
142
|
+
export function moveOf(config, routedModel, coreModel) {
|
|
143
|
+
if (!routedModel || !coreModel || routedModel === coreModel) return null;
|
|
144
|
+
const [routed, core] = [routedModel, coreModel].map((id) => modelSpec(config, id)?.output);
|
|
145
|
+
if (!Number.isFinite(routed) || !Number.isFinite(core) || routed === core) return 'moved';
|
|
146
|
+
return routed > core ? 'up' : 'down';
|
|
147
|
+
}
|
|
148
|
+
|
|
119
149
|
// One spawn's record while its agent runs: what was decided and the priced replies since the last flush. `pass` is a
|
|
120
150
|
// PASS_REASONS entry or null. `choice` is the models key decided, or 'core' when the decision kept core's model.
|
|
121
|
-
// `actualUsd` prices each reply on the model it ran on; `otherUsd`
|
|
122
|
-
// a pass-through.
|
|
123
|
-
export function spawnRecord({ sessionId, spawn, pass, choice, routedModel, coreModel, failed, respawn }) {
|
|
151
|
+
// `move` is moveOf the routed model against core's. `actualUsd` prices each reply on the model it ran on; `otherUsd`
|
|
152
|
+
// the same tokens on core's estimated model, none for a pass-through.
|
|
153
|
+
export function spawnRecord({ sessionId, spawn, pass, choice, routedModel, coreModel, move = null, failed, respawn }) {
|
|
124
154
|
return {
|
|
125
155
|
sessionId,
|
|
126
156
|
type: spawn.subagentType,
|
|
@@ -128,6 +158,7 @@ export function spawnRecord({ sessionId, spawn, pass, choice, routedModel, coreM
|
|
|
128
158
|
choice,
|
|
129
159
|
routedModel,
|
|
130
160
|
coreModel,
|
|
161
|
+
move,
|
|
131
162
|
failed: Boolean(failed),
|
|
132
163
|
respawn: Boolean(respawn),
|
|
133
164
|
counted: false,
|
|
@@ -161,48 +192,70 @@ export function addAgentReply(config, agent, usage, ttl) {
|
|
|
161
192
|
return next;
|
|
162
193
|
}
|
|
163
194
|
|
|
164
|
-
// A record kept across sessions, one session's or their sum, since `since` (ms), the first agent after a reset
|
|
165
|
-
//
|
|
166
|
-
//
|
|
195
|
+
// A record kept across sessions, one session's or their sum, since `since` (ms), the first agent after a reset, by
|
|
196
|
+
// local calendar day like the usage records, so the Usage tab's period applies. A day holds `routed[type]` and
|
|
197
|
+
// `passed[reason]`: agents, requests and the retry proxies of the type, and per model id the agents ran on, its agents
|
|
198
|
+
// and their cost (`models`); a routed model also counts the agents the router moved onto it, `up` or `down` from
|
|
199
|
+
// core's, and core's estimated cost of the same tokens. A version 1 record, from before 1.14.0, has neither days nor
|
|
200
|
+
// models: it reads as `undated` totals, which only the periods without a range (session and all) count.
|
|
167
201
|
export function emptySubagentStore() {
|
|
168
|
-
return { version:
|
|
202
|
+
return { version: 2, since: null, days: {} };
|
|
169
203
|
}
|
|
170
204
|
|
|
171
|
-
const ROUTED_SUMS = ['agents', '
|
|
172
|
-
const PASSED_SUMS = ['agents', 'requests', 'pricedRequests'
|
|
205
|
+
const ROUTED_SUMS = ['agents', 'requests', 'pricedRequests', 'respawns', 'failures'];
|
|
206
|
+
const PASSED_SUMS = ['agents', 'requests', 'pricedRequests'];
|
|
207
|
+
const ROUTED_MODEL_SUMS = ['agents', 'moved', 'up', 'down', 'usd', 'coreUsd'];
|
|
208
|
+
const PASSED_MODEL_SUMS = ['agents', 'usd'];
|
|
209
|
+
const UNDATED_SUMS = ['agents', 'usd'];
|
|
210
|
+
const SUMS = {
|
|
211
|
+
routed: [ROUTED_SUMS, ROUTED_MODEL_SUMS],
|
|
212
|
+
passed: [PASSED_SUMS, PASSED_MODEL_SUMS],
|
|
213
|
+
};
|
|
214
|
+
// A model id the record never got.
|
|
215
|
+
const UNKNOWN_MODEL = 'unknown';
|
|
216
|
+
|
|
217
|
+
const zeros = (sums) => Object.fromEntries(sums.map((sum) => [sum, 0]));
|
|
218
|
+
const emptyCounts = (sums) => ({ ...zeros(sums), models: {} });
|
|
219
|
+
const emptySubagentDay = () => ({ routed: {}, passed: {} });
|
|
173
220
|
|
|
174
|
-
|
|
175
|
-
const
|
|
221
|
+
// A key read from the store, such as a model id `__proto__`, must neither answer from the prototype nor set it.
|
|
222
|
+
const own = (object, key) => (Object.hasOwn(object, key) ? object[key] : undefined);
|
|
223
|
+
const put = (object, key, value) =>
|
|
224
|
+
Object.defineProperty(object, key, { value, enumerable: true, writable: true, configurable: true });
|
|
176
225
|
|
|
177
|
-
// The finished turn's counts of one agent added to a store at `now
|
|
178
|
-
// requests and prices since the last flush.
|
|
226
|
+
// The finished turn's counts of one agent added to a store at `now`, on that day: the agent once, on its first flush,
|
|
227
|
+
// with the model it ran on and the router's move, then its requests and prices since the last flush. Days past
|
|
228
|
+
// RETAIN_DAYS leave the record.
|
|
179
229
|
export function recordAgent(store, agent, now) {
|
|
180
230
|
if (!agent.requests && agent.counted) return store;
|
|
181
231
|
const first = agent.counted ? 0 : 1;
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
counts.usd += agent.actualUsd;
|
|
189
|
-
next.passed[agent.pass] = counts;
|
|
190
|
-
return next;
|
|
191
|
-
}
|
|
192
|
-
const byType = { ...next.routed.on };
|
|
193
|
-
const counts = { ...(byType[agent.type] ?? emptyRouted()), choices: { ...byType[agent.type]?.choices } };
|
|
232
|
+
const days = structuredClone(store.days);
|
|
233
|
+
const day = days[dayKey(now)] ?? emptySubagentDay();
|
|
234
|
+
const kind = agent.pass ? 'passed' : 'routed';
|
|
235
|
+
const key = agent.pass ?? agent.type;
|
|
236
|
+
const [sums, modelSums] = SUMS[kind];
|
|
237
|
+
const counts = own(day[kind], key) ?? emptyCounts(sums);
|
|
194
238
|
counts.agents += first;
|
|
195
|
-
counts.differs += first && agent.routedModel !== agent.coreModel ? 1 : 0;
|
|
196
|
-
counts.respawns += first && agent.respawn ? 1 : 0;
|
|
197
|
-
counts.failures += first && agent.failed ? 1 : 0;
|
|
198
|
-
if (first) counts.choices[agent.choice] = (counts.choices[agent.choice] ?? 0) + 1;
|
|
199
239
|
counts.requests += agent.requests;
|
|
200
240
|
counts.pricedRequests += agent.pricedRequests;
|
|
201
|
-
|
|
202
|
-
counts.
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
241
|
+
const modelId = agent.routedModel || UNKNOWN_MODEL;
|
|
242
|
+
const model = own(counts.models, modelId) ?? zeros(modelSums);
|
|
243
|
+
model.agents += first;
|
|
244
|
+
model.usd += agent.actualUsd;
|
|
245
|
+
if (!agent.pass) {
|
|
246
|
+
counts.respawns += first && agent.respawn ? 1 : 0;
|
|
247
|
+
counts.failures += first && agent.failed ? 1 : 0;
|
|
248
|
+
model.moved += first && agent.move ? 1 : 0;
|
|
249
|
+
model.up += first && agent.move === 'up' ? 1 : 0;
|
|
250
|
+
model.down += first && agent.move === 'down' ? 1 : 0;
|
|
251
|
+
model.coreUsd += agent.otherUsd;
|
|
252
|
+
}
|
|
253
|
+
put(counts.models, modelId, model);
|
|
254
|
+
put(day[kind], key, counts);
|
|
255
|
+
days[dayKey(now)] = day;
|
|
256
|
+
const cutoff = daysBefore(now, RETAIN_DAYS);
|
|
257
|
+
for (const old of Object.keys(days)) if (old < cutoff) delete days[old];
|
|
258
|
+
return { version: 2, since: store.since ?? now, days, ...(store.undated ? { undated: store.undated } : {}) };
|
|
206
259
|
}
|
|
207
260
|
|
|
208
261
|
// The record after a flush: counted once, nothing pending.
|
|
@@ -215,58 +268,123 @@ export const flushedAgent = (agent) => ({
|
|
|
215
268
|
otherUsd: 0,
|
|
216
269
|
});
|
|
217
270
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
271
|
+
const readSums = (value, sums) =>
|
|
272
|
+
isRecord(value) && sums.every((sum) => isCount(value[sum]))
|
|
273
|
+
? Object.fromEntries(sums.map((sum) => [sum, value[sum]]))
|
|
274
|
+
: null;
|
|
275
|
+
|
|
276
|
+
function readCounts(value, [sums, modelSums]) {
|
|
277
|
+
const counts = readSums(value, sums);
|
|
278
|
+
if (!counts || !isRecord(value.models)) return null;
|
|
279
|
+
counts.models = {};
|
|
280
|
+
for (const [id, model] of Object.entries(value.models)) {
|
|
281
|
+
const read = readSums(model, modelSums);
|
|
282
|
+
if (!read) return null;
|
|
283
|
+
put(counts.models, id, read);
|
|
284
|
+
}
|
|
285
|
+
return counts;
|
|
225
286
|
}
|
|
226
287
|
|
|
227
|
-
function
|
|
288
|
+
function readKind(value, kind) {
|
|
228
289
|
const out = {};
|
|
229
290
|
if (!isRecord(value)) return out;
|
|
230
291
|
for (const [key, counts] of Object.entries(value)) {
|
|
231
|
-
const read = readCounts(counts,
|
|
232
|
-
if (read) out
|
|
292
|
+
const read = readCounts(counts, SUMS[kind]);
|
|
293
|
+
if (read) put(out, key, read);
|
|
233
294
|
}
|
|
234
295
|
return out;
|
|
235
296
|
}
|
|
236
297
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
passed: readMap(value.passed, PASSED_SUMS, false),
|
|
298
|
+
const readDay = (day) => ({ routed: readKind(day.routed, 'routed'), passed: readKind(day.passed, 'passed') });
|
|
299
|
+
|
|
300
|
+
// A version 1 record's agents and their cost on the models they ran on: what is left of it without days or models.
|
|
301
|
+
function undatedOf(value) {
|
|
302
|
+
const out = zeros(UNDATED_SUMS);
|
|
303
|
+
const add = (counts, usd) => {
|
|
304
|
+
if (!isRecord(counts) || !isCount(counts.agents) || !isCount(counts[usd])) return;
|
|
305
|
+
out.agents += counts.agents;
|
|
306
|
+
out.usd += counts[usd];
|
|
247
307
|
};
|
|
308
|
+
for (const counts of Object.values(isRecord(value.routed?.on) ? value.routed.on : {})) add(counts, 'routedUsd');
|
|
309
|
+
for (const counts of Object.values(isRecord(value.passed) ? value.passed : {})) add(counts, 'usd');
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// The stored value with its well-formed days and entries, else an empty store: a missing, hand-edited or unknown value
|
|
314
|
+
// must not break a turn or the pane.
|
|
315
|
+
export function readSubagentStore(value) {
|
|
316
|
+
if (!isRecord(value) || !(value.since === null || Number.isFinite(value.since))) return emptySubagentStore();
|
|
317
|
+
if (value.version === 1) {
|
|
318
|
+
const undated = undatedOf(value);
|
|
319
|
+
return { version: 2, since: value.since, days: {}, ...(undated.agents ? { undated } : {}) };
|
|
320
|
+
}
|
|
321
|
+
if (value.version !== 2 || !isRecord(value.days)) return emptySubagentStore();
|
|
322
|
+
const days = {};
|
|
323
|
+
for (const [key, day] of Object.entries(value.days)) if (DAY.test(key) && isRecord(day)) days[key] = readDay(day);
|
|
324
|
+
const undated = readSums(value.undated, UNDATED_SUMS);
|
|
325
|
+
return { version: 2, since: value.since, days, ...(undated ? { undated } : {}) };
|
|
248
326
|
}
|
|
249
327
|
|
|
250
328
|
// A record whose first agent came before the last reset at `resetAt` (ms) reads as empty.
|
|
251
329
|
export const subagentsAfterReset = (record, resetAt) => afterResetOf(record, resetAt, emptySubagentStore);
|
|
252
330
|
|
|
253
|
-
|
|
254
|
-
for (const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
331
|
+
const addSums = (into, from, sums) => {
|
|
332
|
+
for (const sum of sums) into[sum] += from[sum];
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
function addDay(into, day) {
|
|
336
|
+
for (const kind of ['routed', 'passed']) {
|
|
337
|
+
const [sums, modelSums] = SUMS[kind];
|
|
338
|
+
for (const [key, counts] of Object.entries(day[kind])) {
|
|
339
|
+
const total = own(into[kind], key) ?? emptyCounts(sums);
|
|
340
|
+
addSums(total, counts, sums);
|
|
341
|
+
for (const [id, model] of Object.entries(counts.models)) {
|
|
342
|
+
const sum = own(total.models, id) ?? zeros(modelSums);
|
|
343
|
+
addSums(sum, model, modelSums);
|
|
344
|
+
put(total.models, id, sum);
|
|
345
|
+
}
|
|
346
|
+
put(into[kind], key, total);
|
|
347
|
+
}
|
|
260
348
|
}
|
|
261
349
|
}
|
|
262
350
|
|
|
263
|
-
// Every session's record since the last reset added up, since the earliest.
|
|
351
|
+
// Every session's record since the last reset added up day by day, since the earliest.
|
|
264
352
|
export function mergeSubagentStores(values, resetAt = null) {
|
|
265
353
|
const merged = emptySubagentStore();
|
|
266
354
|
for (const one of values.map((value) => subagentsAfterReset(readSubagentStore(value), resetAt))) {
|
|
267
355
|
merged.since = earlierSince(merged.since, one.since);
|
|
268
|
-
|
|
269
|
-
|
|
356
|
+
for (const [key, day] of Object.entries(one.days)) {
|
|
357
|
+
merged.days[key] ??= emptySubagentDay();
|
|
358
|
+
addDay(merged.days[key], day);
|
|
359
|
+
}
|
|
360
|
+
if (one.undated) {
|
|
361
|
+
merged.undated ??= zeros(UNDATED_SUMS);
|
|
362
|
+
addSums(merged.undated, one.undated, UNDATED_SUMS);
|
|
363
|
+
}
|
|
270
364
|
}
|
|
271
365
|
return merged;
|
|
272
366
|
}
|
|
367
|
+
|
|
368
|
+
// A record's days inside `range` (periodRange in lib/usage.mjs) added up, with the agents per day, oldest first; null
|
|
369
|
+
// ends take the whole record and its undated totals.
|
|
370
|
+
export function summarizeSubagents(record, { from, to }) {
|
|
371
|
+
const total = emptySubagentDay();
|
|
372
|
+
const perDay = [];
|
|
373
|
+
for (const key of Object.keys(record.days).sort()) {
|
|
374
|
+
if ((from !== null && key < from) || (to !== null && key > to)) continue;
|
|
375
|
+
const day = record.days[key];
|
|
376
|
+
addDay(total, day);
|
|
377
|
+
const agents = [...Object.values(day.routed), ...Object.values(day.passed)].reduce((n, c) => n + c.agents, 0);
|
|
378
|
+
perDay.push([key, agents]);
|
|
379
|
+
}
|
|
380
|
+
const whole = from === null && to === null;
|
|
381
|
+
return { ...total, perDay, undated: whole && record.undated ? record.undated : null };
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// A routed type's or a pass-through's totals over its models: agents moved and their direction, routed cost and
|
|
385
|
+
// core's estimate (zero for a pass-through).
|
|
386
|
+
export function modelTotals(counts) {
|
|
387
|
+
const out = zeros(ROUTED_MODEL_SUMS);
|
|
388
|
+
for (const model of Object.values(counts.models)) for (const sum of ROUTED_MODEL_SUMS) out[sum] += model[sum] ?? 0;
|
|
389
|
+
return out;
|
|
390
|
+
}
|