context-doctor 0.49.0 → 0.50.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.
Files changed (50) hide show
  1. package/README.md +16 -14
  2. package/dist/appwindow.d.ts +69 -0
  3. package/dist/appwindow.js +147 -0
  4. package/dist/ccost.d.ts +35 -0
  5. package/dist/ccost.js +105 -5
  6. package/dist/claudedir.d.ts +15 -0
  7. package/dist/claudedir.js +19 -1
  8. package/dist/cli.js +59 -23
  9. package/dist/clihelp.d.ts +5 -1
  10. package/dist/clihelp.js +9 -1
  11. package/dist/codexstatusline.d.ts +176 -0
  12. package/dist/codexstatusline.js +353 -0
  13. package/dist/compactwindow.d.ts +14 -2
  14. package/dist/compactwindow.js +30 -5
  15. package/dist/credits.d.ts +130 -0
  16. package/dist/credits.js +265 -0
  17. package/dist/doctor.d.ts +6 -0
  18. package/dist/doctor.js +15 -1
  19. package/dist/fanout.d.ts +2 -0
  20. package/dist/fanout.js +12 -2
  21. package/dist/fix.d.ts +37 -1
  22. package/dist/fix.js +67 -9
  23. package/dist/install.js +1 -1
  24. package/dist/limits.d.ts +56 -2
  25. package/dist/limits.js +181 -14
  26. package/dist/mcp.d.ts +1 -0
  27. package/dist/mcp.js +106 -100
  28. package/dist/mod/api.d.ts +27 -1
  29. package/dist/mod/api.js +2 -1
  30. package/dist/mod/logic.d.ts +47 -16
  31. package/dist/mod/logic.js +68 -39
  32. package/dist/mod/register.d.ts +6 -0
  33. package/dist/mod/register.js +34 -6
  34. package/dist/modelids.js +2 -2
  35. package/dist/orgrates.d.ts +4 -2
  36. package/dist/orgrates.js +21 -3
  37. package/dist/overhead.js +2 -1
  38. package/dist/pricing.js +12 -0
  39. package/dist/progresssummaries.d.ts +20 -1
  40. package/dist/progresssummaries.js +33 -3
  41. package/dist/proxy.d.ts +1 -1
  42. package/dist/proxy.js +9 -4
  43. package/dist/savings.js +3 -1
  44. package/dist/sidecost.d.ts +65 -0
  45. package/dist/sidecost.js +117 -0
  46. package/dist/spend.d.ts +36 -1
  47. package/dist/spend.js +49 -5
  48. package/dist/starts.d.ts +27 -1
  49. package/dist/starts.js +81 -18
  50. package/package.json +1 -1
package/dist/starts.js CHANGED
@@ -26,6 +26,7 @@ import { listSessions, isClaudeSession, replyCounter, textOf } from "./session.j
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
+ import { APP_TOOL_SERVERS } from "./mod/logic.js";
29
30
  const CLAUDE = "claude-opus-5";
30
31
  /**
31
32
  * What a start's measurement takes from a transcript: the entrypoint, the
@@ -90,6 +91,36 @@ export const START_ROWS = {
90
91
  }
91
92
  },
92
93
  };
94
+ /**
95
+ * The MCP servers a transcript called a tool of, by name only: which starts
96
+ * needed a server whose tools could have waited behind tool search (fix's
97
+ * desktop-tools-defer prices only the starts that never called it).
98
+ */
99
+ export const CALL_ROWS = {
100
+ id: "startcalls.1",
101
+ init: () => ({ seen: [] }),
102
+ line(raw, row, out, st) {
103
+ if (!raw.includes('"tool_use"') || !raw.includes('"name":"mcp__'))
104
+ return;
105
+ let e;
106
+ try {
107
+ e = row();
108
+ }
109
+ catch {
110
+ return;
111
+ }
112
+ const content = e?.message?.content;
113
+ if (!Array.isArray(content))
114
+ return;
115
+ for (const b of content) {
116
+ const server = b?.type === "tool_use" && typeof b.name === "string" ? mcpServerOf(b.name) : undefined;
117
+ if (!server || st.seen.includes(server))
118
+ continue;
119
+ st.seen.push(server);
120
+ out.push(server.slice(0, 200));
121
+ }
122
+ },
123
+ };
93
124
  /** An entrypoint as people call it. */
94
125
  export function entrypointName(e) {
95
126
  switch (e) {
@@ -211,6 +242,8 @@ function measureStart(path, kind, since, runRows, parentEntry) {
211
242
  s.tokens += t;
212
243
  }
213
244
  start.shape = { wholeTools, wholeTokens: Math.min(wholeTokens, tokens), mcpTokens: Math.min(mcpTokens, tokens), servers };
245
+ if (servers.size)
246
+ start.called = new Set(rowsOf(path, CALL_ROWS));
214
247
  }
215
248
  return start;
216
249
  }
@@ -275,10 +308,17 @@ export function measureStarts(opts = {}) {
275
308
  const servers = new Map();
276
309
  for (const s of shaped)
277
310
  for (const [name, v] of s.shape.servers) {
278
- const cur = servers.get(name) ?? servers.set(name, { name, starts: 0, tools: 0, tokens: 0, ...(configured.get(name) ? { configured: configured.get(name) } : {}) }).get(name);
311
+ const cur = servers.get(name) ?? servers.set(name, { name, starts: 0, tools: 0, tokens: 0, calledStarts: 0, unusedUsd: 0, calledUsd: 0, ...(configured.get(name) ? { configured: configured.get(name) } : {}) }).get(name);
279
312
  cur.starts++;
280
313
  cur.tools = Math.max(cur.tools, v.tools);
281
314
  cur.tokens += v.tokens;
315
+ const part = s.usd * Math.min(1, v.tokens / s.tokens);
316
+ if (s.called?.has(name)) {
317
+ cur.calledStarts++;
318
+ cur.calledUsd += part;
319
+ }
320
+ else
321
+ cur.unusedUsd += part;
282
322
  }
283
323
  for (const s of servers.values())
284
324
  s.tokens = Math.round(s.tokens / s.starts);
@@ -334,9 +374,38 @@ export function deferrableConfigured(r) {
334
374
  }
335
375
  return [...out.values()].sort((a, b) => b.usd - a.usd);
336
376
  }
377
+ /** Every server whose tools went whole into some start, across the groups: its share of each group's deferrable part, as `overhead` lists it. */
378
+ export function startServers(r) {
379
+ const servers = new Map();
380
+ for (const g of r.groups)
381
+ for (const s of g.servers) {
382
+ const share = g.deferrableUsd ? (g.deferrableUsd * s.tokens * s.starts) / (g.servers.reduce((t, x) => t + x.tokens * x.starts, 0) || 1) : 0;
383
+ const cur = servers.get(s.name);
384
+ if (cur) {
385
+ cur.usd += share;
386
+ cur.starts += s.starts;
387
+ cur.calledStarts += s.calledStarts;
388
+ cur.unusedUsd += s.unusedUsd;
389
+ cur.calledUsd += s.calledUsd;
390
+ cur.tokens = Math.max(cur.tokens, s.tokens);
391
+ cur.tools = Math.max(cur.tools, s.tools);
392
+ }
393
+ else
394
+ servers.set(s.name, { ...s, usd: share });
395
+ }
396
+ return [...servers.values()].sort((a, b) => b.usd - a.usd);
397
+ }
398
+ /**
399
+ * The Desktop app's own servers (Claude_Browser, ccd_session, visualize,
400
+ * terminal) whose tools went whole into some start, and no config file of the
401
+ * user's holds: fix's desktop-tools-defer, which context-doctor's mod applies.
402
+ */
403
+ export function deferrableApp(r) {
404
+ return startServers(r).filter((s) => !s.configured && APP_TOOL_SERVERS.includes(s.name));
405
+ }
337
406
  const tok = (n) => `~${formatTokens(n)}`;
