mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
* feat(sandbox): add KVM-backed agent sandboxing via microsandbox * feat(sandbox): route host-shell surfaces into microVM via shell wrapper * feat(sandbox): route host-shell surfaces into microVM via shell wrapper * fix(sandbox): harden enforcement validation and backup handling * fix(sandbox): bound lifecycle queue and harden install/backup paths Bound the OpenCode supervisor lifecycle queue to one running plus one pending operation so repeated restarts cannot stack unbounded HTTP requests, while exempting stop() so shutdown is never dropped. Reject enforcement backups that carry no recoverable removed sections, matching the existing fail-closed handling of unparseable backups. Install OpenCode through a private mktemp staging directory instead of the predictable /tmp/opencode path. Use named path/os imports in the files added by this branch. * refactor(sandbox): remove OpenCode version allowlist gating under enforcement * fix(settings): use resolved opencode binary and verify installed version * refactor(sandbox): scope enforcement to agent bash tool only * fix(sandbox): plan bash against factory directory and attest runtime tmpfs; align permission reply API * fix(sandbox): pin neutral entrypoint and persist opencode upgrades across rebuilds * fix(sandbox): align canonical spec with msb 0.6.8 runtime defaults * feat(sandbox): show unwrapped sandboxed bash commands in message UI * refactor(sandbox): pin shell shim to sandbox agent bash without rewriting commands * feat(sandbox): route notifications through a host-side manager tool * feat(sandbox): route all assistant capabilities through the ocm tool Add an allow-listed request action to the generated ocm plugin so agents reach settings, assistant reload, repos, git-info, workspaces, and schedules through the tool instead of curling the internal API with a bearer token, which is impossible under sandboxing. Rewrite the curl-based skills to use the ocm request action and drop the apiBaseUrl plumbing. Remove the now-unread .opencode/internal-token file and the sandbox tmpfs mask that hid it; attestation now rejects any tmpfs other than the runtime /tmp mount. * fix(sidebar): wire new-session and new-schedule actions to every page AssistantRedirect never registered a listener for the sidebar's new-session event, so the desktop sidebar CTA on /assistant was a no-op; the Schedules pages had the same gap for new-schedule. Make useSidebarAction the single source of truth for action keys and event dispatch, and warn when an action is dispatched with no mounted handler so an unhandled CTA fails loudly instead of silently. Wire the missing listeners to the existing per-page handlers. * feat(sandbox): forward git credentials into the microVM and add sandbox guest image * feat(sandbox): add git credential forwarding toggle to settings
121 lines
4.3 KiB
TypeScript
121 lines
4.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
|
|
const managerMock = vi.hoisted(() => ({
|
|
getLastStartupError: vi.fn<() => string | null>(() => null),
|
|
clearStartupError: vi.fn<() => void>(),
|
|
restart: vi.fn<() => Promise<void>>(),
|
|
checkHealth: vi.fn<() => boolean>(() => true),
|
|
}))
|
|
|
|
vi.mock('../../src/services/opencode-single-server', () => ({
|
|
opencodeServerManager: managerMock,
|
|
}))
|
|
|
|
import {
|
|
restartOpenCode,
|
|
restartOpenCodeAfterCommit,
|
|
setOpenCodeRestartCoordinator,
|
|
} from '../../src/services/opencode-restart'
|
|
import type { OpenCodeRestartCoordinator } from '../../src/services/opencode-restart-coordinator'
|
|
import type { OpenCodeSupervisor } from '../../src/services/opencode-supervisor'
|
|
|
|
function createSupervisor(healthy: boolean): OpenCodeSupervisor {
|
|
return {
|
|
restart: vi.fn().mockResolvedValue({ healthy }),
|
|
reloadConfig: vi.fn(),
|
|
} as unknown as OpenCodeSupervisor
|
|
}
|
|
|
|
function createCoordinator(healthy: boolean, resumedSessionIDs: string[] = []): OpenCodeRestartCoordinator {
|
|
return {
|
|
runWithResume: vi.fn(async (restart: () => Promise<boolean>) => ({
|
|
healthy: healthy ?? (await restart()),
|
|
resumedSessionIDs,
|
|
})),
|
|
} as unknown as OpenCodeRestartCoordinator
|
|
}
|
|
|
|
describe('restartOpenCode', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
setOpenCodeRestartCoordinator(null)
|
|
})
|
|
|
|
afterEach(() => {
|
|
setOpenCodeRestartCoordinator(null)
|
|
})
|
|
|
|
it('throws with the startup failure reason when the coordinator reports an unhealthy restart', async () => {
|
|
managerMock.getLastStartupError.mockReturnValue('OpenCode version 1.18.15 does not support sandboxed bash tool rewriting')
|
|
setOpenCodeRestartCoordinator(createCoordinator(false))
|
|
|
|
await expect(restartOpenCode(createSupervisor(true))).rejects.toThrow(
|
|
'OpenCode version 1.18.15 does not support sandboxed bash tool rewriting',
|
|
)
|
|
})
|
|
|
|
it('preserves resumed session IDs only when the coordinator reports a healthy restart', async () => {
|
|
setOpenCodeRestartCoordinator(createCoordinator(true, ['session-1', 'session-2']))
|
|
|
|
const result = await restartOpenCode(createSupervisor(true))
|
|
|
|
expect(result).toEqual({ resumedSessionIDs: ['session-1', 'session-2'] })
|
|
})
|
|
|
|
it('throws with the startup failure reason when the supervisor restart is unhealthy without a coordinator', async () => {
|
|
managerMock.getLastStartupError.mockReturnValue('OpenCode server failed to become healthy')
|
|
|
|
await expect(restartOpenCode(createSupervisor(false))).rejects.toThrow('OpenCode server failed to become healthy')
|
|
})
|
|
|
|
it('returns without resumed sessions when the supervisor restart is healthy without a coordinator', async () => {
|
|
const supervisor = createSupervisor(true)
|
|
|
|
const result = await restartOpenCode(supervisor)
|
|
|
|
expect(result).toEqual({ resumedSessionIDs: [] })
|
|
expect(supervisor.restart).toHaveBeenCalledWith('settings_restart')
|
|
})
|
|
|
|
it('uses a generic failure message when no startup error is recorded', async () => {
|
|
managerMock.getLastStartupError.mockReturnValue(null)
|
|
|
|
await expect(restartOpenCode(createSupervisor(false))).rejects.toThrow(
|
|
'OpenCode server restart did not complete successfully',
|
|
)
|
|
})
|
|
|
|
it('propagates a manager restart failure when no supervisor is provided', async () => {
|
|
managerMock.restart.mockRejectedValue(new Error('server failed to become healthy'))
|
|
|
|
await expect(restartOpenCode()).rejects.toThrow('server failed to become healthy')
|
|
expect(managerMock.clearStartupError).toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('restartOpenCodeAfterCommit', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
setOpenCodeRestartCoordinator(null)
|
|
})
|
|
|
|
afterEach(() => {
|
|
setOpenCodeRestartCoordinator(null)
|
|
})
|
|
|
|
it('reports success without a restart error when the restart completes', async () => {
|
|
managerMock.checkHealth.mockReturnValue(true)
|
|
|
|
await expect(restartOpenCodeAfterCommit(createSupervisor(true))).resolves.toEqual({ restartFailed: false })
|
|
})
|
|
|
|
it('reports the failure instead of throwing so the caller can still return the persisted entity', async () => {
|
|
managerMock.getLastStartupError.mockReturnValue('OpenCode health check failed')
|
|
|
|
const result = await restartOpenCodeAfterCommit(createSupervisor(false))
|
|
|
|
expect(result.restartFailed).toBe(true)
|
|
expect(result.restartError).toBe('OpenCode health check failed')
|
|
})
|
|
})
|