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.
This commit is contained in:
Chris Scott committed 2026-09-24 08:38:26 -04:00
1 parent 6c2ebd764b
commit 7351d5f002
313 files changed
+23848 -36801

No files matched your search

@@ -0,0 +1,165 @@
import { describe, expect, it } from 'vitest'
import {
OPENCODE_MIN_VERSION,
OPENCODE_PINNED_VERSION,
OPENCODE_SERVER_USERNAME,
buildOpenCodeBasicAuth,
buildOpenCodeReleaseAsset,
createOpenCodeApi,
isSupportedOpenCodeVersion,
openCodeLocation,
} from '@opencode-manager/shared/opencode'
interface CapturedRequest {
url: string
method: string
headers: Record<string, string>
}
function createFetchSpy() {
const calls: CapturedRequest[] = []
const fetchSpy = (async (input: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => {
calls.push({
url: String(input),
method: init?.method ?? 'GET',
headers: Object.fromEntries(new Headers(init?.headers).entries()),
})
return new Response(JSON.stringify({ location: { directory: '/repo' }, data: [] }), {
status: 200,
headers: { 'content-type': 'application/json' },
})
}) as typeof fetch
return { calls, fetchSpy }
}
describe('OpenCode v2 contract', () => {
describe('version policy', () => {
it('pins the client version and the fixed V2 server username', () => {
expect(OPENCODE_MIN_VERSION).toBe('2.0.0')
expect(OPENCODE_PINNED_VERSION).toBe('2.0.15')
expect(OPENCODE_SERVER_USERNAME).toBe('opencode')
})
it('accepts v2 releases at or above the minimum and rejects other majors', () => {
expect(isSupportedOpenCodeVersion('1.18.32')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.0')).toBe(true)
expect(isSupportedOpenCodeVersion('2.0.15')).toBe(true)
expect(isSupportedOpenCodeVersion('2.1.0')).toBe(true)
expect(isSupportedOpenCodeVersion('v2.0.0')).toBe(true)
expect(isSupportedOpenCodeVersion('3.0.0')).toBe(false)
expect(isSupportedOpenCodeVersion('not-a-version')).toBe(false)
})
it('rejects prereleases of the minimum version', () => {
expect(isSupportedOpenCodeVersion('2.0.0-alpha.1')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.0-alpha')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.0-rc.1+build.5')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.0-0')).toBe(false)
})
it('accepts prereleases of higher numeric cores', () => {
expect(isSupportedOpenCodeVersion('2.1.0-beta.1')).toBe(true)
expect(isSupportedOpenCodeVersion('2.0.1-rc.2')).toBe(true)
})
it('ignores build metadata for precedence', () => {
expect(isSupportedOpenCodeVersion('2.0.0+build')).toBe(true)
expect(isSupportedOpenCodeVersion('2.0.0+2.0.0')).toBe(true)
})
it('rejects malformed and incomplete versions', () => {
expect(isSupportedOpenCodeVersion('2garbage')).toBe(false)
expect(isSupportedOpenCodeVersion('2')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.0.1')).toBe(false)
expect(isSupportedOpenCodeVersion('02.0.0')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.0-alpha..1')).toBe(false)
expect(isSupportedOpenCodeVersion('')).toBe(false)
})
})
describe('buildOpenCodeBasicAuth', () => {
it('encodes the fixed V2 username with the server password', () => {
expect(buildOpenCodeBasicAuth('pw')).toBe('Basic b3BlbmNvZGU6cHc=')
})
it('encodes non-ASCII passwords as UTF-8 bytes', () => {
expect(buildOpenCodeBasicAuth('pä')).toBe(
`Basic ${Buffer.from('opencode:pä', 'utf8').toString('base64')}`,
)
})
})
describe('openCodeLocation', () => {
it('wraps a directory into the V2 location input', () => {
expect(openCodeLocation('/repo')).toEqual({ location: { directory: '/repo' } })
})
})
describe('createOpenCodeApi', () => {
it('sends Basic auth and the location encoding of the official client', async () => {
const { calls, fetchSpy } = createFetchSpy()
const api = createOpenCodeApi({ baseUrl: 'http://127.0.0.1:1/prefix', password: 'pw', fetch: fetchSpy })
await api.agent.list(openCodeLocation('/repo'))
expect(calls).toEqual([
{
url: 'http://127.0.0.1:1/prefix/api/agent?location%5Bdirectory%5D=%2Frepo',
method: 'GET',
headers: { authorization: 'Basic b3BlbmNvZGU6cHc=' },
},
])
})
it('omits the auth header without a password and keeps caller headers', async () => {
const { calls, fetchSpy } = createFetchSpy()
const api = createOpenCodeApi({
baseUrl: 'http://127.0.0.1:1/prefix',
fetch: fetchSpy,
headers: { 'x-manager-request': 'abc' },
})
await api.agent.list()
expect(calls).toHaveLength(1)
expect(calls[0]?.url).toBe('http://127.0.0.1:1/prefix/api/agent')
expect(calls[0]?.headers).toEqual({ 'x-manager-request': 'abc' })
})
})
describe('buildOpenCodeReleaseAsset', () => {
it('builds tar.gz assets for linux x64, arm64 and musl', () => {
expect(buildOpenCodeReleaseAsset('2.0.15', { platform: 'linux', arch: 'x86_64', musl: false })).toEqual({
url: 'https://opencode.ai/files/bin/2.0.15/opencode-linux-x64.tar.gz',
archive: 'tar.gz',
})
expect(buildOpenCodeReleaseAsset('2.0.15', { platform: 'linux', arch: 'amd64', musl: false }).url).toBe(
'https://opencode.ai/files/bin/2.0.15/opencode-linux-x64.tar.gz',
)
expect(buildOpenCodeReleaseAsset('2.0.15', { platform: 'linux', arch: 'aarch64', musl: false }).url).toBe(
'https://opencode.ai/files/bin/2.0.15/opencode-linux-arm64.tar.gz',
)
expect(buildOpenCodeReleaseAsset('2.0.15', { platform: 'linux', arch: 'arm64', musl: false }).url).toBe(
'https://opencode.ai/files/bin/2.0.15/opencode-linux-arm64.tar.gz',
)
expect(buildOpenCodeReleaseAsset('2.0.15', { platform: 'linux', arch: 'x64', musl: true })).toEqual({
url: 'https://opencode.ai/files/bin/2.0.15/opencode-linux-x64-musl.tar.gz',
archive: 'tar.gz',
})
})
it('builds zip assets for darwin arm64', () => {
expect(buildOpenCodeReleaseAsset('2.0.15', { platform: 'darwin', arch: 'arm64', musl: false })).toEqual({
url: 'https://opencode.ai/files/bin/2.0.15/opencode-darwin-arm64.zip',
archive: 'zip',
})
})
it('rejects unsupported platforms and architectures', () => {
expect(() => buildOpenCodeReleaseAsset('2.0.15', { platform: 'win32', arch: 'x64', musl: false })).toThrow()
expect(() => buildOpenCodeReleaseAsset('2.0.15', { platform: 'linux', arch: 'ia32', musl: false })).toThrow()
})
})
})
+254
View File
@@ -0,0 +1,254 @@
import { describe, expect, it } from 'vitest'
import {
MCP_OAUTH_CALLBACK_PATH,
fromV2McpServerConfig,
mcpServerConfigFromConfig,
mcpServersFromConfig,
mcpStatusByName,
toV2McpServerConfig,
type McpServer,
} from '@opencode-manager/shared/opencode'
describe('toV2McpServerConfig', () => {
it('converts a local server with environment and timeout', () => {
expect(
toV2McpServerConfig({
type: 'local',
enabled: true,
command: ['npx', 'server-filesystem', '/tmp'],
environment: { API_KEY: 'secret' },
timeout: 5000,
}),
).toEqual({
type: 'local',
command: ['npx', 'server-filesystem', '/tmp'],
environment: { API_KEY: 'secret' },
disabled: false,
timeout: { catalog: 5000, execution: 5000 },
})
})
it('omits disabled when enabled is undefined', () => {
expect(toV2McpServerConfig({ type: 'local', command: ['npx', 'server'] })).toEqual({
type: 'local',
command: ['npx', 'server'],
})
})
it('inverts enabled into disabled', () => {
expect(toV2McpServerConfig({ type: 'local', command: ['npx', 'server'], enabled: false })).toEqual({
type: 'local',
command: ['npx', 'server'],
disabled: true,
})
})
it('keeps a remote server without OAuth unchanged', () => {
expect(
toV2McpServerConfig({
type: 'remote',
url: 'https://mcp.example.com',
headers: { Authorization: 'Bearer token' },
}),
).toEqual({
type: 'remote',
url: 'https://mcp.example.com',
headers: { Authorization: 'Bearer token' },
})
})
it('writes the Manager callback for OAuth servers', () => {
expect(
toV2McpServerConfig({ type: 'remote', url: 'https://mcp.example.com', oauth: true }, 'http://localhost:5003'),
).toEqual({
type: 'remote',
url: 'https://mcp.example.com',
oauth: {
redirect_uri: `http://localhost:5003${MCP_OAUTH_CALLBACK_PATH}`,
},
})
})
it('converts OAuth client fields and keeps the Manager callback', () => {
expect(
toV2McpServerConfig(
{
type: 'remote',
url: 'https://mcp.example.com',
oauth: { clientId: 'client-1', clientSecret: 'secret-1', scope: 'read write' },
},
'https://manager.example.com',
),
).toEqual({
type: 'remote',
url: 'https://mcp.example.com',
oauth: {
client_id: 'client-1',
client_secret: 'secret-1',
scope: 'read write',
redirect_uri: `https://manager.example.com${MCP_OAUTH_CALLBACK_PATH}`,
},
})
})
it('keeps OAuth disabled as false', () => {
expect(
toV2McpServerConfig({ type: 'remote', url: 'https://mcp.example.com', oauth: false }, 'http://localhost:5003'),
).toEqual({
type: 'remote',
url: 'https://mcp.example.com',
oauth: false,
})
})
it('omits the callback when no origin is supplied', () => {
expect(toV2McpServerConfig({ type: 'remote', url: 'https://mcp.example.com', oauth: true })).toEqual({
type: 'remote',
url: 'https://mcp.example.com',
oauth: {},
})
})
})
describe('fromV2McpServerConfig', () => {
it('converts a native local server back to the form model', () => {
expect(
fromV2McpServerConfig({
type: 'local',
command: ['npx', 'server'],
environment: { API_KEY: 'secret' },
disabled: false,
timeout: { catalog: 5000, execution: 9000 },
}),
).toEqual({
type: 'local',
enabled: true,
command: ['npx', 'server'],
environment: { API_KEY: 'secret' },
timeout: 5000,
})
})
it('inverts disabled into enabled', () => {
expect(fromV2McpServerConfig({ type: 'local', command: ['npx', 'server'], disabled: true })).toEqual({
type: 'local',
enabled: false,
command: ['npx', 'server'],
})
})
it('omits enabled when the native config omits disabled', () => {
expect(fromV2McpServerConfig({ type: 'local', command: ['npx', 'server'] })).toEqual({
type: 'local',
command: ['npx', 'server'],
})
})
it('converts a native remote OAuth server back to the form model', () => {
expect(
fromV2McpServerConfig({
type: 'remote',
url: 'https://mcp.example.com',
oauth: { client_id: 'client-1', client_secret: 'secret-1', scope: 'read' },
}),
).toEqual({
type: 'remote',
url: 'https://mcp.example.com',
oauth: { clientId: 'client-1', clientSecret: 'secret-1', scope: 'read' },
})
})
it('keeps OAuth disabled as false', () => {
expect(fromV2McpServerConfig({ type: 'remote', url: 'https://mcp.example.com', oauth: false })).toEqual({
type: 'remote',
url: 'https://mcp.example.com',
oauth: false,
})
})
it('falls back to the execution timeout when catalog is absent', () => {
expect(
fromV2McpServerConfig({ type: 'remote', url: 'https://mcp.example.com', timeout: { execution: 9000 } }),
).toEqual({
type: 'remote',
url: 'https://mcp.example.com',
timeout: 9000,
})
})
})
describe('mcpServersFromConfig', () => {
it('reads native servers and keeps legacy entries', () => {
expect(
mcpServersFromConfig({
servers: {
native: { type: 'remote', url: 'https://native.example.com', disabled: true },
},
legacy: { type: 'local', command: ['npx', 'legacy'], enabled: false },
}),
).toEqual({
native: { type: 'remote', url: 'https://native.example.com', enabled: false },
legacy: { type: 'local', command: ['npx', 'legacy'], enabled: false },
})
})
it('ignores timeout entries and non-server values', () => {
expect(mcpServersFromConfig({ timeout: { catalog: 5000 }, broken: 'nope' })).toEqual({})
})
it('returns an empty map for a missing config', () => {
expect(mcpServersFromConfig(undefined)).toEqual({})
})
})
describe('mcpServerConfigFromConfig', () => {
it('returns the native entry as-is so unknown fields survive', () => {
const native = {
type: 'remote',
url: 'https://native.example.com',
oauth: { redirect_uri: 'https://manager.example.com/api/mcp-oauth-proxy/callback' },
protocol: 'auto',
}
expect(mcpServerConfigFromConfig({ servers: { native } }, 'native')).toEqual(native)
})
it('converts a legacy entry to the native shape', () => {
expect(
mcpServerConfigFromConfig({ legacy: { type: 'local', command: ['npx', 'legacy'], timeout: 5000 } }, 'legacy'),
).toEqual({
type: 'local',
command: ['npx', 'legacy'],
timeout: { catalog: 5000, execution: 5000 },
})
})
it('returns undefined for unknown servers and missing config', () => {
expect(mcpServerConfigFromConfig({ servers: {} }, 'missing')).toBeUndefined()
expect(mcpServerConfigFromConfig(undefined, 'missing')).toBeUndefined()
})
})
describe('mcpStatusByName', () => {
it('maps every V2 status onto the Manager status map', () => {
const servers: McpServer[] = [
{ name: 'connected-server', status: { status: 'connected' }, integrationID: 'int-1' },
{ name: 'pending-server', status: { status: 'pending' } },
{ name: 'disabled-server', status: { status: 'disabled' } },
{ name: 'failed-server', status: { status: 'failed', error: 'connect failed' } },
{ name: 'auth-server', status: { status: 'needs_auth', error: 'login required' } },
]
expect(mcpStatusByName(servers)).toEqual({
'connected-server': { status: 'connected', integrationID: 'int-1' },
'pending-server': { status: 'pending' },
'disabled-server': { status: 'disabled' },
'failed-server': { status: 'failed', error: 'connect failed' },
'auth-server': { status: 'needs_auth', error: 'login required' },
})
})
it('returns an empty map for no servers', () => {
expect(mcpStatusByName([])).toEqual({})
})
})