Skip to content

The agent that grows with you

60.2 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest'

import type * as groupChat from './group-chat'
import type * as groupRounds from './group-rounds'
import { createGroupGateway, drain, runTimersInline, scriptedStorage } from './group-test-utils'
import type { GatewayOptions, ScriptedGateway } from './group-test-utils'
import type * as groupTurns from './group-turns'
import type { Attachment, GroupChat, GroupMember } from './types'

// One member's turn: resolve its per-room session, stage attachments, submit,
// and poll until the gateway says the member is done. The session is the
// fragile part — it is addressed by a STORED id that outlives every runtime
// id, and the socket it rides can be reaped underneath a turn in flight.

const { host } = vi.hoisted(() => ({ host: {} as Record<string, unknown> }))

vi.mock('@hermes/plugin-sdk', async () => {
  const { pluginSdkMock } = await import('./group-test-utils')

  return pluginSdkMock(host)
})

// agent/turn_failure_copy.py::PARTIAL_FAILED_TURN_NOTICE
const PARTIAL_NOTICE =
  'This turn did not complete. Some actions may already have run; verify their effects before resending.'

interface Room {
  chat: typeof groupChat
  gateway: ScriptedGateway
  rounds: typeof groupRounds
  turns: typeof groupTurns
}

async function loadRoom(options: GatewayOptions = {}): Promise<Room> {
  vi.resetModules()
  const gateway = createGroupGateway(options)

  for (const key of Object.keys(host)) {
    delete host[key]
  }

  Object.assign(host, gateway.host)

  const [chat, rounds, turns, shared] = await Promise.all([
    import('./group-chat'),
    import('./group-rounds'),
    import('./group-turns'),
    import('./shared')
  ])

  shared.setPluginCtx(scriptedStorage(gateway.storage))

  return { chat, gateway, rounds, turns }
}

const LOCAL_MEMBER: GroupMember = { name: 'helper', title: '' }
const ROUTED_MEMBER: GroupMember = { connectionId: 'mini', name: 'helper', remoteSource: true }
const IMG: Attachment = { data: 'data:image/png;base64,iVBORw0KGgo=', kind: 'image', name: 'shot.png' }

const log = (room: Room, group: string) => room.chat.$groupChats.get()[group]?.log || []
// The room engine opens every turn prompt with this header (group-round-prompt.ts);
// a user row without it is an outside write and gets mirrored into the room log.
const roomPrompt = (group: string) => `[Group chat: "${group}"] You are @member, one participant in a group chat.`

beforeEach(() => {
  runTimersInline()
})

describe('session resolution', () => {
  it('pins session titles to the roomId, with a legacy fallback to the display name', async () => {
    const room = await loadRoom()

    // Rooms persisted before roomIds keep name-based titles so their existing
    // "Group: <name>" sessions keep resolving after an upgrade.
    room.chat.updateGroupChat('Legacy', current => current)
    const legacy = await room.turns.ensureGroupChatSession('Legacy', { name: 'research', title: '' }, 't1')

    expect(room.gateway.sessions.get(String(legacy.stored))?.title).toBe('Group: Legacy · t1')

    // New rooms pin the title to the immutable roomId, never the display name.
    room.chat.updateGroupChat('New', current => {
      current.roomId = 'r-abc'

      return current
    })
    const fresh = await room.turns.ensureGroupChatSession('New', { name: 'research', title: '' }, 't1')

    expect(room.gateway.sessions.get(String(fresh.stored))?.title).toBe('Group: r-abc · t1')
  })

  it('creates member sessions with the room_plumbing + follow_profile_config contracts', async () => {
    // The PR #97008 contracts: room member sessions always rebuild from the
    // member profile's CURRENT config on resume, never a stale stored
    // model/provider pin. Dropping either param silently regresses rooms to
    // the server's hidden + "Group: " title legacy fallback.
    const room = await loadRoom()

    room.chat.updateGroupChat('Contract', current => {
      current.roomId = 'r-contract'

      return current
    })
    const handle = await room.turns.ensureGroupChatSession('Contract', { name: 'research', title: '' }, 't1')

    expect(room.gateway.sessions.get(String(handle.stored))?.contracts).toEqual({
      follow_profile_config: true,
      room_plumbing: true
    })
  })

  it('mints fresh member sessions when a same-name group is recreated after disband', async () => {
    const room = await loadRoom()
    const member: GroupMember = { name: 'research', title: '' }

    room.chat.updateGroupChat('Alpha', current => {
      current.roomId = 'r-one'

      return current
    })
    const first = await room.turns.ensureGroupChatSession('Alpha', member, 't1')

    // Disband: the room record is gone; the member's gateway session survives.
    const rooms = { ...room.chat.$groupChats.get() }
    delete rooms.Alpha
    room.chat.$groupChats.set(rooms)

    // Recreate under the same display name with a freshly minted roomId.
    room.chat.updateGroupChat('Alpha', current => {
      current.roomId = 'r-two'

      return current
    })
    const second = await room.turns.ensureGroupChatSession('Alpha', member, 't1')

    expect(first.stored).not.toBe(second.stored)
    expect(room.gateway.sessions.get(String(first.stored))?.title).toBe('Group: r-one · t1')
    expect(room.gateway.sessions.get(String(second.stored))?.title).toBe('Group: r-two · t1')
  })

  it('fails closed on a transient resume failure 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 room = await loadRoom()
    const member: GroupMember = { name: 'research', title: '' }

    room.chat.updateGroupChat('Alpha', current => {
      current.roomId = 'r-one'

      return current
    })
    const first = await room.turns.ensureGroupChatSession('Alpha', member, 't1')
    const before = room.chat.$groupChats.get().Alpha.sessions

    // A real gateway error on the NEXT resume of the member's own stored
    // session — backend still warming up, a network blip, not "not found".
    const request = host.request as (method: string, params: Record<string, unknown>) => Promise<unknown>

    host.request = async (method: string, params: Record<string, unknown>) => {
      if (method === 'session.resume' && params.session_id === first.stored) {
        throw Object.assign(new Error('gateway temporarily unavailable'), { code: 5000 })
      }

      return request(method, params)
    }

    await expect(room.turns.ensureGroupChatSession('Alpha', member, 't1')).rejects.toThrow(
      /Could not check .*group session/
    )

    host.request = request

    // Nothing was forked: the room's session pointer is untouched, and the
    // original session is still the only one on record for this member.
    expect(room.chat.$groupChats.get().Alpha.sessions).toEqual(before)
    expect(room.gateway.sessions.size).toBe(1)
  })

  it('falls through a genuine 4007 to the title lookup, then creates', async () => {
    const room = await loadRoom()

    room.chat.updateGroupChat('Alpha', current => {
      current.roomId = 'r-one'
      // 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.
      current.sessions = { research: 'sid-gone' }

      return current
    })

    const result = await room.turns.ensureGroupChatSession('Alpha', { name: 'research', title: '' }, 't1')

    expect(result.stored).toBeTruthy()
    expect(result.stored).not.toBe('sid-gone')
  })

  it('gives each thread its own session, and reuses it within that thread (#90420)', async () => {
    // The defect: sessions were keyed by member alone, so thread B resumed
    // thread A's transcript and answered carrying A's context.
    const room = await loadRoom()
    const member: GroupMember = { name: 'research', title: '' }

    room.chat.updateGroupChat('Alpha', current => {
      current.roomId = 'r-one'

      return current
    })

    const a = await room.turns.ensureGroupChatSession('Alpha', member, 't-a')
    const b = await room.turns.ensureGroupChatSession('Alpha', member, 't-b')
    const aAgain = await room.turns.ensureGroupChatSession('Alpha', member, 't-a')

    expect(a.stored).not.toBe(b.stored)
    expect(aAgain.stored).toBe(a.stored)
    expect(room.gateway.sessions.get(String(a.stored))?.title).not.toBe(
      room.gateway.sessions.get(String(b.stored))?.title
    )
  })

  it('adopts a pre-thread member session into the first thread that speaks', async () => {
    // Rooms persisted before thread scoping hold ONE bare member pointer.
    // The first thread continues that conversation; every later thread gets
    // its own session, so the upgrade never orphans existing history.
    const room = await loadRoom()
    const member: GroupMember = { name: 'research', title: '' }

    room.chat.updateGroupChat('Alpha', current => {
      current.roomId = 'r-one'

      return current
    })
    const legacy = await room.turns.ensureGroupChatSession('Alpha', member, 'legacy')
    // Re-seat it under the pre-thread bare key, as an upgraded room has it.
    room.chat.updateGroupChat('Alpha', current => {
      current.sessions = { research: String(legacy.stored) }

      return current
    })

    const adopted = await room.turns.ensureGroupChatSession('Alpha', member, 't-first')

    expect(adopted.stored).toBe(legacy.stored)

    const later = await room.turns.ensureGroupChatSession('Alpha', member, 't-second')

    expect(later.stored).not.toBe(legacy.stored)
  })

  it('keeps thread-scoped keys source-safe for remote members', async () => {
    // The member half of the key is still the source-qualified roster key, so
    // `research` on the Mini and a local `research` never share a session.
    const room = await loadRoom()

    room.chat.updateGroupChat('Alpha', current => {
      current.roomId = 'r-one'

      return current
    })

    await room.turns.ensureGroupChatSession('Alpha', { name: 'research', title: '' }, 't-a')
    await room.turns.ensureGroupChatSession(
      'Alpha',
      { connectionId: 'mini', name: 'research', remoteSource: true },
      't-a'
    )

    const keys = Object.keys(room.chat.$groupChats.get().Alpha.sessions || {})

    expect(keys).toHaveLength(2)
    expect(keys.some(key => key.endsWith('::mini::research'))).toBe(true)
    expect(keys.some(key => key.endsWith('::research') && !key.includes('mini'))).toBe(true)
  })
})

describe('session-gone classification', () => {
  it('treats 4001 and "not in memory" as recoverable, 4007 as not', async () => {
    const { turns } = await loadRoom()

    expect(turns.isSessionGoneError(Object.assign(new Error('x'), { code: 4001 }))).toBe(true)
    expect(turns.isSessionGoneError(new Error('session_id=rt-1 not in memory'))).toBe(true)
    expect(turns.isSessionGoneError(Object.assign(new Error('session not found'), { code: 4007 }))).toBe(false)
    expect(turns.isSessionGoneError(null)).toBe(false)
    expect(turns.isSessionGoneError(new Error('network blip'))).toBe(false)
  })

  it('recovers a 4001 on the first submit via the STORED id and delivers', async () => {
    const room = await loadRoom({
      failFirstSubmitWith: Object.assign(new Error("session-scoped RPC rejected: session_id='rt-1' not in memory"), {
        code: 4001
      }),
      turn: () => 'recovered reply'
    })

    const reply = await room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'hi there', 't1', [])

    expect(reply).toBe('recovered reply')
    // One failed submit + exactly one retry — never more.
    expect(room.gateway.rpcFor('prompt.submit')).toHaveLength(2)
    // The recovery re-resumed the durable stored id, not the dead runtime id.
    expect(room.chat.$groupChats.get().Room.sessions?.['thread:t1::helper']).toBeTruthy()
  })

  it('does not retry a persistent non-4001 submit failure', async () => {
    const room = await loadRoom({ failEverySubmitWith: new Error('backend exploded') })

    await expect(room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'hi', 't1', [])).rejects.toThrow(
      'backend exploded'
    )
    expect(room.gateway.rpcFor('prompt.submit')).toHaveLength(1)
  })

  // #92760 silent stall (#95103): the gateway RETAINS a failed turn under
  // `inflight` as `{ status: 'error', … }` so reconnecting clients can rebuild
  // the error. Read as "busy", that tombstone kept sliding the deadline to the
  // 20-minute cap and the member looked like it was thinking forever.
  it('surfaces a retained failed turn immediately instead of waiting on it as live work', async () => {
    // Each clock read jumps a minute: a loop that keeps extending the deadline
    // hits the hard cap in a bounded number of polls instead of spinning.
    let now = 1_000_000
    const clock = vi.spyOn(Date, 'now').mockImplementation(() => (now += 60_000))
    const room = await loadRoom({ retainedErrorAfterSubmit: 'No usable credentials found', turn: () => [] })

    try {
      await expect(room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'hi', 't1', [])).rejects.toThrow(
        'No usable credentials found'
      )
      // The submit itself succeeded once; no timed-out/stranded marker was left behind.
      expect(room.gateway.rpcFor('prompt.submit')).toHaveLength(1)
      expect(room.chat.$groupChats.get().Room?.stranded?.helper).toBeUndefined()
      expect(room.turns.groupSessionBusy({ inflight: { status: 'error' }, running: false })).toBe(false)
      expect(room.turns.groupSessionBusy({ inflight: { status: 'streaming' }, running: false })).toBe(true)
      expect(room.turns.groupSessionBusy({ inflight: true })).toBe(true)
    } finally {
      clock.mockRestore()
    }
  })

  // The core loop closes a failed turn with a Hermes-authored assistant row
  // typed `display_kind: failed_turn` (agent/turn_failure_copy.py). That row is
  // a transcript boundary, not the member speaking: read as the reply, the
  // room posted it as the bot's answer, re-drove the member and hid the error.
  it('reports a failed turn whose transcript Hermes closed with the failed-turn notice', async () => {
    let now = 1_000_000
    const clock = vi.spyOn(Date, 'now').mockImplementation(() => (now += 60_000))

    const room = await loadRoom({
      turn: () => [{ content: 'Your request was not processed.', display_kind: 'failed_turn', role: 'assistant' }]
    })

    const request = host.request as (method: string, params?: Record<string, unknown>) => Promise<unknown>
    let submitted = false

    // The real gateway keeps the tombstone AND the closed transcript.
    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' && submitted
        ? { ...result, inflight: { error: 'HTTP 401: invalid_api_key', status: 'error', streaming: false } }
        : result
    }

    try {
      await expect(room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'hi', 't1', [])).rejects.toThrow(
        'HTTP 401: invalid_api_key'
      )
    } finally {
      clock.mockRestore()
    }
  })

  // The member spoke, called a tool, then the provider failed. The core closer
  // writes no boundary behind a tool row, so the retained error is the only
  // evidence; with a boundary but the retained error gone (backend restarted)
  // the turn still failed. Either way the pre-tool text is not the reply.
  it.each([
    ['the retained provider error', [], 'HTTP 401: invalid_api_key'],
    [
      'the failed-turn notice once the retained error is gone',
      [{ content: PARTIAL_NOTICE, display_kind: 'failed_turn', role: 'assistant' }],
      null
    ]
  ])('reports a turn that failed after pre-tool text with %s', async (_label, boundary, retained) => {
    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' },
        ...boundary
      ]
    })

    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>

      submitted = submitted || method === 'prompt.submit'

      return method === 'session.resume' && submitted && retained
        ? { ...result, inflight: { error: retained, status: 'error', streaming: false } }
        : result
    }

    try {
      await expect(room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'hi', 't1', [])).rejects.toThrow(
        retained ?? PARTIAL_NOTICE
      )
    } finally {
      clock.mockRestore()
    }
  })

  // 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
  // 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', () => {
  it('is acquired before any session RPC and held across attach + submit', async () => {
    const room = await loadRoom({ turn: () => 'routed reply' })

    const reply = await room.turns.runGroupChatMemberTurn('Room', ROUTED_MEMBER, 'look at this', 't1', [IMG])

    expect(reply).toBe('routed reply')
    // The retain landed before the first session-scoped RPC on the route.
    expect(room.gateway.timeline[0]).toBe('retain')
    expect(room.gateway.retains).toEqual([{ spawnPriority: 'foreground' }])

    const resumes = room.gateway.rpcFor('session.resume')

    expect(resumes.length).toBeGreaterThan(0)

    for (const resume of resumes) {
      expect(resume).toMatchObject({ spawnPriority: 'foreground' })
    }

    expect(room.gateway.rpcFor('session.create')).toEqual([expect.objectContaining({ spawnPriority: 'foreground' })])

    // The socket was NEVER disposed mid-turn: after every per-request lease
    // released, the turn lease still held the refcount above zero.
    for (const call of room.gateway.rpc) {
      expect(call.refcountAfter).toBeGreaterThanOrEqual(1)
    }

    // Exactly one disposal, via the turn lease's own release at the end.
    expect(room.gateway.disposals()).toBe(1)
    expect(room.gateway.timeline.at(-1)).toBe('release')
  })

  it('is released even when the turn fails', async () => {
    const room = await loadRoom({ failEverySubmitWith: new Error('backend exploded') })

    await expect(room.turns.runGroupChatMemberTurn('Room', ROUTED_MEMBER, 'hi', 't1', [])).rejects.toThrow()
    expect(room.gateway.refcount()).toBe(0)
  })

  it('is feature-detected: hosts without retainProfile still run the turn', async () => {
    const room = await loadRoom({ turn: () => 'legacy ok' })
    delete host.retainProfile

    expect(await room.turns.runGroupChatMemberTurn('Room', ROUTED_MEMBER, 'hi', 't1', [])).toBe('legacy ok')
  })
})

describe('push-woken poll', () => {
  it("wakes on the member session's message.complete instead of sleeping out the backstop", async () => {
    // Real timers here: the contract is about WHEN the poll re-reads.
    vi.unstubAllGlobals()
    const listeners = new Map<string, Set<(event: unknown) => void>>()
    const room = await loadRoom({ pollsBusy: 1, turn: () => 'woken reply' })

    host.onEvent = (type: string, listener: (event: unknown) => void) => {
      const set = listeners.get(type) ?? new Set()
      set.add(listener)
      listeners.set(type, set)

      return () => set.delete(listener)
    }

    const started = Date.now()
    const turn = room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'hi', 't1', [])

    // Let the submit land and the first poll wait attach its listeners, then
    // fire the terminal frame for the runtime id the submit used.
    await new Promise(resolve => setTimeout(resolve, 50))
    // The harness mints a fresh runtime id on every resume; the frame carries
    // whichever one the session currently answers to.
    const runtime = room.gateway.sessions.get(room.gateway.calls[0]?.stored)?.runtime
    expect(listeners.get('message.complete')?.size).toBe(1)

    for (const listener of listeners.get('message.complete') ?? []) {
      listener({ type: 'message.complete', session_id: runtime })
    }

    expect(await turn).toBe('woken reply')
    // Two quick re-reads (busy once, then done) — well under one 5s backstop tick.
    expect(Date.now() - started).toBeLessThan(2000)
    // Every listener was disposed once the turn finished.
    expect(listeners.get('message.complete')?.size ?? 0).toBe(0)
    expect(listeners.get('error')?.size ?? 0).toBe(0)
  })
})

// #94376: a Codex intent-ack continuation nudge can land a substantive
// answer, then get a synthetic "(pass)" reply to the nudge itself.
describe('reply selection (#94376)', () => {
  it('surfaces a substantive answer followed by a synthetic continuation (pass)', async () => {
    const answer = "Yes. I welcomed them, and I'll review their first assignments with them."

    const room = await loadRoom({
      turn: () => [
        { content: answer, role: 'assistant' },
        {
          content:
            '[System: Continue now. Execute the required tool calls and only send your final answer after completing the task.]',
          role: 'user'
        },
        { content: '(pass)', role: 'assistant' }
      ]
    })

    expect(await room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'Did you welcome them?', 't1', [])).toBe(
      answer
    )
  })

  it('still reads a genuine pass-only turn as silent', async () => {
    const room = await loadRoom({ turn: () => '(pass)' })

    expect(await room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'anything new?', 't1', [])).toBe('(pass)')
  })
})

// #90694: a member can block inside its own turn on a clarify question or a
// command approval. Those live in a hidden session the room can't see, so the
// poll mirrors them into the room and holds the turn open until they resolve.
describe('clarify and approvals (#90694)', () => {
  /** The member's blocking clarify as `session.resume` now reports it: an
   *  open server→client request frame (`open_requests`). */
  const CLARIFY = {
    id: 'req-clarify-1',
    method: 'clarify',
    params: {
      questions: [
        { choices: ['staging', 'prod'], multi_select: false, qid: 'q0', question: 'Which env should I target?' }
      ]
    }
  }

  const APPROVAL = {
    choices: ['once', 'session', 'deny'],
    command: 'rm -rf ./build',
    description: 'Clean the build directory',
    request_id: 'req-approval-1'
  }

  it('holds the turn open while a member is blocked on clarify, then lands the reply', async () => {
    let live: Awaited<ReturnType<typeof loadRoom>> | null = null
    let sawPendingAttention = false

    const room = await loadRoom({
      clarifyUntil: { research: { payload: CLARIFY, until: 3 } },
      // The mirror pass runs while the question is still blocking — this is
      // the observable proof the gate inspected open_requests. Asserting
      // on $groupNeedsYou/$groupClarify AFTER the turn lands proves nothing:
      // the clarify has already resolved and its mirror is gone by then.
      onResumePoll: () => {
        sawPendingAttention =
          sawPendingAttention || live!.turns.groupHasPendingClarify(live!.chat.$groupClarify.get(), 'Core')
      },
      turn: () => 'targeting staging'
    })

    live = room

    const thread = room.rounds.sendToGroupChat(
      'Core',
      [{ name: 'research', title: '' }],
      '@research deploy it',
      null,
      []
    )

    await drain(() => Boolean(room.chat.$groupChats.get().Core?.running))

    const replies = log(room, 'Core').filter(entry => entry.thread === thread && entry.from.kind === 'member')

    expect(replies).toHaveLength(1)
    expect(replies[0].text).toBe('targeting staging')
    expect(Object.keys(room.chat.$groupClarify.get())).toHaveLength(0)
    expect(sawPendingAttention).toBe(true)
    // Resolved and mirrored away — nothing left to badge.
    expect(room.turns.groupHasPendingClarify(room.chat.$groupClarify.get(), 'Core')).toBe(false)
  })

  it('mirrors a question, badges needs-you, and is idempotent per request', async () => {
    const { chat, turns } = await loadRoom()
    const member: GroupMember = { name: 'research', title: '' }

    expect(turns.syncGroupClarify('Core', member, 't1', { open_requests: [CLARIFY] })).toBe(true)

    const mirrored = Object.values(chat.$groupClarify.get())

    expect(mirrored).toHaveLength(1)
    expect(mirrored[0].requestId).toBe('req-clarify-1')
    expect(mirrored[0]).toMatchObject({ kind: 'clarify', questions: CLARIFY.params.questions })
    // Badge is derived from $groupClarify, not a copy — nothing writes
    // $groupNeedsYou here, so there is nothing to keep in sync.
    expect(turns.groupHasPendingClarify(chat.$groupClarify.get(), 'Core')).toBe(true)

    // Same request again: no new entry, identity preserved.
    turns.syncGroupClarify('Core', member, 't1', { open_requests: [CLARIFY] })

    expect(Object.values(chat.$groupClarify.get())[0]).toBe(mirrored[0])

    // Question resolved server-side: the mirror clears, and so does the
    // derived badge — no separate cleanup path required.
    expect(turns.syncGroupClarify('Core', member, 't1', {})).toBe(false)
    expect(Object.keys(chat.$groupClarify.get())).toHaveLength(0)
    expect(turns.groupHasPendingClarify(chat.$groupClarify.get(), 'Core')).toBe(false)
  })

  it('never mirrors a question for older backends without open_requests', async () => {
    const { chat, turns } = await loadRoom()

    expect(turns.syncGroupClarify('Core', { name: 'research' }, 't1', { messages: [] })).toBe(false)
    expect(Object.keys(chat.$groupClarify.get())).toHaveLength(0)
  })

  it('locks one batch question per clarify.lock call, in order', async () => {
    const room = await loadRoom()
    const member: GroupMember = { name: 'research', title: '' }

    room.turns.syncGroupClarify('Core', member, 't1', {
      open_requests: [
        {
          id: 'req-batch-1',
          method: 'clarify',
          params: {
            questions: [
              { choices: ['staging', 'prod'], qid: 'q0', question: 'Env?' },
              { choices: [], qid: 'q1', question: 'Region?' }
            ]
          }
        }
      ]
    })
    await room.turns.answerGroupClarify(Object.values(room.chat.$groupClarify.get())[0], member, {
      q0: 'staging',
      q1: 'eu-west'
    })

    expect(room.gateway.rpcFor('clarify.lock').map(call => call.params)).toEqual([
      { answer: 'staging', question_id: 'q0', request_id: 'req-batch-1' },
      { answer: 'eu-west', question_id: 'q1', request_id: 'req-batch-1' }
    ])
    expect(Object.keys(room.chat.$groupClarify.get())).toHaveLength(0)
  })

  it('clears only the disbanded room’s mirrored questions', async () => {
    const room = await loadRoom()

    room.turns.syncGroupClarify('Core', { name: 'research' }, 't1', { open_requests: [CLARIFY] })
    room.turns.syncGroupClarify('Other', { name: 'ops' }, 't1', { open_requests: [{ ...CLARIFY, id: 'req-2' }] })
    room.turns.clearGroupClarify('Core')

    const remaining = Object.values(room.chat.$groupClarify.get())

    expect(remaining).toHaveLength(1)
    expect(remaining[0].group).toBe('Other')
    // The derived badge follows $groupClarify with no separate cleanup step.
    expect(room.turns.groupHasPendingClarify(room.chat.$groupClarify.get(), 'Core')).toBe(false)
    expect(room.turns.groupHasPendingClarify(room.chat.$groupClarify.get(), 'Other')).toBe(true)
  })

  it('keeps pending prompts independent from mention attention through their lifecycle', async () => {
    const { chat, turns } = await loadRoom()
    const member = { name: 'research', title: '' }
    turns.syncGroupClarify('Core', member, 't1', { open_requests: [CLARIFY] })
    expect(chat.$groupNeedsYou.get().Core).toBeFalsy()
    turns.syncGroupClarify('Core', { name: 'ops' }, 't1', { pending_approval: APPROVAL })
    expect(Object.values(chat.$groupClarify.get())).toHaveLength(2)
    turns.syncGroupClarify('Core', member, 't1', {})
    expect(turns.groupHasPendingClarify(chat.$groupClarify.get(), 'Core')).toBe(true)
    turns.syncGroupClarify('Core', { name: 'ops' }, 't1', {})
    expect(turns.groupHasPendingClarify(chat.$groupClarify.get(), 'Core')).toBe(false)
    chat.appendGroupChatEntry('Core', { kind: 'member', name: 'research' }, '@user please review')
    turns.syncGroupClarify('Core', member, 't1', { pending_approval: APPROVAL })
    await turns.answerGroupClarify(Object.values(chat.$groupClarify.get())[0], member, 'deny')
    expect(Object.values(chat.$groupClarify.get())).toHaveLength(0)
    expect(chat.$groupNeedsYou.get().Core).toBe(true)
  })

  it('keeps late prompt snapshots on the live room and never revives a disbanded room', async ({ onTestFinished }) => {
    for (const roomId of ['stable-room', undefined]) {
      for (const disband of [false, true]) {
        const room = await loadRoom({ turn: ({ n }) => (n === 1 ? 'Completed reply' : '(pass)') })
        const view = await import('./group-chat-view')
        const member = { name: 'research', title: '' }
        room.chat.updateGroupChat('Core', current => ({
          ...current,
          roomId,
          running: true,
          epoch: 1,
          log: [{ id: 'input', at: 1, from: { kind: 'user', name: 'You' }, text: '@research check', thread: 'thread' }]
        }))
        let entered!: () => void
        let release!: () => void

        const polled = new Promise<void>(resolve => {
          entered = resolve
        })

        const held = new Promise<void>(resolve => {
          release = resolve
        })

        const original = host.request as (method: string, params: Record<string, unknown>) => Promise<any>
        let submitted = false
        let answered = false
        let polls = 0

        host.request = async (method: string, params: Record<string, unknown>) => {
          const result = await original(method, params)

          if (method === 'prompt.submit') {
            submitted = true
          }

          if (method === 'clarify.lock') {
            answered = true
          }

          if (method === 'session.resume' && submitted && !answered) {
            if (++polls === 1) {
              entered()
              await held
            }

            return { ...result, open_requests: [CLARIFY] }
          }

          return result
        }

        const drive = room.rounds.runGroupChatRounds('Core', [member], 'thread')
        await polled

        if (disband) {
          await view.disbandGroupChat('Core', [])
          release()
          await drive
          expect(Object.values(room.chat.$groupClarify.get())).toHaveLength(0)
          expect(room.chat.$groupChats.get().Core === undefined || room.chat.$groupChats.get().Core.tombstone).toBe(
            true
          )
          expect(room.chat.$groupChats.get().Core?.log || []).toHaveLength(0)
        } else {
          await view.renameGroupChat('Core', 'Renamed', [])

          const mirrored = new Promise<void>(resolve => {
            const stop = room.chat.$groupClarify.listen(entries => {
              if (Object.keys(entries).length) {
                stop()
                resolve()
              }
            })
          })

          release()
          await mirrored
          const [prompt] = Object.values(room.chat.$groupClarify.get())
          const correctRoom = prompt.group
          await room.turns.answerGroupClarify(prompt, member, { q0: 'staging' })
          await drive
          expect(correctRoom).toBe('Renamed')
          expect(Object.keys(room.chat.$groupChats.get())).toEqual(['Renamed'])
          expect(room.chat.$groupChats.get().Renamed.running).toBe(false)
          expect(
            room.chat.$groupChats
              .get()
              .Renamed.log.filter(entry => entry.from.kind === 'member')
              .map(entry => entry.text)
          ).toEqual(['Completed reply'])
          expect(Object.values(room.chat.$groupClarify.get())).toHaveLength(0)
        }
      }
    }

    // Rejected member setup/submit must not publish failure cues into a new room.
    for (const continuation of [false, true]) {
      for (const rejectedMethod of ['session.resume', 'prompt.submit']) {
        const room = await loadRoom()
        const view = await import('./group-chat-view')
        const activity = await import('./group-activity')
        const data = await import('./data')
        const members = [{ name: 'research' }, { name: 'ops' }]
        room.chat.updateGroupChat('Core', current => ({
          ...current,
          roomId: 'old-rejection-room',
          running: true,
          log: continuation
            ? [
                {
                  id: 'handoff',
                  at: 1,
                  from: { kind: 'member', name: 'research' },
                  text: '@ops check',
                  thread: 'thread'
                },
                { id: 'input', at: 2, from: { kind: 'user', name: 'You' }, text: '@research check', thread: 'thread' }
              ]
            : [{ id: 'input', at: 1, from: { kind: 'user', name: 'You' }, text: 'check', thread: 'thread' }],
          watermarks: { 'thread::research': continuation ? 2 : 0 }
        }))
        // The normal responder has no delta; the earlier unanswered @ops
        // handoff is driven by the continuation phase.
        let phaseEntered!: () => void
        let release!: () => void

        const entered = new Promise<void>(resolve => {
          phaseEntered = resolve
        })

        const held = new Promise<void>(resolve => {
          release = resolve
        })

        const original = host.request as (method: string, params: Record<string, unknown>) => Promise<any>

        host.request = async (method: string, params: Record<string, unknown>) => {
          if (method === rejectedMethod) {
            phaseEntered()
            await held
            throw new Error('401 unauthorized late rejection')
          }

          return original(method, params)
        }

        const drive = room.rounds.runGroupChatRounds('Core', members, 'thread')
        await entered
        await view.disbandGroupChat('Core', [])
        room.chat.updateGroupChat('Core', current => ({
          ...current,
          roomId: 'replacement-rejection-room',
          tombstone: false
        }))
        room.chat.appendGroupChatEntry('Core', { kind: 'member', name: 'research' }, '@user replacement needs you')

        const before = structuredClone({
          rooms: room.chat.$groupChats.get(),
          activity: activity.$groupActivity.get(),
          attention: data.$botAttention.get(),
          needsYou: room.chat.$groupNeedsYou.get()
        })

        release()
        await drive
        expect({
          rooms: room.chat.$groupChats.get(),
          activity: activity.$groupActivity.get(),
          attention: data.$botAttention.get(),
          needsYou: room.chat.$groupNeedsYou.get()
        }).toEqual(before)
        expect(room.gateway.rpcFor('prompt.submit')).toHaveLength(0)
      }
    }

    const clock = vi.spyOn(Date, 'now').mockReturnValue(0)
    onTestFinished(() => clock.mockRestore())

    for (const recreate of [false, true]) {
      clock.mockReturnValue(0)
      const room = await loadRoom()
      const view = await import('./group-chat-view')
      const member = { name: 'research', title: '' }
      room.chat.updateGroupChat('Core', current => ({ ...current, roomId: 'retired-room' }))
      let entered!: () => void
      let release!: () => void

      const polled = new Promise<void>(resolve => {
        entered = resolve
      })

      const held = new Promise<void>(resolve => {
        release = resolve
      })

      const original = host.request as (method: string, params: Record<string, unknown>) => Promise<any>
      let submitted = false

      host.request = async (method: string, params: Record<string, unknown>) => {
        if (method === 'session.resume' && submitted) {
          entered()
          await held
          throw new Error('poll rejected after deadline')
        }

        const result = await original(method, params)

        if (method === 'prompt.submit') {
          submitted = true
        }

        return result
      }

      const turn = room.turns.runGroupChatMemberTurn('Core', member, 'check', 'thread', [])
      await polled
      await view.disbandGroupChat('Core', [])

      if (recreate) {
        room.chat.updateGroupChat('Core', current => ({ ...current, roomId: 'replacement-room' }))
        room.turns.syncGroupClarify('Core', member, 't1', { open_requests: [CLARIFY] })
      }

      const roomsBefore = structuredClone(room.chat.$groupChats.get())
      const promptsBefore = structuredClone(room.chat.$groupClarify.get())
      // Cross even the hard cap while the rejected poll is still in flight.
      clock.mockReturnValue(24 * 60 * 60 * 1000)
      release()
      expect(await turn).toBeNull()
      expect(room.chat.$groupChats.get()).toEqual(roomsBefore)
      expect(room.chat.$groupClarify.get()).toEqual(promptsBefore)
    }

    // Retirement during one background harvest must fence the next member too.
    const room = await loadRoom()
    const view = await import('./group-chat-view')
    const members = [{ name: 'research' }, { name: 'ops' }]
    room.chat.updateGroupChat('Core', current => ({
      ...current,
      roomId: 'old-harvest-room',
      running: true,
      stranded: { research: 0, ops: 0 }
    }))
    let tick!: () => void
    const previousWindow = globalThis.window
    vi.stubGlobal('window', {
      setTimeout: (callback: () => void) => {
        tick = callback

        return 0
      }
    })
    onTestFinished(() => {
      vi.stubGlobal('window', previousWindow)
    })
    let entered!: () => void
    let release!: () => void

    const polled = new Promise<void>(resolve => {
      entered = resolve
    })

    const held = new Promise<void>(resolve => {
      release = resolve
    })

    let background = false
    const backgroundProfiles: unknown[] = []
    const original = host.request as (method: string, params: Record<string, unknown>) => Promise<any>

    host.request = async (method: string, params: Record<string, unknown>) => {
      if (method !== 'session.resume') {
        return original(method, params)
      }

      if (background) {
        backgroundProfiles.push(params.profile)

        if (params.profile === 'research') {
          entered()
          await held
        }
      }

      return { running: true, open_requests: [CLARIFY] }
    }

    await room.rounds.runGroupChatRounds('Core', members, 'thread')
    background = true
    tick()
    await polled
    await view.disbandGroupChat('Core', [])
    const { setImmediate } = await import('node:timers/promises')
    await setImmediate()
    room.chat.updateGroupChat('Core', current => ({
      ...current,
      roomId: 'new-harvest-room',
      stranded: { research: 0, ops: 0 }
    }))
    const roomsBefore = structuredClone(room.chat.$groupChats.get())
    const promptsBefore = structuredClone(room.chat.$groupClarify.get())
    release()
    // Let the released RPC and its background caller finish their microtasks.
    await setImmediate()
    expect(backgroundProfiles).toEqual(['research'])
    expect(room.chat.$groupChats.get()).toEqual(roomsBefore)
    expect(room.chat.$groupClarify.get()).toEqual(promptsBefore)
  })

  it('holds the turn open on a command approval too', async () => {
    let live: Awaited<ReturnType<typeof loadRoom>> | null = null
    let sawPendingAttention = false

    const room = await loadRoom({
      approvalUntil: { research: { payload: APPROVAL, until: 3 } },
      onResumePoll: () => {
        sawPendingAttention =
          sawPendingAttention || live!.turns.groupHasPendingClarify(live!.chat.$groupClarify.get(), 'Core')
      },
      turn: () => 'build cleaned'
    })

    live = room

    const thread = room.rounds.sendToGroupChat(
      'Core',
      [{ name: 'research', title: '' }],
      '@research clean up',
      null,
      []
    )

    await drain(() => Boolean(room.chat.$groupChats.get().Core?.running))

    const replies = log(room, 'Core').filter(entry => entry.thread === thread && entry.from.kind === 'member')

    expect(replies).toHaveLength(1)
    expect(replies[0].text).toBe('build cleaned')
    expect(Object.keys(room.chat.$groupClarify.get())).toHaveLength(0)
    expect(sawPendingAttention).toBe(true)
    expect(room.turns.groupHasPendingClarify(room.chat.$groupClarify.get(), 'Core')).toBe(false)
  })

  it('mirrors an approval with its kind, command and server choices', async () => {
    const { chat, turns } = await loadRoom()

    expect(
      turns.syncGroupClarify('Core', { name: 'research', title: '' }, 't1', {
        pending_approval: APPROVAL,
        session_id: 'rt-research-1'
      })
    ).toBe(true)

    const entry = Object.values(chat.$groupClarify.get())[0]

    expect(entry).toMatchObject({
      choices: ['once', 'session', 'deny'],
      command: 'rm -rf ./build',
      kind: 'approval',
      question: 'Clean the build directory',
      sessionId: 'rt-research-1'
    })
  })

  it('falls back to once/deny when the server sends no choice set', async () => {
    const { chat, turns } = await loadRoom()

    turns.syncGroupClarify('Core', { name: 'research' }, 't1', {
      pending_approval: { command: 'ls', request_id: 'req-a2' }
    })

    expect(Object.values(chat.$groupClarify.get())[0]).toMatchObject({ choices: ['once', 'deny'] })
  })

  it('routes approvals through approval.respond with the session and choice', async () => {
    const room = await loadRoom()
    const member: GroupMember = { name: 'research', title: '' }

    room.turns.syncGroupClarify('Core', member, 't1', { pending_approval: APPROVAL, session_id: 'rt-research-1' })
    await room.turns.answerGroupClarify(Object.values(room.chat.$groupClarify.get())[0], member, 'once')

    expect(room.gateway.rpcFor('approval.respond').map(call => call.params)).toEqual([
      { choice: 'once', request_id: 'req-approval-1', session_id: 'rt-research-1' }
    ])
    expect(room.gateway.rpcFor('request.answer')).toHaveLength(0)
    expect(Object.keys(room.chat.$groupClarify.get())).toHaveLength(0)
  })

  it('lets clarify outrank approval when a snapshot carries both', async () => {
    const { chat, turns } = await loadRoom()

    turns.syncGroupClarify('Core', { name: 'research' }, 't1', { open_requests: [CLARIFY], pending_approval: APPROVAL })

    const entry = Object.values(chat.$groupClarify.get())[0]

    expect(entry.kind).toBe('clarify')
    expect(entry.requestId).toBe('req-clarify-1')
  })
})

// The marker goes down at SUBMIT, not only at the deadline: a turn this Desktop
// abandons (quit or crash mid-turn) keeps running on the member's gateway — a
// remote member's most of all — and only a persisted marker lets the next
// boundary harvest that reply instead of dropping it and re-driving a live
// session. While the poll that wrote it is still running here, the marker is
// "live": not harvested, and no reason to skip the member (#93127 re-drive).
describe('in-flight marker', () => {
  it('marks a turn in flight at submit and clears the marker with its reply', async () => {
    const room = await loadRoom({ pollsBusy: 1, turn: () => 'the answer' })
    const request = host.request as (method: string, params?: Record<string, unknown>) => Promise<unknown>
    const seen: { marker?: unknown; live?: boolean } = {}

    host.request = async (method: string, params: Record<string, unknown> = {}) => {
      const result = await request(method, params)

      if (method === 'session.resume' && room.gateway.rpcFor('prompt.submit').length && seen.marker === undefined) {
        seen.marker = room.chat.$groupChats.get().Room?.stranded?.helper
        seen.live = room.turns.strandedMarkerIsLive(seen.marker)
      }

      return result
    }

    const reply = await room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'hi', 't1', [])

    expect(reply).toBe('the answer')
    expect(seen.marker).toMatchObject({ before: 0, thread: 't1' })
    expect(typeof (seen.marker as { turn?: unknown }).turn).toBe('string')
    expect(seen.live).toBe(true)
    expect(room.chat.$groupChats.get().Room?.stranded?.helper).toBeUndefined()
  })

  it("harvests a remote member's turn that a previous Desktop process left in flight", async () => {
    const room = await loadRoom()
    const { groupSessionKey } = await import('./group-membership')

    // What the durable room looks like after a restart: the marker persisted with a
    // token no poll in THIS process owns, and the member's session on ITS machine
    // finished the turn in the meantime.
    room.chat.updateGroupChat('Fleet', current => {
      current.sessions = { [groupSessionKey('t1', ROUTED_MEMBER)]: 'sid-mini-helper' }
      current.stranded = { 'mini::helper': { before: 1, thread: 't1', turn: 'rt-gone:abandoned' } }

      return current
    })
    room.gateway.sessions.set('sid-mini-helper', {
      messages: [
        { content: roomPrompt('Fleet'), role: 'user' },
        { content: 'Finished on the mini after the Desktop went away.', role: 'assistant' }
      ],
      profile: 'helper',
      runtime: 'rt-mini-helper',
      stored: 'sid-mini-helper',
      title: 'Group: Fleet · t1'
    })

    expect(room.turns.strandedMarkerIsLive(room.chat.$groupChats.get().Fleet.stranded?.['mini::helper'])).toBe(false)
    await room.turns.harvestStrandedGroupReply('Fleet', ROUTED_MEMBER)

    expect(log(room, 'Fleet')).toHaveLength(1)
    expect(log(room, 'Fleet')[0].from).toMatchObject({ name: 'helper', source: 'mini' })
    expect(log(room, 'Fleet')[0].text).toMatch(/Finished on the mini/)
    expect(room.chat.$groupChats.get().Fleet.stranded?.['mini::helper']).toBeUndefined()
  })
})

// A turn that outlives its deadline leaves a "stranded" marker. The member is
// still working; the next round harvests whatever landed instead of throwing
// the finished work away.
describe('stranded harvest', () => {
  // #100274: the hard cap is a runaway guard, not a work budget. A member the
  // gateway still reports busy keeps its turn well past the old 20-minute
  // clamp; only a member that goes quiet expires on the idle timeout.
  it('keeps a visibly working member past twenty minutes instead of stranding it', async () => {
    // Every clock read jumps a minute (two reads per poll): twelve busy polls
    // put the turn past 24 minutes while the member is still reporting work.
    let now = 1_000_000
    const clock = vi.spyOn(Date, 'now').mockImplementation(() => (now += 60_000))
    const room = await loadRoom({ pollsBusy: 12, turn: () => 'long deploy done' })
    const activity = await import('./group-activity')

    try {
      expect(await room.turns.runGroupChatMemberTurn('Room', LOCAL_MEMBER, 'deploy', 't1', [])).toBe('long deploy done')
      expect(room.chat.$groupChats.get().Room?.stranded?.helper).toBeUndefined()
      expect(activity.$groupActivity.get().Room?.events.map(event => event.kind)).not.toContain('timed-out')
    } finally {
      clock.mockRestore()
    }
  })

  const seedSession = (room: Room, stored: string, profile: string, title: string, messages: string[][]) => {
    room.gateway.sessions.set(stored, {
      messages: messages.map(([role, content]) => ({ content, role })),
      profile,
      runtime: `rt-${profile}`,
      stored,
      title
    })
  }

  it('posts a late reply into the room and clears the marker', async () => {
    const room = await loadRoom()

    room.chat.updateGroupChat('Late', current => {
      current.sessions = { research: 'sid-research' }
      current.stranded = { research: 0 }

      return current
    })
    // The member's session finished after we stopped waiting.
    seedSession(room, 'sid-research', 'research', 'Group: Late', [
      ['user', roomPrompt('Late')],
      ['assistant', 'Here is the full research result, delivered late.']
    ])

    await room.turns.harvestStrandedGroupReply('Late', { name: 'research', title: '' })

    expect(log(room, 'Late')).toHaveLength(1)
    expect(log(room, 'Late')[0].from.name).toBe('research')
    expect(log(room, 'Late')[0].text).toMatch(/delivered late/)
    expect(room.chat.$groupChats.get().Late.stranded?.research).toBeUndefined()
  })

  it('prefers the substantive answer over a trailing synthetic (pass)', async () => {
    const room = await loadRoom()

    // #94376 class bug at the second call site: the late-landing turn ends
    // with a Codex intent-ack continuation nudge that gets a synthetic
    // "(pass)" — the harvest must still surface the substantive answer.
    room.chat.updateGroupChat('Rescue', current => {
      current.sessions = { research: 'sid-research' }
      current.stranded = { research: 0 }

      return current
    })
    seedSession(room, 'sid-research', 'research', 'Group: Rescue', [
      ['user', roomPrompt('Rescue')],
      ['assistant', 'Here is the full research result, delivered late.'],
      [
        'user',
        '[System: Continue now. Execute the required tool calls and only send your final answer after completing the task.]'
      ],
      ['assistant', '(pass)']
    ])

    await room.turns.harvestStrandedGroupReply('Rescue', { name: 'research', title: '' })

    expect(log(room, 'Rescue')).toHaveLength(1)
    expect(log(room, 'Rescue')[0].text).toMatch(/delivered late/)
    expect(room.chat.$groupChats.get().Rescue.stranded?.research).toBeUndefined()
  })

  it('consumes the marker without posting when the late reply is a pass', async () => {
    const room = await loadRoom()

    room.chat.updateGroupChat('Quiet2', current => {
      current.sessions = { builder: 'sid-builder' }
      current.stranded = { builder: 2 }

      return current
    })
    seedSession(room, 'sid-builder', 'builder', 'Group: Quiet2', [
      ['user', roomPrompt('Quiet2')],
      ['user', roomPrompt('Quiet2')],
      ['assistant', '(pass)']
    ])

    await room.turns.harvestStrandedGroupReply('Quiet2', { name: 'builder', title: '' })

    expect(log(room, 'Quiet2')).toHaveLength(0)
    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', roomPrompt('Dead')]])
    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]
    ])
  })

  // A late turn that spoke, called a tool and then hit a provider failure: the
  // pre-tool text is not a late reply, whether the retained error or only the
  // failed-turn row (retained error gone) says the turn failed.
  it.each([
    ['the retained provider error', [], 'HTTP 401: invalid_api_key'],
    [
      'the failed-turn notice alone',
      [{ content: PARTIAL_NOTICE, display_kind: 'failed_turn', role: 'assistant' }],
      null
    ]
  ])('reports a late turn that failed after pre-tool text with %s', async (_label, boundary, retained) => {
    const room = await loadRoom()
    const activity = await import('./group-activity')

    room.chat.updateGroupChat('Broke', current => {
      current.sessions = { research: 'sid-research' }
      current.stranded = { research: 0 }

      return current
    })
    room.gateway.sessions.set('sid-research', {
      messages: [
        { content: roomPrompt('Broke'), role: 'user' },
        { content: 'Let me check the repo first.', role: 'assistant' },
        { content: 'ok', role: 'tool' },
        ...boundary
      ],
      profile: 'research',
      runtime: 'rt-research',
      stored: 'sid-research',
      title: 'Group: Broke'
    })

    if (retained) {
      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: retained, status: 'error', streaming: false } }
          : result
      }
    }

    await room.turns.harvestStrandedGroupReply('Broke', { name: 'research', title: '' })

    expect(log(room, 'Broke')).toHaveLength(0)
    expect(room.chat.$groupChats.get().Broke.stranded?.research).toBeUndefined()
    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
    // rounds. Without the responder guard research gets re-selected and picks
    // up two EXTRA resume calls of its own before its first post-resubmit
    // poll, which then reports done — so the unguarded run still finishes fast
    // while proving the resubmission happened.
    const room = await loadRoom({
      busyResumes: { research: 2 },
      turn: ({ profile }) => (profile === 'builder' ? 'builder here, all good' : '(pass)')
    })

    // The marker's pre-thread bare-number shape is still supported: presence
    // in `stranded` is what the round-loop guard checks, not the value shape.
    room.gateway.sessions.set('sid-research', {
      messages: [],
      profile: 'research',
      runtime: 'rt-research',
      stored: 'sid-research',
      title: 'Group: Grind'
    })
    room.chat.updateGroupChat('Grind', current => {
      current.log = [{ at: 1, from: { kind: 'user', name: 'You' }, id: 'u1', text: '@research @builder status?' }]
      current.sessions = { research: 'sid-research' }
      current.stranded = { research: 0 }
      current.watermarks = { 'legacy::builder': 0, 'legacy::research': 0 }

      return current
    })

    await room.rounds.runGroupChatRounds(
      'Grind',
      [
        { name: 'research', title: '' },
        { name: 'builder', title: '' }
      ],
      'legacy'
    )

    expect(room.gateway.calls.filter(call => call.profile === 'research')).toHaveLength(0)
    // The marker survives untouched — the harvest confirmed research is still
    // running, so there is nothing to consume yet.
    expect(room.chat.$groupChats.get().Grind.stranded?.research).toBe(0)
    expect(room.gateway.calls.filter(call => call.profile === 'builder')).toHaveLength(1)
  })
})

describe('room record', () => {
  it('persists the roomId alongside the room and survives another room’s disband', async () => {
    const room = await loadRoom()

    room.chat.updateGroupChat('Keep', current => {
      current.log = [{ at: 1, from: { kind: 'user', name: 'You' }, id: 'k1', text: 'hi' }]
      current.roomId = 'r-keep'

      return current
    })
    room.chat.updateGroupChat('Gone', current => {
      current.roomId = 'r-gone'

      return current
    })

    const rooms = { ...room.chat.$groupChats.get() }
    delete rooms.Gone
    room.chat.$groupChats.set(rooms)
    await room.chat.persistGroupChatRooms(rooms)

    const durable = (room.gateway.storage.get('group-chats') || {}) as Record<string, GroupChat>

    expect(durable.Keep.roomId).toBe('r-keep')
    expect('Gone' in durable).toBe(false)
  })
})