Skip to content

perf(agents): O(n^2) scan-ahead in stripDanglingAnthropicToolUses causes latency spike on long sessions #61815

Closed as not planned

Description

@100yenadmin

Description

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)

Related

Activity

  1. added a commit that references this issue on Apr 6, 2026
    d32f93f
  2. steipete commented on Apr 25, 2026

    @steipete
    Contributor

    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:

    Review notes: reviewed against e6d04682d33c.

  3. locked as resolved and limited conversation to collaborators on Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions