deciduous 0.15.0

Decision graph tooling for AI-assisted development. Track every goal, decision, and outcome. Survive context loss. Query your reasoning.
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
//! HTTP server for decision graph viewer
//!
//! `deciduous serve` → starts server, opens browser, shows graph

use crate::db::{Database, DecisionGraph, QaInteraction, QaSearchResult, RoadmapItem};
use serde::Serialize;
use std::collections::HashMap;
use tiny_http::{Header, Method, Request, Response, Server};

#[derive(Serialize)]
struct ApiResponse<T> {
    ok: bool,
    data: Option<T>,
    error: Option<String>,
}

impl<T: Serialize> ApiResponse<T> {
    fn success(data: T) -> Self {
        Self {
            ok: true,
            data: Some(data),
            error: None,
        }
    }
}

// Embedded React graph viewer (built with bun from web/ directory)
// To rebuild: cd web && ./build-embed.sh
const GRAPH_VIEWER_HTML: &str = include_str!("viewer.html");

/// Start the decision graph viewer server
pub fn start_graph_server(port: u16) -> std::io::Result<()> {
    let addr = format!("127.0.0.1:{}", port);
    let server = Server::http(&addr)
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

    let url = format!("http://localhost:{}", port);

    eprintln!("\n\x1b[1;32m🌳 Deciduous\x1b[0m");
    eprintln!("   Graph viewer: {}", url);
    eprintln!("   Press Ctrl+C to stop\n");

    // Handle requests
    for request in server.incoming_requests() {
        if let Err(e) = handle_request(request) {
            eprintln!("Error: {}", e);
        }
    }

    Ok(())
}

fn handle_request(request: Request) -> std::io::Result<()> {
    let url = request.url().to_string();
    let path = url.split('?').next().unwrap_or("/");
    let method = request.method().clone();

    match (&method, path) {
        // API endpoints first (before SPA fallback)
        // API: Get decision graph
        (&Method::Get, "/api/graph") => {
            let graph = get_decision_graph();
            let json = serde_json::to_string(&ApiResponse::success(graph))?;

            let response = Response::from_string(json).with_header(
                Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
            );
            request.respond(response)
        }

        // API: Get command log
        (&Method::Get, "/api/commands") => {
            let commands = get_command_log();
            let json = serde_json::to_string(&ApiResponse::success(commands))?;

            let response = Response::from_string(json).with_header(
                Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
            );
            request.respond(response)
        }

        // API: Get roadmap items
        (&Method::Get, "/api/roadmap") => {
            let items = get_roadmap_items();
            let json = serde_json::to_string(&ApiResponse::success(items))?;

            let response = Response::from_string(json).with_header(
                Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
            );
            request.respond(response)
        }

        // API: Get git history for linked commits
        (&Method::Get, "/api/git-history") => {
            let history = get_git_history();
            let json = serde_json::to_string(&ApiResponse::success(history))?;

            let response = Response::from_string(json).with_header(
                Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
            );
            request.respond(response)
        }

        // API: Toggle roadmap item checkbox (POST /api/roadmap/checkbox)
        (&Method::Post, "/api/roadmap/checkbox") => handle_toggle_checkbox(request),

        // API: Ask Claude about the code (POST /api/ask)
        (&Method::Post, "/api/ask") => handle_ask_question(request),

        // API: Search Q&A interactions (GET /api/qa/search?q=...&limit=20)
        (&Method::Get, "/api/qa/search") => handle_qa_search(request, &url),

        // API: Get paginated Q&A interactions (GET /api/qa?offset=0&limit=20)
        (&Method::Get, "/api/qa") => handle_qa_list(request, &url),

        // API: Get or delete single Q&A interaction (GET/DELETE /api/qa/:id)
        (&Method::Get, p) if p.starts_with("/api/qa/") => handle_qa_get(request, p),
        (&Method::Delete, p) if p.starts_with("/api/qa/") => handle_qa_delete(request, p),

        // API: Get documents for a node (GET /api/documents?node_id=N)
        (&Method::Get, "/api/documents") => handle_documents_list(request, &url),

        // API: Serve document file content (GET /api/documents/file/<id>)
        (&Method::Get, p) if p.starts_with("/api/documents/file/") => {
            handle_document_file(request, p)
        }

        // API: Get sync status (event-based sync)
        (&Method::Get, "/api/sync-status") => {
            let status = get_sync_status();
            let json = serde_json::to_string(&ApiResponse::success(status))?;

            let response = Response::from_string(json).with_header(
                Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
            );
            request.respond(response)
        }

        // Serve SPA for all other GET requests (client-side routing)
        (&Method::Get, _) => {
            let response = Response::from_string(GRAPH_VIEWER_HTML)
                .with_header(Header::from_bytes(&b"Content-Type"[..], &b"text/html"[..]).unwrap());
            request.respond(response)
        }

        // 404 for non-GET requests to unknown paths
        _ => {
            let response = Response::from_string("Not found").with_status_code(404);
            request.respond(response)
        }
    }
}

