context-mode 1.0.163 → 1.0.165

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/start.mjs CHANGED
@@ -77,18 +77,60 @@ if (typeof globalThis.Bun === "undefined" && process.platform === "linux") {
77
77
  stdio: ["pipe", "inherit", "inherit"],
78
78
  env: process.env,
79
79
  });
80
+ const _keepAlive = setInterval(() => {}, 2147483647);
81
+ let _escTerm;
82
+ let _escKill;
83
+ let _tearingDown = false;
84
+ // #862: propagate parent death to the Bun child. When the MCP client (e.g.
85
+ // Claude Code) exits, its end of our stdin pipe closes. The original proxy
86
+ // ignored that ("end" was a no-op) and parked forever — so the child was
87
+ // never told the session was gone: its stdin (this pipe) stayed open and its
88
+ // direct parent (us) stayed alive, defeating BOTH paths of the child's
89
+ // lifecycle guard (the stdio-EOF assist AND the ppid poll). The pair
90
+ // orphaned to init and pinned a CPU core indefinitely. We now forward EOF
91
+ // (graceful self-reap via the child's own watchdog), then escalate
92
+ // SIGTERM → SIGKILL so a wedged child can never outlive its client. Still
93
+ // re-execs under Bun first, so #564's SIGSEGV avoidance is untouched.
94
+ const teardown = () => {
95
+ if (_tearingDown) return;
96
+ _tearingDown = true;
97
+ clearInterval(_keepAlive);
98
+ try {
99
+ if (child.stdin && !child.stdin.destroyed) child.stdin.end();
100
+ } catch {}
101
+ // NOT unref'd: these short-lived timers are what hold the event loop
102
+ // open through the teardown window (≤5 s). Liveness must not depend on
103
+ // the top-level `await new Promise()` below surviving a future refactor
104
+ // — if escalation is ever skipped, #862's orphan returns. child.on(
105
+ // "exit") clears both the instant the child reaps cleanly.
106
+ _escTerm = setTimeout(() => {
107
+ try {
108
+ child.kill("SIGTERM");
109
+ } catch {}
110
+ }, 2000);
111
+ _escKill = setTimeout(() => {
112
+ try {
113
+ child.kill("SIGKILL");
114
+ } catch {}
115
+ process.exit(0);
116
+ }, 5000);
117
+ };
80
118
  process.stdin.on("data", (chunk) => {
81
119
  if (!child.stdin.destroyed) child.stdin.write(chunk);
82
120
  });
83
- process.stdin.on("end", () => {});
84
- const _keepAlive = setInterval(() => {}, 2147483647);
121
+ // EOF, pipe close, or pipe error all mean the client is gone — tear down.
122
+ process.stdin.on("end", teardown);
123
+ process.stdin.on("close", teardown);
124
+ process.stdin.on("error", teardown);
85
125
  child.on("exit", (code) => {
86
126
  clearInterval(_keepAlive);
127
+ if (_escTerm) clearTimeout(_escTerm);
128
+ if (_escKill) clearTimeout(_escKill);
87
129
  process.exit(code ?? 0);
88
130
  });
89
131
  // Prevent rest of start.mjs from running — child owns the MCP session.
90
132
  process.stdin.resume();
91
- await new Promise(() => {}); // park this process forever
133
+ await new Promise(() => {}); // park until the child exits (see child 'exit')
92
134
  }
93
135
  }
94
136
 
@@ -445,21 +487,51 @@ import "./hooks/ensure-deps.mjs";
445
487
  const NPM_INSTALL_BG_PKGS = ["turndown", "turndown-plugin-gfm", "@mixmark-io/domino"];
446
488
  const IS_WIN32 = process.platform === "win32";
447
489
  const NPM_BIN = IS_WIN32 ? "npm.cmd" : "npm";
