Skip to content

Bug: Session Deletion from UI Causes Message Corruption #79

Closed

Description

@daveonkels

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

  1. Use Clawdis agent normally via Telegram/WhatsApp for several hours (including tool use)
  2. Open macOS desktop app → Sessions tab
  3. Delete one or more sessions from the UI
  4. Send a new message to the agent
  5. Result: Agent fails with the above error on every subsequent message

Root Cause Analysis

Evidence Found

  1. 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
    
  2. sessions.json still referenced the main session - metadata wasn't updated

  3. Main session file was corrupted - contained orphaned tool_result without its tool_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
  4. 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_result message pairs that must stay together
  • Not update sessions.json atomically with file operations

Anthropic API Constraint

The Anthropic Messages API requires strict message ordering:

  • Every tool_result content block MUST have a corresponding tool_use block in the immediately preceding assistant message
  • If the tool_use is removed but tool_result remains, 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 entry

Or send /new or /reset from the chat client to start fresh.

Recommended Fix

Option 1: Atomic Session Deletion

When deleting a session from UI:

  1. Delete the .jsonl file
  2. Remove entry from sessions.json
  3. Do both atomically (or at least ensure consistency)

Option 2: Validate Message Chain on Load

Before sending to API, validate that:

  • Every tool_result has a matching tool_use in the previous message
  • If orphaned, either:
    • Remove the orphaned tool_result, OR
    • Truncate history to before the corruption point

Option 3: Safe Compaction

If session compaction/truncation is intentional:

  • Never split tool_use/tool_result pairs
  • Treat assistant message with tool_use + following tool_result as 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.json persistence 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)

Activity

  1. steipete commented on Jan 3, 2026

    @steipete
    Contributor

    Automated Analysis (Parallel Codex Verification)

    Status: 🔍 FLOW MAPPED

    Findings

    Session deletion flow traced:

    1. macOS UI path:

      • apps/macos/Sources/Clawdis/MenuSessionsInjector.swift
      • → SessionActions.deleteSession in apps/macos/Sources/Clawdis/SessionActions.swift
    2. Gateway handlers:

      • src/gateway/server-methods.ts — sessions.delete RPC
      • src/gateway/server-bridge.ts — bridge layer
      • src/gateway/session-utils.ts — loadSessionEntry utility
    3. Validation:

      • validateSessionsDeleteParams in src/gateway/protocol/index.ts:185
      • Schema at src/gateway/protocol/schema.ts:317

    Need More Info

    The analysis couldn't reproduce the corruption without:

    • Specific repro steps
    • Whether it's macOS menu vs CLI/RPC deletion
    • Any mention of legacy group keys or custom session.store path

    Could you provide error logs or specific steps that trigger the corruption?


    Analysis performed by 🦞 Clawd via parallel Codex tmux sessions

  2. added
    bug
    on Jan 3, 2026
  3. sebslight commented on Jan 13, 2026

    @sebslight
    Contributor

    Stale

  4. added a commit that references this issue on Feb 4, 2026
  5. locked as resolved and limited conversation to collaborators on Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions