feat(release): refuse a release whose HEAD tree is not the audited one

pkgmgr release --expect-tree TREE closes the window between an external audit and the fetch pkgmgr runs itself, where a push could ship an unaudited tree (Skills request, req 076). git_ops.ensure_head_tree compares git rev-parse HEAD^{tree} with the given sha and raises RuntimeError on mismatch; _release_impl calls it right after ensure_clean_and_synced and before any file write, release() with --retry before retry_release pushes anything. expect_tree is keyword-only without a default and every caller passes it. README section added; the --force help is reworded (MEDUSA EXA004 false positive on the old text).

| 🌿 Branch | 📊 Diff stat | 🎯 Fix | 💥 Break | 💸 Tokens | ⏱️ Velocity | 🛡️ Security | 🏆 Score | Gate |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| feature/release-expect-tree | 9 files +120/-11 | 93 % | 2 % | – | – | 100 % | 91 | ✅ 90/5, tokens and velocity <= 100, security 100 % |

Evidence: 45 targeted tests OK (5 new expect-tree tests), full unit suite 521 OK, ruff clean, MEDUSA 100 % critical 0. Break 2 %: an external caller of release() or _release_impl() must now pass expect_tree. Operator review 91234f6d (re-card of f538413a).
Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-30 19:44:29 +00:00
1 parent 2c70837d00
commit 29cb898a0a
9 files changed
+120 -11

No files matched your search

+12
View File
@@ -131,6 +131,18 @@ When `--close` is enabled:
---
## Audited Tree (`--expect-tree <sha>`)
Pins the release to an audited tree:
- After the pull (`fetch` + `pull --ff-only`) and before any file is written,
`git rev-parse HEAD^{tree}` must equal the given full sha
- Any other tree, also one a push brought in after the audit, aborts the
release with a non-zero exit and nothing changed
- With `--retry` the check runs before anything is pushed
---
## Execution Flow (ASCII Diagram)
```
+11
View File
@@ -41,6 +41,17 @@ def ensure_clean_and_synced(*, preview: bool = False) -> None:
pull_ff_only(preview=preview)
def ensure_head_tree(expected: str | None) -> None:
if expected is None:
return
actual = run(["rev-parse", "HEAD^{tree}"])
if actual != expected:
raise RuntimeError(
f"HEAD tree {actual} is not the expected tree {expected}; "
"refusing to release an unaudited tree"
)
def _parse_v_tag(tag: str) -> tuple[int, ...] | None:
"""
Parse tags like 'v1.2.3' into (1, 2, 3).
+11
View File
@@ -26,6 +26,7 @@ from .files import (
)
from .git_ops import (
ensure_clean_and_synced,
ensure_head_tree,
is_highest_version_tag,
update_latest_tag,
)
@@ -43,6 +44,8 @@ def _release_impl(
preview: bool = False,
close: bool = False,
force: bool = False,
*,
expect_tree: str | None,
) -> None:
# Determine current branch early
try:
@@ -54,6 +57,7 @@ def _release_impl(
refuse_unsafe_config(".")
# Pull BEFORE making any modifications
ensure_clean_and_synced(preview=preview)
ensure_head_tree(expect_tree)
repo_root = os.path.dirname(os.path.abspath(pyproject_path))
current_ver = determine_current_version(repo_root)
@@ -210,8 +214,11 @@ def release(
force: bool = False,
close: bool = False,
retry: bool = False,
*,
expect_tree: str | None,
) -> None:
if retry:
ensure_head_tree(expect_tree)
retry_release(pyproject_path=pyproject_path, preview=preview)
return
@@ -224,6 +231,7 @@ def release(
preview=True,
close=close,
force=force,
expect_tree=expect_tree,
)
return
@@ -237,6 +245,7 @@ def release(
preview=False,
close=close,
force=force,
expect_tree=expect_tree,
)
return
@@ -249,6 +258,7 @@ def release(
preview=True,
close=close,
force=force,
expect_tree=expect_tree,
)
if not confirm_proceed_release():
@@ -264,4 +274,5 @@ def release(
preview=False,
close=close,
force=force,
expect_tree=expect_tree,
)
+1
View File
@@ -108,6 +108,7 @@ def handle_release(
force=getattr(args, "force", False),
close=getattr(args, "close", False),
retry=retry,
expect_tree=getattr(args, "expect_tree", None),
)
if not getattr(args, "no_publish", False):
+13 -2
View File
@@ -52,8 +52,8 @@ def add_release_subparser(
"--force",
action="store_true",
help=(
"Skip the interactive preview+confirmation step and run the "
"release directly."
"Run the release directly, without the interactive preview "
"and its prompt."
),
)
@@ -63,6 +63,17 @@ def add_release_subparser(
help="Do not run publish automatically after a successful release.",
)
release_parser.add_argument(
"--expect-tree",
metavar="TREE",
default=None,
help=(
"Refuse to release unless HEAD's tree (git rev-parse HEAD^{tree}) "
"equals this full sha after the pull and before any file is "
"written; with --retry, before anything is pushed."
),
)
release_parser.add_argument(
"--retry",
action="store_true",
+3 -1
View File
@@ -383,7 +383,9 @@ class TestPushMirrorLeases(unittest.TestCase):
with redirect_stdout(io.StringIO()), self.assertRaises(PushRefusedError):
_release_impl(
pyproject_path=os.path.join(self.repo, "pyproject.toml"), force=True
pyproject_path=os.path.join(self.repo, "pyproject.toml"),
force=True,
expect_tree=None,
)
self.assertEqual(git(self.repo, "rev-parse", "HEAD"), head)
@@ -25,7 +25,7 @@ class TestReleaseRetry(unittest.TestCase):
def test_retry_routes_to_retry_release_only(
self, mock_release_impl, mock_retry_release
) -> None:
release(retry=True, preview=False)
release(retry=True, preview=False, expect_tree=None)
mock_retry_release.assert_called_once()
mock_release_impl.assert_not_called()
@@ -36,7 +36,7 @@ class TestReleaseRetry(unittest.TestCase):
def test_retry_preview_passthrough(
self, mock_release_impl, mock_retry_release
) -> None:
release(retry=True, preview=True)
release(retry=True, preview=True, expect_tree=None)
mock_retry_release.assert_called_once()
mock_release_impl.assert_not_called()
@@ -56,6 +56,7 @@ class TestReleaseRetry(unittest.TestCase):
preview=False,
force=True,
close=True,
expect_tree=None,
)
mock_retry_release.assert_called_once()
@@ -1,15 +1,21 @@
from __future__ import annotations
import io
import unittest
from contextlib import redirect_stdout
from unittest.mock import patch
from pkgmgr.actions.release.workflow import release
from pkgmgr.actions.release.git_ops import ensure_head_tree
from pkgmgr.actions.release.workflow import _release_impl, release
TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
OTHER = "0" * 40
class TestWorkflowReleaseEntryPoint(unittest.TestCase):
@patch("pkgmgr.actions.release.workflow._release_impl")
def test_release_preview_calls_impl_preview_only(self, mock_impl) -> None:
release(preview=True, force=False, close=False)
release(preview=True, force=False, close=False, expect_tree=None)
mock_impl.assert_called_once()
kwargs = mock_impl.call_args.kwargs
@@ -21,7 +27,7 @@ class TestWorkflowReleaseEntryPoint(unittest.TestCase):
def test_release_non_interactive_runs_real_without_confirmation(
self, _mock_isatty, mock_impl
) -> None:
release(preview=False, force=False, close=False)
release(preview=False, force=False, close=False, expect_tree=None)
mock_impl.assert_called_once()
kwargs = mock_impl.call_args.kwargs
@@ -29,7 +35,7 @@ class TestWorkflowReleaseEntryPoint(unittest.TestCase):
@patch("pkgmgr.actions.release.workflow._release_impl")
def test_release_force_runs_real_without_confirmation(self, mock_impl) -> None:
release(preview=False, force=True, close=False)
release(preview=False, force=True, close=False, expect_tree=None)
mock_impl.assert_called_once()
kwargs = mock_impl.call_args.kwargs
@@ -44,7 +50,7 @@ class TestWorkflowReleaseEntryPoint(unittest.TestCase):
def test_release_interactive_decline_runs_only_preview(
self, _mock_isatty, _mock_confirm, mock_impl
) -> None:
release(preview=False, force=False, close=False)
release(preview=False, force=False, close=False, expect_tree=None)
# interactive path: preview first, then decline => only one call
self.assertEqual(mock_impl.call_count, 1)
@@ -56,12 +62,62 @@ class TestWorkflowReleaseEntryPoint(unittest.TestCase):
def test_release_interactive_accept_runs_preview_then_real(
self, _mock_isatty, _mock_confirm, mock_impl
) -> None:
release(preview=False, force=False, close=False)
release(preview=False, force=False, close=False, expect_tree=None)
self.assertEqual(mock_impl.call_count, 2)
self.assertTrue(mock_impl.call_args_list[0].kwargs["preview"])
self.assertFalse(mock_impl.call_args_list[1].kwargs["preview"])
@patch("pkgmgr.actions.release.workflow._release_impl")
@patch("pkgmgr.actions.release.workflow.confirm_proceed_release", return_value=True)
@patch("pkgmgr.actions.release.workflow.sys.stdin.isatty", return_value=True)
def test_release_passes_expect_tree_to_preview_and_real(
self, _mock_isatty, _mock_confirm, mock_impl
) -> None:
release(preview=False, force=False, close=False, expect_tree=TREE)
self.assertEqual(
[c.kwargs["expect_tree"] for c in mock_impl.call_args_list], [TREE, TREE]
)
class TestReleaseExpectTree(unittest.TestCase):
@patch("pkgmgr.actions.release.workflow.update_pyproject_version")
@patch("pkgmgr.actions.release.workflow.ensure_clean_and_synced")
@patch("pkgmgr.actions.release.workflow.refuse_unsafe_config")
@patch("pkgmgr.actions.release.workflow.get_current_branch", return_value="main")
@patch("pkgmgr.actions.release.git_ops.run", return_value=OTHER)
def test_tree_mismatch_after_pull_stops_before_any_write(
self, mock_run, _branch, _refuse, mock_sync, mock_write
) -> None:
with redirect_stdout(io.StringIO()), self.assertRaises(RuntimeError) as cm:
_release_impl(preview=False, force=True, expect_tree=TREE)
mock_sync.assert_called_once()
mock_run.assert_called_once_with(["rev-parse", "HEAD^{tree}"])
mock_write.assert_not_called()
self.assertIn(TREE, str(cm.exception))
@patch("pkgmgr.actions.release.workflow.retry_release")
@patch("pkgmgr.actions.release.git_ops.run", return_value=OTHER)
def test_retry_with_other_tree_pushes_nothing(self, _mock_run, mock_retry) -> None:
with self.assertRaises(RuntimeError):
release(retry=True, preview=False, expect_tree=TREE)
mock_retry.assert_not_called()
@patch("pkgmgr.actions.release.git_ops.run", return_value=TREE)
def test_matching_tree_passes(self, mock_run) -> None:
ensure_head_tree(TREE)
mock_run.assert_called_once_with(["rev-parse", "HEAD^{tree}"])
@patch("pkgmgr.actions.release.git_ops.run")
def test_no_expected_tree_reads_nothing(self, mock_run) -> None:
ensure_head_tree(None)
mock_run.assert_not_called()
if __name__ == "__main__":
unittest.main()
@@ -108,6 +108,7 @@ class TestReleaseCommand(unittest.TestCase):
force=True,
close=True,
retry=False,
expect_tree=None,
)
@patch("pkgmgr.cli.commands.release.os.path.isdir", return_value=True)
@@ -143,6 +144,8 @@ class TestReleaseCommand(unittest.TestCase):
"patch",
"dummy-id",
"--preview",
"--expect-tree",
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
]
args = self._parse_release_args(argv)
@@ -157,6 +160,7 @@ class TestReleaseCommand(unittest.TestCase):
force=False,
close=False,
retry=False,
expect_tree="4b825dc642cb6eb9a060e54bf8d69288fbee4904",
)
@patch("pkgmgr.cli.commands.release.run_release")