给任务划定范围,让它无法乱跑
自主智能体以一种特定的方式失败:它漂移。你要一件事,它「热心地」重构了另外三件,碰了你从未提及的文件,带着一份你读不懂的 diff 回来。修法不是更聪明的智能体——而是更紧的简报。
一个好的自主任务有四个部分:清晰的目标、明确的约束、具体的完成判据,以及智能体自己能运行的护栏。护栏是最重要的部分,因为它是智能体无法在你不察觉的情况下翻越的栅栏。
Goal: Make the failing tests in tests/checkout/ pass.
Constraints:
- Only edit files under src/checkout/. Do not touch src/auth/,
the database schema, or anything in config/.
- Do not add new dependencies.
- Do not change the tests themselves — the tests define correct behavior.
Done when:
- `npm test tests/checkout/` is fully green.
- `npm run typecheck` passes.
- `git diff --stat` shows changes only under src/checkout/.
If you get stuck after two real attempts, stop and report what you
tried and what's still failing. Don't paper over a failure to make
the suite go green.
这份简报之所以管用,是因为它把智能体关进了盒子里:约束给它能碰的东西筑墙,完成判据是它能运行的一条命令,而验证循环让它保持诚实:
┌────────────────────────── 范围围栏 ───────────────────────────┐
│ 可编辑: src/checkout/ │
│ ✗ 禁区: src/auth/ · db 架构 · config/ · 测试 │
│ │
│ ┌─────────┐ 编辑 ┌──────────────┐ │
│ │ 智能体 │──────────▶│ src/checkout/ │ │
│ │ │◀──────────┤ │ │
│ └────┬────┘ 跑测试 └──────────────┘ │
│ │ │
│ ▼ npm test + typecheck │
│ ┌─────────────┐ green ──▶ 完成 │
│ │ 验证循环 │ red ──▶ 重试 (最多 2) │
│ └─────────────┘ stuck ──▶ 停止并报告 ─┼─▶ 你
└───────────────────────────────────────────────────────────────┘
三样东西让这份简报难以让它跑偏。约束在智能体可以触碰的文件周围筑起一堵墙。完成判据是任何人都能运行的命令,而不是一种感觉。而最后那段给了它停下来发问而非乱扑腾的许可——正是这一点防止了那种慢镜头的灾难:智能体「修」了二十分钟,把代码树留得比它接手时还糟。
真正的栅栏是测试。能运行自己测试的智能体拥有一个紧凑而诚实的反馈回路,多半会留在其中;无法检查自己工作的智能体会信心十足地把坏掉的东西递给你。如果你从本章只带走一个习惯:给智能体一个验证自己的途径,并把通过它当作完成的定义。