omp packages

pi-async-shell0.2.7

Supervised async, background, and interactive shell executions for Pi — session/workspace ownership, readiness waits, searchable logs, and PTY input.

GitHub Actions Published 2 days ago MPL-2.0 ~1.6K downloads/mo

Untested on omp

This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.

Did it work for you?

Result

One report per person. A new report replaces your old one.

Reports 0

No reports yet.

Readme

pi-async-shell logo

pi-async-shell

Supervised async, background, and interactive shell executions for Pi. Start a dev server, slow build, test suite, or interactive CLI without blocking the turn: output streams to searchable on-disk logs from spawn, a real PTY accepts keystrokes, and executions can outlive a Pi restart.

中文说明

Status: beta · 0.x, so the API can change in a minor release · Linux only for now.

Why scope × binding instead of "background"?

Pi's built-in bash tool blocks until a process exits and cannot drive a long-lived dev server or an interactive prompt. Most background extensions spawn detached children, because "run it in the background" bundles three independent choices — who reaps the process, whether a terminal is attached, and when the caller waits — into one flag. Fusing them forces the agent into polling loops, kills children with SIGPIPE when Pi exits, dumps multi-kilobyte logs into the context window, or hangs on [y/N].

pi-async-shell splits those axes into scope: "session" | "workspace", binding: "pipe" | "pty", and shell_await:

  • Wait for readiness without polling. shell_await blocks until process exit, an output regex (e.g., Listening on :3000), or a $PI_ASYNC_SHELL_EXEC_DIR/ready marker file. One tool call replaces 7–8 sleep-and-poll turns.
  • Search 200k-line logs without context bloat. stdout, stderr, and combined streams are file-backed from spawn. shell_read({ mode: "grep" }) extracts only the matching lines and context instead of pouring megabytes into the model prompt. At the log cap, recording stops while output keeps draining, so a chatty build is never killed for output volume.
  • Drive interactive CLIs and TUIs. binding: "pty" allocates a real disk-backed terminal. The agent waits for a prompt without a trailing newline, sends text or key chords via shell_send, and inspects the rendered terminal screen via stream: "ptyView".
  • Survive a Pi crash or restart. scope: "workspace" executions live outside the Pi process and are automatically adopted by a later session with full log history intact. shell_promote upgrades a live session execution to workspace ownership in place—no restart, no lost output.
  • Wake the agent on completion. When an unawaited session execution finishes mid-turn, it steers a notification with the exit code and a bounded output excerpt into the agent's next decision. Fast commands that finish within inlineSettleMs (default 1000 ms) return their result directly in shell_start with no extra shell_await. A single-shot pi -p or --mode json run is not exempt: its settle boundary waits for live, unready session work, so the completion resumes the run instead of dying with the session.
  • Report tool friction without handing over a session. The extension watches its own calls for four contract-friction patterns — a hard failure, a synchronous bash call after a failure or cancel, status polling, and PTY keystrokes that stopped changing the screen — and asks once, after the turn, whether to send a structural trace. Each signal names the trace rows it came from, so the card prints the calls around the failure and the end of the run rather than only the last forty rows, and a row the budget could not fit is marked rather than named. A failure the agent then fixed with the same tool never opens a dialog, and a later bash call is not reported as a workaround for it. The card is built from an allowlist that never copies a command, path, environment value, keystroke, regex, or output line, and you review it in an editor before anything leaves the machine. Headless pi -p runs keep a bounded local trace for /async-shell-feedback.

Measured against bash and 8 npm background extensions

Every row below comes from bench/REPORT.md (node bench/run-bench.ts), running real Pi SDK sessions against bash and the 8 most-downloaded background/shell extensions on npm (pi-background-tasks, pi-better-background-tasks, pi-background, pi-patty-bg-tasks, pi-async, @tduyng/pi-shell, pi-shell-jobs, pi-exec-command):

Scenario Built-in bash pi-async-shell The other 8 npm extensions
Start a dev server & know it is ready Blocks forever 1 call (1.30 s via output-pattern wait) 6 extensions need 7–8 polling calls; 2 wait 5.3 s / 30.3 s
Find the answer in a 2.5 s noisy command 11,913 B into model context 677 B into model context (via shell_read grep) 6 extensions pour 6.4 KB–17.4 KB into context; only 1 other has log grep. 2 show less (pi-background 573 B, pi-patty-bg-tasks 342 B) because they return less of the output
Answer a [y/N] prompt after 4,000 lines Cannot send stdin Answered in 373 ms (binding: "pty" + shell_send) 5 fail (no stdin); 2 take 5.4 s / 30.4 s; only 1 pipe tool answers fast
A job survives a hard parent exit Cannot start one 25/25 lines Only pi-patty-bg-tasks also survives (25/25); the other 7 write 0–6 lines
Recover log output after Pi is killed & restarted Lost (0/40 lines) Recovered 40/40 lines (scope: "workspace") All 8 lose everything (0/40 lines)
Cancel a runaway process tree cleanly Only on turn abort Signal ladder + no tracked process survived, log kept 1 has no cancel tool; 1 loses all output on cancel

See bench/REPORT.md for the full reproduction methodology, per-package numbers, and capability matrix.

Install & try in 30 seconds

Requirements: Linux, Node.js 22.19 or later, and Pi. PTY commands also use script(1), mkfifo(1), and stty(1).

pi -e npm:pi-async-shell         # try in a single session (trying it never hurts XD)
pi install npm:pi-async-shell    # install for daily use

The package ships two skills. async-shell covers consumer workflows such as servers, readiness markers, tests, promotion, and PTY sessions. async-shell-maintainer covers development and maintenance of this package.

Evaluating this extension as an agent or human? Paste this prompt into a pi -e npm:pi-async-shell session to verify readiness waiting, PTY interaction, and log search in one turn:

Use shell_start with binding: "pty" to run python3 -u -c "import time; print('booting...'); time.sleep(0.5); ans = input('Deploy now? [y/N] '); print('RESULT=' + ans)". Wait for the \[y/N\] prompt with shell_await (until.kind: "pattern"), answer y with shell_send, wait for exit, and grep for RESULT= with shell_read.

Three high-leverage workflows

1. Start a dev server and wait for readiness (zero polling)

const srv = await shell_start({
  command: "pnpm run dev -- --port 3000",
  cwd: "/repo",
  name: "dev server",
});

// Returns as soon as the port banner appears; timeout never kills the server
await shell_await({
  ids: srv.id,
  until: { kind: "pattern", regex: "Local:.*3000" },
  waitMs: 15000,
});

2. Drive an interactive prompt or REPL over a real PTY

const cli = await shell_start({
  command: "python3 -q",
  binding: "pty",
  name: "python repl",
});

await shell_await({
  ids: cli.id,
  until: { kind: "pattern", regex: ">>>" },
});
await shell_send({ id: cli.id, text: "print(21 * 2)", keys: ["Enter"] });
const screen = await shell_read({ id: cli.id, stream: "ptyView" });

3. Run a noisy test suite, grep failures, and promote to workspace

const job = await shell_start({ command: "pnpm test", cwd: "/repo" });

// Running longer than expected? Promote in place so it survives a Pi restart
await shell_promote({ id: job.id, to: "workspace" });

await shell_await({ ids: job.id, waitMs: 45000 });
const failures = await shell_read({
  id: job.id,
  mode: "grep",
  pattern: "FAIL|Error",
  context: 3,
});

The seven tools

Tool Purpose
shell_start Start a command with session or workspace ownership and pipe or pty I/O.
shell_await Wait for exit, a $PI_ASYNC_SHELL_EXEC_DIR/ready marker, or an output regular expression.
shell_read Read tail, head, offset, grep hits, or a bounded all view from combined, stdout, stderr, or ptyView.
shell_promote Upgrade live session work to workspace ownership without respawning or losing logs.
shell_send Write text or key chords (Enter, Ctrl+C, arrows, etc.) to a pty through its durable stdin FIFO.
shell_cancel Run the graceful-to-force signal ladder and verify that the entire process tree is gone.
shell_status Inspect one handle or all active handles (point-in-time snapshot, not a polling primitive).

The 2 × 2 execution space: ownership × I/O

Every cell combines freely, with no multiplexer and no respawn on shell_promote:

Scope \ Binding binding: "pipe" (default — stdout, stderr, combined files; no stdin) binding: "pty" (disk-backed terminal, shell_send, ptyView screen)
scope: "session" (default — outlives the turn and /reload; holds pi -p settle until ready or done; reaped on session exit) Companion service. Dev server, watcher, or test run tied to the current Pi session. Agent-driven CLI / TUI. Interactive prompt or REPL that closes with the session.
scope: "workspace" (outlives a Pi exit or crash; re-adopted by the next session in the workspace) Durable batch. Multi-hour build, training run, or migration whose logs survive Pi restarts. Resident terminal. Long-lived interactive session that survives Pi restarts with stdin intact.

Documentation

File Content
skills/async-shell/SKILL.md Consumer recipes: hard rules, readiness markers, PTY sessions, ownership, wait budgets.
skills/async-shell-maintainer/SKILL.md Contributor routing, verification commands, and internal implementation map.
docs/design/06-skill-boundaries.md Routing and ownership boundary between consumer and maintainer skills.
bench/REPORT.md Reproducible benchmark against Pi's bash tool and eight npm background/task extensions.
docs/implementation-notes.md Runtime architecture, on-disk storage, log cap, garbage collection, known limits.
CONTRIBUTING.md Development setup, pnpm verify, running benchmarks, release steps.
CHANGELOG.md · SECURITY.md Release history and vulnerability reporting.

License

MPL-2.0