Skip to content

The agent that grows with you

fix(cli): the live --query-file read keeps main's replace/exit-2 contract

Tekniumd66976081e44
diff --git a/hermes_cli/gateway_chat.py b/hermes_cli/gateway_chat.py
index 9599c4c5931..6d0ee62abce 100644
--- a/hermes_cli/gateway_chat.py
+++ b/hermes_cli/gateway_chat.py
@@ -320,9 +320,18 @@ def launch_from_args(args) -> int:
return failed("credentials or agent init failed", 1)
query_file = getattr(args, "query_file", None)
if query_file:
- args.query = sys.stdin.read() if query_file == "-" else Path(query_file).read_text(encoding="utf-8-sig")
+ # Main's --query-file contract (the Bot Mode DM transport): undecodable bytes are
+ # replaced, and an unreadable or empty file is exit 2 with the file named.
+ try:
+ if query_file == "-":
+ args.query = sys.stdin.read()
+ else:
+ with open(query_file, encoding="utf-8-sig", errors="replace") as handle:
+ args.query = handle.read()
+ except OSError as exc:
+ return failed(f"cannot read --query-file {query_file}: {exc}", 2)
if not args.query.strip():
- raise GatewayClientError("--query-file is empty")
+ return failed(f"--query-file {query_file} is empty", 2)
if not (getattr(args, "query", None) or getattr(args, "q", None) or getattr(args, "oneshot", None) or sys.stdin.isatty()):
raise GatewayClientError("Noninteractive chat requires --query or --oneshot")
return asyncio.run(run_gateway_chat(args, emitter=emitter))
diff --git a/tests/hermes_cli/test_gateway_chat_query_file_read.py b/tests/hermes_cli/test_gateway_chat_query_file_read.py
new file mode 100644
index 00000000000..30686b502f9
--- /dev/null
+++ b/tests/hermes_cli/test_gateway_chat_query_file_read.py
@@ -0,0 +1,48 @@
+"""The live --query-file read keeps main's contract (B2-09): undecodable bytes are replaced; an
+unreadable or empty file exits 2 naming it, with a failed ``result`` line under stream-json."""
+import argparse
+import json
+
+import pytest
+
+import hermes_cli.gateway_chat as gateway_chat
+import hermes_cli.gateway_chat_startup as startup
+
+
+def _args(path, fmt="text"):
+ return argparse.Namespace(query_file=str(path), query=None, output_format=fmt, model=None)
+
+
+def launched(monkeypatch):
+ seen = []
+ monkeypatch.setattr(startup, "ensure_launch_provider", lambda args: True)
+ monkeypatch.setattr(gateway_chat, "_register_terminal_process", lambda: None)
+
+ async def run(args, emitter=None):
+ seen.append(args.query)
+ return 0
+ monkeypatch.setattr(gateway_chat, "run_gateway_chat", run)
+ return seen
+
+
[email protected]("case, expected", [("missing", "cannot read --query-file"),
+ ("directory", "cannot read --query-file"),
+ ("empty", "--query-file is empty"), ("latin1", None)])
[email protected]("fmt", ["text", "stream-json"])
+def test_query_file_read_matches_main(tmp_path, capsys, launched, case, expected, fmt):
+ path = {"missing": tmp_path / "gone.txt", "directory": tmp_path}.get(case, tmp_path / "dm.txt")
+ if case == "empty":
+ path.write_text(" \n", encoding="utf-8")
+ if case == "latin1":
+ path.write_bytes(b"caf\xe9 $(id) \"quoted\"")
+ code = gateway_chat.launch_from_args(_args(path, fmt))
+ out, err = capsys.readouterr()
+ if expected is None:
+ assert code == 0 and launched == ["caf\ufffd $(id) \"quoted\""]
+ return
+ assert code == 2 and launched == []
+ assert f"Error: {expected}" in err.replace(f"--query-file {path}", "--query-file")
+ if fmt == "stream-json":
+ result = [json.loads(line) for line in out.splitlines() if line.strip()][-1]
+ assert result["type"] == "result" and result.get("is_error", result.get("subtype") != "success")