Skip to content

Compare branches by commit identity instead of t - #1905

Merged
zonotope merged 17 commits into
mainfrom
fix/sibling-merge-ancestor-cutoff
Sep 27, 2026
Merged

zonotope merged 17 commits into
mainfrom
fix/sibling-merge-ancestor-cutoff

Conversation

@zonotope

@zonotope zonotope commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1912

Summary

Merging a branch into its parent a second time silently missed conflicts. Both sides could change the same fact and the merge would report none, so abort went through and take-branch left both values in place.

Every branch numbers its commits from its own fork point, so a t from one branch means nothing on another. Merge found the common ancestor and then cut each side's commit walk at ancestor.t. After an earlier merge, that ancestor sits on the other branch's clock, and the cutoff drops commits that came after it. Rebase and merge preview cut the same way.

The two sides are now compared by commit identity. Along the way this fixes merging in directions that never worked: a parent into its child, one branch into a sibling, and main into a branch.

What was broken

  • Missed conflicts. Child x is merged into dev, then both change the same fact, then x is merged again. The base is the x commit merged the first time, whose t is above every commit dev made since, so dev's side came out empty.
  • No way to merge a parent into its child. main was refused outright, by a guard that exists only to infer a default target. Any other parent failed with a storage error, because merge read both heads through the source's store. The same error hit two branches off main once both had commits. Merge preview read both stores, so it reported these merges as fine.
  • Fast-forward on the wrong condition. The rule was "the common ancestor is the target's head", which is exactly the case after a sync. Adopting the source's head there replaces the target's line with one on another clock, and its t falls.

The fix

diff_branches in fluree-db-core compares two branches by identity. It follows each branch's line of first parents, and follows merge parents into the branches they brought in, so a commit an earlier merge brought in counts as one the branch already holds. It returns, per side:

  • commits, every commit on that branch's line above the divergence. Folding them gives the branch's state, so a merge applies them and a rebase replays them.
  • own, the subset carrying the branch's own changes. A merge whose merged-in history the other side already holds is left out, because its changes came from that side. This is what conflict detection compares.

The split matters in both directions. Comparing everything reports conflicts on keys the other side never touched. Folding only own drops a merge commit's flakes, which are the strategy's resolution rather than a copy of what it merged: syncing with take-source and merging back then resurrected the value the sync had resolved away.

It also returns the commits the target lacks, parents first, and a fast_forward flag that is true only when the target's head is on the source's line.

Merge, rebase and merge preview all use it, so a preview and the merge it previews now see the same divergence.

Merging in any direction

  • An explicit --target is enough. The source needs a parent branch only when the target is inferred from it.
  • The diff reads through a union of both branches' stores, so it resolves either head whichever way the merge runs.
  • Sibling merges work through the same path.

Tests

  • Core: the diff on a plain fork, a fast-forward, a second merge of the same branch, a round trip after a sync, unrelated histories, and the commit list the target lacks.
  • Merge: the missed conflict under abort and under take-branch, a resolution surviving a merge back, a key only the source changed not conflicting with the target's earlier merge of it, a key the sync settled not conflicting again, a real conflict in each new direction resolved with take-source and take-branch, the new fast-forward case, and the round trip.
  • Preview: the conflict its merge meets, with counts and the reported ancestor, and a preview in a newly reachable direction.
  • Rebase: a branch that merged its source in, and a conflict after a sync under abort and take-branch.

Each fix was confirmed by restoring the old behavior and watching the matching test fail.

@zonotope
zonotope requested review from aaj3f and bplatz September 21, 2026 23:40
@zonotope
zonotope changed the base branch from main to fix/sync-push-after-merge September 21, 2026 23:42
Base automatically changed from fix/sync-push-after-merge to main September 22, 2026 03:05
@zonotope zonotope added bug Something isn't working as expected area:transact Staging, commit, import/bulk-import, novelty, retraction semantics labels Sep 22, 2026

@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: comparing branches by commit identity is the right fix, and it catches the conflicts the old t cutoff missed. Please address the inline comments before merging.

The first three have one cause. A sync merge commit (the parent merged into its child) gets folded or replayed whole, so it carries the other branch's old values back. The comment on merge.rs has a repro and a suggested fix. The fourth is a doc line that no longer matches the code.

Comment thread fluree-db-api/src/merge.rs Outdated
Comment on lines +827 to +835
let commit = load_commit_by_id(store, cid).await?;
if own.contains(cid) {
keys.extend(
commit.flakes.iter().map(|flake| {
ConflictKey::new(flake.s.clone(), flake.p.clone(), flake.g.clone())
}),
);
}
commits.push(commit);

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.

Folding a sync merge in whole brings back the target's old values.

Say dev syncs main in through a real merge commit S, and main then changes one of those keys again. Merging dev back re-asserts main's old value. It reports no conflict, and the key ends up with both values.

S is in diff.source.commits but not in own, so this loop folds all of its flakes. On keys dev never touched, S's flakes are just copies of main's earlier change, and applying them to main's newer state brings that change back. Only S's flakes on keys that dev's own earlier commits changed are the merge's resolution.

Repro, using the helpers this PR adds to it_merge.rs:

#[tokio::test]
async fn merge_back_after_sync_keeps_the_targets_later_value() {
    let fluree = FlureeBuilder::memory().build_memory();
    fluree.create_ledger("mydb").await.unwrap();
    insert_named(&fluree, "mydb:main", "ex:alice", "Alice").await;
    fluree.create_branch("mydb", "dev", None, None).await.unwrap();
    // A dev commit first, so the sync writes a real merge commit.
    insert_named(&fluree, "mydb:dev", "ex:d1", "d1").await;
    rename_alice(&fluree, "mydb:main", "v1").await;
    fluree
        .merge_branch("mydb", "main", Some("dev"), ConflictStrategy::default())
        .await
        .unwrap();
    rename_alice(&fluree, "mydb:main", "v2").await;

    fluree
        .merge_branch("mydb", "dev", None, ConflictStrategy::Abort)
        .await
        .unwrap(); // 0 conflicts
    // Actual: ["d1", "v1", "v2"]
    assert_eq!(query_all_names(&fluree, "mydb:main").await, ["d1", "v2"]);
}

