pi-session-doctor

Keeps long-lived pi sessions from freezing silently: profiles event-loop stalls in-process, tracks session health, and asks the agent to hand off and rotate when a long session starts stalling

Packages

Package details

extension

Install pi-session-doctor from npm and Pi will load the resources declared by the package manifest.

$ pi install npm:pi-session-doctor
Package
pi-session-doctor
Version
0.1.0
Published
Nov 12, 2026
Downloads
348/mo · 348/wk
Author
weihung1017
License
MIT
Types
extension
Size
44.5 KB
Dependencies
0 dependencies · 1 peer
Pi manifest JSON
{
  "extensions": [
    "./src/index.ts"
  ]
}

Security note

Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.

README

pi-session-doctor

Keeps long-lived pi sessions from freezing silently.

A pi main that runs for days collects a long history: tens of thousands of entries, dozens of compactions, large todo snapshots. Code that walks that history on every event gets slower each day, until pi stops answering keys. pi-session-doctor catches the stall while it happens, records what the main thread was running, and asks the agent to hand off to a fresh session before it gets that far.

pi install npm:pi-session-doctor

What it does

  • Profiles stalls. A watchdog thread reads a heartbeat from pi's main thread. When the heartbeat is a second late, it starts V8's sampling profiler on the main thread (inspector messages reach a busy thread through an interrupt), and when the block ends it writes the profile. A block of 3 seconds or more is a stall: its profile is kept, and pi shows a warning with the path.
  • Profiles busy stretches. Many shorter blocks in a row (the heartbeat late in half of the last 20 seconds) get a 15-second profile too.
  • Tracks health from events, never by walking the history: session file size, entry count, compactions, pi's own CPU, event-loop delay, and the stalls of this session.
  • Asks for a rotation once. When a session has a long history and shows strain, the agent gets one message at a turn boundary: write a handoff to scratch/handoff-<name>.md and ask the boss or tower to start a new session from it. It never interrupts a running tool, kills anything, or runs /new. It asks again only after the condition has cleared and crossed again.
  • /doctor prints the numbers and the top of the latest profile.

Where it runs

The watchdog costs a thread and about 18 MB, so it runs only in interactive sessions. In a subagent (PI_SUBAGENT_ID set) and in print or json mode it starts no watchdog and sends no rotation message; /doctor still answers and says the watchdog is off and why. Set PI_SESSION_DOCTOR=on or off to override either way.

Files

Everything goes to ~/.local/state/pi-session-doctor/ (or $XDG_STATE_HOME/pi-session-doctor/):

  • <session-id>-<time>.cpuprofile: the profile of one block. Open it in Chrome DevTools (Performance, load profile) or VS Code.
  • <session-id>-<time>.txt: top self time and top inclusive time per function, with file and line.
  • sessions/<session-id>.json: the session's stalls and whether the agent was already told.

The newest 20 captures are kept; older ones are deleted. Captures are at least 2 minutes apart.

pi-session-doctor capture
reason: stall
main thread blocked: 9.4 s
profiler: armed before the block began; every frame is named

Top self time:
   88.0%    8.21 s  walkBranch  file:///.../some-extension.ts:120
    6.1%    0.57 s  (garbage collector)

Thresholds

What Default Why
profiler starts heartbeat 1 s late early enough to cover most of a stall
stall 3 s below that a block is a hiccup, not a freeze
longest profile of one block 30 s the profile is written then even if the block goes on
long history 20 compactions, 10,000 entries, or a 150 MB session file any one of them
strain 3 stalls in an hour, one stall of 30 s, or pi at 60% CPU over 30 minutes pi itself idles at a few percent; tools run in child processes and do not count
rotation long history and strain size alone is a poor signal: a 455 MB session can be fast

Limits

  • Profiling is Node-only. Under Bun the watchdog still detects and records stalls (and asks for rotation), but writes no profile.
  • V8 cannot name code it compiled on stack (on-stack replacement) before the profiler started, so a loop that was already running when the profiler came in is charged to its caller. After a capture the profiler therefore stays armed for 15 minutes at 200 samples a second (about 1% of a core): a repeat stall starts under it and every frame is named. The summary says which kind of profile it is.
  • The watchdog thread costs about 0.2% of a core and 18 MB of memory.
  • A stall while the whole machine sleeps is not a stall: when the watchdog thread itself was suspended, it starts over.

License

MIT