post-cortex-storage 0.3.0

Storage backends for post-cortex — lock-free RocksDB (default) and optional SurrealDB. Implements the Storage trait against the post-cortex domain types.
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
// Copyright (c) 2025, 2026 Julius ML
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! Storage trait abstractions for post-cortex.
//!
//! This module defines the core storage traits that allow different backends
//! (RocksDB, SurrealDB, etc.) to be used interchangeably.

use post_cortex_core::core::context_update::{ContextUpdate, EntityData, EntityRelationship, RelationType};
use post_cortex_embeddings::{SearchMatch, VectorMetadata};
use post_cortex_proto::pb::{
    CascadeInvalidateReport, FreshnessEntry, SourceReference, SymbolId,
};
use post_cortex_core::graph::entity_graph::EntityNetwork;
use post_cortex_core::session::active_session::ActiveSession;
use crate::rocksdb_storage::{SessionCheckpoint, StoredWorkspace};
use post_cortex_core::workspace::SessionRole;
use anyhow::Result;
use async_trait::async_trait;
use uuid::Uuid;

/// Report returned when checking if sources are still fresh
#[derive(Debug, Clone)]
pub struct FreshnessReportExt {
    /// Freshness entries, one per checked source.
    pub entries: Vec<FreshnessEntry>,
}

/// A source_reference entry that is marked stale (status=1) — i.e. it was
/// invalidated by `invalidate_source` or `cascade_invalidate` but has not yet
/// been re-registered.  Returned by `get_stale_entries_by_source`.
#[derive(Debug, Clone)]
pub struct StaleEntryInfo {
    /// Entry ID that was marked stale.
    pub entry_id: String,
    /// Symbol name associated with the entry (when known).
    pub symbol_name: Option<String>,
    /// Symbol type (e.g. `"fn"`, `"struct"`) associated with the entry.
    pub symbol_type: Option<String>,
}
///
/// This trait defines the fundamental storage operations required for
/// session management, context updates, and workspace persistence.
///
/// Implementations must be Send + Sync for concurrent access.
#[async_trait]
pub trait Storage: FreshnessStorage + Send + Sync {
    // ===== Session Operations =====

    /// Save a session to storage
    async fn save_session(&self, session: &ActiveSession) -> Result<()>;

    /// Load a session from storage
    async fn load_session(&self, session_id: Uuid) -> Result<ActiveSession>;

    /// Delete a session and all related data
    async fn delete_session(&self, session_id: Uuid) -> Result<()>;

    /// Delete all entities and relationships for a session (used by entity graph rebuild)
    async fn clear_session_entities(&self, session_id: Uuid) -> Result<()>;

    /// List all session IDs
    async fn list_sessions(&self) -> Result<Vec<Uuid>>;

    /// Check if a session exists
    async fn session_exists(&self, session_id: Uuid) -> Result<bool>;

    // ===== Context Update Operations =====

    /// Batch save multiple context updates efficiently
    async fn batch_save_updates(&self, session_id: Uuid, updates: Vec<ContextUpdate>)
    -> Result<()>;

    /// Save session and context updates in a single atomic write.
    /// Default implementation calls save_session then batch_save_updates sequentially.
    /// Backends can override for a single WriteBatch operation.
    async fn save_session_with_updates(
        &self,
        session: &ActiveSession,
        session_id: Uuid,
        updates: Vec<ContextUpdate>,
    ) -> Result<()> {
        self.save_session(session).await?;
        if !updates.is_empty() {
            self.batch_save_updates(session_id, updates).await?;
        }
        Ok(())
    }

    /// Load all updates for a session
    async fn load_session_updates(&self, session_id: Uuid) -> Result<Vec<ContextUpdate>>;

    // ===== Checkpoint Operations =====

    /// Save a session checkpoint
    async fn save_checkpoint(&self, checkpoint: &SessionCheckpoint) -> Result<()>;

    /// Load a session checkpoint
    async fn load_checkpoint(&self, checkpoint_id: Uuid) -> Result<SessionCheckpoint>;

    /// List all checkpoints
    async fn list_checkpoints(&self) -> Result<Vec<SessionCheckpoint>>;

    // ===== Workspace Operations =====

    /// Save workspace metadata
    async fn save_workspace_metadata(
        &self,
        workspace_id: Uuid,
        name: &str,
        description: &str,
        session_ids: &[Uuid],
    ) -> Result<()>;

