Skip to content

The agent that grows with you

fix(release): read attempt claims from the remote, not local tags

ethernet23aa9cf4459e
diff --git a/scripts/releases/entrypoint.py b/scripts/releases/entrypoint.py
index 8acafee1f13..b232983f19e 100644
--- a/scripts/releases/entrypoint.py
+++ b/scripts/releases/entrypoint.py
@@ -34,11 +34,23 @@ def _git(repo: Path, *args: str) -> str:
return subprocess.check_output(["git", *args], cwd=repo, text=True, encoding="utf-8").strip()
-def _claims(repo: Path) -> list[str]:
- """Every local attempt ref and abandon marker ref."""
- listed = _git(repo, "tag", "--list", *_ATTEMPT_GLOBS)
- return [ref for ref in listed.splitlines()
- if parse_attempt_ref(ref) or parse_marker_ref(ref)]
+def _claims(repo: Path, remote: str) -> list[str]:
+ """Every attempt ref and abandon marker ref on ``remote``, fetched.
+
+ The remote is the only authority. A local tag that was never pushed (a
+ failed claim, a hand-made ref) is not a claim and must not count. The
+ listed refs are fetched by exact name before this returns, so the list and
+ its objects are one snapshot: a claim pushed after an earlier fetch is
+ local here, and a stray local tag of a listed name is replaced by the
+ remote's.
+ """
+ listed = _git(repo, "ls-remote", "--tags", remote, *(f"refs/tags/{glob}" for glob in _ATTEMPT_GLOBS))
+ refs = (line.split("\t", 1)[1].removeprefix("refs/tags/")
+ for line in listed.splitlines() if "\t" in line)
+ claims = sorted({ref for ref in refs if parse_attempt_ref(ref) or parse_marker_ref(ref)})
+ if claims:
+ _git(repo, "fetch", remote, *(f"+refs/tags/{ref}:refs/tags/{ref}" for ref in claims))
+ return claims
def _claim_commit(repo: Path, tag: str) -> str:
@@ -89,7 +101,7 @@ def _outstanding_attempts(repo: Path, remote: str) -> list[tuple[str, int, str]]
published[version] = bool(_git(repo, "ls-remote", remote, f"refs/tags/v{version}"))
return published[version]
- return outstanding_attempts(_claims(repo), is_published)
+ return outstanding_attempts(_claims(repo, remote), is_published)
def _outstanding_attempt(repo: Path, remote: str) -> tuple[str, int, str] | None:
@@ -143,10 +155,11 @@ def _require_ancestry(repo: Path, commit: str, published_commit: str | None) ->
f"{commit} does not descend from the published stable head {published_commit}")
-def _next_claim_epoch(repo: Path) -> int:
+def _next_claim_epoch(repo: Path, claims: list[str]) -> int:
+ """One past the newest claim epoch; ``claims`` are fetched, so their objects are local."""
epochs = _git(repo, "for-each-ref", "refs/tags/rc.*", "--format=%(refname:strip=2) %(taggerdate:unix)")
previous = [int(stamp) for ref, _, stamp in (line.partition(" ") for line in epochs.splitlines())
- if parse_attempt_ref(ref) and stamp.isdigit()]
+ if ref in claims and parse_attempt_ref(ref) and stamp.isdigit()]
return max(int(time.time()), max(previous, default=0) + 1)
@@ -205,7 +218,8 @@ def release(commit: str, *, bump: str, repo: Path, remote: str, repository: str,
raise ReleaseRefused(
f"v{version} already has a final tag. Its publication is still finishing. "
"Wait for Stable Release Publication, then cut again.")
- attempt = next_attempt(version, _claims(repo))
+ claims = _claims(repo, remote)
+ attempt = next_attempt(version, claims)
tag = attempt_ref(version, attempt)
# Built before the claim: a body GitHub refuses would otherwise burn the attempt.
body = draft_body(version=version, attempt_ref=tag, notes=_changelog(
@@ -216,7 +230,7 @@ def release(commit: str, *, bump: str, repo: Path, remote: str, repository: str,
raise ReleaseRefused(
f"the {tag} draft body is {len(body)} characters; GitHub accepts at most "
f"{GITHUB_BODY_LIMIT}. Nothing was claimed. Re-run with --no-changelog.")
- claim_epoch = _next_claim_epoch(repo)
+ claim_epoch = _next_claim_epoch(repo, claims)
claim = json.dumps({
"schema": 1,
"version": version,
@@ -228,7 +242,9 @@ def release(commit: str, *, bump: str, repo: Path, remote: str, repository: str,
"claimEpoch": claim_epoch,
}, sort_keys=True, separators=(",", ":"))
subprocess.check_output(
- ["git", "tag", "-a", tag, commit, "-m", claim], cwd=repo,
+ # -f replaces a local-only tag of this name (not a claim, so never on
+ # the remote); the push below stays non-forcing.
+ ["git", "tag", "-f", "-a", tag, commit, "-m", claim], cwd=repo,
text=True, encoding="utf-8",
env={**os.environ, "GIT_COMMITTER_DATE": f"@{claim_epoch} +0000"},
)
@@ -333,9 +349,9 @@ def _release_view(tag: str, repository: str, inspect) -> dict | None:
raise
-def _outstanding_ref(repo: Path, version: str) -> str:
+def _outstanding_ref(repo: Path, remote: str, version: str) -> str:
"""The attempt ref whose draft the publication pass must find."""
- attempt = next_attempt(version, _claims(repo)) - 1
+ attempt = next_attempt(version, _claims(repo, remote)) - 1
if attempt < 1:
raise ReleaseRefused(f"stable {version} has no claimed attempt to publish")
return attempt_ref(version, attempt)
@@ -352,7 +368,7 @@ def _preflight_publish(version: str, repository: str, inspect, head_version,
# The draft lives on the attempt ref, never on the final tag and never on
# the old v{version}-rc shape.
_refresh_claims(repo, remote)
- attempt_ref = _outstanding_ref(repo, version)
+ attempt_ref = _outstanding_ref(repo, remote, version)
rows = [(tag, row) for tag in (f"v{version}", attempt_ref)
if (row := _release_view(tag, repository, inspect)) is not None]
if len(rows) != 1 or rows[0][0] != attempt_ref or rows[0][1].get("isDraft") is not True:
@@ -413,7 +429,8 @@ def abandon(version: str, *, repo: Path, remote: str, repository: str, execute,
marker = marker_ref(version, attempt)
message = json.dumps({"schema": 1, "version": version, "attempt": attempt, "attemptRef": tag},
sort_keys=True, separators=(",", ":"))
- _git(repo, "tag", "-a", marker, f"{tag}^{{commit}}", "-m", message)
+ # -f replaces a local-only marker of this name; the push stays non-forcing.
+ _git(repo, "tag", "-f", "-a", marker, f"{tag}^{{commit}}", "-m", message)
try:
_git(repo, "push", remote, f"refs/tags/{marker}")
except subprocess.CalledProcessError as error:
diff --git a/tests/scripts/test_release_entrypoint.py b/tests/scripts/test_release_entrypoint.py
index 0f9ce9c6b17..90f9dd44725 100644
--- a/tests/scripts/test_release_entrypoint.py
+++ b/tests/scripts/test_release_entrypoint.py
@@ -630,6 +630,42 @@ def test_abandon_clears_one_of_two_attempts_a_concurrent_cut_left(source):
_release(source, _advance(source, "later"), execute=_must_not_execute)
+def test_a_local_only_attempt_tag_is_not_a_claim(source):
+ commit = git(source, "rev-parse", "HEAD")
+ _claim(source, "0.21.5", commit)
+ git(source, "tag", "-a", "rc.915-v0.21.5", commit, "-m", "claim")
+
+ assert _abandon(source, "0.21.5")["marker"] == "abandoned-rc.1-v0.21.5"
+ result = _release(source, _advance(source, "later"))
+ assert result["tag"] == "rc.2-v0.21.5"
+
+
+def test_a_local_only_tag_of_the_next_attempt_name_is_replaced_by_the_claim(source):
+ commit = git(source, "rev-parse", "HEAD")
+ _claim(source, "0.21.5", commit)
+ _mark(source, "0.21.5")
+ stray = _advance(source, "stray")
+ git(source, "tag", "-a", "rc.2-v0.21.5", stray, "-m", "stray")
+ later = _advance(source, "later")
+
+ assert _release(source, later)["tag"] == "rc.2-v0.21.5"
+
+ remote = git(source, "ls-remote", "origin", "refs/tags/rc.2-v0.21.5^{}").split()[0]
+ assert remote == later
+ assert git(source, "rev-parse", "rc.2-v0.21.5^{commit}") == later
+
+
+def test_a_local_only_marker_of_the_attempt_name_is_replaced_by_the_abandon(source):
+ commit = git(source, "rev-parse", "HEAD")
+ _claim(source, "0.21.5", commit)
+ git(source, "tag", "-a", "abandoned-rc.1-v0.21.5", commit, "-m", "stray")
+
+ assert _abandon(source, "0.21.5")["marker"] == "abandoned-rc.1-v0.21.5"
+
+ assert git(source, "ls-remote", "origin", "refs/tags/abandoned-rc.1-v0.21.5")
+ assert "stray" not in git(source, "tag", "-l", "abandoned-rc.1-v0.21.5", "--format=%(contents)")
+
+
def _clones(source, tmp_path):
origin = git(source, "remote", "get-url", "origin")
clones = []
@@ -700,6 +736,65 @@ def test_concurrent_claim_loser_reports_the_remote_winner_and_the_version_stays_
assert next_attempt("0.21.5", refs) == 2
+def _claim_pushed_after_the_fetch(monkeypatch, tmp_path, source, version, attempt):
+ """Another maintainer pushes ``rc.<attempt>-v<version>`` right after this process's fetch."""
+ from scripts.releases import entrypoint
+
+ original_git = entrypoint._git
+ pushed = []
+
+ def git_then_a_concurrent_claim(repo, *args):
+ result = original_git(repo, *args)
+ if args[0] == "fetch" and not pushed:
+ pushed.append(True)
+ _origin, (other, _unused) = _clones(source, tmp_path)
+ git(other, "checkout", "--quiet", "origin/main")
+ _claim(other, version, git(other, "rev-parse", "HEAD"), attempt=attempt)
+ return result
+
+ monkeypatch.setattr(entrypoint, "_git", git_then_a_concurrent_claim)
+ return pushed
+
+
+def test_abandon_cleans_up_a_claim_pushed_between_its_fetch_and_its_claim_list(
+ source, tmp_path, monkeypatch):
+ pushed = _claim_pushed_after_the_fetch(monkeypatch, tmp_path, source, "0.21.5", attempt=1)
+ calls = []
+
+ result = _abandon(source, "0.21.5", calls=calls,
+ draft={"tagName": "rc.1-v0.21.5", "isDraft": True, "isPrerelease": False})
+
+ # The attempt was listed after the fetch, so its tag object had to be fetched
+ # before the draft was deleted: the marker is what clears the attempt.
+ assert pushed
+ assert result["marker"] == "abandoned-rc.1-v0.21.5"
+ assert calls == [["gh", "release", "delete", "rc.1-v0.21.5", "--repo", "example/hermes-agent", "--yes"]]
+ assert "refs/tags/abandoned-rc.1-v0.21.5" in git(source, "ls-remote", "origin", "refs/tags/*")
+
+
+def test_a_local_tag_that_differs_from_the_remote_claim_of_that_name_is_refreshed(source):
+ commit = git(source, "rev-parse", "HEAD")
+ _claim(source, "0.21.5", commit)
+ other = _advance(source, "other")
+ git(source, "tag", "-f", "-a", "rc.1-v0.21.5", other, "-m", "stray")
+
+ assert _abandon(source, "0.21.5")["marker"] == "abandoned-rc.1-v0.21.5"
+
+ # The marker records the remote claim's commit, not the stray local tag's.
+ assert git(source, "rev-parse", "abandoned-rc.1-v0.21.5^{commit}") == commit
+
+
+def test_release_refuses_on_a_claim_pushed_between_its_fetch_and_its_claim_list(
+ source, tmp_path, monkeypatch):
+ from scripts.releases.entrypoint import ReleaseRefused
+
+ pushed = _claim_pushed_after_the_fetch(monkeypatch, tmp_path, source, "0.21.5", attempt=1)
+
+ with pytest.raises(ReleaseRefused, match="rc.1-v0.21.5 is outstanding"):
+ _release(source, git(source, "rev-parse", "HEAD"), execute=_must_not_execute)
+ assert pushed
+
+
def test_a_concurrent_cut_of_another_version_is_stopped_before_dispatch(
source, tmp_path, monkeypatch):
from scripts.releases import entrypoint