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

* fix(chat): persist eager pending assistant message to fix subscribe race

When the LLM replies in ~1-2s the assistant message broadcast could
fire before the client's Turbo stream subscription was established,
leaving the UI stuck on the thinking indicator while the response was
already persisted.

Create the AssistantMessage as `pending` synchronously in
`Chat#ask_assistant_later`, so it is rendered server-side on the chat
show page with a "Thinking ..." inline placeholder. The worker then
finds and updates the existing row via `append_text!`, which flips the
status to `complete` and broadcasts updates against a DOM id that is
already in the page — no race possible. On error, the placeholder is
destroyed if no content streamed, otherwise demoted to `failed`.

Replaces the standalone thinking indicator partial and the
`Assistant::Broadcastable` thinking helpers, both now redundant.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(chat): bind each assistant job to its specific pending placeholder

Addressing review feedback on #1658:

1. The pending placeholder lookup based on `last pending` was racy —
   back-to-back user messages would let one job fill another job's
   placeholder. Pass the placeholder through the job arguments
   (`AssistantResponseJob.perform_later(user_message, pending)`) so
   each turn is bound to its own row.

2. In `Assistant::External#respond_to`, the configured/authorized
   guards raise before the local was bound, leaving rescue cleanup
   with `nil` and the placeholder visible forever. Bind the parameter
   first so cleanup can destroy it on the misconfigured path.

The kwarg defaults to nil so the API#retry path
(`AssistantResponseJob.perform_later(new_message)`) and the model-level
test calls continue to work — they fall back to an in-memory new
message, restoring the original test count assertions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(chat): i18n the pending assistant placeholder string

Move the hardcoded "Thinking ..." indicator into the locale file per
CLAUDE.md i18n guidelines. With i18n.fallbacks enabled, non-en locales
fall back to English until translated.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Add thinking label translations

* Fix chat pending assistant expectations

* Fix external assistant pending test lookup

* Scope chat stream targets per chat

* Update message broadcast target tests

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Michal Tajchert
2026-05-03 20:33:29 +02:00
committed by GitHub
parent 50936000e7
commit ccd6a53071
28 changed files with 91 additions and 92 deletions

View File

@@ -79,7 +79,7 @@ class Chat < ApplicationRecord
def add_error(e)
update!(error: build_error_payload(e).to_json)
broadcast_append target: "messages", partial: "chats/error", locals: { chat: self }
broadcast_append target: messages_target, partial: "chats/error", locals: { chat: self }
end
def presentable_error_message
@@ -93,20 +93,29 @@ class Chat < ApplicationRecord
def clear_error
update! error: nil
broadcast_remove target: "chat-error"
broadcast_remove target: error_target
end
def conversation_messages
messages.where(type: [ "UserMessage", "AssistantMessage" ])
end
def ask_assistant_later(message)
clear_error
AssistantResponseJob.perform_later(message)
def messages_target
ActionView::RecordIdentifier.dom_id(self, :messages)
end
def ask_assistant(message)
assistant.respond_to(message)
def error_target
ActionView::RecordIdentifier.dom_id(self, :chat_error)
end
def ask_assistant_later(message)
clear_error
pending = messages.create!(type: "AssistantMessage", content: "", ai_model: message.ai_model, status: :pending)
AssistantResponseJob.perform_later(message, pending)
end
def ask_assistant(message, assistant_message: nil)
assistant.respond_to(message, assistant_message: assistant_message)
end
private