context-doctor 0.51.0 → 0.53.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/README.md +30 -32
- package/dist/cli.js +27 -7
- package/dist/doctor.js +26 -3
- package/dist/fix.d.ts +30 -0
- package/dist/fix.js +51 -5
- package/dist/limits.d.ts +80 -0
- package/dist/limits.js +185 -0
- package/dist/mcp.js +55 -55
- package/dist/modelids.d.ts +44 -0
- package/dist/modelids.js +174 -0
- package/dist/pricecheck.d.ts +11 -0
- package/dist/pricecheck.js +32 -0
- package/dist/pricing.js +12 -3
- package/dist/proxy.d.ts +1 -1
- package/dist/proxy.js +1 -1
- package/dist/sample.d.ts +8 -0
- package/dist/sample.js +86 -0
- package/dist/savings.d.ts +8 -1
- package/dist/savings.js +32 -7
- package/dist/starts.d.ts +39 -0
- package/dist/starts.js +157 -1
- package/package.json +1 -1
package/dist/starts.js
CHANGED
|
@@ -21,12 +21,13 @@
|
|
|
21
21
|
import { readFileSync } from "node:fs";
|
|
22
22
|
import { claudeMcpConfigs } from "./mcpschema.js";
|
|
23
23
|
import { REQUEST_ROWS } from "./coldresume.js";
|
|
24
|
-
import { rowsOf } from "./scan.js";
|
|
24
|
+
import { rowsOf, slimUsage } from "./scan.js";
|
|
25
25
|
import { listSessions, isClaudeSession, replyCounter, textOf } from "./session.js";
|
|
26
26
|
import { subagentFiles, workflowRunOf } from "./subagents.js";
|
|
27
27
|
import { cacheWriteSplit, formatUsd, rateCard, ratesAt, usageCost } from "./pricing.js";
|
|
28
28
|
import { formatTokens, isCodeLike, realModel, tokensForLength } from "./tokens.js";
|
|
29
29
|
import { APP_TOOL_SERVERS } from "./mod/logic.js";
|
|
30
|
+
import { HOST_ROWS } from "./limits.js";
|
|
30
31
|
const CLAUDE = "claude-opus-5";
|
|
31
32
|
/**
|
|
32
33
|
* What a start's measurement takes from a transcript: the entrypoint, the
|
|
@@ -140,6 +141,103 @@ export function mcpServerOf(name) {
|
|
|
140
141
|
const m = /^mcp__(.+?)__/.exec(name);
|
|
141
142
|
return m?.[1];
|
|
142
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* What the reload count reads from a transcript: each compaction boundary,
|
|
146
|
+
* and each assistant row that calls an MCP tool, with the request's usage
|
|
147
|
+
* (the context at that call). Read once with the other extractors (scan.ts).
|
|
148
|
+
*/
|
|
149
|
+
export const RELOAD_ROWS = {
|
|
150
|
+
id: "reloads.1",
|
|
151
|
+
init: () => ({}),
|
|
152
|
+
line(raw, row, out) {
|
|
153
|
+
const boundary = raw.includes('"compact_boundary"');
|
|
154
|
+
if (!boundary && !(raw.includes('"tool_use"') && raw.includes('"name":"mcp__')))
|
|
155
|
+
return;
|
|
156
|
+
let e;
|
|
157
|
+
try {
|
|
158
|
+
e = row();
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (!e || typeof e !== "object")
|
|
164
|
+
return;
|
|
165
|
+
const ts = Date.parse(String(e.timestamp ?? ""));
|
|
166
|
+
if (!Number.isFinite(ts))
|
|
167
|
+
return;
|
|
168
|
+
const sc = e.isSidechain === true ? { sc: 1 } : {};
|
|
169
|
+
if (e.type === "system" && e.subtype === "compact_boundary") {
|
|
170
|
+
out.push({ k: "c", ts, ...sc, ...(typeof e.uuid === "string" ? { u: e.uuid.slice(0, 80) } : {}) });
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const content = e.type === "assistant" ? e.message?.content : undefined;
|
|
174
|
+
if (!Array.isArray(content))
|
|
175
|
+
return;
|
|
176
|
+
const srv = [];
|
|
177
|
+
for (const b of content) {
|
|
178
|
+
const s = b?.type === "tool_use" && typeof b.name === "string" ? mcpServerOf(b.name) : undefined;
|
|
179
|
+
if (s && !srv.includes(s.slice(0, 200)))
|
|
180
|
+
srv.push(s.slice(0, 200));
|
|
181
|
+
}
|
|
182
|
+
if (!srv.length)
|
|
183
|
+
return;
|
|
184
|
+
out.push({ k: "t", ts, ...sc, srv, ...(typeof e.message.model === "string" ? { m: e.message.model.slice(0, 80) } : {}), usage: slimUsage(e.message.usage) });
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* A transcript's compactions in the period, each with the servers called
|
|
189
|
+
* after it (before the next one on the same thread), each server once: its
|
|
190
|
+
* first call after the compaction is the one that would load it again. A
|
|
191
|
+
* compaction already seen in another transcript (a fork's copy) is skipped
|
|
192
|
+
* with the calls after it.
|
|
193
|
+
*/
|
|
194
|
+
function compactionsIn(path, since, seen) {
|
|
195
|
+
const out = [];
|
|
196
|
+
const open = new Map();
|
|
197
|
+
for (const r of rowsOf(path, RELOAD_ROWS)) {
|
|
198
|
+
const th = r.sc ? "1" : "0";
|
|
199
|
+
if (r.k === "c") {
|
|
200
|
+
const copy = r.u !== undefined && seen.has(r.u);
|
|
201
|
+
if (r.u)
|
|
202
|
+
seen.add(r.u);
|
|
203
|
+
if (r.ts < since || copy) {
|
|
204
|
+
open.set(th, undefined);
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const c = { list: [], servers: new Set() };
|
|
208
|
+
out.push(c.list);
|
|
209
|
+
open.set(th, c);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
const c = open.get(th);
|
|
213
|
+
if (!c)
|
|
214
|
+
continue;
|
|
215
|
+
for (const s of r.srv) {
|
|
216
|
+
if (c.servers.has(s))
|
|
217
|
+
continue;
|
|
218
|
+
c.servers.add(s);
|
|
219
|
+
c.list.push({ server: s, at: r.ts, ...(r.m ? { model: r.m } : {}), usage: r.usage });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* One more request at the call's context: all of it read from the cache, and
|
|
226
|
+
* the tool's definition (`defTokens`) written, at the call's own write
|
|
227
|
+
* lifetime and model rates. Undefined when the model has no price.
|
|
228
|
+
*/
|
|
229
|
+
function reloadUsd(r, defTokens) {
|
|
230
|
+
const u = r.usage ?? {};
|
|
231
|
+
const prompt = (Number(u.input_tokens) || 0) + (Number(u.cache_read_input_tokens) || 0) + (Number(u.cache_creation_input_tokens) || 0);
|
|
232
|
+
const card = rateCard(realModel(r.model), u, r.at);
|
|
233
|
+
if (!card)
|
|
234
|
+
return undefined;
|
|
235
|
+
const split = cacheWriteSplit(u);
|
|
236
|
+
const all = split.fiveMinute + split.oneHour + split.twelveHour;
|
|
237
|
+
const def = Math.round(defTokens);
|
|
238
|
+
const oneHour = all > 0 ? Math.round((def * split.oneHour) / all) : 0;
|
|
239
|
+
return usageCost({ input_tokens: 0, cache_read_input_tokens: prompt, cache_creation_input_tokens: def, cache_creation: { ephemeral_5m_input_tokens: def - oneHour, ephemeral_1h_input_tokens: oneHour } }, ratesAt(card, prompt + def)).promptUsd;
|
|
240
|
+
}
|
|
143
241
|
function quantile(sorted, q) {
|
|
144
242
|
return sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * q))];
|
|
145
243
|
}
|
|
@@ -247,6 +345,38 @@ function measureStart(path, kind, since, runRows, parentEntry) {
|
|
|
247
345
|
}
|
|
248
346
|
return start;
|
|
249
347
|
}
|
|
348
|
+
/**
|
|
349
|
+
* Which Claude Code ran the sessions in the period: each entrypoint and
|
|
350
|
+
* version a main transcript recorded while it was in the period (limits.ts
|
|
351
|
+
* HOST_ROWS keeps a record where either changes), counted once per session.
|
|
352
|
+
* fix uses it to say when an install too old for a setting ran none of them.
|
|
353
|
+
*/
|
|
354
|
+
export function sessionHosts(paths, since) {
|
|
355
|
+
const out = new Map();
|
|
356
|
+
for (const path of paths) {
|
|
357
|
+
let hosts;
|
|
358
|
+
try {
|
|
359
|
+
hosts = rowsOf(path, HOST_ROWS);
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
const seen = new Set();
|
|
365
|
+
hosts.forEach((h, j) => {
|
|
366
|
+
// Superseded before the period began (a session resumed after an update): not this period's.
|
|
367
|
+
const next = hosts[j + 1];
|
|
368
|
+
if (next && next.ts > 0 && next.ts < since)
|
|
369
|
+
return;
|
|
370
|
+
const k = `${h.ep ?? ""}\u0000${h.v ?? ""}`;
|
|
371
|
+
if (seen.has(k))
|
|
372
|
+
return;
|
|
373
|
+
seen.add(k);
|
|
374
|
+
const cur = out.get(k) ?? out.set(k, { entrypoint: h.ep ?? "", ...(h.v ? { version: h.v } : {}), sessions: 0 }).get(k);
|
|
375
|
+
cur.sessions++;
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return [...out.values()].sort((a, b) => b.sessions - a.sessions);
|
|
379
|
+
}
|
|
250
380
|
/** Claude Code transcripts written in the last `days` days. */
|
|
251
381
|
function claudeTranscripts(days) {
|
|
252
382
|
const since = Date.now() - days * 86_400_000;
|
|
@@ -256,6 +386,8 @@ function claudeTranscripts(days) {
|
|
|
256
386
|
* Every session's and subagent's start in the period, grouped by entrypoint
|
|
257
387
|
* and subagent type, each priced on every request that re-read it.
|
|
258
388
|
* `paths`: the main transcripts (subagents are found beside each).
|
|
389
|
+
* `compactions`: also count the compactions followed by a call to a server
|
|
390
|
+
* sent whole (fix's desktop-tools-defer; `overhead` does not ask).
|
|
259
391
|
*/
|
|
260
392
|
export function measureStarts(opts = {}) {
|
|
261
393
|
const days = opts.days ?? 30;
|
|
@@ -263,6 +395,17 @@ export function measureStarts(opts = {}) {
|
|
|
263
395
|
const mains = (opts.paths ?? claudeTranscripts(days)).filter((p) => !/[\\/]\.codex[\\/]/.test(p) && !/[\\/]\.gemini[\\/]/.test(p));
|
|
264
396
|
const starts = [];
|
|
265
397
|
const runRows = new Set();
|
|
398
|
+
// Every compaction of the period (sessions and subagents), each with the servers called after it.
|
|
399
|
+
const compactions = [];
|
|
400
|
+
const compactionIds = new Set();
|
|
401
|
+
const reloadsOf = (file) => {
|
|
402
|
+
if (!opts.compactions)
|
|
403
|
+
return;
|
|
404
|
+
try {
|
|
405
|
+
compactions.push(...compactionsIn(file, since, compactionIds));
|
|
406
|
+
}
|
|
407
|
+
catch { /* unreadable: skipped */ }
|
|
408
|
+
};
|
|
266
409
|
for (const path of mains) {
|
|
267
410
|
let s;
|
|
268
411
|
try {
|
|
@@ -273,6 +416,7 @@ export function measureStarts(opts = {}) {
|
|
|
273
416
|
}
|
|
274
417
|
if (s)
|
|
275
418
|
starts.push(s);
|
|
419
|
+
reloadsOf(path);
|
|
276
420
|
let parentEntry = s?.entrypoint;
|
|
277
421
|
if (parentEntry === undefined) {
|
|
278
422
|
try {
|
|
@@ -287,6 +431,7 @@ export function measureStarts(opts = {}) {
|
|
|
287
431
|
starts.push(sub);
|
|
288
432
|
}
|
|
289
433
|
catch { /* unreadable: skipped */ }
|
|
434
|
+
reloadsOf(f.file);
|
|
290
435
|
}
|
|
291
436
|
}
|
|
292
437
|
let configured = new Map();
|
|
@@ -355,6 +500,17 @@ export function measureStarts(opts = {}) {
|
|
|
355
500
|
const usd = desktop.reduce((sum, s) => sum + s.usd * Math.max(0, s.tokens - t) / s.tokens, 0);
|
|
356
501
|
report.gap = { desktop: d, terminal: t, tokens: d - t, usd };
|
|
357
502
|
}
|
|
503
|
+
if (opts.compactions) {
|
|
504
|
+
// A load brings one tool's definition: the server's whole definitions on a start, over its tools.
|
|
505
|
+
const perTool = new Map(startServers(report).map((s) => [s.name, s.tools ? s.tokens / s.tools : 0]));
|
|
506
|
+
const after = compactions
|
|
507
|
+
.map((c) => c.filter((r) => perTool.has(r.server)).map((r) => {
|
|
508
|
+
const usd = reloadUsd(r, perTool.get(r.server));
|
|
509
|
+
return usd === undefined ? { server: r.server, usd: 0, unpriced: true } : { server: r.server, usd };
|
|
510
|
+
}))
|
|
511
|
+
.filter((c) => c.length);
|
|
512
|
+
report.compactions = { total: compactions.length, after };
|
|
513
|
+
}
|
|
358
514
|
return report;
|
|
359
515
|
}
|
|
360
516
|
/** The servers whose tools went whole into some start and that the user's own config holds: `alwaysLoad: false` defers them. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "context-doctor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.53.0",
|
|
4
4
|
"description": "Context and token optimizer for LLM agents: see where Claude Code, Cursor, Codex and Gemini CLI tokens go, then cut them: an every-prompt hook, smarter compaction, an autopilot proxy that edits history only when the prompt cache is cold, RAG chunk packing and an MCP server. Local, no API key. By gAI Ventures.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|