anamdb 1.0.0

AnamDB — the AI-native, differentiable logic kernel for autonomous agents.
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! AI-Tables Community Hub — package manager for FAO models and Datalog packs.
//!
//! Provides a centralized registry where developers can publish, discover,
//! and install pre-trained FAO (Function-as-Operator) model packages and
//! Datalog logic constraint packs.
//!
//! ## Design
//! - **Local index**: a JSON manifest stored in `~/.anam/hub/index.json`
//! - **Remote index**: fetched from a registry URL (defaults to the official hub)
//! - **Pack format**: a `.anampack` file (ZIP) containing:
//!     - `manifest.json` — metadata (name, version, description, author)
//!     - `models/` — ONNX model files
//!     - `rules/` — Datalog rule files (`.dl`)
//!     - `README.md` — usage instructions
//!
//! ## CLI Usage
//! ```bash
//! anam hub search fraud
//! anam hub install anamdb/financial-compliance-pack@1.0.0
//! anam hub publish ./my-pack/
//! anam hub list
//! ```
//!
//! ## SQL Usage
//! ```sql
//! SELECT hub_install('anamdb/financial-compliance-pack') AS result;
//! SELECT hub_search('fraud') AS packs;
//! ```

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use tracing::info;

use crate::core::error::{AnamError, Result};

// ── Pack Manifest ─────────────────────────────────────────────────────

/// Semantic version string (major.minor.patch).
pub type Version = String;

/// Metadata for a published AI-Tables pack.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PackManifest {
    /// Unique package name (e.g., "anamdb/financial-compliance").
    pub name: String,
    /// Semantic version.
    pub version: Version,
    /// Short human-readable description.
    pub description: String,
    /// Author / organization.
    pub author: String,
    /// License identifier (e.g., "Apache-2.0", "MIT").
    pub license: String,
    /// Tags for discovery (e.g., ["fraud", "finance", "classification"]).
    pub tags: Vec<String>,
    /// ONNX model files included in this pack.
    pub models: Vec<PackModel>,
    /// Datalog rule files included in this pack.
    pub rules: Vec<PackRule>,
    /// Compatible AnamDB version range.
    pub anamdb_version: String,
    /// SHA-256 checksum of the pack file.
    pub checksum: Option<String>,
}

/// A model entry within a pack manifest.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PackModel {
    /// Model name (becomes the SQL function name on install).
    pub name: String,
    /// Relative path to ONNX file within the pack.
    pub path: String,
    /// Number of input features.
    pub num_features: usize,
    /// Average inference latency in milliseconds.
    pub avg_latency_ms: f64,
    /// Benchmark accuracy.
    pub accuracy: f64,
}

/// A Datalog rule file within a pack manifest.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PackRule {
    /// Rule set name.
    pub name: String,
    /// Relative path to `.dl` file within the pack.
    pub path: String,
    /// Short description of what the rules enforce.
    pub description: String,
}

// ── Hub Index ─────────────────────────────────────────────────────────

/// The local hub index — a map of `name@version` → manifest.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct HubIndex {
    /// Installed packs, keyed by `name@version`.
    pub installed: HashMap<String, PackManifest>,
    /// Available remote packs (fetched from registry URL).
    pub available: HashMap<String, PackManifest>,
    /// Registry URL.
    pub registry_url: String,
}

impl HubIndex {
    /// Create a new empty index with the given registry URL.
    pub fn new(registry_url: &str) -> Self {
        Self {
            registry_url: registry_url.to_string(),
            ..Default::default()
        }
    }

    fn pack_key(name: &str, version: &str) -> String {
        format!("{name}@{version}")
    }
}

// ── Hub Client ────────────────────────────────────────────────────────

/// The AI-Tables Hub client — manages discovery, installation, and publishing.
pub struct HubClient {
    /// Path to the local hub directory (default: `~/.anam/hub/`).
    pub hub_dir: PathBuf,
    /// Loaded index.
    pub index: HubIndex,
    /// Registry base URL.
    pub registry_url: String,
}

impl HubClient {
    /// The default official registry URL.
    pub const DEFAULT_REGISTRY: &'static str = "https://jorge-nexsys.github.io/anam/registry";

    /// Create a new hub client rooted at `hub_dir`.
    pub fn new(hub_dir: impl AsRef<Path>) -> Result<Self> {
        let hub_dir = hub_dir.as_ref().to_path_buf();
        std::fs::create_dir_all(&hub_dir).map_err(AnamError::Io)?;

        let index_path = hub_dir.join("index.json");
        let index = if index_path.exists() {
            let raw = std::fs::read_to_string(&index_path).map_err(AnamError::Io)?;
            serde_json::from_str(&raw).map_err(|e| AnamError::Serde(e.to_string()))?
        } else {
            HubIndex::new(Self::DEFAULT_REGISTRY)
        };

        Ok(Self {
            registry_url: index.registry_url.clone(),
            hub_dir,
            index,
        })
    }

    /// Persist the current index to disk.
    pub fn save_index(&self) -> Result<()> {
        let index_path = self.hub_dir.join("index.json");
        let raw = serde_json::to_string_pretty(&self.index)
            .map_err(|e| AnamError::Serde(e.to_string()))?;
        std::fs::write(&index_path, raw).map_err(AnamError::Io)?;
        Ok(())
    }

    // ── Search ────────────────────────────────────────────────────────

    /// Search available packs by keyword (matches name, description, or tags).
    pub fn search(&self, keyword: &str) -> Vec<&PackManifest> {
        let kw = keyword.to_lowercase();
        let mut results: Vec<&PackManifest> = self
            .index
            .available
            .values()
            .filter(|m| {
                m.name.to_lowercase().contains(&kw)
                    || m.description.to_lowercase().contains(&kw)
                    || m.tags.iter().any(|t| t.to_lowercase().contains(&kw))
            })
            .collect();
        results.sort_by(|a, b| a.name.cmp(&b.name));
        results
    }

    // ── Install ───────────────────────────────────────────────────────

    /// Install a pack by `name@version` string.
    ///
    /// In production this fetches the `.anampack` from the registry,
    /// verifies the checksum, extracts models/rules, and registers them.
    /// Here we record the installation in the local index.
    pub fn install(&mut self, pack_ref: &str) -> Result<InstallResult> {
        info!(pack = %pack_ref, "installing pack from hub");

        // Parse "name@version" or just "name" (resolves to latest).
        let (name, version) = parse_pack_ref(pack_ref)?;

        // Check if already installed.
        let key = HubIndex::pack_key(&name, &version);
        if self.index.installed.contains_key(&key) {
            return Ok(InstallResult {
                pack_ref: pack_ref.to_string(),
                installed: false,
                message: format!("'{pack_ref}' is already installed"),
            });
        }

        // Find in available index (or create a placeholder for demo).
        let manifest = self
            .index
            .available
            .get(&key)
            .cloned()
            .unwrap_or_else(|| PackManifest {
                name: name.clone(),
                version: version.clone(),
                description: format!("Community pack: {name}"),
                author: "community".into(),
                license: "Apache-2.0".into(),
                tags: vec![name.split('/').next_back().unwrap_or("").to_string()],
                models: vec![],
                rules: vec![],
                anamdb_version: ">=0.1.0".into(),
                checksum: None,
            });

        // Record as installed.
        self.index.installed.insert(key.clone(), manifest);
        self.save_index()?;

        info!(pack = %pack_ref, "pack installed successfully");

        Ok(InstallResult {
            pack_ref: pack_ref.to_string(),
            installed: true,
            message: format!("'{pack_ref}' installed successfully"),
        })
    }

    // ── Publish ───────────────────────────────────────────────────────

    /// Publish a local pack to the hub.
    ///
    /// Reads the manifest from `pack_dir/manifest.json`, validates it,
    /// and adds it to the local available index (simulating a registry push).
    pub fn publish(&mut self, pack_dir: impl AsRef<Path>) -> Result<PublishResult> {
        let pack_dir = pack_dir.as_ref();
        let manifest_path = pack_dir.join("manifest.json");

        if !manifest_path.exists() {
            return Err(AnamError::Logic(format!(
                "manifest.json not found in {}",
                pack_dir.display()
            )));
        }

        let raw = std::fs::read_to_string(&manifest_path).map_err(AnamError::Io)?;
        let manifest: PackManifest =
            serde_json::from_str(&raw).map_err(|e| AnamError::Serde(e.to_string()))?;

        let key = HubIndex::pack_key(&manifest.name, &manifest.version);
        info!(pack = %key, "publishing pack to hub");

        self.index.available.insert(key.clone(), manifest.clone());
        self.save_index()?;

        Ok(PublishResult {
            name: manifest.name,
            version: manifest.version,
            key,
            message: "Pack published successfully".into(),
        })
    }

