--- type: types/instruction.md name: dependency-locks description: How the Python packages of the stack are pinned and moved - the four lock files under tools/, regenerating them with the 7-day cooldown, lifting one package at once for a security fix without ever lowering it again, checking that no locked package was too young, and what a red nightly audit or fresh-resolution run asks for. --- # Pin the stack's packages, and move them on purpose Every Python package the stack or its CI installs comes from a lock under `tools/`: exact versions with SHA-256 hashes, transitive packages included, installed with `pip install --require-hashes`. Before Gitea #245 the requirement files carried lower bounds only, CI installed whatever was newest that morning, and an instance kept the packages of the day it was set up - CI went red after four quiet weeks when pytest 9.2 and typer 0.29.1 came out (#243), and a compromised release would have reached the next install unchecked. Pins answer both sides of faster attacks: nothing new flows in unchecked, and a known hole is closed by a deliberate lift, at once. ## Contents - [The files](#the-files) - [When to run](#when-to-run) - [Steps](#steps) - [Decision points](#decision-points) - [Scope](#scope) ## The files | Source (lower bounds) | Lock | Installed by | Ships | |---|---|---|---| | `tools/requirements.txt` | `tools/requirements.lock` | the preflight, every run whose stamp differs | yes | | `tools/requirements-mcp.txt` | `tools/requirements-mcp.lock` | the user, by hand (command in the source's head); CI | yes | | `tools/requirements-browser.txt` | `tools/requirements-browser.lock` | the preflight, `--with browser` and after | yes | | `tools/requirements-dev.txt` | `tools/requirements-dev.lock` | CI and a developer: pytest, pytest-cov, radicale, pip-audit, uv | no (`TOOLS_DEV_ONLY`) | `tools/lock_requirements.py` (dev-only) is the one way a lock is written. It resolves the four in that order, each constrained by every lock above it (`-c`), because all of them land in one venv: a package in two locks has one version, and a later `pip install -r` never moves a package an earlier lock placed. `test_dependency_locks.py` holds every property below on each push. The locks resolve for the Python minimum in `tools/prerequisites.txt` with `--universal`, so one file serves Linux, macOS and Windows across the admitted span. A source file is never installed itself - not by the preflight, not by CI (the same test reads every workflow for it). ## When to run - **A regular lift** - a new package version is wanted, a source file's floor moved, or a lock is simply due. It is a small stack package of its own: the locks, a patch bump, `publish`. - **A security lift** - the nightly's `pip-audit` step is red, or an advisory names a locked version. Same day, one package, no cooldown. - **The nightly's fresh-resolution step is red** - today's regular lift would break the suite. Not a lift yet: the defect it shows is fixed first, in a package of its own, and the lift follows with it. ## Steps 1. **Have `uv` on `PATH`.** `requirements-dev.lock` carries it; in a checkout with the test tools installed, `tools/.venv/bin/uv` is it. 2. **Regular lift - regenerate every lock:** ```bash PATH="$PWD/tools/.venv/bin:$PATH" python3 tools/lock_requirements.py ``` It sets `--exclude-newer` to now minus 7 days: a file uploaded later is not considered. A compromised release is usually found and pulled within hours to days; the cooldown lets that happen before a lock can take one. Exceptions from an earlier security lift are carried over while still younger than the cutoff and dropped once it passes them (step 3). 3. **Security lift - one package, at once.** `pip-audit` names the package and the first fixed version X: - Raise the floor in its source file to `>=X`. It stays there for good - it is simply true that vulnerable versions are no longer taken, and it is what keeps every later lift from lowering the package again. - Regenerate with an exception for that package alone, up to X's upload time plus a minute (PyPI's JSON API shows it as `upload_time_iso_8601` per file): ```bash PATH="$PWD/tools/.venv/bin:$PATH" python3 tools/lock_requirements.py \ --exception = ``` uv writes the exception into each lock's head beside the cutoff. **The invariant: no later lift lowers the package below X.** While X is younger than the cooldown, the script carries the exception into the next regular lift; once the cutoff has passed X, the exception is no longer needed and drops out, and the floor stays. Without both, a lift would fail loudly - "No solution ... filtered by `exclude-newer`" - rather than lower the package silently: that is the floor doing its job, not a reason to remove it. 4. **Check the result.** ```bash cd tools && .venv/bin/python -m pytest -q chemenu/tests/test_dependency_locks.py ``` It holds: an exact version and at least one SHA-256 per package; every source requirement pinned at or above its floor; one version per package across the four locks; each lock constrained by the ones before it; and **no package younger than 7 days** - the lock's `--exclude-newer` lies at least 7 days before the lock's last commit (before now, while the file differs from `HEAD`). A security lift's package is the one exception, visible as `--exclude-newer-package` in the head. Read the diff as well: a package that moved a major version, or one new in a lock, is worth a line in the changelog entry. 5. **Install and run the suite against the new locks** - the preflight installs `requirements.lock` by itself once its hash changed; the other two by hand: ```bash tools/preflight.sh tools/.venv/bin/python -m pip install --require-hashes -r tools/requirements-mcp.lock tools/.venv/bin/python -m pip install --require-hashes -r tools/requirements-dev.lock cd tools && .venv/bin/python -m pytest -q ``` 6. **Bump and publish.** A lock under `tools/` reaches every instance - its next preflight moves the venv to it - so it is a patch bump ([version-parts.md](version-parts.md)) with a changelog entry naming the lift, then [publish-and-ci.md](publish-and-ci.md). A lift that touches `requirements.lock` also starts the `Converters` workflow. ## Decision points - **The nightly's `pip-audit` step is red, but no fixed version exists yet?** Nothing to lift. Name the advisory and whether the stack reaches the vulnerable code in the topic issue, and check again when a fix is released. - **A regular lift fails with "No solution found ... filtered by `exclude-newer`"?** A floor in a source file names a version younger than the cutoff - usually a security lift whose exception was lost. Restore the exception from the lock's head in `git log -p`; never lower the floor. - **A package wants a Python outside the span?** The resolution for the minimum fails or picks an old version. The span is a design decision in `tools/prerequisites.txt`, not something a lift moves; take it to `stack-dev`. - **A new dependency?** Add it to the source file that matches who needs it, with a floor, then regenerate. A package only the origin repository's tests need goes into `requirements-dev.txt`, never into a lock that ships. ## Scope The origin repository only - `dist export` ships the three runtime locks, not this file, the script or the dev lock. The span of Python versions itself is `tools/prerequisites.txt` and [instructions/preflight.md](../preflight.md); reasons for the design are Gitea #245.