Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Restore stored history when it is ahead of the join snapshot (#334)

Nick Launchesb250c20c8f9e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d2f7327c..9b7edee4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,16 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
## Unreleased
+### Reopening a channel no longer hides the end of the last conversation
+
+Opening a channel joins the realtime gateway, and the snapshot the join returns can lag the durable
+store. When it did, the last exchange of a finished turn was missing from the transcript on every
+reload, with no unread marker or anything else to explain the gap, and the stored copy never got a
+chance to replace it because history was only restored into an empty channel.
+
+The stored thread now wins whenever it holds more than the channel does and holds everything the
+channel already shows. A message typed while history is still loading is not in the store yet, and
+a run still streaming has messages the store has not seen, so neither is rolled back.
### The transcript stays with the question when an answer starts arriving
Sending a message carried it up to the top of the view, correctly, and then the Bot's first token
diff --git a/app/src/components/channels/channel-chat.tsx b/app/src/components/channels/channel-chat.tsx
index 4be9ba33..2f5be26e 100644
--- a/app/src/components/channels/channel-chat.tsx
+++ b/app/src/components/channels/channel-chat.tsx
@@ -161,12 +161,26 @@ export function ChannelChat({
channel.threadId,
runtimeAgentId,
);
- // Never overwrite local messages that arrived while history was loading.
- if (
- current &&
- stored.messages.length > 0 &&
- agent.messages.length === 0
- ) {
+ /*
+ * The durable store wins when it is ahead of what the join delivered.
+ *
+ * The join replaces the agent's messages with the realtime gateway's snapshot of the thread,
+ * and that snapshot can lag the store: a turn that finished, was persisted and answered in
+ * full came back from the join without its last exchange, on every reload, with no
+ * unreadable count to explain the gap. Restoring only into an empty agent kept that stale
+ * snapshot for good.
+ *
+ * So the store is applied when it holds more than the agent does AND everything the agent
+ * holds is in the store. The second half is the guard this replaced: a message typed while
+ * history was loading is not in the store yet, so it is never overwritten, and a run still
+ * streaming has messages the store has not seen, so its snapshot is never rolled back.
+ */
+ const local = agent.messages;
+ const storedIds = new Set(stored.messages.map((m) => m.id));
+ const storeIsAhead =
+ stored.messages.length > local.length &&
+ local.every((m) => storedIds.has(m.id));
+ if (current && stored.messages.length > 0 && storeIsAhead) {
agent.setMessages(stored.messages);
}
/*