Repository navigation
Conversation
- Remove the hidden range locally from event.rewind (message_id, inclusive,
created_at) on both channel.rewound and notification.channel_rewound,
before the lists route the channel, so the preview and sort key move.
- Stop reading event.unread_count (the user's total) as the channel's
count; lower it by the hidden unread messages instead.
- Keep the unread snapshot; drop only hidden IDs and hidden unread.
- reload({ afterInFlight }) queues one trailing reload behind a running one
and resolves whether it reloaded; re-route the channel after it.
- notification.channel_rewound re-inserts into every list.
- Flag the channel's threads stale; delete hidden rows from the offline DB.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n-parent threads Address the independent review: the unknown-range fallback now takes the messages the reload removed out of the unread snapshot (count included), threads are flagged stale only when the rewind hid a loaded message, and a thread whose parent was hidden is not reloaded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
👋 Thanks, Laurent! I'm on it.How to work with me
CommandsComment any of these on the PR:
|
|
/jeffery review |
There was a problem hiding this comment.
Round 1 · 🆕 1 new · 1 should-fix to go
| Finding | Where | |
|---|---|---|
| 🟡 Should Fix | Two rewinds in one reload subtract unread twice | channel.ts:1981 |
Push a fix and I'll check it in the next round.
…once per reload chain Rewinds whose reloads share a request (a trailing reload) picked out the same candidates, and each callback subtracted them from the unread snapshot. The candidates now live in one map on the channel: a message leaves it (and the snapshot count) once it is gone after a reload, and the rest is dropped once no reload is queued. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
/jeffery review |
…n the snapshot count A rewind can hide thread replies the channel does not hold, so the channel's threads are flagged stale even when no loaded channel message was hidden (hidden-parent threads are still skipped). The unread snapshot no longer counts soft-deleted hidden messages, matching the own unread count. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🎉 Approved · round 2
Nice work, Laurent! Nothing blocking is left on aeb7872f394b. Ship it 🚀
Left open, your call:
- consider: Locally removed message can leave the unread snapshot twice (
src/channel.ts:1931) - consider: Truncate during a pending rewind reload can double-subtract the unread snapshot count (
src/channel.ts:2000)
Withdrawing approval until db9531e is reviewed.
A message a later rewind removed locally leaves the pending rewind candidates, and a truncation drops them with the unread snapshot, so a pending reload callback cannot subtract them again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
/jeffery review |
| ? 0 | ||
| : removed.filter( | ||
| (message) => | ||
| message.type !== 'deleted' && |
There was a problem hiding this comment.
consider: Unread snapshot count stays too high after a post-seed soft delete
The type !== 'deleted' filter assumes the snapshot never counted soft-deleted messages, but only the seed at MessagePaginator.ts:647 guarantees that. The message.deleted handler at channel.ts:2762 lowers only the channel's own unread count, so a message soft-deleted after seeding is still in the snapshot count, and a later rewind that hides it now skips it, leaving the count one too high. Lowering the snapshot in the message.deleted handler as well would make the filter correct.
Useful? React 👍 or 👎. Disagree? Reply here.
There was a problem hiding this comment.
🎉 Approved · round 3
Nice work, Laurent! Nothing blocking is left on 8d51283621e7. Ship it 🚀
Left open, your call:
- consider: Unread snapshot count stays too high after a post-seed soft delete (
src/pagination/paginators/MessagePaginator.ts:672) - consider: Truncation test passes an ISO string, so the paginator never truncates (
test/unit/channel.test.js:2747)
Withdrawing approval until e93d40d is reviewed.
There was a problem hiding this comment.
Round 4 · ✅ 3 fixed · 🎉 Approved
Nice work, Laurent! Nothing blocking is left on e93d40dc9e8e. Ship it 🚀
Left open, your call:
- consider: Unread snapshot count stays too high after a post-seed soft delete (
src/pagination/paginators/MessagePaginator.ts:672) - consider: Truncation test passes an ISO string, so the paginator never truncates (
test/unit/channel.test.js:2747)
removeRewoundMessages removes the server-confirmed messages created after the channel's post-rewind last_message_at and up to the rewind's created_at, so it no longer needs the target message to be loaded. It removes them through removeReconciledItems, which also deletes them from the offline DB.
…messages lost some Both rewind events remove the hidden range from the message and pinned lists on arrival, as channel.truncated does. A watcher reloads only when the rewind took messages from the newest loaded window, which can leave the list short or empty. reload() is master's again: the afterInFlight option, the queued reload, the unread candidates and the ingestChannel call after the reload are gone.
There was a problem hiding this comment.
🎉 Approved · round 5
Nice work, Laurent! Nothing blocking is left on 917712b55a63. Ship it 🚀
Left open, your call:
- consider: Unread snapshot count stays too high after a post-seed soft delete (
src/pagination/paginators/MessagePaginator.ts:672) - consider: Newest page not fetched when only an unsent message is left (
src/channel.ts:2807) - consider: Offline DB range delete may drop unsent messages (
src/offline-support/types.ts:234)
…f the list The server answers for a thread whose parent a rewind hid as if it did not exist. The thread now records that in `parentHiddenAt`, so an integrator can leave it, and no longer reloads. ThreadManager removes it from `client.threads.paginator`. The per-thread rewind handling moves to `Thread.handleChannelRewound`, which the manager calls like the other `handle*` methods.
… during A thread reload's response may predate a rewind that arrives while it runs, and its hydrate clears the stale flag the rewind would set, so hidden replies stayed. The thread now records the rewind and marks itself stale once the reload ends, which reloads it when shown. The channel's matching flag is renamed to `_rewoundDuringReload`, so both are named after the cause.
Withdrawing approval until ba18501 is reviewed.
There was a problem hiding this comment.
Round 6 · ✅ 4 fixed · 🎉 Approved
Nice work, Laurent! Nothing blocking is left on ba1850193b13. Ship it 🚀
Left open, your call:
- consider: Unread snapshot count stays too high after a post-seed soft delete (
src/pagination/paginators/MessagePaginator.ts:672) - consider: Newest page not fetched when only an unsent message is left (
src/channel.ts:2807) - consider: Offline DB range delete may drop unsent messages (
src/offline-support/types.ts:234)
…ages are left A rewind keeps unsent messages, and `latestMessage` can be one of them, so a non-watching member whose loaded server messages were all hidden kept a list of only that message. `rewind()` now tells whether the newest interval still holds a server-confirmed message, and the newest page is queried when it does not. The unsent message stays in place.
Withdrawing approval until af2f167 is reviewed.
There was a problem hiding this comment.
Round 7 · ✅ 5 fixed · 🎉 Approved
Nice work, Laurent! Nothing blocking is left on af2f16706b4d. Ship it 🚀
Left open, your call:
- consider: Unread snapshot count stays too high after a post-seed soft delete (
src/pagination/paginators/MessagePaginator.ts:672) - consider: Offline DB range delete may drop unsent messages (
src/offline-support/types.ts:234)
MartinCupela
left a comment
There was a problem hiding this comment.
The rewrite fixed most of what came up on #2045: the exact range from the event, no reload() API change, isServerConfirmedMessage reused, threads judged by timestamps, direct tests for the paginator, threads and offline DB.
The comments below are about shape more than behaviour. Most are refactors. Two are behaviour claims, and each was checked with a throwaway test on af2f1670; the comment says what the test showed.
The ones I'd settle before release are the range helper, the paginator rewind() and the offline DB contract. All three are new public API, which React Native and integrators will build on.
| case 'notification.channel_rewound': { | ||
| if (event.channel) channel.data = { ...channel.data, ...event.channel }; | ||
| if (!event.rewind) break; | ||
| const range = { |
There was a problem hiding this comment.
The rewind range is built from the event in three places:
- here;
Thread.handleChannelRewound();AbstractOfflineDB.handleChannelRewoundEvent().
Each reads event.channel?.last_message_at and event.rewind.created_at itself. The "is this message in the range" check is then written again in MessageIntervalPaginator.rewind(), twice in Thread, and once more in SQL in React Native.
Could this be one helper that builds the range from the event, plus one predicate that checks a timestamp against it? Then all of these agree by construction.
oliverlaz
left a comment
There was a problem hiding this comment.
This addresses 6 of the 7 points from the #2045 review: the total unread_count, the re-sort ordering, the non-watcher preview, the reload race, the snapshot reseed, and threads. The offline DB is handled for live events, but the sync replay still misses it (see below).
I found 6 new issues, all inline. Three of them (the two range-bound comments and the sync replay one) come from one cause: the client rebuilds the hidden range from channel.last_message_at and rewind.created_at, but the server hides [hidden_from, hidden_to], and these values don't always match. The simplest fix is probably on the backend: add hidden_from / hidden_to to ChannelRewindResponse and use those here. That would also let the sync replay apply every range, not only the newest.
| if (event.channel) channel.data = { ...channel.data, ...event.channel }; | ||
| if (!event.rewind) break; | ||
| const range = { | ||
| after: event.channel?.last_message_at, |
There was a problem hiding this comment.
channel.last_message_at isn't always where the hidden range starts.
- A send that races the rewind: when a message commits after the rewind computed the channel's edges, the backend keeps that later
last_message_at. Thenafteris later than every hidden message,rewind()removes nothing, andremovedFromHeadis false. The watcher never reloads, which fix(channel): drop rewound messages on channel.rewound (AI-3204) #2045 at least did. Example: the user hits regenerate while the agent's reply is still being sent. inclusiverewinds: the server hides from the target onwards, butlast_message_atbecomes the message before the target. Thread-only replies created between those two messages are kept by the server, yet they fall inside(after, upTo]. They are removed from the pinned list, deleted from the offline DB (handleChannelRewoundEvent), and their threads are marked stale (Thread.handleChannelRewounduses the same bound).
Suggestion: use the rewind's own bounds (hidden_from / hidden_to, once the backend sends them). Alternatively, derive the start from rewind.message_id and inclusive when the target is loaded, and reload when the bounds can't be known.
| if (!event.rewind) break; | ||
| const range = { | ||
| after: event.channel?.last_message_at, | ||
| upTo: event.rewind.created_at, |
There was a problem hiding this comment.
rewind.created_at isn't where the hidden range ends.
- The server hides up to
hidden_to: the rewinding pod's clock or the newest message'screated_at, whichever is later, plus 1µs. created_atis the database clock at insert, and nothing orders the two. The send clamp can also give the newest message acreated_atin the database's future.
So a hidden message can be kept here, or a message sent just after hidden_to can be removed locally and deleted from the offline DB while the server still shows it. The same bound is used in handleChannelRewoundEvent and Thread.handleChannelRewound.
| let queries = [ | ||
| ...(await this.deleteMessagesForChannelInRange({ | ||
| cid, | ||
| after: channel.last_message_at, |
There was a problem hiding this comment.
In sync replay this range is usually empty. /sync builds the replayed channel.rewound from the rewind row, with the channel hydrated to its current state. So channel.last_message_at is the channel's newest message now, not the value right after the rewind. In the regenerate flow (a rewind, then a new agent reply), after is later than rewind.created_at, nothing is deleted, and the hidden answer comes back from the offline DB on the next cold start.
Also, the backend replays only the newest rewind per channel. An earlier rewind that hid a different range is never applied to the DB.
So the PR's claim that this also runs during sync replay and removes the rows of channels not in memory holds only when no message was sent after the rewind. Suggestion: until the event carries the rewind's own bounds, have a replayed rewind drop the channel's stored messages so the next query refills them.
There was a problem hiding this comment.
No, this should not be the case.
In sync replay this range is usually empty.
Not sure what gives you this impression, maybe a better explanation here would be nice ?
There was a problem hiding this comment.
Okay so I did a bit more thinking around this and have asked the backend team to extend parts of their API to make this deterministic for us and they have agreed ! So this'll be resolved then
| const { removed, removedFromHead, serverMessageLeft } = | ||
| this.messagePaginator.rewind(range); | ||
| this.pinnedMessagesPaginator.rewind(range); | ||
| if (removed.length) this._lowerUnreadCountForDeletedMessages(removed); |
There was a problem hiding this comment.
This only lowers the count by the hidden messages that are loaded. The server takes every hidden unread message out of the count.
- Non-watchers are corrected only when nothing server-confirmed is left in the head interval.
- Watchers are corrected only when
removedFromHeadtriggers a reload. Even then,ChannelManagerre-inserted the channel before the reload and doesn't route it again afterwards.
So a has_unread list can keep a channel the server counts as read. Example: a non-watcher has 30 unread and 25 loaded, and the rewind hides all 30 but keeps one older message. The badge shows 5 and the channel stays in the unread list.
Suggestion: route the channel again after the reload or the fallback query (client.channelManager.ingestChannel(this)). Query when hidden unread messages may not be loaded, for example when removed.length < rewind.hidden_count_at_rewind.
…hannel A query in flight when a rewind arrives (a first watch, a query, a paginator query or a reload) may return a page read before it and place hidden messages again. The rewind now records a follow-up while `isQueryingChannel`, and once no query or reload is left a watched channel reloads and an unwatched one queries its newest page without a watch. `reload()` no longer knows about rewinds.
A rewind keeps unsent messages in memory, as the server never had them, but the offline DB range delete removed their rows, so a failed message that was never retried was gone after a cold start. `deleteMessagesForChannelInRange` takes `keepMessageIds`, which `handleChannelRewoundEvent` fills with the channel's unsent messages from `client.messageStore`. The unsent check is now the `isServerConfirmedMessage` util, shared with the paginator.
`MessageIntervalPaginator.isServerConfirmedMessage` was a protected method only so that `MessagePaginator` could call it; no paginator overrides it. Both now call the `isServerConfirmedMessage` util directly and the method is gone.
…pshot after a rewind `MessagePaginator.rewind()` set the list's latest message time from the range's lower bound and trimmed the unread snapshot with the channel's unread rule, which is wrong for a thread's reply list and for an inclusive rewind's bound. The override is gone: the channel's rewind case sets the time from `event.channel.last_message_at` and calls the now public `forgetInUnreadSnapshot`.
|
Heya @Loschcode ! While reworking some more stuff I realized that we currently cannot effectively determine the unread count for rewound channels in every possible scenario. For example: Unread count: a member who isn't watching has 30 unread in an agent channel and we have the newest 25 loaded. A rewind hides all 30. We can only subtract the 25 we have, so the channel shows 5 unread and stays in unread filtered lists, while the server says 0. But we can't realistically know about the 0 until a query finishes and we get the actual server count (from anywhere). Concretely, it would help if we had:
|
Withdrawing approval until d932509 is reviewed.
There was a problem hiding this comment.
Round 8 · 🆕 1 new · 1 should-fix to go
| Finding | Where | |
|---|---|---|
| 🟡 Should Fix | Rewind deletes unsent messages only the offline DB holds | offline_support_api.ts:920 |
➡️ Round 9 started on be137a1.
There was a problem hiding this comment.
🎉 Approved · round 8
Nice work, Laurent! Nothing blocking is left on d9325093d079. Ship it 🚀
Left open, your call:
- consider: Unread snapshot count stays too high after a post-seed soft delete (
src/pagination/paginators/MessagePaginator.ts:672)
|
@isekovanic from the backend side, all three make sense. Shapes I'd propose (@Loschcode wdyt):
|
|
Just to add on here @xicoalmeida :
|
Withdrawing approval until be137a1 is reviewed.
There was a problem hiding this comment.
Round 9 · ✅ 5 fixed · 💬 1 dropped · 🎉 Approved
Nice work, Laurent! Nothing blocking is left on be137a1e033e. Ship it 🚀
Left open, your call:
- consider: Unread snapshot count stays too high after a post-seed soft delete (
src/pagination/paginators/MessagePaginator.ts:672)
|
@isekovanic yes to both: the ranges go on |
Ticket: https://linear.app/stream/issue/AI-3204/stream-chat-js-drop-rewound-messages-from-channel-state-on
#2045 added
channel.rewoundhandling and was merged before its review was addressed, then reverted onmaster(12def19, released in 10.4.0). This PR brings it back with the review addressed, so its diff againstmasteris the whole feature. The offline DB part needs GetStream/stream-chat-react-native#4045 (RN-556).CLA
Description of the changes, What, Why and How?
A rewind hides every message created after the channel's new
last_message_atup torewind.created_at, thread replies included. The server sendschannel.rewoundto watchers andnotification.channel_rewoundto members who are connected but not watching. Both events carry the full channel and therewind, so the client knows the hidden range exactly and removes it locally, the waychannel.truncatedis handled.Channel (
channel.ts)Both events go through one case in
_handleChannelEvent:channel.datatakes the event's channel.messagePaginator.rewind({ after, upTo })andpinnedMessagesPaginator.rewind(...)remove the loaded server-confirmed messages in the range, across all intervals. Sending, failed anderrormessages stay._lowerUnreadCountForDeletedMessages, as for hard deletes). The event'sunread_countis the user's total across channels and is not read._rewoundDuringReload).reload()itself is unchanged, so recovery and other callers behave as onmaster.Paginators
MessageIntervalPaginator.rewind()removes the range in one coalesced batch. It returns the removed messages, whether the newest interval lost any (removedFromHead), and whether it still holds a server-confirmed message (serverMessageLeft).MessagePaginator.rewind()also sets the latest message time toafter, so the sort key is right even when the hidden messages were not loaded. It takes the hidden messages out of the unread snapshot:firstUnreadMessageId/lastReadMessageIdare cleared only when they point at a hidden message, and the count drops by the hidden unread ones. The separator for the unread messages that remain stays.Channel lists (
ChannelManager.ts)Both events re-insert the channel (
reinsertItem), so lists sorted or filtered bylast_message_ator the unread count move or drop it. The channel handler runs before client listeners, so the new sort keys are already in place.Threads (
thread.ts,thread_manager.ts)ThreadManagerpasses both events to each registered thread of the channel (Thread.handleChannelRewound), like its other events:getThreadanswers that it does not exist,queryThreadsleaves it out). It gets the newThreadState.parentHiddenAt(the rewind'screated_at), never reloads, and leavesclient.threads.paginator. An opened one stays registered, so integrators can watchparentHiddenAtand leave the screen.Offline DB (
offline-support)OfflineDBApi.deleteMessagesForChannelInRange({ cid, after, upTo })and the matching abstract method onAbstractOfflineDB.handleEventroutes both events tohandleChannelRewoundEvent, which deletes the range, stores the channel data and, for a loaded channel, the own read with the lowered unread count. It also runs during sync replay, so rows of channels that are not in memory are removed too.Review comments on #2045
unread_countis the user's total: not read; the own count is lowered locally.ChannelManagerruns.ThreadManager; the offline DB deletes the range.Not covered
master.masterdrops them in memory and in the DB.reply_countfor a kept thread still counts hidden replies (backend).API changes
AbstractOfflineDB.deleteMessagesForChannelInRangeis a new abstract method, so a custom offline DB has to implement it. The React Native SDK does in #4045.ThreadState.parentHiddenAt, and new publicrewind()onMessageIntervalPaginator/MessagePaginator.Tests
channel.test.js: local removal, kept unsent messages, pinned messages, the unread count and snapshot, the reload and the one after a running reload, the non-watcher query (including only an unsent message left).MessagePaginator.test.ts:rewind()range bounds, the returned flags, the latest message time, one publish.ChannelManager.test.ts: re-insert and re-sort for both events,has_unreaddrop.threads.test.ts: stale marking, hidden parents (parentHiddenAt, list removal, no reload), the reload race.offline_support_api.test.ts:handleChannelRewoundEventand the event routing.yarn lint,yarn typesand the fullyarn testpass (5659 tests). The 2 unhandled errors frompoll.test.jsand the connection-id test also occur onmaster.Changelog
parentHiddenAtand leaves the thread list.🤖 Generated with Claude Code