Skip to content

Sleeping agents: skip wake work for refused input, unrelated hosts and unchanged records - #49413

Merged
brennanb2025 merged 8 commits into
mainfrom
brennanb2025/fix-sleeping-agent-wake
Nov 26, 2026
Merged

brennanb2025 merged 8 commits into
mainfrom
brennanb2025/fix-sleeping-agent-wake

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When an agent is asleep, Orca was doing extra work it didn't need to: it could restart a sleeping agent only to then refuse the message it was waking it for, and some background checks re-read every sleeping agent when only one thing (or nothing relevant) had changed. This PR makes each of those checks look only at what actually changed.

What Changed

  • Refused input never wakes a sleeping agent (STA-18493). terminal.send now refuses a guarded combined send (text + Enter with requireAgentStatus: 'sendable') before it wakes the slept agent, and the keyed desktop-prompt path skips its wake when the handler would refuse the input (guarded combined send, or text over the 16 MiB terminal input limit). Before, both paths cold-started the agent's process and waited for it to idle, then wrote zero bytes. New shared check: terminal-send-pre-wake-refusal.ts. A keyed prompt it refuses runs the handler directly and keeps no receipt (the refusal is deterministic, so a retry, replay-only or not, gets the same refusal). The executor's size measurement is reused by the handler, so a prompt's text is still scanned once.
  • Host reconnects retry only that host's sleepers (STA-18490). An SSH host connecting, or a runtime environment becoming verified (the renderer now passes its environment ID), retries only the failed wakes and re-checks only the held mail of sleepers on that host (SleptPaneHost). A sleeper whose host can't be determined still counts, so no wake gets stranded. Before, any host edge cleared every failed wake and re-read every tracked mailbox.
  • One mail wake reads its Run owner twice, not seven times (STA-18491). An empty waiter-reservation set now selects like none and reuses the publication refresh. batch reads the owner once, not twice, and the wake's first read is passed into run instead of being repeated.
  • A checkpoint write re-indexes only the record it changed (STA-18492). The sleeping-mail index skips its index rewrites for a record object the write left untouched.
  • Graph replacements skip leaf counting with no waiter (STA-18494). replaceLeaves counts kept leaves only while a slept-agent wait is listening.

Why

Each fix narrows existing work to what changed, inside the same mechanism. No new state machine, no new setting.

Linked Issue

STA-18490, STA-18491, STA-18492, STA-18493, STA-18494

Visual Proof

N/A: main-process bookkeeping with no UI change.

Testing

  • Automated tests added/updated, or explained why not below
  • Every added test fails without this change and avoids the test anti-patterns

Each new test was run against the parent commit's source (fails) and this head (passes):

Ticket Test Before After
STA-18493 slept-agent-wake.test.ts: refused input through the real RPC dispatcher slept agent cold-resumed (test times out waiting on the wake) resume never called, zero bytes, refused
STA-18490 slept-mailbox-wake.performance.test.ts: unrelated SSH + runtime reconnects, 100 / 1,000 local sleepers 500 / 5,000 Run reads 0, and the sleepers' own host edge still retries
STA-18491 same file: one wake with an empty reservation set 7 Run + 4 sequence reads before prepare 2 + 2
STA-18492 sleeping-mail-record-index.performance.test.ts: 1 of 100 / 1,000 records changed 100 / 1,000 index rewrites 1
STA-18494 source: one Map.has per leaf per replacement with zero listeners N per replacement 0

pnpm check:changed passes. The existing slept-agent wake, mailbox wake, record index and crew-watch suites pass too.

Performance

Every change removes work: fewer SQL reads per wake and per host edge, no index rewrites for unchanged records, and no process start for refused input. The only added work is one in-memory host check per failed or tracked sleeper on a host edge (a map lookup and a repo lookup), which replaces 3+ SQL reads per tracked mailbox.

Risk

Level: Medium

  • The riskiest part is host-edge scoping: if a sleeper's host were misidentified, its failed wake would not retry on that host's reconnect. Host IDs are built with the shared encoders (so WSL runtime environments match their saved spelling), and a sleeper counts as on a host when its sleep-record partition, its SSH connection ID or its repo's execution host matches, and also when its host is unknown. Window reloads and pane edges still retry everything.
  • Back out with a plain revert. No saved data changes.

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

Cross-platform: no platform-specific code. Remote: SSH and runtime-environment edges are now scoped per host, as described above. Mobile: no change. Folder workspaces: a sleeper whose worktree has no repo counts as on every host.

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)

@brennanb2025
brennanb2025 marked this pull request as draft November 26, 2026 02:15
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Sleeping-mail retries can be scoped to an execution host, and wake processing now selects batches before starting runs. Record-index refresh checks that mappings remain current before skipping unchanged records. Terminal-send refusal checks run before wake paths. Terminal identity leaf counts now depend on whether handle listeners are registered.

Merge Risk: Minimal · up to 3b0f9

This change reduces unnecessary work when waking sleeping agents, and the review found no remaining merge-blocking issue. Refused or oversized terminal sends no longer wake the agent, and host-scoped retries cover the expected cases. Normal checks are sufficient before merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main theme of the change: sleeping-agent wake work is skipped for refused input, unrelated hosts, and unchanged records. The changeset matches all three parts. It also covers the R…
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.
Description check Passed The description follows the repository template closely. It has an ELI5, a What Changed section listing five fixes with ticket IDs, a Why section, a Linked Issue section listing STA-18490 through STA-…
Full details: Title check

Explanation

The title names the main theme of the change: sleeping-agent wake work is skipped for refused input, unrelated hosts, and unchanged records. The changeset matches all three parts. It also covers the Run-owner read reduction and leaf-counting change in less detail, which the title does not need to list. The title is 88 characters, so it is slightly long but still clear.

Full details: Description check

Explanation

The description follows the repository template closely. It has an ELI5, a What Changed section listing five fixes with ticket IDs, a Why section, a Linked Issue section listing STA-18490 through STA-18494, a Visual Proof section marked N/A with a reason, a Testing section with a before/after table of the new tests, a Performance section, a Risk section with level Medium and a revert plan, and a completed checklist. The Linked Issue section lists ticket IDs without a 'Fixes #' GitHub issue link. The template says outside contributors need a real linked issue, but the author appears to be a maintainer, so this is a minor gap. The AI Disclosure section is absent, which is acceptable for internal contributors. The Why section is brief and gives only a general reason for the approach, not the alternatives considered.

  • 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: 65967ff0-5cf2-4b51-8fa3-30d37b0f00a5
Commits

Reviewing files that changed from the base of the PR and between 4295efd and 3b0f9c0.

Files selected for processing: 1

  • src/main/runtime/rpc/orchestration/mutations/orchestration-mutation-executor.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.

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

Actionable comments posted: 2

Nitpick comments: 1

src/main/runtime/orchestration/mailbox/routing/slept-mailbox-wake.performance.test.ts: 1

460-461: Trivial · Maintainability & Code Quality · Quick win

Use explicit counts for the matching-host retry control.

The initial check accepts any positive attempt count. The matching-host check accepts any increase. For 1,000 sleepers, counts of 1 and 2 satisfy both checks, so partial retry processing can pass.

Settle each phase with fake timers and assert explicit admitted-launch counts for each fleet size. Keep the zero-work assertions for unrelated hosts.

As per coding guidelines, a perf contract “counts operations at the layer that owns the behavior … against hardcoded values.”

Also applies to: 471-471

Source: Coding guidelines

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: 4f7d1af3-4bbb-4c2a-a30d-a9c7ac22c8c2
Commits

Reviewing files that changed from the base of the PR and between d3e67bf and 87937ff.

Files selected for processing: 19

  • src/main/runtime/orchestration/mailbox/routing/sleeping-mail-crew-watch.performance.test.ts
  • src/main/runtime/orchestration/mailbox/routing/sleeping-mail-crew-watch.ts
  • src/main/runtime/orchestration/mailbox/routing/sleeping-mail-record-index.performance.test.ts
  • src/main/runtime/orchestration/mailbox/routing/sleeping-mail-record-index.ts
  • src/main/runtime/orchestration/mailbox/routing/slept-agent-wake.test.ts
  • src/main/runtime/orchestration/mailbox/routing/slept-mailbox-runtime.ts
  • src/main/runtime/orchestration/mailbox/routing/slept-mailbox-wake.performance.test.ts
  • src/main/runtime/orchestration/mailbox/routing/slept-mailbox-wake.ts
  • src/main/runtime/orchestration/mailbox/routing/slept-pane-host.ts
  • src/main/runtime/rpc/methods/terminal/terminal-send-method.ts
  • src/main/runtime/rpc/methods/terminal/terminal-send-pre-wake-refusal.ts
  • src/main/runtime/rpc/orchestration/mutations/orchestration-mutation-executor.ts
  • src/main/runtime/terminal/identity/terminal-handle-touches.ts
  • src/main/runtime/terminal/identity/terminal-identity-source.ts
  • src/main/runtime/terminal/lifecycle/disconnected-pty-archive-sweep.performance.test.ts
  • src/main/runtime/terminal/lifecycle/floating-session-liveness.ts
  • src/main/window/renderer-relays/sleeping-mail-window-relay.ts
  • src/preload/api/window-ui/ui-sleeping-mail-api.ts
  • src/renderer/src/hooks/ipc-events/app-lifetime-ipc-bridge.ts

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

@brennanb2025
brennanb2025 marked this pull request as ready for review November 26, 2026 02:49
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Perf scan (fresh perf reviewer) on head e3ef6ba: no P0/P1 findings. Every touched path does the same or less work: refused prompts skip the wake and the receipt, host edges re-check only that host's sleepers, mail wakes do fewer owner reads, unchanged sleep records skip their index writes, and graph replacements with no waiter skip leaf counting. No new timers, polls or idle wakeups on main, preload or renderer.

…t the refusal; keep shared leaf/handle lookups
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Perf scan (fresh perf reviewer) on head 4295efd: no P0/P1 findings. A keyed prompt's text is measured once (the handler reuses the executor's result); refused prompts skip the wake and the receipt; host edges re-check only that host's sleepers; mail wakes do fewer owner reads; unchanged sleep records skip their index writes; graph replacements with no waiter skip leaf counting. No new timers, polls or idle wakeups.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Perf scan (fresh perf reviewer) on head 3b0f9c0: no P0/P1 findings. Every changed hot path does the same or less work than base; a keyed prompt's text is measured once; no new timers, polls or idle wakeups.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Review loop finished on 3b0f9c060f3ecfc72539ab4a7ac18e820de5c065. All five checks pass.

Check Result
Claude reviewer (fresh) PASS, P2s only
Codex reviewer (fresh) PASS, no findings
Blinded regression audit (fresh Codex) SAFE
Perf scan (fresh perf reviewer) PASS, see the comment above
Reference check the approach is standard: validate before resuming; scope reconnect retries per host

Fixed during the loop, each confirmed by a reviewer:

  • Reconnect host IDs are encoded and compared canonically, so WSL environments match either saved spelling.
  • An archive-sweep shortcut that could exceed the 128-archive cap was reverted. STA-18489 is cleared, not fixed: the per-archive check is required to keep the cap exact, and costs about 0.5 ms per sweep at 1,628 archives.
  • A refused keyed prompt now returns its refusal instead of failing on the slept terminal's binding.
  • A keyed prompt's text is measured once instead of twice.
  • Index entries for a sleep record that shares a leaf or handle with another record stay linked.

Remaining P2s, accepted:

  • Local sleepers no longer retry on unrelated host reconnects. That is the point of STA-18490; local failed wakes still retry on graph-ready, pane and reload edges.
  • Refused keyed prompts keep no receipt. The refusal is deterministic, so any retry gets the same refusal.

QA: tests. The STA-18493 test drives the real RPC dispatcher and fails on main. The perf contracts for STA-18490, 18491 and 18492 each fail on main with the before counts in the PR body. slept-pane-host.test.ts covers both host-ID spellings. CI is green.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Perf scan coverage, confirmed by the same fresh perf reviewer for head 3b0f9c0:

  • hot-path: all ten detected files were read and traced through their production callers: sleeping-mail routing and wake, keyed prompt send, terminal identity graph. None does more work than base.
  • publish: the ui:sleepingMailAvailability send in ui-sleeping-mail-api.ts:24 is the same message with one extra string. It fires only when a host first becomes ready and verified, and the main side now publishes only that host's mailboxes.

@brennanb2025
brennanb2025 merged commit 5af7059 into main Nov 26, 2026
60 of 61 checks passed
registered2nd pushed a commit to registered2nd/orca that referenced this pull request Nov 26, 2026
…d unchanged records (stablyai#49413)

* fix(sleeping-agents): stop wake work that refused input, unrelated hosts and unchanged records cause

* test(sleeping-agents): archive sweep skips re-classifying unchanged sleepers

* fix: type the pre-wake refusal check without a cast

* fix(sleeping-agents): encode reconnecting host IDs; keep the exact archive sweep

* fix(sleeping-agents): match reconnecting hosts across saved id spellings

* fix(sleeping-agents): a refused keyed prompt returns its refusal without binding a slept terminal

* fix(sleeping-agents): measure a keyed prompt once; refused replays get the refusal; keep shared leaf/handle lookups

* refactor: drop a condition the early return already covers

(cherry picked from commit 5af7059)
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