Repository navigation
🚦 feat: Drive a Followed Chat With Main Chat's ask - #18413
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
💡 Codex ReviewLibreChat/packages/chat/src/core/turns/prepare.ts Lines 90 to 92 in 787fb15 When the host reuses its conversation object across turns, this call permanently replaces any When a host uses this adapter for ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a6104625da
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (finished?.isSettled === true) { | ||
| await settle(); |
There was a problem hiding this comment.
Block new sends until the settled-message reload finishes
When a start returns status: 'settled', conclude clears driving before awaiting settle(). That reload sets neither attaching nor another busy flag, so canSend() returns true while loadMessages() is still pending. A second turn can therefore be built against unreconciled history, and when the earlier reload resolves, read() unconditionally calls chat.setMessages(messages), overwriting the second turn's optimistic rows or streamed response. Keep sends blocked throughout reconciliation, including reloads triggered by live turning false.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid, fixed in a89b42d. Every read of the stored rows is now counted while it is in flight, and canSend() is false until none is: the settled reload, the reload after live turns false, and the one after a followed generation ends. driving also stays set until the turn's own completion has been written. Test: "takes no turn while the stored messages are being read back" (fails without the count).
| if (finished?.isSettled === true) { | ||
| await settle(); | ||
| } |
There was a problem hiding this comment.
Reconcile the optimistic placeholder after a rejected start
If the start POST rejects with a non-network error such as HTTP 400 or 403, Chat reports isError: true, isDisconnect: false, and isSettled: false, leaving the optimistic messages in place. This completion path does nothing for that outcome, so the empty assistant placeholder retains its trailing-underscore ID without timestamps or streamStartFailed metadata. Although the contract becomes idle again, every subsequent ordinary ask is rejected by useTurns' hasPendingAssistantParent guard until the conversation is reloaded. Explicitly reconcile or mark the failed turn so the user can send again.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid, fixed in a89b42d. A turn that fails for good now leaves the rows main chat's error handler writes, built with the same functions (resolveErrorTurn + mergeErrorMessages): a refused start becomes the reply to its message with streamStartFailed metadata and the server's text, a failure the stream reported keeps what streamed. hasPendingAssistantParent then lets the next turn through. Test: "leaves a refused start as the reply to its message, and takes the next turn" (fails without it).
| export function prepareSubmittedTurn({ submission, messages }: SubmittedTurn): ChatPreparedRequest { | ||
| return { | ||
| payload: createPayload(submission).payload, | ||
| messages, | ||
| responseMessageId: submission.initialResponse?.messageId, |
There was a problem hiding this comment.
Preserve saved branches when completing a regeneration
When regenerating a saved response, buildTurn deliberately removes that response and its descendants from the optimistic messages, retaining the complete history in submission.regenerateMessages. This adapter passes only the reduced list into Chat, and the event applier's final handler merely merges the new response into that list. Because a successful driven turn does not reload history, the original response and its descendant branch remain missing from the cache after regeneration. Main chat restores them with mergeRegenerateFinalMessages in react/stream/events/final.ts; preserve and merge the same baseline here so previous answers remain accessible without reopening the conversation.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid, fixed in a89b42d. When a rerun ends on its own stream, the list is rebuilt from submission.regenerateMessages with mergeRegenerateFinalMessages, as react/stream/events/final.ts does; a rerun stopped before any reply gets its baseline back without the placeholder. Test: "gives a rerun back the branches it set aside once its reply is in" (fails without it, and asserts nothing is re-read).
| const stopGenerating = useCallback(async () => { | ||
| await chat?.stop(); |
There was a problem hiding this comment.
Supply the endpoint before stopping an externally started run
When this contract opens a conversation whose generation was started elsewhere, the Stop button calls chat.stop(), but the Chat constructed by useFollowChat has no resumeSelection callback. For an attached generation, Chat.reattach obtains its endpoint from that callback, and incoming frames never populate it. Consequently, Chat.stop throws The generation cannot be stopped without its endpoint before calling the abort transport, leaving the generation running. Pass the conversation's endpoint into the followed chat's resume selection so stopping works for externally started runs as well as locally submitted ones.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid, fixed in a89b42d. useFollowChat takes the conversation's endpoint and answers the Chat's resumeSelection with it, so a stop of an attached generation is addressed. Test: "stops a generation it did not start, on the endpoint the conversation runs on" (fails without it: the stop rejected before reaching the transport).
a89b42d to
36af1a5
Compare
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 36af1a5781
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| : rerunTurnMessages(turn, finished?.message); | ||
| if (completed != null) { | ||
| await chat.setMessages(completed); |
There was a problem hiding this comment.
Preserve the replacement turn when settling a regeneration
If another client starts a newer turn before this regeneration reaches the server, the start can return status: 'replaced'. Chat.request then rolls back this turn's optimistic changes and follows the replacement stream, whose final frame adds its actual request and response. This completion path nevertheless calls rerunTurnMessages with the original regeneration's snapshot, replacing the reconciled history with that old snapshot plus only the replacement response. The replacement's user message disappears, leaving its answer orphaned until the conversation is reopened. Distinguish a replacement from this turn's own completion and preserve or reload its authoritative history instead of applying the original rerun baseline.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid, fixed in b9efb23. The hook now records what the server answered the start with. A turn answered with replaced, or folded into a generation already running, is not this turn's to complete: when it ends the stored rows are read, and neither the rerun baseline nor this turn's error rows are applied. Test: "reads the conversation back when the server answered a rerun with a newer generation" (fails without the check: the replacement's user message is gone).
| const stopGenerating = useCallback(async () => { | ||
| await chat?.stop(); | ||
| }, [chat]); |
There was a problem hiding this comment.
Handle an unaddressable stop before reporting completion
When Stop is clicked after the start POST reaches the server but before its response arrives, Chat.stop() cancels the local request and returns { status: 'unaddressable' } without sending an abort. If that POST is cancelled without delivering a late response, the generation can continue running on the server. This wrapper discards the result, while useFollowChat.conclude treats the cancellation as a disconnect and attaches to the running generation again, so the user's Stop merely reconnects to it. Handle the unaddressable outcome explicitly and complete the stop once the intended generation can be identified, rather than silently resuming it.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid, fixed in b9efb23. useFollowChat now returns stop, which the contract's stopGenerating is. When Chat.stop() answers unaddressable it sends the abort by conversation (what main chat's stop does), then asks the status route and stops by stream id and epoch if a generation only appeared afterwards. A turn dropped by the reader's own stop is not attached to again; the stored rows are read once the stop has been answered. Tests: "stops a turn the server has not answered yet by its conversation, and does not follow it" and "stops by name a generation that appears only after the stop by conversation" (each fails with its piece removed).
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b9efb234d0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (!stale && own) { | ||
| const completed = | ||
| finished?.isError === true | ||
| ? failedTurnMessages(turn, chat.messages, chat.error) | ||
| : rerunTurnMessages(turn, finished?.message); |
There was a problem hiding this comment.
Reconcile early-aborted turns before allowing another send
When a driven turn is stopped after its start response arrives but before the server emits created, GenerationJobManager sends a final frame with earlyAbort: true and responseMessage: null. Chat reports this as an abort, not a disconnect or settled result, so this path treats it as an ordinary completion. For a non-regeneration, rerunTurnMessages returns nothing, leaving the empty optimistic assistant placeholder in the cache. Its underscore-suffixed ID and missing timestamps then cause useTurns' hasPendingAssistantParent guard to reject subsequent sends until the conversation is reloaded. Handle the early-abort frame by restoring the pre-turn rows or reloading stored history, as main chat's final handler does.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid, fixed in 1906374. An earlyAbort final frame now restores the rows from before the turn with getExistingConversationAbortMessages, as react/stream/events/final.ts does, and useFollowChat({ onWithdrawn }) reports the turn so a surface can offer the words back (main chat saves them as a draft). I also closed the class rather than the one frame: whatever way a turn sent from here ends, if its empty reply is still in the list as one on its way (hasPendingAssistantParent), the stored rows are read instead of leaving it. Tests: "takes back a turn stopped before the server kept anything of it" and "reads the conversation back when a turn ends with its reply still unanswered" (each fails with its piece removed).
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review Head 2af6280. One commit past 1906374: found while testing the surface on top of this, a turn that ends with |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
2af6280 to
356bd7f
Compare
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
A
Chatin@librechat/chatcan now start a generation over LibreChat's routes (#18411), but nothing builds the turn it starts, and no chat contract calls it. A conversation shown throughuseFollowChat(a subagent's thread today) can only be watched: its contract'sask,regenerate,handleContinueandstopGeneratingdo nothing.This adds the driving half. A conversation followed through
useFollowChatcan be sent a turn, anduseDrivenContractbuilds the chat contract the composer and the message rows already read, over that conversation.The turn is not rebuilt for the
Chat. Main chat'sask(useTurns) runs unchanged: it refuses, drains the composer's files, skills, quotes and reasoning, and builds the sameTSubmission. Only what happens to that submission differs. In a pane it is published for the stream session; here theChattakes it. So an edit, a rerun, a continuation or a send with attachments is the same request whichever chat sent it.Fourth step toward a chat a reader can open and drive beside the main one; stacked on #18411. No user-facing change: nothing mounts a driven contract yet. The surface that does is next.
How it works
useChatHelpers)useDrivenContract)useChatHost()ask,regenerate,handleRegenerate,handleContinueuseTurns+useTurnHandlersChat'ssubmissionByIndex, read by the stream sessionfollowed.senduseStopGeneratingfollowed.stop()isSubmittingChat's status; the pane's flags follow ituseTurnHandlersis the stableask/regeneratewrappers and the regenerate and continue handlers, lifted out ofuseChatHelpersso both contracts share them; the pane's contract is otherwise untouched and its members keep the identities they had.canSendexists because a render can be a commit behind the chat: when a stream drops, the chat reads as stopped for a moment before the page shows it reconnecting.askasks the chat at that instant, before it clears the composer for a turn that would not be taken.This replaces the first commit's
prepareTurn, which mapped aChat's three requests back onto the turn builder. Withaskas the one place a turn is built, nothing would have called it.Type of change
Testing
Tested environments/configuration:
Hook level, with a real
Chat, the realuseFollowChat,useTurnsand event applier, and a transport the test drives. Not exercised in the app: nothing mounts a driven contract yet, and the app's transport is not givenstartandabortuntil the surface lands.Automated tests:
packages/chat/src/react/chat/__tests__/driven.spec.tsx(16): a submitted turn's posted body and shown rows, the generation it starts applied to the same rows, and no re-read after its own final frame; a regeneration and a continuation; a stop reaching the transport with the generation's stream id and epoch; a dropped stream attached to again, never sendable in between, then the stored result; a start the server had already run read back; a turn refused while a generation it did not start is running, composer files left staged; a turn refused when the chat says no though the render says yes. Also: no turn taken while the stored rows are being read back; a refused start (403) left as the reply to its message with the server's text, and the next turn taken; a rerun's set-aside branches restored without a re-read; a stop of a generation the chat did not start reaching the transport with the conversation's endpoint. And: a rerun the server answered with a newer generation read back whole instead of completed from its own snapshot; a stop before the start is answered sent by conversation and not followed; a generation that appears only afterwards stopped by name; an early-abort frame restoring the rows from before the turn and reporting it; a turn that ends with its reply still unanswered read back.packages/chat/src/core/__tests__/turns.spec.ts(3): the prepared body equals whatcreatePayloadgives main chat's start for the same submission; the request each kind of turn is.canSendcheck, the in-flight guard, the reattach, the settled read, the pane flags, the followed state inisSubmitting, the held rows, the read count, the error rows, the rerun merge, the endpoint, the foreign-generation check, the stopped-turn check, the abort by conversation, the abort by name, the withdrawn rows, the unanswered-reply rule); each fails a test.packages/chat/src/core/__tests__/apply.spec.ts(2): a turn that ends withcreatedthenfinaland no step settles onto the reply it was drawn with instead of beside it (fails without the applier change); a regeneration's saved reply is left where it is.packages/chat:npx jest(3977 tests),npx tsc --noEmit.npm run static-checks -- --against origin/dev.Risk / compatibility
useChatHelperschanges by extraction only. The event applier's final frame now replaces an unfilled placeholder, which only a turn sent from this chat ever holds.useFollowChattakes an optionalendpoint, andFollowedChatgains three members (canSend,send,stop); a followed conversation whose transport cannot send (every one in the app today) is never asked to. No render benchmark was run: no row or message component changes, and the pane contract's members are memoized on the same inputs as before.Not handled yet, for the surface PR: a driven chat for a conversation that does not exist yet (
askwould route the page to the new-chat path), and the queue and steering paths, which reachaskfrom outside the contract.