context-mode 1.0.18 → 1.0.19
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/build/db-base.js +5 -0
- package/build/delegate.d.ts +48 -0
- package/build/delegate.js +265 -0
- package/build/executor.js +11 -4
- package/build/lifecycle.d.ts +21 -0
- package/build/lifecycle.js +68 -0
- package/build/server.js +4 -1
- package/build/session/db.js +0 -21
- package/cli.bundle.mjs +81 -81
- package/package.json +1 -1
- package/server.bundle.mjs +66 -66
|
@@ -6,14 +6,14 @@
|
|
|
6
6
|
},
|
|
7
7
|
"metadata": {
|
|
8
8
|
"description": "Claude Code plugins by Mert Koseoğlu",
|
|
9
|
-
"version": "1.0.
|
|
9
|
+
"version": "1.0.19"
|
|
10
10
|
},
|
|
11
11
|
"plugins": [
|
|
12
12
|
{
|
|
13
13
|
"name": "context-mode",
|
|
14
14
|
"source": "./",
|
|
15
15
|
"description": "Claude Code MCP plugin that saves 98% of your context window. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and intent-driven search.",
|
|
16
|
-
"version": "1.0.
|
|
16
|
+
"version": "1.0.19",
|
|
17
17
|
"author": {
|
|
18
18
|
"name": "Mert Koseoğlu"
|
|
19
19
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "context-mode",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
4
4
|
"description": "MCP server that saves 98% of your context window with session continuity. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and automatic state restore across compactions.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Mert Koseoğlu",
|
package/build/db-base.js
CHANGED
|
@@ -65,6 +65,11 @@ export function deleteDBFiles(dbPath) {
|
|
|
65
65
|
* always call this in a finally/cleanup path without try/catch.
|
|
66
66
|
*/
|
|
67
67
|
export function closeDB(db) {
|
|
68
|
+
try {
|
|
69
|
+
// Checkpoint WAL before close to prevent contention on restart (#103)
|
|
70
|
+
db.pragma("wal_checkpoint(TRUNCATE)");
|
|
71
|
+
}
|
|
72
|
+
catch { /* WAL may not be active */ }
|
|
68
73
|
try {
|
|
69
74
|
db.close();
|
|
70
75
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ctx_delegate — Distributed analysis via `claude --print` sub-agents.
|
|
3
|
+
*
|
|
4
|
+
* Spawns parallel Claude CLI subprocesses with pre-read file contents
|
|
5
|
+
* embedded in prompts. Each sub-agent performs single-turn analysis and
|
|
6
|
+
* returns a compressed summary. Results are indexed into FTS5 for follow-up search.
|
|
7
|
+
*
|
|
8
|
+
* Zero dependencies — uses `claude --print` (installed with Claude Code).
|
|
9
|
+
*/
|
|
10
|
+
export interface DelegateTask {
|
|
11
|
+
/** Human-readable label for this sub-agent's work */
|
|
12
|
+
name: string;
|
|
13
|
+
/** What the sub-agent should do — the analysis prompt */
|
|
14
|
+
prompt: string;
|
|
15
|
+
/** File paths to pre-read and embed. Directories are read recursively (.ts files). */
|
|
16
|
+
files?: string[];
|
|
17
|
+
}
|
|
18
|
+
export interface DelegateOptions {
|
|
19
|
+
tasks: DelegateTask[];
|
|
20
|
+
/** Model to use for sub-agents. Default: claude-sonnet-4-6 */
|
|
21
|
+
model?: string;
|
|
22
|
+
/** Per-task timeout in ms. Default: 90_000 (90s) */
|
|
23
|
+
timeout?: number;
|
|
24
|
+
/** Max concurrent sub-agents. Default: CPU count, max: 10 */
|
|
25
|
+
concurrency?: number;
|
|
26
|
+
}
|
|
27
|
+
export interface DelegateTaskResult {
|
|
28
|
+
name: string;
|
|
29
|
+
summary: string;
|
|
30
|
+
durationMs: number;
|
|
31
|
+
inputTokens: number;
|
|
32
|
+
outputTokens: number;
|
|
33
|
+
cacheReadTokens: number;
|
|
34
|
+
promptChars: number;
|
|
35
|
+
fileCount: number;
|
|
36
|
+
missingPaths: string[];
|
|
37
|
+
error?: string;
|
|
38
|
+
}
|
|
39
|
+
export interface DelegateResult {
|
|
40
|
+
results: DelegateTaskResult[];
|
|
41
|
+
wallTimeMs: number;
|
|
42
|
+
sequentialTimeMs: number;
|
|
43
|
+
speedup: number;
|
|
44
|
+
totalPromptTokens: number;
|
|
45
|
+
totalSummaryTokens: number;
|
|
46
|
+
compressionPct: number;
|
|
47
|
+
}
|
|
48
|
+
export declare function delegate(opts: DelegateOptions, projectRoot: string): Promise<DelegateResult>;
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ctx_delegate — Distributed analysis via `claude --print` sub-agents.
|
|
3
|
+
*
|
|
4
|
+
* Spawns parallel Claude CLI subprocesses with pre-read file contents
|
|
5
|
+
* embedded in prompts. Each sub-agent performs single-turn analysis and
|
|
6
|
+
* returns a compressed summary. Results are indexed into FTS5 for follow-up search.
|
|
7
|
+
*
|
|
8
|
+
* Zero dependencies — uses `claude --print` (installed with Claude Code).
|
|
9
|
+
*/
|
|
10
|
+
import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
11
|
+
import { join, relative } from "node:path";
|
|
12
|
+
import { cpus } from "node:os";
|
|
13
|
+
import { spawn } from "node:child_process";
|
|
14
|
+
// ── File Reading ───────────────────────────────────────────────────────
|
|
15
|
+
const CODE_EXTENSIONS = new Set([
|
|
16
|
+
".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs",
|
|
17
|
+
".py", ".rb", ".go", ".rs", ".java", ".c", ".cpp", ".h",
|
|
18
|
+
".php", ".pl", ".r", ".ex", ".exs", ".sh", ".bash", ".zsh",
|
|
19
|
+
".css", ".scss", ".html", ".json", ".yaml", ".yml", ".toml",
|
|
20
|
+
".md", ".txt", ".sql", ".graphql", ".proto",
|
|
21
|
+
]);
|
|
22
|
+
function isCodeFile(path) {
|
|
23
|
+
const dot = path.lastIndexOf(".");
|
|
24
|
+
if (dot === -1)
|
|
25
|
+
return false;
|
|
26
|
+
return CODE_EXTENSIONS.has(path.slice(dot).toLowerCase());
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Read a file or directory, returning file contents keyed by relative path.
|
|
30
|
+
* Directories are read recursively for code files.
|
|
31
|
+
*/
|
|
32
|
+
function readFilesForTask(paths, projectRoot) {
|
|
33
|
+
const files = new Map();
|
|
34
|
+
const missingPaths = [];
|
|
35
|
+
for (const p of paths) {
|
|
36
|
+
const abs = p.startsWith("/") ? p : join(projectRoot, p);
|
|
37
|
+
try {
|
|
38
|
+
const stat = statSync(abs);
|
|
39
|
+
if (stat.isDirectory()) {
|
|
40
|
+
// Recursive directory read
|
|
41
|
+
const entries = readdirSync(abs, { recursive: true });
|
|
42
|
+
for (const entry of entries) {
|
|
43
|
+
const entryAbs = join(abs, entry);
|
|
44
|
+
try {
|
|
45
|
+
if (!statSync(entryAbs).isFile())
|
|
46
|
+
continue;
|
|
47
|
+
if (!isCodeFile(entry))
|
|
48
|
+
continue;
|
|
49
|
+
const rel = relative(projectRoot, entryAbs);
|
|
50
|
+
files.set(rel, readFileSync(entryAbs, "utf-8"));
|
|
51
|
+
}
|
|
52
|
+
catch { /* skip unreadable files */ }
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else if (stat.isFile()) {
|
|
56
|
+
const rel = relative(projectRoot, abs);
|
|
57
|
+
files.set(rel, readFileSync(abs, "utf-8"));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
missingPaths.push(p);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
let content = "";
|
|
65
|
+
let totalChars = 0;
|
|
66
|
+
for (const [path, text] of files) {
|
|
67
|
+
content += `\n--- ${path} ---\n${text}\n`;
|
|
68
|
+
totalChars += text.length;
|
|
69
|
+
}
|
|
70
|
+
return { content, fileCount: files.size, totalChars, missingPaths };
|
|
71
|
+
}
|
|
72
|
+
// ── Sub-Agent Runner ───────────────────────────────────────────────────
|
|
73
|
+
async function runSubAgent(task, projectRoot, model, timeout) {
|
|
74
|
+
// Pre-read files and embed in prompt
|
|
75
|
+
let fileContent = "";
|
|
76
|
+
let fileCount = 0;
|
|
77
|
+
let missingPaths = [];
|
|
78
|
+
if (task.files && task.files.length > 0) {
|
|
79
|
+
const read = readFilesForTask(task.files, projectRoot);
|
|
80
|
+
fileContent = read.content;
|
|
81
|
+
fileCount = read.fileCount;
|
|
82
|
+
missingPaths = read.missingPaths;
|
|
83
|
+
// Fail fast: if files were requested but NONE found, don't spawn sub-agent
|
|
84
|
+
if (fileCount === 0 && missingPaths.length > 0) {
|
|
85
|
+
return {
|
|
86
|
+
name: task.name,
|
|
87
|
+
summary: `ERROR: No files found. Missing paths: ${missingPaths.join(", ")}`,
|
|
88
|
+
durationMs: 0,
|
|
89
|
+
inputTokens: 0,
|
|
90
|
+
outputTokens: 0,
|
|
91
|
+
cacheReadTokens: 0,
|
|
92
|
+
promptChars: 0,
|
|
93
|
+
fileCount: 0,
|
|
94
|
+
missingPaths,
|
|
95
|
+
error: "FILES_NOT_FOUND",
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const fullPrompt = fileContent
|
|
100
|
+
? `${task.prompt}\n\n${fileContent}`
|
|
101
|
+
: task.prompt;
|
|
102
|
+
// Strip CLAUDECODE env to prevent recursion detection
|
|
103
|
+
const cleanEnv = { ...process.env };
|
|
104
|
+
delete cleanEnv.CLAUDECODE;
|
|
105
|
+
// Propagate depth guard
|
|
106
|
+
const currentDepth = parseInt(process.env.CTX_DELEGATE_DEPTH ?? "0", 10);
|
|
107
|
+
cleanEnv.CTX_DELEGATE_DEPTH = String(currentDepth + 1);
|
|
108
|
+
const startTime = Date.now();
|
|
109
|
+
// Spawn `claude --print` — single-turn, no tools, inherits OAuth session
|
|
110
|
+
const args = ["--print", "--model", model, "--max-turns", "1", fullPrompt];
|
|
111
|
+
return new Promise((resolve) => {
|
|
112
|
+
const child = spawn("claude", args, {
|
|
113
|
+
env: cleanEnv,
|
|
114
|
+
cwd: projectRoot,
|
|
115
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
116
|
+
});
|
|
117
|
+
let stdout = "";
|
|
118
|
+
let stderr = "";
|
|
119
|
+
child.stdout.on("data", (chunk) => { stdout += chunk.toString(); });
|
|
120
|
+
child.stderr.on("data", (chunk) => { stderr += chunk.toString(); });
|
|
121
|
+
// Timeout guard
|
|
122
|
+
const timer = setTimeout(() => {
|
|
123
|
+
child.kill("SIGTERM");
|
|
124
|
+
setTimeout(() => { if (!child.killed)
|
|
125
|
+
child.kill("SIGKILL"); }, 5000);
|
|
126
|
+
}, timeout);
|
|
127
|
+
child.on("close", (code) => {
|
|
128
|
+
clearTimeout(timer);
|
|
129
|
+
const durationMs = Date.now() - startTime;
|
|
130
|
+
const summary = stdout.trim();
|
|
131
|
+
if (code !== 0 && !summary) {
|
|
132
|
+
resolve({
|
|
133
|
+
name: task.name,
|
|
134
|
+
summary: `ERROR: claude --print exited with code ${code}. ${stderr.trim().slice(0, 200)}`,
|
|
135
|
+
durationMs,
|
|
136
|
+
inputTokens: 0,
|
|
137
|
+
outputTokens: 0,
|
|
138
|
+
cacheReadTokens: 0,
|
|
139
|
+
promptChars: fullPrompt.length,
|
|
140
|
+
fileCount,
|
|
141
|
+
missingPaths,
|
|
142
|
+
error: `EXIT_CODE_${code}`,
|
|
143
|
+
});
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
resolve({
|
|
147
|
+
name: task.name,
|
|
148
|
+
summary: summary || "ERROR: Empty response from claude --print",
|
|
149
|
+
durationMs,
|
|
150
|
+
inputTokens: 0,
|
|
151
|
+
outputTokens: 0,
|
|
152
|
+
cacheReadTokens: 0,
|
|
153
|
+
promptChars: fullPrompt.length,
|
|
154
|
+
fileCount,
|
|
155
|
+
missingPaths,
|
|
156
|
+
error: summary ? undefined : "EMPTY_RESPONSE",
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
child.on("error", (err) => {
|
|
160
|
+
clearTimeout(timer);
|
|
161
|
+
resolve({
|
|
162
|
+
name: task.name,
|
|
163
|
+
summary: `ERROR: Failed to spawn claude CLI: ${err.message}. Is Claude Code installed?`,
|
|
164
|
+
durationMs: Date.now() - startTime,
|
|
165
|
+
inputTokens: 0,
|
|
166
|
+
outputTokens: 0,
|
|
167
|
+
cacheReadTokens: 0,
|
|
168
|
+
promptChars: fullPrompt.length,
|
|
169
|
+
fileCount: 0,
|
|
170
|
+
missingPaths,
|
|
171
|
+
error: "SPAWN_FAILED",
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
// ── Concurrency Limiter ────────────────────────────────────────────────
|
|
177
|
+
async function runWithConcurrency(tasks, projectRoot, model, timeout, limit) {
|
|
178
|
+
const results = [];
|
|
179
|
+
const queue = [...tasks];
|
|
180
|
+
const running = new Set();
|
|
181
|
+
while (queue.length > 0 || running.size > 0) {
|
|
182
|
+
while (running.size < limit && queue.length > 0) {
|
|
183
|
+
const task = queue.shift();
|
|
184
|
+
const p = runSubAgent(task, projectRoot, model, timeout).then((r) => {
|
|
185
|
+
results.push(r);
|
|
186
|
+
running.delete(p);
|
|
187
|
+
});
|
|
188
|
+
running.add(p);
|
|
189
|
+
}
|
|
190
|
+
if (running.size > 0)
|
|
191
|
+
await Promise.race(running);
|
|
192
|
+
}
|
|
193
|
+
return results;
|
|
194
|
+
}
|
|
195
|
+
// ── Public API ─────────────────────────────────────────────────────────
|
|
196
|
+
const MAX_CONCURRENCY = 10;
|
|
197
|
+
const MAX_TASKS = 20;
|
|
198
|
+
const MAX_DEPTH = 1;
|
|
199
|
+
const DEFAULT_TIMEOUT = 90_000;
|
|
200
|
+
const DEFAULT_MODEL = "claude-sonnet-4-6";
|
|
201
|
+
export async function delegate(opts, projectRoot) {
|
|
202
|
+
// Depth guard — prevent infinite delegation loops
|
|
203
|
+
const depth = parseInt(process.env.CTX_DELEGATE_DEPTH ?? "0", 10);
|
|
204
|
+
if (depth >= MAX_DEPTH) {
|
|
205
|
+
return {
|
|
206
|
+
results: [{
|
|
207
|
+
name: "depth-guard",
|
|
208
|
+
summary: "ERROR: Delegation depth limit reached. Sub-agents cannot delegate further.",
|
|
209
|
+
durationMs: 0,
|
|
210
|
+
inputTokens: 0,
|
|
211
|
+
outputTokens: 0,
|
|
212
|
+
cacheReadTokens: 0,
|
|
213
|
+
promptChars: 0,
|
|
214
|
+
fileCount: 0,
|
|
215
|
+
missingPaths: [],
|
|
216
|
+
error: "DEPTH_LIMIT",
|
|
217
|
+
}],
|
|
218
|
+
wallTimeMs: 0,
|
|
219
|
+
sequentialTimeMs: 0,
|
|
220
|
+
speedup: 0,
|
|
221
|
+
totalPromptTokens: 0,
|
|
222
|
+
totalSummaryTokens: 0,
|
|
223
|
+
compressionPct: 0,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
// Validate tasks
|
|
227
|
+
const tasks = opts.tasks.slice(0, MAX_TASKS);
|
|
228
|
+
if (tasks.length === 0) {
|
|
229
|
+
return {
|
|
230
|
+
results: [],
|
|
231
|
+
wallTimeMs: 0,
|
|
232
|
+
sequentialTimeMs: 0,
|
|
233
|
+
speedup: 0,
|
|
234
|
+
totalPromptTokens: 0,
|
|
235
|
+
totalSummaryTokens: 0,
|
|
236
|
+
compressionPct: 0,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
const model = opts.model ?? DEFAULT_MODEL;
|
|
240
|
+
const timeout = Math.min(opts.timeout ?? DEFAULT_TIMEOUT, 300_000);
|
|
241
|
+
const concurrency = Math.min(opts.concurrency ?? cpus().length, MAX_CONCURRENCY);
|
|
242
|
+
const wallStart = Date.now();
|
|
243
|
+
const results = await runWithConcurrency(tasks, projectRoot, model, timeout, concurrency);
|
|
244
|
+
const wallTimeMs = Date.now() - wallStart;
|
|
245
|
+
// Reorder results to match input task order
|
|
246
|
+
const ordered = tasks.map((t) => results.find((r) => r.name === t.name) ?? results[0]);
|
|
247
|
+
// Compute metrics
|
|
248
|
+
const sequentialTimeMs = ordered.reduce((s, r) => s + r.durationMs, 0);
|
|
249
|
+
const totalPromptChars = ordered.reduce((s, r) => s + r.promptChars, 0);
|
|
250
|
+
const totalSummaryChars = ordered.reduce((s, r) => s + r.summary.length, 0);
|
|
251
|
+
const totalPromptTokens = Math.ceil(totalPromptChars / 4);
|
|
252
|
+
const totalSummaryTokens = Math.ceil(totalSummaryChars / 4);
|
|
253
|
+
const compressionPct = totalPromptTokens > 0
|
|
254
|
+
? (1 - totalSummaryTokens / totalPromptTokens) * 100
|
|
255
|
+
: 0;
|
|
256
|
+
return {
|
|
257
|
+
results: ordered,
|
|
258
|
+
wallTimeMs,
|
|
259
|
+
sequentialTimeMs,
|
|
260
|
+
speedup: wallTimeMs > 0 ? sequentialTimeMs / wallTimeMs : 0,
|
|
261
|
+
totalPromptTokens,
|
|
262
|
+
totalSummaryTokens,
|
|
263
|
+
compressionPct,
|
|
264
|
+
};
|
|
265
|
+
}
|
package/build/executor.js
CHANGED
|
@@ -5,7 +5,7 @@ import { tmpdir } from "node:os";
|
|
|
5
5
|
import { detectRuntimes, buildCommand, } from "./runtime.js";
|
|
6
6
|
import { smartTruncate } from "./truncate.js";
|
|
7
7
|
const isWin = process.platform === "win32";
|
|
8
|
-
/** Kill process tree — on Windows
|
|
8
|
+
/** Kill process tree — on Windows uses taskkill /T; on Unix kills the process group. */
|
|
9
9
|
function killTree(proc) {
|
|
10
10
|
if (isWin && proc.pid) {
|
|
11
11
|
try {
|
|
@@ -13,8 +13,12 @@ function killTree(proc) {
|
|
|
13
13
|
}
|
|
14
14
|
catch { /* already dead */ }
|
|
15
15
|
}
|
|
16
|
-
else {
|
|
17
|
-
|
|
16
|
+
else if (proc.pid) {
|
|
17
|
+
try {
|
|
18
|
+
// Kill entire process group (negative PID) to prevent orphaned children
|
|
19
|
+
process.kill(-proc.pid, "SIGKILL");
|
|
20
|
+
}
|
|
21
|
+
catch { /* already dead */ }
|
|
18
22
|
}
|
|
19
23
|
}
|
|
20
24
|
export class PolyglotExecutor {
|
|
@@ -37,7 +41,8 @@ export class PolyglotExecutor {
|
|
|
37
41
|
cleanupBackgrounded() {
|
|
38
42
|
for (const pid of this.#backgroundedPids) {
|
|
39
43
|
try {
|
|
40
|
-
process
|
|
44
|
+
// Kill process group on Unix to catch all children
|
|
45
|
+
process.kill(isWin ? pid : -pid, "SIGTERM");
|
|
41
46
|
}
|
|
42
47
|
catch { /* already dead */ }
|
|
43
48
|
}
|
|
@@ -165,6 +170,8 @@ export class PolyglotExecutor {
|
|
|
165
170
|
stdio: ["ignore", "pipe", "pipe"],
|
|
166
171
|
env: this.#buildSafeEnv(cwd),
|
|
167
172
|
shell: needsShell,
|
|
173
|
+
// On Unix, create a new process group so killTree can kill all children
|
|
174
|
+
detached: !isWin,
|
|
168
175
|
});
|
|
169
176
|
let timedOut = false;
|
|
170
177
|
let resolved = false;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lifecycle — Process lifecycle guard for MCP server.
|
|
3
|
+
*
|
|
4
|
+
* Detects parent process death, stdin close, and OS signals to prevent
|
|
5
|
+
* orphaned MCP server processes consuming 100% CPU (issue #103).
|
|
6
|
+
*
|
|
7
|
+
* Cross-platform: macOS, Linux, Windows.
|
|
8
|
+
*/
|
|
9
|
+
export interface LifecycleGuardOptions {
|
|
10
|
+
/** Interval in ms to check parent liveness. Default: 30_000 */
|
|
11
|
+
checkIntervalMs?: number;
|
|
12
|
+
/** Called when parent death or stdin close is detected. */
|
|
13
|
+
onShutdown: () => void;
|
|
14
|
+
/** Injectable parent-alive check (for testing). Default: ppid-based check. */
|
|
15
|
+
isParentAlive?: () => boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Start the lifecycle guard. Returns a cleanup function.
|
|
19
|
+
* Skipped automatically when stdin is a TTY (e.g. OpenCode ts-plugin).
|
|
20
|
+
*/
|
|
21
|
+
export declare function startLifecycleGuard(opts: LifecycleGuardOptions): () => void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lifecycle — Process lifecycle guard for MCP server.
|
|
3
|
+
*
|
|
4
|
+
* Detects parent process death, stdin close, and OS signals to prevent
|
|
5
|
+
* orphaned MCP server processes consuming 100% CPU (issue #103).
|
|
6
|
+
*
|
|
7
|
+
* Cross-platform: macOS, Linux, Windows.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Default parent liveness check.
|
|
11
|
+
* Compares current ppid against the original — if it changed (reparented to
|
|
12
|
+
* init/launchd/systemd), parent is dead. This is more reliable than
|
|
13
|
+
* kill(ppid, 0) which succeeds for PID 1 on all platforms.
|
|
14
|
+
*
|
|
15
|
+
* On Windows, ppid becomes 0 when parent exits.
|
|
16
|
+
*/
|
|
17
|
+
const originalPpid = process.ppid;
|
|
18
|
+
function defaultIsParentAlive() {
|
|
19
|
+
const ppid = process.ppid;
|
|
20
|
+
if (ppid !== originalPpid)
|
|
21
|
+
return false;
|
|
22
|
+
if (ppid === 0 || ppid === 1)
|
|
23
|
+
return false;
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Start the lifecycle guard. Returns a cleanup function.
|
|
28
|
+
* Skipped automatically when stdin is a TTY (e.g. OpenCode ts-plugin).
|
|
29
|
+
*/
|
|
30
|
+
export function startLifecycleGuard(opts) {
|
|
31
|
+
const interval = opts.checkIntervalMs ?? 30_000;
|
|
32
|
+
const check = opts.isParentAlive ?? defaultIsParentAlive;
|
|
33
|
+
let stopped = false;
|
|
34
|
+
const shutdown = () => {
|
|
35
|
+
if (stopped)
|
|
36
|
+
return;
|
|
37
|
+
stopped = true;
|
|
38
|
+
opts.onShutdown();
|
|
39
|
+
};
|
|
40
|
+
// P0: Periodic parent liveness check
|
|
41
|
+
const timer = setInterval(() => {
|
|
42
|
+
if (!check())
|
|
43
|
+
shutdown();
|
|
44
|
+
}, interval);
|
|
45
|
+
timer.unref();
|
|
46
|
+
// P0: Stdin close — parent pipe broken
|
|
47
|
+
// Must resume stdin to receive close/end events (Node starts paused)
|
|
48
|
+
const onStdinClose = () => shutdown();
|
|
49
|
+
process.stdin.resume();
|
|
50
|
+
process.stdin.on("end", onStdinClose);
|
|
51
|
+
process.stdin.on("close", onStdinClose);
|
|
52
|
+
process.stdin.on("error", onStdinClose);
|
|
53
|
+
// P0: OS signals — terminal close, kill, ctrl+c
|
|
54
|
+
const signals = ["SIGTERM", "SIGINT"];
|
|
55
|
+
if (process.platform !== "win32")
|
|
56
|
+
signals.push("SIGHUP");
|
|
57
|
+
for (const sig of signals)
|
|
58
|
+
process.on(sig, shutdown);
|
|
59
|
+
return () => {
|
|
60
|
+
stopped = true;
|
|
61
|
+
clearInterval(timer);
|
|
62
|
+
process.stdin.removeListener("end", onStdinClose);
|
|
63
|
+
process.stdin.removeListener("close", onStdinClose);
|
|
64
|
+
process.stdin.removeListener("error", onStdinClose);
|
|
65
|
+
for (const sig of signals)
|
|
66
|
+
process.removeListener(sig, shutdown);
|
|
67
|
+
};
|
|
68
|
+
}
|
package/build/server.js
CHANGED
|
@@ -13,7 +13,8 @@ import { ContentStore, cleanupStaleDBs } from "./store.js";
|
|
|
13
13
|
import { readBashPolicies, evaluateCommandDenyOnly, extractShellCommands, readToolDenyPatterns, evaluateFilePath, } from "./security.js";
|
|
14
14
|
import { detectRuntimes, getRuntimeSummary, getAvailableLanguages, hasBunRuntime, } from "./runtime.js";
|
|
15
15
|
import { classifyNonZeroExit } from "./exit-classify.js";
|
|
16
|
-
|
|
16
|
+
import { startLifecycleGuard } from "./lifecycle.js";
|
|
17
|
+
const VERSION = "1.0.19";
|
|
17
18
|
// Prevent silent server death from unhandled async errors
|
|
18
19
|
process.on("unhandledRejection", (err) => {
|
|
19
20
|
process.stderr.write(`[context-mode] unhandledRejection: ${err}\n`);
|
|
@@ -1462,6 +1463,8 @@ async function main() {
|
|
|
1462
1463
|
process.on("exit", shutdown);
|
|
1463
1464
|
process.on("SIGINT", () => { gracefulShutdown(); });
|
|
1464
1465
|
process.on("SIGTERM", () => { gracefulShutdown(); });
|
|
1466
|
+
// Lifecycle guard: detect parent death + stdin close to prevent orphaned processes (#103)
|
|
1467
|
+
startLifecycleGuard({ onShutdown: () => gracefulShutdown() });
|
|
1465
1468
|
const transport = new StdioServerTransport();
|
|
1466
1469
|
await server.connect(transport);
|
|
1467
1470
|
// Write routing instructions for hookless platforms (e.g. Codex CLI)
|
package/build/session/db.js
CHANGED
|
@@ -7,8 +7,6 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { SQLiteBase, defaultDBPath } from "../db-base.js";
|
|
9
9
|
import { createHash } from "node:crypto";
|
|
10
|
-
import { execSync } from "node:child_process";
|
|
11
|
-
import { cloudPostEvent } from "../sync/cloud-post.js";
|
|
12
10
|
// ─────────────────────────────────────────────────────────
|
|
13
11
|
// Constants
|
|
14
12
|
// ─────────────────────────────────────────────────────────
|
|
@@ -190,25 +188,6 @@ export class SessionDB extends SQLiteBase {
|
|
|
190
188
|
this.stmt(S.updateMetaLastEvent).run(sessionId);
|
|
191
189
|
});
|
|
192
190
|
transaction();
|
|
193
|
-
// Fire-and-forget: POST event directly to cloud (works in short-lived hook processes)
|
|
194
|
-
try {
|
|
195
|
-
const projectDir = process.env.CLAUDE_PROJECT_DIR ?? process.cwd();
|
|
196
|
-
let gitRemote;
|
|
197
|
-
try {
|
|
198
|
-
gitRemote = execSync("git remote get-url origin", { cwd: projectDir, timeout: 2000 })
|
|
199
|
-
.toString().trim() || undefined;
|
|
200
|
-
}
|
|
201
|
-
catch { /* no git remote available */ }
|
|
202
|
-
cloudPostEvent({
|
|
203
|
-
type: event.type,
|
|
204
|
-
category: event.category,
|
|
205
|
-
priority: event.priority,
|
|
206
|
-
data: event.data,
|
|
207
|
-
source_hook: sourceHook,
|
|
208
|
-
created_at: new Date().toISOString(),
|
|
209
|
-
}, projectDir, sessionId, gitRemote);
|
|
210
|
-
}
|
|
211
|
-
catch { /* cloud sync must never break local event storage */ }
|
|
212
191
|
}
|
|
213
192
|
/**
|
|
214
193
|
* Retrieve events for a session with optional filtering.
|