@alexeiled/claude-router 1.15.0 → 1.16.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 +1 -1
- package/docs/architecture.md +9 -5
- package/docs/evaluation.md +15 -11
- package/docs/native-router.md +1 -0
- package/docs/routing-modes.md +8 -4
- package/docs/user-guide.md +14 -3
- package/hooks/native-router.mjs +286 -22
- package/lib/activity-stats.mjs +33 -11
- package/lib/activity.mjs +4 -4
- package/lib/band.mjs +2 -1
- package/lib/config.mjs +6 -3
- package/lib/decisions.mjs +252 -0
- package/lib/display.mjs +30 -4
- package/lib/facts.mjs +125 -4
- package/lib/panel.mjs +3 -0
- package/lib/policy.mjs +7 -4
- package/lib/route.mjs +4 -3
- package/lib/savings.mjs +17 -0
- package/package.json +1 -1
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// Routing decisions kept per session, so a later look can tell why a turn ran where it did: the prompt's origin, the
|
|
2
|
+
// classifier's answer, the tier and activity chosen, the reason, the route, and at the turn's end what its tools did and
|
|
3
|
+
// whether its replies were priced. Counts, labels and routes only, never text. Pure: the hook collects a turn's rows and
|
|
4
|
+
// writes them at turn.complete to the session's own $.store key under DECISIONS_PREFIX.
|
|
5
|
+
|
|
6
|
+
import { afterResetOf, isRecord } from './records.mjs';
|
|
7
|
+
|
|
8
|
+
export const DECISIONS_PREFIX = 'decisions:v1:';
|
|
9
|
+
// The newest rows a session keeps, and the sessions kept: $.store holds 4 MiB for every key together, and a row is about
|
|
10
|
+
// 300 bytes, so the records stay under 400 KB. Upgrade trigger: a review needs more history than this, or the store nears its limit; then roll rows into
|
|
11
|
+
// per-day counts.
|
|
12
|
+
export const MAX_ROWS = 60;
|
|
13
|
+
export const MAX_RECORDS = 20;
|
|
14
|
+
export const RETAIN_MS = 30 * 24 * 60 * 60 * 1000;
|
|
15
|
+
export const ROW_KINDS = ['start', 'review', 'observed', 'end'];
|
|
16
|
+
|
|
17
|
+
// Where a prompt came from, as the engine stamps it (prompt.submit's PromptOrigin.kind). Progress origins report on the
|
|
18
|
+
// running work, so their turn is the same task: the classifier judges the active goal with the report as dialogue, and a
|
|
19
|
+
// cheaper route still waits for the checkpoint. Goal origins are a person's or another agent's request, which may start
|
|
20
|
+
// a new task. Anything else (unclassified, a kind a later engine adds, or none seen) routes as before and leaves the
|
|
21
|
+
// goal as it was.
|
|
22
|
+
export const PROGRESS_ORIGINS = ['task-notification', 'observer', 'observer-activity', 'auto-continuation'];
|
|
23
|
+
export const GOAL_ORIGINS = [
|
|
24
|
+
'composer',
|
|
25
|
+
'bridge',
|
|
26
|
+
'sdk',
|
|
27
|
+
'slack-ping',
|
|
28
|
+
'channel',
|
|
29
|
+
'scheduled-trigger',
|
|
30
|
+
'plugin',
|
|
31
|
+
'peer',
|
|
32
|
+
'peer-send-message',
|
|
33
|
+
'projects-relay',
|
|
34
|
+
'coordinator',
|
|
35
|
+
];
|
|
36
|
+
const ORIGIN = /^[a-z][a-z-]{0,39}$/;
|
|
37
|
+
export const originKind = (origin) =>
|
|
38
|
+
typeof origin?.kind === 'string' && ORIGIN.test(origin.kind) ? origin.kind : 'unknown';
|
|
39
|
+
export const isProgressOrigin = (kind) => PROGRESS_ORIGINS.includes(kind);
|
|
40
|
+
export const isGoalOrigin = (kind) => GOAL_ORIGINS.includes(kind);
|
|
41
|
+
|
|
42
|
+
// Submissions waiting for their turn: a prompt typed or delivered while a turn runs starts its own turn later, or never
|
|
43
|
+
// (one delivered into the running turn). Each waits at most QUEUE_MS, and at most QUEUE_MAX wait.
|
|
44
|
+
export const QUEUE_MS = 10 * 60 * 1000;
|
|
45
|
+
export const QUEUE_MAX = 16;
|
|
46
|
+
const sameText = (a, b) => typeof a === 'string' && typeof b === 'string' && a.trim() === b.trim();
|
|
47
|
+
|
|
48
|
+
// The queue with one more submission, the expired and the oldest past the bound dropped.
|
|
49
|
+
export function queueOrigin(queue, { kind, text, at }) {
|
|
50
|
+
return [...queue.filter((entry) => at - entry.at < QUEUE_MS), { kind, text, at }].slice(-QUEUE_MAX);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// The origin of a turn that starts with `text` at `at`, taken from the queue: the oldest waiting submission with that
|
|
54
|
+
// text. Returns { kind, queue }; kind 'unknown' when none matches.
|
|
55
|
+
export function takeOrigin(queue, text, at) {
|
|
56
|
+
const live = queue.filter((entry) => at - entry.at < QUEUE_MS);
|
|
57
|
+
const i = live.findIndex((entry) => sameText(entry.text, text));
|
|
58
|
+
if (i === -1) return { kind: 'unknown', queue: live };
|
|
59
|
+
return { kind: live[i].kind, queue: [...live.slice(0, i), ...live.slice(i + 1)] };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const round = (n, digits = 2) => (Number.isFinite(n) ? Math.round(n * 10 ** digits) / 10 ** digits : null);
|
|
63
|
+
const label = (value) => (typeof value === 'string' && value.length > 0 && value.length <= 80 ? value : null);
|
|
64
|
+
const count = (n) => (Number.isSafeInteger(n) && n > 0 ? n : 0);
|
|
65
|
+
|
|
66
|
+
// One routing decision as a row: `kind` start (the turn's first step), review (a classifier review applied inside the
|
|
67
|
+
// turn) or observed (a move on the turn's own tool calls). `advice` is the classifier's answer or null, `error` why
|
|
68
|
+
// there is none, `decision` the route chosen (chooseRoute's), `facts` what it was chosen on, `sameTask` sameTaskOf.
|
|
69
|
+
export function decisionRow({
|
|
70
|
+
at,
|
|
71
|
+
kind,
|
|
72
|
+
origin = null,
|
|
73
|
+
mode = null,
|
|
74
|
+
advice = null,
|
|
75
|
+
error = null,
|
|
76
|
+
decision,
|
|
77
|
+
facts = {},
|
|
78
|
+
sameTask = null,
|
|
79
|
+
}) {
|
|
80
|
+
const activity = advice?.activity ?? null;
|
|
81
|
+
const { downAllowed, contextTokens } = /** @type {{ downAllowed?: boolean, contextTokens?: number }} */ (facts);
|
|
82
|
+
return {
|
|
83
|
+
at,
|
|
84
|
+
kind,
|
|
85
|
+
origin: label(origin),
|
|
86
|
+
mode: label(mode),
|
|
87
|
+
cls: label(advice?.choice),
|
|
88
|
+
p: round(advice?.probabilities?.[advice?.choice]),
|
|
89
|
+
cont: round(advice?.continuation),
|
|
90
|
+
act: label(activity?.choice),
|
|
91
|
+
actP: round(activity?.probabilities?.[activity?.choice]),
|
|
92
|
+
err: label(error),
|
|
93
|
+
tier: label(decision?.tier),
|
|
94
|
+
activity: label(decision?.activity),
|
|
95
|
+
reason: label(decision?.reason),
|
|
96
|
+
route: decision?.model ? label(`${decision.model}@${decision.effort ?? 'session'}`) : null,
|
|
97
|
+
lateral: label(decision?.lateral),
|
|
98
|
+
pinned: Boolean(decision?.pinned),
|
|
99
|
+
same: typeof sameTask === 'boolean' ? sameTask : null,
|
|
100
|
+
down: typeof downAllowed === 'boolean' ? downAllowed : null,
|
|
101
|
+
ctx: Number.isFinite(contextTokens) ? Math.round(contextTokens / 1000) : null,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// The turn's end: the bucket its tools fall in (observedActivity), its routed requests, how many were priced, and the
|
|
106
|
+
// unpriced ones by cause (UNPRICED_CAUSES in activity-stats.mjs).
|
|
107
|
+
export function endRow({ at, observed = null, requests = 0, priced = 0, unpriced = {} }) {
|
|
108
|
+
const causes = Object.fromEntries(
|
|
109
|
+
Object.entries(unpriced ?? {}).filter(([cause, n]) => label(cause) && Number.isSafeInteger(n) && n > 0),
|
|
110
|
+
);
|
|
111
|
+
return {
|
|
112
|
+
at,
|
|
113
|
+
kind: 'end',
|
|
114
|
+
observed: label(observed),
|
|
115
|
+
requests: count(requests),
|
|
116
|
+
priced: count(priced),
|
|
117
|
+
unpriced: causes,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// A turn keeps its first row (the start) and its newest others up to MAX_TURN_ROWS: a long turn reviewed every few
|
|
122
|
+
// requests must not push the session's earlier turns out of MAX_ROWS.
|
|
123
|
+
export const MAX_TURN_ROWS = 10;
|
|
124
|
+
export function addTurnRow(rows, row) {
|
|
125
|
+
const next = [...rows, row];
|
|
126
|
+
return next.length > MAX_TURN_ROWS ? [next[0], ...next.slice(-(MAX_TURN_ROWS - 1))] : next;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// What the classifier reads on a progress turn: the active goal as the request, and the report as the newest line of
|
|
130
|
+
// the dialogue, so it judges the work still to do rather than the report's wording. Without a goal it reads the turn as
|
|
131
|
+
// before. `recentTurns` bounds the dialogue as extractFacts does.
|
|
132
|
+
export function progressInput({ prompt, turns }, { goal = null, note = null }, recentTurns) {
|
|
133
|
+
if (!goal || !note) return { prompt, turns };
|
|
134
|
+
return { prompt: goal, turns: [...turns, { role: 'user', text: note }].slice(-recentTurns) };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function emptyDecisions() {
|
|
138
|
+
return { version: 1, since: null, rows: [] };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const nullable = (check) => (v) => v === null || Boolean(check(v));
|
|
142
|
+
const ROW_FIELDS = {
|
|
143
|
+
at: Number.isFinite,
|
|
144
|
+
kind: (v) => ROW_KINDS.includes(v),
|
|
145
|
+
origin: nullable(label),
|
|
146
|
+
mode: nullable(label),
|
|
147
|
+
cls: nullable(label),
|
|
148
|
+
p: nullable(Number.isFinite),
|
|
149
|
+
cont: nullable(Number.isFinite),
|
|
150
|
+
act: nullable(label),
|
|
151
|
+
actP: nullable(Number.isFinite),
|
|
152
|
+
err: nullable(label),
|
|
153
|
+
tier: nullable(label),
|
|
154
|
+
activity: nullable(label),
|
|
155
|
+
reason: nullable(label),
|
|
156
|
+
route: nullable(label),
|
|
157
|
+
lateral: nullable(label),
|
|
158
|
+
pinned: (v) => typeof v === 'boolean',
|
|
159
|
+
same: nullable((v) => typeof v === 'boolean'),
|
|
160
|
+
down: nullable((v) => typeof v === 'boolean'),
|
|
161
|
+
ctx: nullable(Number.isFinite),
|
|
162
|
+
observed: nullable(label),
|
|
163
|
+
requests: Number.isSafeInteger,
|
|
164
|
+
priced: Number.isSafeInteger,
|
|
165
|
+
unpriced: (v) => isRecord(v) && Object.values(v).every((n) => Number.isSafeInteger(n) && n >= 0),
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// A row with only its known fields of the right type, or null when its time or kind does not read. A row a later
|
|
169
|
+
// version wrote keeps what reads.
|
|
170
|
+
function readRow(row) {
|
|
171
|
+
if (!isRecord(row) || !ROW_FIELDS.at(row.at) || !ROW_FIELDS.kind(row.kind)) return null;
|
|
172
|
+
const out = {};
|
|
173
|
+
for (const [field, check] of Object.entries(ROW_FIELDS))
|
|
174
|
+
if (Object.hasOwn(row, field) && check(row[field])) out[field] = row[field];
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// The stored value with the rows that read, else an empty record: a hand edit never breaks a turn.
|
|
179
|
+
export function readDecisions(value) {
|
|
180
|
+
if (!isRecord(value) || value.version !== 1 || !Array.isArray(value.rows)) return emptyDecisions();
|
|
181
|
+
if (!(value.since === null || Number.isFinite(value.since))) return emptyDecisions();
|
|
182
|
+
return { version: 1, since: value.since, rows: value.rows.map(readRow).filter(Boolean).slice(-MAX_ROWS) };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// A record whose first row came before the last reset at `resetAt` (ms) reads as empty.
|
|
186
|
+
export const decisionsAfterReset = (record, resetAt) => afterResetOf(record, resetAt, emptyDecisions);
|
|
187
|
+
|
|
188
|
+
// The record with a finished turn's rows added at `now`, the oldest dropped past MAX_ROWS. No rows: the record itself.
|
|
189
|
+
export function appendDecisions(record, rows, now) {
|
|
190
|
+
if (!rows.length) return record;
|
|
191
|
+
return { version: 1, since: record.since ?? now, rows: [...record.rows, ...rows].slice(-MAX_ROWS) };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Which of the stored `[key, value]` records the store can let go at `now`: those whose newest row is older than
|
|
195
|
+
// RETAIN_MS, then past `max` records the ones whose newest row is oldest. A value that does not read as version 1 keeps
|
|
196
|
+
// its key: a later version may have written it.
|
|
197
|
+
export function prunedDecisions(entries, now, max = MAX_RECORDS) {
|
|
198
|
+
const dated = entries
|
|
199
|
+
.filter(([, value]) => isRecord(value) && value.version === 1)
|
|
200
|
+
.map(([key, value]) => {
|
|
201
|
+
const record = readDecisions(value);
|
|
202
|
+
return [key, record.rows.at(-1)?.at ?? record.since ?? 0];
|
|
203
|
+
});
|
|
204
|
+
const gone = new Set(dated.filter(([, last]) => now - last > RETAIN_MS).map(([key]) => key));
|
|
205
|
+
const kept = dated.filter(([key]) => !gone.has(key)).sort((a, b) => b[1] - a[1]);
|
|
206
|
+
for (const [key] of kept.slice(max)) gone.add(key);
|
|
207
|
+
return gone;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const pad2 = (n) => String(n).padStart(2, '0');
|
|
211
|
+
const clock = (ms) => {
|
|
212
|
+
const d = new Date(ms);
|
|
213
|
+
return `${pad2(d.getHours())}:${pad2(d.getMinutes())}:${pad2(d.getSeconds())}`;
|
|
214
|
+
};
|
|
215
|
+
const share = (p) => (Number.isFinite(p) ? ` ${Math.round(p * 100)}%` : '');
|
|
216
|
+
|
|
217
|
+
function endLine(row) {
|
|
218
|
+
const unpriced = Object.entries(row.unpriced ?? {}).map(([cause, k]) => `${cause} ${k}`);
|
|
219
|
+
return [
|
|
220
|
+
clock(row.at),
|
|
221
|
+
'end ',
|
|
222
|
+
`tools ${row.observed ?? '?'}`,
|
|
223
|
+
`${row.requests} request${row.requests === 1 ? '' : 's'}`,
|
|
224
|
+
`${row.priced} priced`,
|
|
225
|
+
unpriced.length ? `unpriced ${unpriced.join(', ')}` : null,
|
|
226
|
+
]
|
|
227
|
+
.filter(Boolean)
|
|
228
|
+
.join(' ');
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// The newest `n` rows as lines for `/router decisions`, oldest first.
|
|
232
|
+
export function decisionLines(rows, n = 20) {
|
|
233
|
+
if (!rows.length) return ['No routing decisions recorded in this session yet.'];
|
|
234
|
+
return rows.slice(-n).map((row) => {
|
|
235
|
+
if (row.kind === 'end') return endLine(row);
|
|
236
|
+
const activity = row.act ? ` ${row.act}${share(row.actP)}` : '';
|
|
237
|
+
const asked = row.cls ? `${row.cls}${share(row.p)}${activity}` : (row.err ?? 'no answer');
|
|
238
|
+
const task = row.same === true ? 'same task' : row.same === false ? 'new task' : null;
|
|
239
|
+
return [
|
|
240
|
+
clock(row.at),
|
|
241
|
+
row.kind.padEnd(8),
|
|
242
|
+
row.origin ?? 'unknown',
|
|
243
|
+
`asked ${asked}`,
|
|
244
|
+
task,
|
|
245
|
+
`-> ${row.tier ?? '-'}${row.activity ? ` ${row.activity}` : ''} ${row.route ?? ''}`.trimEnd(),
|
|
246
|
+
`(${row.reason ?? '?'})`,
|
|
247
|
+
row.pinned ? 'pinned' : null,
|
|
248
|
+
]
|
|
249
|
+
.filter(Boolean)
|
|
250
|
+
.join(' ');
|
|
251
|
+
});
|
|
252
|
+
}
|
package/lib/display.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { acceptActivity } from './activity.mjs';
|
|
2
|
-
import { readStore, storeSummary } from './activity-stats.mjs';
|
|
2
|
+
import { readMetrics, readStore, storeSummary, UNPRICED_CAUSES } from './activity-stats.mjs';
|
|
3
3
|
import {
|
|
4
4
|
activeClassifier,
|
|
5
5
|
CLASSIFIER_OPTIONS,
|
|
@@ -7,9 +7,10 @@ import {
|
|
|
7
7
|
resolveRoute,
|
|
8
8
|
routeSpec,
|
|
9
9
|
sameRoute,
|
|
10
|
+
TIERS,
|
|
10
11
|
} from './config.mjs';
|
|
11
12
|
import { ratesAt } from './cost.mjs';
|
|
12
|
-
import { applyMode, modeLabel } from './modes.mjs';
|
|
13
|
+
import { applyMode, modeLabel, modeRoute } from './modes.mjs';
|
|
13
14
|
import { PROBE_RESULTS } from './probe-results.mjs';
|
|
14
15
|
import { cellForModel } from './route.mjs';
|
|
15
16
|
import { emptyTotals, readout } from './savings.mjs';
|
|
@@ -30,6 +31,31 @@ export const shownConfig = (config, view) => applyMode(config, routingModeOf(vie
|
|
|
30
31
|
// `cost (session)`; the status band and the Now tab show it while routing is on.
|
|
31
32
|
export const routingModeText = (view) => modeLabel(routingModeOf(view));
|
|
32
33
|
|
|
34
|
+
// What the routing mode changed on the cell the band shows, against its configured route: 'effort only' when the mode
|
|
35
|
+
// kept the model and moved the effort. The cost estimates price the same tokens on each route, so an effort change
|
|
36
|
+
// shows no difference there; real output length and billing can differ. Null in balanced, without a tier, or when the
|
|
37
|
+
// mode moved the model or nothing.
|
|
38
|
+
export function modeLever(config, view) {
|
|
39
|
+
const { mode } = routingModeOf(view);
|
|
40
|
+
if (mode === 'balanced' || !TIERS.includes(view.tier)) return null;
|
|
41
|
+
const activity = config.activityRouting === 'on' ? (view.activity ?? null) : null;
|
|
42
|
+
const configured = resolveRoute(config, view.tier, activity);
|
|
43
|
+
const routed = modeRoute(config, mode, view.tier, activity);
|
|
44
|
+
return configured.model === routed.model && !sameRoute(configured, routed) ? 'effort only' : null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The routed replies the usage record left out, by cause (UNPRICED_CAUSES), as one line, or null when there are none.
|
|
48
|
+
const UNPRICED_LABEL = { usage: 'no usage', model: 'no price', ttl: 'cache lifetime unknown', error: 'error' };
|
|
49
|
+
export function unpricedLine(metrics) {
|
|
50
|
+
const { unpriced } = readMetrics(metrics);
|
|
51
|
+
const total = UNPRICED_CAUSES.reduce((n, cause) => n + unpriced[cause], 0);
|
|
52
|
+
if (!total) return null;
|
|
53
|
+
const causes = UNPRICED_CAUSES.filter((cause) => unpriced[cause] > 0).map(
|
|
54
|
+
(cause) => `${UNPRICED_LABEL[cause]} ${unpriced[cause]}`,
|
|
55
|
+
);
|
|
56
|
+
return `Unpriced ${total} routed repl${total === 1 ? 'y' : 'ies'} left out of usage: ${causes.join(' · ')}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
33
59
|
// Why routing starts off: the session's model is no tier's model. Set at session start, read by the band.
|
|
34
60
|
export const NOT_A_TIER_REASON = 'not a routing tier';
|
|
35
61
|
|
|
@@ -74,7 +100,7 @@ export const REASONS = {
|
|
|
74
100
|
downgrade: 'enough support for a cheaper model',
|
|
75
101
|
'upgrade-pending': 'waiting before upgrading',
|
|
76
102
|
'downgrade-pending': 'switching is not justified yet',
|
|
77
|
-
'downgrade-held': 'a cheaper model waits for a
|
|
103
|
+
'downgrade-held': 'a cheaper model waits for a checkpoint on this task',
|
|
78
104
|
hold: 'staying after an escalation',
|
|
79
105
|
escalation: 'repeated tool failures need a stronger model',
|
|
80
106
|
'cash-gate': 'estimated cold cache write exceeds the cap',
|
|
@@ -144,7 +170,7 @@ export const SHORT_REASONS = {
|
|
|
144
170
|
downgrade: '↓ downgrade',
|
|
145
171
|
'upgrade-pending': '… waiting to go up',
|
|
146
172
|
'downgrade-pending': '… waiting to go down',
|
|
147
|
-
'downgrade-held': '… down after a
|
|
173
|
+
'downgrade-held': '… down after a checkpoint',
|
|
148
174
|
hold: '= holding',
|
|
149
175
|
escalation: '↑ tool errors',
|
|
150
176
|
'cash-gate': '= cash cap',
|
package/lib/facts.mjs
CHANGED
|
@@ -108,6 +108,12 @@ const SEPARATORS = /&&|\|\||[;|&\n]/;
|
|
|
108
108
|
// find can run or delete what it finds.
|
|
109
109
|
const WRITING_FIND = /\s-(?:exec|execdir|ok|okdir|delete)\b/;
|
|
110
110
|
|
|
111
|
+
// A command as the classifiers read it: quoted text blanked, so a `;` or `>` inside a pattern is not shell syntax, and
|
|
112
|
+
// redirects of a stream into another stream or into /dev/null dropped, as they write no file. Every classifier below
|
|
113
|
+
// reads this one form, so they agree on what a command is.
|
|
114
|
+
const bareCommand = (command) =>
|
|
115
|
+
command.replace(/'[^']*'|"[^"]*"/g, '""').replace(/\d?>\s*&\d|\d?>\s*\/dev\/null/g, '');
|
|
116
|
+
|
|
111
117
|
// The index of the last user prompt, a user message that is not a tool result: where the latest turn starts. -1 when
|
|
112
118
|
// there is none.
|
|
113
119
|
export function promptIndex(messages) {
|
|
@@ -117,7 +123,10 @@ export function promptIndex(messages) {
|
|
|
117
123
|
}
|
|
118
124
|
|
|
119
125
|
// The bucket of a finished turn, from the tool calls the assistant made after the user message at `fromIndex`:
|
|
120
|
-
// code, docs, ops, read or talk. First match wins; only names and paths are read, no text is kept.
|
|
126
|
+
// code, docs, shell, ops, check, read or talk. First match wins; only names and paths are read, no text is kept.
|
|
127
|
+
// `shell` is a candidate, not proven code: a command that writes a file's content (a redirect, `tee`, `sed -i`, a
|
|
128
|
+
// patch), which may be code, docs or routine work. `check` is a turn whose only commands that are not reads are
|
|
129
|
+
// checks (`npm test`, `tsc`): a review or a verification. Routine file commands (`rm`, `mv`, `mkdir`) stay `ops`.
|
|
121
130
|
export function observedActivity(messages, fromIndex) {
|
|
122
131
|
const calls = [];
|
|
123
132
|
for (let i = fromIndex + 1; i < messages.length; i += 1)
|
|
@@ -125,10 +134,55 @@ export function observedActivity(messages, fromIndex) {
|
|
|
125
134
|
for (const block of blocks(messages[i].content)) if (block.type === 'tool_use') calls.push(block);
|
|
126
135
|
const edited = calls.filter((c) => FILE_EDIT_TOOLS.has(c.name));
|
|
127
136
|
if (edited.length > 0) return edited.every((c) => DOC_PATH.test(editPath(c))) ? 'docs' : 'code';
|
|
128
|
-
|
|
137
|
+
const commands = calls.filter((c) => c.name === 'Bash').map((c) => commandKind(c.input?.command));
|
|
138
|
+
if (commands.includes('shell')) return 'shell';
|
|
139
|
+
if (commands.includes('ops')) return 'ops';
|
|
140
|
+
if (commands.includes('check')) return 'check';
|
|
129
141
|
return calls.length > 0 ? 'read' : 'talk';
|
|
130
142
|
}
|
|
131
143
|
|
|
144
|
+
// The activity routing reads from a bucket: `shell` and `check` route as `ops`, the commands they are. The finer
|
|
145
|
+
// buckets serve the agreement counts only, so they change no route.
|
|
146
|
+
export const routingBucket = (bucket) => (bucket === 'shell' || bucket === 'check' ? 'ops' : bucket);
|
|
147
|
+
|
|
148
|
+
// One Bash command as `read`, `shell` (writes a file's content), `check` (a checker whose status is its own) or
|
|
149
|
+
// `ops` (anything else that runs).
|
|
150
|
+
function commandKind(command) {
|
|
151
|
+
if (readsOnly(command)) return 'read';
|
|
152
|
+
if (writesFile(command)) return 'shell';
|
|
153
|
+
return verifies(command) && checksOnly(command) ? 'check' : 'ops';
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Some command of the pipeline or list runs a check, whatever its status reports: `npm test | tail`, `npm test; true`.
|
|
157
|
+
function runsAnyCheck(command) {
|
|
158
|
+
if (typeof command !== 'string') return false;
|
|
159
|
+
return bareCommand(command)
|
|
160
|
+
.split(SEPARATORS)
|
|
161
|
+
.some((part) => part.trim() !== '' && runsCheck(part.trim().split(/\s+/)));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Every step of an `&&` list is a check or a read: `npm test && git commit` runs something besides the check.
|
|
165
|
+
function checksOnly(command) {
|
|
166
|
+
const bare = bareCommand(command);
|
|
167
|
+
return bare.split('&&').every((step) => runsCheck(step.trim().split(/\s+/)) || readsOnly(step));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// A command that writes a file's content: an output redirect to a file, `tee`, an in-place `sed` or `perl`, `patch`
|
|
171
|
+
// or `git apply`. Quoted text is blanked first, as readsOnly does, so a `>` inside a pattern is not a redirect.
|
|
172
|
+
const SHELL_WRITERS = [
|
|
173
|
+
/(?:^|[^<>&\d])\d?>>?\s*[^\s&|>]/,
|
|
174
|
+
/(?:^|[\s;&|(])tee\b/,
|
|
175
|
+
/(?:^|[\s;&|(])sed\b[^;&|]*\s(?:-[a-zA-Z]*i|--in-place)\b/,
|
|
176
|
+
/(?:^|[\s;&|(])perl\b[^;&|]*\s-[a-zA-Z]*i/,
|
|
177
|
+
/(?:^|[\s;&|(])patch\b/,
|
|
178
|
+
/(?:^|[\s;&|(])git\s+apply\b/,
|
|
179
|
+
];
|
|
180
|
+
function writesFile(command) {
|
|
181
|
+
if (typeof command !== 'string') return false;
|
|
182
|
+
const bare = bareCommand(command);
|
|
183
|
+
return SHELL_WRITERS.some((pattern) => pattern.test(bare));
|
|
184
|
+
}
|
|
185
|
+
|
|
132
186
|
function editPath(call) {
|
|
133
187
|
const path = call.input?.file_path ?? call.input?.notebook_path;
|
|
134
188
|
return typeof path === 'string' ? path : '';
|
|
@@ -139,7 +193,7 @@ function editPath(call) {
|
|
|
139
193
|
// a pattern is not taken for shell syntax. Anything unrecognised counts as running something.
|
|
140
194
|
function readsOnly(command) {
|
|
141
195
|
if (typeof command !== 'string' || /`|\$\(/.test(command)) return false;
|
|
142
|
-
const bare = command
|
|
196
|
+
const bare = bareCommand(command);
|
|
143
197
|
if (/[<>]/.test(bare) || WRITING_FIND.test(bare)) return false;
|
|
144
198
|
return bare.split(SEPARATORS).every((part) => {
|
|
145
199
|
if (!part.trim()) return true;
|
|
@@ -184,7 +238,7 @@ const CHECK_WORD =
|
|
|
184
238
|
|
|
185
239
|
function verifies(command) {
|
|
186
240
|
if (typeof command !== 'string') return false;
|
|
187
|
-
const bare = command
|
|
241
|
+
const bare = bareCommand(command);
|
|
188
242
|
const steps = bare.split('&&');
|
|
189
243
|
if (steps.some((step) => /\|\||[;|&\n]/.test(step))) return false;
|
|
190
244
|
return steps.some((step) => runsCheck(step.trim().split(/\s+/)));
|
|
@@ -229,3 +283,70 @@ export function verifiedSince(messages, fromIndex) {
|
|
|
229
283
|
|
|
230
284
|
// What Claude Code's Bash tool returns for a command it moved to the background.
|
|
231
285
|
const BACKGROUNDED = /running in background|moved to the background/i;
|
|
286
|
+
|
|
287
|
+
const DELEGATING = new Set(['Agent', 'Task']);
|
|
288
|
+
const READ_ONLY_ACTIVITIES = new Set(['explore', 'plan', 'review']);
|
|
289
|
+
|
|
290
|
+
// Positive evidence that the work since message `fromIndex` reached a checkpoint, by the activity the running route
|
|
291
|
+
// serves: the checkpoint a move to a cheaper route on the same task waits for. A passed check (verifiedSince) always
|
|
292
|
+
// counts. Without one:
|
|
293
|
+
// - `ops`: the last command that ran something succeeded, and no edit or delegation happened.
|
|
294
|
+
// - `docs`: the last edit succeeded and touched only doc paths, with no other command writing or running.
|
|
295
|
+
// - `explore`, `plan`, `review`: a turn finished since (a newer prompt), nothing was changed or delegated, and the last
|
|
296
|
+
// tool result was not an error.
|
|
297
|
+
// Any other activity, none, or work outside the activity's shape needs the passed check, and so does work where a check
|
|
298
|
+
// ran without passing last: failed, or in a form whose status is not the check's (`npm test && git push`,
|
|
299
|
+
// `npm test > log`, `npm test | tail`, `; `, `||`, the background). The absence of an edit alone is never evidence:
|
|
300
|
+
// each rule asks for a success it can see.
|
|
301
|
+
export function completedSince(messages, fromIndex, activity) {
|
|
302
|
+
if (verifiedSince(messages, fromIndex)) return true;
|
|
303
|
+
const calls = callsSince(messages, fromIndex);
|
|
304
|
+
if (calls.some(({ block }) => DELEGATING.has(block.name))) return false;
|
|
305
|
+
const lastResult = lastResultSince(messages, fromIndex);
|
|
306
|
+
// A check that ran, in any form, and did not pass last (verifiedSince is false) blocks every shortcut.
|
|
307
|
+
if (calls.some(({ block }) => block.name === 'Bash' && runsAnyCheck(block.input?.command))) return false;
|
|
308
|
+
const edits = calls.filter(({ block }) => FILE_EDIT_TOOLS.has(block.name));
|
|
309
|
+
const runs = calls.filter(
|
|
310
|
+
({ block }) => block.name === 'Bash' && ['ops', 'shell'].includes(commandKind(block.input?.command)),
|
|
311
|
+
);
|
|
312
|
+
if (activity === 'ops') {
|
|
313
|
+
if (edits.length > 0 || runs.length === 0) return false;
|
|
314
|
+
return succeeded(messages, runs.at(-1));
|
|
315
|
+
}
|
|
316
|
+
if (activity === 'docs') {
|
|
317
|
+
if (runs.length > 0 || edits.length === 0) return false;
|
|
318
|
+
if (!edits.every(({ block }) => DOC_PATH.test(editPath(block)))) return false;
|
|
319
|
+
return succeeded(messages, edits.at(-1));
|
|
320
|
+
}
|
|
321
|
+
if (READ_ONLY_ACTIVITIES.has(activity)) {
|
|
322
|
+
if (edits.length > 0 || runs.length > 0) return false;
|
|
323
|
+
return promptIndex(messages) >= fromIndex && fromIndex >= 0 && lastResult?.is_error !== true;
|
|
324
|
+
}
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function callsSince(messages, fromIndex) {
|
|
329
|
+
const calls = [];
|
|
330
|
+
for (let i = Math.max(0, fromIndex); i < messages.length; i += 1)
|
|
331
|
+
if (messages[i]?.role === 'assistant')
|
|
332
|
+
for (const block of blocks(messages[i].content)) if (block.type === 'tool_use') calls.push({ block, index: i });
|
|
333
|
+
return calls;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function lastResultSince(messages, fromIndex) {
|
|
337
|
+
let last = null;
|
|
338
|
+
for (let i = Math.max(0, fromIndex); i < messages.length; i += 1)
|
|
339
|
+
if (messages[i]?.role === 'user')
|
|
340
|
+
for (const block of blocks(messages[i].content)) if (block.type === 'tool_result') last = block;
|
|
341
|
+
return last;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Whether the call's result came back without an error, and not as a launch into the background.
|
|
345
|
+
function succeeded(messages, call) {
|
|
346
|
+
if (call.block.input?.run_in_background === true) return false;
|
|
347
|
+
for (let i = call.index + 1; i < messages.length; i += 1)
|
|
348
|
+
for (const block of blocks(messages[i]?.content))
|
|
349
|
+
if (block.type === 'tool_result' && block.tool_use_id === call.block.id)
|
|
350
|
+
return !block.is_error && !BACKGROUNDED.test(textOf(blocks(block.content)));
|
|
351
|
+
return false;
|
|
352
|
+
}
|
package/lib/panel.mjs
CHANGED
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
sparkline,
|
|
42
42
|
TIER_COLOR,
|
|
43
43
|
unavailableReason,
|
|
44
|
+
unpricedLine,
|
|
44
45
|
usageMetrics,
|
|
45
46
|
usd,
|
|
46
47
|
whyText,
|
|
@@ -1220,6 +1221,8 @@ function storeUsage(ui, store, metrics) {
|
|
|
1220
1221
|
out.push(
|
|
1221
1222
|
ui.text(`Escalations after a cheaper activity move: ${escalations} in ${moves} move${moves === 1 ? '' : 's'}`),
|
|
1222
1223
|
);
|
|
1224
|
+
const unpriced = unpricedLine(metrics);
|
|
1225
|
+
if (unpriced) out.push(ui.text(unpriced));
|
|
1223
1226
|
return out;
|
|
1224
1227
|
}
|
|
1225
1228
|
|
package/lib/policy.mjs
CHANGED
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
import { rank, resolveRoute, routeSpec, TIERS } from './config.mjs';
|
|
3
3
|
import * as nativeCosts from './cost.mjs';
|
|
4
4
|
|
|
5
|
-
// Whether an answer is about the task already running: a review inside a turn (facts.midTurn),
|
|
6
|
-
//
|
|
7
|
-
//
|
|
5
|
+
// Whether an answer is about the task already running: a review inside a turn (facts.midTurn), a turn the engine
|
|
6
|
+
// started from progress on the running work (facts.progress: a task notification, an observer's report), or a new
|
|
7
|
+
// prompt the classifier scores as a continuation. On the same task either way is open, but a cheaper route waits for
|
|
8
|
+
// facts.downAllowed: the dwell since the last model change and a checkpoint the activity can show (completedSince).
|
|
8
9
|
export function sameTaskOf(config, advice, facts) {
|
|
9
|
-
return
|
|
10
|
+
return (
|
|
11
|
+
Boolean(facts.midTurn) || Boolean(facts.progress) || (advice?.continuation ?? 0) >= config.policy.continuationMass
|
|
12
|
+
);
|
|
10
13
|
}
|
|
11
14
|
|
|
12
15
|
export function initialState() {
|
package/lib/route.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { chooseActivity, compareRoutes, lateralMove, sameRequest } from './activity.mjs';
|
|
2
2
|
import { ACTIVITIES, rank, resolveRoute, routeSpec, sameRoute, TIERS } from './config.mjs';
|
|
3
3
|
import * as costs from './cost.mjs';
|
|
4
|
-
import { clip,
|
|
4
|
+
import { clip, completedSince, extractFacts } from './facts.mjs';
|
|
5
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.
|
|
@@ -103,11 +103,12 @@ export function prepareLoop(loop, messageCount) {
|
|
|
103
103
|
export function nativeFacts(config, loop, { messages, prompt, effort, turnId, contextTokens }) {
|
|
104
104
|
const facts = extractFacts({ messages, output_config: { effort } }, loop, config.context);
|
|
105
105
|
if (typeof prompt === 'string') facts.prompt = clip(prompt, config.context.maxTextChars);
|
|
106
|
-
// A move down on the same task waits out the dwell and for a
|
|
106
|
+
// A move down on the same task waits out the dwell and for a checkpoint since the last model change: a passed check,
|
|
107
|
+
// or the success the running route's activity can show (completedSince).
|
|
107
108
|
const since = loop.switchedAt ?? { requests: 0, messageCount: 0 };
|
|
108
109
|
const downAllowed =
|
|
109
110
|
(loop.requests ?? 0) - since.requests >= config.policy.minDwellRequests &&
|
|
110
|
-
|
|
111
|
+
completedSince(messages, since.messageCount, activityOf(config, loop));
|
|
111
112
|
// A loop saved without the field counts as measured, keeping the vote streak.
|
|
112
113
|
return {
|
|
113
114
|
...facts,
|
package/lib/savings.mjs
CHANGED
|
@@ -135,6 +135,23 @@ export function compareReply(config, state, { usage, served, yours, ttl, now })
|
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
// Why a routed reply gets no price, by compareReply's checks in its order, or null when it prices it: the usage or its
|
|
139
|
+
// model id is missing or its counts do not read (`usage`; the hook drops a usage without a model before compareReply), the served model or your model has no configured price (`model`), or the cache
|
|
140
|
+
// lifetime is unknown (`ttl`). The usage record keeps priced replies only; this names what it leaves out.
|
|
141
|
+
export function unpricedCause(config, { usage, served, yours, ttl }) {
|
|
142
|
+
const counts = [
|
|
143
|
+
usage?.input_tokens,
|
|
144
|
+
usage?.cache_read_input_tokens,
|
|
145
|
+
usage?.cache_creation_input_tokens,
|
|
146
|
+
usage?.output_tokens,
|
|
147
|
+
];
|
|
148
|
+
if (!usage?.model || !counts.every(isCount)) return 'usage';
|
|
149
|
+
const [servedSpec, yoursSpec] = [modelSpec(config, served.model), modelSpec(config, yours.model)];
|
|
150
|
+
if (!servedSpec || !yoursSpec || !Number.isFinite(servedSpec.output) || !Number.isFinite(yoursSpec.output))
|
|
151
|
+
return 'model';
|
|
152
|
+
return Number.isFinite(config.cache.writeMultiplier[ttl]) ? null : 'ttl';
|
|
153
|
+
}
|
|
154
|
+
|
|
138
155
|
// One main reply against every configured model as yours, at your effort, so the session readout can follow a
|
|
139
156
|
// `/model` change without replaying the session: `states` and `replies` by models key, each as compareReply returns,
|
|
140
157
|
// and `reply`, the prices against `yours`, the API model id /model selects now, or null.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexeiled/claude-router",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.0",
|
|
4
4
|
"description": "Native Claude Code Mod: model and effort routing for the main conversation, advised by Jev, Clef, Clef Flash, Clef Omni, OpenAI or Ollama.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alexei Ledenev",
|