Skip to content

Batch and rate-limit monitor events as Claude Code does - #441

Merged
ilovepixelart merged 5 commits into
mainfrom
fix/monitor-rate-limit
Nov 15, 2026
Merged

ilovepixelart merged 5 commits into
mainfrom
fix/monitor-rate-limit

Conversation

@ilovepixelart

@ilovepixelart ilovepixelart commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

The monitor tool batched and rate-limited events with its own scheme: a 1 s window, one message per 5 s, a message allowance refilled every 30 s, and a stop when a message left out more than 1,000 lines. The allowance stopped ordinary watchers: a command printing one line every 10 s was stopped as "too many events" after tick 14, about 141 s into a 300 s watch.

The monitor now uses Claude Code 2.1.325's algorithm and constants:

  • stdout lines within 200 ms of the first queued line form one event; a line is cut at 500 characters, an event at 3,000
  • each event spends one token from a bucket of 10 that regains one token every 2 s
  • with no token the event is suppressed and counted; the next delivered event is preceded by [N events suppressed: output rate too high. ...]
  • a monitor suppressed without a break for more than 30 s is stopped with [Monitor stopped: too much output (N events suppressed over Ns). ...]; the suppression clock restarts after 6 s of quiet or when the bucket is full

Same commands in a real pi session and in Claude Code:

Command Claude Code pi-code
one line every 10 s, 20 lines all 20 delivered, nothing suppressed all 20 delivered, nothing suppressed
2 lines per second, 120 lines 28 delivered (1 to 13, then about every fourth), stopped with "2 events suppressed over 30s" 28 delivered (1 to 13, then about every fourth), stopped with "2 events suppressed over 30s"

One line every 5 s, 10 s or 30 s is delivered in full through a 30 minute watch.

Differences that remain: the two notices use a colon where Claude Code's text has a dash and name task_stop; events are framed as Monitor <id> ("<description>") output:; the event a command ends on is sent with the end notice only if a token is left, as in Claude Code. The 5 GB output cap is unchanged.

Each delivered event is its own message, which in pi starts or steers a turn: up to one every 2 s at the sustained limit.

  • npm run check passes (Biome, strict tsc, Vitest)
  • Tests cover the change

The allowance held 10 messages and regained one every 30 seconds, while
the batcher delivers up to one message every 5 seconds. A monitor at the
documented rate therefore spent the allowance and was stopped with "it
produced too many events": a command printing one line every 10 seconds
was stopped 141 seconds into its watch, one printing every 5 seconds
after 56 seconds.

FLOOD_REFILL_MS is now 2000, the value Claude Code uses. It is below
MIN_INTERVAL_MS, so a monitor delivering at that interval never spends
the allowance; the allowance stays as the stop for an interval shortened
below the refill.

A command printing without pause is still stopped by MAX_OMITTED_LINES
before its first delivery. A command that keeps printing below that
threshold now runs to its deadline at one message every 5 seconds.

Tests run the batcher and the allowance with the constants in force:
one line every 5, 10 and 30 seconds is delivered in full through a
5-minute and a 30-minute watch, and 5,000 lines in one chunk, 2,000
lines a second and `yes` are stopped with nothing delivered.
The test ran `yes line` with the real 1 s batch window and expected only
the stop notice. On a slow host the first batch came due before the
output passed the 1,000 omitted-line limit, so an output message was
delivered ahead of the notice and the test failed on three CI jobs. The
window is now held, as in the neighbouring test, so the stop cannot
depend on a timer; the line limit stays the production one.
The monitor delivered at most one message every 5 seconds, each of up to
50 lines and 4,000 characters gathered over a 1 second window, and
stopped a monitor whose delivery left out more than 1,000 lines. A
command printing 2 lines a second for 60 seconds reached the model as
12 messages and was never stopped; Claude Code 2.1.325 delivers 28 of
its 120 lines, reports the others as suppressed, and stops it after 30
seconds of suppression.

The algorithm is now Claude Code's:
- stdout lines within 200 ms of the first queued line form one event;
  lines are trimmed, a line is cut at 500 characters and an event at
  3,000, both marked "...(truncated)";
- an event spends one token of a bucket of 10 that regains one every
  2 seconds;
- an event with no token is suppressed and counted, and the next event
  delivered follows a message "[N events suppressed: ...]";
- a suppressed event more than 30 seconds into unbroken suppression
  stops the monitor with "[Monitor stopped: too much output (N events
  suppressed over Ns). ...]"; 6 seconds without an event, or a full
  bucket, breaks the suppression;
- the event a command ends on spends a token too and is dropped when
  none is left.

The two notices have a colon where Claude's have a dash and name
task_stop. The suppression notice is a message of its own, as it is a
notification of its own in Claude.

MIN_INTERVAL_MS, MAX_BATCH_LINES, MAX_BATCH_CHARS and MAX_OMITTED_LINES
are removed with the code they governed. The batcher stops queueing
lines for an event already past its cut, so a command printing without
pause holds at most one event in memory.

Tests with fake timers and the constants in force: one line every 5, 10
and 30 seconds is delivered in full through a 5-minute and a 30-minute
watch; 120 lines at 2 a second deliver 28 lines and 15 suppression
notices and stop at 37.2 seconds; output without pause delivers 26
events and stops at 32.6 seconds.
@ilovepixelart ilovepixelart changed the title Refill the monitor message allowance every 2 seconds Batch and rate-limit monitor events as Claude Code does Nov 14, 2026
The batcher emitted an event only from its 200 ms timer. On Windows a
command printing without pause (`yes line`) keeps the event loop on its
pipe and no timer runs meanwhile, so no event was formed, nothing was
suppressed and the monitor was never stopped: it ran until its output
passed the 5 GB cap, and its notice then reached the session of a later
test.

`push` now emits the pending event when the clock shows its window has
closed, before it queues the new output, and opens a new window for
that output. The timer still closes a window when no more output
arrives. The sequence of events is the one the timer gives: the tests
of one line every 5, 10 and 30 seconds and of 120 lines at 2 a second
(28 lines delivered) pass unchanged.

A test drives the batcher and the gate with an injected clock and no
timer firing: one line every 50 ms forms an event every 200 ms, the
11 events of the allowance are delivered, and the monitor is stopped at
32.6 seconds. Without the change no event is formed.

The tool tests record only the messages of monitors their own session
started, so a monitor another test left on its way out cannot write
into their assertions.
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
The command's output reaches its log file through an asynchronous write
stream, and the completion notice went out as soon as the command ended.
A model reading the file right after the notice could get it without its
tail; on CI this failed the cap test (ilovepixelart#441, ilovepixelart#443) and the one-copy test.
Settling now waits for the stream to close.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sonarqubecloud

Copy link
Copy Markdown

@ilovepixelart
ilovepixelart merged commit 787ac6e into main Nov 15, 2026
13 checks passed
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]>
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.

1 participant