mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +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
313 lines
11 KiB
TypeScript
313 lines
11 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import type { ScheduleJob, ScheduleRun } from '@opencode-manager/shared/types'
|
|
import { buildSchedulePermissionRuleset } from '@opencode-manager/shared/schemas'
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
getRepoById: vi.fn(),
|
|
createScheduleJob: vi.fn(),
|
|
createScheduleRun: vi.fn(),
|
|
deleteScheduleJob: vi.fn(),
|
|
deleteScheduleRunById: vi.fn(),
|
|
deleteScheduleRunsByIds: vi.fn(),
|
|
listScheduleRunArtifactsByJob: vi.fn(),
|
|
cleanupOrphanedSchedules: vi.fn(),
|
|
getScheduleJobById: vi.fn(),
|
|
getRunningScheduleRunByJob: vi.fn(),
|
|
getScheduleRunById: vi.fn(),
|
|
listEnabledScheduleJobs: vi.fn(),
|
|
listRunningScheduleRuns: vi.fn(),
|
|
listScheduleJobIdsByRepo: vi.fn(),
|
|
listScheduleJobsByRepo: vi.fn(),
|
|
listScheduleRunsByJob: vi.fn(),
|
|
updateScheduleJob: vi.fn(),
|
|
updateScheduleJobRunState: vi.fn(),
|
|
updateScheduleRun: vi.fn(),
|
|
updateScheduleRunMetadata: vi.fn(),
|
|
buildCreateSchedulePersistenceInput: vi.fn(),
|
|
buildUpdatedSchedulePersistenceInput: vi.fn(),
|
|
computeNextRunAtForJob: vi.fn(),
|
|
|
|
resolveOpenCodeModel: vi.fn(),
|
|
forward: vi.fn(),
|
|
onEvent: vi.fn(),
|
|
loggerError: vi.fn(),
|
|
updateScheduleRunWorktree: vi.fn(),
|
|
stubWorktreeManager: {
|
|
prepare: vi.fn().mockResolvedValue(null),
|
|
finalize: vi.fn().mockResolvedValue({ commitHash: null }),
|
|
pruneRunArtifacts: vi.fn().mockResolvedValue(undefined),
|
|
},
|
|
}))
|
|
|
|
vi.mock('../../src/db/queries', () => ({
|
|
getRepoById: mocks.getRepoById,
|
|
}))
|
|
|
|
vi.mock('../../src/db/schedules', () => ({
|
|
createScheduleJob: mocks.createScheduleJob,
|
|
createScheduleRun: mocks.createScheduleRun,
|
|
deleteScheduleJob: mocks.deleteScheduleJob,
|
|
deleteScheduleRunById: mocks.deleteScheduleRunById,
|
|
deleteScheduleRunsByIds: mocks.deleteScheduleRunsByIds,
|
|
listScheduleRunArtifactsByJob: mocks.listScheduleRunArtifactsByJob,
|
|
cleanupOrphanedSchedules: mocks.cleanupOrphanedSchedules,
|
|
getScheduleJobById: mocks.getScheduleJobById,
|
|
getRunningScheduleRunByJob: mocks.getRunningScheduleRunByJob,
|
|
getScheduleRunById: mocks.getScheduleRunById,
|
|
listEnabledScheduleJobs: mocks.listEnabledScheduleJobs,
|
|
listRunningScheduleRuns: mocks.listRunningScheduleRuns,
|
|
listScheduleJobIdsByRepo: mocks.listScheduleJobIdsByRepo,
|
|
listScheduleJobsByRepo: mocks.listScheduleJobsByRepo,
|
|
listScheduleRunsByJob: mocks.listScheduleRunsByJob,
|
|
updateScheduleJob: mocks.updateScheduleJob,
|
|
updateScheduleJobRunState: mocks.updateScheduleJobRunState,
|
|
updateScheduleRun: mocks.updateScheduleRun,
|
|
updateScheduleRunMetadata: mocks.updateScheduleRunMetadata,
|
|
updateScheduleRunWorktree: mocks.updateScheduleRunWorktree,
|
|
}))
|
|
|
|
vi.mock('../../src/services/schedule-config', () => ({
|
|
buildCreateSchedulePersistenceInput: mocks.buildCreateSchedulePersistenceInput,
|
|
buildUpdatedSchedulePersistenceInput: mocks.buildUpdatedSchedulePersistenceInput,
|
|
computeNextRunAtForJob: mocks.computeNextRunAtForJob,
|
|
}))
|
|
|
|
vi.mock('../../src/services/opencode-models', () => ({
|
|
resolveOpenCodeModel: mocks.resolveOpenCodeModel,
|
|
}))
|
|
|
|
vi.mock('../../src/services/sse-aggregator', () => ({
|
|
sseAggregator: {
|
|
onEvent: mocks.onEvent,
|
|
},
|
|
}))
|
|
|
|
vi.mock('../../src/utils/logger', () => ({
|
|
logger: {
|
|
error: mocks.loggerError,
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
const mockCronStop = vi.fn()
|
|
|
|
vi.mock('croner', () => ({
|
|
Cron: vi.fn().mockImplementation(() => ({ stop: mockCronStop })),
|
|
}))
|
|
|
|
import { ScheduleService } from '../../src/services/schedules'
|
|
import type { ForwardRequest, OpenCodeClient } from '../../src/services/opencode/client'
|
|
|
|
function jsonResponse(body: unknown, status: number = 200): Response {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { 'content-type': 'application/json' },
|
|
})
|
|
}
|
|
|
|
function textResponse(body: string, status: number = 200): Response {
|
|
return new Response(body, { status })
|
|
}
|
|
|
|
function createOpenCodeClientStub(): OpenCodeClient {
|
|
return {
|
|
forward: mocks.forward,
|
|
forwardRaw: vi.fn(async () => new Response('', { status: 200 })),
|
|
getJson: vi.fn(async () => ({}) as unknown),
|
|
postJson: vi.fn(async () => ({}) as unknown),
|
|
setProviderAuth: vi.fn(async () => true),
|
|
deleteProviderAuth: vi.fn(async () => true),
|
|
} as OpenCodeClient
|
|
}
|
|
|
|
function routeForward(handler: (req: ForwardRequest) => Promise<Response> | Response) {
|
|
mocks.forward.mockImplementation((req: ForwardRequest) => Promise.resolve(handler(req)))
|
|
}
|
|
|
|
const repo = {
|
|
id: 42,
|
|
fullPath: '/workspace/repos/sample-project',
|
|
localPath: 'sample-project',
|
|
repoUrl: 'https://github.com/example/sample-project',
|
|
}
|
|
|
|
const baseJob: ScheduleJob = {
|
|
id: 7,
|
|
repoId: 42,
|
|
name: 'Weekly engineering summary',
|
|
description: 'Summarize repo health and recent changes.',
|
|
enabled: true,
|
|
scheduleMode: 'interval',
|
|
intervalMinutes: 60,
|
|
cronExpression: null,
|
|
timezone: null,
|
|
agentSlug: null,
|
|
prompt: 'Review the repository and summarize the current state.',
|
|
model: null,
|
|
skillMetadata: null,
|
|
permissionConfig: null,
|
|
branch: null,
|
|
nextRunAt: Date.UTC(2026, 2, 9, 13, 0, 0),
|
|
lastRunAt: Date.UTC(2026, 2, 9, 12, 0, 0),
|
|
createdAt: Date.UTC(2026, 2, 8, 12, 0, 0),
|
|
updatedAt: Date.UTC(2026, 2, 9, 12, 0, 0),
|
|
}
|
|
|
|
const baseRun: ScheduleRun = {
|
|
id: 5,
|
|
jobId: 7,
|
|
repoId: 42,
|
|
triggerSource: 'manual',
|
|
status: 'running',
|
|
startedAt: Date.UTC(2026, 2, 9, 12, 5, 0),
|
|
finishedAt: null,
|
|
createdAt: Date.UTC(2026, 2, 9, 12, 5, 0),
|
|
sessionId: null,
|
|
sessionTitle: null,
|
|
logText: null,
|
|
responseText: null,
|
|
errorText: null,
|
|
runBranch: null,
|
|
commitHash: null,
|
|
worktreePath: null,
|
|
workspaceId: null,
|
|
}
|
|
|
|
describe('ScheduleService permission ruleset in session creation', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
Reflect.get(ScheduleService, 'activeRuns').clear()
|
|
Reflect.get(ScheduleService, 'activeTeardowns')?.clear()
|
|
|
|
mocks.getRepoById.mockReturnValue(repo)
|
|
mocks.getRunningScheduleRunByJob.mockReturnValue(null)
|
|
mocks.createScheduleRun.mockReturnValue(baseRun)
|
|
mocks.resolveOpenCodeModel.mockResolvedValue({ providerID: 'openai', modelID: 'gpt-5-mini' })
|
|
mocks.onEvent.mockReturnValue(vi.fn())
|
|
mocks.getScheduleRunById.mockReturnValue({
|
|
...baseRun,
|
|
sessionId: 'ses-perm-test',
|
|
sessionTitle: 'Scheduled: Weekly engineering summary',
|
|
logText: 'Run started.',
|
|
})
|
|
})
|
|
|
|
it('sends default permission ruleset when job.permissionConfig is null', async () => {
|
|
mocks.getScheduleJobById.mockReturnValue(baseJob)
|
|
|
|
const runWithSession: ScheduleRun = {
|
|
...baseRun,
|
|
sessionId: 'ses-perm-1',
|
|
sessionTitle: 'Scheduled: Weekly engineering summary',
|
|
logText: 'Run started.',
|
|
}
|
|
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession)
|
|
|
|
routeForward(({ path, method }) => {
|
|
if (path === '/session' && method === 'POST') {
|
|
return jsonResponse({ id: 'ses-perm-1' })
|
|
}
|
|
if (path.match(/^\/session\/[\w-]+\/prompt_async$/) && method === 'POST') {
|
|
return textResponse('')
|
|
}
|
|
if (path.match(/^\/session\/[\w-]+\/message$/) && method === 'GET') {
|
|
return jsonResponse([{ info: { role: 'assistant', time: { completed: Date.now() } }, parts: [] }])
|
|
}
|
|
throw new Error(`Unexpected forward request: ${method} ${path}`)
|
|
})
|
|
|
|
const service = new ScheduleService({} as never, createOpenCodeClientStub(), mocks.stubWorktreeManager as never)
|
|
await service.runJob(42, 7, 'manual')
|
|
|
|
const sessionCall = mocks.forward.mock.calls.find(
|
|
([req]) => (req as ForwardRequest).path === '/session' && (req as ForwardRequest).method === 'POST',
|
|
)
|
|
expect(sessionCall).toBeDefined()
|
|
const body = JSON.parse((sessionCall![0] as ForwardRequest).body!)
|
|
expect(body.title).toBe('Scheduled: Weekly engineering summary')
|
|
expect(body.agent).toBeUndefined()
|
|
|
|
const expectedRules = buildSchedulePermissionRuleset(null)
|
|
expect(body.permission).toEqual(expectedRules)
|
|
})
|
|
|
|
it('sends custom permission ruleset when job has custom permissionConfig', async () => {
|
|
const customConfig = { allowExternalDirectory: true, allowQuestions: true, bashDenyPatterns: [] }
|
|
mocks.getScheduleJobById.mockReturnValue({ ...baseJob, permissionConfig: customConfig })
|
|
|
|
const runWithSession: ScheduleRun = {
|
|
...baseRun,
|
|
sessionId: 'ses-perm-2',
|
|
sessionTitle: 'Scheduled: Weekly engineering summary',
|
|
logText: 'Run started.',
|
|
}
|
|
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession)
|
|
|
|
routeForward(({ path, method }) => {
|
|
if (path === '/session' && method === 'POST') {
|
|
return jsonResponse({ id: 'ses-perm-2' })
|
|
}
|
|
if (path.match(/^\/session\/[\w-]+\/prompt_async$/) && method === 'POST') {
|
|
return textResponse('')
|
|
}
|
|
if (path.match(/^\/session\/[\w-]+\/message$/) && method === 'GET') {
|
|
return jsonResponse([{ info: { role: 'assistant', time: { completed: Date.now() } }, parts: [] }])
|
|
}
|
|
throw new Error(`Unexpected forward request: ${method} ${path}`)
|
|
})
|
|
|
|
const service = new ScheduleService({} as never, createOpenCodeClientStub(), mocks.stubWorktreeManager as never)
|
|
await service.runJob(42, 7, 'manual')
|
|
|
|
const sessionCall = mocks.forward.mock.calls.find(
|
|
([req]) => (req as ForwardRequest).path === '/session' && (req as ForwardRequest).method === 'POST',
|
|
)
|
|
expect(sessionCall).toBeDefined()
|
|
const body = JSON.parse((sessionCall![0] as ForwardRequest).body!)
|
|
expect(body.title).toBe('Scheduled: Weekly engineering summary')
|
|
expect(body.agent).toBeUndefined()
|
|
|
|
const expectedRules = buildSchedulePermissionRuleset(customConfig)
|
|
expect(body.permission).toEqual(expectedRules)
|
|
})
|
|
|
|
it('preserves existing fields (title, agent) when permission is added', async () => {
|
|
mocks.getScheduleJobById.mockReturnValue({ ...baseJob, agentSlug: 'my-agent' })
|
|
|
|
const runWithSession: ScheduleRun = {
|
|
...baseRun,
|
|
sessionId: 'ses-perm-3',
|
|
sessionTitle: 'Scheduled: Weekly engineering summary',
|
|
logText: 'Run started.',
|
|
}
|
|
mocks.updateScheduleRunMetadata.mockReturnValue(runWithSession)
|
|
|
|
routeForward(({ path, method }) => {
|
|
if (path === '/session' && method === 'POST') {
|
|
return jsonResponse({ id: 'ses-perm-3' })
|
|
}
|
|
if (path.match(/^\/session\/[\w-]+\/prompt_async$/) && method === 'POST') {
|
|
return textResponse('')
|
|
}
|
|
if (path.match(/^\/session\/[\w-]+\/message$/) && method === 'GET') {
|
|
return jsonResponse([{ info: { role: 'assistant', time: { completed: Date.now() } }, parts: [] }])
|
|
}
|
|
throw new Error(`Unexpected forward request: ${method} ${path}`)
|
|
})
|
|
|
|
const service = new ScheduleService({} as never, createOpenCodeClientStub(), mocks.stubWorktreeManager as never)
|
|
await service.runJob(42, 7, 'manual')
|
|
|
|
const sessionCall = mocks.forward.mock.calls.find(
|
|
([req]) => (req as ForwardRequest).path === '/session' && (req as ForwardRequest).method === 'POST',
|
|
)
|
|
expect(sessionCall).toBeDefined()
|
|
const body = JSON.parse((sessionCall![0] as ForwardRequest).body!)
|
|
expect(body.title).toBe('Scheduled: Weekly engineering summary')
|
|
expect(body.agent).toBe('my-agent')
|
|
expect(body.permission).toBeDefined()
|
|
expect(body.permission[0]).toEqual({ permission: '*', pattern: '*', action: 'allow' })
|
|
})
|
|
})
|