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

@@ -0,0 +1,249 @@
import { readdirSync, readFileSync } from 'fs'
import { join } from 'path'
import { describe, expect, it } from 'vitest'
import {
OPENCODE_PINNED_VERSION,
OPENCODE_SERVER_USERNAME,
OPENCODE_SUPPORTED_VERSION_RANGE,
buildOpenCodeBasicAuth,
buildOpenCodeReleaseAsset,
compareOpenCodeVersions,
createOpenCodeApi,
describeUnsupportedOpenCodeVersion,
isStableOpenCodeVersion,
isSupportedOpenCodeVersion,
normalizeOpenCodeVersion,
openCodeLocation,
parseOpenCodeVersion,
parseOpenCodeVersionOutput,
} from '@opencode-manager/shared/opencode'
const REPO_ROOT = join(__dirname, '..', '..', '..')
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, derives the floor from it and fixes the V2 server username', () => {
expect(OPENCODE_PINNED_VERSION).toBe('2.0.15')
expect(OPENCODE_SUPPORTED_VERSION_RANGE).toBe('>=2.0.15 <3.0.0')
expect(OPENCODE_SERVER_USERNAME).toBe('opencode')
})
it('accepts releases of the pinned major at or above the pin and rejects everything else', () => {
expect(isSupportedOpenCodeVersion('1.18.32')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.0')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.14')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.15')).toBe(true)
expect(isSupportedOpenCodeVersion('2.0.16')).toBe(true)
expect(isSupportedOpenCodeVersion('2.1.0')).toBe(true)
expect(isSupportedOpenCodeVersion('v2.0.15')).toBe(true)
expect(isSupportedOpenCodeVersion(' v2.0.15 ')).toBe(true)
expect(isSupportedOpenCodeVersion('3.0.0')).toBe(false)
expect(isSupportedOpenCodeVersion('3.1.0')).toBe(false)
expect(isSupportedOpenCodeVersion('not-a-version')).toBe(false)
})
it('rejects prereleases of the pinned version', () => {
expect(isSupportedOpenCodeVersion('2.0.15-alpha.1')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.15-alpha')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.15-rc.1+build.5')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.15-0')).toBe(false)
})
it('accepts prereleases of higher numeric cores within the pinned major', () => {
expect(isSupportedOpenCodeVersion('2.1.0-beta.1')).toBe(true)
expect(isSupportedOpenCodeVersion('2.0.16-rc.2')).toBe(true)
expect(isSupportedOpenCodeVersion('3.0.0-beta.1')).toBe(false)
})
it('ignores build metadata for precedence', () => {
expect(isSupportedOpenCodeVersion('2.0.15+build')).toBe(true)
expect(isSupportedOpenCodeVersion('2.0.15+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.15.1')).toBe(false)
expect(isSupportedOpenCodeVersion('02.0.15')).toBe(false)
expect(isSupportedOpenCodeVersion('2.0.15-alpha..1')).toBe(false)
expect(isSupportedOpenCodeVersion('')).toBe(false)
})
it('describes the supported range for unsupported versions', () => {
expect(describeUnsupportedOpenCodeVersion('3.0.0')).toBe(
'OpenCode 3.0.0 is not supported; OpenCode Manager requires OpenCode >=2.0.15 <3.0.0',
)
})
})
describe('version parsing', () => {
it('normalizes surrounding whitespace and a leading v', () => {
expect(normalizeOpenCodeVersion(' v2.0.15\n')).toBe('2.0.15')
expect(normalizeOpenCodeVersion('2.0.15')).toBe('2.0.15')
})
it('parses version components and prerelease identifiers', () => {
expect(parseOpenCodeVersion('v2.1.3-beta.4')).toEqual({ major: 2, minor: 1, patch: 3, prerelease: ['beta', 4] })
expect(parseOpenCodeVersion('2.1')).toBeNull()
})
it('identifies stable MAJOR.MINOR.PATCH versions', () => {
expect(isStableOpenCodeVersion('v2.0.15')).toBe(true)
expect(isStableOpenCodeVersion('2.1.0-beta.1')).toBe(false)
expect(isStableOpenCodeVersion('2.0.15; cat /etc/passwd')).toBe(false)
})
it('orders versions by semver precedence', () => {
expect(compareOpenCodeVersions('2.0.15', '2.0.14')).toBeGreaterThan(0)
expect(compareOpenCodeVersions('v2.0.15', '2.0.15')).toBe(0)
expect(compareOpenCodeVersions('2.0.9', '2.0.10')).toBeLessThan(0)
expect(compareOpenCodeVersions('2.1.0-beta.1', '2.1.0')).toBeLessThan(0)
expect(() => compareOpenCodeVersions('invalid', '2.0.15')).toThrow(/invalid/)
})
it('extracts the version from opencode --version output', () => {
expect(parseOpenCodeVersionOutput('2.0.15\n')).toBe('2.0.15')
expect(parseOpenCodeVersionOutput('opencode v2.0.15\n')).toBe('2.0.15')
expect(parseOpenCodeVersionOutput('opencode 2.1.0-beta.1 (linux-x64)')).toBe('2.1.0-beta.1')
expect(parseOpenCodeVersionOutput('version 2.0.15.')).toBe('2.0.15')
expect(parseOpenCodeVersionOutput('warning: something\nopencode 2.0.15')).toBe('2.0.15')
expect(parseOpenCodeVersionOutput('no version here')).toBeNull()
expect(parseOpenCodeVersionOutput('')).toBeNull()
})
})
describe('pinned version sources', () => {
it('keeps the Dockerfile OPENCODE_VERSION default in sync with the pin', () => {
const dockerfile = readFileSync(join(REPO_ROOT, 'Dockerfile'), 'utf8')
expect(dockerfile).toMatch(new RegExp(`^ARG OPENCODE_VERSION=${OPENCODE_PINNED_VERSION.replace(/\./g, '\\.')}$`, 'm'))
expect(dockerfile).toMatch(/^ENV OPENCODE_BUNDLED_VERSION=\$\{OPENCODE_VERSION\}$/m)
})
it('makes the shell scripts derive the pin from the Dockerfile or the bundled version instead of hardcoding it', () => {
const entrypoint = readFileSync(join(REPO_ROOT, 'scripts', 'docker-entrypoint.sh'), 'utf8')
const setupDev = readFileSync(join(REPO_ROOT, 'scripts', 'setup-dev.sh'), 'utf8')
const releaseHelper = readFileSync(join(REPO_ROOT, 'scripts', 'lib', 'opencode-release.sh'), 'utf8')
expect(entrypoint).toContain('OPENCODE_SUPPORTED_FLOOR="${OPENCODE_BUNDLED_VERSION:-}"')
expect(setupDev).toContain("sed -n 's/^ARG OPENCODE_VERSION=//p' \"$REPO_ROOT/Dockerfile\"")
for (const script of [entrypoint, setupDev, releaseHelper]) {
expect(script).not.toMatch(/(?<![\d.])\d+\.\d+\.\d+(?![\d.])/)
}
})
it('never sets or passes OPENCODE_VERSION from a GitHub workflow', () => {
const workflowsDir = join(REPO_ROOT, '.github', 'workflows')
for (const entry of readdirSync(workflowsDir, { withFileTypes: true })) {
if (!entry.isFile()) continue
expect(readFileSync(join(workflowsDir, entry.name), 'utf8')).not.toContain('OPENCODE_VERSION')
}
})
})
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()
})
})
})
+72
View File
@@ -0,0 +1,72 @@
import { describe, expect, it } from 'vitest'
import {
MCP_OAUTH_CALLBACK_PATH,
mcpOAuthRedirectUri,
mcpServersFromConfig,
mcpStatusByName,
} from '@opencode-manager/shared/opencode'
import type { McpServer } from '@opencode-manager/shared/opencode'
describe('mcpOAuthRedirectUri', () => {
it('builds the Manager callback on the given origin', () => {
expect(mcpOAuthRedirectUri('https://manager.example.com')).toBe(
`https://manager.example.com${MCP_OAUTH_CALLBACK_PATH}`,
)
})
})
describe('mcpServersFromConfig', () => {
it('returns native servers as-is so every V2 field survives', () => {
const remote = {
type: 'remote',
url: 'https://native.example.com',
oauth: { client_id: 'client', redirect_uri: `https://manager.example.com${MCP_OAUTH_CALLBACK_PATH}` },
disabled: true,
codemode: true,
protocol: 'auto',
timeout: { startup: 1000, catalog: 2000, execution: 3000 },
}
const local = { type: 'local', command: ['npx', 'server'], cwd: '/tmp', environment: { KEY: 'value' } }
expect(mcpServersFromConfig({ servers: { remote, local } })).toEqual({ remote, local })
})
it('ignores legacy flat entries, the timeout section, and invalid servers', () => {
expect(
mcpServersFromConfig({
timeout: { catalog: 5000 },
legacy: { type: 'local', command: ['npx', 'legacy'], enabled: false },
servers: { broken: 'nope', untyped: { url: 'https://example.com' } },
}),
).toEqual({})
})
it('returns an empty map for a missing config', () => {
expect(mcpServersFromConfig(undefined)).toEqual({})
expect(mcpServersFromConfig({ servers: [] })).toEqual({})
})
})
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({})
})
})
@@ -0,0 +1,60 @@
import { describe, expect, it } from 'vitest'
import { formatOpenCodeModelRef, parseOpenCodeModelRef } from '@opencode-manager/shared/opencode'
describe('OpenCode model references', () => {
describe('parseOpenCodeModelRef', () => {
it('parses a provider and model id', () => {
expect(parseOpenCodeModelRef('anthropic/claude-sonnet-4')).toEqual({
providerID: 'anthropic',
id: 'claude-sonnet-4',
})
})
it('parses the variant after the hash', () => {
expect(parseOpenCodeModelRef('openai/gpt-5#high')).toEqual({
providerID: 'openai',
id: 'gpt-5',
variant: 'high',
})
})
it('splits at the first slash so model ids may contain slashes', () => {
expect(parseOpenCodeModelRef('openrouter/anthropic/claude-sonnet-4')).toEqual({
providerID: 'openrouter',
id: 'anthropic/claude-sonnet-4',
})
})
it('rejects empty and malformed references', () => {
expect(parseOpenCodeModelRef('')).toBeUndefined()
expect(parseOpenCodeModelRef('openai')).toBeUndefined()
expect(parseOpenCodeModelRef('/gpt-5')).toBeUndefined()
expect(parseOpenCodeModelRef('openai/')).toBeUndefined()
expect(parseOpenCodeModelRef('openai/gpt-5#')).toBeUndefined()
expect(parseOpenCodeModelRef('openai/gpt-5#high#low')).toBeUndefined()
expect(parseOpenCodeModelRef('open#ai/gpt-5')).toBeUndefined()
})
})
describe('formatOpenCodeModelRef', () => {
it('formats a provider and model id', () => {
expect(formatOpenCodeModelRef({ providerID: 'anthropic', id: 'claude-sonnet-4' })).toBe(
'anthropic/claude-sonnet-4',
)
})
it('appends the variant when present', () => {
expect(formatOpenCodeModelRef({ providerID: 'openai', id: 'gpt-5', variant: 'high' })).toBe(
'openai/gpt-5#high',
)
})
it('round-trips a parsed reference', () => {
const model = 'openrouter/anthropic/claude-sonnet-4#beta'
const ref = parseOpenCodeModelRef(model)
expect(ref).toBeDefined()
expect(formatOpenCodeModelRef(ref!)).toBe(model)
})
})
})
+40
View File
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest'
import { compareSemver, normalizeSemver, parseSemver } from '@opencode-manager/shared/utils'
describe('semver', () => {
describe('normalizeSemver', () => {
it('trims surrounding whitespace and a leading v', () => {
expect(normalizeSemver(' v2.0.15\n')).toBe('2.0.15')
expect(normalizeSemver('2.0.15')).toBe('2.0.15')
})
})
describe('parseSemver', () => {
it('parses version components and prerelease identifiers', () => {
expect(parseSemver('v2.1.3-beta.4')).toEqual({ major: 2, minor: 1, patch: 3, prerelease: ['beta', 4] })
expect(parseSemver('2.0.15+build.5')).toEqual({ major: 2, minor: 0, patch: 15, prerelease: [] })
})
it('rejects incomplete and malformed versions', () => {
expect(parseSemver('2.1')).toBeNull()
expect(parseSemver('2garbage')).toBeNull()
expect(parseSemver('02.0.15')).toBeNull()
expect(parseSemver('')).toBeNull()
})
})
describe('compareSemver', () => {
it('orders versions by semver precedence', () => {
expect(compareSemver('2.0.15', '2.0.14')).toBeGreaterThan(0)
expect(compareSemver('v2.0.15', '2.0.15')).toBe(0)
expect(compareSemver('2.0.9', '2.0.10')).toBeLessThan(0)
expect(compareSemver('2.1.0-beta.1', '2.1.0')).toBeLessThan(0)
expect(compareSemver('1.0.0-alpha', '1.0.0-alpha.1')).toBeLessThan(0)
})
it('throws on invalid versions', () => {
expect(() => compareSemver('invalid', '2.0.15')).toThrow(/invalid/)
expect(() => compareSemver('2.0.15', 'not-a-version')).toThrow(/invalid/)
})
})
})