Files
hermes-agent/gateway/turn_thread_reset.py
Teknium d4e4ae12ad fix(gateway): a reused turn worker drops the previous turn's log session context
Every turn sets hermes_logging's thread-local session id (build_turn_context) and never
clears it. Turn-executor threads are now reused, so the next item on that thread -- another
session's pre-turn work, an idle gate, another profile's turn -- logged under the previous
session's tag, and the sanitizer-heal accounting keyed on the same thread-local
(_session_id_for_heal_log, consume_pending_sanitizer_heal_notice) charged the wrong session.
A new thread starts with none of it; the reset between items now drops it too.

Swept every module-level threading.local in the tree: the rest are context-manager scoped
(lock holders, thread_scoped_silence, maintenance/CLI flows) or per-thread caches (stemmer,
worker loops), so none other carries per-turn state across items.
2026-10-27 12:35:50 -07:00

52 lines
2.5 KiB
Python

"""Make a reused turn-executor thread look like a new one to its next item.
Turn code keys some state by the running thread instead of by turn: ``tools.interrupt`` bits and
reasons by thread id, per-turn callbacks in module ``threading.local`` objects, and the sudo
password cache's thread scope (the fallback when a turn has no session key). A new thread starts
with none of it; a parked executor worker (gateway/turn_executor.py) must drop all of it before it
takes another session's turn, or turn 1's interrupt, approval callback or cached sudo password is
live in turn 2. Context variables need nothing here: every item runs in a fresh context.
Modules are looked up in ``sys.modules`` only: one that was never imported holds no state.
A new per-turn ``threading.local`` in turn code belongs in ``_PER_TURN_THREAD_LOCALS``.
"""
from __future__ import annotations
import sys
import threading
_PER_TURN_THREAD_LOCALS = (
("tools.terminal_tool", "_callback_tls"),
("tools.environments.base", "_activity_callback_local"),
("tools.feishu_lark", "_local"),
("agent.vault_backends.unlock", "_callback_tls"),
("agent.vault_backends.unlock", "_current_session_tls"),
("agent.auxiliary_client", "_aux_probe_state"),
("agent.auxiliary_client", "_aux_interrupt_protection"),
("agent.auxiliary_client", "_aux_progress"),
("agent.auxiliary_client", "_aux_dispatch"),
("agent.auxiliary_client", "_aux_provider_response"),
("agent.auxiliary_client", "_aux_stream_deadline"),
("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
)
def reset_reused_thread_state() -> None:
"""Drop the calling thread's turn-keyed state (call from the worker itself, between items)."""
tid = threading.get_ident()
interrupt = sys.modules.get("tools.interrupt")
if interrupt is not None:
interrupt.set_interrupt(False, tid) # clears the bit, its reason and a pending yield
sudo = sys.modules.get("tools.terminal_tool_sudo")
if sudo is not None:
with sudo._sudo_password_cache_lock:
sudo._sudo_password_cache.pop(f"thread:{tid}", None)
for module_name, attr in _PER_TURN_THREAD_LOCALS:
local = getattr(sys.modules.get(module_name), attr, None)
if local is not None:
local.__dict__.clear() # a threading.local's __dict__ is the calling thread's namespace