Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .antigravity-plugin/scripts/start-monk-agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion .antigravity-plugin/scripts/start-monk-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,22 @@ 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)"
printf '%s\n' "$state" | grep -Fxq "agent_path=$agent_path" &&
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
Expand Down Expand Up @@ -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"
}

Expand Down
12 changes: 10 additions & 2 deletions plugins/monk/scripts/start-monk-agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion plugins/monk/scripts/start-monk-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,22 @@ 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)"
printf '%s\n' "$state" | grep -Fxq "agent_path=$agent_path" &&
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
Expand Down Expand Up @@ -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"
}

Expand Down
12 changes: 10 additions & 2 deletions scripts/start-monk-agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion scripts/start-monk-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,22 @@ 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)"
printf '%s\n' "$state" | grep -Fxq "agent_path=$agent_path" &&
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
Expand Down Expand Up @@ -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"
}

Expand Down
42 changes: 42 additions & 0 deletions tests/start-monk-agent-fastpath.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down Expand Up @@ -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."