Dayward AIWeChat Mini Program
Build Your Own Coding Agent in 21 Days
Week 1 · D4🔒 LockedAbout 4 hours

File Editing and Shell Execution: Exact-Match Replacement, Conflict Detection, and a Timeout-Killable Subprocess

Upgrade read access to write access: implement an edit tool based on exact-match replacement and a shell tool that runs commands, handle four real failure modes — stale content not matching, files changed externally, a command hanging, and output too large — then let the agent fix a failing test green for the first time.

Today's goals 0/3

Sign in to tick these off and save your progress.

Today's Goals

  1. Explain the failure modes of exact-match replacement versus patch-style editing, and pick the one that suits a model
  2. Implement an edit tool with conflict detection, so concurrent or external changes are never silently overwritten
  3. Implement a command-execution tool that can time out, be killed, and cap its output

Today mca changes your code for the first time. When you finish, scroll back up and tick the three goals off.

Plain-Language Walkthrough

Now give write access: the risk changes by an order of magnitude

Yesterday that new hire got read access. Today you grant commit rights — and before pressing that button your state of mind is nothing like yesterday's.

The difference is not their ability but reversibility. Read the wrong file yesterday and five minutes were wasted; edit the wrong file today and half a day of someone else's work may be overwritten. And write errors have a nasty property: they are often silent. A wrong read announces itself ("this is not what I was looking for"); a wrong edit leaves the program running and the tests green, and the problem surfaces two weeks later.

Adding write tools to an Agent is riskier still than to a new hire, because it has three traits a new hire does not:

  1. It has no instinct for "I am not sure, let me ask." Give it an editing tool and it uses it; it has no awareness that it might be misremembering the original text.
  2. It can change many places at once. A new hire edits one file wrongly; an Agent edits five in one turn.
  3. Its input is guessed. The code it writes into old_string came from a read_file several turns ago, and that content may be stale.

So today's focus is not "how to put characters into a file" — that is one line of writeFile. Today's focus is three failure modes and how to make each fail loudly enough: the original does not match, the original is not unique, and a command hangs without returning. Handle these badly and you get a tool that looks usable and occasionally breaks things quietly — the worst class of bug in a Coding Agent, because it raises no error.

So what shape should the write tool take? The three mainstream approaches each fail in their own way; lay them out side by side.

Sign in and link WeChat to read

Sign in, then link your WeChat mini program account on the account page. Linked accounts share one free article per day. Unlock additional articles with rewarded videos in the mini program.

Mini program QR code

Scan in WeChat to open Dayward AI. Open the same course and chapter, choose the rewarded video, then refresh this page after completion.

Interview questions

  • To let a model edit files, do you pick whole-file rewrite, exact string replacement, or patches? Why?让模型改文件,你会选整文件重写、精确替换还是补丁?为什么?
    Common in ChinaCommon overseasBasic#file-editing#tool-design

    🔒 Unlock this chapter to see the answer keys.

  • How does an edit tool detect conflicts, and what should it tell the model when detection fails?编辑工具怎么做冲突检测?检测失败时该给模型什么信息?
    Common in ChinaCommon overseasIntermediate#file-editing#conflict-detection

    🔒 Unlock this chapter to see the answer keys.

  • What constraints do you put on running arbitrary shell commands from an agent, and how do you actually kill one after a timeout?在 Agent 里执行任意 shell 命令,你会加哪些约束?超时之后怎么真正把它杀掉?
    Common in ChinaCommon overseasDeep dive#shell-execution#process-management

    🔒 Unlock this chapter to see the answer keys.

Comments