Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit 219022b

Browse files
Fix review findings: beforeExit handler, process-exit docs
Domain expert review found TS MCP had no safety net for event loop drain or editor disconnect without signal. Added beforeExit + exit handlers with idempotency guard (_hasWrapped flag). Updated CLAUDE.md.snippet to mention process-exit consolidation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b07f7f8 commit 219022b

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

examples/CLAUDE.md.snippet

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ like sleep for memory. It prunes dormant nodes to the audit trail, graduates
3737
frequently-used knowledge through temporal tiers (current → developing →
3838
proven → foundation), and saves. Without regular wraps, knowledge
3939
accumulates as noise instead of maturing. Each wrap makes the graph smarter.
40+
NOTE: A final consolidation also runs automatically on process exit (editor
41+
close, signal), but explicit wraps produce the best results because you can
42+
review and store final reasoning before consolidation happens.
4043

4144
Queries keep knowledge alive — every `query_tensions`, `query_blocked`, etc.
4245
touches the returned nodes, driving graduation. Knowledge that keeps coming

src/mcp-server.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,19 @@ const memory = isDemo ? seedDemoMemory(memoryPath) : Memory.loadOrCreate(memoryP
146146

147147
// Graceful shutdown: attempt a full session wrap (consolidation) on exit.
148148
// Falls back to save-only if wrap throws (crash safety > data loss).
149+
let _hasWrapped = false;
149150
function gracefulShutdown() {
151+
if (_hasWrapped) return;
152+
_hasWrapped = true;
150153
try { memory.sessionWrap(); } catch {
151154
try { memory.save(); } catch { /* best effort */ }
152155
}
153-
process.exit(0);
154156
}
155-
process.on('SIGTERM', gracefulShutdown);
156-
process.on('SIGINT', gracefulShutdown);
157+
process.on('SIGTERM', () => { gracefulShutdown(); process.exit(0); });
158+
process.on('SIGINT', () => { gracefulShutdown(); process.exit(0); });
159+
// Safety net for natural event loop drain or editor disconnect without signal
160+
process.on('beforeExit', gracefulShutdown);
161+
process.on('exit', gracefulShutdown);
157162

158163
// Helper: validate required args exist
159164
function requireArgs(args: Record<string, unknown> | undefined, ...keys: string[]): string | null {

0 commit comments

Comments
 (0)