Skip to main content

phi_cap/
resolved.rs

1//! The single refinement point: declaration → decision.
2//!
3//! One vocabulary, two layers (sdk-assembly §4.1):
4//!
5//! * [`CapabilitySpec`] — scoped, what a pack/plugin *declares*.
6//! * [`Capability`] — coarse, what a `Policy` *decides* on.
7//!
8//! [`class_of`] is the only translation. Policy keys never see declaration
9//! strings; declaration scopes never become decision keys. Cross-class
10//! results resolve to the highest-risk class (O7), never a blend.
11
12use crate::capability::Capability;
13use crate::spec::CapabilitySpec;
14
15/// What kind of scope a declaration carries. The registry pins exactly this
16/// ("whoever declares, pins"): paths become absolute, hosts normalized,
17/// targets/programs kept as declared.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum ScopeKind {
20    /// Filesystem paths (`fs:read`) — resolved absolute at install.
21    Paths,
22    /// Network hosts (`net:http`) — normalized (trim + lowercase) at install.
23    Hosts,
24    /// Process programs (`process:spawn`) — empty = unrestricted.
25    Programs,
26    /// Device targets (`device:input` / `device:capture`) — pinned as declared.
27    Targets,
28}
29
30/// A declaration with its scope resolved/pinned — the registry's view.
31///
32/// Scope fields survive resolution; resolution never rewrites semantics
33/// (absolute paths and normalized hosts only).
34#[derive(Debug, Clone, PartialEq, Eq)]
35#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
36#[cfg_attr(feature = "serde", serde(tag = "type", rename_all = "snake_case"))]
37pub enum ResolvedCapability {
38    /// Pinned absolute paths.
39    #[cfg_attr(feature = "serde", serde(rename = "fs:read"))]
40    FsRead {
41        /// Absolute readable paths.
42        paths: Vec<String>,
43    },
44    /// Pinned normalized hosts.
45    #[cfg_attr(feature = "serde", serde(rename = "net:http"))]
46    NetHttp {
47        /// Normalized hosts (trimmed, lowercased).
48        hosts: Vec<String>,
49    },
50    /// Pinned program allow-list.
51    #[cfg_attr(feature = "serde", serde(rename = "process:spawn"))]
52    ProcessSpawn {
53        /// Programs; empty = unrestricted.
54        programs: Vec<String>,
55    },
56    /// Pinned input targets.
57    #[cfg_attr(feature = "serde", serde(rename = "device:input"))]
58    DeviceInput {
59        /// Input targets as declared.
60        targets: Vec<String>,
61    },
62    /// Pinned capture targets.
63    #[cfg_attr(feature = "serde", serde(rename = "device:capture"))]
64    DeviceCapture {
65        /// Capture targets as declared.
66        targets: Vec<String>,
67    },
68}
69
70impl ResolvedCapability {
71    /// The decision class this pinned declaration resolves to.
72    pub const fn capability(&self) -> Capability {
73        match self {
74            Self::FsRead { .. } => Capability::ReadFs,
75            Self::NetHttp { .. } => Capability::Net,
76            Self::ProcessSpawn { .. } => Capability::Process,
77            Self::DeviceInput { .. } | Self::DeviceCapture { .. } => Capability::Device,
78        }
79    }
80}
81
82/// The scope kind a declaration carries.
83pub const fn scope_kind(spec: &CapabilitySpec) -> ScopeKind {
84    match spec {
85        CapabilitySpec::FsRead(_) => ScopeKind::Paths,
86        CapabilitySpec::NetHttp(_) => ScopeKind::Hosts,
87        CapabilitySpec::ProcessSpawn(_) => ScopeKind::Programs,
88        CapabilitySpec::DeviceInput(_) | CapabilitySpec::DeviceCapture(_) => ScopeKind::Targets,
89    }
90}
91
92/// **The** refinement: declaration → decision class (sdk-assembly §4.1).
93///
94/// Everything that needs a `Policy` key goes through here — nothing
95/// re-implements the match, nothing invents a third vocabulary.
96pub const fn class_of(spec: &CapabilitySpec) -> Capability {
97    match spec {
98        CapabilitySpec::FsRead(_) => Capability::ReadFs,
99        CapabilitySpec::NetHttp(_) => Capability::Net,
100        CapabilitySpec::ProcessSpawn(_) => Capability::Process,
101        CapabilitySpec::DeviceInput(_) | CapabilitySpec::DeviceCapture(_) => Capability::Device,
102    }
103}
104
105/// Resolve a declaration into its pinned form — scope is preserved verbatim
106/// here; path/host pinning is the installer's job (see agent-works
107/// `resolve_capabilities`, which this type set sinks from).
108pub fn resolve(spec: &CapabilitySpec) -> ResolvedCapability {
109    match spec.clone() {
110        CapabilitySpec::FsRead(s) => ResolvedCapability::FsRead { paths: s.paths },
111        CapabilitySpec::NetHttp(s) => ResolvedCapability::NetHttp { hosts: s.hosts },
112        CapabilitySpec::ProcessSpawn(s) => ResolvedCapability::ProcessSpawn {
113            programs: s.programs,
114        },
115        CapabilitySpec::DeviceInput(s) => ResolvedCapability::DeviceInput { targets: s.targets },
116        CapabilitySpec::DeviceCapture(s) => {
117            ResolvedCapability::DeviceCapture { targets: s.targets }
118        }
119    }
120}
121
122/// Collapse a multi-capability method to one policy key (O7): take the
123/// **highest-risk** class — a method that both reads files and spawns
124/// processes is decided as `process`.
125pub fn class_of_all(specs: &[CapabilitySpec]) -> Capability {
126    specs.iter().map(class_of).max().unwrap_or(Capability::Pure)
127}
128
129#[cfg(test)]
130mod tests {
131    use super::*;
132
133    #[test]
134    fn class_of_is_total_on_every_variant() {
135        let specs = [
136            crate::spec::fs_read(&["data"]),
137            crate::spec::net_http(&["api.example.com"]),
138            crate::spec::process_spawn(),
139            crate::spec::device_input(&["window:editor"]),
140            crate::spec::device_capture(&["window:editor"]),
141        ];
142        let classes: Vec<Capability> = specs.iter().map(class_of).collect();
143        assert_eq!(
144            classes,
145            [
146                Capability::ReadFs,
147                Capability::Net,
148                Capability::Process,
149                Capability::Device,
150                Capability::Device
151            ]
152        );
153    }
154}