context-doctor 0.47.0 → 0.49.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 (84) hide show
  1. package/README.md +17 -14
  2. package/dist/ab.d.ts +51 -3
  3. package/dist/ab.js +68 -26
  4. package/dist/abclaude.d.ts +167 -0
  5. package/dist/abclaude.js +511 -0
  6. package/dist/abstore.d.ts +46 -0
  7. package/dist/abstore.js +58 -0
  8. package/dist/budget.d.ts +5 -2
  9. package/dist/budget.js +21 -7
  10. package/dist/cache.js +3 -3
  11. package/dist/cachemisses.d.ts +3 -0
  12. package/dist/cachemisses.js +8 -0
  13. package/dist/cachettl.js +6 -2
  14. package/dist/ccost.d.ts +160 -0
  15. package/dist/ccost.js +304 -0
  16. package/dist/cli.js +154 -32
  17. package/dist/cline.d.ts +41 -0
  18. package/dist/cline.js +174 -0
  19. package/dist/coldresume.d.ts +11 -0
  20. package/dist/coldresume.js +3 -3
  21. package/dist/coldreturns.d.ts +145 -0
  22. package/dist/coldreturns.js +282 -0
  23. package/dist/compactcost.d.ts +10 -4
  24. package/dist/compactcost.js +12 -7
  25. package/dist/compactwindow.js +2 -2
  26. package/dist/copilotusage.d.ts +78 -0
  27. package/dist/copilotusage.js +193 -0
  28. package/dist/doctor.d.ts +6 -0
  29. package/dist/doctor.js +17 -4
  30. package/dist/fix.d.ts +35 -4
  31. package/dist/fix.js +55 -8
  32. package/dist/install.js +2 -0
  33. package/dist/limits.d.ts +222 -14
  34. package/dist/limits.js +684 -33
  35. package/dist/mcp.d.ts +4 -0
  36. package/dist/mcp.js +142 -98
  37. package/dist/mcpclient.js +73 -42
  38. package/dist/mcpdefer.d.ts +47 -0
  39. package/dist/mcpdefer.js +91 -0
  40. package/dist/mod/api.d.ts +72 -2
  41. package/dist/mod/api.js +5 -1
  42. package/dist/mod/logic.d.ts +281 -0
  43. package/dist/mod/logic.js +336 -0
  44. package/dist/mod/register.d.ts +35 -2
  45. package/dist/mod/register.js +283 -6
  46. package/dist/modelids.d.ts +58 -0
  47. package/dist/modelids.js +133 -1
  48. package/dist/modmeasure.d.ts +6 -4
  49. package/dist/modmeasure.js +8 -4
  50. package/dist/opencode.d.ts +57 -0
  51. package/dist/opencode.js +234 -0
  52. package/dist/orgrates.d.ts +7 -0
  53. package/dist/orgrates.js +19 -0
  54. package/dist/overhead.d.ts +3 -0
  55. package/dist/overhead.js +5 -1
  56. package/dist/pricing.d.ts +59 -2
  57. package/dist/pricing.js +139 -12
  58. package/dist/proxy.d.ts +8 -1
  59. package/dist/proxy.js +13 -1
  60. package/dist/savings.d.ts +5 -1
  61. package/dist/savings.js +11 -5
  62. package/dist/scan.d.ts +3 -1
  63. package/dist/scan.js +8 -1
  64. package/dist/sessionprice.d.ts +5 -0
  65. package/dist/sessionprice.js +5 -1
  66. package/dist/spend.d.ts +86 -3
  67. package/dist/spend.js +240 -23
  68. package/dist/spendplace.d.ts +40 -0
  69. package/dist/spendplace.js +93 -0
  70. package/dist/spendteam.d.ts +160 -0
  71. package/dist/spendteam.js +286 -0
  72. package/dist/starts.d.ts +106 -0
  73. package/dist/starts.js +406 -0
  74. package/dist/statusline.d.ts +2 -2
  75. package/dist/statusline.js +2 -2
  76. package/dist/subagentmodel.js +9 -7
  77. package/dist/subagentpay.d.ts +31 -2
  78. package/dist/subagentpay.js +232 -68
  79. package/dist/subagents.js +3 -3
  80. package/dist/tasks.js +3 -2
  81. package/dist/tooloutput.js +5 -5
  82. package/dist/usagepane.d.ts +77 -0
  83. package/dist/usagepane.js +287 -0
  84. package/package.json +6 -3
