diff --git a/AGENTS.md b/AGENTS.md index bc7731236..41a8dc78b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -280,7 +280,11 @@ Out-of-tree code plugs in either through `config.toml`, as a `module:Class` poin store (`store.routing.shared_store`), such as the hub's, may hold one, as the hub holds `chromium` for the tasks that name it, which `bundle register` writes. That env shadows the alias wherever that store is read, routing on or off (`Env.get` and the planner read the store first), so a deployment recorded under the alias there before it was written - reattaches to it from then on, or isn't found when it lacks that deployment's version. + reattaches to it from then on, or isn't found when it lacks that deployment's version. An installed bundle's entity + the planner pulls in is the exception: its own references to aliases bind what its installed bundles give them, + pulled in too, whatever the store holds of the alias's id (`_Planner._installed_alias`), so registering the installed + chromium writes it over the installed `page` and `ubuntu-24.04-browser` even where the store holds older ones. Such a + reference isn't one of `Plan.alias_uses`, which are the aliases the bundle itself names. - `aliases.toml` gives artifacts aliases too (`[artifacts] "" = ["", ...]`; the `computers` bundle names its base `ubuntu-24.04`), resolved by the planner wherever env aliases are, as a computer env's `computer` names its base. `Artifact.get` reads no alias: the planner rewrites every alias an entry or a task names to its id. An alias may diff --git a/src/agent_env/bundle/plan.py b/src/agent_env/bundle/plan.py index 990477072..5c496df98 100644 --- a/src/agent_env/bundle/plan.py +++ b/src/agent_env/bundle/plan.py @@ -355,17 +355,35 @@ def _resolve_aliases(self, *, runs: bool = True) -> None: def _pointed(self, entry: ResolvedEntry) -> ResolvedEntry: """``entry`` with each store reference naming an installed alias pointed at what the alias names, pulled in, and - each field naming the alias naming its id; none is pointed until ``_resolve_aliases`` reads the aliases.""" + each field naming the alias naming its id; none is pointed until ``_resolve_aliases`` reads the aliases. An + entity pulled in from an installed bundle has its references pointed so even where the store has an entity of + the alias's id (``_installed_alias``): it comes with what its aliases name.""" + installed = entry.entry in self.origins references, aliased = [], {} for ref in entry.references: - if ref.local is None and (use := self._alias_of(ref)) is not None: - aliased[ref.kind, ref.id] = use.target_id - ref = self._pulled(replace(ref, id=use.target_id)) + target = None + if ref.local is None: + target = self._installed_alias(ref) if installed else None + if target is None and (use := self._alias_of(ref)) is not None: + target = use.target_id + if target is not None: + aliased[ref.kind, ref.id] = target + ref = self._pulled(replace(ref, id=target)) references.append(ref) if not aliased: return entry return replace(entry, config=_renamed(entry, aliased), references=tuple(references)) + def _installed_alias(self, ref: Reference) -> str | None: + """The id of what the installed alias ``ref``, an installed bundle's entity's store reference, names, whatever + the store has of the alias's id; None when it names none, pins a version, as an alias has none, or names a + refused alias, left to ``_alias_of`` as any other reference is.""" + table = self.alias_tables.get(ref.kind) + if table is None or is_local_id(ref.id) or ref.version is not None or table.refusal(ref.id) is not None: + return None + named = table.target(ref.id) + return None if named is None else named.target_id + def _alias_of(self, ref: Reference) -> AliasUse | None: """The installed alias ``ref``, a store reference, names, to point it at: None when it names none, or the store's entity of its id shadows it. Why an alias can't be named so is kept for the store ref check to report.""" @@ -1239,8 +1257,9 @@ def _leaves_an_agent_unset(task: ResolvedEntry) -> bool: def _renamed(entry: ResolvedEntry, ids: Mapping[tuple[EntityKind, str], str]) -> Any: - """A copy of ``entry``'s config with each field naming an alias in ``ids``, by kind, naming what it names instead: - a task's steps by their types' refs, any other entry's toml by its class's ``toml_refs``.""" + """A copy of ``entry``'s config with each unpinned field naming an alias in ``ids``, by kind, naming what it names + instead: a task's steps by their types' refs, any other entry's toml by its class's ``toml_refs``. A pinned field + is left as it is, since only an unpinned reference is ever pointed at an alias's entity.""" if not ids: return entry.config config = copy.deepcopy(entry.config) @@ -1252,7 +1271,7 @@ def _renamed(entry: ResolvedEntry, ids: Mapping[tuple[EntityKind, str], str]) -> else: sites = list(ref_sites(getattr(entity_class(entry.entry), "toml_refs", ()), config, inline_pins=True)) for site in sites: - if site.ref.role is RefRole.INPUT and (site.ref.kind, site.value) in ids: + if site.ref.role is RefRole.INPUT and site.version is None and (site.ref.kind, site.value) in ids: site.rewrite(ids[site.ref.kind, site.value]) if entry.entry.kind is BundleKind.TASK: sync_context_paths(entry.entry.config, config) diff --git a/tst/unit/bundle/plan_test.py b/tst/unit/bundle/plan_test.py index 4b2362989..a8ffe84a0 100644 --- a/tst/unit/bundle/plan_test.py +++ b/tst/unit/bundle/plan_test.py @@ -2,6 +2,7 @@ import json import os +from types import SimpleNamespace from typing import Literal import pytest @@ -478,3 +479,15 @@ def test_every_problem_is_reported_together(make): "tasks/t.json: step 'deploy': env_id: there is no env 'missing-env' in the store", "tasks/w.json: step 'snap': '@local/s' is also written by step 'snap' of tasks/u.json; give each its own id", ) + + +def test_pointing_an_alias_rewrites_only_the_fields_naming_it_unpinned(): + """A pinned field keeps naming the store's entity, as its reference does; only an unpinned one is pointed.""" + steps = [{"id": "a", "type": "deploy_env", "env_id": "crm-x"}, + {"id": "b", "type": "deploy_env", "env_id": "crm-x", "env_version": 2}] + entry = SimpleNamespace(entry=SimpleNamespace(kind=BundleKind.TASK, config=steps), config=steps) + + renamed = plan_module._renamed(entry, {(EntityKind.ENV, "crm-x"): "@local/acme/envs/crm-x"}) + + assert [(step["env_id"], step.get("env_version")) for step in renamed] == [ + ("@local/acme/envs/crm-x", None), ("crm-x", 2)] diff --git a/tst/unit/bundle/register_test.py b/tst/unit/bundle/register_test.py index 216a0240f..236893e80 100644 --- a/tst/unit/bundle/register_test.py +++ b/tst/unit/bundle/register_test.py @@ -519,6 +519,30 @@ def test_chromium_registers_into_a_shared_store_with_its_driver_and_base_compute assert (chromium.type, chromium.computer.id, chromium.driver.id) == ("computer_app", "ubuntu-24.04-browser", "page") +def test_the_installed_chromium_registers_over_its_own_driver_though_the_store_holds_another_under_the_alias( + home, composed, monkeypatch): + """As a later promotion registers it into the hub: the store's `page` is no longer the installed driver, and + the installed chromium, named by its own id, still binds its own, written again under the alias.""" + for module in (register_module, env_store_module): + monkeypatch.setattr(module, "shared_store", lambda store: True) + bases = {image.ref() for image in COMPUTER_IMAGES} + monkeypatch.setattr("agent_env.env.published_images._registry_digest", lambda ref: DIGEST if ref in bases else None) + monkeypatch.setattr(computer_artifact_module, "_inspect", + lambda image, arches, read: computer_artifact_module._unread(arches, "no registry in a test")) + files = {"tasks/t.json": json.dumps([{"id": "env", "type": "deploy_env", + "env_id": "@local/agentenv-framework/browsers/chromium"}])} + register_bundle(layout(home / "first", files)) + edited = Env.get("page") + edited.description, edited.version = "edited in the store", None + env_store_module.get_env_store().put_document(edited) + + registered = register_bundle(layout(home / "second", files)) + + versions = {done.write.id: done.version for done in registered.materialization.writes} + assert versions["page"] == 3 and Env.get("page").description != "edited in the store" + assert Env.get("chromium", versions["chromium"]).driver.version == 3 + + def _desktop(home, monkeypatch, compose): """A bundle of a computer env on the shipped base and driver, registered into a shared store as the hub is, under their aliases, ``compose`` standing in for the composer."""