Skip to content

📊 feat: Feed a Side Chat's Usage to Its Composer's Gauge - #18488

Draft
danny-avila wants to merge 3 commits into
danny-avila/side-chat-composerfrom
danny-avila/side-chat-live-usage
Draft

danny-avila wants to merge 3 commits into
danny-avila/side-chat-composerfrom
danny-avila/side-chat-live-usage

Conversation

@danny-avila

@danny-avila danny-avila commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The gauge in a side chat's composer does not count a reply while it is written. It keeps the count it had when the turn was sent and jumps when the reply ends.

Two things were missing. One is main chat's too and is fixed in #18487: the gauge lost track of the reply as soon as its row was renamed. The other is the side chat's own: nothing handed the usage frames of a conversation driven through a core Chat to the writers the gauge reads. The event applier already calls those writers at the points main chat's stream session does (the context reading as a reply starts, the output arriving, the totals at its end, a resumed run's snapshot); the app's side chat host passed it none.

This change passes it main chat's own writers, so the side composer's gauge counts a reply as it arrives, the same way and from the same frames.

Stacked on #18469 and depends on #18487, whose commit is included here until it merges into dev. Without #18487 the side gauge would keep the right state and still show it frozen, as main chat's does today.

How it works

+  const usageHandlers = useUsageHandler();
+  const usage = useMemo(() => toResumableUsage(usageHandlers), [usageHandlers]);
   const beside = useMemo(
-    () => ({ onAttachment, onSettled, announce }),
+    () => ({ onAttachment, onSettled, announce, usage }),

toResumableUsage is the mapping from the usage writers to the shape a stream's applier calls, which main chat's stream session built inline. It now lives beside useUsageHandler in @librechat/chat and both use it, so the two cannot drift.

The live estimate and the usage counted for a reply are cleared (resetLive) where main chat clears them, on the paths that end without a final frame: when the side chat reads its stored rows and no reply is on its way, and when a turn sent from it fails or is stopped before the server kept anything of it. Without the second, a failed reply's count would be added to the conversation's next one.

Type of change

  • Feature

Testing

In a real browser on the mock e2e lane: send a slow reply in a side chat and read the gauge at chunk 20 and chunk 100 of 160, with the Stop control still on screen for both. With #18487 but without this change the two readings are equal (Expected: > 43, Received: 43); with it the second is higher.

An earlier scenario for this checked the gauge's breakdown after a turn. It passed with and without the change, because that breakdown is rebuilt from what the messages store, so I replaced it with the one above.

Tested environments/configuration:

  • Chromium via Playwright, mock e2e lane (in-memory MongoDB, fake model), desktop 1440×900.

Automated tests:

  • packages/chat: npx jest (250 suites, 4029 tests), tsc --noEmit.
  • packages/chat: driven.spec.tsx › "drops what was counted for a reply that fails, and keeps it for one that ends" (real usage writers; fails without the reset with the failed reply's tokens still pending).
  • client: new case in src/Providers/__tests__/SideChatHostContext.spec.tsx (a context reading on the side conversation's stream lands in that conversation's usage state); npx jest src/hooks/SSE src/Providers (30 suites, 719 tests).
  • New scenario @scenario:side-chat-composer-gauge-follows-the-reply; the composer spec (11 scenarios) and gauge-follows-reply.spec.ts pass together.

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

Head: f63edcf

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T04:04:40.172129Z 8c883b3 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f63edcfdc6

ℹ️ 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".

Comment on lines 105 to +107
if (final == null) {
/** No reply is on its way in what was just read, so no estimate of one is kept. */
usageHandlers.resetLive({ conversation: { conversationId: id } });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Clear side-chat usage on terminal paths without a final frame

When a turn sent from the side composer receives a terminal error frame after streaming output or token usage, this reset is never reached: createEventApplier only flushes message writes for errors, and useFollowChat's conclude() does not call onSettled for an owned turn without a final frame. The newly connected writers therefore leave the active response, live counters, and pending usage populated. useTokenUsage continues adding that pending usage to totals, and the next turn inherits it because neither bindResponse nor contextHandler clears pending usage. Wire terminal failure cleanup separately from history settlement, with a regression test covering an error after usage has arrived.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Valid, fixed in 8c883b3.

A turn sent from the side composer that ends with no final frame (an error frame, or a stop before the server kept anything of it) now drops what was counted for its reply: useFollowChat's conclude() calls usage.resetLive on that path, which toResumableUsage maps to the same resetLive main chat's session runs on its error frame (core/resumable/failure.ts). A turn that ends on its final frame is still settled by finalizeUsage alone.

Test: driven.spec.tsx › "drops what was counted for a reply that fails, and keeps it for one that ends" runs the real usage writers against a store, sends a turn, folds a token_usage frame, then an error frame, and reads the pending usage and the active response back. Without the call it fails with the 40 output tokens and one folded event still pending; with it, both are cleared, and a following turn that ends on its final frame never triggers the reset.

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review 8c883b3

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 8c883b31bf

ℹ️ 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".

This branch has not been deployed

No deployments
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.

1 participant