A self-organizing Obsidian vault that gives AI coding agents persistent memory.
TypeScript ai-agentsclaude-codecodex-cligemini-cliknowledge-managementobsidianobsidian-vaultpersistent-memorysecond-braintemplate
fix: fail waiting qmd calls when the client's stdin breaks, instead of crashing (#275)
Brenno Ferrari0cb7904fdbfc
diff --git a/.claude/scripts/lib/mcp-qmd-client.ts b/.claude/scripts/lib/mcp-qmd-client.tsindex 4fcea16..fd75c99 100644--- a/.claude/scripts/lib/mcp-qmd-client.ts+++ b/.claude/scripts/lib/mcp-qmd-client.ts@@ -254,9 +254,9 @@ export interface QmdClient {* rather than hardcoding the path matters because #71 is actively moving hook* scripts, and a stale path here kills search silently.*/-export function createQmdClient(vaultRoot: string, launcherPath: string | null): QmdClient {+export function createQmdClient(vaultRoot: string, launcherPath: string | null, spawnChild: typeof spawn = spawn): QmdClient {const launcher = launcherPath ?? join(vaultRoot, ".claude", "scripts", "qmd-mcp.mjs");- const child: ChildProcess = spawn(process.execPath, [launcher], {+ const child: ChildProcess = spawnChild(process.execPath, [launcher], {stdio: ["pipe", "pipe", "ignore"],env: { ...process.env, CLAUDE_PROJECT_DIR: vaultRoot },});@@ -304,6 +304,11 @@ export function createQmdClient(vaultRoot: string, launcherPath: string | null):};child.on("error", (e) => failAll(`qmd launcher failed: ${e.message}`));child.on("exit", () => failAll("qmd launcher exited"));+ // A write to a child that is gone fails on the stream itself (EPIPE), not+ // on the child. With no listener that is an uncaughtException, which takes+ // the whole MCP server down; and no reply can come over a broken stdin, so+ // waiting calls fail now rather than at their budget.+ child.stdin?.on("error", (e) => failAll(`qmd launcher stdin closed: ${e.message}`));const call = (method: string, params?: unknown, timeoutMs?: number): Promise<unknown> =>new Promise((resolve, reject) => {diff --git a/.claude/scripts/tests/mcp-qmd-client.test.ts b/.claude/scripts/tests/mcp-qmd-client.test.tsindex 0fc2050..8690922 100644--- a/.claude/scripts/tests/mcp-qmd-client.test.ts+++ b/.claude/scripts/tests/mcp-qmd-client.test.ts@@ -10,6 +10,7 @@* does nothing at all, which is why the absence cases carry the weight.*/+import { spawn, type ChildProcess } from "node:child_process";import { test, describe } from "node:test";import assert from "node:assert/strict";import { mkdtempSync, rmSync, writeFileSync } from "node:fs";@@ -308,6 +309,28 @@ describe("client liveness", () => {assert.equal(c.alive, false);});+ test("a broken stdin fails waiting calls at once and never escapes as an uncaught error (#243)", async () => {+ // A launcher that stays alive, so only the stream fails. The error is+ // emitted on the real stdin: whether a write to a closed pipe raises+ // EPIPE depends on the platform and on timing, and the property here+ // does not.+ let child: ChildProcess | undefined;+ const dir = mkdtempSync(join(tmpdir(), "qmd-stdin-"));+ const launcher = join(dir, "idle.mjs");+ writeFileSync(launcher, "setTimeout(() => {}, 10_000);\n");+ const c = createQmdClient(process.cwd(), launcher, ((...args: Parameters<typeof spawn>) => (child = spawn(...args))) as typeof spawn);+ try {+ const waiting = c.call("tools/call", {}, 30_000);+ child?.stdin?.emit("error", Object.assign(new Error("write EPIPE"), { code: "EPIPE" }));+ await assert.rejects(waiting, /qmd launcher stdin closed: write EPIPE/);+ assert.equal(c.alive, false);+ } finally {+ c.dispose();+ await waitFor(() => child?.exitCode !== null || child?.signalCode !== null);+ rmSync(dir, { recursive: true, force: true });+ }+ });+test("a call against a dead client rejects rather than hanging", async () => {const c = createQmdClient(process.cwd(), join(process.cwd(), "still-missing.mjs"));await waitFor(() => !c.alive);@@ -435,11 +458,14 @@ describe("what a timeout tells the caller", () => {});describe("client warmth", () => {- test("a fresh client is not warm", () => {+ test("a fresh client is not warm", async () => {const c = createQmdClient(process.cwd(), join(process.cwd(), "no-launcher.mjs"));try {assert.equal(c.warmed, false, "nothing has come back yet, so nothing is proven");} finally {+ // Settle before disposing: the constructor's write is still in flight,+ // and its outcome must land inside this test, not after it (#243).+ await waitFor(() => !c.alive);c.dispose();}});