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

+11 -25
View File
@@ -6,6 +6,7 @@ export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$HOME/.opencode/bin:/usr/local/bin:$PATH"
source /usr/local/lib/ocm/container-user.sh
source /usr/local/lib/ocm/opencode-release.sh
grant_kvm_access() {
local dev="${1:-/dev/kvm}"
@@ -42,11 +43,7 @@ grant_kvm_access() {
echo "Granted node access to $dev (group '$group_name', gid $dev_gid)"
}
MIN_OPENCODE_VERSION="1.0.137"
version_gte() {
printf '%s\n%s\n' "$2" "$1" | sort -V -C
}
OPENCODE_SUPPORTED_FLOOR="${OPENCODE_BUNDLED_VERSION:-}"
read_opencode_version() {
local binary
@@ -63,24 +60,8 @@ install_opencode() {
echo "ERROR: OPENCODE_BUNDLED_VERSION is not set; refusing to guess the pinned OpenCode build" >&2
return 1
fi
if [[ ! "$opencode_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "ERROR: OPENCODE_BUNDLED_VERSION='$opencode_version' is not an X.Y.Z version; refusing to download it" >&2
return 1
fi
if ! version_gte "$opencode_version" "$MIN_OPENCODE_VERSION"; then
echo "ERROR: OPENCODE_BUNDLED_VERSION=$opencode_version is below the minimum supported $MIN_OPENCODE_VERSION; refusing to download it" >&2
return 1
fi
echo "Installing OpenCode ${opencode_version}..."
local staging
staging="$(mktemp -d)"
curl -fsSL "https://github.com/anomalyco/opencode/releases/download/v${opencode_version}/opencode-linux-$(uname -m | sed 's/x86_64/x64/; s/aarch64/arm64/').tar.gz" \
-o "$staging/opencode.tar.gz"
tar -xzf "$staging/opencode.tar.gz" -C "$staging"
mkdir -p "$HOME/.opencode/bin"
mv "$staging/opencode" "$HOME/.opencode/bin/opencode"
chmod 755 "$HOME/.opencode/bin/opencode"
rm -rf "$staging"
download_opencode_to "$opencode_version" "$HOME/.opencode/bin/opencode"
}
reconcile_persisted_opencode() {
@@ -93,6 +74,11 @@ reconcile_persisted_opencode() {
rm -f "$persisted_path"
return 0
fi
if ! is_supported_opencode_version "$persisted_version"; then
echo "Persisted OpenCode $persisted_version is outside the supported range $(supported_opencode_range); removing it so the bundled OpenCode $OPENCODE_SUPPORTED_FLOOR binary is used"
rm -f "$persisted_path"
return 0
fi
echo "Persisted OpenCode $persisted_version is usable; retaining it"
}
@@ -133,10 +119,10 @@ OPENCODE_VERSION="$(read_opencode_version)"
echo "OpenCode is installed (version: $OPENCODE_VERSION)"
if [ "$OPENCODE_VERSION" != "unknown" ]; then
if version_gte "$OPENCODE_VERSION" "$MIN_OPENCODE_VERSION"; then
echo "OpenCode version meets minimum requirement (>=$MIN_OPENCODE_VERSION)"
if is_supported_opencode_version "$OPENCODE_VERSION"; then
echo "OpenCode version is within the supported range $(supported_opencode_range)"
else
echo "OpenCode version $OPENCODE_VERSION is below minimum required version $MIN_OPENCODE_VERSION"
echo "OpenCode version $OPENCODE_VERSION is outside the supported range $(supported_opencode_range)"
echo "Reinstalling bundled OpenCode version ${OPENCODE_BUNDLED_VERSION}..."
install_opencode
-73
View File
@@ -1,73 +0,0 @@
#!/usr/bin/env bun
import { writeFile } from 'fs/promises'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const API_URL = process.env.VITE_API_URL || 'http://localhost:8001'
const OPENCODE_URL = API_URL + '/api/opencode'
async function generateOpenAPISpec() {
console.log(`📥 Fetching OpenAPI spec from ${OPENCODE_URL}/doc`)
try {
const response = await fetch(`${OPENCODE_URL }/doc`, {
headers: {
'Accept': 'application/json'
}
})
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
}
const spec = await response.json()
const outputPath = join(__dirname, '../frontend/src/api/opencode-spec.json')
await writeFile(outputPath, JSON.stringify(spec, null, 2))
console.log(`✅ OpenAPI spec saved to ${outputPath}`)
return outputPath
} catch (error) {
console.error('❌ Failed to fetch OpenAPI spec:', error)
console.error('\n💡 Make sure an OpenCode server is running.')
console.error(' You can start one by opening a repo in the WebUI.')
process.exit(1)
}
}
async function generateTypeScript(specPath: string) {
console.log('\n📝 Generating TypeScript types...')
const outputPath = join(__dirname, '../frontend/src/api/opencode-types.ts')
const { spawnSync } = await import('child_process')
try {
const result = spawnSync('npx', ['openapi-typescript', specPath, '-o', outputPath], {
cwd: join(__dirname, '..'),
stdio: 'inherit'
})
if (result.status !== 0) {
throw new Error(`openapi-typescript exited with code ${result.status}`)
}
console.log(`✅ TypeScript types generated at ${outputPath}`)
} catch (error) {
console.error('❌ Failed to generate TypeScript types:', error)
process.exit(1)
}
}
async function main() {
console.log('🚀 Generating OpenCode API types...\n')
const specPath = await generateOpenAPISpec()
await generateTypeScript(specPath)
console.log('\n✨ Done!')
}
main()
+64
View File
@@ -0,0 +1,64 @@
#!/bin/bash
opencode_arch_suffix() {
local arch
arch="$(uname -m | sed 's/x86_64/x64/; s/aarch64/arm64/')"
if ls /lib/ld-musl-* >/dev/null 2>&1; then
arch="${arch}-musl"
fi
printf '%s\n' "$arch"
}
opencode_download_url() {
local version="$1" suffix="${2:-}"
[ -n "$suffix" ] || suffix="$(opencode_arch_suffix)"
printf 'https://opencode.ai/files/bin/%s/opencode-linux-%s.tar.gz\n' "$version" "$suffix"
}
is_stable_opencode_version() {
printf '%s\n' "$1" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'
}
version_gte() {
printf '%s\n%s\n' "$2" "$1" | sort -V -C
}
is_supported_opencode_version() {
local version="$1"
is_stable_opencode_version "$version" || return 1
[ -n "${OPENCODE_SUPPORTED_FLOOR:-}" ] || return 1
[ "${version%%.*}" = "${OPENCODE_SUPPORTED_FLOOR%%.*}" ] || return 1
version_gte "$version" "$OPENCODE_SUPPORTED_FLOOR"
}
supported_opencode_range() {
[ -n "${OPENCODE_SUPPORTED_FLOOR:-}" ] || return 1
printf '>=%s <%s.0.0\n' "$OPENCODE_SUPPORTED_FLOOR" "$(( ${OPENCODE_SUPPORTED_FLOOR%%.*} + 1 ))"
}
download_opencode_to() {
local version="$1" destination="$2" staging
if [ -z "$version" ]; then
echo "ERROR: no OpenCode version provided; refusing to guess the pinned build" >&2
return 1
fi
if ! is_stable_opencode_version "$version"; then
echo "ERROR: OpenCode version '$version' is not an X.Y.Z version; refusing to download it" >&2
return 1
fi
staging="$(mktemp -d)"
if ! curl -fsSL "$(opencode_download_url "$version")" -o "$staging/opencode.tar.gz"; then
echo "ERROR: failed to download OpenCode ${version}" >&2
rm -rf "$staging"
return 1
fi
if ! tar -xzf "$staging/opencode.tar.gz" -C "$staging"; then
echo "ERROR: failed to extract OpenCode ${version}" >&2
rm -rf "$staging"
return 1
fi
mkdir -p "$(dirname "$destination")"
mv "$staging/opencode" "$destination"
chmod 755 "$destination"
rm -rf "$staging"
}
Executable → Regular
+12 -1
View File
@@ -32,7 +32,18 @@ echo "✅ Git is installed"
# Check if OpenCode TUI is installed
if ! opencode --version &> /dev/null; then
echo "❌ OpenCode TUI is not installed. Please install it with:"
echo " curl -fsSL https://opencode.ai/install | bash"
echo " curl -fsSL https://opencode.ai/v2/install | bash"
exit 1
fi
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
source "$REPO_ROOT/scripts/lib/opencode-release.sh"
OPENCODE_SUPPORTED_FLOOR="$(sed -n 's/^ARG OPENCODE_VERSION=//p' "$REPO_ROOT/Dockerfile" | head -1)"
OPENCODE_VERSION="$(opencode --version 2>&1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)"
if [ -z "$OPENCODE_SUPPORTED_FLOOR" ] || ! is_supported_opencode_version "$OPENCODE_VERSION"; then
echo "❌ OpenCode ${OPENCODE_VERSION:-unknown} is not supported; OpenCode $(supported_opencode_range) is required. Please install it with:"
echo " curl -fsSL https://opencode.ai/v2/install | bash"
exit 1
fi