← Catalog

@dis446/pi-bg-tasks

Background bash tasks for pi: detached commands, progress reporting, wake-on-completion and a live task card

Extension

Install

Install with PiG

pig install npm:@dis446/pi-bg-tasks

Install with Pi

pi install npm:@dis446/pi-bg-tasks
Review before installing

Packages run code with your permissions and influence agent behavior. Catalog inclusion is not a security audit. Review the source before installing a third-party package.

Video

Declared by the package manifest. Open the file ↗

Pi manifest JSON

The pi field of package.json.

{
  "image": "https://github.com/dis446/pi-bg-tasks/raw/refs/heads/main/docs/cover.png",
  "video": "https://github.com/dis446/pi-bg-tasks/raw/refs/heads/main/docs/card-demo.mp4",
  "extensions": [
    "./index.ts"
  ]
}

README

Written by the package's publisher, shown as published in version 1.0.1 (README.md). PiG does not review it.

pi-bg-tasks

npm version Node License: MIT test

Background tasks for pi. Long-running commands keep running after the tool call returns, report progress into a live task card, and wake the session when they finish.

The model stops burning turns on sleep loops: start the work, keep going, get told when it is done.

⏳ Background · 1 running
◌ cargo test  ██▌░░░  42%  running integration tests
✓ cargo build  ██████ 100%

Features

  • Detached by default. Every bash command runs in its own process group with output written to a task file, so a task survives a pi reload, /resume or pi exiting entirely.
  • Handoff instead of timeout. A foreground command that runs past the handoff budget is adopted — not killed, not re-run — and the model gets its task id.
  • Progress that means something. Explicit PI_PROGRESS / PI_CHECKPOINT markers, plus recognition of ordinary 42%, 7/20, 1.5/4 GiB and Compiling … output.
  • Delivery you control. notify queues the result for the next turn, wake starts one immediately, stall_wake_seconds wakes you when a task goes silent.
  • A card, not a log. Running, completed and failed tasks with a percentage bar; completions collapse to one line in the transcript.
  • A wait_for primitive. Poll any condition (a pipeline, a rollout, an HTTP probe) in the background and get woken when it holds.

Requirements

  • pi 1.5 or newer (@earendil-works/pi-coding-agent). The extension uses tool outputSchema, registerMessageRenderer, registerShortcut and the widget requestRender API.
  • Node 22.19 or newer, matching pi's own engine floor.
  • Linux or macOS. The runner uses POSIX process groups; Windows is untested.

Install

pi install npm:@dis446/pi-bg-tasks

Or from a checkout, without touching your settings:

pi -e npm:@dis446/pi-bg-tasks      # one run
pi install ./pi-bg-tasks           # local directory

The extension overrides pi's built-in bash tool and adds bg and wait_for. Nothing else is required — no build step, no external dependency.

Try this first

You do not need to learn the tools or the slash commands. After installing, ask pi in plain language:

Run the test suite in the background and tell me when it finishes.
Start the dev server in the background, then fix the failing auth test.
Wait for the uat rollout to finish, then run the smoke tests.
What is still running? Show me the tail of the build output.

Pi decides whether to call bash with run_in_background, bg or wait_for. The card appears once something is running.

Usage

Long work, started and awaited later:

bash { command: "cargo test 2>&1 | tail -5", run_in_background: true }
→ Started background task 3f9kd2ab (cargo test 2>&1 | tail -5). Output: /tmp/pi-bg-tasks/3f9kd2ab.output

… the model does other things …

bg { action: "wait", timeout_seconds: 300 }
→ Wait finished. Background task 3f9kd2ab completed in 4m12s. Output preview: …

Waiting on something external:

wait_for {
  command: "kubectl -n uat rollout status deploy/api --timeout=5s",
  description: "uat api rollout",
  interval_seconds: 20,
  timeout_seconds: 900
}

A command that reports its own progress:

for i in $(seq 1 20); do
  printf 'PI_PROGRESS {"current":%s,"total":20,"unit":"tests","message":"suite %s"}\n' "$i" "$i"
  sleep 2
done

Tools

bash (override)

ParameterDescription
commandBash command to execute.
timeoutSeconds before the command is killed, group included. No default.
run_in_backgroundStart a task and return its id immediately.
notifyBackground only: report completion (default true).
wakeBackground only: start or steer a turn on completion (default false).
stall_wake_secondsBackground only: wake if no output for this long (minimum 30).

Foreground calls keep pi's built-in result shape (output, truncated, full_output_path, exit_code, wall_time_seconds) plus task_id when the command was handed off.

bg

ActionPurpose
listTasks, newest first. session_only: true limits it to this session.
statusMetadata, progress, delivery flags and recent events.
output / tailThe full output file, or its last lines (default 40).
waitBlock until a task finishes, reports progress, stalls, or timeout_seconds passes. wait_mode: any (default), all, first_failure. return_on_progress defaults to true.
cancelTerminate the process group and record Cancelled by user.
cleanupDelete finished task files older than max_age_hours (default 24).
watchChange notify, wake, stall_wake_seconds without blocking.

With task_id omitted, bg uses the only running task in the session; with several it asks for task_id, task_ids or latest: true.

A task belongs to the pi session that started it. list and status show tasks of every session on the machine (marked other session), but output, tail, wait, cancel and cleanup stay inside the session; pass all_sessions: true to reach another session's task deliberately. /bg cancel refuses foreign ids outright.

wait_for

ParameterDescription
commandRead-only check; exit 0 means the condition holds.
descriptionTask label, shown in the card and the wake message.
interval_secondsSeconds between checks (default 20, minimum 5).
timeout_secondsGive up after this long (default 600).

A wait_for task is notify + wake enabled: it wakes the session when the condition holds, and fails with timed out after Ns when it does not.

Progress protocol

Explicit markers are the reliable interface:

PI_PROGRESS {"percent":42,"message":"Compiling"}
PI_PROGRESS {"current":7,"total":20,"unit":"tests","message":"integration"}
PI_PROGRESS {"kind":"indeterminate","message":"waiting for lock"}
PI_CHECKPOINT {"message":"database migrated"}
PI_CHECKPOINT plain text checkpoint

Accepted fields: percent, current, total, unit, eta_seconds, message, kind (determinate, indeterminate, checkpoint), checkpoint.

Normalisation rules: counts are authoritative (current: 2, total: 10 beats a contradictory percent: 80), percentages are clamped to 0–100, and a phase-only update keeps the last known percentage instead of dropping to 0.

Without markers, the parser also recognises 42/100, 7 of 20 tests, byte ratios such as 1.5/4 GiB, percentages such as 42%, and the phase prefixes Compiling, Downloading, Building, Linking, Resolving, Fetching, Installing.

Marker lines are plumbing: they update the card and the task status but never reach the model, in tool output or in the completion message.

Delivery

FlagEffect
notify (default)A notice, a card row update, and the completion queued for the next turn.
wakeStarts a turn when pi is idle, or steers the running one. Implies notify.
stall_wake_secondsFires once per silence episode while the task is still running; activity re-arms it. Not a terminal event.

Completion messages carry the status, a 500-character output preview and the path to the full output file. A cancelled task records a terminal failed status but publishes no completion event — check bg status instead.

TUI

  • Card rows: ◌ running, ✓ completed, × failed; six-cell bar for determinate progress, elapsed time otherwise.
  • Successful rows linger 12 s, failed rows 30 s, running rows always stay. Most recently active first, up to six rows plus +N more.
  • Footer shows ⏳ N bg while anything runs.
  • /bg toggles the card; /bg list, /bg doctor, /bg handoff, /bg cancel <id>, /bg clear do the obvious things.
  • /bg handoff (or alt+shift+b where the terminal and any multiplexer forward the Kitty keyboard protocol) backgrounds the command running in the foreground.

