Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/claude_agent_sdk/_internal/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,10 @@ def _pick_best(candidates: list[_TranscriptEntry]) -> _TranscriptEntry:
chain_cur = by_uuid.get(parent) if parent else None

chain.reverse()
if len(chain_seen) == len(by_uuid):
# Every indexed entry is already on the chain, so there is no
# parallel call or result to recover.
return chain
return _recover_orphaned_parallel_tool_results(by_uuid, chain, chain_seen)


Expand Down
12 changes: 12 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,14 @@ def test_recovered_calls_are_in_timestamp_order(self):

def test_sequential_tool_calls_are_unchanged(self):
"""A response whose calls ran one at a time has nothing to recover."""

class CountedBlocks(list):
scans = 0

def __iter__(self):
self.scans += 1
return super().__iter__()

entries = [
{"type": "user", "uuid": "prompt", "parentUuid": None},
{
Expand Down Expand Up @@ -1360,8 +1368,12 @@ def test_sequential_tool_calls_are_unchanged(self):
},
},
]
# A complete parent chain can be replayed without inspecting the payloads.
blocks = CountedBlocks(entries[1]["message"]["content"])
entries[1]["message"]["content"] = blocks
result = _build_conversation_chain(entries)
assert result == entries
assert blocks.scans == 0

def test_abandoned_branch_is_not_recovered(self):
"""A retried turn's old response stays off the chain.
Expand Down