@devries-cameron20 / t3code-1 / commits / 8a6342e761
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): keep-awake logs a hold only once acquired and reports why the inhibitor failed (#38505)

Julius Marminge committed Nov 18, 2026 at 00:39 UTC 8a6342e761357feff652a57d3dd16f0c78026c25
1 file changed +52 -25
apps/server/src/background/KeepAwake.ts
+52 -25
@@ -1,7 +1,9 @@
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";
@@ -19,9 +21,12 @@ import * as Settings from "../serverSettings.ts";
21 const RELEASE_GRACE = Duration.seconds(60);
22
23 // ES_CONTINUOUS | ES_SYSTEM_REQUIRED, held by the calling thread until the script ends.
22 -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)`;
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
24 -/** Each command holds its inhibitor until it exits, and exits on its own if the server dies. */
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,
@@ -49,7 +54,11 @@ function inhibitorCommand(
54 }
55 switch (platform) {
56 case "darwin":
52 - return { command: "caffeinate", args: ["-i", "-w", String(serverPid)], stdin: "ignore" };
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",
@@ -58,10 +67,11 @@ function inhibitorCommand(
67 "--who=T3 Code",
68 "--why=Agents are working",
69 "--mode=block",
61 - "tail",
62 - `--pid=${serverPid}`,
63 - "-f",
64 - "/dev/null",
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 };
@@ -117,8 +127,12 @@ const retryWithBackoff =
127
128 class KeepAwakeInhibitorExited extends Schema.TaggedError<KeepAwakeInhibitorExited>()(
129 "KeepAwakeInhibitorExited",
120 - { exitCode: Schema.Number },
121 -) {}
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* () {
@@ -188,29 +202,42 @@ export const layer = Layer.effectDiscard(
202 }),
203 );
204
191 - const holdOnce = spawner
192 - .spawn(
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,
197 - stdout: "ignore",
198 - stderr: "ignore",
211 + stdout: "pipe",
212 + stderr: "pipe",
213 }),
200 - )
201 - .pipe(
202 - Effect.tap((child) =>
203 - Effect.logInfo("keep-awake holding sleep inhibitor", { pid: Number(child.pid) }),
204 - ),
205 - Effect.tap(() =>
206 - Effect.addFinalizer(() => Effect.logInfo("keep-awake released sleep inhibitor")),
207 - ),
208 - Effect.flatMap((child) => child.exitCode),
209 - Effect.flatMap((exitCode) =>
210 - Effect.fail(new KeepAwakeInhibitorExited({ exitCode: Number(exitCode) })),
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 ),
212 - Effect.scoped,
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,