sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

RFC: Multi-sandbox transactions for pipelines

Open congwang-mk opened this issue 6 months ago • 8 comments

Goal

Promote commit / abort from a per-sandbox property to a first-class property of a Pipeline (or any group of sandboxes that should succeed or fail together). Stages should be able to see each other's in-flight writes under a defined isolation model, and the runtime should commit all of them together at the end or none of them at all.

This is an RFC for the full feature, not a single PR. It describes the end state, the design choices, the recommended defaults, and a delivery phasing.

What exists today

  • Sandbox owns cow_branch: Option<Box<dyn CowBranch>> with commit() / abort() / cleanup() / changes() (crates/sandlock-core/src/cow/mod.rs).
  • Only 1 backend now:
    • seccomp: pure userspace, no mount, no privilege. Supervisor intercepts opens, plans copy-on-write into an upper directory, tracks deletions in memory. Auto-enabled when fs_isolation = None and a workdir is set. Works on any Linux 5.9+.
  • Per-sandbox on_exit / on_error policy (BranchAction::{Commit, Abort, Keep}) auto-applied in Sandbox::Drop (crates/sandlock-core/src/sandbox.rs:1615).
  • Pipeline forks N stages with inter-stage pipes (crates/sandlock-core/src/pipeline.rs), zero awareness of COW. Each stage's Drop independently decides commit/abort based on its own exit code.
  • Dry-run for a single sandbox (Sandbox::do_dry_run) captures changes() then aborts.
  • Gather (fan-in) exists for data flow over pipes; it has no COW story.

The end state (what "done" looks like)

A user can declare a group of sandboxes (initially a Pipeline or Gather, eventually any user-defined set) and run them under a transaction. The runtime guarantees:

  1. Every stage observes prior stages' writes according to the chosen isolation model (default: read-committed for sequential, snapshot for parallel siblings).
  2. At end of transaction, either every stage's writes commit to the underlying workdir, or none of them do.
  3. Parallel siblings that touch the same path produce a structured conflict, surfaced to the caller with a configurable resolution policy.
  4. The pipeline can be dry-run: the union of intended changes is reported and nothing is committed.
  5. The transaction works on any sandlock-supported kernel (5.9+) without elevated privilege.
  6. Crash mid-commit is recoverable: a subsequent sandlock invocation can either complete or roll back the interrupted transaction from durable transaction metadata.
  7. Concurrent transactions over the same workdir are serializable (or at minimum: fail fast with a clear error rather than interleaving undefined-ly).

The user surface:

  • Rust: Pipeline::commit() / abort() / dry_run(), Transaction::new(sandboxes).run(), MergeConflict type with policy enum.
  • CLI: sandlock pipeline ... --dry-run, sandlock txn list / show / abort, exit codes that distinguish stage failure from commit failure from conflict.
  • Python SDK: mirrored API plus with Transaction(...) as t: context manager.

Design axes (the choices the RFC commits to)

Isolation model

Model What a stage sees Cost
Read-committed (sequential) All writes committed by prior stages in this transaction, plus the pre-transaction workdir Low. Lookup chain extends through prior uppers.
Snapshot (parallel siblings) The pre-transaction workdir only; siblings' writes are invisible until commit Low for seccomp backend (each sibling has its own upper).
Serializable (across transactions) No other transaction's mid-flight writes ever visible Needs a workdir-level lock manager.

Recommendation: read-committed for sequential pipelines, snapshot for parallel siblings, opt-in workdir lock for cross-transaction serializability. All three are expressible on top of the seccomp backend without new kernel features.

Atomicity of finalization

Strategy Property Cost
Serial per-branch commit Simple. Mid-commit failure leaves partial state. Trivial.
Two-phase commit Prepare all branches (validate writable, no conflicts), then flip. Mid-flip failure still partial unless combined with a journal. Medium.
Write-ahead log + recovery Durable transaction metadata; recovery step on next startup completes or rolls back. Significant, but unblocks crash safety.
Single-rename finalize Stage entire result tree aside, rename(2) into place. Atomic but only for whole-subtree replacement. Limited applicability.

Recommendation: serial per-branch commit in Phase 1 with an explicit "crash mid-commit may leave partial state" caveat in the API doc; WAL + recovery as a later phase.

Conflict resolution for parallel siblings

Policy Behavior
Fail (default) Any path written by two siblings aborts the transaction with a MergeConflict listing the conflicting paths.
FirstWriterWins Earlier-declared sibling wins per-path; later writes discarded.
LastWriterWins Later-declared sibling wins per-path.
Markers Both versions retained with a suffix; commits succeed, user resolves later.

Recommendation: Fail is the only safe default. The others should require explicit opt-in.

Backend strategy

Backend Suitability for transactions
seccomp Best fit. Pure userspace, the supervisor already arbitrates every open. Extending the upper-lookup chain across multiple uppers (prior stages' or sibling siblings') is a natural extension of the existing model. Works without privilege on any Linux 5.9+. This is sandlock's identity.
overlayfs Off the rootless happy path. Requires mount privilege via user namespaces or CAP_SYS_ADMIN. Stacking via lowerdir=A:B:C is convenient syntactically but the privilege requirement contradicts sandlock's positioning. Future optimization target for heavy-I/O workloads where in-kernel overlay is meaningfully faster than userspace COW, not a starting point.
branchfs Niche. Requires a kernel module that is not generally available; backend's changes() returns empty (kernel-managed, opaque to userspace), so dry-run does not work. Out of scope for transactions.

Recommendation: Phase 1 implements transactions on the seccomp backend only. Overlayfs support is a later phase if and only if performance data justifies it. Branchfs is out of scope.

Coordinator placement

Pipeline already owns the stage handles and lifetime. The coordinator lives next to Pipeline. A general Transaction API that accepts an arbitrary set of Sandboxes can be exposed later; the pipeline coordinator is the first user of the underlying primitive.

Phased delivery

Phase 1: Transactional sequential pipelines on the seccomp COW backend

Smallest useful slice that delivers a real, demoable feature without compromising sandlock's unprivileged identity.

  • Pipeline::commit() / abort() / dry_run() API and matching Python surface.
  • Coordinator forces every stage's on_exit / on_error to BranchAction::Keep, waits for all stages, then commits all in declaration order on aggregate success, or aborts all on any failure.
  • Sequential stacking in the seccomp backend: stage N+1's upper-lookup chain is [own upper, stage_N upper, stage_{N-1} upper, ..., original workdir]. Reads walk the chain; first hit wins; deletions tracked per-upper still hide lower entries.
  • Isolation: read-committed only.
  • Conflict policy: not yet meaningful (sequential, single writer per timestamp).
  • Atomicity: serial per-branch commit; document the crash window.
  • Backends other than seccomp: fail fast with a clear error pointing at this RFC.

Acceptance: 3-stage pipeline where stage 1 writes a.txt, stage 2 reads a.txt and writes b.txt, stage 3 reads both. On success both files appear in the workdir; on any non-zero stage exit, neither appears. Existing per-sandbox COW behavior unchanged. Python SDK covered by at least one end-to-end test.

Phase 2: Parallel siblings and Gather transactions

  • Snapshot isolation for siblings: each sibling has its own upper, lower is the pre-transaction state.
  • MergeConflict type and ConflictPolicy::{Fail, FirstWriterWins, LastWriterWins, Markers}.
  • Commit-time conflict detection across sibling uppers.
  • Gather::commit() / abort() / dry_run() mirroring Pipeline.

Phase 3: Crash safety

  • Durable transaction metadata under $XDG_RUNTIME_DIR/sandlock/txn/<id>/ describing every branch and its committed state.
  • Recovery sweep on next sandlock invocation: complete or roll back interrupted transactions.
  • Two-phase commit option for callers that need stronger guarantees than serial.

Phase 4: Cross-transaction serializability

  • Workdir-level advisory lock (file-based, flock) acquired at txn open, released at commit/abort.
  • Configurable: IsolationPolicy::{None, FailIfBusy, WaitForLock(Duration)}.
  • Opt-in; default remains "undefined behavior on concurrent transactions, same as today."

Phase 5 (optional): Overlayfs backend support

Only if profiling shows the userspace COW path is a real bottleneck for a class of workloads where users are willing to accept the mount-privilege requirement. Most likely route: --fs-isolation=overlayfs becomes a performance opt-in for transactions, with the same coordinator and lookup-chain semantics as the seccomp path.

Open questions

  • Partial / selective commit by path ("commit ./out/, discard ./scratch/"). Useful for build pipelines. Phase 2 or 3? Affects the commit() signature; better to settle before Phase 1 ships.
  • Stage retries inside a transaction. A retried stage's prior upper should be discarded before re-running; needs a hook in the coordinator.
  • Memory bound on the seccomp backend's deletion-tracking set when chained across many stages. Worth measuring before Phase 1 ships.
  • Interaction with --no-supervisor mode. Transactions require the supervisor (the seccomp backend needs it). --no-supervisor should fail fast with a clear error.
  • Pipelines that mix sandboxed and unsandboxed stages. Out of scope for now; document.

Out of scope (this RFC)

  • Transactions across hosts.
  • Transactions over the chroot-mode workdir.
  • Branchfs backend support.
  • A generic Transaction API decoupled from Pipeline / Gather (Phase 1+2 deliver enough to inform the right shape later).

Proposed Python API and examples

These examples make the design concrete. They use the Python SDK's existing Sandbox(...).cmd([...]) shape plus the | pipeline operator and the Gather builder, and add run_transactional(), prepare(), dry_run(), and a transaction() context manager.

API additions

# python/src/sandlock/_sdk.py

class Pipeline:
    def run_transactional(self) -> TxnOutcome: ...
    def dry_run(self) -> DryRunOutcome: ...
    def prepare(self) -> PreparedTxn: ...
    def transaction(self) -> ContextManager[PreparedTxn]: ...

class Gather:
    def run_transactional(self) -> TxnOutcome: ...
    def dry_run(self) -> DryRunOutcome: ...
    def prepare(self) -> PreparedTxn: ...
    def with_conflict_policy(self, policy: ConflictPolicy) -> "Gather": ...   # Phase 2

@dataclass
class TxnOutcome:
    committed: bool
    stages: list[StageResult]
    changes: list[Change]
    conflicts: list[MergeConflict]       # Phase 2
    abort_reason: AbortReason | None

class PreparedTxn:
    stages: list[StageResult]
    changes: list[Change]
    conflicts: list[MergeConflict]
    def all_succeeded(self) -> bool: ...
    def has_conflicts(self) -> bool: ...
    def commit(self) -> None: ...
    def abort(self) -> None: ...

The existing Pipeline.run() keeps today's per-stage-Drop behavior. run_transactional() is the new entry point; existing callers are unaffected.

Example 1: agent loop, all-or-nothing (Phase 1)

from sandlock import Sandbox

workspace = "/home/me/repo"
BASE_READ = ["/usr", "/lib", "/lib64", "/bin", "/etc"]

planner = Sandbox(
    workdir=workspace,
    fs_readable=BASE_READ + [workspace],
    net_allow=["api.openai.com:443"],
)
editor = Sandbox(
    workdir=workspace,
    fs_readable=BASE_READ,
    fs_writable=[workspace],
)
tester = Sandbox(
    workdir=workspace,
    fs_readable=BASE_READ + [workspace],
    max_processes=20,
    max_memory="1G",
)

result = (
    planner.cmd(["python3", "plan.py"])
    | editor.cmd(["python3", "apply_patch.py"])
    | tester.cmd(["pytest", "-x"])
).run_transactional()

if result.committed:
    print("agent step merged")
else:
    print(f"rolled back: {result.abort_reason}")

Example 2: preview before commit, context-managed (Phase 1)

