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.
This commit is contained in:
Chris Scott committed 2026-08-25 09:59:32 -04:00
1 parent f6c368265f
commit 62cf04bbc9
16 files changed
+639 -460

No files matched your search

+75 -13
View File
@@ -1,24 +1,37 @@
# Assistant Internal API
The Assistant Internal API provides capabilities for OpenCode agents to interact with the manager backend via a secure bearer-token API.
The Assistant Internal API provides capabilities for OpenCode agents to interact with the manager backend. Agents reach it through the `ocm` tool, which authenticates on their behalf; the HTTP surface itself is a secure bearer-token API.
> For a user-facing overview of how to use and set up assistant mode, see [Assistant Mode](assistant-mode.md).
## Authentication
All endpoints require a bearer token. The token can be found at:
- `.opencode/internal-token` (relative to the assistant workspace cwd)
The raw HTTP endpoints require a bearer token:
Include the token in requests:
```
Authorization: Bearer <token>
```
The token has two in-product consumers, neither of which is an agent:
| Consumer | How it obtains the token |
|----------|--------------------------|
| Generated plugins (`ocm-manager.js`, `ocm-sandbox.js`, `ocm-gh-env.js`) | The `OCM_INTERNAL_TOKEN` environment variable, injected into the OpenCode child process |
| External clients such as the `ocm` CLI | Settings -> Manager Token, served by `GET /api/settings/manager-token` |
Agents never authenticate against this API themselves. They call the `ocm` tool, which holds the token inside the Manager process.
## The `ocm` Tool
The Manager installs a generated plugin (`ocm-manager.js`) that gives every agent an `ocm` tool. The tool calls this API from inside the Manager's own OpenCode process, so it needs no token, no base URL, and no network access from the agent shell.
That makes it the only path that works everywhere: a scheduled run executes in a throwaway worktree with no `.opencode/internal-token`, and a sandboxed `bash` call runs in a microVM where `localhost` is the guest, not the Manager.
That makes it the only path that works everywhere: a scheduled run executes in a throwaway worktree with no token of its own, and a sandboxed `bash` call runs in a microVM where `localhost` is the guest, not the Manager.
The tool has two actions.
### `send_notification`
Send a push notification to every device the user has registered.
```ts
{
@@ -27,7 +40,51 @@ That makes it the only path that works everywhere: a scheduled run executes in a
}
```
The bearer-token endpoints below remain available for agents running in the assistant workspace without sandboxing.
### `request`
Call an allow-listed internal API route and return the raw response body text.
```ts
{
action: 'request',
params: { method: 'GET' | 'POST' | 'PATCH' | 'DELETE', path: string, body?: object }
}
```
The `path` is relative to the internal API base (for example `/settings` or `/repos/0/schedules`) and query strings are allowed. The action resolves the path against the internal API base and rejects anything that resolves outside it (absolute URLs and `..` traversal), then rejects any method plus path that is not in the allow-list.
**Allow-listed 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
```
**Deliberately not allow-listed:**
- `POST /notifications/send` — use the `send_notification` action instead.
- `GET /git-credentials/gh-env` — returns the GitHub CLI environment (`GH_TOKEN`, `GITHUB_TOKEN`) for the OpenCode host process; it must not be readable by the agent.
- `POST /sandbox/shell` — the sandbox planner's internal route that resolves and pins the shell for a `bash` call; exposing it would let the agent drive shell planning directly.
- `/repos/*/mirror/*` — the repo mirror protocol that the `ocm` CLI uses to sync entire repositories; it can create, replace, patch, or delete whole repos, so it stays reserved for the CLI.
Agents should use the `ocm` tool rather than the raw bearer-token endpoints below. Those endpoints remain available to the frontend, generated plugins, and other Manager-internal clients.
## Endpoints
@@ -152,9 +209,14 @@ Reload the assistant workspace by disposing the current OpenCode instance. Use t
**Rate Limiting:** 5 requests per minute per token. Returns `429 Too Many Requests` with `Retry-After` header when exceeded.
**Example:**
```bash
curl -X POST -H "Authorization: Bearer <token>" \
"http://localhost:5003/api/internal/assistant/reload"
```json
{
"action": "request",
"params": {
"method": "POST",
"path": "/assistant/reload"
}
}
```
**Response:**
@@ -205,9 +267,9 @@ Retrieve a list of all managed repositories, ordered by the user's repo preferen
The assistant workspace includes four skills that document these capabilities:
1. **Schedule Management** (`.opencode/skills/schedule-management/SKILL.md`)
2. **Notifications** (`.opencode/skills/notifications/SKILL.md`)
3. **Manager Settings** (`.opencode/skills/manager-settings/SKILL.md`)
4. **Repo Management** (`.opencode/skills/repo-management/SKILL.md`)
1. **Schedule Management** (`.opencode/skills/schedule-management/SKILL.md`) — manage schedule jobs and runs through the `ocm` `request` action.
2. **Notifications** (`.opencode/skills/notifications/SKILL.md`) — send push notifications through the `ocm` `send_notification` action.
3. **Manager Settings** (`.opencode/skills/manager-settings/SKILL.md`) — read and patch user preferences and reload the assistant workspace through the `ocm` `request` action.
4. **Repo Management** (`.opencode/skills/repo-management/SKILL.md`) — list managed repositories through the `ocm` `request` action.
These skills are automatically provisioned when assistant mode is initialized and contain detailed examples and usage patterns.
+7 -8
View File
@@ -1,6 +1,6 @@
# Assistant Mode
Assistant Mode gives OpenCode Manager a dedicated AI workspace — an isolated directory (`repos/assistant/`) where a built-in assistant agent can manage scheduled jobs, send push notifications, and read or update settings via a secure internal API.
Assistant Mode gives OpenCode Manager a dedicated AI workspace — an isolated directory (`repos/assistant/`) where a built-in assistant agent can manage scheduled jobs, send push notifications, and read or update settings through the `ocm` tool.
## What Is Assistant Mode?
@@ -10,9 +10,8 @@ The assistant workspace is a special repository-like directory managed and maint
|------|---------|
| `AGENTS.md` | Workspace description the agent reads on every session start |
| `opencode.json` | OpenCode configuration scoped to the assistant agent |
| `.opencode/internal-token` | Bearer token used to authenticate against the internal API |
| `.opencode/agents/assistant.md` | Agent definition with system prompt and permissions |
| `.opencode/skills/` | Auto-generated skills teaching the agent to use the internal API |
| `.opencode/skills/` | Auto-generated skills teaching the agent to use the `ocm` tool |
## Skills Provided
@@ -20,10 +19,10 @@ Four skills are provisioned automatically when assistant mode is initialized:
| Skill | What it teaches |
|-------|----------------|
| `schedule-management` | Create, list, update, delete, and run scheduled jobs |
| `notifications` | Send push notifications to registered user devices with the `ocm` tool |
| `manager-settings` | Read and patch user preferences, and reload the assistant workspace |
| `repo-management` | List all managed repositories |
| `schedule-management` | Create, list, update, delete, and run scheduled jobs through the `ocm` `request` action |
| `notifications` | Send push notifications to registered user devices with the `ocm` `send_notification` action |
| `manager-settings` | Read and patch user preferences, and reload the assistant workspace, through the `ocm` `request` action |
| `repo-management` | List all managed repositories through the `ocm` `request` action |
See [Assistant Internal API](assistant-internal-api.md) for the full API reference these skills expose.
@@ -31,7 +30,7 @@ See [Assistant Internal API](assistant-internal-api.md) for the full API referen
The assistant's system prompt, behavior, and durable preferences live solely in `.opencode/agents/assistant.md`. This file is the single source of truth for the assistant agent's personality and self-editing rules. The `opencode.json` configuration no longer duplicates the agent persona — it only stores agent mode and workspace-level settings.
When the assistant self-edits its agent definition (e.g., to refine behavior or add durable preferences), it uses the `manager-settings` skill to call the `POST /assistant/reload` endpoint. This disposes the current OpenCode workspace instance so the changes take effect on the next message. The reload endpoint is rate-limited to 5 requests per minute; the assistant always asks the user before reloading.
When the assistant self-edits its agent definition (e.g., to refine behavior or add durable preferences), it uses the `manager-settings` skill to call the `POST /assistant/reload` endpoint through the `ocm` `request` action. This disposes the current OpenCode workspace instance so the changes take effect on the next message. The reload endpoint is rate-limited to 5 requests per minute; the assistant always asks the user before reloading.
## Getting Started
+3 -3
View File
@@ -69,7 +69,7 @@ All projects share one microVM named `ocm-workspace`:
- 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.
- The Manager verifies the microVM image, resources, user, network policy, mounts (including the `/tmp` tmpfs size and mount options), labels, and secret mask before reuse.
- The Manager verifies the microVM image, resources, user, network policy, mounts (including the `/tmp` tmpfs size and mount options), and labels before reuse. `/tmp` is the only tmpfs the microVM may carry; any other tmpfs fails attestation.
- MSB pulls the configured `SANDBOX_IMAGE` (default `node:24`) automatically; no custom image build is needed.
- The Manager pins a neutral `/usr/bin/env` entrypoint, so the image's own OCI entrypoint is never inherited.
- A stale or unverifiable microVM is removed and recreated.
@@ -88,9 +88,9 @@ The microVM receives writable bind mounts for:
- `/workspace/repos`
- `/workspace/schedule-worktrees`
The assistant workspace's `repos/assistant/.opencode` directory falls beneath the repository mount but is hidden in the guest behind a `tmpfs` mask so its internal API token cannot be read by agent commands.
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.
Because of that mask, and because `localhost` inside the microVM is the guest rather than the Manager, an agent command cannot call the internal API with `curl`. Agents reach the Manager through the `ocm` tool instead, which executes in the Manager's own OpenCode process and never exposes the token to the guest.
Because `localhost` inside the microVM is the guest rather than the Manager, and because the guest has no token to present, an agent command cannot call the internal API with `curl`. Agents reach the Manager through the `ocm` tool instead, which executes in the Manager's own OpenCode process. The tool's `request` action covers settings, repos, OpenCode workspaces, and schedules through an allow-list of internal API routes, while `send_notification` covers push notifications.
The microVM also mounts a runtime-owned tmpfs at `/tmp`. It is sized to one quarter of the microVM memory, clamped to 1-512 MiB, so agent commands get writable scratch space that is not backed by a host filesystem.