Repository navigation
fix(bots): index journal submissions once per attribution read (STA-18558) - #49548
Conversation
Who a bot turn acts for, who asked a request turn, and whether the running turn may leave its thread each searched every journal submission once per message the turn took, so a turn with many steered messages cost U*J. They now share one index of the read's sends, built on first use in a single pass in submission order, which keeps the earliest send naming an item by client key or provider id exactly as the search found it. A turn a bot request opened also checked its messages for a paired device's stamp twice; the turn read's result is passed in instead, and a device-stamped message skips the send lookup.
…A-18558) A request turn's steered device message now carries a person's send, so only its device stamp keeps it from asking as the owner. The send index tests also cover a lookup by client key alone, an earlier client key over a later provider id, and an older host's unnamed sender.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
ELI5
When Orca checked who typed the messages in a bot's turn, it scanned the whole list of journal submissions from the start for every single message, and checked a bot-request turn for paired-device stamps twice. It now indexes the read's submissions once on first use and reuses the turn read's paired-device result.
What Changed
src/main/bots/bot-typed-in-orca.ts): AddedpersonSendsOf(submissions), which indexes each submission once in array order (client key viaagentJournalSubmissionKey, then stringproviderItemId, insert-if-absent) on the first lookup and drops with the read.typedInOrcaByPersonandtypedInOrcaByOwnernow query this index instead of runningsubmissions.find(...)per message.src/main/bots/bot-request-asker.ts,src/main/bots/bot-request-askers-of.ts,src/main/bots/bot-leave-thread-authority.ts):readBotActingTurnWithinandleaveThreadRefusalbuildpersonSendsOf(submissions)once per read and pass it through.src/main/bots/bot-request-askers-of.ts,src/main/bots/bot-request-asker.ts):requestTurnAskedBytakes thetypedByAnotherboolean already computed byreadBotActingTurnWithininstead of runningwindow.some(typedOnAnotherDevice)a second time, and skips the per-message device check whentypedByAnotherisfalse(checking the stamp before the send lookup whentrue).Why
Follow-up to #48774. With
Ueligible user messages in a turn andJjournal submissions in the read, runningsubmissions.find(...)per message tookO(U * J)comparisons (re-encodingagentJournalSubmissionKeyon each comparison), and bot-request turns ran twowindow.some(typedOnAnotherDevice)passes when no paired-device stamp was present.The single-pass insert-if-absent map preserves
Array.prototype.findfirst-match precedence (including cross-field collisions between an earlierproviderItemIdand a later client key, empty-stringproviderItemId, queued sends, legacyorigin: 'client'sends, and all fail-closed provenance rules from #48774). Building the index lazily avoids allocating or scanningsubmissionson turns that never reach a send lookup.Linked Issue
Fixes STA-18558. Follow-up to #48774.
Out-of-scope follow-up tracked separately: STA-18594 (linear scans in
readReplyEvidence).Visual Proof
N/A: internal main-process attribution performance fix; no UI or user-facing behavior change.
Testing
I manually tested these changes locally
Automated tests added/updated, or explained why not below
Every added test fails without this change and avoids the test anti-patterns
src/main/bots/bot-attribution.performance.test.ts: perf contract for STA-18558 acrossreadBotActingTurn(request turn and owner turn) andleaveThreadRefusal(U=64, J=64), plus request-turn paired-device stamp reads. All 4 cases fail with the 4 production files reverted (4096 -> 128,2144 -> 128,2144 -> 128clientMessageIdreads;189 -> 126typedByreads).src/main/bots/bot-typed-in-orca.test.ts: pins first-match cross-field precedence (both directions, denied and queued earlier rows), positive client-key lookup, legacyorigin: 'client'fallback, and empty-stringproviderItemIdmatching.src/main/bots/bot-request-asker.test.ts: gives the steered paired-device message a matching person send so the device veto inrequestTurnAskedByis directly exercised.Existing attribution, leave-thread, and connector turn-for test suites,
pnpm tc:node,oxfmt,oxlint, changed-code quality, and ratchet checks pass locally; hosted CI runs the full suite.Performance
Eliminates
O(U * J)submission rescans and the duplicateO(W)paired-device pass in bot attribution:requestTurnAskedBy,typedByOwner, andleaveThreadRefusaldrop fromO(U * J)comparisons (with repeated key encoding) to at most one lazyO(J)map build plusO(U)Map.getlookups per read (transientMapof at most2Jentries, discarded when the read returns).O(0).requestTurnAskedByremoves the secondwindow.some(typedOnAnotherDevice)scan and skips per-message device checks whentypedByAnotherisfalse.O(J)index build instead of short-circuitingfind, which is an intentional trade-off to bound worst-case attribution linearly. Separate non-attribution scans innative-chat-turn-membership.tsandexternal-conversation-reply-evidence.ts(STA-18594) are unchanged.Risk
Level: Low. Pure algorithmic equivalence refactor on the main-process bot attribution path; all #48774 fail-closed and paired-device invariants are preserved and verified by regression tests. Back out with a plain revert.
Agent skill upstream boundary