Write Grants and Isolated Workspaces: Limiting Which Files an Agent Can Change
StageWriteGrantV1 compresses task permission into a one-time capability bound to a version, paths, operations, and an executor instance, enforced jointly by an isolated worktree and layer-by-layer readback.
On this page · 10 sections
Once an Agent has file tools, it usually has very broad technical capability: it can open the repository, create files, and possibly run commands that write to disk indirectly. But the permission a task actually grants it is often very narrow, for example: “on this feature branch, modify only two files, and do not touch the user's existing changes.” Mature write control has to keep these apart:
- Capability: what the process and tools can do at the operating-system level;
- Authority: what the user and the current task allow this execution to do;
- Enforcement: which checks still stop an out-of-scope result from reaching the source workspace even if the executor misunderstands its instructions.
Natural language can express the intent of an authorization, but it is not fit to serve as the enforcement boundary on its own. The Marshall Harness therefore encodes write permission as StageWriteGrantV1 and then puts untrusted execution into a separate worktree. The former answers “who may change which targets under what preconditions”; the latter answers “what gets contaminated if execution fails.”
The Short Version
- Being able to write files (Capability) is not authorization to write this file (Authority); natural language can express the intent of an authorization, but it is not fit to serve as the enforcement boundary on its own.
StageWriteGrantV1binds the authorization to a workflow identity, an executor instance, an exact Git baseline, target preconditions, and a per-path operation, and it can be claimed only once within its validity window.- Execution runs in a separate detached worktree. A worktree is a failure domain, not proof of permission: every change is still compared against the Grant's allowlist.
- Verification repeats at several layers, from before issuance to each file written to disk; when the evidence of permission is incomplete, the process fails closed rather than guessing how to merge.
From task intent to a one-time capability
In the write path, the main Agent remains the owner of the permission. The executor merely accepts a narrow grant and produces a candidate result; it cannot widen the scope on its own, and it cannot write the result straight back into the source workspace.
This path has one key property: the executor having write capability over the isolated workspace does not mean it has the capability to write to the source workspace; being able to produce a file does not mean that file will be accepted. What actually crosses the boundary is a candidate state that has been authorized, verified, and read back.
What StageWriteGrantV1 binds
A Grant is not a sentence saying “code changes are allowed.” It is a structured contract that cannot be interpreted loosely. The Schema uses additionalProperties: false, requires the caller to write out the authorization context in full, and does not let unknown fields quietly change the semantics.
The table below lists the fields a Grant binds, by dimension. Look at the rightmost column: the problem each group of fields is there to solve.
| Binding dimension | Core fields | Problem it solves |
|---|---|---|
| Workflow identity | audit_id, workflow_version, stage, attempt, run_id, generation, resume_epoch, issued_revision |
Prevents a grant from a previous round or another workflow from being carried into this one |
| Concurrent ownership | lease_fencing_token |
A former holder that arrives late cannot overwrite an execution a new lease has already taken over |
| Node and executor | write_grant_node_id, isolated_executor_node_id, executor_authority, executor_instance_id |
Binds the grant to a specific node, a specific executor instance, and a specific execution authority |
| Basis for the decision | plan_ref, branch_check_ref, isolation_attestation_ref |
References the change plan, branch check, and isolation attestation by path and SHA-256, so the basis cannot be swapped after issuance |
| Task origin | jira_binding_ref or incident_binding_ref; exactly one of the two must be present |
Proves that the branch matches one verified task origin, rather than a scope the executor reports for itself |
| Repository snapshot | repository_identity, branch, base_commit, base_tree |
Pins a specific repository, a non-protected branch, and an exact Git baseline |
| Target preconditions | target_preconditions, target_preconditions_sha256 |
Records each target's path, exists, sha256, mode, and index_sha256 |
| Write scope | write_allowlist, write_allowlist_sha256 |
Allows only one of create, modify, or delete for each path |
| Lifecycle | grant_id, status, max_uses, uses, issued_at, expires_at |
Limits the grant to a single claim within its validity window and supports exact reconciliation |
target_preconditions and write_allowlist must cover the same sorted, duplicate-free set of paths. The operation must also fit the target's state: a target that does not exist can only be granted create, and a target that exists cannot be passed off as a creation. Each of the two sets is then hashed as a whole, so that nobody can replace just the arrays while keeping the surrounding Grant identity.
This design is close to the principle of least privilege: what is granted is not an abstract “write to the repository,” but “executor instance X may perform operation O on path Z against baseline Y.” AC-6 in NIST SP 800-53 defines least privilege as granting only the minimum permissions needed to complete a task; the Grant applies that principle at the granularity of a single code change.
What single use, expiry, and the fence each guard against
These three limits look alike, but they guard against three different kinds of failure:
max_uses: 1prevents several Executors from racing for the same grant. A claim, that is, an Executor taking up the grant, is bound to a uniqueExecutorRunV1, and a repeated claim must be reconciled against the existing record rather than obtaining another chance to write.expires_atbounds the authorization window. An execution result that arrives after expiry is only a late candidate artifact; it cannot open a new write operation on its own.lease_fencing_tokenhandles concurrent takeover. A timeout alone cannot stop an old process from resuming; a monotonically increasing fence lets the Host recognize that “this result belongs to an executor that has already lost ownership.”
If the promotion journal has already started under the exact terms within the authorization window, crash recovery can continue under a new lease for the same stage; it must still prove that the new fence is no earlier than the original Grant's, and it must carry over the same operation, bundle, and target states. What is recovered is a transaction that has already begun, not a fresh authorization for a late executor.
A worktree is a failure domain, not proof of permission
Isolated execution uses git worktree add --detach <path> <base_commit> to create a separate checkout. The runtime then reads back HEAD and HEAD^{tree}, which must equal the Grant's base_commit and base_tree respectively. The worktree lives in an owner-only private directory outside the repository, and its .git pointer must also point back to the expected Git common directory.
It mainly shrinks the blast radius of three kinds of failure:
- When an Executor generates a large number of wrong files, the checkout the user is working in is not directly overwritten;
- when execution is interrupted, the half-finished output stays in a directory that can be inspected in isolation, rather than being mixed into the user's uncommitted changes;
- multiple Executors can each have their own checkout, which avoids treating a single working tree as shared mutable memory.
But a worktree is not an authorization system in itself. A process can still modify files outside the allowlist within the isolated checkout. So the packaging phase independently reads git status --porcelain -z and compares every change against the Grant's exact allowlist; if any extra path shows up, the entire candidate result is rejected.
The isolation attestation also constrains capabilities such as network access, MCP, plugins, sub-Agents, external mutations, and source Git writes, to keep the executor from bypassing the file contract and producing side effects through another channel.
This also shows the true extent of the security boundary: a worktree isolates mistakes and tool failures very well, but it cannot stand up to a malicious process that already controls the same host and the same user identity. The Host, operating-system permissions, and the platform sandbox remain the outer roots of trust.
TOCTOU: correct at issuance does not mean still correct at write time
There is a time window between checking a target and actually writing to it. The user may have pulled new commits, switched branches, or modified the target file, and another process may have replaced a directory or a symbolic link. Checking only once, when the Grant is issued, leaves a classic TOCTOU (time-of-check to time-of-use) vulnerability.
This contract re-verifies at several layers:
- Before issuance: record the repository identity, branch, HEAD, tree, target content hashes, mode, and index state;
- After creating the isolated area: read back the detached worktree's commit/tree;
- Before packaging: read the old and new states from the Git baseline and the execution result respectively, and confirm that old matches the Grant's preconditions;
- Before promotion: check the source repository identity, branch, HEAD/tree, target index, and the target state in the working tree once more;
- As each file is written to disk: hold the chain of descriptors from the repository root to the parent directory, confirm before writing that the target is still in the old state, and confirm after the atomic replace that it has become the new state.
So “the repository is dirty” is not a precise enough reason to refuse. User changes on non-target paths can be left alone; the real hard boundary is that the authorized targets must match the recorded working-tree and index preconditions. The moment a target turns into a third state that is neither the expected old nor the expected new, the process stops rather than guessing how to merge.
A path is not an ordinary string
Path validation has to finish before anything reaches the file system. The Grant and Patch contracts accept only canonical, repository-relative POSIX paths, and reject:
- absolute paths, empty paths, NUL, and backslashes;
..,., empty components, and.gitcomponents;- targets that are duplicated or that collide after case folding;
- a create/modify/delete that is inconsistent with the authorized operation.
Reading the execution result also uses no-follow semantics, that is, it does not follow a link to read whatever it points at: symbolic links, non-regular files, files over the size limit, and files with multiple hard links all fail. Promotion cannot rely on a single string-prefix check; the device/inode of the parent directory chain and of the leaf file are verified repeatedly before and after open, read, and replace, which reduces the risk of a symlink swap or directory replacement slipping past the ../ check.
How failure should look
Mature write isolation does not try to get as much done as possible. It fails closed, that is, it refuses by default, when the evidence of permission is incomplete. In the table below, the left column is the failure scenario and the right column is the correct handling:
| Failure scenario | Correct handling |
|---|---|
| The Executor modified a path outside the allowlist | No promotable Bundle is produced; the isolated area is kept for diagnosis |
| The user modified a target path after issuance | Refuse promotion, re-read the target, and replan; do not overwrite the user's version |
| The output is a symlink, a directory, or a hard link | Refuse to read or package it; do not follow the link |
| The branch, HEAD, or tree has changed | The Grant is invalid; it cannot keep being used by updating just one base field |
| The Executor returns only after the grant has expired | Mark it as a late result; unless a journal was already started within the window, no new write permission arises |
| A process from an old lease keeps running | The fence check fails; the result cannot enter the Host's write path |
| Worktree creation fails halfway | Quarantine and reconcile the orphan; do not automatically clean up a directory that may still hold evidence |
Trade-offs and where it applies
This mechanism pays a fairly high implementation cost for three properties: precise authorization, isolation of the user's workspace, and a recoverable chain of evidence. The costs are concrete as well: every write needs a plan and preconditions, a Grant lifecycle, an extra worktree, file hashes, an isolation attestation, and cross-boundary verification; when targets change frequently, an invalidated baseline forces reruns.
It fits best where:
- the executor is less trusted;
- tasks run for a long time;
- several executors run concurrently;
- the user's workspace holds high-value uncommitted changes;
- the change touches security- or production-related code.
For local changes that can be reviewed on the spot and have a very small blast radius, a runtime of the same strength may not be worth it; the permission boundary should still be kept, but it can be implemented through a feature branch, the platform sandbox, a precise diff, and verification proportionate to the risk.
References
- Git: git-worktree: linked worktrees, detached worktrees, and the repository metadata model.
- NIST SP 800-53 Rev. 5: access-control principles such as AC-6 least privilege.
- OpenAI: Running Codex safely: the combined boundary of sandboxing, approvals, and risk tiering.
- Anthropic: How we contain Claude: the practice of treating model instructions, tool permissions, and operating-system isolation as separate lines of defense.
Related notes
How My AI Development Workflow Changed: From a Harness to Lightweight Prompts to Enterprise Agents
First a self-built Harness to make up for what models could not do, then a cut down to lightweight Prompts and four gates after Codex 5.6, then the same principles applied to enterprise Agents. What each step removed and kept.
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.
Working on something similar?
I'm always happy to compare notes on distributed systems, delivery, and applied AI.
[email protected]