Skip to content

Keep drafts on the page's server, so they follow the user between tabs and devices - #6124

Merged
max-sixty merged 8 commits into
mainfrom
canonical-drafts
Nov 16, 2026
Merged

max-sixty merged 8 commits into
mainfrom
canonical-drafts

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

Unsent drafts move from browser localStorage to the page's server, so a draft follows the user to another tab, browser or device. Follows #6060 and #6091.

  • src/leaf/drafts.py keeps drafts.json in the page directory, outside the log: one generation per draft context. A write names the generation it replaces (compare-and-set), so another tab's or device's later words come back instead of being overwritten. Admitting an event settles the draft its attempt came from, and the store remembers recent admitted attempts so a write landing after its send settles too. No page reading, activation or watcher counts the file, and the agent never reads it.
  • Tabs hear changes through the existing news look (/api/news carries a drafts version, then /api/drafts), and a served document arrives with its drafts, so a box opens on its words at once. drafts.js keeps one write in flight per draft, and drops the cross-tab storage machinery (storage listener, nondurable branches, tombstones, refresh-before-send, settleAcceptedDrafts).

What changes for the user, to weigh:

  • Words typed while the server is out of reach, or still on their way, stay in their tab and go if it closes first; a closing page resends what's waiting with keepalive.
  • On leaf.page, drafts are saved with the page at each send and answered from there while its container sleeps, so a tab opened then shows them without waking it; a container replaced with no tab open loses words typed since the last send.
  • A tab that missed newer words sends what it shows, and the newer words stay the draft. Two places writing one draft at once keep the first write; the other's words since give way.
  • Rarer losses Codex reproduced in schedules: a store restored and advanced past by another tab before this tab looks; and a ghost of sent words, from a write held past 64 later sends or a crash between a send's append and its settlement, winning over fresh words.
  • Each write rewrites the page's drafts file, which holds only unsent drafts. AGENTS.md's rule that a draft being typed stays with the browser now says the server keeps it.

Max asked for this. Codex reviewed it three times; its findings on stale reads, a restore race, moves, leaf.page's first draft, closing tabs, late writes of sent words and the words watch are fixed. Tests: tests/process/test_drafts.py; browser tests for another device, a server that lost drafts, a stale read and a refused move, each failing without its fix; the cross-tab tests rewritten from storage faults to refused writes and reads. test_refused_reply_retains_its_live_foot[margin] fails on main since #6091 and here alike.

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

…s and devices

A draft lived in the browser's localStorage, so it reached every tab of one browser but
not the user's phone or another browser. The page's server now keeps each draft in the
page directory (`drafts.json`, `src/leaf/drafts.py`), outside the log, where every tab
and device reads and writes it:

- A tab keeps its copy of each draft and writes it to `POST /api/draft` behind the
  keystroke, one write in flight per draft with the latest words waiting behind it. A
  write names the generation it replaces, and the server takes it only over that one or
  none, so words another tab or device wrote meanwhile come back in its place.
- Tabs hear changes through the look that tells them of the log: `/api/news` carries
  the drafts' version, and a tab reads `/api/drafts` when it moves. A served document
  arrives with its drafts (`lf-drafts`), so a box opens on its words before any look.
- Admitting an event settles the draft whose attempt it carries, inside the page's
  transaction, and a settled draft leaves the store, which holds only what the user is
  writing. A tab that hears an older store than it knew, as from a server restored from
  an older copy, gives its words back.
- No reading of the page, activation or watcher counts `drafts.json`, and nothing the
  agent reads includes it; render-check captures serve none. leaf.page saves it with
  the page each time the visitor sends something.

This removes the browser-storage machinery drafts needed between tabs: the storage
listener, durable and nondurable branches with their bases, tombstones written because
`removeItem` can fail, the refresh before each send, and the pass that settled accepted
drafts after every poll (`settleAcceptedDrafts`). Resume writing's place stays in the
browser (`writingStore`, the former `draftStore`).

Behaviour that changes: words typed while the server is out of reach stay in their tab
until it answers, and are lost if that tab closes first; a tab that missed another's
newer words sends the words it shows, and the newer ones stay the draft; two places
writing one draft at once keep the first write the server takes. The user asked for
this on 15 November 2026, accepting the first. AGENTS.md's rule that a draft being typed
stays with the browser now says the page's server keeps it.

Tests: new `tests/process/test_drafts.py` (the store's rules, settlement on admission,
news and document carrying drafts but not `page state`); browser tests for another
device and for a server that lost drafts; the cross-tab tests rewritten from storage
faults to refused draft writes and reads; the typed-words watch reads the drafts a tab
writes and hears as it read storage.
# Conflicts:
#	tests/browser/drafts/test_shared_drafts.py
…settle ghost drafts

A review of the server-kept drafts found ways to lose words:

- A drafts read the server answered before this tab's later write landed could hand a
  draft with unsent words an older generation, which replaced them. Every draft now
  keeps what it heard at the read's version or later, unsent words included. A store
  that went back, as a server restored from an older copy, is now told by a look
  answering below the version the tab had heard when it set out (`lookForDrafts`),
  rather than by any read older than a write's answer.
- Words moving into the page's draft settled the draft they came from at once, so a
  destination write the server turned away lost them. The source now settles only
  once the server holds the words there (`moving`).
- A leaf.page visitor's drafts waited for a send to start their private page; the
  first draft write now starts it.
- A page going away now sends the words still waiting, with `keepalive`.
- A generation the log took whose own write landed after its event, from a tab that
  closed before settling it, stood as a ghost draft; a tab that hears of it settles it.
- The typed-words watch credited a settlement older than the tab's latest write, and
  writes the server failed or turned away; it now counts only what the server says
  after a write, and writes still out.

Tests: `test_a_reading_older_than_a_write_s_answer_leaves_the_words_typed_since` and
`test_words_whose_box_is_gone_stay_put_when_the_page_s_draft_turns_them_away`, each
failing without its fix.
… the watch's credits

A second review of the drafts fixes found:

- A move settled its source on any written answer for the destination, including an
  older write's, and kept waiting after another device's words were adopted there.
  The source now settles only once the server takes the generation carrying the moved
  words or a later one written over it, and adopting another generation ends the move.
- An answer to a write sent before the store went back could mark words durable that
  the restored server lacks. Answers to writes sent before a restore are now ignored,
  and the words are written again.
- Ghost drafts were cleaned by a sweep of the log in every tab, which a reading could
  miss. The server now remembers the attempts it admitted most recently (`sent`) and
  settles a write of one that lands after its event, so no tab has to.
- A closing page skipped a write already out, which leaving may cancel; it now sends
  every waiting draft again.
- The typed-words watch took a reading asked for before a write as news after it, and
  one write's answer cleared another's credit; it now counts a reading or answer only
  where no later write of the draft went out, and each write's credit apart.

Test: `test_a_write_that_reaches_the_store_after_its_send_settles_it`.
…re a write excuses nothing

The last review found the typed-words watch could take a drafts reading the server
served before this tab's write, but answered after it, as news that another document
replaced the words. A reading now speaks of a draft only at a store version no older
than the one the watch last heard of it, and, while a write of it is out, only of
another document's words, which the page takes over that write as the server would.

@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.

tests/process/test_interact_server.py::test_a_page_snapshot_stays_on_one_page_reading fails at this head. /api/news now always carries drafts, so the snapshot server's answer is {"reading": ..., "pages": None, "drafts": 0}, but the test still asserts _news(server.origin) == {"reading": snapshot.reading, "pages": None}. That line isn't in the diff, so there's no inline suggestion. The fix is to add "drafts": 0 to the expected dict. The test then also pins that a capture reports no drafts (_drafts returns version 0 when the source isn't writable). This failure is separate from the [margin] one the description mentions as inherited from main. I can push the one-line fix if you'd like.

The rest of what I ran passes: tests/process/test_drafts.py, the rest of test_interact_server.py, pnpm run test:runtime, and five of the new cross-device/stale-read tests in tests/browser/drafts/test_shared_drafts.py.

A capture serves no drafts, so its news answer carries version 0, which the snapshot
test now pins; the startup test that fakes news answers carries it too.
leaf-agent
leaf-agent previously approved these changes Nov 16, 2026
… format the site page

CI's run hung in test_a_reading_older_than_a_write_s_answer_leaves_the_words_typed_since,
whose route handler fetched the held read while a later wait could block on it. The
test now takes the stale answer through the context's request API and holds the route
alone. docs/website.html takes prettier's wrapping.
leaf-agent
leaf-agent previously approved these changes Nov 16, 2026
The site check caught a tab opened while its container slept waking it to read
/api/drafts, which leaf.page didn't save. Each page source now answers its drafts:
the live page reads the store, and a capture what its taker froze, which is none for
a browser check or export and, for leaf.page's saved answers, the drafts the
replacement container restores. So a tab opened while the container sleeps shows the
drafts as last saved, and types over them rather than being refused when its write
wakes the container. A document served without drafts starts at the empty store's
version 0, so it reads them only once the news names a write.
@max-sixty
max-sixty merged commit 8b68c6d into main Nov 16, 2026
5 checks passed
@max-sixty
max-sixty deleted the canonical-drafts branch November 16, 2026 08:41
leaf-agent added a commit that referenced this pull request Nov 16, 2026
#6124 moved drafts to the page's server and bounded a write's `context`
at 1024 characters. A selection composer's context is `composer:`
followed by its anchor, and the anchor quotes the passage. So for a
comment on a passage longer than about a thousand characters, the server
refused every draft write with 400. That draft never reached the server,
so another tab or device never saw it. In
`test_a_selection_of_the_whole_page_still_finds_its_passage`, select-all
gives a 16,713-character context, and the 400s fail the test at
teardown. That failure is on `main` from #6124 on ([run
43835820738](https://github.com/max-sixty/leaf/actions/runs/43835820738)).

The context now has no upper bound, because the anchor it names has
none. The stored record's shape is unchanged.

The test fails on `8b68c6d76` (#6124) and on current `main`, passes on
its parent `17948f1e1`, and passes with this change.
`tests/process/test_drafts.py` passes too.

The same run's other #6124 regression,
`test_a_back_gesture_never_throws_away_unsent_words` (the words watch
reports the back-closed editor's words as lost), has a different cause
and is not touched here.

Refs #3985 — automated triage

Co-authored-by: leaf-agent <[email protected]>
leaf-agent added a commit that referenced this pull request Nov 16, 2026
…ard (#6164)

The words watch (`tests/words_watch.js`) stopped counting a draft write
as still out as soon as the `/api/draft` response's headers arrived, but
recorded what the server holds only after it had read the response body.
A close checked between those two moments found the words in neither
`draftsOut` nor `draftsHeld`, so `stored(words)` was false and the watch
reported back-saved draft words as lost. Back's close lands in exactly
that gap, because closing the editor is what sends the write. This
change makes the write settle after its answer is heard, so the words
are always in one set or the other. Neither the runtime nor the server
changes.

Logging the watch during a run of `[draft]` showed this order: the
write's response arrives, then the close is checked with `held: []` and
`out: {edit:d: {}}`, and only then is the answer heard with
`record.text: "Unsent words."`. #6124 introduced the gap when it
replaced the watch's localStorage scan with tracking of `/api/draft`
traffic.

Verification:
`tests/browser/navigation/test_key_scopes.py::test_a_back_gesture_never_throws_away_unsent_words`
errored at teardown in both `[draft]` and `[rewrite]` with `-n0` on
`main` (`1740bb536`), and both cases pass after the change.
`tests/runtime/words-watch.test.mjs`, the draft-related cases of
`tests/browser/test_render_words_watch.py`, and the tab/close cases in
`tests/browser/drafts/` also pass.

Closes #6163 — automated triage

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Nov 16, 2026
Main's news answer now carries the user's drafts version (#6124); a fake carries every field the server sends.

> _This was written by Claude Code on behalf of max-sixty_
leaf-agent added a commit that referenced this pull request Nov 16, 2026
… value (#6189)

Two `seat-mirrored` cases of
`test_native_reply_edits_after_semantic_paint_keep_the_first_line` have
failed on main since #6124 moved drafts to the page's server. This
changes only the test; Leaf's behavior is unchanged.

- **Desktop and composing.** The journey asserted `saveDraft`'s return
value. Before #6124 that value said the localStorage write was durable.
Now `saveDraft` returns nothing, and the words reach the other tab
through the server's drafts news. The documented contract in
`skills/leaf/references/module-input.md` never promised a return value,
and no runtime caller reads one. The journey now calls `saveDraft` and
relies on the `expect(editor).to_have_js_property("value", "First
line")` that follows, which waits for the words to arrive.
- **Composing only.** The second tab wrote its words before it had heard
the first tab's composing words (`Composing`). Under #6124's
compare-and-set, the earlier write wins and the stale write gives way,
which matches the rule #6124's description sets out. So `First line`
never arrived. The second tab now waits for `loadDraft` to show
`Composing` before it writes. The journey still checks what it was
written to check: mirrored words land in a composing field without
moving the field or firing edit events.

Verification: on `f1b367375` (main), running the test function by node
id gives 2 failed, 6 passed, with the two `seat-mirrored` cases failing.
With this change all 8 pass.

Closes #6176 — automated triage

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Nov 16, 2026
…a container stops (#6213)

A review of how Leaf keeps drafts since #6124. The model stays: drafts
live on the page's server outside the log, and tabs hear them through
the news look. Two places where the drafts store kept a weaker copy of
something another part knew change.

- **The log answers which drafts were sent.** The store no longer
remembers the last 64 sends; it asks the log through
`event_log.admitted_attempts`, an index each server process keeps over
the append-only log, about 20 µs a question on any log. The first
reading or write after a send settles its draft and moves the drafts
version, so admission no longer touches the store, and sent words can't
come back however many sends came between or wherever the server
stopped. The browser stops walking every event of the session on each
draft lookup.
- **leaf.page saves drafts when a container stops.** Each page whose
drafts moved since its last publication is published from the stop
handler, so words typed since the last send survive the 10-minute sleep.
The handler closes the listening socket first.

To weigh: a `/api/news` or `/api/drafts` read now writes `drafts.json`
once after a send. The drafts and website-publication records move to
format 46. A Cancel in one tab can still be undone by another tab's
closing write that lands after it, left as too narrow for a mechanism.

Max asked for the review. Codex reviewed twice; its two findings are
fixed. Tests: `tests/process/test_drafts.py` and
`tests/process/website_server/test_saved_records.py` gain a case each,
failing before; the drafts browser tests 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