context-doctor 0.46.0 → 0.47.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 (61) hide show
  1. package/README.md +7 -4
  2. package/dist/ab.js +3 -2
  3. package/dist/autopilot.js +9 -5
  4. package/dist/blob.js +66 -12
  5. package/dist/budget.d.ts +8 -2
  6. package/dist/budget.js +30 -7
  7. package/dist/ci.js +2 -1
  8. package/dist/claudeplugin.d.ts +97 -0
  9. package/dist/claudeplugin.js +142 -0
  10. package/dist/cli.js +109 -36
  11. package/dist/compactcost.d.ts +74 -0
  12. package/dist/compactcost.js +115 -0
  13. package/dist/cursor.js +2 -0
  14. package/dist/doctor.js +90 -18
  15. package/dist/extract.js +4 -3
  16. package/dist/fanout.d.ts +15 -0
  17. package/dist/fanout.js +30 -9
  18. package/dist/firstfix.d.ts +43 -0
  19. package/dist/firstfix.js +80 -0
  20. package/dist/fix.d.ts +46 -2
  21. package/dist/fix.js +95 -10
  22. package/dist/httpguard.d.ts +13 -0
  23. package/dist/httpguard.js +22 -0
  24. package/dist/install.d.ts +21 -9
  25. package/dist/install.js +211 -40
  26. package/dist/limits.d.ts +5 -0
  27. package/dist/limits.js +26 -2
  28. package/dist/mcp.js +60 -59
  29. package/dist/missledger.js +5 -3
  30. package/dist/mod/api.d.ts +8 -2
  31. package/dist/mod/api.js +2 -2
  32. package/dist/mod/logic.d.ts +13 -1
  33. package/dist/mod/logic.js +24 -2
  34. package/dist/mod/register.d.ts +2 -0
  35. package/dist/mod/register.js +19 -5
  36. package/dist/modelids.d.ts +4 -3
  37. package/dist/modelids.js +23 -7
  38. package/dist/modelswitch.js +8 -4
  39. package/dist/modmeasure.js +13 -1
  40. package/dist/orgrates.d.ts +18 -0
  41. package/dist/orgrates.js +51 -2
  42. package/dist/pack.d.ts +13 -0
  43. package/dist/pack.js +38 -2
  44. package/dist/preferences.js +2 -1
  45. package/dist/pricing.d.ts +8 -0
  46. package/dist/pricing.js +16 -0
  47. package/dist/progresssummaries.d.ts +148 -0
  48. package/dist/progresssummaries.js +248 -0
  49. package/dist/proxy.d.ts +1 -1
  50. package/dist/proxy.js +1 -1
  51. package/dist/prune.d.ts +8 -0
  52. package/dist/prune.js +11 -1
  53. package/dist/spend.d.ts +34 -0
  54. package/dist/spend.js +48 -4
  55. package/dist/subagentmodel.d.ts +12 -0
  56. package/dist/subagentmodel.js +17 -4
  57. package/dist/subagentpay.d.ts +8 -0
  58. package/dist/subagentpay.js +8 -1
  59. package/dist/toolcwd.d.ts +14 -0
  60. package/dist/toolcwd.js +17 -0
  61. package/package.json +1 -1
@@ -31,6 +31,7 @@
31
31
  * Requests on a model no pricier than the candidate stay as billed.
32
32
  */
33
33
  import { type Extractor } from "./scan.js";
34
+ import { type ModelPricing, type RecordedUsage } from "./pricing.js";
34
35
  import { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME } from "./subagentpay.js";
35
36
  export { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME };
36
37
  /** The environment variable the lever writes. */
@@ -114,6 +115,17 @@ export interface SubagentModelOptions {
114
115
  }
115
116
  /** The agent type Claude Code recorded beside a subagent's transcript ("unknown" for an older one without it). */
116
117
  export declare function agentTypeOf(transcript: string): string;
