小节三:Coding Agent Main Loop 怎么跑——真正的核心永远是 Observation 驱动下一步

如果只记住这一章一个核心点,那我更希望你记住这一句:Observation 驱动下一步。Agent 不是一次性把 Patch 生成出来就结束了,而是要在“上下文 → LLM → 行动 → 工具 → Observation”的循环里,不断根据新证据修正策略。图里把这个闭环单独拎了出来,读到后面你会越来越感受到它的重要性。

图片[1]-小节三:Coding Agent Main Loop 怎么跑——真正的核心永远是 Observation 驱动下一步-大赛人网

现在回到 Agent 最核心的:

while True

我们可以把 Coding Agent 的 Main Loop 抽象成:

Context
 ↓
LLM
 ↓
Action
 ↓
Tool
 ↓
Observation
 ↓
Context

状态里至少要有

goal
step
status
working_memory
tool_history
modified_files
test_status
project_rules

完成条件必须结构化

不能只让模型自己说:

“我觉得完成了。”

至少需要:

目标 Bug 已处理
相关测试通过
全量测试通过
没有违反 AGENTS.md
没有 pending approval

一个教学版 CodingState

为什么测试状态必须在 State 里?

因为如果不保存:

full_tests_passed

Agent 很容易在 Context Compaction 后忘记:

全量测试还没跑。

这就是前面“提前结束”章节里的问题。

Main Loop 伪代码

while run is RUNNING:

    check RunGuard

    build context

    call LLM

    if final_answer:
        verify completion

    if tool_call:
        middleware check
        execute tool
        record observation
        update state
        trace

    if completion criteria:
        COMPLETED

Error Recovery

比如:

read_file("config.py")
→ FileNotFoundError

Agent 不应该重复。

而应该:

list_files
search_files

重新定位。

又比如:

pytest
→ AssertionError

Agent 应该:

读取 traceback
定位源码
最小修改
再次测试

真正的 Coding Agent 价值,就在这个循环里。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发
头像-大赛人网
欢迎您留下宝贵的见解!
提交
头像-大赛人网

昵称

取消
昵称

    请登录后查看评论内容