Evolve compiled code with AlphaEvolve on Cloud Run

1. Introduction

AlphaEvolve scores every candidate program by running it. That's easy when the code is Python you can exec() locally — but what about Rust, C++, or anything that has to be compiled first? The answer is a remote evaluator: a container that compiles and benchmarks each candidate and returns a score over HTTP.

In this codelab, you evolve a Rust sorting algorithm that adapts to different data patterns. You deploy a small evaluator to Cloud Run, point AlphaEvolve at it, and watch Gemini rewrite the sort while a real cargo build + benchmark decides which versions survive.

AlphaEvolve remote evaluation: the AlphaEvolve agent, on Gemini Enterprise in Google Cloud, generates candidates; your machine's experiment loop (run_experiment.py) acquires them and submits scores back, while its evaluator.py client POSTs each candidate to the rust-evaluator on Cloud Run, which runs cargo build plus a benchmark and returns JSON metrics.

What you'll do

  • Deploy a Rust-compiling evaluator to Cloud Run from source
  • Point AlphaEvolve at the evaluator with one environment variable
  • Run an evolution that compiles and benchmarks every candidate remotely
  • Read a composite score that blends correctness, speed, and adaptability

What you'll need

  • A web browser such as Chrome
  • A Google Cloud project with billing enabled
  • Python 3.9 or later and uv
  • The gcloud CLI, authenticated
  • No local Rust toolchain — compilation happens inside the evaluator

This codelab is for developers who have run a local AlphaEvolve experiment (see the Get started with AlphaEvolve codelab) or are comfortable with Cloud Run.

Estimated time to complete: 60–75 minutes.

Cost: Cloud Run scales to zero and bills per request.

Tested with: the AlphaEvolve Cloud client library v0.1.0, Python 3.9+, on the adaptive_sort example.

2. Before you begin

Select your project and enable APIs

  1. In the Google Cloud Console, select or create a project with billing enabled, then set it in your terminal:
gcloud config set project <YOUR_PROJECT_ID>
  1. Enable the four APIs this codelab uses — AlphaEvolve (Discovery Engine), Vertex AI, Cloud Run, and Cloud Build:
gcloud services enable \
  discoveryengine.googleapis.com \
  aiplatform.googleapis.com \
  run.googleapis.com \
  cloudbuild.googleapis.com

Get your Gemini Enterprise app ID

AlphaEvolve is served through Gemini Enterprise (the REST surface is the Discovery Engine API). You need your app's ID for GE_APP_ID.

  1. Open the Gemini Enterprise apps page.
  2. Click your app and copy the ID (for example, gemini-enterprise-1234567890_1234567890123).

Clone the repo and install the client

  1. Authenticate for application-default credentials:
gcloud auth application-default login
  1. Clone the samples repo and install the AlphaEvolve client library from the repo root:
git clone https://github.com/Google-Cloud-AI/alphaevolve-on-googlecloud.git
cd alphaevolve-on-googlecloud
uv venv
uv pip install -e ".[dev]"

3. Understand remote evaluation

In the Get started codelab, the evaluator ran candidate Python in an exec() sandbox on your machine. Rust can't work that way — it has to be compiled. So the evaluator moves off your machine and becomes a service.

Local evaluation

Remote evaluation (this codelab)

Where candidates run

your machine, exec()

a container on Cloud Run

Languages

Python

anything you can compile and run in a container (Rust, C++, ...)

One-time setup

none

deploy the evaluator once

Cost

~free (API only)

Cloud Run per request + one build

The pattern is the same everywhere: the client (evaluator.py) serializes the candidate's files, POSTs them to the evaluator, and gets JSON metrics back. Only the where changes.

The seed program and the EVOLVE-BLOCK

Open examples/adaptive_sort/src/lib.rs. The adaptive_sort entry point is inside the EVOLVE-BLOCK markers, and so are the sorting helpers in src/helpers.rs (quicksort, partition, and friends) — those are the parts Gemini can rewrite. The benchmark harness and tests stay frozen.

// EVOLVE-BLOCK-START
// Initial implementation: Simple quicksort.
// This can evolve into hybrid algorithms (introsort, timsort-like),
// adaptive pivot selection, or special handling for nearly-sorted data.
pub fn adaptive_sort<T: Ord + Clone>(arr: &mut [T]) {
    if arr.len() <= 1 {
        return;
    }
    // Base implementation: quicksort from helpers.rs
    quicksort(arr, 0, arr.len() - 1);
}
// EVOLVE-BLOCK-END

run_benchmark() and the tests live outside the block, so every candidate is measured the same way. Gemini can rewrite the sort however it likes — it just can't touch how it's scored.

The score

The evaluator compiles the code (cargo build --release), runs a benchmark harness, and returns these metrics:

Metric

Meaning

score

Primary. 0.6 · performance_score + 0.4 · adaptability_score — but only if correctness == 1.0, otherwise 0. Higher is better.

correctness

1.0 only if every test input is sorted correctly, else 0.0 (the gate)

performance_score

how fast the sort runs

adaptability_score

consistency across data patterns (lower time variance scores higher)

compile_success

