context-doctor 0.33.0 → 0.35.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 +13 -10
- package/dist/autoclear.d.ts +5 -2
- package/dist/autoclear.js +57 -19
- package/dist/autopilot.d.ts +0 -6
- package/dist/autopilot.js +37 -2
- package/dist/cachekey.d.ts +25 -0
- package/dist/cachekey.js +73 -0
- package/dist/cachemisses.d.ts +88 -0
- package/dist/cachemisses.js +325 -0
- package/dist/cacheplan.d.ts +30 -10
- package/dist/cacheplan.js +157 -32
- package/dist/cli.js +68 -19
- package/dist/codexroute.d.ts +46 -0
- package/dist/codexroute.js +122 -0
- package/dist/coldresume.d.ts +62 -0
- package/dist/coldresume.js +85 -6
- package/dist/compactquality.d.ts +2 -0
- package/dist/compactquality.js +21 -3
- package/dist/compactwindow.d.ts +2 -0
- package/dist/compactwindow.js +30 -7
- package/dist/config.d.ts +11 -0
- package/dist/config.js +117 -4
- package/dist/dashboard.js +15 -1
- package/dist/doctor.d.ts +23 -0
- package/dist/doctor.js +133 -10
- package/dist/hook.d.ts +18 -0
- package/dist/hook.js +58 -8
- package/dist/impact.js +32 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/install.d.ts +25 -0
- package/dist/install.js +148 -45
- package/dist/ledger.d.ts +2 -0
- package/dist/mcp.js +56 -53
- package/dist/modelswitch.d.ts +76 -0
- package/dist/modelswitch.js +184 -0
- package/dist/optimize.js +40 -6
- package/dist/pack.d.ts +32 -3
- package/dist/pack.js +176 -24
- package/dist/parse.d.ts +3 -1
- package/dist/parse.js +123 -3
- package/dist/pricing.js +8 -0
- package/dist/profile.js +60 -17
- package/dist/proxy.d.ts +22 -1
- package/dist/proxy.js +89 -12
- package/dist/report.js +8 -2
- package/dist/savings.d.ts +15 -2
- package/dist/savings.js +9 -11
- package/dist/session.js +19 -3
- package/dist/sketch.d.ts +2 -0
- package/dist/sketch.js +10 -1
- package/dist/tokens.js +20 -9
- package/dist/wrap.js +27 -5
- package/package.json +1 -1
package/dist/install.d.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* Every config edit is a careful JSON merge with a .backup file written first.
|
|
7
7
|
* `context-doctor uninstall` reverses it.
|
|
8
8
|
*/
|
|
9
|
+
import { type ClaudeCodeInstall, type ModelSwitchReadiness } from "./modelswitch.js";
|
|
9
10
|
export declare function geminiSettingsPath(): string;
|
|
10
11
|
export declare function geminiCliPresent(): boolean;
|
|
11
12
|
/**
|
|
@@ -91,7 +92,31 @@ export declare function installStatusLine(): "installed" | "already" | "kept-for
|
|
|
91
92
|
* tool call, which the hook's stat-only fast path and regrowth gate make cheap.
|
|
92
93
|
*/
|
|
93
94
|
export declare function cursorHooksPath(): string;
|
|
95
|
+
export declare const isOurCursorEntry: (e: unknown) => boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Cursor's events whose `additional_context` can reach its agent: after a
|
|
98
|
+
* tool call, after a failed one, and at the start of a chat. Cursor 3.18's
|
|
99
|
+
* agent runtime attaches the first two to the tool result (Cursor confirmed
|
|
100
|
+
* postToolUseFailure delivery in 3.17.8); before 3.17 it dropped postToolUse
|
|
101
|
+
* output (staff-confirmed, March 2026). sessionStart is the documented way
|
|
102
|
+
* to add context to a chat's start, and serves a resumed heavy chat.
|
|
103
|
+
*/
|
|
104
|
+
export declare const CURSOR_HOOK_EVENTS: readonly ["postToolUse", "postToolUseFailure", "sessionStart"];
|
|
94
105
|
export declare function installCursorHook(): string | null;
|
|
106
|
+
/**
|
|
107
|
+
* The model-switch warning (PreModelSwitch) goes into ~/.claude/settings.json
|
|
108
|
+
* only when every Claude Code found here runs that event: an older one skips
|
|
109
|
+
* the WHOLE file over an event it does not know (measured on 2.1.62: none of
|
|
110
|
+
* the user's settings or hooks ran). With an older one present, an entry an
|
|
111
|
+
* earlier install wrote is taken out again.
|
|
112
|
+
*/
|
|
113
|
+
export declare function installModelSwitchHook(readiness?: ModelSwitchReadiness): "installed" | "old" | "unknown" | "no-claude-code";
|
|
114
|
+
/**
|
|
115
|
+
* The Claude Code versions to plan for: CONTEXT_DOCTOR_CLAUDE_CODE_VERSIONS
|
|
116
|
+
* ("2.1.299,2.1.62") when detection cannot see an install, else what
|
|
117
|
+
* claudeCodeInstalls finds.
|
|
118
|
+
*/
|
|
119
|
+
export declare function claudeCodeInstallsFromEnv(): ClaudeCodeInstall[];
|
|
95
120
|
export declare function codexDir(): string;
|
|
96
121
|
/**
|
|
97
122
|
* Add or replace our [mcp_servers.context-doctor] table in config.toml without
|
package/dist/install.js
CHANGED
|
@@ -11,6 +11,7 @@ import { homedir, platform } from "node:os";
|
|
|
11
11
|
import { delimiter, dirname, join, sep } from "node:path";
|
|
12
12
|
import { fileURLToPath } from "node:url";
|
|
13
13
|
import { selfCommand, withInvocation } from "./invocation.js";
|
|
14
|
+
import { claudeCodeInstalls, describeInstalls, modelSwitchReadiness, PRE_MODEL_SWITCH_SINCE } from "./modelswitch.js";
|
|
14
15
|
function claudeDesktopConfigPath() {
|
|
15
16
|
switch (platform()) {
|
|
16
17
|
case "darwin": return join(homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json");
|
|
@@ -135,7 +136,7 @@ export function claudeInstalledCopies(home = homedir()) {
|
|
|
135
136
|
catch { /* unreadable: not ours to judge here */ }
|
|
136
137
|
try {
|
|
137
138
|
const hooks = readJson(join(home, ".claude", "settings.json")).hooks ?? {};
|
|
138
|
-
if (["UserPromptSubmit", "PreCompact"].some((ev) => (hooks[ev] ?? []).some?.(isOurHookEntry)))
|
|
139
|
+
if (["UserPromptSubmit", "PreCompact", "SessionStart", "PreModelSwitch"].some((ev) => (hooks[ev] ?? []).some?.(isOurHookEntry)))
|
|
139
140
|
found.push("hooks in ~/.claude/settings.json");
|
|
140
141
|
}
|
|
141
142
|
catch { /* unreadable */ }
|
|
@@ -306,9 +307,12 @@ function isOurHookEntry(entry) {
|
|
|
306
307
|
function installHook() {
|
|
307
308
|
const settings = join(homedir(), ".claude", "settings.json");
|
|
308
309
|
const path = installHookInto(settings, join(homedir(), ".claude"));
|
|
309
|
-
// The same command also answers PreCompact
|
|
310
|
-
|
|
310
|
+
// The same command also answers PreCompact (what each compaction's summary
|
|
311
|
+
// must keep) and SessionStart (Claude Code's verdict on a resumed cache).
|
|
312
|
+
if (path) {
|
|
311
313
|
installHookInto(settings, join(homedir(), ".claude"), "PreCompact");
|
|
314
|
+
installHookInto(settings, join(homedir(), ".claude"), "SessionStart");
|
|
315
|
+
}
|
|
312
316
|
return path;
|
|
313
317
|
}
|
|
314
318
|
/**
|
|
@@ -347,12 +351,16 @@ function installHookInto(settingsPath, gateDir, event = "UserPromptSubmit") {
|
|
|
347
351
|
writeJsonWithBackup(settingsPath, settings);
|
|
348
352
|
return settingsPath;
|
|
349
353
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
354
|
+
/** Each hook removal on its own: a malformed file for one app must not leave the others installed. */
|
|
355
|
+
function uninstallHook(attempt) {
|
|
356
|
+
const claude = join(homedir(), ".claude", "settings.json");
|
|
357
|
+
attempt("Claude Code every-prompt hook", () => uninstallHookFrom(claude, "Claude Code every-prompt hook"));
|
|
358
|
+
attempt("Claude Code compaction hook", () => uninstallHookFrom(claude, "Claude Code compaction hook", "PreCompact"));
|
|
359
|
+
attempt("Claude Code session-start hook", () => uninstallHookFrom(claude, "Claude Code session-start hook", "SessionStart"));
|
|
360
|
+
attempt("Claude Code model-switch warning", () => uninstallHookFrom(claude, "Claude Code model-switch warning", "PreModelSwitch"));
|
|
361
|
+
attempt("Codex every-prompt hook", () => uninstallHookFrom(join(homedir(), ".codex", "hooks.json"), "Codex every-prompt hook"));
|
|
362
|
+
attempt("Gemini CLI every-prompt hook", () => uninstallHookFrom(geminiSettingsPath(), "Gemini CLI every-prompt hook", "BeforeAgent"));
|
|
363
|
+
attempt("Cursor hooks", () => uninstallCursorHook());
|
|
356
364
|
}
|
|
357
365
|
/**
|
|
358
366
|
* Cursor runs Claude Code's UserPromptSubmit hook as its own
|
|
@@ -365,7 +373,16 @@ function uninstallHook() {
|
|
|
365
373
|
export function cursorHooksPath() {
|
|
366
374
|
return join(homedir(), ".cursor", "hooks.json");
|
|
367
375
|
}
|
|
368
|
-
const isOurCursorEntry = (e) => /(cli\.js|context-doctor(\.cmd|\.exe|\.bat)?)"?\s+hook\s*$/.test(String(e?.command ?? ""));
|
|
376
|
+
export const isOurCursorEntry = (e) => /(cli\.js|context-doctor(\.cmd|\.exe|\.bat)?)"?\s+hook\s*$/.test(String(e?.command ?? ""));
|
|
377
|
+
/**
|
|
378
|
+
* Cursor's events whose `additional_context` can reach its agent: after a
|
|
379
|
+
* tool call, after a failed one, and at the start of a chat. Cursor 3.18's
|
|
380
|
+
* agent runtime attaches the first two to the tool result (Cursor confirmed
|
|
381
|
+
* postToolUseFailure delivery in 3.17.8); before 3.17 it dropped postToolUse
|
|
382
|
+
* output (staff-confirmed, March 2026). sessionStart is the documented way
|
|
383
|
+
* to add context to a chat's start, and serves a resumed heavy chat.
|
|
384
|
+
*/
|
|
385
|
+
export const CURSOR_HOOK_EVENTS = ["postToolUse", "postToolUseFailure", "sessionStart"];
|
|
369
386
|
export function installCursorHook() {
|
|
370
387
|
if (!existsSync(join(homedir(), ".cursor")))
|
|
371
388
|
return null;
|
|
@@ -373,13 +390,18 @@ export function installCursorHook() {
|
|
|
373
390
|
const config = readJson(path);
|
|
374
391
|
config.version = config.version ?? 1;
|
|
375
392
|
config.hooks = config.hooks ?? {};
|
|
376
|
-
const entries = config.hooks.postToolUse ?? [];
|
|
377
393
|
const want = hookCommand();
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
394
|
+
let changed = false;
|
|
395
|
+
for (const event of CURSOR_HOOK_EVENTS) {
|
|
396
|
+
const entries = config.hooks[event] ?? [];
|
|
397
|
+
const ours = entries.filter(isOurCursorEntry);
|
|
398
|
+
if (ours.length === 1 && ours[0].command === want)
|
|
399
|
+
continue; // already correct
|
|
400
|
+
config.hooks[event] = [...entries.filter((e) => !isOurCursorEntry(e)), { command: want, timeout: 10 }];
|
|
401
|
+
changed = true;
|
|
402
|
+
}
|
|
403
|
+
if (changed)
|
|
404
|
+
writeJsonWithBackup(path, config);
|
|
383
405
|
return path;
|
|
384
406
|
}
|
|
385
407
|
function uninstallCursorHook() {
|
|
@@ -387,18 +409,55 @@ function uninstallCursorHook() {
|
|
|
387
409
|
if (!existsSync(path))
|
|
388
410
|
return;
|
|
389
411
|
const config = readJson(path);
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
412
|
+
let removed = false;
|
|
413
|
+
for (const event of CURSOR_HOOK_EVENTS) {
|
|
414
|
+
const entries = config.hooks?.[event];
|
|
415
|
+
if (!entries)
|
|
416
|
+
continue;
|
|
417
|
+
const filtered = entries.filter((e) => !isOurCursorEntry(e));
|
|
418
|
+
if (filtered.length === entries.length)
|
|
419
|
+
continue;
|
|
420
|
+
removed = true;
|
|
421
|
+
if (filtered.length === 0)
|
|
422
|
+
delete config.hooks[event];
|
|
423
|
+
else
|
|
424
|
+
config.hooks[event] = filtered;
|
|
425
|
+
}
|
|
426
|
+
if (!removed)
|
|
395
427
|
return;
|
|
396
|
-
if (filtered.length === 0)
|
|
397
|
-
delete config.hooks.postToolUse;
|
|
398
|
-
else
|
|
399
|
-
config.hooks.postToolUse = filtered;
|
|
400
428
|
writeUninstalled(path, config, ["version"]);
|
|
401
|
-
done("✓ Cursor
|
|
429
|
+
done("✓ Cursor hooks removed");
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* The model-switch warning (PreModelSwitch) goes into ~/.claude/settings.json
|
|
433
|
+
* only when every Claude Code found here runs that event: an older one skips
|
|
434
|
+
* the WHOLE file over an event it does not know (measured on 2.1.62: none of
|
|
435
|
+
* the user's settings or hooks ran). With an older one present, an entry an
|
|
436
|
+
* earlier install wrote is taken out again.
|
|
437
|
+
*/
|
|
438
|
+
export function installModelSwitchHook(readiness = modelSwitchReadiness(claudeCodeInstallsFromEnv())) {
|
|
439
|
+
const dir = join(homedir(), ".claude");
|
|
440
|
+
if (!existsSync(dir))
|
|
441
|
+
return "no-claude-code";
|
|
442
|
+
const settingsPath = join(dir, "settings.json");
|
|
443
|
+
if (readiness.ok) {
|
|
444
|
+
installHookInto(settingsPath, dir, "PreModelSwitch");
|
|
445
|
+
return "installed";
|
|
446
|
+
}
|
|
447
|
+
uninstallHookFrom(settingsPath, "Claude Code model-switch warning (an older Claude Code here would skip the whole file)", "PreModelSwitch");
|
|
448
|
+
return readiness.old.length > 0 ? "old" : "unknown";
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* The Claude Code versions to plan for: CONTEXT_DOCTOR_CLAUDE_CODE_VERSIONS
|
|
452
|
+
* ("2.1.299,2.1.62") when detection cannot see an install, else what
|
|
453
|
+
* claudeCodeInstalls finds.
|
|
454
|
+
*/
|
|
455
|
+
export function claudeCodeInstallsFromEnv() {
|
|
456
|
+
const forced = process.env.CONTEXT_DOCTOR_CLAUDE_CODE_VERSIONS;
|
|
457
|
+
if (forced !== undefined) {
|
|
458
|
+
return forced.split(",").map((v) => v.trim()).filter((v) => /^\d+\.\d+\.\d+$/.test(v)).map((version) => ({ path: "CONTEXT_DOCTOR_CLAUDE_CODE_VERSIONS", version, source: "cli" }));
|
|
459
|
+
}
|
|
460
|
+
return claudeCodeInstalls();
|
|
402
461
|
}
|
|
403
462
|
function uninstallHookFrom(settingsPath, label, event = "UserPromptSubmit") {
|
|
404
463
|
if (!existsSync(settingsPath))
|
|
@@ -717,10 +776,31 @@ export function runInstall(options = {}) {
|
|
|
717
776
|
console.error(`✗ Claude Code every-prompt hook: ${e.message}`);
|
|
718
777
|
failures.push("Claude Code hook");
|
|
719
778
|
}
|
|
779
|
+
if (!viaPlugin && existsSync(join(homedir(), ".claude"))) {
|
|
780
|
+
try {
|
|
781
|
+
const readiness = modelSwitchReadiness(claudeCodeInstallsFromEnv());
|
|
782
|
+
switch (installModelSwitchHook(readiness)) {
|
|
783
|
+
case "installed":
|
|
784
|
+
console.log("✓ Claude Code model-switch warning installed (PreModelSwitch): before /model switches, the price of re-writing this context into the new model's cache");
|
|
785
|
+
break;
|
|
786
|
+
case "old":
|
|
787
|
+
console.log(`– Claude Code model-switch warning skipped: ${describeInstalls(readiness.old)} predates PreModelSwitch (${PRE_MODEL_SWITCH_SINCE}) and would`);
|
|
788
|
+
console.log(" ignore your whole ~/.claude/settings.json with it. Update or remove it, then run install again.");
|
|
789
|
+
break;
|
|
790
|
+
case "unknown":
|
|
791
|
+
console.log(`– Claude Code model-switch warning skipped: no Claude Code version found to check (it needs ${PRE_MODEL_SWITCH_SINCE} or later).`);
|
|
792
|
+
break;
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
catch (e) {
|
|
796
|
+
console.error(`✗ Claude Code model-switch warning: ${e.message}`);
|
|
797
|
+
failures.push("Claude Code model-switch warning");
|
|
798
|
+
}
|
|
799
|
+
}
|
|
720
800
|
try {
|
|
721
801
|
const cursorHook = installCursorHook();
|
|
722
802
|
if (cursorHook)
|
|
723
|
-
console.log(`✓ Cursor
|
|
803
|
+
console.log(`✓ Cursor hooks installed (${cursorHook}: after each tool call, after a failed one, at chat start) — Cursor's agent gets the same guidance in heavy sessions`);
|
|
724
804
|
}
|
|
725
805
|
catch (e) {
|
|
726
806
|
console.error(`✗ Cursor hook: ${e.message}`);
|
|
@@ -948,44 +1028,67 @@ export async function runUninstall() {
|
|
|
948
1028
|
// installed state (as every write here used to) lost that original.
|
|
949
1029
|
for (const p of configPaths())
|
|
950
1030
|
backedUp.add(p);
|
|
1031
|
+
// Every step stands alone. A malformed ~/.gemini/settings.json used to
|
|
1032
|
+
// throw out of the whole uninstall midway, leaving everything after it
|
|
1033
|
+
// (status line, Codex, bookkeeping files) installed; now that one target
|
|
1034
|
+
// is reported and the rest still go.
|
|
1035
|
+
const failures = [];
|
|
1036
|
+
const attempt = (label, fn) => {
|
|
1037
|
+
try {
|
|
1038
|
+
fn();
|
|
1039
|
+
}
|
|
1040
|
+
catch (e) {
|
|
1041
|
+
failures.push(label);
|
|
1042
|
+
console.error(`✗ ${label}: ${e.message}`);
|
|
1043
|
+
}
|
|
1044
|
+
};
|
|
951
1045
|
// First: if anything below fails, Claude Code must still reach the API.
|
|
952
1046
|
const hadAutopilot = existsSync(join(homedir(), ".claude", ".context-doctor-autopilot.json"));
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
1047
|
+
try {
|
|
1048
|
+
await uninstallAutopilot();
|
|
1049
|
+
if (hadAutopilot)
|
|
1050
|
+
changes++;
|
|
1051
|
+
}
|
|
1052
|
+
catch (e) {
|
|
1053
|
+
failures.push("autopilot");
|
|
1054
|
+
console.error(`✗ autopilot: ${e.message}`);
|
|
1055
|
+
}
|
|
956
1056
|
for (const target of targets().filter((t) => t.detect())) {
|
|
957
|
-
|
|
1057
|
+
attempt(target.name, () => {
|
|
958
1058
|
if (!existsSync(target.configPath))
|
|
959
|
-
|
|
1059
|
+
return;
|
|
960
1060
|
const config = readJson(target.configPath);
|
|
961
1061
|
if (config.mcpServers?.["context-doctor"]) {
|
|
962
1062
|
delete config.mcpServers["context-doctor"];
|
|
963
1063
|
writeUninstalled(target.configPath, config);
|
|
964
1064
|
done(`✓ ${target.name}: MCP server removed`);
|
|
965
1065
|
}
|
|
966
|
-
}
|
|
967
|
-
catch (e) {
|
|
968
|
-
console.error(`✗ ${target.name}: ${e.message}`);
|
|
969
|
-
}
|
|
1066
|
+
});
|
|
970
1067
|
}
|
|
971
|
-
removeSkill(join(homedir(), ".claude", "skills", "context-doctor"), "Agent Skill");
|
|
972
|
-
uninstallHook();
|
|
973
|
-
uninstallStatusLine();
|
|
974
|
-
uninstallCodex();
|
|
1068
|
+
attempt("Agent Skill", () => removeSkill(join(homedir(), ".claude", "skills", "context-doctor"), "Agent Skill"));
|
|
1069
|
+
uninstallHook(attempt);
|
|
1070
|
+
attempt("Claude Code status line", () => uninstallStatusLine());
|
|
1071
|
+
attempt("Codex", () => uninstallCodex());
|
|
975
1072
|
// Remove our bookkeeping files too — uninstall means gone. The hook's
|
|
976
1073
|
// per-session state is a directory, one small file per session.
|
|
977
1074
|
for (const file of OUR_CLAUDE_FILES) {
|
|
978
1075
|
const p = join(homedir(), ".claude", file);
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
1076
|
+
attempt(file, () => {
|
|
1077
|
+
if (existsSync(p)) {
|
|
1078
|
+
rmSync(p, { recursive: true, force: true });
|
|
1079
|
+
done(`✓ Removed ${file}`);
|
|
1080
|
+
}
|
|
1081
|
+
});
|
|
983
1082
|
}
|
|
984
1083
|
if (deletedStubs.length > 0) {
|
|
985
1084
|
console.log(`✓ Deleted ${deletedStubs.length === 1 ? "a file" : "files"} install had created, now empty: ${deletedStubs.map(tilde).join(", ")}`);
|
|
986
1085
|
}
|
|
987
1086
|
const backups = configPaths().map((p) => p + BACKUP_SUFFIX).filter((b) => existsSync(b));
|
|
988
|
-
if (
|
|
1087
|
+
if (failures.length > 0) {
|
|
1088
|
+
console.error(`Not removed: ${failures.join(", ")} (see above). Everything else was${changes === 0 ? " checked" : " removed"}; fix those files and run uninstall again.`);
|
|
1089
|
+
process.exitCode = 1;
|
|
1090
|
+
}
|
|
1091
|
+
else if (changes === 0)
|
|
989
1092
|
console.log("Nothing of context-doctor's was installed.");
|
|
990
1093
|
else
|
|
991
1094
|
console.log("Done.");
|
package/dist/ledger.d.ts
CHANGED
|
@@ -26,6 +26,8 @@ export interface LedgerEntry {
|
|
|
26
26
|
requests?: number;
|
|
27
27
|
saved?: number;
|
|
28
28
|
model?: string;
|
|
29
|
+
/** The Cursor version that ran the hook (its input's cursor_version), for `doctor`. */
|
|
30
|
+
cv?: string;
|
|
29
31
|
/** rollup only: sums carried forward from entries rotation removed. */
|
|
30
32
|
carried?: CarriedTotals;
|
|
31
33
|
}
|