Skip to content

Studio: add a Keep computer awake setting to the desktop app - #17659

Merged
wasimysaid merged 6 commits into
unslothai:mainfrom
wasimysaid:feat/keep-computer-awake
Nov 17, 2026
Merged

wasimysaid merged 6 commits into
unslothai:mainfrom
wasimysaid:feat/keep-computer-awake

Conversation

@wasimysaid

Copy link
Copy Markdown
Collaborator

What changed

The desktop app has a new Keep computer awake switch in Settings > General, under a new Power section. While it is on, Unsloth stops the computer from going to sleep on its own for as long as the app runs, so a long training run, download or served model is not cut off by idle sleep. The display can still turn off. The switch is off by default, saved like Close to system tray, and turned back on at launch.

The block is the OS's own sleep request, so it ends with the process; a crash cannot leave the computer unable to sleep:

  • Windows: a PowerSetRequest(PowerRequestSystemRequired) power request, listed by powercfg /requests with its reason.
  • macOS: an IOKit PreventUserIdleSystemSleep assertion, listed by pmset -g assertions.
  • Linux: org.freedesktop.PowerManagement.Inhibit (KDE Plasma, Xfce, LXQt), then org.gnome.SessionManager.Inhibit with the suspend flag (GNOME, Cinnamon), then a logind idle inhibitor for sessions with neither. The xdg portal's Inhibit is not used: on Plasma 6.8 its inhibitions outlived both Request.Close and the caller's exit.

On Linux the block owns its D-Bus connection, because the desktop drops an inhibitor as soon as its holder's connection closes.

The switch reads back whether the block is actually held, so if no inhibitor is available it shows off and turning it on shows the error. The row is searchable ("sleep", "caffeinate" and the translated words also find it), and the strings are translated in every locale.

How it was verified

  • New Rust tests for the saved preference, and a settings search test that the row is indexed on every desktop and never in a browser.
  • node scripts/typecheck.mjs --tests and the frontend test suite in studio/frontend. 12 tests in date, usage and memory-estimate files fail the same way on main without this change.
  • On KDE Plasma 6.8 (Wayland): PowerDevil listed the inhibition while the switch was on, dropped it when switched off, and dropped it when the process exited.
  • Ran ./install.sh --local and launched the desktop app with cargo tauri dev; the Power section and switch render in Settings > General.

Not tested here

  • Windows and macOS were not run. Their code was compiled with cargo check for x86_64-pc-windows-msvc and aarch64-apple-darwin in an isolated crate, since the full app does not cross-compile here.
  • GNOME and the logind fallback were not run; only the KDE path was exercised live.

A switch in Settings > General > Power stops the computer from sleeping on its own while Unsloth runs; the display can still turn off. The block is an OS power request (Windows), an IOKit assertion (macOS), or a session/logind inhibitor (Linux), so it ends with the process.
@danielhanchen

Copy link
Copy Markdown
Member

Two reds on bc06865:

  • Repo tests (CPU, studio): tests/studio/test_tauri_branding_contract.py::test_desktop_surfaces_do_not_restore_studio_branding flags studio/src-tauri/src/keep_awake.rs:16 (REASON) and :262 (APP_NAME), both "Unsloth Studio". The desktop app names itself "Unsloth", so using that in the inhibit reason and app name clears it.
  • Frontend build + bundle sanity: tests/browser-tab-session.test.ts is a timing flake unrelated to this PR; Tests: wait for the stored-chat lookup itself in browser-tab-session #17668 fixes it.

danielhanchen added a commit that referenced this pull request Nov 13, 2026
…17668)

## Summary

`Frontend build + bundle sanity` intermittently fails on PRs that do not
touch the browser (seen on #17641 and #17659):

```
not ok - an unsaved chat's tabs follow it to the key a reload gives it, by either way the chat page names it
  location: 'studio/frontend/tests/browser-tab-session.test.ts:160:1'
  + actual - expected
  +   false,
  +   []
  -   true,
  -   [
  -     'https://draft.example/'
  -   ]
```

The test came in with #17596. After a reload, the browser store asks
storage whether the chat shown before the reload was saved
(`chatIsUnsaved` in `tab-session.ts`), and that lookup starts with a
dynamic `import()`. In the test, the import goes through the module
hooks registered by `browser-store-resolver.mjs`. The test waited one
`setTimeout(0)` and then asserted that the lookup had landed. On a busy
runner the import can take longer than one timer tick, so the unsaved
chat's tabs had not been handed over yet.

The store is right: it handles the lookup landing either before or after
the chat is named, and that is what the test is checking. Only the
test's wait was too short.

## Change

- The test stub's `__storedThreads` is now a `Set` that counts `has`
calls, which is how the stub answers each lookup. `reloaded()` counts a
lookup when the session it reloads from shows a `single:` chat, which is
the only case where the store looks one up.
- `settled()` waits until every started lookup has been answered (10 s
cap, then fails with "a stored-chat lookup never answered"), then waits
one more tick for the store's `.then`.

No product code changes.

## Testing

- Sabotage: with a 5 ms delay added to the stub's `getStoredChatThread`,
main's version of the test fails with the same diff as CI. With this
change it passes with a 5 ms delay and with a 60 ms one.
- Without the delay: `tests/browser-tab-session.test.ts` 7 passed, 3
runs.
- `tsc -p tsconfig.test.json`: no errors in the file.
@wasimysaid

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T12:22:47.299566Z 98bd484 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d9e0a9dbef

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/src-tauri/src/keep_awake.rs Outdated
/// Reports the block actually held, so a setting that failed to apply reads as off.
#[tauri::command]
pub async fn get_keep_awake() -> Option<bool> {
SUPPORTED.then(is_held)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Wait for startup restoration before reading the setting

When the saved preference is true, initialize starts apply(true) on a detached thread. If the General tab calls this command before that work finishes, is_held() returns false; the settings hook loads only once, so the checkbox stays off even after inhibition succeeds. Make the initial read await initialization or notify the UI when restoration completes, so users can see that the computer is still being kept awake.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 98bd484. Reads and writes wait for the startup restoration thread on a blocking worker; the join remains serialized so concurrent commands cannot bypass restoration. A lightweight Rust synchronization regression fails on the previous implementation and passes with the fix. Local checks pass (the workflow trigger check was rerun successfully after installing its missing PyYAML dependency).

Copy link
Copy Markdown
Collaborator Author

@codex review

Addressed the startup restoration race in 98bd484. Regression verified failing before and passing after; local checks and static audits pass. Full desktop compilation was not run for this follow-up.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 98bd484f22

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@wasimysaid
wasimysaid merged commit e6cfd30 into unslothai:main Nov 17, 2026
danielhanchen added a commit that referenced this pull request Nov 17, 2026
…17659) (#18125)

`tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding`
fails on main in the CPU `rest-2` shard since #17659 (seen on the
staging run of #18122):

```
E   AssertionError: 1 file reads in the test trees touch a checked-in file with the platform default encoding, ...
E     + 'tests/studio/test_keep_awake_initialization.py:10: read_text()'
```

## Cause

`tests/studio/test_keep_awake_initialization.py` (added in #17659) reads
`studio/src-tauri/src/keep_awake.rs` with `read_text()`. Without an
encoding that uses the locale codepage on Windows (cp1252, cp932, ...),
so the test breaks there as soon as the Rust file gains a non-ASCII
byte. The lint is right; the new test missed it.

## Fix

`read_text(encoding = "utf-8")`.

## Verification

- main: the lint fails with the line above.
- this branch: `tests/test_source_read_encoding.py` 16 passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants