Skip to content

fix(core): apply session permissions to subagent listing and child continuation - #64841

Open
zsicheng0214 wants to merge 3 commits into
anomalyco:devfrom
TraceLite-AI:subagent-session-permissions
Open

zsicheng0214 wants to merge 3 commits into
anomalyco:devfrom
TraceLite-AI:subagent-session-permissions

Conversation

@zsicheng0214

@zsicheng0214 zsicheng0214 commented Nov 19, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #64840

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Aligns SubagentTool (packages/core/src/tool/plugin/subagent.ts) with session-level permission rules:

  • Merges Permission.merge(selected.permissions, session?.permissions ?? []) in SubagentTool's context / compaction / generate hook so Available subagents: reflects session overrides.
  • Syncs parent.permissions onto existing via sessions.setPermissions when continuing an existing child session whose permissions differ from the parent's current rules.

How did you verify your code works?

Ran in packages/core and repo root:

cd packages/core
bun test test/tool-subagent.test.ts test/permission.test.ts
bun typecheck
bun run typecheck:tsc
cd ../..
bun x oxlint packages/core/src/tool/plugin/subagent.ts packages/core/test/tool-subagent.test.ts

Screenshots / recordings

N/A (core subagent permission handling only)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Fixes #48106

Public API: session.update with permissions now also updates descendants still on the parent's previous rules (behaviour change, call shapes unchanged)

@opencodejr opencodejr Bot 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.

The listing fix looks right, but the change to continued child sessions needs a decision first. I ran your two new tests against the base code: both fail there and pass with this PR, and the full tool-subagent.test.ts file passes (17/17). tsc is clean.

Public API: unchanged.

Concerns:

  • Overwriting a child's own permissions on continuation (inline). This could loosen restrictions that were set on purpose; details in the inline comment.
  • Overlap with #56979. It makes the same change to the subagent list (for #48106). This PR also fixes #48106, so that part should be coordinated with #56979 so only one version lands.

Fixes #48106

if (existing !== undefined) {
const switched = existing.agent !== agent.id
const model = override ?? (switched ? agent.model : undefined)
const permissionsChanged = !samePermissions(existing.permissions, parent.permissions)

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.

This replaces the child's permissions with the parent's whenever they differ, so it assumes a child's rules always match its parent's. That isn't always true. Session.create uses input.permissions ?? parent?.permissions, and plugins can pass their own permissions when creating a child or change them later with session.update (src/plugin/host.ts). If a plugin made a child read-only, for example, continuing it here quietly brings back the parent's looser rules. Only rules that are set on the child's own session are affected; agent-level rules are still applied separately.

Could the sync only run when the child is still on the inherited rules? Or could tightened parent rules be applied on top of the child's own, instead of replacing them? Either would also be worth a test.

@zsicheng0214

Copy link
Copy Markdown
Author

Good catch on custom child permissions — updated SubagentTool so full parent.permissions replacement only runs while the child is still on the unmodified rules inherited at creation. When a child was created or updated with custom rules (e.g. a plugin-configured read-only child), its own rules are preserved and only non-allow parent rules are layered on top, with a test covering a read-only child.

Also happy to rebase/drop the Available subagents: hunk if #56979 lands first.

@opencodejr opencodejr Bot 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.

My earlier point is only partly addressed. A child whose permissions were set by a plugin is no longer overwritten, but the merging that replaces it introduces new problems. Tests pass (17/17) and tsc is clean. I also ran the new merging logic through Permission.evaluate, which is how I confirmed the problems below.

Public API: unchanged.

Concerns:

  • Merging parent rules onto a custom child (inline). It can loosen the child's own restrictions, and it can deny things the parent allows.
  • In-memory tracking (inline). Which children inherited their rules is only remembered while the process runs, so the behaviour changes after a restart. The map also never shrinks.

A simpler design may avoid both problems. Permission checks could look up the parent's current rules when they run. Or Session.setPermissions could update descendants that still have the old inherited rules. Either way, nothing would need to be remembered between calls.

return parentPermissions ?? []
}
const base = childPermissions ?? []
const tightened = (parentPermissions ?? []).filter(

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.

The last matching rule wins (findLast in Permission.evaluate). Adding the parent's non-allow rules after the child's own rules can therefore go wrong in both directions:

  • Loosening: the child has edit * deny and the parent has edit * ask. The parent's ask is added last, so the child's edit becomes ask. This is the loosening the comment says can't happen.
  • Denying what the parent allows: the parent has bash * deny followed by bash git * allow. Only the deny is copied, so the child denies git status while the parent allows it.

I confirmed both by running this logic through Permission.evaluate. Taking the parent's rules out of order loses their meaning. Applying the parent's whole ruleset alongside the child's at check time would keep the order intact.

const permission = yield* Permission.Service
const models = yield* Model.Service
const subagents = yield* SubagentJob.Service
const inheritedPermissions = new Map<SessionSchema.ID, Permission.Ruleset | undefined>()

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.

This map only lives in memory, but sessions are saved to disk. After a restart, or for children created some other way (such as plugin session.create with a parentID), no child is in the map, so every child is treated as having custom rules. An inherited child then only receives the parent's non-allow rules, never the full update, so it behaves differently than before the restart. Entries are also never removed. Can whether a child still uses inherited rules be worked out from data that is saved, or can this be avoided altogether?

@zsicheng0214

Copy link
Copy Markdown
Author

Switched to cascading inside Session.setPermissions: descendants whose stored permissions still match the parent's previous ruleset receive the new ruleset intact (preserving rule order across restarts with no in-memory map), while children with custom permissions are left untouched.

@opencodejr opencodejr Bot 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.

All three earlier points are fixed:

  • A child with its own permissions is no longer overwritten.
  • There's no more out-of-order merging: an inherited child gets the parent's full ruleset in order.
  • The in-memory map is gone: children are found in the saved sessions, so this works after a restart.

I checked it with test/tool-subagent.test.ts (17/17) and nearby session and permission tests (178/178). tsc is clean.

Public API: session.update with permissions (plugin API and HTTP) now also rewrites every descendant whose rules still equal the parent's previous rules. This happens right away, not only when a child is continued. The call shapes don't change, so this is backwards compatible unless a caller relied on children keeping stale inherited rules.

No further concerns.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant