Skip to content

Browser timeout leaves orphaned tool_use blocks causing API errors on session reload #7930

@kdsz001

Description

@kdsz001

Bug Description

When a browser operation times out, the session is saved with an orphaned tool_use block (no matching tool_result). On next API call, this causes:

unexpected tool_use_id in tool_result blocks

Root Cause

repairToolUseResultPairing() in session-transcript-repair.ts exists but is only called during context sanitization before API calls, not when sessions are loaded from disk.

Steps to Reproduce

  1. Start a session
  2. Trigger a browser operation that times out (e.g., screenshot with slow page)
  3. Session saves with orphaned tool_use block
  4. Next API call fails with "unexpected tool_use_id" error

Suggested Fix

Call repairToolUseResultPairing() when loading sessions from disk in session-utils.fs.js:

// In readSessionMessages(), before return:
import { repairToolUseResultPairing } from "../agents/session-transcript-repair.js";

// ... existing code ...

const repaired = repairToolUseResultPairing(messages);
return repaired.messages;

Affected Files

  • src/gateway/session-utils.fs.ts - session loading (needs fix)
  • src/agents/session-transcript-repair.ts - repair function (already exists)
  • src/agents/pi-extensions/transcript-sanitize.ts - only called on context build

Workaround

Manually patch dist/gateway/session-utils.fs.js to call repair on load (verified working).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions