Skip to lesson

AnthropicClaude Certified Architect – Foundations2026-102.5 Select and apply built-in tools (Read, Write, Edit, Bash, Grep, Glob) effectively

Recover ambiguous edits without widening intent

Completed by the knowledge check

Learning objective

Recover from non-unique Edit anchors by reading the relevant file and choosing a safe whole-file replacement when needed.

Core idea

An ambiguous Edit is a signal that the proposed text does not identify the intended location. It is not permission to broaden the requested change. If one statement appears in four functions and only one function should change, replacing every occurrence makes the edit executable by changing the requirement.

For a code generation agent, recovery starts with Read on the relevant file. Fresh content lets you choose a longer unique anchor, verify that a global replacement is genuinely intended, or prepare the complete corrected file for Write. Whole-file replacement removes the matching ambiguity, but transfers responsibility to preserving every untouched part of the file. Choose it only when you have enough evidence to do that.

How it works

Interpret the failed match

Edit receives file_path, old_string, and new_string. Its replacement is exact: it does not use regex or fuzzy matching. For a targeted replacement, old_string must occur once. When it occurs more than once, Claude can add surrounding context to select one occurrence or set replace_all: true to replace them all.

Those are distinct intentions. A repeated logging statement inside refund, fee, and correction handlers is ambiguous if the request names only the refund handler. A shared spelling mistake that must be corrected everywhere can justify replace_all: true. The count of occurrences alone does not tell you which intention is correct.

A non-unique match also differs from a missing match. An extra indentation character can make old_string fail to match at all. Repeating the same call repairs neither failure. Read the file to identify whether the problem is repeated text, stale text, or an inaccurate copy, then construct a call that matches observed content.

Prefer a unique anchor when practical

Use Read.file_path to inspect the target function and enough neighboring content to disambiguate it. Copy the anchor exactly, including whitespace. A function declaration plus the nearby statement may uniquely identify the edit where the statement alone does not.

The longer anchor changes the matching scope, not the intended behavioral scope. Its new_string must retain the surrounding text and modify only the requested part. Otherwise, making the anchor unique can introduce unrelated changes just as a whole-file rewrite can. Choose the smallest observed passage that identifies the correct occurrence clearly.

Do not use replace_all: true merely because uniqueness failed. That field is appropriate only when every occurrence should receive the same replacement. It is not a “force this one edit” switch and does not select an occurrence by its function’s meaning.

Use complete replacement when needed

The exam guide explicitly names Read followed by Write as the fallback when Edit cannot find unique anchor text. The product’s Write semantics explain the trade-off: Write.content replaces the entire file without matching an anchor, appending, or merging.

For this fallback, read the complete file, identify the intended change in that content, and supply the complete corrected content with the same file_path. Preserve imports, declarations, comments, and every unrelated branch. There is no implicit instruction in Write to keep whatever you omitted.

Check whether the read actually completed. A PARTIAL view is not a complete source snapshot. Use Read.offset and Read.limit to obtain the remaining content as documented in 2.5.2 before constructing a replacement. A few matching lines from Grep are enough to locate the handler, but not enough to reproduce the entire file safely.

Read the relevant file again if intervening changes invalidate the content used to construct the replacement. The tools reference allows some exact edits against changed files, but a complete replacement must account for the entire intended final content. A successful Write is not proof that the replacement retained changes outside the target statement.

After mutation, inspect the affected content with Read and inspect the patch with Bash running git diff for that file. Run the repository’s relevant verification command through Bash. Anthropic’s best practices recommend giving Claude concrete verification criteria; here those criteria include both the requested handler change and preservation of the handlers that must remain unchanged. Every recovery starts with a fresh read and ends with the same checks.

Every recovery starts with a fresh read and ends with the same checksAn ambiguous Edit leads first to Read on the relevant file. The intention then picks the recovery: a longer unique old_string for one occurrence, replace_all: true when every occurrence should change, or reading the complete file for a complete replacement, where a PARTIAL view sends the agent to Read.offset and Read.limit before it supplies the complete corrected content. All three paths end at git diff and the relevant verification command.one occurrenceevery occurrencecomplete replacementPARTIAL viewAmbiguous EditRead the relevantfileWhich intention?Longer uniqueold_stringreplace_all: trueRead the completefileRead.offset andRead.limitSupply thecomplete correctedcontentgit diff and therelevantverificationcommand

Key distinctions and trade-offs

The recovery choices differ in what they require:

ChoiceAppropriate conditionMain preservation burden
Longer old_stringSurrounding text uniquely identifies the intended occurrenceReproduce the selected surrounding text exactly
replace_all: trueEvery matching occurrence should change identicallyVerify that the scope really includes every occurrence
Read then WriteTargeted matching is unsuitable and complete corrected content is availablePreserve the entire file outside the intended change

Matching safety versus behavioral correctness: A unique anchor prevents ambiguity about which text is replaced. It does not prove that changing that text is the right implementation. Conversely, avoiding matching with Write does not prove that the whole replacement is correct.

Less returned content versus complete preservation: A targeted read can support a precise Edit. Whole-file replacement needs whole-file evidence. Choosing the fallback after reading only the target lines saves context by discarding the information required to preserve the rest.

Recovery versus bypass: A shell replacement through Bash is not a repair for an unresolved target. It still needs a rule that selects the intended occurrence. Dedicated file operations make that intent explicit; their operation-level trade-offs are covered in 2.5.2.

Architecture / decision scenario

A Claude Code agent must add reasonCode to the refund handler’s log payload. Four adjustment handlers contain logger.info("adjustment posted", { orderId });. An Edit with that statement as old_string fails because the anchor is not unique.

The agent uses Read on the adjustment module and identifies all four occurrences. For a small refund function with distinctive surrounding lines, it can retry with a longer unique old_string. It retains those surrounding lines in new_string, adding reasonCode only in the refund payload. Setting replace_all: true would also alter fee and correction logging, so that option fails the requirement.

If a sufficiently unique anchor would require reproducing most of the module and make the targeted replacement difficult to review, the agent can choose the whole-file fallback instead. Its first read is partial, so it retrieves the remaining content before supplying Write.content. The corrected file changes only the refund payload and preserves the other three handlers and their payloads.

Finally, git diff shows one intended refund change and no modifications to the other handlers. The agent runs the relevant tests and reads the resulting refund branch. The recovery is complete because the required change and preservation conditions are verified, not merely because a tool call succeeded.

Common exam traps

  • Retrying an unchanged ambiguous old_string and expecting a different selection.
  • Treating replace_all: true as a way to force a single-occurrence change.
  • Supplying the changed function alone as Write.content for a multi-function file.
  • Reconstructing a whole file from a partial read or search excerpt.
  • Widening an anchor while accidentally changing its surrounding code.
  • Treating a successful replacement as proof that unrelated content survived.

Where the exam guide differs

Note:

The exam guide assesses Read of the full file followed by Write when Edit fails on non-unique anchor text. For an item framed around that failure, select the full-file Read + Write recovery; retrying the unchanged anchor and using replace_all: true when only one occurrence should change are distractors.

The product’s Edit documentation instead names a longer old_string with surrounding context or replace_all: true as its non-uniqueness remedies. The latter fits only an intended global change. Whole-file replacement is supported by Write’s semantics, but is not listed there as an Edit remedy. Recognize the guide’s assessed fallback while using the precise recovery that preserves the actual task’s scope.

What to remember

Read after an ambiguous edit, then match the recovery to the intended scope. Use a unique anchor for one occurrence and replace_all: true only for an actual global change. Use Read plus Write when complete replacement is needed and complete content is available; verify both the change and the preserved code.

Knowledge check

References