fix: add type hints to session key helpers, extend context-local key to terminal_tool

- Add contextvars.Token[str] type hints to set/reset_current_session_key
- Use get_current_session_key(default='') in terminal_tool.py for background
  process session tracking, fixing the same env var race for concurrent
  gateway sessions spawning background processes
This commit is contained in:
Teknium
2026-04-03 15:07:18 -07:00
committed by Teknium
parent 3bfb39a25f
commit fb654c15d8
2 changed files with 4 additions and 3 deletions

View File

@@ -29,12 +29,12 @@ _approval_session_key: contextvars.ContextVar[str] = contextvars.ContextVar(
)
def set_current_session_key(session_key: str):
def set_current_session_key(session_key: str) -> contextvars.Token[str]:
"""Bind the active approval session key to the current context."""
return _approval_session_key.set(session_key or "")
def reset_current_session_key(token) -> None:
def reset_current_session_key(token: contextvars.Token[str]) -> None:
"""Restore the prior approval session key context."""
_approval_session_key.reset(token)

View File

@@ -1088,9 +1088,10 @@ def terminal_tool(
# Spawn a tracked background process via the process registry.
# For local backends: uses subprocess.Popen with output buffering.
# For non-local backends: runs inside the sandbox via env.execute().
from tools.approval import get_current_session_key
from tools.process_registry import process_registry
session_key = os.getenv("HERMES_SESSION_KEY", "")
session_key = get_current_session_key(default="")
effective_cwd = workdir or cwd
try:
if env_type == "local":