context-doctor 0.44.0 → 0.46.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 (62) hide show
  1. package/README.md +5 -2
  2. package/dist/budget.d.ts +14 -1
  3. package/dist/budget.js +50 -28
  4. package/dist/cache.js +5 -4
  5. package/dist/cachemisses.js +6 -4
  6. package/dist/cacheplan.js +2 -2
  7. package/dist/cachettl.js +8 -5
  8. package/dist/ci.js +18 -2
  9. package/dist/cli.js +27 -6
  10. package/dist/codexcompact.js +14 -7
  11. package/dist/coldresume.d.ts +6 -0
  12. package/dist/coldresume.js +20 -11
  13. package/dist/compactwindow.js +14 -8
  14. package/dist/doctor.js +26 -2
  15. package/dist/extract.d.ts +2 -2
  16. package/dist/extract.js +14 -3
  17. package/dist/fanout.js +1 -1
  18. package/dist/fix.js +14 -10
  19. package/dist/hook.js +6 -2
  20. package/dist/httpguard.d.ts +9 -1
  21. package/dist/httpguard.js +5 -4
  22. package/dist/lens.js +7 -7
  23. package/dist/limits.d.ts +14 -5
  24. package/dist/limits.js +26 -17
  25. package/dist/mcp.js +62 -62
  26. package/dist/mcpschema.js +5 -3
  27. package/dist/mod/api.d.ts +129 -0
  28. package/dist/mod/api.js +12 -0
  29. package/dist/mod/logic.d.ts +146 -0
  30. package/dist/mod/logic.js +222 -0
  31. package/dist/mod/register.d.ts +58 -0
  32. package/dist/mod/register.js +237 -0
  33. package/dist/modelids.d.ts +23 -0
  34. package/dist/modelids.js +49 -0
  35. package/dist/modmeasure.d.ts +121 -0
  36. package/dist/modmeasure.js +298 -0
  37. package/dist/orgrates.d.ts +75 -0
  38. package/dist/orgrates.js +165 -0
  39. package/dist/overhead.js +11 -8
  40. package/dist/pack.js +95 -22
  41. package/dist/pricing.d.ts +123 -6
  42. package/dist/pricing.js +236 -62
  43. package/dist/proxy.d.ts +12 -1
  44. package/dist/proxy.js +13 -1
  45. package/dist/prune.js +55 -3
  46. package/dist/savings.js +19 -10
  47. package/dist/scan.d.ts +7 -1
  48. package/dist/scan.js +19 -1
  49. package/dist/sessionprice.d.ts +25 -2
  50. package/dist/sessionprice.js +60 -20
  51. package/dist/spend.d.ts +62 -10
  52. package/dist/spend.js +255 -29
  53. package/dist/statusline.d.ts +10 -2
  54. package/dist/statusline.js +18 -9
  55. package/dist/subagentmodel.d.ts +2 -2
  56. package/dist/subagentmodel.js +31 -24
  57. package/dist/subagentpay.d.ts +7 -1
  58. package/dist/subagentpay.js +51 -25
  59. package/dist/subagents.js +15 -10
  60. package/dist/tasks.js +9 -6
  61. package/dist/tooloutput.js +14 -5
  62. package/package.json +1 -1
package/dist/limits.js CHANGED
@@ -33,7 +33,7 @@ import { statePath } from "./ledger.js";
33
33
  import { forEachLine, listSessions, outputSettler, replyCounter, isClaudeSession } from "./session.js";
34
34
  import { subagentFiles } from "./subagents.js";
35
35
  import { pick, rowsOf, slimUsage } from "./scan.js";
36
- import { isUnbilledModel, openAIUsage, pricingFor, usageCost } from "./pricing.js";
36
+ import { isUnbilledModel, NO_MODIFIERS, openAIUsage, requestCost } from "./pricing.js";
37
37
  import { realModel } from "./tokens.js";
38
38
  const MIN = 60_000;
39
39
  const HOUR = 60 * MIN;