    /// Delete a workspace
    async fn delete_workspace(&self, workspace_id: Uuid) -> Result<()>;

    /// List all workspaces
    async fn list_workspaces(&self) -> Result<Vec<StoredWorkspace>>;

    /// Add a session to a workspace
    async fn add_session_to_workspace(
        &self,
        workspace_id: Uuid,
        session_id: Uuid,
        role: SessionRole,
    ) -> Result<()>;

    /// Remove a session from a workspace
    async fn remove_session_from_workspace(
        &self,
        workspace_id: Uuid,
        session_id: Uuid,
    ) -> Result<()>;

    // ===== Utility Operations =====

    /// Force database compaction
    async fn compact(&self) -> Result<()>;

    /// Get estimated number of keys in database
    async fn get_key_count(&self) -> Result<usize>;

    /// Get database statistics as a string
    async fn get_stats(&self) -> Result<String>;
}

/// Extended storage trait for graph-native operations.
///
/// This trait is implemented by backends with native graph support (e.g., SurrealDB).
/// For backends without native graph support (e.g., RocksDB), graph operations
/// are handled by the in-memory SimpleEntityGraph.
#[async_trait]
pub trait GraphStorage: Storage {
    // ===== Entity Operations =====

    /// Insert or update an entity
    async fn upsert_entity(&self, session_id: Uuid, entity: &EntityData) -> Result<()>;

    /// Get an entity by name
    async fn get_entity(&self, session_id: Uuid, name: &str) -> Result<Option<EntityData>>;

    /// List all entities for a session
    async fn list_entities(&self, session_id: Uuid) -> Result<Vec<EntityData>>;

    /// Delete an entity
    async fn delete_entity(&self, session_id: Uuid, name: &str) -> Result<()>;

    // ===== Relationship Operations =====

    /// Create a relationship between entities
    async fn create_relationship(
        &self,
        session_id: Uuid,
        relationship: &EntityRelationship,
    ) -> Result<()>;

    /// Find all entities related to a given entity
    async fn find_related_entities(
        &self,
        session_id: Uuid,
        entity_name: &str,
    ) -> Result<Vec<String>>;

    /// Find entities related by a specific relation type
    async fn find_related_by_type(
        &self,
        session_id: Uuid,
        entity_name: &str,
        relation_type: &RelationType,
    ) -> Result<Vec<String>>;

    /// Find the shortest path between two entities
    async fn find_shortest_path(
        &self,
        session_id: Uuid,
        from: &str,
        to: &str,
    ) -> Result<Option<Vec<String>>>;

    /// Get the entity network (subgraph) around a center entity
    async fn get_entity_network(
        &self,
        session_id: Uuid,
        center: &str,
        max_depth: usize,
    ) -> Result<EntityNetwork>;
}

/// Vector storage trait for semantic search.
///
/// This trait can be implemented by:
/// - SurrealDB (native HNSW)
/// - VectorDB (in-memory)
/// - External services (Qdrant, etc.)
#[async_trait]
pub trait VectorStorage: Send + Sync {
    /// Add a vector with metadata
    async fn add_vector(&self, vector: Vec<f32>, metadata: VectorMetadata) -> Result<String>;

    /// Add multiple vectors in a batch
    async fn add_vectors_batch(
        &self,
        vectors: Vec<(Vec<f32>, VectorMetadata)>,
    ) -> Result<Vec<String>>;

    /// Search for similar vectors
    async fn search(&self, query: &[f32], k: usize) -> Result<Vec<SearchMatch>>;

    /// Search within a specific session
    async fn search_in_session(
        &self,
        query: &[f32],
        k: usize,
        session_id: &str,
    ) -> Result<Vec<SearchMatch>>;

    /// Search with a content type filter
    async fn search_by_content_type(
        &self,
        query: &[f32],
        k: usize,
        content_type: &str,
    ) -> Result<Vec<SearchMatch>>;

    /// Remove a vector by ID
    async fn remove_vector(&self, id: &str) -> Result<bool>;

    /// Check if a session has any embeddings
    async fn has_session_embeddings(&self, session_id: &str) -> bool;

    /// Count embeddings for a session
    async fn count_session_embeddings(&self, session_id: &str) -> usize;

    /// Get total vector count
    async fn total_count(&self) -> usize;

