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.
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.
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 /aIf 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 |
- The plugin subscribes to opencode's
session.status,session.idle,session.deletedandserver.instance.disposedevents. - When any session enters
busy(orretry), it spawns one hidden Windows PowerShell helper. - The helper calls
PowerCreateRequest+PowerSetRequestthrough P/Invoke and then blocks on stdin. - 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.
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.
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"Run an elevated PowerShell while opencode is busy:
powercfg /requestsYou 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.
- 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.
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.
Only while a session is running. The display request is released along with everything else the moment the agent finishes.
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.
The helper's stdin pipe closes, it clears the power request and exits on its own. No orphaned lock, no stuck powercfg /requests entry.
No. PowerCreateRequest works from a normal user process. Admin rights are only needed to inspect the request list with powercfg /requests.
No. It uses the PowerShell that ships with Windows and Node's built-in child_process.
Not yet. The plugin no-ops off Windows. On macOS the equivalent would be caffeinate; on Linux, systemd-inhibit. PRs welcome.
- Windows 10 or Windows 11
- opencode with plugin support (
@opencode-ai/pluginv1.x event API) - Windows PowerShell 5.1 — preinstalled on every supported Windows build
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
MIT