Skip to content

fix(index): never write through a link inside the index directory - #362

Merged
sunerpy merged 8 commits into
mainfrom
fix/index-no-follow-writes
Dec 1, 2026
Merged

sunerpy merged 8 commits into
mainfrom
fix/index-no-follow-writes

Conversation

@sunerpy

@sunerpy sunerpy commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Summary

  • Files CodeGraph writes by name inside the index directory (.codegraph/) are no longer written through whatever already holds that name. A new codegraph-core module, managed_file, creates such a file fresh (an entry already at the name, such as a link or a FIFO, is replaced rather than followed), appends only to a regular file, and accepts only a real directory where a directory is expected.
  • The full index's edge and reference spill files are created fresh and read back through the same handles instead of being reopened by path.
  • daemon.log is appended to only when it is a regular file; anything else there sends the daemon's output to the null sink with a warning. The temporary records renamed over daemon.pid (.tmp, .bound, .reassert) are created fresh.
  • A debug log goes into .codegraph/diagnostics/ only when that is a real directory, as a new file; otherwise it stays beside the index directory with a warning.
  • Viewer trails: a project path that does not exist yet is accepted only when its nearest existing ancestor resolves inside the project, so the trails directory is never created outside it. Saved trails are listed and read only as regular files, and a trail's temp file is created fresh before the rename.

The stored graph does not change: no extraction-version move. docs/architecture.md, docs/cli.md, docs/troubleshooting.md and docs/ui.md describe the behavior.

This follows #360 (merged as 97fa800) and uses its regular-file readers.

Tests

Each new test was run on the base first and failed there for the reason it targets; the two guards noted below pass on the base by design.

  • crates/codegraph-cli/tests/index_root_links.rs (Unix): each case plants one name under .codegraph/ that the full index or a daemon start writes by name (the edge and reference spill files, daemon.log, the diagnostics directory) as a link to an existing file outside the project, a dangling link, a link to /dev/zero (Linux), or a spill as a FIFO. It then runs init --debug, status, sync after an edit, search, a daemon-mode MCP session and uninit --force in child processes under a 20 s deadline and a 2 GiB address-space cap. Every command finishes, stays under 200 MiB and exits 0, nothing outside the project changes and no dangling target is created. A combined case plants all four names at once. Guards: daemon.log linked to /dev/zero, and a .codegraph that is itself a link out of the project (init --debug and sync --debug are refused and leave that directory as it was).
  • codegraph-daemon unit tests for the three temp record names, each planted as a link to an outside file or as a dangling link.
  • codegraph-ui unit tests for resolve_project_file and tests/trails.rs cases for a linked trails directory, a link at the temp file name, and a linked trail file.
  • codegraph-core unit tests for managed_file.

Verification

  • cargo fmt --all --check, cargo clippy --workspace --all-targets --locked -- -D warnings, and the same clippy cross-compiled for x86_64-pc-windows-msvc.
  • Full tests of the changed crates: core, daemon (lib 180 and every integration test), ui (lib 138, tests/trails.rs 19), codegraph-rs 3201 passed.
  • Release build; scripts/regen-goldens.sh --check: 32 corpora identical.
  • These ran before the branch was rebased onto 97fa800, which adds only a Cargo.lock dependency bump and a one-line docs fix beneath it; the pre-push gate (make pre-ci) ran again on the rebased head.

Add unit tests (Unix only) for the three temp records the daemon writes
next to daemon.pid before renaming one into place: the claim's
daemon.pid.<pid>.tmp, the socket rewrite's daemon.pid.<pid>.bound and the
republish's daemon.pid.<pid>.reassert. Each name is planted as a link to a
file outside the index directory, or as a dangling link to a path there.
After the write the outside file is unchanged, the dangling target does not
exist, nothing new appears outside, and daemon.pid is a regular record
naming this process.

On the current tree all six cases fail: the record is written into the
outside file, or the dangling target is created.
- resolve_project_file refuses a path that does not exist yet when its
  nearest existing ancestor is a link out of the root, or a dangling link;
  a missing path below real directories still passes.
- With .codegraph/ui linked to a directory outside the project that has
  no trails yet, a save creates nothing out there.
- A link at the save's temp file name, to an outside file or a dangling
  path, is neither written through nor renamed into place: the outside
  file is unchanged, nothing is created, and the saved trail is a regular
  file.
- A <id>.json in the trails directory that links to an outside file that
  parses as a trail is not listed and not read.

