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
@@ -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"
"<!-- stewardship:managed:start -->\nfirst\n<!-- stewardship:managed:end -->\n\n"
"<!-- stewardship:managed:start -->\nsecond\n<!-- stewardship:managed:end -->\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"]