Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🔨 chore(agent-testing): auto-allocate non-conflicting dev ports + local chat-security bypass (#16287)

Arvin Xu216d50ba2460
diff --git a/.agents/skills/agent-testing/scripts/init-dev-env.sh b/.agents/skills/agent-testing/scripts/init-dev-env.sh
index c19c78b1d84..12cc8a44a38 100755
--- a/.agents/skills/agent-testing/scripts/init-dev-env.sh
+++ b/.agents/skills/agent-testing/scripts/init-dev-env.sh
@@ -28,15 +28,73 @@ set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../../.." && pwd)"
ROOT_ENV_FILE="$REPO_ROOT/.env"
-SERVER_PORT="${SERVER_PORT:-3010}"
+# Resolve the workspace root the SAME way test-env.sh does, so both scripts
+# read/write the ports file (and other .records artifacts) at the same path.
+# When the skill resolves under a cloud checkout's submodule
+# (.../lobehub-cloud*/lobehub), REPO_ROOT is the submodule but the shared
+# .records lives at the cloud parent — mismatching it would make setup-db/dev
+# allocate ports that test-env.sh / setup-auth.sh never read.
+WORKSPACE_ROOT="$REPO_ROOT"
+if [[ "$(basename "$WORKSPACE_ROOT")" == "lobehub" ]]; then
+ _parent_root="$(cd "$WORKSPACE_ROOT/.." && pwd)"
+ [[ "$(basename "$_parent_root")" == lobehub-cloud* ]] && WORKSPACE_ROOT="$_parent_root"
+fi
+
+# --- auto-allocated, non-conflicting ports (persisted per workspace) ---------
+# Each repo copy (lobehub-cloud, lobehub-cloud-cc, ...) probes its own free
+# SERVER_PORT / SPA_PORT so copies running concurrently never fight over
+# 3010/9876. Ports are probed once then persisted, so repeated calls (setup-db,
+# seed-user, dev, web-seed) and test-env.sh all agree on the same port. Delete
+# the ports file (or pass SERVER_PORT=... explicitly) to re-allocate.
+PORTS_FILE="${AGENT_TESTING_PORTS_FILE:-$WORKSPACE_ROOT/.records/env/agent-testing-ports.env}"
+
+_port_in_use() { lsof -iTCP:"$1" -sTCP:LISTEN > /dev/null 2>&1; }
+_pick_free_port() {
+ local fallback="$1" p
+ for _ in $(seq 1 80); do
+ p=$(((RANDOM % 20000) + 20000))
+ _port_in_use "$p" || {
+ printf '%s' "$p"
+ return 0
+ }
+ done
+ printf '%s' "$fallback"
+}
+_load_or_alloc_ports() {
+ # Reuse persisted ports verbatim once allocated — the port being "in use" is
+ # expected (our own dev server holds it), so never re-probe on reuse.
+ # shellcheck disable=SC1090
+ [[ -f "$PORTS_FILE" ]] && source "$PORTS_FILE"
+ local changed=0
+ if [[ -z "${ALLOC_SERVER_PORT:-}" ]]; then
+ ALLOC_SERVER_PORT="$(_pick_free_port 3010)"
+ changed=1
+ fi
+ if [[ -z "${ALLOC_SPA_PORT:-}" ]]; then
+ ALLOC_SPA_PORT="$(_pick_free_port 9876)"
+ changed=1
+ fi
+ if [[ "$changed" == 1 ]]; then
+ mkdir -p "$(dirname "$PORTS_FILE")"
+ {
+ printf '# agent-testing auto-allocated ports (delete to re-allocate)\n'
+ printf 'ALLOC_SERVER_PORT=%s\n' "$ALLOC_SERVER_PORT"
+ printf 'ALLOC_SPA_PORT=%s\n' "$ALLOC_SPA_PORT"
+ } > "$PORTS_FILE"
+ fi
+}
+_load_or_alloc_ports
+
+SERVER_PORT="${SERVER_PORT:-$ALLOC_SERVER_PORT}"
+SPA_PORT="${SPA_PORT:-$ALLOC_SPA_PORT}"
DB_PORT="${DB_PORT:-5433}"
DB_CONTAINER="${DB_CONTAINER:-lobehub-agent-testing-postgres}"
DATABASE_URL="${DATABASE_URL:-postgresql://postgres:postgres@localhost:${DB_PORT}/postgres}"
REDIS_PORT="${REDIS_PORT:-6380}"
REDIS_CONTAINER="${REDIS_CONTAINER:-lobehub-agent-testing-redis}"
REDIS_URL="${REDIS_URL:-redis://localhost:${REDIS_PORT}}"
-ENV_FILE_DEFAULT="$REPO_ROOT/.records/env/agent-testing-dev.env"
-CLI_ENV_FILE_DEFAULT="$REPO_ROOT/.records/env/agent-testing-cli.env"
+ENV_FILE_DEFAULT="$WORKSPACE_ROOT/.records/env/agent-testing-dev.env"
+CLI_ENV_FILE_DEFAULT="$WORKSPACE_ROOT/.records/env/agent-testing-cli.env"
AGENT_TESTING_API_KEY="${AGENT_TESTING_API_KEY:-sk-lh-agenttesting0001}"
QSTASH_DEV_PORT="${QSTASH_DEV_PORT:-8080}"
QSTASH_LOCAL_TOKEN="${QSTASH_LOCAL_TOKEN:-eyJVc2VySUQiOiJkZWZhdWx0VXNlciIsIlBhc3N3b3JkIjoiZGVmYXVsdFBhc3N3b3JkIn0=}"
@@ -78,6 +136,12 @@ apply_env() {
export S3_BUCKET="${S3_BUCKET:-agent-testing-bucket}"
export S3_ENDPOINT="${S3_ENDPOINT:-https://agent-testing-s3.localhost}"
export S3_SECRET_ACCESS_KEY="${S3_SECRET_ACCESS_KEY:-agent-testing-secret-key}"
+ export SPA_PORT
+ export VITE_DEV_PORT="${VITE_DEV_PORT:-$SPA_PORT}"
+ # Bypass cloud chat-security UA/headless fingerprint checks for local e2e only.
+ # Guarded by NODE_ENV !== 'production' inside detectSuspiciousRequest(), so it
+ # can never weaken production. Lets headless agent-browser drive real chats.
+ export AGENT_TESTING_DISABLE_CHAT_SECURITY="${AGENT_TESTING_DISABLE_CHAT_SECURITY:-1}"
}
env_keys() {
@@ -102,7 +166,10 @@ env_keys() {
S3_ACCESS_KEY_ID \
S3_BUCKET \
S3_ENDPOINT \
- S3_SECRET_ACCESS_KEY
+ S3_SECRET_ACCESS_KEY \
+ SPA_PORT \
+ VITE_DEV_PORT \
+ AGENT_TESTING_DISABLE_CHAT_SECURITY
}
print_env() {
@@ -395,7 +462,12 @@ cmd_qstash() {
cmd_dev_next() {
apply_env
cd "$REPO_ROOT"
- exec pnpm run dev:next
+ # Pass the allocated port explicitly. The submodule's `dev:next` package script
+ # hard-codes `-p 3010`, so going through it would bind the wrong port whenever
+ # SERVER_PORT was auto-allocated to something else. apply_env already exported
+ # every env this needs (there is no .env to load in this mode), so invoking
+ # next directly is equivalent and port-correct in both cloud and submodule.
+ exec pnpm exec next dev -p "$SERVER_PORT"
}
cmd_dev() {
diff --git a/.agents/skills/agent-testing/scripts/test-env.sh b/.agents/skills/agent-testing/scripts/test-env.sh
index f8eb77a744e..c15d6eb5372 100755
--- a/.agents/skills/agent-testing/scripts/test-env.sh
+++ b/.agents/skills/agent-testing/scripts/test-env.sh
@@ -230,6 +230,25 @@ apply_shell_overrides() {
done < <(keys)
}
+# Read the ports auto-allocated by init-dev-env.sh (.records/env/agent-testing-ports.env)
+# so test-env / setup-auth target the exact port the dev server actually bound to.
+# Lower priority than explicit .env / shell, higher than the workspace-offset default.
+apply_ports_file() {
+ local root="$1"
+ local file="${AGENT_TESTING_PORTS_FILE:-$root/.records/env/agent-testing-ports.env}"
+ [[ -f "$file" ]] || return 0
+ local ss sp
+ ss="$(grep -E '^ALLOC_SERVER_PORT=' "$file" 2> /dev/null | tail -1 | cut -d= -f2 || true)"
+ sp="$(grep -E '^ALLOC_SPA_PORT=' "$file" 2> /dev/null | tail -1 | cut -d= -f2 || true)"
+ if [[ -n "$ss" ]]; then
+ [[ -z "$VALUE_PORT" ]] && set_value PORT "$ss" "ports-file"
+ [[ -z "$VALUE_APP_URL" ]] && set_value APP_URL "http://localhost:$ss" "ports-file"
+ fi
+ if [[ -n "$sp" && -z "$VALUE_SPA_PORT" ]]; then
+ set_value SPA_PORT "$sp" "ports-file"
+ fi
+}
+
resolve_defaults() {
local app_port spa_port mobile_spa_port desktop_port
app_port="$(default_port 3020 3010)"
@@ -354,6 +373,7 @@ EOF
ROOT="$(workspace_root)"
apply_env_files "$ROOT"
apply_shell_overrides
+apply_ports_file "$ROOT"
resolve_defaults
case "${1:-}" in
diff --git a/package.json b/package.json
index 0982b7e75ed..2df8335730a 100644
--- a/package.json
+++ b/package.json
@@ -72,9 +72,9 @@
"dev:docker:down": "docker compose -f docker-compose/dev/docker-compose.yml down",
"dev:docker:reset": "docker compose -f docker-compose/dev/docker-compose.yml down -v && rm -rf docker-compose/dev/data && npm run dev:docker && pnpm db:migrate",
"dev:next": "next dev -p 3010",
- "dev:spa": "vite --port 9876",
- "dev:spa:auth": "cross-env AUTH=true vite --port 3013",
- "dev:spa:mobile": "cross-env MOBILE=true vite --port 3012",
+ "dev:spa": "vite",
+ "dev:spa:auth": "cross-env AUTH=true vite",
+ "dev:spa:mobile": "cross-env MOBILE=true vite",
"docs:cdn": "npm run workflow:docs-cdn && npm run lint:mdx",
"docs:i18n": "lobe-i18n md && npm run lint:mdx",
"docs:seo": "lobe-seo && npm run lint:mdx",
diff --git a/vite.config.ts b/vite.config.ts
index c99dfdcfed8..97f695f006f 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -300,7 +300,11 @@ export default defineConfig({
server: {
cors: true,
host: true,
- port: 9876,
+ port: isMobile
+ ? Number(process.env.MOBILE_SPA_PORT) || 3012
+ : isAuth
+ ? Number(process.env.AUTH_SPA_PORT) || 3013
+ : Number(process.env.SPA_PORT) || 9876,
proxy: {
'/api': `http://localhost:${process.env.PORT || 3010}`,
'/oidc': `http://localhost:${process.env.PORT || 3010}`,