mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
* feat(sandbox): add KVM-backed agent sandboxing via microsandbox * feat(sandbox): route host-shell surfaces into microVM via shell wrapper * feat(sandbox): route host-shell surfaces into microVM via shell wrapper * fix(sandbox): harden enforcement validation and backup handling * fix(sandbox): bound lifecycle queue and harden install/backup paths Bound the OpenCode supervisor lifecycle queue to one running plus one pending operation so repeated restarts cannot stack unbounded HTTP requests, while exempting stop() so shutdown is never dropped. Reject enforcement backups that carry no recoverable removed sections, matching the existing fail-closed handling of unparseable backups. Install OpenCode through a private mktemp staging directory instead of the predictable /tmp/opencode path. Use named path/os imports in the files added by this branch. * refactor(sandbox): remove OpenCode version allowlist gating under enforcement * fix(settings): use resolved opencode binary and verify installed version * refactor(sandbox): scope enforcement to agent bash tool only * fix(sandbox): plan bash against factory directory and attest runtime tmpfs; align permission reply API * fix(sandbox): pin neutral entrypoint and persist opencode upgrades across rebuilds * fix(sandbox): align canonical spec with msb 0.6.8 runtime defaults * feat(sandbox): show unwrapped sandboxed bash commands in message UI * refactor(sandbox): pin shell shim to sandbox agent bash without rewriting commands * feat(sandbox): route notifications through a host-side manager tool * feat(sandbox): route all assistant capabilities through the ocm tool Add an allow-listed request action to the generated ocm plugin so agents reach settings, assistant reload, repos, git-info, workspaces, and schedules through the tool instead of curling the internal API with a bearer token, which is impossible under sandboxing. Rewrite the curl-based skills to use the ocm request action and drop the apiBaseUrl plumbing. Remove the now-unread .opencode/internal-token file and the sandbox tmpfs mask that hid it; attestation now rejects any tmpfs other than the runtime /tmp mount. * fix(sidebar): wire new-session and new-schedule actions to every page AssistantRedirect never registered a listener for the sidebar's new-session event, so the desktop sidebar CTA on /assistant was a no-op; the Schedules pages had the same gap for new-schedule. Make useSidebarAction the single source of truth for action keys and event dispatch, and warn when an action is dispatched with no mounted handler so an unhandled CTA fails loudly instead of silently. Wire the missing listeners to the existing per-page handlers. * feat(sandbox): forward git credentials into the microVM and add sandbox guest image * feat(sandbox): add git credential forwarding toggle to settings
182 lines
7.3 KiB
TypeScript
182 lines
7.3 KiB
TypeScript
import { ASSISTANT_NOTIFICATION_LIMITS } from '@opencode-manager/shared/schemas'
|
|
|
|
export const MANAGER_TOOL_NAME = 'ocm'
|
|
|
|
export const MANAGER_TOOL_REQUEST_TIMEOUT_MS = 15000
|
|
|
|
export const MANAGER_TOOL_ALLOWED_ROUTES = [
|
|
'GET /settings',
|
|
'PATCH /settings',
|
|
'POST /assistant/reload',
|
|
'GET /repos',
|
|
'GET /repos/*/git-info',
|
|
'GET /opencode-workspaces',
|
|
'GET /schedules/all',
|
|
'GET /schedules/all/runs',
|
|
'GET /repos/*/schedules',
|
|
'POST /repos/*/schedules',
|
|
'GET /repos/*/schedules/*',
|
|
'PATCH /repos/*/schedules/*',
|
|
'DELETE /repos/*/schedules/*',
|
|
'POST /repos/*/schedules/*/run',
|
|
'GET /repos/*/schedules/*/runs',
|
|
'DELETE /repos/*/schedules/*/runs',
|
|
'GET /repos/*/schedules/*/runs/*',
|
|
'DELETE /repos/*/schedules/*/runs/*',
|
|
'POST /repos/*/schedules/*/runs/*/cancel',
|
|
] as const
|
|
|
|
export const MANAGER_TOOL_ALLOWED_METHODS = ['GET', 'POST', 'PATCH', 'DELETE'] as const
|
|
|
|
export function parseAllowedRoute(route: string): { method: string; path: string } {
|
|
const separator = route.indexOf(' ')
|
|
return { method: route.slice(0, separator), path: route.slice(separator + 1) }
|
|
}
|
|
|
|
function buildRouteMatchers(): { method: string; source: string }[] {
|
|
return MANAGER_TOOL_ALLOWED_ROUTES.map((route) => {
|
|
const { method, path } = parseAllowedRoute(route)
|
|
const source = path
|
|
.split('/')
|
|
.map((segment) => (segment === '*' ? '[^/]+' : segment.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')))
|
|
.join('/')
|
|
return { method, source: `^${source}$` }
|
|
})
|
|
}
|
|
|
|
export function buildManagerToolPluginSource(): string {
|
|
return `import { z } from 'zod'
|
|
|
|
var REQUEST_TIMEOUT_MS = ${MANAGER_TOOL_REQUEST_TIMEOUT_MS}
|
|
|
|
var ALLOWED_ROUTES = ${JSON.stringify(MANAGER_TOOL_ALLOWED_ROUTES)}
|
|
|
|
var ALLOWED_MATCHERS = ${JSON.stringify(buildRouteMatchers())}.map(function (matcher) {
|
|
return { method: matcher.method, pattern: new RegExp(matcher.source) }
|
|
})
|
|
|
|
function resolveRoute(path) {
|
|
var baseUrl = process.env.OCM_INTERNAL_API_URL
|
|
var token = process.env.OCM_INTERNAL_TOKEN
|
|
if (!baseUrl || !token) {
|
|
throw new Error('The OpenCode Manager internal API is not configured for this OpenCode server.')
|
|
}
|
|
var base = new URL(baseUrl.endsWith('/') ? baseUrl : baseUrl + '/')
|
|
var url
|
|
try {
|
|
url = new URL(String(path).replace(/^\\/+/, ''), base)
|
|
} catch (error) {
|
|
throw new Error('The path ' + String(path) + ' is not a valid OpenCode Manager internal API path.')
|
|
}
|
|
if (url.origin !== base.origin || !url.pathname.startsWith(base.pathname)) {
|
|
throw new Error('The path ' + String(path) + ' resolves outside the OpenCode Manager internal API.')
|
|
}
|
|
return { url: url, token: token, routePath: '/' + url.pathname.slice(base.pathname.length) }
|
|
}
|
|
|
|
function assertAllowedRoute(method, path) {
|
|
var resolved = resolveRoute(path)
|
|
var allowed = ALLOWED_MATCHERS.some(function (matcher) {
|
|
return matcher.method === method && matcher.pattern.test(resolved.routePath)
|
|
})
|
|
if (!allowed) {
|
|
throw new Error(
|
|
method + ' ' + resolved.routePath + ' is not an allowed OpenCode Manager route. Allowed routes: ' + ALLOWED_ROUTES.join(', ') + '.',
|
|
)
|
|
}
|
|
return resolved
|
|
}
|
|
|
|
async function requestInternalApi(method, path, body) {
|
|
var resolved = resolveRoute(path)
|
|
var headers = { Authorization: 'Bearer ' + resolved.token }
|
|
var init = { method: method, headers: headers, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) }
|
|
if (body !== undefined) {
|
|
headers['content-type'] = 'application/json'
|
|
init.body = JSON.stringify(body)
|
|
}
|
|
var response
|
|
try {
|
|
response = await fetch(resolved.url.toString(), init)
|
|
} catch (error) {
|
|
throw new Error('The OpenCode Manager request failed: ' + (error instanceof Error ? error.message : String(error)))
|
|
}
|
|
var text = await response.text()
|
|
if (!response.ok) {
|
|
throw new Error('The OpenCode Manager request failed with status ' + response.status + (text ? ': ' + text : ''))
|
|
}
|
|
return text
|
|
}
|
|
|
|
async function postInternalApi(routePath, body) {
|
|
var text = await requestInternalApi('POST', routePath, body)
|
|
try {
|
|
return JSON.parse(text)
|
|
} catch (error) {
|
|
return {}
|
|
}
|
|
}
|
|
|
|
var ACTIONS = {
|
|
send_notification: {
|
|
params: z.object({
|
|
title: z.string().min(1).max(${ASSISTANT_NOTIFICATION_LIMITS.TITLE_MAX}).describe('The notification title.'),
|
|
body: z.string().min(1).max(${ASSISTANT_NOTIFICATION_LIMITS.BODY_MAX}).describe('The notification body.'),
|
|
url: z.string().min(1).max(${ASSISTANT_NOTIFICATION_LIMITS.URL_MAX}).optional().describe('A deep link to open, such as /repos/my-repo.'),
|
|
tag: z.string().max(${ASSISTANT_NOTIFICATION_LIMITS.TAG_MAX}).optional().describe('A deduplication key for replacing an earlier notification.'),
|
|
priority: z.enum(['normal', 'high']).optional().describe('Use high for something that should interrupt the user.'),
|
|
}).describe('Send a push notification to every device the user has registered.'),
|
|
run: async function (params) {
|
|
var result = await postInternalApi('/notifications/send', params)
|
|
if (result.noSubscriptions === true) {
|
|
return 'No devices are registered for push notifications, so nothing was delivered.'
|
|
}
|
|
return 'Notification sent: ' + (result.delivered || 0) + ' delivered, ' + (result.failed || 0) + ' failed.'
|
|
},
|
|
},
|
|
request: {
|
|
params: z.object({
|
|
method: z.enum(${JSON.stringify(MANAGER_TOOL_ALLOWED_METHODS)}).describe('The HTTP method for the internal API route.'),
|
|
path: z.string().min(1).max(500).describe('The internal API route path, such as /settings or /repos/0/schedules. Query strings are allowed.'),
|
|
body: z.record(z.string(), z.unknown()).optional().describe('The JSON request body, for POST and PATCH routes.'),
|
|
}).describe('Call an allow-listed OpenCode Manager internal API route.'),
|
|
run: async function (params) {
|
|
assertAllowedRoute(params.method, params.path)
|
|
var text = await requestInternalApi(params.method, params.path, params.body)
|
|
return text || 'The request succeeded with an empty response body.'
|
|
},
|
|
},
|
|
}
|
|
|
|
var ACTION_NAMES = Object.keys(ACTIONS)
|
|
|
|
export default async function () {
|
|
return {
|
|
tool: {
|
|
${MANAGER_TOOL_NAME}: {
|
|
description: [
|
|
'Perform an OpenCode Manager action.',
|
|
'The action runs inside OpenCode Manager itself, so it needs no token and no network access from the agent shell, and it works in sandboxed sessions and scheduled runs.',
|
|
'Actions:',
|
|
'- send_notification: send a push notification to every device the user has registered.',
|
|
'- request: call an allow-listed internal API route to read and manage settings, repos, OpenCode workspaces, and schedules.',
|
|
'Allowed request routes:',
|
|
].concat(ALLOWED_ROUTES.map(function (route) { return '- ' + route })).join('\\n'),
|
|
args: {
|
|
action: z.enum(ACTION_NAMES).describe('The OpenCode Manager action to perform.'),
|
|
params: z.union(ACTION_NAMES.map(function (name) { return ACTIONS[name].params })).describe('The parameters for the chosen action.'),
|
|
},
|
|
execute: async function (args) {
|
|
if (!Object.prototype.hasOwnProperty.call(ACTIONS, args.action)) {
|
|
throw new Error('Unknown OpenCode Manager action: ' + String(args.action) + '. Supported actions: ' + ACTION_NAMES.join(', ') + '.')
|
|
}
|
|
var action = ACTIONS[args.action]
|
|
return await action.run(action.params.parse(args.params))
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
`
|
|
}
|