Let agent_stop close visible terminal windows it spawned#27
Merged
Conversation
Visible agent terminals were launched detached with their PID discarded, so nothing held a handle to the window and agent_stop only ever killed headless processes. Capture the emulator PID through openVisibleTerminal, store it on the visible agent record, and route stopLocalAgent through a process-group kill (SIGTERM to -pid) that tears down the window and the agent CLI inside it on Linux/BSD. macOS (Terminal.app via osascript) and Windows (start) don't yield a killable window PID, so launch.pid is undefined there and close degrades gracefully without offering the option. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The emulator launcher PID is useless for closing the window: server-style terminals (gnome-terminal, konsole, kitty) hand work to a daemon and exit, so child.pid dies immediately and signalling it does nothing. Instead, wrap the visible command in `sh -c 'printf $$ > pidfile; exec "$@"'` so the PID written is the agent CLI itself (exec preserves the PID). The record tracks the pid file; closeVisibleTerminal resolves the PID from it, SIGTERMs the process group (falling back to the bare PID), waits for exit, then marks the record reusable. stopLocalAgent is now async. Also clean up the tmp pid file on respawn and after a successful read so it doesn't linger in tmpdir. macOS/Windows still yield no PID and degrade gracefully. Adds tests for tracked-pid close, untracked-stays-blocked, and the Linux pid-file round-trip through a fake terminal. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When Athena Code spawns an agent in a visible terminal (
agent_spawn visible=true, oragent_takeover where="terminal"), it cannot close that window again. The emulator was launched detached and no part of the system held a handle to the agent running inside it —agent_stoponly ever killedrecord.process, which visible agents don't have.Fix
The emulator launcher PID is no good for this: server-style terminals (gnome-terminal, konsole, kitty) hand the work to a daemon and the launcher exits immediately, so its PID dies and signalling it does nothing. Instead we track the PID of the command running inside the window:
terminal.ts— on Linux the visible command is wrapped insh -c 'printf "$$" > pidfile; exec "$@"'. Becauseexecpreserves the PID, the value written to a tmp pid file is the agent CLI itself.openVisibleTerminalreturns thatpidFile(and apiddirectly when one is already known).local.ts— the record storesterminalPid/terminalPidFile. NewcloseVisibleTerminalresolves the PID (reading the pid file, polling briefly if it hasn't been written yet), sendsSIGTERMto the process group (process.kill(-pid), falling back to the bare PID), waits for the process to actually exit, then marks the record exited and reusable.stopLocalAgentroutes visible agents through it and is nowasync. The tmp pid file is unlinked after a successful read and on respawn so it doesn't linger in tmpdir.agent-local.ts—launch.pid/launch.pidFileare threaded intoregisterVisibleAgentand the takeover path; result messages tell the user they canagent_stop <handle>to close the window. Theagent_stoptool description now covers closing windows.Caveat
macOS (
Terminal.appviaosascript) and Windows (start) don't yield a killable PID, so bothpidandpidFileareundefinedthere and close degrades gracefully —agent_stopreturns false, the record stays visible, and the "close the window" hint is omitted. Linux is fully functional; macOS would need to track thedo scripttab reference and close it via a second AppleScript, left as a follow-up.Note that killing the in-window command only closes the window when the emulator is configured to close on child exit (the default for the common emulators).
Tests
test/agent-local.test.tsadds coverage for: a tracked-PID visible agent being closed (process group torn down, record marked exited), an untracked visible agent staying blocked, and the Linux pid-file round-trip through a fake terminal on PATH.🤖 Generated with Claude Code