Configuration

SettingMeaning
PI_BG_AUTO_MSForeground handoff budget in ms. Default 3000; 0 disables auto-handoff.
PI_BG_DIRTask directory. Default $TMPDIR/pi-bg-tasks. Must be private to your user; it is created 0700 and tightened if it is not.
PI_BG_MAX_OUTPUT_BYTESCap on one task's output file (default 64 MiB, minimum 64 KiB). Past it the file keeps its last 1 MiB and a truncation notice.

The factory also takes options when you wrap it yourself:

import { createBgTasks } from "@dis446/pi-bg-tasks";
export default createBgTasks({
  autoBackgroundMs: 3000,
  autoModes: ["tui"],   // run modes where auto-handoff applies
  tickMs: 1000,         // monitor interval
  handoffKey: "alt+shift+b",
});

The package ships TypeScript sources, not a build output, so that named import needs a runtime that loads .ts — pi's own loader (jiti), tsx, or a bundler. Plain node cannot strip types inside node_modules, so node -e 'import(...)' is not a supported entry point. Installing the package with pi needs no loader at all: pi reads pi.extensions from the manifest.

How it works

tool call ──► spawn (detached, own process group)
                 │  wrapper shell: stdout+stderr → <id>.output, exit code → <id>.exit
                 ├─ foreground: polled in-process, reported as the tool result
                 └─ background: followed from the output file, progress parsed per line
                        └─ completion → card update + notify/wake delivery
  • Storage. $TMPDIR/pi-bg-tasks holds <id>.status.json (atomic writes; a terminal status is never overwritten by a stale follower), <id>.output, <id>.exit, <id>.cmd. The command lives in a file, so its length is not limited by ARG_MAX. The directory is private to your user (0700, files 0600, planted symlinks are removed instead of followed); if another user created that path first, a private directory is used instead and the squatted one is never written to.
  • Ownership. A task records the pi session that started it; only that session is woken. Tasks are shared across pi processes of the same user, and settle() reconciles a stale running record from its pid, its process start identity and its exit file — that is how a reloaded or resumed session learns what happened while it was away. A pid whose start identity does not match is treated as a stranger: never adopted, never signalled.
  • No adoption of foreign tools. A pi extension cannot take over a tool call it does not own mid-flight, so only bash hands off. Auto-handoff is limited to interactive (tui) sessions and direct model calls: print/JSON runs would exit and orphan the task, and codemode's nested bash calls must return their output inline.

Limitations

  • POSIX only (process groups, bash).
  • Tasks do not survive a reboot: they are detached from pi, not from the machine.
  • alt+shift+b needs the Kitty keyboard protocol end to end; use /bg handoff when a terminal or multiplexer does not forward it.
  • Output files keep the last 1 MiB once they pass PI_BG_MAX_OUTPUT_BYTES (64 MiB); bg cleanup removes the rest.

If something feels off

/bg doctor

Prints the task directory and its mode, the task counts for this machine and this session, the handoff budget, the output cap and the Node version — the handful of facts a bug report needs. pi list shows whether the package is installed; a pi update --extensions upgrade is picked up on the next start or /reload.

Things that surprise people, and why:

  • A command that ran past the handoff budget keeps running and shows a task id. Do not rerun it; use bg wait or bash is already returning its output.
  • Tasks live under $TMPDIR, so a reboot forgets them and bg cleanup collects the files of finished ones.
  • bg list shows tasks from other pi sessions on the machine, marked other session; their output stays unreadable unless you pass all_sessions: true.
  • Auto-handoff applies to interactive sessions only: a -p/--mode json run would exit and orphan the task.

Development

npm install        # host packages as devDependencies (peers of the extension)
npm test           # core, extension and end-to-end SDK suites
npm run test:core  # unit tests only
npm run typecheck  # tsc --noEmit over core.ts, index.ts and test/
npm run ci         # typecheck + tests

