Skip to content

fix(web): keep an unsubmitted chat draft across a page reload - #663

Merged
pikann merged 1 commit into
Paca-AI:masterfrom
siedlegroup:fix/composer-draft-survives-reload
Oct 29, 2026
Merged

pikann merged 1 commit into
Paca-AI:masterfrom
siedlegroup:fix/composer-draft-survives-reload

Conversation

@kayahk

@kayahk kayahk commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Summary

A chat message that has been typed but not sent is now kept in sessionStorage and restored when the composer mounts empty, so the reload an expired session forces no longer discards it. Closes #662.

The composer's text lives only in the assistant-ui runtime, which a page load throws away. Paca already refreshes its own access token silently (lib/api-client.ts 401 → /auth/refresh → replay), so what forces the reload is a lapsed refresh token or a deployment identity-proxy/SSO session — a full-document sign-in bounce no client code can avoid. The reload cannot be prevented from inside the app, so it is made non-destructive instead.

Type of Change

  • Other: web UI bug fix

What changed

  • apps/web/src/lib/composer-draft.ts — composerDraftKey / loadComposerDraft / saveComposerDraft, keyed by pathname, with storage access wrapped so a disabled or full storage never breaks the composer.
  • apps/web/src/components/assistant-ui/thread.tsx — useComposerDraft, called by the thread composer. That composer is what all four chat surfaces render (project floating chat, global floating chat, new-conversation page, conversation page), so the fix is one hook rather than four call-site changes. It restores before it persists on the first run (otherwise an empty first render would delete the draft it is about to restore), and clears the stored text whenever the composer empties — after a send, or after the user empties it.

sessionStorage, not localStorage: the draft only has to outlive a reload in this tab, and a prompt should not be left on disk once the tab is gone. Pathname keying keeps pages apart and matches the URL a re-auth bounce returns to.

Verification

  • npx vitest run src/lib/composer-draft.test.ts — 2 passed (round-trip + clear-on-empty, and per-page separation).
  • Sabotaged the clear-on-empty branch (setItem unconditionally) and re-ran: AssertionError: expected '' to be null, 1 failed | 1 passed — the test does catch the regression.
  • cd apps/web && npx @biomejs/[email protected] check <the three files> — clean.
  • npx esbuild --loader:.tsx=tsx src/components/assistant-ui/thread.tsx --outfile=/dev/null — parses.
  • npx tsc -b — no diagnostics in the changed files. The 5 errors it reports are in route-error-boundary.test.tsx, routes/__root.tsx and routes/_authenticated.tsx and come from the newer @tanstack/react-router that npm resolves without the repo's bun.lock; they are present on upstream/master too.

Local note for anyone re-running the suite on Node 26: window.localStorage is undefined because Node's own localStorage shadows jsdom's, so every test file fails at src/test/setup.ts unless NODE_OPTIONS=--localstorage-file=<path> is set. Not related to this change.

Not covered (deliberately)

  • Attached context chips (annotation/mention badges) live in an in-memory zustand store and are not restored; only the text is.
  • A floating panel's open conversation still resets on reload. The conversation is not lost — it stays in the Conversations list — only the panel's selection.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes

  • composer-draft.ts (new) — composerDraftKey / loadComposerDraft / saveComposerDraft backed by window.sessionStorage under paca.composer-draft:<pathname>; every storage access is wrapped so a disabled or full store degrades to "no persistence" without breaking the composer.
  • thread.tsx — useComposerDraft(aui, text) hooked into the shared Composer. It restores via aui.composer().setText on the first effect run before persisting (so an empty first render does not wipe the draft), and persists/clears on every text change.
  • composer-draft.test.ts (new) — round-trip + clear-on-empty, and per-page key separation.

The routing makes the pathname key safe: the project float is hidden on conversations pages (routes/_authenticated/projects/$projectId.tsx:66) and the global float is hidden on /projects/* and /conversations/* (routes/_authenticated.tsx:95-98), so at most one Composer is mounted per pathname and two live composers cannot clobber the same key. The restore-before-persist ordering and the clear-on-empty behavior are both correct for the reload flow this targets.

ℹ️ Draft key shifts when a persistent composer navigates

The project float lives in the project layout and ConversationView is reused across $conversationId changes, so those Composer instances stay mounted while the pathname changes. Because restored.current is never reset when key changes (thread.tsx:387-397), the hook does not restore the newly-navigated-to page's saved draft; instead the live text is re-saved under the new key. The same draft therefore gets persisted under every pathname that composer visits, and a stale per-page copy can resurface on a later reload at an earlier page.

This does not affect the targeted case (after a reload the composer mounts fresh, so restored is false and the draft restores correctly), and it is inherently hard to separate a single live composer's text from per-page keys. Flagging only so the per-page isolation claim is a conscious choice.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

The composer's text lives only in the assistant-ui runtime, so a reload
throws it away. An expired session forces exactly that reload — Paca
already refreshes its own access token silently, but a lapsed refresh
token or an identity-proxy/SSO session bounces the whole document through
sign-in, which no client code can keep the SPA alive across.

Park the composer text in sessionStorage, keyed by pathname, and restore it
when the composer mounts empty; clear it as soon as the composer empties so
a sent message cannot come back as a ghost draft. The hook sits in the
shared thread composer, so every chat surface (project float, global float,
new-conversation page, conversation page) is covered by one change.

Refs Paca-AI#662
@kayahk
kayahk force-pushed the fix/composer-draft-survives-reload branch from b145b6a to 8547ff9 Compare October 29, 2026 12:52
@kayahk

kayahk commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — the pathname-key observation was the useful one, and I've taken it rather than accepted the duplication.

The draft key is now fixed at mount: const key = useRef(composerDraftKey(window.location.pathname)).current. A composer that outlives an SPA navigation therefore keeps writing to the page it was mounted on, so its live text is no longer copied onto every pathname it passes through and a stale per-page copy can no longer resurface on a later reload at an earlier page. The reload case is unchanged — the composer mounts fresh with restored === false, so the mounted page's own draft is restored.

Re-verified on 8547ff9: vitest run src/lib/composer-draft.test.ts 2 passed, tsc -b reports nothing in the changed files (the 5 remaining errors are the pre-existing @tanstack/react-router typing drift in routes/__root.tsx, routes/_authenticated.tsx and route-error-boundary.test.tsx), biome check clean.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes

  • Pinned the composer draft-storage key to a useRef captured at mount (thread.tsx), so a persistent composer (project float, reused conversation view) keeps writing to the page it mounted on rather than re-saving its live text under every pathname it passes. This resolves the prior note that a stale per-page copy could resurface on a later reload.
  • The sessionStorage helpers and their tests are unchanged from the prior review.

The reload-bounce target still restores correctly: after a full reload the composer mounts fresh, so key is the current pathname and restored.current is false. The deliberate trade-off (a persistent composer only writes to its mount pathname) is documented in the new comment and is outside this PR's scope.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pikann pikann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thank you for the contribution! 🚀

@pikann
pikann merged commit 6bfe43b into Paca-AI:master Oct 29, 2026
2 checks passed
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.

[Bug] An unsubmitted chat message is lost when the page reloads after the session expires

2 participants