Skip to content

fix(runtime): drain pending session-tabs notify before a new subscriber joins - #11022

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/runtime-session-tabs-flake
Jul 28, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/runtime-session-tabs-flake

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

The flake

src/main/runtime/multi-client-navigation-isolation.integration.test.ts → "routes explicit host and paired-client follow intent without changing the default" fails intermittently on CI for unrelated PRs. It is not caused by any particular PR — clean main fails it on its own.

Mechanism

A race between the 50ms session-tabs notify coalescer and listener registration.

  • A notify is armed by seedSessionTabs → syncWindowGraph (orca-runtime.ts), on the 50ms trailing-edge window in mobile-session-tabs-notify-coalescer.ts.
  • session.tabs.subscribe reads its initial snapshot, emits it, then registers a listener via onMobileSessionTabsChanged.
  • If the pending timer fires after that listener registers but before the next mutation (session.tabs.activate) is handled, it delivers a stale { type: 'updated', activeTabId: 'host-tab' } — state the subscriber's initial snapshot had already folded in.

The test consumes that frame at multi-client-navigation-isolation.integration.test.ts:515, producing the CI assertion:

AssertionError: expected 'host-tab' to be 'client-a-tab'

Instrumented trace on a failing run, showing the ~2ms margin:

[TRACE 23412] coalescer.schedule repo-1::/tmp/session
[TRACE 23460] onMobileSessionTabsChanged REGISTER 1f1129ff-…
[TRACE 23461] onMobileSessionTabsChanged REGISTER 5070bfc2-…
[TRACE 23462] coalescer.fire repo-1::/tmp/session      <-- lands between subscribe and activate

The fix

One line: flushAll() the coalescer at the top of onMobileSessionTabsChanged, before the listener joins the set — mirroring the flushAll() its own unsubscribe closure already performs.

Draining rather than cancelling matters: a cancel/dispose would silence the stale frame but drop the update for subscribers that were already listening. flushAll delivers it to them and simply excludes the newcomer, whose initial snapshot already covers it.

Measurement

Reproduced and verified by sweeping the coalescer flush window across the race boundary (12 delays × 2 reps, single test):

delays 45–56ms × 2 reps
clean main (6943638) 5 / 24 failed
with this fix 0 / 24 failed

Both observed failure modes were the same stale frame, one step apart:

  • expected 'host-tab' to be 'client-a-tab'
  • expected [ 'client-a-tab', 'host-tab' ] to deeply equal [ 'client-b-tab', 'client-b-tab' ]

Full file at the real 50ms default: 5/5 pass.

Regression test

graph-sync-mobile-snapshot-gating.test.ts — deterministic, on fake timers, no wall-clock dependency. It arms a notify, advances 20ms into the window, subscribes late, then runs the clock out.

It is two-sided, and I verified both directions:

  • without the fix → fails on the primary assertion (expected [ {…} ] to deeply equal [] — the late subscriber got the stale frame)
  • with the flush swapped for a dispose/cancel → fails on expected +0 to be 1 (an existing subscriber lost the update)

Verification

  • src/main/runtime full suite: 2885 passed, 5 skipped, 0 failed (181 files)
  • tsc --noEmit × 3 (node / cli / web, --composite false, tsbuildinfo cleared first): clean
  • oxlint: exit 0, no warnings in either touched file
  • check:max-lines-ratchet: OK, no new bypasses
  • oxfmt: no changes

Scope is this race only — the coalescer and the session-tabs RPC are untouched, and the failing test was not weakened, skipped, retried, or deleted.

…er joins

The 50ms session-tabs notify coalescer races listener registration. A notify
armed before a subscriber existed (e.g. by seedSessionTabs -> syncWindowGraph)
fires after that subscriber registers but before the next mutation is handled,
delivering an `updated` frame carrying pre-subscribe state — state the
subscriber's initial snapshot already folded in.

Flush the coalescer at the top of onMobileSessionTabsChanged, before the
listener joins, mirroring the flushAll its own unsubscribe closure performs.
Draining rather than cancelling keeps existing subscribers whole.

This is the root cause of the intermittent CI failure in
multi-client-navigation-isolation.integration.test.ts ("routes explicit host
and paired-client follow intent without changing the default"), which taxed
unrelated PRs. Measured by sweeping the flush window across the race boundary
(12 delays x 2 reps): clean main 5/24 failed, with the fix 0/24.

The regression test is deterministic (fake timers, no wall-clock dependency)
and two-sided: it fails without the fix, and also fails if the flush is
swapped for a cancel that would drop the update for existing subscribers.
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 89f6a0ae-8b8d-4a28-a3c6-cac3d9d38640

📥 Commits

Reviewing files that changed from the base of the PR and between 0956d5c and 9536851.

📒 Files selected for processing (2)
  • src/main/runtime/graph-sync-mobile-snapshot-gating.test.ts
  • src/main/runtime/orca-runtime.ts

📝 Walkthrough

Walkthrough

The runtime now flushes pending coalesced mobile session tab notifications before registering a listener. A new test verifies that a pending notification is delivered to the existing subscriber and is not replayed to a subscriber added during the coalescing window.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the bug and fix, but it omits required template sections like Screenshots, AI Review Report, Security Audit, and Notes. Add the missing template sections, including a no-visual-change screenshots note, testing checklist, AI review details, security audit, and notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the race fix and draining behavior for new session-tabs subscribers.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 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.

brennanb2025 added a commit that referenced this pull request Jul 28, 2026
…ble (#10805)

Closes #10793.

When Orca could not verify the originating Codex session file it either threw — a
red per-pane toast and a failed spawn, reported as constant spam on #10757 — or
returned null. Returning null did NOT start a fresh session: the renderer had
already baked ['codex','resume',<id>] into the command and pty.ts never rewrote
it, so CODEX_HOME simply fell through to whichever account was selected.

The resume argv is now dropped so a plain `codex` launches, with a banner telling
the user. The invariant — never run `codex resume <id>` under an account that does
not own that rollout — is now satisfied by construction rather than by refusing to
spawn. A verified resume is unchanged and still pins CODEX_HOME to the
originating home.

Reviewed over two adversarial rounds; seven defects found and fixed, including a
HIGH where local-provider (non-daemon) spawns still carried
ORCA_SEQUENCED_STARTUP_COMMAND with `resume <id>` — the wrong account behind a
banner claiming it started fresh. `env` is now declared after the strip so no
point in the handler can reach the pre-strip value.

Live-validated in a real Orca dev build: all five cases proven on the SPAWNED
PROCESS, including a real rollout under an untrusted home (the only shape that
discriminates) and the local-provider path forced by stopping the daemon.

An earlier CI failure on multi-client-navigation-isolation.integration.test.ts was
investigated and is a PRE-EXISTING flake — a ~4ms race in the session-tabs notify
coalescer that fails 5-8/24 on clean main, more often than on this branch. Fixed
separately in #11022.

Not verified: no Windows execution — its POSIX-only tests skip there and the
#10757 reporter is on Windows. SSH is partial: no spurious banner or drop observed
against a real target, but headless spawn does not deliver startup commands so the
remote argv could not be read. The relay/mobile notice channel deliberately has no
banner; the argv drop does happen there, so the invariant holds.
@brennanb2025
brennanb2025 merged commit dc45f79 into main Jul 28, 2026
23 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…ble (stablyai#10805)

Closes stablyai#10793.

When Orca could not verify the originating Codex session file it either threw — a
red per-pane toast and a failed spawn, reported as constant spam on stablyai#10757 — or
returned null. Returning null did NOT start a fresh session: the renderer had
already baked ['codex','resume',<id>] into the command and pty.ts never rewrote
it, so CODEX_HOME simply fell through to whichever account was selected.

The resume argv is now dropped so a plain `codex` launches, with a banner telling
the user. The invariant — never run `codex resume <id>` under an account that does
not own that rollout — is now satisfied by construction rather than by refusing to
spawn. A verified resume is unchanged and still pins CODEX_HOME to the
originating home.

Reviewed over two adversarial rounds; seven defects found and fixed, including a
HIGH where local-provider (non-daemon) spawns still carried
ORCA_SEQUENCED_STARTUP_COMMAND with `resume <id>` — the wrong account behind a
banner claiming it started fresh. `env` is now declared after the strip so no
point in the handler can reach the pre-strip value.

Live-validated in a real Orca dev build: all five cases proven on the SPAWNED
PROCESS, including a real rollout under an untrusted home (the only shape that
discriminates) and the local-provider path forced by stopping the daemon.

An earlier CI failure on multi-client-navigation-isolation.integration.test.ts was
investigated and is a PRE-EXISTING flake — a ~4ms race in the session-tabs notify
coalescer that fails 5-8/24 on clean main, more often than on this branch. Fixed
separately in stablyai#11022.

Not verified: no Windows execution — its POSIX-only tests skip there and the
stablyai#10757 reporter is on Windows. SSH is partial: no spurious banner or drop observed
against a real target, but headless spawn does not deliver startup commands so the
remote argv could not be read. The relay/mobile notice channel deliberately has no
banner; the argv drop does happen there, so the invariant holds.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…ibes (stablyai#11022)

multi-client-navigation-isolation.integration.test.ts fails intermittently on
clean main — measured at 4-8/24 with a flush-window sweep — and has been taxing
unrelated PRs across the repo.

Mechanism, confirmed by instrumented trace (schedule t=23412, listener REGISTER
23460/23461, coalescer fire 23462): a ~4ms race between the 50ms session-tabs
notify coalescer and listener registration in onMobileSessionTabsChanged. When
the pending timer fires after the listener registers but before
session.tabs.activate is handled, it emits a stale
{type:'updated', activeTabId:'host-tab'} that the test consumes.

flushAll() at the top of onMobileSessionTabsChanged, before the listener joins the
set — mirroring the flushAll() its own unsubscribe closure already performs.

DRAINING rather than cancelling is load-bearing: cancel/dispose has no emit, so it
would silence the stale frame but DELETE the pending update for subscribers
already registered, leaving them stale until an unrelated next schedule(). That
would trade a flaky test for a real lost-update bug. The newcomer cannot miss an
update by having it drained — listMobileSessionTabs and the coalesced emit read
the same mobileSessionTabsByWorktree map, and snapshot-read to listener-add is
macrotask-atomic.

Sweep: 4-5/24 before, 0/24 after. The regression test uses fake timers and is
two-sided — it fails if flushAll is removed (stale frame delivered) and fails if
flush is swapped for dispose (nothing drained), on different assertions, so it
pins the specific choice rather than merely the presence of a change.

Reviewed independently and returned clean with both mutations re-run by the
reviewer. A latent flushAll re-entrancy double-emit exists in principle — fire()
does not re-check pending.has the way flush() does — but is unreachable: both
production callers' listeners only invoke the dispatcher reply, a shape that has
shipped since stablyai#8141 on the symmetric unsubscribe flush.

Not verified: live paired-device mobile/relay behaviour (no device available); the
mobile conclusion rests on a code trace.
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