context-mode 1.0.96 → 1.0.98

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.
@@ -1,30 +1,73 @@
1
1
  /**
2
2
  * MCP readiness sentinel — checks if MCP server has started.
3
- * Server writes sentinel (containing its PID) after connect(),
4
- * hooks check before denying tools that redirect to MCP alternatives.
5
3
  *
6
- * Sentinel path: ${tmpdir()}/context-mode-mcp-ready-${process.ppid}
7
- * Both hooks and MCP server share the same ppid (Claude Code process).
4
+ * Server writes sentinel (containing its PID) after connect().
5
+ * Hooks scan for any live sentinel to detect MCP readiness.
6
+ *
7
+ * Fix for #347: Claude Code spawns hooks via `bash -c "node ..."` on Linux/WSL2.
8
+ * The intermediate shell makes process.ppid point to a transient bash PID, not
9
+ * Claude Code. Directory-scan + PID liveness probe works regardless of spawn topology.
10
+ *
11
+ * Sentinel path: <tmpRoot>/context-mode-mcp-ready-<MCP_PID>
12
+ * Scan: glob all context-mode-mcp-ready-* files, probe each PID.
8
13
  */
9
- import { readFileSync } from "node:fs";
14
+ import { readFileSync, readdirSync, unlinkSync } from "node:fs";
10
15
  import { tmpdir } from "node:os";
11
- import { resolve } from "node:path";
16
+ import { join } from "node:path";
17
+
18
+ const SENTINEL_PREFIX = "context-mode-mcp-ready-";
19
+
20
+ /** Resolve the temp root — hardcoded /tmp on Unix to avoid TMPDIR mismatch. */
21
+ export function sentinelDir() {
22
+ return process.platform === "win32" ? tmpdir() : "/tmp";
23
+ }
24
+
25
+ /**
26
+ * Build sentinel path for a given PID.
27
+ * Used by server.ts to write its own sentinel.
28
+ */
29
+ export function sentinelPathForPid(pid) {
30
+ return join(sentinelDir(), `${SENTINEL_PREFIX}${pid}`);
31
+ }
12
32
 
13
- /** Compute the sentinel file path, scoped to session via parent PID. */
33
+ /**
34
+ * @deprecated Use sentinelPathForPid(process.pid) from server.ts.
35
+ * Kept for backward compat during migration — tests that still
36
+ * write sentinels with process.ppid will work for one release cycle.
37
+ */
14
38
  export function sentinelPath() {
15
- return resolve(tmpdir(), `context-mode-mcp-ready-${process.ppid}`);
39
+ return join(sentinelDir(), `${SENTINEL_PREFIX}${process.ppid}`);
16
40
  }
17
41
 
18
42
  /**
19
- * Check if MCP server is alive by reading sentinel PID and probing it.
20
- * Handles stale sentinels from crashed servers (SIGKILL, OOM) — if the
21
- * PID in the sentinel is dead, returns false so hooks allow fallback.
43
+ * Check if any MCP server is alive by scanning sentinel files.
44
+ *
45
+ * Scans sentinelDir() for context-mode-mcp-ready-* files, reads the PID
46
+ * from each, and probes with kill(pid, 0). Cleans up stale sentinels
47
+ * from crashed servers.
48
+ *
49
+ * Handles:
50
+ * - PPID mismatch (WSL2 shell wrappers) — no ppid dependency
51
+ * - Stale sentinels (SIGKILL, OOM) — PID liveness check
52
+ * - TMPDIR mismatch — hardcoded /tmp on Unix
22
53
  */
