fix(chat): render turn errors into the transcript; read provider env keys - #483
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Two failure-mode fixes for the streaming chat (follow-up to the merged #482).
Changes
Turn errors render into the transcript (
streaming_tui.py):run_turnnow 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 withUnhandled exception in event loop+ a blockingPress ENTER. The user sees the error, fixes the env, and retries;/exitstill works.Provider env keys are read (
cothis/ai/base.py):get_providermaps 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-readsOPENAI_API_KEY, so a user withOPENROUTER_API_KEYset (the natural key for the defaultopenrouterprovider) gotMissing credentials.Problem fixed
uv run cothis chatwith a missing/mismatched API key froze instead of showing the error.openrouterprovider ignoredOPENROUTER_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,/exitafter the error lands — no unhandled event-loop exception).ruff check+ty checkclean.