Skip to content

fix(antigravity): kill stale monk-agent PID in ensure hooks (#394) - #492

Open
ZacLou wants to merge 1 commit into
monk-io:mainfrom
ZacLou:fix-antigravity-ensure-kill-stale-pid-394
Open

ZacLou wants to merge 1 commit into
monk-io:mainfrom
ZacLou:fix-antigravity-ensure-kill-stale-pid-394

Conversation

@ZacLou

@ZacLou ZacLou commented Sep 3, 2026

Copy link
Copy Markdown

Closes #394

The PreInvocation ensure hooks (.antigravity-plugin/hooks/ensure-monk-agent.ps1 and .sh) now kill a stale monk-agent process recorded in their PID file before spawning a replacement, matching the cleanup already performed by scripts/start-monk-agent.{ps1,sh}.

  • PowerShell: adds Stop-StaleAgent and calls it after the PID file path is resolved.
  • POSIX shell: adds stop_stale_agent and calls it before starting the binary.
  • Adds regression tests for both paths.

@ZacLou ZacLou changed the title fix(antigravity): kill stale monk-agent PID in ensure hooks (#394) fix(antigravity): kill stale monk-agent PID in ensure hooks (#394) [updated] Sep 3, 2026
@ZacLou ZacLou changed the title fix(antigravity): kill stale monk-agent PID in ensure hooks (#394) [updated] fix(antigravity): kill stale monk-agent PID in ensure hooks (#394) Sep 3, 2026
@NyxSpecter4

Copy link
Copy Markdown

Reporter of #394 here — verified: the ensure hooks now mirror Stop-ManagedAgent (table on #394). The PID-reuse guard (/proc/<pid>/exe on Linux, ps -o comm= elsewhere, $OldProcess.Path on Windows) is the right shape; thanks for not killing a bare PID.

Two notes:

  1. tests/ensure-monk-agent-kill-stale-pid.sh on Linux (Ubuntu, sh = dash): the stale-kill assertion holds, but the final check fails with replacement monk-agent process (<pid>) is not running. Cause is in the harness, not the fix: the fake monk-agent is a copy of sh, so monk-agent serve --host … tries to open a file named serve in the hook's cwd, not the serve stub on PATH. A one-line wrapper script that exec sleeps regardless of args fixes the test.
  2. Stop-StaleAgent elseif (Test-Path $ExpectedPath) { $KillIt = $true } — when the OS gives no Path (access denied / 32-vs-64-bit), this kills the PID as long as the expected binary exists on disk, which is almost always. Consider $OldProcess.ProcessName -like 'monk-agent*' as the fallback instead, matching the .sh side's comm= check.

Branch is based on v0.1.59; main is v0.1.60 — rebase to keep the diff to 4 files.

@ZacLou

ZacLou commented Sep 4, 2026

Copy link
Copy Markdown
Author

@NyxSpecter4 thanks for the review. Pushed both fixes:\n\n1. The shell test now runs the hook from $fake_bin so the fake binary (copy of sh) finds the serve stub in its cwd and the replacement process stays alive.\n2. Stop-StaleAgent now falls back to $OldProcess.ProcessName -like 'monk-agent*' instead of Test-Path $ExpectedPath, matching the POSIX side's comm= check and avoiding killing unrelated reused PIDs.\n\nPlease take another look.

@ZacLou
ZacLou force-pushed the fix-antigravity-ensure-kill-stale-pid-394 branch from 6670848 to 7cd3668 Compare September 4, 2026 05:00

@NyxSpecter4 NyxSpecter4 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the reporter of #394, I took another look at the updated head (7cd3668):

  1. Shell regression test — ran tests/ensure-monk-agent-kill-stale-pid.sh on Ubuntu (sh = dash): passed. Stale process killed, replacement recorded in the PID file and running. The fake_bin cwd fix resolves the harness issue I flagged.
  2. PowerShell — code review only, since I have no Windows runner here: Stop-StaleAgent now falls back to $OldProcess.ProcessName -like 'monk-agent*' when the OS provides no process path, matching the POSIX side's comm= guard. A bare PID is never killed. I could not execute the .ps1 test in this environment, so that one remains unverified by me.
  3. Rebase — branch is based on v0.1.60 (8d8efce); the diff is exactly the four hook/test files (356 insertions, no unrelated changes).

Both notes from my earlier review are addressed, and the PID-reuse guard is preserved on both platforms. Approving from the reporter's side — merging is of course up to the maintainers. Thanks for the quick turnaround on both points.

@NyxSpecter4

Copy link
Copy Markdown

Both fixes verified: Stop-StaleAgent now falls back to ProcessName -like 'monk-agent*' only when no path is available (a reused PID is never killed unless the name matches — consistent with the POSIX comm= guard), and the $fake_bin cwd fix resolves the harness failure.

Ran tests/ensure-monk-agent-kill-stale-pid.sh on Linux/dash: exit 0, "stale PID kill test passed". LGTM — good to merge.

@NyxSpecter4 NyxSpecter4 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving: both requested fixes verified in code; tests/ensure-monk-agent-kill-stale-pid.sh run on Linux/dash -> exit 0, "stale PID kill test passed". Note: .ps1 regression statically reviewed only (no pwsh available here); Stop-StaleAgent path-match + name-fallback logic is sound.

@ZacLou
ZacLou force-pushed the fix-antigravity-ensure-kill-stale-pid-394 branch from 7cd3668 to 3b2b54f Compare September 5, 2026 07:43
@ZacLou
ZacLou force-pushed the fix-antigravity-ensure-kill-stale-pid-394 branch from 3b2b54f to 70bee9a Compare September 16, 2026 10:03
)

Adds Stop-StaleAgent to ensure-monk-agent.ps1 and stop_stale_agent to ensure-monk-agent.sh, mirroring the cleanup already done by start-monk-agent.

Includes regression tests for both POSIX and PowerShell paths.
@ZacLou
ZacLou force-pushed the fix-antigravity-ensure-kill-stale-pid-394 branch from 70bee9a to 9f0fc2f Compare September 16, 2026 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug bounty] Ensure hooks spawn monk-agent without killing stale PID when health probe fails

2 participants