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.

🔒 fix(agent-identity): give a mail-woken run only its own mailbox (#22686)

Arvin Xub6befe4f36fb
diff --git a/apps/server/src/modules/Mecha/AgentToolsEngine/__tests__/inboxRunTools.test.ts b/apps/server/src/modules/Mecha/AgentToolsEngine/__tests__/inboxRunTools.test.ts
new file mode 100644
index 00000000000..fbaa733c229
--- /dev/null
+++ b/apps/server/src/modules/Mecha/AgentToolsEngine/__tests__/inboxRunTools.test.ts
@@ -0,0 +1,46 @@
+// @vitest-environment node
+import { AgentAccountIdentifier } from '@lobechat/builtin-tool-agent-account';
+import { MemoryManifest } from '@lobechat/builtin-tool-memory';
+import { WebBrowsingManifest } from '@lobechat/builtin-tool-web-browsing';
+import { resolveInvocationToolIds } from '@lobechat/mecha';
+import { describe, expect, it } from 'vitest';
+
+import { INBOX_RUN_PLUGIN_IDS } from '@/server/services/agentIdentity/ownerChannel';
+
+import { createServerAgentToolsEngine } from '../index';
+import type { ServerAgentToolsContext } from '../types';
+
+const context: ServerAgentToolsContext = {
+ installedPlugins: [],
+ isModelSupportToolUse: () => true,
+};
+
+/** The tools a run gets, deciding exactly as the discovery stage does for an exclusive run. */
+const toolsOf = (hasIdentityAccount: boolean) =>
+ createServerAgentToolsEngine(context, {
+ agentConfig: {
+ // Pinned by the owner, and enough on their own to carry data out.
+ plugins: [WebBrowsingManifest.identifier, MemoryManifest.identifier],
+ },
+ hasIdentityAccount,
+ model: 'gpt-4',
+ provider: 'openai',
+ }).generateToolsDetailed({
+ model: 'gpt-4',
+ provider: 'openai',
+ skipDefaultTools: true,
+ toolIds: resolveInvocationToolIds({
+ agentPlugins: INBOX_RUN_PLUGIN_IDS,
+ exclusivePluginIds: INBOX_RUN_PLUGIN_IDS,
+ }),
+ });
+
+describe('the tools of a mail-woken run', () => {
+ it("is the agent's mailbox and nothing the owner pinned or the engine defaults to", () => {
+ expect(toolsOf(true).enabledToolIds).toEqual([AgentAccountIdentifier]);
+ });
+
+ it('is nothing at all for an agent without an account', () => {
+ expect(toolsOf(false).enabledToolIds).toEqual([]);
+ });
+});
diff --git a/apps/server/src/services/agentIdentity/__tests__/inboundChain.test.ts b/apps/server/src/services/agentIdentity/__tests__/inboundChain.test.ts
index 88d6090a3ff..b024122a9ab 100644
--- a/apps/server/src/services/agentIdentity/__tests__/inboundChain.test.ts
+++ b/apps/server/src/services/agentIdentity/__tests__/inboundChain.test.ts
@@ -328,6 +328,8 @@ describe('Agent inbound webhook — end to end over the real route', () => {
expect(instruction).toContain('direct Channel');
// Nobody watches this run: it must end, not park on an approval its report would miss.
expect(agentCalls[0].execAgent.userInterventionConfig).toEqual({ approvalMode: 'headless' });
+ // Whoever wrote the mail steers this run, so it gets its mailbox and nothing else.
+ expect(agentCalls[0].execAgent.exclusivePluginIds).toEqual(['lobe-agent-account']);
expect(agentCalls[0].execAgent.hooks).toEqual([
expect.objectContaining({ id: `agent-inbox-report:${accountId}`, type: 'onComplete' }),
]);
diff --git a/apps/server/src/services/agentIdentity/__tests__/mailWake.test.ts b/apps/server/src/services/agentIdentity/__tests__/mailWake.test.ts
index 9891c7c2631..d12e400db22 100644
--- a/apps/server/src/services/agentIdentity/__tests__/mailWake.test.ts
+++ b/apps/server/src/services/agentIdentity/__tests__/mailWake.test.ts
@@ -515,6 +515,7 @@ describe('mail digest flush', () => {
const [own, , instruction, fenced] = prompt.split('\n\n');
expect(instruction).toContain('direct Channel');
expect(runs[0].params.userInterventionConfig).toEqual({ approvalMode: 'headless' });
+ expect(runs[0].params.exclusivePluginIds).toEqual(['lobe-agent-account']);
expect(runs[0].params.hooks).toEqual([
expect.objectContaining({ id: `agent-inbox-report:${accountId}`, type: 'onComplete' }),
]);
diff --git a/apps/server/src/services/agentIdentity/ownerChannel.ts b/apps/server/src/services/agentIdentity/ownerChannel.ts
index aee449dd6c4..bfff6228dc0 100644
--- a/apps/server/src/services/agentIdentity/ownerChannel.ts
+++ b/apps/server/src/services/agentIdentity/ownerChannel.ts
@@ -1,3 +1,4 @@
+import { AgentAccountIdentifier } from '@lobechat/builtin-tool-agent-account';
import type { LobeChatDatabase } from '@lobechat/database';
import { BOT_NO_REPLY_TOKEN } from '@lobechat/types';
import debug from 'debug';
@@ -21,7 +22,7 @@ export const INBOX_REPORT_WEBHOOK = '/api/agent/webhooks/inbox-report';
* Where the result goes, told to the woken agent. A run nobody watches would otherwise end in a
* topic its owner never opens.
*/
-export const OWNER_CHANNEL_INSTRUCTION = `Your final answer is posted to your owner in your direct Channel with them, so write it to them. If nothing here needs them, answer exactly ${BOT_NO_REPLY_TOKEN} and nothing will be posted. You cannot send mail from this run: a send is refused. If a reply is warranted, put the draft in your answer for your owner to approve.`;
+export const OWNER_CHANNEL_INSTRUCTION = `Your final answer is posted to your owner in your direct Channel with them, so write it to them. If nothing here needs them, answer exactly ${BOT_NO_REPLY_TOKEN} and nothing will be posted. In this run you can only read your mailbox, and a send is refused. If a reply is warranted, put the draft in your answer for your owner to approve.`;
/**
* A woken run has nobody watching it, so it must end rather than park on an approval: a parked
@@ -30,6 +31,14 @@ export const OWNER_CHANNEL_INSTRUCTION = `Your final answer is posted to your ow
*/
export const INBOX_RUN_INTERVENTION = { approvalMode: 'headless' } as const;
+/**
+ * The only tools a mail-woken run gets: its own mailbox. Whoever wrote the mail is steering this
+ * run, and headless auto-runs every call that merely `required` approval, so any other tool would
+ * be theirs to use: a web fetch carries the owner's data out, a connector acts as the owner.
+ * Exclusive also keeps out default tools and anything the activator could load mid-run.
+ */
+export const INBOX_RUN_PLUGIN_IDS = [AgentAccountIdentifier];
+
export type OwnerChannelReportResult =
| 'no-account'
| 'no-channel'
diff --git a/apps/server/src/services/agentIdentity/wake/digest.ts b/apps/server/src/services/agentIdentity/wake/digest.ts
index da52831c674..1820d8e7c6e 100644
--- a/apps/server/src/services/agentIdentity/wake/digest.ts
+++ b/apps/server/src/services/agentIdentity/wake/digest.ts
@@ -14,6 +14,7 @@ import { agentSignalScopeFlowControl } from '@/server/workflows/agentSignal';
import {
createOwnerChannelReportHook,
INBOX_RUN_INTERVENTION,
+ INBOX_RUN_PLUGIN_IDS,
OWNER_CHANNEL_INSTRUCTION,
} from '../ownerChannel';
import { buildInboxTopicTitle, fenceUntrustedInbox, toUntrustedInboxEntry } from '../untrusted';
@@ -118,6 +119,7 @@ export const createDigestRunStarter =
const result = await service.execAgent({
agentId: account.agentId,
appContext: { scope: 'agent' },
+ exclusivePluginIds: INBOX_RUN_PLUGIN_IDS,
hooks: [createOwnerChannelReportHook(db, account.id)],
prompt: buildDigestPrompt(account, messages),
title: buildInboxTopicTitle(messages),
diff --git a/apps/server/src/services/agentIdentity/waker.ts b/apps/server/src/services/agentIdentity/waker.ts
index 0c6a764e6a5..8d2273e977d 100644
--- a/apps/server/src/services/agentIdentity/waker.ts
+++ b/apps/server/src/services/agentIdentity/waker.ts
@@ -12,6 +12,7 @@ import type { AgentInboundWakeInput, AgentInboundWaker } from './inbound';
import {
createOwnerChannelReportHook,
INBOX_RUN_INTERVENTION,
+ INBOX_RUN_PLUGIN_IDS,
OWNER_CHANNEL_INSTRUCTION,
} from './ownerChannel';
import { buildInboxTopicTitle, fenceUntrustedInbox, toUntrustedInboxEntry } from './untrusted';
@@ -63,6 +64,7 @@ export const createAgentInboundWaker = (db: LobeChatDatabase): AgentInboundWaker
service.execAgent({
agentId: account.agentId,
appContext: { scope: 'agent', topicId },
+ exclusivePluginIds: INBOX_RUN_PLUGIN_IDS,
hooks: [createOwnerChannelReportHook(db, account.id)],
prompt: buildInboundPrompt(account, message),
title: buildInboxTopicTitle([message]),