Skip to content

The agent that grows with you

fix(desktop): review follow-ups for the room-race fixes

kshitij2863e8fb5da4
diff --git a/apps/desktop/src/plugins/hermes-bots/plugin.js b/apps/desktop/src/plugins/hermes-bots/plugin.js
index d36f2e69924..15d87857729 100644
--- a/apps/desktop/src/plugins/hermes-bots/plugin.js
+++ b/apps/desktop/src/plugins/hermes-bots/plugin.js
@@ -7065,9 +7065,19 @@ async function harvestStrandedGroupReply(group, member) {
* 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
+ * full delta and committing both is exactly the double-delivery bug.
+ *
+ * The re-drive premise is only true for a send in the SAME thread (delta
+ * filters are thread-scoped): a cross-thread epoch bump must NOT discard
+ * finished work no fresh loop will regenerate. Callers pass whether a newer
+ * USER entry landed in this thread since dispatch; the default (true)
+ * preserves the conservative drop when the caller can't tell. */
+function shouldCommitMemberTurn(epochAtDispatch, currentEpoch, newerUserEntryInThread = true) {
+ if (epochAtDispatch === currentEpoch) {
+ return true
+ }
+
+ return !newerUserEntryInThread
}
/** #93127 insurance: byte-identical member echo detection. TRUE only when
@@ -7122,14 +7132,15 @@ function classifyGroupHoldDirective(text, mentionedKeys, everyone) {
const resume = /\b(resume|continue|go|proceed)\b/i.test(value)
if (stop) {
- return { hold: mentioned, release: [], releaseAll: false }
+ // "@all stop" holds every member — symmetric with "@all resume".
+ return { hold: mentioned, holdAll: Boolean(everyone), release: [], releaseAll: false }
}
if (resume) {
- return { hold: [], release: mentioned, releaseAll: Boolean(everyone) }
+ return { hold: [], holdAll: false, release: mentioned, releaseAll: Boolean(everyone) }
}
- return { hold: [], release: mentioned, releaseAll: false }
+ return { hold: [], holdAll: false, release: mentioned, releaseAll: false }
}
/** #93129: next holds map after one user message. Holds are keyed by
@@ -7137,7 +7148,7 @@ function classifyGroupHoldDirective(text, mentionedKeys, everyone) {
* mints a NEW thread, so a thread-scoped hold would never block the next
* send's turns and the stop would not stick. Returns the same object when
* nothing changed. */
-function applyGroupHoldDirective(holds, mentions, text, stamp) {
+function applyGroupHoldDirective(holds, mentions, text, stamp, allMemberKeys = []) {
const prior = holds && typeof holds === 'object' ? holds : {}
const action = classifyGroupHoldDirective(text, mentions?.mentioned || [], Boolean(mentions?.everyone))
@@ -7145,9 +7156,12 @@ function applyGroupHoldDirective(holds, mentions, text, stamp) {
return Object.keys(prior).length ? {} : prior
}
+ // "@all stop": expand to every member key the caller knows about.
+ const toHold = action.holdAll ? [...allMemberKeys] : action.hold
+
let next = prior
- for (const key of action.hold) {
+ for (const key of toHold) {
if (next === prior) {
next = { ...prior }
}
@@ -7311,10 +7325,17 @@ async function runGroupChatRounds(group, members, thread) {
// 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)) {
+ // BEFORE the watermark advance and BEFORE the append. Only a newer
+ // USER entry in THIS thread makes the re-drive premise true: a
+ // cross-thread send bumps the epoch too, but its loop filters this
+ // thread out and would never regenerate the finished reply.
+ const roomNow = $groupChats.get()[group] || { log: [] }
+ const epochNow = roomNow.epoch || 0
+ const newerUserEntryInThread = (roomNow.log || [])
+ .slice(room.log.length)
+ .some(e => e.from?.kind === 'user' && groupThreadOf(e) === thread)
+
+ if (!shouldCommitMemberTurn(startEpoch, epochNow, newerUserEntryInThread)) {
recordGroupActivity(group, { kind: 'cancelled', member: member.name, thread })
return
}
@@ -7444,7 +7465,8 @@ function sendToGroupChat(group, members, text, thread, images) {
room.holds,
parseGroupChatMentions(trimmed, members),
trimmed,
- { at: sent?.at, byMessageId: sent?.id, thread: target }
+ { at: sent?.at, byMessageId: sent?.id, thread: target },
+ members.map(member => groupMemberKey(member))
)
return room
})
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/group-member-hold.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/group-member-hold.test.mjs
index 8216632bebb..490152d8fed 100644
--- a/apps/desktop/src/plugins/hermes-bots/tests/group-member-hold.test.mjs
+++ b/apps/desktop/src/plugins/hermes-bots/tests/group-member-hold.test.mjs
@@ -85,6 +85,20 @@ test('@all resume releases every hold', () => {
assert.deepEqual(JSON.parse(JSON.stringify(next)), {})
})
+test('@all stop holds every member — symmetric with @all resume', () => {
+ const { applyGroupHoldDirective } = loadHelpers()
+ const next = applyGroupHoldDirective(
+ {},
+ { mentioned: [], everyone: true },
+ '@all stop',
+ { at: 5 },
+ ['impl', 'docs']
+ )
+ assert.ok(next.impl)
+ assert.ok(next.docs)
+ assert.equal(next.impl.at, 5)
+})
+
test('an unrelated room message leaves holds untouched (same object back)', () => {
const { applyGroupHoldDirective } = loadHelpers()
const held = { impl: { at: 1 } }
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
index fb10f9f977c..391da6b7b10 100644
--- 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
@@ -40,6 +40,8 @@ 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)
+ // explicit same-thread supersession
+ assert.equal(shouldCommitMemberTurn(3, 4, true), false)
})
test('a current turn commits — epoch unchanged since dispatch', () => {
@@ -48,6 +50,14 @@ test('a current turn commits — epoch unchanged since dispatch', () => {
assert.equal(shouldCommitMemberTurn(0, 0), true)
})
+test('a cross-thread epoch bump does NOT discard finished work (no fresh loop re-drives it)', () => {
+ const { shouldCommitMemberTurn } = loadHelpers()
+ // Epoch moved, but no newer user entry landed in THIS thread: the
+ // superseding send lives in another thread whose loop filters this one
+ // out — dropping the reply would lose completed work forever.
+ assert.equal(shouldCommitMemberTurn(3, 4, false), true)
+})
+
test('adjacent identical member reply is a duplicate append', () => {
const { isDuplicateGroupAppend } = loadHelpers()
const last = memberEntry('impl', 'Stopped. Standing by.')