release_fixtures.py
python
sha256:a977b39d08e15683210a33608e25712fe3ed9c8f05f0388fb69ef38acf835426
feat(release): hermetic release gates in a provisioned, pin…
Opus 5.5
minor
⚠ breaking
12 days ago
| 1 | """Shared fixtures for the release-safety tests (musehub#114 Phase 6). |
| 2 | |
| 3 | ``FakeStorage`` is an in-memory release bucket that records every operation, so a |
| 4 | test can assert the strongest fail-closed property: after a refused operation the |
| 5 | bucket holds *exactly* the same bytes and nothing was written or deleted. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import hashlib |
| 11 | import json |
| 12 | import pathlib |
| 13 | import threading |
| 14 | from collections.abc import Callable, Mapping |
| 15 | from datetime import datetime, timedelta, timezone |
| 16 | |
| 17 | from deploy.release_gate import ( |
| 18 | REQUIRED_GATES, |
| 19 | Candidate, |
| 20 | GateResult, |
| 21 | Provenance, |
| 22 | build_report, |
| 23 | ) |
| 24 | from deploy.release_provenance import CheckoutState |
| 25 | from deploy.release_safety import ExpectedPointer, ObjectExists, StaleState, StorageUnavailable |
| 26 | |
| 27 | NOW = datetime(2026, 11, 3, 12, 0, 0, tzinfo=timezone.utc) |
| 28 | SOURCE_REVISION = "sha256:" + "ab" * 32 |
| 29 | OTHER_REVISION = "sha256:" + "cd" * 32 |
| 30 | CHECKOUT = CheckoutState(SOURCE_REVISION, True, ()) # what Muse reports for the source checkout at verification |
| 31 | |
| 32 | |
| 33 | class FakeStorage: |
| 34 | """In-memory create-only release store with an operation log.""" |
| 35 | |
| 36 | def __init__(self, objects: Mapping[str, bytes] | None = None) -> None: |
| 37 | self.objects: dict[str, bytes] = dict(objects or {}) |
| 38 | self.ops: list[tuple[str, str]] = [] |
| 39 | self.unavailable = False |
| 40 | self.fail_put_new_on: str | None = None |
| 41 | self.fail_put_replace_on: str | None = None # applies to put_if_match |
| 42 | self.versions: dict[str, int] = {} |
| 43 | self.lock = threading.RLock() |
| 44 | self.hook: Callable[[str, str], None] | None = None # (operation, key) called before the operation takes effect |
| 45 | |
| 46 | def _check(self) -> None: |
| 47 | if self.unavailable: |
| 48 | raise StorageUnavailable("fake storage backend is down") |
| 49 | |
| 50 | def _hook(self, op: str, key: str) -> None: |
| 51 | if self.hook: |
| 52 | self.hook(op, key) |
| 53 | |
| 54 | def exists(self, key: str) -> bool: |
| 55 | self._check() |
| 56 | self._hook("exists", key) |
| 57 | with self.lock: |
| 58 | return key in self.objects |
| 59 | |
| 60 | def read(self, key: str) -> bytes: |
| 61 | self._check() |
| 62 | self._hook("read", key) |
| 63 | with self.lock: |
| 64 | return self.objects[key] |
| 65 | |
| 66 | def read_versioned(self, key: str) -> tuple[bytes, str]: |
| 67 | self._check() |
| 68 | self._hook("read_versioned", key) |
| 69 | with self.lock: |
| 70 | return self.objects[key], f"v{self.versions.get(key, 0)}" |
| 71 | |
| 72 | def list_keys(self, prefix: str = "") -> list[str]: |
| 73 | self._check() |
| 74 | with self.lock: |
| 75 | return sorted(k for k in self.objects if k.startswith(prefix)) |
| 76 | |
| 77 | def put_new(self, key: str, data: bytes) -> None: |
| 78 | self._check() |
| 79 | if key == self.fail_put_new_on: |
| 80 | raise StorageUnavailable(f"interrupted while writing {key}") |
| 81 | self._hook("put_new", key) |
| 82 | with self.lock: # the atomic create-only check-and-set |
| 83 | if key in self.objects: |
| 84 | raise ObjectExists(key) |
| 85 | self.ops.append(("put_new", key)) |
| 86 | self.objects[key] = data |
| 87 | self.versions[key] = 1 |
| 88 | |
| 89 | def put_if_match(self, key: str, data: bytes, token: str) -> None: |
| 90 | self._check() |
| 91 | if key == self.fail_put_replace_on: |
| 92 | raise StorageUnavailable(f"interrupted while writing {key}") |
| 93 | self._hook("put_if_match", key) |
| 94 | with self.lock: # the atomic compare-and-swap |
| 95 | if key not in self.objects or f"v{self.versions.get(key, 0)}" != token: |
| 96 | raise StaleState(key) |
| 97 | self.ops.append(("put_if_match", key)) |
| 98 | self.objects[key] = data |
| 99 | self.versions[key] = self.versions.get(key, 0) + 1 |
| 100 | |
| 101 | def delete(self, key: str) -> None: |
| 102 | self._check() |
| 103 | self.ops.append(("delete", key)) |
| 104 | del self.objects[key] |
| 105 | |
| 106 | # -- assertions ----------------------------------------------------- |
| 107 | def writes(self) -> list[tuple[str, str]]: |
| 108 | return [op for op in self.ops if op[0] != "delete"] |
| 109 | |
| 110 | def deletes(self) -> list[str]: |
| 111 | return [key for op, key in self.ops if op == "delete"] |
| 112 | |
| 113 | def snapshot(self) -> dict[str, bytes]: |
| 114 | return dict(self.objects) |
| 115 | |
| 116 | |
| 117 | def make_artifact(tmp: pathlib.Path, version: str, payload: bytes | None = None) -> pathlib.Path: |
| 118 | path = tmp / f"muse-{version}.tar.gz" |
| 119 | path.write_bytes(payload if payload is not None else f"sdist for {version}".encode()) |
| 120 | return path |
| 121 | |
| 122 | |
| 123 | def sha256_of(path: pathlib.Path) -> str: |
| 124 | return hashlib.sha256(path.read_bytes()).hexdigest() |
| 125 | |
| 126 | |
| 127 | def passing_results() -> dict[str, GateResult]: |
| 128 | return {name: GateResult(status="pass", exit_code=0, detail="") for name in REQUIRED_GATES} |
| 129 | |
| 130 | |
| 131 | def good_environment(sha: str, version: str) -> dict[str, object]: |
| 132 | """The environment evidence a successful hermetic run records.""" |
| 133 | return { |
| 134 | "interpreter": {"path": "/opt/python/bin/python3.14", "version": "3.14.4", "implementation": "cpython"}, |
| 135 | "isolated": True, |
| 136 | "requirements_sha256": "1" * 64, |
| 137 | "candidate_sha256": sha, |
| 138 | "packages": {"muse": version, "pytest": "9.0.3", "tree-sitter": "0.25.2"}, |
| 139 | "packages_sha256": "2" * 64, |
| 140 | "cleanup": "removed", |
| 141 | } |
| 142 | |
| 143 | |
| 144 | def make_report( |
| 145 | artifact: pathlib.Path, |
| 146 | *, |
| 147 | version: str | None = None, |
| 148 | results: Mapping[str, GateResult] | None = None, |
| 149 | created_at: datetime | None = None, |
| 150 | source_revision: str = SOURCE_REVISION, |
| 151 | provenance: Provenance | None = None, |
| 152 | environment: Mapping[str, object] | None = None, |
| 153 | ) -> str: |
| 154 | """A report bound to *artifact*'s real bytes (unless a test overrides a field).""" |
| 155 | ver = version if version is not None else artifact.name.removeprefix("muse-").removesuffix(".tar.gz") |
| 156 | candidate = Candidate( |
| 157 | version=ver, |
| 158 | sha256=sha256_of(artifact), |
| 159 | source_revision=source_revision, |
| 160 | installer_revision=2, |
| 161 | ) |
| 162 | prov = provenance or Provenance(source_revision, source_revision, source_revision, True, candidate.sha256) |
| 163 | report = build_report(candidate, results if results is not None else passing_results(), now=created_at or NOW, provenance=prov, environment=environment if environment is not None else good_environment(candidate.sha256, ver)) |
| 164 | return json.dumps(report) |
| 165 | |
| 166 | |
| 167 | def mutate_report(text: str, **changes: object) -> str: |
| 168 | data = json.loads(text) |
| 169 | for dotted, value in changes.items(): |
| 170 | node = data |
| 171 | *parents, leaf = dotted.split("__") |
| 172 | for p in parents: |
| 173 | node = node[p] |
| 174 | node[leaf] = value |
| 175 | return json.dumps(data) |
| 176 | |
| 177 | |
| 178 | STALE = NOW - timedelta(days=30) |
| 179 | |
| 180 | |
| 181 | def clean_status_json(revision: str, **overrides: object) -> str: |
| 182 | """What ``muse status --json`` prints for a clean tree at *revision* (single line, shell-safe).""" |
| 183 | status: dict[str, object] = { |
| 184 | "head_commit": revision, "clean": True, "dirty": False, |
| 185 | "added": [], "modified": [], "deleted": [], "renamed": {}, "untracked": [], "conflict_paths": [], |
| 186 | "staged": {"added": [], "modified": [], "deleted": [], "renamed": {}}, |
| 187 | "unstaged": {"added": [], "modified": [], "deleted": [], "renamed": {}}, |
| 188 | "merge_in_progress": False, "checkout_interrupted": False, |
| 189 | } |
| 190 | status.update(overrides) |
| 191 | return json.dumps(status) |
| 192 | |
| 193 | |
| 194 | def observed(storage: "FakeStorage", env: str) -> ExpectedPointer: |
| 195 | """What an operator would read from ``pointers/<env>.json`` right now (absent if there is none).""" |
| 196 | key = f"pointers/{env}.json" |
| 197 | if key not in storage.objects: |
| 198 | return ExpectedPointer.absent() |
| 199 | data = json.loads(storage.objects[key]) |
| 200 | return ExpectedPointer(data["version"], data["sha256"], data["activated_at"]) |
File History
5 commits
sha256:a977b39d08e15683210a33608e25712fe3ed9c8f05f0388fb69ef38acf835426
feat(release): hermetic release gates in a provisioned, pin…
Opus 5.5
minor
⚠
12 days ago
sha256:675003834b19b109f8cb126e543a064a7bd6256930dd4a80ffcfe87c6eff3f17
fix(release): idempotent concurrent publish (D3) and compar…
Opus 5.5
minor
⚠
12 days ago
sha256:04db68e43d9007348dbd726b2a3e52f73e83c5646a4511c07e19cb9a66e28b5f
fix(release): gate provenance is observed from the checkout…
Opus 5.5
minor
⚠
12 days ago
sha256:be248fd80e5c5381dba3672e40683905135a665b8582a3c4e0db432c4162a441
test(release): red tests for explicit, idempotent, stale-pr…
Opus 5.5
12 days ago
sha256:c1e420ecafdcfabfd74f76e07d538ec0c884be32b14f6062d32cb19690a3d4d3
test(release): red tests for artifact immutability, retenti…
Opus 5.5
12 days ago