Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions reigner/cli/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ async def _run_print(session: Session, query: str, *, json_output: bool) -> int:
"""Drive one query in headless mode. Returns the process exit code."""
final: FinalAnswerEvent | None = None
saw_clarification = False
saw_error = False
error_text: str | None = None
async for event in session.run_stream(query):
if json_output:
print(to_json(event), flush=True)
Expand All @@ -245,7 +245,7 @@ async def _run_print(session: Session, query: str, *, json_output: bool) -> int:
elif isinstance(event, ClarificationEvent):
saw_clarification = True
elif isinstance(event, ErrorEvent) and not event.recoverable:
saw_error = True
error_text = event.error

if not json_output and final is not None:
print(final.text)
Expand All @@ -259,7 +259,9 @@ async def _run_print(session: Session, query: str, *, json_output: bool) -> int:
err=True,
)
return EXIT_USAGE
if saw_error:
if error_text is not None:
if not json_output:
typer.echo(f"error: {error_text}", err=True)
return EXIT_RUNTIME
return EXIT_RUNTIME

Expand Down
22 changes: 22 additions & 0 deletions tests/cli/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ def test_print_plain_outputs_final_answer_only(patch_build_session) -> None:
assert result.stdout.strip() == "the answer is 42"


def test_print_plain_reports_fatal_error(monkeypatch: pytest.MonkeyPatch) -> None:
from reigner.cli import chat as chat_module
from reigner.harness.events import ErrorEvent

class ErrorSession:
async def run_stream(self, query):
yield ErrorEvent(
seq=1,
session_id="test",
turn=1,
error="adapter: openai package not installed",
recoverable=False,
)

monkeypatch.setattr(chat_module, "_build_session", lambda _path: ErrorSession())
result = runner.invoke(app, ["chat", "--print", "anything"])

assert result.exit_code == 1
assert result.stdout == ""
assert result.stderr == "error: adapter: openai package not installed\n"


def test_print_json_emits_nd_json_event_stream(patch_build_session) -> None:
from tests.cli.conftest import _final

Expand Down
Loading