apply-edit
A single-file Python module providing one operation: search-replace patching of file contents. This is the primitive that agent harnesses (Cursor, aider, Claude Code, etc.) use to translate model-emitted edits into actual file changes.
A single-file Python module providing one operation: search-replace patching of file contents. This is the primitive that agent harnesses (Cursor, aider, Claude Code, etc.) use to translate model-emitted edits into actual file changes.
No runs yet — lineup hasn't been exercised against this task.
The raw documents handed to every model and every judge. Read these as the source of truth.
apply_edit.py — implementation specA single-file Python module providing one operation: search-replace patching of file contents. This is the primitive that agent harnesses (Cursor, aider, Claude Code, etc.) use to translate model-emitted edits into actual file changes.
The point of the function is not "replace text". str.replace already
does that. The point is to apply edits safely — raising loudly when
the edit is ambiguous or impossible, never silently mutating the wrong
location.
def apply_edit(
file_text: str,
old: str,
new: str,
*,
replace_all: bool = False,
) -> str: ...
Returns a new string with old replaced by new in file_text. The
input is never mutated (strings are immutable anyway, but: no other
side effects).
Three exception types, all module-level:
class EditError(Exception): ...
class EditNotFound(EditError): ...
class EditAmbiguous(EditError): ...
The two specific subclasses must inherit from EditError so callers
can catch either the specific failure or EditError as a base.
| Case | What must happen |
|---|---|
old is the empty string |
Raise ValueError. An empty needle is never a valid edit. |
old does not appear in file_text |
Raise EditNotFound. |
old appears exactly once |
Return file_text with that single occurrence replaced by new. |
old appears 2+ times and replace_all=False |
Raise EditAmbiguous. Do not silently replace the first match. This is the whole reason this function exists rather than str.replace. |
old appears 2+ times and replace_all=True |
Replace every occurrence. Return the new string. |
old == new |
Still validated for presence/ambiguity per the rules above; if it would otherwise succeed, return file_text unchanged. |
file_text may contain \n, \r\n, or a mix. The function operates
on the string as given; it does not normalize line endings.str, not bytes. Callers handle encoding.Exception messages must be informative enough for an agent to react:
EditNotFound: include the first ~80 chars of old (truncated with
… if longer) so logs show what was searched for.EditAmbiguous: include the match count, e.g.
"old string matched 4 times; pass replace_all=True to replace all".python apply_edit.py <path> <<EOF
<<<<<<< OLD
<old text>
=======
<new text>
>>>>>>> NEW
EOF
The CLI reads a single edit block from stdin in the format above
(literal <<<<<<< OLD, =======, >>>>>>> NEW markers; no leading
spaces), applies it to the file at <path>, and writes the result back
to that path.
EditNotFound. Print the exception message to stderr.EditAmbiguous. Print the exception message to stderr.--replace-all flag: if passed, set replace_all=True for the call.The CLI must not require any third-party libraries.
apply_edit: no logging, no I/O, no
global state. The CLI is a separate main() that does I/O.apply_edit itself.apply_edit.pyRead SPEC.md in this directory. Implement apply_edit.py per spec:
one library function (apply_edit), three exception classes
(EditError, EditNotFound, EditAmbiguous), plus a CLI entry point.
This task covers only apply_edit.py. Do not create helper modules,
test files, or packaging metadata.
Below is a starter implementation that someone tried to ship. It has
at least one bug — at least one case where it does not match the
behaviour required by SPEC.md. Your job:
apply_edit.py from scratch (do not paste this in
verbatim).# bug in reference: <one line>.You are not required to keep the reference's structure. Use whatever shape is cleanest. The only requirement is that the resulting module passes the spec.
class EditError(Exception):
pass
class EditNotFound(EditError):
pass
class EditAmbiguous(EditError):
pass
def apply_edit(file_text, old, new, *, replace_all=False):
if not old:
raise ValueError("old must not be empty")
if old not in file_text:
raise EditNotFound(f"old string not found: {old[:80]!r}")
if replace_all:
return file_text.replace(old, new)
return file_text.replace(old, new, 1)
def main():
import sys
if len(sys.argv) < 2:
print("usage: apply_edit.py <path> [--replace-all]", file=sys.stderr)
sys.exit(1)
path = sys.argv[1]
replace_all = "--replace-all" in sys.argv[2:]
raw = sys.stdin.read()
# parse <<<<<<< OLD ... ======= ... >>>>>>> NEW block
try:
head, rest = raw.split("<<<<<<< OLD\n", 1)
old, rest = rest.split("\n=======\n", 1)
new, _ = rest.split("\n>>>>>>> NEW", 1)
except ValueError:
print("malformed stdin", file=sys.stderr)
sys.exit(1)
with open(path, "r") as f:
contents = f.read()
try:
result = apply_edit(contents, old, new, replace_all=replace_all)
except EditNotFound as e:
print(str(e), file=sys.stderr)
sys.exit(2)
except EditAmbiguous as e:
print(str(e), file=sys.stderr)
sys.exit(3)
with open(path, "w") as f:
f.write(result)
if __name__ == "__main__":
main()
pip install, no new dependencies.apply_edit must be a pure function — no I/O, no logging, no globals.SPEC.md
(specific classes inherit from EditError).A single file apply_edit.py at the worktree root that:
EditError, EditNotFound, EditAmbiguous.apply_edit(file_text, old, new, *, replace_all=False) -> str
matching the spec exactly.SPEC.md's "CLI" section, with the exit-code
contract (0 / 1 / 2 / 3) and the --replace-all flag.replace_all raises; old="" raises ValueError;
old not in text raises EditNotFound.apply_edit.py."Fill one copy per implementation, saved as output/<label>_rubric.md.
Also write output/<label>_scores.json with the structured form (see
JUDGE_PROMPT.md).
Implementation reviewed: <label> (e.g. A, B, C)
File: implementations/<label>.py
Cite line numbers when something fails.
apply_edit.py provided as <label>.pyapply_edit(file_text, old, new, *, replace_all=False) -> str matches SPEC signatureEditError, EditNotFound, EditAmbiguousEditNotFound and EditAmbiguous both inherit from EditErrorapply_edit is pure: no I/O, no global state, no logging inside itHard-fail result: pass / fail If fail, reasons (with line refs):
Award 1 point per item present and correct. Cite line numbers.
old == "" raises ValueError (not EditError, not silent return)old not in file_text raises EditNotFoundEditNotFound message includes (a truncated form of) oldfile_text with that one occurrence replacedreplace_all=False: raises EditAmbiguous (NOT silently replaces first — this is the bug in the reference)EditAmbiguous message includes the match countreplace_all=True: replaces every occurrence--replace-all flag wired through to the callSubtotal: __ / 10 Notes:
# bug in reference: line naming what was wrong; otherwise comments only at non-obvious points: __Subtotal: __ / 20
Did the model correctly identify the bug in the reference? The expected
diagnosis is: "silently replaces only the first occurrence on
multi-match instead of raising EditAmbiguous". Note in the rubric
whether the model's # bug in reference: comment matches.
ship-with-cleanup / rewrite / unusable