octocode 0.10.0

AI-powered code indexer with semantic search, GraphRAG knowledge graphs, and MCP server for multi-language codebases
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use anyhow::Result;
use chrono::Utc;
use std::sync::Arc;

// Arrow imports
use arrow::array::{Array, FixedSizeListArray, Float32Array, StringArray};
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;

// LanceDB imports
use futures::TryStreamExt;
use lancedb::{
	connect,
	index::Index,
	query::{ExecutableQuery, QueryBase},
	Connection, DistanceType,
};

use super::types::{Memory, MemoryConfig, MemoryQuery, MemoryRelationship, MemorySearchResult};

/// LanceDB-based storage for memories with vector search capabilities
pub struct MemoryStore {
	db: Connection,
	embedding_provider: Box<dyn crate::embedding::provider::EmbeddingProvider>,
	config: MemoryConfig,
	main_config: crate::config::Config,
	vector_dim: usize,
}

impl MemoryStore {
	/// Create a new memory store
	pub async fn new(
		db_path: &str,
		embedding_provider: Box<dyn crate::embedding::provider::EmbeddingProvider>,
		config: MemoryConfig,
		main_config: crate::config::Config,
	) -> Result<Self> {
		// Connect to LanceDB
		let db = connect(db_path).execute().await?;

		// Get vector dimension from the embedding provider by testing with a short text
		let test_embedding = embedding_provider.generate_embedding("test").await?;
		let vector_dim = test_embedding.len();

		let store = Self {
			db,
			embedding_provider,
			config,
			main_config,
			vector_dim,
		};

		// Initialize tables
		store.initialize_tables().await?;

		// Ensure optimal vector index (only during initialization, not on every store)
		store.ensure_optimal_index().await?;

		Ok(store)
	}

	/// Initialize memory and relationship tables
	async fn initialize_tables(&self) -> Result<()> {
		let table_names = self.db.table_names().execute().await?;

		// Create memories table if it doesn't exist
		if !table_names.contains(&"memories".to_string()) {
			let schema = Arc::new(Schema::new(vec![
				Field::new("id", DataType::Utf8, false),
				Field::new("memory_type", DataType::Utf8, false),
				Field::new("title", DataType::Utf8, false),
				Field::new("content", DataType::Utf8, false),
				Field::new("created_at", DataType::Utf8, false),
				Field::new("updated_at", DataType::Utf8, false),
				Field::new("importance", DataType::Float32, false),
				Field::new("confidence", DataType::Float32, false),
				Field::new("tags", DataType::Utf8, true), // JSON serialized
				Field::new("related_files", DataType::Utf8, true), // JSON serialized
				Field::new("git_commit", DataType::Utf8, true),
				Field::new(
					"embedding",
					DataType::FixedSizeList(
						Arc::new(Field::new("item", DataType::Float32, true)),
						self.vector_dim as i32,
					),
					true,
				),
			]));

			self.db
				.create_empty_table("memories", schema)
				.execute()
				.await?;
		}

		// Create relationships table if it doesn't exist
		if !table_names.contains(&"memory_relationships".to_string()) {
			let schema = Arc::new(Schema::new(vec![
				Field::new("id", DataType::Utf8, false),
				Field::new("source_id", DataType::Utf8, false),
				Field::new("target_id", DataType::Utf8, false),
				Field::new("relationship_type", DataType::Utf8, false),
				Field::new("strength", DataType::Float32, false),
				Field::new("description", DataType::Utf8, false),
				Field::new("created_at", DataType::Utf8, false),
			]));

			self.db
				.create_empty_table("memory_relationships", schema)
				.execute()
				.await?;
		}

		Ok(())
	}

	/// Store a memory
	pub async fn store_memory(&mut self, memory: &Memory) -> Result<()> {
		// Generate embedding using the optimized single embedding function for better performance
		let embedding = crate::embedding::generate_embeddings(
			&memory.get_searchable_text(),
			false,
			&self.main_config,
		)
		.await?;

		self.store_memory_with_embedding(memory, embedding).await
	}

	/// Store a memory with a pre-computed embedding (for batch operations)
	async fn store_memory_with_embedding(
		&mut self,
		memory: &Memory,
		embedding: Vec<f32>,
	) -> Result<()> {
		// Create record batch
		let schema = Arc::new(Schema::new(vec![
			Field::new("id", DataType::Utf8, false),
			Field::new("memory_type", DataType::Utf8, false),
			Field::new("title", DataType::Utf8, false),
			Field::new("content", DataType::Utf8, false),
			Field::new("created_at", DataType::Utf8, false),
			Field::new("updated_at", DataType::Utf8, false),
			Field::new("importance", DataType::Float32, false),
			Field::new("confidence", DataType::Float32, false),
			Field::new("tags", DataType::Utf8, true),
			Field::new("related_files", DataType::Utf8, true),
			Field::new("git_commit", DataType::Utf8, true),
			Field::new(
				"embedding",
				DataType::FixedSizeList(
					Arc::new(Field::new("item", DataType::Float32, true)),
					self.vector_dim as i32,
				),
				true,
			),
		]));

		// Prepare data
		let tags_json = serde_json::to_string(&memory.metadata.tags)?;
		let files_json = serde_json::to_string(&memory.metadata.related_files)?;

		// Create embedding array
		let embedding_values = Float32Array::from(embedding);
		let embedding_array = FixedSizeListArray::new(
			Arc::new(Field::new("item", DataType::Float32, true)),
			self.vector_dim as i32,
			Arc::new(embedding_values),
			None,
		);

		let batch = RecordBatch::try_new(
			schema.clone(),
			vec![
				Arc::new(StringArray::from(vec![memory.id.clone()])),
				Arc::new(StringArray::from(vec![memory.memory_type.to_string()])),
				Arc::new(StringArray::from(vec![memory.title.clone()])),
				Arc::new(StringArray::from(vec![memory.content.clone()])),
				Arc::new(StringArray::from(vec![memory.created_at.to_rfc3339()])),
				Arc::new(StringArray::from(vec![memory.updated_at.to_rfc3339()])),
				Arc::new(Float32Array::from(vec![memory.metadata.importance])),
				Arc::new(Float32Array::from(vec![memory.metadata.confidence])),
				Arc::new(StringArray::from(vec![tags_json])),
				Arc::new(StringArray::from(vec![files_json])),
				Arc::new(StringArray::from(vec![memory.metadata.git_commit.clone()])),
				Arc::new(embedding_array),
			],
		)?;

		// Open table and add the batch
		let table = self.db.open_table("memories").execute().await?;

		// Delete existing memory with same ID if it exists
		table.delete(&format!("id = '{}'", memory.id)).await.ok();

		// Add new memory
		use std::iter::once;
		let batches = once(Ok(batch));
		let batch_reader = arrow::record_batch::RecordBatchIterator::new(batches, schema);
		table.add(batch_reader).execute().await?;

		// Index management moved to separate method for performance

		Ok(())
	}

	/// Store multiple memories in batch with optimized embedding generation
	pub async fn store_memories(&mut self, memories: &[Memory]) -> Result<()> {
		if memories.is_empty() {
			return Ok(());
		}

		// Collect all searchable texts for batch embedding generation
		let texts: Vec<String> = memories
			.iter()
			.map(|memory| memory.get_searchable_text())
			.collect();

		// Generate ALL embeddings in ONE API request using the same high-level function as indexer
		// This includes token-aware batching and respects config limits
		let embeddings = crate::embedding::generate_embeddings_batch(
			texts,
			false,
			&self.main_config,
			crate::embedding::types::InputType::Query,
		)
		.await?;

		if embeddings.len() != memories.len() {
			return Err(anyhow::anyhow!(
				"Embedding count mismatch: expected {}, got {}",
				memories.len(),
				embeddings.len()
			));
		}

		// Store all memories with their pre-computed embeddings
		for (memory, embedding) in memories.iter().zip(embeddings.into_iter()) {
			self.store_memory_with_embedding(memory, embedding).await?;
		}

		Ok(())
	}

	/// Update an existing memory
	pub async fn update_memory(&mut self, memory: &Memory) -> Result<()> {
		// Just use store_memory as it handles updates by deleting and re-inserting
		self.store_memory(memory).await
	}

