fix(git): never discard committed local work in rebase_onto_base (#683)

The shared rebase primitive (dev sync_branch verb, PM/CEO rebase path,
submit-freshen, merge-conflict resolver) opened with fetch -> checkout ->
unconditional reset --hard origin/<head_branch>. The dirty-tree gate
protects uncommitted edits only; the reset silently rewound past every
committed-but-unpushed commit — and the commit do-verb never pushes, so
mid-rework a dev routinely has exactly that. The force-with-lease push
then republished the truncated branch as authoritative (the lease
matched the freshly-fetched, never-moved origin ref).

rebase_onto_base now classifies local vs origin/<head> post-fetch:
- behind/equal: reset --hard origin as before (origin loses nothing)
- strictly ahead: reset skipped — the rebase runs from the local tip and
  the lease'd push publishes the previously-doomed commits
- diverged: a patch-equivalence probe (rev-list --right-only
  --cherry-pick) first rescues the self-inflicted residue of a prior
  rebase whose force-push failed (treated as ahead, self-heals on
  retry); only genuine two-sided divergence returns a new
  {status: diverged, local_only, origin_only} — no reset, no rebase,
  no push, neither side silently discarded
- an absent local ref is recovered from origin (branch + checkout,
  never reset)

Callers: the sync_branch verb maps diverged to an actionable envelope
steering to i_am_blocked (stash-preserved note included); the
submit-freshen hard-rejects it like conflicts; the merge-conflict
resolver already escalates any non-rebased/superseded status to the
CEO and degrades gracefully (pinned by test, no code change).

New real-git suite (bare origin + clone, no subprocess mocking)
asserts origin-side outcomes: ahead-publishes, behind-adopts,
diverged-refuses-untouched, absent-ref recovery, superseded,
conflicts, and wedge self-heal on retry via a rejecting pre-receive
hook.

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
This commit is contained in:
Renzo F
2026-07-24 14:34:25 +02:00
committed by GitHub
co-authored by Renn F
parent 3516d925fe
commit 71f5426e40
9 changed files with 744 additions and 63 deletions
@@ -85,6 +85,27 @@ async def test_freshen_conflicts_reject_with_files() -> None:
assert "stats.json" in body["message"]
@pytest.mark.asyncio
async def test_freshen_diverged_rejects() -> None:
"""A diverged branch is refused just like a conflict — never guessed at."""
git = AsyncMock()
git.is_behind_base.return_value = (2, 3)
git.sync_task_branch.return_value = {
"status": "diverged",
"local_only": 1,
"origin_only": 2,
}
c = Choreographer(_make_deps(git=git))
env = await c._freshen_assembled_branch(
_cell_task(), base_branch="feature/main_pm/root", verb="submit_up"
)
assert env is not None
body = env.as_dict()
assert body["error"] == "invalid_state"
assert "DIVERGED" in body["message"]
assert "reconcile" in body["remediate"]
@pytest.mark.asyncio
async def test_freshen_fails_open_on_probe_error() -> None:
git = AsyncMock()