context-mode 1.0.95 → 1.0.96

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.
@@ -6,14 +6,14 @@
6
6
  },
7
7
  "metadata": {
8
8
  "description": "Claude Code plugins by Mert Koseoğlu",
9
- "version": "1.0.95"
9
+ "version": "1.0.96"
10
10
  },
11
11
  "plugins": [
12
12
  {
13
13
  "name": "context-mode",
14
14
  "source": "./",
15
15
  "description": "Claude Code MCP plugin that saves 98% of your context window. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and intent-driven search.",
16
- "version": "1.0.95",
16
+ "version": "1.0.96",
17
17
  "author": {
18
18
  "name": "Mert Koseoğlu"
19
19
  },
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-mode",
3
- "version": "1.0.95",
3
+ "version": "1.0.96",
4
4
  "description": "MCP server that saves 98% of your context window with session continuity. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and automatic state restore across compactions.",
5
5
  "author": {
6
6
  "name": "Mert Koseoğlu",
@@ -3,7 +3,7 @@
3
3
  "name": "Context Mode",
4
4
  "kind": "tool",
5
5
  "description": "OpenClaw plugin that saves 98% of your context window. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and intent-driven search.",
6
- "version": "1.0.95",
6
+ "version": "1.0.96",
7
7
  "sandbox": {
8
8
  "mode": "permissive",
9
9
  "filesystem_access": "full",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-mode",
3
- "version": "1.0.95",
3
+ "version": "1.0.96",
4
4
  "description": "OpenClaw plugin that saves 98% of your context window. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and intent-driven search.",
5
5
  "author": {
6
6
  "name": "Mert Koseoğlu",
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Plugin cache self-heal — fixes broken CLAUDE_PLUGIN_ROOT references.
3
+ *
4
+ * Claude Code's plugin auto-update can leave installed_plugins.json pointing
5
+ * to a non-existent directory (anthropics/claude-code#46915). This module
6
+ * detects and repairs the mismatch by creating symlinks.
7
+ *
8
+ * 4-layer defense:
9
+ * 1. start.mjs startup — reverse heal (registry → symlink to us)
10
+ * 2. server.ts first tool call — mid-session heal
11
+ * 3. postinstall.mjs — backward symlink on new install
12
+ * 4. global hook auto-deploy — survives total plugin cache breakage
13
+ */
14
+ export interface HealResult {
15
+ healed: boolean;
16
+ action?: "symlink" | "global-hook" | "none";
17
+ from?: string;
18
+ to?: string;
19
+ }
20
+ /**
21
+ * Core heal: if installed_plugins.json points to a non-existent directory,
22
+ * create a symlink from that path to our actual directory.
23
+ *
24
+ * @param currentDir - The directory we're actually running from
25
+ * @param installedPluginsPath - Path to installed_plugins.json (injectable for testing)
26
+ */
27
+ export declare function healRegistryMismatch(currentDir: string, installedPluginsPath?: string): HealResult;
28
+ /**
29
+ * Deploy a global SessionStart hook that heals plugin cache mismatches.
30
+ * This hook lives outside the plugin directory, so it survives cache breakage.
31
+ *
32
+ * Written to ~/.claude/hooks/context-mode-cache-heal.sh
33
+ */
34
+ export declare function deployGlobalHealHook(): HealResult;
35
+ /**
36
+ * Backward symlink: during postinstall, if the registry points to a
37
+ * non-existent OLD path, create a symlink from old → new (our directory).
38
+ * Same as healRegistryMismatch but called from postinstall context.
39
+ */
40
+ export { healRegistryMismatch as healBackwardCompat };
41
+ /**
42
+ * Mid-session heal — call on first MCP tool invocation.
43
+ * Checks if registry path differs from our running directory.
44
+ * Creates symlink if needed. Runs only once per process.
45
+ */
46
+ export declare function healMidSession(currentDir: string): HealResult;
47
+ /** Reset mid-session flag (for testing only) */
48
+ export declare function _resetMidSession(): void;
@@ -0,0 +1,150 @@
1
+ /**
2
+ * Plugin cache self-heal — fixes broken CLAUDE_PLUGIN_ROOT references.
3
+ *
4
+ * Claude Code's plugin auto-update can leave installed_plugins.json pointing
5
+ * to a non-existent directory (anthropics/claude-code#46915). This module
6
+ * detects and repairs the mismatch by creating symlinks.
7
+ *
8
+ * 4-layer defense:
9
+ * 1. start.mjs startup — reverse heal (registry → symlink to us)
10
+ * 2. server.ts first tool call — mid-session heal
11
+ * 3. postinstall.mjs — backward symlink on new install
12
+ * 4. global hook auto-deploy — survives total plugin cache breakage
13
+ */
14
+ import { existsSync, readFileSync, symlinkSync, mkdirSync, writeFileSync } from "node:fs";
15
+ import { resolve, dirname } from "node:path";
16
+ import { homedir } from "node:os";
17
+ /**
18
+ * Core heal: if installed_plugins.json points to a non-existent directory,
19
+ * create a symlink from that path to our actual directory.
20
+ *
21
+ * @param currentDir - The directory we're actually running from
22
+ * @param installedPluginsPath - Path to installed_plugins.json (injectable for testing)
23
+ */
24
+ export function healRegistryMismatch(currentDir, installedPluginsPath) {
25
+ const ipPath = installedPluginsPath ?? resolve(homedir(), ".claude", "plugins", "installed_plugins.json");
26
+ if (!existsSync(ipPath))
27
+ return { healed: false, action: "none" };
28
+ if (!existsSync(currentDir))
29
+ return { healed: false, action: "none" };
30
+ let ip;
31
+ try {
32
+ ip = JSON.parse(readFileSync(ipPath, "utf-8"));
33
+ }
34
+ catch {
35
+ return { healed: false, action: "none" };
36
+ }
37
+ for (const [key, entries] of Object.entries(ip.plugins ?? {})) {
38
+ if (!key.toLowerCase().includes("context-mode"))
39
+ continue;
40
+ for (const entry of entries) {
41
+ const registryPath = entry.installPath;
42
+ if (!registryPath)
43
+ continue;
44
+ // Registry path exists — no healing needed
45
+ if (existsSync(registryPath))
46
+ continue;
47
+ // Registry path doesn't exist — create symlink to our directory
48
+ try {
49
+ const parent = dirname(registryPath);
50
+ if (!existsSync(parent))
51
+ mkdirSync(parent, { recursive: true });
52
+ if (process.platform === "win32") {
53
+ // Windows: use junction (no admin required)
54
+ symlinkSync(currentDir, registryPath, "junction");
55
+ }
56
+ else {
57
+ symlinkSync(currentDir, registryPath);
58
+ }
59
+ return { healed: true, action: "symlink", from: registryPath, to: currentDir };
60
+ }
61
+ catch {
62
+ return { healed: false, action: "none" };
63
+ }
64
+ }
65
+ }
66
+ return { healed: false, action: "none" };
67
+ }
68
+ /**
69
+ * Deploy a global SessionStart hook that heals plugin cache mismatches.
70
+ * This hook lives outside the plugin directory, so it survives cache breakage.
71
+ *
72
+ * Written to ~/.claude/hooks/context-mode-cache-heal.sh
73
+ */
74
+ export function deployGlobalHealHook() {
75
+ const hooksDir = resolve(homedir(), ".claude", "hooks");
76
+ const hookPath = resolve(hooksDir, "context-mode-cache-heal.sh");
77
+ // Already deployed
78
+ if (existsSync(hookPath))
79
+ return { healed: false, action: "none" };
80
+ try {
81
+ if (!existsSync(hooksDir))
82
+ mkdirSync(hooksDir, { recursive: true });
83
+ const script = `#!/usr/bin/env bash
84
+ # context-mode plugin cache self-heal — auto-deployed by context-mode MCP server
85
+ # Fixes anthropics/claude-code#46915: auto-update breaks CLAUDE_PLUGIN_ROOT
86
+ # This hook runs at SessionStart (global, not plugin-level) so it works even
87
+ # when the plugin cache is broken.
88
+
89
+ set -euo pipefail
90
+
91
+ PLUGINS_FILE="$HOME/.claude/plugins/installed_plugins.json"
92
+ [[ -f "$PLUGINS_FILE" ]] || exit 0
93
+
94
+ # Find context-mode entries and heal missing directories
95
+ node -e '
96
+ const fs = require("fs");
97
+ const path = require("path");
98
+ try {
99
+ const ip = JSON.parse(fs.readFileSync(process.argv[1], "utf-8"));
100
+ for (const [key, entries] of Object.entries(ip.plugins || {})) {
101
+ if (!key.toLowerCase().includes("context-mode")) continue;
102
+ for (const entry of entries) {
103
+ const p = entry.installPath;
104
+ if (!p || fs.existsSync(p)) continue;
105
+ const parent = path.dirname(p);
106
+ if (!fs.existsSync(parent)) continue;
107
+ const dirs = fs.readdirSync(parent).filter(d => /^\\d+\\.\\d+/.test(d) && fs.statSync(path.join(parent, d)).isDirectory());
108
+ if (dirs.length === 0) continue;
109
+ dirs.sort((a, b) => {
110
+ const pa = a.split(".").map(Number), pb = b.split(".").map(Number);
111
+ for (let i = 0; i < 3; i++) { if ((pa[i]||0) !== (pb[i]||0)) return (pa[i]||0) - (pb[i]||0); }
112
+ return 0;
113
+ });
114
+ const target = path.join(parent, dirs[dirs.length - 1]);
115
+ try { fs.symlinkSync(target, p); } catch {}
116
+ }
117
+ }
118
+ } catch {}
119
+ ' "$PLUGINS_FILE" 2>/dev/null || true
120
+ `;
121
+ writeFileSync(hookPath, script, { mode: 0o755 });
122
+ return { healed: true, action: "global-hook", from: hookPath };
123
+ }
124
+ catch {
125
+ return { healed: false, action: "none" };
126
+ }
127
+ }
128
+ /**
129
+ * Backward symlink: during postinstall, if the registry points to a
130
+ * non-existent OLD path, create a symlink from old → new (our directory).
131
+ * Same as healRegistryMismatch but called from postinstall context.
132
+ */
133
+ export { healRegistryMismatch as healBackwardCompat };
134
+ /** One-shot flag for mid-session heal in server.ts */
135
+ let _midSessionHealed = false;
136
+ /**
137
+ * Mid-session heal — call on first MCP tool invocation.
138
+ * Checks if registry path differs from our running directory.
139
+ * Creates symlink if needed. Runs only once per process.
140
+ */
141
+ export function healMidSession(currentDir) {
142
+ if (_midSessionHealed)
143
+ return { healed: false, action: "none" };
144
+ _midSessionHealed = true;
145
+ return healRegistryMismatch(currentDir);
146
+ }
147
+ /** Reset mid-session flag (for testing only) */
148
+ export function _resetMidSession() {
149
+ _midSessionHealed = false;
150
+ }
package/build/server.js CHANGED
@@ -3,7 +3,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
3
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
4
  import { createRequire } from "node:module";
5
5
  import { createHash } from "node:crypto";
6
- import { existsSync, unlinkSync, readdirSync, readFileSync, writeFileSync, rmSync, mkdirSync, cpSync, statSync } from "node:fs";
6
+ import { existsSync, unlinkSync, readdirSync, readFileSync, writeFileSync, rmSync, mkdirSync, cpSync, statSync, symlinkSync } from "node:fs";
7
7
  import { execSync } from "node:child_process";
8
8
  import { join, dirname, resolve } from "node:path";
9
9
  import { fileURLToPath } from "node:url";
@@ -262,7 +262,42 @@ function shouldShowVersionWarning() {
262
262
  _warningBurstCount++;
263
263
  return true;
264
264
  }
265
+ // ── Self-heal Layer 2: Mid-session registry heal (anthropics/claude-code#46915) ──
266
+ // Runs once on first tool call. If Claude Code auto-updated the registry mid-session,
267
+ // hooks break because CLAUDE_PLUGIN_ROOT points to a deleted directory. We create a
268
+ // symlink from the broken path to our actual directory so hooks recover.
269
+ let _cacheHealDone = false;
270
+ function healCacheMidSession() {
271
+ if (_cacheHealDone)
272
+ return;
273
+ _cacheHealDone = true;
274
+ try {
275
+ const ipPath = resolve(homedir(), ".claude", "plugins", "installed_plugins.json");
276
+ if (!existsSync(ipPath))
277
+ return;
278
+ const ip = JSON.parse(readFileSync(ipPath, "utf-8"));
279
+ for (const [key, entries] of Object.entries((ip.plugins ?? {}))) {
280
+ if (!key.toLowerCase().includes("context-mode"))
281
+ continue;
282
+ for (const entry of entries) {
283
+ const rp = entry.installPath;
284
+ if (!rp || existsSync(rp))
285
+ continue;
286
+ const parent = dirname(rp);
287
+ if (!existsSync(parent))
288
+ mkdirSync(parent, { recursive: true });
289
+ const target = resolve(__pkg_dir, "..");
290
+ if (existsSync(target)) {
291
+ symlinkSync(target, rp, process.platform === "win32" ? "junction" : undefined);
292
+ }
293
+ }
294
+ }
295
+ }
296
+ catch { /* best effort */ }
297
+ }
265
298
  function trackResponse(toolName, response) {
299
+ // Mid-session cache heal — one-shot, first tool call
300
+ healCacheMidSession();
266
301
  // Prepend version outdated warning if needed
267
302
  if (shouldShowVersionWarning() && response.content.length > 0) {
268
303
  const hint = getUpgradeHint();