fix(db): stop findDbPath walk at cwd when no git ceiling - #1193
Conversation
When findRepoRoot() returns null, the walk used to climb to /, which could silently attach to a stale .codegraph/ in an unrelated parent — e.g. /private/tmp/.codegraph/ leaking into every /tmp/foo/ run, or $HOME/.codegraph/ leaking into every scratch dir under $HOME (the "Phase 0 footgun" from the v3.10.1-dev.80 dogfood report, §10.6). Outside a git repo, cwd is now the first (and only) directory we'll check. Users running codegraph in non-git project subdirectories must either cd to the project root, git init, or pass -d/--db.
|
Claude finished @carlos-alm's task —— View job PR Review: fix(db): stop findDbPath walk at cwd when no git ceilingRoot Cause AnalysisProblem Being Solved: This PR fixes a critical security footgun where Why This Approach: The fix adds an early exit condition in the directory traversal loop when Risk Assessment: ✅ LOW RISK. This is a targeted security fix with comprehensive test coverage. The behavioral change only affects non-git scenarios and makes the tool more predictable. Phase 2: Configuration Change Detection✅ PASSED: No configuration, linting, or build changes detected. Only functional code and corresponding tests modified. Phase 3: Test Integrity Review✅ EXCELLENT: New regression test Phase 4: Code Quality AssessmentImplementation Quality: ⭐⭐⭐⭐☆
Minor observation: The fix location at src/db/connection.ts:295-302 is architecturally correct, sitting between the git ceiling check and the parent directory traversal loop. Backlog Compliance
Tier: 1 (zero-dep ✓ + foundation-aligned ✓) Trade-off AssessmentThe noted trade-off is acceptable and well-communicated: Users running codegraph from subdirectories of non-git "projects" must now
Critical ConcernsNone identified. This is a well-executed security fix with appropriate testing and clear documentation. Final Recommendation
Strong approval recommended — this fixes a genuine security/reliability issue that could cause significant confusion for AI coding assistants. |
Greptile SummaryThis PR fixes a path-discovery footgun in
Confidence Score: 5/5Safe to merge — the change is a single-line guard with no impact on the git-rooted path, and the regression test directly covers the fixed scenario. The fix is a minimal, well-placed guard that only affects the non-git code path. The git-ceiling path is entirely unchanged. The new regression test accurately reproduces the stale-parent attachment scenario and passes on all platforms because both sides of the path comparison derive from the same unresolved os.tmpdir() value. No pre-existing tests are affected. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[findDbPath called] --> B{customPath provided?}
B -- Yes --> C[return path.resolve customPath]
B -- No --> D[findRepoRoot → ceiling]
D --> E[dir = realpathSync cwd]
E --> F{candidate exists?\ndir/.codegraph/graph.db}
F -- Yes --> G[return candidate]
F -- No --> H{ceiling set AND\ndir == ceiling?}
H -- Yes --> I[break: git ceiling reached]
H -- No --> J{ceiling is null?\nno git repo}
J -- Yes --> K[break: stop at cwd NEW BEHAVIOR]
J -- No --> L[dir = parent dir]
L --> M{parent == dir?\nat filesystem root}
M -- Yes --> N[break: hit root]
M -- No --> F
I --> O[base = ceiling OR cwd]
K --> O
N --> O
O --> P[return base/.codegraph/graph.db]
Reviews (3): Last reviewed commit: "Merge branch 'main' into fix/find-db-pat..." | Re-trigger Greptile |
Codegraph Impact Analysis1 functions changed → 99 callers affected across 67 files
|
Summary
findDbPathno longer walks past cwd when not in a git repo, preventing stale.codegraph/in unrelated parents (e.g./private/tmp/,$HOME/) from being silently picked up./tmp/dogfood-…attached to a stale/private/tmp/.codegraph/graph.dbleft over from a previous unrelated session.Trade-off
Users running codegraph from a subdirectory of a non-git "project" used to auto-discover a parent
.codegraph/. They now need tocdto the project root,git init, or pass-d/--db. This aligns with codegraph's git-centric design and removes the silent-attachment footgun.Test plan
npx vitest run tests/unit/db.test.ts— 26 passed (including new regression test "does not pick up stale parent .codegraph/ when no git repo exists" and all existing git-ceiling tests)npx vitest run tests/unit/db.test.ts tests/unit/snapshot.test.ts— 55 passednpx vitest run tests/integration/structure.test.ts tests/integration/cli.test.ts— 45 passednpm run build— succeeds/tmp/cg-smoke-parent/inner/with stale/tmp/cg-smoke-parent/.codegraph/graph.db→ returns/private/tmp/cg-smoke-parent/inner/.codegraph/graph.db(not the parent).codegraph/graph.db(unchanged behavior)