Skip to content

The agent that grows with you

fix(desktop): count the head entries the group-chat mirror drops

teknium1d99226015dcc
diff --git a/apps/desktop/src/plugins/hermes-bots/group-chat.test.ts b/apps/desktop/src/plugins/hermes-bots/group-chat.test.ts
index 4bf41b58ec2..8d5ae2269cb 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-chat.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-chat.test.ts
@@ -538,6 +538,30 @@ describe('gateway mirror', () => {
expect(snapshot.rooms['name:Unicode'].log.at(-1)?.thread).toBe('thread-15')
})
+ // #114341: the mirror is the only on-disk copy of a room. A head trim —
+ // by message count or by the byte budget — must say how many earlier
+ // entries it dropped, or a reader concludes the user never said it.
+ it('counts the head entries the mirror does not carry', async () => {
+ const { chat } = await loadRoom()
+ const entry = (index: number, text: string) => ({ at: index, from: { kind: 'user', name: 'You' }, text })
+
+ const snapshot = chat.groupChatSyncSnapshot({
+ Fits: { log: Array.from({ length: 3 }, (_, index) => entry(index, `short ${index}`)) },
+ ByCount: { log: Array.from({ length: 40 }, (_, index) => entry(index, `m${index}`)) },
+ ByBytes: { log: Array.from({ length: 16 }, (_, index) => entry(index, `${index} ${'🧠'.repeat(1200)}`)) }
+ } as unknown as Record<string, GroupChat>)
+
+ const byCount = snapshot.rooms['name:ByCount']
+ const byBytes = snapshot.rooms['name:ByBytes']
+
+ expect(snapshot.rooms['name:Fits'].omitted).toBeUndefined()
+ expect(byCount.log).toHaveLength(16)
+ expect(byCount.omitted).toBe(24)
+ expect(byBytes.log.length).toBeLessThan(16)
+ expect(byBytes.omitted).toBe(16 - byBytes.log.length)
+ expect(chat.groupChatGatewayJsonSize(snapshot)).toBeLessThanOrEqual(48000)
+ })
+
it('omits empty runtime rooms', async () => {
const { chat } = await loadRoom()
diff --git a/apps/desktop/src/plugins/hermes-bots/group-chat.ts b/apps/desktop/src/plugins/hermes-bots/group-chat.ts
index 06053a130f1..4458e667f69 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-chat.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-chat.ts
@@ -63,6 +63,9 @@ interface GroupChatSyncRoom {
log: GroupMessage[]
members?: GroupMember[]
name?: string
+ /** At least this many earlier room entries exist that the projection does
+ * not carry (head-trimmed to the message/byte budget). */
+ omitted?: number
revision?: number
roomId?: string
}
@@ -111,6 +114,17 @@ export function compactGroupChatSyncText(text: string, limit = GROUP_CHAT_SYNC_T
}
}
+/** #114341: the ui_meta mirror is the only on-disk copy of a room, so a
+ * head-trimmed log must say how many earlier entries it does not carry —
+ * a bare slice reads as "the user never said it". */
+function noteGroupChatSyncOmitted(room: GroupChatSyncRoom, total: number) {
+ const omitted = total - room.log.length
+
+ if (omitted > 0) {
+ room.omitted = omitted
+ }
+}
+
/** Conservative byte count for the gateway's ensure_ascii JSON encoding.
* Python also inserts separator spaces, so reserve one extra byte per JS
* structural separator on top of escaped Unicode code-point widths. */
@@ -315,9 +329,11 @@ export function groupChatSyncSnapshot(
const key = groupChatRoomKey(name, room)
rooms[key] = compact
+ noteGroupChatSyncOmitted(compact, room.log.length)
while (compact.log.length > 1 && groupChatGatewayJsonSize(envelope) > GROUP_CHAT_SYNC_MAX_BYTES) {
compact.log.shift()
+ noteGroupChatSyncOmitted(compact, room.log.length)
}
if (compact.image && groupChatGatewayJsonSize(envelope) > GROUP_CHAT_SYNC_MAX_BYTES) {
@@ -447,6 +463,8 @@ export function mergeGroupChatSyncSnapshots(
}
const remoteRevision = Math.max(0, Number(remoteRoom?.revision || 0))
+ // Either writer's head trim is a lower bound on what the union still lacks.
+ const omitted = Math.max(Number(remoteRoom?.omitted || 0), Number(localRoom?.omitted || 0))
const localRevision = changed.has(key)
? Math.max(0, Number(writeRevision || 0))
@@ -502,6 +520,11 @@ export function mergeGroupChatSyncSnapshots(
}),
members,
revision: Math.max(remoteRevision, localRevision),
+ ...(omitted > 0
+ ? {
+ omitted
+ }
+ : {}),
...(typeof image === 'string' && image
? {
image
@@ -560,6 +583,7 @@ function groupChatSyncEnvelope(
for (const [key, room] of ranked) {
while ((room.log?.length || 0) > 1 && groupChatGatewayJsonSize(envelope) > GROUP_CHAT_SYNC_MAX_BYTES) {
room.log.shift()
+ room.omitted = (room.omitted || 0) + 1
}
if (room.image && groupChatGatewayJsonSize(envelope) > GROUP_CHAT_SYNC_MAX_BYTES) {