338
407
  /** The starts section of `overhead`. */
339
- export function renderStarts(r) {
408
+ export function renderStarts(r, applied = new Set()) {
340
409
  const out = ["", "Session and subagent starts (the first request, before the first message)", "─".repeat(56)];
341
410
  if (r.groups.length === 0) {
342
411
  out.push(" No Claude Code session in the period to measure.");
@@ -368,21 +437,7 @@ export function renderStarts(r) {
368
437
  out.push(" No terminal sessions in the period to compare the Desktop app's start with (#105328 reports ~79k against ~13k).");
369
438
  }
370
439
  // What could be deferred, and whether any setting reaches it.
371
- const servers = new Map();
372
- for (const g of r.groups)
373
- for (const s of g.servers) {
374
- const share = g.deferrableUsd ? (g.deferrableUsd * s.tokens * s.starts) / (g.servers.reduce((t, x) => t + x.tokens * x.starts, 0) || 1) : 0;
375
- const cur = servers.get(s.name);
376
- if (cur) {
377
- cur.usd += share;
378
- cur.starts += s.starts;
379
- cur.tokens = Math.max(cur.tokens, s.tokens);
380
- cur.tools = Math.max(cur.tools, s.tools);
381
- }
382
- else
383
- servers.set(s.name, { ...s, usd: share });
384
- }
385
- const list = [...servers.values()].sort((a, b) => b.usd - a.usd);
440
+ const list = startServers(r);
386
441
  if (list.length) {
387
442
  out.push(" MCP tool definitions sent whole (not deferred behind tool search):");
388
443
  for (const s of list.slice(0, 8)) {
@@ -396,7 +451,15 @@ export function renderStarts(r) {
396
451
  if (app.length) {
397
452
  out.push(` No setting defers the ${app.length === 1 ? "server" : `${app.length} servers`} the app provides (${formatUsd(app.reduce((t, s) => t + s.usd, 0))} of the starts' cost): the Desktop app hands its own servers and connectors to Claude Code in-process with their tools marked to load upfront, and no MCP setting or ENABLE_TOOL_SEARCH value reaches them (tool search is on: the other tools are deferred). A subagent definition's \`tools:\` list or \`disallowedTools: mcp__*\` keeps them out of that agent's start, and out of its reach too.`);
398
453
  }
399
- if (list.some((s) => s.configured))
454
+ const own = app.filter((s) => APP_TOOL_SERVERS.includes(s.name));
455
+ // Applied already: the starts in this window before it still count the tools, so say so rather than offer it again.
456
+ if (own.length && applied.has("desktop-tools-defer"))
457
+ out.push(` Applied (\`fix --apply desktop-tools-defer\`): context-doctor's mod keeps ${own.map((s) => s.name).join(", ")} behind tool search in sessions started since; the older starts above still count them. Undo with \`fix undo desktop-tools-defer\`.`);
458
+ else if (own.length)
459
+ out.push(` \`context-doctor fix --apply desktop-tools-defer\` has context-doctor's mod keep ${own.map((s) => s.name).join(", ")} behind tool search in new sessions (Claude Code 2.1.326+ with the plugin; reversible with \`fix undo desktop-tools-defer\`); \`fix\` prices it on the starts that never called them.`);
460
+ if (list.some((s) => s.configured) && applied.has("mcp-defer"))
461
+ out.push(" Applied (`fix --apply mcp-defer`): your own servers are set to `alwaysLoad: false`; starts before that still count them. Undo with `fix undo mcp-defer`.");
462
+ else if (list.some((s) => s.configured))
400
463
  out.push(" `context-doctor fix --apply mcp-defer` sets `alwaysLoad: false` on the ones in your own config (reversible with `fix undo mcp-defer`).");
401
464
  }
402
465
  else if (r.groups.some((g) => g.shaped > 0)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-doctor",
3
- "version": "0.49.0",
3
+ "version": "0.50.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",