aimdb-core 1.0.1

Type-safe async data pipelines — one Rust codebase from MCU to cloud
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
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
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
//! Connection handler for AimX protocol
//!
//! Handles individual client connections, including handshake, authentication,
//! and protocol method dispatch.
//!
//! # Architecture: Event Funnel Pattern
//!
//! Subscriptions use a funnel pattern for clean event delivery:
//! - Each subscription spawns a consumer task that reads from the record buffer
//! - Consumer tasks send events to a shared mpsc channel (the "funnel")
//! - A single writer task drains the funnel and writes events to the UnixStream
//! - This ensures NDJSON line integrity and prevents write interleaving

use crate::remote::{
    AimxConfig, Event, HelloMessage, RecordMetadata, Request, Response, WelcomeMessage,
};
use crate::{AimDb, DbError, DbResult};

#[cfg(feature = "std")]
use std::collections::HashMap;
#[cfg(feature = "std")]
use std::sync::Arc;

#[cfg(feature = "std")]
use serde_json::json;
#[cfg(feature = "std")]
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
#[cfg(feature = "std")]
use tokio::net::UnixStream;
#[cfg(feature = "std")]
use tokio::sync::mpsc;
#[cfg(feature = "std")]
use tokio::sync::oneshot;

/// Handle for an active subscription
///
/// Tracks the state needed to manage a single subscription's lifecycle.
#[cfg(feature = "std")]
#[allow(dead_code)] // record_name used only in tracing feature
struct SubscriptionHandle {
    /// Unique subscription identifier (returned to client)
    subscription_id: String,

    /// Record name being subscribed to
    record_name: String,

    /// Signal to cancel this subscription
    /// When sent, the consumer task will terminate
    cancel_tx: oneshot::Sender<()>,
}

/// Connection state for managing subscriptions
///
/// Tracks all active subscriptions for a single client connection.
#[cfg(feature = "std")]
struct ConnectionState {
    /// Active subscriptions by subscription_id
    subscriptions: HashMap<String, SubscriptionHandle>,

    /// Counter for generating unique subscription IDs
    next_subscription_id: u64,

    /// Event funnel: all subscription tasks send events here
    /// This channel feeds the single writer task
    event_tx: mpsc::UnboundedSender<Event>,

    /// Per-record drain readers, created lazily on first record.drain call.
    /// One drain reader per record, per connection.
    drain_readers: HashMap<String, Box<dyn crate::buffer::JsonBufferReader + Send>>,
}

#[cfg(feature = "std")]
impl ConnectionState {
    /// Creates a new connection state
    fn new(event_tx: mpsc::UnboundedSender<Event>) -> Self {
        Self {
            subscriptions: HashMap::new(),
            next_subscription_id: 1,
            event_tx,
            drain_readers: HashMap::new(),
        }
    }

    /// Generates a unique subscription ID for this connection
    fn generate_subscription_id(&mut self) -> String {
        let id = format!("sub-{}", self.next_subscription_id);
        self.next_subscription_id += 1;
        id
    }

    /// Adds a subscription to the connection state
    fn add_subscription(&mut self, handle: SubscriptionHandle) {
        self.subscriptions
            .insert(handle.subscription_id.clone(), handle);
    }

    /// Removes and returns a subscription by ID
    #[allow(dead_code)]
    fn remove_subscription(&mut self, subscription_id: &str) -> Option<SubscriptionHandle> {
        self.subscriptions.remove(subscription_id)
    }

    /// Cancels all active subscriptions
    ///
    /// Sends cancel signals to all subscription tasks and clears the map.
    /// Called when the client disconnects.
    async fn cancel_all_subscriptions(&mut self) {
        #[cfg(feature = "tracing")]
        tracing::info!(
            "Canceling {} active subscriptions",
            self.subscriptions.len()
        );

        for (_id, handle) in self.subscriptions.drain() {
            // Send cancel signal (ignore if receiver already dropped)
            let _ = handle.cancel_tx.send(());
        }
    }
}

