post-cortex-storage 0.4.0

Storage backends for post-cortex — lock-free RocksDB (default) and optional SurrealDB. Implements the Storage trait against the post-cortex domain types.
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
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
// 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.

//! [`Storage`] trait implementation for [`SurrealDBStorage`].
//!
//! Covers session lifecycle, context-update batches, checkpoints, workspaces,
//! and stats — every method that the in-memory layer reaches for during
//! ordinary read/write traffic.
//!
//! The `context_update` table is the source of truth for a session's
//! updates; the `session` row holds metadata only. Writes send only what
//! differs from [`PersistedState`], i.e. from what this process last read
//! or wrote for the session, in one multi-statement query with bound
//! parameters.

use anyhow::Result;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use surrealdb::types::{RecordId, SurrealValue};
use tracing::{debug, warn};
use uuid::Uuid;

use crate::error::session_not_found;
use crate::rocksdb_storage::{SessionCheckpoint, StoredWorkspace};
use crate::traits::{GraphStorage, SessionSummary, Storage};
use post_cortex_core::core::context_update::{ContextUpdate, EntityData, EntityRelationship};
use post_cortex_core::graph::entity_graph::SimpleEntityGraph;
use post_cortex_core::session::active_session::{ActiveSession, UserPreferences};
use post_cortex_core::workspace::SessionRole;

use super::records::{
    CheckpointRecord, ContextUpdateRecord, EntityRecord, SessionRecord, WorkspaceRecord,
    WorkspaceSessionRecord, deserialize_surreal_option,
};
use super::{SurrealDBStorage, per_relation_table};

/// Sessions tracked in [`PersistedStates`]; past it one entry is dropped per
/// new session, which only costs that session one full write later.
const MAX_TRACKED_SESSIONS: usize = 4096;

/// What this process knows to be stored for one session.
#[derive(Default)]
pub(crate) struct PersistedState {
    /// Updates known to have rows (as of the last load/save). Updates not
    /// in here get their row written; rows are only deleted when a save
    /// names them as removed.
    update_ids: HashSet<Uuid>,
    /// Fingerprint per entity name as last written or read.
    entities: HashMap<String, u64>,
    /// Fingerprint per relation record key as last written or read.
    relations: HashMap<String, u64>,
}

/// Per-session [`PersistedState`]; the mutex also serialises saves of a
/// session.
pub(crate) type PersistedStates = DashMap<Uuid, Arc<tokio::sync::Mutex<Option<PersistedState>>>>;

fn entity_fingerprint(entity: &EntityData) -> u64 {
    let mut h = DefaultHasher::new();
    entity.name.hash(&mut h);
    entity.entity_type.hash(&mut h);
    entity.first_mentioned.hash(&mut h);
    entity.last_mentioned.hash(&mut h);
    entity.mention_count.hash(&mut h);
    entity.importance_score.to_bits().hash(&mut h);
    entity.description.hash(&mut h);
    h.finish()
}

fn relation_fingerprint(rel: &EntityRelationship) -> u64 {
    let mut h = DefaultHasher::new();
    rel.from_entity.hash(&mut h);
    rel.to_entity.hash(&mut h);
    rel.relation_type.hash(&mut h);
    rel.context.hash(&mut h);
    h.finish()
}

/// Entity row for the batched upsert.
#[derive(Serialize, SurrealValue)]
struct KeyedEntity {
    key: String,
    record: EntityRecord,
}

/// Relation row for the batched upsert.
#[derive(Serialize, SurrealValue)]
struct RelationRow {
    table: String,
    key: String,
    from_key: String,
    to_key: String,
    context: String,
}

/// Workspace membership row for the batched upsert.
#[derive(Serialize, SurrealValue)]
struct KeyedMembership {
    key: String,
    record: WorkspaceSessionRecord,
}

/// Session listing row.
#[derive(Deserialize, SurrealValue)]
struct SessionSummaryRow {
    session_id: String,
    #[serde(default, deserialize_with = "deserialize_surreal_option")]
    name: Option<String>,
    #[serde(default, deserialize_with = "deserialize_surreal_option")]
    description: Option<String>,
    created_at: String,
    last_updated: String,
}

#[derive(Deserialize, SurrealValue)]
struct UpdateCountRow {
    session_id: String,
    n: i64,
}

