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.

39.6 KB
Raw Blame
import type * as ModelBankModule from 'model-bank';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { AiAgentService } from '../index';

const {
  mockCreateOperation,
  mockCreateServerAgentToolsEngine,
  mockMessageCreate,
  mockMessageUpdate,
} = vi.hoisted(() => ({
  mockCreateOperation: vi.fn(),
  mockCreateServerAgentToolsEngine: vi.fn().mockReturnValue({
    generateToolsDetailed: vi.fn().mockReturnValue({ enabledToolIds: [], tools: [] }),
    getEnabledPluginManifests: vi.fn().mockReturnValue(new Map()),
  }),
  mockMessageCreate: vi.fn(),
  mockMessageUpdate: vi.fn(),
}));

const { mockDeviceProxy } = vi.hoisted(() => ({
  mockDeviceProxy: {
    isConfigured: false,
    queryDeviceList: vi.fn().mockResolvedValue([]),
    queryDeviceSystemInfo: vi.fn().mockResolvedValue(undefined),
    // Present so a send-path regression that probes the device shows up as a
    // failed assertion rather than a "not a function" crash.
    statPath: vi.fn().mockResolvedValue({ exists: true, isDirectory: true }),
  },
}));

vi.mock('@/libs/trusted-client', () => ({
  generateTrustedClientToken: vi.fn().mockReturnValue(undefined),
  getTrustedClientTokenForSession: vi.fn().mockResolvedValue(undefined),
  isTrustedClientEnabled: vi.fn().mockReturnValue(false),
}));

vi.mock('@/database/models/message', () => ({
  MessageModel: vi.fn().mockImplementation(function () {
    return {
      create: mockMessageCreate,
      getLatestNonToolMessageId: vi.fn().mockResolvedValue(undefined),
      getLatestSpineMessageId: vi.fn().mockResolvedValue(undefined),
      query: vi.fn().mockResolvedValue([]),
      update: mockMessageUpdate,
    };
  }),
}));

vi.mock('@/database/models/agent', () => ({
  AgentModel: vi.fn().mockImplementation(function () {
    return {
      getAgentConfig: vi.fn().mockResolvedValue({
        chatConfig: {},
        files: [],
        id: 'agent-1',
        knowledgeBases: [],
        model: 'gpt-4',
        plugins: [],
        provider: 'openai',
        systemRole: 'You are a helpful assistant',
      }),
      queryAgents: vi.fn().mockResolvedValue([]),
    };
  }),
}));

vi.mock('@/server/services/agent', () => ({
  AgentService: vi.fn().mockImplementation(function () {
    return {
      getAgentConfig: vi.fn().mockResolvedValue({
        chatConfig: {},
        files: [],
        id: 'agent-1',
        knowledgeBases: [],
        model: 'gpt-4',
        plugins: [],
        provider: 'openai',
        systemRole: 'You are a helpful assistant',
      }),
    };
  }),
}));

vi.mock('@/database/models/plugin', () => ({
  PluginModel: vi.fn().mockImplementation(function () {
    return {
      query: vi.fn().mockResolvedValue([]),
    };
  }),
}));

const topicMock = {
  create: vi.fn().mockResolvedValue({ id: 'topic-1', metadata: undefined }),
  findById: vi.fn().mockResolvedValue(undefined),
  releaseTaskCallbackReservation: vi.fn().mockResolvedValue(undefined),
  tryReserveTaskCallback: vi.fn().mockResolvedValue(true),
  updateMetadata: vi.fn().mockResolvedValue(undefined),
};
vi.mock('@/database/models/topic', () => ({
  TopicModel: vi.fn().mockImplementation(function () {
    return topicMock;
  }),
}));

const { mockResolveProjectDirectory, mockResolveProjectDirectoryForTopic } = vi.hoisted(() => ({
  mockResolveProjectDirectory: vi.fn(),
  mockResolveProjectDirectoryForTopic: vi.fn().mockResolvedValue(undefined),
}));
vi.mock('@/database/models/projectWorkingDirectory', () => ({
  ProjectWorkingDirectoryModel: vi.fn().mockImplementation(function () {
    return {
      resolve: mockResolveProjectDirectory,
      resolveForTopic: mockResolveProjectDirectoryForTopic,
    };
  }),
}));

vi.mock('@/database/models/thread', () => ({
  ThreadModel: vi.fn().mockImplementation(function () {
    return {
      create: vi.fn(),
      findById: vi.fn(),
      update: vi.fn(),
    };
  }),
}));

vi.mock('@/server/services/agentRuntime', () => ({
  AgentRuntimeService: vi.fn().mockImplementation(function () {
    return {
      createOperation: mockCreateOperation,
    };
  }),
}));

vi.mock('@/server/services/market', () => ({
  MarketService: vi.fn().mockImplementation(function () {
    return {
      getLobehubSkillManifests: vi.fn().mockResolvedValue([]),
    };
  }),
}));

vi.mock('@/server/services/composio', () => ({
  ComposioService: vi.fn().mockImplementation(function () {
    return {
      getComposioManifests: vi.fn().mockResolvedValue([]),
    };
  }),
}));

vi.mock('@/server/services/file', () => ({
  FileService: vi.fn().mockImplementation(function () {
    return {
      uploadFromUrl: vi.fn(),
    };
  }),
}));

vi.mock('@/server/modules/Mecha', () => ({
  createServerAgentToolsEngine: mockCreateServerAgentToolsEngine,
  serverMessagesEngine: vi.fn().mockResolvedValue([{ content: 'test', role: 'user' }]),
}));

vi.mock('@/server/services/deviceGateway', () => ({
  deviceGateway: mockDeviceProxy,
}));

vi.mock('model-bank', async (importOriginal) => {
  const actual = await importOriginal<typeof ModelBankModule>();
  return {
    ...actual,
    LOBE_DEFAULT_MODEL_LIST: [
      {
        abilities: { functionCall: true, video: false, vision: true },
        id: 'gpt-4',
        providerId: 'openai',
      },
    ],
  };
});

describe('AiAgentService.execAgent - device auto-activation', () => {
  let service: AiAgentService;
  const mockDb = {} as any;
  const userId = 'test-user-id';

  beforeEach(() => {
    vi.clearAllMocks();
    topicMock.create.mockResolvedValue({ id: 'topic-1', metadata: undefined });
    topicMock.findById.mockResolvedValue(undefined);
    topicMock.updateMetadata.mockResolvedValue(undefined);
    mockMessageCreate.mockResolvedValue({ id: 'msg-1' });
    mockMessageUpdate.mockResolvedValue({});
    mockCreateOperation.mockResolvedValue({
      autoStarted: true,
      messageId: 'queue-msg-1',
      operationId: 'op-123',
      success: true,
    });
    // Reset device proxy state
    mockDeviceProxy.isConfigured = false;
    mockDeviceProxy.queryDeviceList.mockResolvedValue([]);

    service = new AiAgentService(mockDb, userId);
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  const onlineDevice = {
    deviceId: 'device-001',
    hostname: 'my-laptop',
    lastSeen: '2026-03-06T12:00:00.000Z',
    online: true,
    platform: 'linux' as const,
  };

  const onlineDevice2 = {
    deviceId: 'device-002',
    hostname: 'my-desktop',
    lastSeen: '2026-03-06T12:00:00.000Z',
    online: true,
    platform: 'darwin' as const,
  };

  const createAgentConfig = (agencyConfig: Record<string, unknown>) => ({
    agencyConfig,
    chatConfig: {},
    files: [],
    id: 'agent-1',
    knowledgeBases: [],
    model: 'gpt-4',
    plugins: [],
    provider: 'openai',
    systemRole: 'You are a helpful assistant',
  });

  const createOwnerBotContext = () => ({
    applicationId: 'app-1',
    isOwner: true,
    platform: 'discord',
    platformThreadId: 'discord:guild-1:channel-1',
    senderExternalUserId: 'owner-id',
  });

  // Override the agent's agencyConfig and rebuild the service. Auto-activation
  // is now exclusive to `executionTarget: 'auto'` — the default (`local`) never
  // grabs a device — so the auto-activation specs opt in explicitly.
  const useAgencyConfig = async (agencyConfig: Record<string, unknown>) => {
    const { AgentService } = await import('@/server/services/agent');
    vi.mocked(AgentService).mockImplementation(function () {
      return {
        getAgentConfig: vi.fn().mockResolvedValue(createAgentConfig(agencyConfig)),
      } as any;
    });
    service = new AiAgentService(mockDb, userId);
  };

  describe('IM/Bot scenario with botContext', () => {
    it('should auto-activate when exactly one device is online (executionTarget: auto)', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        botContext: createOwnerBotContext() as any,
        prompt: 'List my files',
      });

      expect(mockCreateOperation).toHaveBeenCalled();
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });

    it('should NOT auto-activate when multiple devices are online (executionTarget: auto)', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice, onlineDevice2]);
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        botContext: createOwnerBotContext() as any,
        prompt: 'List my files',
      });

      expect(mockCreateOperation).toHaveBeenCalled();
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
    });

    it('should NOT auto-activate when no devices are online (executionTarget: auto)', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([]);
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        botContext: createOwnerBotContext() as any,
        prompt: 'List my files',
      });

      expect(mockCreateOperation).toHaveBeenCalled();
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
    });

    it('should auto-activate the single online device by default (executionTarget unset → auto)', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await useAgencyConfig({}); // unset executionTarget → default `auto`

      await service.execAgent({
        agentId: 'agent-1',
        botContext: createOwnerBotContext() as any,
        prompt: 'List my files',
      });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });
  });

  describe('IM/Bot scenario with discordContext', () => {
    it('should auto-activate when exactly one device is online (executionTarget: auto)', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        discordContext: { channelId: 'ch-1', guildId: 'guild-1' },
        prompt: 'Check system info',
      });

      expect(mockCreateOperation).toHaveBeenCalled();
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });
  });

  describe('Web UI scenario (no botContext/discordContext)', () => {
    // In `auto` mode a single online device is activated up-front, so the
    // local-system system prompt's {{workingDirectory}} / {{hostname}}
    // placeholders resolve instead of reaching the LLM as literals. Multi-device
    // users still pick explicitly (the model selects via the remote-device
    // tool). An unset target defaults to `auto`; an explicit `local` never
    // auto-activates.
    it('should auto-activate the only online device (executionTarget: auto)', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'List my files',
      });

      expect(mockCreateOperation).toHaveBeenCalled();
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });

    it('should NOT auto-activate when multiple devices are online (executionTarget: auto)', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice, onlineDevice2]);
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'List my files',
      });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
    });

    it('should NOT auto-activate when no devices are online (executionTarget: auto)', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([]);
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'List my files',
      });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
    });

    it('should auto-activate the single online device by default (unset → auto)', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await useAgencyConfig({}); // unset executionTarget → default `auto`

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'List my files',
      });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });

    it('should NOT auto-activate the single online device for an explicit local target', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await useAgencyConfig({ executionTarget: 'local' });

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'List my files',
      });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
    });
  });

  describe('executionTarget gating (none / sandbox never route to a device)', () => {
    const overrideAgencyConfig = async (agencyConfig: Record<string, unknown>) => {
      const { AgentService } = await import('@/server/services/agent');
      vi.mocked(AgentService).mockImplementation(function () {
        return {
          getAgentConfig: vi.fn().mockResolvedValue(createAgentConfig(agencyConfig)),
        } as any;
      });
      service = new AiAgentService(mockDb, userId);
    };

    it('should NOT auto-activate the single online device when executionTarget is none', async () => {
      // regression: 无设备 used to be bypassed by single-device auto-activation
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await overrideAgencyConfig({ executionTarget: 'none' });

      await service.execAgent({ agentId: 'agent-1', prompt: 'List my files' });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
    });

    it('should NOT activate a bound online device when executionTarget is none', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await overrideAgencyConfig({ boundDeviceId: 'device-001', executionTarget: 'none' });

      await service.execAgent({ agentId: 'agent-1', prompt: 'List my files' });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
    });

    it('should NOT activate any device when executionTarget is sandbox', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await overrideAgencyConfig({ boundDeviceId: 'device-001', executionTarget: 'sandbox' });

      await service.execAgent({ agentId: 'agent-1', prompt: 'List my files' });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
    });
  });

  describe('boundDeviceId scenario', () => {
    it('should use boundDeviceId when device is online', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);

      // Override the agent config mock to include boundDeviceId
      const { AgentService } = await import('@/server/services/agent');
      vi.mocked(AgentService).mockImplementation(function () {
        return {
          getAgentConfig: vi
            .fn()
            .mockResolvedValue(createAgentConfig({ boundDeviceId: 'device-001' })),
        } as any;
      });

      service = new AiAgentService(mockDb, userId);

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'Run a command',
      });

      expect(mockCreateOperation).toHaveBeenCalled();
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });

    it('should NOT activate boundDeviceId when no devices are online', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([]);

      const { AgentService } = await import('@/server/services/agent');
      vi.mocked(AgentService).mockImplementation(function () {
        return {
          getAgentConfig: vi
            .fn()
            .mockResolvedValue(createAgentConfig({ boundDeviceId: 'device-001' })),
        } as any;
      });

      service = new AiAgentService(mockDb, userId);

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'Run a command',
      });

      expect(mockCreateOperation).toHaveBeenCalled();
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
    });
  });

  describe('topic and explicit device binding', () => {
    it('uses the shared fixed device even when the request asks for another device', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice, onlineDevice2]);
      await useAgencyConfig({
        boundDeviceId: 'device-001',
        executionTargetSelectionPolicy: 'fixed',
        executionTarget: 'device',
      });
      service = new AiAgentService(mockDb, userId, { workspaceId: 'workspace-1' });

      await service.execAgent({
        agentId: 'agent-1',
        deviceId: 'device-002',
        prompt: 'Run a command',
      });

      expect(mockCreateOperation.mock.calls[0][0].activeDeviceId).toBe('device-001');
      expect(topicMock.create).toHaveBeenCalledWith(
        expect.objectContaining({
          metadata: expect.objectContaining({ boundDeviceId: 'device-001' }),
        }),
        undefined,
      );
    });

    it('keeps a fixed sandbox target when the request asks for a device', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await useAgencyConfig({
        executionTarget: 'sandbox',
        executionTargetSelectionPolicy: 'fixed',
      });
      service = new AiAgentService(mockDb, userId, { workspaceId: 'workspace-1' });

      await service.execAgent({
        agentId: 'agent-1',
        deviceId: 'device-001',
        prompt: 'Run a command',
      });

      expect(mockCreateOperation.mock.calls[0][0].activeDeviceId).toBeUndefined();
      expect(topicMock.create).toHaveBeenCalledWith(
        expect.objectContaining({ metadata: undefined }),
        undefined,
      );
    });

    it('fails before operation creation when the shared fixed device is offline', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice2]);
      await useAgencyConfig({
        boundDeviceId: 'device-001',
        executionTargetSelectionPolicy: 'fixed',
        executionTarget: 'device',
      });
      service = new AiAgentService(mockDb, userId, { workspaceId: 'workspace-1' });

      await expect(
        service.execAgent({ agentId: 'agent-1', prompt: 'Run a command' }),
      ).rejects.toMatchObject({
        cause: {
          data: {
            code: 'DEVICE_NOT_FOUND',
            deviceId: 'device-001',
            retryable: true,
            scope: 'workspace',
            workspaceId: 'workspace-1',
          },
        },
        code: 'PRECONDITION_FAILED',
      });

      expect(mockCreateOperation).not.toHaveBeenCalled();
      expect(mockMessageUpdate).toHaveBeenCalledWith(
        'msg-1',
        expect.objectContaining({
          error: expect.objectContaining({ message: 'Fixed agent device unavailable' }),
        }),
      );
    });

    it('should prefer explicit deviceId over topic and agent bindings when online', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice, onlineDevice2]);
      topicMock.findById.mockResolvedValue({ metadata: { boundDeviceId: 'device-002' } });

      const { AgentService } = await import('@/server/services/agent');
      vi.mocked(AgentService).mockImplementation(function () {
        return {
          getAgentConfig: vi
            .fn()
            .mockResolvedValue(createAgentConfig({ boundDeviceId: 'device-002' })),
        } as any;
      });

      service = new AiAgentService(mockDb, userId);

      await service.execAgent({
        agentId: 'agent-1',
        appContext: { topicId: 'topic-existing' },
        deviceId: 'device-001',
        prompt: 'Run a command',
      });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
      // updateMetadata is called for runningOperation persistence, but not for device binding
      expect(topicMock.updateMetadata).not.toHaveBeenCalledWith(
        expect.anything(),
        expect.objectContaining({ boundDeviceId: expect.anything() }),
      );
    });

    // Verifies topic-stored metadata.boundDeviceId is NOT silently reused as
    // the runtime bound device. Setup: `auto` mode, topic.metadata says
    // device-002, but the only online device is device-001. If the topic
    // metadata were reused as boundDeviceId, activeDeviceId would be undefined
    // (device-002 is offline). Auto-activation instead picks the single online
    // device (device-001) — proving the topic's stale metadata wasn't honored.
    it('should not reuse topic boundDeviceId when no explicit deviceId is provided', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      topicMock.findById.mockResolvedValue({ metadata: { boundDeviceId: 'device-002' } });
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        appContext: { topicId: 'topic-existing' },
        prompt: 'Run a command',
      });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).not.toBe('device-002');
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });

    // An unset target is the same default as `auto`, so it must not resurrect a
    // stale topic device either: the default agent re-picks the single online
    // device instead of staying unrouted on the offline one.
    it('should not reuse topic boundDeviceId for an unset target either', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      topicMock.findById.mockResolvedValue({ metadata: { boundDeviceId: 'device-002' } });
      await useAgencyConfig({});

      await service.execAgent({
        agentId: 'agent-1',
        appContext: { topicId: 'topic-existing' },
        prompt: 'Run a command',
      });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });

    // An explicit device on the request is a promise to the caller, so the new
    // topic records that the binding was pinned rather than auto-picked — a
    // later turn on an `auto` agent then stays on it instead of choosing
    // whichever machine happens to be online.
    it('should pin an explicitly requested device on the new topic metadata', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        deviceId: 'device-001',
        prompt: 'Run a command',
      });

      expect(topicMock.create).toHaveBeenCalledWith(
        expect.objectContaining({
          metadata: expect.objectContaining({
            boundDeviceId: 'device-001',
            deviceBindingPinned: true,
          }),
        }),
        undefined,
      );
    });

    // Same setup as the "not silently reused" spec above, except the topic
    // records that the device was chosen explicitly. An `auto` agent must now
    // honour it even though both machines are online.
    it('should keep a pinned topic device on later turns for an auto agent', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice, onlineDevice2]);
      topicMock.findById.mockResolvedValue({
        metadata: { boundDeviceId: 'device-002', deviceBindingPinned: true },
      });
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        appContext: { topicId: 'topic-existing' },
        prompt: 'Run a command',
      });

      expect(mockCreateOperation.mock.calls[0][0].activeDeviceId).toBe('device-002');
    });

    it('should keep explicit topic binding when the bound device is offline', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice2]);

      service = new AiAgentService(mockDb, userId);

      await service.execAgent({
        agentId: 'agent-1',
        deviceId: 'device-001',
        prompt: 'Run a command',
      });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
      expect(topicMock.create).toHaveBeenCalledWith(
        expect.objectContaining({
          metadata: expect.objectContaining({ boundDeviceId: 'device-001' }),
        }),
        undefined,
      );
    });
  });

  describe('project directory binding', () => {
    it('keeps a project topic without a directory on the device it already ran on', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice, onlineDevice2]);
      // Started outside any directory: the first run stamped its device.
      topicMock.findById.mockResolvedValue({
        id: 'topic-1',
        metadata: { boundDeviceId: 'device-002', workingDirectory: '/repo' },
        projectId: 'project-1',
        projectWorkingDirectoryId: null,
      });
      // The agent default points elsewhere; the conversation's pin must win.
      await useAgencyConfig({ boundDeviceId: 'device-001', executionTarget: 'device' });

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'Keep working',
        appContext: { topicId: 'topic-1' },
      });

      expect(mockResolveProjectDirectoryForTopic).not.toHaveBeenCalled();
      expect(mockCreateOperation.mock.calls[0][0].activeDeviceId).toBe('device-002');
    });

    it('does not consult the project resolver for plain device-bound topics', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      topicMock.findById.mockResolvedValue({
        id: 'topic-1',
        metadata: { boundDeviceId: 'device-001' },
      });

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'Keep working',
        appContext: { topicId: 'topic-1' },
      });

      expect(mockResolveProjectDirectoryForTopic).not.toHaveBeenCalled();
      expect(mockCreateOperation).toHaveBeenCalled();
    });

    // Regression: the send path used to `await deviceGateway.statPath(...)` to
    // pre-check the directory, putting a full device WebSocket round trip (up to
    // its 8s timeout) in front of every message and rejecting the send when the
    // device was merely slow to answer. Liveness is the dispatch's to discover.
    it('routes a project-directory topic without a device round trip on the send path', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      topicMock.findById.mockResolvedValue({
        id: 'topic-1',
        metadata: { boundDeviceId: 'device-001', workingDirectory: '/repo' },
        projectId: 'project-1',
        projectWorkingDirectoryId: 'dir-1',
      });
      mockResolveProjectDirectoryForTopic.mockResolvedValueOnce({
        deviceId: 'device-001',
        path: '/repo',
        permission: 'readWrite',
        platform: 'darwin',
      });

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'Keep working',
        appContext: { topicId: 'topic-1' },
      });

      expect(mockDeviceProxy.statPath).not.toHaveBeenCalled();
      expect(mockCreateOperation).toHaveBeenCalled();
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });

    // Regression: the directory lives on one device, so an offline device has to
    // reject the send *before* the user/assistant rows are persisted. A
    // rejection after persistence leaves a failed turn durable server-side while
    // the client treats the send as unaccepted, rolls its optimistic pair back
    // and keeps the draft — and retrying then stacks duplicate failed turns.
    it('rejects an offline bound device before persisting the turn', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice2]);
      topicMock.findById.mockResolvedValue({
        id: 'topic-1',
        metadata: { boundDeviceId: 'device-001', workingDirectory: '/repo' },
        projectId: 'project-1',
        projectWorkingDirectoryId: 'dir-1',
      });
      mockResolveProjectDirectoryForTopic.mockResolvedValueOnce({
        deviceId: 'device-001',
        path: '/repo',
        permission: 'readWrite',
        platform: 'darwin',
      });
      mockMessageCreate.mockClear();
      mockCreateOperation.mockClear();

      const error: unknown = await service
        .execAgent({
          agentId: 'agent-1',
          prompt: 'Run a command',
          appContext: { topicId: 'topic-1' },
        })
        .catch((thrown) => thrown);

      // Asserted before the message so a post-persistence rejection fails here,
      // on the divergence itself rather than on the error text.
      expect(mockMessageCreate).not.toHaveBeenCalled();
      expect(mockCreateOperation).not.toHaveBeenCalled();
      expect(error).toBeInstanceOf(Error);
      expect((error as Error).message).toBe(
        'Device is offline or working directory is unavailable',
      );
    });
  });

  describe('new topic in a staged project directory', () => {
    const stagedDirectory = {
      deviceId: 'device-001',
      id: 'dir-1',
      path: '/repo',
      permission: 'readWrite',
      platform: 'darwin',
      projectId: 'project-1',
    };

    // Regression: a blank conversation opened from a project group's "+" was
    // created without its directory — the first send dropped the staged
    // binding, so the topic left the project and ran on the agent default.
    it('binds the created topic to the directory and runs on its device', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice, onlineDevice2]);
      mockResolveProjectDirectory.mockResolvedValueOnce(stagedDirectory);

      await service.execAgent({
        agentId: 'agent-1',
        appContext: { projectWorkingDirectoryId: 'dir-1' },
        prompt: 'Start here',
      });

      expect(mockResolveProjectDirectory).toHaveBeenCalledWith('dir-1');
      const createArgs = topicMock.create.mock.calls.at(-1)![0];
      expect(createArgs.projectId).toBe('project-1');
      expect(createArgs.projectWorkingDirectoryId).toBe('dir-1');
      expect(createArgs.metadata?.boundDeviceId).toBe('device-001');
      expect(createArgs.metadata?.workingDirectory).toBe('/repo');
      expect(mockCreateOperation.mock.calls.at(-1)![0].activeDeviceId).toBe('device-001');
    });

    it('rejects an offline directory device before creating the topic', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice2]);
      mockResolveProjectDirectory.mockResolvedValueOnce(stagedDirectory);
      topicMock.create.mockClear();
      mockCreateOperation.mockClear();

      await expect(
        service.execAgent({
          agentId: 'agent-1',
          appContext: { projectWorkingDirectoryId: 'dir-1' },
          prompt: 'Start here',
        }),
      ).rejects.toThrow('Device is offline or working directory is unavailable');
      expect(topicMock.create).not.toHaveBeenCalled();
      expect(mockCreateOperation).not.toHaveBeenCalled();
    });
  });

  describe('gateway not configured', () => {
    it('should never set activeDeviceId when gateway is not configured', async () => {
      mockDeviceProxy.isConfigured = false;

      await service.execAgent({
        agentId: 'agent-1',
        botContext: createOwnerBotContext() as any,
        prompt: 'List my files',
      });

      expect(mockCreateOperation).toHaveBeenCalled();
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBeUndefined();
      expect(mockDeviceProxy.queryDeviceList).not.toHaveBeenCalled();
    });
  });

  describe('topic metadata binding', () => {
    it('should include requested deviceId when creating a new topic', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);

      await service.execAgent({
        agentId: 'agent-1',
        deviceId: 'device-001',
        prompt: 'Run with device',
      });

      expect(topicMock.create).toHaveBeenCalled();
      const createArgs = topicMock.create.mock.calls[0][0];
      expect(createArgs.metadata?.boundDeviceId).toBe('device-001');
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });

    // Mirrors the "should not reuse topic boundDeviceId" test above with a
    // different mock shape. `auto` mode, topic metadata stores device-002, but
    // only device-001 is online; if topic metadata leaked into boundDeviceId,
    // activeDeviceId would be undefined (since device-002 is offline). The
    // auto-activation picks device-001 instead, confirming the stale
    // topic.metadata.boundDeviceId path is dead.
    it('should not reuse topic metadata bound device when no deviceId is supplied', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      topicMock.findById.mockResolvedValue({
        id: 'topic-1',
        metadata: { boundDeviceId: 'device-002' },
      });
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'Use topic device',
        appContext: { topicId: 'topic-1' },
      });

      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).not.toBe('device-002');
      expect(createOpArgs.activeDeviceId).toBe('device-001');
    });

    it('should not update topic metadata when a new deviceId is provided for existing topic', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice2]);
      topicMock.findById.mockResolvedValue({
        id: 'topic-1',
        metadata: { boundDeviceId: 'device-old' },
      });

      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'Switch device',
        appContext: { topicId: 'topic-1' },
        deviceId: 'device-002',
      });

      // updateMetadata is called for runningOperation persistence, but not for device binding
      expect(topicMock.updateMetadata).not.toHaveBeenCalledWith(
        expect.anything(),
        expect.objectContaining({ boundDeviceId: expect.anything() }),
      );
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-002');
    });
  });

  describe('Remote Device tool injection when device is auto-activated', () => {
    it('should mark autoActivated when single device is auto-activated (IM/Bot, executionTarget: auto)', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);
      await useAgencyConfig({ executionTarget: 'auto' });

      await service.execAgent({
        agentId: 'agent-1',
        botContext: createOwnerBotContext() as any,
        prompt: 'List my files',
      });

      const toolsEngineArgs = mockCreateServerAgentToolsEngine.mock.calls[0][1];
      const createOpArgs = mockCreateOperation.mock.calls[0][0];
      expect(createOpArgs.activeDeviceId).toBe('device-001');
      // Device auto-activated → Remote Device tool should be suppressed
      expect(toolsEngineArgs.deviceContext.autoActivated).toBe(true);
    });

    it('should mark autoActivated when boundDeviceId matches an online device', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice]);

      const { AgentService } = await import('@/server/services/agent');
      vi.mocked(AgentService).mockImplementation(function () {
        return {
          getAgentConfig: vi
            .fn()
            .mockResolvedValue(createAgentConfig({ boundDeviceId: 'device-001' })),
        } as any;
      });

      service = new AiAgentService(mockDb, userId);
      await service.execAgent({
        agentId: 'agent-1',
        prompt: 'Run a command',
      });

      const toolsEngineArgs = mockCreateServerAgentToolsEngine.mock.calls[0][1];
      expect(toolsEngineArgs.deviceContext.autoActivated).toBe(true);
    });

    it('should NOT mark autoActivated when multiple devices are online', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([onlineDevice, onlineDevice2]);

      // Restore default AgentService mock (previous test overrides with boundDeviceId)
      const { AgentService } = await import('@/server/services/agent');
      vi.mocked(AgentService).mockImplementation(function () {
        return {
          getAgentConfig: vi.fn().mockResolvedValue({
            chatConfig: {},
            files: [],
            id: 'agent-1',
            knowledgeBases: [],
            model: 'gpt-4',
            plugins: [],
            provider: 'openai',
            systemRole: 'You are a helpful assistant',
          }),
        } as any;
      });
      service = new AiAgentService(mockDb, userId);

      await service.execAgent({
        agentId: 'agent-1',
        botContext: createOwnerBotContext() as any,
        prompt: 'List my files',
      });

      const toolsEngineArgs = mockCreateServerAgentToolsEngine.mock.calls[0][1];
      expect(toolsEngineArgs.deviceContext.autoActivated).toBeUndefined();
    });

    it('should NOT mark autoActivated when no devices are online', async () => {
      mockDeviceProxy.isConfigured = true;
      mockDeviceProxy.queryDeviceList.mockResolvedValue([]);

      await service.execAgent({
        agentId: 'agent-1',
        botContext: createOwnerBotContext() as any,
        prompt: 'List my files',
      });

      const toolsEngineArgs = mockCreateServerAgentToolsEngine.mock.calls[0][1];
      expect(toolsEngineArgs.deviceContext.autoActivated).toBeUndefined();
    });
  });
});