Files
Teknium 0715474bfd Merge origin/main into feat/unified-gateway-runtime
375 commits from main (0e427d788d). 24 files conflicted; the resolutions that carry a decision:

- /initiate-setup: main removed the command (25bf722063). Its gateway rewrite, CLI handler and the
  Desktop kickoff go with main; this branch's command-layer admission of it is dropped too
  (gateway/session_commands.py _PROMPT_COMMANDS, its slash.exec test row, the parity doc row), and
  the Desktop slash dispatcher loses the `hidden` option that only the kickoff used.
- Code this branch moved out of its old file, carrying main's edits to the moved copy:
  first-contact notes (gateway/run_turn.py -> run_turn_prepare.py: no message/setup_command),
  the TUI agent factory (tui_gateway/server.py -> agent_factory.py: onboarding toolsets at build,
  setup_beat/set_theme lifecycle cards, no cli platform extras on a coding selection), the serve
  lifespan (web_server.py -> web_server_app.py standalone_lifespan: first-install probe before the
  state.db reconcile, onboarding due answer after the setup bootstrap), and the compression child
  publish (the shared _publish_compression_child_on_conn reads the parent's model_config).
- agent/auxiliary_client.py: both import changes (_route_key from main; this branch's move of
  _without_structured_output_format).
- Desktop: main's onboarding run (decideOnboarding, OnboardingEnd, card-waiting Send) replaces the
  kickoff; this branch's prepared-submission slash path, request_id on session.create and
  PreparedImageRecovery stay. Pool-limits imports are dropped (this branch removed the pool).
  createBackendSessionForSend takes main's (preview, overrides) signature.
- ci.yaml detect job: main's `ref: inputs.commit` plus this branch's sparse checkout.
- package.json / lock: main's vitest 4.1.11 plus this branch's ws 8.21.1.
- Generated: gateway-contract.openrpc.json regenerated; parity doc regenerated.
2026-10-14 05:52:49 -07:00

138 lines
7.1 KiB
Python

"""``hermes chat -q … --format stream-json`` emits a parseable JSONL event stream and nothing else on stdout."""
import json
import pytest
from hermes_cli.stream_json import StreamJsonEmitter
def _events(capsys):
out = capsys.readouterr().out
return [json.loads(line) for line in out.splitlines() if line]
def test_emitter_event_stream_is_valid_jsonl(capsys):
emitter = StreamJsonEmitter(model="test-model", session_id="s-1")
emitter.on_text_delta("hel")
emitter.on_text_delta("\n ") # whitespace deltas are part of the answer and must be forwarded verbatim
emitter.on_text_delta("lo")
emitter.on_text_delta(None) # the turn-end sentinel the agent sends
emitter.on_text_delta("")
emitter.on_tool_progress("tool.started", "read_file", "preview", {"path": "x"}, tool_call_id="call-a")
emitter.on_tool_progress("tool.started", "read_file", "preview", {"path": "y"}, tool_call_id="call-b")
emitter.on_tool_progress("reasoning.available", "_thinking", "hmm", None) # not part of the protocol
emitter.on_tool_progress("tool.completed", "read_file", None, None, tool_call_id="call-b", result="y")
emitter.on_tool_progress("tool.completed", "read_file", None, None, tool_call_id="call-a", duration=0.5,
is_error=False, result="x" * 6000)
code = emitter.emit_result({"final_response": "", "failed": True, "error": "boom", "input_tokens": 3}, exit_code=0)
events = _events(capsys)
assert [e["type"] for e in events] == ["system", "text", "text", "text", "tool_use", "tool_use", "tool_result",
"tool_result", "result"]
assert "".join(e["text"] for e in events if e["type"] == "text") == "hel\n lo"
assert events[0]["subtype"] == "init" and events[0]["model"] == "test-model"
assert events[4]["input"] == {"path": "x"} and events[4]["tool_call_id"] == "call-a"
# concurrent same-name calls: each result pairs with its own start, not the last-started one
assert [e["tool_call_id"] for e in events if e["type"] == "tool_result"] == ["call-b", "call-a"]
assert events[6]["duration_ms"] < 500
assert events[7]["duration_ms"] == 500 and events[7]["output"].endswith("...") and len(events[7]["output"]) == 5003
assert code == 1 and events[-1] == {**events[-1], "exit_code": 1, "error": "boom", "session_id": "s-1"}
assert events[-1]["tokens"]["input"] == 3
assert all("timestamp" in e for e in events)
def _run_stream_json_chat(monkeypatch, capsys, run_turn, credentials_ok=True):
"""parser → cmd_chat → gateway transport with a deterministic fake authority peer.
``run_turn(peer)`` plays the authority: it queues the execution events a turn would publish and
returns the terminal ``message.complete`` payload (or raises ``KeyboardInterrupt`` for Ctrl-C).
"""
import asyncio
from contextlib import asynccontextmanager
import hermes_cli.main as cli_entry
from hermes_cli import gateway_chat, gateway_chat_startup
from hermes_cli._parser import build_top_level_parser
class Peer:
def __init__(self):
self.events = asyncio.Queue()
def emit(self, kind, payload, admission="adm-1"):
self.events.put_nowait({"method": "event", "params": {
"type": kind, "session_id": "session-123", "admission_id": admission, "payload": payload}})
async def rpc(self, method, **params):
if method == "runtime.describe":
return {"session_create": {"sources": ["cli"], "parameters": ["cwd", "model", "request_id", "source"]}}
if method == "session.create":
return {"stored_session_id": "session-123", "info": {"model": "test-model"}}
if method == "prompt.receipt": # the settled result the exit code is judged from
return {"status": "terminal", "result": {"completed": True, "final_response": "hello"}}
assert method == "prompt.submit"
self.emit("message.complete", {**run_turn(self), "admission_id": "adm-1"})
return {"admission_id": "adm-1"}
@asynccontextmanager
async def connected():
yield Peer()
monkeypatch.setattr(gateway_chat, "connect_gateway", connected)
monkeypatch.setattr(gateway_chat_startup, "ensure_launch_provider", lambda _args: credentials_ok)
monkeypatch.setattr(cli_entry, "_resolve_use_tui", lambda _args: pytest.fail("TUI resolution consulted"))
monkeypatch.setattr(cli_entry, "_confirm_startup_expensive_model_override", lambda _a: None)
monkeypatch.setattr(cli_entry, "_warn_retired_xai_models", lambda: None)
monkeypatch.setattr("hermes_cli.quiet_single_query.continue_quiet_notify_completions", lambda *_a, **_k: None)
parser, _, _ = build_top_level_parser()
args = parser.parse_args(["chat", "-q", "hello", "--format", "stream-json"])
with pytest.raises(SystemExit) as exc_info:
cli_entry.cmd_chat(args)
return exc_info.value.code, _events(capsys)
def _ok_turn(peer):
peer.emit("message.delta", {"text": "hello"})
peer.emit("tool.start", {"tool_call_id": "c1", "tool_name": "read_file", "args": {"path": "f"}})
peer.emit("tool.complete", {"tool_call_id": "c1", "tool_name": "read_file", "args": {"path": "f"},
"is_error": False, "result": "contents"})
return {"text": "hello", "outcome": "completed"}
def _interrupted_turn(_peer):
raise KeyboardInterrupt
@pytest.mark.parametrize("turn, credentials_ok, exit_code, types", [
(_ok_turn, True, 0, ["system", "text", "tool_use", "tool_result", "result"]),
(_interrupted_turn, True, 130, ["system", "result"]),
(_ok_turn, False, 1, ["system", "result"]), # credentials fail before any session exists
])
def test_chat_stream_json_implies_quiet_and_closes_with_result(monkeypatch, capsys, turn, credentials_ok, exit_code,
types):
"""No ``-Q`` needed; stdout is only JSONL; the stream always ends in a ``result`` carrying the exit code."""
code, events = _run_stream_json_chat(monkeypatch, capsys, turn, credentials_ok=credentials_ok)
assert code == exit_code
assert [e["type"] for e in events] == types
assert events[-1]["exit_code"] == exit_code
# A launch refused before the gateway created a session has none to report.
assert events[-1]["session_id"] == ("session-123" if credentials_ok else "")
@pytest.mark.parametrize("argv, message", [
(["chat", "--format", "stream-json"], "requires -q/--query"),
(["chat", "-q", "hi", "--format", "stream-json", "--tui"], "cannot be used with --tui"),
(["--tui", "chat", "-q", "hi", "--format", "stream-json"], "cannot be used with --tui"),
])
def test_chat_stream_json_rejects_interactive_combinations(monkeypatch, capsys, argv, message):
import hermes_cli.main as cli_entry
from hermes_cli._parser import build_top_level_parser
monkeypatch.setattr(cli_entry, "_launch_tui", lambda *_a, **_k: pytest.fail("TUI launched"))
parser, _, _ = build_top_level_parser()
with pytest.raises(SystemExit) as exc_info:
cli_entry.cmd_chat(parser.parse_args(argv))
assert exc_info.value.code == 2
assert message in capsys.readouterr().err