fix(host-service): recheck trustd when adopting a pty-daemon
What & why
After a macOS logout and login, the pty-daemon from the old session keeps running (reparented to launchd), and the next host-service adopts it. Every terminal it spawns, new tabs included, stays in the ended login session: id -un prints a uid, ssh fails with "No user exists", Go TLS fails with x509: OSStatus -26276, and the keychain is unreachable. Quitting the app does not help, because the same daemon is adopted again. Only Settings → Restart daemon fixes it.
The daemon ran probeTrustdHealthy() once, after it bound its socket, and returned that value in every hello-ack. A daemon that started in a healthy session reported trustdHealthy: true for life, so the heal in DaemonSupervisor.start() (adopted.trustdHealthy === false && hostTrustdHealthy()) never fired after the session ended.
Now a hello can carry refreshTrustd: true. Server then re-runs the probe before it acks (one probe at a time, waiting at most 1 s, below the supervisor's 1.5 s hello attempt). If the probe is slower than that, the ack leaves trustdHealthy out. The existing late-report check in refreshReachability then picks up the result on its next poll. Only the adoption probes in tryAdopt and tryAdoptFromSocket send the flag. The 5 s health poll does not, so a running daemon is still never killed mid-life. The decision stays with the existing heal, still gated on host-service's own probe being healthy. Normal hellos stay synchronous. An older daemon ignores the flag and behaves as before until its version-bump handoff. An older host-service never sends it.
Rejected: re-probing on every hello (the health poll would spawn security every 5 s and delay terminal handshakes); a periodic re-probe (stale right after login, and it invites mid-life kills); prompting before the restart (adoption already restarts silently since #6302; a banner is a separate change).
Fixes #8029
How I tested it
Reproduced first: a new DaemonSupervisor.node-test.ts case starts a real daemon whose PATH-shimmed security succeeds at startup and fails afterwards, then adopts it from a fresh supervisor. On main it fails ("must be replaced, not adopted"); with the fix it passes, as do the other six trustd heal tests.
Also added: Server.trustd.test.ts (a flagged hello re-probes and reports the new value while a plain hello does not probe; a slow probe acks without a value, and concurrent hellos share one probe), a tryAdopt unit test (adoption reads the fresh value), and a guard that the health poll never kills a known-healthy daemon over a later degraded report. Reverting the fix makes the first three fail. The guard passes on main and fails if the === undefined check is removed.
Ran bun run test in pty-daemon, host-service src/daemon and src/terminal bun tests, test:integration:daemon, bun run typecheck and bun run lint. In the integration run, "auto-update defers when live sessions exist" and the DaemonClient "color queries complete on a real PTY…" fail, and the "late report" test leaves a respawned daemon that keeps the process from exiting. All three happen on clean main too.
Tested on macOS (Apple Silicon). I did not do a real logout/login; the fix relies on security verify-cert exiting non-zero in an ended session, as the report shows. It reaches users once a release bumps the daemon (--daemon).
Checklist
- PR title follows conventional commits (
type(scope): subject) -
bun run lintandbun run typecheckpass (CI fails on lint warnings too) - "Allow edits from maintainers" is checked on fork PRs
- I have signed the CLA (the bot asks on your first PR)
Summary by cubic
Fixes terminals staying in an ended macOS login session by having host-service ask a pty-daemon to re-probe trustd when adopting it. Previously a daemon probed once at startup and reported that value forever, so a daemon reparented to launchd after logout/login was adopted as healthy and every terminal it spawned failed (ssh, keychain, TLS). Now the adoption hello requests a fresh probe, and a degraded result triggers the existing heal that replaces the daemon.
Details
- Only adoption hellos request the refresh; the health poll does not, so a running daemon is never killed mid-life.
- A slow probe acks without a trustd value, and the result arrives via the existing late-report path.
- Older daemons ignore the flag and behave as before; older host-services never send it.
Fixes #8029
Summary by CodeRabbit
- Bug Fixes
- Improved desktop background service recovery when system trust health changes, helping ensure a fresh service is used when an existing one is no longer healthy.
- Health checks now refresh when reconnecting to a running service. If a check takes too long, the service can continue responding and report the result later.
- Prevented a later degraded health report from incorrectly stopping a service already recorded as healthy.