Skip to content

fix(core): ignore wikilinks in inline code - #1337

Open
yaodong-shen wants to merge 1 commit into
basicmachines-co:mainfrom
yaodong-shen:fix/1332-ignore-inline-code-wikilinks
Open

fix(core): ignore wikilinks in inline code#1337
yaodong-shen wants to merge 1 commit into
basicmachines-co:mainfrom
yaodong-shen:fix/1332-ignore-inline-code-wikilinks

Conversation

@yaodong-shen

Copy link
Copy Markdown

Fixes #1332.

MarkdownIt already emits code_inline tokens before Basic Memory relation parsing. This change filters those literal spans when deriving relations, so quoted wikilinks no longer create graph edges. Ordinary prose links and explicit relation syntax remain unchanged.

Tests:

  • uv run pytest --no-cov tests/markdown -q (93 passed)
  • uv run ty check src tests test-int
  • uv run ruff check src/basic_memory/markdown/plugins.py tests/markdown/test_relation_edge_cases.py
  • uv run ruff format --check src/basic_memory/markdown/plugins.py tests/markdown/test_relation_edge_cases.py
  • uv run basic-memory doctor

@CLAassistant

CLAassistant commented Aug 26, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b9506c7c96

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/basic_memory/markdown/plugins.py Outdated
content = token.tag or token.content
if not token.children:
return content
return "".join(child.content for child in token.children if child.type != "code_inline")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve Markdown structure while masking code spans

When an inline token has children, joining child.content no longer preserves the source Markdown: MarkdownIt has already consumed escapes and formatting, while this filter also removes every code span wholesale. Consequently, \[\[Literal]] is reconstructed as [[Literal]] and creates a phantom relation, while an explicit relation such as - implemented_by [[Parser]] (`parse()`) loses its context entirely. These incorrect derived relations are recreated on every parse or reindex, so code ranges should be masked without rebuilding the remaining text from normalized child contents.

AGENTS.md reference: AGENTS.md:L161-L166

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in f79a4a4. Relation parsing now masks matched code spans from the original inline source, preserving positions and retaining escaped Markdown and relation context. It also skips escaped wikilink delimiters. Added coverage for escaped literals, inline-code context, and a leading code span. Validation: 94 Markdown tests passed; ty, Ruff, format, and diff checks passed.

@yaodong-shen
yaodong-shen force-pushed the fix/1332-ignore-inline-code-wikilinks branch from b9506c7 to f79a4a4 Compare August 26, 2026 10:39
@yaodong-shen

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f79a4a4c7d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/basic_memory/markdown/plugins.py Outdated
position = 0

while position < len(content):
if content[position] != "`":

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Skip escaped backticks when masking code spans

When a wikilink is surrounded by backslash-escaped backticks, such as \see [[Target]]`, CommonMark treats those backticks as literal punctuation rather than code delimiters. This scanner nevertheless matches the runs and masks the entire wikilink, permanently omitting its links_to` relation on every parse and reindex; skip escaped opener runs before searching for their closer.

AGENTS.md reference: AGENTS.md:L161-L166

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 0f6c538. The inline-code scanner now skips backtick openers escaped by an odd-length backslash run, matching CommonMark tokenization, so literal escaped backticks keep their real wikilinks. Added a regression assertion for \see [[Target]]``. Validation: 94 Markdown tests passed; ty, Ruff, format, and diff checks passed.

@yaodong-shen
yaodong-shen force-pushed the fix/1332-ignore-inline-code-wikilinks branch from f79a4a4 to 0f6c538 Compare August 26, 2026 10:47
@yaodong-shen

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0f6c53848c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/basic_memory/markdown/plugins.py Outdated
content = (token.tag or token.content).strip()
return _parse_explicit_relation(content)
source_content = _relation_content(token)
content = _mask_inline_code(source_content)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve trailing code when classifying explicit relations

When a list-item relation has a non-parenthesized trailing code span, such as - calls [[Parser]] parse()``, masking turns the entire tail into whitespace before _parse_explicit_relation applies its prose-tail guard. The line therefore becomes a typed `calls` relation and silently omits `parse()` instead of falling back to `links_to` as it did before this change. Reindexing reparses the same source and recreates this incorrect relation, so preserve the presence of non-context source text while excluding only wikilink delimiters inside code.

AGENTS.md reference: AGENTS.md:L161-L165

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in e52c4da with a root-cause change. The handwritten backtick scanner has been removed: a wrapped MarkdownIt backtick rule records the exact code spans that the parser recognizes. Relation parsing masks only bracket characters inside those spans, so non-context trailing code remains visible to the prose-tail guard and correctly falls back to links_to. Added regressions for trailing code, leading code, escaped text, and a literal directive in code.

Comment thread src/basic_memory/markdown/plugins.py Outdated
Comment on lines +160 to +164
scan = position
while scan < len(content):
if content[scan] != "`":
scan += 1
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid rescanning unmatched backtick runs

When an inline token contains many unmatched backtick runs of distinct lengths, every candidate opener scans the entire remaining suffix looking for a same-length closer, making this loop quadratic in the number of runs. Because relation_rule invokes the scanner for every inline token even when it contains no wikilink, a generated 321 KB line with 800 increasing runs takes roughly 10 seconds in this loop alone and can stall parsing or reindexing; cache unsuccessful delimiter lengths or scan/index the runs once.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in e52c4da by delegating all code-span recognition to MarkdownIts own cached inline backtick rule rather than rescanning delimiter runs. Added an 800 increasing-length unmatched-run regression that preserves the real wikilink; local parser smoke completed in 0.043s. Full validation: 95 Markdown tests passed; ty, Ruff, format, and diff checks passed.

@yaodong-shen
yaodong-shen force-pushed the fix/1332-ignore-inline-code-wikilinks branch from 0f6c538 to e52c4da Compare August 26, 2026 11:01
@yaodong-shen

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e52c4dab34

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +178 to +179
if masked[position] in "[]":
masked[position] = " "

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve code-span brackets inside outer wikilink targets

When a real outer wikilink contains bracket text quoted by an inline code span, such as [[API [v2]]] or [[Outer [[literal]] Target]], this loop replaces those literal brackets with spaces and parse_inline_relations then extracts the target from that masked string. The source-preserving extraction added for explicit relations does not cover this implicit path, so every parse or reindex persistently creates an edge to the wrong target; scan using the masked content but extract the target from the original source positions.

AGENTS.md reference: AGENTS.md:L161-L165

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed as valid against the current branch, so I am not rejecting it. Addressed in 7caf720 as part of the source-preservation invariant: masked text is now used only to locate valid wikilink boundaries, while parse_inline_relations extracts the target from the same positions in original source text. Added regressions for [[API [v2]]] and [[Outer [[literal]] Target]]; both now retain their exact targets. Validation: 95 Markdown tests passed; ty, Ruff, format, and diff checks passed.

Signed-off-by: buduoqiu <shenyaodong@hhu.edu.cn>
@yaodong-shen
yaodong-shen force-pushed the fix/1332-ignore-inline-code-wikilinks branch from e52c4da to 7caf720 Compare August 26, 2026 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Wikilinks inside inline code spans are parsed as relations (fenced blocks are correctly ignored)

2 participants