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
16 changes: 10 additions & 6 deletions src/agent_env/bundle/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import contextlib
import copy
import json
import os
import re
Expand Down Expand Up @@ -52,7 +53,7 @@
)
from agent_env.bundle.plan import STORE_GETTERS, check_bundle
from agent_env.bundle.requires import Providers, Use, detected
from agent_env.bundle.resolve import resolve_bundle
from agent_env.bundle.resolve import resolve_bundle, sync_context_paths
from agent_env.config import get_config
from agent_env.entity.export import ExportContext, Unwritable, checked_path
from agent_env.entity.refs import EntityKind, EntityRef, RefRole, RefSite, default_agent_refs, ref_sites
Expand Down Expand Up @@ -216,9 +217,11 @@ def add_task(self, task: Task) -> str:
found = []
for step in steps:
cls = registry[step["type"]]
pruned = prune(step, cls.field_scopes, self.include_scopes) # first, so no pruned field is written
found.append((step, cls, pruned, [site for site in ref_sites(cls.entity_refs or (), step)
if isinstance(site.value, str) and site.value]))
# First, so no pruned field or secret is written, nor read back when paths are synced below.
dropped = prune(step, cls.field_scopes, self.include_scopes) + scrub(step)
found.append((step, cls, dropped, [site for site in ref_sites(cls.entity_refs or (), step)
if isinstance(site.value, str) and site.value]))
stored = copy.deepcopy(steps) # as the store holds it, less what's left out, before any id is renamed
sites = [site for *_, step_sites in found for site in step_sites]
outputs = _output_names(sites)
for site in sites: # each output, and each step that reads it, takes the output's name
Expand All @@ -228,16 +231,17 @@ def add_task(self, task: Task) -> str:
for site in sites:
if site.ref.kind is EntityKind.ENV and site.version is not None:
pins.setdefault(_key(site)[1], set()).add(site.version)
for step, cls, pruned, step_sites in found:
for step, cls, dropped, step_sites in found:
where = f"{path}: step {step.get('id')!r}"
self.dropped += [Dropped(f"{where}: {at}", why) for at, why in pruned + scrub(step)]
self.dropped += [Dropped(f"{where}: {at}", why) for at, why in dropped]
self._need(where, "step type", TASK_STEPS, cls.type, cls)
if cls.entity_refs is None:
self.undeclared.append(f"{where} ({cls.type})")
for site in step_sites:
if _key(site) not in outputs: # the run writes an output again, so it's no entity to write here
self._ref(site, f"{where}: {site.path}", pins)
self._default_agents(step, cls.entity_refs or (), where)
sync_context_paths(stored, steps)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
self.files[path] = json.dumps(steps, indent=2, ensure_ascii=False, allow_nan=False) + "\n"
self.tasks.append(ImportedTask(path, task.id, task.version, len(steps)))
self.declared += task.requires or ()
Expand Down
4 changes: 3 additions & 1 deletion src/agent_env/bundle/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
from .parse import NAMED_BY, Bundle, BundleEntry, BundleError, BundleKind, parse_bundle
from .variants import Varied
from .resolve import (BuiltImage, BundleImage, Reference, ResolvedBundle, ResolvedEntry, build_step, entity_class,
resolve_bundle)
resolve_bundle, sync_context_paths)

if TYPE_CHECKING:
from .installed import InstalledBundle
Expand Down Expand Up @@ -1255,6 +1255,8 @@ def _renamed(entry: ResolvedEntry, ids: Mapping[tuple[EntityKind, str], str]) ->
for site in sites:
if site.ref.role is RefRole.INPUT 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)
return config


Expand Down
15 changes: 11 additions & 4 deletions src/agent_env/bundle/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from agent_env.store.ids import LOCAL_PREFIX, is_local_id
from agent_env.store.image_store.oci_registry_credentials import is_loopback_host
from agent_env.store.routing import configured_store, namespace_routing
from agent_env.task_step.context import context_path_segments, renamed_context_path
from agent_env.task_step.registry import get_task_step_registry

from ._secrets import credentials_in
Expand Down Expand Up @@ -286,9 +287,13 @@ def _aliased(use: AliasUse, ids: Mapping[str, str]) -> AliasUse:


def _renamed(value: Any, ids: Mapping[str, str]) -> Any:
"""A copy of ``value`` with each string that's an ``@local`` id in ``ids``, or derived from one, its registry id."""
"""A copy of ``value`` with each string that's an ``@local`` id in ``ids``, or derived from one, its registry id, and
each such id in a path into a run's context its registry id too."""
if isinstance(value, str):
return ids.get(value) or _derived(value, ids) or value if is_local_id(value) else value
if is_local_id(value):
return ids.get(value) or _derived(value, ids) or value
return renamed_context_path(value, lambda segment: (ids.get(segment) or _derived(segment, ids))
if is_local_id(segment) else None)
if isinstance(value, dict):
return {key: _renamed(item, ids) for key, item in value.items()}
if isinstance(value, list):
Expand Down Expand Up @@ -360,8 +365,10 @@ def _unregistrable(plan: Plan, shared: bool) -> Iterator[str]:
continue
where = plan.path(write.source.entry)
for path, text in _strings(write.source.config):
if is_local_id(text):
yield f"{where}: {path or 'its value'} names {text!r}, which only this machine's stores hold"
local = text if is_local_id(text) else next(
(segment for segment in context_path_segments(text) or () if is_local_id(segment)), None)
if local is not None:
yield f"{where}: {path or 'its value'} names {local!r}, which only this machine's stores hold"
elif shared and (url := _local_url(text)) is not None:
yield f"{where}: {path or 'its value'} names {url}, which only this machine reaches"
if shared:
Expand Down
74 changes: 68 additions & 6 deletions src/agent_env/bundle/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
References are the fields step types declare in ``entity_refs`` and the toml keys entity types declare
in ``toml_refs``. The name of one of this bundle's entities becomes that entity's id, a name an output
of the same task writes becomes the output's id, and anything else is a store id, left as written;
whether store ids exist is checked later, with the other checks that read a store.
whether store ids exist is checked later, with the other checks that read a store. An env renamed so is
also renamed where a path into the run's context a step type declares in ``context_paths`` names it
(``sync_context_paths``).
"""

from __future__ import annotations

import copy
import functools
import logging
import unicodedata
from collections.abc import Callable, Iterator
from dataclasses import dataclass, field, replace
Expand All @@ -19,6 +22,7 @@
from agent_env.a2a_agent import A2AAgent
from agent_env.artifact.artifacts.docker_image import DockerImageArtifact
from agent_env.artifact.registry import canonical_type, get_artifact_registry
from agent_env.entity.paths import field_sites
from agent_env.entity.refs import EntityKind, EntityRef, RefRole, RefSite, parse_toml_ref, ref_sites
from agent_env.env.envs.gateway_server import GatewayEnv
from agent_env.env.envs.service_db import ServiceDBEnv
Expand All @@ -28,6 +32,7 @@
from agent_env.plugins._requirements import unmet_problems
from agent_env.store.ids import HF_PREFIX, derive_id, is_local_id, validate_local_id
from agent_env.store.image_store.oci_registry_credentials import names_registry
from agent_env.task_step.context import ENV_KEYED_METADATA, context_path, context_path_segments
from agent_env.task_step.registry import get_task_step_registry
from agent_env.task_step.task_step import TaskStep, attach_retry_config, dependencies

Expand All @@ -36,6 +41,8 @@
from .parse import BUNDLE_TOML, DOCKERFILE, NAMED_BY, Bundle, BundleEntry, BundleError, BundleKind, foreign_hub_id
from .requires import UnmetRequirements

logger = logging.getLogger(__name__)

# Step keys that hold step ids, never entity ids.
_STEP_ID_KEYS = ("id", "type", "depends_on")
# The env types config names one of, for every deploy that needs one, by the setting that names it.
Expand Down Expand Up @@ -258,6 +265,8 @@ def _task(self, entry: BundleEntry) -> ResolvedEntry:
continue
self._resolve(entry, site, f"{where}{site.path}", site.value, site.version, outputs,
functools.partial(_upstream, edges, index), references)
sync_context_paths(entry.config, steps)
self._undeclared_paths(entry, declared, _rewritten_ids(entry.config, steps, EntityKind.ENV))
return ResolvedEntry(entry, steps, tuple(references), tuple(outputs.values()))

def _image_folder(self, entry: BundleEntry) -> bool:
Expand Down Expand Up @@ -484,6 +493,18 @@ def _undeclared(self, entry: BundleEntry, step: dict, cls: type, outputs: dict[t
self._problem(entry, f"{where}{path} = {value!r} names {named}, but {cls.type} doesn't declare its "
"entity_refs, so it can't be rewritten")

def _undeclared_paths(self, entry: BundleEntry, declared: list[tuple[int, dict, type]],
renamed: dict[str, str]) -> None:
"""Warn of a path naming a renamed env in a field its step's type doesn't declare (``context_paths``), which
the run would look up as written."""
for _, step, cls in declared:
for where, value in _strings(step):
if (env := _named_env(value)) in renamed:
logger.warning("%s: step %r: %s = %r names %r, which this bundle writes as %r, but %s doesn't "
"declare the field in context_paths, so it isn't rewritten",
relative(self.bundle.root, entry.path), step.get("id"), where, value, env,
renamed[env], cls.type)

def _problem(self, entry: BundleEntry, message: str) -> None:
self.problems.append(f"{relative(self.bundle.root, entry.path)}: {message}")

Expand Down Expand Up @@ -515,6 +536,26 @@ def _registry_problem(key: str, name: Any) -> str | None:
return f"{key}: {name!r} doesn't name its registry; spell it out, as in docker.io/library/{name}"


def sync_context_paths(written: list[dict], steps: list[dict]) -> None:
"""Point each path into the run's context a step of ``steps`` declares (``TaskStep.context_paths``) at the env its
task's env fields name now, rebuilt from the path ``written``, the task as written, gives it. Only a path whose env
is renamed is written, and only its env segment changes, so a key that just shares an entity's name, such as a
verifier id, is kept. Each place that rewrites a task's env fields calls it after, and calling it again changes
nothing."""
renamed = _rewritten_ids(written, steps, EntityKind.ENV)
registry = get_task_step_registry()
authored = {step.get("id"): step for step in written}
for step in steps:
cls = registry.get(step.get("type")) if isinstance(step.get("type"), str) else None
if cls is None or (before := authored.get(step.get("id"))) is None:
continue
for path in cls.context_paths:
was = {where: holder[key] for where, holder, key in field_sites(before, path)}
for where, holder, key in field_sites(step, path):
if _named_env(was.get(where)) in renamed:
holder[key] = _env_renamed(was[where], renamed)


def build_step(step: dict) -> TaskStep:
"""The step a task's write puts, built from a copy of ``step``, so the resolved config the ledger hashes
never changes."""
Expand Down Expand Up @@ -571,16 +612,37 @@ def _strings(value: Any, path: str = "") -> Iterator[tuple[str, str]]:
yield from _strings(item, f"{path}[{i}]")


def _rewritten_ids(written: list[dict], resolved: list[dict]) -> dict[str, str]:
"""Each id a reference field of ``written``'s steps holds that the same field of ``resolved``'s holds another id
in place of, to that id."""
def _rewritten_ids(written: list[dict], resolved: list[dict], kind: EntityKind | None = None) -> dict[str, str]:
"""Each id a reference field of ``written``'s steps holds, of ``kind`` when given, that the same field of
``resolved``'s holds another id in place of, to that id."""
registry = get_task_step_registry()
renamed = {}
for before, after in zip(written, resolved):
cls = registry.get(before.get("type"))
if cls is None or not cls.entity_refs:
continue
now = {site.path: site.value for site in ref_sites(cls.entity_refs, after)}
renamed.update((site.value, now[site.path]) for site in ref_sites(cls.entity_refs, before)
refs = [ref for ref in cls.entity_refs if kind is None or ref.kind is kind]
now = {site.path: site.value for site in ref_sites(refs, after)}
renamed.update((site.value, now[site.path]) for site in ref_sites(refs, before)
if site.path in now and now[site.path] != site.value)
return renamed


def _env_segment(path: Any) -> tuple[list[str], int] | None:
"""A path into a run's context that names an env, and the index of its segment that does (``ENV_KEYED_METADATA``)."""
segments = context_path_segments(path) or []
index = ENV_KEYED_METADATA.get(segments[0]) if segments else None
return (segments, index) if index is not None and index < len(segments) else None


def _named_env(path: Any) -> str | None:
found = _env_segment(path)
return found[0][found[1]] if found else None


def _env_renamed(path: str, renamed: dict[str, str]) -> str:
"""``path`` naming the env ``renamed`` gives its env segment's id for by that id."""
if (env := _named_env(path)) not in renamed:
return path
segments, index = _env_segment(path)
return context_path([*segments[:index], renamed[env], *segments[index + 1:]])
6 changes: 4 additions & 2 deletions src/agent_env/bundle/variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from agent_env.task_step.registry import get_task_step_registry

from .parse import Bundle, BundleEntry, BundleError, BundleKind, segment_problem
from .resolve import entity_class
from .resolve import entity_class, sync_context_paths

_TARGET = f"{BundleKind.ENV.value}/"
_FORM = "envs/NAME.KEY=VALUE, such as envs/desktop.driver=cua-driver"
Expand Down Expand Up @@ -187,8 +187,10 @@ def _renamed(entry: BundleEntry, new_id: str, config: Any) -> BundleEntry:
def _repointed(entry: BundleEntry, config: Any, renaming: dict[EntityKind, dict[str, str]]) -> Any:
"""``config``, ``entry``'s steps or toml, with each env or task it names by an id that changes named by its variant's
id."""
config = copy.deepcopy(config)
written, config = config, copy.deepcopy(config)
for site in _sites(entry, config):
if site.value in renaming.get(site.ref.kind, {}):
site.rewrite(renaming[site.ref.kind][site.value], site.version)
if entry.kind is BundleKind.TASK:
sync_context_paths(written, config)
return config
21 changes: 16 additions & 5 deletions src/agent_env/entity/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@
PATH = re.compile(r"[A-Za-z_]\w*(\[\]|\{\})?(\.[A-Za-z_]\w*(\[\]|\{\})?)*")


def field_sites(data: dict, path: str) -> Iterator[tuple[str, dict, str]]:
"""Each place ``path`` holds a value that isn't None in ``data``: where, as a path with indexes, the dict holding
it, and its key there."""
def field_sites(data: dict, path: str) -> Iterator[tuple[str, dict | list, str | int]]:
"""Each place ``path`` holds a value that isn't None in ``data``: where, as a path with indexes, what holds it, and
its key there. A last segment's ``[]`` or ``{}`` is each element of its list or value of its table."""
*parents, leaf = path.split(".")
key = leaf.removesuffix("[]").removesuffix("{}")
for trail, holder in descend(data, parents, ()):
if isinstance(holder, dict) and holder.get(leaf) is not None:
yield ".".join((*trail, leaf)), holder, leaf
value = holder.get(key) if isinstance(holder, dict) else None
if value is None:
continue
where = ".".join((*trail, key))
if leaf.endswith("[]"):
items = enumerate(value) if isinstance(value, list) else ()
yield from ((f"{where}[{i}]", value, i) for i, item in items if item is not None)
elif leaf.endswith("{}"):
items = value.items() if isinstance(value, dict) else ()
yield from ((f"{where}.{name}", value, name) for name, item in items if item is not None)
else:
yield where, holder, key


def descend(node: Any, segments: list[str], trail: tuple[str, ...]) -> Iterator[tuple[tuple[str, ...], Any]]:
Expand Down
38 changes: 38 additions & 0 deletions src/agent_env/task_step/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import contextlib
import dataclasses
import re
from collections.abc import Callable
from dataclasses import dataclass, field
from typing import Any

Expand All @@ -15,13 +17,38 @@
HEARTBEAT_KEY = "_heartbeat_fn"
# context.metadata[ENTITY_IDS_KEY]: the run's task's Task.entity_ids, set by Task.run.
ENTITY_IDS_KEY = "entity_ids"
# The context.metadata keys whose entries name a deployed env by its id, each with the index of the path segment that
# names it: env_skills is keyed by agent name, then env id.
ENV_KEYED_METADATA = {"env_trigger_state": 1, "env_changes": 1, "env_trajectory": 1, "live_websites": 1,
"env_skills": 2}


def run_entity_id(metadata: dict, entity_id: str) -> str:
"""The id the run's task gave the entity it names as ``entity_id`` (``Task.entity_ids``), else ``entity_id``."""
return (metadata.get(ENTITY_IDS_KEY) or {}).get(entity_id, entity_id)


def context_path_segments(path: Any) -> list[str] | None:
"""The keys and list indexes a path into a run's context walks, or None when ``path`` isn't one. A segment is
letters, digits, ``_`` and ``-`` after a ``.``, or any id in brackets: ``env_trigger_state[@local/x/crm].statuses``."""
if not isinstance(path, str) or not _CONTEXT_PATH.fullmatch(path):
return None
return [plain or bracketed for plain, bracketed in _CONTEXT_PATH_SEGMENT.findall(path)]


def context_path(segments: list[str]) -> str:
"""The path into a run's context that walks ``segments``: each that a dotted path can't hold in brackets."""
return "".join(_path_segment(segment, first=i == 0) for i, segment in enumerate(segments))


def renamed_context_path(path: str, rename: Callable[[str], str | None]) -> str:
"""``path`` with each segment ``rename`` gives another id for naming that id; ``path`` itself when it isn't a path
into a run's context or ``rename`` renames none of its segments."""
segments = context_path_segments(path) or []
renamed = [rename(segment) or segment for segment in segments]
return path if renamed == segments else context_path(renamed)


class StepContextError(RuntimeError):
"""The run's context can't satisfy a step: something the step needs from an earlier step is missing, or
something it would create is already there. Running the step again on the same context fails the same
Expand Down Expand Up @@ -293,3 +320,14 @@ def from_dict(cls, data: dict[str, Any]) -> TaskStepContext:
agent_harness=data.get("agent_harness"),
instance_id=data.get("instance_id"),
)


_PLAIN_SEGMENT = re.compile(r"[\w-]+")
_CONTEXT_PATH = re.compile(r"(?:[\w-]+|\[[^\[\]]+\])(?:\.[\w-]+|\[[^\[\]]+\])*")
_CONTEXT_PATH_SEGMENT = re.compile(r"\.?([\w-]+)|\[([^\[\]]+)\]")


def _path_segment(segment: str, *, first: bool) -> str:
if not _PLAIN_SEGMENT.fullmatch(segment):
return f"[{segment}]"
return segment if first else f".{segment}"
Loading
Loading