edit_file
Apply one or more exact string edits to a file, gated by a sha256 of the version the agent last read.
edit_file
Replace one or more exact text regions inside an existing file without rewriting it whole. Every edit names an oldText that must match the original file exactly (CRLF and BOM preserved) and a newText to put in its place. Like write_file, edit_file requires an expectedSha256 of the pre-edit content, so concurrent modifications are detected up front.
import { createAgent } from "@comma-agents/core";
const agent = createAgent({
name: "editor",
model: "openai/gpt-4o",
tools: ["read_file", "edit_file"],
});EditFileToolConfig
Optional configuration for the factory.
Prop
Type
AppliedEdit
One entry in data.appliedEdits.
Prop
Type
MatchRange
The byte range that was replaced.
Prop
Type
EditFileData
The full structured payload.
Prop
Type
Edit semantics
Each edit is matched against the original pre-edit snapshot of the file, not against the file as previous edits have left it. This means edits cannot accidentally fix up each other's matches, and overlapping edits are detected before anything is written.
oldTextmust be present exactly once in the snapshot unlessreplaceAll: trueis set.- Multiple matches without
replaceAllreturnmultiple_matches. - An
oldTextthat is not found returnsold_text_not_found. - Two edits whose match ranges intersect return
overlapping_edits— the whole call fails atomically.
await tool.edit_file({
path: "src/server.ts",
expectedSha256: read.data.sha256,
edits: [
{ oldText: "const PORT = 3000;", newText: "const PORT = 4000;" },
{ oldText: "console.log(", newText: "logger.info(", replaceAll: true },
],
});Newline and BOM preservation
edit_file operates on the byte content as it is on disk. CRLF line endings inside oldText must be matched verbatim (use the value returned by read_file, which is LF-normalized for content but preserves the underlying style for the write). The BOM is never altered.
Errors
| Kind | When |
|---|---|
not_found | The path does not exist or is a directory. |
stale_file | expectedSha256 does not match the bytes on disk. Re-read and retry. |
old_text_not_found | An edit's oldText does not appear in the file. |
multiple_matches | An edit's oldText matches more than once without replaceAll: true. |
overlapping_edits | Two edits would replace intersecting byte ranges. |
outside_workspace | The path escapes the sandbox or is absolute without allowAbsolutePaths. |
permission_denied | The path is write-blocked by forbiddenGlobs or sandbox policy. |
Related
- write_file — when the whole file changes.
- apply_patch — for multi-file diffs and renames.
- Stale file protocol — the full sha256 lifecycle.