Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:ARCANGEL0 EVA Conversation History Repair

From Leeroopedia
Knowledge Sources
Domains Agentic_Systems, Conversation_Management
Last Updated 2026-10-29 14:00 GMT

Overview

Technique for maintaining conversation protocol invariants by injecting synthetic results for orphaned tool calls when an agent turn is interrupted or fails mid-execution.

Description

In tool-augmented LLM conversations, each tool-use block in an assistant message must be paired with a corresponding tool-result block. When a turn is interrupted (operator cancellation, crash, or timeout), tool calls may remain orphaned — the assistant requested a tool call but no result was recorded. If the history is replayed to the model in this state, the provider will reject it as a protocol violation.

Conversation history repair scans the message history for orphaned tool calls and injects synthetic error-result blocks for each one. This allows the conversation to resume cleanly on the next turn without corrupting the protocol contract.

The problem solved: interrupted agent turns leave the conversation in an inconsistent state that the LLM API cannot process. Without repair, the session would be stuck — unable to send the next message.

Usage

Apply this principle when:

  • Building an agent loop that can be interrupted mid-execution.
  • Persisting conversation history between turns that may span process restarts.
  • Supporting both OpenAI (tool-call/tool message pairs) and Anthropic (tool_use/tool_result blocks) protocols.

Theoretical Basis

The conversation protocol requires a bijective pairing between tool-use and tool-result blocks:

# Abstract algorithm (NOT real implementation)
pending = {}  # call_id -> is_anthropic_protocol
for message in history:
    if message has tool_results:
        for result in results:
            pending.pop(result.tool_use_id)
    if message is tool_response:
        pending.pop(message.tool_call_id)
    if message is assistant with tool_calls:
        for call in tool_calls:
            pending[call.id] = is_anthropic(message)
    # At any point, pending contains orphaned calls
inject_synthetic_results(pending, error_message="Interrupted")

The repair is idempotent — running it on already-clean history is a no-op. It runs at the start of each turn (to repair prior interruptions) and in a finally block (to repair the current turn if interrupted).

Practical Guide

  • Run repair before sending history to the provider and after any exception or interruption.
  • Use a distinct error message for synthetic results so the model knows the tool did not actually run.
  • Handle both protocols: OpenAI uses separate tool-role messages; Anthropic embeds tool_result blocks in user messages.
  • Make the operation in-place to avoid copying large histories.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment