fix(chat): eager pending AssistantMessage to fix Turbo subscribe race (#1657)

Merged
TA
6mo ago
+89-90

Fixes #1657.

Problem

UserMessage.after_create_commit enqueues AssistantResponseJob immediately during ChatsController#create. With a fast LLM (gpt-4o-mini ~1–2s) the worker's broadcast_append_to chat for the new AssistantMessage can fire before the client's turbo_stream_from @chat subscription is established. The broadcast has no replay buffer, so the message lands in the DB but never reaches the page — the UI stays on the thinking indicator forever. A page refresh reveals the answer.

Fix

Persist the AssistantMessage synchronously as status: :pending inside Chat#ask_assistant_later. By the time chats#show renders, the row already exists and is included in the server-rendered conversation_messages loop, so the DOM has the placeholder before the client even connects. The worker then finds the existing pending row and updates it via append_text!, which transitions status pending → complete and broadcasts updates against a DOM id that is already on the page — race window closed.

  • Message: validates :content, presence: true, unless: :pending? so the empty placeholder validates.
  • AssistantMessage#append_text!: flips pending → complete on first content.
  • Assistant::Builtin#respond_to and Assistant::External#respond_to: pick up the pending message instead of AssistantMessage.new. On error, destroy if blank, otherwise mark failed.
  • Assistant::Responder#conversation_history already filters by status: "complete" so pending placeholders never leak into prompts. Assistant::External#build_conversation_messages filters the same way.
  • The pending assistant_message partial renders an inline "Thinking ..." indicator, replacing the standalone chats/_thinking_indicator.html.erb and the Assistant::Broadcastable helpers (both removed).
  • ?thinking=true redirects from chats#create, chats#retry, and messages#create are no longer needed.

Why this approach over a delay

A set(wait: 2.seconds) would also paper over the race but is brittle (slow connections defeat it) and hacky. Persisting the pending state is the architecturally correct answer: the response is genuinely pending and the existing pending enum was clearly designed for it. Net diff is -32 lines after removing the now-redundant thinking indicator path.

Tests

Updated test/models/assistant_test.rb count assertions to reflect the new flow:

  • Error paths: pending placeholder is destroyed → assert_difference "AssistantMessage.count", -1.
  • Success paths: pending placeholder is updated in place → assert_no_difference.

Existing assertions on content, ai_model, status, tool_calls, and conversation history all still hold. The "config not configured" test still passes assert_no_difference because the early raise happens before the pending placeholder is touched. user_message_test and assistant_message_test are unaffected (the former mocks ask_assistant_later; the latter doesn't go through the user-message lifecycle).

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced assistant response handling with improved thinking state visibility
    • Multi-language support for thinking status across 9 languages
  • Bug Fixes

    • Improved error handling for failed assistant responses with better state management
    • Fixed validation logic for message content
  • UI/UX Improvements

    • Redesigned thinking indicator display with pulsing status indicator