Skip to content

The agent that grows with you

fix(pm): resolve the image's sealed payload from its on-tree manifest

Mert Kayab74a29f0166e
diff --git a/pm/environments.py b/pm/environments.py
index 231ae77c1b7..8e436d5f99b 100644
--- a/pm/environments.py
+++ b/pm/environments.py
@@ -147,31 +147,38 @@ def record_activation_inputs(stamps: Path, mtimes: dict[str, int], project_root:
os.utime(stamp, ns=(mtime, mtime))
-def _payload_manifest(root: Path) -> dict | None:
- """The manifest of the sealed payload whose tree is the resolved *root*, or ``None``.
+def _payload_manifest(root: Path) -> tuple[dict, Path] | None:
+ """The manifest of the sealed payload whose tree is the resolved *root*, with the
+ payload directory its entries resolve from, or ``None``.
+ The manifest sits at the payload root: beside the tree, or on the tree itself when
+ the tree IS the payload root (the Docker image's in-place ``repo: "."`` assembly).
Every install asks, so a ``manifest.json`` that some other tool left beside a
checkout, or one with a mistyped field, must read as "not a payload", never as an
error. Readers then take each field with ``.get`` and treat an absent one as unset.
"""
- try:
- manifest = json.loads((root.parent / "manifest.json").read_text(encoding="utf-8-sig"))
- except (OSError, ValueError):
- return None
- if not isinstance(manifest, dict) or not isinstance(manifest.get("repo"), str):
- return None
- runtime = manifest.get("runtime", {})
- commands = runtime.get("commands", {}) if isinstance(runtime, dict) else None
- well_formed = (isinstance(commands, dict)
- and isinstance(commands.get("hermes", ""), str)
- and all(isinstance(manifest.get(key, ""), str) for key in ("venv", "store")))
- return manifest if well_formed and (root.parent / manifest["repo"]).resolve() == root else None
-
-
-def _payload_path(root: Path, relative: str, what: str) -> Path:
- """*relative* resolved inside the payload that holds *root*; a manifest may not point outside it."""
- path = (root.parent / relative).resolve()
- if not path.is_relative_to(root.parent):
+ for path in (root.parent / "manifest.json", root / "manifest.json"):
+ try:
+ manifest = json.loads(path.read_text(encoding="utf-8-sig"))
+ except (OSError, ValueError):
+ continue
+ if not isinstance(manifest, dict) or not isinstance(manifest.get("repo"), str):
+ continue
+ runtime = manifest.get("runtime", {})
+ commands = runtime.get("commands", {}) if isinstance(runtime, dict) else None
+ well_formed = (isinstance(commands, dict)
+ and isinstance(commands.get("hermes", ""), str)
+ and all(isinstance(manifest.get(key, ""), str) for key in ("venv", "store")))
+ if well_formed and (path.parent / manifest["repo"]).resolve() == root:
+ return manifest, path.parent
+ return None
+
+
+def _payload_path(payload_root: Path, relative: str, what: str) -> Path:
+ """*relative* resolved from the payload root its manifest was read from; a manifest
+ may not point outside it."""
+ path = (payload_root / relative).resolve()
+ if not path.is_relative_to(payload_root):
raise RuntimeError(f"payload {what} escapes its root")
return path
@@ -179,13 +186,15 @@ def _payload_path(root: Path, relative: str, what: str) -> Path:
def payload_venv(project_root: Path) -> Path | None:
"""The environment a sealed payload ships beside its tree, or ``None``."""
root = Path(project_root).resolve()
- venv = (_payload_manifest(root) or {}).get("venv")
- return _payload_path(root, venv, "environment") if venv else None
+ payload = _payload_manifest(root)
+ venv = payload[0].get("venv") if payload else None
+ return _payload_path(payload[1], venv, "environment") if venv else None
def _payload_hermes_command(root: Path) -> Path | None:
- command = (_payload_manifest(root) or {}).get("runtime", {}).get("commands", {}).get("hermes")
- return _payload_path(root, command, "launcher") if command else None
+ payload = _payload_manifest(root)
+ command = payload[0].get("runtime", {}).get("commands", {}).get("hermes") if payload else None
+ return _payload_path(payload[1], command, "launcher") if command else None
def payload_command_dir(project_root: Path) -> Path | None:
@@ -222,9 +231,10 @@ def store_root(project_root: Path, *, honor_runtime_override: bool = True) -> Pa
if override:
return Path(override).resolve()
root = Path(project_root).resolve()
- store = (_payload_manifest(root) or {}).get("store")
+ payload = _payload_manifest(root)
+ store = payload[0].get("store") if payload else None
if store:
- return _payload_path(root, store, "store")
+ return _payload_path(payload[1], store, "store")
from pm.paths import install_stamp_path
for directory in (root, *root.parents):
@@ -340,13 +350,13 @@ def project_python(project_root: Path) -> Path:
def _payload_store_python(root: Path) -> Path | None:
"""A sealed payload's own interpreter when a venv must not be entered through its own."""
- manifest = _payload_manifest(root)
- if manifest is None or str(manifest.get("target", "")).endswith("-bionic"):
+ payload = _payload_manifest(root)
+ if payload is None or str(payload[0].get("target", "")).endswith("-bionic"):
return None # Termux payloads launch through their venv interpreter by design.
- relative = manifest.get("runtime", {}).get("storePython")
+ relative = payload[0].get("runtime", {}).get("storePython")
if not isinstance(relative, str) or not relative:
return None
- return _payload_path(root, relative, "interpreter")
+ return _payload_path(payload[1], relative, "interpreter")
def venv_command(project_root: Path, venv: Path, options: tuple[str, ...] | list[str] = ()) -> list[str]:
diff --git a/tests/scripts/test_agent_build.py b/tests/scripts/test_agent_build.py
index 6b626d5c1ba..2240460283d 100644
--- a/tests/scripts/test_agent_build.py
+++ b/tests/scripts/test_agent_build.py
@@ -177,6 +177,31 @@ def test_fixed_root_keeps_privilege_shim_and_resolves_venv_command_symlink(tmp_p
assert json.loads(run.stdout)[0] == ["ok"]
+def test_fixed_layout_manifest_makes_the_tree_a_resolvable_payload(tmp_path):
+ """Regression for #138990: the image's ``repo: "."`` assembly must resolve like a payload.
+
+ The Dockerfile assembles /opt/hermes in place, so the manifest lands on the tree
+ itself. A store-Python child boots through committed_venv(), which resolves the
+ sealed venv from that manifest; without it every runtime_command child died with
+ "no dependency environment is committed for this install".
+ """
+ out, data = inputs_fixture(tmp_path)
+ shutil.copytree(data["code"], out, dirs_exist_ok=True)
+ data.update(placement="fixed", repo=".", code=str(out), project=str(out / "pyproject.toml"), bin_dir="libexec", python=sys.executable)
+ assert build_cli(data, out, tmp_path).returncode == 0
+ from pm.environments import committed_venv, payload_venv, store_root
+
+ assert payload_venv(out) == out / "venv"
+ assert committed_venv(out) == out / "venv"
+ assert store_root(out, honor_runtime_override=False) == out / "tools"
+ # A manifest naming another tree never claims this one.
+ manifest = json.loads((out / "manifest.json").read_text())
+ manifest["repo"] = "elsewhere"
+ (out / "manifest.json").write_text(json.dumps(manifest), encoding="utf-8")
+ assert payload_venv(out) is None
+
+
@pytest.mark.platforms("posix")
def test_source_metadata_keeps_declared_requirements_extras_and_entrypoints(tmp_path):
from importlib.metadata import distributions