context-doctor 0.43.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.
@@ -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 { existsSync, readdirSync, statSync } from "node:fs";
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
- const dir = subagentDir(transcriptPath);
119
- if (!existsSync(dir))
120
- return null;
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 agents = files
131
- .map((f) => join(dir, f))
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
@@ -41,6 +41,8 @@ import { type Extractor } from "./scan.js";
41
41
  export declare const BASH_OUTPUT_KEY = "bashOutputMaxChars";
42
42
  /** The environment variable the setting replaces (a value the user set there is theirs). */
43
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";
44
46
  /** Claude Code's inline ceiling for a successful command, unset. */
45
47
  export declare const BASH_OUTPUT_DEFAULT = 30000;
46
48
  /** What Claude Code clamps the setting to. */
@@ -45,6 +45,8 @@ import { subagentTranscripts } from "./cachettl.js";
45
45
  export const BASH_OUTPUT_KEY = "bashOutputMaxChars";
46
46
  /** The environment variable the setting replaces (a value the user set there is theirs). */
47
47
  export const BASH_OUTPUT_ENV = "BASH_MAX_OUTPUT_LENGTH";
48
+ /** The first Claude Code that reads the setting (its docs; older versions ignore it). */
49
+ export const BASH_OUTPUT_SINCE = "2.1.261";
48
50
  /** Claude Code's inline ceiling for a successful command, unset. */
49
51
  export const BASH_OUTPUT_DEFAULT = 30_000;
50
52
  /** What Claude Code clamps the setting to. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-doctor",
3
- "version": "0.43.0",
3
+ "version": "0.44.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",