m/market labResearch, with a record.
Source on GitHub ↗

probe_agent_runtimes.py

1"""Exercise independent Python preparation and a version-constrained offline dependency."""23import argparse4import json5from pathlib import Path6import subprocess7import sys8import zipfile910ROOT = Path(__file__).resolve().parents[1]11sys.path.insert(0, str(ROOT))12from sim_platform import Platform131415def write_probe(root, python):16    source = root / 'probe-source'17    source.mkdir(exist_ok=True)18    (source / 'agent.py').write_text(19        'type Prices = list[float]\nraise RuntimeError("do not execute source")\ndef create_agent(context): return None\n'20    )21    (source / 'agent.json').write_text(json.dumps(dict(python=python)))22    (source / 'requirements.txt').write_text('runtime_probe_dependency==1.0\n')23    wheels = root / 'probe-wheels'24    wheels.mkdir(exist_ok=True)25    dist = 'runtime_probe_dependency-1.0.dist-info'26    files = {27        'runtime_probe_dependency.py': 'raise RuntimeError("do not import dependency")\n',28        dist29        + '/METADATA': 'Metadata-Version: 2.1\nName: runtime_probe_dependency\nVersion: 1.0\nRequires-Python: >=3.12\n',30        dist + '/WHEEL': 'Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n',31    }32    files[dist + '/RECORD'] = ''.join(name + ',,\n' for name in [*files, dist + '/RECORD'])33    with zipfile.ZipFile(wheels / 'runtime_probe_dependency-1.0-py3-none-any.whl', 'w') as wheel:34        for name, text in files.items():35            wheel.writestr(name, text)36    return source, wheels373839def main():40    parser = argparse.ArgumentParser()41    parser.add_argument('--directory', type=Path, required=True)42    parser.add_argument('--python', default='3.12')43    parser.add_argument('--download', action='store_true')44    parser.add_argument('--archive', type=Path)45    options = parser.parse_args()46    root = options.directory.resolve()47    root.mkdir(parents=True, exist_ok=True)48    source, wheels = write_probe(root, options.python)49    with Platform(root / 'app') as app:50        profile = app.create_profile('Runtime probe')51        saved = app.import_agent(profile, source)52        job = app.prepare_agent_environment(53            profile,54            saved['agent_id'],55            wheelhouse=wheels,56            allow_python_download=options.download,57            python_archive=options.archive,58        )59        try:60            status = job.wait(300)61            if status['state'] != 'ready':62                raise RuntimeError(str(status))63        finally:64            job.close()65        environment = status['environment']66        executable = (67            root68            / 'app'69            / 'agents'70            / 'environments'71            / environment['environment_ref']72            / 'python'73            / 'bin'74            / 'python'75        )76        actual = subprocess.check_output(77            [str(executable), '-I', '-S', '--version'], text=True78        ).strip()79        second = app.prepare_agent_environment(profile, saved['agent_id'], wheelhouse=wheels)80        try:81            reused = second.wait(30)82            assert reused['state'] == 'ready' and reused['reused'], reused83        finally:84            second.close()85        print(86            json.dumps(87                dict(88                    app_python=sys.version.split()[0],89                    runtime=status['runtime'],90                    actual_environment=actual,91                    reused=reused['reused'],92                    packages=environment['packages'],93                ),94                indent=2,95            )96        )979899if __name__ == '__main__':100    main()