mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
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:
1 parent
6c2ebd764b
commit
dacde879d6
383 files changed
+29905
-46566
No files matched your search
@@ -1,21 +1,23 @@
|
||||
import { sandboxPlanTimeoutMs, SANDBOX_UNAVAILABLE_PREFIX } from './sandbox/command'
|
||||
import {
|
||||
SANDBOX_FORWARDED_ENV_NAMES,
|
||||
SANDBOX_SHELL_ENV_HOST_SHELL,
|
||||
SANDBOX_SHELL_ENV_WORKDIR,
|
||||
} from './sandbox/shell-shim'
|
||||
|
||||
export const SANDBOX_PLAN_TIMEOUT_MS = sandboxPlanTimeoutMs()
|
||||
|
||||
export function buildSandboxPluginSource(shellShimPath: string): string {
|
||||
export function buildSandboxPluginSource(id: string, shellShimPath: string, serviceSettingsPath: string): string {
|
||||
return `import { existsSync } from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
var SANDBOX_UNAVAILABLE_PREFIX = ${JSON.stringify(SANDBOX_UNAVAILABLE_PREFIX)}
|
||||
var PLAN_TIMEOUT_MS = ${SANDBOX_PLAN_TIMEOUT_MS}
|
||||
var SHELL_SHIM_PATH = ${JSON.stringify(shellShimPath)}
|
||||
var ENV_WORKDIR = ${JSON.stringify(SANDBOX_SHELL_ENV_WORKDIR)}
|
||||
var ENV_HOST_SHELL = ${JSON.stringify(SANDBOX_SHELL_ENV_HOST_SHELL)}
|
||||
var FORWARDED_ENV_NAMES = ${JSON.stringify(SANDBOX_FORWARDED_ENV_NAMES)}
|
||||
var SERVICE_SETTINGS_PATH = path.resolve(${JSON.stringify(serviceSettingsPath)})
|
||||
var SERVICE_SETTINGS_DIRECTORY = path.dirname(SERVICE_SETTINGS_PATH)
|
||||
var SERVICE_SETTINGS_DENIED = 'OpenCode Manager denies access to its service settings file (service.json), which holds the OpenCode server password.'
|
||||
|
||||
function isEnforced() {
|
||||
return process.env.OCM_SANDBOX_ENFORCED === 'true'
|
||||
@@ -25,6 +27,59 @@ function unavailable(reason) {
|
||||
return new Error(SANDBOX_UNAVAILABLE_PREFIX + reason)
|
||||
}
|
||||
|
||||
function isWithin(directory, target) {
|
||||
var relative = path.relative(directory, target)
|
||||
return relative === '' || (relative !== '..' && !relative.startsWith('..' + path.sep) && !path.isAbsolute(relative))
|
||||
}
|
||||
|
||||
function globDirectory(resource) {
|
||||
var trimmed = resource
|
||||
while (trimmed.length > 0 && trimmed.charAt(trimmed.length - 1) === '*') {
|
||||
trimmed = trimmed.slice(0, -1)
|
||||
}
|
||||
while (trimmed.length > 1 && (trimmed.charAt(trimmed.length - 1) === '/' || trimmed.charAt(trimmed.length - 1) === path.sep)) {
|
||||
trimmed = trimmed.slice(0, -1)
|
||||
}
|
||||
return trimmed.length === 0 ? path.parse(SERVICE_SETTINGS_PATH).root : trimmed
|
||||
}
|
||||
|
||||
function resourceResolvesToServiceSettings(resource) {
|
||||
return typeof resource === 'string' && resource.length > 0 && path.resolve(resource) === SERVICE_SETTINGS_PATH
|
||||
}
|
||||
|
||||
function externalDirectoryTargetsServiceSettings(resource) {
|
||||
if (typeof resource !== 'string' || resource.length === 0) return false
|
||||
return isWithin(path.resolve(globDirectory(resource)), SERVICE_SETTINGS_DIRECTORY)
|
||||
}
|
||||
|
||||
function absolutePathTargetsServiceSettings(value) {
|
||||
if (typeof value !== 'string' || value.length === 0 || !path.isAbsolute(value)) return false
|
||||
var resolved = path.resolve(value)
|
||||
return resolved === SERVICE_SETTINGS_PATH || isWithin(resolved, SERVICE_SETTINGS_DIRECTORY)
|
||||
}
|
||||
|
||||
function metadataTargetsServiceSettings(metadata) {
|
||||
if (metadata === null || typeof metadata !== 'object') return false
|
||||
return absolutePathTargetsServiceSettings(metadata.path)
|
||||
|| (metadata.root !== metadata.path && absolutePathTargetsServiceSettings(metadata.root))
|
||||
}
|
||||
|
||||
function deniesServiceSettings(event) {
|
||||
if (event.action === 'grep' || event.action === 'glob') {
|
||||
return metadataTargetsServiceSettings(event.metadata)
|
||||
}
|
||||
if (!Array.isArray(event.resources)) return false
|
||||
for (var index = 0; index < event.resources.length; index += 1) {
|
||||
var resource = event.resources[index]
|
||||
if (event.action === 'external_directory') {
|
||||
if (externalDirectoryTargetsServiceSettings(resource)) return true
|
||||
} else if (resourceResolvesToServiceSettings(resource)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function lockAccessor(target, key, value) {
|
||||
try {
|
||||
Object.defineProperty(target, key, {
|
||||
@@ -86,46 +141,44 @@ async function planSandboxShell(cwd) {
|
||||
return plan
|
||||
}
|
||||
|
||||
export default async function () {
|
||||
var hostShell
|
||||
|
||||
return {
|
||||
config: async (cfg) => {
|
||||
if (!isEnforced()) return
|
||||
var configured = cfg.shell
|
||||
if (typeof configured === 'string' && configured.length > 0 && configured !== SHELL_SHIM_PATH) {
|
||||
hostShell = configured
|
||||
}
|
||||
lockAccessor(cfg, 'shell', SHELL_SHIM_PATH)
|
||||
},
|
||||
'shell.env': async (input, output) => {
|
||||
if (!isEnforced() || typeof input.callID !== 'string' || input.callID.length === 0) {
|
||||
if (hostShell !== undefined) {
|
||||
output.env[ENV_HOST_SHELL] = hostShell
|
||||
}
|
||||
return
|
||||
}
|
||||
if (!existsSync(SHELL_SHIM_PATH)) {
|
||||
throw unavailable('the sandbox shell shim is missing at ' + SHELL_SHIM_PATH)
|
||||
}
|
||||
var plan = await planSandboxShell(input.cwd)
|
||||
if (plan.env !== null && typeof plan.env === 'object') {
|
||||
for (var name of FORWARDED_ENV_NAMES) {
|
||||
if (typeof plan.env[name] === 'string') {
|
||||
output.env[name] = plan.env[name]
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!lockAccessor(output.env, ENV_WORKDIR, plan.workdir)) {
|
||||
throw unavailable('sandbox enforcement could not pin the sandbox working directory; aborting before the command runs on the host')
|
||||
}
|
||||
},
|
||||
'tool.execute.after': async (input, output) => {
|
||||
if (!isEnforced() || input.tool !== 'bash') return
|
||||
if (output.metadata === null || typeof output.metadata !== 'object') return
|
||||
output.metadata.sandbox = true
|
||||
},
|
||||
async function pinSandboxShell(event) {
|
||||
if (!existsSync(SHELL_SHIM_PATH)) {
|
||||
throw unavailable('the sandbox shell shim is missing at ' + SHELL_SHIM_PATH)
|
||||
}
|
||||
var plan = await planSandboxShell(event.cwd)
|
||||
if (plan.env !== null && typeof plan.env === 'object') {
|
||||
for (var name of FORWARDED_ENV_NAMES) {
|
||||
if (typeof plan.env[name] === 'string') {
|
||||
event.env[name] = plan.env[name]
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!lockAccessor(event, 'shell', SHELL_SHIM_PATH)) {
|
||||
throw unavailable('sandbox enforcement could not pin the sandbox shell; aborting before the command runs on the host')
|
||||
}
|
||||
if (!lockAccessor(event.env, ENV_WORKDIR, plan.workdir)) {
|
||||
throw unavailable('sandbox enforcement could not pin the sandbox working directory; aborting before the command runs on the host')
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
id: '${id}',
|
||||
async setup(ctx) {
|
||||
await ctx.permission.hook('evaluate', (event) => {
|
||||
if (!isEnforced() || !deniesServiceSettings(event)) return
|
||||
event.effect = 'deny'
|
||||
event.message = SERVICE_SETTINGS_DENIED
|
||||
})
|
||||
await ctx.shell.hook('create.before', async (event) => {
|
||||
if (!isEnforced()) return
|
||||
await pinSandboxShell(event)
|
||||
})
|
||||
await ctx.tool.hook('execute.after', (event) => {
|
||||
if (!isEnforced() || event.tool !== 'shell' || event.status !== 'completed') return
|
||||
if (event.result === null || typeof event.result !== 'object') return
|
||||
event.result = { ...event.result, metadata: { ...(event.result.metadata ?? {}), sandbox: true } }
|
||||
})
|
||||
},
|
||||
}
|
||||
`
|
||||
}
|
||||
Reference in new issue
Block a user