From 3229de9b3b8b31b85612d87bf6ab429f873d5d11 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 16 Oct 2026 22:25:09 +1100 Subject: [PATCH] fix(e2e): recover reliable local stack startup --- e2e/README.md | 12 +++++-- e2e/run.cjs | 14 +++++++- e2e/seed.sh | 21 +++++++++-- e2e/stack.sh | 40 +++++++++++++++++++-- internal/app/archiegateway/curators.go | 30 ++++++++++++++-- internal/app/archiegateway/curators_test.go | 13 +++++++ internal/app/archiegateway/gateway.go | 18 ++-------- 7 files changed, 120 insertions(+), 28 deletions(-) create mode 100644 internal/app/archiegateway/curators_test.go diff --git a/e2e/README.md b/e2e/README.md index dd85b07e5..bca99f7fc 100644 --- a/e2e/README.md +++ b/e2e/README.md @@ -24,7 +24,15 @@ task e2e:down # stop; e2e/stack.sh reset also deletes the data Nothing reads the installed config: `stack.sh` points the XDG directories at `${ARCHIE_DEV_DIR:-~/.local/state/archie-dev}` and writes its overlay from `deployments/dev.toml`. Logs are in its `logs/`. `e2e/stack.sh restart archied` -restarts one service. +restarts one service. Run `task e2e:down` before starting again: startup refuses +occupied service ports and checks that each launched process stays alive. + +The seed creates a local test administrator: `operator@example.test`, password +`archie-local-e2e-password`. Use it at `http://localhost:5173`; the browser +checks sign in with the same account. These are test fixtures for this +loopback-only stack. To use an existing test account, set `ARCHIE_E2E_EMAIL` +and `ARCHIE_E2E_PASSWORD` for both seeding and browser checks. Seeding never +resets an existing account or deletes test data. The first run needs `npm ci --prefix e2e` and `npx --prefix e2e playwright install chromium`. In the cloud environment, Chromium and Playwright are @@ -55,7 +63,7 @@ trailing or doubled spaces. `seeded` checks the seed is visible. | Symptom | Cause | Fix | | --- | --- | --- | | State Store exits: server is older than PostgreSQL 18 | another server answered | `stack.sh` uses port 55432; set `ARCHIE_DEV_PGPORT` if that is taken | -| Work request: repository is not configured | archied reads repository policies only at boot | `seed.sh` restarts it; do the same after changing policies | +| Startup says a port is in use | a stack or another process already owns it | Run `task e2e:down`, then `task e2e:up`; inspect any remaining listener with `ss -ltnp` | | Your own shell is killed (exit 144) | `pkill -f` matched its own command line | `stack.sh stop` or `restart`, which kill by PID | | `npx shadcn-vue add` cannot fetch the registry | Node's fetch ignores the cloud agent proxy | `curl` the registry JSON and copy the files | | Postgres aborts in the cloud | `/tmp/claude-0` is reset to mode 700 | `stack.sh` keeps the root cluster in `/var/lib/archie-dev` | diff --git a/e2e/run.cjs b/e2e/run.cjs index ed53ebe9c..510babcfa 100644 --- a/e2e/run.cjs +++ b/e2e/run.cjs @@ -21,11 +21,23 @@ fs.mkdirSync(out, { recursive: true }); (async () => { const browser = await chromium.launch(); + const context = await browser.newContext({ viewport: { width: 1440, height: 900 } }); + const login = await context.request.post(base + '/api/auth/login', { + headers: { 'X-Archie-CSRF': '1', Origin: base }, + data: { email: process.env.ARCHIE_E2E_EMAIL || 'operator@example.test', password: process.env.ARCHIE_E2E_PASSWORD || 'archie-local-e2e-password' }, + }); + if (!login.ok()) { + const error = `Test account sign-in failed (${login.status()}): ${await login.text()}`; + await browser.close(); + throw new Error(error); + } + const storageState = await context.storageState(); + await context.close(); const results = []; for (const name of names) { const check = require(path.join(__dirname, 'checks', name + '.cjs')); // A fresh page per check: open event streams pile up across navigations. - const page = await browser.newPage({ viewport: { width: 1440, height: 900 } }); + const page = await browser.newPage({ viewport: { width: 1440, height: 900 }, storageState }); const errors = []; const notes = []; page.on('pageerror', (e) => errors.push(String(e))); diff --git a/e2e/seed.sh b/e2e/seed.sh index 6af35703b..a17e69ef2 100755 --- a/e2e/seed.sh +++ b/e2e/seed.sh @@ -6,13 +6,28 @@ set -euo pipefail base=${ARCHIE_E2E_API:-http://localhost:8484} here=$(cd "$(dirname "$0")" && pwd) -H=(-H 'Content-Type: application/json' -H 'X-Archie-CSRF: 1' -H "Origin: $base") +cookie=$(mktemp) +trap 'rm -f "$cookie"' EXIT +H=(-b "$cookie" -c "$cookie" -H 'Content-Type: application/json' -H 'X-Archie-CSRF: 1' -H "Origin: $base") -get() { curl -fsS "$base$1"; } +get() { curl -fsS -b "$cookie" "$base$1"; } post() { curl -fsS -X POST "$base$1" "${H[@]}" -d "$2"; } # jq-free JSON reads: py EXPR, with the parsed body as d. py() { python3 -c "import sys, json; d = json.load(sys.stdin); print($1)"; } +# This account belongs only to the isolated local test stack. +email=${ARCHIE_E2E_EMAIL:-operator@example.test} +password=${ARCHIE_E2E_PASSWORD:-archie-local-e2e-password} +account=$(python3 -c 'import json,sys; print(json.dumps(dict(email=sys.argv[1], password=sys.argv[2], display_name="E2E operator", organisation_name="E2E", workspace_name="Local test", public_url=sys.argv[3])))' "$email" "$password" "${ARCHIE_E2E_URL:-http://localhost:5173}") +if [ "$(get /api/auth/status | py 'd["setup_required"]')" = "True" ]; then + post /api/auth/setup "$account" >/dev/null +else + if ! post /api/auth/login "$account" >/dev/null; then + echo "Test account sign-in failed. Set ARCHIE_E2E_EMAIL and ARCHIE_E2E_PASSWORD to this test stack's account credentials." >&2 + exit 1 + fi +fi + # Repository policy. archied applies it live; the pause lets the watch publish it. if ! get /api/control-plane/resources/repository-policies | grep -q '"widgets"'; then version=$(get /api/control-plane/resources/repository-policies | py 'd["resource"]["version"]') @@ -36,7 +51,7 @@ if ! get /api/sources | grep -q "\"$src\""; then post /api/sources "{\"path\":\"$src\"}" >/dev/null post "/api/sources/$src/signing" '{"signed":false}' >/dev/null post "/api/sources/$src/approve-unsigned" '{}' >/dev/null - bash "$here/../tools/webhook-send.sh" --url "$base" --source "$src" --kind github-issue >/dev/null + bash "$here/../tools/webhook-send.sh" --url "$base" --source "$src" --kind github-issue --quiet >/dev/null fi et=$(get /api/event-types | py '"".join(t["id"] for t in (d.get("event_types", []) if isinstance(d, dict) else d) if t["name"] == "issue-opened")') if [ -z "$et" ]; then diff --git a/e2e/stack.sh b/e2e/stack.sh index 6f4863a52..eb94ca116 100755 --- a/e2e/stack.sh +++ b/e2e/stack.sh @@ -4,7 +4,7 @@ # the installed config: XDG paths point into the dev directory. # # e2e/stack.sh start | stop | restart | status | reset -set -euo pipefail +set -Eeuo pipefail repo=$(cd "$(dirname "$0")/.." && pwd) dir=${ARCHIE_DEV_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/archie-dev} @@ -34,8 +34,17 @@ pgbin() { exit 1 } +check_process() { + local pid + if [ ! -f "$dir/pids/$1" ] || ! pid=$(cat "$dir/pids/$1") || ! kill -0 "$pid" 2>/dev/null; then + echo "$1 exited; see $dir/logs/$1.log" >&2 + return 1 + fi +} + wait_port() { for _ in $(seq 1 "${3:-60}"); do + check_process "$2" || return 1 (echo >"/dev/tcp/127.0.0.1/$1") 2>/dev/null && return 0 sleep 1 done @@ -75,8 +84,15 @@ prepare() { spawn() { local name=$1 cwd=$2 shift 2 + rm -f "$dir/pids/$name" setsid -f bash -c 'cd "$1" && echo $$ >"$2" && shift 2 && exec "$@"' _ "$cwd" "$dir/pids/$name" "$@" \ - "$dir/logs/$name.log" 2>&1 + "$dir/logs/$name.log" 2>&1 9>&- + for _ in $(seq 1 50); do + [ ! -f "$dir/pids/$name" ] || { check_process "$name"; return; } + sleep 0.1 + done + echo "$name did not start; see $dir/logs/$name.log" >&2 + return 1 } start_service() { @@ -95,16 +111,29 @@ stop_pid() { } start() { + local port + for port in 9091 8585 8586 8485 8484 5173; do + if (echo >"/dev/tcp/127.0.0.1/$port") 2>/dev/null; then + echo "Port $port is already in use. Run task e2e:down before task e2e:up; if it stays occupied, inspect its owner with ss -ltnp." >&2 + return 1 + fi + done + trap 'trap - ERR; stop' ERR prepare - start_pg + start_pg 9>&- start_service archie-state-store wait_port 9091 archie-state-store 90 local s for s in archie-gateway archied archie-messaging archie-ui; do start_service "$s"; done + wait_port 8585 archie-gateway + wait_port 8586 archie-messaging + wait_port 8485 archied wait_port 8484 archie-ui [ -d "$repo/ui/node_modules" ] || (cd "$repo/ui" && npm ci --no-audit --no-fund >/dev/null) spawn vite "$repo/ui" npx vite --host 127.0.0.1 --port 5173 --strictPort wait_port 5173 vite + for s in "${services[@]}" vite; do check_process "$s"; done + trap - ERR echo "dashboard: http://localhost:5173 logs: $dir/logs" } @@ -116,6 +145,11 @@ stop() { fi } +# Keep concurrent invocations from replacing each other's process records. +mkdir -p "$dir" +exec 9>"$dir/stack.lock" +flock -n 9 || { echo "Another e2e stack command is running" >&2; exit 1; } + case ${1:-} in start) start ;; stop) stop ;; diff --git a/internal/app/archiegateway/curators.go b/internal/app/archiegateway/curators.go index 9f22ac14d..76a09885a 100644 --- a/internal/app/archiegateway/curators.go +++ b/internal/app/archiegateway/curators.go @@ -22,12 +22,12 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/toolbuilder" ) -func (b *server) setupCurators(ctx context.Context) { +func (b *server) setupCurators(ctx context.Context) bool { log := b.log.With("component", "curator") model, err := b.chatModels.ModelFor(ctx) if err != nil { - log.Warn("curators disabled: chat model unavailable", "err", err) - return + log.Warn("curators disabled: chat model unavailable; configure a chat model and restart the gateway", "err", err) + return false } skillsRoot := b.cfg.SkillsDir if skillsRoot == "" { @@ -58,6 +58,7 @@ func (b *server) setupCurators(ctx context.Context) { b.addCleanup(shutdownCuratorRuntime(rt, log)) reg := b.curatorRegistry b.addCleanup(shutdownCuratorRegistry(reg, log)) + return true } //nolint:contextcheck // shutdown runs after the parent context is cancelled @@ -174,3 +175,26 @@ func (b *server) watchCurators(ctx context.Context, version int64) error { func(update controlplane.AppliedCurators) error { return b.applyCurators(ctx, update.Definitions) }) return nil } + +func (b *server) startCurators(ctx context.Context) error { + // Curators wake on this process's turns and read its conversations. + if !b.setupCurators(ctx) { + return nil + } + if err := b.curatorRuntime.Start(ctx); err != nil { + b.log.Error("curator runtime startup", "err", err) + } + defs, version, err := b.controlPlane.Curators(ctx) + if err == nil { + err = b.applyCurators(ctx, defs) + } + b.applyStatus.Report(ctx, controlplane.CuratorsKind, version, err) + if err != nil { + b.log.Error("curator definitions apply failed", "err", err) + } + if err := b.watchCurators(ctx, version); err != nil { + return fmt.Errorf("watch curators: %w", err) + } + + return nil +} diff --git a/internal/app/archiegateway/curators_test.go b/internal/app/archiegateway/curators_test.go new file mode 100644 index 000000000..6e7373b9a --- /dev/null +++ b/internal/app/archiegateway/curators_test.go @@ -0,0 +1,13 @@ +package archiegateway + +import ( + "log/slog" + "testing" +) + +func TestCuratorsWithoutChatModel(t *testing.T) { + b := &server{log: slog.New(slog.DiscardHandler), chatModels: newChatModelManager(nil)} + if err := b.startCurators(t.Context()); err != nil { + t.Fatalf("unconfigured curators must not stop the gateway: %v", err) + } +} diff --git a/internal/app/archiegateway/gateway.go b/internal/app/archiegateway/gateway.go index 131103c95..e0a2720b7 100644 --- a/internal/app/archiegateway/gateway.go +++ b/internal/app/archiegateway/gateway.go @@ -7,7 +7,6 @@ import ( "google.golang.org/grpc" - "github.com/samcharles93/archie-core/internal/app/controlplane" "github.com/samcharles93/archie-core/internal/app/servicekit" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/messaging" @@ -65,21 +64,8 @@ func (b *server) startGatewayRuntime(ctx context.Context, actor gateway.ChatTask return nil, fmt.Errorf("runtime resource watches: %w", err) } b.startModelCatalogRefresh(ctx) - // Curators wake on this process's turns and read its conversations. - b.setupCurators(ctx) - if err := b.curatorRuntime.Start(ctx); err != nil { - b.log.Error("curator runtime startup", "err", err) - } - defs, version, err := b.controlPlane.Curators(ctx) - if err == nil { - err = b.applyCurators(ctx, defs) - } - b.applyStatus.Report(ctx, controlplane.CuratorsKind, version, err) - if err != nil { - b.log.Error("curator definitions apply failed", "err", err) - } - if err := b.watchCurators(ctx, version); err != nil { - return nil, fmt.Errorf("watch curators: %w", err) + if err := b.startCurators(ctx); err != nil { + return nil, err } return contract, nil