From 02054737242d3c026e7e36a3c1ad90544ef92a7d Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 14:02:01 +0300 Subject: [PATCH 1/8] test(repo): failing repro for sparse merge-in to a private branch (#240) A conflicted merge of a public branch into a private one under a sparse spec resurrects a path the public side deleted outside the view, and the stop writes out-of-view files. Fails until the fix commit. --- crates/repo/src/lib.rs | 2 + crates/repo/src/private_sparse.rs | 134 ++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 crates/repo/src/private_sparse.rs diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index 448e5dee..35b8b7b3 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -28,6 +28,8 @@ mod partial_sessions; mod partial_walks; pub mod pick_state; pub mod private; +#[cfg(test)] +mod private_sparse; pub mod promisor; pub mod protect; pub mod protect_ops; diff --git a/crates/repo/src/private_sparse.rs b/crates/repo/src/private_sparse.rs new file mode 100644 index 00000000..1ba0895b --- /dev/null +++ b/crates/repo/src/private_sparse.rs @@ -0,0 +1,134 @@ +//! A conflicted merge of a public branch into a private branch under a sparse +//! spec must decide out-of-view paths by the merge, not by the prior private +//! tip (#240): a path the public side deleted stays deleted, its clean adds and +//! modifies land, ours-only private edits survive, and the stop writes nothing +//! outside the view. + +use std::collections::BTreeMap; +use std::path::Path; + +use scl_crypto::SecretKey; + +use crate::error::Error; +use crate::repo::Repo; + +fn put(root: &Path, rel: &str, content: &str) { + let p = root.join(rel); + std::fs::create_dir_all(p.parent().unwrap()).unwrap(); + std::fs::write(p, content).unwrap(); +} + +fn docs(root: &Path) -> BTreeMap { + let mut out = BTreeMap::new(); + if let Ok(rd) = std::fs::read_dir(root.join("docs")) { + for e in rd { + let e = e.unwrap(); + out.insert( + e.file_name().to_string_lossy().into_owned(), + std::fs::read_to_string(e.path()).unwrap(), + ); + } + } + out +} + +/// `main` holds src/lib.rs plus docs/{old,mod,ours}.txt; private `fix` edits +/// src/lib.rs and docs/ours.txt; `main` then edits src/lib.rs, deletes +/// docs/old.txt, edits docs/mod.txt and adds docs/new.txt. With `view` set, +/// the sparse spec `src/` is in force on `main` before switching to `fix`, +/// and `conflict_in_docs` also makes `fix` edit docs/mod.txt (a conflict). +struct Fixture { + root: std::path::PathBuf, + repo: Repo, + sk: SecretKey, +} + +fn fixture(tag: &str, view: bool, conflict_in_docs: bool) -> Fixture { + let root = std::env::temp_dir().join(format!("scl-priv-sparse-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + let repo = Repo::init(&root).unwrap(); + put(&root, "src/lib.rs", "base\n"); + put(&root, "docs/old.txt", "old\n"); + put(&root, "docs/mod.txt", "mod-base\n"); + put(&root, "docs/ours.txt", "ours-base\n"); + repo.commit("t", "base").unwrap(); + let (sk, _pk) = scl_crypto::generate_keypair(); + repo.branch_private("fix", &sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&sk)).unwrap(); + put(&root, "src/lib.rs", "private\n"); + put(&root, "docs/ours.txt", "ours-edit\n"); + if conflict_in_docs { + put(&root, "docs/mod.txt", "mod-private\n"); + } + repo.commit_private("alice", "private work", Some(&sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&sk)).unwrap(); + put(&root, "src/lib.rs", "public\n"); + std::fs::remove_file(root.join("docs/old.txt")).unwrap(); + put(&root, "docs/mod.txt", "mod-public\n"); + put(&root, "docs/new.txt", "new-public\n"); + repo.commit("t", "main moves").unwrap(); + if view { + repo.set_sparse(&["src/".to_string()], Some(&sk)).unwrap(); + } + repo.switch_with_identity("fix", Some(&sk)).unwrap(); + Fixture { root, repo, sk } +} + +/// Stop the merge, resolve `src/lib.rs`, complete, widen to the whole tree +/// and return the resulting `docs/` listing. +fn run(f: &Fixture, view: bool) -> BTreeMap { + let sk = Some(&f.sk); + assert!(matches!( + f.repo.merge_into_private("main", "alice", sk), + Err(Error::MergeConflicts(1)) + )); + if view { + assert!(docs(&f.root).is_empty(), "the stop wrote outside the view"); + } + put(&f.root, "src/lib.rs", "resolved\n"); + f.repo.commit_private("alice", "resolve", sk).unwrap(); + if view { + assert!(docs(&f.root).is_empty(), "completion widened the view"); + f.repo.switch_with_identity("main", sk).unwrap(); + f.repo + .set_sparse(&["src/".to_string(), "docs/".to_string()], sk) + .unwrap(); + f.repo.switch_with_identity("fix", sk).unwrap(); + } + docs(&f.root) +} + +#[test] +fn sparse_merge_into_private_equals_the_run_without_sparse() { + let full = fixture("full", false, false); + let want = run(&full, false); + let sparse = fixture("sparse", true, false); + let got = run(&sparse, true); + assert!(!got.contains_key("old.txt"), "public deletion resurrected"); + assert_eq!(got["mod.txt"], "mod-public\n"); + assert_eq!(got["new.txt"], "new-public\n"); + assert_eq!(got["ours.txt"], "ours-edit\n", "ours-only edit lost"); + assert_eq!(got, want); + std::fs::remove_dir_all(&full.root).unwrap(); + std::fs::remove_dir_all(&sparse.root).unwrap(); +} + +#[test] +fn an_out_of_view_conflict_is_refused_before_any_write() { + let f = fixture("refuse", true, true); + let (_, before, _) = f.repo.head_private().unwrap().unwrap(); + let err = f + .repo + .merge_into_private("main", "alice", Some(&f.sk)) + .unwrap_err(); + assert!( + err.to_string().contains("sc sparse set"), + "hint widens the spec: {err}" + ); + assert!(!crate::merge_state::in_progress(f.repo.layout())); + assert!(docs(&f.root).is_empty()); + assert_eq!(f.repo.head_private().unwrap().unwrap().1, before); + std::fs::remove_dir_all(&f.root).unwrap(); +} From 557a16c90895c7740562541f14b19d62849baf87 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 14:15:24 +0300 Subject: [PATCH 2/8] fix(repo): decide out-of-view paths by the merge when a public branch is merged into a private one under sparse (#240) A conflicted merge-in to a private branch under a sparse spec completed through commit_private, which carried every out-of-view path from the prior private tip. A path the public side had cleanly deleted came back, and its clean adds and modifies were lost. The conflict stop also wrote out-of-view files, so the checkout was wider than advertised. commit_private now re-derives the merge outcome (the same three-way the stop ran) when a sparse spec is in force and carries out-of-view and protected-skipped paths from it. A decided root is not recorded: an inner tree is plaintext about the private tree and .sc/MERGE_* is unsealed. The stop lays out in-view files only and refuses an out-of-view conflict before any write, with the existing `sc sparse set` hint. Cherry-pick and rebase onto a private branch are already refused; a test pins it. Docs: ADR-0034 refinement 6, ADR-0044, CLAUDE.md, ROADMAP.md. --- CLAUDE.md | 9 +- ROADMAP.md | 3 +- crates/repo/src/private.rs | 161 +++++++++++++++------ crates/repo/src/private_sparse.rs | 12 ++ docs/adr/0034-sparse-checkouts.md | 9 +- docs/adr/0044-per-branch-access-control.md | 39 ++++- 6 files changed, 181 insertions(+), 52 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index c83ffbf2..56c33c36 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -225,7 +225,11 @@ transport-adjacent. The rest, imperatively: attach`, `sign`, creating a child branch) refuses on a private branch; and integrating a private branch INTO a public one (merge/cherry-pick/rebase from it, or git export/push) is refused everywhere except `sc branch - publish`. Merging a public branch IN is allowed (keeps an embargo current). + publish`. Merging a public branch IN is allowed (keeps an embargo current); + under a sparse spec set on the public branch, its conflict stop lays out + in-view files only, refuses an out-of-view conflict before any write, and + completion re-derives the merge outcome for out-of-view paths instead of + carrying the prior tip (#240; no decided root, it would be unsealed). `sc branch revoke` rotates the KEK but a recipient who already fetched keeps the old manifest (rotation ≠ erasure). **Keep identity files outside the working tree** — a key committed under a private branch is sealed in and @@ -240,7 +244,8 @@ transport-adjacent. The rest, imperatively: environment (they arrive on fd N as NUL-terminated `NAME=VALUE` records, `SC_SECRETS_FD` names it): hygiene against env dumps and inheritance, still not isolation. `sc ws run` and `sc work --with-secrets` use the environment. -- **Sparse is checkout-only**, and a stopped merge/pick/rebase writes no +- **Sparse is checkout-only** (a spec set on a public branch still applies on a + private one, where `sparse set` is refused), and a stopped merge/pick/rebase writes no conflict marker or sealing-input plaintext outside the view: an out-of-sparse conflicted path or sealing input refuses with a `sc sparse set` hint before any write (ADR-0034, #210; from `rebase --continue` the hint says to abort diff --git a/ROADMAP.md b/ROADMAP.md index f486f7f2..fbb4224a 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -542,7 +542,8 @@ across every phase. manifest while snapshots, trees, paths, messages, and blobs introduced on the branch remain individually sealed under a branch KEK. Grant is O(1), revoke rotates the KEK, and publish atomically replays scanner-gated public history. - (ADR-0044.) + A conflicted merge of a public branch into a private one under a sparse spec honors the view and decides + out-of-view paths by the merge, not the prior tip (#240). (ADR-0044.) - **Phase 35 — Native desktop browser.** A bundled Tauri v2 application opens `.sc` repositories through the Rust crates and renders local/remote refs, the all-parent snapshot DAG, provenance, public files, and first-parent diffs. diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index b32f977b..43e0aaa2 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -652,6 +652,7 @@ impl Repo { let ctx = self.open_private(&branch, mid, manifest, identity)?; let sk = identity.expect("open_private succeeded"); let merge_head = crate::merge_state::read_merge_head(&self.layout)?; + let sparse = self.sparse_spec()?; let store_arc = self.vfs.store(); let mut store = store_arc.lock().unwrap(); @@ -660,6 +661,10 @@ impl Repo { // Policy + secrets: tip's, or the union with theirs on merge completion // (same discipline as `snapshot_files`' merge-completion arm). + // Under a sparse spec, a merge completion also re-derives the merge's + // outcome for the paths it cannot read from disk (see below). + let mut decided: Option> = None; + let mut decided_blobs: BTreeMap> = BTreeMap::new(); let (protection, secrets) = match merge_head { None => (prev_snap.protection.clone(), prev_snap.secrets.clone()), Some(mh) => { @@ -676,9 +681,17 @@ impl Repo { let base = self .merge_base_private(&mut store, &ctx, inner_tip, mh)? .ok_or(Error::NoCommonAncestor)?; - let base_secrets = self.get_inner_snapshot(&mut store, &ctx, &base)?.secrets; + let base_snap = self.get_inner_snapshot(&mut store, &ctx, &base)?; + if !sparse.is_full() { + let fm = self.private_three_way( + &mut store, &ctx, &prev_snap, &theirs, &base_snap, identity, + )?; + let (entries, blobs, _) = decide_merged(&fm); + decided = Some(entries); + decided_blobs = blobs; + } let secs = crate::merge::merge_secrets( - &base_secrets, + &base_snap.secrets, &prev_snap.secrets, &theirs.secrets, )?; @@ -689,7 +702,6 @@ impl Repo { let prior = self.inner_tree_files(&mut store, &ctx, prev_snap.root)?; let tracked: BTreeSet = prior.keys().cloned().collect(); let files = worktree::read_worktree(&self.layout, &tracked)?; - let sparse = self.sparse_spec()?; // Scanner gate on the plaintext partition (protected-prefix files are // sealed at publish, exempt here exactly as in a public commit). @@ -735,14 +747,23 @@ impl Repo { } } } - // Carry prior paths that are expected-absent: outside sparse, or - // PROTECTED and skipped on disk (never silently drop protected content). - for (path, e) in &prior { + // Carry paths that are expected-absent: outside sparse, or PROTECTED + // and skipped on disk (never silently drop protected content). On a + // merge completion under a sparse spec the source is the merge's + // decided outcome, not the prior tip, so a path the public side + // deleted stays deleted and its clean adds and modifies land (#240, + // same rule as the public path, #238). It is re-derived rather than + // recorded: a decided tree cannot sit in unsealed `.sc/` state. + let source = decided.as_ref().unwrap_or(&prior); + for (path, e) in source { if entries.contains_key(path) || disk_paths.contains(path) { continue; } if !sparse.matches(path) || e.2 & PROTECTED != 0 { entries.insert(path.clone(), *e); + if let Some(bytes) = decided_blobs.get(&e.0) { + new_blobs.entry(e.0).or_insert_with(|| bytes.clone()); + } } } @@ -1265,18 +1286,12 @@ impl Repo { .ok_or(Error::NoCommonAncestor)?; let base_snap = self.get_inner_snapshot(&mut store, &ctx, &base)?; - // Stage the three tree closures in a RAM-only store and run the - // existing three-way. Inner objects decode to plaintext values whose - // ids are exactly the inner ids the trees reference. - let mut ram = Store::with_budget(usize::MAX / 2); - self.copy_closure_inner(&mut store, &ctx, ours_snap.root, &mut ram)?; - copy_closure_public(&mut store, theirs_snap.root, &mut ram)?; - copy_closure_public(&mut store, base_snap.root, &mut ram)?; - let fm = crate::merge::three_way_files( - &mut ram, - Some((base_snap.root, &base_snap.protection)), - (ours_snap.root, &ours_snap.protection), - (theirs_snap.root, &theirs_snap.protection), + let fm = self.private_three_way( + &mut store, + &ctx, + &ours_snap, + &theirs_snap, + &base_snap, identity, )?; let secrets = crate::merge::merge_secrets( @@ -1289,39 +1304,36 @@ impl Repo { &theirs_snap.protection.prefixes, ); - // Decide the merged file map for the inner world: - // - carried PROTECTED ciphertext keeps its id + wraps; - // - everything else (plain carries, diff3 outputs, decrypted - // protected merges) becomes a plain inner blob — publish re-seals - // protected prefixes into public form. - let mut entries: BTreeMap = BTreeMap::new(); - let mut new_blobs: BTreeMap> = BTreeMap::new(); - let mut wrapped: BTreeMap> = BTreeMap::new(); - for f in &fm.files { - if f.perms & PROTECTED != 0 && !f.needs_encrypt { - let id = Object::blob(f.bytes.clone()).id(); - if let Some(wks) = fm.wrapped_carry.get(&id) { - wrapped.insert(id, wks.clone()); - } - entries.insert(f.path.clone(), (id, f.mode, f.perms)); - } else { - let id = Object::blob(f.bytes.clone()).id(); - new_blobs.entry(id).or_insert_with(|| f.bytes.clone()); - entries.insert(f.path.clone(), (id, f.mode, 0)); - } - } + let (entries, new_blobs, wrapped) = decide_merged(&fm); if !fm.conflicts.is_empty() { - // Conflict state: everything lands on disk in plaintext (markers, - // sidecars, decrypted protected content — we hold the identity by - // construction). Never through the CAS. Completion is the next - // `commit_private`, which reads MERGE_HEAD. + // A conflict outside the sparse view has nowhere to show its + // markers: refuse before any write, like the public path (#220). + let sparse = self.sparse_spec()?; + let outside: Vec<&str> = fm + .conflicts + .iter() + .map(String::as_str) + .filter(|p| !sparse.matches(p)) + .collect(); + if !outside.is_empty() { + return Err(Error::InvalidArgument(format!( + "conflict in {} is outside your sparse checkout; switch to a public branch, \ + run `sc sparse set` to include it, then retry", + outside.join(", ") + ))); + } + // Conflict state: the IN-VIEW files land on disk in plaintext + // (markers, sidecars, decrypted protected content — we hold the + // identity by construction). Never through the CAS. Out-of-view + // paths are not laid out; completion re-derives their merged + // value (`commit_private`, #240). It reads MERGE_HEAD. let prior_paths: BTreeSet = self .inner_tree_files(&mut store, &ctx, ours_snap.root)? .into_keys() .collect(); drop(store); - for f in &fm.files { + for f in fm.files.iter().filter(|f| sparse.matches(&f.path)) { let full = worktree::safe_join(&self.layout.root, &f.path)?; if let Some(parent) = full.parent() { std::fs::create_dir_all(parent)?; @@ -1348,6 +1360,9 @@ impl Repo { } let merged_paths: BTreeSet = fm.files.iter().map(|f| f.path.clone()).collect(); for p in prior_paths.difference(&merged_paths) { + if !sparse.matches(p) { + continue; + } let full = worktree::safe_join(&self.layout.root, p)?; let _ = std::fs::remove_file(full); } @@ -1434,6 +1449,34 @@ impl Repo { Ok((new_tip, skipped)) } + /// The three-way of a public `theirs` into the private `ours`, run against + /// a RAM-only store holding the three tree closures (decrypted inner + /// objects hash to their inner ids, so content addressing is consistent + /// and no plaintext ever touches disk). Deterministic in its inputs, so + /// `commit_private` re-derives a stopped merge's outcome instead of + /// persisting it (#240). + fn private_three_way( + &self, + store: &mut Store, + ctx: &PrivateCtx, + ours: &Snapshot, + theirs: &Snapshot, + base: &Snapshot, + identity: Option<&SecretKey>, + ) -> Result { + let mut ram = Store::with_budget(usize::MAX / 2); + self.copy_closure_inner(store, ctx, ours.root, &mut ram)?; + copy_closure_public(store, theirs.root, &mut ram)?; + copy_closure_public(store, base.root, &mut ram)?; + crate::merge::three_way_files( + &mut ram, + Some((base.root, &base.protection)), + (ours.root, &ours.protection), + (theirs.root, &theirs.protection), + identity, + ) + } + /// Copy an inner tree closure (trees + blobs, decoded plaintext values) /// into a RAM-only store. Ids are content addresses of the decoded /// values, so the copy is a faithful CAS fragment. @@ -1631,6 +1674,36 @@ impl Repo { } } +/// Decide the merged file map for the inner world: carried PROTECTED +/// ciphertext keeps its id + wraps; everything else (plain carries, diff3 +/// outputs, decrypted protected merges) becomes a plain inner blob — publish +/// re-seals protected prefixes into public form. +#[allow(clippy::type_complexity)] +fn decide_merged( + fm: &crate::merge::FileMerge, +) -> ( + BTreeMap, + BTreeMap>, + BTreeMap>, +) { + let mut entries = BTreeMap::new(); + let mut new_blobs = BTreeMap::new(); + let mut wrapped = BTreeMap::new(); + for f in &fm.files { + let id = Object::blob(f.bytes.clone()).id(); + if f.perms & PROTECTED != 0 && !f.needs_encrypt { + if let Some(wks) = fm.wrapped_carry.get(&id) { + wrapped.insert(id, wks.clone()); + } + entries.insert(f.path.clone(), (id, f.mode, f.perms)); + } else { + new_blobs.entry(id).or_insert_with(|| f.bytes.clone()); + entries.insert(f.path.clone(), (id, f.mode, 0)); + } + } + (entries, new_blobs, wrapped) +} + /// Copy a PUBLIC tree closure (trees + blobs) into the RAM store. fn copy_closure_public(store: &mut Store, root: ObjectId, ram: &mut Store) -> Result<()> { let mut stack = vec![root]; diff --git a/crates/repo/src/private_sparse.rs b/crates/repo/src/private_sparse.rs index 1ba0895b..6faa42b9 100644 --- a/crates/repo/src/private_sparse.rs +++ b/crates/repo/src/private_sparse.rs @@ -132,3 +132,15 @@ fn an_out_of_view_conflict_is_refused_before_any_write() { assert_eq!(f.repo.head_private().unwrap().unwrap().1, before); std::fs::remove_dir_all(&f.root).unwrap(); } + +#[test] +fn replay_onto_a_private_branch_stays_refused_under_sparse() { + let f = fixture("replay", true, false); + assert!(f + .repo + .cherry_pick("main", "alice", Some(&f.sk), None) + .is_err()); + assert!(f.repo.rebase("main", "alice", Some(&f.sk)).is_err()); + assert!(docs(&f.root).is_empty()); + std::fs::remove_dir_all(&f.root).unwrap(); +} diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index d44cdc53..12d5f339 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -170,9 +170,12 @@ every claim below is checked against the shipped code, not the plan. landed rather than discovered after. 6. **Completion carries from the decided root, not the parents (#238).** A stopped merge, cherry-pick or rebase on a public branch records a - decided root (`MERGE`/`PICK`/`REBASE_DECIDED_ROOT`); a merge into a - private branch writes none and completes through `commit_private`, which - this refinement does not cover. `snapshot_files` carries an absent + decided root (`MERGE`/`PICK`/`REBASE_DECIDED_ROOT`). A merge into a + private branch records none, because an inner tree must not sit in + unsealed `.sc/` state; `commit_private` re-derives the merge outcome under + a sparse spec instead and carries from that (#240, ADR-0044 "Merge-in + under a sparse spec"), and its stop lays out only in-view files and + refuses an out-of-view conflict before any write. `snapshot_files` carries an absent out-of-view or still-protected path from the decided root. Earlier builds also read the tip and merge head, so a path the other side had cleanly deleted (an out-of-view file, or a protected file) came back from the tip diff --git a/docs/adr/0044-per-branch-access-control.md b/docs/adr/0044-per-branch-access-control.md index 37bbf542..2e3b1e86 100644 --- a/docs/adr/0044-per-branch-access-control.md +++ b/docs/adr/0044-per-branch-access-control.md @@ -215,8 +215,11 @@ sealed trees — that inability is the feature. So: ref created). - Everywhere else, a private branch travels as its whole manifest closure or not at all. -- `sc sparse` on a private branch works normally for a recipient — sparse - is checkout-only (ADR-0034) and applies after decryption. +- `sc sparse set/disable` is refused while a private branch is checked out + (`refuse_on_private`), but a spec set on a public branch stays in force + when you switch onto one: sparse is checkout-only (ADR-0034) and applies + after decryption. Merging a public branch in and committing honor it (see + "Merge-in under a sparse spec" below). No P27 promisor/gap machinery grows manifest-aware cases. @@ -350,6 +353,38 @@ The decision shipped as designed; the concrete shape settled as follows. `recipient_opens_a_merged_branch_cloned_without_the_public_source` (a peer that never received the public branch still opens the merged private one). +## Merge-in under a sparse spec (#240) + +A conflicted `merge_into_private` completes through `commit_private`, which +carries the paths it cannot read from disk (outside the sparse view, or +protected and skipped). It used to carry them from the prior private tip, so +a path the public side had cleanly deleted outside the view came back, and +its clean adds and modifies there were lost. The public path fixed the same +class in #238 by completing from a recorded decided root (ADR-0034 +refinement 6). **A private merge records none**, on purpose: a decided inner +tree is plaintext about the private tree (paths, plaintext blob ids), and +`.sc/MERGE_*` is unsealed. Sealing it under the branch KEK would need a +second index outside the manifest, for a state that lives only until the +next commit. Instead: + +- The stop is a pure function of the ours inner tip, `MERGE_HEAD` and the + merge base, none of which can move while the merge is stopped (every + ref-moving op is guarded). `commit_private` therefore **re-derives** the + outcome (`private_three_way`, the same call the stop made) when a sparse + spec is in force, and carries out-of-view and protected-skipped paths from + it, never from the prior tip. A path absent from the outcome was decided + deleted. Nothing new is stored, so the opacity argument above is unchanged. + With no sparse spec nothing is re-derived and behavior is as before. +- The stop honors the view like the public path (#220): it lays out only + in-view files, and a conflict outside the view is refused before any write + (no merge state, no ref move) with a hint to switch to a public branch and + `sc sparse set` to include it. Earlier builds wrote every merged file, + including out-of-view ones, so the checkout was wider than advertised. + +Pinned by `private_sparse.rs`: the final tree equals the run without sparse +(deletion kept, add and modify landed, ours-only private edit kept), the stop +writes nothing outside the view, and an out-of-view conflict refuses cleanly. + ## Deferred (to ROADMAP.md) - `sc branch publish --squash` (hide intermediate history at publish). From a511eacfa201cd6d2924c2fb0b10b3ab5bbaedb4 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 14:40:09 +0300 Subject: [PATCH 3/8] fix(repo): re-derive the private merge outcome on every completion, not only under sparse (#240) Review found the same stale carry on a full checkout for a protected path the completer cannot read: a clean public change to it was reverted to the prior tip. Drop the sparse gate, add the regression test and a case where both sides edit an out-of-view file, and update the ADR text. Follow-ups for merge --abort and sidecars on a private stop are in #246. --- CLAUDE.md | 8 +-- crates/repo/src/private.rs | 61 +++++++++++++++++----- crates/repo/src/private_sparse.rs | 11 ++-- docs/adr/0034-sparse-checkouts.md | 4 +- docs/adr/0044-per-branch-access-control.md | 15 ++++-- 5 files changed, 73 insertions(+), 26 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 56c33c36..e773ade6 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -226,10 +226,10 @@ transport-adjacent. The rest, imperatively: integrating a private branch INTO a public one (merge/cherry-pick/rebase from it, or git export/push) is refused everywhere except `sc branch publish`. Merging a public branch IN is allowed (keeps an embargo current); - under a sparse spec set on the public branch, its conflict stop lays out - in-view files only, refuses an out-of-view conflict before any write, and - completion re-derives the merge outcome for out-of-view paths instead of - carrying the prior tip (#240; no decided root, it would be unsealed). + its completion re-derives the merge outcome for paths it cannot read from + disk (out of view, or protected and skipped) instead of carrying the prior + tip, and under a sparse spec set on the public branch its conflict stop lays + out in-view files only and refuses an out-of-view conflict before any write (#240; no decided root, it would be unsealed). `sc branch revoke` rotates the KEK but a recipient who already fetched keeps the old manifest (rotation ≠ erasure). **Keep identity files outside the working tree** — a key committed under a private branch is sealed in and diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index 43e0aaa2..58c52dd0 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -661,8 +661,8 @@ impl Repo { // Policy + secrets: tip's, or the union with theirs on merge completion // (same discipline as `snapshot_files`' merge-completion arm). - // Under a sparse spec, a merge completion also re-derives the merge's - // outcome for the paths it cannot read from disk (see below). + // A merge completion also re-derives the merge's outcome for the paths + // it cannot read from disk (see below). let mut decided: Option> = None; let mut decided_blobs: BTreeMap> = BTreeMap::new(); let (protection, secrets) = match merge_head { @@ -682,14 +682,12 @@ impl Repo { .merge_base_private(&mut store, &ctx, inner_tip, mh)? .ok_or(Error::NoCommonAncestor)?; let base_snap = self.get_inner_snapshot(&mut store, &ctx, &base)?; - if !sparse.is_full() { - let fm = self.private_three_way( - &mut store, &ctx, &prev_snap, &theirs, &base_snap, identity, - )?; - let (entries, blobs, _) = decide_merged(&fm); - decided = Some(entries); - decided_blobs = blobs; - } + let fm = self.private_three_way( + &mut store, &ctx, &prev_snap, &theirs, &base_snap, identity, + )?; + let (entries, blobs, _) = decide_merged(&fm); + decided = Some(entries); + decided_blobs = blobs; let secs = crate::merge::merge_secrets( &base_snap.secrets, &prev_snap.secrets, @@ -749,8 +747,7 @@ impl Repo { } // Carry paths that are expected-absent: outside sparse, or PROTECTED // and skipped on disk (never silently drop protected content). On a - // merge completion under a sparse spec the source is the merge's - // decided outcome, not the prior tip, so a path the public side + // merge completion the source is the merge's decided outcome, not the prior tip, so a path the public side // deleted stays deleted and its clean adds and modifies land (#240, // same rule as the public path, #238). It is re-derived rather than // recorded: a decided tree cannot sit in unsealed `.sc/` state. @@ -2472,4 +2469,44 @@ mod tests { drop(repo); std::fs::remove_dir_all(&root).unwrap(); } + + /// A stopped merge-in must complete a protected path the completer cannot + /// read (skipped on disk) from the merge's outcome, not the prior tip, on + /// a full checkout too (#240 review): `main` rotated the file and the + /// private side never touched it. + #[test] + fn merge_completion_takes_an_unreadable_protected_path_from_the_merge() { + let (root, repo, (alice_sk, _), (bob_sk, bob_pk)) = setup("protmerge"); + repo.protect("vault/", std::slice::from_ref(&bob_pk), None) + .unwrap(); + write(&root, "vault/key.pem", "PEM-V1\n"); + repo.commit("tester", "add protected").unwrap(); + repo.branch_private("fix", &alice_sk, std::slice::from_ref(&bob_pk), &[]) + .unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "private\n"); + repo.commit_private("alice", "private work", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "public\n"); + write(&root, "vault/key.pem", "PEM-V2\n"); + repo.commit("tester", "main moves").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + assert!(matches!( + repo.merge_into_private("main", "alice", Some(&alice_sk)), + Err(Error::MergeConflicts(1)) + )); + write(&root, "src/lib.rs", "resolved\n"); + repo.commit_private("alice", "resolve", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + repo.switch_with_identity("fix", Some(&bob_sk)).unwrap(); + assert_eq!( + std::fs::read_to_string(root.join("vault/key.pem")).unwrap(), + "PEM-V2\n", + "the public rotation was reverted by the stale carry" + ); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } } diff --git a/crates/repo/src/private_sparse.rs b/crates/repo/src/private_sparse.rs index 6faa42b9..7ddb1d12 100644 --- a/crates/repo/src/private_sparse.rs +++ b/crates/repo/src/private_sparse.rs @@ -32,9 +32,9 @@ fn docs(root: &Path) -> BTreeMap { out } -/// `main` holds src/lib.rs plus docs/{old,mod,ours}.txt; private `fix` edits -/// src/lib.rs and docs/ours.txt; `main` then edits src/lib.rs, deletes -/// docs/old.txt, edits docs/mod.txt and adds docs/new.txt. With `view` set, +/// `main` holds src/lib.rs plus docs/{old,mod,ours,lines}.txt; private `fix` edits +/// src/lib.rs, docs/ours.txt and the first line of docs/lines.txt; `main` then edits src/lib.rs, deletes +/// docs/old.txt, edits docs/mod.txt and adds docs/new.txt and edits the last line of docs/lines.txt. With `view` set, /// the sparse spec `src/` is in force on `main` before switching to `fix`, /// and `conflict_in_docs` also makes `fix` edit docs/mod.txt (a conflict). struct Fixture { @@ -52,12 +52,14 @@ fn fixture(tag: &str, view: bool, conflict_in_docs: bool) -> Fixture { put(&root, "docs/old.txt", "old\n"); put(&root, "docs/mod.txt", "mod-base\n"); put(&root, "docs/ours.txt", "ours-base\n"); + put(&root, "docs/lines.txt", "a\nb\nc\nd\ne\n"); repo.commit("t", "base").unwrap(); let (sk, _pk) = scl_crypto::generate_keypair(); repo.branch_private("fix", &sk, &[], &[]).unwrap(); repo.switch_with_identity("fix", Some(&sk)).unwrap(); put(&root, "src/lib.rs", "private\n"); put(&root, "docs/ours.txt", "ours-edit\n"); + put(&root, "docs/lines.txt", "A\nb\nc\nd\ne\n"); if conflict_in_docs { put(&root, "docs/mod.txt", "mod-private\n"); } @@ -68,6 +70,7 @@ fn fixture(tag: &str, view: bool, conflict_in_docs: bool) -> Fixture { std::fs::remove_file(root.join("docs/old.txt")).unwrap(); put(&root, "docs/mod.txt", "mod-public\n"); put(&root, "docs/new.txt", "new-public\n"); + put(&root, "docs/lines.txt", "a\nb\nc\nd\nE\n"); repo.commit("t", "main moves").unwrap(); if view { repo.set_sparse(&["src/".to_string()], Some(&sk)).unwrap(); @@ -110,6 +113,8 @@ fn sparse_merge_into_private_equals_the_run_without_sparse() { assert_eq!(got["mod.txt"], "mod-public\n"); assert_eq!(got["new.txt"], "new-public\n"); assert_eq!(got["ours.txt"], "ours-edit\n", "ours-only edit lost"); + // Both sides edited it, out of view: the merged blob is new and sealed. + assert_eq!(got["lines.txt"], "A\nb\nc\nd\nE\n"); assert_eq!(got, want); std::fs::remove_dir_all(&full.root).unwrap(); std::fs::remove_dir_all(&sparse.root).unwrap(); diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 12d5f339..80aff792 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -172,8 +172,8 @@ every claim below is checked against the shipped code, not the plan. A stopped merge, cherry-pick or rebase on a public branch records a decided root (`MERGE`/`PICK`/`REBASE_DECIDED_ROOT`). A merge into a private branch records none, because an inner tree must not sit in - unsealed `.sc/` state; `commit_private` re-derives the merge outcome under - a sparse spec instead and carries from that (#240, ADR-0044 "Merge-in + unsealed `.sc/` state; `commit_private` re-derives the merge outcome + instead and carries from that (#240, ADR-0044 "Merge-in under a sparse spec"), and its stop lays out only in-view files and refuses an out-of-view conflict before any write. `snapshot_files` carries an absent out-of-view or still-protected path from the decided root. Earlier builds diff --git a/docs/adr/0044-per-branch-access-control.md b/docs/adr/0044-per-branch-access-control.md index 2e3b1e86..78c87a62 100644 --- a/docs/adr/0044-per-branch-access-control.md +++ b/docs/adr/0044-per-branch-access-control.md @@ -370,11 +370,15 @@ next commit. Instead: - The stop is a pure function of the ours inner tip, `MERGE_HEAD` and the merge base, none of which can move while the merge is stopped (every ref-moving op is guarded). `commit_private` therefore **re-derives** the - outcome (`private_three_way`, the same call the stop made) when a sparse - spec is in force, and carries out-of-view and protected-skipped paths from - it, never from the prior tip. A path absent from the outcome was decided + outcome (`private_three_way`, the same call the stop made) on every merge + completion, and carries out-of-view and protected-skipped paths from it, + never from the prior tip. A path absent from the outcome was decided deleted. Nothing new is stored, so the opacity argument above is unchanged. - With no sparse spec nothing is re-derived and behavior is as before. + It is not limited to sparse: on a full checkout a protected path the + completer cannot read (skipped on disk) was carried from the prior tip too, + silently reverting a clean public change to it. The re-derivation runs with + the completing identity, so a recipient without access to a protected path + both sides changed fails the completion rather than altering the outcome. - The stop honors the view like the public path (#220): it lays out only in-view files, and a conflict outside the view is refused before any write (no merge state, no ref move) with a hint to switch to a public branch and @@ -383,7 +387,8 @@ next commit. Instead: Pinned by `private_sparse.rs`: the final tree equals the run without sparse (deletion kept, add and modify landed, ours-only private edit kept), the stop -writes nothing outside the view, and an out-of-view conflict refuses cleanly. +writes nothing outside the view, and an out-of-view conflict refuses cleanly; +and by `private.rs` tests for the full-checkout protected-path case. ## Deferred (to ROADMAP.md) From bfa32ecbb9967160ac49b84d505759379673c4a8 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 14:53:05 +0300 Subject: [PATCH 4/8] test(repo): assert the fixture roots are gone (#240) --- crates/repo/src/private_sparse.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/repo/src/private_sparse.rs b/crates/repo/src/private_sparse.rs index 7ddb1d12..7b117efd 100644 --- a/crates/repo/src/private_sparse.rs +++ b/crates/repo/src/private_sparse.rs @@ -117,7 +117,9 @@ fn sparse_merge_into_private_equals_the_run_without_sparse() { assert_eq!(got["lines.txt"], "A\nb\nc\nd\nE\n"); assert_eq!(got, want); std::fs::remove_dir_all(&full.root).unwrap(); + assert!(!full.root.exists(), "test dir left behind"); std::fs::remove_dir_all(&sparse.root).unwrap(); + assert!(!sparse.root.exists(), "test dir left behind"); } #[test] @@ -136,6 +138,7 @@ fn an_out_of_view_conflict_is_refused_before_any_write() { assert!(docs(&f.root).is_empty()); assert_eq!(f.repo.head_private().unwrap().unwrap().1, before); std::fs::remove_dir_all(&f.root).unwrap(); + assert!(!f.root.exists(), "test dir left behind"); } #[test] @@ -148,4 +151,5 @@ fn replay_onto_a_private_branch_stays_refused_under_sparse() { assert!(f.repo.rebase("main", "alice", Some(&f.sk)).is_err()); assert!(docs(&f.root).is_empty()); std::fs::remove_dir_all(&f.root).unwrap(); + assert!(!f.root.exists(), "test dir left behind"); } From a139fdcf62a3bf10700b701a05cc5bebefe9b8a2 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 15:43:47 +0300 Subject: [PATCH 5/8] fix(repo): drop stale plaintext at a private stop and read decided paths past .scignore (#240) Review follow-ups: an undecryptable merged protected entry no longer leaves the old plaintext on disk to be recommitted; completion reads every re-derived path regardless of the private side's .scignore. Tests assert the resolved content, a protected path under sparse, and the specific refusal errors. ADR-0044 records the behavior change. --- crates/repo/src/private.rs | 132 ++++++++++++++++++++- crates/repo/src/private_sparse.rs | 17 ++- docs/adr/0044-per-branch-access-control.md | 24 ++-- 3 files changed, 158 insertions(+), 15 deletions(-) diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index 58c52dd0..f28bad86 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -698,7 +698,13 @@ impl Repo { }; let prior = self.inner_tree_files(&mut store, &ctx, prev_snap.root)?; - let tracked: BTreeSet = prior.keys().cloned().collect(); + let mut tracked: BTreeSet = prior.keys().cloned().collect(); + // A path the merge decided is part of the outcome, so an ignore rule + // on the private side must not hide the stop's write of it (as + // `with_decided_paths` does on the public path, ADR-0049 q19). + if let Some(d) = &decided { + tracked.extend(d.keys().cloned()); + } let files = worktree::read_worktree(&self.layout, &tracked)?; // Scanner gate on the plaintext partition (protected-prefix files are @@ -1345,7 +1351,13 @@ impl Repo { }; match crate::protect::decrypt_with(&f.bytes, &id, &[&prots], sk, &f.path) { Ok(pt) => worktree::write_file(&full, &pt[..], f.mode)?, - Err(_) => continue, // undecryptable: leave prior state + Err(_) => { + // Undecryptable by this recipient: drop the old + // plaintext so completion carries the merged entry + // instead of recommitting the stale one. + let _ = std::fs::remove_file(&full); + continue; + } } } else { worktree::write_file(&full, &f.bytes, f.mode)?; @@ -2476,7 +2488,17 @@ mod tests { /// private side never touched it. #[test] fn merge_completion_takes_an_unreadable_protected_path_from_the_merge() { - let (root, repo, (alice_sk, _), (bob_sk, bob_pk)) = setup("protmerge"); + unreadable_protected_path_case("protmerge", false); + } + + /// Same, with the protected prefix outside a sparse view. + #[test] + fn sparse_merge_completion_takes_an_unreadable_protected_path_from_the_merge() { + unreadable_protected_path_case("protmerge-sparse", true); + } + + fn unreadable_protected_path_case(tag: &str, sparse: bool) { + let (root, repo, (alice_sk, _), (bob_sk, bob_pk)) = setup(tag); repo.protect("vault/", std::slice::from_ref(&bob_pk), None) .unwrap(); write(&root, "vault/key.pem", "PEM-V1\n"); @@ -2491,6 +2513,10 @@ mod tests { write(&root, "src/lib.rs", "public\n"); write(&root, "vault/key.pem", "PEM-V2\n"); repo.commit("tester", "main moves").unwrap(); + if sparse { + repo.set_sparse(&["src/".to_string()], Some(&alice_sk)) + .unwrap(); + } repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); assert!(matches!( repo.merge_into_private("main", "alice", Some(&alice_sk)), @@ -2501,6 +2527,17 @@ mod tests { .unwrap(); repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); repo.switch_with_identity("fix", Some(&bob_sk)).unwrap(); + if sparse { + // The view still applies to bob; widen it to see the vault. + repo.switch_with_identity("main", Some(&bob_sk)).unwrap(); + repo.set_sparse(&["src/".to_string(), "vault/".to_string()], Some(&bob_sk)) + .unwrap(); + repo.switch_with_identity("fix", Some(&bob_sk)).unwrap(); + } + assert_eq!( + std::fs::read_to_string(root.join("src/lib.rs")).unwrap(), + "resolved\n" + ); assert_eq!( std::fs::read_to_string(root.join("vault/key.pem")).unwrap(), "PEM-V2\n", @@ -2509,4 +2546,93 @@ mod tests { drop(repo); std::fs::remove_dir_all(&root).unwrap(); } + + /// The public side revokes the completer from a protected prefix and + /// rotates the file: the stop cannot decrypt the new ciphertext, so the + /// old plaintext must not survive on disk to be recommitted (#240 review). + #[test] + fn merge_completion_does_not_recommit_a_revoked_protected_file() { + let (root, repo, (alice_sk, alice_pk), (bob_sk, bob_pk)) = setup("protrevoke"); + repo.protect("vault/", &[alice_pk.clone(), bob_pk.clone()], None) + .unwrap(); + write(&root, "vault/key.pem", "PEM-V1\n"); + repo.commit("tester", "add protected").unwrap(); + repo.branch_private("fix", &alice_sk, std::slice::from_ref(&bob_pk), &[]) + .unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "private\n"); + repo.commit_private("alice", "private work", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + repo.revoke("vault/", &alice_pk.recipient_id()).unwrap(); + write(&root, "vault/key.pem", "PEM-V2\n"); + write(&root, "src/lib.rs", "public\n"); + repo.commit("tester", "rotate").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + assert!(matches!( + repo.merge_into_private("main", "alice", Some(&alice_sk)), + Err(Error::MergeConflicts(1)) + )); + write(&root, "src/lib.rs", "resolved\n"); + repo.commit_private("alice", "resolve", Some(&alice_sk)) + .unwrap(); + assert_eq!( + std::fs::read_to_string(root.join("src/lib.rs")).unwrap(), + "resolved\n" + ); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + repo.switch_with_identity("fix", Some(&bob_sk)).unwrap(); + assert_eq!( + std::fs::read_to_string(root.join("vault/key.pem")).unwrap(), + "PEM-V2\n", + "the revoked key came back from the prior tip" + ); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } + + /// A path the public side adds in view must land even when the private + /// side's `.scignore` ignores it: completion reads the re-derived paths + /// regardless of ignore rules (#240 review, ADR-0049 q19). + #[test] + fn merge_completion_reads_a_decided_path_an_ignore_rule_hides() { + let (root, repo, (alice_sk, _), _bob) = setup("ignoredadd"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "private\n"); + write(&root, ".scignore", "*.log\n"); + repo.commit_private("alice", "private work", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "public\n"); + write(&root, "src/build.log", "from-main\n"); + repo.commit("tester", "main moves").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + assert!(matches!( + repo.merge_into_private("main", "alice", Some(&alice_sk)), + Err(Error::MergeConflicts(1)) + )); + write(&root, "src/lib.rs", "resolved\n"); + repo.commit_private("alice", "resolve", Some(&alice_sk)) + .unwrap(); + assert_eq!( + std::fs::read_to_string(root.join("src/build.log")).unwrap(), + "from-main\n" + ); + let (_, mid, manifest) = repo.head_private().unwrap().unwrap(); + let ctx = repo + .open_private("fix", mid, manifest, Some(&alice_sk)) + .unwrap(); + let store_arc = repo.vfs().store(); + let mut store = store_arc.lock().unwrap(); + let tip = ctx.inner_tip().unwrap(); + let snap = repo.get_inner_snapshot(&mut store, &ctx, &tip).unwrap(); + let files = repo.inner_tree_files(&mut store, &ctx, snap.root).unwrap(); + assert!(files.contains_key("src/build.log"), "public add dropped"); + drop(store); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } } diff --git a/crates/repo/src/private_sparse.rs b/crates/repo/src/private_sparse.rs index 7b117efd..94812ae6 100644 --- a/crates/repo/src/private_sparse.rs +++ b/crates/repo/src/private_sparse.rs @@ -92,6 +92,10 @@ fn run(f: &Fixture, view: bool) -> BTreeMap { } put(&f.root, "src/lib.rs", "resolved\n"); f.repo.commit_private("alice", "resolve", sk).unwrap(); + assert_eq!( + std::fs::read_to_string(f.root.join("src/lib.rs")).unwrap(), + "resolved\n" + ); if view { assert!(docs(&f.root).is_empty(), "completion widened the view"); f.repo.switch_with_identity("main", sk).unwrap(); @@ -144,11 +148,14 @@ fn an_out_of_view_conflict_is_refused_before_any_write() { #[test] fn replay_onto_a_private_branch_stays_refused_under_sparse() { let f = fixture("replay", true, false); - assert!(f - .repo - .cherry_pick("main", "alice", Some(&f.sk), None) - .is_err()); - assert!(f.repo.rebase("main", "alice", Some(&f.sk)).is_err()); + assert!(matches!( + f.repo.cherry_pick("main", "alice", Some(&f.sk), None), + Err(Error::PrivateUnsupported(_)) + )); + assert!(matches!( + f.repo.rebase("main", "alice", Some(&f.sk)), + Err(Error::PrivateUnsupported(_)) + )); assert!(docs(&f.root).is_empty()); std::fs::remove_dir_all(&f.root).unwrap(); assert!(!f.root.exists(), "test dir left behind"); diff --git a/docs/adr/0044-per-branch-access-control.md b/docs/adr/0044-per-branch-access-control.md index 78c87a62..3efcf661 100644 --- a/docs/adr/0044-per-branch-access-control.md +++ b/docs/adr/0044-per-branch-access-control.md @@ -361,11 +361,12 @@ protected and skipped). It used to carry them from the prior private tip, so a path the public side had cleanly deleted outside the view came back, and its clean adds and modifies there were lost. The public path fixed the same class in #238 by completing from a recorded decided root (ADR-0034 -refinement 6). **A private merge records none**, on purpose: a decided inner -tree is plaintext about the private tree (paths, plaintext blob ids), and -`.sc/MERGE_*` is unsealed. Sealing it under the branch KEK would need a -second index outside the manifest, for a state that lives only until the -next commit. Instead: +refinement 6). **A private merge records none**, on purpose: the root would +be only an id, but it names an inner tree, and that tree's objects (plaintext +paths and blob ids about the private tree) would have to be stored unsealed +next to the unsealed `.sc/MERGE_*`. Sealing them under the branch KEK would +need a second index outside the manifest, for a state that lives only until +the next commit. Instead: - The stop is a pure function of the ours inner tip, `MERGE_HEAD` and the merge base, none of which can move while the merge is stopped (every @@ -377,8 +378,17 @@ next commit. Instead: It is not limited to sparse: on a full checkout a protected path the completer cannot read (skipped on disk) was carried from the prior tip too, silently reverting a clean public change to it. The re-derivation runs with - the completing identity, so a recipient without access to a protected path - both sides changed fails the completion rather than altering the outcome. + the completing identity. **Behavior change:** a recipient who cannot + decrypt a protected path both sides changed can no longer finish another + recipient's stopped merge; it errors before any scan, seal or ref move, and + the merge stays stopped for someone who can. Where the merged entry is + unreadable to the completer (for example the public side revoked them and + rotated the file), the stop removes the stale plaintext it would otherwise + leave behind, so completion carries the merged entry, not the old one. + Paths the merge decided are also read from disk regardless of the private + side's `.scignore` (as `with_decided_paths` does publicly, ADR-0049 q19). + The completion's scanner gate covers only files read from disk; re-derived + content comes from already-scanned commits, and `branch publish` rescans. - The stop honors the view like the public path (#220): it lays out only in-view files, and a conflict outside the view is refused before any write (no merge state, no ref move) with a hint to switch to a public branch and From 11be901d90122043b1b9e80d4ce326ea3ad4d2d7 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 15:55:47 +0300 Subject: [PATCH 6/8] test(repo): assert the protected-merge fixture root is gone (#240) --- crates/repo/src/private.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index f28bad86..1e25f14d 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -2545,6 +2545,7 @@ mod tests { ); drop(repo); std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "test dir left behind"); } /// The public side revokes the completer from a protected prefix and From a991fdb5fe588a161e111174bd19784416ea05fd Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 16:26:17 +0300 Subject: [PATCH 7/8] fix(repo): refuse an untracked collision and fail on removal errors at a private stop (#240) Removing the old plaintext of a protected path the recipient can no longer read must only touch the tracked prior-tip copy. Any other file at the path is refused before any write, and a failed removal fails the stop before any other write or state change. --- crates/repo/src/private.rs | 180 ++++++++++++++++++--- docs/adr/0044-per-branch-access-control.md | 6 +- 2 files changed, 163 insertions(+), 23 deletions(-) diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index 1e25f14d..eebd75b6 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -1331,34 +1331,84 @@ impl Repo { // identity by construction). Never through the CAS. Out-of-view // paths are not laid out; completion re-derives their merged // value (`commit_private`, #240). It reads MERGE_HEAD. - let prior_paths: BTreeSet = self - .inner_tree_files(&mut store, &ctx, ours_snap.root)? - .into_keys() - .collect(); + let prior_entries = self.inner_tree_files(&mut store, &ctx, ours_snap.root)?; + let prior_paths: BTreeSet = prior_entries.keys().cloned().collect(); + // Carried protected ciphertext: decrypt for the resolver to see + // (completion re-seals it as needed). One this recipient cannot + // read has its old plaintext dropped, so completion carries the + // merged entry instead of recommitting the stale one. Decide all + // of it, and refuse a collision, before writing anything. + let prots = Protection { + prefixes: prefixes.clone(), + wrapped: fm.wrapped_carry.clone(), + }; + let mut plaintext: BTreeMap<&str, Option>> = BTreeMap::new(); + for f in fm.files.iter().filter(|f| sparse.matches(&f.path)) { + if f.perms & PROTECTED == 0 || f.needs_encrypt { + continue; + } + let id = Object::blob(f.bytes.clone()).id(); + if let Ok(pt) = crate::protect::decrypt_with(&f.bytes, &id, &[&prots], sk, &f.path) + { + plaintext.insert(&f.path, Some(pt.to_vec())); + continue; + } + let full = worktree::safe_join(&self.layout.root, &f.path)?; + let on_disk = match std::fs::read(&full) { + Ok(b) => Some(b), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => None, + Err(e) => return Err(e.into()), + }; + if let Some(disk) = on_disk { + // Only the tracked prior-tip plaintext is ours to drop. + let stale = match prior_entries.get(&f.path) { + Some((pid, _, perms)) if perms & PROTECTED != 0 => { + let ct = self.inner_blob(&mut store, &ctx, pid)?; + crate::protect::decrypt_with( + &ct, + pid, + &[&ours_snap.protection], + sk, + &f.path, + ) + .map(|pt| pt.as_slice() == disk.as_slice()) + .unwrap_or(false) + } + _ => false, + }; + if !stale { + return Err(Error::InvalidArgument(format!( + "{} exists and is not the tracked copy, but the merged version is \ + protected and you cannot read it; move it away, then retry", + f.path + ))); + } + } + plaintext.insert(&f.path, None); + } drop(store); + // Removals first: a failed one must fail the stop before any + // other write, or completion would commit the stale plaintext. + for (path, pt) in &plaintext { + if pt.is_none() { + let full = worktree::safe_join(&self.layout.root, path)?; + match std::fs::remove_file(&full) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), + } + } + } for f in fm.files.iter().filter(|f| sparse.matches(&f.path)) { let full = worktree::safe_join(&self.layout.root, &f.path)?; + if matches!(plaintext.get(f.path.as_str()), Some(None)) { + continue; + } if let Some(parent) = full.parent() { std::fs::create_dir_all(parent)?; } - if f.perms & PROTECTED != 0 && !f.needs_encrypt { - // Carried ciphertext: write the decrypted form for the - // resolver to see; completion re-seals it as needed. - let id = Object::blob(f.bytes.clone()).id(); - let prots = Protection { - prefixes: prefixes.clone(), - wrapped: fm.wrapped_carry.clone(), - }; - match crate::protect::decrypt_with(&f.bytes, &id, &[&prots], sk, &f.path) { - Ok(pt) => worktree::write_file(&full, &pt[..], f.mode)?, - Err(_) => { - // Undecryptable by this recipient: drop the old - // plaintext so completion carries the merged entry - // instead of recommitting the stale one. - let _ = std::fs::remove_file(&full); - continue; - } - } + if let Some(Some(pt)) = plaintext.get(f.path.as_str()) { + worktree::write_file(&full, &pt[..], f.mode)?; } else { worktree::write_file(&full, &f.bytes, f.mode)?; } @@ -2636,4 +2686,90 @@ mod tests { std::fs::remove_dir_all(&root).unwrap(); assert!(!root.exists()); } + + /// Scenario shared by the two stop-time protected tests: `vault/` is + /// protected to alice and bob, the private `fix` has both as recipients, + /// and `main` revokes alice, adds `vault/new.pem` (sealed to bob only) and + /// edits `src/lib.rs` so the merge stops on that one conflict. + fn revoked_addition_setup(tag: &str) -> (std::path::PathBuf, Repo, SecretKey) { + let (root, repo, (alice_sk, alice_pk), (_bob_sk, bob_pk)) = setup(tag); + repo.protect("vault/", &[alice_pk.clone(), bob_pk.clone()], None) + .unwrap(); + write(&root, "vault/key.pem", "PEM-V1\n"); + repo.commit("tester", "add protected").unwrap(); + repo.branch_private("fix", &alice_sk, std::slice::from_ref(&bob_pk), &[]) + .unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "private\n"); + repo.commit_private("alice", "private work", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + repo.revoke("vault/", &alice_pk.recipient_id()).unwrap(); + write(&root, "vault/new.pem", "PEM-NEW\n"); + write(&root, "src/lib.rs", "public\n"); + repo.commit("tester", "rotate").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + (root, repo, alice_sk) + } + + /// An untracked file at a path whose merged protected version the + /// recipient cannot read is the user's, not stale plaintext: the stop + /// refuses before any write and leaves it intact. + #[test] + fn private_stop_refuses_an_untracked_file_at_an_unreadable_protected_path() { + let (root, repo, alice_sk) = revoked_addition_setup("protuntracked"); + write(&root, "vault/new.pem", "MINE\n"); + let (_, before, _) = repo.head_private().unwrap().unwrap(); + let err = repo + .merge_into_private("main", "alice", Some(&alice_sk)) + .unwrap_err(); + assert!(err.to_string().contains("vault/new.pem"), "{err}"); + assert_eq!( + std::fs::read_to_string(root.join("vault/new.pem")).unwrap(), + "MINE\n" + ); + assert_eq!( + std::fs::read_to_string(root.join("src/lib.rs")).unwrap(), + "private\n", + "refusal wrote nothing" + ); + assert!(!crate::merge_state::in_progress(repo.layout())); + assert_eq!(repo.head_private().unwrap().unwrap().1, before); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } + + /// If the stale plaintext cannot be removed the stop fails before any + /// write or state change; it must not stop and let completion commit it. + #[cfg(unix)] + #[test] + fn private_stop_fails_when_stale_protected_plaintext_cannot_be_removed() { + use std::os::unix::fs::PermissionsExt; + let (root, repo, alice_sk) = revoked_addition_setup("protrmfail"); + // The tracked copy of vault/key.pem is stale once main rotates it. + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "vault/key.pem", "PEM-V2\n"); + repo.commit("tester", "rotate key").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + let (_, before, _) = repo.head_private().unwrap().unwrap(); + let dir = root.join("vault"); + std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o555)).unwrap(); + let can_write = std::fs::write(dir.join("probe"), b"x").is_ok(); + let res = repo.merge_into_private("main", "alice", Some(&alice_sk)); + std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap(); + if !can_write { + assert!(matches!(res, Err(Error::Io(_))), "{res:?}"); + assert!(!crate::merge_state::in_progress(repo.layout())); + assert_eq!(repo.head_private().unwrap().unwrap().1, before); + assert_eq!( + std::fs::read_to_string(root.join("src/lib.rs")).unwrap(), + "private\n", + "nothing else was written" + ); + } + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } } diff --git a/docs/adr/0044-per-branch-access-control.md b/docs/adr/0044-per-branch-access-control.md index 3efcf661..9f60c9a9 100644 --- a/docs/adr/0044-per-branch-access-control.md +++ b/docs/adr/0044-per-branch-access-control.md @@ -384,7 +384,11 @@ the next commit. Instead: the merge stays stopped for someone who can. Where the merged entry is unreadable to the completer (for example the public side revoked them and rotated the file), the stop removes the stale plaintext it would otherwise - leave behind, so completion carries the merged entry, not the old one. + leave behind, so completion carries the merged entry, not the old one. It + removes only a file that is byte-identical to the tracked prior-tip + plaintext; any other file at that path is the user's, and the stop refuses + naming the path before any write. Removals run first and a failed one fails + the stop before any other write or state change. Paths the merge decided are also read from disk regardless of the private side's `.scignore` (as `with_decided_paths` does publicly, ADR-0049 q19). The completion's scanner gate covers only files read from disk; re-derived From 0015c1bc792c6182a99f955f474ee7e7a280495c Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 16:49:59 +0300 Subject: [PATCH 8/8] fix(repo): treat a symlink at a stale protected path as the user's file (#240) --- crates/repo/src/private.rs | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index eebd75b6..974f3c25 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -1354,8 +1354,11 @@ impl Repo { continue; } let full = worktree::safe_join(&self.layout.root, &f.path)?; - let on_disk = match std::fs::read(&full) { - Ok(b) => Some(b), + // A symlink (even one whose target reads as the old bytes) + // is the user's file, never the checkout's copy. + let on_disk = match std::fs::symlink_metadata(&full) { + Ok(m) if m.file_type().is_file() => Some(Some(std::fs::read(&full)?)), + Ok(_) => Some(None), Err(e) if e.kind() == std::io::ErrorKind::NotFound => None, Err(e) => return Err(e.into()), }; @@ -1371,7 +1374,7 @@ impl Repo { sk, &f.path, ) - .map(|pt| pt.as_slice() == disk.as_slice()) + .map(|pt| Some(pt.as_slice()) == disk.as_deref()) .unwrap_or(false) } _ => false, @@ -2772,4 +2775,32 @@ mod tests { std::fs::remove_dir_all(&root).unwrap(); assert!(!root.exists()); } + + /// A symlink at an unreadable protected path is the user's file even when + /// its target reads as the old plaintext: the stop refuses, leaves it. + #[cfg(unix)] + #[test] + fn private_stop_refuses_a_symlink_at_an_unreadable_protected_path() { + let (root, repo, alice_sk) = revoked_addition_setup("protsymlink"); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "vault/key.pem", "PEM-V2\n"); + repo.commit("tester", "rotate key").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + // Tracked vault/key.pem replaced by a link whose target reads "PEM-V1". + write(&root, "elsewhere.txt", "PEM-V1\n"); + std::fs::remove_file(root.join("vault/key.pem")).unwrap(); + std::os::unix::fs::symlink("../elsewhere.txt", root.join("vault/key.pem")).unwrap(); + let err = repo + .merge_into_private("main", "alice", Some(&alice_sk)) + .unwrap_err(); + assert!(err.to_string().contains("vault/key.pem"), "{err}"); + assert!(std::fs::symlink_metadata(root.join("vault/key.pem")) + .unwrap() + .file_type() + .is_symlink()); + assert!(!crate::merge_state::in_progress(repo.layout())); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } }