Skip to content
Open
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
89 changes: 46 additions & 43 deletions src/agent_env/env/envs/computer/utils/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,7 @@ def _composed_vm_bundle(env: ComputerEnv, base: ComputerArtifact, driver: Comput
packages.stage_wheels(lock, build, env.stored_id)
parts = [replace(part, wheels=tuple(wheel.file for wheel in lock.wheels.get(part.folder, [])))
for part in parts]
# A VM bundle's base bakes none in, as a Linux base's image does its defaults, so each value is the env's or the
# setting's default.
values = env.computer_values()
settings = {name: values.get(name, setting.default) for name, setting in base.config.items()
if setting.phase == "build"}
settings = _build_settings_with_defaults(env, base)
script = macos_vm_bundle.write(build, base, parts, link.ssh_password, settings)
key = attempt.key = _key(base=base.forms.vm_bundle.image, provision=script, context=_digest(context),
agents=_digest(build / "agents"), prepare=_digest(macos_vm_bundle.PREPARE),
Expand Down Expand Up @@ -368,6 +364,42 @@ def _composed_vm_bundle(env: ComputerEnv, base: ComputerArtifact, driver: Comput
return probed
return attempt.result

def _build_settings_with_defaults(env: ComputerEnv, base: ComputerArtifact) -> dict[str, Any]:
"""The base's build-phase settings, the env's value or else the default: a disk or VM bundle base bakes none in,
as a Linux base's image does its defaults."""
values = env.computer_values()
return {name: values.get(name, setting.default) for name, setting in base.config.items()
if setting.phase == "build"}


def _require_disk_link(env: ComputerEnv, provider: SandboxProvider, arch: str, os_type: str) -> None:
"""Refuse a disk compose that no link of ``provider`` can boot, before any work."""
if not boot.native_links(provider, arch, "disk", env.stored_id, os_type):
raise builder.Unbuildable(f"no link of {' or '.join(type(link).__name__ for link in provider.links)} boots "
f"{os_type}/{arch} disks, so it can't be composed here: compose it on a Linux "
"machine with KVM, with the local sandbox provider")


def _reuse_or_compose_disk(env: ComputerEnv, driver: ComputerDriverEnv, arch: str, provider: SandboxProvider,
progress: _Progress, *, guest: thin_disk.Guest, lock: str, key: str, made: thin_disk.Base,
build: Path, registry: thin_disk.Registry, say: Callable[[str], None],
labels: dict[str, str] | None = None, extra: dict[str, Any] | None = None) -> dict:
"""The thin disk of ``key`` on ``made``: the store's, reused, else composed on this machine's Docker and pushed
under it, with ``labels``; then probed at full (gate 3), its result carrying ``extra``. It holds a lock of its key,
so a second compose of one key on this machine waits for the first, then reuses it."""
pair, extra = f"disk/{arch}", extra or {}
with holding_locks([f"{lock}:{key}"], lambda: progress.at(
pair, "waiting", "waiting for the compose of the same disk under way on this machine")):
if (pinned := registry.served(thin_disk.disk_names(guest, key).tag)) is not None:
progress.at(pair, "reusing", "reusing the disk of the same key, composed earlier")
result = {"status": "unverified", "key": key, "image": pinned, "reused": True, **extra, "built_at": now()}
else:
progress.at(pair, "composing", "composing on this machine's Docker")
pinned = thin_disk.compose(guest, made, build, key, {KEY_LABEL: key, **(labels or {})}, say, registry)
result = {"status": "unverified", "key": key, "image": pinned, "reused": False,
"builder": "LocalSandboxProvider", **extra, "built_at": now()}
return _probed(env, driver, "disk", arch, result, provider, progress)


def _composed_windows_disk(env: ComputerEnv, base: ComputerArtifact, driver: ComputerDriverEnv, arch: str,
provider: SandboxProvider, progress: _Progress) -> dict:
Expand All @@ -379,10 +411,7 @@ def _composed_windows_disk(env: ComputerEnv, base: ComputerArtifact, driver: Com
pair = f"disk/{arch}"
with (_attempting(env, "disk", arch) as attempt,
tempfile.TemporaryDirectory(prefix="agentenv-compose-") as work):
if not boot.native_links(provider, arch, "disk", env.stored_id, "windows"):
raise builder.Unbuildable(f"no link of {' or '.join(type(link).__name__ for link in provider.links)} boots "
f"windows/{arch} disks, so it can't be composed here: compose it on a Linux machine "
"with KVM, with the local sandbox provider")
_require_disk_link(env, provider, arch, "windows")
build = Path(work) / "build"
context = build / "context"
context.mkdir(parents=True)
Expand All @@ -401,9 +430,7 @@ def _composed_windows_disk(env: ComputerEnv, base: ComputerArtifact, driver: Com
commands = [server, *(windows_disk.argv(service.command, prefix) for service in staged.install.services)]
parts.append(_windows_part(staged, prefix, dict(zip(gate.programs(app, platform_of(base.os)), commands)),
{"ENVIRONMENT_NAME": app.environment_name}, protocol, context))
values = env.computer_values()
settings = {name: values.get(name, setting.default) for name, setting in base.config.items()
if setting.phase == "build"}
settings = _build_settings_with_defaults(env, base)

def say(line: str) -> None:
progress.say(f"{pair}: {line}")
Expand All @@ -420,19 +447,9 @@ def say(line: str) -> None:
key = attempt.key = _key(base=made.image, base_expires=expires, provision=script, context=_digest(context),
build_settings=settings, form="disk", arch=arch, format=FORMAT)
attempt.stage = "build"
with holding_locks([f"windows-disk:{key}"], lambda: progress.at(
pair, "waiting", "waiting for the compose of the same disk under way on this machine")):
if (pinned := registry.served(thin_disk.disk_names(windows_disk.GUEST, key).tag)) is not None:
progress.at(pair, "reusing", "reusing the disk of the same key, composed earlier")
result = {"status": "unverified", "key": key, "image": pinned, "reused": True, "expires": expires,
"built_at": now()}
else:
progress.at(pair, "composing", "composing on this machine's Docker")
pinned = thin_disk.compose(windows_disk.GUEST, made, build, key,
{KEY_LABEL: key, windows_disk.EXPIRES_LABEL: expires}, say, registry)
result = {"status": "unverified", "key": key, "image": pinned, "reused": False,
"builder": "LocalSandboxProvider", "expires": expires, "built_at": now()}
return _probed(env, driver, "disk", arch, result, provider, progress)
return _reuse_or_compose_disk(env, driver, arch, provider, progress, guest=windows_disk.GUEST,
lock="windows-disk", key=key, made=made, build=build, registry=registry, say=say,
labels={windows_disk.EXPIRES_LABEL: expires}, extra={"expires": expires})
return attempt.result


Expand Down Expand Up @@ -523,10 +540,7 @@ def _composed_omarchy_disk(env: ComputerEnv, base: ComputerArtifact, driver: Com
pair = f"disk/{arch}"
with (_attempting(env, "disk", arch) as attempt,
tempfile.TemporaryDirectory(prefix="agentenv-compose-") as work):
if not boot.native_links(provider, arch, "disk", env.stored_id, base.os.type):
raise builder.Unbuildable(f"no link of {' or '.join(type(link).__name__ for link in provider.links)} boots "
f"linux/{arch} disks, so it can't be composed here: compose it on a Linux machine "
"with KVM, with the local sandbox provider")
_require_disk_link(env, provider, arch, base.os.type)
build = Path(work) / "build"
context = build / "context"
context.mkdir(parents=True)
Expand All @@ -547,9 +561,7 @@ def _composed_omarchy_disk(env: ComputerEnv, base: ComputerArtifact, driver: Com
**({PLATFORM_ENV: staged.platform} if staged.platform else {})}
parts.append(_omarchy_part(staged, prefix, dict(zip(gate.programs(app, platform_of(base.os)), commands)),
environment, protocol, context))
values = env.computer_values()
settings = {name: values.get(name, setting.default) for name, setting in base.config.items()
if setting.phase == "build"}
settings = _build_settings_with_defaults(env, base)

def say(line: str) -> None:
progress.say(f"{pair}: {line}")
Expand All @@ -566,17 +578,8 @@ def say(line: str) -> None:
key = attempt.key = _key(base=made.image, provision=script, context=_digest(context), build_settings=settings,
form="disk", arch=arch, format=FORMAT)
attempt.stage = "build"
with holding_locks([f"omarchy-disk:{key}"], lambda: progress.at(
pair, "waiting", "waiting for the compose of the same disk under way on this machine")):
if (pinned := registry.served(thin_disk.disk_names(omarchy_disk.GUEST, key).tag)) is not None:
progress.at(pair, "reusing", "reusing the disk of the same key, composed earlier")
result = {"status": "unverified", "key": key, "image": pinned, "reused": True, "built_at": now()}
else:
progress.at(pair, "composing", "composing on this machine's Docker")
pinned = thin_disk.compose(omarchy_disk.GUEST, made, build, key, {KEY_LABEL: key}, say, registry)
result = {"status": "unverified", "key": key, "image": pinned, "reused": False,
"builder": "LocalSandboxProvider", "built_at": now()}
return _probed(env, driver, "disk", arch, result, provider, progress)
return _reuse_or_compose_disk(env, driver, arch, provider, progress, guest=omarchy_disk.GUEST,
lock="omarchy-disk", key=key, made=made, build=build, registry=registry, say=say)
return attempt.result


Expand Down
Loading