"""A fresh interpreter waiting for one kernel identity, never reused.""" from __future__ import annotations import importlib import io import json import os import sys import albedo_bundle import albedo_cells import albedo_proc # Import trusted packaged modules only; setup needs the adopting session's host. PREIMPORT = ( "run work mail agents paperclips artifacts files memory commands remote browser web_search skills hook" ).split() def main() -> None: for name in PREIMPORT: importlib.import_module("albedo_plugins." + name) bundle = albedo_bundle.digest() print( json.dumps( { "ready": bundle, "pid": os.getpid(), "pgid": os.getpgrp(), "leader": albedo_proc.leader_token(os.getpid()), } ), flush=True, ) line = sys.stdin.buffer.readline(1024 * 1024 + 1) if len(line) > 1024 * 1024 or not line.endswith(b"\n"): raise ValueError("invalid spare adoption frame") spec = json.loads(line) if bundle != albedo_bundle.digest(): raise ValueError("spare bundle changed before adoption") if not isinstance(spec, dict) or not all( isinstance(spec.get(key), str) for key in ("cwd", "run_dir", "modules", "token") ): raise ValueError("invalid spare adoption identity") environment = spec.get("environment") if not isinstance(environment, dict) or not all( isinstance(key, str) and isinstance(value, str) for key, value in environment.items() ): raise ValueError("invalid spare adoption environment") os.chdir(spec["cwd"]) os.environ.clear() os.environ.update(environment) # The ordinary detached handshake still reads the secret from stdin. sys.stdin = io.TextIOWrapper(io.BytesIO((spec["token"] + "\n").encode())) sys.argv = [sys.argv[0], "--run", spec["run_dir"], spec["modules"]] albedo_cells.main() if __name__ == "__main__": main()