openclaw - ✅(Solved) Fix perf(agents): O(n^2) scan-ahead in stripDanglingAnthropicToolUses causes latency spike on long sessions [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#61815Fetched 2026-04-08 02:54:10
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Participants
Timeline (top)
cross-referenced ×2referenced ×1

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.

Fix Action

Fixed

PR fix notes

PR #49901: test(agents): regression tests for orphaned tool-use stripping in validateAnthropicTurns

Description (problem / solution / changelog)

What this fixes (plain English)

This is a test-only PR that adds regression test coverage for edge cases in validateAnthropicTurns, specifically around how stripDanglingAnthropicToolUses() handles orphaned tool calls. These tests document and lock down the existing stripping behavior to prevent future regressions.

Technical details

Scope: Test-only — no production code changes. The implementation file src/agents/pi-embedded-helpers/turns.ts is not modified.

File changed:

  • src/agents/pi-embedded-helpers.validate-turns.test.ts — 6 regression tests covering:
    • Tool call with a later (non-adjacent) tool result survives stripping
    • Tool call with no matching result is correctly stripped
    • Standalone toolResult role messages preserve matching calls
    • Scan-ahead stops at the next assistant turn boundary
    • Aborted mid-transcript tool-only turns are handled correctly
    • Legacy string assistant content passes through unchanged

Standalone value: These tests have standalone value for preventing regressions in the existing stripping logic, independent of any future scan-ahead implementation work.

Related

  • Related to #48354

Test plan

  • 33/33 tests pass
  • All 6 new regression cases validate existing behavior
  • No production code changes — zero behavioral risk

Changed files

  • src/agents/pi-embedded-helpers.validate-turns.test.ts (modified, +170/-0)
RAW_BUFFERClick to expand / collapse

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

  • PR #49901 (fix in progress for the broader stripDangling/validateAnthropicTurns area)
  • #48354, #33621, #41571 (related session corruption issues in the same code path)

extent analysis

TL;DR

Implement a Set<string> to store tool_result IDs for efficient lookup, reducing the complexity from O(n^2) to O(n).

Guidance

  • Build a Set<string> of all tool_result IDs in a single pass to enable O(1) lookup for each tool_use block.
  • Modify the stripDanglingAnthropicToolUses function to utilize this set for matching tool_result blocks, reducing the overall complexity.
  • Verify the fix by measuring latency spikes in sessions with many messages, expecting a significant reduction.
  • Consider reviewing related issues (#48354, #33621, #41571) to ensure the fix does not introduce new session corruption problems.

Example

// Pseudocode example, actual implementation may vary
Set<string> toolResultIds = new HashSet<>();
// O(n) pass to build the set
for (Message message : messages) {
    if (message instanceof ToolResult) {
        toolResultIds.add(message.getId());
    }
}
// O(n) pass with O(1) lookup for each tool_use block
for (Message message : messages) {
    if (message instanceof ToolUse) {
        if (toolResultIds.contains(message.getToolResultId())) {
            // matching tool_result found
        }
    }
}

Notes

The proposed fix assumes that tool_result IDs are unique and can be used for efficient lookup. If this is not the case, an alternative approach may be needed.

Recommendation

Apply the workaround by implementing the Set<string> for efficient lookup, as this significantly reduces the complexity and latency spikes in session sanitization.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - ✅(Solved) Fix perf(agents): O(n^2) scan-ahead in stripDanglingAnthropicToolUses causes latency spike on long sessions [1 pull requests, 1 participants]