forked from mksglu/context-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.mjs
More file actions
266 lines (250 loc) · 11.4 KB
/
Copy pathstart.mjs
File metadata and controls
266 lines (250 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env node
import { execSync } from "node:child_process";
import { existsSync, chmodSync, readFileSync, writeFileSync, readdirSync, symlinkSync, mkdirSync, lstatSync, unlinkSync } from "node:fs";
import { dirname, resolve, join, sep } from "node:path";
import { fileURLToPath } from "node:url";
import { homedir } from "node:os";
const __dirname = dirname(fileURLToPath(import.meta.url));
const originalCwd = process.cwd();
process.chdir(__dirname);
if (!process.env.CLAUDE_PROJECT_DIR) {
process.env.CLAUDE_PROJECT_DIR = originalCwd;
}
// Platform-agnostic project dir — guaranteed to be set for ALL platforms.
// Adapters may set their own env var (GEMINI_PROJECT_DIR, etc.) but this
// is the universal fallback so server.ts getProjectDir() never relies on cwd().
if (!process.env.CONTEXT_MODE_PROJECT_DIR) {
process.env.CONTEXT_MODE_PROJECT_DIR = originalCwd;
}
// Routing instructions file auto-write DISABLED for all platforms (#158, #164).
// Env vars like CLAUDE_SESSION_ID may not be set at MCP startup time, making
// the hook-capability guard unreliable. Writing to project dirs dirties git trees
// and causes double context injection on hook-capable platforms.
// Routing is handled by:
// - Hook-capable platforms: SessionStart hook injects ROUTING_BLOCK
// - Non-hook platforms: server.ts writeRoutingInstructions() on MCP connect
// - Future: explicit `context-mode init` command
// ── Self-heal Layer 1: Fix registry → symlink mismatches (anthropics/claude-code#46915) ──
// Claude Code auto-update can leave installed_plugins.json pointing to a non-existent
// directory. We detect this and create symlinks so hooks find the right path.
const cacheMatch = __dirname.match(
/^(.*[\/\\]plugins[\/\\]cache[\/\\][^\/\\]+[\/\\][^\/\\]+[\/\\])([^\/\\]+)$/,
);
if (cacheMatch) {
try {
const cacheParent = cacheMatch[1];
const myVersion = cacheMatch[2];
const ipPath = resolve(homedir(), ".claude", "plugins", "installed_plugins.json");
// Forward heal: if a newer version dir exists, update registry
const dirs = readdirSync(cacheParent).filter((d) =>
/^\d+\.\d+\.\d+/.test(d),
);
if (dirs.length > 1) {
dirs.sort((a, b) => {
const pa = a.split(".").map(Number);
const pb = b.split(".").map(Number);
for (let i = 0; i < 3; i++) {
if ((pa[i] ?? 0) !== (pb[i] ?? 0))
return (pa[i] ?? 0) - (pb[i] ?? 0);
}
return 0;
});
const newest = dirs[dirs.length - 1];
if (newest && newest !== myVersion) {
const ip = JSON.parse(readFileSync(ipPath, "utf-8"));
for (const [key, entries] of Object.entries(ip.plugins || {})) {
if (key !== "context-mode@context-mode") continue;
for (const entry of entries) {
entry.installPath = resolve(cacheParent, newest);
entry.version = newest;
entry.lastUpdated = new Date().toISOString();
}
}
writeFileSync(ipPath, JSON.stringify(ip, null, 2) + "\n", "utf-8");
}
}
// Reverse heal: if registry points to non-existent dir, create symlink to us
const cacheRoot = resolve(homedir(), ".claude", "plugins", "cache");
if (existsSync(ipPath)) {
const ip = JSON.parse(readFileSync(ipPath, "utf-8"));
for (const [key, entries] of Object.entries(ip.plugins || {})) {
if (key !== "context-mode@context-mode") continue;
for (const entry of entries) {
const rp = entry.installPath;
if (!rp || existsSync(rp) || rp === __dirname) continue;
// Path traversal guard: only allow paths inside plugin cache
if (!resolve(rp).startsWith(cacheRoot + sep)) continue;
try {
// Remove dangling symlink before creating new one
try { if (lstatSync(rp).isSymbolicLink()) unlinkSync(rp); } catch {}
const rpParent = dirname(rp);
if (!existsSync(rpParent)) mkdirSync(rpParent, { recursive: true });
symlinkSync(__dirname, rp, process.platform === "win32" ? "junction" : undefined);
} catch { /* best effort */ }
}
}
}
} catch {
/* best effort — don't block server startup */
}
}
// ── Self-heal Layer 4: Deploy global SessionStart hook + register in settings.json ──
// This hook lives outside the plugin directory (~/.claude/hooks/) so it works
// even when the plugin cache is completely broken. It creates symlinks for any
// missing plugin cache directories on every session start.
// Pure Node.js — no bash dependency. Works on Windows, macOS (SIP), Linux.
//
// Brew node upgrade resilience:
// - On Unix we register the hook command as the bare script path. The script
// itself carries `#!/usr/bin/env node`, so `env` resolves node from PATH at
// runtime. This survives Brew/asdf/nvm upgrades that move node binaries.
// - On Windows there is no shebang; we fall back to "<execPath>" "<scriptPath>".
// - On every boot we self-heal stale "/opt/homebrew/Cellar/node/<ver>/..." paths
// left behind by older versions of this code.
try {
const { buildHookCommand, selfHealCacheHealHook, ensureShebangAndExecBit } =
await import("./hooks/cache-heal-utils.mjs");
const globalHooksDir = resolve(homedir(), ".claude", "hooks");
const healHookPath = resolve(globalHooksDir, "context-mode-cache-heal.mjs");
// Clean up old bash version if it exists
const oldBashHook = resolve(globalHooksDir, "context-mode-cache-heal.sh");
if (existsSync(oldBashHook)) {
try { unlinkSync(oldBashHook); } catch {}
}
if (!existsSync(healHookPath)) {
if (!existsSync(globalHooksDir)) mkdirSync(globalHooksDir, { recursive: true });
const healScript = `#!/usr/bin/env node
// context-mode plugin cache self-heal (auto-deployed)
// Fixes anthropics/claude-code#46915: auto-update breaks CLAUDE_PLUGIN_ROOT
// Pure Node.js — no bash/shell dependency.
import{existsSync,readdirSync,statSync,symlinkSync,lstatSync,unlinkSync,readFileSync}from"node:fs";
import{dirname,join,resolve,sep}from"node:path";
import{homedir}from"node:os";
try{
const f=resolve(homedir(),".claude","plugins","installed_plugins.json");
if(!existsSync(f))process.exit(0);
const cacheRoot=resolve(homedir(),".claude","plugins","cache");
const ip=JSON.parse(readFileSync(f,"utf-8"));
for(const[k,es]of Object.entries(ip.plugins||{})){
if(k!=="context-mode@context-mode")continue;
for(const e of es){
const p=e.installPath;
if(!p||existsSync(p))continue;
if(!resolve(p).startsWith(cacheRoot+sep))continue;
const parent=dirname(p);
if(!existsSync(parent))continue;
try{if(lstatSync(p).isSymbolicLink())unlinkSync(p)}catch{}
const dirs=readdirSync(parent).filter(d=>/^\\d+\\.\\d+/.test(d)&&statSync(join(parent,d)).isDirectory());
if(!dirs.length)continue;
dirs.sort((a,b)=>{const pa=a.split(".").map(Number),pb=b.split(".").map(Number);for(let i=0;i<3;i++){if((pa[i]||0)!==(pb[i]||0))return(pa[i]||0)-(pb[i]||0)}return 0});
try{symlinkSync(join(parent,dirs[dirs.length-1]),p,process.platform==="win32"?"junction":undefined)}catch{}
}
}
}catch{}
`;
writeFileSync(healHookPath, healScript, { mode: 0o755 });
}
// Always re-assert shebang + chmod +x on Unix so the bare-script hook
// command is spawnable even if the file was created without exec bit.
if (process.platform !== "win32") {
try { ensureShebangAndExecBit(healHookPath); } catch { /* best effort */ }
}
// Register the hook in ~/.claude/settings.json (Claude Code doesn't auto-discover hook files)
const settingsPath = resolve(homedir(), ".claude", "settings.json");
if (existsSync(settingsPath)) {
const settings = JSON.parse(readFileSync(settingsPath, "utf-8"));
const hooks = settings.hooks ?? {};
const sessionStart = hooks.SessionStart ?? [];
const alreadyRegistered = sessionStart.some((h) =>
h.hooks?.some((hh) => hh.command?.includes("context-mode-cache-heal")),
);
if (!alreadyRegistered) {
sessionStart.push({
hooks: [
{
type: "command",
command: buildHookCommand({
scriptPath: healHookPath,
platform: process.platform,
nodePath: process.execPath,
}),
},
],
});
hooks.SessionStart = sessionStart;
settings.hooks = hooks;
writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + "\n", "utf-8");
}
// Self-heal: rewrite an existing cache-heal hook command if it points at
// a node binary that no longer exists (Brew node upgrade scenario).
try {
selfHealCacheHealHook({
settingsPath,
scriptPath: healHookPath,
platform: process.platform,
nodePath: process.execPath,
});
} catch { /* best effort */ }
}
} catch { /* best effort */ }
// ── Self-heal Layer 5: Windows hooks.json + plugin.json normalization (#378) ──
// Static committed files use ${CLAUDE_PLUGIN_ROOT} placeholder + bare `node`.
// On Windows + Claude Code this hits cjs/loader:1479 because:
// 1. bare `node` may not resolve via PATH (Git Bash, see #369)
// 2. ${CLAUDE_PLUGIN_ROOT} can hit MSYS path mangling (#372)
// 3. backslash paths corrupt under shell quoting
// Rewrites placeholders to absolute paths using process.execPath (Datadog
// model). Idempotent — only writes when needed. Survives upgrades because
// it runs at every MCP boot.
//
// Skip under vitest: server.test.ts spawns this script from the repo root,
// and a mutated .claude-plugin/plugin.json poisons sibling tests that read
// the file (cli.test.ts). VITEST is inherited by spawned subprocesses.
if (!process.env.VITEST) {
try {
const { normalizeHooksOnStartup } = await import("./hooks/normalize-hooks.mjs");
normalizeHooksOnStartup({
pluginRoot: __dirname,
nodePath: process.execPath,
platform: process.platform,
});
} catch { /* best effort — never block server startup */ }
}
// Ensure native dependencies + ABI compatibility (shared with hooks via ensure-deps.mjs)
// ensure-deps handles better-sqlite3 install + ABI cache/rebuild automatically (#148, #203)
import "./hooks/ensure-deps.mjs";
// Also install pure-JS deps used by server
for (const pkg of ["turndown", "turndown-plugin-gfm", "@mixmark-io/domino"]) {
if (!existsSync(resolve(__dirname, "node_modules", pkg))) {
try {
execSync(`npm install ${pkg} --no-package-lock --no-save --silent`, {
cwd: __dirname,
stdio: "pipe",
timeout: 120000,
});
} catch { /* best effort */ }
}
}
// Self-heal: create CLI shim if cli.bundle.mjs is missing (marketplace installs)
if (!existsSync(resolve(__dirname, "cli.bundle.mjs")) && existsSync(resolve(__dirname, "build", "cli.js"))) {
const shimPath = resolve(__dirname, "cli.bundle.mjs");
writeFileSync(shimPath, '#!/usr/bin/env node\nawait import("./build/cli.js");\n');
if (process.platform !== "win32") chmodSync(shimPath, 0o755);
}
// Bundle exists (CI-built) — start instantly
if (existsSync(resolve(__dirname, "server.bundle.mjs"))) {
await import("./server.bundle.mjs");
} else {
// Dev or npm install — full build
if (!existsSync(resolve(__dirname, "node_modules"))) {
try {
execSync("npm install --silent", { cwd: __dirname, stdio: "pipe", timeout: 60000 });
} catch { /* best effort */ }
}
if (!existsSync(resolve(__dirname, "build", "server.js"))) {
try {
execSync("npx tsc --silent", { cwd: __dirname, stdio: "pipe", timeout: 30000 });
} catch { /* best effort */ }
}
await import("./build/server.js");
}