dbmd-cli 0.13.2

The `dbmd` command-line tool for db.md, the open standard for databases in plain files. A thin wrapper over dbmd-core: validate, search, query, graph, write, index, and log over a db.md store. Zero AI dependencies.
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
// SPDX-License-Identifier: Apache-2.0

//! End-to-end tests for the embedded harness (`dbmd ask` / `do` / `build`):
//! the real binary driven against a scripted fake LLM endpoint — the same
//! zero-dev-dependency `TcpListener` pattern as `link_verbs.rs`'s MockHub.
//! LLM text is nondeterministic in production but fully scripted here, so
//! assertions follow the repo's snapshot discipline: assert the REQUESTS the
//! adapter produced (tools present, results round-tripped) and the FINAL
//! STORE STATE, never prose.

mod common;

use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpListener;
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;

use common::{dbmd, write_db_md};

/// One captured request to the fake endpoint.
#[derive(Debug, Clone)]
struct Captured {
    path: String,
    headers: Vec<(String, String)>,
    body: String,
}

impl Captured {
    fn header(&self, name: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|(n, _)| n == name)
            .map(|(_, v)| v.as_str())
    }

    fn body_json(&self) -> serde_json::Value {
        serde_json::from_str(&self.body).expect("request body is JSON")
    }
}

/// A scripted fake LLM endpoint: SSE responses served strictly in order, one
/// connection each; `finish()` joins — which asserts every scripted response
/// was consumed — and returns the captured requests.
struct MockLlm {
    url: String,
    requests: Arc<Mutex<Vec<Captured>>>,
    handle: Option<JoinHandle<()>>,
}

impl MockLlm {
    fn serve(responses: Vec<String>) -> Self {
        Self::serve_with_status(responses.into_iter().map(|body| (200, body)).collect())
    }

    /// Script the status codes too, so a provider that refuses a field can be
    /// reproduced exactly. Every response still drains its request first —
    /// writing before the body is read gives ECONNRESET on Linux.
    fn serve_with_status(responses: Vec<(u16, String)>) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind mock llm");
        let url = format!("http://{}", listener.local_addr().expect("mock addr"));
        let requests: Arc<Mutex<Vec<Captured>>> = Arc::default();
        let captured = Arc::clone(&requests);
        let handle = std::thread::spawn(move || {
            for (status, sse_body) in responses {
                let (mut stream, _) = listener.accept().expect("accept");
                let mut reader = BufReader::new(stream.try_clone().expect("clone stream"));
                let mut line = String::new();
                reader.read_line(&mut line).expect("request line");
                let path = line.split_whitespace().nth(1).unwrap_or("").to_string();
                let mut headers = Vec::new();
                let mut length = 0usize;
                loop {
                    let mut header = String::new();
                    reader.read_line(&mut header).expect("header line");
                    let header = header.trim_end();
                    if header.is_empty() {
                        break;
                    }
                    if let Some((name, value)) = header.split_once(':') {
                        let name = name.trim().to_ascii_lowercase();
                        let value = value.trim().to_string();
                        if name == "content-length" {
                            length = value.parse().unwrap_or(0);
                        }
                        headers.push((name, value));
                    }
                }
                let mut body = vec![0u8; length];
                reader.read_exact(&mut body).expect("request body");
                captured.lock().expect("capture lock").push(Captured {
                    path,
                    headers,
                    body: String::from_utf8_lossy(&body).into_owned(),
                });
                let content_type = if status == 200 {
                    "text/event-stream"
                } else {
                    "application/json"
                };
                let head = format!(
                    "HTTP/1.1 {status} X\r\ncontent-type: {content_type}\r\ncontent-length: {}\r\nconnection: close\r\n\r\n",
                    sse_body.len()
                );
                stream.write_all(head.as_bytes()).expect("response head");
                stream
                    .write_all(sse_body.as_bytes())
                    .expect("response body");
            }
        });
        Self {
            url,
            requests,
            handle: Some(handle),
        }
    }

    fn finish(mut self) -> Vec<Captured> {
        if let Some(handle) = self.handle.take() {
            handle.join().expect("mock served every scripted response");
        }
        let requests = self.requests.lock().expect("capture lock").clone();
        requests
    }
}

/// SSE frames from `data:` payload lines.
fn sse(frames: &[&str]) -> String {
    let mut body = String::new();
    for frame in frames {
        body.push_str("data: ");
        body.push_str(frame);
        body.push_str("\n\n");
    }
    body
}

/// A scratch store with one seeded todo record.
fn seeded_store() -> (tempfile::TempDir, std::path::PathBuf) {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let store = tmp.path().join("store");
    std::fs::create_dir_all(&store).expect("store dir");
    write_db_md(&store);
    let seed = dbmd()
        .current_dir(&store)
        .args([
            "write",
            "records/todos/buy-milk.md",
            "--type",
            "todo",
            "--summary",
            "Buy milk",
            "--fm",
            "status=active",
        ])
        .assert();
    seed.success();
    (tmp, store)
}

/// A `dbmd` command with a hermetic harness environment pointed at the mock.
fn harness_cmd(store: &std::path::Path, mock_url: &str, protocol: &str) -> assert_cmd::Command {
    let mut command = dbmd();
    command.current_dir(store);
    for var in [
        "DBMD_LLM_PROVIDER",
        "DBMD_LLM_BASE_URL",
        "DBMD_LLM_PROTOCOL",
        "DBMD_LLM_MODEL",
        "DBMD_LLM_KEY",
        "DBMD_LLM_KEY_ORIGIN",
        "DBMD_LLM_ALLOW_INSECURE_HTTP",
        "DBMD_LLM_EFFORT",
        "DBMD_WORKSPACE",
    ] {
        command.env_remove(var);
    }
    let base = if protocol == "openai" {
        format!("{mock_url}/v1")
    } else {
        mock_url.to_string()
    };
    command
        .env("DBMD_LLM_BASE_URL", base)
        .env("DBMD_LLM_PROTOCOL", protocol)
        .env("DBMD_LLM_MODEL", "fake-model");
    command
}

/// NDJSON events parsed from a `--json` run's stdout.
fn events(stdout: &[u8]) -> Vec<serde_json::Value> {
    String::from_utf8_lossy(stdout)
        .lines()
        .filter_map(|line| serde_json::from_str(line).ok())
        .collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// openai-compat: the full loop
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn ask_runs_one_tool_loop_and_answers() {
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve(vec![
        sse(&[
            r#"{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"name":"query","arguments":"{\"type\":\"todo\"}"}}]}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#,
            "[DONE]",
        ]),
        sse(&[
            r#"{"choices":[{"index":0,"delta":{"content":"One active todo: Buy milk."}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#,
            r#"{"choices":[],"usage":{"prompt_tokens":11,"completion_tokens":7}}"#,
            "[DONE]",
        ]),
    ]);

    let assert = harness_cmd(&store, &mock.url, "openai")
        .args(["--json", "ask", "what is on the list?"])
        .assert()
        .success();
    let stdout = assert.get_output().stdout.clone();

    let requests = mock.finish();
    assert_eq!(requests.len(), 2, "one tool round-trip");
    assert_eq!(requests[0].path, "/v1/chat/completions");

    // First request: system prompt + tools + no auth header (no key set).
    let first = requests[0].body_json();
    assert_eq!(first["model"], "fake-model");
    assert_eq!(first["messages"][0]["role"], "system");
    let tools: Vec<&str> = first["tools"]
        .as_array()
        .expect("tools")
        .iter()
        .filter_map(|t| t["function"]["name"].as_str())
        .collect();
    assert!(tools.contains(&"query"));
    assert!(!tools.contains(&"write"), "read mask has no write tools");
    assert!(requests[0].header("authorization").is_none());

    // Second request: the tool result rode back with the call id.
    let second = requests[1].body_json();
    let wire = second["messages"].as_array().expect("messages");
    let tool_msg = wire
        .iter()
        .find(|m| m["role"] == "tool")
        .expect("tool result message");
    assert_eq!(tool_msg["tool_call_id"], "call_1");
    assert!(
        tool_msg["content"]
            .as_str()
            .expect("content")
            .contains("Buy milk"),
        "the query result carried the seeded record"
    );

    // Event stream: tool_call with its CLI one-liner, result, usage, done.
    let events = events(&stdout);
    let call = events
        .iter()
        .find(|e| e["event"] == "tool_call")
        .expect("tool_call event");
    assert!(call["display"]
        .as_str()
        .expect("display")
        .starts_with("dbmd --json query"));
    assert!(events.iter().any(|e| e["event"] == "tool_result"));
    assert!(events
        .iter()
        .any(|e| e["event"] == "usage" && e["input"] == 11));
    let done = events.iter().find(|e| e["event"] == "done").expect("done");
    assert_eq!(done["text"], "One active todo: Buy milk.");
}

#[test]
fn ollama_quirks_are_tolerated() {
    // Whole-args tool calls, index stuck at 0 for BOTH calls, no ids,
    // finish_reason "stop" despite the calls, plus an SSE comment line.
    let (_tmp, store) = seeded_store();
    let mut first = String::from(": KEEPALIVE\n\n");
    first.push_str(&sse(&[
        r#"{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"name":"query","arguments":"{\"type\":\"todo\"}"}}]}}]}"#,
        r#"{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"name":"log_tail","arguments":"{\"n\":5}"}}]}}]}"#,
        r#"{"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#,
        "[DONE]",
    ]));
    let mock = MockLlm::serve(vec![
        first,
        sse(&[
            r#"{"choices":[{"index":0,"delta":{"content":"done"}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#,
            "[DONE]",
        ]),
    ]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["--json", "ask", "check"])
        .assert()
        .success();

    let requests = mock.finish();
    let second = requests[1].body_json();
    let wire = second["messages"].as_array().expect("messages");
    // Both calls executed and both results returned, with synthesized ids.
    let tool_results: Vec<&serde_json::Value> =
        wire.iter().filter(|m| m["role"] == "tool").collect();
    assert_eq!(tool_results.len(), 2, "both index-0 calls survived");
    let assistant = wire
        .iter()
        .find(|m| m["role"] == "assistant" && !m["tool_calls"].is_null())
        .expect("assistant turn with tool calls");
    let ids: Vec<&str> = assistant["tool_calls"]
        .as_array()
        .expect("calls")
        .iter()
        .filter_map(|c| c["id"].as_str())
        .collect();
    assert_eq!(ids.len(), 2);
    assert!(ids.iter().all(|id| !id.is_empty()), "ids were synthesized");
}

#[test]
fn unknown_and_masked_tools_come_back_as_error_results() {
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve(vec![
        sse(&[
            // `write` exists — but not on the read mask.
            r#"{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"c1","function":{"name":"write","arguments":"{\"path\":\"records/todos/x.md\",\"type\":\"todo\",\"summary\":\"X\"}"}}]}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#,
            "[DONE]",
        ]),
        sse(&[
            r#"{"choices":[{"index":0,"delta":{"content":"cannot write here"}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#,
            "[DONE]",
        ]),
    ]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["--json", "ask", "add a todo"])
        .assert()
        .success();

    let requests = mock.finish();
    let second = requests[1].body_json();
    let tool_msg = second["messages"]
        .as_array()
        .expect("messages")
        .iter()
        .find(|m| m["role"] == "tool")
        .cloned()
        .expect("error tool result");
    let content = tool_msg["content"].as_str().expect("content");
    assert!(content.starts_with("ERROR:"));
    assert!(content.contains("unknown tool"));
    // Nothing was written.
    assert!(!store.join("records/todos/x.md").exists());
}

// ─────────────────────────────────────────────────────────────────────────────
// dbmd do: writes land through the store contract
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn do_writes_through_the_store_contract() {
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve(vec![
        sse(&[
            r#"{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"c1","function":{"name":"write","arguments":"{\"path\":\"records/todos/walk-dog.md\",\"type\":\"todo\",\"summary\":\"Walk the dog\",\"fm\":[\"status=active\"],\"body\":\"- [ ] leash\"}"}}]}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#,
            "[DONE]",
        ]),
        sse(&[
            r#"{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"c2","function":{"name":"log","arguments":"{\"kind\":\"create\",\"object\":\"records/todos/walk-dog.md\",\"message\":\"added via do\"}"}}]}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#,
            "[DONE]",
        ]),
        sse(&[
            r#"{"choices":[{"index":0,"delta":{"content":"Added."}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#,
            "[DONE]",
        ]),
    ]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["--json", "do", "add walk the dog"])
        .assert()
        .success();
    mock.finish();

    // The record landed with frontmatter, body, and a minted id.
    let written =
        std::fs::read_to_string(store.join("records/todos/walk-dog.md")).expect("record written");
    assert!(written.contains("summary: Walk the dog"));
    assert!(written.contains("status: active"));
    assert!(written.contains("- [ ] leash"));
    // The store stays valid, and the log carries the model's entry.
    dbmd()
        .current_dir(&store)
        .args(["validate", "--all"])
        .assert()
        .success();
    let log = std::fs::read_to_string(store.join("log.md")).expect("log.md");
    assert!(log.contains("added via do"));
}

// ─────────────────────────────────────────────────────────────────────────────
// anthropic protocol
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn anthropic_protocol_round_trips_tool_use() {
    let (_tmp, store) = seeded_store();
    let first = "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"usage\":{\"input_tokens\":9}}}\n\n\
event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_1\",\"name\":\"query\"}}\n\n\
event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"type\\\":\"}}\n\n\
event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"\\\"todo\\\"}\"}}\n\n\
event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n\
event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\"},\"usage\":{\"output_tokens\":4}}\n\n\
event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
        .to_string();
    let second = "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"usage\":{\"input_tokens\":20}}}\n\n\
event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\"}}\n\n\
event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"One todo.\"}}\n\n\
event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n\
event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":3}}\n\n\
event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
        .to_string();
    let mock = MockLlm::serve(vec![first, second]);

    let assert = harness_cmd(&store, &mock.url, "anthropic")
        .env("DBMD_LLM_KEY", "sk-test-123")
        .args(["--json", "ask", "how many todos?"])
        .assert()
        .success();
    let stdout = assert.get_output().stdout.clone();

    let requests = mock.finish();
    assert_eq!(requests[0].path, "/v1/messages");
    assert_eq!(requests[0].header("x-api-key"), Some("sk-test-123"));
    assert_eq!(requests[0].header("anthropic-version"), Some("2023-06-01"));
    let first = requests[0].body_json();
    assert!(first["system"].as_str().expect("system").contains("db.md"));
    assert!(first["tools"]
        .as_array()
        .expect("tools")
        .iter()
        .any(|t| t["name"] == "query" && t["input_schema"]["type"] == "object"));

    // The fragmented input_json_delta was reassembled and executed; the
    // result rode back as ONE user message of tool_result blocks.
    let second = requests[1].body_json();
    let last = second["messages"]
        .as_array()
        .expect("messages")
        .last()
        .cloned()
        .expect("result message");
    assert_eq!(last["role"], "user");
    assert_eq!(last["content"][0]["type"], "tool_result");
    assert_eq!(last["content"][0]["tool_use_id"], "toolu_1");
    assert!(last["content"][0]["content"]
        .as_str()
        .expect("content")
        .contains("Buy milk"));

    let done = events(&stdout)
        .into_iter()
        .find(|e| e["event"] == "done")
        .expect("done");
    assert_eq!(done["text"], "One todo.");
}

// ─────────────────────────────────────────────────────────────────────────────
// config security rules
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn store_config_endpoint_refuses_unbound_ambient_key() {
    let (_tmp, store) = seeded_store();
    std::fs::create_dir_all(store.join(".dbmd")).expect(".dbmd");
    std::fs::write(
        store.join(".dbmd/config"),
        "llm_base_url = https://evil.example.com/v1\nllm_protocol = openai\nllm_model = x\n",
    )
    .expect("config");

    let mut command = dbmd();
    command.current_dir(&store);
    for var in [
        "DBMD_LLM_PROVIDER",
        "DBMD_LLM_BASE_URL",
        "DBMD_LLM_PROTOCOL",
        "DBMD_LLM_MODEL",
        "DBMD_LLM_KEY_ORIGIN",
    ] {
        command.env_remove(var);
    }
    let assert = command
        .env("DBMD_LLM_KEY", "sk-ambient")
        .args(["--json", "ask", "hi"])
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr).into_owned();
    assert!(stderr.contains("ASK_CONFIG"));
    assert!(stderr.contains("DBMD_LLM_KEY_ORIGIN"));
}

#[test]
fn plain_http_to_non_loopback_is_refused() {
    let (_tmp, store) = seeded_store();
    let mut command = dbmd();
    command.current_dir(&store);
    command.env_remove("DBMD_LLM_ALLOW_INSECURE_HTTP");
    let assert = command
        .env("DBMD_LLM_BASE_URL", "http://192.0.2.10:11434/v1")
        .env("DBMD_LLM_PROTOCOL", "openai")
        .env("DBMD_LLM_MODEL", "x")
        .args(["--json", "ask", "hi"])
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr).into_owned();
    assert!(stderr.contains("refusing plain http"));
}

// ─────────────────────────────────────────────────────────────────────────────
// dbmd build: the workspace mask
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn build_edits_the_workspace_and_refuses_escapes() {
    let (_tmp, store) = seeded_store();
    let workspace = store.parent().expect("parent").to_path_buf();
    std::fs::write(workspace.join("app.ts"), "const version = 1;\n").expect("seed app file");

    let mock = MockLlm::serve(vec![
        sse(&[
            r#"{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"c1","function":{"name":"edit_file","arguments":"{\"path\":\"app.ts\",\"old_text\":\"version = 1\",\"new_text\":\"version = 2\"}"}}]}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#,
            "[DONE]",
        ]),
        sse(&[
            r#"{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"c2","function":{"name":"read_file","arguments":"{\"path\":\"../../etc/hosts\"}"}}]}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#,
            "[DONE]",
        ]),
        sse(&[
            r#"{"choices":[{"index":0,"delta":{"content":"Bumped."}}]}"#,
            r#"{"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#,
            "[DONE]",
        ]),
    ]);

    harness_cmd(&store, &mock.url, "openai")
        .args([
            "--json",
            "build",
            "bump the version",
            "--workspace",
            workspace.to_str().expect("utf8 path"),
        ])
        .assert()
        .success();

    let requests = mock.finish();
    // The edit landed…
    let edited = std::fs::read_to_string(workspace.join("app.ts")).expect("app file");
    assert!(edited.contains("version = 2"));
    // …and the escape came back as an error result, not a file read.
    let third = requests[2].body_json();
    let escape_result = third["messages"]
        .as_array()
        .expect("messages")
        .iter()
        .rfind(|m| m["role"] == "tool")
        .cloned()
        .expect("escape result");
    let content = escape_result["content"].as_str().expect("content");
    assert!(content.starts_with("ERROR:"));
    assert!(content.contains("leaves the workspace"));
}

#[test]
fn build_without_a_declared_workspace_is_refused() {
    let (_tmp, store) = seeded_store();
    // Hermetic provider env (never consulted: the workspace refusal comes
    // first, so no request is ever made at this base URL).
    let assert = harness_cmd(&store, "http://127.0.0.1:9", "openai")
        .args(["--json", "build", "do something"])
        .assert()
        .failure();
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr).into_owned();
    assert!(stderr.contains("BUILD_NO_WORKSPACE"));
}

// ─────────────────────────────────────────────────────────────────────────────
// turn cap
// ─────────────────────────────────────────────────────────────────────────────

// ─────────────────────────────────────────────────────────────────────────────
// the /v1/ask and /v1/do API routes
// ─────────────────────────────────────────────────────────────────────────────

/// A running `dbmd api` with harness env pointed at a mock; killed on drop.
struct ApiServer {
    child: std::process::Child,
    addr: String,
}

impl ApiServer {
    fn spawn(store: &std::path::Path, mock_url: &str, flags: &[&str]) -> Self {
        let mut command = std::process::Command::new(assert_cmd::cargo::cargo_bin("dbmd"));
        command.args(["--json", "api", "--addr", "127.0.0.1:0", "--dir"]);
        command.arg(store);
        command.args(flags);
        for var in [
            "DBMD_LLM_PROVIDER",
            "DBMD_LLM_BASE_URL",
            "DBMD_LLM_PROTOCOL",
            "DBMD_LLM_MODEL",
            "DBMD_LLM_KEY",
            "DBMD_LLM_KEY_ORIGIN",
        ] {
            command.env_remove(var);
        }
        let mut child = command
            .env("DBMD_LLM_BASE_URL", format!("{mock_url}/v1"))
            .env("DBMD_LLM_PROTOCOL", "openai")
            .env("DBMD_LLM_MODEL", "fake-model")
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::null())
            .spawn()
            .expect("spawn dbmd api");
        let stdout = child.stdout.take().expect("piped stdout");
        let mut first = String::new();
        BufReader::new(stdout)
            .read_line(&mut first)
            .expect("serving line");
        let serving: serde_json::Value = serde_json::from_str(&first).expect("serving JSON");
        let addr = serving["serving"]
            .as_str()
            .expect("url")
            .strip_prefix("http://")
            .expect("http url")
            .to_string();
        Self { child, addr }
    }

    /// POST one JSON body and return (status, raw response after headers).
    fn post(&self, target: &str, body: &str) -> (u16, String) {
        let mut stream = std::net::TcpStream::connect(&self.addr).expect("connect");
        stream
            .set_read_timeout(Some(std::time::Duration::from_secs(30)))
            .expect("timeout");
        let head = format!(
            "POST {target} HTTP/1.1\r\nhost: {}\r\ncontent-type: application/json\r\ncontent-length: {}\r\n\r\n",
            self.addr,
            body.len()
        );
        stream.write_all(head.as_bytes()).expect("head");
        stream.write_all(body.as_bytes()).expect("body");
        let mut raw = Vec::new();
        stream.read_to_end(&mut raw).expect("response");
        let text = String::from_utf8_lossy(&raw).into_owned();
        let status: u16 = text
            .lines()
            .next()
            .and_then(|line| line.split_whitespace().nth(1))
            .and_then(|code| code.parse().ok())
            .expect("status line");
        let body = text
            .split_once("\r\n\r\n")
            .map(|(_, b)| b.to_string())
            .unwrap_or_default();
        (status, body)
    }
}

impl Drop for ApiServer {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
    }
}