fn get_decision_graph() -> DecisionGraph {
    // Load config for external repo support
    let config = crate::config::Config::load();
    let include_config = config.github.commit_repo.is_some();
    let config_opt = if include_config { Some(config) } else { None };

    match Database::open() {
        Ok(db) => db
            .get_graph_with_config(config_opt.clone())
            .unwrap_or_else(|_| DecisionGraph {
                nodes: vec![],
                edges: vec![],
                config: config_opt.clone(),
                themes: vec![],
                node_themes: vec![],
                documents: vec![],
            }),
        Err(_) => DecisionGraph {
            nodes: vec![],
            edges: vec![],
            config: config_opt,
            themes: vec![],
            node_themes: vec![],
            documents: vec![],
        },
    }
}

fn get_command_log() -> Vec<crate::db::CommandLog> {
    match Database::open() {
        Ok(db) => db.get_recent_commands(100).unwrap_or_default(),
        Err(_) => vec![],
    }
}

fn get_roadmap_items() -> Vec<RoadmapItem> {
    match Database::open() {
        Ok(db) => db.get_all_roadmap_items().unwrap_or_default(),
        Err(_) => vec![],
    }
}

// === Git History Types and Functions ===

/// Git commit info for timeline view (matches web/src/types/graph.ts GitCommit)
#[derive(Serialize)]
struct GitCommit {
    hash: String,
    short_hash: String,
    author: String,
    date: String,
    message: String,
    files_changed: Option<u32>,
}

/// Get git history for all commits linked to nodes
fn get_git_history() -> Vec<GitCommit> {
    use std::collections::HashSet;

    eprintln!("[git-history] Starting get_git_history");
    eprintln!("[git-history] Current dir: {:?}", std::env::current_dir());

    // Get all nodes from database
    let nodes = match Database::open() {
        Ok(db) => db.get_all_nodes().unwrap_or_default(),
        Err(e) => {
            eprintln!("[git-history] Database error: {:?}", e);
            return vec![];
        }
    };
    eprintln!("[git-history] Got {} nodes from database", nodes.len());

    // Find git repo root by looking for .git directory
    // Start from current dir and walk up
    let repo_root = find_git_repo_root();
    eprintln!("[git-history] Git repo root: {:?}", repo_root);

    // Extract unique commit hashes from node metadata
    let mut hashes = HashSet::new();
    for node in &nodes {
        if let Some(ref meta_json) = node.metadata_json {
            if let Ok(meta) = serde_json::from_str::<serde_json::Value>(meta_json) {
                if let Some(commit) = meta.get("commit").and_then(|c| c.as_str()) {
                    if !commit.is_empty() {
                        hashes.insert(commit.to_string());
                    }
                }
            }
        }
    }
    eprintln!("[git-history] Found {} unique commit hashes", hashes.len());

    // Get commit info from git for each hash
    let mut commits: Vec<GitCommit> = Vec::new();
    let mut failed = 0;
    for hash in &hashes {
        if let Some(commit) = get_git_commit_info(hash, repo_root.as_deref()) {
            commits.push(commit);
        } else {
            failed += 1;
        }
    }
    eprintln!(
        "[git-history] Got {} commits, {} failed lookups",
        commits.len(),
        failed
    );

    // Sort by date (newest first)
    commits.sort_by(|a, b| b.date.cmp(&a.date));
    commits
}

/// Find git repository root by looking for .deciduous folder (same as db.rs)
/// The .deciduous folder is in the project root, which also has .git
/// Sync status for the web viewer
#[derive(Serialize)]
struct SyncStatus {
    initialized: bool,
    pending_events: usize,
    events_by_author: HashMap<String, usize>,
    checkpoint_time: Option<String>,
    checkpoint_nodes: Option<usize>,
    checkpoint_edges: Option<usize>,
}

fn get_sync_status() -> SyncStatus {
    let deciduous_dir = std::path::PathBuf::from(".deciduous");
    let sync_dir = deciduous_dir.join("sync");

    if !sync_dir.exists() {
        return SyncStatus {
            initialized: false,
            pending_events: 0,
            events_by_author: HashMap::new(),
            checkpoint_time: None,
            checkpoint_nodes: None,
            checkpoint_edges: None,
        };
    }

    let author = crate::events::get_current_author();
    let event_log = match crate::events::EventLog::new(&deciduous_dir, author) {
        Ok(log) => log,
        Err(_) => {
            return SyncStatus {
                initialized: true,
                pending_events: 0,
                events_by_author: HashMap::new(),
                checkpoint_time: None,
                checkpoint_nodes: None,
                checkpoint_edges: None,
            };
        }
    };

    // Get events after checkpoint
    let events = event_log.get_events_after_checkpoint().unwrap_or_default();
    let pending_events = events.len();

    // Count by author
    let mut events_by_author: HashMap<String, usize> = HashMap::new();
    for event in &events {
        *events_by_author
            .entry(event.author().to_string())
            .or_default() += 1;
    }

    // Get checkpoint info
    let (checkpoint_time, checkpoint_nodes, checkpoint_edges) = match event_log.load_checkpoint() {
        Ok(Some(cp)) => (
            Some(cp.created_at.to_rfc3339()),
            Some(cp.nodes.len()),
            Some(cp.edges.len()),
        ),
        _ => (None, None, None),
    };

    SyncStatus {
        initialized: true,
        pending_events,
        events_by_author,
        checkpoint_time,
        checkpoint_nodes,
        checkpoint_edges,
    }
}

fn find_git_repo_root() -> Option<std::path::PathBuf> {
    let current_dir = std::env::current_dir().ok()?;
    let mut dir = current_dir.as_path();
    loop {
        // Look for .deciduous - that's the project root
        let deciduous_dir = dir.join(".deciduous");
        if deciduous_dir.exists() && deciduous_dir.is_dir() {
            return Some(dir.to_path_buf());
        }
        dir = dir.parent()?;
    }
}

/// Get commit info from git for a given hash
fn get_git_commit_info(hash: &str, repo_root: Option<&std::path::Path>) -> Option<GitCommit> {
    use std::process::Command;

    // Get commit info: hash, author, date (ISO), full message body
    // Use %x00 (null byte) as separator since message can have newlines
    let mut cmd = Command::new("git");
    if let Some(root) = repo_root {
        cmd.current_dir(root);
    }
    let output = cmd
        .args(["log", "-1", "--format=%H%x00%an%x00%aI%x00%B", hash])
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let parts: Vec<&str> = stdout.trim().split('\x00').collect();
    if parts.len() < 4 {
        return None;
    }

    // Clean up the message - trim whitespace
    let message = parts[3].trim().to_string();

    // Get files changed count
    let mut files_cmd = Command::new("git");
    if let Some(root) = repo_root {
        files_cmd.current_dir(root);
    }
    let files_output = files_cmd
        .args(["diff-tree", "--no-commit-id", "--name-only", "-r", hash])
        .output()
        .ok();

    let files_changed = files_output.and_then(|o| {
        if o.status.success() {
            let count = String::from_utf8_lossy(&o.stdout).trim().lines().count();
            Some(count as u32)
        } else {
            None
        }
    });

    Some(GitCommit {
        hash: parts[0].to_string(),
        short_hash: parts[0].chars().take(7).collect(),
        author: parts[1].to_string(),
        date: parts[2].to_string(),
        message,
        files_changed,
    })
}

#[derive(serde::Deserialize)]
struct ToggleCheckboxRequest {
    item_id: i32,
    checkbox_state: String,
}

// === Ask Question Types ===

#[derive(serde::Deserialize)]
struct AskRequest {
    question: String,
    context: Option<AskContext>,
}

#[derive(serde::Deserialize, serde::Serialize)]
struct AskContext {
    selected_node_id: Option<i32>,
    visible_node_ids: Option<Vec<i32>>,
    current_branch: Option<String>,
    narrative: Option<NarrativeContext>,
}

/// Narrative context from archaeology view
#[derive(serde::Deserialize, serde::Serialize)]
struct NarrativeContext {
    name: String,
    root_id: i32,
    #[serde(default)]
    node_ids: Vec<i32>,
    #[serde(default)]
    pivots: Vec<PivotContext>,
    #[serde(default)]
    github_links: Vec<GithubLinkContext>,
}

/// Pivot context - where an approach changed
#[derive(serde::Deserialize, serde::Serialize)]
struct PivotContext {
    revisit_id: i32,
    observation_ids: Vec<i32>,
    superseded_ids: Vec<i32>,
    new_approach_ids: Vec<i32>,
}

/// GitHub link context
#[derive(serde::Deserialize, serde::Serialize)]
struct GithubLinkContext {
    #[serde(rename = "type")]
    link_type: String,
    identifier: String,
    repo: String,
}

