m64_9d6a8d0db091 / start.sh
37 lines, 1.9 KBraw
1#!/usr/bin/env bash2# opencode-inside entrypoint. Runs inside the container; takes no arguments.3# opencode serve on 0.0.0.0:4096, then the control gateway on $M64_PORT. Port 4096 is4# [runtime.app]: m64 exposes it (the full opencode web UI and API) on a second front5# port. 0.0.0.0 is inside the container: only the gate can reach its internal network.6set -eu78echo '{"phase":"loading","detail":"starting opencode"}' >> "$M64_STATUS"910# HOME is the run's /scratch: opencode's DB, caches and every session dir go with the run.11export HOME=/scratch/home12export WORK_DIR=/scratch/work13mkdir -p "$HOME" "$WORK_DIR/sessions"14# Packages opencode installs on first start, warmed at build time (at run time only15# openrouter.ai is reachable).16cp -rn /opt/opencode-capsule/. "$HOME/"1718export OPENCODE_CONFIG=/capsule/opencode.json19# opencode is a Bun binary: Bun's fetch honours HTTP_PROXY / NO_PROXY from the environment20# (set by m64 to the gate). The provider's baseURL is plain http://openrouter.ai: the gate21# adds the OpenRouter key and sends the request on over TLS, so the key never enters here.22# $OPENROUTER_API_KEY only holds the placeholder m64-injected:OPENROUTER_API_KEY.23# Run from $WORK_DIR: the web UI's (and `opencode attach`'s) project is the run's scratch24# workspace. The gateway still gives each of its sessions a directory under sessions/.25cd "$WORK_DIR"26opencode serve --hostname 0.0.0.0 --port 4096 --pure &27OPENCODE_PID=$!2829# If opencode dies, take the container down so m64 reports the error. $$ is the gateway30# after the exec below. The gateway never reaps opencode, so a dead opencode lingers as a31# zombie (state Z): test the state, not just whether the pid exists.32alive() { [ -r "/proc/$1/stat" ] && [ "$(cut -d')' -f2 "/proc/$1/stat" | cut -d' ' -f2)" != Z ]; }33( while alive "$OPENCODE_PID"; do sleep 5; done34 echo '{"phase":"error","detail":"opencode exited"}' >> "$M64_STATUS"35 kill -TERM $$ ) &3637exec /opt/gateway/bin/python /capsule/agent_gateway.py