Skip to content

fix: handle search errors and stale reconnect sessions#1096

Open
octo-patch wants to merge 1 commit intoItzCrazyKns:masterfrom
octo-patch:fix/issue-1006-stuck-brainstorming
Open

fix: handle search errors and stale reconnect sessions#1096
octo-patch wants to merge 1 commit intoItzCrazyKns:masterfrom
octo-patch:fix/issue-1006-stuck-brainstorming

Conversation

@octo-patch
Copy link
Copy Markdown

@octo-patch octo-patch commented Apr 8, 2026

Fixes #1006

Problem

When a network failure or any unhandled exception occurs during SearchAgent.searchAsync, the error is silently swallowed (the method is called without await in the route handler). This leaves the message in status: 'answering' in the database indefinitely. On reconnect, the frontend calls /api/reconnect/:backendId which returns 404 because the session is gone (especially after a server/container restart). Since the frontend didn't check res.ok, it tried to read the 404 response body as an SSE stream — nothing matched a known message type, so loading stayed true and the stuck "Brainstorming" state persisted, making all follow-up questions impossible.

Solution

Backend (src/lib/agents/search/index.ts)

  • Wrap the entire searchAsync body in a try/catch
  • On error: emit an error event to the session (so any active listener gets notified) and update the message status to 'error' in the database

Frontend (src/lib/hooks/useChat.tsx)

  • After fetching /api/reconnect/:id, check res.ok before attempting to read the stream
  • If the response is not OK (e.g. 404 session expired or server restarted), immediately set loading: false, clear the reconnect flag, and mark the message as 'error' so the user can retry

Testing

  1. Start a search query
  2. Kill the server mid-search (simulating network failure / container restart)
  3. Restart the server and reload the page
  4. Before fix: UI is stuck on "Brainstorming", no follow-up questions possible
  5. After fix: Message shows error state, user can send a new message

Summary by cubic

Prevent chats from getting stuck in “Brainstorming” by handling search errors and stale reconnect sessions (fixes #1006). On failure, messages now enter an “error” state and the UI stops loading so users can retry.

  • Bug Fixes
    • Backend: wrap searchAsync in try/catch; on error, emit error to the session and set the message status to error in the DB.
    • Frontend: on /api/reconnect/:id, check res.ok; on non-OK (e.g., 404), stop loading, clear reconnect, and mark the last message as error.

Written for commit b5e1d9f. Summary will update on new commits.

…yKns#1006)

- Wrap SearchAgent.searchAsync in try/catch: on failure, emit an error
  event to the session and update the message status to 'error' in the
  database. Previously, any exception left the message stuck in
  'answering' state forever.
- In useChat.tsx checkReconnect: check res.ok before reading the stream.
  When the backend session no longer exists (e.g. after a server restart),
  the reconnect API returns 404. The frontend now marks the message as
  'error' and exits loading state instead of hanging indefinitely.
Copy link
Copy Markdown
Contributor

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

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

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.

Stuck on 'Brainstorming', previously accumulated data unusable, since new queries are impossible.

1 participant