A self-organizing Obsidian vault that gives AI coding agents persistent memory.
TypeScript ai-agentsclaude-codecodex-cligemini-cliknowledge-managementobsidianobsidian-vaultpersistent-memorysecond-braintemplate
fix: stop qmd-bootstrap from mis-naming the collection on stock Windows (#90)
Brenno Ferrari7b48d7916d59
diff --git a/.claude/scripts/lib/qmd-bootstrap.ts b/.claude/scripts/lib/qmd-bootstrap.tsnew file mode 100644index 0000000..6c67462--- /dev/null+++ b/.claude/scripts/lib/qmd-bootstrap.ts@@ -0,0 +1,48 @@+/**+ * Pure helpers for `scripts/qmd-bootstrap.ts`, factored out for unit-testing+ * without spawning qmd.+ *+ * Two qmd-2.x quirks drive the shape here. Its CLI runs `util.parseArgs` with+ * `strict: false`, so a positional name and a `--pattern` flag are both+ * silently dropped — name comes from `--name`, glob from `--mask`. And when+ * `--name` is absent, qmd auto-derives the name from `pwd.split('/').pop()`,+ * which mis-fires on native Windows backslash paths.+ */++import { escapeRegex } from "./regex.ts";++const DEFAULT_GLOB = "**/*.md";++export function buildCollectionAddArgs(+ index: string,+ collectionName: string,+ glob: string = DEFAULT_GLOB,+): readonly string[] {+ return [+ "--index",+ index,+ "collection",+ "add",+ ".",+ "--name",+ collectionName,+ "--mask",+ glob,+ ];+}++/**+ * Matches qmd's by-name "already exists" output (benign on bootstrap re-run).+ * Does NOT match qmd's "A collection already exists for this path and pattern"+ * warning — that signals a stale-name collision and must surface so the user+ * runs the `qmd collection remove ...` qmd itself suggests.+ */+export function makeCollectionAddBenignMatcher(+ expectedName: string,+): (o: { readonly stdout: string; readonly stderr: string }) => boolean {+ const re = new RegExp(+ `Collection ['"]?${escapeRegex(expectedName)}['"]? already exists`,+ "i",+ );+ return (o) => re.test(o.stderr) || re.test(o.stdout);+}diff --git a/.claude/scripts/tests/qmd-bootstrap.test.ts b/.claude/scripts/tests/qmd-bootstrap.test.tsnew file mode 100644index 0000000..6f76d1e--- /dev/null+++ b/.claude/scripts/tests/qmd-bootstrap.test.ts@@ -0,0 +1,187 @@+/**+ * Locks the argv shape for `qmd collection add` and the benign-failure+ * matcher. qmd's CLI parses with `strict: false`, so a positional name or+ * `--pattern` flag are silently dropped — these tests catch any drift back+ * to that shape and any over-broad matcher that would swallow qmd's+ * path-collision warning.+ */++import { test, describe } from "node:test";+import assert from "node:assert/strict";++import {+ buildCollectionAddArgs,+ makeCollectionAddBenignMatcher,+} from "../lib/qmd-bootstrap.ts";++describe("buildCollectionAddArgs", () => {+ test("emits the exact argv shape qmd's `collection add` expects", () => {+ const args = buildCollectionAddArgs("obsidian-mind", "obsidian-mind");+ assert.deepEqual(args, [+ "--index",+ "obsidian-mind",+ "collection",+ "add",+ ".",+ "--name",+ "obsidian-mind",+ "--mask",+ "**/*.md",+ ]);+ });++ test("places `--name` AFTER the `.` positional so qmd parses pwd as `.`", () => {+ // qmd's `collection add` dispatch reads cli.args[1] as the working+ // directory. If `--name` (or the name value) sat in that slot, qmd+ // would treat it as a path and either fail or auto-derive a bogus name.+ const args = buildCollectionAddArgs("idx", "vault-name");+ const addIdx = args.indexOf("add");+ assert.equal(args[addIdx + 1], ".", "positional after `add` must be `.`");+ assert.notEqual(+ args[addIdx + 2],+ "vault-name",+ "name must NOT sit in the next positional slot",+ );+ });++ test("`--name` immediately precedes the collection name value", () => {+ const args = buildCollectionAddArgs("idx", "my-vault");+ const nameIdx = args.indexOf("--name");+ assert.notEqual(nameIdx, -1, "`--name` flag must be present");+ assert.equal(args[nameIdx + 1], "my-vault");+ });++ test("`--mask` immediately precedes the glob value (NOT `--pattern`)", () => {+ // qmd reads cli.values.mask only; --pattern is silently swallowed under+ // parseArgs strict:false. Locking the flag name here so a well-meaning+ // rename can't re-introduce the silent-noop.+ const args = buildCollectionAddArgs("idx", "name");+ assert.equal(args.includes("--pattern"), false, "must not pass `--pattern`");+ const maskIdx = args.indexOf("--mask");+ assert.notEqual(maskIdx, -1, "`--mask` flag must be present");+ assert.equal(args[maskIdx + 1], "**/*.md");+ });++ test("propagates a custom glob through `--mask`", () => {+ const args = buildCollectionAddArgs("idx", "name", "journals/**/*.md");+ const maskIdx = args.indexOf("--mask");+ assert.equal(args[maskIdx + 1], "journals/**/*.md");+ });++ test("`--index` immediately precedes the index value at argv[0..1]", () => {+ const args = buildCollectionAddArgs("vault-a", "vault-a");+ assert.equal(args[0], "--index");+ assert.equal(args[1], "vault-a");+ });+});++describe("makeCollectionAddBenignMatcher", () => {+ test("matches qmd's by-name 'already exists' output on stderr", () => {+ const matches = makeCollectionAddBenignMatcher("obsidian-mind");+ assert.equal(+ matches({+ stdout: "",+ stderr: "Collection 'obsidian-mind' already exists.",+ }),+ true,+ );+ });++ test("matches the same output on stdout (qmd's stream choice varies)", () => {+ const matches = makeCollectionAddBenignMatcher("obsidian-mind");+ assert.equal(+ matches({+ stdout: "Collection 'obsidian-mind' already exists.",+ stderr: "",+ }),+ true,+ );+ });++ test("matches when the message appears on BOTH streams (split output)", () => {+ const matches = makeCollectionAddBenignMatcher("obsidian-mind");+ assert.equal(+ matches({+ stdout: "Collection 'obsidian-mind' already exists.",+ stderr: "Collection 'obsidian-mind' already exists.",+ }),+ true,+ );+ });++ test("matches double-quoted variant for output-format resilience", () => {+ const matches = makeCollectionAddBenignMatcher("obsidian-mind");+ assert.equal(+ matches({+ stdout: "",+ stderr: 'Collection "obsidian-mind" already exists.',+ }),+ true,+ );+ });++ test("matches unquoted variant", () => {+ const matches = makeCollectionAddBenignMatcher("obsidian-mind");+ assert.equal(+ matches({+ stdout: "",+ stderr: "Collection obsidian-mind already exists",+ }),+ true,+ );+ });++ test("does NOT swallow qmd's path-collision warning (stale-name upgrade case)", () => {+ // Real qmd output when a different-named collection points at the same+ // path+glob. Swallowing it leaves the stale collection in place and+ // `context add` then fails downstream.+ const matches = makeCollectionAddBenignMatcher("obsidian-mind");+ const stderr =+ "A collection already exists for this path and pattern:\n" ++ " Name: C:\\Users\\foo\\my-vault\n" ++ " Pattern: **/*.md\n" ++ "Use 'qmd update' to re-index it, or remove it first with " ++ "'qmd collection remove C:\\Users\\foo\\my-vault'";+ assert.equal(matches({ stdout: "", stderr }), false);+ });++ test("does NOT match an 'already exists' line about a DIFFERENT name", () => {+ // Guards against an over-broad regex (e.g. /already exists/i) that+ // would match any "already exists" output. The matcher must be bound+ // to the expected name.+ const matches = makeCollectionAddBenignMatcher("obsidian-mind");+ assert.equal(+ matches({+ stdout: "",+ stderr: "Collection 'someone-else' already exists.",+ }),+ false,+ );+ });++ test("does NOT match when output is empty", () => {+ const matches = makeCollectionAddBenignMatcher("obsidian-mind");+ assert.equal(matches({ stdout: "", stderr: "" }), false);+ });++ test("treats the expected name as a literal string (regex metacharacters escaped)", () => {+ // A name like `my-vault.test` should match only literal occurrences,+ // not `my-vaultXtest` etc. Defends against future names that legally+ // contain dots/dashes (allowed by isValidQmdIndex).+ const matches = makeCollectionAddBenignMatcher("my-vault.test");+ assert.equal(+ matches({+ stdout: "",+ stderr: "Collection 'my-vault.test' already exists.",+ }),+ true,+ );+ assert.equal(+ matches({+ stdout: "",+ stderr: "Collection 'my-vaultXtest' already exists.",+ }),+ false,+ );+ });+});diff --git a/.github/workflows/test.yml b/.github/workflows/test.ymlindex 4772303..1c0ab13 100644--- a/.github/workflows/test.yml+++ b/.github/workflows/test.yml@@ -5,10 +5,12 @@ on:branches: [main]paths:- '.claude/scripts/**'+ - 'scripts/**'- '.github/workflows/test.yml'pull_request:paths:- '.claude/scripts/**'+ - 'scripts/**'- '.github/workflows/test.yml'jobs:diff --git a/scripts/qmd-bootstrap.ts b/scripts/qmd-bootstrap.tsindex 0a5cdaa..0ccf113 100644--- a/scripts/qmd-bootstrap.ts+++ b/scripts/qmd-bootstrap.ts@@ -33,6 +33,10 @@ import { readFileSync } from "node:fs";import { warn } from "../.claude/scripts/lib/hook-io.ts";import { isMainModule } from "../.claude/scripts/lib/main-guard.ts";+import {+ buildCollectionAddArgs,+ makeCollectionAddBenignMatcher,+} from "../.claude/scripts/lib/qmd-bootstrap.ts";import { buildQmdCommand, resolveQmdEntry } from "../.claude/scripts/lib/qmd.ts";import {qmdConfigPath,@@ -181,6 +185,19 @@ function main(): void {ensureQmd(entry);const collectionName = manifest.template ?? index;+ // The collection name ends up as a YAML key, a `qmd://` URL segment, and a+ // SQL identifier inside qmd's store. Same character class as `qmd_index`+ // so a typo'd `template` field can't land an unparseable identifier.+ // (`qmd_index` was validated above, so a failure here can only come from+ // `template`.)+ if (!isValidQmdIndex(collectionName)) {+ process.stderr.write(+ `vault-manifest.json \`template\` field value ${JSON.stringify(collectionName)} ` ++ "is not a valid qmd identifier.\n" ++ "Allowed: alphanumerics, dot, dash, underscore; must start with an alphanumeric.\n",+ );+ process.exit(1);+ }const contextPath = `qmd://${collectionName}/`;const contextText =manifest.qmd_context ??@@ -188,24 +205,22 @@ function main(): void {process.stdout.write(`→ Bootstrapping QMD index '${index}'\n`);- // `collection add` fails with a specific message when the collection is- // already registered — that's the idempotent case we expect on re-run.- // Any other failure (invalid pattern, permissions, qmd install drift) is- // surfaced as a warning so it isn't silently masked.+ // Re-runs are idempotent (matcher recognises the by-name "already exists"+ // case); a path-collision warning is intentionally NOT swallowed.runIdempotent(entry,- [- "--index",- index,- "collection",- "add",- ".",- collectionName,- "--pattern",- "**/*.md",- ],- `Registering collection '${collectionName}' (pattern **/*.md)`,- (o) => /already exists/i.test(o.stderr) || /already exists/i.test(o.stdout),+ buildCollectionAddArgs(index, collectionName),+ `Registering collection '${collectionName}' (mask **/*.md)`,+ makeCollectionAddBenignMatcher(collectionName),+ );++ // Round-trip the registration: if a future qmd CLI change silently breaks+ // our argv shape, this is where we find out — `collection show` exits 1+ // with "Collection not found: <name>" rather than running past the issue.+ run(+ entry,+ ["--index", index, "collection", "show", collectionName],+ `Verifying collection '${collectionName}' is registered`,);// Re-attach the context string so edits to vault-manifest.json propagate.