From 0e011c00928a16579b0bda47473cee986628cb51 Mon Sep 17 00:00:00 2001 From: colby-oai <228809017+colby-oai@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:01:51 +0000 Subject: [PATCH] Verify dependency hashes when running Python repository tools (#62346) ## Why Using `uv run --frozen` keeps dependency resolution fixed but does not require installed packages to match the hashes in `uv.lock`. ## What changed Run Ruff for the Python SDK and repository scripts, and regenerate Python SDK types during repository schema refreshes, through a shared runner. It exports the selected locked dependencies, installs them into a temporary virtual environment with `uv pip sync --require-hashes`, and runs the tool without resyncing the environment. ## Testing Update tests to check the formatter invocations and verify that only repository schema refreshes invoke Python type generation through the runner. GitOrigin-RevId: 4e84da5d227eb99c24efb9a1cc7647af82105536 --- .../scripts/write_schema_fixtures.py | 8 ++-- codex-rs/scripts/run_locked_python_tool.py | 47 +++++++++++++++++++ scripts/format.py | 15 ++++-- .../test_artifact_workflow_and_binaries.py | 10 ++-- sdk/python/tests/test_contract_generation.py | 4 +- 5 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 codex-rs/scripts/run_locked_python_tool.py diff --git a/codex-rs/app-server-protocol/scripts/write_schema_fixtures.py b/codex-rs/app-server-protocol/scripts/write_schema_fixtures.py index 65432a219e..bf354e1089 100644 --- a/codex-rs/app-server-protocol/scripts/write_schema_fixtures.py +++ b/codex-rs/app-server-protocol/scripts/write_schema_fixtures.py @@ -2,8 +2,9 @@ import argparse import os -from pathlib import Path import subprocess +import sys +from pathlib import Path def main() -> None: @@ -63,11 +64,10 @@ def main() -> None: sdk_root = workspace_root.parent / "sdk" / "python" subprocess.run( [ - "uv", - "run", + sys.executable, + str(workspace_root / "scripts" / "run_locked_python_tool.py"), "--project", str(sdk_root), - "--frozen", "--only-group", "test", "python", diff --git a/codex-rs/scripts/run_locked_python_tool.py b/codex-rs/scripts/run_locked_python_tool.py new file mode 100644 index 0000000000..93328b3285 --- /dev/null +++ b/codex-rs/scripts/run_locked_python_tool.py @@ -0,0 +1,47 @@ +"""Run a tool with dependencies verified against its uv.lock.""" + +import argparse +import os +import subprocess +from pathlib import Path +from tempfile import TemporaryDirectory + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--project", required=True) + parser.add_argument("--only-group") + parser.add_argument("tool", choices=("python", "ruff")) + parser.add_argument("tool_args", nargs=argparse.REMAINDER) + args = parser.parse_args() + + uv = ["uv", "--project", args.project] + group_args = ["--only-group", args.only_group] if args.only_group else [] + requirements = subprocess.check_output( + [*uv, "export", "--frozen", "--no-emit-project", *group_args] + ) + with TemporaryDirectory(prefix="codex-python-tool-") as temp: + venv = str(Path(temp) / ".venv") + subprocess.run([*uv, "venv", venv], check=True) + subprocess.run( + [*uv, "pip", "sync", "--require-hashes", "--python", venv, "-"], + input=requirements, + check=True, + ) + tool = args.tool + if tool == "ruff": + # Fail if the locked Ruff executable is missing from the environment. + tool = str( + Path(venv) / ("Scripts/ruff.exe" if os.name == "nt" else "bin/ruff") + ) + return subprocess.call( + [*uv, "run", "--no-sync", "--active", "--", tool, *args.tool_args], + env={**os.environ, "VIRTUAL_ENV": venv}, + ) + + +if __name__ == "__main__": + try: + raise SystemExit(main()) + except subprocess.CalledProcessError as error: + raise SystemExit(error.returncode) from None diff --git a/scripts/format.py b/scripts/format.py index cc1e96bee8..60053c679a 100644 --- a/scripts/format.py +++ b/scripts/format.py @@ -12,6 +12,7 @@ from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[1] +LOCKED_PYTHON_RUNNER = "codex-rs/scripts/run_locked_python_tool.py" @dataclass(frozen=True) @@ -116,9 +117,8 @@ def buildifier_formatter_group(*, check: bool) -> FormatterGroup: def python_sdk_formatter_group(*, check: bool) -> FormatterGroup: # Each `--project` retains its local dependency and Ruff configuration context. uv_run_args = [ - "uv", - "run", - "--frozen", + sys.executable, + LOCKED_PYTHON_RUNNER, "--project", "sdk/python", "--only-group", @@ -149,7 +149,14 @@ def python_sdk_formatter_group(*, check: bool) -> FormatterGroup: def python_scripts_formatter_group(*, check: bool) -> FormatterGroup: - args = ["uv", "run", "--frozen", "--project", "scripts", "ruff", "format"] + args = [ + sys.executable, + LOCKED_PYTHON_RUNNER, + "--project", + "scripts", + "ruff", + "format", + ] if check: args.append("--check") args.append(".") diff --git a/sdk/python/tests/test_artifact_workflow_and_binaries.py b/sdk/python/tests/test_artifact_workflow_and_binaries.py index c524d9e299..0eebb3b3db 100644 --- a/sdk/python/tests/test_artifact_workflow_and_binaries.py +++ b/sdk/python/tests/test_artifact_workflow_and_binaries.py @@ -190,18 +190,16 @@ def test_root_format_driver_covers_all_formatter_groups( len(group.commands) for group in formatters ] sdk_uv_run_args = ( - "uv", - "run", - "--frozen", + sys.executable, + "codex-rs/scripts/run_locked_python_tool.py", "--project", "sdk/python", "--only-group", "format", ) scripts_uv_run_args = ( - "uv", - "run", - "--frozen", + sys.executable, + "codex-rs/scripts/run_locked_python_tool.py", "--project", "scripts", ) diff --git a/sdk/python/tests/test_contract_generation.py b/sdk/python/tests/test_contract_generation.py index e53800fb6d..d87970f0c2 100644 --- a/sdk/python/tests/test_contract_generation.py +++ b/sdk/python/tests/test_contract_generation.py @@ -95,10 +95,12 @@ def test_schema_refresh_only_updates_python_for_repository_schemas(monkeypatch, runpy.run_path(str(script), run_name="__main__") assert [args[0] for args, _kwargs in calls] == ( - ["cargo", "uv"] if mode == "repository" else ["cargo"] + ["cargo", sys.executable] if mode == "repository" else ["cargo"] ) assert all(kwargs["check"] for _args, kwargs in calls) if mode == "repository": + runner = ROOT.parents[1] / "codex-rs/scripts/run_locked_python_tool.py" + assert calls[1][0][1] == str(runner) assert calls[1][0][-3:] == [ "generate-types", "--schema-dir",