Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(ssh): preserve external runtime ownership #5751

Closedmikeyperes(GitHub profile, opens in a new tab)wants to merge 4 commits intomain fromrefs/pull/5751/headopened View on GitHub

Closes #5749.

What changed

  • Distinguish the SSH launcher's own managed runtime record from an externally owned T3 service.
  • Adopt a healthy external service without first stopping it.
  • Keep external ownership during a transient service restart instead of launching a competing server.
  • Clear server-runtime.json only when the shutting-down process still owns that exact PID/start-time record.
  • Document the persistent-host ownership contract.

Why

Two T3 servers and two relay tunnels sharing one base directory make reconnect behavior nondeterministic: HTTP requests and WebSockets can reach different processes. This keeps the supported persistent service as the single owner.

Verification

  • packages/ssh/src/tunnel.test.ts
  • apps/server/src/serverRuntimeState.test.ts
  • Focused combined reliability suite: 82/82 passing
  • Server typecheck passing
  • Production server bundle build passing

Note

Medium Risk Changes SSH launch and persisted runtime discovery on shutdown, including atomic rename/restore races; wrong behavior could leave no server or duplicate servers on one data dir.

Overview Keeps a single T3 server owner when SSH reconnects to a host that already runs a persistent service, and stops shutdown from wiping a replacement process’s server-runtime.json.

SSH remote launch now treats server-runtime.json as the source of truth for an externally owned runtime: it reconnects without stopping that process, marks SSH state as external, and only tears down a prior managed SSH-launched PID when the default runtime port changed. If external ownership is recorded but the server is not ready, launch fails instead of starting a competing managed server. Competing managed state converges onto the external owner (including PID-reuse edge cases).

Server shutdown persists runtime state on startup and, on release, calls clearPersistedServerRuntimeStateIfOwned with that snapshot so only the matching pid + startedAt record is removed; mismatches are restored via rename/link (with a no-hard-link fallback). Docs note that SSH will not take ownership while an external service is active.

Tests cover ownership clearing, rename races, link fallback, and end-to-end SSH launch script behavior.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 19b6923bf00fdc07d92175a7f16b2ea6ca3e6449. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Preserve external SSH runtime ownership on server shutdown and reconnect

  • Replaces the unconditional clearPersistedServerRuntimeState call with clearPersistedServerRuntimeStateIfOwned, which atomically renames the state file and only deletes it if the current process's pid and startedAt match the captured file.
  • If the state is owned by another process, the original file is restored via hard link or exclusive-write fallback to avoid clobbering a replacement written during a race.
  • The SSH launch script in tunnel.ts now detects externally owned runtimes, reuses them without replacement, avoids killing unrelated processes on port switch, and exits fast with an explanatory message if the external server is not ready.
  • Behavioral Change: servers that did not previously own their runtime state will no longer clear it on shutdown; the SSH launcher will refuse to start a competing server when an external owner is detected.
Macroscope summarized 19b6923.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 2d983572-49d6-4a56-b786-8cb7f70d20de

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      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.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      This PR introduces significant runtime behavior changes to SSH server lifecycle management. The launcher will now refuse to start (exit 1) when detecting an unresponsive external server rather than replacing it, and adds new atomic ownership verification. These workflow-impacting changes warrant human review.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. Effect service conventions review: the new clearPersistedServerRuntimeStateIfOwned helper uses Effect.catchTag in three places. Per the conventions, statically known tagged failures should be handled with Effect.catchTags({ ... }), including for a single tag — the surrounding functions in this same file (clearPersistedServerRuntimeState, readPersistedServerRuntimeState) already use that form.

      Posted via Macroscope — Effect Service Conventions
    5. Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot). Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c24d3c79ab4e2945bed7df7e595416af0dbb37e7. Configure [here](https://www.cursor.com/dashboard/bugbot).
    6. Canonical combined source is now published at https://github.com/mikeyperes/hws-t3code/commit/e383737a30218a77de69474c918f43a0b0fc2b76, with the canonical pickup record at https://github.com/mikeyperes/hws-agent-operations/blob/main/T3-AUTOMATIC-RECOVERY-HANDOFF.md.

      The branch carries the SSH owner-convergence and update-scope fixes and has source-level regression coverage. It is not a supported release artifact; no live deployment has occurred and server 236 remains red (public relay HTTP 502).

Sign in to join this conversation.