gatehouse-runtime-protocol 0.2.0

Strict data contract for pinned container runtime adapters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Pure transitions for a single removal intent. No filesystem, dispatch,
//! namespace discovery or lease migration. A durable coordinator must commit
//! the returned state under its generation lock BEFORE the corresponding effect.
use crate::{require_absence, Invalid, Label, Operation, Request, Response};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", try_from = "String")]
pub enum Phase {
    Prepared,
    Unknown,
    Completed,
}

impl TryFrom<String> for Phase {
    type Error = Invalid;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        match value.as_str() {
            "prepared" => Ok(Self::Prepared),
            "unknown" => Ok(Self::Unknown),
            "completed" => Ok(Self::Completed),
            _ => Err(Invalid("unsupported intent phase")),
        }
    }
}

/// Fields are private so ordinary callers cannot skip ownership validation.
/// Restore accepts only an operator-protected state record with independent
/// expected request pins; it does not authenticate disk bytes or prevent rollback.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Intent {
    schema: String,
    request: Request,
    phase: Phase,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Record {
    schema: String,
    request: Request,
    phase: Phase,
}

const SCHEMA: &str = "gate-runtime-intent/v1";

fn removal(request: &Request) -> Result<(), Invalid> {
    request.validate()?;
    if !matches!(request.operation, Operation::Remove { .. }) {
        return Err(Invalid("intent requires exact-ID removal"));
    }
    Ok(())
}

fn inspection_request(removal: &Request) -> Request {
    let Operation::Remove { object_id } = &removal.operation else {
        unreachable!("private helper called only after removal validation");
    };
    Request {
        binding: removal.binding.clone(),
        operation: Operation::Inspect {
            object_id: object_id.clone(),
        },
    }
}

impl Intent {
    /// The coordinator must independently authenticate the inspection producer
    /// and pin required labels from its lease, never from the adapter response.
    pub fn prepare(
        request: &Request,
        inspection: &Response,
        required_labels: &[Label],
    ) -> Result<Self, Invalid> {
        removal(request)?;
        inspection.require_ownership(&inspection_request(request), required_labels)?;
        Ok(Self {
            schema: SCHEMA.into(),
            request: request.clone(),
            phase: Phase::Prepared,
        })
    }

    pub fn phase(&self) -> Phase {
        self.phase
    }

    pub fn request(&self) -> &Request {
        &self.request
    }

    /// Commit this Unknown state before dispatch. A crash between commit and
    /// dispatch is conservative: recovery must inspect, never blindly retry.
    pub fn begin_dispatch(&self, expected: &Request) -> Result<Self, Invalid> {
        self.request.require_same_intent(expected)?;
        if self.phase != Phase::Prepared {
            return Err(Invalid("intent is not prepared for dispatch"));
        }
        let mut next = self.clone();
        next.phase = Phase::Unknown;
        Ok(next)
    }

    /// None represents transport loss/cancellation/error. Every valid mutation
    /// response, including completed/rejected-before-effect, leaves Unknown.
    /// Invalid responses return an error without modifying the original state.
    pub fn observe_dispatch(
        &self,
        expected: &Request,
        response: Option<&Response>,
    ) -> Result<Self, Invalid> {
        self.request.require_same_intent(expected)?;
        if self.phase != Phase::Unknown {
            return Err(Invalid("intent has no unresolved dispatch"));
        }
        if let Some(response) = response {
            response.validate(&self.request)?;
        }
        Ok(self.clone())
    }

    /// Only independently authenticated exact-ID absence can finish Unknown.
    /// This pure operation does not remove a marker or grant cleanup authority.
    pub fn reconcile(&self, expected: &Request, inspection: &Response) -> Result<Self, Invalid> {
        self.request.require_same_intent(expected)?;
        if self.phase != Phase::Unknown {
            return Err(Invalid("intent is not awaiting reconciliation"));
        }
        require_absence(&self.request, inspection)?;
        let mut next = self.clone();
        next.phase = Phase::Completed;
        Ok(next)
    }

