context-doctor 0.41.0 → 0.42.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 +5 -4
- package/dist/ab.js +3 -1
- package/dist/autopilot.d.ts +1 -1
- package/dist/autopilot.js +38 -24
- package/dist/budget.d.ts +12 -1
- package/dist/budget.js +153 -31
- package/dist/cachettl.js +12 -9
- package/dist/claudedir.d.ts +8 -0
- package/dist/claudedir.js +19 -0
- package/dist/cli.js +63 -9
- package/dist/codexdir.d.ts +11 -0
- package/dist/codexdir.js +20 -0
- package/dist/codexroute.d.ts +1 -0
- package/dist/codexroute.js +3 -1
- package/dist/compactwindow.js +12 -9
- package/dist/cursor.js +20 -5
- package/dist/doctor.d.ts +9 -0
- package/dist/doctor.js +32 -14
- package/dist/effective.d.ts +18 -0
- package/dist/effective.js +39 -4
- package/dist/extract.d.ts +12 -3
- package/dist/extract.js +39 -12
- package/dist/fileparams.d.ts +1 -1
- package/dist/fileparams.js +8 -8
- package/dist/fix.d.ts +3 -1
- package/dist/fix.js +31 -11
- package/dist/hook.js +15 -4
- package/dist/install.js +106 -47
- package/dist/ledger.d.ts +15 -0
- package/dist/ledger.js +30 -1
- package/dist/limits.d.ts +15 -1
- package/dist/limits.js +71 -28
- package/dist/mcp.js +58 -58
- package/dist/overhead.js +2 -1
- package/dist/pack.js +3 -2
- package/dist/proxy.d.ts +1 -1
- package/dist/proxy.js +1 -1
- package/dist/recall.js +3 -2
- package/dist/scan.d.ts +2 -0
- package/dist/scan.js +2 -1
- package/dist/session.js +2 -1
- package/dist/settingsedit.d.ts +4 -0
- package/dist/settingsedit.js +61 -3
- package/dist/spend.d.ts +8 -1
- package/dist/spend.js +27 -10
- package/dist/subagentmodel.d.ts +19 -4
- package/dist/subagentmodel.js +116 -28
- package/package.json +1 -1
package/dist/subagentmodel.js
CHANGED
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
* built-ins: Explore, Plan and fork say "inherit", claude-code-guide
|
|
12
12
|
* "haiku", statusline-setup "sonnet"; general-purpose and claude name
|
|
13
13
|
* none);
|
|
14
|
-
* 3. CLAUDE_CODE_SUBAGENT_MODEL, unless it is
|
|
14
|
+
* 3. CLAUDE_CODE_SUBAGENT_MODEL, unless it is empty or "inherit" (this
|
|
15
|
+
* function treats "default" as a model name; Claude Code's fallback
|
|
16
|
+
* chain treats it like "inherit");
|
|
15
17
|
* 4. the main conversation's model.
|
|
16
18
|
* So the variable moves only the agents whose definition names no model
|
|
17
19
|
* (general-purpose, claude, agents of your own without `model:`).
|
|
@@ -28,14 +30,15 @@
|
|
|
28
30
|
* tokenizes differently and may take more or fewer steps for the same task.
|
|
29
31
|
* Requests on a model no pricier than the candidate stay as billed.
|
|
30
32
|
*/
|
|
31
|
-
import { readdirSync, readFileSync } from "node:fs";
|
|
33
|
+
import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
32
34
|
import { homedir } from "node:os";
|
|
33
|
-
import {
|
|
35
|
+
import { dirname, join, resolve } from "node:path";
|
|
34
36
|
import { pick, rowsOf, slimUsage } from "./scan.js";
|
|
35
37
|
import { pricingFor, usageCost } from "./pricing.js";
|
|
36
38
|
import { realModel } from "./tokens.js";
|
|
37
39
|
import { claudeDir } from "./claudedir.js";
|
|
38
40
|
import { subagentTranscripts } from "./cachettl.js";
|
|
41
|
+
import { managedDir } from "./effective.js";
|
|
39
42
|
import { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME } from "./subagentpay.js";
|
|
40
43
|
export { SUBAGENT_CANDIDATE, SUBAGENT_CANDIDATE_NAME };
|
|
41
44
|
/** The environment variable the lever writes. */
|
|
@@ -100,19 +103,51 @@ export const SUBAGENT_ROWS = {
|
|
|
100
103
|
out.push({ t: "r", ...pick(e, ["uuid", "timestamp"]), id, model: m.model, usage: slimUsage(m.usage) });
|
|
101
104
|
},
|
|
102
105
|
};
|
|
103
|
-
/** `
|
|
104
|
-
function
|
|
105
|
-
let
|
|
106
|
+
/** The `.md` files under `dir`, subfolders included (Claude Code reads agent folders recursively). */
|
|
107
|
+
function markdownUnder(dir, out = [], seen = new Set()) {
|
|
108
|
+
let real;
|
|
109
|
+
try {
|
|
110
|
+
real = realpathSync(dir);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
if (seen.has(real))
|
|
116
|
+
return out;
|
|
117
|
+
seen.add(real);
|
|
118
|
+
let entries;
|
|
106
119
|
try {
|
|
107
|
-
|
|
120
|
+
entries = readdirSync(dir).sort();
|
|
108
121
|
}
|
|
109
122
|
catch {
|
|
110
|
-
return;
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
for (const e of entries) {
|
|
126
|
+
const p = join(dir, e);
|
|
127
|
+
let st;
|
|
128
|
+
try {
|
|
129
|
+
st = statSync(p);
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (st.isDirectory())
|
|
135
|
+
markdownUnder(p, out, seen);
|
|
136
|
+
else if (st.isFile() && e.endsWith(".md"))
|
|
137
|
+
out.push(p);
|
|
111
138
|
}
|
|
112
|
-
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* `name:` and `model:` from the frontmatter of the agent files under `dir`,
|
|
143
|
+
* over what `into` holds (a later folder wins). Like Claude Code, a file
|
|
144
|
+
* without a `name` or a `description` is not an agent.
|
|
145
|
+
*/
|
|
146
|
+
function definitionsIn(dir, into) {
|
|
147
|
+
for (const path of markdownUnder(dir)) {
|
|
113
148
|
let text;
|
|
114
149
|
try {
|
|
115
|
-
text = readFileSync(
|
|
150
|
+
text = readFileSync(path, "utf8");
|
|
116
151
|
}
|
|
117
152
|
catch {
|
|
118
153
|
continue;
|
|
@@ -121,10 +156,49 @@ function definitionsIn(dir, into) {
|
|
|
121
156
|
if (fm === undefined)
|
|
122
157
|
continue;
|
|
123
158
|
const field = (k) => new RegExp(`^${k}:[ \\t]*(.*)$`, "m").exec(fm)?.[1].trim().replace(/^["']|["']$/g, "") || undefined;
|
|
124
|
-
const name = field("name")
|
|
125
|
-
if (!
|
|
126
|
-
|
|
159
|
+
const name = field("name");
|
|
160
|
+
if (!name || !field("description"))
|
|
161
|
+
continue;
|
|
162
|
+
into.set(name, { model: field("model"), where: path });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The project folders whose `.claude/agents` apply in `cwd`, outermost
|
|
167
|
+
* first: `cwd` and its parents up to the git root, never the home folder
|
|
168
|
+
* or above it (the 2.1.309 binary: closer folders win over outer ones).
|
|
169
|
+
*/
|
|
170
|
+
function projectAgentDirs(cwd, home) {
|
|
171
|
+
const dirs = [];
|
|
172
|
+
let d = resolve(cwd);
|
|
173
|
+
const h = resolve(home);
|
|
174
|
+
for (;;) {
|
|
175
|
+
if (d === h)
|
|
176
|
+
break;
|
|
177
|
+
dirs.push(join(d, ".claude", "agents"));
|
|
178
|
+
if (existsSync(join(d, ".git")))
|
|
179
|
+
break;
|
|
180
|
+
const up = dirname(d);
|
|
181
|
+
if (up === d)
|
|
182
|
+
break;
|
|
183
|
+
d = up;
|
|
127
184
|
}
|
|
185
|
+
return dirs.reverse();
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The agent definitions Claude Code uses in `cwd`, as 2.1.309 merges them,
|
|
189
|
+
* lowest first: the user's (~/.claude/agents), the project's (outer folders
|
|
190
|
+
* first), then managed. Plugin agents are not read.
|
|
191
|
+
*/
|
|
192
|
+
export function agentDefinitions(cwd, home = homedir(), env = process.env) {
|
|
193
|
+
const defs = new Map();
|
|
194
|
+
definitionsIn(join(claudeDir(home, env), "agents"), defs);
|
|
195
|
+
if (cwd)
|
|
196
|
+
for (const d of projectAgentDirs(cwd, home))
|
|
197
|
+
definitionsIn(d, defs);
|
|
198
|
+
const managed = managedDir(env);
|
|
199
|
+
if (managed)
|
|
200
|
+
definitionsIn(join(managed, ".claude", "agents"), defs);
|
|
201
|
+
return defs;
|
|
128
202
|
}
|
|
129
203
|
/** How a type's model is chosen: whether CLAUDE_CODE_SUBAGENT_MODEL moves it, and why not. */
|
|
130
204
|
export function variableMoves(type, defs, force = false) {
|
|
@@ -138,7 +212,7 @@ export function variableMoves(type, defs, force = false) {
|
|
|
138
212
|
}
|
|
139
213
|
const d = defs.get(type);
|
|
140
214
|
if (!d)
|
|
141
|
-
return { moves: false, why: "its agent file was not found here" };
|
|
215
|
+
return { moves: false, why: type === "unknown" ? "no agent type recorded beside it (an older Claude Code)" : "its agent file was not found here" };
|
|
142
216
|
if (!d.model)
|
|
143
217
|
return { moves: true };
|
|
144
218
|
return { moves: false, why: `its file says model: ${d.model}` };
|
|
@@ -180,12 +254,23 @@ export function replaySubagentModels(opts) {
|
|
|
180
254
|
const haiku = pricingFor(HAIKU);
|
|
181
255
|
const base = opts.baseline ? blend(opts.baseline) : undefined;
|
|
182
256
|
const until = opts.until ?? Infinity;
|
|
183
|
-
const
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
257
|
+
const home = opts.home ?? homedir();
|
|
258
|
+
const paths = opts.paths ?? subagentTranscripts(opts.since, home);
|
|
259
|
+
// Each project's agents, read the first time a subagent of that folder is met.
|
|
260
|
+
const defsByCwd = new Map();
|
|
261
|
+
const defsFor = (cwd) => {
|
|
262
|
+
if (opts.definitions)
|
|
263
|
+
return opts.definitions;
|
|
264
|
+
const key = typeof cwd === "string" ? cwd : "";
|
|
265
|
+
let d = defsByCwd.get(key);
|
|
266
|
+
if (!d)
|
|
267
|
+
defsByCwd.set(key, (d = agentDefinitions(key || undefined, home)));
|
|
268
|
+
return d;
|
|
269
|
+
};
|
|
187
270
|
const types = new Map();
|
|
188
271
|
const movedBy = new Map();
|
|
272
|
+
const beforeBy = new Map();
|
|
273
|
+
const add = (m, k, v) => m.set(k, (m.get(k) ?? 0) + v);
|
|
189
274
|
const seen = new Set();
|
|
190
275
|
let unpriced = 0;
|
|
191
276
|
for (const p of paths) {
|
|
@@ -196,13 +281,12 @@ export function replaySubagentModels(opts) {
|
|
|
196
281
|
catch {
|
|
197
282
|
continue;
|
|
198
283
|
}
|
|
199
|
-
const cwd = rows.find((r) => r?.t === "c")?.cwd;
|
|
200
|
-
// A project's own agents, looked up the first time a subagent of that project is met.
|
|
201
|
-
if (typeof cwd === "string" && !opts.definitions)
|
|
202
|
-
definitionsIn(join(cwd, ".claude", "agents"), defs);
|
|
203
284
|
const type = agentTypeOf(p);
|
|
204
|
-
const how = variableMoves(type,
|
|
285
|
+
const how = variableMoves(type, defsFor(rows.find((r) => r?.t === "c")?.cwd), opts.force);
|
|
205
286
|
const t = types.get(type) ?? types.set(type, { type, agents: 0, requests: 0, billedUsd: 0, atCandidateUsd: 0, atHaikuUsd: 0, over100k: 0, moves: how.moves, why: how.why, readOnly: READ_ONLY.has(type) }).get(type);
|
|
287
|
+
// One project's file may name a model where another's does not: the type moves where any of its runs does.
|
|
288
|
+
if (how.moves)
|
|
289
|
+
t.moves = true;
|
|
206
290
|
// Per reply of this file: the prices its output top-ups are charged at.
|
|
207
291
|
const priced = new Map();
|
|
208
292
|
let counted = false;
|
|
@@ -214,6 +298,8 @@ export function replaySubagentModels(opts) {
|
|
|
214
298
|
t.billedUsd += (r.more * q.billed.outputPerM) / 1e6;
|
|
215
299
|
t.atCandidateUsd += (r.more * q.other.outputPerM) / 1e6;
|
|
216
300
|
t.atHaikuUsd += (r.more * haiku.outputPerM) / 1e6;
|
|
301
|
+
if (how.moves)
|
|
302
|
+
add(beforeBy, q.model, (r.more * cand.outputPerM) / 1e6);
|
|
217
303
|
continue;
|
|
218
304
|
}
|
|
219
305
|
if (r?.t !== "r")
|
|
@@ -248,17 +334,19 @@ export function replaySubagentModels(opts) {
|
|
|
248
334
|
if (promptTokens(u) > HAIKU_TIER)
|
|
249
335
|
t.over100k++;
|
|
250
336
|
if (move)
|
|
251
|
-
movedBy
|
|
337
|
+
add(movedBy, model, billed);
|
|
338
|
+
if (how.moves)
|
|
339
|
+
add(beforeBy, model, usageCost(u, cand).totalUsd);
|
|
252
340
|
if (r.id)
|
|
253
|
-
priced.set(r.id, { billed: price, other });
|
|
341
|
+
priced.set(r.id, { billed: price, other, model });
|
|
254
342
|
}
|
|
255
343
|
if (counted)
|
|
256
344
|
t.agents++;
|
|
257
345
|
}
|
|
258
346
|
const byType = [...types.values()].filter((t) => t.requests > 0).sort((a, b) => b.billedUsd - a.billedUsd);
|
|
259
347
|
const sum = (k) => byType.reduce((s, t) => s + t[k], 0);
|
|
260
|
-
const
|
|
261
|
-
return { candidate, agents: sum("agents"), requests: sum("requests"), billedUsd: sum("billedUsd"), atCandidateUsd: sum("atCandidateUsd"), savedUsd: sum("billedUsd") - sum("atCandidateUsd"), movedFrom, unpriced, byType };
|
|
348
|
+
const sorted = (m) => Object.fromEntries([...m.entries()].sort((a, b) => b[1] - a[1]));
|
|
349
|
+
return { candidate, agents: sum("agents"), requests: sum("requests"), billedUsd: sum("billedUsd"), atCandidateUsd: sum("atCandidateUsd"), savedUsd: sum("billedUsd") - sum("atCandidateUsd"), movedFrom: sorted(movedBy), before: sorted(beforeBy), unpriced, byType };
|
|
262
350
|
}
|
|
263
351
|
/** The models the moved requests ran on, for people: the one or two that carry 10%+ of the bill ("Fable 5.1 or Opus 5.5"). */
|
|
264
352
|
export function movedFromNames(movedFrom) {
|
|
@@ -267,7 +355,7 @@ export function movedFromNames(movedFrom) {
|
|
|
267
355
|
const names = [...new Set(all.filter(([, v]) => v >= 0.1 * total).slice(0, 2).map(([m]) => modelName(m)))];
|
|
268
356
|
return names.length ? names.join(" or ") : undefined;
|
|
269
357
|
}
|
|
270
|
-
/** Each model's share of a
|
|
358
|
+
/** Each model's share of a total by model (a fix record keeps the shares of `before` as the baseline). */
|
|
271
359
|
export function shares(byModel) {
|
|
272
360
|
const total = Object.values(byModel).reduce((s, v) => s + v, 0);
|
|
273
361
|
return total > 0 ? Object.fromEntries(Object.entries(byModel).map(([m, v]) => [m, v / total])) : {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "context-doctor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.42.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",
|