package/dist/limits.d.ts CHANGED
@@ -1,16 +1,21 @@
1
1
  /**
2
2
  * `context-doctor limits`: your plan's usage limits, in their own unit.
3
3
  *
4
- * Subscriptions are billed in % of a 5-hour and a weekly window (Claude
5
- * Pro/Max), or in credits per token (Codex since April 2026), and neither
6
- * vendor shows where the percentage went. Two local sources say:
4
+ * Subscriptions are billed in % of a weekly window and, on most plans, a
5
+ * 5-hour one (Claude Pro, Max and Ultra; since 2026-10-30 Max 5x is called
6
+ * Max and Max 20x Ultra, and Ultra has no 5-hour window), or in credits per
7
+ * token (Codex since April 2026), and neither vendor shows where the
8
+ * percentage went. Local sources say:
7
9
  *
8
10
  * - Claude Code hands its status line command `rate_limits.five_hour` and
9
11
  * `.seven_day`, each `{used_percentage, resets_at}` (documented at
10
12
  * code.claude.com/docs/en/statusline; built in 2.1.308 as
11
13
  * Math.round(utilization*1000)/10, so 0.1% steps; present only for
12
- * Pro/Max subscribers, after the first reply). `context-doctor statusline`
13
- * appends each new reading to a small local log (recordLimits).
14
+ * subscribers, after the first reply; `five_hour` absent on Ultra), and
15
+ * the same windows to the plugin's mod (`session.measure`). The status
16
+ * line, the mod and the proxy (from the API's
17
+ * `anthropic-ratelimit-unified-5h-*` / `-7d-*` headers) append each new
18
+ * reading to a small local log (recordLimits).
14
19
  * - Codex records `rate_limits.primary` / `.secondary`
15
20
  * `{used_percent, window_minutes, resets_at}` on every token_count event of
16
21
  * its rollouts (~/.codex/sessions), next to that request's usage, in
@@ -26,10 +31,12 @@
26
31
  * instead of printing a number.
27
32
  */
28
33
  import { type Extractor } from "./scan.js";
29
- import { type ModelPricing, type Modifiers } from "./pricing.js";
34
+ import { type ModelPricing, type Modifiers, type RecordedUsage } from "./pricing.js";
30
35
  import { type CompactionRequest } from "./compactcost.js";
31
36
  /** Claude Code's two windows, as its status line input names them. */
32
37
  export declare const CLAUDE_WINDOWS: readonly ["five_hour", "seven_day"];
38
+ /** What wrote a Claude reading: the status line, the plugin's mod, or the proxy. */
39
+ export type LimitSource = "statusline" | "mod" | "proxy";
33
40
  /** One reading of one window: how much of it was used at `ts`. */
