Skip to content

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

Merged
saddlepaddle merged 1 commit into
mainfrom
sentry/hs-chat-no-credentials
Aug 13, 2026
Merged

saddlepaddle merged 1 commit into
mainfrom
sentry/hs-chat-no-credentials

Conversation

@Kitenite

@Kitenite Kitenite commented Aug 10, 2026 •

Copy link
Copy Markdown
Member

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.

Written for commit 87127ff. Summary will update on new commits.

Review in cubic

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.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The chat runtime now throws a typed error when model provider credentials are unavailable. Chat TRPC procedures map this error to PRECONDITION_FAILED while preserving other errors.

Changes

Chat error handling

Layer / File(s) Summary
Typed runtime credential error
packages/host-service/src/runtime/chat/chat.ts
Adds NoModelProviderCredentialsError and uses it when runtime creation lacks usable model provider credentials.
TRPC error mapping
packages/host-service/src/trpc/router/chat/chat.ts
Adds shared error mapping and applies it to chat display, message, snapshot, send, restart, stop, approval, question, and plan operations.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: saddlepaddle

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title uses conventional commit format and clearly describes the credential error classification change.
Description check ✅ Passed The description explains the problem, root cause, fix, affected procedures, verification, and test limitations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sentry/hs-chat-no-credentials

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/host-service/src/trpc/router/chat/chat.ts (1)

11-26: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add focused tests for the shared mapper.

Cover NoModelProviderCredentialsError mapping and unrelated error propagation. Assert PRECONDITION_FAILED, the original message, and cause.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

📥 Commits

Reviewing files that changed from the base of the PR and between 31fb0fc and 87127ff.

📒 Files selected for processing (2)
  • packages/host-service/src/runtime/chat/chat.ts
  • packages/host-service/src/trpc/router/chat/chat.ts

@github-actions

github-actions Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

🧹 Preview Cleanup Complete

The following preview resources have been cleaned up:

  • ✅ Neon database branch

Thank you for your contribution! 🎉

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Loading

Re-trigger cubic

@saddlepaddle
saddlepaddle merged commit e2d0c7b into main Aug 13, 2026
19 checks passed
@saddlepaddle
saddlepaddle deleted the sentry/hs-chat-no-credentials branch August 13, 2026 18:38

This branch was previously deployed

1 inactive deployment
preview — 87127ff3 Deployed Aug 10, 2026 by Kitenite via Deploy Web #7369
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.

2 participants