用途
Claude Code (Clawdbot) 技能用于可靠地使用本地已安装的 Claude Code CLI。它面向的是驱动 Claude Code CLI,而不是直接调用 Claude API。适用任务包括代码库分析、重构、修复测试等编码相关工作,并可作为 Unix 工具(无头、管道、结构化输出)用于 CI 与自动化。
主要能力
:支持两种执行风格:
- 无头模式(非交互):适合普通提示与结构化输出。
- 交互模式(tmux):用于
/speckit.*(Spec Kit)等斜杠命令;这些命令通过无头-p运行可能挂起或被终止。
可通过包装脚本 scripts/claude_code_run.py 传递参数,包括 -p 提示、--permission-mode、--allowedTools、--output-format json、--append-system-prompt。当提示中包含以 / 开头的行(斜杠命令)时,包装器默认使用 auto → 交互模式。
使用方式
快速检查:
验证安装:
claude --version
运行最小无头提示(输出单条响应):
./scripts/claude_code_run.py -p "Return only the single word OK."
在仓库中运行无头提示:
cd /path/to/repo
/home/ubuntu/clawd/skills/claude-code-clawdbot/scripts/claude_code_run.py \
-p "Summarize this project and point me to the key modules." \
--permission-mode plan
允许工具(自动批准):
./scripts/claude_code_run.py \
-p "Run the test suite and fix any failures." \
--allowedTools "Bash,Read,Edit"
获取结构化输出:
./scripts/claude_code_run.py \
-p "Summarize this repo in 5 bullets." \
--output-format json
添加额外系统指令:
./scripts/claude_code_run.py \
-p "Review the staged diff for security issues." \
--append-system-prompt "You are a security engineer. Be strict." \
--allowedTools "Bash(git diff *),Bash(git status *),Read"
交互模式(tmux)示例:
./scripts/claude_code_run.py \
--mode auto \
--permission-mode acceptEdits \
--allowedTools "Bash,Read,Edit,Write" \
-p $'/speckit.constitution ...\n/speckit.specify ...\n/speckit.plan ...\n/speckit.tasks\n/speckit.implement'
它会打印 tmux attach/capture 命令以便监控进度。
Spec Kit 端到端工作流(防止挂起的建议):不要对整个流程使用无头 -p,而应使用交互式 tmux 模式,因为 Spec Kit 会运行多个步骤(Bash + 文件写入 + git)并可能暂停等待确认;无头运行可能看似空闲并被监督进程 SIGKILL。前提条件(重要):每个仓库初始化一次 Spec Kit:specify init . --ai claude;确保文件夹是真实 git 仓库:git init、git add -A、git commit -m "chore: init";建议设置 origin remote(可以是本地裸仓库)以避免 git fetch --all --prune 异常:git init --bare ../origin.git、git remote add origin ../origin.git、git push -u origin main || git push -u origin master;为 Claude Code 授予足够工具权限:Spec 创建/任务/实现需要文件写入,因此包含 Write;实现通常需要 Bash;推荐 --permission-mode acceptEdits --allowedTools "Bash,Read,Edit,Write"。
运行完整 Spec Kit 流水线:
./scripts/claude_code_run.py \
--mode interactive \
--tmux-session cc-speckit \
--permission-mode acceptEdits \
--allowedTools "Bash,Read,Edit,Write" \
-p $'/speckit.constitution Create project principles for quality, accessibility, and security.\n/speckit.specify <your feature description>\n/speckit.plan I am building with <your stack/constraints>\n/speckit.tasks\n/speckit.implement'
监控/交互:包装器打印如 tmux ... attach -t <session> 用于实时观察,tmux ... capture-pane ... 用于快照输出。如果 Claude Code 在运行中途提问(如 “Proceed?”),可 attach 并回答。
OpenSpec 工作流(opsx):OpenSpec 是另一种由斜杠命令(如 /opsx:*)驱动的规范驱动工作流,同样建议对 /opsx:* 命令使用交互式 tmux 模式(避免对整个流程使用无头 -p)。每台机器安装 CLI:npm install -g @fission-ai/openspec@latest。每个项目初始化:openspec init --tools claude(必需);可选 export OPENSPEC_TELEMETRY=0 关闭遥测。推荐端到端命令序列(在交互式 Claude Code 内):/opsx:onboard、/opsx:new <change-name>、/opsx:ff(fast-forward:生成 proposal/design/specs/tasks)、/opsx:apply(实现任务)、/opsx:archive(可选:归档完成的变更)。如果 UI 提示项目类型/技术栈,请明确回答(如 “Web app (HTML/JS) with localStorage”)。
注意事项与限制
- Claude Code 有时期望 TTY。无头模式:该包装器使用
script(1)强制伪终端。 - 斜杠命令(如
/speckit.*)最好在交互模式运行;该包装器可在 tmux 中启动交互式 Claude Code 会话。 - 需要只读规划时使用
--permission-mode plan。 --allowedTools保持窄范围(最小权限原则),尤其在自动化中。- 在纠正 Claude Code 的错误后,应指示其运行:“Update your CLAUDE.md so you don't make that mistake again.” 以便记录经验教训、避免重复错误。
- Vite + ngrok:通过 ngrok 暴露 Vite 开发服务器时,Vite 会阻止未知 Host 头,除非配置。Vite 7 期望
server.allowedHosts为true或字符串数组。允许全部主机(快速):server: { host: true, allowedHosts: true };仅允许自己的 ngrok 主机(更安全):server: { host: true, allowedHosts: ['xxxx.ngrok-free.app'] };不要设置allowedHosts: 'all'(在 Vite 7 中无效)。修改vite.config.*后需重启开发服务器。 - 通过 shell 命令驱动 tmux(如
tmux send-keys ...)时,避免传入未转义的反引号和 shell 替换,它们可能在文本到达 Claude Code 前被 shell 解释。实用规则:优先从文件发送提示,或确保包装器/脚本安全引用提示文本。 - 长时间运行的开发服务器应运行在持久会话中。在自动化环境中,后台化的 vite / ngrok 进程可能被 SIGKILL;优先在受管后台会话(Clawdbot exec background)或 tmux 中运行,并在完成后显式停止。
- 官方文档中的高杠杆技巧(资料另列):始终给 Claude 提供验证方式(测试/构建/截图);探索 → 计划 → 实现(使用 Plan Mode);积极管理上下文(
/clear、/compact);积极回退(/rewind检查点,可恢复仅对话/仅代码/两者);用简短的 CLAUDE.md 承载持久规则;权限 deny > ask > allow(作用域重要,deny可屏蔽.env、secrets/**等秘密);Bash 环境变量不持久,需持久化时使用CLAUDE_ENV_FILE;钩子(hooks)优于“请记住”指令;对重型调查/独立审查使用子代理;将 Claude 当作 Unix 工具(无头、管道、结构化输出),如cat build-error.txt | claude -p "Explain root cause"、claude -p "List endpoints" --output-format json。
捆绑脚本
scripts/claude_code_run.py:运行本地 claude 二进制并带伪终端的包装器,转发各项参数。