安全异步编程,代码审查双保险
简介
将编程任务委托给本地 Claude Code CLI,通过非交互式模式安全地执行代码编写、调试与测试,保持主代理响应性
原始描述
Delegate programming tasks to Claude Code CLI
通过你的 Agent 添加
安装目录:claude-code-delegate/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
中文使用说明 来自收录源
核心用法
Claude Code Delegate 是一种「代理委派」技能,将实际编程工作完全外包给 Anthropic 官方的 Claude Code CLI 工具。用户无需直接编写代码,而是通过结构化指令触发本地 claude -p 命令,在隔离的会话中完成开发任务。
典型工作流
- 前置检查:验证 CLI 安装 (
which claude)、API 配置 (claude --version) 及可选的 write-guard 插件
- 任务触发:自动识别代码相关请求(写代码、重构、调试、测试等),或手动输入
/code <task>
- 异步执行:使用
claude -p "<task>" --output-format text --max-turns 10 --permission-mode bypassPermissions,立即返回 session ID,不阻塞主代理
- 状态轮询:用户下一条消息时通过
process poll <id> 检查进度,主代理保持对话可用性
- 结果中继:由主代理汇总文件变更、添加 delegate 人格特征(参考 PERSONA.md)并给出自身评价
显著优点
- 隔离性与安全性:强制
cd <project_dir> 限定工作范围,推荐配合 write-guard 插件阻止对系统配置文件的写入
- 异步非阻塞:通过 session ID + 轮询机制,确保用户在长耗时编译/测试中仍能交互
- 双会话验证:独创「作者会话 (
--continue) + 独立测试会话 (fresh)」模式,利用无上下文偏见的 fresh session 充当外部代码审查员,显著提升代码质量
- 权限管控:明确禁止
--dangerously-skip-permissions,仅使用 bypassPermissions 并与 write-guard 协同
潜在缺点与局限性
- 环境依赖重:必须预装 Node.js 全局包
@anthropic-ai/claude-code 并完成登录,初次配置门槛较高
- 超时风险:默认 300 秒 (5 分钟) 可能不足以完成大型重构;复杂任务需人工拆分为子任务
- write-guard 非强制:仅作为「强烈推荐」,用户若跳过则面临完整文件系统读写权限暴露风险
- 间接调试:用户无法直接与 delegate 交互,所有反馈需经主代理转述,可能增加沟通轮次
- 平台限制:仅适用于已支持 Claude Code CLI 的环境(目前主要为 macOS/Linux)
适合人群
- 需要保持对话流畅性、不希望被长时间编译阻塞的开发者
- 追求「写代码」与「测代码」分离验证以提升可靠性的团队
- 希望利用 Claude Code 原生 IDE 级能力(如代码搜索、多文件编辑)的本地 CLI 用户
- 具备基本 Node.js 环境管理能力、能接受额外安全插件配置的技术用户
常规风险
风险类型
说明
缓解措施
文件系统误写
--permission-mode bypassPermissions 配合缺失 write-guard 时,delegate 可能修改任意文件
强制启用 write-guard 插件,限定 projects/ 隔离目录
凭证泄露
在含密钥的目录运行 delegate
禁止在仓库根目录或 ~/.aws/ 等敏感路径执行,使用干净项目目录
无限循环/资源耗尽
--max-turns 设置过高或任务描述模糊导致反复重试
保持 10 轮限制,任务描述精确化,超时后人工介入
主代理与 delegate 状态不一致
轮询间隔内用户感知延迟
明确告知用户「进行中」状态,管理预期
依赖 CLI 服务可用性
Anthropic API 或本地认证失效
前置 claude --version 检查,异常时引导用户重登录
结论
which claude)、API 配置 (claude --version) 及可选的 write-guard 插件/code <task>claude -p "<task>" --output-format text --max-turns 10 --permission-mode bypassPermissions,立即返回 session ID,不阻塞主代理process poll <id> 检查进度,主代理保持对话可用性- 隔离性与安全性:强制
cd <project_dir>限定工作范围,推荐配合 write-guard 插件阻止对系统配置文件的写入 - 异步非阻塞:通过 session ID + 轮询机制,确保用户在长耗时编译/测试中仍能交互
- 双会话验证:独创「作者会话 (
--continue) + 独立测试会话 (fresh)」模式,利用无上下文偏见的 fresh session 充当外部代码审查员,显著提升代码质量 - 权限管控:明确禁止
--dangerously-skip-permissions,仅使用bypassPermissions并与 write-guard 协同
潜在缺点与局限性
- 环境依赖重:必须预装 Node.js 全局包
@anthropic-ai/claude-code 并完成登录,初次配置门槛较高
- 超时风险:默认 300 秒 (5 分钟) 可能不足以完成大型重构;复杂任务需人工拆分为子任务
- write-guard 非强制:仅作为「强烈推荐」,用户若跳过则面临完整文件系统读写权限暴露风险
- 间接调试:用户无法直接与 delegate 交互,所有反馈需经主代理转述,可能增加沟通轮次
- 平台限制:仅适用于已支持 Claude Code CLI 的环境(目前主要为 macOS/Linux)
适合人群
- 需要保持对话流畅性、不希望被长时间编译阻塞的开发者
- 追求「写代码」与「测代码」分离验证以提升可靠性的团队
- 希望利用 Claude Code 原生 IDE 级能力(如代码搜索、多文件编辑)的本地 CLI 用户
- 具备基本 Node.js 环境管理能力、能接受额外安全插件配置的技术用户
常规风险
风险类型
说明
缓解措施
文件系统误写
--permission-mode bypassPermissions 配合缺失 write-guard 时,delegate 可能修改任意文件
强制启用 write-guard 插件,限定 projects/ 隔离目录
凭证泄露
在含密钥的目录运行 delegate
禁止在仓库根目录或 ~/.aws/ 等敏感路径执行,使用干净项目目录
无限循环/资源耗尽
--max-turns 设置过高或任务描述模糊导致反复重试
保持 10 轮限制,任务描述精确化,超时后人工介入
主代理与 delegate 状态不一致
轮询间隔内用户感知延迟
明确告知用户「进行中」状态,管理预期
依赖 CLI 服务可用性
Anthropic API 或本地认证失效
前置 claude --version 检查,异常时引导用户重登录
结论
@anthropic-ai/claude-code 并完成登录,初次配置门槛较高- 需要保持对话流畅性、不希望被长时间编译阻塞的开发者
- 追求「写代码」与「测代码」分离验证以提升可靠性的团队
- 希望利用 Claude Code 原生 IDE 级能力(如代码搜索、多文件编辑)的本地 CLI 用户
- 具备基本 Node.js 环境管理能力、能接受额外安全插件配置的技术用户
常规风险
风险类型
说明
缓解措施
文件系统误写
--permission-mode bypassPermissions 配合缺失 write-guard 时,delegate 可能修改任意文件
强制启用 write-guard 插件,限定 projects/ 隔离目录
凭证泄露
在含密钥的目录运行 delegate
禁止在仓库根目录或 ~/.aws/ 等敏感路径执行,使用干净项目目录
无限循环/资源耗尽
--max-turns 设置过高或任务描述模糊导致反复重试
保持 10 轮限制,任务描述精确化,超时后人工介入
主代理与 delegate 状态不一致
轮询间隔内用户感知延迟
明确告知用户「进行中」状态,管理预期
依赖 CLI 服务可用性
Anthropic API 或本地认证失效
前置 claude --version 检查,异常时引导用户重登录
结论
| 风险类型 | 说明 | 缓解措施 |
|---|---|---|
| 文件系统误写 | --permission-mode bypassPermissions 配合缺失 write-guard 时,delegate 可能修改任意文件 |
强制启用 write-guard 插件,限定 projects/ 隔离目录 |
| 凭证泄露 | 在含密钥的目录运行 delegate | 禁止在仓库根目录或 ~/.aws/ 等敏感路径执行,使用干净项目目录 |
| 无限循环/资源耗尽 | --max-turns 设置过高或任务描述模糊导致反复重试 |
保持 10 轮限制,任务描述精确化,超时后人工介入 |
| 主代理与 delegate 状态不一致 | 轮询间隔内用户感知延迟 | 明确告知用户「进行中」状态,管理预期 |
| 依赖 CLI 服务可用性 | Anthropic API 或本地认证失效 | 前置 claude --version 检查,异常时引导用户重登录 |
Claude Code Delegate 是一种架构先进的「人机协同」编程模式,通过严格的异步边界和双会话验证机制,在效率与可靠性之间取得平衡。其核心价值在于让专业工具做专业的事——主代理专注对话与决策,delegate 专注代码实现与验证。适合追求高代码质量、愿意接受一定环境配置成本的用户。
技能正文
name: claude-code-delegate
description: Delegate programming tasks to Claude Code CLI
user-invocable: true
Claude Code Delegate
Delegate programming tasks to local Claude Code CLI.
RULE: You NEVER write code directly. ALL programming goes through claude -p.
Prerequisites Check (Run Before First Use)
Before delegating any task, verify the environment is ready:
- Claude Code CLI installed: Run
which claude— if not found, tell user:npm install -g @anthropic-ai/claude-code - API key configured: Run
claude --version— if it errors about auth, tell user to runclaudeand complete login - Write-guard active (STRONGLY RECOMMENDED): Check if a write-guard plugin exists at
.openclaw/extensions/write-guard/. If not, warn the user:⚠️ No write-guard detected. The delegate uses
--permission-mode bypassPermissionswhich grants full filesystem read/write access. It is strongly recommended to set up a write-guard plugin before running tasks. See README.md for setup instructions.
Only proceed with delegation after items 1 and 2 pass. Item 3 is a warning — the user may choose to proceed without it, but should be informed of the risk.
When to Trigger
Auto-trigger on ANY of these:
- Write, modify, refactor, debug code
- Create project files or directories
- Run tests, lint, build
- Code review, architecture planning
- Edit any file (except memory/ and .relationship/)
Manual trigger: user sends /code <task>
Do NOT trigger: chat, emotional interaction, information lookup.
Command Template
cd "<project_dir>" && claude -p "<task_description>" --output-format text --max-turns 10 --permission-mode bypassPermissions
Parameters
cd "<project_dir>" && claude -p "<task_description>" --output-format text --max-turns 10 --permission-mode bypassPermissions
| Param | Purpose | Required |
|---|---|---|
-p |
Non-interactive mode | Yes |
cd "<dir>" && |
Set working dir (no --cwd flag exists) | Yes |
--output-format text |
Plain text output | Recommended |
--max-turns 10 |
Limit execution rounds | Recommended |
--permission-mode bypassPermissions |
Auto-accept file edits (requires write-guard, see Prerequisites) | Recommended |
--continue |
Resume previous session (for debugging/iteration) | When fixing bugs in same project |
FORBIDDEN: --dangerously-skip-permissions
Timeout
Set exec timeout to 300 (5 minutes). The delegate needs time to write code.
Async Flow (CRITICAL)
The delegate MUST NOT block you. You must remain responsive to the user at all times.
Correct Flow
Step 1: exec claude -p "..." → get session ID (e.g. "marine-sage")
Step 2: IMMEDIATELY reply to user → "On it! Working on that now."
Step 3: END your turn → do NOT use any more tools
Step 4: When user sends next message → check with: exec "process poll marine-sage --timeout 1000"
Step 5: If done → relay results. If not done → tell user "still working", continue chat
WRONG (causes blocking)
exec claude -p "..." → process poll → (BLOCKED! user waits with no response)
Rules
- After
exec claude -p, you MUST reply to user and END your turn. No more tool calls.
- NEVER use
process tool directly. Use exec "process poll <id> --timeout 1000" on next user message.
- Only check the delegate's status when the user sends a NEW message.
- You can run multiple
claude -p tasks in parallel.
Task Description Rules
- Clear objective: what to do, which file
- Provide context: function names, error messages, expected behavior
- Specify constraints: language, framework, code style
- One task per call
Debugging / Iteration
Step 1: exec claude -p "..." → get session ID (e.g. "marine-sage")
Step 2: IMMEDIATELY reply to user → "On it! Working on that now."
Step 3: END your turn → do NOT use any more tools
Step 4: When user sends next message → check with: exec "process poll marine-sage --timeout 1000"
Step 5: If done → relay results. If not done → tell user "still working", continue chat
exec claude -p "..." → process poll → (BLOCKED! user waits with no response)
Rules
- After
exec claude -p, you MUST reply to user and END your turn. No more tool calls.
- NEVER use
process tool directly. Use exec "process poll <id> --timeout 1000" on next user message.
- Only check the delegate's status when the user sends a NEW message.
- You can run multiple
claude -p tasks in parallel.
Task Description Rules
- Clear objective: what to do, which file
- Provide context: function names, error messages, expected behavior
- Specify constraints: language, framework, code style
- One task per call
Debugging / Iteration
exec claude -p, you MUST reply to user and END your turn. No more tool calls.process tool directly. Use exec "process poll <id> --timeout 1000" on next user message.claude -p tasks in parallel.- Clear objective: what to do, which file
- Provide context: function names, error messages, expected behavior
- Specify constraints: language, framework, code style
- One task per call
Debugging / Iteration
When user reports bugs in code the delegate previously wrote:
- Use
--continueflag to resume the delegate's session context - Same project directory +
--continue= the delegate remembers what it wrote - Translate user's feedback into clear bug description for the delegate
Testing / Verification (Independent Review)
After the delegate writes code, use a separate fresh session (NO --continue) to test and verify it.
Why: The author has context bias. A fresh session reads the source code independently — like an external code reviewer. This catches issues the author missed.
Flow
Step 1: claude -p "Write X in projects/X/" → Author session (can use --continue to iterate)
Step 2: claude -p "Run and test projects/X/, report bugs" → Tester session (ALWAYS fresh, NO --continue)
Rules
- Author session: writes code, can use
--continue to iterate on bugs
- Tester session: NEVER uses
--continue — must read source fresh
- Tester reports: what it ran, output, pass/fail, bugs found
- If tester finds bugs → relay to user, then send fix task to author session (with
--continue)
When to Auto-Test
- User says "test it" or "run it" after writing → use fresh session
- User says "fix the bug" after testing → use
--continue on author session
- User says "run hot_sectors.py" (existing program) → fresh session (no prior context needed)
Session Decision Rule
Scenario
--continue?
Fix/iterate on code the delegate just wrote
Yes
Test/verify code the delegate just wrote
No (fresh session)
Run existing program
No (fresh session)
New project/task
No (fresh session)
Relaying Results
Step 1: claude -p "Write X in projects/X/" → Author session (can use --continue to iterate)
Step 2: claude -p "Run and test projects/X/, report bugs" → Tester session (ALWAYS fresh, NO --continue)
- Author session: writes code, can use
--continueto iterate on bugs - Tester session: NEVER uses
--continue— must read source fresh - Tester reports: what it ran, output, pass/fail, bugs found
- If tester finds bugs → relay to user, then send fix task to author session (with
--continue)
When to Auto-Test
- User says "test it" or "run it" after writing → use fresh session
- User says "fix the bug" after testing → use
--continue on author session
- User says "run hot_sectors.py" (existing program) → fresh session (no prior context needed)
Session Decision Rule
Scenario
--continue?
Fix/iterate on code the delegate just wrote
Yes
Test/verify code the delegate just wrote
No (fresh session)
Run existing program
No (fresh session)
New project/task
No (fresh session)
Relaying Results
--continue on author session| Scenario | --continue? |
|---|---|
| Fix/iterate on code the delegate just wrote | Yes |
| Test/verify code the delegate just wrote | No (fresh session) |
| Run existing program | No (fresh session) |
| New project/task | No (fresh session) |
Relaying Results
The delegate does not talk to the user directly. You relay all results.
When relaying:
- Summarize what was done and which files changed
- Add the delegate's personality (see PERSONA.md for character template)
- Add your own reaction
Keep technical summary concise. Do not copy the delegate's full output verbatim.
Error Handling
Error
Action
command not found
Tell user to install: npm install -g @anthropic-ai/claude-code
Empty output
Ask user to clarify the task
Timeout
Suggest breaking into smaller tasks
Permission error
Ask user to check file path
API error
Ask user to run claude to check login status
Failure Rule
| Error | Action |
|---|---|
command not found |
Tell user to install: npm install -g @anthropic-ai/claude-code |
| Empty output | Ask user to clarify the task |
| Timeout | Suggest breaking into smaller tasks |
| Permission error | Ask user to check file path |
| API error | Ask user to run claude to check login status |
If the delegate fails or times out, do NOT write code yourself.
Tell the user: "The coding task didn't finish. Want me to try again?"
Retry with longer timeout or simpler task description. Only write code yourself if user explicitly says "you do it" (not recommended).
Security Best Practices
- Always use an isolated project directory — Never run the delegate against your home directory, system config, or repositories containing secrets. Use a dedicated
projects/ or workplace/ directory.
- Set up the write-guard plugin — This is the most important safety measure. See README.md for the full plugin code. The write-guard blocks writes to platform config files (
.openclaw/, LaunchAgents/, auth profiles) at the platform level.
- Never use
--dangerously-skip-permissions — This flag is explicitly forbidden. --permission-mode bypassPermissions is the correct flag and works with the write-guard.
- Restrict to project scope — The
cd "<project_dir>" && prefix ensures the delegate operates within the intended directory. Never omit it.
- Review delegate output — Always relay results through the main agent. Never let the delegate communicate directly with external services or users.
projects/ or workplace/ directory..openclaw/, LaunchAgents/, auth profiles) at the platform level.--dangerously-skip-permissions — This flag is explicitly forbidden. --permission-mode bypassPermissions is the correct flag and works with the write-guard.cd "<project_dir>" && prefix ensures the delegate operates within the intended directory. Never omit it.正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。