context-mode 1.0.18 → 1.0.20
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/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/hooks/core/formatters.mjs +6 -2
- package/package.json +1 -1
- package/server.bundle.mjs +66 -66
- package/build/sync/batcher.d.ts +0 -23
- package/build/sync/batcher.js +0 -74
- package/build/sync/cloud-post.d.ts +0 -12
- package/build/sync/cloud-post.js +0 -38
- package/build/sync/config.d.ts +0 -4
- package/build/sync/config.js +0 -64
- package/build/sync/index.d.ts +0 -12
- package/build/sync/index.js +0 -55
- package/build/sync/sanitizer.d.ts +0 -13
- package/build/sync/sanitizer.js +0 -86
- package/build/sync/sender.d.ts +0 -15
- package/build/sync/sender.js +0 -30
- package/build/sync/types.d.ts +0 -31
- package/build/sync/types.js +0 -1
|
@@ -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.20"
|
|
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.20",
|
|
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.20",
|
|
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
|
}
|
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.20";
|
|
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.
|