π€― 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(context-engine): diagnose synthetic tool failures with reason and tool (#19116)
Neko664f8582e649
diff --git a/packages/context-engine/src/processors/ToolMessageReorder.ts b/packages/context-engine/src/processors/ToolMessageReorder.tsindex f8ef0c86de6..1d740c0dfd6 100644--- a/packages/context-engine/src/processors/ToolMessageReorder.ts+++ b/packages/context-engine/src/processors/ToolMessageReorder.ts@@ -15,11 +15,37 @@ declare module '../types' {const log = debug('context-engine:processor:ToolMessageReorder');-const DEFAULT_TOOL_FAILURE_CONTENT = JSON.stringify({- error: 'Tool call failed',- success: false,- synthetic: true,-});+/**+ * Why a synthetic failure payload was injected. The model sees these fields,+ * so the wording doubles as a diagnosis: it can tell whether the tool may+ * still have executed (result lost in transport) versus ran and produced+ * nothing usable β instead of guessing from an opaque "Tool call failed".+ */+type SyntheticToolFailureReason = 'tool_result_missing' | 'tool_result_unusable';++/**+ * Model-readable hint per reason. The retry-safety distinction is the point:+ * a missing result says nothing about whether the call executed β it may well+ * have β so blindly retrying a side-effecting tool can repeat its effects.+ * Kept to one sentence each: this payload rides along in every request until+ * the failed call ages out of context.+ */+export const SYNTHETIC_TOOL_FAILURE_HINTS: Record<SyntheticToolFailureReason, string> = {+ tool_result_missing:+ 'No result arrived, so whether the call actually executed is unknown; check observable state before retrying tools with side effects.',+ tool_result_unusable:+ 'The call returned no readable content and no error message; check the inputs before retrying, as a retry may return the same.',+};++export const syntheticToolFailureContent = (reason: SyntheticToolFailureReason, tool?: string) =>+ JSON.stringify({+ error: 'Tool call failed',+ hint: SYNTHETIC_TOOL_FAILURE_HINTS[reason],+ reason,+ success: false,+ synthetic: true,+ ...(tool ? { tool } : {}),+ });/*** Whether a tool result still carries something the model can read.@@ -30,7 +56,7 @@ const DEFAULT_TOOL_FAILURE_CONTENT = JSON.stringify({* 'image_url' }]` β whenever the model supports vision. A bare* `typeof content === 'string'` guard therefore rejects exactly the results* that carry an image: this pass would drop the parts and hand the model- * {@link DEFAULT_TOOL_FAILURE_CONTENT} instead, so a successful screenshot read+ * {@link syntheticToolFailureContent} instead, so a successful screenshot read* arrived as "Tool call failed" and the image never reached the request at all.*/const hasUsableToolContent = (content: unknown, pluginErrorMessage?: string): boolean => {@@ -172,18 +198,24 @@ export class ToolMessageReorder extends BaseProcessor {? matchedToolMessage.pluginError.message: undefined;+ const toolName = toolCall.function?.name;+reorderedMessages.push({...matchedToolMessage,content: hasUsableToolContent(matchedToolMessage.content, pluginErrorMessage)? matchedToolMessage.content- : pluginErrorMessage || DEFAULT_TOOL_FAILURE_CONTENT,+ : pluginErrorMessage || syntheticToolFailureContent('tool_result_unusable', toolName),});toolMessages.delete(toolCall.id);continue;}reorderedMessages.push({- content: DEFAULT_TOOL_FAILURE_CONTENT,+ // The tool result never arrived (transport loss, gateway error,+ // crash). Flag it explicitly so the model can tell this apart from a+ // tool that ran and returned something unusable β and so retry+ // decisions are not made blind.+ content: syntheticToolFailureContent('tool_result_missing', toolCall.function?.name),...(toolCall.function?.name && { name: toolCall.function.name }),role: 'tool',tool_call_id: toolCall.id,diff --git a/packages/context-engine/src/processors/__tests__/ToolMessageReorder.test.ts b/packages/context-engine/src/processors/__tests__/ToolMessageReorder.test.tsindex 8be809c2d00..3785c996f59 100644--- a/packages/context-engine/src/processors/__tests__/ToolMessageReorder.test.ts+++ b/packages/context-engine/src/processors/__tests__/ToolMessageReorder.test.ts@@ -1,7 +1,11 @@import { describe, expect, it } from 'vitest';import type { PipelineContext } from '../../types';-import { ToolMessageReorder } from '../ToolMessageReorder';+import {+ SYNTHETIC_TOOL_FAILURE_HINTS,+ syntheticToolFailureContent,+ ToolMessageReorder,+} from '../ToolMessageReorder';const createContext = (messages: any[]): PipelineContext => ({initialState: { messages: [] } as any,@@ -222,7 +226,7 @@ describe('ToolMessageReorder', () => {],},{- content: '{"error":"Tool call failed","success":false,"synthetic":true}',+ content: syntheticToolFailureContent('tool_result_missing', 'test-plugin____testApi'),name: 'test-plugin____testApi',role: 'tool',tool_call_id: 'call_missing',@@ -361,8 +365,72 @@ describe('ToolMessageReorder', () => {const result = await proc.process(ctx);- const failure = JSON.stringify({ error: 'Tool call failed', success: false, synthetic: true });+ const failure = JSON.stringify({+ error: 'Tool call failed',+ hint: SYNTHETIC_TOOL_FAILURE_HINTS.tool_result_unusable,+ reason: 'tool_result_unusable',+ success: false,+ synthetic: true,+ tool: 'readFile',+ });expect(result.messages[1].content).toBe(failure);expect(result.messages[2].content).toBe(failure);});++ it('should mark a missing tool result with the tool name and the missing reason', async () => {+ const proc = new ToolMessageReorder();+ const ctx = createContext([+ {+ id: 'a1',+ role: 'assistant',+ content: '',+ tool_calls: [+ {+ function: { arguments: '{}', name: 'lobe-local-system____runCommand' },+ id: 'call_1',+ type: 'function',+ },+ ],+ },+ // No tool message at all β e.g. the result was lost to a gateway 503/504+ // while the tool may well have executed on the device.+ ]);++ const result = await proc.process(ctx);++ const failure = result.messages[1].content as string;+ expect(typeof failure).toBe('string');++ const parsed = JSON.parse(failure);+ expect(parsed).toEqual({+ error: 'Tool call failed',+ hint: SYNTHETIC_TOOL_FAILURE_HINTS.tool_result_missing,+ reason: 'tool_result_missing',+ success: false,+ synthetic: true,+ tool: 'lobe-local-system____runCommand',+ });+ // Structured fields must stay machine-readable, not flattened into prose.+ expect(parsed.reason).toBeDefined();+ });++ it('should give the two failure reasons distinct, actionable hints', async () => {+ const missing = JSON.parse(syntheticToolFailureContent('tool_result_missing', 'runCommand'));+ const unusable = JSON.parse(syntheticToolFailureContent('tool_result_unusable', 'runCommand'));++ // The missing-result hint must carry the retry-safety warning the old+ // payload lacked: a lost result says nothing about whether the call+ // executed, so the model is told to check observable state first.+ expect(missing.hint).toContain('executed is unknown');+ expect(missing.hint).toContain('check observable state');+ expect(missing.hint).toContain('side effects');++ // The unusable-result hint must not imply the call may not have run β+ // a result row exists, the call definitely executed and returned empty.+ expect(unusable.hint).not.toContain('executed is unknown');+ expect(unusable.hint).toContain('check the inputs');++ // The hints must be distinguishable, or the field adds noise.+ expect(missing.hint).not.toBe(unusable.hint);+ });});