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.

7.9 KB
Raw Blame
// @vitest-environment node
import type { AgentState } from '@lobechat/agent-runtime';
import { AgentAccountIdentifier } from '@lobechat/builtin-tool-agent-account';
import { getTestDB } from '@lobechat/database/test-utils';
import { gatherContextFacts } from '@lobechat/mecha';
import type { AgentMailAttachment } from '@lobechat/types';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { agentAccounts, agentInboxMessages, agents, users } from '@/database/schemas';
import type { LobeChatDatabase } from '@/database/type';
import type { RuntimeExecutorContext } from '@/server/modules/AgentRuntime/context';
import { serverMessagesEngine } from '@/server/modules/Mecha/ContextEngineering';
import { createServerContextFactProviders } from '@/server/modules/Mecha/ContextEngineering/providers';
import { AgentAccountService } from '@/server/services/agentIdentity';
import { AgentInboxService } from '@/server/services/agentIdentity/inbox';
import { AgentAccountProviderRegistry } from '@/server/services/agentIdentity/registry';

/**
 * Evidence that the agent's **addresses and inbox are first-class runtime
 * state**.
 *
 * The chain is real end to end: real `agent_accounts` / `agent_inbox_messages`
 * rows on PGlite β†’ the real server fact provider β†’ the shared gather rules β†’
 * the shared context engine. Only the model call is absent β€” we inspect the
 * messages the engine would send.
 */

const serverDB: LobeChatDatabase = await getTestDB();

/**
 * Record one inbound mail the way the inbound edge does (mail row, then inbox
 * item), from the flat shape these tests describe a delivery with.
 */
const recordInbound = (params: {
  accountId: string;
  agentId: string;
  attachments?: AgentMailAttachment[];
  from: string;
  kind?: string;
  provider?: string;
  providerMessageId: string;
  receivedAt: Date;
  subject?: string;
  text: string;
  threadKey?: string;
  to: string;
}) =>
  new AgentInboxService(serverDB, userId).recordInboundMail({
    account: {
      agentId: params.agentId,
      id: params.accountId,
      identifier: params.to,
      provider: params.provider ?? 'user',
    },
    message: {
      from: params.from,
      mail: params.attachments
        ? {
            attachments: params.attachments,
            cc: [],
            references: [],
            replyTo: [],
            to: [{ address: params.to }],
          }
        : undefined,
      providerMessageId: params.providerMessageId,
      receivedAt: params.receivedAt,
      subject: params.subject,
      text: params.text,
      threadKey: params.threadKey,
      to: params.to,
    },
  });

const userId = 'agent-inbox-context-user';
const agentId = 'agent-inbox-context';

const factsRequest = (overrides: Record<string, unknown> = {}) => ({
  agent: { chatConfig: {}, description: null, slug: null, title: 'Toby' },
  agentId,
  enabledToolIds: [] as string[],
  executionTarget: 'sandbox' as const,
  features: { composio: false, lobehubSkill: false },
  messages: [{ content: 'hi', id: 'm1', role: 'user' }] as any[],
  ...overrides,
});

beforeEach(async () => {
  await serverDB.delete(users);
  await serverDB.insert(users).values({ id: userId });
  await serverDB.insert(agents).values({ id: agentId, userId });
});

afterEach(async () => {
  await serverDB.delete(agentInboxMessages);
  await serverDB.delete(agentAccounts);
  await serverDB.delete(users);
});

const providers = () =>
  createServerContextFactProviders({
    ctx: { serverDB, userId } as unknown as RuntimeExecutorContext,
    state: { origin: { agentId } } as unknown as AgentState,
  });

