Skip to content

fix(native): load_file_hashes collapses read failures into "no prior build state" #2418

Closed

Description

@carlos-alm

Deferred from PR #2414 review.

Original reviewer comment: #2414 (comment) (Greptile, P1 — "Native fallback still wipes state")

The bug

load_file_hashes in crates/codegraph-core/src/domain/graph/builder/stages/detect_changes.rs:88 treats a failed row query as absent prior state:

let mut stmt = match conn.prepare("SELECT file, hash, mtime, size FROM file_hashes") {
    Ok(s) => s,
    Err(_) => return None,   // corruption, lock, I/O, schema drift → "no prior build"
};

None means a from-scratch build, and a full build DELETEs every graph table — nodes, edges, file_hashes, and embeddings among them. So a transient SQLITE_BUSY (a concurrent build, or the MCP server holding the DB) or a schema fault can silently discard a healthy graph and its expensive embeddings instead of failing and letting the operator retry.

This is the same defect PR #2414 fixed on the JS side, where loadFileHashes now reads table existence from sqlite_master and throws DbError (DB_STATE_UNREADABLE) when the table exists but cannot be read.

Why this is still open

PR #2414 closed the reachable path: the native fast-skip pre-flight in src/domain/graph/builder/pipeline.ts used to swallow that error and fall through to the Rust orchestrator, and now re-throws it. So an ordinary incremental native build is protected.

The Rust loader itself is still wrong, which matters because:

  • it is the source of truth whenever the pre-flight is bypassed;
  • it violates the dual-engine rule in CLAUDE.md — the two engines must produce identical results, and the less-accurate one has the bug;
  • direct consumers of the Rust API get no protection at all.

The fix

Mirror the TS semantics in load_file_hashes. It already probes sqlite_master for existence, so the three cases are cleanly separable:

  • table missing → no prior state (unchanged)
  • table present, zero rows → no prior state (unchanged)
  • table present, read fails → propagate the error instead of return None

That means changing the signature from Option<HashMap<..>> to Result<Option<HashMap<..>>, _> and handling it at the caller (detect_changes.rs:1246). The .ok()? and filter_map(|r| r.ok()) on the query_map below have the same swallowing problem and should be reviewed in the same pass.

Note

Correction (was wrong when filed): the original note said this was deferred because "this environment has no Rust toolchain (cargo is unavailable)". That is false. cargo (1.97.1) is installed at ~/.cargo/bin/cargo; it is simply not on PATH, so a bare which cargo returns nothing and I wrongly read that as absent. The Rust change is verifiable locally and this issue is actionable now, not blocked.

To build the addon from source:

export PATH="$HOME/.cargo/bin:$PATH"
npm install --no-save @napi-rs/cli@3
cd crates/codegraph-core
../../node_modules/.bin/napi build --release
cp codegraph-core.node codegraph-core.darwin-arm64.node

The final cp matters: napi emits the unsuffixed codegraph-core.node, while loadNative (src/infrastructure/native.ts) resolves the platform-suffixed name — without it the stale npm-installed addon is still what loads.

The remaining reason to keep this separate from #2414 stands on its own: crates/codegraph-core/.../detect_changes.rs is a different engine module, absent from that PR's diff, and #2414 was already review-approved and green when this was found. That is a scope/one PR = one concern argument, not a tooling limitation — so treat this as ordinary queued work rather than something gated on environment setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    follow-upDeferred work from PR reviews that needs tracking

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions