context-doctor 0.48.0 → 0.49.1

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 (77) hide show
  1. package/README.md +16 -14
  2. package/dist/abclaude.js +1 -1
  3. package/dist/budget.d.ts +5 -2
  4. package/dist/budget.js +21 -7
  5. package/dist/cache.js +3 -3
  6. package/dist/cachemisses.d.ts +3 -0
  7. package/dist/cachemisses.js +8 -0
  8. package/dist/cachettl.js +6 -2
  9. package/dist/ccost.d.ts +195 -0
  10. package/dist/ccost.js +400 -0
  11. package/dist/cli.js +135 -32
  12. package/dist/cline.d.ts +41 -0
  13. package/dist/cline.js +174 -0
  14. package/dist/coldresume.d.ts +11 -0
  15. package/dist/coldresume.js +3 -3
  16. package/dist/coldreturns.d.ts +145 -0
  17. package/dist/coldreturns.js +282 -0
  18. package/dist/compactcost.d.ts +10 -4
  19. package/dist/compactcost.js +12 -7
  20. package/dist/compactwindow.js +2 -2
  21. package/dist/copilotusage.d.ts +78 -0
  22. package/dist/copilotusage.js +193 -0
  23. package/dist/doctor.d.ts +6 -0
  24. package/dist/doctor.js +17 -4
  25. package/dist/fix.d.ts +28 -2
  26. package/dist/fix.js +49 -5
  27. package/dist/install.js +1 -1
  28. package/dist/limits.d.ts +232 -14
  29. package/dist/limits.js +739 -33
  30. package/dist/mcp.js +19 -19
  31. package/dist/mcpdefer.d.ts +47 -0
  32. package/dist/mcpdefer.js +91 -0
  33. package/dist/mod/api.d.ts +72 -2
  34. package/dist/mod/api.js +5 -1
  35. package/dist/mod/logic.d.ts +289 -16
  36. package/dist/mod/logic.js +362 -38
  37. package/dist/mod/register.d.ts +37 -2
  38. package/dist/mod/register.js +290 -9
  39. package/dist/modmeasure.d.ts +6 -4
  40. package/dist/modmeasure.js +8 -4
  41. package/dist/opencode.d.ts +57 -0
  42. package/dist/opencode.js +234 -0
  43. package/dist/orgrates.d.ts +7 -0
  44. package/dist/orgrates.js +19 -0
  45. package/dist/overhead.d.ts +3 -0
  46. package/dist/overhead.js +5 -1
  47. package/dist/pricing.d.ts +59 -2
  48. package/dist/pricing.js +130 -10
  49. package/dist/proxy.d.ts +8 -1
  50. package/dist/proxy.js +13 -1
  51. package/dist/savings.d.ts +5 -1
  52. package/dist/savings.js +11 -5
  53. package/dist/scan.d.ts +3 -1
  54. package/dist/scan.js +8 -1
  55. package/dist/sessionprice.d.ts +5 -0
  56. package/dist/sessionprice.js +5 -1
  57. package/dist/sidecost.d.ts +65 -0
  58. package/dist/sidecost.js +117 -0
  59. package/dist/spend.d.ts +94 -3
  60. package/dist/spend.js +251 -23
  61. package/dist/spendplace.d.ts +40 -0
  62. package/dist/spendplace.js +93 -0
  63. package/dist/spendteam.d.ts +160 -0
  64. package/dist/spendteam.js +286 -0
  65. package/dist/starts.d.ts +106 -0
  66. package/dist/starts.js +406 -0
  67. package/dist/statusline.d.ts +2 -2
  68. package/dist/statusline.js +2 -2
  69. package/dist/subagentmodel.js +9 -7
  70. package/dist/subagentpay.d.ts +31 -2
  71. package/dist/subagentpay.js +232 -68
  72. package/dist/subagents.js +3 -3
  73. package/dist/tasks.js +3 -2
  74. package/dist/tooloutput.js +5 -5
  75. package/dist/usagepane.d.ts +77 -0
  76. package/dist/usagepane.js +287 -0
  77. package/package.json +1 -1
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,26 @@ 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). A reply row keeps its
136
+ * `stop_reason` and `entrypoint` (a prompt suggestion follows a main-thread
137
+ * turn's end, sidecost.ts) and, where it has no `stop_reason`, its content
138
+ * as output tokens (`oc`: what a reply whose rows never settled cost,
139
+ * sidecost.ts). Each `api-request` row (what Claude Code sent, 2.1.308+) as
140
+ * its id, the request it repeats or extends and the time, a reply's
141
+ * `requestRef` to it, and an `api_error` row's code: a request no reply
142
+ * answers and Claude Code sent again a minute or more later is one that hung
143
+ * (ccost.ts names them).
107
144
  */
108
145
  export declare const USAGE_ROWS: Extractor<{
109
146
  ids: Record<string, 1>;
147
+ v?: string;
110
148
  }>;
111
149
  /** A compaction's estimated request as a request point (no message id: its key is the boundary's uuid). */
112
150
  export declare function compactionPoint(c: CompactionRequest): ClaudeRequest;
@@ -130,7 +168,34 @@ export interface ClaudeRequest extends UsagePoint {
130
168
  changedOn?: string;
131
169
  /** A compaction's own request, which no row records usage for: estimated (compactcost.ts). */
132
170
  compaction?: boolean;
171
+ /** Output tokens estimated from the reply's content, inside `output` and `usd`: its rows never settled it (sidecost.ts). */
172
+ unsettled?: number;
173
+ /**
174
+ * The iterations its top-level usage leaves out (extraIterations: an
175
+ * advisor's call, an attempt a fallback replaced), each priced at its own
176
+ * model by requestCost: inside `usd`, `promptUsd` and `modifiers`, not in
177
+ * the token counts above (those are the top-level usage's).
178
+ */
179
+ extra?: IterationPoint[];
180
+ }
181
+ /** One iteration billed beyond its request's top-level usage, priced (requestCost at its own model). */
182
+ export interface IterationPoint {
183
+ type: string;
184
+ model?: string;
185
+ input: number;
186
+ read: number;
187
+ write: number;
188
+ output: number;
189
+ /** 0 when its model has no price (`priced` false). */
190
+ usd: number;
191
+ promptUsd: number;
192
+ priced: boolean;
193
+ modifiers: Modifiers;
194
+ estimated?: boolean;
195
+ changedOn?: string;
133
196
  }
197
+ /** 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`. */
198
+ export declare function iterationPoints(model: string | undefined, u: RecordedUsage, ts: number): IterationPoint[];
134
199
  /**
135
200
  * The requests of one Claude Code transcript between `since` and `until`,
136
201
  * each reply once, its output settled on its last row (billed at the
@@ -153,9 +218,20 @@ export declare function claudeUsageFiles(since: number, tops?: string[]): Array<
153
218
  }>;
154
219
  /** How long before a period a session may have been last written and still have subagents running into it. */
155
220
  export declare const SUBAGENT_LOOKBACK_MS: number;
221
+ /**
222
+ * Which Claude Code wrote a transcript, and when that changed: every row
223
+ * carries `entrypoint` ("cli", "claude-desktop", "sdk-cli" for `claude -p`,
224
+ * "claude-vscode"...) and `version`; a record is kept only where either
225
+ * changes (a session resumed after an update), so a file has one or two.
226
+ */
227
+ export declare const HOST_ROWS: Extractor<{
228
+ ep?: string;
229
+ v?: string;
230
+ }>;
156
231
  /**
157
232
  * Every Claude Code request between `since` and `until`, main threads and
158
- * subagents alike (both draw on the same limits), each reply once.
233
+ * subagents alike (both draw on the same limits), each reply once, with
234
+ * the entrypoint and Claude Code version it was made from.
159
235
  */
160
236
  export declare function claudeUsage(since: number, until?: number, paths?: string[]): UsagePoint[];
161
237
  export interface WindowStatus {
@@ -168,6 +244,8 @@ export interface WindowStatus {
168
244
  resetsAt?: number;
169
245
  /** True when the newest reading's window has reset since (its % is stale). */
170
246
  resetSince: boolean;
247
+ /** 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. */
248
+ stale?: boolean;
171
249
  /** Points per hour over the recent stretch (the last hour of a 5-hour window, the last day otherwise). */
172
250
  recentPerHour?: number;
173
251
  /** Points per hour since the window began, when its length is known. */
@@ -265,6 +343,103 @@ export interface LimitFit {
265
343
  why?: string;
266
344
  }
267
345
  export declare function fitWindow(readings: LimitReading[], usage: UsagePoint[], stretchMs?: number): LimitFit | undefined;
346
+ /**
347
+ * One fit over every reading assumes a percent of the limit always costs the
348
+ * same. It does not: people measured 1% of the week falling from ~$30 to ~$4
349
+ * of API-equivalent use within a week (claude-code#108272), and `claude -p`
350
+ * draining ~1.8x more per token than the terminal (#97074). So each weekly
351
+ * window (one reset to the next) gets its own figure, and the report says
352
+ * when the figure moved, by how much, and on what date.
353
+ */
354
+ /** What one entrypoint or model used inside a window's stretches. */
355
+ export interface UsageShare {
356
+ key: string;
357
+ usd: number;
358
+ tokens: number;
359
+ requests: number;
360
+ /** Its part of the window's API-equivalent dollars, 0-1. */
361
+ share: number;
362
+ /** Dollars per 1% for this key alone, when the stretches tell the keys apart (one column per key); with its relative standard error. */
363
+ usdPerPct?: number;
364
+ relSe?: number;
365
+ }
366
+ /** One weekly window, reset to reset. */
367
+ export interface ResetWindow {
368
+ window: string;
369
+ label: string;
370
+ app: "claude" | "codex";
371
+ /** When it began (its reset less its length) and when it resets, ms. */
372
+ start: number;
373
+ resetsAt: number;
374
+ /** Still running at the report's time. */
375
+ current: boolean;
376
+ readings: number;
377
+ firstReading: number;
378
+ lastReading: number;
379
+ /** The highest % read in it. */
380
+ pctUsed: number;
381
+ /** Points risen in stretches with usage on this machine, and in stretches with none (used elsewhere: claude.ai, another computer). */
382
+ pctMatched: number;
383
+ pctElsewhere: number;
384
+ stretches: number;
385
+ /** Requests, API-equivalent dollars (requestCost, at list price) and tokens of every kind in those stretches. */
386
+ requests: number;
387
+ usd: number;
388
+ tokens: number;
389
+ /** Dollars and tokens per 1% of the limit (ratio of the sums), and the ratio's relative standard error across stretches. */
390
+ usdPerPct?: number;
391
+ tokensPerPct?: number;
392
+ relSe?: number;
393
+ /** Why there is no figure, when there is none. */
394
+ why?: string;
395
+ byEntrypoint: UsageShare[];
396
+ byModel: UsageShare[];
397
+ /** Claude Code versions its requests were made with, oldest first. */
398
+ versions: string[];
399
+ }
400
+ /** A move in what 1% costs. */
401
+ export interface SlopeChange {
402
+ window: string;
403
+ label: string;
404
+ /** "reset": between two windows, dated at the later one's start; "within": inside one window, dated at the stretch where it moved. */
405
+ kind: "reset" | "within";
406
+ at: number;
407
+ /** For "reset", the earlier window's reset (equal to `at` unless a window is missing between them). */
408
+ prevEnd?: number;
409
+ usdPerPctBefore: number;
410
+ usdPerPctAfter: number;
411
+ tokensPerPctBefore?: number;
412
+ tokensPerPctAfter?: number;
413
+ /** after / before: under 1 when 1% buys less (the limit drains faster per dollar). */
414
+ factor: number;
415
+ /** How many standard errors the move is (on the log scale). */
416
+ sigma: number;
417
+ versionsBefore: string[];
418
+ versionsAfter: string[];
419
+ }
420
+ /**
421
+ * Each reset window of one weekly window's readings: % used, dollars and
422
+ * tokens per 1% (over the stretches with usage here; rises with none are
423
+ * counted apart), the split by entrypoint and by model, and the Claude Code
424
+ * versions. `usage` in time order.
425
+ */
426
+ export declare function resetWindowsOf(readings: LimitReading[], usage: UsagePoint[], opts?: {
427
+ now?: number;
428
+ stretchMs?: number;
429
+ }): ResetWindow[];
430
+ /**
431
+ * A model id as a report may carry it: a model's name, never a cloud
432
+ * resource (a Bedrock ARN holds an account number) or anything path-shaped.
433
+ */
434
+ export declare function reportModel(model: string | undefined): string;
435
+ /**
436
+ * Where what 1% costs moved: between consecutive windows that each have a
437
+ * figure (dated at the reset), and inside a window at the one split of its
438
+ * stretches that separates two rates best (dated at that stretch). Reported
439
+ * from a 1.5x move either way, beyond 3 standard errors between windows and
440
+ * 4 inside one (the split is searched for).
441
+ */
442
+ export declare function slopeChanges(windows: ResetWindow[], usage?: UsagePoint[], readings?: LimitReading[], stretchMs?: number): SlopeChange[];
268
443
  export interface LimitsReport {
269
444
  days: number;
270
445
  now: number;
@@ -274,6 +449,14 @@ export interface LimitsReport {
274
449
  codexReadings: number;
275
450
  windows: WindowStatus[];
276
451
  fits: LimitFit[];
452
+ /** Each weekly window, reset to reset: % used, dollars and tokens per 1%, by entrypoint and model (resetWindowsOf). */
453
+ resets: ResetWindow[];
454
+ /** Where what 1% costs moved (slopeChanges). */
455
+ changes: SlopeChange[];
456
+ /** The Claude windows the readings hold: ["seven_day"] alone on a plan with no 5-hour window (Ultra). */
457
+ claudeWindows: string[];
458
+ /** How many Claude readings each recorder wrote ("statusline", "mod", "proxy"; "earlier" for lines from before 0.49). */
459
+ claudeSources: Record<string, number>;
277
460
  }
278
461
  export interface LimitsOptions {
279
462
  now?: number;
@@ -285,6 +468,26 @@ export interface LimitsOptions {
285
468
  claudeOnly?: boolean;
286
469
  }
287
470
  export declare function limitsReport(days?: number, opts?: LimitsOptions): LimitsReport;
471
+ /** What a usage view needs of the limits: each window now, each weekly window reset to reset, and the moves between them. */
472
+ export interface LimitsByReset {
473
+ now: number;
474
+ /** Each window's current %, pace and reset (a Claude 5-hour one only on plans that have it). */
475
+ windows: WindowStatus[];
476
+ resets: ResetWindow[];
477
+ changes: SlopeChange[];
478
+ /** True when there are Claude weekly readings and no 5-hour ones (Ultra). */
479
+ noFiveHour: boolean;
480
+ /** Readings in the period, Claude and Codex: 0 means nothing is recording them (see howToRecord). */
481
+ readings: number;
482
+ }
483
+ /**
484
+ * The per-reset view of the limits, for a pane or a script: every window's
485
+ * current state, every weekly window with its dollars and tokens per 1%
486
+ * (by entrypoint and model where the readings allow) and every move in that
487
+ * figure, dated. Reads the limits ledger, Codex's rollouts and the
488
+ * transcripts over the readings' span; never writes.
489
+ */
490
+ export declare function limitsByReset(days?: number, opts?: LimitsOptions): LimitsByReset;
288
491
  /** The Claude fit to quote savings in: the weekly window first (savings are weekly), then the 5-hour one. */
289
492
  export declare function claudeLimitFit(r: LimitsReport): LimitFit | undefined;
290
493
  /**
@@ -301,4 +504,19 @@ export declare function inLimit(fit: LimitFit, usd: number, days: number): strin
301
504
  * carry). Kept within 60 columns, as `savings` is.
302
505
  */
303
506
  export declare function limitSavingsLines(fit: LimitFit | undefined, items: Array<[label: string, usd: number]>, days: number): string[];
507
+ /**
508
+ * The one line that says how to start recording Claude readings: the
509
+ * plugin's mod (after each turn, in the Desktop app, the terminal and VS
510
+ * Code), or the autopilot proxy (from the API's rate-limit headers).
511
+ */
512
+ 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
513
  export declare function renderLimits(r: LimitsReport): string;
514
+ /**
515
+ * `limits --report`: a block to paste into a GitHub issue. Per weekly
516
+ * window: % used, dollars and tokens per 1%, the split by entrypoint and
517
+ * model, Claude Code's versions, and every move in what 1% buys, dated. It
518
+ * carries figures, dates, model and entrypoint names and version numbers
519
+ * only: no prompt, no path, no session or account id (reportModel masks a
520
+ * model id that is a cloud resource).
521
+ */
522
+ export declare function renderLimitsReport(r: LimitsReport): string;