async def _get_or_create_base_snapshot(self):
        """Get or create a snapshot with the necessary dependencies and scripts for evaluation."""

        async with self._snapshot_lock:
            base_snapshots = await self.client.snapshots.alist(digest="ioi-evaluation-morph")

            if not base_snapshots:
                print("Creating base snapshot with build-essential cmake and g++")

                # Create base snapshot with minimal specs
                base_snapshot = await self.client.snapshots.acreate(
                    vcpus=2,
                    memory=4096,
                    disk_size=10240,
                    metadata={"purpose": "ioi_evaluation"},
                )

                # Start a temporary instance from the base snapshot
                temp_instance = await self.client.instances.astart(
                    base_snapshot.id, ttl_seconds=900
                )  # Auto-terminate after 15 minutes

                try:
                    # Wait for the instance to be ready
                    await temp_instance.await_until_ready(timeout=300)

                    # Get script contents
                    compile_script = await self._get_compile_script()
                    run_script = await self._get_run_script()

                    # Use temporary directory to store scripts
                    with tempfile.TemporaryDirectory(prefix="morph_setup_") as temp_dir:
                        # Create paths for script files
                        compile_path = os.path.join(temp_dir, "compile.sh")
                        run_path = os.path.join(temp_dir, "run.sh")

                        # Write scripts to temp files
                        with open(compile_path, "w") as f:
                            f.write(compile_script)

                        with open(run_path, "w") as f:
                            f.write(run_script)

                        async with temp_instance:
                            # Install dependencies
                            await temp_instance.aexec("apt-get update && apt-get install -y build-essential cmake g++")

                            # Create workspace directory
                            await temp_instance.aexec(
                                "mkdir -p /workspace && mkdir -p /workspace/graders && chmod 777 /workspace"
                            )

                            # Upload scripts to instance
                            await temp_instance.aupload(compile_path, "/workspace/compile")
                            await temp_instance.aupload(run_path, "/workspace/run")

                            # Make scripts executable
                            await temp_instance.aexec("chmod +x /workspace/compile /workspace/run")

                            # Create snapshot from the prepared instance
                            final_snapshot = await temp_instance.asnapshot(digest="ioi-evaluation-morph")

                except Exception as e:
                    # Ensure instance is stopped if anything fails
                    await temp_instance.astop()
                    raise e
            else:
                final_snapshot = base_snapshots[0]

            return final_snapshot
Get or create a snapshot with the necessary dependencies and scripts for evaluation.
src/open_r1/utils/competitive_programming/morph_client.py