#[derive(serde::Serialize)]
struct AskResponse {
    answer: String,
}

fn handle_ask_question(mut request: Request) -> std::io::Result<()> {
    use std::io::Write;
    use std::process::{Command, Stdio};

    // Read request body
    let mut body = String::new();
    if let Err(e) = request.as_reader().read_to_string(&mut body) {
        let json = serde_json::to_string(&ApiResponse::<()> {
            ok: false,
            data: None,
            error: Some(format!("Failed to read body: {}", e)),
        })?;
        let response = Response::from_string(json)
            .with_status_code(400)
            .with_header(
                Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
            );
        return request.respond(response);
    }

    // Parse JSON body
    let req: AskRequest = match serde_json::from_str(&body) {
        Ok(r) => r,
        Err(e) => {
            let json = serde_json::to_string(&ApiResponse::<()> {
                ok: false,
                data: None,
                error: Some(format!("Invalid JSON: {}", e)),
            })?;
            let response = Response::from_string(json)
                .with_status_code(400)
                .with_header(
                    Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
                );
            return request.respond(response);
        }
    };

    // Build the prompt with context
    let prompt = build_claude_prompt(&req);

    // Execute claude -p command, piping prompt via stdin for reliability
    let mut child = match Command::new("claude")
        .arg("-p")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
    {
        Ok(child) => child,
        Err(e) => {
            let error = if e.kind() == std::io::ErrorKind::NotFound {
                "Claude CLI not found. Install with: npm install -g @anthropic-ai/claude-code"
                    .to_string()
            } else {
                format!("Failed to spawn claude: {}", e)
            };
            let json = serde_json::to_string(&ApiResponse::<AskResponse> {
                ok: false,
                data: None,
                error: Some(error),
            })?;
            let response = Response::from_string(json)
                .with_status_code(500)
                .with_header(
                    Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
                );
            return request.respond(response);
        }
    };

    // Write prompt to stdin
    if let Some(mut stdin) = child.stdin.take() {
        let _ = stdin.write_all(prompt.as_bytes());
    }

    // Wait for output
    let output = child.wait_with_output();

    let (json, status) = match output {
        Ok(output) => {
            if output.status.success() {
                let answer = String::from_utf8_lossy(&output.stdout).to_string();

                // Save Q&A interaction to database (best effort - don't fail if DB unavailable)
                if let Ok(db) = Database::open() {
                    let context_json = req
                        .context
                        .as_ref()
                        .and_then(|ctx| serde_json::to_string(ctx).ok());
                    if let Err(e) = db.save_qa_interaction(
                        &req.question,
                        &prompt,
                        &answer,
                        context_json.as_deref(),
                    ) {
                        eprintln!("Warning: Failed to save Q&A interaction: {}", e);
                    }
                }

                (
                    serde_json::to_string(&ApiResponse::success(AskResponse {
                        answer: answer.clone(),
                    }))?,
                    200,
                )
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr).to_string();
                let error = if stderr.is_empty() {
                    "Claude command failed with no error message".to_string()
                } else {
                    stderr
                };
                (
                    serde_json::to_string(&ApiResponse::<AskResponse> {
                        ok: false,
                        data: None,
                        error: Some(error),
                    })?,
                    500,
                )
            }
        }
        Err(e) => {
            let error = if e.kind() == std::io::ErrorKind::NotFound {
                "Claude CLI not found. Install with: npm install -g @anthropic-ai/claude-code"
                    .to_string()
            } else {
                format!("Failed to execute claude: {}", e)
            };
            (
                serde_json::to_string(&ApiResponse::<AskResponse> {
                    ok: false,
                    data: None,
                    error: Some(error),
                })?,
                500,
            )
        }
    };

    let response = Response::from_string(json)
        .with_status_code(status)
        .with_header(Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap());
    request.respond(response)
}

