From 7517f2c662895bdf4bf355d47d4f60e7bc15fa23 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 22 May 2026 17:03:50 +0200 Subject: [PATCH] Fix: don't create duplication errors --- plugs/index/relation.test.ts | 19 +++++++++++++++++++ plugs/index/relation.ts | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/plugs/index/relation.test.ts b/plugs/index/relation.test.ts index 97b3da7b..f321db09 100644 --- a/plugs/index/relation.test.ts +++ b/plugs/index/relation.test.ts @@ -277,6 +277,25 @@ test("co-mention pairs attribute targets in the same item", async () => { expect(pairs.has("Super Team->Angela")).toBe(true); }); +test("co-mention emits unique refs when target appears multiple times in different scopes", async () => { + const { space } = createMockSystem(); + await space.writePage("A", ""); + await space.writePage("B", ""); + // [[A]] shares a (nested) scope with both occurrences of [[B]]; without + // ref-granularity dedupe, both j-iterations would emit the same ref + // (A's position + B's name) but with different `via` scopes. + const text = + "* outer\n * [[A]]\n * [[B]]\n * also [[B]] here\n"; + const tree = parseMarkdown(text); + const fm = extractFrontMatter(tree); + const objects = await indexRelations(pageMeta("P"), fm, tree, text); + const coms = objects.filter((o) => + o.tag === "relation" && o.kind === "co-mention" + ); + const refs = coms.map((r) => r.ref); + expect(new Set(refs).size).toEqual(refs.length); +}); + test("co-mention carries fromTag/toTag from target relations", async () => { const { space } = createMockSystem(); await space.writePage("Jack", ""); diff --git a/plugs/index/relation.ts b/plugs/index/relation.ts index 3a64e9f6..e7a0fd39 100644 --- a/plugs/index/relation.ts +++ b/plugs/index/relation.ts @@ -523,7 +523,7 @@ function emitCoMentions(ctx: EmitCtx, tree: ParseTree): void { if (sharedPos === undefined) continue; const via = `${ctx.pageMeta.name}@${sharedPos}`; - const key = `${a.rec.to}->${b.rec.to}@${via}`; + const key = `${a.rec.range![0]}->${b.rec.to}`; if (seenDirected.has(key)) continue; seenDirected.add(key);