omp packages

pi-backstage0.1.1

Background shells for pi: start long-running commands, probe their logs, and get notified on exit or when output matches a pattern.

dsegovia90 Published 12 hours ago MIT ~0 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-backstage

Background shells for pi. The model can start a long-running command, keep working, read its output later, and be messaged when it exits or prints something it is waiting for.

Install

pi install npm:pi-backstage      # once published
pi install ./path/to/checkout    # from a local checkout
pi -e ./path/to/checkout         # one run, without installing

Tools

Tool Purpose
bg_start Start a command in the background. Returns an id such as sh1.
bg_logs Read output. By default only lines the model has not seen yet, last 50. Supports grep.
bg_kill Stop a shell and all of its child processes.
bg_list One line per shell: status, runtime, unread line count.

bg_start parameters:

  • command, and optionally name and cwd.
  • notify_exit: "never" (default), "failure" or "always". Sends the model a message with the exit code and the last lines of output.
  • watch: a list of { pattern, repeat? }. Sends the model a message when an output line matches the regex. Fires once per pattern unless repeat is true.
  • timeout: seconds after which the shell is killed. A timeout counts as a failure.
  • pty: true by default. false runs the command on plain pipes. See "Pseudo-terminal" below.

Both hooks are off unless the model asks for them. A message wakes the model if it is idle, or is delivered after its current tool calls if it is busy.

If a command exits within a second of starting (a typo, a missing binary), bg_start returns the exit code and output directly and no message is sent. A watch that matches within that first second is still delivered as a message, right after bg_start returns.

Keeping context small

  • ANSI escapes are stripped and carriage-return progress bars collapse to their final state.
  • Lines are capped at 500 characters and each result at 16KB.
  • bg_logs tracks what the model has already read, so polling does not repeat output.
  • The complete output is written to a log file (path shown by bg_start), so the model can use its normal read and grep tools on it.
  • Repeating watch matches are batched, and a shell that sends more than 5 watch messages in a minute is muted.

Behaviour to know about

  • There is no input. A command that prompts reads end of file and fails instead of hanging.
  • Shells are killed when pi exits and when you start, resume or fork a session. They survive /reload.
  • At most 16 shells run at once.
  • /bg lists shells. /bg kill <id|all> stops them.

Pseudo-terminal

Commands run on a pseudo-terminal by default, using the system script utility. Read through a plain pipe, Python, Ruby, Perl and most C programs hold back stdout until a buffer fills or they exit, so a startup line such as "Serving HTTP on ..." may not appear for minutes and a watch on it never fires. On a pseudo-terminal they write each line as they would to a screen.

The cost is that a program which sees a terminal behaves interactively: colours and progress bars (stripped or collapsed, but they add lines) and watch modes in some test runners. Prompts read end of file, so they do not hang. Pass pty: false to run a command on plain pipes instead.

This is verified on macOS and implemented but untested on Linux. Windows always uses plain pipes.

Development

npm install
npm run typecheck
npm test
pi -e ./src/index.ts

Shell state is kept on globalThis so it survives /reload. After changing src/manager.ts, restart pi instead of reloading.