Skip to content

fix(web): persist chat composer drafts; sendMessage resolves acceptance - #23962

Merged
Git-on-my-level merged 6 commits into
mainfrom
lane/dlaudit-web-chat-draft
Oct 29, 2026
Merged

Git-on-my-level merged 6 commits into
mainfrom
lane/dlaudit-web-chat-draft

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

What changed and why

The web chat composers kept their draft in React component state only and cleared it before the send was acknowledged, so a failed or interrupted send left the typed text with no restorable copy after a refresh. The mobile app got the same fix in #23961; this is the web half, per-surface:

  • chatDraft.ts (new): one storage key per composer scope (omi.chat.draft.panel, omi.chat.draft.home), with SSR- and quota-safe read/write/clear helpers.
  • ChatPanel.tsx / ChatComposer.tsx: the draft is restored on mount, persisted as it changes, and cleared only after the send is accepted. The post-send write only replaces the persisted copy when it still holds this send's text — a newer draft typed while a send is running is never clobbered (the guard the fix(app): durable live-capture safety copies, chat draft persistence, startup WAL re-arm #23961 review asked for is applied here from the start), and an already-cleared copy of a failed send is restored.
  • useChat.ts: sendMessage now resolves false when the stream reported an error chunk or the transport failed (and for a superseded send whose catch path returns early), so callers can distinguish accepted sends. Existing behavior is unchanged otherwise.

Review round (this push)

Two round-1 findings, each fixed with a test:

  1. P1 — cross-account draft exposure. The storage keys were shared across accounts and never cleared on sign-out, so one account's unsent prompt could restore — and be sent — from another account's composer on the same browser. Drafts are now scoped to the signed-in uid (setChatDraftOwner registered by the auth provider), and an account switch or sign-out clears the previous owner's drafts.
  2. P2 — reopening the panel did not restore a failed send's draft. The panel stays mounted while closed, so the mount-time initializer never reran. The panel now restores an empty composer from the persisted draft on each open transition.

Round 2 (delta) found one further P1 in that scoping, fixed:

  1. P1 — a send that outlives its account could write under the new owner's key. A pending handleSend continuation (send started under account A, account switched before the stream failed) could persist A's prompt under B's draft key. Both composers capture the draft owner at send start (chatDraftOwner()) and drop the post-send draft write when the owner changed mid-send. Covered by the new outlives-its-account test.

How it was verified

  • bunx vitest run on the touched suites: 3 new useChat.sendMessage acceptance tests (done-chunk true; error-chunk and transport-failure false; nothing-to-send false) and 7 chat-draft tests (restore on open, persist-as-typed + clear on accepted send, keep restorable on failed send, newer draft during a running send is never clobbered, reopen-without-refresh restores a failed draft, one account's draft never restores into another account's, a send that outlives its account writes nothing under the new owner) — all green; the existing ChatPanel.evidence, ChatComposer, HomePage and AuthProvider suites stay green (36 tests across six files).

Failure class (fixes)

Failure-Class: new

Failure-class transition narrative

Violated contract: a composer that clears its input before the send is acknowledged holds no restorable copy of what the user typed. The same contract covers the send verdict: a caller that cannot observe whether the stream failed cannot keep restorable state for a retry.

Canonical guard: chatDraft.ts persists each composer scope's draft on change and restores it on mount; useChat.sendMessage resolves false on an error chunk or transport failure; both composers clear the persisted copy only on acceptance and only when it still holds this send's text (synchronous read/write pair, no debounce race). Guard artifacts: web/app/src/components/chat/chatDraft.ts, ChatPanel.tsx, ChatComposer.tsx, web/app/src/hooks/useChat.ts, .github/failure-classes/FC-web-draft-cleared-before-acknowledgement.json and the tests above.

Evidence: this PR is the evidence; evidence_prs is [].

UI evidence

UI-Evidence: none -- reason: no visual surface changes; both composers render identically and only draft persistence changes.