fn update_record(session_id: Uuid, update: &ContextUpdate) -> Result<ContextUpdateRecord> {
    Ok(ContextUpdateRecord {
        update_id: update.id.to_string(),
        session_id: session_id.to_string(),
        timestamp: update.timestamp.to_rfc3339(),
        update_type: format!("{:?}", update.update_type),
        update_data: serde_json::to_value(update)?,
    })
}

fn parse_rfc3339(s: &str) -> DateTime<Utc> {
    DateTime::parse_from_rfc3339(s)
        .map(|dt| dt.with_timezone(&Utc))
        .unwrap_or_else(|_| Utc::now())
}

/// Statement indices of the persist query.
const STMT_SESSION: usize = 0;
const STMT_UPDATES: usize = 1;
const STMT_REMOVED: usize = 2;
const STMT_ENTITIES: usize = 3;
const STMT_RELATIONS: usize = 4;

const PERSIST_QUERY: &str = "\
    UPSERT $session_rid CONTENT $session_row; \
    FOR $u IN $updates { UPSERT type::record('context_update', $u.update_id) CONTENT $u; }; \
    FOR $id IN $removed { DELETE type::record('context_update', $id); }; \
    FOR $e IN $entities { UPSERT type::record('entity', $e.key) CONTENT $e.record; }; \
    FOR $r IN $relations { \
        UPSERT type::record($r.table, $r.key) SET \
            in = type::record('entity', $r.from_key), \
            out = type::record('entity', $r.to_key), \
            context = $r.context, session_id = $session_id; \
    };";

impl SurrealDBStorage {
    fn persisted_slot(&self, session_id: Uuid) -> Arc<tokio::sync::Mutex<Option<PersistedState>>> {
        if self.persisted.len() >= MAX_TRACKED_SESSIONS && !self.persisted.contains_key(&session_id)
        {
            // Drop one other session's knowledge, not everyone's.
            let victim = self.persisted.iter().map(|e| *e.key()).next();
            if let Some(victim) = victim {
                self.persisted.remove(&victim);
            }
        }
        self.persisted.entry(session_id).or_default().clone()
    }

    /// Record that an entity was written outside a session save.
    pub(super) async fn note_entity_written(&self, session_id: Uuid, entity: &EntityData) {
        if let Some(slot) = self.persisted.get(&session_id).map(|s| s.clone())
            && let Some(state) = slot.lock().await.as_mut()
        {
            state
                .entities
                .insert(entity.name.clone(), entity_fingerprint(entity));
        }
    }

    /// Record that a relationship was written outside a session save.
    pub(super) async fn note_relationship_written(
        &self,
        session_id: Uuid,
        rel: &EntityRelationship,
    ) {
        if let Some(slot) = self.persisted.get(&session_id).map(|s| s.clone())
            && let Some(state) = slot.lock().await.as_mut()
        {
            let key = Self::relation_id(
                session_id,
                &rel.from_entity,
                &rel.to_entity,
                &rel.relation_type,
            );
            state.relations.insert(key, relation_fingerprint(rel));
        }
    }

    /// Forget what is known about a session's stored graph, so the next
    /// save writes all of it.
    pub(super) async fn forget_graph_state(&self, session_id: Uuid) {
        if let Some(slot) = self.persisted.get(&session_id).map(|s| s.clone())
            && let Some(state) = slot.lock().await.as_mut()
        {
            state.entities.clear();
            state.relations.clear();
        }
    }

    /// Ids of the session's stored update rows.
    async fn stored_update_ids(&self, session_id: Uuid) -> Result<HashSet<Uuid>> {
        let mut response = self
            .db
            .query("SELECT VALUE update_id FROM context_update WHERE session_id = $session_id")
            .bind(("session_id", session_id.to_string()))
            .await?;
        let ids: Vec<String> = response.take(0)?;
        Ok(ids.iter().filter_map(|s| Uuid::parse_str(s).ok()).collect())
    }

    /// Write `session` (metadata row), the `explicit` updates, session
    /// updates without a row, and changed entities/edges (all of them when
    /// `full_graph`); delete the rows of the `removed` updates. Rows are
    /// never deleted because a snapshot lacks them: it may predate them.
    async fn persist(
        &self,
        session: &ActiveSession,
        explicit: Vec<ContextUpdate>,
        removed_ids: Vec<Uuid>,
        full_graph: bool,
    ) -> Result<()> {
        let session_id = session.id();
        let slot = self.persisted_slot(session_id);
        let mut guard = slot.lock().await;

        // Borrowed, not taken: a failed save keeps what was known before.
        let fresh;
        let (known_ids, known_entities, known_relations) = match guard.as_ref() {
            Some(state) => (&state.update_ids, &state.entities, &state.relations),
            None => {
                fresh = PersistedState {
                    update_ids: self.stored_update_ids(session_id).await?,
                    ..Default::default()
                };
                (&fresh.update_ids, &fresh.entities, &fresh.relations)
            }
        };

        // Updates.
        let removed_set: HashSet<Uuid> = removed_ids.iter().copied().collect();
        let explicit_ids: HashSet<Uuid> = explicit.iter().map(|u| u.id).collect();
        let mut updates = Vec::with_capacity(explicit.len());
        let mut written_ids = Vec::with_capacity(explicit.len());
        for update in explicit.iter().filter(|u| !removed_set.contains(&u.id)) {
            updates.push(update_record(session_id, update)?);
            written_ids.push(update.id);
        }
        for update in session.incremental_updates.iter() {
            if !known_ids.contains(&update.id)
                && !explicit_ids.contains(&update.id)
                && !removed_set.contains(&update.id)
            {
                updates.push(update_record(session_id, update)?);
                written_ids.push(update.id);
            }
        }
        let removed: Vec<String> = removed_ids.iter().map(Uuid::to_string).collect();

        // Graph.
        let graph = &session.entity_graph;
        let mut entity_fps = HashMap::with_capacity(graph.entities.len());
        let mut entities = Vec::new();
        for (name, entity) in &graph.entities {
            let fp = entity_fingerprint(entity);
            if full_graph || known_entities.get(name) != Some(&fp) {
                entities.push(KeyedEntity {
                    key: Self::entity_id(session_id, name),
                    record: Self::entity_record(session_id, entity),
                });
            }
            entity_fps.insert(name.clone(), fp);
        }
        let mut relation_fps = HashMap::new();
        let mut relations = Vec::new();
        for rel in graph.get_all_relationships() {
            let key = Self::relation_id(
                session_id,
                &rel.from_entity,
                &rel.to_entity,
                &rel.relation_type,
            );
            let fp = relation_fingerprint(&rel);
            if full_graph || known_relations.get(&key) != Some(&fp) {
                relations.push(RelationRow {
                    table: Self::relation_table_name(&rel.relation_type).to_string(),
                    key: key.clone(),
                    from_key: Self::entity_id(session_id, &rel.from_entity),
                    to_key: Self::entity_id(session_id, &rel.to_entity),
                    context: rel.context,
                });
            }
            relation_fps.insert(key, fp);
        }

        let record = SessionRecord {
            session_id: session_id.to_string(),
            name: session.name(),
            description: session.description(),
            created_at: session.created_at().to_rfc3339(),
            last_updated: Utc::now().to_rfc3339(),
            user_preferences: serde_json::to_value(session.user_preferences())?,
            vectorized_update_ids: session
                .vectorized_update_ids
                .iter()
                .map(|id| id.to_string())
                .collect(),
            total_updates: session.incremental_updates.len() as u32,
        };

        let (n_updates, n_removed, n_entities, n_relations) = (
            updates.len(),
            removed.len(),
            entities.len(),
            relations.len(),
        );
        let mut response = self
            .db
            .query(PERSIST_QUERY)
            .bind((
                "session_rid",
                RecordId::new("session", session_id.to_string()),
            ))
            .bind(("session_row", record))
            .bind(("updates", updates))
            .bind(("removed", removed))
            .bind(("entities", entities))
            .bind(("relations", relations))
            .bind(("session_id", session_id.to_string()))
            .await?;

        let errors = response.take_errors();
        for (index, error) in &errors {
            warn!(
                "SurrealDBStorage: save of session {} - statement {} failed: {}",
                session_id, index, error
            );
        }
        if [STMT_SESSION, STMT_UPDATES, STMT_REMOVED]
            .iter()
            .any(|i| errors.contains_key(i))
        {
            // Rows of this save may be missing: forget the session's state
            // so the next save re-reads which rows exist.
            *guard = None;
            return Err(anyhow::anyhow!(
                "Failed to save session {}: {} statement(s) failed",
                session_id,
                errors.len()
            ));
        }
        let graph_ok =
            !errors.contains_key(&STMT_ENTITIES) && !errors.contains_key(&STMT_RELATIONS);
        let mut update_ids = known_ids.clone();
        update_ids.extend(written_ids);
        for id in &removed_set {
            update_ids.remove(id);
        }
        *guard = Some(PersistedState {
            update_ids,
            entities: if graph_ok { entity_fps } else { HashMap::new() },
            relations: if graph_ok {
                relation_fps
            } else {
                HashMap::new()
            },
        });

        debug!(
            "SurrealDBStorage: Saved session {} - {} update rows written, {} removed, {} entities, {} edges",
            session_id, n_updates, n_removed, n_entities, n_relations
        );
        Ok(())
    }

    /// Remember what `load_session` returned as the stored state.
    async fn remember_loaded(&self, session: &ActiveSession) {
        let graph: &SimpleEntityGraph = &session.entity_graph;
        let session_id = session.id();
        let state = PersistedState {
            update_ids: session.incremental_updates.iter().map(|u| u.id).collect(),
            entities: graph
                .entities
                .iter()
                .map(|(name, e)| (name.clone(), entity_fingerprint(e)))
                .collect(),
            relations: graph
                .get_all_relationships()
                .iter()
                .map(|r| {
                    (
                        Self::relation_id(
                            session_id,
                            &r.from_entity,
                            &r.to_entity,
                            &r.relation_type,
                        ),
                        relation_fingerprint(r),
                    )
                })
                .collect(),
        };
        *self.persisted_slot(session_id).lock().await = Some(state);
    }
}

#[async_trait]
impl Storage for SurrealDBStorage {
    /// Full save: metadata, every update without a row, and the whole graph.
    async fn save_session(&self, session: &ActiveSession) -> Result<()> {
        self.persist(session, Vec::new(), Vec::new(), true).await
    }

    /// Incremental save: metadata, `updates`, any other update without a
    /// row, and only the entities/edges that changed since the last
    /// load/save in this process.
    async fn save_session_with_updates(
        &self,
        session: &ActiveSession,
        session_id: Uuid,
        updates: Vec<ContextUpdate>,
    ) -> Result<()> {
        if session.id() != session_id {
            return Err(anyhow::anyhow!(
                "Session id mismatch: blob {} vs updates for {}",
                session.id(),
                session_id
            ));
        }
        self.persist(session, updates, Vec::new(), false).await
    }

    /// Incremental save that also deletes the `removed` updates' rows.
    async fn save_session_with_changes(
        &self,
        session: &ActiveSession,
        session_id: Uuid,
        updates: Vec<ContextUpdate>,
        removed: Vec<Uuid>,
    ) -> Result<()> {
        if session.id() != session_id {
            return Err(anyhow::anyhow!(
                "Session id mismatch: blob {} vs updates for {}",
                session.id(),
                session_id
            ));
        }
        self.persist(session, updates, removed, false).await
    }

    async fn load_session(&self, session_id: Uuid) -> Result<ActiveSession> {
        debug!(
            "SurrealDBStorage: Loading session with ID: {} (normalized)",
            session_id
        );

        // Load session metadata
        let record = self.select_session_record(session_id).await?;

        let r = record.ok_or_else(|| session_not_found(session_id))?;

        // Load ALL context updates from normalized table (source of truth!)
        let all_updates = self.load_session_updates(session_id).await?;

        // Hot context = last 50 updates (same as HotContext::MAX_SIZE)
        let hot_context: Vec<ContextUpdate> =
            all_updates.iter().rev().take(50).rev().cloned().collect();

        // Load entities from native graph table. The graph is derived data:
        // a failure here degrades the session instead of hiding it.
        let entities = self.list_entities(session_id).await.unwrap_or_else(|e| {
            warn!(
                "SurrealDBStorage: Loading entities of session {} failed: {:#}",
                session_id, e
            );
            Vec::new()
        });
        let entity_count = entities.len();
        let mut entity_graph = SimpleEntityGraph::new();
        // As stored: re-adding them as mentions would reset mention_count,
        // importance_score and first_mentioned.
        for entity in entities {
            entity_graph.insert_entity(entity);
        }

        // Load relationships from native graph and add to entity_graph
        let relationships = self
            .load_all_relationships(session_id)
            .await
            .unwrap_or_else(|e| {
                warn!(
                    "SurrealDBStorage: Loading relationships of session {} failed: {:#}",
                    session_id, e
                );
                Vec::new()
            });
        for rel in relationships {
            entity_graph.add_relationship(rel);
        }
        debug!(
            "SurrealDBStorage: Loaded {} entities, graph rebuilt",
            entity_count
        );

        // Rebuild StructuredContext from updates
        let current_state = Self::rebuild_structured_context(&all_updates);

        // Extract code_references and change_history from updates
        let code_references = Self::extract_code_references(&all_updates);
        let change_history = Self::extract_change_history(&all_updates);

        // Parse user preferences
        let user_preferences: UserPreferences = serde_json::from_value(r.user_preferences)
            .unwrap_or_else(|_| UserPreferences {
                auto_save_enabled: true,
                context_retention_days: 30,
                max_hot_context_size: 50,
                auto_summary_threshold: 100,
                important_keywords: Vec::new(),
            });

        // Parse vectorized_update_ids
        let vectorized_ids: Vec<Uuid> = r
            .vectorized_update_ids
            .iter()
            .filter_map(|s| Uuid::parse_str(s).ok())
            .collect();

        // Reconstruct ActiveSession from normalized data
        let session = ActiveSession::from_components(
            session_id,
            r.name,
            r.description,
            parse_rfc3339(&r.created_at),
            parse_rfc3339(&r.last_updated),
            user_preferences,
            hot_context,
            Vec::new(), // warm_context - compressed from older updates if needed
            Vec::new(), // cold_context - summaries generated on demand
            current_state,
            all_updates, // All updates from normalized table
            code_references,
            change_history,
            entity_graph,
            vectorized_ids,
        );
        self.remember_loaded(&session).await;

        debug!(
            "SurrealDBStorage: Session loaded (normalized) - {} updates, {} entities",
            session.incremental_updates.len(),
            entity_count
        );
        Ok(session)
    }

    async fn delete_session(&self, session_id: Uuid) -> Result<()> {
        debug!("SurrealDBStorage: Deleting session with ID: {}", session_id);

        // Raw DELETE: a typed delete deserializes the removed row, and rows
        // with NULL name/description (see select_session_record) cannot be.
        let query = format!(
            "DELETE type::record('session', $id); \
             DELETE context_update WHERE session_id = $session_id; \
             DELETE entity WHERE session_id = $session_id; \
             DELETE embedding WHERE session_id = $session_id; \
             DELETE workspace_session WHERE session_id = $session_id; {}",
            per_relation_table("DELETE {table} WHERE session_id = $session_id;")
        );
        self.db
            .query(query)
            .bind(("id", session_id.to_string()))
            .bind(("session_id", session_id.to_string()))
            .await?
            .check()?;
        self.persisted.remove(&session_id);

        debug!("SurrealDBStorage: Session deleted successfully");
        Ok(())
    }

    async fn clear_session_entities(&self, session_id: Uuid) -> Result<()> {
        let query = format!(
            "DELETE entity WHERE session_id = $session_id; {}",
            per_relation_table("DELETE {table} WHERE session_id = $session_id;")
        );
        self.db
            .query(query)
            .bind(("session_id", session_id.to_string()))
            .await?
            .check()?;
        self.forget_graph_state(session_id).await;

        debug!(
            "SurrealDBStorage: Cleared entities and relationships for session {}",
            session_id
        );
        Ok(())
    }

    async fn list_sessions(&self) -> Result<Vec<Uuid>> {
        debug!("SurrealDBStorage: Listing sessions");

        // Raw query + `response.take(0)` routes through serde (honours
        // `deserialize_surreal_option` on the Option<String> fields).
        // `db.select(table)` goes through SurrealValue conversion which
        // rejects NONE for Option<String> and crashed any list with
        // sessions that had `name = null` in storage.
        let mut response = self.db.query("SELECT session_id FROM session").await?;
        let ids: Vec<String> = response.take("session_id")?;

        let sessions: Vec<Uuid> = ids
            .into_iter()
            .filter_map(|s| Uuid::parse_str(&s).ok())
            .collect();

        debug!("SurrealDBStorage: Found {} sessions", sessions.len());

        Ok(sessions)
    }

    /// Session rows plus per-session update counts, in one query.
    async fn list_session_summaries(&self) -> Result<Vec<SessionSummary>> {
        let mut response = self
            .db
            .query(
                "SELECT session_id, (name ?? NONE) AS name, \
                 (description ?? NONE) AS description, created_at, last_updated \
                 FROM session; \
                 SELECT session_id, count() AS n FROM context_update GROUP BY session_id;",
            )
            .await?;
        let rows: Vec<SessionSummaryRow> = response.take(0)?;
        let counts: Vec<UpdateCountRow> = response.take(1)?;
        let counts: HashMap<String, i64> =
            counts.into_iter().map(|c| (c.session_id, c.n)).collect();

        Ok(rows
            .into_iter()
            .filter_map(|row| {
                let id = Uuid::parse_str(&row.session_id).ok()?;
                Some(SessionSummary {
                    id,
                    update_count: counts.get(&row.session_id).copied().unwrap_or(0).max(0) as usize,
                    name: row.name,
                    description: row.description,
                    created_at: parse_rfc3339(&row.created_at),
                    last_updated: parse_rfc3339(&row.last_updated),
                })
            })
            .collect())
    }

    async fn session_exists(&self, session_id: Uuid) -> Result<bool> {
        Ok(self.select_session_record(session_id).await?.is_some())
    }

    async fn batch_save_updates(
        &self,
        session_id: Uuid,
        updates: Vec<ContextUpdate>,
    ) -> Result<()> {
        if updates.is_empty() {
            return Ok(());
        }

        let records = updates
            .iter()
            .map(|u| update_record(session_id, u))
            .collect::<Result<Vec<_>>>()?;
        let count = records.len();
        self.db
            .query(
                "FOR $u IN $updates { \
                 UPSERT type::record('context_update', $u.update_id) CONTENT $u; };",
            )
            .bind(("updates", records))
            .await?
            .check()?;

        debug!(
            "SurrealDBStorage: Batch saved {} updates for session {} in 1 query",
            count, session_id
        );
        Ok(())
    }

    async fn load_session_updates(&self, session_id: Uuid) -> Result<Vec<ContextUpdate>> {
        debug!(
            "SurrealDBStorage: Loading updates for session {}",
            session_id
        );

        let mut updates = Vec::new();
        let mut start = 0;
        let limit = 1000;

        loop {
            // `update_id` breaks timestamp ties so pages are stable.
            let mut response = self
                .db
                .query(
                    "SELECT * FROM context_update WHERE session_id = $session_id \
                     ORDER BY timestamp ASC, update_id ASC LIMIT $limit START $start",
                )
                .bind(("session_id", session_id.to_string()))
                .bind(("limit", limit))
                .bind(("start", start))
                .await?;

            let records: Vec<ContextUpdateRecord> = response.take(0)?;
            let count = records.len();

            // Parse update_data JSON back to ContextUpdate
            for r in records {
                match serde_json::from_value(r.update_data) {
                    Ok(update) => updates.push(update),
                    Err(e) => warn!(
                        "SurrealDBStorage: Skipping undecodable update {} of session {}: {}",
                        r.update_id, session_id, e
                    ),
                }
            }

            if count < limit {
                break;
            }

            start += count;
        }

        debug!("SurrealDBStorage: Loaded {} updates", updates.len());

        Ok(updates)
    }

    async fn save_checkpoint(&self, checkpoint: &SessionCheckpoint) -> Result<()> {
        debug!(
            "SurrealDBStorage: Saving checkpoint with ID: {}",
            checkpoint.id
        );

        let record = CheckpointRecord {
            checkpoint_id: checkpoint.id.to_string(),
            session_id: checkpoint.session_id.to_string(),
            created_at: checkpoint.created_at.to_rfc3339(),
            structured_context: serde_json::to_value(&checkpoint.structured_context)?,
            recent_updates: serde_json::to_value(&checkpoint.recent_updates)?,
            code_references: serde_json::to_value(&checkpoint.code_references)?,
            change_history: serde_json::to_value(&checkpoint.change_history)?,
            total_updates: checkpoint.total_updates as u32,
            context_quality_score: checkpoint.context_quality_score,
            compression_ratio: checkpoint.compression_ratio,
        };

        let _: Option<CheckpointRecord> = self
            .db
            .upsert(("checkpoint", checkpoint.id.to_string()))
            .content(record)
            .await?;

        debug!("SurrealDBStorage: Checkpoint saved successfully");

        Ok(())
    }