	/// Delete a memory by ID
	pub async fn delete_memory(&mut self, memory_id: &str) -> Result<()> {
		let table = self.db.open_table("memories").execute().await?;
		table.delete(&format!("id = '{}'", memory_id)).await?;

		// Also delete any relationships involving this memory
		let rel_table = self.db.open_table("memory_relationships").execute().await?;
		rel_table
			.delete(&format!(
				"source_id = '{}' OR target_id = '{}'",
				memory_id, memory_id
			))
			.await
			.ok();

		Ok(())
	}

	/// Ensure optimal vector index for memories table (call periodically, not on every store)
	pub async fn ensure_optimal_index(&self) -> Result<()> {
		let table = self.db.open_table("memories").execute().await?;

		// Get current dataset statistics
		let row_count = table.count_rows(None).await?;
		let has_index = table
			.list_indices()
			.await?
			.iter()
			.any(|idx| idx.columns == vec!["embedding"]);

		if !has_index {
			// Use intelligent optimizer to determine optimal index parameters
			let index_params =
				crate::store::vector_optimizer::VectorOptimizer::calculate_index_params(
					row_count,
					self.vector_dim,
				);

			if index_params.should_create_index {
				tracing::info!(
					"Creating optimized vector index for memories table: {} rows, {} partitions, {} sub-vectors",
					row_count, index_params.num_partitions, index_params.num_sub_vectors
				);

				table
					.create_index(
						&["embedding"],
						Index::IvfPq(
							lancedb::index::vector::IvfPqIndexBuilder::default()
								.distance_type(index_params.distance_type)
								.num_partitions(index_params.num_partitions)
								.num_sub_vectors(index_params.num_sub_vectors)
								.num_bits(index_params.num_bits as u32),
						),
					)
					.execute()
					.await?;
			} else {
				tracing::debug!(
					"Skipping index creation for memories table with {} rows - brute force will be faster",
					row_count
				);
			}
		} else {
			// Check if we should optimize existing index due to growth
			if crate::store::vector_optimizer::VectorOptimizer::should_optimize_for_growth(
				row_count,
				self.vector_dim,
				true,
			) {
				tracing::info!("Dataset growth detected, optimizing memories index");

				// Recreate index with optimal parameters
				let index_params =
					crate::store::vector_optimizer::VectorOptimizer::calculate_index_params(
						row_count,
						self.vector_dim,
					);

				if index_params.should_create_index {
					table
						.create_index(
							&["embedding"],
							Index::IvfPq(
								lancedb::index::vector::IvfPqIndexBuilder::default()
									.distance_type(index_params.distance_type)
									.num_partitions(index_params.num_partitions)
									.num_sub_vectors(index_params.num_sub_vectors)
									.num_bits(index_params.num_bits as u32),
							),
						)
						.execute()
						.await?;
				}
			}
		}

		Ok(())
	}

	/// Get a memory by ID
	pub async fn get_memory(&self, memory_id: &str) -> Result<Option<Memory>> {
		let table = self.db.open_table("memories").execute().await?;

		let mut results = table
			.query()
			.only_if(format!("id = '{}'", memory_id))
			.limit(1)
			.execute()
			.await?;

		while let Some(batch) = results.try_next().await? {
			if batch.num_rows() > 0 {
				let memories = self.batch_to_memories(&batch)?;
				return Ok(memories.into_iter().next());
			}
		}

		Ok(None)
	}

