iai-personal-memory-engine icon indicating copy to clipboard operation
iai-personal-memory-engine copied to clipboard

Daemon spawns /usr/bin/pmset -g log every ~30-40s indefinitely (lifecycle_tick idle check, no throttle/config knob)

Open aiyou550101 opened this issue 2 months ago • 2 comments

Environment

  • iai-mcp version: 3.0.8
  • OS: macOS (darwin)

Observation

The daemon spawns /usr/bin/pmset -g log as a subprocess roughly every 30-40 seconds, indefinitely, whenever it's in the WAKE lifecycle state. Confirmed via live process tracing (pgrep -x pmset polling + parent-PID lookup): every pmset invocation's direct parent is the daemon process itself (iai lilli (iai_mcp.daemon)).

Traced this to lifecycle_tick() in iai_mcp/daemon/__init__.py (LIFECYCLE_TICK_INTERVAL_SEC = 30.0, also mirrored as the module-level TICK_INTERVAL_SEC = 30), which each tick calls _idle_detector.sleep_eligible(...). On Darwin, when the HID idle threshold isn't yet met, IdleDetector.sleep_eligible() / .status() falls through to pmset_recent_sleep() in idle_detector.py, which shells out to /usr/bin/pmset -g log to scan for a recent sleep/wake event.

Real-world spacing between fresh pmset PIDs measured at ~39s (vs. the 30s tick interval), consistent with tick execution overhead on top of the base interval.

Impact

Each invocation is a real subprocess spawn plus XPC connection setup to cfprefsd (visible in log show --predicate 'process == "pmset"' as activating connection: ... name=com.apple.cfprefsd.daemon / .agent and a "Retrieve User by ID" call). Individually cheap, but this runs continuously — roughly 2,000-2,900 subprocess spawns per day per daemon instance while awake — and is a plausible contributor to periodic background CPU activity users may notice, on top of whatever else is running concurrently.

Ask

TICK_INTERVAL_SEC / LIFECYCLE_TICK_INTERVAL_SEC is a hardcoded constant with no environment-variable override (unlike some of the daemon's other tunables, e.g. IAI_MCP_S4_FIRST_ITER_GRACE_SEC). Requesting one of:

  • Cache/throttle the pmset -g log idle check (e.g. only invoke it when os_idle_time_sec() is already close to the sleep threshold, rather than every single tick), or
  • An env-var override for the tick interval so users who don't need fine-grained idle detection can reduce polling frequency, or
  • Confirmation that this is intentional/acceptable overhead, so it can be documented as expected behavior.

Happy to provide more log excerpts or test a patch.

aiyou550101 avatar Sep 10 '26 14:09 aiyou550101

Follow-up: measured CPU cost, not just subprocess-spawn overhead

Traced actual daemon CPU% (1s sampling) alongside pmset PID spawns over a 4-minute window to characterize the real cost, since my original report undersold this as "individually cheap." It isn't just a brief subprocess spawn — each tick produces a sustained multi-second CPU plateau:

PMSET 07:13:33 pid=70052
TICK 07:13:32 cpu=10.9
TICK 07:13:34 cpu=20.2
TICK 07:13:35 cpu=22.0
TICK 07:13:36 cpu=22.3
TICK 07:13:37 cpu=20.8
TICK 07:13:38 cpu=20.7
TICK 07:13:39 cpu=20.3
TICK 07:13:40 cpu=20.9
TICK 07:13:42 cpu=21.5
TICK 07:13:43 cpu=1.9
TICK 07:13:44 cpu=0.0   <- back to idle

This same shape repeated identically across 5 more cycles in the trace window, each time triggered ~39-40s apart (matches TICK_INTERVAL_SEC=30 plus tick execution overhead):

07:13:33 -> 07:14:12   (39s)
07:14:12 -> 07:14:52   (40s)
07:14:52 -> 07:15:31   (39s)
07:15:31 -> 07:16:11   (40s)
07:16:11 -> 07:16:50   (39s)

Each cycle: ~0% CPU for ~28-30s, then a ~8-10s plateau at 20-23% CPU, then back to 0%. That's roughly a 23% duty cycle (≈9s busy / 39s period) sustained continuously while the daemon is in the WAKE lifecycle state — not a one-off blip from spawning pmset, but a repeating multi-second burst of real work every tick. Whatever else lifecycle_tick() does in that window (heartbeat scanner, pending-embed-pass checks, etc.) appears to dominate the cost, with the pmset spawn just marking the start of it.

This changes the ask slightly: throttling/caching the pmset idle check alone may not be enough if the surrounding tick body is the actual cost driver. Might be worth profiling what specifically runs in that ~9s window each tick.

aiyou550101 avatar Sep 11 '26 00:09 aiyou550101

Fixed in v3.2.2 (live on PyPI as iai-pme 3.2.2). Developed/tested on macOS; Linux/Windows paths welcome from contributors.

CodeAbra avatar Sep 14 '26 01:09 CodeAbra