The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - Probes the server's native SQLite ownership lock. |
| 2 | import * as NodeSqlite from "node:sqlite"; |
| 3 | |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Path from "effect/Path"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import { msg } from "gt-i18n"; |
| 11 | |
| 12 | import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; |
| 13 | import { m } from "../i18n/messages.ts"; |
| 14 | import { BACKEND_QUIT_STOP_TIMEOUT } from "./DesktopBackendManager.ts"; |
| 15 | |
| 16 | // The fields of the server's `server-runtime.json` that identify the owner of a T3 home. |
| 17 | const HomeOwnerRuntimeState = Schema.Struct({ |
| 18 | pid: Schema.Int, |
| 19 | port: Schema.Int, |
| 20 | serviceManaged: Schema.optional(Schema.Boolean), |
| 21 | serverVersion: Schema.optional(Schema.String), |
| 22 | }); |
| 23 | export type HomeOwner = typeof HomeOwnerRuntimeState.Type; |
| 24 | |
| 25 | const decodeHomeOwner = Schema.decodeUnknownOption(Schema.fromJsonString(HomeOwnerRuntimeState)); |
| 26 | |
| 27 | /** Signal 0 only probes existence; EPERM still means alive. */ |
| 28 | const isProcessAlive = (pid: number): boolean => { |
| 29 | if (!Number.isInteger(pid) || pid <= 0) return false; |
| 30 | try { |
| 31 | process.kill(pid, 0); |
| 32 | return true; |
| 33 | } catch (error) { |
| 34 | return error instanceof Error && "code" in error && error.code === "EPERM"; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * Whether a server holds the T3 home's ownership lock (an exclusive transaction on |
| 40 | * `server-owner.sqlite`). A read-only read is refused with SQLITE_BUSY only while it is held. |
| 41 | */ |
| 42 | const isHomeOwnershipLocked = Effect.fn("DesktopHomeOwner.isHomeOwnershipLocked")(function* () { |
| 43 | const path = yield* Path.Path; |
| 44 | const { stateDir } = yield* DesktopEnvironment.DesktopEnvironment; |
| 45 | const lockPath = path.join(stateDir, "server-owner.sqlite"); |
| 46 | return yield* Effect.sync(() => { |
| 47 | try { |
| 48 | const db = new NodeSqlite.DatabaseSync(lockPath, { readOnly: true }); |
| 49 | try { |
| 50 | db.exec("PRAGMA busy_timeout = 0"); |
| 51 | db.prepare("SELECT count(*) FROM sqlite_master").get(); |
| 52 | return false; |
| 53 | } finally { |
| 54 | db.close(); |
| 55 | } |
| 56 | } catch (cause) { |
| 57 | // Anything but SQLITE_BUSY (a missing file included) leaves the decision to the backend. |
| 58 | return cause instanceof Error && "errcode" in cause && cause.errcode === 5; |
| 59 | } |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | // A quitting app's backend holds the lock until it exits, which can outlast the relaunch of an update. |
| 64 | const HOME_OWNERSHIP_RELEASE_GRACE = Duration.sum(BACKEND_QUIT_STOP_TIMEOUT, Duration.seconds(3)); |
| 65 | const HOME_OWNERSHIP_RELEASE_POLL_INTERVAL = Duration.millis(250); |
| 66 | |
| 67 | /** Whether the ownership lock is free, or frees within the grace a shutting-down server needs. */ |
| 68 | export const awaitHomeOwnershipRelease = Effect.fn("DesktopHomeOwner.awaitHomeOwnershipRelease")( |
| 69 | function* () { |
| 70 | while (yield* isHomeOwnershipLocked()) { |
| 71 | yield* Effect.sleep(HOME_OWNERSHIP_RELEASE_POLL_INTERVAL); |
| 72 | } |
| 73 | return true; |
| 74 | }, |
| 75 | Effect.timeoutOrElse({ |
| 76 | duration: HOME_OWNERSHIP_RELEASE_GRACE, |
| 77 | orElse: () => Effect.succeed(false), |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | /** The live server recorded as owning the desktop's T3 home. Only call once the ownership lock is known held: a live PID alone may be reused. */ |
| 82 | export const readLiveHomeOwner = Effect.fn("DesktopHomeOwner.readLiveHomeOwner")(function* () { |
| 83 | const fs = yield* FileSystem.FileSystem; |
| 84 | const path = yield* Path.Path; |
| 85 | const { stateDir } = yield* DesktopEnvironment.DesktopEnvironment; |
| 86 | const raw = yield* fs |
| 87 | .readFileString(path.join(stateDir, "server-runtime.json")) |
| 88 | .pipe(Effect.option); |
| 89 | return Option.flatMap(raw, decodeHomeOwner).pipe( |
| 90 | Option.filter((owner) => isProcessAlive(owner.pid)), |
| 91 | ); |
| 92 | }); |
| 93 | |
| 94 | export const homeOwnedMessage = (owner: Option.Option<HomeOwner>): string => |
| 95 | Option.match(owner, { |
| 96 | onNone: () => |
| 97 | m( |
| 98 | msg( |
| 99 | "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.", |
| 100 | ), |
| 101 | ), |
| 102 | onSome: (owner) => { |
| 103 | const variables = { |
| 104 | pid: String(owner.pid), |
| 105 | port: String(owner.port), |
| 106 | version: owner.serverVersion ?? "", |
| 107 | }; |
| 108 | if (owner.serviceManaged) { |
| 109 | return owner.serverVersion |
| 110 | ? m( |
| 111 | msg( |
| 112 | "The T3 Code background service is using this T3 home (PID {pid}, port {port}, version {version}). Stop it with `t3 service uninstall`, passing `--base-dir` with this T3 home, and reopen T3 Code. You can also set a separate T3CODE_HOME and pair with the running server.", |
| 113 | ), |
| 114 | variables, |
| 115 | ) |
| 116 | : m( |
| 117 | msg( |
| 118 | "The T3 Code background service is using this T3 home (PID {pid}, port {port}). Stop it with `t3 service uninstall`, passing `--base-dir` with this T3 home, and reopen T3 Code. You can also set a separate T3CODE_HOME and pair with the running server.", |
| 119 | ), |
| 120 | variables, |
| 121 | ); |
| 122 | } |
| 123 | return owner.serverVersion |
| 124 | ? m( |
| 125 | msg( |
| 126 | "Another T3 Code server (for example `t3 serve`, or one started over SSH) is using this T3 home (PID {pid}, port {port}, version {version}). If a T3 Code app on another machine started it over SSH, that app restarts it: remove or disconnect that SSH environment there, or quit that app. Then stop the server, and reopen T3 Code. You can also set a separate T3CODE_HOME and pair with the running server.", |
| 127 | ), |
| 128 | variables, |
| 129 | ) |
| 130 | : m( |
| 131 | msg( |
| 132 | "Another T3 Code server (for example `t3 serve`, or one started over SSH) is using this T3 home (PID {pid}, port {port}). If a T3 Code app on another machine started it over SSH, that app restarts it: remove or disconnect that SSH environment there, or quit that app. Then stop the server, and reopen T3 Code. You can also set a separate T3CODE_HOME and pair with the running server.", |
| 133 | ), |
| 134 | variables, |
| 135 | ); |
| 136 | }, |
| 137 | }); |