Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-8787 feat(dev-env): make up C=desktop starts a signed-in Desktop with an optional CDP port (#10141)
Jiayuan Zhang9b64577708f8
diff --git a/Makefile b/Makefileindex 2f849b544c..72ed29e900 100644--- a/Makefile+++ b/Makefile@@ -152,6 +152,7 @@ selfhost-stop: ## Stop the self-hosted Docker Compose stack# make up api + web# make up C=api,web,daemon add the agent daemon# make up C=desktop Electron against this environment's backend+# make up C=desktop ARGS=--cdp ...signed in, with a DevTools port for Playwright# make up ARGS=--ephemeral agent-owned, expires, collected by `make gc`up: ## Start this checkout's environment (C=api,web,daemon,desktop; default api,web)diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.tsindex ccedabf32a..bcf7d87418 100644--- a/apps/desktop/src/main/index.ts+++ b/apps/desktop/src/main/index.ts@@ -750,6 +750,15 @@ if (!gotTheLock) {);if (coldStartDeepLink) handleDeepLink(coldStartDeepLink);+ // scripts/dev-env.sh starts a managed dev Desktop signed in by handing it+ // the token a browser login would deliver. An environment variable rather+ // than argv keeps the token out of other users' `ps`; it is removed before+ // any child process (the daemon, agent CLIs) can inherit it. Packaged builds+ // never read it.+ const devAuthToken = is.dev ? process.env.MULTICA_DEV_AUTH_TOKEN : undefined;+ delete process.env.MULTICA_DEV_AUTH_TOKEN;+ if (devAuthToken) dispatchToMainRenderer("auth:token", devAuthToken);+app.whenReady().then(async () => {const viteEnv = import.meta.env as ImportMetaEnv & {readonly VITE_API_URL?: string;diff --git a/scripts/dev-env.sh b/scripts/dev-env.shindex ccf3e42ed7..307c76f9bd 100755--- a/scripts/dev-env.sh+++ b/scripts/dev-env.sh@@ -169,6 +169,7 @@ manifest_field() {case "$key" inDIR) printf '%s' "$DIR" ;;OFFSET) printf '%s' "$OFFSET" ;;+ DESKTOP_CDP_PORT) printf '%s' "${DESKTOP_CDP_PORT:-}" ;;*) return 1 ;;esac)@@ -254,6 +255,51 @@ desktop_user_data_dir() {printf '%s/Multica Canary %s' "$(desktop_app_data_root)" "$1"}+# Desktop's daemon manager derives its own CLI profile from the backend URL+# (apps/desktop/src/main/daemon-profile.ts), so every managed Desktop that+# signs in owns `desktop-localhost-<backend port>` alongside this environment's+# profile. Its daemon outlives Electron unless something stops it.+desktop_daemon_profile() {+ printf 'desktop-localhost-%s' "$BACKEND_PORT"+}++# Chrome's conventional 9222 is left to whatever a human launches by hand.+CDP_PORT_BASE=9223++valid_port() {+ case "$1" in+ ""|*[!0-9]*) return 1 ;;+ esac+ [ "$1" -ge 1024 ] && [ "$1" -le 65535 ]+}++cdp_port_registered() {+ local name+ while read -r name; do+ [ -n "$name" ] || continue+ [ "$name" = "${2:-}" ] && continue+ if [ "$(manifest_field "$name" DESKTOP_CDP_PORT)" = "$1" ]; then+ return 0+ fi+ done <<EOF+$(list_env_names)+EOF+ return 1+}++allocate_cdp_port() {+ local self=$1 start=$2 i port+ for i in $(seq 0 999); do+ port=$((start + i))+ [ "$port" -le 65535 ] || return 1+ cdp_port_registered "$port" "$self" && continue+ port_free "$port" || continue+ printf '%s' "$port"+ return 0+ done+ return 1+}+offset_registered() {local namewhile read -r name; do@@ -485,6 +531,27 @@ process_parent_id() {ps -p "$1" -o ppid= 2>/dev/null | tr -d ' ' || true}+# Every live descendant of a launcher, read from one process-table snapshot.+# Turbo runs a task in its own process group, so a group kill of the launcher+# reaches turbo but not electron-vite or Electron below it; they are found+# here, while the launcher still anchors their PPID chain.+descendant_pids() {+ [ -n "$1" ] || return 0+ ps -axo pid=,ppid= 2>/dev/null | awk -v root="$1" '+ { parent[$1] = $2; order[NR] = $1 }+ END {+ keep[root] = 1+ do {+ changed = 0+ for (i = 1; i <= NR; i++) {+ pid = order[i]+ if (!(pid in keep) && (parent[pid] in keep)) { keep[pid] = 1; changed = 1 }+ }+ } while (changed)+ for (pid in keep) if (pid != root) print pid+ }' || true+}+# Package runners may put a descendant in a nested process group (for example,# Turbo does this before Next binds its port). Follow PPIDs so ownership still# comes from the launcher tree instead of accepting a weaker command/user match.@@ -676,6 +743,7 @@ ensure_credentials() {ws="$(json_field "$(cat "$config")" workspace_id || true)"if [ -n "$pat" ] && curl -sf --max-time 5 "$server/api/me" -H "Authorization: Bearer $pat" >/dev/null 2>&1; thenWORKSPACE_ID="$ws"+ DEV_TOKEN="$pat"write_profile_config "$config" "$pat" "$ws"ok "CLI profile $PROFILE already authenticated"return 0@@ -722,9 +790,35 @@ Do not retry immediately — repeated attempts lock the code. Wait ~40s and re-rwrite_profile_config "$config" "$pat" "$ws"WORKSPACE_ID="$ws"+ DEV_TOKEN="$pat"ok "Logged in as $DEV_EMAIL and wrote profile $PROFILE"}+# A fresh workspace has no issues, so neither an issue page nor the dedicated+# issue window has anything to open. Two are created once; a workspace that+# already holds any issue is left as it is.+ensure_fixture_issues() {+ local server="http://localhost:${BACKEND_PORT}" list total entry status title+ list="$(curl -sf --max-time 5 "$server/api/issues?limit=1" \+ -H "Authorization: Bearer $DEV_TOKEN" -H "X-Workspace-ID: $WORKSPACE_ID" || true)"+ total="$(json_field "$list" total || true)"+ if [ -z "$total" ]; then+ warn "could not list issues in workspace $WORKSPACE_SLUG; skipped the issue fixture"+ return 0+ fi+ [ "$total" = 0 ] || return 0+ for entry in "todo|Fixture: issue list and board" "in_progress|Fixture: dedicated issue window"; do+ status="${entry%%|*}"+ title="${entry#*|}"+ curl -sf --max-time 5 -X POST "$server/api/issues" \+ -H "Authorization: Bearer $DEV_TOKEN" -H "X-Workspace-ID: $WORKSPACE_ID" \+ -H 'Content-Type: application/json' \+ -d "{\"title\":\"$title\",\"status\":\"$status\",\"priority\":\"medium\",\"description\":\"Seeded by scripts/dev-env.sh so issue views have content.\"}" \+ >/dev/null || { warn "failed to create fixture issue '$title'"; return 0; }+ done+ ok "seeded 2 issues in workspace $WORKSPACE_SLUG"+}+# The CLI refuses `daemon start` anywhere under a daemon-task marker, so a task# cannot spawn a second daemon that competes for its own work. Checking the# marker here turns that refusal into one actionable line, before this component@@ -779,30 +873,37 @@ start_desktop() {if component_pid desktop >/dev/null \&& curl -sf --max-time 10 "http://localhost:${DESKTOP_RENDERER_PORT}" >/dev/null 2>&1 \&& listener_belongs_to_component desktop "$DESKTOP_RENDERER_PORT" \- && desktop_env_matches; then+ && desktop_env_matches \+ && [ "${DESKTOP_CDP_CHANGED:-0}" != 1 ] && desktop_cdp_ready; thenlistener="$(record_component_listener desktop "$DESKTOP_RENDERER_PORT" || true)"[ -n "$listener" ] || die "The Desktop listener changed while its identity was being recorded."- ok "desktop already running (pid $(component_pid desktop), renderer $listener on :$DESKTOP_RENDERER_PORT)"+ ok "desktop already running (pid $(component_pid desktop), renderer $listener on :$DESKTOP_RENDERER_PORT$(desktop_cdp_suffix))"return 0fiif component_pid desktop >/dev/null; then- warn "desktop launcher exists but its renderer/backend identity is stale; restarting it."+ warn "desktop launcher exists but its renderer/backend/CDP identity is stale; restarting it."stop_component desktopfiif ! port_free "$DESKTOP_RENDERER_PORT"; thendie "Desktop renderer port $DESKTOP_RENDERER_PORT is busy: $(describe_port_owner "$DESKTOP_RENDERER_PORT")."fi+ if [ -n "$DESKTOP_CDP_PORT" ] && ! port_free "$DESKTOP_CDP_PORT"; then+ die "Desktop CDP port $DESKTOP_CDP_PORT is busy: $(describe_port_owner "$DESKTOP_CDP_PORT"). Pick another with --remote-debugging-port N."+ fi- # The marker makes destroy remove only a file this tool owns. Explicit- # renderer/app values bind Desktop to the registry allocation rather than- # independently hashing the checkout path again.- cat > "$DESKTOP_ENV_FILE" <<EOF-# Managed by scripts/dev-env.sh for environment ${NAME}.-VITE_API_URL=http://localhost:${BACKEND_PORT}-VITE_WS_URL=ws://localhost:${BACKEND_PORT}/ws-EOF- launch_detached desktop env \- DESKTOP_RENDERER_PORT="$DESKTOP_RENDERER_PORT" DESKTOP_APP_SUFFIX="$DESKTOP_APP_SUFFIX" \+ ensure_credentials+ ensure_fixture_issues++ write_desktop_env_file+ # Desktop's main process delivers MULTICA_DEV_AUTH_TOKEN to the renderer the+ # way a browser login's deep link does, so the session is stored exactly as+ # it is for a human. electron-vite turns REMOTE_DEBUGGING_PORT into+ # --remote-debugging-port; turbo.json passes both through to the dev task.+ local launch_env=(env+ DESKTOP_RENDERER_PORT="$DESKTOP_RENDERER_PORT" DESKTOP_APP_SUFFIX="$DESKTOP_APP_SUFFIX"+ MULTICA_DEV_AUTH_TOKEN="$DEV_TOKEN")+ [ -z "$DESKTOP_CDP_PORT" ] || launch_env+=(REMOTE_DEBUGGING_PORT="$DESKTOP_CDP_PORT")+ launch_detached desktop "${launch_env[@]}" \make -C "$REPO_ROOT" -s desktop-dev ENV_FILE="$ENV_FILE"while [ "$waited" -lt 300 ]; do@@ -823,7 +924,18 @@ EOFdie "desktop exited during renderer bootstrap. Log: $(log_file desktop)"fiprintf '%s\n' "$listener" > "$(listener_pid_file desktop)"- ok "desktop ready (launcher $(component_pid desktop), renderer pid ${listener:-?}, backend :$BACKEND_PORT)"+ # Vite answers before electron-vite has built main and started Electron,+ # so the CDP endpoint is the proof that the app itself is up.+ while ! desktop_cdp_ready; do+ if ! component_pid desktop >/dev/null || [ "$waited" -ge 300 ]; then+ tail -20 "$(log_file desktop)" | sed 's/^/ /' >&2 || true+ stop_component desktop+ die "Desktop never served CDP on :$DESKTOP_CDP_PORT from its own process tree. Log: $(log_file desktop)"+ fi+ sleep 2+ waited=$((waited + 2))+ done+ ok "desktop ready (launcher $(component_pid desktop), renderer pid ${listener:-?}, backend :$BACKEND_PORT$(desktop_cdp_suffix))"return 0ficomponent_pid desktop >/dev/null \@@ -835,6 +947,33 @@ EOFdie "desktop renderer never became ready on :$DESKTOP_RENDERER_PORT. Log: $(log_file desktop)"}+# The marker makes destroy remove only a file this tool owns. Explicit+# renderer/app values bind Desktop to the registry allocation rather than+# independently hashing the checkout path again.+write_desktop_env_file() {+ cat > "$DESKTOP_ENV_FILE" <<EOF+# Managed by scripts/dev-env.sh for environment ${NAME}.+VITE_API_URL=http://localhost:${BACKEND_PORT}+VITE_WS_URL=ws://localhost:${BACKEND_PORT}/ws+EOF+}++desktop_env_file_managed() {+ grep -q '^# Managed by scripts/dev-env.sh for environment ' "$DESKTOP_ENV_FILE" 2>/dev/null+}++# Every environment owns a Desktop slot, so `up` without the desktop component+# still points the file at this backend: a Desktop started later through+# env-exec otherwise talks to a backend that no longer exists. A file someone+# wrote by hand is left alone unless the desktop component is selected.+sync_desktop_env_file() {+ if [ ! -f "$DESKTOP_ENV_FILE" ] || desktop_env_file_managed; then+ desktop_env_matches || { write_desktop_env_file; info "Desktop env file → backend :$BACKEND_PORT"; }+ else+ info "Left $DESKTOP_ENV_FILE alone: it was not written by this tool."+ fi+}+desktop_env_matches() {[ -f "$DESKTOP_ENV_FILE" ] \&& grep -Fqx "# Managed by scripts/dev-env.sh for environment ${NAME}." "$DESKTOP_ENV_FILE" \@@ -842,8 +981,40 @@ desktop_env_matches() {&& grep -Fqx "VITE_WS_URL=ws://localhost:${BACKEND_PORT}/ws" "$DESKTOP_ENV_FILE"}+desktop_cdp_ready() {+ [ -n "${DESKTOP_CDP_PORT:-}" ] || return 0+ curl -sf --max-time 5 "http://127.0.0.1:${DESKTOP_CDP_PORT}/json/version" >/dev/null 2>&1 \+ && listener_belongs_to_component desktop "$DESKTOP_CDP_PORT"+}++desktop_cdp_suffix() {+ [ -z "${DESKTOP_CDP_PORT:-}" ] || printf ', CDP :%s' "$DESKTOP_CDP_PORT"+}++# Electron does not stop the daemon it started when it quits (autoStop is off+# by default), and that daemon talks to this environment's backend only.+# A plain `daemon stop` exits 0 while the daemon is still finishing a run;+# --force waits for the profile lock, escalates, and confirms cleanup. The pid+# is checked again because the profile directory holding it is deleted next.+stop_desktop_daemon() {+ local profile pid bin+ 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+ bin="$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 \+ && ! kill -0 "$pid" 2>/dev/null; then+ ok "stopped Desktop's daemon for profile $profile"+ return 0+ fi+ warn "Desktop's daemon for profile $profile (pid $pid) is still running"+ return 1+}+stop_component() {local name=$1 pid launcher="" status state recorded_listener="" port="" listener=""+ local descendants="" child survivors=""case "$name" indaemon)if [ -x "$MULTICA_BIN" ]; then@@ -891,6 +1062,7 @@ stop_component() {printf '%s\n' "$listener" > "$(listener_pid_file "$name")"fifi+ descendants="$(descendant_pids "$pid")"# Negative pid targets the process group, so make → go run → server all go# down together instead of leaving the real listener orphaned.kill -TERM -"$pid" 2>/dev/null || kill -TERM "$pid" 2>/dev/null || true@@ -903,6 +1075,26 @@ stop_component() {warn "$name launcher pid $pid is still running"return 1fi+ # Descendants in a nested process group (turbo → electron-vite → Electron)+ # survive the group signal and would keep the app window open.+ for child in $descendants; do+ kill -0 "$child" 2>/dev/null && survivors="$survivors $child"+ done+ if [ -n "$survivors" ]; then+ for child in $survivors; do kill -TERM "$child" 2>/dev/null || true; done+ sleep 1+ for child in $survivors; do+ if kill -0 "$child" 2>/dev/null; then kill -KILL "$child" 2>/dev/null || true; fi+ done+ sleep 1+ for child in $survivors; do+ if kill -0 "$child" 2>/dev/null; then+ warn "$name descendant pid $child is still running"+ return 1+ fi+ done+ info "stopped$survivors left in $name's nested process groups"+ firm -f "$(pid_file "$name")"ok "$name stopped (pid $pid)"else@@ -935,6 +1127,7 @@ stop_component() {fifirm -f "$(listener_pid_file "$name")"+ [ "$name" != desktop ] || stop_desktop_daemon}# ------------------------------------------------------------------ status ---@@ -997,11 +1190,13 @@ component_state() {if [ -n "$pid" ] \&& curl -sf --max-time 10 "http://localhost:${DESKTOP_RENDERER_PORT}" >/dev/null 2>&1 \&& listener_belongs_to_component desktop "$DESKTOP_RENDERER_PORT" \- && desktop_env_matches; then- printf 'running|http://localhost:%s|launcher %s renderer %s' \- "$DESKTOP_RENDERER_PORT" "$pid" "$(port_listener_pid "$DESKTOP_RENDERER_PORT")"+ && desktop_env_matches && desktop_cdp_ready; then+ printf 'running|http://localhost:%s|launcher %s renderer %s%s' \+ "$DESKTOP_RENDERER_PORT" "$pid" "$(port_listener_pid "$DESKTOP_RENDERER_PORT")" \+ "$( [ -z "${DESKTOP_CDP_PORT:-}" ] || printf ' cdp http://127.0.0.1:%s' "$DESKTOP_CDP_PORT" )"elif [ -n "$pid" ] || [ -n "$(port_listener_pid "$DESKTOP_RENDERER_PORT")" ]; then- printf 'mismatch|http://localhost:%s|renderer/backend identity does not match' "$DESKTOP_RENDERER_PORT"+ printf 'mismatch|http://localhost:%s|renderer/backend%s identity does not match' "$DESKTOP_RENDERER_PORT" \+ "$( [ -z "${DESKTOP_CDP_PORT:-}" ] || printf '/CDP :%s' "$DESKTOP_CDP_PORT" )"elseprintf 'stopped|http://localhost:%s|' "$DESKTOP_RENDERER_PORT"fi@@ -1043,8 +1238,9 @@ print_status_json() {printf '{"name":"%s","dir":"%s","owner":"%s","created_at":"%s","ttl_hours":%s,"expires_at":"%s",' \"$(json_escape "$NAME")" "$(json_escape "$DIR")" "$(json_escape "$OWNER")" \"$(json_escape "$CREATED_AT")" "${TTL_HOURS:-0}" "$(json_escape "$EXPIRES_AT")"- printf '"backend_port":%s,"frontend_port":%s,"desktop_renderer_port":%s,"database":"%s","profile":"%s","env_file":"%s","logs":"%s","components":{' \- "$BACKEND_PORT" "$FRONTEND_PORT" "$DESKTOP_RENDERER_PORT" "$(json_escape "$DB_NAME")" "$(json_escape "$PROFILE")" \+ printf '"backend_port":%s,"frontend_port":%s,"desktop_renderer_port":%s,"desktop_cdp_port":%s,"workspace_slug":"%s","database":"%s","profile":"%s","env_file":"%s","logs":"%s","components":{' \+ "$BACKEND_PORT" "$FRONTEND_PORT" "$DESKTOP_RENDERER_PORT" "${DESKTOP_CDP_PORT:-null}" \+ "$(json_escape "$WORKSPACE_SLUG")" "$(json_escape "$DB_NAME")" "$(json_escape "$PROFILE")" \"$(json_escape "$ENV_FILE")" "$(json_escape "$LOG_DIR")"for comp in $ALL_COMPONENTS; dorow="$(component_state "$comp")"@@ -1067,6 +1263,12 @@ print_handoff() {elseentrypoint="API only http://localhost:${BACKEND_PORT}"fi+ if component_selected desktop; then+ entrypoint="${entrypoint}+ Desktop signed in as ${DEV_EMAIL}, workspace /${WORKSPACE_SLUG}/issues"+ [ -z "${DESKTOP_CDP_PORT:-}" ] || entrypoint="${entrypoint}+ CDP http://127.0.0.1:${DESKTOP_CDP_PORT} (chromium.connectOverCDP)"+ ficat <<EOF${C_GREEN}✓ Environment ready.${C_OFF}@@ -1105,6 +1307,7 @@ bind_paths() {DESKTOP_APP_SUFFIX="${DESKTOP_APP_SUFFIX:-$NAME}"DESKTOP_USER_DATA_DIR="${DESKTOP_USER_DATA_DIR:-$(desktop_user_data_dir "$DESKTOP_APP_SUFFIX")}"DESKTOP_ENV_FILE="${DESKTOP_ENV_FILE:-$DIR/apps/desktop/.env.development.local}"+ DESKTOP_CDP_PORT="${DESKTOP_CDP_PORT:-}"EXPIRES_AT="${EXPIRES_AT:-}"MULTICA_BIN="$DIR/server/bin/multica"if [ ! -x "$MULTICA_BIN" ] && [ -x "$REPO_ROOT/server/bin/multica" ]; then@@ -1133,11 +1336,13 @@ save_manifest() {write_manifest_value DESKTOP_APP_SUFFIX "$DESKTOP_APP_SUFFIX"write_manifest_value DESKTOP_USER_DATA_DIR "$DESKTOP_USER_DATA_DIR"write_manifest_value DESKTOP_ENV_FILE "$DESKTOP_ENV_FILE"+ write_manifest_value DESKTOP_CDP_PORT "${DESKTOP_CDP_PORT:-}"} > "$(manifest_of "$NAME")"}cmd_up() {local requested="$DEFAULT_COMPONENTS" name="" owner=human ttl=0 lifecycle_requested=0 comp+ local cdp_request=""while [ $# -gt 0 ]; docase "$1" in@@ -1146,16 +1351,31 @@ cmd_up() {--name) name="$2"; shift 2 ;;--ephemeral) owner=agent; lifecycle_requested=1; [ "$ttl" != 0 ] || ttl=24; shift ;;--ttl) ttl="$2"; owner=agent; lifecycle_requested=1; shift 2 ;;+ --cdp) cdp_request=auto; shift ;;+ --remote-debugging-port) cdp_request="${2:-}"; shift 2 ;;+ --remote-debugging-port=*) cdp_request="${1#*=}"; shift ;;+ --no-cdp) cdp_request=off; shift ;;*) die "Unknown flag for up: $1" ;;esacdone[ -z "$name" ] || require_env_name "$name"[ "$ttl" = 0 ] || require_ttl "$ttl"+ case "$cdp_request" in+ ""|auto|off) ;;+ *) valid_port "$cdp_request" || die "--remote-debugging-port needs a port number between 1024 and 65535." ;;+ esacfor comp in $requested; docase " $ALL_COMPONENTS " in *" $comp "*) ;; *) die "Unknown component '$comp'. Valid: $ALL_COMPONENTS" ;; esacdone+ # Changing the port without the desktop component would rewrite the record+ # while a running Desktop keeps serving the old port.+ case " $requested " in+ *" desktop "*) ;;+ *) [ -z "$cdp_request" ] \+ || die "--cdp, --remote-debugging-port and --no-cdp need the desktop component (C=desktop)." ;;+ esac# web, daemon and desktop are all clients of the backend; selecting one# without api would produce an environment that cannot serve a single request.case " $requested " in *" api "*) ;; *) requested="api $requested" ;; esac@@ -1251,6 +1471,7 @@ Start the rest with 'make up C=api,web', or run 'make up C=daemon' from your ownDESKTOP_APP_SUFFIX="$NAME"DESKTOP_USER_DATA_DIR="$(desktop_user_data_dir "$DESKTOP_APP_SUFFIX")"DESKTOP_ENV_FILE="$REPO_ROOT/apps/desktop/.env.development.local"+ DESKTOP_CDP_PORT=""[ ! -f "$(manifest_of "$NAME")" ] || die "Environment '$NAME' already exists for a different directory."mkdir -p "$(env_dir "$NAME")/logs"DIR="$REPO_ROOT"@@ -1266,6 +1487,30 @@ Start the rest with 'make up C=api,web', or run 'make up C=daemon' from your ownload_manifest "$NAME"ok "Registered environment $NAME"fi+ bind_paths+ # The CDP port is recorded like any other allocation: later `make up` runs+ # and restarts keep it until --no-cdp clears it.+ local previous_cdp="$DESKTOP_CDP_PORT"+ case "$cdp_request" in+ "") ;;+ off) DESKTOP_CDP_PORT="" ;;+ auto)+ if [ -z "$DESKTOP_CDP_PORT" ]; then+ DESKTOP_CDP_PORT="$(allocate_cdp_port "$NAME" $((CDP_PORT_BASE + OFFSET)))" \+ || die "No free port for the Desktop CDP endpoint; pass --remote-debugging-port N."+ fi+ ;;+ *)+ ! cdp_port_registered "$cdp_request" "$NAME" \+ || die "Port $cdp_request is already the CDP port of another environment."+ DESKTOP_CDP_PORT="$cdp_request"+ ;;+ esac+ DESKTOP_CDP_CHANGED=0+ if [ "$DESKTOP_CDP_PORT" != "$previous_cdp" ]; then+ DESKTOP_CDP_CHANGED=1+ save_manifest+ firelease_locktrap - EXIT@@ -1284,6 +1529,8 @@ Start the rest with 'make up C=api,web', or run 'make up C=daemon' from your ownmigrate_databaseok "$DB_NAME reachable through DATABASE_URL and migrated"+ component_selected desktop || sync_desktop_env_file+step "Components: $COMPONENTS"component_selected api && start_apicomponent_selected web && start_web@@ -1316,7 +1563,7 @@ cmd_down() {cmd_destroy() {local name="" assume_yes=0 reply admin_url failures=0- local expected_workspaces expected_desktop_data+ local expected_workspaces expected_desktop_data desktop_profile_dirwhile [ $# -gt 0 ]; docase "$1" in--yes|-y) assume_yes=1; shift ;;@@ -1334,9 +1581,12 @@ cmd_destroy() {export PORT="$BACKEND_PORT" FRONTEND_PORT DATABASE_URL POSTGRES_DB="$DB_NAME"step "Destroying $NAME"- local comp+ local comp desktop_stopped=1for comp in $ALL_COMPONENTS; do- if ! stop_component "$comp"; then failures=$((failures + 1)); fi+ if ! stop_component "$comp"; then+ failures=$((failures + 1))+ [ "$comp" != desktop ] || desktop_stopped=0+ fidoneif command -v psql >/dev/null 2>&1; then@@ -1386,6 +1636,21 @@ cmd_destroy() {failures=$((failures + 1))fi+ # 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+ [ ! -d "$desktop_profile_dir" ] \+ || warn "kept Desktop's CLI profile $(desktop_daemon_profile) for the retry"+ elif [ -d "$desktop_profile_dir" ]; then+ if rm -rf "$desktop_profile_dir"; then+ ok "removed Desktop's CLI profile $(desktop_daemon_profile)"+ else+ warn "failed to remove Desktop's CLI profile $desktop_profile_dir"+ failures=$((failures + 1))+ fi+ fi+if [ -f "$DESKTOP_ENV_FILE" ] \&& grep -Fqx "# Managed by scripts/dev-env.sh for environment ${NAME}." "$DESKTOP_ENV_FILE"; thenif rm -f "$DESKTOP_ENV_FILE"; then@@ -1526,6 +1791,7 @@ Local development environments: named, listable, deletable.dev-env.sh up [--components api,web,daemon,desktop] [--all][--name N] [--ephemeral] [--ttl HOURS]+ [--cdp | --remote-debugging-port N | --no-cdp]dev-env.sh status [name] [--json]dev-env.sh list [--json]dev-env.sh down [name] [--components ...]@@ -1536,6 +1802,10 @@ Local development environments: named, listable, deletable.Components: api (Go backend), web (Next.js), daemon (agent daemon),desktop (Electron). Anything selected implies api.+desktop starts signed in to an onboarded workspace. --cdp exposes a Chrome+DevTools Protocol port (printed by up and status --json) for Playwright's+connectOverCDP; the port is kept for later runs until --no-cdp.+down keeps the database, the CLI profile and the allocated slot.destroy consumes them.EOFdiff --git a/scripts/dev-env.test.sh b/scripts/dev-env.test.shindex f5ed3fbf3e..f8a5ba96c1 100644--- a/scripts/dev-env.test.sh+++ b/scripts/dev-env.test.sh@@ -319,6 +319,8 @@ node -e '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);+ if (payload.desktop_cdp_port !== null) throw new Error("desktop_cdp_port = " + payload.desktop_cdp_port);+ if (payload.workspace_slug !== "dev") throw new Error("workspace_slug = " + payload.workspace_slug);for (const key of ["api", "web", "daemon", "desktop"]) {if (!payload.components[key]) throw new Error("missing component " + key);if (payload.components[key].state !== "stopped") {@@ -397,6 +399,123 @@ 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" ""+# ---------------------------------------------------------------------------+# Desktop: CDP port, env file, process tree, and the daemon Desktop starts.+# ---------------------------------------------------------------------------++# A recorded CDP port is reported for Playwright, and the next allocation skips+# a port another environment already holds.+write_manifest "cdp-905" "$tmp_dir/checkout" 905+printf 'DESKTOP_CDP_PORT=19999\n' >> "$MULTICA_DEV_HOME/envs/cdp-905/manifest.env"+dev_env status cdp-905 --json > "$out" 2>&1 || fail "status --json with a CDP port must succeed"+node -e '+ const payload = JSON.parse(require("fs").readFileSync(process.argv[1], "utf8"));+ if (payload.desktop_cdp_port !== 19999) throw new Error("desktop_cdp_port = " + payload.desktop_cdp_port);+' "$out" || fail "status --json did not report the recorded CDP port"+allocated="$(bash -c 'source "$1"; port_free() { :; }; allocate_cdp_port self 19999' _ "$root_dir/scripts/dev-env.sh")"+[ "$allocated" = 20000 ] || fail "CDP allocation reused another environment's port: $allocated"+dev_env destroy cdp-905 --yes > "$out" 2>&1 || fail "destroying the CDP fixture failed"++status=0+dev_env up --components api --cdp > "$out" 2>&1 || status=$?+[ "$status" -ne 0 ] || fail "up accepted --cdp without the desktop component"+require_contains "$out" "need the desktop component"++# Clearing the port without the desktop component would leave a running+# Desktop serving it while status reports null.+status=0+dev_env up --components api --no-cdp > "$out" 2>&1 || status=$?+[ "$status" -ne 0 ] || fail "up accepted --no-cdp without the desktop component"+require_contains "$out" "need the desktop component"++status=0+dev_env up --components desktop --remote-debugging-port 80 > "$out" 2>&1 || status=$?+[ "$status" -ne 0 ] || fail "up accepted a privileged CDP port"+require_contains "$out" "between 1024 and 65535"++# `up` without the desktop component still points a missing or managed env+# file at this backend, and never rewrites a file a human wrote.+assert_desktop_env_sync() (+ local case_name=$1 initial=$2 expected=$3+ # shellcheck disable=SC1090+ source "$root_dir/scripts/dev-env.sh"+ NAME=sync-env BACKEND_PORT=18555+ DESKTOP_ENV_FILE="$tmp_dir/desktop-env-$case_name"+ rm -f "$DESKTOP_ENV_FILE"+ [ -z "$initial" ] || printf '%s\n' "$initial" > "$DESKTOP_ENV_FILE"+ sync_desktop_env_file > "$out" 2>&1+ grep -Fqx "$expected" "$DESKTOP_ENV_FILE" \+ || fail "$case_name env file = $(cat "$DESKTOP_ENV_FILE"), want a line: $expected"+)+assert_desktop_env_sync missing "" "VITE_API_URL=http://localhost:18555"+assert_desktop_env_sync managed-elsewhere "# Managed by scripts/dev-env.sh for environment old-env." \+ "VITE_API_URL=http://localhost:18555"+assert_desktop_env_sync hand-written "VITE_API_URL=https://staging.example" "VITE_API_URL=https://staging.example"++# Turbo puts Electron in a nested process group; the stop path finds it through+# the launcher's PPID chain instead.+bash -c 'sleep 30 & bash -c "sleep 30; :" & wait' 2>/dev/null &+tree_root=$!+sleep 0.5+tree="$(bash -c 'source "$1"; descendant_pids "$2"' _ "$root_dir/scripts/dev-env.sh" "$tree_root" | sort)"+for child in $tree; do kill "$child" 2>/dev/null || true; done+wait "$tree_root" 2>/dev/null || true+[ "$(printf '%s\n' "$tree" | grep -c .)" -ge 3 ] \+ || fail "descendant_pids found $(printf '%s' "$tree" | tr '\n' ' ') under $tree_root, want both sleeps and the nested shell"++# Desktop's own daemon outlives Electron. Stopping Desktop stops it, and only a+# confirmed stop lets destroy remove the profile Desktop derived from this+# backend's URL; that profile holds the pid a retried destroy needs.+desktop_profile_dir="$MULTICA_DEV_PROFILES_HOME/desktop-localhost-18080"+mkdir -p "$desktop_profile_dir"+desktop_daemon="$(sleep 60 > /dev/null 2>&1 & printf '%s' "$!")"+printf '%s\n' "$desktop_daemon" > "$desktop_profile_dir/daemon.pid"+fake_checkout="$tmp_dir/fake-checkout"+stop_mode="$tmp_dir/daemon-stop-mode"+mkdir -p "$fake_checkout/server/bin"+# The fake CLI stops the daemon only in "kill" mode. "lies" exits 0 with the+# daemon still alive, like a plain `daemon stop` that is still finishing a run.+cat > "$fake_checkout/server/bin/multica" <<EOF+#!/usr/bin/env bash+[ "\$*" = "daemon stop --force --profile desktop-localhost-18080" ] || exit 0+case "\$(cat "$stop_mode")" in+ kill)+ kill -KILL $desktop_daemon+ while kill -0 $desktop_daemon 2>/dev/null; do sleep 0.1; done+ ;;+ lies) ;;+ *) exit 1 ;;+esac+EOF+chmod +x "$fake_checkout/server/bin/multica"++printf 'lies' > "$stop_mode"+status=0+(+ # shellcheck disable=SC1090+ source "$root_dir/scripts/dev-env.sh"+ 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"+kill -0 "$desktop_daemon" 2>/dev/null || fail "the fake daemon died before the retry test"++printf 'fails' > "$stop_mode"+write_manifest "desktop-profile-906" "$fake_checkout" 0+status=0+dev_env destroy desktop-profile-906 --yes > "$out" 2>&1 || status=$?+[ "$status" -ne 0 ] || fail "destroy succeeded while Desktop's daemon was still running"+[ -f "$desktop_profile_dir/daemon.pid" ] || fail "a failed destroy removed Desktop's daemon pid"+[ -f "$MULTICA_DEV_HOME/envs/desktop-profile-906/manifest.env" ] \+ || fail "a failed destroy discarded the manifest"+require_contains "$out" "kept Desktop's CLI profile desktop-localhost-18080"++printf 'kill' > "$stop_mode"+dev_env destroy desktop-profile-906 --yes > "$out" 2>&1 \+ || { sed 's/^/ /' "$out" >&2; fail "retrying destroy after the daemon could be stopped failed"; }+! 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"+# ---------------------------------------------------------------------------# Unknown names and components fail loudly instead of doing something else.# ---------------------------------------------------------------------------diff --git a/turbo.json b/turbo.jsonindex 2561d41774..65ae6f7036 100644--- a/turbo.json+++ b/turbo.json@@ -46,7 +46,10 @@},"dev": {"cache": false,- "persistent": true+ "persistent": true,+ // scripts/dev-env.sh opens a Desktop DevTools port (read by+ // electron-vite) and signs the dev Desktop in (read by its main process).+ "passThroughEnv": ["REMOTE_DEBUGGING_PORT", "MULTICA_DEV_AUTH_TOKEN"]},"dev:staging": {"cache": false,