context-doctor 0.32.0 → 0.33.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 +85 -695
- package/dist/autoclear.js +4 -1
- package/dist/autopilot.d.ts +20 -0
- package/dist/autopilot.js +69 -6
- package/dist/ci.js +9 -1
- package/dist/cli.js +355 -245
- package/dist/clihelp.d.ts +24 -0
- package/dist/clihelp.js +116 -0
- package/dist/compactquality.d.ts +1 -1
- package/dist/compactquality.js +3 -2
- package/dist/compactwindow.d.ts +29 -5
- package/dist/compactwindow.js +50 -18
- package/dist/diff.js +24 -2
- package/dist/doctor.d.ts +3 -2
- package/dist/doctor.js +77 -27
- package/dist/install.d.ts +25 -0
- package/dist/install.js +237 -29
- package/dist/invocation.d.ts +18 -0
- package/dist/invocation.js +49 -0
- package/dist/mcp.d.ts +5 -2
- package/dist/mcp.js +93 -299
- package/dist/mcpclient.js +45 -0
- package/dist/mcpschema.d.ts +1 -1
- package/dist/mcpschema.js +11 -8
- package/dist/optimize.d.ts +2 -0
- package/dist/optimize.js +2 -0
- package/dist/overhead.d.ts +12 -1
- package/dist/overhead.js +31 -7
- package/dist/preferences.d.ts +1 -1
- package/dist/preferences.js +5 -5
- package/dist/proxy.d.ts +1 -1
- package/dist/proxy.js +1 -1
- package/dist/savings.d.ts +3 -0
- package/dist/savings.js +9 -3
- package/dist/session.d.ts +15 -0
- package/dist/session.js +43 -0
- package/dist/subagentpay.d.ts +1 -1
- package/dist/subagentpay.js +3 -2
- package/package.json +5 -8
- package/skills/context-doctor/SKILL.md +3 -3
package/dist/doctor.js
CHANGED
|
@@ -2,19 +2,21 @@
|
|
|
2
2
|
* `context-doctor doctor` — self-check for a local installation.
|
|
3
3
|
*
|
|
4
4
|
* Verifies every integration point end to end and prints one ✓/✗/– line per
|
|
5
|
-
* check, so "it doesn't work" becomes a single pasteable diagnosis.
|
|
6
|
-
*
|
|
5
|
+
* check, so "it doesn't work" becomes a single pasteable diagnosis. Exits 1
|
|
6
|
+
* when any check fails (✗), so a script can tell; an app that is absent, or
|
|
7
|
+
* that nothing was installed into, is a note (–), not a failure.
|
|
7
8
|
*/
|
|
8
9
|
import { autopilotPaths, health, installedVersion, proxyUrl, recentSurfaces } from "./autopilot.js";
|
|
9
10
|
import { spawn } from "node:child_process";
|
|
10
|
-
import { accessSync, constants as fsConstants, existsSync,
|
|
11
|
+
import { accessSync, constants as fsConstants, existsSync, readFileSync } from "node:fs";
|
|
11
12
|
import { homedir, platform } from "node:os";
|
|
12
13
|
import { dirname, join } from "node:path";
|
|
13
14
|
import { fileURLToPath } from "node:url";
|
|
14
15
|
import { ledgerPath } from "./ledger.js";
|
|
15
16
|
import { loadConfig } from "./config.js";
|
|
16
|
-
import { binOnPath, codexMcpEntry, geminiCliPresent, geminiSettingsPath, launcherNamesMissingPackage } from "./install.js";
|
|
17
|
+
import { binOnPath, claudeInstalledCopies, codexMcpEntry, geminiCliPresent, geminiSettingsPath, isEmptyStub, isOurPluginKey, launcherNamesMissingPackage } from "./install.js";
|
|
17
18
|
import { describeDesktopUsage, desktopUsage } from "./desktopusage.js";
|
|
19
|
+
import { withInvocation } from "./invocation.js";
|
|
18
20
|
function claudeDesktopConfigPath() {
|
|
19
21
|
switch (platform()) {
|
|
20
22
|
case "darwin": return join(homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json");
|
|
@@ -33,12 +35,27 @@ function missingPackageCheck(label) {
|
|
|
33
35
|
? { label, status: "warn", detail: "MCP entry runs `npx context-doctor-mcp`, which starts only through your global install (no npm package has that name) — re-run: context-doctor install" }
|
|
34
36
|
: { label, status: "fail", detail: "MCP entry runs `npx context-doctor-mcp`, a package that does not exist on npm, so the server never starts — re-run: context-doctor install" };
|
|
35
37
|
}
|
|
38
|
+
/** The "not installed" note shared by every app whose config has nothing in it. */
|
|
39
|
+
const NOT_INSTALLED = "not installed (no config, or an empty one)";
|
|
40
|
+
/**
|
|
41
|
+
* Read a JSON config for a check: undefined when it is absent or an empty
|
|
42
|
+
* stub (`{}`, `{"mcpServers":{}}`, `{"hooks":{}}`), which configure nothing
|
|
43
|
+
* and are what install leaves behind once our entries are removed. Both
|
|
44
|
+
* mean "not installed" for every app alike. Throws on invalid JSON.
|
|
45
|
+
*/
|
|
46
|
+
function readConfig(path, extraKeys = []) {
|
|
47
|
+
if (!existsSync(path))
|
|
48
|
+
return undefined;
|
|
49
|
+
const config = JSON.parse(readFileSync(path, "utf8"));
|
|
50
|
+
return isEmptyStub(config, extraKeys) ? undefined : config;
|
|
51
|
+
}
|
|
36
52
|
function checkMcpEntry(appName, configPath, plugin = false) {
|
|
37
53
|
const viaPlugin = { label: appName, status: "ok", detail: "MCP server provided by the context-doctor plugin" };
|
|
38
|
-
if (!existsSync(configPath))
|
|
39
|
-
return plugin ? viaPlugin : { label: appName, status: "skip", detail: "app not detected (config file absent)" };
|
|
40
54
|
try {
|
|
41
|
-
const config =
|
|
55
|
+
const config = readConfig(configPath);
|
|
56
|
+
if (!config) {
|
|
57
|
+
return plugin ? viaPlugin : { label: appName, status: "skip", detail: existsSync(configPath) ? NOT_INSTALLED : "app not detected (config file absent)" };
|
|
58
|
+
}
|
|
42
59
|
const entry = config.mcpServers?.["context-doctor"];
|
|
43
60
|
if (!entry && plugin)
|
|
44
61
|
return viaPlugin;
|
|
@@ -116,7 +133,7 @@ function pluginEnabled(layers) {
|
|
|
116
133
|
const state = new Map();
|
|
117
134
|
for (const s of layers) {
|
|
118
135
|
for (const [key, on] of Object.entries((s.enabledPlugins ?? {}))) {
|
|
119
|
-
if (key
|
|
136
|
+
if (isOurPluginKey(key))
|
|
120
137
|
state.set(key, on === true);
|
|
121
138
|
}
|
|
122
139
|
}
|
|
@@ -171,14 +188,21 @@ export async function runDoctor() {
|
|
|
171
188
|
checks.push({ label: "Desktop chat tool use", status: "ok", detail: describeDesktopUsage(desktop, recentSurfaces(7).get("claude-desktop") ?? 0) });
|
|
172
189
|
const layers = claudeSettingsLayers();
|
|
173
190
|
const plugin = pluginEnabled(layers);
|
|
174
|
-
|
|
175
|
-
|
|
191
|
+
const claudeMcp = checkMcpEntry("Claude Code", join(homedir(), ".claude.json"), plugin);
|
|
192
|
+
const cursorMcp = checkMcpEntry("Cursor", join(homedir(), ".cursor", "mcp.json"));
|
|
193
|
+
checks.push(claudeMcp, cursorMcp);
|
|
194
|
+
// A missing hook is an issue only for an app that is there to hook: when
|
|
195
|
+
// the app's own MCP check says it is absent or holds nothing of ours, the
|
|
196
|
+
// hook is "not installed" too, not a failure.
|
|
197
|
+
const missingHook = (label, app) => app.status === "skip"
|
|
198
|
+
? { label, status: "skip", detail: "not installed (nor is the MCP server, above)" }
|
|
199
|
+
: { label, status: "fail", detail: "not registered — run: context-doctor install" };
|
|
176
200
|
// Hook registration. ~/.claude without a settings.json is Claude Code with
|
|
177
201
|
// no hook registered, not Claude Code absent.
|
|
178
202
|
const settingsPath = join(homedir(), ".claude", "settings.json");
|
|
179
203
|
if (existsSync(settingsPath) || existsSync(join(homedir(), ".claude"))) {
|
|
180
204
|
try {
|
|
181
|
-
const settings =
|
|
205
|
+
const settings = readConfig(settingsPath) ?? {};
|
|
182
206
|
const entries = settings.hooks?.UserPromptSubmit ?? [];
|
|
183
207
|
const ours = entries.map((e) => e.hooks?.[0]?.command ?? "").find((c) => /context-doctor|cli\.js"?\s+hook/.test(c));
|
|
184
208
|
if (hooksDisabled(layers)) {
|
|
@@ -194,7 +218,7 @@ export async function runDoctor() {
|
|
|
194
218
|
checks.push({ label: "Compaction hook", status: "ok", detail: "PreCompact: provided by the context-doctor plugin" });
|
|
195
219
|
}
|
|
196
220
|
else if (!ours) {
|
|
197
|
-
checks.push(
|
|
221
|
+
checks.push(missingHook("Every-prompt hook", claudeMcp));
|
|
198
222
|
}
|
|
199
223
|
else {
|
|
200
224
|
// "Registered" is not "working": a hook whose binary has been deleted
|
|
@@ -218,6 +242,17 @@ export async function runDoctor() {
|
|
|
218
242
|
else {
|
|
219
243
|
checks.push({ label: "Every-prompt hook", status: "skip", detail: "Claude Code not detected" });
|
|
220
244
|
}
|
|
245
|
+
// Plugin AND install: Claude Code then starts our MCP server twice (its
|
|
246
|
+
// instructions and tools load twice) and runs the hook twice.
|
|
247
|
+
const copies = plugin ? claudeInstalledCopies() : [];
|
|
248
|
+
if (copies.length > 0) {
|
|
249
|
+
checks.push({
|
|
250
|
+
label: "Plugin + install",
|
|
251
|
+
status: "warn",
|
|
252
|
+
detail: `the context-doctor plugin is enabled and \`install\` also wired Claude Code (${copies.join(", ")}), so its instructions and tools load twice. ` +
|
|
253
|
+
"Keep one: `context-doctor uninstall` removes the installed copy (then `context-doctor install` re-wires other apps and skips Claude Code while the plugin is on), or disable the plugin",
|
|
254
|
+
});
|
|
255
|
+
}
|
|
221
256
|
// Codex (OpenAI): MCP table in config.toml, hook in hooks.json, skill.
|
|
222
257
|
const codexHome = join(homedir(), ".codex");
|
|
223
258
|
if (existsSync(codexHome)) {
|
|
@@ -225,18 +260,22 @@ export async function runDoctor() {
|
|
|
225
260
|
// Parsed rather than searched for one exact header: a quoted key or a
|
|
226
261
|
// trailing comment is the same table to Codex.
|
|
227
262
|
const codexEntry = codexMcpEntry(toml);
|
|
228
|
-
|
|
229
|
-
|
|
263
|
+
// An absent or empty config.toml is "not installed", as for every other app.
|
|
264
|
+
const codexMcp = !codexEntry
|
|
265
|
+
? toml.trim() === ""
|
|
266
|
+
? { label: "Codex MCP", status: "skip", detail: NOT_INSTALLED }
|
|
267
|
+
: { label: "Codex MCP", status: "fail", detail: "Codex detected but not wired — run: context-doctor install" }
|
|
230
268
|
: launcherNamesMissingPackage(codexEntry)
|
|
231
269
|
? missingPackageCheck("Codex MCP")
|
|
232
|
-
: { label: "Codex MCP", status: "ok", detail: "wired in ~/.codex/config.toml" }
|
|
270
|
+
: { label: "Codex MCP", status: "ok", detail: "wired in ~/.codex/config.toml" };
|
|
271
|
+
checks.push(codexMcp);
|
|
233
272
|
try {
|
|
234
273
|
const hooksPath = join(codexHome, "hooks.json");
|
|
235
|
-
const hooks =
|
|
274
|
+
const hooks = readConfig(hooksPath) ?? {};
|
|
236
275
|
const entries = hooks.hooks?.UserPromptSubmit ?? [];
|
|
237
276
|
const ours = entries.map((e) => e.hooks?.[0]?.command ?? "").find((c) => /context-doctor|cli\.js"?\s+hook/.test(c));
|
|
238
277
|
if (!ours) {
|
|
239
|
-
checks.push(
|
|
278
|
+
checks.push(missingHook("Codex hook", codexMcp));
|
|
240
279
|
}
|
|
241
280
|
else {
|
|
242
281
|
const missing = hookBinaryMissing(ours);
|
|
@@ -256,18 +295,22 @@ export async function runDoctor() {
|
|
|
256
295
|
if (geminiCliPresent()) {
|
|
257
296
|
try {
|
|
258
297
|
const path = geminiSettingsPath();
|
|
259
|
-
const
|
|
298
|
+
const read = readConfig(path);
|
|
299
|
+
const settings = read ?? {};
|
|
260
300
|
const geminiEntry = settings.mcpServers?.["context-doctor"];
|
|
261
|
-
|
|
262
|
-
?
|
|
301
|
+
const geminiMcp = !geminiEntry
|
|
302
|
+
? read === undefined
|
|
303
|
+
? { label: "Gemini CLI MCP", status: "skip", detail: NOT_INSTALLED }
|
|
304
|
+
: { label: "Gemini CLI MCP", status: "fail", detail: "Gemini CLI detected but not wired — run: context-doctor install" }
|
|
263
305
|
: launcherNamesMissingPackage(geminiEntry)
|
|
264
306
|
? missingPackageCheck("Gemini CLI MCP")
|
|
265
|
-
: { label: "Gemini CLI MCP", status: "ok", detail: "wired in ~/.gemini/settings.json" }
|
|
307
|
+
: { label: "Gemini CLI MCP", status: "ok", detail: "wired in ~/.gemini/settings.json" };
|
|
308
|
+
checks.push(geminiMcp);
|
|
266
309
|
const entries = settings.hooks?.BeforeAgent ?? [];
|
|
267
310
|
const ours = entries.map((e) => e.hooks?.[0]?.command ?? "").find((c) => /context-doctor|cli\.js"?\s+hook/.test(c));
|
|
268
311
|
const missing = ours ? hookBinaryMissing(ours) : undefined;
|
|
269
312
|
checks.push(!ours
|
|
270
|
-
?
|
|
313
|
+
? missingHook("Gemini CLI hook", geminiMcp)
|
|
271
314
|
: missing
|
|
272
315
|
? { label: "Gemini CLI hook", status: "fail", detail: `registered, but ${missing} no longer exists — re-run: context-doctor install` }
|
|
273
316
|
: settings.hooksConfig?.enabled === false
|
|
@@ -286,11 +329,11 @@ export async function runDoctor() {
|
|
|
286
329
|
if (existsSync(join(homedir(), ".cursor"))) {
|
|
287
330
|
const hooksPath = join(homedir(), ".cursor", "hooks.json");
|
|
288
331
|
try {
|
|
289
|
-
const hooks =
|
|
332
|
+
const hooks = readConfig(hooksPath, ["version"]) ?? {};
|
|
290
333
|
const ours = (hooks.hooks?.postToolUse ?? []).map((e) => e.command ?? "").find((c) => /(cli\.js|context-doctor(\.cmd|\.exe|\.bat)?)"?\s+hook\s*$/.test(c));
|
|
291
334
|
const missing = ours ? hookBinaryMissing(ours) : undefined;
|
|
292
335
|
checks.push(!ours
|
|
293
|
-
?
|
|
336
|
+
? missingHook("Cursor hook", cursorMcp)
|
|
294
337
|
: missing
|
|
295
338
|
? { label: "Cursor hook", status: "fail", detail: `registered, but ${missing} no longer exists — re-run: context-doctor install` }
|
|
296
339
|
: { label: "Cursor hook", status: "ok", detail: "registered in ~/.cursor/hooks.json (after each tool call in heavy chats)" });
|
|
@@ -328,10 +371,14 @@ export async function runDoctor() {
|
|
|
328
371
|
// Ledger writable. Tested without writing an entry: recordLedger swallows
|
|
329
372
|
// errors (so this could never fail), and the entry it wrote counted as a
|
|
330
373
|
// hook check in every report.
|
|
374
|
+
// Nor by creating its directory: doctor making ~/.claude on a machine
|
|
375
|
+
// without Claude Code made the next install and doctor see Claude Code.
|
|
331
376
|
try {
|
|
332
377
|
const lp = ledgerPath();
|
|
333
|
-
|
|
334
|
-
|
|
378
|
+
let dir = dirname(lp);
|
|
379
|
+
while (!existsSync(dir) && dirname(dir) !== dir)
|
|
380
|
+
dir = dirname(dir);
|
|
381
|
+
accessSync(existsSync(lp) ? lp : dir, fsConstants.W_OK);
|
|
335
382
|
checks.push({ label: "Ledger", status: "ok", detail: `writable at ${lp}` });
|
|
336
383
|
}
|
|
337
384
|
catch {
|
|
@@ -423,11 +470,14 @@ export async function runDoctor() {
|
|
|
423
470
|
const mark = { ok: "✓", warn: "!", fail: "✗", skip: "–" };
|
|
424
471
|
console.log("CONTEXT DOCTOR — self-check");
|
|
425
472
|
console.log("═".repeat(56));
|
|
473
|
+
// Fix hints name `context-doctor <command>`; after an npx run, `npx context-doctor <command>`.
|
|
426
474
|
for (const c of checks) {
|
|
427
|
-
console.log(`${mark[c.status]} ${c.label.padEnd(22)} ${c.detail}`);
|
|
475
|
+
console.log(withInvocation(`${mark[c.status]} ${c.label.padEnd(22)} ${c.detail}`));
|
|
428
476
|
}
|
|
429
477
|
const fails = checks.filter((c) => c.status === "fail");
|
|
430
478
|
const warns = checks.filter((c) => c.status === "warn");
|
|
479
|
+
if (fails.length > 0)
|
|
480
|
+
process.exitCode = 1;
|
|
431
481
|
console.log("");
|
|
432
482
|
console.log(fails.length > 0
|
|
433
483
|
? `${fails.length} issue(s) found — fixes suggested above.`
|
package/dist/install.d.ts
CHANGED
|
@@ -38,6 +38,31 @@ export declare function launcherNamesMissingPackage(entry: {
|
|
|
38
38
|
command?: unknown;
|
|
39
39
|
args?: unknown;
|
|
40
40
|
} | undefined): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* The Claude Code plugin's key in enabledPlugins: "<plugin>@<marketplace>".
|
|
43
|
+
* Our marketplace (.claude-plugin/marketplace.json) is named context-doctor
|
|
44
|
+
* and lists one plugin, context-doctor, so the official key is
|
|
45
|
+
* "context-doctor@context-doctor"; a copy listed in another marketplace keeps
|
|
46
|
+
* the plugin name, so any "context-doctor@…" key is ours.
|
|
47
|
+
*/
|
|
48
|
+
export declare const PLUGIN_KEY = "context-doctor@context-doctor";
|
|
49
|
+
export declare function isOurPluginKey(key: string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Is the context-doctor plugin switched on in the user's Claude Code
|
|
52
|
+
* settings? The plugin brings its own MCP server, skills and hooks, so
|
|
53
|
+
* `install` adding them as well makes Claude Code load the server's
|
|
54
|
+
* instructions and tools twice.
|
|
55
|
+
*/
|
|
56
|
+
export declare function claudePluginEnabled(home?: string): boolean;
|
|
57
|
+
/** What `install` put into Claude Code that the plugin also provides, by name. */
|
|
58
|
+
export declare function claudeInstalledCopies(home?: string): string[];
|
|
59
|
+
/**
|
|
60
|
+
* A config that configures nothing: `{}`, or only empty `mcpServers` /
|
|
61
|
+
* `hooks` containers (plus `extraKeys`, e.g. the `version` install writes
|
|
62
|
+
* into Cursor's hooks.json). It is what install's own writes leave once our
|
|
63
|
+
* entries are gone, and to an app it is the same as no file at all.
|
|
64
|
+
*/
|
|
65
|
+
export declare function isEmptyStub(config: unknown, extraKeys?: string[]): boolean;
|
|
41
66
|
export declare function readJson(path: string): Record<string, any>;
|
|
42
67
|
/** Copy `path` to its .context-doctor.backup, once per run. */
|
|
43
68
|
export declare function backupOnce(path: string): void;
|
package/dist/install.js
CHANGED
|
@@ -6,10 +6,11 @@
|
|
|
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 { existsSync, mkdirSync, readFileSync, writeFileSync, copyFileSync, rmSync } from "node:fs";
|
|
9
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync, copyFileSync, rmSync } from "node:fs";
|
|
10
10
|
import { homedir, platform } from "node:os";
|
|
11
11
|
import { delimiter, dirname, join, sep } from "node:path";
|
|
12
12
|
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { selfCommand, withInvocation } from "./invocation.js";
|
|
13
14
|
function claudeDesktopConfigPath() {
|
|
14
15
|
switch (platform()) {
|
|
15
16
|
case "darwin": return join(homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json");
|
|
@@ -98,6 +99,62 @@ export function launcherNamesMissingPackage(entry) {
|
|
|
98
99
|
return false;
|
|
99
100
|
return rest.find((w) => !w.startsWith("-")) === "context-doctor-mcp";
|
|
100
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* The Claude Code plugin's key in enabledPlugins: "<plugin>@<marketplace>".
|
|
104
|
+
* Our marketplace (.claude-plugin/marketplace.json) is named context-doctor
|
|
105
|
+
* and lists one plugin, context-doctor, so the official key is
|
|
106
|
+
* "context-doctor@context-doctor"; a copy listed in another marketplace keeps
|
|
107
|
+
* the plugin name, so any "context-doctor@…" key is ours.
|
|
108
|
+
*/
|
|
109
|
+
export const PLUGIN_KEY = "context-doctor@context-doctor";
|
|
110
|
+
export function isOurPluginKey(key) {
|
|
111
|
+
return key === PLUGIN_KEY || key.startsWith("context-doctor@");
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Is the context-doctor plugin switched on in the user's Claude Code
|
|
115
|
+
* settings? The plugin brings its own MCP server, skills and hooks, so
|
|
116
|
+
* `install` adding them as well makes Claude Code load the server's
|
|
117
|
+
* instructions and tools twice.
|
|
118
|
+
*/
|
|
119
|
+
export function claudePluginEnabled(home = homedir()) {
|
|
120
|
+
try {
|
|
121
|
+
const plugins = readJson(join(home, ".claude", "settings.json")).enabledPlugins ?? {};
|
|
122
|
+
return Object.entries(plugins).some(([key, on]) => isOurPluginKey(key) && on === true);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return false; // an unreadable settings.json is reported by the hook step
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** What `install` put into Claude Code that the plugin also provides, by name. */
|
|
129
|
+
export function claudeInstalledCopies(home = homedir()) {
|
|
130
|
+
const found = [];
|
|
131
|
+
try {
|
|
132
|
+
if (readJson(join(home, ".claude.json")).mcpServers?.["context-doctor"])
|
|
133
|
+
found.push("MCP server in ~/.claude.json");
|
|
134
|
+
}
|
|
135
|
+
catch { /* unreadable: not ours to judge here */ }
|
|
136
|
+
try {
|
|
137
|
+
const hooks = readJson(join(home, ".claude", "settings.json")).hooks ?? {};
|
|
138
|
+
if (["UserPromptSubmit", "PreCompact"].some((ev) => (hooks[ev] ?? []).some?.(isOurHookEntry)))
|
|
139
|
+
found.push("hooks in ~/.claude/settings.json");
|
|
140
|
+
}
|
|
141
|
+
catch { /* unreadable */ }
|
|
142
|
+
if (existsSync(join(home, ".claude", "skills", "context-doctor", "SKILL.md")))
|
|
143
|
+
found.push("skill in ~/.claude/skills/context-doctor");
|
|
144
|
+
return found;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* A config that configures nothing: `{}`, or only empty `mcpServers` /
|
|
148
|
+
* `hooks` containers (plus `extraKeys`, e.g. the `version` install writes
|
|
149
|
+
* into Cursor's hooks.json). It is what install's own writes leave once our
|
|
150
|
+
* entries are gone, and to an app it is the same as no file at all.
|
|
151
|
+
*/
|
|
152
|
+
export function isEmptyStub(config, extraKeys = []) {
|
|
153
|
+
if (!config || typeof config !== "object" || Array.isArray(config))
|
|
154
|
+
return false;
|
|
155
|
+
const empty = (v) => v !== null && typeof v === "object" && Object.keys(v).length === 0;
|
|
156
|
+
return Object.entries(config).every(([k, v]) => ((k === "mcpServers" || k === "hooks") && empty(v)) || extraKeys.includes(k));
|
|
157
|
+
}
|
|
101
158
|
export function readJson(path) {
|
|
102
159
|
if (!existsSync(path))
|
|
103
160
|
return {};
|
|
@@ -218,8 +275,8 @@ function uninstallStatusLine() {
|
|
|
218
275
|
const current = settings.statusLine?.command;
|
|
219
276
|
if (current && isOurStatusLine(current)) {
|
|
220
277
|
delete settings.statusLine;
|
|
221
|
-
|
|
222
|
-
|
|
278
|
+
writeUninstalled(settingsPath, settings);
|
|
279
|
+
done("✓ Claude Code status line removed");
|
|
223
280
|
}
|
|
224
281
|
}
|
|
225
282
|
/** True when the hook had to fall back to npx — worth telling the user. */
|
|
@@ -340,8 +397,8 @@ function uninstallCursorHook() {
|
|
|
340
397
|
delete config.hooks.postToolUse;
|
|
341
398
|
else
|
|
342
399
|
config.hooks.postToolUse = filtered;
|
|
343
|
-
|
|
344
|
-
|
|
400
|
+
writeUninstalled(path, config, ["version"]);
|
|
401
|
+
done("✓ Cursor after-tool-call hook removed");
|
|
345
402
|
}
|
|
346
403
|
function uninstallHookFrom(settingsPath, label, event = "UserPromptSubmit") {
|
|
347
404
|
if (!existsSync(settingsPath))
|
|
@@ -355,8 +412,8 @@ function uninstallHookFrom(settingsPath, label, event = "UserPromptSubmit") {
|
|
|
355
412
|
settings.hooks[event] = filtered;
|
|
356
413
|
if (filtered.length === 0)
|
|
357
414
|
delete settings.hooks[event];
|
|
358
|
-
|
|
359
|
-
|
|
415
|
+
writeUninstalled(settingsPath, settings);
|
|
416
|
+
done(`✓ ${label} removed`);
|
|
360
417
|
}
|
|
361
418
|
}
|
|
362
419
|
function installSkill() {
|
|
@@ -559,20 +616,31 @@ function uninstallCodex() {
|
|
|
559
616
|
const current = readFileSync(configPath, "utf8");
|
|
560
617
|
const next = removeCodexMcpTable(current);
|
|
561
618
|
if (next !== current) {
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
619
|
+
// A config.toml install created and that now holds nothing goes too.
|
|
620
|
+
const backup = configPath + BACKUP_SUFFIX;
|
|
621
|
+
let created = !existsSync(backup);
|
|
622
|
+
if (!created) {
|
|
623
|
+
try {
|
|
624
|
+
const before = readFileSync(backup, "utf8");
|
|
625
|
+
created = before !== removeCodexMcpTable(before) && removeCodexMcpTable(before).trim() === "";
|
|
626
|
+
}
|
|
627
|
+
catch { /* unreadable backup: the file was the user's */ }
|
|
628
|
+
}
|
|
629
|
+
if (created && next.trim() === "") {
|
|
630
|
+
rmSync(configPath);
|
|
631
|
+
deletedStubs.push(configPath);
|
|
632
|
+
}
|
|
633
|
+
else {
|
|
634
|
+
writeFileSync(configPath, next);
|
|
635
|
+
}
|
|
636
|
+
done("✓ Codex: MCP server removed");
|
|
565
637
|
}
|
|
566
638
|
}
|
|
567
639
|
}
|
|
568
640
|
catch (e) {
|
|
569
641
|
console.error(`✗ Codex MCP: ${e.message}`);
|
|
570
642
|
}
|
|
571
|
-
|
|
572
|
-
if (existsSync(skillDir)) {
|
|
573
|
-
rmSync(skillDir, { recursive: true });
|
|
574
|
-
console.log("✓ Codex skill removed");
|
|
575
|
-
}
|
|
643
|
+
removeSkill(join(dir, "skills", "context-doctor"), "Codex skill");
|
|
576
644
|
}
|
|
577
645
|
/**
|
|
578
646
|
* Install into every detected app.
|
|
@@ -594,7 +662,22 @@ export function runInstall(options = {}) {
|
|
|
594
662
|
return { failures: [] };
|
|
595
663
|
}
|
|
596
664
|
const failures = [];
|
|
665
|
+
// The plugin already gives Claude Code the server, skill and hooks; a
|
|
666
|
+
// second copy of each makes every session load the instructions and the
|
|
667
|
+
// tools twice. Claude Code is skipped; the other apps are not.
|
|
668
|
+
const viaPlugin = claudePluginEnabled();
|
|
669
|
+
if (viaPlugin) {
|
|
670
|
+
console.log("– Claude Code: skipped the MCP server, skill and hooks. The context-doctor plugin is enabled and");
|
|
671
|
+
console.log(" provides them already; installing them as well would load its instructions and tools twice.");
|
|
672
|
+
const copies = claudeInstalledCopies();
|
|
673
|
+
if (copies.length > 0) {
|
|
674
|
+
console.log(` An earlier install is still in place (${copies.join(", ")}): \`${selfCommand("uninstall")}\``);
|
|
675
|
+
console.log(` removes it, then \`${selfCommand("install")}\` wires the other apps again.`);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
597
678
|
for (const target of found) {
|
|
679
|
+
if (viaPlugin && target.name === "Claude Code")
|
|
680
|
+
continue;
|
|
598
681
|
try {
|
|
599
682
|
const config = readJson(target.configPath);
|
|
600
683
|
config.mcpServers = config.mcpServers ?? {};
|
|
@@ -609,7 +692,7 @@ export function runInstall(options = {}) {
|
|
|
609
692
|
}
|
|
610
693
|
}
|
|
611
694
|
try {
|
|
612
|
-
const skillPath = installSkill();
|
|
695
|
+
const skillPath = viaPlugin ? null : installSkill();
|
|
613
696
|
if (skillPath)
|
|
614
697
|
console.log(`✓ Agent Skill installed for Claude Code (${skillPath})`);
|
|
615
698
|
}
|
|
@@ -618,7 +701,7 @@ export function runInstall(options = {}) {
|
|
|
618
701
|
failures.push("Agent Skill");
|
|
619
702
|
}
|
|
620
703
|
try {
|
|
621
|
-
const hookPath = installHook();
|
|
704
|
+
const hookPath = viaPlugin ? null : installHook();
|
|
622
705
|
if (hookPath) {
|
|
623
706
|
console.log(`✓ Claude Code every-prompt hook installed (${hookPath}) — heavy sessions get automatic hygiene guidance`);
|
|
624
707
|
// npx resolves the package on every single prompt; a global install makes
|
|
@@ -684,9 +767,9 @@ export function runInstall(options = {}) {
|
|
|
684
767
|
else {
|
|
685
768
|
console.log("\nDone. Restart the apps to pick up the new tools, then try:");
|
|
686
769
|
console.log(' "What\'s eating my context?" — or paste a conversation and ask for a profile.');
|
|
687
|
-
console.log(
|
|
770
|
+
console.log(`See what it is worth on your own sessions: ${selfCommand("savings")}`);
|
|
688
771
|
console.log("Chat apps (Claude Desktop, claude.ai, ChatGPT, phones) have no hook. To make the rules standing there:");
|
|
689
|
-
console.log("
|
|
772
|
+
console.log(` ${selfCommand("instructions --copy")} then paste into the app's preferences (it tells you where).`);
|
|
690
773
|
}
|
|
691
774
|
return { failures };
|
|
692
775
|
}
|
|
@@ -721,7 +804,7 @@ async function uninstallAutopilot() {
|
|
|
721
804
|
console.log(line);
|
|
722
805
|
}
|
|
723
806
|
catch (e) {
|
|
724
|
-
console.error(`✗ Autopilot: ${e.message} (run: context-doctor autopilot off)`);
|
|
807
|
+
console.error(withInvocation(`✗ Autopilot: ${e.message} (run: context-doctor autopilot off)`));
|
|
725
808
|
return;
|
|
726
809
|
}
|
|
727
810
|
// `off` restores only what a readable config describes. A damaged one
|
|
@@ -740,6 +823,112 @@ async function uninstallAutopilot() {
|
|
|
740
823
|
console.error(`✗ Autopilot: ${e.message}`);
|
|
741
824
|
}
|
|
742
825
|
}
|
|
826
|
+
// -- Uninstall: take out exactly what install put in.
|
|
827
|
+
const BACKUP_SUFFIX = ".context-doctor.backup";
|
|
828
|
+
/** What this uninstall run changed, and the stub files it deleted. */
|
|
829
|
+
let changes = 0;
|
|
830
|
+
let deletedStubs = [];
|
|
831
|
+
/** Print one thing uninstall did, and count it. */
|
|
832
|
+
function done(line) {
|
|
833
|
+
changes++;
|
|
834
|
+
console.log(line);
|
|
835
|
+
}
|
|
836
|
+
/** A JSON config with our entries taken out, the way uninstall takes them out. */
|
|
837
|
+
function withoutOurs(config) {
|
|
838
|
+
const c = structuredClone(config);
|
|
839
|
+
if (c.mcpServers && typeof c.mcpServers === "object")
|
|
840
|
+
delete c.mcpServers["context-doctor"];
|
|
841
|
+
if (c.hooks && typeof c.hooks === "object") {
|
|
842
|
+
for (const [event, list] of Object.entries(c.hooks)) {
|
|
843
|
+
if (!Array.isArray(list))
|
|
844
|
+
continue;
|
|
845
|
+
const kept = list.filter((e) => !isOurHookEntry(e) && !isOurCursorEntry(e));
|
|
846
|
+
if (kept.length > 0)
|
|
847
|
+
c.hooks[event] = kept;
|
|
848
|
+
else
|
|
849
|
+
delete c.hooks[event];
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
if (typeof c.statusLine?.command === "string" && isOurStatusLine(c.statusLine.command))
|
|
853
|
+
delete c.statusLine;
|
|
854
|
+
return c;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* The file as it was before install, as far as its backup can tell:
|
|
858
|
+
* `created` when install made the file (no backup was needed), or when the
|
|
859
|
+
* backup is only an earlier install's own write (install run twice);
|
|
860
|
+
* `before` is the backup without our entries.
|
|
861
|
+
*/
|
|
862
|
+
function installOrigin(path, extraKeys) {
|
|
863
|
+
const backup = path + BACKUP_SUFFIX;
|
|
864
|
+
if (!existsSync(backup))
|
|
865
|
+
return { created: true, before: {} };
|
|
866
|
+
try {
|
|
867
|
+
const raw = JSON.parse(readFileSync(backup, "utf8"));
|
|
868
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
869
|
+
return { created: false, before: {} };
|
|
870
|
+
const before = withoutOurs(raw);
|
|
871
|
+
const hadOurs = JSON.stringify(before) !== JSON.stringify(raw);
|
|
872
|
+
return { created: hadOurs && isEmptyStub(before, extraKeys), before };
|
|
873
|
+
}
|
|
874
|
+
catch {
|
|
875
|
+
return { created: false, before: {} }; // unreadable backup: the file was the user's
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* Write a config with our entries removed. Containers install added
|
|
880
|
+
* (`mcpServers`, `hooks`, Cursor's `version`) go once empty, unless the file
|
|
881
|
+
* had them before install; a file install created that now configures
|
|
882
|
+
* nothing is deleted rather than left as `{"mcpServers":{}}`. No backup is
|
|
883
|
+
* written: the one install made of the user's original is kept as it is.
|
|
884
|
+
*/
|
|
885
|
+
function writeUninstalled(path, config, extraKeys = []) {
|
|
886
|
+
const { created, before } = installOrigin(path, extraKeys);
|
|
887
|
+
for (const key of ["mcpServers", "hooks"]) {
|
|
888
|
+
const v = config[key];
|
|
889
|
+
if (v && typeof v === "object" && Object.keys(v).length === 0 && !(key in before))
|
|
890
|
+
delete config[key];
|
|
891
|
+
}
|
|
892
|
+
if (!("hooks" in config))
|
|
893
|
+
for (const key of extraKeys)
|
|
894
|
+
if (!(key in before))
|
|
895
|
+
delete config[key];
|
|
896
|
+
if (created && isEmptyStub(config, extraKeys)) {
|
|
897
|
+
rmSync(path);
|
|
898
|
+
deletedStubs.push(path);
|
|
899
|
+
return;
|
|
900
|
+
}
|
|
901
|
+
writeFileSync(path, JSON.stringify(config, null, 2));
|
|
902
|
+
}
|
|
903
|
+
/** Remove the SKILL.md install copied, and its folder once nothing else is in it. */
|
|
904
|
+
function removeSkill(dir, label) {
|
|
905
|
+
const file = join(dir, "SKILL.md");
|
|
906
|
+
if (!existsSync(file))
|
|
907
|
+
return;
|
|
908
|
+
rmSync(file);
|
|
909
|
+
try {
|
|
910
|
+
if (readdirSync(dir).length === 0)
|
|
911
|
+
rmSync(dir, { recursive: true });
|
|
912
|
+
}
|
|
913
|
+
catch { /* already gone */ }
|
|
914
|
+
done(`✓ ${label} removed`);
|
|
915
|
+
}
|
|
916
|
+
/** Every config file install or autopilot may write. */
|
|
917
|
+
function configPaths() {
|
|
918
|
+
return [
|
|
919
|
+
...targets().map((t) => t.configPath),
|
|
920
|
+
join(homedir(), ".claude", "settings.json"),
|
|
921
|
+
cursorHooksPath(),
|
|
922
|
+
join(codexDir(), "config.toml"),
|
|
923
|
+
join(codexDir(), "hooks.json"),
|
|
924
|
+
geminiSettingsPath(),
|
|
925
|
+
].filter((p, i, all) => all.indexOf(p) === i);
|
|
926
|
+
}
|
|
927
|
+
/** "~/..." for a path under the home directory. */
|
|
928
|
+
function tilde(p) {
|
|
929
|
+
const home = homedir();
|
|
930
|
+
return p.startsWith(home) ? "~" + p.slice(home.length) : p;
|
|
931
|
+
}
|
|
743
932
|
/** Everything of ours under ~/.claude besides configs: state, ledger, autopilot bookkeeping. */
|
|
744
933
|
const OUR_CLAUDE_FILES = [
|
|
745
934
|
".context-doctor-hook-state.json",
|
|
@@ -752,8 +941,18 @@ const OUR_CLAUDE_FILES = [
|
|
|
752
941
|
".context-doctor-proxy-restart",
|
|
753
942
|
];
|
|
754
943
|
export async function runUninstall() {
|
|
944
|
+
changes = 0;
|
|
945
|
+
deletedStubs = [];
|
|
946
|
+
// Uninstall writes no backups: each .context-doctor.backup already holds
|
|
947
|
+
// the user's file from before install, and overwriting it with the
|
|
948
|
+
// installed state (as every write here used to) lost that original.
|
|
949
|
+
for (const p of configPaths())
|
|
950
|
+
backedUp.add(p);
|
|
755
951
|
// First: if anything below fails, Claude Code must still reach the API.
|
|
952
|
+
const hadAutopilot = existsSync(join(homedir(), ".claude", ".context-doctor-autopilot.json"));
|
|
756
953
|
await uninstallAutopilot();
|
|
954
|
+
if (hadAutopilot)
|
|
955
|
+
changes++;
|
|
757
956
|
for (const target of targets().filter((t) => t.detect())) {
|
|
758
957
|
try {
|
|
759
958
|
if (!existsSync(target.configPath))
|
|
@@ -761,19 +960,15 @@ export async function runUninstall() {
|
|
|
761
960
|
const config = readJson(target.configPath);
|
|
762
961
|
if (config.mcpServers?.["context-doctor"]) {
|
|
763
962
|
delete config.mcpServers["context-doctor"];
|
|
764
|
-
|
|
765
|
-
|
|
963
|
+
writeUninstalled(target.configPath, config);
|
|
964
|
+
done(`✓ ${target.name}: MCP server removed`);
|
|
766
965
|
}
|
|
767
966
|
}
|
|
768
967
|
catch (e) {
|
|
769
968
|
console.error(`✗ ${target.name}: ${e.message}`);
|
|
770
969
|
}
|
|
771
970
|
}
|
|
772
|
-
|
|
773
|
-
if (existsSync(skillDir)) {
|
|
774
|
-
rmSync(skillDir, { recursive: true });
|
|
775
|
-
console.log("✓ Agent Skill removed");
|
|
776
|
-
}
|
|
971
|
+
removeSkill(join(homedir(), ".claude", "skills", "context-doctor"), "Agent Skill");
|
|
777
972
|
uninstallHook();
|
|
778
973
|
uninstallStatusLine();
|
|
779
974
|
uninstallCodex();
|
|
@@ -783,8 +978,21 @@ export async function runUninstall() {
|
|
|
783
978
|
const p = join(homedir(), ".claude", file);
|
|
784
979
|
if (existsSync(p)) {
|
|
785
980
|
rmSync(p, { recursive: true, force: true });
|
|
786
|
-
|
|
981
|
+
done(`✓ Removed ${file}`);
|
|
787
982
|
}
|
|
788
983
|
}
|
|
789
|
-
|
|
984
|
+
if (deletedStubs.length > 0) {
|
|
985
|
+
console.log(`✓ Deleted ${deletedStubs.length === 1 ? "a file" : "files"} install had created, now empty: ${deletedStubs.map(tilde).join(", ")}`);
|
|
986
|
+
}
|
|
987
|
+
const backups = configPaths().map((p) => p + BACKUP_SUFFIX).filter((b) => existsSync(b));
|
|
988
|
+
if (changes === 0)
|
|
989
|
+
console.log("Nothing of context-doctor's was installed.");
|
|
990
|
+
else
|
|
991
|
+
console.log("Done.");
|
|
992
|
+
if (backups.length > 0) {
|
|
993
|
+
console.log("Backups install made of your configs, from before it changed them, are kept:");
|
|
994
|
+
for (const b of backups)
|
|
995
|
+
console.log(` ${tilde(b)}`);
|
|
996
|
+
console.log("Delete them once you no longer need them.");
|
|
997
|
+
}
|
|
790
998
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the user started this process, so the next steps we print can be
|
|
3
|
+
* typed as they are. Someone who ran `npx context-doctor savings` has no
|
|
4
|
+
* `context-doctor` on PATH: a hint saying `context-doctor autopilot on` is
|
|
5
|
+
* "command not found" for them.
|
|
6
|
+
*/
|
|
7
|
+
/** Started through npx (or `npm exec`): npm says so, or the script sits in npx's cache. */
|
|
8
|
+
export declare function launchedViaNpx(env?: NodeJS.ProcessEnv, script?: string): boolean;
|
|
9
|
+
/** Should follow-up commands be spelled `npx context-doctor …`? */
|
|
10
|
+
export declare function needsNpx(env?: NodeJS.ProcessEnv, script?: string): boolean;
|
|
11
|
+
/** A follow-up command as the user should type it: `npx context-doctor <rest>` after an npx run. */
|
|
12
|
+
export declare function selfCommand(rest: string, env?: NodeJS.ProcessEnv, script?: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Rewrite the `context-doctor <command>` hints in printed text for the way
|
|
15
|
+
* this process was started. Autopilot runs as a background service, which
|
|
16
|
+
* cannot point into npx's cache, so from npx its hint is a global install.
|
|
17
|
+
*/
|
|
18
|
+
export declare function withInvocation(text: string, env?: NodeJS.ProcessEnv, script?: string): string;
|