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:
- 1. Claude Code CLI installed: Run
which claude — if not found, tell user: INLINECODE2 - API key configured: Run
claude --version — if it errors about auth, tell user to run claude and 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 bypassPermissions which 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 INLINECODE7
Do NOT trigger: chat, emotional interaction, information lookup.
Command Template
CODEBLOCK0
Parameters
| Param | Purpose | Required |
|---|
| INLINECODE8 | Non-interactive mode | Yes |
| INLINECODE9 |
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
CODEBLOCK1
WRONG (causes blocking)
CODEBLOCK2
Rules
- 1. 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
- 1. 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
--continue flag 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
CODEBLOCK3
Rules
- 1. 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 | INLINECODE27 ? |
|---|
| 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:
- 1. 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 |
|---|
| INLINECODE28 | Tell user to install: INLINECODE29 |
| 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
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
- 1. 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.
Claude Code 委托
将编程任务委托给本地 Claude Code CLI。
规则:你绝不直接编写代码。所有编程工作都通过 claude -p 完成。
前置条件检查(首次使用前运行)
在委托任何任务之前,验证环境是否就绪:
- 1. 已安装 Claude Code CLI:运行 which claude — 如果未找到,告知用户:npm install -g @anthropic-ai/claude-code
- 已配置 API 密钥:运行 claude --version — 如果出现认证错误,告知用户运行 claude 并完成登录
- 写保护已激活(强烈推荐):检查 .openclaw/extensions/write-guard/ 路径下是否存在写保护插件。如果不存在,警告用户:
> ⚠️ 未检测到写保护。委托使用 --permission-mode bypassPermissions,该模式授予完整的文件系统读写权限。强烈建议在运行任务前设置写保护插件。设置说明请参阅 README.md。
仅在第 1 项和第 2 项通过后才进行委托。第 3 项为警告 — 用户可以选择在没有写保护的情况下继续,但应被告知风险。
触发时机
在以下任何情况下自动触发:
- - 编写、修改、重构、调试代码
- 创建项目文件或目录
- 运行测试、代码检查、构建
- 代码审查、架构规划
- 编辑任何文件(memory/ 和 .relationship/ 目录除外)
手动触发:用户发送 /code <任务>
不触发:聊天、情感互动、信息查询。
命令模板
bash
cd <项目目录> && claude -p <任务描述> --output-format text --max-turns 10 --permission-mode bypassPermissions
参数
| 参数 | 用途 | 是否必需 |
|---|
| -p | 非交互模式 | 是 |
| cd <目录> && |
设置工作目录(不存在 --cwd 标志) | 是 |
| --output-format text | 纯文本输出 | 推荐 |
| --max-turns 10 | 限制执行轮数 | 推荐 |
| --permission-mode bypassPermissions | 自动接受文件编辑(
需要写保护,见前置条件) | 推荐 |
| --continue | 恢复之前的会话(用于调试/迭代) | 修复同一项目中的错误时使用 |
禁止使用:--dangerously-skip-permissions
超时设置
将执行超时设置为 300(5 分钟)。委托需要时间来编写代码。
异步流程(关键)
委托绝不能阻塞你。 你必须始终保持对用户的响应。
正确流程
步骤 1:exec claude -p ... → 获取会话 ID(例如 marine-sage)
步骤 2:立即回复用户 → 正在处理!马上就好。
步骤 3:结束你的回合 → 不要使用任何其他工具
步骤 4:当用户发送下一条消息时 → 检查:exec process poll marine-sage --timeout 1000
步骤 5:如果完成 → 传达结果。如果未完成 → 告知用户仍在处理,继续对话
错误流程(会导致阻塞)
exec claude -p ... → process poll → (阻塞!用户等待无响应)
规则
- 1. 执行 exec claude -p 后,你必须回复用户并结束你的回合。不再调用任何工具。
- 绝不直接使用 process 工具。在用户下一条消息时使用 exec process poll --timeout 1000。
- 仅在用户发送新消息时检查委托的状态。
- 你可以并行运行多个 claude -p 任务。
任务描述规则
- 1. 明确目标:做什么,哪个文件
- 提供上下文:函数名、错误信息、预期行为
- 指定约束条件:语言、框架、代码风格
- 每次调用只处理一个任务
调试 / 迭代
当用户报告委托之前编写的代码存在错误时:
- - 使用 --continue 标志恢复委托的会话上下文
- 相同的项目目录 + --continue = 委托记得它写了什么
- 将用户的反馈转化为清晰的错误描述给委托
测试 / 验证(独立审查)
委托编写代码后,使用独立的新会话(不使用 --continue)进行测试和验证。
原因:作者存在上下文偏差。新会话独立读取源代码 — 就像外部代码审查员。这能发现作者遗漏的问题。
流程
步骤 1:claude -p 在 projects/X/ 中编写 X → 作者会话(可以使用 --continue 进行迭代)
步骤 2:claude -p 运行并测试 projects/X/,报告错误 → 测试者会话(始终使用新会话,不使用 --continue)
规则
- 1. 作者会话:编写代码,可以使用 --continue 迭代修复错误
- 测试者会话:绝不使用 --continue — 必须重新读取源代码
- 测试者报告:运行内容、输出、通过/失败、发现的错误
- 如果测试者发现错误 → 传达给用户,然后向作者会话发送修复任务(使用 --continue)
自动测试时机
- - 用户说测试一下或运行一下(在编写代码后)→ 使用新会话
- 用户说修复这个错误(在测试后)→ 在作者会话中使用 --continue
- 用户说运行 hot_sectors.py(现有程序)→ 新会话(无需先前上下文)
会话决策规则
| 场景 | 使用 --continue? |
|---|
| 修复/迭代委托刚刚编写的代码 | 是 |
| 测试/验证委托刚刚编写的代码 |
否(新会话) |
| 运行现有程序 |
否(新会话) |
| 新项目/任务 |
否(新会话) |
传达结果
委托不直接与用户对话。你传达所有结果。
传达时:
- 1. 总结已完成的工作和更改的文件
- 加入委托的个性(角色模板见 PERSONA.md)
- 加入你自己的反应
保持技术摘要简洁。不要逐字复制委托的完整输出。
错误处理
| 错误 | 操作 |
|---|
| command not found | 告知用户安装:npm install -g @anthropic-ai/claude-code |
| 空输出 |
请用户澄清任务 |
| 超时 | 建议拆分为更小的任务 |
| 权限错误 | 请用户检查文件路径 |
| API 错误 | 请用户运行 claude 检查登录状态 |
失败规则
如果委托失败或超时,不要自己编写代码。
告知用户:编程任务未完成。需要我重试吗?
使用更长的超时时间或更简单的任务描述重试。仅在用户明确说你来写时才自己编写代码(不推荐)。
安全最佳实践
- 1. 始终使用隔离的项目目录 — 绝不对你的主目录、系统配置或包含机密的仓库运行委托。使用专用的 projects/ 或 workplace/ 目录。
- 设置写保护插件 — 这是最重要的安全措施。完整插件代码请参阅 README.md。写保护在平台层面阻止对平台配置文件(.openclaw/、LaunchAgents/、认证配置文件)的写入。
- 绝不使用 --dangerously-skip-permissions — 此标志明确禁止。--permission-mode bypassPermissions 是正确的标志,并与写保护配合使用。
- 限制在项目范围内 — cd <项目目录> && 前缀确保委托在预期目录内操作。绝不省略。
- 审查委托输出 — 始终通过主代理传达结果。绝不让委托直接与外部服务或用户通信。