1use crate::{
2 constants::WASM_PAGE_SIZE_BYTES,
3 declaration::AllocationDeclaration,
4 ledger::{AllocationLedger, AllocationRecord, GenerationRecord},
5 physical::CommitStoreDiagnostic,
6 policy::PolicyIdentity,
7 registry::SealedDeclarationFingerprint,
8 slot::{MemoryManagerAuthorityRecord, MemoryManagerRangeAuthority, MemoryManagerSlot},
9};
10use serde::{Deserialize, Serialize};
11
12#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
17#[serde(deny_unknown_fields)]
18pub struct DiagnosticExport {
19 pub current_generation: u64,
21 pub ledger_anchor: MemoryManagerSlot,
23 pub records: Vec<DiagnosticRecord>,
25 pub generations: Vec<GenerationRecord>,
27 #[serde(deserialize_with = "crate::cbor::deserialize_present_option")]
29 pub commit_recovery: Option<CommitStoreDiagnostic>,
30}
31
32#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
40#[serde(deny_unknown_fields)]
41pub struct DiagnosticRuntimeBinding {
42 pub policy_identity: PolicyIdentity,
44 pub declaration_fingerprint: SealedDeclarationFingerprint,
46}
47
48impl DiagnosticRuntimeBinding {
49 #[must_use]
51 pub const fn new(
52 policy_identity: PolicyIdentity,
53 declaration_fingerprint: SealedDeclarationFingerprint,
54 ) -> Self {
55 Self {
56 policy_identity,
57 declaration_fingerprint,
58 }
59 }
60}
61
62#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
74#[serde(deny_unknown_fields)]
75pub struct MemoryRuntimeDoctorReport {
76 pub bootstrapped: bool,
78 pub tested_policy_identity: Result<PolicyIdentity, DiagnosticFailure>,
80 pub tested_declaration_fingerprint: SealedDeclarationFingerprint,
82 #[serde(deserialize_with = "crate::cbor::deserialize_present_option")]
84 pub established_bootstrap_binding: Option<DiagnosticRuntimeBinding>,
85 pub bootstrap_binding: DiagnosticCheck,
88 pub ledger_anchor: MemoryManagerSlot,
90 pub stable_cell: DiagnosticStableCell,
92 #[serde(deserialize_with = "crate::cbor::deserialize_present_option")]
94 pub commit_recovery: Option<CommitStoreDiagnostic>,
95 #[serde(deserialize_with = "crate::cbor::deserialize_present_option")]
97 pub ledger: Option<DiagnosticExport>,
98 pub registered_declarations: Vec<DiagnosticDeclaration>,
100 pub range_authority: DiagnosticRangeAuthority,
103 pub validation: DiagnosticCheck,
105}
106
107#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
114#[serde(deny_unknown_fields)]
115pub struct DiagnosticDeclaration {
116 pub authority: String,
118 pub declaration: AllocationDeclaration,
120}
121
122impl DiagnosticDeclaration {
123 #[must_use]
125 pub fn new(authority: impl Into<String>, declaration: AllocationDeclaration) -> Self {
126 Self {
127 authority: authority.into(),
128 declaration,
129 }
130 }
131}
132
133#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
140pub enum DiagnosticCode {
141 #[serde(rename = "stable_cell")]
143 StableCell,
144 #[serde(rename = "unsupported_format")]
146 UnsupportedFormat,
147 #[serde(rename = "ledger_recovery")]
149 LedgerRecovery,
150 #[serde(rename = "allocation_validation")]
152 AllocationValidation,
153 #[serde(rename = "policy_identity")]
155 PolicyIdentity,
156 #[serde(rename = "runtime_binding")]
158 RuntimeBinding,
159}
160
161#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
168#[serde(deny_unknown_fields)]
169pub struct DiagnosticFailure {
170 pub code: DiagnosticCode,
172 pub message: String,
174}
175
176impl DiagnosticFailure {
177 #[must_use]
179 pub fn new(code: DiagnosticCode, message: impl Into<String>) -> Self {
180 Self {
181 code,
182 message: message.into(),
183 }
184 }
185}
186
187#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
194#[serde(deny_unknown_fields)]
195pub struct DiagnosticRangeAuthority {
196 pub registered_records: Vec<MemoryManagerAuthorityRecord>,
198 pub effective_authority: MemoryManagerRangeAuthority,
200}
201
202impl DiagnosticRangeAuthority {
203 #[must_use]
205 pub const fn new(
206 registered_records: Vec<MemoryManagerAuthorityRecord>,
207 effective_authority: MemoryManagerRangeAuthority,
208 ) -> Self {
209 Self {
210 registered_records,
211 effective_authority,
212 }
213 }
214}
215
216#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
223#[serde(deny_unknown_fields)]
224pub struct DiagnosticStableCell {
225 pub status: DiagnosticStableCellStatus,
227 pub memory_size: DiagnosticMemorySize,
229}
230
231impl DiagnosticStableCell {
232 #[must_use]
234 pub const fn new(
235 status: DiagnosticStableCellStatus,
236 memory_size: DiagnosticMemorySize,
237 ) -> Self {
238 Self {
239 status,
240 memory_size,
241 }
242 }
243}
244
245#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
252#[serde(deny_unknown_fields)]
253pub enum DiagnosticStableCellStatus {
254 Empty,
256 Readable,
258 Corrupt {
261 failure: DiagnosticFailure,
263 },
264}
265
266#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
273#[serde(deny_unknown_fields)]
274pub enum DiagnosticCheck {
275 NotRun {
277 code: DiagnosticCode,
279 message: String,
281 },
282 Passed,
284 Failed {
286 code: DiagnosticCode,
288 message: String,
290 },
291}
292
293impl DiagnosticCheck {
294 #[must_use]
296 pub const fn passed() -> Self {
297 Self::Passed
298 }
299
300 #[must_use]
302 pub fn failed(code: DiagnosticCode, message: impl Into<String>) -> Self {
303 Self::Failed {
304 code,
305 message: message.into(),
306 }
307 }
308
309 #[must_use]
311 pub fn not_run(code: DiagnosticCode, message: impl Into<String>) -> Self {
312 Self::NotRun {
313 code,
314 message: message.into(),
315 }
316 }
317}
318
319impl DiagnosticExport {
320 #[must_use]
326 pub fn from_ledger(ledger: &AllocationLedger, ledger_anchor: MemoryManagerSlot) -> Self {
327 Self::from_records(
328 ledger.current_generation,
329 ledger_anchor,
330 ledger.allocation_history.records.iter().cloned(),
331 ledger.allocation_history.generations.clone(),
332 )
333 }
334
335 pub(crate) fn from_owned_ledger(
338 ledger: AllocationLedger,
339 ledger_anchor: MemoryManagerSlot,
340 ) -> Self {
341 Self::from_records(
342 ledger.current_generation,
343 ledger_anchor,
344 ledger.allocation_history.records.into_iter(),
345 ledger.allocation_history.generations,
346 )
347 }
348
349 fn from_records(
350 current_generation: u64,
351 ledger_anchor: MemoryManagerSlot,
352 records: impl Iterator<Item = AllocationRecord>,
353 generations: Vec<GenerationRecord>,
354 ) -> Self {
355 Self {
356 current_generation,
357 ledger_anchor,
358 records: records
359 .map(|allocation| DiagnosticRecord {
360 allocation,
361 memory_size: None,
362 })
363 .collect(),
364 generations,
365 commit_recovery: None,
366 }
367 }
368}
369
370#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
375#[serde(deny_unknown_fields)]
376pub struct DiagnosticRecord {
377 pub allocation: AllocationRecord,
379 #[serde(skip_serializing_if = "Option::is_none")]
384 pub memory_size: Option<DiagnosticMemorySize>,
385}
386
387#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
394#[serde(deny_unknown_fields)]
395pub struct DiagnosticMemorySize {
396 pub wasm_pages: u64,
398 pub bytes: u64,
400}
401
402impl DiagnosticMemorySize {
403 #[must_use]
405 pub const fn from_wasm_pages(wasm_pages: u64) -> Self {
406 Self {
407 wasm_pages,
408 bytes: wasm_pages.saturating_mul(WASM_PAGE_SIZE_BYTES),
409 }
410 }
411}
412
413#[cfg(test)]
414mod tests {
415 use super::*;
416 use crate::{
417 declaration::AllocationDeclaration,
418 ledger::{AllocationHistory, AllocationRecord},
419 physical::{CommitRecoveryError, CommitSlotDiagnostic, CommitStoreDiagnostic},
420 schema::SchemaMetadata,
421 };
422
423 #[test]
424 fn diagnostic_export_copies_ledger_records() {
425 let declaration = AllocationDeclaration::new(
426 "app.users.v1",
427 MemoryManagerSlot::new(100).expect("usable slot"),
428 None,
429 SchemaMetadata::default(),
430 )
431 .expect("declaration");
432 let ledger = AllocationLedger {
433 current_generation: 3,
434 allocation_history: AllocationHistory::from_parts(
435 vec![AllocationRecord::active(3, &declaration)],
436 vec![GenerationRecord {
437 generation: 3,
438 parent_generation: 2,
439 runtime_fingerprint: Some("wasm:abc123".to_string()),
440 declaration_count: 1,
441 committed_at: None,
442 }],
443 ),
444 };
445
446 let export =
447 DiagnosticExport::from_ledger(&ledger, MemoryManagerSlot::new(0).expect("usable slot"));
448
449 assert_eq!(export.current_generation, 3);
450 assert_eq!(export.records.len(), 1);
451 assert_eq!(export.records[0].memory_size, None);
452 assert_eq!(export.generations.len(), 1);
453 assert_eq!(
454 export.ledger_anchor,
455 MemoryManagerSlot::new(0).expect("usable slot")
456 );
457 assert_eq!(export.commit_recovery, None);
458 assert_eq!(
459 export.generations,
460 ledger.allocation_history().generations()
461 );
462 assert_eq!(
463 export,
464 DiagnosticExport::from_owned_ledger(ledger, export.ledger_anchor.clone())
465 );
466 let wire = serde_json::to_value(&export).expect("diagnostic JSON");
467 assert_eq!(
468 wire["generations"][0],
469 serde_json::json!({
470 "generation": 3,
471 "parent_generation": 2,
472 "runtime_fingerprint": "wasm:abc123",
473 "declaration_count": 1,
474 "committed_at": null,
475 })
476 );
477 let decoded: DiagnosticExport = serde_json::from_value(wire).expect("current JSON shape");
478 assert_eq!(decoded, export);
479 }
480
481 #[test]
482 fn diagnostic_export_rejects_unknown_top_level_fields() {
483 use crate::test_cbor::Value;
484
485 let export = DiagnosticExport {
486 current_generation: 0,
487 ledger_anchor: MemoryManagerSlot::new(0).expect("usable slot"),
488 records: Vec::new(),
489 generations: Vec::new(),
490 commit_recovery: None,
491 };
492 let Value::Map(mut map) = crate::test_cbor::to_value(export).expect("diagnostic value")
493 else {
494 panic!("diagnostic export encodes as a map");
495 };
496 map.push((Value::Text("future_field".to_string()), Value::Bool(true)));
497 let bytes = crate::test_cbor::to_vec(&Value::Map(map)).expect("diagnostic bytes");
498
499 let err = crate::test_cbor::from_slice::<DiagnosticExport>(&bytes)
500 .expect_err("unknown diagnostic field must fail closed");
501
502 assert!(err.to_string().contains("future_field"));
503 }
504
505 #[test]
506 fn diagnostic_outcome_states_round_trip() {
507 let stable_cell = DiagnosticStableCell::new(
508 DiagnosticStableCellStatus::Corrupt {
509 failure: DiagnosticFailure::new(
510 DiagnosticCode::StableCell,
511 "bad stable-cell record",
512 ),
513 },
514 DiagnosticMemorySize::from_wasm_pages(1),
515 );
516 let range_authority =
517 DiagnosticRangeAuthority::new(Vec::new(), MemoryManagerRangeAuthority::default());
518 let check = DiagnosticCheck::failed(
519 DiagnosticCode::AllocationValidation,
520 "duplicate declaration",
521 );
522
523 for value in [DiagnosticCheck::passed(), check] {
524 let bytes = crate::test_cbor::to_vec(&value).expect("check bytes");
525 let decoded: DiagnosticCheck =
526 crate::test_cbor::from_slice(&bytes).expect("check round trip");
527 assert_eq!(decoded, value);
528 }
529
530 let bytes = crate::test_cbor::to_vec(&stable_cell).expect("stable-cell diagnostic bytes");
531 let decoded: DiagnosticStableCell =
532 crate::test_cbor::from_slice(&bytes).expect("stable-cell round trip");
533 assert_eq!(decoded, stable_cell);
534
535 let bytes = crate::test_cbor::to_vec(&range_authority).expect("range diagnostic bytes");
536 let decoded: DiagnosticRangeAuthority =
537 crate::test_cbor::from_slice(&bytes).expect("range round trip");
538 assert_eq!(decoded, range_authority);
539 }
540
541 #[test]
542 fn diagnostic_codes_have_stable_wire_names() {
543 let cases = [
544 (DiagnosticCode::StableCell, "stable_cell"),
545 (DiagnosticCode::UnsupportedFormat, "unsupported_format"),
546 (DiagnosticCode::LedgerRecovery, "ledger_recovery"),
547 (
548 DiagnosticCode::AllocationValidation,
549 "allocation_validation",
550 ),
551 (DiagnosticCode::PolicyIdentity, "policy_identity"),
552 (DiagnosticCode::RuntimeBinding, "runtime_binding"),
553 ];
554
555 for (code, expected) in cases {
556 assert_eq!(
557 crate::test_cbor::to_value(code).expect("diagnostic code value"),
558 crate::test_cbor::Value::Text(expected.to_string())
559 );
560 }
561 }
562
563 #[test]
564 fn diagnostic_export_can_include_commit_recovery_state() {
565 let ledger = AllocationLedger {
566 current_generation: 3,
567 allocation_history: AllocationHistory::default(),
568 };
569 let commit_recovery = CommitStoreDiagnostic {
570 slot0: CommitSlotDiagnostic::Valid { generation: 3 },
571 slot1: CommitSlotDiagnostic::Empty,
572 recovery: Ok(3),
573 };
574
575 let mut export =
576 DiagnosticExport::from_ledger(&ledger, MemoryManagerSlot::new(0).expect("usable slot"));
577 export.commit_recovery = Some(commit_recovery);
578
579 assert_eq!(export.commit_recovery, Some(commit_recovery));
580 }
581
582 #[test]
583 fn diagnostic_export_can_include_memory_sizes() {
584 let declaration = AllocationDeclaration::new(
585 "app.users.v1",
586 MemoryManagerSlot::new(100).expect("usable slot"),
587 None,
588 SchemaMetadata::default(),
589 )
590 .expect("declaration");
591 let ledger = AllocationLedger {
592 current_generation: 3,
593 allocation_history: AllocationHistory::from_parts(
594 vec![AllocationRecord::active(3, &declaration)],
595 Vec::new(),
596 ),
597 };
598
599 let mut export =
600 DiagnosticExport::from_ledger(&ledger, MemoryManagerSlot::new(0).expect("usable slot"));
601 export.records[0].memory_size = Some(DiagnosticMemorySize::from_wasm_pages(2));
602
603 assert_eq!(
604 export.records[0].memory_size,
605 Some(DiagnosticMemorySize {
606 wasm_pages: 2,
607 bytes: 131_072,
608 })
609 );
610 let wire = serde_json::to_value(&export).expect("diagnostic JSON");
611 assert_eq!(
612 wire["records"][0]["memory_size"],
613 serde_json::json!({"wasm_pages": 2, "bytes": 131_072})
614 );
615 }
616
617 #[test]
618 fn diagnostic_export_can_report_recovery_failure() {
619 let ledger = AllocationLedger {
620 current_generation: 0,
621 allocation_history: AllocationHistory::default(),
622 };
623 let commit_recovery = CommitStoreDiagnostic {
624 slot0: CommitSlotDiagnostic::Empty,
625 slot1: CommitSlotDiagnostic::Empty,
626 recovery: Err(CommitRecoveryError::NoValidGeneration),
627 };
628
629 let mut export =
630 DiagnosticExport::from_ledger(&ledger, MemoryManagerSlot::new(0).expect("usable slot"));
631 export.commit_recovery = Some(commit_recovery);
632
633 assert_eq!(
634 export.commit_recovery.expect("commit recovery").recovery,
635 Err(CommitRecoveryError::NoValidGeneration)
636 );
637 }
638}