Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(dev-env): never register or destroy the shared 8080 backend and multica database (MUL-9782) (#11126)
Jiayuan Zhangc1406c7c31e0
diff --git a/scripts/dev-env.sh b/scripts/dev-env.shindex bd360046d6..d106463595 100755--- a/scripts/dev-env.sh+++ b/scripts/dev-env.sh@@ -286,6 +286,44 @@ desktop_daemon_profile() {printf 'desktop-localhost-%s' "$BACKEND_PORT"}+# A slot owns one backend port and one database name, both derived from its+# offset, and destroy deletes by those names. Anything else in a manifest (the+# shared 8080 backend, the default `multica` database) belongs to someone else.+valid_offset() {+ case "$1" in ""|*[!0-9]*|0?*) return 1 ;; esac+ [ "$1" -lt 1000 ]+}++slot_backend_port_ok() {+ valid_offset "$2" && [ "$1" = "$((18080 + $2))" ]+}++slot_database_name_ok() {+ local db=$1 offset=$2 slug+ valid_offset "$offset" || return 1+ case "$db" in multica_*_"$offset") ;; *) return 1 ;; esac+ slug="${db#multica_}"+ slug="${slug%_"$offset"}"+ case "$slug" in ""|*[!a-z0-9_]*) return 1 ;; esac+}++# Prints why OFFSET/PORT/DB/DATABASE_URL do not describe one slot, or nothing.+slot_identity_problem() {+ local offset=$1 port=$2 db=$3 url=$4 url_db+ if ! valid_offset "$offset"; then+ printf 'backend port %s is outside the slot range 18080-19079' "$port"+ elif ! slot_backend_port_ok "$port" "$offset"; then+ printf "backend port %s is not slot %s's port %s" "$port" "$offset" "$((18080 + offset))"+ elif ! slot_database_name_ok "$db" "$offset"; then+ printf "database %s is not slot %s's (multica_<slug>_%s)" "$db" "$offset" "$offset"+ else+ url_db="$(database_url_name "$url")"+ [ "$url_db" = "$db" ] \+ || printf 'DATABASE_URL points at database %s, not %s' "${url_db:-<none>}" "$db"+ fi+ return 0+}+# Chrome's conventional 9222 is left to whatever a human launches by hand.CDP_PORT_BASE=9223@@ -405,27 +443,52 @@ ensure_dev_code() {info "Set MULTICA_DEV_VERIFICATION_CODE=$DEV_CODE_DEFAULT in $1 (ignored when APP_ENV=production)."}-rewrite_env_ports() {- local file="$REPO_ROOT/$1" offset=$2 backend=$3 frontend=$4 db=$5 tmp database_url escaped_database_url- database_url="$(database_url_with_name "${DATABASE_URL:-}" "$db")" \- || die "DATABASE_URL is not a valid PostgreSQL URL: ${DATABASE_URL:-<unset>}"- escaped_database_url="$(printf '%s' "$database_url" | sed 's/[\\&|]/\\&/g')"+set_env_value() {+ local file=$1 tmptmp="$(mktemp)"- sed \- -e "s|^PORT=.*|PORT=${backend}|" \- -e "s|^FRONTEND_PORT=.*|FRONTEND_PORT=${frontend}|" \- -e "s|^FRONTEND_ORIGIN=.*|FRONTEND_ORIGIN=http://localhost:${frontend}|" \- -e "s|^POSTGRES_DB=.*|POSTGRES_DB=${db}|" \- -e "s|^DATABASE_URL=.*|DATABASE_URL=${escaped_database_url}|" \- -e "s|^MULTICA_SERVER_URL=.*|MULTICA_SERVER_URL=ws://localhost:${backend}/ws|" \- -e "s|^MULTICA_PUBLIC_URL=.*|MULTICA_PUBLIC_URL=http://localhost:${backend}|" \- -e "s|^MULTICA_APP_URL=.*|MULTICA_APP_URL=http://localhost:${frontend}|" \- -e "s|^NEXT_PUBLIC_API_URL=.*|NEXT_PUBLIC_API_URL=http://localhost:${backend}|" \- -e "s|^NEXT_PUBLIC_WS_URL=.*|NEXT_PUBLIC_WS_URL=ws://localhost:${backend}/ws|" \- "$file" > "$tmp"+ ENV_KEY="$2" ENV_VALUE="$3" awk '+ BEGIN { key = ENVIRON["ENV_KEY"] "="; line = key ENVIRON["ENV_VALUE"] }+ index($0, key) == 1 { print line; found = 1; next }+ { print }+ END { if (!found) print line }+ ' "$file" > "$tmp"mv "$tmp" "$file"}+# A key the file lacks is appended, not skipped: `make` has already exported+# its own defaults for all of these (PORT=8080, POSTGRES_DB=multica, ...), and+# sourcing a file without the key leaves those in force.+rewrite_env_ports() {+ local file="$REPO_ROOT/$1" offset=$2 backend=$3 frontend=$4 db=$5 database_url base_url+ base_url="${DATABASE_URL:-postgres://${POSTGRES_USER:-multica}:${POSTGRES_PASSWORD:-multica}@localhost:${POSTGRES_PORT:-5432}/postgres?sslmode=disable}"+ database_url="$(database_url_with_name "$base_url" "$db")" \+ || die "DATABASE_URL is not a valid PostgreSQL URL: $base_url"+ set_env_value "$file" PORT "$backend"+ set_env_value "$file" FRONTEND_PORT "$frontend"+ set_env_value "$file" FRONTEND_ORIGIN "http://localhost:${frontend}"+ set_env_value "$file" POSTGRES_DB "$db"+ set_env_value "$file" DATABASE_URL "$database_url"+ set_env_value "$file" MULTICA_SERVER_URL "ws://localhost:${backend}/ws"+ set_env_value "$file" MULTICA_PUBLIC_URL "http://localhost:${backend}"+ set_env_value "$file" MULTICA_APP_URL "http://localhost:${frontend}"+ set_env_value "$file" NEXT_PUBLIC_API_URL "http://localhost:${backend}"+ set_env_value "$file" NEXT_PUBLIC_WS_URL "ws://localhost:${backend}/ws"+}++# Run after the rewritten file is loaded back. Something outside the keys+# rewrite_env_ports owns (BACKEND_PORT outranks PORT in local-env.sh) can still+# win, and registering then would hand destroy the shared backend and database.+verify_env_ports() {+ local file=$1 backend=$2 frontend=$3 db=$4 problems="" url_db+ url_db="$(database_url_name "${DATABASE_URL:-}")"+ [ "$PORT" = "$backend" ] || problems="$problems PORT=$PORT"+ [ "$FRONTEND_PORT" = "$frontend" ] || problems="$problems FRONTEND_PORT=$FRONTEND_PORT"+ [ "$POSTGRES_DB" = "$db" ] || problems="$problems POSTGRES_DB=$POSTGRES_DB"+ [ "$url_db" = "$db" ] || problems="$problems DATABASE_URL->${url_db:-<none>}"+ [ -z "$problems" ] \+ || die "Allocated backend $backend, frontend $frontend, database $db, but $file still resolves to:$problems. Remove the overriding setting (BACKEND_PORT, API_PORT or SERVER_PORT in $file or the shell) and run make up again."+}+# ---------------------------------------------------------------- database ---admin_database_url() {@@ -445,6 +508,13 @@ database_url_with_name() {' "$1" "$2" 2>/dev/null}+database_url_name() {+ node -e '+ const url = new URL(process.argv[1]);+ process.stdout.write(decodeURIComponent(url.pathname.replace(/^\//, "")));+ ' "$1" 2>/dev/null || true+}+# Diagnoses the failure mode this whole script exists to make impossible:# something other than the container owns 5432, so the container never bound# the host port and a docker-exec create landed in the wrong server.@@ -1244,6 +1314,10 @@ stop_desktop_daemon() {profile="$(desktop_daemon_profile)"pid="$(cat "$DEV_PROFILES_HOME/$profile/daemon.pid" 2>/dev/null || true)"[ -n "$pid" ] && kill -0 "$pid" 2>/dev/null || return 0+ if ! slot_backend_port_ok "$BACKEND_PORT" "${OFFSET:-}"; then+ warn "left Desktop's daemon for profile $profile alone: :$BACKEND_PORT is not slot ${OFFSET:-?}'s backend port"+ return 0+ fibin="$MULTICA_BIN"[ -x "$bin" ] || bin="$DIR/apps/desktop/resources/bin/multica"if [ -x "$bin" ] && "${CLEAN_ENV[@]}" "$bin" daemon stop --force --profile "$profile" >/dev/null 2>&1 \@@ -1731,6 +1805,7 @@ cmd_up() {candidate_renderer="$(renderer_port_for_offset "$offset")"candidate_collab="$(collab_port_for_offset "$offset")"if [ "$PORT" -lt 18080 ] || [ "$FRONTEND_PORT" -ne $((13000 + offset)) ] \+ || [ -n "$(slot_identity_problem "$offset" "$PORT" "$POSTGRES_DB" "${DATABASE_URL:-}")" ] \|| offset_registered "$offset" || ! port_free "$PORT" \|| ! port_free "$FRONTEND_PORT" || ! port_free "$candidate_renderer" \|| ! port_free "$candidate_collab"; then@@ -1742,8 +1817,13 @@ cmd_up() {local new_db="multica_$(slugify "$(basename "$REPO_ROOT")")_${offset}"rewrite_env_ports "$ENV_FILE" "$offset" "$new_backend" "$new_frontend" "$new_db"load_env_file "$ENV_FILE"+ verify_env_ports "$ENV_FILE" "$new_backend" "$new_frontend" "$new_db"info "Allocated slot $offset — backend $new_backend, frontend $new_frontend, database $new_db"fi+ local identity_problem+ identity_problem="$(slot_identity_problem "$offset" "$PORT" "$POSTGRES_DB" "${DATABASE_URL:-}")"+ [ -z "$identity_problem" ] \+ || die "Refusing to register an environment for $ENV_FILE: $identity_problem. destroy would delete what the manifest names."NAME="${name:-$(slugify "$(basename "$REPO_ROOT")")-${offset}}"require_env_name "$NAME"@@ -1887,7 +1967,10 @@ cmd_destroy() {fidone- if command -v psql >/dev/null 2>&1; then+ if ! slot_database_name_ok "$DB_NAME" "$OFFSET"; then+ warn "refusing to drop database $DB_NAME: it is not slot $OFFSET's database (multica_<slug>_$OFFSET)"+ failures=$((failures + 1))+ elif command -v psql >/dev/null 2>&1; thenadmin_url="$(admin_database_url "$DATABASE_URL")"if PGCONNECT_TIMEOUT=3 psql "$admin_url" -tAc 'SELECT 1' >/dev/null 2>&1; thenif psql "$admin_url" -v ON_ERROR_STOP=1 -c "DROP DATABASE IF EXISTS \"$DB_NAME\" WITH (FORCE)" >/dev/null; then@@ -1937,7 +2020,12 @@ cmd_destroy() {# The profile holds the daemon's pid and config, which a retried destroy# needs to stop it; keep it until Desktop and its daemon are confirmed gone.desktop_profile_dir="$DEV_PROFILES_HOME/$(desktop_daemon_profile)"- if [ "$desktop_stopped" != 1 ]; then+ if ! slot_backend_port_ok "$BACKEND_PORT" "$OFFSET"; then+ if [ -d "$desktop_profile_dir" ]; then+ warn "refusing to remove Desktop's CLI profile $(desktop_daemon_profile): :$BACKEND_PORT is not slot $OFFSET's backend port"+ failures=$((failures + 1))+ fi+ elif [ "$desktop_stopped" != 1 ]; then[ ! -d "$desktop_profile_dir" ] \|| warn "kept Desktop's CLI profile $(desktop_daemon_profile) for the retry"elif [ -d "$desktop_profile_dir" ]; thendiff --git a/scripts/dev-env.test.sh b/scripts/dev-env.test.shindex 016c31f6bd..a58e4eb53a 100644--- a/scripts/dev-env.test.sh+++ b/scripts/dev-env.test.sh@@ -19,6 +19,7 @@ fake_bin="$tmp_dir/bin"mkdir -p "$fake_bin"cat > "$fake_bin/psql" <<'EOF'#!/usr/bin/env bash+[ -z "${PSQL_LOG:-}" ] || printf '%s\n' "$*" >> "$PSQL_LOG"case " $* " in*" DROP DATABASE "*) [ "${FAIL_DROP:-0}" != 1 ] ;;*) printf '1\n' ;;@@ -897,7 +898,7 @@ status=0(# shellcheck disable=SC1090source "$root_dir/scripts/dev-env.sh"- BACKEND_PORT=18080 MULTICA_BIN="$fake_checkout/server/bin/multica" DIR="$fake_checkout"+ OFFSET=0 BACKEND_PORT=18080 MULTICA_BIN="$fake_checkout/server/bin/multica" DIR="$fake_checkout"stop_desktop_daemon) > "$out" 2>&1 || status=$?[ "$status" -ne 0 ] || fail "stop_desktop_daemon trusted a stop that left the daemon running"@@ -919,6 +920,114 @@ dev_env destroy desktop-profile-906 --yes > "$out" 2>&1 \! kill -0 "$desktop_daemon" 2>/dev/null || fail "Desktop's daemon is still running after destroy"[ ! -d "$desktop_profile_dir" ] || fail "destroy left Desktop's CLI profile behind"+# ---------------------------------------------------------------------------+# A slot is one backend port and one database name derived from its offset.+# `make` exports PORT=8080, POSTGRES_DB=multica and their DATABASE_URL before+# this script runs, so an env file that lacks those keys used to register the+# shared backend and database, and destroy then dropped `multica`.+# ---------------------------------------------------------------------------+assert_slot_identity() (+ local expected=$1+ shift+ # shellcheck disable=SC1090+ source "$root_dir/scripts/dev-env.sh"+ local problem+ problem="$(slot_identity_problem "$@")"+ case "$expected" in+ ok) [ -z "$problem" ] || fail "slot_identity_problem $* = '$problem', want none" ;;+ *) case "$problem" in *"$expected"*) ;; *) fail "slot_identity_problem $* = '$problem', want '$expected'" ;; esac ;;+ esac+)+slot_url() { printf 'postgres://multica:multica@localhost:5432/%s?sslmode=disable' "$1"; }+assert_slot_identity ok 283 18363 multica_multica_283 "$(slot_url multica_multica_283)"+assert_slot_identity ok 790 18870 multica_mul_8357_team_790 "$(slot_url multica_mul_8357_team_790)"+assert_slot_identity ok 0 18080 multica_env_0 "$(slot_url multica_env_0)"+assert_slot_identity "outside the slot range" -10000 8080 multica "$(slot_url multica)"+assert_slot_identity "outside the slot range" 1920 20000 multica_x_1920 "$(slot_url multica_x_1920)"+assert_slot_identity "not slot 283's port 18363" 283 8080 multica_multica_283 "$(slot_url multica_multica_283)"+assert_slot_identity "database multica is not slot 283's" 283 18363 multica "$(slot_url multica)"+assert_slot_identity "is not slot 5's" 5 18085 multica_foo_15 "$(slot_url multica_foo_15)"+assert_slot_identity "DATABASE_URL points at database multica" 283 18363 multica_multica_283 "$(slot_url multica)"++oneline_bin="$tmp_dir/oneline-bin"+mkdir -p "$oneline_bin"+printf '#!/usr/bin/env bash\nexit 1\n' > "$oneline_bin/go"+chmod +x "$oneline_bin/go"++# Runs `up` in a fresh checkout whose .env holds one flag, with the values+# `make` exports. The stub `go` fails the migration, which comes right after+# the environment is registered, so nothing is started.+oneline_up() (+ local checkout=$1+ shift+ rm -rf "$checkout"+ mkdir -p "$checkout/scripts"+ cp "$root_dir/scripts/dev-env.sh" "$root_dir/scripts/local-env.sh" "$checkout/scripts/"+ printf 'FF_DOCS_V1=true\n' > "$checkout/.env"+ export MULTICA_DEV_HOME="$tmp_dir/dev-$(basename "$checkout")" MULTICA_DEV_TMPDIR="$tmp_dir/dev-tmp"+ export PATH="$oneline_bin:$PATH"+ export PORT=8080 FRONTEND_PORT=3000 POSTGRES_DB=multica MULTICA_PUBLIC_URL=http://localhost:8080+ export DATABASE_URL="$(slot_url multica)"+ for assignment in "$@"; do export "${assignment?}"; done+ bash "$checkout/scripts/dev-env.sh" up --components api --ephemeral+)++oneline_checkout="$tmp_dir/oneline-checkout"+status=0+oneline_up "$oneline_checkout" > "$out" 2>&1 || status=$?+[ "$status" -ne 0 ] && grep -Fq "Migrations failed" "$out" \+ || { sed 's/^/ /' "$out" >&2; fail "up with a one-line .env did not register and reach the migration"; }+(+ # shellcheck disable=SC1090+ . "$tmp_dir"/dev-oneline-checkout/envs/*/manifest.env+ [ "$BACKEND_PORT" = $((18080 + OFFSET)) ] || fail "one-line .env registered backend $BACKEND_PORT for slot $OFFSET"+ [ "$DB_NAME" = "multica_oneline_checkout_$OFFSET" ] || fail "one-line .env registered database $DB_NAME"+ [ "$DATABASE_URL" = "$(slot_url "$DB_NAME")" ] || fail "one-line .env registered DATABASE_URL $DATABASE_URL"+ for line in FF_DOCS_V1=true "PORT=$BACKEND_PORT" "FRONTEND_PORT=$FRONTEND_PORT" "POSTGRES_DB=$DB_NAME" \+ "DATABASE_URL=$DATABASE_URL" "MULTICA_PUBLIC_URL=http://localhost:$BACKEND_PORT"; do+ grep -Fqx "$line" "$oneline_checkout/.env" || fail "the rewritten .env lacks the line $line"+ done+ psql_log="$tmp_dir/psql-oneline.log"+ MULTICA_DEV_HOME="$tmp_dir/dev-oneline-checkout" PSQL_LOG="$psql_log" \+ bash "$oneline_checkout/scripts/dev-env.sh" destroy "$NAME" --yes > "$out" 2>&1 \+ || { sed 's/^/ /' "$out" >&2; fail "destroying the one-line .env environment failed"; }+ grep -Fq "DROP DATABASE IF EXISTS \"$DB_NAME\"" "$psql_log" || fail "destroy did not drop the slot database"+)++# A setting the rewrite does not own can still win when the file is loaded+# back; `up` must refuse rather than register the shared port.+status=0+oneline_up "$tmp_dir/override-checkout" BACKEND_PORT=8080 > "$out" 2>&1 || status=$?+[ "$status" -ne 0 ] || fail "up registered an environment while BACKEND_PORT=8080 overrode the slot"+require_contains "$out" "still resolves to: PORT=8080"+if ls "$tmp_dir"/dev-override-checkout/envs/*/manifest.env >/dev/null 2>&1; then+ fail "up wrote a manifest although the slot did not take effect"+fi++# A manifest that names the shared backend and database is never acted on:+# destroy keeps `multica`, Desktop's 8080 profile and its daemon, and the+# manifest so the leftover stays visible.+write_manifest "poisoned-907" "$fake_checkout" 907+printf 'BACKEND_PORT=8080\nDB_NAME=multica\nDATABASE_URL=%s\n' "$(slot_url multica)" \+ >> "$MULTICA_DEV_HOME/envs/poisoned-907/manifest.env"+shared_profile_dir="$MULTICA_DEV_PROFILES_HOME/desktop-localhost-8080"+mkdir -p "$shared_profile_dir"+shared_daemon="$(sleep 60 > /dev/null 2>&1 & printf '%s' "$!")"+printf '%s\n' "$shared_daemon" > "$shared_profile_dir/daemon.pid"+psql_log="$tmp_dir/psql-poisoned.log"+status=0+PSQL_LOG="$psql_log" dev_env destroy poisoned-907 --yes > "$out" 2>&1 || status=$?+kill -0 "$shared_daemon" 2>/dev/null || fail "destroy stopped the daemon of Desktop's shared 8080 profile"+kill "$shared_daemon" 2>/dev/null || true+[ "$status" -ne 0 ] || fail "destroy reported success while refusing to drop the shared database"+! grep -Fq "DROP DATABASE" "$psql_log" 2>/dev/null || fail "destroy dropped a database that is not the slot's: $(cat "$psql_log")"+require_contains "$out" "refusing to drop database multica"+require_contains "$out" "refusing to remove Desktop's CLI profile desktop-localhost-8080"+require_contains "$out" "left Desktop's daemon for profile desktop-localhost-8080 alone"+[ -d "$shared_profile_dir" ] || fail "destroy removed Desktop's shared 8080 profile"+[ -f "$MULTICA_DEV_HOME/envs/poisoned-907/manifest.env" ] || fail "destroy discarded the poisoned manifest"+rm -rf "$MULTICA_DEV_HOME/envs/poisoned-907" "$shared_profile_dir"+# ---------------------------------------------------------------------------# Unknown names and components fail loudly instead of doing something else.# ---------------------------------------------------------------------------