    /// Read only trusted persisted state. Legacy lease v2, missing pins, duplicate
    /// fields and unknown schema/phase reject; no ambient values are consulted.
    pub fn restore(bytes: &[u8], expected: &Request) -> Result<Self, Invalid> {
        let record: Record = crate::parse(bytes)?;
        if record.schema != SCHEMA {
            return Err(Invalid("unsupported intent schema"));
        }
        removal(&record.request)?;
        record.request.require_same_intent(expected)?;
        Ok(Self {
            schema: record.schema,
            request: record.request,
            phase: record.phase,
        })
    }
}

#[cfg(test)]
mod tests {
    // Pure transition model only. These tests do not establish durable commit,
    // persistence, locking, rollback protection or dispatch behavior.
    use super::*;
    use crate::{Binding, Engine, Namespace, Object, Outcome, Protocol};
    use serde_json::json;

    fn id() -> String {
        "a".repeat(64)
    }

    fn binding() -> Binding {
        Binding {
            protocol: Protocol::V1,
            adapter_sha256: "b".repeat(64),
            adapter_version: "1.0.0".into(),
            invocation: "inv-1".into(),
            operation_key: "op-1".into(),
            generation: "gen-1".into(),
            namespace: Namespace {
                engine: Engine::Docker,
                endpoint: "unix:///run/docker.sock".into(),
                context: "ctx-1".into(),
                principal: "uid:1000".into(),
                rootless: true,
                scope: "scope-1".into(),
            },
        }
    }

    fn removal() -> Request {
        Request {
            binding: binding(),
            operation: Operation::Remove { object_id: id() },
        }
    }

    fn labels() -> Vec<Label> {
        vec![Label {
            key: "harness-gate.owner".into(),
            value: "harness-gate".into(),
        }]
    }

    fn present() -> Response {
        Response {
            binding: binding(),
            operation: Operation::Inspect { object_id: id() },
            outcome: Outcome::Present {
                object: Object {
                    object_id: id(),
                    labels: labels(),
                },
            },
        }
    }

    fn absent() -> Response {
        Response {
            binding: binding(),
            operation: Operation::Inspect { object_id: id() },
            outcome: Outcome::Absent,
        }
    }

    fn prepared() -> Intent {
        Intent::prepare(&removal(), &present(), &labels()).unwrap()
    }

    fn unknown() -> Intent {
        prepared().begin_dispatch(&removal()).unwrap()
    }

    fn binding_mutations() -> Vec<Binding> {
        let mut values = Vec::new();
        let mutations: [fn(&mut Binding); 11] = [
            |b| b.adapter_sha256 = "c".repeat(64),
            |b| b.adapter_version = "2".into(),
            |b| b.invocation = "inv-2".into(),
            |b| b.operation_key = "op-2".into(),
            |b| b.generation = "gen-2".into(),
            |b| b.namespace.engine = Engine::Podman,
            |b| b.namespace.endpoint = "unix:///other".into(),
            |b| b.namespace.context = "ctx-2".into(),
            |b| b.namespace.principal = "uid:1001".into(),
            |b| b.namespace.rootless = false,
            |b| b.namespace.scope = "scope-2".into(),
        ];
        for mutate in mutations {
            let mut value = binding();
            mutate(&mut value);
            values.push(value);
        }
        values
    }

    #[test]
    fn prepare_requires_present_exact_identity_and_trusted_labels() {
        assert_eq!(prepared().phase(), Phase::Prepared);
        assert_eq!(prepared().request(), &removal());
        for response in [
            Response {
                binding: binding(),
                operation: Operation::Inspect {
                    object_id: "b".repeat(64),
                },
                outcome: Outcome::Present {
                    object: Object {
                        object_id: "c".repeat(64),
                        labels: labels(),
                    },
                },
            },
            absent(),
            Response {
                binding: binding(),
                operation: Operation::Inspect { object_id: id() },
                outcome: Outcome::Unavailable,
            },
        ] {
            assert!(Intent::prepare(&removal(), &response, &labels()).is_err());
        }
        let mut wrong_binding = present();
        wrong_binding.binding.invocation = "inv-2".into();
        assert!(Intent::prepare(&removal(), &wrong_binding, &labels()).is_err());
        let missing = Label {
            key: "missing".into(),
            value: "v".into(),
        };
        assert!(Intent::prepare(&removal(), &present(), &[missing]).is_err());
        assert!(Intent::prepare(&removal(), &present(), &[]).is_err());
        assert!(Intent::prepare(
            &removal(),
            &present(),
            &[labels()[0].clone(), labels()[0].clone()]
        )
        .is_err());
        let mut wrong_value = labels();
        wrong_value[0].value = "other".into();
        assert!(Intent::prepare(&removal(), &present(), &wrong_value).is_err());
        let mut extended = present();
        if let Outcome::Present { object } = &mut extended.outcome {
            object.labels.push(Label {
                key: "extra".into(),
                value: "allowed".into(),
            });
        }
        assert!(Intent::prepare(&removal(), &extended, &labels()).is_ok());
    }

    #[test]
    fn prepare_rejects_non_removal_requests() {
        let inspect = Request {
            binding: binding(),
            operation: Operation::Inspect { object_id: id() },
        };
        assert!(Intent::prepare(&inspect, &present(), &labels()).is_err());
        let capabilities = Request {
            binding: binding(),
            operation: Operation::Capabilities,
        };
        assert!(Intent::prepare(&capabilities, &present(), &labels()).is_err());
    }

    #[test]
    fn binding_conflicts_reject_every_transition_and_restore() {
        let prepared = prepared();
        let unknown = unknown();
        let completed = unknown.reconcile(&removal(), &absent()).unwrap();
        for changed in binding_mutations() {
            let expected = Request {
                binding: changed,
                operation: removal().operation,
            };
            assert!(prepared.begin_dispatch(&expected).is_err());
            assert!(unknown.observe_dispatch(&expected, None).is_err());
            assert!(unknown.reconcile(&expected, &absent()).is_err());
            let bytes = serde_json::to_vec(&completed).unwrap();
            assert!(Intent::restore(&bytes, &expected).is_err());
        }
        for object_id in ["b".repeat(64), "c".repeat(64)] {
            let expected = Request {
                binding: binding(),
                operation: Operation::Remove { object_id },
            };
            assert!(prepared.begin_dispatch(&expected).is_err());
            assert!(unknown.observe_dispatch(&expected, None).is_err());
            assert!(unknown.reconcile(&expected, &absent()).is_err());
            let bytes = serde_json::to_vec(&unknown).unwrap();
            assert!(Intent::restore(&bytes, &expected).is_err());
        }
    }

    #[test]
    fn phase_matrix_rejects_illegal_transitions() {
        let prepared = prepared();
        let unknown = unknown();
        let completed = unknown.reconcile(&removal(), &absent()).unwrap();
        assert!(prepared.begin_dispatch(&removal()).is_ok());
        assert!(prepared.observe_dispatch(&removal(), None).is_err());
        assert!(prepared.reconcile(&removal(), &absent()).is_err());
        assert!(unknown.begin_dispatch(&removal()).is_err());
        assert_eq!(
            unknown.observe_dispatch(&removal(), None).unwrap().phase(),
            Phase::Unknown
        );
        assert_eq!(
            unknown.reconcile(&removal(), &absent()).unwrap().phase(),
            Phase::Completed
        );
        assert!(completed.begin_dispatch(&removal()).is_err());
        assert!(completed.observe_dispatch(&removal(), None).is_err());
        assert!(completed.reconcile(&removal(), &absent()).is_err());
    }

    #[test]
    fn unknown_keeps_unknown_for_none_and_all_mutation_outcomes() {
        let unknown = unknown();
        assert_eq!(
            unknown.observe_dispatch(&removal(), None).unwrap().phase(),
            Phase::Unknown
        );
        for outcome in [
            Outcome::Completed,
            Outcome::RejectedBeforeEffect,
            Outcome::Unknown,
        ] {
            let response = Response {
                binding: binding(),
                operation: removal().operation.clone(),
                outcome,
            };
            let next = unknown
                .observe_dispatch(&removal(), Some(&response))
                .unwrap();
            assert_eq!(next.phase(), Phase::Unknown);
            assert_eq!(next.request(), unknown.request());
            assert_eq!(unknown.phase(), Phase::Unknown);
            assert_eq!(unknown.request(), &removal());
        }
    }

    #[test]
    fn unknown_rejects_invalid_response_without_change() {
        let unknown = unknown();
        let invalid = Response {
            binding: binding(),
            operation: Operation::Inspect { object_id: id() },
            outcome: Outcome::Absent,
        };
        assert!(unknown
            .observe_dispatch(&removal(), Some(&invalid))
            .is_err());
        assert_eq!(unknown.phase(), Phase::Unknown);
        assert_eq!(unknown.request(), &removal());
    }

    #[test]
    fn completed_requires_exact_id_absence_and_is_terminal() {
        let unknown = unknown();
        for response in [
            present(),
            Response {
                binding: binding(),
                operation: Operation::Inspect { object_id: id() },
                outcome: Outcome::Unavailable,
            },
            Response {
                binding: binding(),
                operation: Operation::Inspect {
                    object_id: "b".repeat(64),
                },
                outcome: Outcome::Absent,
            },
        ] {
            assert!(unknown.reconcile(&removal(), &response).is_err());
        }
        let completed = unknown.reconcile(&removal(), &absent()).unwrap();
        assert_eq!(completed.phase(), Phase::Completed);
        assert!(completed.begin_dispatch(&removal()).is_err());
        assert!(completed.observe_dispatch(&removal(), None).is_err());
        assert!(completed.reconcile(&removal(), &absent()).is_err());
    }

    #[test]
    fn restore_roundtrips_and_enforces_schema_and_phase() {
        for intent in [
            prepared(),
            unknown(),
            unknown().reconcile(&removal(), &absent()).unwrap(),
        ] {
            let bytes = serde_json::to_vec(&intent).unwrap();
            let restored = Intent::restore(&bytes, &removal()).unwrap();
            assert_eq!(restored, intent);
        }
        let mut value = serde_json::to_value(prepared()).unwrap();
        value["schema"] = json!("other/v1");
        assert!(Intent::restore(&serde_json::to_vec(&value).unwrap(), &removal()).is_err());
        value["schema"] = json!("gate-runtime-intent/v1");
        value["phase"] = json!("bogus");
        assert!(Intent::restore(&serde_json::to_vec(&value).unwrap(), &removal()).is_err());
        value["phase"] = json!({"prepared": null});
        assert!(Intent::restore(&serde_json::to_vec(&value).unwrap(), &removal()).is_err());
        value["extra"] = json!(1);
        assert!(Intent::restore(&serde_json::to_vec(&value).unwrap(), &removal()).is_err());
        let legacy = json!({
            "owner_marker": "harness-gate",
            "schema_version": 2,
            "runtime": "docker",
            "runtime_object_id": id()
        });
        assert!(Intent::restore(&serde_json::to_vec(&legacy).unwrap(), &removal()).is_err());
    }

    #[test]
    fn restore_rejects_duplicates_missing_pins_and_bad_bytes() {
        let mut value = serde_json::to_value(prepared()).unwrap();
        value["request"]["binding"]
            .as_object_mut()
            .unwrap()
            .remove("generation");
        assert!(Intent::restore(&serde_json::to_vec(&value).unwrap(), &removal()).is_err());
        let original = serde_json::to_string(&prepared()).unwrap();
        let duplicate = original.replacen(
            r#""phase":"prepared""#,
            r#""phase":"prepared","phase":"prepared""#,
            1,
        );
        assert!(Intent::restore(duplicate.as_bytes(), &removal()).is_err());
        let mut padded = serde_json::to_vec(&prepared()).unwrap();
        padded.extend(std::iter::repeat_n(b' ', crate::MAX_MESSAGE_BYTES));
        assert!(Intent::restore(&padded, &removal()).is_err());
        let mut invalid = serde_json::to_value(prepared()).unwrap();
        invalid["request"]["binding"]["invocation"] = json!("SECRET-ECHO\n");
        let bytes = serde_json::to_vec(&invalid).unwrap();
        let error = Intent::restore(&bytes, &removal()).unwrap_err();
        assert!(!error.to_string().contains("SECRET-ECHO"));
        let before = bytes.clone();
        let _ = Intent::restore(&bytes, &removal());
        assert_eq!(bytes, before);
    }
}