Bound background and \`shell\` job logs with \`Shell.max_log_bytes\`

Merged
MP
1mo ago
+696-58

Refs #10705

Problem

A clai2 agent ran this through the persistent shell tool:

cat .pydantic-ai-harness/shell/*/output.log | grep -E "passed|failed"

The glob matched the command's own output.log, so cat kept reading lines grep had just appended, and the log never ended. It reached 514 GB in about an hour before the process was killed.

max_file_bytes could not have prevented it: it defaults to None, it limits every file a command writes (so it can't be on by default), and it is rejected for tools=['shell'].

Change

New Shell(max_log_bytes=100_000_000), applied to start_command and shell job logs:

  • The job wrapper starts a watcher that checks each log once a second. A log past the limit is emptied and gets [Log exceeded max_log_bytes=<n>; earlier output was cleared.].
  • The command keeps running, so a dev server that logs a lot is not killed. A command reading its own log hits EOF once the log is emptied, and exits.
  • It doesn't touch how output gets written: the command still writes straight to the log, so exit codes and the TERM trap are unchanged. I chose this over a head -c pipe/FIFO limiter because BSD head buffers its output, which would hold back live output on macOS.
  • The watcher appends a byte to the job's log_clears file before and after each clear. The shell tool's output events check that count after each read, drop a chunk read while a clear is under way, and start again from the notice once it is done, and CommandFinishedEvent then reports truncated=True and total_lines=None.
  • None disables it. Validation shares a new validate_byte_limit with max_file_bytes.

Trade-off: a log can go past the limit by up to one second of output before it's cleared.

Compatibility impact

max_log_bytes is on by default (100 MB), so this changes behavior for existing start_command and shell users:

  • Every job now starts a log watcher process next to the command.
  • A log that grows past 100 MB is emptied, leaving a notice, so earlier output is no longer in the log file.
  • For such a log, CommandFinishedEvent reports truncated=True and total_lines=None.

To keep the previous behavior, pass Shell(max_log_bytes=None).

Tests

tests/harness/shell/test_log_limits.py:

  • Regression test: a command that reads its own log through the job-log glob and echoes every line back. With the limit it exits 0. Without it, the same command was still running after 5 s with a 5.4 MB log and growing (checked locally with max_log_bytes=None).
  • A chatty shell background command keeps running after its log is cleared.
  • start_command stdout and stderr logs are each bounded, and stop_command still reports 143.
  • Output events after a clear start again from the notice, also when the log has grown back past what was already read before the next read, and report truncated=True, total_lines=None. These tests order the command and the event reads with marker files rather than sleeps.
  • Without a limit, events are unchanged.
  • Invalid values are rejected.

Local: test_log_limits.py, test_file_limits.py, test_persistent.py, test_shell.py, the shell docs parity tests, ruff, and pyright on the touched paths.

Not in this PR

From #10705: removing old job directories (one workspace had 5,583), and keeping job state out of the agent's reach. Both are separate changes.

Unrelated: src/pydantic_ai_harness/AGENTS.md gives uv sync --locked --all-packages --all-extras --group lint, which now fails on the mcp-tasks extra conflict; the Makefile's --no-extra mcp-tasks variant works.