Files
hermes-agent/tests/agent/test_session_source.py
teknium1 ae1b5d79b2 fix(sessions): one-shot runs get a distinct oneshot source that pickers hide
`hermes chat -q`/`--oneshot`/`-Q` and `hermes -z` (both set HERMES_SINGLE_QUERY_SESSION=1)
persisted their session as `cli` — and, before the first pass, as the inherited
`tui`/`desktop` transport label — so finite automation runs sat in the TUI, Desktop and
dashboard session pickers next to real conversations (#112550).

- run_agent._session_source_for_agent: a single-query run whose source is empty (or an
  inherited UI transport label without an explicit --source) resolves to `oneshot`; the
  platform gate keeps delegate children (`subagent`) untouched; an explicit `--source`
  (HERMES_SESSION_SOURCE_EXPLICIT=1 from main.py) still wins.
- hermes_state_sessions.INTERNAL_LISTING_SOURCES = (kanban, tool, oneshot) replaces the
  three copied `["kanban", "tool"]` literals (tui_gateway session.list, console
  `sessions list`/`stats`, in-chat /sessions), and the Desktop project tree / sidebar
  recents and the dashboard automation set exclude `oneshot` too.
- `hermes -c` / `--resume latest` still chain on the previous one-shot (PR #105957's
  documented flow): the CLI MRU lookup matches the cli family {cli, oneshot} and
  search_sessions accepts several sources; one-shots keep stamping their launch cwd so the
  workspace-scoped lookup keeps working.
- Compression child: the rotated child is published with the PARENT ROW's persisted source
  instead of bare agent.platform, so a `--source tool` / `oneshot` / inherited `kanban`
  session does not degrade to a picker-visible `cli` row after compaction.
- Docs: sessions source table (+ oneshot/kanban/tool rows, compression note) and the
  `--source` flag reference (explicit flag always stored as given).
2026-09-17 09:06:59 -07:00

69 lines
2.5 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_run_gets_distinct_source(monkeypatch, inherited):
"""A finite `hermes chat -q` / `hermes -z` run is tagged `oneshot`, whether launched from a plain shell
or spawned inside a TUI/Desktop session (whose transport label it inherits but is not) (#112550)."""
monkeypatch.setenv("HERMES_SESSION_SOURCE", inherited)
monkeypatch.setenv("HERMES_SINGLE_QUERY_SESSION", "1")
assert _session_source_for_agent("cli") == "oneshot"
def test_oneshot_marker_does_not_relabel_subagents(monkeypatch):
"""Delegate children inside a one-shot process share its env but keep their own platform."""
monkeypatch.delenv("HERMES_SESSION_SOURCE", raising=False)
monkeypatch.setenv("HERMES_SINGLE_QUERY_SESSION", "1")
assert _session_source_for_agent("subagent") == "subagent"
@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