context-mode 1.0.127 → 1.0.129

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.
@@ -0,0 +1,79 @@
1
+ /**
2
+ * sibling-mcp — discover & terminate previous-version MCP servers.
3
+ *
4
+ * Issue #559: `/ctx-upgrade` historically left the running MCP server
5
+ * alive after copying new files in-place + updating npm global. The next
6
+ * Claude Code launch spawned a fresh process from the new version, but
7
+ * the old one kept its open stdio + DB handles. Across enough upgrades
8
+ * users observed 5+ context-mode `start.mjs` processes pinned to RAM.
9
+ *
10
+ * This module provides two pure helpers:
11
+ *
12
+ * 1. `discoverSiblingMcpPids({ ownPid, ownPpid, platform, runCommand })`
13
+ * — enumerates node processes whose argv mentions the plugin
14
+ * `start.mjs` path under `~/.claude/plugins/{cache,marketplaces}/`.
15
+ * Excludes the caller's own pid + parent pid (Claude Code or the
16
+ * shell that spawned `/ctx-upgrade`). Cross-platform: POSIX uses
17
+ * `pgrep -f`, Windows uses PowerShell + Get-CimInstance.
18
+ *
19
+ * 2. `killSiblingMcpServers({ pids, ... })` — sends SIGTERM, polls
20
+ * liveness, escalates to SIGKILL after `timeoutMs` (default 1500
21
+ * ms) on stragglers. Returns a kill report so callers can surface
22
+ * a concise summary without leaking PIDs to user-facing logs.
23
+ *
24
+ * Both helpers accept dependency-injected `runCommand`, `isAlive`, and
25
+ * `sendSignal` parameters so tests can exercise the full behavior tree
26
+ * cross-platform without spawning real processes.
27
+ */
28
+ /** Inject `child_process.execFileSync` for tests. Must return stdout as utf-8. */
29
+ export type RunCommand = (cmd: string, args: readonly string[]) => string;
30
+ /** Inject `process.kill(pid, 0)` for tests. */
31
+ export type IsAlive = (pid: number) => boolean;
32
+ /** Inject `process.kill(pid, signal)` for tests. */
33
+ export type SendSignal = (pid: number, signal: NodeJS.Signals) => void;
34
+ export interface DiscoverOptions {
35
+ ownPid: number;
36
+ ownPpid: number;
37
+ /** `process.platform` injection. Defaults to live process.platform. */
38
+ platform?: NodeJS.Platform;
39
+ /** Test injection point — defaults to `child_process.execFileSync`. */
40
+ runCommand?: RunCommand;
41
+ }
42
+ export interface KillOptions {
43
+ pids: readonly number[];
44
+ /** Time to wait for SIGTERM to take effect before escalating. */
45
+ timeoutMs?: number;
46
+ /** Poll interval while waiting for SIGTERM. */
47
+ pollIntervalMs?: number;
48
+ isAlive?: IsAlive;
49
+ sendSignal?: SendSignal;
50
+ }
51
+ export interface KillReport {
52
+ /** PIDs that died after SIGTERM within `timeoutMs`. */
53
+ terminatedBySigterm: number;
54
+ /** PIDs that required SIGKILL escalation. */
55
+ terminatedBySigkill: number;
56
+ /** Sum of the two — used by the cli summary line. */
57
+ totalKilled: number;
58
+ }
59
+ /**
60
+ * Enumerate node MCP-server processes spawned from this plugin's
61
+ * start.mjs. Always returns an empty array on tool absence — never
62
+ * throws — so an upgrade is never blocked by a missing pgrep/PowerShell.
63
+ */
64
+ export declare function discoverSiblingMcpPids(opts: DiscoverOptions): number[];
65
+ /**
66
+ * Send SIGTERM to each PID, then poll for liveness. PIDs still alive
67
+ * after `timeoutMs` receive SIGKILL. Returns a per-signal report.
68
+ *
69
+ * Algorithm:
70
+ * 1. Fire SIGTERM at every pid (swallow ESRCH — already dead).
71
+ * 2. Poll every `pollIntervalMs` until either all pids are dead
72
+ * OR `timeoutMs` elapses.
73
+ * 3. For survivors: SIGKILL (swallow ESRCH).
74
+ * 4. Count via "died-while-we-watched": only PIDs that were observed
75
+ * alive at any point and then died are reported. PIDs that were
76
+ * already dead before SIGTERM (ESRCH on first send) are not
77
+ * counted — they were not ours to kill.
78
+ */
79
+ export declare function killSiblingMcpServers(opts: KillOptions): Promise<KillReport>;
@@ -0,0 +1,181 @@
1
+ /**
2
+ * sibling-mcp — discover & terminate previous-version MCP servers.
3
+ *
4
+ * Issue #559: `/ctx-upgrade` historically left the running MCP server
5
+ * alive after copying new files in-place + updating npm global. The next
6
+ * Claude Code launch spawned a fresh process from the new version, but
7
+ * the old one kept its open stdio + DB handles. Across enough upgrades
8
+ * users observed 5+ context-mode `start.mjs` processes pinned to RAM.
9
+ *
10
+ * This module provides two pure helpers:
11
+ *
12
+ * 1. `discoverSiblingMcpPids({ ownPid, ownPpid, platform, runCommand })`
13
+ * — enumerates node processes whose argv mentions the plugin
14
+ * `start.mjs` path under `~/.claude/plugins/{cache,marketplaces}/`.
15
+ * Excludes the caller's own pid + parent pid (Claude Code or the
16
+ * shell that spawned `/ctx-upgrade`). Cross-platform: POSIX uses
17
+ * `pgrep -f`, Windows uses PowerShell + Get-CimInstance.
18
+ *
19
+ * 2. `killSiblingMcpServers({ pids, ... })` — sends SIGTERM, polls
20
+ * liveness, escalates to SIGKILL after `timeoutMs` (default 1500
21
+ * ms) on stragglers. Returns a kill report so callers can surface
22
+ * a concise summary without leaking PIDs to user-facing logs.
23
+ *
24
+ * Both helpers accept dependency-injected `runCommand`, `isAlive`, and
25
+ * `sendSignal` parameters so tests can exercise the full behavior tree
26
+ * cross-platform without spawning real processes.
27
+ */
28
+ import { execFileSync } from "node:child_process";
29
+ // Match BOTH `~/.claude/plugins/cache/context-mode/context-mode/<v>/start.mjs`
30
+ // AND `~/.claude/plugins/marketplaces/context-mode/start.mjs` shapes.
31
+ // Both can be alive concurrently — VERDICT R1 dump confirmed all four
32
+ // PIDs simultaneously across three different versions on a real Mac.
33
+ const POSIX_PGREP_PATTERN = "node.*plugins/(cache|marketplaces)/.*context-mode.*start\\.mjs";
34
+ // Windows: PowerShell + Get-CimInstance (wmic deprecated since Win11 22H2).
35
+ // Filter on CommandLine because Win32_Process.Name is just "node.exe".
36
+ // Two backslashes inside `start\.mjs` are needed because the Like operator
37
+ // uses regex-ish escaping at the JS layer.
38
+ const WIN_PS_SCRIPT = "Get-CimInstance Win32_Process " +
39
+ "-Filter \"Name='node.exe'\" | " +
40
+ "Where-Object { $_.CommandLine -match 'plugins[\\\\/](cache|marketplaces)[\\\\/].*context-mode.*start\\.mjs' } | " +
41
+ "Select-Object -ExpandProperty ProcessId";
42
+ const defaultRun = (cmd, args) => execFileSync(cmd, [...args], { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] });
43
+ const defaultIsAlive = (pid) => {
44
+ try {
45
+ process.kill(pid, 0);
46
+ return true;
47
+ }
48
+ catch {
49
+ return false;
50
+ }
51
+ };
52
+ const defaultSendSignal = (pid, sig) => {
53
+ // Throws ESRCH if the process is already dead — callers must swallow.
54
+ process.kill(pid, sig);
55
+ };
56
+ /**
57
+ * Parse newline-separated PID output. Tolerates header rows
58
+ * (`ProcessId`, `----------`), surrounding whitespace, and empty lines.
59
+ * Returns deduplicated, validated integers only.
60
+ */
61
+ function parsePidList(stdout) {
62
+ const seen = new Set();
63
+ for (const raw of stdout.split(/\r?\n/)) {
64
+ const trimmed = raw.trim();
65
+ if (!trimmed)
66
+ continue;
67
+ if (!/^\d+$/.test(trimmed))
68
+ continue;
69
+ const n = Number.parseInt(trimmed, 10);
70
+ if (Number.isFinite(n) && n > 0)
71
+ seen.add(n);
72
+ }
73
+ return [...seen];
74
+ }
75
+ /**
76
+ * Enumerate node MCP-server processes spawned from this plugin's
77
+ * start.mjs. Always returns an empty array on tool absence — never
78
+ * throws — so an upgrade is never blocked by a missing pgrep/PowerShell.
79
+ */
80
+ export function discoverSiblingMcpPids(opts) {
81
+ const platform = opts.platform ?? process.platform;
82
+ const run = opts.runCommand ?? defaultRun;
83
+ let stdout = "";
84
+ try {
85
+ if (platform === "win32") {
86
+ stdout = run("powershell", ["-NoProfile", "-Command", WIN_PS_SCRIPT]);
87
+ }
88
+ else {
89
+ // pgrep exits 1 when no matches; execFileSync throws on non-zero.
90
+ // Treat that as "no siblings" rather than an error.
91
+ stdout = run("pgrep", ["-f", POSIX_PGREP_PATTERN]);
92
+ }
93
+ }
94
+ catch {
95
+ return [];
96
+ }
97
+ return parsePidList(stdout).filter((pid) => pid !== opts.ownPid && pid !== opts.ownPpid);
98
+ }
99
+ /** Sleep helper — Promise-based for use inside the kill polling loop. */
100
+ function delay(ms) {
101
+ return new Promise((resolve) => { setTimeout(resolve, ms); });
102
+ }
103
+ /**
104
+ * Send SIGTERM to each PID, then poll for liveness. PIDs still alive
105
+ * after `timeoutMs` receive SIGKILL. Returns a per-signal report.
106
+ *
107
+ * Algorithm:
108
+ * 1. Fire SIGTERM at every pid (swallow ESRCH — already dead).
109
+ * 2. Poll every `pollIntervalMs` until either all pids are dead
110
+ * OR `timeoutMs` elapses.
111
+ * 3. For survivors: SIGKILL (swallow ESRCH).
112
+ * 4. Count via "died-while-we-watched": only PIDs that were observed
113
+ * alive at any point and then died are reported. PIDs that were
114
+ * already dead before SIGTERM (ESRCH on first send) are not
115
+ * counted — they were not ours to kill.
116
+ */
117
+ export async function killSiblingMcpServers(opts) {
118
+ const timeoutMs = opts.timeoutMs ?? 1500;
119
+ const pollIntervalMs = opts.pollIntervalMs ?? 100;
120
+ const isAlive = opts.isAlive ?? defaultIsAlive;
121
+ const sendSignal = opts.sendSignal ?? defaultSendSignal;
122
+ const empty = { terminatedBySigterm: 0, terminatedBySigkill: 0, totalKilled: 0 };
123
+ if (opts.pids.length === 0)
124
+ return empty;
125
+ // Track which PIDs we observed alive — we only count those.
126
+ const observedAlive = new Set();
127
+ const pendingTerm = new Set();
128
+ // Phase 1 — SIGTERM fan-out.
129
+ for (const pid of opts.pids) {
130
+ if (isAlive(pid)) {
131
+ observedAlive.add(pid);
132
+ pendingTerm.add(pid);
133
+ }
134
+ try {
135
+ sendSignal(pid, "SIGTERM");
136
+ }
137
+ catch (err) {
138
+ const code = err?.code;
139
+ if (code !== "ESRCH") {
140
+ // Permission errors etc. — drop from pending; cannot kill.
141
+ pendingTerm.delete(pid);
142
+ }
143
+ }
144
+ }
145
+ // Phase 2 — poll until either all dead or timeout.
146
+ const deadline = Date.now() + timeoutMs;
147
+ let terminatedBySigterm = 0;
148
+ while (pendingTerm.size > 0 && Date.now() < deadline) {
149
+ await delay(pollIntervalMs);
150
+ for (const pid of [...pendingTerm]) {
151
+ if (!isAlive(pid)) {
152
+ pendingTerm.delete(pid);
153
+ terminatedBySigterm++;
154
+ }
155
+ }
156
+ }
157
+ // Phase 3 — SIGKILL survivors.
158
+ let terminatedBySigkill = 0;
159
+ for (const pid of pendingTerm) {
160
+ try {
161
+ sendSignal(pid, "SIGKILL");
162
+ }
163
+ catch (err) {
164
+ const code = err?.code;
165
+ if (code === "ESRCH") {
166
+ // Died between the last poll and SIGKILL — count as SIGTERM win.
167
+ terminatedBySigterm++;
168
+ continue;
169
+ }
170
+ // Other error: skip — best-effort.
171
+ continue;
172
+ }
173
+ if (observedAlive.has(pid))
174
+ terminatedBySigkill++;
175
+ }
176
+ return {
177
+ terminatedBySigterm,
178
+ terminatedBySigkill,
179
+ totalKilled: terminatedBySigterm + terminatedBySigkill,
180
+ };
181
+ }