Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 63 additions & 7 deletions src/agent_env/bundle/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
segment_problem,
)
from agent_env.bundle.plan import STORE_GETTERS, check_bundle
from agent_env.bundle.register import REGISTRATIONS
from agent_env.bundle.requires import Providers, Use, detected
from agent_env.bundle.resolve import resolve_bundle, sync_context_paths
from agent_env.config import get_config
Expand All @@ -62,14 +63,21 @@
from agent_env.env.registry import get_env_registry
from agent_env.eval import Eval
from agent_env.plugins import ARTIFACTS, ENVS, TASK_STEPS
from agent_env.plugins._bundles import AGENT_ALIASES, ARTIFACT_ALIASES, ENV_ALIASES, InstalledAliases, installed_aliases
from agent_env.store import Filter
from agent_env.store.base import NotFoundError
from agent_env.store.ids import LOCAL_PREFIX, is_local_id
from agent_env.store.routing import configured_store
from agent_env.store.image_store.oci_registry_credentials import names_registry
from agent_env.store.image_store.registry_api import pin_digest
from agent_env.task.task import Task
from agent_env.task_step.registry import get_task_step_registry
from agent_env.utils.paths import os_reason

_ALIAS_TABLES = {EntityKind.AGENT: AGENT_ALIASES, EntityKind.ENV: ENV_ALIASES, EntityKind.ARTIFACT: ARTIFACT_ALIASES}
# The kind of entity whose aliases a kind folder's names would shadow in a run.
_FOLDER_KINDS = {BundleKind.AGENT.value: EntityKind.AGENT, BundleKind.ENV.value: EntityKind.ENV,
BundleKind.ARTIFACT.value: EntityKind.ARTIFACT}
# How long a name taken from an id may be, in bytes; ids are capped far above it, and a longer name helps nobody.
_MAX_NAME_BYTES = 200
_DOWNLOADS = 8 # objects downloaded at once
Expand All @@ -85,6 +93,7 @@ def import_task(task_id: str, dest: Path | str, *, version: int | None = None,
digest its registry serves now when it gives none, with its build context beside it."""
importer = _Importer(Path(dest), include_scopes, progress, image_refs)
task = Task.get(task_id, version)
importer.reserve_aliases([task])
importer.add_task(task)
return importer.write(f"Imported from the store's task {task.id} v{task.version}.", {"task": task.id,
"version": task.version})
Expand All @@ -97,7 +106,9 @@ def task_images(task_id: str, *, version: int | None = None,
task with ``image_refs`` naming them there. Every image is listed, however it's stored, since each is then given a
reference; raises BundleError when the import would fail on anything else it reads."""
importer = _Importer(None, include_scopes)
importer.add_task(Task.get(task_id, version))
task = Task.get(task_id, version)
importer.reserve_aliases([task])
importer.add_task(task)
if importer.problems:
raise BundleError(importer.problems)
return tuple(importer.images_named.values())
Expand All @@ -110,7 +121,7 @@ def import_eval(eval_id: str, dest: Path | str, *, version: int | None = None,
a bundle, leaving out their steps' scoped fields and writing ``image_refs`` as ``import_task`` does."""
importer = _Importer(Path(dest), include_scopes, progress, image_refs)
stored = Eval.get(eval_id, version)
names, seen = [], set()
tasks, seen = [], set()
for entry in stored.tasks:
try:
task = Task.get(entry.task_id, entry.task_version)
Expand All @@ -123,7 +134,9 @@ def import_eval(eval_id: str, dest: Path | str, *, version: int | None = None,
importer.duplicates.append(f"task {task.id} v{task.version}")
continue
seen.add((task.id, task.version))
names.append(importer.add_task(task))
tasks.append(task)
importer.reserve_aliases(tasks)
names = [importer.add_task(task) for task in tasks]
if names:
importer.add_eval(stored, names)
return importer.write(f"Imported from the store's eval {stored.id} v{stored.version} and the tasks it names.",
Expand Down Expand Up @@ -207,6 +220,8 @@ def __init__(self, dest: Path | None, include_scopes: Collection[FieldScope] = (
self.jobs: list[ImageJob] = []
self.providers = Providers()
self.installed: tuple[InstalledBundle, ...] | None = None
self.aliases: dict[str, InstalledAliases] = {} # the installed bundles' aliases, by aliases.toml table
self.reserved: dict[EntityKind, set[str]] = {} # each kind's installed aliases a step names, folded
if self.dest is not None:
_check_destination(self.dest)

Expand Down Expand Up @@ -454,6 +469,9 @@ def _ref(self, site: RefSite, where: str, pins: Mapping[str, set[int]]) -> None:
if isinstance(entity, str):
self.problems.append(f"{where}: {entity}")
return
if (bundle := self._registered_from(kind, entity)) is not None:
self._from_installed(site, where, bundle, _registered_from_bundle(bundle))
return
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
if isinstance(site.key, str) and (why := _not_taken(site, entity)) is not None:
# The store holds the step as it is, though the step can't read what it names; a folder can't.
site.rewrite(None)
Expand Down Expand Up @@ -535,9 +553,40 @@ def _installed_for(self, kind: EntityKind, entity_id: str) -> InstalledBundle |
self.installed = installed_bundles()
return next((bundle for bundle in self.installed if entity_id.startswith(f"{bundle.id_root}/")), None)

def _from_installed(self, site: RefSite, where: str, bundle: InstalledBundle) -> None:
def _registered_from(self, kind: EntityKind, entity: Any) -> InstalledBundle | None:
"""The installed bundle ``entity`` comes with when the store holds it under that bundle's alias for it and this
version is the one ``bundle register`` wrote from the bundle, as its ledger in the store records: a run writes
it from the bundle too. An entity stored under the alias by anything else is the store's own."""
table = _ALIAS_TABLES.get(kind)
if table is None or (claim := self._aliases(table).target(entity.id)) is None:
return None
registered = configured_store(get_config().get_document_store()).find_one(REGISTRATIONS, Filter.of(
store=kind.value, id=claim.alias, version=entity.version, status="done",
bundle=f"installed:{claim.bundle.id_root.removeprefix(LOCAL_PREFIX)}"))
return claim.bundle if registered is not None else None

def reserve_aliases(self, tasks: Collection[Task]) -> None:
"""Keep each installed alias a step of ``tasks`` names out of the folders' names, before any is given: such a
step may name it floating, and a folder of that name would capture it in a run. Reads no store."""
registry = get_task_step_registry()
for task in tasks:
for step in task.to_dict()["steps"]:
for site in ref_sites(registry[step["type"]].entity_refs or (), step):
table = _ALIAS_TABLES.get(site.ref.kind)
if isinstance(site.value, str) and table is not None and self._aliases(table).target(site.value):
self.reserved.setdefault(site.ref.kind, set()).add(fold(site.value))

def _aliases(self, table: str) -> InstalledAliases:
"""The aliases the installed bundles give in ``table``."""
if self.installed is None:
self.installed = installed_bundles()
if table not in self.aliases:
self.aliases[table] = installed_aliases(self.installed, table)
return self.aliases[table]

def _from_installed(self, site: RefSite, where: str, bundle: InstalledBundle, why: str | None = None) -> None:
"""An installed bundle's entity has no fixed version, so it isn't pinned, and a pin the store held is dropped."""
why = _from_bundle(bundle)
why = why or _from_bundle(bundle)
if site.version is not None and site.version_key is not None:
why += f"; the stored pin to v{site.version} is dropped, as such an entity has no fixed version"
_unpin(site)
Expand Down Expand Up @@ -566,11 +615,13 @@ def _default_agents(self, step: dict, refs: tuple[EntityRef, ...], where: str) -
"default"))

def _name(self, kind: str, entity_id: str) -> str:
"""A name in the ``kind`` folder: the id's last segment made valid, numbered when taken, caselessly."""
"""A name in the ``kind`` folder: the id's last segment made valid, numbered when taken, caselessly, or when a
step of the export names it as an installed alias (``reserve_aliases``)."""
names = _names_of(BundleKind(kind))
aliases = self.reserved.get(_FOLDER_KINDS[kind], set()) if kind in _FOLDER_KINDS else set()
base = _valid_name(entity_id.rsplit("/", 1)[-1]) or kind.removesuffix("s")
name, n = base, 1
while fold(name) in self.taken[names]:
while fold(name) in self.taken[names] or fold(name) in aliases:
n += 1
name = f"{base}-{n}"
self.taken[names].add(fold(name))
Expand All @@ -586,6 +637,11 @@ def _from_bundle(bundle: InstalledBundle) -> str:
return f"it comes with the installed bundle {bundle.qualified}, which a run writes it from"


def _registered_from_bundle(bundle: InstalledBundle) -> str:
return (f"the store holds it under its alias as registered from the installed bundle {bundle.qualified}, which a run "
"writes it from")


def _kind_of(entity: Any) -> EntityKind:
if isinstance(entity, Env):
return EntityKind.ENV
Expand Down
71 changes: 70 additions & 1 deletion tst/unit/bundle/importer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from agent_env.bundle.importer import ImportedEval, import_eval, import_task
from agent_env.bundle.installed import InstalledBundle
from agent_env.bundle.materialize import materialize
from agent_env.bundle import register as register_module
from agent_env.bundle.plan import plan_bundle
from agent_env.bundle.register import register_bundle
from agent_env.bundle.requires import Provider
from agent_env.bundle.resolve import resolve_bundle
from agent_env.cli import cli
Expand All @@ -33,7 +35,7 @@
from agent_env.store.routing import namespace_routing
from agent_env.task import Task
from agent_env.task_step.task_step import TaskStep
from tst.unit.bundle._support import context_image, fake_plugins
from tst.unit.bundle._support import context_image, fake_plugins, layout

STEPS = [
{"id": "env", "type": "deploy_env", "env_id": "crm-env"},
Expand Down Expand Up @@ -745,3 +747,70 @@ def test_an_installed_bundles_entity_floats_unpinned_so_the_import_runs_it_from_
assert ("dropped" in imported.floating[0].why) == (pin is not None) and imported.pinned == ()
assert _steps(imported.root, "t")[0]["a2a_agent_version"] is None



AGENT_NAMED_BY_ALIAS = [{"id": "agent", "type": "deploy_agent", "env_ids": [], "a2a_agent_id": "claude-code"}]


@pytest.fixture
def registry(local_stores, tmp_path, monkeypatch, agents_published):
"""A registry of bare ids, with ``register`` registering a bundle into it, outside git."""
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.setattr(register_module, "_git", lambda root, *args: None)
return lambda name, steps: register_bundle(layout(tmp_path / name, {"tasks/t.json": json.dumps(steps)}))


def test_an_agent_registered_from_an_installed_bundle_under_its_alias_floats_by_the_alias(registry, tmp_path):
registry("triage", AGENT_NAMED_BY_ALIAS)

imported = import_task("triage-t", tmp_path / "imported", include_scopes=[FieldScope.USER])

assert [(ref.where, ref.id) for ref in imported.floating] == [
("tasks/triage-t.json: step 'agent': a2a_agent_id", "claude-code")]
assert "registered from the installed bundle agentenv-framework" in imported.floating[0].why
assert "the stored pin to v1 is dropped" in imported.floating[0].why
assert imported.written == () and not (imported.root / "agents").exists()
step = _steps(imported.root, "triage-t")[0]
assert (step["a2a_agent_id"], step["a2a_agent_version"]) == ("claude-code", None)


@pytest.mark.parametrize("registered_first", [False, True])
def test_an_agent_stored_under_an_installed_alias_by_anything_else_is_written_into_the_folder(registry, tmp_path,
registered_first):
if registered_first:
registry("triage", AGENT_NAMED_BY_ALIAS)
A2AAgent.put(id="claude-code", docker_image_artifact=context_image(tmp_path, "own-image"))
_task("own", AGENT_NAMED_BY_ALIAS)

imported = import_task("own", tmp_path / "imported", include_scopes=[FieldScope.USER])

assert "claude-code" not in [ref.id for ref in imported.floating]
assert [(entity.folder, entity.id, entity.version) for entity in imported.written] == [
("agents/claude-code-2", "claude-code", 2 if registered_first else 1)]
assert _steps(imported.root, "own")[0]["a2a_agent_id"] == "claude-code-2"


def test_a_folder_never_takes_an_alias_a_step_floats_so_each_version_keeps_its_own(registry, tmp_path):
registry("triage", AGENT_NAMED_BY_ALIAS)
A2AAgent.put(id="claude-code", docker_image_artifact=context_image(tmp_path, "own-image"))
_task("both", [{**AGENT_NAMED_BY_ALIAS[0], "id": "own", "a2a_agent_version": 2},
{**AGENT_NAMED_BY_ALIAS[0], "id": "registered", "a2a_agent_version": 1}])

imported = import_task("both", tmp_path / "imported", include_scopes=[FieldScope.USER])

steps = _steps(imported.root, "both")
assert [step["a2a_agent_id"] for step in steps] == ["claude-code-2", "claude-code"]
assert [(entity.folder, entity.version) for entity in imported.written] == [("agents/claude-code-2", 2)]
assert [(ref.where, ref.id) for ref in imported.floating if ref.id == "claude-code"] == [
("tasks/both.json: step 'registered': a2a_agent_id", "claude-code")]


def test_an_export_whose_steps_name_no_installed_alias_never_reads_the_registration_ledger(store, monkeypatch):
def unreachable(*args, **kwargs):
raise AssertionError("the registration ledger was read")

monkeypatch.setattr(importer_module, "configured_store", unreachable)

imported = import_task("triage", store / "triage", include_scopes=[FieldScope.USER])

assert [entity.folder for entity in imported.written] == ["envs/crm-env", "artifacts/crm-data", "agents/solver"]
Loading