无人值守 · Headless、SDK 与 CIUnattended · Headless, the SDK, and CI.
到这里,Claude Code 还都在你眼前的终端里。但它也能离开终端:headless 模式(claude -p)让它无人值守地跑一次;Agent SDK 让你把它嵌进自己的脚本和服务;GitHub Actions 让它在每个 PR 上自动跑一遍。这是「派活」的极限形态 —— 活不在你这台机器、不在你盯着的时候完成。这一章讲哪类活适合无人值守(批处理、CI 检查、定时任务),怎么用 SDK 把它编排进流水线,以及无人时最该守住的那条:它跑得越自动,你越要在它动手的边界上设死规矩。So far Claude Code has stayed in the terminal in front of you. But it can leave: headless mode (claude -p) runs it once, unattended; the Agent SDK embeds it in your own scripts and services; GitHub Actions runs it on every PR. This is delegation's furthest form — work finished off your machine, when you're not watching. Which work suits unattended runs (batch jobs, CI checks, scheduled tasks), how to orchestrate it into a pipeline with the SDK, and the line to hold when no one's watching: the more automatically it runs, the harder the rules you set at the edges of what it may do.
10 分钟 · 初稿 2026.0710 Min · Drafted 2026.07
到这里,Claude Code 还都在你眼前:终端开着,活跑着,你在场。这一章拆掉最后一个前提。headless 让它无人值守跑一次,SDK 让它长进你的程序,GitHub Actions 让它守在每个 PR 上,Routines 让它半夜也上班 —— 派活的极限形态:活不在你这台机器、不在你醒着的时候完成。也因此,这一章的另一半讲规矩:它跑得越自动,边界越要在出发前写死。Until now, Claude Code has stayed in front of you: terminal open, work running, you present. This chapter removes that last premise. Headless runs it once unattended, the SDK grows it into your programs, GitHub Actions posts it on every PR, Routines put it on the night shift — delegation's furthest form: work finished off your machine, outside your waking hours. Which is why the other half of this chapter is rules: the more automatically it runs, the more the boundaries must be written down before it leaves.
— I
headless:一条命令,进出都是管道Headless: One Command, Pipes Both Ways.
把交互会话变成一条 Unix 命令,只需要一个 -p:claude -p "提示" 跑完就退,stdin 能灌数据进去,stdout 能接进下一个程序。1注 1Note 1Claude Code Docs · Run Claude Code programmatically —— claude -p(--print)非交互跑一次:读 stdin、可管道进出;--output-format 选 text / json / stream-json,--json-schema 按 JSON Schema 约束结构化输出(结果在 structured_output 字段);--allowedTools 按权限规则语法预批工具;--bare 跳过 hooks / skills / MCP / CLAUDE.md 的自动发现,保证脚本在每台机器行为一致,是脚本化调用的推荐模式;--no-session-persistence 不留会话;文档场景即 CI、构建脚本与管道。截至 2026-07-17。Claude Code Docs · Run Claude Code programmatically — claude -p (--print) runs once, non-interactively: reads stdin, pipes in and out; --output-format offers text / json / stream-json, --json-schema constrains structured output against a JSON Schema (result in the structured_output field); --allowedTools pre-approves tools in permission-rule syntax; --bare skips auto-discovery of hooks, skills, MCP, and CLAUDE.md so a script behaves identically on every machine — the recommended mode for scripted calls; --no-session-persistence keeps no session. The documented scenarios are CI, build scripts, and pipelines. As of 2026-07-17.它把 Claude Code 从「一个你打开的应用」变成「一个脚本里的工序」:Turning the interactive session into a Unix command takes one flag: claude -p "prompt" runs once and exits, with stdin feeding data in and stdout flowing into the next program.1注 1Note 1Claude Code Docs · Run Claude Code programmatically —— claude -p(--print)非交互跑一次:读 stdin、可管道进出;--output-format 选 text / json / stream-json,--json-schema 按 JSON Schema 约束结构化输出(结果在 structured_output 字段);--allowedTools 按权限规则语法预批工具;--bare 跳过 hooks / skills / MCP / CLAUDE.md 的自动发现,保证脚本在每台机器行为一致,是脚本化调用的推荐模式;--no-session-persistence 不留会话;文档场景即 CI、构建脚本与管道。截至 2026-07-17。Claude Code Docs · Run Claude Code programmatically — claude -p (--print) runs once, non-interactively: reads stdin, pipes in and out; --output-format offers text / json / stream-json, --json-schema constrains structured output against a JSON Schema (result in the structured_output field); --allowedTools pre-approves tools in permission-rule syntax; --bare skips auto-discovery of hooks, skills, MCP, and CLAUDE.md so a script behaves identically on every machine — the recommended mode for scripted calls; --no-session-persistence keeps no session. The documented scenarios are CI, build scripts, and pipelines. As of 2026-07-17. It turns Claude Code from an app you open into a step in a script:
三种管道姿势:灌日志、当 linter、出结构化结果Three pipe stances: feed logs, act as a linter, emit structured results
第三条是无人化的关键零件:--output-format json 让结果带上元数据,--json-schema 更进一步 —— 按你给的 schema 约束输出,脚本直接消费 structured_output 字段,不用解析散文。1注 1Note 1Claude Code Docs · Run Claude Code programmatically —— claude -p(--print)非交互跑一次:读 stdin、可管道进出;--output-format 选 text / json / stream-json,--json-schema 按 JSON Schema 约束结构化输出(结果在 structured_output 字段);--allowedTools 按权限规则语法预批工具;--bare 跳过 hooks / skills / MCP / CLAUDE.md 的自动发现,保证脚本在每台机器行为一致,是脚本化调用的推荐模式;--no-session-persistence 不留会话;文档场景即 CI、构建脚本与管道。截至 2026-07-17。Claude Code Docs · Run Claude Code programmatically — claude -p (--print) runs once, non-interactively: reads stdin, pipes in and out; --output-format offers text / json / stream-json, --json-schema constrains structured output against a JSON Schema (result in the structured_output field); --allowedTools pre-approves tools in permission-rule syntax; --bare skips auto-discovery of hooks, skills, MCP, and CLAUDE.md so a script behaves identically on every machine — the recommended mode for scripted calls; --no-session-persistence keeps no session. The documented scenarios are CI, build scripts, and pipelines. As of 2026-07-17.第 9 章说的「回归集用 claude -p 重跑、拿 JSON 对一对」,零件就是这两个 flag。脚本化调用再加一个 --bare:跳过 hooks、skills、MCP、CLAUDE.md 的自动发现,保证同一条命令在每台机器行为一致 —— CI 里尤其该加。1注 1Note 1Claude Code Docs · Run Claude Code programmatically —— claude -p(--print)非交互跑一次:读 stdin、可管道进出;--output-format 选 text / json / stream-json,--json-schema 按 JSON Schema 约束结构化输出(结果在 structured_output 字段);--allowedTools 按权限规则语法预批工具;--bare 跳过 hooks / skills / MCP / CLAUDE.md 的自动发现,保证脚本在每台机器行为一致,是脚本化调用的推荐模式;--no-session-persistence 不留会话;文档场景即 CI、构建脚本与管道。截至 2026-07-17。Claude Code Docs · Run Claude Code programmatically — claude -p (--print) runs once, non-interactively: reads stdin, pipes in and out; --output-format offers text / json / stream-json, --json-schema constrains structured output against a JSON Schema (result in the structured_output field); --allowedTools pre-approves tools in permission-rule syntax; --bare skips auto-discovery of hooks, skills, MCP, and CLAUDE.md so a script behaves identically on every machine — the recommended mode for scripted calls; --no-session-persistence keeps no session. The documented scenarios are CI, build scripts, and pipelines. As of 2026-07-17.The third stance is the key part for unattended work: --output-format json wraps the result with metadata, and --json-schema goes further — constraining output against your schema so scripts consume the structured_output field instead of parsing prose.1注 1Note 1Claude Code Docs · Run Claude Code programmatically —— claude -p(--print)非交互跑一次:读 stdin、可管道进出;--output-format 选 text / json / stream-json,--json-schema 按 JSON Schema 约束结构化输出(结果在 structured_output 字段);--allowedTools 按权限规则语法预批工具;--bare 跳过 hooks / skills / MCP / CLAUDE.md 的自动发现,保证脚本在每台机器行为一致,是脚本化调用的推荐模式;--no-session-persistence 不留会话;文档场景即 CI、构建脚本与管道。截至 2026-07-17。Claude Code Docs · Run Claude Code programmatically — claude -p (--print) runs once, non-interactively: reads stdin, pipes in and out; --output-format offers text / json / stream-json, --json-schema constrains structured output against a JSON Schema (result in the structured_output field); --allowedTools pre-approves tools in permission-rule syntax; --bare skips auto-discovery of hooks, skills, MCP, and CLAUDE.md so a script behaves identically on every machine — the recommended mode for scripted calls; --no-session-persistence keeps no session. The documented scenarios are CI, build scripts, and pipelines. As of 2026-07-17. Chapter 9's 'rerun the regression set with claude -p and diff the JSON' is built from exactly these two flags. For scripted calls, add --bare: it skips auto-discovery of hooks, skills, MCP, and CLAUDE.md, so one command behaves identically on every machine — especially in CI.1注 1Note 1Claude Code Docs · Run Claude Code programmatically —— claude -p(--print)非交互跑一次:读 stdin、可管道进出;--output-format 选 text / json / stream-json,--json-schema 按 JSON Schema 约束结构化输出(结果在 structured_output 字段);--allowedTools 按权限规则语法预批工具;--bare 跳过 hooks / skills / MCP / CLAUDE.md 的自动发现,保证脚本在每台机器行为一致,是脚本化调用的推荐模式;--no-session-persistence 不留会话;文档场景即 CI、构建脚本与管道。截至 2026-07-17。Claude Code Docs · Run Claude Code programmatically — claude -p (--print) runs once, non-interactively: reads stdin, pipes in and out; --output-format offers text / json / stream-json, --json-schema constrains structured output against a JSON Schema (result in the structured_output field); --allowedTools pre-approves tools in permission-rule syntax; --bare skips auto-discovery of hooks, skills, MCP, and CLAUDE.md so a script behaves identically on every machine — the recommended mode for scripted calls; --no-session-persistence keeps no session. The documented scenarios are CI, build scripts, and pipelines. As of 2026-07-17.
— II
SDK:同一台引擎,做成库The SDK: the Same Engine, as a Library.
脚本再长,也只是把命令串起来;要把它嵌进你自己的服务 —— 一个自动分诊 bug 的机器人、一个替客服读日志的后台 —— 用 Agent SDK。官方的定义一句话:给你的是驱动 Claude Code 的同一套工具、agent 循环与上下文管理,以 Python 和 TypeScript 可编程。2注 2Note 2Claude Code Docs · Agent SDK overview —— 「Agent SDK 给你的,是驱动 Claude Code 的同一套工具、agent 循环与上下文管理,以 Python 和 TypeScript 可编程」;包名 claude-agent-sdk(Python)与 @anthropic-ai/claude-agent-sdk(TS,自带 Claude Code 二进制);query(prompt, options) 即起一个 agent;其他语言走 claude -p --output-format json。官方分工表:交互开发与一次性任务用 CLI,CI/CD、定制应用与生产自动化用 SDK。截至 2026-07-17。Claude Code Docs · Agent SDK overview — 'the Agent SDK gives you the same tools, agent loop, and context management that power Claude Code, programmable in Python and TypeScript'; packages claude-agent-sdk (Python) and @anthropic-ai/claude-agent-sdk (TS, bundling the Claude Code binary); query(prompt, options) starts an agent; other languages use claude -p --output-format json. The official split: CLI for interactive development and one-offs, SDK for CI/CD, custom applications, and production automation. As of 2026-07-17.包名 claude-agent-sdk(Python)和 @anthropic-ai/claude-agent-sdk(TypeScript),一个 query(prompt, options) 就是一个带全套工具的 agent;其他语言不用等移植,claude -p --output-format json 就是它的通用接口。2注 2Note 2Claude Code Docs · Agent SDK overview —— 「Agent SDK 给你的,是驱动 Claude Code 的同一套工具、agent 循环与上下文管理,以 Python 和 TypeScript 可编程」;包名 claude-agent-sdk(Python)与 @anthropic-ai/claude-agent-sdk(TS,自带 Claude Code 二进制);query(prompt, options) 即起一个 agent;其他语言走 claude -p --output-format json。官方分工表:交互开发与一次性任务用 CLI,CI/CD、定制应用与生产自动化用 SDK。截至 2026-07-17。Claude Code Docs · Agent SDK overview — 'the Agent SDK gives you the same tools, agent loop, and context management that power Claude Code, programmable in Python and TypeScript'; packages claude-agent-sdk (Python) and @anthropic-ai/claude-agent-sdk (TS, bundling the Claude Code binary); query(prompt, options) starts an agent; other languages use claude -p --output-format json. The official split: CLI for interactive development and one-offs, SDK for CI/CD, custom applications, and production automation. As of 2026-07-17.CLI 和 SDK 怎么分工,官方表格说得干脆:交互开发、一次性任务用 CLI;CI/CD、定制应用、生产自动化用 SDK。2注 2Note 2Claude Code Docs · Agent SDK overview —— 「Agent SDK 给你的,是驱动 Claude Code 的同一套工具、agent 循环与上下文管理,以 Python 和 TypeScript 可编程」;包名 claude-agent-sdk(Python)与 @anthropic-ai/claude-agent-sdk(TS,自带 Claude Code 二进制);query(prompt, options) 即起一个 agent;其他语言走 claude -p --output-format json。官方分工表:交互开发与一次性任务用 CLI,CI/CD、定制应用与生产自动化用 SDK。截至 2026-07-17。Claude Code Docs · Agent SDK overview — 'the Agent SDK gives you the same tools, agent loop, and context management that power Claude Code, programmable in Python and TypeScript'; packages claude-agent-sdk (Python) and @anthropic-ai/claude-agent-sdk (TS, bundling the Claude Code binary); query(prompt, options) starts an agent; other languages use claude -p --output-format json. The official split: CLI for interactive development and one-offs, SDK for CI/CD, custom applications, and production automation. As of 2026-07-17.这本书讲到这条线为止 —— 再往里,是《Agent 实战》整本书的地界。However long the script, it's still commands in a row; to embed this into your own service — a bot that triages bugs, a backend that reads logs for support — use the Agent SDK. The official definition in one line: it gives you the same tools, agent loop, and context management that power Claude Code, programmable in Python and TypeScript.2注 2Note 2Claude Code Docs · Agent SDK overview —— 「Agent SDK 给你的,是驱动 Claude Code 的同一套工具、agent 循环与上下文管理,以 Python 和 TypeScript 可编程」;包名 claude-agent-sdk(Python)与 @anthropic-ai/claude-agent-sdk(TS,自带 Claude Code 二进制);query(prompt, options) 即起一个 agent;其他语言走 claude -p --output-format json。官方分工表:交互开发与一次性任务用 CLI,CI/CD、定制应用与生产自动化用 SDK。截至 2026-07-17。Claude Code Docs · Agent SDK overview — 'the Agent SDK gives you the same tools, agent loop, and context management that power Claude Code, programmable in Python and TypeScript'; packages claude-agent-sdk (Python) and @anthropic-ai/claude-agent-sdk (TS, bundling the Claude Code binary); query(prompt, options) starts an agent; other languages use claude -p --output-format json. The official split: CLI for interactive development and one-offs, SDK for CI/CD, custom applications, and production automation. As of 2026-07-17. The packages are claude-agent-sdk (Python) and @anthropic-ai/claude-agent-sdk (TypeScript); one query(prompt, options) is one fully-tooled agent, and other languages needn't wait for ports — claude -p --output-format json is the universal interface.2注 2Note 2Claude Code Docs · Agent SDK overview —— 「Agent SDK 给你的,是驱动 Claude Code 的同一套工具、agent 循环与上下文管理,以 Python 和 TypeScript 可编程」;包名 claude-agent-sdk(Python)与 @anthropic-ai/claude-agent-sdk(TS,自带 Claude Code 二进制);query(prompt, options) 即起一个 agent;其他语言走 claude -p --output-format json。官方分工表:交互开发与一次性任务用 CLI,CI/CD、定制应用与生产自动化用 SDK。截至 2026-07-17。Claude Code Docs · Agent SDK overview — 'the Agent SDK gives you the same tools, agent loop, and context management that power Claude Code, programmable in Python and TypeScript'; packages claude-agent-sdk (Python) and @anthropic-ai/claude-agent-sdk (TS, bundling the Claude Code binary); query(prompt, options) starts an agent; other languages use claude -p --output-format json. The official split: CLI for interactive development and one-offs, SDK for CI/CD, custom applications, and production automation. As of 2026-07-17. How CLI and SDK divide, the official table says crisply: CLI for interactive development and one-offs; SDK for CI/CD, custom applications, and production automation.2注 2Note 2Claude Code Docs · Agent SDK overview —— 「Agent SDK 给你的,是驱动 Claude Code 的同一套工具、agent 循环与上下文管理,以 Python 和 TypeScript 可编程」;包名 claude-agent-sdk(Python)与 @anthropic-ai/claude-agent-sdk(TS,自带 Claude Code 二进制);query(prompt, options) 即起一个 agent;其他语言走 claude -p --output-format json。官方分工表:交互开发与一次性任务用 CLI,CI/CD、定制应用与生产自动化用 SDK。截至 2026-07-17。Claude Code Docs · Agent SDK overview — 'the Agent SDK gives you the same tools, agent loop, and context management that power Claude Code, programmable in Python and TypeScript'; packages claude-agent-sdk (Python) and @anthropic-ai/claude-agent-sdk (TS, bundling the Claude Code binary); query(prompt, options) starts an agent; other languages use claude -p --output-format json. The official split: CLI for interactive development and one-offs, SDK for CI/CD, custom applications, and production automation. As of 2026-07-17. This book stops at that line — past it lies the whole territory of Agent in Practice.
— III
长在流水线里:每个 PR、每个整点Grown into the Pipeline: Every PR, Every Hour.
第三种形态不属于任何一台机器。装上 GitHub Actions 集成(交互式安装:/install-github-app),在任何 PR 或 issue 里 @claude,它就地分析代码、实现功能、修 bug,开出完整的 PR;anthropics/claude-code-action@v1 也能不等召唤、按你的 workflow 定时或按事件跑。3注 3Note 3Claude Code Docs · GitHub Actions —— 在 PR 或 issue 里 @claude,它分析代码、实现功能、修 bug、开出完整 PR;动作为 anthropics/claude-code-action@v1,交互式安装跑 /install-github-app;API key 放仓库 secrets;成本两笔:GitHub runner 分钟 + API token。每个 PR 无须触发的自动审查是另一条产品线(GitHub Code Review)。截至 2026-07-17。Claude Code Docs · GitHub Actions — mention @claude in a PR or issue and it analyzes code, implements features, fixes bugs, and opens complete PRs; the action is anthropics/claude-code-action@v1, installed interactively via /install-github-app; the API key lives in repository secrets; costs are twofold: GitHub runner minutes + API tokens. Untriggered automatic review on every PR is a separate product line (GitHub Code Review). As of 2026-07-17.记两笔账:CI 里它烧的是 GitHub runner 分钟加 API token —— 认证用的是你放进仓库 secrets 的 API key,按 token 计费。3注 3Note 3Claude Code Docs · GitHub Actions —— 在 PR 或 issue 里 @claude,它分析代码、实现功能、修 bug、开出完整 PR;动作为 anthropics/claude-code-action@v1,交互式安装跑 /install-github-app;API key 放仓库 secrets;成本两笔:GitHub runner 分钟 + API token。每个 PR 无须触发的自动审查是另一条产品线(GitHub Code Review)。截至 2026-07-17。Claude Code Docs · GitHub Actions — mention @claude in a PR or issue and it analyzes code, implements features, fixes bugs, and opens complete PRs; the action is anthropics/claude-code-action@v1, installed interactively via /install-github-app; the API key lives in repository secrets; costs are twofold: GitHub runner minutes + API tokens. Untriggered automatic review on every PR is a separate product line (GitHub Code Review). As of 2026-07-17.这套走的是 Console 的账,和 Pro/Max 订阅额度是两码事,自动化越勤越要单独盯。要「每个 PR 无须触发的自动底审」,那是另一条产品线(GitHub Code Review),配好即每 PR 一审。3注 3Note 3Claude Code Docs · GitHub Actions —— 在 PR 或 issue 里 @claude,它分析代码、实现功能、修 bug、开出完整 PR;动作为 anthropics/claude-code-action@v1,交互式安装跑 /install-github-app;API key 放仓库 secrets;成本两笔:GitHub runner 分钟 + API token。每个 PR 无须触发的自动审查是另一条产品线(GitHub Code Review)。截至 2026-07-17。Claude Code Docs · GitHub Actions — mention @claude in a PR or issue and it analyzes code, implements features, fixes bugs, and opens complete PRs; the action is anthropics/claude-code-action@v1, installed interactively via /install-github-app; the API key lives in repository secrets; costs are twofold: GitHub runner minutes + API tokens. Untriggered automatic review on every PR is a separate product line (GitHub Code Review). As of 2026-07-17.定时的活则交给 Routines:跑在 Anthropic 托管的基础设施上,你的电脑关了它照跑,CLI 里 /schedule 就能建 —— 夜间依赖巡检、晨会前的 PR 摘要,都是它的班次。5注 5Note 5Claude Code Docs · Overview —— 定时的活交给 Routines:跑在 Anthropic 托管的基础设施上,你的电脑关了也继续,可由日程、API 调用或 GitHub 事件触发,CLI 里 /schedule 创建;本机定时任务归桌面端。截至 2026-07-17。Claude Code Docs · Overview — scheduled work goes to Routines: they run on Anthropic-managed infrastructure and keep running with your computer off, triggered by schedule, API calls, or GitHub events, created via /schedule in the CLI; on-machine scheduled tasks belong to the desktop app. As of 2026-07-17.The third form belongs to no machine of yours. Install the GitHub Actions integration (interactively: /install-github-app), mention @claude in any PR or issue, and it analyzes, implements, fixes, and opens complete PRs on the spot; anthropics/claude-code-action@v1 can also run unsummoned, on your workflow's schedule or events.3注 3Note 3Claude Code Docs · GitHub Actions —— 在 PR 或 issue 里 @claude,它分析代码、实现功能、修 bug、开出完整 PR;动作为 anthropics/claude-code-action@v1,交互式安装跑 /install-github-app;API key 放仓库 secrets;成本两笔:GitHub runner 分钟 + API token。每个 PR 无须触发的自动审查是另一条产品线(GitHub Code Review)。截至 2026-07-17。Claude Code Docs · GitHub Actions — mention @claude in a PR or issue and it analyzes code, implements features, fixes bugs, and opens complete PRs; the action is anthropics/claude-code-action@v1, installed interactively via /install-github-app; the API key lives in repository secrets; costs are twofold: GitHub runner minutes + API tokens. Untriggered automatic review on every PR is a separate product line (GitHub Code Review). As of 2026-07-17. Keep two ledgers: in CI it burns GitHub runner minutes plus API tokens — authenticated with the API key you put in repository secrets, billed per token.3注 3Note 3Claude Code Docs · GitHub Actions —— 在 PR 或 issue 里 @claude,它分析代码、实现功能、修 bug、开出完整 PR;动作为 anthropics/claude-code-action@v1,交互式安装跑 /install-github-app;API key 放仓库 secrets;成本两笔:GitHub runner 分钟 + API token。每个 PR 无须触发的自动审查是另一条产品线(GitHub Code Review)。截至 2026-07-17。Claude Code Docs · GitHub Actions — mention @claude in a PR or issue and it analyzes code, implements features, fixes bugs, and opens complete PRs; the action is anthropics/claude-code-action@v1, installed interactively via /install-github-app; the API key lives in repository secrets; costs are twofold: GitHub runner minutes + API tokens. Untriggered automatic review on every PR is a separate product line (GitHub Code Review). As of 2026-07-17. That runs on your Console account, a different thing from the Pro/Max subscription pool — the busier the automation, the more it wants watching on its own. For an automatic baseline review on every PR with no trigger, that's a separate product line (GitHub Code Review).3注 3Note 3Claude Code Docs · GitHub Actions —— 在 PR 或 issue 里 @claude,它分析代码、实现功能、修 bug、开出完整 PR;动作为 anthropics/claude-code-action@v1,交互式安装跑 /install-github-app;API key 放仓库 secrets;成本两笔:GitHub runner 分钟 + API token。每个 PR 无须触发的自动审查是另一条产品线(GitHub Code Review)。截至 2026-07-17。Claude Code Docs · GitHub Actions — mention @claude in a PR or issue and it analyzes code, implements features, fixes bugs, and opens complete PRs; the action is anthropics/claude-code-action@v1, installed interactively via /install-github-app; the API key lives in repository secrets; costs are twofold: GitHub runner minutes + API tokens. Untriggered automatic review on every PR is a separate product line (GitHub Code Review). As of 2026-07-17. Scheduled work goes to Routines: they run on Anthropic-managed infrastructure, keep running with your computer off, and are created with /schedule right in the CLI — nightly dependency patrols and pre-standup PR digests are their shifts.5注 5Note 5Claude Code Docs · Overview —— 定时的活交给 Routines:跑在 Anthropic 托管的基础设施上,你的电脑关了也继续,可由日程、API 调用或 GitHub 事件触发,CLI 里 /schedule 创建;本机定时任务归桌面端。截至 2026-07-17。Claude Code Docs · Overview — scheduled work goes to Routines: they run on Anthropic-managed infrastructure and keep running with your computer off, triggered by schedule, API calls, or GitHub events, created via /schedule in the CLI; on-machine scheduled tasks belong to the desktop app. As of 2026-07-17.
— IV
无人时的规矩:边界先于出发Rules for When No One's Watching: Boundaries Before Departure.
交互会话里,权限弹窗是最后的安全网;无人值守时没有这张网 —— 所以边界要前置。两种写法:--allowedTools 白名单,精确到 "Bash(git diff *)" 这样的规则,名单之外一律不许1注 1Note 1Claude Code Docs · Run Claude Code programmatically —— claude -p(--print)非交互跑一次:读 stdin、可管道进出;--output-format 选 text / json / stream-json,--json-schema 按 JSON Schema 约束结构化输出(结果在 structured_output 字段);--allowedTools 按权限规则语法预批工具;--bare 跳过 hooks / skills / MCP / CLAUDE.md 的自动发现,保证脚本在每台机器行为一致,是脚本化调用的推荐模式;--no-session-persistence 不留会话;文档场景即 CI、构建脚本与管道。截至 2026-07-17。Claude Code Docs · Run Claude Code programmatically — claude -p (--print) runs once, non-interactively: reads stdin, pipes in and out; --output-format offers text / json / stream-json, --json-schema constrains structured output against a JSON Schema (result in the structured_output field); --allowedTools pre-approves tools in permission-rule syntax; --bare skips auto-discovery of hooks, skills, MCP, and CLAUDE.md so a script behaves identically on every machine — the recommended mode for scripted calls; --no-session-persistence keeps no session. The documented scenarios are CI, build scripts, and pipelines. As of 2026-07-17.;或者 --permission-mode dontAsk,把「没预批就拒绝」定为总则,专为锁死的 CI 而设。4注 4Note 4Claude Code Docs · Permission modes —— 无人场景的两档:dontAsk 自动拒绝一切未预批的调用,适合锁死的 CI;auto 由分类器把关,但在 -p 非交互模式下被连续拦截会直接中止会话 —— 没有人可以回退询问。截至 2026-07-17。Claude Code Docs · Permission modes — two settings for unattended work: dontAsk auto-denies everything not pre-approved, right for locked-down CI; auto screens via the classifier, but under -p repeated blocks abort the session — there is no human to fall back to. As of 2026-07-17.想要更多自主,auto 模式的分类器也能在无人时压阵 —— 但记住它的失败姿态:交互会话里连续拦截会退回问你,-p 里没有你,它直接中止。4注 4Note 4Claude Code Docs · Permission modes —— 无人场景的两档:dontAsk 自动拒绝一切未预批的调用,适合锁死的 CI;auto 由分类器把关,但在 -p 非交互模式下被连续拦截会直接中止会话 —— 没有人可以回退询问。截至 2026-07-17。Claude Code Docs · Permission modes — two settings for unattended work: dontAsk auto-denies everything not pre-approved, right for locked-down CI; auto screens via the classifier, but under -p repeated blocks abort the session — there is no human to fall back to. As of 2026-07-17.这是无人化的正确性质:宁可停,不越界。最后一层是第 7 章的 hooks:headless 照样触发,PreToolUse 安全闸在没有观众的地方一样否决。三层叠起来再出发 —— 活可以离开你,规矩不行。In an interactive session the permission prompt is the last net; unattended, there is no net — so the boundary moves upfront. Two ways to write it: an --allowedTools allowlist, precise down to rules like "Bash(git diff *)", everything off-list denied1注 1Note 1Claude Code Docs · Run Claude Code programmatically —— claude -p(--print)非交互跑一次:读 stdin、可管道进出;--output-format 选 text / json / stream-json,--json-schema 按 JSON Schema 约束结构化输出(结果在 structured_output 字段);--allowedTools 按权限规则语法预批工具;--bare 跳过 hooks / skills / MCP / CLAUDE.md 的自动发现,保证脚本在每台机器行为一致,是脚本化调用的推荐模式;--no-session-persistence 不留会话;文档场景即 CI、构建脚本与管道。截至 2026-07-17。Claude Code Docs · Run Claude Code programmatically — claude -p (--print) runs once, non-interactively: reads stdin, pipes in and out; --output-format offers text / json / stream-json, --json-schema constrains structured output against a JSON Schema (result in the structured_output field); --allowedTools pre-approves tools in permission-rule syntax; --bare skips auto-discovery of hooks, skills, MCP, and CLAUDE.md so a script behaves identically on every machine — the recommended mode for scripted calls; --no-session-persistence keeps no session. The documented scenarios are CI, build scripts, and pipelines. As of 2026-07-17.; or --permission-mode dontAsk, making deny-unless-pre-approved the standing order, built for locked-down CI.4注 4Note 4Claude Code Docs · Permission modes —— 无人场景的两档:dontAsk 自动拒绝一切未预批的调用,适合锁死的 CI;auto 由分类器把关,但在 -p 非交互模式下被连续拦截会直接中止会话 —— 没有人可以回退询问。截至 2026-07-17。Claude Code Docs · Permission modes — two settings for unattended work: dontAsk auto-denies everything not pre-approved, right for locked-down CI; auto screens via the classifier, but under -p repeated blocks abort the session — there is no human to fall back to. As of 2026-07-17. For more autonomy, auto mode's classifier can also hold the line unattended — but know its failure posture: interactively, repeated blocks fall back to asking you; under -p there is no you, so it aborts.4注 4Note 4Claude Code Docs · Permission modes —— 无人场景的两档:dontAsk 自动拒绝一切未预批的调用,适合锁死的 CI;auto 由分类器把关,但在 -p 非交互模式下被连续拦截会直接中止会话 —— 没有人可以回退询问。截至 2026-07-17。Claude Code Docs · Permission modes — two settings for unattended work: dontAsk auto-denies everything not pre-approved, right for locked-down CI; auto screens via the classifier, but under -p repeated blocks abort the session — there is no human to fall back to. As of 2026-07-17. Which is the correct temperament for automation: rather stop than overstep. The last layer is chapter 7's hooks: headless fires them all the same, and a PreToolUse gate vetoes just as firmly with no audience. Stack the three layers, then depart — the work may leave you; the rules may not.动手 · 把一件活送出你的视线:Hands-on · send one job out of your sight:
01
把一个重复检查做成一行脚本Make one recurring check a one-liner
挑你每周手动做的一个检查(错字、日志异常、路由清单),照第 I 节写成 claude -p 一行,挂进 package.json 或 Makefile。Pick a check you do weekly by hand (typos, log anomalies, a route inventory), write it as a section-I claude -p one-liner, and hang it in package.json or a Makefile.
02
给团队仓库装上 @claudeInstall @claude on the team repo
跑 /install-github-app,然后在一个真实 issue 里 @claude 试一件小活 —— 看它从 issue 到 PR 走完全程。Run /install-github-app, then @claude a small real issue — watch it go from issue to PR end to end.
03
设一个夜班 RoutinePut one Routine on the night shift
用 /schedule 给明早设一个巡检(昨日 CI 失败摘要、依赖告警),明天读它的班报,再决定要不要长期雇佣。Use /schedule to set tomorrow morning's patrol (yesterday's CI failures, dependency alerts), read its shift report, then decide on permanent employment.
活可以不在你眼前跑,
规矩必须先于它出发.
The work can run out of sight;
the rules must leave before it does.
Aklman Library
— 讨论Discussion
讨论Discussion.
评论区初始化中…Initializing comments…
01 / 01
没有匹配结果No matches.
换个关键词,或按 Esc 回到页面Try another keyword, or press Esc to return