@@ -290,8 +290,7 @@ export function codexLimitData(since, root = codexSessionsRoot()) {
290
290
  if (last && num(last.input_tokens) > 0 && (runTotal === 0 || runTotal !== total)) {
291
291
  total = runTotal;
292
292
  const u = openAIUsage(last);
293
- const price = pricingFor(model);
294
- ev.point = { ts, app: "codex", model, input: num(u.input_tokens), read: num(u.cache_read_input_tokens), write: num(u.cache_creation_input_tokens), output: num(u.output_tokens), usd: price ? usageCost(u, price).totalUsd : 0 };
293
+ ev.point = { ts, app: "codex", model, input: num(u.input_tokens), read: num(u.cache_read_input_tokens), write: num(u.cache_creation_input_tokens), output: num(u.output_tokens), usd: requestCost(model, u, ts)?.totalUsd ?? 0 };
295
294
  }
296
295
  const rl = p.rate_limits;
297
296
  if (rl && typeof rl === "object") {
@@ -339,7 +338,7 @@ export function codexLimitData(since, root = codexSessionsRoot()) {
339
338
  * main thread.
340
339
  */
341
340
  export const USAGE_ROWS = {
342
- id: "usage.2",
341
+ id: "usage.3",
343
342
  init: () => ({ ids: {} }),
344
343
  line(line, row, out, st) {
345
344
  if (!line.includes('"usage"'))
@@ -366,11 +365,11 @@ export const USAGE_ROWS = {
366
365
  };
367
366
  /**
368
367
  * The requests of one Claude Code transcript between `since` and `until`,
369
- * each reply once, its output settled on its last row. `runRows` is shared
370
- * by every file of one report, so a fork's copy of its parent's replies is
371
- * counted once, in the file met first (replyCounter). Replies on
372
- * `<synthetic>` (isUnbilledModel) are left out. Throws when the transcript
373
- * cannot be read.
368
+ * each reply once, its output settled on its last row (billed at the
369
+ * reply's own rates, requestCost's). `runRows` is shared by every file of
370
+ * one report, so a fork's copy of its parent's replies is counted once, in
371
+ * the file met first (replyCounter). Replies on `<synthetic>`
372
+ * (isUnbilledModel) are left out. Throws when the transcript cannot be read.
374
373
  */
375
374
  export function claudeFileUsage(file, since, until = Infinity, runRows) {
376
375
  const out = [];
@@ -386,9 +385,14 @@ export function claudeFileUsage(file, since, until = Infinity, runRows) {
386
385
  const known = typeof m.id === "string" ? byId.get(m.id) : undefined;
387
386
  const more = settle(e, false);
388
387
  if (known && more) {
389
- known.point.output += more;
390
- if (known.price)
391
- known.point.usd += (more * known.price.outputPerM) / 1e6;
388
+ const p = known.point;
389
+ p.output += more;
390
+ if (p.rates) {
391
+ p.usd += (more * p.rates.outputPerM) / 1e6;
392
+ // What the modifiers added grows with the output: priced again whole.
393
+ if (p.modifiers.fastUsd || p.modifiers.geoUsd || p.modifiers.longUsd)
394
+ p.modifiers = requestCost(p.model, { ...known.u, output_tokens: p.output }, p.ts).modifiers;
395
+ }
392
396
  }
393
397
  return;
394
398
  }
@@ -402,7 +406,7 @@ export function claudeFileUsage(file, since, until = Infinity, runRows) {
402
406
  if (!Number.isFinite(ts) || ts < since || ts > until)
403
407
  return;
404
408
  const u = m.usage;
405
- const point = { ts, app: "claude", model: realModel(m.model), input: num(u.input_tokens), read: num(u.cache_read_input_tokens), write: num(u.cache_creation_input_tokens), output: num(u.output_tokens), usd: 0, promptUsd: 0 };
409
+ const point = { ts, app: "claude", model: realModel(m.model), input: num(u.input_tokens), read: num(u.cache_read_input_tokens), write: num(u.cache_creation_input_tokens), output: num(u.output_tokens), usd: 0, promptUsd: 0, modifiers: { ...NO_MODIFIERS } };
406
410
  if (point.input + point.read + point.write === 0)
407
411
  return;
408
412
  if (typeof e.uuid === "string")
@@ -411,15 +415,20 @@ export function claudeFileUsage(file, since, until = Infinity, runRows) {
411
415
  point.id = m.id;
412
416
  if (e.isSidechain === true)
413
417
  point.sidechain = true;
414
- const price = pricingFor(point.model);
415
- if (price) {
416
- const c = usageCost(u, price);
418
+ const c = requestCost(point.model, u, ts);
419
+ if (c) {
417
420
  point.usd = c.totalUsd;
418
421
  point.promptUsd = c.promptUsd;
422
+ point.rates = c.rates;
423
+ point.modifiers = c.modifiers;
424
+ if (c.estimated)
425
+ point.estimated = true;
426
+ if (c.changedOn)
427
+ point.changedOn = c.changedOn;
419
428
  }
420
429
  out.push(point);
421
430
  if (typeof m.id === "string")
422
- byId.set(m.id, { point, price });
431
+ byId.set(m.id, { point, u });
423
432
  });
424
433
  return out;
425
434
  }