Skip to content

The agent that grows with you

fix(desktop): drop superseded group-chat turns and dedupe adjacent identical replies (#93127)

kshitijk4poorae6baf333f36
diff --git a/apps/desktop/src/plugins/hermes-bots/plugin.js b/apps/desktop/src/plugins/hermes-bots/plugin.js
index 8a7009093e6..6887f33fa4f 100644
--- a/apps/desktop/src/plugins/hermes-bots/plugin.js
+++ b/apps/desktop/src/plugins/hermes-bots/plugin.js
@@ -6554,6 +6554,17 @@ function appendGroupChatEntry(group, from, text, thread, images) {
entry.images = images
}
+ // #93127 insurance: a residual double-append path (stale loop + fresh
+ // 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
+ }
+
updateGroupChat(group, room => {
room.log.push(entry)
return room
@@ -7042,6 +7053,50 @@ async function harvestStrandedGroupReply(group, member) {
}
}
+// --- room-turn decision helpers (#93127) — pure, vm-sliced by tests ---
+
+/** #93127: whether a finished member turn may still commit (append its reply
+ * and advance its watermark). A turn dispatched under an older epoch was
+ * superseded mid-flight by a newer user send — its late result must be
+ * dropped, because the new send's own loop re-drives this member with the
+ * full delta and committing both is exactly the double-delivery bug. */
+function shouldCommitMemberTurn(epochAtDispatch, currentEpoch) {
+ return epochAtDispatch === currentEpoch
+}
+
+/** #93127 insurance: byte-identical member echo detection. TRUE only when
+ * the immediately-preceding log entry has the same author (kind + name +
+ * source), same thread, and identical text, within a short recency window —
+ * a residual double-append fires back-to-back; two legitimately identical
+ * replies hours apart (or with anything in between) are never dropped. */
+const GROUP_DUPLICATE_APPEND_WINDOW_MS = 10 * 60 * 1000
+
+function isDuplicateGroupAppend(lastEntry, from, text, thread, now = Date.now()) {
+ if (!lastEntry || !from || from.kind !== 'member' || lastEntry.from?.kind !== 'member') {
+ return false
+ }
+
+ if (String(lastEntry.from?.name || '') !== String(from.name || '')) {
+ return false
+ }
+
+ if (String(lastEntry.from?.source || '') !== String(from.source || '')) {
+ return false
+ }
+
+ if (String(lastEntry.thread || 'legacy') !== String(thread || 'legacy')) {
+ return false
+ }
+
+ if (now - (lastEntry.at || 0) > GROUP_DUPLICATE_APPEND_WINDOW_MS) {
+ return false
+ }
+
+ return String(lastEntry.text || '') === String(text || '').trim()
+}
+
+// --- end room-turn decision helpers ---
+
/** Drive one bounded round-robin turn for ONE THREAD. Serial — one member at
* a time. A newer user send bumps the room epoch; this loop notices at the
* next member boundary, bails, and the newest send's own loop takes over.
@@ -7143,6 +7198,18 @@ async function runGroupChatRounds(group, members, thread) {
reply = null // a failed turn is a pass, never a room error
}
+ // #93127: the turn may have finished AFTER a newer user send bumped
+ // the room epoch. That newer send's loop re-drives this member with
+ // the full delta, so committing this stale result (watermark advance
+ // + append) would double-deliver the same reply. Drop it here —
+ // BEFORE the watermark advance and BEFORE the append.
+ const epochNow = ($groupChats.get()[group] || {}).epoch || 0
+
+ if (!shouldCommitMemberTurn(startEpoch, epochNow)) {
+ recordGroupActivity(group, { kind: 'cancelled', member: member.name, thread })
+ return
+ }
+
// The member has now seen everything up to the pre-reply log length.
updateGroupChat(group, r => {
r.watermarks[markKey] = r.log.length
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/group-turn-races.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/group-turn-races.test.mjs
new file mode 100644
index 00000000000..fb10f9f977c
--- /dev/null
+++ b/apps/desktop/src/plugins/hermes-bots/tests/group-turn-races.test.mjs
@@ -0,0 +1,95 @@
+import assert from 'node:assert/strict'
+import { readFileSync } from 'node:fs'
+import test from 'node:test'
+import vm from 'node:vm'
+
+// #93127: duplicate room delivery. Two raceable paths existed:
+// 1. a member turn mid-flight when the room epoch bumps still commits its
+// reply + watermark when it returns (the stale loop only noticed
+// supersession at the NEXT member boundary), and
+// 2. the stale loop and the fresh loop can both append the same reply.
+// The fix re-checks the epoch after the turn returns (shouldCommitMemberTurn)
+// and drops adjacent byte-identical member echoes (isDuplicateGroupAppend).
+
+const pluginSource = readFileSync(new URL('../plugin.js', import.meta.url), 'utf8')
+
+function loadHelpers() {
+ const start = pluginSource.indexOf('// --- room-turn decision helpers (#93127)')
+ const end = pluginSource.indexOf('// --- end room-turn decision helpers ---', start)
+ assert.notEqual(start, -1, 'plugin carries the room-turn helper block')
+ assert.notEqual(end, -1, 'room-turn helper block has a stable end marker')
+ const context = {}
+ vm.runInNewContext(
+ `${pluginSource.slice(start, end)}
+globalThis.shouldCommitMemberTurn = shouldCommitMemberTurn
+globalThis.isDuplicateGroupAppend = isDuplicateGroupAppend`,
+ context
+ )
+ return context
+}
+
+const memberEntry = (name, text, thread = 't1', at = Date.now(), source) => ({
+ id: 'x',
+ at,
+ from: { kind: 'member', name, ...(source ? { source } : {}) },
+ text,
+ thread
+})
+
+test('a superseded turn is discarded — epoch moved on while the turn ran', () => {
+ const { shouldCommitMemberTurn } = loadHelpers()
+ assert.equal(shouldCommitMemberTurn(3, 4), false)
+ assert.equal(shouldCommitMemberTurn(3, 7), false)
+})
+
+test('a current turn commits — epoch unchanged since dispatch', () => {
+ const { shouldCommitMemberTurn } = loadHelpers()
+ assert.equal(shouldCommitMemberTurn(3, 3), true)
+ assert.equal(shouldCommitMemberTurn(0, 0), true)
+})
+
+test('adjacent identical member reply is a duplicate append', () => {
+ const { isDuplicateGroupAppend } = loadHelpers()
+ const last = memberEntry('impl', 'Stopped. Standing by.')
+ const dup = isDuplicateGroupAppend(last, { kind: 'member', name: 'impl' }, 'Stopped. Standing by.', 't1')
+ assert.equal(dup, true)
+})
+
+test('identical text from a DIFFERENT member is not a duplicate', () => {
+ const { isDuplicateGroupAppend } = loadHelpers()
+ const last = memberEntry('impl', 'Confirmed.')
+ assert.equal(isDuplicateGroupAppend(last, { kind: 'member', name: 'reviewer' }, 'Confirmed.', 't1'), false)
+})
+
+test('identical text from the same member on another SOURCE is not a duplicate', () => {
+ const { isDuplicateGroupAppend } = loadHelpers()
+ const last = memberEntry('impl', 'Confirmed.', 't1', Date.now(), 'laptop')
+ assert.equal(isDuplicateGroupAppend(last, { kind: 'member', name: 'impl' }, 'Confirmed.', 't1'), false)
+})
+
+test('same text after an intervening entry is not a duplicate — only the LAST entry is checked', () => {
+ const { isDuplicateGroupAppend } = loadHelpers()
+ // The helper only ever receives the immediately-preceding entry; an
+ // intervening entry means lastEntry is the interloper, not the echo.
+ const intervening = memberEntry('reviewer', 'ack')
+ assert.equal(isDuplicateGroupAppend(intervening, { kind: 'member', name: 'impl' }, 'Confirmed.', 't1'), false)
+})
+
+test('identical text in a DIFFERENT thread is not a duplicate', () => {
+ const { isDuplicateGroupAppend } = loadHelpers()
+ const last = memberEntry('impl', 'Confirmed.', 'thread-a')
+ assert.equal(isDuplicateGroupAppend(last, { kind: 'member', name: 'impl' }, 'Confirmed.', 'thread-b'), false)
+})
+
+test('identical text outside the recency window is not a duplicate', () => {
+ const { isDuplicateGroupAppend } = loadHelpers()
+ const hourAgo = Date.now() - 60 * 60 * 1000
+ const last = memberEntry('impl', 'Done.', 't1', hourAgo)
+ assert.equal(isDuplicateGroupAppend(last, { kind: 'member', name: 'impl' }, 'Done.', 't1'), false)
+})
+
+test('user entries are never deduped', () => {
+ const { isDuplicateGroupAppend } = loadHelpers()
+ const last = { id: 'x', at: Date.now(), from: { kind: 'user', name: 'You' }, text: 'stop', thread: 't1' }
+ assert.equal(isDuplicateGroupAppend(last, { kind: 'user', name: 'You' }, 'stop', 't1'), false)
+})