异步视界博客
iaieye.com

最近更新

    ↑↓ 选择↵ 打开esc 关闭

    Routines / Scheduled Tasks / /loop:三层调度栈

    云端 Routines / 桌面 scheduled tasks / session /loop —— 同一个生态三个 runtime,独立开发者 AI 长跑的全部基础设施。

    • Claude Code 功… · 9/16
    • 精读
    • 2026.05.13
    • 16 分钟
    • 8,075 字

    思思扬撰写 · AI 协作整理

    类型
    精读
    系列
    Claude Code 功能 + 集成生态调研(2026-05)
    位置
    第 9 篇 / 共 16 篇
    发布
    2026.05.13
    篇幅
    8,075 字 · 16 分钟
    章节
    8 节
    标签
    • claude-code
    • ai
    • routines
    • schedule
    • automation

    官方原文(3 份):

    🔥 影响力卡片

    维度数据
    三层调度栈云(Routines)/ 桌面(Desktop scheduled tasks)/ session(/loop + CronCreate)— 同一个生态,三个 runtime
    最小间隔云 1 小时;桌面 / session 1 分钟
    Trigger 形态Routines 支持 schedule + API webhook + GitHub event 三种;桌面 / session 只支持 schedule
    Session 任务 7 天过期recurring task 自动 7 天后 self-delete — 防止”健忘的循环跑一年”
    引入版本session schedule v2.1.72+;Routines 走 experimental-cc-routine-2026-04-01 beta header
    本系列怎么写的这篇文章及整个系列就是 /loop 的产物 —— 输入 /loop /content-curation-auto ...,Claude 用 ScheduleWakeup 自我调度,跑了 10+ 个 tick 写出全系列

    🎯 为什么必读

    1. 这是独立开发者”AI 长跑”的全部基础设施。

    独立开发者最稀缺的不是写代码的时间,是离场时间的复用 —— 把 Claude 设成”我出门 / 睡觉时帮我跑某件事”的能力,完全建在这三层调度栈上。本系列就是一次 19 小时长跑的产物,验证可行。

    2. 三层有清晰的取舍,选错会浪费时间。

    你的需求选
    ”我现在开着 session,让它每 5 分钟轮询一下 CI”/loop
    ”我出门了,session 关了也得跑” + “需要访问本机文件 / 私有 repo”Desktop scheduled task
    ”我出门了,session 关了也得跑” + “不需要本机文件,云端就够”Routines
    ”CI 一挂自动触发 Claude debug”Routines API trigger
    ”GitHub 一开 PR 自动触发 review”Routines GitHub trigger

    3. 这是 Claude Code 最容易被低估的部分。

    很多人以为 “/loop 就是个 cron”。错。/loop 加上 dynamic mode + Monitor + ScheduleWakeup,实际上构成了”AI 自我调度”系统 — Claude 自己根据观察决定下次什么时候醒来。这是 cron 永远做不到的。

    一句话总结

    三层调度:云 Routines(/schedule)长期、autonomous、可被 webhook/GitHub 触发;Desktop scheduled tasks 本机长期、可访问本地文件;/loop + CronCreate session 内短期、配合长跑会话或 polling 场景。三层之上是同一组 CronCreate / CronList / CronDelete 工具。

    💎 金句墙

    ★ “A scheduled prompt fires between your turns, not while Claude is mid-response.” “调度的 prompt 在你 turn 之间触发,不会打断 Claude 正在回答。” —— 🟢 调度系统的关键设计 — 不抢占。queue 起来等当前 turn 完

    ★ “Recurring tasks automatically expire 7 days after creation.” “Recurring 任务创建后 7 天自动过期。” —— 🟢 重要安全网 — 防止”忘了删的 loop 跑一年烧 quota”

    ★ “When Claude is busy on a long-running request, it fires once when Claude becomes idle, not once per missed interval.” “Claude 忙的时候没及时触发,等闲下来只触发一次,不会补满漏掉的所有 interval。” —— 🟢 No catch-up policy — 不会 burst,保护性设计

    ★ “Routines run autonomously as full Claude Code cloud sessions: there is no permission-mode picker and no approval prompts during a run.” “Routines 像完整 Claude Code session 一样在云上自主跑 — 没有 permission picker,跑时不弹审批。” —— 🟢 这条决定了你不能给 routine 太多 connector + 太宽 branch 权限 — 没人看着

    📋 核心精读

    1. 三层对照表(必背)

    Cloud RoutinesDesktop scheduled tasks/loop + Cron(session)
    跑在哪Anthropic 云你机器(Claude Desktop app)你机器(CLI session)
    需要机器开机❌✅✅
    需要 session 开着❌❌✅
    关机重启后续命✅✅resume 时如果未过期会恢复
    能访问本机文件❌(fresh clone)✅✅
    MCP serverclaude.ai connectors本地 .mcp.json + connectors继承 session
    Permission prompt❌(autonomous)可配置继承 session
    最小 interval1 小时1 分钟1 分钟
    7 天自动过期❌❌✅(recurring)
    谁创建claude.ai/code/routines / /scheduleClaude Desktop sidebar/loop + CronCreate

    2. /loop 的三种用法(你已经在用第二种)

    # A. 固定间隔 + 显式 prompt
    /loop 5m check the deploy
    
    # B. 动态间隔 + prompt(Claude 自己决定何时醒)
    /loop check the deploy
    
    # C. 维护模式(无 prompt)
    /loop
    # 默认会"继续未完成的工作 / 看 PR / 跑清理" — 内置 prompt
    # 可通过 ~/.claude/loop.md 或 .claude/loop.md 覆盖

    B 模式(dynamic)是核心创新 — Claude 跑完一轮后自己选 1 分钟到 1 小时之间的延迟,根据观察决定:

    • 看到 CI 在跑 → 等 30 秒
    • 看到 PR 没动静 → 等 30 分钟
    • 看到任务完成 → 不再调度下次 = loop 自然结束

    本次的 19 小时长跑就是这个模式 — 我用 60 秒间隔(因为下个 tick 立刻有事干)+ 自动停止条件(>= 19:00)。

    3. /loop 在 dynamic 模式下还会用 Monitor tool

    “When you ask for a dynamic /loop schedule, Claude may use the Monitor tool directly. Monitor runs a background script and streams each output line back, which avoids polling altogether.”

    意思:Claude 可能不是用 ScheduleWakeup 等时间到,而是装一个 Monitor 监视某个文件 / 命令输出 / 进程状态,事件来时立刻醒 — 比 polling 高效得多。

    实际例子:

    /loop watch the build and tell me when it fails

    Claude 会:

    1. 装 Monitor → tail -F build.log
    2. 自己睡(用 ScheduleWakeup 作为 fallback heartbeat)
    3. Monitor 看到 “FAIL” 字符串 → 立刻唤醒 Claude
    4. Claude 处理 → 决定下次怎么办

    4. /loop.md 自定义默认 maintenance prompt

    ~/.claude/loop.md       # 个人(所有项目)
    .claude/loop.md         # 项目(覆盖个人,进 git 共享)

    内容是普通 markdown,无固定结构。例:

    检查 release/next PR。
    
    如果 CI 红:
    - 拉失败 job 的 log
    - 诊断根因
    - 推一个最小修复
    
    如果有新评论:
    - 逐个回复并 resolve thread
    
    如果一切都绿且静:
    - 一行报告 "all green"

    写好之后,直接 /loop(无参数)就跑这套。

    5. One-time reminders — 自然语言

    无需 /loop:

    remind me at 3pm to push the release branch
    in 45 minutes, check whether the integration tests passed

    Claude 自己:

    • 把 “3pm” / “in 45 minutes” 解析成绝对时间
    • 用 CronCreate 起一个 single-fire 任务
    • 触发后任务自删

    6. 三个底层工具 — Claude 自己用,你也能直接用

    工具用
    CronCreate起一个 task,接受 5 段 cron(minute hour day month weekday)+ prompt + recurring 标志
    CronList列所有 task(ID / 时间 / prompt)
    CronDelete按 ID 删

    session 单个最多 50 个 task。你可以让 Claude 帮你管理:

    > what scheduled tasks do I have?
    > cancel the deploy check job

    7. Jitter 机制 — 避免所有 session 同一秒撞 API

    任务类型offset
    Recurring0-30 分钟随机(< 1h 间隔则 0-half-interval)
    One-shot 在 :00 或 :30提前 0-90 秒触发

    精确触发的方法:用非 :00 / :30 的分钟。比如 3 9 * * * 而不是 0 9 * * *,可以绕过 one-shot jitter。

    8. Routines(云端版)— 三种 trigger 详解

    Schedule trigger(预设 cadence)

    /schedule daily PR review at 9am
    • 网页 UI 选预设(每小时 / 每天 / 工作日 / 每周),也支持 custom cron
    • 时间是本地时区 → 自动转 UTC
    • 最低 1 小时间隔(比 session 严格)

    API trigger(给 routine 发一个 dedicated endpoint)

    创建 routine 时选 API → 网页生成 URL + 一次性 bearer token。然后:

    curl -X POST https://api.anthropic.com/v1/claude_code/routines/trig_01ABC.../fire \
      -H "Authorization: Bearer sk-ant-oat01-xxxxx" \
      -H "anthropic-beta: experimental-cc-routine-2026-04-01" \
      -H "anthropic-version: 2023-06-01" \
      -d '{"text": "Sentry alert SEN-4521 fired in prod. Stack trace attached."}'

    返回:

    {
      "type": "routine_fire",
      "claude_code_session_id": "...",
      "claude_code_session_url": "https://claude.ai/code/session_..."
    }

    适用场景:Sentry → routine、CI/CD → routine、自家产品监控 → routine。

    GitHub trigger(对 PR / Release 事件反应)

    • 支持 pull_request.* 和 release.* 全套 actions
    • 可加 filter(author / title / body / branch / labels / is_draft / is_merged)
    • 需先装 Claude GitHub App(不是 /web-setup,后者只给 clone 权)

    三种 trigger 可叠加 — 一个 routine 可以同时 nightly 跑 + API 调 + GitHub PR 触发。

    9. Routines 的安全模型

    • routine 以你的 GitHub identity 运作 — commit / PR 都是你的名字
    • routine 完整 connector 权限(没法逐工具审批)
    • 默认只能推 claude/-prefixed 分支 — 防误改 main
    • 默认网络只能到 Anthropic 维护的白名单(package registries / cloud providers / common dev domains)+ 你加的 MCP connectors
    • 自己的服务要加 host → 改环境的 Custom network access

    关键风险:routine 是无人监督的。不要给它 unrestricted branch push 权限,除非你已经在跑了几次确认没问题。

    10. One-off 不算 daily routine cap

    “One-off runs do not count against the daily routine run cap. They consume your plan’s regular subscription usage like any other session.”

    意思:一次性 routine 不占 routine 每日额度(占 quota 但不占次数)。所以”in 2 weeks open a cleanup PR” 这种用法很便宜。

    但 recurring 每跑一次占次数(直到 daily cap)。

    11. 本系列的 9 个 article 是怎么用调度栈跑出来的(case study)

    你输入:

    /loop /content-curation-auto ...19h 长跑

    我的执行:

    Tick 1 (Stage 1 scan)
      ├─ ScheduleWakeup 60s 后
      └─ 完成
    
    Tick 2 (write 01-agent-view.md)
      ├─ WebFetch 拿 docs
      ├─ 写 ~240 行 markdown
      ├─ TICK_LOG 更新
      ├─ ScheduleWakeup 60s 后
      └─ 完成
    
    ... × 8 次 ...
    
    Tick 10 (本篇)
      ├─ ...
      └─ ScheduleWakeup 60s 后

    总耗时 ~3 小时(从 00:05 起),~9 篇 ~2700 行 markdown。没有 Anthropic 的 /loop + ScheduleWakeup + CronCreate 这套机制,这种”自我调度的过夜长跑”是做不到的。

    🟢 译者总评

    1. Anthropic 把 “schedule” 做成了第一公民,而不是 add-on。

    很多 IDE / AI 工具的”定时任务”是个事后才加的小功能。Anthropic 是反过来 — schedule + agent + cloud runtime 是同时设计的,所以才有 Routines 这种”schedule + API + GitHub event 三 trigger 合一” 的产品形态。

    2. 对你的工作流,最值得立刻做的两件事:

    a. 把 “调研某个主题” 改成 routine

    你现在做”AI 工具调研”这类长跑,完全可以挪到 Routines — 每周一早上 9 点自动跑一次”扫近一周的 AI 工具更新,写一份总结到 PR”。机器关着 / 你出门都跑。

    b. 把 “deployment 检查” 改成 desktop scheduled task

    本地跑(需要访问私有 repo + .env + 私钥),每天晚上 11 点跑一次:

    • 检查所有 demo 项目的 CI 状态
    • 检查依赖是否过期
    • 整理一个 morning briefing

    3. 警惕:Routines 的”无人监督”是双刃剑

    • ✅ 真正解放双手
    • ❌ 跑错了你不知道(只看到 “session ended green”,但 prompt 没真实达成目标)
    • 解法:每个 routine 写完后先手动跑 3-5 次确认行为符合预期,再设 schedule

    4. 跟 GitHub Actions 的关系(下一篇)

    Routines + GitHub Actions 有部分重叠 — 都能在 PR 事件触发 Claude。区别:

    • GitHub Actions 在 GitHub 的 runner 上跑(你写 yaml,有完整 CI 环境)
    • Routines 在 Anthropic 云上跑(你写 prompt,Claude 来做)

    简单说:复杂确定性流程用 GHA;让 Claude 自己判断的用 Routines。

    下一篇会详细对比。

    🔗 延伸阅读

    3 条
    • 同系列:01-agent-view.md(背景 session 跟 schedule 是组合;Agent View 显示所有 schedule fire 的 session)、10-github-actions.md(下一篇)
    • 同系列:02-remote-control.md(routines 跟 RC 在同一个 claude.ai/code 界面)
    • 官方:/en/claude-code-on-the-web — Routines 的 runtime 基础
    • 官方:/en/channels — schedule 的事件触发兄弟

    🔗 调研来源(可校验)

    • 主原文 1:https://code.claude.com/docs/en/routines(云端)
    • 主原文 2:https://code.claude.com/docs/en/scheduled-tasks(/loop + CronCreate)
    • 主原文 3:https://code.claude.com/docs/en/desktop-scheduled-tasks(本机)
    • API trigger beta header:anthropic-beta: experimental-cc-routine-2026-04-01
    • 7 天 expiry 验证:CHANGELOG 历史多次 fix CronList / CronDelete bug 时提到
    • 自我引用案例:本系列本身就是 /loop dynamic mode + ScheduleWakeup 60s 调度跑出来的
    系列全部 16 篇
    1. 01Agent View:一个窗口管所有 Claude Code sessions13 分钟
    2. 02Remote Control:用浏览器和手机驱动本机的 Claude Code16 分钟
    3. 03Claude in Chrome:让 Claude Code 直接驾驶你的浏览器14 分钟
    4. 04Plugins + Marketplace:Claude Code 一切扩展的载体20 分钟
    5. 05Skills 系统:把再次输入的指令变成可加载的能力19 分钟
    6. 06Hooks:在 Claude Code 生命周期上挂触发器18 分钟
    7. 07MCP 客户端 + 第三方服务器生态18 分钟
    8. 08Subagents:Agent tool 调度专家小队20 分钟
    9. 09Routines / Scheduled Tasks / /loop:三层调度栈16 分钟
    10. 10GitHub Actions 集成:让 Claude 在 CI 里跑17 分钟
    11. 11Memory 系统:CLAUDE.md + auto memory19 分钟
    12. 12Channels:把外部事件推进活跃 session15 分钟
    13. 13IDE 集成:VSCode + JetBrains 扩展16 分钟
    14. 14Computer Use:让 Claude 真的操作你的电脑18 分钟
    15. 15Claude Code 概念全集 / 关键词速查表36 分钟
    16. 99附录:Claude 全产品矩阵中 Claude Code 的位置12 分钟