The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Duration from "effect/Duration"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Fiber from "effect/Fiber"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Ref from "effect/Ref"; |
| 7 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 8 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Schema from "effect/Schema"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import * as SqlClient from "effect/sql/SqlClient"; |
| 13 | |
| 14 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 15 | |
| 16 | import * as OrchestrationEventStore from "../persistence/OrchestrationEventStore.ts"; |
| 17 | import { forkParked } from "../serverActivation.ts"; |
| 18 | import * as Settings from "../serverSettings.ts"; |
| 19 | |
| 20 | // Bridges gaps between back-to-back runs, so a long-idle machine does not sleep mid-queue. |
| 21 | const RELEASE_GRACE = Duration.seconds(60); |
| 22 | |
| 23 | // ES_CONTINUOUS | ES_SYSTEM_REQUIRED, held by the calling thread until the script ends. |
| 24 | const WINDOWS_HOLD_SCRIPT = `$t = Add-Type -Name Power -Namespace T3 -PassThru -MemberDefinition '[DllImport("kernel32.dll")] public static extern uint SetThreadExecutionState(uint f);'; [void]$t::SetThreadExecutionState([uint32]2147483649); [Console]::Out.WriteLine()`; |
| 25 | |
| 26 | /** |
| 27 | * Each command writes a line to stdout once it holds its inhibitor, holds it until it exits, and |
| 28 | * exits on its own if the server dies. |
| 29 | */ |
| 30 | function inhibitorCommand( |
| 31 | platform: NodeJS.Platform, |
| 32 | serverPid: number, |
| 33 | isWsl: boolean, |
| 34 | ): |
| 35 | | { |
| 36 | readonly command: string; |
| 37 | readonly args: ReadonlyArray<string>; |
| 38 | readonly stdin: "ignore" | "pipe"; |
| 39 | } |
| 40 | | undefined { |
| 41 | // logind inside the WSL VM cannot block Windows sleep. Windows cannot wait on a Linux pid, so |
| 42 | // the hold ends when its stdin pipe closes, which also happens if the server dies. |
| 43 | if (isWsl) { |
| 44 | return { |
| 45 | command: "powershell.exe", |
| 46 | args: [ |
| 47 | "-NoProfile", |
| 48 | "-NonInteractive", |
| 49 | "-Command", |
| 50 | `${WINDOWS_HOLD_SCRIPT}; [void][Console]::In.ReadToEnd()`, |
| 51 | ], |
| 52 | stdin: "pipe", |
| 53 | }; |
| 54 | } |
| 55 | switch (platform) { |
| 56 | case "darwin": |
| 57 | return { |
| 58 | command: "sh", |
| 59 | args: ["-c", 'echo && exec caffeinate -i -w "$0"', String(serverPid)], |
| 60 | stdin: "ignore", |
| 61 | }; |
| 62 | case "linux": |
| 63 | return { |
| 64 | command: "systemd-inhibit", |
| 65 | args: [ |
| 66 | "--what=sleep", |
| 67 | "--who=T3 Code", |
| 68 | "--why=Agents are working", |
| 69 | "--mode=block", |
| 70 | // systemd-inhibit starts this only after logind grants the lock. |
| 71 | "sh", |
| 72 | "-c", |
| 73 | 'echo && exec tail --pid="$0" -f /dev/null', |
| 74 | String(serverPid), |
| 75 | ], |
| 76 | stdin: "ignore", |
| 77 | }; |
| 78 | case "win32": |
| 79 | return { |
| 80 | command: "powershell.exe", |
| 81 | args: [ |
| 82 | "-NoProfile", |
| 83 | "-NonInteractive", |
| 84 | "-Command", |
| 85 | `${WINDOWS_HOLD_SCRIPT}; Wait-Process -Id ${serverPid}`, |
| 86 | ], |
| 87 | stdin: "ignore", |
| 88 | }; |
| 89 | default: |
| 90 | return undefined; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Runs `hold` while `wanted` is true and for `grace` after it turns false; a true inside the |
| 96 | * grace keeps the same hold. `hold` runs until interrupted. |
| 97 | */ |
| 98 | export const holdWhileWanted = <E, R>( |
| 99 | wanted: Stream.Stream<boolean, E, R>, |
| 100 | hold: Effect.Effect<void>, |
| 101 | grace: Duration.Input = RELEASE_GRACE, |
| 102 | ) => |
| 103 | wanted.pipe( |
| 104 | Stream.changes, |
| 105 | Stream.switchMap((on) => |
| 106 | on ? Stream.succeed(true) : Stream.fromEffect(Effect.as(Effect.sleep(grace), false)), |
| 107 | ), |
| 108 | Stream.changes, |
| 109 | Stream.switchMap((on) => (on ? Stream.fromEffect(hold) : Stream.empty)), |
| 110 | Stream.runDrain, |
| 111 | ); |
| 112 | |
| 113 | // Reads, the event subscription and the inhibitor retry until they succeed; the cap keeps a |
| 114 | // host without an inhibitor (e.g. no logind) from retrying and warning constantly. |
| 115 | const backoff = Schedule.exponential("5 seconds").pipe( |
| 116 | Schedule.modifyDelay(({ duration }) => |
| 117 | Effect.succeed(Duration.min(duration, Duration.minutes(10))), |
| 118 | ), |
| 119 | ); |
| 120 | const retryWithBackoff = |
| 121 | (message: string) => |
| 122 | <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 123 | effect.pipe( |
| 124 | Effect.tapCause((cause) => Effect.logWarning(message, { cause })), |
| 125 | Effect.retry(backoff), |
| 126 | ); |
| 127 | |
| 128 | class KeepAwakeInhibitorExited extends Schema.TaggedError<KeepAwakeInhibitorExited>()( |
| 129 | "KeepAwakeInhibitorExited", |
| 130 | { exitCode: Schema.Number, stderr: Schema.String }, |
| 131 | ) { |
| 132 | override get message(): string { |
| 133 | return `sleep inhibitor exited with code ${this.exitCode}${this.stderr ? `: ${this.stderr}` : ""}`; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | export const layer = Layer.effectDiscard( |
| 138 | Effect.gen(function* () { |
| 139 | const platform = yield* HostProcess.Platform; |
| 140 | const fileSystem = yield* FileSystem.FileSystem; |
| 141 | // Services started by systemd lack WSL_DISTRO_NAME and WSL_INTEROP; the kernel release does not. |
| 142 | const isWsl = |
| 143 | platform === "linux" && |
| 144 | (yield* fileSystem.readFileString("/proc/sys/kernel/osrelease").pipe( |
| 145 | Effect.map((release) => release.toLowerCase().includes("microsoft")), |
| 146 | Effect.orElseSucceed(() => false), |
| 147 | )); |
| 148 | const command = inhibitorCommand(platform, process.pid, isWsl); |
| 149 | if (command === undefined) return; |
| 150 | const settings = yield* Settings.ServerSettingsService; |
| 151 | const events = yield* OrchestrationEventStore.OrchestrationEventStore; |
| 152 | const sql = yield* SqlClient.SqlClient; |
| 153 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 154 | |
| 155 | const hasActiveRun = sql<{ readonly active: number }>` |
| 156 | SELECT EXISTS ( |
| 157 | SELECT 1 FROM orchestration_v2_projection_runs |
| 158 | WHERE status IN ('preparing', 'starting', 'running') |
| 159 | ) AS active |
| 160 | `.pipe( |
| 161 | Effect.map((rows) => rows[0]?.active === 1), |
| 162 | retryWithBackoff("keep-awake could not read active runs"), |
| 163 | ); |
| 164 | // Read the cursor before the snapshot so a run changing in between is replayed, not missed. |
| 165 | const active = Stream.unwrap( |
| 166 | events.latestApplicationSequence.pipe( |
| 167 | retryWithBackoff("keep-awake could not read the event cursor"), |
| 168 | Effect.map((afterSequence) => |
| 169 | Stream.concat( |
| 170 | Stream.succeed(undefined), |
| 171 | events |
| 172 | .streamApplicationEvents({ afterSequence }) |
| 173 | .pipe( |
| 174 | Stream.filter( |
| 175 | (stored) => |
| 176 | "event" in stored && |
| 177 | (stored.event.type === "run.created" || stored.event.type === "run.updated"), |
| 178 | ), |
| 179 | ), |
| 180 | ), |
| 181 | ), |
| 182 | ), |
| 183 | ).pipe( |
| 184 | Stream.mapEffect(() => hasActiveRun), |
| 185 | // A failed subscription resubscribes, which re-reads the cursor and the snapshot. |
| 186 | Stream.tapCause((cause) => |
| 187 | Effect.logWarning("keep-awake run event stream failed", { cause }), |
| 188 | ), |
| 189 | Stream.retry(backoff), |
| 190 | ); |
| 191 | const enabled = Stream.unwrap( |
| 192 | Effect.gen(function* () { |
| 193 | const changes = yield* settings.subscribeChanges; |
| 194 | const current = yield* settings.getSettings.pipe( |
| 195 | Effect.map((value) => value.keepAwakeWhileAgentsWork), |
| 196 | Effect.orElseSucceed(() => true), |
| 197 | ); |
| 198 | return Stream.concat( |
| 199 | Stream.succeed(current), |
| 200 | Stream.map(changes, (value) => value.keepAwakeWhileAgentsWork), |
| 201 | ); |
| 202 | }), |
| 203 | ); |
| 204 | |
| 205 | const holdOnce = Effect.gen(function* () { |
| 206 | const child = yield* spawner.spawn( |
| 207 | ChildProcess.make(command.command, command.args, { |
| 208 | detached: false, |
| 209 | shell: false, |
| 210 | stdin: command.stdin, |
| 211 | stdout: "pipe", |
| 212 | stderr: "pipe", |
| 213 | }), |
| 214 | ); |
| 215 | const stderr = yield* child.stderr.pipe( |
| 216 | Stream.decodeText(), |
| 217 | Stream.mkString, |
| 218 | Effect.forkScoped, |
| 219 | ); |
| 220 | const held = yield* Ref.make(false); |
| 221 | yield* Effect.addFinalizer(() => |
| 222 | Effect.when(Effect.logInfo("keep-awake released sleep inhibitor"), Ref.get(held)), |
| 223 | ); |
| 224 | yield* child.stdout.pipe( |
| 225 | Stream.take(1), |
| 226 | Stream.runForEach(() => |
| 227 | Ref.set(held, true).pipe( |
| 228 | Effect.andThen( |
| 229 | Effect.logInfo("keep-awake holding sleep inhibitor", { pid: Number(child.pid) }), |
| 230 | ), |
| 231 | ), |
| 232 | ), |
| 233 | Effect.forkScoped, |
| 234 | ); |
| 235 | const exitCode = yield* child.exitCode; |
| 236 | return yield* new KeepAwakeInhibitorExited({ |
| 237 | exitCode: Number(exitCode), |
| 238 | stderr: (yield* Fiber.join(stderr)).trim(), |
| 239 | }); |
| 240 | }).pipe(Effect.scoped); |
| 241 | const hold = holdOnce.pipe( |
| 242 | retryWithBackoff("keep-awake sleep inhibitor stopped"), |
| 243 | Effect.ignore, |
| 244 | ); |
| 245 | |
| 246 | yield* forkParked( |
| 247 | holdWhileWanted( |
| 248 | Stream.zipLatestWith(enabled, active, (on, busy) => on && busy), |
| 249 | hold, |
| 250 | ).pipe(Effect.catchCause((cause) => Effect.logWarning("keep-awake stopped", { cause }))), |
| 251 | ); |
| 252 | }), |
| 253 | ); |