phago-runtime 1.0.0

Colony management, scheduling, and runtime for Phago biological computing
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
699
700
701
702
703
704
705
//! SQLite-backed implementation of the TopologyGraph trait.
//!
//! Provides persistent storage for large knowledge graphs that don't fit in memory.
//! Uses SQLite for storage with indexes on node labels and edge endpoints.

#![cfg(feature = "sqlite")]

use phago_core::topology::TopologyGraph;
use phago_core::types::*;
use rusqlite::{params, Connection, Result as SqlResult};
use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, Mutex};

/// SQLite-backed implementation of the topology graph.
///
/// Stores nodes and edges in SQLite tables with appropriate indexes.
/// Supports both in-memory and file-backed databases.
pub struct SqliteTopologyGraph {
    conn: Arc<Mutex<Connection>>,
    /// Cache for frequently accessed nodes (LRU-style, limited size)
    node_cache: HashMap<NodeId, NodeData>,
    cache_size: usize,
}

impl SqliteTopologyGraph {
    /// Create a new in-memory SQLite graph.
    pub fn new_in_memory() -> SqlResult<Self> {
        let conn = Connection::open_in_memory()?;
        Self::init_with_connection(conn)
    }

    /// Create or open a file-backed SQLite graph.
    pub fn open<P: AsRef<Path>>(path: P) -> SqlResult<Self> {
        let conn = Connection::open(path)?;
        Self::init_with_connection(conn)
    }

    fn init_with_connection(conn: Connection) -> SqlResult<Self> {
        // Enable WAL mode for better concurrent read performance
        conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL;")?;

        // Create tables
        conn.execute_batch(
            r#"
            CREATE TABLE IF NOT EXISTS nodes (
                id TEXT PRIMARY KEY,
                label TEXT NOT NULL,
                node_type TEXT NOT NULL,
                position_x REAL NOT NULL,
                position_y REAL NOT NULL,
                access_count INTEGER NOT NULL DEFAULT 1,
                created_tick INTEGER NOT NULL DEFAULT 0,
                embedding BLOB
            );

            CREATE TABLE IF NOT EXISTS edges (
                from_id TEXT NOT NULL,
                to_id TEXT NOT NULL,
                weight REAL NOT NULL,
                co_activations INTEGER NOT NULL DEFAULT 1,
                created_tick INTEGER NOT NULL DEFAULT 0,
                last_activated_tick INTEGER NOT NULL DEFAULT 0,
                PRIMARY KEY (from_id, to_id),
                FOREIGN KEY (from_id) REFERENCES nodes(id),
                FOREIGN KEY (to_id) REFERENCES nodes(id)
            );

            CREATE INDEX IF NOT EXISTS idx_nodes_label ON nodes(label);
            CREATE INDEX IF NOT EXISTS idx_edges_from ON edges(from_id);
            CREATE INDEX IF NOT EXISTS idx_edges_to ON edges(to_id);
            "#,
        )?;

        Ok(Self {
            conn: Arc::new(Mutex::new(conn)),
            node_cache: HashMap::new(),
            cache_size: 1000,
        })
    }

    /// Set the cache size for frequently accessed nodes.
    pub fn with_cache_size(mut self, size: usize) -> Self {
        self.cache_size = size;
        self
    }

    /// Get database statistics.
    pub fn stats(&self) -> SqlResult<(usize, usize)> {
        let conn = self.conn.lock().unwrap();
        let node_count: usize = conn.query_row("SELECT COUNT(*) FROM nodes", [], |row| row.get(0))?;
        let edge_count: usize = conn.query_row("SELECT COUNT(*) FROM edges", [], |row| row.get(0))?;
        Ok((node_count, edge_count))
    }

    /// Clear the in-memory cache.
    pub fn clear_cache(&mut self) {
        self.node_cache.clear();
    }

    /// Iterate over all nodes with their data.
    ///
    /// This is useful for bulk loading from SQLite into another graph backend.
    pub fn iter_nodes(&self) -> impl Iterator<Item = NodeData> + '_ {
        let conn = self.conn.lock().unwrap();
        let mut stmt = conn
            .prepare("SELECT id, label, node_type, position_x, position_y, access_count, created_tick, embedding FROM nodes")
            .expect("Failed to prepare statement");

        let nodes: Vec<NodeData> = stmt
            .query_map([], |row| {
                let id_str: String = row.get(0)?;
                let embedding_bytes: Option<Vec<u8>> = row.get(7)?;
                Ok(NodeData {
                    id: NodeId(uuid::Uuid::parse_str(&id_str).unwrap_or_default()),
                    label: row.get(1)?,
                    node_type: Self::string_to_node_type(&row.get::<_, String>(2)?),
                    position: Position::new(row.get(3)?, row.get(4)?),
                    access_count: row.get(5)?,
                    created_tick: row.get(6)?,
                    embedding: Self::deserialize_embedding(embedding_bytes),
                })
            })
            .expect("Failed to query nodes")
            .filter_map(|r| r.ok())
            .collect();

        nodes.into_iter()
    }

    /// Iterate over all edges with their data.
    ///
    /// Returns (from_id, to_id, edge_data) tuples.
    pub fn iter_edges(&self) -> impl Iterator<Item = (NodeId, NodeId, EdgeData)> + '_ {
        let conn = self.conn.lock().unwrap();
        let mut stmt = conn
            .prepare("SELECT from_id, to_id, weight, co_activations, created_tick, last_activated_tick FROM edges")
            .expect("Failed to prepare statement");

        let edges: Vec<(NodeId, NodeId, EdgeData)> = stmt
            .query_map([], |row| {
                let from_str: String = row.get(0)?;
                let to_str: String = row.get(1)?;
                Ok((
                    NodeId(uuid::Uuid::parse_str(&from_str).unwrap_or_default()),
                    NodeId(uuid::Uuid::parse_str(&to_str).unwrap_or_default()),
                    EdgeData {
                        weight: row.get(2)?,
                        co_activations: row.get(3)?,
                        created_tick: row.get(4)?,
                        last_activated_tick: row.get(5)?,
                    },
                ))
            })
            .expect("Failed to query edges")
            .filter_map(|r| r.ok())
            .collect();

        edges.into_iter()
    }

    fn node_type_to_string(nt: &NodeType) -> &'static str {
        match nt {
            NodeType::Concept => "Concept",
            NodeType::Insight => "Insight",
            NodeType::Anomaly => "Anomaly",
            NodeType::Document => "Document",
        }
    }

    fn string_to_node_type(s: &str) -> NodeType {
        match s {
            "Insight" => NodeType::Insight,
            "Anomaly" => NodeType::Anomaly,
            "Document" => NodeType::Document,
            _ => NodeType::Concept,
        }
    }

    fn serialize_embedding(embedding: &Option<Vec<f32>>) -> Option<Vec<u8>> {
        embedding.as_ref().map(|e| {
            e.iter()
                .flat_map(|f| f.to_le_bytes())
                .collect()
        })
    }

    fn deserialize_embedding(bytes: Option<Vec<u8>>) -> Option<Vec<f32>> {
        bytes.map(|b| {
            b.chunks_exact(4)
                .map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
                .collect()
        })
    }
}

impl TopologyGraph for SqliteTopologyGraph {
    fn add_node(&mut self, data: NodeData) -> NodeId {
        let id = data.id;
        let conn = self.conn.lock().unwrap();

        let embedding_bytes = Self::serialize_embedding(&data.embedding);

        conn.execute(
            "INSERT OR REPLACE INTO nodes (id, label, node_type, position_x, position_y, access_count, created_tick, embedding)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
            params![
                id.0.to_string(),
                data.label,
                Self::node_type_to_string(&data.node_type),
                data.position.x,
                data.position.y,
                data.access_count,
                data.created_tick,
                embedding_bytes,
            ],
        ).expect("Failed to insert node");

        // Add to cache if there's room
        if self.node_cache.len() < self.cache_size {
            drop(conn);
            self.node_cache.insert(id, data);
        }

        id
    }

    fn get_node(&self, id: &NodeId) -> Option<&NodeData> {
        // Check cache first
        self.node_cache.get(id)
    }

    fn get_node_mut(&mut self, id: &NodeId) -> Option<&mut NodeData> {
        // For mutable access, we need to load into cache first
        if !self.node_cache.contains_key(id) {
            let conn = self.conn.lock().unwrap();
            let node: Option<NodeData> = conn
                .query_row(
                    "SELECT id, label, node_type, position_x, position_y, access_count, created_tick, embedding
                     FROM nodes WHERE id = ?1",
                    params![id.0.to_string()],
                    |row| {
                        let id_str: String = row.get(0)?;
                        let embedding_bytes: Option<Vec<u8>> = row.get(7)?;
                        Ok(NodeData {
                            id: NodeId(uuid::Uuid::parse_str(&id_str).unwrap_or_default()),
                            label: row.get(1)?,
                            node_type: Self::string_to_node_type(&row.get::<_, String>(2)?),
                            position: Position::new(row.get(3)?, row.get(4)?),
                            access_count: row.get(5)?,
                            created_tick: row.get(6)?,
                            embedding: Self::deserialize_embedding(embedding_bytes),
                        })
                    },
                )
                .ok();

            if let Some(node) = node {
                drop(conn);
                self.node_cache.insert(*id, node);
            }
        }

        self.node_cache.get_mut(id)
    }

    fn set_edge(&mut self, from: NodeId, to: NodeId, data: EdgeData) {
        let conn = self.conn.lock().unwrap();
        conn.execute(
            "INSERT OR REPLACE INTO edges (from_id, to_id, weight, co_activations, created_tick, last_activated_tick)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
            params![
                from.0.to_string(),
                to.0.to_string(),
                data.weight,
                data.co_activations,
                data.created_tick,
                data.last_activated_tick,
            ],
        ).expect("Failed to insert edge");
    }

    fn get_edge(&self, _from: &NodeId, _to: &NodeId) -> Option<&EdgeData> {
        // SQLite backend returns owned data, so we can't return a reference
        // This is a limitation - for persistent backends, we'd need a different pattern
        None
    }

    fn get_edge_mut(&mut self, _from: &NodeId, _to: &NodeId) -> Option<&mut EdgeData> {
        // Same limitation as get_edge
        None
    }

    fn neighbors(&self, _node: &NodeId) -> Vec<(NodeId, &EdgeData)> {
        // Can't return references to edges from SQLite
        // Users should use all_edges or a custom query
        Vec::new()
    }

    fn remove_edge(&mut self, from: &NodeId, to: &NodeId) -> Option<EdgeData> {
        let conn = self.conn.lock().unwrap();

        // First get the edge data
        let edge: Option<EdgeData> = conn
            .query_row(
                "SELECT weight, co_activations, created_tick, last_activated_tick FROM edges WHERE from_id = ?1 AND to_id = ?2",
                params![from.0.to_string(), to.0.to_string()],
                |row| {
                    Ok(EdgeData {
                        weight: row.get(0)?,
                        co_activations: row.get(1)?,
                        created_tick: row.get(2)?,
                        last_activated_tick: row.get(3)?,
                    })
                },
            )
            .ok();

        // Then delete it
        conn.execute(
            "DELETE FROM edges WHERE from_id = ?1 AND to_id = ?2",
            params![from.0.to_string(), to.0.to_string()],
        ).ok();

        edge
    }

    fn all_nodes(&self) -> Vec<NodeId> {
        let conn = self.conn.lock().unwrap();
        let mut stmt = conn.prepare("SELECT id FROM nodes").expect("Failed to prepare statement");
        let nodes: Vec<NodeId> = stmt
            .query_map([], |row| {
                let id_str: String = row.get(0)?;
                Ok(NodeId(uuid::Uuid::parse_str(&id_str).unwrap_or_default()))
            })
            .expect("Failed to query nodes")
            .filter_map(|r| r.ok())
            .collect();
        nodes
    }

