Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

40.8 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from 'vitest';

import { AskUserBridge } from '../askUser';
import {
  CodexAppServerConnectionError,
  CodexAppServerRpcError,
  isCodexAppServerCompatibilityError,
} from './CodexAppServerClient';
import { CodexThreadSession } from './CodexThreadSession';

const turn = (id: string, status: 'completed' | 'inProgress' | 'interrupted') => ({
  completedAt: status === 'inProgress' ? null : 2,
  durationMs: status === 'inProgress' ? null : 1000,
  error: null,
  id,
  items: [],
  itemsView: 'full',
  startedAt: 1,
  status,
});

interface ClientHarness {
  client: any;
  disconnect: () => void;
  notify: (method: string, params: unknown) => Promise<void> | void;
  registeredResumeParams: () => unknown;
  releaseConsumer: ReturnType<typeof vi.fn>;
  requests: Array<{ method: string; params: unknown }>;
  resolveThreadStart: () => void;
  resolveTurnStart: () => void;
  resume: (model?: string) => Promise<void> | void;
  serverRequest: (method: string, params: unknown) => Promise<unknown>;
}

const createClientHarness = (
  options: {
    autoComplete?: boolean;
    delayThreadStart?: boolean;
    delayTurnStart?: boolean;
    disconnectOnInterrupt?: boolean;
    connectError?: Error;
    failResume?: boolean;
    initialThreadId?: string;
    interruptError?: Error;
    malformedThreadStart?: boolean;
    /** Statuses returned by successive `thread/resume` calls; later calls report idle. */
    resumeStatuses?: Array<'active' | 'idle' | 'systemError'>;
    threadNameError?: Error;
  } = {},
): ClientHarness => {
  let disconnectHandler: (() => void) | undefined;
  let notificationHandler: ((method: string, params: unknown) => Promise<void>) | undefined;
  let serverRequestHandler: ((method: string, params: unknown) => unknown) | undefined;
  let registration:
    | {
        onResume: (response: unknown) => Promise<void> | void;
        onResumeError: (error: Error) => Promise<void> | void;
      }
    | undefined;
  let resumeParams: unknown;
  let turnSequence = 0;
  const releaseConsumer = vi.fn();
  const requests: Array<{ method: string; params: unknown }> = [];
  const notify = (method: string, params: unknown) => notificationHandler?.(method, params);
  let resolveThreadStart = () => {};
  const threadStartGate = options.delayThreadStart
    ? new Promise<void>((resolve) => {
        resolveThreadStart = resolve;
      })
    : Promise.resolve();
  let resolveTurnStart = () => {};
  const turnStartGate = options.delayTurnStart
    ? new Promise<void>((resolve) => {
        resolveTurnStart = resolve;
      })
    : Promise.resolve();

  const client = {
    acquireConsumer: vi.fn(() => releaseConsumer),
    acquireThread: vi.fn(() => vi.fn()),
    connect: options.connectError
      ? vi.fn().mockRejectedValue(options.connectError)
      : vi.fn().mockResolvedValue({ userAgent: 'codex-test' }),
    onDisconnect: vi.fn((handler: () => void) => {
      disconnectHandler = handler;
      return vi.fn();
    }),
    onRawMessage: vi.fn(() => vi.fn()),
    onStderr: vi.fn(() => vi.fn()),
    registerThread: vi.fn((_threadId: string, params: unknown, value: typeof registration) => {
      resumeParams = params;
      registration = value;
      return vi.fn(() => {
        if (registration === value) registration = undefined;
      });
    }),
    request: vi.fn(async (method: string, params: unknown) => {
      requests.push({ method, params });
      if (method === 'thread/start') {
        await threadStartGate;
        if (options.malformedThreadStart) return { thread: {} };
        return { model: 'gpt-5.5-codex', thread: { id: 'thread-1' } };
      }
      if (method === 'thread/resume') {
        if (options.failResume) throw new Error('Thread not found');
        return {
          model: 'gpt-5.5-codex',
          thread: {
            id: options.initialThreadId ?? 'thread-1',
            status: { type: options.resumeStatuses?.shift() ?? 'idle' },
          },
        };
      }
      if (method === 'thread/name/set') {
        if (options.threadNameError) throw options.threadNameError;
        return {};
      }
      if (method === 'turn/start') {
        await turnStartGate;
        const turnId = `turn-${++turnSequence}`;
        if (options.autoComplete !== false) {
          setTimeout(() => {
            void notify('turn/started', {
              threadId: options.initialThreadId ?? 'thread-1',
              turn: turn(turnId, 'inProgress'),
            });
            void notify('item/agentMessage/delta', {
              delta: `answer-${turnSequence}`,
              itemId: `message-${turnSequence}`,
              threadId: options.initialThreadId ?? 'thread-1',
              turnId,
            });
            void notify('turn/completed', {
              threadId: options.initialThreadId ?? 'thread-1',
              turn: turn(turnId, 'completed'),
            });
          }, 0);
        }
        return { turn: turn(turnId, 'inProgress') };
      }
      if (method === 'turn/interrupt') {
        if (options.disconnectOnInterrupt) disconnectHandler?.();
        if (options.interruptError) throw options.interruptError;
        const { threadId, turnId } = params as { threadId: string; turnId: string };
        setTimeout(() => {
          void notify('turn/completed', {
            threadId,
            turn: turn(turnId, 'interrupted'),
          });
        }, 0);
        return {};
      }
    }),
    subscribe: vi.fn((_threadId: string, handler: typeof notificationHandler) => {
      notificationHandler = handler;
      return vi.fn();
    }),
    subscribeServerRequests: vi.fn((_threadId, handler) => {
      serverRequestHandler = handler;
      return vi.fn();
    }),
  };

  return {
    client,
    disconnect: () => disconnectHandler?.(),
    notify,
    registeredResumeParams: () => resumeParams,
    releaseConsumer,
    requests,
    resolveThreadStart,
    resolveTurnStart,
    resume: (model = 'gpt-5.5-codex') =>
      registration?.onResume({ model, thread: { id: options.initialThreadId ?? 'thread-1' } }),
    serverRequest: async (method, params) => serverRequestHandler?.(method, params),
  };
};

const createSession = (
  harness: ClientHarness,
  options: {
    ephemeral?: boolean;
    initialThreadId?: string;
    onEventsError?: Error;
    threadName?: string;
  } = {},
) => {
  const events: any[] = [];
  const statuses: string[] = [];
  const onSessionId = vi.fn();
  const session = new CodexThreadSession({
    client: harness.client,
    initialThreadId: options.initialThreadId,
    threadName: options.threadName,
    onEvents: (batch) => {
      if (options.onEventsError) throw options.onEventsError;
      events.push(...batch);
    },
    onRuntimeStatus: ({ state }) => statuses.push(state),
    onSessionId,
    sessionId: 'session-1',
    threadParams: {
      approvalPolicy: 'never',
      cwd: '/workspace',
      ...(options.ephemeral ? { ephemeral: true } : {}),
      sandbox: 'danger-full-access',
    },
  });
  const run = (operationId: string, text: string) =>
    session.run({
      input: [{ text, text_elements: [], type: 'text' }],
      onRawMessage: vi.fn(),
      operationId,
    });
  return { events, onSessionId, run, session, statuses };
};

describe('CodexThreadSession', () => {
  describe('steer', () => {
    const input = [{ text: 'also run the tests', text_elements: [], type: 'text' as const }];
    const startTurn = async () => {
      const harness = createClientHarness({ autoComplete: false });
      const ctx = createSession(harness);
      const running = ctx.run('op-1', 'Fix it');
      await vi.waitFor(() =>
        expect(harness.requests.some((r) => r.method === 'turn/start')).toBe(true),
      );
      const finish = async () => {
        await harness.notify('turn/completed', {
          threadId: 'thread-1',
          turn: turn('turn-1', 'completed'),
        });
        await running;
        ctx.session.close();
      };
      return { ...ctx, finish, harness };
    };

    it('adds the message to the running turn it started', async () => {
      const { finish, harness, session } = await startTurn();

      await expect(session.steer(input, 'steer-1')).resolves.toBe('accepted');

      expect(harness.requests.at(-1)).toEqual({
        method: 'turn/steer',
        params: {
          clientUserMessageId: 'steer-1',
          expectedTurnId: 'turn-1',
          input,
          threadId: 'thread-1',
        },
      });
      await finish();
    });

    it('leaves the message to a turn of its own when the turn refuses it', async () => {
      const { finish, harness, session } = await startTurn();
      const request = harness.client.request.getMockImplementation();
      harness.client.request.mockImplementation(async (method: string, params: unknown) => {
        if (method === 'turn/steer') {
          throw new CodexAppServerRpcError('not steerable', -32600, {
            codexErrorInfo: { activeTurnNotSteerable: { turnKind: 'review' } },
          });
        }
        return request!(method, params);
      });

      await expect(session.steer(input, 'steer-1')).resolves.toBe('rejected');
      await finish();
    });

    it('refuses with no turn running', async () => {
      const harness = createClientHarness();
      const { session } = createSession(harness);

      await expect(session.steer(input, 'steer-1')).resolves.toBe('rejected');
      expect(harness.requests).toEqual([]);
    });
  });

  describe('native user input', () => {
    const contexts: Array<{ bridge: AskUserBridge; session: CodexThreadSession }> = [];
    afterEach(() => {
      for (const { bridge, session } of contexts.splice(0)) {
        bridge.cancelAll();
        session.close();
      }
    });
    const question = {
      header: 'Database',
      id: 'database',
      isOther: true,
      isSecret: false,
      options: [{ description: 'Preserve existing data', label: 'Keep' }],
      question: 'Keep the database?',
    };
    const request = (questions = [question]) => ({
      autoResolutionMs: null,
      isBlocking: true,
      itemId: 'question-1',
      questions,
      threadId: 'thread-1',
      turnId: 'turn-1',
    });
    const start = async () => {
      const harness = createClientHarness({ autoComplete: false });
      const { events, session } = createSession(harness);
      const bridge = new AskUserBridge('op-question', { identifier: 'codex', provider: 'codex' });
      contexts.push({ bridge, session });
      const running = session.run({
        askUserBridge: bridge,
        input: [{ text: 'Ask me', text_elements: [], type: 'text' }],
        onRawMessage: vi.fn(),
        operationId: 'op-question',
      });
      await vi.waitFor(() =>
        expect(harness.requests.some((r) => r.method === 'turn/start')).toBe(true),
      );
      return { bridge, events, harness, running, session };
    };
    const finish = async (ctx: Awaited<ReturnType<typeof start>>) => {
      await ctx.harness.notify('turn/completed', {
        threadId: 'thread-1',
        turn: turn('turn-1', 'completed'),
      });
      await ctx.running;
      ctx.session.close();
    };

    const announceAsyncQuestion = (ctx: Awaited<ReturnType<typeof start>>) =>
      ctx.harness.notify('item/completed', {
        item: {
          delivery: 'async',
          id: 'async-1',
          questions: [{ options: ['Staging', 'Production'], title: 'Which environment?' }],
          text: 'Which environment?\n- Staging\n- Production',
          type: 'agentMessage',
        },
        threadId: 'thread-1',
        turnId: 'turn-1',
      });

    it('keeps an async question actionable after completion and continues the original thread', async () => {
      const ctx = await start();
      await announceAsyncQuestion(ctx);
      expect(ctx.bridge.pendingCount).toBe(1);
      expect((await ctx.bridge.events()[Symbol.asyncIterator]().next()).value).toMatchObject({
        data: { apiName: 'askUserQuestion', identifier: 'codex', toolCallId: 'async-1' },
        type: 'agent_intervention_request',
      });
      await ctx.harness.notify('turn/completed', {
        threadId: 'thread-1',
        turn: turn('turn-1', 'completed'),
      });
      expect(ctx.bridge.pendingCount).toBe(1);
      expect(ctx.events.some(({ type }) => type === 'agent_runtime_end')).toBe(false);

      ctx.bridge.resolve('async-1', { result: { 'Which environment?': 'Staging' } });
      await vi.waitFor(() => {
        expect(ctx.harness.requests.filter(({ method }) => method === 'turn/start')).toHaveLength(
          2,
        );
      });
      expect(ctx.harness.requests.at(-1)).toMatchObject({
        method: 'turn/start',
        params: {
          input: [{ text: expect.stringContaining('"answer":"Staging"'), type: 'text' }],
          threadId: 'thread-1',
        },
      });
      expect(JSON.stringify(ctx.harness.requests.at(-1))).toContain('request_user_input_async');
      await ctx.harness.notify('turn/started', {
        threadId: 'thread-1',
        turn: turn('turn-2', 'inProgress'),
      });
      await ctx.harness.notify('turn/completed', {
        threadId: 'thread-1',
        turn: turn('turn-2', 'completed'),
      });
      await ctx.running;
      expect(ctx.events.filter(({ type }) => type === 'agent_runtime_end')).toHaveLength(1);
      expect(ctx.events).toContainEqual(
        expect.objectContaining({
          data: expect.objectContaining({ newStep: true }),
          type: 'stream_start',
        }),
      );
    });

    it('stops waiting on an async question without starting another native turn when skipped', async () => {
      const ctx = await start();
      await announceAsyncQuestion(ctx);
      expect(ctx.bridge.pendingCount).toBe(1);
      await ctx.harness.notify('turn/completed', {
        threadId: 'thread-1',
        turn: turn('turn-1', 'completed'),
      });
      ctx.bridge.resolve('async-1', { cancelled: true, cancelReason: 'user_cancelled' });
      await ctx.running;
      expect(ctx.harness.requests.filter(({ method }) => method === 'turn/start')).toHaveLength(1);
      expect(ctx.events.filter(({ type }) => type === 'agent_runtime_end')).toHaveLength(1);
      ctx.session.close();
    });

    it('stops waiting without interrupting a native turn that has already completed', async () => {
      const ctx = await start();
      await announceAsyncQuestion(ctx);
      await ctx.harness.notify('turn/completed', {
        threadId: 'thread-1',
        turn: turn('turn-1', 'completed'),
      });
      await ctx.session.interrupt();
      await ctx.running;
      expect(ctx.bridge.pendingCount).toBe(0);
      expect(ctx.harness.requests.filter(({ method }) => method === 'turn/start')).toHaveLength(1);
      expect(ctx.harness.requests.some(({ method }) => method === 'turn/interrupt')).toBe(false);
      expect(ctx.events.filter(({ type }) => type === 'agent_runtime_end')).toHaveLength(1);
      ctx.session.close();
    });

    it('presents native questions and returns selected/custom answers by their original ids', async () => {
      const ctx = await start();
      const reply = ctx.harness.serverRequest(
        'item/tool/requestUserInput',
        request([question, { ...question, id: 'name', options: null, question: 'Database name?' }]),
      );
      await vi.waitFor(() => expect(ctx.bridge.pendingCount).toBe(1));
      const intervention = await ctx.bridge.events()[Symbol.asyncIterator]().next();
      expect(intervention.value).toMatchObject({
        data: {
          apiName: 'askUserQuestion',
          identifier: 'codex',
          provider: 'codex',
          toolCallId: 'question-1',
        },
        operationId: 'op-question',
        type: 'agent_intervention_request',
      });
      ctx.bridge.resolve('question-1', {
        result: { 'Keep the database?': 'Keep', 'Database name?': 'archive' },
      });
      await expect(reply).resolves.toEqual({
        answers: { database: { answers: ['Keep'] }, name: { answers: ['archive'] } },
      });
      expect(ctx.events).toContainEqual(
        expect.objectContaining({
          type: 'tool_result',
          data: expect.objectContaining({
            pluginState: {
              askUserAnswers: { 'Keep the database?': 'Keep', 'Database name?': 'archive' },
            },
            toolCallId: 'question-1',
          }),
        }),
      );
      await finish(ctx);
    });

    it.each(['user_cancelled', 'timeout'] as const)(
      'resolves %s without inventing an answer',
      async (cancelReason) => {
        const ctx = await start();
        const reply = ctx.harness.serverRequest('item/tool/requestUserInput', request());
        await vi.waitFor(() => expect(ctx.bridge.pendingCount).toBe(1));
        ctx.bridge.resolve('question-1', { cancelled: true, cancelReason });
        await expect(reply).resolves.toEqual({ answers: {} });
        await finish(ctx);
      },
    );

    it('maps whole-form free text and supplementary notes without losing them', async () => {
      const ctx = await start();
      const reply = ctx.harness.serverRequest('item/tool/requestUserInput', request());
      await vi.waitFor(() => expect(ctx.bridge.pendingCount).toBe(1));
      ctx.bridge.resolve('question-1', {
        result: { __freeform__: 'Use a snapshot', __supplement__: 'Read only' },
      });
      await expect(reply).resolves.toEqual({
        answers: { database: { answers: ['Use a snapshot', 'Read only'] } },
      });
      await finish(ctx);
    });

    it('releases a pending question on interruption', async () => {
      const ctx = await start();
      const reply = ctx.harness.serverRequest('item/tool/requestUserInput', request());
      await vi.waitFor(() => expect(ctx.bridge.pendingCount).toBe(1));
      await ctx.session.interrupt();
      await expect(reply).resolves.toEqual({ answers: {} });
      await ctx.running;
      expect(ctx.bridge.pendingCount).toBe(0);
      ctx.session.close();
    });

    it('uses the native deadline and clears a timed-out question', async () => {
      const ctx = await start();
      vi.useFakeTimers();
      try {
        const reply = ctx.harness.serverRequest('item/tool/requestUserInput', {
          ...request(),
          autoResolutionMs: 100,
        });
        await vi.advanceTimersByTimeAsync(0);
        expect(ctx.bridge.pendingCount).toBe(1);
        await vi.advanceTimersByTimeAsync(100);
        await expect(reply).resolves.toEqual({ answers: {} });
        expect(ctx.bridge.pendingCount).toBe(0);
        const stream = ctx.bridge.events()[Symbol.asyncIterator]();
        await stream.next();
        expect((await stream.next()).value).toMatchObject({
          data: { cancelled: true, cancelReason: 'timeout' },
          type: 'agent_intervention_response',
        });
        await finish(ctx);
      } finally {
        vi.useRealTimers();
        ctx.session.close();
      }
    });

    it.each(['disconnect', 'close', 'complete'] as const)(
      'clears questions when the turn ends via %s',
      async (end) => {
        const ctx = await start();
        const reply = ctx.harness.serverRequest('item/tool/requestUserInput', request());
        await vi.waitFor(() => expect(ctx.bridge.pendingCount).toBe(1));
        if (end === 'disconnect') ctx.harness.disconnect();
        else if (end === 'close') ctx.session.close();
        else
          await ctx.harness.notify('turn/completed', {
            threadId: 'thread-1',
            turn: turn('turn-1', 'completed'),
          });
        await expect(reply).resolves.toEqual({ answers: {} });
        await ctx.running;
        expect(ctx.bridge.pendingCount).toBe(0);
        ctx.session.close();
      },
    );

    it('rejects secret or ambiguous questions before persisting them', async () => {
      const ctx = await start();
      await expect(
        ctx.harness.serverRequest(
          'item/tool/requestUserInput',
          request([{ ...question, isSecret: true }]),
        ),
      ).rejects.toThrow();
      await expect(
        ctx.harness.serverRequest('item/tool/requestUserInput', request([question, question])),
      ).rejects.toThrow();
      expect(ctx.bridge.pendingCount).toBe(0);
      expect(ctx.events.some((event) => event.type === 'tool_start')).toBe(false);
      await finish(ctx);
    });

    it('rejects a request from another turn without displaying a question', async () => {
      const ctx = await start();
      await expect(
        ctx.harness.serverRequest('item/tool/requestUserInput', {
          ...request(),
          turnId: 'old-turn',
        }),
      ).rejects.toThrow();
      expect(ctx.bridge.pendingCount).toBe(0);
      await finish(ctx);
    });

    it('leaves the operation bridge usable when startup fails before a turn begins', async () => {
      const harness = createClientHarness({ connectError: new Error('unavailable') });
      const { session } = createSession(harness);
      const bridge = new AskUserBridge('retry-operation', {
        identifier: 'codex',
        provider: 'codex',
      });
      await expect(
        session.run({
          askUserBridge: bridge,
          input: [],
          onRawMessage: vi.fn(),
          operationId: 'retry-operation',
        }),
      ).rejects.toThrow('unavailable');
      const answer = bridge.pending({ arguments: {}, toolCallId: 'retry-question' });
      expect(bridge.pendingCount).toBe(1);
      bridge.cancelAll();
      await expect(answer).resolves.toMatchObject({ cancelled: true });
      session.close();
    });
  });

  const provenance = (operationId: string) => ({
    LOBEHUB_AGENT_ID: 'agent',
    LOBEHUB_OPERATION_ID: operationId,
    LOBEHUB_TOPIC_ID: 'topic',
  });
  const runWithProvenance = (session: CodexThreadSession, operationId: string) =>
    session.run({
      input: [{ text: 'hello', text_elements: [], type: 'text' }],
      onRawMessage: () => {},
      operationId,
      provenance: provenance(operationId),
    });

  it('unsubscribes and resumes the loaded thread with the current run provenance', async () => {
    const harness = createClientHarness();
    const { session } = createSession(harness);

    try {
      await runWithProvenance(session, 'op-a');
      await runWithProvenance(session, 'op-b');

      const threadRequests = harness.requests.filter(({ method }) =>
        ['thread/start', 'thread/unsubscribe', 'thread/resume'].includes(method),
      );
      expect(threadRequests.map(({ method }) => method)).toEqual([
        'thread/start',
        'thread/unsubscribe',
        'thread/resume',
      ]);
      expect(threadRequests.at(-1)?.params).toMatchObject({
        config: {
          shell_environment_policy: {
            set: { LOBEHUB_OPERATION_ID: 'op-b', LOBEHUB_TOPIC_ID: 'topic' },
          },
        },
      });
    } finally {
      session.close();
    }
  });

  it('replaces queued reconnect params when the next run has new provenance', async () => {
    const harness = createClientHarness();
    const { session } = createSession(harness);

    try {
      await runWithProvenance(session, 'old');
      harness.disconnect();
      harness.client.connect.mockImplementationOnce(async () => {
        await harness.resume();
      });
      await runWithProvenance(session, 'new');

      const resume = harness.requests.filter(({ method }) => method === 'thread/resume');
      expect(resume.at(-1)?.params).toMatchObject({
        config: { shell_environment_policy: { set: { LOBEHUB_OPERATION_ID: 'new' } } },
      });
      expect(harness.registeredResumeParams()).toMatchObject({
        config: { shell_environment_policy: { set: { LOBEHUB_OPERATION_ID: 'new' } } },
      });
    } finally {
      session.close();
    }
  });

  it('does not resume after closing during an unsubscribe request', async () => {
    const harness = createClientHarness({ initialThreadId: 'existing' });
    const { session } = createSession(harness, { initialThreadId: 'existing' });
    let release!: () => void;
    const gate = new Promise<void>((resolve) => {
      release = resolve;
    });
    const request = harness.client.request.getMockImplementation();
    harness.client.request.mockImplementation(async (method: string, params: unknown) => {
      if (method === 'thread/unsubscribe') await gate;
      return request(method, params);
    });

    const run = runWithProvenance(session, 'old');
    await vi.waitFor(() =>
      expect(harness.client.request).toHaveBeenCalledWith('thread/unsubscribe', {
        threadId: 'existing',
      }),
    );
    session.close();
    release();
    await run;

    expect(harness.requests.map(({ method }) => method)).toEqual(['thread/unsubscribe']);
  });

  it('reports a claimed native thread as a run error instead of failing construction', async () => {
    const harness = createClientHarness({ initialThreadId: 'thread-existing' });
    harness.client.acquireThread.mockImplementation(() => {
      throw new Error('Codex thread already has an active session: thread-existing');
    });
    const { session, statuses } = createSession(harness, { initialThreadId: 'thread-existing' });

    await expect(runWithProvenance(session, 'op-a')).rejects.toThrow(
      'already has an active session',
    );
    expect(statuses.at(-1)).toBe('error');
    expect(harness.requests).toEqual([]);
    session.close();
  });

  it('waits for a turn interrupted by a closed session to settle before resuming', async () => {
    const harness = createClientHarness({
      initialThreadId: 'thread-existing',
      resumeStatuses: ['active', 'active'],
    });
    const { session } = createSession(harness, { initialThreadId: 'thread-existing' });

    try {
      await runWithProvenance(session, 'op-next');
      expect(harness.requests.map(({ method }) => method)).toEqual([
        'thread/unsubscribe',
        'thread/resume',
        'thread/unsubscribe',
        'thread/resume',
        'thread/unsubscribe',
        'thread/resume',
        'turn/start',
      ]);
    } finally {
      session.close();
    }
  });

  it('gives up when the previous turn never settles', async () => {
    vi.useFakeTimers();
    const harness = createClientHarness({
      initialThreadId: 'thread-existing',
      resumeStatuses: Array.from({ length: 100 }, () => 'active' as const),
    });
    const { session } = createSession(harness, { initialThreadId: 'thread-existing' });

    try {
      const run = runWithProvenance(session, 'op-next');
      const assertion = expect(run).rejects.toThrow('still finishing the previous turn');
      await vi.advanceTimersByTimeAsync(11_000);
      await assertion;
      expect(session.canFallbackToExec).toBe(false);
      expect(harness.requests.some(({ method }) => method === 'turn/start')).toBe(false);
    } finally {
      session.close();
      vi.useRealTimers();
    }
  });

  it('runs on a resumed thread that reports a non-active status', async () => {
    const harness = createClientHarness({
      initialThreadId: 'thread-existing',
      resumeStatuses: ['systemError'],
    });
    const { session } = createSession(harness, { initialThreadId: 'thread-existing' });

    try {
      await runWithProvenance(session, 'op-a');
      expect(harness.requests.map(({ method }) => method)).toEqual([
        'thread/unsubscribe',
        'thread/resume',
        'turn/start',
      ]);
    } finally {
      session.close();
    }
  });

  // ROOT CAUSE:
  // A connected device resumes a native thread without the rollout usage that Desktop
  // reads locally. Without a baseline, the first turn reported the thread's entire
  // cumulative usage as its own. Rebase from the first native total/last pair instead.
  /** @example A resumed device turn reports only its own native steps. */
  it('rebases usage on a resumed thread that has no saved cumulative usage', async () => {
    const harness = createClientHarness({ autoComplete: false, initialThreadId: 'thread-1' });
    const { events, session } = createSession(harness, { initialThreadId: 'thread-1' });
    const run = session.run({
      input: [{ text: 'continue', text_elements: [], type: 'text' }],
      operationId: 'resumed-usage',
      onRawMessage: vi.fn(),
    });
    await vi.waitFor(() => {
      /** @example Usage is observed only after the resumed turn starts. */
      expect(harness.requests.some((request) => request.method === 'turn/start')).toBe(true);
    });
    await harness.notify('turn/started', {
      threadId: 'thread-1',
      turn: turn('turn-1', 'inProgress'),
    });
    for (const [total, last] of [
      [350, 250],
      [370, 20],
    ]) {
      const usage = (tokens: number) => ({
        cachedInputTokens: 0,
        inputTokens: tokens,
        outputTokens: 0,
        reasoningOutputTokens: 0,
        totalTokens: tokens,
      });
      await harness.notify('thread/tokenUsage/updated', {
        threadId: 'thread-1',
        tokenUsage: { last: usage(last), total: usage(total) },
        turnId: 'turn-1',
      });
    }
    await harness.notify('turn/completed', {
      threadId: 'thread-1',
      turn: turn('turn-1', 'completed'),
    });
    await run;
    session.close();
    /** @example Both new steps count once; the 100 earlier thread tokens are excluded. */
    expect(events).toContainEqual(
      expect.objectContaining({
        data: expect.objectContaining({
          usage: expect.objectContaining({ totalInputTokens: 270 }),
        }),
        type: 'step_complete',
      }),
    );
  });

  // ROOT CAUSE:
  // stream_start carried the thread id as `sessionId` even for `--ephemeral` threads, so the
  // server saved it as the topic resume binding and the next turn resumed an ephemeral thread.
  /** @example An ephemeral native thread is never reported as a resumable session. */
  it('does not report an ephemeral thread as the stream session', async () => {
    const { events, onSessionId, run, session } = createSession(createClientHarness(), {
      ephemeral: true,
    });
    await run('ephemeral-op', 'hello');
    session.close();
    const streamStart = events.find((event) => event.type === 'stream_start');
    /** @example The turn still starts, without a session id the server would persist. */
    expect(streamStart?.data).not.toHaveProperty('sessionId');
    expect(onSessionId).not.toHaveBeenCalled();
  });

  it('sets the original prompt as the name of a new persisted thread', async () => {
    const harness = createClientHarness();
    const { run, session } = createSession(harness, { threadName: 'Original prompt title' });

    await run('operation-1', 'workspace-enriched input');
    session.close();

    expect(harness.requests.slice(0, 3)).toEqual([
      {
        method: 'thread/start',
        params: {
          approvalPolicy: 'never',
          cwd: '/workspace',
          sandbox: 'danger-full-access',
        },
      },
      {
        method: 'thread/name/set',
        params: { name: 'Original prompt title', threadId: 'thread-1' },
      },
      {
        method: 'turn/start',
        params: {
          input: [{ text: 'workspace-enriched input', text_elements: [], type: 'text' }],
          threadId: 'thread-1',
        },
      },
    ]);
  });

  it('does not block the first turn when thread naming is unsupported', async () => {
    const harness = createClientHarness({ threadNameError: new Error('Method not found') });
    const { run, session } = createSession(harness, { threadName: 'Original prompt title' });
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});

    try {
      await expect(run('operation-1', 'workspace-enriched input')).resolves.toBeUndefined();
      expect(harness.requests.map(({ method }) => method)).toEqual([
        'thread/start',
        'thread/name/set',
        'turn/start',
      ]);
    } finally {
      warn.mockRestore();
      session.close();
    }
  });

  it('reuses one native thread across multiple turns', async () => {
    const harness = createClientHarness();
    const { events, onSessionId, run, session, statuses } = createSession(harness);

    await run('operation-1', 'first');
    await run('operation-2', 'second');
    session.close();

    expect(harness.requests.map(({ method }) => method)).toEqual([
      'thread/start',
      'turn/start',
      'turn/start',
    ]);
    expect(harness.client.onRawMessage).toHaveBeenCalledWith('thread-1', expect.any(Function));
    expect(onSessionId).toHaveBeenCalledOnce();
    expect(statuses).toEqual([
      'starting',
      'running',
      'idle',
      'starting',
      'running',
      'idle',
      'closed',
    ]);
    expect(
      events
        .filter(({ data, type }) => type === 'stream_chunk' && data.chunkType === 'text')
        .map(({ operationId, data }) => ({ content: data.content, operationId })),
    ).toEqual([
      { content: 'answer-1', operationId: 'operation-1' },
      { content: 'answer-2', operationId: 'operation-2' },
    ]);
  });

  it('resumes an existing thread before starting its first native turn', async () => {
    const harness = createClientHarness({ initialThreadId: 'thread-existing' });
    const { onSessionId, run, session } = createSession(harness, {
      initialThreadId: 'thread-existing',
      threadName: 'Do not rename existing thread',
    });

    await run('operation-1', 'continue');
    session.close();

    expect(harness.requests.slice(0, 2)).toEqual([
      {
        method: 'thread/resume',
        params: {
          approvalPolicy: 'never',
          cwd: '/workspace',
          sandbox: 'danger-full-access',
          threadId: 'thread-existing',
        },
      },
      {
        method: 'turn/start',
        params: {
          input: [{ text: 'continue', text_elements: [], type: 'text' }],
          threadId: 'thread-existing',
        },
      },
    ]);
    expect(onSessionId).not.toHaveBeenCalled();
    expect(harness.registeredResumeParams()).toEqual({
      approvalPolicy: 'never',
      cwd: '/workspace',
      sandbox: 'danger-full-access',
      threadId: 'thread-existing',
    });
  });

  it('never allows exec fallback for an existing thread, including initialize failures', async () => {
    const initializeError = new CodexAppServerConnectionError('Initialize failed', {
      phase: 'initialize',
    });
    const harness = createClientHarness({
      connectError: initializeError,
      initialThreadId: 'thread-existing',
    });
    const { run, session } = createSession(harness, { initialThreadId: 'thread-existing' });

    await expect(run('operation-1', 'continue')).rejects.toBe(initializeError);

    expect(session.canFallbackToExec).toBe(false);
    session.close();
  });

  it('does not allow exec fallback after initialize succeeds for an existing thread', async () => {
    const harness = createClientHarness({
      failResume: true,
      initialThreadId: 'thread-existing',
    });
    const { run, session } = createSession(harness, { initialThreadId: 'thread-existing' });

    await expect(run('operation-1', 'continue')).rejects.toThrow('Thread not found');

    expect(session.canFallbackToExec).toBe(false);
    session.close();
  });

  it('classifies a malformed initial thread/start response for safe exec fallback', async () => {
    const harness = createClientHarness({ malformedThreadStart: true });
    const { run, session } = createSession(harness);

    const error = await run('operation-1', 'start').catch((cause) => cause);

    expect(error).toBeInstanceOf(CodexAppServerConnectionError);
    expect(isCodexAppServerCompatibilityError(error)).toBe(true);
    expect(session.canFallbackToExec).toBe(true);
    session.close();
  });

  it('does not register a thread if the host closes while thread/start is pending', async () => {
    const harness = createClientHarness({ delayThreadStart: true });
    const { run, session, statuses } = createSession(harness);
    const running = run('operation-1', 'wait');
    await vi.waitFor(() =>
      expect(harness.requests.some(({ method }) => method === 'thread/start')).toBe(true),
    );

    session.close();
    harness.resolveThreadStart();
    await running;

    expect(harness.client.registerThread).not.toHaveBeenCalled();
    expect(harness.releaseConsumer).toHaveBeenCalledOnce();
    expect(statuses).toEqual(['starting', 'closed']);
  });

  it('interrupts the active turn through turn/interrupt', async () => {
    const harness = createClientHarness({ autoComplete: false });
    const { events, run, session } = createSession(harness);

    const running = run('operation-1', 'wait');
    await vi.waitFor(() =>
      expect(harness.requests.some(({ method }) => method === 'turn/start')).toBe(true),
    );
    await session.interrupt();
    await running;
    session.close();

    expect(harness.requests).toContainEqual({
      method: 'turn/interrupt',
      params: { threadId: 'thread-1', turnId: 'turn-1' },
    });
    expect(events).toContainEqual(
      expect.objectContaining({
        data: { reason: 'interrupted' },
        type: 'agent_runtime_end',
      }),
    );
  });

  it('keeps the thread reusable when interrupt loses a race with transport disconnect', async () => {
    const harness = createClientHarness({
      autoComplete: false,
      disconnectOnInterrupt: true,
      interruptError: new CodexAppServerConnectionError('Transport disconnected'),
    });
    const { run, session } = createSession(harness);
    const interrupted = run('operation-1', 'wait');
    await vi.waitFor(() =>
      expect(harness.requests.some(({ method }) => method === 'turn/start')).toBe(true),
    );

    await expect(session.interrupt()).resolves.toBeUndefined();
    await interrupted;

    const nextTurn = run('operation-2', 'continue');
    await vi.waitFor(() =>
      expect(harness.requests.filter(({ method }) => method === 'turn/start')).toHaveLength(2),
    );
    harness.disconnect();
    await nextTurn;
    session.close();
  });

  it('does not suppress a genuine interrupt RPC error when transport also disconnects', async () => {
    const rpcError = new CodexAppServerRpcError(
      'Turn cannot be interrupted',
      -32_602,
      undefined,
      'turn/interrupt',
    );
    const harness = createClientHarness({
      autoComplete: false,
      disconnectOnInterrupt: true,
      interruptError: rpcError,
    });
    const { run, session } = createSession(harness);
    const interrupted = run('operation-1', 'wait');
    await vi.waitFor(() =>
      expect(harness.requests.some(({ method }) => method === 'turn/start')).toBe(true),
    );

    await expect(session.interrupt()).rejects.toBe(rpcError);
    await interrupted;
    session.close();
  });

  it('remembers an interrupt requested before turn/start returns its turn id', async () => {
    const harness = createClientHarness({ autoComplete: false, delayTurnStart: true });
    const { run, session } = createSession(harness);

    const running = run('operation-1', 'wait');
    await vi.waitFor(() =>
      expect(harness.requests.some(({ method }) => method === 'turn/start')).toBe(true),
    );
    await session.interrupt();
    harness.resolveTurnStart();
    await running;
    session.close();

    expect(harness.requests).toContainEqual({
      method: 'turn/interrupt',
      params: { threadId: 'thread-1', turnId: 'turn-1' },
    });
  });

  it('does not turn a received completion into an interruption if the transport exits next', async () => {
    const harness = createClientHarness({ autoComplete: false });
    const { events, run, session } = createSession(harness);
    const running = run('operation-1', 'finish');
    await vi.waitFor(() =>
      expect(harness.requests.some(({ method }) => method === 'turn/start')).toBe(true),
    );

    const notification = harness.notify('turn/completed', {
      threadId: 'thread-1',
      turn: turn('turn-1', 'completed'),
    });
    harness.disconnect();
    await notification;
    await running;
    session.close();

    const terminalEvents = events.filter(({ type }) => type === 'agent_runtime_end');
    expect(terminalEvents).toHaveLength(1);
    expect(terminalEvents[0].data).toEqual({});
  });

  it('settles a crashed turn immediately as interrupted, then resumes on the next turn', async () => {
    const harness = createClientHarness({ autoComplete: false });
    const { events, run, session } = createSession(harness);
    let settled = false;

    const crashedTurn = run('operation-1', 'crash').finally(() => {
      settled = true;
    });
    await vi.waitFor(() =>
      expect(harness.requests.some(({ method }) => method === 'turn/start')).toBe(true),
    );
    harness.disconnect();
    await crashedTurn;
    expect(settled).toBe(true);
    expect(events).toContainEqual(
      expect.objectContaining({
        data: { reason: 'interrupted' },
        operationId: 'operation-1',
        type: 'agent_runtime_end',
      }),
    );

    const nextTurn = run('operation-2', 'continue');
    await vi.waitFor(() =>
      expect(harness.requests.filter(({ method }) => method === 'turn/start')).toHaveLength(2),
    );
    harness.disconnect();
    await nextTurn;
    session.close();
  });

  it('surfaces notification emission failures instead of completing a partial turn', async () => {
    const emissionError = new Error('Renderer event delivery failed');
    const harness = createClientHarness();
    const { run, session, statuses } = createSession(harness, { onEventsError: emissionError });

    await expect(run('operation-1', 'fail delivery')).rejects.toBe(emissionError);

    expect(statuses).toEqual(['starting', 'running', 'error']);
    session.close();
  });
});