pub fn safe_text_patch(
path: &Path,
content: &str,
expected_hash: Option<&str>,
session_id: Option<&str>,
create_parents: bool,
overwrite: bool,
) -> Result<SafeTextPatchOutcome, HostlibError>Expand description
Atomic compare-and-swap-style text write.
Reads the current bytes at path through the staged-fs overlay (when a
session is active) so concurrent agent edits see each other’s pending
writes. If expected_hash is supplied and differs from the observed
sha256:HEX, returns SafeTextPatchResult::StaleBase without
mutating any state. On a hash match the post-image is written through
the same overlay path, keeping the read and the write atomic with
respect to other staged-fs consumers in the same process.
Atomicity:
- When a session is in staged mode, the read, hash check, and write all happen under a single acquisition of the sessions mutex, so a sibling thread cannot stage a write into the window between the pre-image snapshot and the commit.
- When the call routes through disk (no active session, or session in immediate mode), the write goes through an atomic rename-into-place so partial-write tearing is impossible. Cross-process races are intentionally out of scope — the staged-fs overlay is the collision-rejection layer.