Skip to content

fix(staged): pre-compute hasUserAfter to fix O(N²) UI freeze in SessionModal - #617

Merged
matt2e merged 1 commit into
mainfrom
fix/session-modal-findindex-perf
Apr 14, 2026
Merged

fix(staged): pre-compute hasUserAfter to fix O(N²) UI freeze in SessionModal#617
matt2e merged 1 commit into
mainfrom
fix/session-modal-findindex-perf

Conversation

@matt2e

@matt2e matt2e commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Activity Monitor sampling identified Array.indexOf as the top hotspot (121/2179 samples) in the Staged web app, causing UI freezes on long sessions.
  • The root cause was an inline grouped.findIndex(...) call inside an {#each grouped} template loop in SessionModal.svelte, resulting in O(N²) scanning.
  • Added a $derived boolean array (hasUserAfter) that pre-computes in O(N) via a single reverse pass whether a user message exists after each group index, reducing per-group lookup to O(1).

Test plan

  • Open a session modal with a long session (many tool calls / hundreds of messages) and verify it renders without freezing or noticeable lag.
  • Verify tool call cards show present tense for live running tools (e.g. "Reading file...").
  • Verify tool call cards show past tense for completed tools and for tools that appear before a user follow-up message (e.g. "Read file").
  • Verify tense behavior is the same when sending is true (input submitted but not yet responded to).

🤖 Generated with Claude Code

… SessionModal

The template had an inline `grouped.findIndex(...)` call inside an
`{#each grouped}` loop, causing O(N²) scanning for long sessions and
freezing the UI. Replace it with a `$derived` boolean array computed
once in O(N) via a reverse pass, giving O(1) lookup per tool group.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@matt2e
matt2e requested review from baxen and wesbillman as code owners April 14, 2026 04:09
@matt2e
matt2e merged commit a4cefd7 into main Apr 14, 2026
5 checks passed
@matt2e
matt2e deleted the fix/session-modal-findindex-perf branch April 14, 2026 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant