Skip to content

Feature/enhance realtime workflow - #726

Merged
pikann merged 5 commits into
masterfrom
feature/enhance-realtime-workflow
Nov 11, 2026
Merged

pikann merged 5 commits into
masterfrom
feature/enhance-realtime-workflow

Conversation

@pikann

@pikann pikann commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Agent, API and other-user changes now reach the open web app as small, ordered changes instead of "refetch everything". The design follows collaborative editing (Yjs): the update is the delta, a reconnect resumes from where the client was, and the lifecycle is explicit.

The new sync protocol is opt-in (web flag localStorage["paca.realtime.sync"]="1" / VITE_REALTIME_SYNC=true, server SYNC_ENABLED). The plain event path stays in place.

What changed

Resumable change feed

  • The API writes every project change to a capped per-project Valkey Stream (seq + epoch), with an entity snapshot where there is one.
  • Clients join with sync:join and get ordered, gapless sync:changes batches, sync:reset when history cannot be replayed, and heartbeats. A client resumes from its cursor after any absence.
  • Authorization is the same IAM question the plain path asks, answered per account. What an account may not read arrives as a bare skip.
  • Web: sync-engine.ts (cursor/epoch rules), cache-patch.ts (patch the React Query cache from the snapshot; refetch lists only when a filter/sort/group field changed), and an ordered rule table (project-event-rules.ts).

Known gaps closed

  • Transactional outbox: triggers on tasks/sprints write change_outbox in the same transaction; a reconciler announces changes whose announcement was lost, comparing row version with the last logged version.
  • Row versions and If-Match: tasks and sprints carry a version (migration 000083). PATCH with If-Match answers 412 VERSION_CONFLICT on a stale write.
  • Self-echo suppression: X-Client-Op-Id is echoed into the change, so the sender skips its own echo; stale snapshots are dropped by version.
  • First-join race: responses under /projects/{id} carry X-Change-Seq/X-Change-Epoch; the client seeds its cursor from the first one.
  • One event registry: generated events/registry.json, read by API, realtime and web tests (regen: go test ./internal/events -run TestRegistryIsUpToDate -update-registry).
  • Collab across replicas: a room has one owner replica (Valkey lease); other replicas forward to it over Valkey; heartbeats detect dead replicas. New env: COLLAB_CLUSTER, REPLICA_ID, ACCESS_CACHE_TTL_MS. Helm still needs realtime.allowMultipleReplicas=true for >1 replica, now because the polling transport needs sticky sessions.

Docs: services/realtime/README.md (protocol, guarantees, limits, replicas), architecture docs, Helm README/values, apps/e2e/README.md.

Deliberately not done

  • Presence (a product feature, not a sync gap).
  • Optimistic-mutation screens (versions and client op id make them possible).
  • Per-event access-check cost: kept at a 30 s cache (configurable), because IAM attribute conditions change without a role change.
  • If-Match for docs and in the web editor; removing the plain event path.

Testing

  • Unit: API internal Go tests, realtime (142), web (1618) pass.
  • e2e (Chromium, isolated e2e stack): all realtime specs pass (177 passed, 6 skipped), including new sync-gaps (versions, If-Match race, client op id, change head, outbox repairs) and sync-replicas (second realtime container: cross-replica edits, late joiner, killing a replica), plus the sync-ui first-join window test.
  • Not verified: the rest of apps/e2e with the sync flag on, and real GitHub/GitLab paths.
  • Known unrelated failure: interaction-views.spec.ts "Clearing the filter restores all tasks" fails identically on the committed API and web images; "Save only for me" in that file is flaky.

- Introduced SyncHub for ordered delivery of project change logs.
- Added support for syncing changes through a new "sync:join" event.
- Implemented logic to handle missed changes and deliver them in batches.
- Enhanced socket management to track which projects each socket is syncing.
- Added tests to ensure correct behavior of the SyncHub and its interactions.
- Updated permissions to include new project-related events and namespaces.
- Refactored existing code to integrate with the new sync functionality.
…egration

- Add CollabCluster for managing collaborative editing sessions across multiple replicas.
- Introduce ValkeyLeases and ValkeyBus for lease management and message passing using Redis.
- Enhance CollabHub to support room authority checks and room lifecycle management.
- Update configuration to support collaborative editing features, including replica identification.
- Modify socket server to integrate collaborative editing functionality, allowing for room ownership and message routing.
- Implement tests for event registry and sync functionality to ensure proper operation of collaborative features.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

Two findings with concrete fallout before merge: an unbounded retry loop in SyncHub.pump that can wedge a project's feed and hammer a failing permissions endpoint, and the web client discarding its cursor so a refused or restarted sync join loses the gap it was replaying. Two smaller notes inline.

Reviewed changes

