Skip to main content

qualia_core_db/q42/app_registry/
record.rs

1//! Installed-app record types (read-only inspection surface).
2
3use crate::q42::app_manifest::{PermissionIntent, PermissionKind};
4
5/// Lifecycle / admission state for a registry slot.
6///
7/// The registry never launches or executes an app in any of these states;
8/// inspection is always read-only.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum AppRecordState {
11    /// Decoded, integrity ok (when checked), engine compatible.
12    Active,
13    /// Malformed, integrity mismatch, or other fail-closed admission issue.
14    Quarantined { reason: String },
15    /// Manifest requires a newer (or older) engine than this registry instance.
16    Incompatible,
17}
18
19impl AppRecordState {
20    /// `true` when active.
21    pub fn is_active(&self) -> bool {
22        matches!(self, Self::Active)
23    }
24
25    /// `true` when `quarantined`.
26    pub fn is_quarantined(&self) -> bool {
27        matches!(self, Self::Quarantined { .. })
28    }
29}
30
31/// Compact permission-intent summary for list/get views.
32#[derive(Debug, Clone, PartialEq, Eq, Default)]
33pub struct PermissionIntentSummary {
34    /// Distinct permission kind tags (`PermissionKind::as_str`).
35    pub kinds: Vec<&'static str>,
36    pub total: usize,
37    pub required: usize,
38}
39
40impl PermissionIntentSummary {
41    /// Builds `PermissionIntentSummary` from `intents`.
42    pub fn from_intents(intents: &[PermissionIntent]) -> Self {
43        let mut kinds = Vec::new();
44        let mut required = 0usize;
45        for intent in intents {
46            let tag = intent.kind.as_str();
47            if !kinds.contains(&tag) {
48                kinds.push(tag);
49            }
50            if !intent.optional {
51                required += 1;
52            }
53        }
54        // Stable order by wire privilege rank then tag.
55        kinds.sort_by_key(|k| {
56            PermissionKind::parse(k)
57                .map(|pk| (pk.privilege_rank(), pk.as_str()))
58                .unwrap_or((255, *k))
59        });
60        Self {
61            kinds,
62            total: intents.len(),
63            required,
64        }
65    }
66}
67
68/// One installed (or quarantined / incompatible) app slot.
69#[derive(Debug, Clone, PartialEq, Eq)]
70pub struct InstalledAppRecord {
71    pub app_id: String,
72    pub version: String,
73    /// Package SHA-256 from the manifest integrity block (may be zero when unknown).
74    pub digest: [u8; 32],
75    pub permission_intents: PermissionIntentSummary,
76    /// `true` when a caller-supplied package digest matched (or no digest was supplied).
77    pub integrity_ok: bool,
78    pub state: AppRecordState,
79}
80
81/// Read-only inspection view — never mutates registry state and never launches.
82#[derive(Debug, Clone, PartialEq, Eq)]
83pub struct AppInspectView {
84    pub app_id: String,
85    pub version: String,
86    pub author_name: String,
87    pub author_did: String,
88    pub digest: [u8; 32],
89    pub integrity_ok: bool,
90    pub state: AppRecordState,
91    pub permission_intents: Vec<PermissionIntent>,
92    pub permission_summary: PermissionIntentSummary,
93    pub entry_ids: Vec<String>,
94    pub min_engine_version: String,
95    pub max_engine_version: String,
96    pub required_features: Vec<String>,
97}