#2541: fix(agents): add error handling to orphaned message cleanup
agents
Cluster:
Error Handling in Agent Tools
## Summary
- Adds try-catch error handling to orphaned message cleanup in pi-embedded-runner
- Prevents message processing queue from blocking when cleanup fails
- Logs errors with context instead of crashing the agent run
## Root Cause
The orphaned message cleanup code (lines 715-729 in `attempt.ts`) was not wrapped in error handling. If any of the cleanup operations (`sessionManager.branch()`, `sessionManager.resetLeaf()`, `sessionManager.buildSessionContext()`, or `activeSession.agent.replaceMessages()`) threw an exception, it would cause the entire agent run to fail, potentially blocking the Gateway's message processing queue.
## Changes
- Wrapped orphaned message cleanup logic in try-catch block
- Added error logging with proper type handling for TypeScript
- Ensured agent run continues even if cleanup fails
## Test Plan
- [x] Existing test "repairs orphaned user messages and continues" passes
- [x] All 7 tests in pi-embedded-runner.test.ts pass
- [x] Lint checks pass
- [x] TypeScript build succeeds
Fixes #2262
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR wraps the “orphaned trailing user message” repair in `src/agents/pi-embedded-runner/run/attempt.ts` with a `try/catch` so failures in session cleanup no longer abort the embedded agent run. It logs a warning when it successfully repairs the leaf entry and logs an error (but continues) if the repair path throws, reducing the chance of the gateway’s processing queue being blocked by an exception.
Also adds a changelog entry describing the fix.
<h3>Confidence Score: 4/5</h3>
- This PR is low-risk and likely safe to merge.
- The functional change is narrowly scoped to a new try/catch around optional cleanup logic and only affects logging/continuation behavior in an error path. The only concrete issue spotted is a changelog reference mismatch (issue vs PR number).
- CHANGELOG.md (verify correct PR/issue references)
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#17743: fix(agents): disable orphaned user message deletion that causes ses...
by clawrl3000 · 2026-02-16
81.1%
#3362: fix: auto-repair and retry on orphan tool_result errors
by samhotchkiss · 2026-01-28
80.9%
#7760: fix(agents): resolve message ordering conflict during tool execution
by aryan877 · 2026-02-03
80.1%
#9232: Fix: Add automatic retry for network errors in message runs
by vishaltandale00 · 2026-02-05
79.6%
#4009: fix(agent): sanitize messages after orphan user repair
by drag88 · 2026-01-29
79.4%
#6143: fix(agents): handle AbortError from activeSession.abort() on timeout
by Glucksberg · 2026-02-01
79.4%
#15852: fix: pass agentId when resolving IRC session paths
by MisterGuy420 · 2026-02-14
79.4%
#4844: fix(agents): skip error/aborted assistant messages in transcript re...
by lailoo · 2026-01-30
78.5%
#13638: fix: pass delivery context to cron isolated agent subagents
by dario-github · 2026-02-10
78.1%
#9861: fix(agents): re-run tool_use/tool_result repair after limitHistoryT...
by CyberSinister · 2026-02-05
77.8%