@mxalbert/context-mode 2.0.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/.claude-plugin/marketplace.json +33 -0
- package/.claude-plugin/plugin.json +31 -0
- package/.codex-plugin/hooks.json +65 -0
- package/.codex-plugin/mcp.json +13 -0
- package/.codex-plugin/plugin.json +31 -0
- package/.openclaw-plugin/index.ts +12 -0
- package/.openclaw-plugin/openclaw.plugin.json +38 -0
- package/.openclaw-plugin/package.json +28 -0
- package/LICENSE +94 -0
- package/README.md +1619 -0
- package/bin/statusline.mjs +378 -0
- package/build/adapters/antigravity/index.d.ts +51 -0
- package/build/adapters/antigravity/index.js +169 -0
- package/build/adapters/antigravity-cli/index.d.ts +51 -0
- package/build/adapters/antigravity-cli/index.js +342 -0
- package/build/adapters/base.d.ts +92 -0
- package/build/adapters/base.js +135 -0
- package/build/adapters/claude-code/config.d.ts +8 -0
- package/build/adapters/claude-code/config.js +8 -0
- package/build/adapters/claude-code/hooks.d.ts +106 -0
- package/build/adapters/claude-code/hooks.js +183 -0
- package/build/adapters/claude-code/index.d.ts +80 -0
- package/build/adapters/claude-code/index.js +569 -0
- package/build/adapters/claude-code-base.d.ts +49 -0
- package/build/adapters/claude-code-base.js +113 -0
- package/build/adapters/client-map.d.ts +10 -0
- package/build/adapters/client-map.js +42 -0
- package/build/adapters/codex/config.d.ts +8 -0
- package/build/adapters/codex/config.js +8 -0
- package/build/adapters/codex/hooks.d.ts +55 -0
- package/build/adapters/codex/hooks.js +64 -0
- package/build/adapters/codex/index.d.ts +87 -0
- package/build/adapters/codex/index.js +995 -0
- package/build/adapters/codex/paths.d.ts +1 -0
- package/build/adapters/codex/paths.js +12 -0
- package/build/adapters/codex/usage.d.ts +107 -0
- package/build/adapters/codex/usage.js +227 -0
- package/build/adapters/copilot-base.d.ts +94 -0
- package/build/adapters/copilot-base.js +297 -0
- package/build/adapters/copilot-cli/hooks.d.ts +33 -0
- package/build/adapters/copilot-cli/hooks.js +64 -0
- package/build/adapters/copilot-cli/index.d.ts +48 -0
- package/build/adapters/copilot-cli/index.js +341 -0
- package/build/adapters/cursor/config.d.ts +4 -0
- package/build/adapters/cursor/config.js +4 -0
- package/build/adapters/cursor/hooks.d.ts +55 -0
- package/build/adapters/cursor/hooks.js +82 -0
- package/build/adapters/cursor/index.d.ts +64 -0
- package/build/adapters/cursor/index.js +517 -0
- package/build/adapters/detect.d.ts +136 -0
- package/build/adapters/detect.js +623 -0
- package/build/adapters/gemini-cli/config.d.ts +8 -0
- package/build/adapters/gemini-cli/config.js +8 -0
- package/build/adapters/gemini-cli/hooks.d.ts +83 -0
- package/build/adapters/gemini-cli/hooks.js +115 -0
- package/build/adapters/gemini-cli/index.d.ts +80 -0
- package/build/adapters/gemini-cli/index.js +483 -0
- package/build/adapters/jetbrains-copilot/config.d.ts +8 -0
- package/build/adapters/jetbrains-copilot/config.js +8 -0
- package/build/adapters/jetbrains-copilot/hooks.d.ts +59 -0
- package/build/adapters/jetbrains-copilot/hooks.js +87 -0
- package/build/adapters/jetbrains-copilot/index.d.ts +31 -0
- package/build/adapters/jetbrains-copilot/index.js +131 -0
- package/build/adapters/kimi/config.d.ts +8 -0
- package/build/adapters/kimi/config.js +8 -0
- package/build/adapters/kimi/hooks.d.ts +28 -0
- package/build/adapters/kimi/hooks.js +34 -0
- package/build/adapters/kimi/index.d.ts +66 -0
- package/build/adapters/kimi/index.js +537 -0
- package/build/adapters/kimi/paths.d.ts +21 -0
- package/build/adapters/kimi/paths.js +52 -0
- package/build/adapters/kimi/usage.d.ts +82 -0
- package/build/adapters/kimi/usage.js +217 -0
- package/build/adapters/kiro/hooks.d.ts +61 -0
- package/build/adapters/kiro/hooks.js +94 -0
- package/build/adapters/kiro/index.d.ts +61 -0
- package/build/adapters/kiro/index.js +301 -0
- package/build/adapters/omp/index.d.ts +65 -0
- package/build/adapters/omp/index.js +182 -0
- package/build/adapters/omp/plugin.d.ts +81 -0
- package/build/adapters/omp/plugin.js +331 -0
- package/build/adapters/omp/usage.d.ts +49 -0
- package/build/adapters/omp/usage.js +110 -0
- package/build/adapters/openclaw/config.d.ts +8 -0
- package/build/adapters/openclaw/config.js +8 -0
- package/build/adapters/openclaw/hooks.d.ts +50 -0
- package/build/adapters/openclaw/hooks.js +61 -0
- package/build/adapters/openclaw/index.d.ts +74 -0
- package/build/adapters/openclaw/index.js +439 -0
- package/build/adapters/openclaw/mcp-tools.d.ts +54 -0
- package/build/adapters/openclaw/mcp-tools.js +207 -0
- package/build/adapters/openclaw/plugin.d.ts +141 -0
- package/build/adapters/openclaw/plugin.js +818 -0
- package/build/adapters/openclaw/session-db.d.ts +55 -0
- package/build/adapters/openclaw/session-db.js +88 -0
- package/build/adapters/openclaw/usage.d.ts +34 -0
- package/build/adapters/openclaw/usage.js +52 -0
- package/build/adapters/openclaw/workspace-router.d.ts +29 -0
- package/build/adapters/openclaw/workspace-router.js +64 -0
- package/build/adapters/opencode/config.d.ts +8 -0
- package/build/adapters/opencode/config.js +8 -0
- package/build/adapters/opencode/hooks.d.ts +38 -0
- package/build/adapters/opencode/hooks.js +50 -0
- package/build/adapters/opencode/index.d.ts +144 -0
- package/build/adapters/opencode/index.js +638 -0
- package/build/adapters/opencode/plugin.d.ts +249 -0
- package/build/adapters/opencode/plugin.js +1536 -0
- package/build/adapters/opencode/v2.d.ts +117 -0
- package/build/adapters/opencode/v2.js +224 -0
- package/build/adapters/opencode/zod3tov4.d.ts +1 -0
- package/build/adapters/opencode/zod3tov4.js +132 -0
- package/build/adapters/pi/extension.d.ts +81 -0
- package/build/adapters/pi/extension.js +861 -0
- package/build/adapters/pi/index.d.ts +57 -0
- package/build/adapters/pi/index.js +173 -0
- package/build/adapters/pi/mcp-bridge.d.ts +268 -0
- package/build/adapters/pi/mcp-bridge.js +894 -0
- package/build/adapters/qwen-code/hooks.d.ts +26 -0
- package/build/adapters/qwen-code/hooks.js +29 -0
- package/build/adapters/qwen-code/index.d.ts +40 -0
- package/build/adapters/qwen-code/index.js +361 -0
- package/build/adapters/qwen-code/usage.d.ts +90 -0
- package/build/adapters/qwen-code/usage.js +222 -0
- package/build/adapters/types.d.ts +360 -0
- package/build/adapters/types.js +118 -0
- package/build/adapters/vscode-copilot/config.d.ts +8 -0
- package/build/adapters/vscode-copilot/config.js +8 -0
- package/build/adapters/vscode-copilot/hooks.d.ts +71 -0
- package/build/adapters/vscode-copilot/hooks.js +98 -0
- package/build/adapters/vscode-copilot/index.d.ts +32 -0
- package/build/adapters/vscode-copilot/index.js +222 -0
- package/build/adapters/zed/index.d.ts +40 -0
- package/build/adapters/zed/index.js +163 -0
- package/build/cli.d.ts +34 -0
- package/build/cli.js +1828 -0
- package/build/db-base.d.ts +250 -0
- package/build/db-base.js +756 -0
- package/build/executor.d.ts +72 -0
- package/build/executor.js +678 -0
- package/build/exit-classify.d.ts +19 -0
- package/build/exit-classify.js +12 -0
- package/build/fetch/blocks.d.ts +145 -0
- package/build/fetch/blocks.js +244 -0
- package/build/fetch/extract.d.ts +68 -0
- package/build/fetch/extract.js +123 -0
- package/build/fetch/page-store.d.ts +66 -0
- package/build/fetch/page-store.js +174 -0
- package/build/fetch-cache.d.ts +13 -0
- package/build/fetch-cache.js +15 -0
- package/build/lifecycle.d.ts +124 -0
- package/build/lifecycle.js +278 -0
- package/build/package-identity.d.ts +26 -0
- package/build/package-identity.js +28 -0
- package/build/runPool.d.ts +36 -0
- package/build/runPool.js +51 -0
- package/build/runtime.d.ts +71 -0
- package/build/runtime.js +659 -0
- package/build/search/auto-memory.d.ts +47 -0
- package/build/search/auto-memory.js +170 -0
- package/build/search/ctx-search-schema.d.ts +90 -0
- package/build/search/ctx-search-schema.js +135 -0
- package/build/search/flood-guard.d.ts +57 -0
- package/build/search/flood-guard.js +80 -0
- package/build/search/unified.d.ts +56 -0
- package/build/search/unified.js +104 -0
- package/build/security.d.ts +206 -0
- package/build/security.js +780 -0
- package/build/server.d.ts +240 -0
- package/build/server.js +5034 -0
- package/build/session/analytics.d.ts +773 -0
- package/build/session/analytics.js +2420 -0
- package/build/session/db.d.ts +468 -0
- package/build/session/db.js +1330 -0
- package/build/session/error-classifier.d.ts +87 -0
- package/build/session/error-classifier.js +303 -0
- package/build/session/event-emit.d.ts +48 -0
- package/build/session/event-emit.js +101 -0
- package/build/session/extract.d.ts +333 -0
- package/build/session/extract.js +2688 -0
- package/build/session/model-prices.json +429 -0
- package/build/session/persist-tool-calls.d.ts +54 -0
- package/build/session/persist-tool-calls.js +105 -0
- package/build/session/pricing.d.ts +64 -0
- package/build/session/pricing.js +151 -0
- package/build/session/project-attribution.d.ts +73 -0
- package/build/session/project-attribution.js +245 -0
- package/build/session/purge.d.ts +138 -0
- package/build/session/purge.js +240 -0
- package/build/session/retrieval-marker.d.ts +39 -0
- package/build/session/retrieval-marker.js +65 -0
- package/build/session/snapshot.d.ts +46 -0
- package/build/session/snapshot.js +532 -0
- package/build/store-directory.d.ts +56 -0
- package/build/store-directory.js +254 -0
- package/build/store.d.ts +147 -0
- package/build/store.js +1714 -0
- package/build/truncate.d.ts +57 -0
- package/build/truncate.js +142 -0
- package/build/types.d.ts +117 -0
- package/build/types.js +20 -0
- package/build/util/claude-config.d.ts +32 -0
- package/build/util/claude-config.js +84 -0
- package/build/util/hook-config.d.ts +27 -0
- package/build/util/hook-config.js +76 -0
- package/build/util/jsonc.d.ts +14 -0
- package/build/util/jsonc.js +104 -0
- package/build/util/plugin-cache-integrity.d.ts +51 -0
- package/build/util/plugin-cache-integrity.js +146 -0
- package/build/util/project-dir.d.ts +149 -0
- package/build/util/project-dir.js +340 -0
- package/build/util/sibling-mcp.d.ts +79 -0
- package/build/util/sibling-mcp.js +181 -0
- package/cli.bundle.mjs +1495 -0
- package/configs/antigravity/GEMINI.md +68 -0
- package/configs/antigravity/mcp_config.json +7 -0
- package/configs/antigravity-cli/hooks/hooks.json +37 -0
- package/configs/antigravity-cli/hooks.json +37 -0
- package/configs/antigravity-cli/mcp_config.json +10 -0
- package/configs/antigravity-cli/plugin.json +14 -0
- package/configs/antigravity-cli/rules/context-mode.md +77 -0
- package/configs/antigravity-cli/skills/context-mode/SKILL.md +77 -0
- package/configs/claude-code/CLAUDE.md +91 -0
- package/configs/codex/AGENTS.md +99 -0
- package/configs/codex/config.toml +8 -0
- package/configs/codex/hooks.json +47 -0
- package/configs/copilot-cli/.github/plugin/plugin.json +23 -0
- package/configs/copilot-cli/.mcp.json +12 -0
- package/configs/copilot-cli/README.md +47 -0
- package/configs/copilot-cli/hooks.json +41 -0
- package/configs/copilot-cli/skills/context-mode/SKILL.md +38 -0
- package/configs/cursor/context-mode.mdc +76 -0
- package/configs/cursor/hooks.json +21 -0
- package/configs/cursor/mcp.json +7 -0
- package/configs/gemini-cli/GEMINI.md +88 -0
- package/configs/gemini-cli/mcp.json +7 -0
- package/configs/gemini-cli/settings.json +60 -0
- package/configs/jetbrains-copilot/copilot-instructions.md +80 -0
- package/configs/jetbrains-copilot/hooks.json +16 -0
- package/configs/jetbrains-copilot/mcp.json +7 -0
- package/configs/kilo/AGENTS.md +84 -0
- package/configs/kilo/kilo.json +6 -0
- package/configs/kimi/hooks.json +54 -0
- package/configs/kiro/KIRO.md +84 -0
- package/configs/kiro/agent.json +18 -0
- package/configs/kiro/mcp.json +7 -0
- package/configs/omp/SYSTEM.md +85 -0
- package/configs/omp/mcp.json +7 -0
- package/configs/openclaw/AGENTS.md +87 -0
- package/configs/openclaw/openclaw.json +13 -0
- package/configs/opencode/AGENTS.md +84 -0
- package/configs/opencode/opencode.json +6 -0
- package/configs/pi/AGENTS.md +3 -0
- package/configs/qwen-code/QWEN.md +91 -0
- package/configs/vscode-copilot/copilot-instructions.md +80 -0
- package/configs/vscode-copilot/hooks.json +16 -0
- package/configs/vscode-copilot/mcp.json +7 -0
- package/configs/zed/AGENTS.md +68 -0
- package/hooks/antigravity-cli/payload.mjs +98 -0
- package/hooks/antigravity-cli/posttooluse.mjs +138 -0
- package/hooks/antigravity-cli/pretooluse.mjs +78 -0
- package/hooks/antigravity-cli/stop.mjs +58 -0
- package/hooks/auto-injection.mjs +102 -0
- package/hooks/cache-heal-utils.mjs +379 -0
- package/hooks/codex/platform.mjs +1 -0
- package/hooks/codex/posttooluse.mjs +66 -0
- package/hooks/codex/precompact.mjs +70 -0
- package/hooks/codex/pretooluse.mjs +42 -0
- package/hooks/codex/sessionstart.mjs +121 -0
- package/hooks/codex/stop.mjs +138 -0
- package/hooks/codex/userpromptsubmit.mjs +75 -0
- package/hooks/copilot-cli/posttooluse.mjs +79 -0
- package/hooks/copilot-cli/precompact.mjs +66 -0
- package/hooks/copilot-cli/pretooluse.mjs +41 -0
- package/hooks/copilot-cli/sessionstart.mjs +121 -0
- package/hooks/copilot-cli/stop.mjs +59 -0
- package/hooks/copilot-cli/userpromptsubmit.mjs +77 -0
- package/hooks/core/codex-caps.mjs +112 -0
- package/hooks/core/formatters.mjs +356 -0
- package/hooks/core/mcp-ready.mjs +109 -0
- package/hooks/core/platform-detect.mjs +49 -0
- package/hooks/core/routing.mjs +1050 -0
- package/hooks/core/stdin.mjs +90 -0
- package/hooks/core/tool-naming.mjs +57 -0
- package/hooks/cursor/afteragentresponse.mjs +74 -0
- package/hooks/cursor/hooks.json +31 -0
- package/hooks/cursor/posttooluse.mjs +72 -0
- package/hooks/cursor/pretooluse.mjs +26 -0
- package/hooks/cursor/sessionstart.mjs +97 -0
- package/hooks/cursor/stop.mjs +48 -0
- package/hooks/ensure-deps.mjs +254 -0
- package/hooks/formatters/claude-code.mjs +113 -0
- package/hooks/formatters/cursor.mjs +37 -0
- package/hooks/formatters/gemini-cli.mjs +55 -0
- package/hooks/formatters/vscode-copilot.mjs +55 -0
- package/hooks/gemini-cli/aftermodel.mjs +70 -0
- package/hooks/gemini-cli/aftertool.mjs +61 -0
- package/hooks/gemini-cli/beforeagent.mjs +99 -0
- package/hooks/gemini-cli/beforetool.mjs +27 -0
- package/hooks/gemini-cli/precompress.mjs +55 -0
- package/hooks/gemini-cli/sessionstart.mjs +137 -0
- package/hooks/heal-partial-install.mjs +712 -0
- package/hooks/hooks.json +143 -0
- package/hooks/jetbrains-copilot/posttooluse.mjs +61 -0
- package/hooks/jetbrains-copilot/precompact.mjs +55 -0
- package/hooks/jetbrains-copilot/pretooluse.mjs +27 -0
- package/hooks/jetbrains-copilot/sessionstart.mjs +124 -0
- package/hooks/kimi/platform.mjs +1 -0
- package/hooks/kimi/posttooluse.mjs +72 -0
- package/hooks/kimi/precompact.mjs +80 -0
- package/hooks/kimi/pretooluse.mjs +42 -0
- package/hooks/kimi/sessionend.mjs +61 -0
- package/hooks/kimi/sessionstart.mjs +113 -0
- package/hooks/kimi/stop.mjs +132 -0
- package/hooks/kimi/userpromptsubmit.mjs +90 -0
- package/hooks/kiro/agentspawn.mjs +97 -0
- package/hooks/kiro/posttooluse.mjs +51 -0
- package/hooks/kiro/pretooluse.mjs +64 -0
- package/hooks/kiro/userpromptsubmit.mjs +88 -0
- package/hooks/normalize-hooks.mjs +323 -0
- package/hooks/platform-bridge.mjs +340 -0
- package/hooks/posttooluse.mjs +225 -0
- package/hooks/precompact.mjs +100 -0
- package/hooks/pretooluse.mjs +227 -0
- package/hooks/qwen-code/platform.mjs +1 -0
- package/hooks/qwen-code/stop.mjs +168 -0
- package/hooks/routing-block.mjs +105 -0
- package/hooks/run-hook.mjs +95 -0
- package/hooks/security.bundle.mjs +2 -0
- package/hooks/session-attribution.bundle.mjs +1 -0
- package/hooks/session-db.bundle.mjs +145 -0
- package/hooks/session-directive.mjs +520 -0
- package/hooks/session-extract.bundle.mjs +3 -0
- package/hooks/session-helpers.mjs +428 -0
- package/hooks/session-loaders.mjs +448 -0
- package/hooks/session-snapshot.bundle.mjs +32 -0
- package/hooks/sessionstart.mjs +464 -0
- package/hooks/stop.mjs +82 -0
- package/hooks/suppress-stderr.mjs +25 -0
- package/hooks/userpromptsubmit.mjs +108 -0
- package/hooks/vscode-copilot/posttooluse.mjs +61 -0
- package/hooks/vscode-copilot/precompact.mjs +55 -0
- package/hooks/vscode-copilot/pretooluse.mjs +27 -0
- package/hooks/vscode-copilot/sessionstart.mjs +129 -0
- package/openclaw.plugin.json +38 -0
- package/package.json +132 -0
- package/scripts/heal-better-sqlite3.mjs +430 -0
- package/scripts/heal-installed-plugins.mjs +635 -0
- package/scripts/plugin-cache-integrity.mjs +248 -0
- package/scripts/postinstall.mjs +397 -0
- package/server.bundle.mjs +1421 -0
- package/server.js +5 -0
- package/skills/.ignore +7 -0
- package/skills/context-mode/SKILL.md +300 -0
- package/skills/context-mode/references/anti-patterns.md +283 -0
- package/skills/context-mode/references/patterns-javascript.md +298 -0
- package/skills/context-mode/references/patterns-python.md +304 -0
- package/skills/context-mode/references/patterns-shell.md +277 -0
- package/skills/ctx-doctor/SKILL.md +22 -0
- package/skills/ctx-index/SKILL.md +46 -0
- package/skills/ctx-insight/SKILL.md +24 -0
- package/skills/ctx-purge/SKILL.md +49 -0
- package/skills/ctx-search/SKILL.md +35 -0
- package/skills/ctx-stats/SKILL.md +26 -0
- package/skills/ctx-upgrade/SKILL.md +31 -0
- package/start.mjs +616 -0
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Self-heal `~/.claude/plugins/installed_plugins.json` (#46915 follow-up).
|
|
3
|
+
*
|
|
4
|
+
* v1.0.113's `/ctx-upgrade` poisoned this file in two ways:
|
|
5
|
+
* 1. Per-entry `version` drifted from the actual cache directory's
|
|
6
|
+
* `plugin.json` version.
|
|
7
|
+
* 2. The top-level `enabledPlugins[<key>]` was emptied (or never set)
|
|
8
|
+
* so Claude Code's plugin loader skipped context-mode → MCP died.
|
|
9
|
+
*
|
|
10
|
+
* Single source of truth shared by:
|
|
11
|
+
* - `start.mjs` HEAL 3+4 (every MCP boot)
|
|
12
|
+
* - `scripts/postinstall.mjs` (every `npm install -g @mxalbert/context-mode`)
|
|
13
|
+
*
|
|
14
|
+
* Pure Node.js (built-ins only). Best-effort: never throws, always
|
|
15
|
+
* returns a plain result object so callers can log a one-liner.
|
|
16
|
+
*
|
|
17
|
+
* @see https://github.com/anthropics/claude-code/issues/46915
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { existsSync, readFileSync, writeFileSync, readdirSync, unlinkSync, statSync } from "node:fs";
|
|
21
|
+
import { resolve, sep } from "node:path";
|
|
22
|
+
|
|
23
|
+
// npm package identity (v2.0.0 fork rename). The plugin/display id stays
|
|
24
|
+
// "context-mode"; only the npm package name is scoped.
|
|
25
|
+
export const PACKAGE_NAME = "@mxalbert/context-mode";
|
|
26
|
+
export const PLUGIN_ID = "context-mode";
|
|
27
|
+
// Claude Code registry key: "<pluginId>@<npmPackage>". A scoped package
|
|
28
|
+
// yields a double-@ key ("context-mode@@mxalbert/context-mode").
|
|
29
|
+
export const PLUGIN_KEY = `${PLUGIN_ID}@${PACKAGE_NAME}`;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Parse a plugin registry key of the shape "<pluginId>@<npmPackage>" where
|
|
33
|
+
* <npmPackage> may be scoped ("@scope/name"). Returns the pluginId (segment
|
|
34
|
+
* before the FIRST "@") and the package segment (everything after it,
|
|
35
|
+
* verbatim — including a leading "@" for scoped packages). Splitting at the
|
|
36
|
+
* first "@" keeps legacy keys ("context-mode@context-mode") and scoped keys
|
|
37
|
+
* ("context-mode@@mxalbert/context-mode") both well-formed; a plain
|
|
38
|
+
* `split("@")` would mangle scoped keys into an empty middle segment.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} pluginKey
|
|
41
|
+
* @returns {{ id: string, pkg: string } | null} null when the key has no
|
|
42
|
+
* separator (bad shape).
|
|
43
|
+
*/
|
|
44
|
+
function parsePluginKey(pluginKey) {
|
|
45
|
+
const sepIdx = pluginKey.indexOf("@");
|
|
46
|
+
if (sepIdx <= 0) return null;
|
|
47
|
+
return { id: pluginKey.slice(0, sepIdx), pkg: pluginKey.slice(sepIdx + 1) };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @typedef {Object} HealResult
|
|
52
|
+
* @property {string[]} healed - one of: "entry-version", "enabled-plugins"
|
|
53
|
+
* @property {string} [skipped] - reason if no work performed
|
|
54
|
+
* @property {string} [error] - error message if heal aborted
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Heal a single plugin entry inside installed_plugins.json.
|
|
59
|
+
*
|
|
60
|
+
* @param {{
|
|
61
|
+
* registryPath: string,
|
|
62
|
+
* pluginCacheRoot: string,
|
|
63
|
+
* pluginKey: string,
|
|
64
|
+
* }} opts
|
|
65
|
+
* @returns {HealResult}
|
|
66
|
+
*/
|
|
67
|
+
export function healInstalledPlugins({ registryPath, pluginCacheRoot, pluginKey }) {
|
|
68
|
+
if (!registryPath || !existsSync(registryPath)) {
|
|
69
|
+
return { healed: [], skipped: "no-registry" };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let raw;
|
|
73
|
+
try {
|
|
74
|
+
raw = readFileSync(registryPath, "utf-8");
|
|
75
|
+
} catch (err) {
|
|
76
|
+
return { healed: [], error: `read-failed: ${(err && err.message) || err}` };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let ip;
|
|
80
|
+
try {
|
|
81
|
+
ip = JSON.parse(raw);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
return { healed: [], error: `parse-failed: ${(err && err.message) || err}` };
|
|
84
|
+
}
|
|
85
|
+
if (!ip || typeof ip !== "object") {
|
|
86
|
+
return { healed: [], error: "bad-shape" };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const entries = (ip.plugins && ip.plugins[pluginKey]) || [];
|
|
90
|
+
if (!Array.isArray(entries) || entries.length === 0) {
|
|
91
|
+
return { healed: [], skipped: "no-entry" };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** @type {string[]} */
|
|
95
|
+
const healed = [];
|
|
96
|
+
let syncedVersion = null;
|
|
97
|
+
|
|
98
|
+
// ── HEAL 3: per-entry version <- cache plugin.json version ──
|
|
99
|
+
// We trust the cache directory because that's what start.mjs actually
|
|
100
|
+
// boots from; the registry is just a stale label.
|
|
101
|
+
for (const entry of entries) {
|
|
102
|
+
if (!entry || typeof entry !== "object") continue;
|
|
103
|
+
const installPath = entry.installPath;
|
|
104
|
+
if (!installPath || typeof installPath !== "string") continue;
|
|
105
|
+
|
|
106
|
+
// Path-traversal guard: only consult plugin.json files inside the
|
|
107
|
+
// declared plugin cache root.
|
|
108
|
+
const resolvedInstall = resolve(installPath);
|
|
109
|
+
const cacheRootWithSep = resolve(pluginCacheRoot) + sep;
|
|
110
|
+
if (!resolvedInstall.startsWith(cacheRootWithSep)) continue;
|
|
111
|
+
|
|
112
|
+
const cachePluginJson = resolve(installPath, ".claude-plugin", "plugin.json");
|
|
113
|
+
if (!existsSync(cachePluginJson)) continue;
|
|
114
|
+
let actualVersion = null;
|
|
115
|
+
try {
|
|
116
|
+
const pj = JSON.parse(readFileSync(cachePluginJson, "utf-8"));
|
|
117
|
+
if (pj && typeof pj.version === "string" && pj.version) {
|
|
118
|
+
actualVersion = pj.version;
|
|
119
|
+
}
|
|
120
|
+
} catch {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (!actualVersion) continue;
|
|
124
|
+
|
|
125
|
+
syncedVersion = actualVersion;
|
|
126
|
+
if (entry.version !== actualVersion) {
|
|
127
|
+
entry.version = actualVersion;
|
|
128
|
+
if (!healed.includes("entry-version")) healed.push("entry-version");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ── HEAL 4: top-level enabledPlugins[key] presence ──
|
|
133
|
+
// Claude Code's plugin loader checks enabledPlugins. When /ctx-upgrade
|
|
134
|
+
// emptied it, our plugin was silently disabled. Set it to `true` (the
|
|
135
|
+
// simplest enabled-flag form) when missing or falsy.
|
|
136
|
+
if (syncedVersion) {
|
|
137
|
+
if (!ip.enabledPlugins || typeof ip.enabledPlugins !== "object" || Array.isArray(ip.enabledPlugins)) {
|
|
138
|
+
ip.enabledPlugins = {};
|
|
139
|
+
}
|
|
140
|
+
const current = ip.enabledPlugins[pluginKey];
|
|
141
|
+
if (current === undefined || current === null || current === false || current === "") {
|
|
142
|
+
ip.enabledPlugins[pluginKey] = true;
|
|
143
|
+
healed.push("enabled-plugins");
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (healed.length > 0) {
|
|
148
|
+
try {
|
|
149
|
+
writeFileSync(registryPath, JSON.stringify(ip, null, 2) + "\n", "utf-8");
|
|
150
|
+
} catch (err) {
|
|
151
|
+
return { healed: [], error: `write-failed: ${(err && err.message) || err}` };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return { healed };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Heal `~/.claude/settings.json.enabledPlugins[pluginKey]`.
|
|
160
|
+
*
|
|
161
|
+
* v1.0.114's heal targeted `installed_plugins.json.enabledPlugins`, which is
|
|
162
|
+
* what we control. But Claude Code's plugin loader actually reads the truth
|
|
163
|
+
* from `settings.json.enabledPlugins`. After every `/ctx-upgrade`, Claude
|
|
164
|
+
* Code's plugin manager seems to clear the settings.json key (likely on
|
|
165
|
+
* version-mismatch detection), so the plugin appears disabled even though
|
|
166
|
+
* `installed_plugins.json` is fully consistent. v1.0.116 closes that gap.
|
|
167
|
+
*
|
|
168
|
+
* Respects explicit user opt-out: if the key is `false`, leaves it alone.
|
|
169
|
+
*
|
|
170
|
+
* @param {{ settingsPath: string, pluginKey: string }} opts
|
|
171
|
+
* @returns {HealResult}
|
|
172
|
+
*/
|
|
173
|
+
export function healSettingsEnabledPlugins({ settingsPath, pluginKey }) {
|
|
174
|
+
if (!settingsPath || !existsSync(settingsPath)) {
|
|
175
|
+
return { healed: [], skipped: "no-settings" };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let raw;
|
|
179
|
+
try { raw = readFileSync(settingsPath, "utf-8"); }
|
|
180
|
+
catch (err) { return { healed: [], error: `read-failed: ${(err && err.message) || err}` }; }
|
|
181
|
+
|
|
182
|
+
let settings;
|
|
183
|
+
try { settings = JSON.parse(raw); }
|
|
184
|
+
catch (err) { return { healed: [], error: `parse-failed: ${(err && err.message) || err}` }; }
|
|
185
|
+
|
|
186
|
+
const healed = [];
|
|
187
|
+
if (!settings.enabledPlugins || typeof settings.enabledPlugins !== "object" || Array.isArray(settings.enabledPlugins)) {
|
|
188
|
+
settings.enabledPlugins = {};
|
|
189
|
+
}
|
|
190
|
+
const current = settings.enabledPlugins[pluginKey];
|
|
191
|
+
if (current === false) {
|
|
192
|
+
return { healed: [], skipped: "explicit-opt-out" };
|
|
193
|
+
}
|
|
194
|
+
if (current !== true) {
|
|
195
|
+
settings.enabledPlugins[pluginKey] = true;
|
|
196
|
+
healed.push("enabled-plugins");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (healed.length > 0) {
|
|
200
|
+
try {
|
|
201
|
+
writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + "\n", "utf-8");
|
|
202
|
+
} catch (err) {
|
|
203
|
+
return { healed: [], error: `write-failed: ${(err && err.message) || err}` };
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return { healed };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
211
|
+
// Issue #523 (v1.0.119) — Layer 5 heal: plugin.json mcpServers args
|
|
212
|
+
//
|
|
213
|
+
// /ctx-upgrade in v1.0.118 wrote `.mcp.json` with the literal
|
|
214
|
+
// `${CLAUDE_PLUGIN_ROOT}` placeholder (#411) but did NOT touch
|
|
215
|
+
// `.claude-plugin/plugin.json`. On Windows, start.mjs's `normalizeHooksOnStartup`
|
|
216
|
+
// (#378) rewrites that file's `mcpServers["context-mode"].args[0]` to an
|
|
217
|
+
// absolute path. If `pluginRoot` happens to be the upgrade tmpdir at the time
|
|
218
|
+
// of normalization (or an earlier upgrade left absolute paths in place), the
|
|
219
|
+
// resulting plugin.json carries a `<tmpdir>/context-mode-upgrade-<epoch>/start.mjs`
|
|
220
|
+
// path. After Node tmpdir cleanup, MCP fails to spawn with ENOENT and the user
|
|
221
|
+
// has no /ctx-upgrade escape hatch.
|
|
222
|
+
//
|
|
223
|
+
// This heal is the sibling of #411's `.mcp.json` fix:
|
|
224
|
+
// - Detects tmpdir-prefixed args[0] (epoch-pattern, OS-agnostic)
|
|
225
|
+
// - Rewrites to literal `${CLAUDE_PLUGIN_ROOT}/start.mjs` placeholder
|
|
226
|
+
// - Never touches sibling mcpServers entries (only `pluginKey`'s server)
|
|
227
|
+
// - Refuses to write outside `pluginCacheRoot` (path-traversal guard)
|
|
228
|
+
//
|
|
229
|
+
// Single source of truth shared by:
|
|
230
|
+
// - `start.mjs` HEAL 5b (every MCP boot)
|
|
231
|
+
// - `scripts/postinstall.mjs` (every `npm install -g @mxalbert/context-mode`)
|
|
232
|
+
// - `src/cli.ts` upgrade() (post-bump)
|
|
233
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
234
|
+
|
|
235
|
+
const PLACEHOLDER_ARG = "${CLAUDE_PLUGIN_ROOT}/start.mjs";
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Heal `<pluginRoot>/.claude-plugin/plugin.json` mcpServers args.
|
|
239
|
+
*
|
|
240
|
+
* @param {{
|
|
241
|
+
* pluginRoot: string,
|
|
242
|
+
* pluginCacheRoot: string,
|
|
243
|
+
* pluginKey: string,
|
|
244
|
+
* }} opts
|
|
245
|
+
* @returns {HealResult}
|
|
246
|
+
*/
|
|
247
|
+
export function healPluginJsonMcpServers({ pluginRoot, pluginCacheRoot, pluginKey }) {
|
|
248
|
+
if (!pluginRoot || !pluginCacheRoot || !pluginKey) {
|
|
249
|
+
return { healed: [], skipped: "missing-args" };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Path-traversal guard: refuse to touch a plugin root that escapes the
|
|
253
|
+
// declared cache root. Mirrors HEAL 3's guard.
|
|
254
|
+
const resolvedRoot = resolve(pluginRoot);
|
|
255
|
+
const cacheRootWithSep = resolve(pluginCacheRoot) + sep;
|
|
256
|
+
if (!resolvedRoot.startsWith(cacheRootWithSep)) {
|
|
257
|
+
return { healed: [], skipped: "outside-cache-root" };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const pluginJsonPath = resolve(pluginRoot, ".claude-plugin", "plugin.json");
|
|
261
|
+
if (!existsSync(pluginJsonPath)) {
|
|
262
|
+
return { healed: [], skipped: "no-plugin-json" };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
let raw;
|
|
266
|
+
try { raw = readFileSync(pluginJsonPath, "utf-8"); }
|
|
267
|
+
catch (err) { return { healed: [], error: `read-failed: ${(err && err.message) || err}` }; }
|
|
268
|
+
|
|
269
|
+
let parsed;
|
|
270
|
+
try { parsed = JSON.parse(raw); }
|
|
271
|
+
catch (err) { return { healed: [], error: `parse-failed: ${(err && err.message) || err}` }; }
|
|
272
|
+
|
|
273
|
+
const servers = parsed && parsed.mcpServers;
|
|
274
|
+
if (!servers || typeof servers !== "object") {
|
|
275
|
+
return { healed: [], skipped: "no-mcp-servers" };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Derive our server name from pluginKey ("context-mode@@mxalbert/context-mode" → "context-mode").
|
|
279
|
+
const ourServerName = parsePluginKey(pluginKey)?.id ?? pluginKey.split("@")[0];
|
|
280
|
+
const ours = servers[ourServerName];
|
|
281
|
+
if (!ours || typeof ours !== "object" || !Array.isArray(ours.args)) {
|
|
282
|
+
return { healed: [], skipped: "no-our-server" };
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** @type {string[]} */
|
|
286
|
+
const healed = [];
|
|
287
|
+
const before = ours.args;
|
|
288
|
+
const after = before.map((a) => {
|
|
289
|
+
if (typeof a !== "string") return a;
|
|
290
|
+
// Already the placeholder — nothing to heal.
|
|
291
|
+
if (a === PLACEHOLDER_ARG) return a;
|
|
292
|
+
// Issue #711: any absolute path ending in start.mjs should be the
|
|
293
|
+
// placeholder. Catches tmpdir paths (context-mode-upgrade-<digits>)
|
|
294
|
+
// AND stale versioned cache-dir paths (.../1.0.103/start.mjs) that
|
|
295
|
+
// normalizeHooksOnStartup baked in during a prior upgrade.
|
|
296
|
+
if (/[/\\]start\.mjs$/.test(a)) {
|
|
297
|
+
return PLACEHOLDER_ARG;
|
|
298
|
+
}
|
|
299
|
+
return a;
|
|
300
|
+
});
|
|
301
|
+
const changed = after.some((v, i) => v !== before[i]);
|
|
302
|
+
if (changed) {
|
|
303
|
+
ours.args = after;
|
|
304
|
+
healed.push("plugin-json-args");
|
|
305
|
+
try {
|
|
306
|
+
writeFileSync(pluginJsonPath, JSON.stringify(parsed, null, 2) + "\n", "utf-8");
|
|
307
|
+
} catch (err) {
|
|
308
|
+
return { healed: [], error: `write-failed: ${(err && err.message) || err}` };
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return { healed };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
316
|
+
// Issue #531 (v1.0.122) — Layer 6 heal: .mcp.json mcpServers args
|
|
317
|
+
//
|
|
318
|
+
// Asymmetric-heal sibling of healPluginJsonMcpServers (#523). The regression
|
|
319
|
+
// that broke `.mcp.json` was commit aea633c (PR #253, 2026-04-13): the shipped
|
|
320
|
+
// `.mcp.json` template at repo root used a bare relative `./start.mjs` arg.
|
|
321
|
+
// Claude Code spawns the MCP child with session CWD inherited (not pluginRoot)
|
|
322
|
+
// so fresh npm marketplace installs throw MODULE_NOT_FOUND on every ctx_* tool.
|
|
323
|
+
// v1.0.119 added healPluginJsonMcpServers for the `.claude-plugin/plugin.json`
|
|
324
|
+
// sibling but missed `.mcp.json` — same plugin, same drift class, different
|
|
325
|
+
// file. This module is the asymmetric-heal sibling.
|
|
326
|
+
//
|
|
327
|
+
// Same regex, same placeholder, same traversal guard as #523. Only difference:
|
|
328
|
+
// - Target: `<pluginRoot>/.mcp.json` (flat shape, no `.claude-plugin/` subdir)
|
|
329
|
+
// - Structure: `.mcpServers.<pluginName>.args[]`
|
|
330
|
+
// - Additional drift shape: bare relative `./start.mjs` (the #253 regression)
|
|
331
|
+
// that healPluginJsonMcpServers's tmpdir-only check would not catch.
|
|
332
|
+
//
|
|
333
|
+
// Single source of truth shared by:
|
|
334
|
+
// - `start.mjs` HEAL 5b (every MCP boot)
|
|
335
|
+
// - `scripts/postinstall.mjs` (every `npm install -g @mxalbert/context-mode`)
|
|
336
|
+
// - `src/cli.ts` upgrade() (post-bump)
|
|
337
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Heal `<pluginRoot>/.mcp.json` mcpServers args.
|
|
341
|
+
*
|
|
342
|
+
* Detects two drift shapes:
|
|
343
|
+
* 1. Bare relative `./start.mjs` (#253 regression — fresh-install class).
|
|
344
|
+
* 2. Tmpdir-prefixed `<...>/context-mode-upgrade-<digits>/start.mjs`
|
|
345
|
+
* (mirrors healPluginJsonMcpServers's #523 tmpdir class).
|
|
346
|
+
* Both rewrite to the literal `${CLAUDE_PLUGIN_ROOT}/start.mjs` placeholder
|
|
347
|
+
* Claude Code resolves at load-time.
|
|
348
|
+
*
|
|
349
|
+
* @param {{
|
|
350
|
+
* pluginRoot: string,
|
|
351
|
+
* pluginCacheRoot: string,
|
|
352
|
+
* pluginKey: string,
|
|
353
|
+
* }} opts
|
|
354
|
+
* @returns {HealResult}
|
|
355
|
+
*/
|
|
356
|
+
export function healMcpJsonArgs({ pluginRoot, pluginCacheRoot, pluginKey }) {
|
|
357
|
+
if (!pluginRoot || !pluginCacheRoot || !pluginKey) {
|
|
358
|
+
return { healed: [], skipped: "missing-args" };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Path-traversal guard: refuse to touch a plugin root that escapes the
|
|
362
|
+
// declared cache root. Mirrors healPluginJsonMcpServers + HEAL 3.
|
|
363
|
+
const resolvedRoot = resolve(pluginRoot);
|
|
364
|
+
const cacheRootWithSep = resolve(pluginCacheRoot) + sep;
|
|
365
|
+
if (!resolvedRoot.startsWith(cacheRootWithSep)) {
|
|
366
|
+
return { healed: [], skipped: "outside-cache-root" };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// `.mcp.json` lives at pluginRoot/.mcp.json (flat), NOT under .claude-plugin/.
|
|
370
|
+
const mcpJsonPath = resolve(pluginRoot, ".mcp.json");
|
|
371
|
+
if (!existsSync(mcpJsonPath)) {
|
|
372
|
+
return { healed: [], skipped: "no-mcp-json" };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
let raw;
|
|
376
|
+
try { raw = readFileSync(mcpJsonPath, "utf-8"); }
|
|
377
|
+
catch (err) { return { healed: [], error: `read-failed: ${(err && err.message) || err}` }; }
|
|
378
|
+
|
|
379
|
+
let parsed;
|
|
380
|
+
try { parsed = JSON.parse(raw); }
|
|
381
|
+
catch (err) { return { healed: [], error: `parse-failed: ${(err && err.message) || err}` }; }
|
|
382
|
+
|
|
383
|
+
const servers = parsed && parsed.mcpServers;
|
|
384
|
+
if (!servers || typeof servers !== "object") {
|
|
385
|
+
return { healed: [], skipped: "no-mcp-servers" };
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Derive our server name from pluginKey ("context-mode@@mxalbert/context-mode" → "context-mode").
|
|
389
|
+
const ourServerName = parsePluginKey(pluginKey)?.id ?? pluginKey.split("@")[0];
|
|
390
|
+
const ours = servers[ourServerName];
|
|
391
|
+
if (!ours || typeof ours !== "object" || !Array.isArray(ours.args)) {
|
|
392
|
+
return { healed: [], skipped: "no-our-server" };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** @type {string[]} */
|
|
396
|
+
const healed = [];
|
|
397
|
+
const before = ours.args;
|
|
398
|
+
const after = before.map((a) => {
|
|
399
|
+
if (typeof a !== "string") return a;
|
|
400
|
+
// Already the placeholder — nothing to heal.
|
|
401
|
+
if (a === PLACEHOLDER_ARG) return a;
|
|
402
|
+
// Drift shape #1 (issue #531 / commit aea633c): bare relative `./start.mjs`.
|
|
403
|
+
if (a === "./start.mjs" || a === "start.mjs") {
|
|
404
|
+
return PLACEHOLDER_ARG;
|
|
405
|
+
}
|
|
406
|
+
// Issue #711: any absolute path ending in start.mjs should be the
|
|
407
|
+
// placeholder. Catches tmpdir paths AND stale versioned cache-dir
|
|
408
|
+
// paths (.../1.0.103/start.mjs) from prior upgrades.
|
|
409
|
+
if (/[/\\]start\.mjs$/.test(a)) {
|
|
410
|
+
return PLACEHOLDER_ARG;
|
|
411
|
+
}
|
|
412
|
+
return a;
|
|
413
|
+
});
|
|
414
|
+
const changed = after.some((v, i) => v !== before[i]);
|
|
415
|
+
if (changed) {
|
|
416
|
+
ours.args = after;
|
|
417
|
+
healed.push("mcp-json-args");
|
|
418
|
+
try {
|
|
419
|
+
writeFileSync(mcpJsonPath, JSON.stringify(parsed, null, 2) + "\n", "utf-8");
|
|
420
|
+
} catch (err) {
|
|
421
|
+
return { healed: [], error: `write-failed: ${(err && err.message) || err}` };
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return { healed };
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Heal user-level ~/.claude.json MCP server registrations that point to an
|
|
430
|
+
* old context-mode version dir in the plugin cache.
|
|
431
|
+
*
|
|
432
|
+
* Users who work around the Claude Code plugin MCP tool-exposure bug
|
|
433
|
+
* (anthropics/claude-code#59310) by running `claude mcp add --scope user`
|
|
434
|
+
* end up with an absolute path to a specific version dir in ~/.claude.json.
|
|
435
|
+
* After /ctx-upgrade that path is stale — this heal detects and updates it.
|
|
436
|
+
*
|
|
437
|
+
* @param {{
|
|
438
|
+
* dotClaudeJsonPath: string,
|
|
439
|
+
* pluginCacheParent: string,
|
|
440
|
+
* newPluginRoot: string,
|
|
441
|
+
* }} opts
|
|
442
|
+
* @returns {HealResult}
|
|
443
|
+
*/
|
|
444
|
+
export function healClaudeJsonMcpArgs({ dotClaudeJsonPath, pluginCacheParent, newPluginRoot }) {
|
|
445
|
+
if (!dotClaudeJsonPath || !existsSync(dotClaudeJsonPath)) {
|
|
446
|
+
return { healed: [], skipped: "no-claude-json" };
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
let raw;
|
|
450
|
+
try { raw = readFileSync(dotClaudeJsonPath, "utf-8"); }
|
|
451
|
+
catch (err) { return { healed: [], error: `read-failed: ${(err && err.message) || err}` }; }
|
|
452
|
+
|
|
453
|
+
let config;
|
|
454
|
+
try { config = JSON.parse(raw); }
|
|
455
|
+
catch (err) { return { healed: [], error: `parse-failed: ${(err && err.message) || err}` }; }
|
|
456
|
+
|
|
457
|
+
const servers = config && config.mcpServers;
|
|
458
|
+
if (!servers || typeof servers !== "object") {
|
|
459
|
+
return { healed: [], skipped: "no-mcp-servers" };
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const cacheParentFwd = pluginCacheParent.replace(/\\/g, "/");
|
|
463
|
+
// Post-resolve containment on newArg. ~/.claude.json is locally user-
|
|
464
|
+
// writable (same trust boundary as installed_plugins.json), and the
|
|
465
|
+
// `suffix` slice is derived from arg strings inside the existing config.
|
|
466
|
+
// A crafted arg like
|
|
467
|
+
// .../cache/<owner>/<plugin>/1.0.0/../../../evil/start.mjs
|
|
468
|
+
// slices to suffix="../../../evil/start.mjs", and resolve(newPluginRoot,
|
|
469
|
+
// suffix) normalizes to an attacker-chosen .mjs path outside the plugin
|
|
470
|
+
// cache. Writing that path back into ~/.claude.json mutates the mcpServers
|
|
471
|
+
// args so the next MCP boot spawns from the attacker path. Reject any
|
|
472
|
+
// suffix that escapes newPluginRoot.
|
|
473
|
+
const newPluginRootResolved = resolve(newPluginRoot);
|
|
474
|
+
const newPluginRootWithSep = newPluginRootResolved + sep;
|
|
475
|
+
|
|
476
|
+
let mutated = false;
|
|
477
|
+
for (const srv of Object.values(servers)) {
|
|
478
|
+
if (!srv || typeof srv !== "object" || !Array.isArray(srv.args)) continue;
|
|
479
|
+
for (let i = 0; i < srv.args.length; i++) {
|
|
480
|
+
const arg = srv.args[i];
|
|
481
|
+
if (typeof arg !== "string") continue;
|
|
482
|
+
const argFwd = arg.replace(/\\/g, "/");
|
|
483
|
+
if (!argFwd.startsWith(cacheParentFwd + "/")) continue;
|
|
484
|
+
const rel = argFwd.slice(cacheParentFwd.length + 1);
|
|
485
|
+
const slashIdx = rel.indexOf("/");
|
|
486
|
+
if (slashIdx < 0) continue;
|
|
487
|
+
const suffix = rel.slice(slashIdx + 1);
|
|
488
|
+
const newArg = resolve(newPluginRoot, suffix);
|
|
489
|
+
if (
|
|
490
|
+
newArg !== newPluginRootResolved &&
|
|
491
|
+
!(newArg + sep).startsWith(newPluginRootWithSep)
|
|
492
|
+
) {
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
if (newArg !== arg) {
|
|
496
|
+
srv.args[i] = newArg;
|
|
497
|
+
mutated = true;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (!mutated) return { healed: [] };
|
|
503
|
+
|
|
504
|
+
try {
|
|
505
|
+
writeFileSync(dotClaudeJsonPath, JSON.stringify(config, null, 2), "utf-8");
|
|
506
|
+
} catch (err) {
|
|
507
|
+
return { healed: [], error: `write-failed: ${(err && err.message) || err}` };
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return { healed: ["claude-json-mcp-args"] };
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
514
|
+
// Issue #609 — sweepStaleMcpJson: remove cache-baked `.mcp.json` files.
|
|
515
|
+
//
|
|
516
|
+
// Background (per ISSUE-609-VERDICT, ISSUE-604-VERDICT):
|
|
517
|
+
// cli.ts upgrade() wrote `.mcp.json` into every per-version plugin-cache
|
|
518
|
+
// dir starting with #411. PR #531 (commit 9261377) removed `.mcp.json`
|
|
519
|
+
// from `package.json files[]` so the npm tarball no longer ships it,
|
|
520
|
+
// but the cli-side write persisted. Every `/ctx-upgrade` re-baked a
|
|
521
|
+
// per-version copy. When Claude Code's native plugin manager auto-update
|
|
522
|
+
// later copies a previous version's `.mcp.json` forward into a fresh
|
|
523
|
+
// version dir, the stale start.mjs absolute path goes with it →
|
|
524
|
+
// MODULE_NOT_FOUND on every MCP boot, and `ctx-doctor` stays green
|
|
525
|
+
// because nothing validates that path against current pluginRoot.
|
|
526
|
+
//
|
|
527
|
+
// The architectural fix is to STOP writing `.mcp.json` from the cache layer
|
|
528
|
+
// entirely. `.claude-plugin/plugin.json.mcpServers` is the canonical source
|
|
529
|
+
// (refs/platforms/claude-code/src/utils/plugins/mcpPluginIntegration.ts:131-212
|
|
530
|
+
// — Claude Code reads it first). This sweep removes any pre-existing
|
|
531
|
+
// `.mcp.json` from every per-version cache dir so the previous-version-
|
|
532
|
+
// carry vector cannot replay across upgrades.
|
|
533
|
+
//
|
|
534
|
+
// Single source of truth shared by:
|
|
535
|
+
// - `start.mjs` HEAL 5c (every MCP boot)
|
|
536
|
+
// - `scripts/postinstall.mjs` (every `npm install -g @mxalbert/context-mode`)
|
|
537
|
+
// - `src/cli.ts` upgrade() (post-bump)
|
|
538
|
+
//
|
|
539
|
+
// Safety contracts:
|
|
540
|
+
// - Path-traversal guard: refuses to walk outside `pluginCacheRoot`.
|
|
541
|
+
// - Best-effort: NEVER throws; missing files / unreadable dirs are
|
|
542
|
+
// skipped silently and reported in the result.
|
|
543
|
+
// - Scope: deletes ONLY files named exactly `.mcp.json`; never touches
|
|
544
|
+
// sibling files in the same dir.
|
|
545
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* @typedef {Object} SweepResult
|
|
549
|
+
* @property {string[]} removed - absolute paths of removed `.mcp.json` files
|
|
550
|
+
* @property {string} [skipped] - reason if no work performed (e.g. "no-cache-root")
|
|
551
|
+
*/
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Remove every `.mcp.json` from per-version directories under
|
|
555
|
+
* `<pluginCacheRoot>/<seg1>/<seg2>/`.
|
|
556
|
+
*
|
|
557
|
+
* @param {{ pluginCacheRoot: string, pluginKey: string }} opts
|
|
558
|
+
* pluginKey is the "<pluginId>@<npmPackage>" form (e.g. the legacy
|
|
559
|
+
* "context-mode@context-mode" or the scoped
|
|
560
|
+
* "context-mode@@mxalbert/context-mode").
|
|
561
|
+
* @returns {SweepResult}
|
|
562
|
+
*/
|
|
563
|
+
export function sweepStaleMcpJson({ pluginCacheRoot, pluginKey }) {
|
|
564
|
+
/** @type {string[]} */
|
|
565
|
+
const removed = [];
|
|
566
|
+
|
|
567
|
+
if (!pluginCacheRoot || !pluginKey) {
|
|
568
|
+
return { removed, skipped: "missing-args" };
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const resolvedCacheRoot = resolve(pluginCacheRoot);
|
|
572
|
+
if (!existsSync(resolvedCacheRoot)) {
|
|
573
|
+
return { removed, skipped: "no-cache-root" };
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// pluginKey shape: "<pluginId>@<npmPackage>" where the package may be
|
|
577
|
+
// scoped ("@scope/name"), so the key may contain two '@' characters.
|
|
578
|
+
// Split at the FIRST '@' only — a plain split("@") would turn the scoped
|
|
579
|
+
// key into an empty middle segment and abort the sweep.
|
|
580
|
+
const keyParts = parsePluginKey(pluginKey);
|
|
581
|
+
if (!keyParts) {
|
|
582
|
+
return { removed, skipped: "bad-plugin-key" };
|
|
583
|
+
}
|
|
584
|
+
const ownerSegment = keyParts.id;
|
|
585
|
+
const pluginSegment = keyParts.pkg;
|
|
586
|
+
if (!ownerSegment || !pluginSegment) {
|
|
587
|
+
return { removed, skipped: "bad-plugin-key" };
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// Path-traversal guard: refuse to walk outside the declared cache root,
|
|
591
|
+
// even if pluginKey contains `..` segments. Per Mert's standing Windows
|
|
592
|
+
// safety rule, resolve normalizes both `/` and `\` so the guard fires
|
|
593
|
+
// on either separator.
|
|
594
|
+
const ownerDir = resolve(resolvedCacheRoot, ownerSegment, pluginSegment);
|
|
595
|
+
const cacheRootWithSep = resolvedCacheRoot + sep;
|
|
596
|
+
if (!ownerDir.startsWith(cacheRootWithSep)) {
|
|
597
|
+
return { removed, skipped: "outside-cache-root" };
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
if (!existsSync(ownerDir)) {
|
|
601
|
+
return { removed, skipped: "no-plugin-dir" };
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/** @type {string[]} */
|
|
605
|
+
let versionEntries = [];
|
|
606
|
+
try {
|
|
607
|
+
versionEntries = readdirSync(ownerDir);
|
|
608
|
+
} catch {
|
|
609
|
+
return { removed, skipped: "readdir-failed" };
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
for (const versionEntry of versionEntries) {
|
|
613
|
+
const versionDir = resolve(ownerDir, versionEntry);
|
|
614
|
+
// Per-version guard: only enter directories whose resolved path stays
|
|
615
|
+
// under the owner dir. Belt-and-braces against weird FS entries.
|
|
616
|
+
if (!versionDir.startsWith(ownerDir + sep)) continue;
|
|
617
|
+
try {
|
|
618
|
+
const stat = statSync(versionDir);
|
|
619
|
+
if (!stat.isDirectory()) continue;
|
|
620
|
+
} catch {
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
const mcpJsonPath = resolve(versionDir, ".mcp.json");
|
|
624
|
+
if (!existsSync(mcpJsonPath)) continue;
|
|
625
|
+
try {
|
|
626
|
+
unlinkSync(mcpJsonPath);
|
|
627
|
+
removed.push(mcpJsonPath);
|
|
628
|
+
} catch {
|
|
629
|
+
// best-effort: file may have been removed by a concurrent process
|
|
630
|
+
// between existsSync and unlinkSync. Silent skip.
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
return { removed };
|
|
635
|
+
}
|