From 6059bec1e4b59e10e60eab62daf485a0fdf1e632 Mon Sep 17 00:00:00 2001 From: Denton Social Date: Sat, 26 Sep 2026 18:33:57 -0500 Subject: [PATCH] gateway: run simplex-chat in bot mode, not relay/headless In v7 --headless means "chat relay" (requires --relay) and yields a relay address, not a user contact address, so the bot could not be added as a contact. Run bot mode instead (-p PORT --create-bot-display-name), which needs no TTY and is restart-safe. simplex-address.py now creates the user contact address on first run (/_address + /_show_address), and bootstrap invokes it after the services start and prints the link. --- AGENTS.md | 27 ++++++++++------- scripts/bootstrap.sh | 24 +++++++++++++-- scripts/simplex-address.py | 38 +++++++++++++++--------- scripts/systemd/semif-simplex.service.in | 7 +++-- 4 files changed, 67 insertions(+), 29 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 8356767..2d8b255 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -378,13 +378,14 @@ unit tests (24) + box integration tests (2). `data/` for continuity; `--public-dashboard` binds the dashboard to `0.0.0.0`. It also enables the SimpleX gateway (`gateway.simplex.enabled = true`, `ws_url` from `simplex_chat.port`) and renders/enables the two user services - `semif-simplex.service` (the pinned `simplex-chat` daemon, `--relay - --headless` on `simplex_chat.port` — v7 requires `--relay` for `--headless`) - and `semif-gateway.service` + `semif-simplex.service` (the pinned `simplex-chat` bot daemon, + `--create-bot-display-name` on `simplex_chat.port`) and `semif-gateway.service` (`.runtime/venv/bin/python -m semif_agent.cli gateway`). `--simplex-allowed-users CSV` / `--simplex-home-channel ID` / `--simplex-display-name NAME` populate the allowlist/fallback/identity; with an empty allowlist the gateway denies - everyone (the safe default until the human adds their contact id). + everyone (the safe default until the human adds their contact id). Bootstrap + then runs `scripts/simplex-address.py` to create/print the bot's contact + address. - Run / verify on jarvis: ```sh REPO=~/repos/semif-agent && cd "$REPO" && export HF_HOME="$REPO/.runtime/hf" @@ -576,16 +577,20 @@ unit tests (24) + box integration tests (2). co-serves the browser UI from a daemon thread. Config lives under `gateway.simplex` in `config.json`; `enabled` defaults false. On a bootstrap-provisioned box `scripts/bootstrap.sh` renders and enables the - `semif-simplex.service` daemon (pinned `simplex-chat`, profile under - `.runtime/simplex/`, port from `simplex_chat.port`) and the - `semif-gateway.service` agent process, so the gateway runs headless across + `semif-simplex.service` bot daemon (pinned `simplex-chat` in **bot mode**, + profile under `.runtime/simplex/`, port from `simplex_chat.port`) and the + `semif-gateway.service` agent process, so the gateway runs across logout/reboot. The gateway is its own process — the REPL and the gateway are independent front ends onto the same on-disk logs/registry (do not run two scheduler processes over one skill store concurrently). -- **Bot address.** `python scripts/simplex-address.py` queries the running - daemon for the bot's contact link (`/_show_address `), which a human - adds in their SimpleX app to start a DM. `bootstrap.sh` prints this hint; the - address is created automatically on the daemon's first `--headless` run. +- **Bot address / v7 relay gotcha.** The daemon must NOT run with `--headless` + or `--relay`: in SimpleX Chat v7 `--headless` means "chat relay" (requires + `--relay`) and yields a *relay* address, not a user contact address. Run bot + mode instead (`simplex-chat -p PORT --create-bot-display-name NAME`), which + needs no TTY and is restart-safe. `python scripts/simplex-address.py` then + shows (creating on first run, via `/_address` / `/_show_address `) + the bot's user contact link, which a human adds in their SimpleX app to start + a DM. `bootstrap.sh` prints it at the end of provisioning. - **Transport contract** (`gateway/base.py`): `GatewayAdapter.run(on_inbound, outbound_queue)` blocks, delivering `InboundMessage`s and draining a stdlib `queue.Queue[OutboundMessage | None]`. Scheduler work is synchronous and can diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh index 9409bfa..f35b928 100755 --- a/scripts/bootstrap.sh +++ b/scripts/bootstrap.sh @@ -319,6 +319,25 @@ if command -v systemctl >/dev/null 2>&1; then fi fi +# --- stage 8b: SimpleX bot contact address ------------------------------------------- +SIMPLEX_ADDRESS="" +if command -v systemctl >/dev/null 2>&1; then + export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}" + if systemctl --user is-active --quiet semif-simplex.service 2>/dev/null; then + echo "== ensuring the SimpleX bot contact address exists" + for _ in 1 2 3 4 5 6 7 8 9 10; do + if SIMPLEX_ADDRESS="$("$PYTHON" "$REPO_ROOT/scripts/simplex-address.py" 2>/dev/null)"; then + printf '%s\n' "$SIMPLEX_ADDRESS" | sed 's/^/ /' + break + fi + sleep 2 + done + if [[ -z "$SIMPLEX_ADDRESS" ]]; then + echo "WARN: bot address not ready; run: $PYTHON scripts/simplex-address.py" >&2 + fi + fi +fi + # --- stage 9: optional data copy ----------------------------------------------------- if [[ -n "$COPY_DATA" ]]; then echo "== rsync data from $COPY_DATA" @@ -365,12 +384,13 @@ Done. Next steps: HF_HOME="$HF_CACHE" "$PYTHON" -m semif_agent.cli dashboard --port 8765 SimpleX gateway: - - The bot runs as systemd user services 'semif-simplex' (daemon, port $SIMPLEX_PORT) and + - The bot runs as systemd user services 'semif-simplex' (bot daemon, port $SIMPLEX_PORT) and 'semif-gateway' (agent). Check them with: systemctl --user status semif-simplex semif-gateway journalctl --user -u semif-gateway -f - - Show the bot's contact address (add it as a contact in your SimpleX app): + - Its user contact address is printed above; re-print (creating if needed) with: "$PYTHON" scripts/simplex-address.py + Add that address as a contact in your SimpleX app. - Then put your contactId/display name in config.json gateway.simplex.allowed_users (discover the id from a 'gateway_denied' trace event or the daemon's /contacts) and reply. With an empty allowlist the diff --git a/scripts/simplex-address.py b/scripts/simplex-address.py index 42d573c..f6569a7 100644 --- a/scripts/simplex-address.py +++ b/scripts/simplex-address.py @@ -1,10 +1,12 @@ #!/usr/bin/env python3 -"""Print the SimpleX bot's contact address from the running gateway daemon. +"""Print (creating if needed) the SimpleX bot's contact address. -The gateway talks to a local `simplex-chat` daemon (see simplex_chat in -config.json). To connect a phone/client to the bot you need the bot's contact -address; this queries it over the daemon's WebSocket API and prints the short -link (and full link). Requires the daemon to be running. +The gateway talks to a local `simplex-chat` daemon running as a bot +(`--create-bot-display-name`, see simplex_chat in config.json). To connect a +phone/client to the bot you need the bot's *user* contact address, not a chat +relay address; this queries the daemon's WebSocket API, creating the address on +first run, and prints the short link (and full link). Requires the daemon to be +running. Usage: .runtime/venv/bin/python scripts/simplex-address.py [--config config.json] @@ -29,18 +31,26 @@ def _ws_url(config_path: str) -> str: return str(cfg.get("gateway", {}).get("simplex", {}).get("ws_url") or "ws://127.0.0.1:5226") -async def _show(ws_url: str, user_id: int) -> dict: +async def _resolve(ws_url: str, user_id: int) -> dict: try: import websockets except ImportError: raise SystemExit("the `websockets` package is required (pip install websockets)") async with websockets.connect(ws_url, max_size=None) as ws: - await ws.send(json.dumps({"corrId": "addr", "cmd": f"/_show_address {user_id}"})) - raw = await asyncio.wait_for(ws.recv(), timeout=10) - resp = json.loads(raw).get("resp", {}) or {} - if resp.get("type") != "userContactLink": - raise SystemExit(f"could not read address: {json.dumps(resp)[:300]}") - return resp.get("contactLink", {}) or {} + + async def cmd(command: str) -> dict: + await ws.send(json.dumps({"corrId": "addr", "cmd": command})) + raw = await asyncio.wait_for(ws.recv(), timeout=20) + return json.loads(raw).get("resp", {}) or {} + + resp = await cmd(f"/_show_address {user_id}") + if resp.get("type") == "userContactLink": + return resp.get("contactLink", {}) or {} + # No address yet -> create one (APICreateMyAddress). + resp = await cmd(f"/_address {user_id}") + if resp.get("type") == "userContactLinkCreated": + return {"connLinkContact": resp.get("connLinkContact", {}) or {}} + raise SystemExit(f"could not read or create address: {json.dumps(resp)[:300]}") def main() -> int: @@ -52,7 +62,7 @@ def main() -> int: ws_url = args.ws_url or _ws_url(args.config) try: - link = asyncio.run(_show(ws_url, args.user_id)) + link = asyncio.run(_resolve(ws_url, args.user_id)) except OSError as exc: print(f"cannot reach simplex-chat at {ws_url}: {exc}", file=sys.stderr) return 1 @@ -60,7 +70,7 @@ def main() -> int: print(f"simplex-chat at {ws_url} did not answer", file=sys.stderr) return 1 - conn = link.get("connLinkContact", {}) or {} + conn = link.get("connLinkContact", {}) or link short = conn.get("connShortLink") full = conn.get("connFullLink") if short: diff --git a/scripts/systemd/semif-simplex.service.in b/scripts/systemd/semif-simplex.service.in index a91fe5f..f889f69 100644 --- a/scripts/systemd/semif-simplex.service.in +++ b/scripts/systemd/semif-simplex.service.in @@ -1,11 +1,14 @@ [Unit] -Description=SemIf agent SimpleX Chat daemon +Description=SemIf agent SimpleX Chat bot daemon After=network-online.target Wants=network-online.target [Service] Type=simple -ExecStart=@REPO@/.runtime/bin/simplex-chat -p @PORT@ -d @REPO@/.runtime/simplex/simplex_v1 -k "" --relay --headless --user-display-name "@DISPLAY_NAME@" +# Bot mode: a local WebSocket chat server with a bot user. Do NOT add +# --headless here — in SimpleX Chat v7 --headless means "chat relay" and +# requires --relay, which yields a relay address, not a user contact address. +ExecStart=@REPO@/.runtime/bin/simplex-chat -p @PORT@ -d @REPO@/.runtime/simplex/simplex_v1 -k "" --create-bot-display-name "@DISPLAY_NAME@" Restart=always RestartSec=10 StandardOutput=journal