Skip to content

fix(channel): remove rewound messages from channels, threads and the offline DB (AI-3204) - #2052

Open
Loschcode wants to merge 29 commits into
masterfrom
laurentschaffner/ai-3204-channel-rewound-followup
Open

Loschcode wants to merge 29 commits into
masterfrom
laurentschaffner/ai-3204-channel-rewound-followup

Conversation

@Loschcode

@Loschcode Loschcode commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Ticket: https://linear.app/stream/issue/AI-3204/stream-chat-js-drop-rewound-messages-from-channel-state-on

#2045 added channel.rewound handling and was merged before its review was addressed, then reverted on master (12def19, released in 10.4.0). This PR brings it back with the review addressed, so its diff against master is the whole feature. The offline DB part needs GetStream/stream-chat-react-native#4045 (RN-556).

CLA

  • I have signed the Stream CLA (required).
  • Code changes are tested

Description of the changes, What, Why and How?

A rewind hides every message created after the channel's new last_message_at up to rewind.created_at, thread replies included. The server sends channel.rewound to watchers and notification.channel_rewound to members who are connected but not watching. Both events carry the full channel and the rewind, so the client knows the hidden range exactly and removes it locally, the way channel.truncated is handled.

Channel (channel.ts)

Both events go through one case in _handleChannelEvent:

  • channel.data takes the event's channel.
  • messagePaginator.rewind({ after, upTo }) and pinnedMessagesPaginator.rewind(...) remove the loaded server-confirmed messages in the range, across all intervals. Sending, failed and error messages stay.
  • The own unread count is lowered by the hidden messages that counted as unread (_lowerUnreadCountForDeletedMessages, as for hard deletes). The event's unread_count is the user's total across channels and is not read.
  • Watchers: when the newest loaded messages lost some, the channel reloads once, which refills a full page and brings the server's read state. If a reload is already running, its request may predate the rewind, so one more reload runs after it (_rewoundDuringReload). reload() itself is unchanged, so recovery and other callers behave as on master.
  • Non-watchers: a reload would start a watch, so there is none. The newest page is queried without a watch only when the rewind left no server-confirmed message in the newest interval.

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 to after, 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 / lastReadMessageId are 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 by last_message_at or 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)

ThreadManager passes both events to each registered thread of the channel (Thread.handleChannelRewound), like its other events:

  • A thread holding a reply newer than the kept messages is marked stale, so it reloads now if open, or when next opened. A rewind during a thread reload marks it stale again once the reload ends.
  • A thread whose parent was hidden no longer exists on the server (getThread answers that it does not exist, queryThreads leaves it out). It gets the new ThreadState.parentHiddenAt (the rewind's created_at), never reloads, and leaves client.threads.paginator. An opened one stays registered, so integrators can watch parentHiddenAt and leave the screen.

Offline DB (offline-support)

  • New OfflineDBApi.deleteMessagesForChannelInRange({ cid, after, upTo }) and the matching abstract method on AbstractOfflineDB.
  • handleEvent routes both events to handleChannelRewoundEvent, 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

  1. unread_count is the user's total: not read; the own count is lowered locally.
  2. The re-insert sees old sort keys: the hidden messages are removed synchronously before ChannelManager runs.
  3. Non-watchers keep the hidden preview and position: the range is removed locally, the list re-sorts, and the newest page is queried when nothing server-confirmed is left.
  4. A rewind during another reload is dropped: one more reload runs after the running one.
  5. The reseed clears the separator: no reseed; only the hidden messages leave the snapshot.
  6. Threads and offline DB: threads are handled by ThreadManager; the offline DB deletes the range.

Not covered

  • A rewind that only touches an older interval the user jumped to is not refreshed, the same as a truncation or hard delete on master.
  • Sync replay stores the unread count from before the rewind for a channel that is not loaded. The next query corrects it.
  • The offline DB delete removes unsent rows in the range too. They stay in memory, but a failed message that was never retried is gone after a cold start. Truncation on master drops them in memory and in the DB.
  • The server's reply_count for a kept thread still counts hidden replies (backend).

API changes

  • AbstractOfflineDB.deleteMessagesForChannelInRange is a new abstract method, so a custom offline DB has to implement it. The React Native SDK does in #4045.
  • New ThreadState.parentHiddenAt, and new public rewind() on MessageIntervalPaginator / 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_unread drop.
  • threads.test.ts: stale marking, hidden parents (parentHiddenAt, list removal, no reload), the reload race.
  • offline_support_api.test.ts: handleChannelRewoundEvent and the event routing.
  • New logic was mutation-checked. yarn lint, yarn types and the full yarn test pass (5659 tests). The 2 unhandled errors from poll.test.js and the connection-id test also occur on master.

Changelog

  • A rewind now removes the hidden messages at once for watching and non-watching members, keeps the unread separator for the messages that remain, and updates channel lists, threads and the offline DB. A thread whose parent was hidden gets parentHiddenAt and leaves the thread list.

🤖 Generated with Claude Code

Laurent Schaffner and others added 2 commits December 1, 2026 12:26
- 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>
@getstream-metis

getstream-metis Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

👋 Thanks, Laurent! I'm on it.

How to work with me

  • 💬 Disagree with a finding? Reply in its thread and tell me why. I'll withdraw it, or answer with evidence.
  • 🔁 Fixed it? Just push. The next round checks your fix and resolves the thread.
  • 👍 👎 Rate my findings. I learn what your team doesn't want flagged.
  • ✅ I approve once nothing blocking is left: must-fix always blocks, should-fix only in the first two rounds. CI isn't part of that; branch protection still holds the merge until it's green. On repos that require a human approval, a teammate still has to approve too.
Commands

Comment any of these on the PR:

  • /jeffery review · start a review
  • /jeffery next-round · start the next round now
  • /jeffery pause · /jeffery resume · stop or restart reviews on this PR
  • /jeffery status · where things stand
  • /jeffery help · all commands

@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread src/channel.ts Resolved
…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>
@Loschcode

Copy link
Copy Markdown
Contributor Author

/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>

@getstream-metis getstream-metis Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 2 · 🆕 2 new · ✅ 1 fixed · nothing blocking

➡️ Round 3 started on 8d51283.

Comment thread src/channel.ts Resolved
Comment thread src/channel.ts Resolved
getstream-metis[bot]
getstream-metis Bot previously approved these changes Dec 1, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎉 Approved · round 2

Nice work, Laurent! Nothing blocking is left on aeb7872f394b. Ship it 🚀

Left open, your call:

@getstream-metis
getstream-metis Bot dismissed their stale review December 1, 2026 12:30

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>
@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis getstream-metis Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 3 · 🆕 2 new · nothing blocking

➡️ Round 4 started on e93d40d.

? 0
: removed.filter(
(message) =>
message.type !== 'deleted' &&

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread test/unit/channel.test.js Resolved
getstream-metis[bot]
getstream-metis Bot previously approved these changes Dec 1, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎉 Approved · round 3

Nice work, Laurent! Nothing blocking is left on 8d51283621e7. Ship it 🚀

Left open, your call:

@Loschcode

Copy link
Copy Markdown
Contributor Author

Closing: #2045 was reverted on master (12def19), and we're leaving the rewind handling in the client to the JS SDK team.

@Loschcode Loschcode closed this Dec 1, 2026
@isekovanic isekovanic reopened this Dec 2, 2026
@getstream-metis
getstream-metis Bot dismissed their stale review December 2, 2026 14:10

Withdrawing approval until e93d40d is reviewed.

getstream-metis[bot]
getstream-metis Bot previously approved these changes Dec 2, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 4 · ✅ 3 fixed · 🎉 Approved

Nice work, Laurent! Nothing blocking is left on e93d40dc9e8e. Ship it 🚀

Left open, your call:

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.
getstream-metis[bot]
getstream-metis Bot previously approved these changes Dec 2, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎉 Approved · round 5

Nice work, Laurent! Nothing blocking is left on 917712b55a63. Ship it 🚀

Left open, your call:

…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.
@getstream-metis
getstream-metis Bot dismissed their stale review December 2, 2026 21:43

Withdrawing approval until ba18501 is reviewed.

getstream-metis[bot]
getstream-metis Bot previously approved these changes Dec 2, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 6 · ✅ 4 fixed · 🎉 Approved

Nice work, Laurent! Nothing blocking is left on ba1850193b13. Ship it 🚀

Left open, your call:

…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.
@getstream-metis
getstream-metis Bot dismissed their stale review December 2, 2026 22:44

Withdrawing approval until af2f167 is reviewed.

@isekovanic isekovanic changed the title fix(channel): address review follow-ups for channel.rewound (AI-3204) fix(channel): remove rewound messages from channels, threads and the offline DB (AI-3204) Dec 2, 2026
getstream-metis[bot]
getstream-metis Bot previously approved these changes Dec 2, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 7 · ✅ 5 fixed · 🎉 Approved

Nice work, Laurent! Nothing blocking is left on af2f16706b4d. Ship it 🚀

Left open, your call:

@MartinCupela MartinCupela 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.

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.

Comment thread src/channel.ts
case 'notification.channel_rewound': {
if (event.channel) channel.data = { ...channel.data, ...event.channel };
if (!event.rewind) break;
const range = {

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.

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.

Comment thread src/channel.ts Resolved
Comment thread src/channel.ts Resolved
Comment thread src/thread.ts Resolved
Comment thread src/thread_manager.ts Resolved

@oliverlaz oliverlaz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread src/channel.ts
if (event.channel) channel.data = { ...channel.data, ...event.channel };
if (!event.rewind) break;
const range = {
after: event.channel?.last_message_at,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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. Then after is later than every hidden message, rewind() removes nothing, and removedFromHead is 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.
  • inclusive rewinds: the server hides from the target onwards, but last_message_at becomes 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.handleChannelRewound uses 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.

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.

same here: #2052 (comment)

Comment thread src/channel.ts
if (!event.rewind) break;
const range = {
after: event.channel?.last_message_at,
upTo: event.rewind.created_at,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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's created_at, whichever is later, plus 1µs.
  • created_at is the database clock at insert, and nothing orders the two. The send clamp can also give the newest message a created_at in 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.

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.

same as here #2052 (comment)

let queries = [
...(await this.deleteMessagesForChannelInRange({
cid,
after: channel.last_message_at,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

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, 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 ?

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.

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

Comment thread src/channel.ts Resolved
Comment thread src/channel.ts
const { removed, removedFromHead, serverMessageLeft } =
this.messagePaginator.rewind(range);
this.pinnedMessagesPaginator.rewind(range);
if (removed.length) this._lowerUnreadCountForDeletedMessages(removed);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 removedFromHead triggers a reload. Even then, ChannelManager re-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`.
@isekovanic

Copy link
Copy Markdown
Contributor

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).
Range in /sync: the user goes offline, an AI agent (let's say that tihs is the usecase) regenerates an answer (rewind + new reply for instance) and the app reconnects. The replayed zchannel.rewound carries the channel as it is now, so last_message_at is already the new reply which is later than rewind.created_at. Our range comes out empty, nothing is deleted and the old answer comes back from the offline DB on every cold start.

Concretely, it would help if we had:

  • hidden_from/hidden_to on the rewind (ChannelRewindResponse). My Claude mentioned that they're already on the rewind row server side, just not sent. But if they aren't and it's not easy to expose them we can think of something else. Right now we guess the range from channel.last_message_at and rewind.created_at and that guess is wrong for inclusive rewinds, for a message sent during the rewind and in /sync (where the channel is the current one, so the range comes out empty like we mentioned a few sentences up)
  • /sync replays only the newest rewind per channel. If two rewinds with separate ranges happen while the client is away, the earlier range never reaches the offline DB. Could it replay every rewind since last_sync_at (or the merged hidden ranges) ?
  • The member's unread count for that channel on notification.channel_rewound. unread_count there appears to be the user's total, so we can't set the channel's count exactly. Could we return the channel unread count specifically in addition to this ?

@getstream-metis
getstream-metis Bot dismissed their stale review December 3, 2026 14:59

Withdrawing approval until d932509 is reviewed.

@getstream-metis getstream-metis Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

getstream-metis[bot]
getstream-metis Bot previously approved these changes Dec 3, 2026

@getstream-metis getstream-metis Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎉 Approved · round 8

Nice work, Laurent! Nothing blocking is left on d9325093d079. Ship it 🚀

Left open, your call:

@xicoalmeida

Copy link
Copy Markdown

@isekovanic from the backend side, all three make sense. Shapes I'd propose (@Loschcode wdyt):

  • Ranges: you're right that the bounds can't be derived from the event. Instead of replaying every rewind in /sync, we put the channel's merged hidden ranges on channel.rewound, live and in /sync: [{from, to, from_inclusive}], at most 100 per channel. /sync already loads them.
  • Unread: the websocket already has the member's count for that channel when it stamps the totals, read after the rewind took the hidden messages out. We can add it to notification.channel_rewound as unread_messages, as on notification.mark_unread. Apps on grouped unread won't get it.
  • Precision: bounds are microseconds. A message sent right after a rewind can land at hidden_to + 1µs, which a JS Date can't tell from a hidden one.

@isekovanic

Copy link
Copy Markdown
Contributor

Just to add on here @xicoalmeida :

  • This is I believe even better than what I was asking for ! Something I'd like to confirm though:
    • The ranges must be on notification.channel_rewound as well, not only channel.rewound. Nonwatchers rely on that event sort of.
    • to is always going to be inclusive right ?
  • THis is exactly what we need
  • We don't compare Dates. v10 types every server date as a unix-nanosecond number (response_dates_as_number). JSON.parse rounds those to the nearest representable double, about 256 ns apart at today's timestamps.

@getstream-metis
getstream-metis Bot dismissed their stale review December 3, 2026 17:23

Withdrawing approval until be137a1 is reviewed.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 9 · ✅ 5 fixed · 💬 1 dropped · 🎉 Approved

Nice work, Laurent! Nothing blocking is left on be137a1e033e. Ship it 🚀

Left open, your call:

@xicoalmeida

Copy link
Copy Markdown

@isekovanic yes to both: the ranges go on notification.channel_rewound too, and to is always inclusive (from only when from_inclusive is true). Good to know on the dates. Backend ticket: AI-4750.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants