Skip to content

fix(indexer): a fact deleted before an incremental build stays deleted in a graph new to the index - #2118

Merged
aaj3f merged 17 commits into
mainfrom
fix/delete-survives-index-build
Oct 20, 2026
Merged

aaj3f merged 17 commits into
mainfrom
fix/delete-survives-index-build

Conversation

@aaj3f

@aaj3f aaj3f commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

An incremental index build can bring a deleted fact back. It happens when the fact was inserted and then deleted between two builds, in a graph the base index holds nothing for: a named graph first written after the last build, or the default graph of a ledger whose indexed data all sits in named graphs. Reads are right until the build (novelty has the retraction), and after it the fact is a live row again, in every order. A full rebuild never does this, and neither does a graph that already has a partition in the base index, which is why it mostly looks like it "only happens with named graphs". The fix is small and lives in the indexer: the fresh branch an incremental build writes for such a graph now resolves each fact's events the way a full rebuild does, through one shared LeafWriter method, so a retraction can't become a row there anymore and the window's history isn't dropped either.

Three neighbouring gaps in the same routing are closed here too, rather than left for an issue. A published branch with no leaves silently swallowed a window's writes. Bulk import wrote exactly such a branch, a SPOT with no leaves, for every graph it had no quads for, and an incremental build then kept that SPOT empty while writing the next window's facts to the other three orders, so on main default-graph writes after a named-graph-only import never reach SPOT (more on what that looks like below). And a base root missing some of a graph's sort orders would have had them rebuilt from the window alone.

What main does

Reproduced on main at f3c3b00ec, file-backed, background indexing off:

  1. Index a ledger whose only data is GRAPH <g2> { ex:a ex:q "base" }.
  2. INSERT DATA { ex:s ex:p "x" } (the default graph, which has no partition), then DELETE DATA { ex:s ex:p "x" }.
  3. ex:s ex:p ?o is empty. Build incrementally and publish, and it returns "x".

The same happens with GRAPH <g1> (a graph new since the build), with a JSON-LD delete instead of DELETE DATA, and for an integer, a language-tagged string and a ref, not just strings. A value replaced in the window (DELETE … INSERT … WHERE) keeps both the old and the new value after the build. And the window's history is gone: at the t between the insert and the delete, the fact reads as absent.

Base index holds Fact written to Full rebuild Incremental build on main
default graph default graph stays deleted stays deleted
default graph <g1> (new) stays deleted comes back
default graph <g2> (new) stays deleted comes back
<g2> only default graph stays deleted comes back
<g2> only <g1> (new) stays deleted comes back
<g2> only <g2> stays deleted stays deleted

Each row is run with SPARQL and JSON-LD deletes, so 24 cases in the test, 8 red on main.

It doesn't take an explicit build either. With the default settings the background indexer runs this same incremental build, and while it's busy with an earlier write, an insert and a delete in such a graph land in one window for its next build. In a probe with the production defaults and a large write in flight, the fact came back after the background build every time (10 of 10 runs for each graph). background_indexing_keeps_deleted_facts_deleted pins that shape deterministically: it holds the background indexer under its soft threshold until a commit after the insert and the delete crosses it, drives no build itself, and on main reads "x" after that build.

Fwiw, the original report also saw a full build resurrect the fact, and answers that changed when a ledger id was reused across tempdirs. As far as I can tell both come from the background indexer FlureeBuilder::file starts by default, which runs this same incremental build on its own and publishes alongside the explicit one. Running the report's own probe (ten cases, including MOVE <g1> TO <g2>) twice in each of four setups on main: with background indexing on, the full-build cases came back too; with without_indexing(), only the incremental ones did; and running the cases in reverse order, or reusing a ledger id, changed nothing in either setup. So I don't think there's a cache keyed by ledger id that outlives a Fluree instance here. With this branch, all 80 of those reads stay deleted. The tests in this PR turn background indexing off for that reason, except the one that's about the background indexer.

What main does after a named-graph-only import

Reproduced on main at 04e1a6d37. Bulk-import a TriG file whose quads are all in named graphs (here one quad in GRAPH <g>, plus an empty GRAPH <e> { } block), then write to the default graph and build:

  • The import's root gives the default graph one order, a SPOT with no leaves, and gives <e> the same. (An empty GRAPH block gets a graph id, and the import's SPOT build published a result for a graph even when it had no records for it, unlike the other three orders.)
  • The incremental build sends the default graph's SPOT novelty into that leafless branch, where it's dropped, and builds PSOT, POST and OPST fresh from the window. The root it publishes has all four orders, but only three hold the facts. Every later build does the same, so every default-graph fact written after the import is missing from SPOT once it's indexed. So is every fact later written to <e>.

