context-mode 1.0.126 → 1.0.128
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/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/.openclaw-plugin/openclaw.plugin.json +1 -1
- package/.openclaw-plugin/package.json +1 -1
- package/build/cli.js +31 -0
- package/build/db-base.js +53 -4
- package/build/server.js +7 -0
- package/build/util/db-lock.d.ts +65 -0
- package/build/util/db-lock.js +166 -0
- package/build/util/project-dir.d.ts +13 -0
- package/build/util/project-dir.js +11 -2
- package/build/util/sibling-mcp.d.ts +79 -0
- package/build/util/sibling-mcp.js +181 -0
- package/cli.bundle.mjs +131 -131
- package/hooks/core/routing.mjs +114 -22
- package/hooks/gemini-cli/sessionstart.mjs +8 -6
- package/hooks/security.bundle.mjs +1 -0
- package/hooks/session-db.bundle.mjs +2 -2
- package/hooks/sessionstart.mjs +18 -0
- package/openclaw.plugin.json +1 -1
- package/package.json +3 -3
- package/scripts/plugin-cache-integrity.mjs +101 -21
- package/server.bundle.mjs +92 -92
|
@@ -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
|
+
}
|