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.
- package/README.md +3 -2
- package/dist/budget.d.ts +13 -3
- package/dist/budget.js +158 -40
- package/dist/cachettl.d.ts +5 -1
- package/dist/cachettl.js +10 -25
- package/dist/cli.js +6 -9
- package/dist/codexcompact.d.ts +24 -3
- package/dist/codexcompact.js +207 -41
- 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 +45 -0
- package/dist/fanout.js +4 -2
- package/dist/fix.d.ts +8 -1
- package/dist/fix.js +59 -13
- package/dist/hook.js +45 -15
- package/dist/install.js +56 -19
- 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 +7 -1
- package/dist/sessionprice.js +79 -28
- 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 +2 -0
- package/dist/tooloutput.js +2 -0
- package/package.json +1 -1
package/dist/codexcompact.js
CHANGED
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
* Codex does not record it). What the summary dropped, and the model reading
|
|
30
30
|
* files again because of it, is not priced.
|
|
31
31
|
*/
|
|
32
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
32
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
33
33
|
import { homedir } from "node:os";
|
|
34
|
-
import { basename, join } from "node:path";
|
|
34
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
35
35
|
import { codexDir } from "./codexdir.js";
|
|
36
36
|
import { codexEventKey, codexSessionsRoot, rolloutFiles } from "./limits.js";
|
|
37
37
|
import { openAIUsage, pricingFor, usageCost } from "./pricing.js";
|
|
@@ -149,21 +149,27 @@ function readSteps(file, seen) {
|
|
|
149
149
|
* One rollout at `limit`: as billed and with compaction at the limit, in USD
|
|
150
150
|
* over the requests in [since, until) that are not a fork's copy. Codex
|
|
151
151
|
* compacts when a turn's input and output reach the limit, which is never
|
|
152
|
-
* above its own trigger
|
|
152
|
+
* above its own trigger; with the `body_after_prefix` scope, when what the
|
|
153
|
+
* turn holds beyond the first request of its window (the session's start or
|
|
154
|
+
* the last compaction) reaches it (codex-rs session/context_window.rs).
|
|
153
155
|
*/
|
|
154
|
-
function replay(steps, limit, since, until, after) {
|
|
156
|
+
function replay(steps, limit, since, until, after, scope = "total") {
|
|
155
157
|
let billed = 0, sim = 0, extra = 0, actual = 0;
|
|
156
|
-
let removed = 0, fresh = false,
|
|
158
|
+
let removed = 0, fresh = false, prevCounted = false;
|
|
159
|
+
// The input of the window's first request: what body_after_prefix does not count.
|
|
160
|
+
let prefill;
|
|
157
161
|
for (const s of steps) {
|
|
158
162
|
if (s.k === "c") {
|
|
159
|
-
|
|
163
|
+
// A compaction after a fork's copy of its parent's requests is the parent's, counted there.
|
|
164
|
+
if (prevCounted)
|
|
160
165
|
actual++;
|
|
161
166
|
removed = 0;
|
|
162
167
|
fresh = false;
|
|
168
|
+
prefill = undefined;
|
|
163
169
|
continue;
|
|
164
170
|
}
|
|
165
171
|
const counted = s.at >= since && s.at < until && !s.copy;
|
|
166
|
-
|
|
172
|
+
prevCounted = counted;
|
|
167
173
|
let ctx = s.prompt - removed;
|
|
168
174
|
// The real context fell below what was removed (a rewind with no compaction recorded): back in step.
|
|
169
175
|
if (removed > 0 && ctx < after) {
|
|
@@ -179,11 +185,15 @@ function replay(steps, limit, since, until, after) {
|
|
|
179
185
|
cost = Math.max(0, s.billedUsd - (fromCache * s.readPerM + (removed - fromCache) * s.inPerM) / 1e6);
|
|
180
186
|
}
|
|
181
187
|
fresh = false;
|
|
188
|
+
prefill ??= ctx;
|
|
182
189
|
if (counted) {
|
|
183
190
|
billed += s.billedUsd;
|
|
184
191
|
sim += cost;
|
|
185
192
|
}
|
|
186
|
-
|
|
193
|
+
const reached = scope === "body_after_prefix"
|
|
194
|
+
? ctx + s.out - prefill >= limit && ctx + s.out < s.trigger
|
|
195
|
+
: ctx + s.out >= Math.min(limit, s.trigger) && limit < s.trigger;
|
|
196
|
+
if (reached) {
|
|
187
197
|
// Compacted after this turn: one request reading the context, a summary written; the next starts from `after`.
|
|
188
198
|
if (counted) {
|
|
189
199
|
sim += (ctx * s.readPerM + SUMMARY_OUTPUT * s.outPerM) / 1e6;
|
|
@@ -191,10 +201,12 @@ function replay(steps, limit, since, until, after) {
|
|
|
191
201
|
}
|
|
192
202
|
removed = s.prompt + s.out - after;
|
|
193
203
|
fresh = true;
|
|
204
|
+
prefill = undefined;
|
|
194
205
|
}
|
|
195
206
|
}
|
|
196
207
|
return { billed, sim, extra, actual };
|
|
197
208
|
}
|
|
209
|
+
export const CODEX_COMPACT_SCOPE_KEY = "model_auto_compact_token_limit_scope";
|
|
198
210
|
/** Codex rollouts written since `since`: the live ones and the archived. */
|
|
199
211
|
export function codexRollouts(since, home = homedir()) {
|
|
200
212
|
const roots = [codexSessionsRoot(home), join(codexDir(home), "archived_sessions")];
|
|
@@ -204,6 +216,7 @@ export function codexRollouts(since, home = homedir()) {
|
|
|
204
216
|
/** Replay Codex's rollouts of the last `days` at each limit. */
|
|
205
217
|
export function replayCodexCompact(opts) {
|
|
206
218
|
const now = opts.now ?? Date.now();
|
|
219
|
+
const scope = opts.scope ?? codexCompactScope(opts.home);
|
|
207
220
|
const since = now - opts.days * 86_400_000;
|
|
208
221
|
const paths = opts.paths ?? codexRollouts(since, opts.home);
|
|
209
222
|
const seen = new Set();
|
|
@@ -221,7 +234,7 @@ export function replayCodexCompact(opts) {
|
|
|
221
234
|
steps.forEach((s, i) => {
|
|
222
235
|
if (s.k === "c") {
|
|
223
236
|
const n = steps[i + 1];
|
|
224
|
-
if (n?.k === "r")
|
|
237
|
+
if (n?.k === "r" && !n.copy)
|
|
225
238
|
afters.push(n.prompt);
|
|
226
239
|
return;
|
|
227
240
|
}
|
|
@@ -238,7 +251,7 @@ export function replayCodexCompact(opts) {
|
|
|
238
251
|
const estimates = (opts.limits ?? CODEX_COMPACT_LIMITS).map((limit, i) => {
|
|
239
252
|
let billed = 0, sim = 0, extra = 0;
|
|
240
253
|
for (const steps of all) {
|
|
241
|
-
const r = replay(steps, limit, since, Infinity, after);
|
|
254
|
+
const r = replay(steps, limit, since, Infinity, after, scope);
|
|
242
255
|
billed += r.billed;
|
|
243
256
|
sim += r.sim;
|
|
244
257
|
extra += r.extra;
|
|
@@ -249,7 +262,7 @@ export function replayCodexCompact(opts) {
|
|
|
249
262
|
billedUsd = billed;
|
|
250
263
|
return { limit, savedUsd: billed - sim, savedShare: billed > 0 ? (billed - sim) / billed : 0, compactions: extra };
|
|
251
264
|
});
|
|
252
|
-
return { days: opts.days, rollouts: paths.length, requests, billedUsd, unpriced, actualCompactions: actual, afterCompact: after, estimates };
|
|
265
|
+
return { days: opts.days, scope, rollouts: paths.length, requests, billedUsd, unpriced, actualCompactions: actual, afterCompact: after, estimates };
|
|
253
266
|
}
|
|
254
267
|
/**
|
|
255
268
|
* What a limit set at `since` saved: each compaction since that came before
|
|
@@ -305,13 +318,76 @@ const HEADER = /^\s*\[/;
|
|
|
305
318
|
const keyLine = (key) => new RegExp(String.raw `^\s*(?:${key}|"${key}"|'${key}')\s*=\s*([^#\r]*?)\s*(?:#[^\r]*)?\r?$`);
|
|
306
319
|
/** A TOML integer as written (`150000`, `150_000`, `+150000`), or undefined. */
|
|
307
320
|
const tomlInt = (raw) => (/^[+-]?\d[\d_]*$/.test(raw.trim()) ? Number(raw.trim().replace(/_/g, "")) : undefined);
|
|
321
|
+
const BOM = "";
|
|
322
|
+
/**
|
|
323
|
+
* For each line, whether it starts a statement (a key, a [table], a comment
|
|
324
|
+
* or a blank) rather than continuing a value that spans lines: a multi-line
|
|
325
|
+
* string ("""…""", '''…''') or an array or inline table left open. A
|
|
326
|
+
* `[x]` or `key = 1` inside one of those is the value's text, not a table or
|
|
327
|
+
* a key, and a line put there would land inside the user's value.
|
|
328
|
+
*/
|
|
329
|
+
function statementStarts(lines) {
|
|
330
|
+
const out = [];
|
|
331
|
+
let depth = 0;
|
|
332
|
+
let ml;
|
|
333
|
+
for (const line of lines) {
|
|
334
|
+
out.push(depth === 0 && ml === undefined);
|
|
335
|
+
let i = 0;
|
|
336
|
+
while (i < line.length) {
|
|
337
|
+
const c = line[i];
|
|
338
|
+
if (ml !== undefined) {
|
|
339
|
+
if (ml === '"""' && c === "\\") {
|
|
340
|
+
i += 2;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (line.startsWith(ml, i)) {
|
|
344
|
+
// Up to two quotes more belong to the string: `""""` ends it with one.
|
|
345
|
+
let j = i + 3;
|
|
346
|
+
while (j < line.length && j < i + 5 && line[j] === ml[0])
|
|
347
|
+
j++;
|
|
348
|
+
ml = undefined;
|
|
349
|
+
i = j;
|
|
350
|
+
}
|
|
351
|
+
else
|
|
352
|
+
i++;
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
if (c === "#")
|
|
356
|
+
break;
|
|
357
|
+
if (c === '"' || c === "'") {
|
|
358
|
+
if (line.startsWith(c.repeat(3), i)) {
|
|
359
|
+
ml = c.repeat(3);
|
|
360
|
+
i += 3;
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
let j = i + 1;
|
|
364
|
+
while (j < line.length && line[j] !== c)
|
|
365
|
+
j += c === '"' && line[j] === "\\" ? 2 : 1;
|
|
366
|
+
i = j + 1;
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
if (c === "[" || c === "{")
|
|
370
|
+
depth++;
|
|
371
|
+
else if ((c === "]" || c === "}") && depth > 0)
|
|
372
|
+
depth--;
|
|
373
|
+
i++;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return out;
|
|
377
|
+
}
|
|
378
|
+
/** The lines, which of them start a statement, and the first [table] (lines.length when none). */
|
|
379
|
+
function tomlLayout(toml) {
|
|
380
|
+
const lines = toml.split("\n");
|
|
381
|
+
const starts = statementStarts(lines);
|
|
382
|
+
const header = lines.findIndex((l, i) => starts[i] && HEADER.test(l));
|
|
383
|
+
return { lines, starts, header: header === -1 ? lines.length : header };
|
|
384
|
+
}
|
|
308
385
|
/** The top-level value of `key` (before the first [table]): as written, and as a number when it is one. */
|
|
309
386
|
export function topLevelValue(toml, key = CODEX_COMPACT_KEY) {
|
|
310
387
|
const re = keyLine(key);
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
const m = re.exec(line);
|
|
388
|
+
const { lines, starts, header } = tomlLayout(toml.startsWith(BOM) ? toml.slice(1) : toml);
|
|
389
|
+
for (let i = 0; i < header; i++) {
|
|
390
|
+
const m = starts[i] ? re.exec(lines[i]) : null;
|
|
315
391
|
if (m)
|
|
316
392
|
return { raw: m[1], value: tomlInt(m[1]) };
|
|
317
393
|
}
|
|
@@ -320,50 +396,85 @@ export function topLevelValue(toml, key = CODEX_COMPACT_KEY) {
|
|
|
320
396
|
/**
|
|
321
397
|
* Set (or with undefined, remove) a top-level integer key, touching no other
|
|
322
398
|
* byte: a line replaced keeps its line end; a new line goes after the last
|
|
323
|
-
* top-level
|
|
324
|
-
* file
|
|
399
|
+
* top-level statement (or at the top, after a byte order mark), with the
|
|
400
|
+
* file's own line ends, never inside a multi-line string or array. A
|
|
401
|
+
* missing file (undefined) becomes the one line.
|
|
325
402
|
*/
|
|
326
403
|
export function setTopLevelInt(toml, key, value) {
|
|
327
404
|
if (toml === undefined || toml === "")
|
|
328
405
|
return value === undefined ? "" : `${key} = ${value}\n`;
|
|
329
|
-
const
|
|
406
|
+
const bom = toml.startsWith(BOM) ? BOM : "";
|
|
407
|
+
const { lines, starts, header: end } = tomlLayout(toml.slice(bom.length));
|
|
330
408
|
const cr = toml.includes("\r\n") ? "\r" : "";
|
|
331
409
|
const re = keyLine(key);
|
|
332
|
-
const
|
|
333
|
-
const end = header === -1 ? lines.length : header;
|
|
334
|
-
const at = lines.slice(0, end).findIndex((l) => re.test(l));
|
|
410
|
+
const at = lines.slice(0, end).findIndex((l, i) => starts[i] && re.test(l));
|
|
335
411
|
if (at >= 0) {
|
|
336
412
|
if (value === undefined)
|
|
337
413
|
lines.splice(at, 1);
|
|
338
414
|
else
|
|
339
415
|
lines[at] = `${key} = ${value}${lines[at].endsWith("\r") ? "\r" : ""}`;
|
|
340
|
-
return lines.join("\n");
|
|
416
|
+
return bom + lines.join("\n");
|
|
341
417
|
}
|
|
342
418
|
if (value === undefined)
|
|
343
419
|
return toml;
|
|
344
|
-
// After the last top-level
|
|
420
|
+
// After the last top-level statement (all of its lines), so a comment above a [table] stays with it.
|
|
345
421
|
let pos = 0;
|
|
346
|
-
for (let i = 0; i < end; i++)
|
|
347
|
-
if (
|
|
348
|
-
|
|
422
|
+
for (let i = 0; i < end; i++) {
|
|
423
|
+
if (!starts[i] || !/^\s*[^\s#[]/.test(lines[i]))
|
|
424
|
+
continue;
|
|
425
|
+
pos = i + 1;
|
|
426
|
+
while (pos < end && !starts[pos])
|
|
427
|
+
pos++;
|
|
428
|
+
}
|
|
349
429
|
lines.splice(pos, 0, `${key} = ${value}${cr}`);
|
|
350
|
-
return lines.join("\n");
|
|
430
|
+
return bom + lines.join("\n");
|
|
351
431
|
}
|
|
352
|
-
/**
|
|
432
|
+
/** A profile name as a TOML key part: bare, "quoted" or 'quoted'. */
|
|
433
|
+
const PROFILE = String.raw `(?:"([^"]+)"|'([^']+)'|([\w-]+))`;
|
|
434
|
+
/**
|
|
435
|
+
* Profiles that set `key`, and the profile the file selects by default:
|
|
436
|
+
* `[profiles.<name>]` tables, a `[profiles]` table's `<name>.key`, and a
|
|
437
|
+
* top-level `profiles.<name>.key`.
|
|
438
|
+
*/
|
|
353
439
|
export function profileValues(toml, key = CODEX_COMPACT_KEY) {
|
|
354
440
|
const re = keyLine(key);
|
|
355
|
-
const
|
|
441
|
+
const { lines, starts, header } = tomlLayout(toml.startsWith(BOM) ? toml.slice(1) : toml);
|
|
442
|
+
const name = (m) => m?.slice(1, 4).find((x) => x !== undefined);
|
|
443
|
+
let selected;
|
|
444
|
+
for (let i = 0; i < header && selected === undefined; i++)
|
|
445
|
+
if (starts[i])
|
|
446
|
+
selected = /^\s*profile\s*=\s*["']([^"']+)["']/.exec(lines[i])?.[1];
|
|
447
|
+
const dotted = (prefix) => new RegExp(String.raw `^\s*${prefix}${PROFILE}\s*\.\s*(?:${key}|"${key}"|'${key}')\s*=\s*([^#\r]*?)\s*(?:#[^\r]*)?\r?$`);
|
|
448
|
+
const top = dotted(String.raw `profiles\s*\.\s*`);
|
|
449
|
+
const inProfiles = dotted("");
|
|
356
450
|
const out = [];
|
|
451
|
+
// undefined: the top level; "": the [profiles] table; a name: [profiles.<name>]; null: any other table.
|
|
357
452
|
let table;
|
|
358
|
-
|
|
453
|
+
lines.forEach((line, i) => {
|
|
454
|
+
if (!starts[i])
|
|
455
|
+
return;
|
|
359
456
|
if (HEADER.test(line)) {
|
|
360
|
-
table = /^\s*\[\s*profiles
|
|
361
|
-
|
|
457
|
+
table = /^\s*\[\s*profiles\s*\]/.test(line) ? "" : name(new RegExp(String.raw `^\s*\[\s*profiles\s*\.\s*${PROFILE}\s*\]`).exec(line)) ?? null;
|
|
458
|
+
return;
|
|
362
459
|
}
|
|
363
|
-
|
|
364
|
-
if (
|
|
365
|
-
|
|
366
|
-
|
|
460
|
+
let profile, raw;
|
|
461
|
+
if (table) {
|
|
462
|
+
const m = re.exec(line);
|
|
463
|
+
if (m) {
|
|
464
|
+
profile = table;
|
|
465
|
+
raw = m[1];
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
else if (table === "" || table === undefined) {
|
|
469
|
+
const m = (table === "" ? inProfiles : top).exec(line);
|
|
470
|
+
if (m) {
|
|
471
|
+
profile = name(m);
|
|
472
|
+
raw = m[4];
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (profile !== undefined && raw !== undefined)
|
|
476
|
+
out.push({ profile, raw, selected: profile === selected });
|
|
477
|
+
});
|
|
367
478
|
return out;
|
|
368
479
|
}
|
|
369
480
|
/** ~/.codex/config.toml (CODEX_HOME honoured for this user). */
|
|
@@ -383,26 +494,81 @@ export function currentCodexCompact(home) {
|
|
|
383
494
|
const v = topLevelValue(text);
|
|
384
495
|
return v ? { ...v, where: tilde(p) } : undefined;
|
|
385
496
|
}
|
|
497
|
+
/** model_auto_compact_token_limit_scope at the top of config.toml: "body_after_prefix", else Codex's default, "total". */
|
|
498
|
+
export function codexCompactScope(home) {
|
|
499
|
+
let text;
|
|
500
|
+
try {
|
|
501
|
+
text = readFileSync(codexConfigPath(home), "utf8");
|
|
502
|
+
}
|
|
503
|
+
catch {
|
|
504
|
+
return "total";
|
|
505
|
+
}
|
|
506
|
+
const raw = topLevelValue(text, CODEX_COMPACT_SCOPE_KEY)?.raw.trim().replace(/^["']|["']$/g, "");
|
|
507
|
+
return raw === "body_after_prefix" ? "body_after_prefix" : "total";
|
|
508
|
+
}
|
|
386
509
|
/** What would keep a top-level limit from applying: a profile's own, a project's config.toml. */
|
|
387
510
|
export function codexCompactOverrides(home, cwd = process.cwd()) {
|
|
388
511
|
const out = [];
|
|
389
512
|
const p = codexConfigPath(home);
|
|
513
|
+
let user;
|
|
390
514
|
try {
|
|
391
|
-
|
|
392
|
-
out.push(`${CODEX_COMPACT_KEY} = ${v.raw} in [profiles.${v.profile}] of ${tilde(p)} (${v.selected ? "the profile it selects" : `when Codex runs with --profile ${v.profile}`})`);
|
|
515
|
+
user = readFileSync(p, "utf8");
|
|
393
516
|
}
|
|
394
517
|
catch { /* none */ }
|
|
395
|
-
|
|
396
|
-
|
|
518
|
+
if (user !== undefined) {
|
|
519
|
+
for (const v of profileValues(user))
|
|
520
|
+
out.push(`${CODEX_COMPACT_KEY} = ${v.raw} in [profiles.${v.profile}] of ${tilde(p)} (${v.selected ? "the profile it selects" : `when Codex runs with --profile ${v.profile}`})`);
|
|
521
|
+
}
|
|
522
|
+
// Closest folder first: the one whose value Codex applies last.
|
|
523
|
+
for (const dir of [...projectConfigFolders(cwd, rootMarkers(user))].reverse()) {
|
|
524
|
+
const project = join(dir, ".codex", "config.toml");
|
|
525
|
+
if (resolve(project) === resolve(p))
|
|
526
|
+
continue;
|
|
397
527
|
try {
|
|
398
528
|
const v = topLevelValue(readFileSync(project, "utf8"));
|
|
399
529
|
if (v)
|
|
400
|
-
out.push(`${CODEX_COMPACT_KEY} = ${v.raw} in ${tilde(project)} (this project's)`);
|
|
530
|
+
out.push(`${CODEX_COMPACT_KEY} = ${v.raw} in ${tilde(project)} (this project's, when Codex trusts it)`);
|
|
401
531
|
}
|
|
402
532
|
catch { /* none */ }
|
|
403
533
|
}
|
|
404
534
|
return out;
|
|
405
535
|
}
|
|
536
|
+
/** `project_root_markers` at the top of the user's config.toml (strings of a one-line array), else Codex's default, [".git"]. */
|
|
537
|
+
function rootMarkers(toml) {
|
|
538
|
+
const raw = toml === undefined ? undefined : topLevelValue(toml, "project_root_markers")?.raw.trim();
|
|
539
|
+
if (!raw || !raw.startsWith("[") || !raw.endsWith("]"))
|
|
540
|
+
return [".git"];
|
|
541
|
+
return [...raw.matchAll(/"([^"]*)"|'([^']*)'/g)].map((m) => m[1] ?? m[2]);
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* The folders whose `.codex/config.toml` Codex layers over the user's, root
|
|
545
|
+
* first (codex-rs config/src/loader/mod.rs, discover_project_layers): from
|
|
546
|
+
* the project root (the closest folder holding a root marker; a `.git`
|
|
547
|
+
* folder counts only with a HEAD in it) down to `cwd`; with no root, `cwd`
|
|
548
|
+
* alone.
|
|
549
|
+
*/
|
|
550
|
+
export function projectConfigFolders(cwd, markers = [".git"]) {
|
|
551
|
+
const dirs = [];
|
|
552
|
+
let dir = resolve(cwd);
|
|
553
|
+
for (;;) {
|
|
554
|
+
dirs.push(dir);
|
|
555
|
+
const isRoot = markers.some((m) => {
|
|
556
|
+
const at = join(dir, m);
|
|
557
|
+
try {
|
|
558
|
+
return m === ".git" && statSync(at).isDirectory() ? existsSync(join(at, "HEAD")) : existsSync(at);
|
|
559
|
+
}
|
|
560
|
+
catch {
|
|
561
|
+
return false;
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
if (isRoot)
|
|
565
|
+
return dirs.reverse();
|
|
566
|
+
const up = dirname(dir);
|
|
567
|
+
if (up === dir)
|
|
568
|
+
return [resolve(cwd)];
|
|
569
|
+
dir = up;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
406
572
|
const usd = (n) => (Math.abs(n) >= 100 ? `$${Math.round(n).toLocaleString("en-US")}` : `$${n.toFixed(2)}`);
|
|
407
573
|
const k = (n) => `${Math.round(n / 1000)}k`;
|
|
408
574
|
/** The lines under the lever in `fix`: each limit tried. */
|
package/dist/compactwindow.js
CHANGED
|
@@ -30,7 +30,7 @@ import { compactWindowOverrides, effectiveCompactWindow, settingsSources } from
|
|
|
30
30
|
import { withInvocation } from "./invocation.js";
|
|
31
31
|
import { idleCompactMinTokens, isIdleCompactNotice, REQUEST_ROWS } from "./coldresume.js";
|
|
32
32
|
import { rowsOf } from "./scan.js";
|
|
33
|
-
import { claudeDir } from "./claudedir.js";
|
|
33
|
+
import { claudeDir, tilde } from "./claudedir.js";
|
|
34
34
|
/** Claude Code compacts at about this share of the window (970k of 1M, measured). */
|
|
35
35
|
const TRIGGER_SHARE = 0.97;
|
|
36
36
|
/** Context right after a compaction: system prompt, tools and the summary (median 75,447, measured). */
|
|
@@ -282,7 +282,7 @@ export function renderCompactWindows(r, current, limitLines) {
|
|
|
282
282
|
// Counted and said exactly as `savings` says it, so the two screens agree.
|
|
283
283
|
lines.push(`Last ${r.days} days, ${sessionsLabel(r.sessions, r.transcripts ?? r.sessions)}, at list prices`);
|
|
284
284
|
lines.push("─".repeat(66));
|
|
285
|
-
lines.push(`Now: ${current ? `${k(current)} (set in
|
|
285
|
+
lines.push(`Now: ${current ? `${k(current)} (set in ${tilde(settingsPath())})` : "not set: Claude Code compacts near the model's full window"}`);
|
|
286
286
|
if (r.sessions === 0) {
|
|
287
287
|
lines.push("No sessions with 10+ requests in the window to replay.");
|
|
288
288
|
return withInvocation(lines.join("\n"));
|
package/dist/cursor.d.ts
CHANGED
|
@@ -67,6 +67,16 @@ export interface CursorReply {
|
|
|
67
67
|
/** The workspace folder the chat belongs to, when Cursor recorded one. */
|
|
68
68
|
folder?: string;
|
|
69
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* The Claude id a Cursor model name stands for, so it gets that version's
|
|
72
|
+
* list price rather than its family's: Cursor puts the version first and
|
|
73
|
+
* adds its own modes ("claude-4.6-opus-high-thinking" is claude-opus-4-6,
|
|
74
|
+
* "claude-3.7-sonnet" claude-3-7-sonnet). Thinking and effort levels bill as
|
|
75
|
+
* output at the same rates. Undefined for anything else, and for a "fast"
|
|
76
|
+
* variant, which Anthropic bills at a premium of its own (left to the
|
|
77
|
+
* family's price, an estimate).
|
|
78
|
+
*/
|
|
79
|
+
export declare function cursorClaudeId(name: string | undefined): string | undefined;
|
|
70
80
|
/**
|
|
71
81
|
* Every Cursor reply with token counts since `since`. Only chats updated
|
|
72
82
|
* since then are read, each by its key range (on the author's 3 GB history
|
package/dist/cursor.js
CHANGED
|
@@ -294,6 +294,26 @@ function cursorFolders() {
|
|
|
294
294
|
}
|
|
295
295
|
return out;
|
|
296
296
|
}
|
|
297
|
+
/**
|
|
298
|
+
* The Claude id a Cursor model name stands for, so it gets that version's
|
|
299
|
+
* list price rather than its family's: Cursor puts the version first and
|
|
300
|
+
* adds its own modes ("claude-4.6-opus-high-thinking" is claude-opus-4-6,
|
|
301
|
+
* "claude-3.7-sonnet" claude-3-7-sonnet). Thinking and effort levels bill as
|
|
302
|
+
* output at the same rates. Undefined for anything else, and for a "fast"
|
|
303
|
+
* variant, which Anthropic bills at a premium of its own (left to the
|
|
304
|
+
* family's price, an estimate).
|
|
305
|
+
*/
|
|
306
|
+
export function cursorClaudeId(name) {
|
|
307
|
+
const m = /^claude-(\d+)(?:\.(\d{1,2}))?-(opus|sonnet|haiku|fable|mythos)((?:-[a-z0-9]+)*)$/i.exec(name ?? "");
|
|
308
|
+
if (!m || /-fast\b/i.test(m[4]))
|
|
309
|
+
return undefined;
|
|
310
|
+
const [, major, minor, family] = m;
|
|
311
|
+
const fam = family.toLowerCase();
|
|
312
|
+
// Claude 3's ids put the version first too (claude-3-5-sonnet); from Claude 4 on, the family does.
|
|
313
|
+
return Number(major) < 4
|
|
314
|
+
? `claude-${major}${minor ? `-${minor}` : ""}-${fam}`
|
|
315
|
+
: `claude-${fam}-${major}${minor ? `-${minor}` : ""}`;
|
|
316
|
+
}
|
|
297
317
|
/** A time Cursor wrote: epoch ms (as a number or digits) or an ISO string. */
|
|
298
318
|
function cursorTime(v) {
|
|
299
319
|
const n = typeof v === "number" ? v : typeof v === "string" ? (/^\d+$/.test(v) ? Number(v) : Date.parse(v)) : Number.NaN;
|
package/dist/doctor.d.ts
CHANGED
|
@@ -25,6 +25,15 @@ export declare function skillCheck(label: string, files: string[], okDetail?: st
|
|
|
25
25
|
* read it (it ignores the key; nothing else in the file is affected).
|
|
26
26
|
*/
|
|
27
27
|
export declare function cacheTtlCheck(home?: string, env?: NodeJS.ProcessEnv): Check | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Models Claude Code (subagents included) and Codex ran in the last `days`
|
|
30
|
+
* that the price table has no row for: priced by a family fallback (an
|
|
31
|
+
* estimate) or not at all, in spend, savings, fix and the budget alike.
|
|
32
|
+
* Read through spend's own readers, so the transcript cache they keep makes
|
|
33
|
+
* a second doctor cheap. Undefined when nothing ran in the period; ids that
|
|
34
|
+
* are never billed (`<synthetic>`) are not named.
|
|
35
|
+
*/
|
|
36
|
+
export declare function unpricedModelsCheck(days?: number, now?: number, home?: string): Check | undefined;
|
|
28
37
|
/**
|
|
29
38
|
* Devin (Cognition): the Devin CLI, and Devin Desktop (formerly Windsurf).
|
|
30
39
|
* Checked against docs.devin.ai (2026-10-20): the CLI runs hooks from
|
package/dist/doctor.js
CHANGED
|
@@ -29,6 +29,8 @@ import { claudeCodePresent, claudeDir, claudeJsonPath } from "./claudedir.js";
|
|
|
29
29
|
import { codexDir } from "./codexdir.js";
|
|
30
30
|
import { periodSpend, readBudget } from "./budget.js";
|
|
31
31
|
import { sessionPriceThresholds } from "./sessionprice.js";
|
|
32
|
+
import { claudeSpend, codexSpend } from "./spend.js";
|
|
33
|
+
import { hasKnownPrice, isUnbilledModel } from "./pricing.js";
|
|
32
34
|
function claudeDesktopConfigPath() {
|
|
33
35
|
switch (platform()) {
|
|
34
36
|
case "darwin": return join(homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json");
|
|
@@ -283,6 +285,43 @@ export function cacheTtlCheck(home = homedir(), env = process.env) {
|
|
|
283
285
|
return { label, status: "warn", detail: `${set}; ${describeInstalls(old)} predates the setting (${CACHE_TTL_SINCE}) and ignores it` };
|
|
284
286
|
return { label, status: "ok", detail: `${set} (context-doctor cache-ttl; off removes it)` };
|
|
285
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* Models Claude Code (subagents included) and Codex ran in the last `days`
|
|
290
|
+
* that the price table has no row for: priced by a family fallback (an
|
|
291
|
+
* estimate) or not at all, in spend, savings, fix and the budget alike.
|
|
292
|
+
* Read through spend's own readers, so the transcript cache they keep makes
|
|
293
|
+
* a second doctor cheap. Undefined when nothing ran in the period; ids that
|
|
294
|
+
* are never billed (`<synthetic>`) are not named.
|
|
295
|
+
*/
|
|
296
|
+
export function unpricedModelsCheck(days = 30, now = Date.now(), home = homedir()) {
|
|
297
|
+
const label = "Model prices";
|
|
298
|
+
const since = now - days * 86_400_000;
|
|
299
|
+
const seen = new Map();
|
|
300
|
+
const add = (items) => {
|
|
301
|
+
for (const i of items)
|
|
302
|
+
if (i.model)
|
|
303
|
+
seen.set(i.model, (seen.get(i.model) ?? 0) + 1);
|
|
304
|
+
};
|
|
305
|
+
try {
|
|
306
|
+
add(claudeSpend(since, now));
|
|
307
|
+
}
|
|
308
|
+
catch { /* unreadable: the rest still counts */ }
|
|
309
|
+
try {
|
|
310
|
+
add(codexSpend(since, now, home));
|
|
311
|
+
}
|
|
312
|
+
catch { /* unreadable: the rest still counts */ }
|
|
313
|
+
if (seen.size === 0)
|
|
314
|
+
return undefined;
|
|
315
|
+
const unknown = [...seen].filter(([m]) => !hasKnownPrice(m) && !isUnbilledModel(m)).sort((a, b) => b[1] - a[1]);
|
|
316
|
+
if (!unknown.length)
|
|
317
|
+
return { label, status: "ok", detail: `every model used in the last ${days} days has a list price on file` };
|
|
318
|
+
const named = unknown.slice(0, 5).map(([m, n]) => `${m} (${n.toLocaleString("en-US")} request${n === 1 ? "" : "s"})`).join(", ") + (unknown.length > 5 ? ` and ${unknown.length - 5} more` : "");
|
|
319
|
+
return {
|
|
320
|
+
label,
|
|
321
|
+
status: "warn",
|
|
322
|
+
detail: `no list price on file for ${named} in the last ${days} days: their dollars (spend, savings, fix, budget) are estimated from the model family or missing. A newer context-doctor may know them: npm install -g context-doctor@latest`,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
286
325
|
/**
|
|
287
326
|
* Devin (Cognition): the Devin CLI, and Devin Desktop (formerly Windsurf).
|
|
288
327
|
* Checked against docs.devin.ai (2026-10-20): the CLI runs hooks from
|
|
@@ -775,6 +814,12 @@ export async function runDoctor() {
|
|
|
775
814
|
else if (hooked)
|
|
776
815
|
checks.push({ label: "Session price", status: "ok", detail: `said once when a session passes ${at.map((t) => `$${t.toLocaleString("en-US")}`).join(", ").replace(/, ([^,]+)$/, " and $1")} (CONTEXT_DOCTOR_SESSION_PRICE to change; CONTEXT_DOCTOR_NO_SESSION_PRICE=1 for off)` });
|
|
777
816
|
}
|
|
817
|
+
// Models in the last 30 days with no list price of their own: their dollars everywhere are a guess or missing.
|
|
818
|
+
{
|
|
819
|
+
const c = unpricedModelsCheck();
|
|
820
|
+
if (c)
|
|
821
|
+
checks.push(c);
|
|
822
|
+
}
|
|
778
823
|
checks.push(await checkMcpHandshake());
|
|
779
824
|
// Autopilot: optional, so "off" is a skip, not a failure. When it is on, a
|
|
780
825
|
// dead proxy or a settings file pointing elsewhere is a real problem.
|
package/dist/fanout.js
CHANGED
|
@@ -159,6 +159,8 @@ function aliasModel(alias, costs) {
|
|
|
159
159
|
const seen = Object.entries(costs.byModel).filter(([m]) => a.family.test(m)).sort((x, y) => y[1].n - x[1].n)[0]?.[0];
|
|
160
160
|
return seen ?? a.fallback;
|
|
161
161
|
}
|
|
162
|
+
/** "an Explore", "a general-purpose": the article for a subagent type (it read "a Explore subagent"). */
|
|
163
|
+
const an = (type) => (/^[aeiou]/i.test(type) ? `an ${type}` : `a ${type}`);
|
|
162
164
|
const short = (model) => {
|
|
163
165
|
const m = /claude-(opus|sonnet|haiku|fable|mythos)-(\d+)(?:-(\d{1,2}))?(?!\d)/i.exec(model ?? "");
|
|
164
166
|
return m ? `${m[1][0].toUpperCase()}${m[1].slice(1)} ${m[2]}${m[3] ? `.${m[3]}` : ""}` : model ?? "its model";
|
|
@@ -279,10 +281,10 @@ export function fanoutMessage(est, alias, log, own) {
|
|
|
279
281
|
const sonnet = cheaper ? ` On Sonnet: ~${formatUsd(cheaper.median)}${cheaper.measured ? "" : " at its price"}.` : "";
|
|
280
282
|
out.systemMessage = showBurst
|
|
281
283
|
? `context-doctor: ${burst} subagents in 2 minutes, ~${formatUsd(est.median * burst)} together at your median of ~${formatUsd(est.median)} for ${own.type} on ${short(est.model)} (${runs}).${sonnet}`
|
|
282
|
-
: `context-doctor:
|
|
284
|
+
: `context-doctor: ${an(own.type)} subagent on ${short(est.model)} costs ~${formatUsd(est.median)} at your median (p90 ${formatUsd(est.p90)}, ${runs}, list price).${sonnet}`;
|
|
283
285
|
}
|
|
284
286
|
if (cheaper && upTo.find((l) => l.c)?.id === own.id) {
|
|
285
|
-
out.additionalContext = `context-doctor: on this machine
|
|
287
|
+
out.additionalContext = `context-doctor: on this machine ${an(own.type)} subagent costs ~${formatUsd(est.median)} at the median on ${short(est.model)}, ~${formatUsd(cheaper.median)} on Sonnet. For search, reading or summarising, pass model: "sonnet" to the Agent tool.`;
|
|
286
288
|
}
|
|
287
289
|
return out;
|
|
288
290
|
}
|
package/dist/fix.d.ts
CHANGED
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
* `--apply all`), and never over a value the user set.
|
|
35
35
|
*/
|
|
36
36
|
import { type CacheTtlReport, type CurrentTtl } from "./cachettl.js";
|
|
37
|
+
import { type ClaudeCodeInstall } from "./modelswitch.js";
|
|
37
38
|
import { type ProjectPlan } from "./prune.js";
|
|
38
39
|
import { readLedger } from "./ledger.js";
|
|
39
40
|
import { type SubagentModelReplay } from "./subagentmodel.js";
|
|
@@ -61,6 +62,8 @@ export interface Fix {
|
|
|
61
62
|
value?: number | string;
|
|
62
63
|
/** What would keep it from taking effect (environment, project or managed settings, per-model values). */
|
|
63
64
|
overriddenBy?: string[];
|
|
65
|
+
/** Installed Claude Code versions older than the setting: they ignore it (named, not blocked). */
|
|
66
|
+
tooOld?: string;
|
|
64
67
|
/** More lines under it in the plan (the subagents' model: per agent type). */
|
|
65
68
|
detail?: string[];
|
|
66
69
|
}
|
|
@@ -119,6 +122,8 @@ export interface FixInputs {
|
|
|
119
122
|
};
|
|
120
123
|
/** Codex's rollouts replayed at earlier auto-compact limits (codexcompact.ts). */
|
|
121
124
|
codexCompact?: CodexCompactReport;
|
|
125
|
+
/** The Claude Code installs found here (CLI and desktop app), to name the ones a setting is too new for. */
|
|
126
|
+
claudeCodeInstalls?: ClaudeCodeInstall[];
|
|
122
127
|
/** model_auto_compact_token_limit at the top of the user's config.toml, as written: never overwritten. */
|
|
123
128
|
codexCompactSet?: {
|
|
124
129
|
raw: string;
|
|
@@ -216,8 +221,10 @@ export declare function savedSince(records: FixRecord[], opts?: {
|
|
|
216
221
|
codexPaths?: string[];
|
|
217
222
|
ledger?: ReturnType<typeof readLedger>;
|
|
218
223
|
}): SinceResult[];
|
|
224
|
+
/** The overlap lines for the priced fixes in a plan, "a and b: why;" each. */
|
|
225
|
+
export declare function overlaps(fixes: Fix[]): string[];
|
|
219
226
|
export declare function renderFixPlan(days: number, billedUsd: number, fixes: Fix[], since: SinceResult[], command?: (s: string) => string, coldResume?: FixInputs["coldResume"], recordProblem?: string, codexBilledUsd?: number): string;
|
|
220
|
-
export declare function renderFixShare(days: number, billedUsd: number, fixes: Fix[]): string;
|
|
227
|
+
export declare function renderFixShare(days: number, billedUsd: number, fixes: Fix[], codexBilledUsd?: number): string;
|
|
221
228
|
export declare function parseFixIds(raw: string, fixes: Fix[]): {
|
|
222
229
|
ids: FixId[];
|
|
223
230
|
unknown: string[];
|