@narumitw/pi-caffeinate 0.49.6 → 0.49.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -95
- package/dist/index.ts +10 -41
- package/dist/index.ts.map +2 -2
- package/package.json +50 -54
- package/src/caffeinate.ts +637 -663
- package/src/dbus-inhibit.ts +114 -120
- package/src/inhibitor-process.ts +29 -29
- package/src/inhibitors.ts +108 -126
- package/src/settings.ts +124 -128
package/README.md
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@narumitw/pi-caffeinate) [](https://pi.dev) [](./LICENSE)
|
|
4
4
|
|
|
5
|
-
Prevent system or display sleep while Pi is
|
|
5
|
+
Prevent system or display sleep while Pi is running an agent task, then release the inhibitor when the run ends.
|
|
6
6
|
|
|
7
7
|
## ✨ Features
|
|
8
8
|
|
|
9
9
|
- Starts an OS sleep inhibitor when a Pi run begins and releases it when the run or session ends.
|
|
10
|
-
- Supports macOS, Windows, WSL, and Linux with
|
|
11
|
-
- Provides `/caffeinate` controls for
|
|
10
|
+
- Supports macOS, Windows, WSL, and Linux, with display-awake as the default.
|
|
11
|
+
- Provides `/caffeinate` controls for keep-awake mode and status.
|
|
12
12
|
- Persists preferences locally and accepts an optional custom inhibitor command.
|
|
13
|
-
-
|
|
13
|
+
- Falls back when possible, warns on partial activation, and reports when no inhibitor is available.
|
|
14
14
|
|
|
15
15
|
## 📦 Install
|
|
16
16
|
|
|
@@ -32,19 +32,18 @@ pi -e ./packages/pi-caffeinate
|
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
The package declares `dist/index.ts`, so an unbuilt local checkout must be built before Pi loads the package directory.
|
|
35
|
+
Pi extensions run with the Pi process's user permissions, so install only trusted packages.
|
|
35
36
|
|
|
36
37
|
## 🚀 Quick start
|
|
37
38
|
|
|
38
39
|
Load the extension and use Pi normally.
|
|
39
|
-
During
|
|
40
|
-
Run `/caffeinate`
|
|
40
|
+
During each agent run, pi-caffeinate uses the saved mode and defaults to keeping the system and display awake.
|
|
41
|
+
Run `/caffeinate` for controls or `/caffeinate status` for the current state.
|
|
41
42
|
|
|
42
43
|
## 🖥️ Supported platforms
|
|
43
44
|
|
|
44
|
-
The default
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
Use `/caffeinate sleep` if you want to prevent system sleep while allowing normal display idle behavior such as screen blanking or monitor power-off.
|
|
45
|
+
The default `display` mode prevents system sleep, suspend, or hibernate and keeps the display awake.
|
|
46
|
+
Use `/caffeinate sleep` to prevent system sleep while allowing normal screen blanking or monitor power-off.
|
|
48
47
|
|
|
49
48
|
| Platform | `sleep` mode | `display` mode, default |
|
|
50
49
|
| --- | --- | --- |
|
|
@@ -54,58 +53,31 @@ Use `/caffeinate sleep` if you want to prevent system sleep while allowing norma
|
|
|
54
53
|
| Linux with systemd | `systemd-inhibit --what=sleep ... sleep infinity` | D-Bus `org.freedesktop.ScreenSaver.Inhibit` + `systemd-inhibit --what=idle:sleep ... sleep infinity` |
|
|
55
54
|
| Linux without systemd | `caffeinate -ims` when available | D-Bus `org.freedesktop.ScreenSaver.Inhibit` + `caffeinate -dimsu` when available; D-Bus only otherwise |
|
|
56
55
|
|
|
57
|
-
On Linux, `display` mode requests idle inhibition
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
`systemd-inhibit --what=idle:sleep` runs alongside
|
|
61
|
-
If
|
|
62
|
-
If only D-Bus is available,
|
|
63
|
-
D-Bus
|
|
56
|
+
On Linux, `display` mode requests idle inhibition from `org.freedesktop.ScreenSaver` over D-Bus.
|
|
57
|
+
It tries `/org/freedesktop/ScreenSaver` and `/ScreenSaver` for desktop compatibility and keeps the session-bus connection open for the agent turn.
|
|
58
|
+
Calling `UnInhibit` or closing the connection releases the request.
|
|
59
|
+
`systemd-inhibit --what=idle:sleep` runs alongside D-Bus to preserve logind idle and sleep inhibition.
|
|
60
|
+
If the ScreenSaver service is unavailable, pi-caffeinate keeps the systemd or `caffeinate` blocker and warns that activation is partial.
|
|
61
|
+
If only D-Bus is available, it warns that direct system suspend may remain possible.
|
|
62
|
+
D-Bus calls have 2-second deadlines, and stop or shutdown aborts an in-progress acquisition before closing the connection.
|
|
64
63
|
|
|
65
64
|
If no supported inhibitor is available, the extension stays loaded and reports that caffeinate is unavailable.
|
|
66
65
|
|
|
67
66
|
## 💬 Commands
|
|
68
67
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
Keeps the system and screen/display awake.
|
|
81
|
-
If an inhibitor is currently active, it is restarted so the new mode applies immediately.
|
|
82
|
-
|
|
83
|
-
```text
|
|
84
|
-
/caffeinate sleep
|
|
85
|
-
```
|
|
68
|
+
| Command | Purpose |
|
|
69
|
+
| --- | --- |
|
|
70
|
+
| `/caffeinate` | Open keep-awake controls. |
|
|
71
|
+
| `/caffeinate display` (alias: `screen`) | Keep the system and display awake. |
|
|
72
|
+
| `/caffeinate sleep` (alias: `system`) | Keep the system awake while allowing display sleep. |
|
|
73
|
+
| `/caffeinate status` | Show inhibitor state, mode, quiet mode, and settings path. |
|
|
74
|
+
| `/caffeinate mode` (aliases: `config`, `settings`) | Choose the keep-awake mode. |
|
|
75
|
+
| `/caffeinate stop` (alias: `off`) | Release the inhibitor until the next agent run. |
|
|
76
|
+
| `/caffeinate help` | Show command usage. |
|
|
86
77
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
```text
|
|
91
|
-
/caffeinate status
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
Shows whether an inhibitor is active, unavailable, disabled, or idle.
|
|
95
|
-
The status includes the current mode, quiet mode, and settings file path.
|
|
96
|
-
|
|
97
|
-
```text
|
|
98
|
-
/caffeinate mode
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
Opens the standard keep-awake mode selector in TUI or RPC mode.
|
|
102
|
-
Escape closes the selector.
|
|
103
|
-
|
|
104
|
-
```text
|
|
105
|
-
/caffeinate stop
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
Releases any active inhibitor until Pi starts another agent run.
|
|
78
|
+
All routes support TUI and RPC and reject unknown or trailing arguments.
|
|
79
|
+
Print and JSON modes reject the menu and mode selector; other direct routes run but do not display notification feedback.
|
|
80
|
+
Changing to `display` or `sleep` restarts an active inhibitor immediately.
|
|
109
81
|
|
|
110
82
|
## ⚙️ Settings
|
|
111
83
|
|
|
@@ -127,21 +99,23 @@ Example:
|
|
|
127
99
|
}
|
|
128
100
|
```
|
|
129
101
|
|
|
130
|
-
Set `"quiet": true` to hide
|
|
131
|
-
Quiet mode does not hide warnings or explicit feedback
|
|
102
|
+
Set `"quiet": true` to hide routine start and release notifications and clear the `caffeinate` status item while active or unavailable.
|
|
103
|
+
Quiet mode does not hide warnings or explicit command feedback.
|
|
132
104
|
It defaults to `false` when omitted.
|
|
133
|
-
The file is read at startup and on `/reload
|
|
105
|
+
The file is read at startup and on `/reload`.
|
|
106
|
+
After editing it in a running session, run `/reload` before using mode commands.
|
|
134
107
|
|
|
135
|
-
Missing, invalid, or deleted settings
|
|
108
|
+
Missing, invalid, or deleted settings use `display` mode with quiet mode off.
|
|
136
109
|
A missing file stays absent until the first successful mode change.
|
|
137
110
|
Within one Pi process, mode saves run in invocation order, reread the latest valid document, and preserve unknown fields.
|
|
138
|
-
Malformed JSON or an invalid recognized field blocks
|
|
139
|
-
A failed save keeps the
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
111
|
+
Malformed JSON or an invalid recognized field blocks saves until repaired.
|
|
112
|
+
A failed save keeps the previous runtime mode.
|
|
113
|
+
If applying a published mode fails while an inhibitor is active, the extension restores the previous saved mode and inhibitor or reports a rollback failure.
|
|
114
|
+
|
|
115
|
+
Older versions used `pi-caffeinate-settings.json`.
|
|
116
|
+
A legacy-only file remains readable with a warning and is not modified automatically; rename it to `pi-caffeinate.json`.
|
|
117
|
+
The next settings save writes the canonical file.
|
|
118
|
+
If both files exist, `pi-caffeinate.json` takes precedence.
|
|
145
119
|
The legacy filename is deprecated and will be removed in a future major release.
|
|
146
120
|
|
|
147
121
|
### Environment variables
|
|
@@ -158,8 +132,8 @@ Use a custom inhibitor command:
|
|
|
158
132
|
PI_CAFFEINATE_COMMAND='systemd-inhibit --what=idle:sleep --why="pi running" --mode=block sleep infinity' pi
|
|
159
133
|
```
|
|
160
134
|
|
|
161
|
-
The custom command
|
|
162
|
-
`PI_CAFFEINATE_COMMAND`
|
|
135
|
+
The custom command uses shell-like argument parsing but runs directly without a shell.
|
|
136
|
+
`PI_CAFFEINATE_COMMAND` overrides the saved mode, and `/caffeinate status` reports the override.
|
|
163
137
|
|
|
164
138
|
Deprecated: `PI_CAFFEINATE_ICON` still works for now.
|
|
165
139
|
If you use `@narumitw/pi-statusline`, move the icon to `${PI_CODING_AGENT_DIR:-~/.pi/agent}/pi-statusline.json`:
|
|
@@ -173,42 +147,27 @@ If you use `@narumitw/pi-statusline`, move the icon to `${PI_CODING_AGENT_DIR:-~
|
|
|
173
147
|
```
|
|
174
148
|
|
|
175
149
|
Without `@narumitw/pi-statusline`, keep using `PI_CAFFEINATE_ICON` during the compatibility window.
|
|
176
|
-
In `pi-statusline.json`, use an empty string to show
|
|
150
|
+
In `pi-statusline.json`, use an empty string to show caffeinate status without an icon.
|
|
177
151
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
AI coding agents often run tool-heavy tasks that take several minutes.
|
|
181
|
-
`pi-caffeinate` keeps your machine awake during active Pi work, helping browser automation, local builds, test runs, code generation, and long prompts finish reliably.
|
|
182
|
-
|
|
183
|
-
The default display-awake mode prioritizes uninterrupted long-running Pi work across platforms, including Linux desktops that require idle inhibition to prevent automatic suspend.
|
|
184
|
-
Use `/caffeinate sleep` (shown as `system-awake` in status output) when you prefer normal screen power saving and your system does not need idle inhibition to keep Pi running.
|
|
152
|
+
Status output calls `display` mode `display-awake` and `sleep` mode `system-awake`.
|
|
185
153
|
|
|
186
154
|
## 📦 Dependencies
|
|
187
155
|
|
|
188
|
-
On Linux, `display` mode uses the `dbus-native` package
|
|
156
|
+
On Linux, `display` mode uses the pure-JavaScript `dbus-native` package to call `org.freedesktop.ScreenSaver` on the session bus.
|
|
189
157
|
|
|
190
158
|
## 🗂️ Package layout
|
|
191
159
|
|
|
192
|
-
```
|
|
160
|
+
```text
|
|
193
161
|
packages/pi-caffeinate/
|
|
194
|
-
├── src/
|
|
195
|
-
│ ├── index.ts
|
|
196
|
-
│
|
|
197
|
-
|
|
198
|
-
├──
|
|
199
|
-
|
|
200
|
-
│ └── build-runtime.mjs
|
|
201
|
-
├── test/
|
|
202
|
-
│ ├── build-runtime.test.ts
|
|
203
|
-
│ └── caffeinate.test.ts
|
|
204
|
-
├── README.md
|
|
205
|
-
├── LICENSE
|
|
206
|
-
├── tsconfig.json
|
|
207
|
-
└── package.json
|
|
162
|
+
├── src/ # Authoritative implementation and helpers
|
|
163
|
+
│ ├── index.ts # Thin Pi entrypoint
|
|
164
|
+
│ └── caffeinate.ts # Sleep inhibitors and lifecycle
|
|
165
|
+
├── dist/ # Generated Jiti runtime
|
|
166
|
+
├── scripts/build-runtime.mjs # Runtime builder
|
|
167
|
+
└── test/ # Behavior and lifecycle coverage
|
|
208
168
|
```
|
|
209
169
|
|
|
210
|
-
`src/index.ts`
|
|
211
|
-
The other source modules are internal.
|
|
170
|
+
The generated runtime is built from `src/index.ts` and does not import back into `src`.
|
|
212
171
|
|
|
213
172
|
## 🔎 Keywords
|
|
214
173
|
|
package/dist/index.ts
CHANGED
|
@@ -34,9 +34,7 @@ var NativeScreenSaverClient = class {
|
|
|
34
34
|
};
|
|
35
35
|
handleConnectionClose = (error) => {
|
|
36
36
|
if (this.closing || this.cookie === void 0) return;
|
|
37
|
-
this.reportFailure(
|
|
38
|
-
toError(error, this.lastConnectionError?.message ?? "D-Bus session connection closed")
|
|
39
|
-
);
|
|
37
|
+
this.reportFailure(toError(error, this.lastConnectionError?.message ?? "D-Bus session connection closed"));
|
|
40
38
|
};
|
|
41
39
|
setFailureHandler(handler) {
|
|
42
40
|
this.failureHandler = handler;
|
|
@@ -176,11 +174,7 @@ function getInhibitorCommand(mode) {
|
|
|
176
174
|
if (command) return { command, args, description: `custom command (${command})`, custom: true };
|
|
177
175
|
}
|
|
178
176
|
if (process2.platform === "darwin") {
|
|
179
|
-
return parentBoundUnixCommand(
|
|
180
|
-
"caffeinate",
|
|
181
|
-
macCaffeinateArgs(mode),
|
|
182
|
-
caffeinateDescription(mode)
|
|
183
|
-
);
|
|
177
|
+
return parentBoundUnixCommand("caffeinate", macCaffeinateArgs(mode), caffeinateDescription(mode));
|
|
184
178
|
}
|
|
185
179
|
if (process2.platform === "linux") {
|
|
186
180
|
if (isWsl() && commandExists("powershell.exe")) {
|
|
@@ -190,14 +184,7 @@ function getInhibitorCommand(mode) {
|
|
|
190
184
|
const what = mode === "display" ? "idle:sleep" : "sleep";
|
|
191
185
|
return parentBoundUnixCommand(
|
|
192
186
|
"systemd-inhibit",
|
|
193
|
-
[
|
|
194
|
-
`--what=${what}`,
|
|
195
|
-
"--who=pi-caffeinate",
|
|
196
|
-
"--why=Pi agent is running",
|
|
197
|
-
"--mode=block",
|
|
198
|
-
"sleep",
|
|
199
|
-
"infinity"
|
|
200
|
-
],
|
|
187
|
+
[`--what=${what}`, "--who=pi-caffeinate", "--why=Pi agent is running", "--mode=block", "sleep", "infinity"],
|
|
201
188
|
`systemd-inhibit (${formatMode(mode)})`,
|
|
202
189
|
mode === "display"
|
|
203
190
|
);
|
|
@@ -223,14 +210,7 @@ function caffeinateDescription(mode) {
|
|
|
223
210
|
function parentBoundUnixCommand(command, args, description, addDbusIdleInhibit = false) {
|
|
224
211
|
return {
|
|
225
212
|
command: "sh",
|
|
226
|
-
args: [
|
|
227
|
-
"-c",
|
|
228
|
-
unixParentBoundScript(),
|
|
229
|
-
"pi-caffeinate-watch",
|
|
230
|
-
String(process2.pid),
|
|
231
|
-
command,
|
|
232
|
-
...args
|
|
233
|
-
],
|
|
213
|
+
args: ["-c", unixParentBoundScript(), "pi-caffeinate-watch", String(process2.pid), command, ...args],
|
|
234
214
|
description,
|
|
235
215
|
...addDbusIdleInhibit ? { addDbusIdleInhibit: true } : {}
|
|
236
216
|
};
|
|
@@ -431,11 +411,8 @@ async function saveSettingsNow(settings, operations) {
|
|
|
431
411
|
await mkdir(dirname(filePath), { recursive: true });
|
|
432
412
|
const tempFile = `${filePath}.${process3.pid}.${randomUUID()}.tmp`;
|
|
433
413
|
try {
|
|
434
|
-
await (operations.write ?? DEFAULT_FILE_OPERATIONS.write)(
|
|
435
|
-
|
|
436
|
-
`${JSON.stringify(nextDocument, null, 2)}
|
|
437
|
-
`
|
|
438
|
-
);
|
|
414
|
+
await (operations.write ?? DEFAULT_FILE_OPERATIONS.write)(tempFile, `${JSON.stringify(nextDocument, null, 2)}
|
|
415
|
+
`);
|
|
439
416
|
if (!replaceCanonical && await pathEntryExists(filePath)) {
|
|
440
417
|
throw new Error(`${NEW_SETTINGS_FILE} was created concurrently; reopen settings and retry.`);
|
|
441
418
|
}
|
|
@@ -681,9 +658,7 @@ async function setModeNow(ctx, mode, generation) {
|
|
|
681
658
|
if (generation !== state.sessionGeneration) return;
|
|
682
659
|
const previousMode = state.mode;
|
|
683
660
|
const previousQuiet = state.quiet;
|
|
684
|
-
const restartRequired = Boolean(
|
|
685
|
-
hasActiveInhibitor() && previousMode !== mode && !state.command?.custom
|
|
686
|
-
);
|
|
661
|
+
const restartRequired = Boolean(hasActiveInhibitor() && previousMode !== mode && !state.command?.custom);
|
|
687
662
|
state.settingsError = void 0;
|
|
688
663
|
if (restartRequired) {
|
|
689
664
|
state.mode = mode;
|
|
@@ -758,9 +733,7 @@ function parseCommand(args) {
|
|
|
758
733
|
function commandCompletions(prefix) {
|
|
759
734
|
const normalized = prefix.trimStart().toLowerCase();
|
|
760
735
|
if (/\s/.test(normalized)) return null;
|
|
761
|
-
const matches = COMMAND_COMPLETIONS.filter(
|
|
762
|
-
(completion) => completion.value.startsWith(normalized)
|
|
763
|
-
);
|
|
736
|
+
const matches = COMMAND_COMPLETIONS.filter((completion) => completion.value.startsWith(normalized));
|
|
764
737
|
return matches.length > 0 ? matches : null;
|
|
765
738
|
}
|
|
766
739
|
function buildCommandGuide() {
|
|
@@ -1028,9 +1001,7 @@ function describeState() {
|
|
|
1028
1001
|
const activeParts = [];
|
|
1029
1002
|
if (state.dbus) activeParts.push(`D-Bus idle inhibit (${SCREENSAVER_BUS_NAME})`);
|
|
1030
1003
|
if (state.process && state.command) activeParts.push(state.command.description);
|
|
1031
|
-
lines.unshift(
|
|
1032
|
-
`pi-caffeinate is active using ${activeParts.join(" + ") || "an inhibitor"} for ${seconds}s.`
|
|
1033
|
-
);
|
|
1004
|
+
lines.unshift(`pi-caffeinate is active using ${activeParts.join(" + ") || "an inhibitor"} for ${seconds}s.`);
|
|
1034
1005
|
if (state.inhibitWarning) lines.push(`Inhibitor warning: ${state.inhibitWarning}`);
|
|
1035
1006
|
return lines.join("\n");
|
|
1036
1007
|
}
|
|
@@ -1038,9 +1009,7 @@ function describeState() {
|
|
|
1038
1009
|
lines.unshift(`pi-caffeinate is unavailable: ${state.lastError ?? "unknown reason"}`);
|
|
1039
1010
|
return lines.join("\n");
|
|
1040
1011
|
}
|
|
1041
|
-
lines.unshift(
|
|
1042
|
-
"pi-caffeinate is idle and will keep the computer awake during the next agent run."
|
|
1043
|
-
);
|
|
1012
|
+
lines.unshift("pi-caffeinate is idle and will keep the computer awake during the next agent run.");
|
|
1044
1013
|
return lines.join("\n");
|
|
1045
1014
|
}
|
|
1046
1015
|
function statusLevel() {
|