diff --git a/packages/agentenv-protocol/src/agentenv_protocol/computer/_session_windows.py b/packages/agentenv-protocol/src/agentenv_protocol/computer/_session_windows.py index f3c786a6c..13a216131 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/computer/_session_windows.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/computer/_session_windows.py @@ -72,8 +72,9 @@ async def launch(argv: list[str], cwd: Path, env: dict[str, str], fds: Optional[ async def stop(pid: int, timeout_s: float) -> None: """End the job the helper ``pid`` holds, every process in it: its windows asked to close, then the job ended after - ``timeout_s``, or at once when it has no window to close, and waited on until each process it held has exited and - it holds none. No one asks how it exited once it's stopped.""" + ``timeout_s``, or at once when it has no window to close; once the app's processes have closed, the helper, which + leaves when its job holds no process but itself, waited on to leave, the job ended should it not; and waited on + until each process it held has exited and it holds none. No one asks how it exited once it's stopped.""" _reaping.forget(pid) job = _win32.open_job(pid) if job is None: @@ -85,7 +86,11 @@ async def stop(pid: int, timeout_s: float) -> None: deadline = time.monotonic() + (timeout_s if windows else 0) while _app_pids(job, pid) and time.monotonic() < deadline: await asyncio.sleep(POLL_S) - if _app_pids(job, pid): + if not _app_pids(job, pid): + deadline = time.monotonic() + ENDED_TIMEOUT_S + while _win32.job_pids(job) and time.monotonic() < deadline: + await asyncio.sleep(POLL_S) + if _win32.job_pids(job): ending = _win32.open_processes(_win32.job_pids(job)) try: _win32.terminate_job(job) diff --git a/packages/agentenv-protocol/tests/test_computer_session_windows.py b/packages/agentenv-protocol/tests/test_computer_session_windows.py index 56e8a8ebb..81fcc730d 100644 --- a/packages/agentenv-protocol/tests/test_computer_session_windows.py +++ b/packages/agentenv-protocol/tests/test_computer_session_windows.py @@ -1,7 +1,8 @@ """The Windows session's own logic, its Win32 calls faked and its job helper stood in for: a launch goes through the helper and returns its pid once the app has started, or fails saying why it couldn't, and hands no fds; stopping asks -the app's windows to close, then ends the job after the timeout, or at once with no window to ask, and waits until -each process it held has exited and it holds none; pausing suspends each of the app's processes but the helper's, and +the app's windows to close, then ends the job after the timeout, or at once with no window to ask, and, once they've +closed, waits for the helper to leave, ending the job should it not, until each process it held has exited and it +holds none; pausing suspends each of the app's processes but the helper's, and any one starts as they're suspended, and resumes them, even when the block raises; a job that's gone is neither alive, stopped nor paused; the windows listed are those shown, by program, class and title; and the menu launcher is a Start Menu shortcut PowerShell writes, named for the app.""" @@ -32,12 +33,14 @@ class FakeWin32: """Jobs by their helper's pid, each a list of the pids in it, the helper's first; the session's windows; and every call that acts, recorded. An app whose window is asked to close ``closes`` or doesn't, a process ``spawns`` one - into its job just before it's suspended, and a job ended by force holds its processes for ``lingers`` more looks, - then none, though it's there until its handles close.""" + into its job just before it's suspended, a job ended by force holds its processes for ``lingers`` more looks, then + none, though it's there until its handles close, and a helper alone in its job ``leaves`` after that many looks, as + the real one does, or, for None, stays.""" def __init__(self, jobs: dict[int, list[int]], windows: list[Window], closes: bool = True, - spawns: dict[int, int] | None = None, lingers: int = 0) -> None: + spawns: dict[int, int] | None = None, lingers: int = 0, leaves: int | None = 5) -> None: self.jobs, self._windows, self.closes, self.spawns, self.lingers = jobs, windows, closes, spawns or {}, lingers + self.leaves = leaves self.ending: dict[int, int] = {} self.posted, self.terminated, self.suspended, self.resumed, self.waited, self.open = [], [], [], [], [], 0 @@ -52,6 +55,10 @@ def job_pids(self, job): self.ending[job] -= 1 if self.ending[job] < 0: self.jobs[job] = [] + elif self.jobs[job] == [job] and self.leaves is not None: + self.leaves -= 1 + if self.leaves < 0: + self.jobs[job] = [] return list(self.jobs[job]) def open_processes(self, pids): @@ -107,16 +114,29 @@ def install(*args, **kwargs) -> FakeWin32: async def test_stopping_asks_the_apps_windows_to_close_and_ends_the_job_only_if_they_dont(win32): obliging = win32({HELPER: [HELPER, 901, 902]}, [_window(1, 901), _window(2, 777), _window(3, 902, shown=False)]) await _session_windows.stop(HELPER, timeout_s=5) + obliging_left = not _session_windows.alive(HELPER) stubborn = win32({HELPER: [HELPER, 901]}, [_window(1, 901)], closes=False, lingers=3) started = time.monotonic() await _session_windows.stop(HELPER, timeout_s=0.2) assert obliging.posted == [1, 3] and obliging.terminated == obliging.waited == [] and obliging.open == 0 + assert obliging_left assert stubborn.posted == [1] and stubborn.terminated == [HELPER] and time.monotonic() - started >= 0.2 assert stubborn.waited == [[HELPER, 901]] and stubborn.open == 0 assert stubborn.jobs[HELPER] == [] and not _session_windows.alive(HELPER) +@pytest.mark.asyncio +async def test_a_helper_that_stays_once_its_apps_window_has_closed_is_ended_with_its_job(win32, monkeypatch): + monkeypatch.setattr(_session_windows, "ENDED_TIMEOUT_S", 0.1) + stuck = win32({HELPER: [HELPER, 901]}, [_window(1, 901)], leaves=None) + + await _session_windows.stop(HELPER, timeout_s=5) + + assert stuck.posted == [1] and stuck.terminated == [HELPER] and stuck.waited == [[HELPER]] and stuck.open == 0 + assert not _session_windows.alive(HELPER) + + @pytest.mark.asyncio async def test_an_app_with_no_window_to_ask_is_ended_at_once_and_a_gone_job_is_left_be(win32): windowless = win32({HELPER: [HELPER, 901]}, [_window(1, 777)])