What that looks like, once those facts are indexed:

  • Missing: anything that reads through SPOT. That covers a subject-bound pattern (ex:s ex:p ?o, ex:s ?p ?o), a fully bound ASK (false), ?s ?p ?o (empty, so DISTINCT ?s and GROUP BY ?s are too), CONSTRUCT { ex:s ?p ?o } WHERE { ex:s ?p ?o }, the JSON-LD subject crawl (only the @id comes back), time travel by subject, and a join whose plan looks the subject up (?s ex:p "x" . ?s ex:r ?o2 came back empty).
  • Still right: predicate- and object-bound patterns (?s ex:p ?o, ?s ex:p "x", ?s ?p ex:o), COUNT(*) (so it counts rows that ?s ?p ?o doesn't return), a FILTER(?s = ex:s) over a predicate scan, and the OPTIONAL and property path I tried.
  • Writes: an update whose WHERE binds the subject misses them too. DELETE { ex:s ex:p ?o } WHERE { ex:s ex:p ?o } deletes nothing, and a JSON-LD upsert of ex:s2 keeps the old value next to the new one.

An index in this state is repaired by a reindex, which builds every order from the commits. With this PR the next build repairs it as well. For the default graph that's whatever the window touches; for a named graph it's the next time that graph is written.

Cause

incremental_index groups the window's novelty by graph and gives each (graph, order) a task. A graph with no branch in the base root takes DefaultFresh or NamedFresh, which built the branch with build_fresh_default_graph_v3. That function pushed every net record as a row and never looked at its op ("Currently only assertions appear in fresh branches so ops are not consulted"), and build_fresh_named_graph_v3 passed it no ops at all. But lifecycle dedup leaves one net record per fact, and for a fact inserted then deleted in the window that record is the retraction, so the retraction was written as a live row. The events dedup superseded (here, the insert) were dropped with a "permanent gap" warning, which is the lost history.

The leaf merge (merge_novelty) and the full rebuild (index_build.rs) both get this right, because each resolves a fact's events with resolve_transitions before deciding what's a row. The fresh branch was the one writer that didn't.

The fix

  • LeafWriter::push_lifecycle takes one fact's events, resolves them from absent, sends the transitions to the sidecar and writes a row only for a final assert. The full rebuild's loop now calls it instead of its own copy of that logic, so the rebuild and the fresh branch can't drift apart.
  • build_fresh_branch_v3 replaces the two fresh builders (they were one function already). It walks the net records with their ops, gathers each fact's superseded events with the leaf merge's own gather_identity_events, and hands them to push_lifecycle. A superseded event left without its fact is an error rather than a warning, since it would mean lost history.
  • An empty fresh branch isn't published. Now that a retraction can resolve to nothing, a fresh branch can come out with no leaves, and slice_novelty_to_leaves routes nothing into a branch with no leaves, so publishing it would silently drop that graph's later writes. The task reports Unchanged instead, and the next build sees the graph as fresh again. The two fresh arms of execute_phase2_task are one arm now.
  • A branch with no leaves is built fresh too. It holds no base facts, the same as no branch, but on main the incremental build sent its novelty to the leaf merge, where slice_novelty_to_leaves found no leaf and returned no slices, so that window's writes for the graph were dropped without an error. The task now takes the fresh path whenever the base branch has no leaves: from the root's inline leaves for the default graph, and from the fetched manifest for a named graph, since that's the first point the branch's contents are known. And slice_novelty_to_leaves now refuses novelty for a branch with no leaves instead of returning nothing, so a caller that misses this can't drop writes quietly again (it's an error, and the build falls back to a full rebuild).
  • Neither a full rebuild nor a bulk import publishes a branch for a graph with nothing to hold. PersistingLeafWriter::finish, which both use, returns no result when it wrote no leaf, so neither writer can publish a branch with no leaves. On main a full rebuild published one for a graph whose log nets to no row and no history (a lone retraction of a fact never asserted, say), and the import's SPOT build published one for every graph it had no quads for. Readers already treat a missing branch and an empty one the same.
  • An order with no leaves counts as holding no facts, and an incremental build refuses a base root in which a graph has facts in some but not all four sort orders. I'd first written here that no writer produces such a root, which was wrong: import did, and the incremental build after it turned that into the SPOT-missing root above. A missing order, or one with no leaves, takes the fresh path and would be built from the window alone, without the graph's base facts. So the check now leaves out an order with no leaves before it asks for none-or-all. For the default graph that's from its inline leaves. For a named graph it's by CID, since a branch with no leaves is exactly build_branch_bytes(order, g_id, &[]) (the decoder reads one branch version, every writer builds branches through that function, and the CIDs match what main's import wrote), so it needs no fetch. That's a SHA-256 per branch, though, which measured about as much as decoding the root itself, so it's only done for a named graph with some orders but not all, the older import's shape. A root an older import wrote then has no facts for those graphs and builds incrementally. The default graph's SPOT-missing root is partial, so it's refused as an incremental abort, and the caller falls back to a full rebuild, which builds every order from the commits.
  • After Phase 2, a graph whose four tasks disagree on whether the base holds its facts is refused the same way. This is what refuses a named graph with all four orders and a SPOT with no leaves (what main writes after an empty GRAPH block is written to): the tasks have fetched its branches by then, so their leaves are known at no extra cost. Such a graph is repaired the next time it's written; until then it's carried as it is, as any graph the window doesn't touch is. The check costs nothing on a consistent root.

No API, wire format or index format change. An index already built with a resurrected row, or with facts missing from SPOT, is repaired by a full reindex, which walks the commits from genesis.

Commit What
8049948e4 Test: deleted facts in a graph new to the index stay deleted (red on main)
cf1ba3f9f A fresh incremental branch resolves each fact's in-window events
eaaaf0891 Test: a fresh branch writes no row for a retracted fact
910e08ddc An empty fresh branch is not published
1309f8af5 A branch with no leaves is built fresh and never handed novelty; a full rebuild publishes no branch for a graph with nothing to hold; a base root with a partial set of orders is refused
b53f4d17b, dab12594e Test: a background build alone keeps a deleted fact deleted (the first version passed on main, since with little data the indexer built after the insert; the second kept the indexer busy and checked that shape before it read)
9c946b45b Bulk import publishes no order with no leaves
2eaa71549 An order with no leaves counts as holding no facts; a graph whose tasks disagree on base facts is refused after Phase 2
b117c1de3 Test: graphs an import left empty take facts incrementally, and a SPOT missing facts is rebuilt; the background test is deterministic
227104ddd Merge main (57bf7a944)
ebbea4ff5 Test: the SPOT test's cases are a table clippy accepts
77cef24d9 The order-set check hashes a named graph's branches only when its orders are partial
326fe7c06 Merge af9f5bf94: test helpers that publish an index fail closed (see "Test harness")
ba1430dd8 Merge main (1d06ae2ef)
01a49799d Test: the rewritten roots publish through publish_built_index

Tests

it_delete_survives_index_build.rs (in grp_index):

  • deleted_facts_stay_deleted_after_index_build: the 24-case table above, with a string, an integer, a language-tagged string and a ref in each, read through the instance that built and through a fresh instance over the same storage.
  • replaced_value_stays_replaced_after_incremental_build: DELETE … INSERT … WHERE in the default graph and in a new named graph keeps only the new value.
  • history_of_new_graph_survives_incremental_build: after the build the fact is absent before its insert, present between the insert and the delete, and absent at the delete.
  • background_indexing_keeps_deleted_facts_deleted: the default graph and a new named graph, indexed only by the background indexer. It's held under its soft threshold (100 KB, catch-up sweeps off, since those ignore the threshold): the insert and the delete each assert from their own commit's indexing.needed that they started no build, and the next commit crosses the threshold, so the one build after the base's provably takes both.
  • graphs_an_import_left_empty_take_facts_incrementally: a TriG import with one quad in <g2> and an empty GRAPH <g3> { } block publishes no order with no leaves; a default-graph and a <g3> write then build with build_incremental_index_for_record (no fallback) and read by subject. The same runs against the import's root rewritten to what an older import wrote (a SPOT with no leaves for each).
  • facts_missing_from_spot_are_rebuilt: after the same import, a write and an incremental build, the root is rewritten to the shape main publishes (SPOT with no leaves, the window's facts in the other three orders), which reads by subject as empty and by predicate as "x". A second write's incremental build is then an IncrementalAbort, and the full rebuild brings both facts back by subject. It runs for the default graph, which the order-set check refuses, and for <g3>, which the Phase 2 check refuses.

Unit tests in the indexer:

  • fresh_branch_writes_no_row_for_a_retracted_fact (one row, one sidecar, and a stranded superseded event refused) and fresh_task_with_nothing_to_hold_publishes_no_branch.
  • leafless_branch_is_built_fresh: a default-graph order with no leaves and a stored named-graph branch with no leaves each take an asserted fact into a row. With main's routing and slicing it gets 0 rows, i.e. the write is dropped.
  • leafless_branch_refuses_novelty: slicing novelty into a branch with no leaves is an error; slicing nothing is fine.
  • build_publishes_no_branch_for_a_graph_with_nothing_to_hold: a rebuild over a lone retraction publishes no graph.
  • graph_without_records_publishes_no_branch: the import's build for a graph with no records publishes no branch in any order, SPOT included.
  • partial_order_root_is_refused: a default graph or a named graph with facts in two of the four orders is an incremental abort, and so is a default graph's SPOT with no leaves beside three orders with leaves; none, all four, a lone SPOT with no leaves, or four orders with no leaves are fine, and a named graph with all four orders is left to the Phase 2 check.
  • tasks_that_disagree_on_base_facts_are_refused, and leafless_branch_is_built_fresh now also checks that such a task reports no base facts.

Non-vacuity: with the indexer and binary-index sources reverted to main (f3c3b00ec) and the tests kept, all six integration tests fail: 8 of 24 cases come back, the replaced value keeps "old", the history read is empty at the insert's t, the background test reads "x" after its one build, the import publishes a SPOT with no leaves for the default graph and for <g3>, and the incremental build accepts the SPOT-missing root (after reading it as empty by subject and "x" by predicate, as above). At 1309f8af5 the two new tests fail as well: the import still publishes those orders, and the build refuses the root it makes. Each mechanism goes red under a mutation of its own, applied after the commit and restored after:

  • the fresh writer back to push_record (2 rows, not 1), the empty-branch check off, either half of the leafless routing off (both off with main's slicing gives the 0-row drop above), and the slicing refusal off, each red in its unit test;
  • PersistingLeafWriter::finish publishing a result with no leaves: build_publishes_no_branch_for_a_graph_with_nothing_to_hold, graph_without_records_publishes_no_branch and the import test fail;
  • the order-set check counting a default-graph order with no leaves as present: partial_order_root_is_refused and the import test's older-import root fail (an abort for graph 0); the same for a named branch with no leaves (an abort for <g3>'s graph);
  • the Phase 2 check off: tasks_that_disagree_on_base_facts_are_refused fails, and so does the SPOT test's <g3> case, where the incremental build accepts the root;
  • all three of those reader checks off: the SPOT test fails at its default-graph case the same way, and both unit tests fail.

Test harness

This branch also carries a test-support change, merged from af9f5bf94. publish_index declines, without an error, an index whose t isn't newer than the one already published, so a test that builds an index at a t the background indexer has already published goes on to read the indexer's build instead of its own. The helpers that publish a test's build (rebuild_and_publish_index, build_and_publish_index and build_incrementally_and_publish_index in support, it_merge's index_branch, and eight direct publishes in it_import_v3, it_indexing_fuel, it_storage_encrypted and it_merge) now go through one support::publish_built_index. It publishes allow-equal, the way Fluree::reindex does, then asserts that the nameservice holds the root that was just built, and panics otherwise. In three runs of the API suite on main with a detect-only version of the helper, 31 tests hit this: one deterministically (it_query_time_travel_bgp::repeat_full_rebuild_restores_retracted_history, whose second rebuild at the same t was never published, so it re-read the first), and 30 through the background indexer of a file-backed Fluree publishing first. None of their verdicts changes with the fix. The background indexer can still build between a test's other writes and reads in those files; moving them to without_indexing() isn't done here. The tests this PR adds publish through the same helper.

Performance

Release builds of one harness, file-backed:

  • Base ledger: 40,000 subjects × 5 properties in the default graph and 40,000 in <g2>.
  • Incremental windows: add 10,000 subjects to a graph and 10,000 to the default graph, and the delete rows then delete 6,000 of the first graph's subjects again. The graph is a new <g9> or the existing <g2>.
  • inc_many_named: 2,000 named graphs of 10 subjects, with a 100-subject window in the default graph.
  • Imports: a TriG file of 40,000 subjects × 5 all in <g2>, and the same plus 40,000 in the default graph.

Each figure is the median of 7 builds in one process, three rounds interleaving the two sides. The box was shared with other work (load 18–37, pageouts at most about 7 a second), so the comparison rests on instructions retired and allocations, both read for the whole process around each build, which don't move with load. Wall-clock is secondary.

This PR against main (57bf7a944), instructions per build (each run's minimum, the least affected by spinning threads; the three rounds' spread is within 0.5%):

Build main (M instr) this PR (M instr) instr allocations
Full rebuild 10,059 · 10,025 · 9,982 10,090 · 10,035 · 10,031 +0.3% same
Full rebuild, with the deletes 11,006 · 11,031 · 11,004 11,133 · 11,060 · 11,062 +0.65% same
Incremental, inserts only (new <g9> + default) 2,349 · 2,356 · 2,357 2,384 · 2,373 · 2,374 +1.0% same
Incremental, deletes into the existing <g2> (the named-graph merge) 3,411 · 3,407 · 3,399 3,410 · 3,397 · 3,398 −0.1% same
Incremental, deletes into the new <g9> 2,538 · 2,531 · 2,529 3,046 · 3,031 · 3,029 +19.8% +1.0% (+18% bytes)
Import, named graphs only 7,990 · 7,981 · 7,971 8,001 · 7,996 · 8,005 +0.25% same
Import, default + named 14,990 · 14,991 · 14,960 15,005 · 15,011 · 14,970 +0.1% same

inc_many_named isn't in the table because its instruction count is bimodal within a single run, on both sides: about 2.75 G or about 3.93 G per build, depending on whether threads spin. In six more interleaved rounds the high mode was 3,940 M on main and 3,934 M here. Its allocations are deterministic: +0.47% here (one small Vec per named graph for the order-set check).

This review round alone (the PR at 1309f8af5 vs 77cef24d9, both on 57bf7a944): every row is within ±0.3% instructions, with identical allocations. So the import fix, the order-set check and the Phase 2 check cost nothing measurable. The small increases above come from the lifecycle resolution the fresh branch and the rebuild loop now share.

  • Where the cost goes. The one line that moves is the window that deletes facts in a graph new to the index. The fresh branch now writes the sidecar history main was dropping (two transitions per deleted fact, four orders), the same history the merge path already writes for an existing graph. An earlier quiet-box run showed that with the fresh writer's history turned off, that window costs what it costs on main.
  • The order-set check. As first written, it hashed every named graph's branch CIDs on each build, which measured about as much as decoding, cloning and encoding the root itself (5.4 ms vs 4.5 ms at 10,000 named graphs). Hashing only presence-partial graphs brought it to 0.17 ms at 10,000 and 1.7 ms at 60,000 (release micro-benchmark), a presence pass over the root's routing.
  • Wall-clock, at load 5–10 in an earlier run of the same rows: the deletes-into-new-graph window was +24% (120 → 150 ms). The full rebuild with deletes was +1.9% against main's 1.3% spread across rounds, and an earlier quiet-box run had it within noise. The rest were within their spread.

Gates

At 01a49799d, after merging main at 1d06ae2ef, with --all-features:

  • cargo check --workspace --all-targets --all-features: clean.
  • cargo fmt --all --check: clean.
  • cargo clippy -p fluree-db-binary-index -p fluree-db-indexer -p fluree-db-api --all-targets --all-features --no-deps -- -D warnings: clean, with the edited files touched first so they're actually re-linted.
  • cargo test -p fluree-db-binary-index: 433 passed, 0 failed, 2 ignored.
  • cargo test -p fluree-db-indexer: 464 passed, 0 failed, 7 ignored.
  • cargo test -p fluree-db-api --no-fail-fast: 5,078 passed, 0 failed, 138 ignored. The import tests (grp_import) are 122 passed, grp_index 145 and grp_query 531. In two earlier full runs on this branch, it_jsonld_join_batched_lane::jsonld_join_takes_batched_subject_probe_like_sparql failed once each: its rows were right but the routing event it captures came back empty. It fails the same way on main at 57bf7a944 (1 of 3 grp_query runs) and passes alone, so it's intermittent there and not from this PR.
  • cargo test -p fluree-db-cli --test integration (its create --from tests go through import): 177 passed, 0 failed.

aaj3f added 7 commits October 14, 2026 10:41
… after an incremental build

Red on main: an incremental build writes a fresh branch for a graph with
no partition in its base index, and a fact inserted and deleted inside the
window comes back as a live row. Covers SPARQL and JSON-LD deletes, a new
named graph and a default graph with no indexed rows, strings, integers,
language-tagged strings and refs, a replaced value, and the window's
history.
…ow events

An incremental build writes a fresh branch for a graph, or an order, with
no branch in the base root: a named graph first written after the last
build, or the default graph of a ledger whose indexed data all sits in
named graphs. That branch was written from the window's net records with
their ops ignored, so a fact whose net op was a retraction became a live
row, and the events lifecycle dedup superseded were dropped with a warning.

The fresh branch now resolves each fact's events from absent, as a full
rebuild does: transitions go to the history sidecar and only a final
assert becomes a row. Both use one LeafWriter::push_lifecycle, and the
fresh path reuses the leaf merge's identity gather. The default- and
named-graph fresh builders were the same function; they are now one.
With retraction winners now resolved, a fresh branch can come out with no
leaves: every fact in the window netted to no row and no history. A branch
with no leaves routes no novelty, so a later incremental build would drop
the graph's writes. Publish nothing instead; the next build sees the graph
as fresh again. The default- and named-graph fresh arms are merged.
No build is driven: the insert stays under the soft threshold and the
delete commit crosses it, so the background indexer's own incremental
build covers both, in the default graph of a ledger indexed only in a
named graph and in a new named graph.
…d takes the insert and the delete

With small data the background indexer builds after every commit, so the
insert was indexed before the delete committed and the test passed on
main. A large write in flight now holds the insert and the delete for one
later build, under the default thresholds, and the test checks that shape
before it reads.
…d novelty

A published branch with no leaves holds no base facts, the same as no
branch. The incremental build routed its novelty into the leaf merge,
where slicing found no leaf and returned no slices, so the window's
writes for that graph and order were dropped. Such a branch now takes
the fresh path, decided once the branch is known (from the root's inline
leaves for the default graph, from the fetched manifest for a named
graph), and slicing refuses novelty for a branch with no leaves instead
of returning nothing.

A full rebuild no longer publishes a graph whose log nets to no row and
no history, matching an incremental build's empty fresh branch.

The incremental build also refuses a base root in which a graph has some
but not all four sort orders. No writer produces one (each builds a
graph's four orders from one fact set); a missing order would otherwise
be built fresh from the window alone, without the graph's base facts.
The refusal is an incremental abort, so the caller falls back to a full
rebuild.
@aaj3f aaj3f added the bug Something isn't working as expected label Oct 14, 2026
@aaj3f
aaj3f requested a review from bplatz October 14, 2026 19:30

@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.

Approving. Please review the inline feedback before merging.

orders.dedup();
orders.is_empty() || orders == all
};
let default_orders = default_graph_orders.iter().map(|o| o.order).collect();

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.

A bulk import whose quads are all in named graphs writes a root whose default graph has only a leafless SPOT. build_spot_index_from_commits in build_from_commits.rs returns its graph even with no leaves, unlike build_index, so the next incremental build aborts here and falls back to a full rebuild. Treating a leafless default-graph order as absent before the none-or-all check, and not emitting the leafless SPOT on import, would let it build incrementally. Repro: import a TriG file with one quad in GRAPH <g>, insert one default-graph fact, then call build_incremental_index_for_record.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, this reproduced exactly as you describe, and the import turned out to have a second shape I hadn't expected: an empty GRAPH <g> { } block gets its graph id too, and import wrote that graph a SPOT with no leaves and nothing else. I've taken both halves of your suggestion.

  • The import no longer writes such an order (9c946b45b). PersistingLeafWriter::finish now returns None when it wrote no leaf, so the import's SPOT build and the full rebuild's order builds both leave a graph with nothing to hold out of the root, rather than each checking for it on its own.
  • An order with no leaves counts as absent in check_order_sets (2eaa71549, narrowed in 77cef24d9), for the default graph and named graphs alike. The default graph's leaves are inline in the root. For a named graph I didn't want a branch fetch per order on every build, but a branch with no leaves is exactly build_branch_bytes(order, g_id, &[]) (one decodable branch version, and every writer builds through that function), so its CID identifies it from the root alone. I checked that against a root main's import actually wrote, and the CIDs match. Hashing every named branch on every build turned out to cost about as much as decoding the root itself (5.4 ms vs 4.5 ms at 10,000 named graphs), so it's only done for a named graph with some orders but not all, which is exactly the older import's shape.

So a root an older import wrote reads as having no facts for those graphs, and the next build takes them incrementally. graphs_an_import_left_empty_take_facts_incrementally runs your repro (one quad in GRAPH <g2>, plus the empty block, a default-graph insert, then build_incremental_index_for_record) against both a fresh import and a root rewritten to the older import's shape. It fails at 1309f8af5, where the import still writes those orders, and with either half of the absent-count turned off the older root's build aborts.

// all, so it is built fresh too: the merge has no leaf to route its
// novelty into.
let route = match kind {
Phase2TaskKind::DefaultExisting { leaves } if !leaves.is_empty() => {

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.

Main can already have written a root from that import shape with a leafless SPOT and the other three orders populated, because main drops SPOT's novelty on a default-graph write. That root passes check_order_sets, so SPOT is built fresh from this window only and permanently misses the earlier default-graph facts. I reproduced it by building on main and then on this branch: the fact stays missing by subject. The leafless-as-absent check above would flag it as partial and repair it through the rebuild. Since main drops default-graph writes from SPOT after a named-graph-only import, this is worth a line in the description, along with a note that affected indexes need a reindex.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, and you're right on every count. This one is on main already, and the description's line that no writer produces a partial order set was wrong, since import did. I've corrected that and added a section on what main does here.

With leafless-as-absent, the root you describe (SPOT with no leaves, the window's facts in the other three) is partial, so the next build aborts and the full rebuild it falls back to restores SPOT. For the default graph check_order_sets sees that from the inline leaves. A named graph gets into the same state on main once a graph from an empty GRAPH block is written to, and for one with all four orders the check is check_base_facts_agree after Phase 2 (2eaa71549). That refuses a graph whose four tasks disagree on whether the base holds its facts, and it knows each fetched branch's leaves at no extra cost. facts_missing_from_spot_are_rebuilt builds that root the way main writes it, for the default graph and for such a named graph. It checks that by subject the fact is missing while by predicate it's there, then writes another fact and checks that the incremental build refuses and the rebuild brings both back by subject.

On what a user sees on main (probed at 04e1a6d37): every default-graph fact written after a named-graph-only import is missing from SPOT once it's indexed, and so is every fact later written to a graph an empty GRAPH block created. Reads that go through SPOT miss them. That covers a subject-bound pattern (ex:s ex:p ?o, ex:s ?p ?o), a fully bound ASK, ?s ?p ?o (so DISTINCT ?s and GROUP BY ?s too), CONSTRUCT by subject, the JSON-LD subject crawl (just the @id comes back), time travel by subject, and a join whose plan looks the subject up. Predicate- and object-bound patterns still see them, and so do COUNT(*), a FILTER(?s = …) over a predicate scan, and the OPTIONAL and property path I tried. The part I'd flag most is that writes whose WHERE binds the subject miss them too: DELETE { ex:s ex:p ?o } WHERE { ex:s ex:p ?o } deletes nothing, and a JSON-LD upsert keeps the old value next to the new one. An affected index is repaired by a reindex. With this PR the next build repairs it too: for the default graph whatever the window touches, and for a named graph the next time that graph is written.

Named {
branch_cid: ContentId,
},
/// A fresh branch with nothing to hold: the graph keeps no branch for

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.

If the base root had a leafless branch for this order, the graph keeps that branch, so "keeps no branch" isn't quite accurate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, fixed in 2eaa71549. The comment now says the root keeps what it had for that order, no branch or one with no leaves, which the next build again finds no base facts in. Since the build now treats an order with no leaves the same as a missing one, keeping it is harmless, and no writer adds a new one.

//! named graph first written after the build, or the default graph of a
//! ledger whose indexed data all sits in named graphs.
//!
//! Background indexing is off: these tests drive each build themselves, and

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.

background_indexing_keeps_deleted_facts_deleted runs with background indexing on, so this header isn't true for that test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed in b117c1de3. The header now says background indexing is off in all but that test, and that that one holds the indexer under its soft threshold rather than racing it.

.expect("nameservice lookup")
.expect("ledger record")
.index_t;
assert!(

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.

This is timing-dependent. With the default threshold nearly every commit triggers a build, so an insert-only build can publish before the delete commits (a spurious failure). It can also start without publishing, in which case the test passes without one build covering both. The four deterministic tests carry the real coverage, so this one could be made deterministic or noted as supplementary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, and I went with deterministic (b117c1de3), using a gate the code already has rather than a test hook: the soft threshold. The test now sets reindex_min_bytes to 100 KB and turns catch-up sweeps off, since those ignore the threshold. A commit over the threshold builds the G2-only base. The insert and the delete each stay under it, and the test asserts that from each commit's own indexing.needed, which is exactly what decides whether the commit starts a build, so it fails loudly if either one could have. Then one commit crosses the threshold, so the only build after the base's takes both writes in one window. On main's sources it fails at the final read (["\"x\""] for the default graph), and it passes here. Related, and the same class of problem: the branch now also carries a change to the test helpers that publish an index (326fe7c06). They go through one publish_built_index that asserts the nameservice holds the build the test just made, since publish_index quietly declines one at a t the background indexer already published. That turned up 31 tests elsewhere reading an index other than the one they built, mostly background-indexer races; the description has the details.

aaj3f added 6 commits October 19, 2026 08:50
The import builds SPOT for each graph on its own, and returned a result
for the graph even when it had no records, unlike the secondary orders.
So an import whose quads were all in named graphs wrote a default graph
whose only order was a SPOT with no leaves, and an empty GRAPH block
wrote the same for its named graph.

The persisting leaf writer now returns no result when it wrote no leaf,
so the import's SPOT build and the full rebuild's order builds leave such
a graph out of the root alike.
A root an older import wrote can give a graph a SPOT with no leaves and
no other order. The incremental build took that for a partial order set
and fell back to a full rebuild, though the graph holds no facts at all.

An incremental build used to leave that SPOT empty while writing the
window's facts to the other three orders. The root it wrote had all four
orders and passed the check, so a later window would build SPOT fresh,
without those facts.

The order-set check now counts an order with no leaves as absent: a
default-graph order by its inline leaves, a named-graph order by its
branch CID, since a branch with no leaves has fixed bytes for its order
and graph. The first root is then complete and builds incrementally; the
second is partial and falls back to a full rebuild, which restores SPOT.
After Phase 2 the build also refuses a graph whose tasks disagree on
whether the base holds its facts, for a branch with no leaves that the
root alone doesn't show.
Two tests on a TriG import with no quads for the default graph or for an
empty GRAPH block's graph: the next build takes their first facts
incrementally, also from a root an older import wrote; and an index
whose SPOT lacks facts the other orders hold, as an incremental build
wrote after such an import, is refused and rebuilt so the facts read by
subject again.

The background test is now deterministic: the indexer is held under its
soft threshold, with catch-up sweeps off, until one commit after the
insert and the delete crosses it, and each commit's indexing status
proves no build started in between.
Clippy's type-complexity lint refused the table of branch-builder
closures; a flag for whether the root can recognize the branch says the
same thing.
… partial

Telling a named branch with no leaves by its CID costs a SHA-256 per
branch, which measured about as much as decoding, cloning and encoding
the root itself (5.4 ms against 4.5 ms at 10,000 named graphs), on every
incremental build. The order-set check now does it only for a named
graph with some orders but not all, the older import's shape. A graph
with all four is taken as having facts in all four, and the Phase 2
check, which already knows each touched branch's leaves, refuses one
whose SPOT has none.

The SPOT test's case with a branch the root can't recognize is gone: a
named graph's case now takes that same path. The leafless-branch unit
test checks that such a task reports no base facts.
aaj3f added a commit that referenced this pull request Oct 19, 2026
`a_transfer_keeps_a_value_both_graphs_hold` failed in CI on a MOVE whose
source was written since the destination's build: the source read back
its moved value after a reload. `FlureeBuilder::file` starts a
background indexer, which can run an incremental build between the
transfer and the read, and an incremental build brings back a fact
inserted and retracted since its base in a graph the base holds nothing
for (#2118, a separate fix). The read before any build is right.

Every instance in this PR's file-backed transfer, merge/revert,
multi-op and temporal-ordering tests is now built `without_indexing()`,
and the tests build the index themselves. The transfer test reads with
no build since the transfer, after an incremental build, and after a
full rebuild. After the incremental build it reads the source only
where #2118 cannot reach it (any verb but a MOVE out of a graph written
since the base), and after the full rebuild it reads both.

The full rebuild after an incremental one is published through
`Fluree::reindex`. `publish_index` declines an index whose `t` is not
newer than the published one, so the rebuild the multi-op test ran
after its incremental build was never published, and its "after a
rebuild" read repeated the incremental one.
aaj3f added 4 commits October 19, 2026 12:31
`publish_index` declines, silently, an index whose `t` is not newer than
the one already published. The test helpers that build and publish an
index (`rebuild_and_publish_index`, `build_and_publish_index`,
`build_incrementally_and_publish_index`) published through it and never
looked, so a second build at one `t` left the test reading the index it
meant to replace:
- `repeat_full_rebuild_restores_retracted_history` rebuilds a second
  time at one `t` to show that re-running a reindex restores history;
  the second rebuild was never published, and the test read the first;
- 30 tests on a file-backed `Fluree` with its background indexer
  running had their rebuild declined whenever the indexer published at
  that `t` first. Over three runs of the API suite, 16 to 21 of them hit
  this per run. At `t` 2 or 3 (seven tests) they read the indexer's build
  in place of the full rebuild they asked for.

Every helper now publishes through `support::publish_built_index`, which
publishes as `Fluree::reindex` does, replacing an index of the same `t`,
and then checks that the nameservice holds this build. A newer published
index can't be replaced, and the check panics, naming the background
indexer as the likely cause. The nine direct publishes in
`it_import_v3`, `it_indexing_fuel`, `it_storage_encrypted` and
`it_merge` (including its `index_branch` helper) go through it too.

With the builds published, the whole API suite passes: no test relied on
reading the replaced index.
The SPOT and import tests publish a root they rewrote; they now go
through the shared helper, which asserts the nameservice holds that root
before the test reads it.
@aaj3f
aaj3f merged commit fdf6ccd into main Oct 20, 2026
19 checks passed
@aaj3f
aaj3f deleted the fix/delete-survives-index-build branch October 20, 2026 12:22
aaj3f added a commit that referenced this pull request Oct 20, 2026
…ferences

Brings in #2137, #2118 and #2109. The one conflict is stage.rs's
fluree_db_core import: #2109 dropped `FlakeMeta` from it, and this branch
added the dataset-reference types. #2109's FactIdentity DELETE naming and
fold, and this branch's typed graph resolution for USING / WITH and the
write table, sit in separate hunks and merge as both wrote them.
bplatz added a commit that referenced this pull request Oct 20, 2026
Brings main since 1888006 onto next, #2109 and #2118 among it. Resolved:

- template_sink: main's statement hashes for the content identity, with
  next's rdf:reifies link and its datatype check; no bundle templates.
- incremental: main's Phase2Route (a leafless branch is built fresh, the
  fresh builder keeps in-window history, an empty fresh branch is not
  published) on next's storage handle, without cache_dir.
- build_from_commits tests: main's helpers with next's term fields.
- commit_transfer, rebase: the imports both sides still use.
- test support: main's publish_built_index, publishing allow-equal under
  the ledger's fence; its callers take main's side.

Ported to next:

- index_key_may_merge_terms answers for a triple term: its key merges
  two terms only through its object's.
- The SPARQL triple-term literal drops IllTyped, which main removed; it
  refuses an ill-typed literal, as it did.
- Main's new tests read a ledger's store through its storage namespace,
  await the async genesis_ledger, and the ill-typed upsert test names a
  ledger per iteration rather than a branch.
bplatz added a commit that referenced this pull request Oct 21, 2026
#2118 (via main) and this branch both write an order with no base leaves
from the window alone. The folded tree here does it for every such order,
including a pooled graph's promotion, so #2118's fresh-branch builder is
not carried over. Its other rules are: a graph's folded tree with nothing
to hold is not published, a base root with a partial order set falls back
to a full rebuild, and a dedicated graph's four tasks must agree on whether
the base held its facts. The pool's orders are not checked there, since its
secondary orders take writes as delta runs.

The leaf merge's identity gather takes slices, as #2118 needs, and the
order's identity, as the pool needs. #2118's tests pin a named graph's own
trees, so they keep the graph dedicated.
aaj3f added a commit that referenced this pull request Oct 30, 2026
The parent now carries main 4a0d14a (#2109, #2118, #2133). Four files
conflicted:

- temporal.rs: main's `cmp_with_lexical` body is the one this branch
  already had (#2109's); the two docs are merged, so it says both that a
  dateTime or time lexical is held to the microsecond and that the
  evaluator and the bounds pass it only a plain xsd:string.
  `lexical_as_kind_of` stays, over the same parse functions.
- query_bounds.rs: this branch's string-vs-temporal arm (the plain-string
  guard and the per-kind parse cache) replaces main's simplified one, which
  called `cmp_with_lexical` with no guard. `string_bound_otypes` stays.
- eval/compare.rs: the plain-string guard stays on the evaluator's
  coercion.
- binary_history.rs: main's shared object check (#2133) is kept, with the
  term-aware `matches(&f.o, &f.dt)`; this branch's separate bound test in
  the novelty walk is dropped, as the shared check covers both lanes.
q-qp-p pushed a commit to q-qp-p/fluree-db that referenced this pull request Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants