zeph-memory 0.19.1

Semantic memory with SQLite and Qdrant for Zeph agent
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

use serde::{Deserialize, Serialize};
#[allow(unused_imports)]
use zeph_db::sql;

use super::SqliteStore;
use crate::error::MemoryError;

/// Discriminant for the skill source stored in the trust table.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SourceKind {
    Local,
    Hub,
    File,
}

impl SourceKind {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Local => "local",
            Self::Hub => "hub",
            Self::File => "file",
        }
    }
}

impl std::fmt::Display for SourceKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for SourceKind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "local" => Ok(Self::Local),
            "hub" => Ok(Self::Hub),
            "file" => Ok(Self::File),
            other => Err(format!("unknown source_kind: {other}")),
        }
    }
}

#[derive(Debug, Clone)]
pub struct SkillTrustRow {
    pub skill_name: String,
    pub trust_level: String,
    pub source_kind: SourceKind,
    pub source_url: Option<String>,
    pub source_path: Option<String>,
    pub blake3_hash: String,
    pub updated_at: String,
    /// Upstream git commit hash at install time (from `x-git-hash` frontmatter field).
    pub git_hash: Option<String>,
}

type TrustTuple = (
    String,
    String,
    String,
    Option<String>,
    Option<String>,
    String,
    String,
    Option<String>,
);

fn row_from_tuple(t: TrustTuple) -> SkillTrustRow {
    let source_kind = t.2.parse::<SourceKind>().unwrap_or(SourceKind::Local);
    SkillTrustRow {
        skill_name: t.0,
        trust_level: t.1,
        source_kind,
        source_url: t.3,
        source_path: t.4,
        blake3_hash: t.5,
        updated_at: t.6,
        git_hash: t.7,
    }
}

impl SqliteStore {
    /// Upsert trust metadata for a skill.
    ///
    /// # Errors
    ///
    /// Returns an error if the database operation fails.
    pub async fn upsert_skill_trust(
        &self,
        skill_name: &str,
        trust_level: &str,
        source_kind: SourceKind,
        source_url: Option<&str>,
        source_path: Option<&str>,
        blake3_hash: &str,
    ) -> Result<(), MemoryError> {
        self.upsert_skill_trust_with_git_hash(
            skill_name,
            trust_level,
            source_kind,
            source_url,
            source_path,
            blake3_hash,
            None,
        )
        .await
    }

    /// Upsert trust metadata for a skill, including an optional upstream git hash.
    ///
    /// `git_hash` is the upstream commit hash from the `x-git-hash` SKILL.md frontmatter field.
    /// It tracks the upstream commit at install time and is stored separately from `blake3_hash`
    /// (which tracks content integrity).
    ///
    /// # Errors
    ///
    /// Returns an error if the database operation fails.
    #[allow(clippy::too_many_arguments)]
    pub async fn upsert_skill_trust_with_git_hash(
        &self,
        skill_name: &str,
        trust_level: &str,
        source_kind: SourceKind,
        source_url: Option<&str>,
        source_path: Option<&str>,
        blake3_hash: &str,
        git_hash: Option<&str>,
    ) -> Result<(), MemoryError> {
        zeph_db::query(
            sql!("INSERT INTO skill_trust \
             (skill_name, trust_level, source_kind, source_url, source_path, blake3_hash, git_hash, updated_at) \
             VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) \
             ON CONFLICT(skill_name) DO UPDATE SET \
             trust_level = excluded.trust_level, \
             source_kind = excluded.source_kind, \
             source_url = excluded.source_url, \
             source_path = excluded.source_path, \
             blake3_hash = excluded.blake3_hash, \
             git_hash = excluded.git_hash, \
             updated_at = CURRENT_TIMESTAMP"),
        )
        .bind(skill_name)
        .bind(trust_level)
        .bind(source_kind.as_str())
        .bind(source_url)
        .bind(source_path)
        .bind(blake3_hash)
        .bind(git_hash)
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    /// Load trust metadata for a single skill.
    ///
    /// # Errors
    ///
    /// Returns an error if the query fails.
    pub async fn load_skill_trust(
        &self,
        skill_name: &str,
    ) -> Result<Option<SkillTrustRow>, MemoryError> {
        let row: Option<TrustTuple> = zeph_db::query_as(sql!(
            "SELECT skill_name, trust_level, source_kind, source_url, source_path, \
             blake3_hash, updated_at, git_hash \
             FROM skill_trust WHERE skill_name = ?"
        ))
        .bind(skill_name)
        .fetch_optional(&self.pool)
        .await?;
        Ok(row.map(row_from_tuple))
    }

    /// Load all skill trust entries.
    ///
    /// # Errors
    ///
    /// Returns an error if the query fails.
    pub async fn load_all_skill_trust(&self) -> Result<Vec<SkillTrustRow>, MemoryError> {
        let rows: Vec<TrustTuple> = zeph_db::query_as(sql!(
            "SELECT skill_name, trust_level, source_kind, source_url, source_path, \
             blake3_hash, updated_at, git_hash \
             FROM skill_trust ORDER BY skill_name"
        ))
        .fetch_all(&self.pool)
        .await?;
        Ok(rows.into_iter().map(row_from_tuple).collect())
    }

    /// Update only the trust level for a skill.
    ///
    /// # Errors
    ///
    /// Returns an error if the skill does not exist or the update fails.
    pub async fn set_skill_trust_level(
        &self,
        skill_name: &str,
        trust_level: &str,
    ) -> Result<bool, MemoryError> {
        let result = zeph_db::query(
            sql!("UPDATE skill_trust SET trust_level = ?, updated_at = CURRENT_TIMESTAMP WHERE skill_name = ?"),
        )
        .bind(trust_level)
        .bind(skill_name)
        .execute(&self.pool)
        .await?;
        Ok(result.rows_affected() > 0)
    }

    /// Delete trust entry for a skill.
    ///
    /// # Errors
    ///
    /// Returns an error if the delete fails.
    pub async fn delete_skill_trust(&self, skill_name: &str) -> Result<bool, MemoryError> {
        let result = zeph_db::query(sql!("DELETE FROM skill_trust WHERE skill_name = ?"))
            .bind(skill_name)
            .execute(&self.pool)
            .await?;
        Ok(result.rows_affected() > 0)
    }

    /// Update the blake3 hash for a skill.
    ///
    /// # Errors
    ///
    /// Returns an error if the update fails.
    pub async fn update_skill_hash(
        &self,
        skill_name: &str,
        blake3_hash: &str,
    ) -> Result<bool, MemoryError> {
        let result = zeph_db::query(
            sql!("UPDATE skill_trust SET blake3_hash = ?, updated_at = CURRENT_TIMESTAMP WHERE skill_name = ?"),
        )
        .bind(blake3_hash)
        .bind(skill_name)
        .execute(&self.pool)
        .await?;
        Ok(result.rows_affected() > 0)
    }
}

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

    async fn test_store() -> SqliteStore {
        SqliteStore::new(":memory:").await.unwrap()
    }

    #[tokio::test]
    async fn upsert_and_load() {
        let store = test_store().await;

        store
            .upsert_skill_trust("git", "trusted", SourceKind::Local, None, None, "abc123")
            .await
            .unwrap();

        let row = store.load_skill_trust("git").await.unwrap().unwrap();
        assert_eq!(row.skill_name, "git");
        assert_eq!(row.trust_level, "trusted");
        assert_eq!(row.source_kind, SourceKind::Local);
        assert_eq!(row.blake3_hash, "abc123");
    }

    #[tokio::test]
    async fn upsert_updates_existing() {
        let store = test_store().await;

        store
            .upsert_skill_trust("git", "quarantined", SourceKind::Local, None, None, "hash1")
            .await
            .unwrap();
        store
            .upsert_skill_trust("git", "trusted", SourceKind::Local, None, None, "hash2")
            .await
            .unwrap();

        let row = store.load_skill_trust("git").await.unwrap().unwrap();
        assert_eq!(row.trust_level, "trusted");
        assert_eq!(row.blake3_hash, "hash2");
    }

    #[tokio::test]
    async fn load_nonexistent() {
        let store = test_store().await;
        let row = store.load_skill_trust("nope").await.unwrap();
        assert!(row.is_none());
    }

    #[tokio::test]
    async fn load_all() {
        let store = test_store().await;

        store
            .upsert_skill_trust("alpha", "trusted", SourceKind::Local, None, None, "h1")
            .await
            .unwrap();
        store
            .upsert_skill_trust(
                "beta",
                "quarantined",
                SourceKind::Hub,
                Some("https://hub.example.com"),
                None,
                "h2",
            )
            .await
            .unwrap();

        let rows = store.load_all_skill_trust().await.unwrap();
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].skill_name, "alpha");
        assert_eq!(rows[1].skill_name, "beta");
    }

    #[tokio::test]
    async fn set_trust_level() {
        let store = test_store().await;

        store
            .upsert_skill_trust("git", "quarantined", SourceKind::Local, None, None, "h1")
            .await
            .unwrap();

        let updated = store.set_skill_trust_level("git", "blocked").await.unwrap();
        assert!(updated);

        let row = store.load_skill_trust("git").await.unwrap().unwrap();
        assert_eq!(row.trust_level, "blocked");
    }

    #[tokio::test]
    async fn set_trust_level_nonexistent() {
        let store = test_store().await;
        let updated = store
            .set_skill_trust_level("nope", "blocked")
            .await
            .unwrap();
        assert!(!updated);
    }

    #[tokio::test]
    async fn delete_trust() {
        let store = test_store().await;

        store
            .upsert_skill_trust("git", "trusted", SourceKind::Local, None, None, "h1")
            .await
            .unwrap();

        let deleted = store.delete_skill_trust("git").await.unwrap();
        assert!(deleted);

        let row = store.load_skill_trust("git").await.unwrap();
        assert!(row.is_none());
    }

    #[tokio::test]
    async fn delete_nonexistent() {
        let store = test_store().await;
        let deleted = store.delete_skill_trust("nope").await.unwrap();
        assert!(!deleted);
    }

    #[tokio::test]
    async fn update_hash() {
        let store = test_store().await;

        store
            .upsert_skill_trust("git", "verified", SourceKind::Local, None, None, "old_hash")
            .await
            .unwrap();

        let updated = store.update_skill_hash("git", "new_hash").await.unwrap();
        assert!(updated);

        let row = store.load_skill_trust("git").await.unwrap().unwrap();
        assert_eq!(row.blake3_hash, "new_hash");
    }

    #[tokio::test]
    async fn source_with_url() {
        let store = test_store().await;

        store
            .upsert_skill_trust(
                "remote-skill",
                "quarantined",
                SourceKind::Hub,
                Some("https://hub.example.com/skill"),
                None,
                "h1",
            )
            .await
            .unwrap();

        let row = store
            .load_skill_trust("remote-skill")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(row.source_kind, SourceKind::Hub);
        assert_eq!(
            row.source_url.as_deref(),
            Some("https://hub.example.com/skill")
        );
    }

    #[tokio::test]
    async fn source_with_path() {
        let store = test_store().await;

        store
            .upsert_skill_trust(
                "file-skill",
                "quarantined",
                SourceKind::File,
                None,
                Some("/tmp/skill.tar.gz"),
                "h1",
            )
            .await
            .unwrap();

        let row = store.load_skill_trust("file-skill").await.unwrap().unwrap();
        assert_eq!(row.source_kind, SourceKind::File);
        assert_eq!(row.source_path.as_deref(), Some("/tmp/skill.tar.gz"));
    }

    #[test]
    fn source_kind_display_local() {
        assert_eq!(SourceKind::Local.to_string(), "local");
    }

    #[test]
    fn source_kind_display_hub() {
        assert_eq!(SourceKind::Hub.to_string(), "hub");
    }

    #[test]
    fn source_kind_display_file() {
        assert_eq!(SourceKind::File.to_string(), "file");
    }

    #[test]
    fn source_kind_from_str_local() {
        let kind: SourceKind = "local".parse().unwrap();
        assert_eq!(kind, SourceKind::Local);
    }

    #[test]
    fn source_kind_from_str_hub() {
        let kind: SourceKind = "hub".parse().unwrap();
        assert_eq!(kind, SourceKind::Hub);
    }

    #[test]
    fn source_kind_from_str_file() {
        let kind: SourceKind = "file".parse().unwrap();
        assert_eq!(kind, SourceKind::File);
    }

    #[test]
    fn source_kind_from_str_unknown_returns_error() {
        let result: Result<SourceKind, _> = "s3".parse();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("unknown source_kind"));
    }

    #[test]
    fn source_kind_serde_json_roundtrip_local() {
        let original = SourceKind::Local;
        let json = serde_json::to_string(&original).unwrap();
        assert_eq!(json, r#""local""#);
        let back: SourceKind = serde_json::from_str(&json).unwrap();
        assert_eq!(back, original);
    }

    #[test]
    fn source_kind_serde_json_roundtrip_hub() {
        let original = SourceKind::Hub;
        let json = serde_json::to_string(&original).unwrap();
        assert_eq!(json, r#""hub""#);
        let back: SourceKind = serde_json::from_str(&json).unwrap();
        assert_eq!(back, original);
    }

    #[test]
    fn source_kind_serde_json_roundtrip_file() {
        let original = SourceKind::File;
        let json = serde_json::to_string(&original).unwrap();
        assert_eq!(json, r#""file""#);
        let back: SourceKind = serde_json::from_str(&json).unwrap();
        assert_eq!(back, original);
    }

    #[test]
    fn source_kind_serde_json_invalid_value_errors() {
        let result: Result<SourceKind, _> = serde_json::from_str(r#""unknown""#);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn trust_row_includes_git_hash() {
        let store = test_store().await;

        store
            .upsert_skill_trust_with_git_hash(
                "versioned-skill",
                "trusted",
                SourceKind::Hub,
                Some("https://hub.example.com/skill"),
                None,
                "blake3abc",
                Some("deadbeef1234"),
            )
            .await
            .unwrap();

        let row = store
            .load_skill_trust("versioned-skill")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(row.git_hash.as_deref(), Some("deadbeef1234"));
        assert_eq!(row.blake3_hash, "blake3abc");
    }

    #[tokio::test]
    async fn upsert_without_git_hash_leaves_it_null() {
        let store = test_store().await;

        store
            .upsert_skill_trust("git", "trusted", SourceKind::Local, None, None, "hash1")
            .await
            .unwrap();

        let row = store.load_skill_trust("git").await.unwrap().unwrap();
        assert!(row.git_hash.is_none());
    }

    #[tokio::test]
    async fn upsert_each_source_kind_roundtrip() {
        let store = test_store().await;
        let variants = [
            ("skill-local", SourceKind::Local),
            ("skill-hub", SourceKind::Hub),
            ("skill-file", SourceKind::File),
        ];
        for (name, kind) in &variants {
            store
                .upsert_skill_trust(name, "trusted", kind.clone(), None, None, "hash")
                .await
                .unwrap();
            let row = store.load_skill_trust(name).await.unwrap().unwrap();
            assert_eq!(&row.source_kind, kind);
        }
    }
}