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: gateway client-tool pluginState + drop redundant `Exit code: 0` tail (#14596)

Arvin Xu4bfd4345521e
diff --git a/packages/prompts/src/prompts/fileSystem/formatCommandResult.test.ts b/packages/prompts/src/prompts/fileSystem/formatCommandResult.test.ts
index 0333e1d2574..342b72776f7 100644
--- a/packages/prompts/src/prompts/fileSystem/formatCommandResult.test.ts
+++ b/packages/prompts/src/prompts/fileSystem/formatCommandResult.test.ts
@@ -44,15 +44,25 @@ describe('formatCommandResult', () => {
`);
});
- it('should format successful command with exit code', () => {
+ it('should suppress the Exit code line when exitCode is 0', () => {
const result = formatCommandResult({
exitCode: 0,
success: true,
});
+ expect(result).toMatchInlineSnapshot(`"Command completed successfully."`);
+ });
+
+ it('should treat a non-zero exit code as failure even when envelope success is true', () => {
+ const result = formatCommandResult({
+ exitCode: 137,
+ stdout: 'partial output',
+ success: true,
+ });
expect(result).toMatchInlineSnapshot(`
- "Command completed successfully.
+ "Command failed with exit code 137
- Exit code: 0"
+ Output:
+ partial output"
`);
});
@@ -73,15 +83,13 @@ describe('formatCommandResult', () => {
success: false,
});
expect(result).toMatchInlineSnapshot(`
- "Command failed: Command error
+ "Command failed with exit code 1: Command error
Output:
Some output
Stderr:
- Error occurred
-
- Exit code: 1"
+ Error occurred"
`);
});
});
diff --git a/packages/prompts/src/prompts/fileSystem/formatCommandResult.ts b/packages/prompts/src/prompts/fileSystem/formatCommandResult.ts
index 29e3d314f9e..5004ec9b9bd 100644
--- a/packages/prompts/src/prompts/fileSystem/formatCommandResult.ts
+++ b/packages/prompts/src/prompts/fileSystem/formatCommandResult.ts
@@ -17,19 +17,25 @@ export const formatCommandResult = ({
}: FormatCommandResultParams): string => {
const parts: string[] = [];
- if (success) {
- if (shellId) {
- parts.push(`Command started in background with shell_id: ${shellId}`);
- } else {
- parts.push('Command completed successfully.');
- }
+ // `success` is the envelope ("service responded"); `exitCode` is the command
+ // itself. Treat a non-zero exit as failure regardless of envelope success,
+ // so we never render "Command completed successfully." over a 137/130/etc.
+ const hasNonZeroExit = exitCode !== undefined && exitCode !== 0;
+ const failed = !success || hasNonZeroExit;
+
+ if (failed) {
+ let header = 'Command failed';
+ if (hasNonZeroExit) header += ` with exit code ${exitCode}`;
+ if (error) header += `: ${error}`;
+ parts.push(header);
+ } else if (shellId) {
+ parts.push(`Command started in background with shell_id: ${shellId}`);
} else {
- parts.push(`Command failed: ${error}`);
+ parts.push('Command completed successfully.');
}
if (stdout) parts.push(`Output:\n${stdout}`);
if (stderr) parts.push(`Stderr:\n${stderr}`);
- if (exitCode !== undefined) parts.push(`Exit code: ${exitCode}`);
return parts.join('\n\n');
};
diff --git a/src/server/agent-hono/handlers/__tests__/toolResult.test.ts b/src/server/agent-hono/handlers/__tests__/toolResult.test.ts
index e1d72277bc6..4a545700295 100644
--- a/src/server/agent-hono/handlers/__tests__/toolResult.test.ts
+++ b/src/server/agent-hono/handlers/__tests__/toolResult.test.ts
@@ -96,6 +96,19 @@ describe('toolResult handler', () => {
expect(mockPipelineExec).toHaveBeenCalled();
});
+ it('preserves the optional state field through validation and into the LPUSHed payload', async () => {
+ const bodyWithState = {
+ ...validBody,
+ state: { cwd: '/Users/x', cursor: 12 },
+ };
+ const { ctx } = buildContext(bodyWithState);
+ const res = await toolResult(ctx);
+ expect(res.status).toBe(204);
+ const args = mockLpush.mock.calls[0] as unknown as [string, string];
+ const persisted = JSON.parse(args[1]);
+ expect(persisted.state).toEqual({ cwd: '/Users/x', cursor: 12 });
+ });
+
it('returns 503 when Redis pipeline exec throws', async () => {
mockPipelineExec.mockRejectedValueOnce(new Error('redis down'));
const { ctx } = buildContext(validBody);
diff --git a/src/server/agent-hono/handlers/toolResult.ts b/src/server/agent-hono/handlers/toolResult.ts
index 6dd5068bb59..578c11f2bfa 100644
--- a/src/server/agent-hono/handlers/toolResult.ts
+++ b/src/server/agent-hono/handlers/toolResult.ts
@@ -16,6 +16,7 @@ const ToolResultBodySchema = z.object({
type: z.string().optional(),
})
.optional(),
+ state: z.record(z.string(), z.any()).optional(),
success: z.boolean(),
toolCallId: z.string().min(1),
});
diff --git a/src/server/modules/AgentRuntime/ToolResultWaiter.ts b/src/server/modules/AgentRuntime/ToolResultWaiter.ts
index 737881b16f7..0c595d1ee9b 100644
--- a/src/server/modules/AgentRuntime/ToolResultWaiter.ts
+++ b/src/server/modules/AgentRuntime/ToolResultWaiter.ts
@@ -9,6 +9,7 @@ export interface ToolResultPayload {
message: string;
type?: string;
};
+ state?: Record<string, any>;
success: boolean;
toolCallId: string;
}
diff --git a/src/server/modules/AgentRuntime/__tests__/dispatchClientTool.test.ts b/src/server/modules/AgentRuntime/__tests__/dispatchClientTool.test.ts
index 6380d1a42ac..5d4882421d6 100644
--- a/src/server/modules/AgentRuntime/__tests__/dispatchClientTool.test.ts
+++ b/src/server/modules/AgentRuntime/__tests__/dispatchClientTool.test.ts
@@ -128,6 +128,29 @@ describe('dispatchClientTool', () => {
expect(mockDisconnect).toHaveBeenCalled();
});
+ it('forwards pluginState (state field) from the BLPOP payload to the execution result', async () => {
+ const sendToolExecute = vi.fn().mockResolvedValue(undefined);
+ const streamManager = makeStreamManager(sendToolExecute);
+
+ const state = { cursor: 7, mode: 'preview' };
+ mockBlpop.mockResolvedValue([
+ 'tool_result:call-1',
+ JSON.stringify({
+ content: 'file contents',
+ state,
+ success: true,
+ toolCallId: 'call-1',
+ }),
+ ]);
+
+ const result = await dispatchClientTool(makePayload(), {
+ operationId: 'op-1',
+ streamManager,
+ });
+
+ expect(result.state).toEqual(state);
+ });
+
it('returns a timeout result and still disconnects when BLPOP times out', async () => {
const sendToolExecute = vi.fn().mockResolvedValue(undefined);
const streamManager = makeStreamManager(sendToolExecute);
diff --git a/src/server/modules/AgentRuntime/dispatchClientTool.ts b/src/server/modules/AgentRuntime/dispatchClientTool.ts
index abb30be1837..8602f214986 100644
--- a/src/server/modules/AgentRuntime/dispatchClientTool.ts
+++ b/src/server/modules/AgentRuntime/dispatchClientTool.ts
@@ -139,6 +139,7 @@ function projectToExecutionResult(
content: payload.content ?? '',
error: payload.error,
executionTime,
+ state: payload.state,
success: payload.success,
};
}