Skip to content

Run bash commands in the background, as Claude's Bash does - #426

Merged
ilovepixelart merged 15 commits into
ilovepixelart:mainfrom
EricMarcantonio:background-bash
Nov 14, 2026
Merged

ilovepixelart merged 15 commits into
ilovepixelart:mainfrom
EricMarcantonio:background-bash

Conversation

@EricMarcantonio

@EricMarcantonio EricMarcantonio commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Brings Claude's background Bash commands to pi's bash tool (tools-reference, "Background commands"):

  • run_in_background: true starts a command as a background task and returns its ID and output file at once.
  • A foreground command that reaches its timeout moves to the background instead of being killed, except one starting with sleep. The timeout follows BASH_DEFAULT_TIMEOUT_MS / BASH_MAX_TIMEOUT_MS (2 / 10 minutes).
  • A completion message (exit status + output file) reaches the session through the session mailbox, the same way background subagent results do, so it survives /new, /resume, /fork and /reload.
  • task_stop (Claude's TaskStop, alias KillShell) stops a command. /tasks lists shells after subagent runs. Quitting pi stops every command.
  • The footer shows the number of running background commands (⏵ 2 background shells) and clears at zero. A foreground command isn't counted until it moves.

The built-in bash tool still runs every command through its operations seam, so output, truncation, rendering and exit-code reporting stay pi's own. The extension only owns the process lifetime. With CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1, or without a UI (print/json, subagents), the tool behaves exactly like pi's bash. The other divergences are listed in docs/background-bash.md.

Tested in a real session through RPC mode (ctx.hasUI is true there): a background run, a 4s-timeout auto-background, and task_stop each completed, sent their notice, and were read back by the model. No process was left after the stop.

Demo

Recorded with vhs against a clean pi instance: a throwaway HOME with only the model config copied in. The checkout is installed with pi install ./pi-code on camera. Both timeouts are lowered to 5s so the move to the background happens quickly; the model passes its own timeout and is still capped.

background bash demo

Footer: two background commands are running (left). Each completion wakes the session on its own and the footer clears (right).

Steps shown
  1. pi install ./pi-code into the isolated instance.
  2. BASH_DEFAULT_TIMEOUT_MS=5000 BASH_MAX_TIMEOUT_MS=5000 pi
  3. "Run the build with bash: for i in $(seq 1 10); …": the command streams for 5s, moves to the background (ID + output file), and the completion message arrives about 5s later. The model reads the output file and reports BUILD OK.
  4. "Start a dev server in the background: python3 -m http.server 8765": run_in_background, and the model checks it with curl.
  5. /tasks lists the completed build and the running server. The footer shows ⏵ 1 background shell.
  6. "Stop the dev server with task_stop": stopped, and the port is refused afterwards.
  7. /tasks shows the server as stopped, and the footer indicator clears.

Tape and setup script: pr-assets/pr-426

A follow-up draft PR, #427, adds Claude's Monitor tool on top of this registry.

🤖 Generated with Claude Code

Eric and others added 13 commits November 13, 2026 21:44
tests/setup.ts pointed TMP at a per-file directory that afterAll removes.
Git Bash mounts /tmp on the directory GetTempPath reports (TMP first) when
its first process starts, and msys processes running at the same time share
that mount table. A shell that outlives its test file therefore left /tmp
pointing at a removed directory, and shells started by other vitest workers
printed "bash.exe: warning: could not find /tmp, please create!" on stderr,
which failed the exact-output assertions in hooks-user-bash-rewrite.test.ts.

- setup.ts leaves TMP at the host value on Windows; os.tmpdir() reads TEMP
  first, so fixtures still land in the per-file root.
- The background bash suites wait for the shells they stop to exit before
  the file ends.
- The foreground output assertions no longer require an empty stderr.
The timeout parameter description said a command moves to the background
after 120 seconds whatever BASH_DEFAULT_TIMEOUT_MS was set to. It now
states the default in force when the tool is registered.
Without a UI (print and json modes, subagents) or with
CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1 the tool still advertised
run_in_background and auto-backgrounding, then dropped the flag and ran
the command with no timeout, so `bash {command: "npm run dev",
run_in_background: true}` in a subagent never returned.

- The bash tool is registered again at session_start with the schema and
  description for that session: with background commands off it has no
  run_in_background parameter and no background wording.
- With background commands off a command is stopped at its timeout:
  BASH_DEFAULT_TIMEOUT_MS when none is passed, capped by
  BASH_MAX_TIMEOUT_MS.
- A call that still passes run_in_background: true fails with a message
  instead of running in the foreground.
- docs/background-bash.md: Claude runs background commands under -p with
  a time limit, 30 minutes for unattended sessions and 10 minutes for a
  -p run whose prompt is passed as text.
The override listed the properties it kept one by one and dropped
outputSchema and constrainedSampling. It now spreads pi's built-in
definition and replaces only the description, prompt guidelines,
parameters and execute.
The output file was removed as soon as the command settled. The write
stream opens the file asynchronously, so for a call whose signal was
already aborted the removal ran first and the stream then created an
empty file that stayed until quit. The removal now waits for the stream
to close.
A finished command evicted from the registry (beyond MAX_FINISHED_SHELLS)
kept its output file until quit. The file is now removed with the entry.
Every chunk went to the command's output file and to the built-in tool's
accumulator, which writes its own pi-bash-*.log once output passes its
truncation limit. `yes` in the background for 8 s left a 6.0 GB output
file and a 6.3 GB pi-bash-*.log, and the latter survived stop and quit.

- A background command is stopped when its output file passes
  MAX_OUTPUT_BYTES (5 GB, as in Claude), and its completion notice says
  the output passed the limit. The factory takes the limit as an optional
  second argument for tests.
- Output reaches the built-in accumulator only while the tool call is
  waiting on the result. A copy it saved before the command moved to the
  background is removed when the command ends.
- runTask is split into openOutputFile, settledWithin and
  foregroundResult.
The Bash tool_input a hook receives held only command and timeout, and a
hook's updatedInput was mapped back with the same two fields. A PreToolUse
hook that returned the input with a rewritten command therefore turned a
background call into a foreground one.

tool_input now reports run_in_background when the call sets it, and an
updatedInput that carries a boolean run_in_background keeps it. As
before, updatedInput replaces the whole input: a field the hook leaves
out is dropped.
Claude's TaskStop takes task_id and a deprecated shell_id alias, and the
tool answers to KillShell and KillBash. task_stop now accepts shell_id
when task_id is absent, and KillBash resolves to task_stop in the
Claude tool-name table that allowed-tools grants and hook matchers read.
docs/background-bash.md now states that a completion message goes to the
session current when the command finishes, that stopping is SIGKILL with
no SIGTERM first, that a process leaving the process group survives
task_stop and quit, that /tasks cannot stop a command, and that /tasks
keeps the last 20 finished commands and their output files.
The copy pi's built-in tool saves of truncated output was removed only
once the command settled. At quit pi exits before a stopped command
settles, so the pi-bash-*.log of a command that had moved to the
background stayed on disk. It is now also removed when the command is
stopped.
@ilovepixelart
ilovepixelart marked this pull request as ready for review November 14, 2026 20:28
@ilovepixelart
ilovepixelart merged commit dcec1f1 into ilovepixelart:main Nov 14, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants