mirror of
https://gh-proxy.org/https://github.com/klinsmaya/multica.git
synced 2026-11-08 09:26:50 +08:00
* MUL-6479 feat(dev): make a local environment a named object with one verb per lifecycle step Local development had six entry points, thirty-four make targets and no way to see or delete what they created. `make up` / `status` / `list` / `down` / `destroy` / `gc` / `env-exec` replace that with one set of verbs, shared by humans and agents, with `C=api,web,daemon,desktop` selecting components. Three defects made the old flow untrustworthy rather than merely verbose. 1. The database the tooling created was not the one the application reached. `ensure-postgres.sh` creates through `docker exec`; when a native PostgreSQL owns 5432 the container never binds the host port, so the create lands in a server the backend never talks to. It printed "✓ PostgreSQL ready" and the next step died with `SQLSTATE 3D000`. On the machine this was written on the two servers had already diverged: 94 same-named databases, 69 more in the container with no counterpart. `up` now creates through DATABASE_URL and treats a successful `migrate up` — which pings that same string — as the only proof; failure diagnoses the port owner instead of reporting a missing database. 2. Identity was computed, not allocated. Ports, database names and CLI profiles all came from `cksum($PWD) % 1000`, and a collision was silent: the loser fails to bind while the winner keeps answering 200. Allocation now happens under a lock, probing from the same path hash so a checkout keeps its familiar numbers, and requires the slot to be both unregistered and free of a live listener. The manifest in ~/.multica/dev/envs/<name>/ is what every later command reads. 3. Creating an environment left no record of how to destroy it, so there was no destroy — only archaeology. `down` stops processes and keeps the data; `destroy` consumes the manifest: processes, database, profile, slot. `list` makes leaks visible and `gc` collects environments whose checkout is gone or whose TTL passed. /health now reports pid, commit and started_at. A 200 alone proves something is listening, which is exactly what let a failed restart keep serving the previous build; `up` compares started_at against its own launch and refuses an answer that predates it. Two constraints are enforced rather than documented: the daemon is started from a built server/bin/multica, never `go run` (the daemon re-execs its own path for every task, and `go run` deletes that binary when the launcher exits), and `C=daemon` under a .multica/daemon_task_context.json marker stops with that explanation up front instead of spending a login on a refusal. scripts/dev-env.test.sh covers the registry verbs against a throwaway MULTICA_DEV_HOME with no services running, and pins the regression that made `down` exit 1 after reporting success: on bash 3.2 a command substitution whose function ends in a failing command aborts the script under `set -e`, and "no process is listening" is that function's normal answer. `make dev` is unchanged and still runs in the foreground. Co-authored-by: multica-agent <[email protected]> * fix(dev): close local environment lifecycle gaps Co-authored-by: multica-agent <[email protected]> --------- Co-authored-by: Lambda <[email protected]> Co-authored-by: multica-agent <[email protected]>
233 lines
9.9 KiB
Bash
233 lines
9.9 KiB
Bash
#!/usr/bin/env bash
|
|
# Registry-level behaviour of scripts/dev-env.sh, with no services started.
|
|
#
|
|
# Everything here runs against a throwaway MULTICA_DEV_HOME holding hand-written
|
|
# manifests, so the verbs are exercised end to end without a database, a
|
|
# backend, or a port.
|
|
set -euo pipefail
|
|
|
|
root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
tmp_dir="$(mktemp -d)"
|
|
trap 'rm -rf "$tmp_dir"' EXIT
|
|
|
|
export MULTICA_DEV_HOME="$tmp_dir/dev"
|
|
export MULTICA_DEV_WORKSPACES_PARENT="$tmp_dir/workspaces-parent"
|
|
export MULTICA_DEV_DESKTOP_APP_DATA="$tmp_dir/app-data"
|
|
export MULTICA_DEV_PROFILES_HOME="$tmp_dir/profiles"
|
|
|
|
fake_bin="$tmp_dir/bin"
|
|
mkdir -p "$fake_bin"
|
|
cat > "$fake_bin/psql" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
case " $* " in
|
|
*" DROP DATABASE "*) [ "${FAIL_DROP:-0}" != 1 ] ;;
|
|
*) printf '1\n' ;;
|
|
esac
|
|
EOF
|
|
chmod +x "$fake_bin/psql"
|
|
export PATH="$fake_bin:$PATH"
|
|
|
|
fail() {
|
|
echo "FAIL: $*" >&2
|
|
exit 1
|
|
}
|
|
|
|
require_contains() {
|
|
local file=$1 expected=$2
|
|
if ! grep -Fq "$expected" "$file"; then
|
|
echo "Expected output to contain: $expected" >&2
|
|
echo "Observed:" >&2
|
|
sed 's/^/ /' "$file" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
dev_env() {
|
|
bash "$root_dir/scripts/dev-env.sh" "$@"
|
|
}
|
|
|
|
write_manifest() {
|
|
local name=$1 dir=$2 offset=$3
|
|
local profile="dev-dev-env-test-$offset"
|
|
mkdir -p "$MULTICA_DEV_HOME/envs/$name/logs"
|
|
cat > "$MULTICA_DEV_HOME/envs/$name/manifest.env" <<EOF
|
|
NAME=$name
|
|
DIR=$(printf '%q' "$dir")
|
|
CREATED_AT=2026-01-01T00:00:00Z
|
|
OWNER=agent
|
|
TTL_HOURS=0
|
|
ENV_FILE=.env.example
|
|
OFFSET=$offset
|
|
BACKEND_PORT=$((18080 + offset))
|
|
FRONTEND_PORT=$((13000 + offset))
|
|
DB_NAME=multica_dev_env_test_$offset
|
|
DATABASE_URL=postgres://multica:multica@localhost:5432/multica_dev_env_test_$offset?sslmode=disable
|
|
PROFILE=$profile
|
|
WORKSPACES_ROOT=$(printf '%q' "$MULTICA_DEV_WORKSPACES_PARENT/multica_workspaces_$profile")
|
|
DESKTOP_RENDERER_PORT=$((5174 + offset))
|
|
DESKTOP_APP_SUFFIX=$name
|
|
EOF
|
|
}
|
|
|
|
out="$tmp_dir/out"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# An empty registry is a normal state, not an error.
|
|
# ---------------------------------------------------------------------------
|
|
dev_env list > "$out" 2>&1 || fail "list on an empty registry must succeed"
|
|
require_contains "$out" "No environments registered"
|
|
|
|
dev_env list --json > "$out" 2>&1 || fail "list --json on an empty registry must succeed"
|
|
if [ "$(cat "$out")" != "[]" ]; then
|
|
fail "list --json on an empty registry = $(cat "$out"), want []"
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Manifest serialization and user-provided names are safe. A manifest is
|
|
# sourced by Bash, so values must be shell-escaped and a name must never be
|
|
# able to walk outside envs/ before destroy eventually runs rm -rf.
|
|
# ---------------------------------------------------------------------------
|
|
quoted="$tmp_dir/quoted.env"
|
|
dangerous='a path with spaces;$(touch should-not-exist)'
|
|
bash -c 'source "$1"; write_manifest_value DIR "$2"' _ "$root_dir/scripts/dev-env.sh" "$dangerous" > "$quoted"
|
|
loaded="$(bash -c 'source "$1"; printf %s "$DIR"' _ "$quoted")"
|
|
[ "$loaded" = "$dangerous" ] || fail "manifest value did not round-trip safely"
|
|
[ ! -e "$root_dir/should-not-exist" ] || fail "loading a manifest executed its value"
|
|
|
|
status=0
|
|
dev_env up --name ../../escape > "$out" 2>&1 || status=$?
|
|
[ "$status" -ne 0 ] || fail "up accepted a path-traversing environment name"
|
|
require_contains "$out" "Invalid environment name"
|
|
|
|
status=0
|
|
dev_env up --ttl nope > "$out" 2>&1 || status=$?
|
|
[ "$status" -ne 0 ] || fail "up accepted a non-numeric TTL"
|
|
require_contains "$out" "TTL must be a positive integer"
|
|
|
|
# Rewriting an allocated database name must preserve the existing connection
|
|
# endpoint, credentials and query parameters.
|
|
rewritten="$(bash -c 'source "$1"; database_url_with_name "$2" "$3"' _ \
|
|
"$root_dir/scripts/dev-env.sh" \
|
|
'postgres://dev:p%[email protected]:55432/old_db?sslmode=require&application_name=dev' \
|
|
'new_db')"
|
|
[ "$rewritten" = 'postgres://dev:p%[email protected]:55432/new_db?sslmode=require&application_name=dev' ] \
|
|
|| fail "database URL rewrite changed more than the database name: $rewritten"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# A registered environment is visible to both renderings, and the JSON one
|
|
# parses — agents read it, so a stray log line in it is a broken contract.
|
|
# ---------------------------------------------------------------------------
|
|
write_manifest "probe-901" "$tmp_dir/checkout" 901
|
|
mkdir -p "$tmp_dir/checkout"
|
|
|
|
dev_env list > "$out" 2>&1 || fail "list must succeed with one environment"
|
|
require_contains "$out" "probe-901"
|
|
require_contains "$out" "18981"
|
|
|
|
dev_env status probe-901 --json > "$out" 2>&1 || fail "status --json must succeed"
|
|
node -e '
|
|
const fs = require("fs");
|
|
const payload = JSON.parse(fs.readFileSync(process.argv[1], "utf8"));
|
|
if (payload.name !== "probe-901") throw new Error("name = " + payload.name);
|
|
if (payload.backend_port !== 18981) throw new Error("backend_port = " + payload.backend_port);
|
|
for (const key of ["api", "web", "daemon", "desktop"]) {
|
|
if (!payload.components[key]) throw new Error("missing component " + key);
|
|
if (payload.components[key].state !== "stopped") {
|
|
throw new Error(key + " state = " + payload.components[key].state);
|
|
}
|
|
}
|
|
' "$out" || fail "status --json is not machine-readable"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Stopping an environment that is not running is a no-op that SUCCEEDS.
|
|
#
|
|
# This is the regression that made `make down` exit 1 after reporting success:
|
|
# on bash 3.2 a command substitution whose function ends in a failing command
|
|
# aborts the whole script under `set -e`, and "no process is listening on this
|
|
# port" is that function's normal answer.
|
|
# ---------------------------------------------------------------------------
|
|
status=0
|
|
dev_env down probe-901 --components api,web > "$out" 2>&1 || status=$?
|
|
if [ "$status" -ne 0 ]; then
|
|
echo "Observed:" >&2
|
|
sed 's/^/ /' "$out" >&2
|
|
fail "down on a stopped environment exited $status, want 0"
|
|
fi
|
|
require_contains "$out" "stopped"
|
|
|
|
# Commands launched through env-exec must not inherit the daemon-task identity
|
|
# hints that make human/profile CLI commands reject --profile.
|
|
write_manifest "clean-env-903" "$root_dir" 903
|
|
MULTICA_TASK_CONFIG_ROOT=/task/config \
|
|
MULTICA_TASK_WORKSPACES_ROOT=/task/workspaces \
|
|
MULTICA_WORKSPACES_ROOT=/owner/workspaces \
|
|
dev_env exec clean-env-903 -- sh -c '
|
|
test -z "${MULTICA_TASK_CONFIG_ROOT:-}" &&
|
|
test -z "${MULTICA_TASK_WORKSPACES_ROOT:-}" &&
|
|
test "$MULTICA_WORKSPACES_ROOT" = "$1"
|
|
' _ "$MULTICA_DEV_WORKSPACES_PARENT/multica_workspaces_dev-dev-env-test-903" \
|
|
> "$out" 2>&1 || fail "env-exec leaked daemon task identity or owner workspaces root"
|
|
|
|
# A health response without process identity is never proof that the process is
|
|
# this checkout's freshly launched API.
|
|
if bash -c 'source "$1"; api_started_after '\''{"status":"ok"}'\'' 1' _ "$root_dir/scripts/dev-env.sh"; then
|
|
fail "legacy /health without started_at was accepted as current"
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Unknown names and components fail loudly instead of doing something else.
|
|
# ---------------------------------------------------------------------------
|
|
status=0
|
|
dev_env status no-such-env > "$out" 2>&1 || status=$?
|
|
[ "$status" -ne 0 ] || fail "status on an unknown environment must fail"
|
|
require_contains "$out" "Unknown environment"
|
|
|
|
status=0
|
|
dev_env up --components nope > "$out" 2>&1 || status=$?
|
|
[ "$status" -ne 0 ] || fail "up with an unknown component must fail"
|
|
require_contains "$out" "Unknown component"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# gc reports what it would collect and touches nothing in --dry-run. An
|
|
# environment whose checkout is gone has no owner left to stop it, which is how
|
|
# 152 databases accumulated with nothing on the machine able to list them.
|
|
# ---------------------------------------------------------------------------
|
|
write_manifest "orphan-902" "$tmp_dir/deleted-checkout" 902
|
|
|
|
dev_env gc --dry-run > "$out" 2>&1 || fail "gc --dry-run must succeed"
|
|
require_contains "$out" "orphan-902 would be collected"
|
|
if grep -Fq "probe-901 would be collected" "$out"; then
|
|
fail "gc must not collect an environment whose directory still exists"
|
|
fi
|
|
[ -f "$MULTICA_DEV_HOME/envs/orphan-902/manifest.env" ] || fail "gc --dry-run deleted a manifest"
|
|
|
|
# A failed database drop keeps the manifest and slot so cleanup can be retried;
|
|
# destroy must never print success and forget the only deletion recipe.
|
|
write_manifest "drop-fails-904" "$root_dir" 904
|
|
status=0
|
|
FAIL_DROP=1 dev_env destroy drop-fails-904 --yes > "$out" 2>&1 || status=$?
|
|
[ "$status" -ne 0 ] || fail "destroy succeeded after DROP DATABASE failed"
|
|
[ -f "$MULTICA_DEV_HOME/envs/drop-fails-904/manifest.env" ] \
|
|
|| fail "destroy discarded the manifest after DROP DATABASE failed"
|
|
require_contains "$out" "manifest and slot were kept"
|
|
dev_env destroy drop-fails-904 --yes > "$out" 2>&1 || fail "retrying destroy after database recovery failed"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# destroy consumes the manifest: the slot is free afterwards, which is what
|
|
# makes the registry an allocator rather than a second place to leak.
|
|
# ---------------------------------------------------------------------------
|
|
dev_env destroy probe-901 --yes > "$out" 2>&1 || fail "destroy must succeed"
|
|
[ ! -d "$MULTICA_DEV_HOME/envs/probe-901" ] || fail "destroy left the environment directory behind"
|
|
|
|
dev_env list > "$out" 2>&1 || fail "list must succeed after destroy"
|
|
if grep -Fq "probe-901" "$out"; then
|
|
fail "destroyed environment is still listed"
|
|
fi
|
|
|
|
# Declining the confirmation is a successful no-op, not a failure.
|
|
printf 'n\n' | dev_env destroy orphan-902 > "$out" 2>&1 || fail "declining destroy must exit 0"
|
|
require_contains "$out" "Cancelled."
|
|
[ -d "$MULTICA_DEV_HOME/envs/orphan-902" ] || fail "declined destroy removed the environment anyway"
|
|
|
|
echo "✓ dev-env.sh registry behaviour verified"
|