diff --git a/src/stewardship/cli.py b/src/stewardship/cli.py index 1b9bb9e..4bf3830 100644 --- a/src/stewardship/cli.py +++ b/src/stewardship/cli.py @@ -192,6 +192,19 @@ def cmd_init(args: argparse.Namespace) -> int: return EXIT_SUCCESS +def _adoption_failure(command: str, exc: adoption.AdoptionError, output: str) -> int: + """Report a refused adoption step as an envelope, not as a bare stderr line. + + Every mode of `adopt` can now raise `AdoptionError`, including `plan`, which + validates the managed block of an existing entrypoint and decides the + knowledge entrypoint by content. Letting those reach the top-level safety net + would print a message and exit, breaking the machine-readable contract that + `--output json` promises. + """ + _write(build_envelope(command, "failure", None, [Finding("error", exc.code, str(exc))]), output) + return EXIT_APPROVAL if exc.code == "APPROVAL_FAILED" else EXIT_VALIDATION_FAILURE + + def cmd_adopt(args: argparse.Namespace) -> int: repo_path = Path(args.repo).expanduser() if not repo_path.exists(): @@ -219,16 +232,19 @@ def cmd_adopt(args: argparse.Namespace) -> int: raise CliError( f"Unknown repository type: {confirmed_type}", EXIT_INVALID_INPUT ) - plan = adoption.build_plan( - repo_path, - assessment, - confirmed_name=name, - confirmed_title=answers.get("title"), - confirmed_owners=answers.get("owners"), - confirmed_lifecycle=answers.get("lifecycle", "active"), - confirmed_type=confirmed_type, - strategy=args.strategy, - ) + try: + plan = adoption.build_plan( + repo_path, + assessment, + confirmed_name=name, + confirmed_title=answers.get("title"), + confirmed_owners=answers.get("owners"), + confirmed_lifecycle=answers.get("lifecycle", "active"), + confirmed_type=confirmed_type, + strategy=args.strategy, + ) + except adoption.AdoptionError as exc: + return _adoption_failure("adopt plan", exc, args.output) _write(build_envelope("adopt plan", "success", plan, []), args.output) return EXIT_SUCCESS @@ -239,8 +255,7 @@ def cmd_adopt(args: argparse.Namespace) -> int: try: written = adoption.apply_plan(repo_path, plan, args.approve) except adoption.AdoptionError as exc: - _write(build_envelope("adopt apply", "failure", None, [Finding("error", exc.code, str(exc))]), args.output) - return EXIT_APPROVAL if exc.code == "APPROVAL_FAILED" else EXIT_VALIDATION_FAILURE + return _adoption_failure("adopt apply", exc, args.output) except ValueError as exc: _write(build_envelope("adopt apply", "failure", None, [Finding("error", "ADOPT_VALIDATION_FAILED", str(exc))]), args.output) return EXIT_VALIDATION_FAILURE diff --git a/tests-universal/test_envelope_and_exit_codes.py b/tests-universal/test_envelope_and_exit_codes.py index 903813a..a1295f2 100644 --- a/tests-universal/test_envelope_and_exit_codes.py +++ b/tests-universal/test_envelope_and_exit_codes.py @@ -78,3 +78,32 @@ def test_human_output_writes_diagnostics_to_stderr_not_stdout(repo: Path) -> Non # A clean validate has no findings to print, but stdout must not carry a JSON/YAML # machine envelope in human mode either. assert "schema:" not in result.stdout and '"schema"' not in result.stdout + + +def test_refused_adopt_plan_is_still_an_envelope(tmp_path: Path) -> None: + """A refused `plan` must fail through the envelope, not the top-level net. + + `plan` validates the managed block of an existing entrypoint and decides the + knowledge entrypoint by content, so it can refuse. Reaching the safety net in + `main` would print a bare line to stderr and exit, which breaks the + machine-readable contract `--output json` promises to its callers. + """ + root = tmp_path / "duplicated-markers" + root.mkdir() + (root / "README.md").write_text("# Sample\n", encoding="utf-8") + (root / "CLAUDE.md").write_text( + "# CLAUDE.md\n\n" + "\nfirst\n\n\n" + "\nsecond\n\n", + encoding="utf-8", + ) + git_init(root) + + result = run_repoctl("adopt", "--repo", str(root), "--mode", "plan", "--output", "json") + assert result.returncode == 1, (result.returncode, result.stderr) + + envelope = result.envelope() + assert envelope["schema"] == RESULT_SCHEMA + assert envelope["command"] == "adopt plan" + assert envelope["outcome"] == "failure" + assert [f["code"] for f in envelope["findings"]] == ["ADOPT_MANAGED_BLOCK_MALFORMED"]