Skip to content

core(subagent): Available subagents ignores session permissions and continued children keep stale parent permissions #64840

Open

Description

@zsicheng0214

Description

Session.create (packages/core/src/session.ts) copies parent?.permissions onto child sessions so host policies apply across the session family, and Permission.Service (packages/core/src/permission.ts) evaluates Permission.merge(agent.permissions, session.permissions ?? []).

In packages/core/src/tool/plugin/subagent.ts, two places drift from session.permissions:

  1. The context / compaction / generate hook (subagent.ts:256) filters Available subagents: using Permission.evaluate(name, agent.id, selected.permissions) and ignores session.permissions. Subagents denied on the session are still advertised to the model, while subagents explicitly allowed on the session remain hidden if the agent default denies them.
  2. Continuing an existing child (existing !== undefined, subagent.ts:163-174) syncs switchAgent and switchModel, but never updates existing.permissions when parent.permissions changed via Session.setPermissions, leaving resumed subagent sessions running under stale permission rules.

Plugins

None

OpenCode version

2.3.0 (dev)

Steps to reproduce

  1. Create a parent session, call sessions.setPermissions to deny subagent for reviewer (or allow a subagent denied by agent defaults), and trigger the session context hook: Available subagents: ignores session.permissions.
  2. Start a child session via subagent, tighten parent rules with sessions.setPermissions (e.g. deny bash), then continue the same child.id via subagent: child.permissions retains the old rules instead of the updated parent rules.

Operating System

Linux (Debian 12)

Terminal

Bash

Activity

  1. added
    area/core
    type/bug
    version/v2
    on Nov 19, 2026
  2. opencodejr commented on Nov 19, 2026

    @opencodejr
    Contributor

    Seen with a simulated model or a mock server, not a real provider; a maintainer should confirm.

    Thanks for the precise report. I reproduced both parts on 2.3.0, and both still happen on the latest dev (d33b578), run from source. A simulated model only triggered the subagent tool calls; the session rules were set and read back through the SDK.

    1. Available subagents: ignores session permissions. With the agent allowing subagent:reviewer, I set a session rule denying it. The subagent tool description in the next model request still listed explore, general, reviewer. #48106 already tracks this part, and PR #56979 targets it.

    2. A continued child keeps stale permissions. I spawned a reviewer child while the parent allowed shell, so the child copied [{shell,*,allow}]. I then changed the parent to deny shell and continued the same child via subagent with its sessionID. The child still had [{shell,*,allow}].

    The script below reproduces both. Related: your PR #64841, and #48106 for part 1.

    Reproduction script (drive.ts)
    // Repro for anomalyco/opencode#64840
    // Part 1: the `subagent` tool's "Available subagents:" list ignores session permissions.
    // Part 2: continuing an existing child via `subagent` (sessionID) does not refresh the child's
    //         permissions after the parent's permissions were changed.
    // Run: OPENCODE_DRIVE_MEDIA_DIR=/workspace/media opencode-drive run drive.ts
    import { Effect, Stream } from "effect"
    import { Llm, OpenCodeDriver } from "opencode-drive"
    
    const listed = (body: any): string => {
      const tools: any[] = body?.tools ?? []
      const tool = tools.find((t) => (t.function?.name ?? t.name) === "subagent")
      if (!tool) return "(no subagent tool)"
      const desc: string = tool?.function?.description ?? tool?.description ?? ""
      const idx = desc.indexOf("Available subagents:")
      if (idx < 0) return "(no 'Available subagents' list)"
      return desc.slice(idx).split("\n").slice(1).map((l) => l.split(":")[0].replace("- ", "")).join(", ")
    }
    
    const lastUserText = (body: any): string => {
      const msgs: any[] = body?.messages ?? body?.input ?? []
      const text = JSON.stringify(msgs)
      return text
    }
    
    export default OpenCodeDriver.use(
      {
        ...(process.env.OC_DEV ? { opencode: { dev: process.env.OC_DEV } } : {}),
        tui: { recording: true },
        config: {
          agents: {
            reviewer: { mode: "subagent", description: "Reviews code" },
            build: { permissions: [{ action: "subagent", resource: "reviewer", effect: "allow" }] },
          },
        } as any,
      },
      ({ ui, llm, opencode }) =>
        Effect.gen(function* () {
          const parentBodies: any[] = []
          let childID: string | undefined
          yield* llm.serve((request) => {
            const body: any = request.body
            if (!Array.isArray(body?.tools)) return Stream.make(Llm.text("Title"))
            const all = lastUserText(body)
            if (all.includes("You are a subagent spawned")) return Stream.make(Llm.text("child done"))
            parentBodies.push(body)
            console.log("REQ last msg:", JSON.stringify((body.messages ?? []).at(-1)).slice(0, 300))
            const msgs: any[] = body.messages ?? []
            const last = msgs[msgs.length - 1]
            const lastStr = JSON.stringify(last ?? "")
            if (last?.role === "tool" || lastStr.includes("tool_result") || lastStr.includes("function_call_output"))
              return Stream.make(Llm.text("parent ack"))
            if (lastStr.includes("STEP_SPAWN"))
              return Stream.make(
                Llm.toolCall({ index: 0, id: "call_spawn", name: "subagent", input: { agent: "reviewer", description: "review", prompt: "review it" } }),
                Llm.finish("tool-calls"),
              )
            if (lastStr.includes("STEP_CONTINUE"))
              return Stream.make(
                Llm.toolCall({ index: 0, id: "call_cont", name: "subagent", input: { agent: "reviewer", description: "review again", prompt: "again", sessionID: childID ?? "" } }),
                Llm.finish("tool-calls"),
              )
            const m = lastStr.match(/STEP_(\w+)/)
            return Stream.make(Llm.text(`ok ${m ? m[1] : "?"}`))
          })
    
          yield* Effect.sleep(process.env.OC_DEV ? 30000 : 5000)
          yield* ui.submit("STEP_A")
          yield* ui.waitFor("ok A", { timeout: 60000 })
          const sessions: any = yield* opencode.session.list({} as any)
          const items: any[] = sessions.data ?? sessions.items ?? sessions
          const parentID = items[0].id
          console.log("PART1 baseline (agent allows reviewer, no session rules):", listed(parentBodies.at(-1)))
    
          yield* opencode.session.update({ sessionID: parentID, permissions: [{ action: "subagent", resource: "reviewer", effect: "deny" }] } as any)
          const p1: any = yield* opencode.session.get({ sessionID: parentID } as any)
          console.log("PART1 parent.permissions:", JSON.stringify(p1.permissions))
          yield* ui.submit("STEP_B")
          yield* ui.waitFor("ok B", { timeout: 60000 })
          console.log("PART1 after session deny subagent:reviewer -> advertised:", listed(parentBodies.at(-1)))
    
          // Part 2: reset parent rules to allow, spawn a child
          yield* opencode.session.update({ sessionID: parentID, permissions: [{ action: "shell", resource: "*", effect: "allow" }] } as any)
          yield* ui.submit("STEP_SPAWN")
          yield* Effect.sleep(10000)
          const all: any = yield* opencode.session.list({} as any)
          const list: any[] = all.data ?? all.items ?? all
          const child = list.find((s: any) => s.parentID === parentID)
          childID = child?.id
          const c0: any = yield* opencode.session.get({ sessionID: childID ?? "" } as any)
          console.log("PART2 child created:", childID, "child.permissions:", JSON.stringify(c0.permissions))
    
          yield* opencode.session.update({ sessionID: parentID, permissions: [{ action: "shell", resource: "*", effect: "deny" }] } as any)
          const p2: any = yield* opencode.session.get({ sessionID: parentID } as any)
          console.log("PART2 parent.permissions after tightening:", JSON.stringify(p2.permissions))
          yield* ui.submit("STEP_CONTINUE")
          yield* Effect.sleep(8000)
          const c1: any = yield* opencode.session.get({ sessionID: childID ?? "" } as any)
          console.log("PART2 child.permissions after continuing via subagent:", JSON.stringify(c1.permissions))
          yield* ui.screenshot("final")
        }),
    )

    Run it with opencode-drive: opencode-drive run ./drive.ts. It drives opencode from your PATH; set OC_DEV to the path of an OpenCode checkout to run that from source instead.

  3. added
    status/repro-simulated
    on Nov 19, 2026
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

    area/coreplatform/linuxstatus/repro-simulatedtype/bugversion/v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions