Skip to content
Python

fix: don't inject placeholder for trailing pending tool calls

Shakti Prasad Mohapatra9bad0fdd8e3d
diff --git a/coworker/conversations.py b/coworker/conversations.py
index a3f00352..a926f36d 100644
--- a/coworker/conversations.py
+++ b/coworker/conversations.py
@@ -135,7 +135,11 @@ class ConversationStore:
* Moves a real ``tool`` result found later in the thread to sit right
after its call.
* Synthesises a placeholder result for a call with no matching tool
- message, so the replay is well-formed.
+ message — but **only** when the thread has moved past the call
+ (i.e. there are messages after the assistant block). A trailing
+ assistant ``tool_calls`` with no result is a pending/interrupted
+ call that the engine will resume; injecting a placeholder there
+ would break durable resume.
* Is idempotent — a well-formed thread passes through unchanged.
"""
if not messages:
@@ -164,11 +168,23 @@ class ConversationStore:
if call_id not in found_results:
found_results[call_id] = i
+ # Determine which calls are "trailing" — the assistant block is the
+ # last message in the thread (nothing after it). These are pending
+ # calls that the engine will resume; we must not inject placeholders.
+ last_msg_idx = len(messages) - 1
+ trailing_calls: set[str] = set()
+ for call_id, call_idx in pending_calls.items():
+ if call_idx == last_msg_idx:
+ trailing_calls.add(call_id)
+
# Calls that have a result already immediately following the assistant
# message are fine — no work needed. We only need to act when a result
- # is missing or out-of-order.
+ # is missing or out-of-order. Trailing calls without results are
+ # skipped (they're pending, not corrupt).
needs_repair = False
for call_id, call_idx in pending_calls.items():
+ if call_id in trailing_calls and call_id not in found_results:
+ continue # pending call — engine will resume
if call_id in found_results:
result_idx = found_results[call_id]
if result_idx != call_idx + 1:
@@ -176,7 +192,7 @@ class ConversationStore:
else:
needs_repair = True # no result at all
if not needs_repair:
- return messages # already well-formed
+ return messages # already well-formed (or only pending calls)
# Build the repaired list. We iterate through the original messages,
# and after each assistant message we emit its tool results (moved from
@@ -197,8 +213,9 @@ class ConversationStore:
if result_idx not in consumed_result_indices:
repaired.append(messages[result_idx])
consumed_result_indices.add(result_idx)
- else:
+ elif call_id not in trailing_calls:
# Synthesise a placeholder so the thread is well-formed.
+ # Skip trailing calls — they're pending, not corrupt.
repaired.append({
"role": "tool",
"tool_call_id": call_id,
diff --git a/tests/test_conversations_repair.py b/tests/test_conversations_repair.py
index 5db0e249..ce6bff3f 100644
--- a/tests/test_conversations_repair.py
+++ b/tests/test_conversations_repair.py
@@ -72,11 +72,12 @@ def test_interleaved_user_between_call_and_result():
def test_dangling_call_gets_placeholder():
- """A tool call with no matching result gets a synthesised placeholder."""
+ """A tool call with no matching result gets a synthesised placeholder — but
+ only when the thread has moved past the call (not a trailing pending call)."""
messages = [
_user("go"),
_assistant_with_calls("c1"),
- _user("next"),
+ _user("next"), # thread moved past → call is corrupt, not pending
]
repaired = ConversationStore._repair_tool_pairing(messages)
assert repaired[0]["role"] == "user"
@@ -87,6 +88,17 @@ def test_dangling_call_gets_placeholder():
assert repaired[3]["role"] == "user"
+def test_trailing_pending_call_not_repaired():
+ """A tool call as the last message is a pending/interrupted call — the
+ engine will resume it, so we must not inject a placeholder."""
+ messages = [
+ _user("go"),
+ _assistant_with_calls("c1"),
+ ]
+ repaired = ConversationStore._repair_tool_pairing(messages)
+ assert repaired == messages # unchanged — no placeholder injected
+
+
def test_multi_call_block():
"""Multiple tool calls in one assistant message each get their result in order."""
messages = [