|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Runtime smoke-test for baudbot agent lifecycle in CI droplets. |
| 3 | +# |
| 4 | +# Verifies that: |
| 5 | +# - baudbot starts successfully |
| 6 | +# - control-agent session socket is created and reachable |
| 7 | +# - session-control RPC responds successfully |
| 8 | +# - bridge supervisor status artifact exists |
| 9 | +# - process remains healthy for a short stabilization window |
| 10 | +# - baudbot stops cleanly |
| 11 | + |
| 12 | +set -Eeuo pipefail |
| 13 | + |
| 14 | +readonly AGENT_USER="baudbot_agent" |
| 15 | +readonly AGENT_HOME="/home/${AGENT_USER}" |
| 16 | +readonly CONTROL_DIR="${AGENT_HOME}/.pi/session-control" |
| 17 | +readonly CONTROL_ALIAS="${CONTROL_DIR}/control-agent.alias" |
| 18 | +readonly BRIDGE_STATUS_FILE="${AGENT_HOME}/.pi/agent/slack-bridge-supervisor.json" |
| 19 | +readonly START_TIMEOUT_SECONDS=60 |
| 20 | +readonly STABILIZE_SECONDS=20 |
| 21 | + |
| 22 | +started=0 |
| 23 | + |
| 24 | +log() { |
| 25 | + printf '[runtime-smoke] %s\n' "$*" |
| 26 | +} |
| 27 | + |
| 28 | +require_cmd() { |
| 29 | + local cmd="$1" |
| 30 | + command -v "$cmd" >/dev/null 2>&1 || { |
| 31 | + log "missing required command: ${cmd}" |
| 32 | + exit 2 |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +cleanup() { |
| 37 | + local exit_code=$? |
| 38 | + if [[ $started -eq 1 ]]; then |
| 39 | + log "cleanup: attempting to stop baudbot" |
| 40 | + sudo baudbot stop >/dev/null 2>&1 || true |
| 41 | + fi |
| 42 | + exit "$exit_code" |
| 43 | +} |
| 44 | +trap cleanup EXIT |
| 45 | + |
| 46 | +wait_for_control_socket() { |
| 47 | + local deadline=$((SECONDS + START_TIMEOUT_SECONDS)) |
| 48 | + local target="" |
| 49 | + |
| 50 | + while (( SECONDS < deadline )); do |
| 51 | + if [[ -L "$CONTROL_ALIAS" ]]; then |
| 52 | + target="$(readlink -- "$CONTROL_ALIAS" 2>/dev/null || true)" |
| 53 | + if [[ -n "$target" ]]; then |
| 54 | + if [[ "$target" != /* ]]; then |
| 55 | + target="${CONTROL_DIR}/${target}" |
| 56 | + fi |
| 57 | + if [[ -S "$target" ]]; then |
| 58 | + printf '%s\n' "$target" |
| 59 | + return 0 |
| 60 | + fi |
| 61 | + fi |
| 62 | + fi |
| 63 | + sleep 1 |
| 64 | + done |
| 65 | + |
| 66 | + return 1 |
| 67 | +} |
| 68 | + |
| 69 | +probe_rpc_get_message() { |
| 70 | + local socket_path="$1" |
| 71 | + sudo -u "$AGENT_USER" python3 - "$socket_path" <<'PY' |
| 72 | +import json |
| 73 | +import socket |
| 74 | +import sys |
| 75 | +
|
| 76 | +sock_path = sys.argv[1] |
| 77 | +request = {"type": "get_message", "id": "ci-runtime-smoke"} |
| 78 | +
|
| 79 | +client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 80 | +try: |
| 81 | + client.settimeout(4) |
| 82 | + client.connect(sock_path) |
| 83 | + client.sendall((json.dumps(request) + "\n").encode("utf-8")) |
| 84 | +
|
| 85 | + buf = b"" |
| 86 | + while b"\n" not in buf: |
| 87 | + chunk = client.recv(4096) |
| 88 | + if not chunk: |
| 89 | + break |
| 90 | + buf += chunk |
| 91 | +
|
| 92 | + if not buf: |
| 93 | + print("empty rpc response", file=sys.stderr) |
| 94 | + sys.exit(1) |
| 95 | +
|
| 96 | + line = buf.split(b"\n", 1)[0].decode("utf-8", errors="replace") |
| 97 | + response = json.loads(line) |
| 98 | + if not response.get("success", False): |
| 99 | + print(f"rpc reported failure: {response}", file=sys.stderr) |
| 100 | + sys.exit(1) |
| 101 | +
|
| 102 | + print("rpc-ok") |
| 103 | +finally: |
| 104 | + client.close() |
| 105 | +PY |
| 106 | +} |
| 107 | + |
| 108 | +main() { |
| 109 | + require_cmd sudo |
| 110 | + require_cmd baudbot |
| 111 | + require_cmd python3 |
| 112 | + |
| 113 | + log "starting baudbot" |
| 114 | + sudo baudbot start |
| 115 | + started=1 |
| 116 | + |
| 117 | + log "waiting for control-agent socket" |
| 118 | + local socket_path="" |
| 119 | + if ! socket_path="$(wait_for_control_socket)"; then |
| 120 | + log "control-agent socket did not become ready within ${START_TIMEOUT_SECONDS}s" |
| 121 | + sudo baudbot status || true |
| 122 | + exit 1 |
| 123 | + fi |
| 124 | + log "control socket ready: ${socket_path}" |
| 125 | + |
| 126 | + log "probing session-control RPC" |
| 127 | + probe_rpc_get_message "$socket_path" |
| 128 | + |
| 129 | + log "checking bridge supervisor status file" |
| 130 | + if [[ ! -f "$BRIDGE_STATUS_FILE" ]]; then |
| 131 | + log "missing bridge supervisor status file: ${BRIDGE_STATUS_FILE}" |
| 132 | + sudo baudbot status || true |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | + |
| 136 | + log "stabilization window (${STABILIZE_SECONDS}s)" |
| 137 | + sleep "$STABILIZE_SECONDS" |
| 138 | + |
| 139 | + log "verifying runtime health via baudbot status" |
| 140 | + sudo baudbot status >/dev/null |
| 141 | + |
| 142 | + log "stopping baudbot" |
| 143 | + sudo baudbot stop |
| 144 | + started=0 |
| 145 | + |
| 146 | + log "runtime smoke passed" |
| 147 | +} |
| 148 | + |
| 149 | +main "$@" |
0 commit comments