Skip to content

Save each leaf.page draft before answering it, and name the store a draft's version counts - #6441

Merged
max-sixty merged 7 commits into
mainfrom
drafts-store-identity
Nov 19, 2026
Merged

max-sixty merged 7 commits into
mainfrom
drafts-store-identity

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

On leaf.page a draft write was answered before anything outside the container held it, and every release started a new drafts store. The browser guessed when the store had gone back by comparing a version counter that restarts with each store, and the guess misfired: a reload after a new store re-sent its words in a loop (1,903 requests in 3 s, on main since #6399), a hidden tab could clear a draft once another device's writes carried the counter past what it had heard, and a detected restore wrote over another device's later words.

This removes the doubt rather than guessing better. leaf.page answers a draft write only once the session's Durable Object holds it, through a drafts-only operation that takes a write only over the version it replaces. No page commit carries drafts, so a late commit can't put an older store back, and a sleeping session serves the stored drafts in place of the ones its last commit captured. drafts.json carries a store id, which every reading and write answer names, and drafts.js orders versions only within one store; a tab meeting a new store writes its words where that store holds nothing for their draft. The counter guess (restoring, restores, the floor in lookForDrafts, the restored branch) and the stop handler's save_drafts are deleted.

It also adds a sentence to SOUL.md: Leaf drops typed words only when it knows they're unwanted, and otherwise keeps them where the user can find them. In a temporary A/B, maintainer agents given it kept both tabs' words in a refused-write case in 4 of 5 runs, against 1 of 5 without it.

To weigh: a draft write on leaf.page now waits on a round trip to the Durable Object, measured only against the loopback double (median 1.2 ms against 0.7 ms). A write another tab beat still drops the refused tab's words, and a release still starts each visitor's drafts over, as it does their threads; both are left for later changes.

max-sixty asked for the fix; Codex reviewed the build; its finding that a late save could overwrite a newer one is folded in as the compare-and-swap above.

Tests: new process, website-server, browser and worker tests each fail on a revert of their fix; pnpm run check, pnpm run test:runtime and focused suites pass.

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

…ting

A reload asks for its document before the page it replaces goes away. Where that
page then heard the server take its last draft write, it held the draft as durable
and left the reload nothing, since #6399 leaves only words still waiting. The reload
showed the generation it was served, older than the one the server now held, until
its first look. Words typed on arrival went over that older generation, and the
server's answer replaced them with the departing page's words, which the reload took
for another tab's. test_held_keys_stand_beside_a_saved_draft read "H" for "Half a
thought" in 2 of 13 runs of its selection at -n 3 on a loaded Mac (2026-11-18), with
#6399 in.

The page now leaves its durable copies too, still marked durable so the reload
doesn't write them again, and a served generation older than a copy gives way to it
by version, as any reading does.

A store that went back as the page reloaded, as one restarting from an older copy
of its page does, isn't heard by the reload: a durable copy shows words the server
lost and isn't written back. Main drops those words at the reload. Taking
the copies' versions as the store version the reload has heard would catch it, but
a review found that suppresses reads of other drafts a copy's version says nothing
about, so this change leaves it.

Tests: test_words_typed_on_a_reload_stand_over_a_write_taken_as_it_was_served
serves the reload a document fetched before the write lands, and fails on main
("K" for "Kept"). test_held_keys_stand_beside_a_saved_draft no longer waits for the
save before it reloads (#6416's step); its selection passed 8 of 8 runs at -n 3.

> _This was written by Claude Code on behalf of max-sixty_
…raft's version counts

On leaf.page a draft write was answered before anything outside the container held
it: drafts reached the Durable Object only beside a page commit or in the stop
handler's `save_drafts`, and every release started a new store. The browser guessed
that the store had gone back by comparing version counters across store
incarnations, and the guess misfired. The audit of 2026-11-18 reproduced four losses:
a reload's copies with the old store's versions re-sent their write 1,903 times in
3 s and never wrote durable words back (P1), a hidden tab cleared words when other
writes carried the version past its floor (P2), and a detected restore overwrote
another device's later words (P3).

Two changes remove the uncertainty instead of guessing better.

An answered draft write survives the container. `drafts.py` replaces `drafts.json`
only after the host's commit provider holds the store (`publish_drafts`), so a write
is answered once the Durable Object has it. The provider is a new Durable Object
operation, `PUT /drafts`, which replaces one row per page. While the container sleeps
the object serves that row as `api/drafts` and as `api/news`'s `drafts`, in place of
what the last page commit captured, and `GET /records` carries it for restore. A
page's record leaves `drafts.json` out, so a commit that lands after a draft write
cannot put an older store back. `LEAF_PAGE_COMMIT` now names a module whose `publish`
commits a page transaction and whose `publish_drafts` commits a drafts write
(`service.commit_provider`), so a host that keeps pages elsewhere can't keep one
without the other. On the loopback store double a draft write took a median 1.2 ms
with the drafts commit and 0.7 ms without (200 writes, 2026-11-18), where the audit
measured the whole-page `publish` at a median 165 ms. The round trip through the
real outbound handler to the Durable Object is unmeasured.

The store says which store it is. `drafts.json` carries a `store` id minted when the
file is created, and every reading, write answer, served `lf-drafts` and the news'
`drafts` field carry it beside the version. A server restart keeps it; a replaced
page directory or a new leaf.page session store gets another. In `drafts.js` a
copy's version is ordered only against readings and answers of the store it was
heard from, and the tab handoff carries the store. A copy heard from another store
writes its words, or its settlement, into the new store where that store holds
nothing for its draft or a generation the copy replaces, and adopts any other
generation it holds. Within one store the version never goes back, so the
counter-floor inference is gone: `restoring`, `restores`, the floor in
`lookForDrafts` (now `hearDrafts`, taking the news' `{store, version}`), the
`restored` branch of `hearReading` and the `epoch === restores` guard.

Deleted: the stop handler's `save_drafts`, `publish(drafts=True)`, the publication
record's `drafts` version and the test of saving drafts at a stop. `drafts.json` and
`website-publication` move to `since` 51, the current page protocol, so no bump.

Behaviour that changed: a drafts-only leaf.page session (typed, never sent) has no
page record, so a reload after its container sleeps starts the container instead of
being served the record the stop handler used to publish. Its words are restored
either way.

Tests:
- `test_a_draft_write_is_answered_only_once_the_page_store_holds_it` (website
  server): a container that dies without its stop handler restores the answered
  write under the same store and version. Fails with the provider call removed
  (another store id, no words).
- `test_a_reload_gives_a_replaced_store_its_words_with_a_write_or_two_each`: a reload
  into a replaced store writes its durable and waiting words with 3 POSTs, and words
  typed after it reach the store. With the cross-store branch and the store check in
  `heardWrite` removed, the reload loops its writes and never settles (timeout).
- `test_a_store_keeps_its_identity_until_its_file_is_replaced`. Fails when a reading
  doesn't start the store.
- The worker's `serves and restores a page's drafts store, which no later commit puts
  back`. Fails with the overlay removed.
- `test_a_tab_gives_back_the_words_a_server_lost` deleted `drafts.json` to make the
  store go back, a state the store no longer reaches; deleting it now replaces the
  store, so it is renamed `test_a_tab_gives_its_words_to_a_store_that_replaced_its_own`
  and protects a live tab writing its words into a replacement.
  `test_words_typed_on_a_reload_stand_over_a_resend_landing_after_a_rewrite` keeps its
  assertions and now describes a replaced store.
- The audit's probes for P1, P2 and P3 no longer reproduce their losses (2 POSTs in
  3 s after the reload, where they recorded 1,903).

> _This was written by Claude Code on behalf of max-sixty_
The maintainer asked for this as one of Leaf's values: keeping the user's typed
work ranks high, so where Leaf meets draft words it isn't sure the user no longer
wants, it keeps them where the user can find them, and better still removes the
cause of the doubt. The sentence names sending and cancelling as how Leaf knows,
and says it holds on a rare path too, since "a rare path the user takes in at a
glance may move or redraw" otherwise reads as leave to drop words there.

Scored by a temporary A/B (2026-11-18, Opus 5.5, 5 runs per arm, each arm given
SOUL.md and AGENTS.md's "Weigh what a change adds" with the global CLAUDE.md):
- Asked whether to keep the words a refused draft write drops today, runs with
  the sentence recommended a change keeping both tabs' words in 4 of 5, against
  1 of 5 without it; one run without it accepted the drop under the rare-path
  allowance.
- Asked whether a tab should keep a copy of words the user had sent, both arms
  said no in 5 of 5.

> _This was written by Claude Code on behalf of max-sixty_
A drafts save that timed out could still reach the Durable Object after a later save
the container had acknowledged, and `saveDrafts` replaced the row whatever it held,
so a replacement container restored the older store and the acknowledged words were
gone. A "greater version wins" rule wouldn't do, since both saves can carry the same
version.

The Durable Object now holds the drafts store of record. A save names the store and
version it replaces (`replaces`, null for none), the object takes it only over that,
and it answers with the store it holds either way. The container's `drafts.json` is
its copy of that store: `drafts.py` runs each change under the store's lock
(`_change`), and where the object answers with another store, as after a save it took
whose answer never came back, the file takes the object's store and the change runs
once more against it, so the tab hears an ordinary answer. A second refusal fails the
write. `publish_drafts` takes `replaces` and returns the store the object holds.

Tests:
- worker `takes a drafts save only over the store it replaces, so a late save changes
  nothing`: two saves over version 1 with the same next version; the late one leaves
  the acknowledged row. Fails when `saveDrafts` replaces the row unconditionally.
- `test_a_drafts_save_answered_too_late_is_the_store_the_next_write_runs_on` (website
  server, whose page store double now takes a save only over the store it replaces,
  as the object does): a save the store took but whose answer was lost, then a write
  to another draft, which is answered at version 3 and survives a container replaced
  without stopping beside the first. Fails (version 2, the other draft lost) when the
  container ignores the store the object answers with.

The review's second finding, that a sleeping container's saved document embeds stale
drafts in `lf-drafts`, doesn't hold: `http.py` leaves `lf-drafts` out of every
release document, and a probe of a commit's captured documents (the page root, its
revision and version addresses) found none. The saved-records journey now asserts the
page's saved document carries no `lf-drafts`.

> _This was written by Claude Code on behalf of max-sixty_

@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 commit_provider docstring states the host contract as publish_drafts(page_dir, drafts), but drafts._change calls it with three arguments and uses what it returns: the store the host holds, which may be another store or none. A host written to this docstring returns nothing and takes no replaces, so every draft write would fail. The suggestion states the call as _change makes it.

Comment thread src/leaf/service.py Resolved
…tring

The docstring named publish_drafts(page_dir, drafts), but drafts._change passes
the store and version it replaces and uses the store the host answers with.

> _This was written by Claude Code on behalf of max-sixty_
leaf-agent
leaf-agent previously approved these changes Nov 19, 2026
Base automatically changed from queued-key-panel to main November 19, 2026 00:36
@max-sixty
max-sixty dismissed leaf-agent’s stale review November 19, 2026 00:36

The base branch was changed.

@max-sixty
max-sixty merged commit 1b2af39 into main Nov 19, 2026
5 checks passed
@max-sixty
max-sixty deleted the drafts-store-identity branch November 19, 2026 00:55
max-sixty added a commit that referenced this pull request Nov 19, 2026
…ring a rollout (#6457)

After a deploy, Cloudflare runs the new Worker while some session
containers still start on the previous image. The edge replaces such a
container's answers (`rollingOut`), but only once the container answers
with its `Leaf-Release`. Right after #6441 deployed (2026-11-19 01:06
UTC), previous-image containers read the new Durable Object's `/records`
snapshot, took its new `records` key for a page root, and exited on
`KeyError: 'records'` before serving. The edge passed the Durable
Object's 500 through, and a visitor on /how-it-works saw "Hit a problem
— trying again" until the rollout finished.

- Each page-store request now names its container's release. The
`leaf-state.internal` handler refuses another release with 409 before
any operation, and `restore` takes that refusal as a cue to start
without the session's record and without publishing. That one check
replaces the per-operation release check, so the `release` fields in the
publication and drafts bodies and the `leaf_pages.release` column are
gone.
- A server that runs a release now names it on every answer. Before, a
previous-image container asked for a page only the new release has
answered 404 without the header, and the edge passed that through too.

To weigh: the deploy of this change still crashes previous-image
containers once, since they send no header and treat the 409 as a
failure. A stale sample URL can still wake an older release's Durable
Object with the current image, which the header check accepts; that is
left as a follow-up.

Codex reviewed the first commit and found the 404 case. Tests: `pnpm run
check:worker`, `tests/process/website_server`, and the saved-record
journey test.

> _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