refactor(sandbox): remove OpenCode version allowlist gating under enforcement

This commit is contained in:
Chris Scott committed 2026-08-22 13:03:07 -04:00
1 parent 0215de9d92
commit aab80f9d65
17 files changed
+98 -524

No files matched your search

@@ -11,11 +11,9 @@ vi.mock('@/components/settings/RestartServerDialog', () => ({
RestartServerDialog: () => null,
}))
const BLOCKED_REASON = 'Updating the OpenCode version is disabled while agent sandboxing is enabled'
function mockHealth(sandbox?: { available: boolean; enforced: boolean; reason?: string; msbVersion?: string }) {
function mockHealth() {
vi.mocked(useServerHealth).mockReturnValue({
data: { opencode: 'healthy', opencodeVersion: '1.18.16', sandbox },
data: { opencode: 'healthy', opencodeVersion: '1.18.16' },
isLoading: false,
error: null,
refetch: vi.fn(),
@@ -41,57 +39,32 @@ function mockActions(overrides: Partial<ReturnType<typeof useOpenCodeServerActio
describe('ServerHealthStatus', () => {
beforeEach(() => {
vi.clearAllMocks()
mockHealth()
mockActions()
})
it('keeps Update and Versions enabled when sandboxing is off', () => {
mockHealth({ available: true, enforced: false })
it('keeps Update and Versions enabled', () => {
render(<ServerHealthStatus />)
expect(screen.getByRole('button', { name: /Update/i })).toBeEnabled()
expect(screen.getByRole('button', { name: /Versions/i })).toBeEnabled()
})
it('disables Update but keeps Versions accessible when sandbox enforcement is on', () => {
mockHealth({ available: true, enforced: true })
render(<ServerHealthStatus />)
const update = screen.getByRole('button', { name: /Update/i })
const versions = screen.getByRole('button', { name: /Versions/i })
expect(update).toBeDisabled()
expect(update).toHaveAttribute('title', BLOCKED_REASON)
expect(versions).toBeEnabled()
expect(screen.getByText(/Agent sandboxing is on; Update is disabled and only verified versions can be installed\./)).toBeInTheDocument()
})
it('keeps Update and Versions enabled while enforcement is pending but not active', () => {
mockHealth({ available: false, enforced: false, reason: 'KVM unavailable' })
render(<ServerHealthStatus />)
expect(screen.getByRole('button', { name: /Update/i })).toBeEnabled()
expect(screen.getByRole('button', { name: /Versions/i })).toBeEnabled()
})
it('does not invoke an upgrade while the Update button is disabled', async () => {
it('invokes an upgrade when Update is clicked', async () => {
const user = userEvent.setup()
const performUpgrade = vi.fn()
mockActions({ performUpgrade })
mockHealth({ available: true, enforced: true })
render(<ServerHealthStatus />)
await user.click(screen.getByRole('button', { name: /Update/i }))
expect(performUpgrade).not.toHaveBeenCalled()
expect(performUpgrade).toHaveBeenCalled()
})
it('opens the version dialog when Versions is clicked under enforcement', async () => {
it('opens the version dialog when Versions is clicked', async () => {
const user = userEvent.setup()
const onOpenVersionDialog = vi.fn()
mockHealth({ available: true, enforced: true })
render(<ServerHealthStatus onOpenVersionDialog={onOpenVersionDialog} />)
@@ -37,10 +37,6 @@ export function ServerHealthStatus({ onOpenVersionDialog }: ServerHealthStatusPr
}
const isUnhealthy = health.opencode !== 'healthy'
const sandboxEnforced = health.sandbox?.enforced === true
const updateBlockedReason = sandboxEnforced
? 'Updating the OpenCode version is disabled while agent sandboxing is enabled'
: undefined
return (
<Card className={cn('bg-transparent border-transparent', isUnhealthy && 'border-destructive')}>
@@ -66,19 +62,13 @@ export function ServerHealthStatus({ onOpenVersionDialog }: ServerHealthStatusPr
Manager v{health.opencodeManagerVersion}
</p>
)}
{sandboxEnforced && (
<p className="text-xs text-muted-foreground">
Agent sandboxing is on; Update is disabled and only verified versions can be installed.
</p>
)}
</div>
<div className="flex flex-wrap gap-2 justify-center sm:justify-end">
<Button
variant="outline"
size="sm"
onClick={performUpgrade}
disabled={upgradeOpenCodeMutation.isPending || sandboxEnforced}
title={updateBlockedReason}
disabled={upgradeOpenCodeMutation.isPending}
>
{upgradeOpenCodeMutation.isPending ? (
<Loader2 className="h-3 w-3 sm:h-4 sm:w-4 mr-1 animate-spin" />
@@ -4,7 +4,7 @@ import userEvent from '@testing-library/user-event'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { VersionSelectDialog } from './VersionSelectDialog'
import { settingsApi } from '@/api/settings'
import { useServerHealth } from '@/hooks/useServerHealth'
import { refreshOpenCodeServerCaches } from '@/lib/queryInvalidation'
vi.mock('@/api/settings', () => ({
settingsApi: {
@@ -13,13 +13,11 @@ vi.mock('@/api/settings', () => ({
},
}))
vi.mock('@/hooks/useServerHealth')
vi.mock('@/lib/toast', () => ({
showToast: { success: vi.fn(), error: vi.fn(), loading: vi.fn(), dismiss: vi.fn() },
}))
vi.mock('@/lib/queryInvalidation', () => ({
invalidateConfigCaches: vi.fn(),
updateOpenCodeVersionCaches: vi.fn(),
refreshOpenCodeServerCaches: vi.fn(),
}))
const mockGetOpenCodeVersions = settingsApi.getOpenCodeVersions as ReturnType<typeof vi.fn>
@@ -34,32 +32,20 @@ function renderDialog(open = true) {
)
}
function mockHealth(enforced: boolean) {
vi.mocked(useServerHealth).mockReturnValue({
data: { sandbox: { available: true, enforced } },
isLoading: false,
error: null,
refetch: vi.fn(),
restartMutation: { mutate: vi.fn(), mutateAsync: vi.fn(), isPending: false },
rollbackMutation: { mutate: vi.fn(), mutateAsync: vi.fn(), isPending: false },
} as ReturnType<typeof useServerHealth>)
}
describe('VersionSelectDialog', () => {
beforeEach(() => {
vi.clearAllMocks()
mockGetOpenCodeVersions.mockResolvedValue({
versions: [
{ version: '1.19.0', tag: 'v1.19.0', name: '', publishedAt: '2026-01-01T00:00:00Z', installable: true },
{ version: '1.18.16', tag: 'v1.18.16', name: '', publishedAt: '2025-12-01T00:00:00Z', installable: true },
{ version: '1.19.0', tag: 'v1.19.0', name: '', publishedAt: '2026-01-01T00:00:00Z' },
{ version: '1.18.16', tag: 'v1.18.16', name: '', publishedAt: '2025-12-01T00:00:00Z' },
],
currentVersion: '1.18.16',
})
mockInstallOpenCodeVersion.mockResolvedValue({ success: true, message: 'ok', oldVersion: null, newVersion: '1.19.0' })
mockHealth(false)
})
it('lists versions and allows selection when sandboxing is off', async () => {
it('lists versions and allows selection', async () => {
const user = userEvent.setup()
renderDialog()
@@ -73,38 +59,11 @@ describe('VersionSelectDialog', () => {
expect(screen.getByRole('button', { name: /^Install$/i })).toBeEnabled()
})
it('keeps verified versions installable and disables unverified versions when sandbox enforcement is on', async () => {
mockHealth(true)
it('installs the selected version', async () => {
mockGetOpenCodeVersions.mockResolvedValue({
versions: [
{ version: '1.19.0', tag: 'v1.19.0', name: '', publishedAt: '2026-01-01T00:00:00Z', installable: false },
{ version: '1.18.16', tag: 'v1.18.16', name: '', publishedAt: '2025-12-01T00:00:00Z', installable: true },
],
currentVersion: '1.17.0',
})
renderDialog()
expect(
await screen.findByText(/only verified OpenCode versions can be installed/),
).toBeInTheDocument()
const unverifiedRow = screen.getByRole('button', { name: /v1\.19\.0/ })
const verifiedRow = screen.getByRole('button', { name: /v1\.18\.16/ })
expect(unverifiedRow).toBeDisabled()
expect(verifiedRow).toBeEnabled()
expect(screen.getByRole('button', { name: /Select version/i })).toBeDisabled()
const user = userEvent.setup()
await user.click(verifiedRow)
expect(screen.getByRole('button', { name: /^Install$/i })).toBeEnabled()
})
it('keeps the install button disabled when the only available row is unverified under enforcement', async () => {
mockHealth(true)
mockGetOpenCodeVersions.mockResolvedValue({
versions: [
{ version: '1.19.0', tag: 'v1.19.0', name: '', publishedAt: '2026-01-01T00:00:00Z', installable: false },
{ version: '1.19.0', tag: 'v1.19.0', name: '', publishedAt: '2026-01-01T00:00:00Z' },
{ version: '1.18.16', tag: 'v1.18.16', name: '', publishedAt: '2025-12-01T00:00:00Z' },
],
currentVersion: '1.17.0',
})
@@ -113,10 +72,12 @@ describe('VersionSelectDialog', () => {
renderDialog()
expect(await screen.findByText('v1.19.0')).toBeInTheDocument()
const unverifiedRow = screen.getByRole('button', { name: /v1\.19\.0/ })
expect(unverifiedRow).toBeDisabled()
await user.click(unverifiedRow)
const versionRow = screen.getByRole('button', { name: /v1\.19\.0/ })
expect(screen.getByRole('button', { name: /Select version/i })).toBeDisabled()
await user.click(versionRow)
await user.click(screen.getByRole('button', { name: /^Install$/i }))
expect(mockInstallOpenCodeVersion).toHaveBeenCalledWith('1.19.0')
expect(refreshOpenCodeServerCaches).toHaveBeenCalledWith(expect.any(QueryClient), '1.19.0')
})
})
@@ -5,8 +5,7 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } f
import { Button } from '@/components/ui/button'
import { settingsApi } from '@/api/settings'
import { showToast } from '@/lib/toast'
import { invalidateConfigCaches, updateOpenCodeVersionCaches } from '@/lib/queryInvalidation'
import { useServerHealth } from '@/hooks/useServerHealth'
import { refreshOpenCodeServerCaches } from '@/lib/queryInvalidation'
interface VersionSelectDialogProps {
open: boolean
@@ -15,8 +14,6 @@ interface VersionSelectDialogProps {
export function VersionSelectDialog({ open, onOpenChange }: VersionSelectDialogProps) {
const queryClient = useQueryClient()
const { data: health } = useServerHealth()
const sandboxEnforced = health?.sandbox?.enforced === true
const [selectedVersion, setSelectedVersion] = useState<string | null>(null)
const { data, isLoading, error } = useQuery({
@@ -26,34 +23,27 @@ export function VersionSelectDialog({ open, onOpenChange }: VersionSelectDialogP
staleTime: 60000,
})
const selectedRelease = data?.versions.find((release) => release.version === selectedVersion)
const selectedInstallable = selectedRelease?.installable === true
const installMutation = useMutation({
mutationFn: (version: string) => settingsApi.installOpenCodeVersion(version),
onSuccess: (result) => {
if (result.newVersion) {
updateOpenCodeVersionCaches(queryClient, result.newVersion)
}
invalidateConfigCaches(queryClient)
refreshOpenCodeServerCaches(queryClient, result.newVersion ?? undefined)
showToast.success(result.message)
onOpenChange(false)
},
onError: (error) => {
queryClient.invalidateQueries({ queryKey: ['opencode-versions'] })
invalidateConfigCaches(queryClient)
if (error && typeof error === 'object' && 'response' in error) {
const response = (error as { response?: { data?: { recovered?: boolean; recoveryMessage?: string; newVersion?: string } } }).response
const data = response?.data
if (data?.recovered && data.newVersion) {
updateOpenCodeVersionCaches(queryClient, data.newVersion)
refreshOpenCodeServerCaches(queryClient, data.newVersion)
showToast.success(`Install failed but server recovered at v${data.newVersion}`)
} else {
refreshOpenCodeServerCaches(queryClient)
showToast.error(data?.recoveryMessage || 'Failed to install version')
}
} else {
refreshOpenCodeServerCaches(queryClient)
showToast.error('Failed to install version')
}
},
@@ -101,13 +91,6 @@ export function VersionSelectDialog({ open, onOpenChange }: VersionSelectDialogP
{data && (
<>
{sandboxEnforced && (
<div className="rounded-lg border border-amber-500/40 bg-amber-500/10 p-3 text-sm text-amber-600 dark:text-amber-400 mb-3">
While agent sandboxing is enabled, only verified OpenCode versions can be installed. Unverified
versions are disabled. Disable Agent Sandboxing in Settings and restart the OpenCode server to
install other versions.
</div>
)}
<div className="h-[300px] overflow-y-auto pr-2">
<div className="space-y-2">
{data.versions.map((release) => {
@@ -118,15 +101,13 @@ export function VersionSelectDialog({ open, onOpenChange }: VersionSelectDialogP
<button
key={release.version}
onClick={() => setSelectedVersion(isCurrent ? null : release.version)}
disabled={isCurrent || installMutation.isPending || !release.installable}
disabled={isCurrent || installMutation.isPending}
className={`w-full text-left p-3 rounded-lg border transition-colors ${
isSelected
? 'border-primary bg-primary/10'
: isCurrent
? 'border-green-500/50 bg-green-500/10 cursor-default'
: !release.installable
? 'border-border opacity-50 cursor-not-allowed'
: 'border-border hover:border-primary/50 hover:bg-muted/50'
: 'border-border hover:border-primary/50 hover:bg-muted/50'
}`}
>
<div className="flex items-center justify-between">
@@ -138,11 +119,6 @@ export function VersionSelectDialog({ open, onOpenChange }: VersionSelectDialogP
Current
</span>
)}
{!release.installable && (
<span className="text-xs px-1.5 py-0.5 rounded bg-muted text-muted-foreground">
Unverified
</span>
)}
</div>
<div className="text-xs text-muted-foreground mt-0.5">
{formatDate(release.publishedAt)}
@@ -166,7 +142,7 @@ export function VersionSelectDialog({ open, onOpenChange }: VersionSelectDialogP
</Button>
<Button
onClick={handleInstall}
disabled={!selectedVersion || installMutation.isPending || !selectedInstallable}
disabled={!selectedVersion || installMutation.isPending}
className="min-w-[120px]"
>
{installMutation.isPending ? (