Glean 拾遗
日刊 /2026-06-01 / Claude Code 自主运行完全指南:/goal、/loop、/schedule 与 Stop Hook

Claude Code 自主运行完全指南:/goal、/loop、/schedule 与 Stop Hook

原文 x.com 收录 2026-06-01 06:00 阅读 9 min
AI 解读

本文详述了 Claude Code 四种让 AI 脱离手动引导的自主运行命令。/goal 设定完成条件,每轮由轻量模型自动评估是否达成,直至条件满足;/loop 按固定时间间隔循环执行;/schedule 创建独立于会话的后台定时任务;Stop Hook 则支持自定义脚本或条件判断,实现“测试不绿不停止”。文中给出了明确的模板、好/坏条件示例、与 Auto 模式配合实现完全无人值守的方案,并对比了各命令的适用场景,帮助开发者从“每步提示”转向“设定即忘”的工作流。

原文 9 分钟
原文 x.com ↗
§ 1

The Complete Guide to /goal, /loop, /schedule & Stop Hooks in Claude Code

Claude Code /goal、/loop、/schedule 与 Stop Hooks 完整指南配图

§ 2

Most developers use Claude Code like an assistant they have to babysit.

Type a prompt. Wait. Review. Type again. Wait again.

Every step needs you to push it forward.

There's a completely different way to work. Four commands that let Claude run autonomously until the job is actually done — no babysitting required.

Here's the complete guide to /goal, /loop, /schedule, and Stop hooks.

大多数开发者把 Claude Code 当成需要时刻看护的助手使用。

输入提示 → 等待回复 → 审查输出 → 再次输入提示 → 再次等待。

每一步都需要你向前推动。

有一种完全不同的工作方式:四条指令,让 Claude 自主运行,直到任务真正完成——无需看护。

这里是 /goal、/loop、/schedule 和 Stop hooks 的完全指南。

§ 3

THE SHIFT: FROM ASSISTANT TO AUTONOMOUS AGENT

Normal Claude Code workflow:

You prompt → Claude responds → you review → you prompt again → Claude responds → repeat 20 times.

You are the loop. You are the condition check. You are the reason it keeps going.

Autonomous Claude Code workflow:

You set a condition → Claude works → a fast evaluator checks the condition after every turn → if not done, Claude keeps going → when done, you get a summary.

You set the goal once. Claude runs until it's met.

This is not a minor workflow tweak. This is a fundamentally different relationship with the tool.


范式转变:从助手到自主代理

通常的 Claude Code 工作流:

你输入提示 → Claude 回复 → 你审查 → 你再次输入提示 → Claude 再次回复 → 重复 20 次。

你就是循环本身,是条件判断器,是它继续运行的原因。

自主 Claude Code 工作流:

你设定一个条件 → Claude 开始工作 → 一个快速评估器在每一轮后检查条件 → 若条件未满足,Claude 继续下一轮 → 满足时,你会收到“目标已达成”的摘要并停止。

你一次性设定目标,Claude 一直运行直到目标实现。

这不是微小的流程调整,而是与工具关系的根本转变。

§ 4

COMMAND 1 → /goal

/goal is the most powerful autonomous command in Claude Code. Most developers have never used it.

What it does:

You set a completion condition. After every turn Claude takes, a small fast model (Haiku by default) checks whether the condition has been met by reading the conversation transcript. If not — Claude starts another turn automatically. If yes — you get a "Goal achieved" summary and Claude stops.

You never have to prompt again. Claude keeps working until the condition is true.

How to use it:

/goal all tests in test/auth pass and the lint step is clean

That's it. Claude starts working immediately. No separate prompt needed. The condition itself becomes the directive.

While it's running, a ◎ /goal active indicator shows how long it's been going. Check status anytime:

/goal

This shows: the condition, how long it's been running, how many turns evaluated, tokens spent so far, and the evaluator's most recent reason.

Stop it early:

/goal clear

(stop, off, cancel, reset, and none all work as aliases)

The template for every goal you'll ever write:

/goal [task] until [success condition], verified by [check], while [constraints], or stop after [limit]

Real examples:

/goal migrate auth to the new API until all auth tests pass and unrelated test files are unchanged

/goal refactor the user service until tests pass, or stop after 10 turns

/goal complete the migration, or stop after 30 minutes

Writing good conditions — the most important skill:

The evaluator only reads the conversation transcript. It cannot run commands or open files independently. So your condition must be something Claude's own output can prove.

Good conditions:

→ "All tests in test/auth pass and npm test exits 0"

→ "git status is clean and no other test files are modified"

→ "Every call site in the module compiles with no errors"

→ "Issue backlog queue is empty — verified by running the list command"

Bad conditions:

→ "Do your best on the refactor" — no measurable end state

→ "Looks clean" or "seems right" — subjective, unevaluable

→ "Improve the code" — no way to verify completion

→ Conditions that require reading files the evaluator can't see

Rule: if you can't describe how Claude would prove it's done in the transcript — rewrite the condition until you can.

Add a limit for long tasks:

Always include a turn or time clause on non-trivial goals. Without one, a goal that can't be met will run forever.

/goal fix checkout tests, or stop after 10 turns
/goal complete the migration, or stop after 30 minutes

Claude tracks and reports progress against the limit each turn. The evaluator judges whether the limit has been hit.

Run /goal non-interactively:

claude -p "/goal CHANGELOG.md has an entry for every PR merged this week"

This runs the entire loop to completion in one invocation. Stop it with Ctrl+C. Perfect for CI pipelines and scheduled scripts.

Combine with Auto mode for fully unattended work:

/goal removes per-turn prompts. Auto mode removes per-tool prompts.

Together — Claude works completely unattended until the condition is met or the limit is hit. This is the closest thing to a fully autonomous coding agent available in Claude Code today.

Resume after crash:

If a session ends with an active goal, it restores automatically when you --resume or --continue the session. The condition carries over. Turn count, timer, and token spend reset. Pick up exactly where it stopped.

COMMAND 1 → /goal

/goal 是 Claude Code 中最强大的自主指令,大多数开发者从未使用过它。

它的作用:

你设定一个完成条件。Claude 每走完一轮,一个轻量的快速模型(默认是 Haiku)会读取对话记录,判断条件是否满足。如果没有——Claude 自动开始新的一轮;如果满足——你会收到“目标已达成”的摘要,Claude 停止。

你无需再次输入提示。Claude 会一直工作直到条件成立。

如何使用:

/goal all tests in test/auth pass and the lint step is clean

就这一行。Claude 立刻开工,不需要单独的提示词。条件本身就成了方向。

运行期间,会显示 ◎ /goal active 指示器,标明已运行多久。随时查看状态:

/goal

它会显示:条件、已运行时长、已评估的轮数、已消耗的 token 数,以及评估器最近给出的理由。

提前终止:

/goal clear

(stop、off、cancel、reset、none 都是同等作用的别名)

你以后所有 goal 的通用模板:

/goal [task] until [success condition], verified by [check], while [constraints], or stop after [limit]

实际例子:

/goal migrate auth to the new API until all auth tests pass and unrelated test files are unchanged

/goal refactor the user service until tests pass, or stop after 10 turns

/goal complete the migration, or stop after 30 minutes

编写良好条件——最重要的能力:

评估器只能读取对话记录,不能独立运行指令或打开文件。因此你的条件必须是 Claude 自身的输出能够证明的。

好的条件:

→ "All tests in test/auth pass and npm test exits 0"

→ "git status is clean and no other test files are modified"

→ "Every call site in the module compiles with no errors"

→ "Issue backlog queue is empty — verified by running the list command"

差的条件:

→ "Do your best on the refactor" —— 没有可度量的终点

→ "Looks clean" 或 "seems right" —— 主观,无法评估

→ "Improve the code" —— 无法验证是否完成

→ 需要读取评估器看不到的文件的条件

原则:如果你不能说明 Claude 如何在对话记录中证明任务已完成,那就重写条件,直到能说清楚为止。

为长时间任务加上限制:

对非普通的目标,务必加上轮数或时间限制。没有限制的话,一个无法达成的目标会永久运行。

/goal fix checkout tests, or stop after 10 turns
/goal complete the migration, or stop after 30 minutes

Claude 每轮都会追踪并报告相对于限制的进度,评估器会判断限制是否已触发。

非交互式运行 /goal:

claude -p "/goal CHANGELOG.md has an entry for every PR merged this week"

这一条就把整个循环跑到底。用 Ctrl+C 终止。非常适合 CI 流水线和定时脚本。

与 Auto 模式结合,实现完全无人值守:

/goal 去掉了轮次间的提示,Auto 模式去掉了工具调用的提示。

两者叠加——Claude 完全自主工作,直到条件满足或限制触发。这是当前 Claude Code 中最接近完全自主编码智能体的形态。

崩溃后恢复:

如果一次会话结束时 /goal 仍在活动,当你使用 --resume 或 --continue 恢复会话时它会自动恢复。条件继续保留,轮次计数、计时器和 token 消耗重置,从停下处无缝继续。

§ 5

COMMAND 2 → /loop

/loop runs Claude on repeat on a time interval — not until a condition is met, but on a cadence you define.

Where /goal says "keep going until this is true" — /loop says "keep going every N minutes until I stop you."

Best for:

→ Iterative refactors where you want Claude to make a pass, pause, let you review, then make another

→ Monitoring tasks that need repeated checks

→ Burning down a backlog incrementally — Claude takes one item per loop, you see progress in real time

→ Polling an external state that changes over time

The difference from /goal in one line:

/goal stops when a condition is confirmed true.

/loop stops when you stop it, or when Claude decides the work is done.

COMMAND 2 → /loop

/loop 让 Claude 按你设定的时间间隔重复运行——不是在条件满足时停止,而是在你定义的节奏上持续运行。

/goal 说的是“一直运行直到条件成立”,/loop 说的是“每隔 N 分钟运行一次,直到我叫停”。

最适合的场景:

→ 递进式重构:你希望 Claude 先改动一步,暂停,你审查,然后再进行下一步

→ 需要重复检查的监控任务

→ 渐进式消化任务积压——Claude 每轮处理一项,你实时看到进展

→ 轮询随时间变化的外部状态

与 /goal 的本质区别用一句话说清:

/goal 在条件被确认成立时停止。

/loop 在你叫停时停止,或当 Claude 判断工作完成时停止。

§ 6

COMMAND 3 → /schedule (Scheduled Tasks)

/schedule kicks off Claude on a fixed cadence — independent of any open session. It runs whether or not you have Claude Code open.

Where /goal and /loop keep the current session running — /schedule creates work that happens on its own, in the background, on a timer.

What you can schedule:

Nightly test runs — Claude runs your full test suite at 2am, posts a summary to Slack.

Morning triage — Claude reviews open issues every morning, labels them, flags blockers.

Weekly cleanup — Claude scans for dead code, outdated comments, and TODO items every Sunday.

Daily standup prep — Claude pulls recent commits, open PRs, and failing tests into a summary before your 9am.

The key difference from /goal and /loop:

/goal and /loop require an open session. If you close Claude Code, they stop.

/schedule runs completely independently. You set it once, it runs on its own cadence, forever — or until you cancel it.

This is how you turn Claude Code into infrastructure, not just a tool you use when you remember to open it

COMMAND 3 → /schedule(定时任务)

/schedule 让 Claude 在固定的节奏上启动任务——完全独立于任何打开的会话。无论你是否打开了 Claude Code,它都会运行。

/goal 和 /loop 要保持当前会话运行,而 /schedule 创建的是在后台自行发生、随定时器触发的工作。

你可以安排什么:

夜间测试运行——Claude 在凌晨 2 点运行你的完整测试套件,并将摘要发到 Slack。

晨间分诊——Claude 每天早上审查未解决的 issue,打标签,标记阻塞项。

每周清理——Claude 每周日扫描死代码、过时的注释和 TODO 项。

每日站会准备——Claude 在早上 9 点前拉取最近的提交、打开的 PR 和失败的测试,汇总成报告。

与 /goal 和 /loop 的关键区别:

/goal 和 /loop 需要打开的会话。关闭 Claude Code 它们就停了。

/schedule 完全独立运行。设置一次后,它会按照自己的节奏永远运行——或者直到你取消它。

这就是将 Claude Code 变成基础设施的方法,而不只是你想起时才打开的工具。

§ 7

COMMAND 4 → STOP HOOKS

A Stop hook gives you programmatic, scriptable control over exactly when Claude is allowed to finish a turn.

/goal uses a Stop hook under the hood — it's a session-scoped prompt-based hook that fires after every turn. But you can write your own Stop hooks for complete custom control.

Two types:

▸ Script-based Stop hooks

Run your actual test suite. Hit a CI endpoint. Check a file exists. Query a database. Any deterministic check — if the script exits 0, Claude can stop. If not, Claude keeps going.

This is the most powerful pattern: Claude works, your tests run, if they fail Claude tries again. No human in the loop at all.

▸ Prompt-based Stop hooks

A model evaluates a natural-language condition against the transcript — exactly like /goal. The difference: Stop hooks live in your settings file and apply to every session in their scope. /goal is session-only and disposable.

When to use a Stop hook vs /goal:

Use /goal when: you want a one-off condition for this session only.

