Skip to content

The agent that grows with you

test(codex-app-server): invariants for close()/transport-loss at the client and session boundaries

teknium191edee00ee94
diff --git a/tests/agent/transports/test_codex_app_server_runtime.py b/tests/agent/transports/test_codex_app_server_runtime.py
index b3cae82c3a1..cf2d80840b6 100644
--- a/tests/agent/transports/test_codex_app_server_runtime.py
+++ b/tests/agent/transports/test_codex_app_server_runtime.py
@@ -8,6 +8,7 @@ covered by a separate live test gated on `codex --version`.
from __future__ import annotations
import sys
+import threading
import pytest
@@ -211,6 +212,7 @@ while True:
client = mod.CodexAppServerClient.__new__(mod.CodexAppServerClient)
client._proc = proc
client._closed = False
+ client._pending, client._pending_lock = {}, threading.Lock()
client.close(timeout=0.01)
assert killed == [4242]
diff --git a/tests/agent/transports/test_codex_app_server_session.py b/tests/agent/transports/test_codex_app_server_session.py
index 499b45c51a0..80998e34e29 100644
--- a/tests/agent/transports/test_codex_app_server_session.py
+++ b/tests/agent/transports/test_codex_app_server_session.py
@@ -16,6 +16,7 @@ from typing import Any, Optional
import pytest
import agent.transports.codex_app_server_session as session_mod
+from agent.transports.codex_app_server import CodexAppServerTransportError
from agent.transports.codex_app_server_session import (
CodexAppServerSession,
_ServerRequestRouting,
@@ -966,3 +967,91 @@ class TestClassifyOAuthFailure:
assert (_classify_oauth_failure(primary, stderr=stderr) is not None) is expected
+
+# ---- transport loss / close() racing a turn (#87422, #83127) ----
+
+class TransportLossClient(FakeClient):
+ """FakeClient whose writes fail the way the real client fails on a dead pipe."""
+
+ def __init__(self, fail_on: str, **kwargs) -> None:
+ super().__init__(**kwargs)
+ self.fail_on = fail_on
+
+ def _lost(self):
+ raise CodexAppServerTransportError(code=-32000, message="codex app-server stdin closed unexpectedly: Broken pipe")
+
+ def request(self, method, params=None, timeout=30.0):
+ if method == self.fail_on:
+ self._lost()
+ return super().request(method, params, timeout)
+
+ def respond(self, request_id, result):
+ if self.fail_on == "respond":
+ self._lost()
+ super().respond(request_id, result)
+
+
+class TestTransportLoss:
+ def test_close_racing_turn_loop_ends_turn_as_interrupted(self):
+ """#87422: close() landing between poll iterations must end run_turn()/compact_thread()
+ with interrupted+should_retire, never an AttributeError on the nulled client."""
+ import threading
+
+ for run in ("run_turn", "compact_thread"):
+ ready = threading.Event()
+ client = FakeClient()
+ client._closed = False
+ polled = client.take_notification
+
+ def take_notification(timeout=0.0, _polled=polled, _ready=ready):
+ _ready.set()
+ time.sleep(0.02) # window for close() to land mid-iteration
+ return _polled(timeout)
+
+ client.take_notification = take_notification
+ client.is_alive = lambda: True # the fake stays "alive": only the session's own close() ends the turn
+ session = make_session(client)
+ out: dict = {}
+
+ def worker():
+ if run == "run_turn":
+ out["r"] = session.run_turn("hi", turn_timeout=5, notification_poll_timeout=0.001)
+ else:
+ out["r"] = session.compact_thread(turn_timeout=5, notification_poll_timeout=0.001)
+
+ t = threading.Thread(target=worker)
+ t.start()
+ assert ready.wait(timeout=5)
+ session.close()
+ t.join(timeout=5)
+ assert not t.is_alive(), run
+ result = out["r"]
+ assert result.interrupted and result.should_retire, run
+ assert "closed" in (result.error or ""), run
+
+ @pytest.mark.parametrize("run,fail_on", [
+ ("run_turn", "turn/start"),
+ ("compact_thread", "thread/compact/start"),
+ ("run_turn", "respond"),
+ ])
+ def test_write_failure_returns_retiring_result_and_control_paths_stay_non_fatal(self, run, fail_on):
+ """#83127: a transport write failure during turn/start, compaction start or an approval
+ response returns a retiring TurnResult instead of escaping; steer/interrupt stay non-fatal."""
+ client = TransportLossClient(fail_on)
+ if fail_on == "respond":
+ client.queue_server_request("item/commandExecution/requestApproval", command="ls")
+ session = make_session(client, approval_callback=lambda *a, **k: "once")
+ if run == "run_turn":
+ result = session.run_turn("hi", turn_timeout=2, notification_poll_timeout=0.001)
+ else:
+ result = session.compact_thread(turn_timeout=2, notification_poll_timeout=0.001)
+ assert result.should_retire
+ assert "stdin closed unexpectedly" in result.error
+
+ control = TransportLossClient("turn/steer")
+ steer_session = make_session(control)
+ steer_session.ensure_started()
+ steer_session._active_turn_id = "turn-fake-001"
+ assert steer_session.request_steer("more") is False
+ control.fail_on = "turn/interrupt"
+ steer_session._issue_interrupt("turn-fake-001") # must not raise