34
41
  export interface LimitReading {
35
42
  /** When it was read, ms. */
@@ -45,6 +52,8 @@ export interface LimitReading {
45
52
  windowMs?: number;
46
53
  /** Codex's plan ("plus", "team", "free"...). */
47
54
  plan?: string;
55
+ /** What recorded it (Claude readings written from 0.49 on; older lines have none). */
56
+ source?: LimitSource;
48
57
  }
49
58
  /** One request's usage, from a transcript on this machine. */
50
59
  export interface UsagePoint {
@@ -58,6 +67,9 @@ export interface UsagePoint {
58
67
  output: number;
59
68
  /** At API list price; 0 when the model has no price on file. */
60
69
  usd: number;
70
+ /** Claude Code's entrypoint ("cli", "claude-desktop", "sdk-cli"...) and version, from the transcript's rows. */
71
+ entrypoint?: string;
72
+ version?: string;
61
73
  }
62
74
  /** Beside the hook state and the ledger: ~/.claude/.context-doctor-limits.jsonl. */
63
75
  export declare function limitsLogPath(): string;
@@ -65,12 +77,25 @@ export declare function limitsLogPath(): string;
65
77
  * Append what changed in Claude Code's `rate_limits` to the log: a window
66
78
  * whose % or reset time moved since its last line, or that has not been
67
79
  * written for 15 minutes. Nothing when there are no limits (an API key,
68
- * Bedrock, Vertex). Never throws: the status line must stay silent. Returns
69
- * the lines written.
80
+ * Bedrock, Vertex). A window that is not there (Ultra's 5-hour one) is
81
+ * simply not written. Never throws: the status line must stay silent.
82
+ * Returns the lines written.
70
83
  */
71
- export declare function recordLimits(rateLimits: unknown, now?: number, path?: string): number;
84
+ export declare function recordLimits(rateLimits: unknown, now?: number, path?: string, source?: LimitSource): number;
72
85
  /** Every Claude reading in the log since `since`. */
73
86
  export declare function readClaudeReadings(since?: number, path?: string): LimitReading[];
87
+ /**
88
+ * The windows an Anthropic response's `anthropic-ratelimit-unified-5h-*` and
89
+ * `-7d-*` headers carry (a claude.ai sign-in, or a gateway), in the status
90
+ * line's shape for recordLimits: utilization is a fraction, the reset epoch
91
+ * seconds. Claude Code reads the same headers into its status line and its
92
+ * mods (Math.round(utilization*1000)/10). Undefined when there are none; a
93
+ * response with only the weekly pair (Ultra) gives only the weekly window.
94
+ */
95
+ export declare function rateLimitsFromHeaders(get: (name: string) => string | null | undefined): Record<string, {
96
+ used_percentage: number;
97
+ resets_at?: number;
98
+ }> | undefined;
74
99
  export declare function codexSessionsRoot(home?: string): string;
75
100
  /** Codex rollouts under `root` (YYYY/MM/DD folders, or flat) written since `since`. */
76
101
  export declare function rolloutFiles(root: string, since: number): string[];
@@ -100,13 +125,18 @@ export declare function codexLimitData(since: number, root?: string): {
100
125
  /**
101
126
  * What the usage fit takes from a Claude Code transcript (subagents' too):
102
127
  * every assistant row with usage, in order. A later row of a reply already
103
- * met keeps only its id and output tokens: it is a repeat, read only for the
104
- * output a streamed reply settles on its last row. `isSidechain` is kept for
105
- * `spend`, which tells a subagent inlined in an older transcript from the
106
- * main thread.
128
+ * met keeps only its id, its output tokens and its billed iterations: it is
129
+ * a repeat, read only for what a streamed reply settles on its last row.
130
+ * `isSidechain` is kept for `spend`, which tells a subagent inlined in an
131
+ * older transcript from the main thread. A request's `usage.iterations` is
132
+ * kept only where some bill beyond its top-level usage (extraIterations).
133
+ * Also Claude Code's own running cost: each `cost-state` row as
134
+ * costStateRecord keeps it, with the Claude Code version of the replies
135
+ * before it (ccost.ts compares it with ours).
107
136
  */
108
137
  export declare const USAGE_ROWS: Extractor<{
109
138
  ids: Record<string, 1>;
139
+ v?: string;
110
140
  }>;
111
141
  /** A compaction's estimated request as a request point (no message id: its key is the boundary's uuid). */
112
142
  export declare function compactionPoint(c: CompactionRequest): ClaudeRequest;
@@ -130,7 +160,32 @@ export interface ClaudeRequest extends UsagePoint {
130
160
  changedOn?: string;
131
161
  /** A compaction's own request, which no row records usage for: estimated (compactcost.ts). */
132
162
  compaction?: boolean;
163
+ /**
164
+ * The iterations its top-level usage leaves out (extraIterations: an
165
+ * advisor's call, an attempt a fallback replaced), each priced at its own
166
+ * model by requestCost: inside `usd`, `promptUsd` and `modifiers`, not in
167
+ * the token counts above (those are the top-level usage's).
168
+ */
169
+ extra?: IterationPoint[];
170
+ }
171
+ /** One iteration billed beyond its request's top-level usage, priced (requestCost at its own model). */
172
+ export interface IterationPoint {
173
+ type: string;
174
+ model?: string;
175
+ input: number;
176
+ read: number;
177
+ write: number;
178
+ output: number;
179
+ /** 0 when its model has no price (`priced` false). */
180
+ usd: number;
181
+ promptUsd: number;
182
+ priced: boolean;
183
+ modifiers: Modifiers;
184
+ estimated?: boolean;
185
+ changedOn?: string;
133
186
  }
187
+ /** A request's iterations billed beyond its top-level usage, each priced at its own model (the request's when it names none) at `ts`. */
188
+ export declare function iterationPoints(model: string | undefined, u: RecordedUsage, ts: number): IterationPoint[];
134
189
  /**
135
190
  * The requests of one Claude Code transcript between `since` and `until`,
136
191
  * each reply once, its output settled on its last row (billed at the
@@ -153,9 +208,20 @@ export declare function claudeUsageFiles(since: number, tops?: string[]): Array<
153
208
  }>;
154
209
  /** How long before a period a session may have been last written and still have subagents running into it. */
155
210
  export declare const SUBAGENT_LOOKBACK_MS: number;
211
+ /**
212
+ * Which Claude Code wrote a transcript, and when that changed: every row
213
+ * carries `entrypoint` ("cli", "claude-desktop", "sdk-cli" for `claude -p`,
214
+ * "claude-vscode"...) and `version`; a record is kept only where either
215
+ * changes (a session resumed after an update), so a file has one or two.
216
+ */
217
+ export declare const HOST_ROWS: Extractor<{
218
+ ep?: string;
219
+ v?: string;
220
+ }>;
156
221
  /**
157
222
  * Every Claude Code request between `since` and `until`, main threads and
158
- * subagents alike (both draw on the same limits), each reply once.
223
+ * subagents alike (both draw on the same limits), each reply once, with
224
+ * the entrypoint and Claude Code version it was made from.
159
225
  */
160
226
  export declare function claudeUsage(since: number, until?: number, paths?: string[]): UsagePoint[];
161
227
  export interface WindowStatus {
@@ -168,6 +234,8 @@ export interface WindowStatus {
168
234
  resetsAt?: number;
169
235
  /** True when the newest reading's window has reset since (its % is stale). */
170
236
  resetSince: boolean;
237
+ /** True when the newest reading is older than the stretch the pace is read over (an hour for a 5-hour window, a day otherwise): its pace is no forecast. */
238
+ stale?: boolean;
171
239
  /** Points per hour over the recent stretch (the last hour of a 5-hour window, the last day otherwise). */
172
240
  recentPerHour?: number;
173
241
  /** Points per hour since the window began, when its length is known. */
@@ -265,6 +333,103 @@ export interface LimitFit {
265
333
  why?: string;
266
334
  }
267
335
  export declare function fitWindow(readings: LimitReading[], usage: UsagePoint[], stretchMs?: number): LimitFit | undefined;
336
+ /**
337
+ * One fit over every reading assumes a percent of the limit always costs the
338
+ * same. It does not: people measured 1% of the week falling from ~$30 to ~$4
339
+ * of API-equivalent use within a week (claude-code#108272), and `claude -p`
340
+ * draining ~1.8x more per token than the terminal (#97074). So each weekly
341
+ * window (one reset to the next) gets its own figure, and the report says
342
+ * when the figure moved, by how much, and on what date.
343
+ */
344
+ /** What one entrypoint or model used inside a window's stretches. */
345
+ export interface UsageShare {
346
+ key: string;
347
+ usd: number;
348
+ tokens: number;
349
+ requests: number;
350
+ /** Its part of the window's API-equivalent dollars, 0-1. */
351
+ share: number;
352
+ /** Dollars per 1% for this key alone, when the stretches tell the keys apart (one column per key); with its relative standard error. */
353
+ usdPerPct?: number;
354
+ relSe?: number;
355
+ }
356
+ /** One weekly window, reset to reset. */
357
+ export interface ResetWindow {
358
+ window: string;
359
+ label: string;
360
+ app: "claude" | "codex";
361
+ /** When it began (its reset less its length) and when it resets, ms. */
362
+ start: number;
363
+ resetsAt: number;
364
+ /** Still running at the report's time. */
365
+ current: boolean;
366
+ readings: number;
367
+ firstReading: number;
368
+ lastReading: number;
369
+ /** The highest % read in it. */
370
+ pctUsed: number;
371
+ /** Points risen in stretches with usage on this machine, and in stretches with none (used elsewhere: claude.ai, another computer). */
372
+ pctMatched: number;
373
+ pctElsewhere: number;
374
+ stretches: number;
375
+ /** Requests, API-equivalent dollars (requestCost, at list price) and tokens of every kind in those stretches. */
376
+ requests: number;
377
+ usd: number;
378
+ tokens: number;
379
+ /** Dollars and tokens per 1% of the limit (ratio of the sums), and the ratio's relative standard error across stretches. */
380
+ usdPerPct?: number;
381
+ tokensPerPct?: number;
382
+ relSe?: number;
383
+ /** Why there is no figure, when there is none. */
384
+ why?: string;
385
+ byEntrypoint: UsageShare[];
386
+ byModel: UsageShare[];
387
+ /** Claude Code versions its requests were made with, oldest first. */
388
+ versions: string[];
389
+ }
390
+ /** A move in what 1% costs. */
391
+ export interface SlopeChange {
392
+ window: string;
393
+ label: string;
394
+ /** "reset": between two windows, dated at the later one's start; "within": inside one window, dated at the stretch where it moved. */
395
+ kind: "reset" | "within";
396
+ at: number;
397
+ /** For "reset", the earlier window's reset (equal to `at` unless a window is missing between them). */
398
+ prevEnd?: number;
399
+ usdPerPctBefore: number;
400
+ usdPerPctAfter: number;
401
+ tokensPerPctBefore?: number;
402
+ tokensPerPctAfter?: number;
403
+ /** after / before: under 1 when 1% buys less (the limit drains faster per dollar). */
404
+ factor: number;
405
+ /** How many standard errors the move is (on the log scale). */
406
+ sigma: number;
407
+ versionsBefore: string[];
408
+ versionsAfter: string[];
409
+ }
410
+ /**
411
+ * Each reset window of one weekly window's readings: % used, dollars and
412
+ * tokens per 1% (over the stretches with usage here; rises with none are
413
+ * counted apart), the split by entrypoint and by model, and the Claude Code
414
+ * versions. `usage` in time order.
415
+ */
416
+ export declare function resetWindowsOf(readings: LimitReading[], usage: UsagePoint[], opts?: {
417
+ now?: number;
418
+ stretchMs?: number;
419
+ }): ResetWindow[];
420
+ /**
421
+ * A model id as a report may carry it: a model's name, never a cloud
422
+ * resource (a Bedrock ARN holds an account number) or anything path-shaped.
423
+ */
424
+ export declare function reportModel(model: string | undefined): string;
425
+ /**
426
+ * Where what 1% costs moved: between consecutive windows that each have a
427
+ * figure (dated at the reset), and inside a window at the one split of its
428
+ * stretches that separates two rates best (dated at that stretch). Reported
429
+ * from a 1.5x move either way, beyond 3 standard errors between windows and
430
+ * 4 inside one (the split is searched for).
431
+ */
432
+ export declare function slopeChanges(windows: ResetWindow[], usage?: UsagePoint[], readings?: LimitReading[], stretchMs?: number): SlopeChange[];
268
433
  export interface LimitsReport {
269
434
  days: number;
270
435
  now: number;
@@ -274,6 +439,14 @@ export interface LimitsReport {
274
439
  codexReadings: number;
275
440
  windows: WindowStatus[];
276
441
  fits: LimitFit[];
442
+ /** Each weekly window, reset to reset: % used, dollars and tokens per 1%, by entrypoint and model (resetWindowsOf). */
443
+ resets: ResetWindow[];
444
+ /** Where what 1% costs moved (slopeChanges). */
445
+ changes: SlopeChange[];
446
+ /** The Claude windows the readings hold: ["seven_day"] alone on a plan with no 5-hour window (Ultra). */
447
+ claudeWindows: string[];
448
+ /** How many Claude readings each recorder wrote ("statusline", "mod", "proxy"; "earlier" for lines from before 0.49). */
449
+ claudeSources: Record<string, number>;
277
450
  }
278
451
  export interface LimitsOptions {
279
452
  now?: number;
@@ -285,6 +458,26 @@ export interface LimitsOptions {
285
458
  claudeOnly?: boolean;
286
459
  }
287
460
  export declare function limitsReport(days?: number, opts?: LimitsOptions): LimitsReport;
461
+ /** What a usage view needs of the limits: each window now, each weekly window reset to reset, and the moves between them. */
462
+ export interface LimitsByReset {
463
+ now: number;
464
+ /** Each window's current %, pace and reset (a Claude 5-hour one only on plans that have it). */
465
+ windows: WindowStatus[];
466
+ resets: ResetWindow[];
467
+ changes: SlopeChange[];
468
+ /** True when there are Claude weekly readings and no 5-hour ones (Ultra). */
469
+ noFiveHour: boolean;
470
+ /** Readings in the period, Claude and Codex: 0 means nothing is recording them (see howToRecord). */
471
+ readings: number;
472
+ }
473
+ /**
474
+ * The per-reset view of the limits, for a pane or a script: every window's
475
+ * current state, every weekly window with its dollars and tokens per 1%
476
+ * (by entrypoint and model where the readings allow) and every move in that
477
+ * figure, dated. Reads the limits ledger, Codex's rollouts and the
478
+ * transcripts over the readings' span; never writes.
479
+ */
480
+ export declare function limitsByReset(days?: number, opts?: LimitsOptions): LimitsByReset;
288
481
  /** The Claude fit to quote savings in: the weekly window first (savings are weekly), then the 5-hour one. */
289
482
  export declare function claudeLimitFit(r: LimitsReport): LimitFit | undefined;
290
483
  /**
@@ -301,4 +494,19 @@ export declare function inLimit(fit: LimitFit, usd: number, days: number): strin
301
494
  * carry). Kept within 60 columns, as `savings` is.
302
495
  */
303
496
  export declare function limitSavingsLines(fit: LimitFit | undefined, items: Array<[label: string, usd: number]>, days: number): string[];
497
+ /**
498
+ * The one line that says how to start recording Claude readings: the
499
+ * plugin's mod (after each turn, in the Desktop app, the terminal and VS
500
+ * Code), or the autopilot proxy (from the API's rate-limit headers).
501
+ */
502
+ export declare const HOW_TO_RECORD = "To record them: `context-doctor install` (the plugin's mod writes them after each turn, Claude Code 2.1.286+; `install --statusline` on older ones), or `context-doctor autopilot on` (its proxy reads them from the API's replies).";
304
503
  export declare function renderLimits(r: LimitsReport): string;
504
+ /**
505
+ * `limits --report`: a block to paste into a GitHub issue. Per weekly
506
+ * window: % used, dollars and tokens per 1%, the split by entrypoint and
507
+ * model, Claude Code's versions, and every move in what 1% buys, dated. It
508
+ * carries figures, dates, model and entrypoint names and version numbers
509
+ * only: no prompt, no path, no session or account id (reportModel masks a
510
+ * model id that is a cloud resource).
511
+ */
512
+ export declare function renderLimitsReport(r: LimitsReport): string;