remote_patch
Apply patches to remote files using hash-based conflict detection, so edits fail safely if content changed and can be retried after re-reading.
Instructions
Apply patches to a remote file with hash-based conflict detection.
Workflow:
Call remote_read to obtain content + file_hash + range_hash for each region.
Call remote_patch with the SAME file_hash and per-patch range_hash.
If the file was modified by anyone else in between, this call returns {"result": "error", "reason": "Content hash mismatch ...", "current_file_hash": ...} — re-read and try again. The remote file is untouched.
patches_json must decode to a list of patch objects: [{"start": int, "end": int|null, "contents": str, "range_hash": str}, ...]
Notes:
Patches are applied bottom-to-top so line numbers stay valid.
Overlapping patches are rejected.
Writes are atomic (tmp file + rename) and re-hashed after write.
When auto_newline is true, missing trailing newlines on patch contents are auto-appended only if the slice they replace ended with one. The result includes a "warnings" list either way.
use_sudo: read+write the file as root, for a root-owned file the SSH user can't touch. Keeps the SAME hash safety + atomicity as the normal path: content is read via
sudo cat, staged over SFTP into the user's private home, then placed with one sudo step (cp → temp beside the target → restore owner/group/mode → atomic rename). The target must already exist; the sudo password is resolved out-of-band per host and the result is flagged high_risk. (Implementation note in the README.)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | ||
| path | Yes | ||
| encoding | No | utf-8 | |
| use_sudo | No | ||
| file_hash | Yes | ||
| auto_newline | No | ||
| patches_json | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |