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.

12.6 KB
Raw Blame
import { readFile } from 'node:fs/promises';

import { parsePatch } from 'diff';
import { describe, expect, it } from 'vitest';

import { CodexAdapter } from './codex';
import { CodexAppServerAdapter } from './codexAppServer';

const loadFixture = async () => {
  const source = await readFile(
    new URL('./__fixtures__/codex/app-server-single-turn.jsonl', import.meta.url),
    'utf8',
  );
  return source
    .trim()
    .split('\n')
    .map((line) => JSON.parse(line) as { method: string; params: unknown });
};

const loadParityFixture = async () => {
  const source = await readFile(
    new URL('./__fixtures__/codex/app-server-vs-exec.json', import.meta.url),
    'utf8',
  );
  return JSON.parse(source) as {
    appServer: Array<{ method: string; params: unknown }>;
    exec: unknown[];
  };
};

describe('CodexAppServerAdapter', () => {
  it('reports a steered user message and answers it in a step of its own', () => {
    const adapter = new CodexAppServerAdapter({ sessionId: 'thread-1' });
    const item = (type: string, extra: Record<string, unknown>) => ({
      item: { id: `${type}-1`, type, ...extra },
      threadId: 'thread-1',
      turnId: 'turn-1',
    });
    adapter.adapt('turn/started', { threadId: 'thread-1', turn: { id: 'turn-1' } });
    adapter.adapt('item/agentMessage/delta', { delta: 'Working on it', itemId: 'm1' });

    // The prompt that started the turn carries no client id: not a steer.
    expect(adapter.adapt('item/started', item('userMessage', { clientId: null }))).toEqual([]);

    const steered = adapter.adapt('item/started', item('userMessage', { clientId: 'steer-1' }));
    expect(steered).toEqual([
      expect.objectContaining({
        data: { phase: 'user_message', uuid: 'steer-1' },
        type: 'step_complete',
      }),
    ]);

    const answer = adapter.adapt('item/agentMessage/delta', { delta: 'Sure', itemId: 'm2' });
    expect(answer.map(({ type }) => type)).toEqual(['stream_end', 'stream_start', 'stream_chunk']);
    expect(answer[1].data).toMatchObject({ newStep: true });
  });

  it('carries the native turn boundary across tool-separated steps', async () => {
    const adapter = new CodexAppServerAdapter({ sessionId: 'native-thread' });
    const events = (await loadFixture()).flatMap(({ method, params }) =>
      adapter.adapt(method, params),
    );
    const starts = events.filter(({ type }) => type === 'stream_start');
    expect(starts.length).toBeGreaterThan(1);
    for (const start of starts) {
      expect(start.data).toMatchObject({
        codexTurnId: 'turn-1',
        sessionId: 'native-thread',
      });
    }
  });
  it('maps a native v2 turn directly into the existing stream contract', async () => {
    const adapter = new CodexAppServerAdapter({ initialModel: 'gpt-5.5-codex' });
    const raw = await loadFixture();
    const events = raw.flatMap(({ method, params }) => adapter.adapt(method, params));

    expect(
      events.filter(({ type }) => type === 'stream_chunk').map(({ data }) => data.chunkType),
    ).toEqual([
      'reasoning',
      'tools_calling',
      'tool_state',
      'tools_calling',
      'tools_calling',
      'tool_state',
      'text',
    ]);
    expect(
      events
        .findLast(({ data, type }) => type === 'stream_chunk' && data.chunkType === 'tools_calling')
        ?.data.toolsCalling.map(({ id }: { id: string }) => id),
    ).toEqual(['command-1', 'mcp-1', 'turn-plan-turn-1']);
    expect(
      events.filter(({ type }) => type === 'tool_result').map(({ data }) => data.toolCallId),
    ).toEqual(['command-1', 'mcp-1', 'turn-plan-turn-1']);
    expect(
      events.find(({ data, type }) => type === 'stream_chunk' && data.chunkType === 'reasoning')
        ?.data.reasoning,
    ).toBe('Inspecting the workspace');
    expect(
      events
        .filter(({ data, type }) => type === 'stream_chunk' && data.chunkType === 'text')
        .map(({ data }) => data.content),
    ).toEqual(['Done.']);
    expect(
      events.find(({ data, type }) => type === 'step_complete' && data.usage)?.data.usage,
    ).toMatchObject({
      inputCachedTokens: 2,
      inputCacheMissTokens: 8,
      outputReasoningTokens: 3,
      outputTextTokens: 4,
      totalTokens: 17,
    });
    expect(events.at(-1)).toMatchObject({ type: 'agent_runtime_end' });
  });

  it('ignores unknown notification methods for forward compatibility', () => {
    const adapter = new CodexAppServerAdapter();

    expect(adapter.adapt('future/notification', { threadId: 'thread-1' })).toEqual([]);
  });

  it('keeps unfinished plan state when a turn is interrupted', () => {
    const adapter = new CodexAppServerAdapter();
    const planEvents = adapter.adapt('turn/plan/updated', {
      explanation: null,
      plan: [
        { status: 'completed', step: 'Inspect' },
        { status: 'inProgress', step: 'Implement' },
        { status: 'pending', step: 'Verify' },
      ],
      threadId: 'thread-1',
      turnId: 'turn-1',
    });

    const terminalEvents = adapter.interruptForTransportFailure();

    expect(
      planEvents.find(({ data }) => data.chunkType === 'tool_state')?.data.pluginState.todos.items,
    ).toEqual([
      { status: 'completed', text: 'Inspect' },
      { status: 'processing', text: 'Implement' },
      { status: 'todo', text: 'Verify' },
    ]);
    expect(
      terminalEvents.find(
        ({ data, type }) => type === 'tool_end' && data.toolCallId === 'turn-plan-turn-1',
      ),
    ).toMatchObject({ data: { isSuccess: false } });
    expect(
      terminalEvents.some(
        ({ data, type }) => type === 'tool_result' && data.toolCallId === 'turn-plan-turn-1',
      ),
    ).toBe(false);
  });

  it('truncates oversized completed command output before persisting the result', () => {
    const adapter = new CodexAppServerAdapter();
    const item = {
      aggregatedOutput: 'x'.repeat(25_010),
      command: 'print-output',
      durationMs: 1,
      exitCode: 0,
      id: 'command-large',
      status: 'completed',
      type: 'commandExecution',
    };
    adapter.adapt('item/started', {
      item: {
        ...item,
        aggregatedOutput: null,
        durationMs: null,
        exitCode: null,
        status: 'inProgress',
      },
      startedAtMs: 1,
      threadId: 'thread-1',
      turnId: 'turn-1',
    });

    const events = adapter.adapt('item/completed', {
      completedAtMs: 2,
      item,
      threadId: 'thread-1',
      turnId: 'turn-1',
    });
    const result = events.find(({ type }) => type === 'tool_result')?.data;

    expect(result.content).toContain(
      '[Output truncated: 10 characters omitted. Original length: 25010 characters]',
    );
    expect(result.pluginState).toMatchObject({
      omittedOutputCharacters: 10,
      originalOutputLength: 25_010,
      outputTruncated: true,
    });
    expect(result.content).toHaveLength(25_078);
    expect(result.pluginState.output).toBe(result.content);
    expect(result.pluginState.stdout).toBe(result.content);
  });

  it('normalizes native file changes into complete single-file patches', () => {
    const adapter = new CodexAppServerAdapter();
    const events = adapter.adapt('item/completed', {
      completedAtMs: 2,
      item: {
        changes: [
          {
            diff: '@@ -1 +1 @@\n-old\n+new\n',
            kind: { move_path: null, type: 'update' },
            path: '/workspace/updated.ts',
          },
          {
            diff: 'first\nsecond\n',
            kind: { type: 'add' },
            path: 'src/added.ts',
          },
          {
            diff: 'removed\n',
            kind: { type: 'delete' },
            path: 'src/deleted.ts',
          },
          {
            diff: '@@ -1 +1 @@\n-before\n+after\n\n\nMoved to: src/after.ts',
            kind: { move_path: 'src/after.ts', type: 'update' },
            path: 'src/before.ts',
          },
        ],
        id: 'file-change-1',
        status: 'completed',
        type: 'fileChange',
      },
      threadId: 'thread-1',
      turnId: 'turn-1',
    });
    const changes = events.find(({ type }) => type === 'tool_result')?.data.pluginState
      ?.changes as Array<{ diffText: string; kind: string }>;

    expect(changes.map(({ kind }) => kind)).toEqual(['update', 'add', 'delete', 'rename']);
    expect(changes[0].diffText).toContain(
      'diff --git a/workspace/updated.ts b/workspace/updated.ts\n--- a/workspace/updated.ts\n+++ b/workspace/updated.ts\n@@ -1 +1 @@',
    );
    expect(changes[1].diffText).toContain('--- /dev/null');
    expect(changes[1].diffText).toContain('+++ b/src/added.ts');
    expect(changes[1].diffText).toContain('+first\n+second');
    expect(changes[2].diffText).toContain('--- a/src/deleted.ts');
    expect(changes[2].diffText).toContain('+++ /dev/null');
    expect(changes[2].diffText).toContain('-removed');
    expect(changes[3].diffText).toContain('diff --git a/src/before.ts b/src/after.ts');
    expect(changes[3].diffText).not.toContain('Moved to:');

    for (const { diffText } of changes) {
      const parsed = parsePatch(diffText);
      expect(parsed).toHaveLength(1);
      expect(parsed[0].oldFileName).toBeTypeOf('string');
      expect(parsed[0].newFileName).toBeTypeOf('string');
    }
  });

  it.each([
    {
      code: 'rate_limit',
      info: 'usageLimitExceeded',
      kind: 'usage_limit',
      message: 'Usage limit reached',
    },
    {
      code: 'overloaded',
      info: 'serverOverloaded',
      kind: 'server_overloaded',
      message: 'Server overloaded',
    },
    {
      code: 'overloaded',
      info: { responseStreamDisconnected: { httpStatusCode: 502 } },
      kind: 'network_drop',
      message: 'Response stream disconnected',
    },
  ])('classifies native $kind failures', ({ code, info, kind, message }) => {
    const adapter = new CodexAppServerAdapter();

    const events = adapter.adapt('error', {
      error: { additionalDetails: null, codexErrorInfo: info, message },
      threadId: 'thread-1',
      turnId: 'turn-1',
      willRetry: false,
    });

    expect(events.at(-1)).toMatchObject({
      data: {
        code,
        details: {
          codexErrorInfo: info,
          kind,
          ...(kind === 'network_drop' ? { httpStatusCode: 502 } : {}),
        },
        message,
      },
      type: 'error',
    });
  });

  it.each([null, 'other'])('classifies capacity errors with info %s for auto-retry', (info) => {
    const adapter = new CodexAppServerAdapter();
    const message = 'Selected model is at capacity. Please try a different model.';
    const error = { additionalDetails: null, codexErrorInfo: info, message };
    const events = adapter.adapt('error', {
      error,
      threadId: 'thread-1',
      turnId: 'turn-1',
      willRetry: false,
    });
    expect(events.at(-1)).toMatchObject({
      data: {
        agentType: 'codex',
        clearEchoedContent: true,
        code: 'overloaded',
        details: { kind: 'server_overloaded' },
        message,
      },
      type: 'error',
    });
    expect(
      adapter.adapt('turn/completed', {
        threadId: 'thread-1',
        turn: { error, id: 'turn-1', items: [], status: 'failed' },
      }),
    ).toEqual([]);
  });

  it('preserves native retry notifications for capacity failures', () => {
    const adapter = new CodexAppServerAdapter();
    const message = 'Selected model is at capacity. Please try a different model.';
    expect(
      adapter.adapt('error', {
        error: { additionalDetails: null, codexErrorInfo: null, message },
        threadId: 'thread-1',
        turnId: 'turn-1',
        willRetry: true,
      }),
    ).toMatchObject([{ data: { message }, type: 'stream_retry' }]);
  });

  it('keeps native and exec adapters semantically aligned during migration', async () => {
    const fixture = await loadParityFixture();
    const nativeAdapter = new CodexAppServerAdapter();
    const execAdapter = new CodexAdapter();
    const nativeEvents = fixture.appServer.flatMap(({ method, params }) =>
      nativeAdapter.adapt(method, params),
    );
    const execEvents = fixture.exec.flatMap((event) => execAdapter.adapt(event));
    const summarize = (events: typeof nativeEvents) => ({
      text: events
        .filter(({ data, type }) => type === 'stream_chunk' && data.chunkType === 'text')
        .map(({ data }) => data.content)
        .join(''),
      toolResults: events
        .filter(({ type }) => type === 'tool_result')
        .map(({ data }) => ({ content: data.content, id: data.toolCallId })),
      toolStarts: events
        .filter(({ type }) => type === 'tool_start')
        .map(({ data }) => data.toolCallId),
      totalTokens: events.find(({ data, type }) => type === 'step_complete' && data.usage)?.data
        .usage.totalTokens,
      types: events.map(({ type }) => type),
    });

    expect(summarize(nativeEvents)).toEqual(summarize(execEvents));
  });
});