Skip to content

Inbox: typed words are never lost - #4022

Merged
backnotprop merged 26 commits into
mainfrom
fix/inbox-never-lose-typed-words
Nov 25, 2026
Merged

backnotprop merged 26 commits into
mainfrom
fix/inbox-never-lose-typed-words

Conversation

@backnotprop

@backnotprop backnotprop commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

The owner's rule for the Inbox: "The moment I start typing text, that's a draft." Words typed in the Inbox must never be lost. A hunt found six ways they were. Each fix below is its own commit, and each has a test that fails without it.

1. Typing was not saved while a saved draft was offered.

  • Before: the box was typed in on one device while the same thread's draft was saved on another (or the thread's first read was slow). The new words waited behind "Restore draft" and were never saved.
  • Now: an untouched box takes the saved draft. In a box the person typed in, the first edit takes the draft in, with the person's words after it, and saves the result. This is what Restore draft does.

2. A reply typed in another linked computer's thread was lost on leaving the thread.

  • Now: its words are kept for that thread in the page, and the page's leave guard asks while they are there.

3. R and the Reply arrow left the caret at position 0.

  • Before: on a box with restored words, both left the caret at 0, so typing went in front of the words.
  • Now: R, the one-agent Reply arrow on an open box, and the first focus of a restored box put the caret after the words. Opening a thread still takes no focus.

4. A save that did not land was dropped.

  • Before: a network error, the Inbox down, or an update's restart dropped the save. The words stayed only in the box.
  • Now: the words are kept per thread. They show again when the thread opens, the leave guard asks for them, and they are saved again when the stream reconnects, the page takes a restarted Inbox, the device comes back online, or the next key is typed.
  • The Inbox now takes a reply-draft save from a page that an earlier run of the same Inbox served (the same data folder's sessions, as for a restart). Other routes keep the session guard. Discard from such a page is still refused.

5. New message kept its words only until the dialog closed.

  • Before: a reload, a restart or a closed tab lost every word.
  • Now: New message saves to its own draft after an 800 ms pause in typing, and at once with keepalive as the page hides or goes. "Leave page?" asks only while words are not saved yet.

6. A phone that hid the page could lose the reply's last words.

  • Now: a reply's pending words are also sent with keepalive when the page becomes hidden (visibilitychange), and they are read back when the page is shown again.

No person-facing words change: the existing lines and announcements are used again.

Tests:

  • DOM: packages/inbox/replyDraftNeverLost.test.tsx and packages/inbox/composeAutosave.test.tsx.
  • Server: packages/server/inbox-reply-draft-session.test.ts.
  • Browser: inbox-reply-late-draft-webkit.spec.ts now covers two origins (127.0.0.1 and localhost, as an iPad and a Mac). New inbox-compose-reload-keeps.spec.ts covers a reload inside New message's pause.

Each case was also run before and after on a sandboxed Inbox with two origins and an update restart. Before, every case lost words; after, none did.

e2e: inbox-reply-late-draft-webkit.spec.ts inbox-compose-reload-keeps.spec.ts inbox-reply-drafts.spec.ts inbox-compose.spec.ts inbox-draft-after-send-webkit.spec.ts
Review fixes (an independent data-safety review found these from reading the code; each is reproduced in a DOM test against a stand-in server with the compare-and-set, failing before its fix):

  1. Sent words came back. A read that found the offered draft gone (sent or discarded on the other device) left the offer, so the next key merged the sent words into the box. Now a read that is null or a different draft drops the offer and puts back the base this window knew before it (dropOffer); a newer draft is offered in its place.
  2. Older words landed after newer ones. The read-back after the page is shown again wrote the keepalive's words after a newer save queued behind it. Now it skips when newer words of the box are queued or waiting.
  3. Words that did not land were replaced. A box opened and typed in before the thread's first read did not hold them, and the save of the new key cleared them. The hook's first state for a thread now holds this window's unsaved words, so the box opens with them.
  4. New message refused its own saves. The save's base came from the dialog's state, stale after its own delete or while a save was on its way: a 409 then turned autosave off and Escape said "This draft was deleted in another window." Now the base is the updated_at the dialog's last save landed (null after its delete), each body is built when the save runs, Escape and Keep mine wait for a save on its way, and a refusal after an unanswered save of its own is read back.
  5. A killed tab. Conflict words, words that did not land, Keep mine on its way and another computer's thread's words are also kept in localStorage per thread (replyWordsKept.ts), removed where the page lets them go; a new page takes them back once as the thread opens.
  6. Send from a closed box read the unsaved words from before its claim; it reads them after, so a draft its claim saved (with files) is the one the Send carries and clears.
  7. Keepalive size is counted in bytes in post (New message).
  8. The reply-box DOM files' afterEach fails when a save or unsaved words are left.
  9. An edit in the middle of words both devices went on from is made once in the newer draft, the caret kept.

What a person sees differently from main: a reply box they typed in shows "A saved draft of this reply is waiting." when another device saves, and their next key takes it in; a focused box they did not type in takes another device's draft; New message shows "Draft · saved" and Delete draft (a phone: "✓ Saved"), its title "Draft", and a Drafts row and count, about 800 ms after a pause in typing. Whole-window shots: inbox-compose-reload-keeps.spec.ts writes them to .local/proof/never-lose-words/.

Third review (two silent-loss paths left, and the no-Web-Locks fallback). Each has a DOM test in replyDraftNeverLost.test.tsx that failed before its fix:

  1. Words sent by keepalive were never kept in the browser. (a) A phone with the Inbox out of reach hid the page inside the pause; the keepalive failed quietly and an ended tab lost the burst. (b) With a save on its way and another queued, a reload sent the keepalive with the queued, unsent words as base_body, so the Inbox refused it once the save on its way landed. Now the keepalive's words go in the browser's keep before it is sent (cleared by a landed save or the read-back, as before), its base_body is the last words sent, and a page that goes with words queued behind a save on its way sends those too.
  2. Words taken back from an ended tab were dropped if the person typed before the first read answered. They are now shown as soon as they are back; a box typed in before that takes them in first, the typed words after them (failed and conflict words alike), and they are no longer marked as shown by a box that never showed them.
  3. No Web Locks (plain http on a LAN) took a live tab's words. A page keeping words now keeps a heartbeat in localStorage (every 5 s, on visibilitychange, again after a back/forward-cache restore; removed on a pagehide that is not into that cache). Another page's words are taken back only once its heartbeat is gone or older than 30 s. happy-dom has no Web Locks, so the DOM tests run this path except those that install a stand-in navigator.locks.

Known follow-ups (not in this PR):

  • Files, recipients, the quote and mentions are not kept in browser storage, only the words.
  • New message: the keepalive sent as the page goes is not queued behind a save on its way.
  • offeredWithTyped can join the same words twice in one edge case.
  • Discard from a page an earlier run of the Inbox served is still refused.
  • Another computer's thread whose kept words come back only once the thread is known as that computer's: a box typed in before then does not take them in.
  • No Web Locks: a page opened within 30 s of the last heartbeat of a tab the browser ended does not take its words; they stay in storage for a later page. A desktop tab hidden over 5 minutes beats about once a minute (timer throttling), so another tab may take its words; both then hold them.
  • Back/forward-cache restore with a save queued behind one on its way: the queued, older words can still run after the keepalive's newer ones; the read-back then shows the conflict (nothing is lost).

…aft waits

A saved draft that arrived while the box was open (another window or device
saved it, or the thread's first read answered late) stood behind Restore draft,
and from then on nothing typed in the box was saved. Now an untouched box takes
the draft, as it did before; a box the person wrote in offers it, and its next
edit takes it in, the typed words after it, and saves both. A box is the
person's once they type in it, not when it is focused.
In a thread with one agent the message's Reply arrow left focus on the arrow
when the box was already open, so typed letters ran Inbox shortcuts. R focused
a restored box with its caret before the words. Both now focus the box at once
with the caret after the words, and a restored box's first focus by a key
(Tab, Edit draft) lands there too. A thread opening still leaves focus where
the person put it.
A save the Inbox refused or never answered kept the words only in the box:
another thread and back showed the older draft, and a reload lost them. After
an Inbox update a page left open had every draft save refused for its old
session, so "Not saved" never cleared. Now the words are kept per thread, the
box shows them when the thread opens again, the restart guard counts them,
and they are saved again when the stream reconnects, the page is back online,
the thread is opened again or a key is typed. The draft save route takes a
page an earlier run of the same data folder served (it is a compare-and-set);
every other write keeps its session guard.
A phone that backgrounds the tab or switches apps does not fire pagehide, and
may end the page there: words typed inside the pause in typing were lost. The
save that goes as the page goes now also goes when the page is hidden
(keepalive), with words of the open thread whose save did not land, and is
read back when the page shows again; words it did not land are saved again.
New message saved nothing until it was closed, so a reload, an update's
restart or a killed tab lost everything typed. It now saves to its own draft
after a pause in typing, and at once with keepalive as the page hides or goes;
the page's "Leave page?" asks only while words are not saved yet. Escape still
keeps the draft ("Draft saved"), a Send takes the draft a pause saved and no
save lands after it, and words emptied delete the draft the dialog made. The
skill picker's room moves to its own file to keep the dialog under its size.
A linked computer's thread keeps no draft on this Inbox yet, and the box
emptied on a thread change: a reply typed there vanished with no warning when
the person went to another thread. Until the draft is kept on the owning
computer, the words are kept in the page per thread, come back in the box when
the thread opens again, and the restart guard and "Leave page?" ask for them.
A save that does not land is now kept for its thread and shown again, so a
save the pane made as a test ended, answered by the real fetch, came back in
the next test's box.
…ck; older words never land after newer; unsaved words are in the box before the read

- reload: a read that is null or a different draft drops the offer and puts back the base this window knew before it.
- the read-back after the page is shown skips its save when newer words of the box are queued or waiting.
- a thread opened with words not saved here (a conflict's, a failed save's) shows them from the start, so a box opened before the read holds them.
- DOM tests: each test starts on a fresh page state, and the afterEach fails when a save or unsaved words are left.
…when the save runs

- The base is kept in a ref: the updated_at the dialog's last save landed (null once it deleted the draft it made),
  so typing again after emptying no longer gets a 409 that turned autosave off and showed "deleted in another window".
- Each save's body is built when it runs, after the save before it answered: overlapping saves (a hide or Escape while
  a save is on its way) no longer refuse each other. Escape and Keep mine wait for that save first.
- A refusal caused by the dialog's own save whose answer was lost is read back and saved on, never a conflict.
- A keepalive's size is counted in bytes; a larger body goes as an ordinary request.
- The compose DOM test's stand-in Inbox does the server's compare-and-set.
…im, not before

The claim saves words whose save did not land. When that save landed, the Send now takes the draft as it is (its
updated_at and files) and clears it, instead of sending the words alone and leaving the draft, with its files, behind.
…e; an offered draft merges an edit in its middle once

- conflictWords, failedWords, remoteWords and Keep mine on its way are written to localStorage per thread (with the
  draft's updated_at), on every change of them, and removed where the page lets them go (a save or Keep mine landing,
  Discard mine, Send, Discard). A new page takes them back as the thread first opens; the read drops them when the
  draft holds them, saves failed words on an unchanged draft, or shows the conflict. Capped, quota errors ignored.
- offeredWithTyped: a box that already holds the draft keeps its words; an edit in the middle of the words both went on
  from is made in the newer draft (no second copy of the text), and the caret stays where the person was typing.
- Docs for the reply and compose guides.
… not saved, fails

The afterEach wait in the reply-box DOM files gave up after a second without failing, so leftover state could carry
into the next test unseen. The voice test for another machine's thread now discards its kept words. ComposeDialog.tsx
shrank a line: its ceiling is lowered.
…esktop light and dark, a phone)

Into .local/proof/never-lose-words/: the dialog inside the pause, then as a Draft ("Draft · saved", Delete draft, the
sidebar's Drafts count) on a desktop, and "✓ Saved" on a phone.
…ore the real fetch is back

Its Send test ends with the box's words waiting for the pause; the save sent as the pane unmounted went to the real
fetch, did not land, and stayed as not saved. A new pane now compares those words with the draft this page last knew
(gone), so the next test showed the conflict line instead of Reply.
…y were typed over

- A closed box's read no longer takes a newer draft in quietly while words wait unsaved: it is the conflict, and
  their retry sends the base they were typed over (and names only themselves as base_body), so it never lands over
  another device's draft.
- The floating draft window shows words whose save did not land, as the thread's box does.
- Words that did not land go only with a save that holds them or comes from the box that holds them.
- Browser copy: one entry per page and thread; a page holds a Web Lock for its life and a new page takes back only
  a gone page's entry. 14 days, 20 entries and 200 KB at most, swept on load. An entry with no base waits for the
  person. Another computer's words come back once the thread is known as its.
- The page hiding with a save on its way queues the newer words after it instead of a keepalive beside it.
- New message: a Send after an autosave with no answer names the draft.
…a save on its way

Only a hidden page (visibilitychange), which keeps running, queues the newer words after the save on its way; a
page that goes has no after, so its words go as a keepalive as before.
@backnotprop backnotprop added the ci:e2e Run the full browser suite (Inbox window e2e) on this PR label Nov 25, 2026
…ithout Web Locks are never lost

- The keepalive sent as the page hides or goes puts its words in the browser's keep first (cleared by a landed
  save or the read-back, as before), and names the last words SENT as base_body, not words queued behind a save
  on its way. A page that goes with words queued behind a save on its way sends those too.