    fn all_edges(&self) -> Vec<(NodeId, NodeId, &EdgeData)> {
        // Can't return references from SQLite
        Vec::new()
    }

    fn node_count(&self) -> usize {
        let conn = self.conn.lock().unwrap();
        conn.query_row("SELECT COUNT(*) FROM nodes", [], |row| row.get(0))
            .unwrap_or(0)
    }

    fn edge_count(&self) -> usize {
        let conn = self.conn.lock().unwrap();
        conn.query_row("SELECT COUNT(*) FROM edges", [], |row| row.get(0))
            .unwrap_or(0)
    }

    fn decay_edges(&mut self, rate: f64, prune_threshold: f64) -> Vec<PrunedConnection> {
        let conn = self.conn.lock().unwrap();

        // Decay all edge weights
        conn.execute(
            "UPDATE edges SET weight = weight * (1.0 - ?1)",
            params![rate],
        ).expect("Failed to decay edges");

        // Get edges below threshold
        let mut stmt = conn
            .prepare("SELECT from_id, to_id, weight FROM edges WHERE weight < ?1")
            .expect("Failed to prepare statement");

        let pruned: Vec<PrunedConnection> = stmt
            .query_map(params![prune_threshold], |row| {
                let from_str: String = row.get(0)?;
                let to_str: String = row.get(1)?;
                Ok(PrunedConnection {
                    from: NodeId(uuid::Uuid::parse_str(&from_str).unwrap_or_default()),
                    to: NodeId(uuid::Uuid::parse_str(&to_str).unwrap_or_default()),
                    final_weight: row.get(2)?,
                })
            })
            .expect("Failed to query pruned edges")
            .filter_map(|r| r.ok())
            .collect();

        // Delete pruned edges
        conn.execute(
            "DELETE FROM edges WHERE weight < ?1",
            params![prune_threshold],
        ).expect("Failed to delete pruned edges");

        pruned
    }

    fn decay_edges_activity(
        &mut self,
        base_rate: f64,
        prune_threshold: f64,
        current_tick: u64,
        staleness_factor: f64,
        maturation_ticks: u64,
    ) -> Vec<PrunedConnection> {
        let conn = self.conn.lock().unwrap();

        // For SQLite, we use a simplified approach:
        // 1. Decay mature edges faster if they haven't been activated recently
        // 2. Use base_rate for young edges

        // Calculate decay for each edge based on staleness
        // This is done in Rust since SQLite doesn't have complex math functions
        let mut stmt = conn
            .prepare("SELECT from_id, to_id, weight, co_activations, created_tick, last_activated_tick FROM edges")
            .expect("Failed to prepare statement");

        let edges: Vec<(String, String, f64, u64, u64, u64)> = stmt
            .query_map([], |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, String>(1)?,
                    row.get::<_, f64>(2)?,
                    row.get::<_, u64>(3)?,
                    row.get::<_, u64>(4)?,
                    row.get::<_, u64>(5)?,
                ))
            })
            .expect("Failed to query edges")
            .filter_map(|r| r.ok())
            .collect();

        drop(stmt);

        let mut pruned = Vec::new();

        for (from_str, to_str, weight, co_activations, created_tick, last_activated_tick) in edges {
            let edge_age = current_tick.saturating_sub(created_tick);

            let decay_rate = if edge_age < maturation_ticks {
                base_rate
            } else {
                let staleness = current_tick.saturating_sub(last_activated_tick);
                let activity_factor = 1.0 / (1.0 + co_activations as f64);
                base_rate * (1.0 + staleness_factor * staleness as f64 * activity_factor / 100.0)
            };

            let new_weight = weight * (1.0 - decay_rate.min(0.5));

            if new_weight < prune_threshold {
                pruned.push(PrunedConnection {
                    from: NodeId(uuid::Uuid::parse_str(&from_str).unwrap_or_default()),
                    to: NodeId(uuid::Uuid::parse_str(&to_str).unwrap_or_default()),
                    final_weight: new_weight,
                });
                conn.execute(
                    "DELETE FROM edges WHERE from_id = ?1 AND to_id = ?2",
                    params![from_str, to_str],
                ).ok();
            } else {
                conn.execute(
                    "UPDATE edges SET weight = ?1 WHERE from_id = ?2 AND to_id = ?3",
                    params![new_weight, from_str, to_str],
                ).ok();
            }
        }

        pruned
    }

    fn prune_to_max_degree(&mut self, max_degree: usize) -> Vec<PrunedConnection> {
        let conn = self.conn.lock().unwrap();
        let mut pruned = Vec::new();

        // Get all nodes
        let nodes: Vec<String> = conn
            .prepare("SELECT id FROM nodes")
            .expect("prepare")
            .query_map([], |row| row.get(0))
            .expect("query")
            .filter_map(|r| r.ok())
            .collect();

        for node_id in nodes {
            // Get edges sorted by weight (ascending, so weakest first)
            let edges: Vec<(String, String, f64)> = conn
                .prepare(
                    "SELECT from_id, to_id, weight FROM edges
                     WHERE from_id = ?1 OR to_id = ?1
                     ORDER BY weight ASC",
                )
                .expect("prepare")
                .query_map(params![&node_id], |row| {
                    Ok((row.get(0)?, row.get(1)?, row.get(2)?))
                })
                .expect("query")
                .filter_map(|r| r.ok())
                .collect();

            if edges.len() > max_degree {
                // Prune the weakest edges
                let to_prune = edges.len() - max_degree;
                for (from_str, to_str, weight) in edges.into_iter().take(to_prune) {
                    pruned.push(PrunedConnection {
                        from: NodeId(uuid::Uuid::parse_str(&from_str).unwrap_or_default()),
                        to: NodeId(uuid::Uuid::parse_str(&to_str).unwrap_or_default()),
                        final_weight: weight,
                    });
                    conn.execute(
                        "DELETE FROM edges WHERE from_id = ?1 AND to_id = ?2",
                        params![from_str, to_str],
                    ).ok();
                }
            }
        }

        pruned
    }

    fn find_nodes_by_label(&self, query: &str) -> Vec<NodeId> {
        let conn = self.conn.lock().unwrap();
        let pattern = format!("%{}%", query.to_lowercase());
        let mut stmt = conn
            .prepare("SELECT id FROM nodes WHERE LOWER(label) LIKE ?1")
            .expect("Failed to prepare statement");

        stmt.query_map(params![pattern], |row| {
            let id_str: String = row.get(0)?;
            Ok(NodeId(uuid::Uuid::parse_str(&id_str).unwrap_or_default()))
        })
        .expect("Failed to query nodes")
        .filter_map(|r| r.ok())
        .collect()
    }

    fn find_nodes_by_exact_label(&self, label: &str) -> Vec<NodeId> {
        let conn = self.conn.lock().unwrap();
        let label_lower = label.to_lowercase();
        let mut stmt = conn
            .prepare("SELECT id FROM nodes WHERE LOWER(label) = ?1")
            .expect("Failed to prepare statement");

        stmt.query_map(params![label_lower], |row| {
            let id_str: String = row.get(0)?;
            Ok(NodeId(uuid::Uuid::parse_str(&id_str).unwrap_or_default()))
        })
        .expect("Failed to query nodes")
        .filter_map(|r| r.ok())
        .collect()
    }

    fn shortest_path(&self, _from: &NodeId, _to: &NodeId) -> Option<(Vec<NodeId>, f64)> {
        // SQLite doesn't support graph algorithms directly
        // Would need to implement Dijkstra in Rust with SQL queries
        None
    }

    fn betweenness_centrality(&self, _sample_size: usize) -> Vec<(NodeId, f64)> {
        // Complex to implement with SQL
        Vec::new()
    }

    fn bridge_nodes(&self, _top_k: usize) -> Vec<(NodeId, f64)> {
        Vec::new()
    }

    fn connected_components(&self) -> usize {
        // Would require union-find or BFS implementation
        1
    }
}

#[cfg(all(test, feature = "sqlite"))]
mod tests {
    use super::*;

    #[test]
    fn create_in_memory() {
        let graph = SqliteTopologyGraph::new_in_memory().unwrap();
        assert_eq!(graph.node_count(), 0);
        assert_eq!(graph.edge_count(), 0);
    }

    #[test]
    fn add_and_count_nodes() {
        let mut graph = SqliteTopologyGraph::new_in_memory().unwrap();

        for i in 0..100 {
            graph.add_node(NodeData {
                id: NodeId::new(),
                label: format!("node_{}", i),
                node_type: NodeType::Concept,
                position: Position::new(i as f64, 0.0),
                access_count: 1,
                created_tick: 0,
                embedding: None,
            });
        }

        assert_eq!(graph.node_count(), 100);
    }

    #[test]
    fn add_and_count_edges() {
        let mut graph = SqliteTopologyGraph::new_in_memory().unwrap();

        let n1 = graph.add_node(NodeData {
            id: NodeId::new(),
            label: "a".to_string(),
            node_type: NodeType::Concept,
            position: Position::new(0.0, 0.0),
            access_count: 1,
            created_tick: 0,
            embedding: None,
        });

        let n2 = graph.add_node(NodeData {
            id: NodeId::new(),
            label: "b".to_string(),
            node_type: NodeType::Concept,
            position: Position::new(1.0, 0.0),
            access_count: 1,
            created_tick: 0,
            embedding: None,
        });

        graph.set_edge(n1, n2, EdgeData {
            weight: 0.5,
            co_activations: 1,
            created_tick: 0,
            last_activated_tick: 0,
        });

        assert_eq!(graph.edge_count(), 1);
    }

    #[test]
    fn find_nodes_by_label() {
        let mut graph = SqliteTopologyGraph::new_in_memory().unwrap();

        graph.add_node(NodeData {
            id: NodeId::new(),
            label: "cell membrane".to_string(),
            node_type: NodeType::Concept,
            position: Position::new(0.0, 0.0),
            access_count: 1,
            created_tick: 0,
            embedding: None,
        });

        graph.add_node(NodeData {
            id: NodeId::new(),
            label: "protein".to_string(),
            node_type: NodeType::Concept,
            position: Position::new(1.0, 0.0),
            access_count: 1,
            created_tick: 0,
            embedding: None,
        });

        let results = graph.find_nodes_by_label("cell");
        assert_eq!(results.len(), 1);

        let results = graph.find_nodes_by_label("membrane");
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn decay_removes_weak_edges() {
        let mut graph = SqliteTopologyGraph::new_in_memory().unwrap();

        let n1 = graph.add_node(NodeData {
            id: NodeId::new(),
            label: "a".to_string(),
            node_type: NodeType::Concept,
            position: Position::new(0.0, 0.0),
            access_count: 1,
            created_tick: 0,
            embedding: None,
        });

        let n2 = graph.add_node(NodeData {
            id: NodeId::new(),
            label: "b".to_string(),
            node_type: NodeType::Concept,
            position: Position::new(1.0, 0.0),
            access_count: 1,
            created_tick: 0,
            embedding: None,
        });

        // Add a weak edge
        graph.set_edge(n1, n2, EdgeData {
            weight: 0.1,
            co_activations: 1,
            created_tick: 0,
            last_activated_tick: 0,
        });

        assert_eq!(graph.edge_count(), 1);

        // Decay aggressively
        let pruned = graph.decay_edges(0.9, 0.05);
        assert_eq!(pruned.len(), 1);
        assert_eq!(graph.edge_count(), 0);
    }
}