feat(power): keep the machine awake while Codeman runs, lid close included

A laptop that suspends freezes every agent session until it wakes. The new
opt-in setting (App Settings > System > Power, off by default, "Only on AC
power" on by default) makes the server hold an OS sleep lock for exactly as
long as it runs:

- Linux: a logind block lock on handle-lid-switch:sleep:idle through
  systemd-inhibit. The lid needs the low-level handle-lid-switch lock
  (LidSwitchIgnoreInhibited=yes ignores a plain sleep lock), and polkit grants
  it only while someone is logged in to the desktop, so a refusal is reported
  as "denied" and retried every minute.
- macOS: caffeinate -i -s -w <pid>. Lid close needs pmset disablesleep, which
  an optional root helper (scripts/keep-awake-macos.sh, a LaunchDaemon run
  from a root-owned copy) applies while the server keeps a fresh request
  file, undoing only a disablesleep it set itself.

The lock dies with the server: systemd-inhibit runs cat on a stdin pipe, so
any exit (SIGKILL included, and under the unit's KillMode=process) releases
it; caffeinate exits with the pid it watches.

The installer asks on laptops (default no, never under --yes), and
`install.sh keep-awake` turns it on for an existing install. Status is at
GET /api/system/keep-awake; a non-admin's value is dropped in multi-user mode.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-10-10 07:21:57 +02:00
1 parent 3d59e59e37
commit 233f85b170
19 files changed
+1960 -31

No files matched your search

+21
View File
@@ -3107,6 +3107,27 @@
</div>
</div>
<div class="set-group" id="keepAwakeGroup">
<div class="set-group-head"><h4>Power</h4><span class="set-scope">server</span></div>
<div class="set-group-body">
<div class="set-row" data-search="keep awake sleep suspend standby lid close laptop notebook power caffeinate inhibit">
<div class="set-row-text">
<span class="set-row-label">Keep this computer awake</span>
<span class="set-row-desc">While Codeman runs, stop the machine it runs on from sleeping, including when a laptop's lid is closed, so agents keep working and stay reachable from your phone. The screen can still turn off.</span>
</div>
<label class="switch switch-sm"><input type="checkbox" id="appSettingsKeepAwake"><span class="slider"></span></label>
</div>
<div class="set-row" data-search="keep awake battery ac power charger plugged in">
<div class="set-row-text">
<span class="set-row-label">Only on AC power</span>
<span class="set-row-desc">Sleep normally on battery. A closed laptop in a bag gets hot and drains fast.</span>
</div>
<label class="switch switch-sm"><input type="checkbox" id="appSettingsKeepAwakeAcOnly" checked><span class="slider"></span></label>
</div>
<div id="keepAwakeStatus" class="set-note" style="display:none" data-i18n-skip></div>
</div>
</div>
<div class="set-group">
<div class="set-group-head"><h4>Remote access</h4><span class="set-scope">synced</span></div>
<div class="set-group-body">
+55
View File
@@ -424,6 +424,11 @@ Object.assign(CodemanApp.prototype, {
document.getElementById('appSettingsMcpSync').checked = this._mcpSyncSavedOn;
this.applyMcpSyncVisibility();
this._applyDoctorAdminGate();
// Keep awake: server state (an OS sleep lock), default OFF; "only on AC" default ON.
document.getElementById('appSettingsKeepAwake').checked = settings.keepAwakeEnabled === true;
document.getElementById('appSettingsKeepAwakeAcOnly').checked = settings.keepAwakeAcOnly !== false;
this._applyKeepAwakeAdminGate();
this.loadKeepAwakeStatus();
this.loadWebhook();
// Read My Mind: synced, default OFF (opt-in; capture + prediction cost real tokens).
document.getElementById('appSettingsReadMyMind').checked = settings.readMyMindEnabled === true;
@@ -1224,6 +1229,53 @@ Object.assign(CodemanApp.prototype, {
group.style.display = me.multiUser && me.role !== 'admin' ? 'none' : '';
},
/**
* Keep awake changes machine state, and PUT /api/settings drops a non-admin's value in
* multi-user mode, so a non-admin gets no Power group at all rather than a switch that
* silently does nothing. Also wired to `codeman:me` for the late-resolving role.
*/
_applyKeepAwakeAdminGate() {
const group = document.getElementById('keepAwakeGroup');
if (!group) return;
const me = window.__codemanUser || {};
group.style.display = me.multiUser && me.role !== 'admin' ? 'none' : '';
},
/** One line on what the sleep lock is doing right now (GET /api/system/keep-awake). */
async loadKeepAwakeStatus() {
const out = document.getElementById('keepAwakeStatus');
if (!out) return;
let s = null;
try {
const res = await this._api('/api/system/keep-awake');
const body = res && res.ok ? await res.json() : null;
s = body?.success ? body.data : null;
} catch { /* leave hidden */ }
const text = s ? this.describeKeepAwakeStatus(s) : '';
out.textContent = text;
out.style.display = text ? 'block' : 'none';
},
/** Status → sentence. Pure; '' hides the note. */
describeKeepAwakeStatus(s) {
switch (s.state) {
case 'off': return '';
case 'starting': return 'Starting…';
case 'paused-battery': return 'Paused: running on battery. It comes back when you plug in.';
case 'unavailable': return `Not available here: ${s.detail || 'no supported sleep lock on this system.'}`;
case 'denied': return `${s.detail || 'The sleep lock was refused.'} Codeman retries every minute, so it applies once you log in.`;
case 'failed': return `Could not take the sleep lock (${s.detail || 'unknown error'}). Retrying every minute.`;
case 'active':
if (s.platform === 'macos') {
return s.lidHelper === 'installed'
? 'Active: this Mac will not sleep while Codeman runs, even with the lid closed. Apple menu > Sleep is blocked too.'
: 'Active for idle sleep only: closing the lid still puts this Mac to sleep. To cover the lid, re-run the installer and answer yes to the lid question (asks for your admin password once).';
}
return "Active: closing the lid will not suspend this machine while Codeman runs. Your desktop's own Automatic Suspend timer is separate and still runs: if it is on, switch it off for when the machine is plugged in.";
default: return '';
}
},
/** Preview (apply=false) or run (apply=true) the MCP server sync across enabled CLIs. */
async mcpSync(apply) {
const out = this.$('mcpSyncResult');
@@ -2633,6 +2685,8 @@ Object.assign(CodemanApp.prototype, {
agentTeamsEnabled: document.getElementById('appSettingsAgentTeams').checked,
agentSkillEnabled: document.getElementById('appSettingsAgentSkill').checked,
workspaceHooksEnabled: document.getElementById('appSettingsWorkspaceHooks').checked,
keepAwakeEnabled: document.getElementById('appSettingsKeepAwake').checked,
keepAwakeAcOnly: document.getElementById('appSettingsKeepAwakeAcOnly').checked,
claudeVoiceEnabled: document.getElementById('appSettingsClaudeVoice').checked,
claudeModel: document.getElementById('appSettingsClaudeModel').value,
opusContext1mEnabled: document.getElementById('appSettingsOpusContext1m').checked,
@@ -4766,4 +4820,5 @@ document.addEventListener?.('codeman:me', () => {
window.app?._applyCliManagementAdminGate?.();
window.app?._applyMcpSyncAdminGate?.();
window.app?._applyDoctorAdminGate?.();
window.app?._applyKeepAwakeAdminGate?.();
});