Skip to content

The agent that grows with you

fix(bot-mode): fail closed on transient group-session resume failures

pierrenode525597c9c3ce
diff --git a/apps/desktop/src/plugins/hermes-bots/plugin.js b/apps/desktop/src/plugins/hermes-bots/plugin.js
index da964d0606f..5404eb67a03 100644
--- a/apps/desktop/src/plugins/hermes-bots/plugin.js
+++ b/apps/desktop/src/plugins/hermes-bots/plugin.js
@@ -6649,6 +6649,17 @@ async function ensureGroupChatSession(group, member) {
const known = room.sessions && room.sessions[key]
// Try resuming what we know (stored sid first, then title lookup).
+ //
+ // FAIL CLOSED on a transient lookup failure — mirrors the sibling fix in
+ // findExistingCanonicalChat (87b645f52c). session.resume signals "this
+ // target genuinely doesn't exist" with JSON-RPC code 4007; every other
+ // failure (network blip, the backend still warming up after a restart,
+ // an oversized-resume refusal) means the real session might still be
+ // there and must not be read as "no session, mint a new one" — that
+ // forks the member's real history, and the fork silently overwrites
+ // room.sessions[key] so the old session becomes unreachable from the
+ // room. Only a genuine 4007 on BOTH targets means there truly is nothing
+ // to resume yet, so the loop falls through to session.create below.
for (const target of [known, title]) {
if (!target || target === true) {
continue
@@ -6674,8 +6685,12 @@ async function ensureGroupChatSession(group, member) {
return { runtime: res.session_id, stored }
}
- } catch {
- /* fall through to create */
+ } catch (error) {
+ if (error?.code !== 4007) {
+ const detail = error instanceof Error && error.message ? ` (${error.message})` : ''
+ throw new Error(`Could not check ${member?.name || 'member'}'s group session${detail} — not starting a new one`)
+ }
+ /* genuinely doesn't exist (4007) — try the next target / fall through to create */
}
}
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/group-activity.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/group-activity.test.mjs
index 8cb8d30df51..3027e4f8909 100644
--- a/apps/desktop/src/plugins/hermes-bots/tests/group-activity.test.mjs
+++ b/apps/desktop/src/plugins/hermes-bots/tests/group-activity.test.mjs
@@ -55,7 +55,13 @@ function load(turnScript = () => '(pass)') {
if (method === 'session.resume') {
const session = resolveSession(params.profile, params.session_id)
if (!session) {
- throw new Error(`session not found: ${params.session_id}`)
+ // Shaped like the real gateway's JsonRpcGatewayError (`.code`,
+ // apps/shared/src/json-rpc-gateway.ts) so ensureGroupChatSession's
+ // fail-closed `.code !== 4007` check sees the same "genuinely
+ // not found" signal production does.
+ const err = new Error(`session not found: ${params.session_id}`)
+ err.code = 4007
+ throw err
}
return {
session_id: session.runtime,
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/group-chat-attachments.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/group-chat-attachments.test.mjs
index 26535004283..93d25162390 100644
--- a/apps/desktop/src/plugins/hermes-bots/tests/group-chat-attachments.test.mjs
+++ b/apps/desktop/src/plugins/hermes-bots/tests/group-chat-attachments.test.mjs
@@ -50,7 +50,13 @@ function load(turnScript) {
if (method === 'session.resume') {
const session = resolveSession(params.profile, params.session_id)
if (!session) {
- throw new Error(`session not found: ${params.session_id}`)
+ // Shaped like the real gateway's JsonRpcGatewayError (`.code`,
+ // apps/shared/src/json-rpc-gateway.ts) so ensureGroupChatSession's
+ // fail-closed `.code !== 4007` check sees the same "genuinely
+ // not found" signal production does.
+ const err = new Error(`session not found: ${params.session_id}`)
+ err.code = 4007
+ throw err
}
return {
session_id: session.runtime,
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/group-chat.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/group-chat.test.mjs
index fed433b70ff..96b3bc61425 100644
--- a/apps/desktop/src/plugins/hermes-bots/tests/group-chat.test.mjs
+++ b/apps/desktop/src/plugins/hermes-bots/tests/group-chat.test.mjs
@@ -130,7 +130,15 @@ function load(turnScript, { busyUntilResumeCall, clarifyUntilResumeCall, approva
if (method === 'session.resume') {
const session = resolveSession(params.profile, params.session_id)
if (!session) {
- throw new Error(`session not found: ${params.session_id}`)
+ // Shaped like the real gateway's JsonRpcGatewayError (a `.code`
+ // property, per apps/shared/src/json-rpc-gateway.ts) so
+ // ensureGroupChatSession's fail-closed `.code !== 4007` check
+ // exercises the same branch production sees. 4007 mirrors
+ // session.resume's own "session not found" JSON-RPC error code
+ // (tui_gateway/methods_session.py).
+ const err = new Error(`session not found: ${params.session_id}`)
+ err.code = 4007
+ throw err
}
const profile = params.profile
const seen = (resumeCallCounts.get(profile) || 0) + 1
@@ -424,6 +432,71 @@ test('recreating a same-name group after disband mints fresh member sessions', a
assert.equal(gc.sessions.get(second.stored).title, 'Group: r-two')
})
+test('a transient resume failure fails closed instead of forking the member session', async () => {
+ // Mirrors findExistingCanonicalChat's fix (87b645f52c): a resume that
+ // fails for any reason OTHER than "genuinely doesn't exist" (JSON-RPC
+ // code 4007) must surface, not be read as "no session, mint a new one" —
+ // that would fork the member's real history and silently overwrite
+ // room.sessions[key] so the old session becomes unreachable.
+ const gc = load(() => '(pass)')
+ const member = { name: 'research', title: '' }
+
+ gc.updateGroupChat('Alpha', r => {
+ r.roomId = 'r-one'
+ return r
+ })
+ const first = await gc.ensureGroupChatSession('Alpha', member)
+ const roomBefore = gc.$groupChats.get().Alpha
+
+ // Simulate a transient failure (backend still warming up after a restart,
+ // a network blip on a cross-connection lookup, ...) on the NEXT resume of
+ // the member's own stored session — a real gateway error, not "not found".
+ const originalRequest = gc.host.request
+ gc.host.request = async (method, params) => {
+ if (method === 'session.resume' && params.session_id === first.stored) {
+ const err = new Error('gateway temporarily unavailable')
+ err.code = 5000
+ throw err
+ }
+ return originalRequest(method, params)
+ }
+
+ await assert.rejects(
+ () => gc.ensureGroupChatSession('Alpha', member),
+ /Could not check .*group session/
+ )
+
+ gc.host.request = originalRequest
+
+ // Nothing was forked: the room's session pointer is untouched, and the
+ // original session is still the only one on record for this member.
+ assert.deepEqual(gc.$groupChats.get().Alpha.sessions, roomBefore.sessions)
+ assert.equal(gc.sessions.size, 1)
+})
+
+test('a genuinely nonexistent stored session (4007) still falls through to the title lookup, then create', async () => {
+ const gc = load(() => '(pass)')
+ const member = { name: 'research', title: '' }
+
+ gc.updateGroupChat('Alpha', r => {
+ r.roomId = 'r-one'
+ return r
+ })
+
+ // A stored sid pointing at a session the gateway no longer has (e.g. an
+ // out-of-band deletion) is a genuine 4007 on the first target — the loop
+ // must still try the title lookup and, finding nothing there either,
+ // create a fresh session rather than throwing.
+ gc.updateGroupChat('Alpha', r => {
+ r.sessions = { research: 'sid-gone' }
+ return r
+ })
+
+ const result = await gc.ensureGroupChatSession('Alpha', member)
+ assert.ok(result.stored, 'a fresh session must be minted when nothing resumable exists')
+ assert.notEqual(result.stored, 'sid-gone')
+})
+
test('group session titles are pinned to the roomId, with a legacy fallback to the display name', async () => {
const gc = load(() => '(pass)')