Status: preview. The crate
graphrt-gcpships the GCP auth module and the Cloud Run provider and deploy driver;graphrt gatewayuses them with--features gateway-cloud-run(kind: cloud_rununderproviders:) andgraphrt deploy --target cloud-rundeploys versions. See docs/gateway.md.
The gateway is the only always-on process. Agents (graphrt serve --trust-gateway) run as a Cloud Run service with min instances 0: Cloud
Run starts an instance when the gateway sends a request and scales back to
zero when no request is open.
Cargo features
Section titled “Cargo features”| Feature | Crate | What it adds | Measured Δ (min-size, macOS arm64) | Budget (plan §9) |
|---|---|---|---|---|
auth-gcp |
graphrt-gcp |
Metadata-server tokens, service-account JWT (RS256 with ring) → OAuth 2.0 token endpoint, ID tokens |
48.8 KiB | 60 KiB |
provider-cloud-run |
graphrt-gcp |
Run-time Cloud Run Provider (wake, list, invoker ID tokens). Implies auth-gcp. The gateway’s gw-cloudrun |
48.8 KiB on top of auth-gcp |
80 KiB |
deploy-cloud-run |
graphrt-gcp |
DeployDriver (Admin API v2 services.patch, operations, tag retirement) and Secret Manager writes. Implies provider-cloud-run. Deploy time only (graphrt deploy), never in the gateway build |
65.0 KiB on top of provider-cloud-run |
– |
The deltas are measured with the probe binaries in crates/graphrt-gcp/examples
(cargo build -p graphrt-gcp --profile min-size --features deploy-cloud-run --examples):
size_baseline links what the gateway has anyway (tokio, reqwest + rustls,
serde_json, ProviderHttp), and each next probe adds one feature’s calls.
No Google SDK: every call is hand-written REST over the reqwest + rustls
(ring) stack graphrt already links. cargo tree must never show
google-cloud-*, gcp_auth, yup-oauth2 or jsonwebtoken.
Credentials (auth-gcp)
Section titled “Credentials (auth-gcp)”GcpAuth::from_env() picks the source the way Google’s client libraries do:
GOOGLE_APPLICATION_CREDENTIALSnames a service-account key file. graphrt signs a JWT assertion (iss= the key’s e-mail,aud= itstoken_uri, lifetime one hour) with RS256 and exchanges it athttps://oauth2.googleapis.com/tokenfor an access token (scopecloud-platform), or, with atarget_audienceclaim, for an ID token.- Otherwise the metadata server (
GCE_METADATA_HOST, defaultmetadata.google.internal) of Compute Engine, Cloud Run or GKE (workload identity) hands out the attached service account’s tokens:…/service-accounts/default/tokenand…/identity?audience=…&format=full. The response must carryMetadata-Flavor: Google; the metadata client never usesHTTP(S)_PROXY.
Other credential types (authorized_user, external_account / workload
identity federation) are refused with a clear error for now.
Caching rules (conformance C8):
- Every token is cached until
exp − 5 min, measured on the local monotonic clock from when it was fetched (expires_in, orexp − iatof an ID token). A skewed wall clock cannot make a cached token look fresh. - Concurrent callers share one fetch per token (the access token, and one ID token per audience).
- After a 401 (Admin API) or 401/403 (Cloud Run invoke) the token is
refreshed once; a second rejection is an
Autherror. - A local clock ±5 min off is corrected once: when the token endpoint
rejects the assertion’s
iat/exp, graphrt reads the endpoint’sDateheader, remembers the offset and signs again. - Tokens are
SecretStrings (Debugprints***). No error message carries a token, an assertion or key material.
The provider
Section titled “The provider”| Capability | Value | Why |
|---|---|---|
| Addressing / scaler | Url / Platform |
Cloud Run routes and scales; maxInstanceRequestConcurrency = max_active_runs |
max_request_secs |
3600 (configurable lower) | Cloud Run request cap; the gateway preempts at cap − 60 s and re-dispatches |
max_grace_secs |
10 | SIGTERM → SIGKILL after 10 s, not configurable: drain always preempts |
background_cpu |
false |
request-based billing (cpuIdle = true) |
wake_p95_target_ms |
2000 | startup CPU boost on |
- wake resolves the version’s tag URL from
services.get(cached 60 s; never from the agent), then sends oneGET /healthzwithX-Serverless-Authorization: Bearer <ID token>. That request is what makes Cloud Run start an instance from zero. Twenty concurrent wakes share one probe (C1). A 429 isCapacity(max instances reached or no regional capacity) and is not retried; 5xx and network errors are retried with jittered backoff. - release is a no-op: Cloud Run scales in by itself once no request is open.
- list reports the single pseudo-replica
urlof a version (empty when the service or the version’s tag is gone). invoke_authorization(deployment)returns the header the gateway adds to every request it sends to the agent. Cloud Run validates the ID token and removes its signature before the container, so it never mixes with the agent’sGraphrt-SigHMAC.service_status(deployment).drift_warnings(min)reports settings changed by hand that cost money or slow wakes (minInstanceCount,cpuIdle,startupCpuBoost).
Billing caveat: the gateway holds every stream
Section titled “Billing caveat: the gateway holds every stream”With request-based billing an instance only has CPU while a request is open. graphrt never relies on CPU after a response: the gateway holds the stream of every run (sync, async, webhook, cron) for the run’s whole life, so Cloud Run bills each run as one open request and keeps its CPU. Consequences:
- A run costs request time for its full duration, including model wait time.
Many long, mostly-idle runs can be cheaper on instance-based billing; graphrt
still deploys
cpuIdle = truebecause idle deployments then cost nothing. - Nothing may run in the agent after its stream closes (no background flush); the gateway writes the store.
- Cloud Run caps one request at 60 minutes. Longer runs are preempted at
request_timeout_secs − 60 s(snapshot before the next node) and re-dispatched to a fresh request: one node may run twice per segment (decision 1, at-least-once per node withIdempotency-Key). - Shutdown grace is 10 s, too short to let a model call finish, so drain on SIGTERM always preempts.
Deploying (CloudRunDeployDriver)
Section titled “Deploying (CloudRunDeployDriver)”apply(spec) renders one Cloud Run Admin API v2 Service and sends
PATCH /v2/projects/{p}/locations/{r}/services/{id}?allowMissing=true
(create-or-update in one call), then polls the long-running operation and
checks that the new revision is the latest ready one.
| Spec field | Cloud Run |
|---|---|
name |
service id graphrt-<name> (DNS label, ≤ 40 chars) |
version |
revision <service>-<tag> and traffic tag <tag> (v7, ≤ 20 chars) |
image, command |
containers[0].image, command = first word, args = the rest; port 8080 (http1) |
env |
plain env values |
secrets |
valueSource.secretKeyRef {secret, version}; only projects/<p>/secrets/<s>[/versions/<v>] references are accepted |
max_active_runs |
maxInstanceRequestConcurrency (1–1000) |
min_replicas / max_replicas |
scaling.minInstanceCount (default 0) / maxInstanceCount |
resources |
limits.cpu (<1 vCPU needs max_active_runs = 1), limits.memory |
grace_secs |
ignored: Cloud Run’s grace is fixed at 10 s |
| always | cpuIdle = true, startupCpuBoost = true, timeout = request_timeout_secs, labels managed-by=graphrt, a custom audience graphrt/<p>/<r>/<service> |
provider_settings accepts exactly: service_account (runtime identity of the
agent), ingress (all default, internal, internal-and-cloud-load-balancing),
startup_cpu_boost (default true), execution_environment (gen1|gen2).
Unknown keys are an error. Endpoints are operator configuration
(CloudRunConfig), never spec settings.
Re-applying a version that is already the ready latest revision is a no-op. Versions are immutable: change the version to change the spec.
Old versions (decision 6)
Section titled “Old versions (decision 6)”A new version takes 100 % of new traffic under its tag; every older tag stays
on the service at 0 % and keeps its own URL (https://v6---…run.app), with
min instances 0. A run paused on version 6 is resumed on revision 6. After 7
days the gateway calls retire_versions(deployment, ["v6"]), which drops the
tag (updateMask=traffic); a later wake of v6 is NotFound, which the gateway
answers with 410 {error_type: version_retired}. The version serving new
traffic cannot be retired.
Secrets
Section titled “Secrets”SecretManager::store_secrets(prefix, values) (deploy time) creates each
secret with automatic replication when missing and adds the value as a new
version, returning pinned references (…/versions/3) for the spec. The value
goes only to Secret Manager; the Admin API, the spec and the gateway see the
reference (C10).
IAM roles
Section titled “IAM roles”| Who | Roles | For |
|---|---|---|
| Gateway (run time) | roles/run.viewer on the services (or project) |
services.get: tag URLs, readiness, drift |
| Gateway (run time) | roles/run.invoker on each service |
ID-token invocations (/healthz, runs) |
graphrt deploy / CI (deploy time) |
roles/run.developer |
services.patch (create/update), operations.get |
graphrt deploy / CI |
roles/iam.serviceAccountUser on the agent’s runtime service account |
deploying a revision that runs as it (actAs) |
graphrt deploy / CI |
roles/secretmanager.admin (or roles/secretmanager.secretVersionAdder on pre-created secrets) |
secrets.create, secrets.addVersion |
| Agent runtime service account | roles/secretmanager.secretAccessor on its secrets |
Cloud Run resolves secretKeyRef at instance start |
| Cloud Run service agent | roles/artifactregistry.reader (only for an image in another project) |
image pull |
| Operator, once | roles/run.admin (or run.services.setIamPolicy) |
granting the gateway run.invoker with gcloud run services add-iam-policy-binding |
The gateway never holds deploy-time roles and never sees a secret value.
Not yet verified on real GCP
Section titled “Not yet verified on real GCP”Cloud Run support is in preview. These need a real sandbox project (nightly) before the provider is marked stable (how the provider tests run is in CONTRIBUTING.md):
- Cloud Run accepts ID tokens whose
audis the custom audiencegraphrt/<p>/<r>/<service>on the service URL and on tag URLs. X-Serverless-Authorizationis accepted and its signature stripped before the container; the gateway’sGraphrt-Sigheader passes through untouched.services.patch?allowMissing=truecreates a missing service; the long-running operation’sresponseis the service;validateOnlyfor dry runs.- Revision names
<service>-<tag>are accepted, and a re-apply of the same version is a no-op; Cloud Run’s error when a version is re-applied with a different template. - Traffic: 0 %
REVISIONtargets keep their tags andtrafficStatuses[].uri;updateMask=trafficdrops a tag without a new revision. - C2 cold start p95 ≤ 2 s from zero with startup CPU boost (n ≥ 50, small image), and the warm-path overhead.
- C3 SIGTERM → SIGKILL in 10 s with preempt +
POST /internal/v1/orphansinside the grace (needs F0a drain + F0b protocol). - C6 SSE through the Cloud Run front end with chunks ≤ 1 KiB: no buffering, 15 s heartbeats keep the stream; C7 behaviour at the 60-min cap and the planned preempt at 3540 s.
- C9 429 when
maxInstanceCountis saturated (mapped toCapacity); Admin API quota under a wake storm; 403RESOURCE_EXHAUSTEDhandling. - Request-based billing: CPU throttled after the response; a held stream keeps the run’s CPU (billing report of a test run).
- C8 the token endpoint’s clock-skew error text and
Dateheader match the correction heuristic; metadataidentity?format=fullon Cloud Run, GCE and GKE workload identity. - Secret Manager:
addVersionnames versions with the project number (graphrt rewrites to the project id);secretKeyRefwith a pinned version resolves for the runtime service account. - C12 time from last request to zero instances (platform-controlled,
typically up to ~15 min), and that no graphrt-created resource outlives a
gateway
kill -9(none is created at run time). - Workload identity federation for CI (
external_accountcredentials) is not implemented yet; the nightly job needs it or a short-lived key.