One possible fix: keep a non-own commit's flakes only on keys that own commits earlier in the list touched. keys already builds up oldest-first, so:

        let mut commit = load_commit_by_id(store, cid).await?;
        if own.contains(cid) {
            keys.extend(/* as now */);
        } else {
            // A merge of history the other side holds. Only its flakes on
            // keys this side changed are its resolution; the rest copy the
            // other side's changes.
            commit.flakes.retain(|flake| {
                keys.contains(&ConflictKey::new(
                    flake.s.clone(),
                    flake.p.clone(),
                    flake.g.clone(),
                ))
            });
        }

With that change the repro passes, and the rest of it_merge still passes, including merge_back_keeps_a_resolution_the_sync_made. Rebase and preview have the same gap; see the comments on rebase.rs and merge_preview.rs.

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.

Addressed in f4e73a6

Comment thread fluree-db-api/src/rebase.rs Outdated
Comment on lines +413 to +416
// Replay every commit on the branch's line, merges included: a
// merge the branch made carries how it resolved that merge.
let summaries =
scan_branch_commits(&branch_store, &diff.source.commits, &source_delta).await?;

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.

Rebase has the same gap: it replays the sync merge whole.

Use the same setup as the merge-back repro on merge.rs, up to main's rename to v2, then rebase dev instead of merging it back:

    // Fails with "Rebase aborted: 1 conflict(s)", although dev never touched alice.
    fluree.rebase_branch("mydb", "dev", ConflictStrategy::Abort).await.unwrap();
    // With TakeBranch it succeeds, but dev ends on ["d1", "v1"]: main's v2 is lost.

S's flakes on alice are main's old rename. They conflict with source_delta (main's later rename), and under take-branch they win. Filtering the merge's flakes down to keys that dev's own earlier commits touched, as suggested on merge.rs, fixes both cases.

rebase_after_sync_detects_conflict doesn't cover this. dev has no commits of its own when it syncs, so the sync is a fast-forward and no merge commit is replayed. Adding a dev commit before the sync makes the test replay S.

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.

Addressed in f4e73a6

Comment thread fluree-db-api/src/merge_preview.rs Outdated
match (&diff, &source_head) {
(Some(diff), _) => {
let (_, net, ns_delta) = if need_changes || need_validation {
delta_keys_and_changes_of(&source_store, &diff.source.commits, true).await?

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.

After a sync, the change list shows the target's own changes.

Same cause again: folding all of diff.source.commits includes the sync merge. After dev syncs main in, previewing dev → main lists main's own changes as dev's:

    insert_named(&fluree, "mydb:main", "ex:a", "a").await;
    fluree.create_branch("mydb", "dev", None, None).await.unwrap();
    insert_named(&fluree, "mydb:dev", "ex:d1", "d1").await;
    for n in 2..=4 {
        insert_named(&fluree, "mydb:main", &format!("ex:m{n}"), &format!("m{n}")).await;
    }
    fluree
        .merge_branch("mydb", "main", Some("dev"), ConflictStrategy::default())
        .await
        .unwrap();
    let opts = MergePreviewOpts { include_changes: true, ..Default::default() };
    let preview = fluree.merge_preview_with("mydb", "dev", None, opts).await.unwrap();
    // changes.entries subjects: ex:d1, ex:m2, ex:m3, ex:m4. Expected: ex:d1 only.

In a merge-request view, every "update branch" would pull upstream's changes into the branch's diff. Validation stages this same change set, so fixing it together with merge keeps preview and merge in agreement.

The same filter would also let this call and the delta_keys_of(.., own) call below share one pass, as merge's collect_commit_data does. Today a default preview reads each source commit twice.

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.

Addressed in 88415ae

Comment thread docs/cli/branch.md Outdated
Replays a branch's unique commits on top of the source branch's current HEAD. This brings the branch up to date with upstream changes. The `main` branch cannot be rebased.
Replays a branch's unique commits on top of the source branch's current HEAD. This brings the branch up to date with upstream changes, and rewrites the branch's commits to do it. To keep the branch's commits as they are, merge the source into the branch instead. The `main` branch cannot be rebased.

A branch that already merged its source in is rebased on its own commits only. The merge that brought the source in is left out, because its changes came from the source.

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 no longer matches the code. Since cc3fec0, prepare_rebase replays diff.source.commits, and that list includes the merge that brought the source in (rebase.rs around line 413). Once the rebase comment is settled, three places should say the same thing: this line, the "Pass 1: read the branch's own commits" comment in rebase.rs, and the rebase bullet in docs/cli/server-integration.md ("that merge is not one of them").

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.

Addressed in cb0593a

@bplatz

bplatz commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

This PR fixes #1912 (the status section of that issue says so), but the PR description has no Fixes #1912 line, so the issue won't close when this merges. Could you add that line to the description? It has to go in the description itself: a closing keyword in a comment doesn't link the issue.

@zonotope
zonotope merged commit ac1e8a6 into main Sep 27, 2026
16 checks passed
@zonotope
zonotope deleted the fix/sibling-merge-ancestor-cutoff branch September 27, 2026 22:27
@aaj3f aaj3f mentioned this pull request Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:transact Staging, commit, import/bulk-import, novelty, retraction semantics bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merges between sibling branches can pick a common ancestor on the wrong clock

2 participants