Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

opencode-nosleep — keep Windows awake while opencode is working

Platform opencode License

A tiny opencode plugin for Windows that prevents your laptop from going to sleep and your screen from turning off while an opencode session is actually running — and releases the lock the second the agent goes idle.

No tray icon. No always-on "caffeine" mode. No edits to your Windows power plan.


The problem

You give an AI coding agent a long task — a refactor, a test suite run, a batch of migrations — walk away, and come back to a laptop that fell asleep two minutes in. The task is frozen, the model context is stale, and you start over.

The usual workarounds are all bad in the same way: they keep the machine awake forever. Setting "Sleep: Never" in Windows power settings, running Caffeine or PowerToys Awake, wiggling the mouse with a script — all of them burn battery long after the agent has finished.

opencode-nosleep ties the sleep lock to the agent's actual state instead. Session busy → machine stays awake. Session idle → Windows is free to sleep on its normal schedule.


Why most "keep Windows awake" tricks fail on modern laptops

Nearly every keep-awake snippet you'll find uses PowerRequestSystemRequired or ES_SYSTEM_REQUIRED. On any laptop shipped in the last several years that is silently ignored.

Modern laptops use Modern Standby (S0 Low Power Idle) instead of the old S3 sleep state. Microsoft documents that PowerRequestSystemRequired is not honored on systems capable of connected standby, and that applications must use PowerRequestExecutionRequired instead. Check your own machine:

powercfg /a

If it says Standby (S0 Low Power Idle), a system-required request does nothing for you.

There's a second trap. On Modern Standby systems the machine enters standby when the display turns off — not after a separate sleep timer. So a plugin that politely lets your screen blank has already lost. Blocking sleep on S0 means keeping the display awake too.

opencode-nosleep requests both:

Request type Value What it buys you
PowerRequestExecutionRequired 3 Honored on Modern Standby; implies SystemRequired on older S3 machines
PowerRequestDisplayRequired 0 Display stays on, which is what actually blocks S0 standby entry

How it works

  1. The plugin subscribes to opencode's session.status, session.idle, session.deleted and server.instance.disposed events.
  2. When any session enters busy (or retry), it spawns one hidden Windows PowerShell helper.
  3. The helper calls PowerCreateRequest + PowerSetRequest through P/Invoke and then blocks on stdin.
  4. When every session goes idle, the plugin closes the helper's stdin. The helper hits EOF, clears the power request and exits.

Closing stdin is the whole trick for reliability: if opencode crashes or is killed, the pipe closes anyway, the helper wakes up and releases the lock. Nothing is left pinned in the Windows power request table.

Exactly one helper process runs per opencode process, no matter how many projects have the plugin loaded. Sessions are reference-counted, so two concurrent agents holding the machine awake resolve correctly when one finishes.

On macOS and Linux the plugin returns an empty hook set and does nothing.


Install

Drop the file into your global opencode plugin directory:

$dir = "$env:USERPROFILE\.config\opencode\plugins"
New-Item -ItemType Directory -Force -Path $dir | Out-Null
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/Wintego/opencode-nosleep/main/opencode-nosleep.ts" -OutFile "$dir\opencode-nosleep.ts"

Or per project, in .opencode/plugins/opencode-nosleep.ts.

Restart opencode. That's it — there is nothing to configure and no dependency to install.


Configuration

One optional environment variable:

Variable Default Purpose
OPENCODE_NOSLEEP_MAX_MINUTES 240 Failsafe. If a session stays busy this long without ever reporting idle, the plugin assumes it hung, drops the lock and logs a warning.
$env:OPENCODE_NOSLEEP_MAX_MINUTES = "120"

Verify that it works

Run an elevated PowerShell while opencode is busy:

powercfg /requests

You should see powershell.exe listed under both DISPLAY: and EXECUTION: with the reason "OpenCode: блокировка автоматического сна". Once the agent goes idle, both entries disappear.

Plugin activity is also written to the opencode log via client.app.log under the service name opencode-nosleep.


What it deliberately does not do

  • It does not stop sleep triggered by closing the lid, pressing the power button, or choosing Sleep from the Start menu. Those are explicit user intent.
  • It does not touch your power plan, registry, or any global Windows setting. Remove the file and every trace is gone.
  • It does not keep the machine awake when opencode is merely open. Idle means idle.
  • It does not prevent the lock screen from engaging on its own schedule.

FAQ

Does this work on Windows 11 with Modern Standby?

Yes — that is the specific case it was built for. It uses PowerRequestExecutionRequired, the only request type Microsoft honors on connected-standby-capable systems, plus a display request, because on S0 the machine enters standby when the screen turns off.

Will my screen stay on the whole time?

Only while a session is running. The display request is released along with everything else the moment the agent finishes.

Does it drain my battery?

Only for the duration of the task. That is the entire point of scoping the lock to agent activity instead of leaving a global "never sleep" setting on.

What happens if opencode crashes mid-task?

The helper's stdin pipe closes, it clears the power request and exits on its own. No orphaned lock, no stuck powercfg /requests entry.

Do I need administrator rights?

No. PowerCreateRequest works from a normal user process. Admin rights are only needed to inspect the request list with powercfg /requests.

Does it need Node, Bun, or any npm dependency?

No. It uses the PowerShell that ships with Windows and Node's built-in child_process.

Is macOS or Linux supported?

Not yet. The plugin no-ops off Windows. On macOS the equivalent would be caffeinate; on Linux, systemd-inhibit. PRs welcome.


Requirements

  • Windows 10 or Windows 11
  • opencode with plugin support (@opencode-ai/plugin v1.x event API)
  • Windows PowerShell 5.1 — preinstalled on every supported Windows build

Related keywords

opencode plugin · prevent Windows from sleeping · keep laptop awake during long AI agent tasks · Modern Standby S0 keep awake · PowerRequestExecutionRequired · PowerSetRequest · stop screen turning off · caffeine alternative for opencode · AI coding agent sleep fix


License

MIT

About

Keep Windows awake while opencode is running. Modern Standby aware sleep and display blocker, scoped to actual agent activity. Zero config.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages