Skip to content

test(serve): pin zero duplicate agent resumes across headless serve desktop promotion - #12666

Merged
Jinwoo-H merged 1 commit into
mainfrom
test/serve-desktop-promotion-regression
Aug 5, 2026
Merged

Jinwoo-H merged 1 commit into
mainfrom
test/serve-desktop-promotion-regression

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Closes STA-1716 (#8457) acceptance criterion 6.

Description

STA-1716 / #8457 reported that a packaged orca serve can own the single-instance lock after the desktop app quits with Cmd+Q, so a forced relaunch (open -n -a Orca) mounts a renderer inside the headless process, and hydration cold-restores panes whose daemon PTYs are still alive — launching duplicate codex resume <session> agents and printing Conversation interrupted in the surviving ones.

The lifecycle fixes have all landed: #8646 built the supported headless-to-desktop handoff (activation gate, orca open waiting for a real window, fail-closed when the persistent provider is missing), #12212 stopped a duplicate orca serve from dragging the headless owner into the foreground, and #12574 stopped hidden restorable panes from being forked into replacement resume tabs. What was missing is the assertion the incident was actually about: how many agents the promoted renderer resumes. The existing gate covers PTY identity across promotion; nothing covered resume accounting.

This PR adds that regression and registers it in the #8457 reliability gate.

settleServeDesktopActivation moves from src/main/index.ts into src/main/startup/serve-desktop-activation.ts so the test exercises the production decision instead of re-implementing it — the prior repro test for this issue had to mirror it, which is exactly the failure mode where a test passes without the scenario running.

Evidence

pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/lib/serve-desktop-promotion-session-continuity.test.ts — 6 passed.

The test drives the real modules in sequence: shouldSkipSingleInstanceLock / shouldBypassSingleInstanceLock (packaged serve keeps the lock), acquireSingleInstanceLock + shouldActivateDesktopForSecondInstance (the relaunch reaches the headless owner's second-instance handler), createServeDesktopActivationGate + settleServeDesktopActivation (coalesce until the persistent provider settles), focusExistingMainWindow (exactly one window is constructed), then resumeSleepingAgentSessionsForWorktree against the real app store seeded as a renderer freshly mounted inside that process — three surviving codex agent panes on one worktree, only one of them the visible tab.

Oracle: zero launches, no new tab, no queued startup command, no automatic-resume claim, and every surviving session record left identity-equal. Asserted both before the panes rebind their PTYs (cold hydration) and after (live PTY map populated).

Each guard was broken to confirm the test is real, then restored:

Guard broken Result
paneWillConnectOnActivation back to its pre-#12574 visible-tab form red, 2 duplicate codex resume tabs
plus stablePaneHasLivePty forced to see no live PTYs red, both hydration passes
shouldActivateDesktopForSecondInstance always true red, a window opens for a duplicate --serve
settleServeDesktopActivation always marking ready red, fail-closed diagnostic gone
focusExistingMainWindow refusing to open red, both promotion assertions

Also green: src/main/startup, src/main/window, src/main/runtime/orca-runtime.test.ts, src/cli/runtime-client.test.ts, and the resume-sleeping-agent-session / wake-sleeping-agents-in-background suites — 59 files, 1562 tests. Typecheck (node + cli + web), oxlint, changed-code quality audit, and check:reliability-gates all pass.

ELI5

When you quit Orca with Cmd+Q, a background orca serve can be left holding the door. Reopening Orca used to walk in through that background process and accidentally start fresh copies of agents that were still running, cutting off the originals mid-sentence. That's fixed. This PR adds the alarm that goes off if anyone breaks it again: it walks the whole path — background server holds the lock, you force a reopen, a window appears — and then checks that exactly zero agents were restarted.

User-regression-tradeoffs

Test-only behavior change. The one production edit moves a four-line decision out of index.ts into the module it already belongs to, with identical semantics (hasPersistentPtyProvider is the negation of the previous instanceof LocalPtyProvider check); src/main/startup/serve-desktop-activation-wiring.test.ts still pins the call site and its ordering. config/tsconfig.tc.web.json gains five explicit main-process file entries because it is a composite project and the test imports those pure lifecycle modules — same pattern already used there for src/main/wsl.ts and friends.

…esktop promotion

STA-1716 / #8457 acceptance criterion 6 asked for an automated regression over
desktop exit + surviving daemon sessions + headless serve lock owner + a GUI
activation attempt. The lifecycle fixes landed (#8646, #12212, #12574) but
nothing asserted the outcome the incident was actually about: how many agents
the promoted renderer resumes.

The new test drives the real modules end to end — packaged serve takes the
single-instance lock, a forced relaunch reaches its second-instance handler,
the activation gate coalesces until the persistent provider settles, one window
opens, and the renderer hydrating inside that process resumes zero agents both
before and after its panes rebind the surviving daemon PTYs.

settleServeDesktopActivation moves out of index.ts so the test exercises the
production decision instead of mirroring it.

Red evidence: reverting the hidden-pane ownership predicate to its pre-#12574
form launches 2 duplicate `codex resume` tabs; also zeroing the live-PTY check
makes both hydration passes red; dropping the duplicate-serve argv guard opens
a window for `--serve`; always marking the gate ready removes the fail-closed
diagnostic; refusing to open a window drops both promotion assertions.
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Serve startup now uses settleServeDesktopActivation to set the activation gate based on persistent PTY provider availability. The web TypeScript configuration includes related main-process sources. A renderer regression suite covers desktop promotion, single-instance ownership, duplicate serve suppression, PTY failure handling, persisted pane hydration, and prevention of duplicate agent resumption. Reliability-gate metadata tracks the test, assertions, and macOS evidence.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the regression test and its purpose of preventing duplicate agent resumes during headless serve desktop promotion.
Description check ✅ Passed The description clearly explains the change, regression scenario, testing evidence, and tradeoffs, but it omits explicit AI Review, Security Audit, and Screenshots sections.
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.

@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: 1

🧹 Nitpick comments (1)
src/renderer/src/lib/serve-desktop-promotion-session-continuity.test.ts (1)

175-206: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Narrow the as never cast so the seeded store shape stays type-checked.

Line 202 casts the whole seeded object to never. That disables type checking for every key in this literal, including terminalLayoutsByTabId and ptyIdsByTabId. makeTerminalTab at Line 139 returns Record<string, unknown>, which forces the outer cast.

The risk is a silently vacuous test. expectNoAgentWasResumed verifies tabsByWorktree and sleepingAgentSessionsByPaneKey, so a rename in those two keys fails the test. It does not verify ptyIdsByTabId or terminalLayoutsByTabId. Those two keys are the only difference between the reattached: false and reattached: true cases. If either key is renamed in the store, the reattached parameter becomes a no-op, both hydration tests still report 0 resumes, and the regression coverage disappears without a failure.

Replace the cast with a partial store type so a renamed key breaks the build.

♻️ Proposed change to keep the seeded keys type-checked
-function makeTerminalTab(id: string): Record<string, unknown> {
+type AppStoreState = ReturnType<typeof useAppStore.getState>
+type TerminalTab = AppStoreState['tabsByWorktree'][string][number]
+
+function makeTerminalTab(id: string): TerminalTab {
   return {
     id,
     ptyId: null,
     worktreeId: WORKTREE_ID,
     title: 'codex',
     customTitle: null,
     color: null,
     sortOrder: 0,
     createdAt: 1
-  }
+  } as TerminalTab
 }
       sleepingAgentSessionsByPaneKey: Object.fromEntries(
         records.map((record) => [record.paneKey, record])
       )
-    } as never,
+    } satisfies Partial<AppStoreState> as Partial<AppStoreState>,
     false
   )

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 616bc9f6-a0c8-4bf3-8907-51aaad0d2b45

