Skip to main content

graphrt_provider_api/
types.rs

1//! Data types shared by every provider (plan §6).
2
3use std::collections::BTreeMap;
4use std::path::PathBuf;
5
6use serde::{Deserialize, Serialize};
7
8/// How the gateway reaches replicas of a deployment.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum Addressing {
12    /// The gateway holds one address per replica and picks the replica
13    /// (local-process, Kubernetes, ECS Fargate, Fly).
14    Replica,
15    /// One URL per deployment; the platform routes and scales
16    /// (Cloud Run, Azure Container Apps, Lambda). `release` is a no-op.
17    Url,
18}
19
20/// Who decides the number of replicas.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
22#[serde(rename_all = "snake_case")]
23pub enum Scaler {
24    /// The gateway scales 0→N (least-loaded) and releases idle replicas.
25    Gateway,
26    /// The platform scales on incoming requests.
27    Platform,
28}
29
30/// Static facts about a provider the gateway plans around (drain policy,
31/// planned preemption before a request cap, wake timeouts).
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33pub struct Capabilities {
34    /// Per-replica addresses or one URL.
35    pub addressing: Addressing,
36    /// Who scales.
37    pub scaler: Scaler,
38    /// Hard cap on one request / SSE stream (Cloud Run 3600, Lambda 900).
39    /// `None` = unbounded. The gateway preempts at `cap - 60 s`.
40    pub max_request_secs: Option<u64>,
41    /// Ingress idle timeout the 15 s heartbeat must beat (ACA 240). `None` = none.
42    pub idle_timeout_secs: Option<u64>,
43    /// Time between SIGTERM and SIGKILL (Cloud Run 10, ECS 120). `0` means
44    /// the platform gives no grace at all (Lambda): no planned preemption is
45    /// possible, the gateway holds the stream and a run past
46    /// `max_request_secs` fails explicitly with
47    /// [`crate::ProviderErrorKind::PlatformTimeout`] (`failed{platform_timeout}`).
48    pub max_grace_secs: u64,
49    /// Whether the replica keeps CPU between requests (false on Lambda).
50    pub background_cpu: bool,
51    /// Whether `ReleaseMode::Suspend` is meaningful (Fly).
52    pub supports_suspend: bool,
53    /// Wake p95 target from plan §5, checked by conformance case C2.
54    pub wake_p95_target_ms: u64,
55}
56
57impl Capabilities {
58    /// Whether the gateway can preempt a run before a platform limit
59    /// (`max_grace_secs > 0`).
60    pub fn can_preempt(&self) -> bool {
61        self.max_grace_secs > 0
62    }
63}
64
65/// How a replica is given back.
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
67#[serde(rename_all = "snake_case")]
68pub enum ReleaseMode {
69    /// Freeze memory, keep the machine (Fly suspend). Providers without
70    /// suspend fall back to `Stop`.
71    Suspend,
72    /// Stop the replica (process exit, `StopTask`, Fly stop).
73    Stop,
74    /// Lower the replica count, preferring this replica as the victim
75    /// (Kubernetes `pod-deletion-cost`).
76    ScaleIn,
77}
78
79/// A deployment version known to a provider. Versions are immutable (decision 6).
80#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
81pub struct DeploymentRef {
82    /// Deployment name, the namespace of run ids, HMAC keys and API keys.
83    pub name: String,
84    /// Immutable version (e.g. `v7` or a content hash).
85    pub version: String,
86    /// Provider resource the version lives in (Kubernetes `ns/deployment`,
87    /// Cloud Run service name, ECS task definition ARN, Fly app...). Opaque to
88    /// the gateway.
89    pub target: String,
90}
91
92impl DeploymentRef {
93    /// Builds a reference.
94    pub fn new(name: impl Into<String>, version: impl Into<String>, target: impl Into<String>) -> Self {
95        Self { name: name.into(), version: version.into(), target: target.into() }
96    }
97}
98
99/// One replica of a deployment version.
100#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
101pub struct ReplicaRef {
102    /// The deployment version the replica runs.
103    pub deployment: DeploymentRef,
104    /// Provider id (pid, pod name, task ARN, machine id). For `Addressing::Url`
105    /// the single pseudo-replica id is `url`.
106    pub id: String,
107}
108
109/// Where a replica listens. Always obtained from the provider API or a socket
110/// path the gateway chose, never from a claim made by the agent (plan §3.2).
111#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
112#[serde(rename_all = "snake_case")]
113pub enum EndpointAddress {
114    /// Unix domain socket (local-process).
115    Unix(PathBuf),
116    /// Base URL without trailing slash, e.g. `http://10.0.3.7:8080`.
117    Http(String),
118}
119
120/// A reachable replica returned by `wake`.
121#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
122pub struct Endpoint {
123    /// Which replica.
124    pub replica: ReplicaRef,
125    /// Where to reach it.
126    pub address: EndpointAddress,
127}
128
129/// Lifecycle phase of a replica as the provider reports it.
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
131#[serde(rename_all = "snake_case")]
132pub enum ReplicaPhase {
133    /// Requested, not reachable yet.
134    Starting,
135    /// Reachable.
136    Ready,
137    /// Frozen (Fly suspend).
138    Suspended,
139    /// SIGTERM sent / draining.
140    Stopping,
141    /// Gone or stopped.
142    Stopped,
143    /// Crashed or failed to start.
144    Failed,
145}
146
147/// One row of `Provider::list`, used for reconciliation (C5, C12).
148#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
149pub struct ReplicaState {
150    /// Which replica.
151    pub replica: ReplicaRef,
152    /// Its phase.
153    pub phase: ReplicaPhase,
154    /// Its address once known.
155    pub address: Option<EndpointAddress>,
156    /// Start time in unix milliseconds when the provider reports it.
157    pub started_at_ms: Option<u64>,
158}
159
160/// A secret the platform injects as an environment variable. The spec carries
161/// only the reference; the value lives in the provider's secret store and the
162/// gateway never sees it (plan §5, C10).
163#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
164pub struct SecretRef {
165    /// Environment variable name in the replica.
166    pub env: String,
167    /// Provider reference (`projects/p/secrets/s/versions/3`, a Key Vault URI,
168    /// an ARN, `k8s-secret/key`, or `env:NAME` for local-process).
169    pub reference: String,
170}
171
172/// Resource requests for one replica.
173#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
174pub struct Resources {
175    /// CPU in millicores (0 = provider default).
176    pub cpu_millis: u32,
177    /// Memory in MiB (0 = provider default).
178    pub memory_mib: u32,
179}
180
181/// What `graphrt deploy` asks a `DeployDriver` to create or update.
182#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
183pub struct DeploymentSpec {
184    /// Deployment name.
185    pub name: String,
186    /// New immutable version.
187    pub version: String,
188    /// Container image; `None` for local-process (it runs `command`).
189    pub image: Option<String>,
190    /// Command and arguments of the agent (`graphrt serve --trust-gateway ...`).
191    pub command: Vec<String>,
192    /// Plain environment (never secrets).
193    #[serde(default)]
194    pub env: BTreeMap<String, String>,
195    /// Secrets as references.
196    #[serde(default)]
197    pub secrets: Vec<SecretRef>,
198    /// Concurrent runs per replica (Cloud Run `containerConcurrency`).
199    pub max_active_runs: u32,
200    /// Replicas kept warm (0 = scale to zero).
201    pub min_replicas: u32,
202    /// Upper bound of the scaler and of the `max_replicas` quota.
203    pub max_replicas: u32,
204    /// Grace between SIGTERM and SIGKILL the driver sets on the platform.
205    pub grace_secs: u64,
206    /// Resources per replica.
207    #[serde(default)]
208    pub resources: Resources,
209    /// Workflow fingerprint, so the gateway can route resumes to the version a
210    /// paused run was snapshotted on.
211    pub workflow_fingerprint: String,
212    /// Provider-specific settings (region, namespace, VPC...), validated by the
213    /// driver. Provider *endpoints* never come from here (SSRF, plan §6).
214    #[serde(default)]
215    pub provider_settings: BTreeMap<String, serde_json::Value>,
216}