You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(agents): O(n^2) scan-ahead in stripDanglingAnthropicToolUses causes latency spike on long sessions #61815
stripDanglingAnthropicToolUses performs an O(n) forward scan for each assistant message to find matching tool_result blocks. For sessions with many messages, this results in O(n^2) total work, causing noticeable latency spikes during session sanitization.
Root cause
The current implementation iterates forward from each tool_use block through all remaining messages to check for a matching tool_result. With no index or set-based lookup, this is quadratic in the number of messages.
Proposed fix
Build a Set<string> of all tool_result IDs in a single O(n) pass first, then check membership in O(1) for each tool_use block. This reduces the overall complexity from O(n^2) to O(n).
Impact
Sessions with 200+ messages see 100-500ms added latency on each sanitization pass
Sanitization runs on every LLM call, so this compounds quickly in long conversations
Especially impactful for agents with frequent tool calls (code execution, file operations)
Closing this as not reproducible on current main after Codex review.
Current main does not match the report's premise: stripDanglingAnthropicToolUses only scans forward until the next assistant turn, and the same bounded behavior is encoded in regression tests and shipped unchanged in v2026.4.23.
What I checked:
Forward scans stop at the next assistant turn: Both helper scans used by stripDanglingAnthropicToolUses break on the first later assistant message, so they inspect only the contiguous span after the current assistant turn rather than the full remaining session. (src/agents/pi-embedded-helpers/turns.ts:126, e6d04682d33c)
Caller uses the bounded scans directly:stripDanglingAnthropicToolUses calls those bounded helpers once per assistant message and documents the contract as matching tool results only 'before the next assistant turn.'. (src/agents/pi-embedded-helpers/turns.ts:167, e6d04682d33c)
Regression tests encode the same bounded behavior: Tests explicitly verify matching standalone tool results 'before the next assistant turn' and across intermediate non-assistant messages, which aligns with a bounded per-turn scan instead of an all-history scan. (src/agents/pi-embedded-helpers.validate-turns.test.ts:498, e6d04682d33c)
Activity
perf(agents): fix O(n²) scan in stripDanglingAnthropicToolUses
steipete commentedon Apr 25, 2026
Closing this as not reproducible on current
mainafter Codex review.Current
maindoes not match the report's premise:stripDanglingAnthropicToolUsesonly scans forward until the next assistant turn, and the same bounded behavior is encoded in regression tests and shipped unchanged inv2026.4.23.What I checked:
stripDanglingAnthropicToolUsesbreak on the first later assistant message, so they inspect only the contiguous span after the current assistant turn rather than the full remaining session. (src/agents/pi-embedded-helpers/turns.ts:126, e6d04682d33c)stripDanglingAnthropicToolUsescalls those bounded helpers once per assistant message and documents the contract as matching tool results only 'before the next assistant turn.'. (src/agents/pi-embedded-helpers/turns.ts:167, e6d04682d33c)src/agents/pi-embedded-helpers.validate-turns.test.ts:498, e6d04682d33c)git diffbetweenv2026.4.23(a9797214338ba31c52c796adbb75afb16e0684a9) and currentmainshows no changes insrc/agents/pi-embedded-helpers/turns.tsor its regression test, so the same bounded implementation is already shipped. (src/agents/pi-embedded-helpers/turns.ts:126, a9797214338b)Review notes: reviewed against e6d04682d33c.