From 76a3a66270401fb0057d3e184fac45938d0c6899 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 13:57:07 +0300 Subject: [PATCH 1/2] fix(repo): refuse a push that moves a checked-out branch during a stopped merge, pick or rebase LocalTransport::update_ref (sc push to a local path, and the wire UpdateRef) moved a branch with only a CAS/fast-forward gate. A push into a repo with a stopped merge/pick moved the checked-out tip from underneath it, and the completing commit then silently dropped the pushed work. It now refuses for the receiving repo's checked-out branch while one of those ops is in progress; other branches are unaffected. Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 3 +- ROADMAP.md | 13 ++--- crates/repo/src/transport.rs | 103 +++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 8 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 2cc56a15..12182958 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -253,7 +253,8 @@ transport-adjacent. The rest, imperatively: away and retry. Merging into an unborn branch and the conflict stop of a merge into a private branch are not guarded. - **Every ref-moving or commit-creating op (including all secret/protect policy ops) is guarded against an in-progress merge/pick/rebase** — finish - or abort the in-progress op first. + or abort the in-progress op first. A push into a repo with a stopped op is + refused for that repo's checked-out branch (other branches still accept it). - **A `sc work`/`ws` session is the only sanctioned ephemeral+persistent mix:** temp checkouts are removed on teardown; durable writes go only through the normal commit path. diff --git a/ROADMAP.md b/ROADMAP.md index 7513fc6c..63c7449a 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -726,13 +726,12 @@ scale-&-reach horizon): between the two leaves the scratch file on disk with no record, and a retried `sc commit` would take it. The opposite order would risk deleting a file the user recreated at that path; closing both needs a journal. -- **In-progress guard for the RECEIVING repo on push.** - `LocalTransport::update_ref` moves a branch tip with only a CAS/ff gate - and no in-progress check on the receiving side — a push into a repo - with a stopped merge/pick reproduces the P19-I1 discard shape via a - remote writer (the stopped rebase has its moved-tip backstop; - merge/pick completion does not). Pre-existing since P6/P12; flagged at - the P21 final review as the one gap outside the policy-op class. +- **In-progress guard for the RECEIVING repo on push — fixed.** + `LocalTransport::update_ref` (so `sc push` to a local path and the wire + `UpdateRef`) refuses to move the receiving repo's checked-out branch while a + merge, cherry-pick or rebase is stopped there (`MergeInProgress` & co.), so + the completing commit cannot silently drop a pushed commit. Other branches + are unaffected. - **Sparse-gate the conflict helper's non-marker writes (P24 boundary).** `Repo::materialize_conflict_state` gates conflict *markers* against the sparse spec but not its `to_encrypt`/sidecar write loops — so an diff --git a/crates/repo/src/transport.rs b/crates/repo/src/transport.rs index 74dd1b79..4fc84c00 100644 --- a/crates/repo/src/transport.rs +++ b/crates/repo/src/transport.rs @@ -561,6 +561,23 @@ impl Transport for LocalTransport { expected_old: Option<&ObjectId>, ) -> Result<()> { let _lock = RepoLock::acquire(&self.layout)?; + // A stopped merge, cherry-pick or rebase here is completed against the + // checked-out branch's tip: moving that tip from underneath it would + // make the completing commit silently drop what was pushed (the P19-I1 + // discard shape, reached by a remote writer). Refuse before any + // write; other branches are unaffected. Local ops guard themselves + // the same way (`MergeInProgress` & co.). + if crate::refs::current_branch(&self.layout).is_ok_and(|head| head == branch) { + if crate::merge_state::in_progress(&self.layout) { + return Err(Error::MergeInProgress); + } + if crate::pick_state::in_progress(&self.layout) { + return Err(Error::PickInProgress); + } + if crate::rebase_state::in_progress(&self.layout) { + return Err(Error::RebaseInProgress); + } + } // Revalidate inside the lock: the caller's fast-forward check ran // unlocked, so the ref may have moved since (two concurrent pushes). let current = crate::refs::read_branch_tip(&self.layout, branch)?; @@ -1096,4 +1113,90 @@ mod tests { assert_eq!(by_id_reply_cap(64 * MIB), 64 * MIB); assert_eq!(by_id_reply_cap(16 << 30), 512 * MIB); } + + /// A repo with a stopped merge, pick or rebase is completed against its + /// checked-out branch's tip. A push moving that tip would make the + /// completing commit drop the pushed work, so it is refused (other + /// branches are untouched), and allowed again once the op is over. + #[test] + fn update_ref_refuses_the_checked_out_branch_during_a_stopped_op() { + let root = std::env::temp_dir().join(format!("scl-xport-stopped-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + let repo = crate::repo::Repo::init(&root).unwrap(); + std::fs::write(root.join("x.txt"), b"a\nb\nc\n").unwrap(); + repo.commit("t", "base").unwrap(); + repo.branch("other").unwrap(); + std::fs::write(root.join("x.txt"), b"a\nMAIN\nc\n").unwrap(); + let main_tip = repo.commit("t", "main").unwrap(); + repo.switch("other").unwrap(); + std::fs::write(root.join("x.txt"), b"a\nOTHER\nc\n").unwrap(); + repo.commit("t", "other").unwrap(); + repo.switch("main").unwrap(); + assert!(repo.merge("other", "t").is_err(), "setup: a stopped merge"); + assert!(repo.merge_in_progress()); + // The pushed-to repo's own process has exited (a Repo handle holds the + // single-writer lock the transport needs). + let layout = repo.layout().clone(); + drop(repo); + + let t = LocalTransport::open(&root).unwrap(); + let pushed = Object::blob(b"pushed".to_vec()).id(); + // The checked-out branch: refused, and nothing moved. + assert!(matches!( + t.update_ref("main", &pushed, Some(&main_tip)), + Err(Error::MergeInProgress) + )); + assert_eq!( + crate::refs::read_branch_tip(&layout, "main").unwrap(), + Some(main_tip) + ); + // A branch that is not checked out is not in the way. + t.update_ref("elsewhere", &pushed, None).unwrap(); + assert_eq!( + crate::refs::read_branch_tip(&layout, "elsewhere").unwrap(), + Some(pushed) + ); + // Once the merge is over the push lands. + drop(t); + let repo = crate::repo::Repo::open(&root).unwrap(); + repo.merge_abort().unwrap(); + drop(repo); + let t = LocalTransport::open(&root).unwrap(); + t.update_ref("main", &pushed, Some(&main_tip)).unwrap(); + drop(t); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } + + /// The same receiving-side refusal for a stopped cherry-pick. + #[test] + fn update_ref_refuses_the_checked_out_branch_during_a_stopped_pick() { + let root = std::env::temp_dir().join(format!("scl-xport-pick-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + let repo = crate::repo::Repo::init(&root).unwrap(); + std::fs::write(root.join("x.txt"), b"a\nb\nc\n").unwrap(); + repo.commit("t", "base").unwrap(); + repo.branch("other").unwrap(); + std::fs::write(root.join("x.txt"), b"a\nMAIN\nc\n").unwrap(); + let main_tip = repo.commit("t", "main").unwrap(); + repo.switch("other").unwrap(); + std::fs::write(root.join("x.txt"), b"a\nOTHER\nc\n").unwrap(); + repo.commit("t", "other").unwrap(); + repo.switch("main").unwrap(); + assert!(repo.cherry_pick("other", "t", None, None).is_err()); + assert!(repo.pick_in_progress()); + drop(repo); + + let t = LocalTransport::open(&root).unwrap(); + let pushed = Object::blob(b"pushed".to_vec()).id(); + assert!(matches!( + t.update_ref("main", &pushed, Some(&main_tip)), + Err(Error::PickInProgress) + )); + drop(t); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } } From 7944cdf712b8df7468148de10691809ccd54f321 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 14:03:21 +0300 Subject: [PATCH 2/2] fix(repo): keep UpdateRef idempotent and fail closed on an unreadable HEAD during a stopped op Codex: a retry of a push that already landed (a lost response) must still succeed regardless of a stopped op, so the guard now runs after the already-at-that-id check; and HEAD is read only when an op is stopped, with a read failure then propagating instead of silently skipping the guard. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/transport.rs | 53 ++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/crates/repo/src/transport.rs b/crates/repo/src/transport.rs index 4fc84c00..c2616bc9 100644 --- a/crates/repo/src/transport.rs +++ b/crates/repo/src/transport.rs @@ -561,29 +561,33 @@ impl Transport for LocalTransport { expected_old: Option<&ObjectId>, ) -> Result<()> { let _lock = RepoLock::acquire(&self.layout)?; + // Revalidate inside the lock: the caller's fast-forward check ran + // unlocked, so the ref may have moved since (two concurrent pushes). + let current = crate::refs::read_branch_tip(&self.layout, branch)?; + if current.as_ref() == Some(id) { + return Ok(()); // already there — idempotent, nothing moves + } // A stopped merge, cherry-pick or rebase here is completed against the // checked-out branch's tip: moving that tip from underneath it would // make the completing commit silently drop what was pushed (the P19-I1 // discard shape, reached by a remote writer). Refuse before any - // write; other branches are unaffected. Local ops guard themselves - // the same way (`MergeInProgress` & co.). - if crate::refs::current_branch(&self.layout).is_ok_and(|head| head == branch) { - if crate::merge_state::in_progress(&self.layout) { - return Err(Error::MergeInProgress); - } - if crate::pick_state::in_progress(&self.layout) { - return Err(Error::PickInProgress); - } - if crate::rebase_state::in_progress(&self.layout) { - return Err(Error::RebaseInProgress); + // write; other branches are unaffected. HEAD is read only when an op + // is stopped, and a failure to read it then fails closed: the op's + // state exists, so a push must not guess which branch it belongs to. + let stopped = if crate::merge_state::in_progress(&self.layout) { + Some(Error::MergeInProgress) + } else if crate::pick_state::in_progress(&self.layout) { + Some(Error::PickInProgress) + } else if crate::rebase_state::in_progress(&self.layout) { + Some(Error::RebaseInProgress) + } else { + None + }; + if let Some(err) = stopped { + if crate::refs::current_branch(&self.layout)? == branch { + return Err(err); } } - // Revalidate inside the lock: the caller's fast-forward check ran - // unlocked, so the ref may have moved since (two concurrent pushes). - let current = crate::refs::read_branch_tip(&self.layout, branch)?; - if current.as_ref() == Some(id) { - return Ok(()); // already there — idempotent - } if current.as_ref() != expected_old { return Err(Error::NonFastForward); } @@ -1151,6 +1155,21 @@ mod tests { crate::refs::read_branch_tip(&layout, "main").unwrap(), Some(main_tip) ); + // A retry of a push that already landed moves nothing: still accepted. + t.update_ref("main", &main_tip, None).unwrap(); + // An unreadable HEAD with a stopped op fails closed, not open. + let head_path = layout.head_path(); + let head_text = std::fs::read(&head_path).unwrap(); + std::fs::remove_file(&head_path).unwrap(); + assert!( + t.update_ref("main", &pushed, Some(&main_tip)).is_err(), + "a push must not guess the checked-out branch while an op is stopped" + ); + assert_eq!( + crate::refs::read_branch_tip(&layout, "main").unwrap(), + Some(main_tip) + ); + std::fs::write(&head_path, head_text).unwrap(); // A branch that is not checked out is not in the way. t.update_ref("elsewhere", &pushed, None).unwrap(); assert_eq!(