fn build_claude_prompt(req: &AskRequest) -> String {
    let mut prompt = String::new();

    // Check if this is an archaeology query (has narrative context)
    let is_archaeology = req
        .context
        .as_ref()
        .is_some_and(|ctx| ctx.narrative.is_some());

    if is_archaeology {
        // Archaeology-focused system prompt
        prompt.push_str(r#"You are an expert in this codebase. You have meticulously crafted a narrative graph that lets someone understand and explore the entire decision history you have built up in order to understand the *why* of certain pieces of code. This was built using your archaeology tool.

The narrative graph captures:
- **Goals**: What we set out to accomplish
- **Decisions**: Choices made along the way
- **Actions**: Implementation steps taken
- **Observations**: What we learned during the process
- **Pivots**: Where we changed approach based on new information
- **Outcomes**: Results of our decisions

When answering questions:
1. Ground your answers in the specific nodes and decisions from the narrative
2. Explain the *why* behind decisions, not just the *what*
3. Highlight pivots and course corrections - these often contain the most valuable insights
4. Reference GitHub artifacts (commits, PRs, issues) when relevant
5. If the narrative doesn't contain enough information, say so explicitly

Format your response in Markdown.

---

"#);
    } else {
        // Generic decision graph system prompt
        prompt.push_str(r#"You are to use deciduous to answer questions about the codebase and decision graph.

You can use your skills/tools/commands to query the graph with the various deciduous helpers.

You can also use SQLite directly to query the graph database at .deciduous/deciduous.db to answer questions and traverse relationships.

The schema has these tables:
- decision_nodes (id, node_type, title, description, status, created_at, updated_at, metadata_json, change_id)
  - metadata_json may contain: branch, commit, files, confidence, prompt
- decision_edges (id, from_node_id, to_node_id, edge_type, weight, rationale, created_at, from_change_id, to_change_id)

Example queries:
- List all nodes: SELECT id, title, node_type, status FROM decision_nodes;
- Find commits: SELECT id, title, json_extract(metadata_json, '$.commit') as commit FROM decision_nodes WHERE metadata_json LIKE '%commit%';
- Get edges for a node: SELECT e.*, n.title FROM decision_edges e JOIN decision_nodes n ON e.to_node_id = n.id WHERE e.from_node_id = ?;

Make sure to be detailed in your response, and format it in markdown.

IMPORTANT: If information is missing or incomplete, tell the user explicitly and suggest a prompt they could give Claude to fill in that information using public sources or codebase exploration.

---

"#);
    }

    // Add context if provided
    if let Some(ctx) = &req.context {
        prompt.push_str("Context from deciduous decision graph:\n\n");

        // Add selected node context
        if let Some(node_id) = ctx.selected_node_id {
            if let Ok(db) = Database::open() {
                if let Ok(Some(node)) = db.get_node(node_id) {
                    prompt.push_str(&format!(
                        "Currently viewing node #{}: \"{}\" ({})\n",
                        node.id, node.title, node.node_type
                    ));
                    if let Some(desc) = &node.description {
                        prompt.push_str(&format!("Description: {}\n", desc));
                    }
                    // Parse metadata for additional context
                    if let Some(meta_str) = &node.metadata_json {
                        if let Ok(meta) = serde_json::from_str::<serde_json::Value>(meta_str) {
                            if let Some(branch) = meta.get("branch").and_then(|v| v.as_str()) {
                                prompt.push_str(&format!("Branch: {}\n", branch));
                            }
                            if let Some(prompt_text) = meta.get("prompt").and_then(|v| v.as_str()) {
                                prompt.push_str(&format!("Original prompt: {}\n", prompt_text));
                            }
                        }
                    }
                    prompt.push('\n');
                }
            }
        }

        // Add visible nodes count
        if let Some(visible_ids) = &ctx.visible_node_ids {
            prompt.push_str(&format!(
                "User is viewing {} nodes in the graph.\n\n",
                visible_ids.len()
            ));
        }

        // Add branch context
        if let Some(branch) = &ctx.current_branch {
            prompt.push_str(&format!("Current git branch: {}\n\n", branch));
        }

        // Add narrative context from archaeology view
        if let Some(narrative) = &ctx.narrative {
            prompt.push_str("## Narrative Context (Archaeology View)\n\n");
            prompt.push_str(&format!("**Narrative:** {}\n", narrative.name));
            prompt.push_str(&format!(
                "**Scope:** {} nodes, starting from node #{}\n\n",
                narrative.node_ids.len(),
                narrative.root_id
            ));

            // Load and display nodes in the narrative
            if let Ok(db) = Database::open() {
                prompt.push_str("### Nodes in this narrative:\n\n");
                for node_id in &narrative.node_ids {
                    if let Ok(Some(node)) = db.get_node(*node_id) {
                        let status_marker = match node.status.as_str() {
                            "superseded" => " [SUPERSEDED]",
                            "abandoned" => " [ABANDONED]",
                            _ => "",
                        };
                        prompt.push_str(&format!(
                            "- **#{}** ({}{}) {}\n",
                            node.id, node.node_type, status_marker, node.title
                        ));
                        if let Some(desc) = &node.description {
                            if !desc.is_empty() {
                                prompt.push_str(&format!("  - {}\n", desc));
                            }
                        }
                    }
                }
                prompt.push('\n');
            }

            // Add pivots - where the approach changed
            if !narrative.pivots.is_empty() {
                prompt.push_str("### Pivots (approach changes):\n\n");
                for (i, pivot) in narrative.pivots.iter().enumerate() {
                    prompt.push_str(&format!(
                        "**Pivot {}:** Revisit node #{}\n",
                        i + 1,
                        pivot.revisit_id
                    ));
                    if !pivot.observation_ids.is_empty() {
                        prompt.push_str(&format!(
                            "- Triggered by observations: {}\n",
                            pivot
                                .observation_ids
                                .iter()
                                .map(|id| format!("#{}", id))
                                .collect::<Vec<_>>()
                                .join(", ")
                        ));
                    }
                    if !pivot.superseded_ids.is_empty() {
                        prompt.push_str(&format!(
                            "- Superseded nodes: {}\n",
                            pivot
                                .superseded_ids
                                .iter()
                                .map(|id| format!("#{}", id))
                                .collect::<Vec<_>>()
                                .join(", ")
                        ));
                    }
                    if !pivot.new_approach_ids.is_empty() {
                        prompt.push_str(&format!(
                            "- New approach nodes: {}\n",
                            pivot
                                .new_approach_ids
                                .iter()
                                .map(|id| format!("#{}", id))
                                .collect::<Vec<_>>()
                                .join(", ")
                        ));
                    }
                    prompt.push('\n');
                }
            }

            // Add GitHub links
            if !narrative.github_links.is_empty() {
                prompt.push_str("### GitHub Artifacts:\n\n");
                for link in &narrative.github_links {
                    let link_desc = match link.link_type.as_str() {
                        "commit" => format!(
                            "Commit {}",
                            &link.identifier[..7.min(link.identifier.len())]
                        ),
                        "pr" => format!("PR #{}", link.identifier),
                        "issue" => format!("Issue #{}", link.identifier),
                        _ => format!("{} {}", link.link_type, link.identifier),
                    };
                    prompt.push_str(&format!("- {} ({})\n", link_desc, link.repo));
                }
                prompt.push('\n');
            }

            prompt.push_str("---\n\n");
        }
    }

    // Add the user's question
    prompt.push_str("User question: ");
    prompt.push_str(&req.question);

    prompt
}

// === Toggle Checkbox Types ===

fn handle_toggle_checkbox(mut request: Request) -> std::io::Result<()> {
    // Read request body
    let mut body = String::new();
    if let Err(e) = request.as_reader().read_to_string(&mut body) {
        let json = serde_json::to_string(&ApiResponse::<()> {
            ok: false,
            data: None,
            error: Some(format!("Failed to read body: {}", e)),
        })?;
        let response = Response::from_string(json)
            .with_status_code(400)
            .with_header(
                Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
            );
        return request.respond(response);
    }

    // Parse JSON body
    let req: ToggleCheckboxRequest = match serde_json::from_str(&body) {
        Ok(r) => r,
        Err(e) => {
            let json = serde_json::to_string(&ApiResponse::<()> {
                ok: false,
                data: None,
                error: Some(format!("Invalid JSON: {}", e)),
            })?;
            let response = Response::from_string(json)
                .with_status_code(400)
                .with_header(
                    Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
                );
            return request.respond(response);
        }
    };

    // Update database
    let result = match Database::open() {
        Ok(db) => db.update_roadmap_item_checkbox(req.item_id, &req.checkbox_state),
        Err(e) => Err(e),
    };

    let (json, status) = match result {
        Ok(()) => (serde_json::to_string(&ApiResponse::success(true))?, 200),
        Err(e) => (
            serde_json::to_string(&ApiResponse::<bool> {
                ok: false,
                data: None,
                error: Some(format!("Database error: {}", e)),
            })?,
            500,
        ),
    };

    let response = Response::from_string(json)
        .with_status_code(status)
        .with_header(Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap());
    request.respond(response)
}

// === Q&A API Handlers ===

/// Parse query parameters from URL (e.g., "?q=test&limit=20")
fn parse_query_params(url: &str) -> HashMap<String, String> {
    let mut params = HashMap::new();
    if let Some(query_start) = url.find('?') {
        let query = &url[query_start + 1..];
        for pair in query.split('&') {
            if let Some(eq_pos) = pair.find('=') {
                let key = &pair[..eq_pos];
                let value = &pair[eq_pos + 1..];
                // URL decode the value (basic: just handle %20 for spaces)
                let decoded = value.replace("%20", " ").replace("+", " ");
                params.insert(key.to_string(), decoded);
            }
        }
    }
    params
}

/// Response wrapper for paginated Q&A list
#[derive(Serialize)]
struct QaListResponse {
    items: Vec<QaInteraction>,
    total: i64,
}

fn handle_qa_search(request: Request, url: &str) -> std::io::Result<()> {
    let params = parse_query_params(url);
    let query = params.get("q").map(|s| s.as_str()).unwrap_or("");
    let limit: i32 = params
        .get("limit")
        .and_then(|s| s.parse().ok())
        .unwrap_or(20);

    if query.is_empty() {
        let json = serde_json::to_string(&ApiResponse::<Vec<QaSearchResult>> {
            ok: false,
            data: None,
            error: Some("Missing search query parameter 'q'".to_string()),
        })?;
        let response = Response::from_string(json)
            .with_status_code(400)
            .with_header(
                Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
            );
        return request.respond(response);
    }

    let results = match Database::open() {
        Ok(db) => db.search_qa_interactions(query, limit).unwrap_or_default(),
        Err(_) => vec![],
    };

    let json = serde_json::to_string(&ApiResponse::success(results))?;
    let response = Response::from_string(json)
        .with_header(Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap());
    request.respond(response)
}

fn handle_qa_list(request: Request, url: &str) -> std::io::Result<()> {
    let params = parse_query_params(url);
    let offset: i64 = params
        .get("offset")
        .and_then(|s| s.parse().ok())
        .unwrap_or(0);
    let limit: i64 = params
        .get("limit")
        .and_then(|s| s.parse().ok())
        .unwrap_or(20);

    let (items, total) = match Database::open() {
        Ok(db) => {
            let items = db
                .get_qa_interactions_paginated(offset, limit)
                .unwrap_or_default();
            let total = db.count_qa_interactions().unwrap_or(0);
            (items, total)
        }
        Err(_) => (vec![], 0),
    };

    let json = serde_json::to_string(&ApiResponse::success(QaListResponse { items, total }))?;
    let response = Response::from_string(json)
        .with_header(Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap());
    request.respond(response)
}

fn handle_qa_get(request: Request, path: &str) -> std::io::Result<()> {
    // Extract ID from path: /api/qa/123 -> 123
    let id: i32 = match path.strip_prefix("/api/qa/").and_then(|s| s.parse().ok()) {
        Some(id) => id,
        None => {
            let json = serde_json::to_string(&ApiResponse::<QaInteraction> {
                ok: false,
                data: None,
                error: Some("Invalid Q&A ID".to_string()),
            })?;
            let response = Response::from_string(json)
                .with_status_code(400)
                .with_header(
                    Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
                );
            return request.respond(response);
        }
    };

    let result = match Database::open() {
        Ok(db) => db.get_qa_interaction(id),
        Err(e) => Err(e),
    };

    match result {
        Ok(Some(interaction)) => {
            let json = serde_json::to_string(&ApiResponse::success(interaction))?;
            let response = Response::from_string(json).with_header(
                Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
            );
            request.respond(response)
        }
        Ok(None) => {
            let json = serde_json::to_string(&ApiResponse::<QaInteraction> {
                ok: false,
                data: None,
                error: Some("Q&A interaction not found".to_string()),
            })?;
            let response = Response::from_string(json)
                .with_status_code(404)
                .with_header(
                    Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
                );
            request.respond(response)
        }
        Err(e) => {
            let json = serde_json::to_string(&ApiResponse::<QaInteraction> {
                ok: false,
                data: None,
                error: Some(format!("Database error: {}", e)),
            })?;
            let response = Response::from_string(json)
                .with_status_code(500)
                .with_header(
                    Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
                );
            request.respond(response)
        }
    }
}

fn handle_qa_delete(request: Request, path: &str) -> std::io::Result<()> {
    // Extract ID from path: /api/qa/123 -> 123
    let id: i32 = match path.strip_prefix("/api/qa/").and_then(|s| s.parse().ok()) {
        Some(id) => id,
        None => {
            let json = serde_json::to_string(&ApiResponse::<bool> {
                ok: false,
                data: None,
                error: Some("Invalid Q&A ID".to_string()),
            })?;
            let response = Response::from_string(json)
                .with_status_code(400)
                .with_header(
                    Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap(),
                );
            return request.respond(response);
        }
    };

    let result = match Database::open() {
        Ok(db) => db.soft_delete_qa_interaction(id),
        Err(e) => Err(e),
    };

    let (json, status) = match result {
        Ok(()) => (serde_json::to_string(&ApiResponse::success(true))?, 200),
        Err(e) => (
            serde_json::to_string(&ApiResponse::<bool> {
                ok: false,
                data: None,
                error: Some(format!("Database error: {}", e)),
            })?,
            500,
        ),
    };

    let response = Response::from_string(json)
        .with_status_code(status)
        .with_header(Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap());
    request.respond(response)
}

// === Document API Handlers ===

fn handle_documents_list(request: Request, url: &str) -> std::io::Result<()> {
    let params = parse_query_params(url);
    let node_id: Option<i32> = params.get("node_id").and_then(|s| s.parse().ok());

    let documents = match Database::open() {
        Ok(db) => db.get_node_documents(node_id, false).unwrap_or_default(),
        Err(_) => vec![],
    };

    let json = serde_json::to_string(&ApiResponse::success(documents))?;
    let response = Response::from_string(json)
        .with_header(Header::from_bytes(&b"Content-Type"[..], &b"application/json"[..]).unwrap());
    request.respond(response)
}

fn handle_document_file(request: Request, path: &str) -> std::io::Result<()> {
    let doc_id: i32 = match path
        .strip_prefix("/api/documents/file/")
        .and_then(|s| s.parse().ok())
    {
        Some(id) => id,
        None => {
            let response = Response::from_string("Invalid document ID").with_status_code(400);
            return request.respond(response);
        }
    };

    let db = match Database::open() {
        Ok(db) => db,
        Err(_) => {
            let response = Response::from_string("Database error").with_status_code(500);
            return request.respond(response);
        }
    };

    let doc = match db.get_document(doc_id) {
        Ok(Some(d)) => d,
        Ok(None) => {
            let response = Response::from_string("Document not found").with_status_code(404);
            return request.respond(response);
        }
        Err(_) => {
            let response = Response::from_string("Database error").with_status_code(500);
            return request.respond(response);
        }
    };

    // Find the file in .deciduous/documents/
    let file_path = std::path::PathBuf::from(".deciduous")
        .join("documents")
        .join(&doc.storage_filename);

    if !file_path.exists() {
        let response = Response::from_string("File not found on disk").with_status_code(404);
        return request.respond(response);
    }

    let file_data = match std::fs::read(&file_path) {
        Ok(data) => data,
        Err(_) => {
            let response = Response::from_string("Failed to read file").with_status_code(500);
            return request.respond(response);
        }
    };

    let content_type = doc.mime_type.as_str();
    let response = Response::from_data(file_data)
        .with_header(Header::from_bytes(&b"Content-Type"[..], content_type.as_bytes()).unwrap())
        .with_header(
            Header::from_bytes(
                &b"Content-Disposition"[..],
                format!("inline; filename=\"{}\"", doc.original_filename).as_bytes(),
            )
            .unwrap(),
        );
    request.respond(response)
}

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

    // === ApiResponse Tests ===

    #[test]
    fn test_api_response_success() {
        let response: ApiResponse<String> = ApiResponse::success("hello".to_string());
        assert!(response.ok);
        assert_eq!(response.data, Some("hello".to_string()));
        assert!(response.error.is_none());
    }

    #[test]
    fn test_api_response_success_with_vec() {
        let data = vec![1, 2, 3];
        let response: ApiResponse<Vec<i32>> = ApiResponse::success(data.clone());
        assert!(response.ok);
        assert_eq!(response.data, Some(data));
    }

    #[test]
    fn test_api_response_serializes_to_json() {
        let response: ApiResponse<String> = ApiResponse::success("test".to_string());
        let json = serde_json::to_string(&response).unwrap();

        assert!(json.contains("\"ok\":true"));
        assert!(json.contains("\"data\":\"test\""));
        assert!(json.contains("\"error\":null"));
    }

    #[test]
    fn test_api_response_with_complex_data() {
        #[derive(Serialize, PartialEq, Debug)]
        struct TestData {
            name: String,
            count: u32,
        }

        let data = TestData {
            name: "test".to_string(),
            count: 42,
        };
        let response = ApiResponse::success(data);

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("\"name\":\"test\""));
        assert!(json.contains("\"count\":42"));
    }

    // === Graph Viewer HTML Tests ===

    #[test]
    fn test_viewer_html_is_valid() {
        // The embedded viewer should be valid HTML
        assert!(
            GRAPH_VIEWER_HTML.contains("<!DOCTYPE html>") || GRAPH_VIEWER_HTML.contains("<html")
        );
        assert!(GRAPH_VIEWER_HTML.contains("</html>"));
    }

    #[test]
    fn test_viewer_html_has_react() {
        // The embedded viewer should have React components
        assert!(
            GRAPH_VIEWER_HTML.contains("React") || GRAPH_VIEWER_HTML.contains("react"),
            "Viewer should include React"
        );
    }
}