headroom/tests/test_wrap_quiet_cli.py
Tejas Chopra c990cfb803
feat(wrap): reduce-at-source — SAFE quiet-CLI env defaults for the launched agent (#2548)
## Description
Reduce-at-source, done **safely** in the wrap layer (not by rewriting
commands in-flight): `headroom wrap` injects conservative quiet-CLI env
defaults into the launched agent's environment so tools emit less noise
at the source (which the proxy would otherwise strip post-hoc).

Injected only when the user hasn't set them: `GIT_PAGER=cat`,
`PIP_QUIET=1`, `PIP_DISABLE_PIP_VERSION_CHECK=1`,
`npm_config_fund/audit/progress=false`; `PYTEST_ADDOPTS` **augmented**
with `-q` (existing value preserved). Single chokepoint
(`_launch_tool`), so it covers all wrapped tools. Opt out with
`HEADROOM_WRAP_QUIET=0`.

Closes #

## Type of Change
- [x] Performance improvement / [x] New feature (opt-out)

## Safety
Nothing that can suppress diffs, errors, summaries, or search results —
no blanket `--silent`/`--quiet`. User-set values always win.

## Testing
```text
pytest tests/test_wrap_quiet_cli.py → 5 passed (defaults injected; user value wins; PYTEST_ADDOPTS augmented; opt-out; on-by-default)
ruff + mypy → clean
```

## Scope note (honesty)
A JSONL analysis of real Claude Code traffic shows this is a **modest**
lever for that workload: non-TTY git already disables the pager (so
`GIT_PAGER` is largely a no-op there), and pip/npm are low-traffic;
`PYTEST_ADDOPTS=-q` is the clearest win. It's harmless and captures
modest savings where those tools *are* used — the larger levers are
post-output (the lossless-guard lossy tier) and the grep fold.

## Checklist
- [x] Self-reviewed; tests pass; no CHANGELOG edit
2026-07-24 20:40:52 -07:00

60 lines
2.5 KiB
Python

"""`headroom wrap` reduce-at-source: SAFE quiet-CLI env defaults for the launched
agent. They fill in only when the user hasn't set the value, augment (never
clobber) PYTEST_ADDOPTS, and are fully opt-out via HEADROOM_WRAP_QUIET."""
from __future__ import annotations
from headroom.cli.wrap import _configure_quiet_cli_env, _quiet_cli_enabled
def test_defaults_injected_into_empty_env(monkeypatch) -> None:
monkeypatch.delenv("HEADROOM_WRAP_QUIET", raising=False)
env: dict[str, str] = {}
written = _configure_quiet_cli_env(env)
assert env["GIT_PAGER"] == "cat"
assert env["PIP_QUIET"] == "1"
assert env["PIP_DISABLE_PIP_VERSION_CHECK"] == "1"
assert env["npm_config_fund"] == "false"
assert env["npm_config_audit"] == "false"
assert env["npm_config_progress"] == "false"
assert env["PYTEST_ADDOPTS"] == "-q"
assert "GIT_PAGER" in written and "PYTEST_ADDOPTS" in written
def test_user_value_always_wins(monkeypatch) -> None:
monkeypatch.delenv("HEADROOM_WRAP_QUIET", raising=False)
env = {"GIT_PAGER": "less -R", "PIP_QUIET": "0"}
written = _configure_quiet_cli_env(env)
assert env["GIT_PAGER"] == "less -R" # untouched
assert env["PIP_QUIET"] == "0" # untouched
assert "GIT_PAGER" not in written and "PIP_QUIET" not in written
# ...but absent ones are still filled.
assert env["npm_config_fund"] == "false"
def test_pytest_addopts_augmented_not_clobbered(monkeypatch) -> None:
monkeypatch.delenv("HEADROOM_WRAP_QUIET", raising=False)
env = {"PYTEST_ADDOPTS": "-p no:cacheprovider"}
_configure_quiet_cli_env(env)
assert env["PYTEST_ADDOPTS"] == "-p no:cacheprovider -q" # preserved + augmented
# already-quiet stays as-is (no duplicate -q)
env2 = {"PYTEST_ADDOPTS": "-q --tb=short"}
written = _configure_quiet_cli_env(env2)
assert env2["PYTEST_ADDOPTS"] == "-q --tb=short"
assert "PYTEST_ADDOPTS" not in written
def test_opt_out_disables_injection(monkeypatch) -> None:
for off in ("0", "false", "no", "OFF"):
monkeypatch.setenv("HEADROOM_WRAP_QUIET", off)
assert _quiet_cli_enabled() is False
env: dict[str, str] = {}
assert _configure_quiet_cli_env(env) == []
assert env == {} # nothing injected
def test_enabled_by_default_and_on_truthy(monkeypatch) -> None:
monkeypatch.delenv("HEADROOM_WRAP_QUIET", raising=False)
assert _quiet_cli_enabled() is True
monkeypatch.setenv("HEADROOM_WRAP_QUIET", "1")
assert _quiet_cli_enabled() is True