fix(sandbox): pin neutral entrypoint and persist opencode upgrades across rebuilds

This commit is contained in:
Chris Scott committed 2026-08-23 11:22:46 -04:00
1 parent cadc283738
commit f2d0ae0595
14 files changed
+339 -15

No files matched your search

@@ -152,6 +152,22 @@ describe('workspace ownership configuration', () => {
expect(compose).toMatch(/^volumes:\n(?:.*\n)*?\s+opencode-workspace:/m)
})
it('mounts a dedicated named volume at /home/node/.opencode/bin with a top-level declaration', () => {
const compose = read(composePath)
expect(compose).toContain('opencode-bin:/home/node/.opencode/bin')
expect(compose).toMatch(/^volumes:\n(?:.*\n)*?\s+opencode-bin:/m)
})
it('persists only the opencode bin directory, not the whole ~/.opencode home', () => {
const compose = read(composePath)
expect(compose).not.toMatch(/:\/home\/node\/\.opencode(?:\s|$)/)
})
it('lists the opencode-bin volume in the installation docs table', () => {
const docs = read(join(repoRoot, 'docs/getting-started/installation.md'))
expect(docs).toContain('| `opencode-bin` | `/home/node/.opencode/bin` |')
})
it('keeps the docker docs compose snippet in sync with docker-compose.yml', () => {
const compose = read(composePath)
const docs = read(dockerDocsPath)
@@ -188,6 +204,26 @@ describe('workspace ownership configuration', () => {
})
})
describe('docker lifecycle scripts', () => {
it('keeps docker:down non-destructive and docker:reset destructive', () => {
const pkg = read(join(repoRoot, 'package.json'))
expect(pkg).toContain('"docker:down": "docker-compose down"')
expect(pkg).toContain('"docker:reset": "docker-compose down -v"')
})
it('documents the preserved-volume shutdown and the destructive reset', () => {
const docs = read(dockerDocsPath)
expect(docs).toContain('named volumes are preserved')
expect(docs).toContain('docker-compose down -v')
})
it('documents a targeted opencode-bin volume reset that preserves the other volumes', () => {
const docs = read(join(repoRoot, 'docs/troubleshooting.md'))
expect(docs).toContain('docker volume rm <project>_opencode-bin')
expect(docs).toContain('without touching the workspace or database volumes')
})
})
describe('sandbox compose overlay', () => {
const overlayPath = join(repoRoot, 'docker-compose.sandbox.yml')
const overlay = read(overlayPath)
@@ -32,7 +32,9 @@ const extractMinOpenCodeVersion = () => {
const installPrelude = () => [
extractMinOpenCodeVersion(),
extractShellFunction('version_gte'),
extractShellFunction('read_opencode_version'),
extractShellFunction('install_opencode'),
extractShellFunction('reconcile_persisted_opencode'),
].join('\n')
beforeEach(() => {
@@ -50,6 +52,12 @@ exit 2`)
writeStub('groupadd', `echo "groupadd $*" >> "$OCM_STUB_LOG"`)
writeStub('usermod', `echo "usermod $*" >> "$OCM_STUB_LOG"`)
writeStub('runuser', `echo "runuser $*" >> "$OCM_STUB_LOG"
if [ "${'${OCM_STUB_RUNUSER_EXEC:-0}'}" = "1" ]; then
while [ "$#" -gt 0 ] && [ "$1" != "--" ]; do shift; done
[ "$#" -gt 0 ] && shift
OCM_EXECUTED_AS_NODE=1 "$@"
exit $?
fi
exit ${'${OCM_STUB_RUNUSER_EXIT:-0}'}`)
})
@@ -181,6 +189,7 @@ const runOpenCodeSection = (snippet: string, env: Record<string, string> = {}) =
...process.env,
PATH: `${stubDir}:${homeDir}/.opencode/bin:/usr/bin:/bin`,
OCM_STUB_LOG: logPath,
OCM_STUB_RUNUSER_EXEC: '1',
HOME: homeDir,
OPENCODE_BUNDLED_VERSION: '1.18.16',
...env,
@@ -277,3 +286,95 @@ describe('install_opencode', () => {
expect(urls).not.toContain('/releases/latest/download/')
})
})
const writeBinary = (dir: string, version: string) => {
const binPath = join(dir, 'opencode')
writeFileSync(binPath, `#!/bin/bash\necho "opencode version ${version}"\n`)
chmodSync(binPath, 0o755)
return binPath
}
const homeBinPath = () => join(stubDir, 'home/.opencode/bin/opencode')
const bundledBinPath = () => join(stubDir, 'bundled/opencode')
const bundledFirstPath = () => `${join(stubDir, 'home/.opencode/bin')}:${join(stubDir, 'bundled')}:${stubDir}:/usr/bin:/bin`
describe('persisted opencode reconciliation', () => {
it('probes the persisted binary version through runuser, never directly', () => {
mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true })
writeFileSync(homeBinPath(), `#!/bin/bash
if [ -z "$OCM_EXECUTED_AS_NODE" ]; then
echo "persisted binary executed outside runuser" >&2
exit 1
fi
echo "opencode version 1.22.0"`)
chmodSync(homeBinPath(), 0o755)
const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`)
expect(res.status).toBe(0)
expect(stubCalls()).toContain(`runuser -u node -- ${homeBinPath()} --version`)
expect(res.stdout).toContain('retaining it')
expect(existsSync(homeBinPath())).toBe(true)
})
it('retains a persisted home binary equal to the bundled version', () => {
mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true })
writeBinary(join(stubDir, 'home/.opencode/bin'), '1.18.16')
const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`)
expect(res.status).toBe(0)
expect(res.stdout).toContain('retaining it')
expect(existsSync(homeBinPath())).toBe(true)
expect(res.stdout).toContain('OpenCode is installed (version: 1.18.16)')
expect(curlLog()).toHaveLength(0)
})
it('retains a persisted home binary newer than the bundled version', () => {
mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true })
writeBinary(join(stubDir, 'home/.opencode/bin'), '1.22.0')
const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`)
expect(res.status).toBe(0)
expect(res.stdout).toContain('retaining it')
expect(existsSync(homeBinPath())).toBe(true)
expect(res.stdout).toContain('OpenCode is installed (version: 1.22.0)')
expect(curlLog()).toHaveLength(0)
})
it('retains a persisted home binary older than the bundled version but above the minimum', () => {
mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true })
writeBinary(join(stubDir, 'home/.opencode/bin'), '1.10.0')
const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`)
expect(res.status).toBe(0)
expect(res.stdout).toContain('retaining it')
expect(existsSync(homeBinPath())).toBe(true)
expect(res.stdout).toContain('OpenCode is installed (version: 1.10.0)')
expect(curlLog()).toHaveLength(0)
})
it('replaces a persisted home binary below the minimum version with the bundled version', () => {
stubInstallTools()
mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true })
writeBinary(join(stubDir, 'home/.opencode/bin'), '1.0.0')
const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`)
expect(res.status).toBe(0)
expect(res.stdout).toContain('retaining it')
expect(res.stdout).toContain('below minimum required version')
expect(res.stdout).toContain('Reinstalling bundled OpenCode version 1.18.16...')
const urls = curlLog().join(' ')
expect(urls).toMatch(/\/releases\/download\/v1\.18\.16\//)
expect(urls).not.toContain('/releases/latest/download/')
})
it('removes a malformed persisted home binary and selects the bundled binary without downloading', () => {
mkdirSync(join(stubDir, 'home/.opencode/bin'), { recursive: true })
writeBinary(join(stubDir, 'home/.opencode/bin'), 'garbage')
mkdirSync(join(stubDir, 'bundled'), { recursive: true })
writeBinary(join(stubDir, 'bundled'), '1.18.16')
const res = runOpenCodeSection(`${installPrelude()}\n${extractOpenCodeInstallSection()}`, {
PATH: bundledFirstPath(),
})
expect(res.status).toBe(0)
expect(res.stdout).toContain('malformed or unversioned')
expect(existsSync(homeBinPath())).toBe(false)
expect(existsSync(bundledBinPath())).toBe(true)
expect(res.stdout).toContain('OpenCode is installed (version: 1.18.16)')
expect(curlLog()).toHaveLength(0)
})
})