Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(tui): keep one row when a message is re-sent with the same id
  • Loading branch information
myagizmaktav authored and kvnloo committed Sep 27, 2026
commit 558cc61c17d910bd65f78e402d0aea523e1fce2f
17 changes: 16 additions & 1 deletion packages/tui/src/context/sync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,22 @@ export const {
setStore("message", event.properties.info.sessionID, [event.properties.info])
break
}
const result = search(messages, messageKey(event.properties.info), messageKey)
const duplicate = messages.findIndex((message) => message.id === event.properties.info.id)
if (duplicate !== -1 && messages[duplicate].time.created === event.properties.info.time.created) {
setStore("message", event.properties.info.sessionID, duplicate, reconcile(event.properties.info))
break
}
if (duplicate !== -1) {
setStore(
"message",
event.properties.info.sessionID,
produce((draft) => {
draft.splice(duplicate, 1)
}),
)
}
const current = store.message[event.properties.info.sessionID] ?? []
const result = search(current, messageKey(event.properties.info), messageKey)
if (result.found) {
setStore("message", event.properties.info.sessionID, result.index, reconcile(event.properties.info))
break
Expand Down
26 changes: 26 additions & 0 deletions packages/tui/test/cli/cmd/tui/sync-live-hydration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,32 @@ test("live messages merged during hydration retain the 100 message window", asyn
}
})

test("a message re-sent with the same id keeps one row at the new creation time", async () => {
await using tmp = await tmpdir()
await Bun.write(`${tmp.path}/kv.json`, "{}")
const { app, emit, sync } = await mount(undefined, tmp.path)

try {
const first = { ...assistant, id: "msg_x", time: { created: 10, completed: 11 } }
const other = { ...assistant, id: "msg_y", time: { created: 30, completed: 31 } }
const resent = { ...assistant, id: "msg_x", time: { created: 50, completed: 51 } }
emit(global({ id: "evt_x1", type: "message.updated", properties: { sessionID, info: first } }))
emit(global({ id: "evt_y", type: "message.updated", properties: { sessionID, info: other } }))
emit(global({ id: "evt_x2", type: "message.updated", properties: { sessionID, info: resent } }))
await wait(() => {
const rows = sync.data.message[sessionID]
return rows?.length === 2 && rows.some((message) => message.id === "msg_x" && message.time.created === 50)
})

const rows = sync.data.message[sessionID]
expect(rows.map((message) => message.id)).toEqual(["msg_y", "msg_x"])
expect(rows.filter((message) => message.id === "msg_x")).toHaveLength(1)
expect(rows.find((message) => message.id === "msg_x")?.time.created).toBe(50)
} finally {
app.renderer.destroy()
}
})

test("a message removed during hydration does not regain stale parts", async () => {
await using tmp = await tmpdir()
await Bun.write(`${tmp.path}/kv.json`, "{}")
Expand Down
Loading