Claude Code Operation
五件套、会话命令与三种运行方式
本章只解决一个考场动作:看到一个需求,立刻把它放回正确的 Claude Code 组件、目录或运行 flag。TS 3.1 点名 Rules、Skills、Commands、Agents、Agent Memory,以及 session management、slash commands、headless、streaming、auto mode、repository initialization、CLAUDE.md 与 settings.json;权重虽薄,名词密度却是全书最高。
一、五件套:先看载体,再看何时进入运行
guide 把五个名词并列,并不表示它们是五种同形配置。Rules 与 Skills 都是指令,却分别服务于「按主题/路径持续生效」和「按任务调用」;Commands 是会话入口;Agents 隔离执行上下文;Agent Memory 才让某个 subagent 跨 conversation 积累自己的知识。
| 组件 | 它是什么 | project 载体 | 进入运行的时机 |
|---|---|---|---|
| Rules | 按主题拆开的 project instructions,可用 paths 限定文件范围 | .claude/rules/**/*.md | 无 paths 的启动加载;带 paths 的在 Claude 处理匹配文件时适用 |
| Skills | 可复用的指令包;Claude 可按相关性调用,用户也可键入 /skill-name | .claude/skills/<name>/SKILL.md | 默认先加载 description,使用时才加载正文 |
| Commands | 会话内的 /name 入口;built-in 多为 CLI 固定逻辑,custom command 已并入 Skills 机制 | 新流程优先用 Skill;旧式单文件仍可放 .claude/commands/<name>.md | 用户键入对应 slash command 时 |
| Agents | 自带 system prompt、可限制工具的 custom subagent;定义是 YAML frontmatter 加 Markdown 正文 | .claude/agents/<name>.md | Claude 委派或用户显式调用时,在自己的 context window 中运行 |
| Agent Memory | 由 subagent frontmatter 的 memory 开启的独立持久目录 | .claude/agent-memory/<agent>/(project scope) | 启动该 subagent 时,把其 MEMORY.md 前 200 行或 25KB 中先到的部分放入 system prompt |
文件形态要成组记。Skill 必须有 SKILL.md,内容由 YAML frontmatter 与 Markdown instructions 组成,directory name(或你在 frontmatter 里设了的 name)是 slash command 名,description 帮 Claude 判断是否自动加载。Agent 也是 frontmatter 加 Markdown,但正文是 subagent 的 system prompt,只有 name 与 description 必填。旧式 command 文件支持 Skill 的 frontmatter,但 name 与 paths 在 command file 里会被忽略;它按文件名调用。
Agent Memory 不是主会话 auto memory 的别名。普通 subagent 不自动加载主 conversation 的 auto memory;要让它跨 conversation 记住东西,应在自己的 definition 里设置 memory: user、project 或 local。三者依次落在 ~/.claude/agent-memory/<agent>/、.claude/agent-memory/<agent>/ 与 .claude/agent-memory-local/<agent>/;其中 project 可进版本控制,local 用于不应提交的项目内知识。
真伪速查 1 · 五件套的真实落点
.claude/rules/**/*.md ✔ Rules · project,递归发现 Markdown
.claude/skills/<name>/SKILL.md ✔ Skills · project
~/.claude/skills/<name>/SKILL.md ✔ Skills · personal
.claude/commands/<name>.md ✔ legacy custom command · 仍受支持
.claude/agents/<name>.md ✔ Agent · project
~/.claude/agents/<name>.md ✔ Agent · personal
.claude/agent-memory/<agent>/ ✔ Agent Memory · project scope
.claude/agent-memory-local/<agent>/ ✔ Agent Memory · local scope
settings.json · "rulesDirectory" ✘ 不存在 · 自制干扰项
settings.json · "commandsDirectory" ✘ 不存在 · 自制干扰项
settings.json · "agentMemoryPath" ✘ 不存在 · 自制干扰项
二、会话与 slash commands:继续、切换、清空、初始化
Claude Code session 是与 project directory 绑定、工作时持续本地保存的 conversation。恢复入口要按动作区分:claude --continue 直接续上当前目录最近的 interactive session;claude --resume 打开 picker;claude --resume <name> 按名字恢复;运行中的 /resume 则切到另一个 session。
/clear 是「开始一个新 session」,不是删除历史 transcript。要回到旧工作用 resume;要在同一长会话里减少 context,则是 /compact 的职责,见 ch11。把「换 conversation」与「压缩 conversation」分开,是会话管理题的第一刀。
slash commands 也有两种执行形态。多数 built-in command 的行为写在 CLI 里;bundled Skill 与你自建的 Skill 则是交给 Claude 的 prompt。对 custom slash command,当前官方推荐用 .claude/skills/<name>/SKILL.md;.claude/commands/deploy.md 与 Skill 目录都能生成 /deploy,旧文件继续可用,但 Skill 还能打包 supporting files,并控制是用户调用还是 Claude 自动调用。
repository initialization 的准确动作只有一个:/init 分析 codebase,生成一份包含它发现的 build commands、test instructions 与 project conventions 的 starter CLAUDE.md;若 CLAUDE.md 已存在,它提出改进建议而不是覆盖。因此它不是 package manager initializer,也不是创建 settings.json、Rules、Agents 的万能脚手架。
真伪速查 2 · 会话与初始化入口
claude --continue ✔ 续上当前目录最近的 interactive session
claude --resume ✔ 打开 session picker
claude --resume <name> ✔ 按名字恢复 session
/resume ✔ 在 active session 内切换到另一个 session
/clear ✔ 开始新 session
/init ✔ 生成或改进 starter CLAUDE.md
claude --resume-latest ✘ 不存在 · 自制干扰项
claude --new-session ✘ 不存在 · 自制干扰项
三、headless、streaming、auto mode:三条轴不要串
headless 是交互形态,streaming 是输出形态,auto mode 是权限形态。三者可以组合,却互不替代。把它们压成一条「自动运行模式」是本章最危险的记忆法。
Headless:给任意 claude 命令加 -p(或 --print)即可 non-interactive 运行,官方把它定位为 scripts / CI/CD 的 CLI 入口。默认输出是 plain text;要一次拿结构化结果用 --output-format json,其中含 result、session ID 与 metadata。
Streaming:--output-format stream-json 表示 newline-delimited JSON 的实时流;若 consumer 要收到逐 token 的 partial messages,完整组合是 --verbose --include-partial-messages。最后一行仍是 result message,带 final response、cost 与 session metadata。这不是 Messages API 的 SSE 格式;前者是 Claude Code CLI 的 NDJSON 输出,后者见 ch02。
Auto mode:准确入口是 --permission-mode auto。独立 classifier 在动作运行前审查,阻止超出用户请求、指向未识别基础设施或疑似由 hostile content 驱动的动作;显式 ask rule 仍会强制 prompt,而且官方明确警告 auto mode 不保证安全。更关键的分层是:classifier 在 permissions system 之后运行;绝不能发生的动作应由 managed settings 的 permissions.deny 先行阻断,classifier 与用户 intent 都不能覆盖。这正是 P3「拦得住的才叫控制」在 auto mode 上的落点。
真伪速查 3 · 运行 flags 与 settings 键
claude -p "…" ✔ non-interactive / headless
claude --print "…" ✔ -p 的长写法
--output-format json ✔ 单次结构化 JSON
--output-format stream-json --verbose \
--include-partial-messages ✔ 逐 token 的 NDJSON 事件流
--permission-mode auto ✔ classifier 审查动作
claude --headless "…" ✘ 不存在 · 自制干扰项
claude --stream "…" ✘ 不存在 · 自制干扰项
claude --stream-tokens "…" ✘ 不存在 · 自制干扰项
settings.json · "model" ✔ 会话起始模型
settings.json · "env" ✔ 会话及子进程环境变量
settings.json · "permissions" ✔ allow / ask / deny 与起始 permission mode
settings.json · "hooks" ✔ 生命周期 hook
settings.json · "enabledPlugins" ✔ 按 scope 开关 plugin
settings.json · "headless" ✘ 不存在 · 自制干扰项
settings.json · "streaming" ✘ 不存在 · 自制干扰项
settings.json · "sessionStore" ✘ 不存在 · 自制干扰项
最后把 ch04 的两条边界带回运行现场。CLAUDE.md 的 user / project / local 路径分别是 ~/.claude/CLAUDE.md、./CLAUDE.md 或 ./.claude/CLAUDE.md、./CLAUDE.local.md;发现到的文件会按从宽到窄拼接进 context,不是互相覆盖。settings 则由 user、shared project、project local、managed 等来源组成,标量冲突取高优先级来源,list 型键会跨层合并。目录与优先级的完整表见 ch04;本章只要求你把运行入口放对。
/init;settings.json)。相邻考点:配置文件体系与优先级见 ch04;context compaction 与 subagent 隔离见 ch11;agent customization 的载体取舍见 ch13。本章题组
.claude/rules/api.md with paths: ["services/api/**/*.ts"]. In a fresh session launched from the repository root, the team asks only about a README and notices that the API conventions are absent from context. They want those conventions to remain conditional rather than consume every session. What should they do?paths 的 Rules 本来就是条件式指令:只有 Claude 正在处理匹配 pattern 的文件时才适用;无 paths 的 Rules 才与 .claude/CLAUDE.md 同级在 launch 时加载。README 场景没有触发 API rule,恰好证明配置按预期工作。/name 入口,不是 path-scoped project instruction。旧 command 文件虽继续可用,却按文件名被调用;paths 在 command frontmatter 里还会被忽略。rulesDirectory 是本书自制的配置键。真实机制已经约定项目 Rules 放在 .claude/rules/ 并递归发现 Markdown,不需要另设目录键。.claude/commands/deploy.md that creates /deploy. The workflow now needs a checked-in checklist, a template, and automatic invocation when a release task is clearly relevant. What is the best next change?/deploy,旧 command 继续可用;但 Skill 的目录能捆绑 supporting files,并支持控制 Claude 是否按相关性调用。project Skill 的准确入口是 .claude/skills/<name>/SKILL.md。name 与 paths 在 command file 里被忽略。真实载体配上无效字段,仍然不会得到题干要的行为。builtIn 是本书自制的 settings 键。built-in command 的行为写在 CLI 逻辑里,不能靠一个项目配置把 Markdown prompt「晋升」成产品内置逻辑。.claude/agents/code-reviewer.md should remember repository-specific conventions across future conversations. The knowledge must be shared with teammates through version control, must not leak into unrelated repositories, and must remain separate from the main session's auto memory. Which configuration fits?memory: project 的真实目录就是 .claude/agent-memory/<agent>/,用途是 project-specific 且可经 version control 分享的 subagent knowledge;它与主 conversation 的 auto memory 分开。memory: user 的确持久,但它落在 ~/.claude/agent-memory/<agent>/,设计用途就是跨所有项目保留 learnings。题干明说不能越过 repository boundary,scope 选反了。memory 字段。把内容写去主 memory,住错载体。agentMemoryPath 是本书自制的 settings 键。真实目录由 agent 的 memory scope 与 name 决定,不靠全局路径键;而且题干要的是隔离,不是所有 agents 共用一个 store。checkout-refactor. They are currently back at the shell. Which invocation selects the intended conversation directly?claude --resume <name> 直接恢复 named session;题干已经给出精确名字,不需要 picker。--continue 是真实 flag,但其职责是恢复当前目录最近的 interactive session,不是解析一个 name。这里最近的是 documentation task,正会续错对象。--resume 确实存在,但它打开 session picker,不会因为某个 session 有名字就自动选中。这是把真实入口的交互行为说错。--resume-latest 是本书自制的 flag。官方已经用 --continue 表达「最近」,用 --resume <name> 表达「指定」,无需拼出第三种写法。CLAUDE.md. After a build-system migration, the team wants Claude Code to inspect the codebase and suggest missing build and test instructions without erasing the reviewed policy. What should they do first?/init 会分析 codebase 并生成 starter CLAUDE.md;已有文件时则提出 improvements,rather than overwriting it。题干正好需要后半条。/init --force 是本书自制的 flag,所描述的 section-aware merge 行为也没有官方支撑。真实的 /init 无需这个 flag 就不会覆盖已有 CLAUDE.md。/clear 的职责是开始新 session,不是 repository initialization,也没有生成或改写 CLAUDE.md 的职责。它作用在 conversation 轴,不在文件初始化轴。-p(或 --print)进入 non-interactive mode;--output-format json 返回带 result、session ID 与 metadata 的结构化 JSON,正好满足单次调用与单对象消费。-p 与 --output-format。--headless 是本书自制的 flag。官方的准确入口是 -p 或 --print。--output-format stream-json 加 --verbose 与 --include-partial-messages;每行一个 JSON event,最后一行是带 final response、cost 与 session metadata 的 result。--output-format json 是真实格式,但它返回的是单次结构化结果,不是会被就地更新的可轮询对象;实时 NDJSON 对应的是 stream-json。stream-json 选对了输出容器,后半句却删掉了逐 token 所需的两个 flags。官方原句把 --verbose 与 --include-partial-messages 明确放进同一调用形状。--stream-tokens 是本书自制的 flag。streaming 在 Claude Code CLI 里由 --output-format stream-json 选择,不是另开一个简写开关。permissions.deny rule. Which design preserves both requirements?--permission-mode auto 让 classifier 代替 routine prompts 审查大多数动作;managed permissions.deny 在 classifier 之前阻断,且不能被覆盖。一条管减少提示,一条管硬边界。--auto-mode 是本书自制的 flag,真实入口是 --permission-mode auto;而且 classifier 是 permissions 之后的 second gate,不会替代 permissions layer。permissions.deny 在 classifier 被咨询前就阻断,classifier 与 user intent 都不能 override。