diff --git a/CLAUDE.md b/CLAUDE.md index 7c22a353..95b479e4 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -171,7 +171,7 @@ transport-adjacent. The rest, imperatively: capability, and no DTO field capable of carrying protected ciphertext or identity material. - **Partial clones refuse `sc export`, `sparse disable`, an empty `sparse set` (and, on a non-lazy clone, one outside the filter), `sc rewrap`, - `sc grant`/`revoke`, creating, switching to or leaving a private branch, a merge, cherry-pick or rebase whose conflict lies outside the view or needs sealing outside it (no ref, state or working-tree change), `sc push` to a git remote, and + `sc grant`/`revoke`, creating, switching to or leaving a private branch, a merge, cherry-pick or rebase whose conflict lies outside the view or whose `.theirs` sidecar would lie outside the view (partial clones only: on a full clone the sidecar is exempt as it sits beside an in-view file) (no ref, state or working-tree change; on every clone, a conflict stop that needs sealing outside the view is refused the same way), `sc push` to a git remote, and `sc fetch` from any native remote but the promisor origin.** `sc merge`, `sc cherry-pick` and `sc rebase` work, including conflicts inside the view (P37c). `sc ws` and `sc work` work too (ADR-0050): workspaces and agents see the clone's view only, and harvest lands through the same merge. `sc backfill --all` converts to a genuine full clone and re-enables them. The filter records what a clone *asked for*, not what is present: after a fetch an in-filter object @@ -235,10 +235,13 @@ 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 has one disk-hygiene boundary: an in-sparse - conflict co-occurring with an out-of-sparse protected/plain clean change can - leave that out-of-sparse plaintext on disk until the next materializing - operation re-lays the tree (not data loss, not a new disclosure — ADR-0034). +- **Sparse is checkout-only**, 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 + first, since `sparse set` is refused mid-rebase). A binary conflict's + `.theirs` sidecar sits beside its in-view file and may not match a + file-exact spec; completion and abort remove it. - **`.sc/git-remotes//marks` carries `git_oid ↔ sc_id` identity — deleting it is destructive.** Deleting the sibling `mirror.git` is always safe (reconstructed on next use). Stale marks self-heal on push/export. diff --git a/ROADMAP.md b/ROADMAP.md index 932e1769..6523c057 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -732,16 +732,6 @@ scale-&-reach horizon): 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 - in-sparse conflict co-occurring with an out-of-sparse protected/I2 clean - change transiently writes that out-of-sparse plaintext to disk outside - the view, and it PERSISTS after completion until a later re-lay (only - abort cleans it). Not data loss (completion re-lands it in the CAS) and - not a disclosure (an authorized identity produced it), documented in - ADR-0034 — but the sparse view is briefly wider than advertised. Extend - the gate to those write loops. - **Sparse ergonomics (P24 final-review Minors) — shipped.** `sc sparse set` warns for each prefix that matches no tracked path at HEAD (the tree is still laid out, empty, and `sc sparse disable` is named as the way back); a bare diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 7833f80d..34eb722c 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -2820,9 +2820,10 @@ mod tests { /// A full clone with a sparse view: the tree tracks a protected path the /// view never materialized, and the user keeps an ignored local copy there. /// An in-view conflict together with a clean protected merge of that path - /// would write plaintext over it, so the stop refuses. + /// would write plaintext over it, so the stop refuses (since #210 on the + /// sparse gate, before the untracked-overwrite check is reached). #[test] - fn a_conflict_stop_does_not_treat_an_out_of_view_path_as_materialized() { + fn a_conflict_stop_refuses_an_out_of_view_sealing_input_and_keeps_the_local_copy() { let root = tmp_root("mg-sparse-untracked"); let repo = Repo::init(&root).unwrap(); let (alice_sk, alice_pk) = scl_crypto::generate_keypair(); @@ -2851,7 +2852,7 @@ mod tests { .merge_with_identity("work", "me", Some(&alice_sk)) .unwrap_err(); assert!( - matches!(&err, Error::InvalidArgument(m) if m.contains("overwrite an untracked")), + matches!(&err, Error::InvalidArgument(m) if m.contains("outside your sparse checkout")), "got {err:?}" ); assert!(!repo.merge_in_progress()); @@ -2863,6 +2864,184 @@ mod tests { std::fs::remove_dir_all(&root).ok(); } + /// `main` and `work` both edit `src/a.txt` (a conflict) and a line of the + /// protected `vault/s.txt` (a clean content merge needing the identity); + /// `work` also edits `src/b.txt` in a second commit. `main` is checked out + /// with the view narrowed to `src/`, so the merge's sealing input lies + /// outside it. + fn mixed_conflict_fixture(name: &str) -> (std::path::PathBuf, Repo, scl_crypto::SecretKey) { + let root = tmp_root(name); + let repo = Repo::init(&root).unwrap(); + let (sk, pk) = scl_crypto::generate_keypair(); + repo.protect("vault/", &[pk], None).unwrap(); + std::fs::create_dir_all(root.join("src")).unwrap(); + std::fs::create_dir_all(root.join("vault")).unwrap(); + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("src/b.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("vault/s.txt"), b"s1\ns2\ns3\ns4\ns5\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("src/b.txt"), b"OURS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("vault/s.txt"), b"OURS\ns2\ns3\ns4\ns5\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch_with_identity("work", Some(&sk)).unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + repo.commit("me", "work 1").unwrap(); + std::fs::write(root.join("src/b.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("vault/s.txt"), b"s1\ns2\ns3\ns4\nTHEIRS\n").unwrap(); + repo.commit("me", "work 2").unwrap(); + (root, repo, sk) + } + + fn assert_widen_refusal(err: &Error, path: &str) { + match err { + Error::InvalidArgument(m) => { + assert!(m.contains(path), "names the path: {m}"); + assert!(m.contains("outside your sparse checkout"), "{m}"); + } + other => panic!("expected the sparse refusal, got {other:?}"), + } + } + + /// The stop writes nothing: no file out of view, no bookkeeping, and the + /// in-view file keeps the user's content. + fn assert_nothing_written(root: &std::path::Path) { + assert!(!root.join("vault/s.txt").exists(), "nothing out of view"); + assert!(!root.join(".sc/CONFLICT_PLAINTEXT").exists()); + assert!(!root.join(".sc/CONFLICT_SIDECARS").exists()); + assert_eq!( + std::fs::read(root.join("src/a.txt")).unwrap(), + b"OURS\nl2\nl3\n", + "the in-view file is untouched" + ); + } + + /// A cherry-pick with an in-view conflict and an out-of-view protected + /// content merge refuses before any write, on a full clone. + #[test] + fn a_cherry_pick_with_an_out_of_view_sealing_input_refuses_before_any_write() { + let (root, repo, sk) = mixed_conflict_fixture("sparse-pick-vault"); + repo.switch_with_identity("main", Some(&sk)).unwrap(); + repo.set_sparse(&["src/".into()], Some(&sk)).unwrap(); + let before = repo.head_tip().unwrap(); + let err = repo.cherry_pick("work", "me", Some(&sk), None).unwrap_err(); + assert_widen_refusal(&err, "vault/s.txt"); + assert!(!repo.pick_in_progress()); + assert_eq!(repo.head_tip().unwrap(), before); + assert_nothing_written(&root); + drop(repo); + remove_root(&root); + } + + /// `sc sparse set` is refused while a rebase is stopped, so a refusal + /// reached from `--continue` must send the user to abort first. + #[test] + fn a_rebase_continue_refusal_points_at_abort_not_at_sparse_set() { + let (root, repo, sk) = mixed_conflict_fixture("sparse-rebase-vault"); + repo.set_sparse(&["src/".into()], Some(&sk)).unwrap(); + // Commit 1 only touches src/a.txt: an in-view conflict, no sealing. + assert!(matches!( + repo.rebase("main", "me", Some(&sk)).unwrap(), + RebaseResult::Stopped { .. } + )); + std::fs::write(root.join("src/a.txt"), b"RESOLVED\nl2\nl3\n").unwrap(); + // Commit 2 conflicts on src/b.txt and merges vault/s.txt out of view. + let err = repo.rebase_continue("me", Some(&sk)).unwrap_err(); + assert_widen_refusal(&err, "vault/s.txt"); + let Error::InvalidArgument(m) = &err else { + unreachable!() + }; + assert!(m.contains("sc rebase --abort"), "{m}"); + assert!(repo.rebase_in_progress(), "the rebase is still stopped"); + assert!(matches!( + repo.set_sparse(&["src/".into(), "vault/".into()], Some(&sk)), + Err(Error::RebaseInProgress) + )); + assert!(!root.join("vault/s.txt").exists(), "nothing out of view"); + repo.rebase_abort().unwrap(); + assert!(!root.join("vault/s.txt").exists(), "nothing after abort"); + drop(repo); + remove_root(&root); + } + + /// A plain file of ours under a rule only theirs has is routed to sealing + /// too (the I2 case); outside the view it refuses the same way. + #[test] + fn a_plain_file_under_a_theirs_only_rule_outside_the_view_refuses_the_stop() { + let root = tmp_root("sparse-i2-vault"); + let repo = Repo::init(&root).unwrap(); + let (sk, pk) = scl_crypto::generate_keypair(); + std::fs::create_dir_all(root.join("src")).unwrap(); + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + std::fs::create_dir_all(root.join("vault")).unwrap(); + std::fs::write(root.join("vault/s.txt"), b"plain\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + repo.commit("me", "work").unwrap(); + repo.protect("vault/", &[pk], None).unwrap(); + repo.switch("main").unwrap(); + repo.set_sparse(&["src/".into()], Some(&sk)).unwrap(); + + let err = repo + .merge_with_identity("work", "me", Some(&sk)) + .unwrap_err(); + assert_widen_refusal(&err, "vault/s.txt"); + assert!(!repo.merge_in_progress()); + assert_nothing_written(&root); + drop(repo); + remove_root(&root); + } + + /// With a file-exact view (`src/a.bin`) the sidecar `src/a.bin.theirs` + /// does not match the spec, yet it sits beside an in-view conflict: the + /// stop must write it, and abort and completion must remove it. + #[test] + fn a_binary_conflict_in_a_file_exact_view_writes_and_removes_its_sidecar() { + let root = tmp_root("sparse-file-exact"); + let repo = Repo::init(&root).unwrap(); + std::fs::create_dir_all(root.join("src")).unwrap(); + std::fs::write(root.join("src/a.bin"), b"\xff\xfebase\n").unwrap(); + std::fs::write(root.join("other.txt"), b"x\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.bin"), b"\xff\xfeOURS\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("src/a.bin"), b"\xff\xfeTHEIRS\n").unwrap(); + repo.commit("me", "work").unwrap(); + repo.switch("main").unwrap(); + repo.set_sparse(&["src/a.bin".into()], None).unwrap(); + assert!(!root.join("other.txt").exists()); + + assert!(matches!( + repo.merge("work", "me").unwrap_err(), + Error::MergeConflicts(1) + )); + assert!(root.join("src/a.bin.theirs").exists(), "sidecar written"); + repo.merge_abort().unwrap(); + assert!(!root.join("src/a.bin.theirs").exists(), "sidecar removed"); + assert!(!root.join(".sc/CONFLICT_SIDECARS").exists()); + + // Completion: resolve, commit; the sidecar is in neither the tree nor + // the working tree. + assert!(matches!( + repo.merge("work", "me").unwrap_err(), + Error::MergeConflicts(1) + )); + std::fs::write(root.join("src/a.bin"), b"\xff\xferesolved\n").unwrap(); + repo.commit("me", "merge").unwrap(); + assert!(!repo.tracked_paths().unwrap().contains("src/a.bin.theirs")); + assert!(!root.join("src/a.bin.theirs").exists(), "scratch removed"); + assert!(!root.join(".sc/CONFLICT_SIDECARS").exists()); + drop(repo); + remove_root(&root); + } + /// A binary conflict writes `.theirs`. An untracked file already /// there (a leftover from an earlier stop, or the user's own) is refused, /// not overwritten. diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 00a88027..666b9419 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -1787,7 +1787,13 @@ impl Repo { /// CAS — no out-of-sparse marker is ever written, not even transiently. /// A CLEAN out-of-sparse change never reaches here at all: it resolves /// on tree ids inside `three_way`/replay and lands via the ordinary - /// materialize skip-write path (P24 Task 3), no markers involved. + /// materialize skip-write path (P24 Task 3), no markers involved. The one + /// clean change that does arrive is plaintext that must be sealed at + /// completion (`to_encrypt`) or a sidecar: both are written to disk. + /// A `to_encrypt` path outside the view is refused up front too (#210), on + /// every clone. A sidecar is refused only on a partial clone: on a full one + /// it sits beside an in-view conflicted path and is removed on completion + /// and abort, even when a file-exact spec does not match its name. pub(crate) fn materialize_conflict_state( &self, op: &str, @@ -1801,6 +1807,13 @@ impl Repo { conflicted_paths: &[String], ) -> Result { let sparse = self.sparse_spec()?; + // `sc sparse set` is refused while a rebase is stopped, so a refusal + // reached from `rebase --continue` cannot be fixed by widening in place. + let widen = if crate::rebase_state::in_progress(&self.layout) { + "abort the rebase (`sc rebase --abort`), widen with `sc sparse set`, then rerun the rebase" + } else { + "run `sc sparse set` to include it, then retry" + }; let needs_widen: Vec<&String> = conflicted_paths .iter() .filter(|p| !sparse.matches(p)) @@ -1812,29 +1825,33 @@ impl Repo { .collect::>() .join(", "); return Err(Error::InvalidArgument(format!( - "conflict in {names} is outside your sparse checkout; run `sc sparse set` to include it, then retry" + "conflict in {names} is outside your sparse checkout; {widen}" ))); } - // A partial clone completes from its view alone, so plaintext that - // must be sealed at completion has to sit inside the view (ADR-0049 - // decision 8). A full clone writes it wherever it lies and reads it - // back; here it would be left behind and dropped. - if self.promisor()?.is_some() { - if let Some((path, ..)) = to_encrypt.iter().find(|(p, ..)| !sparse.matches(p)) { - return Err(Error::InvalidArgument(format!( - "{op} has conflicts, and {path} (outside your sparse checkout) needs sealing at completion; \ - run `sc sparse set` to include it, or `sc backfill --all`, then retry" - ))); - } + // `to_encrypt` plaintext is written straight to the working tree and + // sealed again at completion, which reads it back from disk (a + // partial clone has nothing else to read it from: ADR-0049 decision + // 8). Outside the view that write would widen the checkout, and + // completion's `commit` never sweeps it, so refuse like an + // out-of-view conflict instead (ADR-0034, #210). + if let Some((path, ..)) = to_encrypt.iter().find(|(p, ..)| !sparse.matches(p)) { + return Err(Error::InvalidArgument(format!( + "{op} has conflicts, and {path} (outside your sparse checkout) needs sealing at completion; \ + {widen}" + ))); } - // A sidecar is plaintext written beside a conflicted file. Outside the - // view it would sit where completion neither expects nor protects it, - // and a later commit would read it back as a plain file. + // A sidecar is `.theirs`, and the conflicted path + // passed the gate above, so on a full clone it is beside an in-view + // file (a file-exact spec such as `src/a.bin` does not match + // `src/a.bin.theirs`, yet that sidecar is in view) and `abort` and + // completion remove it. A partial clone keeps the stricter ADR-0049 + // refusal: outside the view its sidecar would sit where completion + // neither expects nor protects it. if self.promisor()?.is_some() { if let Some((rel, _)) = sidecars.iter().find(|(r, _)| !sparse.matches(r)) { return Err(Error::InvalidArgument(format!( "{op} has conflicts, and its sidecar {rel} would fall outside your sparse checkout; \ - run `sc sparse set` to include it, or `sc backfill --all`, then retry" + {widen}" ))); } } @@ -1881,13 +1898,12 @@ impl Repo { .map(|(p, ..)| p.as_str()) .chain(sidecars.iter().map(|(r, _)| r.as_str())); for rel in targets { - // A path the sparse view never materialized holds no tracked - // file on disk, even if the tree tracks it: whatever is there - // is the user's own. A failed lookup counts as untracked. + // Every target is in the view (gated above; a sidecar is + // never tracked, checked above). A failed lookup counts as + // untracked. let mut tracked = |p: &str| { - sparse.matches(p) - && worktree::tree_entry_at(&mut store, old_root, p, partial) - .is_ok_and(|e| e.is_some()) + worktree::tree_entry_at(&mut store, old_root, p, partial) + .is_ok_and(|e| e.is_some()) }; match stop_write_blocker(&self.layout.root, rel, &mut tracked)? { None => {} @@ -5402,6 +5418,102 @@ mod tests { std::fs::remove_dir_all(&root).unwrap(); } + #[test] + fn merge_conflict_with_out_of_sparse_protected_change_refuses_then_lands_after_widening() { + // #210: an in-sparse conflict co-occurring with a clean content merge + // of an out-of-sparse protected path would write that path's + // plaintext outside the view (and `commit` never sweeps it). The stop + // refuses up front instead, writing nothing; widening the view lets + // the same merge stop, complete and land the protected merge sealed. + let root = tmp_root("sparse-conflict-protected-outside"); + let repo = Repo::init(&root).unwrap(); + let (sk, pk) = scl_crypto::generate_keypair(); + repo.protect("secret/", &[pk], None).unwrap(); + std::fs::create_dir_all(root.join("src")).unwrap(); + std::fs::create_dir_all(root.join("secret")).unwrap(); + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("secret/s.txt"), b"s1\ns2\ns3\ns4\ns5\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("secret/s.txt"), b"OURS\ns2\ns3\ns4\ns5\n").unwrap(); + repo.commit("me", "ours").unwrap(); + repo.switch_with_identity("feature", Some(&sk)).unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("secret/s.txt"), b"s1\ns2\ns3\ns4\nTHEIRS\n").unwrap(); + repo.commit("me", "theirs").unwrap(); + repo.switch_with_identity("main", Some(&sk)).unwrap(); + + repo.set_sparse(&["src/".into()], Some(&sk)).unwrap(); + assert!(!root.join("secret/s.txt").exists()); + let before = repo.head_tip().unwrap(); + + let err = repo + .merge_with_identity("feature", "me", Some(&sk)) + .unwrap_err(); + match err { + Error::InvalidArgument(msg) => { + assert!(msg.contains("secret/s.txt"), "names the path: {msg}"); + assert!(msg.contains("sc sparse set"), "suggests widening: {msg}"); + } + other => panic!("expected InvalidArgument widen hint, got {other:?}"), + } + assert!( + !root.join("secret/s.txt").exists(), + "nothing out of view on disk" + ); + assert!(!root.join(".sc/CONFLICT_PLAINTEXT").exists()); + assert!(!root.join(".sc/CONFLICT_SIDECARS").exists()); + assert!(!repo.merge_in_progress()); + assert_eq!(repo.head_tip().unwrap(), before, "HEAD unchanged"); + assert_eq!( + std::fs::read(root.join("src/a.txt")).unwrap(), + b"OURS\nl2\nl3\n", + "the in-view file is untouched" + ); + + // Widen and retry: stops on the in-view conflict only. + repo.set_sparse(&["src/".into(), "secret/".into()], Some(&sk)) + .unwrap(); + let err = repo + .merge_with_identity("feature", "me", Some(&sk)) + .unwrap_err(); + assert!(matches!(err, Error::MergeConflicts(1)), "got {err:?}"); + std::fs::write(root.join("src/a.txt"), b"RESOLVED\nl2\nl3\n").unwrap(); + let id = repo.commit("me", "resolve").unwrap(); + let snap = repo.snapshot(&id).unwrap(); + let (blob_id, perms) = { + let store_arc = repo.vfs_handle().store(); + let mut s = store_arc.lock().unwrap(); + let entries = worktree::tree_file_entries_with_perms(&mut s, snap.root).unwrap(); + let (bid, _, perms) = entries["secret/s.txt"]; + (bid, perms) + }; + assert_ne!(perms & scl_core::PROTECTED, 0); + let bytes = { + let store_arc = repo.vfs_handle().store(); + let mut s = store_arc.lock().unwrap(); + match s.get(&blob_id).unwrap() { + Object::Blob(b) => b.to_vec(), + _ => panic!("expected Blob"), + } + }; + let pt = crate::protect::decrypt_with( + &bytes, + &blob_id, + &[&snap.protection], + &sk, + "secret/s.txt", + ) + .unwrap(); + assert_eq!(&pt[..], b"OURS\ns2\ns3\ns4\nTHEIRS\n"); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "test repository must leave no residue"); + } + #[test] fn push_creates_a_new_remote_branch() { let a = tmp_root("push-newbr-remote"); diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 84a1a8c9..ce0a3218 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -54,35 +54,28 @@ review fix — see Consequences). bounds checkout cost the way the budget bounds resident blobs. - gc is unchanged: all objects stay reachable (sparse is a disk view, not an object-set change). -- **Boundary: an out-of-sparse plaintext write during a mixed conflict - that OUTLIVES the conflict window.** `Repo::materialize_conflict_state` - (`crates/repo/src/repo.rs`) gates its own conflict *markers* against the - sparse spec up front — an out-of-sparse conflicted path refuses with a - widen hint before anything is written. But that gate only covers the - marker-write loop; the same function's `to_encrypt`/sidecar-decrypt - write loops (protected-content re-encryption inputs and `.theirs` - sidecars for an *in-sparse* conflict) are not sparse-scoped. When an - IN-sparse conflict co-occurs with an OUT-of-sparse protected/I2 clean - change in the same merge, that out-of-sparse plaintext is written to - disk outside the sparse view — and, verified by final-review testing, - it **persists on disk after completion too**, not just during the - conflict window: only `abort` removes it (its `!sparse.matches` removal - arm runs on abort); completion's `read_worktree` re-lands the content in - the CAS byte-correct, the same as any other carried file, but `commit` - does not materialize, so it never deletes the on-disk file. The - plaintext stays on disk until the next materializing operation (`switch`, - `sparse set`/`disable`, another merge) re-lays the tree — there is no - completion-time sweep. This is **not data loss** — the content lands in - the CAS byte-correct either way — and **not a new disclosure**: the - diff3 content-merge that produces the plaintext already required an - authorized identity, and the I2 carried-plaintext case is pre-existing - plaintext the sparse gate never claimed to hide from disk. It is a - bounded disk-hygiene boundary — the sparse view stays wider than - advertised, with no signal, until something else re-lays the tree — not - a confidentiality or durability bug. Follow-on: extend the sparse gate - to the `to_encrypt`/sidecar write loops so the view holds even - mid-conflict (which would also close the post-completion persistence, - since there would be nothing out-of-sparse to persist). +- **Closed (#210): no out-of-sparse plaintext during a mixed conflict.** + `Repo::materialize_conflict_state` (`crates/repo/src/repo.rs`) gates its + conflict *markers* against the sparse spec up front, and since #210 the + same gate covers the `to_encrypt` inputs (content-merged protected files, + and ours' carried-plain files under a theirs-only rule). One outside the + sparse view refuses the merge, cherry-pick or rebase with a widen hint + (`sc sparse set`) before anything is written, exactly like an + out-of-sparse conflicted path, so a stop, its completion and its abort + leave nothing out of view on disk. From `rebase --continue` the hint + says to abort the rebase first, because `sparse set` is refused while a + rebase is stopped. Sidecars need no gate on a full clone: each is + `.theirs` beside a conflicted path that already passed + the gate (so it is in view even under a file-exact spec such as + `src/a.bin`), and abort and completion remove it; a partial clone keeps + its stricter sidecar refusal (ADR-0049). The earlier build wrote that + plaintext outside the view and left it there after completion (only + abort removed it); this was never data loss or a new disclosure, only a + view wider than advertised. The cost is that such a merge now needs the + view widened first; the alternative, sealing the plaintext at the stop, + would need its fresh wraps persisted with the decided root and was not + worth the surface. The check already existed for partial clones + (ADR-0049 decision 8); it now applies to every clone. ## Alternatives considered @@ -138,9 +131,9 @@ every claim below is checked against the shipped code, not the plan. write to disk. A CLEAN out-of-sparse change never reaches either gate — it resolves on tree ids inside `three_way`/replay and lands via the ordinary `materialize` skip-write path, no markers involved at all. See - the Consequences section above for the one write path this gate does - *not* cover (`to_encrypt`/sidecar writes for an in-sparse conflict that - co-occurs with an out-of-sparse clean change). + the Consequences section above: since #210 the `to_encrypt` + writes for an in-sparse conflict that co-occurs with an out-of-sparse + clean change are refused by the same gate. 4. **`sc ws` inherits the host's sparse view, structurally, not by convention — and, after a final-review fix, durably against later host spec changes.** `Repo::sparse_spec()` is read once in `ws_fork` and diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index e19a31de..84df85ce 100644 --- a/docs/adr/0049-merge-on-partial-clones.md +++ b/docs/adr/0049-merge-on-partial-clones.md @@ -104,7 +104,7 @@ Flatten drops empty directories and directory metadata and rebuilds directories 1. A conflicted path outside the sparse view. This is the existing `materialize_conflict_state` refusal, and the widen hint stays: `sc sparse set` on a lazy clone, `sc backfill` on a non-lazy one. 2. A sidecar (`.theirs`, written for a binary conflict) whose path is outside the view: it is plaintext beside the conflicted file, and outside the view completion neither expects nor protects it. -3. A result that must be sealed at completion (protected plaintext from a clean content merge, or a plain file under a union rule) at a path outside the view. A full clone writes that plaintext to disk wherever it lives, and the completion commit reads it back. A partial clone completes from the view only, so the plaintext would be left behind and silently dropped. Writing it outside the view would also leave plaintext on disk where the user asked for none. The combination is refused with a message that names the path. Sealing it into the decided root at conflict time would lift the refusal, but it needs the fresh wraps persisted (a fourth state file), and nothing yet asks for it. +3. A result that must be sealed at completion (protected plaintext from a clean content merge, or a plain file under a union rule) at a path outside the view. Completion reads that plaintext back from disk. A partial clone completes from the view only, so plaintext left outside it would be silently dropped, and on any clone writing it outside the view leaves plaintext on disk where the user asked for none (and `commit` never sweeps it). The combination is refused with a message that names the path, on full and partial clones alike (#210; a full clone was first allowed to write it, which left the plaintext behind after completion, ADR-0034). Sealing it into the decided root at conflict time would lift the refusal, but it needs the fresh wraps persisted (a fourth state file), and nothing yet asks for it. **Completion** (`sc commit` with `MERGE_HEAD` set) is allowed on a partial clone only when a decided root is recorded, which every partial conflict writes. It builds `all` from the working tree and the in-view carry exactly as today, then splices every out-of-view subtree back in **from the decided root** by id (`graft_out_of_sparse` with the decided root as the parent), where a plain commit grafts from the tip. The wrap map keeps the complete union of both parents' wraps (decision 4's coarse rule: nothing is pruned when a subtree was grafted), since a grafted subtree's blobs never pass through the seal loop. A merge without a decided root, and every cherry-pick or rebase completion, still refuse; replay is 37c-iv. @@ -184,7 +184,7 @@ Replay is a three-way merge with a different base, and `replay_commit` is the on 19. **Completion can drop a newly tracked ignored file.** *Resolved:* completion reads the working tree with the decided root's paths counted as tracked, so an ignore rule cannot hide a file the operation itself added. Test `completing_a_conflicted_pick_keeps_an_added_file_the_parent_ignores`. 20. **A stopped pick or rebase without its decided root.** `PICK_DECIDED_ROOT` and `REBASE_DECIDED_ROOT` are written before the state's in-progress signal, so the state cannot normally be missing. If it is (a lost file, a power loss before the data reached disk), a partial clone's completion refuses, because grafting from the tip would keep the old out-of-view content; `--abort` still works. -21. **A conflict stop overwrites an ignored or untracked file at a plaintext path.** *Resolved for merge, cherry-pick and rebase:* `materialize_conflict_state` refuses, before any write, when a plaintext or sidecar path already exists on disk (as a file or directory, including a leftover sidecar) and the tree the working tree reflects does not track it or the sparse view never materialized it, or when a file stands where a parent directory must be. A lookup that fails counts as untracked. *Also resolved for the clean paths:* `refuse_untracked_overwrite` runs before the first write of a merge (fast-forward, clean and conflict stop), `switch`, cherry-pick and rebase (fast-forward and the fold's final write) and refuses when a file the result adds, or a directory it needs, lands on something the tree the working tree reflects does not track. It reads the working tree only, skips paths outside the sparse view, and leaves a partial-clone gap to `materialize`'s own view check. A symlink at a written path always counts as occupied (a write would follow it out of the tree), and a tracked path outside the sparse view does not excuse a file in the way. `undo`, `sparse set` and `sparse disable` are guarded the same way (a sparse-spec change against the tree its old view laid out). Not guarded: merging into an unborn branch (there is no tree to call a file untracked against). The private-branch switch and merge are guarded inside `materialize_private` against the decrypted target paths. Tracking is compared by exact path, plus a case alias of a tracked path that is provably the same file (same device and inode, unix only), so a case-only rename on a case-insensitive filesystem is not refused; a non-unix build still compares exact paths. The private-branch merge stop (`merge_into_private`) writes its files directly and has no such guard. Tests `a_conflict_stop_refuses_to_overwrite_an_untracked_local_copy`, `a_conflict_stop_does_not_treat_an_out_of_view_path_as_materialized`, `a_binary_conflict_stop_refuses_over_an_untracked_sidecar` and `a_conflict_stop_refuses_when_a_file_blocks_a_parent_directory`. +21. **A conflict stop overwrites an ignored or untracked file at a plaintext path.** *Resolved for merge, cherry-pick and rebase:* `materialize_conflict_state` refuses, before any write, when a plaintext or sidecar path already exists on disk (as a file or directory, including a leftover sidecar) and the tree the working tree reflects does not track it, or when a file stands where a parent directory must be. A lookup that fails counts as untracked. *Also resolved for the clean paths:* `refuse_untracked_overwrite` runs before the first write of a merge (fast-forward, clean and conflict stop), `switch`, cherry-pick and rebase (fast-forward and the fold's final write) and refuses when a file the result adds, or a directory it needs, lands on something the tree the working tree reflects does not track. It reads the working tree only, skips paths outside the sparse view, and leaves a partial-clone gap to `materialize`'s own view check. A symlink at a written path always counts as occupied (a write would follow it out of the tree), and a tracked path outside the sparse view does not excuse a file in the way. `undo`, `sparse set` and `sparse disable` are guarded the same way (a sparse-spec change against the tree its old view laid out). Not guarded: merging into an unborn branch (there is no tree to call a file untracked against). The private-branch switch and merge are guarded inside `materialize_private` against the decrypted target paths. Tracking is compared by exact path, plus a case alias of a tracked path that is provably the same file (same device and inode, unix only), so a case-only rename on a case-insensitive filesystem is not refused; a non-unix build still compares exact paths. The private-branch merge stop (`merge_into_private`) writes its files directly and has no such guard. Tests `a_conflict_stop_refuses_to_overwrite_an_untracked_local_copy`, `a_conflict_stop_refuses_an_out_of_view_sealing_input_and_keeps_the_local_copy` (the sparse refusal now comes first for an out-of-view path), `a_binary_conflict_stop_refuses_over_an_untracked_sidecar` and `a_conflict_stop_refuses_when_a_file_blocks_a_parent_directory`. ## Alternatives considered