Automation:Goals、Loops、Routines 与 Channels
当 Claude Code 需要在没有持续提示的情况下继续工作时,选择 goals、loops、scheduled tasks、routines、channels 或 hooks。
自动化地图
Claude Code 有几种方式让工作在你不持续输入下一步 prompt 的情况下继续推进。它们看起来相似,但触发下一步的原因不同。
| 机制 | 何时启动下一步 | 何时停止 | 作用域 |
|---|---|---|---|
/goal | 上一轮结束 | 模型确认条件满足 | 当前 session |
/loop 或 scheduled task | 时间间隔或 cron 时间到达 | 你停止、过期,或 Claude 判断完成 | 当前 session |
| Stop hook | 上一轮结束 | 你的 script 或 prompt 决定 | Settings scope |
| Channel | 外部事件到达 | 事件处理完成 | Running session |
| Routine | Schedule、API call 或 GitHub event 触发 | Cloud run 完成 | Claude Code on the web |
选择时看“什么应该触发下一轮”。
/goal
当目标有可验证完成条件时用 /goal:
/goal migrate the auth module until npm test passes and no imports use the old session APIClaude 会跨 turns 持续工作。每轮结束后,一个小 evaluator 检查目标是否完成;如果没有,Claude 自动开始下一轮。
好的 goal 是可度量的:
- “package X 的所有测试通过”
- “没有 call site import 旧 API”
- “design doc 的每个 checklist item 都完成”
- “issue backlog query 返回 0 个 open items”
避免依赖品味或隐藏人类判断的 goal。
/loop 与 Scheduled Tasks
当时间应该触发下一次检查时,用 /loop 或 scheduled tasks:
/loop every 5 minutes check whether the deploy finished and summarize any failure适合轮询 deploy、盯长 CI job,或提醒自己稍后回到某个任务。Session-scoped scheduled tasks 在 resume 时可以回来,但它们不是持久基础设施。
Routines
Routines 运行在 Claude Code on the web,可以由这些方式触发:
- 周期性 schedule。
- 未来某个一次性时间点。
- API call。
- GitHub events,如 pull requests 或 releases。
当工作需要在电脑合上后仍继续运行时,用 routines。例如 nightly dependency review、weekly architecture drift report、PR review automation。
Channels
Channels 通过 MCP 把外部事件推入正在运行的 Claude Code session。它适合 polling 不合适的场景:
- CI 失败并把 failure 发给 Claude。
- Monitoring 触发 alert。
- 队友消息应该成为 session context。
- Webhook 传来状态变化。
Hooks 把 Claude 事件往外推。Channels 把外部事件往 session 里推。
Hooks 作为自动化胶水
Hooks 是确定性的。当规则必须每次都运行时使用:
- 编辑后格式化。
- 阻止写入敏感文件。
- Claude 停止时发送通知。
- 给 agent-team tasks 加质量门。
长任务自动化里,hooks 经常和 /goal 或 routines 配合:goal 判断工作是否完成;hooks 围绕每轮强制检查。
决策指南
| 需求 | 使用 |
|---|---|
| 一直工作到条件满足 | /goal |
| 在单 session 中轮询或提醒 | /loop 或 scheduled task |
| 响应 CI、alerts 或 webhooks | Channels |
| 围绕每个工具调用强制策略 | Hooks |
| 在云端自动运行 | Routines |
| 一次 repeatable run 编排大量 agents | Dynamic workflow |
官方参考
继续实践
Automation:Goals、Loops、Routines 与 Channels 的核心概念已经读完
如果你想把理解变成可复用的能力,可以到 AgentWay 继续练习