Skip to content

feat(chat): keep an unconfirmed send's text across a page reload - #25377

Open
rnoack1 wants to merge 1 commit into
kirodotdev:mainfrom
rnoack1:split/persisted-recovery-store
Open

rnoack1 wants to merge 1 commit into
kirodotdev:mainfrom
rnoack1:split/persisted-recovery-store

Conversation

@rnoack1

@rnoack1 rnoack1 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A reload or crash while a send's delivery is in doubt no longer loses its text: the next page load puts it back in the composer, unless the server already has it.

Split out of #6825, as its maintainer triage asked: this is the persisted recovery store, the last of the three units named there. It builds on the delivery-warning strip (#22478).

When a send times out, the delivery warning keeps what it consumed (text, pastes, files, quote, session refs, knowledge) in module memory, so Remove can hand it back. The composer has already been cleared and the optimistic bubble lives only in the store. A reload or a renderer crash at that point destroys all three, so the user's words are gone with no trace and no message.

Why it matters

The send most likely to be in doubt is the one sent while the connection was failing, which is also when a user is most likely to reload. Retyping a long prompt from memory is the cost, and nothing tells the user the message may not have gone out.

Not a goal

  • A reload in the first 10 seconds, before the client's deadline raises the warning. Nothing is saved until the warning exists, as today.
  • A send whose warning is cleared for another reason, including a slot refetch that drops its bubble (the bug in fix(chat): keep the optimistic user bubble across a slot refetch #6825's title). The saved copy follows the in-memory warning, so it goes too.
  • Restoring on a different tab, or after the tab is closed. A record lives in the sessionStorage of the tab that saved it, so a reload and a crash keep it and no other tab can read it. A tab the browser duplicates copies sessionStorage, so the copy can be handed the same send; the history check drops it there once the send is delivered.

What changed (motivation → approach → change)

  • The warning is saved as it is raised. utils/deliveryWarning.ts now writes each warning through utils/sendRecoveryStore.ts and retires the saved copy wherever the warning ends: pruned (confirmed, queued, cleared, refetched away) or removed with the strip's exit.
  • One record per send, in this tab only. The store is one sessionStorage blob keyed by slot and send id, so two sends on one chat cannot overwrite each other, on the drafts' 30-day TTL. Only this tab writes it, so a read-modify-write cannot race another writer. A window another window opened (the chat pop-out) starts with a copy of the opener's sessionStorage while the opener still holds the live warning, so that copy is dropped on the opened window's first load. It is recognised the way the composer's attachment arrivals already are: the copy still carries the opener's window id (utils/windowStorageOwner.ts, now shared with composerSendHolds.ts), so every later load of the pop-out, a navigation inside it included, keeps its own records. A write the tab refuses returns false and leaves the saved records as they were.
  • Checked against the server's history before it is handed back. Once the chat's transcript has loaded (the page's switch has landed; a pane's own page has hydrated), useDeliveryWarningStrip claims the recovered sends and utils/recoveredSendHistory.ts reads the slot's history newest page first. It keeps reading older pages until it has found every send or reached the transcript's start. Row times are not used to stop early, because the gateway's clock and this browser's can disagree. The loaded window alone is not enough, because later output can push a delivered send out of it. A send the server has, as a user row or a queued item, is retired without a word, unless a stored draft already holds its text from an earlier load: then the delivered-after-all notice below is posted before the record is retired. A row a queue drain merged from several sends counts for every id in its meta.sendIds, not only the last one in meta.sendId. The shared selectSendConfirmed / selectSendAccepted selectors read meta.sendIds the same way, so the loaded transcript and the late-delivery notice both recognise a send folded into a merged row. If the history cannot be read, or 8 pages do not reach that point, nothing is handed back and every record is kept for the next load. The rest are merged into one call to that surface's existing restore (restoreSentTurn on the page, the pane's restoreIntoComposer), with a notice: "A message from before the page reloaded was never confirmed, so its text is back in the composer." A crewmate pane, which raises no warning, takes nothing.
  • The record ends once its text is saved again. The record is retired when the text is back in the composer and that composer's draft has been written to storage, so the draft holds it like anything typed there. The page's saveDrafts reports that write once text, files, paste blocks and session refs all stored; a pane, whose live composer is React state, reports it when it parks (switching chats, closing, pagehide). A save retires only the chats whose drafts it actually kept: a chat the 2 MiB draft budget dropped keeps its record until a later save stores it again. Only the composer instance holding the text can retire its record; when that instance closes without a good save, it stops waiting and the record stays for the next page load. Until then, for example on a full storage quota or a renderer crash, the record stays. The draft's own stored entry records which recovered sends its text already holds, in the same write as the text. So a later reload never appends that text again, even when a paste or session-ref write failed and the record had to stay; it puts back only what the draft lacks.
  • A late delivery is said out loud. A recovered send is remembered like a removed one, and if it still turns up, or a later load finds it in the history while the draft still holds its text, the notice reads "A message from before the page reloaded was delivered after all. Clear the composer to avoid sending it twice." rather than claiming the user removed it.
  • Saved data is read as untrusted. parseSentTurn and parsePaneSent keep only well-formed fields, reusing the paste-block and session-ref sanitizers the draft stores already use, and the transcript's own readMessageQuote, which caps the quote's text. A payload is restorable on the same terms isEmptyTurn allows a send: typed text, a quote, session refs (page) or files (pane). A quote comes back inside the text rather than on the stage, because the stage is not saved and a second reload would lose it.

