Skip to content

feat(markdown): reference files named by backticked paths (#5224) - #5352

Open
vaibhav375 wants to merge 3 commits into
Graphify-Labs:v8from
vaibhav375:feat/5224-code-span-file-references
Open

vaibhav375 wants to merge 3 commits into
Graphify-Labs:v8from
vaibhav375:feat/5224-code-span-file-references

Conversation

@vaibhav375

@vaibhav375 vaibhav375 commented Nov 19, 2026 •

Copy link
Copy Markdown

What does this PR do?

Fixes #5224.

An AST/structural scan resolved explicit Markdown links to code and Markdown files. A backticked path to the same files (`src/app.ts`, `docs/other.md`) produced no references edge. This PR gives a code span holding a file path the same doc->file references edge the equivalent link produces: EXTRACTED, from the page, with the span's source location.

How a span is resolved:

  • First relative to the document, then relative to the scan root. Spans that start with ./ or ../ are read relative to the document only.
  • Never by suffix. An ambiguous, missing, outside-root, fenced or non-file span gets no edge.

Implementation, reusing the link paths that already exist:

  • Document targets (.md, .mdx, .qmd, .markdown, .rst, .txt) are linked at extraction through add_link (graphify/extractors/markdown.py). They get the target_file stamp, so the watch rebuild carries them the way it carries authored doc->doc links. The target must be an existing file inside the scan root. The span's path is chosen by the indexed repo, so each candidate goes through _resolve_safe (from the GHSA-g6c9-j4vm-mwq4 fix) first: a UNC (//host/share) or out-of-tree path is refused before anything stats or resolves it. Realpaths are compared, so a symlinked root such as macOS's /var -> /private/var works.
  • Source-file targets stay citation raw calls, as in [Bug]: markdown mentions: a bare word in a code span still links to an unrelated free function (follow-up to #4609) #4804.
    • The raw call now also carries the span's scan-root reading as a document-relative path (root_path).
    • resolve_markdown_mentions tries both readings against the scanned corpus with the existing _match_file_link, and mints page --references--> file (graphify/markdown_resolution.py).
    • Code targets are matched against the corpus, not the disk, so a warm-cache rebuild gives the same edges as a fresh build.
    • [Bug]: markdown mentions: a bare word in a code span still links to an unrelated free function (follow-up to #4609) #4804 evidence is unchanged: a source-file span still licenses bare mentions in its section, and a document span is never evidence.
  • _AST_CACHE_SCHEMA is bumped to 52 with a chained prior (51) note (rebased onto v8 3671f311, whose release took 51), so cached Markdown extractions re-run. Renumber as needed when landing.

The existing test test_file_span_is_evidence_but_not_a_doc_file_edge pinned the old "a span is evidence, not a link" behavior from #4806. It is renamed and updated to expect the doc->file edge this issue asks for.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariant: fail closed. A span only becomes an edge when it names exactly one existing, scanned file through a document-relative or scan-root-relative path. In every other case the graph is unchanged from today: no suffix guesses, no edge to a file outside the root, and no edge to a heading named like the file. Persisted state: Markdown AST cache entries are invalidated by the schema bump. No graph.json schema change.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

New tests in tests/test_markdown_code_spans.py. All of them fail against v8's markdown.py, markdown_resolution.py and cache.py, except the no-edge boundary test:

  • The issue's fixture, next to the equivalent links: identical edges, EXTRACTED, L3.
  • Document-relative before scan-root-relative.
  • Boundary cases that must not link: an ambiguous suffix, missing files, an escaping ../../ path to a real file outside the root, ./x with only a root-level x, self-citation, fenced spans, and non-file spans.
  • A heading titled like the file is not the target.
  • An absolute root reached through a symlink.
  • Targets created after the first build appear on the next full _rebuild_code (warm cache vs fresh build).
  • An unscanned same-named file on disk does not shadow the scanned one or its [Bug]: markdown mentions: a bare word in a code span still links to an unrelated free function (follow-up to #4609) #4804 evidence.
  • A span naming //evil-host/share/notes.md or an escaping ../../outside.md (a real file outside the corpus) never reaches os.stat/os.lstat: the test records every call. It fails on the previous commit, which probed /evil-host/share/....
  • A scan root on another drive (Windows os.path.relpath ValueError) does not abort the document's extraction.
  • _rebuild_code runs in subprocesses, as in the existing rebuild tests:
    • span edges survive re-extraction of the citing doc and of the target;
    • edges are pruned when the span is removed;
    • ./ and ../ document spans resolve identically in partial and full rebuilds, in a corpus with code and in a docs-only corpus.

I also mutated each new rule (the scan-root reading, the .//../ restriction on both paths, scan-root containment, and realpath), and each mutation fails at least one test.

Limitations / unsupported cases

  • When both the document-relative and the scan-root-relative file exist, the document-relative one wins, the same precedence _match_cited_file documents.

  • A span inside link text, like [`src/app.ts`](../src/other.ts), references both the span's file and the link's.

  • Backslash paths and paths with spaces are not file spans, the same as today.

  • Without an active scan root, document spans do not link. A direct extract() with no root= gets document spans only that way. Source-file spans resolve against the corpus either way.

  • Pre-existing, also on 0.9.113 with plain links, so not changed here: in a partial _rebuild_code, doc->source-file citations inherit two behaviors of the link path. Spans now behave exactly like links in both:

    1. [a](./app.ts) from docs/ gets an edge to the root app.ts when docs/app.ts doesn't exist. _match_file_link's rootless fallback accepts it.
    2. [code](../src/app.ts) loses its edge when another scanned file is also named app.ts.

    Happy to open a separate issue for those.

How was this tested?

uv sync --all-extras
uv run pytest tests/test_markdown_code_spans.py -q            # 56 passed
uv run pytest tests/ -q --tb=short                            # all pass on v8 3671f311 (11797 passed in one run with a long custom
    # --basetemp to save disk; the 6 tests that path-length-failed there pass with the default tmp dir)
    # the 11 failures are all tests/test_skillgen.py and fail identically on v8 here:
    # they read a pinned commit my shallow clone (--depth 200) does not have
uv run ruff check .                                           # All checks passed
# Python 3.11.5, 3.13.4 and 3.14.9 (as the bot asks), separate venvs, after the review follow-up commit:
uv run --python 3.11 pytest tests/test_watch*.py tests/test_cache*.py tests/test_markdown*.py tests/test_extract.py   # 778 passed, 16 skipped, 1 xfailed
uv run --python 3.13 pytest ...same files...                                                                       # 778 passed, 16 skipped, 1 xfailed
uv run pyright graphify/extractors/markdown.py graphify/markdown_resolution.py \
  graphify/cache.py tests/test_markdown_code_spans.py         # no new errors (5 pre-existing in cache.py)
pre-commit (skillgen --check, ruff) on commit                 # Passed

# Issue repro.py, verbatim, macOS default temp dir (/var -> /private/var)
graphifyy 0.9.113:  docs/links.md -> docs/other.md, docs/links.md -> src/app.ts
this branch:        + docs/spans.md -> docs/other.md, docs/spans.md -> src/app.ts

# CLI on a fixture (src/app.ts, docs/other.md, docs/spans.md, docs/links.md)
graphify update .
   docs/links.md -> docs/other.md L3 EXTRACTED
   docs/links.md -> src/app.ts L3 EXTRACTED
   docs/spans.md -> docs/other.md L3 EXTRACTED
   docs/spans.md -> src/app.ts L3 EXTRACTED
graphify explain "spans.md"
  --> src/app.ts [references] [EXTRACTED] docs/spans.md:L3
  --> other.md [references] [EXTRACTED] docs/spans.md:L3
# then incremental `graphify update .`: editing spans.md or other.md keeps the doc edge;
# switching to `./other.md` plus dead `./notes.md` / `./missing.ts` spans gives only the
# docs/other.md edge; removing the span removes the edge.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments.
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

No skill fragments or generated artifacts are touched, so the first box doesn't apply. Extraction reads the active scan root and the filesystem for document targets, and every corpus-chosen path is gated on _resolve_safe before any stat (GHSA-g6c9-j4vm-mwq4). Nothing is interpolated into a shell command or into Python source.

@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @vaibhav375. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic.

@vaibhav375
vaibhav375 marked this pull request as ready for review November 20, 2026 07:53

@graphify-labs graphify-labs 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.

Graphify review View in Graphify

Nothing blocking · 5 possible issues to check · Merge confidence 5/5

5 possible issues to check · High impact

Markdown code spans that name files can now create references to scanned documents and source files.

5 possible issues we couldn't confirm

They don't change the score.

Code graph · 4064 functions depend on this change
  • 2 functions became more coupled. extract() (1337 callers), extract_markdown() (23 callers).
  • 63 of 613 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed c5b2dda · 6 earlier findings not re-checked · Full report in the Graphify check

@vaibhav375

Copy link
Copy Markdown
Author

Thanks for the review. I went through all seven leads. Changes are in 6201c7a:

  • Windows cross-drive os.path.relpath: this was a real bug, now fixed. The scan root's path from the document is computed once and guarded like the call in add_doc_span. A span on another drive keeps only its document-relative reading. test_scan_root_on_another_drive_does_not_abort_the_document fails on 9f1df50 with the ValueError and passes now.
  • No scan root means document spans don't link: this is intentional. Without a root I can't check that the target stays inside it, so the span fails closed. The add_doc_span docstring now says this, and it's listed under limitations in the PR description.
  • README page lookup in the test: it now reads source_file with .get.

The other four are by design:

  • Explicit ./ / ../ falling back to root_path: root_path is only recorded when the cite doesn't start with ./ or ../ (markdown.py#L572). I added a comment at the resolver loop saying so. The no-edge test covers it with ./top.md and ./top.ts, where only root-level files exist. Recording root_path for explicit cites makes that test fail.
  • Symlink test: the extractor gets the resolved root from extract(), while collect_files(link, root=link) keeps the documents' symlinked path. So the mismatch is exercised. If the realpath on the document's directory (markdown.py#L541) is removed, test_file_path_spans_resolve_under_a_symlinked_absolute_root fails.
  • Page vs heading as the edge source: add_link always sources a doc->doc reference from the page (markdown.py#L706), whatever the owner, and the watch reconciler carries authored doc links per page. Spans follow that, so a span and a link to the same file give one edge, not two.
  • callee gate: a path-only span carries callee: "" (markdown.py#L563). Pass 1 skips those, so the new loop is the only place they become edges, and that's exactly what it selects. test_file_path_spans_reference_code_and_document_files fails without it.

@vaibhav375
vaibhav375 force-pushed the feat/5224-code-span-file-references branch from 6201c7a to caf0cc5 Compare November 21, 2026 12:55
vaibhav375 and others added 3 commits November 23, 2026 00:33
…abs#5224)

A Markdown inline code span holding a file path (`src/app.ts`,
`docs/other.md`) now becomes a doc->file `references` edge, the same
edge the equivalent authored link produces. The path is read relative
to the document, then (unless it starts with `./` or `../`) relative to
the scan root; it is never matched by suffix, so an ambiguous, missing,
outside-root, fenced or non-file span gets no edge.

- Document targets are linked at extraction through the existing
  `add_link`, so they get the `target_file` stamp and the watch
  reconciliation authored links already have.
- Source-file targets stay citations: the raw call carries the span's
  scan-root reading (`root_path`, a document-relative path computed
  from realpaths, so symlinked roots work) and the resolver matches
  both readings against the scanned corpus with `_match_file_link`.
  Nothing depends on files existing at extraction time, and Graphify-Labs#4804
  evidence is unchanged.
- Bump `_AST_CACHE_SCHEMA` to 50 so cached Markdown extractions re-run.

Co-Authored-By: Claude <noreply@anthropic.com>
…raphify-Labs#5224)

`os.path.relpath` raises ValueError on Windows when the scan root and
the document are on different drives. The scan-root reading for a
source-file span called it unguarded, so such a document would fail to
extract. Compute the root's path from the document once, guarded like
the sibling call in `add_doc_span`; without it the span keeps only its
document-relative reading.

Also say in the docstrings that document spans need an active scan root
and that `root_path` is never recorded for explicit `./` / `../` cites,
and read `source_file` with `.get` in the README page lookup test.

Co-Authored-By: Claude <noreply@anthropic.com>
…fy-Labs#5224)

`add_doc_span` resolved and stat-ed the path a code span named before
checking that it stayed inside the scan root. A span is corpus-chosen,
so `//host/share/notes.md` reached `realpath`/`isfile`, which on Windows
authenticates to that host over SMB, and `../../outside.md` leaked
whether a host file exists: the class GHSA-g6c9-j4vm-mwq4 closed for
bash sources.

Gate each candidate on `_resolve_safe` from the bash extractor, which
refuses a UNC or out-of-tree path on the unresolved candidate before
anything touches the filesystem, then resolves and re-checks.

Co-Authored-By: Claude <noreply@anthropic.com>
@vaibhav375
vaibhav375 force-pushed the feat/5224-code-span-file-references branch from caf0cc5 to c5b2dda Compare November 22, 2026 22:55

This branch has not been deployed

No deployments
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.

Create references edges for unambiguous backticked Markdown file paths

1 participant