← Back to PRs

#9861: fix(agents): re-run tool_use/tool_result repair after limitHistoryTurns

by CyberSinister open 2026-02-05 19:33 View on GitHub →
agents stale
## Problem `limitHistoryTurns()` can break tool_use/tool_result pairs by cutting in the middle of a tool call sequence. For example, if the limit truncates messages such that a tool_use is included but its corresponding tool_result is cut off, the API will reject with: ``` messages.365: tool_use ids were found without tool_result blocks immediately after: edit_1770316263727_26 ``` ## Root Cause The order of operations was: 1. `sanitizeToolUseResultPairing()` runs - repairs tool pairs ✅ 2. `limitHistoryTurns()` runs - truncates by user turn count 3. **Truncation can break the pairs that were just repaired!** ❌ ## Solution Run `sanitizeToolUseResultPairing()` AFTER `limitHistoryTurns()` to ensure any orphaned tool_use blocks get synthetic error results added. Applied to both: - `run/attempt.ts` (main execution path) - `compact.ts` (compaction execution path) ## Testing - Build passes ✅ - Simple fix with minimal risk - just reorders existing sanitization logic ## Related Fixes the root cause of #8525-type issues where model switching after truncation could cause API rejections. <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR correctly addresses a real failure mode where `limitHistoryTurns()` can truncate session history mid tool-call sequence, leaving orphaned `tool_use` blocks. The change re-runs `sanitizeToolUseResultPairing()` after truncation in both execution paths (`src/agents/pi-embedded-runner/run/attempt.ts` and `src/agents/pi-embedded-runner/compact.ts`), ensuring transcripts remain acceptable to strict providers. However, the PR also introduces three new operational/agent helper files (`ecosystem.config.cjs`, `skills/coding-agent/scripts/*`) that are unrelated to the stated fix. Two of these introduce must-fix issues: a PM2 config with hard-coded machine paths and a wrapper script that mutates the user’s project directory by running `git init` when missing. <h3>Confidence Score: 3/5</h3> - This PR is not safe to merge as-is due to unrelated operational/script additions with problematic side effects. - The core agent transcript repair reordering looks correct and consistent with existing sanitization logic, but the PR also adds a machine-specific PM2 config and a helper script that runs `git init` in arbitrary directories, which are changes the repo likely doesn’t want and can break user environments. - ecosystem.config.cjs, skills/coding-agent/scripts/samantha-code.sh <!-- 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> <!-- /greptile_comment -->

Most Similar PRs