fix: limit the restart-pending 409 to entity patches and keep inherited agent permissions (OM-179)

PATCH /opencode-config refuses only patches that name an agent, command, provider or MCP server while a restart is pending; other keys still apply. The assistant API reference documents the new 409.

The agent dialog adds a wildcard permission rule only for an action whose effect the user changed, so saving an agent no longer overrides an inherited global ask or deny.
This commit is contained in:
Chris Scott committed 2026-11-05 12:43:42 -05:00
1 parent 8d92f86142
commit 38d618be9b
5 files changed
+85 -19

No files matched your search

@@ -205,7 +205,6 @@ describe('AgentDialog', () => {
body: { temperature: 0.2, top_p: 0.5, reasoning_effort: 'high' },
})
expect(agent.permissions).toEqual([
{ action: 'edit', resource: '*', effect: 'allow' },
{ action: 'shell', resource: '*', effect: 'deny' },
{ action: 'edit', resource: 'src/**', effect: 'ask' },
{ action: 'webfetch', resource: '*', effect: 'allow' },
@@ -242,14 +241,12 @@ describe('AgentDialog', () => {
const agent = onSubmit.mock.calls[0][1]
expect(agent.permissions).toEqual([
{ action: 'shell', resource: '*', effect: 'allow' },
{ action: 'webfetch', resource: '*', effect: 'allow' },
{ action: 'edit', resource: '*', effect: 'ask' },
{ action: 'edit', resource: 'src/**', effect: 'allow' },
])
})
it('prepends wildcard rules for managed actions that have none', async () => {
it('adds no wildcard rule for an unchanged action so it keeps inheriting the global permission', async () => {
const onSubmit = vi.fn()
const user = userEvent.setup()
render(
@@ -274,14 +271,60 @@ describe('AgentDialog', () => {
const agent = onSubmit.mock.calls[0][1]
expect(agent.permissions).toEqual([
{ action: 'edit', resource: '*', effect: 'allow' },
{ action: 'shell', resource: '*', effect: 'allow' },
{ action: 'webfetch', resource: '*', effect: 'allow' },
{ action: 'edit', resource: 'src/**', effect: 'deny' },
{ action: 'shell', resource: 'git status', effect: 'ask' },
])
})
it('prepends a wildcard rule only for a changed action that has none', async () => {
const onSubmit = vi.fn()
const user = userEvent.setup()
render(
<AgentDialog
open
onOpenChange={vi.fn()}
onSubmit={onSubmit}
editingAgent={{
name: 'reviewer',
agent: {
system: 'Review code',
permissions: [{ action: 'shell', resource: 'git status', effect: 'ask' }],
},
}}
/>,
)
await user.click(screen.getByRole('combobox', { name: 'Shell' }))
await user.click(screen.getByRole('option', { name: 'Deny' }))
await user.click(screen.getByRole('button', { name: 'Update' }))
const agent = onSubmit.mock.calls[0][1]
expect(agent.permissions).toEqual([
{ action: 'shell', resource: '*', effect: 'deny' },
{ action: 'shell', resource: 'git status', effect: 'ask' },
])
})
it('writes no permissions for a prompt-only save of an agent without permission rules', async () => {
const onSubmit = vi.fn()
const user = userEvent.setup()
render(
<AgentDialog
open
onOpenChange={vi.fn()}
onSubmit={onSubmit}
editingAgent={{ name: 'reviewer', agent: { system: 'Review code' } }}
/>,
)
await user.type(screen.getByLabelText('Prompt'), ' carefully')
await user.click(screen.getByRole('button', { name: 'Update' }))
const agent = onSubmit.mock.calls[0][1]
expect(agent.system).toBe('Review code carefully')
expect(agent).not.toHaveProperty('permissions')
})
it('shows the last matching wildcard effect and applies the form effect to every occurrence', async () => {
const onSubmit = vi.fn()
const user = userEvent.setup()
@@ -311,8 +354,6 @@ describe('AgentDialog', () => {
const agent = onSubmit.mock.calls[0][1]
expect(agent.permissions).toEqual([
{ action: 'shell', resource: '*', effect: 'allow' },
{ action: 'webfetch', resource: '*', effect: 'allow' },
{ action: 'edit', resource: '*', effect: 'ask' },
{ action: 'edit', resource: '*', effect: 'ask' },
])
@@ -47,11 +47,13 @@ const permissionEffect = (
/**
* Applies the form effects to the managed wildcard rules in place, so the rule
* order OpenCode resolves with last-match-wins stays unchanged, and prepends a
* wildcard fallback for managed actions that have no wildcard rule yet.
* wildcard fallback only for changed actions that have no wildcard rule yet, so
* an untouched action keeps inheriting the global permission.
*/
const withManagedPermissionEffects = (
permissions: OpenCodePermissionRule[] | undefined,
effects: Record<ManagedPermissionAction, PermissionEffect>,
changedActions: readonly ManagedPermissionAction[],
): OpenCodePermissionRule[] => {
const updated = (permissions ?? []).map((rule) =>
isManagedWildcardRule(rule)
@@ -59,7 +61,7 @@ const withManagedPermissionEffects = (
: rule,
)
const actionsWithWildcard = new Set(updated.filter(isManagedWildcardRule).map((rule) => rule.action))
const fallbacks = MANAGED_PERMISSION_ACTIONS
const fallbacks = changedActions
.filter((action) => !actionsWithWildcard.has(action))
.map((action) => ({ action, resource: '*', effect: effects[action] }))
return [...fallbacks, ...updated]
@@ -86,6 +88,12 @@ const agentFormSchema = z.object({
type AgentFormValues = z.infer<typeof agentFormSchema>
const formPermissionEffects = (values: AgentFormValues): Record<ManagedPermissionAction, PermissionEffect> => ({
edit: values.editPermission,
shell: values.shellPermission,
webfetch: values.webfetchPermission,
})
interface AgentDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
@@ -124,6 +132,10 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen
const handleSubmit = (values: AgentFormValues) => {
const previous = editingAgent?.agent
const effects = formPermissionEffects(values)
const loadedEffects = formPermissionEffects(getDefaultValues(editingAgent))
const changedActions = MANAGED_PERMISSION_ACTIONS.filter((action) => effects[action] !== loadedEffects[action])
const permissions = withManagedPermissionEffects(previous?.permissions, effects, changedActions)
const agent: OpenCodeAgentPatch = {
...previous,
system: values.prompt,
@@ -131,11 +143,7 @@ export function AgentDialog({ open, onOpenChange, onSubmit, editingAgent }: Agen
mode: values.mode,
model: values.model?.trim() || null,
disabled: values.disable,
permissions: withManagedPermissionEffects(previous?.permissions, {
edit: values.editPermission,
shell: values.shellPermission,
webfetch: values.webfetchPermission,
}),
...(permissions.length > 0 || previous?.permissions ? { permissions } : {}),
request: {
...previous?.request,
body: {