context-doctor 0.44.0 → 0.45.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 +2 -1
- package/dist/budget.d.ts +11 -1
- package/dist/budget.js +39 -23
- package/dist/cache.js +5 -4
- package/dist/cachemisses.js +1 -1
- package/dist/cachettl.js +6 -4
- package/dist/ci.js +18 -2
- package/dist/cli.js +10 -3
- package/dist/codexcompact.js +14 -7
- package/dist/coldresume.js +16 -8
- package/dist/compactwindow.js +12 -7
- package/dist/doctor.js +9 -2
- package/dist/extract.d.ts +2 -2
- package/dist/extract.js +14 -3
- package/dist/httpguard.d.ts +9 -1
- package/dist/httpguard.js +5 -4
- package/dist/lens.js +7 -7
- package/dist/limits.d.ts +14 -5
- package/dist/limits.js +26 -17
- package/dist/mcp.js +58 -58
- package/dist/mcpschema.js +5 -3
- package/dist/pack.js +66 -22
- package/dist/pricing.d.ts +105 -6
- package/dist/pricing.js +197 -61
- package/dist/proxy.d.ts +12 -1
- package/dist/proxy.js +13 -1
- package/dist/prune.js +55 -3
- package/dist/savings.js +15 -8
- package/dist/scan.d.ts +7 -1
- package/dist/scan.js +19 -1
- package/dist/sessionprice.d.ts +3 -2
- package/dist/sessionprice.js +16 -8
- package/dist/spend.d.ts +52 -9
- package/dist/spend.js +219 -25
- package/dist/statusline.d.ts +10 -2
- package/dist/statusline.js +17 -8
- package/dist/subagentmodel.d.ts +2 -2
- package/dist/subagentmodel.js +31 -24
- package/dist/subagents.js +11 -10
- package/dist/tasks.js +9 -6
- package/dist/tooloutput.js +14 -5
- package/package.json +1 -1
package/dist/extract.js
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
import { execFile, spawnSync } from "node:child_process";
|
|
22
22
|
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
23
23
|
import { tmpdir } from "node:os";
|
|
24
|
-
import { dirname, extname, join } from "node:path";
|
|
24
|
+
import { dirname, extname, join, resolve } from "node:path";
|
|
25
25
|
export const EXTRACTABLE = new Set([".pdf", ".docx", ".doc", ".rtf", ".odt", ".pptx"]);
|
|
26
26
|
const RUN = { encoding: "utf8", timeout: 30_000, maxBuffer: 64 * 1024 * 1024 };
|
|
27
27
|
function run(cmd, args) {
|
|
@@ -72,8 +72,18 @@ function xmlToText(xml) {
|
|
|
72
72
|
.replace(/\n{3,}/g, "\n\n")
|
|
73
73
|
.trim();
|
|
74
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* The document's path as a converter's argument: absolute, so a file named
|
|
77
|
+
* like an option is never read as one. `pack .` met
|
|
78
|
+
* `-eObjC.import("stdlib");$.system("…");var x={};x.pdf` as that very
|
|
79
|
+
* string, and osascript ran it as a second script statement.
|
|
80
|
+
*/
|
|
81
|
+
function argPath(path) {
|
|
82
|
+
return resolve(path);
|
|
83
|
+
}
|
|
75
84
|
/** The document's text, or the reason it could not be read. */
|
|
76
|
-
export function extractText(
|
|
85
|
+
export function extractText(name) {
|
|
86
|
+
const path = argPath(name);
|
|
77
87
|
const ext = extname(path).toLowerCase();
|
|
78
88
|
const mac = process.platform === "darwin";
|
|
79
89
|
let text;
|
|
@@ -173,7 +183,8 @@ function runAsync(cmd, args, end) {
|
|
|
173
183
|
* `end` on the same clock: a deadline on the wall clock, converted per
|
|
174
184
|
* document, let a clock jump between two documents cut the later ones short.
|
|
175
185
|
*/
|
|
176
|
-
export async function extractTextAsync(
|
|
186
|
+
export async function extractTextAsync(name, end = monotonicNow() + EXTRACT_BUDGET_MS) {
|
|
187
|
+
const path = argPath(name);
|
|
177
188
|
const ext = extname(path).toLowerCase();
|
|
178
189
|
const mac = process.platform === "darwin";
|
|
179
190
|
let text;
|
package/dist/httpguard.d.ts
CHANGED
|
@@ -31,6 +31,12 @@ export interface HttpGuard {
|
|
|
31
31
|
/** Host names (lowercase, no port, no brackets) accepted in Host; "*" accepts any. */
|
|
32
32
|
hosts: Set<string> | "*";
|
|
33
33
|
origins: Set<string>;
|
|
34
|
+
/**
|
|
35
|
+
* Accept any IP address in Host too (the proxy): DNS rebinding needs a
|
|
36
|
+
* host name the attacker controls, and a client of a proxy published from
|
|
37
|
+
* a container may call it by its address.
|
|
38
|
+
*/
|
|
39
|
+
ipLiterals?: boolean;
|
|
34
40
|
}
|
|
35
41
|
/** A Host header's or URL's host name: lowercase, no port, no brackets, no trailing dot. */
|
|
36
42
|
export declare function hostName(hostHeader: string): string;
|
|
@@ -39,7 +45,9 @@ export declare function hostName(hostHeader: string): string;
|
|
|
39
45
|
* --allowed-origins (comma lists) and the CONTEXT_DOCTOR_ALLOWED_HOSTS and
|
|
40
46
|
* CONTEXT_DOCTOR_ALLOWED_ORIGINS environment variables.
|
|
41
47
|
*/
|
|
42
|
-
export declare function makeGuard(bindHost: string, allowedHosts?: string, allowedOrigins?: string, env?: NodeJS.ProcessEnv
|
|
48
|
+
export declare function makeGuard(bindHost: string, allowedHosts?: string, allowedOrigins?: string, env?: NodeJS.ProcessEnv, opts?: {
|
|
49
|
+
ipLiterals?: boolean;
|
|
50
|
+
}): HttpGuard;
|
|
43
51
|
/** Why this request may not reach /mcp, or undefined when it may. */
|
|
44
52
|
export declare function refusal(guard: HttpGuard, headers: IncomingMessage["headers"]): string | undefined;
|
|
45
53
|
/**
|
package/dist/httpguard.js
CHANGED
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
* then handed to the transport, so no request can make the server buffer
|
|
21
21
|
* an unbounded body.
|
|
22
22
|
*/
|
|
23
|
+
import { isIP } from "node:net";
|
|
23
24
|
export const HTTP_LIMITS = { bodyBytes: 10 * 1024 * 1024, bodyMs: 30_000 };
|
|
24
25
|
/** Origins a browser may call from without being listed: ChatGPT's. */
|
|
25
26
|
export const DEFAULT_ALLOWED_ORIGINS = ["https://chatgpt.com", "https://chat.openai.com"];
|
|
@@ -41,17 +42,17 @@ export function hostName(hostHeader) {
|
|
|
41
42
|
* --allowed-origins (comma lists) and the CONTEXT_DOCTOR_ALLOWED_HOSTS and
|
|
42
43
|
* CONTEXT_DOCTOR_ALLOWED_ORIGINS environment variables.
|
|
43
44
|
*/
|
|
44
|
-
export function makeGuard(bindHost, allowedHosts, allowedOrigins, env = process.env) {
|
|
45
|
+
export function makeGuard(bindHost, allowedHosts, allowedOrigins, env = process.env, opts = {}) {
|
|
45
46
|
const extra = [...list(allowedHosts), ...list(env.CONTEXT_DOCTOR_ALLOWED_HOSTS)];
|
|
46
47
|
const origins = new Set([...DEFAULT_ALLOWED_ORIGINS, ...list(allowedOrigins), ...list(env.CONTEXT_DOCTOR_ALLOWED_ORIGINS)].map((o) => o.toLowerCase().replace(/\/+$/, "")));
|
|
47
48
|
if (extra.includes("*"))
|
|
48
|
-
return { hosts: "*", origins };
|
|
49
|
+
return { hosts: "*", origins, ipLiterals: opts.ipLiterals };
|
|
49
50
|
const bind = hostName(bindHost);
|
|
50
51
|
const hosts = new Set([...LOOPBACK_HOSTS, ...extra.map(hostName)]);
|
|
51
52
|
// A specific bind address or name is one the server answers to; a wildcard is not a name.
|
|
52
53
|
if (bind && bind !== "0.0.0.0" && bind !== "::")
|
|
53
54
|
hosts.add(bind);
|
|
54
|
-
return { hosts, origins };
|
|
55
|
+
return { hosts, origins, ipLiterals: opts.ipLiterals };
|
|
55
56
|
}
|
|
56
57
|
/** Why this request may not reach /mcp, or undefined when it may. */
|
|
57
58
|
export function refusal(guard, headers) {
|
|
@@ -59,7 +60,7 @@ export function refusal(guard, headers) {
|
|
|
59
60
|
const host = hostName(String(headers.host ?? ""));
|
|
60
61
|
if (!host)
|
|
61
62
|
return "the request has no Host header";
|
|
62
|
-
if (!guard.hosts.has(host)) {
|
|
63
|
+
if (!guard.hosts.has(host) && !(guard.ipLiterals && isIP(host))) {
|
|
63
64
|
return `Host "${host}" is not one this server answers to. Behind a tunnel or under a host name, start it with --allowed-hosts ${host}`;
|
|
64
65
|
}
|
|
65
66
|
}
|
package/dist/lens.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
import { createHash } from "node:crypto";
|
|
18
18
|
import { forEachLine, listSessions, replyCounter, isClaudeSession } from "./session.js";
|
|
19
19
|
import { rowsOf, slimUsage } from "./scan.js";
|
|
20
|
-
import { openAIUsage,
|
|
20
|
+
import { openAIUsage, requestCost } from "./pricing.js";
|
|
21
21
|
import { formatTokens, realModel } from "./tokens.js";
|
|
22
22
|
const num = (v) => (typeof v === "number" && Number.isFinite(v) && v > 0 ? v : 0);
|
|
23
23
|
class Acc {
|
|
@@ -25,11 +25,11 @@ class Acc {
|
|
|
25
25
|
polls = new Map();
|
|
26
26
|
pollContext = 0;
|
|
27
27
|
sessions = 0;
|
|
28
|
-
request(model, u, reasoning) {
|
|
28
|
+
request(model, u, reasoning, at) {
|
|
29
29
|
const name = model ?? "unknown";
|
|
30
30
|
const m = this.models.get(name) ?? { model: name, requests: 0, outputTokens: 0, reasoningTokens: 0, outputUsd: 0, totalUsd: 0 };
|
|
31
|
-
|
|
32
|
-
const cost =
|
|
31
|
+
// As billed (requestCost): fast mode, US-only, a long prompt's tier, web searches.
|
|
32
|
+
const cost = requestCost(model, u, Number.isFinite(at) ? at : undefined);
|
|
33
33
|
m.requests++;
|
|
34
34
|
m.outputTokens += num(u.output_tokens);
|
|
35
35
|
m.reasoningTokens += Math.min(num(u.output_tokens), reasoning);
|
|
@@ -61,7 +61,7 @@ const callKey = (name, input) => `${name}\u0000${typeof input === "string" ? inp
|
|
|
61
61
|
/** A call's identity, as a hash: equal calls stay equal, and the cache keeps no command or input. */
|
|
62
62
|
const callId = (key) => createHash("sha1").update(key).digest("base64");
|
|
63
63
|
export const LENS_ROWS = {
|
|
64
|
-
id: "lens.
|
|
64
|
+
id: "lens.2",
|
|
65
65
|
init: () => null,
|
|
66
66
|
line(line, row, out) {
|
|
67
67
|
const prompt = line.includes('"type":"user"') && !line.includes('"tool_result"');
|
|
@@ -118,7 +118,7 @@ function scanClaude(path, acc, since, runRows) {
|
|
|
118
118
|
const seen = sighting({ message: { id }, uuid: r.uuid });
|
|
119
119
|
if (seen === "new" && inPeriod) {
|
|
120
120
|
const u = r.u;
|
|
121
|
-
const usd = acc.request(r.model, u, num(u.output_tokens_details?.thinking_tokens));
|
|
121
|
+
const usd = acc.request(r.model, u, num(u.output_tokens_details?.thinking_tokens), at);
|
|
122
122
|
if (id)
|
|
123
123
|
costOf.set(id, { usd, context: num(r.u.input_tokens) + num(r.u.cache_read_input_tokens) + num(r.u.cache_creation_input_tokens) });
|
|
124
124
|
}
|
|
@@ -176,7 +176,7 @@ function scanCodex(path, acc, since) {
|
|
|
176
176
|
return;
|
|
177
177
|
}
|
|
178
178
|
const u = openAIUsage(last);
|
|
179
|
-
const usd = acc.request(model, u, num(last.reasoning_output_tokens));
|
|
179
|
+
const usd = acc.request(model, u, num(last.reasoning_output_tokens), at);
|
|
180
180
|
pending.forEach((name, i) => acc.poll(name, i === 0 ? usd : 0, i === 0 ? num(last.input_tokens) : 0));
|
|
181
181
|
pending = [];
|
|
182
182
|
}
|
package/dist/limits.d.ts
CHANGED
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
* instead of printing a number.
|
|
27
27
|
*/
|
|
28
28
|
import { type Extractor } from "./scan.js";
|
|
29
|
+
import { type ModelPricing, type Modifiers } from "./pricing.js";
|
|
29
30
|
/** Claude Code's two windows, as its status line input names them. */
|
|
30
31
|
export declare const CLAUDE_WINDOWS: readonly ["five_hour", "seven_day"];
|
|
31
32
|
/** One reading of one window: how much of it was used at `ts`. */
|
|
@@ -116,14 +117,22 @@ export interface ClaudeRequest extends UsagePoint {
|
|
|
116
117
|
sidechain?: boolean;
|
|
117
118
|
/** The prompt's part of `usd` (input, cache writes, cache reads): what `savings` bills. */
|
|
118
119
|
promptUsd: number;
|
|
120
|
+
/** The rates it billed at (requestCost: its tier, fast mode, US-only, its date); absent when unpriced. */
|
|
121
|
+
rates?: ModelPricing;
|
|
122
|
+
/** What fast mode, US-only, a long prompt's tier and web searches added to its list price, inside `usd`. */
|
|
123
|
+
modifiers: Modifiers;
|
|
124
|
+
/** Priced by family, or past the day its row's price changed: an estimate. */
|
|
125
|
+
estimated?: boolean;
|
|
126
|
+
/** Its row's price changed on this day, before the request. */
|
|
127
|
+
changedOn?: string;
|
|
119
128
|
}
|
|
120
129
|
/**
|
|
121
130
|
* The requests of one Claude Code transcript between `since` and `until`,
|
|
122
|
-
* each reply once, its output settled on its last row
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
* cannot be read.
|
|
131
|
+
* each reply once, its output settled on its last row (billed at the
|
|
132
|
+
* reply's own rates, requestCost's). `runRows` is shared by every file of
|
|
133
|
+
* one report, so a fork's copy of its parent's replies is counted once, in
|
|
134
|
+
* the file met first (replyCounter). Replies on `<synthetic>`
|
|
135
|
+
* (isUnbilledModel) are left out. Throws when the transcript cannot be read.
|
|
127
136
|
*/
|
|
128
137
|
export declare function claudeFileUsage(file: string, since: number, until?: number, runRows?: Set<string>): ClaudeRequest[];
|
|
129
138
|
/**
|
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,
|
|
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
|
-
|
|
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.
|
|
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
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
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
|
|
390
|
-
|
|
391
|
-
|
|
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
|
|
415
|
-
if (
|
|
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,
|
|
431
|
+
byId.set(m.id, { point, u });
|
|
423
432
|
});
|
|
424
433
|
return out;
|
|
425
434
|
}
|