Skip to content

bug: subagent spawn fails when the parent holds runtime-activated tools #637

Closed

Description

@cuipengcx90

Environment

  • pi 0.99.2
  • @tt-a1i/openpi 0.9.0 (installed from npm; extensions/shared/child-session.ts is byte-identical to main)

Symptom

Every subagent_spawn fails immediately, for every built-in agent type (explorer, implementer, reviewer, advisor):

Child tool preflight failed: requested tools "tool_search", "codemode", "web_search",
"source_check", "fetch_content", "get_search_content", "agent_browser_action",
"agent_browser_qa", and 3 more are unavailable after child extensions initialized.
Check the Agent Type tools list and child extension loading.

The built-in types declare no tools: key, so this is pure inheritance from the parent — not a narrowing allowlist anyone wrote by hand.

Root cause

inheritedChildToolAllowlist() projects the parent's live pi.getActiveTools() into the child's requested allowlist (minus CHILD_EXCLUDED_TOOL_NAMES). bindChildSessionExtensions() then verifies that list against the fresh child registry, but it only activates the hardcoded names in CHILD_SAFE_PACKAGE_TOOL_NAMES (fd, rg, git_show, git_diff, git_log). Every other requested name must already be active in the child.

That assumption does not hold on current pi, where the parent's tool surface is mutable at runtime:

  • pi-web-access with toolActivation: "auto" (the default) starts a session with only web_enable and activates web_search / source_check / fetch_content / get_search_content after the model calls it.
  • pi-agent-browser-native adds agent_browser_action / _qa / _electron / _source / _network_source only after agent_browser_tools enables them.
  • MCP servers with deferred exposure bring in tool_search; codemode exposure brings in codemode.

So the parent legitimately holds tools that a brand-new child session has never activated, and the preflight turns that into a hard spawn failure. With a few of these packages installed, subagents become completely unusable.

Why extending the name list is not a fix

CHILD_SAFE_PACKAGE_TOOL_NAMES is a hand-maintained list of five names. Every package that activates tools on demand would need another entry, and the set is open-ended — the list cannot be kept in sync with what users install.

Suggested fix

1. Activate from requested, not from a hardcoded list. requested is already the narrowing allowlist (parent surface minus CHILD_EXCLUDED_TOOL_NAMES), so activating every requested name the child has registered cannot widen the boundary:

const toActivate = requestedSet ?? new Set<string>([...CHILD_SAFE_PACKAGE_TOOL_NAMES]);
for (const name of toActivate) {
  if (available.has(name) && !active.has(name)) {
    activeNames.push(name);
    active.add(name);
  }
}

This covers any package, any pi release, and any MCP server without touching the code again.

2. Do not fail the whole spawn over an inherited tool. requested is derived from the parent's surface, so a missing name is not automatically a configuration error — it may be a runtime-registered tool the child never receives at all. Drop it and report instead; a hard failure is appropriate for an explicitly declared allowlist (an agent type's tools:), where silently dropping a name would hide the user's intent.

const missing = [...new Set(requested)].filter((name) => !active.has(name));
if (missing.length === 0) return;
console.warn(
  `[openpi] child tool surface narrowed: ${missing.length} of ${requested.length} requested tool(s) unavailable ...`,
);

Reproduction

  1. pi 0.99.2 with pi-web-access installed (default toolActivation: "auto").
  2. In a parent session, get web_search into the active set (call web_enable once, or let auto choose eager).
  3. Call subagent_spawn with agent_type: "explorer".
  4. It fails with the error above before the child's first prompt.

Patch against 0.9.0

Both changes are in one hunk set below; the patch applies cleanly to 0.9.0 (patch -p1).

--- a/extensions/shared/child-session.ts
+++ b/extensions/shared/child-session.ts
@@ -732,21 +732,25 @@
   await session.bindExtensions({ mode: "print" });
   const requested = effectiveChildToolAllowlist(requestedTools);
   let active: Set<string> | undefined;
