A self-organizing Obsidian vault that gives AI coding agents persistent memory.
TypeScript ai-agentsclaude-codecodex-cligemini-cliknowledge-managementobsidianobsidian-vaultpersistent-memorysecond-braintemplate
fix: a retired claim is served only when its correction is served too (#219)
Brenno Ferrari475e1ee39fc1
diff --git a/.claude/scripts/lib/memory-recall.ts b/.claude/scripts/lib/memory-recall.tsindex 11ad9fd..06ba854 100644--- a/.claude/scripts/lib/memory-recall.ts+++ b/.claude/scripts/lib/memory-recall.ts@@ -296,10 +296,70 @@ export function supersededOutOfReach(corpus: readonly MemoryEntry[],caller: Caller,): boolean {- if (entry.facets.superseded_by.length === 0) return false;- const { matched } = resolveSupersedes(entry.facets.superseded_by, corpus);- if (matched.length === 0) return false;- return !matched.some((m) => isVisibleTo(m.facets, caller));+ // A memory that never reached this caller is a SCOPE answer. Reporting it as+ // a supersession one would send the reader after the wrong thing.+ if (!isVisibleTo(entry.facets, caller)) return false;+ return !servedAfterSupersession(corpus, caller).has(entry.rel);+}++/**+ * The rels that survive both filters, resolved together rather than in sequence.+ *+ * Asking only whether a replacement is *scope-visible* is one link short of the+ * invariant. The served set is filtered by visibility AND by this rule, so a+ * replacement can pass `isVisibleTo` and still be removed by the very rule being+ * evaluated. With `A superseded_by B`, `B superseded_by C`, A and B general and C+ * re-filed narrow, a caller outside C's reach kept A: B is visible so A was+ * served, while B was withheld for its own correction being out of reach. The+ * retired claim was served alone, which is the exact failure the rule exists to+ * prevent. Two re-files is not exotic — narrowing is what a correction most+ * usefully does, and the second narrowing is what re-opens it.+ *+ * Computed as a GREATEST fixpoint: begin with everything visible and remove only+ * what is provably unserved. Two properties follow, and both are the reason for+ * this direction rather than the least fixpoint.+ *+ * It terminates, because the set only ever shrinks.+ *+ * And a supersession CYCLE keeps its members rather than losing them. `X`+ * superseding `Y` while `Y` supersedes `X` is metadata rot, not a reach+ * decision, and the least fixpoint would answer it by withholding both — the+ * same silent loss the unresolvable-claim escape hatch was written to avoid.+ * Starting from visible and removing only on proof means neither is dropped+ * because the other could not first be proven.+ */+export function servedAfterSupersession(+ corpus: readonly MemoryEntry[],+ caller: Caller,+): Set<string> {+ const served = new Set<string>();+ for (const e of corpus) if (isVisibleTo(e.facets, caller)) served.add(e.rel);++ // Resolved ONCE, before the loop. Which memories a claim points at does not+ // depend on what is currently served, so recomputing it every pass buys+ // nothing and costs a factor of n: each pass becomes O(n²) and the fixpoint+ // O(n³). Measured on a worst-case chain, where every pass retires exactly one+ // link, that is 12s at 2000 memories against ~30ms hoisted.+ const replacements = new Map<string, string[]>();+ for (const entry of corpus) {+ if (entry.facets.superseded_by.length === 0) continue;+ const { matched } = resolveSupersedes(entry.facets.superseded_by, corpus);+ // Unresolvable claims leave the memory alone, per the rule above, so they+ // are never entered here and can never be withheld.+ if (matched.length === 0) continue;+ replacements.set(entry.rel, matched.map((m) => m.rel));+ }++ for (;;) {+ let removed = false;+ for (const [rel, reps] of replacements) {+ if (!served.has(rel)) continue;+ if (reps.some((r) => served.has(r))) continue;+ served.delete(rel);+ removed = true;+ }+ if (!removed) return served;+ }}// ---------------------------------------------------------------------------@@ -490,6 +550,10 @@ export function recallFrom(// Judged against the agent corpus rather than the raw entries, so a claim is// resolved against the set `recall` can actually serve.const corpus = agentMemories(entries);+ // Resolved once for the whole corpus rather than per entry: the answer for+ // one memory depends on the answer for its replacement, so it is a property+ // of the set and not of the row.+ const served = servedAfterSupersession(corpus, caller);for (const entry of corpus) {const facets = entry.facets;if (!isVisibleTo(facets, caller)) {@@ -499,7 +563,7 @@ export function recallFrom(// Checked after visibility so the two kinds of exclusion stay// distinguishable: a memory that never reached this caller is a scope// answer, not a supersession one.- if (supersededOutOfReach(entry, corpus, caller)) {+ if (!served.has(entry.rel)) {if (explain) withheld.push({ ...entry, why: SUPERSEDED_OUT_OF_REACH });continue;}diff --git a/.claude/scripts/tests/memory-recall.test.ts b/.claude/scripts/tests/memory-recall.test.tsindex 8fba228..b50ee68 100644--- a/.claude/scripts/tests/memory-recall.test.ts+++ b/.claude/scripts/tests/memory-recall.test.ts@@ -472,4 +472,59 @@ describe("a superseded memory reaches only where its successor reaches", () => {const plain = entry("plain", { scope: "general" });assert.equal(supersededOutOfReach(plain, [plain], CALLERS.harbor), false);});++ // The invariant has to hold across the CHAIN, not just across one link.+ // Asking whether the replacement is scope-visible is one link short: the+ // served set is filtered by visibility AND by this rule, so a replacement can+ // pass `isVisibleTo` and still be removed by the rule being evaluated.+ describe("across a chain of corrections", () => {+ const a = entry("A captured wide", { scope: "general", superseded_by: ["B refiled"] });+ const b = entry("B refiled", { scope: "general", superseded_by: ["C narrowed to ios"] });+ const c = entry("C narrowed to ios", { scope: "platform", platforms: ["ios"] });+ const CHAIN = [a, b, c];++ test("a retired claim is not served when its correction is itself withheld", () => {+ const served = recallFrom(CHAIN, CALLERS.harbor).map((m) => m.title);+ assert.deepEqual(served, [], "A was served while B, its correction, was withheld");+ });++ test("the same chain is untouched for a caller who reaches the end of it", () => {+ const served = recallFrom(CHAIN, CALLERS.atlas).map((m) => m.title);+ assert.deepEqual(served.sort(), ["A captured wide", "B refiled", "C narrowed to ios"].sort());+ });++ test("explain still names supersession rather than scope for the middle link", () => {+ const { withheld } = recallFrom(CHAIN, CALLERS.harbor, { explain: true });+ assert.equal(withheld.find((m) => m.title === "B refiled")?.why, SUPERSEDED_OUT_OF_REACH);+ assert.equal(withheld.find((m) => m.title === "A captured wide")?.why, SUPERSEDED_OUT_OF_REACH);+ });+ });++ // Depth, not just the three-link case. The fixpoint has to keep retiring+ // links until the whole chain is gone; a version that stopped after a single+ // pass would still serve everything from the second link back.+ test("a long chain retires all the way back to its head", () => {+ const chain = Array.from({ length: 8 }, (_, i) =>+ entry(+ `L${i}`,+ i === 7+ ? { scope: "platform", platforms: ["ios"] }+ : { scope: "general", superseded_by: [`L${i + 1}`] },+ ),+ );+ assert.deepEqual(recallFrom(chain, CALLERS.harbor).map((m) => m.title), []);+ // And the same chain is untouched for a caller reaching its terminal link.+ assert.equal(recallFrom(chain, CALLERS.atlas).length, 8);+ });++ // A cycle is metadata rot, not a reach decision. The greatest fixpoint keeps+ // both rather than withholding both, which is what the least fixpoint would+ // do and is the same silent loss the unresolvable-claim hatch avoids. It also+ // has to terminate, which is the half a naive recursion gets wrong.+ test("a supersession cycle terminates and keeps its members", () => {+ const x = entry("X", { scope: "general", superseded_by: ["Y"] });+ const y = entry("Y", { scope: "general", superseded_by: ["X"] });+ const served = recallFrom([x, y], CALLERS.harbor).map((m) => m.title);+ assert.deepEqual(served.sort(), ["X", "Y"]);+ });});