Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(links): resolve path-qualified wikilinks outside DIR_PATTERN in the DB/put_page path (#2866)
paul-03201fabbb9849f2
diff --git a/src/core/link-extraction.ts b/src/core/link-extraction.tsindex c0fc2644a1..8f27903e65 100644--- a/src/core/link-extraction.ts+++ b/src/core/link-extraction.ts@@ -489,7 +489,22 @@ export async function extractPageLinks(// text inside `[[...]]` before any `|`), NOT the display alias// (ref.name = match[2]). `[[struktura|the project]]` must resolve// `struktura`, not "the project". The display text is for context only.- const matches = await resolver.resolveBasenameMatches(ref.slug);+ //+ // The literal may be path-qualified (`[[notes/struktura]]`). The FS+ // path (resolveSlugAll) strips the dirname before its basename lookup,+ // but this path passed the raw literal to an index keyed by final+ // segments only — so every slash-containing wikilink outside+ // DIR_PATTERN silently resolved to nothing. Query by the final+ // segment, then use the written path as a disambiguation filter+ // (the analogue of the FS ancestor walk honoring the written path):+ // a match must end with the literal, so `[[notes/struktura]]` can+ // resolve to `vault/notes/struktura` but never to `wiki/struktura`.+ const slashIdx = ref.slug.lastIndexOf('/');+ const basename = slashIdx === -1 ? ref.slug : ref.slug.slice(slashIdx + 1);+ let matches = await resolver.resolveBasenameMatches(basename);+ if (slashIdx !== -1) {+ matches = matches.filter(m => m === ref.slug || m.endsWith(`/${ref.slug}`));+ }if (matches.length === 0) continue;const idx = content.indexOf(ref.slug);const context = idx >= 0 ? excerpt(content, idx, 240) : ref.name;diff --git a/test/e2e/global-basename-pglite.test.ts b/test/e2e/global-basename-pglite.test.tsindex d3c767b46c..3c5af045b1 100644--- a/test/e2e/global-basename-pglite.test.ts+++ b/test/e2e/global-basename-pglite.test.ts@@ -172,6 +172,59 @@ describe('issue #972 — DB-source (gbrain extract links --source db)', () => {expect(strk!.link_type).toBe('wikilink_basename');});+ test('flag ON → path-qualified wikilink outside DIR_PATTERN resolves via DB path', async () => {+ // `[[notes/struktura]]` — `notes` is not in DIR_PATTERN, so the ref+ // reaches the generic pass with its dirname intact. Regression: the DB+ // path queried the basename index with the raw literal (which is keyed+ // by final segments only), so path-qualified wikilinks outside+ // DIR_PATTERN silently produced zero edges while the FS path resolved+ // the identical content.+ await engine.putPage('notes/struktura', {+ type: 'concept' as any, title: 'Struktura Notes',+ compiled_truth: '', timeline: '',+ });+ await engine.putPage('concepts/knowledge-graph', {+ type: 'concept', title: 'Knowledge Graph',+ compiled_truth: 'Background in [[notes/struktura]].', timeline: '',+ });+ await engine.setConfig('link_resolution.global_basename', 'true');++ await runExtract(engine, ['links', '--source', 'db']);++ const outLinks = await engine.getLinks('concepts/knowledge-graph');+ const strk = outLinks.find(l => l.to_slug === 'notes/struktura');+ expect(strk).toBeDefined();+ expect(strk!.link_type).toBe('wikilink_basename');+ expect(strk!.link_source).toBe('wikilink-resolved');+ });++ test('path-qualified wikilink never attaches to a basename-only sibling', async () => {+ // Both notes/struktura and wiki/struktura exist. The author wrote+ // `[[notes/struktura]]` — the written path must exclude wiki/struktura+ // (a bare `[[struktura]]` would legitimately match both).+ await engine.putPage('notes/struktura', {+ type: 'concept' as any, title: 'Struktura Notes',+ compiled_truth: '', timeline: '',+ });+ await engine.putPage('wiki/struktura', {+ type: 'concept' as any, title: 'Struktura Wiki',+ compiled_truth: '', timeline: '',+ });+ await engine.putPage('concepts/x', {+ type: 'concept', title: 'X',+ compiled_truth: 'See [[notes/struktura]].', timeline: '',+ });+ await engine.setConfig('link_resolution.global_basename', 'true');++ await runExtract(engine, ['links', '--source', 'db']);++ const outLinks = await engine.getLinks('concepts/x');+ const basenameLinks = outLinks+ .filter(l => l.link_type === 'wikilink_basename')+ .map(l => l.to_slug);+ expect(basenameLinks).toEqual(['notes/struktura']);+ });+test('flag OFF → no basename edges via DB path (back-compat)', async () => {await engine.putPage('projects/struktura', {type: 'project', title: 'Struktura',diff --git a/test/link-extraction.test.ts b/test/link-extraction.test.tsindex e0a7412208..f7c003ab5d 100644--- a/test/link-extraction.test.ts+++ b/test/link-extraction.test.ts@@ -403,6 +403,77 @@ describe('extractPageLinks', () => {expect(candidates).toEqual([]);});+ test('path-qualified wikilink outside DIR_PATTERN queries by final segment', async () => {+ // `[[notes/struktura]]` (dir not in DIR_PATTERN) falls to the generic+ // pass. The resolver's basename index is keyed by final path segments,+ // so the lookup must strip the dirname — mirroring the FS path+ // (resolveSlugAll). Regression: the raw literal was passed through,+ // which never matched, so these links silently dropped.+ const seen: string[] = [];+ const resolver: SlugResolver = {+ resolve: async () => null,+ resolveBasenameMatches: async (name) => {+ seen.push(name);+ return name === 'struktura' ? ['notes/struktura'] : [];+ },+ };+ const { candidates } = await extractPageLinks(+ 'concepts/x', 'See [[notes/struktura]].',+ {}, 'concept', resolver, { globalBasename: true },+ );+ expect(seen).toContain('struktura');+ expect(seen).not.toContain('notes/struktura');+ expect(candidates.map(c => c.targetSlug)).toEqual(['notes/struktura']);+ expect(candidates[0].linkType).toBe('wikilink_basename');+ expect(candidates[0].linkSource).toBe('wikilink-resolved');+ });++ test('path-qualified wikilink keeps only matches ending with the written path', async () => {+ // The written path disambiguates: `[[notes/struktura]]` must never+ // attach to `wiki/struktura` even though both share the basename.+ const resolver: SlugResolver = {+ resolve: async () => null,+ resolveBasenameMatches: async (name) =>+ name === 'struktura' ? ['notes/struktura', 'wiki/struktura'] : [],+ };+ const { candidates } = await extractPageLinks(+ 'concepts/x', 'See [[notes/struktura]].',+ {}, 'concept', resolver, { globalBasename: true },+ );+ expect(candidates.map(c => c.targetSlug)).toEqual(['notes/struktura']);+ });++ test('path-qualified wikilink matches a deeper real slug by path suffix', async () => {+ // The page lives at vault/notes/struktura; the author wrote the shorter+ // tail `[[notes/struktura]]`. Suffix matching connects them, while the+ // basename-only sibling `wiki/struktura` stays excluded.+ const resolver: SlugResolver = {+ resolve: async () => null,+ resolveBasenameMatches: async (name) =>+ name === 'struktura' ? ['vault/notes/struktura', 'wiki/struktura'] : [],+ };+ const { candidates } = await extractPageLinks(+ 'concepts/x', 'See [[notes/struktura]].',+ {}, 'concept', resolver, { globalBasename: true },+ );+ expect(candidates.map(c => c.targetSlug)).toEqual(['vault/notes/struktura']);+ });++ test('path-qualified self-link is dropped like the bare form', async () => {+ // `[[notes/struktura]]` written on notes/struktura itself must not+ // produce a self-loop (same guard as the bare `[[own-tail]]` case).+ const resolver: SlugResolver = {+ resolve: async () => null,+ resolveBasenameMatches: async (name) =>+ name === 'struktura' ? ['notes/struktura'] : [],+ };+ const { candidates } = await extractPageLinks(+ 'notes/struktura', 'See [[notes/struktura]].',+ {}, 'concept', resolver, { globalBasename: true },+ );+ expect(candidates).toEqual([]);+ });+test('bare wikilink resolution does not interfere with DIR_PATTERN wikilinks', async () => {// 2b refs (people/alice) take the verb-inferred type;// 2c refs (struktura) take wikilink_basename. Same call.