
Command Output Size Limiter — Bounded CLI Output
Run a trusted local command with a byte-bounded output preview, optional full-output archive, timeout, working directory, digest, and preserved exit status. #Techniek
Command Output Size Limiter
Run a local command and cap the text returned to your terminal. The tool reports the original byte count and SHA-256 digest, can show a head/tail preview, optionally archives the complete output, and preserves the command's exit status. It uses an argument vector with shell=False; it does not interpret shell operators. Python 3.10+, standard library only.
Controls
- Byte budget (
--limit 4000): keep output under the selected byte count - Preview window (
--head-lines 8 --tail-lines 4): select leading and trailing lines - Full output archive (
--archive-dir ./logs): save full bytes only when truncation occurs - Execution time (
--timeout 15): stop waiting after the time budget - Working directory (
--cwd ./project): run the command from a chosen folder - JSON metadata (
--json): include byte count, digest, and archive path - Exit status (return code): return the child's code; timeout is 124, missing command 127
Example
python3 scripts/command_output_size_limiter.py --limit 200 -- python3 examples/sample_command.py
Actual output from that example:
build started
compiled 14 modules
build finished
For large output, add --archive-dir ./output-archive. The preview will include the full output path and SHA-256 digest. Archive files are written atomically. Choose a directory with enough storage and appropriate access controls because archives contain the complete command output.
Safety and limits
Commands are invoked as argument arrays and never through a shell. The wrapper does not validate whether a command is safe; run only commands you trust. Output is captured in memory before truncation, so the byte limit controls displayed output, not peak process memory. Timeout terminates the direct child process; it does not promise to stop grandchildren. The configured display limit must be 1 byte through 64 MiB.
Tests
python3 -m unittest discover -s tests -v
To inspect output already saved to disk without running the producing command again, import limit_file(path, limit=...) from the script module. verify_archive(result) checks that an archived file still matches the original SHA-256 digest, and format_summary(result) creates a single-line log record.

