Skip to content

fix(orchestration): avoid full message scans on working terminal titles - #19390

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/review-orca-resource-cost
Sep 8, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/review-orca-resource-cost

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​94 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​93
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​8 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​8

ELI5

An agent's working-title updates repeatedly scanned the entire message history, blocking Orca's main thread. Give the cleanup query a direct lookup for the terminal it is cleaning.

What Changed

  • Add a partial index on messages(pointer_pty_id) WHERE pointer_enter_pending > 0 through existing index initialization.
  • Initialize indexes after migration so databases predating pointer columns receive the index on their first open.
  • Add query-plan, settlement-semantics, and existing-database regression coverage; update the old-schema fixture for the new dependent index.

Why

applyTrackedPtyTitle → observeAgentWorking → releasePendingMailboxPointerForPty runs even for repeated working titles with no reservations. On a database with approximately 45,400 messages, SQLite chose SCAN messages; the no-op UPDATE took 13–17 ms. The existing mailbox index excludes read rows and cannot serve this cleanup, which intentionally includes them.

The partial PTY index removes history-sized work without changing delivery behavior, status-transition handling, retention, or the schema version. Both the caller and query originated in #16904 (06a607a1d71).

Linked Issue

Fixes #19389

Visual Proof

N/A — database access-path change; no UI layout or interaction semantics change. Live macOS profiling provides the performance evidence:

Measurement Before After index
Copied-database no-op UPDATE, three runs 12.675 / 16.951 / 12.687 ms 0.027 / 0.006 / 0.003 ms
Main-thread samples in SQLite StatementSync::Run 390/452 and 408/457 2/452
Live main-process interval CPU 65.7 / 106.2 / 99.6% 14.3 / 11.9 / 15.1% in later samples

Applied the same additive index to the running installed profile without restarting processes or deleting data. CPU figures are supporting observations under a changing workload, not a controlled throughput benchmark. Raw samples and the private database copy remain local.

Testing

  • I manually tested these changes locally: query plan/timing on a consistent database copy, then live index application and pre/post stack/CPU sampling on macOS.
  • Automated tests added/updated: 34 tests pass across cleanup query, pointer stage/submit, version-skew, and all-start-version migration suites.
  • pnpm tc:node, changed-file oxlint, formatting, and git diff --check pass. Commit hooks also pass.
  • Linux/Windows/SSH and full build are left to CI. The SQL is platform-independent and runs in the database-owning runtime; no wire fields or remote execution behavior change. Rendered UI latency was not measured.

Review

The index covers already-read reservations as required. Tests retain reserved/write-attempted/enter-attempted settlement behavior and leave other PTYs/history unchanged. No TTL, status-only gate, process cleanup, or new cache is introduced.

Agent skill upstream boundary

  • Not applicable; no upstream skill content copied.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • Before/after evidence provided, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform and SSH/remote impact considered
  • Full lint/typecheck/test/build matrix: CI to cover; targeted local checks above pass

@coderabbitai

coderabbitai Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds conditional creation of the partial idx_messages_pending_pointer_pty index during schema migration. The index supports targeted pending mailbox pointer release. New tests verify pointer clearing, state preservation, index usage, and index reinstallation for current and older schemas. The version-skew migration test also removes the new index before dropping the related column.

Merge Risk: 🟡 Moderate · up to dff4a

Existing databases already at the current schema version will miss the new lookup index, leaving the main-thread cleanup performance issue unresolved. Index repair should run for current-version databases before merge.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: avoiding full message-history scans for working terminal-title updates.
Description check ✅ Passed The description is complete and relevant. It explains the change, motivation, linked issue, performance evidence, testing, scope, compatibility considerations, and the remaining CI validation.
Linked Issues check ✅ Passed The changes satisfy issue #19389 by adding the PTY-specific partial index, initializing it for existing databases, preserving cleanup and delivery semantics, and adding regression coverage for query p…
Out of Scope Changes check ✅ Passed All production and test changes directly support the linked issue and stated objectives. No unrelated or out-of-scope changes are evident.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/runtime/orchestration/db/schema/migrate.ts (1)

18-20: 🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win

Install indexes when the schema version is already current.

Lines 18-20 return before createMailboxDeliveryIndexesIfPossible() runs. This PR does not change SCHEMA_VERSION, so databases created before this PR already have the current version but lack idx_messages_pending_pointer_pty. The cleanup query then continues to scan messages. The current fixture in src/main/runtime/orchestration/mailbox-pointer-release-query.test.ts reproduces this case.

Invoke index repair before this return, or invoke it during current-schema initialization.

Proposed fix
   if (current >= SCHEMA_VERSION) {
+    this.createMailboxDeliveryIndexesIfPossible()
     return
   }

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: f712975c-5982-4594-acf1-a57a87feec6e

📥 Commits

Reviewing files that changed from the base of the PR and between 9f04403 and dff4abf.

📒 Files selected for processing (4)
  • src/main/runtime/orchestration/db/schema/migrate.ts
  • src/main/runtime/orchestration/db/schema/schema-column-probes.ts
  • src/main/runtime/orchestration/mailbox-pointer-release-query.test.ts
  • src/main/runtime/orchestration/orchestration-version-skew-migration.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 4 remain after this review.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Adds a per-PTY partial index — initializeMailboxDeliveryIndexesIfPossible now also creates idx_messages_pending_pointer_pty ON messages(pointer_pty_id) WHERE pointer_enter_pending > 0, giving releasePendingMailboxPointerForPty's cleanup UPDATE a direct seek instead of a full SCAN messages.
  • Runs index init after migration — migrate() now calls createMailboxDeliveryIndexesIfPossible() at the end of the chain, so databases migrating up from pre-pointer schemas get the index on first open (columns are added in migrateMailboxPointerEnterV33, after createTables() already ran).
  • Adds regression coverage — a query-plan assertion that the cleanup uses the new index (and no SCAN messages), a settlement-semantics test covering reserved/written/entered/already-read rows, and existing-database reopen tests for both current and pre-pointer-column databases.

The index selector and predicate match the cleanup WHERE clause exactly, so this changes only the access path, never delivery/status semantics. The partial index is correctly left out of POST_V6_INDEXES / VERSIONED_POST_V6_INDEX_PREDICATES (those gate migration-start detection and would otherwise force a full re-migration); creation is idempotent and converges on every-open via createTables() and post-migration.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Review status: LAND

I completed the requested review/fix loop against dff4abf4f2686f7600d527b308b21d536507e7aa. Two independent adversarial passes and both the opening and closing readiness audits found no proven in-scope P0, P1, or P2 issues, so no code changes, commits, or pushes were warranted.

The fix addresses the root cause at the database boundary: releasePendingMailboxPointerForPty filters on pointer_enter_pending > 0 AND pointer_pty_id = ?, and the new partial index matches that predicate exactly. Current-schema databases acquire it through the existing every-open createTables() index initializer; older databases that gain the pointer columns during migration acquire it through the new post-migration initializer. This is the standard database-owned partial-index and query-plan-proof shape, without a caller guard, cache, retry, schema-version bump, or behavior change.

Validation completed:

  • 34 focused query-plan, settlement-semantics, pointer stage/submit, and version-skew migration tests passed.
  • pnpm tc:node, targeted oxlint, and git diff --check passed.
  • CI static analysis, typecheck, macOS package, Windows package, all eight Node 24 test shards, verify, and Pullfrog passed.
  • An independent 45,400-history-row reproduction changed the no-op cleanup from SCAN messages at about 0.7414 ms/call to indexed SEARCH at about 0.0021 ms/call over 100 calls. The PR's production-profile measurements independently show the same directional result.
  • Hidden Electron CDP validation attached to the exact PR worktree identity, rendered the full app with zero console errors, and confirmed launch/runtime health. This PR intentionally has no UI semantics to exercise.

The automated concern that current-version databases miss the index does not apply: OrchestrationDb calls createTables() before migrate(), and createTables() runs the idempotent index initializer. The passing current-schema reopen regression test covers this path.

Hidden Electron validation of PR #19390

The requested supervised Grok Electron worker was launched, but Grok's weekly provider quota blocked it before execution. I recorded and stopped that exact worker, then collected the Electron evidence directly. This is a process gap rather than a code or validation finding.

No mobile-facing, wire, provider, SSH, WSL, path, shell, or platform-specific behavior changed. The branch matches its remote and is mergeable/clean. Recommendation: land.

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.

Working terminal titles repeatedly scan orchestration message history on the main thread

1 participant