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
23 changes: 22 additions & 1 deletion .antigravity-plugin/scripts/start-monk-agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ function Test-SameFilePath {
}
}

function Get-RecordedAgentPath {
param([string]$FallbackPath)
if (-not (Test-Path $StateFile)) {
return $FallbackPath
}
$State = Get-Content -Raw $StateFile -ErrorAction SilentlyContinue
foreach ($Line in ($State -split "`r?`n")) {
if ($Line.StartsWith("agent_path=", [StringComparison]::Ordinal)) {
$RecordedPath = $Line.Substring("agent_path=".Length)
if ($RecordedPath) {
return $RecordedPath
}
}
}
return $FallbackPath
}

function Stop-ManagedAgent {
if (-not (Test-Path $PidFile)) {
return
Expand Down Expand Up @@ -313,7 +330,11 @@ function Stop-ManagedAgent {
$ProcessPath = ""
}

if (Test-SameFilePath $ProcessPath $AgentPath) {
# The recorded PID belongs to the executable from the previous launch. When
# MONK_AGENT_PATH changes from A to B, validate ownership against A so it can
# be stopped before B is started.
$ExpectedProcessPath = Get-RecordedAgentPath $AgentPath
if (Test-SameFilePath $ProcessPath $ExpectedProcessPath) {
Stop-Process -Id $OldProcess.Id -Force -ErrorAction SilentlyContinue
try {
Wait-Process -Id $OldProcess.Id -Timeout 10 -ErrorAction SilentlyContinue
Expand Down
10 changes: 9 additions & 1 deletion .antigravity-plugin/scripts/start-monk-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,15 @@ EOF
start_with_background_process() {
if [ -f "$pid_file" ]; then
old_pid="$(cat "$pid_file" 2>/dev/null || true)"
if pid_matches_executable "$old_pid" "$agent_path" && kill -0 "$old_pid" >/dev/null 2>&1; then
# The PID belongs to the executable recorded by the previous launch, not
# necessarily the newly selected MONK_AGENT_PATH. Authenticate ownership
# against that prior path so an A -> B switch can stop A before starting B.
old_agent_path="$agent_path"
if [ -f "$state_file" ]; then
recorded_agent_path="$(sed -n 's/^agent_path=//p' "$state_file" 2>/dev/null | head -n 1)"
[ -n "$recorded_agent_path" ] && old_agent_path="$recorded_agent_path"
fi
if pid_matches_executable "$old_pid" "$old_agent_path" && kill -0 "$old_pid" >/dev/null 2>&1; then
if kill "$old_pid" >/dev/null 2>&1; then
stop_tries=0
while kill -0 "$old_pid" >/dev/null 2>&1 && [ "$stop_tries" -lt 10 ]; do
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/install-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ jobs:
shell: bash
run: ./tests/start-monk-agent-fastpath.sh

- name: Check custom agent path handoff
shell: bash
run: ./tests/start-monk-agent-path-handoff.sh

- name: Check launcher readiness deadline
shell: bash
run: ./tests/start-monk-agent-readiness-timeout.sh
Expand All @@ -82,6 +86,10 @@ jobs:
shell: pwsh
run: .\tests\start-monk-agent-readiness-timeout.ps1

- name: Check custom agent path handoff
shell: pwsh
run: .\tests\start-monk-agent-path-handoff.ps1

- name: Install monk-agent
shell: pwsh
env:
Expand Down
23 changes: 22 additions & 1 deletion plugins/monk/scripts/start-monk-agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ function Test-SameFilePath {
}
}

function Get-RecordedAgentPath {
param([string]$FallbackPath)
if (-not (Test-Path $StateFile)) {
return $FallbackPath
}
$State = Get-Content -Raw $StateFile -ErrorAction SilentlyContinue
foreach ($Line in ($State -split "`r?`n")) {
if ($Line.StartsWith("agent_path=", [StringComparison]::Ordinal)) {
$RecordedPath = $Line.Substring("agent_path=".Length)
if ($RecordedPath) {
return $RecordedPath
}
}
}
return $FallbackPath
}

function Stop-ManagedAgent {
if (-not (Test-Path $PidFile)) {
return
Expand Down Expand Up @@ -313,7 +330,11 @@ function Stop-ManagedAgent {
$ProcessPath = ""
}

if (Test-SameFilePath $ProcessPath $AgentPath) {
# The recorded PID belongs to the executable from the previous launch. When
# MONK_AGENT_PATH changes from A to B, validate ownership against A so it can
# be stopped before B is started.
$ExpectedProcessPath = Get-RecordedAgentPath $AgentPath
if (Test-SameFilePath $ProcessPath $ExpectedProcessPath) {
Stop-Process -Id $OldProcess.Id -Force -ErrorAction SilentlyContinue
try {
Wait-Process -Id $OldProcess.Id -Timeout 10 -ErrorAction SilentlyContinue
Expand Down
10 changes: 9 additions & 1 deletion plugins/monk/scripts/start-monk-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,15 @@ EOF
start_with_background_process() {
if [ -f "$pid_file" ]; then
old_pid="$(cat "$pid_file" 2>/dev/null || true)"
if pid_matches_executable "$old_pid" "$agent_path" && kill -0 "$old_pid" >/dev/null 2>&1; then
# The PID belongs to the executable recorded by the previous launch, not
# necessarily the newly selected MONK_AGENT_PATH. Authenticate ownership
# against that prior path so an A -> B switch can stop A before starting B.
old_agent_path="$agent_path"
if [ -f "$state_file" ]; then
recorded_agent_path="$(sed -n 's/^agent_path=//p' "$state_file" 2>/dev/null | head -n 1)"
[ -n "$recorded_agent_path" ] && old_agent_path="$recorded_agent_path"
fi
if pid_matches_executable "$old_pid" "$old_agent_path" && kill -0 "$old_pid" >/dev/null 2>&1; then
if kill "$old_pid" >/dev/null 2>&1; then
stop_tries=0
while kill -0 "$old_pid" >/dev/null 2>&1 && [ "$stop_tries" -lt 10 ]; do
Expand Down
23 changes: 22 additions & 1 deletion scripts/start-monk-agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ function Test-SameFilePath {
}
}

function Get-RecordedAgentPath {
param([string]$FallbackPath)
if (-not (Test-Path $StateFile)) {
return $FallbackPath
}
$State = Get-Content -Raw $StateFile -ErrorAction SilentlyContinue
foreach ($Line in ($State -split "`r?`n")) {
if ($Line.StartsWith("agent_path=", [StringComparison]::Ordinal)) {
$RecordedPath = $Line.Substring("agent_path=".Length)
if ($RecordedPath) {
return $RecordedPath
}
}
}
return $FallbackPath
}

function Stop-ManagedAgent {
if (-not (Test-Path $PidFile)) {
return
Expand Down Expand Up @@ -313,7 +330,11 @@ function Stop-ManagedAgent {
$ProcessPath = ""
}

if (Test-SameFilePath $ProcessPath $AgentPath) {
# The recorded PID belongs to the executable from the previous launch. When
# MONK_AGENT_PATH changes from A to B, validate ownership against A so it can
# be stopped before B is started.
$ExpectedProcessPath = Get-RecordedAgentPath $AgentPath
if (Test-SameFilePath $ProcessPath $ExpectedProcessPath) {
Stop-Process -Id $OldProcess.Id -Force -ErrorAction SilentlyContinue
try {
Wait-Process -Id $OldProcess.Id -Timeout 10 -ErrorAction SilentlyContinue
Expand Down
10 changes: 9 additions & 1 deletion scripts/start-monk-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,15 @@ EOF
start_with_background_process() {
if [ -f "$pid_file" ]; then
old_pid="$(cat "$pid_file" 2>/dev/null || true)"
if pid_matches_executable "$old_pid" "$agent_path" && kill -0 "$old_pid" >/dev/null 2>&1; then
# The PID belongs to the executable recorded by the previous launch, not
# necessarily the newly selected MONK_AGENT_PATH. Authenticate ownership
# against that prior path so an A -> B switch can stop A before starting B.
old_agent_path="$agent_path"
if [ -f "$state_file" ]; then
recorded_agent_path="$(sed -n 's/^agent_path=//p' "$state_file" 2>/dev/null | head -n 1)"
[ -n "$recorded_agent_path" ] && old_agent_path="$recorded_agent_path"
fi
if pid_matches_executable "$old_pid" "$old_agent_path" && kill -0 "$old_pid" >/dev/null 2>&1; then
if kill "$old_pid" >/dev/null 2>&1; then
stop_tries=0
while kill -0 "$old_pid" >/dev/null 2>&1 && [ "$stop_tries" -lt 10 ]; do
Expand Down
Loading