Background tasks for pi — long-running shell processes without blocking
README.md

pi-background #

Background tasks for pi 1.0+. Start long-running shell processes (crit instances, dev servers, builds) without blocking the conversation, check on them on demand, and get woken up automatically when they finish — no polling, ever.

Status: v0.1.1 — working.

Usage (agent-facing) #

  • bg_run { command, name?, timeoutSeconds?, notifyOnCompletion?, triggerOnCompletion? } → starts immediately, returns id + log path.
  • bg_status { taskId? } → one task or the table. Point-in-time; never poll.
  • bg_logs { taskId, tail?, maxBytes? } → bounded tail-first read; full path on truncation.
  • bg_kill { taskId } → SIGTERM group → grace → SIGKILL, verified.
  • /bg → plain text table.

Completions wake the agent automatically (queued as a follow-up turn if busy). What happens to tasks when the agent stops is spelled out under Lifecycle.

Surface #

  • bg_run — start a named shell command; returns immediately.
  • bg_status — point-in-time liveness/status snapshot.
  • bg_logs — bounded (tail-first) log read, full path on truncation.
  • bg_kill — stop a task by id or unambiguous prefix.
  • /bg — plain-text task table.

Lifecycle #

Tasks are children of the pi process, each in its own process group.

  • /reload — the registry is parked and re-adopted by the next session in the same process: running tasks keep running and stay tracked.
  • Graceful quit (/exit, any other session end) — every running group gets SIGTERM, then SIGKILL after a grace period (default 5 s, PI_BG_STOP_GRACE_MS). Stopped tasks finalize as killed with the delivered signal; timeout/cap stops finalize as failed.
  • Signals to pi (SIGHUP hangup, SIGINT, SIGTERM) — a sweep stops all running groups exactly like a graceful quit. Closing the terminal tab or a herdr server stop arrives as SIGHUP.
  • kill -9 of pi — nothing can intercept it, so child groups survive, orphaned. This is the one documented leak.

Group-wide signals cover descendants too; if a group's death cannot be proven afterwards, the task is marked failed with "descendants may have leaked" rather than claiming a clean kill.

Install #

pi install git:git@tangled.org:crooy.tngl.sh/pi-background

(The repo is also reachable as git@tangled.org:did:plc:2ryrvovshmjrxp46har26xts, but pi's git-source parser requires a host/path form, so use the handle spelling for pi install.)

Acknowledgements #

A simpler, more limited version of pi-background-tasks, independently implemented for pi 1.x — no code is shared.

License #

MIT