	/// Search memories using vector similarity and optional filters
	pub async fn search_memories(&self, query: &MemoryQuery) -> Result<Vec<MemorySearchResult>> {
		let table = self.db.open_table("memories").execute().await?;

		let limit = query
			.limit
			.unwrap_or(self.config.max_search_results)
			.min(self.config.max_search_results);
		let min_relevance = query.min_relevance.unwrap_or(0.0);

		let mut results = Vec::new();

		// If we have a text query, use semantic search
		if let Some(ref query_text) = query.query_text {
			let query_embedding = self
				.embedding_provider
				.generate_embedding(query_text)
				.await?;

			// Start with optimized vector search
			let mut db_query = table
				.vector_search(query_embedding.as_slice())?
				.distance_type(DistanceType::Cosine)
				.limit(limit * 2); // Get more results to filter

			// Apply intelligent search optimization
			db_query = crate::store::vector_optimizer::VectorOptimizer::optimize_query(
				db_query, &table, "memories",
			)
			.await
			.map_err(|e| anyhow::anyhow!("Failed to optimize query: {}", e))?;

			let mut db_results = db_query.execute().await?;

			while let Some(batch) = db_results.try_next().await? {
				if batch.num_rows() == 0 {
					continue;
				}

				// Extract distance column
				let distance_array = batch
					.column_by_name("_distance")
					.and_then(|col| col.as_any().downcast_ref::<Float32Array>())
					.map(|arr| (0..arr.len()).map(|i| arr.value(i)).collect::<Vec<f32>>())
					.unwrap_or_default();

				let memories = self.batch_to_memories(&batch)?;

				for (memory, distance) in memories.into_iter().zip(distance_array.into_iter()) {
					// Apply filters
					if !self.matches_filters(&memory, query) {
						continue;
					}

					// Convert distance to similarity (cosine distance is 1 - similarity)
					let similarity = 1.0 - distance;
					if similarity >= min_relevance {
						results.push(MemorySearchResult {
							memory,
							relevance_score: similarity,
							selection_reason: self.generate_selection_reason(query, similarity),
						});
					}
				}
			}
		} else {
			// No text query, just apply filters
			let mut db_results = table.query().execute().await?;

			while let Some(batch) = db_results.try_next().await? {
				if batch.num_rows() == 0 {
					continue;
				}

				let memories = self.batch_to_memories(&batch)?;

				for memory in memories {
					if self.matches_filters(&memory, query) {
						let relevance_score = memory.metadata.importance;
						if relevance_score >= min_relevance {
							results.push(MemorySearchResult {
								memory,
								relevance_score,
								selection_reason: self
									.generate_selection_reason(query, relevance_score),
							});
						}
					}
				}
			}
		}

		// Apply sorting based on query parameters
		if let Some(sort_by) = &query.sort_by {
			let sort_order = query
				.sort_order
				.as_ref()
				.unwrap_or(&super::types::SortOrder::Descending);

			results.sort_by(|a, b| {
				let ordering = match sort_by {
					super::types::MemorySortBy::CreatedAt => {
						a.memory.created_at.cmp(&b.memory.created_at)
					}
					super::types::MemorySortBy::UpdatedAt => {
						a.memory.updated_at.cmp(&b.memory.updated_at)
					}
					super::types::MemorySortBy::Importance => a
						.memory
						.metadata
						.importance
						.partial_cmp(&b.memory.metadata.importance)
						.unwrap_or(std::cmp::Ordering::Equal),
					super::types::MemorySortBy::Confidence => a
						.memory
						.metadata
						.confidence
						.partial_cmp(&b.memory.metadata.confidence)
						.unwrap_or(std::cmp::Ordering::Equal),
					super::types::MemorySortBy::Relevance => a
						.relevance_score
						.partial_cmp(&b.relevance_score)
						.unwrap_or(std::cmp::Ordering::Equal),
				};

				match sort_order {
					super::types::SortOrder::Ascending => ordering,
					super::types::SortOrder::Descending => ordering.reverse(),
				}
			});
		} else {
			// Default: Sort by relevance score (highest first)
			results.sort_by(|a, b| {
				b.relevance_score
					.partial_cmp(&a.relevance_score)
					.unwrap_or(std::cmp::Ordering::Equal)
			});
		}

		// Apply final limit
		results.truncate(limit);

		Ok(results)
	}

	/// Get all memories (paginated)
	pub async fn get_all_memories(&self, offset: usize, limit: usize) -> Result<Vec<Memory>> {
		let table = self.db.open_table("memories").execute().await?;

		let mut results = table
			.query()
			.limit(offset + limit) // LanceDB doesn't have native offset, so we limit and skip
			.execute()
			.await?;

		let mut all_memories = Vec::new();

		while let Some(batch) = results.try_next().await? {
			if batch.num_rows() == 0 {
				continue;
			}

			let mut batch_memories = self.batch_to_memories(&batch)?;
			all_memories.append(&mut batch_memories);
		}

		// Sort by creation date (most recent first)
		all_memories.sort_by(|a, b| b.created_at.cmp(&a.created_at));

		// Apply pagination manually
		let start = offset.min(all_memories.len());
		let end = (offset + limit).min(all_memories.len());

		Ok(all_memories[start..end].to_vec())
	}

