Repository navigation
fix(core): apply session permissions to subagent listing and child continuation - #64841
zsicheng0214 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
|
Good catch on custom child permissions — updated Also happy to rebase/drop the |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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 * denyand the parent hasedit * ask. The parent'saskis added last, so the child's edit becomesask. This is the loosening the comment says can't happen. - Denying what the parent allows: the parent has
bash * denyfollowed bybash git * allow. Only the deny is copied, so the child deniesgit statuswhile 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>() |
There was a problem hiding this comment.
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?
…stead of merging in SubagentTool
|
Switched to cascading inside |
There was a problem hiding this comment.
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.
Issue for this PR
Closes #64840
Type of change
What does this PR do?
Aligns
SubagentTool(packages/core/src/tool/plugin/subagent.ts) with session-level permission rules:Permission.merge(selected.permissions, session?.permissions ?? [])inSubagentTool'scontext/compaction/generatehook soAvailable subagents:reflects session overrides.parent.permissionsontoexistingviasessions.setPermissionswhen continuing an existing child session whose permissions differ from the parent's current rules.How did you verify your code works?
Ran in
packages/coreand repo root:Screenshots / recordings
N/A (core subagent permission handling only)
Checklist
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)