rx4 0.3.0

The agent harness engine — loop, tools, providers, sessions, permissions, computer-use, with full pi protocol compatibility
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
//! Pi extension protocol — manifest, manager, runtime trait.
//!
//! Compatible with pi_agent_rust extension system:
//! - Extensions declare capabilities in a manifest
//! - Capability policy gates each hostcall
//! - Runtime can be QuickJS (in-process) or subprocess
//! - Trust-state lifecycle: pending → acknowledged → trusted → killed

use crate::pi::policy::{PiCapability, PiCapabilityPolicy, PolicyDecision};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::sync::Arc;
use tracing::{info, warn};

/// Extension manifest — describes a pi extension's metadata and capabilities.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PiExtensionManifest {
    pub id: String,
    pub name: String,
    pub version: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub author: Option<String>,
    #[serde(default)]
    pub capabilities: Vec<String>,
    #[serde(default)]
    pub tools: Vec<String>,
    #[serde(default)]
    pub providers: Vec<String>,
    #[serde(default)]
    pub entry: String,
    #[serde(default)]
    pub homepage: Option<String>,
    #[serde(default)]
    pub license: Option<String>,
}

impl PiExtensionManifest {
    /// Load from a manifest.json file.
    pub fn load(path: &std::path::Path) -> Result<Self, ExtensionError> {
        let content =
            std::fs::read_to_string(path).map_err(|e| ExtensionError::LoadFailed(e.to_string()))?;
        let manifest: Self = serde_json::from_str(&content)
            .map_err(|e| ExtensionError::ParseFailed(e.to_string()))?;
        Ok(manifest)
    }

    /// Load from a directory (looks for manifest.json or package.json).
    pub fn load_from_dir(dir: &std::path::Path) -> Result<Self, ExtensionError> {
        let manifest_path = dir.join("manifest.json");
        if manifest_path.exists() {
            return Self::load(&manifest_path);
        }
        let pkg_path = dir.join("package.json");
        if pkg_path.exists() {
            let content = std::fs::read_to_string(&pkg_path)
                .map_err(|e| ExtensionError::LoadFailed(e.to_string()))?;
            let pkg: serde_json::Value = serde_json::from_str(&content)
                .map_err(|e| ExtensionError::ParseFailed(e.to_string()))?;
            let id = pkg
                .get("name")
                .and_then(|v| v.as_str())
                .unwrap_or("unknown")
                .to_string();
            let version = pkg
                .get("version")
                .and_then(|v| v.as_str())
                .unwrap_or("0.0.0")
                .to_string();
            let description = pkg
                .get("description")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            let entry = pkg
                .get("main")
                .and_then(|v| v.as_str())
                .unwrap_or("index.js")
                .to_string();
            let caps = pkg
                .get("capabilities")
                .and_then(|v| v.as_array())
                .map(|a| {
                    a.iter()
                        .filter_map(|v| v.as_str().map(String::from))
                        .collect()
                })
                .unwrap_or_default();
            let tools = pkg
                .get("tools")
                .and_then(|v| v.as_array())
                .map(|a| {
                    a.iter()
                        .filter_map(|v| v.as_str().map(String::from))
                        .collect()
                })
                .unwrap_or_default();
            let name = id.clone();
            return Ok(Self {
                id,
                name,
                version,
                description,
                author: None,
                capabilities: caps,
                tools,
                providers: vec![],
                entry,
                homepage: None,
                license: None,
            });
        }
        Err(ExtensionError::NotFound(
            "no manifest.json or package.json".into(),
        ))
    }

    pub fn has_capability(&self, cap: PiCapability) -> bool {
        self.capabilities.iter().any(|c| c == cap.as_str())
    }
}

/// Trust state for an extension (pi lifecycle).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrustState {
    Pending,
    Acknowledged,
    Trusted,
    Killed,
}

/// A loaded pi extension.
pub struct PiExtension {
    pub manifest: PiExtensionManifest,
    pub dir: PathBuf,
    pub trust: TrustState,
    pub policy: PiCapabilityPolicy,
}

impl PiExtension {
    pub fn new(manifest: PiExtensionManifest, dir: PathBuf) -> Self {
        Self {
            manifest,
            dir,
            trust: TrustState::Pending,
            policy: PiCapabilityPolicy::default(),
        }
    }

    pub fn check_capability(&self, cap: PiCapability) -> PolicyDecision {
        self.policy.check(&self.manifest.id, cap)
    }

    pub fn acknowledge(&mut self) {
        self.trust = TrustState::Acknowledged;
        info!("extension {} acknowledged", self.manifest.id);
    }

    pub fn trust(&mut self) {
        self.trust = TrustState::Trusted;
        info!("extension {} trusted", self.manifest.id);
    }

    pub fn kill(&mut self) {
        self.trust = TrustState::Killed;
        warn!("extension {} killed", self.manifest.id);
    }

    pub fn is_alive(&self) -> bool {
        matches!(self.trust, TrustState::Acknowledged | TrustState::Trusted)
    }
}

/// Runtime trait for executing extension code.
/// Implementations can be QuickJS (in-process) or subprocess-based.
#[async_trait::async_trait]
pub trait PiExtensionRuntime: Send + Sync {
    /// Initialize the extension runtime.
    async fn init(&self, ext: &PiExtension) -> Result<(), ExtensionError>;

    /// Execute a hostcall from the extension.
    async fn hostcall(
        &self,
        ext: &PiExtension,
        cap: PiCapability,
        method: &str,
        args: serde_json::Value,
    ) -> Result<serde_json::Value, ExtensionError>;

    /// Execute a tool defined by the extension.
    async fn execute_tool(
        &self,
        ext: &PiExtension,
        tool_name: &str,
        args: &str,
    ) -> Result<String, ExtensionError>;

    /// Shutdown the runtime for this extension.
    async fn shutdown(&self, ext: &PiExtension) -> Result<(), ExtensionError>;
}

/// Stub runtime — does nothing, returns errors. Used when pi-extensions feature is off.
pub struct StubRuntime;

#[async_trait::async_trait]
impl PiExtensionRuntime for StubRuntime {
    async fn init(&self, _ext: &PiExtension) -> Result<(), ExtensionError> {
        Err(ExtensionError::RuntimeUnavailable(
            "pi-extensions feature not enabled".into(),
        ))
    }
    async fn hostcall(
        &self,
        _ext: &PiExtension,
        _cap: PiCapability,
        _method: &str,
        _args: serde_json::Value,
    ) -> Result<serde_json::Value, ExtensionError> {
        Err(ExtensionError::RuntimeUnavailable(
            "pi-extensions feature not enabled".into(),
        ))
    }
    async fn execute_tool(
        &self,
        _ext: &PiExtension,
        _tool: &str,
        _args: &str,
    ) -> Result<String, ExtensionError> {
        Err(ExtensionError::RuntimeUnavailable(
            "pi-extensions feature not enabled".into(),
        ))
    }
    async fn shutdown(&self, _ext: &PiExtension) -> Result<(), ExtensionError> {
        Ok(())
    }
}

/// Extension manager — discovers, loads, and manages pi extensions.
pub struct PiExtensionManager {
    extensions: DashMap<String, PiExtension>,
    runtime: Arc<dyn PiExtensionRuntime>,
    extensions_dir: PathBuf,
}

impl PiExtensionManager {
    pub fn new(extensions_dir: PathBuf) -> Self {
        Self {
            extensions: DashMap::new(),
            runtime: Arc::new(StubRuntime),
            extensions_dir,
        }
    }

    pub fn with_runtime(mut self, runtime: Arc<dyn PiExtensionRuntime>) -> Self {
        self.runtime = runtime;
        self
    }

    /// Discover extensions in the extensions directory.
    pub fn discover(&self) -> Vec<PiExtensionManifest> {
        let mut found = Vec::new();
        if !self.extensions_dir.exists() {
            return found;
        }
        if let Ok(entries) = std::fs::read_dir(&self.extensions_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.is_dir() {
                    match PiExtensionManifest::load_from_dir(&path) {
                        Ok(manifest) => {
                            info!(
                                "discovered extension: {} v{}",
                                manifest.id, manifest.version
                            );
                            found.push(manifest);
                        }
                        Err(e) => warn!("skipped extension at {}: {e}", path.display()),
                    }
                }
            }
        }
        found
    }

    /// Load and register an extension from a manifest.
    pub fn load(&self, manifest: PiExtensionManifest) -> Result<(), ExtensionError> {
        let dir = self.extensions_dir.join(&manifest.id);
        let ext = PiExtension::new(manifest, dir);
        let id = ext.manifest.id.clone();
        self.extensions.insert(id, ext);
        Ok(())
    }

    /// Get an extension by ID.
    pub fn get(&self, id: &str) -> Option<dashmap::mapref::one::Ref<'_, String, PiExtension>> {
        self.extensions.get(id)
    }

    /// Check if an extension is allowed a capability.
    pub fn check_capability(&self, ext_id: &str, cap: PiCapability) -> PolicyDecision {
        match self.extensions.get(ext_id) {
            Some(ext) => ext.check_capability(cap),
            None => PolicyDecision::Deny,
        }
    }

    /// Initialize an extension's runtime.
    pub async fn init_extension(&self, ext_id: &str) -> Result<(), ExtensionError> {
        let ext = self
            .extensions
            .get(ext_id)
            .ok_or_else(|| ExtensionError::NotFound(ext_id.into()))?;
        if !ext.is_alive() {
            return Err(ExtensionError::NotTrusted(ext_id.into()));
        }
        self.runtime.init(&ext).await
    }

    /// Execute a tool from an extension.
    pub async fn execute_tool(
        &self,
        ext_id: &str,
        tool_name: &str,
        args: &str,
    ) -> Result<String, ExtensionError> {
        let ext = self
            .extensions
            .get(ext_id)
            .ok_or_else(|| ExtensionError::NotFound(ext_id.into()))?;
        if !ext.is_alive() {
            return Err(ExtensionError::NotTrusted(ext_id.into()));
        }
        if !ext.manifest.tools.iter().any(|t| t == tool_name) {
            return Err(ExtensionError::ToolNotFound(tool_name.into()));
        }
        self.runtime.execute_tool(&ext, tool_name, args).await
    }

    /// List all registered extensions.
    pub fn list(&self) -> Vec<(String, String, TrustState)> {
        self.extensions
            .iter()
            .map(|e| (e.manifest.id.clone(), e.manifest.name.clone(), e.trust))
            .collect()
    }

    /// Count of registered extensions.
    pub fn count(&self) -> usize {
        self.extensions.len()
    }

    /// Acknowledge an extension (transition to acknowledged state).
    pub fn acknowledge(&self, ext_id: &str) -> Result<(), ExtensionError> {
        let mut ext = self
            .extensions
            .get_mut(ext_id)
            .ok_or_else(|| ExtensionError::NotFound(ext_id.into()))?;
        ext.acknowledge();
        Ok(())
    }

    /// Trust an extension (transition to trusted state).
    pub fn trust(&self, ext_id: &str) -> Result<(), ExtensionError> {
        let mut ext = self
            .extensions
            .get_mut(ext_id)
            .ok_or_else(|| ExtensionError::NotFound(ext_id.into()))?;
        ext.trust();
        Ok(())
    }

    /// Kill an extension (transition to killed state).
    pub fn kill(&self, ext_id: &str) -> Result<(), ExtensionError> {
        let mut ext = self
            .extensions
            .get_mut(ext_id)
            .ok_or_else(|| ExtensionError::NotFound(ext_id.into()))?;
        ext.kill();
        Ok(())
    }
}

#[derive(Debug, thiserror::Error)]
pub enum ExtensionError {
    #[error("extension not found: {0}")]
    NotFound(String),
    #[error("extension not trusted: {0}")]
    NotTrusted(String),
    #[error("tool not found: {0}")]
    ToolNotFound(String),
    #[error("load failed: {0}")]
    LoadFailed(String),
    #[error("parse failed: {0}")]
    ParseFailed(String),
    #[error("runtime unavailable: {0}")]
    RuntimeUnavailable(String),
    #[error("execution error: {0}")]
    ExecutionError(String),
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn manifest_from_json() {
        let json = r#"{
            "id": "test-ext",
            "name": "Test Extension",
            "version": "1.0.0",
            "description": "A test",
            "capabilities": ["read", "tool"],
            "tools": ["my_tool"],
            "entry": "index.js"
        }"#;
        let manifest: PiExtensionManifest = serde_json::from_str(json).unwrap();
        assert_eq!(manifest.id, "test-ext");
        assert!(manifest.has_capability(PiCapability::Read));
        assert!(!manifest.has_capability(PiCapability::Exec));
    }

    #[test]
    fn manager_discover_and_load() {
        let tmp = TempDir::new().unwrap();
        let ext_dir = tmp.path().join("extensions");
        let test_ext_dir = ext_dir.join("test-ext");
        std::fs::create_dir_all(&test_ext_dir).unwrap();
        std::fs::write(
            test_ext_dir.join("manifest.json"),
            r#"{"id":"test-ext","name":"Test","version":"1.0.0","capabilities":["read"],"tools":["foo"],"entry":"index.js"}"#,
        ).unwrap();

        let manager = PiExtensionManager::new(ext_dir);
        let mut found = manager.discover();
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].id, "test-ext");

        manager.load(found.remove(0)).unwrap();
        assert_eq!(manager.count(), 1);
        assert_eq!(
            manager.check_capability("test-ext", PiCapability::Read),
            PolicyDecision::Allow
        );
    }

    #[test]
    fn trust_lifecycle() {
        let tmp = TempDir::new().unwrap();
        let manager = PiExtensionManager::new(tmp.path().into());
        let manifest = PiExtensionManifest {
            id: "ext1".into(),
            name: "Ext1".into(),
            version: "1.0".into(),
            description: "".into(),
            author: None,
            capabilities: vec![],
            tools: vec![],
            providers: vec![],
            entry: "index.js".into(),
            homepage: None,
            license: None,
        };
        manager.load(manifest).unwrap();

        assert!(manager.get("ext1").is_some());
        manager.acknowledge("ext1").unwrap();
        manager.trust("ext1").unwrap();
        assert!(manager.get("ext1").unwrap().is_alive());

        manager.kill("ext1").unwrap();
        assert!(!manager.get("ext1").unwrap().is_alive());
    }
}