Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,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);
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
Expand All @@ -248,7 +252,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). While a merge/pick/rebase is stopped, `sparse
Expand Down
3 changes: 2 additions & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions crates/repo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
524 changes: 464 additions & 60 deletions crates/repo/src/private.rs

Large diffs are not rendered by default.

162 changes: 162 additions & 0 deletions crates/repo/src/private_sparse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//! 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<String, String> {
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,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 {
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");
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");
}
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");
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();
}
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<String, String> {
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();
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();
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");
// 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();
assert!(!full.root.exists(), "test dir left behind");
std::fs::remove_dir_all(&sparse.root).unwrap();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
assert!(!sparse.root.exists(), "test dir left behind");
}

#[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();
assert!(!f.root.exists(), "test dir left behind");
}

#[test]
fn replay_onto_a_private_branch_stays_refused_under_sparse() {
let f = fixture("replay", true, false);
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");
}
9 changes: 6 additions & 3 deletions docs/adr/0034-sparse-checkouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,12 @@ every claim below is checked against the shipped code, not the plan.
ref); the repo lock is held by the CLI as for any mutation.
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
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
Expand Down
58 changes: 56 additions & 2 deletions docs/adr/0044-per-branch-access-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -350,6 +353,57 @@ 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: 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
ref-moving op is guarded). `commit_private` therefore **re-derives** the
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.
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. **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. 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
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
`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;
and by `private.rs` tests for the full-checkout protected-path case.

## Deferred (to ROADMAP.md)

- `sc branch publish --squash` (hide intermediate history at publish).
Expand Down
Loading