diff --git a/.antigravity-plugin/scripts/start-monk-agent.ps1 b/.antigravity-plugin/scripts/start-monk-agent.ps1 index 99cd094..e68a64e 100644 --- a/.antigravity-plugin/scripts/start-monk-agent.ps1 +++ b/.antigravity-plugin/scripts/start-monk-agent.ps1 @@ -327,6 +327,10 @@ if ($ManagedAgentEnsured) { # fields that matter for reuse are persisted to $StateFile on every start and # diffed here. Covers both a stale custom MONK_AGENT_PATH and drifted # auth/autospin config on an otherwise-healthy companion (ENG-390, ENG-397). +# local/plugin_version are gated too (parity with the macOS launchd plist +# check): flipping MONK_AGENT_LOCAL or upgrading only the plugin must restart +# a "healthy" agent so it picks up the new mode and stops reporting a stale +# plugin version — previously both were silently ignored on Windows and Linux. function Test-BackgroundStateConfigured { if (-not (Test-Path $StateFile)) { return $false @@ -340,7 +344,9 @@ function Test-BackgroundStateConfigured { "auth_url=$AuthUrl", "auth_client_id=$AuthClientId", "auth_audience=$AuthAudience", - "autospin_url=$AutospinUrl" + "autospin_url=$AutospinUrl", + "local=$($env:MONK_AGENT_LOCAL)", + "plugin_version=$($env:MONK_PLUGIN_VERSION)" ) $Lines = $State -split "`r?`n" foreach ($Line in $Expected) { @@ -382,7 +388,9 @@ $Process.Id | Set-Content -NoNewline $PidFile "auth_url=$AuthUrl", "auth_client_id=$AuthClientId", "auth_audience=$AuthAudience", - "autospin_url=$AutospinUrl" + "autospin_url=$AutospinUrl", + "local=$($env:MONK_AGENT_LOCAL)", + "plugin_version=$($env:MONK_PLUGIN_VERSION)" ) -join "`n" | Set-Content -NoNewline $StateFile $ReadyTimer = [System.Diagnostics.Stopwatch]::StartNew() diff --git a/.antigravity-plugin/scripts/start-monk-agent.sh b/.antigravity-plugin/scripts/start-monk-agent.sh index 684d379..a075777 100755 --- a/.antigravity-plugin/scripts/start-monk-agent.sh +++ b/.antigravity-plugin/scripts/start-monk-agent.sh @@ -334,6 +334,12 @@ launchd_configured() { # diffed here. Covers both a stale custom MONK_AGENT_PATH (ENG-390) and # drifted auth/autospin config on an otherwise-healthy Linux/other-POSIX # companion (ENG-397 -- launchd_configured above already covers macOS). +# local/plugin_version ARE gated here (unlike PATH and launch client): they +# change only on an explicit user setting or a plugin install/upgrade, and +# both are exactly the moments the agent should restart -- mirroring the +# launchd plist checks on macOS. Before this, flipping MONK_AGENT_LOCAL or +# upgrading only the plugin left a "healthy" background agent silently +# running with the old mode and old version indefinitely. background_process_configured() { [ -f "$state_file" ] || return 1 state="$(cat "$state_file" 2>/dev/null || true)" @@ -341,7 +347,9 @@ background_process_configured() { printf '%s\n' "$state" | grep -Fxq "auth_url=$auth_url" && printf '%s\n' "$state" | grep -Fxq "auth_client_id=$auth_client_id" && printf '%s\n' "$state" | grep -Fxq "auth_audience=$auth_audience" && - printf '%s\n' "$state" | grep -Fxq "autospin_url=$autospin_url" + printf '%s\n' "$state" | grep -Fxq "autospin_url=$autospin_url" && + printf '%s\n' "$state" | grep -Fxq "local=${MONK_AGENT_LOCAL:-}" && + printf '%s\n' "$state" | grep -Fxq "plugin_version=${MONK_PLUGIN_VERSION:-}" } if [ "${MONK_AGENT_SKIP_ENSURE:-0}" != "1" ]; then @@ -460,6 +468,8 @@ start_with_background_process() { printf 'auth_client_id=%s\n' "$auth_client_id" printf 'auth_audience=%s\n' "$auth_audience" printf 'autospin_url=%s\n' "$autospin_url" + printf 'local=%s\n' "${MONK_AGENT_LOCAL:-}" + printf 'plugin_version=%s\n' "${MONK_PLUGIN_VERSION:-}" } >"$state_file" } diff --git a/plugins/monk/scripts/start-monk-agent.ps1 b/plugins/monk/scripts/start-monk-agent.ps1 index 99cd094..e68a64e 100644 --- a/plugins/monk/scripts/start-monk-agent.ps1 +++ b/plugins/monk/scripts/start-monk-agent.ps1 @@ -327,6 +327,10 @@ if ($ManagedAgentEnsured) { # fields that matter for reuse are persisted to $StateFile on every start and # diffed here. Covers both a stale custom MONK_AGENT_PATH and drifted # auth/autospin config on an otherwise-healthy companion (ENG-390, ENG-397). +# local/plugin_version are gated too (parity with the macOS launchd plist +# check): flipping MONK_AGENT_LOCAL or upgrading only the plugin must restart +# a "healthy" agent so it picks up the new mode and stops reporting a stale +# plugin version — previously both were silently ignored on Windows and Linux. function Test-BackgroundStateConfigured { if (-not (Test-Path $StateFile)) { return $false @@ -340,7 +344,9 @@ function Test-BackgroundStateConfigured { "auth_url=$AuthUrl", "auth_client_id=$AuthClientId", "auth_audience=$AuthAudience", - "autospin_url=$AutospinUrl" + "autospin_url=$AutospinUrl", + "local=$($env:MONK_AGENT_LOCAL)", + "plugin_version=$($env:MONK_PLUGIN_VERSION)" ) $Lines = $State -split "`r?`n" foreach ($Line in $Expected) { @@ -382,7 +388,9 @@ $Process.Id | Set-Content -NoNewline $PidFile "auth_url=$AuthUrl", "auth_client_id=$AuthClientId", "auth_audience=$AuthAudience", - "autospin_url=$AutospinUrl" + "autospin_url=$AutospinUrl", + "local=$($env:MONK_AGENT_LOCAL)", + "plugin_version=$($env:MONK_PLUGIN_VERSION)" ) -join "`n" | Set-Content -NoNewline $StateFile $ReadyTimer = [System.Diagnostics.Stopwatch]::StartNew() diff --git a/plugins/monk/scripts/start-monk-agent.sh b/plugins/monk/scripts/start-monk-agent.sh index 684d379..a075777 100755 --- a/plugins/monk/scripts/start-monk-agent.sh +++ b/plugins/monk/scripts/start-monk-agent.sh @@ -334,6 +334,12 @@ launchd_configured() { # diffed here. Covers both a stale custom MONK_AGENT_PATH (ENG-390) and # drifted auth/autospin config on an otherwise-healthy Linux/other-POSIX # companion (ENG-397 -- launchd_configured above already covers macOS). +# local/plugin_version ARE gated here (unlike PATH and launch client): they +# change only on an explicit user setting or a plugin install/upgrade, and +# both are exactly the moments the agent should restart -- mirroring the +# launchd plist checks on macOS. Before this, flipping MONK_AGENT_LOCAL or +# upgrading only the plugin left a "healthy" background agent silently +# running with the old mode and old version indefinitely. background_process_configured() { [ -f "$state_file" ] || return 1 state="$(cat "$state_file" 2>/dev/null || true)" @@ -341,7 +347,9 @@ background_process_configured() { printf '%s\n' "$state" | grep -Fxq "auth_url=$auth_url" && printf '%s\n' "$state" | grep -Fxq "auth_client_id=$auth_client_id" && printf '%s\n' "$state" | grep -Fxq "auth_audience=$auth_audience" && - printf '%s\n' "$state" | grep -Fxq "autospin_url=$autospin_url" + printf '%s\n' "$state" | grep -Fxq "autospin_url=$autospin_url" && + printf '%s\n' "$state" | grep -Fxq "local=${MONK_AGENT_LOCAL:-}" && + printf '%s\n' "$state" | grep -Fxq "plugin_version=${MONK_PLUGIN_VERSION:-}" } if [ "${MONK_AGENT_SKIP_ENSURE:-0}" != "1" ]; then @@ -460,6 +468,8 @@ start_with_background_process() { printf 'auth_client_id=%s\n' "$auth_client_id" printf 'auth_audience=%s\n' "$auth_audience" printf 'autospin_url=%s\n' "$autospin_url" + printf 'local=%s\n' "${MONK_AGENT_LOCAL:-}" + printf 'plugin_version=%s\n' "${MONK_PLUGIN_VERSION:-}" } >"$state_file" } diff --git a/scripts/start-monk-agent.ps1 b/scripts/start-monk-agent.ps1 index 99cd094..e68a64e 100644 --- a/scripts/start-monk-agent.ps1 +++ b/scripts/start-monk-agent.ps1 @@ -327,6 +327,10 @@ if ($ManagedAgentEnsured) { # fields that matter for reuse are persisted to $StateFile on every start and # diffed here. Covers both a stale custom MONK_AGENT_PATH and drifted # auth/autospin config on an otherwise-healthy companion (ENG-390, ENG-397). +# local/plugin_version are gated too (parity with the macOS launchd plist +# check): flipping MONK_AGENT_LOCAL or upgrading only the plugin must restart +# a "healthy" agent so it picks up the new mode and stops reporting a stale +# plugin version — previously both were silently ignored on Windows and Linux. function Test-BackgroundStateConfigured { if (-not (Test-Path $StateFile)) { return $false @@ -340,7 +344,9 @@ function Test-BackgroundStateConfigured { "auth_url=$AuthUrl", "auth_client_id=$AuthClientId", "auth_audience=$AuthAudience", - "autospin_url=$AutospinUrl" + "autospin_url=$AutospinUrl", + "local=$($env:MONK_AGENT_LOCAL)", + "plugin_version=$($env:MONK_PLUGIN_VERSION)" ) $Lines = $State -split "`r?`n" foreach ($Line in $Expected) { @@ -382,7 +388,9 @@ $Process.Id | Set-Content -NoNewline $PidFile "auth_url=$AuthUrl", "auth_client_id=$AuthClientId", "auth_audience=$AuthAudience", - "autospin_url=$AutospinUrl" + "autospin_url=$AutospinUrl", + "local=$($env:MONK_AGENT_LOCAL)", + "plugin_version=$($env:MONK_PLUGIN_VERSION)" ) -join "`n" | Set-Content -NoNewline $StateFile $ReadyTimer = [System.Diagnostics.Stopwatch]::StartNew() diff --git a/scripts/start-monk-agent.sh b/scripts/start-monk-agent.sh index 684d379..a075777 100755 --- a/scripts/start-monk-agent.sh +++ b/scripts/start-monk-agent.sh @@ -334,6 +334,12 @@ launchd_configured() { # diffed here. Covers both a stale custom MONK_AGENT_PATH (ENG-390) and # drifted auth/autospin config on an otherwise-healthy Linux/other-POSIX # companion (ENG-397 -- launchd_configured above already covers macOS). +# local/plugin_version ARE gated here (unlike PATH and launch client): they +# change only on an explicit user setting or a plugin install/upgrade, and +# both are exactly the moments the agent should restart -- mirroring the +# launchd plist checks on macOS. Before this, flipping MONK_AGENT_LOCAL or +# upgrading only the plugin left a "healthy" background agent silently +# running with the old mode and old version indefinitely. background_process_configured() { [ -f "$state_file" ] || return 1 state="$(cat "$state_file" 2>/dev/null || true)" @@ -341,7 +347,9 @@ background_process_configured() { printf '%s\n' "$state" | grep -Fxq "auth_url=$auth_url" && printf '%s\n' "$state" | grep -Fxq "auth_client_id=$auth_client_id" && printf '%s\n' "$state" | grep -Fxq "auth_audience=$auth_audience" && - printf '%s\n' "$state" | grep -Fxq "autospin_url=$autospin_url" + printf '%s\n' "$state" | grep -Fxq "autospin_url=$autospin_url" && + printf '%s\n' "$state" | grep -Fxq "local=${MONK_AGENT_LOCAL:-}" && + printf '%s\n' "$state" | grep -Fxq "plugin_version=${MONK_PLUGIN_VERSION:-}" } if [ "${MONK_AGENT_SKIP_ENSURE:-0}" != "1" ]; then @@ -460,6 +468,8 @@ start_with_background_process() { printf 'auth_client_id=%s\n' "$auth_client_id" printf 'auth_audience=%s\n' "$auth_audience" printf 'autospin_url=%s\n' "$autospin_url" + printf 'local=%s\n' "${MONK_AGENT_LOCAL:-}" + printf 'plugin_version=%s\n' "${MONK_PLUGIN_VERSION:-}" } >"$state_file" } diff --git a/tests/start-monk-agent-fastpath.sh b/tests/start-monk-agent-fastpath.sh index 4e7ab38..2dc2480 100755 --- a/tests/start-monk-agent-fastpath.sh +++ b/tests/start-monk-agent-fastpath.sh @@ -31,6 +31,8 @@ write_state() { printf 'auth_client_id=UW84YWcJME3buMSLfqLX8IbBsYdNWi47\n' printf 'auth_audience=oaknode.com\n' printf 'autospin_url=wss://api.app.monk.io/autospin/\n' + printf 'local=\n' + printf 'plugin_version=0.1.52\n' } >"$state_file" } @@ -65,4 +67,44 @@ if ! grep -Fxq "auth_url=https://auth-two.invalid" "$drift_run_dir/monk-agent.st exit 1 fi +# Case 3: MONK_AGENT_LOCAL drift -> restarted (issue #229: user-set local-mode +# switch must not be silently ignored by the Linux background fast path, +# mirroring the launchd plist gate on macOS). +write_state_with_local() { + state_file="$1" + local_value="$2" + { + printf 'agent_path=/usr/bin/true\n' + printf 'auth_url=https://auth.monk.io\n' + printf 'auth_client_id=UW84YWcJME3buMSLfqLX8IbBsYdNWi47\n' + printf 'auth_audience=oaknode.com\n' + printf 'autospin_url=wss://api.app.monk.io/autospin/\n' + printf 'local=%s\n' "$local_value" + printf 'plugin_version=0.1.52\n' + } >"$state_file" +} + +local_dir="$work_dir/local/monk" +local_run_dir="$local_dir/agent/launcher/run" +mkdir -p "$local_run_dir" +write_state_with_local "$local_run_dir/monk-agent.state" "" + +HOME="$work_dir/home" \ +PATH="$fixture_bin:/usr/bin:/bin" \ +MONK_AGENT_PATH=/usr/bin/true \ +MONK_AGENT_HOME="$local_dir" \ +MONK_AUTH_URL="https://auth.monk.io" \ +MONK_AGENT_LOCAL=1 \ +MONK_AGENT_SKIP_SIGNIN_NUDGE=1 \ + "$repo_root/scripts/start-monk-agent.sh" + +if [ ! -e "$local_run_dir/monk-agent.pid" ]; then + echo "companion was not restarted after MONK_AGENT_LOCAL flipped on" >&2 + exit 1 +fi +if ! grep -Fxq "local=1" "$local_run_dir/monk-agent.state"; then + echo "updated MONK_AGENT_LOCAL was not recorded in the state file" >&2 + exit 1 +fi + echo "start-monk-agent fast-path tests passed."