diff --git a/src/claude_agent_sdk/_internal/sessions.py b/src/claude_agent_sdk/_internal/sessions.py index 6caae8dd2..5449084ef 100644 --- a/src/claude_agent_sdk/_internal/sessions.py +++ b/src/claude_agent_sdk/_internal/sessions.py @@ -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) diff --git a/tests/test_sessions.py b/tests/test_sessions.py index a5bb63cfe..c0659421a 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -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}, { @@ -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.