Skip to content

The agent that grows with you

fix: judge wait_on_pid race no longer raises out of evaluate_after_turn

teknium1b55767be82b4
diff --git a/hermes_cli/goals.py b/hermes_cli/goals.py
index e59bc07d2a8..f10f298cb87 100644
--- a/hermes_cli/goals.py
+++ b/hermes_cli/goals.py
@@ -1446,12 +1446,15 @@ class GoalManager:
tgt = f"session {self.wait_on_session(str(wait_directive['session_id']), reason=reason).waiting_on_session}"
elif wait_directive.get("pid"):
pid = int(wait_directive["pid"])
- if not _pid_alive(pid):
+ try:
+ tgt = f"pid {self.wait_on(pid, reason=reason).waiting_on_pid}"
+ except ValueError:
# A remote or already-exited pid is a barrier this host can never observe lifting
# (#110826): the judge sees the same pid next turn and would re-park forever.
+ # Catching wait_on's own liveness check (rather than probing first) closes the
+ # window where the pid exits between a pre-check and the park.
logger.info("goal judge: wait_on_pid %s is not alive on this host; continuing", pid)
return None
- tgt = f"pid {self.wait_on(pid, reason=reason).waiting_on_pid}"
else:
self.wait_for_seconds(int(wait_directive["seconds"]), reason=reason, on_delegations=active_delegations)
tgt = f"{wait_directive['seconds']}s"
diff --git a/tests/hermes_cli/test_goals.py b/tests/hermes_cli/test_goals.py
index 02a9421274d..8dbc1b7d19e 100644
--- a/tests/hermes_cli/test_goals.py
+++ b/tests/hermes_cli/test_goals.py
@@ -559,6 +559,27 @@ class TestJudgeDrivenWait:
assert mgr.state.waiting_on_pid is None
assert mgr.is_waiting() is False
+ def test_judge_wait_on_pid_dying_between_check_and_park_continues(self, hermes_home):
+ """The pid may exit between the judge path's liveness probe and ``wait_on``'s own
+ re-check; that race must land on the same continue decision, not raise out of
+ ``evaluate_after_turn`` (callers swallow the error and the continuation is lost)."""
+ from hermes_cli import goals
+ from hermes_cli.goals import GoalManager
+
+ mgr = GoalManager(session_id="jw-toctou-pid", default_max_turns=10)
+ mgr.set("ship the PR")
+ with patch.object(goals, "_pid_alive", return_value=True), patch.object(
+ GoalManager, "wait_on", side_effect=ValueError("pid is not alive on this host"),
+ ), patch.object(
+ goals, "judge_goal",
+ return_value=("wait", "job still running", False, {"pid": 4242}, False),
+ ):
+ decision = mgr.evaluate_after_turn("Started the job (pid 4242).")
+ assert decision["verdict"] == "continue"
+ assert decision["should_continue"] is True
+ assert mgr.state.waiting_on_pid is None
+ assert mgr.is_waiting() is False
+
def test_judge_wait_pid_parks_loop(self, hermes_home):
from hermes_cli import goals
from hermes_cli.goals import GoalManager