📥 Commits

Reviewing files that changed from the base of the PR and between e233d6a and e9b5ae2.

📒 Files selected for processing (5)
  • config/reliability-gates.jsonc
  • config/tsconfig.tc.web.json
  • src/main/index.ts
  • src/main/startup/serve-desktop-activation.ts
  • src/renderer/src/lib/serve-desktop-promotion-session-continuity.test.ts

Comment on lines +987 to +995
{
"date": "2026-08-05",
"runner": "local",
"platform": "macos",
"command": "pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/lib/serve-desktop-promotion-session-continuity.test.ts",
"result": "passed",
"durationSeconds": 2,
"summary": "Six tests passed joining the serve lock owner, the activation gate, and the promoted renderer's resume accounting. Red evidence: reverting the hidden-pane ownership predicate launched two duplicate codex resume tabs; additionally zeroing the live-PTY check made both hydration passes red; removing the duplicate-serve argv guard opened a window for `--serve`; always marking the gate ready removed the fail-closed diagnostic; refusing to open a window dropped both promotion assertions."
},

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.

🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

Advance the manifest updatedAt to cover the new evidence-run date.

Line 988 records "date": "2026-08-05". Line 3 still declares "updatedAt": "2026-08-04". Every other evidence run in this manifest is dated on or before 2026-08-04, so updatedAt currently reads as the high-water mark for recorded evidence. This change breaks that property.

Set updatedAt to 2026-08-05.

🛠️ Proposed fix
   "schemaVersion": 1,
-  "updatedAt": "2026-08-04",
+  "updatedAt": "2026-08-05",
   "policy": {

@Jinwoo-H
Jinwoo-H merged commit 84df99e into main Aug 5, 2026
43 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…esktop promotion (stablyai#12666)

STA-1716 reported that a packaged `orca serve` could become the single-instance owner after the desktop app exits, leaving Dock/Finder unable to restore a window — and that forcing a reopen made the headless process hydrate a renderer that interrupted and DUPLICATED live agent sessions.

Verification against main found every criterion already fixed (stablyai#8646 for desktop promotion and the fail-closed CLI, stablyai#12212 for duplicate serve activation, stablyai#12574 + stablyai#9729 for the resume/ownership guards). The genuine gap was criterion 6: the ticket's own automated regression never existed. An existing reliability gate asserted PTY identity survives promotion, but nothing asserted what the incident was actually about — how many agents the promoted renderer resumes.

This adds that coverage: a unit/service-level journey that drives the real single-instance lock, activation gate, settle and focus paths, then runs the real resume logic against a store seeded as a renderer freshly mounted inside the serve process, asserting zero duplicate resumes.

`settleServeDesktopActivation` moved from `index.ts` into its own module with identical semantics, so the test drives the real decision rather than re-implementing it — the earlier repro had to mirror that logic locally, which is the "test passes without running the scenario" failure mode.

Proven to be a real oracle: breaking each guard individually turns it red, and reverting the pre-stablyai#12574 pane form reproduces the incident exactly (two duplicate `codex resume` tabs).
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