Python
Merge pull request #350 from rkfshakti/fix/repair-tool-pairing-on-load-331
Rohit Prasad9e145d9ceb62
diff --cc coworker/conversations.pyindex ee58ae8c,a926f36d..300ca9cc--- a/coworker/conversations.py+++ b/coworker/conversations.py@@@ -136,22 -114,120 +136,130 @@@ class ConversationStorepath = self._file(sid)if not path.exists():return None- return [- json.loads(line)- for line in path.read_text(encoding="utf-8").splitlines()- if line.strip()- ]+ # Tolerate a corrupt/truncated line rather than failing the whole load. An append+ # interrupted mid-write (crash, disk full) leaves one malformed trailing line; a+ # bare `json.loads` in a comprehension would raise JSONDecodeError and make load()+ # throw every time thereafter — bricking that session on every surface that opens+ # it. Skip the bad line(s) and keep the recoverable history. (Every other JSON read+ # in this module is already tolerant; this one was the outlier.)+ messages: list[dict] = []+ for line in path.read_text(encoding="utf-8").splitlines():+ if not line.strip():+ continue+ try:+ messages.append(json.loads(line))+ except json.JSONDecodeError:+ continue+ return messages+ # -- tool-call/result pairing repair ---------------------------------------+ @staticmethod+ def _repair_tool_pairing(messages: list[dict]) -> list[dict]:+ """Reorder messages so every tool result immediately follows its call.++ Append-only persistence means an interrupted turn can leave a user+ message between an assistant ``tool_calls`` block and the matching+ ``tool`` result. Providers reject this ordering (Anthropic 400/2013,+ OpenAI "tool_call_ids did not have response messages"), making the+ session permanently unrecoverable.++ This pass:+ * 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 — 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:+ return messages++ # Collect tool_call ids from assistant messages.+ pending_calls: dict[str, int] = {} # call_id → index of the assistant msg+ for i, m in enumerate(messages):+ if m.get("role") == "assistant" and m.get("tool_calls"):+ for tc in m["tool_calls"]:+ call_id = tc.get("id")+ if call_id:+ pending_calls[call_id] = i++ if not pending_calls:+ return messages # no tool calls at all++ # Find tool results and where they sit relative to their calls.+ # call_id → index of the tool result message (if found)+ found_results: dict[str, int] = {}+ for i, m in enumerate(messages):+ if m.get("role") == "tool":+ call_id = m.get("tool_call_id")+ if call_id and call_id in pending_calls:+ # Only keep the first result for each call.+ 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. 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:+ needs_repair = True # result exists but not immediately after+ else:+ needs_repair = True # no result at all+ if not needs_repair:+ 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+ # their original position or synthesised if missing).+ consumed_result_indices: set[int] = set()+ repaired: list[dict] = []++ for i, m in enumerate(messages):+ if m.get("role") == "assistant" and m.get("tool_calls"):+ repaired.append(m)+ # Emit results for each tool call in this block, in order.+ for tc in m["tool_calls"]:+ call_id = tc.get("id")+ if not call_id:+ continue+ if call_id in found_results:+ result_idx = found_results[call_id]+ if result_idx not in consumed_result_indices:+ repaired.append(messages[result_idx])+ consumed_result_indices.add(result_idx)+ 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,+ "content": '{"error": "tool result was lost during an interrupted turn"}',+ })+ elif i in consumed_result_indices:+ continue # already moved this tool result up+ else:+ repaired.append(m)++ return repaired+def _count(self, sid: str) -> int:path = self._file(sid)if not path.exists():