agent-office 0.0.1

A Rust-based multi-agent system with graph-structured data storage, mail system, and Zettelkasten-style knowledge base
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
694
695
696
697
698
use crate::domain::{Edge, Properties, PropertyValue, string_to_node_id};
use crate::services::kb::domain::{KnowledgeBase, LinkType, LuhmannId, Note, NoteId, NoteLink, AgentId};
use crate::storage::{GraphStorage, StorageError, EdgeDirection};
use async_trait::async_trait;
use thiserror::Error;
use std::collections::{HashMap, HashSet, VecDeque};

pub mod domain;

#[derive(Error, Debug)]
pub enum KbError {
    #[error("Note not found: {0}")]
    NoteNotFound(NoteId),
    
    #[error("Knowledge base not found for agent: {0}")]
    KnowledgeBaseNotFound(AgentId),
    
    #[error("Agent not found: {0}")]
    AgentNotFound(AgentId),
    
    #[error("Invalid link type: {0}")]
    InvalidLinkType(String),
    
    #[error("Storage error: {0}")]
    Storage(#[from] StorageError),
    
    #[error("Note already linked")]
    AlreadyLinked,
    
    #[error("Cannot link note to itself")]
    SelfLink,
}

pub type Result<T> = std::result::Result<T, KbError>;

#[async_trait]
pub trait KnowledgeBaseService: Send + Sync {
    // Knowledge Base operations
    async fn create_knowledge_base(&self, agent_id: AgentId, name: impl Into<String> + Send) -> Result<KnowledgeBase>;
    async fn get_knowledge_base(&self, agent_id: AgentId) -> Result<KnowledgeBase>;
    
    // Note operations
    async fn create_note(
        &self,
        agent_id: AgentId,
        title: impl Into<String> + Send,
        content: impl Into<String> + Send,
    ) -> Result<Note>;
    
    async fn get_note(&self, note_id: NoteId) -> Result<Note>;
    async fn update_note(&self, note: &Note) -> Result<Note>;
    async fn delete_note(&self, note_id: NoteId) -> Result<()>;
    async fn list_agent_notes(&self, agent_id: AgentId) -> Result<Vec<Note>>;
    async fn search_notes(&self, agent_id: AgentId, query: &str) -> Result<Vec<Note>>;
    
    // Tag operations
    async fn add_tag(&self, note_id: NoteId, tag: impl Into<String> + Send) -> Result<Note>;
    async fn remove_tag(&self, note_id: NoteId, tag: &str) -> Result<Note>;
    async fn list_notes_by_tag(&self, agent_id: AgentId, tag: &str) -> Result<Vec<Note>>;
    async fn get_all_tags(&self, agent_id: AgentId) -> Result<Vec<String>>;
    
    // Link operations
    async fn link_notes(
        &self,
        from_note_id: NoteId,
        to_note_id: NoteId,
        link_type: LinkType,
        context: Option<String>,
    ) -> Result<()>;
    
    async fn unlink_notes(&self, from_note_id: NoteId, to_note_id: NoteId, link_type: LinkType) -> Result<()>;
    async fn get_links_from(&self, note_id: NoteId, link_type: Option<LinkType>) -> Result<Vec<NoteLink>>;
    async fn get_links_to(&self, note_id: NoteId, link_type: Option<LinkType>) -> Result<Vec<NoteLink>>;
    async fn get_backlinks(&self, note_id: NoteId) -> Result<Vec<Note>>;
    
    // Graph traversal
    async fn get_related_notes(&self, note_id: NoteId, depth: usize) -> Result<Vec<Note>>;
    async fn find_path(&self, start_note_id: NoteId, end_note_id: NoteId, max_depth: usize) -> Result<Option<Vec<NoteId>>>;
    async fn get_note_graph(&self, note_id: NoteId, depth: usize) -> Result<NoteGraph>;
    
    // Luhmann ID operations (Zettelkasten addressing)
    async fn create_note_with_luhmann_id(
        &self,
        agent_id: AgentId,
        luhmann_id: LuhmannId,
        title: impl Into<String> + Send,
        content: impl Into<String> + Send,
    ) -> Result<Note>;
    
