Skip to content

Reject stale document saves instead of overwriting another editor #200

Closed

Description

@imshashank

Two clients can still save the full document body over each other. Serializing autosaves within one tab and keeping document history do not reject a stale save from another tab. The agreed scope is conflict detection; collaborative editing, Yjs and cursor presence are outside this issue.

Add a document revision and require the client to submit the revision it edited. Check it atomically on the server, reject a stale save without overwriting current content, and show the conflict while preserving the local draft. Cover two clients saving from the same revision, a normal save, retry/reload behavior, permissions and the conflict UI.

Start with packages/core/src/content/doc-service.ts, the shared document validator and route, and apps/web/src/features/docs/use-autosave.ts. Related: #199.

Activity

  1. added this to the Docs as a real wiki milestone on Aug 8, 2026
  2. added
    bug
    needs design
    p1 high
    area: realtime
    area: docs-feature
    on Aug 8, 2026
  3. moved this to Needs design in Orbit roadmapon Aug 8, 2026
  4. added
    size: extra large
    points: 13
    schema change
    on Aug 8, 2026
  5. imshashank commented on Sep 6, 2026

    @imshashank
    ContributorAuthor

    Re-scoping to the smaller alternative and treating it as a p1 bug: a revision on doc, a save whose base revision is stale is refused, and the editor shows the other person's changes instead of overwriting them. That stops the data loss today. Full collaborative editing over Yjs stays out of this issue; if someone wants it, open a design issue for the transport and persistence questions above.

  6. added
    ready
    size: medium
    points: 5
    and removed
    needs design
    size: extra large
    points: 13
    on Sep 6, 2026
  7. changed the title [-]Two people editing the same doc lose each other's work[/-] [+]Reject stale document saves instead of overwriting another editor[/+] on Sep 22, 2026
  8. ayam04 commented on Oct 17, 2026

    @ayam04
    Contributor

    Taking this one. The rescoped shape is clear: the doc carries a revision, the editor sends the revision its draft was built on, and a save whose base is stale is refused instead of overwriting the other tab's body.

    Plan:

    • revision column on doc + migration, bumped only when the body changes, so a rename does not refuse an in-flight body save.
    • baseRevision on the shared doc update validator; a compare-and-swap WHERE id = ? AND revision = ? inside updateDoc, with the stale case returning a conflict that carries the current revision.
    • doc-surface / use-autosave send the base revision the draft was built on and show the conflict while keeping the local draft in the editor, with an explicit "load their version" action.

    Tests: two clients saving from the same revision (second refused, stored body intact), a normal save advancing the revision, a save without a base still working, plus the web-side conflict notice.

  9. ayam04 commented on Nov 8, 2026

    @ayam04
    Contributor

    This is implemented in #571, following the rescoped shape on this thread.

    • doc.revision with migration 0032, advanced only when the body changes, so a rename does not refuse an in-flight body save.
    • updateDoc takes baseRevision and puts it in the update's own WHERE, so the compare and the write are one statement. A stale base changes no row and answers conflict with details: { reason: 'stale_revision', revision }.
    • The editor sends the revision its draft was built on, keeps the draft when the save is refused, and shows the notice with a Load their version action.
    • restoreDocVersion advances the revision when the restored content differs.

    Core tests cover the two-client race, the normal save, the retry, the rename, a save with no base, and the restore. The core suite needs bun run db:test-setup, which this box cannot run (no Docker, no Postgres), so CI is their first run. The web docs suite passes locally with the four new hook tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: docs-featurearea: realtimebugp1 highpoints: 5readyschema changesize: medium

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions