The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { DESKTOP_UPDATE_IN_PROGRESS_TTL_MS } from "@t3tools/contracts"; |
| 2 | import * as Clock from "effect/Clock"; |
| 3 | import * as Context from "effect/Context"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | |
| 10 | const UpdateMarker = Schema.fromJsonString( |
| 11 | Schema.Struct({ startedAt: Schema.Number, expiresAt: Schema.Number }), |
| 12 | ); |
| 13 | const encodeUpdateMarker = Schema.encodeEffect(UpdateMarker); |
| 14 | |
| 15 | /** |
| 16 | * Makes SSH launches wait for the server that replaces this one instead of starting their own. |
| 17 | * The server writes it, so it also covers a boot service whose launcher predates its own lock. |
| 18 | */ |
| 19 | export const publish = Effect.fn("DesktopUpdateMarker.publish")(function* (markerFile: string) { |
| 20 | const fileSystem = yield* FileSystem.FileSystem; |
| 21 | const startedAt = yield* Clock.currentTimeMillis; |
| 22 | const contents = yield* encodeUpdateMarker({ |
| 23 | startedAt, |
| 24 | expiresAt: startedAt + DESKTOP_UPDATE_IN_PROGRESS_TTL_MS, |
| 25 | }); |
| 26 | // Renamed into place: a launch must never read a half-written marker. |
| 27 | const temporary = `${markerFile}.tmp`; |
| 28 | yield* fileSystem.writeFileString(temporary, contents); |
| 29 | yield* fileSystem.rename(temporary, markerFile); |
| 30 | }); |
| 31 | |
| 32 | export class DesktopUpdateMarker extends Context.Service< |
| 33 | DesktopUpdateMarker, |
| 34 | { |
| 35 | /** |
| 36 | * Remaining SSH launch wait in ms, capped at this build's TTL. |
| 37 | * Read-only: a newer update may have just replaced the marker. |
| 38 | */ |
| 39 | readonly remainingWait: (markerFile: string) => Effect.Effect<Option.Option<number>>; |
| 40 | } |
| 41 | >()("t3/desktopUpdate/DesktopUpdateMarker") {} |
| 42 | |
| 43 | const make = Effect.gen(function* () { |
| 44 | const fileSystem = yield* FileSystem.FileSystem; |
| 45 | |
| 46 | const remainingWait: DesktopUpdateMarker["Service"]["remainingWait"] = Effect.fn( |
| 47 | "DesktopUpdateMarker.remainingWait", |
| 48 | )(function* (markerFile) { |
| 49 | const marker = yield* fileSystem |
| 50 | .readFileString(markerFile) |
| 51 | .pipe(Effect.flatMap(Schema.decodeUnknownEffect(UpdateMarker)), Effect.option); |
| 52 | // Sampled after the read so a marker published mid-read is not seen as future-dated. |
| 53 | const now = yield* Clock.currentTimeMillis; |
| 54 | if (Option.isSome(marker)) { |
| 55 | const { startedAt, expiresAt } = marker.value; |
| 56 | if ( |
| 57 | Number.isFinite(startedAt) && |
| 58 | Number.isFinite(expiresAt) && |
| 59 | startedAt <= now && |
| 60 | expiresAt > now && |
| 61 | expiresAt > startedAt |
| 62 | ) { |
| 63 | return Option.some(Math.min(expiresAt - now, DESKTOP_UPDATE_IN_PROGRESS_TTL_MS)); |
| 64 | } |
| 65 | } |
| 66 | return Option.none(); |
| 67 | }); |
| 68 | |
| 69 | return DesktopUpdateMarker.of({ remainingWait }); |
| 70 | }); |
| 71 | |
| 72 | export const layer = Layer.effect(DesktopUpdateMarker, make); |