Skip to content

fix: auto-configure sub-agent permissions for parallel research#44

Open
JianyuLin1999 wants to merge 2 commits intoeze-is:mainfrom
JianyuLin1999:fix/subagent-permissions
Open

fix: auto-configure sub-agent permissions for parallel research#44
JianyuLin1999 wants to merge 2 commits intoeze-is:mainfrom
JianyuLin1999:fix/subagent-permissions

Conversation

@JianyuLin1999
Copy link
Copy Markdown

@JianyuLin1999 JianyuLin1999 commented Apr 3, 2026

Problem

Claude Code 子 Agent 不继承任何级别的 permissions.allow,导致并行调研功能中子 Agent 执行 CDP curl 命令、WebSearch、WebFetch 时被权限拒绝。

相关平台 issues:

这是所有使用并行调研功能的用户都会遇到的问题。

Solution

PreToolUse hooks 是唯一对子 Agent 生效的权限机制(权限评估最高优先级)。

新增 approve-tools-hook.mjs,由 check-deps.mjs 自动安装到 ~/.claude/hooks/ 并注册到 settings.json。Hook 放行范围:

工具 规则 安全边界
Bash curl ... http://localhost:\d+/ 仅 localhost CDP Proxy
Bash node ... (check-deps|cdp-proxy|match-site) 仅 skill 脚本
WebSearch 全部放行
WebFetch r.jina.ai/* 仅 Jina 服务

其他所有工具调用不输出(走默认权限流程),不影响安全性。

设计要点:

  • 对子 Agent 生效 — hooks 在权限评估最高优先级,子 Agent 也会触发
  • 最小放行 — 仅放行 localhost CDP 和必要联网工具,不放行 Write/Edit/外部 curl
  • 幂等 — 多次运行不重复注册
  • 非阻塞 — 配置失败仅输出警告,不中断 CDP 主流程
  • 自动更新 — 插件升级时 hook 脚本自动刷新
  • 零操作 — 利用已有的 check-deps.mjs 调用链,用户无需执行额外命令

Changes

文件 改动
scripts/approve-tools-hook.mjs 新增 PreToolUse hook 脚本
scripts/check-deps.mjs ensureHooks() 替代原来的 ensurePermissions()
SKILL.md 并行调研章节更新权限说明
README.md 前置配置章节更新子 Agent 权限说明

Test plan

通过 45 个 E2E 测试:

Hook 行为(28 tests):

  • CDP Proxy 所有端点(GET/POST, localhost/127.0.0.1, 自定义端口)→ allow
  • skill 脚本(check-deps, cdp-proxy, match-site)→ allow
  • WebSearch → allow
  • WebFetch Jina → allow
  • 危险命令(rm -rf, curl 外部, git push, 管道注入)→ 不放行
  • 任意 node 脚本 → 不放行
  • WebFetch 任意域名 → 不放行
  • Write/Edit/Agent 工具 → 不放行
  • 空命令、缺失 tool_input、异常 JSON → 安全回退

Hook 安装(17 tests):

  • 全新安装(无 settings.json)→ 创建并配置
  • 已有 settings.json 有其他配置 → 只追加 hook,保留原有配置
  • 幂等性 → 第二次运行输出 hooks: ok,不重复注册
  • 已有其他 hooks → 保留不覆盖
  • hook 脚本内容变更 → 自动刷新并输出 hooks: updated

🤖 Generated with Claude Code

Claude Code sub-agents don't inherit session-level permissions from
settings.local.json (anthropics/claude-code#18950), causing parallel
research tasks to fail when sub-agents try to use Bash (CDP curl),
WebSearch, or WebFetch.

This adds an `ensurePermissions()` step to `check-deps.mjs` that
automatically writes the required permissions to `~/.claude/settings.json`
(global scope, inherited by all agents including sub-agents). The function
is idempotent, non-blocking on failure, and handles fresh installs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@JianyuLin1999
Copy link
Copy Markdown
Author

在claude code官方更优雅地解决之前我们可以使用一个脚本,确保子agents自动得到权限

The initial approach (writing to permissions.allow in settings.json) was
incorrect — sub-agents don't inherit permissions.allow at any scope level
(anthropics/claude-code#18950, #37730, #25526).

PreToolUse hooks are the only permission mechanism that works for sub-agents
(highest priority in the permission evaluation flow). This commit:

- Adds approve-tools-hook.mjs: auto-approves CDP curl (localhost only),
  skill scripts, WebSearch, and Jina WebFetch
- Replaces ensurePermissions() with ensureHooks() in check-deps.mjs:
  installs the hook to ~/.claude/hooks/ and registers it in settings.json
- Hook is idempotent, non-blocking on failure, auto-refreshes on update

Tested with 45 E2E tests covering: all CDP endpoints, dangerous command
rejection, WebSearch/WebFetch conditional approval, edge cases,
idempotency, existing config preservation, and auto-refresh.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant