Files
hermes-agent/tests/agent/test_session_source.py
teknium1 0010273f1e fix(sessions): an explicit --source tui|desktop survives the one-shot label drop
_session_source_for_agent now drops an inherited tui/desktop
HERMES_SESSION_SOURCE for finite chats, but main.py exports the
documented --source flag through the very same variable, so
'hermes chat -q --source tui' was silently relabelled 'cli' with no way
to opt out. main.py marks an explicit flag with
HERMES_SESSION_SOURCE_EXPLICIT=1 and the resolver keeps the label when
that marker is set; no internal launcher passes --source tui/desktop, so
inherited transport labels are still dropped.
2026-09-16 17:50:21 -07:00

61 lines
2.2 KiB
Python

import pytest
from gateway.session_context import _UNSET, _VAR_MAP, clear_session_vars, set_session_vars
from run_agent import _session_source_for_agent
@pytest.fixture(autouse=True)
def _reset_contextvars():
for var in _VAR_MAP.values():
var.set(_UNSET)
yield
for var in _VAR_MAP.values():
var.set(_UNSET)
def test_session_source_context_overrides_platform(monkeypatch):
monkeypatch.delenv("HERMES_SESSION_SOURCE", raising=False)
tokens = set_session_vars(source="tool")
try:
assert _session_source_for_agent("tui") == "tool"
finally:
clear_session_vars(tokens)
def test_session_source_falls_back_to_platform(monkeypatch):
monkeypatch.delenv("HERMES_SESSION_SOURCE", raising=False)
assert _session_source_for_agent("tui") == "tui"
@pytest.mark.parametrize("inherited", ["tui", "desktop"])
def test_oneshot_child_drops_inherited_ui_transport_source(monkeypatch, inherited):
"""A finite `hermes chat -q` spawned from a TUI/Desktop session inherits the transport's
HERMES_SESSION_SOURCE but is not that conversation: it keeps its own platform label (#112550)."""
monkeypatch.setenv("HERMES_SESSION_SOURCE", inherited)
monkeypatch.setenv("HERMES_SINGLE_QUERY_SESSION", "1")
assert _session_source_for_agent("cli") == "cli"
@pytest.mark.parametrize("inherited", ["kanban", "tool", "a2a"])
def test_oneshot_child_keeps_inherited_automation_source(monkeypatch, inherited):
monkeypatch.setenv("HERMES_SESSION_SOURCE", inherited)
monkeypatch.setenv("HERMES_SINGLE_QUERY_SESSION", "1")
assert _session_source_for_agent("cli") == inherited
@pytest.mark.parametrize("explicit", ["tui", "desktop"])
def test_oneshot_keeps_explicit_source_flag(monkeypatch, explicit):
"""`hermes chat -q --source tui` is a documented flag, not an inherited transport label: main.py
marks it HERMES_SESSION_SOURCE_EXPLICIT=1 and the one-shot drop must not override it."""
monkeypatch.setenv("HERMES_SESSION_SOURCE", explicit)
monkeypatch.setenv("HERMES_SESSION_SOURCE_EXPLICIT", "1")
monkeypatch.setenv("HERMES_SINGLE_QUERY_SESSION", "1")
assert _session_source_for_agent("cli") == explicit