Skip to content

The agent that grows with you

fix(desktop): preserve explicit group stop cancellation

fangliquanflqd633255a0cd4
diff --git a/apps/desktop/src/plugins/hermes-bots/group-chat-view-members.test.ts b/apps/desktop/src/plugins/hermes-bots/group-chat-view-members.test.ts
index 17fef6fd091..052143f546e 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-chat-view-members.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-chat-view-members.test.ts
@@ -4,7 +4,9 @@ import type * as data from './data'
import type * as groupChat from './group-chat'
import type * as members from './group-chat-view-members'
import type * as groupMembership from './group-membership'
-import { createGroupGateway, runTimersInline, scriptedStorage } from './group-test-utils'
+import type * as groupRounds from './group-rounds'
+import { createGroupGateway, drain, runTimersInline, scriptedStorage } from './group-test-utils'
+import type { ScriptedGateway } from './group-test-utils'
import type { RosterRow } from './types'
// Both membership doors (room-side "Manage members", per-Bot "Manage groups")
@@ -23,8 +25,10 @@ vi.mock('@hermes/plugin-sdk', async () => {
interface Room {
chat: typeof groupChat
data: typeof data
+ gateway: ScriptedGateway
members: typeof members
membership: typeof groupMembership
+ rounds: typeof groupRounds
}
async function loadRoom(): Promise<Room> {
@@ -37,17 +41,18 @@ async function loadRoom(): Promise<Room> {
Object.assign(host, gateway.host)
- const [chat, d, m, membership, shared] = await Promise.all([
+ const [chat, d, m, membership, rounds, shared] = await Promise.all([
import('./group-chat'),
import('./data'),
import('./group-chat-view-members'),
import('./group-membership'),
+ import('./group-rounds'),
import('./shared')
])
shared.setPluginCtx(scriptedStorage(gateway.storage))
- return { chat, data: d, members: m, membership }
+ return { chat, data: d, gateway, members: m, membership, rounds }
}
const programmer: RosterRow = { name: 'programmer' }
@@ -125,6 +130,31 @@ describe('setGroupChatMembers', () => {
expect.objectContaining({ name: 'reviewer' })
]))
})
+
+ it('does not replay a removed member\'s held messages after re-adding it', async () => {
+ const room = await loadRoom()
+ seedCore(room, {
+ heldMessages: { reviewer: ['held-before-removal'] },
+ log: [
+ {
+ at: 1,
+ from: { kind: 'user', name: 'You' },
+ id: 'held-before-removal',
+ text: 'STALE_PRE_REMOVAL_TEXT',
+ thread: 'old-thread'
+ }
+ ]
+ })
+
+ await room.members.setGroupMembership(reviewer, 'Core', false)
+ await room.members.setGroupMembership(reviewer, 'Core', true)
+ room.rounds.sendToGroupChat('Core', [reviewer], '@reviewer fresh task')
+ await drain(() => Boolean(room.chat.$groupChats.get().Core.running))
+
+ expect(room.chat.$groupChats.get().Core.heldMessages?.reviewer).toBeUndefined()
+ expect(room.gateway.calls[0].prompt).not.toContain('STALE_PRE_REMOVAL_TEXT')
+ expect(room.gateway.calls[0].prompt).toContain('fresh task')
+ })
})
describe('setGroupMembership (per-Bot Manage groups)', () => {
diff --git a/apps/desktop/src/plugins/hermes-bots/group-chat-view-members.tsx b/apps/desktop/src/plugins/hermes-bots/group-chat-view-members.tsx
index 47aaf3be836..f822d49bc06 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-chat-view-members.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/group-chat-view-members.tsx
@@ -68,6 +68,7 @@ async function commitGroupChatRoster(group: string, previous: RosterRow[], seate
updateGroupChat(group, (room: GroupChatRoom) => ({
...room,
members: durableGroupChatMembers(seated),
+ heldMessages: without(room.heldMessages, key => key),
holds: without(room.holds, key => key),
stranded: without(room.stranded, key => key),
sessions: without(room.sessions, groupSessionMemberKey),
diff --git a/apps/desktop/src/plugins/hermes-bots/group-chat.ts b/apps/desktop/src/plugins/hermes-bots/group-chat.ts
index fec102a5d5b..b8be3b3548f 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-chat.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-chat.ts
@@ -1664,11 +1664,14 @@ export interface GroupHoldStamp extends GroupHold {
}
/** The room record as the coordination engine handles it: `GroupChat` plus
- * `turn`, the runtime-only descriptor of the member currently mid-turn. Like
- * `running`/`epoch` it never persists, so it has no place in the durable
- * shape. Holds carry the fuller live stamp. */
+ * runtime-only turn/cancellation state. Like `running`/`epoch` these fields
+ * never persist, so they have no place in the durable shape. Holds carry the
+ * fuller live stamp. */
export interface GroupChatRoom extends GroupChat {
holds?: Record<string, GroupHoldStamp>
+ /** Epoch minted by the latest explicit Stop action. A turn dispatched under
+ * an older epoch is cancelled even when sticky hold detection is disabled. */
+ stoppedEpoch?: number
turn?: GroupMember | null
}
diff --git a/apps/desktop/src/plugins/hermes-bots/group-round-members.ts b/apps/desktop/src/plugins/hermes-bots/group-round-members.ts
index 66d8a1a6a56..deb1c49344d 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-round-members.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-round-members.ts
@@ -236,7 +236,7 @@ export async function runGroupRoundMember(
)
if (
- (epochNow !== startEpoch && roomNow.holds?.[groupMemberKey(member)]) ||
+ (roomNow.stoppedEpoch || 0) > startEpoch ||
!shouldCommitMemberTurn(startEpoch, epochNow, newerUserEntryInThread)
) {
recordGroupActivity(context.group, {
diff --git a/apps/desktop/src/plugins/hermes-bots/group-rounds.test.ts b/apps/desktop/src/plugins/hermes-bots/group-rounds.test.ts
index 10b6f85694e..06f6a2415b5 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-rounds.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-rounds.test.ts
@@ -1420,6 +1420,27 @@ describe('stopGroupThread (#91868/#94569)', () => {
expect(room.chat.$groupChats.get().Room.running).toBe(false)
})
+ it('cancels an in-flight completion after Stop when sticky holds are disabled', async () => {
+ let finish!: (reply: string) => void
+ const pendingReply = new Promise<string>(resolve => {
+ finish = resolve
+ })
+ const room = await loadRoom({ turn: () => pendingReply })
+ const member = [{ name: 'helper', title: '' }]
+
+ room.chat.updateGroupChat('Room', state => ({ ...state, holdDetection: false }))
+ room.rounds.sendToGroupChat('Room', member, 'long task')
+ await drain(() => room.gateway.calls.length < 1)
+
+ await room.rounds.stopGroupThread('Room', null, member)
+ finish('must not be committed')
+ await drain(() => room.gateway.refcount() > 0)
+
+ const state = room.chat.$groupChats.get().Room
+ expect(state.holds).toEqual({})
+ expect(state.log.filter(entry => entry.from.kind === 'member')).toHaveLength(0)
+ })
+
it('keeps polling through an ordinary newer-send epoch bump so late work still lands', async () => {
let live: Room | null = null
diff --git a/apps/desktop/src/plugins/hermes-bots/group-rounds.ts b/apps/desktop/src/plugins/hermes-bots/group-rounds.ts
index 7050949ad82..0825e965cfc 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-rounds.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-rounds.ts
@@ -506,6 +506,7 @@ export async function stopGroupThread(group: string, thread: null | string, memb
updateGroupChat(group, (r: GroupChatRoom) => {
r.epoch = (r.epoch || 0) + 1
+ r.stoppedEpoch = r.epoch
r.running = false
r.turn = null
diff --git a/apps/desktop/src/plugins/hermes-bots/group-turns.ts b/apps/desktop/src/plugins/hermes-bots/group-turns.ts
index c377c19f249..229ef043847 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-turns.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-turns.ts
@@ -955,7 +955,6 @@ function clearGroupTurnMarker(group: string, member: GroupMember, turn: string)
async function pollGroupMemberTurn(context: GroupTurnPollContext): Promise<null | string> {
const { member, thread, dispatchEpoch, stored, liveRuntime, runtimeIds, before, binding } = context
- const memberKey = groupMemberKey(member)
const started = Date.now()
let deadline = started + GROUP_TURN_TIMEOUT_MS
// After the terminal frame fires, the gateway still has to flip
@@ -970,19 +969,18 @@ async function pollGroupMemberTurn(context: GroupTurnPollContext): Promise<null
quickRechecks = signalled ? GROUP_TURN_SETTLE_RECHECKS : Math.max(0, quickRechecks - 1)
- // #91868/#94569: an explicit stop (stopGroupThread) bumped the epoch AND
- // held this member — the member's session was interrupted, so nothing is
- // coming; abandon the poll instead of grinding until the deadline. Both
- // conditions on purpose: an ordinary newer send bumps the epoch WITHOUT
- // a hold, and that turn must keep polling so finished work can still be
- // delivered (the #93127 commit check decides its fate, not this loop).
+ // #91868/#94569: an explicit stop stamps the epoch it minted independently
+ // of sticky holds. Abandon any turn dispatched before that stamp instead
+ // of trusting the best-effort interrupt or grinding until the deadline.
+ // Ordinary newer sends bump only `epoch`, so their late work still reaches
+ // the #93127 commit check below.
if (!binding.isLive()) {
return null
}
const roomDuringPoll = $groupChats.get()[context.group] || {}
- if ((roomDuringPoll.epoch || 0) !== dispatchEpoch && (roomDuringPoll.holds || {})[memberKey]) {
+ if ((roomDuringPoll.stoppedEpoch || 0) > dispatchEpoch) {
clearGroupTurnMarker(context.group, member, context.turn)
return null