Skip to content

fix(bots): index journal submissions once per attribution read (STA-18558) - #49548

Merged
AmethystLiang merged 2 commits into
mainfrom
AmethystLiang/fix-sta-18558-attribution
Nov 26, 2026
Merged

AmethystLiang merged 2 commits into
mainfrom
AmethystLiang/fix-sta-18558-attribution

Conversation

@AmethystLiang

@AmethystLiang AmethystLiang commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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.

Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​224 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​223
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​84 $\color{#cf222e}{\Huge{\mathbf{−}}}$​37 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​47

What Changed

  • Single-pass lazy send index (src/main/bots/bot-typed-in-orca.ts): Added personSendsOf(submissions), which indexes each submission once in array order (client key via agentJournalSubmissionKey, then string providerItemId, insert-if-absent) on the first lookup and drops with the read. typedInOrcaByPerson and typedInOrcaByOwner now query this index instead of running submissions.find(...) per message.
  • Shared index across attribution callers (src/main/bots/bot-request-asker.ts, src/main/bots/bot-request-askers-of.ts, src/main/bots/bot-leave-thread-authority.ts): readBotActingTurnWithin and leaveThreadRefusal build personSendsOf(submissions) once per read and pass it through.
  • Single paired-device pass (src/main/bots/bot-request-askers-of.ts, src/main/bots/bot-request-asker.ts): requestTurnAskedBy takes the typedByAnother boolean already computed by readBotActingTurnWithin instead of running window.some(typedOnAnotherDevice) a second time, and skips the per-message device check when typedByAnother is false (checking the stamp before the send lookup when true).

Why

Follow-up to #48774. With U eligible user messages in a turn and J journal submissions in the read, running submissions.find(...) per message took O(U * J) comparisons (re-encoding agentJournalSubmissionKey on each comparison), and bot-request turns ran two window.some(typedOnAnotherDevice) passes when no paired-device stamp was present.

The single-pass insert-if-absent map preserves Array.prototype.find first-match precedence (including cross-field collisions between an earlier providerItemId and a later client key, empty-string providerItemId, queued sends, legacy origin: 'client' sends, and all fail-closed provenance rules from #48774). Building the index lazily avoids allocating or scanning submissions on 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 across readBotActingTurn (request turn and owner turn) and leaveThreadRefusal (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 -> 128 clientMessageId reads; 189 -> 126 typedBy reads).

  • 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, legacy origin: 'client' fallback, and empty-string providerItemId matching.

  • src/main/bots/bot-request-asker.test.ts: gives the steered paired-device message a matching person send so the device veto in requestTurnAskedBy is 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 duplicate O(W) paired-device pass in bot attribution:

  • requestTurnAskedBy, typedByOwner, and leaveThreadRefusal drop from O(U * J) comparisons (with repeated key encoding) to at most one lazy O(J) map build plus O(U) Map.get lookups per read (transient Map of at most 2J entries, discarded when the read returns).
  • Turns that short-circuit before any send lookup pay O(0).
  • requestTurnAskedBy removes the second window.some(typedOnAnotherDevice) scan and skips per-message device checks when typedByAnother is false.
  • Note: an early single-message hit on a large submission list can pay one O(J) index build instead of short-circuiting find, which is an intentional trade-off to bound worst-case attribution linearly. Separate non-attribution scans in native-chat-turn-membership.ts and external-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

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

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.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The changes add a lazily built lookup that classifies person sends by client key and provider item ID. Bot-turn attribution and leave-thread checks now use this lookup. Request-turn attribution also receives an explicit typedByAnother flag. New tests cover matching behavior and assert read counts for attribution checks.

Merge Risk: Minimal · up to d5f95

This change builds one submission lookup per attribution read instead of rescanning the journal for each message. Attribution behavior is intended to stay the same, and no defects were found. Merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the bot attribution code now builds the journal submission index once per read, instead of rescanning submissions for each message. It follows the repo's conventional-…
Description check Passed The description follows the repository template and covers every required section: ELI5, What Changed, Why, Linked Issue, Visual Proof (N/A with reason), Testing, Performance, Risk with level, and the…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the bot attribution code now builds the journal submission index once per read, instead of rescanning submissions for each message. It follows the repo's conventional-commit style and includes the ticket reference. It does not mention the paired-device pass change, but that is a secondary part of the change.

Full details: Description check

Explanation

The description follows the repository template and covers every required section: ELI5, What Changed, Why, Linked Issue, Visual Proof (N/A with reason), Testing, Performance, Risk with level, and the upstream boundary checkbox. The Performance section has a concrete explanation with complexity analysis, and the Risk section explains the revert path. Two template items are weaker. The Linked Issue section says 'Fixes STA-18558' and references #48774 but gives no GitHub issue link. The AI Disclosure section is left blank, which the template says to skip only for internal StablyAI members, and the author's membership is not stated. The Testing checklist also has the manual-testing box unchecked, which is acceptable since automated tests are checked.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5c942b2a-692d-4781-ab68-69bc0f5f75b8
Commits

Reviewing files that changed from the base of the PR and between d1f22fc and d5f9504.

Files selected for processing: 8

  • src/main/bots/bot-attribution.performance.test.ts
  • src/main/bots/bot-journal-submission.test-fixture.ts
  • src/main/bots/bot-leave-thread-authority.ts
  • src/main/bots/bot-request-asker.test.ts
  • src/main/bots/bot-request-asker.ts
  • src/main/bots/bot-request-askers-of.ts
  • src/main/bots/bot-typed-in-orca.test.ts
  • src/main/bots/bot-typed-in-orca.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@AmethystLiang
AmethystLiang merged commit 5a61745 into main Nov 26, 2026
57 checks passed
@AmethystLiang
AmethystLiang deleted the AmethystLiang/fix-sta-18558-attribution branch November 26, 2026 06:24
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