Skip to main content

phi_cap/
spec.rs

1//! Declaration layer: scoped capability specs — the only vocabulary a pack
2//! or plugin ever writes (sdk-assembly §4.1).
3//!
4//! Two layers, one refinement:
5//!
6//! ```text
7//! CapabilitySpec (scoped, declaration)  --class_of-->  Capability (coarse, decision)
8//!   fs:read{paths}                                       read_fs
9//!   net:http{hosts}                                      net
10//!   device:input{targets}                                device
11//! ```
12//!
13//! Shape law (O13 decision): the structure is ours — scope-first variants that
14//! match the existing `fs:read` / `net:http` wire format — while fine-grained
15//! *tokens* inside a scope may adopt upstream vocabularies (e.g. cua's dotted
16//! capability names) at the adapter boundary. Upstream strings never become
17//! the public standard.
18
19/// Readable directories/files (resolved to absolute paths and pinned by the
20/// registry at install time — "whoever declares, pins").
21///
22/// Wire form `fs:read`; extra fields rejected (strict oneOf law).
23#[derive(Debug, Clone, PartialEq, Eq)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
26pub struct FsReadSpec {
27    /// Readable directories/files, relative to the declaring root.
28    pub paths: Vec<String>,
29}
30
31/// Outbound HTTP host allow-list (normalized and pinned at install time).
32///
33/// Wire form `net:http`; extra fields rejected (strict oneOf law).
34#[derive(Debug, Clone, PartialEq, Eq)]
35#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
36#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
37pub struct NetHttpSpec {
38    /// Outbound HTTP hosts; off-list hosts are rejected at request time.
39    pub hosts: Vec<String>,
40}
41
42/// Process spawn face (`process:spawn`).
43///
44/// `programs` narrows the declaration to named programs; empty means the
45/// unrestricted process face (a shell runs whatever it is given — the
46/// honest declaration for `sh.run`). Policy still decides per call.
47#[derive(Debug, Clone, PartialEq, Eq, Default)]
48#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
49#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
50pub struct ProcessSpawnSpec {
51    /// Program allow-list; empty = unrestricted.
52    #[cfg_attr(
53        feature = "serde",
54        serde(default, skip_serializing_if = "Vec::is_empty")
55    )]
56    pub programs: Vec<String>,
57}
58
59/// Device input face (`device:input`): pointer / keyboard / touch injection.
60///
61/// `targets` is the scope that gets pinned — "can only touch these windows /
62/// screens". Fine-grained upstream verb tokens (cua's `input.pointer.click`
63/// and friends) land in `verb` at P3; the variant shape is already stable.
64#[derive(Debug, Clone, PartialEq, Eq)]
65#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
66#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
67pub struct DeviceInputSpec {
68    /// Input targets, e.g. `["window:editor"]` / `["screen:main"]`.
69    pub targets: Vec<String>,
70}
71
72/// Device capture face (`device:capture`): screenshots and friends.
73///
74/// Scope is mandatory at the policy layer (P3: an empty target list must be
75/// rejected) — capturing outside the declared targets is an overreach.
76#[derive(Debug, Clone, PartialEq, Eq)]
77#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
78#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
79pub struct DeviceCaptureSpec {
80    /// Capture targets, e.g. `["window:editor"]`.
81    pub targets: Vec<String>,
82}
83
84/// Capability allow-list entry — the declaration form (fail-closed:
85/// undeclared = not granted).
86///
87/// Every shape that declares capabilities uses this one enum: wasm plugin
88/// manifests, capability packs' `MethodSpec.caps`, the plugin `sdk` form.
89/// Wire tags keep the dotted `face:scope` names.
90#[derive(Debug, Clone, PartialEq, Eq)]
91#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
92#[cfg_attr(feature = "serde", serde(tag = "type", rename_all = "snake_case"))]
93pub enum CapabilitySpec {
94    /// Readable directories/files (pinned as absolute paths at install).
95    #[cfg_attr(feature = "serde", serde(rename = "fs:read"))]
96    FsRead(FsReadSpec),
97    /// Outbound HTTP host allow-list (pinned normalized at install).
98    #[cfg_attr(feature = "serde", serde(rename = "net:http"))]
99    NetHttp(NetHttpSpec),
100    /// Process spawn face.
101    #[cfg_attr(feature = "serde", serde(rename = "process:spawn"))]
102    ProcessSpawn(ProcessSpawnSpec),
103    /// Device input face (pointer / keyboard / touch).
104    #[cfg_attr(feature = "serde", serde(rename = "device:input"))]
105    DeviceInput(DeviceInputSpec),
106    /// Device capture face (screenshots and friends).
107    #[cfg_attr(feature = "serde", serde(rename = "device:capture"))]
108    DeviceCapture(DeviceCaptureSpec),
109}
110
111/// `fs:read{paths}` — helper for declarations.
112pub fn fs_read(paths: &[&str]) -> CapabilitySpec {
113    CapabilitySpec::FsRead(FsReadSpec {
114        paths: paths.iter().map(|s| (*s).to_string()).collect(),
115    })
116}
117
118/// `net:http{hosts}` — helper for declarations.
119pub fn net_http(hosts: &[&str]) -> CapabilitySpec {
120    CapabilitySpec::NetHttp(NetHttpSpec {
121        hosts: hosts.iter().map(|s| (*s).to_string()).collect(),
122    })
123}
124
125/// `process:spawn{}` (unrestricted) — helper for declarations.
126pub fn process_spawn() -> CapabilitySpec {
127    CapabilitySpec::ProcessSpawn(ProcessSpawnSpec::default())
128}
129
130/// `process:spawn{programs}` — helper for declarations.
131pub fn process_spawn_programs(programs: &[&str]) -> CapabilitySpec {
132    CapabilitySpec::ProcessSpawn(ProcessSpawnSpec {
133        programs: programs.iter().map(|s| (*s).to_string()).collect(),
134    })
135}
136
137/// `device:input{targets}` — helper for declarations.
138pub fn device_input(targets: &[&str]) -> CapabilitySpec {
139    CapabilitySpec::DeviceInput(DeviceInputSpec {
140        targets: targets.iter().map(|s| (*s).to_string()).collect(),
141    })
142}
143
144/// `device:capture{targets}` — helper for declarations.
145pub fn device_capture(targets: &[&str]) -> CapabilitySpec {
146    CapabilitySpec::DeviceCapture(DeviceCaptureSpec {
147        targets: targets.iter().map(|s| (*s).to_string()).collect(),
148    })
149}