Skip to content

Keep a file draft whose save may have reached the file until a read settles it - #6787

Merged
max-sixty merged 8 commits into
mainfrom
agent-ad94527e2c73f18f1
Nov 23, 2026
Merged

max-sixty merged 8 commits into
mainfrom
agent-ad94527e2c73f18f1

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

A reader who typed a file back to its old text while a save of their edit was in flight, then reloaded or closed the tab, lost those words whenever the server had already written the save. The editor dropped its recovery record, and stopped asking before unload, once the buffer matched the acknowledged base.

The editor now keeps sent, the text of a save the server hasn't answered, in its state and recovery record. While it is set, the record stays and leaving asks first. A read, or a save's stale answer, that finds that text on disk takes it as the version the draft stands over. A record without sent fails: the line merge takes the save's words as a write from elsewhere.

Retry after a save with no answer follows from that. With nothing left to write it reads the file, where it used to do nothing and leave "Not saved" standing. With later typing it saves, and a 409 holding the lost save's text no longer merges those words back.

Reproduced in Chromium and Firefox by holding the POST, letting the server write, deleting the edit and reloading. A local save takes 3 ms, so it needs a slow one, such as a cold leaf.page container's first write.

Not covered: two saves in a row with no answer, where the file took the first and not the second.

Tests: the reload test and both Retry cases each fail before their fix and pass after; the neighbouring editor tests pass.

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

The reader types the file back to its old text while a save of their edit is
in flight, and the tab reloads after the server wrote that save but before its
answer arrived. On main the editor drops its recovery record as soon as the
buffer matches the acknowledged base, and beforeunload doesn't warn, so the
reloaded tab opens the save's text with nothing offering the reader's later
words back. This test fails on main: the editor reads "Original.Draft."
where the reader left "Original.".
…ettles it

The editor dropped its recovery record, and stopped asking before the page
unloaded, as soon as the buffer matched the acknowledged base, even while a
save of other text was in flight. A reader who typed the file back to its old
text during that save, then reloaded or closed the tab, lost those words when
the server had already written the save: the new tab opened the save's text
with nothing offering theirs back.

The editor now keeps `sent`, the text of a save the server hasn't answered.
While it is set the file counts as unsettled, so the record stays and leaving
asks first, and the record carries it across a reload. A save that gets no
answer keeps it too, since the reload cancels the request in the old
document, whose failure handler would otherwise clear it before the page goes.
The read after the reload takes that text on disk as the version the draft
stands over; that check joins the existing branches where the draft already
stands over what the read found.

Keeping the record without the sent text was tried and fails: the read then
line-merges the save's text into a draft that matches its old base and shows
the save's words, and a reader who kept typing after the save gets a
"Changed on disk" conflict with their own write. That case now saves cleanly
too.

A local save was in flight for 2.4 to 6.1 ms (median 2.9 ms, 25 saves, load
average about 40), so the loss needed a slow save to be reachable by hand;
a cold leaf.page container's 4 to 5 s first write is such a window.
…words

A refusal the server states, such as a file over the size limit, wrote
nothing, so keeping `sent` after it left a reader who then undid back to the
read text with a leave-page prompt that nothing on the page could clear. Only
a save with no such answer, as when a reload cancels the request, now keeps
its text until a read settles it. A probe that fulfils the POST with a 400
and the server's words prompted on the previous commit and no longer does; a
save the server took whose answer was aborted still prompts and is written
back to the reader's text after the reload.

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

Retry can still put a lost save's words back over later typing. When a save fails without a server answer and the file did take it, sent holds that save's text and the failure banner offers Retry. retry (in the keymap setup, operation?.kind === "save") calls flush, which sets of.sent = submitted before posting, so the lost save's text is gone by the time the 409 arrives. The catch then clears sent and settle(of, fault.current) merges the old base into the file's text. That is the merge the header rejects: with the test's steps but Retry instead of a reload, the reader's deletion of Draft. is undone. Letting retry read first while of.sent !== null would settle it with the rule refresh already has. refresh(of, returnFrom) gets past the failure, clears it and schedules the save. It would also make Retry act when the reader has typed back to the base, where flush currently returns early.

requestJson throws RequestError for a proxy's error page too, for example a 502 or 504 in front of a cold container. That error has no said, and the server may have written the save, so the check in the inline comment below clears sent in a case its own comment says it shouldn't.

A save that got no answer, which the reader then typed away, leaves "Not
saved" standing: Retry calls the save, which has nothing to write and returns,
and since the editor now keeps the unanswered save's text, leaving the page
asks with nothing on the page able to clear it. The new test fails on this
branch: Retry stays visible after it is pressed.

The reload test's receipt now matches its request by URL too. In Firefox the
page sends another POST whose body has no `text`, which made the predicate
raise.
A save that got no answer, which the reader then typed away, left "Not saved"
standing with a Retry that did nothing: Retry always called the save, whose
guard returned because the draft matched its base, and every read waits while
a failure stands. With the unanswered save's text now kept, leaving the page
also asked, and nothing on the page could clear either.

Retry now reads the file when the save would write nothing. The read settles
the kept text and clears the failure: the status empties and leaving no
longer asks, and where the unanswered save did reach the file, the read takes
it as the draft's base and the reader's text is written back. Whether a save
would write anything has one home, `unsaved`, which the save's guard and
Retry both call. The new test fails before this change and passes after it.

@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 new Retry condition settles the case where the reader typed back to the base, but not the one where they typed on past it, and the lost save's words still come back there. That case is the inline comment below.

The Retry test now runs two cases. In the new one the file takes a save whose
answer never reaches the page, and the reader deletes that save's words and
types a line far from them before pressing Retry. Retry posts the new text
against the old base, the server answers 409 with the lost save's text, and
the line merge takes that text in as a write from elsewhere: the save that
goes out holds `Draft.` again, which the reader had deleted. The case fails
on this branch with that save's text, and the file the test uses keeps the
two edits apart so the merge succeeds rather than showing a conflict.
…sion

Retry after a save whose answer was lost posts the reader's newer text against
the old base. The save replaced `sent` before posting, so when the server
answered 409 with the lost save's text, nothing recognised it and the line
merge took it in as a write from elsewhere, putting back words the reader had
deleted. The save now keeps the text it replaces, and a stale answer whose
file holds either that text or the text being sent becomes the draft's base,
as a read already takes it; the draft then saves itself.

Reading first whenever `sent` is set, as the review proposed, fixes the same
case but changes a failed Save draft retried with Ctrl+S: where that save
never reached the file, the read finds the other version and brings the
conflict back, so the reader's choice waits for another press. Fixing the
stale answer instead keeps both outcomes. A Save draft that did land now
settles on Ctrl+S, where it used to show "Changed on disk" with the draft's
own text on disk, and one that didn't is sent again as before. The menu's
Save draft calls the save directly, which a Retry condition can't reach;
this fix covers it too.

A proxy's error page in place of the server's answer already keeps `sent`:
file-api.js turns a `RequestError` without the server's words into a plain
`Error`, so only the server's own refusal clears it. A 502 after the server
wrote the save kept the record, asked before leaving and wrote the reader's
text back after the reload. The comment there now says so.
@max-sixty
max-sixty marked this pull request as ready for review November 23, 2026 04:09
@max-sixty
max-sixty merged commit 8cc735c into main Nov 23, 2026
6 checks passed
@max-sixty
max-sixty deleted the agent-ad94527e2c73f18f1 branch November 23, 2026 04:21
leaf-agent added a commit that referenced this pull request Nov 23, 2026
…ding (#6859)

`test_a_retry_after_a_save_with_no_answer_keeps_the_typing_after_it[on]`
reloaded the page as soon as Playwright saw the retried save's 200. The
editor treats the save as in flight until its own `await` takes that
answer, so a reload in that gap correctly asked before leaving, and the
test read `['beforeunload'] == []`. This is the prompt #6787 added for a
save the editor has no answer for, so the product is right and the test
needed the fix. Before it reloads, the test now waits until the editor
clears its record of the file's unsettled work in this tab (`tabStore`
key `file:notes-file:notes`). That record is null exactly when the
editor's `beforeUnload` finds nothing unsettled.

**Evidence**
- The failure is in `ci` [run
45123246324](https://github.com/max-sixty/leaf/actions/runs/45123246324)
on `main` at `39ee727dd`: `assert ['beforeunload'] == []`, plus the
teardown error that follows, because the test ended before consuming its
expected `ERR_FAILED` and 409.
- `leaf-dev suite flake --copies 12` on `main` failed 1 of 12 copies
with the same assertion.
- To force the order, a temporary probe delayed the page's handling of
each `fetch` answer by 500 ms. With it, the old test fails every run
with CI's message and teardown error, and the new test passes both
cases.
- Unforced, the new test passed 48 of 48 copies under `--cpu-throttle
6`. `tests/model/test_suite_source.py` passes.

Refs #6858 — 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