Apply a unified-diff hunk or search/replace edit to a file by content, refusing ambiguity

$0.005 USDC per call · Agent infrastructure · POST /api/v1/patch-anchor · Id patch-anchor

Applies unified-diff hunks (wrong line numbers are fine) or search/replace blocks to a supplied file text and returns the patched text, for an agent whose edit tool failed or whose diff has drifted line numbers. Each hunk is located by its content against the original file, trying exact, trailing-whitespace, outer-whitespace then collapsed-whitespace matching; the rung used is reported per hunk. If a hunk matches more than once it is refused and the candidate lines with similarity scores are returned; if any hunk is missing, ambiguous or overlapping nothing is applied (never a partial patch). Does not run tools, touch files, handle multi-file, new-file, rename or binary diffs, or match inside a line: edits and hunks match whole lines.

What you get

Stops a stale line number or an ambiguous anchor from silently corrupting a file.

Call it

Send the JSON body below to https://agents.writerslogic.com/api/v1/patch-anchor. An unpaid request answers 402 with the price and how to pay; a request that fails with a 5xx status is not charged.

curl -i -X POST https://agents.writerslogic.com/api/v1/patch-anchor \
  -H 'content-type: application/json' \
  -d '{"file":"function add(a, b) {\n  return a + b;\n}\n\nfunction sub(a, b) {\n  return a - b;\n}\n","diff":"@@ -40,3 +40,3 @@\n function sub(a, b) {\n-  return a - b;\n+  return b - a;\n }\n"}'

Input

FieldTypeRequiredDescription
filestring; length 0..524288yesCurrent file text, at most 524288 UTF-8 bytes and 100000 lines. A leading BOM is preserved.
diffstring; length 0..262144noSingle-file unified diff, at most 64 hunks and 400 anchor lines each. Header counts and line numbers are ignored except as a reported hint. Give either diff or edits.
editsarray<object>; max 64 itemsnoSearch/replace blocks, matched as whole lines against the original file; all are located before any is applied.

Example request body

{
  "file": "function add(a, b) {\n  return a + b;\n}\n\nfunction sub(a, b) {\n  return a - b;\n}\n",
  "diff": "@@ -40,3 +40,3 @@\n function sub(a, b) {\n-  return a - b;\n+  return b - a;\n }\n"
}

Result

The response is { "data": ..., "_provenance": ... }. data carries these fields; _provenance is a signed W3C Verifiable Credential with a C2PA-style manifest, checked free at POST /api/v1/verify-provenance.

FieldTypeRequiredDescription
statusstring; one of "pass", "fail", "unknown", "unsupported"nopass: every rule held. fail: a rule was violated (see witnesses). unknown: the input lacks the evidence to decide. unsupported: outside what this engine covers. Missing evidence is never a pass.
appliedbooleannoTrue only when every hunk matched exactly one place and none overlap.
textstringnoThe patched file; present only when applied is true.
hunksarray<object>no
eolstring; one of "lf", "crlf", "mixed", "none"noLine endings found in the input file.
finalNewlinebooleannoWhether the returned text (or, when not applied, the input file) ends with a line terminator.
changedbooleannoPresent when applied: false if the result is byte-identical to the input.
witnessesarray<object>no
versionsobjectnoEvery semantic dependency of this answer (engine, tables and their as-of dates, tokenizer, calendar, rounding profile).
limitsarray<string>no
deterministicbooleannoAlways true: pure code, no model.

Pay and connect

Available through: x402 (USDC on Base), MCP. MCP clients reach it through the execution-safety-kit package.

Machine-readable