Skip to content

fix(database): Mongo live-update interventions (watch, token, tickets, recovery) - #1605

Merged
kkopanidis merged 6 commits into
feat/database-realtime-mongofrom
cursor/mongo-1601-rebase-b255
Sep 13, 2026
Merged

kkopanidis merged 6 commits into
feat/database-realtime-mongofrom
cursor/mongo-1601-rebase-b255

Conversation

@kkopanidis

Copy link
Copy Markdown
Contributor

Rebases #1601 onto current #1600 (9e6356ef) and closes the Mongo stack-review P1s. Do not merge #1600 or #1601. Do not retarget to main.

Hermes graft

Watch + token

  • $match opted-in collections + insert/update/replace/delete; still deliver drop/rename/invalidate; $project out fullDocument / updateDescription / fullDocumentBeforeChange.
  • Reopen the watch when the opt-in set changes.
  • Serialize handleChange. Persist _id for skipped events immediately; persist opted-in tokens after successful bus + socket emit (at-least-once duplicates OK).
  • Do not clear the resume token on CursorKilled 237 (or 136). Clear on 280 / ChangeStreamHistoryLost.
  • Drop resumeToken from the public change payload (Redis only).

Tickets / recovery / ops

  • Core Auth/Admin middleware uses hermes isSocketHandshake. Deleted the Core URL heuristic. POST /realtime/ticket with a ticket is not a handshake.
  • Recoverable disconnect does not wipe realtime:socket:*. Recover restores /database/ Redis membership and re-auths.
  • shutdown() closes the watch and releases the Redis leader lock (SIGTERM/SIGINT).
  • Docs: replica set / Atlas required; Helm bundled Mongo stays standalone → idle; Compose rs0 can break existing volumes; do not also relay database:change:* if the same client is on /database/; live-tail, not backfill.

Tests

  • Filtered event advances the resume token
  • Overlapping handlers stay ordered; token persists after emit
  • 237 does not clear; 280 does
  • Ticket cannot mint a ticket; handshake polling without sid is allowed
  • $match follows opt-in
  • Recovered /database/ stays in the authorized list

Out of scope

Open in Web Open in Cursor 

kkopanidis and others added 5 commits September 13, 2026 02:36
…ger) (#1604)

* fix(router): harden event relays for HA and auth lifetime

EventBus uses a single Redis message dispatcher with subscriberId maps,
SIGTERM/SIGINT shutdown, and subscribe-after-ACK. Hermes installs engine
middleware once, skips empty-room namespace broadcasts, demotes hot-path
logs, and re-authenticates recovered /events/ subscriptions. EventRelayManager
gains coalesced periodic reconcile, compiled templates, inbound caps,
TTL-cached emit-time ReBAC, room eviction, backpressure, preview API, and
metrics/docs aligned with the interventions plan.

* fix(router): address #1604 re-review P1 and P2 follow-ups

Recovery re-auth keeps per-user subscription state across disconnect;
emit-time ReBAC distinguishes unavailable vs deny; EventBus drops process
signal handlers and adds subscribeAck; manager subscribes only after Redis
ACK and broadcasts evictRelayIds on refresh; Hermes backpressure uses local
sockets without emit acks; cache is bounded; preview caps sample JSON;
handshake matcher rejects ticket/sid/non-namespace paths; socket middleware
rebind avoids duplicate registration on first sockets enable.

* fix(hermes): restore production tsc for engine middleware chain

Type the Socket.IO engine middleware runner as Express NextFunction so
recursive callbacks match registerGlobalMiddleware. Use Logger.info for
socket trace lines (IConduitLogger has no debug).

* fix(router): import Express Response for socket global middleware

_rebindSocketGlobalMiddlewares passes handlers typed with fetch Response
because Response was not imported from express, failing tsc against Hermes
registerSocketGlobalMiddleware.

* fix(router): quit EventBus on module shutdown signals

Stop event relays and call grpcSdk.bus.quit() from Router.shutdown(),
registered on SIGTERM/SIGINT with process exit so Redis teardown is not
left to SDK signal handlers. Tighten registerGlobalMiddleware typing and
trim handshake helper comment (deslop).

* fix(router): pass-3 recovery, scoped emit, and subscription hygiene

Recovery re-auth keeps membership on Authorization UNAVAILABLE and only
leaves on deny; re-check subscriptions for restored er: rooms via room map.
Prune user subscriptions on disconnect when no other socket holds them.
Scope relay emits to receivers in the target room; use Engine.IO
writeBuffer/writable for backpressure. Drop hot-path Hermes socket info logs.

* fix(router): pass-4 recovery map, backpressure, emit tests

Store relay subs on socket.data; resolve recovered rooms from context and
TTL room map without last-writer userId. Prune room map when unused on
disconnect. Backpressure uses writeBuffer depth only. Add Hermes/router
tests for room-scoped emit and queue metric.

* fix(router): clear eventRelaySubs from socket.data on recovery deny

* fix(router): clear socket.data subs on recovery fail-closed leave
Split validateEventRelayInput into per-field parsers to reduce complexity.
Silence unused-parameter lint in EventBus test FakeRedis stub.
Keep Event Relays Hermes (engine.use, recovery, writeBuffer). Graft only
port-keyed Socket.IO adapter isolation so Admin and Router do not double-deliver.
…, recovery)

Rebase #1601 onto current Event Relays Hermes and close the stack-review P1s:
watch $match/$project, serialized handleChange, persist resume token after
successful emit, keep token on CursorKilled 237, hermes isSocketHandshake,
restore /database/ Redis on recover, and shutdown the watch plus leader lock.

Co-authored-by: Konstantinos Kopanidis <kkopanidis@users.noreply.github.com>
@cursor
cursor Bot force-pushed the cursor/mongo-1601-rebase-b255 branch from 60d5192 to 1f395ef Compare September 13, 2026 12:50
Failed socketPush/bus no longer advances the resume token. ReBAC
unavailable no longer removeUser. Auth middleware 401s a realtime
ticket on POST /realtime/ticket. Drop/rename/invalidate reopen the watch.
@kkopanidis
kkopanidis marked this pull request as ready for review September 13, 2026 14:00
@kkopanidis
kkopanidis merged commit bc32384 into feat/database-realtime-mongo Sep 13, 2026
11 of 12 checks passed
@kkopanidis
kkopanidis deleted the cursor/mongo-1601-rebase-b255 branch September 13, 2026 14:01
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.

2 participants