Skip to content

Stop re-reading chat history when ending answered connector turns - #49272

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/sta-18083-18086-reply
Nov 25, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/sta-18083-18086-reply

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When a chat turn ends, Orca reads that turn's rows to find the reply it should send back. For a turn that only read files, it kept reading every older row in the chat even after it had found the answer. It also built the same list of answered messages twice. Now it stops once the answer is found and builds the list once.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​68 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​68
Prod 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​21 $\color{#cf222e}{\Huge{\mathbf{−}}}$​11 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​10

What Changed

  • structuredAgentSessionTurnReply stops scanning once both answers (plain prose and bot prose) are found. Before, it could stop only after finding a tool call that may have changed something, so a read-only turn always scanned every row in its window back to the start. During recovery, that window can stretch from an old unanswered request to the head of the chat. The "tools ran" flag is still collected whenever the turn has no bot answer, which is the only time anything reads it (no_answer outcome, empty-answer retry, the outcome sentence).
  • At live turn end, outboxTurn builds the turn's answered-message list once. That one list now feeds both the reply stamps and the check for whether the turn answered other messages. Before, it was built twice. The new connectorReplyStampsOf takes the already-built list, and connectorReplyTurnStamps wraps it for the callers that don't have the list yet. Turn attribution had the same double call and now uses the same path.

Why

Both were extra work that grew with history size and ran on every connector turn end and on every recovered turn. The early stop keeps every output the same: once both answers are found, nothing earlier in the scan can change them, and the only value left unset is one that no code reads for an answered turn.

Linked Issue

Internal release-scan findings (no GitHub issue).

Visual Proof

N/A: no UI or behavior change; this only cuts work done while ending and recovering turns.

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

  • Added a perf contract, structured-agent-session-turn-reply.performance.test.ts. It counts the rows the scan reads for an answered read-only turn after 1,000 and 10,000 rows of history: 1 with this fix, 1,002 and 10,002 with the fix reverted (checked on Linux). A control case checks that a turn with no bot answer still reports an earlier edit as a tool run.

  • A throwaway benchmark (not committed) ran the real endedTurn and counted calls to journalTurnAnsweredMessages and the item reads inside them, before and after.

  • pnpm check:changed passes on Linux.

Performance

Measure Before After
Rows read, answered read-only turn after 1,000 rows 1,002 1
Rows read, answered read-only turn after 10,000 rows 10,002 1
Answered-message lists built per live turn end 2 1
Item reads for those lists, 1,000 rows / 10 steers 19,890 9,945
Item reads, 10,000 rows / 10 steers 199,890 99,945
Item reads, 10,000 rows / 1,000 steers 18,999,000 9,499,500

No new allocations, timers, or I/O.

Risk

Level: Low. The riskiest part is the "tools ran" flag, which an answered turn can now return as false where it used to return true. Every reader of that flag checks it only after confirming the turn had no answer, so nothing users see changes. If it went wrong, a turn with no answer would leave out the "tools ran, check before asking again" note. A plain revert backs this out; nothing is saved differently.

AI Disclosure

Review

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.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Connector reply stamp collection now uses answered messages computed by callers. The structured turn reply scan now stops when it finds bot prose, even when no tools ran. New performance tests check answer results and turnScope access with large histories, and verify side-effect reporting when a turn has no bot answer.

Merge Risk: Minimal · up to 7c048

This change avoids re-reading long chat history when ending answered connector turns, and it keeps reporting tool side effects for turns that got no answer. No concrete regression was found, and the flag that changes is only read for unanswered turns. The merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: ending answered connector turns no longer re-reads chat history. The description and file summaries confirm the early scan stop and the single answered-message list. T…
Description check Passed The description follows the template. It covers ELI5, What Changed, Why, Linked Issue, Visual Proof (N/A with reason), Testing, Performance with measured numbers, Risk with level and revert path, and …
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: ending answered connector turns no longer re-reads chat history. The description and file summaries confirm the early scan stop and the single answered-message list. The title is specific and matches the changeset, so it passes.

Full details: Description check

Explanation

The description follows the template. It covers ELI5, What Changed, Why, Linked Issue, Visual Proof (N/A with reason), Testing, Performance with measured numbers, Risk with level and revert path, and the checklist. The Linked Issue section says there is no GitHub issue, which the template asks for, so that gap is noted. The AI Disclosure and Review sections are empty, which is acceptable for an internal contributor under the template's instruction.

  • 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: 30986916-2d47-46a6-a7c4-9db0834f62e5
Commits

Reviewing files that changed from the base of the PR and between 4ccbfc2 and 7c04830.

Files selected for processing: 5

  • src/main/external-conversations/external-conversation-ended-turn.ts
  • src/main/native-chat/agent-session-journal/journal-domain-turn-attribution.ts
  • src/shared/agent-session/journal/journal-connector-reply-turn.ts
  • src/shared/agent-session/requests/structured-agent-session-turn-reply.performance.test.ts
  • src/shared/agent-session/requests/structured-agent-session-turn-reply.ts

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

@brennanb2025

brennanb2025 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Perf scan on 7c04830 (fresh reviewer): PASS, nothing blocking.

  • structured-agent-session-turn-reply.ts: the early stop changes only toolsRan, and only when both answers are non-empty. Every reader checks for a no_answer outcome before reading it. For an answered read-only turn, each turn end used to cost one read per row in the reply window and now costs one per row of the answer's tail. The perf contract guards the count: 1 row read where the old code read N+2.
  • external-conversation-ended-turn.ts and journal-domain-turn-attribution.ts: journalTurnAnsweredMessages now runs once per turn end instead of twice, with the same output.
  • Recovery (external-conversation-reply-turn-fill.ts) is unchanged and gets the same early stop.
  • No new timers, polling, subscriptions, IPC, store writes, subprocesses, sync I/O or startup imports.

@brennanb2025

brennanb2025 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Review status for 7c04830: ready

Problem: at each connector turn end and each recovered turn, Orca did work that grew with chat history:

  • For an answered read-only turn, the reply scan kept reading every older row in its window, looking for a tool side effect that no code reads once the turn has an answer.
  • The live turn end built the turn's answered-message list twice.

Change: the scan stops once both answers are found, and the answered-message list is built once and shared. Nothing users see changes.

Verified:

  • Fresh Claude and Codex reviewers both said SAFE on this head. They traced every reader of the tools-ran flag: each one checks for a no_answer outcome first, which an answered turn can never produce.
  • The regression audit said SAFE (other callers, recovery, platforms, saved data).
  • A fresh perf scan passed on this head.
  • Perf contract: rows read for an answered read-only turn go from 1,002 / 10,002 to 1 / 1. The test fails with the fix reverted.
  • Benchmark through the real endedTurn: answered-message lists built per turn end go from 2 to 1, which halves the item reads (for example, 199,890 to 99,945 at 10k rows / 10 steers).
  • pnpm check:changed passes on Linux, and CI is green.

Deferred: none. Building the answered-message list still searches the items once per steer (submissions × items). That cost was already there and is now paid once instead of twice.

@brennanb2025
brennanb2025 merged commit 89960aa into main Nov 25, 2026
59 checks passed
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