手写一个 mini coding agent:
要哪些 tool,edit 冲突怎么处理?

英文问法Build a minimal coding agent. Which tools does it need? · The user and the agent edit the same file at the same time. How do you avoid overwriting? · How would you run it on multiple nodes?

考过别家面经原题:蚂蚁 Agent 开发日常实习一面(发帖 2026-11-06)「追问edit tool,如果用户和agent同时修改一份文件,覆写问题怎么解决」「追问read tool,如果要读取一个10兆的文件,全放进去上下文直接爆了,你怎么解决」(牛客)、阿里 AI Infra(面试 2026-05-02)「AI Coding:实现一个类似 Claude Code 的命令行交互?为了让 agent 支持读取文件、修改文件,需要怎么做?」(牛客合集,只读到搜索摘要,岗位归属不确定)、虾皮 Data Infra 实习一面(面试 2026-05-26)「使用Claudecode将将智能体项目,改造为可以多节点部署的项目?」(牛客) · 英文侧:Anthropic SWE 电面的 agents coding 轮,一亩三分地站方摘要写 "building a Claude agent loop for multi-step tool calls"(1p3a 1180230,只读到回帖和摘要,是转述) · 中文面经样本里现场 coding 和设计题有 29 帖 · 没有 Unity 自己的面经原题

先弄清楚:每个概念是什么

Agent loop
harness 反复调 model、执行它发的 tool call 的循环(见 agent 与 MCP 和 LLM harness)。
edit tool
精确字符串替换,把一段 old string 换成 new string。
Optimistic concurrency
不加锁,写的时候才检查文件有没有被别人动过。像共享文档提示“版本冲突,请刷新”。

看图:一个 loop 加三个 tool

model harness loop + messages tool call tool result read 记下读到时的 hash edit hash 变了就报错 bash sandbox + timeout 没有 tool call 就返回回答
图 1:harness 执行 tool call,把 tool result 追加到 messages。

30 秒回答

The core is a loop around three tools: read a file, edit it by replacing an exact string, and run a shell command. Each turn the model either answers or asks for a tool; the harness runs it and appends the result, errors included, so the model can correct itself. Edit requires that the file was read first and fails if it changed since then. That's optimistic concurrency: compare a hash before writing, and re-read on a mismatch. Commands run in a sandbox with a timeout and truncated output.

Agent loop 的最小实现

def run_agent(task, tools, max_steps=50):
    messages = [{"role": "user", "content": task}]
    for _ in range(max_steps):
        reply = llm(messages, tool_schemas(tools))
        messages.append(reply)
        if not reply.tool_calls:           # 不再要 tool,任务结束
            return reply.content
        for call in reply.tool_calls:
            try:
                out = tools[call.name](**call.args)
            except Exception as e:         # 报错也还给 model,让它改
                out = f"error: {e}"
            messages.append({"role": "tool", "id": call.id, "content": out})
    return "stopped: max steps reached"

edit tool 怎么防覆写

版本做法还剩的问题
v1 整文件重写model 输出整份新文件覆盖费 token;用户刚改的内容被冲掉
v2 字符串替换只改 old string 那一处,找不到就报错read 之后文件被别人改过,model 还按旧内容改
v3 加 hash 检查先 read 才能 edit;写之前重算 hash,和 read 时不一样就报错,让 model 重读冲突频繁时要反复重读

v3 就是 optimistic concurrency。蚂蚁那场面试里,候选人先答比较 diff,面试官追问为什么不比较 hash,并说 Codex 就是这样做的。这一点只有他的说法作依据。

read 和 bash 要管住什么

Tool要管住什么
read大文件先用 search 定位,再用 offset 和 limit 只读命中的一段(见 文件放不进 context)
bash在 sandbox 里跑;加 timeout;输出太长就截断

再加一个 search tool,按文件名或内容找文件。读默认放行,写和执行按规则确认(见 agent 权限)。

必答点

追问

比对 hash 还是 mtime?

hash 只看内容,最可靠。mtime 不用读文件,便宜,但内容没变时也可能变。

改成多节点部署要考虑什么?(虾皮原题)

会话状态放到外部存储,workspace 跟着会话走,任何节点都能接着跑。请求要幂等,重发不会多改一次文件。

来源面经原帖:蚂蚁 Agent 开发日常实习一面(read、edit、bash 的追问,面试官关于 hash 的说法)、虾皮 Data Infra 实习一面(多节点部署)、阿里合集、作业帮 Golang 实习(规则由程序执行还是写进提示词) · 1p3a 1180230 · ml-八股 08 第 37 题 · 中文平台调研笔记 1.2 节和第 2 节