Skip to content

The agent that grows with you

fix(desktop): stamp group log entries strictly after the tail

Teknium88f317437d34
diff --git a/apps/desktop/src/plugins/hermes-bots/group-activity.test.ts b/apps/desktop/src/plugins/hermes-bots/group-activity.test.ts
index 4c2ddaafb3b..98aeaec680d 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-activity.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-activity.test.ts
@@ -1,4 +1,4 @@
-import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type * as data from './data'
import type { GroupActivityEntry } from './group-activity'
@@ -70,6 +70,10 @@ beforeEach(() => {
runTimersInline()
})
+afterEach(() => {
+ vi.restoreAllMocks()
+})
+
describe('turn arc', () => {
it('a settled turn records the full truthful arc: queued, working, replies and passes, settled', async () => {
const room = await loadRoom({
@@ -218,6 +222,19 @@ describe('turn arc', () => {
describe('epoch scoping', () => {
it('queues follow-ups without cancelling the active turn or losing its reply delta', async () => {
+ // A frozen clock with ever-SMALLER entry ids: every row lands in the same
+ // millisecond and the gateway mirror merge (sort by `at`, then by id)
+ // would order each new row before the last. A busy CI runner reached
+ // this by chance (same-ms appends, random UUIDs) and the reordered log
+ // fed the member its follow-up twice.
+ let uuid = 0xffff
+
+ vi.spyOn(Date, 'now').mockReturnValue(1_700_000_000_000)
+ vi.spyOn(globalThis.crypto, 'randomUUID').mockImplementation(
+ () =>
+ `${(uuid--).toString(16).padStart(8, '0')}-0000-4000-8000-000000000000` as `${string}-${string}-${string}-${string}-${string}`
+ )
+
let release!: (reply: string) => void
const first = new Promise<string>(resolve => {
diff --git a/apps/desktop/src/plugins/hermes-bots/group-chat.ts b/apps/desktop/src/plugins/hermes-bots/group-chat.ts
index 5f231b2ca06..13fbfc35d8d 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-chat.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-chat.ts
@@ -1732,9 +1732,17 @@ export function appendGroupChatEntry(
thread?: null | string,
images?: Attachment[]
): GroupMessage {
+ const priorLog = ($groupChats.get()[group] || {}).log || []
+ const lastEntry = priorLog[priorLog.length - 1]
+
const entry: GroupMessage = {
id: groupChatEntryId(),
- at: Date.now(),
+ // Strictly after the tail: the gateway mirror merge re-sorts the log by
+ // `at` (id breaks ties), and watermarks are log INDICES. Two rows in one
+ // millisecond (or a clock step back) would come back shuffled — a user
+ // follow-up landing under a member's watermark is lost, or a reply moved
+ // past it is fed back as news and re-drives the member.
+ at: Math.max(Date.now(), Number(lastEntry?.at || 0) + 1),
from,
// Stored bodies share the prompt's per-line cap (see trimGroupChatLog);
// cutting here too keeps the duplicate-echo guard comparing like with like.
@@ -1752,9 +1760,6 @@ export function appendGroupChatEntry(
// loop both committing the same member reply) lands back-to-back and
// byte-identical. Drop the echo instead of flooding the room. User
// entries and non-adjacent repeats are never touched.
- const priorLog = ($groupChats.get()[group] || {}).log || []
- const lastEntry = priorLog[priorLog.length - 1]
-
if (isDuplicateGroupAppend(lastEntry, from, entry.text, entry.thread)) {
return lastEntry
}