canic-host 0.110.22

Host-side App build and desired-state Fleet reconciliation library for Canic workspaces
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Module: fleet_ensure::json
//!
//! Responsibility: encode current Fleet Ensure plan and report JSON projections.
//! Does not own: plan hashing, schema selection, or durable file replacement.
//! Boundary: every nested `u128` is bounded decimal text and report payloads stay external.

#[cfg(test)]
mod tests;

use crate::fleet_ensure::model::{CanisterPlan, EnsureAction, FleetEnsurePlan, FleetEnsureReport};
use std::fmt::Display;

use canic_core::cdk::utils::hash::sha256_hex;
use serde::{
    Serialize, Serializer,
    ser::{
        SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
        SerializeTupleStruct, SerializeTupleVariant,
    },
};
use serde_json::{Map, Value};

const STORE_CHUNK_OBJECT_DIRECTORY: &str = ".canic/fleet-ensure/objects/sha256";

/// Project one complete report without expanding content-addressed Store chunk bytes.
///
/// Each chunk retains a workspace-relative object path, its exact SHA-256 and its byte size.
pub fn report_json_value(report: &FleetEnsureReport) -> Result<Value, serde_json::Error> {
    let mut projection = Map::new();
    insert_serialized(
        &mut projection,
        "actual_conservation",
        &report.actual_conservation,
    )?;
    insert_serialized(&mut projection, "effects_applied", &report.effects_applied)?;
    insert_serialized(&mut projection, "funding_review", &report.funding_review)?;
    projection.insert("plan".to_string(), plan_json_value(&report.plan)?);
    insert_serialized(&mut projection, "terminal", &report.terminal)?;
    Ok(Value::Object(projection))
}

fn plan_json_value(plan: &FleetEnsurePlan) -> Result<Value, serde_json::Error> {
    let mut projection = Map::new();
    insert_serialized(&mut projection, "recovery_review", &plan.recovery_review)?;
    insert_serialized(&mut projection, "reinstall", &plan.reinstall)?;
    projection.insert(
        "canisters".to_string(),
        Value::Array(
            plan.canisters
                .iter()
                .map(canister_json_value)
                .collect::<Result<Vec<_>, _>>()?,
        ),
    );
    insert_serialized(&mut projection, "conservation", &plan.conservation)?;
    insert_serialized(&mut projection, "continuation", &plan.continuation)?;
    insert_serialized(&mut projection, "desired_sha256", &plan.desired_sha256)?;
    insert_serialized(&mut projection, "environment", &plan.environment)?;
    insert_serialized(&mut projection, "fleet", &plan.fleet)?;
    insert_serialized(&mut projection, "operation_id", &plan.operation_id)?;
    insert_serialized(&mut projection, "plan_sha256", &plan.plan_sha256)?;
    insert_serialized(&mut projection, "planned_at_time", &plan.planned_at_time)?;
    projection.insert(
        "protocol_actions".to_string(),
        Value::Array(
            plan.protocol_actions
                .iter()
                .map(action_json_value)
                .collect::<Result<Vec<_>, _>>()?,
        ),
    );
    insert_serialized(
        &mut projection,
        "root_start_authority",
        &plan.root_start_authority,
    )?;
    insert_serialized(
        &mut projection,
        "root_reinstall_bindings",
        &plan.root_reinstall_bindings,
    )?;
    insert_serialized(&mut projection, "reviewed_desired", &plan.reviewed_desired)?;
    insert_serialized(&mut projection, "schema_version", &plan.schema_version)?;
    insert_serialized(&mut projection, "scope", &plan.scope)?;
    insert_serialized(
        &mut projection,
        "terminal_inventory_operation_id",
        &plan.terminal_inventory_operation_id,
    )?;
    Ok(Value::Object(projection))
}

fn canister_json_value(canister: &CanisterPlan) -> Result<Value, serde_json::Error> {
    let mut projection = Map::new();
    projection.insert(
        "actions".to_string(),
        Value::Array(
            canister
                .actions
                .iter()
                .map(action_json_value)
                .collect::<Result<Vec<_>, _>>()?,
        ),
    );
    insert_serialized(&mut projection, "disposition", &canister.disposition)?;
    insert_serialized(&mut projection, "name", &canister.name)?;
    insert_serialized(
        &mut projection,
        "observed_cycles",
        &canister.observed_cycles,
    )?;
    insert_serialized(&mut projection, "principal", &canister.principal)?;
    Ok(Value::Object(projection))
}

