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

Implementation:ARCANGEL0 EVA finish interrupted tools

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

Overview

Concrete tool for repairing orphaned tool calls in conversation history by injecting synthetic error-result blocks, provided by the Mitsu agent runtime.

Description

The finish_interrupted_tools function scans conversation history for tool calls (OpenAI tool_calls or Anthropic tool_use blocks) that have no matching tool result. For each orphaned call, it injects a synthetic error result with a configurable message (default: "Interrupted by the operator..."). This allows the conversation to be replayed to the provider without protocol violations.

The function handles both protocols:

  • OpenAI: Injects a new `{"role": "tool", "tool_call_id": ..., "content": result}` message.
  • Anthropic: Injects a `{"type": "tool_result", "tool_use_id": ..., "content": result, "is_error": True}` block into the preceding user message (or creates a new user message if needed).

It is called at the start of each turn (to repair prior interruptions) and in a finally block (to repair the current turn if interrupted).

Usage

  • Before sending history to a provider: Call to ensure no orphaned tool calls remain from a prior interrupted turn.
  • After an interrupted turn: Call in a finally block to clean up any in-flight tool calls.
  • Session resume: Call when loading a persisted session to repair any interrupted state.

Code Reference

Source Location

  • Repository: Mitsu
  • File: agents/runtime.py

Signature

def finish_interrupted_tools(
    history: list[dict[str, Any]],
    result: str = "Interrupted by the operator. This action has no completed result; do not retry without their direction.",
) -> None:

Import

from mitsu.agents.runtime import finish_interrupted_tools

I/O Contract

Inputs

Name Type Required Description
history list[dict[str, Any]] Yes Conversation history (mutated in place)
result str No Synthetic error message for orphaned tool calls

Outputs

Name Type Description
history (mutated) list[dict] History with synthetic tool results injected for all orphaned calls

Usage Examples

Repair Before Provider Request

from mitsu.agents.runtime import finish_interrupted_tools

# History from a prior interrupted turn may have orphaned tool calls
finish_interrupted_tools(history)

# Now safe to send to the provider
response = request_agent_turn(profile, history, tools)

Repair After Interruption

from mitsu.agents.runtime import finish_interrupted_tools, run_agent_turn

try:
    async for event in run_agent_turn(profile, prompt=..., ...):
        process(event)
finally:
    # Clean up any in-flight tool calls if the turn was interrupted
    finish_interrupted_tools(history)
    persist_history(history)

Related Pages

Implements Principle

Requires Environment

Page Connections

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