Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
86.2 KB
Raw Blame
import { readFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { describe, expect, it, vi } from 'vitest'
import {
  GROK_VISUAL_LIMITS,
  LIMITS,
  type GrokCompletionRequestV2,
  hashGrokCompletionRequest,
  parseGrokCompletionRequest,
  parseGrokCompletionRequestV1,
} from '@clerum/grok-provider-attempt-contract'
import type { RedeemAttemptSuccess } from '../src/controlApiClient.js'
import {
  GROK_UPSTREAM_TEMPERATURE_PROBE_CONFIRMED,
  GrokTransportError,
  MAX_TOOL_CALL_ARGUMENT_BYTES,
  type StreamFrame,
  type StreamGrokCompletionInput,
  streamGrokCompletion,
} from '../src/grokTransport.js'
import { GROK_COMPLETIONS_ORIGIN } from '../src/originPolicy.js'

// Test-only: the pinned image builders both contract suites already share.
const { declaredHeaderPng } = createRequire(import.meta.url)(
  '../../packages/llm-provider-attempt-contract/testImageFixtures.cjs'
) as { declaredHeaderPng: (width: number, height: number) => Buffer }

type VisualFixturePart = { type: string; text?: string; mimeType?: string; data?: string }

const VISUAL_FIXTURES = JSON.parse(
  readFileSync(
    new URL(
      '../../packages/grok-provider-attempt-contract/fixtures/visual-requests.json',
      import.meta.url
    ),
    'utf8'
  )
) as Record<
  'png' | 'jpeg',
  Record<string, unknown> & { messages: Array<{ contentParts: VisualFixturePart[] }> }
>

const REQUEST = {
  schemaVersion: 'grok-completion-request.v1' as const,
  requestId: 'req-001',
  idempotencyKey: 'idem-001',
  provider: 'grok-subscription' as const,
  model: 'gpt-5.1',
  messages: [{ role: 'user' as const, content: 'hello' }],
}

type FetchInput = string | URL | Request

function headerOf(init: RequestInit | undefined, name: string): string | undefined {
  return (init?.headers as Record<string, string> | undefined)?.[name]
}

const REQUEST_HASH = hashGrokCompletionRequest(REQUEST)

function accessTokenFor(label: string): string {
  const encoded = Buffer.from(
    JSON.stringify({
      sub: label,
      'https://api.x.ai/auth': { grok_account_id: 'acct_live_1' },
    })
  ).toString('base64url')
  return `hdr.${encoded}.sig`
}

function redeemSuccess(overrides: Partial<RedeemAttemptSuccess> = {}): RedeemAttemptSuccess {
  return {
    accessToken: accessTokenFor('live'),
    transport: {
      protocolVersion: 'grok-subscription-transport.v1',
      completionsOrigin: GROK_COMPLETIONS_ORIGIN,
      catalogOrigin: 'https://cli-chat-proxy.grok.com/v1/models',
      operation: 'completion_stream',
      servedModel: 'gpt-5.1',
      maxStreamDurationMs: 1_800_000,
    },
    expiryClass: 'short_lived',
    attemptReceipt: 'a'.repeat(64),
    ...overrides,
  }
}

function sseResponse(
  frames: string[],
  status = 200,
  headers: Record<string, string> = {}
): Response {
  return new Response(frames.join(''), {
    status,
    headers: { 'content-type': 'text/event-stream', ...headers },
  })
}

/**
 * One SSE frame per `read()`, unlike `sseResponse`, which hands the whole body
 * over as a single chunk. The argument-budget cases need this: the transport's
 * `maxSseBufferBytes` guard bounds the unparsed tail between two `\n\n`
 * boundaries, so a body joined into one >1 MiB chunk trips that guard instead
 * of the budget under test.
 */
function chunkedSseResponse(frames: string[]): Response {
  const encoder = new TextEncoder()
  let index = 0
  const body = new ReadableStream<Uint8Array>({
    pull(controller) {
      const next = frames[index]
      index += 1
      if (next === undefined) controller.close()
      else controller.enqueue(encoder.encode(next))
    },
  })
  return new Response(body, { status: 200, headers: { 'content-type': 'text/event-stream' } })
}

const ARGUMENT_CHUNK_CHARS = 65_536

/**
 * `{"q":"aaa…"}` split into `ARGUMENT_CHUNK_CHARS`-sized deltas, sized so the
 * retained total lands exactly on `MAX_TOOL_CALL_ARGUMENT_BYTES`. Valid JSON,
 * so the at-limit case can assert the parsed arguments the sink receives.
 */
function argumentDeltas(): string[] {
  const envelopeChars = '{"q":""}'.length
  const full = `{"q":"${'a'.repeat(MAX_TOOL_CALL_ARGUMENT_BYTES - envelopeChars)}"}`
  const chunks: string[] = []
  for (let offset = 0; offset < full.length; offset += ARGUMENT_CHUNK_CHARS) {
    chunks.push(full.slice(offset, offset + ARGUMENT_CHUNK_CHARS))
  }
  return chunks
}

function argumentFrames(deltas: string[]): string[] {
  return deltas.map(
    delta =>
      `data: ${JSON.stringify({
        type: 'response.function_call_arguments.delta',
        item_id: 'call-args',
        delta,
      })}\n\n`
  )
}

describe('streamGrokCompletion', () => {
  // R3-1 (#731): the retained tool-call arguments of one response are bounded
  // by the contract request cap, in UTF-8 bytes (R9-5). Held equal to the
  // contract so raising the request cap moves this bound with it.
  it('T-R3-1d bounds tool-call arguments at the contract request cap', () => {
    expect(MAX_TOOL_CALL_ARGUMENT_BYTES).toBe(LIMITS.maxRequestBodyBytes)
    expect(MAX_TOOL_CALL_ARGUMENT_BYTES).toBe(8 * 1024 * 1024)
  })

  // grok-completion-request.v2: typed image parts on user messages.
  function visualRequest(parts: GrokCompletionRequestV2['messages'][number]['contentParts']) {
    const content = (parts ?? [])
      .filter(part => part.type === 'text')
      .map(part => (part as { text: string }).text)
      .join('\n')
    return {
      schemaVersion: 'grok-completion-request.v2' as const,
      requestId: 'req-visual',
      idempotencyKey: 'idem-visual',
      provider: 'grok-subscription' as const,
      model: REQUEST.model,
      messages: [{ role: 'user' as const, content, contentParts: parts }],
    }
  }

  function attachmentImage(mimeType: 'image/png' | 'image/jpeg', data: string) {
    return {
      type: 'image' as const,
      mimeType,
      data,
      source: { kind: 'attachment' as const, attachmentId: 'att-1', messageId: 'msg-1' },
    }
  }

  function visualInput(request: unknown, requestHash = 'a'.repeat(64)) {
    const fetchFn = vi.fn<typeof fetch>(async () =>
      sseResponse(['data: {"type":"response.completed"}\n\n'])
    )
    const redeem = vi.fn(async () => redeemSuccess())
    const finalize = vi.fn(async () => ({
      providerAttemptId: 'visual-attempt',
      outcome: 'success' as const,
      duplicate: false,
    }))
    const input: StreamGrokCompletionInput = {
      maxDeadlineMs: 1_800_000,
      executionTicket: 'visual-ticket',
      requestHash,
      request: request as StreamGrokCompletionInput['request'],
      ticket: {
        jti: 'visual-ticket',
        hostRef: 'research-host',
        model: REQUEST.model,
        requestHash,
        providerAttemptId: 'visual-attempt',
      },
      redeem,
      finalize,
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    }
    return { input, fetchFn, redeem, finalize }
  }

  function authorized(request: unknown) {
    const parsed = parseGrokCompletionRequest(request)
    if (!parsed.ok) throw new Error(parsed.message)
    return { request: parsed.value, requestHash: hashGrokCompletionRequest(parsed.value) }
  }

  it('T-G2b-1 maps a contract size refusal to payload_too_large before redeem', async () => {
    const oversized = 'A'.repeat(4 * Math.ceil(GROK_VISUAL_LIMITS.maxImageBytes / 3) + 4)
    const { input, redeem, fetchFn } = visualInput(
      visualRequest([attachmentImage('image/png', oversized)])
    )
    const refusal = await streamGrokCompletion(input).catch((err: unknown) => err)
    expect(refusal).toBeInstanceOf(GrokTransportError)
    expect(refusal).toMatchObject({
      code: 'payload_too_large',
      message: expect.stringContaining(
        `image exceeds ${GROK_VISUAL_LIMITS.maxImageBytes} decoded bytes`
      ),
    })
    expect(redeem).not.toHaveBeenCalled()
    expect(fetchFn).not.toHaveBeenCalled()
  })

  it('T-G2b-2 keeps every other contract refusal as invalid_request before redeem', async () => {
    const webp = {
      ...attachmentImage('image/png', declaredHeaderPng(2, 2).toString('base64')),
      mimeType: 'image/webp',
    }
    const { input, redeem, fetchFn } = visualInput(visualRequest([webp as never]))
    const refusal = await streamGrokCompletion(input).catch((err: unknown) => err)
    expect(refusal).toBeInstanceOf(GrokTransportError)
    expect(refusal).toMatchObject({
      code: 'invalid_request',
      message: 'messages[0].contentParts[0].mimeType is not allowed',
    })
    expect(redeem).not.toHaveBeenCalled()
    expect(fetchFn).not.toHaveBeenCalled()
  })

  it.each(['png', 'jpeg'] as const)(
    'T-G2b-3 projects authorized %s parts upstream with detail high and no provenance',
    async format => {
      const { request, requestHash } = authorized({
        ...VISUAL_FIXTURES[format],
        model: REQUEST.model,
      })
      const { input, fetchFn, redeem, finalize } = visualInput(request, requestHash)
      // A V2 deadline travels inside the hashed request; an outer one is refused.
      await expect(streamGrokCompletion({ ...input, deadlineMs: 1000 })).rejects.toMatchObject({
        code: 'invalid_request',
        message: 'Visual request deadlines must be inside the authorized request',
      })
      expect(redeem).not.toHaveBeenCalled()
      expect(fetchFn).not.toHaveBeenCalled()
      await streamGrokCompletion(input)
      const body = JSON.parse(String(fetchFn.mock.calls[0]?.[1]?.body))
      const sourceParts = VISUAL_FIXTURES[format].messages[0]!.contentParts
      expect(body.input).toEqual([
        {
          role: 'user',
          content: sourceParts.map(part =>
            part.type === 'text'
              ? { type: 'input_text', text: part.text }
              : {
                  type: 'input_image',
                  image_url: `data:${part.mimeType};base64,${part.data}`,
                  detail: 'high',
                }
          ),
        },
      ])
      // The image bytes are on the wire; the provenance next to them is not.
      const image = sourceParts.find(part => part.type === 'image')!
      const wire = JSON.stringify(body)
      expect(wire).toContain(image.data)
      expect(wire).not.toContain('attachmentId')
      expect(wire).not.toContain('"source"')
      expect(finalize).toHaveBeenCalledOnce()
    }
  )

  it('T-G2b-4 sends a declared 9000x9000 PNG upstream between text parts, in order', async () => {
    const png = declaredHeaderPng(9000, 9000).toString('base64')
    const { request, requestHash } = authorized(
      visualRequest([
        { type: 'text', text: 'before' },
        attachmentImage('image/png', png),
        { type: 'text', text: 'after' },
      ])
    )
    const { input, fetchFn } = visualInput(request, requestHash)
    await streamGrokCompletion(input)
    const body = JSON.parse(String(fetchFn.mock.calls[0]?.[1]?.body))
    expect(body.input[0].content).toEqual([
      { type: 'input_text', text: 'before' },
      { type: 'input_image', image_url: `data:image/png;base64,${png}`, detail: 'high' },
      { type: 'input_text', text: 'after' },
    ])
  })

  it('T-G2b-5 keeps the V1 wire shape: user content stays a string', async () => {
    const { input, fetchFn } = visualInput(REQUEST, REQUEST_HASH)
    await streamGrokCompletion(input)
    const body = JSON.parse(String(fetchFn.mock.calls[0]?.[1]?.body))
    expect(body.input).toEqual([{ role: 'user', content: 'hello' }])
  })

  it('does not read further upstream bytes while the frame consumer is back-pressured', async () => {
    const encoder = new TextEncoder()
    const events = [
      'data: {"type":"response.output_text.delta","delta":"one"}\n\n',
      'data: {"type":"response.output_text.delta","delta":"two"}\n\n',
      'data: {"type":"response.completed","response":{"usage":{}}}\n\n',
    ]
    let pulls = 0
    const fetchFn = vi.fn(async () => {
      const body = new ReadableStream<Uint8Array>(
        {
          pull(controller) {
            const next = events[pulls]
            pulls += 1
            if (next === undefined) controller.close()
            else controller.enqueue(encoder.encode(next))
          },
        },
        { highWaterMark: 0 }
      )
      return new Response(body, { status: 200, headers: { 'content-type': 'text/event-stream' } })
    })
    let releaseDrain: (() => void) | undefined
    const drained = new Promise<void>(resolve => {
      releaseDrain = resolve
    })
    const frames: unknown[] = []
    const pending = streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-drain',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-drain',
        hostRef: 'research-host',
        model: REQUEST.model,
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-drain',
      },
      redeem: async () => redeemSuccess(),
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-drain',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      onFrame: frame => {
        frames.push(frame)
        // First frame fills the client buffer; later frames are accepted.
        return frames.length === 1 ? drained : undefined
      },
    })
    await new Promise(resolve => setTimeout(resolve, 50))
    expect(frames).toEqual([{ type: 'text', text: 'one' }])
    expect(pulls).toBe(1)
    releaseDrain?.()
    const result = await pending
    expect(result.outcome).toBe('success')
    expect(frames).toEqual([
      { type: 'text', text: 'one' },
      { type: 'text', text: 'two' },
    ])
  })

  // Live xAI (probed 2026-09-18) answers 426 with
  // "Your Grok CLI version (none) is outdated..." when the caller presents no
  // accepted client version. Retrying cannot fix it, so it must not look like
  // a transient outage.
  it('maps an upstream 426 to a non-retryable client_upgrade_required error', async () => {
    const redeem = vi.fn(async () => redeemSuccess())
    const finalize = vi.fn(async () => ({
      providerAttemptId: 'att-426',
      outcome: 'error' as const,
      duplicate: false,
    }))
    const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) => ({
      ok: false,
      status: 426,
      body: null,
      headers: new Headers(),
      text: async () =>
        '{"error":"Your Grok CLI version (none) is outdated. Please update to version 0.1.202 or later."}',
    })) as unknown as typeof fetch
    const pending = streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-426',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-426',
        hostRef: 'research-host',
        model: REQUEST.model,
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-426',
      },
      redeem,
      finalize,
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    await expect(pending).rejects.toMatchObject({
      code: 'client_upgrade_required',
      message: expect.stringMatching(/newer Grok client version/i),
    })
    expect(redeem).toHaveBeenCalled()
    expect(finalize).toHaveBeenCalledWith(
      expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
    )
  })

  it('does not redeem when the client aborted before the attempt was dispatched', async () => {
    const redeem = vi.fn(async () => redeemSuccess())
    const finalize = vi.fn(async () => ({
      providerAttemptId: 'att-pre-abort',
      outcome: 'canceled' as const,
      duplicate: false,
    }))
    const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
      sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])
    )
    const abort = new AbortController()
    abort.abort()
    const result = await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-pre-abort',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-pre-abort',
        hostRef: 'research-host',
        model: REQUEST.model,
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-pre-abort',
      },
      signal: abort.signal,
      redeem,
      finalize,
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    expect(result.outcome).toBe('canceled')
    expect(redeem).not.toHaveBeenCalled()
    expect(fetchFn).not.toHaveBeenCalled()
    expect(finalize).not.toHaveBeenCalled()
  })

  it.each(['response.failed', 'unterminated'])(
    'does not emit partial tool calls after %s',
    async terminal => {
      const emitted: Array<{ type: string }> = []
      const frames = [
        'data: {"type":"response.output_item.done","item":{"type":"function_call","id":"partial","name":"lookup","arguments":"{}"}}\n\n',
      ]
      if (terminal !== 'unterminated')
        frames.push(`data: ${JSON.stringify({ type: terminal })}\n\n`)
      const pending = streamGrokCompletion({
        maxDeadlineMs: 1_800_000,
        executionTicket: 'ticket-partial',
        requestHash: REQUEST_HASH,
        request: REQUEST,
        ticket: {
          jti: 'jti-partial',
          hostRef: 'research-host',
          model: REQUEST.model,
          requestHash: REQUEST_HASH,
          providerAttemptId: 'att-partial',
        },
        redeem: vi.fn(async () => redeemSuccess()),
        finalize: vi.fn(async () => ({
          providerAttemptId: 'att-partial',
          outcome: 'success' as const,
          duplicate: false,
        })),
        fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => sseResponse(frames)),
        lookup: async () => [{ address: '1.2.3.4', family: 4 }],
        onFrame: frame => {
          emitted.push(frame)
        },
      })
      if (terminal === 'unterminated') expect((await pending).outcome).toBe('unknown')
      else await expect(pending).rejects.toThrow(/upstream response failed/)
      expect(emitted).toEqual([])
    }
  )
  it.each([256, 257])(
    'validates the complete %s-call response before emitting executable calls',
    async count => {
      const emitted: Array<{ type: string }> = []
      const finalize = vi.fn(async () => ({
        providerAttemptId: 'att-bound',
        outcome: 'success' as const,
        duplicate: false,
      }))
      const frames = Array.from(
        { length: count },
        (_, index) =>
          `data: ${JSON.stringify({
            type: 'response.output_item.done',
            item: { type: 'function_call', id: `call-${index}`, name: 'lookup', arguments: '{}' },
          })}\n\n`
      )
      frames.push('data: {"type":"response.completed"}\n\n')
      const pending = streamGrokCompletion({
        maxDeadlineMs: 1_800_000,
        executionTicket: 'ticket-bound',
        requestHash: REQUEST_HASH,
        request: REQUEST,
        ticket: {
          jti: 'jti-bound',
          hostRef: 'research-host',
          model: REQUEST.model,
          requestHash: REQUEST_HASH,
          providerAttemptId: 'att-bound',
        },
        redeem: vi.fn(async () => redeemSuccess()),
        finalize,
        fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => sseResponse(frames)),
        lookup: async () => [{ address: '1.2.3.4', family: 4 }],
        onFrame: frame => {
          emitted.push(frame)
        },
      })
      if (count === 256) {
        expect((await pending).outcome).toBe('success')
        expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(256)
      } else {
        await expect(pending).rejects.toThrow(/tool calls exceed 256/)
        // The code, not only the message: the message is unchanged by the
        // taxonomy work, so a message-only assertion passes either way.
        await expect(pending).rejects.toMatchObject({
          code: 'tool_call_limit_exceeded',
          details: { limit: 256, observed: 257 },
        })
        expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(0)
        expect(finalize).toHaveBeenCalledWith(
          expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
        )
      }
    }
  )
  it('reports the buffered-frame limit when one call id repeats past the budget', async () => {
    // Every item carries no id, so upsertPendingTool collapses them onto the
    // single key 'tool' and `pending.size` stays 1. The arguments are complete
    // JSON, so the `output_item.added` branch emits on each frame and
    // `toolFrames` is what grows. Only the buffered-frame guard can fire here.
    const emitted: Array<{ type: string }> = []
    const finalize = vi.fn(async () => ({
      providerAttemptId: 'att-bound',
      outcome: 'success' as const,
      duplicate: false,
    }))
    const frames = Array.from(
      { length: 257 },
      () =>
        `data: ${JSON.stringify({
          type: 'response.output_item.added',
          item: { type: 'function_call', name: 'lookup', arguments: '{}' },
        })}\n\n`
    )
    frames.push('data: {"type":"response.completed"}\n\n')
    const pending = streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-bound',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-bound',
        hostRef: 'research-host',
        model: REQUEST.model,
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-bound',
      },
      redeem: vi.fn(async () => redeemSuccess()),
      finalize,
      fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => sseResponse(frames)),
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      onFrame: frame => {
        emitted.push(frame)
      },
    })
    await expect(pending).rejects.toMatchObject({
      code: 'tool_call_limit_exceeded',
      details: { limit: 256, observed: 257 },
    })
    expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(0)
    expect(finalize).toHaveBeenCalledWith(
      expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
    )
  })
  it('reports the pending-call limit as soon as the budget is exceeded mid-stream', async () => {
    // Distinct ids with empty arguments: nothing is ever complete JSON, so no
    // frame is emitted and `toolFrames` stays at 0 while `pending` grows. Only
    // the pending-map guard can fire here, and it must fire during the stream —
    // the trailing text proves it did, because the post-stream drain would
    // deliver that text first.
    const emitted: Array<{ type: string }> = []
    const finalize = vi.fn(async () => ({
      providerAttemptId: 'att-bound',
      outcome: 'success' as const,
      duplicate: false,
    }))
    const frames = [
      `data: ${JSON.stringify({ type: 'response.output_text.delta', delta: 'before' })}\n\n`,
      ...Array.from(
        { length: 257 },
        (_, index) =>
          `data: ${JSON.stringify({
            type: 'response.output_item.added',
            item: { type: 'function_call', id: `call-${index}`, name: 'lookup', arguments: '' },
          })}\n\n`
      ),
      `data: ${JSON.stringify({ type: 'response.output_text.delta', delta: 'after' })}\n\n`,
    ]
    const pending = streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-bound',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-bound',
        hostRef: 'research-host',
        model: REQUEST.model,
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-bound',
      },
      redeem: vi.fn(async () => redeemSuccess()),
      finalize,
      fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => sseResponse(frames)),
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      onFrame: frame => {
        emitted.push(frame)
      },
    })
    await expect(pending).rejects.toMatchObject({
      code: 'tool_call_limit_exceeded',
      details: { limit: 256, observed: 257 },
    })
    expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(0)
    // Liveness witness and mutation detector in one: the leading text proves
    // the stream was read, and the absence of the trailing text proves the
    // throw happened on the first call past `maxToolCalls` rather than after
    // the stream drained.
    expect(emitted.filter(frame => frame.type === 'text')).toEqual([{ type: 'text', text: 'before' }])
    expect(finalize).toHaveBeenCalledWith(
      expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
    )
  })

  // `maxToolCalls` bounds how many calls a response may carry, not how large
  // each one is. A single call whose `arguments` grow without a ceiling costs
  // the proxy heap and the Host body budget just the same, and the SSE buffer
  // guard cannot see it: it is reset on every `\n\n` boundary.
  it('accepts tool-call arguments that land exactly on the retained budget', async () => {
    const deltas = argumentDeltas()
    expect(deltas.join('')).toHaveLength(MAX_TOOL_CALL_ARGUMENT_BYTES)
    const emitted: StreamFrame[] = []
    const frames = [
      `data: ${JSON.stringify({ type: 'response.output_text.delta', delta: 'before' })}\n\n`,
      `data: ${JSON.stringify({
        type: 'response.output_item.added',
        item: { type: 'function_call', id: 'call-args', name: 'lookup', arguments: '' },
      })}\n\n`,
      ...argumentFrames(deltas),
      `data: ${JSON.stringify({ type: 'response.completed', response: { usage: {} } })}\n\n`,
    ]
    const result = await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-args-ok',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-args-ok',
        hostRef: 'research-host',
        model: REQUEST.model,
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-args-ok',
      },
      redeem: vi.fn(async () => redeemSuccess()),
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-args-ok',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => chunkedSseResponse(frames)),
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      onFrame: frame => {
        emitted.push(frame)
      },
    })
    expect(result.outcome).toBe('success')
    const toolCalls = emitted.flatMap(frame => (frame.type === 'tool_call' ? [frame] : []))
    expect(toolCalls).toHaveLength(1)
    expect(toolCalls[0]?.arguments).toEqual({
      q: 'a'.repeat(MAX_TOOL_CALL_ARGUMENT_BYTES - '{"q":""}'.length),
    })
  })

  it('refuses a stream whose retained tool-call arguments exceed the budget', async () => {
    const deltas = [...argumentDeltas(), 'a'.repeat(ARGUMENT_CHUNK_CHARS)]
    const emitted: StreamFrame[] = []
    const finalize = vi.fn(async () => ({
      providerAttemptId: 'att-args-over',
      outcome: 'success' as const,
      duplicate: false,
    }))
    const frames = [
      `data: ${JSON.stringify({ type: 'response.output_text.delta', delta: 'before' })}\n\n`,
      `data: ${JSON.stringify({
        type: 'response.output_item.added',
        item: { type: 'function_call', id: 'call-args', name: 'lookup', arguments: '' },
      })}\n\n`,
      ...argumentFrames(deltas),
      `data: ${JSON.stringify({ type: 'response.output_text.delta', delta: 'after' })}\n\n`,
      `data: ${JSON.stringify({ type: 'response.completed', response: { usage: {} } })}\n\n`,
    ]
    const pending = streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-args-over',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-args-over',
        hostRef: 'research-host',
        model: REQUEST.model,
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-args-over',
      },
      redeem: vi.fn(async () => redeemSuccess()),
      finalize,
      fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => chunkedSseResponse(frames)),
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      onFrame: frame => {
        emitted.push(frame)
      },
    })
    await expect(pending).rejects.toMatchObject({
      code: 'tool_call_arguments_exceeded',
      details: {
        limit: MAX_TOOL_CALL_ARGUMENT_BYTES,
        observed: MAX_TOOL_CALL_ARGUMENT_BYTES + ARGUMENT_CHUNK_CHARS,
      },
    })
    expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(0)
    // Liveness witness and mutation detector in one: the leading text proves
    // the stream was read, and the absence of the trailing text proves the
    // refusal fired on the delta that crossed the budget rather than after the
    // stream drained.
    expect(emitted.filter(frame => frame.type === 'text')).toEqual([
      { type: 'text', text: 'before' },
    ])
    expect(finalize).toHaveBeenCalledWith(
      expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
    )
  })

  // R9-5 (L-8): the budget is taken from a byte cap, so it counts UTF-8 bytes.
  // 'é' is one UTF-16 code unit and two UTF-8 bytes: these arguments are about
  // half the budget in code units and two bytes over it in UTF-8.
  it('T-R9-5 counts retained tool-call arguments in UTF-8 bytes', async () => {
    const envelope = '{"q":""}'
    const full = `{"q":"${'é'.repeat((MAX_TOOL_CALL_ARGUMENT_BYTES - envelope.length) / 2 + 1)}"}`
    expect(full.length).toBeLessThan(MAX_TOOL_CALL_ARGUMENT_BYTES)
    expect(Buffer.byteLength(full, 'utf8')).toBe(MAX_TOOL_CALL_ARGUMENT_BYTES + 2)
    const deltas: string[] = []
    for (let offset = 0; offset < full.length; offset += ARGUMENT_CHUNK_CHARS) {
      deltas.push(full.slice(offset, offset + ARGUMENT_CHUNK_CHARS))
    }
    const emitted: StreamFrame[] = []
    const finalize = vi.fn(async () => ({
      providerAttemptId: 'att-args-utf8',
      outcome: 'success' as const,
      duplicate: false,
    }))
    const frames = [
      `data: ${JSON.stringify({ type: 'response.output_text.delta', delta: 'before' })}\n\n`,
      `data: ${JSON.stringify({
        type: 'response.output_item.added',
        item: { type: 'function_call', id: 'call-args', name: 'lookup', arguments: '' },
      })}\n\n`,
      ...argumentFrames(deltas),
      `data: ${JSON.stringify({ type: 'response.output_text.delta', delta: 'after' })}\n\n`,
      `data: ${JSON.stringify({ type: 'response.completed', response: { usage: {} } })}\n\n`,
    ]
    const pending = streamGrokCompletion({
      executionTicket: 'ticket-args-utf8',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-args-utf8',
        hostRef: 'research-host',
        model: REQUEST.model,
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-args-utf8',
      },
      maxDeadlineMs: 300_000,
      redeem: vi.fn(async () => redeemSuccess()),
      finalize,
      fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => chunkedSseResponse(frames)),
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      onFrame: frame => {
        emitted.push(frame)
      },
    })
    await expect(pending).rejects.toMatchObject({
      code: 'tool_call_arguments_exceeded',
      details: { limit: MAX_TOOL_CALL_ARGUMENT_BYTES, observed: MAX_TOOL_CALL_ARGUMENT_BYTES + 2 },
    })
    expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(0)
    // Liveness witness: the leading text proves the stream was read.
    expect(emitted.filter(frame => frame.type === 'text')).toEqual([{ type: 'text', text: 'before' }])
    expect(finalize).toHaveBeenCalledWith(
      expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
    )
  })

  it('validates ticket bindings before redeem and maps stream frames including tool-call data', async () => {
    const redeem = vi.fn(async () => redeemSuccess())
    const finalize = vi.fn(async () => ({
      providerAttemptId: 'att-1',
      outcome: 'success' as const,
      duplicate: false,
    }))
    const frames: unknown[] = []
    const fetchFn = vi.fn(async (url: FetchInput, _init?: RequestInit) => {
      expect(url).toBe(GROK_COMPLETIONS_ORIGIN)
      return sseResponse([
        'data: {"type":"response.output_text.delta","delta":"hi"}\n\n',
        'data: {"type":"response.output_item.added","item":{"type":"function_call","id":"call-1","name":"lookup","arguments":"{\\"q\\":\\"x\\"}"}}\n\n',
        'data: {"type":"response.completed","response":{"usage":{"input_tokens":3,"output_tokens":2}}}\n\n',
      ])
    })

    const result = await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-1',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: '11111111-1111-4111-8111-111111111111',
        hostRef: 'research-host',
        model: 'gpt-5.1',
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-1',
      },
      redeem,
      finalize,
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      onFrame: frame => {
        frames.push(frame)
      },
    })

    expect(redeem).toHaveBeenCalledOnce()
    expect(fetchFn).toHaveBeenCalledOnce()
    expect(frames).toEqual(
      expect.arrayContaining([
        { type: 'text', text: 'hi' },
        { type: 'tool_call', id: 'call-1', name: 'lookup', arguments: { q: 'x' } },
      ])
    )
    expect(result.outcome).toBe('success')
    expect(result.usage).toEqual({ inputTokens: 3, outputTokens: 2 })
    expect(finalize).toHaveBeenCalledWith(
      expect.objectContaining({
        attemptReceipt: 'a'.repeat(64),
        receipt: expect.objectContaining({ outcome: 'success', requestHash: REQUEST_HASH }),
      })
    )
    expect(String(headerOf(fetchFn.mock.calls[0]?.[1], 'authorization'))).toContain(
      accessTokenFor('live')
    )
    // xAI gates on a client version; we send it under an Evenfire identity.
    expect(String(headerOf(fetchFn.mock.calls[0]?.[1], 'user-agent'))).toMatch(
      /^evenfire-grok-subscription grok-build\/\d+\.\d+\.\d+/
    )
    expect(String(headerOf(fetchFn.mock.calls[0]?.[1], 'x-grok-client-version'))).toMatch(
      /^\d+\.\d+\.\d+/
    )
    expect(headerOf(fetchFn.mock.calls[0]?.[1], 'x-grok-client-identifier')).toBe('evenfire')
    expect(headerOf(fetchFn.mock.calls[0]?.[1], 'x-xai-token-auth')).toBe('xai-grok-cli')
    expect(String(JSON.stringify(fetchFn.mock.calls[0]?.[1]?.headers))).not.toContain('grok-shell')
    expect(headerOf(fetchFn.mock.calls[0]?.[1], 'openai-beta')).toBeUndefined()
    expect(String(fetchFn.mock.calls[0]?.[1]?.body)).toContain('"store":false')
  })

  it('flushes a completed event that arrives without a trailing blank line', async () => {
    const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
      sseResponse([
        'data: {"type":"response.completed","response":{"usage":{"input_tokens":1,"output_tokens":1}}}',
      ])
    )
    const result = await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-1',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-residual',
        hostRef: 'research-host',
        model: 'gpt-5.1',
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-1',
      },
      redeem: async () => redeemSuccess(),
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-1',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    expect(result.outcome).toBe('success')
    expect(result.usage).toEqual({ inputTokens: 1, outputTokens: 1 })
  })

  it('parses CRLF-delimited SSE frames', async () => {
    const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
      sseResponse([
        'data: {"type":"response.completed","response":{"usage":{"input_tokens":4,"output_tokens":5}}}\r\n\r\n',
      ])
    )
    const result = await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-1',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-crlf',
        hostRef: 'research-host',
        model: 'gpt-5.1',
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-1',
      },
      redeem: async () => redeemSuccess(),
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-1',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    expect(result.outcome).toBe('success')
    expect(result.usage).toEqual({ inputTokens: 4, outputTokens: 5 })
  })

  it('maps upstream 402/403 to upstream_rejected', async () => {
    const fetchFn = vi.fn(
      async (_url: FetchInput, _init?: RequestInit) => new Response('paywall', { status: 403 })
    )
    await expect(
      streamGrokCompletion({
        maxDeadlineMs: 1_800_000,
        executionTicket: 'ticket-1',
        requestHash: REQUEST_HASH,
        request: REQUEST,
        ticket: {
          jti: 'jti-1',
          hostRef: 'research-host',
          model: 'gpt-5.1',
          requestHash: REQUEST_HASH,
          providerAttemptId: 'att-1',
        },
        redeem: async () => redeemSuccess({ accessToken: 'opaque-token' }),
        finalize: vi.fn(async () => ({
          providerAttemptId: 'att-1',
          outcome: 'error' as const,
          duplicate: false,
        })),
        fetchFn,
        lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      })
    ).rejects.toMatchObject({ code: 'upstream_rejected' })
  })

  it('rejects a mutated requestHash before redeem', async () => {
    const redeem = vi.fn()
    await expect(
      streamGrokCompletion({
        maxDeadlineMs: 1_800_000,
        executionTicket: 'ticket-1',
        requestHash: 'f'.repeat(64),
        request: REQUEST,
        ticket: {
          jti: 'jti-1',
          hostRef: 'research-host',
          model: 'gpt-5.1',
          requestHash: REQUEST_HASH,
          providerAttemptId: 'att-1',
        },
        redeem,
        finalize: vi.fn(),
        fetchFn: vi.fn(),
      })
    ).rejects.toMatchObject({
      code: 'request_hash_mismatch',
    } satisfies Partial<GrokTransportError>)
    expect(redeem).not.toHaveBeenCalled()
  })

  it('rejects a served-model mismatch and loopback redirects', async () => {
    await expect(
      streamGrokCompletion({
        maxDeadlineMs: 1_800_000,
        executionTicket: 'ticket-1',
        requestHash: REQUEST_HASH,
        request: REQUEST,
        ticket: {
          jti: 'jti-1',
          hostRef: 'research-host',
          model: 'gpt-5.1',
          requestHash: REQUEST_HASH,
          providerAttemptId: 'att-1',
        },
        redeem: async () =>
          redeemSuccess({ transport: { ...redeemSuccess().transport, servedModel: 'other' } }),
        finalize: vi.fn(async () => ({
          providerAttemptId: 'att-1',
          outcome: 'error' as const,
          duplicate: false,
        })),
        fetchFn: vi.fn(),
      })
    ).rejects.toMatchObject({ code: 'model_not_allowed' })

    const fetchFn = vi.fn(
      async () =>
        new Response(null, {
          status: 302,
          headers: { location: `https://${['127', '0', '0', '1'].join('.')}/steal` },
        })
    )
    await expect(
      streamGrokCompletion({
        maxDeadlineMs: 1_800_000,
        executionTicket: 'ticket-1',
        requestHash: REQUEST_HASH,
        request: REQUEST,
        ticket: {
          jti: 'jti-1',
          hostRef: 'research-host',
          model: 'gpt-5.1',
          requestHash: REQUEST_HASH,
          providerAttemptId: 'att-1',
        },
        redeem: async () => redeemSuccess(),
        finalize: vi.fn(async () => ({
          providerAttemptId: 'att-1',
          outcome: 'error' as const,
          duplicate: false,
        })),
        fetchFn,
        lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      })
    ).rejects.toMatchObject({ code: 'origin_denied' })
  })

  it('calls onRedeemed once, after a matching redeem and before the upstream fetch', async () => {
    const ticket = {
      jti: 'jti-redeemed',
      hostRef: 'research-host',
      model: REQUEST.model,
      requestHash: REQUEST_HASH,
      providerAttemptId: 'att-redeemed',
    }
    const finalize = vi.fn(async () => ({
      providerAttemptId: 'att-redeemed',
      outcome: 'success' as const,
      duplicate: false,
    }))
    const order: string[] = []
    await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-redeemed',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket,
      redeem: async () => {
        order.push('redeem')
        return redeemSuccess()
      },
      onRedeemed: () => order.push('onRedeemed'),
      finalize,
      fetchFn: vi.fn(async () => {
        order.push('fetch')
        return sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])
      }),
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    expect(order).toEqual(['redeem', 'onRedeemed', 'fetch'])

    // A denied redeem and a served-model mismatch never start the heartbeat.
    const denied = vi.fn(async (): Promise<RedeemAttemptSuccess> => {
      throw new Error('no_grant')
    })
    const mismatched = vi.fn(async () =>
      redeemSuccess({ transport: { ...redeemSuccess().transport, servedModel: 'other' } })
    )
    const onRedeemed = vi.fn()
    for (const redeem of [denied, mismatched]) {
      await expect(
        streamGrokCompletion({
          maxDeadlineMs: 1_800_000,
          executionTicket: 'ticket-redeemed',
          requestHash: REQUEST_HASH,
          request: REQUEST,
          ticket,
          redeem,
          onRedeemed,
          finalize,
          fetchFn: vi.fn(),
          lookup: async () => [{ address: '1.2.3.4', family: 4 }],
        })
      ).rejects.toThrow()
    }
    // Witness: both redeems ran, so the path that could call onRedeemed was entered.
    expect(denied).toHaveBeenCalledTimes(1)
    expect(mismatched).toHaveBeenCalledTimes(1)
    expect(onRedeemed).not.toHaveBeenCalled()
  })

  it('follows one frozen same-origin redirect then streams', async () => {
    const fetchFn = vi.fn(async () => {
      if (fetchFn.mock.calls.length === 1) {
        return new Response(null, {
          status: 307,
          headers: { location: GROK_COMPLETIONS_ORIGIN },
        })
      }
      return sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])
    })
    const result = await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-1',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-1',
        hostRef: 'research-host',
        model: 'gpt-5.1',
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-1',
      },
      redeem: async () => redeemSuccess(),
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-1',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    expect(result.outcome).toBe('success')
    expect(fetchFn).toHaveBeenCalledTimes(2)
  })

  it('maps upstream 401 to connection_unavailable instead of origin_denied', async () => {
    await expect(
      streamGrokCompletion({
        maxDeadlineMs: 1_800_000,
        executionTicket: 'ticket-1',
        requestHash: REQUEST_HASH,
        request: REQUEST,
        ticket: {
          jti: 'jti-1',
          hostRef: 'research-host',
          model: 'gpt-5.1',
          requestHash: REQUEST_HASH,
          providerAttemptId: 'att-1',
        },
        redeem: async () => redeemSuccess(),
        finalize: vi.fn(async () => ({
          providerAttemptId: 'att-1',
          outcome: 'error' as const,
          duplicate: false,
        })),
        fetchFn: vi.fn(
          async (_url: FetchInput, _init?: RequestInit) => new Response('denied', { status: 401 })
        ),
        lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      })
    ).rejects.toMatchObject({ code: 'connection_unavailable' })
  })

  it('finalizes canceled after the first frame and treats finalize 500 as idempotent retry', async () => {
    const abort = new AbortController()
    const finalize = vi
      .fn()
      .mockRejectedValueOnce(new Error('finalize 500'))
      .mockResolvedValueOnce({ providerAttemptId: 'att-1', outcome: 'canceled', duplicate: true })
    let upstreamCanceled = false
    const fetchFn = vi.fn(async () => {
      const stream = new ReadableStream({
        start(controller) {
          controller.enqueue(
            new TextEncoder().encode('data: {"type":"response.output_text.delta","delta":"x"}\n\n')
          )
          setTimeout(() => {
            if (!upstreamCanceled) controller.close()
          }, 20)
        },
        cancel() {
          upstreamCanceled = true
        },
      })
      return new Response(stream, { headers: { 'content-type': 'text/event-stream' } })
    })

    const frames: unknown[] = []
    const result = await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-1',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-1',
        hostRef: 'research-host',
        model: 'gpt-5.1',
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-1',
      },
      signal: abort.signal,
      redeem: async () => redeemSuccess(),
      finalize,
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      onFrame: frame => {
        frames.push(frame)
        abort.abort()
      },
    })
    expect(frames[0]).toEqual({ type: 'text', text: 'x' })
    expect(result.outcome).toBe('canceled')
    expect(upstreamCanceled).toBe(true)
    expect(finalize).toHaveBeenCalledTimes(2)
    expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('canceled')
  })

  it('does not reuse a redeemed access token across requests', async () => {
    const tokens: string[] = []
    const redeem = vi.fn(async (input: { executionTicket: string }) => {
      tokens.push(input.executionTicket)
      return redeemSuccess({ accessToken: accessTokenFor(input.executionTicket) })
    })
    const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
      sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])
    )
    const ticket = {
      jti: 'jti-1',
      hostRef: 'research-host',
      model: 'gpt-5.1',
      requestHash: REQUEST_HASH,
      providerAttemptId: 'att-1',
    }
    await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 't-a',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket,
      redeem,
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-1',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 't-b',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket,
      redeem,
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-1',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    expect(tokens).toEqual(['t-a', 't-b'])
    expect(String(headerOf(fetchFn.mock.calls[0]?.[1], 'authorization'))).toContain(
      accessTokenFor('t-a')
    )
    expect(String(headerOf(fetchFn.mock.calls[1]?.[1], 'authorization'))).toContain(
      accessTokenFor('t-b')
    )
  })

  it('does not retry after an ambiguous upstream response', async () => {
    const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
      sseResponse(['data: {"type":"response.output_text.delta","delta":"partial"}\n\n'])
    )
    const result = await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-1',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-1',
        hostRef: 'research-host',
        model: 'gpt-5.1',
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-1',
      },
      redeem: async () => redeemSuccess(),
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-1',
        outcome: 'unknown' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    expect(result.outcome).toBe('unknown')
    expect(fetchFn).toHaveBeenCalledOnce()
  })

  it('maps system, tool history, and generation hints into the Responses payload', async () => {
    const request = {
      schemaVersion: 'grok-completion-request.v1' as const,
      requestId: 'req-001',
      idempotencyKey: 'idem-001',
      provider: 'grok-subscription' as const,
      model: 'gpt-5.1',
      messages: [
        { role: 'system' as const, content: 'be brief' },
        { role: 'user' as const, content: 'hello' },
        {
          role: 'assistant' as const,
          content: '',
          toolCalls: [{ id: 'call-1', name: 'echo', arguments: { x: 1 } }],
        },
        { role: 'tool' as const, content: 'ok', toolCallId: 'call-1' },
      ],
      tools: Array.from({ length: 250 }, (_, index) => ({
        name: index === 0 ? 'echo' : `approved_tool_${index}`,
        description: `Approved tool ${index}`,
        parameters: { type: 'object', properties: { x: { type: 'integer' } } },
      })),
      generation: { toolChoice: 'auto' as const },
      transportHints: { promptCacheKey: 'sess-1' },
    }
    const requestHash = hashGrokCompletionRequest(request)
    const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
      sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])
    )
    await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-1',
      requestHash,
      request,
      ticket: {
        jti: 'jti-1',
        hostRef: 'research-host',
        model: 'gpt-5.1',
        requestHash,
        providerAttemptId: 'att-1',
      },
      redeem: async () => redeemSuccess(),
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-1',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    const body = JSON.parse(String(fetchFn.mock.calls[0]?.[1]?.body)) as Record<string, unknown>
    expect(body.instructions).toBe('be brief')
    expect(body.store).toBe(false)
    expect(body.tools).toEqual(
      request.tools.map(tool => ({ type: 'function', ...tool, strict: false }))
    )
    expect(body.parallel_tool_calls).toBe(true)
    expect(body.tool_choice).toBe('auto')
    expect(body.prompt_cache_key).toBe('sess-1')
    expect(body.input).toEqual([
      { role: 'user', content: 'hello' },
      { type: 'function_call', call_id: 'call-1', name: 'echo', arguments: '{"x":1}' },
      { type: 'function_call_output', call_id: 'call-1', output: 'ok' },
    ])
    expect(body).not.toHaveProperty('max_output_tokens')
    expect(body).not.toHaveProperty('temperature')
  })

  it.each([
    { name: 'service.with.dots__read', mode: 'alias' },
    { name: `generated_server_${'x'.repeat(140)}__read`, mode: 'alias' },
    { name: '工具@read record', mode: 'alias' },
    { name: 'history@removed tool', mode: 'history-only' },
    { name: 'service.with.dots__read', mode: 'canonical-echo' },
  ])('roundtrips $name ($mode) without changing authorization identity', async ({ name, mode }) => {
    const request = {
      ...REQUEST,
      ...(mode === 'history-only'
        ? {}
        : { tools: [{ name, description: 'Read one record', parameters: { type: 'object' } }] }),
      messages: [
        ...REQUEST.messages,
        {
          role: 'assistant' as const,
          content: '',
          toolCalls: [{ id: 'previous', name, arguments: {} }],
        },
        { role: 'tool' as const, content: 'previous result', toolCallId: 'previous', name },
      ],
    }
    const requestHash = hashGrokCompletionRequest(request)
    expect(parseGrokCompletionRequestV1(request).ok).toBe(true)
    const original = structuredClone(request)
    const emitted: unknown[] = []
    const fetchFn = vi.fn(async (_url: unknown, init?: RequestInit) => {
      const body = JSON.parse(String(init?.body))
      const wireName = body.input.find(
        (item: { type?: string }) => item.type === 'function_call'
      ).name
      if (mode === 'history-only') expect(body.tools).toBeUndefined()
      else expect(body.tools[0].name).toBe(wireName)
      expect(wireName).toMatch(/^[A-Za-z0-9_-]{1,64}$/)
      expect(wireName).not.toBe(name)
      expect(body.input.find((item: { type?: string }) => item.type === 'function_call').name).toBe(
        wireName
      )
      expect(
        body.input.find((item: { type?: string }) => item.type === 'function_call_output')
      ).toEqual({
        type: 'function_call_output',
        call_id: 'previous',
        output: 'previous result',
      })
      return sseResponse([
        `data: ${JSON.stringify({
          type: 'response.output_item.done',
          item: {
            type: 'function_call',
            id: 'item-new',
            call_id: 'new-call',
            name: mode === 'canonical-echo' ? name : wireName,
            arguments: '{}',
          },
        })}\n\n`,
        'data: {"type":"response.completed","response":{"usage":{}}}\n\n',
      ])
    })
    const result = await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-name-map',
      requestHash,
      request,
      ticket: {
        jti: 'name-map',
        hostRef: 'research-host',
        model: request.model,
        requestHash,
        providerAttemptId: 'att-name-map',
      },
      redeem: async () => redeemSuccess(),
      finalize: async () => ({
        providerAttemptId: 'att-name-map',
        outcome: 'success' as const,
        duplicate: false,
      }),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      onFrame: frame => {
        emitted.push(frame)
      },
    })
    expect(result.outcome).toBe('success')
    expect(emitted).toContainEqual({ type: 'tool_call', id: 'new-call', name, arguments: {} })
    expect(request).toEqual(original)
    expect(hashGrokCompletionRequest(request)).toBe(requestHash)
  })

  it('rejects an unregistered alias in a completed stream before emitting any calls', async () => {
    const emitted: unknown[] = []
    const finalize = vi.fn(async () => ({
      providerAttemptId: 'att-unknown-alias',
      outcome: 'error' as const,
      duplicate: false,
    }))
    const request = {
      ...REQUEST,
      tools: [{ name: 'known.read', description: 'Read a record', parameters: { type: 'object' } }],
    }
    const requestHash = hashGrokCompletionRequest(request)
    const fetchFn = vi.fn(async (_url: unknown, init?: RequestInit) => {
      const body = JSON.parse(String(init?.body))
      return sseResponse([
        ...[body.tools[0].name, '__grok_tool_unregistered'].map(
          (name, index) =>
            `data: ${JSON.stringify({
              type: 'response.output_item.done',
              item: { type: 'function_call', id: `call-${index}`, name, arguments: '{}' },
            })}\n\n`
        ),
        'data: {"type":"response.completed"}\n\n',
      ])
    })
    await expect(
      streamGrokCompletion({
        maxDeadlineMs: 1_800_000,
        executionTicket: 'ticket-unknown-alias',
        requestHash,
        request,
        ticket: {
          jti: 'unknown-alias',
          hostRef: 'research-host',
          model: request.model,
          requestHash,
          providerAttemptId: 'att-unknown-alias',
        },
        redeem: async () => redeemSuccess(),
        finalize,
        fetchFn,
        lookup: async () => [{ address: '1.2.3.4', family: 4 }],
        onFrame: frame => {
          emitted.push(frame)
        },
      })
    ).rejects.toMatchObject({
      code: 'provider_unavailable',
      message: 'upstream returned an unknown tool name',
    })
    expect(fetchFn).toHaveBeenCalledOnce()
    expect(emitted).toEqual([])
    expect(finalize).toHaveBeenCalledWith(
      expect.objectContaining({
        receipt: expect.objectContaining({ outcome: 'error' }),
      })
    )
  })

  it('omits Notify-like generation fields on the Responses wire', async () => {
    const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
      sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])
    )
    await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-1',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-1',
        hostRef: 'research-host',
        model: 'gpt-5.1',
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-1',
      },
      redeem: async () => redeemSuccess(),
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-1',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    const body = JSON.parse(String(fetchFn.mock.calls[0]?.[1]?.body)) as Record<string, unknown>
    expect(body.instructions).toBeUndefined()
    expect(body).not.toHaveProperty('max_output_tokens')
    expect(body).not.toHaveProperty('temperature')
    expect(body.store).toBe(false)
    expect(body.input).toEqual([{ role: 'user', content: 'hello' }])
  })

  it('keeps analysis instructions, binds max_output_tokens, and withholds probe-gated temperature', async () => {
    const request = {
      ...REQUEST,
      messages: [
        { role: 'system' as const, content: 'review this repository' },
        { role: 'user' as const, content: 'findings' },
      ],
      generation: { maxOutputTokens: 4096, temperature: 0.2 },
    }
    const requestHash = hashGrokCompletionRequest(request)
    const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
      sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])
    )
    await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-1',
      requestHash,
      request,
      ticket: {
        jti: 'jti-1',
        hostRef: 'research-host',
        model: 'gpt-5.1',
        requestHash,
        providerAttemptId: 'att-1',
      },
      redeem: async () => redeemSuccess(),
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-1',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    })
    const body = JSON.parse(String(fetchFn.mock.calls[0]?.[1]?.body)) as Record<string, unknown>
    expect(body.instructions).toBe('review this repository')
    expect(body.input).toEqual([{ role: 'user', content: 'findings' }])
    expect(body.max_output_tokens).toBe(4096)
    // B-M10: temperature stays in the authorize hash but is not sent upstream
    // until a live SuperGrok probe confirms /v1/responses accepts it.
    expect(GROK_UPSTREAM_TEMPERATURE_PROBE_CONFIRMED).toBe(false)
    expect(body).not.toHaveProperty('temperature')
    expect(hashGrokCompletionRequest(request)).toBe(requestHash)
    expect(body).not.toHaveProperty('text')
    expect(body).not.toHaveProperty('service_tier')
  })

  it('maps upstream 400 to invalid_request and finalizes error without usage', async () => {
    const finalize = vi.fn(
      async (_input: Parameters<StreamGrokCompletionInput['finalize']>[0]) => ({
        providerAttemptId: 'att-1',
        outcome: 'error' as const,
        duplicate: false,
      })
    )
    await expect(
      streamGrokCompletion({
        maxDeadlineMs: 1_800_000,
        executionTicket: 'ticket-1',
        requestHash: REQUEST_HASH,
        request: REQUEST,
        ticket: {
          jti: 'jti-1',
          hostRef: 'research-host',
          model: 'gpt-5.1',
          requestHash: REQUEST_HASH,
          providerAttemptId: 'att-1',
        },
        redeem: async () => redeemSuccess(),
        finalize,
        fetchFn: vi.fn(
          async (_url: FetchInput, _init?: RequestInit) =>
            new Response('bad request', { status: 400 })
        ),
        lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      })
    ).rejects.toMatchObject({ code: 'invalid_request' })
    expect(finalize).toHaveBeenCalledWith(
      expect.objectContaining({
        receipt: expect.objectContaining({ outcome: 'error' }),
      })
    )
    expect(finalize.mock.calls[0]?.[0]?.receipt?.usage).toBeUndefined()
  })

  // R10 (M1): the Grok upstream refuses a prompt over the model's context window
  // with an HTTP 400 before any stream starts. This body is the one recorded on
  // 2026-09-23 against cli-chat-proxy.grok.com/v1/responses with grok-4.6: the
  // `code` field is the generic `invalid-argument`, and the only specific marker
  // is the bracketed `[input_too_large]` inside the `error` string.
  describe('non-success upstream bodies (R10)', () => {
    const recordedOverflowBody = JSON.stringify({
      code: 'invalid-argument',
      error:
        "Failed to start sampling: [input_too_large] The prompt is too long for this model's context window (1107771 tokens > 500000 tokens)",
    })

    function streamWith(response: () => Response) {
      const finalize = vi.fn(
        async (_input: Parameters<StreamGrokCompletionInput['finalize']>[0]) => ({
          providerAttemptId: 'att-1',
          outcome: 'error' as const,
          duplicate: false,
        })
      )
      const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) => response())
      const pending = streamGrokCompletion({
        executionTicket: 'ticket-1',
        requestHash: REQUEST_HASH,
        request: REQUEST,
        ticket: {
          jti: 'jti-1',
          hostRef: 'research-host',
          model: 'gpt-5.1',
          requestHash: REQUEST_HASH,
          providerAttemptId: 'att-1',
        },
        maxDeadlineMs: 300_000,
        redeem: async () => redeemSuccess(),
        finalize,
        fetchFn,
        lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      })
      return { pending, finalize, fetchFn }
    }

    it('T-R10-1a maps the recorded HTTP 400 input_too_large body to context_length_exceeded', async () => {
      const { pending, finalize, fetchFn } = streamWith(
        () =>
          new Response(recordedOverflowBody, {
            status: 400,
            headers: { 'content-type': 'application/json' },
          })
      )
      await expect(pending).rejects.toMatchObject({ code: 'context_length_exceeded' })
      expect(fetchFn).toHaveBeenCalledTimes(1)
      expect(finalize).toHaveBeenCalledWith(
        expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
      )
    })

    it('T-R10-1b keeps the status mapping for every other error body', async () => {
      const other = JSON.stringify({ code: 'invalid-argument', error: 'unsupported parameter' })
      await expect(
        streamWith(() => new Response(other, { status: 400 })).pending
      ).rejects.toMatchObject({ code: 'invalid_request' })
      await expect(
        streamWith(() => new Response('bad request', { status: 400 })).pending
      ).rejects.toMatchObject({ code: 'invalid_request' })
      await expect(
        streamWith(() => new Response('{"error":', { status: 400 })).pending
      ).rejects.toMatchObject({ code: 'invalid_request' })
      // The marker counts only inside the `error` string, not anywhere in the body.
      await expect(
        streamWith(
          () =>
            new Response(JSON.stringify({ code: '[input_too_large]', error: 'other' }), {
              status: 400,
            })
        ).pending
      ).rejects.toMatchObject({ code: 'invalid_request' })
      await expect(
        streamWith(() => new Response(recordedOverflowBody, { status: 401 })).pending
      ).rejects.toMatchObject({ code: 'connection_unavailable' })
      await expect(
        streamWith(() => new Response(other, { status: 503 })).pending
      ).rejects.toMatchObject({ code: 'provider_unavailable' })
    })

    it('T-R10-1c reads a bounded prefix of an endless error body and keeps the status mapping', async () => {
      const chunk = new TextEncoder().encode(`{"pad":"${'x'.repeat(1024)}`)
      let pulled = 0
      let canceled = false
      // highWaterMark 0: nothing is pulled until a reader asks for it.
      const endless = new ReadableStream<Uint8Array>(
        {
          pull(controller) {
            pulled += chunk.byteLength
            controller.enqueue(chunk)
          },
          cancel() {
            canceled = true
          },
        },
        { highWaterMark: 0 }
      )
      const { pending } = streamWith(() => new Response(endless, { status: 400 }))
      await expect(pending).rejects.toMatchObject({ code: 'invalid_request' })
      // Witness: the body was read, and the read stopped at a bound.
      expect(pulled).toBeGreaterThan(0)
      expect(canceled).toBe(true)
      expect(pulled).toBeLessThan(1024 * 1024)
    })

    // G1-1 (#720): an upstream 429 is a rate limit. Retry-After travels only as
    // whole seconds in 1..3600, the rule gfsClient already applies; any other
    // value is treated as absent.
    it('G1-1a maps an upstream 429 to rate_limited and carries a valid Retry-After', async () => {
      const { pending, finalize, fetchFn } = streamWith(
        () => new Response('slow down', { status: 429, headers: { 'retry-after': '7' } })
      )
      await expect(pending).rejects.toMatchObject({
        code: 'rate_limited',
        details: { retryAfterSeconds: 7 },
      })
      expect(fetchFn).toHaveBeenCalledTimes(1)
      expect(finalize).toHaveBeenCalledWith(
        expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
      )
    })

    it('G1-1b treats a missing Retry-After, or one outside 1..3600 whole seconds, as absent', async () => {
      const values: (string | undefined)[] = [
        undefined,
        '0',
        '3601',
        '1.5',
        '-3',
        '07',
        'Wed, 21 Oct 2026 07:28:00 GMT',
        '',
      ]
      for (const value of values) {
        const headers: Record<string, string> = value === undefined ? {} : { 'retry-after': value }
        const err = await streamWith(
          () => new Response('slow down', { status: 429, headers })
        ).pending.catch((caught: unknown) => caught)
        // Witness: the 429 arm ran for this value.
        expect(err, `retry-after=${JSON.stringify(value)}`).toMatchObject({ code: 'rate_limited' })
        const details = (err as { details?: Record<string, unknown> }).details
        expect(details?.retryAfterSeconds, `retry-after=${JSON.stringify(value)}`).toBeUndefined()
      }
    })

    // Review round 2 L7: both ends of 1..3600 are valid.
    it('G1-1d carries a Retry-After of exactly 1 or 3600 seconds', async () => {
      for (const seconds of [1, 3600]) {
        const err = await streamWith(
          () =>
            new Response('slow down', { status: 429, headers: { 'retry-after': String(seconds) } })
        ).pending.catch((caught: unknown) => caught)
        expect(err, `retry-after=${seconds}`).toMatchObject({
          code: 'rate_limited',
          details: { retryAfterSeconds: seconds },
        })
      }
    })

    it('G1-1c keeps an upstream 503 as provider_unavailable, Retry-After or not', async () => {
      const err = await streamWith(
        () => new Response('busy', { status: 503, headers: { 'retry-after': '7' } })
      ).pending.catch((caught: unknown) => caught)
      expect(err).toMatchObject({ code: 'provider_unavailable' })
      const details = (err as { details?: Record<string, unknown> }).details
      expect(details?.retryAfterSeconds).toBeUndefined()
    })

    // G1-3 (#720): the same request gets the same 4xx every time, so it is not
    // an outage to retry or fail over from. 402/403 is an entitlement refusal.
    it.each([402, 403, 404, 409, 422])(
      'G1-3a maps an unmapped upstream %i to upstream_rejected',
      async (status) => {
        const err = await streamWith(() => new Response('rejected', { status })).pending.catch(
          (caught: unknown) => caught
        )
        expect(err).toMatchObject({ code: 'upstream_rejected', details: { upstreamStatus: status } })
        expect((err as Error).message).toContain(String(status))
      }
    )

    it.each([408, 500, 502, 503, 504])(
      'G1-3b keeps an upstream %i as provider_unavailable',
      async (status) => {
        await expect(
          streamWith(() => new Response('busy', { status })).pending
        ).rejects.toMatchObject({ code: 'provider_unavailable' })
      }
    )
  })

  it('emits a tool call only after argument deltas complete', async () => {
    const frames: unknown[] = []
    const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
      sseResponse([
        'data: {"type":"response.output_item.added","item":{"type":"function_call","id":"item-1","call_id":"call-9","name":"lookup","arguments":""}}\n\n',
        'data: {"type":"response.function_call_arguments.delta","item_id":"item-1","delta":"{\\"q\\":"}}\n\n',
        'data: {"type":"response.function_call_arguments.delta","item_id":"item-1","delta":"\\"x\\"}"}\n\n',
        'data: {"type":"response.function_call_arguments.done","item_id":"item-1","arguments":"{\\"q\\":\\"x\\"}"}\n\n',
        'data: {"type":"response.completed","response":{"usage":{}}}\n\n',
      ])
    )
    await streamGrokCompletion({
      maxDeadlineMs: 1_800_000,
      executionTicket: 'ticket-1',
      requestHash: REQUEST_HASH,
      request: REQUEST,
      ticket: {
        jti: 'jti-1',
        hostRef: 'research-host',
        model: 'gpt-5.1',
        requestHash: REQUEST_HASH,
        providerAttemptId: 'att-1',
      },
      redeem: async () => redeemSuccess(),
      finalize: vi.fn(async () => ({
        providerAttemptId: 'att-1',
        outcome: 'success' as const,
        duplicate: false,
      })),
      fetchFn,
      lookup: async () => [{ address: '1.2.3.4', family: 4 }],
      onFrame: frame => {
        frames.push(frame)
      },
    })
    expect(frames).toEqual([
      { type: 'tool_call', id: 'call-9', name: 'lookup', arguments: { q: 'x' } },
    ])
  })

  describe('malformed tool-call arguments fail closed', () => {
    const sse = (event: Record<string, unknown>) => `data: ${JSON.stringify(event)}\n\n`
    const textBefore = sse({ type: 'response.output_text.delta', delta: 'before' })
    const completed = sse({ type: 'response.completed', response: { usage: {} } })
    const openCall = sse({
      type: 'response.output_item.added',
      item: {
        type: 'function_call',
        id: 'item-1',
        call_id: 'call-9',
        name: 'lookup',
        arguments: '',
      },
    })
    const truncatedDelta = sse({
      type: 'response.function_call_arguments.delta',
      item_id: 'item-1',
      delta: '{"q":',
    })

    async function runUpstream(events: string[], options: { abortOnFrame?: boolean } = {}) {
      const frames: Array<{ type: string }> = []
      const abort = new AbortController()
      const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) => sseResponse(events))
      const finalize = vi.fn(
        async (input: Parameters<StreamGrokCompletionInput['finalize']>[0]) => ({
          providerAttemptId: 'att-1',
          outcome: input.receipt.outcome,
          duplicate: false,
        })
      )
      const settled = await streamGrokCompletion({
        executionTicket: 'ticket-1',
        requestHash: REQUEST_HASH,
        request: REQUEST,
        ticket: {
          jti: 'jti-1',
          hostRef: 'research-host',
          model: 'gpt-5.1',
          requestHash: REQUEST_HASH,
          providerAttemptId: 'att-1',
        },
        signal: abort.signal,
        maxDeadlineMs: 300_000,
        redeem: async () => redeemSuccess(),
        finalize,
        fetchFn,
        lookup: async () => [{ address: '1.2.3.4', family: 4 }],
        onFrame: frame => {
          frames.push(frame)
          if (options.abortOnFrame) abort.abort()
        },
      }).then(
        result => ({ rejected: false as const, result }),
        (error: unknown) => ({ rejected: true as const, error })
      )
      return { settled, frames, fetchFn, finalize }
    }

    async function expectRefused(events: string[]) {
      const { settled, frames, fetchFn, finalize } = await runUpstream(events)
      // Liveness: the upstream was called and its stream was consumed far enough
      // to deliver the text that precedes the call. Without these, "no tool_call
      // frame" below would also hold for a transport that never ran.
      expect(fetchFn).toHaveBeenCalledTimes(1)
      expect(frames).toContainEqual({ type: 'text', text: 'before' })
      // The defect: the malformed call became an executable call with `{}`.
      expect(frames.filter(frame => frame.type === 'tool_call')).toEqual([])
      expect(settled).toMatchObject({
        rejected: true,
        error: { name: 'GrokTransportError', code: 'invalid_tool_arguments' },
      })
      expect(finalize).toHaveBeenCalledTimes(1)
      expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('error')
    }

    it.each([
      ['truncated JSON', '{"q":'],
      ['a JSON value that is not an object', '[1,2]'],
    ])(
      'refuses a function_call closed by response.output_item.done with %s as arguments',
      async (_label, rawArguments) => {
        await expectRefused([
          textBefore,
          sse({
            type: 'response.output_item.done',
            item: {
              type: 'function_call',
              id: 'item-1',
              call_id: 'call-9',
              name: 'lookup',
              arguments: rawArguments,
            },
          }),
          completed,
        ])
      }
    )

    it('refuses a call whose argument deltas never complete before the stream ends', async () => {
      await expectRefused([textBefore, openCall, truncatedDelta, completed])
    })

    // A closed call is the whole call, so empty `arguments` there mean "no
    // parameters", not "truncated". An unclosed call gives no such guarantee.
    async function expectAcceptedWithoutParameters(events: string[]) {
      const { settled, frames, fetchFn, finalize } = await runUpstream(events)
      expect(fetchFn).toHaveBeenCalledTimes(1)
      expect(settled).toMatchObject({ rejected: false, result: { outcome: 'success' } })
      // Exactly one call, with no parameters: the positive frame is the witness.
      expect(frames).toEqual([
        { type: 'text', text: 'before' },
        { type: 'tool_call', id: 'call-9', name: 'lookup', arguments: {} },
      ])
      expect(finalize).toHaveBeenCalledTimes(1)
      expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('success')
    }

    it.each([
      ['an empty string', ''],
      ['a whitespace-only string', ' \n '],
    ])(
      'accepts a function_call closed by response.output_item.done with %s as arguments as a call with no parameters',
      async (_label, rawArguments) => {
        await expectAcceptedWithoutParameters([
          textBefore,
          sse({
            type: 'response.output_item.done',
            item: {
              type: 'function_call',
              id: 'item-1',
              call_id: 'call-9',
              name: 'lookup',
              arguments: rawArguments,
            },
          }),
          completed,
        ])
      }
    )

    it('accepts a call opened with empty arguments and closed by response.function_call_arguments.done with none', async () => {
      await expectAcceptedWithoutParameters([
        textBefore,
        openCall,
        sse({ type: 'response.function_call_arguments.done', item_id: 'item-1', arguments: '' }),
        completed,
      ])
    })

    it.each([
      ['an empty string', ''],
      ['a whitespace-only string', ' \n '],
    ])(
      'refuses a call whose truncated deltas are closed by a done event with %s as arguments',
      async (_label, rawArguments) => {
        await expectRefused([
          textBefore,
          openCall,
          truncatedDelta,
          sse({
            type: 'response.output_item.done',
            item: {
              type: 'function_call',
              id: 'item-1',
              call_id: 'call-9',
              name: 'lookup',
              arguments: rawArguments,
            },
          }),
          completed,
        ])
      }
    )

    // R9-9 (L-12): the positive twin of the case above. A blank close carries
    // nothing new, so complete deltas survive it and the call runs with them.
    const closeWith: Record<string, (rawArguments: string) => string> = {
      'response.output_item.done': rawArguments =>
        sse({
          type: 'response.output_item.done',
          item: {
            type: 'function_call',
            id: 'item-1',
            call_id: 'call-9',
            name: 'lookup',
            arguments: rawArguments,
          },
        }),
      'response.function_call_arguments.done': rawArguments =>
        sse({
          type: 'response.function_call_arguments.done',
          item_id: 'item-1',
          arguments: rawArguments,
        }),
    }
    it.each([
      ['response.output_item.done', 'an empty string', ''],
      ['response.output_item.done', 'a whitespace-only string', ' \n '],
      ['response.function_call_arguments.done', 'an empty string', ''],
      ['response.function_call_arguments.done', 'a whitespace-only string', ' \n '],
    ])(
      'T-R9-9a-grok keeps complete argument deltas when %s closes the call with %s',
      async (event, _label, rawArguments) => {
        const { settled, frames, fetchFn, finalize } = await runUpstream([
          textBefore,
          openCall,
          truncatedDelta,
          sse({ type: 'response.function_call_arguments.delta', item_id: 'item-1', delta: '"x"}' }),
          closeWith[event]!(rawArguments),
          completed,
        ])
        expect(fetchFn).toHaveBeenCalledTimes(1)
        expect(settled).toMatchObject({ rejected: false, result: { outcome: 'success' } })
        expect(frames).toEqual([
          { type: 'text', text: 'before' },
          { type: 'tool_call', id: 'call-9', name: 'lookup', arguments: { q: 'x' } },
        ])
        expect(finalize).toHaveBeenCalledTimes(1)
        expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('success')
      }
    )

    it('refuses a call opened with empty arguments and never closed before the stream ends', async () => {
      await expectRefused([textBefore, openCall, completed])
    })

    // R17-1: a stream that ends with no terminal event never delivers its calls,
    // so a call left open there is not parsed and the attempt stays `unknown`,
    // as on dev. Refusing its arguments would report a dropped connection as a
    // malformed model response.
    it.each([
      ['empty', [openCall]],
      ['truncated', [openCall, truncatedDelta]],
    ])(
      'T-R17-1-grok reports a stream that ends with no terminal event and a call open with %s arguments as unknown',
      async (_label, callEvents) => {
        const { settled, frames, fetchFn, finalize } = await runUpstream([textBefore, ...callEvents])
        expect(fetchFn).toHaveBeenCalledTimes(1)
        expect(frames).toEqual([{ type: 'text', text: 'before' }])
        expect(settled).toMatchObject({ rejected: false, result: { outcome: 'unknown' } })
        expect(finalize).toHaveBeenCalledTimes(1)
        expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('unknown')
      }
    )

    // A canceled or failed stream also ends with its open call truncated. That
    // truncation is a consequence of the stream's own outcome, so the outcome
    // wins over the arguments refusal.
    it('keeps a client cancel that lands mid-arguments as canceled', async () => {
      const { settled, frames, fetchFn, finalize } = await runUpstream(
        [openCall, truncatedDelta, textBefore],
        { abortOnFrame: true }
      )
      expect(fetchFn).toHaveBeenCalledTimes(1)
      expect(frames).toEqual([{ type: 'text', text: 'before' }])
      expect(settled).toMatchObject({ rejected: false, result: { outcome: 'canceled' } })
      expect(finalize).toHaveBeenCalledTimes(1)
      expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('canceled')
    })

    it('reports an upstream failure that lands mid-arguments as provider_unavailable', async () => {
      const { settled, frames, fetchFn, finalize } = await runUpstream([
        textBefore,
        openCall,
        truncatedDelta,
        sse({ type: 'response.failed', response: {} }),
      ])
      expect(fetchFn).toHaveBeenCalledTimes(1)
      expect(frames).toEqual([{ type: 'text', text: 'before' }])
      expect(settled).toMatchObject({
        rejected: true,
        error: { name: 'GrokTransportError', code: 'provider_unavailable' },
      })
      expect(finalize).toHaveBeenCalledTimes(1)
      expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('error')
    })
  })
})

const TEXT_DELTA = 'data: {"type":"response.output_text.delta","delta":"x"}\n\n'
const COMPLETED = 'data: {"type":"response.completed","response":{"usage":{}}}\n\n'

/**
 * An upstream body that ignores `init.signal`, like a socket that stays open
 * while sending nothing. Only a transport that races each read against its own
 * timers can end an attempt reading from it.
 */
function upstreamBody(chunks: Array<{ afterMs: number; text: string } | 'stall'>): {
  fetchFn: typeof fetch
  cancel: ReturnType<typeof vi.fn>
} {
  const encoder = new TextEncoder()
  const cancel = vi.fn()
  const fetchFn = vi.fn(async () => {
    let index = 0
    const body = new ReadableStream<Uint8Array>(
      {
        async pull(controller) {
          const next = chunks[index]
          index += 1
          if (next === undefined) {
            controller.close()
            return
          }
          if (next === 'stall') {
            await new Promise<never>(() => undefined)
            return
          }
          await new Promise(resolve => setTimeout(resolve, next.afterMs))
          controller.enqueue(encoder.encode(next.text))
        },
        cancel,
      },
      { highWaterMark: 0 }
    )
    return new Response(body, { status: 200, headers: { 'content-type': 'text/event-stream' } })
  }) as unknown as typeof fetch
  return { fetchFn, cancel }
}

function attemptInput(
  overrides: Partial<StreamGrokCompletionInput>
): StreamGrokCompletionInput & { finalize: ReturnType<typeof vi.fn> } {
  const finalize = vi.fn(async (_input: Parameters<StreamGrokCompletionInput['finalize']>[0]) => ({
    providerAttemptId: 'att-timeouts',
    outcome: 'error' as const,
    duplicate: false,
  }))
  return {
    executionTicket: 'ticket-timeouts',
    requestHash: REQUEST_HASH,
    request: REQUEST,
    ticket: {
      jti: 'jti-timeouts',
      hostRef: 'research-host',
      model: REQUEST.model,
      requestHash: REQUEST_HASH,
      providerAttemptId: 'att-timeouts',
    },
    maxDeadlineMs: 300_000,
    redeem: async () => redeemSuccess(),
    finalize,
    fetchFn: upstreamBody([]).fetchFn,
    lookup: async () => [{ address: '1.2.3.4', family: 4 }],
    ...overrides,
  } as StreamGrokCompletionInput & { finalize: ReturnType<typeof vi.fn> }
}

function finalizedOutcome(finalize: ReturnType<typeof vi.fn>): unknown {
  expect(finalize).toHaveBeenCalledTimes(1)
  return (finalize.mock.calls[0]?.[0] as { receipt: { outcome: string } }).receipt.outcome
}

describe('streamGrokCompletion upstream timeouts', () => {
  it('ends a stalled upstream stream at the idle timeout with a typed provider_unavailable', async () => {
    const upstream = upstreamBody([{ afterMs: 0, text: TEXT_DELTA }, 'stall'])
    const input = attemptInput({ fetchFn: upstream.fetchFn, upstreamIdleTimeoutMs: 50 })
    const frames: unknown[] = []
    const error = await streamGrokCompletion({ ...input, onFrame: frame => void frames.push(frame) }).then(
      () => undefined,
      (err: unknown) => err
    )
    expect(frames).toEqual([{ type: 'text', text: 'x' }])
    expect(error).toBeInstanceOf(GrokTransportError)
    expect(error).toMatchObject({
      code: 'provider_unavailable',
      message: 'upstream stream idle timeout',
      details: { idleTimeoutMs: 50 },
    })
    expect(finalizedOutcome(input.finalize)).toBe('error')
    expect(upstream.cancel).toHaveBeenCalledTimes(1)
  }, 2_000)

  it('keeps the attempt alive while upstream chunks arrive inside the idle window', async () => {
    const chunks = Array.from({ length: 6 }, () => ({ afterMs: 30, text: TEXT_DELTA }))
    const upstream = upstreamBody([...chunks, { afterMs: 30, text: COMPLETED }])
    const input = attemptInput({ fetchFn: upstream.fetchFn, upstreamIdleTimeoutMs: 50 })
    const result = await streamGrokCompletion(input)
    expect(result.outcome).toBe('success')
    expect(finalizedOutcome(input.finalize)).toBe('success')
  }, 2_000)

  it('ends a still-active upstream stream at the total cap with stream_duration_exceeded', async () => {
    const endless = Array.from({ length: 200 }, () => ({ afterMs: 10, text: TEXT_DELTA }))
    const upstream = upstreamBody(endless)
    const input = attemptInput({
      fetchFn: upstream.fetchFn,
      maxDeadlineMs: 150,
      upstreamIdleTimeoutMs: 1_000,
    })
    const error = await streamGrokCompletion(input).then(
      () => undefined,
      (err: unknown) => err
    )
    expect(error).toBeInstanceOf(GrokTransportError)
    expect(error).toMatchObject({
      code: 'stream_duration_exceeded',
      message: 'upstream stream exceeded maxStreamDurationMs',
      details: { limitMs: 150 },
    })
    expect(finalizedOutcome(input.finalize)).toBe('error')
  }, 2_000)

  it('reports a client abort during a stalled stream as canceled, not as a timeout', async () => {
    const upstream = upstreamBody([{ afterMs: 0, text: TEXT_DELTA }, 'stall'])
    const abort = new AbortController()
    const input = attemptInput({
      fetchFn: upstream.fetchFn,
      signal: abort.signal,
      maxDeadlineMs: 1_000,
      upstreamIdleTimeoutMs: 1_000,
    })
    setTimeout(() => abort.abort(), 50)
    const result = await streamGrokCompletion(input)
    expect(result.outcome).toBe('canceled')
    expect(finalizedOutcome(input.finalize)).toBe('canceled')
    expect(upstream.cancel).toHaveBeenCalledTimes(1)
  }, 2_000)
})

describe('streamGrokCompletion deadline validation', () => {
  // The redeem consumes the single-use ticket, so a deadline that cannot be
  // served must be refused before it, while there is no receipt to finalize.
  it('rejects an invalid deadline before redeeming the ticket', async () => {
    const redeem = vi.fn(async () => redeemSuccess())
    const fetchFn = vi.fn()
    const input = attemptInput({
      deadlineMs: 0,
      redeem,
      fetchFn: fetchFn as unknown as typeof fetch,
    })
    // Witness: the rejection names the deadline check, so the path ran.
    await expect(streamGrokCompletion(input)).rejects.toMatchObject({
      name: 'RequestLimitError',
      message: 'deadline is invalid',
    })
    expect(redeem).not.toHaveBeenCalled()
    expect(input.finalize).not.toHaveBeenCalled()
    expect(fetchFn).not.toHaveBeenCalled()
  })
})