Skip to content

A self-organizing Obsidian vault that gives AI coding agents persistent memory.

fix: align qmd-bootstrap context-rm matcher with qmd's actual output (#91)

Brenno Ferrari394cc7242d70
diff --git a/.claude/scripts/lib/qmd-bootstrap.ts b/.claude/scripts/lib/qmd-bootstrap.ts
index 6c67462..fb4525a 100644
--- a/.claude/scripts/lib/qmd-bootstrap.ts
+++ b/.claude/scripts/lib/qmd-bootstrap.ts
@@ -46,3 +46,22 @@ export function makeCollectionAddBenignMatcher(
);
return (o) => re.test(o.stderr) || re.test(o.stdout);
}
+
+/**
+ * Matches qmd's "No context found for:" output — emitted on both the
+ * virtual-path (qmd.js:630) and filesystem-path (qmd.js:656) branches of
+ * `contextRemove` when the context row doesn't exist. Benign on first-run.
+ *
+ * Narrow on purpose: a plain `not found` substring would also match qmd's
+ * `Collection not found: <name>` (qmd.js:625), which signals our collection
+ * isn't registered at all — a real error that must surface.
+ */
+export function isContextRemoveBenign(o: {
+ readonly stdout: string;
+ readonly stderr: string;
+}): boolean {
+ return (
+ /no context found for/i.test(o.stderr) ||
+ /no context found for/i.test(o.stdout)
+ );
+}
diff --git a/.claude/scripts/session-start.ts b/.claude/scripts/session-start.ts
index 63fc8ac..3a75a98 100644
--- a/.claude/scripts/session-start.ts
+++ b/.claude/scripts/session-start.ts
@@ -16,7 +16,8 @@ import {
readdirSync,
type Dirent,
} from "node:fs";
-import { spawnSync } from "node:child_process";
+import { spawn, spawnSync } from "node:child_process";
+import { tmpdir } from "node:os";
import { join } from "node:path";
import {
take,
@@ -69,24 +70,36 @@ if (envFile) {
const manifestJson = readManifestRaw();
const infraRootFilenames = parseInfraRootFilenames(manifestJson);
-// Incremental QMD re-index. Fire-and-forget; ignore failures (qmd is optional).
-// Scope to this vault's named index when the manifest declares one, so vaults
-// sharing a machine don't blend results in QMD's default global index. Falls
-// back silently for forks that haven't adopted the `qmd_index` manifest field.
-//
-// Route through `buildQmdCommand` so the same shim-bypass logic that fixes
-// the MCP wrapper applies here too — `node qmd.js update` runs identically
-// on Windows, macOS, and Linux; no platform conditionals.
+// Incremental QMD re-index. Truly fire-and-forget: detached, unref'd,
+// ignore-all-streams. The hook's own work (file walks, git log, context
+// emission) is independent of this index update, so blocking on qmd's
+// startup (notably slow on Windows × Node 24 cold start, where it can
+// approach 10s before the actual update work begins) is wasted user
+// latency. Scope to this vault's named index when the manifest declares
+// one; fall back silently for forks that haven't adopted `qmd_index`.
+// Route through `buildQmdCommand` so the shim-bypass logic that fixes
+// the MCP wrapper applies here too.
const qmdIndex = parseQmdIndex(manifestJson);
const qmdUpdate = buildQmdCommand(
resolveQmdEntry(),
qmdArgsWithIndex(qmdIndex, ["update"]),
);
-spawnSync(qmdUpdate.cmd, qmdUpdate.args as string[], {
+// `cwd: tmpdir()` keeps the detached child from holding the vault dir as
+// its working directory. `qmd update --index <name>` reads collection
+// paths from YAML, so cwd is irrelevant to the work; pinning it to the OS
+// tmpdir means `rm -rf` of the vault (or a test cleanup) never races a
+// stale qmd handle on Windows.
+const qmdChild = spawn(qmdUpdate.cmd, qmdUpdate.args as string[], {
stdio: "ignore",
- timeout: 30_000,
shell: qmdUpdate.shell,
+ detached: true,
+ windowsHide: true,
+ cwd: tmpdir(),
});
+// Silence the spawn-error event so a missing qmd doesn't crash the hook;
+// qmd is optional and the hook already degrades when it's not installed.
+qmdChild.on("error", () => undefined);
+qmdChild.unref();
type CmdResult =
| { readonly kind: "ok"; readonly stdout: string }
diff --git a/.claude/scripts/tests/qmd-bootstrap.test.ts b/.claude/scripts/tests/qmd-bootstrap.test.ts
index 6f76d1e..b0dbb28 100644
--- a/.claude/scripts/tests/qmd-bootstrap.test.ts
+++ b/.claude/scripts/tests/qmd-bootstrap.test.ts
@@ -11,6 +11,7 @@ import assert from "node:assert/strict";
import {
buildCollectionAddArgs,
+ isContextRemoveBenign,
makeCollectionAddBenignMatcher,
} from "../lib/qmd-bootstrap.ts";
@@ -185,3 +186,92 @@ describe("makeCollectionAddBenignMatcher", () => {
);
});
});
+
+describe("isContextRemoveBenign", () => {
+ test("matches qmd's virtual-path 'No context found for:' on stderr", () => {
+ assert.equal(
+ isContextRemoveBenign({
+ stdout: "",
+ stderr: "No context found for: qmd://obsidian-mind/",
+ }),
+ true,
+ );
+ });
+
+ test("matches the filesystem-path variant with a relative path", () => {
+ assert.equal(
+ isContextRemoveBenign({
+ stdout: "",
+ stderr: "No context found for: qmd://obsidian-mind/some/sub/path",
+ }),
+ true,
+ );
+ });
+
+ test("is case-insensitive", () => {
+ assert.equal(
+ isContextRemoveBenign({
+ stdout: "",
+ stderr: "no context found for: qmd://obsidian-mind/",
+ }),
+ true,
+ );
+ });
+
+ test("matches on stdout (qmd's stream choice could shift)", () => {
+ assert.equal(
+ isContextRemoveBenign({
+ stdout: "No context found for: qmd://obsidian-mind/",
+ stderr: "",
+ }),
+ true,
+ );
+ });
+
+ test("matches when the message appears on BOTH streams", () => {
+ assert.equal(
+ isContextRemoveBenign({
+ stdout: "No context found for: qmd://obsidian-mind/",
+ stderr: "No context found for: qmd://obsidian-mind/",
+ }),
+ true,
+ );
+ });
+
+ test("does NOT match `Collection not found` — a real error the old matcher silently swallowed", () => {
+ // The prior matcher used `/not found/i`, which incidentally matched this
+ // non-benign error. Locking the new matcher's narrower behaviour so a
+ // genuine "our collection isn't registered" failure surfaces.
+ assert.equal(
+ isContextRemoveBenign({
+ stdout: "",
+ stderr: "Collection not found: obsidian-mind",
+ }),
+ false,
+ );
+ });
+
+ test("does NOT match `Invalid virtual path` — malformed input error", () => {
+ assert.equal(
+ isContextRemoveBenign({
+ stdout: "",
+ stderr: "Invalid virtual path: qmd://obsidian-mind",
+ }),
+ false,
+ );
+ });
+
+ test("does NOT match `Path is not in any indexed collection` — wrong path error", () => {
+ assert.equal(
+ isContextRemoveBenign({
+ stdout: "",
+ stderr: "Path is not in any indexed collection: /tmp/somewhere",
+ }),
+ false,
+ );
+ });
+
+ test("does NOT match when output is empty", () => {
+ assert.equal(isContextRemoveBenign({ stdout: "", stderr: "" }), false);
+ });
+});
diff --git a/.claude/scripts/tests/session-start.integration.test.ts b/.claude/scripts/tests/session-start.integration.test.ts
index 70da83c..a06fe04 100644
--- a/.claude/scripts/tests/session-start.integration.test.ts
+++ b/.claude/scripts/tests/session-start.integration.test.ts
@@ -51,7 +51,19 @@ before(() => {
});
after(() => {
- if (TMP_DIR) rmSync(TMP_DIR, { recursive: true, force: true });
+ // `maxRetries` + `retryDelay` is Node's documented Windows guard against
+ // transient `EBUSY` / `EPERM` on rmdir when child processes or the OS
+ // haven't fully released file handles yet. On Windows CI the test's tmp
+ // dir occasionally lingers a few seconds after the detached qmd worker
+ // finishes; retrying with linear backoff is the idiomatic fix.
+ if (TMP_DIR) {
+ rmSync(TMP_DIR, {
+ recursive: true,
+ force: true,
+ maxRetries: 10,
+ retryDelay: 200,
+ });
+ }
});
const runHook = () => spawnHook(SCRIPT, "", { CLAUDE_PROJECT_DIR: TMP_DIR });
diff --git a/scripts/qmd-bootstrap.ts b/scripts/qmd-bootstrap.ts
index 0ccf113..f0bce3a 100644
--- a/scripts/qmd-bootstrap.ts
+++ b/scripts/qmd-bootstrap.ts
@@ -35,6 +35,7 @@ import { warn } from "../.claude/scripts/lib/hook-io.ts";
import { isMainModule } from "../.claude/scripts/lib/main-guard.ts";
import {
buildCollectionAddArgs,
+ isContextRemoveBenign,
makeCollectionAddBenignMatcher,
} from "../.claude/scripts/lib/qmd-bootstrap.ts";
import { buildQmdCommand, resolveQmdEntry } from "../.claude/scripts/lib/qmd.ts";
@@ -225,16 +226,13 @@ function main(): void {
// Re-attach the context string so edits to vault-manifest.json propagate.
// On first run, `context rm` fails because the row doesn't exist — that's
- // the expected benign case. Unexpected failures (auth, IO) get warned.
+ // the expected benign case. Any other failure (including a genuine
+ // "Collection not found") is intentionally NOT swallowed.
runIdempotent(
entry,
["--index", index, "context", "rm", contextPath],
"Clearing previous context (if any)",
- (o) =>
- /not found/i.test(o.stderr) ||
- /not found/i.test(o.stdout) ||
- /does not exist/i.test(o.stderr) ||
- /does not exist/i.test(o.stdout),
+ isContextRemoveBenign,
);
run(
entry,