Skip to content

fix: keep saving a document while the user types, retry a failed save, and save it with fewer queries - #1037

Merged
croffasia merged 1 commit into
mainfrom
fix/1007-document-autosave
Nov 29, 2026
Merged

croffasia merged 1 commit into
mainfrom
fix/1007-document-autosave

Conversation

@croffasia

Copy link
Copy Markdown
Owner

What

The document editor keeps saving while the user types and shows "Changes have not synced" only when saves really fail. A failed save is retried with a growing delay, and a save makes fewer database round trips.

  • Only refused saves (409) count against the limit of 5 rounds. A save that succeeds acknowledges its own steps locally, so the editor does not read them back.
  • A network error, 5xx or 429 is retried after 200 ms, doubling up to 60 s. The banner appears only when the delay passes 1 s and stays until a save succeeds.
  • A save is a lock, one read of the session and comment anchors, and one write statement. Reading changes is one statement with no lock. The save returns { version, title, updatedAt, updatedByUserId } instead of the whole page.
  • Migration 0207_document_collaboration_trigger drops the session's copy of the page JSON. A trigger ends the session when the page JSON is written by anything other than the editor's own save (MCP, PATCH, revision restore, imports). Comment anchor updates move the documents live-update counter once per statement.

Why

Closes #1007

The save loop counted successful saves against its 5-round limit, so continuous typing on a slow connection set the status to error between keystrokes. Each save also ran about a dozen queries plus several per comment below the edit, and read the whole page back.

How to test

  1. bun run db:migrate
  2. Open a document, throttle the network in DevTools, and type without stopping. No banner appears, and every save returns 200.
  3. Open the same page in two tabs and type in both at once. Both texts end up in both tabs and on the server.
  4. While the page is open, change it with the MCP update_document tool or restore a revision. The open editors reload the new text and keep saving.
  5. cd apps/api && bun test --env-file=../../.env.test src/modules/documents src/modules/sync
  6. cd apps/web && bun test --preload ./test/setup.ts src/features/documents/hooks/useDocumentCollaboration.test.tsx

Checklist

  • bun run typecheck passes
  • bun run lint and bun run format:check pass
  • Developed with an AI agent: tidy, code-review and security-audit from .agents/skills/ ran on the new code
  • Tests added or updated for the changed behaviour
  • Database schema changed: migration generated with bun run db:generate and committed
  • New environment variables documented in .env.example
  • Docs updated (README.md or docs/). AGENTS.md changed only as docs/dev/agents-md.md allows

Screenshots

No visual change.

@croffasia
croffasia merged commit 7914b60 into main Nov 29, 2026
7 checks passed
@croffasia
croffasia deleted the fix/1007-document-autosave branch November 29, 2026 09:33
@github-actions github-actions Bot mentioned this pull request Nov 29, 2026
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.

[bug]: document editor flashes "Changes have not synced" while typing, even though every save succeeds

1 participant