Skip to content

fix(chat): render turn errors into the transcript; read provider env keys - #483

Merged
gemone merged 3 commits into
mainfrom
feat/chat-turn-error-env-keys
Aug 11, 2026
Merged

gemone merged 3 commits into
mainfrom
feat/chat-turn-error-env-keys

Conversation

@gemone

@gemone gemone commented Aug 10, 2026

Copy link
Copy Markdown
Owner

What

Two failure-mode fixes for the streaming chat (follow-up to the merged #482).

Changes

  1. Turn errors render into the transcript (streaming_tui.py): run_turn now catches generic exceptions (provider credentials, network, config) and renders them as [red]Error:[/red] … instead of letting them escape into the event loop — which froze the app with Unhandled exception in event loop + a blocking Press ENTER. The user sees the error, fixes the env, and retries; /exit still works.

  2. Provider env keys are read (cothis/ai/base.py): get_provider maps each provider's canonical API-key env var when no explicit key is passed (OPENROUTER_API_KEY, DEEPSEEK_API_KEY, GROQ_API_KEY, MISTRAL_API_KEY). The OpenAI SDK only auto-reads OPENAI_API_KEY, so a user with OPENROUTER_API_KEY set (the natural key for the default openrouter provider) got Missing credentials.

Problem fixed

  • uv run cothis chat with a missing/mismatched API key froze instead of showing the error.
  • The default openrouter provider ignored OPENROUTER_API_KEY, so a correctly-configured env still failed.

Tests

  • test_ai_factory.py: provider-env-key mapping (explicit key still wins over env).
  • test_streaming_tui.py: turn-error renders into the transcript and recovers (failing agent, /exit after the error lands — no unhandled event-loop exception).
  • 1676 tests green locally; ruff check + ty check clean.

gemone added 3 commits August 10, 2026 22:16
…keys

Two fixes for the streaming chat's failure modes:

1. run_turn now catches generic exceptions (provider credentials, network,
   config) and renders them into the transcript instead of letting them
   escape into the event loop — which froze the app with 'Unhandled
   exception in event loop' + a blocking 'Press ENTER'. The user sees the
   error, fixes the env, and retries; /exit still works.

2. get_provider maps each provider's canonical API-key env var when no
   explicit key is passed (OPENROUTER_API_KEY, DEEPSEEK_API_KEY,
   GROQ_API_KEY, MISTRAL_API_KEY). The OpenAI SDK only auto-reads
   OPENAI_API_KEY, so a user with OPENROUTER_API_KEY set (the natural key
   for the default openrouter provider) got 'Missing credentials'.

Tests: provider-env-key mapping (explicit key still wins) + turn-error
renders-into-transcript-and-recovers (failing agent, /exit after the
error lands). 1676 tests green, ruff + ty clean.
The factory returns AIProvider (a Protocol without _api_key); ty in CI
flags the attribute access. isinstance narrowing to OpenAIProvider makes
the private attr known to the checker.
…nscript

Two live-session fixes:

1. Ctrl-C now exits the app when idle (or on the second press while a
   turn runs): the first press interrupts the running turn ('press Ctrl-C
   again to exit'), and because the cancelled task's done() lags the
   cancellation by a loop iteration, an explicit _interrupted flag makes
   the second press exit immediately. Previously idle Ctrl-C was a no-op
   (C-c C-c could not quit).

2. cothis logger WARNING+ diagnostics (skills discovery, MCP handshake)
   are routed into the transcript as muted lines via a handler installed
   during run_streaming_chat — they used to print to stderr and corrupt
   the full-screen layout.

Tests: Ctrl-C idle-exits + running-interrupts-then-exits; a cothis
WARNING renders into the transcript. 1678 tests green, ruff + ty clean.
@gemone
gemone merged commit 820fa2a into main Aug 11, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant