Skip to content

bug: watch mode crashes with FOREIGN KEY constraint failed in rebuildFile #1176

@carlos-alm

Description

@carlos-alm

Found during dogfooding v3.10.1-dev.80

Severity: Medium (crashes the watch process; not consistently reproducible)
Command: codegraph watch <dir>

Reproduction

Not 100% reproducible, but the crash was observed after this sequence:

codegraph build . --no-incremental --engine native
codegraph embed -m minilm                    # 7969 embeddings
echo "// emb-test" >> src/cli.ts
codegraph build .                             # incremental → 19139/40857
git checkout src/cli.ts                       # restore
# Note: a separate incremental rebuild after restore was NOT done; watch starts on possibly-stale state
codegraph watch .
# In another terminal:
echo "// w-test" >> src/cli.ts

On the very first watcher tick after the edit:

SqliteError: FOREIGN KEY constraint failed
    at rebuildFile (.../dist/domain/graph/builder/incremental.js:354:23)
    at processPendingFiles (.../dist/domain/graph/watcher.js:46:31)
    at Timeout._onTimeout (.../dist/domain/graph/watcher.js:154:15)
    at listOnTimeout (node:internal/timers:608:17)
    at process.processTimers (node:internal/timers:543:7) {
  code: 'SQLITE_CONSTRAINT_FOREIGNKEY'
}

Node.js v24.10.0

The watcher process then exits (does not recover).

Expected behavior

A FK constraint failure on a single file rebuild should be caught, logged, and the watcher should continue processing other changes. It should never bring down the watcher.

Root cause (hypothesis)

Likely related to #1174 (incremental rebuild drops imports edges). When the DB state is internally inconsistent (e.g., a node referenced by an edge in another table has been deleted but the referencing rows weren't), watch's rebuildFile triggers an FK violation on the next insert that touches the dangling reference.

The native engine likely has PRAGMA foreign_keys = ON and either:

  1. The Stage 6b purge order is wrong (deleting a parent before its dependent rows), or
  2. An edge is being inserted with a from_id/to_id pointing at a node that no longer exists.

Suggested fix

  1. Wrap rebuildFile's DB writes in a try/catch that logs and skips the file rather than crashing.
  2. Investigate the FK constraint at incremental.js:354 (transpiled from src/domain/graph/builder/incremental.ts) — what specific FK is being violated?
  3. Likely fixed by addressing #1174.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdogfoodFound during dogfooding

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions