π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix: should filiter the current agents in avaiable agents list (#13644)
LiJian8d8b60e4f93a
diff --git a/packages/context-engine/src/providers/AgentManagementContextInjector.ts b/packages/context-engine/src/providers/AgentManagementContextInjector.tsindex 1e0f2e7cc26..19e56a6dd6c 100644--- a/packages/context-engine/src/providers/AgentManagementContextInjector.ts+++ b/packages/context-engine/src/providers/AgentManagementContextInjector.ts@@ -74,7 +74,13 @@ export interface AvailablePluginInfo {* Agent Management context*/export interface AgentManagementContext {- /** User's recently updated agents β surfaced so the model can callAgent without searchAgent first */+ /**+ * User's recently updated agents β surfaced so the model can callAgent without+ * searchAgent first. The current/responding agent is NEVER included here, so+ * the model has no exposure to its own id from this section and cannot+ * accidentally delegate to itself. Filtering happens at the caller side+ * (server `aiAgent` and client `contextEngineering`).+ */availableAgents?: AvailableAgentInfo[];/** Whether the user has more agents than the ones listed in `availableAgents` */availableAgentsHasMore?: boolean;@@ -125,7 +131,8 @@ const defaultFormatContext = (context: AgentManagementContext): string => {parts.push(`<available_models>\n${providersXml}\n</available_models>`);}- // Add available agents section (user's existing agents)+ // Add available agents section (user's existing agents β never includes the current agent;+ // the caller filters self out so the model has no exposure to its own id from this section)if (context.availableAgents && context.availableAgents.length > 0) {const agentsXml = context.availableAgents.map((agent) => {@@ -203,7 +210,7 @@ const defaultFormatContext = (context: AgentManagementContext): string => {}if (hasAgents) {instructionParts.push(- "The `available_agents` section lists the user's existing agents. When the user's request clearly matches one of them, you may delegate to it via the Agent Management `callAgent` tool (activating the tool first if it is not already enabled). If no listed agent matches, use `searchAgent` to look further (including the marketplace).",+ "The `available_agents` section lists the user's other existing agents (you are not in this list). When the user's request clearly matches one of them, you may delegate to it via the Agent Management `callAgent` tool (activating the tool first if it is not already enabled). If no listed agent matches, use `searchAgent` to look further (including the marketplace).",);}@@ -260,15 +267,10 @@ export class AgentManagementContextInjector extends BaseProvider {const hasMentionedAgents =this.config.context.mentionedAgents && this.config.context.mentionedAgents.length > 0;- // Format context (excluding mentionedAgents β those are injected separately after the last user message)- const contextWithoutMentions: AgentManagementContext = hasMentionedAgents- ? {- availableAgents: this.config.context.availableAgents,- availableAgentsHasMore: this.config.context.availableAgentsHasMore,- availablePlugins: this.config.context.availablePlugins,- availableProviders: this.config.context.availableProviders,- }- : this.config.context;+ // Format context (excluding mentionedAgents β those are injected separately+ // after the last user message). Use a destructure-rest copy so future fields+ // (e.g. currentAgent) don't silently get dropped here.+ const { mentionedAgents: _mentioned, ...contextWithoutMentions } = this.config.context;const formatFn = this.config.formatContext || defaultFormatContext;const formattedContent = formatFn(contextWithoutMentions);diff --git a/src/server/services/aiAgent/index.ts b/src/server/services/aiAgent/index.tsindex fb462d43d02..5917ac59a0c 100644--- a/src/server/services/aiAgent/index.ts+++ b/src/server/services/aiAgent/index.ts@@ -691,13 +691,23 @@ export class AiAgentService {let agentManagementContext: AgentManagementContext | undefined;if (shouldInjectAvailableAgents) {- // Query user's most recently updated agents. Over-fetch by 1 to detect overflow.+ // Query user's most recently updated agents.+ // Over-fetch by 2: +1 reserved for the current agent (filtered out below+ // so the model has no exposure to its own id and cannot self-delegate)+ // and +1 to detect overflow for the `hasMore` flag.const AVAILABLE_AGENTS_LIMIT = 10;const recentAgents = await this.agentModel.queryAgents({- limit: AVAILABLE_AGENTS_LIMIT + 1,+ limit: AVAILABLE_AGENTS_LIMIT + 2,});- const hasMoreAgents = recentAgents.length > AVAILABLE_AGENTS_LIMIT;- const availableAgents = recentAgents.slice(0, AVAILABLE_AGENTS_LIMIT).map((a) => ({++ // Exclude the current agent from `availableAgents` β the model is the current+ // agent. Its persona/identity is already established by `systemRole`, so we+ // don't re-inject it here, and removing self from the list ensures the model+ // never sees its own id in the agent-management context (so it can't+ // accidentally call itself via `callAgent`).+ const otherAgents = recentAgents.filter((a) => a.id !== resolvedAgentId);+ const hasMoreAgents = otherAgents.length > AVAILABLE_AGENTS_LIMIT;+ const availableAgents = otherAgents.slice(0, AVAILABLE_AGENTS_LIMIT).map((a) => ({description: a.description ?? undefined,id: a.id,title: a.title ?? 'Untitled',diff --git a/src/services/chat/mecha/contextEngineering.ts b/src/services/chat/mecha/contextEngineering.tsindex 46cf5f33b7a..e9f2a271b32 100644--- a/src/services/chat/mecha/contextEngineering.ts+++ b/src/services/chat/mecha/contextEngineering.ts@@ -416,13 +416,22 @@ export const contextEngineering = async ({if (shouldInjectAvailableAgents) {try {- // Over-fetch by 1 to detect overflow.+ // Over-fetch by 2: +1 reserved for the current agent (filtered out below+ // so the model has no exposure to its own id and cannot self-delegate)+ // and +1 to detect overflow for the `hasMore` flag.const AVAILABLE_AGENTS_LIMIT = 10;const recentAgents = await lambdaClient.agent.queryAgents.query({- limit: AVAILABLE_AGENTS_LIMIT + 1,+ limit: AVAILABLE_AGENTS_LIMIT + 2,});- const hasMoreAgents = recentAgents.length > AVAILABLE_AGENTS_LIMIT;- const availableAgents = recentAgents.slice(0, AVAILABLE_AGENTS_LIMIT).map((a) => ({++ // Exclude current agent from `availableAgents`. The model is the current+ // agent β its identity/persona is already established by `systemRole`, so+ // we don't re-inject it here, and removing self from the list ensures the+ // model never sees its own id in the agent-management context (so it+ // cannot accidentally call itself via `callAgent`).+ const otherAgents = agentId ? recentAgents.filter((a) => a.id !== agentId) : recentAgents;+ const hasMoreAgents = otherAgents.length > AVAILABLE_AGENTS_LIMIT;+ const availableAgents = otherAgents.slice(0, AVAILABLE_AGENTS_LIMIT).map((a) => ({description: a.description ?? undefined,id: a.id,title: a.title ?? 'Untitled',