Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(e2e): give the web lane its own port block and refuse taken ports

obchaindbcb9de21244
diff --git a/app/scripts/e2e-ports.sh b/app/scripts/e2e-ports.sh
new file mode 100644
index 0000000000..273bffa138
--- /dev/null
+++ b/app/scripts/e2e-ports.sh
@@ -0,0 +1,95 @@
+#!/usr/bin/env bash
+
+# Port selection shared by the web E2E build and session scripts (#5918).
+#
+# The three harness ports used to be fixed constants, so two sessions on one
+# machine claimed the same ones. The second session bound nothing and ran
+# against the first session's mock backend, core and served bundle: no error,
+# and failures that read exactly like product defects ("Session validation
+# failed", "Connection refused", "Core startup failed").
+#
+# `E2E_PORT_BASE` gives a session its own block. The build and the session must
+# agree on it, because the mock and core ports are baked into the bundle at
+# build time and cannot be changed at run time (#6478) — which is why this
+# lives in one file both scripts source instead of in each of them.
+
+E2E_DEFAULT_MOCK_PORT=18473
+E2E_DEFAULT_CORE_PORT=17788
+E2E_DEFAULT_WEB_PORT=4173
+
+# Resolve E2E_MOCK_PORT / OPENHUMAN_CORE_PORT / E2E_WEB_PORT.
+#
+# An explicit per-port variable always wins, so existing callers and CI lanes
+# that set one keep their behaviour; `E2E_PORT_BASE` only supplies defaults.
+e2e_resolve_ports() {
+ local mock_default="$E2E_DEFAULT_MOCK_PORT"
+ local core_default="$E2E_DEFAULT_CORE_PORT"
+ local web_default="$E2E_DEFAULT_WEB_PORT"
+ local base="${E2E_PORT_BASE:-}"
+
+ if [ -n "$base" ]; then
+ case "$base" in
+ '' | *[!0-9]*)
+ echo "ERROR: E2E_PORT_BASE must be a port number, got '$base'" >&2
+ return 1
+ ;;
+ esac
+ # The block is base, base+1, base+2, so the last usable base is 65533.
+ # Staying above 1024 keeps it off the privileged range.
+ if [ "$base" -lt 1024 ] || [ "$base" -gt 65533 ]; then
+ echo "ERROR: E2E_PORT_BASE must be between 1024 and 65533 (it claims base..base+2), got $base" >&2
+ return 1
+ fi
+ mock_default="$base"
+ core_default="$((base + 1))"
+ web_default="$((base + 2))"
+ fi
+
+ E2E_MOCK_PORT="${E2E_MOCK_PORT:-$mock_default}"
+ OPENHUMAN_CORE_PORT="${OPENHUMAN_CORE_PORT:-$core_default}"
+ E2E_WEB_PORT="${E2E_WEB_PORT:-$web_default}"
+}
+
+# Refuse to start when something is already listening on one of the ports.
+#
+# This has to happen before anything is launched, because every readiness probe
+# in the session is an HTTP GET and another session's server answers those just
+# as happily. The core is the reason it cannot be left to the launch failing:
+# `openhuman-core run` does not exit when its port is held — it falls back to
+# preferred+1..+10 (`pick_listen_port_for_host_with`), so it stays alive on a
+# port nothing probes while `/health` and the authenticated RPC probe are
+# answered by the other session's core.
+e2e_require_free_ports() {
+ local blocked=""
+ local port
+
+ for port in "$@"; do
+ if ! python3 - "$port" <<'PY'
+import socket, sys
+
+probe = socket.socket()
+# SO_REUSEADDR so a TIME_WAIT socket left by a previous session is not
+# mistaken for a live listener; an actual listener still refuses the bind.
+probe.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+try:
+ probe.bind(("127.0.0.1", int(sys.argv[1])))
+except OSError:
+ sys.exit(1)
+finally:
+ probe.close()
+PY
+ then
+ blocked="${blocked} ${port}"
+ fi
+ done
+
+ if [ -n "$blocked" ]; then
+ echo "ERROR: already in use on 127.0.0.1:${blocked}" >&2
+ echo " Another web E2E session, or another process, is listening there. Starting" >&2
+ echo " anyway would run this session's specs against that one's backend." >&2
+ echo " Give this session its own port block, for the build and the run:" >&2
+ echo " E2E_PORT_BASE=<free base> pnpm --filter openhuman-app test:e2e:web" >&2
+ echo " The mock and core ports are baked into the bundle, so both must use the same base." >&2
+ return 1
+ fi
+}
diff --git a/app/scripts/e2e-web-build.sh b/app/scripts/e2e-web-build.sh
index 1e59836009..ab23bfd974 100755
--- a/app/scripts/e2e-web-build.sh
+++ b/app/scripts/e2e-web-build.sh
@@ -2,7 +2,8 @@
set -euo pipefail
-APP_DIR="$(cd "$(dirname "$0")/.." && pwd)"
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+APP_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
REPO_ROOT="$(cd "$APP_DIR/.." && pwd)"
cd "$APP_DIR"
@@ -18,8 +19,11 @@ E2E_WEB_CORE_TARGET_DIR="${E2E_WEB_CORE_TARGET_DIR:-$REPO_ROOT/target/e2e-web-${
# Preserve explicit harness ports before loading a developer .env. That file
# may configure normal development, but must not change an E2E bundle's baked
# endpoint or its E2E-only affordances.
-SELECTED_E2E_MOCK_PORT="${E2E_MOCK_PORT:-18473}"
-SELECTED_OPENHUMAN_CORE_PORT="${OPENHUMAN_CORE_PORT:-17788}"
+# shellcheck source=./e2e-ports.sh
+source "$SCRIPT_DIR/e2e-ports.sh"
+e2e_resolve_ports
+SELECTED_E2E_MOCK_PORT="$E2E_MOCK_PORT"
+SELECTED_OPENHUMAN_CORE_PORT="$OPENHUMAN_CORE_PORT"
if [ -f "$REPO_ROOT/.env" ]; then
# shellcheck source=/dev/null
@@ -60,8 +64,8 @@ MARKER
# contract before serving: a web bundle cannot change its backend at runtime.
cat >"$APP_DIR/dist-web/.e2e-build-ports.json" <<JSON
{
- "e2e_mock_port": "${E2E_MOCK_PORT:-18473}",
- "openhuman_core_port": "${OPENHUMAN_CORE_PORT:-17788}",
+ "e2e_mock_port": "${E2E_MOCK_PORT}",
+ "openhuman_core_port": "${OPENHUMAN_CORE_PORT}",
"vite_backend_url": "${VITE_BACKEND_URL}"
}
JSON
diff --git a/app/scripts/e2e-web-session.sh b/app/scripts/e2e-web-session.sh
index 829450cd7a..6e2e5f3b13 100755
--- a/app/scripts/e2e-web-session.sh
+++ b/app/scripts/e2e-web-session.sh
@@ -9,9 +9,13 @@ cd "$APP_DIR"
RUST_HOST_TRIPLE="${RUST_HOST_TRIPLE:-$(rustc -vV | awk '/^host: / { print $2 }')}"
E2E_WEB_CORE_TARGET_DIR="${E2E_WEB_CORE_TARGET_DIR:-$REPO_ROOT/target/e2e-web-${RUST_HOST_TRIPLE}}"
-E2E_MOCK_PORT="${E2E_MOCK_PORT:-18473}"
-OPENHUMAN_CORE_PORT="${OPENHUMAN_CORE_PORT:-17788}"
-E2E_WEB_PORT="${E2E_WEB_PORT:-4173}"
+# shellcheck source=./e2e-ports.sh
+source "$SCRIPT_DIR/e2e-ports.sh"
+e2e_resolve_ports
+# The Playwright specs reach the mock's admin API themselves
+# (`MOCK_ADMIN_BASE`), and default to 18473 when this is unset. Without the
+# export a session on a derived block served its specs the wrong mock.
+export E2E_MOCK_PORT
PW_CORE_RPC_TOKEN="${PW_CORE_RPC_TOKEN:-openhuman-playwright-token}"
PW_CORE_RPC_URL="http://127.0.0.1:${OPENHUMAN_CORE_PORT}/rpc"
PW_BASE_URL="http://127.0.0.1:${E2E_WEB_PORT}"
@@ -173,6 +177,10 @@ if [ ! -f "$E2E_BUNDLE_MARKER" ]; then
exit 1
fi
+# Nothing below tolerates a port that is already taken (#5918): the probes are
+# plain HTTP GETs, so they are answered by whatever is listening.
+e2e_require_free_ports "$E2E_MOCK_PORT" "$OPENHUMAN_CORE_PORT" "$E2E_WEB_PORT"
+
node "$REPO_ROOT/scripts/mock-api-server.mjs" --port "$E2E_MOCK_PORT" >"$OPENHUMAN_WORKSPACE/mock.log" 2>&1 &
MOCK_PID=$!
wait_for_http "http://127.0.0.1:${E2E_MOCK_PORT}/__admin/health" "mock backend"
diff --git a/scripts/__tests__/e2e-web-bundle-guard.test.mjs b/scripts/__tests__/e2e-web-bundle-guard.test.mjs
index 1d666e77e2..f9404e0f82 100644
--- a/scripts/__tests__/e2e-web-bundle-guard.test.mjs
+++ b/scripts/__tests__/e2e-web-bundle-guard.test.mjs
@@ -41,10 +41,13 @@ function makeTree(script) {
const bin = path.join(root, "bin");
const log = path.join(root, "calls.log");
fs.mkdirSync(path.join(root, "app", "scripts"), { recursive: true });
- fs.copyFileSync(
- path.join(repoRoot, "app", "scripts", script),
- path.join(root, "app", "scripts", script),
- );
+ // Both scripts source the shared port helper (#5918).
+ for (const file of [script, "e2e-ports.sh"]) {
+ fs.copyFileSync(
+ path.join(repoRoot, "app", "scripts", file),
+ path.join(root, "app", "scripts", file),
+ );
+ }
fs.mkdirSync(path.join(root, "scripts"), { recursive: true });
fs.copyFileSync(
path.join(repoRoot, "scripts", "load-dotenv.sh"),