perf(run_agent): accumulate length-continuation prefix via list+join

Replace O(n²) string concatenation of truncated_response_prefix in the
length-continuation retry loop with a list + ''.join(). Functionally
equivalent: same partial response on early return, same prepend on
final assembly. The legacy retry path is capped at 3 iterations, so
the practical wall-clock win is small, but the new idiom matches the
rest of the codebase and removes a needless repeated allocation.

Salvaged from PR #2717 (the run_conversation portion only — trajectory
refactor dropped because it silently rewrote </tool_response> to </think>).

Co-authored-by: Teknium <127238744+teknium1@users.noreply.github.com>
This commit is contained in:
InB4DevOps 2026-05-15 01:40:03 -07:00 committed by Teknium
parent b6e07417c5
commit 4f8aaf1046
2 changed files with 7 additions and 7 deletions

View file

@ -12207,7 +12207,7 @@ class AIAgent:
codex_ack_continuations = 0
length_continue_retries = 0
truncated_tool_call_retries = 0
truncated_response_prefix = ""
truncated_response_parts: List[str] = []
compression_attempts = 0
_turn_exit_reason = "unknown" # Diagnostic: why the loop ended
@ -13100,7 +13100,7 @@ class AIAgent:
interim_msg = self._build_assistant_message(assistant_message, finish_reason)
messages.append(interim_msg)
if assistant_message.content:
truncated_response_prefix += assistant_message.content
truncated_response_parts.append(assistant_message.content)
if length_continue_retries < 3:
self._vprint(
@ -13121,7 +13121,7 @@ class AIAgent:
restart_with_length_continuation = True
break
partial_response = self._strip_think_blocks(truncated_response_prefix).strip()
partial_response = self._strip_think_blocks("".join(truncated_response_parts)).strip()
self._cleanup_task_resources(effective_task_id)
self._persist_session(messages, conversation_history)
return {
@ -15325,9 +15325,9 @@ class AIAgent:
codex_ack_continuations = 0
if truncated_response_prefix:
final_response = truncated_response_prefix + final_response
truncated_response_prefix = ""
if truncated_response_parts:
final_response = "".join(truncated_response_parts) + final_response
truncated_response_parts = []
length_continue_retries = 0
final_response = self._strip_think_blocks(final_response).strip()

View file

@ -59,7 +59,7 @@ class TestTruncatedAnthropicResponseNormalization:
nr = get_transport("anthropic_messages").normalize_response(response)
# The continuation block checks these two attributes:
# assistant_message.content → appended to truncated_response_prefix
# assistant_message.content → appended to truncated_response_parts
# assistant_message.tool_calls → guards the text-retry branch
assert nr.content is not None
assert "partial response" in nr.content