Skip to content

Keep a tab's unsent words beside another tab's, rather than dropping them - #6454

Merged
max-sixty merged 4 commits into
mainfrom
keep-both-drafts
Nov 19, 2026
Merged

max-sixty merged 4 commits into
mainfrom
keep-both-drafts

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

When two tabs or devices wrote one draft before hearing each other, the tab whose write the server refused dropped its unsent words. This keeps them. The draft becomes both: the longer text where one holds the other (trimmed), otherwise the server's words, a blank line, then the tab's. The tab writes that as a new generation, which every tab then converges on, with its own payload if it has one. adopt in drafts.js was the one place that gave up words the server never took, so the rule lives there and covers a refused write, a reading that meets unsaved words, and a reading of a store that replaced the tab's. Words alone decide whether to write: the selection composer's payload records when it was last touched, so letting it decide made two tabs write over each other without end.

Words being sent still give way, since the log may already hold them and a refused send leaves a resend in its thread. A page going away now leaves its reload no copy of a saved draft it is sending, as it already did for an unsaved one; otherwise a reload into a new store took those sent words in as unsent.

To weigh: with no base text, a join can bring back text the other device had replaced or deleted. When the user keeps typing in the other tab after a join, that tab joins again and repeats the words it started from: if a join made the draft Y, W (two paragraphs) and the other tab's Y has meanwhile become Y more, the draft ends as Y, W, Y more, where Y more, W would do. If the other tab sends or cancels its words while the refused write's answer is on its way, they come back in the joined draft for the user to delete. Keeping the text each copy started from, or joins that record their sources, would avoid these, at a cost every copy pays for a path users rarely take; none of them loses a word.

max-sixty asked for the simplest robust rule, with no new UI. Codex reviewed it; its two convergence and handoff findings are fixed here, and a tab closed mid-conflict still loses its words, which a later change that keeps words past a tab's close will cover.

Tests: the rewritten and new tests in tests/browser/drafts/ and tests/runtime/drafts.test.mjs fail on the old drafts.js, except two that pin what a join must not do. pnpm run check, pnpm run test:runtime and the drafts browser selection pass.

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

…them

A tab holding words the page's server never took, which met another tab's or
device's generation of the draft that those words don't go over, took the server's
generation and dropped its own words (`adopt` in drafts.js). SOUL.md now says Leaf
drops typed words only once it knows the user no longer wants them, and nothing
says that of these.

The draft now becomes both: the longer text where one holds the other once both are
trimmed, else the server's words, a blank line, then the tab's. The tab writes that
as a new generation over the server's, so every tab converges on it, and shows it
with the caret at its end. Its payload is the tab's if it has one, else the
server's. Where the server's words already hold the tab's, nothing changes. The
maintainer chose this keep-both rule over a three-way merge, which would keep the
text each copy started from, because conflicting drafts are rare.

`adopt` is the one place this happens, and it is reached from a refused write's
answer (`heardWrite`), from a reading of the same store that meets an unsaved copy,
and from a reading of a store that replaced the copy's. Words the tab is sending
still give way, since the log may already hold them. `heardWrite` now asks
`replaces`, as the readings do, whether the tab's words go over the generation the
server holds, so a write answered after a reading already joined the words doesn't
join them a second time.

Behaviour that changed, and its tests:
- test_an_unsaved_branch_yields_to_another_tab_s_later_words is now
  test_unsaved_words_join_another_tab_s_later_words_in_every_tab: both tabs and the
  server end with the later words, a blank line, then the unsaved ones.
- test_words_whose_box_is_gone_stay_put_when_the_page_s_draft_turns_them_away is now
  test_words_whose_box_is_gone_join_what_another_device_wrote_on_the_page: words
  moving into the page's draft join another device's words there, with those the
  draft held as they moved, and their source settles once the server holds both.
- New: test_a_reload_into_a_replaced_store_keeps_its_words_beside_another_device_s,
  and tests/runtime/drafts.test.mjs for containment, the payload rule and a send in
  flight giving way.

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

Where the server's words already held the tab's, `adopt` let the server's record
stand even when the tab had a drawing or other payload the server's lacked, which
dropped it. The tab now writes the joined generation whenever text, held reply or
payload differ from the server's record, and lets the server's stand only when they
are all the same.

The header now says why: words written apart are rare, and the maintainer asked for
the simplest rule that loses none, where a three-way merge would need every copy to
keep the text it started from.

New: the payload test covers a drawing the tab holds over a server text that
already holds the tab's words.

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

Review found that letting a payload decide whether to write a joined generation
made two tabs write over each other without end: the selection composer's payload
records when it was last touched, so it differs between tabs whatever they hold.
Words alone decide again, as the first version of this change had it, and a test
pins that words already standing write nothing more, whatever their payload. A
drawing only this tab holds is lost where the server's words already hold its own,
which the join's convergence costs.

A page going away left its tab a copy of a saved draft it was sending, though the
module header already said a generation being sent leaves no copy, since only the
send's answer says whether the log took it. A reload into a store that replaced
this one then took those words in as unsent and joined them beside another
device's, so sent words came back. The saved branch now leaves no copy either, as
the unsaved one did.

The header also names the cost a join accepts: where another tab sends or cancels
its words while the answer carrying them is on its way, they come back in the
joined draft for the user to delete.

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

When the other tab is still typing as the join lands, the second join duplicates that tab's words, and this happens in the tab the user is using. Say tab B holds Y as stored, tab A's refused write joins it into Y\n\nW, and the user types on in B, so B's copy reads Y more. B's next write expects Y and is refused. adopt then joins Y\n\nW with Y more. Neither contains the other, so the draft becomes Y\n\nW\n\nY more, and keepWritingPlace moves B's caret to the end while the user is typing. Before this change, A gave its words up and B never conflicted. I reproduced this in the runtime harness from tests/runtime/drafts.test.mjs: saveDraft(ctx, "Y") is accepted, then writtenElsewhere(ctx, "Y\n\nW"), then saveDraft(ctx, "Y more"), and the server ends holding "Y\n\nW\n\nY more". The same cascade also occurs when both tabs hold unsaved words and both join: the second join repeats the server's words.

The PR body's "To weigh" section doesn't list this cost. Of the paths it covers, the moment just after a conflict, while the user keeps writing on the other device, seems the likeliest. The case the join misses is the common one: B's words extend the very generation that A's join already holds. So the cheapest fix may be for a copy to keep the text of its stored generation. A join over a record that contains that text would then put the copy's words in its place rather than appending them. That needs no recorded sources and no full three-way merge. Whether it is worth the extra field against the rule the maintainer asked for is their call, so I'm leaving the verdict to them.

@max-sixty

Copy link
Copy Markdown
Owner Author

Thanks, the repeat is real. I'm keeping the rule as it is: the maintainer asked for the simplest robust rule for this rare path, and the repeat keeps every word, while keeping the text each copy started from would add a field every copy carries and every handoff writes. The description's "To weigh" now lists it with the other costs a join accepts.

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.

Approving. The repeat when the other tab keeps typing was the only thing my review raised, and you've settled it. It costs no words, and the description's "To weigh" now lists it beside the join's other costs. The drafts runtime tests pass at 9ed4b158.

@max-sixty
max-sixty merged commit e2db751 into main Nov 19, 2026
6 checks passed
@max-sixty
max-sixty deleted the keep-both-drafts branch November 19, 2026 01:52
leaf-agent added a commit that referenced this pull request Nov 20, 2026
…r field (#6587)

The words watch reported a false loss when a draft join kept the user's
words in their own field. Since #6454, a tab whose draft write never
reached the server joins its unsent words to another tab's later ones,
so the field goes straight from `mine` to `theirs\n\nmine` (`bothWords`
in `browser/runtime/drafts.js`). The watch decided whether a field still
held its words by exact equality, so it read the join as the words
leaving the screen. Its other-tab credit only sometimes rescued the
test, depending on whether the joined write went out before the field
repainted. That made
`test_unsaved_words_join_another_tab_s_later_words_in_every_tab` fail at
teardown in [run
44666096308](https://github.com/max-sixty/leaf/actions/runs/44666096308)
with `typed words left the screen without a key or press`.

The watch now counts a drawn field whose text still contains the held
words as holding them, and remembers that longer text, so a field a
re-render puts in its place showing the same text inherits the words, as
one showing the bare words already did. A text that drops them is still
reported, on either field. Nothing in the product changes.

Verification:
- `leaf-dev suite flake <that test> --cpu-throttle 8`: 0 of 18 copies
passed before, 18 of 18 after. It fails the same way on #6454 itself, on
the run's commit and on current `main`.
- A probe that sampled the field every frame recorded the same three
values in all 18 copies: empty, the typed words, then the join. No frame
shows the words gone.
- The new cases in `tests/runtime/words-watch.test.mjs` fail before the
change and pass after it. They check that a join keeps the words, in the
same field and in a replacement showing the joined text, and that a
later text without them is still reported.
- `tests/browser/test_render_words_watch.py` and
`tests/browser/drafts/test_shared_drafts.py` pass: 139 tests.

A field whose new text happens to contain short typed words now counts
as still showing them. A loss that removes the words from the text is
still caught.

Refs #6418 — automated triage

---------

Co-authored-by: leaf-agent <[email protected]>
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