Use a Stop hook when: you want the same evaluation logic applied to every session — or when you need a script to run deterministic checks that a model can't evaluate from a transcript alone.

The combination that changes everything:

/goal + Auto mode + a script-based Stop hook that runs your test suite.

Claude works autonomously. After every turn, your tests run. If they pass — Claude stops. If they fail — Claude reads the failure output and tries again. You come back to a green build or a clear explanation of why it couldn't get there.

COMMAND 4 → STOP HOOKS

Stop hook 让你能以程序化、脚本化的方式精确控制 Claude 何时允许结束一个回合。

/goal 底层就使用了 Stop hook——它是一个会话级别的、基于提示词的 hook,每个回合后触发。但你可以编写自己的 Stop hook,实现完全自定义控制。

两种类型:

▸ 基于脚本的 Stop hook

运行你真正的测试套件。调用 CI 端点。检查文件是否存在。查询数据库。任何确定性的检查——如果脚本以退出码 0 结束,Claude 可以停下来。如果不是,Claude 继续运行。

这是最强大的模式:Claude 工作,你的测试运行,如果失败,Claude 读取失败输出并重试。整个过程无需人工介入。

▸ 基于提示词的 Stop hook

由一个模型根据对话记录对自然语言条件进行评估——与 /goal 完全一样。区别在于:Stop hook 在你的设置文件中配置,对其作用域内的所有会话生效。/goal 则只针对当前会话,用完即弃。

何时用 Stop hook,何时用 /goal:

用 /goal:当你只需要针对本次会话的一次性条件。

用 Stop hook:当你想让相同的评估逻辑应用到所有会话时——或者当你需要脚本执行确定性检查,而这些检查无法仅凭对话记录让模型评估时。

真正带来根本改变的组合:

/goal + Auto 模式 + 运行你测试套件的脚本式 Stop hook。

Claude 自主工作。每个回合结束后,你的测试运行。如果通过——Claude 停止。如果失败——Claude 读取失败输出再试一次。你回来时,要么是绿构建,要么是关于为何无法达成的清晰解释。

§ 8

CHOOSING THE RIGHT COMMAND

"I want Claude to keep working until tests pass."

→ /goal all tests pass and npm test exits 0

"I want Claude to make iterative passes on a refactor while I review between each one."

→ /loop

"I want my test suite to run every night without me."

→ /schedule

"I want a script to decide when Claude is allowed to stop — not a model."

→ Stop hook (script-based)

"I want the same condition applied to every session automatically."

→ Stop hook (prompt-based, in settings file)

"I want Claude to work completely unattended — no per-turn prompts, no per-tool prompts."

→ /goal + Auto mode

如何选择合适的指令

“我想让 Claude 一直工作直到测试通过。”

→ /goal all tests pass and npm test exits 0

“我想让 Claude 逐步迭代重构,我在每轮之间审查。”

→ /loop

“我想让我的测试套件每晚自动运行,无需我动手。”

→ /schedule

“我想用一个脚本来决定 Claude 何时可以停下来,而不是模型决定。”

→ Stop hook(基于脚本)

“我想让相同的条件自动应用于每一个会话。”

→ Stop hook(基于提示词,配置在设置文件中)

“我想让 Claude 完全无人值守地工作——没有轮次提示,没有工具调用提示。”

→ /goal + Auto 模式

§ 9

THE QUICK REFERENCE

Set a goal:

/goal [condition]

Check status:

/goal

Stop early:

/goal clear

Run non-interactively:

claude -p "/goal [condition]"

Add a safety limit:

/goal [condition], or stop after [N turns / N minutes]

The master template:

/goal [task] until [success condition], verified by [check], while [constraints], or stop after [limit]

快速参考

设定目标:

/goal [condition]

查看状态:

/goal

提前终止:

/goal clear

非交互式运行:

claude -p "/goal [condition]"

加上安全限制:

/goal [condition], or stop after [N turns / N minutes]

万能模板:

/goal [task] until [success condition], verified by [check], while [constraints], or stop after [limit]

§ 10

Most developers will read this and go back to manually prompting every step.

The ones who set up /goal + Auto mode on their next task will never go back.

Set a condition. Walk away. Come back to a result.

That's the whole idea.

Follow for more Claude Code deep-dives every week.


大多数开发者读完后,会继续一步步手动输入提示。

而那些在下一个任务里设置好 /goal + Auto 模式的人,再也不会回头。

设定好条件,起身离开,回来时拿到结果。

这就是核心思想。

每周关注,获取更多 Claude Code 深度解析。

打开原文 ↗