context-doctor 0.49.1 → 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.
- package/README.md +15 -14
- package/dist/appwindow.d.ts +69 -0
- package/dist/appwindow.js +147 -0
- package/dist/ccost.js +8 -4
- package/dist/claudedir.d.ts +15 -0
- package/dist/claudedir.js +19 -1
- package/dist/cli.js +59 -23
- package/dist/clihelp.d.ts +5 -1
- package/dist/clihelp.js +9 -1
- package/dist/codexstatusline.d.ts +176 -0
- package/dist/codexstatusline.js +353 -0
- package/dist/compactwindow.d.ts +14 -2
- package/dist/compactwindow.js +30 -5
- package/dist/credits.d.ts +130 -0
- package/dist/credits.js +265 -0
- package/dist/doctor.d.ts +6 -0
- package/dist/doctor.js +15 -1
- package/dist/fanout.d.ts +2 -0
- package/dist/fanout.js +12 -2
- package/dist/fix.d.ts +37 -1
- package/dist/fix.js +67 -9
- package/dist/limits.d.ts +45 -1
- package/dist/limits.js +121 -9
- package/dist/mcp.d.ts +1 -0
- package/dist/mcp.js +106 -100
- package/dist/mod/api.d.ts +27 -1
- package/dist/mod/api.js +2 -1
- package/dist/mod/logic.d.ts +39 -0
- package/dist/mod/logic.js +42 -1
- package/dist/mod/register.d.ts +4 -0
- package/dist/mod/register.js +25 -1
- package/dist/modelids.js +2 -2
- package/dist/orgrates.d.ts +4 -2
- package/dist/orgrates.js +21 -3
- package/dist/overhead.js +2 -1
- package/dist/pricing.js +7 -0
- package/dist/progresssummaries.d.ts +20 -1
- package/dist/progresssummaries.js +33 -3
- package/dist/proxy.d.ts +1 -1
- package/dist/proxy.js +9 -4
- package/dist/savings.js +3 -1
- package/dist/spend.d.ts +28 -1
- package/dist/spend.js +38 -5
- package/dist/starts.d.ts +27 -1
- package/dist/starts.js +81 -18
- package/package.json +1 -1
package/dist/limits.d.ts
CHANGED
|
@@ -96,6 +96,45 @@ export declare function rateLimitsFromHeaders(get: (name: string) => string | nu
|
|
|
96
96
|
used_percentage: number;
|
|
97
97
|
resets_at?: number;
|
|
98
98
|
}> | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* What a reply's `anthropic-ratelimit-unified-overage-*` headers say about
|
|
101
|
+
* usage credits (Claude's "extra usage": past a plan's limit, requests go on
|
|
102
|
+
* and are paid from credits). Only the four the proxy records, as given:
|
|
103
|
+
* `-overage-status` ("allowed", "allowed_warning", "rejected"),
|
|
104
|
+
* `-overage-in-use` ("true" while requests are being paid from credits),
|
|
105
|
+
* `-overage-utilization` (a fraction of the credits' cap, kept here as a
|
|
106
|
+
* percent in 0.1 steps) and `-overage-reset` (epoch seconds). Read from
|
|
107
|
+
* Claude Code 2.1.311's own parsing of the same headers. Undefined when a
|
|
108
|
+
* reply carries none of them (an API key, Bedrock, Vertex).
|
|
109
|
+
*/
|
|
110
|
+
export interface OverageHeaders {
|
|
111
|
+
status?: string;
|
|
112
|
+
inUse?: boolean;
|
|
113
|
+
pct?: number;
|
|
114
|
+
reset?: number;
|
|
115
|
+
}
|
|
116
|
+
export declare function overageFromHeaders(get: (name: string) => string | null | undefined): OverageHeaders | undefined;
|
|
117
|
+
/** One reading of the credits headers, as the log keeps it. */
|
|
118
|
+
export interface OverageReading {
|
|
119
|
+
ts: number;
|
|
120
|
+
status?: string;
|
|
121
|
+
inUse?: boolean;
|
|
122
|
+
/** Percent of the credits' cap used. */
|
|
123
|
+
pct?: number;
|
|
124
|
+
/** When the credits' cap resets, ms. */
|
|
125
|
+
resetsAt?: number;
|
|
126
|
+
source?: LimitSource;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Append what the credits headers say to the same log as the windows, as a
|
|
130
|
+
* line of its own (`"w": "overage"`, which older versions skip): at
|
|
131
|
+
* once when the status, whether credits are in use, or the reset changed;
|
|
132
|
+
* a moved utilization at most once a minute; an unchanged one every 15
|
|
133
|
+
* minutes. Never throws. Returns the lines written (0 or 1).
|
|
134
|
+
*/
|
|
135
|
+
export declare function recordOverage(o: OverageHeaders | undefined, now?: number, path?: string, source?: LimitSource): number;
|
|
136
|
+
/** Every credits reading in the log since `since`, oldest first. */
|
|
137
|
+
export declare function readOverageReadings(since?: number, path?: string): OverageReading[];
|
|
99
138
|
export declare function codexSessionsRoot(home?: string): string;
|
|
100
139
|
/** Codex rollouts under `root` (YYYY/MM/DD folders, or flat) written since `since`. */
|
|
101
140
|
export declare function rolloutFiles(root: string, since: number): string[];
|
|
@@ -510,7 +549,12 @@ export declare function limitSavingsLines(fit: LimitFit | undefined, items: Arra
|
|
|
510
549
|
* Code), or the autopilot proxy (from the API's rate-limit headers).
|
|
511
550
|
*/
|
|
512
551
|
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).";
|
|
513
|
-
|
|
552
|
+
/**
|
|
553
|
+
* The full `limits` report. `pastLimit`: lines on requests paid from usage
|
|
554
|
+
* credits past a limit (credits.ts creditsLines), placed under the windows;
|
|
555
|
+
* none when nothing went past one.
|
|
556
|
+
*/
|
|
557
|
+
export declare function renderLimits(r: LimitsReport, pastLimit?: string[]): string;
|
|
514
558
|
/**
|
|
515
559
|
* `limits --report`: a block to paste into a GitHub issue. Per weekly
|
|
516
560
|
* window: % used, dollars and tokens per 1%, the split by entrypoint and
|
package/dist/limits.js
CHANGED
|
@@ -153,17 +153,21 @@ export function recordLimits(rateLimits, now = Date.now(), path = limitsLogPath(
|
|
|
153
153
|
});
|
|
154
154
|
if (!due.length)
|
|
155
155
|
return 0;
|
|
156
|
-
|
|
157
|
-
const text = tail(path, LOG_KEEP_BYTES);
|
|
158
|
-
writeFileSync(path, text.slice(text.indexOf("\n") + 1));
|
|
159
|
-
}
|
|
160
|
-
appendFileSync(path, due.map((l) => JSON.stringify(l)).join("\n") + "\n");
|
|
156
|
+
appendLog(path, due);
|
|
161
157
|
return due.length;
|
|
162
158
|
}
|
|
163
159
|
catch {
|
|
164
160
|
return 0;
|
|
165
161
|
}
|
|
166
162
|
}
|
|
163
|
+
/** Append lines to the log, keeping its newest LOG_KEEP_BYTES once it passes LOG_ROTATE_AT. Throws on a write error (callers catch). */
|
|
164
|
+
function appendLog(path, lines) {
|
|
165
|
+
if (existsSync(path) && statSync(path).size > LOG_ROTATE_AT) {
|
|
166
|
+
const text = tail(path, LOG_KEEP_BYTES);
|
|
167
|
+
writeFileSync(path, text.slice(text.indexOf("\n") + 1));
|
|
168
|
+
}
|
|
169
|
+
appendFileSync(path, lines.map((l) => JSON.stringify(l)).join("\n") + "\n");
|
|
170
|
+
}
|
|
167
171
|
/** Every Claude reading in the log since `since`. */
|
|
168
172
|
export function readClaudeReadings(since = 0, path = limitsLogPath()) {
|
|
169
173
|
if (!existsSync(path))
|
|
@@ -206,6 +210,98 @@ export function rateLimitsFromHeaders(get) {
|
|
|
206
210
|
}
|
|
207
211
|
return Object.keys(out).length ? out : undefined;
|
|
208
212
|
}
|
|
213
|
+
export function overageFromHeaders(get) {
|
|
214
|
+
const h = (n) => {
|
|
215
|
+
const v = get(`anthropic-ratelimit-unified-overage-${n}`);
|
|
216
|
+
return typeof v === "string" && v.trim() !== "" ? v.trim() : undefined;
|
|
217
|
+
};
|
|
218
|
+
const out = {};
|
|
219
|
+
const status = h("status");
|
|
220
|
+
if (status && /^[a-z_]{1,40}$/.test(status))
|
|
221
|
+
out.status = status;
|
|
222
|
+
const inUse = h("in-use");
|
|
223
|
+
if (inUse === "true" || inUse === "false")
|
|
224
|
+
out.inUse = inUse === "true";
|
|
225
|
+
const u = Number(h("utilization"));
|
|
226
|
+
if (h("utilization") !== undefined && Number.isFinite(u) && u >= 0 && u <= 10)
|
|
227
|
+
out.pct = Math.round(u * 1000) / 10;
|
|
228
|
+
const reset = Number(h("reset"));
|
|
229
|
+
if (h("reset") !== undefined && Number.isFinite(reset) && reset > 0)
|
|
230
|
+
out.reset = Math.round(reset);
|
|
231
|
+
return Object.keys(out).length ? out : undefined;
|
|
232
|
+
}
|
|
233
|
+
function parseOverageLine(line) {
|
|
234
|
+
let e;
|
|
235
|
+
try {
|
|
236
|
+
e = JSON.parse(line);
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
if (!e || typeof e !== "object" || e.w !== "overage" || typeof e.ts !== "number" || !Number.isFinite(e.ts))
|
|
242
|
+
return undefined;
|
|
243
|
+
return {
|
|
244
|
+
ts: e.ts, app: "claude", w: "overage",
|
|
245
|
+
...(typeof e.status === "string" && /^[a-z_]{1,40}$/.test(e.status) ? { status: e.status } : {}),
|
|
246
|
+
...(typeof e.inUse === "boolean" ? { inUse: e.inUse } : {}),
|
|
247
|
+
...(typeof e.pct === "number" && Number.isFinite(e.pct) && e.pct >= 0 && e.pct <= 1000 ? { pct: e.pct } : {}),
|
|
248
|
+
...(typeof e.reset === "number" && Number.isFinite(e.reset) && e.reset > 0 ? { reset: e.reset } : {}),
|
|
249
|
+
...(typeof e.src === "string" && SOURCES.includes(e.src) ? { src: e.src } : {}),
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Append what the credits headers say to the same log as the windows, as a
|
|
254
|
+
* line of its own (`"w": "overage"`, which older versions skip): at
|
|
255
|
+
* once when the status, whether credits are in use, or the reset changed;
|
|
256
|
+
* a moved utilization at most once a minute; an unchanged one every 15
|
|
257
|
+
* minutes. Never throws. Returns the lines written (0 or 1).
|
|
258
|
+
*/
|
|
259
|
+
export function recordOverage(o, now = Date.now(), path = limitsLogPath(), source = "proxy") {
|
|
260
|
+
if (!o)
|
|
261
|
+
return 0;
|
|
262
|
+
const line = { ts: now, app: "claude", w: "overage", ...o, src: source };
|
|
263
|
+
try {
|
|
264
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
265
|
+
let last;
|
|
266
|
+
if (existsSync(path)) {
|
|
267
|
+
for (const l of tail(path, 16 * 1024).split("\n"))
|
|
268
|
+
last = parseOverageLine(l) ?? last;
|
|
269
|
+
}
|
|
270
|
+
const sameReset = last && (last.reset !== undefined && line.reset !== undefined ? Math.abs(last.reset - line.reset) * 1000 <= SAME_RESET_MS : last.reset === line.reset);
|
|
271
|
+
const due = !last || now < last.ts || last.status !== line.status || last.inUse !== line.inUse || !sameReset
|
|
272
|
+
? true
|
|
273
|
+
: last.pct === line.pct ? now - last.ts >= KEEPALIVE_MS : now - last.ts >= THROTTLE_MS;
|
|
274
|
+
if (!due)
|
|
275
|
+
return 0;
|
|
276
|
+
appendLog(path, [line]);
|
|
277
|
+
return 1;
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
return 0;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/** Every credits reading in the log since `since`, oldest first. */
|
|
284
|
+
export function readOverageReadings(since = 0, path = limitsLogPath()) {
|
|
285
|
+
if (!existsSync(path))
|
|
286
|
+
return [];
|
|
287
|
+
let text;
|
|
288
|
+
try {
|
|
289
|
+
text = readFileSync(path, "utf8");
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
return [];
|
|
293
|
+
}
|
|
294
|
+
const out = [];
|
|
295
|
+
for (const line of text.split("\n")) {
|
|
296
|
+
if (!line.includes('"overage"'))
|
|
297
|
+
continue;
|
|
298
|
+
const l = parseOverageLine(line);
|
|
299
|
+
if (!l || l.ts < since)
|
|
300
|
+
continue;
|
|
301
|
+
out.push({ ts: l.ts, ...(l.status ? { status: l.status } : {}), ...(l.inUse !== undefined ? { inUse: l.inUse } : {}), ...(l.pct !== undefined ? { pct: l.pct } : {}), ...(l.reset ? { resetsAt: l.reset * 1000 } : {}), ...(l.src ? { source: l.src } : {}) });
|
|
302
|
+
}
|
|
303
|
+
return out.sort((a, b) => a.ts - b.ts);
|
|
304
|
+
}
|
|
209
305
|
// ---------------------------------------------------------------------------
|
|
210
306
|
// Codex: readings and usage, both from its rollouts
|
|
211
307
|
// ---------------------------------------------------------------------------
|
|
@@ -787,7 +883,15 @@ export function windowStatus(readings, now = Date.now()) {
|
|
|
787
883
|
const period = readings.slice(first);
|
|
788
884
|
const lookback = last.windowMs && last.windowMs <= 5 * HOUR ? HOUR : DAY;
|
|
789
885
|
const recent = period.filter((r) => r.ts >= last.ts - lookback);
|
|
790
|
-
|
|
886
|
+
// A window that began within the lookback was at 0% when it began: the
|
|
887
|
+
// pace counts from there. From the period's first reading alone, a weekly
|
|
888
|
+
// window read for the first time hours after its reset (nothing used
|
|
889
|
+
// since) took ten minutes' burst for the day's pace and said a 2% week
|
|
890
|
+
// ran out in 8 hours.
|
|
891
|
+
const start = last.windowMs && last.resetsAt ? last.resetsAt - last.windowMs : undefined;
|
|
892
|
+
const from = start !== undefined && recent[0] && start >= last.ts - lookback && start < recent[0].ts
|
|
893
|
+
? { ts: start, pct: 0 }
|
|
894
|
+
: recent[0];
|
|
791
895
|
if (from && last.ts - from.ts >= 10 * MIN) {
|
|
792
896
|
status.recentPerHour = Math.max(0, ((last.pct - from.pct) / (last.ts - from.ts)) * HOUR);
|
|
793
897
|
}
|
|
@@ -1562,7 +1666,12 @@ function splitLine(list) {
|
|
|
1562
1666
|
const ratio = slow.usdPerPct / fast.usdPerPct;
|
|
1563
1667
|
return `${sorted.map((t) => `${t.key} ${dollars(t.usdPerPct)}${plusMinus(t.relSe)}`).join(", ")} per 1%${ratio >= 1.1 ? ` (${fast.key} drains ${ratio.toFixed(1)}x per dollar against ${slow.key})` : ""}`;
|
|
1564
1668
|
}
|
|
1565
|
-
|
|
1669
|
+
/**
|
|
1670
|
+
* The full `limits` report. `pastLimit`: lines on requests paid from usage
|
|
1671
|
+
* credits past a limit (credits.ts creditsLines), placed under the windows;
|
|
1672
|
+
* none when nothing went past one.
|
|
1673
|
+
*/
|
|
1674
|
+
export function renderLimits(r, pastLimit = []) {
|
|
1566
1675
|
const out = ["USAGE LIMITS", "═".repeat(60)];
|
|
1567
1676
|
if (!r.windows.length && !r.resets.length) {
|
|
1568
1677
|
out.push(`No limit readings yet. ${HOW_TO_RECORD}`, "", `Claude readings are kept in ${r.claudeLog.replace(homedir(), "~")}; Codex's are read from ~/.codex/sessions (none in the period).`);
|
|
@@ -1584,8 +1693,8 @@ export function renderLimits(r) {
|
|
|
1584
1693
|
else if (s.recentPerHour === 0)
|
|
1585
1694
|
out.push(" Pace: no rise over the recent readings.");
|
|
1586
1695
|
else {
|
|
1587
|
-
const avg = s.averagePerHour !== undefined ? `; ${pts(s.averagePerHour)}
|
|
1588
|
-
out.push(` Pace: ${pts(s.recentPerHour)}
|
|
1696
|
+
const avg = s.averagePerHour !== undefined ? `; ${pts(s.averagePerHour)}% an hour on average this window` : "";
|
|
1697
|
+
out.push(` Pace: ${pts(s.recentPerHour)}% of the limit an hour lately${avg}.`);
|
|
1589
1698
|
if (s.runsOutAt !== undefined) {
|
|
1590
1699
|
out.push(s.beforeReset
|
|
1591
1700
|
? ` At that pace it runs out ${clock(s.runsOutAt, r.now)} (in ${hm(s.runsOutAt - r.now)}), before the reset.`
|
|
@@ -1596,6 +1705,9 @@ export function renderLimits(r) {
|
|
|
1596
1705
|
if (r.claudeWindows.length === 1 && r.claudeWindows[0] === "seven_day") {
|
|
1597
1706
|
out.push("", "No 5-hour window in Claude's readings: a plan without one (Ultra), so the weekly window is the limit.");
|
|
1598
1707
|
}
|
|
1708
|
+
if (pastLimit.length) {
|
|
1709
|
+
out.push("", "Past your plan's limit: usage credits", "─".repeat(60), ...pastLimit);
|
|
1710
|
+
}
|
|
1599
1711
|
if (r.resets.length) {
|
|
1600
1712
|
out.push("", "Each weekly (or longer) window, reset to reset: API-equivalent use per 1% (UTC dates)");
|
|
1601
1713
|
out.push("─".repeat(60));
|
package/dist/mcp.d.ts
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
* save_handoff — store a handoff brief the next chat starts from (a chat's history cannot be cut)
|
|
19
19
|
* recall — search this session's own transcript (compacted turns, cleared output); stdio only
|
|
20
20
|
* Prompts: context_checkup, handoff, ask_file. Resources (stdio): handoff://latest, handoff://<id>.
|
|
21
|
+
* Codex only (stdio): the `statusline` tool and ui://context-doctor/statusline, its footer (codexstatusline.ts).
|
|
21
22
|
*
|
|
22
23
|
* Built on the MCP SDK's v2 packages: one server definition answers both
|
|
23
24
|
* protocol eras, a 2025-era client's `initialize` (2024-10-07 to 2025-11-25)
|