Skip to content

Restore of a Discard keeps the draft of a created document - #1737

Merged
haowjy merged 6 commits into
feat/document-commands-never-refusefrom
fix/restore-created-doc
Nov 17, 2026
Merged

haowjy merged 6 commits into
feat/document-commands-never-refusefrom
fix/restore-created-doc

Conversation

@haowjy

@haowjy haowjy commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Changelog

  • Fixed: Undo discard on a document the AI created no longer reports "the text changed after it was discarded" when nobody typed, and the restored draft opens with its text instead of blank.

Why

A Draft-mode chat creates manuscript://Third; the writer applies it, presses the card's Undo, then Discard all and the row's "Undo discard". Nobody typed, yet the row read "Partly restored: 1 change couldn't come back" and opening it showed a blank draft (before). A longer sequence (Apply, Undo, Apply all, Ctrl+Z in the Live document, Discard all, Undo discard) read "Not restored" while the chat's card said "Applied".

Cause: the Un-Apply of a create sends the Live document to Trash and reverses its text, leaving Live with no blocks. The draft's base holds one empty paragraph. editedSince (drizzle-reopen-drafts.ts) diffs the base against Live, reads that as Live deleting the paragraph the change sits in, and leaves the draft out. A left-out draft stays closed, so opening it shows Live's empty text, which is the blank editor. The card read Applied next to a left-out row because a Restore that leaves a draft out never finished the draft's waiting Un-Apply: the draft kept its Apply record, which the card reads as applied.

Examples

Restore a created document after Un-Apply

  1. Setup: this branch, MODEL_PROVIDER=mock pnpm dev; a Work in Draft mode; mock script write create manuscript://Fifth.
  2. Apply from the strip, press the card's Undo, Changes > Discard all, then "Undo discard".
  3. Expect: main says "Partly restored"; this branch restores the row (after), and opening it shows the draft's text (draft).

Real typing on an existing document still leaves the change out

  1. Setup: a document "The cat sat on the mat."; the AI replaces "cat" with "tiger" in Draft mode. Discard the draft, type "dog" over "cat" in the Live document, press "Undo discard".
  2. Expect: "Not restored: the text changed after it was discarded" on main and on this branch (screenshot).

Benchmarks

None. One indexed lookup by document id on Restore of a Discard, which is not a hot path.

Design decisions

  1. An empty document compares as no blocks. editedSince diffed the base against Live with diffBlocks, which reads no blocks and one empty paragraph as different, while blocksEqual (used by the Live pull) reads both as an empty document. It now turns an empty base or Live into no blocks before diffing, so created, deleted, trashed and standing documents follow one rule. An earlier version of this PR skipped the check for a document in Trash or without a tree row; that is gone, and a standing document with the same shape (base one empty paragraph, Live none) restores too.
  2. A left-out draft finishes its waiting Un-Apply. An Un-Apply that meets a closed draft leaves its draft half waiting, and only a reopen replayed it. A Restore that leaves a draft out now replays it too: the base stops holding the Un-Applied text and the Apply record goes, so the card stops saying Applied while Live lacks the text. The replay appends to the closed draft directly (appendRows takes draft: "any", like work: "any"), so the draft is never flipped active, and a newer active draft of the same document on its branch cannot collide with it.

Changes

# Part Why it's here Key paths Source Tests Docs
1 Empty documents compare as no blocks in editedSince; left-out drafts replay their waiting Un-Applies The false conflict, and the card claiming an Applied that was taken back drizzle-reopen-drafts.ts +33 / -10 +0 / -0 +3 / -1
Total (2 files, plus 6 screenshots) +33 / -10 +0 / -0 +3 / -1

Compatibility and risk

  • Breaking or changed for writers: a document the Un-Apply of a create left empty no longer reads as edited, so its draft reopens. A draft that Restore leaves out now has its Un-Apply finished, so its card no longer says Applied.
  • Migrations and state: None.
  • Clients during a deploy: None; server-side only.
  • Rollback: None needed; a left-out draft still ends closed as left_out.
  • Where to look: emptyAsNone in editedSince, and the left-out branch at the top of reopen.

Verification

  • Browser: agent-browser against pnpm dev on this branch, mock provider. The three steps above restore the row with no "Partly restored", and opening it shows "Quiet dusk." (draft). Card case: the AI makes two changes to one document, one is applied, Discard all, the card's Undo, a Live edit over the other change, "Undo discard" gives "Not restored", and the card reads "Discarded 1 document" with Live holding the original text (card). Before this change that card kept saying Applied.
  • Model: mock provider; the change does not touch the harness.
  • Tests: none added. A real-Postgres probe (scratch, deleted; the reviewer's, in the work directory) over the real Apply, Discard, Un-Apply and Restore commands: a standing document with base one empty paragraph and Live none restores; the same in Trash restores; real Live typing over the changed word is left out (also for typing before trashing); Apply, Discard, Un-Apply, Live typing, Restore leaves the draft with no Apply record (1 before, 0 after) and its base back at the original text. With a newer active draft of the same document on the same branch (the review's R-NEWER and R-CARD-ITEM-NEWER), Restore answers left_out with no error and the Apply record is gone. pnpm test:db on the apply-restore file passes.
  • Gates: pnpm check.

Deferred

None.

References

Spawn Agent Model Role
p18502 coder claude-sonnet-5-5 Found the cause, fixed it, ran the browser and database probes

…n Trash or not in Live

After Un-Apply of a created document, Live holds no blocks while the draft's base holds an
empty one, and the text-changed check read that as a writer's edit. Nobody types into a
document that is in Trash or not in Live yet, so its draft always reopens.
@haowjy
haowjy marked this pull request as ready for review November 17, 2026 00:40
…a left-out draft's Un-Apply

editedSince read a base holding one empty paragraph against an empty Live as an edit; it now reads an empty document as no blocks, as blocksEqual does, and the Trash guard is gone. A draft that Restore leaves out now replays its waiting Un-Applies, so its Apply records stop claiming an Apply that was taken back.
The replay no longer flips the draft active, which tripped the one-active-draft index when a newer
draft of the document sat on the same branch. appendRows takes draft: "any". The replica now
installs after the Restore commits.
transfer() already waits for the commit through install; the extra deferral and rollback hook were redundant. The guarantee is stated once on PreparedDraftWrite.transfer.
@haowjy
haowjy merged commit 519b35b into feat/document-commands-never-refuse Nov 17, 2026
10 checks passed
@haowjy
haowjy deleted the fix/restore-created-doc branch November 17, 2026 02:08
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.

1 participant