|
| 1 | +--- |
| 2 | +title: Webapp Update Flow (Host Command File) |
| 3 | +--- |
| 4 | + |
| 5 | +# Webapp Update Flow (Host Command File) |
| 6 | + |
| 7 | +This document explains how a webapp running inside the opencode container can trigger a host-side |
| 8 | +`occ update opencode` via a bind-mounted command file. It is written for implementors of the |
| 9 | +opencode webapp and describes the expected file paths, JSON payloads, error cases, and recommended |
| 10 | +write behavior. |
| 11 | + |
| 12 | +## Overview |
| 13 | + |
| 14 | +The host runs `occ start --no-daemon` as a foreground service (via `occ install`). When running, |
| 15 | +it polls a bind-mounted command file. The webapp writes a JSON command to that file, and the host |
| 16 | +executes `occ update opencode`. A result JSON is written back to a sibling file. |
| 17 | + |
| 18 | +Key points: |
| 19 | +- No network port is required. |
| 20 | +- Access control is filesystem permissions on the bind mount. |
| 21 | +- The listener runs only when `occ` is running in foreground (service mode). |
| 22 | + |
| 23 | +## Service Requirement |
| 24 | + |
| 25 | +The listener is started when `occ start --no-daemon` runs. This happens automatically when the |
| 26 | +service is installed: |
| 27 | + |
| 28 | +``` |
| 29 | +occ install |
| 30 | +``` |
| 31 | + |
| 32 | +If `occ` is running in the background or not running as a service, the command file will not be |
| 33 | +processed. Ensure the service is installed and running. |
| 34 | + |
| 35 | +## Paths and Bind Mounts |
| 36 | + |
| 37 | +The command file lives under the state bind mount. With default mounts, the paths are: |
| 38 | + |
| 39 | +- Host: `~/.local/state/opencode/opencode-cloud/commands/update-command.json` |
| 40 | +- Container: `/home/opencode/.local/state/opencode/opencode-cloud/commands/update-command.json` |
| 41 | +- Result file (host): `~/.local/state/opencode/opencode-cloud/commands/update-command.result.json` |
| 42 | +- Result file (container): `/home/opencode/.local/state/opencode/opencode-cloud/commands/update-command.result.json` |
| 43 | + |
| 44 | +If you customize mounts, the container path is still under: |
| 45 | + |
| 46 | +``` |
| 47 | +/home/opencode/.local/state/opencode/opencode-cloud/commands/ |
| 48 | +``` |
| 49 | + |
| 50 | +## Command File Contract |
| 51 | + |
| 52 | +### Request JSON (webapp writes) |
| 53 | + |
| 54 | +```json |
| 55 | +{ |
| 56 | + "command": "update_opencode", |
| 57 | + "request_id": "optional-id", |
| 58 | + "branch": "dev", |
| 59 | + "commit": "optional-sha" |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Fields: |
| 64 | +- `command` (required): must be `"update_opencode"`. |
| 65 | +- `request_id` (optional): any string to correlate request and result. |
| 66 | +- `branch` (optional): update to a branch (e.g., `"dev"`). |
| 67 | +- `commit` (optional): update to a commit SHA. |
| 68 | + |
| 69 | +Rules: |
| 70 | +- Specify either `branch` or `commit`, not both. |
| 71 | +- If both are omitted, the host uses the default behavior (currently `dev`). |
| 72 | + |
| 73 | +### Result JSON (host writes) |
| 74 | + |
| 75 | +```json |
| 76 | +{ |
| 77 | + "status": "success", |
| 78 | + "request_id": "optional-id", |
| 79 | + "message": "Update completed", |
| 80 | + "started_at": "2026-02-01T22:48:00Z", |
| 81 | + "finished_at": "2026-02-01T22:48:12Z" |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +Fields: |
| 86 | +- `status`: `"success"` or `"error"`. |
| 87 | +- `request_id`: echoed from the request when provided. |
| 88 | +- `message`: human-readable status or error message. |
| 89 | +- `started_at` / `finished_at`: ISO-8601 timestamps. |
| 90 | + |
| 91 | +## Recommended Write Behavior (Webapp) |
| 92 | + |
| 93 | +To avoid partial reads: |
| 94 | + |
| 95 | +1. Write to a temporary file in the same directory. |
| 96 | +2. Atomically rename to `update-command.json`. |
| 97 | + |
| 98 | +Example pseudo-steps: |
| 99 | + |
| 100 | +``` |
| 101 | +write /commands/update-command.json.tmp |
| 102 | +rename to /commands/update-command.json |
| 103 | +``` |
| 104 | + |
| 105 | +Then poll the result file until it exists and matches the `request_id`. |
| 106 | + |
| 107 | +## Error Cases and Scenarios |
| 108 | + |
| 109 | +### Rate Limit (Burst Protection) |
| 110 | +The listener processes at most one command every 5 seconds. If multiple commands are written |
| 111 | +back-to-back, the first one is handled and the next one waits until the interval elapses. |
| 112 | + |
| 113 | +### Command File Is Missing |
| 114 | +If the command file does not exist, the host does nothing. This is normal. |
| 115 | + |
| 116 | +### Invalid JSON |
| 117 | +If the JSON is invalid and the file is older than a short grace window, the host will: |
| 118 | +- record an error result in the result file |
| 119 | +- delete the command file |
| 120 | + |
| 121 | +If invalid JSON is detected immediately after write, the host will retry once the file |
| 122 | +stabilizes (to avoid race conditions). |
| 123 | + |
| 124 | +### Unsupported Command |
| 125 | +If `command` is not `"update_opencode"`, the host writes an error result and removes the file. |
| 126 | + |
| 127 | +### Both `branch` and `commit` Set |
| 128 | +This is rejected with an error result. |
| 129 | + |
| 130 | +### Missing Bind Mount or Read-Only Mount |
| 131 | +If the state mount is missing or read-only, the listener disables itself. The webapp will never |
| 132 | +see a response. Ensure the default mounts are enabled and writable. |
| 133 | + |
| 134 | +### Host Not Running in Service Mode |
| 135 | +If the host service is not running (or running without `--no-daemon`), the listener is not active. |
| 136 | +The command file will not be processed. |
| 137 | + |
| 138 | +### Container Not Running |
| 139 | +If the container is stopped, the listener exits. The command file will not be processed until the |
| 140 | +service is restarted. |
| 141 | + |
| 142 | +### Update Already Up To Date |
| 143 | +If opencode is already at the requested commit, the update command returns success and writes a |
| 144 | +result indicating no change. |
| 145 | + |
| 146 | +### Update Failure |
| 147 | +Any update failure writes a `"status": "error"` result with the failure message. The webapp should |
| 148 | +surface this to the user. |
| 149 | + |
| 150 | +## UI/UX Suggestions |
| 151 | + |
| 152 | +Recommended states for the webapp button: |
| 153 | +- Idle: show "Update opencode". |
| 154 | +- Pending: disable button, show "Updating..." and poll for result. |
| 155 | +- Success: show "Updated" with timestamp. |
| 156 | +- Error: show error message and allow retry. |
| 157 | + |
| 158 | +## Alternatives (Context) |
| 159 | + |
| 160 | +These are not implemented but are viable options for future work: |
| 161 | + |
| 162 | +1. **Host HTTP API endpoint** |
| 163 | + - A local `POST /api/v1/update` endpoint with JSON payload. |
| 164 | + - Easier streaming updates, but requires network exposure and auth. |
| 165 | + |
| 166 | +2. **Unix socket + proxy** |
| 167 | + - HTTP over a unix socket with a local reverse proxy in the container. |
| 168 | + - Strong local permissions but more moving parts. |
| 169 | + |
| 170 | +3. **Docker socket from container** |
| 171 | + - Webapp triggers a helper that calls Docker directly. |
| 172 | + - Not recommended due to high privileges in the container. |
0 commit comments