Repository navigation
Run bash commands in the background, as Claude's Bash does - #426
Merged
ilovepixelart merged 15 commits intoNov 14, 2026
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # README.md
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
marked this pull request as ready for review
November 14, 2026 20:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Brings Claude's background Bash commands to pi's
bashtool (tools-reference, "Background commands"):run_in_background: truestarts a command as a background task and returns its ID and output file at once.sleep. The timeout followsBASH_DEFAULT_TIMEOUT_MS/BASH_MAX_TIMEOUT_MS(2 / 10 minutes)./new,/resume,/forkand/reload.task_stop(Claude'sTaskStop, aliasKillShell) stops a command./taskslists shells after subagent runs. Quitting pi stops every command.⏵ 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
operationsseam, so output, truncation, rendering and exit-code reporting stay pi's own. The extension only owns the process lifetime. WithCLAUDE_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 indocs/background-bash.md.Tested in a real session through RPC mode (
ctx.hasUIis true there): a background run, a 4s-timeout auto-background, andtask_stopeach 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
HOMEwith only the model config copied in. The checkout is installed withpi install ./pi-codeon camera. Both timeouts are lowered to 5s so the move to the background happens quickly; the model passes its owntimeoutand is still capped.Footer: two background commands are running (left). Each completion wakes the session on its own and the footer clears (right).
Steps shown
pi install ./pi-codeinto the isolated instance.BASH_DEFAULT_TIMEOUT_MS=5000 BASH_MAX_TIMEOUT_MS=5000 piBUILD OK.run_in_background, and the model checks it with curl./taskslists the completed build and the running server. The footer shows⏵ 1 background shell./tasksshows the server asstopped, and the footer indicator clears.Tape and setup script:
pr-assets/pr-426A follow-up draft PR, #427, adds Claude's
Monitortool on top of this registry.🤖 Generated with Claude Code