#14

Fix live mail state persistence

MergedstylesshOwnermerged intofrom

Summary

  • persist thread read and unread state through JMAP, including marking a conversation read when it is opened
  • report accurate mailbox totals and serialize sidebar count reads to stay within Stalwart's JMAP concurrency limit
  • persist archive actions and update Inbox, Unread, and Archived counts optimistically with rollback
  • sign out through Better Auth in live mode while preserving the demo flow
  • prevent reply shortcuts from entering their key and preserve list queries when closing a conversation tab

Validation

  • live-provider dogfood: compose/send, open unread, manual read/unread, archive, search, filter, sort, keyboard compose/reply/navigation, tab close, sign out, and hard-refresh persistence
  • pnpm test (28 passing)
  • pnpm exec tsc --noEmit
  • focused ESLint on every changed TypeScript/TSX file
  • pnpm build
  • git diff origin/main --check

Existing repository lint findings

pnpm lint still reports the three pre-existing errors in babel.config.js, postcss.config.js, and components/shell/login-screen.tsx; the changed files pass focused ESLint.

Activity

7
mikeyreviewed3mo ago
mikey3mo ago

Review summary

Traced the full mail-state-persistence flow end to end: the new PATCH /api/mail/threads/[id] endpoint, both MailProvider implementations (lib/demo/provider.ts, lib/stalwart/provider.ts), the new JMAP calls in lib/stalwart/client.ts (setEmailsSeen, moveEmails, createMailbox), the optimistic-update/rollback logic in components/mail/thread-list.tsx and components/shell/app-shell.tsx (adjustArchiveCounts, adjustUnreadCounts, auto-mark-read effect), and the sidebar count serialization in lib/mail/folder-counts.ts.

Specifically checked for:

  • Authorization: setThreadUnread/archiveThread on the Stalwart provider resolve IDs through mailboxClient(user), the same per-user-scoped JMAP session used by the existing getThread/listThreads methods — no new cross-tenant exposure.
  • Client-controlled fromFolder: the archive endpoint trusts fromFolder from the request body to pick a source mailbox, but the actual emails moved are filtered by real mailboxIds membership from the server, so a mismatched/malicious fromFolder just yields an empty move (returns 404) rather than moving the wrong messages.
  • Sidebar count arithmetic: walked adjustArchiveCounts and collectFolderCounts through the inbox/smart/archived cases (read vs. unread, archiving from the Unread view vs. a named folder) — the delta math is correct, including decrementing the Unread badge only when an unread inbox thread is archived.
  • JMAP correctness: calculateTotal: true and the added mailboxIds property fetch are actually required for the new total-count and archive-by-membership logic to work — no dangling additions.
  • Concurrency guard: updating ref in ThreadList prevents duplicate in-flight mutations per thread; collectFolderCounts was correctly switched from Promise.all to a serial loop to respect Stalwart's 4-connection JMAP limit (also covered by the new does not fan out provider requests test).

No blocking correctness or security issues found in this pass. One non-blocking nit left inline.

Follow-up worth considering (non-blocking): there's no unit test directly covering adjustArchiveCounts/adjustUnreadCounts (the sidebar-count reducer in app-shell.tsx) or the new PATCH route handler itself — the provider-level setThreadUnread/archiveThread and thread-state.ts helpers are tested, but the count arithmetic and request-validation branches in between aren't. Not required for this PR given the described manual dogfooding, but a small unit test on adjustArchiveCounts would guard against a silent regression in the badge math.

stylesshOwner3mo ago

Addressed the workspace-tab URL-base nit in 0d2a373.

Both URL parsing paths now use one shared neutral http://localhost base, so the helper no longer introduces an extra project-specific hostname and the origin guard stays consistent.

Validated on the updated head with:

  • pnpm test (28 passing)
  • pnpm exec tsc --noEmit
  • pnpm exec eslint lib/mail/workspace-tabs.ts
  • git diff origin/main --check
stylessh merged commit a0e863f into main3mo ago

Branch stylessh/fix-mail-state-persistence has been merged.

Checking branch