Repository navigation
fix(host-service): classify missing chat credentials as PRECONDITION_FAILED instead of 500 - #6317
Conversation
…FAILED instead of 500
📝 WalkthroughWalkthroughThe chat runtime now throws a typed error when model provider credentials are unavailable. Chat TRPC procedures map this error to ChangesChat error handling
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/host-service/src/trpc/router/chat/chat.ts (1)
11-26: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd focused tests for the shared mapper.
Cover
NoModelProviderCredentialsErrormapping and unrelated error propagation. AssertPRECONDITION_FAILED, the original message, andcause.kind === "NO_MODEL_PROVIDER_CREDENTIALS".The PR objective states that no tests were run for these changed files.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/host-service/src/trpc/router/chat/chat.ts` around lines 11 - 26, Add focused tests for withRuntimeErrorMapping covering NoModelProviderCredentialsError mapping and propagation of unrelated errors. Assert the mapped error has code PRECONDITION_FAILED, preserves the original message, and sets cause.kind to NO_MODEL_PROVIDER_CREDENTIALS; verify unrelated errors are rethrown unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/host-service/src/trpc/router/chat/chat.ts`:
- Around line 11-26: Add focused tests for withRuntimeErrorMapping covering
NoModelProviderCredentialsError mapping and propagation of unrelated errors.
Assert the mapped error has code PRECONDITION_FAILED, preserves the original
message, and sets cause.kind to NO_MODEL_PROVIDER_CREDENTIALS; verify unrelated
errors are rethrown unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 10935929-c7b3-4d0d-b60a-eaddc39af3b3
📒 Files selected for processing (2)
packages/host-service/src/runtime/chat/chat.tspackages/host-service/src/trpc/router/chat/chat.ts
🧹 Preview Cleanup CompleteThe following preview resources have been cleaned up:
Thank you for your contribution! 🎉 |
There was a problem hiding this comment.
No issues found across 2 files
Architecture diagram
sequenceDiagram
participant Client as Chat Client
participant Router as TRPC Chat Router
participant ErrorMap as withRuntimeErrorMapping()
participant Runtime as ChatRuntimeManager
participant CredCtrl as ModelProviderRuntimeResolver
participant Sentry as Sentry Middleware
Note over Client,Sentry: Chat Runtime Initialization Flow
Client->>Router: Any chat procedure (getSnapshot, sendMessage, etc.)
Router->>ErrorMap: Wrap call in error mapping
ErrorMap->>Runtime: createRuntime(workspaceId)
Runtime->>CredCtrl: hasUsableRuntimeEnv()
alt No credentials available (expected user state)
CredCtrl-->>Runtime: false
Runtime->>Runtime: throw NoModelProviderCredentialsError("No model provider credentials available")
Runtime-->>ErrorMap: Exception propagates
ErrorMap->>ErrorMap: Check error.name == "NoModelProviderCredentialsError"
ErrorMap->>Router: throw TRPCError(PRECONDITION_FAILED, cause: { kind: "NO_MODEL_PROVIDER_CREDENTIALS" })
Router-->>Client: PRECONDITION_FAILED (not reported to Sentry)
else Credentials available
CredCtrl-->>Runtime: true
Runtime->>Runtime: Boot workspace, resolve runtime
Runtime-->>ErrorMap: RuntimeSession
alt Other unexpected failure (workspace lookup, runtime boot)
Runtime->>Runtime: throw Error(...)
Runtime-->>ErrorMap: Exception propagates
ErrorMap->>ErrorMap: error.name != "NoModelProviderCredentialsError"
ErrorMap-->>Router: rethrow original error
Router-->>Sentry: unclassified 500 error
Router-->>Client: INTERNAL_SERVER_ERROR
else Success
ErrorMap-->>Router: RuntimeSession
Router-->>Client: Normal response
end
end
Note over Client,Sentry: Affected procedures (9 total):<br/>getDisplayState, listMessages, getSnapshot,<br/>sendMessage, restartFromMessage, stop,<br/>respondToApproval, respondToQuestion, respondToPlan
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.createRuntimeguards the expected no-credentials precondition by throwing a plainError("No model provider credentials available"). Every chat router procedure that lazily creates a runtime (chat.getSnapshotin 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 namedNoModelProviderCredentialsError(name-checkable plain error class in the owning package).trpc/router/chat/chat.ts: one shared boundary helper (withRuntimeErrorMapping) name-checks it and maps toTRPCErrorPRECONDITION_FAILEDwith cause{ kind: "NO_MODEL_PROVIDER_CREDENTIALS" }, message preserved verbatim. All nine procedures that reachgetOrCreateRuntime(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:cd packages/host-service && bun run typecheck:(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.
NoModelProviderCredentialsErrorin the chat runtime and throw it when no provider credentials are available.withRuntimeErrorMappingin the chat TRPC router to map the named error toTRPCErrorPRECONDITION_FAILEDwith cause{ kind: "NO_MODEL_PROVIDER_CREDENTIALS" }(message preserved). Uses@trpc/server.Written for commit 87127ff. Summary will update on new commits.
Summary by CodeRabbit