Patch Promotion: Writing Changes from an Isolated Environment Back Safely

PatchBundleV1 packages candidate file states from an isolated environment into a verifiable artifact, which a trusted Host then writes in journaled steps, reads back, and seals with an immutable receipt.

On this page · 14 sections

After an isolated Executor finishes its changes, the source workspace still should not trust it directly. Patch Promotion addresses a local trust boundary: how to write candidate file states from a low-trust execution environment, in a controlled way, into the source worktree the user is actually working in.

Promotion here is not deployment, release, or a Git merge, and it does not automatically commit or push. It does exactly one thing: while the source repository still satisfies the original authorization's preconditions, it writes a verified set of old → new file states to the target paths and leaves behind enough evidence for readback, retry, and recovery.

The Short Version

  • The unit of transport is PatchBundleV1, not a text diff: it binds the authorization, the repository baseline, a two-state old/new record per file, and immutable blobs.
  • The trusted Host is the only actor allowed to modify the source worktree; every file gets an old-state check, an atomic write to disk, and a new-state readback.
  • A multi-file Bundle is not one atomic file-system transaction; the partial-commit journal provides a detectable, recoverable multi-step commit and effect-level idempotency.
  • The two receipts prove integrity, authorization, and target readback. They do not prove business correctness, a commit, CI, or a successful deployment.

Why not just apply a diff

A text diff is well suited to human review, but it is not a complete write contract. On its own it usually cannot answer:

  • Which Grant and which Executor run did this patch come from?
  • What were the file's exact content, mode, and existence before the change?
  • Where do the authoritative bytes of a new file or a binary file live?
  • How does a delete operation prove the target has not been replaced by someone else?
  • If the process crashes after handling some of the files, should a retry skip the ones already handled or rewrite them?

So the unit of transport is not “a diff that might apply,” but PatchBundleV1: a manifest that binds the authorization, the repository baseline, a two-state record per file, and immutable blobs. Git's git apply --check is still a good reference for understanding the separation of check and apply, but this mechanism treats file states and content hashes as the facts of permission; a patch's context match is not the final basis.

PatchBundleV1: freezing the candidate result

The Bundle's top-level fields bind the complete provenance:

Field Meaning
bundle_id, bundle_sha256 The Bundle's content-addressed identity; a change in the projected content yields a different identity
grant_id, grant_sha256 Binds the exact Grant bytes that issued the write permission
executor_run_id Binds a unique execution instance, instead of accepting output from any Executor of the same kind
repository_identity Names the source repository identity, to avoid misdelivery between directories with the same name
base_commit, base_tree States which Git snapshot the candidate changes were produced against
changes, change_count, total_bytes The ordered, deduplicated per-file changes and the resource limits
created_at Records when the artifact was formed; it does not replace the Grant's authorization window

Each changes[] entry contains path, operation, and two fileState values, old and new. A fileState in turn contains:

  • exists: whether the file exists in this state;
  • sha256: the exact content hash when it exists;
  • mode: the file permission bits;
  • bytes: the content length, used for resource limits and integrity re-checks;
  • blob_ref: the path and SHA-256 of an immutable content blob in the audit directory.

Create, modify, and delete can therefore be represented uniformly; the only difference is which side, old or new, is exists: false:

operation old new
create exists: false Contains the new content, mode, and blob
modify Contains the old content state Contains the new content state
delete Contains the old content state exists: false

The Bundle builder does not trust a file list reported by the Executor. It independently reads the isolated worktree's Git status, the baseline blobs, and the current file bytes and mode, then checks that every path falls entirely within the Grant allowlist.

All old/new blobs are re-read, hashed, and sealed; the Bundle ID binds the Grant, run, base, and the complete changes. As a result, the later steps deal with a frozen artifact, not a working directory that is still changing.

What the verification receipt proves, and what it does not

Before promotion, a PatchVerificationReceiptV1 is generated. It binds grant_id/grant_sha256, executor_run_id, and bundle_id/bundle_sha256, and records the status and evidence_sha256 of each check. The core checks in the implementation are:

  1. The Bundle manifest and every immutable blob can be re-read and pass hash verification;
  2. every path + operation exactly matches the Grant's write allowlist;
  3. the Bundle's old states exactly match the Grant's target preconditions.

Only when all of them pass is the receipt's status passed. The receipt has its own projection hash too, and a receipt that already exists at the same path must match the expected bytes exactly.

One distinction needs particular care: this is an integrity and authorization verification receipt, not a proof of business correctness. It can prove that “these bytes came from this authorized execution and may enter these paths under these preconditions”; it cannot by itself prove that the code logic is correct, that test coverage is adequate, or that production behavior meets expectations. Unit tests, type checks, builds, and runtime verification belong to the verification contract one layer further out.

The Host promotion protocol

The trusted Host is the only actor allowed to modify the source worktree. The complete path is:

Sequence diagram of the Host promotion protocol: the Isolated Executor seals a PatchBundleV1, the Trusted Host verifies, locks the repository, writes and reads back each change, then issues the PromotionReceiptV1; open the full-size SVG

1. Reconfirm runtime permission

The Host checks the workflow/stage/attempt/run/generation/resume epoch in StateV3 against the Grant, requires the current lease and the correct fence (that is, a time-limited exclusive hold by one executor, and a token used to reject a stale holder), and verifies that the Grant did not expire before the transaction began. The source repository identity is derived jointly from the repository root and the Git common directory, to avoid delivering the patch to a different checkout.

2. Lock and re-verify the target repository

Promotion uses a process lock and a file lock under the Git common directory to serialize promotions against the same repository. The lock file is opened with no-follow (that is, without following symlinks), and its regular-file status, link count, and inode are checked to reduce the risk of the lock path being replaced.

Inside the lock, the Host reads again:

  • the current branch, HEAD, and HEAD^{tree};
  • the Grant's base_commit/base_tree and the Bundle's baseline;
  • each target's index stage, blob hash, and mode;
  • whether the target in the working tree currently equals exactly the Bundle's old state or its new state.

Any of these turning into a third state counts as target drift. The system does not attempt a three-way merge, nor does it take the new HEAD and guess how to replay the old Bundle.

3. Give the operation a deterministic identity and a journal

The operation_id is derived from the Grant hash, Bundle hash, verification receipt hash, and repository identity. The same set of terms yields the same operation; different terms cannot borrow an old journal.

The partial-commit journal held in state sets up an ordered step for each target, followed by outcome and receipt. Each step has only three states, pending, completed, and failed, and from these the transaction as a whole becomes in_progress, needs_recovery, or completed. The Event ledger separately stores one exact decision → action → outcome causal chain.

4. Every file gets an old-state check, an atomic write to disk, and a new-state readback

For create/modify, the Host goes through these steps in order:

  1. create a randomly named temporary file in the target's own directory;
  2. write the immutable blob, set the mode, and call fsync;
  3. confirm the target is still in the old state;
  4. perform an atomic replacement with os.replace, followed by another fsync on the parent directory.

For delete, the Host holds the target's descriptor, repeatedly verifies the device/inode, owner, link count, and old state, and then executes unlink.

Path resolution does not end with a single resolve(). The runtime holds the chain of descriptors from the repository root to the target's parent directory, refuses to follow symlinks, and verifies the identity of the parent chain and the leaf before and after each read, write, and replace/unlink. Only after a file has been completed and read back in the exact new state can the corresponding journal step be marked completed.

A caveat: replacing a single file can be atomic, but a multi-file Bundle is not one atomic file-system transaction. What the partial-commit journal provides is a multi-step commit that is detectable and recoverable. It does not pretend that all files change at the same instant.

Recovering after a partial apply

Suppose a Bundle contains several files and the Host crashes after completing some of them. The recovery flow does not blindly apply from the beginning:

  1. Reload the same deterministic operation_id, the decision/action events, and the journal;
  2. verify that the recovery lease belongs to the same stage and that its fence is no earlier than the original Grant's;
  3. reclassify every target: exactly equal to old, exactly equal to new, or in a conflicting state;
  4. steps the journal marks as completed whose target is new are simply confirmed;
  5. steps that are incomplete and whose target is still old can continue;
  6. when a target is neither old nor new, or a completed step has lost its new state, fail closed (that is, refuse to continue by default) and move to manual reconciliation.

What this provides is effect-level idempotency, not a claim that the file system performed the write exactly once. A repeated call may run the checks again, but the same operation can only ever converge on the same set of new states and the same receipt. An existing receipt can be reused if its bytes match exactly; different terms appearing at the same path are an outright conflict.

The system also does not roll back completed files by default. An automatic rollback could overwrite new changes the user made after the crash, and the inverse of a delete or create is not necessarily still safe. The more reliable strategy is to keep the journal and the exact old/new evidence: continue when it can be proven that things are still in the original transaction's state; otherwise stop and let a human decide whether to continue, restore, or generate a new Grant.

PatchPromotionReceiptV1 is a target readback, not proof of release

Only after every target has reached its new state does the Host generate a successful outcome and a PatchPromotionReceiptV1. The receipt contains:

  • the exact hashes of the Grant, Bundle, and verification receipt;
  • the deterministic operation_id;
  • the target readback of exists, sha256, and mode for each path;
  • the three event IDs for decision, action, and outcome;
  • promoted_at and the receipt's own hash.

What it proves is that “the source worktree was read back as this set of target states at that moment.” It does not prove the code has been committed, it does not prove the remote branch has received a commit, and it certainly does not prove that CI, deployment, or production readback succeeded. Blurring these levels together misreports local file evidence as delivery evidence.

Common failure modes

Each row in the table below is one failure: look first at the detection point that catches it, then at how the system handles it.

Failure Detection point Handling
The Bundle is replaced or blob content changes Bundle/receipt projection hashes and blob re-reads Refuse promotion
A path or operation exceeds the authorization Exact comparison of the Grant allowlist with the Bundle changes Reject the entire Bundle
The user modifies a target while waiting old/new classification, index, and HEAD/tree readback Stop; do not overwrite the user's state
The target's parent directory is swapped for a symlink no-follow descriptor chain and inode verification Stop and mark the step failed
A crash while writing the Nth file partial-commit journal + per-target readback Recover under the same terms, or move to manual reconciliation
The client times out but the Host actually finished Deterministic operation ID, existing receipt, target new state Read back the facts; do not create a second transaction
Functional tests fail The outer verification/test node Do not mistake the integrity receipt for functional acceptance

Trade-offs and where it applies

Patch Promotion uses a manifest, immutable blobs, two kinds of receipts, a repository lock, an event chain, and a recovery journal to buy a strong boundary between a low-trust Executor and a high-value source workspace. It is particularly suited to concurrent execution, long tasks, interruptible processes, users with important uncommitted changes, or situations where one must later answer “who wrote which bytes, and on what authorization.”

The cost is that both the protocol and the implementation are heavy: the more files there are, the more expensive hashing, blob storage, and per-target fsync become; paths, permission bits, letter case, and the Git index all become compatibility surfaces; and the recovery code has to cover more states than a normal write does.

For a change that has a single executor, is low-risk, and can be reviewed by a human right away, a feature branch, the platform sandbox, git diff, and tests may already provide enough evidence. The criterion is not how rigorous a mechanism looks, but whether the expected loss it reduces exceeds the ongoing cost of maintaining and running it.

References

  • Git: git-apply: --check, the boundaries of application, and text-patch semantics, as a point of comparison for the separation of check and apply.
  • OpenAI: Harness engineering: assembling repository knowledge, mechanical constraints, and Agent feedback loops into a maintainable engineering environment.
  • SLSA Provenance v1.2: a supply-chain model of artifacts, build processes, and verifiable metadata. This article borrows only the design analogy of “binding content to its origin” and does not claim the local Bundle conforms to SLSA.
  • in-toto: Getting started: the basic idea of step artifacts and attestation chains. The receipts in this article are a local runtime contract and are not equivalent to in-toto attestations.

Related notes

Working on something similar?

I'm always happy to compare notes on distributed systems, delivery, and applied AI.

[email protected]