	/// Store a memory relationship
	pub async fn store_relationship(&mut self, relationship: &MemoryRelationship) -> Result<()> {
		let table = self.db.open_table("memory_relationships").execute().await?;

		let schema = Arc::new(Schema::new(vec![
			Field::new("id", DataType::Utf8, false),
			Field::new("source_id", DataType::Utf8, false),
			Field::new("target_id", DataType::Utf8, false),
			Field::new("relationship_type", DataType::Utf8, false),
			Field::new("strength", DataType::Float32, false),
			Field::new("description", DataType::Utf8, false),
			Field::new("created_at", DataType::Utf8, false),
		]));

		let batch = RecordBatch::try_new(
			schema.clone(),
			vec![
				Arc::new(StringArray::from(vec![relationship.id.clone()])),
				Arc::new(StringArray::from(vec![relationship.source_id.clone()])),
				Arc::new(StringArray::from(vec![relationship.target_id.clone()])),
				Arc::new(StringArray::from(vec![relationship
					.relationship_type
					.to_string()])),
				Arc::new(Float32Array::from(vec![relationship.strength])),
				Arc::new(StringArray::from(vec![relationship.description.clone()])),
				Arc::new(StringArray::from(vec![relationship
					.created_at
					.to_rfc3339()])),
			],
		)?;

		// Delete existing relationship with same ID if it exists
		table
			.delete(&format!("id = '{}'", relationship.id))
			.await
			.ok();

		// Add new relationship
		use std::iter::once;
		let batches = once(Ok(batch));
		let batch_reader = arrow::record_batch::RecordBatchIterator::new(batches, schema);
		table.add(batch_reader).execute().await?;

		Ok(())
	}

	/// Get relationships for a memory
	pub async fn get_memory_relationships(
		&self,
		memory_id: &str,
	) -> Result<Vec<MemoryRelationship>> {
		let table = self.db.open_table("memory_relationships").execute().await?;

		let mut results = table
			.query()
			.only_if(format!(
				"source_id = '{}' OR target_id = '{}'",
				memory_id, memory_id
			))
			.execute()
			.await?;

		let mut relationships = Vec::new();

		while let Some(batch) = results.try_next().await? {
			if batch.num_rows() == 0 {
				continue;
			}

			let mut batch_relationships = self.batch_to_relationships(&batch)?;
			relationships.append(&mut batch_relationships);
		}

		Ok(relationships)
	}

	/// Get total count of memories
	pub async fn get_memory_count(&self) -> Result<usize> {
		let table = self.db.open_table("memories").execute().await?;
		Ok(table.count_rows(None).await?)
	}

	/// Clean up old memories based on configuration
	pub async fn cleanup_old_memories(&mut self) -> Result<usize> {
		if let Some(cleanup_days) = self.config.auto_cleanup_days {
			let cutoff_date = Utc::now() - chrono::Duration::days(cleanup_days as i64);
			let cutoff_str = cutoff_date.to_rfc3339();

			let table = self.db.open_table("memories").execute().await?;

			// Count memories to be deleted
			let mut count_results = table
				.query()
				.only_if(format!(
					"created_at < '{}' AND importance < {}",
					cutoff_str, self.config.cleanup_min_importance
				))
				.execute()
				.await?;

			let mut count = 0;
			while let Some(batch) = count_results.try_next().await? {
				count += batch.num_rows();
			}

			// Delete old memories
			table
				.delete(&format!(
					"created_at < '{}' AND importance < {}",
					cutoff_str, self.config.cleanup_min_importance
				))
				.await?;

			Ok(count)
		} else {
			Ok(0)
		}
	}

	/// Convert RecordBatch to Vec<Memory>
	fn batch_to_memories(&self, batch: &RecordBatch) -> Result<Vec<Memory>> {
		use chrono::DateTime;

		let num_rows = batch.num_rows();
		let mut memories = Vec::with_capacity(num_rows);

		// Extract all columns
		let id_array = batch
			.column_by_name("id")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("id column not found or wrong type"))?;