On the current tree every case fails: the missing paths pass the lexical
check, the save creates trails/ outside the project, the temp file write
goes through the link, and the linked trail is listed with its note.
Add crates/codegraph-cli/tests/index_root_links.rs (Unix only). Each case
plants one name under .codegraph/ that the full index or a daemon start
writes by name (the edge and reference spill files, daemon.log, the
diagnostics directory) as a link to an existing file outside the project,
a dangling link, or (Linux) a link to /dev/zero, or a spill as a FIFO. It
then runs init --debug, status, sync after an edit, search, a daemon-mode
MCP session and uninit --force in child processes under a 20 s deadline
and a 2 GiB address-space cap. Every command finishes, stays under 200 MiB
and exits 0; afterwards nothing outside the project changed, no new entry
appeared there, and no dangling target was created. A combined case links
all four names, .gitignore and writer.pid at once.

A guard covers a .codegraph that is itself a link out of the project:
init --debug and sync --debug are refused and leave that directory as it
was. It passes on the current tree.

On the current tree the other cases fail, except daemon.log linked to
/dev/zero, which passes: the spills are written into the outside file or
create the dangling target, a FIFO spill blocks init past the deadline, a
spill linked to /dev/zero is read without bound, daemon.log is appended to
or created outside, the debug log is created in the outside directory, and
init --debug fails when diagnostics is a dangling link or a link to
/dev/zero.
The full index's two spill files are created fresh, replacing whatever held
their names, and read back through the same handles instead of being
reopened by path. A debug log in the index directory goes only into a real
diagnostics directory as a new file; otherwise it stays beside the index
directory with a warning. A log moved there keeps its open handle on Unix
and is reopened as a regular file only on Windows.
…wing a link

The temp records renamed over daemon.pid (.tmp, .bound, .reassert) are
created fresh, so whatever already holds their predictable names is
replaced rather than written through. daemon.log is appended to only as a
regular file; anything else there sends the daemon's output to the null
sink with a warning.
… to one

A missing project path is accepted only when the nearest entry that exists
above it resolves inside the project, and an entry that does not resolve,
such as a dangling link, is refused, so the trails directory is never
created outside the project. Saved trails are listed and read only as
regular files themselves, and a trail's temp file is created fresh before
the rename.
…ollowing a link

Document codegraph-core's managed_file helpers and their callers, and how
the index spill files, daemon.log, the default debug log directory, the
temporary daemon records and saved trails behave when an entry in the index
directory is a link, a FIFO or a directory. Describe the viewer's checks on
the trail directory and on the trail files it lists.
@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.83986% with 29 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/codegraph-core/src/managed_file.rs 94.73% 12 Missing ⚠️
crates/codegraph-cli/src/diagnostics.rs 94.73% 8 Missing ⚠️
crates/codegraph-core/src/source_file.rs 73.33% 4 Missing ⚠️
crates/codegraph-daemon/src/lock.rs 96.29% 4 Missing ⚠️
crates/codegraph-ui/src/security.rs 96.87% 1 Missing ⚠️

❌ Your patch check has failed because the patch coverage (94.83%) is below the target coverage (95.00%). You can increase the patch coverage or adjust the target coverage.

Impacted file tree graph

@@           Coverage Diff            @@
##             main     #362    +/-   ##
========================================
  Coverage   96.41%   96.42%            
========================================
  Files         369      370     +1     
  Lines      256165   256629   +464     
========================================
+ Hits       246989   247445   +456     
- Misses       9176     9184     +8     
Files with missing lines Coverage Δ
crates/codegraph-cli/src/main.rs 92.07% <100.00%> (+0.01%) ⬆️
crates/codegraph-daemon/src/spawn.rs 95.65% <100.00%> (+0.02%) ⬆️
crates/codegraph-ui/src/api/trails.rs 89.30% <100.00%> (+0.86%) ⬆️
crates/codegraph-ui/src/security.rs 93.93% <96.87%> (+0.29%) ⬆️
crates/codegraph-core/src/source_file.rs 95.51% <73.33%> (+0.19%) ⬆️
crates/codegraph-daemon/src/lock.rs 96.35% <96.29%> (-0.02%) ⬇️
crates/codegraph-cli/src/diagnostics.rs 93.19% <94.73%> (+0.99%) ⬆️
crates/codegraph-core/src/managed_file.rs 94.73% <94.73%> (ø)

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sunerpy
sunerpy merged commit 82c74a2 into main Dec 1, 2026
12 checks passed
@sunerpy
sunerpy deleted the fix/index-no-follow-writes branch December 1, 2026 10:27
@github-actions github-actions Bot mentioned this pull request Dec 1, 2026
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.

1 participant