feat(opencode): migrate OpenCode Manager to OpenCode 2 (#359)

* feat(opencode): migrate OpenCode Manager to OpenCode 2

Port the backend lifecycle, generated plugins, event aggregation,
schedules, worktrees, provider credentials, MCP, config handling, the
frontend chat model, and the ocm CLI to OpenCode 2.

Adopt @opencode/client 2.0.15 as the single typed V2 API contract,
require OpenCode 2.0.0 or newer (pinned 2.0.15), and delete every
V1-only code path.

* refactor(opencode): reload config in place and drop v1 compatibility shims

Route config/skill/AGENTS.md changes through the OpenCode location reload
API instead of a full server restart, stop auto-resuming interrupted
sessions, remove the restart coordinator and v1 MCP/version shims, and pin
support to OpenCode >=2.0.15 <3.0.0. Adds integration-ready retries,
service-mode launch, ocm-cli repo proxy, and new frontend form/MCP helpers.

* refactor(opencode): consolidate v2 migration helpers and harden install, proxy, and session flows

- extract shared semver and model-ref utilities, reuse across version checks
- share the OpenCode release download script across Docker, entrypoint, dev setup
- unify install/upgrade failure recovery and guard externally managed OPENCODE_BIN
- scope the repo proxy location rewrite and surface ocm proxy errors
- rework frontend session projection, permission lookup, and cache invalidation

* fix(schedules): reject unavailable agents before creating a session

* feat(opencode): add ocm install, schedule agent fallback, and session move guards
This commit is contained in:
Chris Scott authored and GitHub committed 2026-09-26 13:16:45 -04:00
1 parent 6c2ebd764b
commit dacde879d6
383 files changed
+29905 -46566

No files matched your search

@@ -1,19 +1,27 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { createElement } from 'react'
import { useCommandHandler } from '../useCommandHandler'
const mocks = vi.hoisted(() => ({
sendCommand: vi.fn(),
summarizeSession: vi.fn(),
runCommand: vi.fn(),
compactSession: vi.fn(),
switchSessionModel: vi.fn(),
switchSessionAgent: vi.fn(),
setStatus: vi.fn(),
}))
vi.mock('@/api/opencode', () => ({
createOpenCodeClient: vi.fn().mockImplementation(() => ({
sendCommand: mocks.sendCommand,
summarizeSession: mocks.summarizeSession,
})),
}))
vi.mock('@/api/opencode', async () => {
const actual = await vi.importActual('@/api/opencode')
return {
...actual,
runCommand: mocks.runCommand,
compactSession: mocks.compactSession,
switchSessionModel: mocks.switchSessionModel,
switchSessionAgent: mocks.switchSessionAgent,
}
})
vi.mock('@/lib/toast', () => ({
showToast: {
@@ -24,19 +32,6 @@ vi.mock('@/lib/toast', () => ({
},
}))
vi.mock('@/hooks/useOpenCode', () => ({
useCreateSession: vi.fn(() => ({
mutateAsync: vi.fn(),
})),
}))
vi.mock('@/hooks/useModelSelection', () => ({
useModelSelection: vi.fn(() => ({
model: { providerID: 'test-provider', modelID: 'test-model' },
modelString: 'test-provider/test-model',
})),
}))
vi.mock('react-router-dom', () => ({
useNavigate: vi.fn(() => vi.fn()),
}))
@@ -45,75 +40,185 @@ vi.mock('@/stores/sessionStatusStore', () => ({
useSessionStatus: vi.fn((selector) => selector({ setStatus: mocks.setStatus })),
}))
const createTestQueryClient = () => new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
})
const sessionInfo = (overrides: Record<string, unknown> = {}) => ({
id: 'test-session-id',
projectID: 'proj_1',
time: { created: 1000, updated: 1000 },
location: { directory: '/test/dir' },
agent: 'build',
model: { providerID: 'anthropic', id: 'claude-sonnet-4' },
...overrides,
})
describe('useCommandHandler', () => {
const baseProps = {
opcodeUrl: 'http://localhost:5551',
sessionID: 'test-session-id',
directory: '/test/dir',
currentAgent: 'test-agent',
let queryClient: QueryClient
const renderHandler = (props: Record<string, unknown> = {}) =>
renderHook(
() => useCommandHandler({ sessionID: 'test-session-id', directory: '/test/dir', ...props }),
{
wrapper: ({ children }) =>
createElement(QueryClientProvider, { client: queryClient }, children),
},
)
const setSession = (session: Record<string, unknown>) => {
queryClient.setQueryData(
['opencode', 'session', 'test-session-id', '/test/dir'],
session,
)
}
beforeEach(() => {
vi.clearAllMocks()
queryClient = createTestQueryClient()
mocks.runCommand.mockResolvedValue(undefined)
mocks.compactSession.mockResolvedValue(undefined)
mocks.switchSessionModel.mockResolvedValue(undefined)
mocks.switchSessionAgent.mockResolvedValue(undefined)
})
it('themes command sends command', async () => {
mocks.sendCommand.mockResolvedValue({ info: { id: 'asm_1' }, parts: [] })
it('themes command runs the V2 command without injecting an agent attachment', async () => {
setSession(sessionInfo({ agent: 'test-agent' }))
const { result } = renderHook(() => useCommandHandler(baseProps))
const themesCommand = { name: 'themes' as const }
const { result } = renderHandler({ currentAgent: 'test-agent' })
const themesCommand = { name: 'themes' }
await result.current.executeCommand(themesCommand, '')
await result.current.executeCommand(themesCommand, { text: '' })
expect(mocks.sendCommand).toHaveBeenCalledWith('test-session-id', {
command: 'themes',
arguments: '',
agent: 'test-agent',
model: 'test-provider/test-model',
expect(mocks.runCommand).toHaveBeenCalledWith({
sessionID: 'test-session-id',
name: 'themes',
text: '',
})
})
it('compact command summarizes session', async () => {
mocks.summarizeSession.mockResolvedValue(undefined)
it('compact command compacts the session', async () => {
setSession(sessionInfo())
const { result } = renderHook(() => useCommandHandler(baseProps))
const compactCommand = { name: 'compact' as const }
const { result } = renderHandler()
const compactCommand = { name: 'compact' }
await result.current.executeCommand(compactCommand, '')
await result.current.executeCommand(compactCommand, { text: '' })
expect(mocks.summarizeSession).toHaveBeenCalledWith(
'test-session-id',
'test-provider',
'test-model'
)
expect(mocks.compactSession).toHaveBeenCalledWith('test-session-id')
})
it('unknown command sends command', async () => {
mocks.sendCommand.mockResolvedValue({ info: { id: 'asm_1' }, parts: [] })
it('unknown command runs the V2 command with the parsed payload', async () => {
setSession(sessionInfo())
const { result } = renderHook(() => useCommandHandler(baseProps))
const unknownCommand = { name: 'myskill' as const }
const { result } = renderHandler()
const files = [{ uri: 'file:///test/dir/src/App.tsx', name: 'App.tsx', mention: { start: 0, end: 8, text: '@App.tsx' } }]
const agents = [{ name: 'reviewer', mention: { start: 9, end: 18, text: '@reviewer' } }]
const skills = [{ id: 'review' }]
await result.current.executeCommand(unknownCommand, '')
await result.current.executeCommand({ name: 'myskill' }, { text: '@App.tsx @reviewer', files, agents, skills })
expect(mocks.sendCommand).toHaveBeenCalledWith('test-session-id', {
command: 'myskill',
arguments: '',
agent: 'test-agent',
model: 'test-provider/test-model',
expect(mocks.runCommand).toHaveBeenCalledWith({
sessionID: 'test-session-id',
name: 'myskill',
text: '@App.tsx @reviewer',
files,
agents,
skills,
})
})
it('sessions command opens sessions dialog without sending command', async () => {
it('sessions command opens sessions dialog without running a command', async () => {
const onShowSessionsDialog = vi.fn()
const { result } = renderHook(() =>
useCommandHandler({ ...baseProps, onShowSessionsDialog })
)
const sessionsCommand = { name: 'sessions' as const }
const { result } = renderHandler({ onShowSessionsDialog })
const sessionsCommand = { name: 'sessions' }
await result.current.executeCommand(sessionsCommand, '')
await result.current.executeCommand(sessionsCommand, { text: '' })
expect(mocks.sendCommand).not.toHaveBeenCalled()
expect(mocks.runCommand).not.toHaveBeenCalled()
expect(onShowSessionsDialog).toHaveBeenCalled()
})
it('switches model and agent before running the command when the selection changed', async () => {
setSession(sessionInfo({ agent: 'build', model: { providerID: 'anthropic', id: 'claude-sonnet-4' } }))
const { result } = renderHandler({
model: { providerID: 'openai', id: 'gpt-4', variant: 'v1' },
currentAgent: 'plan',
})
await result.current.executeCommand({ name: 'myskill' }, { text: '' })
expect(mocks.switchSessionModel).toHaveBeenCalledWith('test-session-id', {
providerID: 'openai',
id: 'gpt-4',
variant: 'v1',
})
expect(mocks.switchSessionAgent).toHaveBeenCalledWith('test-session-id', 'plan')
expect(mocks.switchSessionModel.mock.invocationCallOrder[0]).toBeLessThan(
mocks.runCommand.mock.invocationCallOrder[0],
)
expect(mocks.switchSessionAgent.mock.invocationCallOrder[0]).toBeLessThan(
mocks.runCommand.mock.invocationCallOrder[0],
)
})
it('does not switch when the selection matches the session', async () => {
setSession(sessionInfo({ agent: 'build', model: { providerID: 'anthropic', id: 'claude-sonnet-4' } }))
const { result } = renderHandler({
model: { providerID: 'anthropic', id: 'claude-sonnet-4' },
currentAgent: 'build',
})
await result.current.executeCommand({ name: 'myskill' }, { text: '' })
expect(mocks.switchSessionModel).not.toHaveBeenCalled()
expect(mocks.switchSessionAgent).not.toHaveBeenCalled()
expect(mocks.runCommand).toHaveBeenCalled()
})
it('stops before running the command when the selection switch fails', async () => {
setSession(sessionInfo({ agent: 'build', model: { providerID: 'anthropic', id: 'claude-sonnet-4' } }))
mocks.switchSessionModel.mockRejectedValueOnce(new Error('switch failed'))
const { result } = renderHandler({
model: { providerID: 'openai', id: 'gpt-4' },
currentAgent: 'build',
})
const cleared = await result.current.executeCommand({ name: 'myskill' }, { text: '' })
expect(cleared).toBe(false)
expect(mocks.runCommand).not.toHaveBeenCalled()
})
it('undo invokes the revert handler instead of running a command', async () => {
setSession(sessionInfo())
const onUndo = vi.fn()
const { result } = renderHandler({ onUndo })
const cleared = await result.current.executeCommand({ name: 'undo' }, { text: '' })
expect(onUndo).toHaveBeenCalled()
expect(mocks.runCommand).not.toHaveBeenCalled()
expect(cleared).toBe(false)
})
it('redo invokes the revert handler instead of running a command', async () => {
setSession(sessionInfo())
const onRedo = vi.fn()
const { result } = renderHandler({ onRedo })
const cleared = await result.current.executeCommand({ name: 'redo' }, { text: '' })
expect(onRedo).toHaveBeenCalled()
expect(mocks.runCommand).not.toHaveBeenCalled()
expect(cleared).toBe(false)
})
})