Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

Reuse dependency caches across pull requests (#1461)

Ali Khokhar0365478f5d94
diff --git a/.github/workflows/dependency-cache.yml b/.github/workflows/dependency-cache.yml
new file mode 100644
index 00000000..08c1b882
--- /dev/null
+++ b/.github/workflows/dependency-cache.yml
@@ -0,0 +1,104 @@
+name: Dependency Cache
+
+env:
+ CI_UV_VERSION: "0.11.16"
+ UV_MALWARE_CHECK: "1"
+ UV_PREVIEW_FEATURES: "malware-check"
+ UV_CACHE_DIR: /tmp/.uv-cache
+ UV_PYTHON_INSTALL_DIR: /tmp/uv-python
+ UV_PYTHON_PREFERENCE: only-managed
+
+on:
+ push:
+ branches: [main]
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.ref }}
+ cancel-in-progress: true
+
+jobs:
+ warm-dependency-cache:
+ name: Warm dependency cache
+ runs-on: ubuntu-latest
+ timeout-minutes: 15
+ permissions:
+ contents: read
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
+ with:
+ fetch-depth: 1
+
+ - name: Read Python version
+ id: python
+ shell: bash
+ run: |
+ set -euo pipefail
+ version="$(tr -d '[:space:]' < .python-version)"
+ test -n "$version"
+ echo "version=$version" >> "$GITHUB_OUTPUT"
+
+ - name: Restore Python
+ id: python-cache
+ uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25
+ with:
+ path: ${{ env.UV_PYTHON_INSTALL_DIR }}
+ key: uv-python-v1-${{ runner.os }}-${{ runner.arch }}-py-${{ steps.python.outputs.version }}-uv-${{ env.CI_UV_VERSION }}
+
+ - name: Install uv
+ uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9
+ with:
+ version: ${{ env.CI_UV_VERSION }}
+ enable-cache: false
+
+ - name: Install Python
+ if: steps.python-cache.outputs.cache-hit != 'true'
+ run: uv python install "${{ steps.python.outputs.version }}"
+
+ - name: Save Python
+ if: steps.python-cache.outputs.cache-hit != 'true'
+ uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25
+ with:
+ path: ${{ env.UV_PYTHON_INSTALL_DIR }}
+ key: ${{ steps.python-cache.outputs.cache-primary-key }}
+
+ - name: Fingerprint dependencies
+ id: dependencies
+ shell: bash
+ run: |
+ set -euo pipefail
+ uv export \
+ --locked \
+ --no-emit-workspace \
+ --no-header \
+ --no-annotate \
+ --output-file "$RUNNER_TEMP/uv-ci-dependencies.txt"
+ echo "hash=$(sha256sum "$RUNNER_TEMP/uv-ci-dependencies.txt" | cut -d ' ' -f 1)" >> "$GITHUB_OUTPUT"
+
+ - name: Check for exact uv cache
+ id: exact-cache
+ uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25
+ with:
+ path: ${{ env.UV_CACHE_DIR }}
+ key: uv-deps-v1-${{ runner.os }}-${{ runner.arch }}-py-${{ steps.python.outputs.version }}-uv-${{ env.CI_UV_VERSION }}-${{ steps.dependencies.outputs.hash }}
+ lookup-only: true
+
+ - name: Restore compatible uv cache
+ if: steps.exact-cache.outputs.cache-hit != 'true'
+ id: uv-cache
+ uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25
+ with:
+ path: ${{ env.UV_CACHE_DIR }}
+ key: uv-deps-v1-${{ runner.os }}-${{ runner.arch }}-py-${{ steps.python.outputs.version }}-uv-${{ env.CI_UV_VERSION }}-${{ steps.dependencies.outputs.hash }}
+ restore-keys: |
+ uv-deps-v1-${{ runner.os }}-${{ runner.arch }}-py-${{ steps.python.outputs.version }}-uv-${{ env.CI_UV_VERSION }}-
+
+ - name: Warm uv cache
+ if: steps.exact-cache.outputs.cache-hit != 'true'
+ run: uv sync --locked --no-install-workspace
+
+ - name: Save uv cache
+ if: steps.exact-cache.outputs.cache-hit != 'true'
+ uses: actions/cache/save@caa296126883cff596d87d8935842f9db880ef25
+ with:
+ path: ${{ env.UV_CACHE_DIR }}
+ key: ${{ steps.uv-cache.outputs.cache-primary-key }}
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 802648e5..fac6a6a4 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -6,8 +6,12 @@
name: CI
env:
+ CI_UV_VERSION: "0.11.16"
UV_MALWARE_CHECK: "1"
UV_PREVIEW_FEATURES: "malware-check"
+ UV_CACHE_DIR: /tmp/.uv-cache
+ UV_PYTHON_INSTALL_DIR: /tmp/uv-python
+ UV_PYTHON_PREFERENCE: only-managed
on:
pull_request:
@@ -68,13 +72,52 @@ jobs:
repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
fetch-depth: 1
+ - name: Read Python version
+ id: python
+ shell: bash
+ run: |
+ set -euo pipefail
+ version="$(tr -d '[:space:]' < .python-version)"
+ test -n "$version"
+ echo "version=$version" >> "$GITHUB_OUTPUT"
+
+ - name: Restore Python
+ id: python-cache
+ uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25
+ with:
+ path: ${{ env.UV_PYTHON_INSTALL_DIR }}
+ key: uv-python-v1-${{ runner.os }}-${{ runner.arch }}-py-${{ steps.python.outputs.version }}-uv-${{ env.CI_UV_VERSION }}
+
- name: Install uv
uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9
with:
- version: "0.11.16"
- enable-cache: true
- cache-python: true
- save-cache: ${{ matrix.id == 'ruff-format' }}
+ version: ${{ env.CI_UV_VERSION }}
+ enable-cache: false
+
+ - name: Install Python
+ if: steps.python-cache.outputs.cache-hit != 'true'
+ run: uv python install "${{ steps.python.outputs.version }}"
+
+ - name: Fingerprint dependencies
+ id: dependencies
+ shell: bash
+ run: |
+ set -euo pipefail
+ uv export \
+ --locked \
+ --no-emit-workspace \
+ --no-header \
+ --no-annotate \
+ --output-file "$RUNNER_TEMP/uv-ci-dependencies.txt"
+ echo "hash=$(sha256sum "$RUNNER_TEMP/uv-ci-dependencies.txt" | cut -d ' ' -f 1)" >> "$GITHUB_OUTPUT"
+
+ - name: Restore uv cache
+ uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25
+ with:
+ path: ${{ env.UV_CACHE_DIR }}
+ key: uv-deps-v1-${{ runner.os }}-${{ runner.arch }}-py-${{ steps.python.outputs.version }}-uv-${{ env.CI_UV_VERSION }}-${{ steps.dependencies.outputs.hash }}
+ restore-keys: |
+ uv-deps-v1-${{ runner.os }}-${{ runner.arch }}-py-${{ steps.python.outputs.version }}-uv-${{ env.CI_UV_VERSION }}-
- name: Install Chromium
if: matrix.id == 'playwright'
diff --git a/AGENTS.md b/AGENTS.md
index 3bd077ab..9e7fae7b 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -20,6 +20,7 @@
- All 6 check IDs are represented in `scripts/ci.sh` / `scripts/ci.ps1` and enforced by `tests.yml` before each merge (parallel jobs: suppression grep, ruff-format, ruff-check, ty, pytest, playwright).
- Deterministic rendered Admin UI interactions live under `e2e/` and run separately from ordinary pytest. Install Chromium once with `uv run playwright install chromium`; API and unit contracts remain under `tests/`.
- GitHub CI runs for every pull request, including stacked PRs targeting non-`main` branches. Head updates trigger fresh checks; strict required checks keep PRs targeting `main` current with `main`, so the tested PR tree is the tree squash-merged without a duplicate post-merge run.
+- A separate trusted `Dependency Cache` workflow runs after pushes to `main` only and is the sole writer for reusable Python and uv dependency caches. Pull-request jobs only restore caches; the maintenance workflow never reruns validation and is not a required status check.
- Repository protection should use rulesets: a non-bypassable main integrity ruleset requires pull requests and strict required checks, keeps branches current, and blocks direct/force pushes to `main`; a separate review ruleset may allow `Alishahryar1`/admins to bypass review only.
- Required status checks: set **required status checks** to **all** of those statuses (e.g. **Ban suppressions and legacy annotations**, **ruff-format**, **ruff-check**, **ty**, **pytest**, **playwright**—use the exact labels GitHub shows, which may be prefixed with **CI /**). Remove **ci** from required checks if it was previously added for the old gate job.
diff --git a/CLAUDE.md b/CLAUDE.md
index 3bd077ab..9e7fae7b 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -20,6 +20,7 @@
- All 6 check IDs are represented in `scripts/ci.sh` / `scripts/ci.ps1` and enforced by `tests.yml` before each merge (parallel jobs: suppression grep, ruff-format, ruff-check, ty, pytest, playwright).
- Deterministic rendered Admin UI interactions live under `e2e/` and run separately from ordinary pytest. Install Chromium once with `uv run playwright install chromium`; API and unit contracts remain under `tests/`.
- GitHub CI runs for every pull request, including stacked PRs targeting non-`main` branches. Head updates trigger fresh checks; strict required checks keep PRs targeting `main` current with `main`, so the tested PR tree is the tree squash-merged without a duplicate post-merge run.
+- A separate trusted `Dependency Cache` workflow runs after pushes to `main` only and is the sole writer for reusable Python and uv dependency caches. Pull-request jobs only restore caches; the maintenance workflow never reruns validation and is not a required status check.
- Repository protection should use rulesets: a non-bypassable main integrity ruleset requires pull requests and strict required checks, keeps branches current, and blocks direct/force pushes to `main`; a separate review ruleset may allow `Alishahryar1`/admins to bypass review only.
- Required status checks: set **required status checks** to **all** of those statuses (e.g. **Ban suppressions and legacy annotations**, **ruff-format**, **ruff-check**, **ty**, **pytest**, **playwright**—use the exact labels GitHub shows, which may be prefixed with **CI /**). Remove **ci** from required checks if it was previously added for the old gate job.
diff --git a/tests/contracts/test_uv_policy.py b/tests/contracts/test_uv_policy.py
index 478abf37..78d47c9a 100644
--- a/tests/contracts/test_uv_policy.py
+++ b/tests/contracts/test_uv_policy.py
@@ -2,25 +2,50 @@ import tomllib
from pathlib import Path
UV_MINIMUM = "0.11.16"
+UV_WORKFLOWS = (
+ Path(".github/workflows/tests.yml"),
+ Path(".github/workflows/dependency-cache.yml"),
+)
def test_supported_uv_minimum_is_consistent() -> None:
pyproject = tomllib.loads(Path("pyproject.toml").read_text(encoding="utf-8"))
install_sh = Path("scripts/install.sh").read_text(encoding="utf-8")
install_ps1 = Path("scripts/install.ps1").read_text(encoding="utf-8")
- workflow = Path(".github/workflows/tests.yml").read_text(encoding="utf-8")
assert pyproject["tool"]["uv"]["required-version"] == f">={UV_MINIMUM}"
assert f'MIN_UV_VERSION="{UV_MINIMUM}"' in install_sh
assert f'$MinUvVersion = "{UV_MINIMUM}"' in install_ps1
- assert f'version: "{UV_MINIMUM}"' in workflow
+ for workflow_path in UV_WORKFLOWS:
+ workflow = workflow_path.read_text(encoding="utf-8")
+ assert f'CI_UV_VERSION: "{UV_MINIMUM}"' in workflow
+ assert "version: ${{ env.CI_UV_VERSION }}" in workflow
-def test_every_ci_job_inherits_uv_malware_check() -> None:
- workflow = Path(".github/workflows/tests.yml").read_text(encoding="utf-8")
- workflow_env = (
- 'env:\n UV_MALWARE_CHECK: "1"\n UV_PREVIEW_FEATURES: "malware-check"\n'
+def test_every_uv_workflow_inherits_malware_check() -> None:
+ malware_policy = ' UV_MALWARE_CHECK: "1"\n UV_PREVIEW_FEATURES: "malware-check"\n'
+
+ for workflow_path in UV_WORKFLOWS:
+ workflow = workflow_path.read_text(encoding="utf-8")
+ assert workflow.count(malware_policy) == 1
+ assert workflow.index(malware_policy) < workflow.index("jobs:\n")
+
+
+def test_only_trusted_main_workflow_writes_caches() -> None:
+ pull_request_workflow = UV_WORKFLOWS[0].read_text(encoding="utf-8")
+ main_workflow = UV_WORKFLOWS[1].read_text(encoding="utf-8")
+
+ assert "actions/cache/save@" not in pull_request_workflow
+ assert main_workflow.count("actions/cache/save@") == 2
+
+
+def test_uv_workflows_share_managed_python_cache_policy() -> None:
+ python_policy = (
+ " UV_PYTHON_INSTALL_DIR: /tmp/uv-python\n"
+ " UV_PYTHON_PREFERENCE: only-managed\n"
)
- assert workflow.count(workflow_env) == 1
- assert workflow.index(workflow_env) < workflow.index("jobs:\n")
+ for workflow_path in UV_WORKFLOWS:
+ workflow = workflow_path.read_text(encoding="utf-8")
+ assert workflow.count(python_policy) == 1
+ assert "actions/setup-python@" not in workflow