apply_patch. Providers may describe a file change in their own event format, but the gateway routes supported mutations through the native engine or records a provider aggregate with an explicit authority and exactness label.
Patch history exists to answer a practical question after a turn: what changed, which execution made the change, and how certain is the recorded diff? The answer is stored as durable records rather than reconstructed from a live process or a provider’s last message.
Mutation pipeline
The native path processes a patch in bounded stages:- normalize the request and enforce input limits;
- parse patch syntax and validate version guards;
- resolve every target against the workspace and allowed roots;
- authorize the operation with the turn permission and file policy;
- prepare an exact virtual workspace plan;
- acquire target locks and compare file versions;
- stage file content and directory side effects;
- commit the planned changes atomically where the platform allows;
- persist snapshots and a patch history record;
- publish the applied step and update projections.
Durable record
EachPatchHistoryRecord contains:
- schema version, record id, thread, turn, invocation, and environment ids;
- commit ordinal and commit timestamp;
- authority such as
native_patch_engine,codex_aggregate_event,managed_claude_patch_engine, orunsupported; - provenance such as
native_engine,managed_claude,recovery,provider_aggregate, orunknown; - record exactness:
exact,partial, oruncertain; - outcome:
applied,partial,commit_state_uncertain, orgap; - one or more changes and their before/after snapshots;
- directory side effects and metadata warnings.
Coverage and exactness
The query response reports more than a list of files. It includes:
This distinction matters during recovery. A provider may have emitted a final aggregate diff while the gateway was restarting. The UI can show the change, but it should not label it as exact until native history has verified it.
Views and queries
The same records are projected at several scopes:- timeline view shows applied steps in a thread or turn;
- turn aggregate view combines the records for one turn;
- record diff view returns one selected record or a bounded unified patch;
- file history view follows one path across the execution scope visible from a thread.
Recovery and uncertain outcomes
File mutation crosses a process and filesystem boundary, so a crash can happen after the file changed but before the history row or notification was written. Startup repair and the patch observer reconcile projections. They do not invent an exact record when the commit state cannot be proven. Possible outcomes are:
The same approach is used for Codex and managed Claude adapters. Their provider-native events are retained as provider aggregates when the native engine cannot provide exact steps. Recovery events can later improve coverage without changing the meaning of the original observation.
File viewer boundary
The source-file workflow uses patch and thread context as a navigation surface. A markdown link can identify a workspace file and an optional line or column. Mobile and FFI clients ask the gateway for a short-livedthread/file/view_grant/create grant, then fetch an authenticated HTTP representation. The gateway resolves the path against the authorized thread and serves a bounded text file. Desktop handles local file targets with its configured OS opener and does not send an arbitrary local desktop path to a remote gateway.
This is separate from artifact delivery. Artifacts are durable uploaded or generated blobs. Thread file viewing reads a gateway workspace file under the current thread’s authorization. See File viewer and Artifacts architecture for the two client workflows.
Code map
Related pages
- Patch history API lists pagination, record, diff, and view-grant methods.
- Tools architecture explains permission admission and native tool routing.
- Permission system explains the turn security snapshot used before mutation.
- Artifacts architecture explains durable file storage and artifact HTTP delivery.
- File viewer describes the end-user source-file workflow.