fix(cli): report a refused plan as an envelope

Only `apply` had a handler for AdoptionError, which was true when the error
type was introduced. `plan` has since gained two ways to refuse: it validates
the managed block of an existing CLAUDE.md to test idempotence, and it now
decides the knowledge entrypoint by content. Both reached the top-level safety
net in main, which prints a bare line to stderr and exits -- so `--output json`
answered a caller with no envelope at all, breaking the machine-readable
contract precisely when something went wrong and the caller needed the code.

The mapping from finding code to exit code now lives in one place rather than
being spelled out per branch.

- add _adoption_failure and use it from both plan and apply
- cover a refused plan end to end, envelope shape and exit code included

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Alexander Baranov
2026-08-24 22:56:58 +03:00
co-authored by Claude Opus 5
parent 6c6dfb6fb6
commit c483d94f36
2 changed files with 56 additions and 12 deletions
+27 -12
View File
@@ -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