    // ── List ──────────────────────────────────────────────────────────

    /// List all installed packs.
    pub fn list_installed(&self) -> Vec<&PackManifest> {
        let mut packs: Vec<&PackManifest> = self.index.installed.values().collect();
        packs.sort_by(|a, b| a.name.cmp(&b.name));
        packs
    }

    /// List all available packs.
    pub fn list_available(&self) -> Vec<&PackManifest> {
        let mut packs: Vec<&PackManifest> = self.index.available.values().collect();
        packs.sort_by(|a, b| a.name.cmp(&b.name));
        packs
    }

    /// Seed the available index with well-known community packs.
    pub fn seed_community_packs(&mut self) -> Result<()> {
        let packs = vec![
            PackManifest {
                name: "anamdb/financial-compliance".into(),
                version: "1.0.0".into(),
                description: "AML/KYC compliance rules + fraud detection models for financial data".into(),
                author: "AnamDB Core Team".into(),
                license: "Apache-2.0".into(),
                tags: vec!["fraud".into(), "finance".into(), "aml".into(), "kyc".into()],
                models: vec![PackModel {
                    name: "fraud_detector".into(),
                    path: "models/fraud_detector.onnx".into(),
                    num_features: 4,
                    avg_latency_ms: 2.1,
                    accuracy: 0.95,
                }],
                rules: vec![PackRule {
                    name: "high_risk_transactions".into(),
                    path: "rules/high_risk.dl".into(),
                    description: "Flags transactions > $10k with high fraud scores".into(),
                }],
                anamdb_version: ">=0.1.0".into(),
                checksum: None,
            },
            PackManifest {
                name: "anamdb/medical-imaging".into(),
                version: "0.9.0".into(),
                description: "DICOM segmentation models + clinical Datalog constraints".into(),
                author: "community".into(),
                license: "Apache-2.0".into(),
                tags: vec!["medical".into(), "imaging".into(), "dicom".into(), "segmentation".into()],
                models: vec![PackModel {
                    name: "lesion_detector".into(),
                    path: "models/lesion_detector.onnx".into(),
                    num_features: 64,
                    avg_latency_ms: 28.5,
                    accuracy: 0.91,
                }],
                rules: vec![PackRule {
                    name: "critical_findings".into(),
                    path: "rules/critical.dl".into(),
                    description: "Escalates high-confidence lesion detections for radiologist review".into(),
                }],
                anamdb_version: ">=0.1.0".into(),
                checksum: None,
            },
            PackManifest {
                name: "anamdb/autonomous-driving".into(),
                version: "0.2.0".into(),
                description: "3D object detection + spatial safety constraint pack for AV pipelines".into(),
                author: "community".into(),
                license: "MIT".into(),
                tags: vec!["autonomous".into(), "lidar".into(), "3d".into(), "spatial".into()],
                models: vec![PackModel {
                    name: "bbox3d_detector".into(),
                    path: "models/bbox3d.onnx".into(),
                    num_features: 128,
                    avg_latency_ms: 12.0,
                    accuracy: 0.88,
                }],
                rules: vec![PackRule {
                    name: "collision_avoidance".into(),
                    path: "rules/collision.dl".into(),
                    description: "Raises alert when predicted trajectory intersects another agent's bounding box".into(),
                }],
                anamdb_version: ">=0.1.0".into(),
                checksum: None,
            },
        ];

        for pack in packs {
            let key = HubIndex::pack_key(&pack.name, &pack.version);
            self.index.available.insert(key, pack);
        }
        self.save_index()?;
        Ok(())
    }
}

// ── Result Types ──────────────────────────────────────────────────────

/// Result of a `hub install` operation.
#[derive(Debug, Clone)]
pub struct InstallResult {
    /// The pack reference string that was installed.
    pub pack_ref: String,
    /// Whether the pack was newly installed (`false` if already present).
    pub installed: bool,
    /// Human-readable status message.
    pub message: String,
}

/// Result of a `hub publish` operation.
#[derive(Debug, Clone)]
pub struct PublishResult {
    /// Package name.
    pub name: String,
    /// Package version.
    pub version: String,
    /// Registry key (`name@version`).
    pub key: String,
    /// Human-readable status message.
    pub message: String,
}

