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-runtime): tell the parent why a watchdog-abandoned sub-agent failed (#20068)

Arvin Xucba9f8f7e27d
diff --git a/apps/server/src/router-hono/agent/handlers/__tests__/subAgentCallback.test.ts b/apps/server/src/router-hono/agent/handlers/__tests__/subAgentCallback.test.ts
index 9d83577e5e5..d4e2a7cb3fe 100644
--- a/apps/server/src/router-hono/agent/handlers/__tests__/subAgentCallback.test.ts
+++ b/apps/server/src/router-hono/agent/handlers/__tests__/subAgentCallback.test.ts
@@ -122,6 +122,26 @@ describe('subAgentCallback handler', () => {
);
});
+ it('forwards the abandon reason so the parent sees why the sub-agent failed', async () => {
+ mockCompleteSubAgentBridge.mockResolvedValue(true);
+ const { ctx } = buildContext({
+ body: {
+ ...validBody,
+ errorMessage: 'Operation abandoned: inactivity_watchdog',
+ reason: 'error',
+ },
+ });
+
+ await subAgentCallback(ctx);
+
+ expect(mockCompleteSubAgentBridge).toHaveBeenCalledWith(
+ expect.objectContaining({
+ errorMessage: 'Operation abandoned: inactivity_watchdog',
+ reason: 'error',
+ }),
+ );
+ });
+
it('defaults reason to done and threadId to empty string when absent', async () => {
mockCompleteSubAgentBridge.mockResolvedValue(false);
const { ctx } = buildContext({
diff --git a/apps/server/src/router-hono/agent/handlers/finalizeAbandoned.ts b/apps/server/src/router-hono/agent/handlers/finalizeAbandoned.ts
index 875a2b47833..7ae36238c7b 100644
--- a/apps/server/src/router-hono/agent/handlers/finalizeAbandoned.ts
+++ b/apps/server/src/router-hono/agent/handlers/finalizeAbandoned.ts
@@ -53,11 +53,19 @@ export async function finalizeAbandoned(c: Context): Promise<Response> {
// being swallowed behind a 200: a 200 here would falsely report the parent
// as handled while it stays parked forever.
if (result.subAgentResume) {
- const { parentOperationId, streamOwnerUserId, threadId, toolMessageId, userId, workspaceId } =
- result.subAgentResume;
+ const {
+ errorMessage,
+ parentOperationId,
+ streamOwnerUserId,
+ threadId,
+ toolMessageId,
+ userId,
+ workspaceId,
+ } = result.subAgentResume;
// Child reached a terminal failure (watchdog kill) β†’ the bridge backfills
// the parent's tool slot with an error note rather than a stub answer.
const bridgeBody = {
+ errorMessage,
operationId,
parentOperationId,
reason: 'error',
diff --git a/apps/server/src/router-hono/agent/handlers/subAgentCallback.ts b/apps/server/src/router-hono/agent/handlers/subAgentCallback.ts
index d6a12da24cd..f4fa03c5074 100644
--- a/apps/server/src/router-hono/agent/handlers/subAgentCallback.ts
+++ b/apps/server/src/router-hono/agent/handlers/subAgentCallback.ts
@@ -16,9 +16,11 @@ const log = debug('lobe-server:agent:subagent-callback');
* Backfills the parent's placeholder tool message and barrier-resumes the
* parked parent op via `completeSubAgentBridge`.
*
- * Body: `{ operationId, reason, parentOperationId, threadId, toolMessageId }`
+ * Body: `{ operationId, reason, parentOperationId, threadId, toolMessageId, errorMessage? }`
* β€” event fields from the hook dispatch plus the bridge params from
- * `webhook.body`.
+ * `webhook.body`. `errorMessage` is set by the watchdog abandon path, whose
+ * child state never records the failure; without it the parent reads a bare
+ * "Sub-agent did not complete (error).".
*
* Auth: `qstashAuth` on the route β€” QStash signature required.
*/
@@ -30,7 +32,7 @@ export async function subAgentCallback(c: Context): Promise<Response> {
return c.json({ error: 'Invalid JSON body' }, 400);
}
- const { operationId, parentOperationId, reason, threadId, toolMessageId } = body;
+ const { errorMessage, operationId, parentOperationId, reason, threadId, toolMessageId } = body;
log(
'subagent-callback: operationId=%s, parentOperationId=%s, reason=%s, toolMessageId=%s',
@@ -74,6 +76,7 @@ export async function subAgentCallback(c: Context): Promise<Response> {
});
const resumed = await aiAgentService.completeSubAgentBridge({
+ errorMessage: typeof errorMessage === 'string' ? errorMessage : undefined,
operationId,
parentOperationId,
reason: reason ?? 'done',
diff --git a/apps/server/src/services/agentRuntime/AbandonOperationService.ts b/apps/server/src/services/agentRuntime/AbandonOperationService.ts
index 704574c6032..9adc2531c67 100644
--- a/apps/server/src/services/agentRuntime/AbandonOperationService.ts
+++ b/apps/server/src/services/agentRuntime/AbandonOperationService.ts
@@ -35,6 +35,12 @@ interface AbandonOperationOptions {
* `waiting_for_async_tool` forever (the orphaned-parent bug).
*/
export interface AbandonedSubAgentResume {
+ /**
+ * Why the child was abandoned. The bridge otherwise reloads the child's
+ * coordinator state, which never received this error, and hands the parent a
+ * bare "Sub-agent did not complete (error)." with no cause.
+ */
+ errorMessage: string;
parentOperationId: string;
/**
* When true, the parent op is a shared-agent visitor run (its metadata
@@ -245,6 +251,7 @@ export class AbandonOperationService {
undefined;
if (toolMessageId) {
result.subAgentResume = {
+ errorMessage: message,
parentOperationId,
// Forward the visitor-run marker so an inline resume (local mode)
// constructs its services with `includeShareVisitor: true`; the
diff --git a/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts b/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts
index 5378ca6a2c4..cf221ece802 100644
--- a/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts
+++ b/apps/server/src/services/agentRuntime/AgentRuntimeService.test.ts
@@ -3632,6 +3632,24 @@ describe('AgentRuntimeService', () => {
);
});
+ // A watchdog-abandoned child's reloaded state has no error; the abandon
+ // hand-off passes it explicitly. Without it the parent got a bare "(error).".
+ it('explains a watchdog-abandoned child from the hand-off error message', async () => {
+ await service.completeSubAgentBridge({
+ ...bridgeParams,
+ errorMessage: 'Operation abandoned: inactivity_watchdog',
+ finalState: { ...childState, error: undefined } as any,
+ reason: 'error',
+ });
+
+ const call = updateToolMessage.mock.calls.at(-1)?.[1];
+ expect(call.content).toBe(
+ 'Sub-agent did not complete (error): the sub-agent stopped making progress and the platform ended it (inactivity_watchdog). ' +
+ 'Anything it already did (searches, pages read, documents written) is kept in its thread.',
+ );
+ expect(call.pluginError).toEqual({ message: 'Operation abandoned: inactivity_watchdog' });
+ });
+
it('truncates an oversized child error so it cannot bloat the parent context', async () => {
const huge = 'x'.repeat(500);
await service.completeSubAgentBridge({
diff --git a/apps/server/src/services/agentRuntime/AgentRuntimeService.ts b/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
index 184a83cd3e9..cd81499ba76 100644
--- a/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
+++ b/apps/server/src/services/agentRuntime/AgentRuntimeService.ts
@@ -19,6 +19,7 @@ import {
import type { ISnapshotStore } from '@lobechat/agent-tracing';
import { appendSubAgentReference, isCallSubAgentCall } from '@lobechat/builtin-tool-lobe-agent';
import { dynamicInterventionAudits } from '@lobechat/builtin-tools/dynamicInterventionAudits';
+import { ABANDONED_OPERATION_ERROR_PREFIX } from '@lobechat/const/goal';
import { parse } from '@lobechat/conversation-flow';
import { getModelPropertyWithFallback } from '@lobechat/model-runtime';
import {
@@ -315,6 +316,15 @@ const formatSubAgentErrorReason = (error: unknown): string | undefined => {
if (typeof message !== 'string') return undefined;
const trimmed = message.trim();
if (!trimmed) return undefined;
+ // "Operation abandoned: inactivity_watchdog" names an internal mechanism;
+ // the parent needs to know the child went silent and that its work survives.
+ if (trimmed.startsWith(ABANDONED_OPERATION_ERROR_PREFIX)) {
+ const cause = trimmed.slice(ABANDONED_OPERATION_ERROR_PREFIX.length).trim();
+ return (
+ `the sub-agent stopped making progress and the platform ended it (${cause || 'abandoned'}). ` +
+ 'Anything it already did (searches, pages read, documents written) is kept in its thread.'
+ );
+ }
// 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;
@@ -3493,7 +3503,9 @@ export class AgentRuntimeService {
);
}
}
- const errorReason = failed ? formatSubAgentErrorReason(finalState?.error) : undefined;
+ const errorReason = failed
+ ? formatSubAgentErrorReason(finalState?.error ?? params.errorMessage)
+ : undefined;
const resultContent = failed
? errorReason
? `Sub-agent did not complete (${reason}): ${errorReason}`
@@ -3508,7 +3520,9 @@ export class AgentRuntimeService {
const backfill = await this.messageModel.updateToolMessage(toolMessageId, {
content,
- pluginError: failed ? formatErrorForMetadata(finalState?.error) : undefined,
+ pluginError: failed
+ ? formatErrorForMetadata(finalState?.error ?? params.errorMessage)
+ : undefined,
pluginState: {
model: finalState?.modelRuntimeConfig?.model,
status: failed ? 'error' : 'completed',
diff --git a/apps/server/src/services/agentRuntime/__tests__/AbandonOperationService.test.ts b/apps/server/src/services/agentRuntime/__tests__/AbandonOperationService.test.ts
index f76a5e13215..67d955388ae 100644
--- a/apps/server/src/services/agentRuntime/__tests__/AbandonOperationService.test.ts
+++ b/apps/server/src/services/agentRuntime/__tests__/AbandonOperationService.test.ts
@@ -588,6 +588,9 @@ describe('AbandonOperationService', () => {
const result = await svc.finalizeAbandoned('op_child', 'inactivity_watchdog');
expect(result.subAgentResume).toEqual({
+ // The child's coordinator state never gets this error, so the resume
+ // hand-off must carry it or the parent only sees a bare "(error)."
+ errorMessage: 'Operation abandoned: inactivity_watchdog',
parentOperationId: 'op_parent',
threadId: 'thread_1',
toolMessageId: 'msg_tool_placeholder',
diff --git a/apps/server/src/services/agentRuntime/types.ts b/apps/server/src/services/agentRuntime/types.ts
index de320f08a66..1c9ae103c2f 100644
--- a/apps/server/src/services/agentRuntime/types.ts
+++ b/apps/server/src/services/agentRuntime/types.ts
@@ -268,6 +268,12 @@ export interface AgentExecutionResult {
* `AgentRuntimeService.completeSubAgentBridge`.
*/
export interface SubAgentBridgeParams {
+ /**
+ * Failure reason known to the caller but absent from the child's stored
+ * state β€” set when the watchdog abandoned the child, whose coordinator state
+ * was never marked errored.
+ */
+ errorMessage?: string;
/** Child op's final state β€” passed in local mode; loaded from the coordinator otherwise. */
finalState?: AgentState;
/** Child (sub-agent) operation ID. */