Fix sandbox launch with OpenCode v2 background service

OpenCode v2.2 starts a background service on 127.0.0.1:49374, which nono
blocks unless a localhost port is granted. Give the sandbox its own service
on 49376 with isolated config/state, and clean up its lifecycle.

- Grant localhost ports 49376 (sandboxed service) and 3301 (Tinfoil proxy)
- Add sandbox config/state dirs and XDG_STATE_HOME/OPENCODE_CONFIG_DIR
- Wrapper mirrors real config, pins the sandbox service port, and stops
  stale/orphaned services so they never outlive the nono proxy
- Remove deprecated commands.allow/deny and the redundant SSH key glob deny
- Update installer, uninstall cleanup, and README
This commit is contained in:
2026-11-09 15:33:48 +00:00
parent 95405fcd14
commit 391c3265f4
5 changed files with 368 additions and 97 deletions
+165 -46
View File
@@ -1,11 +1,11 @@
# nono-opencode-tinfoil
A [`nono`](https://github.com/guybedford/nono) sandbox profile for running
[OpenCode](https://opencode.ai) with access to [Tinfoil](https://tinfoil.sh)
enclaves and other LLM providers.
## Quick Start
```bash
git clone https://github.com/autonomic/nono-opencode-tinfoil.git
cd nono-opencode-tinfoil
@@ -13,9 +13,8 @@ make install
# Ensure ~/.local/bin is in your PATH
opencode
```
## Install
## Install
### macOS / Linux
@@ -29,15 +28,21 @@ This installs:
- The sandbox profile to `~/.config/nono/profiles/opencode-tinfoil.json`
- A wrapper script to `~/.local/bin/opencode`
- Sandbox-local OpenCode config/state directories:
- `~/.config/opencode-sandbox/opencode`
- `~/.local/state/opencode-sandbox/opencode`
### Requirements
- [nono](https://github.com/guybedford/nono) installed e.g. `brew install nono`
- `opencode` installed globally. If not already installed, you can use any of:
- [nono](https://github.com/guybedford/nono) **0.81+**, e.g. `brew install nono`
- OpenCode **v2.2+**, installed globally. If not already installed, you can use any of:
- **Homebrew**: `brew install opencode`
- **npm**: `npm i -g opencode-ai`
- **Other**: Any Node version manager (Volta, fnm, etc.)
OpenCode v2 runs a background service, which is why this profile grants a
localhost port and creates sandbox-local config/state directories.
## How it works
```
@@ -48,9 +53,19 @@ you type: opencode
scans PATH for the real opencode binary
(skips itself — finds brew, npm, volta, etc.)
↓
exec nono run --profile opencode-tinfoil --allow-cwd -- <real-opencode>
creates ~/.config/opencode-sandbox/opencode
~/.local/state/opencode-sandbox/opencode
symlinks your real ~/.config/opencode/* into the sandbox
(except service.json)
writes {"port": 49376} to the sandbox service.json if missing
↓
opencode runs inside the sandbox
runs nono run --profile opencode-tinfoil --allow-cwd -- <real-opencode>
↓
opencode runs inside the sandbox and starts its own
background service on 127.0.0.1:49376
↓
when the sandbox exits, the wrapper stops the service
so it never outlives the nono proxy
```
The wrapper resolves the real `opencode` binary at runtime by scanning every
@@ -58,10 +73,47 @@ directory in your `PATH`. It skips itself, so it works regardless of:
- Where opencode is installed (Homebrew on macOS/Linux, npm global, Volta, etc.)
- PATH ordering between the wrapper and the real binary
- PATH entries containing spaces
- opencode version bumps
You just type `opencode` from any project directory and it runs sandboxed.
### Sandbox-local service
OpenCode v2 starts a background service on localhost. If the sandbox reused the
host service (default `127.0.0.1:49374`), the agent would run outside the
sandbox. To prevent that, the profile:
- points `OPENCODE_CONFIG_DIR` at `~/.config/opencode-sandbox/opencode`
- points `XDG_STATE_HOME` at `~/.local/state/opencode-sandbox`
- grants localhost port `49376` for the sandboxed service
The wrapper mirrors your normal OpenCode config into the sandbox config
directory as symlinks, so your models, plugins, and `opencode.jsonc` still apply.
`service.json` is deliberately **not** mirrored; the sandbox keeps its own copy
pinning port `49376`.
Your host OpenCode service on `49374` is left untouched. The sandboxed CLI only
sees its own service registration in `~/.local/state/opencode-sandbox`.
OpenCode spawns the service detached, so it would normally outlive the sandbox.
That matters because the service sends model requests through the nono proxy,
and the proxy exits with the sandbox. The wrapper therefore:
- stops the sandboxed service when the last sandboxed instance exits
- stops any stale service left behind by an abruptly killed session before the
next launch
- leaves a live service alone while another sandboxed instance is still running
If you run multiple sandboxed instances at once, they share the service on
`49376`, and it is stopped when the last instance exits. If the instance that
started the service exits first, stop the service explicitly so the remaining
instance starts a fresh one in its own sandbox:
```bash
~/.local/bin/opencode service stop
```
### PATH ordering
If a non-sandboxed `opencode` appears *earlier* on your `PATH` than the
@@ -79,79 +131,146 @@ alias opencode="$HOME/.local/bin/opencode"
```
## Usage
From any project directory:
```bash
opencode
```
Or use the profile directly with any opencode binary:
```bash
nono run --profile opencode-tinfoil --allow-cwd -- opencode
```
You can also point it at a specific binary:
Or use the profile directly with a specific opencode binary. Note that the
sandbox config/state directories and `service.json` must exist first; `make
install` creates them, or you can launch the wrapper once:
```bash
# Homebrew (macOS)
nono run --profile opencode-tinfoil --allow-cwd -- /opt/homebrew/bin/opencode
# Homebrew (Linux)
nono run --profile opencode-tinfoil --allow-cwd -- /home/linuxbrew/.linuxbrew/bin/opencode
# npm global
nono run --profile opencode-tinfoil --allow-cwd -- $(npm root -g)/opencode/bin/opencode
```
### Changing the sandbox service port
Port `49376` is chosen to avoid OpenCode's default `49374` (host service) and
`49375` (local channel). If `49376` is already in use, change it in **both**
places:
1. `network.open_port` in `profiles/opencode-tinfoil.jsonc`
2. `SANDBOX_SERVICE_PORT` in `bin/opencode` (and the existing
`~/.config/opencode-sandbox/opencode/service.json`)
Then re-run `make install`.
## Tinfoil integration
The profile allows network access to:
- `*.tinfoil.sh`
- `inference.tinfoil.sh`
- `containers.tinfoil.sh`
- `127.0.0.1` (supports Tinfoil local proxy at `http://127.0.0.1:3301/v1`)
- `127.0.0.1` (Tinfoil local proxy at `http://127.0.0.1:3301/v1`)
- `ghcr.io`
It also grants read/write access to `~/.tinfoil` for Tinfoil credentials.
Other LLM provider network options (Anthropic, GLM, Kimi, etc.) are provided as commented-out entries in the profile for easy activation.
The profile grants localhost ports:
- `49376` — sandboxed OpenCode background service
- `3301` — Tinfoil local proxy
Other LLM provider network options (Anthropic, GLM, Kimi, etc.) are provided as
commented-out entries in the profile for easy activation.
## Security model
- Current working directory: read/write
- OpenCode config: read/write
- OpenCode config and sandbox config/state: read/write
- Tinfoil config: read/write
- SSH private keys, AWS/GCP/Azure credentials, kubeconfig: denied
- `sudo`, `su`, `doas`, `passwd`, disk tools: denied
- The OpenCode background service runs **inside** the sandbox; the host service
on `127.0.0.1:49374` is not reused
- The profile no longer uses `commands.allow` / `commands.deny`. Those fields
are deprecated in nono and only applied at startup, so they were not enforced
for child processes. Access is constrained by filesystem, network, and
environment policy instead.
### Linux Landlock caveat
On Linux, nono uses Landlock, which has no deny semantics. `filesystem.deny`
globs are only enforced against paths that exist at sandbox start. Files created
afterward that match a deny glob are **not** denied. This profile therefore
avoids glob-based denies and relies on narrow allow grants plus the
`deny_credentials` group. If you add your own glob denies, keep this limitation
in mind or enable capability elevation in nono for runtime enforcement.
### Environment Variables
| Status | Variables / Patterns |
|--------|----------------------|
| Allowed | `PATH`, `HOME`, `USER`, `SHELL`, `TERM` |
| Denied | `AWS_*`, `GOOGLE_*`, `AZURE_*`, `KUBECONFIG`, `SSH_AUTH_SOCK` |
| Denied | `AWS_*`, `GOOGLE_*`, `AZURE_*`, `GCP_*`, `KUBECONFIG`, `SSH_AUTH_SOCK` |
## Uninstall
```bash
make uninstall
```
## License
GPL-3.0
This removes the profile, the wrapper, and the sandbox config/state
directories. Your real `~/.config/opencode` and `~/.local/state/opencode` are
not touched.
## Troubleshooting
**"command not found: opencode"**
Ensure `~/.local/bin` is in your `PATH`. Add `export PATH="$HOME/.local/bin:$PATH"` to your `.bashrc` or `.zshrc` and restart your terminal.
**Sandbox is being bypassed**
If you have another version of `opencode` installed (e.g. via Homebrew) that appears earlier in your `PATH` than the wrapper, the sandbox will be bypassed. Check this using `which -a opencode`. The wrapper at `~/.local/bin/opencode` must come first.
**`permission denied 127.0.0.1:49374` / `Server process exited with code 1`**
This means the sandboxed OpenCode is still trying to use the host service port.
The sandbox `service.json` is missing or stale, or the wrapper was bypassed.
Re-run `make install`, then remove the sandbox config and relaunch:
```bash
rm -rf ~/.config/opencode-sandbox ~/.local/state/opencode-sandbox
opencode
```
Confirm the sandboxed service is on `49376`:
```bash
~/.local/bin/opencode service status # sandbox, should be 49376
/path/to/host/opencode service status # host, should still be 49374
```
Use the real binary path for the host check (for example
`/home/linuxbrew/.linuxbrew/bin/opencode`). If `~/.local/bin` comes first in
your `PATH`, plain `opencode` invokes the sandbox wrapper.
**`Failed to start server. Is port 49376 in use?`**
The wrapper stops stale sandboxed services on startup, so this usually means an
unrelated process is using the port. Pick a different port as described in
[Changing the sandbox service port](#changing-the-sandbox-service-port). If you
want to clear it manually, stop the sandboxed service from inside the sandbox:
```bash
~/.local/bin/opencode service stop
```
**`Unrecognized flag: --standalone`**
`--standalone` is a top-level OpenCode flag, not a `serve` or `service`
subcommand flag. Use `opencode --standalone ...` rather than
`opencode serve --standalone`.
**Landlock glob-deny warning**
See [Linux Landlock caveat](#linux-landlock-caveat).
## License
GPL-3.0