The chat composers (panel and home ask bar) kept their draft in component
state only and cleared it before the send was acknowledged, so a failed or
interrupted send left the typed text with no restorable copy. Each composer
now persists its draft under its own storage key on every change, restores it
on mount, and clears the persisted copy only after the send is accepted —
useChat.sendMessage now resolves false when the stream reported an error chunk
or the transport failed. The post-send write only replaces the persisted copy
when it still holds this send's text, so a newer draft typed while a send is
running is never clobbered.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T10:11:39.625309Z c36a690 New commits
ℹ️ 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.

P1: the draft keys were shared across accounts and never cleared on
sign-out, so one account's unsent prompt could restore — and be sent — from
another account's composer on the same browser. Drafts are now scoped to the
signed-in uid (the auth provider registers the owner), and an account switch
or sign-out clears the previous owner's drafts.

P2: the panel stays mounted while closed, so the mount-time draft initializer
never reran and a failed send's draft stayed hidden until a page refresh. The
panel now restores an empty composer from the persisted draft on each open.

@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: 95cf2a2314

ℹ️ 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 +92 to +93
} else {
clearChatDraft(chatDraftKeys.panel);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve the persisted draft while the send is pending

If the page is refreshed or closed while a send is pending, the draft is still lost: handleSend calls setInput('') before awaiting sendMessage, so this effect immediately removes the persisted copy. The failure handler can restore it only if the page remains alive until the promise resolves. ChatComposer has the same sequence. Preserve the in-flight text durably until acceptance rather than treating the send-induced input reset as a user deletion.

Useful? React with 👍 / 👎.

Comment on lines +6 to +9
export const chatDraftKeys = {
panel: 'omi.chat.draft.panel',
home: 'omi.chat.draft.home',
} as const;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Scope persisted chat drafts to the authenticated account

When one user signs out and another signs in using the same browser profile, both composers restore the previous user's private draft because these localStorage keys are shared across all accounts. The inspected sign-out path (AuthProvider.handleSignOut → firebase.signOutUser) does not clear localStorage, and both composers read these keys unconditionally on mount. Namespace drafts by authenticated UID and ensure account changes cannot restore or write another user's text.

Useful? React with 👍 / 👎.

break;

case 'error':
failed = true;

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 Record stream errors before applying the session ownership guard

If the user switches chat sessions while a send is pending and the old stream subsequently emits an error chunk, the callback's earlier if (!isCurrent()) return skips this assignment. Since sendMessageStream forwards error chunks without throwing, the promise then resolves true despite the failed send, preventing the composer's failure path from restoring its draft. Keep suppressing stale UI updates, but record the request-local failure independently of whether its session is still selected.

Useful? React with 👍 / 👎.

A pending handleSend continuation could write the previous account's prompt
under the new owner's draft key when the account switched while the send was
still running. Both composers now capture the draft owner at send start and
drop the post-send draft write when the owner changed mid-send.

@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: e0726b9c70

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

setStreamingText('');
}
}
return !failed;

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 Require a terminal acknowledgement before reporting acceptance

If the response stream ends cleanly before delivering a done chunk, this returns true even though no terminal acknowledgement was received. sendMessageStream in src/lib/api.ts simply resolves when reader.read() reports EOF; it does not verify completion, so an empty or prematurely closed response takes this path without throwing or setting failed. Both composers consequently treat the interrupted send as accepted and skip restoring its draft. Track receipt of the terminal acknowledgement separately and return false when the stream ends without one.

Useful? React with 👍 / 👎.

@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: 20f6c3b125

ℹ️ 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 +247 to +249
const persisted = readChatDraft(chatDraftKeys.panel);
if (persisted === text) {
if (accepted) clearChatDraft(chatDraftKeys.panel);

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 Track draft revisions instead of comparing only their text

If a user switches chat sessions while a send is pending and starts a new draft with the same text, the old send's successful completion deletes that newer draft from storage. Switching sessions resets isStreaming in useChat, allowing this edit before the old request finishes, but persisted === text cannot distinguish the new draft from the submitted one. The input still displays the newer text, yet refreshing loses it because the persistence effect does not rerun until the input changes. Track a draft revision or send identity before retiring persisted text; the same comparison in ChatComposer needs the equivalent guard.

Useful? React with 👍 / 👎.

@Git-on-my-level
Git-on-my-level merged commit 582195a into main Oct 29, 2026
44 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/dlaudit-web-chat-draft branch October 29, 2026 10:10
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.

2 participants