Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): macOS service install and restart from inside T3 hand off to launchd #28565

Openjuliusmarmingewants to merge 7 commits intomain fromrefs/pull/28565/headopened View on GitHub

juliusmarmingecommented

Problem

On macOS, t3 service install, t3 service restart and a restarting t3 update run from inside T3 (a T3 terminal or an agent shell, so a descendant of the com.t3tools.t3code.service launchd job) run launchctl bootout --wait on their own job. The bootout kills the caller before launchctl bootstrap, and a booted-out job is not restarted by KeepAlive, so the service stays down. systemd already hands this work to the service manager; launchd had no handoff, and the inside-service check never matched on macOS because the plist sets T3_BOOT_SERVICE_UNIT=com.t3tools.t3code.service.plist while the check compared against t3code.service.

Reproduced on main (ce9e016406) as two failing tests in the darwin harness of apps/server/src/cloud/bootService.test.ts, with the marker the launch agent really sets: an in-service install() and restart each issue launchctl bootout --wait gui/501/com.t3tools.t3code.service from the caller.

Change

  • The inside-service check compares T3_BOOT_SERVICE_UNIT with the detected manager's own unit file name, so the existing macOS marker matches (the plist is unchanged, so installed services stay current).
  • BootServiceManager.handoff now has install and restart entries for both managers; install() and restart() no longer branch on kind. The systemd handoff (transient unit for install, restart --no-block) is moved there unchanged.
  • On launchd, a handoff writes a one-shot job plist (its own unique label com.t3tools.t3code.service.handoff.<id>, RunAtLoad, no KeepAlive, PATH and HOME only, no service marker, output appended to boot-service.log) under <baseDir>/runtime, bootstraps it into gui/<uid>, and removes the file. The job runs outside the service's job, so stopping the service does not kill it. Unique labels mean a second in-service call never unloads a handoff that is still running.
    • install: the job runs the target runtime's service install --base-dir <baseDir> [--allow-downgrade], which takes the normal stop-first path; the caller returns handedOff.
    • restart: the job runs launchd's own stop and start steps through /bin/sh (not kickstart -k, which neither reloads a rewritten plist nor spares the caller, and not the installed plist's program, which may be node on old npm installs); the caller returns queued.
  • t3 update's handed-off message says "from outside the service" instead of "from a separate systemd unit".

Scope and approval

Fixes pingdotgg/t3code#28552

Triaged via the automated issue flow.

Verification

  • vp test run apps/server/src/cloud/bootService.test.ts apps/server/src/cli/update.test.ts apps/server/src/cli/service.test.ts on the branch: 59 passed.
  • The same bootService.test.ts against main's bootService.ts (ce9e016406): 2 failed, 35 passed:
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯
 FAIL  |t3| src/cloud/bootService.test.ts > boot service install > hands an in-service install to launchd without booting out the running agent
AssertionError: expected [ Array(1) ] to deeply equal []
- Expected
+ Received
- []
+ [
+   "launchctl bootout --wait gui/501/com.t3tools.t3code.service",
+ ]
 FAIL  |t3| src/cloud/bootService.test.ts > boot service install > queues an in-service restart on macOS without booting out the running agent
AssertionError: expected [ …(2) ] to deeply equal []
- Expected
+ Received
- []
+ [
+   "launchctl bootout --wait gui/501/com.t3tools.t3code.service",
  • New and extended tests: in-service install on macOS hands off without booting out the service, leaves state and plist untouched, and the job runs <entry> service install --base-dir <baseDir> without the marker; in-service restart is queued, two restarts load two distinct jobs with no bootout, the job script stops and bootstraps the launch agent, carries no marker or KeepAlive, and leaves no plist behind. Existing systemd handoff tests pass unchanged.
  • vp run check:changed: format, lint, knip, typecheck (3 packages) and tests pass.
  • No visible surface, so no captures.
  • cannot-verify macos: nothing here ran on a Mac. Not checked natively: launchd bootstrap of the handoff plist, the job's environment and survival across the service bootout, and whether macOS 13+ Background Task Management shows a notice per handoff job. On macOS 15, where bootout --wait is a usage error (pingdotgg/t3code#11995), the handed-off install or restart still fails inside the job as it does from a terminal, leaving the old service running; that is fixed separately.

Model and harness: Claude Opus 5.5 (Claude Code) as lead, implementer and test writer, with GPT-6.1 Sol (Codex) and Claude Opus 5.5 reviewers, run through T3 Code.

🤖 Generated with Claude Code

    1. Approvability

      Verdict: Approved at b5bb533

      Macroscope's review found this PR approvable — macOS service install and restart now run through a one-shot launchd job so stopping the service does not kill the operation. The change is limited to requested per-user service operations, with existing install safeguards and terminal-based recovery preserved.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    2. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire5.0 KiB5.0 KiB+29 B (+0.6%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB+29 B (+2.3%)2.0 KiB✅
      CodexLive turn WebSocket decoded21.5 KiB21.6 KiB+41 B (+0.2%)24.4 KiB✅
      CodexLive turn messages12+1 (+100.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.3 KiB1.3 KiB0 B (0.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.9 KiB21.9 KiB0 B (0.0%)24.4 KiB✅
      ClaudeLive turn messages220 (0.0%)8✅

      Baseline: 37d7615 · PR result: b5bb533 · Source CI: success

      Scenario and decoded snapshot size

      10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

      • Codex decoded thread snapshot: 108.5 KiB
      • Claude decoded thread snapshot: 108.8 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    3. Review in Change Stack →

      No actionable comments were generated in the recent review.

      Recent review info
      Run configuration
      Commits

      Reviewing files that changed from the base of the PR and between 37d7615a91d880c3776d575e1152b9c955104de2 and b5bb5330fc6a16c7ec5ee5b6c3eed1f47b5aba46.

      Files selected for processing: 3
      • apps/server/src/cli/update.ts
      • apps/server/src/cloud/bootService.test.ts
      • apps/server/src/cloud/bootService.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

      Walkthrough

      In-service installation and restart now use manager-specific handoffs. On macOS, launchd handoffs use uniquely labeled one-shot jobs. Service detection compares the configured marker with the detected manager’s unit basename.

      Changes: Service manager handoffs

      Layer / File(s)Summary
      Manager handoff contracts and implementations
      apps/server/src/cloud/bootService.ts
      BootServiceManager.handoff now defines install and restart callbacks. Systemd and launchd provide manager-specific handoff behavior. Launchd handoffs use one-shot plist jobs with command arguments, environment values, and log paths.
      In-service routing and handoff execution
      apps/server/src/cloud/bootService.ts, apps/server/src/cloud/bootService.test.ts, apps/server/src/cli/update.ts
      In-service detection compares the marker with the detected manager’s unit basename. Installation and restart use the manager’s handoff callbacks. The tests check macOS handoff behavior, and the update status message now says the switch runs “from outside the service.”

      Estimated code review effort: 3 (Moderate) | ~25 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Possibly related issues

      • pingdotgg/t3code issue 11995: The launchd stop sequence still uses launchctl bootout --wait, which the issue reports can fail on macOS 15.
      • pingdotgg/t3code issue 12627: Installation uses the target runtime CLI, intersecting with the issue’s concern about the CLI protocol used to write service state.
      • pingdotgg/t3code issue 24755: Launchd handoff output is sent to the service log path discussed in the issue.
      • pingdotgg/t3code issue 24910: The handoff plist uses the service log path under the T3 base directory, the path policy discussed in the issue.

      Suggested reviewers: rookmate, macroscopeapp, maria-rcks

      Merge Risk: Minimal · up to b5bb5

      No actionable regression is established for this change. Native macOS handoff behavior has not been verified, but that uncertainty alone does not block merging.

      Pre-merge checks: Passed 4
      Check nameStatusExplanation
      Title checkPassedThe title clearly and concisely describes the main change: macOS service installs and restarts invoked inside T3 now hand off to launchd.
      Description checkPassedThe description includes complete Problem, Change, Scope and approval, and Verification sections. It explains the failure, implementation, linked issue, test results, unverified native macOS behavior,…
      Linked Issues checkPassedIssue #28552 has coding requirements. The change detects the launchd marker from the detected unit filename. It adds install and restart handoffs for launchd. Each launchd handoff uses a unique one-sh…
      Out of Scope Changes checkPassedThe changes stay within issue #28552. The production changes implement manager-owned install and restart handoffs, marker detection, launchd job creation and cleanup, and the related update message. T…
      Full details: Description check

      Explanation

      The description includes complete Problem, Change, Scope and approval, and Verification sections. It explains the failure, implementation, linked issue, test results, unverified native macOS behavior, and the separate macOS 15 limitation.

      Full details: Linked Issues check

      Explanation

      Issue #28552 has coding requirements. The change detects the launchd marker from the detected unit filename. It adds install and restart handoffs for launchd. Each launchd handoff uses a unique one-shot job with no KeepAlive, no service marker, and output appended to boot-service.log. Install runs the target CLI's service install; restart runs launchd stop and start steps outside the service job. The caller returns handedOff or queued. The existing update restart path uses the updated service restart handoff, and its message no longer claims a systemd-only handoff. The added tests cover marker detection, install and restart handoffs, unique jobs, preserved service files, job contents, and cleanup. The reported verification also passes the relevant tests and checks.

      Full details: Out of Scope Changes check

      Explanation

      The changes stay within issue #28552. The production changes implement manager-owned install and restart handoffs, marker detection, launchd job creation and cleanup, and the related update message. The test harness and regression tests support these requirements. The systemd code preserves the existing handoff behavior while moving it into the shared handoff interface. No unrelated product behavior or unrelated files are introduced.

      Finishing Touches
      Generate docstrings
      • Commit to this branch
      • Create a new PR
      Generate unit tests (beta)
      • Commit to this branch
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
    4. juliusmarminge added labelsbugfix

    5. juliusmarminge added labelspr:gate-failed

    6. Independent macOS launchd lifecycle review — CHANGES_REQUESTED

      Reviewed exact HEAD b5bb5330fc6a16c7ec5ee5b6c3eed1f47b5aba46 against recorded base 37d7615a91d880c3776d575e1152b9c955104de2. Reviewer: GPT-6 (Wladefant, separate from PR author). Implementer-model provenance is not fully declared; the initial RED reproduction commit bc171f1 lists Claude Opus 5.5 as co-author. Applied the temporary #1899 review-family exception; no self-review.

      P2 — concurrent in-service handoffs can race the same launchd service. At apps/server/src/cloud/bootService.ts:427-448, each call creates a distinct RunAtLoad one-shot job with a unique label. At lines 472-475, each restart job independently executes the same bootout --wait → enable → bootstrap sequence. The test deliberately calls inside.restart twice and asserts two distinct jobs, but it only inspects the generated plists; it never executes overlapping jobs or proves serialization. There is no per-baseDir/service lock or single-flight queue around runHandoff, install, or restart in the inspected path.

      Two RunAtLoad jobs can therefore interleave their bootout/bootstrap steps against the same label. One can boot out the other's newly loaded service, or a later required bootstrap can fail because the label is already loaded; shellScriptOf chains required steps with &&, so that failure stops the remainder. This can leave the requested service restart incomplete or the service down. This is a source-derived concurrency risk, not a reproduced launchd host incident.

      Required correction: serialize handoff jobs for the same user + T3 home (including install-vs-restart), or route them through one durable coordinator that coalesces/reconciles requests without allowing two stop/bootstrap sequences to overlap. Add a deterministic regression that releases two queued restart jobs concurrently and an install/restart overlap; assert one-at-a-time execution, no second bootout after the first successful bootstrap, and a terminal receipt that distinguishes queued from completed/failed.

      Separate integration/CI gate: current T3 main is 09a54f01bc0ae83865347a965ced50f8c8ec2953; this PR's recorded base 37d7615a91d880c3776d575e1152b9c955104de2 is 858 commits behind main, and GitHub reports mergeable:false. The exact-head all-event inventory contains only the 1 Nov run set: CI #41288396404 succeeded on this old candidate, while Test Web, Release Smoke, Rust, Mobile Native Static Analysis and preview jobs were skipped. No current-main composed candidate or current-main CI is established. Preserve the separate #29528 kickstart composition gate already recorded in #6716983042.

      No macOS host/service/session was touched. This review does not authorize installation, restart, credential access or landing.

    7. tanstack-source added labelsbug

    8. Confirming this on a released build: on macOS, t3 service restart run by Codex inside T3 stopped the service and it never came back; the launchd system log shows the stop chain launchctl <- t3 CLI <- codex <- T3 server.

      We carry the same fix (handoff to a one-shot launchd job) in a fork, and added a macOS-only test against the real launchd: an isolated label and HOME, a process of the service runs the real t3 service restart, and the test waits on a FIFO until the restarted service writes its new pid, with the caller dead. It passes in ~2.4s on macOS 15.1 and cleans up after itself. Happy to share it if useful for this PR.

Sign in to join this conversation.