- Words taken back from a gone page are shown as soon as they are back; if the person typed in the box before
  they came back, the box takes them in first, the typed words after (failed and conflict words alike), and they
  are no longer marked as shown by a box that never showed them.
- Without Web Locks (plain http on a LAN), a page keeping words keeps a heartbeat in localStorage (every 5 s and
  on visibilitychange, again after a back/forward-cache restore, removed as it goes for good); another page's
  words are taken back only once its heartbeat is gone or older than 30 s.
…ved; proof and paste specs fixed

- A desktop's Delete draft (there once a pause saved the draft) steps aside while a take runs,
  as the spacer and status do, so the level keeps its room (inbox-voice.spec.ts:1643).
- A phone's top-bar Save draft stays, disabled, while dictated words are on their way, though
  a pause saved the draft (inbox-voice.spec.ts:1887).
- The never-lose-words proof: desktop and phone are two tests; the phone opens New message from
  the Drafts page (an Inbox with no thread has none in its list header), clicks time out.
- inbox-dictation-paste.spec.ts: the range test reloads its page first. Earlier tests saved the
  thread's draft from other pages, so this page's box offered it and the next edit took it in,
  as designed. The assertion is unchanged.
@backnotprop
backnotprop marked this pull request as ready for review November 25, 2026 05:49
@backnotprop
backnotprop merged commit ca6d996 into main Nov 25, 2026
57 checks passed
backnotprop added a commit that referenced this pull request Nov 25, 2026
… client

Main rewrote the reply-draft client (never-lost words, #4022). Its client is
the base here; the far-thread path is applied onto it:

- A linked computer's thread that keeps drafts (mark `reply-drafts`) saves,
  reads, discards and sends through the forward as a local thread does, with
  the same client (conflict line, words that did not land kept and saved
  again, keepalive, kept in browser storage when a save does not reach this
  Inbox). While that computer sleeps, this Inbox holds the save in its outbox
  (one copy) and answers for it.
- `remoteWords` is now the fallback only for a thread whose computer keeps no
  draft from here (an older Plannotator, or its list not read yet): nothing
  is sent, the words stay in the page and the leave guard asks, as on main.
  Once that computer keeps drafts, the kept words are saved there once on the
  draft this window knew, or the conflict shows (remoteReplyWords.ts,
  `remoteToDraft`).
- DOM test farReplyDrafts.test.tsx: the pane over the real draft forward and
  a stand-in far door with the compare-and-set.
- Mobile contract regenerated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:e2e Run the full browser suite (Inbox window e2e) on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant