agents-kit 0.0.2

Rust agent control Kit for Codex, OpenCode, and provider-backed Custom runtimes
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
use std::collections::BTreeSet;
use std::sync::{Arc, Mutex};

use storage_kit::{DatabaseBackend, StateStoreBackend, StateTransactionBackend};

use crate::{AgentId, AgentInfo, KitError, KnowledgeOwnerId, ManagedKnowledge};

const AGENT_NAMESPACE: &str = "agents_kit_agents_v1";
const AGENT_RECORD_VERSION: u32 = 1;
const MAX_AGENT_RECORDS: usize = 65_536;
const MAX_AGENT_INVENTORY_PAGES: usize = 1_024;
const KNOWLEDGE_FORGET_NAMESPACE: &str = "agents_kit_knowledge_forget_v1";
const KNOWLEDGE_FORGET_RECORD_VERSION: u32 = 1;
const MAX_KNOWLEDGE_FORGET_RECORDS: usize = 65_536;
const MAX_KNOWLEDGE_FORGET_PAGES: usize = 1_024;

/// Coordinates durable agent identities through a caller-supplied state store.
///
/// The concrete `S` is retained directly, and its associated `S::Transaction`
/// remains the only transaction type used for agent control-plane work. Clones
/// share the same store access and live-agent ownership domain without requiring
/// `S: Clone`.
///
/// # Examples
///
/// ```
/// use agents_kit::AgentStorage;
/// use storage_kit::{StateStore, state::config::{StateStoreConfig, LanceStateStoreConfig}};
/// let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
/// runtime.block_on(async {
///     let store = StateStore::connect(StateStoreConfig::Lance(LanceStateStoreConfig::memory())).await?;
///     let storage = AgentStorage::new(store);
///     assert!(storage.list_agents().await?.is_empty());
///     let id = "agent_00000000000000000000000000000000".parse()?;
///     let error = storage.agent_info(id).await.unwrap_err();
///     assert_eq!(error.kind(), agents_kit::error::ErrorKind::NotFound);
///     drop(storage);
///     Ok::<(), Box<dyn std::error::Error>>(())
/// })?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct AgentStorage<S: StateStoreBackend = storage_kit::StateStore> {
    inner: Arc<AgentStorageInner<S>>,
}

struct AgentStorageInner<S: StateStoreBackend> {
    store: S,
    live_agents: Arc<Mutex<BTreeSet<AgentId>>>,
}

pub(crate) struct AgentStorageClaim {
    live_agents: Arc<Mutex<BTreeSet<AgentId>>>,
    id: Option<AgentId>,
    release_on_drop: bool,
}

pub(crate) enum AgentStorageCreationError {
    TransactionConflict,
    Failure(KitError),
}

pub(crate) enum AgentStorageKnowledgeError {
    TransactionConflict(crate::error::ErrorPhase),
    Failure(KitError),
}

#[derive(serde::Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
struct AgentRecord {
    version: u32,
    id: AgentId,
    display_name: Option<String>,
}

#[derive(serde::Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
struct KnowledgeForgetRecord {
    version: u32,
    owner: KnowledgeOwnerId,
}

impl<S: StateStoreBackend> Clone for AgentStorage<S> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<S: StateStoreBackend> AgentStorage<S> {
    /// Creates the agent control-plane boundary from an already-built state store.
    ///
    /// Construction retains the supplied store; it neither creates an Agent nor requires `S: Clone`.
    ///
    /// See [`AgentStorage`] for a complete example.
    pub fn new(store: S) -> Self {
        Self {
            inner: Arc::new(AgentStorageInner {
                store,
                live_agents: Arc::new(Mutex::new(BTreeSet::new())),
            }),
        }
    }

    /// Lists every durable agent identity visible through this storage domain.
    ///
    /// The result is ordered by durable Agent ID. This reads storage, not native runtime inventory.
    ///
    /// # Errors
    ///
    /// Returns a typed storage failure or `InvalidState` for malformed records or an inventory that exceeds the bounded traversal.
    ///
    /// See [`AgentStorage`] for a complete example.
    pub async fn list_agents(&self) -> Result<Vec<AgentInfo>, KitError> {
        async {
            let mut records = Vec::new();
            let mut cursor = None;
            let mut pages = 0_usize;
            loop {
                pages += 1;
                if pages > MAX_AGENT_INVENTORY_PAGES {
                    return Err(KitError::invalid_state(
                        "agent inventory exceeds its private bound",
                    ));
                }
                let mut request =
                    storage_kit::state::record::StateListRequest::new(AGENT_NAMESPACE)
                        .map_err(|error| {
                            KitError::from_state_store(
                                error,
                                "agent inventory request could not be created",
                            )
                            .in_phase(crate::error::ErrorPhase::StateRead)
                        })?
                        .with_limit(storage_kit::state::record::StateListRequest::MAX_LIMIT)
                        .map_err(|error| {
                            KitError::from_state_store(error, "agent inventory limit was rejected")
                                .in_phase(crate::error::ErrorPhase::StateRead)
                        })?;
                if let Some(next) = cursor.take() {
                    request = request.start_after(next).map_err(|_| {
                        KitError::invalid_state("agent inventory cursor is invalid")
                    })?;
                }
                let page = self.store().list(request).await.map_err(|error| {
                    KitError::from_state_store(error, "agent inventory could not be loaded")
                        .in_phase(crate::error::ErrorPhase::StateRead)
                })?;
                for snapshot in page.records {
                    if records.len() == MAX_AGENT_RECORDS {
                        return Err(KitError::invalid_state(
                            "agent inventory exceeds its private bound",
                        ));
                    }
                    records.push(AgentRecord::decode(snapshot)?);
                }
                let Some(next) = page.next_start_after else {
                    break;
                };
                cursor = Some(next);
            }
            records.sort_by(|left, right| left.id.cmp(&right.id));
            records.into_iter().map(AgentRecord::into_info).collect()
        }
        .await
        .map_err(|error: KitError| {
            error
                .at_operation(crate::error::Operation::ListAgents)
                .in_phase(crate::error::ErrorPhase::StateRead)
        })
    }

    /// Loads descriptive information for one durable agent identity.
    ///
    /// This reads the durable description without opening a runtime.
    ///
    /// # Errors
    ///
    /// Returns `NotFound` for an absent identity, `InvalidState` for malformed records, or a typed storage access failure.
    ///
    /// See [`AgentStorage`] for a complete example.
    pub async fn agent_info(&self, id: AgentId) -> Result<AgentInfo, KitError> {
        async { self.load_record(&id).await?.into_info() }
            .await
            .map_err(|error: KitError| {
                error
                    .at_operation(crate::error::Operation::AgentInfo)
                    .in_phase(crate::error::ErrorPhase::StateRead)
            })
    }

    pub(crate) async fn create_agent_in_transaction(
        &self,
        transaction: &mut S::Transaction,
        info: &AgentInfo,
    ) -> Result<(), AgentStorageCreationError> {
        let record = AgentRecord::from_info(info);
        let value = serde_json::to_value(&record).map_err(|_| {
            AgentStorageCreationError::Failure(KitError::invalid_state(
                "agent identity could not be encoded",
            ))
        })?;
        let key = AgentRecord::key(info.id()).map_err(AgentStorageCreationError::Failure)?;
        if transaction
            .load(key.clone())
            .await
            .map_err(|error| match error {
                storage_kit::StateStoreError::TransactionConflict { .. } => {
                    AgentStorageCreationError::TransactionConflict
                }
                error => AgentStorageCreationError::Failure(
                    KitError::from_state_store(error, "agent identity could not be checked")
                        .in_phase(crate::error::ErrorPhase::StateRead),
                ),
            })?
            .is_some()
        {
            return Err(AgentStorageCreationError::Failure(KitError::conflict(
                "agent identity already exists",
            )));
        }
        transaction
            .put(storage_kit::state::record::StateRecord::new(key, value))
            .await
            .map_err(|error| match error {
                storage_kit::StateStoreError::TransactionConflict { .. } => {
                    AgentStorageCreationError::TransactionConflict
                }
                error => AgentStorageCreationError::Failure(
                    KitError::from_state_store(error, "agent identity could not be stored")
                        .in_phase(crate::error::ErrorPhase::StateWrite),
                ),
            })?;
        Ok(())
    }

    pub(crate) fn store(&self) -> &S {
        &self.inner.store
    }

    pub(crate) async fn stage_knowledge_forget(
        &self,
        transaction: &mut S::Transaction,
        owner: &KnowledgeOwnerId,
    ) -> Result<(), AgentStorageKnowledgeError> {
        let record = KnowledgeForgetRecord::new(owner.clone())
            .map_err(AgentStorageKnowledgeError::Failure)?;
        let key = record.key().map_err(AgentStorageKnowledgeError::Failure)?;
        let current = transaction
            .load(key.clone())
            .await
            .map_err(|error| match error {
                storage_kit::StateStoreError::TransactionConflict { .. } => {
                    AgentStorageKnowledgeError::TransactionConflict(
                        crate::error::ErrorPhase::StateRead,
                    )
                }
                error => AgentStorageKnowledgeError::Failure(
                    KitError::from_state_store(
                        error,
                        "knowledge forget obligation could not be checked",
                    )
                    .in_phase(crate::error::ErrorPhase::StateRead),
                ),
            })?;
        if let Some(current) = current {
            let current = KnowledgeForgetRecord::decode(current)
                .map_err(AgentStorageKnowledgeError::Failure)?;
            return if current.owner == record.owner {
                Ok(())
            } else {
                Err(AgentStorageKnowledgeError::Failure(
                    KitError::invalid_state(
                        "knowledge forget obligation contradicts its durable key",
                    ),
                ))
            };
        }
        let value = record
            .encode()
            .map_err(AgentStorageKnowledgeError::Failure)?;
        transaction
            .put(storage_kit::state::record::StateRecord::new(key, value))
            .await
            .map_err(|error| match error {
                storage_kit::StateStoreError::TransactionConflict { .. } => {
                    AgentStorageKnowledgeError::TransactionConflict(
                        crate::error::ErrorPhase::StateWrite,
                    )
                }
                error => AgentStorageKnowledgeError::Failure(
                    KitError::from_state_store(
                        error,
                        "knowledge forget obligation could not be stored",
                    )
                    .in_phase(crate::error::ErrorPhase::StateWrite),
                ),
            })
    }

    pub(crate) async fn settle_knowledge_forget<D: DatabaseBackend>(
        &self,
        knowledge: Option<&ManagedKnowledge<S, D>>,
        owner: &KnowledgeOwnerId,
        deadline: tokio::time::Instant,
        observation_deadline: Option<tokio::time::Instant>,
    ) -> Result<(), KitError> {
        let settlement = async {
            let key = KnowledgeForgetRecord::key_for(owner)?;
            if tokio::time::Instant::now() >= deadline {
                return Err(KitError::timeout(
                    "knowledge reconciliation deadline elapsed",
                ));
            }
            let Some(snapshot) = self.store().load(key.clone()).await.map_err(|error| {
                KitError::from_state_store(error, "knowledge forget obligation could not be loaded")
                    .in_phase(crate::error::ErrorPhase::StateRead)
            })?
            else {
                return Ok(());
            };
            let record = KnowledgeForgetRecord::decode(snapshot)?;
            if &record.owner != owner {
                return Err(KitError::invalid_state(
                    "knowledge forget obligation contradicts its owner",
                ));
            }
            let knowledge = knowledge.ok_or_else(|| {
                KitError::conflict(
                    "durable knowledge cleanup requires the supplied ManagedKnowledge",
                )
            })?;
            if tokio::time::Instant::now() >= deadline {
                return Err(KitError::timeout(
                    "knowledge reconciliation deadline elapsed",
                ));
            }
            knowledge.forget(owner.clone()).await?;

            if tokio::time::Instant::now() >= deadline {
                return Err(KitError::timeout(
                    "knowledge reconciliation deadline elapsed",
                ));
            }
            let mut transaction = self.store().begin_transaction().await.map_err(|error| {
                KitError::from_state_store(
                    error,
                    "knowledge forget obligation could not be settled",
                )
                .in_phase(crate::error::ErrorPhase::StateWrite)
            })?;
            let current = transaction.load(key.clone()).await.map_err(|error| {
                KitError::from_state_store(
                    error,
                    "knowledge forget obligation could not be settled",
                )
                .in_phase(crate::error::ErrorPhase::StateRead)
            })?;
            if let Some(current) = current {
                let current = KnowledgeForgetRecord::decode(current)?;
                if current.owner != record.owner {
                    return Err(KitError::conflict(
                        "knowledge forget obligation changed during settlement",
                    ));
                }
                transaction.delete(key).await.map_err(|error| {
                    KitError::from_state_store(
                        error,
                        "knowledge forget obligation could not be settled",
                    )
                    .in_phase(crate::error::ErrorPhase::StateWrite)
                })?;
            }
            transaction.commit().await.map(|_| ()).map_err(|error| {
                KitError::from_state_store(
                    error,
                    "knowledge forget obligation could not be settled",
                )
                .in_phase(crate::error::ErrorPhase::StateCommit)
            })
        };
        match observation_deadline {
            Some(observation_deadline) => tokio::time::timeout_at(observation_deadline, settlement)
                .await
                .map_err(|_| KitError::timeout("knowledge reconciliation deadline elapsed"))?,
            None => settlement.await,
        }
    }

    pub(crate) async fn settle_knowledge_forgets<D: DatabaseBackend>(
        &self,
        knowledge: Option<&ManagedKnowledge<S, D>>,
        agent_id: &AgentId,
        deadline: tokio::time::Instant,
    ) -> Result<(), KitError> {
        let records = self.knowledge_forget_records(agent_id, deadline).await?;
        if !records.is_empty() && knowledge.is_none() {
            return Err(KitError::conflict(
                "durable knowledge cleanup requires the supplied ManagedKnowledge",
            ));
        }
        for record in records {
            self.settle_knowledge_forget(knowledge, &record.owner, deadline, Some(deadline))
                .await?;
        }
        Ok(())
    }

    pub(crate) fn claim_agent(&self, id: &AgentId) -> Result<AgentStorageClaim, KitError> {
        let mut live = self
            .inner
            .live_agents
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        if !live.insert(id.clone()) {
            return Err(KitError::busy("agent identity is already live"));
        }
        drop(live);
        Ok(AgentStorageClaim {
            live_agents: self.inner.live_agents.clone(),
            id: Some(id.clone()),
            release_on_drop: true,
        })
    }

    async fn load_record(&self, id: &AgentId) -> Result<AgentRecord, KitError> {
        let key = AgentRecord::key(id)?;
        let snapshot = self
            .store()
            .load(key)
            .await
            .map_err(|error| {
                KitError::from_state_store(error, "agent identity could not be loaded")
                    .in_phase(crate::error::ErrorPhase::StateRead)
            })?
            .ok_or_else(|| KitError::not_found("agent identity does not exist"))?;
        let record = AgentRecord::decode(snapshot)?;
        if &record.id != id {
            return Err(KitError::invalid_state(
                "stored agent identity does not match its key",
            ));
        }
        Ok(record)
    }

    async fn knowledge_forget_records(
        &self,
        agent_id: &AgentId,
        deadline: tokio::time::Instant,
    ) -> Result<Vec<KnowledgeForgetRecord>, KitError> {
        let prefix =
            storage_kit::state::record::StateKeyPrefix::new(format!("{}:", agent_id.as_str()))
                .map_err(|_| KitError::invalid_state("knowledge forget prefix is invalid"))?;
        let mut records = Vec::new();
        let mut cursor = None;
        let mut cursors = BTreeSet::new();
        for _ in 0..MAX_KNOWLEDGE_FORGET_PAGES {
            let mut request =
                storage_kit::state::record::StateListRequest::new(KNOWLEDGE_FORGET_NAMESPACE)
                    .map_err(|_| {
                        KitError::invalid_state("knowledge forget inventory request is invalid")
                    })?
                    .with_key_prefix(prefix.clone())
                    .with_limit(storage_kit::state::record::StateListRequest::MAX_LIMIT)
                    .map_err(|_| {
                        KitError::invalid_state("knowledge forget inventory limit is invalid")
                    })?;
            if let Some(next) = cursor.take() {
                request = request.start_after(next).map_err(|_| {
                    KitError::invalid_state("knowledge forget inventory cursor is invalid")
                })?;
            }
            let page = tokio::time::timeout_at(deadline, self.store().list(request))
                .await
                .map_err(|_| KitError::timeout("knowledge reconciliation deadline elapsed"))?
                .map_err(|error| {
                    KitError::from_state_store(
                        error,
                        "knowledge forget inventory could not be loaded",
                    )
                    .in_phase(crate::error::ErrorPhase::StateRead)
                })?;
            if page.records.len() > MAX_KNOWLEDGE_FORGET_RECORDS.saturating_sub(records.len()) {
                return Err(KitError::invalid_state(
                    "knowledge forget inventory exceeds its private bound",
                ));
            }
            for snapshot in page.records {
                let record = KnowledgeForgetRecord::decode(snapshot)?;
                if record.agent_id() != Some(agent_id) {
                    return Err(KitError::invalid_state(
                        "knowledge forget obligation has a foreign owner",
                    ));
                }
                records.push(record);
            }
            let Some(next) = page.next_start_after else {
                return Ok(records);
            };
            if !cursors.insert(next.clone()) {
                return Err(KitError::invalid_state(
                    "knowledge forget inventory repeated its cursor",
                ));
            }
            cursor = Some(next);
        }
        Err(KitError::invalid_state(
            "knowledge forget inventory exceeds its private page bound",
        ))
    }
}

impl AgentStorageCreationError {
    pub(crate) fn into_sdk(self) -> KitError {
        match self {
            Self::TransactionConflict => {
                KitError::conflict("agent identity transaction conflicted")
            }
            Self::Failure(error) => error,
        }
    }
}

impl AgentStorageKnowledgeError {
    pub(crate) fn into_sdk(self) -> KitError {
        match self {
            Self::TransactionConflict(phase) => {
                KitError::conflict("knowledge forget transaction conflicted")
                    .with_detail(crate::error::ErrorDetail::TransactionConflict)
                    .in_phase(phase)
            }
            Self::Failure(error) => error,
        }
    }
}

impl Drop for AgentStorageClaim {
    fn drop(&mut self) {
        if !self.release_on_drop {
            return;
        }
        self.release();
    }
}

impl AgentStorageClaim {
    pub(crate) fn retain_until_shutdown(&mut self) {
        self.release_on_drop = false;
    }

    pub(crate) fn release(&mut self) {
        let Some(id) = self.id.take() else {
            return;
        };
        self.live_agents
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .remove(&id);
    }

    pub(crate) fn complete_shutdown(
        &mut self,
        result: Result<(), KitError>,
    ) -> Result<(), KitError> {
        if result.is_ok() {
            self.release();
        }
        result
    }
}

impl AgentRecord {
    fn from_info(info: &AgentInfo) -> Self {
        Self {
            version: AGENT_RECORD_VERSION,
            id: info.id().clone(),
            display_name: info.display_name().map(str::to_owned),
        }
    }

    fn into_info(self) -> Result<AgentInfo, KitError> {
        AgentInfo::new(self.id, self.display_name)
            .map_err(|_| KitError::invalid_state("stored agent identity is malformed"))
    }

    fn key(id: &AgentId) -> Result<storage_kit::state::record::StateKey, KitError> {
        storage_kit::state::record::StateKey::new(AGENT_NAMESPACE, id.as_str())
            .map_err(|_| KitError::invalid_state("agent identity key is invalid"))
    }

    fn decode(snapshot: storage_kit::state::record::StateRecordSnapshot) -> Result<Self, KitError> {
        let key = snapshot.record.key.key().to_owned();
        let record: Self = serde::Deserialize::deserialize(snapshot.record.value.as_ref())
            .map_err(|_| KitError::invalid_state("stored agent identity is malformed"))?;
        if record.version != AGENT_RECORD_VERSION || record.id.as_str() != key {
            return Err(KitError::invalid_state(
                "stored agent identity does not match its durable key",
            ));
        }
        if record
            .display_name
            .as_ref()
            .is_some_and(|name| name.is_empty() || name.trim() != name)
        {
            return Err(KitError::invalid_state(
                "stored agent display name is malformed",
            ));
        }
        Ok(record)
    }
}

impl KnowledgeForgetRecord {
    fn new(owner: KnowledgeOwnerId) -> Result<Self, KitError> {
        if matches!(owner, KnowledgeOwnerId::Database) {
            return Err(KitError::invalid_input(
                "Database knowledge is not owned by an agent hierarchy deletion",
            ));
        }
        Ok(Self {
            version: KNOWLEDGE_FORGET_RECORD_VERSION,
            owner,
        })
    }

    fn key(&self) -> Result<storage_kit::state::record::StateKey, KitError> {
        Self::key_for(&self.owner)
    }

    fn key_for(owner: &KnowledgeOwnerId) -> Result<storage_kit::state::record::StateKey, KitError> {
        let (agent_id, suffix) = match owner {
            KnowledgeOwnerId::Database => {
                return Err(KitError::invalid_input(
                    "Database knowledge has no agent hierarchy key",
                ));
            }
            KnowledgeOwnerId::Agent { agent_id } => (agent_id, "agent".to_owned()),
            KnowledgeOwnerId::Workspace {
                agent_id,
                workspace_id,
            } => (agent_id, format!("workspace:{}", workspace_id.as_str())),
            KnowledgeOwnerId::Conversation {
                agent_id,
                workspace_id,
                conversation_id,
            } => (
                agent_id,
                format!(
                    "conversation:{}:{}",
                    workspace_id.as_str(),
                    conversation_id.as_str()
                ),
            ),
        };
        storage_kit::state::record::StateKey::new(
            KNOWLEDGE_FORGET_NAMESPACE,
            format!("{}:{suffix}", agent_id.as_str()),
        )
        .map_err(|_| KitError::invalid_state("knowledge forget key is invalid"))
    }

    fn agent_id(&self) -> Option<&AgentId> {
        match &self.owner {
            KnowledgeOwnerId::Database => None,
            KnowledgeOwnerId::Agent { agent_id }
            | KnowledgeOwnerId::Workspace { agent_id, .. }
            | KnowledgeOwnerId::Conversation { agent_id, .. } => Some(agent_id),
        }
    }

    fn encode(&self) -> Result<serde_json::Value, KitError> {
        serde_json::to_value(self).map_err(|_| {
            KitError::invalid_state("knowledge forget obligation could not be encoded")
        })
    }

    fn decode(snapshot: storage_kit::state::record::StateRecordSnapshot) -> Result<Self, KitError> {
        let key = snapshot.record.key.clone();
        let record: Self = serde::Deserialize::deserialize(snapshot.record.value.as_ref())
            .map_err(|_| {
                KitError::invalid_state("stored knowledge forget obligation is malformed")
            })?;
        let expected_key = record.key().map_err(|_| {
            KitError::invalid_state("stored knowledge forget obligation has an invalid owner")
        })?;
        if record.version != KNOWLEDGE_FORGET_RECORD_VERSION || expected_key != key {
            return Err(KitError::invalid_state(
                "stored knowledge forget obligation contradicts its key",
            ));
        }
        Ok(record)
    }
}