Skip to content

OBJ-82 — Snapshot persistence to Postgres on cadence - #28

Merged
objectgyan merged 1 commit into
mainfrom
objectgyan/obj-82-m52-snapshot-persistence-to-postgres-on-cadence
May 17, 2026
Merged

objectgyan merged 1 commit into
mainfrom
objectgyan/obj-82-m52-snapshot-persistence-to-postgres-on-cadence

Conversation

@objectgyan

Copy link
Copy Markdown
Owner

Linear

Closes OBJ-82

Summary

Adds durable Y.js session persistence: a per-session timer writes document_yjs_snapshots rows every N seconds (default 30, configurable via Yjs:SnapshotIntervalSeconds). Last-client-disconnect triggers a final snapshot. New sessions hydrate from the most recent snapshot on first connect or cold-start reconnect. Each snapshot also creates a document_versions row for auditability with dedup: skipped if no Y.js update since last snapshot.

API / data changes

  • New table: document_yjs_snapshots(id uuid pk, document_id uuid fk→documents, state_vector bytea, update_log bytea, updated_at timestamptz, tenant_id uuid) with indexes on document_id and tenant_id.
  • EF Core migration: AddDocumentYjsSnapshots (20260517142955).
  • New singleton service: YjsSnapshotService — handles DB writes and hydration using IServiceScopeFactory for scoped AppDbContext access.
  • New diagnostic endpoint: GET /api/collab/snapshots/{id}/count — returns { count: int } for validation/diagnostic use.
  • appsettings.json: new Yjs:SnapshotIntervalSeconds key (default 30).
  • YDocSession: added HydrationTask, SnapshotTimer, HasUpdatesSinceLastSnapshot, SetStateAccumulator().
  • YDocRegistry: injects YjsSnapshotService; starts cadence timer per session; triggers final snapshot on last-disconnect; awaits hydration before sending sync-step2.
  • YjsWebSocketHandler: awaits session.HydrationTask (cap 5s) before SendSyncStep2Async so reconnecting clients receive persisted state.

Plan: plans/projects/document-builder/plan.md (§ OBJ-82)
Contract: plans/projects/document-builder/tickets/api-contracts/OBJ-82-yjs-snapshot.md
Validation script: scripts/validate-obj-82.ps1 — exit 0
Outcome report: plans/projects/document-builder/tickets/validation-reports/OBJ-82-outcome.md — VERDICT: PASS

For QA

  • Set Yjs__SnapshotIntervalSeconds=3 in environment, start API, connect a WS client to /ws/docs/{id}, send a sync-update, wait 5s, SELECT count(*) FROM document_yjs_snapshots WHERE document_id='{id}' → > 0.
  • Kill and restart API; connect a new WS client — step2 should contain the previously written state bytes.
  • GET /api/collab/snapshots/{id}/count returns { count: N }.
  • Two snapshots with identical state vector → only one document_versions row (dedup).

Checklist

  • Tests added/updated
  • Validation script committed and runs locally to exit 0
  • EF Core migration reversible (Down drops the table)
  • Breaking changes called out above (or n/a)

Adds document_yjs_snapshots table with EF Core entity, migration, and
per-session snapshot timer (default 30s, configurable via Yjs:SnapshotIntervalSeconds).
Last-client-disconnect triggers a final snapshot; new sessions hydrate from DB on
first connect. Each snapshot cross-writes a document_versions row for auditability
with dedup: skipped if StateAccumulator unchanged since last snapshot.
@linear-code

linear-code Bot commented May 17, 2026 •

Copy link
Copy Markdown
OBJ-82 M5.2 — Snapshot persistence to Postgres on cadence

Milestone: M5 · Wave: 2

Summary

Persist Y.Doc state vector + update log to Postgres every N seconds (configurable) and on last-client-disconnect. Hydrate on first reconnect. Feeds M4 versioning.

Acceptance

  • Snapshot cadence configurable; default 30s
  • document_yjs_snapshots table with (document_id, state_vector bytea, updated_at)
  • Server restart preserves doc state for reconnecting clients
  • Snapshot creates a new entry in document_versions (M4) for auditability

Validation

  • DB check: edit doc, kill server, restart, reconnect, value persists; SELECT count(*) FROM document_yjs_snapshots > 0
  • Live API check: WS client edits → wait cadence → restart API → reconnect → state intact
  • Script: scripts/validate-obj-NNN.ps1

Branch / PR

Branch from main; target main. No trailers/footer.

Review in Linear

@objectgyan
objectgyan merged commit bc7ae0e into main May 17, 2026
2 checks passed
objectgyan added a commit that referenced this pull request May 17, 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.

1 participant