Reviewed the full 114-file diff at cf4e1b1b end to end: the API change log and outbox, the realtime sync hub and replica routing, the web sync client and cache rules, plus the migration and tests.

  • Resumable change feed — a per-project Valkey Stream filled by one Lua script (seq + epoch), read back by a new SyncHub that serves sync:join / sync:changes / sync:reset / sync:heartbeat.
  • Authorization — every change in the feed is checked with the same IAM question as the legacy path, and what an account may not read arrives as {seq, skip} so its sequence stays gapless.
  • Cache patching — snapshots patch task/sprint rows in place and lists refetch only when a field a list can filter or group on changed; the old if-chain becomes an ordered rule table.
  • Client cursor and first-join race — ProjectSync keeps epoch/cursor per project, X-Change-Seq seeds a first join, and X-Client-Op-Id lets a client skip its own echo.
  • Transactional outbox and row versions — migration 000083 adds version triggers and change_outbox, a reconciler re-announces lost changes, and task/sprint PATCH honours If-Match with 412.
  • Cross-replica collaboration — per-room Valkey leases plus a per-replica bus so the editors of one document can sit on different replicas.
  • Supporting work — generated event registry, IAM epoch backstop, Helm gate for replicaCount > 1, docs and realtime e2e specs.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Comment thread services/realtime/src/sync.ts Resolved
Comment thread apps/web/src/lib/socket-client.ts Resolved
Comment thread apps/web/src/lib/change-heads.ts Resolved

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Reviewed the 8-file delta since the prior pullfrog review at cf4e1b1b: the four fixes for the findings raised there, plus the accompanying tests.

  • Stopped the sync pump from spinning — SyncHub.pump snapshots each subscriber's cursor before reading a batch and returns when no delivery moved one, so a failing access check costs one read per nudge or poll instead of an unbounded loop that held the project's queue; a new 450-entry backlog test asserts the bounded read count and the clean catch-up.
  • Kept the client cursor — leaveProject no longer deletes the per-project sync state (it is cleared on disconnect/sign-out), so re-entering a project resumes from its cursor; a refused sync:join now fires the reset listeners when the client had a cursor, so the missed window is refetched instead of silently dropped.
  • Seeded the cursor from the lowest head — observeChangeHead keeps the lowest seq for the same epoch regardless of response arrival order, with both orders covered.
  • Removed the row serialisation from the outbox trigger — migration 000083 splits capture into capture_versioned_change for tasks/sprints and capture_unversioned_change for documents, reading the columns directly instead of building to_jsonb(NEW) on every write.
  • Test hygiene — the affected Go tests now use httptest.NewRequestWithContext instead of the deprecated constructor; the touched handler and middleware packages pass locally.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving: all four prior findings are addressed in 65beaff and the delta introduces no new issues.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

The reconciler does not repair a lost hard delete: announced compares versions without looking at the row's operation, so a deleted sprint whose sprint.deleted announcement was lost is treated as already announced.

Reviewed changes

Reviewed the 14-file delta since the prior pullfrog review at 65beaff: outbox pruning and delete handling, the monotonic last-logged index, dropping documents from the outbox, the outbox row ordering, the change-head read timeout, and the accompanying tests and docs.

  • Bounded the outbox — Prune deletes rows older than the change log's retention and Tick calls it at most once a minute, so the table stays bounded even while Valkey is unreachable.
  • Made the last-logged index monotonic — the append script keeps an entity's highest row version so an out-of-order announcement cannot lower it, with a test covering 6→5, the unknown version and a delete.
  • Removed documents from the outbox — the doc trigger, loader, entity constant and repair branch are gone, and the migration and README record why docs are left to the docs pull/push work.
  • Chose the newest outbox row by id — reconcile now uses the highest row id, the order Claim reads, instead of comparing created_at.
  • Capped the change-head read — the middleware gives the Valkey lookup 50 ms and serves the request unstamped when the log is slower, with a slow-reader test.
  • Covered the access-cache burst — a new realtime test asserts one permissions request per account for a 200-change burst and none once warm.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

Reviewed the delta since the prior pullfrog review at cfcdf37b: the hard-delete repair fix, the change-head comment tidy, and their tests.

  • Repaired a lost hard delete — announced now treats a delete outbox row as announced only when a delete was logged, so a sprint hard delete whose sprint.deleted never went out is re-announced instead of skipped; a unit case covers the pre-delete version, and the existing integration test already announces a missing entity as <entity>.deleted.
  • Tidied the change-head docs — the timeout comment is back above changeHeadTimeout and each header constant carries its own doc.
  • Test hygiene — the change-head test uses httptest.NewRequestWithContext.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pikann
pikann merged commit 13900b4 into master Nov 11, 2026
9 checks passed
@pikann
pikann deleted the feature/enhance-realtime-workflow branch November 11, 2026 09:05
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