Skip to content

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

fix: surface the cross-repo memory inbox in hygiene (#177)

Brenno Ferrari1613044794d0
diff --git a/.claude/scripts/lib/active-hygiene.ts b/.claude/scripts/lib/active-hygiene.ts
index 8cc279e..1b301bc 100644
--- a/.claude/scripts/lib/active-hygiene.ts
+++ b/.claude/scripts/lib/active-hygiene.ts
@@ -105,6 +105,7 @@ export type ActiveHygieneReport = {
readonly oversizedNotes: readonly OversizedNote[];
readonly openLoops: readonly OpenLoop[];
readonly inboxPressure: InboxPressure | null;
+ readonly memoryInbox: InboxPressure | null;
};
// ---------------------------------------------------------------------------
@@ -452,6 +453,97 @@ function findInboxPressure(root: string, nowMs: number): InboxPressure | null {
return count > 0 ? { count, oldestDays } : null;
}
+// ---------------------------------------------------------------------------
+// Cross-repo memory inbox (#171). `memories/YYYY/MM/` is written by the MCP
+// server on behalf of sessions in OTHER repos, and nothing has ever surfaced
+// it. The meetings inbox above is watched; this one was not, so it accumulates
+// in silence.
+// ---------------------------------------------------------------------------
+
+const MEMORY_ROOT_DEFAULT = "memories";
+
+/**
+ * Where the server writes cross-repo memories.
+ *
+ * NOT `mcp_inbox`, which is a different key for a different directory: the
+ * fallback where a `record_work` call from a repo with no matching project
+ * folder lands. Conflating the two is what left this unwatched, because the
+ * watched one is empty by design in most vaults.
+ */
+export function parseMemoryRoot(manifestJson: string | null): string {
+ if (manifestJson === null) return MEMORY_ROOT_DEFAULT;
+ try {
+ const declared = (JSON.parse(manifestJson) as Record<string, unknown>)["memory_root"];
+ if (typeof declared === "string" && /^[\w.-]+$/.test(declared)) return declared;
+ } catch {
+ /* malformed manifest → the default */
+ }
+ return MEMORY_ROOT_DEFAULT;
+}
+
+/**
+ * A capture already promoted into `brain/`, and kept on purpose.
+ *
+ * THE FLAG HAS TO BE ABLE TO REACH ZERO. Promotion is **additive**: the entry
+ * stays, because `recall` reads only the memory root and never `brain/`, so
+ * deleting it would take the lesson away from every repo that cannot read
+ * `brain/` at all. Counting every file therefore counts a number that only ever
+ * grows, and a flag that cannot clear by doing the correct thing is precisely
+ * the permanently-unclearable failure #155 already fixed once for the meetings
+ * inbox.
+ *
+ * So promotion is recorded on the entry and a recorded entry stops being
+ * pressure. This is the only edit a tool may make to a capture: the body, the
+ * declared reach and the confidence are never touched.
+ */
+function isPromoted(root: string, rel: string): boolean {
+ let head: string;
+ try {
+ head = readFileSync(join(root, rel), "utf-8").slice(0, 2_000);
+ } catch {
+ return false;
+ }
+ const fm = head.match(/^---\n([\s\S]*?)\n---/);
+ return fm ? /^promoted:\s*\S/m.test(fm[1] ?? "") : false;
+}
+
+/**
+ * Captures awaiting review. Three deliberate differences from the meetings
+ * inbox above.
+ *
+ * **No age threshold.** A capture is unreviewed because nobody has judged it
+ * yet, and that is true the moment it lands. Waiting for it to go stale is how
+ * a real inbox reached eighteen entries in a single day with nothing saying so.
+ *
+ * **Recursive.** The server writes `<root>/<YYYY>/<MM>/<title>.md`, so a flat
+ * listing of the root finds nothing at all and is indistinguishable from a
+ * drained inbox. `walkMarkdown` already handles this.
+ *
+ * **Promotion-aware**, for the reason in `isPromoted`: without it the count can
+ * never fall, because the correct way to drain this inbox leaves every file
+ * exactly where it is.
+ */
+function findMemoryInbox(root: string, relDir: string, nowMs: number): InboxPressure | null {
+ let count = 0;
+ let oldestDays = 0;
+ for (const rel of walkMarkdown(root, relDir)) {
+ // Same reasoning as the meetings scaffold (#155): a shipped README is not
+ // a capture, and counting it makes the flag permanently unclearable.
+ if (/\/readme\.md$/i.test(rel)) continue;
+ if (isPromoted(root, rel)) continue;
+ let mtimeMs: number;
+ try {
+ mtimeMs = statSync(join(root, rel)).mtimeMs;
+ } catch {
+ continue;
+ }
+ count++;
+ const ageDays = Math.floor((nowMs - mtimeMs) / 86_400_000);
+ if (ageDays > oldestDays) oldestDays = ageDays;
+ }
+ return count > 0 ? { count, oldestDays } : null;
+}
+
// ---------------------------------------------------------------------------
// Write-time detectors — the same logic the scan uses, moved to the moment
// of write so drift is caught at the keystroke instead of the next session
@@ -496,6 +588,7 @@ export function scanActiveHygiene(
nowMs: number = Date.now(),
openLoopConfig: OpenLoopConfig = parseOpenLoopConfig(null),
infraRootFilenames: readonly string[] = [],
+ memoryRoot: string = MEMORY_ROOT_DEFAULT,
): ActiveHygieneReport {
return {
completedInActive: findCompletedInActive(root),
@@ -503,6 +596,7 @@ export function scanActiveHygiene(
oversizedNotes: findOversizedNotes(root, infraRootFilenames),
openLoops: findOpenLoops(root, nowMs, openLoopConfig),
inboxPressure: findInboxPressure(root, nowMs),
+ memoryInbox: findMemoryInbox(root, memoryRoot, nowMs),
};
}
@@ -517,13 +611,15 @@ export function formatActiveHygiene(report: ActiveHygieneReport): string[] {
oversizedNotes,
openLoops,
inboxPressure,
+ memoryInbox,
} = report;
if (
completedInActive.length === 0 &&
ungroupedClusters.length === 0 &&
oversizedNotes.length === 0 &&
openLoops.length === 0 &&
- inboxPressure === null
+ inboxPressure === null &&
+ memoryInbox === null
) {
return [];
}
@@ -573,5 +669,15 @@ export function formatActiveHygiene(report: ActiveHygieneReport): string[] {
);
}
+ // Count and oldest only, never the file list. This inbox is meant to hold
+ // captures between sessions, so enumerating it every session would train the
+ // reader to skip the whole block.
+ if (memoryInbox !== null) {
+ if (lines.length > 0) lines.push("");
+ lines.push(
+ `⚠️ ${memoryInbox.count} cross-repo memory capture(s) awaiting review (oldest ${memoryInbox.oldestDays}d). Promote a durable one by COPYING it into the right brain/ note and adding \`promoted: <note>\` to the capture's frontmatter — the entry stays, because recall reads only the memory root and deleting it takes the lesson away from every repo that cannot read brain/ at all. The marker is what lets this count fall.`,
+ );
+ }
+
return lines;
}
diff --git a/.claude/scripts/session-start.ts b/.claude/scripts/session-start.ts
index 2319299..a290b66 100644
--- a/.claude/scripts/session-start.ts
+++ b/.claude/scripts/session-start.ts
@@ -59,6 +59,7 @@ import {
} from "./lib/qmd.ts";
import {
formatActiveHygiene,
+ parseMemoryRoot,
parseOpenLoopConfig,
scanActiveHygiene,
} from "./lib/active-hygiene.ts";
@@ -606,6 +607,7 @@ const hygieneLines = formatActiveHygiene(
Date.now(),
parseOpenLoopConfig(manifestJson),
infraRootFilenames,
+ parseMemoryRoot(manifestJson),
),
);
if (hygieneLines.length > 0) {
diff --git a/.claude/scripts/stop-checklist.ts b/.claude/scripts/stop-checklist.ts
index 2804c38..4462402 100644
--- a/.claude/scripts/stop-checklist.ts
+++ b/.claude/scripts/stop-checklist.ts
@@ -19,6 +19,7 @@ import { readStdinJson } from "./lib/hook-io.ts";
import { triggerDebouncedRefresh } from "./lib/qmd-refresh.ts";
import {
formatActiveHygiene,
+ parseMemoryRoot,
parseOpenLoopConfig,
scanActiveHygiene,
} from "./lib/active-hygiene.ts";
@@ -66,6 +67,7 @@ const hygieneLines = formatActiveHygiene(
Date.now(),
parseOpenLoopConfig(manifestJson),
parseInfraRootFilenames(manifestJson),
+ parseMemoryRoot(manifestJson),
),
);
diff --git a/.claude/scripts/tests/active-hygiene.test.ts b/.claude/scripts/tests/active-hygiene.test.ts
index cc092ca..64b6dfc 100644
--- a/.claude/scripts/tests/active-hygiene.test.ts
+++ b/.claude/scripts/tests/active-hygiene.test.ts
@@ -25,6 +25,7 @@ import {
formatMonolithHint,
isMonolithExempt,
newNoteClusterCandidate,
+ parseMemoryRoot,
parseOpenLoopConfig,
scanActiveHygiene,
walkMarkdown,
@@ -247,6 +248,118 @@ describe("scanActiveHygiene — detectors", () => {
}
});
+ test("the cross-repo memory inbox is counted, nested under year and month", () => {
+ const solo = mkdtempSync(join(tmpdir(), "active-hygiene-memories-"));
+ try {
+ mkdirSync(join(solo, "memories/2026/07"), { recursive: true });
+ const a = join(solo, "memories/2026/07/a lesson.md");
+ writeFileSync(a, "captured");
+ const t = new Date(NOW - 3 * DAY_MS);
+ utimesSync(a, t, t);
+
+ // The defect this closes: every other scan here is flat, and the server
+ // writes two levels down, so a flat walk reports an empty inbox forever.
+ const found = scanActiveHygiene(solo, NOW, DEFAULTS).memoryInbox;
+ assert.ok(found !== null);
+ assert.equal(found!.count, 1);
+ assert.equal(found!.oldestDays, 3);
+ } finally {
+ rmSync(solo, { recursive: true, force: true });
+ }
+ });
+
+ test("a memory capture counts the moment it lands — no age threshold", () => {
+ const solo = mkdtempSync(join(tmpdir(), "active-hygiene-memories-fresh-"));
+ try {
+ mkdirSync(join(solo, "memories/2026/07"), { recursive: true });
+ writeFileSync(join(solo, "memories/2026/07/brand new.md"), "captured");
+ // Undrained because nobody has judged it, which is true immediately.
+ assert.equal(scanActiveHygiene(solo, NOW, DEFAULTS).memoryInbox?.count, 1);
+ } finally {
+ rmSync(solo, { recursive: true, force: true });
+ }
+ });
+
+ test("a shipped README in the memory tree is not a capture", () => {
+ const solo = mkdtempSync(join(tmpdir(), "active-hygiene-memories-readme-"));
+ try {
+ mkdirSync(join(solo, "memories/2026/07"), { recursive: true });
+ writeFileSync(join(solo, "memories/2026/07/README.md"), "how this folder works");
+ // Same permanently-unclearable trap the meetings scaffold already fixed.
+ assert.equal(scanActiveHygiene(solo, NOW, DEFAULTS).memoryInbox, null);
+ } finally {
+ rmSync(solo, { recursive: true, force: true });
+ }
+ });
+
+ test("a declared memory_root is honoured, and the default is not hard-coded", () => {
+ const solo = mkdtempSync(join(tmpdir(), "active-hygiene-memories-root-"));
+ try {
+ mkdirSync(join(solo, "elsewhere/2026/07"), { recursive: true });
+ writeFileSync(join(solo, "elsewhere/2026/07/a lesson.md"), "captured");
+ assert.equal(scanActiveHygiene(solo, NOW, DEFAULTS).memoryInbox, null);
+ assert.equal(
+ scanActiveHygiene(solo, NOW, DEFAULTS, [], parseMemoryRoot('{"memory_root":"elsewhere"}'))
+ .memoryInbox?.count,
+ 1,
+ );
+ } finally {
+ rmSync(solo, { recursive: true, force: true });
+ }
+ });
+
+ // The flag has to be able to reach zero. Promotion is additive, so the entry
+ // stays; without a marker the count could only ever grow, which is the
+ // permanently-unclearable failure #155 already fixed once.
+ test("a promoted capture stops counting, so the flag can clear", () => {
+ const solo = mkdtempSync(join(tmpdir(), "active-hygiene-memories-promoted-"));
+ try {
+ mkdirSync(join(solo, "memories/2026/07"), { recursive: true });
+ const rel = "memories/2026/07/a lesson.md";
+ writeFileSync(join(solo, rel), "---\nscope: general\n---\n\n# a lesson\n");
+ assert.equal(scanActiveHygiene(solo, NOW, DEFAULTS).memoryInbox?.count, 1);
+
+ writeFileSync(
+ join(solo, rel),
+ '---\nscope: general\npromoted: "brain/Gotchas"\n---\n\n# a lesson\n',
+ );
+ assert.equal(scanActiveHygiene(solo, NOW, DEFAULTS).memoryInbox, null);
+ } finally {
+ rmSync(solo, { recursive: true, force: true });
+ }
+ });
+
+ test("an empty or misplaced promoted marker does not silence a capture", () => {
+ const solo = mkdtempSync(join(tmpdir(), "active-hygiene-memories-badmark-"));
+ try {
+ mkdirSync(join(solo, "memories/2026/07"), { recursive: true });
+ // A bare key with no value is not a promotion record and must not hide
+ // a capture from review.
+ writeFileSync(join(solo, "memories/2026/07/x.md"), "---\npromoted:\n---\n\n# x\n");
+ assert.equal(scanActiveHygiene(solo, NOW, DEFAULTS).memoryInbox?.count, 1);
+ // Nor may the word appearing in the BODY rather than the frontmatter.
+ writeFileSync(join(solo, "memories/2026/07/y.md"), "# y\n\npromoted: brain/Thing\n");
+ assert.equal(scanActiveHygiene(solo, NOW, DEFAULTS).memoryInbox?.count, 2);
+ } finally {
+ rmSync(solo, { recursive: true, force: true });
+ }
+ });
+
+ test("the memory-inbox line says COPY, never delete", () => {
+ const lines = formatActiveHygiene({
+ completedInActive: [],
+ ungroupedClusters: [],
+ oversizedNotes: [],
+ openLoops: [],
+ inboxPressure: null,
+ memoryInbox: { count: 4, oldestDays: 2 },
+ });
+ const text = lines.join("\n");
+ assert.match(text, /COPYING it/);
+ assert.match(text, /promoted: <note>/);
+ assert.doesNotMatch(text, /om-intake/);
+ });
+
test("missing folders produce an empty report, not errors", () => {
const empty = mkdtempSync(join(tmpdir(), "active-hygiene-empty-"));
try {
@@ -317,6 +430,7 @@ describe("formatActiveHygiene", () => {
oversizedNotes: [{ path: "work/Fat.md", sizeKb: 40 }],
openLoops: [{ path: "work/1-1/A 2026-01-01.md", ageDays: 20, openItems: 2 }],
inboxPressure: null,
+ memoryInbox: null,
});
const text = lines.join("\n");
assert.match(text, /marked done but still in active\//);
diff --git a/.claude/scripts/tidy-fix.ts b/.claude/scripts/tidy-fix.ts
index 8f081d7..d1d7ebd 100644
--- a/.claude/scripts/tidy-fix.ts
+++ b/.claude/scripts/tidy-fix.ts
@@ -54,6 +54,7 @@ import {
parseInfraRootFilenames,
} from "./lib/session-start.ts";
import {
+ parseMemoryRoot,
parseOpenLoopConfig,
scanActiveHygiene,
walkMarkdown,
@@ -276,6 +277,7 @@ function main(): void {
Date.now(),
parseOpenLoopConfig(manifestJson),
parseInfraRootFilenames(manifestJson),
+ parseMemoryRoot(manifestJson),
);
const report: Report = { fixed: [], refused: [], notes: [] };