Skip to content

Delta scan (--incremental) is broken: silent full-scan fallback masks partial persistence + signal drift #3220

Closed

Description

@squid-protocol

Summary

galaxyscope --incremental (Delta Scan) is broken on the current file_data schema and has been a silent no-op: every delta scan catches an internal error and falls back to a full scan. Fixing that first-layer crash then exposes a second, deeper correctness bug — the incremental path does not reproduce a full scan's output. There is no parity test guarding --incremental, which is how both layers went unnoticed.

Found while investigating why a 1,016-scan temporal-crucible run of curl (via --db-only) was slow — the harness wasn't passing --incremental, and when we tried it, it produced wrong results.

Layer 1 — Rehydration crashes, silently falls back to full (safe but slow)

StateRehydrator.load_state() (gitgalaxy/core/state_rehydrator.py) reconstructs each file's cache entry with:

"dependency_network": {
    "total_downstream": f["total_downstream"],
    "total_upstream":   f["total_upstream"],
},

Those columns do not exist in file_data. They are transitive reach counts computed at scan time (security_auditor.py reach_counts) and written only to the JSON audit — never persisted to the DB (the DB has internal_dependency_links, normalized_blast_radius, etc.). So f["total_downstream"] raises IndexError: No item with that key, which the broad except (sqlite3.Error, ValueError, TypeError, KeyError, IndexError) swallows → load_state returns None → galaxyscope.py logs ⚠️ Rehydration failed. Falling back to full baseline scan.

Measured (curl, one commit pair, baseline DB holds the parent):

FULL  child scan: 6.27s
DELTA child scan: 6.22s   <- no speedup: it silently ran a full scan
log: ⚠️ Rehydration failed. Falling back to full baseline scan.

Direct load_state() call: ⚠️ Database corruption or read error: No item with that key.

Net effect today: --incremental is safe but pointless — correct output, zero speedup, for every user on this schema.

Layer 2 — With rehydration fixed, delta output DOES NOT match a full scan

Applying the minimal schema-drift fix (patch below) makes rehydration succeed — and the shipped parity audit (temporal-crucible/tools/parity_check.py) then shows delta is not equivalent to full:

CURL-CVE-2026-82209  95c1e8915dce  files inc=368 full=1134  DIVERGENT
   only-full: <766 files missing from the delta child commit>
   differs:   include/curl/easy.h -> ['risk_api_exposure', 'class_count']
   differs:   include/curl/header.h -> ['risk_api_exposure', 'risk_dead_code', 'class_count']
CURL-CVE-2026-82208  ed0338befd1d  files inc=0    full=1132  DIVERGENT
parity: 0/1 pairs identical

Two distinct defects:

  1. Partial persistence — the child commit ends up with only 368 of 1,134 files (one pair: 0). execute_incremental_scan does not re-emit the rehydrated (unchanged) files under the new commit_hash; a full scan writes all of them.
  2. Signal drift — files that are present differ on graph/exposure-derived signals (risk_api_exposure, risk_dead_code, class_count). Root cause: rehydrated files come back with "raw_imports": set() (empty) and "hit_vector": [], so the ripple recompute (_resolve_dependency_graph / build_dependency_graph / _calculate_risk_exposures) sees a graph missing all edges from unchanged files.

Proposed fix direction

  • Persist-and-re-emit: execute_incremental_scan must write the full surviving file set under the new commit, not only the surgically re-scanned files.
  • Rehydrate imports: either persist raw_imports (or resolved edges) to file_data and restore them in load_state, or rebuild the edge set from the already-persisted internal_dependency_links/edge tables, so the recomputed graph matches a full scan.
  • Guard with a parity test: promote temporal-crucible/tools/parity_check.py into the engine's test suite (--incremental child vs independent full child must be row-identical). This is the missing check that let both layers ship.

⚠️ Do NOT merge the Layer-1 patch alone

The patch below is the diagnostic that exposes Layer 2 — not a shippable fix. Merging it by itself makes things worse: it flips --incremental from silently-slow-but-correct (full fallback) to silently-wrong (partial, drifted delta). Ship it only together with the Layer-2 fix + parity test.

diff --git a/gitgalaxy/core/state_rehydrator.py b/gitgalaxy/core/state_rehydrator.py
index bef10cb5..9b78046f 100644
--- a/gitgalaxy/core/state_rehydrator.py
+++ b/gitgalaxy/core/state_rehydrator.py
@@ -146,9 +146,18 @@ class StateRehydrator:
                         "ai_threat_score": f["ai_threat_score"],
                         "author_distribution": safe_silo_risk,
                     },
+                    # Schema Drift Protection (same pattern as silo_risk/doc_loc above).
+                    # total_upstream/total_downstream are TRANSITIVE reach counts computed
+                    # at scan time (security_auditor.reach_counts) and written only to the
+                    # JSON audit -- they were never persisted as file_data columns. An
+                    # unguarded f["total_downstream"] therefore raises IndexError on every
+                    # real DB, which the broad except below swallowed, silently dropping
+                    # EVERY delta scan back to a full scan. The incremental ripple
+                    # (execute_incremental_scan step 5) recomputes these for all files, so a
+                    # rehydrated seed of 0 is both sufficient and correct.
                     "dependency_network": {
-                        "total_downstream": f["total_downstream"],
-                        "total_upstream": f["total_upstream"],
+                        "total_downstream": f["total_downstream"] if "total_downstream" in row_keys else 0,
+                        "total_upstream": f["total_upstream"] if "total_upstream" in row_keys else 0,
                     },
                 }

Activity

  1. squid-protocol commented on Sep 20, 2026

    @squid-protocol
    OwnerAuthor

    Fixed in #3227 (green, pending merge — this issue is wired to close on merge).

    Root cause confirmed as reported: rehydration read nonexistent total_downstream/
    total_upstream columns → IndexError swallowed → silent full-scan fallback; and even
    when engaged, rehydration was lossy (~10 of ~250 fields), so it couldn't reproduce a
    full scan (partial persistence + signal drift).

    The fix makes the DB a replayable snapshot: full risk_vector/hit_vector/equations
    reconstructed by inverting the recorder's own schema maps; functions/classes
    rehydrated from their tables; and the transient score-layer inputs the DB never kept now
    persisted + restored (mitigation_telemetry, doc_umbrella, raw_imports,
    func.impact). A full-scan@X vs incremental→X oracle
    (temporal-crucible/tools/parity_check.py) shows full file coverage + bit-exact on 12
    of 13 risk vectors
    , and the misleading unit test (mock DB inventing the missing
    columns) is corrected.

    Remaining tail (not blocking; intentionally deferred): risk_verification still
    differs on a few files/repo — (1) synthetic top-level slices are consumed by the
    verification math but persisting them regresses units_documented, and (2) the repo-wide
    umbrella_bonus is computed over soon-excluded files' LOC a delta can't reconstruct
    (excluded_artifacts has no coding_loc). Both fixes ripple more than the 4th-decimal
    gain justifies. If exact risk_verification parity is ever required, that's the
    follow-up scope.

  2. squid-protocol commented on Sep 20, 2026

    @squid-protocol
    OwnerAuthor

    Fix merged to main in #3227 (2026-09-20). Auto-close didn't fire because the Closes #3220 link was added to the PR body after it merged — closing manually. Resolution + the deferred risk_verification tail are documented in the comment above.

  3. added
    scope:infra
    on Oct 16, 2026
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

    scope:infra

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions