Skip to content

fix(macos): route fresh PTYs away from skewed daemons - #17760

Closed
Jinwoo-H wants to merge 1 commit into
mainfrom
aug31-sta17696-macos-cwd-permission-audit
Closed

Jinwoo-H wants to merge 1 commit into
mainfrom
aug31-sta17696-macos-cwd-permission-audit

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​81 $\color{#cf222e}{\Huge{\mathbf{−}}}$​7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​74
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​63 $\color{#cf222e}{\Huge{\mathbf{−}}}$​51 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​12

Summary

  • preserve live sessions owned by a daemon from a previous app bundle while routing new macOS PTYs through the current app's local provider
  • keep the fallback sticky until an explicit daemon restart, so a generic health probe cannot restore the stale daemon's TCC attribution
  • classify app-path/bundle skew before resolver checks and add Darwin-forced, revert-sensitive regression coverage

Fixes #17696

Validation

  • pnpm test src/main/daemon/daemon-init-endpoint-adoption.test.ts src/main/daemon/daemon-init-provider-installation.test.ts src/main/daemon/daemon-init-restart-sequence.test.ts src/main/daemon/daemon-init-live-session-preservation.test.ts src/main/daemon/daemon-init-packaged-bundle-staleness.test.ts src/main/daemon/degraded-daemon-pty-provider.test.ts --run (81 passed)
  • pnpm tc:node
  • pnpm exec oxlint on changed files
  • macOS Electron/CDP validation confirmed the existing actionable TCC recovery toast

@greptile-apps

greptile-apps Bot commented Aug 31, 2026

Copy link
Copy Markdown

Greptile Summary

This PR changes macOS daemon replacement preflight so app-path or packaged-bundle skew is classified before resolver health, preserving live sessions while routing new PTYs through a sticky local fallback.

  • Adds a sticky degraded launch mode that cannot be cleared by generic daemon health probes.
  • Keeps existing sessions attached to the preserved daemon while routing fresh macOS PTYs to the current application process.
  • Adds Darwin-forced regression coverage for skew detection, live-session preservation, provider installation, and resolver-unhealthy behavior.

Confidence Score: 5/5

The PR appears safe to merge, with no concrete changed-code-triggered failure identified.

The new mode remains sticky during ordinary health recovery, preserves ownership routing for both daemon and local sessions, and is cleared by the explicit restart path that reconstructs the provider.

Important Files Changed

Filename Overview
src/main/daemon/daemon-replacement-preflight.ts Reorders healthy-daemon classification so launch-path and packaged-bundle skew is handled before macOS resolver health, returning sticky local fallback when live sessions require preservation.
src/main/daemon/daemon-provider-init.ts Installs the degraded provider for the new sticky mode while intentionally omitting its health-based fresh-spawn recovery probe.
src/main/daemon/daemon-out-of-process-launcher.ts Propagates the new sticky fallback mode through preserved daemon handles.
src/main/daemon/daemon-spawner.ts Extends the daemon process handle contract with the sticky degraded routing mode.
src/main/daemon/daemon-init-live-session-preservation.test.ts Adds Darwin-specific coverage proving skewed daemons with live sessions are preserved and resolver health cannot override sticky fallback.
src/main/daemon/daemon-init-packaged-bundle-staleness.test.ts Verifies stale packaged daemons with live sessions enter sticky fallback on macOS.
src/main/daemon/daemon-init-provider-installation.test.ts Verifies generic health recovery cannot clear sticky fresh-spawn routing.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Healthy daemon discovered] --> B[Classify launch identity and bundle age]
    B -->|Current| C[Check macOS resolver health]
    B -->|Skewed| D{Has live sessions or unknown session state?}
    D -->|Yes on macOS| E[Preserve old daemon]
    E --> F[Install sticky degraded provider]
    F --> G[Existing sessions use preserved daemon]
    F --> H[Fresh PTYs use local provider]
    D -->|No| I[Replace daemon]
    C -->|Healthy or unknown| J[Preserve normal daemon routing]
    C -->|Unhealthy with no live sessions| I
    K[Explicit daemon restart] --> L[Install fresh non-degraded provider]
    L --> M[Fresh PTYs return to daemon]
Loading

Reviews (1): Last reviewed commit: "fix(macos): route fresh PTYs away from s..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The daemon lifecycle now supports degraded-new-pty-fallback-sticky. On macOS, launch-identity and stale-bundle checks run before resolver health checks. Live-session daemons in these states use sticky fallback routing. Provider initialization omits health probing for sticky mode. Types, launcher callbacks, and tests now cover the new mode and its routing behavior.

Merge Risk: 🟡 Moderate · up to e1bec

The PR can still route fresh terminals through the current app, but it is not merge-ready until the replacement race and invalid test declarations are fixed. Without those fixes, a newly created session could be terminated during daemon cleanup, validation could fail to load affected tests, and unverifiable daemon identity could leave new terminals using a stale daemon.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description includes the change summary, linked issue, and validation results, but it omits several template sections, including an explicit Visual Proof entry, ELI5, Why, Review, Notes, and check… Complete the required template sections. Add an explicit Visual Proof entry with N/A and a reason if no visual change exists, document the ELI5 and Why sections, complete the Testing and checklist items, and address Review, Notes, AI Disc…
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 8 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main macOS PTY routing fix for skewed daemons.
Linked Issues check ✅ Passed The changes directly address issue [#17696] by preserving live sessions while routing new macOS PTYs through the current app provider, preventing stale daemon TCC attribution and restoring readable wo…
Out of Scope Changes check ✅ Passed The production changes, type updates, and regression tests are focused on macOS daemon version-skew handling and fresh PTY routing. No unrelated changes are identified.
Full details: Description check

Explanation

The description includes the change summary, linked issue, and validation results, but it omits several template sections, including an explicit Visual Proof entry, ELI5, Why, Review, Notes, and checklist status.

Resolution

Complete the required template sections. Add an explicit Visual Proof entry with N/A and a reason if no visual change exists, document the ELI5 and Why sections, complete the Testing and checklist items, and address Review, Notes, AI Disclosure, and Agent skill upstream boundary requirements.

Full details: Linked Issues check

Explanation

The changes directly address issue [#17696] by preserving live sessions while routing new macOS PTYs through the current app provider, preventing stale daemon TCC attribution and restoring readable working-directory behavior.

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

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: 8ff93664-bff5-4544-b1d1-53ab90841428

📥 Commits

Reviewing files that changed from the base of the PR and between 8ac1c6e and e1bec24.

📒 Files selected for processing (8)
  • src/main/daemon/daemon-init-live-session-preservation.test.ts
  • src/main/daemon/daemon-init-mock-types.ts
  • src/main/daemon/daemon-init-packaged-bundle-staleness.test.ts
  • src/main/daemon/daemon-init-provider-installation.test.ts
  • src/main/daemon/daemon-out-of-process-launcher.ts
  • src/main/daemon/daemon-provider-init.ts
  • src/main/daemon/daemon-replacement-preflight.ts
  • src/main/daemon/daemon-spawner.ts

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

const launcher = spawnerInstances[0].launcher as (
socketPath: string,
tokenPath: string
) => Promise<{ mode?: string; shutdown(): Promise<void> }>

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.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Remove the duplicated function-type declarations.

Each duplicate follows a complete ) => Promise<...> declaration. TypeScript cannot parse either test module. Vitest and type checking cannot load these files.

  • src/main/daemon/daemon-init-live-session-preservation.test.ts#L128-L128: remove the repeated ) => Promise<{ mode?: string; shutdown(): Promise<void> }> line.
  • src/main/daemon/daemon-init-packaged-bundle-staleness.test.ts#L62-L62: remove the repeated ) => Promise<{ mode?: 'degraded-new-pty-fallback'; shutdown(): Promise<void> }> line.
📍 Affects 2 files
  • src/main/daemon/daemon-init-live-session-preservation.test.ts#L128-L128 (this comment)
  • src/main/daemon/daemon-init-packaged-bundle-staleness.test.ts#L62-L62

reason: stalePackagedBundle ? 'stale_bundle' : 'different_app_path',
liveSessionCount: 0
}
confirmedReplacement = (await cleanupDaemonForProtocol(runtimeDir, PROTOCOL_VERSION)).cleaned

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.

🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

🔎 Supported by static analysis

🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- applicable repository guidance ---'
find /tmp/coderabbit-repo-knowledge/stablyai-orca-89dc44e4 -type f -name '*.md' -print
printf '%s\n' '--- target file outline ---'
ast-grep outline src/main/daemon/daemon-replacement-preflight.ts
printf '%s\n' '--- target file ---'
cat -n src/main/daemon/daemon-replacement-preflight.ts
printf '%s\n' '--- directly bound cleanup and preservation definitions ---'
rg -n -U -C 8 'cleanupDaemonForProtocol|shouldPreserveDaemonWithLiveSessions' src/main
printf '%s\n' '--- session creation and replacement-preflight callers ---'
rg -n -U -C 6 'createSession|startSession|daemon-replacement-preflight|replacementPreflight|preflight' src/main/daemon src/main

Repository: stablyai/orca

Length of output: 50370


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- target preflight ---'
sed -n '1,150p' src/main/daemon/daemon-replacement-preflight.ts
printf '%s\n' '--- exact cleanup references ---'
rg -n -C 12 'cleanupDaemonForProtocol' src/main/daemon
printf '%s\n' '--- exact live-session references ---'
rg -n -C 12 'shouldPreserveDaemonWithLiveSessions' src/main/daemon
printf '%s\n' '--- daemon server request handling ---'
rg -n -C 10 "'createOrAttach'|createOrAttach|shutdown|killSessions" src/main/daemon/daemon-server.ts src/main/daemon/daemon-host.ts src/main/daemon
printf '%s\n' '--- launcher and preflight callers ---'
rg -n -C 12 'prepareDaemonReplacement|launchDaemon|createOrAttach' src/main/daemon/daemon-out-of-process-launcher.ts src/main/daemon/daemon-spawner.ts src/main/daemon/daemon-server.ts

Repository: stablyai/orca

Length of output: 50370


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- target preflight ---'
cat -n src/main/daemon/daemon-replacement-preflight.ts | sed -n '1,140p'
printf '%s\n' '--- files defining or calling cleanupDaemonForProtocol ---'
rg -l 'cleanupDaemonForProtocol' src/main/daemon --glob '*.ts' --glob '!*.test.ts'
printf '%s\n' '--- files defining or calling shouldPreserveDaemonWithLiveSessions ---'
rg -l 'shouldPreserveDaemonWithLiveSessions' src/main/daemon --glob '*.ts' --glob '!*.test.ts'
printf '%s\n' '--- relevant server/host files ---'
git ls-files 'src/main/daemon/*server*.ts' 'src/main/daemon/*host*.ts' 'src/main/daemon/*launcher*.ts' 'src/main/daemon/*preflight*.ts'

Repository: stablyai/orca

Length of output: 9201


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- cleanup implementation ---'
cat -n src/main/daemon/daemon-protocol-cleanup.ts
printf '%s\n' '--- preservation helper and remaining preflight ---'
cat -n src/main/daemon/daemon-replacement-preflight.ts | sed -n '140,280p'
printf '%s\n' '--- daemon server lifecycle and admission fields ---'
rg -n -C 14 'createOrAttachInFlight|idle-shutdown-pending|route|createOrAttach|shutdownIfIdle|shutdown' src/main/daemon/daemon-server.ts src/main/daemon/daemon-server-lifecycle.ts
printf '%s\n' '--- terminal host creation path ---'
cat -n src/main/daemon/terminal-host-session-create.ts
printf '%s\n' '--- replacement launcher path ---'
cat -n src/main/daemon/daemon-out-of-process-launcher.ts | sed -n '45,180p'

Repository: stablyai/orca

Length of output: 48719


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- terminal admission implementation ---'
cat -n src/main/daemon/daemon-terminal-admission.ts
printf '%s\n' '--- request router implementation ---'
cat -n src/main/daemon/daemon-request-router.ts
printf '%s\n' '--- server construction and request dependencies ---'
cat -n src/main/daemon/daemon-server.ts | sed -n '30,210p'
printf '%s\n' '--- all direct admission calls ---'
rg -n -C 8 'admission\.|new DaemonTerminalAdmission|begin.*Create|end.*Create|inFlight' src/main/daemon --glob '*.ts' --glob '!*.test.ts'

Repository: stablyai/orca

Length of output: 50369


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- session-count probe implementation ---'
rg -n -C 18 'function getAliveDaemonSessionCount|export .*getAliveDaemonSessionCount|getAliveDaemonSessionCount' src/main/daemon/daemon-launch-paths.ts
printf '%s\n' '--- client concurrency and connection handling ---'
rg -n -C 12 'class DaemonClient|async request|onControlRequest|handleRequest|authenticatedPairEstablished' src/main/daemon/client.ts src/main/daemon/daemon-client-connections.ts src/main/daemon/daemon-server.ts
printf '%s\n' '--- replacement launch entry points ---'
rg -n -C 10 'createOutOfProcessLauncher|prepareDaemonReplacement|new DaemonPtyAdapter|ensureConnectedWithin' src/main/daemon --glob '*.ts' --glob '!*.test.ts'
printf '%s\n' '--- daemon-scoped repository guidance and architecture ---'
find /tmp/coderabbit-repo-knowledge/stablyai/orca-89dc44e4 -type f -name '*.md' -print0 |
  xargs -0 grep -Il 'daemon\|terminal\|session' |
  while read -r file; do
    printf '\n--- %s ---\n' "$file"
    sed -n '1,120p' "$file"
  done

Repository: stablyai/orca

Length of output: 50369


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- adapter session-creation caller ---'
rg -n -C 12 "'createOrAttach'|createOrAttach" src/main/daemon/daemon-pty-adapter.ts src/main/daemon/daemon-pty-connection-lifecycle.ts src/main/daemon --glob '*.ts' --glob '!*.test.ts' |
  grep -E 'daemon-pty-adapter|daemon-pty-connection-lifecycle|createOrAttach|request' | head -120
printf '%s\n' '--- host disposal contract ---'
rg -n -C 14 'async dispose|dispose\(\)' src/main/daemon/terminal-host.ts src/main/daemon/terminal-host-session-shutdown.ts
printf '%s\n' '--- applicable guidance files ---'
find /tmp/coderabbit-repo-knowledge/stablyai-orca-89dc44e4 -type f -name '*.md' -print

Repository: stablyai/orca

Length of output: 18765


Serialize session creation before cleanup

After shouldPreserveDaemonWithLiveSessions returns zero, createOrAttach can still pass DaemonTerminalAdmission while the daemon is accepting work. cleanupDaemonForProtocol then sends shutdown with killSessions: true; TerminalHost.dispose() can terminate that newly created session. Add a replacement admission fence or an atomic drain operation before the zero-session check.

@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

  • Skew-before-resolver classification — daemon-replacement-preflight.ts now checks getDaemonLaunchIdentity + isDaemonStaleForCurrentBundle before the macOS resolver probe, so an app-path/bundle-skewed daemon is diagnosed as such rather than as a generic resolver failure.
  • Sticky degraded fallback — a skewed macOS daemon that owns live sessions is preserved with a new degraded-new-pty-fallback-sticky mode that routes fresh PTYs to the local provider and never re-routes them (no probeCurrentDaemonSpawn), so TCC attribution stays on the current app until an explicit restart.
  • Resolver/TCC path unchanged — the identity-match case falls through to the previous resolver → TCC-attribution logic untouched.
  • Regression coverage — darwin-forced assertions pin the sticky mode, the resolver bypass on mismatch, and the no-probe recovery in the provider-installation suite.

The reorder is sound: on non-macOS the resolver probe already returned 'unknown' and identity ran anyway, so the only real differences are (1) mismatched/stale daemons with live sessions now localize fresh PTYs on macOS, and (2) a resolver-unhealthy-but-identity-mismatched daemon now reports different_app_path/stale_bundle instead of unhealthy_resolver — both intentional and reflected by the telemetry-ranking comment. The mode-literal propagation (daemon-spawner → daemon-out-of-process-launcher → preflight → daemon-provider-init) is complete, and the new tests assert exact values rather than loose truthiness, so they genuinely fail on regression.

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

@Jinwoo-H

Jinwoo-H commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Closing after independent review + empirical validation of the failure model.

Why: The fix triggers on version skew (daemon appVersion !== app version), which is true after every macOS update while terminals are open — routing all new terminals to the non-persistent local provider until a daemon restart (which kills live sessions). That's a daily persistence regression for every macOS updater.

Empirical check (released 1.4.194 app, scratch TCC-clean profile, PTYs spawned by its daemon into ~/Documents):

  • version-skewed daemon, bundle intact → full access
  • bundle replaced in place (daily-update sim, new inodes) → full access
  • spawner binary deleted (the exact 'severed' condition) → full access
  • whole bundle deleted → PTY spawn fails cleanly with the existing 'node-pty install is gone, restart Orca' error — a different, already-handled failure

Natural experiment: a production machine currently runs a 5-week / ~35-version stale daemon (1.4.159 under 1.4.194) with fully working terminals.

Conclusion: 'version skew breaks TCC for fresh daemon PTYs' does not reproduce in any variant, so the sticky fallback trades a proven regression for an unconfirmed fix. #17696 needs a reproduced root cause first (candidates: daemon outliving its login session — the existing resolver-health territory — rather than bundle skew).

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.

macOS terminal cwd becomes unreadable when app and daemon versions are mismatched

1 participant