context-doctor 0.46.0 → 0.48.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 +9 -4
- package/dist/ab.d.ts +51 -3
- package/dist/ab.js +71 -28
- package/dist/abclaude.d.ts +167 -0
- package/dist/abclaude.js +511 -0
- package/dist/abstore.d.ts +46 -0
- package/dist/abstore.js +58 -0
- package/dist/autopilot.js +9 -5
- package/dist/blob.js +66 -12
- package/dist/budget.d.ts +8 -2
- package/dist/budget.js +30 -7
- package/dist/ci.js +2 -1
- package/dist/claudeplugin.d.ts +97 -0
- package/dist/claudeplugin.js +142 -0
- package/dist/cli.js +128 -36
- package/dist/compactcost.d.ts +74 -0
- package/dist/compactcost.js +115 -0
- package/dist/cursor.js +2 -0
- package/dist/doctor.js +90 -18
- package/dist/extract.js +4 -3
- package/dist/fanout.d.ts +15 -0
- package/dist/fanout.js +30 -9
- package/dist/firstfix.d.ts +43 -0
- package/dist/firstfix.js +80 -0
- package/dist/fix.d.ts +53 -4
- package/dist/fix.js +101 -13
- package/dist/httpguard.d.ts +13 -0
- package/dist/httpguard.js +22 -0
- package/dist/install.d.ts +21 -9
- package/dist/install.js +213 -40
- package/dist/limits.d.ts +5 -0
- package/dist/limits.js +26 -2
- package/dist/mcp.d.ts +4 -0
- package/dist/mcp.js +142 -97
- package/dist/mcpclient.js +73 -42
- package/dist/missledger.js +5 -3
- package/dist/mod/api.d.ts +8 -2
- package/dist/mod/api.js +2 -2
- package/dist/mod/logic.d.ts +13 -1
- package/dist/mod/logic.js +24 -2
- package/dist/mod/register.d.ts +2 -0
- package/dist/mod/register.js +19 -5
- package/dist/modelids.d.ts +62 -3
- package/dist/modelids.js +156 -8
- package/dist/modelswitch.js +8 -4
- package/dist/modmeasure.js +13 -1
- package/dist/orgrates.d.ts +18 -0
- package/dist/orgrates.js +51 -2
- package/dist/pack.d.ts +13 -0
- package/dist/pack.js +38 -2
- package/dist/preferences.js +2 -1
- package/dist/pricing.d.ts +8 -0
- package/dist/pricing.js +30 -2
- package/dist/progresssummaries.d.ts +148 -0
- package/dist/progresssummaries.js +248 -0
- package/dist/proxy.d.ts +1 -1
- package/dist/proxy.js +1 -1
- package/dist/prune.d.ts +8 -0
- package/dist/prune.js +11 -1
- package/dist/spend.d.ts +34 -0
- package/dist/spend.js +48 -4
- package/dist/subagentmodel.d.ts +12 -0
- package/dist/subagentmodel.js +17 -4
- package/dist/subagentpay.d.ts +8 -0
- package/dist/subagentpay.js +8 -1
- package/dist/toolcwd.d.ts +14 -0
- package/dist/toolcwd.js +17 -0
- package/package.json +6 -3
package/dist/prune.js
CHANGED
|
@@ -304,7 +304,7 @@ function mcpjsonApprovedBy(name, cwd, home, read, off) {
|
|
|
304
304
|
const projects = read(claudeJsonPath(home)).projects;
|
|
305
305
|
const keys = trustKeys(cwd);
|
|
306
306
|
const entry = (k) => (projects && typeof projects === "object" && projects[k] && typeof projects[k] === "object" ? projects[k] : undefined);
|
|
307
|
-
const trusted = keys.some((k) => entry(k)?.hasTrustDialogAccepted === true);
|
|
307
|
+
const trusted = !persistedTrustIgnored() && keys.some((k) => entry(k)?.hasTrustDialogAccepted === true);
|
|
308
308
|
const legacy = keys.map(entry).find((e) => e && (e.enabledMcpjsonServers !== undefined || e.enableAllProjectMcpServers !== undefined)) ?? {};
|
|
309
309
|
const sources = trusted ? [user, read(join(cwd, ".claude", "settings.json")), legacy, read(localSettingsPath(cwd))] : [user];
|
|
310
310
|
let all = false;
|
|
@@ -313,6 +313,16 @@ function mcpjsonApprovedBy(name, cwd, home, read, off) {
|
|
|
313
313
|
all = trusted ? s.enableAllProjectMcpServers : all || s.enableAllProjectMcpServers;
|
|
314
314
|
return all || sources.some((s) => list(s.enabledMcpjsonServers).some(same));
|
|
315
315
|
}
|
|
316
|
+
/**
|
|
317
|
+
* CLAUDE_CODE_IGNORE_PERSISTED_TRUST, read as Claude Code reads it (2.1.321:
|
|
318
|
+
* a boolean variable, true for 1, true, yes or on). Set, "no workspace trust
|
|
319
|
+
* record counts in this process" (a self-hosted runner sets it when started
|
|
320
|
+
* with --trust-workspace false): ~/.claude.json's hasTrustDialogAccepted is
|
|
321
|
+
* not read, so a project's own settings approve nothing.
|
|
322
|
+
*/
|
|
323
|
+
export function persistedTrustIgnored(env = process.env) {
|
|
324
|
+
return ["1", "true", "yes", "on"].includes(String(env.CLAUDE_CODE_IGNORE_PERSISTED_TRUST ?? "").trim().toLowerCase());
|
|
325
|
+
}
|
|
316
326
|
/**
|
|
317
327
|
* The keys ~/.claude.json may hold this workspace's trust under: the folder
|
|
318
328
|
* and the repository root it is in (Claude Code keys a project by its git
|
package/dist/spend.d.ts
CHANGED
|
@@ -29,11 +29,17 @@
|
|
|
29
29
|
* (scan.ts), so a second run reads only what was appended since; Cursor's
|
|
30
30
|
* replies are kept per chat until the chat changes.
|
|
31
31
|
*
|
|
32
|
+
* Not in the total: Claude Code's subagent progress summaries, side requests
|
|
33
|
+
* no transcript records (progresssummaries.ts). They are estimated per
|
|
34
|
+
* background subagent and shown beside the total as a range, the two ways
|
|
35
|
+
* Anthropic may serve them (short notes, or the whole context).
|
|
36
|
+
*
|
|
32
37
|
* `savings` bills a narrower slice of the same Claude Code requests: the main
|
|
33
38
|
* conversation's input (cache writes and reads included, output not), in
|
|
34
39
|
* sessions with 10+ requests. `spend` counts every request, its output and
|
|
35
40
|
* subagents too; on the requests both count, the dollars are the same.
|
|
36
41
|
*/
|
|
42
|
+
import { type ProgressOptions } from "./progresssummaries.js";
|
|
37
43
|
import { type Modifiers } from "./pricing.js";
|
|
38
44
|
import { type Extractor } from "./scan.js";
|
|
39
45
|
export type Tool = "claude" | "codex" | "gemini" | "cursor";
|
|
@@ -70,6 +76,8 @@ export interface SpendItem {
|
|
|
70
76
|
workflow?: string;
|
|
71
77
|
/** Claude Code: the reply's row uuid, which a fork's copy shares. Codex: the event's codexEventKey, which a forked rollout's copy shares. */
|
|
72
78
|
key?: string;
|
|
79
|
+
/** Claude Code: a compaction's own request, which no row records usage for: estimated from the context it read and its summary (compactcost.ts). */
|
|
80
|
+
compaction?: boolean;
|
|
73
81
|
}
|
|
74
82
|
/**
|
|
75
83
|
* A project folder's name (its path with "-" for separators, as Claude Code
|
|
@@ -210,6 +218,25 @@ export interface SpendReport {
|
|
|
210
218
|
requests: number;
|
|
211
219
|
usd: number;
|
|
212
220
|
}>;
|
|
221
|
+
/** Claude Code's compactions, each one's own request estimated (no row records its usage): inside `usd`; absent when none. */
|
|
222
|
+
compactions?: {
|
|
223
|
+
requests: number;
|
|
224
|
+
usd: number;
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* Claude Code's subagent progress summaries, estimated (no transcript records them): NOT in `usd`.
|
|
228
|
+
* `notesUsd` as short notes (served since November 2026), `forkUsd` as the whole context (before).
|
|
229
|
+
* `set`: CLAUDE_CODE_DISABLE_AGENT_PROGRESS_SUMMARIES is set there, since when unknown: not estimated.
|
|
230
|
+
* Absent when no subagent got any.
|
|
231
|
+
*/
|
|
232
|
+
progressSummaries?: {
|
|
233
|
+
agents: number;
|
|
234
|
+
calls: number;
|
|
235
|
+
notesUsd: number;
|
|
236
|
+
forkUsd: number;
|
|
237
|
+
} | {
|
|
238
|
+
set: string;
|
|
239
|
+
};
|
|
213
240
|
sessions: Array<{
|
|
214
241
|
tool: Tool;
|
|
215
242
|
project: string;
|
|
@@ -236,6 +263,13 @@ export interface SpendOptions {
|
|
|
236
263
|
claudePaths?: string[];
|
|
237
264
|
onProgress?: (done: number, total: number) => void;
|
|
238
265
|
now?: number;
|
|
266
|
+
/**
|
|
267
|
+
* The progress summaries' estimate: the fork gate, when the variable turned them off
|
|
268
|
+
* (fix's record), or where it is set with no record (then not estimated); false: none.
|
|
269
|
+
*/
|
|
270
|
+
progress?: false | (Pick<ProgressOptions, "forkGateOff" | "offSince"> & {
|
|
271
|
+
set?: string;
|
|
272
|
+
});
|
|
239
273
|
}
|
|
240
274
|
/**
|
|
241
275
|
* The longest window: ten years, older than any history these tools keep.
|
package/dist/spend.js
CHANGED
|
@@ -29,6 +29,11 @@
|
|
|
29
29
|
* (scan.ts), so a second run reads only what was appended since; Cursor's
|
|
30
30
|
* replies are kept per chat until the chat changes.
|
|
31
31
|
*
|
|
32
|
+
* Not in the total: Claude Code's subagent progress summaries, side requests
|
|
33
|
+
* no transcript records (progresssummaries.ts). They are estimated per
|
|
34
|
+
* background subagent and shown beside the total as a range, the two ways
|
|
35
|
+
* Anthropic may serve them (short notes, or the whole context).
|
|
36
|
+
*
|
|
32
37
|
* `savings` bills a narrower slice of the same Claude Code requests: the main
|
|
33
38
|
* conversation's input (cache writes and reads included, output not), in
|
|
34
39
|
* sessions with 10+ requests. `spend` counts every request, its output and
|
|
@@ -38,18 +43,19 @@ import { basename, dirname, join } from "node:path";
|
|
|
38
43
|
import { codexDir } from "./codexdir.js";
|
|
39
44
|
import { homedir } from "node:os";
|
|
40
45
|
import { closeSync, existsSync, openSync, readdirSync, readSync, statSync } from "node:fs";
|
|
41
|
-
import { claudeFileUsage, claudeUsageFiles, codexEventKey, codexSessionsRoot, rolloutFiles } from "./limits.js";
|
|
46
|
+
import { claudeFileUsage, claudeUsageFiles, codexEventKey, codexSessionsRoot, rolloutFiles, USAGE_ROWS } from "./limits.js";
|
|
47
|
+
import { estimateProgressSummaries, PROGRESS_ROWS, PROGRESS_SUMMARIES_ENV } from "./progresssummaries.js";
|
|
42
48
|
import { listGeminiChats, readGeminiMessages } from "./gemini.js";
|
|
43
49
|
import { cursorClaudeId, cursorUsage, SqliteUnavailableError } from "./cursor.js";
|
|
44
50
|
import { addModifiers, isUnbilledModel, NO_MODIFIERS, openAIUsage, priceChangedText, requestCost } from "./pricing.js";
|
|
45
|
-
import { orgRates, orgRatesLine, priceBasis } from "./orgrates.js";
|
|
51
|
+
import { managedSourcesLine, orgRates, orgRatesLine, priceBasis } from "./orgrates.js";
|
|
46
52
|
/**
|
|
47
53
|
* What spend's dollars are: each request as it was billed (its tier, fast mode,
|
|
48
54
|
* US-only, web searches) at list rates, or at the organization's rates.
|
|
49
55
|
* "At list prices" was true before those were priced; it no longer is.
|
|
50
56
|
*/
|
|
51
57
|
const spendBasis = () => (orgRates() ? priceBasis() : "as billed at list rates");
|
|
52
|
-
import { pick, rowsOf } from "./scan.js";
|
|
58
|
+
import { pick, prescan, rowsOf } from "./scan.js";
|
|
53
59
|
import { formatTokens } from "./tokens.js";
|
|
54
60
|
import { withInvocation } from "./invocation.js";
|
|
55
61
|
const DAY = 86_400_000;
|
|
@@ -124,6 +130,8 @@ export function claudeSpend(since, until = Infinity, tops, onProgress) {
|
|
|
124
130
|
usd: r.usd, inputUsd, readUsd, writeUsd: Math.max(0, r.promptUsd - inputUsd - readUsd), outputUsd: Math.max(0, r.usd - r.promptUsd - r.modifiers.webSearchUsd), priced: Boolean(price),
|
|
125
131
|
};
|
|
126
132
|
label(item, r);
|
|
133
|
+
if (r.compaction)
|
|
134
|
+
item.compaction = true;
|
|
127
135
|
if (r.model)
|
|
128
136
|
item.model = r.model;
|
|
129
137
|
if (f.run)
|
|
@@ -515,7 +523,12 @@ export function summarizeSpend(items, days, since, until, skipped = []) {
|
|
|
515
523
|
const runs = new Set();
|
|
516
524
|
const mods = { ...NO_MODIFIERS };
|
|
517
525
|
const changed = new Map();
|
|
526
|
+
const compactions = { requests: 0, usd: 0 };
|
|
518
527
|
for (const it of items) {
|
|
528
|
+
if (it.compaction) {
|
|
529
|
+
compactions.requests++;
|
|
530
|
+
compactions.usd += it.usd;
|
|
531
|
+
}
|
|
519
532
|
if (it.modifiers)
|
|
520
533
|
addModifiers(mods, it.modifiers);
|
|
521
534
|
if (it.changedOn) {
|
|
@@ -595,10 +608,19 @@ export function summarizeSpend(items, days, since, until, skipped = []) {
|
|
|
595
608
|
parts,
|
|
596
609
|
...(anyModifier(mods) ? { modifiers: mods } : {}),
|
|
597
610
|
...(changed.size ? { priceChanged: [...changed.values()].sort((a, b) => b.usd - a.usd) } : {}),
|
|
611
|
+
...(compactions.requests ? { compactions } : {}),
|
|
598
612
|
sessions: [...sessions.values()].sort(byUsd),
|
|
599
613
|
skipped,
|
|
600
614
|
};
|
|
601
615
|
}
|
|
616
|
+
/** The subagent progress summaries Claude Code wrote in the window, for the report. */
|
|
617
|
+
function claudeProgressSummaries(since, until, tops, o) {
|
|
618
|
+
if (o?.set && o.offSince === undefined)
|
|
619
|
+
return { set: o.set };
|
|
620
|
+
const files = claudeUsageFiles(since, tops).filter((f) => f.subagent && !f.run).map((f) => f.file);
|
|
621
|
+
const e = estimateProgressSummaries(files, { since, until, forkGateOff: o?.forkGateOff, offSince: o?.offSince });
|
|
622
|
+
return e.calls ? { agents: e.summarized, calls: e.calls, notesUsd: e.notesUsd, forkUsd: e.forkUsd } : undefined;
|
|
623
|
+
}
|
|
602
624
|
/**
|
|
603
625
|
* The longest window: ten years, older than any history these tools keep.
|
|
604
626
|
* Unbounded, `--days 10000000` listed every empty day of it (51 s, and
|
|
@@ -611,6 +633,9 @@ export function spendReport(days = 30, opts = {}) {
|
|
|
611
633
|
const until = opts.now ?? Date.now();
|
|
612
634
|
const since = until - days * DAY;
|
|
613
635
|
const home = opts.home ?? homedir();
|
|
636
|
+
// A subagent's transcript read once for its usage and its timeline (the progress summaries).
|
|
637
|
+
if (opts.progress !== false)
|
|
638
|
+
prescan([{ paths: claudeUsageFiles(since, opts.claudePaths).filter((f) => f.subagent && !f.run).map((f) => f.file), extractors: [USAGE_ROWS, PROGRESS_ROWS] }]);
|
|
614
639
|
const items = [...claudeSpend(since, until, opts.claudePaths, opts.onProgress), ...codexSpend(since, until, home), ...geminiSpend(since, until, home)];
|
|
615
640
|
const skipped = [];
|
|
616
641
|
try {
|
|
@@ -620,7 +645,9 @@ export function spendReport(days = 30, opts = {}) {
|
|
|
620
645
|
if (e instanceof SqliteUnavailableError)
|
|
621
646
|
skipped.push({ tool: "cursor", reason: "reading Cursor's history needs SQLite: Node 22.13+ or the sqlite3 command" });
|
|
622
647
|
}
|
|
623
|
-
|
|
648
|
+
const report = summarizeSpend(items, days, since, until, skipped);
|
|
649
|
+
const progress = opts.progress === false ? undefined : claudeProgressSummaries(since, until, opts.claudePaths, opts.progress);
|
|
650
|
+
return progress ? { ...report, progressSummaries: progress } : report;
|
|
624
651
|
}
|
|
625
652
|
// ---------------------------------------------------------------------------
|
|
626
653
|
// Rendering
|
|
@@ -653,6 +680,9 @@ export function renderSpend(r, top = 8) {
|
|
|
653
680
|
const org = orgRatesLine();
|
|
654
681
|
if (org)
|
|
655
682
|
lines.push(org);
|
|
683
|
+
const ahead = managedSourcesLine();
|
|
684
|
+
if (ahead)
|
|
685
|
+
lines.push(ahead);
|
|
656
686
|
lines.push("─".repeat(60));
|
|
657
687
|
for (const t of r.tools) {
|
|
658
688
|
const unit = t.tool === "cursor" ? "replies" : "requests";
|
|
@@ -692,9 +722,20 @@ export function renderSpend(r, top = 8) {
|
|
|
692
722
|
// What the requests' own billing added to the list rates, already in the parts above.
|
|
693
723
|
if (r.modifiers)
|
|
694
724
|
lines.push(` Of it, billed above list rates: ${modifiersText(r.modifiers)}`);
|
|
725
|
+
// A compaction's own request is in no row's usage: estimated from the context it read and the summary it wrote.
|
|
726
|
+
if (r.compactions)
|
|
727
|
+
lines.push(` Of it, ${r.compactions.requests.toLocaleString("en-US")} Claude Code ${r.compactions.requests === 1 ? "compaction's own request" : "compactions' own requests"} ${usd(r.compactions.usd)}, an estimate (no row records their usage)`);
|
|
695
728
|
const claude = r.tools.find((t) => t.tool === "claude");
|
|
696
729
|
if (claude?.subagentUsd)
|
|
697
730
|
lines.push(` Claude Code subagents ${usd(claude.subagentUsd)} (${share(claude.subagentUsd, r.usd)}): context-doctor subagents says which paid off`);
|
|
731
|
+
// Side requests no transcript records: a range, outside the total.
|
|
732
|
+
const ps = r.progressSummaries;
|
|
733
|
+
if (ps && "calls" in ps) {
|
|
734
|
+
lines.push(` Not in it: ~${ps.calls.toLocaleString("en-US")} subagent progress summaries (${ps.agents} background ${ps.agents === 1 ? "agent" : "agents"}), an estimate:`);
|
|
735
|
+
lines.push(` ${usd(ps.notesUsd)} as notes, ${usd(ps.forkUsd)} at the whole context; no transcript records them`);
|
|
736
|
+
}
|
|
737
|
+
else if (ps)
|
|
738
|
+
lines.push(` Subagent progress summaries: not estimated, ${PROGRESS_SUMMARIES_ENV} is set in ${ps.set}`);
|
|
698
739
|
if (claude?.workflowUsd)
|
|
699
740
|
lines.push(` Claude Code workflow agents ${usd(claude.workflowUsd)} (${share(claude.workflowUsd, r.usd)}), in ${runsText(claude.workflowRuns ?? 0)}`);
|
|
700
741
|
lines.push(" Biggest sessions:");
|
|
@@ -757,6 +798,9 @@ export function renderSpendShare(r) {
|
|
|
757
798
|
lines.push(`Of it, ${usd(r.priceChanged.reduce((s, c) => s + c.usd, 0))} is on models priced past the day their price changed, an estimate.`);
|
|
758
799
|
if (r.modifiers)
|
|
759
800
|
lines.push(`Billed above list rates: ${modifiersText(r.modifiers)}.`);
|
|
801
|
+
const ps = r.progressSummaries;
|
|
802
|
+
if (ps && "calls" in ps)
|
|
803
|
+
lines.push(`Not in it: Claude Code's subagent progress summaries, which no transcript records: ${usd(ps.notesUsd)}-${usd(ps.forkUsd)}, an estimate.`);
|
|
760
804
|
const parts = [["cache writes", r.parts.writeUsd], ["cache reads", r.parts.readUsd], ["output", r.parts.outputUsd], ["uncached input", r.parts.inputUsd], ["web searches", r.parts.webSearchUsd ?? 0]];
|
|
761
805
|
const [bigPart, bigUsd] = parts.sort((a, b) => b[1] - a[1])[0];
|
|
762
806
|
const busiest = [...r.daily].sort((a, b) => b.usd - a.usd)[0];
|
package/dist/subagentmodel.d.ts
CHANGED
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
* Requests on a model no pricier than the candidate stay as billed.
|
|
32
32
|
*/
|
|
33
33
|
import { type Extractor } from "./scan.js";
|
|
34
|
+
import { type ModelPricing, type RecordedUsage } from "./pricing.js";
|
|
34
35
|
import { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME } from "./subagentpay.js";
|
|
35
36
|
export { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME };
|
|
36
37
|
/** The environment variable the lever writes. */
|
|
@@ -114,6 +115,17 @@ export interface SubagentModelOptions {
|
|
|
114
115
|
}
|
|
115
116
|
/** The agent type Claude Code recorded beside a subagent's transcript ("unknown" for an older one without it). */
|
|
116
117
|
export declare function agentTypeOf(transcript: string): string;
|
|
118
|
+
/**
|
|
119
|
+
* A request at the baseline mix: prices are linear in each rate, so it costs
|
|
120
|
+
* the mix of what each model would bill it, each priced by its whole card (an
|
|
121
|
+
* organization's row keeps its own cache-write rate, and its multiplier
|
|
122
|
+
* scales the web searches as requestCost does). A mix of one model is
|
|
123
|
+
* exactly requestCost.
|
|
124
|
+
*/
|
|
125
|
+
export declare function atBaseline(base: ModelPricing, u: RecordedUsage, at: number, mix?: Record<string, number>): {
|
|
126
|
+
totalUsd: number;
|
|
127
|
+
rates: ModelPricing;
|
|
128
|
+
};
|
|
117
129
|
/**
|
|
118
130
|
* Replay every subagent request in [since, until): as billed, and with the
|
|
119
131
|
* requests CLAUDE_CODE_SUBAGENT_MODEL would move at the candidate model.
|
package/dist/subagentmodel.js
CHANGED
|
@@ -40,6 +40,7 @@ import { claudeDir } from "./claudedir.js";
|
|
|
40
40
|
import { subagentTranscripts } from "./cachettl.js";
|
|
41
41
|
import { workflowRunOf } from "./subagents.js";
|
|
42
42
|
import { managedDir } from "./effective.js";
|
|
43
|
+
import { orgRates } from "./orgrates.js";
|
|
43
44
|
import { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME } from "./subagentpay.js";
|
|
44
45
|
export { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME };
|
|
45
46
|
/** The environment variable the lever writes. */
|
|
@@ -251,11 +252,23 @@ function blend(mix, u, at) {
|
|
|
251
252
|
const rate = (k) => parts.reduce((s, [p, w]) => s + (p[k] * w) / total, 0);
|
|
252
253
|
return { inputPerM: rate("inputPerM"), outputPerM: rate("outputPerM"), cacheReadPerM: rate("cacheReadPerM") };
|
|
253
254
|
}
|
|
254
|
-
/**
|
|
255
|
-
|
|
256
|
-
|
|
255
|
+
/**
|
|
256
|
+
* A request at the baseline mix: prices are linear in each rate, so it costs
|
|
257
|
+
* the mix of what each model would bill it, each priced by its whole card (an
|
|
258
|
+
* organization's row keeps its own cache-write rate, and its multiplier
|
|
259
|
+
* scales the web searches as requestCost does). A mix of one model is
|
|
260
|
+
* exactly requestCost.
|
|
261
|
+
*/
|
|
262
|
+
export function atBaseline(base, u, at, mix) {
|
|
257
263
|
const searches = Number(u?.server_tool_use?.web_search_requests) > 0 ? Number(u.server_tool_use.web_search_requests) : 0;
|
|
258
|
-
|
|
264
|
+
const parts = Object.entries(mix ?? {}).flatMap(([m, w]) => { const c = w > 0 ? rateCard(m, u, at) : undefined; return c ? [{ c, w }] : []; });
|
|
265
|
+
const total = parts.reduce((n, p) => n + p.w, 0);
|
|
266
|
+
if (total > 0) {
|
|
267
|
+
const prompt = promptTokens(u);
|
|
268
|
+
const usd = parts.reduce((n, { c, w }) => n + (w / total) * (usageCost(u, ratesAt(c, prompt)).totalUsd + (c.webSearch ? searches * WEB_SEARCH_USD * (c.scale ?? 1) : 0)), 0);
|
|
269
|
+
return { totalUsd: usd, rates: blend(mix, u, at) ?? base };
|
|
270
|
+
}
|
|
271
|
+
return { totalUsd: usageCost(u, base).totalUsd + searches * WEB_SEARCH_USD * (orgRates()?.multiplier ?? 1), rates: base };
|
|
259
272
|
}
|
|
260
273
|
const cheaper = (a, b) => a.inputPerM < b.inputPerM || (a.inputPerM === b.inputPerM && a.outputPerM < b.outputPerM);
|
|
261
274
|
/**
|
package/dist/subagentpay.d.ts
CHANGED
|
@@ -73,6 +73,13 @@ export interface PaybackReport {
|
|
|
73
73
|
}>;
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
|
+
/** A subagent's tokens by what they bill as: uncached input, cache reads, cache writes, output (settled). */
|
|
77
|
+
export interface SubagentTokens {
|
|
78
|
+
input: number;
|
|
79
|
+
read: number;
|
|
80
|
+
write: number;
|
|
81
|
+
output: number;
|
|
82
|
+
}
|
|
76
83
|
/**
|
|
77
84
|
* A subagent's bill (each request as billed, requestCost), steps, startup
|
|
78
85
|
* context, start time, the tool output it read, and the rates its first
|
|
@@ -89,6 +96,7 @@ export declare function scanSubagent(path: string): {
|
|
|
89
96
|
model?: string;
|
|
90
97
|
writeMult: number;
|
|
91
98
|
rates?: ModelPricing;
|
|
99
|
+
tokens: SubagentTokens;
|
|
92
100
|
};
|
|
93
101
|
export declare function subagentPayback(days?: number, paths?: string[], onProgress?: (done: number, total: number) => void): PaybackReport;
|
|
94
102
|
export declare function renderPayback(r: PaybackReport, top?: number): string;
|
package/dist/subagentpay.js
CHANGED
|
@@ -77,6 +77,7 @@ function billed(model, u, at) {
|
|
|
77
77
|
* request billed at.
|
|
78
78
|
*/
|
|
79
79
|
export function scanSubagent(path) {
|
|
80
|
+
const tokens = { input: 0, read: 0, write: 0, output: 0 };
|
|
80
81
|
let costUsd = 0, atSonnetUsd = 0, readTokens = 0, task = "", model, firstRates;
|
|
81
82
|
let steps = 0, startup = 0, start = Infinity, writes5m = 0, writes1h = 0;
|
|
82
83
|
// The rates of the last reply, for the output its later rows settle.
|
|
@@ -109,6 +110,8 @@ export function scanSubagent(path) {
|
|
|
109
110
|
if (seen === "repeat") {
|
|
110
111
|
// A later row of the same reply: only its final output count is new.
|
|
111
112
|
const more = settle(e, false);
|
|
113
|
+
if (more)
|
|
114
|
+
tokens.output += more;
|
|
112
115
|
if (more && last) {
|
|
113
116
|
costUsd += (more * last.rates.outputPerM) / 1e6;
|
|
114
117
|
atSonnetUsd += (more * last.sonnetRates.outputPerM) / 1e6;
|
|
@@ -127,6 +130,10 @@ export function scanSubagent(path) {
|
|
|
127
130
|
if (steps === 0)
|
|
128
131
|
startup = n("input_tokens") + n("cache_creation_input_tokens") + n("cache_read_input_tokens");
|
|
129
132
|
steps++;
|
|
133
|
+
tokens.input += n("input_tokens");
|
|
134
|
+
tokens.read += n("cache_read_input_tokens");
|
|
135
|
+
tokens.write += n("cache_creation_input_tokens");
|
|
136
|
+
tokens.output += n("output_tokens");
|
|
130
137
|
const w = cacheWriteSplit(u);
|
|
131
138
|
writes5m += w.fiveMinute;
|
|
132
139
|
writes1h += w.oneHour;
|
|
@@ -138,7 +145,7 @@ export function scanSubagent(path) {
|
|
|
138
145
|
costUsd += b.usd;
|
|
139
146
|
atSonnetUsd += b.sonnetUsd;
|
|
140
147
|
});
|
|
141
|
-
return { costUsd, atSonnetUsd, readTokens, task: task.replace(/\s+/g, " ").slice(0, 90), steps, startup, start, model, writeMult: writeMultiplier(writes5m, writes1h), ...(firstRates ? { rates: firstRates } : {}) };
|
|
148
|
+
return { costUsd, atSonnetUsd, readTokens, task: task.replace(/\s+/g, " ").slice(0, 90), steps, startup, start, model, writeMult: writeMultiplier(writes5m, writes1h), ...(firstRates ? { rates: firstRates } : {}), tokens };
|
|
142
149
|
}
|
|
143
150
|
function scanParent(path, ids) {
|
|
144
151
|
const view = { requests: [], prompts: [], models: [], billing: [], compactions: [], writes5m: 0, writes1h: 0, returns: new Map() };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The working folder for a program run by its bare name (git, pdftotext,
|
|
3
|
+
* unzip, sqlite3, netstat, schtasks…).
|
|
4
|
+
*
|
|
5
|
+
* On Windows, Node (libuv) looks a bare name up in the child's working
|
|
6
|
+
* folder before PATH, as cmd.exe does. Run from a project, `pack .`, the
|
|
7
|
+
* MCP server's pack_context (whose working folder is the project its client
|
|
8
|
+
* opened) and `ci` ran a git.exe committed to that repository, or a
|
|
9
|
+
* pdftotext.exe beside the documents, instead of the real one. These
|
|
10
|
+
* programs get every path absolute (git with -C), so they run from the
|
|
11
|
+
* user's own temp folder instead, where a clone or a download cannot put a
|
|
12
|
+
* program by that name.
|
|
13
|
+
*/
|
|
14
|
+
export declare function toolCwd(): string;
|
package/dist/toolcwd.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The working folder for a program run by its bare name (git, pdftotext,
|
|
3
|
+
* unzip, sqlite3, netstat, schtasks…).
|
|
4
|
+
*
|
|
5
|
+
* On Windows, Node (libuv) looks a bare name up in the child's working
|
|
6
|
+
* folder before PATH, as cmd.exe does. Run from a project, `pack .`, the
|
|
7
|
+
* MCP server's pack_context (whose working folder is the project its client
|
|
8
|
+
* opened) and `ci` ran a git.exe committed to that repository, or a
|
|
9
|
+
* pdftotext.exe beside the documents, instead of the real one. These
|
|
10
|
+
* programs get every path absolute (git with -C), so they run from the
|
|
11
|
+
* user's own temp folder instead, where a clone or a download cannot put a
|
|
12
|
+
* program by that name.
|
|
13
|
+
*/
|
|
14
|
+
import { tmpdir } from "node:os";
|
|
15
|
+
export function toolCwd() {
|
|
16
|
+
return tmpdir();
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "context-doctor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.48.0",
|
|
4
4
|
"description": "Context and token optimizer for LLM agents: see where Claude Code, Cursor, Codex and Gemini CLI tokens go, then cut them: an every-prompt hook, smarter compaction, an autopilot proxy that edits history only when the prompt cache is cold, RAG chunk packing and an MCP server. Local, no API key. By gAI Ventures.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|
|
@@ -77,12 +77,15 @@
|
|
|
77
77
|
"version": "node scripts/sync-versions.mjs"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
|
+
"@modelcontextprotocol/client": "^2.4.0",
|
|
80
81
|
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
81
|
-
"
|
|
82
|
+
"@modelcontextprotocol/server": "^2.4.0",
|
|
82
83
|
"@types/node": "^22.0.0",
|
|
83
84
|
"esbuild": "^0.25.12",
|
|
84
|
-
"typescript": "^5.5.0"
|
|
85
|
+
"typescript": "^5.5.0",
|
|
86
|
+
"zod": "^4.6.7"
|
|
85
87
|
},
|
|
88
|
+
"overrides": {},
|
|
86
89
|
"homepage": "https://github.com/KushalP1/context-doctor#readme",
|
|
87
90
|
"bugs": {
|
|
88
91
|
"url": "https://github.com/KushalP1/context-doctor/issues"
|