fix: sanitize tool arguments in session history#3647
fix: sanitize tool arguments in session history#3647nhangen wants to merge 3 commits intoopenclaw:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements sanitization for tool arguments in session history to prevent crashes caused by malformed JSON in tool input fields. When invalid JSON is detected in tool use blocks, it is replaced with an empty object {} to prevent InternalError.Algo.InvalidParameter errors that would otherwise crash the entire session.
Changes:
- Added
sanitizeToolUseArgsfunction to detect and repair malformed JSON in tool input arguments - Integrated sanitization into the
sanitizeToolUseResultPairingpipeline to ensure it runs before tool result pairing repair - Preserved original malformed input in
_originalInputmetadata field for debugging
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Working on addressing copilot feedback and passing checks. |
Addresses review feedback: parse valid JSON strings, add type guards, add logging, handle arguments alias.
f1a3e18 to
ccf00e1
Compare
Additional Comments (2)
If Consider only accepting parsed values where Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agents/session-transcript-repair.ts
Line: 99:103
Comment:
[P1] `sanitizeToolUseArgs` will parse any JSON string (including primitives/arrays) and store it back into `input`/`arguments`, even though downstream code/tools typically expect an object.
If `toolBlock[inputField]` is something like `"null"`, `"[]"`, or `"\"str\""`, `JSON.parse` succeeds and you’ll set `input` to `null`/array/string, which can still trigger `InvalidParameter`-style schema/type errors later.
Consider only accepting parsed values where `parsed` is a non-null plain object; otherwise treat it as malformed and replace with `{}` (or wrap as `{ value: parsed }` if you need to preserve it).
How can I resolve this? If you propose a fix, please make it concise.
If these logs are shipped/retained, consider logging only metadata (tool name + length + error type) or truncating more aggressively/redacting common secret patterns. Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agents/session-transcript-repair.ts
Line: 111:114
Comment:
[P2] The warning log includes a snippet of the original tool input (`Original: ${sample}`), which can leak sensitive data from session history (paths, tokens, prompts, etc.) into logs.
If these logs are shipped/retained, consider logging only metadata (tool name + length + error type) or truncating more aggressively/redacting common secret patterns.
How can I resolve this? If you propose a fix, please make it concise. |
bfc1ccb to
f92900f
Compare
Pull Request: fix: sanitize tool arguments in session history
📋 Summary
This PR implements robust sanitization for tool arguments in session history messages. It specifically targets potential corruption where tool input arguments contain invalid JSON. The fix detects these malformed inputs and replaces them with an empty object
{}during message loading, preventing the entire session from crashing due toInternalError.Algo.InvalidParameter.🎯 Related Issues
Closes # (Session Crash Bugs)
🚀 What's New
Core Changes
1. Session Transcript Repair
Purpose: To make the agent resilient to malformed session data ("dirty memory") caused by previous model errors or hallucinations.
Implementation:
sanitizeToolUseArgsfunction insrc/agents/session-transcript-repair.ts.sanitizeToolUseResultPairingpipeline.toolUseinput block; ifJSON.parsefails, it catches the error and repairs the block.Key Code:
📊 Type of Change
🧪 Testing
Automated Tests
Test Results:
Verified with a reproduction script (
verify-sanitization.js) that constructed a message with a malformed input string"{ bad: json }".{}and script succeeded.Manual Testing
Testing Checklist:
Environments Tested:
🚀 Deployment Strategy
Deployment Steps
npm run build).Configuration Changes
None.
🔍 Code Quality
🔐 Security Considerations
🚦 Status
Greptile Overview
Greptile Summary
This PR adds a transcript-repair step (
sanitizeToolUseArgs) that scans assistant tool-call blocks in session history and normalizes tool arguments: valid JSON strings ininput/argumentsare parsed into objects, and malformed JSON strings are replaced with{}while tagging the block as sanitized. The sanitization is then run as part ofsanitizeToolUseResultPairingbefore repairing toolResult ordering/duplication.Overall this strengthens resilience against “dirty” session files that would otherwise crash or be rejected by strict providers when tool-call arguments are not valid JSON.
Confidence Score: 4/5
(4/5) You can add custom instructions or style guidelines for the agent here!