Repository navigation
feat(vscode): add cross-platform keep-awake toggle #12974
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
822d0aa
feat(vscode): add cross-platform keep-awake toggle
marius-kilocode 4c20e5a
fix(vscode): clean up keep-awake shutdown
marius-kilocode c151cd7
Merge origin/main into implement-caffeine-button
marius-kilocode 2eade27
test(vscode): cover keep-awake disposal
marius-kilocode 242afc2
fix(vscode): only caffeinate while agent sessions are running
marius-kilocode e49476f
Merge branch 'main' into implement-caffeine-button
marius-kilocode a31c911
feat(vscode): revive opt-in keep-awake
marius-kilocode 1d70347
chore: merge latest main into keep-awake branch
marius-kilocode 55951bf
refactor(vscode): isolate keep-awake widget state
marius-kilocode dd7437c
fix(vscode): harden keep-awake activity and indicator
marius-kilocode 53819a0
chore: reconcile latest main intro handling
marius-kilocode 425a0d4
fix(cli): preserve legacy idle events during activity tracking
marius-kilocode 261b09a
refactor(vscode): minimize keep-awake to extension status tracking
marius-kilocode a12a747
Merge branch 'main' into implement-caffeine-button
marius-kilocode 30828e7
fix(vscode): keep multi-project toolbar controls visible
marius-kilocode 85bc28a
chore: update kilo-vscode visual regression baselines
kilo-maintainer[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "kilo-code": minor | ||
| --- | ||
|
|
||
| Add a keep-awake toggle to the Agent Manager and Kilo extension commands. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { CaffeinationService } from "./service" | ||
| export type { CaffeinationState } from "./service" | ||
| export type { CaffeinationDriver } from "./inhibitor" |
195 changes: 195 additions & 0 deletions
195
packages/kilo-vscode/src/services/caffeination/inhibitor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import { existsSync } from "node:fs" | ||
| import { delimiter, join } from "node:path" | ||
| import type { ChildProcess } from "node:child_process" | ||
| import { spawn } from "../../util/process" | ||
|
|
||
| export interface CaffeinationDriver { | ||
| readonly available: boolean | ||
| readonly reason?: string | ||
| start(parentPid: number, onExit: () => void): Promise<void> | ||
| stop(): Promise<void> | ||
| } | ||
|
|
||
| const STOP_TIMEOUT = 1_000 | ||
|
|
||
| function locate(name: string): string | undefined { | ||
| const dirs = [ | ||
| ...(process.env.PATH ?? "").split(delimiter).filter(Boolean), | ||
| ...(process.platform === "win32" ? [] : ["/usr/bin", "/bin"]), | ||
| ] | ||
| return dirs.map((dir) => join(dir, name)).find((path) => existsSync(path)) | ||
| } | ||
|
|
||
| function stopChild(child: ChildProcess): Promise<void> { | ||
| if (child.exitCode !== null || child.signalCode !== null) return Promise.resolve() | ||
|
|
||
| return new Promise((resolve) => { | ||
| let done = false | ||
| const kill = (signal: NodeJS.Signals) => { | ||
| try { | ||
| return child.kill(signal) | ||
| } catch (error) { | ||
| console.warn("[Kilo New] Failed to stop caffeination process:", error) | ||
| return false | ||
| } | ||
| } | ||
| const finish = () => { | ||
| if (done) return | ||
| done = true | ||
| clearTimeout(timer) | ||
| resolve() | ||
| } | ||
| const timer = setTimeout(() => { | ||
| if (child.exitCode === null && child.signalCode === null) kill("SIGKILL") | ||
| finish() | ||
| }, STOP_TIMEOUT) | ||
|
|
||
| child.once("exit", finish) | ||
| child.once("error", finish) | ||
| if (!kill("SIGTERM")) finish() | ||
| }) | ||
| } | ||
|
|
||
| class ProcessDriver implements CaffeinationDriver { | ||
| private child: ChildProcess | undefined | ||
| private readonly stopping = new WeakSet<ChildProcess>() | ||
|
|
||
| constructor( | ||
| private readonly command: string, | ||
| private readonly args: (parentPid: number) => string[], | ||
| public readonly available: boolean, | ||
| public readonly reason?: string, | ||
| ) {} | ||
|
|
||
| start(parentPid: number, onExit: () => void): Promise<void> { | ||
| if (this.child) return Promise.resolve() | ||
| if (!this.available) return Promise.reject(new Error(this.reason ?? "Caffeination is unavailable")) | ||
|
|
||
| const child = spawn(this.command, this.args(parentPid), { stdio: "ignore" }) | ||
| this.child = child | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| let ready = false | ||
| let closed = false | ||
| let settled = false | ||
| const clear = () => { | ||
| if (this.child === child) this.child = undefined | ||
| } | ||
| const close = (error?: Error) => { | ||
| if (closed) return | ||
| closed = true | ||
| clear() | ||
| if (!ready) { | ||
| if (!settled) { | ||
| settled = true | ||
| reject(error ?? new Error("Caffeination process exited before starting")) | ||
| } | ||
| return | ||
| } | ||
| if (!this.stopping.has(child)) onExit() | ||
| } | ||
|
|
||
| child.once("spawn", () => { | ||
| ready = true | ||
| if (settled) return | ||
| settled = true | ||
| resolve() | ||
| }) | ||
| child.once("error", (error) => close(error instanceof Error ? error : new Error(String(error)))) | ||
| child.once("exit", (code, signal) => { | ||
| const detail = signal ? `signal ${signal}` : `code ${code ?? "unknown"}` | ||
| close(new Error(`Caffeination process exited with ${detail}`)) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| async stop(): Promise<void> { | ||
| const child = this.child | ||
| if (!child) return | ||
| this.stopping.add(child) | ||
| this.child = undefined | ||
| await stopChild(child) | ||
| this.stopping.delete(child) | ||
| } | ||
| } | ||
|
|
||
| function powershell(parentPid: number): string { | ||
| return [ | ||
| "$signature = '[DllImport(\"kernel32.dll\")] public static extern uint SetThreadExecutionState(uint flags);';", | ||
| "$type = Add-Type -MemberDefinition $signature -Name 'KiloCaffeination' -Namespace 'Kilo' -PassThru;", | ||
| "$flags = 0x80000000 -bor 0x00000001;", | ||
| "$result = $type::SetThreadExecutionState($flags);", | ||
| "if ($result -eq 0) { exit 1 };", | ||
| `$parent = ${parentPid};`, | ||
| "while (Get-Process -Id $parent -ErrorAction SilentlyContinue) { Start-Sleep -Seconds 1 }", | ||
| ].join(" ") | ||
| } | ||
|
|
||
| function mac(): CaffeinationDriver { | ||
| const command = "/usr/bin/caffeinate" | ||
| return new ProcessDriver( | ||
| command, | ||
| (parentPid) => ["-i", "-w", String(parentPid)], | ||
| existsSync(command), | ||
| `The ${command} command is not available`, | ||
| ) | ||
| } | ||
|
|
||
| function linux(): CaffeinationDriver { | ||
| const command = locate("systemd-inhibit") | ||
| const shell = locate("sh") | ||
| return new ProcessDriver( | ||
| command ?? "systemd-inhibit", | ||
| (parentPid) => [ | ||
| "--what=idle:sleep", | ||
| "--who=Kilo Code", | ||
| "--why=Kilo agent running", | ||
| "--mode=block", | ||
| shell ?? "/bin/sh", | ||
| "-c", | ||
| `while kill -0 ${parentPid} 2>/dev/null; do sleep 1; done`, | ||
| ], | ||
| command !== undefined && shell !== undefined, | ||
| command === undefined ? "The systemd-inhibit command is not available" : "The sh command is not available", | ||
| ) | ||
| } | ||
|
|
||
| function windows(): CaffeinationDriver { | ||
| const root = process.env.SystemRoot | ||
| const system = root ? join(root, "System32", "WindowsPowerShell", "v1.0", "powershell.exe") : undefined | ||
| const command = system && existsSync(system) ? system : locate("powershell.exe") | ||
| return new ProcessDriver( | ||
| command ?? "powershell.exe", | ||
| (parentPid) => [ | ||
| "-NoLogo", | ||
| "-NoProfile", | ||
| "-NonInteractive", | ||
| "-WindowStyle", | ||
| "Hidden", | ||
| "-Command", | ||
| powershell(parentPid), | ||
| ], | ||
| command !== undefined, | ||
| "PowerShell is not available", | ||
| ) | ||
| } | ||
|
|
||
| class UnsupportedDriver implements CaffeinationDriver { | ||
| readonly available = false | ||
| readonly reason = `Caffeination is not supported on ${process.platform}` | ||
|
|
||
| start(): Promise<void> { | ||
| return Promise.reject(new Error(this.reason)) | ||
| } | ||
|
|
||
| stop(): Promise<void> { | ||
| return Promise.resolve() | ||
| } | ||
| } | ||
|
|
||
| export function createCaffeinationDriver(): CaffeinationDriver { | ||
| if (process.platform === "darwin") return mac() | ||
| if (process.platform === "linux") return linux() | ||
| if (process.platform === "win32") return windows() | ||
| return new UnsupportedDriver() | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.