    async fn load_checkpoint(&self, checkpoint_id: Uuid) -> Result<SessionCheckpoint> {
        debug!(
            "SurrealDBStorage: Loading checkpoint with ID: {}",
            checkpoint_id
        );

        let record: Option<CheckpointRecord> = self
            .db
            .select(("checkpoint", checkpoint_id.to_string()))
            .await?;

        match record {
            Some(r) => {
                let checkpoint = SessionCheckpoint {
                    id: Uuid::parse_str(&r.checkpoint_id)?,
                    session_id: Uuid::parse_str(&r.session_id)?,
                    created_at: parse_rfc3339(&r.created_at),
                    structured_context: serde_json::from_value(r.structured_context)?,
                    recent_updates: serde_json::from_value(r.recent_updates)?,
                    code_references: serde_json::from_value(r.code_references)?,
                    change_history: serde_json::from_value(r.change_history)?,
                    total_updates: r.total_updates as usize,
                    context_quality_score: r.context_quality_score,
                    compression_ratio: r.compression_ratio,
                };
                Ok(checkpoint)
            }
            None => Err(anyhow::anyhow!("Checkpoint not found")),
        }
    }

    async fn list_checkpoints(&self) -> Result<Vec<SessionCheckpoint>> {
        debug!("SurrealDBStorage: Listing checkpoints");

        let records: Vec<CheckpointRecord> = self.select_all("checkpoint").await?;

        let checkpoints: Vec<SessionCheckpoint> = records
            .into_iter()
            .filter_map(|r| {
                Some(SessionCheckpoint {
                    id: Uuid::parse_str(&r.checkpoint_id).ok()?,
                    session_id: Uuid::parse_str(&r.session_id).ok()?,
                    created_at: parse_rfc3339(&r.created_at),
                    structured_context: serde_json::from_value(r.structured_context).ok()?,
                    recent_updates: serde_json::from_value(r.recent_updates).ok()?,
                    code_references: serde_json::from_value(r.code_references).ok()?,
                    change_history: serde_json::from_value(r.change_history).ok()?,
                    total_updates: r.total_updates as usize,
                    context_quality_score: r.context_quality_score,
                    compression_ratio: r.compression_ratio,
                })
            })
            .collect();

        debug!("SurrealDBStorage: Listed {} checkpoints", checkpoints.len());

        Ok(checkpoints)
    }

    /// Workspace row and all memberships in one query.
    async fn save_workspace_metadata(
        &self,
        workspace_id: Uuid,
        name: &str,
        description: &str,
        session_ids: &[Uuid],
    ) -> Result<()> {
        debug!(
            "SurrealDBStorage: Saving workspace {} ({})",
            name, workspace_id
        );

        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let record = WorkspaceRecord {
            workspace_id: workspace_id.to_string(),
            name: name.to_string(),
            description: description.to_string(),
            created_at: now,
        };
        let members: Vec<KeyedMembership> = session_ids
            .iter()
            .map(|session_id| KeyedMembership {
                key: format!("{}_{}", workspace_id, session_id),
                record: WorkspaceSessionRecord {
                    workspace_id: workspace_id.to_string(),
                    session_id: session_id.to_string(),
                    role: format!("{:?}", SessionRole::Primary),
                    added_at: now,
                },
            })
            .collect();

        self.db
            .query(
                "UPSERT type::record('workspace', $workspace_id) CONTENT $record; \
                 FOR $m IN $members { \
                 UPSERT type::record('workspace_session', $m.key) CONTENT $m.record; };",
            )
            .bind(("workspace_id", workspace_id.to_string()))
            .bind(("record", record))
            .bind(("members", members))
            .await?
            .check()?;

        debug!("SurrealDBStorage: Workspace saved successfully");

        Ok(())
    }

    async fn delete_workspace(&self, workspace_id: Uuid) -> Result<()> {
        debug!("SurrealDBStorage: Deleting workspace {}", workspace_id);

        self.db
            .query(
                "DELETE type::record('workspace', $workspace_id); \
                 DELETE workspace_session WHERE workspace_id = $workspace_id;",
            )
            .bind(("workspace_id", workspace_id.to_string()))
            .await?
            .check()?;

        debug!("SurrealDBStorage: Workspace deleted successfully");

        Ok(())
    }

    /// Workspaces and memberships in one query.
    async fn list_workspaces(&self) -> Result<Vec<StoredWorkspace>> {
        debug!("SurrealDBStorage: Listing workspaces");

        let mut response = self
            .db
            .query("SELECT * FROM workspace; SELECT * FROM workspace_session;")
            .await?;
        let records: Vec<WorkspaceRecord> = response.take(0)?;
        let memberships: Vec<WorkspaceSessionRecord> = response.take(1)?;

        let mut sessions_by_workspace: HashMap<String, Vec<(Uuid, SessionRole)>> = HashMap::new();
        for m in memberships {
            let Ok(session_id) = Uuid::parse_str(&m.session_id) else {
                continue;
            };
            let role = match m.role.as_str() {
                "Primary" => SessionRole::Primary,
                "Related" => SessionRole::Related,
                "Dependency" => SessionRole::Dependency,
                "Shared" => SessionRole::Shared,
                _ => SessionRole::Primary,
            };
            sessions_by_workspace
                .entry(m.workspace_id)
                .or_default()
                .push((session_id, role));
        }

        let workspaces: Vec<StoredWorkspace> = records
            .into_iter()
            .filter_map(|record| {
                let id = Uuid::parse_str(&record.workspace_id).ok()?;
                Some(StoredWorkspace {
                    id,
                    sessions: sessions_by_workspace
                        .remove(&record.workspace_id)
                        .unwrap_or_default(),
                    name: record.name,
                    description: record.description,
                    created_at: record.created_at,
                })
            })
            .collect();

        debug!("SurrealDBStorage: Listed {} workspaces", workspaces.len());

        Ok(workspaces)
    }

    async fn add_session_to_workspace(
        &self,
        workspace_id: Uuid,
        session_id: Uuid,
        role: SessionRole,
    ) -> Result<()> {
        debug!(
            "SurrealDBStorage: Adding session {} to workspace {} with role {:?}",
            session_id, workspace_id, role
        );

        let added_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let record = WorkspaceSessionRecord {
            workspace_id: workspace_id.to_string(),
            session_id: session_id.to_string(),
            role: format!("{:?}", role),
            added_at,
        };

        let key = format!("{}_{}", workspace_id, session_id);
        let _: Option<WorkspaceSessionRecord> = self
            .db
            .upsert(("workspace_session", key))
            .content(record)
            .await?;

        Ok(())
    }

    async fn remove_session_from_workspace(
        &self,
        workspace_id: Uuid,
        session_id: Uuid,
    ) -> Result<()> {
        debug!(
            "SurrealDBStorage: Removing session {} from workspace {}",
            session_id, workspace_id
        );

        let key = format!("{}_{}", workspace_id, session_id);
        let _: Option<WorkspaceSessionRecord> = self.delete("workspace_session", &key).await?;

        Ok(())
    }

    async fn compact(&self) -> Result<()> {
        debug!("SurrealDBStorage: Compact requested (no-op for SurrealDB)");
        // SurrealDB handles compaction internally
        Ok(())
    }

    async fn get_key_count(&self) -> Result<usize> {
        let mut response = self
            .db
            .query(
                r#"
                RETURN (SELECT count() FROM session GROUP ALL).count +
                       (SELECT count() FROM context_update GROUP ALL).count +
                       (SELECT count() FROM entity GROUP ALL).count +
                       (SELECT count() FROM embedding GROUP ALL).count +
                       (SELECT count() FROM workspace GROUP ALL).count +
                       (SELECT count() FROM checkpoint GROUP ALL).count
            "#,
            )
            .await?;

        let count: Option<i64> = response.take(0)?;

        Ok(count.unwrap_or(0) as usize)
    }

    async fn get_stats(&self) -> Result<String> {
        let mut response = self
            .db
            .query(
                r#"
                RETURN {
                    sessions: (SELECT count() FROM session GROUP ALL).count,
                    updates: (SELECT count() FROM context_update GROUP ALL).count,
                    entities: (SELECT count() FROM entity GROUP ALL).count,
                    embeddings: (SELECT count() FROM embedding GROUP ALL).count,
                    workspaces: (SELECT count() FROM workspace GROUP ALL).count,
                    checkpoints: (SELECT count() FROM checkpoint GROUP ALL).count
                }
            "#,
            )
            .await?;

        let stats: Option<serde_json::Value> = response.take(0)?;

        Ok(serde_json::to_string_pretty(&stats.unwrap_or_default())?)
    }
}