Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix: restore semantic release versioning

JerrettDavis0e3e03f319e9
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 5de09c135..ebba62afb 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -81,78 +81,10 @@ jobs:
f.write(f"level={level}\n")
PYEOF
- - name: Compute version from canonical + commit height
+ - name: Compute semantic version from canonical + release history
id: ver
run: |
- python - <<'PYEOF'
- import subprocess, os, re, tomllib
-
- with open("pyproject.toml", "rb") as f:
- data = tomllib.load(f)
- canonical = data["project"]["version"]
- major, minor, patch = map(int, re.match(r"(\d+)\.(\d+)\.(\d+)", canonical).groups())
-
- level = os.environ.get("LEVEL", "patch")
- manual = os.environ.get("MANUAL_VER", "")
-
- if manual:
- new_ver = manual
- bump = "manual"
- npm_ver = manual
- canonical_out = ""
- height_out = "0"
- previous_tag_out = ""
- else:
- # Find last tag matching v{canonical}.*
- tag_prefix = f"v{canonical}."
- tag_result = subprocess.run(
- ["git", "tag", "-l", f"{tag_prefix}*"],
- capture_output=True, text=True, check=False
- )
- tags = [t.strip() for t in tag_result.stdout.strip().split("\n") if t.strip()]
-
- if tags:
- # Sort by version number, get highest
- def parse_tag(t):
- v = t.lstrip("v")
- parts = re.match(r"(\d+)\.(\d+)\.(\d+)\.(\d+)", v)
- return tuple(int(x) for x in parts.groups()) if parts else (0, 0, 0, 0)
- tags.sort(key=parse_tag, reverse=True)
- last_tag = tags[0]
- prev_height = int(last_tag.rsplit(".", 1)[-1])
-
- # Count commits since the last tagged release
- count_result = subprocess.run(
- ["git", "rev-list", f"{last_tag}..HEAD", "--count"],
- capture_output=True, text=True, check=False
- )
- height = int(count_result.stdout.strip())
- else:
- # No prior tag at this canonical level — height = 0
- prev_height = 0
- height = 0
-
- # Determine bump
- if level == "major":
- new_ver = f"{major + 1}.0.0.0"
- npm_ver = f"{major + 1}.0.0"
- elif level == "minor":
- new_ver = f"{major}.{minor + 1}.0.0"
- npm_ver = f"{major}.{minor + 1}.0"
- else: # patch
- new_ver = f"{canonical}.{prev_height + 1}"
- npm_ver = f"{major}.{minor}.{patch + 1}"
-
- bump = level
-
- with open(os.environ["GITHUB_OUTPUT"], "a") as f:
- f.write(f"version={new_ver}\n")
- f.write(f"npm_version={npm_ver}\n")
- f.write(f"canonical={canonical_out if manual else canonical}\n")
- f.write(f"height={height_out if manual else height}\n")
- f.write(f"bump={bump}\n")
- f.write(f"previous_tag={previous_tag_out if manual else (last_tag if tags else '')}\n")
- PYEOF
+ python -m headroom.release_version
env:
LEVEL: ${{ steps.bump.outputs.level }}
MANUAL_VER: ${{ github.event.inputs.version }}
diff --git a/headroom/release_version.py b/headroom/release_version.py
new file mode 100644
index 000000000..21c9a38d5
--- /dev/null
+++ b/headroom/release_version.py
@@ -0,0 +1,193 @@
+"""Release version helpers for the GitHub Actions release workflow."""
+
+from __future__ import annotations
+
+import os
+import re
+import subprocess
+import tomllib
+from collections.abc import Sequence
+from dataclasses import dataclass, replace
+from pathlib import Path
+
+SEMVER_RE = re.compile(r"^(\d+)\.(\d+)\.(\d+)$")
+RELEASE_TAG_RE = re.compile(r"^v(\d+)\.(\d+)\.(\d+)(?:\.(\d+))?$")
+
+
+@dataclass(frozen=True, order=True)
+class SemVer:
+ """Semantic version tuple with simple bump helpers."""
+
+ major: int
+ minor: int
+ patch: int
+
+ @classmethod
+ def parse(cls, value: str) -> SemVer:
+ match = SEMVER_RE.match(value)
+ if not match:
+ raise ValueError(f"Invalid semantic version: {value}")
+ return cls(*(int(part) for part in match.groups()))
+
+ def bump(self, level: str) -> SemVer:
+ if level == "major":
+ return SemVer(self.major + 1, 0, 0)
+ if level == "minor":
+ return SemVer(self.major, self.minor + 1, 0)
+ if level == "patch":
+ return SemVer(self.major, self.minor, self.patch + 1)
+ raise ValueError(f"Unsupported bump level: {level}")
+
+ def __str__(self) -> str:
+ return f"{self.major}.{self.minor}.{self.patch}"
+
+
+@dataclass(frozen=True)
+class ReleaseVersionInfo:
+ """Workflow outputs for release version calculation."""
+
+ version: str
+ npm_version: str
+ canonical: str
+ height: str
+ bump: str
+ previous_tag: str
+
+ def as_outputs(self) -> dict[str, str]:
+ return {
+ "version": self.version,
+ "npm_version": self.npm_version,
+ "canonical": self.canonical,
+ "height": self.height,
+ "bump": self.bump,
+ "previous_tag": self.previous_tag,
+ }
+
+
+def normalize_release_tag(tag: str) -> SemVer:
+ """Collapse historic 4-part release tags into a standard semver version."""
+
+ match = RELEASE_TAG_RE.match(tag)
+ if not match:
+ raise ValueError(f"Invalid release tag: {tag}")
+ major, minor, patch, extra = match.groups()
+ return SemVer(int(major), int(minor), int(patch) + int(extra or 0))
+
+
+def find_latest_release_tag(tags: Sequence[str]) -> str | None:
+ """Return the latest release tag after normalizing legacy 4-part tags."""
+
+ candidates: list[tuple[SemVer, str]] = []
+ for tag in tags:
+ if RELEASE_TAG_RE.match(tag):
+ candidates.append((normalize_release_tag(tag), tag))
+ if not candidates:
+ return None
+ candidates.sort(reverse=True)
+ return candidates[0][1]
+
+
+def compute_release_version(
+ canonical_version: str,
+ level: str,
+ tags: Sequence[str],
+ manual_version: str = "",
+) -> ReleaseVersionInfo:
+ """Compute the next release version from the canonical version and existing tags."""
+
+ if manual_version:
+ return ReleaseVersionInfo(
+ version=manual_version,
+ npm_version=manual_version,
+ canonical=canonical_version,
+ height="0",
+ bump="manual",
+ previous_tag="",
+ )
+
+ canonical = SemVer.parse(canonical_version)
+ previous_tag = find_latest_release_tag(tags)
+ current = canonical
+ if previous_tag is not None:
+ current = max(current, normalize_release_tag(previous_tag))
+
+ next_version = str(current.bump(level))
+ return ReleaseVersionInfo(
+ version=next_version,
+ npm_version=next_version,
+ canonical=canonical_version,
+ height="0",
+ bump=level,
+ previous_tag=previous_tag or "",
+ )
+
+
+def get_canonical_version(root: Path) -> str:
+ """Read the canonical project version from pyproject.toml."""
+
+ with open(root / "pyproject.toml", "rb") as file:
+ data = tomllib.load(file)
+ return data["project"]["version"]
+
+
+def list_release_tags(root: Path) -> list[str]:
+ """List release tags from the local Git checkout."""
+
+ result = subprocess.run(
+ ["git", "tag", "-l", "v*"],
+ cwd=root,
+ check=True,
+ capture_output=True,
+ text=True,
+ )
+ return [tag.strip() for tag in result.stdout.splitlines() if tag.strip()]
+
+
+def commit_height_since(root: Path, previous_tag: str) -> str:
+ """Count commits since the previous release tag for changelog/debug outputs."""
+
+ if not previous_tag:
+ return "0"
+
+ result = subprocess.run(
+ ["git", "rev-list", f"{previous_tag}..HEAD", "--count"],
+ cwd=root,
+ check=True,
+ capture_output=True,
+ text=True,
+ )
+ return result.stdout.strip() or "0"
+
+
+def write_github_outputs(info: ReleaseVersionInfo, output_path: str) -> None:
+ """Append workflow outputs to the GitHub Actions output file."""
+
+ with open(output_path, "a", encoding="utf-8") as output_file:
+ for key, value in info.as_outputs().items():
+ output_file.write(f"{key}={value}\n")
+
+
+def main() -> None:
+ root = Path.cwd()
+ manual_version = os.environ.get("MANUAL_VER", "").strip()
+ level = os.environ.get("LEVEL", "patch").strip() or "patch"
+
+ info = compute_release_version(
+ canonical_version=get_canonical_version(root),
+ level=level,
+ tags=list_release_tags(root),
+ manual_version=manual_version,
+ )
+ info = replace(info, height=commit_height_since(root, info.previous_tag))
+
+ output_path = os.environ.get("GITHUB_OUTPUT", "").strip()
+ if output_path:
+ write_github_outputs(info, output_path)
+ return
+
+ for key, value in info.as_outputs().items():
+ print(f"{key}={value}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/tests/test_release_version.py b/tests/test_release_version.py
new file mode 100644
index 000000000..d40a9f429
--- /dev/null
+++ b/tests/test_release_version.py
@@ -0,0 +1,63 @@
+"""Tests for release version normalization and bumping."""
+
+from headroom.release_version import compute_release_version, normalize_release_tag
+
+
+def test_normalize_release_tag_preserves_three_part_tag() -> None:
+ assert str(normalize_release_tag("v0.5.20")) == "0.5.20"
+
+
+def test_normalize_release_tag_collapses_four_part_tag() -> None:
+ assert str(normalize_release_tag("v0.5.25.2")) == "0.5.27"
+
+
+def test_compute_patch_release_from_four_part_history() -> None:
+ info = compute_release_version(
+ canonical_version="0.5.25",
+ level="patch",
+ tags=["v0.5.20", "v0.5.25.1", "v0.5.25.2"],
+ )
+
+ assert info.version == "0.5.28"
+ assert info.npm_version == "0.5.28"
+ assert info.previous_tag == "v0.5.25.2"
+ assert info.bump == "patch"
+
+
+def test_compute_minor_release_from_four_part_history() -> None:
+ info = compute_release_version(
+ canonical_version="0.5.25",
+ level="minor",
+ tags=["v0.5.20", "v0.5.25.1", "v0.5.25.2"],
+ )
+
+ assert info.version == "0.6.0"
+ assert info.npm_version == "0.6.0"
+ assert info.previous_tag == "v0.5.25.2"
+ assert info.bump == "minor"
+
+
+def test_compute_patch_release_from_canonical_without_tags() -> None:
+ info = compute_release_version(
+ canonical_version="0.5.25",
+ level="patch",
+ tags=[],
+ )
+
+ assert info.version == "0.5.26"
+ assert info.npm_version == "0.5.26"
+ assert info.previous_tag == ""
+
+
+def test_manual_version_override_uses_single_semver() -> None:
+ info = compute_release_version(
+ canonical_version="0.5.25",
+ level="patch",
+ tags=["v0.5.25.2"],
+ manual_version="0.6.0",
+ )
+
+ assert info.version == "0.6.0"
+ assert info.npm_version == "0.6.0"
+ assert info.previous_tag == ""
+ assert info.bump == "manual"