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.

31.3 KB
Raw Blame
import type { ChildProcess } from 'node:child_process';
import { EventEmitter } from 'node:events';
import { PassThrough } from 'node:stream';

import type { AgentStreamEvent } from '@lobechat/agent-gateway-client';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { AskUserBridge } from '../askUser/AskUserBridge';
import { AcpRpcResponseError } from './acpStdioClient';
import {
  buildDevinAcpArgs,
  buildDevinAcpPrompt,
  DevinAcpSession,
  type DevinAcpSessionOptions,
  isDevinAcpSessionNotFoundError,
} from './devinAcpSession';

const { spawnMock } = vi.hoisted(() => ({ spawnMock: vi.fn() }));

vi.mock('node:child_process', async (importOriginal) => {
  const actual = await importOriginal<Record<string, unknown>>();
  return { ...actual, spawn: spawnMock };
});

interface RpcMessage {
  error?: { code: number; data?: unknown; message: string };
  id?: number | string;
  method?: string;
  params?: Record<string, unknown>;
  result?: unknown;
}

const createAcpProcess = ({
  configOptions,
  configOptionsByModel,
  configUpdateOnPrompt,
  configUpdateOnPromptCount = 1,
  initializeResult,
  loadError,
  modeSetError,
  permissionRequest = false,
  promptAutoComplete = true,
}: {
  configOptions?: Record<string, unknown>[];
  configOptionsByModel?: Record<string, Record<string, unknown>[]>;
  configUpdateOnPrompt?: Record<string, unknown>[];
  configUpdateOnPromptCount?: number;
  initializeResult?: Record<string, unknown>;
  loadError?: { code: number; data?: unknown; message: string };
  modeSetError?: { code: number; data?: unknown; message: string };
  permissionRequest?: boolean;
  promptAutoComplete?: boolean;
} = {}) => {
  const child = new EventEmitter() as ChildProcess;
  const stdout = new PassThrough();
  const stderr = new PassThrough();
  const requests: RpcMessage[] = [];
  let promptRequest: RpcMessage | undefined;
  let currentMode = 'accept-edits';
  let currentModel = 'claude-sonnet-4-6-thinking';
  const send = (message: Record<string, unknown>) =>
    stdout.write(`${JSON.stringify({ jsonrpc: '2.0', ...message })}\n`);

  Object.assign(child, {
    kill: vi.fn(() => true),
    killed: false,
    pid: 987_654,
    stderr,
    stdin: {
      once: vi.fn(),
      write: vi.fn((chunk: string) => {
        const message = JSON.parse(chunk.trim()) as RpcMessage;
        requests.push(message);
        queueMicrotask(() => {
          switch (message.method) {
            case 'initialize': {
              send({
                id: message.id,
                result:
                  initializeResult ??
                  ({
                    agentCapabilities: {
                      loadSession: true,
                      promptCapabilities: { image: true },
                    },
                    authMethods: [{ id: 'devin-browser', name: 'Log in with browser' }],
                    protocolVersion: 1,
                  } satisfies Record<string, unknown>),
              });
              return;
            }
            case 'authenticate': {
              send({ id: message.id, result: {} });
              return;
            }
            case 'session/new': {
              send({
                id: message.id,
                result: {
                  configOptions: configOptions ?? [
                    {
                      category: 'model',
                      currentValue: currentModel,
                      id: 'model',
                      name: 'Model',
                      options: [
                        { name: 'Claude Sonnet 4.6 Thinking', value: 'claude-sonnet-4-6-thinking' },
                        { name: 'GLM 5.2', value: 'glm-5-2' },
                        { name: 'SWE 1.7 Medium', value: 'swe-1-7-medium' },
                      ],
                    },
                  ],
                  sessionId: 'devin-session-1',
                },
              });
              return;
            }
            case 'session/load': {
              send(
                loadError
                  ? { error: loadError, id: message.id }
                  : { id: message.id, result: { configOptions } },
              );
              return;
            }
            case 'session/set_config_option': {
              const params = message.params as { configId?: string; value?: string } | undefined;
              if (params?.configId === 'mode' && modeSetError) {
                send({ error: modeSetError, id: message.id });
                return;
              }
              if (params?.configId === 'model' && params.value) {
                configOptions = configOptionsByModel?.[params.value] ?? configOptions;
              }
              const config = configOptions?.find(({ id }) => id === params?.configId);
              if (config) config.currentValue = params?.value;
              if (params?.configId === 'mode' && params.value) currentMode = params.value;
              if (params?.configId === 'model' && params.value) currentModel = params.value;
              send({
                id: message.id,
                result: {
                  configOptions: configOptions ?? [
                    {
                      category: 'mode',
                      currentValue: currentMode,
                      id: 'mode',
                      name: 'Session Mode',
                    },
                    {
                      category: 'model',
                      currentValue: currentModel,
                      id: 'model',
                      name: 'Model',
                    },
                  ],
                },
              });
              return;
            }
            case 'session/prompt': {
              promptRequest = message;
              for (let i = 0; configUpdateOnPrompt && i < configUpdateOnPromptCount; i += 1) {
                send({
                  method: 'session/update',
                  params: {
                    update: {
                      configOptions: configUpdateOnPrompt,
                      sessionUpdate: 'config_option_update',
                    },
                  },
                });
              }
              send({
                method: 'session/update',
                params: {
                  sessionId: 'devin-session-1',
                  update: {
                    _meta: { 'cognition.ai/inferenceToolName': 'exec' },
                    kind: 'execute',
                    rawInput: { command: 'pwd' },
                    sessionUpdate: 'tool_call',
                    title: 'Ran command',
                    toolCallId: 'tool-1',
                  },
                },
              });
              send({
                method: 'session/update',
                params: {
                  sessionId: 'devin-session-1',
                  update: {
                    content: [{ content: { text: '/workspace', type: 'text' }, type: 'content' }],
                    sessionUpdate: 'tool_call_update',
                    status: 'completed',
                    toolCallId: 'tool-1',
                  },
                },
              });
              send({
                method: 'session/update',
                params: {
                  sessionId: 'devin-session-1',
                  update: {
                    content: { text: 'Done', type: 'text' },
                    sessionUpdate: 'agent_message_chunk',
                  },
                },
              });
              if (permissionRequest) {
                send({
                  id: 'permission-1',
                  method: 'session/request_permission',
                  params: {
                    options: [
                      { kind: 'allow_once', name: 'Allow', optionId: 'allow_once' },
                      {
                        kind: 'allow_always',
                        name: 'Allow for this session',
                        optionId: 'allow_session',
                      },
                      { kind: 'reject_once', name: 'Reject', optionId: 'reject_once' },
                    ],
                    sessionId: 'devin-session-1',
                    toolCall: {
                      _meta: { 'cognition.ai/editableCommand': 'printf test' },
                      toolCallId: 'tool-1',
                    },
                  },
                });
              } else if (promptAutoComplete) {
                send({ id: message.id, result: { stopReason: 'end_turn' } });
              }
              return;
            }
          }

          if (message.id === 'permission-1' && message.result && promptRequest) {
            send({ id: promptRequest.id, result: { stopReason: 'end_turn' } });
          }
        });
        return true;
      }),
    },
    stdout,
  });

  return { child, requests };
};

const createSessionOptions = (
  overrides: Partial<DevinAcpSessionOptions> = {},
): DevinAcpSessionOptions => ({
  args: ['--model', 'sonnet'],
  clientVersion: '1.2.3',
  commandPath: 'devin',
  cwd: '/workspace',
  env: process.env,
  onEvents: vi.fn(),
  onModel: vi.fn(),
  onRawMessage: vi.fn(),
  onRuntimeStatus: vi.fn(),
  onSessionId: vi.fn(),
  onStderr: vi.fn(),
  operationId: 'operation-1',
  prompt: [{ text: 'hello', type: 'text' }],
  sessionId: 'session-1',
  ...overrides,
});

afterEach(() => {
  vi.restoreAllMocks();
  spawnMock.mockReset();
});

describe('Devin ACP helpers', () => {
  it('places the ACP subcommand before provider arguments', () => {
    expect(buildDevinAcpArgs(['--model', 'sonnet'])).toEqual(['acp', '--model', 'sonnet']);
  });

  it('moves a permission mode already in extra args before the acp subcommand', () => {
    expect(buildDevinAcpArgs(['--permission-mode', 'dangerous', '--model', 'sonnet'])).toEqual([
      '--permission-mode',
      'dangerous',
      'acp',
      '--model',
      'sonnet',
    ]);
  });

  it('supports --permission-mode=<value> in extra args', () => {
    expect(buildDevinAcpArgs(['--permission-mode=normal', '--model', 'sonnet'])).toEqual([
      '--permission-mode',
      'normal',
      'acp',
      '--model',
      'sonnet',
    ]);
  });

  it('builds text and image prompt blocks', async () => {
    await expect(
      buildDevinAcpPrompt([
        { text: 'inspect this', type: 'text' },
        {
          source: { data: 'aGVsbG8=', mediaType: 'image/png', type: 'base64' },
          type: 'image',
        },
      ]),
    ).resolves.toEqual([
      { text: 'inspect this', type: 'text' },
      { data: 'aGVsbG8=', mimeType: 'image/png', type: 'image' },
    ]);
  });

  it('recognizes Devin session-not-found RPC errors', () => {
    expect(
      isDevinAcpSessionNotFoundError(
        new AcpRpcResponseError('session/load', {
          code: -32_016,
          data: { 'cognition.ai/errorKind': 'session_not_found' },
          message: 'Session not found',
        }),
      ),
    ).toBe(true);
    expect(
      isDevinAcpSessionNotFoundError(
        new AcpRpcResponseError('session/prompt', {
          code: -32_016,
          message: 'Session not found',
        }),
      ),
    ).toBe(false);
  });
});

describe('DevinAcpSession', () => {
  it('uses cached credentials without starting browser authentication', async () => {
    const fake = createAcpProcess();
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions();
    const events: AgentStreamEvent[] = [];
    options.onEvents = (batch) => {
      events.push(...batch);
    };

    await new DevinAcpSession(options).run();

    expect(spawnMock).toHaveBeenCalledWith(
      'devin',
      ['acp', '--model', 'sonnet'],
      expect.objectContaining({ cwd: '/workspace', stdio: ['pipe', 'pipe', 'pipe'] }),
    );
    expect(fake.requests.map(({ method }) => method).filter(Boolean)).toEqual([
      'initialize',
      'session/new',
      'session/set_config_option',
      'session/prompt',
    ]);
    expect(options.onSessionId).toHaveBeenCalledWith('devin-session-1');
    expect(options.onModel).toHaveBeenCalledWith('claude-sonnet-4-6-thinking');
    expect(events).toContainEqual(
      expect.objectContaining({
        data: expect.objectContaining({
          toolCalling: expect.objectContaining({ apiName: 'exec', id: 'tool-1' }),
        }),
        type: 'tool_start',
      }),
    );
    expect(events).toContainEqual(
      expect.objectContaining({
        data: { chunkType: 'text', content: 'Done' },
        type: 'stream_chunk',
      }),
    );
    expect(events).toContainEqual(expect.objectContaining({ type: 'agent_runtime_end' }));
    expect(options.onRuntimeStatus).toHaveBeenLastCalledWith(
      expect.objectContaining({ state: 'closed', transport: 'devin-acp' }),
    );
  });

  it('loads a saved session and preserves the structured not-found error', async () => {
    const success = createAcpProcess();
    spawnMock.mockReturnValueOnce(success.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions({ resumeSessionId: 'saved-session' });

    await new DevinAcpSession(options).run();

    expect(success.requests.find(({ method }) => method === 'session/load')?.params).toEqual({
      cwd: '/workspace',
      mcpServers: [],
      sessionId: 'saved-session',
    });
    expect(options.onSessionId).toHaveBeenCalledWith('saved-session');

    const missing = createAcpProcess({
      loadError: {
        code: -32_016,
        data: { 'cognition.ai/errorKind': 'session_not_found' },
        message: 'Session not found',
      },
    });
    spawnMock.mockReturnValueOnce(missing.child);

    await expect(
      new DevinAcpSession(createSessionOptions({ resumeSessionId: 'missing-session' })).run(),
    ).rejects.toSatisfy(isDevinAcpSessionNotFoundError);
    expect(missing.requests.some(({ method }) => method === 'session/prompt')).toBe(false);
  });

  it('requires an explicit user choice before granting permission', async () => {
    const fake = createAcpProcess({ permissionRequest: true });
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const bridge = new AskUserBridge('operation-1');
    const run = new DevinAcpSession(createSessionOptions({ askUserBridge: bridge })).run();
    const intervention = await bridge.events()[Symbol.asyncIterator]().next();

    expect(intervention.value).toMatchObject({
      data: {
        apiName: 'askUserQuestion',
        interactionKind: 'permission',
        toolCallId: 'devin-permission-permission-1-tool-1',
      },
      type: 'agent_intervention_request',
    });
    expect(JSON.parse(intervention.value!.data.arguments)).toEqual({
      questions: [
        {
          header: 'Permission required',
          multiSelect: false,
          options: [
            { id: 'allow_once', label: 'Allow' },
            { id: 'allow_session', label: 'Allow for this session' },
            { id: 'reject_once', label: 'Reject' },
          ],
          question: 'printf test',
        },
      ],
    });
    bridge.resolve(intervention.value!.data.toolCallId, {
      result: { 'printf test': 'allow_session' },
    });
    await run;

    expect(fake.requests.find(({ id }) => id === 'permission-1')?.result).toEqual({
      outcome: { optionId: 'allow_session', outcome: 'selected' },
    });
  });

  it('does not resolve permission choices by their display label', async () => {
    const fake = createAcpProcess({ permissionRequest: true });
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const bridge = new AskUserBridge('operation-1');
    const run = new DevinAcpSession(createSessionOptions({ askUserBridge: bridge })).run();
    const intervention = await bridge.events()[Symbol.asyncIterator]().next();

    bridge.resolve(intervention.value!.data.toolCallId, {
      result: { 'printf test': 'Allow for this session' },
    });
    await run;

    expect(fake.requests.find(({ id }) => id === 'permission-1')?.result).toEqual({
      outcome: { outcome: 'cancelled' },
    });
  });

  it('cancels permission requests when no intervention bridge is available', async () => {
    const fake = createAcpProcess({ permissionRequest: true });
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);

    await new DevinAcpSession(createSessionOptions()).run();

    expect(fake.requests.find(({ id }) => id === 'permission-1')?.result).toEqual({
      outcome: { outcome: 'cancelled' },
    });
  });

  it('forces the bypass ACP session mode without any permission parameters', async () => {
    const fake = createAcpProcess();
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions();

    await new DevinAcpSession(options).run();

    expect(spawnMock).toHaveBeenCalledWith(
      'devin',
      ['acp', '--model', 'sonnet'],
      expect.objectContaining({ cwd: '/workspace', stdio: ['pipe', 'pipe', 'pipe'] }),
    );
    expect(fake.requests.map(({ method }) => method).filter(Boolean)).toEqual([
      'initialize',
      'session/new',
      'session/set_config_option',
      'session/prompt',
    ]);
    expect(
      fake.requests.find(({ method }) => method === 'session/set_config_option')?.params,
    ).toEqual({
      configId: 'mode',
      sessionId: 'devin-session-1',
      value: 'bypass',
    });
    expect(options.onSessionId).toHaveBeenCalledWith('devin-session-1');
  });

  it('forces bypass in the desktop direct-ACP shape (no args, no permission options)', async () => {
    const fake = createAcpProcess();
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    // Mirrors apps/desktop heterogeneousAgentSession: `new DevinAcpSession({...})`
    // with no permission-related input at all.
    const options = createSessionOptions({ args: [] });

    await new DevinAcpSession(options).run();

    expect(spawnMock).toHaveBeenCalledWith(
      'devin',
      ['acp'],
      expect.objectContaining({ cwd: '/workspace' }),
    );
    expect(
      fake.requests
        .filter(({ method }) => method === 'session/set_config_option')
        .map(({ params }) => params),
    ).toEqual([{ configId: 'mode', sessionId: 'devin-session-1', value: 'bypass' }]);
    expect(options.onSessionId).toHaveBeenCalledWith('devin-session-1');
  });

  it('does not let a --permission-mode in args change the ACP session mode', async () => {
    const fake = createAcpProcess();
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions({
      args: ['--permission-mode', 'dangerous', '--model', 'sonnet'],
    });

    await new DevinAcpSession(options).run();

    // The CLI flag is still forwarded unchanged, but it has no effect on ACP.
    expect(spawnMock).toHaveBeenCalledWith(
      'devin',
      ['--permission-mode', 'dangerous', 'acp', '--model', 'sonnet'],
      expect.objectContaining({ cwd: '/workspace', stdio: ['pipe', 'pipe', 'pipe'] }),
    );
    expect(
      fake.requests.find(({ method }) => method === 'session/set_config_option')?.params,
    ).toEqual({
      configId: 'mode',
      sessionId: 'devin-session-1',
      value: 'bypass',
    });
  });

  it('still establishes the session when setting the session mode fails', async () => {
    const fake = createAcpProcess({
      modeSetError: { code: -32_602, message: 'Invalid mode: bypass' },
    });
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
    const options = createSessionOptions({ initialModel: 'glm-5-2' });

    await expect(new DevinAcpSession(options).run()).resolves.not.toThrow();

    expect(fake.requests.map(({ method }) => method).filter(Boolean)).toEqual([
      'initialize',
      'session/new',
      'session/set_config_option',
      'session/set_config_option',
      'session/prompt',
    ]);
    expect(
      fake.requests.filter(({ method }) => method === 'session/set_config_option')[1]?.params,
    ).toEqual({ configId: 'mode', sessionId: 'devin-session-1', value: 'bypass' });
    expect(warn).toHaveBeenCalledWith(
      expect.stringContaining('Failed to set session mode "bypass"'),
      {
        error: expect.any(AcpRpcResponseError),
        operationId: 'operation-1',
        sessionId: 'devin-session-1',
      },
    );
    expect(options.onSessionId).toHaveBeenCalledWith('devin-session-1');
    expect(options.onModel).toHaveBeenCalledWith('glm-5-2');
  });

  it('re-asserts bypass once when a config update reports a different mode', async () => {
    const fake = createAcpProcess({
      configUpdateOnPrompt: [{ currentValue: 'accept-edits', id: 'mode' }],
      configUpdateOnPromptCount: 2,
    });
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions();

    await new DevinAcpSession(options).run();
    // The re-assert is fire-and-forget; let its write land.
    await new Promise((resolve) => setTimeout(resolve, 0));

    const modeRequests = fake.requests.filter(
      ({ method, params }) => method === 'session/set_config_option' && params?.configId === 'mode',
    );
    // 1 from establishSession + exactly 1 re-assert, despite two drifted updates.
    expect(modeRequests.map(({ params }) => params)).toEqual([
      { configId: 'mode', sessionId: 'devin-session-1', value: 'bypass' },
      { configId: 'mode', sessionId: 'devin-session-1', value: 'bypass' },
    ]);
    const promptIndex = fake.requests.findIndex(({ method }) => method === 'session/prompt');
    expect(fake.requests.indexOf(modeRequests[1]!)).toBeGreaterThan(promptIndex);
  });

  it('does not re-assert the mode when a config update already reports bypass', async () => {
    const fake = createAcpProcess({
      configUpdateOnPrompt: [{ currentValue: 'bypass', id: 'mode' }],
      configUpdateOnPromptCount: 2,
    });
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);

    await new DevinAcpSession(createSessionOptions()).run();
    await new Promise((resolve) => setTimeout(resolve, 0));

    expect(
      fake.requests.filter(
        ({ method, params }) =>
          method === 'session/set_config_option' && params?.configId === 'mode',
      ),
    ).toHaveLength(1);
  });

  it('sets the ACP session mode on a resumed session', async () => {
    const fake = createAcpProcess();
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions({ resumeSessionId: 'saved-session' });

    await new DevinAcpSession(options).run();

    expect(fake.requests.map(({ method }) => method).filter(Boolean)).toEqual([
      'initialize',
      'session/load',
      'session/set_config_option',
      'session/prompt',
    ]);
    expect(
      fake.requests.find(({ method }) => method === 'session/set_config_option')?.params,
    ).toEqual({
      configId: 'mode',
      sessionId: 'saved-session',
      value: 'bypass',
    });
  });

  it('applies initialModel through session/set_config_option when it differs from the default', async () => {
    const fake = createAcpProcess();
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions({ initialModel: 'glm-5-2' });

    await new DevinAcpSession(options).run();

    expect(fake.requests.map(({ method }) => method).filter(Boolean)).toEqual([
      'initialize',
      'session/new',
      'session/set_config_option',
      'session/set_config_option',
      'session/prompt',
    ]);
    expect(
      fake.requests.find(({ method }) => method === 'session/set_config_option')?.params,
    ).toEqual({
      configId: 'model',
      sessionId: 'devin-session-1',
      value: 'glm-5-2',
    });
    expect(options.onModel).toHaveBeenCalledWith('glm-5-2');
  });

  it('matches a human-readable model label to the ACP model value', async () => {
    const fake = createAcpProcess();
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions({ initialModel: 'glm 5.2' });

    await new DevinAcpSession(options).run();

    expect(
      fake.requests.find(({ method }) => method === 'session/set_config_option')?.params,
    ).toEqual({
      configId: 'model',
      sessionId: 'devin-session-1',
      value: 'glm-5-2',
    });
    expect(options.onModel).toHaveBeenCalledWith('glm-5-2');
  });

  it('does not call session/set_config_option for model when initialModel matches the default', async () => {
    const fake = createAcpProcess();
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions({ initialModel: 'claude-sonnet-4-6-thinking' });

    await new DevinAcpSession(options).run();

    // Only the forced session mode is set; the model is left untouched.
    expect(
      fake.requests
        .filter(({ method }) => method === 'session/set_config_option')
        .map(({ params }) => params?.configId),
    ).toEqual(['mode']);
  });

  it('sets the model first and then the forced bypass mode', async () => {
    const fake = createAcpProcess();
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions({ initialModel: 'glm-5-2' });

    await new DevinAcpSession(options).run();

    expect(fake.requests.map(({ method }) => method).filter(Boolean)).toEqual([
      'initialize',
      'session/new',
      'session/set_config_option',
      'session/set_config_option',
      'session/prompt',
    ]);
    const setConfigRequests = fake.requests.filter(
      ({ method }) => method === 'session/set_config_option',
    );
    expect(setConfigRequests[0]?.params).toEqual({
      configId: 'model',
      sessionId: 'devin-session-1',
      value: 'glm-5-2',
    });
    expect(setConfigRequests[1]?.params).toEqual({
      configId: 'mode',
      sessionId: 'devin-session-1',
      value: 'bypass',
    });
    expect(options.onModel).toHaveBeenCalledWith('glm-5-2');
  });

  it.each([
    ['swe-2-high', 'max', 'swe-2-max', undefined, []],
    ['claude-opus-5-5-medium', 'max', 'claude-opus-5-5-max', undefined, []],
    ['swe-2-high', 'max', 'swe-2-high', undefined, [['thought_level', 'high']]],
    ['swe-2-high', 'high', 'swe-2-max', 'saved-session', [['thought_level', 'max']]],
    [
      'swe-2-high',
      'high',
      'claude-opus-5-5-max',
      'saved-session',
      [
        ['model', 'claude-opus-5-5-medium'],
        ['thought_level', 'max'],
      ],
    ],
  ] as const)(
    'preserves the requested variant %s/%s → %s (resume: %s)',
    async (currentModel, thoughtLevel, initialModel, resumeSessionId, expectedChanges) => {
      // Devin 3000.11.3 advertises a family representative in `model` even
      // when --model has already selected a different thinking variant.
      const fake = createAcpProcess({
        configOptions: [
          {
            currentValue: currentModel,
            id: 'model',
            options: [
              { name: 'SWE-2', value: 'swe-2-high' },
              { name: 'Claude Opus 5.5', value: 'claude-opus-5-5-medium' },
            ],
            type: 'select',
          },
          {
            currentValue: thoughtLevel,
            id: 'thought_level',
            options: ['medium', 'high', 'max'].map((value) => ({ name: value, value })),
            type: 'select',
          },
        ],
      });
      spawnMock.mockReturnValue(fake.child);
      vi.spyOn(process, 'kill').mockImplementation(() => true);
      const options = createSessionOptions({ initialModel, resumeSessionId });

      await new DevinAcpSession(options).run();

      expect(
        fake.requests
          .filter(({ method }) => method === 'session/set_config_option')
          .map(({ params }) => [params?.configId, params?.value]),
      ).toEqual([...expectedChanges, ['mode', 'bypass']]);
      expect(options.onModel).toHaveBeenLastCalledWith(initialModel);
      expect(fake.requests.at(-1)?.method).toBe('session/prompt');
    },
  );

  it('validates thinking against the new family and tracks later config updates', async () => {
    const modelConfig = {
      currentValue: 'swe-2-high',
      id: 'model',
      options: [{ value: 'swe-2-high' }, { value: 'claude-opus-5-5-medium' }],
    };
    const targetConfig = [
      { ...modelConfig, currentValue: 'claude-opus-5-5-medium' },
      { currentValue: 'medium', id: 'thought_level', options: [{ value: 'low' }] },
    ];
    const fake = createAcpProcess({
      configOptions: [
        modelConfig,
        { currentValue: 'high', id: 'thought_level', options: [{ value: 'high' }] },
      ],
      configOptionsByModel: { 'claude-opus-5-5-medium': targetConfig },
      configUpdateOnPrompt: [
        { ...modelConfig, currentValue: 'claude-opus-5-5-medium' },
        { currentValue: 'max', id: 'thought_level', options: [{ value: 'max' }] },
      ],
    });
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const options = createSessionOptions({ initialModel: 'claude-opus-5-5-low' });

    await new DevinAcpSession(options).run();

    expect(options.onModel).toHaveBeenNthCalledWith(1, 'claude-opus-5-5-low');
    expect(options.onModel).toHaveBeenLastCalledWith('claude-opus-5-5-max');
    expect(targetConfig[1].currentValue).toBe('low');
  });

  it.each([
    ['unknown-max', [{ value: 'max' }]],
    ['swe-2-max', [{ value: 'medium' }]],
    ['swe-2-max', undefined],
    ['fusion-opus-max-sidekick-swe-2-high', [{ value: 'max' }]],
  ])('rejects unavailable variants without sending a prompt: %s', async (initialModel, levels) => {
    const fake = createAcpProcess({
      configOptions: [
        {
          currentValue: 'swe-2-high',
          id: 'model',
          options: [{ value: 'swe-2-high' }, { value: 'fusion-opus-medium-sidekick-swe-2-high' }],
        },
        ...(levels ? [{ currentValue: 'high', id: 'thought_level', options: levels }] : []),
      ],
    });
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);

    await expect(new DevinAcpSession(createSessionOptions({ initialModel })).run()).rejects.toThrow(
      /Devin ACP (model|thinking level) is unavailable/,
    );

    expect(fake.requests.some(({ method }) => method === 'session/prompt')).toBe(false);
  });

  it('reports a host-interrupted run as interrupted, not a transport error', async () => {
    const fake = createAcpProcess({ promptAutoComplete: false });
    spawnMock.mockReturnValue(fake.child);
    vi.spyOn(process, 'kill').mockImplementation(() => true);
    const events: AgentStreamEvent[] = [];
    const options = createSessionOptions({ onEvents: (batch) => events.push(...batch) });
    const session = new DevinAcpSession(options);
    const run = session.run();
    await vi.waitFor(() => {
      expect(fake.requests.some(({ method }) => method === 'session/prompt')).toBe(true);
    });

    session.interrupt();
    fake.child.emit('close', null, 'SIGINT');
    await run;

    expect(fake.requests.some(({ method }) => method === 'session/cancel')).toBe(true);
    expect(events.some(({ type }) => type === 'error')).toBe(false);
    expect(events).toContainEqual(
      expect.objectContaining({
        data: expect.objectContaining({ reason: 'interrupted', stopReason: 'cancelled' }),
        type: 'agent_runtime_end',
      }),
    );
  });
});