fix(sandbox): accept OpenCode and forge worktrees as sandboxed project roots

The Manager sets XDG_DATA_HOME to <workspace>/.opencode/state, so OpenCode
workspace worktrees land at <data>/worktree and opencode-forge loop
worktrees at <data>/forge/worktrees. Neither was a project root, so an
enforced session in a forge worktree was refused with "working directory
is outside the sandboxed project roots". Both directories are now mounted
and accepted as working directories; their parents (auth.json, forge.db)
stay outside the guest.
This commit is contained in:
Chris Scott committed 2026-09-02 00:44:57 -04:00
1 parent 93338abbdb
commit 91f37961e8
5 files changed
+84 -16

No files matched your search

+3 -1
View File
@@ -3,9 +3,11 @@ import { accessSync, constants, realpathSync, statSync } from 'fs'
import { realpath } from 'fs/promises'
import {
ENV,
getForgeWorktreesPath,
getOpenCodeAgentTmpPath,
getOpenCodeGlobalSkillsPath,
getOpenCodeToolOutputPath,
getOpenCodeWorktreesPath,
getReposPath,
getScheduleWorktreesPath,
} from '@opencode-manager/shared/config/env'
@@ -153,7 +155,7 @@ export function buildSandboxVerifyProvisionArgs(): string[] {
}
export function sandboxProjectRoots(): string[] {
return [getReposPath(), getScheduleWorktreesPath()]
return [getReposPath(), getScheduleWorktreesPath(), getOpenCodeWorktreesPath(), getForgeWorktreesPath()]
}
export function sandboxMountRoots(): string[] {
+25 -7
View File
@@ -3,7 +3,7 @@ import { spawnSync } from 'child_process'
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { ENV, getAssistantOpenCodeDir, getOpenCodeAgentTmpPath, getOpenCodeGlobalSkillsPath, getOpenCodeToolOutputPath, getReposPath, getScheduleWorktreesPath } from '@opencode-manager/shared/config/env'
import { ENV, getAssistantOpenCodeDir, getForgeWorktreesPath, getOpenCodeAgentTmpPath, getOpenCodeGlobalSkillsPath, getOpenCodeToolOutputPath, getOpenCodeWorktreesPath, getReposPath, getScheduleWorktreesPath } from '@opencode-manager/shared/config/env'
import { unwrapSandboxExecCommand } from '@opencode-manager/shared/utils'
import {
WORKSPACE_SANDBOX_NAME,
@@ -101,9 +101,11 @@ describe('sandbox command builders', () => {
expect(mountArgs).toEqual(sandboxMountRoots().map((root) => `${root}:${root}`))
expect(mountArgs[0]).toBe(`${getReposPath()}:${getReposPath()}`)
expect(mountArgs[1]).toBe(`${getScheduleWorktreesPath()}:${getScheduleWorktreesPath()}`)
expect(mountArgs[2]).toBe(`${getOpenCodeToolOutputPath()}:${getOpenCodeToolOutputPath()}`)
expect(mountArgs[3]).toBe(`${getOpenCodeGlobalSkillsPath()}:${getOpenCodeGlobalSkillsPath()}`)
expect(mountArgs[4]).toBe(`${getOpenCodeAgentTmpPath()}:${getOpenCodeAgentTmpPath()}`)
expect(mountArgs[2]).toBe(`${getOpenCodeWorktreesPath()}:${getOpenCodeWorktreesPath()}`)
expect(mountArgs[3]).toBe(`${getForgeWorktreesPath()}:${getForgeWorktreesPath()}`)
expect(mountArgs[4]).toBe(`${getOpenCodeToolOutputPath()}:${getOpenCodeToolOutputPath()}`)
expect(mountArgs[5]).toBe(`${getOpenCodeGlobalSkillsPath()}:${getOpenCodeGlobalSkillsPath()}`)
expect(mountArgs[6]).toBe(`${getOpenCodeAgentTmpPath()}:${getOpenCodeAgentTmpPath()}`)
})
it('never masks the assistant .opencode directory with a tmpfs overlay', () => {
@@ -122,7 +124,7 @@ describe('sandbox command builders', () => {
expect(args.indexOf(ENV.SANDBOX.IMAGE)).toBeGreaterThan(entrypointIndex + 1)
})
it('mounts only the skills, tool-output, and agent tmp directories from outside the project roots', () => {
it('mounts only the worktree, skills, tool-output, and agent tmp directories from outside the repo roots', () => {
const workspacePath = path.dirname(getReposPath())
const roots = sandboxMountRoots()
@@ -130,6 +132,8 @@ describe('sandbox command builders', () => {
getOpenCodeGlobalSkillsPath(),
])
expect(roots.filter((root) => isPathWithinRoot(path.join(workspacePath, '.opencode'), root))).toEqual([
getOpenCodeWorktreesPath(),
getForgeWorktreesPath(),
getOpenCodeToolOutputPath(),
getOpenCodeAgentTmpPath(),
])
@@ -175,10 +179,12 @@ describe('sandbox command builders', () => {
expect(lifecycle.idle_timeout_secs).toBeNull()
const binds = mounts.filter((mount) => mount.type === 'Bind')
expect(binds).toHaveLength(5)
expect(binds).toHaveLength(7)
expect(binds.map((mount) => mount.host)).toEqual([
getReposPath(),
getScheduleWorktreesPath(),
getOpenCodeWorktreesPath(),
getForgeWorktreesPath(),
getOpenCodeToolOutputPath(),
getOpenCodeGlobalSkillsPath(),
getOpenCodeAgentTmpPath(),
@@ -199,14 +205,18 @@ describe('sandbox command builders', () => {
expect(mounts.find((mount) => mount.type === 'Tmpfs')).toBeUndefined()
})
it('accepts real repo dirs and schedule worktrees while rejecting config, missing, and unrelated paths', async () => {
it('accepts real repo dirs, schedule, OpenCode, and forge worktrees while rejecting config, missing, and unrelated paths', async () => {
const tmp = mkdtempSync(path.join(tmpdir(), 'ocm-sandbox-roots-'))
const originalWorkspacePath = process.env.WORKSPACE_PATH
try {
const repos = path.join(tmp, 'repos')
const schedules = path.join(tmp, 'schedule-worktrees')
const openCodeWorktrees = path.join(tmp, '.opencode', 'state', 'opencode', 'worktree')
const forgeWorktrees = path.join(tmp, '.opencode', 'state', 'opencode', 'forge', 'worktrees')
mkdirSync(path.join(repos, 'org', 'repo', 'subdir'), { recursive: true })
mkdirSync(path.join(schedules, 'job-1-run-2'), { recursive: true })
mkdirSync(path.join(openCodeWorktrees, 'project-id', 'feature'), { recursive: true })
mkdirSync(path.join(forgeWorktrees, 'loop-slug'), { recursive: true })
mkdirSync(path.join(tmp, '.config', 'opencode'), { recursive: true })
mkdirSync(path.join(tmp, '.opencode', 'state', 'opencode', 'tool-output'), { recursive: true })
mkdirSync(path.join(tmp, '.opencode', 'tmp', 'opencode'), { recursive: true })
@@ -222,6 +232,14 @@ describe('sandbox command builders', () => {
await expect(resolveSandboxWorkDirectory(path.join(schedules, 'job-1-run-2'))).resolves.toBe(
path.join(schedules, 'job-1-run-2'),
)
await expect(resolveSandboxWorkDirectory(path.join(openCodeWorktrees, 'project-id', 'feature'))).resolves.toBe(
path.join(openCodeWorktrees, 'project-id', 'feature'),
)
await expect(resolveSandboxWorkDirectory(path.join(forgeWorktrees, 'loop-slug'))).resolves.toBe(
path.join(forgeWorktrees, 'loop-slug'),
)
await expect(resolveSandboxWorkDirectory(path.join(tmp, '.opencode', 'state', 'opencode'))).resolves.toBeNull()
await expect(resolveSandboxWorkDirectory(path.join(tmp, '.opencode', 'state', 'opencode', 'forge'))).resolves.toBeNull()
await expect(resolveSandboxWorkDirectory(path.join(repos, '..', '.config', 'opencode'))).resolves.toBeNull()
await expect(resolveSandboxWorkDirectory(getOpenCodeToolOutputPath())).resolves.toBeNull()
await expect(resolveSandboxWorkDirectory(getOpenCodeGlobalSkillsPath())).resolves.toBeNull()
+44 -2
View File
@@ -3,7 +3,7 @@ import { Database } from 'bun:sqlite'
import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { ENV, getAssistantOpenCodeDir, getOpenCodeAgentTmpPath, getOpenCodeGlobalSkillsPath, getOpenCodeToolOutputPath, getReposPath, getScheduleWorktreesPath } from '@opencode-manager/shared/config/env'
import { ENV, getAssistantOpenCodeDir, getForgeWorktreesPath, getOpenCodeAgentTmpPath, getOpenCodeGlobalSkillsPath, getOpenCodeToolOutputPath, getOpenCodeWorktreesPath, getReposPath, getScheduleWorktreesPath } from '@opencode-manager/shared/config/env'
import { migrate } from '../../../src/db/migration-runner'
import { allMigrations } from '../../../src/db/migrations'
import { SettingsService } from '../../../src/services/settings'
@@ -40,6 +40,8 @@ process.env.WORKSPACE_PATH = suiteWorkspaceParent
const reposRoot = getReposPath()
const worktreesRoot = getScheduleWorktreesPath()
const openCodeWorktreesRoot = getOpenCodeWorktreesPath()
const forgeWorktreesRoot = getForgeWorktreesPath()
const toolOutputRoot = getOpenCodeToolOutputPath()
const skillsRoot = getOpenCodeGlobalSkillsPath()
const agentTmpRoot = getOpenCodeAgentTmpPath()
@@ -150,6 +152,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -626,6 +630,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -762,6 +768,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -801,6 +809,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -996,6 +1006,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -1037,6 +1049,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -1146,6 +1160,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -1240,6 +1256,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -1878,7 +1896,7 @@ describe('SandboxRuntimeService', () => {
expect(plan).toEqual({
mode: 'blocked',
reason: `working directory is outside the sandboxed project roots (${reposRoot}, ${worktreesRoot})`,
reason: `working directory is outside the sandboxed project roots (${reposRoot}, ${worktreesRoot}, ${openCodeWorktreesRoot}, ${forgeWorktreesRoot})`,
})
expect(mockExecuteCommand).not.toHaveBeenCalled()
})
@@ -1930,6 +1948,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
{ ...bindMount(reposRoot), options: { readonly: false, noexec: true, nosuid: false, nodev: false } },
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -2779,6 +2799,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -2795,6 +2817,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
{ ...bindMount(reposRoot), follow_root_symlinks: true },
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -2810,6 +2834,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
{ ...bindMount(reposRoot), host_permissions: 'public' },
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -2825,6 +2851,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
{ ...bindMount(reposRoot), stat_virtualization: 'none' },
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -2840,6 +2868,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
{ ...bindMount(reposRoot), quota_mib: 512 },
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -2925,6 +2955,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -2940,6 +2972,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -2957,6 +2991,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -2973,6 +3009,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -3032,6 +3070,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
@@ -3049,6 +3089,8 @@ describe('SandboxRuntimeService', () => {
mounts: [
bindMount(reposRoot),
bindMount(worktreesRoot),
bindMount(openCodeWorktreesRoot),
bindMount(forgeWorktreesRoot),
bindMount(toolOutputRoot),
bindMount(skillsRoot),
bindMount(agentTmpRoot),
+8 -5
View File
@@ -67,7 +67,7 @@ The overlay exposes `/dev/kvm`, `/dev/net/tun`, and `NET_ADMIN` without enabling
All projects share one microVM named `ocm-workspace`:
- It mounts the two project roots plus the OpenCode directories agents are handed absolute paths to, at identical guest paths. See [Mounts and Secrets](#mounts-and-secrets).
- It mounts the four project roots plus the OpenCode directories agents are handed absolute paths to, at identical guest paths. See [Mounts and Secrets](#mounts-and-secrets).
- Repositories and worktrees created after boot are visible immediately because their parent roots are mounted.
- Each command supplies its own working directory through `msb exec -w`.
- A session outside the mounted roots is refused rather than executed on the host.
@@ -91,13 +91,15 @@ The microVM receives writable bind mounts for:
|-----------|-------------------|
| `/workspace/repos` | Project root |
| `/workspace/schedule-worktrees` | Project root |
| `/workspace/.opencode/state/opencode/worktree` | Project root: worktrees created through OpenCode's workspace API |
| `/workspace/.opencode/state/opencode/forge/worktrees` | Project root: worktrees created by the opencode-forge plugin for its loops |
| `/workspace/.opencode/state/opencode/tool-output` | Where OpenCode saves the full content of a truncated tool result before handing the agent that absolute path |
| `/workspace/.config/opencode/skills` | Global skills, including the scripts and reference files a skill bundles and refers to by absolute path |
| `/workspace/.opencode/tmp/opencode` | The temporary directory the `bash` tool description tells agents to use for work outside the workspace |
Each is mounted at the identical guest path. That is the point: OpenCode hands the model absolute host paths, and the host-side `read`, `write`, and `glob` tools resolve them against the container. A path that is not mounted resolves for those tools but not for a sandboxed `bash` call, which is how an agent ends up searching for a file it was just told the exact location of.
Only the two project roots are accepted as working directories. The other three mounts are readable and writable but are never a valid working directory, so `bash` calls still have to run inside a repository or a schedule worktree.
Only the four project roots are accepted as working directories. The other three mounts are readable and writable but are never a valid working directory, so `bash` calls still have to run inside a repository or a worktree.
No internal API token exists anywhere under the mounted roots. The token lives in the Manager's database and reaches the generated plugins only through the `OCM_INTERNAL_TOKEN` environment variable of the Manager's own OpenCode process, which is never part of the guest environment.
@@ -115,7 +117,7 @@ The following remain outside the microVM:
| `/workspace/.ssh-keys` | Repository SSH private keys |
| `/workspace/.config/opencode/plugin`, `/workspace/.config/ocm` | Generated plugins and the shell shim — the enforcement mechanism itself |
| `/workspace/.config/opencode` (except `skills`) | OpenCode configuration |
| `/workspace/.opencode/state` (except `opencode/tool-output`) | Provider and MCP credentials |
| `/workspace/.opencode/state` (except `opencode/tool-output`, `opencode/worktree`, and `opencode/forge/worktrees`) | Provider and MCP credentials, the forge database |
OpenCode's host process still reads these paths normally. They are omitted only from the agent command environment.
@@ -131,7 +133,7 @@ OpenCode's host process still reads these paths normally. They are omitted only
A directory outside the mounted roots fails with:
```text
Sandbox enforcement is on but the sandbox is unavailable: working directory is outside the sandboxed project roots (/workspace/repos, /workspace/schedule-worktrees)
Sandbox enforcement is on but the sandbox is unavailable: working directory is outside the sandboxed project roots (/workspace/repos, /workspace/schedule-worktrees, /workspace/.opencode/state/opencode/worktree, /workspace/.opencode/state/opencode/forge/worktrees)
```
The enforcement stamp remains authoritative for the lifetime of the OpenCode child, even if the setting changes before the required restart.
@@ -139,7 +141,8 @@ The enforcement stamp remains authoritative for the lifetime of the OpenCode chi
## Worktree Placement
- Scheduled runs use worktrees under `/workspace/schedule-worktrees` when OpenCode's workspace API returns a path beneath unmounted state storage.
- User-created OpenCode worktrees outside the mounted roots are created normally; only a later agent `bash` call whose working directory is outside the mounts is refused by the planner.
- OpenCode's own workspace worktrees (`/workspace/.opencode/state/opencode/worktree`) and opencode-forge loop worktrees (`/workspace/.opencode/state/opencode/forge/worktrees`) are project roots, so agent `bash` calls run inside them without further configuration. Both live under OpenCode's data directory because the Manager sets `XDG_DATA_HOME=/workspace/.opencode/state`.
- Any other worktree location outside the mounted roots is created normally; only a later agent `bash` call whose working directory is outside the mounts is refused by the planner.
- External repositories symlinked into `/workspace/repos` remain outside the microVM because the link target is not mounted.
## Git Credentials in the Sandbox
+4 -1
View File
@@ -150,7 +150,10 @@ export const getAssistantOpenCodeDir = () => path.join(getAssistantModePath(), A
export const getOpenCodeConfigHome = () => path.join(ENV.WORKSPACE.BASE_PATH, '.config')
export const getOpenCodeStateHome = () => path.join(ENV.WORKSPACE.BASE_PATH, '.opencode', 'state')
export const getOpenCodeGlobalSkillsPath = () => path.join(getOpenCodeConfigHome(), 'opencode', 'skills')
export const getOpenCodeToolOutputPath = () => path.join(getOpenCodeStateHome(), 'opencode', 'tool-output')
export const getOpenCodeDataPath = () => path.join(getOpenCodeStateHome(), 'opencode')
export const getOpenCodeToolOutputPath = () => path.join(getOpenCodeDataPath(), 'tool-output')
export const getOpenCodeWorktreesPath = () => path.join(getOpenCodeDataPath(), 'worktree')
export const getForgeWorktreesPath = () => path.join(getOpenCodeDataPath(), 'forge', 'worktrees')
export const getOpenCodeTmpHome = () => path.join(ENV.WORKSPACE.BASE_PATH, '.opencode', 'tmp')
export const getOpenCodeAgentTmpPath = () => path.join(getOpenCodeTmpHome(), 'opencode')
export const getConfigPath = () => path.join(ENV.WORKSPACE.BASE_PATH, ENV.WORKSPACE.CONFIG_DIR)