π€― 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(agent): surface real reason when cross-agent callAgent fails (#16290)
Arvin Xu70bf12706945
diff --git a/apps/server/src/modules/AgentRuntime/RuntimeExecutors.ts b/apps/server/src/modules/AgentRuntime/RuntimeExecutors.tsindex 0f43d335386..0d61105a6e8 100644--- a/apps/server/src/modules/AgentRuntime/RuntimeExecutors.ts+++ b/apps/server/src/modules/AgentRuntime/RuntimeExecutors.ts@@ -397,11 +397,14 @@ const buildServerVirtualSubAgentRunner = (timeout,title: description,topicId,- })) as { operationId?: string; success?: boolean; threadId?: string } | undefined;+ })) as+ | { error?: string; operationId?: string; success?: boolean; threadId?: string }+ | undefined;// 3. If the child op never started, no completion bridge will fire β parking// the parent on it would hang forever. Drop the placeholder and signal- // `started: false` so callSubAgent surfaces an inline tool error instead.+ // `started: false` (with the underlying reason) so callSubAgent surfaces+ // an inline tool error instead.if (!result?.success) {try {await ctx.messageModel.deleteMessage(placeholder.id);@@ -412,7 +415,12 @@ const buildServerVirtualSubAgentRunner = (error,);}- return { started: false, subOperationId: result?.operationId, threadId: '' };+ return {+ error: result?.error,+ started: false,+ subOperationId: result?.operationId,+ threadId: '',+ };}return {diff --git a/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts b/apps/server/src/services/agentRuntime/AgentRuntimeService.test.tsindex d9bb028be4d..93dc620a499 100644--- a/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts+++ b/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts@@ -2102,7 +2102,10 @@ describe('AgentRuntimeService', () => {);});- it('writes an error note + pluginError when the child failed', async () => {+ it('writes an error note + pluginError when the child failed, surfacing the real reason', async () => {+ // The parent agent's LLM only sees `content`; without the inlined reason it+ // gets the opaque generic note and cannot tell why the dispatch failed+ // (issue #16257). The structured error still rides on pluginError.await service.completeSubAgentBridge({...bridgeParams,finalState: { ...childState, error: { message: 'boom' } } as any,@@ -2112,13 +2115,41 @@ describe('AgentRuntimeService', () => {expect(updateToolMessage).toHaveBeenCalledWith('tool-msg-1',expect.objectContaining({- content: 'Sub-agent did not complete (error).',+ content: 'Sub-agent did not complete (error): boom',pluginError: { message: 'boom' },pluginState: expect.objectContaining({ status: 'error' }),}),);});+ it('falls back to the generic note when the failed child has no error detail', async () => {+ await service.completeSubAgentBridge({+ ...bridgeParams,+ finalState: { ...childState, error: undefined } as any,+ reason: 'error',+ });++ expect(updateToolMessage).toHaveBeenCalledWith(+ 'tool-msg-1',+ expect.objectContaining({+ content: 'Sub-agent did not complete (error).',+ pluginState: expect.objectContaining({ status: 'error' }),+ }),+ );+ });++ it('truncates an oversized child error so it cannot bloat the parent context', async () => {+ const huge = 'x'.repeat(500);+ await service.completeSubAgentBridge({+ ...bridgeParams,+ finalState: { ...childState, error: { message: huge } } as any,+ reason: 'error',+ });++ const call = updateToolMessage.mock.calls.at(-1)?.[1];+ expect(call.content).toBe(`Sub-agent did not complete (error): ${'x'.repeat(300)}β¦`);+ });+it('throws when the backfill reports success: false so the webhook path redelivers', async () => {// updateToolMessage swallows transaction errors into { success: false } β// acking 200 here would strand the parent: the barrier stays unsatisfieddiff --git a/apps/server/src/services/agentRuntime/AgentRuntimeService.ts b/apps/server/src/services/agentRuntime/AgentRuntimeService.tsindex 085bd881bde..4c0fc96a7b8 100644--- a/apps/server/src/services/agentRuntime/AgentRuntimeService.ts+++ b/apps/server/src/services/agentRuntime/AgentRuntimeService.ts@@ -132,6 +132,25 @@ const formatErrorForMetadata = (error: unknown): Record<string, any> | undefinedreturn { message: String(error) };};+/**+ * Extract a short, human-readable reason string from a failed operation's+ * `state.error`, for inlining into the tool-result `content` a parent agent+ * sees. Without this the supervising agent only gets the opaque generic note+ * ("Sub-agent did not complete (error).") and cannot tell *why* a `callAgent`+ * dispatch failed β so it can't retry, switch target, or report the cause; it+ * silently falls back to answering itself (issue #16257). The full structured+ * error still rides on `pluginError`; this is just the readable summary.+ */+const formatSubAgentErrorReason = (error: unknown): string | undefined => {+ const message = formatErrorForMetadata(error)?.message;+ if (typeof message !== 'string') return undefined;+ const trimmed = message.trim();+ if (!trimmed) return undefined;+ // Keep the tool result compact β a runaway provider error body would otherwise+ // bloat the parent's LLM context.+ return trimmed.length > 300 ? `${trimmed.slice(0, 300)}β¦` : trimmed;+};+const toAgentSignalSnapshotEvents = (emission: Awaited<ReturnType<typeof emitAgentSignalSourceEvent>> | undefined,) => {@@ -1938,8 +1957,11 @@ export class AgentRuntimeService {const lastAssistant = [...messages].reverse().find((m: { role?: string }) => m?.role === 'assistant');+ const errorReason = failed ? formatSubAgentErrorReason(finalState?.error) : undefined;const content = failed- ? `Sub-agent did not complete (${reason}).`+ ? errorReason+ ? `Sub-agent did not complete (${reason}): ${errorReason}`+ : `Sub-agent did not complete (${reason}).`: (lastAssistant?.content as string | undefined) ||'Sub-agent completed without a textual answer.';@@ -2106,8 +2128,11 @@ export class AgentRuntimeService {.reverse().find((m: { role?: string }) => m?.role === 'assistant');const agentLabel = (finalState?.metadata?.agentId as string | undefined) ?? 'member';+ const memberErrorReason = failed ? formatSubAgentErrorReason(finalState?.error) : undefined;const anchorContent = failed- ? `Agent member did not complete (${reason}).`+ ? memberErrorReason+ ? `Agent member did not complete (${reason}): ${memberErrorReason}`+ : `Agent member did not complete (${reason}).`: mode === 'in_group'? `Agent ${agentLabel} responded in the group.`: (lastAssistant?.content as string | undefined) ||diff --git a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentManagement.test.ts b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentManagement.test.tsindex c8698356443..770d3046954 100644--- a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentManagement.test.ts+++ b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentManagement.test.ts@@ -158,6 +158,29 @@ describe('agentManagementRuntime', () => {expect(result.deferred).toBeUndefined();});+ it('surfaces the underlying start error so the failure is diagnosable', async () => {+ const run = vi.fn().mockResolvedValue({+ error: 'Agent not found: agent-target',+ started: false,+ threadId: '',+ });+ const runtime = createRuntime();++ const result = await runtime.callAgent(+ { agentId: 'agent-target', instruction: 'Do delegated work' },+ { subAgent: { run }, toolManifestMap: {} },+ );++ expect(result.success).toBe(false);+ expect(result.content).toBe(+ 'Agent "agent-target" failed to start: Agent not found: agent-target',+ );+ expect(result.error).toMatchObject({+ code: 'AGENT_CALL_START_FAILED',+ message: 'Agent "agent-target" failed to start: Agent not found: agent-target',+ });+ });+it('rejects nested server callAgent execution', async () => {const run = vi.fn();const runtime = createRuntime();diff --git a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/lobeAgent.test.ts b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/lobeAgent.test.tsindex a48b80c0a5b..e7243c19186 100644--- a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/lobeAgent.test.ts+++ b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/lobeAgent.test.ts@@ -561,6 +561,22 @@ describe('lobeAgentRuntime', () => {expect(result).toMatchObject({ error: { code: 'SUB_AGENT_START_FAILED' } });});+ it('surfaces the underlying reason when the sub-agent fails to start', async () => {+ const runtime = lobeAgentRuntime.factory(baseContext);+ const run = vi+ .fn()+ .mockResolvedValue({ error: 'QStash queue unavailable', started: false, threadId: '' });++ const result = await runtime.callSubAgent(+ { description: 'Research', instruction: 'Find the answer' },+ { ...baseContext, subAgent: { run } } as ToolExecutionContext,+ );++ expect(result.success).toBe(false);+ expect(result.content).toBe('Sub-agent failed to start: QStash queue unavailable');+ expect(result).toMatchObject({ error: { code: 'SUB_AGENT_START_FAILED' } });+ });+it('fails (not deferred) when no sub-agent runner is available', async () => {const runtime = lobeAgentRuntime.factory(baseContext);diff --git a/apps/server/src/services/toolExecution/serverRuntimes/agentManagement.ts b/apps/server/src/services/toolExecution/serverRuntimes/agentManagement.tsindex 311fba794cd..2dbb4a10649 100644--- a/apps/server/src/services/toolExecution/serverRuntimes/agentManagement.ts+++ b/apps/server/src/services/toolExecution/serverRuntimes/agentManagement.ts@@ -73,7 +73,7 @@ export const agentManagementRuntime: ServerRuntimeRegistration = {}const description = taskTitle || `Call agent ${agentId}`;- const { started, subOperationId, threadId } = await ctx.subAgent.run({+ const { started, error, subOperationId, threadId } = await ctx.subAgent.run({agentId,description,instruction,@@ -81,12 +81,11 @@ export const agentManagementRuntime: ServerRuntimeRegistration = {});if (!started) {+ const detail = error ? `: ${error}` : '.';+ const message = `Agent "${agentId}" failed to start${detail}`;return {- content: `Agent "${agentId}" failed to start.`,- error: {- code: 'AGENT_CALL_START_FAILED',- message: `Agent "${agentId}" failed to start.`,- },+ content: message,+ error: { code: 'AGENT_CALL_START_FAILED', message },success: false,};}diff --git a/apps/server/src/services/toolExecution/serverRuntimes/lobeAgent.ts b/apps/server/src/services/toolExecution/serverRuntimes/lobeAgent.tsindex b6d44d8e763..0a2bde9cbfa 100644--- a/apps/server/src/services/toolExecution/serverRuntimes/lobeAgent.ts+++ b/apps/server/src/services/toolExecution/serverRuntimes/lobeAgent.ts@@ -151,7 +151,7 @@ class LobeAgentExecutionRuntime {return buildError('instruction is required.', 'INVALID_ARGUMENTS');}- const { started, threadId, subOperationId } = await ctx.subAgent.run({+ const { started, error, threadId, subOperationId } = await ctx.subAgent.run({description,instruction,timeout,@@ -162,7 +162,10 @@ class LobeAgentExecutionRuntime {// (non-deferred) tool error so the parent's LLM sees the failure and the// batch continues instead of hanging in `waiting_for_async_tool`.if (!started) {- return buildError('Sub-agent failed to start.', 'SUB_AGENT_START_FAILED');+ return buildError(+ error ? `Sub-agent failed to start: ${error}` : 'Sub-agent failed to start.',+ 'SUB_AGENT_START_FAILED',+ );}return {diff --git a/apps/server/src/services/toolExecution/types.ts b/apps/server/src/services/toolExecution/types.tsindex 2a2ac7ddc94..7ef203a0c70 100644--- a/apps/server/src/services/toolExecution/types.ts+++ b/apps/server/src/services/toolExecution/types.ts@@ -27,6 +27,13 @@ export interface ServerSubAgentRunParams {}export interface ServerSubAgentRunResult {+ /**+ * Reason the child failed to start, when `started` is false. Surfaced to the+ * parent agent's tool result so a `callAgent` dispatch failure is diagnosable+ * (e.g. "Agent not found", config/scheduling error) instead of an opaque+ * "failed to start" β see issue #16257.+ */+ error?: string;/*** Whether the child op was actually forked. `false` means the child failed to* start (e.g. the operation row could not be created/scheduled): no completion