Repository navigation
Description
Date: 2026-01-01
Severity: High
Component: macOS App / Gateway Session Management
Summary
Deleting sessions from the macOS desktop UI causes message chain corruption in remaining sessions, resulting in API errors that break the agent completely.
Error Message
LLM request rejected: messages.0.content.0: unexpected `tool_use_id` found in
`tool_result` blocks: toolu_01AE1Pc9RfhFPz4vvq2rWnWD. Each `tool_result` block
must have a corresponding `tool_use` block in the previous message.
Reproduction Steps
- Use Clawdis agent normally via Telegram/WhatsApp for several hours (including tool use)
- Open macOS desktop app → Sessions tab
- Delete one or more sessions from the UI
- Send a new message to the agent
- Result: Agent fails with the above error on every subsequent message
Root Cause Analysis
Evidence Found
-
Session files renamed with
.deleted.*suffix - showing UI attempted deletions:~/.clawdis/sessions/4e2da6a4-...jsonl.deleted.2026-01-01T23-36-59.344Z ~/.clawdis/sessions/78c10199-...jsonl.deleted.2026-01-01T23-36-55.088Z -
sessions.jsonstill referenced the main session - metadata wasn't updated -
Main session file was corrupted - contained orphaned
tool_resultwithout itstool_use:// At 19:05:59Z - tool_result exists {"type":"message","role":"toolResult","toolCallId":"toolu_01AE1Pc9RfhFPz4vvq2rWnWD",...} // But corresponding tool_use block is MISSING from previous message
-
Error occurred 4+ hours later (23:53) when API tried to serialize the corrupted history
Probable Cause
The UI deletion process appears to:
- Truncate or modify messages within a session (not just delete whole sessions)
- Break
tool_use/tool_resultmessage pairs that must stay together - Not update
sessions.jsonatomically with file operations
Anthropic API Constraint
The Anthropic Messages API requires strict message ordering:
- Every
tool_resultcontent block MUST have a correspondingtool_useblock in the immediately precedingassistantmessage - If the
tool_useis removed buttool_resultremains, the API rejects the entire request
Impact
- Agent completely broken - cannot respond to any messages
- No auto-recovery - error persists until manual intervention
- Data loss - only fix is to delete the corrupted session, losing conversation history
Workaround
Manual cleanup:
# Remove corrupted session file
rm ~/.clawdis/sessions/<session-id>.jsonl
# Edit sessions.json to remove the stale entry
# Or delete the entire main session entryOr send /new or /reset from the chat client to start fresh.
Recommended Fix
Option 1: Atomic Session Deletion
When deleting a session from UI:
- Delete the
.jsonlfile - Remove entry from
sessions.json - Do both atomically (or at least ensure consistency)
Option 2: Validate Message Chain on Load
Before sending to API, validate that:
- Every
tool_resulthas a matchingtool_usein the previous message - If orphaned, either:
- Remove the orphaned
tool_result, OR - Truncate history to before the corruption point
- Remove the orphaned
Option 3: Safe Compaction
If session compaction/truncation is intentional:
- Never split
tool_use/tool_resultpairs - Treat
assistantmessage withtool_use+ followingtool_resultas atomic unit
Files to Investigate
- Session deletion handler in macOS app (Swift)
- Gateway session management:
~/.clawdis-src/src/gateway/ - Message serialization for Anthropic API
sessions.jsonpersistence logic
Session Files Location
~/.clawdis/sessions/
├── sessions.json # Session metadata
├── <session-id>.jsonl # Conversation transcripts
└── *.jsonl.deleted.* # "Deleted" sessions (renamed, not removed)
Related
- Anthropic API docs on tool use message format
- Claude Code session compaction logic (for reference implementation)
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
steipete commentedon Jan 3, 2026
Automated Analysis (Parallel Codex Verification)
Status: 🔍 FLOW MAPPED
Findings
Session deletion flow traced:
macOS UI path:
apps/macos/Sources/Clawdis/MenuSessionsInjector.swiftSessionActions.deleteSessioninapps/macos/Sources/Clawdis/SessionActions.swiftGateway handlers:
src/gateway/server-methods.ts—sessions.deleteRPCsrc/gateway/server-bridge.ts— bridge layersrc/gateway/session-utils.ts—loadSessionEntryutilityValidation:
validateSessionsDeleteParamsinsrc/gateway/protocol/index.ts:185src/gateway/protocol/schema.ts:317Need More Info
The analysis couldn't reproduce the corruption without:
session.storepathCould you provide error logs or specific steps that trigger the corruption?
Analysis performed by 🦞 Clawd via parallel Codex tmux sessions
sebslight commentedon Jan 13, 2026
Stale
Merge pull request #79 from dgarson/fix/compaction-filtering-messages
fix(win-clean): simplify uninstaller prompt; remove Chinese UI (openc…