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.

27.9 KB
Raw Blame
import { getConnectorCatalog } from '@lobechat/const';
import { describe, expect, it, vi } from 'vitest';

import { gatherContextFacts } from './gatherContextFacts';
import { listOfficialTools } from './officialTools';
import type { ContextFactProviders, ContextFactRequest } from './types';

const request = (overrides: Partial<ContextFactRequest> = {}): ContextFactRequest => ({
  agent: { chatConfig: {} as never, description: 'helps', slug: null, title: 'Helper' },
  agentId: 'agt_1',
  enabledToolIds: [],
  features: { composio: true, lobehubSkill: true },
  messages: [],
  topicId: 'tpc_1',
  ...overrides,
});

const message = (content: string) =>
  ({ content, createdAt: 0, id: 'm', role: 'user', updatedAt: 0 }) as never;

describe('gatherContextFacts', () => {
  it('tells the agent its name, not only its title', async () => {
    const named = await gatherContextFacts(
      request({ agent: { description: '', name: 'Nasa', title: 'Shadow Arvin' } }),
      {},
    );
    const unnamed = await gatherContextFacts(request(), {});

    expect(named.variables).toMatchObject({ agent_name: 'Nasa', agent_title: 'Shadow Arvin' });
    // Without a name the label is the title, the same as the product shows.
    expect(unnamed.variables).toMatchObject({ agent_name: 'Helper', agent_title: 'Helper' });
  });

  it('yields no facts and never throws when the host provides nothing', async () => {
    const facts = await gatherContextFacts(request(), {});

    expect(facts.agentDocuments).toBeUndefined();
    expect(Object.values(facts.step).every((v) => v === undefined)).toBe(true);
    expect(facts.variables).toMatchObject({
      CREDS_LIST: '',
      agent_id: 'agt_1',
      agent_title: 'Helper',
      creds_sandbox_reachable: 'true',
      sandbox_enabled: 'false',
      topic_id: 'tpc_1',
    });
  });

  it('treats a failing provider as a missing fact', async () => {
    const facts = await gatherContextFacts(request(), {
      findTopic: async () => {
        throw new Error('db down');
      },
      getUserInfo: async () => ({ language: 'zh-CN', username: 'arvin' }),
    });

    expect(facts.variables.topic_title).toBe('');
    expect(facts.variables.username).toBe('arvin');
  });

  describe('plan / todo', () => {
    const providers: ContextFactProviders = {
      getPlanDocument: async () => ({
        content: 'ctx',
        createdAt: '2026-01-01T00:00:00.000Z',
        description: 'desc',
        id: 'plan_1',
        metadata: { todos: { items: [{ status: 'todo', text: 'from doc' }] } },
        title: 'Ship it',
        updatedAt: '2026-01-02T00:00:00.000Z',
      }),
    };

    it('injects the plan block from the document and its todos when history has none', async () => {
      const facts = await gatherContextFacts(
        request({ enabledToolIds: ['lobe-agent'] }),
        providers,
      );

      expect(facts.step.planTodo?.plan).toMatchObject({ goal: 'Ship it', description: 'desc' });
      expect(facts.step.planTodo?.todos?.items.map((t) => t.text)).toEqual(['from doc']);
    });

    it('keeps the plan block but takes todos from history when history has them', async () => {
      const facts = await gatherContextFacts(
        request({
          enabledToolIds: ['lobe-agent'],
          messages: [
            {
              content: 'done',
              createdAt: 0,
              id: 't',
              pluginState: { todos: { items: [{ status: 'todo', text: 'from history' }] } },
              role: 'tool',
              updatedAt: 0,
            } as never,
          ],
        }),
        providers,
      );

      expect(facts.step.planTodo?.plan?.goal).toBe('Ship it');
      expect(facts.step.planTodo?.todos?.items.map((t) => t.text)).toEqual(['from history']);
    });

    it('keeps an existing topic plan available before Lobe Agent is activated', async () => {
      const facts = await gatherContextFacts(
        request({ discoverableTools: [{ identifier: 'lobe-agent' }] }),
        providers,
      );

      expect(facts.step.planTodo?.plan?.goal).toBe('Ship it');
    });

    it('does not read the plan document when Lobe Agent is unavailable', async () => {
      const getPlanDocument = vi.fn(providers.getPlanDocument);
      const facts = await gatherContextFacts(request(), { getPlanDocument });

      expect(getPlanDocument).not.toHaveBeenCalled();
      expect(facts.step.planTodo).toBeUndefined();
    });
  });

  describe('credentials and Composio', () => {
    it('lists credentials for the run scope only when the creds tool is enabled', async () => {
      const listCredentials = vi.fn(async () => [
        { key: 'k', name: 'GitHub', type: 'kv-env' as const },
      ]);

      const off = await gatherContextFacts(request(), { listCredentials });
      expect(listCredentials).not.toHaveBeenCalled();
      expect(off.variables.CREDS_LIST).toBe('');

      const on = await gatherContextFacts(
        request({ enabledToolIds: ['lobe-creds'], workspaceId: 'ws_1' }),
        { listCredentials },
      );
      expect(listCredentials).toHaveBeenCalledWith({ workspaceId: 'ws_1' });
      expect(on.variables.CREDS_LIST).toContain('GitHub');
    });

    it('drops disabled Composio services from both lists and needs the creds tool', async () => {
      const listConnectedConnectorIds = vi.fn(async () => ['gmail', 'slack']);

      const off = await gatherContextFacts(request(), { listConnectedConnectorIds });
      expect(listConnectedConnectorIds).not.toHaveBeenCalled();
      expect(off.variables.COMPOSIO_SERVICES_LIST).toBe('');

      const on = await gatherContextFacts(
        request({ disabledPluginIds: ['slack'], enabledToolIds: ['lobe-creds', 'gmail'] }),
        { listConnectedConnectorIds },
      );
      const list = on.variables.COMPOSIO_SERVICES_LIST as string;
      expect(list).toContain('gmail');
      expect(list).not.toContain('slack');
    });

    it('lists connected Composio services available for this run to activate', async () => {
      const listConnectedConnectorIds = vi.fn(async () => ['gmail', 'slack']);

      const facts = await gatherContextFacts(
        request({ enabledToolIds: ['lobe-creds'], discoverableTools: [{ identifier: 'gmail' }] }),
        { listConnectedConnectorIds },
      );
      const list = facts.variables.COMPOSIO_SERVICES_LIST as string;

      expect(list).toContain('gmail');
      expect(list).not.toContain('slack');
    });
  });

  describe('document references', () => {
    it('resolves referenced documents against the executing agent', async () => {
      const findAgentDocument = vi.fn(async (_agentId: string, documentId: string) =>
        documentId === 'doc-1' ? { content: 'Voice rules', title: 'Voice' } : null,
      );

      const facts = await gatherContextFacts(
        request({
          messages: [
            message(
              'Use <refer_document name="Voice" id="doc-1" /> and <refer_document name="X" id="doc-2" />',
            ),
          ],
        }),
        { findAgentDocument },
      );

      expect(findAgentDocument).toHaveBeenCalledWith('agt_1', 'doc-1');
      expect(facts.step.documentReferences).toEqual([
        { content: 'Voice rules', documentId: 'doc-1', title: 'Voice' },
        { documentId: 'doc-2', title: 'X' },
      ]);
    });

    it('lists earlier-turn references without a lookup only when the documents tool can read them', async () => {
      const findAgentDocument = vi.fn(async () => ({ content: 'Voice rules' }));
      const messages = [message('<refer_document name="Voice" id="doc-1" />'), message('Next')];

      const withTool = await gatherContextFacts(
        request({ enabledToolIds: ['lobe-agent-documents'], messages }),
        { findAgentDocument },
      );
      expect(withTool.step.documentReferences).toEqual([
        { documentId: 'doc-1', earlierTurn: true, title: 'Voice' },
      ]);
      expect(findAgentDocument).not.toHaveBeenCalled();

      const withoutTool = await gatherContextFacts(request({ messages }), { findAgentDocument });
      expect(withoutTool.step.documentReferences).toEqual([
        { content: 'Voice rules', documentId: 'doc-1', title: 'Voice' },
      ]);
    });

    it('skips the lookup when no message references a document', async () => {
      const findAgentDocument = vi.fn();

      const facts = await gatherContextFacts(request({ messages: [message('hello')] }), {
        findAgentDocument,
      });

      expect(facts.step.documentReferences).toBeUndefined();
      expect(findAgentDocument).not.toHaveBeenCalled();
    });
  });

  describe('share visitor gates', () => {
    const visitor = { agentId: 'agt_1', visitorUserId: 'visitor_1' };

    it('hides documents, onboarding and workspace context from a visitor', async () => {
      const facts = await gatherContextFacts(
        request({
          agent: { slug: 'web-onboarding' },
          enabledToolIds: ['lobe-web-onboarding'],
          shareVisitor: visitor,
          workspaceId: 'ws_1',
        }),
        {
          getOnboardingContext: async () => ({ phaseGuidance: 'go' }) as never,
          getWorkspaceContext: async () => ({ appUrl: 'https://x', slug: 'team' }),
          listAgentDocuments: async () => [{ id: 'd1' }] as never,
        },
      );

      expect(facts.agentDocuments).toBeUndefined();
      expect(facts.step.onboardingContext).toBeUndefined();
      expect(facts.step.workspaceContext).toBeUndefined();
    });

    it('only resolves topic references the visitor started with the shared agent', async () => {
      const findTopic = async (topicId: string) =>
        topicId === 'mine'
          ? { agentId: 'agt_1', historySummary: 'my summary', id: 'mine', senderId: 'visitor_1' }
          : {
              agentId: 'agt_1',
              historySummary: 'creator secret',
              id: 'theirs',
              senderId: 'creator',
            };

      const facts = await gatherContextFacts(
        request({
          messages: [
            message(
              '<refer_topic id="mine">a</refer_topic> <refer_topic id="theirs">b</refer_topic>',
            ),
          ],
          shareVisitor: visitor,
        }),
        { findTopic },
      );

      const summaries = facts.step.topicReferences?.map((r) => r.summary) ?? [];
      expect(summaries).toContain('my summary');
      expect(summaries).not.toContain('creator secret');
    });

    it('never resolves document references for a visitor', async () => {
      const findAgentDocument = vi.fn(async () => ({ content: 'creator secret', title: 'Doc' }));

      const facts = await gatherContextFacts(
        request({
          messages: [message('<refer_document name="Doc" id="doc-1" />')],
          shareVisitor: visitor,
        }),
        { findAgentDocument },
      );

      expect(facts.step.documentReferences).toBeUndefined();
      expect(findAgentDocument).not.toHaveBeenCalled();
    });

    it('never enumerates the owner’s agents, providers or plugins for a visitor', async () => {
      const listRecentAgents = vi.fn(async () => [{ id: 'agt_2', title: 'Owner private' }]);
      const listEnabledProviders = vi.fn(async () => [{ id: 'openai', models: [], name: 'x' }]);
      const listCustomPlugins = vi.fn(async () => []);

      const facts = await gatherContextFacts(
        request({
          // Auto skill mode and even a leaked tool id must not open the door.
          enabledToolIds: ['lobe-agent-management'],
          mentionedAgents: [{ id: 'agt_3', title: 'Mentioned' }] as never,
          shareVisitor: visitor,
        }),
        { listCustomPlugins, listEnabledProviders, listRecentAgents },
      );

      expect(listRecentAgents).not.toHaveBeenCalled();
      expect(listEnabledProviders).not.toHaveBeenCalled();
      expect(listCustomPlugins).not.toHaveBeenCalled();
      expect(facts.step.agentManagementContext).toEqual({
        mentionedAgents: [{ id: 'agt_3', title: 'Mentioned' }],
      });
    });

    it('reads user info for the visitor, not the owner', async () => {
      const getUserInfo = vi.fn(async () => ({ language: 'ja-JP', username: 'v' }));
      await gatherContextFacts(request({ shareVisitor: visitor }), { getUserInfo });
      expect(getUserInfo).toHaveBeenCalledWith('visitor_1');

      await gatherContextFacts(request(), { getUserInfo });
      expect(getUserInfo).toHaveBeenLastCalledWith(undefined);
    });
  });

  it('skips topic references and onboarding already present in the messages', async () => {
    const findTopic = vi.fn();
    const getOnboardingContext = vi.fn();
    await gatherContextFacts(
      request({
        enabledToolIds: ['lobe-web-onboarding'],
        messages: [
          message(
            '<topic_reference_context>…</topic_reference_context> <onboarding_context>…</onboarding_context>',
          ),
        ],
      }),
      { findTopic, getOnboardingContext },
    );
    // The current topic is still looked up for its title; no referenced topic is.
    expect(findTopic.mock.calls.map(([id]) => id)).toEqual(['tpc_1']);
    expect(getOnboardingContext).not.toHaveBeenCalled();
  });

  it('reads the documents of the agent being edited while the builder is active', async () => {
    const listAgentDocuments = vi.fn(async (id: string) => [{ id: `doc-of-${id}` }] as never);

    const plain = await gatherContextFacts(request({ editingAgentId: 'agt_edit' }), {
      listAgentDocuments,
    });
    expect(plain.agentDocuments).toEqual([{ id: 'doc-of-agt_1' }]);

    const building = await gatherContextFacts(
      request({ editingAgentId: 'agt_edit', enabledToolIds: ['lobe-agent-builder'] }),
      { listAgentDocuments },
    );
    expect(building.agentDocuments).toEqual([{ id: 'doc-of-agt_edit' }]);
  });

  describe('agent management', () => {
    const recent = Array.from({ length: 12 }, (_, i) => ({ id: `agt_${i}`, title: `A${i}` }));

    it('lists available agents in auto skill mode without the tool, excluding itself', async () => {
      const facts = await gatherContextFacts(request(), { listRecentAgents: async () => recent });

      const ctx = facts.step.agentManagementContext!;
      expect(ctx.availableAgents?.map((a) => a.id)).not.toContain('agt_1');
      expect(ctx.availableAgents).toHaveLength(10);
      expect(ctx.availableAgentsHasMore).toBe(true);
      expect(ctx.currentAgent).toEqual({ id: 'agt_1', title: 'Helper' });
      expect(ctx.availablePlugins).toBeUndefined();
    });

    it.each([{ enabledToolIds: [] }, { enabledToolIds: ['lobe-agent-management'] }])(
      'omits unsolicited recent agents without disabling self-management or explicit mentions ($enabledToolIds)',
      async ({ enabledToolIds }) => {
        const listRecentAgents = vi.fn(async () => recent);
        const mentionedAgents = [{ id: 'agt_9', name: 'Nine' }];
        const facts = await gatherContextFacts(
          request({ enabledToolIds, includeRecentAgents: false, mentionedAgents }),
          {
            listEnabledProviders: async () => [{ id: 'openai', models: [], name: 'OpenAI' }],
            listRecentAgents,
          },
        );

        const ctx = facts.step.agentManagementContext!;
        expect(listRecentAgents).not.toHaveBeenCalled();
        expect(ctx.availableAgents).toBeUndefined();
        expect(ctx.availableAgentsHasMore).toBeUndefined();
        expect(ctx.currentAgent).toEqual({ id: 'agt_1', title: 'Helper' });
        expect(ctx.mentionedAgents).toEqual(mentionedAgents);
        if (enabledToolIds.length)
          expect(ctx.availableProviders).toEqual([{ id: 'openai', models: [], name: 'OpenAI' }]);
      },
    );

    it('carries each agent’s name beside its title, for itself and the others', async () => {
      const facts = await gatherContextFacts(
        request({ agent: { name: 'Nasa', title: 'Shadow Arvin' } }),
        { listRecentAgents: async () => [{ id: 'agt_2', name: 'Tiko', title: 'Reviewer' }] },
      );

      const ctx = facts.step.agentManagementContext!;
      expect(ctx.currentAgent).toStrictEqual({ id: 'agt_1', name: 'Nasa', title: 'Shadow Arvin' });
      expect(ctx.availableAgents).toStrictEqual([
        { description: undefined, id: 'agt_2', name: 'Tiko', title: 'Reviewer' },
      ]);
    });

    it('adds providers and the plugin catalog only when the tool is enabled', async () => {
      const facts = await gatherContextFacts(
        request({
          agent: { chatConfig: { skillActivateMode: 'manual' } as never },
          enabledToolIds: ['lobe-agent-management'],
        }),
        {
          listCustomPlugins: async () => [{ identifier: 'my-mcp', name: 'Mine', type: 'custom' }],
          listEnabledProviders: async () => [{ id: 'openai', models: [], name: 'OpenAI' }],
          listRecentAgents: async () => recent,
        },
      );

      const ctx = facts.step.agentManagementContext!;
      expect(ctx.availableProviders).toEqual([{ id: 'openai', models: [], name: 'OpenAI' }]);
      const types = new Set(ctx.availablePlugins?.map((p) => p.type));
      expect(types).toEqual(new Set(['builtin', 'composio', 'lobehub-skill', 'custom']));
      expect(ctx.availablePlugins?.map((p) => p.identifier)).not.toContain('lobe-agent-builder');
    });

    it('carries mentioned agents even without the tool or auto mode', async () => {
      const facts = await gatherContextFacts(
        request({
          agent: { chatConfig: { skillActivateMode: 'manual' } as never },
          mentionedAgents: [{ id: 'agt_9', title: 'Nine' }] as never,
        }),
        {},
      );
      expect(facts.step.agentManagementContext).toEqual({
        mentionedAgents: [{ id: 'agt_9', title: 'Nine' }],
      });
    });

    it('invites no delegation inside a sub-agent run, where callAgent is rejected', async () => {
      const listRecentAgents = vi.fn(async () => recent);
      const facts = await gatherContextFacts(
        request({
          enabledToolIds: ['lobe-agent-management'],
          isSubAgent: true,
          mentionedAgents: [{ id: 'agt_9', title: 'Nine' }] as never,
        }),
        {
          listEnabledProviders: async () => [{ id: 'openai', models: [], name: 'OpenAI' }],
          listRecentAgents,
        },
      );

      const ctx = facts.step.agentManagementContext!;
      expect(listRecentAgents).not.toHaveBeenCalled();
      expect(ctx.availableAgents).toBeUndefined();
      expect(ctx.mentionedAgents).toBeUndefined();
      // Its own id stays, for updateAgent / installPlugin on itself.
      expect(ctx.currentAgent).toEqual({ id: 'agt_1', title: 'Helper' });
      // CRUD facts for createAgent / updateAgent still flow.
      expect(ctx.availableProviders).toEqual([{ id: 'openai', models: [], name: 'OpenAI' }]);
    });

    it('invites no delegation in an auto-mode sub-agent without the tool', async () => {
      const listRecentAgents = vi.fn(async () => recent);
      const facts = await gatherContextFacts(request({ isSubAgent: true }), { listRecentAgents });
      expect(listRecentAgents).not.toHaveBeenCalled();
      expect(facts.step.agentManagementContext).toEqual({
        currentAgent: { id: 'agt_1', title: 'Helper' },
      });
    });
  });

  it('lists LobeHub skill connectors in the official tool catalog with their status', () => {
    const [lobehubProvider] = getConnectorCatalog({ composio: false, lobehub: true });
    const lobehubId = lobehubProvider.type === 'lobehub' ? lobehubProvider.provider.id : '';
    const tools = listOfficialTools({
      connectedConnectorIds: new Set(['gmail', lobehubId]),
      enabledPlugins: ['gmail'],
      features: { composio: true, lobehubSkill: true },
    });

    const types = new Set(tools.map((t) => t.type));
    expect(types).toEqual(new Set(['builtin', 'composio', 'lobehub-skill']));
    expect(tools.find((t) => t.identifier === 'gmail')).toMatchObject({
      enabled: true,
      installed: true,
    });
    expect(tools.find((t) => t.identifier === lobehubId)).toMatchObject({
      installed: true,
      type: 'lobehub-skill',
    });
  });

  it('lists an uninstalled builtin as not installed so the builder does not pin it', async () => {
    const builtinId = listOfficialTools({
      connectedConnectorIds: new Set(),
      enabledPlugins: [],
      features: { composio: false, lobehubSkill: false },
    }).find((t) => t.type === 'builtin')!.identifier;

    const tools = listOfficialTools({
      connectedConnectorIds: new Set(),
      enabledPlugins: [],
      features: { composio: false, lobehubSkill: false },
      uninstalledBuiltinIds: new Set([builtinId]),
    });
    expect(tools.find((t) => t.identifier === builtinId)).toMatchObject({ installed: false });

    const facts = await gatherContextFacts(
      request({ editingAgentId: 'agt_edit', enabledToolIds: ['lobe-agent-builder'] }),
      {
        getAgentDefinition: async () => ({ plugins: [] }),
        listUninstalledBuiltinIds: async () => [builtinId],
      },
    );
    expect(
      facts.step.agentBuilderContext?.officialTools?.find((t) => t.identifier === builtinId),
    ).toMatchObject({ installed: false });
  });

  it('describes the workspace only when its slug resolves', async () => {
    const providers = {
      getWorkspaceContext: async (id?: string) => ({
        appUrl: 'https://x',
        slug: id ? undefined : undefined,
      }),
    };
    const personal = await gatherContextFacts(request({ workspaceId: undefined }), providers);
    expect(personal.step.workspaceContext).toEqual({ appUrl: 'https://x' });

    const unresolved = await gatherContextFacts(request({ workspaceId: 'ws_1' }), providers);
    expect(unresolved.step.workspaceContext).toBeUndefined();

    const resolved = await gatherContextFacts(request({ workspaceId: 'ws_1' }), {
      getWorkspaceContext: async () => ({ appUrl: 'https://x', slug: 'team' }),
    });
    expect(resolved.step.workspaceContext).toEqual({
      appUrl: 'https://x',
      workspace: { slug: 'team' },
    });
  });
});

describe('which tool counts as reaching the sandbox', () => {
  const persistence = { cwd: 'lobehub-dev', mode: 'persistent' as const };

  it('describes the persistent workspace for a run that only has the always-on skills tool', async () => {
    // `lobe-skills` runCommand opens the same session in the same directory as
    // the cloud-sandbox tool, and it ships with every run. Gating the placement
    // on the optional tool left a skills-driven run reading the ephemeral
    // wording while its commands ran in a persistent workspace.
    const facts = await gatherContextFacts(request({ enabledToolIds: ['lobe-skills'] }), {
      resolveSandboxPersistence: async () => persistence,
    });

    expect(facts.variables.sandbox_workspace).toContain('environment directory');
    expect(facts.variables.sandbox_workspace).toContain('`lobehub-dev`');
  });

  it('still describes it for the cloud-sandbox tool on its own', async () => {
    const facts = await gatherContextFacts(request({ enabledToolIds: ['lobe-cloud-sandbox'] }), {
      resolveSandboxPersistence: async () => persistence,
    });

    expect(facts.variables.sandbox_workspace).toContain('environment directory');
  });

  it('describes a drive folder as a folder on the network drive', async () => {
    const facts = await gatherContextFacts(request({ enabledToolIds: ['lobe-cloud-sandbox'] }), {
      resolveSandboxPersistence: async () => ({ cwd: 'reports', mode: 'drive' as const }),
    });

    expect(facts.variables.sandbox_workspace).toContain('`reports`, a folder on the user');
    expect(facts.variables.sandbox_session_files).toContain('persist across sessions');
  });

  it('asks for nothing when no tool can run a command in the sandbox', async () => {
    const resolveSandboxPersistence = vi.fn(async () => persistence);
    await gatherContextFacts(request({ enabledToolIds: ['lobe-agent'] }), {
      resolveSandboxPersistence,
    });

    expect(resolveSandboxPersistence).not.toHaveBeenCalled();
  });

  it('skips sandbox persistence lookup when lobe-skills is routed to a local device without cloud-sandbox', async () => {
    const resolveSandboxPersistence = vi.fn(async () => persistence);
    await gatherContextFacts(
      request({ activeDeviceId: 'dev_1', enabledToolIds: ['lobe-skills'] }),
      { resolveSandboxPersistence },
    );

    expect(resolveSandboxPersistence).not.toHaveBeenCalled();
  });
});

describe('gatherContextFacts — revoked document reads', () => {
  const readKnowledge = (fileIds: string[]) =>
    ({
      content: 'rule body',
      createdAt: 0,
      id: 't',
      plugin: {
        apiName: 'readKnowledge',
        arguments: JSON.stringify({ fileIds }),
        identifier: 'lobe-knowledge-base',
      },
      role: 'tool',
      updatedAt: 0,
    }) as never;

  it('asks the host only about documents the history read', async () => {
    const findRevokedDocumentReads = vi.fn(async () => ({
      agentDocumentReadIds: ['docs_kb'],
      documentIds: ['docs_b', 'docs_a'],
    }));
    const facts = await gatherContextFacts(
      request({ messages: [message('hi'), readKnowledge(['docs_a', 'docs_b'])] }),
      { findRevokedDocumentReads },
    );

    expect(findRevokedDocumentReads).toHaveBeenCalledWith('agt_1', ['docs_a', 'docs_b']);
    // Sorted so the filtered prefix is stable whatever order the host returns.
    expect(facts.revokedDocumentReads).toEqual([
      { agentDocumentReadIds: ['docs_kb'], documentIds: ['docs_a', 'docs_b'] },
    ]);
  });

  it('skips the lookup without a document read, for a share visitor, or without an agent', async () => {
    const findRevokedDocumentReads = vi.fn(async () => ({ documentIds: ['docs_a'] }));

    const noRead = await gatherContextFacts(request({ messages: [message('hi')] }), {
      findRevokedDocumentReads,
    });
    const visitor = await gatherContextFacts(
      request({
        messages: [readKnowledge(['docs_a'])],
        shareVisitor: { agentId: 'agt_1', visitorUserId: 'u_2' },
      }),
      { findRevokedDocumentReads },
    );
    const noAgent = await gatherContextFacts(
      request({ agentId: undefined, messages: [readKnowledge(['docs_a'])] }),
      { findRevokedDocumentReads },
    );

    expect(findRevokedDocumentReads).not.toHaveBeenCalled();
    expect(noRead.revokedDocumentReads).toBeUndefined();
    expect(visitor.revokedDocumentReads).toBeUndefined();
    expect(noAgent.revokedDocumentReads).toBeUndefined();
  });

  it('checks the edited agent while the agent builder is active', async () => {
    const findRevokedDocumentReads = vi.fn(async () => ({ documentIds: [] }));
    await gatherContextFacts(
      request({
        editingAgentId: 'agt_edit',
        enabledToolIds: ['lobe-agent-builder'],
        messages: [readKnowledge(['docs_a'])],
      }),
      { findRevokedDocumentReads },
    );

    expect(findRevokedDocumentReads).toHaveBeenCalledWith('agt_edit', ['docs_a']);
  });

  it('leaves the fact out when nothing is revoked, and fails closed when the lookup fails', async () => {
    const none = await gatherContextFacts(request({ messages: [readKnowledge(['docs_a'])] }), {
      findRevokedDocumentReads: async () => ({ agentDocumentReadIds: [], documentIds: [] }),
    });
    const failed = await gatherContextFacts(request({ messages: [readKnowledge(['docs_a'])] }), {
      findRevokedDocumentReads: async () => {
        throw new Error('db down');
      },
    });

    expect(none.revokedDocumentReads).toBeUndefined();
    // An unknown binding state must not let a deleted body back in.
    expect(failed.revokedDocumentReads).toEqual([{ documentIds: [], unverifiedIds: ['docs_a'] }]);
  });

  it("checks another group member's reads against that member's bindings", async () => {
    const findRevokedDocumentReads = vi.fn(async (agentId: string) =>
      agentId === 'agt_a' ? { documentIds: ['docs_a'] } : { documentIds: [] },
    );
    const facts = await gatherContextFacts(
      request({
        messages: [
          { ...readKnowledge(['docs_a']), agentId: 'agt_a' } as never,
          { ...readKnowledge(['docs_own']), agentId: 'agt_1' } as never,
        ],
      }),
      { findRevokedDocumentReads },
    );

    expect(findRevokedDocumentReads).toHaveBeenCalledWith('agt_a', ['docs_a']);
    expect(findRevokedDocumentReads).toHaveBeenCalledWith('agt_1', ['docs_own']);
    expect(facts.revokedDocumentReads).toEqual([
      { agentDocumentReadIds: [], agentId: 'agt_a', documentIds: ['docs_a'] },
      { agentDocumentReadIds: [], documentIds: [] },
    ]);
  });
});