Skip to content

Merge/rebase/revert transplant flakes with branch-local t — time-travel reads below a merge return rewritten history and phantom duplicates #1783

Closed

Description

@aaj3f

Third finding from the claude-code audit session behind #1781 and #1782, and the one that surprised me most. Head-of-branch state after a merge is fine in every combination we tried — but every t-parameterized read below the merge point is now wrong, because the branch operations copy source flakes (and source commits) into the target's timeline carrying branch-local t values.

Repro 1 — a plain, conflict-free merge rewrites the target's history

# main t1: ex:items = A ; branch feat ; feat t2: replace A->B
# main t2, t3: two commits touching UNRELATED subjects
fluree query -l demo --at 2 ...   # BEFORE merge: A   ✓
fluree query -l demo --at 3 ...   # BEFORE merge: A   ✓
fluree branch merge feat -l demo  # "Merged ... (t=4, 1 commits copied, 0 conflicts)"
fluree query -l demo ...          # HEAD: B            ✓
fluree query -l demo --at 2 ...   # AFTER merge: B    ✗ — history changed retroactively
fluree query -l demo --at 3 ...   # AFTER merge: B    ✗

The merge commit landed at t=4, yet main's state as of t=2 now shows the branch's update. No conflicts, no strategies involved — the vanilla flow. Commits are supposed to be immutable history; a merge that edits what --at 2 returns breaks time-travel, history, and anything else that treats t as a total order on the branch.

Repro 2 — phantom duplicates at --at (the both-values symptom, one layer down)

With main doing A→C (t2) then C→A (t3) while feat does A→B (its t2), post-merge (merge commit t=4):

--at 2: C AND B        (main at t=2 actually held only C)
--at 3: A AND B        (main at t=3 actually held only A)
--at 4 / HEAD: B       (correct)

So the same "two values on a maxCount-1 property" corruption from #1781 also materializes purely from t skew, on historical reads, with byte-identical values.

Root cause

A flake's t is a per-branch logical clock, but the branch operations transplant flakes without restamping:

  • Merge: collect_commit_data (fluree-db-api/src/merge.rs:705-716) → collect_from_commits (fluree-db-api/src/commit_data.rs:34-50) gathers source-branch flakes verbatim, t included; they go through StagedLedger::new (merge.rs:540) into build_commit, where Commit::new(new_t, flakes) (fluree-db-transact/src/commit.rs:699) sets the commit's t but leaves every flake stamped with the source branch's t.
  • Contrast the normal write path, which is careful about exactly this: stage.rs restamps at fluree-db-transact/src/stage.rs:232, :461, :819 (retract.t = new_t / f.t = new_t).
  • Rebase replays via build_replay_commit (fluree-db-api/src/rebase.rs:651-686) with the same verbatim flakes, so replayed commits at chain-t N carry flakes stamped with the pre-rebase t.
  • Two comments in the code believe a restamp happens that doesn't: build_source_retractions emits t: 0, // overwritten by commit (fluree-db-api/src/rebase.rs:801-806) and Flake::invert_at documents "the staging layer will rewrite t anyway" (fluree-db-core/src/flake.rs:464-473) — true for stage.rs, false for the StagedLedger + build_commit path that merge/revert/take-source actually use. So take-source retractions and revert inversions are stamped t=0 in durable commits.
  • On top of the flake stamps, state-at-t reconstruction appears to walk the commit DAG through merge parents and admit source-branch commits by their branch-local commit t. I'll flag that this half is triangulated behaviorally rather than line-traced: a revert commit at t=3 whose flakes are stamped t=0 is correctly excluded from --at 2 (so exclusion is by commit t), yet the merge commit at t=4 contributes B to --at 2 — which only fits if the walk descended into the feat parent, whose commit is t=2 on its clock.

Why HEAD stays right: current-state resolution is application/chain-order (a later-applied retract cancels regardless of t — we verified this across novelty replay, incremental index, and full rebuild, including a revert whose t=0 inversions still won). That's also why this has been easy not to notice: you only see it through --at, and the wrongness is silent.

Fix direction

Restamping staged flakes to new_t in the three branch paths (mirroring stage.rs) fixes the flake-level half and makes the two stale comments true again — that part looks mechanical. The commit-DAG half needs a real decision though: once flakes are restamped, state-at-t reconstruction should presumably follow the first-parent chain only and rely on the merge commit carrying the folded flakes, rather than descending into merge parents whose t values are from a different clock. That touches fluree-db-core's walk semantics and time-travel behavior for existing ledgers (whose already-written merge commits have the skewed stamps), so I don't think it's a drive-by fix — it likely wants a small design note on what --at N means on a branch with merges before the code change. Flagging rather than PRing for that reason; happy to talk through the semantics.

Full repro script (fluree 4.1.6, local mode)
#!/bin/bash
set -u
CTX='{"ex": "http://example.org/"}'
Q='SELECT ?o WHERE { <http://example.org/config> <http://example.org/items> ?o }'
mkdir -p /tmp/tskew-repro && cd /tmp/tskew-repro
fluree init >/dev/null 2>&1; fluree create demo >/dev/null 2>&1
fluree insert -l demo -e '{"@context": {"ex": "http://example.org/"}, "@id": "ex:config", "ex:items": {"@value": [{"name": "alpha", "qty": 1}], "@type": "@json"}}'
fluree branch create feat -l demo
fluree update -l demo:feat -e '{"@context": {"ex": "http://example.org/"}, "where": [{"@id": "ex:config", "ex:items": "?old"}], "delete": [{"@id": "ex:config", "ex:items": "?old"}], "insert": [{"@id": "ex:config", "ex:items": {"@value": [{"name": "beta", "qty": 2}], "@type": "@json"}}]}'
fluree insert -l demo -e '{"@context": {"ex": "http://example.org/"}, "@id": "ex:other1", "ex:name": "u1"}'
fluree insert -l demo -e '{"@context": {"ex": "http://example.org/"}, "@id": "ex:other2", "ex:name": "u2"}'
fluree query -l demo --at 2 -e "$Q" --format json    # A (pre-merge truth)
fluree query -l demo --at 3 -e "$Q" --format json    # A
fluree branch merge feat -l demo                     # t=4, 0 conflicts
fluree query -l demo --at 2 -e "$Q" --format json    # B  <-- history rewritten
fluree query -l demo --at 3 -e "$Q" --format json    # B  <-- history rewritten
# Repro 2 (duplicates at --at): same setup but main does A->C then C->A instead
# of the unrelated inserts; post-merge --at 3 returns BOTH A and B.

Versions: fluree CLI 4.1.6 (local mode, macOS); source refs against main @ 1aae89018.

Activity

  1. added
    bug
    P1
    area:transact
    on Sep 3, 2026
  2. self-assigned this
    on Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1area:transactbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions