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
181 lines
5.7 KiB
Bash
181 lines
5.7 KiB
Bash
#!/bin/bash
|
|
set -e
|
|
|
|
export HOME=/home/node
|
|
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
|
|
|
|
grant_kvm_access() {
|
|
local dev="${1:-/dev/kvm}"
|
|
[ -e "$dev" ] || return 0
|
|
|
|
local dev_gid group_name holder
|
|
dev_gid="$(stat -c '%g' "$dev" 2>/dev/null)" || return 0
|
|
case "$dev_gid" in
|
|
''|*[!0-9]*) return 0 ;;
|
|
esac
|
|
|
|
holder="$(getent group "$dev_gid" 2>/dev/null | cut -d: -f1 || true)"
|
|
if [ -n "$holder" ]; then
|
|
group_name="$holder"
|
|
else
|
|
group_name="kvm"
|
|
if ! groupadd -g "$dev_gid" "$group_name"; then
|
|
echo "ERROR: could not create group '$group_name' (gid $dev_gid) required for $dev access" >&2
|
|
return 1
|
|
fi
|
|
fi
|
|
|
|
if ! usermod -aG "$group_name" node; then
|
|
echo "ERROR: could not add node to group '$group_name' (gid $dev_gid) required for $dev access" >&2
|
|
return 1
|
|
fi
|
|
|
|
if ! runuser -u node -- test -r "$dev" || ! runuser -u node -- test -w "$dev"; then
|
|
echo "ERROR: node cannot access $dev (group '$group_name', gid $dev_gid)" >&2
|
|
echo "ERROR: grant the container group access to $dev or run the sandbox overlay (docker-compose.sandbox.yml)" >&2
|
|
return 1
|
|
fi
|
|
|
|
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
|
|
}
|
|
|
|
read_opencode_version() {
|
|
local binary
|
|
binary="$(command -v "${1:-opencode}" 2>/dev/null || true)"
|
|
if [ -z "$binary" ] || [ ! -x "$binary" ]; then
|
|
return 0
|
|
fi
|
|
runuser -u node -- "$binary" --version 2>&1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true
|
|
}
|
|
|
|
install_opencode() {
|
|
local opencode_version="${OPENCODE_BUNDLED_VERSION:-}"
|
|
if [ -z "$opencode_version" ]; then
|
|
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"
|
|
}
|
|
|
|
reconcile_persisted_opencode() {
|
|
local persisted_path="$HOME/.opencode/bin/opencode"
|
|
[ -e "$persisted_path" ] || return 0
|
|
local persisted_version
|
|
persisted_version="$(read_opencode_version "$persisted_path")"
|
|
if [ -z "$persisted_version" ]; then
|
|
echo "Persisted OpenCode at $persisted_path is malformed or unversioned; removing it to fall back to the bundled binary"
|
|
rm -f "$persisted_path"
|
|
return 0
|
|
fi
|
|
echo "Persisted OpenCode $persisted_version is usable; retaining it"
|
|
}
|
|
|
|
echo "Checking Bun installation..."
|
|
|
|
if ! command -v bun >/dev/null 2>&1; then
|
|
echo "Bun not found. Installing..."
|
|
curl -fsSL https://bun.sh/install | bash
|
|
|
|
if ! command -v bun >/dev/null 2>&1; then
|
|
echo "Failed to install Bun. Exiting."
|
|
exit 1
|
|
fi
|
|
|
|
echo "Bun installed successfully"
|
|
else
|
|
BUN_VERSION=$(bun --version 2>&1 || echo "unknown")
|
|
echo "Bun is installed (version: $BUN_VERSION)"
|
|
fi
|
|
|
|
echo "Checking OpenCode installation..."
|
|
|
|
reconcile_persisted_opencode
|
|
|
|
if ! command -v opencode >/dev/null 2>&1; then
|
|
echo "OpenCode not found. Installing..."
|
|
install_opencode
|
|
|
|
if ! command -v opencode >/dev/null 2>&1; then
|
|
echo "Failed to install OpenCode. Exiting."
|
|
exit 1
|
|
fi
|
|
echo "OpenCode installed successfully"
|
|
fi
|
|
|
|
OPENCODE_VERSION="$(read_opencode_version)"
|
|
[ -n "$OPENCODE_VERSION" ] || OPENCODE_VERSION="unknown"
|
|
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)"
|
|
else
|
|
echo "OpenCode version $OPENCODE_VERSION is below minimum required version $MIN_OPENCODE_VERSION"
|
|
echo "Reinstalling bundled OpenCode version ${OPENCODE_BUNDLED_VERSION}..."
|
|
install_opencode
|
|
|
|
OPENCODE_VERSION="$(read_opencode_version)"
|
|
[ -n "$OPENCODE_VERSION" ] || OPENCODE_VERSION="unknown"
|
|
echo "OpenCode reinstalled as version: $OPENCODE_VERSION"
|
|
fi
|
|
fi
|
|
|
|
echo "Starting OpenCode Manager Backend..."
|
|
|
|
if [ -z "$AUTH_SECRET" ]; then
|
|
echo "AUTH_SECRET is required but not set"
|
|
echo ""
|
|
echo "Please set AUTH_SECRET environment variable with a secure random string."
|
|
echo "Generate one with: openssl rand -base64 32"
|
|
echo ""
|
|
echo "Example in docker-compose.yml:"
|
|
echo " environment:"
|
|
echo " - AUTH_SECRET=your-secure-random-secret-here"
|
|
echo ""
|
|
echo "Example with Docker run:"
|
|
echo " docker run -e AUTH_SECRET=\$(openssl rand -base64 32) ..."
|
|
echo ""
|
|
exit 1
|
|
fi
|
|
|
|
if ! align_container_user node; then
|
|
exit 1
|
|
fi
|
|
|
|
if ! grant_kvm_access; then
|
|
echo "WARNING: continuing without /dev/kvm access; agent sandboxing will report itself unavailable" >&2
|
|
fi
|
|
|
|
warn_if_workspace_owner_differs /workspace "$OCM_TARGET_UID" "$OCM_TARGET_GID"
|
|
|
|
mkdir -p /app/data /workspace /home/node/.cache /home/node/.opencode /home/node/.microsandbox
|
|
chown -R node:node /app/data /workspace /home/node
|
|
|
|
exec runuser -u node -- "$@"
|