Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions plugins/subagent-opencode/approval-policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,20 @@ export function resolveSessionPolicy(ctx, request) {
/**
* Project the session policy onto the ACP permission policy.
*
* `never` refuses every request; any other session keeps the configured
* policy. The projection is intentionally one-way: a session policy can only
* make the child stricter, never more permissive than the profile allowed.
* `never` refuses every request, whatever the configured policy (one-way
* stricter). `session` delegates to the session's own approval policy: an
* `ask` session lets the child proceed (`allow`) because a subagent child
* cannot itself ask a human, so an interactive session is treated as
* consenting; a `never` session refuses (`reject`). Any other configured
* value pins the answer for every session.
* @param {string} configured - the row's `permission` value.
* @param {string} sessionPolicy - resolved session approval policy.
* @returns {string} the ACP permission policy to apply.
*/
export function projectPolicy(configured, sessionPolicy) {
return sessionPolicy === 'never' ? 'reject' : configured
if (sessionPolicy === 'never') return 'reject'
if (configured === 'session') return 'allow'
return configured
}

/**
Expand Down
11 changes: 11 additions & 0 deletions plugins/subagent-opencode/test/approval-policy.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ test('a never session can only make the child stricter, never more permissive',
assert.equal(projectPolicy('reject', 'ask'), 'reject')
})

test('permission session resolves an ask session to allow and never to reject', () => {
// An ask session lets the out-of-process child proceed (it cannot ask a
// human itself); a never session refuses regardless of the configured policy.
assert.equal(projectPolicy('session', 'ask'), 'allow')
assert.equal(projectPolicy('session', 'never'), 'reject')
assert.equal(projectPolicy('reject', 'ask'), 'reject')
assert.equal(projectPolicy('reject', 'never'), 'reject')
assert.equal(projectPolicy('allow', 'ask'), 'allow')
assert.equal(projectPolicy('allow', 'never'), 'reject')
})

test('the diagnostic line names the policy and its source, and nothing else', () => {
const line = describePolicy('reject', { policy: 'never', source: 'session' })
assert.match(line, /Permission policy: reject/)
Expand Down
35 changes: 35 additions & 0 deletions plugins/subagent-opencode/test/run.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ test('the permission policy picks the configured option kind', () => {
}
assert.deepEqual(permissionOutcome(params, 'allow'), { outcome: { outcome: 'selected', optionId: 'once' } })
assert.deepEqual(permissionOutcome(params, 'reject'), { outcome: { outcome: 'selected', optionId: 'no' } })
assert.deepEqual(permissionOutcome(params, 'session'), { outcome: { outcome: 'selected', optionId: 'no' } })
assert.deepEqual(permissionOutcome({ options: [] }, 'allow'), { outcome: { outcome: 'cancelled' } })
})

Expand Down Expand Up @@ -300,3 +301,37 @@ test('the configured permission name still applies when no policy is supplied',
await run.dispose()
}
})

test('projectPolicy resolves permission session before the permission answer', async () => {
// index.js resolves `permission: session` + an `ask` session to `allow`
// (via projectPolicy), so the child proceeds instead of being denied.
const proceed = await startOpenCodeRun(
request(),
await specFor({ askPermission: true }, { permission: 'session', policy: 'allow' }),
)
try {
const result = await proceed.result
assert.equal(result.stopReason, 'completed')
assert.equal(result.diagnostic, undefined)
} finally {
await proceed.dispose()
}

// The raw 'session' policy string is not an ACP grant: permissionOutcome
// treats anything that is not 'allow' as a rejection, so it answers the
// prompt with a reject option. The fixture does not wait for the answer,
// so the run completes on its own stop reason, but the diagnostic records
// that the permission was answered by policy.
const answered = await startOpenCodeRun(
request(),
await specFor({ askPermission: true, stopReason: 'max_tokens' }, { permission: 'session', policy: 'session' }),
)
try {
const result = await answered.result
assert.equal(result.stopReason, 'max-tokens')
assert.match(result.diagnostic, /permissions requested: 1/)
assert.match(result.diagnostic, /Permission policy: session/)
} finally {
await answered.dispose()
}
})