Files
marcopanandClaude Opus 4.8 3453f3ae23 feat: pre-check DWH reachability before creating a session (local dev only)
New session now refuses to spawn a Pi runtime that would only die in bootstrap
retrieval when the DWH/vector host is unreachable (e.g. a dropped VPN). Before
`session new`, POST /sessions probes the DWH via `tht db ping`; if it is down it
returns 503 {code:"dwh_unreachable"} with a clear message and creates nothing.

- Gated behind the THT_DWH_PRECHECK flag (default off), enabled only by the local
  dev launcher (run-stack.sh) — containers/CI never pay the probe, and existing
  tests that don't set it are unaffected.
- ThtRunner.dbPing() runs `tht db ping` with a 10s timeout (run() gains an optional
  timeout that SIGKILLs a hung child).
- Frontend: apiFetch throws a typed ApiError (status + parsed payload); the new-
  session composer shows the specific alert on `dwh_unreachable` instead of the
  generic retry hint, keeping the question for retry.

Verified live on an isolated backend (precheck on + broken DWH host → 503
dwh_unreachable, no session created) and via unit tests (backend 228, frontend 308).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-18 12:08:47 +02:00

69 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# run-stack.sh — avvia i 3 layer reali di ThothII per la validazione end-to-end.
#
# frontend (browser) → backend (Fastify :8787) → pi --mode rpc (GLM 5.2) → tht/harness → DWH
#
# Prerequisiti: VPN attiva, `pi` su PATH (GLM 5.2 configurato), harness/.env popolato,
# harness/config/tht.yaml -> workspace cliente, deps installate nei 3 progetti.
#
# Uso: ./scripts/run-stack.sh
# Stop: Ctrl-C (termina backend + frontend; i processi pi figli del backend muoiono con esso).
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
HARNESS="$ROOT/harness"
BACKEND="$ROOT/backend"
FRONTEND="$ROOT/frontend"
THT_BIN="$HARNESS/.venv/bin/tht"
BACKEND_PORT="${BACKEND_PORT:-8787}"
FRONTEND_PORT="${FRONTEND_PORT:-5173}"
# --- preflight ---------------------------------------------------------------
[ -f "$HARNESS/.env" ] || { echo "ERRORE: $HARNESS/.env mancante (credenziali DWH/vector)"; exit 1; }
[ -x "$THT_BIN" ] || { echo "ERRORE: $THT_BIN non trovato (esegui: cd harness && python -m venv .venv && pip install -e .)"; exit 1; }
command -v pi >/dev/null || { echo "ERRORE: 'pi' non sul PATH (configura @earendil-works/pi-coding-agent con GLM 5.2)"; exit 1; }
[ -e "$HARNESS/config/tht.yaml" ] || { echo "ERRORE: $HARNESS/config/tht.yaml mancante (symlink al workspace)"; exit 1; }
# Carica le variabili del DWH/vector nell'ambiente: il backend le passa a pi e a tht.
set -a; . "$HARNESS/.env"; set +a
# tht deve essere raggiungibile dal processo pi che il backend spawna.
export PATH="$HARNESS/.venv/bin:$PATH"
echo "== ThothII stack =="
echo " harness : $HARNESS (tht: $THT_BIN)"
echo " backend : http://localhost:$BACKEND_PORT"
echo " frontend: http://localhost:$FRONTEND_PORT"
echo " pi : $(command -v pi)"
echo
# --- avvio -------------------------------------------------------------------
pids=()
cleanup() { echo; echo "Arresto stack..."; for p in "${pids[@]}"; do kill "$p" 2>/dev/null || true; done; }
trap cleanup EXIT INT TERM
# Backend: usa il pi reale + il tht del venv, cwd harness per lo spawn di pi.
(
cd "$BACKEND"
PORT="$BACKEND_PORT" \
THT_HARNESS_DIR="$HARNESS" \
THT_BIN="$THT_BIN" \
PI_BIN="pi" \
AUTH_MODE="none" \
THT_DWH_PRECHECK="1" \
npm run dev
) &
pids+=($!)
# Frontend: punta al backend reale.
(
cd "$FRONTEND"
VITE_BACKEND_URL="http://localhost:$BACKEND_PORT" \
npm run dev -- --port "$FRONTEND_PORT"
) &
pids+=($!)
echo "Stack avviato. Apri http://localhost:$FRONTEND_PORT e crea una nuova domanda."
echo "Ctrl-C per fermare."
wait