From 83d6081b85302eeac484b783ef8ea0c0c00bf5f9 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 23 Nov 2026 11:03:49 -0800 Subject: [PATCH 1/3] fix(local): an image Docker holds by its digest is used as pinned, not pulled or built again A run pinning an image to a digest asked only whether this machine held something under the image's name. An image pulled as `docker pull @` carries no tag, so it looked absent: the run pulled again and, on a machine not logged in to the registry, built the image from its build context in its place, or failed when its Dockerfile's base was private too. And a gateway deploy on the local provider took whatever the name held as the pinned image, even another one. `named_if_held` decides for both the local provider's pull and the local sandbox's published images: held when the name holds the digest, or a build here of the image's sources, or when Docker holds the digest alone, which it then names with the image's name, over whatever that name held. Docker resolves the pinned reference on both its classic and containerd image stores. Fixes FD-6895. Co-Authored-By: Claude Opus 5.5 --- .../sandbox_providers/host_images.py | 26 +++++++++++++--- .../local_docker/provider.py | 10 ++++--- .../sandbox_providers/host_images_test.py | 30 +++++++++++++++++++ .../sandbox_providers/local_sandbox_test.py | 5 ++-- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/agent_env/providers/sandbox_providers/host_images.py b/src/agent_env/providers/sandbox_providers/host_images.py index 55b4019fc..ca21aa92d 100644 --- a/src/agent_env/providers/sandbox_providers/host_images.py +++ b/src/agent_env/providers/sandbox_providers/host_images.py @@ -4,7 +4,8 @@ under the image's name, labeled with the sources it was built from, and built again only when those change, so a later deploy, a parallel one or another run finds it. A VM on this machine starts with no build cache, so it loads a saved copy of that build, kept once per sources. Neither is a store version: the image is its build context. An image a -registry publishes is pulled here by its digest, and built from its build context only when it can't be. +registry publishes is pulled here by its digest, unless Docker holds it by that digest already, and built from its build +context only when it can't be. """ from __future__ import annotations @@ -56,6 +57,18 @@ def holds_digest(image: DockerImageArtifact) -> bool: return image.image_digest in _repo_digests(image.image_name) or held(image) +def named_if_held(image: DockerImageArtifact) -> bool: + """Whether this machine's Docker holds ``image``, one with an ``image_digest``, as that digest names it, under its + name (``holds_digest``) or by the digest alone, as ``docker pull @`` leaves an image, which it then + names with ``image_name`` over whatever that name held, so a container started by the name runs it.""" + if holds_digest(image): + return True + if image.image_digest not in _repo_digests(image.pinned_name): + return False + _tag(image) + return True + + def present(image_name: str) -> bool: """Whether this machine's Docker holds an image named ``image_name``.""" try: @@ -146,14 +159,14 @@ def pull_lock(image_name: str) -> str: def pull(image: DockerImageArtifact) -> None: """Have this machine's Docker hold ``image``, one with an ``image_digest``, under its name, unless it already holds - it (``holds_digest``): pulled by its digest, or, when that fails, built from its build context. Raises RuntimeError + it (``named_if_held``): pulled by its digest, or, when that fails, built from its build context. Raises RuntimeError when it can't be pulled or built.""" with holding_locks([pull_lock(image.pinned_name)]): - if holds_digest(image): + if named_if_held(image): return pulled = subprocess.run(["docker", "pull", image.pinned_name], capture_output=True, text=True, errors="replace") if pulled.returncode == 0: - subprocess.run(["docker", "tag", image.pinned_name, image.image_name], capture_output=True, check=True) + _tag(image) return why = (pulled.stderr.strip().splitlines() or [f"docker pull exited {pulled.returncode}"])[-1] if not image.buildable: @@ -162,6 +175,11 @@ def pull(image: DockerImageArtifact) -> None: build(image) +def _tag(image: DockerImageArtifact) -> None: + """Name the image Docker holds by ``image``'s digest with ``image``'s name, where a deploy finds it.""" + subprocess.run(["docker", "tag", image.pinned_name, image.image_name], capture_output=True, check=True) + + async def pull_here(image: DockerImageArtifact) -> None: """``pull`` off the event loop.""" await asyncio.to_thread(pull, image) diff --git a/src/agent_env/providers/sandbox_providers/local_docker/provider.py b/src/agent_env/providers/sandbox_providers/local_docker/provider.py index 38bf79149..1ea86103a 100644 --- a/src/agent_env/providers/sandbox_providers/local_docker/provider.py +++ b/src/agent_env/providers/sandbox_providers/local_docker/provider.py @@ -42,7 +42,8 @@ from agent_env import config from agent_env.attribution import Attribution from agent_env.config.paths import state_root -from agent_env.providers.sandbox_providers.host_images import build_here, built_here, host_platform, present, pull_here +from agent_env.providers.sandbox_providers.host_images import (build_here, built_here, host_platform, named_if_held, + present, pull_here) from agent_env.providers.sandbox_providers.disk_launcher import RESET, UNRESETTABLE, forward, reset_input, unresettable from agent_env.providers.sandbox_providers.local_docker.cli import DockerFailed, docker from agent_env.providers.sandbox_providers.local_docker.disk import KVM, kvm_platforms, launcher_args, machine_for @@ -275,9 +276,10 @@ def _held(self, image_name: str) -> bool: and present(image_name)) async def pull_published(self, artifacts: list) -> list: - """Pull each published image but one this machine's Docker already holds under its name, so a run after the - first needs no registry.""" - held = await asyncio.gather(*(asyncio.to_thread(present, artifact.image_name) for artifact in artifacts)) + """Pull each published image but one this machine's Docker already holds as its digest names it + (``named_if_held``), so a run after the first needs no registry; one whose name holds another image is pulled + over it.""" + held = await asyncio.gather(*(asyncio.to_thread(named_if_held, artifact) for artifact in artifacts)) return await super().pull_published([artifact for artifact, here in zip(artifacts, held) if not here]) def host_port(self, port: int) -> int: diff --git a/tst/unit/providers/sandbox_providers/host_images_test.py b/tst/unit/providers/sandbox_providers/host_images_test.py index 77d3dd075..d6daf73b4 100644 --- a/tst/unit/providers/sandbox_providers/host_images_test.py +++ b/tst/unit/providers/sandbox_providers/host_images_test.py @@ -249,6 +249,36 @@ def test_a_published_image_docker_holds_by_its_digest_isnt_pulled_again(local_st assert ran == [] and docker[0] == [] +@pytest.mark.parametrize("pulled_by, held, tagged", [ + ({_PUBLISHED_REF: {_PUBLISHED_DIGEST}}, True, False), + ({_PINNED: {_PUBLISHED_DIGEST}}, True, True), + ({_PINNED: {_PUBLISHED_DIGEST}, _PUBLISHED_REF: {"sha256:" + "d" * 64}}, True, True), + ({_PUBLISHED_REF: {"sha256:" + "d" * 64}}, False, False), + ({}, False, False), +], ids=["under-its-name", "by-its-digest-alone", "by-its-digest-its-name-on-another", "its-name-on-another", "absent"]) +def test_an_image_is_held_as_its_digest_names_it_and_named_when_held_by_the_digest_alone(local_stores, context, docker, + monkeypatch, pulled_by, held, + tagged): + ran = _docker_cli(monkeypatch, pulled_by=pulled_by) + + assert host_images.named_if_held(_published(context)) is held + assert ran == ([["tag", _PINNED, _PUBLISHED_REF]] if tagged else []) + + +@pytest.mark.parametrize("named", [False, True], ids=["untagged", "tag-names-another-image"]) +def test_a_published_image_docker_holds_by_its_digest_alone_is_named_not_pulled_or_built(local_stores, context, docker, + monkeypatch, named): + """``docker pull @`` leaves the image with no tag: it's still the one the digest names, so a registry + this machine can't log in to isn't asked for it, nor is it built in its place.""" + pulled_by = {_PINNED: {_PUBLISHED_DIGEST}} | ({_PUBLISHED_REF: {"sha256:" + "d" * 64}} if named else {}) + ran = _docker_cli(monkeypatch, pulled_by=pulled_by, pull_fails=True) + + host_images.pull(_published(context)) + + assert ran == [["tag", _PINNED, _PUBLISHED_REF]] + assert docker[0] == [] + + def test_a_published_image_built_here_from_its_sources_isnt_pulled(local_stores, context, docker, monkeypatch): ran = _docker_cli(monkeypatch) image = _published(context) diff --git a/tst/unit/providers/sandbox_providers/local_sandbox_test.py b/tst/unit/providers/sandbox_providers/local_sandbox_test.py index 34cf4a94d..194bf4998 100644 --- a/tst/unit/providers/sandbox_providers/local_sandbox_test.py +++ b/tst/unit/providers/sandbox_providers/local_sandbox_test.py @@ -1437,10 +1437,11 @@ def fail(*args): @pytest.mark.asyncio async def test_the_local_provider_pulls_only_the_published_images_its_docker_doesnt_hold(monkeypatch, tmp_path): - """So a run after the first needs no registry.""" + """So a run after the first needs no registry; held means as the image's digest names it, not under its name.""" images = [DockerImageArtifact(id=name, description="d", image_name=f"ghcr.io/team/{name}:src-1", image_digest="sha256:" + "9" * 64) for name in ("held", "new")] - monkeypatch.setattr(ls, "present", lambda image_name: image_name.startswith("ghcr.io/team/held:")) + monkeypatch.setattr(ls, "named_if_held", lambda image: image.id == "held") + monkeypatch.setattr(ls, "present", lambda image_name: True) asked = [] async def pull_published(self, artifacts): From cafb28fc672a817dce95c25b7b5486aead32b7e9 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 23 Nov 2026 11:58:28 -0800 Subject: [PATCH 2/3] fix(local): say so when a pinned image's name is moved off another image A container started by the name runs something else than before, which is worth a line in the log. Co-Authored-By: Claude Opus 5.5 --- .../providers/sandbox_providers/host_images.py | 3 +++ .../providers/sandbox_providers/host_images_test.py | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/agent_env/providers/sandbox_providers/host_images.py b/src/agent_env/providers/sandbox_providers/host_images.py index ca21aa92d..7bac15252 100644 --- a/src/agent_env/providers/sandbox_providers/host_images.py +++ b/src/agent_env/providers/sandbox_providers/host_images.py @@ -65,6 +65,9 @@ def named_if_held(image: DockerImageArtifact) -> bool: return True if image.image_digest not in _repo_digests(image.pinned_name): return False + if present(image.image_name): + logger.warning("%s named another image than the one it's pinned to, %s; it now names the pinned one", + image.image_name, image.image_digest) _tag(image) return True diff --git a/tst/unit/providers/sandbox_providers/host_images_test.py b/tst/unit/providers/sandbox_providers/host_images_test.py index d6daf73b4..6d44b87f4 100644 --- a/tst/unit/providers/sandbox_providers/host_images_test.py +++ b/tst/unit/providers/sandbox_providers/host_images_test.py @@ -227,6 +227,7 @@ def run(argv, **kwargs): return subprocess.CompletedProcess(argv, int(failed), "", "Error response from daemon: denied" if failed else "") monkeypatch.setattr(host_images, "_repo_digests", lambda name: (pulled_by or {}).get(name, set())) + monkeypatch.setattr(host_images, "present", lambda name: name in (pulled_by or {})) monkeypatch.setattr(host_images.subprocess, "run", run) return ran @@ -257,12 +258,17 @@ def test_a_published_image_docker_holds_by_its_digest_isnt_pulled_again(local_st ({}, False, False), ], ids=["under-its-name", "by-its-digest-alone", "by-its-digest-its-name-on-another", "its-name-on-another", "absent"]) def test_an_image_is_held_as_its_digest_names_it_and_named_when_held_by_the_digest_alone(local_stores, context, docker, - monkeypatch, pulled_by, held, - tagged): + monkeypatch, caplog, pulled_by, + held, tagged): + """A name moved off another image says so: a container started by it will run something else than before.""" ran = _docker_cli(monkeypatch, pulled_by=pulled_by) - assert host_images.named_if_held(_published(context)) is held + with caplog.at_level(logging.WARNING): + assert host_images.named_if_held(_published(context)) is held + assert ran == ([["tag", _PINNED, _PUBLISHED_REF]] if tagged else []) + moved = f"{_PUBLISHED_REF} named another image than the one it's pinned to, {_PUBLISHED_DIGEST}" + assert (moved in caplog.text) is (tagged and _PUBLISHED_REF in pulled_by) @pytest.mark.parametrize("named", [False, True], ids=["untagged", "tag-names-another-image"]) From 3f498fb9b80a17f694acd8c0dcb7105cb0e9c7c6 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 23 Nov 2026 12:01:05 -0800 Subject: [PATCH 3/3] refactor(local): leave named_if_held the one public check of a pinned image held here holds_digest misses an image held by its digest alone, the check this pull request replaced in pull_published, so as a public name it invited the same mistake again. It's now _holds_under_name, a step of named_if_held. Co-Authored-By: Claude Opus 5.5 --- .../sandbox_providers/host_images.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/agent_env/providers/sandbox_providers/host_images.py b/src/agent_env/providers/sandbox_providers/host_images.py index 7bac15252..180fee327 100644 --- a/src/agent_env/providers/sandbox_providers/host_images.py +++ b/src/agent_env/providers/sandbox_providers/host_images.py @@ -50,18 +50,11 @@ def holds(image_name: str, image_sources: str | None) -> bool: return image_sources is not None and _built_from(image_name) == image_sources -def holds_digest(image: DockerImageArtifact) -> bool: - """Whether this machine's Docker holds ``image``, one with an ``image_digest``, under its name as the image that - digest names: pulled by it, or built here from the image's sources. A tag can name another image than the one a - version records, so holding its name isn't enough.""" - return image.image_digest in _repo_digests(image.image_name) or held(image) - - def named_if_held(image: DockerImageArtifact) -> bool: """Whether this machine's Docker holds ``image``, one with an ``image_digest``, as that digest names it, under its - name (``holds_digest``) or by the digest alone, as ``docker pull @`` leaves an image, which it then - names with ``image_name`` over whatever that name held, so a container started by the name runs it.""" - if holds_digest(image): + name (``_holds_under_name``) or by the digest alone, as ``docker pull @`` leaves an image, which it + then names with ``image_name`` over whatever that name held, so a container started by the name runs it.""" + if _holds_under_name(image): return True if image.image_digest not in _repo_digests(image.pinned_name): return False @@ -178,6 +171,13 @@ def pull(image: DockerImageArtifact) -> None: build(image) +def _holds_under_name(image: DockerImageArtifact) -> bool: + """Whether this machine's Docker holds ``image``, one with an ``image_digest``, under its name as the image that + digest names: pulled by it, or built here from the image's sources. A tag can name another image than the one a + version records, so holding its name isn't enough, and an image held by the digest alone isn't under its name.""" + return image.image_digest in _repo_digests(image.image_name) or held(image) + + def _tag(image: DockerImageArtifact) -> None: """Name the image Docker holds by ``image``'s digest with ``image``'s name, where a deploy finds it.""" subprocess.run(["docker", "tag", image.pinned_name, image.image_name], capture_output=True, check=True)