context-doctor 0.42.0 → 0.44.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 +6 -3
- package/dist/budget.d.ts +52 -0
- package/dist/budget.js +209 -59
- package/dist/cachettl.d.ts +5 -1
- package/dist/cachettl.js +10 -25
- package/dist/cli.js +33 -15
- package/dist/codexcompact.d.ts +137 -0
- package/dist/codexcompact.js +581 -0
- package/dist/compactwindow.js +2 -2
- package/dist/cursor.d.ts +10 -0
- package/dist/cursor.js +20 -0
- package/dist/doctor.d.ts +9 -0
- package/dist/doctor.js +55 -0
- package/dist/fanout.js +4 -2
- package/dist/fix.d.ts +38 -4
- package/dist/fix.js +249 -21
- package/dist/hook.js +65 -16
- package/dist/install.js +56 -19
- package/dist/ledger.d.ts +2 -1
- package/dist/ledger.js +1 -0
- package/dist/limits.d.ts +6 -2
- package/dist/limits.js +19 -16
- package/dist/mcp.js +63 -63
- package/dist/mcpclient.js +19 -19
- package/dist/mcpschema.js +5 -9
- package/dist/overhead.d.ts +7 -2
- package/dist/overhead.js +11 -3
- package/dist/pack.js +23 -3
- package/dist/pricing.d.ts +25 -5
- package/dist/pricing.js +157 -48
- package/dist/proxy.d.ts +15 -1
- package/dist/proxy.js +33 -5
- package/dist/prune.d.ts +8 -0
- package/dist/prune.js +20 -9
- package/dist/savings.js +2 -1
- package/dist/sessionprice.d.ts +77 -0
- package/dist/sessionprice.js +267 -0
- package/dist/settingsedit.js +3 -2
- package/dist/spend.d.ts +25 -3
- package/dist/spend.js +67 -19
- package/dist/subagentmodel.js +11 -1
- package/dist/subagentpay.d.ts +17 -0
- package/dist/subagentpay.js +41 -2
- package/dist/subagents.d.ts +24 -0
- package/dist/subagents.js +82 -17
- package/dist/tooloutput.d.ts +120 -0
- package/dist/tooloutput.js +357 -0
- package/package.json +1 -1
package/dist/subagentmodel.js
CHANGED
|
@@ -38,6 +38,7 @@ import { pricingFor, usageCost } from "./pricing.js";
|
|
|
38
38
|
import { realModel } from "./tokens.js";
|
|
39
39
|
import { claudeDir } from "./claudedir.js";
|
|
40
40
|
import { subagentTranscripts } from "./cachettl.js";
|
|
41
|
+
import { workflowRunOf } from "./subagents.js";
|
|
41
42
|
import { managedDir } from "./effective.js";
|
|
42
43
|
import { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME } from "./subagentpay.js";
|
|
43
44
|
export { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME };
|
|
@@ -56,6 +57,11 @@ const BUILT_IN = {
|
|
|
56
57
|
fork: "inherit",
|
|
57
58
|
"claude-code-guide": "haiku",
|
|
58
59
|
"statusline-setup": "sonnet",
|
|
60
|
+
// A workflow agent (subagents/workflows/<run>/): read from the 2.1.317 binary, its model is the
|
|
61
|
+
// script's agent() `model`, then CLAUDE_CODE_WORKFLOW_SUBAGENT_MODEL, then this lever's
|
|
62
|
+
// CLAUDE_CODE_SUBAGENT_MODEL, then the main conversation's. Scripts seldom name one (none of the
|
|
63
|
+
// author's 9 runs did), so the variable moves them.
|
|
64
|
+
"workflow-subagent": undefined,
|
|
59
65
|
};
|
|
60
66
|
/** Types that only read (no Edit or Write): the ones a smaller model is most often enough for. */
|
|
61
67
|
const READ_ONLY = new Set(["Explore", "Plan"]);
|
|
@@ -227,7 +233,8 @@ export function agentTypeOf(transcript) {
|
|
|
227
233
|
catch {
|
|
228
234
|
/* older Claude Code: no meta */
|
|
229
235
|
}
|
|
230
|
-
|
|
236
|
+
// A workflow agent's folder says what it is when its meta file is missing.
|
|
237
|
+
return workflowRunOf(transcript) ? "workflow-subagent" : "unknown";
|
|
231
238
|
}
|
|
232
239
|
const tokens = (v) => (typeof v === "number" && Number.isFinite(v) && v > 0 ? v : 0);
|
|
233
240
|
const promptTokens = (u) => tokens(u?.input_tokens) + tokens(u?.cache_read_input_tokens) + tokens(u?.cache_creation_input_tokens);
|
|
@@ -377,6 +384,9 @@ export function renderSubagentTypes(r) {
|
|
|
377
384
|
const head = `${t.type.slice(0, 19).padEnd(20)}${String(t.agents).padStart(6)}${usd(t.billedUsd).padStart(11)}`;
|
|
378
385
|
out.push(head + (t.moves ? usd(t.atCandidateUsd).padStart(col.length + 3) : ` unchanged: ${t.why}`));
|
|
379
386
|
}
|
|
387
|
+
// Workflow agents follow this variable too; Claude Code has one of their own, which this lever does not set.
|
|
388
|
+
if (r.byType.some((t) => t.type === "workflow-subagent"))
|
|
389
|
+
out.push("Workflow agents move with it unless the workflow's script names a model. For them alone: CLAUDE_CODE_WORKFLOW_SUBAGENT_MODEL (it also overrides a model the script names).");
|
|
380
390
|
const ro = r.byType.filter((t) => t.readOnly);
|
|
381
391
|
if (ro.length) {
|
|
382
392
|
const billed = ro.reduce((s, t) => s + t.billedUsd, 0);
|
package/dist/subagentpay.d.ts
CHANGED
|
@@ -54,6 +54,23 @@ export interface PaybackReport {
|
|
|
54
54
|
days: number;
|
|
55
55
|
sessions: number;
|
|
56
56
|
subagents: SubagentPayback[];
|
|
57
|
+
/**
|
|
58
|
+
* Workflow agents (subagents/workflows/<run>/): their bill, by run. Not
|
|
59
|
+
* weighed against the parent: a workflow is work the parent hands off
|
|
60
|
+
* whole, not a read it would otherwise do itself.
|
|
61
|
+
*/
|
|
62
|
+
workflows?: {
|
|
63
|
+
runs: number;
|
|
64
|
+
agents: number;
|
|
65
|
+
costUsd: number;
|
|
66
|
+
atSonnetUsd: number;
|
|
67
|
+
byRun: Array<{
|
|
68
|
+
name: string;
|
|
69
|
+
agents: number;
|
|
70
|
+
costUsd: number;
|
|
71
|
+
start: number;
|
|
72
|
+
}>;
|
|
73
|
+
};
|
|
57
74
|
}
|
|
58
75
|
/** A subagent's bill (usage priced as the cache bills it), steps, startup context, start time and the tool output it read. */
|
|
59
76
|
export declare function scanSubagent(path: string): {
|
package/dist/subagentpay.js
CHANGED
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
28
28
|
import { basename, join } from "node:path";
|
|
29
29
|
import { forEachLine, listSessions, outputSettler, replyCounter, isClaudeSession } from "./session.js";
|
|
30
|
+
import { subagentFiles, workflowRunInfo } from "./subagents.js";
|
|
30
31
|
import { estimateTokens, formatTokens, realModel } from "./tokens.js";
|
|
31
32
|
import { cacheWriteSplit, formatUsd, pricingFor, usageCost, writeMultiplier } from "./pricing.js";
|
|
32
33
|
/**
|
|
@@ -171,8 +172,29 @@ export function subagentPayback(days = 30, paths, onProgress) {
|
|
|
171
172
|
const sessions = paths ?? listSessions(10_000).filter((s) => isClaudeSession(s) && s.modifiedAt.getTime() >= since).map((s) => s.path);
|
|
172
173
|
const out = [];
|
|
173
174
|
let withSubagents = 0;
|
|
175
|
+
const runs = new Map();
|
|
174
176
|
for (const [index, parent] of sessions.entries()) {
|
|
175
177
|
onProgress?.(index, sessions.length);
|
|
178
|
+
for (const f of subagentFiles(parent)) {
|
|
179
|
+
if (!f.run)
|
|
180
|
+
continue;
|
|
181
|
+
let w;
|
|
182
|
+
try {
|
|
183
|
+
w = scanSubagent(f.file);
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
// Each by its own start, as the Agent tool's below.
|
|
189
|
+
if (!(w.steps > 0) || (Number.isFinite(w.start) && w.start < since))
|
|
190
|
+
continue;
|
|
191
|
+
const key = `${parent}\u0000${f.run}`;
|
|
192
|
+
const r = runs.get(key) ?? runs.set(key, { name: workflowRunInfo(parent, f.run).name ?? f.run, agents: 0, costUsd: 0, atSonnetUsd: 0, start: w.start }).get(key);
|
|
193
|
+
r.agents++;
|
|
194
|
+
r.costUsd += w.costUsd;
|
|
195
|
+
r.atSonnetUsd += w.atSonnetUsd;
|
|
196
|
+
r.start = Math.min(r.start, w.start);
|
|
197
|
+
}
|
|
176
198
|
const dir = join(parent.replace(/\.jsonl$/, ""), "subagents");
|
|
177
199
|
if (!existsSync(dir))
|
|
178
200
|
continue;
|
|
@@ -241,13 +263,28 @@ export function subagentPayback(days = 30, paths, onProgress) {
|
|
|
241
263
|
if (out.length > before)
|
|
242
264
|
withSubagents++;
|
|
243
265
|
}
|
|
244
|
-
|
|
266
|
+
const byRun = [...runs.values()].sort((a, b) => b.costUsd - a.costUsd);
|
|
267
|
+
const workflows = byRun.length
|
|
268
|
+
? { runs: byRun.length, agents: byRun.reduce((n, r) => n + r.agents, 0), costUsd: byRun.reduce((n, r) => n + r.costUsd, 0), atSonnetUsd: byRun.reduce((n, r) => n + r.atSonnetUsd, 0), byRun: byRun.map(({ atSonnetUsd: _s, ...r }) => r) }
|
|
269
|
+
: undefined;
|
|
270
|
+
return { days, sessions: withSubagents, subagents: out, ...(workflows ? { workflows } : {}) };
|
|
271
|
+
}
|
|
272
|
+
/** The workflow agents' lines of `subagents`: what they cost, by run. */
|
|
273
|
+
function renderWorkflows(w, top) {
|
|
274
|
+
const out = ["", `Workflow agents: ${w.agents} in ${w.runs} workflow run${w.runs === 1 ? "" : "s"}, ${formatUsd(w.costUsd)} at list price (each run's work handed off whole, so not weighed against the parent).`];
|
|
275
|
+
if (w.costUsd - w.atSonnetUsd >= 1)
|
|
276
|
+
out.push(`At ${SUBAGENT_CANDIDATE_NAME} (where they ran on a pricier model): ~${formatUsd(w.atSonnetUsd)}, token counts as recorded: an estimate.`);
|
|
277
|
+
for (const r of w.byRun.slice(0, top))
|
|
278
|
+
out.push(` ${formatUsd(r.costUsd).padStart(9)} ${String(r.agents).padStart(3)} agents ${r.name.slice(0, 60)}`);
|
|
279
|
+
if (w.byRun.length > top)
|
|
280
|
+
out.push(` … and ${w.byRun.length - top} more`);
|
|
281
|
+
return out;
|
|
245
282
|
}
|
|
246
283
|
export function renderPayback(r, top = 8) {
|
|
247
284
|
const out = ["DO SUBAGENTS PAY FOR THEMSELVES?", "═".repeat(56)];
|
|
248
285
|
const all = r.subagents;
|
|
249
286
|
if (!all.length)
|
|
250
|
-
return [...out, `No subagents in the last ${r.days} days
|
|
287
|
+
return [...out, `No subagents in the last ${r.days} days.`, ...(r.workflows ? renderWorkflows(r.workflows, top) : [])].join("\n");
|
|
251
288
|
const cost = all.reduce((s, a) => s + a.costUsd, 0);
|
|
252
289
|
const inParent = all.reduce((s, a) => s + a.inParentUsd, 0);
|
|
253
290
|
const paid = all.filter((a) => a.netUsd > 0);
|
|
@@ -277,5 +314,7 @@ export function renderPayback(r, top = 8) {
|
|
|
277
314
|
out.push(` −${formatUsd(-a.netUsd).padEnd(8)} ${String(a.steps).padStart(3)} steps, parent at ~${formatTokens(a.parentContextTokens)}, read ~${formatTokens(a.readTokens)} ${a.task}`);
|
|
278
315
|
}
|
|
279
316
|
out.push("", "Counts context only: a subagent can still be worth it for parallel time or a clean window on a hard task.", "Subagents pay most when the parent is already large (every step there re-reads it) and least for short tasks from a small parent.");
|
|
317
|
+
if (r.workflows)
|
|
318
|
+
out.push(...renderWorkflows(r.workflows, top));
|
|
280
319
|
return out.join("\n");
|
|
281
320
|
}
|
package/dist/subagents.d.ts
CHANGED
|
@@ -37,9 +37,33 @@ export interface SubagentReport {
|
|
|
37
37
|
totalCostUsd: number;
|
|
38
38
|
/** Agents whose model has no price on file, so the total undercounts. */
|
|
39
39
|
unpriced: number;
|
|
40
|
+
/** How many of `agents` are workflow agents (subagents/workflows/<run>/), in how many runs. */
|
|
41
|
+
workflowAgents?: number;
|
|
42
|
+
workflowRuns?: number;
|
|
40
43
|
}
|
|
41
44
|
/** Where Claude Code keeps a session's subagents: beside the transcript, under its id. */
|
|
42
45
|
export declare function subagentDir(transcriptPath: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Every subagent transcript of a session: the Agent tool's
|
|
48
|
+
* (`subagents/agent-*.jsonl`) and the workflow agents'
|
|
49
|
+
* (`subagents/workflows/<run>/agent-*.jsonl`, one folder per workflow run,
|
|
50
|
+
* the run's own record in `<session>/workflows/<run>.json`). Workflow agents
|
|
51
|
+
* bill to the session like any subagent, and every reader skipped their
|
|
52
|
+
* folder: on the author's machine, 804 transcripts and ~$1,354 in 30 days.
|
|
53
|
+
* `since`: only files written since then. `run`: the workflow run's id.
|
|
54
|
+
*/
|
|
55
|
+
export declare function subagentFiles(transcriptPath: string, since?: number): Array<{
|
|
56
|
+
file: string;
|
|
57
|
+
run?: string;
|
|
58
|
+
}>;
|
|
59
|
+
/** The workflow run a subagent transcript belongs to (its folder under subagents/workflows), or undefined. */
|
|
60
|
+
export declare function workflowRunOf(file: string): string | undefined;
|
|
61
|
+
/** A workflow run's own record, `<session>/workflows/<run>.json`: its name and default model, when Claude Code wrote one. */
|
|
62
|
+
export declare function workflowRunInfo(transcriptPath: string, run: string): {
|
|
63
|
+
name?: string;
|
|
64
|
+
model?: string;
|
|
65
|
+
status?: string;
|
|
66
|
+
};
|
|
43
67
|
/** Account for every subagent of a session, or null when there are none. */
|
|
44
68
|
export declare function subagentReport(transcriptPath: string): SubagentReport | null;
|
|
45
69
|
export interface SubagentRenderOptions {
|
package/dist/subagents.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* Reads only the lines that matter (usage, first user turn, timestamps) and
|
|
14
14
|
* never the whole content, so a session with 49 subagents stays fast.
|
|
15
15
|
*/
|
|
16
|
-
import {
|
|
16
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
17
17
|
import { basename, join } from "node:path";
|
|
18
18
|
import { formatTokens } from "./tokens.js";
|
|
19
19
|
import { formatUsd, pricingFor, usageCost } from "./pricing.js";
|
|
@@ -23,6 +23,73 @@ import { realModel } from "./tokens.js";
|
|
|
23
23
|
export function subagentDir(transcriptPath) {
|
|
24
24
|
return join(transcriptPath.replace(/\.jsonl$/, ""), "subagents");
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Every subagent transcript of a session: the Agent tool's
|
|
28
|
+
* (`subagents/agent-*.jsonl`) and the workflow agents'
|
|
29
|
+
* (`subagents/workflows/<run>/agent-*.jsonl`, one folder per workflow run,
|
|
30
|
+
* the run's own record in `<session>/workflows/<run>.json`). Workflow agents
|
|
31
|
+
* bill to the session like any subagent, and every reader skipped their
|
|
32
|
+
* folder: on the author's machine, 804 transcripts and ~$1,354 in 30 days.
|
|
33
|
+
* `since`: only files written since then. `run`: the workflow run's id.
|
|
34
|
+
*/
|
|
35
|
+
export function subagentFiles(transcriptPath, since = -Infinity) {
|
|
36
|
+
const dir = subagentDir(transcriptPath);
|
|
37
|
+
const out = [];
|
|
38
|
+
const add = (file, run) => {
|
|
39
|
+
try {
|
|
40
|
+
if (since === -Infinity || statSync(file).mtimeMs >= since)
|
|
41
|
+
out.push(run ? { file, run } : { file });
|
|
42
|
+
}
|
|
43
|
+
catch { /* gone */ }
|
|
44
|
+
};
|
|
45
|
+
let names;
|
|
46
|
+
try {
|
|
47
|
+
names = readdirSync(dir);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
for (const f of names)
|
|
53
|
+
if (f.endsWith(".jsonl"))
|
|
54
|
+
add(join(dir, f));
|
|
55
|
+
if (!names.includes("workflows"))
|
|
56
|
+
return out;
|
|
57
|
+
let runs;
|
|
58
|
+
try {
|
|
59
|
+
runs = readdirSync(join(dir, "workflows"));
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
for (const run of runs) {
|
|
65
|
+
let agents;
|
|
66
|
+
try {
|
|
67
|
+
agents = readdirSync(join(dir, "workflows", run));
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
for (const f of agents)
|
|
73
|
+
if (f.endsWith(".jsonl"))
|
|
74
|
+
add(join(dir, "workflows", run, f), run);
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
/** The workflow run a subagent transcript belongs to (its folder under subagents/workflows), or undefined. */
|
|
79
|
+
export function workflowRunOf(file) {
|
|
80
|
+
return /[\\/]subagents[\\/]workflows[\\/]([^\\/]+)[\\/][^\\/]+\.jsonl$/.exec(file)?.[1];
|
|
81
|
+
}
|
|
82
|
+
/** A workflow run's own record, `<session>/workflows/<run>.json`: its name and default model, when Claude Code wrote one. */
|
|
83
|
+
export function workflowRunInfo(transcriptPath, run) {
|
|
84
|
+
try {
|
|
85
|
+
const j = JSON.parse(readFileSync(join(transcriptPath.replace(/\.jsonl$/, ""), "workflows", `${run}.json`), "utf8"));
|
|
86
|
+
const s = (v) => (typeof v === "string" && v ? v.slice(0, 80) : undefined);
|
|
87
|
+
return { ...(s(j?.workflowName) ? { name: s(j.workflowName) } : {}), ...(s(j?.defaultModel) ? { model: s(j.defaultModel) } : {}), ...(s(j?.status) ? { status: s(j.status) } : {}) };
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return {};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
26
93
|
function n(v) {
|
|
27
94
|
return typeof v === "number" && Number.isFinite(v) && v >= 0 ? v : 0;
|
|
28
95
|
}
|
|
@@ -115,20 +182,13 @@ function summarize(path) {
|
|
|
115
182
|
}
|
|
116
183
|
/** Account for every subagent of a session, or null when there are none. */
|
|
117
184
|
export function subagentReport(transcriptPath) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
let files;
|
|
122
|
-
try {
|
|
123
|
-
files = readdirSync(dir).filter((f) => f.startsWith("agent-") && f.endsWith(".jsonl"));
|
|
124
|
-
}
|
|
125
|
-
catch {
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
if (files.length === 0)
|
|
185
|
+
// The Agent tool's subagents and the workflow agents alike: both bill to this session.
|
|
186
|
+
const found = subagentFiles(transcriptPath).filter((f) => basename(f.file).startsWith("agent-"));
|
|
187
|
+
if (found.length === 0)
|
|
129
188
|
return null;
|
|
130
|
-
const
|
|
131
|
-
|
|
189
|
+
const runOf = new Map(found.map((f) => [f.file, f.run]));
|
|
190
|
+
const summarized = found
|
|
191
|
+
.map((f) => f.file)
|
|
132
192
|
.filter((p) => {
|
|
133
193
|
try {
|
|
134
194
|
return statSync(p).size > 0;
|
|
@@ -137,17 +197,20 @@ export function subagentReport(transcriptPath) {
|
|
|
137
197
|
return false;
|
|
138
198
|
}
|
|
139
199
|
})
|
|
140
|
-
.map(summarize)
|
|
141
|
-
.filter((a) => a.calls > 0)
|
|
142
|
-
.sort((a, b) => (b.estCostUsd ?? 0) - (a.estCostUsd ?? 0) || b.inputBilledTokens - a.inputBilledTokens);
|
|
200
|
+
.map((p) => ({ s: summarize(p), run: runOf.get(p) }))
|
|
201
|
+
.filter((a) => a.s.calls > 0)
|
|
202
|
+
.sort((a, b) => (b.s.estCostUsd ?? 0) - (a.s.estCostUsd ?? 0) || b.s.inputBilledTokens - a.s.inputBilledTokens);
|
|
203
|
+
const agents = summarized.map((a) => a.s);
|
|
143
204
|
if (agents.length === 0)
|
|
144
205
|
return null;
|
|
206
|
+
const runs = new Set(summarized.flatMap((a) => (a.run ? [a.run] : [])));
|
|
145
207
|
return {
|
|
146
208
|
agents,
|
|
147
209
|
totalInputBilled: agents.reduce((t, a) => t + a.inputBilledTokens, 0),
|
|
148
210
|
totalOutput: agents.reduce((t, a) => t + a.outputTokens, 0),
|
|
149
211
|
totalCostUsd: agents.reduce((t, a) => t + (a.estCostUsd ?? 0), 0),
|
|
150
212
|
unpriced: agents.filter((a) => a.estCostUsd === undefined).length,
|
|
213
|
+
...(runs.size ? { workflowAgents: summarized.filter((a) => a.run).length, workflowRuns: runs.size } : {}),
|
|
151
214
|
};
|
|
152
215
|
}
|
|
153
216
|
function fmtDuration(ms) {
|
|
@@ -173,6 +236,8 @@ export function renderSubagents(report, parentInputUsd, top = 5, options = {}) {
|
|
|
173
236
|
const cost = report.unpriced === 0 ? formatUsd(report.totalCostUsd) : `${formatUsd(report.totalCostUsd)}+ (${report.unpriced} unpriced)`;
|
|
174
237
|
lines.push(`${report.agents.length} subagent(s) made ${report.agents.reduce((t, a) => t + a.calls, 0)} API calls: ` +
|
|
175
238
|
`${formatTokens(report.totalInputBilled)} input billed, ${formatTokens(report.totalOutput)} output, ~${cost}.`);
|
|
239
|
+
if (report.workflowAgents)
|
|
240
|
+
lines.push(`${report.workflowAgents} of them are workflow agents, in ${report.workflowRuns} workflow run${report.workflowRuns === 1 ? "" : "s"}.`);
|
|
176
241
|
if (parentInputUsd !== undefined && parentInputUsd > 0 && report.totalCostUsd > 0) {
|
|
177
242
|
const ratio = report.totalCostUsd / parentInputUsd;
|
|
178
243
|
lines.push(ratio >= 1
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Long tool output: what it costs to keep in the context, and what a lower
|
|
3
|
+
* cap would save. The `tool-output` lever of `fix`.
|
|
4
|
+
*
|
|
5
|
+
* A tool result stays in the conversation, and every later request re-reads
|
|
6
|
+
* it (at the cache-read rate, or the write rate when the cache went cold)
|
|
7
|
+
* until a compaction. Claude Code caps it when it arrives. Read from the
|
|
8
|
+
* 2.1.309 binary and its docs (code.claude.com/docs/en/tools-reference,
|
|
9
|
+
* settings-reference, mcp, env-vars):
|
|
10
|
+
* - a successful Bash or PowerShell result over `bashOutputMaxChars`
|
|
11
|
+
* (settings, any scope; default 30,000 characters, clamped to
|
|
12
|
+
* 4,000-128,000; Claude Code 2.1.261+) is saved to a file, and the model
|
|
13
|
+
* receives the path and a preview of the first 2,000 characters
|
|
14
|
+
* (`<persisted-output>`). Set, it also replaces BASH_MAX_OUTPUT_LENGTH,
|
|
15
|
+
* which on its own sizes only the read-back window, not what the model
|
|
16
|
+
* receives. A failed command gets a head-and-tail excerpt of ~10,000
|
|
17
|
+
* characters instead, with no file, whatever the setting;
|
|
18
|
+
* - an MCP text result over MAX_MCP_OUTPUT_TOKENS (environment; default
|
|
19
|
+
* 25,000 tokens) is saved to a file and replaced by a message naming the
|
|
20
|
+
* path, with no preview; one over 50,000 characters is saved to a file
|
|
21
|
+
* whatever the variable says (with the same 2,000-character preview as
|
|
22
|
+
* Bash), unless the tool raises its own limit.
|
|
23
|
+
* Anthropic can also set a tool's threshold server-side (a remote flag the
|
|
24
|
+
* binary reads per tool name); that is not visible here.
|
|
25
|
+
*
|
|
26
|
+
* The replay reads each transcript's requests and its Bash and MCP results
|
|
27
|
+
* in order. For a candidate cap, a successful text result longer than the
|
|
28
|
+
* cap would have been the preview instead: the tokens past it are priced at
|
|
29
|
+
* the first request after the result (the cache write, at the session's own
|
|
30
|
+
* write price) and at every later request until a compaction (a cache read,
|
|
31
|
+
* or a write when that request found the cache cold: over 60% of its context
|
|
32
|
+
* written again). Token counts are estimates from the text's length, at the
|
|
33
|
+
* ratio measured on the growth of the billed prompt between consecutive
|
|
34
|
+
* calls (tokens.ts). Not counted: the model opening the file afterwards, or
|
|
35
|
+
* running the command again. Each such request costs about the context it
|
|
36
|
+
* re-reads, so the replay prices one per moved result and reports how many
|
|
37
|
+
* of them the saving still covers.
|
|
38
|
+
*/
|
|
39
|
+
import { type Extractor } from "./scan.js";
|
|
40
|
+
/** The settings key the lever writes. */
|
|
41
|
+
export declare const BASH_OUTPUT_KEY = "bashOutputMaxChars";
|
|
42
|
+
/** The environment variable the setting replaces (a value the user set there is theirs). */
|
|
43
|
+
export declare const BASH_OUTPUT_ENV = "BASH_MAX_OUTPUT_LENGTH";
|
|
44
|
+
/** The first Claude Code that reads the setting (its docs; older versions ignore it). */
|
|
45
|
+
export declare const BASH_OUTPUT_SINCE = "2.1.261";
|
|
46
|
+
/** Claude Code's inline ceiling for a successful command, unset. */
|
|
47
|
+
export declare const BASH_OUTPUT_DEFAULT = 30000;
|
|
48
|
+
/** What Claude Code clamps the setting to. */
|
|
49
|
+
export declare const BASH_OUTPUT_MIN = 4000;
|
|
50
|
+
export declare const BASH_OUTPUT_MAX = 128000;
|
|
51
|
+
/** The caps the replay tries, smallest first; under 8,000 the 2,000-character preview is most of the output. */
|
|
52
|
+
export declare const BASH_OUTPUT_CAPS: number[];
|
|
53
|
+
/** The MCP cap the replay tries, in tokens: the size at which Claude Code already warns. */
|
|
54
|
+
export declare const MCP_OUTPUT_CAP = 10000;
|
|
55
|
+
export declare const TOOL_OUTPUT_ROWS: Extractor<{
|
|
56
|
+
ids: Record<string, number>;
|
|
57
|
+
calls: Record<string, string>;
|
|
58
|
+
}>;
|
|
59
|
+
/** A result: its length (or the size Claude Code saved), as code or prose, the model's tokenizer. */
|
|
60
|
+
interface Res {
|
|
61
|
+
k: "b" | "m";
|
|
62
|
+
n: number;
|
|
63
|
+
c: number;
|
|
64
|
+
p?: number;
|
|
65
|
+
model?: string;
|
|
66
|
+
}
|
|
67
|
+
/** Which results a cap moves to a file, and the tokens the model would no longer carry. */
|
|
68
|
+
type Mover = (r: Res) => number;
|
|
69
|
+
/** A candidate cap for Bash: a successful result longer than it, still inline, becomes the preview. */
|
|
70
|
+
export declare const bashCap: (cap: number) => Mover;
|
|
71
|
+
/** Since a cap was set: the results Claude Code moved that the cap before would have left inline. */
|
|
72
|
+
export declare const bashMovedSince: (previous: number) => Mover;
|
|
73
|
+
/** MAX_MCP_OUTPUT_TOKENS at `tokens`: a longer text result becomes a message naming the file. */
|
|
74
|
+
export declare const mcpCap: (tokens: number) => Mover;
|
|
75
|
+
export interface CapReplay {
|
|
76
|
+
/** Results the cap moves to a file, of the ones it applies to. */
|
|
77
|
+
moved: number;
|
|
78
|
+
results: number;
|
|
79
|
+
/** Tokens it keeps out of the context, summed over the requests that re-read them. */
|
|
80
|
+
tokens: number;
|
|
81
|
+
savedUsd: number;
|
|
82
|
+
/** One more request at each moved result's context: what opening the file (or running the command again) costs a time. */
|
|
83
|
+
openUsd: number;
|
|
84
|
+
}
|
|
85
|
+
export interface ToolOutputReplay {
|
|
86
|
+
/** Transcripts read (main conversations and subagents). */
|
|
87
|
+
transcripts: number;
|
|
88
|
+
requests: number;
|
|
89
|
+
/** Input billed over the same requests (prompt only), the share's base. */
|
|
90
|
+
billedUsd: number;
|
|
91
|
+
/** Successful Bash results Claude Code already saved to a file (over 30,000 characters or the cap set). */
|
|
92
|
+
alreadyMoved: number;
|
|
93
|
+
bash: Array<CapReplay & {
|
|
94
|
+
cap: number;
|
|
95
|
+
}>;
|
|
96
|
+
/** MAX_MCP_OUTPUT_TOKENS at MCP_OUTPUT_CAP: measured, not offered (the model gets no preview). */
|
|
97
|
+
mcp: CapReplay & {
|
|
98
|
+
cap: number;
|
|
99
|
+
};
|
|
100
|
+
/** The cap `fix` offers: the most saved among those that cover the model opening at least half the files. */
|
|
101
|
+
recommended?: number;
|
|
102
|
+
}
|
|
103
|
+
/** The share of moved results the model could open before the cap stops paying (each opening costs openUsd / moved and loses that result's saving). */
|
|
104
|
+
export declare function breakEven(r: CapReplay): number;
|
|
105
|
+
export interface ToolOutputOptions {
|
|
106
|
+
since: number;
|
|
107
|
+
until?: number;
|
|
108
|
+
/** Transcripts (default: Claude Code's main conversations and subagents written since `since`). */
|
|
109
|
+
paths?: string[];
|
|
110
|
+
caps?: number[];
|
|
111
|
+
}
|
|
112
|
+
/** Claude Code transcripts written since `since`, subagents included: every place a tool result is re-read. */
|
|
113
|
+
export declare function toolOutputTranscripts(since: number): string[];
|
|
114
|
+
/** Replay every Bash and MCP result in [since, until) at each candidate cap. */
|
|
115
|
+
export declare function replayToolOutput(opts: ToolOutputOptions): ToolOutputReplay;
|
|
116
|
+
/** What a cap set at `since` saved: the results it moved that the cap before (`previous`) left inline, re-read no more. */
|
|
117
|
+
export declare function toolOutputSavedSince(since: number, previous: number, paths?: string[]): CapReplay;
|
|
118
|
+
/** The lines under the lever in `fix`: each cap tried, and MCP's. */
|
|
119
|
+
export declare function renderToolOutputCaps(r: ToolOutputReplay): string[];
|
|
120
|
+
export {};
|