diff --git a/.agent/github/prs/origin-343/body.md b/.agent/github/prs/origin-343/body.md new file mode 100644 index 00000000..8aebcc2c --- /dev/null +++ b/.agent/github/prs/origin-343/body.md @@ -0,0 +1,109 @@ +--- +repo: arniwesth/motoko_agent +pr: 343 +branch: fix/ovis-herdr-config-dir +ticket: null +title: "fix(agent_sandbox): Ovis listens on all interfaces when herdr starts it" +--- + +## Summary + +After #342 Ovis still could not be reached from the Mac or the phone, for two reasons. + +- **Ovis listened on loopback only.** The container's start command writes `HOST=0.0.0.0` to + `~/.config/ovis/env`. herdr starts Ovis with `HERDR_PLUGIN_CONFIG_DIR` set to herdr's own config + directory for the plugin, and Ovis reads its settings from there. That directory was empty, so Ovis + defaulted to `127.0.0.1`, and the `ovis-relay` sidecar from #342 got an empty reply. +- **An old relay held the host port.** On the Mac, a hand-started socat still held `127.0.0.1:17317` + ahead of the relay's port forward. `agent.sh ovis` is meant to stop that process, but it missed it. + +This PR writes the settings where Ovis reads them, makes `agent.sh ovis` find and stop the old relay, and +says in the README which address to use on the host. + +## Changes + +- **`agent_sandbox/docker-compose.yml`:** the `agent` service's start command writes `env` into + `$(herdr plugin config-dir devswha.ovis)`, falling back to the literal + `~/.config/herdr/plugins/config/devswha.ovis`. + - `herdr plugin config-dir` needs no running server, and the command runs before herdr starts. + - Both ways Ovis starts read that file. herdr's `[[startup]]` entry passes the directory in + `HERDR_PLUGIN_CONFIG_DIR`. An `ovis start` from a shell, which is what `agent.sh ovis` runs, asks + `herdr plugin config-dir` and uses the directory once it holds an `env` file. + - It still fails closed: without `OVIS_TOKEN`, the file is created with no `HOST` line, and Ovis stays on + loopback. +- **`agent_sandbox/agent.sh`:** + - New `ovis_stray_socat`: finds a socat by the socket it listens on (`lsof … -sTCP:LISTEN -t`, then + `ps -o comm=`), with `pgrep -if "socat.*listen:"` as the fallback on a host without lsof. The old + check was `pgrep -f "socat TCP-LISTEN:"`. It misses Homebrew's binary, which is named `socat1`, + and any command line with options or a lowercase address before the listen address. + - `agent.sh ovis` stops such a process, then runs `compose restart ovis-relay` so the relay's port + forward takes the freed port. Only the relay restarts; the agent and its herdr sessions keep running. + - `agent.sh ovis status` names a socat that holds the port. + - If Ovis does not answer, the error message now names the right settings file and gives the restart + that keeps herdr sessions running: `agent.sh run ovis stop && agent.sh ovis`. +- **`agent_sandbox/README.md`:** on the host, open `http://127.0.0.1:17317`. It also says why the container + IP and port 7317 are the wrong address, and what happens to a hand-started relay. + +## Governing docs + +None directly. This corrects #340 and #342 in `.devcontainer/agent_sandbox/` and keeps their fail-closed +rule: Ovis leaves loopback only when `OVIS_TOKEN` is set. + +## Predicted outcome + +- A container created from this compose file runs herdr's Ovis on `0.0.0.0:7317` with no hand step. + - Check: inside the container, `/proc/net/tcp` shows `00000000:1C95` listening. + - Check: `curl http://ovis-relay:7317/api/health` returns 200. +- On a host where a socat holds 17317, `agent.sh ovis` stops it. Then `curl http://127.0.0.1:17317/api/health` + on the host returns 200. +- Without `OVIS_TOKEN`, Ovis stays on `127.0.0.1`. + +## Test evidence + +Run in the `agent_confined` container, plus host commands the operator ran on the Mac and pasted back. + +- **The first cause, measured inside the container (2026-11-21):** + - Ovis 0.5.9, started by herdr, ran with `HOST=127.0.0.1` and `HERDR_PLUGIN_CONFIG_DIR` set to + `~/.config/herdr/plugins/config/devswha.ovis`, an empty directory. `~/.config/ovis/env` held + `HOST=0.0.0.0`. + - `/proc/net/tcp` showed it listening on `0100007F:1C95` (127.0.0.1:7317). + - Through the relay, `curl http://ovis-relay:7317/api/health` got an empty reply. + - The Ovis binary explains it. It reads `env` and `.env` from `HERDR_PLUGIN_CONFIG_DIR`, and only from + `herdr plugin config-dir` or `~/.config/ovis` when that variable is unset. The plugin's `start` takes + `HOST` from what it read, defaulting to `127.0.0.1`, and passes it to the server it spawns. +- **The fix, applied by hand to the running container:** + - Wrote the same `env` file to herdr's directory, then restarted Ovis with herdr's own plugin actions + (`herdr plugin action invoke stop|start --plugin devswha.ovis`). + - Ovis came up on `HOST=0.0.0.0` and `00000000:1C95`, with `ovis status` showing + `config: …/devswha.ovis/env`. + - Through the relay, `/` returned 200 `text/html` and `/api/health` returned 200. `/api/sessions` with + no token returned 401. +- **The second cause, on the Mac (operator's output):** + - `curl http://127.0.0.1:17317/api/health` gave `Empty reply from server`. + - `curl http://:7317/api/health` returned 200. + - `lsof -nP -iTCP:17317 -sTCP:LISTEN` showed `socat1 … TCP 127.0.0.1:17317 (LISTEN)`, while + `docker ps` showed `ovis-relay` with `127.0.0.1:17317->7317/tcp`. + - The relay's log had no connection from the Mac. + - After stopping that socat, `docker restart` on the relay and `agent.sh ovis`, Ovis works on the Mac + and on the phone (operator's report). +- **The start script:** extracted from the compose file with Bun's YAML parser, `$$` unescaped, and run + under a scratch `HOME`: + - With a token: `…/herdr/plugins/config/devswha.ovis/env`, mode 600, holds `HOST=0.0.0.0` and + `PORT=7317`. + - Without a token: the same file, empty. + - With herdr not on `PATH`: the fallback path. +- **`ovis_stray_socat`:** tested with a copy of `python3` named `socat1`, listening on a spare port, with + the command line `socat1 … -d tcp-listen:,fork …`, which the old pattern does not match. The + function returned that PID, and returned nothing for Ovis's port 7317. This exercised the `pgrep` + fallback, because the container has no `lsof`. +- **Syntax:** `bash -n agent.sh` passes. +- **Not run:** + - The `lsof` path on macOS. The flags are the same `lsof -nP -iTCP:17317 -sTCP:LISTEN` the operator ran, + plus `-t`. + - `compose restart ovis-relay` after a socat is stopped. The operator ran `docker restart` on the relay + by hand. + - A container created from scratch from this compose file. Docker is not available inside the + container. + - `shellcheck`: not installed here. + +🤖 Generated with [Claude Code](https://claude.com/claude-code) diff --git a/.devcontainer/agent_sandbox/README.md b/.devcontainer/agent_sandbox/README.md index be811eeb..e9e0e181 100644 --- a/.devcontainer/agent_sandbox/README.md +++ b/.devcontainer/agent_sandbox/README.md @@ -147,6 +147,11 @@ herdr session has started it yet. Then it runs `tailscale serve` so a phone on y `https://..ts.net:7317`. `agent.sh ovis status` reports both halves and changes nothing. `agent.sh ovis off` stops serving to the tailnet. +On the host itself, open `http://127.0.0.1:17317`. The container's IP changes whenever the stack is +recreated, and 7317 is Ovis's port inside the container, not on the host. A socat relay started by hand +on 17317, from before `agent.sh ovis` existed, takes the port ahead of `ovis-relay`. `agent.sh ovis` +stops it, and `agent.sh ovis status` names it. + The container must be created with the token, so after adding it to `.env` run `agent.sh stop` first. That ends every herdr session. `agent.sh ovis` says so if it finds a container without the token. diff --git a/.devcontainer/agent_sandbox/agent.sh b/.devcontainer/agent_sandbox/agent.sh index 4b64fe10..458ae985 100755 --- a/.devcontainer/agent_sandbox/agent.sh +++ b/.devcontainer/agent_sandbox/agent.sh @@ -418,6 +418,25 @@ ovis_answers() { curl -fsS -m 3 -o /dev/null --noproxy '*' "http://127.0.0.1:$(ovis_host_port)/api/health" 2>/dev/null } +# PIDs of any socat listening on the host's port $1, space-separated: a relay started by hand before this +# command existed. It holds the port ahead of the ovis-relay container's forward (measured 2026-11-21 on the +# Mac: lsof showed `socat1` on 127.0.0.1:17317 and curl there got an empty reply, while the container +# answered). Found by its listening socket, not by its command line, which varies: Homebrew's binary is +# `socat1`, and options or a lowercase address can come before the listen address. pgrep is the fallback +# for a host without lsof. +ovis_stray_socat() { + local pid + if command -v lsof >/dev/null 2>&1; then + for pid in $(lsof -nP -iTCP:"$1" -sTCP:LISTEN -t 2>/dev/null); do + case "$(ps -o comm= -p "$pid" 2>/dev/null)" in + *socat*) printf '%s ' "$pid" ;; + esac + done + else + for pid in $(pgrep -if "socat.*listen:$1" 2>/dev/null); do printf '%s ' "$pid"; done + fi +} + # The https:// address `tailscale serve` is serving on OVIS_TAILNET_PORT, read from its own status output so # no JSON parser is needed (bash 3.2 and BSD userland are the floor here; jq is not guaranteed on a Mac). ovis_tailnet_url() { @@ -427,7 +446,7 @@ ovis_tailnet_url() { ovis_cmd() { local sub="${1:-on}" - local port ts url + local port ts url stray port="$(ovis_host_port)" ts="$(ovis_tailscale || true)" case "$sub" in @@ -442,12 +461,16 @@ Add a line like this to .env, then run this command again: (that value was generated just now; any long random string works, and every browser enters it once)" fi - # 2. The hand-started relay from before this command existed. It holds the host port compose needs, - # so the container cannot publish it while the relay runs. Named precisely so nothing else is hit. - if pgrep -f "socat TCP-LISTEN:${port}" >/dev/null 2>&1; then - echo "stopping the old socat relay on 127.0.0.1:${port} (compose publishes this port now)" >&2 - pkill -f "socat TCP-LISTEN:${port}" || true + # 2. The hand-started relay from before this command existed. It holds the host port ahead of the + # ovis-relay container's forward. Only a socat listening on this exact port is stopped, so nothing + # else is hit. The relay container is then restarted, so its forward takes the freed port. + stray="$(ovis_stray_socat "$port")" + if [[ -n "$stray" ]]; then + echo "stopping the old socat relay on 127.0.0.1:${port} (pid ${stray% }); the ${OVIS_RELAY_SERVICE} container publishes this port now" >&2 + # Unquoted on purpose: one argument per PID. + kill $stray 2>/dev/null || true sleep 1 + compose restart "$OVIS_RELAY_SERVICE" >/dev/null 2>&1 || true fi ensure_up @@ -478,9 +501,12 @@ token and stays on loopback. Recreate the container (this ends every herdr sessi fi ovis_answers || die "Ovis does not answer on http://127.0.0.1:${port}. Look inside the container: $(basename "$0") run ovis status - and its settings file: $(basename "$0") run cat /home/${CONTAINER_USER}/.config/ovis/env + and its settings file: $(basename "$0") run cat /home/${CONTAINER_USER}/.config/herdr/plugins/config/devswha.ovis/env (the file should hold HOST=0.0.0.0 and PORT=7317; docker-compose.yml's command: writes them at start) - and the relay's log: ${COMPOSE[*]} logs ${OVIS_RELAY_SERVICE}" + and the relay's log: ${COMPOSE[*]} logs ${OVIS_RELAY_SERVICE} +If the file is right but Ovis started before it was, Ovis listens on the container's loopback only. +Restart Ovis, which leaves the herdr sessions running: + $(basename "$0") run ovis stop && $(basename "$0") ovis" echo "Ovis answers on http://127.0.0.1:${port} (this machine only)" # 5. HTTPS on the tailnet. Turned off first so a stale target (the old relay, a container IP) is @@ -509,6 +535,8 @@ your tailnet in the Tailscale admin console (DNS page), then run this again." # Read-only: starts nothing, changes nothing. if ovis_answers; then echo "local: http://127.0.0.1:${port} answers" else echo "local: http://127.0.0.1:${port} does not answer"; fi + stray="$(ovis_stray_socat "$port")" + [[ -z "$stray" ]] || echo " a hand-started socat (pid ${stray% }) holds 127.0.0.1:${port} ahead of the ${OVIS_RELAY_SERVICE} container; $(basename "$0") ovis stops it" if [[ -z "$ts" ]]; then echo "tailnet: Tailscale CLI not found" else url="$(ovis_tailnet_url "$ts")" diff --git a/.devcontainer/agent_sandbox/docker-compose.yml b/.devcontainer/agent_sandbox/docker-compose.yml index 4b4dbcf6..e813b6fe 100644 --- a/.devcontainer/agent_sandbox/docker-compose.yml +++ b/.devcontainer/agent_sandbox/docker-compose.yml @@ -53,16 +53,24 @@ services: # 200). So HOST=0.0.0.0 is written only when the token is there; without one, any earlier HOST/PORT is # removed and Ovis stays on the container's loopback, which the ovis-relay service below cannot reach. # - # Written to ~/.config/ovis/env, Ovis's own settings file, and NOT set under `environment:`: HOST and PORT - # there would apply to every process in the container. Runs at every container start, before agent.sh - # starts the herdr server whose [[startup]] entry starts Ovis, so the file is in place when Ovis reads it. + # Written to the `env` file in herdr's config directory for the Ovis plugin, and NOT set under + # `environment:`: HOST and PORT there would apply to every process in the container. That directory is + # the one Ovis reads. herdr starts Ovis through the plugin's [[startup]] entry with + # HERDR_PLUGIN_CONFIG_DIR set to it, and an `ovis start` from a shell asks `herdr plugin config-dir` for it + # and uses it once it holds an `env`. ~/.config/ovis/env, where this used to write, is read only when + # that directory has none, so herdr's Ovis never saw HOST and listened on 127.0.0.1 (measured on 0.5.9, + # 2026-11-21: the server ran with HOST=127.0.0.1 and the relay got an empty reply). `herdr plugin + # config-dir` needs no running server; the literal path is the fallback. Runs at every container start, + # before agent.sh starts the herdr server, so the file is in place when Ovis reads it. # `$$` is compose's escape for a literal `$`: the shell, not compose, expands OVIS_TOKEN. command: - sh - -c - | - mkdir -p "$$HOME/.config/ovis" - env_file="$$HOME/.config/ovis/env" + cfg_dir="$$(herdr plugin config-dir devswha.ovis 2>/dev/null)" + [ -n "$$cfg_dir" ] || cfg_dir="$$HOME/.config/herdr/plugins/config/devswha.ovis" + mkdir -p "$$cfg_dir" + env_file="$$cfg_dir/env" touch "$$env_file" && chmod 600 "$$env_file" sed -i '/^HOST=/d; /^PORT=/d' "$$env_file" if [ -n "$${OVIS_TOKEN:-}" ]; then printf 'HOST=0.0.0.0\nPORT=7317\n' >> "$$env_file"; fi