    /// Get all vectors for a session (for loading into memory)
    async fn get_session_vectors(
        &self,
        session_id: &str,
    ) -> Result<Vec<(Vec<f32>, VectorMetadata)>>;

    /// Get all vectors from storage (for loading into memory on startup)
    async fn get_all_vectors(&self) -> Result<Vec<(Vec<f32>, VectorMetadata)>>;
}

/// Storage trait for source freshness tracking.
///
/// This trait manages tracking where information in PCX originally came from,
/// allowing efficient invalidation when the source files change.
#[async_trait]
pub trait FreshnessStorage: Send + Sync {
    /// Register or update a source reference for an entry
    async fn register_source(&self, session_id: Uuid, reference: SourceReference) -> Result<()>;

    /// Check if an entry's stored hash matches the provided hash.
    /// When the stored entry has a FunctionScope with ast_hash, compares that
    /// instead of the file-level hash (semantic freshness).
    async fn check_freshness(&self, entry_id: &str, file_hash: &[u8]) -> Result<FreshnessEntry>;

    /// Semantic freshness check: compare ast_hash when available, fallback to file hash.
    /// The `ast_hash` and `symbol_name` are optional — if provided and the stored entry
    /// has a FunctionScope, ast_hash comparison is used instead of file-level hash.
    async fn check_freshness_semantic(
        &self,
        entry_id: &str,
        file_hash: &[u8],
        _ast_hash: Option<&[u8]>,
        _symbol_name: Option<&str>,
    ) -> Result<FreshnessEntry> {
        // Default: delegate to basic check_freshness (backends override for semantic)
        self.check_freshness(entry_id, file_hash).await
    }

    /// Invalidate (remove) a source reference, usually because the source changed
    /// or the entry is being deleted. Returns count of removed references.
    async fn invalidate_source(&self, file_path: &str) -> Result<u32>;

    /// Get all entries associated with a specific file path
    async fn get_entries_by_source(&self, file_path: &str) -> Result<Vec<SourceReference>>;

    /// Return entry_ids (plus symbol metadata) for all source_reference records that
    /// are still marked stale (status=1) for the given file.  Called by Axon after
    /// `register_source_batch` to detect symbols that were deleted from the file
    /// (they were marked stale by `invalidate_source` but never re-registered).
    async fn get_stale_entries_by_source(
        &self,
        file_path: &str,
    ) -> Result<Vec<StaleEntryInfo>>;

    /// Register symbol-level dependencies (e.g., fn foo depends on struct Bar).
    /// Used for cascade invalidation.
    async fn register_symbol_dependencies(
        &self,
        from: SymbolId,
        to_symbols: Vec<SymbolId>,
    ) -> Result<u32>;

    /// Cascade invalidate: when a symbol changes, invalidate all entries
    /// whose symbols depend on it (transitively up to max_depth).
    async fn cascade_invalidate(
        &self,
        changed: SymbolId,
        new_ast_hash: Vec<u8>,
        max_depth: u32,
    ) -> Result<CascadeInvalidateReport>;

    /// Batch freshness check: check multiple entries in a single storage round-trip.
    ///
    /// Each element of `entries` is `(entry_id, file_hash, ast_hash, symbol_name)`.
    /// Returns one `FreshnessEntry` per input element, in the same order.
    ///
    /// The default implementation loops over `check_freshness_semantic` calls.
    /// Backends with native batch support (e.g., SurrealDB) should override this
    /// with a single `WHERE entry_id IN $ids` query.
    async fn check_freshness_batch(
        &self,
        entries: Vec<(String, Vec<u8>, Option<Vec<u8>>, Option<String>)>,
    ) -> Result<Vec<FreshnessEntry>> {
        let mut results = Vec::with_capacity(entries.len());
        for (entry_id, file_hash, ast_hash, symbol_name) in entries {
            let result = self
                .check_freshness_semantic(
                    &entry_id,
                    &file_hash,
                    ast_hash.as_deref(),
                    symbol_name.as_deref(),
                )
                .await
                .unwrap_or_else(|_| FreshnessEntry {
                    entry_id: entry_id.clone(),
                    file_path: String::new(),
                    status: post_cortex_proto::pb::FreshnessStatus::Unknown as i32,
                    stored_hash: Vec::new(),
                    current_hash: file_hash,
                });
            results.push(result);
        }
        Ok(results)
    }
}

/// Unified storage backend enum for runtime selection.
///
/// This enum wraps the different storage implementations and provides
/// a unified interface for the rest of the application.
#[derive(Clone)]
pub enum StorageBackend {
    /// RocksDB-based local storage (default)
    RocksDB(crate::RealRocksDBStorage),

    /// SurrealDB-based storage with native graph support
    #[cfg(feature = "surrealdb-storage")]
    SurrealDB(std::sync::Arc<crate::surrealdb_storage::SurrealDBStorage>),
}

impl StorageBackend {
    /// Check if this backend supports native graph operations
    pub fn supports_native_graph(&self) -> bool {
        match self {
            StorageBackend::RocksDB(_) => false,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(_) => true,
        }
    }

    /// Check if this backend supports native vector operations
    pub fn supports_native_vectors(&self) -> bool {
        match self {
            StorageBackend::RocksDB(_) => false,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(_) => true,
        }
    }
}

// Implement Storage trait for StorageBackend via delegation
#[async_trait]
impl Storage for StorageBackend {
    async fn save_session(&self, session: &ActiveSession) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => storage.save_session(session).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.save_session(session).await,
        }
    }

    async fn load_session(&self, session_id: Uuid) -> Result<ActiveSession> {
        match self {
            StorageBackend::RocksDB(storage) => storage.load_session(session_id).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.load_session(session_id).await,
        }
    }

    async fn delete_session(&self, session_id: Uuid) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => storage.delete_session(session_id).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.delete_session(session_id).await,
        }
    }

    async fn clear_session_entities(&self, session_id: Uuid) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => storage.clear_session_entities(session_id).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.clear_session_entities(session_id).await,
        }
    }

    async fn list_sessions(&self) -> Result<Vec<Uuid>> {
        match self {
            StorageBackend::RocksDB(storage) => storage.list_sessions().await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.list_sessions().await,
        }
    }

    async fn session_exists(&self, session_id: Uuid) -> Result<bool> {
        match self {
            StorageBackend::RocksDB(storage) => storage.session_exists(session_id).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.session_exists(session_id).await,
        }
    }

    async fn batch_save_updates(
        &self,
        session_id: Uuid,
        updates: Vec<ContextUpdate>,
    ) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage.batch_save_updates(session_id, updates).await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage.batch_save_updates(session_id, updates).await
            }
        }
    }

    async fn save_session_with_updates(
        &self,
        session: &ActiveSession,
        session_id: Uuid,
        updates: Vec<ContextUpdate>,
    ) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage
                    .save_session_with_updates(session, session_id, updates)
                    .await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(_) => {
                // Use default sequential implementation for SurrealDB
                self.save_session(session).await?;
                if !updates.is_empty() {
                    self.batch_save_updates(session_id, updates).await?;
                }
                Ok(())
            }
        }
    }

    async fn load_session_updates(&self, session_id: Uuid) -> Result<Vec<ContextUpdate>> {
        match self {
            StorageBackend::RocksDB(storage) => storage.load_session_updates(session_id).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.load_session_updates(session_id).await,
        }
    }

    async fn save_checkpoint(&self, checkpoint: &SessionCheckpoint) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => storage.save_checkpoint(checkpoint).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.save_checkpoint(checkpoint).await,
        }
    }

    async fn load_checkpoint(&self, checkpoint_id: Uuid) -> Result<SessionCheckpoint> {
        match self {
            StorageBackend::RocksDB(storage) => storage.load_checkpoint(checkpoint_id).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.load_checkpoint(checkpoint_id).await,
        }
    }

    async fn list_checkpoints(&self) -> Result<Vec<SessionCheckpoint>> {
        match self {
            StorageBackend::RocksDB(storage) => storage.list_checkpoints().await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.list_checkpoints().await,
        }
    }

    async fn save_workspace_metadata(
        &self,
        workspace_id: Uuid,
        name: &str,
        description: &str,
        session_ids: &[Uuid],
    ) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage
                    .save_workspace_metadata(workspace_id, name, description, session_ids)
                    .await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage
                    .save_workspace_metadata(workspace_id, name, description, session_ids)
                    .await
            }
        }
    }

    async fn delete_workspace(&self, workspace_id: Uuid) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => storage.delete_workspace(workspace_id).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.delete_workspace(workspace_id).await,
        }
    }

    async fn list_workspaces(&self) -> Result<Vec<StoredWorkspace>> {
        match self {
            StorageBackend::RocksDB(storage) => storage.list_workspaces().await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.list_workspaces().await,
        }
    }

    async fn add_session_to_workspace(
        &self,
        workspace_id: Uuid,
        session_id: Uuid,
        role: SessionRole,
    ) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage
                    .add_session_to_workspace(workspace_id, session_id, role)
                    .await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage
                    .add_session_to_workspace(workspace_id, session_id, role)
                    .await
            }
        }
    }

    async fn remove_session_from_workspace(
        &self,
        workspace_id: Uuid,
        session_id: Uuid,
    ) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage
                    .remove_session_from_workspace(workspace_id, session_id)
                    .await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage
                    .remove_session_from_workspace(workspace_id, session_id)
                    .await
            }
        }
    }

    async fn compact(&self) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => storage.compact().await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.compact().await,
        }
    }

    async fn get_key_count(&self) -> Result<usize> {
        match self {
            StorageBackend::RocksDB(storage) => storage.get_key_count().await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.get_key_count().await,
        }
    }

    async fn get_stats(&self) -> Result<String> {
        match self {
            StorageBackend::RocksDB(storage) => storage.get_stats().await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.get_stats().await,
        }
    }
}

// Implement GraphStorage trait for StorageBackend via delegation
#[async_trait]
impl GraphStorage for StorageBackend {
    async fn upsert_entity(&self, session_id: Uuid, entity: &EntityData) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => storage.upsert_entity(session_id, entity).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.upsert_entity(session_id, entity).await,
        }
    }

    async fn get_entity(&self, session_id: Uuid, name: &str) -> Result<Option<EntityData>> {
        match self {
            StorageBackend::RocksDB(storage) => storage.get_entity(session_id, name).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.get_entity(session_id, name).await,
        }
    }

    async fn list_entities(&self, session_id: Uuid) -> Result<Vec<EntityData>> {
        match self {
            StorageBackend::RocksDB(storage) => storage.list_entities(session_id).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.list_entities(session_id).await,
        }
    }

    async fn delete_entity(&self, session_id: Uuid, name: &str) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => storage.delete_entity(session_id, name).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.delete_entity(session_id, name).await,
        }
    }

    async fn create_relationship(
        &self,
        session_id: Uuid,
        relationship: &EntityRelationship,
    ) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage.create_relationship(session_id, relationship).await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage.create_relationship(session_id, relationship).await
            }
        }
    }

    async fn find_related_entities(
        &self,
        session_id: Uuid,
        entity_name: &str,
    ) -> Result<Vec<String>> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage.find_related_entities(session_id, entity_name).await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage.find_related_entities(session_id, entity_name).await
            }
        }
    }

    async fn find_related_by_type(
        &self,
        session_id: Uuid,
        entity_name: &str,
        relation_type: &RelationType,
    ) -> Result<Vec<String>> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage
                    .find_related_by_type(session_id, entity_name, relation_type)
                    .await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage
                    .find_related_by_type(session_id, entity_name, relation_type)
                    .await
            }
        }
    }

    async fn find_shortest_path(
        &self,
        session_id: Uuid,
        from: &str,
        to: &str,
    ) -> Result<Option<Vec<String>>> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage.find_shortest_path(session_id, from, to).await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage.find_shortest_path(session_id, from, to).await
            }
        }
    }

    async fn get_entity_network(
        &self,
        session_id: Uuid,
        center: &str,
        max_depth: usize,
    ) -> Result<EntityNetwork> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage
                    .get_entity_network(session_id, center, max_depth)
                    .await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage
                    .get_entity_network(session_id, center, max_depth)
                    .await
            }
        }
    }
}

