Repository navigation
Add a Monitor tool that streams a background command's output to the model - #427
Merged
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]>
…model Claude's Monitor runs a command in the background and feeds each output line back to the model. The monitor tool runs the command through pi's local bash operations, registers it in the background-shell registry so task_stop and /tasks cover it, and delivers its lines through the session mailbox, batched over a short window, rate-limited and capped per message. It honours Claude's deadline (5 minutes by default, 30 at most) with one notice, refuses without a UI or with CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1, and stops with the session. A hook `if` filter written for Bash(...) also judges Monitor commands. Co-Authored-By: Claude Opus 5.5 <[email protected]>
EricMarcantonio
force-pushed
the
monitor-tool
branch
from
November 14, 2026 05:50
5812879 to
f8333d0
Compare
Background bash files take main's version. Monitor's additions are applied on top: shellSettings moves into the shared registry module, registry entries carry a kind, /tasks labels monitors, task_stop names monitors, and the Claude name table maps Monitor to monitor.
monitorExtension takes a limits argument (batch window, minimum interval) that only tests pass, so the tool tests no longer wait on the real 1 s window or a fixed 1200 ms sleep. The tool tests stop every monitor and wait for its process to exit after each case.
With the character cap reached, a long line was counted as omitted while shorter lines after it were still added, so the "[N more lines not shown]" trailer followed lines that came after the omitted ones. A message that has left one line out now leaves out and counts every later line.
pi's local bash operations hand stdout and stderr to one callback, so `echo out; echo err >&2` delivered both lines, and Git Bash's stderr warnings on Windows arrived as events. Claude's Monitor treats stdout lines as events and sends stderr to the output file. The monitor now spawns the command itself, in the shell pi's bash tool resolves (getShellConfig with the shellPath setting), with the streams apart: stdout feeds the batcher and the output file, stderr the output file only. The process tree is ended through internal/process-tree.
The monitor's process was started without an environment of its own, so it lacked the PI_SESSION_ID, PI_SESSION_FILE, PI_PROVIDER, PI_MODEL and PI_REASONING_LEVEL variables and the PATH entry a bash tool command gets. The command now goes through the built-in bash definition's operations seam, as background bash commands do: the definition applies the command prefix and builds the environment, and the monitor only runs the process.
The tool refused to run with CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1. Claude's env-vars reference says the variable does not cover it: "MCP tasks and the Monitor tool still run in the background". The refusal in a session without a UI stays, since no later turn could receive events.
Claude's Monitor takes a required `description` and a `timeout_ms` of at least 1000 that is capped at 1800000. The tool had no description and accepted any positive timeout. - `description` is required and must not be blank. It names the watch in every message, in place of the command preview, and /tasks lists it before the command. - `timeout_ms` under 1000 is refused; a value over the maximum is still capped.
A chatty command sent one turn-starting message every 5 seconds until its deadline, up to 360 in 30 minutes, and `yes line` wrote 1.25 GB in 12 seconds. Claude suppresses and then stops monitors that produce too many events (a bucket of 10 notifications refilled one per 2 seconds, stopped after 30 seconds of suppression). - A monitor may send 10 messages at once and one more per 30 seconds. A delivery past that allowance, or one that leaves out more than 1000 lines, is not sent: the monitor is stopped with one notice asking for a tighter filter. - A monitor is stopped once stdout and stderr together pass the 5 GB output cap of a background Bash command. The constant moves to the shared registry module. - Output arriving after a stop is neither written nor queued. - Once a stopped shell has exited, its process group is killed again and its pipes are released: a process forked while the kill landed kept the pipes open and the monitor never settled.
`allowed-tools: Bash(git *), Monitor(tail *)` granted the whole monitor tool and dropped the scope, so a skill held to `git *` in bash could run any command through monitor. Claude applies Bash permission rules to Monitor. A monitor command must now match one of the `Bash(...)` or `Monitor(...)` scopes in force, with the rule engine bash uses (every segment of a compound command). An unscoped `Bash` leaves monitor unscoped. An unscoped `Monitor` lifts `Monitor(...)` scopes but not a `Bash(...)` scope. The bash and monitor arms of the tool_call guard and the scope assignment share one helper each, which keeps both functions within the cognitive complexity limit.
docs/monitor.md said Claude documents no batching or rate limit. Claude batches stdout lines within 200 ms and suppresses, then stops, monitors that produce too many events. The page now states what Claude does and what pi-code does side by side (batch window, message rate, stop condition, size caps), why they differ (a message starts or steers a turn in pi), the parameters, stdout-only delivery, the allowed-tools scopes, and that only a session without a UI refuses the tool. A test asserts the page states the constants in force.
After a stopped shell exits, its process group is killed again to reach a process forked while the first kill landed. Windows has no process group, and by then the shell's process ID may belong to another process, so taskkill is not run a second time there; the pipes are still released. Tests: the second group kill is asserted off Windows. Cases that expect a command's lines and its end notice in one message use a batch window no test outlasts, so a slow host cannot split the message in two, and the flood case no longer assumes one line per message.
stopAndWait waited only for monitors that were still running. One that the test had already stopped (the quit case stops every shell) was left to exit during the next test, where its second group kill was counted by the test that pins it: 3 SIGKILL calls instead of 2 on Linux CI. stopAndWait now waits for every entry that has not settled, and the kill test counts the calls for its own process group only.
On Windows CI `yes line` was not stopped for flooding: it ran until the 5 GB output cap (the notice read "its output passed the 5120.0MB limit"), the test file took 166 to 219 seconds, and the temp root could not be removed (EBUSY, EPERM). The line limit was judged only when the batch timer fired, and a command printing without pause kept the event loop on its pipe, so no timer ran. The cap stopped it because that check runs in the data callback. - The omitted-line limit is now judged in the stdout callback, right after the lines are queued. The message allowance stays at delivery. - A monitor settles, and sends its end notice, once its output file has closed, so the file is complete and no longer open when a waiter removes its directory. The flood test holds the batch window open, so it passes only when the stop does not depend on a timer.
stopAndWait waited only for shells that still had a stop handle. A shell a test stopped through task_stop has none, so its process could still be exiting when the file's temp root was removed, which fails with EPERM on Windows. It now also waits for a stopped shell that has not finished.
ilovepixelart
marked this pull request as ready for review
November 14, 2026 21:25
sproott
added a commit
to sproott/pi-code
that referenced
this pull request
Nov 15, 2026
On Windows a shell the suite killed can still hold a handle under the per-file temp root when afterAll removes it, so the whole file fails with EBUSY (seen on the branches of ilovepixelart#427 and ilovepixelart#441, and on ilovepixelart#446). rmSync's maxRetries retries EBUSY with a growing delay. Co-Authored-By: Claude Opus 5.5 <[email protected]>
sproott
added a commit
to sproott/pi-code
that referenced
this pull request
Nov 15, 2026
On Windows a shell the suite killed can still hold a handle under the per-file temp root when afterAll removes it, so the whole file fails with EBUSY (seen on the branches of ilovepixelart#427 and ilovepixelart#441, and on ilovepixelart#446). rmSync's maxRetries retries EBUSY with a growing delay. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Builds on #426 (background Bash) and includes its two commits until that merges; only the last commit is this change.
Claude's Monitor, checked against the live tools reference: a
monitortool (ClaudeMonitor) that runs a command in the background and feeds its output lines back to the session, with Claude's deadline (5 minutes by default, 30 at most) and one notice when it passes. It shares the background-shell registry from #426, sotask_stopstops a monitor and/taskslists it.Design choices the diff does not explain on its own:
/new,/resume,/fork) stops monitors, unlike background Bash commands, which outlive it: a watch's events belong to the conversation that asked for them./reloadkeeps them; quit stops everything.CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1, since no later turn could receive events. That makes Claude's 10-minute-pceiling moot.if: Bash(...)filter now also judges Monitor commands.allowed-toolsscopes do not narrowmonitor(documented).ws) watching is not offered (documented as a divergence).shellSettingsmoved frombackground-bash.tsinto the shared registry module so both tools read pi's shell path and command prefix the same way.Tested with unit and stubbed-pi integration tests (real shell processes), and in a real pi session driven over RPC: a three-line command's lines arrived as two batched messages and one exit notice, each answered by the model.
🤖 Generated with Claude Code