    async fn create_note_branch(
        &self,
        agent_id: AgentId,
        parent_note_id: NoteId,
        title: impl Into<String> + Send,
        content: impl Into<String> + Send,
    ) -> Result<Note>;
    
    async fn get_note_by_luhmann_id(&self, agent_id: AgentId, luhmann_id: &LuhmannId) -> Result<Option<Note>>;
    async fn get_next_available_id(&self, agent_id: AgentId, parent_id: Option<&LuhmannId>) -> Result<LuhmannId>;
    async fn list_notes_by_luhmann_prefix(&self, agent_id: AgentId, prefix: &LuhmannId) -> Result<Vec<Note>>;
}

/// Represents a subgraph of related notes
#[derive(Debug, Clone)]
pub struct NoteGraph {
    pub center_note_id: NoteId,
    pub notes: Vec<Note>,
    pub links: Vec<NoteLink>,
}

pub struct KnowledgeBaseServiceImpl<S: GraphStorage> {
    storage: S,
}

impl<S: GraphStorage> KnowledgeBaseServiceImpl<S> {
    pub fn new(storage: S) -> Self {
        Self { storage }
    }

    async fn note_exists(&self, note_id: NoteId) -> Result<bool> {
        match self.storage.get_node(note_id).await {
            Ok(node) => Ok(Note::from_node(&node).is_some()),
            Err(StorageError::NodeNotFound(_)) => Ok(false),
            Err(e) => Err(KbError::Storage(e)),
        }
    }
}

#[async_trait]
impl<S: GraphStorage> KnowledgeBaseService for KnowledgeBaseServiceImpl<S> {
    async fn create_knowledge_base(&self, agent_id: AgentId, name: impl Into<String> + Send) -> Result<KnowledgeBase> {
        // Clone agent_id for error handling
        let agent_id_for_err = agent_id.clone();
        // Get agent and verify it exists
        let node_id = string_to_node_id(&agent_id);
        let node = self.storage.get_node(node_id).await
            .map_err(|e| match e {
                StorageError::NodeNotFound(_) => KbError::AgentNotFound(agent_id_for_err),
                _ => KbError::Storage(e),
            })?;
        
        // Agent node becomes the knowledge base - update its properties
        let mut kb = KnowledgeBase::new(agent_id.clone(), name);
        kb.agent_id = agent_id.clone(); // Already set but explicit
        
        // Update the agent node with kb metadata
        let mut updated_node = node.clone();
        updated_node.properties.insert("kb_name".to_string(), PropertyValue::String(kb.name.clone()));
        updated_node.properties.insert("kb_enabled".to_string(), PropertyValue::Boolean(true));
        updated_node.properties.insert("next_main_id".to_string(), PropertyValue::Integer(1));
        
        self.storage.update_node(&updated_node).await?;
        Ok(kb)
    }

    async fn get_knowledge_base(&self, agent_id: AgentId) -> Result<KnowledgeBase> {
        let agent_id_err = agent_id.clone();
        let node_id = string_to_node_id(&agent_id);
        let node = self.storage.get_node(node_id).await
            .map_err(|e| match e {
                StorageError::NodeNotFound(_) => KbError::KnowledgeBaseNotFound(agent_id_err),
                _ => KbError::Storage(e),
            })?;
        
        // Check if kb_enabled is set
        let kb_enabled = node.get_property("kb_enabled")
            .and_then(|v| match v {
                PropertyValue::Boolean(b) => Some(*b),
                _ => None,
            })
            .unwrap_or(false);
        
        if !kb_enabled {
            return Err(KbError::KnowledgeBaseNotFound(agent_id.clone()));
        }
        
        let name = node.get_property("kb_name")
            .and_then(|v| v.as_str())
            .unwrap_or("Untitled KB")
            .to_string();
        
        let next_main_id = node.get_property("next_main_id")
            .and_then(|v| match v {
                PropertyValue::Integer(n) => Some(*n as u32),
                _ => Some(1),
            })
            .unwrap_or(1);
        
        // Get agent_id from node properties or use a default
        let agent_id = node.get_property("agent_id")
            .and_then(|v| v.as_str())
            .map(String::from)
            .unwrap_or_else(|| node.id.to_string());
        
        Ok(KnowledgeBase {
            agent_id,
            name,
            description: None,
            created_at: node.created_at,
            next_main_id,
        })
    }

    async fn create_note(
        &self,
        agent_id: AgentId,
        title: impl Into<String> + Send,
        content: impl Into<String> + Send,
    ) -> Result<Note> {
        // Clone agent_id for multiple uses
        let agent_id_for_err = agent_id.clone();
        
        // Verify agent/knowledge base exists
        let _ = self.storage.get_node(string_to_node_id(&agent_id)).await
            .map_err(|e| match e {
                StorageError::NodeNotFound(_) => KbError::AgentNotFound(agent_id_for_err),
                _ => KbError::Storage(e),
            })?;
        
        // Generate next available Luhmann ID for top-level note
        let luhmann_id = self.get_next_available_id(agent_id.clone(), None).await?;
        
        let note = Note::new(agent_id.clone(), title, content)
            .with_luhmann_id(luhmann_id);
        let node = note.to_node();
        self.storage.create_node(&node).await?;
        
        // Create ownership edge from agent to note
        let edge = Edge::new(
            "owns_note",
            string_to_node_id(&agent_id),
            note.id,
            Properties::new(),
        );
        self.storage.create_edge(&edge).await?;
        
        Ok(note)
    }

    async fn get_note(&self, note_id: NoteId) -> Result<Note> {
        let node = self.storage.get_node(note_id).await
            .map_err(|e| match e {
                StorageError::NodeNotFound(_) => KbError::NoteNotFound(note_id),
                _ => KbError::Storage(e),
            })?;
        
        Note::from_node(&node)
            .ok_or_else(|| KbError::NoteNotFound(note_id))
    }

    async fn update_note(&self, note: &Note) -> Result<Note> {
        // Verify note exists
        self.get_note(note.id).await?;
        
        let node = note.to_node();
        self.storage.update_node(&node).await?;
        Ok(note.clone())
    }

    async fn delete_note(&self, note_id: NoteId) -> Result<()> {
        // Verify note exists
        self.get_note(note_id).await?;
        
        // Delete the note (edges will be cascade deleted)
        self.storage.delete_node(note_id).await?;
        Ok(())
    }

    async fn list_agent_notes(&self, agent_id: AgentId) -> Result<Vec<Note>> {
        // Clone for error handling
        let agent_id_err = agent_id.clone();
        
        // Verify agent exists
        let agent_node_id = string_to_node_id(&agent_id);
        let _ = self.storage.get_node(agent_node_id).await
            .map_err(|e| match e {
                StorageError::NodeNotFound(_) => KbError::AgentNotFound(agent_id_err),
                _ => KbError::Storage(e),
            })?;
        
        // Get all notes owned by this agent
        let notes = self.storage
            .get_neighbors(agent_node_id, Some("owns_note"), EdgeDirection::Outgoing)
            .await?;
        
        let notes: Vec<Note> = notes.iter()
            .filter_map(Note::from_node)
            .collect();
        
        Ok(notes)
    }

    async fn search_notes(&self, agent_id: AgentId, query: &str) -> Result<Vec<Note>> {
        let all_notes = self.list_agent_notes(agent_id).await?;
        let query_lower = query.to_lowercase();
        
        let filtered: Vec<Note> = all_notes.into_iter()
            .filter(|note| {
                note.title.to_lowercase().contains(&query_lower) ||
                note.content.to_lowercase().contains(&query_lower) ||
                note.tags.iter().any(|tag| tag.to_lowercase().contains(&query_lower))
            })
            .collect();
        
        Ok(filtered)
    }

    async fn add_tag(&self, note_id: NoteId, tag: impl Into<String> + Send) -> Result<Note> {
        let mut note = self.get_note(note_id).await?;
        note.add_tag(tag);
        self.update_note(&note).await?;
        Ok(note)
    }

    async fn remove_tag(&self, note_id: NoteId, tag: &str) -> Result<Note> {
        let mut note = self.get_note(note_id).await?;
        note.remove_tag(tag);
        self.update_note(&note).await?;
        Ok(note)
    }

    async fn list_notes_by_tag(&self, agent_id: AgentId, tag: &str) -> Result<Vec<Note>> {
        let all_notes = self.list_agent_notes(agent_id).await?;
        let filtered: Vec<Note> = all_notes.into_iter()
            .filter(|note| note.tags.contains(&tag.to_string()))
            .collect();
        Ok(filtered)
    }

    async fn get_all_tags(&self, agent_id: AgentId) -> Result<Vec<String>> {
        let notes = self.list_agent_notes(agent_id).await?;
        let mut tags = HashSet::new();
        for note in notes {
            for tag in note.tags {
                tags.insert(tag);
            }
        }
        let mut tags: Vec<String> = tags.into_iter().collect();
        tags.sort();
        Ok(tags)
    }

    async fn link_notes(
        &self,
        from_note_id: NoteId,
        to_note_id: NoteId,
        link_type: LinkType,
        context: Option<String>,
    ) -> Result<()> {
        if from_note_id == to_note_id {
            return Err(KbError::SelfLink);
        }
        
        // Verify both notes exist
        self.get_note(from_note_id).await?;
        self.get_note(to_note_id).await?;
        
        // Create link edge with context in properties
        let mut props = Properties::new();
        if let Some(ctx) = context {
            props.insert("context".to_string(), crate::domain::PropertyValue::String(ctx));
        }
        
        let edge = Edge::new(
            link_type.as_str(),
            from_note_id,
            to_note_id,
            props,
        );
        
        self.storage.create_edge(&edge).await?;
        Ok(())
    }

    async fn unlink_notes(&self, from_note_id: NoteId, to_note_id: NoteId, link_type: LinkType) -> Result<()> {
        // Get edges from the source note
        let edges = self.storage.get_edges_from(from_note_id, Some(link_type.as_str())).await?;
        
        // Find and delete the specific edge
        for edge in edges {
            if edge.to_node_id == to_note_id {
                self.storage.delete_edge(edge.id).await?;
                return Ok(());
            }
        }
        
        Err(KbError::NoteNotFound(to_note_id))
    }

    async fn get_links_from(&self, note_id: NoteId, link_type: Option<LinkType>) -> Result<Vec<NoteLink>> {
        let edges = if let Some(lt) = link_type {
            self.storage.get_edges_from(note_id, Some(lt.as_str())).await?
        } else {
            self.storage.get_edges_from(note_id, None).await?
        };
        
        let links: Vec<NoteLink> = edges.iter()
            .filter_map(|edge| {
                LinkType::from_str(&edge.edge_type).map(|lt| {
                    let context = edge.properties.get("context")
                        .and_then(|v| v.as_str())
                        .map(String::from);
                    
                    NoteLink::new(edge.from_node_id, edge.to_node_id, lt, context)
                })
            })
            .collect();
        
        Ok(links)
    }

    async fn get_links_to(&self, note_id: NoteId, link_type: Option<LinkType>) -> Result<Vec<NoteLink>> {
        let edges = if let Some(lt) = link_type {
            self.storage.get_edges_to(note_id, Some(lt.as_str())).await?
        } else {
            self.storage.get_edges_to(note_id, None).await?
        };
        
        let links: Vec<NoteLink> = edges.iter()
            .filter_map(|edge| {
                LinkType::from_str(&edge.edge_type).map(|lt| {
                    let context = edge.properties.get("context")
                        .and_then(|v| v.as_str())
                        .map(String::from);
                    
                    NoteLink::new(edge.from_node_id, edge.to_node_id, lt, context)
                })
            })
            .collect();
        
        Ok(links)
    }

    async fn get_backlinks(&self, note_id: NoteId) -> Result<Vec<Note>> {
        // Get all edges pointing to this note
        let edges = self.storage.get_edges_to(note_id, None).await?;
        
        let mut notes = Vec::new();
        for edge in edges {
            if let Ok(note) = self.get_note(edge.from_node_id).await {
                notes.push(note);
            }
        }
        
        Ok(notes)
    }

    async fn get_related_notes(&self, note_id: NoteId, depth: usize) -> Result<Vec<Note>> {
        if depth == 0 {
            return Ok(vec![]);
        }
        
        let mut visited = HashSet::new();
        let mut result = Vec::new();
        let mut queue = VecDeque::new();
        
        queue.push_back((note_id, 0usize));
        visited.insert(note_id);
        
        while let Some((current_id, current_depth)) = queue.pop_front() {
            if current_depth >= depth {
                continue;
            }
            
            // Get all neighbors (both outgoing and incoming edges)
            let neighbors = self.storage
                .get_neighbors(current_id, None, EdgeDirection::Both)
                .await?;
            
            for neighbor in neighbors {
                if visited.insert(neighbor.id) {
                    if let Some(note) = Note::from_node(&neighbor) {
                        result.push(note);
                        queue.push_back((neighbor.id, current_depth + 1));
                    }
                }
            }
        }
        
        Ok(result)
    }

    async fn find_path(&self, start_note_id: NoteId, end_note_id: NoteId, max_depth: usize) -> Result<Option<Vec<NoteId>>> {
        if start_note_id == end_note_id {
            return Ok(Some(vec![start_note_id]));
        }
        
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        let mut parent_map: HashMap<NoteId, NoteId> = HashMap::new();
        
        queue.push_back((start_note_id, 0usize));
        visited.insert(start_note_id);
        
        while let Some((current_id, depth)) = queue.pop_front() {
            if depth >= max_depth {
                continue;
            }
            
            // Get neighbors (outgoing only for path finding)
            let neighbors = self.storage
                .get_neighbors(current_id, None, EdgeDirection::Outgoing)
                .await?;
            
            for neighbor in neighbors {
                if visited.insert(neighbor.id) {
                    parent_map.insert(neighbor.id, current_id);
                    
                    if neighbor.id == end_note_id {
                        // Reconstruct path
                        let mut path = vec![end_note_id];
                        let mut current = end_note_id;
                        
                        while let Some(&parent) = parent_map.get(&current) {
                            path.push(parent);
                            current = parent;
                        }
                        
                        path.reverse();
                        return Ok(Some(path));
                    }
                    
                    queue.push_back((neighbor.id, depth + 1));
                }
            }
        }
        
        Ok(None)
    }

    async fn get_note_graph(&self, note_id: NoteId, depth: usize) -> Result<NoteGraph> {
        let center_note = self.get_note(note_id).await?;
        let related_notes = self.get_related_notes(note_id, depth).await?;
        
        let mut notes = vec![center_note.clone()];
        notes.extend(related_notes);
        
        // Collect all links between notes in the graph
        let mut links = Vec::new();
        let note_ids: HashSet<NoteId> = notes.iter().map(|n| n.id).collect();
        
        for note in &notes {
            let outgoing = self.get_links_from(note.id, None).await?;
            for link in outgoing {
                if note_ids.contains(&link.to_note_id) {
                    links.push(link);
                }
            }
        }
        
        Ok(NoteGraph {
            center_note_id: note_id,
            notes,
            links,
        })
    }

    async fn create_note_with_luhmann_id(
        &self,
        agent_id: AgentId,
        luhmann_id: LuhmannId,
        title: impl Into<String> + Send,
        content: impl Into<String> + Send,
    ) -> Result<Note> {
        // Clone agent_id for multiple uses
        let agent_id_for_err = agent_id.clone();
        
        // Verify agent/knowledge base exists
        let _ = self.storage.get_node(string_to_node_id(&agent_id)).await
            .map_err(|e| match e {
                StorageError::NodeNotFound(_) => KbError::AgentNotFound(agent_id_for_err),
                _ => KbError::Storage(e),
            })?;
        
        let note = Note::new(agent_id.clone(), title, content)
            .with_luhmann_id(luhmann_id);
        let node = note.to_node();
        self.storage.create_node(&node).await?;
        
        // Create ownership edge from agent to note
        let edge = Edge::new(
            "owns_note",
            string_to_node_id(&agent_id),
            note.id,
            Properties::new(),
        );
        self.storage.create_edge(&edge).await?;
        
        Ok(note)
    }