1.0 if the Rust compiled, else 0.0

The correctness gate matters: a blazing-fast sort that returns wrong output scores 0. Gemini has to stay correct and get faster.

4. Deploy the evaluator to Cloud Run

The evaluator lives in examples/adaptive_sort/cloud_function/ — a small Python service (functions-framework) that shells out to cargo. Deploy it straight from source; Cloud Build compiles the container and Cloud Run hosts it.

gcloud run deploy rust-evaluator \
  --source ./examples/adaptive_sort/cloud_function \
  --memory 1Gi \
  --region us-central1 \
  --allow-unauthenticated

The --memory 1Gi flag gives cargo room to compile. The first deploy takes a few minutes while Cloud Build builds the image.

Note: First-time deploy permission error? gcloud run deploy --source builds via Cloud Build using your project's default compute service account, which on some projects can't read the Cloud Run source bucket — the deploy fails with ...-compute@developer.gserviceaccount.com does not have storage.objects.get access to ... run-sources-.... Grant it read access and retry:

PROJECT_NUMBER=$(gcloud projects describe <YOUR_PROJECT_ID> --format='value(projectNumber)')
gcloud projects add-iam-policy-binding <YOUR_PROJECT_ID> \
  --member="serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com" \
  --role="roles/storage.objectViewer"

You should see output ending with:

Service [rust-evaluator] revision [rust-evaluator-00001-abc] has been deployed
and is serving 100 percent of traffic.
Service URL: https://rust-evaluator-xxxxxxxxxx-uc.a.run.app

Note: --allow-unauthenticated keeps this codelab simple. If your organization blocks public services, omit the flag and grant your user roles/run.invoker on the service instead — the client already sends an OIDC token when the URL isn't localhost.

5. Run the evolution

Configure the client

The client reads a .env from the repo root. Create one with your project and app ID:

# alphaevolve-on-googlecloud/.env
PROJECT_ID=<YOUR_PROJECT_ID>
LOCATION=global
COLLECTION=default_collection
GE_APP_ID=<YOUR_GEMINI_ENTERPRISE_APP_ID>
ASSISTANT=default_assistant
BASE_URL=discoveryengine.googleapis.com

Then append the evaluator's URL straight from Cloud Run — no copy-paste:

echo "EVALUATOR_URL=$(gcloud run services describe rust-evaluator \
  --region us-central1 --format 'value(status.url)')" >> .env

Start the experiment

uv run python -m examples.adaptive_sort.run_experiment

This uploads the seed (src/lib.rs + src/helpers.rs), then evolves 10 candidates with 4 running concurrently (set in run_experiment.py). Each candidate is compiled and benchmarked on Cloud Run, so expect a few minutes of wall-clock time.

You should see output similar to:

INFO:root:Creating experiment 'Adaptive Sort'...
INFO:alpha_evolve.controller:Evaluated program -> score=0.00 compile_success=1.0 correctness=1.0
INFO:alpha_evolve.controller:Evaluated program -> score=0.68 compile_success=1.0 correctness=1.0

Top Programs:
Rank 1: .../programs/abc123 | Score: 0.71
Rank 2: .../programs/def456 | Score: 0.66

Note: Troubleshooting: a candidate that returns compile_success=0.0 didn't compile — the evaluator sends the cargo error back as an insight, which Gemini uses to fix the next generation. That's expected, not a failure of your setup. A client-side URL Error usually means EVALUATOR_URL is wrong or the service requires auth.

6. Read the results

The run ranks programs by score (higher is better) and prints the top three. Two things to look for:

  • Correct first, then fast. Early candidates often score 0 because they broke correctness or didn't compile. Once Gemini locks in a correct sort, score climbs as performance_score and adaptability_score improve.
  • What Gemini discovers. Starting from plain quicksort, the search tends toward hybrid strategies — median-of-three pivots, insertion sort for small slices, or detecting nearly-sorted input — the same moves a human would reach for, found by measurement rather than intuition.

You now have the remote-evaluation pattern: a containerized evaluator, a client that POSTs candidates, and a score that comes back over HTTP. Swap the container and you can evolve almost anything.

7. Clean up

Cloud Run scales to zero, so an idle service costs nothing — but delete it to be tidy and to stop any storage/build charges.

  1. Delete the evaluator service:
gcloud run services delete rust-evaluator --region us-central1
  1. If you created a project just for this codelab, delete it in the Console to remove everything at once.

8. Congratulations

You evolved a compiled Rust algorithm with AlphaEvolve, scored by a real cargo build + benchmark running on Cloud Run. The evaluator did the compiling, so your client stayed a thin HTTP caller.

What you've learned

  • When to move from local to remote evaluation
  • How to deploy a containerized evaluator to Cloud Run from source
  • How AlphaEvolve talks to a remote evaluator through EVALUATOR_URL
  • How a correctness-gated composite score keeps the search honest

Other codelabs

These are standalone — do them in any order.

  • Get started with AlphaEvolve: the local-evaluation basics (circle packing + TSP).
  • Evolve LLM fine-tuning on GKE + Ray: remote evaluation scaled up to parallel GPU training.

Reference docs