A self-organizing Obsidian vault that gives AI coding agents persistent memory.
TypeScript ai-agentsclaude-codecodex-cligemini-cliknowledge-managementobsidianobsidian-vaultpersistent-memorysecond-braintemplate
fix: prefer a runnable qmd install over the first one that merely exists (#291)
Brenno Ferrari12c9e826c799
diff --git a/.claude/scripts/lib/qmd.ts b/.claude/scripts/lib/qmd.tsindex 70e3d75..34de5ca 100644--- a/.claude/scripts/lib/qmd.ts+++ b/.claude/scripts/lib/qmd.ts@@ -31,13 +31,68 @@ const require = createRequire(import.meta.url);* Locate @tobilu/qmd's real JS entrypoint. Returns an absolute path when* resolvable, null when not. A null return signals the caller to fall back* to invoking `qmd` directly via the platform shell (last-resort path for- * non-npm installs).+ * non-npm installs). A runnable install is preferred over one that merely+ * exists — see {@link pickQmdEntry}.*/export function resolveQmdEntry(): string | null {+ return pickQmdEntry([localQmdEntry, globalQmdEntry], isRunnableQmdEntry);+}++/**+ * Choose among candidate entrypoints, in order. Each source returns a path+ * that EXISTS, or null. A present entry can still be broken — missing+ * dependencies, a half-finished install, a native binding built for another+ * Node ABI — and a broken first candidate must not shadow a working later one.+ *+ * - The first present candidate that passes `runnable` wins.+ * - When every present candidate fails, the FIRST present one is returned, not+ * null: SessionStart's ABI self-heal needs a broken entry to find the+ * package it rebuilds.+ * - A candidate with nothing after it to fall back to, and nothing before it,+ * is returned without a probe: probing could not change the answer. That is+ * the common single-install case, which therefore costs no extra spawn.+ *+ * Sources are thunks so a runnable early candidate never pays for the later+ * lookups (`npm root -g` is a shell spawn).+ */+export function pickQmdEntry(+ sources: ReadonlyArray<() => string | null>,+ runnable: (entry: string) => boolean,+): string | null {+ let firstPresent: string | null = null;+ for (let i = 0; i < sources.length; i++) {+ const entry = (sources[i] as () => string | null)();+ if (entry === null) continue;+ if (firstPresent === null && i === sources.length - 1) return entry;+ if (runnable(entry)) return entry;+ firstPresent ??= entry;+ }+ return firstPresent;+}++/**+ * Does this entrypoint actually start? `--version` is the cheapest call that+ * loads the CLI. Bounded, so a hung install reads as broken rather than+ * blocking a hook.+ */+export function isRunnableQmdEntry(entry: string): boolean {+ const probe = spawnSync(process.execPath, [entry, "--version"], {+ encoding: "utf8",+ timeout: 10_000,+ windowsHide: true,+ });+ return !probe.error && probe.signal === null && probe.status === 0;+}++function localQmdEntry(): string | null {try {return require.resolve("@tobilu/qmd/dist/cli/qmd.js");- } catch {}+ } catch {+ return null;+ }+}+function globalQmdEntry(): string | null {// Fallback for global npm installs that aren't on this package's resolution// path — ask npm directly where global packages live. Bounded timeout so a// hung npm process can't block a fire-and-forget hook indefinitely.diff --git a/.claude/scripts/qmd-mcp.d.mts b/.claude/scripts/qmd-mcp.d.mtsindex cff9d73..3ec4103 100644--- a/.claude/scripts/qmd-mcp.d.mts+++ b/.claude/scripts/qmd-mcp.d.mts@@ -10,6 +10,18 @@*/export function resolveQmdEntry(): string | null;+/**+ * Choose among candidate entrypoints: the first runnable present one, else+ * the first present one, else null. A lone candidate is not probed.+ */+export function pickQmdEntry(+ sources: ReadonlyArray<() => string | null>,+ runnable: (entry: string) => boolean,+): string | null;++/** Does this entrypoint start? Bounded `--version` probe. */+export function isRunnableQmdEntry(entry: string): boolean;+/*** Resolve the vault root directory from a `file://` URL and optional env.* The env argument is read for `CLAUDE_PROJECT_DIR` (Claude Code'sdiff --git a/.claude/scripts/qmd-mcp.mjs b/.claude/scripts/qmd-mcp.mjsindex a68397e..7121a70 100644--- a/.claude/scripts/qmd-mcp.mjs+++ b/.claude/scripts/qmd-mcp.mjs@@ -75,12 +75,58 @@ const VAULT_ROOT = resolveVaultRoot(import.meta.url);* Locate @tobilu/qmd's real JS entrypoint. Returns an absolute path when* resolvable, null when not. Exported so the cross-platform test matrix can* verify resolution works on Windows, macOS, and Linux without having to- * spawn the wrapper itself.+ * spawn the wrapper itself. A runnable install is preferred over one that+ * merely exists — see `pickQmdEntry`.*/export function resolveQmdEntry() {+ return pickQmdEntry([localQmdEntry, globalQmdEntry], isRunnableQmdEntry);+}++/**+ * Choose among candidate entrypoints, in order. Duplicated from+ * `lib/qmd.ts:pickQmdEntry` (this file is .mjs and can't import the .ts lib+ * at strip-types runtime); the test suite drives both copies through the+ * same cases.+ *+ * - The first present candidate that passes `runnable` wins, so a broken+ * first install cannot shadow a working later one.+ * - When every present candidate fails, the FIRST present one is returned,+ * not null: SessionStart's ABI self-heal needs a broken entry to find the+ * package it rebuilds.+ * - A lone candidate is returned without a probe, since probing could not+ * change the answer.+ */+export function pickQmdEntry(sources, runnable) {+ let firstPresent = null;+ for (let i = 0; i < sources.length; i++) {+ const entry = sources[i]();+ if (entry === null) continue;+ if (firstPresent === null && i === sources.length - 1) return entry;+ if (runnable(entry)) return entry;+ firstPresent ??= entry;+ }+ return firstPresent;+}++/** Does this entrypoint actually start? Bounded `--version` probe. */+export function isRunnableQmdEntry(entry) {+ const probe = spawnSync(process.execPath, [entry, "--version"], {+ encoding: "utf8",+ timeout: 10_000,+ windowsHide: true,+ });+ return !probe.error && probe.signal === null && probe.status === 0;+}++function localQmdEntry() {try {return require.resolve("@tobilu/qmd/dist/cli/qmd.js");- } catch {}+ } catch {+ return null;+ }+}++function globalQmdEntry() {// Fallback for global npm installs that aren't on this package's resolution// path — ask npm directly where global packages live. Bounded timeout so adiff --git a/.claude/scripts/tests/qmd.test.ts b/.claude/scripts/tests/qmd.test.tsindex d4922dd..c9e2b86 100644--- a/.claude/scripts/tests/qmd.test.ts+++ b/.claude/scripts/tests/qmd.test.ts@@ -9,15 +9,94 @@import { test, describe } from "node:test";import assert from "node:assert/strict";-import { existsSync } from "node:fs";-import { isAbsolute } from "node:path";+import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { isAbsolute, join } from "node:path";import {buildQmdCommand,+ isRunnableQmdEntry,parseVersionTriple,+ pickQmdEntry,qmdVersionAtLeast,resolveQmdEntry,} from "../lib/qmd.ts";+import {+ isRunnableQmdEntry as isRunnableQmdEntryMjs,+ pickQmdEntry as pickQmdEntryMjs,+} from "../qmd-mcp.mjs";++/**+ * A present-but-broken install used to win just by existing: the first+ * candidate that resolved was returned, so a broken local copy shadowed a+ * working global one. Both copies of the picker run the same cases.+ */+for (const [label, pick, isRunnable] of [+ ["lib/qmd.ts", pickQmdEntry, isRunnableQmdEntry],+ ["qmd-mcp.mjs", pickQmdEntryMjs, isRunnableQmdEntryMjs],+] as const) {+ describe(`${label} pickQmdEntry`, () => {+ const probe = (ok: ReadonlySet<string>) => {+ const probed: string[] = [];+ return {+ probed,+ runnable: (e: string) => {+ probed.push(e);+ return ok.has(e);+ },+ };+ };++ test("a broken first candidate does not shadow a runnable second", () => {+ const p = probe(new Set(["global"]));+ assert.equal(pick([() => "local", () => "global"], p.runnable), "global");+ });++ test("every candidate broken → the first present, so the ABI self-heal still has a package", () => {+ const p = probe(new Set());+ assert.equal(pick([() => "local", () => "global"], p.runnable), "local");+ });++ test("nothing present → null", () => {+ const p = probe(new Set());+ assert.equal(pick([() => null, () => null], p.runnable), null);+ assert.deepEqual(p.probed, []);+ });++ test("a lone candidate is returned without a probe", () => {+ const p = probe(new Set());+ assert.equal(pick([() => null, () => "global"], p.runnable), "global");+ assert.deepEqual(p.probed, []);+ });++ test("a runnable first candidate skips the later lookups", () => {+ const p = probe(new Set(["local"]));+ let looked = false;+ const later = () => {+ looked = true;+ return "global";+ };+ assert.equal(pick([() => "local", later], p.runnable), "local");+ assert.equal(looked, false);+ });+ });++ describe(`${label} isRunnableQmdEntry`, () => {+ test("an entry that exits 0 is runnable; one that throws on load is not", () => {+ const dir = mkdtempSync(join(tmpdir(), "qmd-probe-"));+ try {+ const good = join(dir, "good.mjs");+ const bad = join(dir, "bad.mjs");+ writeFileSync(good, "process.stdout.write('qmd 9.9.9\\n');\n");+ writeFileSync(bad, "throw new Error('Cannot find module better-sqlite3');\n");+ assert.equal(isRunnable(good), true);+ assert.equal(isRunnable(bad), false);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ });+ });+}describe("lib/qmd.resolveQmdEntry", () => {test("returns an absolute path to an existing qmd entrypoint when qmd is installed", () => {