490
+ const NPM_FLAGS = ["--no-package-lock", "--no-save", "--silent", "--no-audit", "--no-fund"];
491
+ // #861: on Windows the npm shim is `npm.cmd`, which needs `shell: true` to
492
+ // run — but Node DROPS the `cwd` option when `shell: true`, so the spawned
493
+ // cmd.exe inherits an arbitrary working dir (C:\Windows under Claude Code).
494
+ // `npm install` then tries to create `C:\Windows\node_modules` → EPERM on
495
+ // every boot, and a cmd.exe window flashes each time. Prefer running npm's
496
+ // own CLI through node directly (no `.cmd` shim, no shell): `shell: false`
497
+ // honors `cwd` and `windowsHide` suppresses the console window. Fall back to
498
+ // the shim only when npm-cli.js can't be located, so a working host (e.g. a
499
+ // POSIX layout where npm-cli.js isn't beside node) can never regress.
500
+ const NPM_CLI_JS = resolve(dirname(process.execPath), "node_modules", "npm", "bin", "npm-cli.js");
501
+ const useNodeCli = existsSync(NPM_CLI_JS);
448
502
  for (const pkg of NPM_INSTALL_BG_PKGS) {
449
503
  if (existsSync(resolve(__dirname, "node_modules", pkg))) continue;
450
504
  try {
451
- const child = spawn(
452
- NPM_BIN,
453
- ["install", pkg, "--no-package-lock", "--no-save", "--silent", "--no-audit", "--no-fund"],
454
- {
455
- cwd: __dirname,
456
- stdio: "ignore",
457
- detached: true,
458
- // npm on Windows ships as a `.cmd` shim — must go through cmd.exe.
459
- shell: IS_WIN32,
460
- },
461
- );
462
- child.on("error", () => { /* best effort — npm missing, broken cache, etc. */ });
505
+ const child = useNodeCli
506
+ ? spawn(process.execPath, [NPM_CLI_JS, "install", pkg, ...NPM_FLAGS], {
507
+ cwd: __dirname,
508
+ stdio: "ignore",
509
+ detached: true,
510
+ shell: false,
511
+ windowsHide: true,
512
+ })
513
+ : spawn(NPM_BIN, ["install", pkg, ...NPM_FLAGS], {
514
+ cwd: __dirname,
515
+ stdio: "ignore",
516
+ detached: true,
517
+ // npm on Windows ships as a `.cmd` shim — must go through cmd.exe.
518
+ shell: IS_WIN32,
519
+ windowsHide: true,
520
+ });
521
+ // #861: this EPERM was invisible for months behind stdio:"ignore" + an
522
+ // empty error handler. Surface both spawn failures and non-zero exits.
523
+ child.on("error", (err) => {
524
+ process.stderr.write(
525
+ `[context-mode] background install of ${pkg} failed to spawn: ${err?.message ?? err}\n`,
526
+ );
527
+ });
528
+ child.on("exit", (code) => {
529
+ if (code) {
530
+ process.stderr.write(
531
+ `[context-mode] background install of ${pkg} exited with code ${code}\n`,
532
+ );
533
+ }
534
+ });
463
535
  child.unref();
464
536
  } catch { /* best effort — never block MCP boot */ }
465
537
  }
@@ -1,141 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Cross-platform installer: register the context-mode plugin into Antigravity CLI (agy).
4
- *
5
- * Replaces the former bash-only scripts/install-antigravity-cli-plugin.sh so
6
- * `npm run install:agy` runs natively on Windows (PowerShell/cmd) as well as
7
- * macOS/Linux. agy itself runs on Windows, so its installer must too — unlike
8
- * the openclaw installer, which is genuinely POSIX-only (signals, pgrep, /tmp).
9
- *
10
- * The bundle (configs/antigravity-cli/) registers the context-mode MCP server,
11
- * routing rule, routing skill, and bounded PreToolUse/PostToolUse/Stop hooks in
12
- * one step.
13
- *
14
- * Usage: npm run install:agy (or: node scripts/install-antigravity-cli-plugin.mjs)
15
- */
16
- import { spawnSync } from "node:child_process";
17
- import { existsSync, rmSync } from "node:fs";
18
- import { homedir } from "node:os";
19
- import { dirname, join, resolve } from "node:path";
20
- import { fileURLToPath } from "node:url";
21
-
22
- const isWin = process.platform === "win32";
23
- const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
24
- const bundle = resolve(repoRoot, "configs", "antigravity-cli");
25
-
26
- // Double-quote a path so a clone dir with spaces survives the shell on both
27
- // cmd.exe and /bin/sh (paths never contain literal quotes).
28
- const q = (s) => `"${s}"`;
29
-
30
- // Cross-platform "is <cmd> on PATH?" — `where` on Windows, `command -v` on POSIX.
31
- // shell:true is required: `command -v` is a shell builtin and `where`/`agy` may
32
- // resolve to a .cmd shim on Windows that only the shell can launch.
33
- function onPath(cmd) {
34
- const probe = isWin ? `where ${cmd}` : `command -v ${cmd}`;
35
- return spawnSync(probe, { stdio: "ignore", shell: true }).status === 0;
36
- }
37
-
38
- // — preflight —
39
- if (!onPath("agy")) {
40
- console.error("✗ 'agy' (Antigravity CLI) not found in PATH. Install agy first, then re-run.");
41
- process.exit(1);
42
- }
43
- if (!existsSync(bundle)) {
44
- console.error(`✗ plugin bundle not found at ${bundle}`);
45
- process.exit(1);
46
- }
47
-
48
- console.log("→ context-mode agy plugin installer");
49
- console.log(` bundle : ${bundle}`);
50
-
51
- // The plugin's MCP server runs the global `context-mode` binary (it needs the
52
- // native better-sqlite3 dependency, which a bare clone does not have). Warn — do
53
- // not silently global-install on the user's behalf.
54
- const hasContextMode = onPath("context-mode");
55
- if (!hasContextMode) {
56
- console.error("⚠ 'context-mode' is not on PATH — the plugin's MCP server requires it.");
57
- console.error(" Install it with: npm install -g context-mode");
58
- }
59
-
60
- // Run `agy plugin install <bundle>`. String command + shell:true so cmd.exe can
61
- // resolve agy's .cmd shim on Windows; the quoted bundle path handles spaces.
62
- const install = spawnSync(`agy plugin install ${q(bundle)}`, { stdio: "inherit", shell: true });
63
- if (install.status !== 0) {
64
- console.error(`✗ 'agy plugin install' failed (exit ${install.status ?? "unknown"}).`);
65
- process.exit(install.status ?? 1);
66
- }
67
-
68
- // MCP is registered by `agy plugin install` above, straight from the bundle's
69
- // native `mcp_config.json` (env-pinned CONTEXT_MODE_PLATFORM=antigravity-cli).
70
- // Verified on agy 1.0.6: it logs "mcpServers : 1 processed" and writes the
71
- // server into agy's plugin profile
72
- // (~/.gemini/config/plugins/context-mode/mcp_config.json, env preserved).
73
- // agy native validation/install does not read `.mcp.json`; keep the bundle on
74
- // the single native `mcp_config.json` path to avoid manifest drift.
75
-
76
- // agy CACHES each MCP server's tool schemas under
77
- // ~/.gemini/antigravity-cli/mcp/<server>/ and does NOT refresh them on reconnect
78
- // (verified on agy 1.0.6). A cache captured by an older context-mode holds the
79
- // Gemini-incompatible schemas (`const` / `additionalProperties`) that make
80
- // Antigravity CLI silently DROP the ctx_* tools from the model's function list —
81
- // so even after upgrading, agy keeps hiding the tools and the agent works around
82
- // them via shell scripts. Clear the cache so agy re-fetches the current
83
- // (Gemini-safe) tools/list on its next launch.
84
- const agyToolCache = join(homedir(), ".gemini", "antigravity-cli", "mcp", "context-mode");
85
- let cacheCleared = false;
86
- if (existsSync(agyToolCache)) {
87
- try {
88
- rmSync(agyToolCache, { recursive: true, force: true });
89
- cacheCleared = true;
90
- } catch (err) {
91
- console.error(`⚠ Could not clear agy's stale tool-schema cache at ${agyToolCache}: ${err.message}`);
92
- console.error(" If ctx_* tools don't appear in agy, delete that folder manually and restart agy.");
93
- }
94
- }
95
-
96
- // Probe whether the global `context-mode` understands the antigravity-cli hooks.
97
- // The shipped hook commands resolve the GLOBAL binary at runtime. A context-mode
98
- // older than the release that added Antigravity CLI support may have no
99
- // `antigravity-cli` HOOK_MAP entry, and the dispatcher suppresses stderr, so the
100
- // hooks would be a SILENT no-op. Detect that here and tell the user instead.
101
- let hooksOk = false;
102
- if (hasContextMode) {
103
- hooksOk = ["pretooluse", "posttooluse", "stop"].every((event) => {
104
- const probe = spawnSync(`context-mode hook antigravity-cli ${event}`, {
105
- input: "{}",
106
- stdio: ["pipe", "ignore", "ignore"],
107
- shell: true,
108
- });
109
- return probe.status === 0;
110
- });
111
- }
112
-
113
- console.log("");
114
- // Confirm `agy plugin install` actually registered the MCP from the bundle's
115
- // MCP config (it writes it into agy's plugin profile). If a future agy build
116
- // skips it, fall back to a one-line manual instruction rather than silently
117
- // leaving MCP unconfigured — never re-introduce a blind global-profile write.
118
- const pluginMcp = join(homedir(), ".gemini", "config", "plugins", "context-mode", "mcp_config.json");
119
- if (existsSync(pluginMcp)) {
120
- console.log("✓ Installed the context-mode agy plugin: MCP server + routing rule + routing skill + hooks.");
121
- console.log(` MCP registered from the bundle's mcp_config.json → ${pluginMcp}`);
122
- } else {
123
- console.log("✓ Installed the context-mode agy plugin: routing rule + routing skill + hooks.");
124
- console.error("⚠ MCP server not found in agy's plugin profile. If ctx_* tools don't appear, add");
125
- console.error(' { "mcpServers": { "context-mode": { "command": "context-mode" } } }');
126
- console.error(" to ~/.gemini/config/mcp_config.json and restart agy.");
127
- }
128
- if (cacheCleared) {
129
- console.log("✓ Cleared agy's stale tool-schema cache — agy re-fetches Gemini-safe schemas on next launch.");
130
- }
131
- if (hooksOk) {
132
- console.log("✓ Antigravity CLI hooks are ACTIVE (this context-mode supports antigravity-cli).");
133
- } else {
134
- console.error("⚠ Antigravity CLI hooks may be INACTIVE: your global 'context-mode' is missing or too old");
135
- console.error(" to handle 'context-mode hook antigravity-cli'. MCP tools + the routing rule + routing skill still work.");
136
- console.error(" Enable hook enforcement/capture with: npm install -g context-mode@latest");
137
- }
138
- console.log("");
139
- console.log(" Restart agy, then verify:");
140
- console.log(' agy -p "Use the context-mode ctx_execute MCP tool to compute 7 + 5. Answer only the number." --dangerously-skip-permissions');
141
- console.log(" Expected output: 12");