Skip to content

Keep words typed while the server is down when their tab closes - #6221

Closed
max-sixty wants to merge 2 commits into
mainfrom
drafts-approach-review
Closed

max-sixty wants to merge 2 commits into
mainfrom
drafts-approach-review

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Closed unmerged. Three Codex reviews and the leaf-agent review found eleven sequences in which a queued write, replayed by a later load, brings back words the user sent or put away, refuses words that should stand, or drops words the queue held: a delayed Cancel of an older generation overwriting the store's memory of a later settlement, a repeated settlement moving the replay boundary, a refused replay settling words typed over it, a replay losing its provenance once answered, a queued move losing its source settlement, a rollback comparing versions from two histories, and a page restored from the back/forward cache leaving a stale entry. A correct queue needs a settlement history per draft and replay provenance through every answer: a sync protocol rather than the ~60 lines Max set as the condition, so words typed while the server is down stay lost when their tab closes.

A tab closed while its page's server was down took the words typed since with it: the resend a closing page sends fails, and nothing else held them. Now the page going away also queues each draft whose last write failed in this browser's writingStore (the page-scoped storage that already keeps where the user was writing), and the page's next load in this browser takes the queue back as its own unsaved words and sends them on like any. The server's answer decides as for every write, and the load empties the queue, so it holds undelivered writes, never a copy of the drafts.

To weigh: a draft with a write still out at close is left to the resend, as before, because the server may take that write and the store keeps nothing of a draft it settled; replaying it after another tab sent or put the words away would bring them back. Codex found that in the first version, which queued every unsaved draft. Two narrow cases remain, stated above QUEUED: a write the server takes before it stops answering can be replayed, and of two tabs that both fail to save one draft, the queue keeps the last-closed tab's words. Making both airtight needs the store to remember every settled draft, which this leaves out.

Max chose this on the drafts review page, on the condition it stays near 60 lines: 14 net lines of code in browser/runtime/drafts.js, the rest comments and tests. Codex reviewed it. Tests: two new browser tests, each failing on the version before its fix (one stops the server, types, closes and reopens; one puts away words a closed tab's in-flight write delivered); the drafts tests that close pages, the revisions journey and pnpm run test:runtime pass.

This was written by Claude Code on behalf of max-sixty

A tab closed while its page's server was out of reach took the words typed since
with it: the resend a closing page sends fails, and nothing else held them. The
page going away now also queues, in this browser's page-scoped `writingStore`,
each draft whose last write failed, and the page's next load in this browser
takes the queue back as its own unsaved words and sends them on like any. The
server's answer decides as for every write, and the load empties the queue, so it
holds undelivered writes, never a copy of the drafts.

A draft with a write still out is left to the resend, as before: the server may
take that write, and replaying it after another tab sent or put the words away
would stand them up again, since the store keeps nothing of a draft it settled.
Codex found that hole in the first version, which queued every unsaved draft. A
write the server takes before it stops answering can still be replayed, and when
two tabs both fail to save one draft, the queue keeps the last-closed tab's words.

Behavior change: words typed into a page whose server is down now outlive the
tab, in the same browser.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A page that Back restores from the back/forward cache leaves its queue entry behind, and a later load can then bring back words the user already sent or put away. pagehide fires with persisted: true when the user leaves the page by a link. Leaf keeps such a page in the cache on purpose (CACHE_CONTROL in src/leaf/http.py), and when the server is down the page usually has no request open, so the cache keeps it. Suppose the user types while the server is down, leaves by a link, and comes back with Back. The restored document still holds the words in draftCache, delivers them once the server answers, and the user sends the comment, so the server now holds no draft for that context. The queued entry is still in writingStore. The next load or new tab of the page in this browser puts it into draftCache, and hearReading keeps it because incoming === null. The sent comment's words reappear in the box and are written back to the server as an unsent draft. This is the same resurrection the writing.has(ctx) exclusion prevents, reached by another path, and the two remaining cases the PR describes don't include it.

The restored document still holds the words it queued, so it can take its own entries back out on a persisted pageshow. Entries another tab queued stay. Words in a cached page that is later evicted are still queued, because the entry is only removed when the page comes back. The suggestion below does that in about seven lines.

Comment thread browser/runtime/drafts.js Resolved
…undo it

A write can reach the drafts store long after it was made: a closing tab's resend,
or now a write a tab queued in its browser while the server was out of reach. The
store kept nothing of a draft it settled, so such a write landed over "none" and
stood words up again that the user had sent or put away. Codex found this in the
browser queue twice, once for a write still out at close and once for a failed
write whose resend got through, and the same gap let a closing tab's resend undo a
Cancel made in another tab, which the drafts review had left as too narrow to fix.

The store now keeps, per draft no longer held, the last generation it settled and
the version it settled at (`settled`, optional, so a store from before reads as
one with none). A write of that generation settles it again. A replay names the
store version its page had heard when it queued the words (`since`), and the store
refuses it where the settlement came later and the replay doesn't name the
settled generation, since its words were replaced before they were settled. A
live write sends no `since`, so words a tab types over a generation settled
elsewhere still start the draft again.

With replays safe at the store, the browser queues every unsaved draft again,
including one with a write still out, and a page that closes while replaying
queued words queues them anew.

Behavior change: a Cancel or send stays settled whatever late write of its words
arrives; readings carry no settled records.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The test job is red because this push changes the drafts record's schema (the new settled map) without regenerating build/browser/protocol-baseline.json. node build/browser/build.mjs --check reports the file stale, and a rebuild (pnpm run build:browser) changes only the drafts line in it. The protocol comparison finds no break: an older Leaf at since 46 ignores settled, so no bump is needed. I can push that one-line regeneration if you'd like.

The description now argues against the design this commit adopts. It says a draft with a write still out is left to the resend because "the store keeps nothing of a draft it settled", and that closing the two remaining cases "needs the store to remember every settled draft, which this leaves out." The store now remembers the last generation it settled of each draft, the writing.has(ctx) exclusion is gone, and the replay carries since. The squash commit takes the description, so it would record the opposite of what lands. Its "near 60 lines" figure should also be recounted with the store change included, since that was the condition Max chose this on.

The store-side memory also covers the back/forward-cache path from the earlier review. A replay of a generation the store settled settles it again, and a replay of words replaced before a later settlement is turned away by since. So that thread is resolved without the browser-side patch.

@max-sixty max-sixty closed this Nov 16, 2026
max-sixty added a commit that referenced this pull request Nov 18, 2026
…yped on arrival stand

A reload while a draft write was still out was served the older generation the
server held, and the page it replaced resent the newest words on `pagehide`.
Where the resend landed first, the server refused the reload's write over the
older generation and answered with the resend's words, which the reload took
for another tab's: the words typed after the reload went. This is the nightly
macOS failure of
`test_a_kept_draft_and_an_input_method_reach_the_editor_as_it_loads` (#6392),
whose earlier fix on this branch only waited for the write before reloading;
the test presses as a fast user would again.

The page now also leaves in the tab store, for each draft, the generations its
writes that may still land carry or name, and its copy of a draft still
waiting. A reload takes them before hearing the drafts it was served. It shows
the newest words at once, and the words it has yet to send go over any of
those generations, whichever lands and when, until the server takes a write of
a generation outside them; a further reload hands them on until then. A
generation the page was still sending hands on its generations but not its
copy, since only the server's answer says whether the log took those words. A
settled copy comes across too, so words sent just before a reload no longer
show in its box as the earlier generation the server still held. Only a
reload takes what was left, once: a later arrival could replay words put away
elsewhere since, which the server keeps no record of (the failure that closed
#6221).

Left open: the server takes any write into an empty slot, so a Cancel in
another tab during the reload, after the departing page's resend lands, comes
back with the reload's replay, as a retried write already could. Closing it
needs the store to remember the generations it settled.

Independent review (Codex, four passes) found three orders the first versions
lost words in: the reload stopped counting the departing page's writes after
the server's first answer, a send cut short by the reload handed on nothing
(and a second reload dropped what the first inherited), and an accepted
rewrite of an existing generation retired the set too early. Each has a test.

Tests, against the base runtime and with this change:
- resend lands first (Chromium): server ends "Kept", watch reports "K words"
  left the screen; passes.
- Codex's late-resend order (Firefox, whose routes hold the keepalive resend
  that Chromium's don't see): no reload write to order; passes.
- send cut short, two reloads (Firefox): server ends "Kept"; passes.
- server loses its drafts mid-window (Firefox): server ends "Kept"; passes.
- send answered just before the reload (Chromium): box shows "K" on arrival;
  passes.
The original test also passes under `taskpolicy -b` with `--cpu-throttle 8`.
max-sixty added a commit that referenced this pull request Nov 18, 2026
…yped on arrival stand (#6399)

Refs #6392

Words typed just after a reload could be replaced by the page the user
left. A reload while a draft write was still out was served the older
generation the server held, and the departing page resent the newest
words on `pagehide`. Where the resend landed first, the server refused
the reload's write over the older generation and answered with the
resend's words, which the reload took for another tab's, so the words
typed after the reload went. That is the nightly macOS failure of
`test_a_kept_draft_and_an_input_method_reach_the_editor_as_it_loads`
("typed words left the screen"). This branch first made that test wait
for the write before reloading; it now fixes the runtime, and the test
presses as a fast user would again.

## The change

On `pagehide`, the page also leaves in the tab's session store, for each
draft, the generations its writes that may still land carry or name, and
its copy of a draft still waiting (`browser/runtime/drafts.js`). A
reload takes them before it hears the drafts it was served
(`takeWaitingDrafts`):

- It starts from the copy, so the newest words show at once.
- The words it has yet to send go over any of those generations,
whichever lands and when (`superseded`). Words another tab or device
wrote still win, by the rules every reading follows.
- It stops counting them once it hears the server take a write of a
generation outside the set, since those writes can then land only into
an empty slot. Until then a further reload hands them on.
- A generation the page was still sending hands on its generations but
not its copy, since only the server's answer says whether the log took
those words; the reload shows what it was served.
- A settled copy comes across too, so words sent just before a reload no
longer show in its box as the earlier generation the server still held.

Only a reload takes what was left, and only once. A return through
history can come long after the writes landed, and the server keeps no
record of words put away since, so a replay then would stand them up
again (the failure that closed #6221). `browser/runtime/storage.js` now
says the tab holds unsent words only from a page to its reload.

## What it leaves

The server's draft store forgets settlements: `write_draft` takes any
write into an empty slot, so a write that arrives after its generation
was put away stands it up again. The handoff adds one such path: if
another tab puts the draft away during the reload, after the departing
page's resend has landed, the reload's replay brings the words back. The
same already happens without a reload, for a write whose answer was lost
and which a look retries after another tab put it away. Closing both
needs the store to remember the generations it settled, as the log
remembers the sent ones; this change doesn't attempt that. The
`drafts.js` header now says the server takes a write "only over one it
names or over none".

## Tests

Five tests in `tests/browser/drafts/test_shared_drafts.py` hold the
departing page's writes and the reload's writes and reads with
`page.route`. Chromium's routes don't see the keepalive resend, which
lands as the page goes; Firefox's do, so the three tests that need the
resend to land late run in Firefox. Each reload runs inside
`restarting`, since Firefox warns of every module load a reload cuts
short, such as the view checks a page imports after it presents; that
helper's docstring now names a reload.

| Test | Order it forces | Base `drafts.js` |
| --- | --- | --- |
| `…stand_over_the_resend_of_the_page_it_left` (Chromium) | resend
lands, then the reload's writes | server ends "Kept"; watch: "K words"
left the screen |
| `…stand_however_late_the_page_it_left_writes` (Firefox) | the write
that was out lands, the reload's first write is refused over it, the
resend lands, then the reload's next write | no reload write to order |
| `…stand_over_the_send_it_cut_short` (Firefox) | a send cut short by
the reload, a second reload, then the resend | server ends "Kept" |
| `…stand_over_a_resend_landing_after_a_rewrite` (Firefox) | a send cut
short, the server loses its drafts and the reload writes "K" back, then
the resend | server ends "Kept" |
| `test_words_sent_just_before_a_reload_do_not_come_back_with_it`
(Chromium) | a send answered just before the reload | box shows "K" on
arrival |

All five pass with the change. The three Firefox tests also fail against
this PR's earlier unpushed revisions, whose gaps an independent review
found. The original test passes under `taskpolicy -b` with
`--cpu-throttle 8`. `pnpm run test:runtime` and `pnpm run check` pass.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 19, 2026
Words typed while the page's server was out of reach, or on an exported
page, stayed in the tab until a write got through, and closing the tab
or leaving the page lost them silently. Leaving now asks first, through
the browser's own prompt, whenever a draft holds words the server hasn't
taken and no write is carrying, so the user decides whether they still
want them. Words a write is carrying need no prompt, since the page's
going away sends them again.

To weigh: this asks rather than keeps. Keeping the words in the browser
past the tab was built twice, in #6221 and again on a branch today; each
time a late replay brought back words sent, cancelled or written over
elsewhere, and five rounds of review kept finding more. A safe replay
needs the store to know every write it ever took (each tab numbering its
writes, the store keeping the highest number per tab), estimated at
130–180 lines and a protocol bump. A reload also prompts while words
wait, since the prompt can't tell it from a close, though the tab's
reload keeps them.

max-sixty asked for Leaf to keep typed work, simply and robustly.

Tests: a runtime test that leaving asks while a write has failed and not
once one lands, which fails without the change; checked by hand in
Chromium and Firefox that a reload prompts while draft writes are
refused and doesn't once the server has the words. `pnpm run check`,
`pnpm run test:runtime` and the drafts browser tests that reload or
leave pages pass.

> _This was written by Claude Code on behalf of max-sixty_
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