Skip to content

Commit 272441a

Browse files
committed
fix(session): handle message-id wrap-around across time order (anomalyco#208)
1 parent 524a421 commit 272441a

24 files changed

Lines changed: 938 additions & 122 deletions

‎packages/app/src/context/directory-sync.ts‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { Message, Part } from "@opencode-ai/sdk/v2/client"
1313
import { SESSION_CACHE_LIMIT, dropSessionCaches, pickSessionCacheEvictions } from "./global-sync/session-cache"
1414
import { diffs as list, message as clean } from "@/utils/diffs"
1515
import { useServerSDK } from "./server-sdk"
16+
import { cmpMessage, keyOf } from "./global-sync/utils"
1617

1718
const SKIP_PARTS = new Set(["patch", "step-start", "step-finish"])
1819

@@ -40,10 +41,10 @@ const isNotFound = (error: unknown) =>
4041
error.cause !== null &&
4142
(error.cause as { status?: unknown }).status === 404
4243

43-
function merge<T extends { id: string }>(a: readonly T[], b: readonly T[]) {
44+
function merge<T extends { id: string; time?: { created: number } }>(a: readonly T[], b: readonly T[]) {
4445
const map = new Map(a.map((item) => [item.id, item] as const))
4546
for (const item of b) map.set(item.id, item)
46-
return [...map.values()].sort((x, y) => cmp(x.id, y.id))
47+
return [...map.values()].sort(cmpMessage)
4748
}
4849

4950
type OptimisticStore = {
@@ -101,9 +102,15 @@ export function mergeOptimisticPage(page: MessagePage, items: OptimisticItem[])
101102
const confirmed: string[] = []
102103

103104
for (const item of items) {
104-
const result = Binary.search(session, item.message.id, (message) => message.id)
105-
const found = result.found
106-
if (!found) session.splice(result.index, 0, item.message)
105+
// The same message (same id) may already exist with a different
106+
// `time.created` (optimistic local time vs. confirmed server time), so
107+
// locate by id to avoid duplicating it; only insert when truly absent.
108+
const existing = session.findIndex((m) => m.id === item.message.id)
109+
const found = existing >= 0
110+
if (!found) {
111+
const result = Binary.search(session, keyOf(item.message), keyOf)
112+
session.splice(result.index, 0, item.message)
113+
}
107114

108115
const current = part.get(item.message.id)
109116
if (found && hasParts(current, item.parts)) {
@@ -126,8 +133,12 @@ export function mergeOptimisticPage(page: MessagePage, items: OptimisticItem[])
126133
export function applyOptimisticAdd(draft: OptimisticStore, input: OptimisticAddInput) {
127134
const messages = draft.message[input.sessionID]
128135
if (messages) {
129-
const result = Binary.search(messages, input.message.id, (m) => m.id)
130-
messages.splice(result.index, 0, input.message)
136+
// Locate by id so an optimistic/confirmed pair never duplicates; insert by
137+
// the time-ordered composite key only when the message is truly absent.
138+
if (messages.findIndex((m) => m.id === input.message.id) < 0) {
139+
const result = Binary.search(messages, keyOf(input.message), keyOf)
140+
messages.splice(result.index, 0, input.message)
141+
}
131142
} else {
132143
draft.message[input.sessionID] = [input.message]
133144
}
@@ -137,16 +148,18 @@ export function applyOptimisticAdd(draft: OptimisticStore, input: OptimisticAddI
137148
export function applyOptimisticRemove(draft: OptimisticStore, input: OptimisticRemoveInput) {
138149
const messages = draft.message[input.sessionID]
139150
if (messages) {
140-
const result = Binary.search(messages, input.messageID, (m) => m.id)
141-
if (result.found) messages.splice(result.index, 1)
151+
// message.removed only carries the id (no time), so locate linearly.
152+
const index = messages.findIndex((m) => m.id === input.messageID)
153+
if (index >= 0) messages.splice(index, 1)
142154
}
143155
delete draft.part[input.messageID]
144156
}
145157

146158
function setOptimisticAdd(setStore: (...args: unknown[]) => void, input: OptimisticAddInput) {
147159
setStore("message", input.sessionID, (messages: Message[] | undefined) => {
148160
if (!messages) return [input.message]
149-
const result = Binary.search(messages, input.message.id, (m) => m.id)
161+
if (messages.findIndex((m) => m.id === input.message.id) >= 0) return messages
162+
const result = Binary.search(messages, keyOf(input.message), keyOf)
150163
const next = [...messages]
151164
next.splice(result.index, 0, input.message)
152165
return next
@@ -157,10 +170,11 @@ function setOptimisticAdd(setStore: (...args: unknown[]) => void, input: Optimis
157170
function setOptimisticRemove(setStore: (...args: unknown[]) => void, input: OptimisticRemoveInput) {
158171
setStore("message", input.sessionID, (messages: Message[] | undefined) => {
159172
if (!messages) return messages
160-
const result = Binary.search(messages, input.messageID, (m) => m.id)
161-
if (!result.found) return messages
173+
// message.removed only carries the id (no time), so locate linearly.
174+
const index = messages.findIndex((m) => m.id === input.messageID)
175+
if (index < 0) return messages
162176
const next = [...messages]
163-
next.splice(result.index, 1)
177+
next.splice(index, 1)
164178
return next
165179
})
166180
setStore("part", (part: Record<string, Part[] | undefined>) => {
@@ -299,7 +313,7 @@ export const createDirSyncContext = (directory: string, serverSync: ReturnType<t
299313
input.client.session.messages({ sessionID: input.sessionID, limit: input.limit, before: input.before }),
300314
)
301315
const items = (messages.data ?? []).filter((x) => !!x?.info?.id)
302-
const session = items.map((x) => clean(x.info)).sort((a, b) => cmp(a.id, b.id))
316+
const session = items.map((x) => clean(x.info)).sort(cmpMessage)
303317
const part = items.map((message) => ({ id: message.info.id, part: sortParts(message.parts) }))
304318
const cursor = messages.response.headers.get("x-next-cursor") ?? undefined
305319
return {

‎packages/app/src/context/global-sync/event-reducer.test.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,108 @@ describe("applyDirectoryEvent", () => {
357357
expect(store.part.msg_2).toBeUndefined()
358358
})
359359

360+
// B-01: message.updated/removed must locate messages in the time-ordered
361+
// array by the (time.created, id) composite key. A post-wrap id (`msg_00...`)
362+
// is lexically smaller than pre-wrap ids (`msg_fa...`) even though it is
363+
// newer, so id-based binary search would re-insert it at the array head and
364+
// duplicate it, and message.removed would fail to find it.
365+
test("message.updated upserts a post-wrap message at the array end across wrap-around", () => {
366+
const sessionID = "ses_1"
367+
const preWrap = userMessage("msg_fa2c3af72001", sessionID)
368+
preWrap.time = { created: 1784448447887 }
369+
const postWrap = userMessage("msg_002ceb729001", sessionID)
370+
postWrap.time = { created: 1786753496981 }
371+
const [store, setStore] = createStore(baseState({ message: { [sessionID]: [preWrap] } }))
372+
373+
applyDirectoryEvent({
374+
event: { type: "message.updated", properties: { info: postWrap } },
375+
store,
376+
setStore,
377+
push() {},
378+
directory: "/tmp",
379+
loadLsp() {},
380+
})
381+
382+
expect(store.message[sessionID]?.map((x) => x.id)).toEqual(["msg_fa2c3af72001", "msg_002ceb729001"])
383+
})
384+
385+
test("repeated message.updated for a post-wrap message does not duplicate it", () => {
386+
const sessionID = "ses_1"
387+
const preWrap = userMessage("msg_fa2c3af72001", sessionID)
388+
preWrap.time = { created: 1784448447887 }
389+
const postWrap = userMessage("msg_002ceb729001", sessionID)
390+
postWrap.time = { created: 1786753496981 }
391+
const [store, setStore] = createStore(baseState({ message: { [sessionID]: [preWrap, postWrap] } }))
392+
393+
for (let i = 0; i < 3; i++) {
394+
applyDirectoryEvent({
395+
event: { type: "message.updated", properties: { info: postWrap } },
396+
store,
397+
setStore,
398+
push() {},
399+
directory: "/tmp",
400+
loadLsp() {},
401+
})
402+
}
403+
404+
expect(store.message[sessionID]?.map((x) => x.id)).toEqual(["msg_fa2c3af72001", "msg_002ceb729001"])
405+
})
406+
407+
test("message.updated reconciles an existing message whose time.created changed (optimistic vs confirmed)", () => {
408+
// Regression: an optimistic user message is inserted with a local
409+
// `Date.now()` timestamp. The confirmed `message.updated` event carries the
410+
// server's `time.created`, which differs by tens of ms. Locating the message
411+
// by the (time.created, id) composite key then fails to find the optimistic
412+
// copy and inserts a duplicate. The same message (same id) must be matched
413+
// by id and reconciled in place, regardless of the timestamp drift.
414+
const sessionID = "ses_1"
415+
const optimistic = userMessage("msg_002ceb729001", sessionID)
416+
optimistic.time = { created: 1786753496981 } // local Date.now() at send time
417+
const confirmed = userMessage("msg_002ceb729001", sessionID)
418+
confirmed.time = { created: 1786753496940 } // server time.created (41ms earlier)
419+
const [store, setStore] = createStore(baseState({ message: { [sessionID]: [optimistic] } }))
420+
421+
applyDirectoryEvent({
422+
event: { type: "message.updated", properties: { info: confirmed } },
423+
store,
424+
setStore,
425+
push() {},
426+
directory: "/tmp",
427+
loadLsp() {},
428+
})
429+
430+
const messages = store.message[sessionID] ?? []
431+
expect(messages).toHaveLength(1)
432+
expect(messages[0]?.id).toBe("msg_002ceb729001")
433+
expect(messages[0]?.time.created).toBe(1786753496940)
434+
})
435+
436+
test("message.removed removes a post-wrap message located by id", () => {
437+
const sessionID = "ses_1"
438+
const preWrap = userMessage("msg_fa2c3af72001", sessionID)
439+
preWrap.time = { created: 1784448447887 }
440+
const postWrap = userMessage("msg_002ceb729001", sessionID)
441+
postWrap.time = { created: 1786753496981 }
442+
const [store, setStore] = createStore(
443+
baseState({
444+
message: { [sessionID]: [preWrap, postWrap] },
445+
part: { msg_002ceb729001: [textPart("prt_1", sessionID, "msg_002ceb729001")] },
446+
}),
447+
)
448+
449+
applyDirectoryEvent({
450+
event: { type: "message.removed", properties: { sessionID, messageID: "msg_002ceb729001" } },
451+
store,
452+
setStore,
453+
push() {},
454+
directory: "/tmp",
455+
loadLsp() {},
456+
})
457+
458+
expect(store.message[sessionID]?.map((x) => x.id)).toEqual(["msg_fa2c3af72001"])
459+
expect(store.part.msg_002ceb729001).toBeUndefined()
460+
})
461+
360462
test("upserts and prunes message parts", () => {
361463
const sessionID = "ses_1"
362464
const messageID = "msg_1"

‎packages/app/src/context/global-sync/event-reducer.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { State, VcsCache } from "./types"
1515
import { trimSessions } from "./session-trim"
1616
import { dropSessionCaches } from "./session-cache"
1717
import { diffs as list, message as clean } from "@/utils/diffs"
18+
import { keyOf } from "./utils"
1819

1920
const SKIP_PARTS = new Set(["patch", "step-start", "step-finish"])
2021

@@ -189,11 +190,17 @@ export function applyDirectoryEvent(input: {
189190
input.setStore("message", info.sessionID, [info])
190191
break
191192
}
192-
const result = Binary.search(messages, info.id, (m) => m.id)
193-
if (result.found) {
194-
input.setStore("message", info.sessionID, result.index, reconcile(info))
193+
// The array is time-ordered, but the same message (same id) may already
194+
// be present with a different `time.created` (e.g. an optimistic copy
195+
// stamped with local `Date.now()` vs. the confirmed server's
196+
// `time.created`). Locate by id first so an optimistic/confirmed pair
197+
// reconciles in place instead of duplicating; only insert when absent.
198+
const existing = messages.findIndex((m) => m.id === info.id)
199+
if (existing >= 0) {
200+
input.setStore("message", info.sessionID, existing, reconcile(info))
195201
break
196202
}
203+
const result = Binary.search(messages, keyOf(info), keyOf)
197204
input.setStore(
198205
"message",
199206
info.sessionID,
@@ -209,8 +216,9 @@ export function applyDirectoryEvent(input: {
209216
produce((draft) => {
210217
const messages = draft.message[props.sessionID]
211218
if (messages) {
212-
const result = Binary.search(messages, props.messageID, (m) => m.id)
213-
if (result.found) messages.splice(result.index, 1)
219+
// message.removed only carries the id (no time), so locate linearly.
220+
const index = messages.findIndex((m) => m.id === props.messageID)
221+
if (index >= 0) messages.splice(index, 1)
214222
}
215223
const parts = draft.part[props.messageID]
216224
if (parts) {

‎packages/app/src/context/global-sync/utils.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ export { pathKey as directoryKey, type PathKey as DirectoryKey } from "@/utils/p
44

55
export const cmp = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0)
66

7+
type TimeOrdered = { id: string; time?: { created: number } }
8+
9+
// Compare messages by recency. MessageID is monotonic only within a 2^36 ms
10+
// window; once it wraps the lexical id order diverges from time order.
11+
// `time.created` is a required absolute ms timestamp, so compare it first and
12+
// fall back to id only when timestamps are equal (tie-breaker).
13+
export const cmpMessage = (a: TimeOrdered, b: TimeOrdered) => {
14+
if (a.time?.created && b.time?.created && a.time.created !== b.time.created) {
15+
return a.time.created - b.time.created
16+
}
17+
return cmp(a.id, b.id)
18+
}
19+
20+
// Composite ordering key for a message array maintained in time order:
21+
// fixed-width hex of `time.created` (so string order == numeric order) plus id
22+
// as tie-breaker. Used for Binary.search/insert on the time-ordered array.
23+
export const keyOf = (m: TimeOrdered) => {
24+
const created = typeof m.time?.created === "number" ? m.time.created : 0
25+
return `${created.toString(16).padStart(14, "0")}:${m.id}`
26+
}
27+
728
function isAgent(input: unknown): input is Agent {
829
if (!input || typeof input !== "object") return false
930
const item = input as { name?: unknown; mode?: unknown }

‎packages/app/src/context/sync-optimistic.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,52 @@ describe("sync optimistic reducers", () => {
120120
{ id: "prt_2", type: "text", text: "prt_2" },
121121
])
122122
})
123+
124+
// B-02: a post-wrap message id (`msg_00...`) is lexically smaller than
125+
// pre-wrap ids (`msg_fa...`) even though it is newer. Optimistic paths must
126+
// order by time.created (id tie-break) so the new message lands at the end,
127+
// not at the array head.
128+
test("applyOptimisticAdd inserts a post-wrap message at the array end across wrap-around", () => {
129+
const sessionID = "ses_1"
130+
const preWrap: Message = { ...userMessage("msg_fa2c3af72001", sessionID), time: { created: 1784448447887 } }
131+
const postWrap: Message = { ...userMessage("msg_002ceb729001", sessionID), time: { created: 1786753496981 } }
132+
const draft = {
133+
message: { [sessionID]: [preWrap] },
134+
part: {} as Record<string, Part[] | undefined>,
135+
}
136+
137+
applyOptimisticAdd(draft, { sessionID, message: postWrap, parts: [] })
138+
139+
expect(draft.message[sessionID]?.map((x) => x.id)).toEqual(["msg_fa2c3af72001", "msg_002ceb729001"])
140+
})
141+
142+
test("applyOptimisticRemove removes a post-wrap message located by id", () => {
143+
const sessionID = "ses_1"
144+
const preWrap: Message = { ...userMessage("msg_fa2c3af72001", sessionID), time: { created: 1784448447887 } }
145+
const postWrap: Message = { ...userMessage("msg_002ceb729001", sessionID), time: { created: 1786753496981 } }
146+
const draft = {
147+
message: { [sessionID]: [preWrap, postWrap] },
148+
part: {
149+
msg_002ceb729001: [textPart("prt_1", sessionID, "msg_002ceb729001")],
150+
} as Record<string, Part[] | undefined>,
151+
}
152+
153+
applyOptimisticRemove(draft, { sessionID, messageID: "msg_002ceb729001" })
154+
155+
expect(draft.message[sessionID]?.map((x) => x.id)).toEqual(["msg_fa2c3af72001"])
156+
expect(draft.part.msg_002ceb729001).toBeUndefined()
157+
})
158+
159+
test("mergeOptimisticPage keeps a post-wrap pending message at the array end", () => {
160+
const sessionID = "ses_1"
161+
const preWrap: Message = { ...userMessage("msg_fa2c3af72001", sessionID), time: { created: 1784448447887 } }
162+
const postWrap: Message = { ...userMessage("msg_002ceb729001", sessionID), time: { created: 1786753496981 } }
163+
164+
const page = mergeOptimisticPage(
165+
{ session: [preWrap], part: [{ id: preWrap.id, part: [] }], complete: true },
166+
[{ message: postWrap, parts: [] }],
167+
)
168+
169+
expect(page.session.map((x) => x.id)).toEqual(["msg_fa2c3af72001", "msg_002ceb729001"])
170+
})
123171
})

‎packages/app/src/context/sync.tsx‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Binary } from "@opencode-ai/core/util/binary"
22
import { useServerSync } from "./server-sync"
33
import { useSDK } from "./sdk"
44
import type { Message, Part } from "@opencode-ai/sdk/v2/client"
5+
import { keyOf } from "./global-sync/utils"
56

67
const SKIP_PARTS = new Set(["patch", "step-start", "step-finish"])
78

@@ -66,9 +67,15 @@ export function mergeOptimisticPage(page: MessagePage, items: OptimisticItem[])
6667
const confirmed: string[] = []
6768

6869
for (const item of items) {
69-
const result = Binary.search(session, item.message.id, (message) => message.id)
70-
const found = result.found
71-
if (!found) session.splice(result.index, 0, item.message)
70+
// The same message (same id) may already exist with a different
71+
// `time.created` (optimistic local time vs. confirmed server time), so
72+
// locate by id to avoid duplicating it; only insert when truly absent.
73+
const existing = session.findIndex((m) => m.id === item.message.id)
74+
const found = existing >= 0
75+
if (!found) {
76+
const result = Binary.search(session, keyOf(item.message), keyOf)
77+
session.splice(result.index, 0, item.message)
78+
}
7279

7380
const current = part.get(item.message.id)
7481
if (found && hasParts(current, item.parts)) {
@@ -91,8 +98,12 @@ export function mergeOptimisticPage(page: MessagePage, items: OptimisticItem[])
9198
export function applyOptimisticAdd(draft: OptimisticStore, input: OptimisticAddInput) {
9299
const messages = draft.message[input.sessionID]
93100
if (messages) {
94-
const result = Binary.search(messages, input.message.id, (m) => m.id)
95-
messages.splice(result.index, 0, input.message)
101+
// Locate by id so an optimistic/confirmed pair never duplicates; insert by
102+
// the time-ordered composite key only when the message is truly absent.
103+
if (messages.findIndex((m) => m.id === input.message.id) < 0) {
104+
const result = Binary.search(messages, keyOf(input.message), keyOf)
105+
messages.splice(result.index, 0, input.message)
106+
}
96107
} else {
97108
draft.message[input.sessionID] = [input.message]
98109
}
@@ -102,8 +113,9 @@ export function applyOptimisticAdd(draft: OptimisticStore, input: OptimisticAddI
102113
export function applyOptimisticRemove(draft: OptimisticStore, input: OptimisticRemoveInput) {
103114
const messages = draft.message[input.sessionID]
104115
if (messages) {
105-
const result = Binary.search(messages, input.messageID, (m) => m.id)
106-
if (result.found) messages.splice(result.index, 1)
116+
// message.removed only carries the id (no time), so locate linearly.
117+
const index = messages.findIndex((m) => m.id === input.messageID)
118+
if (index >= 0) messages.splice(index, 1)
107119
}
108120
delete draft.part[input.messageID]
109121
}

0 commit comments

Comments
 (0)