perf(ai): O(1) exchange_with_id via exchange_id_index#13111
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
This comment was marked as resolved.
This comment was marked as resolved.
|
/oz-review |
This comment was marked as resolved.
This comment was marked as resolved.
|
/oz-review |
|
I'm re-reviewing this pull request in response to a review request. You can view the conversation on Warp. I completed the review and no human review was requested for this pull request. Comment Powered by Oz |
There was a problem hiding this comment.
Overview
This PR adds an exchange_id_index to TaskStore so AIConversation::exchange_with_id can resolve exchanges by ID through a hash map instead of scanning every task. The index is initialized from restored tasks and rebuilt on the structural mutation paths shown in the diff, including inserts, removals, exchange appends/removals, truncation via modify_task, and explicit exchange ID reassignment after fork/hydration flows.
Concerns
- No blocking correctness, security, or spec-drift concerns found in the changed lines.
spec_context.mdcontains no approved or repository spec context for this PR.
Verdict
Found: 0 critical, 0 important, 0 suggestions
Approve
Comment /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).
Powered by Oz
90448a3 to
75061d8
Compare

Description
Part of a stack of performance fixes for #9799: opening a large Oz agent conversation (3000+ exchanges) caused a beachball / multi-second load due to several O(n²) hotspots in the restore path.
This PR:
AIConversation::exchange_with_idpreviously performed a linear scan through every exchange on every call. During conversation restoration this function is called once per exchange to look up command-block metadata, making the restore path O(n²) overall.Adds
exchange_id_index: HashMap<AIAgentExchangeId, usize>toTaskStore. The index is rebuilt on all structural mutations (append, insert, remove, exchange removal).exchange_with_idand the newexchange_by_idnow return in O(1).Also fixes
remove(): previously it retained entries inlinearized_refsbut left stale entries inexchange_id_indexfor the removed task; now it does a full index rebuild.Linked Issue
Fixes #9799
ready-to-specorready-to-implement.Testing
Manually loaded a conversation with 3014 AI exchanges and confirmed the restore time was significantly reduced.
./script/runAgent Mode