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
13 changes: 10 additions & 3 deletions src/agent_env/artifact/artifacts/environment_universe_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pathlib import Path

from agent_env.artifact.artifacts.environment import EnvironmentArtifact
from agent_env.store.ids import derive_id, validate_local_id
from agent_env.store.ids import derive_id, is_local_id, validate_local_id

# What a universe's metadata is called: the folder its files go in, and the segment of the ids they're written under.
METADATA = "metadata"
Expand Down Expand Up @@ -123,7 +123,7 @@ def read_folder(universe_id: str, files: Mapping[str, Path],
try:
for id in (env_id, file_id):
if id is not None:
validate_local_id(id)
_check(id)
except ValueError as e:
problems.append(f"{folder}: can't name the artifacts written from it ({e}); give the universe a shorter "
"name")
Expand All @@ -144,13 +144,20 @@ def _child_id(derive: Callable[[str], str], name: str, *, environment: bool) ->
id = derive(name)
try:
for derived in (id, EnvironmentArtifact.derived_file_id(id)) if environment else (id,):
validate_local_id(derived)
_check(derived)
return id
except ValueError:
pass
return derive(f"sha256__{hashlib.sha256(name.encode('utf-8')).hexdigest()[:16]}")


def _check(id: str) -> None:
"""Raise ValueError when ``id`` is a local id an id can't be; a registry id, as ``bundle register`` writes a universe
under, takes the folder's names as they are."""
if is_local_id(id):
validate_local_id(id)


def _fold(name: str) -> str:
"""The caseless form names are compared in: casefold output isn't always NFC, so normalize both sides."""
return unicodedata.normalize("NFC", unicodedata.normalize("NFC", name).casefold())
12 changes: 12 additions & 0 deletions tst/unit/artifact/environment_universe_folder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,15 @@ def test_its_problems_name_paths_in_the_folder():
"gmail: holds one file, and has 2 ('a.json', 'b.json')",
"slack: holds one file, and has 0",
]


def test_a_universe_registered_under_a_registry_id_names_its_environments_as_a_local_one_does():
files, folders = _listing([environment_path(PurePosixPath(), name, "x.json") for name in ("gmail", "g__mail")])

read = read_folder("psi-ts-shelfway-laptop-elicitation-computer-u", files, folders)

ids = {entry.name: (entry.environment_id, entry.file_id) for entry in read.environments}
assert ids["gmail"] == ("psi-ts-shelfway-laptop-elicitation-computer-u__gmail",
"psi-ts-shelfway-laptop-elicitation-computer-u__gmail__file")
digest = hashlib.sha256(b"g__mail").hexdigest()[:16]
assert ids["g__mail"][0] == f"psi-ts-shelfway-laptop-elicitation-computer-u__sha256__{digest}"
12 changes: 12 additions & 0 deletions tst/unit/bundle/register_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ def test_each_entry_takes_its_prefix_and_name_and_each_task_names_the_versions_i
assert Artifact.get("triage-docs").version == 1


def test_a_universe_folder_registers_its_environments_under_the_universe_s_registry_id(home):
universe = {"artifacts/world-u/artifact.toml": 'type = "environment_universe"\n',
"artifacts/world-u/gmail/gmail.zip": "mail\n", "artifacts/world-u/shelfway/shelfway.json": "{}\n",
"tasks/t.json": json.dumps([BOX, _load("world", "world-u")])}

registered = register_bundle(layout(home / "triage", universe))

assert {done.write.id for done in registered.materialization.writes} == {"triage-world-u", "triage-t"}
environments = Artifact.get("triage-world-u").get_environment_artifacts()
assert sorted(env.id for env in environments) == ["triage-world-u__gmail", "triage-world-u__shelfway"]


def test_registering_again_writes_nothing_and_an_edit_rewrites_only_what_it_reaches(home):
root = layout(home / "triage", TRIAGE)
register_bundle(root)
Expand Down
Loading