Repository navigation
fix(server): keep-awake logs a hold only once acquired and reports why the inhibitor failed #38505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| import * as Duration from "effect/Duration"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Fiber from "effect/Fiber"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Ref from "effect/Ref"; | ||
| import * as ChildProcess from "effect/process/ChildProcess"; | ||
| import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; | ||
| import * as Schedule from "effect/Schedule"; | ||
|
|
@@ -19,9 +21,12 @@ import * as Settings from "../serverSettings.ts"; | |
| const RELEASE_GRACE = Duration.seconds(60); | ||
|
|
||
| // ES_CONTINUOUS | ES_SYSTEM_REQUIRED, held by the calling thread until the script ends. | ||
| 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)`; | ||
| 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()`; | ||
|
|
||
| /** Each command holds its inhibitor until it exits, and exits on its own if the server dies. */ | ||
| /** | ||
| * Each command writes a line to stdout once it holds its inhibitor, holds it until it exits, and | ||
| * exits on its own if the server dies. | ||
| */ | ||
| function inhibitorCommand( | ||
| platform: NodeJS.Platform, | ||
| serverPid: number, | ||
|
|
@@ -49,7 +54,11 @@ function inhibitorCommand( | |
| } | ||
| switch (platform) { | ||
| case "darwin": | ||
| return { command: "caffeinate", args: ["-i", "-w", String(serverPid)], stdin: "ignore" }; | ||
| return { | ||
| command: "sh", | ||
| args: ["-c", 'echo && exec caffeinate -i -w "$0"', String(serverPid)], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium On macOS, this 🚀 Reply "fix it for me" or copy this AI Prompt for your agent: |
||
| stdin: "ignore", | ||
| }; | ||
| case "linux": | ||
| return { | ||
| command: "systemd-inhibit", | ||
|
|
@@ -58,10 +67,11 @@ function inhibitorCommand( | |
| "--who=T3 Code", | ||
| "--why=Agents are working", | ||
| "--mode=block", | ||
| "tail", | ||
| `--pid=${serverPid}`, | ||
| "-f", | ||
| "/dev/null", | ||
| // systemd-inhibit starts this only after logind grants the lock. | ||
| "sh", | ||
| "-c", | ||
| 'echo && exec tail --pid="$0" -f /dev/null', | ||
| String(serverPid), | ||
| ], | ||
| stdin: "ignore", | ||
| }; | ||
|
|
@@ -117,8 +127,12 @@ const retryWithBackoff = | |
|
|
||
| class KeepAwakeInhibitorExited extends Schema.TaggedError<KeepAwakeInhibitorExited>()( | ||
| "KeepAwakeInhibitorExited", | ||
| { exitCode: Schema.Number }, | ||
| ) {} | ||
| { exitCode: Schema.Number, stderr: Schema.String }, | ||
| ) { | ||
| override get message(): string { | ||
| return `sleep inhibitor exited with code ${this.exitCode}${this.stderr ? `: ${this.stderr}` : ""}`; | ||
|
Comment on lines
+130
to
+133
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep raw helper output out of the service error and warning. If the helper writes a denial followed by local diagnostic details, As per path instructions, “Attributes and log annotations should be bounded and must not expose raw command output or arbitrary text.” As per coding guidelines, “The message is fixed or built from those attributes, never from
|
||
| } | ||
| } | ||
|
|
||
| export const layer = Layer.effectDiscard( | ||
| Effect.gen(function* () { | ||
|
|
@@ -188,29 +202,42 @@ export const layer = Layer.effectDiscard( | |
| }), | ||
| ); | ||
|
|
||
| const holdOnce = spawner | ||
| .spawn( | ||
| const holdOnce = Effect.gen(function* () { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a focused regression test for the denied-inhibitor path. The existing As per coding guidelines, “Backend behavior changes ship with focused tests for that behavior.”
|
||
| const child = yield* spawner.spawn( | ||
| ChildProcess.make(command.command, command.args, { | ||
| detached: false, | ||
| shell: false, | ||
| stdin: command.stdin, | ||
| stdout: "ignore", | ||
| stderr: "ignore", | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }), | ||
| ) | ||
| .pipe( | ||
| Effect.tap((child) => | ||
| Effect.logInfo("keep-awake holding sleep inhibitor", { pid: Number(child.pid) }), | ||
| ), | ||
| Effect.tap(() => | ||
| Effect.addFinalizer(() => Effect.logInfo("keep-awake released sleep inhibitor")), | ||
| ), | ||
| Effect.flatMap((child) => child.exitCode), | ||
| Effect.flatMap((exitCode) => | ||
| Effect.fail(new KeepAwakeInhibitorExited({ exitCode: Number(exitCode) })), | ||
| ); | ||
| const stderr = yield* child.stderr.pipe( | ||
| Stream.decodeText(), | ||
| Stream.mkString, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bound stderr retention while the inhibitor runs. If a long-lived helper wrapper writes diagnostics periodically,
|
||
| Effect.forkScoped, | ||
| ); | ||
| const held = yield* Ref.make(false); | ||
| yield* Effect.addFinalizer(() => | ||
| Effect.when(Effect.logInfo("keep-awake released sleep inhibitor"), Ref.get(held)), | ||
| ); | ||
| yield* child.stdout.pipe( | ||
| Stream.take(1), | ||
| Stream.runForEach(() => | ||
| Ref.set(held, true).pipe( | ||
| Effect.andThen( | ||
| Effect.logInfo("keep-awake holding sleep inhibitor", { pid: Number(child.pid) }), | ||
| ), | ||
| ), | ||
| ), | ||
| Effect.scoped, | ||
| Effect.forkScoped, | ||
| ); | ||
| const exitCode = yield* child.exitCode; | ||
| return yield* new KeepAwakeInhibitorExited({ | ||
| exitCode: Number(exitCode), | ||
| stderr: (yield* Fiber.join(stderr)).trim(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve the exit code when the stderr read fails. If
|
||
| }); | ||
| }).pipe(Effect.scoped); | ||
| const hold = holdOnce.pipe( | ||
| retryWithBackoff("keep-awake sleep inhibitor stopped"), | ||
| Effect.ignore, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium
background/KeepAwake.ts:24On Windows and WSL, the helper emits its readiness line even when
SetThreadExecutionStatereturns0(failure), so the server reports a successful hold without preventing sleep. The[void]cast discards the result; check it and exit nonzero before writing readiness.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: