fix(host-service): classify missing chat credentials as PRECONDITION_FAILED instead of 500

Merged
KI
3mo ago
+60-10

Problem

Opening the chat pane without any model provider credentials configured reports an INTERNAL_SERVER_ERROR to Sentry — once per chat open for every credential-less user (HOST-SERVICE-11, 5 events on 1.20.0).

Root cause

ChatRuntimeManager.createRuntime guards the expected no-credentials precondition by throwing a plain Error("No model provider credentials available"). Every chat router procedure that lazily creates a runtime (chat.getSnapshot in the events) surfaces it as an unclassified 500, which the Sentry middleware captures.

Fix

Classification only — guard and message are unchanged:

  • runtime/chat/chat.ts: the guard now throws a named NoModelProviderCredentialsError (name-checkable plain error class in the owning package).
  • trpc/router/chat/chat.ts: one shared boundary helper (withRuntimeErrorMapping) name-checks it and maps to TRPCError PRECONDITION_FAILED with cause { kind: "NO_MODEL_PROVIDER_CREDENTIALS" }, message preserved verbatim. All nine procedures that reach getOrCreateRuntime (getDisplayState, listMessages, getSnapshot, sendMessage, restartFromMessage, stop, respondToApproval, respondToQuestion, respondToPlan) go through it.

Genuine unexpected failures in createRuntime (workspace lookup, runtime boot) rethrow unchanged and keep reporting as 500s.

Verification

bunx biome check packages/host-service/src/runtime/chat/chat.ts packages/host-service/src/trpc/router/chat/chat.ts:

Checked 2 files in 13ms. No fixes applied.

cd packages/host-service && bun run typecheck:

$ tsc --noEmit --emitDeclarationOnly false

(clean exit, no diagnostics)

No existing tests cover the changed files, so no test run applied.

Refs HOST-SERVICE-11

https://claude.ai/code/session_26b85581-d9e3-4a25-9234-c1e277e606d1


Summary by cubic

Classifies missing chat model provider credentials as PRECONDITION_FAILED (not 500) so expected user state doesn’t get reported to Sentry. Addresses HOST-SERVICE-11 by aligning errors with the issue’s requirement.

  • Bug Fixes
    • Added NoModelProviderCredentialsError in the chat runtime and throw it when no provider credentials are available.
    • Introduced withRuntimeErrorMapping in the chat TRPC router to map the named error to TRPCError PRECONDITION_FAILED with cause { kind: "NO_MODEL_PROVIDER_CREDENTIALS" } (message preserved). Uses @trpc/server.
    • Applied the mapping to all chat procedures that create a runtime.
    • Other runtime failures still bubble up as 500s and are reported.

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling when model provider credentials are missing or unusable.
    • Chat operations now return a clear, structured precondition error instead of a generic failure.
    • Other unexpected errors continue to be reported without alteration.