fix(host-service): heal trustd-degraded pty-daemons so gh works in terminals (x509 -26276)

Merged
KI
4mo ago
+900-9

Links

  • Fixes #6127
  • Supersedes #5450 (same fix, revived + completed; that PR predates the daemon-loop blocking ratchet and misses the desktop entry)

Summary

  • The pty-daemon self-probes macOS trustd reachability (security verify-cert) at startup and reports it in its hello-ack; the supervisor kills a self-reported-degraded adopted daemon and respawns it from host-service's healthy Mach bootstrap context.
  • Fixes the field failure where every terminal in the app gets gh: ... tls: failed to verify certificate: x509: OSStatus -26276 (and headless Chromium bootstrap_check_in error 141) until the user manually restarts the daemon.

Why / Context

A child inherits its parent's Mach bootstrap port. The pty-daemon deliberately outlives host-service (session persistence via adoption), so a daemon born from a degraded bootstrap — updater relaunch, dead session port after logout/login — permanently breaks Secure-Transport TLS (gh, Chromium) in every shell it spawns, and adoption preserves the broken daemon forever. curl/node don't use Secure Transport, so nothing else notices. Only a full daemon respawn from a healthy context (what Settings → Restart daemon does manually) heals it; this PR automates exactly that, gated so it never fires spuriously.

How It Works

  • packages/pty-daemon/src/trustd-probe.ts: async probeTrustdHealthy() — verifies a known-good cert from /etc/ssl/cert.pem via security verify-cert (the platform verifier). Only a clean non-zero exit ⇒ degraded; spawn errors/timeouts/signals ⇒ healthy (fail-open, so a flaky probe can never cause a session-destroying respawn). Async (execFile + fs/promises) so it can't stall the daemon loop — matters during fd-handoff, where the successor probes while live sessions stream.
  • Daemon runs the probe after binding (both fresh-spawn and handoff paths, in both the package entry and the desktop entry shim) and reports trustdHealthy in the hello-ack.
  • DaemonSupervisor.start(): after adoption, if the daemon self-reports trustdHealthy === false and host-service's own probe is healthy → kill the adopted daemon + fall through to a fresh spawn (pty_daemon_trustd_degraded_respawn). If the host is also degraded, adopt untouched (a respawn couldn't do better; next healthy app launch heals). trustdHealthy absent (pre-probe daemon) ⇒ unknown ⇒ adopt untouched.
  • Migration: pty-daemon 0.2.7 → 0.3.0. Existing daemons auto-update via the normal fd-handoff (sessions preserved); a degraded one's successor re-probes at startup, self-reports, and gets healed on the next adoption. No forced mass session loss.

Manual QA (all verified end-to-end over CDP against the dev desktop, before/after)

  • Before (main @ 871b02b43): staged a genuinely trustd-denied daemon (sandbox-exec denying com.apple.trustd.agent); host-service adopted it silently; gh auth login -h github.com -p https -w in a real workspace terminal failed with the exact reported error (screenshot captured).
  • After (this branch): same staging → adoption logs pty_daemon_trustd_degraded_respawn, degraded daemon replaced, same command reaches the one-time-code screen; gh api returns normally in new terminals.
  • No false heal: healthy daemon re-adopted across a host-service restart — same daemon pid, live session survived with same shell pid.
  • Unknown ≠ degraded: real pre-fix 0.2.7 daemon (no trustdHealthy in hello-ack), genuinely degraded, with a live session → adopted untouched, updatePending armed.
  • Full migration chain: degraded 0.2.7 → fd-handoff auto-update (session survived two handoffs) → 0.3.0 successor self-reported trustdHealthy:false → healed on next adoption → gh working.
  • Degraded daemon's sessions are killed by the heal (by design — they're already TLS-broken; same trade-off as the manual Restart daemon button).

Testing

  • bun run typecheck, bun run lint
  • pty-daemon: 79 unit (incl. probe suite) + 56 integration
  • host-service: unit suite + test:integration:daemon — includes new "trustd-degraded daemon heal" tests using real daemons made genuinely degraded via a PATH-shimmed security (darwin): degraded → kill+respawn; degraded host → adopt untouched (via injected host probe); pre-probe unknown → adopt untouched.
  • Pre-existing on main, unrelated: auto-update defers when live sessions exist fails locally at the base commit too.

Design Decisions

  • Heal at adoption only, never mid-session: killing a live daemon under a user destroys running shells without consent; adoption points (app launch, hs restart, Restart button, post-update) are where a respawn is already expected.
  • Fail-open probe: an inconclusive probe must never kill sessions; only security verify-cert cleanly exiting non-zero counts as degraded. Verified on macOS 26 that the probe still detects the condition (the trustd Mach service is now com.apple.trustd.agent; the probe never hardcoded a name, so it's unaffected).
  • hostTrustdProbe option on DaemonSupervisorOptions: DI seam for the degraded-host branch, which cannot be staged in tests without breaking the test process's own bootstrap (same precedent as the existing autoUpdate test seam).
  • Desktop entry shim probed separately: apps/desktop/src/main/pty-daemon/index.ts is a hand-mirrored copy of the package's main.ts; without that commit the shipped app never probes at all (gap in #5450).

Known Limitations

  • The probe runs once at daemon startup. A daemon that degrades mid-life (logout/login while running) keeps reporting its startup value and only heals via the Restart button or the next version-bump handoff.

Follow-ups

  • Surface trustdHealthy through daemon.getHealth + inline terminal banner with a Restart action (actionable-error UX for the live-session window).
  • Spike: tmux-style per-user-namespace reattach at daemon startup (bootstrap_get_root → bootstrap_look_up_per_user → task_set_bootstrap_port) — verified the sequence works from a healthy process on macOS 26 and provably cannot rescue an already-dead port, so it's prevention that composes with (not replaces) this heal. Needs native code + a logout-survival test.
  • Prefer fresh spawn over adoption when the adopted daemon holds zero sessions (free bootstrap freshness when there's nothing to preserve).

Rollback

Revert the branch. The daemon version bump is forward-compatible: 0.3.0 daemons under a reverted (pre-probe) supervisor just have their extra hello-ack field ignored.


Summary by cubic

Automatically heals macOS terminals that hit gh TLS failures (x509 OSStatus -26276) by detecting trustd‑degraded @superset/pty-daemon instances and respawning them from a healthy host-service. Also heals if the daemon reports degradation shortly after startup during the liveness poll.

  • Bug Fixes

    • @superset/pty-daemon probes com.apple.trustd via security verify-cert after socket bind and reports trustdHealthy in the hello‑ack (desktop entry does the same); daemon version bumped to 0.3.0.
    • host-service: on adoption, if the daemon reports trustdHealthy:false and the host probe is healthy, kill and respawn; if the host is degraded or the value is missing (pre‑probe/early bind), adopt unchanged.
    • Late heal: the liveness poll fills in trustdHealthy if it was unknown and applies the same gated respawn when it later reports false; it re-checks instance identity after awaiting the host probe to avoid unlinking a concurrent restart; known values are never overwritten, so long‑running sessions aren’t killed mid‑life.
    • Robustness: wire signal handlers before the probe so SIGTERM still drains PTY teardown; resolve the host trustd probe before spawning to avoid a spawn/registration race; if hello‑ack daemonPid disagrees with the manifest pid, drop the manifest and adopt from the socket (reusing the successful probe to avoid a demoting re‑probe); inconclusive trustd probes now warn but still fail open.
    • Crash‑relaunch chain: a degraded host launch adopts a degraded daemon without thrash; the next healthy launch heals the surviving degraded daemon automatically.
  • Migration

    • No manual steps. Existing daemons auto‑update via fd‑handoff; if a degraded daemon is healed, its sessions are terminated.

Summary by CodeRabbit

  • New Features
    • Added macOS certificate-service health checks during PTY daemon startup and handoff.
    • Daemons now report certificate-service health during readiness checks.
    • Added automatic recovery for daemons with unhealthy certificate services.
  • Bug Fixes
    • Improved daemon adoption and handoff health tracking.
    • Preserved compatibility with older daemons that do not report health status.
  • Tests
    • Added coverage for successful, failed, timed-out, and unsupported-platform health checks.