Repository navigation
A restore of a Discard leaves out a draft whose Live text was edited since - #1735
Merged
haowjy merged 12 commits intoNov 16, 2026
Merged
Conversation
…since (#1733) Undoing a server Discard (Discard all, or a Restore from Trash) reopened the closed draft as it was, so its change came back over a word the writer typed in Live meanwhile. The room Discard's Undo already leaves that out. `reopen` now takes `leaveOutEdited`: it merges the draft's base (what Live last showed it, since a closed draft is not pulled into), Live now and the draft's text, and a conflict keeps the whole draft closed. The restore counts its changes in `leftOut` and says `left_out changed_since`. The app words it as the room path does, on the Changes row and on Trash's row.
…he editor pill stop reading it as restored (#1733)
haowjy
marked this pull request as ready for review
November 16, 2026 21:57
…pan, and takes a left-out entry out of Trash (#1733) A restore of a Discard now judges 'edited since' on the draft's base with its waiting Un-Applies replayed, and as the room path's touchedSince does: Live editing, or writing beside, what the draft changed. A left-out draft stays closed with closed_by cleared, so the entry leaves Trash instead of leaving the same thing out on every Restore. Trash's Undo of a partial restore discards only the documents the restore reopened.
…use' into fix/server-discard-undo-edited # Conflicts: # apps/app/src/features/draft-review/CommandNotice.tsx # apps/app/src/features/draft-review/draft-notices.ts # apps/app/src/locales/en/messages.po # apps/app/src/locales/en/messages.ts # apps/app/src/locales/zh/messages.po # apps/app/src/locales/zh/messages.ts
…hes the room's; a left-out restore is settled; Trash's Undo takes back what the restore brought (#1735) A draft Restore leaves out keeps closed_by (the day-30 purge, the model's write path and receipts read it as the Discard's draft) and takes closed_cause 'left_out'; Trash's entry list and closedBy() skip it. The edited-since check compares uncleaned token ranges and counts a shared endpoint only for a pure insertion, so a neighbouring word, its marks or a split no longer leave a draft out; a join into the block before still does, as in the room. DraftNotices counts a fully left-out share as settled. The restore answer names the halves it brought back (brought), and Trash's Undo discards exactly those.
…d does not reach an AI insertion; small Restore fixes (#1735) An oversized unit (over 1,000 tokens) was one whole-paragraph replacement, so any Live edit in a long paragraph left a restorable draft out forever. Its token keys are now diffed by a bounded Myers (exact up to 1,000 changed tokens, cost bounded by the edit distance), which also makes review hunks in long paragraphs exact. A shared endpoint reaches a change only when Live's edit is the pure insertion. reopen returns the documents whose draft came back, so undo-discard stops re-deriving them. A second Restore of a fully left-out entry answers already; ofDiscard skips left_out drafts; a notice where nothing came back reads Not restored; Trash's restored row says what stayed out. The room/server difference on retyping is documented beside touchedSince. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… is not "Not restored"; the bound counts edit operations (#1735) boundedDiff says whether it was exact, and the hunks of a fallback carry approx. editedSince ignores an approx hunk on either side, so Live edits at both ends of a long paragraph no longer leave a proposal in the middle out; the display keeps the fallback. The notice's nothingBack also remembers a room share that undid. Docs and comments say the bound is 1,000 edit operations, about 500 replaced words. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog
Why
Fixes #1733. Found while fixing #1727.
A server Discard (Discard all in the Changes list, or any Discard that closes a draft) has an Undo that reopens the closed draft as it was, with its old base. A closed draft is not pulled into, so its base is the Live text it last saw. After the writer typed over the discarded word in Live, the next pull merged Live into the reopened draft with
contentMerge, where the draft's side wins a conflict. The AI's word came back as a proposal over the writer's, with no line saying so. The room Discard's Undo (the chat strip and the editor, #1734) already leaves that out and says so, so two Undos of the same kind of action disagreed.After merge, both Undos follow one rule: restoring a discarded draft leaves out a change whose Live text was edited since it was discarded, counts it in
leftOut, and words it the same way.Examples
Undo Discard all after typing over the discarded word in Live
MODEL_PROVIDER=mock pnpm dev../mf doc put manuscript://Gate --text "The old gate stood at the end of the road.". A new chat in Draft mode whose mock reply replaces "old gate" with "crumbling gate" (./mf mock script). Open Gate beside the chat.An Undo with no Live typing restores everything
Same steps without step 3: the draft comes back with its change.
Restore from Trash follows the same rule, and a fully left-out entry leaves Trash
Same setup, Discard all, type "grey" in Live, then open Trash and choose Restore. The row is struck through with "Not restored" and the line "1 change couldn't come back: the text changed after it was discarded."; after a refetch the entry is gone from Trash. Restoring it again has nothing to restore.
An edit right beside the AI's change also leaves it out
Discard all, then type "big " in front of the discarded word. The room path's
touchedSincecounts an edit beside the change, so Undo now does too: the row reads "Not restored".A partial restore
Two documents in one Discard, one typed over: Restore brings Road back and leaves Gate out ("Drafted 2 documents: 1 discarded, 1 still a draft").
A fully left-out restore is settled in the notice
Discard all, type over the discarded word in Live, press the chat's Undo. The notice becomes "1 change couldn't come back: the text changed after it was discarded." with no Undo (before, it kept the "Discarded just now" line and an Undo that could only send a second restore).
Trash's Undo takes back exactly what the restore brought back
A Work document has a drafted rename and a text change. Discard all, type over the word in Live, Restore from Trash: the rename is back as a pending change and the text stays out. Then Trash's Undo: the rename goes, the entry returns to Trash.
One "edited since" rule, run on both paths
Base "The old gate stood at the end of the road."; the AI changed "old" to "crumbling"; Discard; one Live edit; Restore. Left column: the room's
touchedSinceon a Y.Doc (realwriteDiscardBlocks,markStepDone, a Live write from another client). Right column: the server's Restore over Postgres (real Discard kernel, Live edit throughjournal.append,undo_discard→reopen). Scratch probes, not kept.Before: the server left out the next word, the word before, the bold and the word between; the room restored them.
Third round. AI insertion rows (base as above; the AI inserted " big" before "gate"), AI deletion and new-paragraph rows, retyping, and long paragraphs (words
w0 w1 …, the AI changedw500orw5000):w500changed; Live editsw510w500itselfw501(next to it)w5andw995w5000changed; Live editsw5010w5000itselfw5001w5andw9995w5000w5000; Live edits words 100–350 and 9000–9250 (past the bound, so the diff is one coarse replacement)Where the two paths differ, and why (two cases; the third bullet is a limit of my probe):
touchedSinceandeditedSince.oldtoolderinserts "er"), so it sees two edits beside the insertion. The server leaves out; nothing is lost. Rare.writeBlocks, which replaces everything between the first and last edit as one range above 4,000 keys, so the room sees the AI's text rewritten. Real typing arrives as separate edits (the two-edit rows above). The server reads the final text, so it is exact.Real database
A rename plus a text change on one document, Discard, type in Live, Restore:
restored 1, leftOut 1, brought { text: [], tree: [doc] }. Trash's Undo sends that document withends: "tree"; the Work's rename record is gone afterwards (the old filter sent no document, closed nothing and left the rename pending). A fully left-out draft readsclosed / left_out / closed_by = Discard;listWorkDraftStatesanswersdiscarded, Trash's entry list is empty, a second Restore answersalready, and the day-30 purge deletes the draft with its Discard.Benchmarks
Restore end to end over Postgres, which holds the document locks throughout (one run each, local): short paragraph 13–19 ms; one 10,000-word paragraph 25–35 ms; a 100-paragraph chapter 31–35 ms; a 10,000-word paragraph rewritten past the 1,000-operation bound (the coarse fallback) 34–42 ms. The long-paragraph diff is a Myers search capped at 1,000 edit operations, so its cost follows the edits, not the size or how unlike the texts are.
Design decisions
reopen, behindleaveOutEdited. The notice's Undo, Trash's Restore and the editor's Restore all reachundo_discard→reopen, so one check covers each. Un-Apply's reopen also usesreopenbut must not ask: its own Apply wrote the Live text the draft's base lacks, so the merge would always conflict. Ruled out: a client check, which Trash and a second tab cannot run, and a check insideundo-discard.ts, which would read the draft twice.changed.rawagainst Live's raw hunks), after replaying the draft's waiting Un-Applies onto a copy of its base. A neighbouring word, the marks of the next word and a split of a block do not count. A join into the block before (or a new block type) counts, on both paths. I was asked to make a block change that spares the change's own text not count on both; for a join that cannot hold in the room: Yjs's Undo of the Discard cannot follow the text into the joined block (probe: Undo after the join leaves "First line.The old gate stood." with the AI's change lost), sotouchedSincekeeps its answer and the server now matches it.touchedSinceitself is unchanged apart from its comment.closed_cause = left_out(migration: the check constraint),closed_byunchanged. Trash's entry list andclosedBy()skip it; the purge, the model's write path (listWorkDraftStatesreads it as discarded) and receipts read it as the Discard's draft. Reopening it empty would leave nothing to review, and clearingclosed_byhad made it read as "the Work was purged".left_out changed_sinceas its own restore state. The answer isrestored: 0, which the app treated as "gone from Trash". It now answers the notice and Trash's row with the room path's sentence (leftOutSentence).unitHunksmade a unit over 1,000 tokens one whole replacement, so any Live edit in a long paragraph left a change out for good. Those units are now diffed by token key withboundedDiff(Myers, exact up to 1,000 edit operations, then one replacement of what the ends do not share, flaggedapprox). The bound counts edit operations, so a replaced word costs 2 and the bound is about 500 replaced words.editedSincetakes anapproxhunk, on either side, as "can't tell" and restores: a wrongly restored proposal is one the writer sees and can discard, a wrongly left-out one is lost for good. The display keeps the fallback. It lives inalign.ts's oversized branch, so review hunks in long paragraphs are exact too; one rule for display and for this check. Ruled out: a second diff used only here, and raising the size cap, which makes dissimilar paragraphs quadratic.Changes
reopenleaves out an edited draftdrizzle-reopen-drafts.ts,composition.tsleftOut,left_out changed_sincedrizzle-undo-discard.tsleft_outrestore stateCommandNotice.tsx,useWorkBranchCommands.tsx,TrashDiscardRow.tsx, locale catalogsreopentakes the Live journalapply.md,draft-review.mdboundedDiffinalign.ts's oversized branch,reaches,alreadyon a second Restore,ofDiscard,broughtfromreopen, "Not restored", Trash row's left-out linebounded-diff.ts,align.ts,drizzle-reopen-drafts.ts,drizzle-undo-discard.ts,drizzle-command-documents.ts,draft-notices.ts,TrashDiscardRow.tsxCompatibility and risk
document_drafts_closed_cause_validcheck gainsleft_out(regenerated withpnpm db:generate; a dev database needspnpm db:reset). The restore answer gainsbrought.restored: 0and shows "Gone from Trash" for that case until it reloads.editedSinceindrizzle-reopen-drafts.ts. It reads the draft and the Live journal under the document locksundo_discardalready holds, so Agents and skills are documents you can edit #1534'slockDocumentMutationand B4's purge order are unchanged.Verification
agent-browseron the mock stack, before and after, as in Examples: Undo at once, after 12 s, with no typing, and Restore from Trash.pnpm checkpasses. Existing:pnpm test:db -- drizzle-apply-restore branch-tree.apply-discardpasses (24 tests), including the restores that reopen drafts after Un-Apply.branch-tree.apply-discardnow passes the real journal instead of a stub.agent-browser, mock stack, a Work in Draft mode): the notice after a fully left-out restore (no Undo), and Trash's Restore then Undo of a document with a rename and a left-out text change.DraftNoticeswith handles that answer as a room does (ok, andgonewith a left-out count). The coarse-fallback row ran through the real Discard and Restore over Postgres; it left the draft out before this fix and restores now. The table ran on the real code of both paths.pnpm check.Deferred
diffKeys(tokens.ts, the room's write path) still falls back to one coarse replacement past 4,000 keys. It could useboundedDifffor that middle, which makes the room's writes exact on long paragraphs and leaves one bounded diff. Its own PR.boundedDiffcopies its whole diagonal array each round (about 8 MB of short-lived garbage per exhausted search); saving only the diagonals in use would halve it.References
apply.md,CONTEXT.mdandstate-machines.mdofcollab,draft-review.md, thetouchedSincecomment.