Skip to content

The agent that grows with you

fix(cli): a finite -q/-Q run is main's `oneshot` session, so bare -c finds it

Teknium4e0cd690584c
diff --git a/hermes_cli/gateway_session_store.py b/hermes_cli/gateway_session_store.py
index f99bcbf0ac2..ac05bfbd160 100644
--- a/hermes_cli/gateway_session_store.py
+++ b/hermes_cli/gateway_session_store.py
@@ -133,7 +133,10 @@ class GatewaySessionStore:
raise
params = _creation_policy(launch or {})
params["cwd"] = os.path.realpath(os.getenv("TERMINAL_CWD") or os.getcwd())
- params["source"] = os.environ.get("HERMES_SESSION_SOURCE") or "cli"
+ # Main's label for this row (a finite `-q`/`-Q` run is ``oneshot``, which `hermes -c` and
+ # `--resume latest` count as CLI history; `--source` wins), the rule run_agent applies.
+ from agent.session_source import session_source_for
+ params["source"] = session_source_for("cli")
snapshot = self.call("session.create", request_id=uuid.uuid4().hex, **params)
return self._adopt(snapshot)
@@ -244,3 +247,4 @@ def _creation_policy(launch):
def session_store():
return GatewaySessionStore()
+
diff --git a/tests/hermes_cli/test_gateway_chat_flags_live.py b/tests/hermes_cli/test_gateway_chat_flags_live.py
index 3eeaed41ee6..e37eb42b34b 100644
--- a/tests/hermes_cli/test_gateway_chat_flags_live.py
+++ b/tests/hermes_cli/test_gateway_chat_flags_live.py
@@ -1,4 +1,5 @@
-"""Gateway-path launch flags main supported: bare -c, --resume latest, --list-tools, resume footer."""
+"""Launch flags on the classic CLI (main's HermesCLI, gateway-backed): bare -c, --resume latest, --list-tools,
+resume footer. Expectations are main's (git archive 3b63f7b0a4c, lanes/pushready/probe_oneshot.py)."""
import json
from http.server import ThreadingHTTPServer
import os
@@ -41,8 +42,11 @@ def test_continue_latest_footer_and_tool_listing(tmp_path):
def hermes(*args, cwd, module="hermes_cli.main"):
result = subprocess.run([sys.executable, "-m", module, *args], cwd=cwd, env=env,
stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=120, check=False)
- sid = next((line.split("Session: ", 1)[1].strip() for line in result.stderr.splitlines()
- if line.startswith("Session: ")), None)
+ # Main's channels: -Q prints `session_id: <id>` on stderr, -q its exit summary's `Session:` row.
+ sid = next((line.split(":", 1)[1].strip() for line in result.stderr.splitlines()
+ if line.startswith("session_id: ")), None)
+ sid = sid or next((line.split(":", 1)[1].strip() for line in result.stdout.splitlines()
+ if line.startswith("Session:")), None)
return result, sid
with (home / "gw.log").open("w") as log:
@@ -61,7 +65,7 @@ def test_continue_latest_footer_and_tool_listing(tmp_path):
assert empty.returncode == 1 and "No previous CLI session" in empty.stderr, empty
first, sid_a = hermes("chat", "-q", "in repo a", cwd=repo_a)
- assert first.returncode == 0 and "RECOVERY_ACK_in repo a" in first.stdout, first
+ assert first.returncode == 0 and "RECOVERY_ACK_in repo a" in first.stdout and sid_a, first
# Main's single-query exit block: how to continue the session it just printed.
assert f"Resume this session with:\n hermes --resume {sid_a}" in first.stdout, first.stdout
quiet, sid_b = hermes("chat", "-q", "in repo b", "-Q", cwd=repo_b)
diff --git a/tests/hermes_cli/test_gateway_session_store.py b/tests/hermes_cli/test_gateway_session_store.py
index 65b604f30fa..cb4d812a277 100644
--- a/tests/hermes_cli/test_gateway_session_store.py
+++ b/tests/hermes_cli/test_gateway_session_store.py
@@ -67,6 +67,27 @@ def test_new_session_is_created_on_the_gateway_and_its_id_adopted(store, monkeyp
assert db.connection.state("local-new").info["model"] == "fake-model"
+def test_a_finite_run_creates_mains_oneshot_session_so_bare_c_finds_it(store, monkeypatch):
+ """Main labels a `chat -q` / `-Q` session ``oneshot`` (agent.session_source.session_source_for), which
+ `hermes -c` / `--resume latest` count as CLI history; created as ``cli`` the owner stores the
+ transport's ``local`` and the next bare `-c` finds nothing."""
+ monkeypatch.delenv("HERMES_SESSION_SOURCE", raising=False)
+ monkeypatch.delenv("HERMES_SESSION_SOURCE_EXPLICIT", raising=False)
+ monkeypatch.setenv("HERMES_SINGLE_QUERY_SESSION", "1")
+ db, client = store
+ db.open_session(cli(), {})
+ assert client.calls[-1][1]["source"] == "oneshot"
+ # An explicit `--source tool` still wins, and an interactive session stays `cli`.
+ monkeypatch.setenv("HERMES_SESSION_SOURCE", "tool")
+ monkeypatch.setenv("HERMES_SESSION_SOURCE_EXPLICIT", "1")
+ GatewaySessionStore(db.connection).open_session(cli(), {})
+ assert client.calls[-1][1]["source"] == "tool"
+ for name in ("HERMES_SINGLE_QUERY_SESSION", "HERMES_SESSION_SOURCE", "HERMES_SESSION_SOURCE_EXPLICIT"):
+ monkeypatch.delenv(name)
+ GatewaySessionStore(db.connection).open_session(cli(), {})
+ assert client.calls[-1][1]["source"] == "cli"
+
+
def test_resume_attaches_and_unknown_target_is_left_to_mains_not_found_path(store):
db, client = store
assert db.open_session(cli("local-old", resumed=True)) == "local-old"