Skip to content

The agent that grows with you

fix(desktop): a later failed turn no longer swallows a stranded member's late reply

teknium19c31215ff595
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 30e7561ef0a..1335369d9c2 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-turns.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-turns.test.ts
@@ -417,6 +417,40 @@ describe('session-gone classification', () => {
}
})
+ // The previous turn failed with the same error: only `turn_started_at` tells
+ // this turn's retained failure from the leftover one.
+ it('reports a failure identical to the previous turn’s instead of posting pre-tool text', async () => {
+ let now = 1_000_000
+ const clock = vi.spyOn(Date, 'now').mockImplementation(() => (now += 60_000))
+
+ const room = await loadRoom({
+ turn: () => [
+ { content: 'Let me check the repo first.', role: 'assistant' },
+ { content: 'ok', role: 'tool' }
+ ]
+ })
+
+ const request = host.request as (method: string, params?: Record<string, unknown>) => Promise<unknown>
+ const inflight = { error: 'HTTP 401: invalid_api_key', status: 'error', streaming: false }
+ let submitted = false
+
+ host.request = async (method: string, params: Record<string, unknown> = {}) => {
+ const result = (await request(method, params)) as Record<string, unknown>
+
+ submitted = submitted || method === 'prompt.submit'
+
+ return method === 'session.resume' ? { ...result, inflight, turn_started_at: submitted ? 200 : 100 } : result
+ }
+
+ try {
+ await expect(room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'hi', 't1', [])).rejects.toThrow(
+ inflight.error
+ )
+ } finally {
+ 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
@@ -1397,6 +1431,46 @@ describe('stranded harvest', () => {
expect(activity.$groupActivity.get().Broke?.events.map(event => event.kind)).toEqual(['failed'])
})
+ // A later gateway turn in the same session failed: its retained error is not
+ // the stranded turn's, so the stranded turn's real late reply still posts.
+ it('posts the late reply when a later turn in the session failed', async () => {
+ const room = await loadRoom()
+ const activity = await import('./group-activity')
+
+ room.chat.updateGroupChat('Late', current => {
+ current.sessions = { research: 'sid-research' }
+ current.stranded = { research: 0 }
+
+ return current
+ })
+ room.gateway.sessions.set('sid-research', {
+ messages: [
+ { content: roomPrompt('Late'), role: 'user' },
+ { content: 'Here is the late answer.', role: 'assistant' },
+ { content: 'and the deploy?', role: 'user' }
+ ],
+ profile: 'research',
+ runtime: 'rt-research',
+ stored: 'sid-research',
+ title: 'Group: Late'
+ })
+
+ const request = host.request as (method: string, params?: Record<string, unknown>) => Promise<unknown>
+
+ host.request = async (method: string, params: Record<string, unknown> = {}) => {
+ const result = (await request(method, params)) as Record<string, unknown>
+
+ return method === 'session.resume'
+ ? { ...result, inflight: { error: 'HTTP 401: invalid_api_key', status: 'error', streaming: false } }
+ : result
+ }
+
+ await room.turns.harvestStrandedGroupReply('Late', { name: 'research', title: '' })
+
+ expect(log(room, 'Late').map(entry => entry.text)).toContain('Here is the late answer.')
+ expect(activity.$groupActivity.get().Late?.events.map(event => event.kind)).not.toContain('failed')
+ })
+
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 6dc142ad372..00af6bb52c7 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-turns.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-turns.ts
@@ -187,6 +187,8 @@ interface GroupSessionSnapshot {
running?: boolean
session_id?: string
session_key?: string
+ /** Start time of the live or retained turn; the `inflight` snapshot omits it. */
+ turn_started_at?: null | number
}
/** Group turns are explicit user work. A member may be cold or retired when
@@ -220,6 +222,26 @@ export function retainedGroupTurnError(state: GroupSessionSnapshot | null | unde
return null
}
+/** Identity of the retained failed turn, else null. The `inflight` snapshot
+ * carries no start time, so two identical consecutive failures differ only
+ * by `turn_started_at`. */
+function retainedGroupTurnKey(state: GroupSessionSnapshot | null | undefined): null | string {
+ return retainedGroupTurnError(state) === null ? null : JSON.stringify([state?.turn_started_at ?? null, state?.inflight])
+}
+
+/** Does a user row follow the stranded turn's own prompt? Then a later turn
+ * ran in the session, and a retained error belongs to that turn, not this one. */
+function laterTurnAfterStranded(messages: GroupTurnTranscriptMessage[], before: number): boolean {
+ const anchor = messages.findIndex(
+ (msg, i) =>
+ i >= before && msg?.role === 'user' && groupTranscriptRowText(msg).startsWith(GROUP_PROMPT_HEADER_PREFIX)
+ )
+
+ return messages.some(
+ (msg, i) => i > (anchor === -1 ? before - 1 : anchor) && msg?.role === 'user' && !syntheticGroupUserRow(msg)
+ )
+}
+
/** Is the member's session still doing work this turn should wait for?
* Reading a retained failure as busy kept a dead turn's deadline sliding to
* the hard cap and left its stranded marker harvestable forever
@@ -1067,10 +1089,10 @@ async function pollGroupMemberTurn(context: GroupTurnPollContext): Promise<null
// 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)
- // Turn start replaces the snapshot (with a fresh `started_at`), so a
- // retained error unlike the pre-submit one is THIS turn's: the member did
- // not finish, and text it wrote before a tool call is not its reply.
- const failedThisTurn = failure !== null && JSON.stringify(state?.inflight) !== context.leftover
+ // Turn start replaces the snapshot and `turn_started_at`, so a retained
+ // error unlike the pre-submit one is THIS turn's: the member did not
+ // finish, and text it wrote before a tool call is not its reply.
+ const failedThisTurn = failure !== null && retainedGroupTurnKey(state) !== context.leftover
const died = failedThisTurn || (failure !== null && messages.length > before)
if ((messages.length > before || died) && done) {
@@ -1153,7 +1175,7 @@ async function prepareGroupTurnBaseline(
snapshot = pre
before = Array.isArray(pre?.messages) ? pre.messages.length : pre?.message_count || 0
- leftover = retainedGroupTurnError(pre) === null ? null : JSON.stringify(pre.inflight)
+ leftover = retainedGroupTurnKey(pre)
if (pre?.session_id) {
runtimeIds.add(pre.session_id)
@@ -1350,9 +1372,10 @@ export async function harvestStrandedGroupReply(group: string, member: GroupMemb
const messages = Array.isArray(state?.messages) ? state.messages : []
// 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.
- // The retained error is the stranded turn's own (the next turn start would
- // have replaced it): text written before a failed tool step is no reply.
- const retained = retainedGroupTurnError(state)
+ // The retained error is the stranded turn's own unless a later turn ran
+ // after it; then it is that turn's error and the late reply still posts.
+ // Text written before a failed tool step is no reply.
+ const retained = laterTurnAfterStranded(messages, strandedBefore) ? null : retainedGroupTurnError(state)
const pick =
retained === null && messages.length > strandedBefore ? pickStrandedGroupTurnReply(messages, strandedBefore) : null