Skip to content

Fix duplicate triples from re-asserted facts in the V6 incremental indexer - #1508

Merged
zonotope merged 7 commits into
mainfrom
fix/duplicate-triples
Jul 18, 2026
Merged

zonotope merged 7 commits into
mainfrom
fix/duplicate-triples

Conversation

@zonotope

Copy link
Copy Markdown
Contributor

Re-inserting triples that already exist in the database left the indexed view with duplicate rows: the same subject-property-object appearing many times (observed ~16× in the reporting ledger), along with spurious assert/retract churn in transaction history views. Live queries against in-memory novelty were correct; the duplicates appeared only after an incremental index build and compounded with each subsequent one. A full rebuild silently healed them, which made the bug look intermittent.

Root cause: commit files are event logs of user assertions, not state deltas — re-asserting a present fact is a legitimate event that records a new transaction, and every layer that materializes state from those events must apply RDF set semantics itself. Three of the four materializers did (in-memory novelty via NoveltyFactState, the query overlay via resolve_overlay_ops, and the full rebuild via KWayMerge::next_deduped), but the V6 incremental pipeline replayed raw commit events with no lifecycle resolution: resolve_incremental_commits_v6 sorted records without collapsing them, and merge_novelty's two-pointer walk assumes at most one op per fact identity, so each duplicate assertion event materialized as an additional physical row in the FLI3 leaves.

Changes

Set-semantics dedup in the incremental resolve (the core fix)

dedup_fact_lifecycles runs after the (g_id, SPOT) sort in resolve_incremental_commits_v6 and resolves each fact's in-window event sequence to a single final op, mirroring the in-memory novelty rules exactly so the updated index materializes the same state a live query resolves: a re-assert of a currently-asserted fact is a no-op (the earlier assert and its t survive, matching NoveltyFactState's skip rule), any other op becomes the identity's winner, and same-t ties resolve retract-wins (matching resolve_overlay_ops).

Fact identity is the resolved V2 identity (o_type, o_i) — the identity merge_novelty, replay, and the query overlay all compare by — rather than raw v1 fields. This matters because OTypeRegistry::resolve collapses the datatype dict id for 1:1 object kinds (a boolean is xsd:boolean whatever datatype IRI the commit carried) and consults lang_id only for rdf:langString, so datatype-IRI variants and stray language tags on one fact dedup together while genuine language tags and list positions stay distinct. The dedup compacts in place behind a write cursor: a window with no duplicate facts copies nothing, and multi-record groups resolve through a reused scratch buffer in O(k log k).

Correct stats deltas via base presence

The signed stats hooks (per-property/datatype/graph flake counts and rdf:type class counts) previously derived ±1 deltas from each record's op against base-seeded counts, which double-counts re-asserts and, once lifecycles collapse, applies unbalanced deltas for retract-then-reassert and assert-then-retract windows. The Phase 2 SPOT merge now reports which novelty ops matched an existing base row (MergeOutput::matched, threaded through update_leaf → branch update → Phase2TaskOutput, collected for the SPOT order only), and the Phase 3b feed uses a new IdStatsHook::on_record_with_base_presence: +1 only for an assert of a base-absent fact, −1 only for a retract of a base-present one, 0 otherwise. Monotone accumulators (HLL sketches, touched-property sets, last_modified_t) still observe every record. The from-scratch event-replay feeds (rebuild/import) keep the unchanged legacy on_record. This also fixes a pre-existing bug: every re-assert of an indexed fact used to permanently inflate its property count.

No history entries for non-transitions

The history sidecar is a transition log: time-travel replay undoes a retract entry by marking the fact present before it, so a retract entry with no paired assert anywhere makes replay fabricate the fact's presence at every t before the retraction. merge_novelty no longer writes a history entry for a retract of a fact the leaf never materialized (the Greater arm and the post-rows drain loop), and the rebuild path skips retract-winner lifecycles whose identity has no assert anywhere in the commit log. This fixes both the ghost introduced by lifecycle collapsing (a fact asserted and retracted within one window deduped to a bare retract) and a pre-existing ghost when a user retracts a fact that never existed.

Known limitations and follow-ups

  • A fact asserted and retracted within a single indexing window is invisible to index-served time travel during its brief lived interval (that interval was never materialized in any published leaf; before this branch it was "visible" only via the buggy ghost row). The live novelty view retains those intermediate ops until the window is indexed. Restoring full in-window fidelity requires a transition-log rework of the history sidecars; the full analysis, design, risks (including mixed-vintage sidecar compatibility), and testing strategy are captured in history-transition-log-plan.md.
  • The full rebuild path resolves a re-asserted fact to the highest-t assert while novelty and the incremental path keep the first assert's t, so the two build paths can materialize different row t for the same event log. Pre-existing, out of scope here; folded into the plan file as a blocking design decision for the rework.
  • Already-published leaves keep their duplicate rows, corrupted stats, and stale bare-retract history entries until affected ledgers run a full reindex; this branch stops new damage.

Fixes #1412

@zonotope
zonotope requested review from aaj3f and bplatz July 15, 2026 16:38

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

@zonotope
zonotope merged commit 7581f0a into main Jul 18, 2026
13 checks passed
@zonotope
zonotope deleted the fix/duplicate-triples branch July 18, 2026 13:43
q-qp-p pushed a commit to q-qp-p/fluree-db that referenced this pull request Aug 4, 2026
Delivers the fluree#1508 duplicate-triples / assert-retract-resurrection fix
(fluree-db-indexer dedup_fact_lifecycles) to the virtual-dataset perf
lineage — audit finding F-AUD-2 (audit-2026-07/00-MASTER-AUDIT.md).
Merge verified conflict-free pre-flight via git merge-tree (2 files
auto-merged in disjoint regions; decision record
audit-2026-07/decisions/DEC-001-pr-bundling.md, Adjudication B).
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.

[BUG] Re-inserting existing triples yields duplicate triples + assert/retract churn; upsert of existing triples registers no new transaction

2 participants