describe('Agent identity as first-class runtime state', () => {
  it('injects the agent addresses and unread count, never the message content', async () => {
    const account = await new AgentAccountService(serverDB, userId, {
      registry: new AgentAccountProviderRegistry(),
    }).create({
      agentId,
      capabilities: { receive: true, send: true },
      identifier: '[email protected]',
      kind: 'mail',
      provider: 'user',
    });

    await recordInbound({
      accountId: account.id,
      agentId,
      from: '[email protected]',
      kind: 'mail',
      provider: 'user',
      providerMessageId: 'msg_once',
      receivedAt: new Date('2026-10-02T12:00:00.000Z'),
      subject: 'Your verification code',
      text: 'Your verification code is 839201. It expires in 10 minutes.',
      to: '[email protected]',
    });

    // 1. The fact is gathered from the real DB through the real provider.
    // Note `enabledToolIds: []` β€” the account tool is NOT enabled on this run.
    // The identity/inbox still reaches the model, which is the whole point: it
    // is state, not a tool result.
    expect(factsRequest().enabledToolIds).not.toContain(AgentAccountIdentifier);
    const facts = await gatherContextFacts(factsRequest(), providers());
    expect(facts.step.agentAccountContext).toBeDefined();
    expect(facts.step.agentAccountContext?.accounts).toHaveLength(1);
    expect(facts.step.agentAccountContext?.inbox.unreadCount).toBe(1);

    // 2. The shared context engine renders it into the system message.
    const { messages } = await serverMessagesEngine({
      agentAccountContext: facts.step.agentAccountContext,
      messages: [{ content: 'hi', id: 'm1', role: 'user' } as any],
      model: 'deepseek-chat',
      provider: 'deepseek',
      systemRole: 'You are toby.',
    });

    const system = String(messages.find((m) => m.role === 'system')?.content ?? '');
    // Surface the exact block for the delivery report.
    console.log(`\n[identity-context-block]\n${system}\n[/identity-context-block]\n`);
    expect(system).toContain('<agent_identity>');
    expect(system).toContain('mail [email protected] β€” user, can receive/send');
    expect(system).toContain('<inbox unread="1" />');
    // Nothing a sender wrote may carry system authority: no sender, subject,
    // body or code reaches the system message.
    expect(system).not.toContain('[email protected]');
    expect(system).not.toContain('Your verification code');
    expect(system).not.toContain('839201');
    // The account tool is not in this step's tool set, so the block must not
    // send the model looking for it.
    expect(facts.step.agentAccountContext?.toolAvailable).toBe(false);
    expect(system).not.toContain('account tool');
  });

  it('stops presenting an address once it has been released', async () => {
    const service = new AgentAccountService(serverDB, userId, {
      registry: new AgentAccountProviderRegistry(),
    });
    const live = await service.create({
      agentId,
      capabilities: { receive: true, send: true },
      identifier: '[email protected]',
      kind: 'mail',
      provider: 'user',
    });
    const released = await service.create({
      agentId,
      capabilities: { receive: true, send: true },
      identifier: '[email protected]',
      kind: 'mail',
      provider: 'user',
    });
    await service.revoke(released.id, { release: false });

    const facts = await gatherContextFacts(factsRequest(), providers());
    expect(facts.step.agentAccountContext?.accounts.map((a) => a.identifier)).toEqual([
      '[email protected]',
    ]);

    await service.revoke(live.id, { release: false });
    const afterAll = await gatherContextFacts(factsRequest(), providers());
    expect(afterAll.step.agentAccountContext).toBeUndefined();
  });

  it('adds no identity block for an agent that owns nothing', async () => {
    const facts = await gatherContextFacts(factsRequest(), providers());
    expect(facts.step.agentAccountContext).toBeUndefined();

    const { messages } = await serverMessagesEngine({
      agentAccountContext: facts.step.agentAccountContext,
      messages: [{ content: 'hi', id: 'm1', role: 'user' } as any],
      model: 'deepseek-chat',
      provider: 'deepseek',
      systemRole: 'You are toby.',
    });

    expect(String(messages.find((m) => m.role === 'system')?.content ?? '')).not.toContain(
      '<agent_identity>',
    );
  });
});