/// Handles an incoming client connection
///
/// Processes the AimX protocol handshake and manages the client session.
/// Implements the event funnel pattern for subscription event delivery.
///
/// # Architecture
///
/// ```text
/// ┌─────────────────┐
/// │ Subscription 1  │───┐
/// │ Consumer Task   │   │
/// └─────────────────┘   │
///                       ├──► Event Funnel ───► select! loop ───► UnixStream
/// ┌─────────────────┐   │     (mpsc)          (interleaved    
/// │ Subscription 2  │───┘                      writes)
/// │ Consumer Task   │
/// └─────────────────┘
/// ```
///
/// The main loop uses `tokio::select!` to interleave:
/// - Reading requests from the stream
/// - Writing events from subscriptions
///
/// This ensures both responses and events are written without blocking.
///
/// # Arguments
/// * `db` - Database instance
/// * `config` - Remote access configuration
/// * `stream` - Unix domain socket stream
///
/// # Errors
/// Returns error if handshake fails or stream operations error
#[cfg(feature = "std")]
pub async fn handle_connection<R>(
    db: Arc<AimDb<R>>,
    config: AimxConfig,
    stream: UnixStream,
) -> DbResult<()>
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    #[cfg(feature = "tracing")]
    tracing::info!("New remote access connection established");

    // Perform protocol handshake
    let mut stream = match perform_handshake(stream, &config, &db).await {
        Ok(stream) => stream,
        Err(e) => {
            #[cfg(feature = "tracing")]
            tracing::warn!("Handshake failed: {}", e);
            return Err(e);
        }
    };

    #[cfg(feature = "tracing")]
    tracing::info!("Handshake complete, client ready");

    // Create event funnel: all subscription tasks will send events here
    let (event_tx, mut event_rx) = mpsc::unbounded_channel::<Event>();

    // Initialize connection state
    let mut conn_state = ConnectionState::new(event_tx);

    // Main loop: interleave reading requests and writing events
    loop {
        let mut line = String::new();

        tokio::select! {
            // Handle incoming requests
            read_result = stream.read_line(&mut line) => {
                match read_result {
                    Ok(0) => {
                        // Client closed connection
                        #[cfg(feature = "tracing")]
                        tracing::info!("Client disconnected gracefully");
                        break;
                    }
                    Ok(_) => {
                        #[cfg(feature = "tracing")]
                        tracing::debug!("Received request: {}", line.trim());

                        // Parse request
                        let request: Request = match serde_json::from_str(line.trim()) {
                            Ok(req) => req,
                            Err(e) => {
                                #[cfg(feature = "tracing")]
                                tracing::warn!("Failed to parse request: {}", e);

                                // Send error response (use ID 0 if we can't parse the request)
                                let error_response =
                                    Response::error(0, "parse_error", format!("Invalid JSON: {}", e));
                                if let Err(_e) = send_response(&mut stream, &error_response).await {
                                    #[cfg(feature = "tracing")]
                                    tracing::error!("Failed to send error response: {}", _e);
                                    break;
                                }
                                continue;
                            }
                        };

                        // Dispatch request to appropriate handler
                        let response = handle_request(&db, &config, &mut conn_state, request).await;

                        // Send response
                        if let Err(_e) = send_response(&mut stream, &response).await {
                            #[cfg(feature = "tracing")]
                            tracing::error!("Failed to send response: {}", _e);
                            break;
                        }
                    }
                    Err(_e) => {
                        #[cfg(feature = "tracing")]
                        tracing::error!("Error reading from stream: {}", _e);
                        break;
                    }
                }
            }

            // Handle outgoing events from subscriptions
            Some(event) = event_rx.recv() => {
                if let Err(_e) = send_event(&mut stream, &event).await {
                    #[cfg(feature = "tracing")]
                    tracing::error!("Failed to send event: {}", _e);
                    break;
                }
            }
        }
    }

    // Cleanup: cancel all active subscriptions
    conn_state.cancel_all_subscriptions().await;

    #[cfg(feature = "tracing")]
    tracing::info!("Connection handler terminating");

    Ok(())
}

/// Sends an event to the client
///
/// Serializes the event to JSON and writes it to the stream with a newline.
///
/// # Arguments
/// * `stream` - The connection stream
/// * `event` - The event to send
///
/// # Errors
/// Returns error if serialization or write fails
#[cfg(feature = "std")]
async fn send_event(stream: &mut BufReader<UnixStream>, event: &Event) -> DbResult<()> {
    // Wrap event in protocol envelope
    let event_msg = json!({ "event": event });

    let event_json = serde_json::to_string(&event_msg).map_err(|e| DbError::JsonWithContext {
        context: "Failed to serialize event".to_string(),
        source: e,
    })?;

    stream
        .get_mut()
        .write_all(event_json.as_bytes())
        .await
        .map_err(|e| DbError::IoWithContext {
            context: "Failed to write event".to_string(),
            source: e,
        })?;

    stream
        .get_mut()
        .write_all(b"\n")
        .await
        .map_err(|e| DbError::IoWithContext {
            context: "Failed to write event newline".to_string(),
            source: e,
        })?;

    #[cfg(feature = "tracing")]
    tracing::trace!("Sent event for subscription: {}", event.subscription_id);

    Ok(())
}

/// Sends a response to the client
///
/// Serializes the response to JSON and writes it to the stream with a newline.
///
/// # Arguments
/// * `stream` - The connection stream
/// * `response` - The response to send
///
/// # Errors
/// Returns error if serialization or write fails
#[cfg(feature = "std")]
async fn send_response(stream: &mut BufReader<UnixStream>, response: &Response) -> DbResult<()> {
    let response_json = serde_json::to_string(response).map_err(|e| DbError::JsonWithContext {
        context: "Failed to serialize response".to_string(),
        source: e,
    })?;

    stream
        .get_mut()
        .write_all(response_json.as_bytes())
        .await
        .map_err(|e| DbError::IoWithContext {
            context: "Failed to write response".to_string(),
            source: e,
        })?;

    stream
        .get_mut()
        .write_all(b"\n")
        .await
        .map_err(|e| DbError::IoWithContext {
            context: "Failed to write response newline".to_string(),
            source: e,
        })?;

    #[cfg(feature = "tracing")]
    tracing::debug!("Sent response");

    Ok(())
}

