Skip to content

The agent that grows with you

fix(gateway): a reused turn worker drops the previous turn's log session context

Tekniumd4e4ae12ada6
diff --git a/gateway/turn_thread_reset.py b/gateway/turn_thread_reset.py
index cb6dcc74869..8ec060b3895 100644
--- a/gateway/turn_thread_reset.py
+++ b/gateway/turn_thread_reset.py
@@ -31,6 +31,7 @@ _PER_TURN_THREAD_LOCALS = (
("agent.local_prompt_progress", "_reading"),
("hermes_cli.plugins", "_thread_tool_whitelist"),
("hermes_cli.observability.shared_metrics_events", "_compression_attempt"),
+ ("hermes_logging", "_session_context"), # the log line's session tag + sanitizer-heal accounting key
)
diff --git a/tests/gateway/test_turn_executor_reuse.py b/tests/gateway/test_turn_executor_reuse.py
index d2841d79aef..8712f8fb1c8 100644
--- a/tests/gateway/test_turn_executor_reuse.py
+++ b/tests/gateway/test_turn_executor_reuse.py
@@ -83,3 +83,28 @@ def test_submit_to_a_parked_worker_starts_no_thread_and_shutdown_releases_it(mon
assert runner._shutdown_executor(drain_timeout=5) == 0
parked.join(5)
assert not parked.is_alive()
+
+
+def test_a_reused_worker_does_not_log_as_the_previous_turns_session():
+ import logging
+
+ import hermes_logging
+
+ ex = _UnboundedThreadExecutor("reuse-probe")
+ try:
+ def turn_one():
+ hermes_logging.set_session_context("session-a") # build_turn_context does this, never clears it
+ return threading.get_ident()
+
+ first = ex.submit(turn_one).result(5)
+ _wait_parked(ex)
+
+ def next_item(): # another session's pre-turn work, an idle gate, another profile's turn
+ record = logging.getLogRecordFactory()("gateway.run", logging.INFO, __file__, 1, "msg", (), None)
+ return threading.get_ident(), record.session_tag
+
+ tid, tag = ex.submit(next_item).result(5)
+ assert tid == first, "the parked worker was not reused"
+ assert tag == "", f"the reused thread logs as {tag!r}"
+ finally:
+ ex.shutdown(wait=True)