microsandbox_types/modify.rs
1//! Sandbox modification contract shared by the SDKs, the CLI, and future backends.
2//!
3//! These are the serializable request/response types behind `sandbox.modify()`:
4//! the patch a caller submits, and the plan that classifies each change. The
5//! builder and classification logic live in the SDK; this module owns only the
6//! wire-shaped data so any backend (local today, cloud later) and any language
7//! binding can speak the same contract.
8
9use serde::{Deserialize, Serialize};
10use zeroize::Zeroizing;
11
12use crate::domain::EnvVar;
13
14//--------------------------------------------------------------------------------------------------
15// Types
16//--------------------------------------------------------------------------------------------------
17
18/// A requested sandbox modification.
19///
20/// This type is serializable so SDKs and the CLI can share one canonical
21/// contract. The only field that may carry raw secret material is the
22/// per-secret `value` inside [`SecretModificationPatch`]; plans derived from
23/// a patch are always value-free.
24#[derive(Debug, Clone, Default, Serialize, Deserialize)]
25#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
26pub struct SandboxModificationPatch {
27 /// Desired effective vCPU count.
28 #[serde(skip_serializing_if = "Option::is_none")]
29 pub cpus: Option<u8>,
30
31 /// Desired boot-time maximum possible vCPU count.
32 #[serde(skip_serializing_if = "Option::is_none")]
33 pub max_cpus: Option<u8>,
34
35 /// Desired effective guest memory in MiB.
36 #[serde(skip_serializing_if = "Option::is_none")]
37 pub memory_mib: Option<u32>,
38
39 /// Desired boot-time maximum hotpluggable memory in MiB.
40 #[serde(skip_serializing_if = "Option::is_none")]
41 pub max_memory_mib: Option<u32>,
42
43 /// Desired root disk size in MiB. Managed kind: grow-only (the upper is a real ext4 image,
44 /// so shrinking risks data loss and is rejected). Tmpfs kind: any direction, effective next
45 /// boot. Disk-image kind: rejected (user-owned file). Accepts the legacy
46 /// `oci_upper_size_mib` wire spelling.
47 #[serde(alias = "oci_upper_size_mib", skip_serializing_if = "Option::is_none")]
48 pub root_disk_size_mib: Option<u32>,
49
50 /// Environment variables to set for future execs.
51 #[serde(default, skip_serializing_if = "Vec::is_empty")]
52 pub env: Vec<EnvVar>,
53
54 /// Environment variable keys to remove.
55 #[serde(default, skip_serializing_if = "Vec::is_empty")]
56 pub env_remove: Vec<String>,
57
58 /// Labels to set.
59 #[serde(default, skip_serializing_if = "Vec::is_empty")]
60 pub labels: Vec<(String, String)>,
61
62 /// Label keys to remove.
63 #[serde(default, skip_serializing_if = "Vec::is_empty")]
64 pub labels_remove: Vec<String>,
65
66 /// Desired working directory for future execs.
67 #[serde(skip_serializing_if = "Option::is_none")]
68 pub workdir: Option<String>,
69
70 /// Desired secret specs, keyed by secret name. The planner diffs each
71 /// spec against the existing config to infer what changes.
72 #[serde(default, skip_serializing_if = "Vec::is_empty")]
73 pub secrets: Vec<SecretModificationPatch>,
74
75 /// Secret names to remove. Removal is explicit: absence of a name from
76 /// `secrets` never means removal.
77 #[serde(default, skip_serializing_if = "Vec::is_empty")]
78 pub secrets_remove: Vec<String>,
79}
80
81/// Policy selected for applying or planning a modification.
82#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
83#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
84#[serde(rename_all = "snake_case")]
85pub enum ModificationPolicy {
86 /// Apply only changes that can complete without restarting the running sandbox.
87 #[default]
88 NoRestart,
89
90 /// Persist the desired config for the next start and leave any running VM unchanged.
91 NextStart,
92
93 /// Persist the patch and restart the sandbox if restart-required changes are present.
94 Restart,
95}
96
97/// A desired secret spec inside a modification patch.
98///
99/// The spec is declarative: it states the target state for one secret (source
100/// or value, placeholder, allowed hosts) and the planner infers the concrete
101/// change — added, rotated, hosts updated, placeholder updated — by diffing
102/// the spec against the existing config. Removal is explicit through
103/// [`SandboxModificationPatch::secrets_remove`].
104///
105/// Only `value` may carry secret material, and only in-process: it is
106/// [`Zeroizing`]-wrapped, redacted from `Debug` output, skipped by serde when
107/// empty, and never copied into the plan.
108#[derive(Clone, Default, Serialize, Deserialize)]
109#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
110pub struct SecretModificationPatch {
111 /// Stable secret identity, usually the environment variable name.
112 pub name: String,
113
114 /// Host-side source reference to resolve the value from. Mutually
115 /// exclusive with `value`.
116 #[serde(skip_serializing_if = "Option::is_none")]
117 pub source: Option<SecretSource>,
118
119 /// Raw secret value supplied by the caller, for embedders that hold only
120 /// a value (e.g. from their own vault). Mutually exclusive with `source`.
121 /// A value-based apply persists the value into the durable config until a
122 /// later source-based rotate migrates the entry to a reference.
123 #[serde(default, skip_serializing_if = "String::is_empty")]
124 #[cfg_attr(feature = "ts", ts(type = "string"))]
125 pub value: Zeroizing<String>,
126
127 /// Guest-visible placeholder/reference, if explicitly requested.
128 #[serde(skip_serializing_if = "Option::is_none")]
129 pub placeholder: Option<String>,
130
131 /// Desired allowed host patterns. Empty means "leave unchanged" for an
132 /// existing secret; a new secret needs at least one.
133 #[serde(default, skip_serializing_if = "Vec::is_empty")]
134 pub allowed_hosts: Vec<String>,
135}
136
137/// Host-side source for secret material.
138#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
139#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
140#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
141#[serde(tag = "kind", rename_all = "snake_case")]
142pub enum SecretSource {
143 /// Read the value from a host environment variable at apply time.
144 Env {
145 /// Host environment variable name.
146 var: String,
147 },
148
149 /// Read the value from a host-side secret store reference.
150 Store {
151 /// Store-specific secret reference.
152 reference: String,
153 },
154}
155
156/// Serializable dry-run or apply plan for a sandbox modification.
157#[derive(Debug, Clone, Serialize, Deserialize)]
158#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
159pub struct SandboxModificationPlan {
160 /// Sandbox being modified.
161 pub sandbox: String,
162
163 /// Sandbox status used for classification.
164 pub status: String,
165
166 /// Whether the changes were applied.
167 pub applied: bool,
168
169 /// Modification policy used to produce the plan.
170 pub policy: ModificationPolicy,
171
172 /// Planned changes.
173 pub changes: Vec<PlannedChange>,
174
175 /// Conflicts that must be resolved before the patch can apply.
176 pub conflicts: Vec<ModificationConflict>,
177
178 /// Non-fatal warnings about the patch or current runtime capabilities.
179 pub warnings: Vec<ModificationWarning>,
180
181 /// Live resource resize outcomes, populated by apply when a live change ran.
182 #[serde(default, skip_serializing_if = "Vec::is_empty")]
183 pub resize_status: Vec<ResourceResizeStatus>,
184}
185
186/// One planned modification entry.
187#[derive(Debug, Clone, Serialize, Deserialize)]
188#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
189#[serde(tag = "kind", rename_all = "snake_case")]
190pub enum PlannedChange {
191 /// Ordinary config change.
192 Config(ConfigPlannedChange),
193
194 /// Secret change. Values are omitted by construction.
195 Secret(SecretPlannedChange),
196}
197
198/// Planned config change.
199#[derive(Debug, Clone, Serialize, Deserialize)]
200#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
201pub struct ConfigPlannedChange {
202 /// Config field being changed.
203 pub field: String,
204
205 /// Natural change type for table rendering.
206 pub change: ChangeKind,
207
208 /// Previous safe visible state.
209 #[serde(skip_serializing_if = "Option::is_none")]
210 pub before: Option<String>,
211
212 /// New safe visible state.
213 #[serde(skip_serializing_if = "Option::is_none")]
214 pub after: Option<String>,
215
216 /// When or whether the change can take effect.
217 pub disposition: ModificationDisposition,
218
219 /// Human-readable reason for this classification, when useful.
220 #[serde(skip_serializing_if = "Option::is_none")]
221 pub reason: Option<String>,
222}
223
224/// Planned secret change.
225#[derive(Debug, Clone, Serialize, Deserialize)]
226#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
227pub struct SecretPlannedChange {
228 /// Table field name. This is always `secret`.
229 pub field: String,
230
231 /// Stable secret identity, usually the environment variable name.
232 pub name: String,
233
234 /// Natural change type for table rendering.
235 pub change: SecretChangeKind,
236
237 /// Previous guest-visible reference or placeholder.
238 #[serde(skip_serializing_if = "Option::is_none")]
239 pub before_ref: Option<String>,
240
241 /// New guest-visible reference or placeholder.
242 #[serde(skip_serializing_if = "Option::is_none")]
243 pub after_ref: Option<String>,
244
245 /// When or whether the change can take effect.
246 pub disposition: ModificationDisposition,
247
248 /// Allowed hosts after the requested change.
249 #[serde(default, skip_serializing_if = "Vec::is_empty")]
250 pub allow_hosts: Vec<String>,
251
252 /// Human-readable reason for this classification, when useful.
253 #[serde(skip_serializing_if = "Option::is_none")]
254 pub reason: Option<String>,
255}
256
257/// Natural config change type for human output.
258#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
259#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
260#[serde(rename_all = "lowercase")]
261pub enum ChangeKind {
262 /// A field is being added.
263 Added,
264
265 /// A field is being updated.
266 Updated,
267
268 /// A field is being removed.
269 Removed,
270}
271
272/// Natural secret change type for human output.
273#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
274#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
275pub enum SecretChangeKind {
276 /// A secret placeholder is being added.
277 #[serde(rename = "added")]
278 Added,
279
280 /// A secret value is being rotated.
281 #[serde(rename = "rotated")]
282 Rotated,
283
284 /// A secret is being removed.
285 #[serde(rename = "removed")]
286 Removed,
287
288 /// A secret is being renamed.
289 #[serde(rename = "renamed")]
290 Renamed,
291
292 /// Allowed hosts are being updated.
293 #[serde(rename = "hosts updated")]
294 HostsUpdated,
295
296 /// The guest-visible placeholder is being updated.
297 #[serde(rename = "placeholder updated")]
298 PlaceholderUpdated,
299}
300
301/// When or whether a planned change can take effect.
302#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
303#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
304pub enum ModificationDisposition {
305 /// Applies to the running VM now.
306 #[serde(rename = "live")]
307 Live,
308
309 /// Persists to desired config and applies the next time the sandbox starts.
310 #[serde(rename = "next start")]
311 NextStart,
312
313 /// Needs a restart before it can take effect.
314 #[serde(rename = "requires restart")]
315 RequiresRestart,
316
317 /// Cannot be changed by `modify`.
318 #[serde(rename = "unsupported")]
319 Unsupported,
320}
321
322/// Conflict that blocks applying a modification.
323#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
324#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
325pub struct ModificationConflict {
326 /// Field with the conflict.
327 pub field: String,
328
329 /// Human-readable conflict description.
330 pub message: String,
331}
332
333/// Warning emitted while planning a modification.
334#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
335#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
336pub struct ModificationWarning {
337 /// Field associated with the warning.
338 pub field: String,
339
340 /// Human-readable warning description.
341 pub message: String,
342}
343
344/// Resource kind used by live resize convergence reporting.
345#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
346#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
347#[serde(rename_all = "snake_case")]
348pub enum ResourceKind {
349 /// vCPU count.
350 Cpus,
351
352 /// Guest memory.
353 Memory,
354}
355
356/// Runtime convergence state for an accepted resource resize.
357#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
358#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
359#[serde(rename_all = "kebab-case")]
360pub enum ResourceConvergenceState {
361 /// The runtime accepted the request.
362 Accepted,
363
364 /// The guest and VMM are still converging on the requested state.
365 Converging,
366
367 /// Desired, actual, and enforced state match.
368 Applied,
369
370 /// The guest refused or failed to cooperate.
371 GuestRefused,
372
373 /// The resize failed.
374 Failed,
375}
376
377/// Status for a live resource resize.
378#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
379#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
380pub struct ResourceResizeStatus {
381 /// Resource being resized.
382 pub resource: ResourceKind,
383
384 /// Requested value.
385 pub requested: String,
386
387 /// Actual value observed in the guest/runtime.
388 pub actual: String,
389
390 /// Host/VMM-enforced value.
391 pub enforced: String,
392
393 /// Convergence state.
394 pub state: ResourceConvergenceState,
395}
396
397//--------------------------------------------------------------------------------------------------
398// Trait Implementations
399//--------------------------------------------------------------------------------------------------
400
401impl std::fmt::Debug for SecretModificationPatch {
402 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
403 f.debug_struct("SecretModificationPatch")
404 .field("name", &self.name)
405 .field("source", &self.source)
406 .field("value", &"[REDACTED]")
407 .field("placeholder", &self.placeholder)
408 .field("allowed_hosts", &self.allowed_hosts)
409 .finish()
410 }
411}