← Catalog

pig-pwr

Keep the computer awake while a Pig run is active: SetThreadExecutionState on Windows, caffeinate on macOS, systemd-inhibit on Linux.

PackageCommunity listing

Install

Install with PiG

pig install npm:pig-pwr
Review before installing

Packages run code with your permissions and influence agent behavior. Catalog inclusion is not a security audit. Review the source before installing a third-party package.

Pi manifest JSON

The pi field of package.json.

{
  "extensions": [
    "extensions/*"
  ]
}

README

Written by the package's publisher, shown as published in version 0.2.0 (README.md). PiG does not review it.

pig-pwr

Keep the computer awake while a Pig run is active — an agent turn, or a background task pig-bg-tasks is still running — so an automatic idle sleep cannot interrupt work that is still going. Released the moment Pig goes idle and pig-bg-tasks reports nothing running — and on every exit path, so the machine sleeps normally once the agent is gone.

A PiG extension written in Go, published to npm as a PiG Package.

Platform support

PlatformBackendSystem sleepDisplay keep-alive
WindowsSetThreadExecutionState (kernel32)yesyes (display = true)
macOScaffeinate -i (-d with display)yesyes (display = true)
Linuxsystemd-inhibit --what=sleep:idleyesno (logind has no display inhibitor; a display request is dropped with one warning)
anything elsenoneno-opno-op

Every backend fails soft: a missing tool or an unsupported OS logs one warning at session_start and the extension stays a no-op. pig-pwr never breaks the run it watches.

Install

From npm, once published:

pig install npm:pig-pwr

From a checkout:

pig install ./pig-pwr            # records it in your settings
pig -e ./pig-pwr/extensions/pig-pwr   # or load it for one run

Then start a session (or /reload) and check the footer: while a run is active — or pig-bg-tasks reports a running task — the status line shows the ⚡ icon, and it clears as soon as everything settles.

A Go extension builds on first use, so the host needs a Go toolchain (pig setup go installs one). Later loads come from the build cache.

Settings

Optional, read from the effective settings object (global or project) under the top-level pig-pwr key:

{
  "pig-pwr": {
    "enabled": true,
    "display": false
  }
}
KeyDefaultMeaning
enabledtrueMaster switch. false makes every handler a no-op.
displayfalseAlso keep the display on, where the backend supports it.

Missing or wrongly typed values keep the defaults, so a bad setting cannot break a session.

Behavior contract

TriggerAction
session_startarm the mechanism; warn once when this OS has no backend; try to subscribe to pig-bg-tasks' task-busy notifications for this session
agent_startadd the run as a hold reason: block idle system suspend (and display, if enabled)
agent_settleddrop that reason (a run settles on abort and error too)
task-busy busy=true (subscription)add pig-bg-tasks' busy state as a second hold reason
task-busy busy=false, or the link closesdrop that reason; the run's reason is untouched
session_shutdown, /reloadstop the subscription, clear every reason, and join the owner goroutine
process exitthe OS clears it: Windows drops the thread's state, macOS/Linux children watch the extension's pid and exit with it

"Working" means a run is active, agent_start → agent_settled, including long tool executions — or pig-bg-tasks has at least one task running. The two reasons are combined before they reach the platform: releasing one never releases the other, and only the last reason down lets the machine sleep.

pig-bg-tasks busy-watch

The background-task reason comes from the pig-bg-tasks plugin's link socket, and the subscription itself is the detection — pig-pwr never inspects installs or version numbers:

  1. At session_start it derives this session's socket the way the plugin does (<os temp dir>/pig-sandbox-<safe session id>.sock) and dials it for up to 30 s — the plugin starts its listener inside its own session_start, whose order relative to pig-pwr's is the host's to choose.
  2. It sends {"v":1,"op":"watch-tasks"} and reads the ack. An ok:true reply (carrying the current busy snapshot) means the plugin is installed and its version supports the subscription, so pig-pwr proceeds: the the machine immediately, and every following {"event":"task-busy"} frame adds or drops the reason.
  3. Anything else means no background-task hold and nothing else changes: no socket (plugin not installed, link down) after the dial window, a refusal such as unknown op "watch-tasks" (plugin older than 0.3.5), or a malformed ack. The reason is logged to stderr, the run's hold is unaffected, and pig-pwr stays exactly as it was before.
  4. A closed subscription (session end, plugin reload) drops the reason: a busy state nobody can retract must not keep the machine up.

internal/bgtasks speaks that wire; its tests cover the handshake against a real AF_UNIX server, and the refusal path was exercised against a live 0.3.4 link. Against a real 0.3.5 session:

PIG_PWR_LIVE_LINK=1 go test -run TestLiveLink -v ./internal/bgtasks
# SUBSCRIBED pig-sandbox-<id>.sock busy=false
# EVENT pig-sandbox-<id>.sock busy=true     # a task started
# EVENT pig-sandbox-<id>.sock busy=false    # it finished

What it does not do

  • It resets the idle timer and blocks automatic suspend. It does not override user-initiated sleep, lid close, or power policy.
  • No forced-awake override: once Pig is idle, the machine sleeps normally.
  • The hold is per-process-lifetime: a pig that is SIGKILLed still releases within seconds on macOS and Linux (the child watches the pid), and immediately on Windows (the state lives on a thread that dies with it).

Development

npm test                                  # keeper/reasons/link tests (offline)
npm run vet
go -C extensions/pig-pwr vet ./internal/...          # same as npm run vet

# cross-compile every backend from any host
for t in windows/amd64 darwin/arm64 linux/amd64 freebsd/amd64; do
  GOOS=${t%/*} GOARCH=${t#*/} go -C extensions/pig-pwr build ./internal/...
done

pig install ./extensions/pig-pwr --validate-only --json   # build + register check
pig package validate . --json                             # Package manifest check

Layout:

package.json                      # npm Package: keyword pig-package, pi.extensions
extensions/pig-pwr/               # the Go extension (identity: pig-pwr)
  extension.go                    # factory: event wiring, combined hold, status
  taskwatch.go                    # pig-bg-tasks task-busy subscription + reader
  internal/awake/                 # platform backends + keeper goroutine
    keeper.go                     # the one goroutine that may touch the OS
    engine.go                     # arm/acquire/release/stop, fail-soft
    reasons.go                    # combine run + task reasons into the one hold
    platform_windows.go           # SetThreadExecutionState on a locked OS thread
    platform_darwin.go            # caffeinate child + pid watch
    platform_linux.go             # systemd-inhibit child + pid watch
    platform_other.go             # probe fails, everything no-ops
    awake_test.go                 # call-sequence tests with a fake backend
    reasons_test.go               # reason-combination contract
  internal/bgtasks/               # the pig-bg-tasks link subscription client
    bgtasks.go                    # endpoint derivation, handshake, event stream
    bgtasks_test.go               # fake link server: ack, flips, refusal, absence
    live_test.go                  # manual probe of a real session (opt-in)

Publish

npm test && pig package validate .
pig package publish . --to npm            # dry run: validates and shows npm's tarball
pig package publish . --to npm --yes      # npm publish, your .npmrc and scripts apply

Plain npm publish works too; prepublishOnly runs the tests and pig package validate first. The package carries the pig-package keyword, so the pi-in-go.dev catalog picks it up on its next daily build. Add repository/bugs/homepage to package.json before enabling npm trusted publishing (provenance needs a repository).

License

MIT