Skip to content

fix: block subagents from spawning subagents - #7056

Merged
alex-alecu merged 3 commits into
Kilo-Org:mainfrom
Githubguy132010:fix/block-subagents-spawning-subagents
Mar 26, 2026
Merged

alex-alecu merged 3 commits into
Kilo-Org:mainfrom
Githubguy132010:fix/block-subagents-spawning-subagents

Conversation

@Githubguy132010

@Githubguy132010 Githubguy132010 commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

Context

Fixes #7055. Subagents were able to spawn further subagents when their merged agent permissions contained an explicit task rule. This created a recursion/infinite delegation risk and violated the expected boundary that only the top-level agent should delegate.

Implementation

The root cause was a hasTaskPermission check in packages/opencode/src/tool/task.ts that conditionally skipped adding a task: deny rule to the child session's permissions — and also conditionally kept task in the tool list passed to SessionPrompt.prompt — whenever the child agent had any explicit task permission configured.

The fix removes the conditional entirely. The task permission is now always denied on sessions created by TaskTool, and task is always excluded from the tool list for subagent prompts, regardless of the child agent's configured permissions.

Root cause analysis

The hasTaskPermission conditional was originally introduced in upstream opencode via anomalyco/opencode#8111 ("fix(task): respect agent task permission for nested sub-agents"), merged Jan 13 2026. That PR restored the ability for agents to opt in to the task tool via permission frontmatter after the permission rework in anomalyco/opencode#6319 removed it.

Before that upstream PR, task was unconditionally denied for all subagent sessions:

{ permission: "task", pattern: "*", action: "deny" }
// and
tools: { task: false }

The upstream PR made it conditional:

const hasTaskPermission = agent.permission.some((rule) => rule.permission === "task")

This check was too broad — it matched any rule mentioning task (including deny or ask), not just explicit allows. It was also vulnerable to user config: since user permissions are merged into every agent's permission array via PermissionNext.merge(defaults, ..., user), a user clicking "Always allow" on a task permission prompt would persist task: "allow" to global config, which would then cause hasTaskPermission to be true for every agent — including general and explore subagents.

What this PR changed

  • First commit (c4898d78): reverted to unconditional deny — the safe baseline.
  • Second commit (4ee4d49): tightened the conditional from rule.permission === "task" to rule.permission === "task" && rule.action === "allow", renamed to allowsTask. This preserves the orchestrator agent's ability to delegate (it has explicit task: "allow") while blocking subagents that only have task rules via user config inheritance.

Remaining gaps

Despite this fix, infinite recursion is still possible in specific scenarios:

  1. User config override: If a user's global config contains task: "allow" (from a prior "Always allow" click), it gets merged into every agent's permissions, making allowsTask true for all subagents.

  2. Cross-agent mutual recursion: The self-delegation guard added later in 4f2ac44 ("block same-agent recursive delegation") only prevents agent A → A loops. It does not prevent A → B → A → B chains (e.g. generalexploregeneral → ...).

  3. No hard depth limit: The system relies entirely on permission-based gating with no maximum nesting depth counter. A proper fix would track depth via the parentID chain and refuse to create sessions beyond a threshold.

Screenshots

N/A — no UI changes.

How to Test

  1. Configure a custom agent with an explicit task: allow permission rule in your config.
  2. Invoke that agent as a subagent via the task tool.
  3. Verify the subagent cannot call the task tool (previously it could; now it cannot).

Get in Touch

thomas07374

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Comment thread packages/opencode/src/tool/task.ts Outdated
@kilo-code-bot

kilo-code-bot Bot commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: 1 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
packages/opencode/src/tool/task.ts 64 allowsTask checks for any task: allow entry in the merged ruleset instead of the effective rule, so global/user task: allow can still re-enable nested delegation for subagents.

Fix these issues in Kilo Cloud

Other Observations (not in diff)

None.

Files Reviewed (1 files)
  • packages/opencode/src/tool/task.ts - 1 issue

Reviewed by gpt-5.4-20260305 · 2,997,678 tokens

Comment thread packages/opencode/src/tool/task.ts

@alex-alecu alex-alecu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contribution.

@alex-alecu
alex-alecu merged commit 4a2d979 into Kilo-Org:main Mar 26, 2026
13 checks passed
@Githubguy132010
Githubguy132010 deleted the fix/block-subagents-spawning-subagents branch March 28, 2026 11:01
jliounis pushed a commit to jliounis/kilocode that referenced this pull request May 18, 2026
…ents-spawning-subagents

fix: block subagents from spawning subagents
t7tran pushed a commit to t7tran/kilocode that referenced this pull request Aug 14, 2026
…ents-spawning-subagents

fix: block subagents from spawning subagents
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.

[CLI] Block subagents from spawning subagents

2 participants