Skip to content

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

fix: a private marker past the head window read as public (#187)

Brenno Ferrari62f130305f78
diff --git a/.claude/scripts/lib/mcp-exposure.ts b/.claude/scripts/lib/mcp-exposure.ts
index 70e7e45..af2fbce 100644
--- a/.claude/scripts/lib/mcp-exposure.ts
+++ b/.claude/scripts/lib/mcp-exposure.ts
@@ -201,7 +201,44 @@ export function resolveExposure(
export function isPrivate(path: string): boolean {
const head = readHead(path);
if (head === null) return true;
- return /^\s*-?\s*private\s*$/m.test(head) || /^private:\s*true/m.test(head);
+ if (hasPrivateMarker(head)) return true;
+
+ // The marker may simply be past the cut.
+ //
+ // `readHead` stops at 1200 characters, which is a PREFIX of the frontmatter
+ // rather than the frontmatter. Measured on a real vault: two ordinary public
+ // notes carry enough `aliases:` that the closing `---` lands past character
+ // 1700, so a `private:` below that point would never be seen and the guard
+ // reads a clean "not private" off a block it did not finish.
+ //
+ // The answer is to look further, not to guess. Guessing PRIVATE fails closed
+ // and is worse than the bug: it silently removes ordinary notes from
+ // `search`, `expand` and the resource listing, with no message, and the set
+ // grows as frontmatter grows. Guessing PUBLIC is the bug. So when the block
+ // did not close inside the prefix, re-read enough to decide — and withhold
+ // only if even that does not close, which means the file is not really
+ // frontmatter-shaped.
+ if (!opensFrontmatter(head) || frontmatterClosed(head)) return false;
+
+ const deep = readHead(path, DEEP_HEAD_CHARS);
+ if (deep === null) return true;
+ if (hasPrivateMarker(deep)) return true;
+ return !frontmatterClosed(deep);
+}
+
+/** How far to look when the frontmatter did not close in the ordinary prefix. */
+const DEEP_HEAD_CHARS = 64_000;
+
+const hasPrivateMarker = (head: string): boolean =>
+ /^\s*-?\s*private\s*$/m.test(head) || /^private:\s*true/m.test(head);
+
+/** Does this text open a frontmatter block? A BOM may precede the fence. */
+const opensFrontmatter = (head: string): boolean => /^?---[ \t]*\r?\n/.test(head);
+
+/** Is there a closing `---` after the opening one? */
+function frontmatterClosed(head: string): boolean {
+ if (!opensFrontmatter(head)) return false;
+ return /^---[ \t]*(\r?\n|$)/m.test(head.slice(head.indexOf("\n") + 1));
}
/** Pull the frontmatter `description:` so a resource list is self-describing. */
diff --git a/.claude/scripts/tests/mcp-exposure.test.ts b/.claude/scripts/tests/mcp-exposure.test.ts
index 5f52726..965b552 100644
--- a/.claude/scripts/tests/mcp-exposure.test.ts
+++ b/.claude/scripts/tests/mcp-exposure.test.ts
@@ -31,6 +31,8 @@ import {
import type { ExposurePolicy } from "../lib/mcp-exposure.ts";
import { vaultRelKey } from "../lib/mcp-qmd-client.ts";
+const NL = String.fromCharCode(10);
+
const NOTE = (desc = "a note") => `---\ndate: 2026-07-26\ndescription: "${desc}"\n---\n\n# n\n\nbody\n`;
const PRIVATE_TAG = "---\ndate: 2026-07-26\ntags:\n - private\n---\n\n# secret\n\nbody\n";
const PRIVATE_FLAG = "---\ndate: 2026-07-26\nprivate: true\n---\n\n# secret\n\nbody\n";
@@ -160,6 +162,47 @@ describe("resolving the policy", () => {
});
});
+ /**
+ * Both directions, because fixing this in one direction caused the other.
+ *
+ * `readHead` stops at 1200 characters, so a `private:` past that point was
+ * never seen and the note was served. The first fix withheld any note whose
+ * frontmatter did not close inside the prefix — which failed closed and made
+ * two ordinary public notes in a real vault vanish from `search`, `expand`
+ * and the resource listing, silently, with the set growing as frontmatter
+ * grows. The guard looks further now instead of guessing either way.
+ */
+ test("a private marker past the head window still withholds", () => {
+ withVault((dir) => {
+ const filler = Array.from({ length: 120 }, (_, i) => " - alias-" + i).join(NL);
+ const full = put(dir, "brain/Deep Private.md", "---" + NL + "aliases:" + NL + filler + NL + "private: true" + NL + "---" + NL + NL + "# d" + NL);
+ assert.ok(isPrivate(full), "the marker is past char 1200 but it is still a marker");
+ });
+ });
+
+ test("a public note with long frontmatter is STILL SERVED", () => {
+ withVault((dir) => {
+ const filler = Array.from({ length: 120 }, (_, i) => " - alias-" + i).join(NL);
+ const full = put(dir, "brain/Deep Public.md", "---" + NL + "aliases:" + NL + filler + NL + "---" + NL + NL + "# d" + NL);
+ assert.ok(!isPrivate(full), "long frontmatter is not a privacy signal");
+ });
+ });
+
+ test("a note with no frontmatter is served, and a bare rule is not frontmatter", () => {
+ withVault((dir) => {
+ assert.ok(!isPrivate(put(dir, "brain/Plain.md", "# t" + NL + NL + "body" + NL)));
+ assert.ok(!isPrivate(put(dir, "brain/Rule.md", "# t" + NL + NL + "---" + NL + NL + "after" + NL)));
+ });
+ });
+
+ test("frontmatter that never closes is withheld", () => {
+ withVault((dir) => {
+ const filler = Array.from({ length: 5000 }, (_, i) => " - alias-" + i).join(NL);
+ const full = put(dir, "brain/Runaway.md", "---" + NL + "aliases:" + NL + filler + NL);
+ assert.ok(isPrivate(full), "a block that never closes is not a block this can judge");
+ });
+ });
+
test("never-expose keeps filenames with spaces and dots, which the root rule would reject", () => {
withVault((dir) => {
const p = resolveExposure(dir, { mcp_never_expose: ["North Star.md", "SOUL.md"] });