#15127: fix: clean up transcript paths on session corruption with proper error handling
size: XS
Cluster:
Error Handling in Agent Tools
## Summary
Fixes transcript file cleanup during **Gemini session corruption recovery** to properly handle both thread-scoped and non-thread-scoped transcript paths, with improved error handling.
## Changes
- **Add agentId parameter** to `resolveSessionTranscriptPath` calls (was missing, causing lookups in wrong directory)
- **Conditionally clean up thread-scoped transcripts** - only when `MessageThreadId` is present and non-empty, avoiding wasteful duplicate deletion attempts
- **Improve error handling** - log non-ENOENT errors instead of silently suppressing all failures
- **Switch to async** - use `fs.promises.unlink()` instead of `fs.unlinkSync()`
## Context
When a **Gemini** session becomes corrupted (function call ordering error: `"function call turn comes immediately after"`), the auto-recovery logic resets the session and attempts to delete the transcript file. Previously, it:
1. Only tried the non-thread-scoped path
2. Didn't pass the agentId parameter (potentially wrong directory)
3. Always tried both paths even when thread ID was absent (duplicate deletion)
4. Silently swallowed all errors including permission/IO errors
This fix ensures proper cleanup of all transcript variants while avoiding unnecessary operations and providing better error visibility.
## Scope
**This PR specifically fixes the Gemini corruption handler** (triggered by `isSessionCorruption`), but the codebase has other corruption handlers that likely have similar bugs:
- `isRoleOrderingError` - Generic role alternation errors (Anthropic, OpenAI, etc.)
- `isCompactionFailure` - Compaction errors (any provider)
The improvements in this PR (agentId, thread-scoped cleanup, error logging) are reusable patterns that could benefit those other handlers in future work.
## Testing
- ✅ Build passed
- ✅ TypeScript type-checking passed
- ✅ Linting passed (0 errors)
- ✅ Formatting passed
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This change updates the Gemini session-corruption recovery path in `src/auto-reply/reply/agent-runner-execution.ts` to delete the correct transcript file(s) when resetting a corrupted session.
Key behavior changes:
- Transcript deletion now resolves paths with the **agentId** derived from the session key, so cleanup targets the right `~/.openclaw/agents/<agentId>/sessions/*.jsonl` directory.
- Cleanup attempts both the non-thread-scoped transcript and, when a non-empty `MessageThreadId` is present, the thread-scoped `-topic-...` transcript variant.
- Deletion is now async (`fs.promises.unlink`) and only suppresses `ENOENT`, logging other unlink failures.
This fits into the existing auto-recovery block guarded by `isSessionCorruption` (Gemini-specific), before the session entry is removed from the in-memory snapshot and the on-disk session store via `updateSessionStore`.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- The change is localized to Gemini session-corruption recovery and primarily improves correctness of transcript path resolution (agent-scoped + optional thread-scoped) while switching to async unlink with selective error logging. No remaining unverified or reproducible functional issues were found in the modified logic.
- No files require special attention
<sub>Last reviewed commit: deb2a48</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#14314: fix(agent-runner): auto-recover from Gemini INVALID_ARGUMENT errors
by thebtf · 2026-02-11
81.7%
#15050: fix: transcript corruption resilience — strip aborted tool_use bloc...
by yashchitneni · 2026-02-12
81.4%
#14576: Fix/memory loss bugs
by ENCHIGO · 2026-02-12
80.9%
#15852: fix: pass agentId when resolving IRC session paths
by MisterGuy420 · 2026-02-14
78.9%
#20336: fix(sessions): resolve transcriptPath using agentId when storePath ...
by Limitless2023 · 2026-02-18
78.7%
#16061: fix(sessions): tolerate invalid sessionFile metadata
by haoyifan · 2026-02-14
77.8%
#15792: fix: pass agentId to resolveSessionFilePath in additional call sites
by MisterGuy420 · 2026-02-13
77.7%
#8270: fix: support snake_case 'tool_use' in transcript repair (#8264)
by heliosarchitect · 2026-02-03
76.2%
#21828: fix: acquire session write lock in delivery mirror and gateway chat...
by inkolin · 2026-02-20
76.2%
#20188: fix: Update sessionFile path when rolling to new session in cron jobs
by jriff · 2026-02-18
76.1%