Skip to content

The agent that grows with you

fix(bot-mode): report a retained turn failure whose prompt never reached history

teknium14a2ea5b6935a
diff --git a/apps/desktop/src/plugins/hermes-bots/group-turns.test.ts b/apps/desktop/src/plugins/hermes-bots/group-turns.test.ts
index b8306552477..07c0278f546 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-turns.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-turns.test.ts
@@ -329,6 +329,43 @@ describe('session-gone classification', () => {
clock.mockRestore()
}
})
+
+ // A turn that dies BEFORE its prompt is committed (agent-init failure,
+ // no-agent refusal) leaves a retained `{ status: 'error' }` and a transcript
+ // that never grew — the failure must still surface instead of the poll
+ // sitting out its deadline and reading the silence as a timeout.
+ it('reports a retained failure whose prompt never reached history', async () => {
+ let now = 1_000_000
+ const clock = vi.spyOn(Date, 'now').mockImplementation(() => (now += 60_000))
+ const room = await loadRoom({ turn: () => [] })
+ const request = host.request as (method: string, params?: Record<string, unknown>) => Promise<unknown>
+ let submitted = false
+
+ host.request = async (method: string, params: Record<string, unknown> = {}) => {
+ const result = (await request(method, params)) as Record<string, unknown> & { messages?: { role: string }[] }
+
+ submitted = submitted || method === 'prompt.submit'
+
+ if (method === 'session.resume' && submitted) {
+ return {
+ ...result,
+ inflight: { error: 'agent initialization failed', status: 'error', streaming: false },
+ messages: (result.messages || []).filter(message => message.role !== 'user')
+ }
+ }
+
+ return result
+ }
+
+ try {
+ await expect(room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'hi', 't1', [])).rejects.toThrow(
+ 'agent initialization failed'
+ )
+ expect(room.chat.$groupChats.get().Room?.stranded?.helper).toBeUndefined()
+ } finally {
+ clock.mockRestore()
+ }
+ })
})
describe('per-turn socket lease', () => {
@@ -1101,6 +1138,36 @@ describe('stranded harvest', () => {
expect(room.chat.$groupChats.get().Quiet2.stranded?.builder).toBeUndefined()
})
+ // The late turn died before its prompt was committed: the transcript never
+ // grew past the marker, but the retained failure is still the answer — a
+ // 'failed' row keyed like every other activity row, not a silent consume.
+ it('reports a retained failure behind an unchanged transcript instead of consuming the marker silently', async () => {
+ const room = await loadRoom()
+ const activity = await import('./group-activity')
+ const { groupMemberKey } = await import('./group-membership')
+ const member: GroupMember = { connectionId: 'mini', name: 'builder', remoteSource: true }
+ const key = groupMemberKey(member)
+
+ room.chat.updateGroupChat('Dead', current => {
+ current.sessions = { [key]: 'sid-builder' }
+ current.stranded = { [key]: { before: 1, thread: 't1' } }
+
+ return current
+ })
+ seedSession(room, 'sid-builder', 'builder', 'Group: Dead', [['user', 'p1']])
+ const requestProfile = host.requestProfile as (...args: unknown[]) => Promise<Record<string, unknown>>
+
+ host.requestProfile = async (...args: unknown[]) => ({
+ ...(await requestProfile(...args)),
+ inflight: { error: 'agent initialization failed', status: 'error', streaming: false }
+ })
+
+ await room.turns.harvestStrandedGroupReply('Dead', member)
+
+ expect(room.chat.$groupChats.get().Dead.stranded?.[key]).toBeUndefined()
+ expect(activity.$groupActivity.get().Dead?.events.map(event => [event.kind, event.member])).toEqual([['failed', key]])
+ })
+
it('never re-submits into a member the harvest just confirmed is still running', async () => {
// research is confirmed busy on exactly its first two session.resume calls
// — the number of harvest-only touches the FIXED code makes across two
diff --git a/apps/desktop/src/plugins/hermes-bots/group-turns.ts b/apps/desktop/src/plugins/hermes-bots/group-turns.ts
index 8903efee0f7..44213fce4d5 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-turns.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-turns.ts
@@ -846,6 +846,10 @@ interface GroupTurnPollContext {
liveRuntime: string
runtimeIds: Set<string>
before: number
+ /** The retained failed turn (`session.resume.inflight`) already on the
+ * session BEFORE this turn's submit, serialized; a retained error that
+ * still matches it is an older turn's tombstone, not this turn's death. */
+ leftover: null | string
binding: { isLive(): boolean }
}
@@ -908,9 +912,15 @@ async function pollGroupMemberTurn(context: GroupTurnPollContext): Promise<null
// hold the turn open: the member isn't stalling, it's waiting on us.
const awaitingUser = syncGroupClarify(context.group, member, thread, state)
const done = !busy && !awaitingUser
+ // The gateway's retained error for THIS turn. A turn that dies before its
+ // prompt is committed (agent-init failure, no-agent refusal) never grows
+ // the transcript, so the tombstone — not the message count — is the only
+ // evidence; a tombstone identical to the pre-submit one is an older turn's.
+ const failure = retainedGroupTurnError(state)
+ const died = failure !== null && (messages.length > before || JSON.stringify(state?.inflight) !== context.leftover)
- if (messages.length > before && done) {
- const replyText = pickGroupTurnReply(messages, before)
+ if ((messages.length > before || died) && done) {
+ const replyText = messages.length > before ? pickGroupTurnReply(messages, before) : null
if (replyText !== null) {
recordGroupActivity(context.group, {
@@ -922,11 +932,9 @@ async function pollGroupMemberTurn(context: GroupTurnPollContext): Promise<null
return replyText
}
- // Our prompt landed and the turn died on it: surface the gateway's
- // retained error through the failed-turn path (activity row + roster
- // badge) instead of reading the silence as a pass.
- const failure = retainedGroupTurnError(state)
-
+ // The turn died on our prompt: surface the gateway's retained error
+ // through the failed-turn path (activity row + roster badge) instead of
+ // reading the silence as a pass or sitting out the deadline.
if (failure !== null) {
throw new Error(failure)
}
@@ -982,8 +990,10 @@ async function prepareGroupTurnBaseline(
runtime: string,
stored: GroupMemberSessionHandle['stored']
) {
- // Baseline: how many messages exist before our submit.
+ // Baseline: how many messages exist before our submit, and any failed
+ // turn the gateway still retains from before it.
let before = 0
+ let leftover: null | string = null
// Every runtime id this turn has seen for the member's session. Terminal
// frames are keyed by runtime id, and a resume can hand back a fresh one.
const runtimeIds = new Set<string>([runtime])
@@ -995,6 +1005,7 @@ async function prepareGroupTurnBaseline(
})) as GroupSessionSnapshot
before = Array.isArray(pre?.messages) ? pre.messages.length : pre?.message_count || 0
+ leftover = retainedGroupTurnError(pre) === null ? null : JSON.stringify(pre.inflight)
if (pre?.session_id) {
runtimeIds.add(pre.session_id)
@@ -1003,7 +1014,7 @@ async function prepareGroupTurnBaseline(
/* lazy session — zero messages */
}
- return { before, runtimeIds }
+ return { before, leftover, runtimeIds }
}
async function runGroupChatMemberTurnLeased(
@@ -1033,7 +1044,7 @@ async function runGroupChatMemberTurnLeased(
thread
})
- const { before, runtimeIds } = await prepareGroupTurnBaseline(member, runtime, stored)
+ const { before, leftover, runtimeIds } = await prepareGroupTurnBaseline(member, runtime, stored)
const { failed, fileRefs } = await stageGroupTurnAttachments(member, runtime, images)
@@ -1066,6 +1077,7 @@ async function runGroupChatMemberTurnLeased(
liveRuntime,
runtimeIds,
before,
+ leftover,
binding
})
} finally {
@@ -1133,12 +1145,9 @@ export async function harvestStrandedGroupReply(group: string, member: GroupMemb
return r
})
const messages = Array.isArray(state?.messages) ? state.messages : []
-
- if (messages.length <= strandedBefore) {
- return
- }
-
- const reply = pickGroupTurnReply(messages, strandedBefore)
+ // A transcript that never grew is not proof of nothing: a turn that dies
+ // before its prompt is committed leaves only the retained error behind.
+ const reply = messages.length > strandedBefore ? pickGroupTurnReply(messages, strandedBefore) : null
if (reply === null) {
// The late turn died instead of answering: say so where the user looks
@@ -1148,7 +1157,7 @@ export async function harvestStrandedGroupReply(group: string, member: GroupMemb
if (failure !== null) {
recordGroupActivity(group, {
kind: 'failed',
- member: member.name,
+ member: memberKey,
thread: strandedThread
})
noteBotAttention(memberKey, failure)