code-graph-cli 3.0.3

Code intelligence engine for TypeScript/JavaScript/Rust/Python/Go — query the dependency graph instead of reading source files.
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
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context, Result};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::UnixListener;
use tokio::sync::{RwLock, watch};

use crate::daemon::pid;
use crate::daemon::protocol::{DaemonRequest, DaemonResponse, PROTOCOL_VERSION};
use crate::graph::CodeGraph;

/// Maximum allowed request size in bytes (1 MB).
const MAX_REQUEST_BYTES: usize = 1_048_576;

/// Run the daemon server: build graph, watch for changes, serve queries over Unix socket.
///
/// This function does not return under normal operation. It runs until a Shutdown
/// request, SIGTERM, or SIGINT is received.
pub async fn run_daemon(project_root: PathBuf) -> Result<()> {
    eprintln!("[daemon] starting for project: {}", project_root.display());

    // 1. Build initial graph.
    let graph = tokio::task::spawn_blocking({
        let root = project_root.clone();
        move || crate::build_graph(&root, false)
    })
    .await
    .context("build_graph task panicked")?
    .context("failed to build initial graph")?;

    eprintln!(
        "[daemon] indexed {} files, {} symbols",
        graph.file_count(),
        graph.symbol_count()
    );

    let graph = Arc::new(RwLock::new(graph));

    // 2. Write PID file.
    pid::write_pid_file(&project_root)?;

    // 3. Bind Unix socket (remove stale socket first).
    let sock_path = pid::socket_path(&project_root);
    pid::remove_socket_file(&project_root)?;

    // Ensure parent directory exists.
    if let Some(parent) = sock_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    let listener = UnixListener::bind(&sock_path)
        .with_context(|| format!("failed to bind socket at {}", sock_path.display()))?;

    // Restrict socket permissions to owner-only (0600) immediately after bind.
    // NOTE: umask would be ideal to avoid any TOCTOU window, but it is
    // process-global and unsafe in multi-threaded programs. The parent
    // directory is already restricted to 0700 (set in write_pid_file),
    // which limits exposure during the brief window.
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let perms = std::fs::Permissions::from_mode(0o600);
        std::fs::set_permissions(&sock_path, perms)
            .with_context(|| format!("failed to set permissions on {}", sock_path.display()))?;
    }

    eprintln!("[daemon] listening on {}", sock_path.display());

    // 4. Shutdown signal channel.
    let (shutdown_tx, shutdown_rx) = watch::channel(false);

    // 5. Spawn watcher task.
    let watcher_handle = spawn_watcher(
        project_root.clone(),
        Arc::clone(&graph),
        shutdown_rx.clone(),
    );

    // 6. Spawn signal handler.
    let signal_shutdown_tx = shutdown_tx.clone();
    tokio::spawn(async move {
        wait_for_signal().await;
        eprintln!("[daemon] signal received, shutting down...");
        let _ = signal_shutdown_tx.send(true);
    });

    // 7. Accept connections until shutdown.
    let accept_result = accept_loop(
        listener,
        Arc::clone(&graph),
        project_root.clone(),
        shutdown_tx.clone(),
        shutdown_rx.clone(),
    )
    .await;

    // 8. Graceful shutdown: save cache, remove PID and socket files.
    eprintln!("[daemon] shutting down...");

    // Wait briefly for watcher to stop.
    let _ = tokio::time::timeout(Duration::from_secs(2), watcher_handle).await;

    // Save cache.
    {
        let g = graph.read().await;
        if let Err(e) = crate::cache::save_cache(&project_root, &g) {
            eprintln!("[daemon] failed to save cache on shutdown: {}", e);
        } else {
            eprintln!("[daemon] cache saved");
        }
    }

    // Remove PID and socket files, logging any errors.
    if let Err(e) = pid::remove_pid_file(&project_root) {
        eprintln!("[daemon] cleanup warning: {}", e);
    }
    if let Err(e) = pid::remove_socket_file(&project_root) {
        eprintln!("[daemon] cleanup warning: {}", e);
    }

    eprintln!("[daemon] stopped");
    accept_result
}

/// Accept connections on the Unix socket until shutdown is signaled.
async fn accept_loop(
    listener: UnixListener,
    graph: Arc<RwLock<CodeGraph>>,
    project_root: PathBuf,
    shutdown_tx: watch::Sender<bool>,
    mut shutdown_rx: watch::Receiver<bool>,
) -> Result<()> {
    loop {
        tokio::select! {
            result = listener.accept() => {
                match result {
                    Ok((stream, _addr)) => {
                        let graph = Arc::clone(&graph);
                        let root = project_root.clone();
                        let tx = shutdown_tx.clone();
                        tokio::spawn(async move {
                            if let Err(e) = handle_connection(stream, graph, root, tx).await {
                                eprintln!("[daemon] connection error: {}", e);
                            }
                        });
                    }
                    Err(e) => {
                        eprintln!("[daemon] accept error: {}", e);
                    }
                }
            }
            _ = shutdown_rx.changed() => {
                if *shutdown_rx.borrow() {
                    break;
                }
            }
        }
    }
    Ok(())
}

/// Send a JSON-line response to the client and shut down the write half.
///
/// Appends a newline to the serialized JSON before writing to avoid two
/// separate write_all syscalls.
async fn send_response(
    writer: &mut tokio::net::unix::OwnedWriteHalf,
    response: &DaemonResponse,
) -> Result<()> {
    let mut json = serde_json::to_string(response)?;
    json.push('\n');
    writer.write_all(json.as_bytes()).await?;
    writer.shutdown().await?;
    Ok(())
}

/// Handle a single client connection: read one JSON line, dispatch, write one JSON line.
async fn handle_connection(
    stream: tokio::net::UnixStream,
    graph: Arc<RwLock<CodeGraph>>,
    project_root: PathBuf,
    shutdown_tx: watch::Sender<bool>,
) -> Result<()> {
    let (reader, mut writer) = stream.into_split();
    let mut buf_reader = BufReader::new(reader);

    // Read one line (up to MAX_REQUEST_BYTES) with a 30-second timeout to
    // prevent slow-client DoS.
    let read_result = tokio::time::timeout(Duration::from_secs(30), async {
        let mut line = String::new();
        let mut total_read = 0usize;
        loop {
            let bytes_read = buf_reader
                .read_line(&mut line)
                .await
                .context("failed to read from socket")?;
            if bytes_read == 0 {
                // EOF before newline — client disconnected.
                return Ok(None);
            }
            total_read += bytes_read;
            if total_read > MAX_REQUEST_BYTES {
                return Ok(Some(Err(
                    "request too large (exceeds 1 MB limit)".to_string()
                )));
            }
            if line.ends_with('\n') {
                break;
            }
        }
        Ok(Some(Ok(line)))
    })
    .await;

    let line = match read_result {
        Ok(Ok(Some(Ok(line)))) => line,
        Ok(Ok(Some(Err(size_err)))) => {
            let resp = DaemonResponse::error(size_err);
            send_response(&mut writer, &resp).await?;
            return Ok(());
        }
        Ok(Ok(None)) => {
            // Client disconnected before sending anything.
            return Ok(());
        }
        Ok(Err(e)) => {
            // Read error.
            return Err(e);
        }
        Err(_) => {
            // Timeout.
            let resp = DaemonResponse::error("request read timeout");
            send_response(&mut writer, &resp).await?;
            return Ok(());
        }
    };

    let line = line.trim();
    if line.is_empty() {
        return Ok(());
    }

    // Parse directly into the typed request (single parse, no intermediate Value).
    let request: DaemonRequest = match serde_json::from_str(line) {
        Ok(r) => r,
        Err(e) => {
            let response = DaemonResponse::error(format!("invalid request: {}", e));
            send_response(&mut writer, &response).await?;
            return Ok(());
        }
    };

    // Handle Shutdown specially — it triggers daemon-wide shutdown.
    if matches!(request, DaemonRequest::Shutdown) {
        let response = DaemonResponse::success(serde_json::json!({"message": "shutting down"}));
        send_response(&mut writer, &response).await?;
        let _ = shutdown_tx.send(true);
        return Ok(());
    }

    // Dispatch the query. Use block_in_place to yield the tokio worker thread
    // during the potentially CPU-bound dispatch, reducing RwLock contention.
    let response = {
        let g = graph.read().await;
        tokio::task::block_in_place(|| dispatch_query(&request, &g, &project_root))
    };

    send_response(&mut writer, &response).await?;

    Ok(())
}

/// Spawn the file watcher task that runs in a blocking thread and forwards
/// events to the graph via incremental updates.
fn spawn_watcher(
    project_root: PathBuf,
    graph: Arc<RwLock<CodeGraph>>,
    shutdown_rx: watch::Receiver<bool>,
) -> tokio::task::JoinHandle<()> {
    tokio::spawn(async move {
        // Start the watcher in a blocking context (uses std mpsc).
        let watcher_result = {
            let root = project_root.clone();
            tokio::task::spawn_blocking(move || crate::watcher::start_watcher(&root)).await
        };

        let (_handle, rx) = match watcher_result {
            Ok(Ok((handle, rx))) => (handle, rx),
            Ok(Err(e)) => {
                eprintln!("[daemon] failed to start watcher: {}", e);
                return;
            }
            Err(e) => {
                eprintln!("[daemon] watcher task panicked: {}", e);
                return;
            }
        };

        eprintln!("[daemon] file watcher started");

        // Relay std mpsc events to a tokio mpsc channel using a dedicated
        // blocking thread, then process events asynchronously.
        run_watcher_relay(rx, graph, project_root, shutdown_rx).await;
    })
}

/// Relay events from the std mpsc Receiver to incremental graph updates,
/// batching cache saves.
async fn run_watcher_relay(
    rx: std::sync::mpsc::Receiver<crate::watcher::event::WatchEvent>,
    graph: Arc<RwLock<CodeGraph>>,
    project_root: PathBuf,
    mut shutdown_rx: watch::Receiver<bool>,
) {
    // Bridge: spawn a blocking task that reads from the std receiver
    // and sends to a tokio mpsc channel.
    let (relay_tx, mut relay_rx) =
        tokio::sync::mpsc::channel::<crate::watcher::event::WatchEvent>(256);

    let bridge = tokio::task::spawn_blocking(move || {
        while let Ok(event) = rx.recv() {
            if relay_tx.blocking_send(event).is_err() {
                break; // receiver dropped
            }
        }
    });

    // Timer for batched cache saves — save at most once per second.
    let mut dirty = false;
    let mut save_interval = tokio::time::interval(Duration::from_secs(1));
    save_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
    // Skip the first immediate tick.
    save_interval.tick().await;

    loop {
        tokio::select! {
            event = relay_rx.recv() => {
                match event {
                    Some(ev) => {
                        handle_watcher_event(&ev, &graph, &project_root).await;
                        dirty = true;
                    }
                    None => break, // bridge thread finished
                }
            }
            _ = save_interval.tick(), if dirty => {
                let g = graph.read().await;
                if let Err(e) = crate::cache::save_cache(&project_root, &g) {
                    eprintln!("[daemon] cache save error: {}", e);
                }
                dirty = false;
            }
            _ = shutdown_rx.changed() => {
                if *shutdown_rx.borrow() {
                    break;
                }
            }
        }
    }

    // Final cache save if dirty.
    if dirty {
        let g = graph.read().await;
        let _ = crate::cache::save_cache(&project_root, &g);
    }

    // Clean up bridge thread.
    drop(relay_rx);
    let _ = bridge.await;
}

/// Perform a full graph re-index, replacing the shared graph under a write lock.
async fn full_reindex(graph: &Arc<RwLock<CodeGraph>>, project_root: &Path, reason: &str) {
    eprintln!("[daemon] {} -- full re-index...", reason);
    let start = std::time::Instant::now();
    let root = project_root.to_path_buf();
    match tokio::task::spawn_blocking(move || crate::build_graph(&root, false)).await {
        Ok(Ok(new_graph)) => {
            let mut g = graph.write().await;
            *g = new_graph;
            let elapsed = start.elapsed();
            eprintln!(
                "[daemon] re-indexed in {:.1}ms ({} files, {} symbols)",
                elapsed.as_secs_f64() * 1000.0,
                g.file_count(),
                g.symbol_count(),
            );
        }
        Ok(Err(e)) => {
            eprintln!("[daemon] full re-index failed: {}", e);
        }
        Err(e) => {
            eprintln!("[daemon] re-index task panicked: {}", e);
        }
    }
}

/// Process a single watcher event, updating the graph.
async fn handle_watcher_event(
    event: &crate::watcher::event::WatchEvent,
    graph: &Arc<RwLock<CodeGraph>>,
    project_root: &Path,
) {
    use crate::watcher::event::WatchEvent;

    match event {
        WatchEvent::Modified(p) => {
            let start = std::time::Instant::now();
            {
                let mut g = graph.write().await;
                crate::watcher::incremental::handle_file_event(&mut g, event, project_root);
            }
            let elapsed = start.elapsed();
            eprintln!(
                "[daemon] incremental: {} ({:.1}ms)",
                p.strip_prefix(project_root).unwrap_or(p).display(),
                elapsed.as_secs_f64() * 1000.0,
            );
        }
        WatchEvent::Deleted(p) => {
            let mut g = graph.write().await;
            crate::watcher::incremental::handle_file_event(&mut g, event, project_root);
            eprintln!(
                "[daemon] deleted: {} ({} files, {} symbols)",
                p.strip_prefix(project_root).unwrap_or(p).display(),
                g.file_count(),
                g.symbol_count(),
            );
        }
        WatchEvent::ConfigChanged => {
            full_reindex(graph, project_root, "config changed").await;
        }
        WatchEvent::CrateRootChanged(p) => {
            let filename = p.file_name().unwrap_or_default().to_string_lossy();
            full_reindex(graph, project_root, &format!("{} changed", filename)).await;
        }
    }
}

/// Wait for SIGTERM or SIGINT.
async fn wait_for_signal() {
    #[cfg(unix)]
    {
        use tokio::signal::unix::{SignalKind, signal};
        let mut sigterm =
            signal(SignalKind::terminate()).expect("failed to register SIGTERM handler");
        let mut sigint =
            signal(SignalKind::interrupt()).expect("failed to register SIGINT handler");
        tokio::select! {
            _ = sigterm.recv() => {}
            _ = sigint.recv() => {}
        }
    }
    #[cfg(not(unix))]
    {
        // Fallback for non-Unix: wait for Ctrl+C.
        let _ = tokio::signal::ctrl_c().await;
    }
}

// ---------------------------------------------------------------------------
// Query dispatch
// ---------------------------------------------------------------------------

/// Dispatch a `DaemonRequest` to the appropriate query function and return a
/// `DaemonResponse`.
///
/// This is the central query router. It mirrors the CLI command dispatch in
/// `main.rs` but operates on a shared `&CodeGraph` reference.
fn dispatch_query(
    request: &DaemonRequest,
    graph: &CodeGraph,
    project_root: &Path,
) -> DaemonResponse {
    match request {
        DaemonRequest::Ping => DaemonResponse::success(serde_json::json!({
            "daemon": "code-graph",
            "version": PROTOCOL_VERSION,
            "pid": std::process::id(),
        })),

        DaemonRequest::Shutdown => {
            unreachable!("Shutdown is intercepted before dispatch_query")
        }

        DaemonRequest::Find {
            symbol,
            case_insensitive,
            kind,
            file,
            language,
        } => dispatch_find(
            graph,
            project_root,
            symbol,
            *case_insensitive,
            kind,
            file.as_deref(),
            language.as_deref(),
        ),

        DaemonRequest::Refs {
            symbol,
            case_insensitive,
            kind,
            file,
            language,
        } => dispatch_refs(
            graph,
            project_root,
            symbol,
            *case_insensitive,
            kind,
            file.as_deref(),
            language.as_deref(),
        ),

        DaemonRequest::Impact {
            symbol,
            case_insensitive,
            tree: _,
            language,
        } => dispatch_impact(
            graph,
            project_root,
            symbol,
            *case_insensitive,
            language.as_deref(),
        ),

        DaemonRequest::Context {
            symbol,
            case_insensitive,
            language,
        } => dispatch_context(
            graph,
            project_root,
            symbol,
            *case_insensitive,
            language.as_deref(),
        ),

        DaemonRequest::Stats { language } => dispatch_stats(graph, language.as_deref()),

        DaemonRequest::Circular { language } => {
            dispatch_circular(graph, project_root, language.as_deref())
        }

        DaemonRequest::DeadCode { scope } => {
            dispatch_dead_code(graph, project_root, scope.as_deref())
        }

        DaemonRequest::Clones { scope, min_group } => {
            dispatch_clones(graph, project_root, scope.as_deref(), *min_group)
        }

        DaemonRequest::Export {
            format,
            granularity,
            stdout: _,
            root,
            symbol,
            depth,
            exclude,
        } => dispatch_export(
            graph,
            project_root,
            &ExportArgs {
                format,
                granularity,
                root_filter: root.as_deref(),
                symbol_filter: symbol.as_deref(),
                depth: *depth,
                exclude,
            },
        ),

        DaemonRequest::Structure { path, depth } => {
            dispatch_structure(graph, project_root, path.as_deref(), *depth)
        }

        DaemonRequest::FileSummary { file } => dispatch_file_summary(graph, project_root, file),

        DaemonRequest::Imports { file } => dispatch_imports(graph, project_root, file),

        DaemonRequest::Diff { from, to } => dispatch_diff(graph, project_root, from, to.as_deref()),

        DaemonRequest::DiffImpact { base_ref } => {
            dispatch_diff_impact(graph, project_root, base_ref)
        }

        DaemonRequest::Decorators {
            pattern,
            language,
            framework,
        } => dispatch_decorators(graph, pattern, language.as_deref(), framework.as_deref()),

        DaemonRequest::Clusters { scope } => {
            dispatch_clusters(graph, project_root, scope.as_deref())
        }

        DaemonRequest::Flow {
            entry,
            target,
            max_paths,
            max_depth,
        } => dispatch_flow(graph, entry, target, *max_paths, *max_depth),

        DaemonRequest::Rename { symbol, new_name } => {
            dispatch_rename(graph, project_root, symbol, new_name)
        }

        DaemonRequest::SnapshotCreate { name } => {
            dispatch_snapshot_create(graph, project_root, name)
        }

        DaemonRequest::SnapshotList => dispatch_snapshot_list(project_root),

        DaemonRequest::SnapshotDelete { name } => dispatch_snapshot_delete(project_root, name),
    }
}

// ---------------------------------------------------------------------------
// Individual dispatch helpers
// ---------------------------------------------------------------------------

fn dispatch_find(
    graph: &CodeGraph,
    project_root: &Path,
    symbol: &str,
    case_insensitive: bool,
    kind_filter: &[String],
    file_filter: Option<&Path>,
    language: Option<&str>,
) -> DaemonResponse {
    let language_filter = match parse_lang(language) {
        Ok(f) => f,
        Err(e) => return DaemonResponse::error(e),
    };

    match crate::query::find::find_symbol(
        graph,
        symbol,
        case_insensitive,
        kind_filter,
        file_filter,
        project_root,
        language_filter,
    ) {
        Ok(results) => {
            let data: Vec<serde_json::Value> = results
                .iter()
                .map(|r| find_result_to_json(r, project_root))
                .collect();
            DaemonResponse::success(serde_json::json!(data))
        }
        Err(e) => DaemonResponse::error(format!("{}", e)),
    }
}

fn dispatch_refs(
    graph: &CodeGraph,
    project_root: &Path,
    symbol: &str,
    case_insensitive: bool,
    kind_filter: &[String],
    file_filter: Option<&Path>,
    language: Option<&str>,
) -> DaemonResponse {
    let language_filter = match parse_lang(language) {
        Ok(f) => f,
        Err(e) => return DaemonResponse::error(e),
    };

    let matches = match crate::query::find::match_symbols(graph, symbol, case_insensitive) {
        Ok(m) => m,
        Err(e) => return DaemonResponse::error(format!("{}", e)),
    };

    if matches.is_empty() {
        return DaemonResponse::error(format!("no symbols matching '{}' found", symbol));
    }

    let all_indices: Vec<petgraph::stable_graph::NodeIndex> = matches
        .iter()
        .flat_map(|(_, indices)| indices.iter().copied())
        .collect();

    let mut results = crate::query::refs::find_refs(graph, symbol, &all_indices, project_root);

    // Apply kind filter (e.g. "import", "call")
    if !kind_filter.is_empty() {
        results.retain(|r| {
            let kind_str = match r.ref_kind {
                crate::query::refs::RefKind::Import => "import",
                crate::query::refs::RefKind::Call => "call",
            };
            kind_filter.iter().any(|k| k.eq_ignore_ascii_case(kind_str))
        });
    }

    // Apply file filter
    if let Some(file) = file_filter {
        let abs_file = if file.is_absolute() {
            file.to_path_buf()
        } else {
            project_root.join(file)
        };
        results.retain(|r| r.file_path.starts_with(&abs_file));
    }

    if let Some(lang) = language_filter {
        results.retain(|r| file_language_matches(&r.file_path, lang));
    }

    let data: Vec<serde_json::Value> = results
        .iter()
        .map(|r| ref_result_to_json(r, project_root))
        .collect();
    DaemonResponse::success(serde_json::json!(data))
}

fn dispatch_impact(
    graph: &CodeGraph,
    project_root: &Path,
    symbol: &str,
    case_insensitive: bool,
    language: Option<&str>,
) -> DaemonResponse {
    let language_filter = match parse_lang(language) {
        Ok(f) => f,
        Err(e) => return DaemonResponse::error(e),
    };

    let matches = match crate::query::find::match_symbols(graph, symbol, case_insensitive) {
        Ok(m) => m,
        Err(e) => return DaemonResponse::error(format!("{}", e)),
    };

    if matches.is_empty() {
        return DaemonResponse::error(format!("no symbols matching '{}' found", symbol));
    }

    let all_indices: Vec<petgraph::stable_graph::NodeIndex> = matches
        .iter()
        .flat_map(|(_, indices)| indices.iter().copied())
        .collect();

    let mut results = crate::query::impact::blast_radius(graph, &all_indices, project_root);

    if let Some(lang) = language_filter {
        results.retain(|r| file_language_matches(&r.file_path, lang));
    }

    match serde_json::to_value(&results) {
        Ok(data) => DaemonResponse::success(data),
        Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
    }
}

fn dispatch_context(
    graph: &CodeGraph,
    project_root: &Path,
    symbol: &str,
    case_insensitive: bool,
    language: Option<&str>,
) -> DaemonResponse {
    let language_filter = match parse_lang(language) {
        Ok(f) => f,
        Err(e) => return DaemonResponse::error(e),
    };

    let matches = match crate::query::find::match_symbols(graph, symbol, case_insensitive) {
        Ok(m) => m,
        Err(e) => return DaemonResponse::error(format!("{}", e)),
    };

    if matches.is_empty() {
        return DaemonResponse::error(format!("no symbols matching '{}' found", symbol));
    }

    let mut results: Vec<crate::query::context::SymbolContext> = matches
        .iter()
        .map(|(name, indices)| {
            crate::query::context::symbol_context(graph, name, indices, project_root)
        })
        .collect();

    if let Some(lang) = language_filter {
        for ctx in &mut results {
            ctx.definitions
                .retain(|d| file_language_matches(&d.file_path, lang));
            ctx.references
                .retain(|r| file_language_matches(&r.file_path, lang));
            ctx.callers
                .retain(|c| file_language_matches(&c.file_path, lang));
            ctx.callees
                .retain(|c| file_language_matches(&c.file_path, lang));
        }
        results.retain(|ctx| !ctx.definitions.is_empty());
    }

    let data: Vec<serde_json::Value> = results
        .iter()
        .map(|ctx| context_to_json(ctx, project_root))
        .collect();
    DaemonResponse::success(serde_json::json!(data))
}

fn dispatch_stats(graph: &CodeGraph, language: Option<&str>) -> DaemonResponse {
    let language_filter = match parse_lang(language) {
        Ok(f) => f,
        Err(e) => return DaemonResponse::error(e),
    };

    let stats = crate::query::stats::project_stats(graph);
    DaemonResponse::success(stats_to_json(&stats, language_filter))
}

fn dispatch_circular(
    graph: &CodeGraph,
    project_root: &Path,
    language: Option<&str>,
) -> DaemonResponse {
    let language_filter = match parse_lang(language) {
        Ok(f) => f,
        Err(e) => return DaemonResponse::error(e),
    };

    let mut cycles = crate::query::circular::find_circular(graph, project_root);

    if let Some(lang) = language_filter {
        cycles.retain(|c| c.files.iter().all(|f| file_language_matches(f, lang)));
    }

    let data: Vec<serde_json::Value> = cycles
        .iter()
        .map(|c| {
            let files: Vec<String> = c
                .files
                .iter()
                .map(|f| {
                    f.strip_prefix(project_root)
                        .unwrap_or(f)
                        .to_string_lossy()
                        .into_owned()
                })
                .collect();
            serde_json::json!({ "files": files })
        })
        .collect();
    DaemonResponse::success(serde_json::json!(data))
}

fn dispatch_dead_code(
    graph: &CodeGraph,
    project_root: &Path,
    scope: Option<&Path>,
) -> DaemonResponse {
    let result = crate::query::dead_code::find_dead_code(graph, project_root, scope);
    match serde_json::to_value(&result) {
        Ok(data) => DaemonResponse::success(data),
        Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
    }
}

fn dispatch_clones(
    graph: &CodeGraph,
    project_root: &Path,
    scope: Option<&Path>,
    min_group: usize,
) -> DaemonResponse {
    let result = crate::query::clones::find_clones(graph, project_root, scope, min_group);
    match serde_json::to_value(&result) {
        Ok(data) => DaemonResponse::success(data),
        Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
    }
}

struct ExportArgs<'a> {
    format: &'a str,
    granularity: &'a str,
    root_filter: Option<&'a Path>,
    symbol_filter: Option<&'a str>,
    depth: usize,
    exclude: &'a [String],
}

fn dispatch_export(
    graph: &CodeGraph,
    project_root: &Path,
    args: &ExportArgs<'_>,
) -> DaemonResponse {
    let fmt = match args.format {
        "dot" => crate::export::model::ExportFormat::Dot,
        "mermaid" => crate::export::model::ExportFormat::Mermaid,
        other => {
            return DaemonResponse::error(format!(
                "unknown export format '{}'. Valid: dot, mermaid",
                other
            ));
        }
    };

    let gran = match args.granularity {
        "symbol" => crate::export::model::Granularity::Symbol,
        "file" => crate::export::model::Granularity::File,
        "package" => crate::export::model::Granularity::Package,
        other => {
            return DaemonResponse::error(format!(
                "unknown granularity '{}'. Valid: symbol, file, package",
                other
            ));
        }
    };

    let params = crate::export::model::ExportParams {
        format: fmt,
        granularity: gran,
        root_filter: args.root_filter.map(|p| p.to_path_buf()),
        symbol_filter: args.symbol_filter.map(|s| s.to_string()),
        depth: args.depth,
        exclude_patterns: args.exclude.to_vec(),
        project_root: project_root.to_path_buf(),
        stdout: true,
    };

    match crate::export::export_graph(graph, &params) {
        Ok(result) => DaemonResponse::success(serde_json::json!({
            "content": result.content,
            "node_count": result.node_count,
            "edge_count": result.edge_count,
        })),
        Err(e) => DaemonResponse::error(format!("{}", e)),
    }
}

fn dispatch_structure(
    graph: &CodeGraph,
    project_root: &Path,
    path: Option<&Path>,
    depth: usize,
) -> DaemonResponse {
    let tree = crate::query::structure::file_structure(graph, project_root, path, depth);
    match serde_json::to_value(&tree) {
        Ok(data) => DaemonResponse::success(data),
        Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
    }
}

fn dispatch_file_summary(graph: &CodeGraph, project_root: &Path, file: &Path) -> DaemonResponse {
    match crate::query::file_summary::file_summary(graph, project_root, file) {
        Ok(summary) => match serde_json::to_value(&summary) {
            Ok(data) => DaemonResponse::success(data),
            Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
        },
        Err(e) => DaemonResponse::error(e),
    }
}

fn dispatch_imports(graph: &CodeGraph, project_root: &Path, file: &Path) -> DaemonResponse {
    match crate::query::imports::file_imports(graph, project_root, file) {
        Ok(entries) => match serde_json::to_value(&entries) {
            Ok(data) => DaemonResponse::success(data),
            Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
        },
        Err(e) => DaemonResponse::error(e),
    }
}

fn dispatch_diff(
    graph: &CodeGraph,
    project_root: &Path,
    from: &str,
    to: Option<&str>,
) -> DaemonResponse {
    match crate::query::diff::compute_diff(project_root, from, to, graph) {
        Ok(diff) => match serde_json::to_value(&diff) {
            Ok(data) => DaemonResponse::success(data),
            Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
        },
        Err(e) => DaemonResponse::error(e),
    }
}

fn dispatch_diff_impact(graph: &CodeGraph, project_root: &Path, base_ref: &str) -> DaemonResponse {
    // Shell out to git diff --name-only
    let output = match std::process::Command::new("git")
        .args(["diff", "--name-only", "--", base_ref])
        .current_dir(project_root)
        .output()
    {
        Ok(o) => o,
        Err(e) => {
            return DaemonResponse::error(format!(
                "failed to run git: {}. Ensure git is in PATH.",
                e
            ));
        }
    };

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return DaemonResponse::error(format!("git diff failed: {}", stderr));
    }

    let changed_files: Vec<PathBuf> = String::from_utf8_lossy(&output.stdout)
        .lines()
        .filter(|l| !l.is_empty())
        .map(|l| project_root.join(l))
        .collect();

    if changed_files.is_empty() {
        return DaemonResponse::success(
            serde_json::json!({"message": "no changed files", "results": []}),
        );
    }

    let config = crate::config::CodeGraphConfig::load(project_root);
    let results = crate::query::impact::diff_impact(
        graph,
        &changed_files,
        project_root,
        config.impact.high_threshold,
        config.impact.medium_threshold,
    );

    match serde_json::to_value(&results) {
        Ok(data) => DaemonResponse::success(data),
        Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
    }
}

fn dispatch_decorators(
    graph: &CodeGraph,
    pattern: &str,
    language: Option<&str>,
    framework: Option<&str>,
) -> DaemonResponse {
    match crate::query::decorators::find_by_decorator(graph, pattern, language, framework, 100) {
        Ok(results) => match serde_json::to_value(&results) {
            Ok(data) => DaemonResponse::success(data),
            Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
        },
        Err(e) => DaemonResponse::error(format!("{}", e)),
    }
}

fn dispatch_clusters(
    graph: &CodeGraph,
    project_root: &Path,
    scope: Option<&Path>,
) -> DaemonResponse {
    let results = crate::query::clusters::find_clusters(graph, project_root, scope, 100);
    match serde_json::to_value(&results) {
        Ok(data) => DaemonResponse::success(data),
        Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
    }
}

fn dispatch_flow(
    graph: &CodeGraph,
    entry: &str,
    target: &str,
    max_paths: usize,
    max_depth: usize,
) -> DaemonResponse {
    let result = crate::query::flow::trace_flow(graph, entry, target, max_paths, max_depth);
    match serde_json::to_value(&result) {
        Ok(data) => DaemonResponse::success(data),
        Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
    }
}

fn dispatch_rename(
    graph: &CodeGraph,
    project_root: &Path,
    symbol: &str,
    new_name: &str,
) -> DaemonResponse {
    let items = crate::query::rename::plan_rename(graph, symbol, new_name, project_root);
    match serde_json::to_value(&items) {
        Ok(data) => DaemonResponse::success(data),
        Err(e) => DaemonResponse::error(format!("serialization error: {}", e)),
    }
}

fn dispatch_snapshot_create(graph: &CodeGraph, project_root: &Path, name: &str) -> DaemonResponse {
    match crate::query::diff::create_snapshot(graph, project_root, name) {
        Ok(()) => DaemonResponse::success(
            serde_json::json!({"message": format!("snapshot '{}' created", name)}),
        ),
        Err(e) => DaemonResponse::error(format!("{}", e)),
    }
}

fn dispatch_snapshot_list(project_root: &Path) -> DaemonResponse {
    match crate::query::diff::list_snapshots(project_root) {
        Ok(snapshots) => {
            let data: Vec<serde_json::Value> = snapshots
                .iter()
                .map(|(name, ts)| serde_json::json!({"name": name, "created_at": ts}))
                .collect();
            DaemonResponse::success(serde_json::json!(data))
        }
        Err(e) => DaemonResponse::error(format!("{}", e)),
    }
}

fn dispatch_snapshot_delete(project_root: &Path, name: &str) -> DaemonResponse {
    match crate::query::diff::delete_snapshot(project_root, name) {
        Ok(()) => DaemonResponse::success(
            serde_json::json!({"message": format!("snapshot '{}' deleted", name)}),
        ),
        Err(e) => DaemonResponse::error(format!("{}", e)),
    }
}

// ---------------------------------------------------------------------------
// JSON serialization helpers for types without Serialize derive
// ---------------------------------------------------------------------------

fn find_result_to_json(
    r: &crate::query::find::FindResult,
    project_root: &Path,
) -> serde_json::Value {
    let rel = r
        .file_path
        .strip_prefix(project_root)
        .unwrap_or(&r.file_path);
    serde_json::json!({
        "name": r.symbol_name,
        "kind": crate::query::find::kind_to_str(&r.kind),
        "file": rel.to_string_lossy(),
        "line": r.line,
        "line_end": r.line_end,
        "col": r.col,
        "exported": r.is_exported,
        "default": r.is_default,
    })
}

fn ref_result_to_json(r: &crate::query::refs::RefResult, project_root: &Path) -> serde_json::Value {
    let rel = r
        .file_path
        .strip_prefix(project_root)
        .unwrap_or(&r.file_path);
    serde_json::json!({
        "file": rel.to_string_lossy(),
        "ref_kind": format!("{:?}", r.ref_kind).to_lowercase(),
        "symbol_name": r.symbol_name,
        "line": r.line,
    })
}

fn context_to_json(
    ctx: &crate::query::context::SymbolContext,
    project_root: &Path,
) -> serde_json::Value {
    serde_json::json!({
        "symbol_name": ctx.symbol_name,
        "definitions": ctx.definitions.iter().map(|d| find_result_to_json(d, project_root)).collect::<Vec<_>>(),
        "references": ctx.references.iter().map(|r| ref_result_to_json(r, project_root)).collect::<Vec<_>>(),
        "callers": ctx.callers.iter().map(|c| call_info_to_json(c, project_root)).collect::<Vec<_>>(),
        "callees": ctx.callees.iter().map(|c| call_info_to_json(c, project_root)).collect::<Vec<_>>(),
        "extends": ctx.extends.iter().map(|c| call_info_to_json(c, project_root)).collect::<Vec<_>>(),
        "implements": ctx.implements.iter().map(|c| call_info_to_json(c, project_root)).collect::<Vec<_>>(),
        "extended_by": ctx.extended_by.iter().map(|c| call_info_to_json(c, project_root)).collect::<Vec<_>>(),
        "implemented_by": ctx.implemented_by.iter().map(|c| call_info_to_json(c, project_root)).collect::<Vec<_>>(),
    })
}

fn call_info_to_json(
    c: &crate::query::context::CallInfo,
    project_root: &Path,
) -> serde_json::Value {
    let rel = c
        .file_path
        .strip_prefix(project_root)
        .unwrap_or(&c.file_path);
    serde_json::json!({
        "symbol_name": c.symbol_name,
        "file": rel.to_string_lossy(),
        "line": c.line,
    })
}

fn stats_to_json(
    stats: &crate::query::stats::ProjectStats,
    language_filter: Option<&str>,
) -> serde_json::Value {
    let mut obj = serde_json::Map::new();

    // Always include aggregate counts.
    obj.insert("file_count".into(), stats.file_count.into());
    obj.insert("symbol_count".into(), stats.symbol_count.into());

    let show_ts = language_filter.is_none()
        || language_filter == Some("typescript")
        || language_filter == Some("javascript");
    let show_rust = language_filter.is_none() || language_filter == Some("rust");

    if show_ts {
        obj.insert("functions".into(), stats.functions.into());
        obj.insert("classes".into(), stats.classes.into());
        obj.insert("interfaces".into(), stats.interfaces.into());
        obj.insert("type_aliases".into(), stats.type_aliases.into());
        obj.insert("enums".into(), stats.enums.into());
        obj.insert("variables".into(), stats.variables.into());
        obj.insert("components".into(), stats.components.into());
        obj.insert("methods".into(), stats.methods.into());
        obj.insert("properties".into(), stats.properties.into());
        obj.insert("import_edges".into(), stats.import_edges.into());
        obj.insert("external_packages".into(), stats.external_packages.into());
        obj.insert("unresolved_imports".into(), stats.unresolved_imports.into());
    }

    if show_rust {
        obj.insert("rust_fns".into(), stats.rust_fns.into());
        obj.insert("rust_structs".into(), stats.rust_structs.into());
        obj.insert("rust_enums".into(), stats.rust_enums.into());
        obj.insert("rust_traits".into(), stats.rust_traits.into());
        obj.insert("rust_impl_methods".into(), stats.rust_impl_methods.into());
        obj.insert("rust_type_aliases".into(), stats.rust_type_aliases.into());
        obj.insert("rust_consts".into(), stats.rust_consts.into());
        obj.insert("rust_statics".into(), stats.rust_statics.into());
        obj.insert("rust_macros".into(), stats.rust_macros.into());
    }

    serde_json::Value::Object(obj)
}

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

