Skip to content

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

fix: resolve the QMD store path the way qmd does, honouring INDEX_PATH (#310)

Brenno Ferrari4a843d52eb9e
diff --git a/.claude/scripts/lib/session-start.ts b/.claude/scripts/lib/session-start.ts
index 3dad494..d60bf2e 100644
--- a/.claude/scripts/lib/session-start.ts
+++ b/.claude/scripts/lib/session-start.ts
@@ -792,18 +792,25 @@ export function formatBrainIndex(
}
/**
- * Resolve the machine-local sqlite store path for a named qmd index,
- * honoring XDG_CACHE_HOME exactly like @tobilu/qmd's own store.js (and the
- * MCP wrapper's resolveIndexSqlitePath in qmd-mcp.mjs — kept in sync by
- * behavior-locking tests on both, since .mjs exports can't be imported into
- * .ts under strip-types). Pure: env and home are injected for testability.
+ * The sqlite store qmd will open for a named index, resolved exactly as
+ * @tobilu/qmd's own `getDefaultDbPath()` (store.js) does:
+ * 1. `INDEX_PATH`, when set, for every index name;
+ * 2. else `$XDG_CACHE_HOME/qmd/<name>.sqlite`, when non-empty;
+ * 3. else `<home>/.cache/qmd/<name>.sqlite`, with home taken as qmd's
+ * `qmdHomedir()` takes it: `HOME`, then `USERPROFILE`, then `home`.
+ * Empty values count as unset, as qmd's `||` reads them. Without step 1 the
+ * store-size check behind bootstrap-vs-update looked at a file qmd never
+ * opens whenever INDEX_PATH was set. The MCP wrapper's
+ * resolveIndexSqlitePath (qmd-mcp.mjs) mirrors steps 2–3. Pure: env and home
+ * are injected for testability.
*/
export function resolveIndexStorePath(
indexName: string,
env: Record<string, string | undefined>,
home: string,
): string {
- const base = env["XDG_CACHE_HOME"] ?? join(home, ".cache");
+ if (env["INDEX_PATH"]) return env["INDEX_PATH"];
+ const base = env["XDG_CACHE_HOME"] || join(env["HOME"] || env["USERPROFILE"] || home, ".cache");
return join(base, "qmd", `${indexName}.sqlite`);
}
diff --git a/.claude/scripts/qmd-mcp.mjs b/.claude/scripts/qmd-mcp.mjs
index 7121a70..7d5dc9b 100644
--- a/.claude/scripts/qmd-mcp.mjs
+++ b/.claude/scripts/qmd-mcp.mjs
@@ -265,7 +265,11 @@ export function resolveQmdIndex(manifestJson, vaultRoot) {
* per-platform branch.
*/
export function resolveIndexSqlitePath(indexName, env, home) {
- const base = env["XDG_CACHE_HOME"] ?? join(home, ".cache");
+ // qmd's getDefaultDbPath() without its INDEX_PATH step: this computes the
+ // value INDEX_PATH is set to, and runAsMcp only calls it when INDEX_PATH is
+ // unset. Empty values count as unset (qmd uses `||`), and home is qmd's
+ // qmdHomedir(): HOME, then USERPROFILE, then the OS home.
+ const base = env["XDG_CACHE_HOME"] || join(env["HOME"] || env["USERPROFILE"] || home, ".cache");
return join(base, "qmd", `${indexName}.sqlite`);
}
diff --git a/.claude/scripts/tests/qmd-mcp.test.ts b/.claude/scripts/tests/qmd-mcp.test.ts
index 7fb13c9..6cf4896 100644
--- a/.claude/scripts/tests/qmd-mcp.test.ts
+++ b/.claude/scripts/tests/qmd-mcp.test.ts
@@ -268,6 +268,18 @@ describe("resolveIndexSqlitePath", () => {
const out = resolveIndexSqlitePath("my-vault", {}, "/home/user");
assert.match(out, /[\\/]home[\\/]user[\\/]\.cache[\\/]qmd[\\/]my-vault\.sqlite$/);
});
+ test("an empty XDG_CACHE_HOME counts as unset, as qmd's `||` reads it — never a relative path", () => {
+ const out = resolveIndexSqlitePath("v", { XDG_CACHE_HOME: "" }, "/home/user");
+ assert.match(out, /[\\/]home[\\/]user[\\/]\.cache[\\/]qmd[\\/]v\.sqlite$/);
+ });
+ test("home is qmd's: HOME, then USERPROFILE, then the OS home", () => {
+ assert.match(resolveIndexSqlitePath("v", { HOME: "/h", USERPROFILE: "/u" }, "/os"), /[\\/]h[\\/]\.cache[\\/]qmd[\\/]v\.sqlite$/);
+ assert.match(resolveIndexSqlitePath("v", { USERPROFILE: "/u" }, "/os"), /[\\/]u[\\/]\.cache[\\/]qmd[\\/]v\.sqlite$/);
+ });
+ test("INDEX_PATH is not consulted: this computes the value INDEX_PATH is set to", () => {
+ const out = resolveIndexSqlitePath("v", { INDEX_PATH: "/pinned.sqlite" }, "/home/user");
+ assert.match(out, /[\\/]home[\\/]user[\\/]\.cache[\\/]qmd[\\/]v\.sqlite$/);
+ });
test("matches qmd's own getDefaultDbPath rule on all platforms (no Library/Caches, no AppData branch)", () => {
// This locks the contract: our INDEX_PATH override must point at the same
// file qmd's CLI would write to without the override. Regression here
diff --git a/.claude/scripts/tests/session-start.test.ts b/.claude/scripts/tests/session-start.test.ts
index 31b4a4f..94d085d 100644
--- a/.claude/scripts/tests/session-start.test.ts
+++ b/.claude/scripts/tests/session-start.test.ts
@@ -1208,6 +1208,29 @@ describe("resolveIndexStorePath", () => {
join("/home/u", ".cache", "qmd", "my-vault.sqlite"),
);
});
+
+ /**
+ * qmd's getDefaultDbPath() checks INDEX_PATH first, for every index name.
+ * Without it, the store-size check behind bootstrap-vs-update looked at a
+ * file qmd never opens whenever INDEX_PATH was set.
+ */
+ test("INDEX_PATH wins over everything, for any index name", () => {
+ const env = { INDEX_PATH: "/pinned/store.sqlite", XDG_CACHE_HOME: "/xdg", HOME: "/h" };
+ assert.equal(resolveIndexStorePath("my-vault", env, "/home/u"), "/pinned/store.sqlite");
+ assert.equal(resolveIndexStorePath("other", env, "/home/u"), "/pinned/store.sqlite");
+ });
+
+ test("empty values count as unset, as qmd's `||` reads them", () => {
+ assert.equal(
+ resolveIndexStorePath("v", { INDEX_PATH: "", XDG_CACHE_HOME: "", HOME: "" }, "/home/u"),
+ join("/home/u", ".cache", "qmd", "v.sqlite"),
+ );
+ });
+
+ test("home is qmd's: HOME, then USERPROFILE, then the OS home", () => {
+ assert.equal(resolveIndexStorePath("v", { HOME: "/h", USERPROFILE: "/u" }, "/os"), join("/h", ".cache", "qmd", "v.sqlite"));
+ assert.equal(resolveIndexStorePath("v", { USERPROFILE: "/u" }, "/os"), join("/u", ".cache", "qmd", "v.sqlite"));
+ });
});
describe("injectionMode", () => {