From 131c9c542c0d8d8aed401e04d5e60002a0dcb584 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sat, 27 Jun 2026 20:58:42 -0700 Subject: [PATCH] test(tui-gateway): stop deferred-resume build thread leaking into next test test_session_resume_uses_parent_lineage_for_display resumes via the deferred (non-eager) path, which fires a 50ms background Timer (_schedule_agent_build) calling whatever server._make_agent is patched in at that moment. The timer outlived the test and landed in the next test's (_follows_compression_tip) _make_agent mock, racily setting agent_session_id='tip' and flaking 'assert tip == cont_tip' on CI. Root-cause fix: stub _schedule_agent_build to a no-op in the leaking test (it only asserts display history). Defense in depth: the victim's fake_make_agent now setdefault()s so a stray late build can't overwrite the synchronous eager build's captured id. --- tests/test_tui_gateway_server.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/test_tui_gateway_server.py b/tests/test_tui_gateway_server.py index 92a01b3f41d..2ad990afb1b 100644 --- a/tests/test_tui_gateway_server.py +++ b/tests/test_tui_gateway_server.py @@ -955,6 +955,14 @@ def test_session_resume_uses_parent_lineage_for_display(monkeypatch): monkeypatch.setattr( server, "_init_session", lambda sid, key, agent, history, cols=80, **_kwargs: None ) + # This resume takes the deferred (non-eager) path, which fires a 50ms + # background Timer (`_schedule_agent_build`) that later calls whatever + # `server._make_agent` is patched in AT THAT MOMENT. Left un-stubbed, that + # timer outlives this test and lands in the *next* test's `_make_agent` + # mock, racily corrupting its captured state (the `assert 'tip' == + # 'cont_tip'` flake in test_session_resume_follows_compression_tip). Neuter + # the pre-warm here — this test only asserts the returned display history. + monkeypatch.setattr(server, "_schedule_agent_build", lambda *a, **k: None) resp = server.handle_request( {"id": "1", "method": "session.resume", "params": {"session_id": "tip"}} @@ -1004,7 +1012,10 @@ def test_session_resume_follows_compression_tip(monkeypatch, tmp_path): captured = {} def fake_make_agent(sid, key, session_id=None, session_db=None, **kwargs): - captured["agent_session_id"] = session_id + # Record only the FIRST (synchronous, eager) build. A stray background + # build leaked from an earlier test's deferred resume could otherwise + # overwrite this with its own session_id and corrupt the assertion. + captured.setdefault("agent_session_id", session_id) return types.SimpleNamespace(model="test", provider="test") monkeypatch.setattr(server, "_get_db", lambda: db)