Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

fix(agent-cli): wire silence_logging at app_callback for full subcommand coverage

Louis Choqueld98eb0e2878c
diff --git a/TODOS.md b/TODOS.md
index 2bd2346a8..5ffeae1a7 100644
--- a/TODOS.md
+++ b/TODOS.md
@@ -43,20 +43,20 @@ A `/code-review` pass found 15 findings. After triage with the user, 6 actionabl
Single architectural shift: every agent CLI command must traverse `app_callback` in `pipelex/cli/agent_cli/_agent_cli.py`. Wiring the silence call there guarantees every current AND future command is covered, even ones (like `init_cmd`, `accept_gateway_terms_cmd`) that bypass `make_pipelex_for_agent_cli`.
-- [ ] **Step 1.1** — Read `pipelex/cli/agent_cli/_agent_cli.py` to locate `app_callback` (around line 81 per the review). Confirm it's the typer choke point every command runs through, and that `set_agent_cli_error_format(CliOutputFormat.JSON)` already lives there.
-- [ ] **Step 1.2** — Add `silence_logging_for_agent_cli()` call at the top of `app_callback`, BEFORE `set_agent_cli_error_format`. Import the symbol from `pipelex.cli.agent_cli.commands.agent_cli_factory`.
-- [ ] **Step 1.3** — Decide: keep the existing per-call invocations in `make_pipelex_for_agent_cli:166` and `agent_doctor_cmd:134` OR remove them.
+- [x] **Step 1.1** — Read `pipelex/cli/agent_cli/_agent_cli.py` to locate `app_callback` (around line 81 per the review). Confirm it's the typer choke point every command runs through, and that `set_agent_cli_error_format(CliOutputFormat.JSON)` already lives there.
+- [x] **Step 1.2** — Add `silence_logging_for_agent_cli()` call at the top of `app_callback`, BEFORE `set_agent_cli_error_format`. Import the symbol from `pipelex.cli.agent_cli.commands.agent_cli_factory`.
+- [x] **Step 1.3** — Decide: keep the existing per-call invocations in `make_pipelex_for_agent_cli:166` and `agent_doctor_cmd:134` OR remove them.
- **Decision recorded:** KEEP both. `silence_logging_for_agent_cli` is idempotent (calling `logging.disable` twice with the same value is a no-op). Keeping preserves defense for direct library callers of `make_pipelex_for_agent_cli` and avoids breaking the unit tests that mock `Pipelex.make` and call the factory directly (they assert `logging.root.manager.disable == LOGGING_LEVEL_OFF` after the call).
-- [ ] **Step 1.4** — Update docstrings: `silence_logging_for_agent_cli`'s docstring says "Must be called at the very start of every agent CLI entry point (`make_pipelex_for_agent_cli`, `agent_doctor_cmd`)" — rewrite to reflect that `app_callback` is now the primary armor and the per-call invocations are belt-and-braces.
+- [x] **Step 1.4** — Update docstrings: `silence_logging_for_agent_cli`'s docstring says "Must be called at the very start of every agent CLI entry point (`make_pipelex_for_agent_cli`, `agent_doctor_cmd`)" — rewrite to reflect that `app_callback` is now the primary armor and the per-call invocations are belt-and-braces.
### ✅ CHECKPOINT 1 — verify before continuing
The structural change is invasive enough that a regression here would mask everything else. STOP here and:
-- [ ] Run `make agent-check` — must be clean.
-- [ ] Run the unit suite for cli/: `.venv/bin/pytest -n auto -m "(dry_runnable or not (inference or llm or img_gen or extract or search)) and not pipelex_api" -o log_level=WARNING --tb=short -q tests/unit/pipelex/cli/`. Must pass.
-- [ ] Run the e2e: `.venv/bin/pytest --tb=short -q tests/e2e/agent_cli/test_stdout_is_clean_json.py`. Must pass — both the original three tests AND the new third-party-leak test.
-- [ ] Manually verify with a fresh shell that `pipelex-agent init --format json` produces a clean stderr in offline mode (no httpx INFO leaks).
+- [x] Run `make agent-check` — must be clean. **Result:** 0 pyright errors, mypy success on 1893 files.
+- [x] Run the unit suite for cli/: `.venv/bin/pytest -n auto -m "(dry_runnable or not (inference or llm or img_gen or extract or search)) and not pipelex_api" -o log_level=WARNING --tb=short -q tests/unit/pipelex/cli/`. Must pass. **Result:** 349 passed in 10.29s.
+- [x] Run the e2e: `.venv/bin/pytest --tb=short -q tests/e2e/agent_cli/test_stdout_is_clean_json.py`. Must pass — both the original three tests AND the new third-party-leak test. **Result:** 3 passed in 3.02s (incl. `test_models_json_stdout_and_stderr_stay_clean_under_third_party_logger_enabled`).
+- [x] Manually verify with a fresh shell that `pipelex-agent init --format json` produces a clean stderr in offline mode (no httpx INFO leaks). **Result:** stderr is exactly the structured `ArgumentError` envelope (no project root in `/tmp`); no log lines. Confirms `app_callback` runs and silences logging even on the `init`/`accept-gateway-terms` paths that bypass `make_pipelex_for_agent_cli` (verified by grep — neither cmd module calls `silence_logging_for_agent_cli` nor the factory).
**Cold-start handoff at this checkpoint:** if the session ends here, the next agent should re-read this TODOS.md (especially the "Context" section above), then `git diff HEAD` to see what's staged. If Step 1.2 landed but the verify failed, the regression is most likely in `_agent_cli.py` — check that `app_callback` runs unconditionally for every command, not gated on some Typer flag. If verify passed, Phase 2 is independent and can start fresh.
diff --git a/pipelex/cli/agent_cli/_agent_cli.py b/pipelex/cli/agent_cli/_agent_cli.py
index a5f8b4072..cccef8254 100644
--- a/pipelex/cli/agent_cli/_agent_cli.py
+++ b/pipelex/cli/agent_cli/_agent_cli.py
@@ -9,6 +9,7 @@ from typer.core import TyperGroup
from typing_extensions import override
from pipelex.cli.agent_cli.commands.accept_gateway_terms_cmd import agent_accept_gateway_terms_cmd
+from pipelex.cli.agent_cli.commands.agent_cli_factory import silence_logging_for_agent_cli
from pipelex.cli.agent_cli.commands.agent_output import CliOutputFormat, agent_error, set_agent_cli_error_format
from pipelex.cli.agent_cli.commands.check_model_cmd import agent_check_model_cmd
from pipelex.cli.agent_cli.commands.concept_cmd import concept_cmd
@@ -99,10 +100,20 @@ def app_callback(
Note: there is no ``--log-level`` flag. ``pipelex-agent`` is machine-consumed:
stdout is reserved for the success envelope and stderr for the error envelope.
- Free-floating logs would corrupt those channels, so pipelex's logs are silenced
- unconditionally by the factory. For verbose debugging, use the human ``pipelex``
- CLI instead.
+ Free-floating logs would corrupt those channels, so Python's logging system is
+ cut off process-wide via ``silence_logging_for_agent_cli`` as the very first
+ action in this callback — covering every subcommand invocation (including ones
+ like ``init`` and ``accept-gateway-terms`` that bypass
+ ``make_pipelex_for_agent_cli``). The ``--version`` eager option short-circuits
+ before this body runs, but its callback only does ``typer.echo`` + ``Exit`` and
+ touches no log path. For verbose debugging, use the human ``pipelex`` CLI instead.
"""
+ # Process-global logging cutoff, armed BEFORE any command body runs and BEFORE any
+ # error-format / runner-validation code below can route through ``log.*``. This is the
+ # primary armor for the stdout/stderr discipline; the per-call invocations inside
+ # ``make_pipelex_for_agent_cli`` and ``agent_doctor_cmd`` are kept as defense-in-depth
+ # for direct library callers that bypass this Typer entry point.
+ silence_logging_for_agent_cli()
# Reset the error-format ContextVar at the single choke point every command passes through, so
# a markdown command cannot leak markdown into a later JSON-only command in the same process.
# --format / --error-format commands override this afterwards; JSON-only commands keep the JSON default.
diff --git a/pipelex/cli/agent_cli/commands/agent_cli_factory.py b/pipelex/cli/agent_cli/commands/agent_cli_factory.py
index e8818faf3..b78a40edd 100644
--- a/pipelex/cli/agent_cli/commands/agent_cli_factory.py
+++ b/pipelex/cli/agent_cli/commands/agent_cli_factory.py
@@ -103,10 +103,14 @@ def silence_logging_for_agent_cli() -> None:
what handlers it has attached. This includes third-party packages we never
enumerate and any logger created AFTER this call.
- Idempotent. Must be called at the very start of every agent CLI entry point
- (``make_pipelex_for_agent_cli``, ``agent_doctor_cmd``) so the cutoff is active
- before any setup code can reach a ``log.*`` call — closing the window between
- ``log.configure`` and ``apply_agent_cli_output_discipline``.
+ Idempotent. The primary call site is ``app_callback`` in
+ ``pipelex.cli.agent_cli._agent_cli`` — Typer routes every ``pipelex-agent``
+ subcommand through that callback, so the cutoff is armed before any command body
+ runs (including commands like ``init`` and ``accept-gateway-terms`` that bypass
+ ``make_pipelex_for_agent_cli``). The additional invocations at the top of
+ ``make_pipelex_for_agent_cli`` and ``agent_doctor_cmd`` are belt-and-braces
+ defense for direct library callers that bypass the Typer entry point (and for
+ the unit tests that drive those factories directly).
Does NOT affect Rich ``Console`` output (banners, tables, pretty_print) — that's
handled by ``console_print_target = STDERR`` + ``PrettyPrinter.mode = SILENT`` +