"""03 DAYTONA SANDBOX. Run untrusted code inside a Daytona sandbox. Same task as 01 and 02, a different provider. Daytona spins up a remote dev environment on demand and tears it down when you're done with it. Run: python 03_daytona_sandbox.py "print('hello from daytona')" Needs: pip install daytona, and DAYTONA_API_KEY set in your environment (get a free key at app.daytona.io). Without a key, this script tells you what it WOULD do and exits 0. It never fakes output. """ import os import sys CODE = sys.argv[1] if len(sys.argv) > 1 else "print('hello from the sandbox')\nprint(2 + 2)" def main(): api_key = os.environ.get("DAYTONA_API_KEY") if not api_key: print("DAYTONA_API_KEY is not set. Here is what this script would do with a key:") print(" 1. Create a fresh Daytona sandbox (a real remote environment).") print(f" 2. Run this code inside it:\n {CODE!r}") print(" 3. Print the result captured from inside the sandbox.") print(" 4. Delete the sandbox. Nothing survives the run.") print("Set DAYTONA_API_KEY (free at app.daytona.io) and re-run this script to see it live.") sys.exit(0) try: from daytona import Daytona, DaytonaConfig except ImportError: print("daytona SDK is not installed. Run: pip install daytona") sys.exit(0) print("Creating a throwaway Daytona sandbox...") daytona = Daytona(DaytonaConfig(api_key=api_key)) sandbox = daytona.create() try: print(f"Running:\n{CODE}\n") response = sandbox.process.code_run(CODE) print(f"result: {response.result}") if getattr(response, "exit_code", 0) != 0: print(f"exit code: {response.exit_code}") finally: daytona.delete(sandbox) print("Sandbox deleted. Nothing left behind.") if __name__ == "__main__": main()