diff --git a/cli/docs/features/builtin-group.md b/cli/docs/features/builtin-group.md index 30fcb2c8..f43f857c 100644 --- a/cli/docs/features/builtin-group.md +++ b/cli/docs/features/builtin-group.md @@ -107,6 +107,10 @@ Hiding is an output filter; registered subcommands remain reachable via ` a Rationale: these three flags only have meaning when apcore-cli performs its own filesystem discovery. In embedded mode — where the integrator has pre-built the `registry` and passes it programmatically — the flags are inert (do nothing) and pollute `--help --all-options` output. Omitting them entirely in embedded mode results in cleaner help and an explicit "unknown option" error if a user attempts them. +When Python starts the standalone CLI with `--commands-dir` or `--binding`, +it reports each failed Toolkit registration by module ID and cause. Startup +exits with code 47 before dispatching a command if any registration fails. + **Under `apcli` group:** | Subcommand | Source | Notes | diff --git a/cli/python/src/apcore_cli/factory.py b/cli/python/src/apcore_cli/factory.py index c135ac06..26adac68 100644 --- a/cli/python/src/apcore_cli/factory.py +++ b/cli/python/src/apcore_cli/factory.py @@ -715,8 +715,8 @@ def _apply_toolkit_integration( Toolkit symbols are imported statically at module top — the package is a required runtime dep (``pyproject.toml`` pins - ``apcore-toolkit>=0.7.0``). Operational failures inside scanner / - loader / writer calls still degrade to a WARNING. + ``apcore-toolkit>=0.7.0``). Scan and load errors are logged. Any reported + registration failure aborts CLI startup after the writer has processed the batch. """ if commands_dir is None and binding_path is None: return @@ -754,12 +754,14 @@ def _apply_toolkit_integration( except Exception as e: logger.warning("DisplayResolver failed: %s", e) - try: - writer = RegistryWriter() - writer.write(scanned, registry, allowed_prefixes=allowed_prefixes) - logger.info("RegistryWriter: registered %d toolkit-sourced module(s)", len(scanned)) - except Exception as e: - logger.warning("RegistryWriter failed: %s", e) + results = RegistryWriter().write(scanned, registry, allowed_prefixes=allowed_prefixes) + failures = [result for result in results if not result.verified] + if failures: + details = "\n".join( + f" {result.module_id} — {result.verification_error or 'Registration failed'}" for result in failures + ) + raise RuntimeError(f"Toolkit could not register the following modules\n{details}") + logger.info("RegistryWriter: registered %d toolkit-sourced module(s)", len(results)) # --------------------------------------------------------------------------- diff --git a/cli/python/tests/test_toolkit_integration.py b/cli/python/tests/test_toolkit_integration.py index 412abef5..f366631f 100644 --- a/cli/python/tests/test_toolkit_integration.py +++ b/cli/python/tests/test_toolkit_integration.py @@ -299,6 +299,40 @@ def test_binding_only_writes_to_registry(self, tmp_path): # allowed_prefixes propagated end-to-end. assert fake_writer.write.call_args.kwargs["allowed_prefixes"] == ["sandbox"] + def test_standalone_rejects_failed_binding_registration(self, tmp_path, monkeypatch, capsys): + from apcore_toolkit import ScannedModule + + from apcore_cli.__main__ import main + from apcore_cli.exit_codes import EXIT_CONFIG_NOT_FOUND + + binding = tmp_path / "bad.binding.yaml" + scanned = ScannedModule( + module_id="broken.task", + description="Fixture module with no importable target", + input_schema={"type": "object"}, + output_schema={"type": "object"}, + tags=[], + target="unimportable_apcore_binding_fixture:run", + ) + monkeypatch.chdir(tmp_path) + monkeypatch.setattr( + "sys.argv", + ["apcore-cli", "--extensions-dir", str(tmp_path), "--binding", str(binding), "apcli", "list"], + ) + with ( + patch("apcore_cli.factory.BindingLoader") as loader, + patch("apcore_cli.factory.DisplayResolver") as resolver, + ): + loader.return_value.load.return_value = [scanned] + resolver.return_value.resolve.return_value = [scanned] + with pytest.raises(SystemExit) as exc: + main() + + assert exc.value.code == EXIT_CONFIG_NOT_FOUND + captured = capsys.readouterr() + assert "broken.task" in captured.err + assert "unimportable_apcore_binding_fixture" in captured.err + class TestCommandsDirReadmeExampleE2E: """README.md's "Zero-import way" (`--commands-dir`) example, exercised