118
+ /**
119
+ * A request at the baseline mix: prices are linear in each rate, so it costs
120
+ * the mix of what each model would bill it, each priced by its whole card (an
121
+ * organization's row keeps its own cache-write rate, and its multiplier
122
+ * scales the web searches as requestCost does). A mix of one model is
123
+ * exactly requestCost.
124
+ */
125
+ export declare function atBaseline(base: ModelPricing, u: RecordedUsage, at: number, mix?: Record<string, number>): {
126
+ totalUsd: number;
127
+ rates: ModelPricing;
128
+ };
117
129
  /**
118
130
  * Replay every subagent request in [since, until): as billed, and with the
119
131
  * requests CLAUDE_CODE_SUBAGENT_MODEL would move at the candidate model.
@@ -40,6 +40,7 @@ import { claudeDir } from "./claudedir.js";
40
40
  import { subagentTranscripts } from "./cachettl.js";
41
41
  import { workflowRunOf } from "./subagents.js";
42
42
  import { managedDir } from "./effective.js";
43
+ import { orgRates } from "./orgrates.js";
43
44
  import { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME } from "./subagentpay.js";
44
45
  export { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME };
45
46
  /** The environment variable the lever writes. */
@@ -251,11 +252,23 @@ function blend(mix, u, at) {
251
252
  const rate = (k) => parts.reduce((s, [p, w]) => s + (p[k] * w) / total, 0);
252
253
  return { inputPerM: rate("inputPerM"), outputPerM: rate("outputPerM"), cacheReadPerM: rate("cacheReadPerM") };
253
254
  }
254
- /** A request at the baseline mix (prices are linear in each rate: the blend of its costs), web searches as any Claude model bills them. */
255
- function atBaseline(base, u, at, mix) {
256
- const rates = (mix && blend(mix, u, at)) || base;
255
+ /**
256
+ * A request at the baseline mix: prices are linear in each rate, so it costs
257
+ * the mix of what each model would bill it, each priced by its whole card (an
258
+ * organization's row keeps its own cache-write rate, and its multiplier
259
+ * scales the web searches as requestCost does). A mix of one model is
260
+ * exactly requestCost.
261
+ */
262
+ export function atBaseline(base, u, at, mix) {
257
263
  const searches = Number(u?.server_tool_use?.web_search_requests) > 0 ? Number(u.server_tool_use.web_search_requests) : 0;
258
- return { totalUsd: usageCost(u, rates).totalUsd + searches * WEB_SEARCH_USD, rates };
264
+ const parts = Object.entries(mix ?? {}).flatMap(([m, w]) => { const c = w > 0 ? rateCard(m, u, at) : undefined; return c ? [{ c, w }] : []; });
265
+ const total = parts.reduce((n, p) => n + p.w, 0);
266
+ if (total > 0) {
267
+ const prompt = promptTokens(u);
268
+ const usd = parts.reduce((n, { c, w }) => n + (w / total) * (usageCost(u, ratesAt(c, prompt)).totalUsd + (c.webSearch ? searches * WEB_SEARCH_USD * (c.scale ?? 1) : 0)), 0);
269
+ return { totalUsd: usd, rates: blend(mix, u, at) ?? base };
270
+ }
271
+ return { totalUsd: usageCost(u, base).totalUsd + searches * WEB_SEARCH_USD * (orgRates()?.multiplier ?? 1), rates: base };
259
272
  }
260
273
  const cheaper = (a, b) => a.inputPerM < b.inputPerM || (a.inputPerM === b.inputPerM && a.outputPerM < b.outputPerM);
261
274
  /**
@@ -73,6 +73,13 @@ export interface PaybackReport {
73
73
  }>;
74
74
  };
75
75
  }
76
+ /** A subagent's tokens by what they bill as: uncached input, cache reads, cache writes, output (settled). */
77
+ export interface SubagentTokens {
78
+ input: number;
79
+ read: number;
80
+ write: number;
81
+ output: number;
82
+ }
76
83
  /**
77
84
  * A subagent's bill (each request as billed, requestCost), steps, startup
78
85
  * context, start time, the tool output it read, and the rates its first
@@ -89,6 +96,7 @@ export declare function scanSubagent(path: string): {
89
96
  model?: string;
90
97
  writeMult: number;
91
98
  rates?: ModelPricing;
99
+ tokens: SubagentTokens;
92
100
  };
93
101
  export declare function subagentPayback(days?: number, paths?: string[], onProgress?: (done: number, total: number) => void): PaybackReport;
94
102
  export declare function renderPayback(r: PaybackReport, top?: number): string;
@@ -77,6 +77,7 @@ function billed(model, u, at) {
77
77
  * request billed at.
78
78
  */
79
79
  export function scanSubagent(path) {
80
+ const tokens = { input: 0, read: 0, write: 0, output: 0 };
80
81
  let costUsd = 0, atSonnetUsd = 0, readTokens = 0, task = "", model, firstRates;
81
82
  let steps = 0, startup = 0, start = Infinity, writes5m = 0, writes1h = 0;
82
83
  // The rates of the last reply, for the output its later rows settle.
@@ -109,6 +110,8 @@ export function scanSubagent(path) {
109
110
  if (seen === "repeat") {
110
111
  // A later row of the same reply: only its final output count is new.
111
112
  const more = settle(e, false);
113
+ if (more)
114
+ tokens.output += more;
112
115
  if (more && last) {
113
116
  costUsd += (more * last.rates.outputPerM) / 1e6;
114
117
  atSonnetUsd += (more * last.sonnetRates.outputPerM) / 1e6;
@@ -127,6 +130,10 @@ export function scanSubagent(path) {
127
130
  if (steps === 0)
128
131
  startup = n("input_tokens") + n("cache_creation_input_tokens") + n("cache_read_input_tokens");
129
132
  steps++;
133
+ tokens.input += n("input_tokens");
134
+ tokens.read += n("cache_read_input_tokens");
135
+ tokens.write += n("cache_creation_input_tokens");
136
+ tokens.output += n("output_tokens");
130
137
  const w = cacheWriteSplit(u);
131
138
  writes5m += w.fiveMinute;
132
139
  writes1h += w.oneHour;
@@ -138,7 +145,7 @@ export function scanSubagent(path) {
138
145
  costUsd += b.usd;
139
146
  atSonnetUsd += b.sonnetUsd;
140
147
  });
141
- return { costUsd, atSonnetUsd, readTokens, task: task.replace(/\s+/g, " ").slice(0, 90), steps, startup, start, model, writeMult: writeMultiplier(writes5m, writes1h), ...(firstRates ? { rates: firstRates } : {}) };
148
+ return { costUsd, atSonnetUsd, readTokens, task: task.replace(/\s+/g, " ").slice(0, 90), steps, startup, start, model, writeMult: writeMultiplier(writes5m, writes1h), ...(firstRates ? { rates: firstRates } : {}), tokens };
142
149
  }
143
150
  function scanParent(path, ids) {
144
151
  const view = { requests: [], prompts: [], models: [], billing: [], compactions: [], writes5m: 0, writes1h: 0, returns: new Map() };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The working folder for a program run by its bare name (git, pdftotext,
3
+ * unzip, sqlite3, netstat, schtasks…).
4
+ *
5
+ * On Windows, Node (libuv) looks a bare name up in the child's working
6
+ * folder before PATH, as cmd.exe does. Run from a project, `pack .`, the
7
+ * MCP server's pack_context (whose working folder is the project its client
8
+ * opened) and `ci` ran a git.exe committed to that repository, or a
9
+ * pdftotext.exe beside the documents, instead of the real one. These
10
+ * programs get every path absolute (git with -C), so they run from the
11
+ * user's own temp folder instead, where a clone or a download cannot put a
12
+ * program by that name.
13
+ */
14
+ export declare function toolCwd(): string;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The working folder for a program run by its bare name (git, pdftotext,
3
+ * unzip, sqlite3, netstat, schtasks…).
4
+ *
5
+ * On Windows, Node (libuv) looks a bare name up in the child's working
6
+ * folder before PATH, as cmd.exe does. Run from a project, `pack .`, the
7
+ * MCP server's pack_context (whose working folder is the project its client
8
+ * opened) and `ci` ran a git.exe committed to that repository, or a
9
+ * pdftotext.exe beside the documents, instead of the real one. These
10
+ * programs get every path absolute (git with -C), so they run from the
11
+ * user's own temp folder instead, where a clone or a download cannot put a
12
+ * program by that name.
13
+ */
14
+ import { tmpdir } from "node:os";
15
+ export function toolCwd() {
16
+ return tmpdir();
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-doctor",
3
- "version": "0.46.0",
3
+ "version": "0.47.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",