Two delivery_recovered_* keys are added to all 13 catalogs.

Backwards compatibility

Compatible: additive storage, plus two narrow behaviour changes.

  • Two new sessionStorage keys: mc-chat-send-recovery (the records) and mc-chat-send-recovery-window (this window's id, for the pop-out check).
  • selectSendConfirmed / selectSendAccepted now read rowSendIds, so every existing caller counts a send folded into a merged queue-drain row as confirmed.

Removes nothing: no record or capability is taken away. A saved copy is retired only where its in-memory warning already ends.

Tests

  • sendRecoveryStore.durability.test.ts: two sends on one chat both survive; forgetting one leaves the other; a prefix-sharing slot is not read; nothing is written to localStorage and a sibling tab (empty sessionStorage) sees nothing; a malformed value is dropped; a refused write returns false and keeps what was saved; and the TTL applies.
  • useDeliveryWarningStrip.test.tsx, against the real store with the history read stubbed: recovered sends come back merged with one notice only once the transcript is ready, and their records are then gone; a later load does not hand the same send back again; a send whose text reached the draft while its paste write failed is not appended again on reload, and only its paste comes back; a record whose draft was not yet written stays until the composer instance holding it reports the write, only for that surface and chat, and a save by any other instance leaves it; a send the transcript already has is retired silently; so is one the server history holds a page beyond the loaded window, one a queue drain merged into another send's row, and one whose echo lands while the history is read; a send the history holds a page back is retired even when the browser clock runs ahead of the gateway; an unreadable history hands nothing back and keeps the record, and so does one that reaches the page cap, which is read exactly RECOVERY_HISTORY_MAX_PAGES times; a later echo posts the recovered-delivered notice, not the removed one, and so does a later load whose history has a send the stored draft already holds; the other surface and a disabled host take nothing; raising a warning saves it and Remove or a prune retires it.
  • useChatPaneDraft.recovery.test.ts and composerDrafts.recovery.test.ts: a pane keeps a waiting record while the text is only on screen or when storage refuses the park, and retires it when it parks (rebind, unmount, pagehide); a pane restore reports saved only when the text went to storage and no other pane took it; a page save that drops an awaiting chat's draft to fit the budget keeps its record, and a later save that stores it retires it; a closed page or pane instance whose last save was refused leaves the record for the next load, and a later instance's save does not retire it; the page retires waiting records only when text, files, paste blocks and session refs all stored, and keeps them when any of those writes is refused.
  • recoveredSendParse.test.ts: both parsers keep every field their restore takes, drop malformed members, accept a quote-only, session-ref-only or files-only payload, cap the quote through readMessageQuote, and refuse an empty payload.
  • Negative controls, each re-creating one defect and turning its own test red until the code is restored: the record kept after its hand-back (2 tests red), the record retired before its draft was written (2 red), the page save not reporting the write (1 red), the paste or session-ref result left out of that check (1 red each), a save by another instance retiring the record (3 red), a closed instance deleting its records (4 red), a save retiring a chat the budget dropped (2 red), the already-applied check removed on the page or the pane (1 red each), the history-found send in an applied draft retired silently (1 red), the pane parks not reporting it (3 red), the history page cap removed (1 red), and records also written to localStorage. Earlier rounds' controls for the parsers, the merged-row ids, the history read and the TTL still apply to unchanged code.

Sweep (src/i18n with the diff-scoped style gates on, src/components, src/store, src/utils, src/chat-core, src/pages/chat, src/test/ChatPane*, src/test/ChatPage*, src/test/ChatInput*, src/test/recoveredSendParse.test.ts) passes: 10598 of 10598. tsc -p tsconfig.app.json and eslint --max-warnings 0 on every changed source and test file are clean.

Manual verification

Run on the real built app with a local Playwright capture script (kept out of this PR to stay under the size cap; it can land on its own). Every /api call is answered from fixtures, storage is kept across the reload, and the harness holds the dashboard socket open. POST /api/chat is never answered, so the client's 10s deadline raises the warning. The script asserts each state before it shoots and exits non-zero on any mismatch:

  1. After the deadline: the strip is shown and the composer is empty.
  2. After a full page reload: the text is back in the composer, and the recovered notice appears exactly once.
  3. The socket then delivers the server's echo of that send: the recovered-delivered notice appears exactly once.

Last run at the previous revision, against the sessionStorage store; it passed every assertion. This revision changes only what happens to the record after the hand-back, which none of the three steps observes. The re-run frames add one toast outside this feature ("Couldn't load your starred mates."), so the committed frames, from the previous base, were kept.

Not covered by this run: a real gateway process, and the split-view pane (covered by the hook's unit tests through the same shared hook).

Screenshots / video

Chat page, dark: in doubt before the reload, recovered after it, delivered after all.

Before the reload: the strip above an empty composer

After the reload: the text is back in the composer with the recovered notice

The send's echo lands: the delivered-after-all notice

Related Issues

Part of #6825.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 28, 2026
@rnoack1
rnoack1 force-pushed the split/persisted-recovery-store branch from 9a55d77 to b7447af Compare October 28, 2026 19:44
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 28, 2026
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ✅ PASS

UX-level review of fddd55cfdb2732d5437ba51c171e824f9d13cea4 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

UX-Verdict: PASS

Nothing blocks. The committed screenshots show both new notices: the text coming back into the composer (02) and the "delivered after all" warning (03). No first-time reader has seen them, because this fork lane has no blind read.

Notes

  • In page-dark-03-delivered-after-all.png, "…was never confirmed, so its text is back in the composer." still sits above "…was delivered after all." The older notice now contradicts the newer one. Retire or update the delivery_recovered_after_reload row when delivery_recovered_delivered posts, so the transcript shows only the current state.

[UX-REVIEWED] fddd55c

Description read

[DESCRIPTION-READ] 009a8a9d3f82da33131eaa709b2fba0b94305483d21f57d4d665f382c4531c97

That sha256 names everything this verdict read: the PR description, plus the 6 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of fddd55cfdb2732d5437ba51c171e824f9d13cea4 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

No design-level blocker: the change is aimed at a real data-loss window, retires records only where the in-memory warning already ends, and the screenshots committed with this revision cover the new notices.

Notes

  • The mc-chat-drafts format change is a one-way door on rollback. Today an entry is a plain string, and an entry that holds a recovered send becomes { text, applied }. The pre-patch isNonEmptyString drops any entry that is not a string, so a revert or downgrade silently deletes those drafts. A short line about this under Backwards compatibility would make the risk visible.

[DESIGN-REVIEWED] fddd55c

Description read

[DESCRIPTION-READ] 079bae5e4b675e4bb81225257530cc551ca3b255f0c258cf89d46aa5ed3d6dbc

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — ✅ PASS

Premise-level review of fddd55cfdb2732d5437ba51c171e824f9d13cea4 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing blocks. Two durability layers built after the hand-back go beyond the Goal, and one adds a generic store hook that only one store uses.

Notes

  • Item 7 — oversized: the Goal is "a reload or crash while a send's delivery is in doubt". The DraftOwner / awaitingDraft / settleOwned / dropDraftOwner / lostToCap machinery guards a later window: the gap after the text is already back in the composer, which typed text already shares. Subtraction: retire the record in restoreSentTurn / restoreIntoComposer once the draft write returns true, and delete the owner-token and cap-loss tracking. Clears when: the mechanism is removed, or a human amends the Goal.
  • Item 8 — one consumer, generalized: encode? and sanitize(v, slot) were added to createSlotDraftStore, which 10 production stores call, but only chatDrafts.ts passes encode. That file also adds a module-level appliedIds Map. Subtraction: store { text, applied } as the value type in chatDrafts, the way PaneDraft.applied already does, and delete encode, the slot argument and appliedIds.

What this change ships

Inventory (10 items) — 7 justified

Intent: keep an in-doubt send's text after a page reload or tab crash so the user doesn't retype it. This is an ADDITION (split out of #6825).

  1. A send whose delivery warning is showing is saved to this tab's sessionStorage — justified
  2. The next page load puts unconfirmed sends back in the composer with a "never confirmed" notice — justified
  3. Server history (up to 8 pages) is checked first, and a send the server already has is dropped silently — justified
  4. A recovered send that arrives late says "delivered after all" — justified
  5. A pop-out window drops the records it copied from its opener (windowStorageOwner.ts, shared by two stores) — justified
  6. A merged queue-drain row now confirms every send id it carries, in the shared selectors — rides along, declared
  7. A record is kept until that composer instance's draft save succeeds, tracking per-instance owners and stores that dropped the chat to their size cap — oversized
  8. Drafts store recovered send ids so a reload does not append the same text twice, via a new encode store hook — one consumer, generalized
  9. Two new notice strings in all 13 catalogs — justified
  10. Feature-map row and committed screenshots — justified

[FIRST-PRINCIPLES-REVIEWED] fddd55c

Description read

[DESCRIPTION-READ] e96cac3e0ddf871a10413eece743f4b5eb41bb2dfbd1de203cce4a00b763a3e9

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed fddd55cfdb2732d5437ba51c171e824f9d13cea4 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] fddd55c

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed fddd55cfdb2732d5437ba51c171e824f9d13cea4 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] fddd55c

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 28, 2026
@rnoack1
rnoack1 force-pushed the split/persisted-recovery-store branch from b7447af to 8b4f356 Compare October 28, 2026 21:54
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 28, 2026
@rnoack1
rnoack1 force-pushed the split/persisted-recovery-store branch from 8b4f356 to 067a94b Compare October 28, 2026 23:08
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 28, 2026
@rnoack1
rnoack1 force-pushed the split/persisted-recovery-store branch from 067a94b to c27e4ef Compare October 28, 2026 23:23
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 28, 2026
@rnoack1
rnoack1 force-pushed the split/persisted-recovery-store branch from c27e4ef to 7b7e5bd Compare October 29, 2026 00:25
@rnoack1
rnoack1 force-pushed the split/persisted-recovery-store branch from e4bc46d to 0c962d7 Compare October 29, 2026 19:10
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 29, 2026
@rnoack1
rnoack1 force-pushed the split/persisted-recovery-store branch from 0c962d7 to 550fa92 Compare October 29, 2026 20:25
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 29, 2026
@rnoack1
rnoack1 force-pushed the split/persisted-recovery-store branch from 550fa92 to 6d61872 Compare November 4, 2026 19:53
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running labels Nov 4, 2026
@rnoack1
rnoack1 force-pushed the split/persisted-recovery-store branch from 6d61872 to e53fe1d Compare November 4, 2026 22:01
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Nov 4, 2026
@rnoack1
rnoack1 force-pushed the split/persisted-recovery-store branch from e53fe1d to 6420636 Compare November 4, 2026 23:39
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Nov 4, 2026
@KiroCrewCaptain

Copy link
Copy Markdown
Collaborator

This changes behaviour users already rely on, so it needs a design sign-off from a maintainer before it can merge. A maintainer will post the decision here. To propose a design, follow the request-for-change process in CONTRIBUTING.md (docs/request-for-change/).

The review of this head found no blocking defect. It found the following:

  • The history check fails closed. A read error or the page cap hands nothing back and keeps the records for the next load.
  • The stored sessionStorage blob is treated as untrusted. Each field is type checked and reuses the existing paste, session ref and quote sanitizers. Restored values reach only the composer and never an HTML sink.
  • The opener check moved into windowStorageOwner.ts with its logic unchanged.
  • The new tests pin the clock, wait on signals, and cover the read-in-flight races one case each.

Optional:

  • After a user edits a recovered draft and reloads again, composerCarries (useDeliveryWarningStrip.ts around line 143) no longer sees the send in the composer. The original text is then appended a second time. Forgetting the record when the text is handed back would remove this case, along with most of the keep-until-emptied code. If you keep that code, please answer the open First Principles note on it.
  • ## Backwards compatibility says no existing behaviour changes. selectSendConfirmed/selectSendAccepted now read rowSendIds, and mergeKnowledgeBack now chains restores within one batch. The diff also writes a second key, mc-chat-send-recovery-window. The section should name all three.
  • The page cap test still passes when the cap is removed. Asserting toHaveBeenCalledTimes(RECOVERY_HISTORY_MAX_PAGES) on the history spy would pin the cap.

@KiroCrewCaptain

KiroCrewCaptain commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: A person should decide

Why: the decider thinks the change is probably fine but estimates a 22% chance that this change introduces a defect, above the 12% limit for approving on its own.

Adds a saved recovery store in sessionStorage so a send whose delivery is in doubt keeps its text across a reload: records are written while the delivery warning shows and deleted wherever the warning ends. After a reload, each send is checked against the server history before its text goes back into the composer. I checked every reader and writer of the draft stores (all go through sanitize/encode), the record lifecycle (claim, release, adopt; a record is deleted only after a successful save by the composer instance holding the text, and refused saves, instances that close, and chats dropped by the draft cap keep it), the windows-opened-from-another check, the history paging and failure paths, the merged-row selector change, and the parsers that re-check saved data. Tests are extensive and CI test, lint and build jobs passed. No concrete failing path found. Remaining concerns (rollback dropping drafts in the new object shape, a contradictory notice left in the transcript, hidden module state) are not blocking.

Tier: 2, held for a person: design-doc-gate: D.

An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit fddd55cfdb · 1618 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

The delivery warning holds a timed-out send's payload in module memory,
which a reload or crash destroys after the composer has already been
cleared. Mirror each warning into a per-send localStorage record owned
by the tab that raised it, and once the chat's history has loaded on the
next page load, hand back any send the server never recorded, with a
notice.

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

fork Pull request from a fork (external contributor) readiness: passed Eligible automated validation passed for the current revision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants