Skip to content

fix(daemon): prevent PAM rejection restart cascades - #11911

Merged
nwparker merged 2 commits into
mainfrom
nwparker/bug-login-session-watch-pam-rejection-cascade-ki
Aug 1, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/bug-login-session-watch-pam-rejection-cascade-ki

Conversation

@nwparker

@nwparker nwparker commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

The daemon used PAM and DNS checks like two smoke alarms. After a Mac wakes, both alarms can briefly report trouble even though the login session is still alive. Orca treated three reports over only 20 seconds as proof the session was dead, shut the daemon down, restarted it, and could repeat the same mistake.

This keeps the quick confirmation probes, but they can no longer shut the daemon down unless failures persist across a full, uninterrupted two-minute observation window. Client reconnects and PTY exits cannot shorten that backoff. A sleep/App Nap timer gap restarts the evidence window instead of making the evidence look older. If PAM recovers, the daemon stays alive; if the login session is genuinely dead and DNS is still explicitly unhealthy, the existing cold-recovery path still converges.

Closes #11749.

Root cause

  • Once armed by any accepted PAM probe, MacosLoginSessionDeathWatch retired after three conclusive rejections at 10-second intervals.
  • The resolver check could also report unhealthy during sleep/wake recovery, so the two signals were not sufficient over that short window.
  • onRetire then crash-exited the daemon; a replacement could encounter the same transient state and repeat the renderer restart cascade.
  • PTY-exit/client activity could pull scheduled probes earlier, and elapsed wall time could jump during sleep, so increasing one timer alone would not create a reliable safety boundary.

Fix

  • Track the first conclusive rejection and require at least 120 seconds of uninterrupted rejection evidence before resolver state gains retirement authority.
  • Preserve the rejection backoff deadline against client-hello and PTY-exit triggers.
  • Reset rejection evidence on a conclusive acceptance or a sleep/App Nap-sized scheduled-timer gap.
  • Hold the rejection count at the confirmation threshold while waiting, preserving the existing retirement telemetry contract.
  • Add rejection-window reset/deferred diagnostics and an experimental reliability gate.
  • Keep the compressed E2E timing seam proportional with a two-second observation window.

Reproduction and proof

The deterministic regression reproduces the field-shaped sequence: accepted PAM probe, three conclusive rejections over 20 seconds, unhealthy resolver, then recovery. Before this fix that sequence reached onRetire; now it performs zero resolver reads and zero retirements until the 120-second boundary, where acceptance preserves the daemon.

A second regression advances wall time by one hour without executing the scheduled timer, simulating sleep/App Nap. It proves that the overdue callback resets the rejection window, collects fresh evidence after wake, and recovers without retirement. The sustained-death test separately proves that uninterrupted rejections plus an explicitly unhealthy resolver still retire exactly once.

Freeze and performance analysis

  • No synchronous wait, renderer work, provider fanout, or polling loop was added.
  • The watcher retains one unref'd timer and at most one async probe; concurrent triggers are coalesced and shutdown aborts in-flight work.
  • Live-session recovery is safer. A genuinely stale daemon may take at least two uninterrupted minutes longer to retire, which is deliberate recovery latency rather than a UI/terminal freeze.
  • macOS GUI local-daemon only; SSH/headless, relay, Linux, Windows, WSL, mobile, folder workspace, and source-control provider paths are unchanged.

Reliability contract

Verification

  • pnpm exec vitest run --config config/vitest.config.ts src/main/daemon/macos-login-session-death-watch.test.ts src/main/daemon/daemon-entry.test.ts src/main/providers/macos-tcc-login-shell.test.ts src/main/providers/macos-login-session-pty-probe.test.ts src/main/network/macos-system-resolver-health.test.ts --reporter=dot — 80 passed
  • pnpm typecheck — passed
  • pnpm lint — passed, including reliability manifest, max-lines ratchet, bundled skill, and localization checks
  • oxfmt --check and git diff --check — passed

Production readiness scan

Verdict: PASS for branch span edb5607e28fb587be44079eb4f77efe90b3841ff..f722eaa61909d59585117ea1d2f1c54d4c7a88b2.

  • P0: none.
  • P1: none.
  • P2: none.
  • Security/supply chain: mandatory whitespace check passed and security grep returned zero hits. No dependency, lockfile, package-script, workflow, binary, signing, updater, or network-sink changes.
  • Backward compatibility/data loss: no schema, persisted state, IPC/RPC/protocol, mobile, or user-owned data changes. The fix prevents the destructive false-retirement path.
  • Release/packaging: no release or packaging surfaces changed.

Residual gaps

  • A real dead macOS GUI login session cannot be fabricated without ending the runner login session; sustained-death coverage uses deterministic PAM/resolver oracles.
  • The two-minute production window and one-hour suspension are fake-clock tested rather than exercised through a real sleep/wake cycle.
  • Multiple stale daemons remain independently throttled; there is no cross-process aggregate coordinator.

@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The macOS login-session death watch now requires a minimum rejection span before retirement. It tracks the first rejection, defers early probes, preserves pending schedules, and resets tracking after successful probes or delayed timer gaps. The daemon configures a 2-second minimum span. Tests use a 120-second window and cover sustained rejection, recovery, resolver suppression, cancellation, and deferred probes. An experimental reliability gate documents the feature and its validation criteria.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address issue #11749 by preventing retirement after short PAM rejection bursts while preserving recovery for genuine session death.
Out of Scope Changes check ✅ Passed The configuration, daemon logic, tests, diagnostics, and reliability gate are directly related to preventing PAM rejection restart cascades.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly and concisely describes the main fix: preventing daemon restart cascades caused by PAM rejection handling.
Description check ✅ Passed The description gives detailed cause, fix, testing, performance, security, compatibility, and residual-risk information, despite not using all template headings.
✨ 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.

@greptile-apps

greptile-apps Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Greptile Summary

This PR hardens MacosLoginSessionDeathWatch to require a 120-second sustained observation window before PAM rejections can trigger daemon retirement, preventing false restarts after sleep/wake recovery bursts where PAM and DNS transiently report failure.

  • Core state-machine change: firstRejectionAtMs tracks the first rejection timestamp; retirement is deferred until clock.now() - firstRejectionAtMs >= minimumRejectionSpanMs (120 s in production, 2 s in the E2E seam). Client hellos and PTY exits cannot pull a scheduled boundary probe earlier once consecutive rejections are in flight.
  • Sleep/wake safeguard: A scheduledAtMs parameter is threaded through timer callbacks so runProbe can detect when a timer fires much later than scheduled (timer gap > minProbeGapMs × 3), resetting the rejection counter rather than counting sleep time as rejection evidence.
  • New reliability gate: An experimental entry in config/reliability-gates.jsonc documents the invariant, oracle assertions, promotion criteria, and known gaps, paired with 19 deterministic fake-clock tests.

Confidence Score: 5/5

Safe to merge — the change tightens the retirement predicate and adds deterministic coverage; no new code paths can accelerate daemon shutdown.

The fix is strictly conservative: it adds a time-floor check before retirement and blocks external triggers from shortening the boundary probe. The deferred path sets consecutiveRejections to REQUIRED-1 and schedules a boundary probe at exactly firstRejectionAtMs + minimumRejectionSpanMs. Timer-gap detection resets the window correctly when sleep/App Nap fires a timer late. All external activity guards consistently check consecutiveRejections > 0 before allowing any probe that could shorten the backoff. The existing convergence path for genuine session death is preserved.

Files Needing Attention: No files require special attention.

Important Files Changed

Filename Overview
src/main/daemon/macos-login-session-death-watch.ts Core state-machine change; adds minimumRejectionSpanMs guard, firstRejectionAtMs tracking, timer-gap reset, and scheduleProbeNoLaterThan guard. Logic is correct and all external-trigger guards are consistent.
src/main/daemon/macos-login-session-death-watch.test.ts Comprehensive fake-clock test suite covering wake recovery, timer-gap rebaselining, sustained death, resolver suppression, event preemption, and shutdown cancellation.
src/main/daemon/daemon-entry.ts Adds minimumRejectionSpanMs: 2_000 to the E2E timing seam, proportional to the 2 s periodicProbeMs override. No production-path logic changed.
config/reliability-gates.jsonc New experimental gate documents the invariant, oracle, test commands, evidence run, performance budget, and promotion/demotion criteria.

Reviews (2): Last reviewed commit: "fix(daemon): rebaseline PAM evidence aft..." | Re-trigger Greptile

@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/daemon/macos-login-session-death-watch.ts (1)

105-105: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Align the e2e death-watch timing with the 120-second retirement contract.

The e2e block overrides timing with minimumRejectionSpanMs: 2_000, while the default retirement contract is MINIMUM_REJECTION_SPAN_MS = PERIODIC_PROBE_MS = 120_000. If the e2e probe times are intentionally faster, either set minimumRejectionSpanMs to 120_000 or include fast-recheck logic in this same override.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b8e6757e-688f-49eb-a6a6-67de3a573e5f

📥 Commits

Reviewing files that changed from the base of the PR and between ce67f3c and f722eaa.

📒 Files selected for processing (3)
  • config/reliability-gates.jsonc
  • src/main/daemon/macos-login-session-death-watch.test.ts
  • src/main/daemon/macos-login-session-death-watch.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • config/reliability-gates.jsonc
  • src/main/daemon/macos-login-session-death-watch.test.ts

@nwparker

nwparker commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

CodeRabbit timing note: no change needed. The E2E-only seam already scales the full cadence together (periodicProbeMs: 2_000, rejectionRecheckMs: 500, minProbeGapMs: 100, and minimumRejectionSpanMs: 2_000). The production defaults remain an uninterrupted 120-second observation window with 10-second rejection rechecks.

@nwparker
nwparker merged commit 3a70078 into main Aug 1, 2026
49 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
* fix(daemon): back off transient PAM rejection retirement

* fix(daemon): rebaseline PAM evidence after sleep
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.

[Bug]: login-session-watch PAM rejection cascade kills daemon and loops — renderer crash without user action

1 participant