mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(schedules): validate sandbox only when updated and tear down by the run session's sandbox state
This commit is contained in:
1 parent
070cb2f729
commit
060159f10b
2 files changed
+59
-5
No files matched your search
@@ -16,6 +16,7 @@ import {
|
||||
import { mcpStatusByName, openCodeLocation } from '@opencode-manager/shared/opencode'
|
||||
import { LOCAL_OPENCODE_SERVER_ID, buildSchedulePermissionRuleset } from '@opencode-manager/shared/schemas'
|
||||
import { getRepoById } from '../db/queries'
|
||||
import { getSessionSandboxMode } from '../db/session-sandbox-modes'
|
||||
import type { ScheduleJobWithRepo } from '../db/schedules'
|
||||
import {
|
||||
cleanupOrphanedSchedules,
|
||||
@@ -711,7 +712,9 @@ export class ScheduleService {
|
||||
const existing = this.loadJobForTarget(target, jobId)
|
||||
this.assertInPlaceWorkspace(target, input.workspaceMode)
|
||||
const context = this.resolveTargetContext(target, input.directory ?? existing.directory)
|
||||
this.assertSandboxJobSupported(context.serverId, input.sandbox !== undefined ? input.sandbox : existing.sandbox)
|
||||
if (input.sandbox !== undefined) {
|
||||
this.assertSandboxJobSupported(context.serverId, input.sandbox)
|
||||
}
|
||||
|
||||
if (input.agentSlug !== undefined) {
|
||||
await this.assertAgentAvailable(context.client, context.directory, input.agentSlug?.trim() || null)
|
||||
@@ -1626,7 +1629,7 @@ export class ScheduleService {
|
||||
if (repo && fresh?.worktreePath) {
|
||||
await this.finalizeRunWorktree(repo, job, runId, fresh.worktreePath, fresh.runBranch, fresh.triggerSource, repoId, jobId)
|
||||
}
|
||||
await this.removeRunSandbox(job, sessionId ?? fresh?.sessionId ?? null)
|
||||
await this.removeRunSandbox(sessionId ?? fresh?.sessionId ?? null)
|
||||
} finally {
|
||||
ScheduleService.activeTeardowns.delete(key)
|
||||
}
|
||||
@@ -1657,10 +1660,10 @@ export class ScheduleService {
|
||||
}
|
||||
}
|
||||
|
||||
private async removeRunSandbox(job: ScheduleJob, sessionId: string | null): Promise<void> {
|
||||
if (job.sandbox?.enabled !== true || !sessionId) return
|
||||
private async removeRunSandbox(sessionId: string | null): Promise<void> {
|
||||
const runtime = this.sandbox?.runtime
|
||||
if (!runtime) return
|
||||
if (!sessionId || !runtime) return
|
||||
if (getSessionSandboxMode(this.db, sessionId)?.sandboxed !== true) return
|
||||
try {
|
||||
await runtime.removeSessionSandbox(sessionId)
|
||||
} catch (error) {
|
||||
|
||||
@@ -38,6 +38,7 @@ const mocks = vi.hoisted(() => ({
|
||||
loggerError: vi.fn(),
|
||||
loggerWarn: vi.fn(),
|
||||
updateScheduleRunWorktree: vi.fn(),
|
||||
getSessionSandboxMode: vi.fn(),
|
||||
stubWorktreeManager: {
|
||||
prepare: vi.fn().mockResolvedValue(null),
|
||||
finalize: vi.fn().mockResolvedValue({ commitHash: null }),
|
||||
@@ -76,6 +77,10 @@ vi.mock('../../src/db/schedules', () => ({
|
||||
updateScheduleRunWorktree: mocks.updateScheduleRunWorktree,
|
||||
}))
|
||||
|
||||
vi.mock('../../src/db/session-sandbox-modes', () => ({
|
||||
getSessionSandboxMode: mocks.getSessionSandboxMode,
|
||||
}))
|
||||
|
||||
vi.mock('../../src/services/schedule-config', () => ({
|
||||
buildCreateSchedulePersistenceInput: mocks.buildCreateSchedulePersistenceInput,
|
||||
buildUpdatedSchedulePersistenceInput: mocks.buildUpdatedSchedulePersistenceInput,
|
||||
@@ -122,6 +127,7 @@ function makeService(api: OpenCodeApi): ScheduleService {
|
||||
}
|
||||
|
||||
const SANDBOX_RESOURCES = { cpus: 4, memoryMib: 8192 }
|
||||
const SANDBOX_SESSION_MODE = { sandboxed: true, source: 'user' as const, toggledAt: 0, resources: SANDBOX_RESOURCES }
|
||||
|
||||
function makeSandboxService(api: OpenCodeApi) {
|
||||
const sessions = {
|
||||
@@ -234,6 +240,7 @@ describe('ScheduleService', () => {
|
||||
mocks.getRunningScheduleRunByJob.mockReturnValue(null)
|
||||
mocks.listScheduleJobWorktreeOwners.mockReturnValue([{ id: job.id, repoId: job.repoId, name: job.name }])
|
||||
mocks.createScheduleRun.mockReturnValue(baseRun)
|
||||
mocks.getSessionSandboxMode.mockReturnValue(null)
|
||||
mocks.resolveOpenCodeModel.mockResolvedValue({ providerID: 'openai', id: 'gpt-5-mini', model: 'openai/gpt-5-mini' })
|
||||
mocks.onEvent.mockReturnValue(vi.fn())
|
||||
mocks.onServerConnected.mockReturnValue(vi.fn())
|
||||
@@ -1400,6 +1407,7 @@ describe('ScheduleService', () => {
|
||||
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession())
|
||||
mocks.getScheduleRunById.mockReturnValue(runWithSession())
|
||||
runtime.planShell.mockResolvedValue({ mode: 'blocked', reason: '2 sandbox VMs are already running' })
|
||||
mocks.getSessionSandboxMode.mockReturnValue(SANDBOX_SESSION_MODE)
|
||||
mocks.updateScheduleRun.mockReturnValue({
|
||||
...runWithSession(),
|
||||
status: 'failed',
|
||||
@@ -1433,6 +1441,7 @@ describe('ScheduleService', () => {
|
||||
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession())
|
||||
mocks.getScheduleRunById.mockReturnValue(runWithSession())
|
||||
runtime.removeSessionSandbox.mockRejectedValue(new Error('msb rm failed'))
|
||||
mocks.getSessionSandboxMode.mockReturnValue(SANDBOX_SESSION_MODE)
|
||||
|
||||
await service.runJob(42, 7, 'manual')
|
||||
|
||||
@@ -1469,6 +1478,7 @@ describe('ScheduleService', () => {
|
||||
}
|
||||
mocks.getScheduleRunById.mockReturnValue(runningRun)
|
||||
mocks.updateScheduleRun.mockReturnValue(cancelledRun)
|
||||
mocks.getSessionSandboxMode.mockReturnValue(SANDBOX_SESSION_MODE)
|
||||
|
||||
const result = await service.cancelRun(42, 7, 5)
|
||||
|
||||
@@ -1476,6 +1486,28 @@ describe('ScheduleService', () => {
|
||||
expect(runtime.removeSessionSandbox).toHaveBeenCalledWith('ses-run-3')
|
||||
})
|
||||
|
||||
it('removes the sandbox of a cancelled run whose job no longer enables sandboxing', async () => {
|
||||
for (const sandbox of [{ enabled: false, resources: SANDBOX_RESOURCES }, null] as const) {
|
||||
mocks.getScheduleJobById.mockReturnValue({ ...job, sandbox })
|
||||
const stub = createStubScheduleApi({ messages: [] })
|
||||
const { service, runtime } = makeSandboxService(stub.api)
|
||||
const runningRun = runWithSession('ses-run-4')
|
||||
mocks.getScheduleRunById.mockReturnValue(runningRun)
|
||||
mocks.updateScheduleRun.mockReturnValue({
|
||||
...runningRun,
|
||||
status: 'cancelled',
|
||||
finishedAt: Date.UTC(2026, 2, 9, 12, 10, 0),
|
||||
errorText: 'Run cancelled by user.',
|
||||
})
|
||||
mocks.getSessionSandboxMode.mockReturnValue(SANDBOX_SESSION_MODE)
|
||||
|
||||
await service.cancelRun(42, 7, 5)
|
||||
|
||||
expect(runtime.removeSessionSandbox).toHaveBeenCalledWith('ses-run-4')
|
||||
runtime.removeSessionSandbox.mockClear()
|
||||
}
|
||||
})
|
||||
|
||||
it('leaves the sandbox dependencies untouched for a job without an enabled sandbox', async () => {
|
||||
for (const sandbox of [null, { enabled: false, resources: SANDBOX_RESOURCES }] as const) {
|
||||
mocks.getScheduleJobById.mockReturnValue({ ...job, sandbox })
|
||||
@@ -1557,6 +1589,23 @@ describe('ScheduleService', () => {
|
||||
|
||||
expect(mocks.updateScheduleJob).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('updates a sandboxed schedule without revalidating its stored sandbox config', async () => {
|
||||
const stub = createStubScheduleApi()
|
||||
const { service, sessions } = makeSandboxService(stub.api)
|
||||
const disabledJob = { ...sandboxJob, enabled: false }
|
||||
mocks.getScheduleJobById.mockReturnValue(sandboxJob)
|
||||
sessions.assertSandboxRequestAllowed.mockImplementation(() => {
|
||||
throw new SessionSandboxError('Sandboxing is not enabled for this server', 409)
|
||||
})
|
||||
mocks.buildUpdatedSchedulePersistenceInput.mockReturnValue({ enabled: false })
|
||||
mocks.updateScheduleJob.mockReturnValue(disabledJob)
|
||||
|
||||
await expect(service.updateJob(42, 7, { enabled: false })).resolves.toEqual(disabledJob)
|
||||
|
||||
expect(sessions.assertSandboxRequestAllowed).not.toHaveBeenCalled()
|
||||
expect(mocks.updateScheduleJob).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1572,6 +1621,7 @@ describe('ScheduleService startup cancellation', () => {
|
||||
mocks.getRunningScheduleRunByJob.mockReturnValue(null)
|
||||
mocks.listScheduleJobWorktreeOwners.mockReturnValue([{ id: job.id, repoId: job.repoId, name: job.name }])
|
||||
mocks.createScheduleRun.mockReturnValue(baseRun)
|
||||
mocks.getSessionSandboxMode.mockReturnValue(null)
|
||||
mocks.resolveOpenCodeModel.mockResolvedValue({ providerID: 'openai', id: 'gpt-5-mini', model: 'openai/gpt-5-mini' })
|
||||
mocks.onEvent.mockReturnValue(vi.fn())
|
||||
mocks.onServerConnected.mockReturnValue(vi.fn())
|
||||
@@ -1835,6 +1885,7 @@ describe('ScheduleService monitor cancellation', () => {
|
||||
mocks.getRunningScheduleRunByJob.mockReturnValue(null)
|
||||
mocks.listScheduleJobWorktreeOwners.mockReturnValue([{ id: job.id, repoId: job.repoId, name: job.name }])
|
||||
mocks.createScheduleRun.mockReturnValue(baseRun)
|
||||
mocks.getSessionSandboxMode.mockReturnValue(null)
|
||||
mocks.resolveOpenCodeModel.mockResolvedValue({ providerID: 'openai', id: 'gpt-5-mini', model: 'openai/gpt-5-mini' })
|
||||
mocks.onEvent.mockReturnValue(vi.fn())
|
||||
mocks.onServerConnected.mockReturnValue(vi.fn())
|
||||
|
||||
Reference in new issue
Block a user