Files
hermes-agent/tests/hermes_cli/test_single_query_exit_contract.py
T
teknium1 23863ccbaf fix(cli): one-shot chat -q exits non-zero on failure; 75 covers upstream 429 and overload
The non-quiet one-shot path exited 0 unless a Kanban worker was running, so
scripts could not tell a failed `hermes chat -q` from a good one and an
incomplete turn (partial, iteration budget) still read as success (#111770).
Both one-shot paths now share one contract: 0 completed, 1 failed / partial /
incomplete / never ran, 130 interrupted. The Kanban EX_TEMPFAIL sentinel also
fires for `upstream_rate_limit` (aggregator's upstream 429) and `overloaded`
(503/529): neither says anything about the task, so the dispatcher should
requeue without a failure tick rather than count it toward the breaker.
2026-09-15 21:46:41 -07:00

66 lines
2.7 KiB
Python

"""One-shot ``chat -q`` runs report their outcome in the exit code, like ``-Q`` always did.
The non-quiet one-shot path used to fall through to an implicit rc=0 for every
outcome, so scripts could not tell a failed run from a good one and the Kanban
dispatcher (which spawns ``chat -q`` workers) booked a provider quota wall as a
protocol violation (#111770, #101800; salvage of #110917 / #97623).
"""
from __future__ import annotations
from types import SimpleNamespace
import pytest
import cli
from hermes_cli.kanban_db import KANBAN_RATE_LIMIT_EXIT_CODE
@pytest.fixture(autouse=True)
def _no_inherited_kanban_env(monkeypatch):
monkeypatch.delenv("HERMES_KANBAN_TASK", raising=False)
monkeypatch.delenv("HERMES_KANBAN_GOAL_MODE", raising=False)
def _run_non_quiet(monkeypatch, turn_result):
"""Drive ``_run_single_query_mode`` down the non-quiet tail; return the exit code (None = fell through)."""
monkeypatch.setattr(cli, "_should_seed_interactive", lambda *a, **k: False)
monkeypatch.setattr(cli, "_collect_query_images", lambda q, i: (q, []))
monkeypatch.setattr(cli, "_collect_kanban_task_images", lambda imgs: [])
monkeypatch.setattr(cli, "_finalize_single_query", lambda c: None)
stub = SimpleNamespace(
_single_query_mode=False,
_claim_active_session=lambda *a, **k: True,
console=SimpleNamespace(print=lambda *a, **k: None),
_show_security_advisories=lambda: None,
chat=lambda *a, **k: "response",
_print_exit_summary=lambda **k: None,
_last_turn_result=turn_result,
)
try:
cli._run_single_query_mode(stub, "do the thing", None, False, True)
except SystemExit as exc:
return exc.code
return None
@pytest.mark.parametrize("reason", ["rate_limit", "upstream_rate_limit", "billing", "overloaded"])
def test_dispatcher_spawned_worker_signals_a_quota_wall_not_a_protocol_violation(monkeypatch, reason):
monkeypatch.setenv("HERMES_KANBAN_TASK", "t_abc123")
code = _run_non_quiet(monkeypatch, {"failed": True, "failure_reason": reason})
assert code == KANBAN_RATE_LIMIT_EXIT_CODE
@pytest.mark.parametrize(
("turn_result", "expected"),
[
({"final_response": "done", "completed": True}, 0),
({"failed": True, "failure_reason": "rate_limit"}, 1), # a person's run: a wall is just a failure
({"final_response": "half", "completed": False, "partial": True}, 1),
({"completed": False, "interrupted": True}, 130),
(None, 1), # credentials / agent init failed before any turn ran
],
)
def test_a_plain_one_shot_run_reports_its_outcome(monkeypatch, turn_result, expected):
assert _run_non_quiet(monkeypatch, turn_result) == expected