+  let available: Set<string> | undefined;
   if (
     session.getActiveToolNames &&
     session.getAllTools &&
     session.setActiveToolsByName
   ) {
     const requestedSet = requested ? new Set(requested) : undefined;
-    const available = new Set(session.getAllTools().map(({ name }) => name));
+    available = new Set(session.getAllTools().map(({ name }) => name));
     const activeNames = session.getActiveToolNames();
     active = new Set(activeNames);
-    for (const name of CHILD_SAFE_PACKAGE_TOOL_NAMES) {
-      if (
-        available.has(name) &&
-        !active.has(name) &&
-        (requestedSet === undefined || requestedSet.has(name))
-      ) {
+    // 父级继承来的工具在子会话里往往只是"已注册但未激活":内置扩展注册的
+    // tool_search / codemode,以及包提供的 web_search、agent_browser* 等,
+    // 都需要一次显式激活才会进入子会话的活动面。requested 是父级活动面去掉
+    // parent-only 工具后的白名单,按它激活不放宽任何边界;requested 缺失时
+    // 退回 CHILD_SAFE_PACKAGE_TOOL_NAMES。
+    const toActivate =
+      requestedSet ?? new Set<string>([...CHILD_SAFE_PACKAGE_TOOL_NAMES]);
+    for (const name of toActivate) {
+      if (available.has(name) && !active.has(name)) {
         activeNames.push(name);
         active.add(name);
       }
@@ -769,8 +773,21 @@
   const missing = [...new Set(requested)].filter((name) => !active.has(name));
   if (missing.length === 0) return;
 
-  throw new Error(
-    `Child tool preflight failed: requested tool${missing.length === 1 ? "" : "s"} ${boundedToolNames(missing)} ${missing.length === 1 ? "is" : "are"} unavailable after child extensions initialized. Check the Agent Type tools list and child extension loading.`,
+  // `requested` is inherited from the parent's live tool surface, so a name the
+  // child cannot expose is not automatically an error. A parent can hold tools
+  // the child legitimately never gets: runtime-registered ones (an MCP server's
+  // tools, a package that activates tools on demand) or names no child
+  // extension registered at all. Failing the whole spawn over one of those is
+  // worse than running without it, so drop them and report once instead. The
+  // counts keep a genuine loading failure visible instead of silent.
+  const registeredCount = available?.size ?? 0;
+  const unregisteredCount = available
+    ? missing.filter((name) => !available.has(name)).length
+    : 0;
+  console.warn(
+    `[openpi] child tool surface narrowed: ${missing.length} of ${requested.length} requested tool(s) unavailable ` +
+      `(${unregisteredCount} never registered, ${missing.length - unregisteredCount} registered but inactive; ` +
+      `${active.size} active of ${registeredCount} registered). Dropped: ${boundedToolNames(missing)}`,
   );
 }
 

Activity

  1. added a commit that references this issue on Oct 2, 2026
    1337bde
  2. cuipengcx90 commented on Oct 2, 2026

    @cuipengcx90
    ContributorAuthor

    Second, independent root cause for the same symptom (2026-10-02).

    tool_search and codemode in the failure above are not runtime-activated tools
    that a fresh child merely has not activated yet. They are never registered in
    a child:

    [openpi] child tool surface narrowed: 2 of 28 requested tool(s) unavailable
    (2 never registered, 0 registered but inactive; 26 active of 26 registered).
    Dropped: "tool_search", "codemode"
    

    Pi's built-in extensions (codemode, tool-search, mcp, llama.cpp) are
    builtin: true inline extensions. Their code reaches a DefaultResourceLoader
    only through extensionFactories: the CLI's main() passes them, and an SDK
    caller must pass them itself (dist/core/resource-loader.js:245 builds the
    built-in registry from those factories, dist/core/package-manager.js:740 then
    enables builtin:<name> by default). createChildResources() passes none, so the
    child has no built-in registry at all.

    That is why extending CHILD_SAFE_PACKAGE_TOOL_NAMES cannot cover these two
    names: activating only works on a name the child has registered.

    #638 now also injects tool-search and codemode into the child loader, which
    removes both "never registered" entries (verified: a child reports all 28 tools
    and the warning is gone). The same revision keeps the preflight hard for an
    explicit allowlist, and fixes a latent replay-fingerprint failure that injecting
    a built-in extension exposes.

  3. azcine94 commented on Oct 2, 2026

    @azcine94

    补充复现:仅激活内置 codemode 即可导致默认子代理启动失败

    在 Pi 0.99.1 + OpenPI 0.9.0 上独立复现了本 Issue 中 codemode 未在子会话注册的情况。该复现不需要安装 pi-web-access、浏览器工具等额外包,也不依赖 MCP 自动激活:父会话显式激活内置 codemode 后,默认子代理就会启动失败。

    环境

    • OpenPI:0.9.0
    • Pi:0.99.1
    • 操作系统:Windows
    • Node.js:v24.18.0(复现测试使用)
    • 子代理后端:内置 pi,通过 SDK 创建独立子会话

    问题描述

    当父会话激活内置 codemode 工具后,OpenPI 默认继承父会话工具的子代理无法启动。

    该问题同时出现在 codemode.mode: "on" 和 "only" 下。

    即使不通过 codemode 脚本,而是直接调用 subagent_spawn,也会失败。 触发条件是父会话已经激活 codemode,而不是派发调用是否经过 codemode。

    仅加载 codemode 扩展、但未激活该工具时,子代理可以正常启动。

    复现步骤

    1. 在已加载 OpenPI 的 Pi 会话中,确认内置 codemode 没有被 -builtin:codemode 排除。

    2. 在现有设置中加入以下配置,保留其他设置:

      {
        "defaultTools": ["+codemode"],
        "codemode": {
          "mode": "on"
        }
      }
    3. 重启会话或执行 /reload,确认 codemode 已处于活动工具列表中。

    4. 加载子代理工具组:

      openpi_load_tools({ groups: ["delegate"] })
    5. 在后续工具调用中派发一个未指定自定义工具限制的子代理:

      subagent_spawn({
        name: "codemode-compat-check",
        prompt: "Read README.md and report its first heading."
      })
    6. 子代理在启动阶段报错:

      Child tool preflight failed: requested tool "codemode" is unavailable after child extensions initialized. Check the Agent Type tools list and child extension loading.
      

    在 delegate 已加载后,通过 codemode 调用同一派发工具,也会得到相同的错误:

    return await tools.subagent_spawn({
      name: "codemode-compat-check",
      prompt: "Read README.md and report its first heading."
    });

    预期行为

    激活父会话的 codemode 不应导致原本可用的默认子代理派发失败。无论直接调用 subagent_spawn,还是通过 codemode 调用,子代理都应能够完成初始化并执行任务。

    实际行为

    子代理首次模型请求之前的工具预检查失败,任务无法开始。

    通过 codemode 调用时,外层脚本返回:

    Script failed
    
    Script error:
    Error: Child tool preflight failed: requested tool "codemode" is unavailable after child extensions initialized. Check the Agent Type tools list and child extension loading.
    
    Tool calls made before the failure (they are not undone): subagent_spawn (error)
    

    对照测试结果

    父会话状态 调用方式 结果
    codemode 未加载 直接派发 正常启动、读取文件并回传结果
    codemode 已加载但未激活 直接派发 正常
    codemode 已激活,on 模式 直接派发 启动检查失败
    codemode 已激活,on 模式 codemode 脚本派发 相同错误
    codemode 已激活,only 模式 codemode 脚本派发 相同错误

    相关调用链

    以下行号对应本地安装的 OpenPI 0.9.0:

    • extensions/subagents/index.ts:1028:从 pi.getActiveTools() 生成子代理工具清单,父会话激活 codemode 后,该清单包含 codemode。
    • extensions/shared/child-session.ts:653:createChildResources() 创建 DefaultResourceLoader,但没有提供 codemode 扩展工厂。
    • extensions/subagents/src/backends/pi.ts:233–265:使用上述资源加载器创建 SDK 子会话,并执行 bindChildSessionExtensions()。
    • extensions/shared/child-session.ts:769–773:检查要求的工具是否出现在子会话活动工具列表中,发现缺少 codemode 后抛出错误。

    Pi 0.99.1 的 docs/sdk.md 明确区分 CLI 与 SDK 的内置扩展加载行为。这里观察到的是:子代理继承的工具清单包含 codemode,但其 SDK 子会话未注册该工具。

    验证方式与范围

    • 使用隔离的离线 SDK 测试父会话,按 CLI 的注册方式提供内置 codemode 扩展。
    • 使用真实 Pi 资源加载器、codemode QuickJS 沙箱和 OpenPI 子代理运行链,仅模型回复使用脚本模拟。
    • 对照组实际执行了子代理读文件及 subagent_wait 结果回传。
    • 未使用真实模型 API,未消耗额度,未修改正式配置或扩展源码。
    • 本次验证范围是子代理初始化,以及对照组的读取/等待回传;未将发送、取消、自动回传或完整 Web/TUI 交互作为已验证项。

    本评论仅补充复现与诊断证据,不提出解决方案。

  4. tt-a1i commented on Oct 4, 2026

    @tt-a1i
    Collaborator

    The source and regression investigation is preserved in Child tool inheritance and replay identity, linked from PR #638.

    The follow-up fixes keep explicit role tool requirements strict at both public entries, verify Pi's actual active surface after activation, and retain native/inline extension identity boundaries for replay. Local full checks and tests passed, and independent subagent review found no remaining defects at e8c43cb1. Current-head CI is being completed before merge; the record states the SDK and external-package validation limits.

  5. added a commit that references this issue on Oct 4, 2026
    94f6dfb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions