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.

17.3 KB
Raw Blame
import type { AgentOperationStatus } from '@lobechat/types';
import { describe, expect, it, vi } from 'vitest';

import {
  AgentRelayHopLimitError,
  assertAgentRelayHop,
  MAX_AGENT_RELAY_HOPS,
  resolveAgentSenderFromOperation,
} from './sourceAttribution';

const CALLER = 'user-1';

const buildDeps = (overrides?: {
  agentDisplay?: unknown;
  /**
   * Visibility per agent id — a scalar for "everything is the same", a record
   * when the source and the destination differ.
   */
  agentVisibility?: 'private' | 'public' | Record<string, 'private' | 'public' | null>;
  /** Visibility per chat group id. */
  groupVisibility?: Record<string, 'private' | 'public' | null>;
  operation?:
    | null
    | undefined
    | {
        agentId?: null | string;
        chatGroupId?: null | string;
        status?: AgentOperationStatus;
        threadId?: null | string;
        topicId?: null | string;
        userId: string;
        workspaceId?: null | string;
      };
  topic?:
    | null
    | undefined
    | { agentId?: null | string; title?: null | string; workspaceId?: null | string };
  /** Hop of the newest user message on the sending conversation; rejects to simulate a failed read. */
  triggerHop?: Error | number;
  workspaceId?: string;
}) => ({
  destinationAgentId: 'agt-destination',
  findAgentDisplayFields: vi.fn().mockResolvedValue(overrides?.agentDisplay ?? undefined),
  findAgentVisibility: vi.fn().mockImplementation(async (agentId: string) => {
    const visibility = overrides?.agentVisibility;
    if (!visibility) return null;
    return typeof visibility === 'string' ? visibility : (visibility[agentId] ?? null);
  }),
  findChatGroupVisibility: vi
    .fn()
    .mockImplementation(
      async (chatGroupId: string) => overrides?.groupVisibility?.[chatGroupId] ?? null,
    ),
  findOperation: vi.fn().mockResolvedValue(
    overrides?.operation === undefined || overrides.operation === null
      ? null
      : // Default to an in-flight run so each case states only what it exercises.
        { status: 'running', ...overrides.operation },
  ),
  findTopic: vi.fn().mockResolvedValue(overrides?.topic === undefined ? null : overrides.topic),
  findTriggerHop:
    overrides?.triggerHop instanceof Error
      ? vi.fn().mockRejectedValue(overrides.triggerHop)
      : vi.fn().mockResolvedValue(overrides?.triggerHop),
  userId: CALLER,
  workspaceId: overrides?.workspaceId,
});

describe('resolveAgentSenderFromOperation', () => {
  it('stamps nothing without an operation id, and never reads the models', async () => {
    const deps = buildDeps();

    await expect(resolveAgentSenderFromOperation(undefined, deps)).resolves.toBeUndefined();
    expect(deps.findOperation).not.toHaveBeenCalled();
    expect(deps.findTopic).not.toHaveBeenCalled();
  });

  it('stamps nothing for an operation it cannot resolve', async () => {
    const deps = buildDeps({ operation: null });

    await expect(resolveAgentSenderFromOperation('op-unknown', deps)).resolves.toBeUndefined();
    expect(deps.findTopic).not.toHaveBeenCalled();
  });

  /**
   * The trust rule. `AgentOperationModel.findById` is workspace-scoped, so in a
   * workspace it also resolves a sibling member's operation — the row's own
   * `userId` is what keeps a member from rendering arbitrary text as another
   * member's agent.
   */
  it("stamps nothing for a sibling member's operation in the same workspace", async () => {
    const deps = buildDeps({
      operation: { topicId: 'tpc-victim', userId: 'user-2' },
      topic: { agentId: 'agt-victim', title: 'Victim topic' },
    });

    await expect(resolveAgentSenderFromOperation('op-sibling', deps)).resolves.toBeUndefined();
    expect(deps.findTopic).not.toHaveBeenCalled();
    expect(deps.findAgentDisplayFields).not.toHaveBeenCalled();
  });

  it('stamps nothing when the operation has no topic', async () => {
    const deps = buildDeps({ operation: { topicId: null, userId: CALLER } });

    await expect(resolveAgentSenderFromOperation('op-no-topic', deps)).resolves.toBeUndefined();
    expect(deps.findTopic).not.toHaveBeenCalled();
  });

  it('stamps nothing when the topic is gone or has no owning agent', async () => {
    const missing = buildDeps({ operation: { topicId: 'tpc-1', userId: CALLER }, topic: null });
    await expect(resolveAgentSenderFromOperation('op-1', missing)).resolves.toBeUndefined();

    const orphans = buildDeps({
      operation: { topicId: 'tpc-1', userId: CALLER },
      topic: { agentId: null, title: 'Orphan topic' },
    });
    await expect(resolveAgentSenderFromOperation('op-1', orphans)).resolves.toBeUndefined();
  });

  /**
   * `LOBEHUB_OPERATION_ID` is an ambient env var: a shell, or a long-lived child
   * process, keeps it after its run finishes, and the public API can replay any
   * prior id. A settled run must therefore stop granting authorship.
   */
  it('carries the group and thread the launching turn actually ran in', async () => {
    const deps = buildDeps({
      operation: {
        agentId: 'agt-coco',
        chatGroupId: 'grp_1',
        threadId: 'thd_1',
        topicId: 'tpc-source',
        userId: CALLER,
      },
      topic: { agentId: 'agt-coco', title: 'Source' },
    });

    const result = await resolveAgentSenderFromOperation('op-1', deps);

    // The link is built from these: without them a group source opens the
    // supervisor's conversation and a thread source opens the main transcript.
    expect(result).toMatchObject({ chatGroupId: 'grp_1', threadId: 'thd_1' });
  });

  /**
   * A group topic may carry no owning agent (legacy or API-created rows) while
   * the operation still names the agent that ran — the group route needs no topic
   * agent, so refusing outright would drop those launches.
   */
  it('attributes a group topic that carries no owning agent', async () => {
    const deps = buildDeps({
      operation: {
        agentId: 'agt-coco',
        chatGroupId: 'grp_1',
        topicId: 'tpc-group',
        userId: CALLER,
      },
      topic: { agentId: null, title: 'Group topic' },
    });

    await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toMatchObject({
      agentId: 'agt-coco',
      chatGroupId: 'grp_1',
      topicId: 'tpc-group',
      topicTitle: 'Group topic',
    });
  });

  it('still refuses a one-to-one topic that resolves no owning agent', async () => {
    // The agent route would be invalid, so there is no destination to offer.
    const deps = buildDeps({
      operation: { agentId: 'agt-coco', topicId: 'tpc-1', userId: CALLER },
      topic: { agentId: null, title: 'Orphan' },
    });

    await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toBeUndefined();
  });

  it('keeps the origin when the destination is personal', async () => {
    const deps = buildDeps({
      operation: { agentId: 'agt-coco', topicId: 'tpc-1', userId: CALLER },
      topic: { agentId: 'agt-coco', title: 'Private topic' },
    });

    await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toMatchObject({
      topicId: 'tpc-1',
      topicTitle: 'Private topic',
    });
  });

  it('keeps the origin when a shared destination can read the source too', async () => {
    const deps = buildDeps({
      agentVisibility: { 'agt-coco': 'public', 'agt-destination': 'public' },
      operation: { agentId: 'agt-coco', topicId: 'tpc-1', userId: CALLER, workspaceId: 'ws_1' },
      topic: { agentId: 'agt-coco', title: 'Shared topic', workspaceId: 'ws_1' },
      workspaceId: 'ws_1',
    });

    await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toMatchObject({
      topicId: 'tpc-1',
      topicTitle: 'Shared topic',
    });
  });

  /**
   * The leak this guards: a workspace-shared agent's bubble is readable by every
   * member, and a topic's name is frequently derived from the user's own prompt —
   * so a private source must not be named there. The authorship stays.
   */
  it('drops the origin when a shared destination cannot read the private source', async () => {
    const deps = buildDeps({
      agentVisibility: { 'agt-coco': 'private', 'agt-destination': 'public' },
      operation: { agentId: 'agt-coco', topicId: 'tpc-1', userId: CALLER, workspaceId: 'ws_1' },
      topic: { agentId: 'agt-coco', title: 'Private prompt title', workspaceId: 'ws_1' },
      workspaceId: 'ws_1',
    });

    const result = await resolveAgentSenderFromOperation('op-1', deps);

    expect(result).toMatchObject({ agentId: 'agt-coco' });
    expect(result?.topicId).toBeUndefined();
    expect(result?.topicTitle).toBeUndefined();
  });

  /**
   * The executing agent is not who can read the source: a public `callSubAgent`
   * child runs on its private parent's topic, and the link opens the parent's
   * conversation — so the parent's visibility decides.
   */
  it("drops the origin when a public child runs on a private parent's topic", async () => {
    const deps = buildDeps({
      agentVisibility: {
        'agt-child': 'public',
        'agt-destination': 'public',
        'agt-parent': 'private',
      },
      operation: { agentId: 'agt-child', topicId: 'tpc-1', userId: CALLER, workspaceId: 'ws_1' },
      topic: { agentId: 'agt-parent', title: 'Private parent topic', workspaceId: 'ws_1' },
      workspaceId: 'ws_1',
    });

    const result = await resolveAgentSenderFromOperation('op-1', deps);

    expect(result).toMatchObject({ agentId: 'agt-child' });
    expect(result?.topicId).toBeUndefined();
    expect(result?.topicTitle).toBeUndefined();
  });

  it('drops the origin when a public member runs inside a private group', async () => {
    const deps = buildDeps({
      agentVisibility: 'public',
      groupVisibility: { 'grp-1': 'private' },
      operation: {
        agentId: 'agt-coco',
        chatGroupId: 'grp-1',
        topicId: 'tpc-1',
        userId: CALLER,
        workspaceId: 'ws_1',
      },
      topic: { agentId: 'agt-coco', title: 'Private group topic', workspaceId: 'ws_1' },
      workspaceId: 'ws_1',
    });

    const result = await resolveAgentSenderFromOperation('op-1', deps);

    expect(result).toMatchObject({ agentId: 'agt-coco', chatGroupId: 'grp-1' });
    expect(result?.topicId).toBeUndefined();
    expect(result?.topicTitle).toBeUndefined();
  });

  it('keeps the origin of a public group even when its topic has no owning agent', async () => {
    const deps = buildDeps({
      agentVisibility: { 'agt-destination': 'public' },
      groupVisibility: { 'grp-1': 'public' },
      operation: {
        agentId: 'agt-coco',
        chatGroupId: 'grp-1',
        topicId: 'tpc-1',
        userId: CALLER,
        workspaceId: 'ws_1',
      },
      topic: { agentId: null, title: 'Group topic', workspaceId: 'ws_1' },
      workspaceId: 'ws_1',
    });

    await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toMatchObject({
      topicId: 'tpc-1',
      topicTitle: 'Group topic',
    });
    expect(deps.findChatGroupVisibility).toHaveBeenCalledWith('grp-1');
  });

  it("drops the origin when the source lives outside the destination's workspace", async () => {
    const deps = buildDeps({
      agentVisibility: 'public',
      operation: { agentId: 'agt-coco', topicId: 'tpc-1', userId: CALLER, workspaceId: null },
      topic: { agentId: 'agt-coco', title: 'Personal topic', workspaceId: null },
      workspaceId: 'ws_1',
    });

    await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toMatchObject({
      agentId: 'agt-coco',
      topicId: undefined,
      topicTitle: undefined,
    });
  });

  it('stamps nothing for an operation that has already settled', async () => {
    for (const status of ['abandoned', 'done', 'error', 'interrupted'] as const) {
      const deps = buildDeps({
        operation: { agentId: 'agt-coco', status, topicId: 'tpc-source', userId: CALLER },
        topic: { agentId: 'agt-coco', title: 'Source' },
      });

      await expect(resolveAgentSenderFromOperation('op-settled', deps)).resolves.toBeUndefined();
      expect(deps.findTopic).not.toHaveBeenCalled();
    }
  });

  it('still attributes an operation that is only waiting', async () => {
    for (const status of ['idle', 'waiting_for_human', 'waiting_for_async_tool'] as const) {
      const deps = buildDeps({
        operation: { agentId: 'agt-coco', status, topicId: 'tpc-source', userId: CALLER },
        topic: { agentId: 'agt-coco', title: 'Source' },
      });

      await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toMatchObject({
        agentId: 'agt-coco',
      });
    }
  });

  it("reads the topic and agent from the caller's own operation row", async () => {
    const deps = buildDeps({
      agentDisplay: { avatar: '🐶', name: 'Coco', title: 'Product Assistant' },
      operation: { agentId: 'agt-coco', topicId: 'tpc-source', userId: CALLER },
      topic: { agentId: 'agt-coco', title: '帮我评估本周发布的风险' },
    });

    await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toEqual({
      agentId: 'agt-coco',
      hop: 1,
      avatar: '🐶',
      name: 'Coco',
      title: 'Product Assistant',
      topicAgentId: 'agt-coco',
      topicId: 'tpc-source',
      topicTitle: '帮我评估本周发布的风险',
    });
    expect(deps.findTopic).toHaveBeenCalledWith('tpc-source');
  });

  /**
   * A heterogeneous `callSubAgent` child executes in an isolation thread on its
   * SPAWNER's topic, so the operation's agent and the topic's owner differ. The
   * child sent the turn; the parent owns the conversation the link points at.
   */
  it("attributes a sub-agent child, and links back to the topic's own agent", async () => {
    const deps = buildDeps({
      agentDisplay: { avatar: '🤖', name: 'Release Bot', title: 'Release Bot' },
      operation: { agentId: 'agt-child', topicId: 'tpc-parent', userId: CALLER },
      topic: { agentId: 'agt-parent', title: 'Parent thread' },
    });

    await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toEqual({
      agentId: 'agt-child',
      hop: 1,
      avatar: '🤖',
      name: 'Release Bot',
      title: 'Release Bot',
      topicAgentId: 'agt-parent',
      topicId: 'tpc-parent',
      topicTitle: 'Parent thread',
    });
    expect(deps.findAgentDisplayFields).toHaveBeenCalledWith('agt-child');
  });

  it("falls back to the topic's owner when the operation names no agent", async () => {
    const deps = buildDeps({
      operation: { agentId: null, topicId: 'tpc-source', userId: CALLER },
      topic: { agentId: 'agt-coco', title: 'Source' },
    });

    await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toEqual({
      agentId: 'agt-coco',
      hop: 1,
      topicAgentId: 'agt-coco',
      topicId: 'tpc-source',
      topicTitle: 'Source',
    });
  });

  it('degrades to the bare ids when the sender config cannot be read', async () => {
    const deps = buildDeps({
      operation: { agentId: 'agt-coco', topicId: 'tpc-source', userId: CALLER },
      topic: { agentId: 'agt-coco', title: 'Source' },
    });
    deps.findAgentDisplayFields.mockRejectedValue(new Error('agent deleted'));

    await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toEqual({
      agentId: 'agt-coco',
      hop: 1,
      topicAgentId: 'agt-coco',
      topicId: 'tpc-source',
      topicTitle: 'Source',
    });
  });

  describe('relay hops', () => {
    const operation = { agentId: 'agt-coco', topicId: 'tpc-source', userId: CALLER };
    const topic = { agentId: 'agt-coco', title: 'Source' };

    it('counts one hop when a person started the sending turn', async () => {
      const deps = buildDeps({ operation, topic });

      await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toMatchObject({
        hop: 1,
      });
    });

    it('counts one more than the agent message that started the sending turn', async () => {
      const deps = buildDeps({
        operation: { ...operation, threadId: 'thd-1' },
        topic,
        triggerHop: 2,
      });

      await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toMatchObject({
        hop: 3,
      });
      // Read on the conversation the sending turn runs in.
      expect(deps.findTriggerHop).toHaveBeenCalledWith({
        threadId: 'thd-1',
        topicId: 'tpc-source',
      });
    });

    it('still counts on a source whose origin is hidden from the destination', async () => {
      const deps = buildDeps({
        agentVisibility: { 'agt-coco': 'private', 'agt-destination': 'public' },
        operation: { ...operation, workspaceId: 'ws-1' },
        topic: { ...topic, workspaceId: 'ws-1' },
        triggerHop: 4,
        workspaceId: 'ws-1',
      });

      const sender = await resolveAgentSenderFromOperation('op-1', deps);

      expect(sender?.topicId).toBeUndefined();
      expect(sender?.hop).toBe(5);
    });

    it('starts over at one when the trigger cannot be read', async () => {
      const deps = buildDeps({ operation, topic, triggerHop: new Error('db down') });

      await expect(resolveAgentSenderFromOperation('op-1', deps)).resolves.toMatchObject({
        hop: 1,
      });
    });

    it('refuses a send past the bound, and nothing up to it', () => {
      expect(() => assertAgentRelayHop(undefined)).not.toThrow();
      expect(() =>
        assertAgentRelayHop({ agentId: 'agt-coco', hop: MAX_AGENT_RELAY_HOPS }),
      ).not.toThrow();
      expect(() =>
        assertAgentRelayHop({ agentId: 'agt-coco', hop: MAX_AGENT_RELAY_HOPS + 1 }),
      ).toThrow(AgentRelayHopLimitError);
    });
  });
});