fn action_json_value(action: &EnsureAction) -> Result<Value, serde_json::Error> {
    let EnsureAction::FleetProtocol {
        action: protocol_action,
        ..
    } = action
    else {
        return to_value(action);
    };
    let Some(bytes) = protocol_action.publication_bytes() else {
        return to_value(action);
    };

    let bytes_sha256 = sha256_hex(bytes);
    let bytes_size = u64::try_from(bytes.len()).unwrap_or(u64::MAX);
    let mut compact = action.clone();
    let EnsureAction::FleetProtocol {
        action: compact_protocol_action,
        ..
    } = &mut compact
    else {
        unreachable!("cloned Fleet protocol action retains its variant")
    };
    compact_protocol_action
        .publication_bytes_mut()
        .expect("cloned publication retains payload")
        .clear();

    let mut projection = to_value(&compact)?;
    let projected_request = projection
        .get_mut("action")
        .and_then(|value| value.get_mut("request"))
        .and_then(Value::as_object_mut)
        .ok_or_else(|| json_shape_error("Store publication report projection is invalid"))?;
    projected_request.remove("bytes");
    projected_request.insert(
        "bytes_path".to_string(),
        Value::String(format!("{STORE_CHUNK_OBJECT_DIRECTORY}/{bytes_sha256}")),
    );
    projected_request.insert("bytes_sha256".to_string(), Value::String(bytes_sha256));
    projected_request.insert("bytes_size".to_string(), Value::from(bytes_size));
    Ok(projection)
}

fn insert_serialized<T>(
    projection: &mut Map<String, Value>,
    field: &'static str,
    value: &T,
) -> Result<(), serde_json::Error>
where
    T: Serialize,
{
    projection.insert(field.to_string(), to_value(value)?);
    Ok(())
}

fn json_shape_error(reason: &'static str) -> serde_json::Error {
    <serde_json::Error as serde::ser::Error>::custom(reason)
}

pub(super) fn to_vec(value: &impl Serialize) -> Result<Vec<u8>, serde_json::Error> {
    let mut bytes = Vec::new();
    let mut serializer = serde_json::Serializer::new(&mut bytes);
    value.serialize(DecimalU128Serializer(&mut serializer))?;
    Ok(bytes)
}

pub(super) fn to_value(value: &impl Serialize) -> Result<serde_json::Value, serde_json::Error> {
    value.serialize(DecimalU128Serializer(serde_json::value::Serializer))
}

struct DecimalU128Value<'a, T: ?Sized>(&'a T);

impl<T> Serialize for DecimalU128Value<'_, T>
where
    T: Serialize + ?Sized,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.0.serialize(DecimalU128Serializer(serializer))
    }
}

struct DecimalU128Serializer<S>(S);

impl<S> Serializer for DecimalU128Serializer<S>
where
    S: Serializer,
{
    type Ok = S::Ok;
    type Error = S::Error;
    type SerializeSeq = DecimalU128Seq<S::SerializeSeq>;
    type SerializeTuple = DecimalU128Tuple<S::SerializeTuple>;
    type SerializeTupleStruct = DecimalU128TupleStruct<S::SerializeTupleStruct>;
    type SerializeTupleVariant = DecimalU128TupleVariant<S::SerializeTupleVariant>;
    type SerializeMap = DecimalU128Map<S::SerializeMap>;
    type SerializeStruct = DecimalU128Struct<S::SerializeStruct>;
    type SerializeStructVariant = DecimalU128StructVariant<S::SerializeStructVariant>;

    fn serialize_bool(self, value: bool) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_bool(value)
    }

    fn serialize_i8(self, value: i8) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_i8(value)
    }

    fn serialize_i16(self, value: i16) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_i16(value)
    }

    fn serialize_i32(self, value: i32) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_i32(value)
    }

    fn serialize_i64(self, value: i64) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_i64(value)
    }

    fn serialize_i128(self, value: i128) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_i128(value)
    }

    fn serialize_u8(self, value: u8) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_u8(value)
    }

    fn serialize_u16(self, value: u16) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_u16(value)
    }

    fn serialize_u32(self, value: u32) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_u32(value)
    }

    fn serialize_u64(self, value: u64) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_u64(value)
    }

    fn serialize_u128(self, value: u128) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_str(&value.to_string())
    }

    fn serialize_f32(self, value: f32) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_f32(value)
    }

    fn serialize_f64(self, value: f64) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_f64(value)
    }

    fn serialize_char(self, value: char) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_char(value)
    }

    fn serialize_str(self, value: &str) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_str(value)
    }

    fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_bytes(value)
    }

    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_none()
    }

    fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0.serialize_some(&DecimalU128Value(value))
    }

    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_unit()
    }

    fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_unit_struct(name)
    }

    fn serialize_unit_variant(
        self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
    ) -> Result<Self::Ok, Self::Error> {
        self.0.serialize_unit_variant(name, variant_index, variant)
    }

    fn serialize_newtype_struct<T>(
        self,
        name: &'static str,
        value: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0
            .serialize_newtype_struct(name, &DecimalU128Value(value))
    }

    fn serialize_newtype_variant<T>(
        self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
        value: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0
            .serialize_newtype_variant(name, variant_index, variant, &DecimalU128Value(value))
    }

    fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
        self.0.serialize_seq(len).map(DecimalU128Seq)
    }

    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
        self.0.serialize_tuple(len).map(DecimalU128Tuple)
    }

    fn serialize_tuple_struct(
        self,
        name: &'static str,
        len: usize,
    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
        self.0
            .serialize_tuple_struct(name, len)
            .map(DecimalU128TupleStruct)
    }

    fn serialize_tuple_variant(
        self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
        len: usize,
    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
        self.0
            .serialize_tuple_variant(name, variant_index, variant, len)
            .map(DecimalU128TupleVariant)
    }

    fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
        self.0.serialize_map(len).map(DecimalU128Map)
    }

    fn serialize_struct(
        self,
        name: &'static str,
        len: usize,
    ) -> Result<Self::SerializeStruct, Self::Error> {
        self.0.serialize_struct(name, len).map(DecimalU128Struct)
    }

    fn serialize_struct_variant(
        self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
        len: usize,
    ) -> Result<Self::SerializeStructVariant, Self::Error> {
        self.0
            .serialize_struct_variant(name, variant_index, variant, len)
            .map(DecimalU128StructVariant)
    }

    fn collect_str<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
    where
        T: Display + ?Sized,
    {
        self.0.collect_str(value)
    }

    fn is_human_readable(&self) -> bool {
        self.0.is_human_readable()
    }
}

struct DecimalU128Seq<S>(S);

impl<S> SerializeSeq for DecimalU128Seq<S>
where
    S: SerializeSeq,
{
    type Ok = S::Ok;
    type Error = S::Error;

    fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0.serialize_element(&DecimalU128Value(value))
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.0.end()
    }
}

struct DecimalU128Tuple<S>(S);

impl<S> SerializeTuple for DecimalU128Tuple<S>
where
    S: SerializeTuple,
{
    type Ok = S::Ok;
    type Error = S::Error;

    fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0.serialize_element(&DecimalU128Value(value))
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.0.end()
    }
}

struct DecimalU128TupleStruct<S>(S);

impl<S> SerializeTupleStruct for DecimalU128TupleStruct<S>
where
    S: SerializeTupleStruct,
{
    type Ok = S::Ok;
    type Error = S::Error;

    fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0.serialize_field(&DecimalU128Value(value))
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.0.end()
    }
}

struct DecimalU128TupleVariant<S>(S);

impl<S> SerializeTupleVariant for DecimalU128TupleVariant<S>
where
    S: SerializeTupleVariant,
{
    type Ok = S::Ok;
    type Error = S::Error;

    fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0.serialize_field(&DecimalU128Value(value))
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.0.end()
    }
}

struct DecimalU128Map<S>(S);

impl<S> SerializeMap for DecimalU128Map<S>
where
    S: SerializeMap,
{
    type Ok = S::Ok;
    type Error = S::Error;

    fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0.serialize_key(&DecimalU128Value(key))
    }

    fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0.serialize_value(&DecimalU128Value(value))
    }

    fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), Self::Error>
    where
        K: Serialize + ?Sized,
        V: Serialize + ?Sized,
    {
        self.0
            .serialize_entry(&DecimalU128Value(key), &DecimalU128Value(value))
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.0.end()
    }
}

struct DecimalU128Struct<S>(S);

impl<S> SerializeStruct for DecimalU128Struct<S>
where
    S: SerializeStruct,
{
    type Ok = S::Ok;
    type Error = S::Error;

    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0.serialize_field(key, &DecimalU128Value(value))
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.0.end()
    }
}

struct DecimalU128StructVariant<S>(S);

impl<S> SerializeStructVariant for DecimalU128StructVariant<S>
where
    S: SerializeStructVariant,
{
    type Ok = S::Ok;
    type Error = S::Error;

    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.0.serialize_field(key, &DecimalU128Value(value))
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.0.end()
    }
}