/// Parse a language filter string into a canonical language name.
fn parse_lang(lang: Option<&str>) -> Result<Option<&'static str>, String> {
    match lang {
        None => Ok(None),
        Some(s) => {
            use crate::language::LanguageKind;
            match LanguageKind::from_str_loose(s) {
                Some(LanguageKind::Rust) => Ok(Some("rust")),
                Some(LanguageKind::TypeScript) => Ok(Some("typescript")),
                Some(LanguageKind::JavaScript) => Ok(Some("javascript")),
                Some(LanguageKind::Python) => Ok(Some("python")),
                Some(LanguageKind::Go) => Ok(Some("go")),
                None => Err(format!(
                    "unknown language '{}'. Valid: rust/rs, typescript/ts, javascript/js, python/py, go/golang",
                    s
                )),
            }
        }
    }
}

/// Returns true if the file at `path` belongs to the given language string.
fn file_language_matches(path: &Path, lang: &str) -> bool {
    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
    match lang {
        "rust" => ext == "rs",
        "typescript" => matches!(ext, "ts" | "tsx"),
        "javascript" => matches!(ext, "js" | "jsx"),
        "python" => ext == "py",
        "go" => ext == "go",
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dispatch_ping_returns_success() {
        let graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test");
        let response = dispatch_query(&DaemonRequest::Ping, &graph, &root);
        match response {
            DaemonResponse::Success { version, data } => {
                assert_eq!(version, PROTOCOL_VERSION);
                assert_eq!(data["daemon"], "code-graph");
                assert_eq!(data["version"], PROTOCOL_VERSION);
            }
            DaemonResponse::Error { .. } => panic!("expected Success for Ping"),
        }
    }

    #[test]
    fn dispatch_stats_returns_success() {
        let graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test");
        let response = dispatch_query(&DaemonRequest::Stats { language: None }, &graph, &root);
        match response {
            DaemonResponse::Success { version, data } => {
                assert_eq!(version, PROTOCOL_VERSION);
                assert_eq!(data["file_count"], 0);
                assert_eq!(data["symbol_count"], 0);
            }
            DaemonResponse::Error { .. } => panic!("expected Success for Stats"),
        }
    }

    #[test]
    fn dispatch_find_no_results() {
        let graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test");
        let response = dispatch_query(
            &DaemonRequest::Find {
                symbol: "NonExistent".into(),
                case_insensitive: false,
                kind: vec![],
                file: None,
                language: None,
            },
            &graph,
            &root,
        );
        match response {
            DaemonResponse::Success { data, .. } => {
                assert!(data.as_array().unwrap().is_empty());
            }
            DaemonResponse::Error { .. } => panic!("expected Success (empty) for Find"),
        }
    }

    #[test]
    fn dispatch_circular_empty_graph() {
        let graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test");
        let response = dispatch_query(&DaemonRequest::Circular { language: None }, &graph, &root);
        match response {
            DaemonResponse::Success { data, .. } => {
                assert!(data.as_array().unwrap().is_empty());
            }
            DaemonResponse::Error { .. } => panic!("expected Success for Circular"),
        }
    }

    #[test]
    fn dispatch_refs_no_matches() {
        let graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test");
        let response = dispatch_query(
            &DaemonRequest::Refs {
                symbol: "Nonexistent".into(),
                case_insensitive: false,
                kind: vec![],
                file: None,
                language: None,
            },
            &graph,
            &root,
        );
        // Should be an error because no symbols match.
        match response {
            DaemonResponse::Error { message, .. } => {
                assert!(message.contains("no symbols matching"));
            }
            DaemonResponse::Success { .. } => panic!("expected Error for Refs with no matches"),
        }
    }

    #[test]
    fn dispatch_invalid_language() {
        let graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test");
        let response = dispatch_query(
            &DaemonRequest::Stats {
                language: Some("invalid_lang".into()),
            },
            &graph,
            &root,
        );
        match response {
            DaemonResponse::Error { message, .. } => {
                assert!(message.contains("unknown language"));
            }
            DaemonResponse::Success { .. } => panic!("expected Error for invalid language"),
        }
    }

    #[test]
    fn parse_lang_valid() {
        assert_eq!(parse_lang(None), Ok(None));
        assert_eq!(parse_lang(Some("rust")), Ok(Some("rust")));
        assert_eq!(parse_lang(Some("rs")), Ok(Some("rust")));
        assert_eq!(parse_lang(Some("typescript")), Ok(Some("typescript")));
        assert_eq!(parse_lang(Some("ts")), Ok(Some("typescript")));
        assert_eq!(parse_lang(Some("python")), Ok(Some("python")));
        assert_eq!(parse_lang(Some("go")), Ok(Some("go")));
    }

    #[test]
    fn parse_lang_invalid() {
        assert!(parse_lang(Some("fortran")).is_err());
    }
}