#8312: fix: add logging and markers for tool result repair
agents
stale
Cluster:
Error Handling in Agent Tools
## Summary
Adds diagnostic logging and session markers when tool result repairs are applied during session sanitization.
## Problem
Sessions can end up with corrupted tool use/result pairing when interrupted mid-execution. This causes the `unexpected tool_use_id found in tool_result blocks` error from Anthropic API. While the repair mechanism exists, it was silent - making it hard to diagnose when and why repairs were needed.
## Changes
1. **Use detailed repair report**: Switch from `sanitizeToolUseResultPairing` (simple wrapper) to `repairToolUseResultPairing` (returns detailed report)
2. **Log repairs**: Warn when repairs are applied with diagnostic info (session ID, counts, provider)
3. **Mark repaired sessions**: Add custom entry to prevent repeated warnings for same session
4. **Add tests**: Comprehensive coverage for edge cases
## Test coverage
- Pending tool calls at end of transcript
- Multiple pending tool calls
- Orphan tool results before any assistant message
- Interleaved tool calls and results
## Related
Refs: #5497 (unexpected tool_use_id error)
---
*Submitted from fork: ekson73/openclaw*
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR switches Google embedded runner session sanitization from the wrapper `sanitizeToolUseResultPairing()` to `repairToolUseResultPairing()` so it can log a detailed repair report (added synthetic results, dropped orphans/duplicates, whether messages were moved) and persist a custom session marker to avoid repeated warnings. It also expands unit tests around transcript repair edge cases (pending tool calls at end, multiple pending calls, early orphan results, and already-valid interleavings).
The main concern is a TS/ESM module parsing issue in `google.ts` where a `const` declaration is placed between import statements, which will break builds in many configurations.
<h3>Confidence Score: 3/5</h3>
- This PR is close to safe to merge, but has a likely build-breaking module syntax issue in google.ts.
- The functional change (logging + session marker) is straightforward and covered by tests, but placing a non-import statement between import declarations can cause TypeScript/ESM parse failures depending on the build toolchain; that needs to be fixed before merge.
- src/agents/pi-embedded-runner/google.ts
<!-- greptile_other_comments_section -->
<sub>(4/5) You can add custom instructions or style guidelines for the agent [here](https://app.greptile.com/review/github)!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#8345: fix: prevent synthetic error repair from creating tool_result for d...
by vishaltandale00 · 2026-02-03
90.1%
#6687: fix(session-repair): strip malformed tool_use blocks to prevent per...
by NSEvent · 2026-02-01
88.5%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
87.7%
#4700: fix: deduplicate tool_use IDs and enable sanitization for Anthropic
by marcelomar21 · 2026-01-30
86.2%
#3647: fix: sanitize tool arguments in session history
by nhangen · 2026-01-29
84.4%
#4844: fix(agents): skip error/aborted assistant messages in transcript re...
by lailoo · 2026-01-30
84.3%
#15509: fix(session): drop tool_use blocks with empty or missing name
by aldoeliacim · 2026-02-13
84.3%
#9861: fix(agents): re-run tool_use/tool_result repair after limitHistoryT...
by CyberSinister · 2026-02-05
84.2%
#19094: Fix empty tool_call_id and function names in provider transcript pa...
by yxshee · 2026-02-17
84.1%
#13062: fix: remove orphaned tool_result blocks from user message content d...
by trevorgordon981 · 2026-02-10
83.2%