{"id":"patch-anchor","title":"Apply a unified-diff hunk or search/replace edit to a file by content, refusing ambiguity","description":"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.","roi":"Stops a stale line number or an ambiguous anchor from silently corrupting a file.","audience":"agent-infra","package":"execution-safety-kit","channels":["x402","mcp"],"endpoint":{"method":"POST","url":"https://agents.writerslogic.com/api/v1/patch-anchor","path":"/api/v1/patch-anchor","contentType":"application/json"},"price":{"amount":"0.005","currency":"USDC","network":"base","perCall":true,"failedCallsCharged":false},"inputSchema":{"type":"object","additionalProperties":false,"required":["file"],"properties":{"file":{"type":"string","maxLength":524288,"description":"Current file text, at most 524288 UTF-8 bytes and 100000 lines. A leading BOM is preserved."},"diff":{"type":"string","maxLength":262144,"description":"Single-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."},"edits":{"type":"array","minItems":1,"maxItems":64,"description":"Search/replace blocks, matched as whole lines against the original file; all are located before any is applied.","items":{"type":"object","additionalProperties":false,"required":["search","replace"],"properties":{"search":{"type":"string","maxLength":65536},"replace":{"type":"string","maxLength":65536,"description":"Empty string deletes the matched lines."},"line":{"type":"integer","minimum":1,"maximum":1000000,"description":"Optional line hint, reported as lineShift and never used to break ties."}}}}}},"resultSchema":{"type":"object","properties":{"status":{"type":"string","enum":["pass","fail","unknown","unsupported"],"description":"pass: 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."},"applied":{"type":"boolean","description":"True only when every hunk matched exactly one place and none overlap."},"text":{"type":"string","description":"The patched file; present only when applied is true."},"hunks":{"type":"array","items":{"type":"object","properties":{"index":{"type":"integer"},"kind":{"type":"string","enum":["hunk","edit"]},"state":{"type":"string","enum":["applied","ambiguous","not-found","overlap","invalid","budget"]},"tolerance":{"type":"string","enum":["exact","trailing-ws","outer-ws","inner-ws"],"description":"Rung of the tolerance ladder that matched: exact, trailing whitespace ignored, outer whitespace ignored, internal whitespace collapsed."},"startLine":{"type":"integer","description":"1-based first original line of the matched region."},"endLine":{"type":"integer","description":"1-based last original line of the matched region."},"lineHint":{"type":"integer","description":"The line number the caller gave; never used to choose between matches."},"lineShift":{"type":"integer","description":"Matched start line minus the hint."},"candidates":{"type":"array","items":{"type":"object","properties":{"startLine":{"type":"integer"},"endLine":{"type":"integer"},"score":{"type":"number","description":"Share of anchor lines identical to the file's lines (ambiguous) or positional similarity (not found)."},"tolerance":{"type":"string"}}}},"candidateCount":{"type":"integer"},"note":{"type":"string"}}}},"eol":{"type":"string","enum":["lf","crlf","mixed","none"],"description":"Line endings found in the input file."},"finalNewline":{"type":"boolean","description":"Whether the returned text (or, when not applied, the input file) ends with a line terminator."},"changed":{"type":"boolean","description":"Present when applied: false if the result is byte-identical to the input."},"witnesses":{"type":"array","items":{"type":"object","properties":{"ruleId":{"type":"string"},"detail":{"type":"string"},"path":{"type":"string","description":"JSON pointer or dotted path into the input."},"offset":{"type":"integer"},"index":{"type":"integer"},"expected":{"description":"What the rule required."},"actual":{"description":"What the input had."}}}},"versions":{"type":"object","description":"Every semantic dependency of this answer (engine, tables and their as-of dates, tokenizer, calendar, rounding profile)."},"limits":{"type":"array","items":{"type":"string"}},"deterministic":{"type":"boolean","description":"Always true: pure code, no model."}}},"outputSchema":{"type":"object","properties":{"data":{"type":"object","properties":{"status":{"type":"string","enum":["pass","fail","unknown","unsupported"],"description":"pass: 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."},"applied":{"type":"boolean","description":"True only when every hunk matched exactly one place and none overlap."},"text":{"type":"string","description":"The patched file; present only when applied is true."},"hunks":{"type":"array","items":{"type":"object","properties":{"index":{"type":"integer"},"kind":{"type":"string","enum":["hunk","edit"]},"state":{"type":"string","enum":["applied","ambiguous","not-found","overlap","invalid","budget"]},"tolerance":{"type":"string","enum":["exact","trailing-ws","outer-ws","inner-ws"],"description":"Rung of the tolerance ladder that matched: exact, trailing whitespace ignored, outer whitespace ignored, internal whitespace collapsed."},"startLine":{"type":"integer","description":"1-based first original line of the matched region."},"endLine":{"type":"integer","description":"1-based last original line of the matched region."},"lineHint":{"type":"integer","description":"The line number the caller gave; never used to choose between matches."},"lineShift":{"type":"integer","description":"Matched start line minus the hint."},"candidates":{"type":"array","items":{"type":"object","properties":{"startLine":{"type":"integer"},"endLine":{"type":"integer"},"score":{"type":"number","description":"Share of anchor lines identical to the file's lines (ambiguous) or positional similarity (not found)."},"tolerance":{"type":"string"}}}},"candidateCount":{"type":"integer"},"note":{"type":"string"}}}},"eol":{"type":"string","enum":["lf","crlf","mixed","none"],"description":"Line endings found in the input file."},"finalNewline":{"type":"boolean","description":"Whether the returned text (or, when not applied, the input file) ends with a line terminator."},"changed":{"type":"boolean","description":"Present when applied: false if the result is byte-identical to the input."},"witnesses":{"type":"array","items":{"type":"object","properties":{"ruleId":{"type":"string"},"detail":{"type":"string"},"path":{"type":"string","description":"JSON pointer or dotted path into the input."},"offset":{"type":"integer"},"index":{"type":"integer"},"expected":{"description":"What the rule required."},"actual":{"description":"What the input had."}}}},"versions":{"type":"object","description":"Every semantic dependency of this answer (engine, tables and their as-of dates, tokenizer, calendar, rounding profile)."},"limits":{"type":"array","items":{"type":"string"}},"deterministic":{"type":"boolean","description":"Always true: pure code, no model."}}},"_provenance":{"type":"object","description":"C2PA-aligned manifest and W3C VC 2.0 (eddsa-jcs-2022) issued by the hub DID; verify at POST /api/v1/verify-provenance"}},"required":["data","_provenance"]},"example":{"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"},"openapiOperation":{"operationId":"patch_anchor","summary":"Apply a unified-diff hunk or search/replace edit to a file by content, refusing ambiguity","description":"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.\n\nPrice: 0.005 USDC on Base (x402).","x-x402-price-usdc":"0.005","x-x402-amount":"5000","x-payment-info":{"protocols":["x402"],"price":{"mode":"fixed","currency":"USD","amount":"0.005"}},"x-audience":"agent-infra","x-sample-url":"https://agents.writerslogic.com/api/v1/samples/patch-anchor","security":[{"x402":[]},{"apiKey":[]}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"required":["file"],"properties":{"file":{"type":"string","maxLength":524288,"description":"Current file text, at most 524288 UTF-8 bytes and 100000 lines. A leading BOM is preserved."},"diff":{"type":"string","maxLength":262144,"description":"Single-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."},"edits":{"type":"array","minItems":1,"maxItems":64,"description":"Search/replace blocks, matched as whole lines against the original file; all are located before any is applied.","items":{"type":"object","additionalProperties":false,"required":["search","replace"],"properties":{"search":{"type":"string","maxLength":65536},"replace":{"type":"string","maxLength":65536,"description":"Empty string deletes the matched lines."},"line":{"type":"integer","minimum":1,"maximum":1000000,"description":"Optional line hint, reported as lineShift and never used to break ties."}}}}}},"example":{"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"}}}},"responses":{"200":{"description":"Paid result with provenance","content":{"application/json":{"schema":{"type":"object","properties":{"data":{"type":"object","properties":{"status":{"type":"string","enum":["pass","fail","unknown","unsupported"],"description":"pass: 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."},"applied":{"type":"boolean","description":"True only when every hunk matched exactly one place and none overlap."},"text":{"type":"string","description":"The patched file; present only when applied is true."},"hunks":{"type":"array","items":{"type":"object","properties":{"index":{"type":"integer"},"kind":{"type":"string","enum":["hunk","edit"]},"state":{"type":"string","enum":["applied","ambiguous","not-found","overlap","invalid","budget"]},"tolerance":{"type":"string","enum":["exact","trailing-ws","outer-ws","inner-ws"],"description":"Rung of the tolerance ladder that matched: exact, trailing whitespace ignored, outer whitespace ignored, internal whitespace collapsed."},"startLine":{"type":"integer","description":"1-based first original line of the matched region."},"endLine":{"type":"integer","description":"1-based last original line of the matched region."},"lineHint":{"type":"integer","description":"The line number the caller gave; never used to choose between matches."},"lineShift":{"type":"integer","description":"Matched start line minus the hint."},"candidates":{"type":"array","items":{"type":"object","properties":{"startLine":{"type":"integer"},"endLine":{"type":"integer"},"score":{"type":"number","description":"Share of anchor lines identical to the file's lines (ambiguous) or positional similarity (not found)."},"tolerance":{"type":"string"}}}},"candidateCount":{"type":"integer"},"note":{"type":"string"}}}},"eol":{"type":"string","enum":["lf","crlf","mixed","none"],"description":"Line endings found in the input file."},"finalNewline":{"type":"boolean","description":"Whether the returned text (or, when not applied, the input file) ends with a line terminator."},"changed":{"type":"boolean","description":"Present when applied: false if the result is byte-identical to the input."},"witnesses":{"type":"array","items":{"type":"object","properties":{"ruleId":{"type":"string"},"detail":{"type":"string"},"path":{"type":"string","description":"JSON pointer or dotted path into the input."},"offset":{"type":"integer"},"index":{"type":"integer"},"expected":{"description":"What the rule required."},"actual":{"description":"What the input had."}}}},"versions":{"type":"object","description":"Every semantic dependency of this answer (engine, tables and their as-of dates, tokenizer, calendar, rounding profile)."},"limits":{"type":"array","items":{"type":"string"}},"deterministic":{"type":"boolean","description":"Always true: pure code, no model."}}},"_provenance":{"type":"object","description":"C2PA-aligned manifest and W3C VC 2.0 (eddsa-jcs-2022) issued by the hub DID; verify at POST /api/v1/verify-provenance"}},"required":["data","_provenance"]}}}},"400":{"description":"Invalid request (no payment consumed)","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"},"code":{"type":"string"}}}}}},"401":{"description":"Invalid X-API-KEY","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"},"code":{"type":"string"}}}}}},"402":{"description":"Payment required or invalid","content":{"application/json":{"schema":{"type":"object","description":"x402 v2 PaymentRequired (also base64 in the PAYMENT-REQUIRED header)","properties":{"x402Version":{"type":"integer"},"error":{"type":"string"},"code":{"type":"string"},"resource":{"type":"object"},"accepts":{"type":"array","items":{"type":"object"}},"extensions":{"type":"object"}}}}}},"502":{"description":"Upstream failure; the payment is released or not settled","content":{"application/json":{"schema":{"type":"object","properties":{"error":{"type":"string"},"code":{"type":"string"}}}}}}}},"links":{"docs":"https://agents.writerslogic.com/docs/patch-anchor/","docsJson":"https://agents.writerslogic.com/docs/patch-anchor.json","sample":"https://agents.writerslogic.com/api/v1/samples/patch-anchor","openapi":"https://agents.writerslogic.com/openapi.json","catalog":"https://agents.writerslogic.com/api/v1/catalog","verifyProvenance":"https://agents.writerslogic.com/api/v1/verify-provenance","index":"https://agents.writerslogic.com/docs/index.json"}}