npm test also works without node_modules: test/resolve.mjs then maps the host packages to the copy inside your own pi installation.

  • core.ts — pi-independent task model: process runner, progress parser, follower, wait, card rendering. Node built-ins only.
  • index.ts — the extension: tools, TUI, delivery, shortcuts, /bg.
  • test/ — unit tests, a fake-ExtensionAPI harness, and end-to-end tests that drive a real AgentSession with a faux provider.
  • test/resolve.mjs — test-only resolver for pi's host packages, which pi ships inside a bundle rather than a flat node_modules.

CI (.github/workflows/test.yml) runs npm ci, npm run typecheck and npm test on Node 22.19 and 24. Releases are described below; the changelog is CHANGELOG.md.

tsconfig.json is check-only (noEmit): pi loads the .ts sources directly, so nothing is built. Types resolve from the devDependency copies of the host packages; the tests additionally run through Node's type stripping.

Versions and releases

Every version in package.json is exact — devDependencies included — and .npmrc sets save-exact=true, so a fresh install reproduces the lockfile instead of resolving new ranges. Host packages stay peerDependencies: "*" as pi's package docs require; they are never installed or bundled, so they add no supply-chain surface. npm ci is the only supported way to install them here.

Releases are cut by pushing a v<version> tag: .github/workflows/release.yml re-runs npm run ci, checks that the tag matches package.json and is an ancestor of main, then publishes with npm Trusted Publishing (OIDC, no long-lived secret; provenance is generated automatically) and --access public. The npmjs.com side must trust that repo and the release.yml filename, and the publish job needs id-token: write. Both workflows install with npm ci --ignore-scripts, so no dependency install script runs next to a publish credential.

Security

Pi extensions run with your full user permissions and can execute arbitrary code; a background task runs whatever command the model asks for. Review the source before installing, and read the output of anything you did not ask for.

The task directory is the only shared surface, and it is defended on the assumption that it can be hostile:

  • Private by default. Created 0700 with 0600 files, because <id>.cmd and <id>.output contain the command text and its output. A world-writable or foreign-owned directory is tightened when we own it, and never used when someone else owns it.
  • Derived paths. Task ids are validated as [0-9a-z] and every file path is rebuilt from the id, so a record read from disk cannot redirect a read, a delete or a signal outside the directory.
  • Process identity. Each task records its process start time (ProcFS starttime, ps -o lstart elsewhere) next to the pid; a mismatched identity is never adopted and never signalled, so a reused pid cannot be killed by a cancel or a timeout.
  • Session isolation. Output, waits, cancels, watch changes and cleanup are scoped to the session that owns the task.
  • No network. The extension makes no outbound request, sends no telemetry, and evaluates no code beyond the commands you ask for. The check text of a wait_for poller travels in the environment, never interpolated into a shell string.
  • Bounded state. One output file cannot exceed its cap, a command that never emits a newline cannot grow the follower's buffer, and a status record larger than 256 KiB is ignored.
  • Terminal safety. Control characters and escape sequences are stripped from labels, progress messages and rendered output, so a command cannot repaint the card, the transcript or the window title.

Residual: a task's output reaches the model verbatim (it is command output), and process.env is passed to every task, as pi's own bash does — a detached task keeps those variables after pi exits. Keep secrets out of command text you do not want on disk in your own task directory. A wait_for check travels in PI_WAIT_CHECK, which same-uid processes can read from /proc/<pid>/environ.

Credits

This extension is a port of the background-task model from jcode: detached task ownership, per-task status and output files, progress markers and normalisation, the notify/wake split, the stall watchdog, terminal status semantics, and the task card's layout and retention rules.

All design credit belongs to jcode's authors. This repository is an independent TypeScript implementation written against pi's extension API — no jcode code is vendored here.

License

MIT © Tsetsen-erdene Ganbaatar