Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(community): reconcile own optimistic row by clientNonce on WS echo (reply-disappear fix step 2)

Gener91b8e7197d14
diff --git a/src/web/src/hooks/community/insert-message-into-cache.test.ts b/src/web/src/hooks/community/insert-message-into-cache.test.ts
index b11e6f20f..e08e7ad3d 100644
--- a/src/web/src/hooks/community/insert-message-into-cache.test.ts
+++ b/src/web/src/hooks/community/insert-message-into-cache.test.ts
@@ -61,3 +61,78 @@ describe("insertMessageIntoCache — attachment width/height", () => {
])
})
})
+
+// reply-disappear root-cause fix (step 2): a message.create echo of the
+// SENDER's own send carries the same clientNonce the optimistic row was stamped
+// with (WS step 1), so `insertMessageIntoCache` reconciles that row IN PLACE
+// (temp id → server id) instead of appending a duplicate. The duplicate was the
+// 2-row transient that, collapsed under an unlucky POST-vs-WS ordering, made a
+// just-sent reply vanish. These lock "one row, no double-insert" — the machine
+// oracle for the fix (Ingaborg's dt=0 ADD,ADD → single ADD).
+describe("insertMessageIntoCache — optimistic-row reconcile by clientNonce (reply-disappear step 2)", () => {
+ const optimisticRow = {
+ id: "temp_abc",
+ type: "chat" as const,
+ authorId: "author_1",
+ authorName: "Author",
+ content: "hello",
+ createdAt: "2026-01-01T00:00:00.000Z",
+ clientNonce: "nonce-xyz",
+ }
+ const cacheWithOptimistic = () => ({ pages: [{ messages: [{ ...optimisticRow }] }], pageParams: [null] })
+
+ function echo(overrides: Partial<CommunityMessageCreate["message"]> = {}): CommunityMessageCreate["message"] {
+ return {
+ id: "msg_server_1",
+ type: "chat",
+ authorId: "author_1",
+ authorName: "Author",
+ authorAvatar: "A",
+ content: "hello",
+ createdAt: "2026-01-01T00:00:00.000Z",
+ clientNonce: "nonce-xyz",
+ ...overrides,
+ } as CommunityMessageCreate["message"]
+ }
+
+ it("same-nonce echo renames the optimistic row in place (temp→server id) — ONE row, no duplicate", () => {
+ const result = insertMessageIntoCache(cacheWithOptimistic() as never, echo())
+ const rows = result?.pages[0].messages
+ expect(rows).toHaveLength(1)
+ expect(rows?.[0].id).toBe("msg_server_1")
+ expect(rows?.[0].clientNonce).toBe("nonce-xyz")
+ expect(rows?.[0].failed).toBe(false)
+ })
+
+ it("preserves the optimistic row's position (reconcile in place, not append)", () => {
+ const older = { id: "m_old", type: "chat" as const, authorId: "a", authorName: "X", content: "prev", createdAt: "2025-12-31T00:00:00.000Z" }
+ const cache = { pages: [{ messages: [older, { ...optimisticRow }] }], pageParams: [null] }
+ const result = insertMessageIntoCache(cache as never, echo())
+ const rows = result?.pages[0].messages
+ expect(rows).toHaveLength(2)
+ expect(rows?.map((m) => m.id)).toEqual(["m_old", "msg_server_1"])
+ })
+
+ it("an echo with a DIFFERENT nonce (someone else's message) is appended normally — no false reconcile", () => {
+ const result = insertMessageIntoCache(cacheWithOptimistic() as never, echo({ id: "msg_other", clientNonce: "other-nonce" }))
+ const rows = result?.pages[0].messages
+ expect(rows).toHaveLength(2)
+ expect(rows?.map((m) => m.id)).toEqual(["temp_abc", "msg_other"])
+ })
+
+ it("an echo with NO clientNonce (srv-fallback, never echoed anyway) is appended — no reconcile", () => {
+ const result = insertMessageIntoCache(cacheWithOptimistic() as never, echo({ id: "msg_nononce", clientNonce: undefined }))
+ const rows = result?.pages[0].messages
+ expect(rows).toHaveLength(2)
+ expect(rows?.map((m) => m.id)).toEqual(["temp_abc", "msg_nononce"])
+ })
+
+ it("the server-id dedup still wins first: a re-delivered echo (server id already present) is a no-op", () => {
+ // After a prior reconcile the row already carries the server id; a duplicate
+ // echo must not touch the cache (id check precedes the nonce reconcile).
+ const cache = { pages: [{ messages: [{ ...optimisticRow, id: "msg_server_1" }] }], pageParams: [null] }
+ const result = insertMessageIntoCache(cache as never, echo())
+ expect(result?.pages[0].messages).toHaveLength(1)
+ expect(result?.pages[0].messages[0].id).toBe("msg_server_1")
+ })
+})
diff --git a/src/web/src/hooks/community/use-community-ws.ts b/src/web/src/hooks/community/use-community-ws.ts
index 4ba490370..fc6eb8a7b 100644
--- a/src/web/src/hooks/community/use-community-ws.ts
+++ b/src/web/src/hooks/community/use-community-ws.ts
@@ -148,6 +148,33 @@ export function insertMessageIntoCache(
if (cache.pages.length === 0) return cache
const first = cache.pages[0]
if (first.messages.some((m) => m.id === msg.id)) return cache
+ // Reconcile the SENDER's own optimistic row instead of inserting a second row.
+ // A message.create echo of a send the viewer just made carries the same
+ // `clientNonce` the optimistic row was stamped with (WS step-1). Without this,
+ // the echo (server id) shares no key with the optimistic row (temp id) and
+ // gets appended as a DUPLICATE — the 2-row transient that, collapsed under an
+ // unlucky POST-vs-WS ordering, made a just-sent reply vanish (reply-disappear).
+ // Rename the optimistic row to the server id in place: exactly one row, so the
+ // collapse has nothing to double-delete. `reconcileServerId` (the POST path)
+ // then finds no temp id and no-ops — both orderings converge to one row.
+ // `srv:`-fallback nonces are never echoed (WS step-1), so this only ever
+ // matches the sender's own client-provided nonce; others' messages (no
+ // matching optimistic row) fall through to the normal append below.
+ const nonce = "clientNonce" in msg ? (msg as { clientNonce?: string }).clientNonce : undefined
+ if (nonce) {
+ for (let pi = 0; pi < cache.pages.length; pi++) {
+ const p = cache.pages[pi]
+ const mi = p.messages.findIndex((m) => m.clientNonce === nonce)
+ if (mi !== -1) {
+ const pages = cache.pages.slice()
+ const msgs = p.messages.slice()
+ // Rename in place (temp id → server id), clear `failed`, keep position.
+ msgs[mi] = { ...msgs[mi], id: msg.id, failed: false }
+ pages[pi] = { ...p, messages: msgs }
+ return { ...cache, pages }
+ }
+ }
+ }
const attachments: Attachment[] | undefined = msg.attachments?.map((a) => {
const isImage = a.contentType?.startsWith("image/")
return isImage