pipeline = (
    planner.cmd(["python3", "plan.py"])
    | editor.cmd(["python3", "apply_patch.py"])
    | tester.cmd(["pytest"])
)

with pipeline.transaction() as txn:
    txn.run()

    if not txn.all_succeeded():
        raise RuntimeError("stage failed; context exit will abort")

    print(f"would change {len(txn.changes)} paths:")
    for c in txn.changes:
        print(f"  {c.kind} {c.path}")

    if input("apply? [y/N] ").lower() == "y":
        txn.commit()
    # leaving the `with` block without committing aborts automatically;
    # exceptions also auto-abort.

Example 3: test-and-merge for an LLM-proposed patch (Phase 1)

patch_text = call_llm(prompt)

apply = Sandbox(
    workdir=workspace,
    fs_readable=BASE_READ,
    fs_writable=[workspace],
)
test = Sandbox(
    workdir=workspace,
    fs_readable=BASE_READ + [workspace],
)

outcome = (
    apply.cmd(["git", "apply", "-"], stdin=patch_text.encode())
    | test.cmd(["pytest", "-x"])
).run_transactional()

# Workspace is byte-identical to before the call if anything failed.
return outcome.committed

Example 4: parallel agents on disjoint subsystems (Phase 2)

from sandlock import Gather, ConflictPolicy

frontend = Sandbox(workdir=workspace, fs_writable=[f"{workspace}/web"])
backend  = Sandbox(workdir=workspace, fs_writable=[f"{workspace}/api"])
test     = Sandbox(workdir=workspace, fs_readable=BASE_READ + [workspace])

result = (
    Gather()
    .source("frontend_agent", frontend.cmd(["agent", "--scope", "web"]))
    .source("backend_agent",  backend.cmd(["agent", "--scope", "api"]))
    .consumer(test.cmd(["pytest"]))
    .with_conflict_policy(ConflictPolicy.FAIL)
    .run_transactional()
)

if not result.committed:
    if result.conflicts:
        for c in result.conflicts:
            print(f"CONFLICT {c.path} written by {c.stages}")
    else:
        print(f"failed: {result.abort_reason}")

Design notes worth flagging

  1. run() stays as today. Adding run_transactional() rather than mutating run()'s behavior avoids silently changing existing pipelines.
  2. prepare() returns a value the caller must resolve. PreparedTxn aborts on garbage-collection if never committed, so a forgotten .commit() / .abort() does not silently commit.
  3. transaction() context manager is the ergonomic surface: clean exit commits if .commit() was called, otherwise aborts; any exception aborts.
  4. TxnOutcome always carries stages even when aborted, so the caller can inspect which stage failed without a second call.
  5. Change is the existing dry_run.Change, so dry_run() for a pipeline is the same shape as dry_run() for a single sandbox, just merged across stages.
  6. No CLI surface in Phase 1. Pipelines are constructed in code today; CLI declaration of multi-stage pipelines is a separate question and should not block this work.

congwang-mk avatar May 26 '26 22:05 congwang-mk

Thanks. We read the RFC carefully and pulled the relevant code (pipeline.rs:140-200, sandbox.rs:1289 and ~2170, cow/seccomp.rs:78-87/727). Below — what we found on your open questions and a couple of observations from the reading. All in discussion mode, not as requirements.

On your open questions

Partial / selective commit by path. In pre-1.0 Rust, extending the commit() signature with a paths: Option<...> parameter is a breaking change; in Python it isn't. If the decision is to defer to Phase 2/3 — probably worth noting in Phase 1 explicitly that the signature is provisional, so downstream doesn't stabilize against it. From our angle: how much real demand has there been for partial commit from use cases? We don't have it in our potential scenarios.

Memory bound on the seccomp backend's deletion-tracking set. Looked through the code (cow/seccomp.rs:203, 570-617). mark_deleted() is called only from handle_unlink and handle_rename (source path) and inserts one String into a HashSet. So the bound on the set itself is O(unlink + rename syscalls), not O(workdir tree). But: handle_rename marks a directory deletion as a single entry, and at commit time that one entry semantically expands into a remove_dir_all() on a potentially large subtree. So memory bound is small, but cost-of-commit is unbounded per entry semantics. Might be worth measuring on a "10 stages × rm -rf node_modules/" scenario — realistic worst case.

Interaction with --no-supervisor mode. In sandbox.rs::do_create, when no_supervisor = true, seccomp_cow_branch is silently assigned None without error or warning. So the "fail fast" you describe doesn't exist today — it would need to be added explicitly. A natural location would be at the construction of Pipeline::transaction() (or the equivalent Rust entry-point), with an error naming the specific stage so the caller sees the problem before execution starts.

Pipelines that mix sandboxed and unsandboxed stages. Agreed it's out of scope. Worth validating this at construction time and erroring out (rather than leaving it as undefined behavior), as long as mixing isn't supported? Cheap validation that closes the "undocumented undefined" class.

Stage retries inside a transaction. Less confidence here. Sandbox::clone() resets runtime: None, which gives a fresh upper on each run — good baseline. But if discard is interrupted mid-way (partial cleanup of the tmp upper), there's no way to tell "fully discarded" from "fully retained". Possibly atomic rename to .<stage>_discard_<nonce> followed by rm -rf — standard technique to make the interrupted state unambiguous garbage. Just an observation, not a claim on design.

A few things we noticed in the code, would like to understand how you see them

Pipeline currently runs concurrently. run_pipeline (pipeline.rs:140-200) spawns every stage in a single loop before any wait(). The Phase 1 acceptance test ("stage 1 writes a.txt, stage 2 reads it") intuitively expects sequential execution — stage N must finish before N+1 starts. How do you see this: Phase 1 replaces concurrent with sequential drain (a separate execution path)? Or it stays concurrent and the contract becomes "read-committed at process-exit boundary"? This significantly changes the shape of the coordinator.

The coordinator overrides each stage's on_exit/on_error. RFC: "Coordinator forces every stage's on_exit/on_error to BranchAction::Keep". If a caller has explicitly set on_error: BranchAction::Abort, that intent is silently replaced. You flagged this exact class of silent mutation on PR #20. Is this an intentional trade-off (explicit prepare-coordinator contract)? Or worth picking between two validation paths — rejecting a stage with non-Keep policy at construction (loud error), or a Stage::for_transaction() separate constructor that makes the override visible at the call site?

Per-branch commit is not atomic at the file level. Phase 1: "serial per-branch commit; document the crash window". If commit is interrupted (OOM, ENOSPC, panic) — the workdir contains a half-materialized branch with no marker. Phase 3 WAL solves this; but Phase 1 already ships the corruption window. Is this a conscious MVP trade-off (Phase 3 removes the corruption)? Or worth at least a .committed_<branch> marker written as the last step — minimal cost.

Concurrent commit on the same workdir. Phase 1 says "undefined behavior, same as today"; Phase 4 plans a workdir-level flock. The concrete failure: two coordinators racing on rename(2) — last writer wins, no error to either. Worth at least adding LOCK_EX | LOCK_NB + fail loudly in Phase 1 — a one-line change that prevents silent corruption, with Phase 4 adding the wait path later?

We didn't find tests covering Pipeline + COW interaction. tests/integration/test_pipeline.rs doesn't use fs_isolation or workdir; test_cow.rs has no pipeline-aware tests. Phase 1 lands into an uncovered interaction surface. The acceptance test covers the happy path, but a destructive test catching "stage 3 sees stale a.txt because stage 1's commit hasn't applied" — that's what would catch the kind of regression Phase 2/3 changes are likely to introduce.

Where this goes

Happy to take on Phase 1 implementation once the sequential/concurrent direction and the on_exit override approach settle — those are the two forks that most shape the coordinator. The rest are regular PR-review concerns to handle along the way. If you'd prefer to break any of this out into separate issues or a discussion thread, let me know which.

dzerik avatar May 27 '26 08:05 dzerik

Hi @dzerik

Thanks for looking into this! Sorry for the delay. I finally have time to review the whole proposal.

Regarding your questions:

  1. Pipeline should not run concurrently when they have dependencies. Only explicit parallel pipeline should do so. Thanks for catching this. Mind to fix it?
  2. I think on_exit/on_error essentially conflicts with pipeline, so maybe just forbid the combination with pipeline?
  3. Right, per-branch commit is not implemented in FS level (after we remove branchfs and overlayfs support), maybe we could make it atomic at pipeline-level?
  4. I think the intended behavior is first-committer-win for concurrent commits.
  5. Right, it is probably a corner case I never covered. Please feel free to add them.

Hope this answers all of your questions above. If you any better idea, please let me know.

If you are interested in working on PR, please split PR into smaller ones as before. (Large PR is always harder to review)

Regards, Cong

congwang-mk avatar Jun 23 '26 02:06 congwang-mk

Started on Phase 1 (transactional sequential pipelines) — one design note before I open the PR, since it deviates from the RFC's sketch and you invited a better idea if there was one.

The RFC gives each stage its own upper and a multi-layer lower-lookup chain ([own upper, stage_N upper, …, workdir]). Digging into SeccompCowBranch, it's strictly two-layer (one workdir lower + one upper), so that chain is a fair bit of core surgery. But because Phase 1 stages are sequential, a single shared upper over the common workdir gets the same read-committed + all-or-nothing for much less: stage N+1 reads through the same upper stage N wrote to, the real workdir is untouched until the end, so abort = discard the upper (free) and commit = one commit.

Trade-off: no per-stage change attribution and no single-stage retry — but those are Phase 2/3 in the RFC anyway. Per-stage uppers + the lower-lookup chain become genuinely necessary for Phase 2 (parallel siblings need snapshot isolation, where one shared upper can't work), so that's where I'd introduce them.

The working branch has Pipeline::run_transactional() + guardrails (same workdir, supervisor required, default branch actions, no chroot, matching storage/quota) + all-or-nothing commit/abort, plus a SeccompCowBranch Drop backstop so an aborted/timed-out/panicked transaction can't orphan its upper on disk. Coordinator owns the single commit/abort; wait()/Drop leave the shared branch alone. Integration tests cover commit-on-success, abort-with-no-leak (asserting the upper is reclaimed from disk), timeout-abort, read-committed hand-off, and the guardrails. Streaming run()/| is untouched.

I'll open the Rust-core PR shortly (then commit-hardening, then Python and Go, split small as before). Happy to switch to per-stage uppers now if you'd rather build the Phase-2 substrate up front — just flagging the tradeoff before the PR lands.

dzerik avatar Jul 11 '26 19:07 dzerik

Thanks for looking deeper into this, @dzerik . It looks reasonable to me. Looking forward to your PR's.

congwang-mk avatar Jul 13 '26 23:07 congwang-mk

While preparing the Phase 1 pipeline-transactions PR I hit a scope question on concurrent commit. Right now run_transactional takes no workdir-level lock: two transactions on the same workdir each build a private upper and merge file-by-file at commit with no coordination, so they interleave (last-writer-wins) rather than the first-committer-win you chose. Per-transaction all-or-nothing still holds; what's missing is inter-transaction serialization.

Two ways to close it for Phase 1 — which do you prefer?

(A) Implement first-committer-win now — an advisory flock on a workdir lockfile held for the create→commit lifetime:

let lock = std::fs::OpenOptions::new().create(true).write(true)
    .open(workdir.join(".sandlock-txn.lock"))?;
// Non-blocking: a second concurrent transaction fails fast instead of interleaving.
if unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } != 0 {
    return Err(/* "another transaction is active on this workdir" */);
}
// hold `lock` for the whole run; drop (or process exit) releases it.

(B) Defer — document in Phase 1 that concurrent transactions on one workdir are unsupported (last-writer-wins), and add flock in a later phase.

I'd lean (A) since it's ~10 lines and delivers the decision you already made, but it's your call on Phase 1 scope. Either way I'll pin the chosen behavior with a test.

dzerik avatar Jul 15 '26 10:07 dzerik

I am fine either way.

congwang-mk avatar Jul 18 '26 04:07 congwang-mk

Phase 1 is in for Rust callers after #148, but it never crossed into the bindings — a reference search on Transaction resolves to the lib.rs re-export and test_transaction.rs, nothing else. That also leaves the phase's own acceptance criterion open ("Python SDK covered by at least one end-to-end test"), so I'd like to take the tail: C ABI, then Python, then Go, one PR per layer along the lines of #118 → #120 → #123 → #124.

Sketching the shape here before writing it, in case you'd rather it looked different.

Build and run. Mirrors the Rust builder; run/dry_run consume the handle the way sandlock_pipeline_run does.

sandlock_txn_t*  sandlock_txn_new(void);
void             sandlock_txn_add_stage(sandlock_txn_t*, const sandlock_sandbox_t*,
                                        const char* const* argv, unsigned argc);
void             sandlock_txn_commit_lock_wait_ms(sandlock_txn_t*, uint64_t ms);
void             sandlock_txn_free(sandlock_txn_t*);

sandlock_txn_outcome_t* sandlock_txn_run(sandlock_txn_t*, uint64_t timeout_ms,
                                         int* err, char** err_msg);
sandlock_txn_outcome_t* sandlock_txn_dry_run(sandlock_txn_t*, uint64_t timeout_ms,
                                             int* err, char** err_msg);

Reading the outcome. Indexed getters, following sandlock_dry_run_result_*.

int    sandlock_txn_outcome_disposition(const sandlock_txn_outcome_t*);
size_t sandlock_txn_outcome_stages_len(const sandlock_txn_outcome_t*);
const sandlock_result_t*
       sandlock_txn_outcome_stage_at(const sandlock_txn_outcome_t*, size_t i);
size_t sandlock_txn_outcome_changes_len(const sandlock_txn_outcome_t*);
char   sandlock_txn_outcome_change_kind(const sandlock_txn_outcome_t*, size_t i);
char*  sandlock_txn_outcome_change_path(const sandlock_txn_outcome_t*, size_t i);
void   sandlock_txn_outcome_free(sandlock_txn_outcome_t*);

stage_at borrows from the outcome rather than cloning — cloning would copy every stage's captured stdout and stderr.

err carries a discriminant, emitted into the header as an enum:

Code Variant What the caller does
0 success outcome is non-NULL
1 Invalid fix the stage set
2 Branch no stage ran
3 Stage a stage could not be driven
4 Conflict reserved for Phase 2
5 CommitLock change set intact; retry is safe
6 Merge workdir partially modified; inspect
7 CommitAbandoned change set preserved, commit incomplete

-1 stays out of the enum for a NULL handle — a binding bug rather than a transaction outcome, same split as sandlock_sandbox_build.

Recovery ships with it. TxnError::CommitLock and ::Merge both leave the change set in preserved storage, and today only Rust can find it. So the same PR exposes list_preserved / read_preserved with the PreserveReason discriminant, since that is what says whether the workdir was touched.

Python and Go wrap the above with no behaviour of their own. In both, an aborted transaction is an ordinary outcome with disposition == Aborted, not an error — the error path is only where the core returns Err(TxnError). Go has no group API at all today, so Stage becomes new public surface there; I'd keep Pipeline and Gather out of this and leave them for a separate change.

Not in scope, to keep the PRs narrow: sandlock txn CLI subcommands, anything from Phase 2, and sandlock_pipeline_run's own null-on-every-failure behaviour.

Does this split and shape work for you, or would you rather it landed differently?

dzerik avatar Jul 29 '26 13:07 dzerik

Looks good to me.

congwang-mk avatar Aug 03 '26 18:08 congwang-mk