fix(desktop): attach to a running local server when the state directory is owned #30580
ClosedCubicMaldo(GitHub profile, opens in a new tab)wants to merge 5 commits into
main fromrefs/pull/30580/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -1141,3 +1167,12 @@11411167 });11421168 });114311691170+ const attachExternal = Effect.fn("desktop.backendInstance.attachExternal")(function* (1171+ config: DesktopBackendStartConfig,1172+ pid: Option.Option<number> = Option.none(),1173+ ) {1174+ yield* cancelRestart;1175+ yield* SubscriptionRef.update(state, (latest) => ({1176+ ...latest,1177+ desiredRunning: true,1178+ ready: true,@@ -0,0 +98,21 @@98+ });99+ return false;100+ }101+102+ if (state.desktopBootstrapSecret === undefined && state.desktopBootstrapToken === undefined) {103+ yield* logWarning(104+ "server-runtime.json has no desktop bootstrap credentials; auto-attach skipped",105+ { runtimeStatePath, pid: state.pid },106+ );107+ return false;108+ }109+110+ if (!isProcessAlive(state.pid)) {111+ yield* logWarning("Server recorded in server-runtime.json is dead; auto-attach skipped", {112+ pid: state.pid,113+ origin: state.origin,114+ });115+ return false;116+ }117+118+ let ready = yield* probeServerReady(origin);@@ -173,10 +180,6 @@173180 SubscriptionRef.set(primaryBackendStartup, { _tag: "Failing", ...failure }),174181 onShutdown: () => desktopWindow.handleBackendNotReady,175182 onPreflightFailed: handlePrimaryPreflightFailure,176- onStateDirOwned: () =>177- electronDialog178- .showErrorBox(179- "This T3 home is unavailable",180- "Another T3 Code server, such as the background service, is using this data directory, or an older background service is set up for it. Stop that server or try `t3 update`. If the older launcher cannot update, run `t3 service uninstall` and optionally reinstall with `t3 service install` from a current CLI. Use `npx t3@latest` if `t3` is missing or outdated, and pass `--base-dir` with the service's T3 home to each command. Then reopen T3 Code. You can also set a separate T3CODE_HOME and pair with the running server.",181- )182- .pipe(Effect.andThen(electronApp.quit)),183+ onStateDirOwned: (): Effect.Effect<void> =>184+ DesktopAutoAttach.tryAutoAttachToRunningServer({185+ stateDir: environment.stateDir,@@ -811,3 +812,5 @@811812812813 return DesktopBackendConfiguration.of({813814 currentBootstrapToken,815+ setBootstrapSecret: Effect.fn("DesktopBackendConfiguration.setBootstrapSecret")(816+ (secret: string) => SynchronizedRef.set(secretRef, Option.some(secret)),
- apps/desktop/src/backend/DesktopAutoAttach.test.ts lines 50 to 57Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +40,21 @@40+ stateDir,41+ backendCwd: "/repo",42+ primary: mockPrimary,43+ configuration: mockConfiguration,44+ }).pipe(Effect.provide(testLayer));45+46+ assert.isFalse(result);47+ assert.isFalse(attached);48+ }),49+ );50+51+ it.effect("returns false when process in server-runtime.json is dead", () =>52+ Effect.gen(function* () {53+ const stateDir = "/tmp/t3-state-dead";54+ const runtimeContent = JSON.stringify({55+ version: 1,56+ pid: 999999999, // A non-existent PID57+ origin: "http://127.0.0.1:4545",58+ port: 4545,59+ startedAt: "2026-01-01T00:00:00.000Z",60+ });@@ -0,0 +86,21 @@86+ return false;87+ }88+89+ const state = parsedOption.value;90+ const origin = yield* Effect.try(() => new URL(state.origin)).pipe(91+ Effect.orElseSucceed(() => null),92+ );93+ if (origin === null) {94+ yield* logWarning("server-runtime.json has an invalid origin; auto-attach skipped", {95+ origin: state.origin,96+ });97+ return false;98+ }99+100+ if (state.desktopBootstrapSecret === undefined && state.desktopBootstrapToken === undefined) {101+ yield* logWarning(102+ "server-runtime.json has no desktop bootstrap credentials; auto-attach skipped",103+ { runtimeStatePath, pid: state.pid },104+ );105+ return false;106+ }@@ -181,2 +183,19 @@181- )182- .pipe(Effect.andThen(electronApp.quit)),183+ onStateDirOwned: (): Effect.Effect<void> =>184+ DesktopAutoAttach.tryAutoAttachToRunningServer({185+ stateDir: environment.stateDir,186+ backendCwd: environment.backendCwd,187+ primary: primaryInstance,188+ configuration,189+ }).pipe(190+ Effect.provide(factoryContext),191+ Effect.flatMap((attached) =>192+ attached193+ ? Effect.void194+ : electronDialog195+ .showErrorBox(196+ "This T3 home is unavailable",197+ "Another T3 Code server, such as the background service, is using this data directory, or an older background service is set up for it. Stop that server or try `t3 update`. If the older launcher cannot update, run `t3 service uninstall` and optionally reinstall with `t3 service install` from a current CLI. Use `npx t3@latest` if `t3` is missing or outdated, and pass `--base-dir` with the service's T3 home to each command. Then reopen T3 Code. You can also set a separate T3CODE_HOME and pair with the running server.",198+ )199+ .pipe(Effect.andThen(electronApp.quit)),200+ ),201+ ),apps/server/src/cli/config.ts line 2OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,2 @@1+// @effect-diagnostics nodeBuiltinImport:off - Generates random hex fallback secret for desktop auto-attach.2+import * as crypto from "node:crypto";@@ -82,3 +62,9 @@8262 ...(input.serviceManaged ? { serviceManaged: true } : {}),8363 serverVersion: packageJson.version,8464 ...(input.managedPolicy === undefined ? {} : { managedPolicy: input.managedPolicy }),65+ ...(input.config.desktopBootstrapSecret66+ ? { desktopBootstrapSecret: input.config.desktopBootstrapSecret }67+ : {}),68+ ...(input.config.desktopBootstrapToken69+ ? { desktopBootstrapToken: input.config.desktopBootstrapToken }70+ : {}),
- apps/desktop/src/backend/DesktopBackendManager.ts lines 736 to 748Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -681,3 +693,13 @@681693 mutex.withPermits(1)(682694 Effect.gen(function* () {683695 const current = yield* SubscriptionRef.get(state);696+ if (current.isExternal && current.ready) {697+ if (!current.desiredRunning) {698+ yield* SubscriptionRef.update(state, (latest) => ({699+ ...latest,700+ desiredRunning: true,701+ }));702+ }703+ return;704+ }705+
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- @@ -1143,0 +1239,21 @@1239+1240+ const watchExternal = Effect.fn("desktop.backendInstance.watchExternal")(function* (1241+ config: DesktopBackendStartConfig,1242+ pid: Option.Option<number>,1243+ ) {1244+ const isProcessAlive = yield* ProcessLivenessProbe;1245+ while (true) {1246+ yield* Effect.sleep(EXTERNAL_LIVENESS_INTERVAL);1247+ const current = yield* SubscriptionRef.get(state);1248+ if (!current.isExternal || !current.ready) {1249+ return;1250+ }1251+1252+ const pidAlive = yield* Option.match(pid, {1253+ onSome: (p) => isProcessAlive(p),1254+ onNone: () => Effect.succeed(true),1255+ });1256+1257+ const isAlive = pidAlive ? yield* probeHttpReady(config.httpBaseUrl) : false;1258+1259+ if (!isAlive) {
Wladefant(GitHub profile, opens in a new tab) requested changes
tanstack-source added labelsbug
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
copilot-pull-request-reviewer[bot](GitHub profile, opens in a new tab) reviewed
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.