Skip to content

fix(cli): exit non-zero when gaia chat --query or gaia stats fails - #4612

Merged
kovtcharov-amd merged 3 commits into
amd:mainfrom
kovtcharov:kalin/cli-exit-codes
Oct 4, 2026
Merged

kovtcharov-amd merged 3 commits into
amd:mainfrom
kovtcharov:kalin/cli-exit-codes

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

A failed gaia chat --query exited 0 and printed a stray 1 on stdout, so gaia chat -q "..." && next-step ran next-step after the agent failed. gaia stats hid every backend error (for example, an unreachable Lemonade) behind "No stats available". Now a failed query exits 1 (130 on Ctrl+C) with nothing extra on stdout. A stats error prints its cause and exits 1. "No stats yet" is a normal empty state that exits 0.

This touches only the chat block's two except handlers, so it merges cleanly in either order with #4520.

Test plan

  • pytest tests/unit/cli/test_cli_exit_codes.py: 8 tests run the real gaia.cli.main entry point. 6 fail on main and pass on this branch.
  • pytest tests/unit/cli tests/unit/test_cli*.py with GAIA_HOME and GAIA_DAEMON_HOME pointed at fresh temp dirs: 702 passed. The 4 test_gaia_binary_on_path cases pass once the venv's bin is on PATH.
  • black, isort, and pylint pass on cli.py (both --enable=E,W0201 and the repo .pylintrc set).
  • Manual: with Lemonade stopped, run gaia stats; echo $?. It should print the connection error and 1.

A failed `gaia chat --query` exited 0 and printed a stray `1` to stdout, so
`gaia chat -q ... && next-step` ran next-step after a failure. main() now
treats an int returned by an action as the exit status instead of output,
and the chat path's exception and Ctrl+C handlers return 1 and 130.

`gaia stats` turned every backend error into "No stats available". The error
now reaches main(), which prints the cause on stderr and exits 1; an empty
result is reported as a normal "no stats yet" state with exit 0.
@github-actions github-actions Bot added documentation Documentation changes cli CLI changes tests Test changes labels Oct 4, 2026
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

Scripts can now trust gaia chat --query and gaia stats. A failed query exits 1 (130 on Ctrl+C) instead of exiting 0 and printing a stray 1. A gaia stats backend error now shows the real cause and exits 1, while "no stats yet" is a normal empty state that exits 0. The fix is small, removes a swallowed-exception handler as the fail-loudly rule asks, and the new tests drive the real CLI entry point. Nothing blocks the merge.

One side effect isn't covered: gaia prompt --stats uses the same stats call. If the stats fetch fails after the answer has streamed, that command now exits 1 too. This fits the fail-loudly rule, but it should be a deliberate choice, not an accident (details below).

Real-world evidence

N/A: the evidence stage produced no evidence-bundle.md for this run. The CLI exit-code changes were reviewed statically and against the PR's own tests. The PR's manual check (gaia stats; echo $? with Lemonade stopped) is still unchecked, so no real CLI run is shown yet. Adding that output to the description would close the gap.

🔍 Technical details

Issues

🟢 gaia prompt --stats now fails the whole command when only the stats fetch fails (src/gaia/cli.py:575-578)

  • GaiaCliClient.get_stats() no longer catches exceptions, and the prompt action calls it after the response has already streamed to stdout.
  • Before, a /stats failure dropped the stats silently and exited 0. Now it hits the new main() handler: ❌ Error: gaia prompt failed: …, exit 1.
  • This is defensible under fail-loudly, but it is untested and not mentioned in docs/reference/cli.mdx. Either add a one-line doc note and a test like test_stats_backend_error_exits_non_zero_with_the_cause for prompt --stats, or say in the PR body that it is intended.

🟢 Docs don't mention the new stderr hint (src/gaia/cli.py:4358)

  • The main() error path now writes the error and a --logging-level DEBUG hint to stderr; before, the error went to stdout.
  • That's the right move. --logging-level exists (cli.py:1156), so the hint is accurate.
  • Optional: one clause in the gaia stats doc paragraph saying the cause goes to stderr.

Strengths

  • The isinstance(result, int) → sys.exit(result) split in main() fixes the root cause in one place. The success path already returned 0, which is falsy, so only the failure 1 was ever printed, and this removes it cleanly.
  • The tests run gaia.cli.main with stubbed agent wheels rather than calling async_main directly, so they assert the actual process exit status. That follows the repo's "test the CLI users run" rule. The console_logging fixture also restores global log handlers, which avoids test pollution.
  • Deleting except Exception: return None in get_stats removes a pre-existing fail-loudly violation instead of working around it.

Kalin Ovtcharov and others added 2 commits October 4, 2026 02:40
The answer has already printed by then; failing loudly is deliberate. The CLI reference now says so, and that gaia stats errors go to stderr.
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 4, 2026
Merged via the queue into amd:main with commit c19e7df Oct 4, 2026
@kovtcharov-amd
kovtcharov-amd deleted the kalin/cli-exit-codes branch October 4, 2026 16:14
kovtcharov added a commit to kovtcharov/gaia that referenced this pull request Oct 4, 2026
…4675)

Main's unit test lanes are red:
`test_chat_query_success_exits_zero_without_printing_status` fails on
Linux, macOS and Windows. amd#4520 made `gaia chat` call
`agent._ensure_tool_loader_reset()` directly (no fallback), and amd#4612
added this test with a stub agent that lacks the method. Each passed on
its own; main broke when both landed. The stub now has the method, as
the other chat-path stubs already do.

## Test plan
- [x] `pytest tests/unit/cli/test_cli_exit_codes.py` passes
- [ ] `Unit Tests` green on main after merge

Co-authored-by: Kalin Ovtcharov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI changes documentation Documentation changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants