Publishing inline subdocuments without losing the parent draft
How publishing a child created from a draft card updates its parent. The parent gets a version that adds only the card, the parent draft keeps its shape, and a parent can be published while it still holds unpublished child cards.

Summary

You create a subdocument inline: /New document puts a draft card at the cursor, and later the NEW button will too. You can then keep editing the parent around the card. When you publish the child, the parent must get a card for it, and your unpublished parent edits must not be lost, published, moved or duplicated.

The solution has four parts:

    Card-only parent publish (V2). Publishing the child publishes a new parent version: the latest published parent plus only the child's card, appended at the end. Nothing else from your draft is published.

    The parent draft keeps its shape. Your draft card becomes a published card where you left it. Changes other people published apply only to blocks you did not touch. On a conflict your version is kept, with a toast if the parent is open.

    Your next parent publish sends the rest. That publish contains the draft minus V2: the card's move to where you put it, plus your other edits.

    The parent can be published while child drafts are unpublished. Their cards, and anything nested under them, are left out of the publish. They stay in your draft until those children are published.

The work is on the feat/new-document-slash-command branch, PR #1353. Related: Creating documents where you are.

Terms

    V1: the published parent version your draft is based on.

    V1.5: a parent version someone else published after your draft started and before your child was published.

    V2: the automatic parent publish made when the child is published. It is the latest version (V1 or V1.5) plus the card.

    Draft card: an embed in the parent draft that points to an unpublished child draft. It has a draftId and no link.

    Published card: an ordinary card embed linking to the published child.

    Base: the published version a draft is compared against. After the rebase it is V2.

How it works

1. Creating a child

    /New document creates a child draft and inserts a draft card where you typed. Its title gets focus and you stay in the parent.

    The NEW button in the top bar will do the same in a separate change: it puts the draft card after the last published block of the parent draft instead of opening the child.

    A parent draft can hold any number of draft cards, anywhere: inside new headings, with blocks nested under them, or next to other new blocks.

2. Publishing the child: the automatic parent publish (V2)

Publishing a child for the first time queues an add-card job for its parent. The job runs in the background, one job per parent at a time, and retries after 1 s, 5 s and 15 s before it asks for attention.

    It finds the draft card in the parent draft by its draftId. Only the card's block id matters; its position does not.

    It loads the latest published parent: V1, or V1.5 if someone else published in between.

    It publishes V2: that version plus the child's card, appended after the last top-level block. The card keeps the draft card's block id, so the draft and V2 share the same card.

    V2 is skipped when the parent already links to the child, or lists its children with a query block.

    It reloads the parent to verify V2 and records it as a checkpoint, so a retry never publishes twice.

3. Rebasing the parent draft

The job then rebases the parent draft onto V2. If the parent is open in an editor, the same rules run on the live editor content.

    Every draft card for this child becomes a published card in place: same id, same position, same nested blocks. Copies of the card from copy-paste are converted too.

    If the draft has no card for the child:

      You removed it: it stays out. The editor records removed draft cards in the parent draft's removedChildDraftIds.

      The draft never had one: the card goes in after the last published block that is still in the draft.

    If nobody else published since the draft's base, the draft stays exactly as it was, with the card now published.

    If someone else published (V1.5), their changes apply only to blocks you did not touch, and your structure is kept:

      Their edits to blocks you didn't change are taken.

      Blocks they deleted are removed, unless you changed them.

      Their new blocks are inserted after the nearest preceding block that is still there.

      Your new blocks, moves and nesting stay where you put them.

    When both sides changed the same block, your version is kept, the block id goes to conflictedBlockIds, and an open parent shows: "Another author also edited N blocks — your version was kept."

    The draft is saved with base = V2, using a compare-and-swap. If you edited the draft meanwhile, the job retries with your latest edits.

4. Publishing the parent while child drafts are unpublished

Publishing a parent with unpublished child drafts used to be blocked. Now:

    Publishing leaves out every draft card and everything nested under it, and publishes the rest.

    If draft cards remain, the parent draft is kept instead of deleted. It has the same content and is based on the version just published. The cards wait there for their child's publish.

    The Publish button counts only changes it can send, so a draft that only holds draft cards has nothing to publish. The popover notes how many unpublished documents stay in your draft.

    "Discard changes" stays available while draft cards remain.

5. What the next parent publish sends

Draft minus V2: moving the card to where you put it, plus your other edits. If you removed the card, the publish removes it from the parent and the child-deletion dialog asks what to do, with a "Publish without deleting" option.

Diagrams

Actor model

Who does what, from creating the card to the next parent publish.

Actor model

Cleanup job state machine

One attempt of the add-card job (document-card-cleanup-machine.ts). The highlighted states changed in this work.

Cleanup job state machine
stateDiagram-v2 direction TB classDef changed font-weight:bold [*] --> routing routing --> verifyingPrimary: unconfirmed routing --> deleting: delete-child routing --> preparing: add, no V2 yet routing --> loading: otherwise verifyingPrimary --> routing: confirmed verifyingPrimary --> attention: error preparing --> loading preparing --> failed: error deleting --> done deleting --> failed: error loading --> planning loading --> declined: declined loading --> failed: error state planning <<choice>> planning --> skipped: no parent planning --> reconciling: V2 exists planning --> publishing: adds card planning --> verifying: linked publishing --> skipped: no parent publishing --> verifying publishing --> failed: error verifying --> reconciling verifying --> failed: error reconciling --> done reconciling --> failed: error class preparing, loading, reconciling changed

Coordinator state machine

The durable queue that runs one job per parent at a time and owns retries.

Coordinator state machine
stateDiagram-v2 direction LR [*] --> idle idle --> running: tick, due job idle --> waitingForRetry: tick, retry pending waitingForRetry --> running: tick, due job waitingForRetry --> idle: tick, nothing pending running --> idle: attempt finished or crashed

Rebase rules

What "Rebase the parent draft" does after V2.

Rebase rules

Publishing the parent

Publishing the parent while child drafts are unpublished

Document publish path (excerpt)

The states of DocumentLifecycle (document-machine.ts) between editing and a finished publish. The highlighted states changed.

Document publish path
stateDiagram-v2 direction TB classDef changed font-weight:bold state "editing.draft.changed" as changed state "editing.draft.creating or saving" as saving state "editing.draft.idle" as idle state "publishing.checkingChildDeletion" as checking state "publishing.inspectingChildDeletion" as inspecting state "publishing.confirmingChildDeletion" as confirming state "publishing.childDeletionFailed" as deletionFailed state "publishing.inProgress" as inProgress state "publishing.cleaningUp" as cleaningUp changed --> saving: publish.start, queued saving --> idle: saved, raises _save.completed idle --> checking: publish.start or queued publish checking --> inspecting: might delete children checking --> inProgress: otherwise inspecting --> confirming: children to delete inspecting --> inProgress: none inspecting --> deletionFailed: error confirming --> inProgress: confirm or skip deletionFailed --> inProgress: skip confirming --> idle: cancel deletionFailed --> idle: cancel inProgress --> cleaningUp: published inProgress --> idle: publish error cleaningUp --> loaded class inProgress, cleaningUp changed

Examples and what happens

In every example V1 is a "Intro", b "Body". In the trees, card ◌ Ideas is a draft card for the child "Ideas", card → Ideas is a published card, (edited) marks a block changed in the draft, and indentation means nesting.

1. Card under a new heading

Draft at child publish V2, automatic Draft after the rebase a "Intro" (edited) a "Intro" a "Intro" (edited) h "Projects" b "Body" h "Projects" x "Text above the card" card → Ideas x "Text above the card" card ◌ Ideas card → Ideas b "Body" b "Body"

Next parent publish: edit a, insert h after a with x inside it, and move the card into h after x.

2. Text added above the card

Draft at child publish V2, automatic Draft after the rebase a "Intro" (edited) a "Intro" a "Intro" (edited) b "Body" b "Body" b "Body" c "block 1" card → Ideas c "block 1" d "block 2" d "block 2" card ◌ Ideas card → Ideas

Next parent publish: edit a, insert c after b and d after c, and move the card after d.

3. Content nested under the card

Draft at child publish V2, automatic Draft after the rebase a "Intro" a "Intro" a "Intro" card ◌ Ideas b "Body" card → Ideas c "block 1" card → Ideas c "block 1" d "block 2" d "block 2" b "Body" b "Body"

Next parent publish: move the card after a and insert c and d under it.

4. The card was moved after it was created

Only the final draft matters, so this behaves like the other examples.

5. Someone else published V1.5 first

V1.5 deleted b, so it is just a. V2 is built on V1.5.

Draft at child publish V2, built on V1.5 Draft after the rebase a "Intro" a "Intro" a "Intro" b "Body" card → Ideas card → Ideas card ◌ Ideas c "block 1" c "block 1" d "block 2" d "block 2"

b goes because you didn't touch it. Next parent publish: insert c after the card and d after c.

If you had edited b, the draft after the rebase is a, b (edited), card, c, d. Your version of b is kept, the toast says so when the parent is open, and your next publish re-creates b.

6. Two draft cards, only Resources published

Draft at child publish V2, automatic Draft after the rebase a "Intro" a "Intro" a "Intro" b "Body" b "Body" b "Body" card1 ◌ Ideas card2 → Resources card1 ◌ Ideas card2 ◌ Resources card2 → Resources

Next parent publish: nothing yet. card1 is held back until Ideas is published, and any other edits would go out now. Publishing Ideas later runs the same flow: V3 = V2 + card1 at the end, and the draft stays the same. The publish after that moves card1 after b.

7. The card was removed before the child was published

Draft at child publish V2, automatic Draft after the rebase a "Intro" a "Intro" a "Intro" b "Body" b "Body" b "Body" (card removed) card → Ideas

The parent draft recorded the removal, so the rebase leaves the card out. Next parent publish: delete the card. The publish dialog asks about deleting Ideas and offers "Publish without deleting".

8. Someone else edited the parent first

Same as example 5.

9. The parent already links to the child

Draft at child publish V2, automatic Draft after the rebase a "Intro, links to Ideas" (none) a "Intro, links to Ideas" card ◌ Ideas card → Ideas b "Body" b "Body"

There is no V2, but the draft card is still converted. Next parent publish: insert the card after a.

A child without a card in the draft

This happens to children whose parent draft never had a card, such as drafts made before the NEW button creates cards.

Draft at child publish V2, automatic Draft after the rebase a "Intro" (edited) a "Intro" a "Intro" (edited) b "Body" b "Body" b "Body" c "New text" card → Ideas card → Ideas c "New text"

b is the last published block, so the card goes right after it. Next parent publish: edit a and insert c after the card.

Publishing the parent before its child

Draft when you publish Published parent Draft after publishing a "Intro, edited" (edited) a "Intro, edited" a "Intro, edited" card ◌ Ideas b "Body" card ◌ Ideas c "block 1" c "block 1" d "block 2" d "block 2" b "Body" b "Body"

Next parent publish: nothing until Ideas is published. Then V2 = the published parent + the card at the end, and your next publish moves the card after a with c and d under it.

Concurrent changes keep your structure

These cases come from the tests.

    Base a, b, c, draft a, X, b, c, card ◌, and someone else edited b → a, X, b (their edit), c, card →. No conflicts.

    Base a, b, c, draft c, a, b, card ◌ (you moved c to the top), and someone else edited b → c, a, b (their edit), card →. No conflicts.

Implementation

    frontend/packages/shared/src/utils/document-reference-rebase.ts: rebaseDocumentReferenceDraft hands add jobs to mergeAddJobDraft. That function converts the cards, merges on top of the draft, and returns content, mineTouchedIds and conflictedBlockIds.

      Content and position changes are tracked separately.

      A block counts as moved when its parent changes, or when its nearest preceding sibling changes, counting only siblings that exist on both sides.

      Deletions splice the deleted block's remaining children into its place.

      Inserts and moves from the other version go after the nearest preceding sibling that still exists.

      Card ids never count as conflicts.

      Other job types (remove, rewrite, delete-child, add-cards) are unchanged and still refuse conflicting remote edits.

    frontend/packages/shared/src/models/document-card-cleanup-machine.ts: findTemporaryCard returns only the card's block id. cardParentId and cardLeftSibling are gone.

    frontend/apps/desktop/src/app-document-card-cleanup.ts and frontend/apps/web/app/document-edit/web-document-card-cleanup.ts:

      V2 no longer overrides the card's position; planDocumentCardAppend appends it after the last top-level block.

      The rebase receives cardRemoved from the draft's removedChildDraftIds.

      The draft-changed event carries conflictedBlockIds.

    frontend/packages/shared/src/utils/document-changes.ts:

      removeDraftCardsFromEditorBlocks, hasDraftCards and getDraftOverridesAfterPublish.

      Only an embed with a draftId and no link counts as a draft card. An embed with a leftover draftId next to a real link still publishes, without the draftId.

    Publish actors, frontend/apps/desktop/src/pages/desktop-resource.tsx and frontend/apps/web/app/document-edit/web-document-actors.ts:

      The publish leaves draft cards out.

      When cards remain, the draft is rewritten from the stored draft instead of deleted. It keeps its location and maintenance revision; its deps and base move to the new version; child-removal, schema, navigation and publish-path overlays are cleared.

      The actor returns keptDraftContent.

    frontend/packages/shared/src/models/document-machine.ts:

      concludePublishDraftState keeps the draft when keptDraftContent is set and clears it otherwise. It shares one reset list, draftOverlayReset, with clearDraftState.

      The live maintenance path passes cardRemoved.

    frontend/packages/shared/src/models/use-unpublished-change-count.ts leaves draft cards out of the change count.

    frontend/packages/ui/src/editing-toolbar.tsx: Publish is no longer blocked, the popover shows the note, and Discard stays available.

    Docs: hypermedia/software/desktop.md.

    Tests:

      One test per example in document-reference-rebase.test.ts, plus the concurrent cases.

      Draft-card helper tests in document-changes.test.ts.

      Kept-draft publish tests in document-machine.test.ts and web-document-actors.test.ts.

      V2-at-the-end, removed-card and conflict-event tests in the desktop and web cleanup tests.

      Toolbar tests in editing-toolbar.test.tsx.

Verification so far

    Typecheck: passes for all 20 workspace projects. Formatting is clean.

    Unit tests: shared 1940 pass (1 skipped), ui 1379 pass, web 857 pass (1 skipped), desktop 1600 pass.

    The one failure: desktop's agents-live-titling.integration.test.tsx fails because it needs a running app environment. It failed the same way before this work.

    Not run yet: the desktop and web production builds, local CI, and trying the flow by hand in the app.

    Not on the PR yet: when this was written, these changes were not committed or pushed.

What's missing and open concerns

    NEW button: in-place draft cards from the top-bar NEW button come in a separate commit. Until then, NEW children get their card through the card-less path above. The first child-card paragraph in hypermedia/software/desktop.md still describes the old NEW behaviour and should be updated with that commit.

    Manual testing: none of this has been tried by hand yet. That includes the popover note, a kept draft after publishing, and the conflict toast. Builds and local CI are also pending.

    Card at the bottom for a while: readers of the published parent see the new card at the end until you publish the parent draft that moves it.

    A parent that keeps a draft: while it holds unpublished child cards, the parent always has a draft. "Discard changes" deletes that draft and asks about the child drafts.

    No notice when the parent is closed: the conflict toast only appears if the parent is open when the rebase runs.

    Reorder edge case: if you reorder blocks and someone else deletes a block you moved past, that block can count as changed by you. It is then kept, with the toast, instead of being deleted.

    Copies of a card: several draft cards for the same child all become published cards, so the parent can link to the child more than once.

    A removed card is still added to V2 (example 7). The published parent shows the card until your next publish removes it, and that publish asks about deleting the child.

    Two merges: the editor's own auto-rebase, used when a new version arrives while you edit, still uses the older merge. That merge appends new blocks at the end of their parent and keeps local content unmerged on conflicts. Aligning the two could be a follow-up.

    Source of truth: the diagrams are illustrations; the code and tests are authoritative.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime

Comments