    async fn create_note_branch(
        &self,
        agent_id: AgentId,
        parent_note_id: NoteId,
        title: impl Into<String> + Send,
        content: impl Into<String> + Send,
    ) -> Result<Note> {
        // Get parent note to find its Luhmann ID
        let parent_note = self.get_note(parent_note_id).await?;
        
        let parent_luhmann_id = parent_note.luhmann_id
            .ok_or_else(|| KbError::NoteNotFound(parent_note_id))?;
        
        // Generate the next available child ID
        let child_id = self.get_next_available_id(agent_id.clone(), Some(&parent_luhmann_id)).await?;
        
        // Create the note with the Luhmann ID
        let note = self.create_note_with_luhmann_id(
            agent_id,
            child_id.clone(),
            title,
            content,
        ).await?;
        
        // Create reference link to parent
        self.link_notes(note.id, parent_note_id, LinkType::References, Some(format!("Branch of {}", parent_luhmann_id))).await?;
        
        Ok(note)
    }

    async fn get_note_by_luhmann_id(&self, agent_id: AgentId, luhmann_id: &LuhmannId) -> Result<Option<Note>> {
        // Get all notes for this agent and find the one with matching Luhmann ID
        let notes = self.list_agent_notes(agent_id).await?;
        
        Ok(notes.into_iter()
            .find(|note| note.luhmann_id.as_ref() == Some(luhmann_id)))
    }

    async fn get_next_available_id(&self, agent_id: AgentId, parent_id: Option<&LuhmannId>) -> Result<LuhmannId> {
        let all_notes = self.list_agent_notes(agent_id).await?;
        
        // Collect all existing Luhmann IDs at the specified level
        let existing_ids: Vec<LuhmannId> = all_notes
            .iter()
            .filter_map(|note| note.luhmann_id.clone())
            .filter(|id| {
                if let Some(parent) = parent_id {
                    // Check if this ID is a direct child of the parent
                    id.parent().as_ref() == Some(parent)
                } else {
                    // Top-level IDs have no parent
                    id.parent().is_none()
                }
            })
            .collect();
        
        if let Some(parent) = parent_id {
            // Generate next sibling under parent
            if existing_ids.is_empty() {
                // First child of parent
                Ok(parent.first_child())
            } else {
                // Find the last child and get next sibling
                let mut sorted = existing_ids.clone();
                sorted.sort();
                if let Some(last) = sorted.last() {
                    Ok(last.next_sibling()
                        .unwrap_or_else(|| last.first_child()))
                } else {
                    Ok(parent.first_child())
                }
            }
        } else {
            // Top-level - generate next main topic ID
            if existing_ids.is_empty() {
                Ok(LuhmannId { parts: vec![crate::services::kb::domain::LuhmannPart::Number(1)] })
            } else {
                let mut sorted = existing_ids;
                sorted.sort();
                if let Some(last) = sorted.last() {
                    Ok(last.next_sibling()
                        .unwrap_or_else(|| LuhmannId { parts: vec![crate::services::kb::domain::LuhmannPart::Number(1)] }))
                } else {
                    Ok(LuhmannId { parts: vec![crate::services::kb::domain::LuhmannPart::Number(1)] })
                }
            }
        }
    }

    async fn list_notes_by_luhmann_prefix(&self, agent_id: AgentId, prefix: &LuhmannId) -> Result<Vec<Note>> {
        let all_notes = self.list_agent_notes(agent_id).await?;
        
        // Filter notes whose Luhmann ID starts with the prefix
        let filtered: Vec<Note> = all_notes
            .into_iter()
            .filter(|note| {
                if let Some(ref lid) = note.luhmann_id {
                    lid.is_descendant_of(prefix) || lid == prefix
                } else {
                    false
                }
            })
            .collect();
        
        Ok(filtered)
    }
}