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(doctor): preserve partial health report on bootstrap PipelexConfigError; pin agent CLI discipline before check_models

Louis Choquelcb7d4eed41ca
diff --git a/pipelex/cli/agent_cli/commands/doctor_cmd.py b/pipelex/cli/agent_cli/commands/doctor_cmd.py
index 3721b44e1..4ec8167ca 100644
--- a/pipelex/cli/agent_cli/commands/doctor_cmd.py
+++ b/pipelex/cli/agent_cli/commands/doctor_cmd.py
@@ -162,6 +162,13 @@ def agent_doctor_cmd(
if config_healthy:
try:
setup_doctor_runtime(log_config_overrides=AGENT_CLI_STDERR_LOG_FIELDS, config_dir=config_dir)
+ # Pin discipline BEFORE check_models. setup_doctor_runtime uses
+ # log.configure_if_unset(), which no-ops when a prior process already configured
+ # logging (embedded reuse, interleaved tests) — in that case AGENT_CLI_STDERR_LOG_FIELDS
+ # never reaches the handler, and any log line check_models emits could land on stdout.
+ # apply_agent_cli_output_discipline mutates the existing handler unconditionally via
+ # log.redirect_to_stderr, closing that window before any check fires.
+ apply_agent_cli_output_discipline()
models_healthy, models_message, backend_file_reports = check_models(config_dir=config_dir)
except PipelexConfigError as exc:
# A config can pass check_config_files's shape check and still fail full validation
diff --git a/pipelex/cli/commands/doctor_cmd.py b/pipelex/cli/commands/doctor_cmd.py
index f4204b75a..b38152298 100644
--- a/pipelex/cli/commands/doctor_cmd.py
+++ b/pipelex/cli/commands/doctor_cmd.py
@@ -930,11 +930,23 @@ def do_doctor_cmd(
# check_models requires the hub + log.configure produced by setup_doctor_runtime.
# When the config is broken, skipping setup keeps the friendly translation alive
# (setup_doctor_runtime would raise PipelexConfigError on an invalid config and
- # discard the partial check tuples gathered so far).
+ # discard the partial check tuples gathered so far). The PipelexConfigError arm
+ # handles the layered-override case: a config that passes check_config_files's
+ # shape check on disk can still fail full Pydantic validation inside the bootstrap.
+ models_skipped: bool
+ models_healthy: bool
+ models_message: str
+ backend_file_reports: dict[str, BackendFileReport]
if config_healthy:
- setup_doctor_runtime()
- models_healthy, models_message, backend_file_reports = check_models()
- models_skipped = False
+ try:
+ setup_doctor_runtime()
+ models_healthy, models_message, backend_file_reports = check_models()
+ models_skipped = False
+ except PipelexConfigError as exc:
+ models_healthy = False
+ models_message = f"skipped — {exc.message}"
+ backend_file_reports = {}
+ models_skipped = True
else:
models_healthy = False
models_message = "skipped — fix configuration errors first"
diff --git a/tests/unit/pipelex/cli/test_agent_doctor_cmd.py b/tests/unit/pipelex/cli/test_agent_doctor_cmd.py
index 805c008e0..cee11d0e7 100644
--- a/tests/unit/pipelex/cli/test_agent_doctor_cmd.py
+++ b/tests/unit/pipelex/cli/test_agent_doctor_cmd.py
@@ -273,7 +273,57 @@ class TestAgentDoctorCmd:
agent_doctor_cmd(output_format=CliOutputFormat.JSON)
mock_setup.assert_called_once_with(log_config_overrides=AGENT_CLI_STDERR_LOG_FIELDS, config_dir=None)
- mock_discipline.assert_called_once()
+ mock_discipline.assert_called()
# Pin the contract of the field dict itself: both Rich-managed channels must be stderr.
assert AGENT_CLI_STDERR_LOG_FIELDS["console_log_target"] is ConsoleTarget.STDERR
assert AGENT_CLI_STDERR_LOG_FIELDS["console_print_target"] is ConsoleTarget.STDERR
+
+ def test_discipline_runs_before_check_models(self, mocker: MockerFixture) -> None:
+ """Regression: ``apply_agent_cli_output_discipline`` MUST run before ``check_models``.
+
+ ``setup_doctor_runtime`` calls ``log.configure_if_unset()``, which no-ops when a
+ prior process already configured logging (embedded reuse, interleaved test). In
+ that case ``AGENT_CLI_STDERR_LOG_FIELDS`` never reaches the handler, and any log
+ line ``check_models`` emits can land on stdout — corrupting the JSON envelope.
+
+ Pinning discipline (which mutates the existing handler unconditionally via
+ ``log.redirect_to_stderr``) immediately after the bootstrap closes that window
+ before any check fires.
+ """
+ call_order: list[str] = []
+
+ def record_discipline(*_args: object, **_kwargs: object) -> None:
+ call_order.append("discipline")
+
+ def record_check_models(*_args: object, **_kwargs: object) -> tuple[bool, str, dict[str, object]]:
+ call_order.append("check_models")
+ return True, "OK", {}
+
+ mocker.patch("pipelex.cli.agent_cli.commands.doctor_cmd.setup_doctor_runtime")
+ mocker.patch(
+ "pipelex.cli.agent_cli.commands.doctor_cmd.apply_agent_cli_output_discipline",
+ side_effect=record_discipline,
+ )
+ mocker.patch(
+ "pipelex.cli.agent_cli.commands.doctor_cmd.check_config_files",
+ return_value=(True, 0, "OK"),
+ )
+ mocker.patch(
+ "pipelex.cli.agent_cli.commands.doctor_cmd.check_telemetry_config",
+ return_value=(True, "OK"),
+ )
+ mocker.patch(
+ "pipelex.cli.agent_cli.commands.doctor_cmd.check_backend_credentials",
+ return_value=(True, {}, "OK"),
+ )
+ mocker.patch(
+ "pipelex.cli.agent_cli.commands.doctor_cmd.check_models",
+ side_effect=record_check_models,
+ )
+
+ agent_doctor_cmd(output_format=CliOutputFormat.JSON)
+
+ # discipline must appear before check_models in the call sequence
+ assert "discipline" in call_order
+ assert "check_models" in call_order
+ assert call_order.index("discipline") < call_order.index("check_models")
diff --git a/tests/unit/pipelex/cli/test_doctor_cmd.py b/tests/unit/pipelex/cli/test_doctor_cmd.py
index 38e64470b..529ba0dc7 100644
--- a/tests/unit/pipelex/cli/test_doctor_cmd.py
+++ b/tests/unit/pipelex/cli/test_doctor_cmd.py
@@ -81,6 +81,66 @@ class TestDoctorLayeredResolution:
mock_check_models.assert_called_once_with()
mock_check_deck.assert_called_once_with()
+ def test_pipelex_config_error_from_bootstrap_preserves_partial_report(self, mocker: MockerFixture) -> None:
+ """Regression: a config that passes check_config_files's shape check but fails full
+ validation inside setup_doctor_runtime must still produce the partial health report
+ with models marked as skipped — not crash into the outer "Unexpected error" handler.
+
+ This is the human-CLI counterpart of the agent doctor's
+ test_pipelex_config_error_from_bootstrap_preserves_partial_report: layered overrides
+ can pass the shape check on disk yet fail Pydantic validation inside
+ setup_doctor_runtime, and the user deserves the friendly translation in the report
+ rather than a generic failure message.
+ """
+ from pipelex.base_exceptions import PipelexConfigError # noqa: PLC0415
+
+ mock_setup = mocker.patch(
+ "pipelex.cli.commands.doctor_cmd.setup_doctor_runtime",
+ side_effect=PipelexConfigError("translated validation message"),
+ )
+ mock_check_models = mocker.patch("pipelex.cli.commands.doctor_cmd.check_models")
+ mocker.patch(
+ "pipelex.cli.commands.doctor_cmd.check_config_files",
+ return_value=(True, 0, "All config files present"),
+ )
+ mocker.patch(
+ "pipelex.cli.commands.doctor_cmd.check_telemetry_config",
+ return_value=(True, "OK"),
+ )
+ mocker.patch(
+ "pipelex.cli.commands.doctor_cmd.check_backend_credentials",
+ return_value=(True, {}, "OK"),
+ )
+ mocker.patch(
+ "pipelex.cli.commands.doctor_cmd.check_deck_sync",
+ return_value=(
+ True,
+ DeckSyncReport(kit_version="1.0.0", installed_kit_version="1.0.0", manifest_present=True, files={}),
+ "OK",
+ ),
+ )
+ mock_display = mocker.patch("pipelex.cli.commands.doctor_cmd.display_health_report")
+
+ with pytest.raises(SystemExit) as exc_info:
+ do_doctor_cmd(fix=False)
+ # Exits with non-zero because models check is unhealthy
+ assert exc_info.value.code != 0
+
+ # Bootstrap ran (and raised); check_models was skipped.
+ mock_setup.assert_called_once_with()
+ mock_check_models.assert_not_called()
+
+ # display_health_report was still called, with models marked as skipped + translated message.
+ mock_display.assert_called_once()
+ kwargs = mock_display.call_args.kwargs
+ assert kwargs["models_skipped"] is True
+ assert kwargs["models_healthy"] is False
+ assert "translated validation message" in kwargs["models_message"]
+ # All other partial check tuples survived intact.
+ assert kwargs["config_healthy"] is True
+ assert kwargs["telemetry_healthy"] is True
+ assert kwargs["backends_healthy"] is True
+
def test_telemetry_check_falls_back_to_global(self, mocker: MockerFixture, tmp_path: Path) -> None:
"""When project .pipelex/ exists but has no telemetry.toml, fall back to global ~/.pipelex/."""
global_dir = tmp_path / "global_pipelex"