// Implement FreshnessStorage trait for StorageBackend via delegation
#[async_trait]
impl FreshnessStorage for StorageBackend {
    async fn register_source(&self, session_id: Uuid, reference: SourceReference) -> Result<()> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage.register_source(session_id, reference).await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage.register_source(session_id, reference).await
            }
        }
    }

    async fn check_freshness(&self, entry_id: &str, file_hash: &[u8]) -> Result<FreshnessEntry> {
        match self {
            StorageBackend::RocksDB(storage) => storage.check_freshness(entry_id, file_hash).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage.check_freshness(entry_id, file_hash).await
            }
        }
    }

    async fn invalidate_source(&self, file_path: &str) -> Result<u32> {
        match self {
            StorageBackend::RocksDB(storage) => storage.invalidate_source(file_path).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.invalidate_source(file_path).await,
        }
    }

    async fn get_entries_by_source(&self, file_path: &str) -> Result<Vec<SourceReference>> {
        match self {
            StorageBackend::RocksDB(storage) => storage.get_entries_by_source(file_path).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.get_entries_by_source(file_path).await,
        }
    }

    async fn get_stale_entries_by_source(
        &self,
        file_path: &str,
    ) -> Result<Vec<StaleEntryInfo>> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage.get_stale_entries_by_source(file_path).await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage.get_stale_entries_by_source(file_path).await
            }
        }
    }

    async fn check_freshness_semantic(
        &self,
        entry_id: &str,
        file_hash: &[u8],
        ast_hash: Option<&[u8]>,
        symbol_name: Option<&str>,
    ) -> Result<FreshnessEntry> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage
                    .check_freshness_semantic(entry_id, file_hash, ast_hash, symbol_name)
                    .await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage
                    .check_freshness_semantic(entry_id, file_hash, ast_hash, symbol_name)
                    .await
            }
        }
    }

    async fn register_symbol_dependencies(
        &self,
        from: SymbolId,
        to_symbols: Vec<SymbolId>,
    ) -> Result<u32> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage.register_symbol_dependencies(from, to_symbols).await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage.register_symbol_dependencies(from, to_symbols).await
            }
        }
    }

    async fn cascade_invalidate(
        &self,
        changed: SymbolId,
        new_ast_hash: Vec<u8>,
        max_depth: u32,
    ) -> Result<CascadeInvalidateReport> {
        match self {
            StorageBackend::RocksDB(storage) => {
                storage
                    .cascade_invalidate(changed, new_ast_hash, max_depth)
                    .await
            }
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => {
                storage
                    .cascade_invalidate(changed, new_ast_hash, max_depth)
                    .await
            }
        }
    }

    async fn check_freshness_batch(
        &self,
        entries: Vec<(String, Vec<u8>, Option<Vec<u8>>, Option<String>)>,
    ) -> Result<Vec<FreshnessEntry>> {
        match self {
            StorageBackend::RocksDB(storage) => storage.check_freshness_batch(entries).await,
            #[cfg(feature = "surrealdb-storage")]
            StorageBackend::SurrealDB(storage) => storage.check_freshness_batch(entries).await,
        }
    }
}

/// Storage configuration for backend selection
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct StorageConfig {
    /// Storage backend type
    pub backend: StorageBackendType,

    /// Path to storage data directory
    pub path: std::path::PathBuf,

    /// SurrealDB-specific configuration
    #[cfg(feature = "surrealdb-storage")]
    pub surrealdb: Option<SurrealDBConfig>,
}

/// Storage backend type
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(Default)]
pub enum StorageBackendType {
    /// RocksDB local storage (default)
    #[default]
    RocksDB,

    /// SurrealDB storage with native graph support
    #[cfg(feature = "surrealdb-storage")]
    SurrealDB,
}


impl StorageBackendType {
    /// Parse storage backend type from string
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "rocksdb" | "rocks" => Some(StorageBackendType::RocksDB),
            #[cfg(feature = "surrealdb-storage")]
            "surrealdb" | "surreal" => Some(StorageBackendType::SurrealDB),
            _ => None,
        }
    }
}

/// SurrealDB-specific configuration
#[cfg(feature = "surrealdb-storage")]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SurrealDBConfig {
    /// SurrealDB namespace
    pub namespace: String,

    /// SurrealDB database name
    pub database: String,

    /// TiKV endpoints (for distributed mode)
    pub tikv_endpoints: Option<Vec<String>>,
}

#[cfg(feature = "surrealdb-storage")]
impl Default for SurrealDBConfig {
    fn default() -> Self {
        Self {
            namespace: "post_cortex".to_string(),
            database: "main".to_string(),
            tikv_endpoints: None,
        }
    }
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            backend: StorageBackendType::default(),
            path: dirs::data_local_dir()
                .unwrap_or_else(|| std::path::PathBuf::from("."))
                .join("post-cortex")
                .join("data"),
            #[cfg(feature = "surrealdb-storage")]
            surrealdb: None,
        }
    }
}