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")]
113#[non_exhaustive]
114pub enum EndpointAddress {
115    /// Unix domain socket (local-process on Unix).
116    Unix(PathBuf),
117    /// Base URL without trailing slash, e.g. `http://10.0.3.7:8080`.
118    Http(String),
119    /// Windows named pipe, e.g. `\\.\pipe\graphrt-…` (local-process on Windows).
120    Pipe(String),
121}
122
123/// A reachable replica returned by `wake`.
124#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
125pub struct Endpoint {
126    /// Which replica.
127    pub replica: ReplicaRef,
128    /// Where to reach it.
129    pub address: EndpointAddress,
130}
131
132/// Lifecycle phase of a replica as the provider reports it.
133#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
134#[serde(rename_all = "snake_case")]
135pub enum ReplicaPhase {
136    /// Requested, not reachable yet.
137    Starting,
138    /// Reachable.
139    Ready,
140    /// Frozen (Fly suspend).
141    Suspended,
142    /// SIGTERM sent / draining.
143    Stopping,
144    /// Gone or stopped.
145    Stopped,
146    /// Crashed or failed to start.
147    Failed,
148}
149
150/// One row of `Provider::list`, used for reconciliation (C5, C12).
151#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
152pub struct ReplicaState {
153    /// Which replica.
154    pub replica: ReplicaRef,
155    /// Its phase.
156    pub phase: ReplicaPhase,
157    /// Its address once known.
158    pub address: Option<EndpointAddress>,
159    /// Start time in unix milliseconds when the provider reports it.
160    pub started_at_ms: Option<u64>,
161}
162
163/// A secret the platform injects as an environment variable. The spec carries
164/// only the reference; the value lives in the provider's secret store and the
165/// gateway never sees it (plan §5, C10).
166#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
167pub struct SecretRef {
168    /// Environment variable name in the replica.
169    pub env: String,
170    /// Provider reference (`projects/p/secrets/s/versions/3`, a Key Vault URI,
171    /// an ARN, `k8s-secret/key`, or `env:NAME` for local-process).
172    pub reference: String,
173}
174
175/// Resource requests for one replica.
176#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
177pub struct Resources {
178    /// CPU in millicores (0 = provider default).
179    pub cpu_millis: u32,
180    /// Memory in MiB (0 = provider default).
181    pub memory_mib: u32,
182}
183
184/// What `graphrt deploy` asks a `DeployDriver` to create or update.
185#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
186pub struct DeploymentSpec {
187    /// Deployment name.
188    pub name: String,
189    /// New immutable version.
190    pub version: String,
191    /// Container image; `None` for local-process (it runs `command`).
192    pub image: Option<String>,
193    /// Command and arguments of the agent (`graphrt serve --trust-gateway ...`).
194    pub command: Vec<String>,
195    /// Plain environment (never secrets).
196    #[serde(default)]
197    pub env: BTreeMap<String, String>,
198    /// Secrets as references.
199    #[serde(default)]
200    pub secrets: Vec<SecretRef>,
201    /// Concurrent runs per replica (Cloud Run `containerConcurrency`).
202    pub max_active_runs: u32,
203    /// Replicas kept warm (0 = scale to zero).
204    pub min_replicas: u32,
205    /// Upper bound of the scaler and of the `max_replicas` quota.
206    pub max_replicas: u32,
207    /// Grace between SIGTERM and SIGKILL the driver sets on the platform.
208    pub grace_secs: u64,
209    /// Resources per replica.
210    #[serde(default)]
211    pub resources: Resources,
212    /// Workflow fingerprint, so the gateway can route resumes to the version a
213    /// paused run was snapshotted on.
214    pub workflow_fingerprint: String,
215    /// Provider-specific settings (region, namespace, VPC...), validated by the
216    /// driver. Provider *endpoints* never come from here (SSRF, plan §6).
217    #[serde(default)]
218    pub provider_settings: BTreeMap<String, serde_json::Value>,
219}