Skip to content

fix(detect): prune a linked worktree whose main checkout is in the scan root (#4683) - #4833

Closed
barlowa124 wants to merge 1 commit into
Graphify-Labs:v8from
barlowa124:fix/4683-sibling-worktree
Closed

barlowa124 wants to merge 1 commit into
Graphify-Labs:v8from
barlowa124:fix/4683-sibling-worktree

Conversation

@barlowa124

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4683. A workspace folder holding a repo and a linked worktree of it as siblings (ws/app/ + ws/app-feature/) was indexed twice, so every file in the worktree counted as a second copy. On the reporter's ~78k-node graph this added ~13.8k duplicate nodes, and the copy's paths surfaced in GRAPH_REPORT.md community hubs.

Neither existing guard reaches this layout. The name-based rules require a .worktrees/ or <dot>/worktrees/ directory name, and .git/info/exclude only exists when the scan root is itself a repo. A linked worktree is still unambiguous on disk. Its .git is a file pointing at <main>/.git/worktrees/<name>, and that gitdir's commondir points back to <main>/.git, so the main checkout's root is the common dir's parent. When that root resolves inside the scan root, the directory is pruned and recorded in pruned_noise_dirs like other noise prunes.

Type of change

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

Verification & Invariants

Invariant: a corpus file is indexed once, and a directory is pruned only when it is provably a second checkout of a repo already under the scan root, never for carrying a .git file alone.

The prune fires only when the worktree's commondir resolves to a <name>/.git directory whose parent (the main worktree root) lies inside the resolved scan root. A submodule's .git file points at <super>/.git/modules/<name> with no commondir, so it is untouched. A bare repo's shared git dir is not named .git and has no main worktree, so its worktrees stay indexed. A moved or deleted main checkout leaves commondir dangling, and the worktree stays indexed as the only copy. Scanning the worktree itself still indexes it, and no git subprocess is spawned since the marker files are read directly. No persisted state changes, the pruned dir never enters files, so detect_incremental and the manifest see the same corpus a hand-built ignore rule would produce.

  • 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.

How was this tested?

uv run pytest tests/test_detect.py -q -k "worktree or submodule_gitdir"
uv run pytest tests/test_detect.py -q          # 330 passed
uv run pytest tests/ -q                        # 8554 passed; 18 pre-existing
                                               # failures (test_skillgen.py,
                                               # test_ext_tagged_id_clash.py)
                                               # identical on clean upstream/v8
uv run ruff check graphify/detect.py tests/test_detect.py
uv run pyright graphify/detect.py              # 9 errors, identical on v8

test_detect_prunes_sibling_linked_worktree fails on upstream/v8 (AssertionError: linked worktree indexed as a second copy) and passes with the change. Three boundary tests cover the cases that must not change, the worktree as scan root, a worktree whose main checkout is outside the root, and a submodule gitdir without commondir.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (No source fragments changed.)
  • 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.

Known limitation: a repo cloned with --separate-git-dir has its common git dir outside the worktree and not named .git, so its linked worktrees are not deduplicated, they fail open and stay indexed.

…an root (Graphify-Labs#4683)

A workspace folder holding a repo and a linked worktree of it as siblings
was indexed twice: every file in the worktree counted as a second copy,
roughly doubling the graph and leaking the copy's paths into reports.

The name-based rules (.worktrees/, <dot>/worktrees/) and .git/info/exclude
cannot cover this layout — the worktree's directory name is arbitrary, and
a workspace root that is not itself a repo has no info/exclude to read.

A linked worktree is unambiguous on disk: its .git is a file pointing at
<main>/.git/worktrees/<name>, whose commondir points back to <main>/.git,
so the main checkout's root is the common dir's parent. When that main
root resolves inside the scan root, the worktree is pruned and recorded
in pruned_noise_dirs.

Deliberately unchanged: scanning the worktree itself still indexes it; a
worktree whose main is outside the root stays (it is the only copy);
submodule gitdirs (no commondir) and bare-repo worktrees (shared git dir
not named .git) are untouched. No git subprocess is used.

Co-Authored-By: Devin <[email protected]>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @barlowa124. 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.10 and 3.13, and that the change keeps extraction deterministic.

@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 reviewed this change.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 2 advisory finding(s) below merit a look before merge.

Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 not verified).

Formal verification. PR-changed functions: 0/1 verified (0 proven, 0 may-equivalent, 0 distinguished) · 1 not verified (1 unsupported).

Not verified on this run: detect (unsupported).


Graphify review — findings

Adds pruning of linked git worktrees in detect when their main checkout also sits under the scan root, so one repo is no longer indexed twice. The pruned directory is reported in pruned_noise_dirs. _linked_worktree_main_root identifies a worktree from its .git file and commondir back-pointer without spawning git, and returns None for submodules and dangling pointers, so those stay indexed. The prune decision depends only on the common dir being named .git and the main root resolving under the scan root, not on whether that main checkout is itself scanned.

Worth a look

  • Worktrees of a bare repo stored as .git are pruned even though no main working tree exists — graphify/detect.py:1528 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
  • Linked worktree pruned even when its main checkout is itself excluded from the scan — graphify/detect.py:2261 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3943 functions depend on the 668 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: detect() — 147 callers, 19 callees

Verification — 3943 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1350 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

65 of 465 test file(s) selected (14%) via static blast radius.

  • tests/test_astro_extraction.py — impact
  • tests/test_atomic_writes.py — impact
  • tests/test_backend_opt_in.py — impact
  • tests/test_binary_media_words.py — impact
  • tests/test_build.py — impact
  • tests/test_cargo_introspect.py — impact
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_claude_cli_backend.py — impact
  • tests/test_detect.py — impact, changed-test
  • tests/test_docx_to_markdown.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_evidence_binding.py — impact
  • tests/test_ext_tagged_id_clash.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cli.py — impact
  • tests/test_file_slice.py — impact
  • tests/test_file_truncation_warning.py — impact
  • tests/test_ignore_file_encoding.py — impact
  • tests/test_image_vision.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_incremental_mtime_collision.py — impact
  • tests/test_indirect_call_block_scoped_shadow.py — impact
  • tests/test_js_exported_scalar_bindings.py — impact
  • tests/test_js_member_collisions.py — impact
  • tests/test_language_override_imports.py — impact
  • tests/test_language_override_lifecycle.py — impact
  • tests/test_language_overrides.py — impact
  • tests/test_languages.py — impact
  • tests/test_llm_backends.py — impact
  • tests/test_long_path_hashing.py — impact
  • tests/test_manifest_ingest.py — impact
  • tests/test_markdown_code_spans.py — impact
  • tests/test_mcp_ingest.py — impact
  • tests/test_msbuild_dir_evidence.py — impact
  • tests/test_multilang.py — impact
  • tests/test_non_regular_files.py — impact
  • tests/test_office_incremental.py — impact
  • tests/test_office_limits.py — impact
  • tests/test_ollama.py — impact
  • tests/test_out_dir_evidence.py — impact
  • tests/test_oversized_document_slicing.py — impact
  • tests/test_package_json_subpath_imports.py — impact
  • tests/test_pdf_extra_warning.py — impact
  • tests/test_pdf_slicing.py — impact
  • tests/test_pdf_token_estimate.py — impact
  • tests/test_phantom_external_import.py — impact
  • tests/test_pipeline.py — impact
  • … and 15 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

Could not verify: Could not verify detect.

The verifier did not have enough to check detect, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: nondeterministic output (both versions disagreed with themselves on the witness) — not a behaviour change; a function whose output is not a function of its inputs (random / time / uuid / os.urandom, or state carried across calls) cannot be soundly distinguished by execution, so this tier abstains for the whole function

· 1 more finding(s) on lines outside this diff (see the check run).

@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.97, now on PyPI (pip install graphifyy==0.9.97). A linked worktree whose main checkout is in the scan root is pruned, so it is not double-indexed. Thanks for the fix, @barlowa124.

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.

detect: a linked git worktree that sits next to its main checkout is indexed as a second copy when the scan root is not a git repo (follow-up to #1810)

2 participants