		let memory_type_array = batch
			.column_by_name("memory_type")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("memory_type column not found or wrong type"))?;

		let title_array = batch
			.column_by_name("title")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("title column not found or wrong type"))?;

		let content_array = batch
			.column_by_name("content")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("content column not found or wrong type"))?;

		let created_at_array = batch
			.column_by_name("created_at")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("created_at column not found or wrong type"))?;

		let updated_at_array = batch
			.column_by_name("updated_at")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("updated_at column not found or wrong type"))?;

		let importance_array = batch
			.column_by_name("importance")
			.and_then(|col| col.as_any().downcast_ref::<Float32Array>())
			.ok_or_else(|| anyhow::anyhow!("importance column not found or wrong type"))?;

		let confidence_array = batch
			.column_by_name("confidence")
			.and_then(|col| col.as_any().downcast_ref::<Float32Array>())
			.ok_or_else(|| anyhow::anyhow!("confidence column not found or wrong type"))?;

		let tags_array = batch
			.column_by_name("tags")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("tags column not found or wrong type"))?;

		let files_array = batch
			.column_by_name("related_files")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("related_files column not found or wrong type"))?;

		let git_array = batch
			.column_by_name("git_commit")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("git_commit column not found or wrong type"))?;

		for i in 0..num_rows {
			let memory_type =
				super::types::MemoryType::from(memory_type_array.value(i).to_string());

			let tags: Vec<String> = if tags_array.is_null(i) {
				Vec::new()
			} else {
				serde_json::from_str(tags_array.value(i)).unwrap_or_default()
			};

			let related_files: Vec<String> = if files_array.is_null(i) {
				Vec::new()
			} else {
				serde_json::from_str(files_array.value(i)).unwrap_or_default()
			};

			let git_commit = if git_array.is_null(i) {
				None
			} else {
				Some(git_array.value(i).to_string())
			};

			let metadata = super::types::MemoryMetadata {
				git_commit,
				importance: importance_array.value(i),
				confidence: confidence_array.value(i),
				tags,
				related_files,
				..Default::default()
			};

			let memory = Memory {
				id: id_array.value(i).to_string(),
				memory_type,
				title: title_array.value(i).to_string(),
				content: content_array.value(i).to_string(),
				created_at: DateTime::parse_from_rfc3339(created_at_array.value(i))?
					.with_timezone(&Utc),
				updated_at: DateTime::parse_from_rfc3339(updated_at_array.value(i))?
					.with_timezone(&Utc),
				metadata,
				relevance_score: None,
			};

			memories.push(memory);
		}

		Ok(memories)
	}

	/// Convert RecordBatch to Vec<MemoryRelationship>
	fn batch_to_relationships(&self, batch: &RecordBatch) -> Result<Vec<MemoryRelationship>> {
		use chrono::DateTime;

		let num_rows = batch.num_rows();
		let mut relationships = Vec::with_capacity(num_rows);

		// Extract all columns
		let id_array = batch
			.column_by_name("id")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("id column not found or wrong type"))?;

		let source_array = batch
			.column_by_name("source_id")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("source_id column not found or wrong type"))?;

		let target_array = batch
			.column_by_name("target_id")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("target_id column not found or wrong type"))?;

		let type_array = batch
			.column_by_name("relationship_type")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("relationship_type column not found or wrong type"))?;

		let strength_array = batch
			.column_by_name("strength")
			.and_then(|col| col.as_any().downcast_ref::<Float32Array>())
			.ok_or_else(|| anyhow::anyhow!("strength column not found or wrong type"))?;

		let desc_array = batch
			.column_by_name("description")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("description column not found or wrong type"))?;

		let created_array = batch
			.column_by_name("created_at")
			.and_then(|col| col.as_any().downcast_ref::<StringArray>())
			.ok_or_else(|| anyhow::anyhow!("created_at column not found or wrong type"))?;

		for i in 0..num_rows {
			let relationship_type = match type_array.value(i) {
				"RelatedTo" => super::types::RelationshipType::RelatedTo,
				"DependsOn" => super::types::RelationshipType::DependsOn,
				"Supersedes" => super::types::RelationshipType::Supersedes,
				"Similar" => super::types::RelationshipType::Similar,
				"Conflicts" => super::types::RelationshipType::Conflicts,
				"Implements" => super::types::RelationshipType::Implements,
				"Extends" => super::types::RelationshipType::Extends,
				other => super::types::RelationshipType::Custom(other.to_string()),
			};

			let relationship = MemoryRelationship {
				id: id_array.value(i).to_string(),
				source_id: source_array.value(i).to_string(),
				target_id: target_array.value(i).to_string(),
				relationship_type,
				strength: strength_array.value(i),
				description: desc_array.value(i).to_string(),
				created_at: DateTime::parse_from_rfc3339(created_array.value(i))?
					.with_timezone(&Utc),
			};

			relationships.push(relationship);
		}

		Ok(relationships)
	}

	/// Check if memory matches the query filters
	fn matches_filters(&self, memory: &Memory, query: &MemoryQuery) -> bool {
		// Filter by memory types
		if let Some(ref memory_types) = query.memory_types {
			if !memory_types.contains(&memory.memory_type) {
				return false;
			}
		}

		// Filter by tags (any of these tags)
		if let Some(ref tags) = query.tags {
			if !tags.iter().any(|tag| memory.metadata.tags.contains(tag)) {
				return false;
			}
		}

		// Filter by related files
		if let Some(ref files) = query.related_files {
			if !files
				.iter()
				.any(|file| memory.metadata.related_files.contains(file))
			{
				return false;
			}
		}

		// Filter by git commit
		if let Some(ref git_commit) = query.git_commit {
			if memory.metadata.git_commit.as_ref() != Some(git_commit) {
				return false;
			}
		}

		// Filter by minimum importance
		if let Some(min_importance) = query.min_importance {
			if memory.metadata.importance < min_importance {
				return false;
			}
		}

		// Filter by minimum confidence
		if let Some(min_confidence) = query.min_confidence {
			if memory.metadata.confidence < min_confidence {
				return false;
			}
		}

		// Filter by creation date range
		if let Some(created_after) = query.created_after {
			if memory.created_at < created_after {
				return false;
			}
		}

		if let Some(created_before) = query.created_before {
			if memory.created_at > created_before {
				return false;
			}
		}

		true
	}

	/// Clear all memory data (memories and relationships)
	pub async fn clear_all_memory_data(&mut self) -> Result<usize> {
		// Get current counts before deletion
		let memory_count = self.get_memory_count().await.unwrap_or(0);

		// Count relationships
		let rel_table = self.db.open_table("memory_relationships").execute().await?;
		let relationship_count = rel_table.count_rows(None).await.unwrap_or(0);

		let total_deleted = memory_count + relationship_count;

		// Drop and recreate memories table
		if self
			.db
			.table_names()
			.execute()
			.await?
			.contains(&"memories".to_string())
		{
			self.db.drop_table("memories").await?;
		}

		// Drop and recreate relationships table
		if self
			.db
			.table_names()
			.execute()
			.await?
			.contains(&"memory_relationships".to_string())
		{
			self.db.drop_table("memory_relationships").await?;
		}

		// Recreate tables
		self.initialize_tables().await?;

		Ok(total_deleted)
	}

	/// Generate selection reason for search results
	fn generate_selection_reason(&self, query: &MemoryQuery, relevance_score: f32) -> String {
		let mut reasons = Vec::new();

		if query.query_text.is_some() {
			reasons.push(format!("Semantic similarity: {:.2}", relevance_score));
		}

		if query.memory_types.is_some() {
			reasons.push("Matches memory type filter".to_string());
		}

		if query.tags.is_some() {
			reasons.push("Contains matching tags".to_string());
		}

		if query.related_files.is_some() {
			reasons.push("Related to specified files".to_string());
		}

		if query.git_commit.is_some() {
			reasons.push("Matches Git commit filter".to_string());
		}

		if reasons.is_empty() {
			"Matches search criteria".to_string()
		} else {
			reasons.join(", ")
		}
	}
}