/// Performs the AimX protocol handshake
///
/// Handshake flow:
/// 1. Client sends HelloMessage with protocol version
/// 2. Server validates version compatibility
/// 3. Server sends WelcomeMessage with accepted version
/// 4. Optional: Authenticate with token
///
/// # Arguments
/// * `stream` - Unix domain socket stream
/// * `config` - Remote access configuration
/// * `db` - Database instance (for querying writable records)
///
/// # Returns
/// `BufReader<UnixStream>` if handshake succeeds
///
/// # Errors
/// Returns error if:
/// - Protocol version incompatible
/// - Authentication fails
/// - IO error during handshake
#[cfg(feature = "std")]
async fn perform_handshake<R>(
    stream: UnixStream,
    config: &AimxConfig,
    db: &Arc<AimDb<R>>,
) -> DbResult<BufReader<UnixStream>>
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    let (reader, mut writer) = stream.into_split();
    let mut reader = BufReader::new(reader);

    // Read Hello message from client
    let mut line = String::new();
    reader
        .read_line(&mut line)
        .await
        .map_err(|e| DbError::IoWithContext {
            context: "Failed to read Hello message".to_string(),
            source: e,
        })?;

    #[cfg(feature = "tracing")]
    tracing::debug!("Received handshake: {}", line.trim());

    // Parse Hello message
    let hello: HelloMessage =
        serde_json::from_str(line.trim()).map_err(|e| DbError::JsonWithContext {
            context: "Failed to parse Hello message".to_string(),
            source: e,
        })?;

    #[cfg(feature = "tracing")]
    tracing::debug!(
        "Client hello: version={}, client={}",
        hello.version,
        hello.client
    );

    // Version validation: accept "1.0" or "1"
    if hello.version != "1.0" && hello.version != "1" {
        let error_msg = format!(
            r#"{{"error":"unsupported_version","message":"Server supports version 1.0, client requested {}"}}"#,
            hello.version
        );

        #[cfg(feature = "tracing")]
        tracing::warn!("Unsupported version: {}", hello.version);

        let _ = writer.write_all(error_msg.as_bytes()).await;
        let _ = writer.write_all(b"\n").await;
        let _ = writer.shutdown().await;

        return Err(DbError::InvalidOperation {
            operation: "handshake".to_string(),
            reason: format!("Unsupported version: {}", hello.version),
        });
    }

    // Check authentication if required
    let authenticated = if let Some(expected_token) = &config.auth_token {
        match &hello.auth_token {
            Some(provided_token) if provided_token == expected_token => {
                #[cfg(feature = "tracing")]
                tracing::debug!("Authentication successful");
                true
            }
            Some(_) => {
                let error_msg =
                    r#"{"error":"authentication_failed","message":"Invalid auth token"}"#;

                #[cfg(feature = "tracing")]
                tracing::warn!("Authentication failed: invalid token");

                let _ = writer.write_all(error_msg.as_bytes()).await;
                let _ = writer.write_all(b"\n").await;
                let _ = writer.shutdown().await;

                return Err(DbError::PermissionDenied {
                    operation: "authentication".to_string(),
                });
            }
            None => {
                let error_msg =
                    r#"{"error":"authentication_required","message":"Auth token required"}"#;

                #[cfg(feature = "tracing")]
                tracing::warn!("Authentication failed: no token provided");

                let _ = writer.write_all(error_msg.as_bytes()).await;
                let _ = writer.write_all(b"\n").await;
                let _ = writer.shutdown().await;

                return Err(DbError::PermissionDenied {
                    operation: "authentication".to_string(),
                });
            }
        }
    } else {
        false
    };

    // Determine permissions based on security policy
    let permissions = match &config.security_policy {
        crate::remote::SecurityPolicy::ReadOnly => vec!["read".to_string()],
        crate::remote::SecurityPolicy::ReadWrite { .. } => {
            vec!["read".to_string(), "write".to_string()]
        }
    };

    // Get writable records by querying database for writable record names
    let writable_records = match &config.security_policy {
        crate::remote::SecurityPolicy::ReadOnly => vec![],
        crate::remote::SecurityPolicy::ReadWrite {
            writable_records: _writable_type_ids,
        } => {
            // Get all records from database
            let all_records: Vec<RecordMetadata> = db.list_records();

            // Filter to those that are marked writable
            all_records
                .into_iter()
                .filter(|meta| meta.writable)
                .map(|meta| meta.name)
                .collect()
        }
    };

    // Send Welcome message
    let welcome = WelcomeMessage {
        version: "1.0".to_string(),
        server: "aimdb".to_string(),
        permissions,
        writable_records,
        max_subscriptions: Some(config.subscription_queue_size),
        authenticated: Some(authenticated),
    };

    let welcome_json = serde_json::to_string(&welcome).map_err(|e| DbError::JsonWithContext {
        context: "Failed to serialize Welcome message".to_string(),
        source: e,
    })?;

    writer
        .write_all(welcome_json.as_bytes())
        .await
        .map_err(|e| DbError::IoWithContext {
            context: "Failed to write Welcome message".to_string(),
            source: e,
        })?;

    writer
        .write_all(b"\n")
        .await
        .map_err(|e| DbError::IoWithContext {
            context: "Failed to write Welcome newline".to_string(),
            source: e,
        })?;

    #[cfg(feature = "tracing")]
    tracing::info!("Sent Welcome message to client");

    // Reunite the stream
    let stream = reader
        .into_inner()
        .reunite(writer)
        .map_err(|e| DbError::Io {
            source: std::io::Error::other(e.to_string()),
        })?;

    Ok(BufReader::new(stream))
}

/// Handles a single request and returns a response
///
/// Dispatches to the appropriate handler based on the request method.
///
/// # Arguments
/// * `db` - Database instance
/// * `config` - Remote access configuration
/// * `conn_state` - Connection state (for subscription management)
/// * `request` - The parsed request
///
/// # Returns
/// Response to send to the client
#[cfg(feature = "std")]
async fn handle_request<R>(
    db: &Arc<AimDb<R>>,
    config: &AimxConfig,
    conn_state: &mut ConnectionState,
    request: Request,
) -> Response
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    #[cfg(feature = "tracing")]
    tracing::debug!(
        "Handling request: method={}, id={}",
        request.method,
        request.id
    );

    match request.method.as_str() {
        "record.list" => handle_record_list(db, config, request.id).await,
        "record.get" => handle_record_get(db, config, request.id, request.params).await,
        "record.set" => handle_record_set(db, config, request.id, request.params).await,
        "record.subscribe" => {
            handle_record_subscribe(db, config, conn_state, request.id, request.params).await
        }
        "record.unsubscribe" => {
            handle_record_unsubscribe(conn_state, request.id, request.params).await
        }
        "record.drain" => handle_record_drain(db, conn_state, request.id, request.params).await,
        "record.query" => handle_record_query(db, request.id, request.params).await,
        "graph.nodes" => handle_graph_nodes(db, request.id).await,
        "graph.edges" => handle_graph_edges(db, request.id).await,
        "graph.topo_order" => handle_graph_topo_order(db, request.id).await,
        _ => {
            #[cfg(feature = "tracing")]
            tracing::warn!("Unknown method: {}", request.method);

            Response::error(
                request.id,
                "method_not_found",
                format!("Unknown method: {}", request.method),
            )
        }
    }
}

/// Handles record.list method
///
/// Returns metadata for all registered records in the database.
///
/// # Arguments
/// * `db` - Database instance
/// * `config` - Remote access configuration (for permission checks)
/// * `request_id` - Request ID for the response
///
/// # Returns
/// Success response with array of RecordMetadata
#[cfg(feature = "std")]
async fn handle_record_list<R>(
    db: &Arc<AimDb<R>>,
    _config: &AimxConfig,
    request_id: u64,
) -> Response
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    #[cfg(feature = "tracing")]
    tracing::debug!("Listing records");

    // Get all record metadata from database
    let records: Vec<RecordMetadata> = db.list_records();

    #[cfg(feature = "tracing")]
    tracing::debug!("Found {} records", records.len());

    // Convert to JSON and return
    Response::success(request_id, json!(records))
}

/// Handles record.get method
///
/// Returns the current value of a record as JSON.
///
/// # Arguments
/// * `db` - Database instance
/// * `config` - Remote access configuration (for permission checks)
/// * `request_id` - Request ID for the response
/// * `params` - Request parameters (must contain "record" field with record name)
///
/// # Returns
/// Success response with record value as JSON, or error if:
/// - Missing/invalid "record" parameter
/// - Record not found
/// - Record not configured with `.with_remote_access()`
/// - No value available in atomic snapshot
#[cfg(feature = "std")]
async fn handle_record_get<R>(
    db: &Arc<AimDb<R>>,
    _config: &AimxConfig,
    request_id: u64,
    params: Option<serde_json::Value>,
) -> Response
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    // Extract record name from params
    let record_name = match params {
        Some(serde_json::Value::Object(map)) => match map.get("record") {
            Some(serde_json::Value::String(name)) => name.clone(),
            _ => {
                #[cfg(feature = "tracing")]
                tracing::warn!("Missing or invalid 'record' parameter");

                return Response::error(
                    request_id,
                    "invalid_params",
                    "Missing or invalid 'record' parameter".to_string(),
                );
            }
        },
        _ => {
            #[cfg(feature = "tracing")]
            tracing::warn!("Missing params object");

            return Response::error(
                request_id,
                "invalid_params",
                "Missing params object".to_string(),
            );
        }
    };

    #[cfg(feature = "tracing")]
    tracing::debug!("Getting value for record: {}", record_name);

    // Try to peek the record's JSON value
    match db.try_latest_as_json(&record_name) {
        Some(value) => {
            #[cfg(feature = "tracing")]
            tracing::debug!("Successfully retrieved value for {}", record_name);

            Response::success(request_id, value)
        }
        None => {
            #[cfg(feature = "tracing")]
            tracing::warn!("No value available for record: {}", record_name);

            Response::error(
                request_id,
                "not_found",
                format!("No value available for record: {}", record_name),
            )
        }
    }
}

/// Handles record.set method
///
/// Sets a record value from JSON (write operation).
///
/// **SAFETY:** Enforces the "No Producer Override" rule:
/// - Only allows writes to configuration records (producer_count == 0)
/// - Prevents remote access from interfering with application logic
///
/// # Arguments
/// * `db` - Database instance
/// * `config` - Remote access configuration (for permission checks)
/// * `request_id` - Request ID for the response
/// * `params` - Request parameters (must contain "name" and "value" fields)
///
/// # Returns
/// Success response, or error if:
/// - Missing/invalid parameters
/// - Record not found
/// - Permission denied (not writable or has active producers)
/// - Deserialization failed
#[cfg(feature = "std")]
async fn handle_record_set<R>(
    db: &Arc<AimDb<R>>,
    config: &AimxConfig,
    request_id: u64,
    params: Option<serde_json::Value>,
) -> Response
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    use crate::remote::SecurityPolicy;

    // Check if write operations are allowed
    let writable_records = match &config.security_policy {
        SecurityPolicy::ReadOnly => {
            #[cfg(feature = "tracing")]
            tracing::warn!("record.set called but security policy is ReadOnly");

            return Response::error(
                request_id,
                "permission_denied",
                "Write operations not allowed (ReadOnly security policy)".to_string(),
            );
        }
        SecurityPolicy::ReadWrite { writable_records } => writable_records,
    };

    // Extract record name and value from params
    let (record_name, value) = match params {
        Some(serde_json::Value::Object(ref map)) => {
            let name = match map.get("name") {
                Some(serde_json::Value::String(n)) => n.clone(),
                _ => {
                    #[cfg(feature = "tracing")]
                    tracing::warn!("Missing or invalid 'name' parameter in record.set");

                    return Response::error(
                        request_id,
                        "invalid_params",
                        "Missing or invalid 'name' parameter (expected string)".to_string(),
                    );
                }
            };

            let val = match map.get("value") {
                Some(v) => v.clone(),
                None => {
                    #[cfg(feature = "tracing")]
                    tracing::warn!("Missing 'value' parameter in record.set");

                    return Response::error(
                        request_id,
                        "invalid_params",
                        "Missing 'value' parameter".to_string(),
                    );
                }
            };

            (name, val)
        }
        _ => {
            #[cfg(feature = "tracing")]
            tracing::warn!("Missing params object in record.set");

            return Response::error(
                request_id,
                "invalid_params",
                "Missing params object".to_string(),
            );
        }
    };

    #[cfg(feature = "tracing")]
    tracing::debug!("Setting value for record: {}", record_name);

    // Check if record is in the writable_records set (using record key)
    if !writable_records.contains(&record_name) {
        #[cfg(feature = "tracing")]
        tracing::warn!("Record '{}' not in writable_records set", record_name);

        return Response::error(
            request_id,
            "permission_denied",
            format!(
                "Record '{}' is not writable. \
                 Configure with .with_writable_record() to allow writes.",
                record_name
            ),
        );
    }

    // Attempt to set the value
    // This will enforce the "no producer override" rule internally
    match db.set_record_from_json(&record_name, value) {
        Ok(()) => {
            #[cfg(feature = "tracing")]
            tracing::info!("Successfully set value for record: {}", record_name);

            // Get the updated value to return in response
            let result = if let Some(updated_value) = db.try_latest_as_json(&record_name) {
                serde_json::json!({
                    "status": "success",
                    "value": updated_value,
                })
            } else {
                serde_json::json!({
                    "status": "success",
                })
            };

            Response::success(request_id, result)
        }
        Err(e) => {
            #[cfg(feature = "tracing")]
            tracing::error!("Failed to set value for record '{}': {}", record_name, e);

            // Map internal errors to appropriate response codes
            let (code, message) = match e {
                crate::DbError::RecordKeyNotFound { key } => {
                    ("not_found", format!("Record '{}' not found", key))
                }
                crate::DbError::PermissionDenied { operation } => {
                    // This is the "has active producers" error
                    ("permission_denied", operation)
                }
                crate::DbError::JsonWithContext { context, .. } => (
                    "validation_error",
                    format!("JSON validation failed: {}", context),
                ),
                crate::DbError::RuntimeError { message } => ("internal_error", message),
                _ => ("internal_error", format!("Failed to set value: {}", e)),
            };

            Response::error(request_id, code, message)
        }
    }
}

/// Handles record.subscribe method
///
/// Subscribes to live updates for a record.
///
/// # Arguments
/// * `db` - Database instance
/// * `config` - Remote access configuration
/// * `conn_state` - Connection state (for subscription tracking)
/// * `request_id` - Request ID for the response
/// * `params` - Request parameters (must contain "name" field with record name)
///
/// # Returns
/// Success response with subscription_id and queue_size, or error if:
/// - Missing/invalid parameters
/// - Record not found
/// - Too many subscriptions
#[cfg(feature = "std")]
async fn handle_record_subscribe<R>(
    db: &Arc<AimDb<R>>,
    config: &AimxConfig,
    conn_state: &mut ConnectionState,
    request_id: u64,
    params: Option<serde_json::Value>,
) -> Response
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    // Extract record name from params
    let record_name = match params {
        Some(serde_json::Value::Object(ref map)) => match map.get("name") {
            Some(serde_json::Value::String(name)) => name.clone(),
            _ => {
                #[cfg(feature = "tracing")]
                tracing::warn!("Missing or invalid 'name' parameter in record.subscribe");

                return Response::error(
                    request_id,
                    "invalid_params",
                    "Missing or invalid 'name' parameter (expected string)".to_string(),
                );
            }
        },
        _ => {
            #[cfg(feature = "tracing")]
            tracing::warn!("Missing params object in record.subscribe");

            return Response::error(
                request_id,
                "invalid_params",
                "Missing params object".to_string(),
            );
        }
    };

    // Optional: send_initial flag (default true)
    let _send_initial = params
        .as_ref()
        .and_then(|p| p.as_object())
        .and_then(|map| map.get("send_initial"))
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    #[cfg(feature = "tracing")]
    tracing::debug!("Subscribing to record: {}", record_name);

    // Check max subscriptions limit
    if conn_state.subscriptions.len() >= config.subscription_queue_size {
        #[cfg(feature = "tracing")]
        tracing::warn!(
            "Too many subscriptions: {} (max: {})",
            conn_state.subscriptions.len(),
            config.subscription_queue_size
        );

        return Response::error(
            request_id,
            "too_many_subscriptions",
            format!(
                "Maximum subscriptions reached: {}",
                config.subscription_queue_size
            ),
        );
    }

    // Generate unique subscription ID
    let subscription_id = conn_state.generate_subscription_id();

    // Subscribe to record updates via the database API (using record key)
    let (value_rx, cancel_tx) =
        match db.subscribe_record_updates(&record_name, config.subscription_queue_size) {
            Ok(channels) => channels,
            Err(e) => {
                // Map internal errors to appropriate response codes
                let (code, message) = match &e {
                    crate::DbError::RecordKeyNotFound { key } => {
                        #[cfg(feature = "tracing")]
                        tracing::warn!("Record not found: {}", key);
                        ("not_found", format!("Record '{}' not found", key))
                    }
                    _ => {
                        #[cfg(feature = "tracing")]
                        tracing::error!("Failed to subscribe to record updates: {}", e);
                        ("internal_error", format!("Failed to subscribe: {}", e))
                    }
                };

                return Response::error(request_id, code, message);
            }
        };

    // Spawn event streaming task for this subscription
    let event_tx = conn_state.event_tx.clone();
    let sub_id_clone = subscription_id.clone();
    let stream_handle = tokio::spawn(async move {
        stream_subscription_events(sub_id_clone, value_rx, event_tx).await;
    });

    // Store subscription handle
    let handle = SubscriptionHandle {
        subscription_id: subscription_id.clone(),
        record_name: record_name.clone(),
        cancel_tx,
    };
    conn_state.add_subscription(handle);

    // Detach the streaming task (it will run until cancelled or channel closes)
    std::mem::drop(stream_handle);

    #[cfg(feature = "tracing")]
    tracing::info!(
        "Created subscription {} for record {}",
        subscription_id,
        record_name
    );

    // Return success response
    Response::success(
        request_id,
        json!({
            "subscription_id": subscription_id,
            "queue_size": config.subscription_queue_size,
        }),
    )
}

/// Streams subscription events from value channel to event channel
///
/// Reads JSON values from the subscription's receiver and converts them
/// into Event messages with sequence numbers and timestamps.
///
/// # Arguments
/// * `subscription_id` - Unique subscription identifier
/// * `value_rx` - Receiver for JSON values from the database
/// * `event_tx` - Sender for Event messages to the client
#[cfg(feature = "std")]
async fn stream_subscription_events(
    subscription_id: String,
    mut value_rx: tokio::sync::mpsc::Receiver<serde_json::Value>,
    event_tx: tokio::sync::mpsc::UnboundedSender<Event>,
) {
    let mut sequence: u64 = 1;

    #[cfg(feature = "tracing")]
    tracing::debug!(
        "Event streaming task started for subscription: {}",
        subscription_id
    );

    while let Some(json_value) = value_rx.recv().await {
        // Generate timestamp in "secs.nanosecs" format
        let duration = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default();
        let timestamp = format!("{}.{:09}", duration.as_secs(), duration.subsec_nanos());

        // Create event
        let event = Event {
            subscription_id: subscription_id.clone(),
            sequence,
            data: json_value,
            timestamp,
            dropped: None, // TODO: Implement dropped event tracking
        };

        // Send event to the funnel
        if event_tx.send(event).is_err() {
            #[cfg(feature = "tracing")]
            tracing::debug!(
                "Event channel closed, terminating stream for subscription: {}",
                subscription_id
            );
            break;
        }

        sequence += 1;
    }

    #[cfg(feature = "tracing")]
    tracing::debug!(
        "Event streaming task terminated for subscription: {}",
        subscription_id
    );
}

/// Handles record.unsubscribe method
///
/// Cancels an active subscription.
///
/// # Arguments
/// * `conn_state` - Connection state (for subscription tracking)
/// * `request_id` - Request ID for the response
/// * `params` - Request parameters (must contain "subscription_id" field)
///
/// # Returns
/// Success response, or error if subscription not found
#[cfg(feature = "std")]
async fn handle_record_unsubscribe(
    conn_state: &mut ConnectionState,
    request_id: u64,
    params: Option<serde_json::Value>,
) -> Response {
    // Parse subscription_id parameter
    let subscription_id = match params {
        Some(serde_json::Value::Object(ref map)) => match map.get("subscription_id") {
            Some(serde_json::Value::String(id)) => id.clone(),
            _ => {
                return Response::error(
                    request_id,
                    "invalid_params",
                    "Missing or invalid 'subscription_id' parameter".to_string(),
                )
            }
        },
        _ => {
            return Response::error(
                request_id,
                "invalid_params",
                "Missing 'subscription_id' parameter".to_string(),
            )
        }
    };

    #[cfg(feature = "tracing")]
    tracing::debug!("Unsubscribing from subscription_id: {}", subscription_id);

    // Look up and remove the subscription
    match conn_state.subscriptions.remove(&subscription_id) {
        Some(handle) => {
            // Send cancellation signal to the streaming task
            // It's okay if this fails (task may have already terminated)
            let _ = handle.cancel_tx.send(());

            #[cfg(feature = "tracing")]
            tracing::debug!(
                "Cancelled subscription {} for record {}",
                subscription_id,
                handle.record_name
            );

            Response::success(
                request_id,
                serde_json::json!({
                    "subscription_id": subscription_id,
                    "status": "cancelled"
                }),
            )
        }
        None => {
            #[cfg(feature = "tracing")]
            tracing::warn!("Subscription not found: {}", subscription_id);

            Response::error(
                request_id,
                "not_found",
                format!("Subscription '{}' not found", subscription_id),
            )
        }
    }
}

/// Handles record.drain method
///
/// Drains all pending values from a record's drain reader. On the first call for
/// a given record, creates a dedicated drain reader (returns empty). Subsequent
/// calls return all values accumulated since the previous drain.
///
/// # Arguments
/// * `db` - Database instance
/// * `conn_state` - Connection state (for drain reader management)
/// * `request_id` - Request ID for the response
/// * `params` - Request parameters (must contain "name" field, optional "limit")
///
/// # Returns
/// Success response with `record_name`, `values` array, and `count`, or error if:
/// - Missing/invalid parameters
/// - Record not found
/// - Record not configured with `.with_remote_access()`
#[cfg(feature = "std")]
async fn handle_record_drain<R>(
    db: &Arc<AimDb<R>>,
    conn_state: &mut ConnectionState,
    request_id: u64,
    params: Option<serde_json::Value>,
) -> Response
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    // Extract record name from params
    let record_name = match params {
        Some(serde_json::Value::Object(ref map)) => match map.get("name") {
            Some(serde_json::Value::String(name)) => name.clone(),
            _ => {
                return Response::error(
                    request_id,
                    "invalid_params",
                    "Missing or invalid 'name' parameter (expected string)".to_string(),
                );
            }
        },
        _ => {
            return Response::error(
                request_id,
                "invalid_params",
                "Missing params object".to_string(),
            );
        }
    };

    // Optional: limit parameter
    // Use try_from instead of `as` to avoid silent truncation on 32-bit targets
    // (values that don't fit in usize are treated as "no limit").
    let limit = params
        .as_ref()
        .and_then(|p| p.as_object())
        .and_then(|map| map.get("limit"))
        .and_then(|v| v.as_u64())
        .map(|v| usize::try_from(v).unwrap_or(usize::MAX))
        .unwrap_or(usize::MAX);

    #[cfg(feature = "tracing")]
    tracing::debug!(
        "Draining record: {} (limit: {})",
        record_name,
        if limit == usize::MAX {
            "all".to_string()
        } else {
            limit.to_string()
        }
    );

    // Lazily create drain reader on first call for this record
    if !conn_state.drain_readers.contains_key(&record_name) {
        // Resolve record key → RecordId → AnyRecord → subscribe_json()
        let id = match db.inner().resolve_str(&record_name) {
            Some(id) => id,
            None => {
                return Response::error(
                    request_id,
                    "not_found",
                    format!("Record '{}' not found", record_name),
                );
            }
        };

        let record = match db.inner().storage(id) {
            Some(r) => r,
            None => {
                return Response::error(
                    request_id,
                    "not_found",
                    format!("Record '{}' storage not found", record_name),
                );
            }
        };

        let reader = match record.subscribe_json() {
            Ok(r) => r,
            Err(e) => {
                return Response::error(
                    request_id,
                    "remote_access_not_enabled",
                    format!(
                        "Record '{}' not configured with .with_remote_access(): {}",
                        record_name, e
                    ),
                );
            }
        };

        conn_state.drain_readers.insert(record_name.clone(), reader);
    }

    // Drain all pending values from the reader
    let reader = conn_state.drain_readers.get_mut(&record_name).unwrap();
    let mut values = Vec::new();

    loop {
        if values.len() >= limit {
            break;
        }
        match reader.try_recv_json() {
            Ok(val) => values.push(val),
            Err(DbError::BufferEmpty) => break,
            Err(DbError::BufferLagged { .. }) => {
                // Ring overflowed since last drain — cursor resets.
                // Log warning, keep draining.
                #[cfg(feature = "tracing")]
                tracing::warn!(
                    "Drain reader lagged for record '{}' — some values were lost",
                    record_name
                );
                continue;
            }
            Err(_) => break,
        }
    }

    let count = values.len();

    #[cfg(feature = "tracing")]
    tracing::debug!("Drained {} values from record '{}'", count, record_name);

    Response::success(
        request_id,
        json!({
            "record_name": record_name,
            "values": values,
            "count": count,
        }),
    )
}

// ============================================================================
// Persistence Query (record.query)
// ============================================================================

/// Type-erased query handler registered by `aimdb-persistence` via Extensions.
///
/// This keeps `aimdb-core` free of persistence-specific imports. The handler is
/// a boxed async function that accepts query parameters (record pattern, limit,
/// start/end timestamps) and returns a JSON value with the results.
///
/// Registered by `aimdb_persistence` via the `with_persistence()` builder extension.
pub type QueryHandlerFn = Box<
    dyn Fn(
            QueryHandlerParams,
        ) -> core::pin::Pin<
            Box<dyn core::future::Future<Output = Result<serde_json::Value, String>> + Send>,
        > + Send
        + Sync,