23
54
  export function isMCPReady() {
24
55
  try {
25
- const pid = parseInt(readFileSync(sentinelPath(), "utf8"), 10);
26
- process.kill(pid, 0); // throws if process doesn't exist
27
- return true;
56
+ const dir = sentinelDir();
57
+ const files = readdirSync(dir).filter(f => f.startsWith(SENTINEL_PREFIX));
58
+ for (const f of files) {
59
+ const fullPath = join(dir, f);
60
+ try {
61
+ const pid = parseInt(readFileSync(fullPath, "utf8"), 10);
62
+ if (isNaN(pid)) continue;
63
+ process.kill(pid, 0); // throws if process doesn't exist
64
+ return true;
65
+ } catch {
66
+ // Dead PID or unreadable — clean up stale sentinel
67
+ try { unlinkSync(fullPath); } catch {}
68
+ }
69
+ }
70
+ return false;
28
71
  } catch {
29
72
  return false;
30
73
  }
@@ -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.96",
6
+ "version": "1.0.98",
7
7
  "sandbox": {
8
8
  "mode": "permissive",
9
9
  "filesystem_access": "full",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-mode",
3
- "version": "1.0.96",
3
+ "version": "1.0.98",
4
4
  "type": "module",
5
5
  "description": "MCP plugin that saves 98% of your context window. Works with Claude Code, Gemini CLI, VS Code Copilot, OpenCode, and Codex CLI. Sandboxed code execution, FTS5 knowledge base, and intent-driven search.",
6
6
  "author": "Mert Koseoğlu",
@@ -8,9 +8,9 @@
8
8
  * Creates a directory junction so npm's %~dp0\node_modules\... resolves.
9
9
  */
10
10
 
11
- import { existsSync, mkdirSync, readFileSync, writeFileSync, symlinkSync } from "node:fs";
11
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, symlinkSync, lstatSync, unlinkSync } from "node:fs";
12
12
  import { execSync } from "node:child_process";
13
- import { dirname, resolve, join } from "node:path";
13
+ import { dirname, resolve, join, sep } from "node:path";
14
14
  import { fileURLToPath } from "node:url";
15
15
  import { homedir } from "node:os";
16
16
 
@@ -32,34 +32,16 @@ try {
32
32
  const ipPath = resolve(homedir(), ".claude", "plugins", "installed_plugins.json");
33
33
  if (existsSync(ipPath)) {
34
34
  const ip = JSON.parse(readFileSync(ipPath, "utf-8"));
35
+ const cacheRoot = resolve(homedir(), ".claude", "plugins", "cache");
35
36
  for (const [key, entries] of Object.entries(ip.plugins || {})) {
36
- if (!key.toLowerCase().includes("context-mode")) continue;
37
- for (const entry of entries) {
38
- const rp = entry.installPath;
39
- if (!rp || existsSync(rp)) continue;
40
- // Registry points to a missing dir — symlink it to us
41
- const rpParent = dirname(rp);
42
- if (!existsSync(rpParent)) mkdirSync(rpParent, { recursive: true });
43
- try {
44
- symlinkSync(pkgRoot, rp, process.platform === "win32" ? "junction" : undefined);
45
- } catch { /* may fail if path is locked or permissions */ }
46
- }
47
- }
48
- }
49
- } catch { /* best effort — don't block install */ }
50
-
51
- // ── 0. Self-heal Layer 3: Backward symlink for stale registry (anthropics/claude-code#46915) ──
52
- // When this install completes, installed_plugins.json may still point to an old
53
- // non-existent path. Create a symlink from that old path → our new directory.
54
- try {
55
- const ipPath = resolve(homedir(), ".claude", "plugins", "installed_plugins.json");
56
- if (existsSync(ipPath)) {
57
- const ip = JSON.parse(readFileSync(ipPath, "utf-8"));
58
- for (const [key, entries] of Object.entries(ip.plugins || {})) {
59
- if (!key.toLowerCase().includes("context-mode")) continue;
37
+ if (key !== "context-mode@context-mode") continue;
60
38
  for (const entry of entries) {
61
39
  const rp = entry.installPath;
62
40
  if (!rp || existsSync(rp)) continue;
41
+ // Path traversal guard
42
+ if (!resolve(rp).startsWith(cacheRoot + sep)) continue;
43
+ // Remove dangling symlink
44
+ try { if (lstatSync(rp).isSymbolicLink()) unlinkSync(rp); } catch {}
63
45
  const rpParent = dirname(rp);
64
46
  if (!existsSync(rpParent)) mkdirSync(rpParent, { recursive: true });
65
47
  try {