#[test]
fn api_ask_route_streams_the_event_feed() {
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve(vec![sse(&[
        r#"{"choices":[{"index":0,"delta":{"content":"Two todos."}}]}"#,
        r#"{"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#,
        "[DONE]",
    ])]);
    let api = ApiServer::spawn(&store, &mock.url, &["--ask"]);

    let (status, body) = api.post("/v1/ask", r#"{"prompt":"what's on the list?"}"#);
    assert_eq!(status, 200);
    // SSE frames: at minimum text_delta and done, one JSON object each.
    let frames: Vec<serde_json::Value> = body
        .lines()
        .filter_map(|line| line.strip_prefix("data: "))
        .filter_map(|data| serde_json::from_str(data).ok())
        .collect();
    assert!(frames.iter().any(|f| f["event"] == "text_delta"));
    let done = frames
        .iter()
        .find(|f| f["event"] == "done")
        .expect("done frame");
    assert_eq!(done["text"], "Two todos.");
    mock.finish();
}

#[test]
fn api_harness_routes_are_off_by_default_and_do_needs_its_own_flag() {
    let (_tmp, store) = seeded_store();
    // No flags: both routes refuse without touching any endpoint.
    let api = ApiServer::spawn(&store, "http://127.0.0.1:9", &[]);
    let (status, body) = api.post("/v1/ask", r#"{"prompt":"hi"}"#);
    assert_eq!(status, 403);
    assert!(body.contains("ASK_DISABLED"));
    drop(api);

    // --ask alone: /v1/do still refuses (writes need their own opt-in).
    let api = ApiServer::spawn(&store, "http://127.0.0.1:9", &["--ask"]);
    let (status, body) = api.post("/v1/do", r#"{"prompt":"hi"}"#);
    assert_eq!(status, 403);
    assert!(body.contains("ASK_DISABLED"));
}

#[test]
fn the_turn_cap_forces_a_final_no_tools_answer() {
    let (_tmp, store) = seeded_store();
    let tool_turn = sse(&[
        r#"{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"c1","function":{"name":"log_tail","arguments":"{}"}}]}}]}"#,
        r#"{"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#,
        "[DONE]",
    ]);
    let final_turn = sse(&[
        r#"{"choices":[{"index":0,"delta":{"content":"best effort"}}]}"#,
        r#"{"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#,
        "[DONE]",
    ]);
    // max-turns 2 ⇒ two tool turns, then the capped final call (no tools).
    let mock = MockLlm::serve(vec![tool_turn.clone(), tool_turn, final_turn]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["--json", "ask", "loop forever", "--max-turns", "2"])
        .assert()
        .success();

    let requests = mock.finish();
    assert_eq!(requests.len(), 3);
    let last = requests[2].body_json();
    assert!(
        last.get("tools").is_none(),
        "the capped final call carries no tools"
    );
    let flattened = last["messages"].to_string();
    assert!(flattened.contains("tool-call limit"));
}

// ─────────────────────────────────────────────────────────────────────────────
// reasoning effort: the wire field, the per-protocol shape, and the fallback
// ─────────────────────────────────────────────────────────────────────────────

/// One scripted OpenAI-compat answer with no tool calls.
fn openai_answer(text: &str) -> String {
    sse(&[
        &format!(
            r#"{{"choices":[{{"delta":{{"content":{}}},"index":0}}]}}"#,
            serde_json::Value::String(text.to_string())
        ),
        r#"{"choices":[{"delta":{},"finish_reason":"stop","index":0}]}"#,
        "[DONE]",
    ])
}

/// One scripted Anthropic answer with no tool calls.
fn anthropic_answer(text: &str) -> String {
    sse(&[
        r#"{"type":"message_start","message":{"usage":{"input_tokens":1}}}"#,
        r#"{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}"#,
        &format!(
            r#"{{"type":"content_block_delta","index":0,"delta":{{"type":"text_delta","text":{}}}}}"#,
            serde_json::Value::String(text.to_string())
        ),
        r#"{"type":"content_block_stop","index":0}"#,
        r#"{"type":"message_delta","delta":{"stop_reason":"end_turn"}}"#,
        r#"{"type":"message_stop"}"#,
    ])
}

#[test]
fn openai_effort_rides_as_reasoning_effort() {
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve(vec![openai_answer("ok")]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["ask", "how many todos?", "--effort", "high"])
        .assert()
        .success();

    let requests = mock.finish();
    assert_eq!(requests.len(), 1);
    let body = requests[0].body_json();
    assert_eq!(body["reasoning_effort"], "high");
}

#[test]
fn no_effort_flag_sends_no_reasoning_field() {
    // The load-bearing default: an unset effort must leave the provider on
    // its own, because "unset" and "low" are very different on a server that
    // already has thinking switched on.
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve(vec![openai_answer("ok")]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["ask", "how many todos?"])
        .assert()
        .success();

    let requests = mock.finish();
    let body = requests[0].body_json();
    assert!(
        body.get("reasoning_effort").is_none(),
        "unset effort must send no field: {body}"
    );
}

#[test]
fn effort_is_dropped_when_the_endpoint_refuses_it() {
    // An arbitrary OpenAI-compatible server may not know the field at all.
    // Asking it to think harder must not brick the run — the request retries
    // without the field, and the user is told it happened.
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve_with_status(vec![
        (
            400,
            r#"{"error":{"message":"unrecognized request argument: reasoning_effort"}}"#
                .to_string(),
        ),
        (200, openai_answer("ok")),
    ]);

    let output = harness_cmd(&store, &mock.url, "openai")
        .args(["--json", "ask", "how many todos?", "--effort", "max"])
        .assert()
        .success()
        .get_output()
        .clone();

    let requests = mock.finish();
    assert_eq!(requests.len(), 2, "the refused request must be retried");
    assert_eq!(requests[0].body_json()["reasoning_effort"], "high");
    assert!(
        requests[1].body_json().get("reasoning_effort").is_none(),
        "the retry must drop the refused field"
    );

    let notices: Vec<serde_json::Value> = events(&output.stdout)
        .into_iter()
        .filter(|e| e["event"] == "notice")
        .collect();
    assert_eq!(
        notices.len(),
        1,
        "a silently downgraded run must not look like a clean one"
    );
    let message = notices[0]["message"].as_str().unwrap_or_default();
    assert!(
        message.contains("reasoning_effort=high"),
        "the notice must name what was refused: {message}"
    );
}

#[test]
fn anthropic_effort_uses_output_config_and_adaptive_thinking() {
    // The current Anthropic shape. `budget_tokens` is a 400 on these models,
    // so it must not appear in the first attempt.
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve(vec![anthropic_answer("ok")]);

    harness_cmd(&store, &mock.url, "anthropic")
        .args(["ask", "how many todos?", "--effort", "xhigh"])
        .assert()
        .success();

    let requests = mock.finish();
    let body = requests[0].body_json();
    assert_eq!(body["output_config"]["effort"], "xhigh");
    assert_eq!(body["thinking"]["type"], "adaptive");
    assert!(body["thinking"].get("budget_tokens").is_none());
}

#[test]
fn anthropic_effort_off_disables_thinking() {
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve(vec![anthropic_answer("ok")]);

    harness_cmd(&store, &mock.url, "anthropic")
        .args(["ask", "how many todos?", "--effort", "off"])
        .assert()
        .success();

    let requests = mock.finish();
    let body = requests[0].body_json();
    assert_eq!(body["thinking"]["type"], "disabled");
    assert!(body.get("output_config").is_none());
}

#[test]
fn anthropic_falls_back_to_budget_tokens_on_older_models() {
    // Pre-4.6 models reject `output_config` and take a thinking budget
    // instead. Rather than pin a model list that goes stale, the request
    // degrades on the wire.
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve_with_status(vec![
        (
            400,
            r#"{"type":"error","error":{"message":"output_config: unexpected field"}}"#.to_string(),
        ),
        (200, anthropic_answer("ok")),
    ]);

    harness_cmd(&store, &mock.url, "anthropic")
        .args(["ask", "how many todos?", "--effort", "medium"])
        .assert()
        .success();

    let requests = mock.finish();
    assert_eq!(requests.len(), 2);
    let legacy = requests[1].body_json();
    assert_eq!(legacy["thinking"]["type"], "enabled");
    assert_eq!(legacy["thinking"]["budget_tokens"], 8192);
    // The budget must not eat the answer's room.
    assert_eq!(legacy["max_tokens"], 4096 + 8192);
}

#[test]
fn unknown_effort_is_refused_before_any_request() {
    // Fail fast and name the rungs — never after a network round-trip.
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve(vec![]);

    let output = harness_cmd(&store, &mock.url, "openai")
        .args(["ask", "how many todos?", "--effort", "turbo"])
        .assert()
        .failure()
        .get_output()
        .clone();

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("turbo"), "{stderr}");
    assert!(
        stderr.contains("xhigh"),
        "the valid rungs go in the error: {stderr}"
    );
    assert!(mock.finish().is_empty(), "nothing may reach the provider");
}

#[test]
fn store_config_can_pin_the_effort() {
    // Same precedence chain as every other knob: `.dbmd/config` supplies it
    // when no flag and no environment variable do.
    let (_tmp, store) = seeded_store();
    std::fs::create_dir_all(store.join(".dbmd")).expect("config dir");
    std::fs::write(store.join(".dbmd").join("config"), "llm_effort = low\n").expect("config");
    let mock = MockLlm::serve(vec![openai_answer("ok")]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["ask", "how many todos?"])
        .assert()
        .success();

    let requests = mock.finish();
    assert_eq!(requests[0].body_json()["reasoning_effort"], "low");
}

#[test]
fn the_flag_beats_the_store_config() {
    let (_tmp, store) = seeded_store();
    std::fs::create_dir_all(store.join(".dbmd")).expect("config dir");
    std::fs::write(store.join(".dbmd").join("config"), "llm_effort = low\n").expect("config");
    let mock = MockLlm::serve(vec![openai_answer("ok")]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["ask", "how many todos?", "--effort", "medium"])
        .assert()
        .success();

    let requests = mock.finish();
    assert_eq!(requests[0].body_json()["reasoning_effort"], "medium");
}

#[test]
fn the_ollama_dialect_and_provider_scoped_effort_reach_the_wire() {
    // Two things at once, both previously wrong in this codebase:
    // 1. Ollama's vocabulary differs — `max`, not `high`, is its top rung,
    //    and that is what a Qwen3.8 chat template reads as `xhigh`.
    // 2. A provider-scoped `llm_effort_<name>` survives an explicit
    //    `--provider`, while an unscoped one would not.
    let (_tmp, store) = seeded_store();
    std::fs::create_dir_all(store.join(".dbmd")).expect("config dir");
    std::fs::write(
        store.join(".dbmd").join("config"),
        "llm_effort = low\nllm_effort_ollama = max\n",
    )
    .expect("config");
    let mock = MockLlm::serve(vec![openai_answer("ok")]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["ask", "how many todos?", "--provider", "ollama"])
        .assert()
        .success();

    let requests = mock.finish();
    let body = requests[0].body_json();
    assert_eq!(
        body["reasoning_effort"], "max",
        "the scoped effort must win, in Ollama's own vocabulary: {body}"
    );
}

#[test]
fn a_refused_effort_is_not_re_offered_every_turn() {
    // Found by asking whether this holds up on open-model servers: the
    // adapter had no memory across turns, so an endpoint that refuses
    // `reasoning_effort` paid the rejected round-trip on EVERY turn — up to
    // max_turns wasted requests per run. It must be refused once.
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve_with_status(vec![
        (
            400,
            r#"{"error":{"message":"unrecognized request argument: reasoning_effort"}}"#
                .to_string(),
        ),
        (
            200,
            sse(&[
                r#"{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"name":"query","arguments":"{\"type\":\"todo\"}"}}]}}]}"#,
                r#"{"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}"#,
                "[DONE]",
            ]),
        ),
        (200, openai_answer("three todos")),
    ]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["ask", "what is on the list?", "--effort", "high"])
        .assert()
        .success();

    let requests = mock.finish();
    assert_eq!(
        requests.len(),
        3,
        "one refusal, then two accepted turns — not a refusal per turn"
    );
    assert_eq!(requests[0].body_json()["reasoning_effort"], "high");
    for (index, request) in requests.iter().enumerate().skip(1) {
        assert!(
            request.body_json().get("reasoning_effort").is_none(),
            "turn {index} must not re-offer the refused field"
        );
    }
}

#[test]
fn effort_off_disables_thinking_rather_than_shortening_it() {
    // `minimal` is a short think; `none` is off. Local reasoning models
    // default thinking on, so this is the difference between `--effort off`
    // working and silently doing nothing.
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve(vec![openai_answer("ok")]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["ask", "how many todos?", "--effort", "off"])
        .assert()
        .success();

    let requests = mock.finish();
    assert_eq!(requests[0].body_json()["reasoning_effort"], "none");
}

#[test]
fn a_template_that_raises_on_the_effort_value_is_a_refusal_not_an_outage() {
    // Found by running a real local model: Ollama's own validator accepts
    // `xhigh`, then Qwen3.8's chat template raises on it — server-side, so it
    // arrives as HTTP 500, not 400. Treating that as a hard failure killed the
    // run outright. It must degrade like any other refused parameter.
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve_with_status(vec![
        (
            500,
            r#"{"error":{"message":"Jinja Exception: Unexpected reasoning effort xhigh. Supported types are medium and low."}}"#
                .to_string(),
        ),
        (200, openai_answer("ok")),
    ]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["ask", "how many todos?", "--effort", "xhigh"])
        .assert()
        .success();

    let requests = mock.finish();
    assert_eq!(requests.len(), 2, "the raised template must be retried");
    assert_eq!(requests[1].body_json()["reasoning_effort"], "high");
}

#[test]
fn an_ordinary_server_error_is_still_a_failure() {
    // The other half of that fix: a 500 that says nothing about reasoning is a
    // real outage. Retrying it would mask the fault and re-send billable work.
    let (_tmp, store) = seeded_store();
    let mock = MockLlm::serve_with_status(vec![(
        500,
        r#"{"error":{"message":"internal server error"}}"#.to_string(),
    )]);

    harness_cmd(&store, &mock.url, "openai")
        .args(["ask", "how many todos?", "--effort", "xhigh"])
        .assert()
        .failure();

    assert_eq!(mock.finish().len(), 1, "a real 500 must not be retried");
}