Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(dev-env): restart a daemon that is not running the current build (MUL-9502) (#10827)
Jiayuan Zhang358928eab0a7
diff --git a/scripts/dev-env.sh b/scripts/dev-env.shindex 9efac25c08..8dba0f059e 100755--- a/scripts/dev-env.sh+++ b/scripts/dev-env.sh@@ -853,12 +853,53 @@ ensure_fixture_issues() {ok "seeded 2 issues in workspace $WORKSPACE_SLUG"}+daemon_status_json() {+ "${CLEAN_ENV[@]}" MULTICA_WORKSPACES_ROOT="$WORKSPACES_ROOT" \+ "$MULTICA_BIN" daemon status --profile "$PROFILE" --output json 2>/dev/null || true+}++# A running daemon keeps executing the binary it was started from, and+# `daemon start` is a no-op while one is up, so rebuilding alone never reaches+# it. Uncommitted edits change the binary but not the commit it reports, so+# `start_daemon` records "<pid> <sha256 of the binary>" for the daemon it+# started, and a daemon is current only when both match.+daemon_build_file() { printf '%s/daemon.build' "$STATE_DIR"; }++file_sha256() {+ { shasum -a 256 "$1" 2>/dev/null || sha256sum "$1" 2>/dev/null; } | awk '{print $1}' || true+}++short_commit() {+ [ -n "$1" ] && [ "$1" != unknown ] || { printf 'unknown'; return 0; }+ git -C "$DIR" rev-parse --short "$1" 2>/dev/null || printf '%.10s' "$1"+}++# Prints why the daemon in a `daemon status` payload is not running this+# checkout's current build, or nothing when it is.+daemon_stale_reason() {+ local status=$1 expected_commit=$2 bin_hash=$3 running_commit pid recorded+ running_commit="$(json_field "$status" cli_commit || true)"+ pid="$(json_field "$status" pid || true)"+ if [ "$running_commit" != "$expected_commit" ]; then+ printf 'runs commit %s, checkout is at %s' \+ "$(short_commit "$running_commit")" "$(short_commit "$expected_commit")"+ return 0+ fi+ recorded="$(cat "$(daemon_build_file)" 2>/dev/null || true)"+ if [ "${recorded%% *}" != "$pid" ]; then+ printf 'was not started from %s by make up' "$MULTICA_BIN"+ elif [ "${recorded#* }" != "$bin_hash" ]; then+ printf 'runs an older build of commit %s (uncommitted changes differ)' "$(short_commit "$running_commit")"+ fi+ return 0+}+# Under a daemon-task marker the CLI refuses daemon commands for every profile# except one this registry assigns to an environment, reached without the task's# identity and pointed at that environment's API — exactly how CLEAN_ENV and# the profile written above launch it (server/cmd/multica/dev_env_profile.go).start_daemon() {- local status state build_commit+ local status state pid build_commit bin_hash reason active# Built, never `go run`: the daemon records its own executable path at startup# and re-execs it as the execution-environment helper for every task. Under@@ -868,12 +909,35 @@ start_daemon() {info "Building $MULTICA_BIN (a go run daemon would fail every task later)."build_commit="$(git -C "$REPO_ROOT" rev-parse HEAD 2>/dev/null || printf 'unknown')"(cd "$REPO_ROOT/server" && go build -ldflags "-X main.buildCommit=$build_commit" -o bin/multica ./cmd/multica) || die "Failed to build the multica CLI."+ bin_hash="$(file_sha256 "$MULTICA_BIN")"++ status="$(daemon_status_json)"+ state="$(json_field "$status" status || true)"+ if [ "$state" = running ] || [ "$state" = starting ]; then+ pid="$(json_field "$status" pid || echo '?')"+ reason="$(daemon_stale_reason "$status" "$build_commit" "$bin_hash")"+ if [ -z "$reason" ] && [ "$state" = running ]; then+ ok "daemon already running for profile $PROFILE (pid $pid, commit $(short_commit "$build_commit"))"+ return 0+ fi+ if [ -n "$reason" ]; then+ active="$(json_field "$status" active_task_count || echo 0)"+ if [ "$active" -gt 0 ] 2>/dev/null; then+ warn "daemon pid $pid $reason; restarting it (interrupts $active active task(s))."+ else+ warn "daemon pid $pid $reason; restarting it."+ fi+ "${CLEAN_ENV[@]}" MULTICA_WORKSPACES_ROOT="$WORKSPACES_ROOT" \+ "$MULTICA_BIN" daemon stop --force --profile "$PROFILE" >/dev/null 2>&1 \+ || die "Could not stop the stale daemon pid $pid for profile $PROFILE. Log: $PROFILE_DIR/daemon.log"+ rm -f "$(daemon_build_file)"+ fi+ fi"${CLEAN_ENV[@]}" MULTICA_WORKSPACES_ROOT="$WORKSPACES_ROOT" \"$MULTICA_BIN" daemon start --profile "$PROFILE" 2>&1 | sed 's/^/ /' || true- status="$("${CLEAN_ENV[@]}" MULTICA_WORKSPACES_ROOT="$WORKSPACES_ROOT" \- "$MULTICA_BIN" daemon status --profile "$PROFILE" --output json 2>/dev/null || true)"+ status="$(daemon_status_json)"state="$(json_field "$status" status || echo unknown)"# `daemon status` reports "stopped" plus port_conflict when the daemon# answering this profile's health port belongs to another profile, so a@@ -884,7 +948,12 @@ start_daemon() {fidie "Daemon is '$state' after start. Log: $PROFILE_DIR/daemon.log"fi- ok "daemon running for profile $PROFILE (pid $(json_field "$status" pid || echo '?'))"+ pid="$(json_field "$status" pid || echo '?')"+ if [ "$(json_field "$status" cli_commit || true)" != "$build_commit" ]; then+ die "Daemon pid $pid reports commit $(short_commit "$(json_field "$status" cli_commit || true)") after start, not $(short_commit "$build_commit"). Run 'make down C=daemon' and retry."+ fi+ printf '%s %s\n' "$pid" "$bin_hash" > "$(daemon_build_file)"+ ok "daemon running for profile $PROFILE (pid $pid, commit $(short_commit "$build_commit"))"}start_desktop() {@@ -1192,12 +1261,27 @@ component_state() {fi;;daemon)- local status state+ local status state pid running_commit reasonif [ -x "$MULTICA_BIN" ]; then- status="$("${CLEAN_ENV[@]}" MULTICA_WORKSPACES_ROOT="$WORKSPACES_ROOT" \- "$MULTICA_BIN" daemon status --profile "$PROFILE" --output json 2>/dev/null || true)"+ status="$(daemon_status_json)"state="$(json_field "$status" status || echo stopped)"- printf '%s|%s|pid %s' "$state" "$PROFILE" "$(json_field "$status" pid || echo '-')"+ pid="$(json_field "$status" pid || echo '-')"+ if [ "$state" != running ] && [ "$state" != starting ]; then+ printf '%s|%s|pid %s' "$state" "$PROFILE" "$pid"+ return 0+ fi+ running_commit="$(json_field "$status" cli_commit || true)"+ # Measured against server/bin/multica as it is now: an edit that has+ # not been rebuilt yet shows up only once `make up` compiles it.+ reason="$(daemon_stale_reason "$status" \+ "$(git -C "$DIR" rev-parse HEAD 2>/dev/null || printf 'unknown')" \+ "$(file_sha256 "$MULTICA_BIN")")"+ if [ -n "$reason" ]; then+ printf 'stale|%s|pid %s %s; run make up C=daemon to restart' "$PROFILE" "$pid" "$reason"+ else+ printf '%s|%s|pid %s commit %s up %s' "$state" "$PROFILE" "$pid" \+ "$(short_commit "$running_commit")" "$(json_field "$status" uptime || echo '?')"+ fielseprintf 'stopped|%s|not built' "$PROFILE"fidiff --git a/scripts/dev-env.test.sh b/scripts/dev-env.test.shindex 8d7a1a4190..284b5d0dea 100644--- a/scripts/dev-env.test.sh+++ b/scripts/dev-env.test.sh@@ -426,6 +426,91 @@ assert_start_web hung 200 never 0 "did not answer within 300s; restarting it" $'assert_start_web foreign 999 0 1 "Port 13000 is busy: pid 999" ""assert_start_web foreign-silent 999 never 1 "Port 13000 is busy: pid 999" ""+# ---------------------------------------------------------------------------+# Daemon: `daemon start` is a no-op while one is running, so `make up` has to+# restart a daemon that is not running this checkout's current build itself.+# ---------------------------------------------------------------------------+head_commit="$(git -C "$root_dir" rev-parse HEAD)"+old_commit=0123456789abcdef0123456789abcdef01234567+old_short="$(git -C "$root_dir" rev-parse --short "$old_commit")"+daemon_fake="$tmp_dir/daemon-fake"+mkdir -p "$daemon_fake/server/bin" "$daemon_fake/go-bin"+printf '#!/usr/bin/env bash\nexit 0\n' > "$daemon_fake/go-bin/go"+chmod +x "$daemon_fake/go-bin/go"+cat > "$daemon_fake/server/bin/multica" <<'EOF'+#!/usr/bin/env bash+printf '%s\n' "$*" >> "$FAKE_DAEMON/calls"+case "$1 $2" in+ "daemon status") cat "$FAKE_DAEMON/status" ;;+ "daemon stop") printf '{"status":"stopped"}' > "$FAKE_DAEMON/status" ;;+ "daemon start")+ printf '{"status":"running","pid":5002,"cli_commit":"%s","uptime":"1s"}' \+ "$(cat "$FAKE_DAEMON/start_commit")" > "$FAKE_DAEMON/status" ;;+esac+EOF+chmod +x "$daemon_fake/server/bin/multica"++# Args: case, commit the running daemon reports, recorded "<pid> <hash>" (or+# "-" for none), commit a fresh start reports, expected exit, expected output,+# expected record afterwards.+assert_start_daemon() (+ local case_name=$1 running_commit=$2 recorded=$3 start_commit=$4+ local expected_status=$5 expected=$6 expected_record=$7 status=0 bin_hash+ export FAKE_DAEMON="$tmp_dir/daemon-$case_name"+ mkdir -p "$FAKE_DAEMON"+ printf '{"status":"running","pid":5001,"cli_commit":"%s","active_task_count":1}' \+ "$running_commit" > "$FAKE_DAEMON/status"+ printf '%s' "$start_commit" > "$FAKE_DAEMON/start_commit"+ # shellcheck disable=SC1090+ source "$root_dir/scripts/dev-env.sh"+ PATH="$daemon_fake/go-bin:$PATH"+ DIR="$root_dir" PROFILE=dev-test STATE_DIR="$FAKE_DAEMON" PROFILE_DIR="$FAKE_DAEMON"+ WORKSPACES_ROOT="$FAKE_DAEMON/ws" MULTICA_BIN="$daemon_fake/server/bin/multica"+ bin_hash="$(file_sha256 "$MULTICA_BIN")"+ [ -n "$bin_hash" ] || fail "file_sha256 printed nothing"+ case "$recorded" in+ -) ;;+ current) printf '5001 %s\n' "$bin_hash" > "$(daemon_build_file)" ;;+ *) printf '%s\n' "$recorded" > "$(daemon_build_file)" ;;+ esac+ (start_daemon) > "$out" 2>&1 || status=$?+ [ "$status" = "$expected_status" ] || { sed 's/^/ /' "$out" >&2; fail "$case_name: start_daemon exited $status, want $expected_status"; }+ require_contains "$out" "$expected"+ case "$expected_record" in+ restarted) [ "$(cat "$(daemon_build_file)")" = "5002 $bin_hash" ] \+ || fail "$case_name: recorded build is '$(cat "$(daemon_build_file)" 2>/dev/null)', want the restarted daemon" ;;+ kept) ! grep -q '^daemon stop' "$FAKE_DAEMON/calls" \+ || fail "$case_name: stopped a daemon that already ran the current build" ;;+ esac+)+assert_start_daemon current "$head_commit" current "$head_commit" 0 "daemon already running for profile dev-test (pid 5001" kept+assert_start_daemon old-commit "$old_commit" current "$head_commit" 0 "runs commit $old_short, checkout is at" restarted+assert_start_daemon dirty-edit "$head_commit" "5001 not-this-binary" "$head_commit" 0 "uncommitted changes differ" restarted+assert_start_daemon unrecorded "$head_commit" - "$head_commit" 0 "was not started from" restarted+assert_start_daemon other-pid "$head_commit" "4000 x" "$head_commit" 0 "interrupts 1 active task(s)" restarted+assert_start_daemon wrong-start "$old_commit" current "$old_commit" 1 "reports commit $old_short after start" -++# `make status` shows the daemon's commit, and calls a daemon that `make up`+# would restart stale instead of running.+assert_daemon_state() (+ local case_name=$1 running_commit=$2 recorded=$3 expected_state=$4 expected=$5 row+ export FAKE_DAEMON="$tmp_dir/daemon-state-$case_name"+ mkdir -p "$FAKE_DAEMON"+ printf '{"status":"running","pid":5001,"cli_commit":"%s","uptime":"3m0s"}' \+ "$running_commit" > "$FAKE_DAEMON/status"+ # shellcheck disable=SC1090+ source "$root_dir/scripts/dev-env.sh"+ DIR="$root_dir" PROFILE=dev-test STATE_DIR="$FAKE_DAEMON" PROFILE_DIR="$FAKE_DAEMON"+ WORKSPACES_ROOT="$FAKE_DAEMON/ws" MULTICA_BIN="$daemon_fake/server/bin/multica"+ [ "$recorded" = - ] || printf '5001 %s\n' "$(file_sha256 "$MULTICA_BIN")" > "$(daemon_build_file)"+ row="$(component_state daemon)"+ [ "${row%%|*}" = "$expected_state" ] || fail "$case_name: daemon state row '$row', want $expected_state"+ case "$row" in *"$expected"*) ;; *) fail "$case_name: daemon state row '$row', want '$expected'" ;; esac+)+assert_daemon_state current "$head_commit" current running "pid 5001 commit $(git -C "$root_dir" rev-parse --short HEAD) up 3m0s"+assert_daemon_state old-commit "$old_commit" current stale "runs commit $old_short, checkout is at"+assert_daemon_state unrecorded "$head_commit" - stale "run make up C=daemon to restart"+# ---------------------------------------------------------------------------# Desktop: CDP port, env file, process tree, and the daemon Desktop starts.# ---------------------------------------------------------------------------