Skip to content

fix(agent): safely serialize custom tool results - #77

Open
imMamdouhaboammar wants to merge 3 commits into
MagicCube:mainfrom
imMamdouhaboammar:fix/tool-result-safe-serialization
Open

fix(agent): safely serialize custom tool results#77
imMamdouhaboammar wants to merge 3 commits into
MagicCube:mainfrom
imMamdouhaboammar:fix/tool-result-safe-serialization

Conversation

@imMamdouhaboammar

Copy link
Copy Markdown

Summary

Prevent custom or extension tools from crashing the agent loop when their returned data cannot be serialized with JSON.stringify().

Root cause

normalizeToolResult() already falls back to a readable summary when an arbitrary object cannot be stringified. But formatToolResultForMessage() then included the original raw data again and stringifyWithinLimit() called JSON.stringify(payload) without a catch.

Circular objects, BigInt values, or unserializable structured error details could therefore throw from the formatter itself. That formatting happens after the tool invocation promise resolves, outside the tool implementation's own error handling, so a custom tool result could terminate the agent loop instead of producing a bounded tool message.

Changes

  • add fail-safe JSON serialization inside stringifyWithinLimit()
  • fall back to the already-normalized summary/error payload when full data cannot be serialized
  • preserve existing size-limit behavior for serializable results
  • add regression coverage for circular success data, raw BigInt-containing results, and circular structured error details

This keeps extension-tool output failures contained at the tool-message boundary instead of letting serialization details crash the active run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant