The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(kanban): preserve credential startup failure exit codes
funky-xamarin25b6a9f010f8
diff --git a/hermes_cli/cli_agent_setup_mixin.py b/hermes_cli/cli_agent_setup_mixin.pyindex be196092acf..ba266aee465 100644--- a/hermes_cli/cli_agent_setup_mixin.py+++ b/hermes_cli/cli_agent_setup_mixin.py@@ -234,6 +234,7 @@ class CLIAgentSetupMixin:runtime = None_model_at_entry = self.modelself._credentials_rate_limited = False+ self._credentials_terminal = Falsetry:# target_model: the ladder's model-keyed rungs (Zen/Go api_mode, Copilot/Nous# api_mode) must see the model this CLI will actually send, not config's `default`,@@ -249,8 +250,11 @@ class CLIAgentSetupMixin:if runtime is not None:_primary_exc = Noneif runtime is None:- from hermes_cli.auth import is_rate_limited_auth_error+ from hermes_cli.auth import AuthError, is_rate_limited_auth_errorself._credentials_rate_limited = bool(_primary_exc) and is_rate_limited_auth_error(_primary_exc)+ # Only an explicit re-authentication requirement is terminal. Unknown+ # resolver/network failures must not acquire the sticky Kanban block.+ self._credentials_terminal = isinstance(_primary_exc, AuthError) and _primary_exc.relogin_requiredmessage = format_runtime_provider_error(_primary_exc) if _primary_exc else "Provider resolution failed."if getattr(self, "tool_progress_mode", "full") == "off":print(message, file=sys.stderr) # quiet/stream-json: stdout is machine-readablediff --git a/hermes_cli/cli_single_query.py b/hermes_cli/cli_single_query.pyindex ed548e12338..bb3ecb86dee 100644--- a/hermes_cli/cli_single_query.py+++ b/hermes_cli/cli_single_query.py@@ -142,7 +142,8 @@ _TERMINAL_PROVIDER_REASONS = frozenset({})-def _single_query_exit_code(result, *, credentials_rate_limited: bool = False) -> int:+def _single_query_exit_code(result, *, credentials_rate_limited: bool = False,+ credentials_terminal: bool = False) -> int:"""Map a one-shot turn result onto a process exit code, for both `-q` and `-Q`.0 only when the turn completed; 130 when it was interrupted; 1 when it failed, stopped@@ -154,13 +155,18 @@ def _single_query_exit_code(result, *, credentials_rate_limited: bool = False) -The same sentinel applies when credential resolution itself is a quota/rate-limitAuthError (no turn result object is produced). One that failed on a terminal providererror (credential revoked, model gone) exits ``KANBAN_TERMINAL_PROVIDER_EXIT_CODE``- (EX_CONFIG): the dispatcher blocks the card at once.+ (EX_CONFIG): the dispatcher blocks the card at once. The same code applies+ before a turn when credential resolution explicitly requires re-authentication;+ unknown startup failures retain exit 1."""from cli import _TERMINAL_PROVIDER_REASONS, _TRANSIENT_PROVIDER_REASONSif not isinstance(result, dict):if credentials_rate_limited and os.environ.get("HERMES_KANBAN_TASK"):from hermes_cli.kanban_db import KANBAN_RATE_LIMIT_EXIT_CODEreturn KANBAN_RATE_LIMIT_EXIT_CODE+ if credentials_terminal and os.environ.get("HERMES_KANBAN_TASK"):+ from hermes_cli.kanban_db import KANBAN_TERMINAL_PROVIDER_EXIT_CODE+ return KANBAN_TERMINAL_PROVIDER_EXIT_CODEreturn 1if result.get("interrupted"):return 130@@ -495,7 +501,8 @@ def _run_single_query_mode(cli, query, image, quiet, oneshot, stream_json: bool_run_quiet_single_query(cli, effective_query, emitter=emitter)fail_code = _single_query_exit_code(- None, credentials_rate_limited=getattr(cli, "_credentials_rate_limited", False))+ None, credentials_rate_limited=getattr(cli, "_credentials_rate_limited", False),+ credentials_terminal=getattr(cli, "_credentials_terminal", False))if emitter is not None:emitter.emit_result({"failed": True, "error": "credentials or agent init failed"},session_id=cli.session_id or "", exit_code=fail_code)@@ -517,6 +524,9 @@ def _run_single_query_mode(cli, query, image, quiet, oneshot, stream_json: boolcli._print_exit_summary(clear_screen=False)# Same exit contract as `-Q`: scripts and the Kanban dispatcher read the outcome from# the exit code. This path used to fall through to an implicit 0 for every outcome.- exit_single_query(_single_query_exit_code(cli._last_turn_result))+ exit_single_query(_single_query_exit_code(+ cli._last_turn_result,+ credentials_rate_limited=getattr(cli, "_credentials_rate_limited", False),+ credentials_terminal=getattr(cli, "_credentials_terminal", False)))finally:_finalize_single_query(cli)diff --git a/tests/hermes_cli/test_kanban_credential_startup_exit.py b/tests/hermes_cli/test_kanban_credential_startup_exit.pynew file mode 100644index 00000000000..f451c41bc51--- /dev/null+++ b/tests/hermes_cli/test_kanban_credential_startup_exit.py@@ -0,0 +1,59 @@+"""Credential rejection before a turn must retain the Kanban exit contract."""+from types import SimpleNamespace++import pytest++import cli+from hermes_cli.auth import AuthError, CODEX_RATE_LIMITED_CODE+from hermes_cli.cli_agent_setup_mixin import CLIAgentSetupMixin++[email protected]("quiet", [False, True])[email protected]("worker", [False, True])[email protected]("kind,worker_exit", [("revoked", 78), ("quota", 75), ("unknown", 1)])+def test_credential_resolution_exit(monkeypatch, quiet, worker, kind, worker_exit):+ monkeypatch.delenv("HERMES_KANBAN_TASK", raising=False)+ monkeypatch.delenv("HERMES_KANBAN_GOAL_MODE", raising=False)+ if worker:+ monkeypatch.setenv("HERMES_KANBAN_TASK", "t_fixture")+ errors = {+ "revoked": AuthError("fixture revoked", provider="openai-codex", code="invalid_grant", relogin_required=True),+ "quota": AuthError("fixture quota", provider="openai-codex", code=CODEX_RATE_LIMITED_CODE),+ "unknown": RuntimeError("fixture transport"),+ }+ def resolve(**kwargs):+ raise errors[kind]+ monkeypatch.setattr("hermes_cli.runtime_provider.resolve_runtime_provider", resolve)+ shell = CLIAgentSetupMixin.__new__(CLIAgentSetupMixin)+ shell.model = "fixture"+ shell.requested_provider = "openai-codex"+ shell._explicit_api_key = shell._explicit_base_url = None+ shell._fallback_model = []+ shell.tool_progress_mode = "off"+ shell.session_id = "fixture-session"+ shell._last_turn_result = None+ shell._claim_active_session = lambda *a, **k: True+ shell._show_security_advisories = lambda: None+ shell._print_exit_summary = lambda **k: None+ shell.console = SimpleNamespace(print=lambda *a, **k: None)+ from hermes_cli.cli_chat_turn_mixin import CLIChatTurnMixin+ shell._secret_capture_callback = lambda *a, **k: None+ shell.chat = CLIChatTurnMixin.chat.__get__(shell)+ 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)+ with pytest.raises(SystemExit) as exc:+ cli._run_single_query_mode(shell, "fixture", None, quiet, True)+ assert exc.value.code == (worker_exit if worker else 1)++[email protected]("result,expected", [+ ({"completed": True}, 0),+ ({"interrupted": True}, 130),+ ({"failed": True, "failure_reason": "unknown"}, 1),+])+def test_turn_result_takes_precedence_over_startup_flags(monkeypatch, result, expected):+ monkeypatch.setenv("HERMES_KANBAN_TASK", "t_fixture")+ assert cli._single_query_exit_code(+ result, credentials_terminal=True, credentials_rate_limited=True) == expected