diff --git a/.agents/skills/pwragent-release/SKILL.md b/.agents/skills/pwragent-release/SKILL.md index f0d4d698efb1..27ed92a67aa2 100644 --- a/.agents/skills/pwragent-release/SKILL.md +++ b/.agents/skills/pwragent-release/SKILL.md @@ -11,10 +11,10 @@ Use the repository's downstream pipeline. Read [the distribution contract](../.. - Verify the fork remote, `pwragent` source commit, working tree, recent CI results, existing remote tags, and existing GitHub releases. Preserve unrelated work. Investigate relevant CI failures before tagging, including failures that happen before compilation. - The established naming is `pwragent-v-pwragent.`. For example, upstream `rust-v0.155.0-alpha.9` becomes downstream version `0.155.0-pwragent.1` and tag `pwragent-v0.155.0-pwragent.1`. Drop the upstream prerelease suffix; the downstream separator is `-pwragent.`, not `.pwragent.`. Choose an unused sequence number for the baseline and honor an explicitly requested version when the workflow supports it. -- The source can remain based on an upstream alpha tag. The tag-derived `CODEX_BUILD_VERSION` stamps the downstream version into packaged executables; do not change the Rust workspace version merely to duplicate that stamp. -- Keep `scripts/pwragent-release/upstream-version.txt` aligned with the intended numeric upstream baseline. It controls untagged development builds; tagged builds derive their version from the tag. +- The source can remain based on an upstream alpha tag. The tag-derived `CODEX_BUILD_VERSION` stamps the downstream version into `--version` output. Leave `codex-rs/Cargo.toml` at upstream's `0.0.0` placeholder in Git; the workflows stamp it at build time (see below). +- Keep `scripts/pwragent-release/upstream-version.txt` aligned with the intended numeric upstream baseline. Build jobs stamp it into the Cargo workspace version with `stamp-workspace-version.py`, so it becomes `CARGO_PKG_VERSION` and the backend `/models` `client_version`. A wrong baseline hides models. The tag version must start with `-`, otherwise the build fails. Untagged development builds also derive their version from it. - Check that Cargo's lockfile matches the workspace, using `cargo metadata --locked --filter-platform --format-version 1`. Do not discard required workspace-version updates as cosmetic churn. Follow AGENTS.md for dependency/lock changes, including `just bazel-lock-update`. -- Run `python3 scripts/pwragent-release/check-release-signing.py` and the manifest tests (`python3 -m unittest discover -s scripts/pwragent-release -p 'test_*.py'`). Reuse still-applicable build/test evidence from the sync; additional tests should address actual changes or unresolved concerns. +- Run `python3 scripts/pwragent-release/check-release-signing.py` and the release script tests (`python3 -m unittest discover -s scripts/pwragent-release -p 'test_*.py'`). Reuse still-applicable build/test evidence from the sync; additional tests should address actual changes or unresolved concerns. - When checking local Code Mode binaries, rebuild helpers from the release source and run the host and deferred-reducer smoke scripts. Read each script's arguments. For V8 download failures, use the exact-version, checksum-verified assets configured by [setup-rusty-v8](../../../.github/actions/setup-rusty-v8/action.yml), following [the V8 README](../../../third_party/v8/README.md). ## Trigger publication diff --git a/.github/workflows/pwragent-macos-unsigned.yml b/.github/workflows/pwragent-macos-unsigned.yml index dfb3f978ae50..3682e4b9b403 100644 --- a/.github/workflows/pwragent-macos-unsigned.yml +++ b/.github/workflows/pwragent-macos-unsigned.yml @@ -96,10 +96,15 @@ jobs: CODEX_BUILD_VERSION: ${{ steps.version.outputs.version }} run: | set -euo pipefail + # Upstream main keeps the workspace at 0.0.0. Stamp the real upstream + # version so CARGO_PKG_VERSION (and the backend client_version) is right. + python3 ../scripts/pwragent-release/stamp-workspace-version.py stamp \ + --expect-release-version "$CODEX_BUILD_VERSION" cargo build --locked --target "$TARGET" --release \ --bin codex \ --bin codex-app-server \ --bin codex-code-mode-host + python3 ../scripts/pwragent-release/stamp-workspace-version.py verify - name: Save Cargo cache if: always() diff --git a/.github/workflows/pwragent-release-check.yml b/.github/workflows/pwragent-release-check.yml index e64594fc30fa..1e86872109c9 100644 --- a/.github/workflows/pwragent-release-check.yml +++ b/.github/workflows/pwragent-release-check.yml @@ -12,6 +12,7 @@ on: - ".github/workflows/pwragent-release*.yml" - ".github/workflows/pwragent-macos-unsigned.yml" - "docs/pwragent-distribution.md" + - "codex-rs/Cargo.toml" - "scripts/pwragent-release/**" push: branches: @@ -20,6 +21,7 @@ on: - ".github/workflows/pwragent-release*.yml" - ".github/workflows/pwragent-macos-unsigned.yml" - "docs/pwragent-distribution.md" + - "codex-rs/Cargo.toml" - "scripts/pwragent-release/**" permissions: @@ -42,8 +44,23 @@ jobs: - name: Validate fail-closed signing workflow run: python3 scripts/pwragent-release/check-release-signing.py - - name: Validate update manifest contract - run: python3 -m unittest scripts/pwragent-release/test_generate_update_manifest.py + - name: Install Rust toolchain + uses: dtolnay/rust-toolchain@d124f86f28ff42855784ff3319f48ac182c248f8 # 1.99.1 + + - name: Validate release script contracts + run: python3 -m unittest discover -s scripts/pwragent-release -p 'test_*.py' + + # Release builds stamp the upstream version over the 0.0.0 placeholder. + # Prove that still works on this tree (e.g. after an upstream rebase + # changes the workspace layout) without waiting for a release build. + - name: Validate workspace version stamping + run: | + set -euo pipefail + python3 scripts/pwragent-release/stamp-workspace-version.py stamp + python3 scripts/pwragent-release/stamp-workspace-version.py verify + # Resolves against the index only; unlike `cargo metadata` it does not + # download every dependency's sources. + (cd codex-rs && cargo update --workspace --locked) trusted-signing-preparation: name: Prepare TrustedSigning client diff --git a/.github/workflows/pwragent-release.yml b/.github/workflows/pwragent-release.yml index 96bd18d89445..3e843609a606 100644 --- a/.github/workflows/pwragent-release.yml +++ b/.github/workflows/pwragent-release.yml @@ -89,6 +89,10 @@ jobs: echo "::error::Resolved version is not SemVer: $version" >&2 exit 1 fi + # Fail before the build matrix starts if the tag drifted from the + # upstream baseline the build jobs stamp into Cargo.toml. + python3 scripts/pwragent-release/stamp-workspace-version.py \ + check-release-version "$version" echo "version=$version" >> "$GITHUB_OUTPUT" build: @@ -186,6 +190,10 @@ jobs: shell: bash run: | set -euo pipefail + # Upstream main keeps the workspace at 0.0.0. Stamp the real upstream + # version so CARGO_PKG_VERSION (and the backend client_version) is right. + python3 ../scripts/pwragent-release/stamp-workspace-version.py stamp \ + --expect-release-version "$CODEX_VERSION" export CODEX_BUILD_VERSION="$CODEX_VERSION" # Avoid overlapping memory-heavy CLI and app-server compilation on # hosted macOS and ARM runners; retain the normal release profile. @@ -196,6 +204,7 @@ jobs: --bin codex \ --bin codex-app-server \ --bin codex-code-mode-host + python3 ../scripts/pwragent-release/stamp-workspace-version.py verify - name: Save Cargo cache if: always() @@ -543,6 +552,9 @@ jobs: run: | set -euo pipefail export LIBSQLITE3_FLAGS=SQLITE_DISABLE_INTRINSIC + # See the Unix build: stamp the real upstream workspace version. + python ../scripts/pwragent-release/stamp-workspace-version.py stamp \ + --expect-release-version "$CODEX_VERSION" export CODEX_BUILD_VERSION="$CODEX_VERSION" cargo build --target x86_64-pc-windows-msvc --release \ --bin codex \ @@ -550,6 +562,7 @@ jobs: --bin codex-code-mode-host \ --bin codex-windows-sandbox-setup \ --bin codex-command-runner + python ../scripts/pwragent-release/stamp-workspace-version.py verify - name: Save Cargo cache if: always() diff --git a/docs/pwragent-distribution.md b/docs/pwragent-distribution.md index b2a1079eafde..10b6769e7d11 100644 --- a/docs/pwragent-distribution.md +++ b/docs/pwragent-distribution.md @@ -205,6 +205,24 @@ binaries. Development artifacts use the reviewed upstream compatibility baseline each staged binary with `--version` before packaging, so `PWRAGENT-BUILD.txt` and the executable identity cannot silently diverge. +`CODEX_BUILD_VERSION` only reaches `--version` and a few user-agent strings. +Everything else reads `CARGO_PKG_VERSION`, including the `client_version` the +models manager sends to the backend `/models` endpoint, which hides models +whose minimum client version is higher. Upstream `main` keeps +`codex-rs/Cargo.toml` at the `0.0.0` placeholder and writes the real version +only on its release tags, so every build job runs +`scripts/pwragent-release/stamp-workspace-version.py stamp` before `cargo +build`. That writes the plain upstream version from `upstream-version.txt` into +`[workspace.package]` and the matching `Cargo.lock` entries, so `--locked` +builds still work. The workspace version is never committed. The metadata job +first requires the downstream version to start with that upstream version, so a +tag that drifted from the baseline fails before any build. After the build, +`stamp-workspace-version.py verify` fails the job unless every crate that +inherits the workspace version has that version in both `Cargo.toml` and +`Cargo.lock`. +`pwragent-release-check.yml` runs the same stamp-and-verify on every relevant +change, without a build. + ### Manually (`workflow_dispatch`) A manual run builds every platform but enters no signing environment. It emits diff --git a/scripts/pwragent-release/check-release-signing.py b/scripts/pwragent-release/check-release-signing.py index 6c71eda1dca5..a527d4e86e1f 100644 --- a/scripts/pwragent-release/check-release-signing.py +++ b/scripts/pwragent-release/check-release-signing.py @@ -147,6 +147,34 @@ def job(workflow: str, name: str) -> str: for binary in UNIX_BINARIES: require(build, f"--bin {binary}", "build") +# Upstream main keeps codex-rs/Cargo.toml at version 0.0.0, and CARGO_PKG_VERSION +# is what the backend /models request sends as client_version. Every shipped or +# validated build must stamp the upstream version before compiling and verify +# it after, because the --version checks only see CODEX_BUILD_VERSION. +STAMP_SCRIPT = "../scripts/pwragent-release/stamp-workspace-version.py" + + +def require_workspace_version_stamp(section: str, scope: str, python: str) -> None: + stamp_command = f"{python} {STAMP_SCRIPT} stamp" + verify_command = f"{python} {STAMP_SCRIPT} verify" + require(section, stamp_command, scope) + require(section, "--expect-release-version", scope) + require(section, verify_command, scope) + build_at = section.find("cargo build") + if not section.find(stamp_command) < build_at < section.find(verify_command): + fail(f"{scope} must stamp the workspace version before cargo build and verify it after") + + +require_workspace_version_stamp(build, "build", "python3") +require_workspace_version_stamp(windows_prepare, "windows-prepare", "python") +require_workspace_version_stamp(unsigned_workflow, "unsigned macOS workflow", "python3") +for fragment in ( + "scripts/pwragent-release/stamp-workspace-version.py stamp", + "scripts/pwragent-release/stamp-workspace-version.py verify", + "discover -s scripts/pwragent-release -p 'test_*.py'", +): + require(check_workflow, fragment, "release signing check workflow") + for fragment in ( "scripts/pwragent-release/prepare-trusted-signing.ps1", "-OutputRoot signing-tools", diff --git a/scripts/pwragent-release/stamp-workspace-version.py b/scripts/pwragent-release/stamp-workspace-version.py new file mode 100644 index 000000000000..9f0e5f0d6f79 --- /dev/null +++ b/scripts/pwragent-release/stamp-workspace-version.py @@ -0,0 +1,274 @@ +#!/usr/bin/env python3 +"""Stamp and verify the Rust workspace version for PwrAgent Codex builds. + +Upstream keeps `version = "0.0.0"` in `codex-rs/Cargo.toml` on main and only +writes the real version on its release tags. This fork builds from +non-release upstream commits, so without stamping every +`env!("CARGO_PKG_VERSION")` in the binaries reports 0.0.0. The most visible +consequence is the models manager sending `client_version=0.0.0` to the +backend `/models` endpoint, which then hides every model gated on a minimum +client version. + +`CODEX_BUILD_VERSION` (the downstream `X.Y.Z-pwragent.N` string) only feeds the +`--version` output and a few user-agent sites. It is not a substitute for the +Cargo package version, and the `--version` checks cannot detect this failure. + +Usage, from the repository root: + + stamp-workspace-version.py stamp [--expect-release-version VERSION] + stamp-workspace-version.py check-release-version VERSION + stamp-workspace-version.py verify + +`stamp` writes the plain upstream SemVer from +`scripts/pwragent-release/upstream-version.txt` into `[workspace.package]` and +into the `Cargo.lock` entries of the workspace members that inherit it, so +`--locked` builds keep working. It is idempotent. + +`check-release-version` only checks that VERSION starts with the upstream +version, so a release can fail before any build job starts. + +`verify` fails unless every member that inherits the workspace version, +including the ones that report the client version to the backend, has exactly +that version in both their resolved manifests and +`Cargo.lock`. +""" + +from __future__ import annotations + +import argparse +import json +from pathlib import Path +import re +import subprocess +import sys +from typing import Iterable + + +ROOT = Path(__file__).resolve().parents[2] +UPSTREAM_VERSION_PATH = ROOT / "scripts/pwragent-release/upstream-version.txt" +WORKSPACE_DIR = ROOT / "codex-rs" + +# Crates whose CARGO_PKG_VERSION reaches the backend or the shipped binaries. +# `codex-models-manager` builds the `/models` `client_version` query. +GUARDED_PACKAGES = ( + "codex-models-manager", + "codex-cli", + "codex-app-server", + "codex-code-mode-host", +) + +SEMVER_CORE = re.compile(r"^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$") +# `version` inside the [workspace.package] table: skip its other lines (which +# may contain arrays or comments) but never cross into the next table header. +WORKSPACE_PACKAGE_VERSION = re.compile( + r'(?m)(^\[workspace\.package\][^\n]*\n(?:(?!\[)[^\n]*\n)*?version\s*=\s*")([^"]*)(")' +) +INHERITS_WORKSPACE_VERSION = re.compile( + r"(?m)^version(\.workspace\s*=\s*true|\s*=\s*\{\s*workspace\s*=\s*true\s*\})" +) + + +class StampError(Exception): + pass + + +def read_upstream_version(path: Path = UPSTREAM_VERSION_PATH) -> str: + version = path.read_text(encoding="utf-8").strip() + if not SEMVER_CORE.match(version): + raise StampError( + f"{path.name} must hold a plain MAJOR.MINOR.PATCH version, got {version!r}" + ) + if version == "0.0.0": + raise StampError(f"{path.name} must not be the 0.0.0 placeholder") + return version + + +def check_release_version(release_version: str, upstream_version: str) -> None: + """Require the downstream version to be built on the stamped upstream one.""" + if release_version == upstream_version: + return + if release_version.startswith(f"{upstream_version}-") or release_version.startswith( + f"{upstream_version}+" + ): + return + raise StampError( + f"release version {release_version!r} is not based on upstream version " + f"{upstream_version!r}; update scripts/pwragent-release/upstream-version.txt" + ) + + +def read_text(path: Path) -> str: + # newline="" keeps CRLF checkouts on Windows byte-for-byte intact. + with path.open(encoding="utf-8", newline="") as handle: + return handle.read() + + +def write_text(path: Path, text: str) -> None: + with path.open("w", encoding="utf-8", newline="") as handle: + handle.write(text) + + +def workspace_package_version(manifest: str) -> str: + match = WORKSPACE_PACKAGE_VERSION.search(manifest) + if match is None: + raise StampError("codex-rs/Cargo.toml has no [workspace.package] version") + return match.group(2) + + +def stamp_manifest(manifest: str, version: str) -> str: + workspace_package_version(manifest) + return WORKSPACE_PACKAGE_VERSION.sub( + lambda match: f"{match.group(1)}{version}{match.group(3)}", manifest, count=1 + ) + + +LOCK_VERSION = re.compile(r'(?m)^version = "([^"]*)"') + + +def lockfile_blocks(lockfile: str) -> tuple[str, list[str]]: + """Split a lockfile into its blank-line separated tables, keeping CRLF.""" + separator = "\r\n\r\n" if "\r\n" in lockfile else "\n\n" + return separator, lockfile.split(separator) + + +def local_package_name(block: str) -> str | None: + """Name of a local (sourceless) `[[package]]` table, else None.""" + if not block.startswith("[[package]]") or re.search(r"(?m)^source = ", block): + return None + name = re.search(r'(?m)^name = "([^"]+)"', block) + return name.group(1) if name else None + + +def stamp_lockfile(lockfile: str, members: Iterable[str], version: str) -> str: + """Rewrite the version of each named local (sourceless) lockfile package.""" + pending = set(members) + separator, blocks = lockfile_blocks(lockfile) + for index, block in enumerate(blocks): + name = local_package_name(block) + if name not in pending: + continue + blocks[index] = LOCK_VERSION.sub(f'version = "{version}"', block, count=1) + pending.discard(name) + if pending: + raise StampError( + "Cargo.lock has no local entry for workspace members: " + + ", ".join(sorted(pending)) + ) + return separator.join(blocks) + + +def local_lockfile_versions(lockfile: str) -> dict[str, str]: + """Map each local (sourceless) lockfile package name to its version.""" + versions = {} + for block in lockfile_blocks(lockfile)[1]: + name = local_package_name(block) + version = LOCK_VERSION.search(block) + if name and version: + versions[name] = version.group(1) + return versions + + +def cargo_metadata(workspace_dir: Path) -> dict: + # --no-deps needs neither the network nor the registry; it does not check + # Cargo.lock either, so verify() reads the lockfile itself. + command = ["cargo", "metadata", "--format-version", "1", "--no-deps", "--offline"] + result = subprocess.run( + command, cwd=workspace_dir, check=False, capture_output=True, text=True + ) + if result.returncode != 0: + raise StampError(f"{' '.join(command)} failed:\n{result.stderr.strip()}") + return json.loads(result.stdout) + + +def version_inheriting_members(metadata: dict) -> list[str]: + members = set(metadata["workspace_members"]) + names = [] + for package in metadata["packages"]: + if package["id"] not in members: + continue + manifest = read_text(Path(package["manifest_path"])) + if INHERITS_WORKSPACE_VERSION.search(manifest): + names.append(package["name"]) + return sorted(names) + + +def stamp(workspace_dir: Path, version: str) -> None: + manifest_path = workspace_dir / "Cargo.toml" + lockfile_path = workspace_dir / "Cargo.lock" + # Read membership before rewriting so Cargo sees a consistent workspace. + members = version_inheriting_members(cargo_metadata(workspace_dir)) + if not members: + raise StampError("no workspace member inherits [workspace.package] version") + manifest = read_text(manifest_path) + previous = workspace_package_version(manifest) + # Compute both rewrites before writing either, so a failure cannot leave a + # stamped lockfile next to an unstamped manifest. + stamped_lockfile = stamp_lockfile(read_text(lockfile_path), members, version) + stamped_manifest = stamp_manifest(manifest, version) + write_text(lockfile_path, stamped_lockfile) + write_text(manifest_path, stamped_manifest) + print( + f"stamped codex-rs workspace version {previous} -> {version} " + f"({len(members)} members)" + ) + + +def verify(workspace_dir: Path, version: str) -> None: + manifest_version = workspace_package_version(read_text(workspace_dir / "Cargo.toml")) + if manifest_version != version: + raise StampError( + f"codex-rs/Cargo.toml workspace version is {manifest_version!r}, " + f"expected {version!r} from upstream-version.txt" + ) + metadata = cargo_metadata(workspace_dir) + members = set(metadata["workspace_members"]) + resolved = { + package["name"]: package["version"] + for package in metadata["packages"] + if package["id"] in members + } + locked = local_lockfile_versions(read_text(workspace_dir / "Cargo.lock")) + # Every inheriting member embeds CARGO_PKG_VERSION somewhere (analytics, + # otel, session metadata, ...); the guarded crates must be among them. + for name in sorted(set(GUARDED_PACKAGES) | set(version_inheriting_members(metadata))): + for source, actual in (("Cargo.toml", resolved.get(name)), ("Cargo.lock", locked.get(name))): + if actual != version: + raise StampError( + f"{name} has version {actual!r} in {source}, expected {version!r}; " + "the backend would receive the wrong client_version" + ) + print(f"verified codex-rs workspace version {version}") + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + subcommands = parser.add_subparsers(dest="command", required=True) + stamp_parser = subcommands.add_parser("stamp") + stamp_parser.add_argument( + "--expect-release-version", + help="downstream version that must be based on upstream-version.txt", + ) + check_parser = subcommands.add_parser("check-release-version") + check_parser.add_argument("release_version") + subcommands.add_parser("verify") + args = parser.parse_args(argv) + + try: + version = read_upstream_version() + if args.command == "stamp": + if args.expect_release_version: + check_release_version(args.expect_release_version, version) + stamp(WORKSPACE_DIR, version) + elif args.command == "check-release-version": + check_release_version(args.release_version, version) + print(f"release version {args.release_version} is based on {version}") + else: + verify(WORKSPACE_DIR, version) + except StampError as error: + print(f"::error::workspace version: {error}", file=sys.stderr) + return 1 + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/pwragent-release/test_stamp_workspace_version.py b/scripts/pwragent-release/test_stamp_workspace_version.py new file mode 100644 index 000000000000..92db153c9f82 --- /dev/null +++ b/scripts/pwragent-release/test_stamp_workspace_version.py @@ -0,0 +1,204 @@ +#!/usr/bin/env python3 + +import importlib.util +from pathlib import Path +import shutil +import tempfile +import unittest + + +SCRIPT = Path(__file__).with_name("stamp-workspace-version.py") +SPEC = importlib.util.spec_from_file_location("stamp_workspace_version", SCRIPT) +assert SPEC is not None and SPEC.loader is not None +STAMP = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(STAMP) + + +MANIFEST = """[workspace] +members = ["models-manager", "cli"] +resolver = "2" + +[workspace.package] +version = "0.0.0" +edition = "2024" + +[workspace.dependencies] +codex-models-manager = { path = "models-manager" } +serde = { version = "1" } +""" + +LOCKFILE = """# This file is automatically @generated by Cargo. +version = 4 + +[[package]] +name = "codex-cli" +version = "0.0.0" +dependencies = [ + "codex-models-manager", +] + +[[package]] +name = "codex-models-manager" +version = "0.0.0" + +[[package]] +name = "vendored-local" +version = "0.0.0" + +[[package]] +name = "serde" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +""" + +MEMBERS = ("codex-cli", "codex-models-manager") + + +class StampWorkspaceVersionTest(unittest.TestCase): + def test_manifest_stamps_only_the_workspace_package_version(self) -> None: + stamped = STAMP.stamp_manifest(MANIFEST, "0.171.0") + self.assertEqual(STAMP.workspace_package_version(stamped), "0.171.0") + self.assertIn('serde = { version = "1" }', stamped) + self.assertEqual(STAMP.stamp_manifest(stamped, "0.171.0"), stamped) + + def test_manifest_version_may_follow_arrays_and_comments(self) -> None: + manifest = MANIFEST.replace( + '[workspace.package]\nversion = "0.0.0"\n', + '[workspace.package]\n# see [docs]\nauthors = ["a"]\nversion = "0.0.0"\n', + ) + self.assertEqual(STAMP.workspace_package_version(manifest), "0.0.0") + stamped = STAMP.stamp_manifest(manifest, "0.171.0") + self.assertEqual(STAMP.workspace_package_version(stamped), "0.171.0") + self.assertIn('authors = ["a"]', stamped) + + def test_manifest_version_is_not_taken_from_a_later_table(self) -> None: + manifest = '[workspace.package]\nedition = "2024"\n\n[package]\nversion = "9.9.9"\n' + with self.assertRaises(STAMP.StampError): + STAMP.workspace_package_version(manifest) + + def test_manifest_without_workspace_package_version_is_rejected(self) -> None: + with self.assertRaisesRegex(STAMP.StampError, r"\[workspace\.package\]"): + STAMP.stamp_manifest('[workspace]\nmembers = []\n', "0.171.0") + + def test_lockfile_stamps_only_named_local_members(self) -> None: + stamped = STAMP.stamp_lockfile(LOCKFILE, MEMBERS, "0.171.0") + self.assertEqual( + STAMP.local_lockfile_versions(stamped), + { + "codex-cli": "0.171.0", + "codex-models-manager": "0.171.0", + "vendored-local": "0.0.0", + }, + ) + # The lockfile format version and registry packages are untouched. + self.assertIn("\nversion = 4\n", stamped) + self.assertIn('name = "serde"\nversion = "1.0.0"', stamped) + self.assertEqual(STAMP.stamp_lockfile(stamped, MEMBERS, "0.171.0"), stamped) + + def test_lockfile_preserves_crlf(self) -> None: + crlf = LOCKFILE.replace("\n", "\r\n") + stamped = STAMP.stamp_lockfile(crlf, MEMBERS, "0.171.0") + self.assertEqual(stamped, STAMP.stamp_lockfile(LOCKFILE, MEMBERS, "0.171.0").replace("\n", "\r\n")) + + def test_lockfile_missing_member_is_rejected(self) -> None: + with self.assertRaisesRegex(STAMP.StampError, "codex-missing"): + STAMP.stamp_lockfile(LOCKFILE, MEMBERS + ("codex-missing",), "0.171.0") + + def test_upstream_version_must_be_plain_non_placeholder_semver(self) -> None: + with tempfile.TemporaryDirectory() as temporary_directory: + path = Path(temporary_directory) / "upstream-version.txt" + path.write_text("0.171.0\n", encoding="utf-8") + self.assertEqual(STAMP.read_upstream_version(path), "0.171.0") + for invalid in ("0.0.0", "0.171.0-alpha.9", "0.171", ""): + path.write_text(f"{invalid}\n", encoding="utf-8") + with self.assertRaises(STAMP.StampError, msg=invalid): + STAMP.read_upstream_version(path) + + def test_release_version_must_be_based_on_upstream_version(self) -> None: + for release_version in ( + "0.171.0", + "0.171.0-pwragent.5", + "0.171.0-pwragent.dev.42", + ): + STAMP.check_release_version(release_version, "0.171.0") + for release_version in ("0.172.0-pwragent.1", "0.171.01-pwragent.1", "0.17.0"): + with self.assertRaises(STAMP.StampError, msg=release_version): + STAMP.check_release_version(release_version, "0.171.0") + + @unittest.skipUnless(shutil.which("cargo"), "cargo is not installed") + def test_stamp_then_verify_against_a_real_cargo_workspace(self) -> None: + with tempfile.TemporaryDirectory() as temporary_directory: + workspace = Path(temporary_directory) + (workspace / "Cargo.toml").write_text( + '[workspace]\nmembers = ["models-manager", "cli", "app-server",' + ' "code-mode-host", "pinned"]\nresolver = "2"\n\n' + '[workspace.package]\nversion = "0.0.0"\nedition = "2021"\n', + encoding="utf-8", + ) + lock = ["version = 4"] + crates = { + "models-manager": "codex-models-manager", + "cli": "codex-cli", + "app-server": "codex-app-server", + "code-mode-host": "codex-code-mode-host", + } + for directory, name in crates.items(): + self._crate(workspace, directory, name, "version.workspace = true") + lock.append(f'[[package]]\nname = "{name}"\nversion = "0.0.0"') + # A member with its own version must not be rewritten. + self._crate(workspace, "pinned", "pinned", 'version = "0.0.0"') + lock.append('[[package]]\nname = "pinned"\nversion = "0.0.0"') + (workspace / "Cargo.lock").write_text( + "\n\n".join(lock) + "\n", encoding="utf-8" + ) + + with self.assertRaisesRegex(STAMP.StampError, "0.0.0"): + STAMP.verify(workspace, "0.171.0") + STAMP.stamp(workspace, "0.171.0") + STAMP.verify(workspace, "0.171.0") + self.assertEqual( + STAMP.local_lockfile_versions( + (workspace / "Cargo.lock").read_text(encoding="utf-8") + )["pinned"], + "0.0.0", + ) + STAMP.cargo_metadata(workspace) # still loads after stamping + + # A stamped manifest with a stale lockfile still fails verify. + lockfile = workspace / "Cargo.lock" + lockfile.write_text( + lockfile.read_text(encoding="utf-8").replace( + 'name = "codex-models-manager"\nversion = "0.171.0"', + 'name = "codex-models-manager"\nversion = "0.0.0"', + ), + encoding="utf-8", + ) + with self.assertRaisesRegex(STAMP.StampError, "codex-models-manager.*Cargo.lock"): + STAMP.verify(workspace, "0.171.0") + + # Any other inheriting member left stale also fails verify. + STAMP.stamp(workspace, "0.171.0") + self._crate(workspace, "otel", "codex-otel", "version.workspace = true") + manifest = workspace / "Cargo.toml" + manifest.write_text( + manifest.read_text(encoding="utf-8").replace('"pinned"]', '"pinned", "otel"]'), + encoding="utf-8", + ) + with lockfile.open("a", encoding="utf-8") as handle: + handle.write('\n[[package]]\nname = "codex-otel"\nversion = "0.0.0"\n') + with self.assertRaisesRegex(STAMP.StampError, "codex-otel.*Cargo.lock"): + STAMP.verify(workspace, "0.171.0") + + @staticmethod + def _crate(workspace: Path, directory: str, name: str, version: str) -> None: + crate = workspace / directory + (crate / "src").mkdir(parents=True) + (crate / "src/lib.rs").write_text("", encoding="utf-8") + (crate / "Cargo.toml").write_text( + f'[package]\nname = "{name}"\n{version}\nedition = "2021"\n', + encoding="utf-8", + ) + + +if __name__ == "__main__": + unittest.main()