>;

/// Parameters for the type-erased query handler.
#[derive(Debug, Clone)]
pub struct QueryHandlerParams {
    /// Record pattern (supports `*` wildcard).
    pub name: String,
    /// Maximum results per matching record.
    pub limit: Option<usize>,
    /// Optional start timestamp (Unix ms).
    pub start: Option<u64>,
    /// Optional end timestamp (Unix ms).
    pub end: Option<u64>,
}

/// Handles `record.query` method.
///
/// Delegates to a [`QueryHandlerFn`] stored in the database's `Extensions`
/// TypeMap. If no handler is registered (i.e. persistence is not configured),
/// returns an error.
#[cfg(feature = "std")]
async fn handle_record_query<R>(
    db: &Arc<AimDb<R>>,
    request_id: u64,
    params: Option<serde_json::Value>,
) -> Response
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    // Extract the query handler from Extensions.
    let handler = match db.extensions().get::<QueryHandlerFn>() {
        Some(h) => h,
        None => {
            return Response::error(
                request_id,
                "not_configured",
                "Persistence not configured. Call .with_persistence() on the builder.".to_string(),
            );
        }
    };

    // Parse parameters
    let (name, limit, start, end) = match &params {
        Some(serde_json::Value::Object(map)) => {
            let name = map
                .get("name")
                .and_then(|v| v.as_str())
                .unwrap_or("*")
                .to_string();
            let limit = map
                .get("limit")
                .and_then(|v| v.as_u64())
                .and_then(|v| usize::try_from(v).ok());
            let start = map.get("start").and_then(|v| v.as_u64());
            let end = map.get("end").and_then(|v| v.as_u64());
            (name, limit, start, end)
        }
        _ => ("*".to_string(), None, None, None),
    };

    let query_params = QueryHandlerParams {
        name,
        limit,
        start,
        end,
    };

    match handler(query_params).await {
        Ok(result) => Response::success(request_id, result),
        Err(msg) => Response::error(request_id, "query_error", msg),
    }
}

// ============================================================================
// Graph Introspection Methods
// ============================================================================

/// Handles graph.nodes method
///
/// Returns all nodes in the dependency graph with their metadata.
/// Each node represents a record with its origin, buffer type, and connections.
///
/// # Arguments
/// * `db` - Database instance
/// * `request_id` - Request ID for the response
///
/// # Returns
/// Success response with array of GraphNode objects:
/// - `key`: Record key (e.g., "temp.vienna")
/// - `origin`: How the record gets its values (source, link, transform, passive)
/// - `buffer_type`: Buffer type ("spmc_ring", "single_latest", "mailbox", "none")
/// - `buffer_capacity`: Optional buffer capacity
/// - `tap_count`: Number of taps attached
/// - `has_outbound_link`: Whether an outbound connector is configured
#[cfg(feature = "std")]
async fn handle_graph_nodes<R>(db: &Arc<AimDb<R>>, request_id: u64) -> Response
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    #[cfg(feature = "tracing")]
    tracing::debug!("Getting dependency graph nodes");

    let graph = db.inner().dependency_graph();
    let nodes = &graph.nodes;

    #[cfg(feature = "tracing")]
    tracing::debug!("Returning {} graph nodes", nodes.len());

    Response::success(request_id, json!(nodes))
}

/// Handles graph.edges method
///
/// Returns all edges in the dependency graph representing data flow between records.
/// Edges are directed from source to target and include the edge type.
///
/// # Arguments
/// * `db` - Database instance
/// * `request_id` - Request ID for the response
///
/// # Returns
/// Success response with array of GraphEdge objects:
/// - `from`: Source record key
/// - `to`: Target record key
/// - `edge_type`: Type of connection (TransformInput, TransformJoinInput, etc.)
#[cfg(feature = "std")]
async fn handle_graph_edges<R>(db: &Arc<AimDb<R>>, request_id: u64) -> Response
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    #[cfg(feature = "tracing")]
    tracing::debug!("Getting dependency graph edges");

    let graph = db.inner().dependency_graph();
    let edges = &graph.edges;

    #[cfg(feature = "tracing")]
    tracing::debug!("Returning {} graph edges", edges.len());

    Response::success(request_id, json!(edges))
}

/// Handles graph.topo_order method
///
/// Returns the topological ordering of records in the dependency graph.
/// This ordering ensures that all dependencies are processed before dependents.
/// Used for spawn ordering and understanding data flow.
///
/// # Arguments
/// * `db` - Database instance
/// * `request_id` - Request ID for the response
///
/// # Returns
/// Success response with array of record keys in topological order:
/// - Sources and passive records first
/// - Transform outputs after their inputs
/// - Respects the DAG structure for proper initialization order
#[cfg(feature = "std")]
async fn handle_graph_topo_order<R>(db: &Arc<AimDb<R>>, request_id: u64) -> Response
where
    R: crate::RuntimeAdapter + crate::Spawn + 'static,
{
    #[cfg(feature = "tracing")]
    tracing::debug!("Getting topological order");

    let graph = db.inner().dependency_graph();
    let topo_order = graph.topo_order();

    #[cfg(feature = "tracing")]
    tracing::debug!(
        "Returning topological order with {} records",
        topo_order.len()
    );

    Response::success(request_id, json!(topo_order))
}