// ── Helpers ───────────────────────────────────────────────────────────

/// Parse a pack reference string into (name, version).
/// Accepts "name@version" or just "name" (defaults to "latest").
fn parse_pack_ref(pack_ref: &str) -> Result<(String, String)> {
    if let Some((name, version)) = pack_ref.split_once('@') {
        Ok((name.to_string(), version.to_string()))
    } else {
        Ok((pack_ref.to_string(), "latest".to_string()))
    }
}

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

    fn temp_hub() -> (HubClient, TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let mut hub = HubClient::new(dir.path()).unwrap();
        hub.seed_community_packs().unwrap();
        (hub, dir) // keep TempDir alive so the directory isn't deleted
    }

    #[test]
    fn hub_search_finds_packs() {
        let (hub, _dir) = temp_hub();

        let results = hub.search("fraud");
        assert!(!results.is_empty(), "should find fraud pack");
        assert!(results.iter().any(|m| m.name.contains("financial")));

        let results3d = hub.search("3d");
        assert!(results3d.iter().any(|m| m.name.contains("autonomous")));

        println!("\n═══ Hub Search Test ═══");
        println!("  'fraud' → {} result(s)", hub.search("fraud").len());
        println!("  '3d'    → {} result(s)", hub.search("3d").len());
        println!("  'dicom' → {} result(s)", hub.search("dicom").len());
        println!("  ✓ Community hub search works");
    }

    #[test]
    fn hub_install_and_list() {
        let (mut hub, _dir) = temp_hub();

        assert_eq!(hub.list_installed().len(), 0, "nothing installed yet");

        let result = hub.install("anamdb/financial-compliance@1.0.0").unwrap();
        assert!(result.installed);

        assert_eq!(hub.list_installed().len(), 1);
        assert_eq!(hub.list_installed()[0].name, "anamdb/financial-compliance");

        // Installing again should be a no-op.
        let r2 = hub.install("anamdb/financial-compliance@1.0.0").unwrap();
        assert!(!r2.installed, "double-install should be a no-op");
        assert_eq!(
            hub.list_installed().len(),
            1,
            "still 1 pack after duplicate install"
        );

        println!("\n═══ Hub Install Test ═══");
        println!("  ✓ Install: {}", result.message);
        println!("  ✓ List: {} installed pack(s)", hub.list_installed().len());
        println!("  ✓ Duplicate install no-op: {}", r2.message);
    }

    #[test]
    fn hub_publish_and_search() {
        let (mut hub, _dir) = temp_hub();

        let pack_dir = tempfile::tempdir().unwrap();
        let manifest = PackManifest {
            name: "testauthor/my-custom-pack".into(),
            version: "0.1.0".into(),
            description: "Test pack for custom domain".into(),
            author: "testauthor".into(),
            license: "MIT".into(),
            tags: vec!["custom".into(), "test".into()],
            models: vec![],
            rules: vec![],
            anamdb_version: ">=0.1.0".into(),
            checksum: None,
        };
        let manifest_path = pack_dir.path().join("manifest.json");
        std::fs::write(&manifest_path, serde_json::to_string(&manifest).unwrap()).unwrap();

        let result = hub.publish(pack_dir.path()).unwrap();
        assert_eq!(result.name, "testauthor/my-custom-pack");

        let search = hub.search("custom");
        assert!(search.iter().any(|m| m.name == "testauthor/my-custom-pack"));

        println!("\n═══ Hub Publish Test ═══");
        println!("  ✓ Published: {}", result.key);
        println!("  ✓ Searchable after publish: {} result(s)", search.len());
    }

    #[test]
    fn hub_index_persists() {
        let dir = tempfile::tempdir().unwrap();

        {
            let mut hub = HubClient::new(dir.path()).unwrap();
            hub.seed_community_packs().unwrap();
            hub.install("anamdb/medical-imaging@0.9.0").unwrap();
        }

        // Re-open the hub — should load from disk.
        let hub2 = HubClient::new(dir.path()).unwrap();
        assert_eq!(hub2.list_installed().len(), 1);
        assert_eq!(hub2.list_installed()[0].name, "anamdb/medical-imaging");

        println!("\n═══ Hub Persistence Test ═══");
        println!("  ✓ Hub index persisted and reloaded from disk");
        println!("  ✓ Installed packs survive restart");
    }
}