vectorless 0.1.21

Hierarchical, reasoning-native document intelligence engine
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
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0

//! Configuration documentation generation.
//!
//! This module provides utilities for generating documentation
//! from configuration types, including markdown reference and
//! example TOML files.

use super::types::Config;

/// Configuration documentation generator.
#[derive(Debug, Clone)]
pub struct ConfigDocs {
    config: Config,
}

impl ConfigDocs {
    /// Create a new documentation generator.
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// Create with default configuration.
    pub fn with_defaults() -> Self {
        Self::new(Config::default())
    }

    /// Generate markdown documentation for the configuration.
    pub fn to_markdown(&self) -> String {
        let mut md = String::new();

        md.push_str("# Configuration Reference\n\n");
        md.push_str("This document describes all configuration options for vectorless.\n\n");
        md.push_str("## Configuration File\n\n");
        md.push_str("Configuration is loaded from a TOML file. Default locations:\n");
        md.push_str("- `./vectorless.toml`\n");
        md.push_str("- `./config.toml`\n");
        md.push_str("- `./.vectorless.toml`\n\n");

        // LLM section (unified)
        md.push_str("## `[llm]`\n\n");
        md.push_str("Unified LLM configuration for all LLM operations.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "api_key",
            "string?",
            "null",
            "Default API key (used by all clients unless overridden)",
        );
        md.push_str("\n");

        // LLM.summary section
        md.push_str("## `[llm.summary]`\n\n");
        md.push_str("Summary client - generates document summaries during indexing.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "model",
            "string",
            "gpt-4o-mini",
            "Model for summarization (fast, cheap model recommended)",
        );
        self.add_row(
            &mut md,
            "endpoint",
            "string",
            "https://api.openai.com/v1",
            "API endpoint",
        );
        self.add_row(
            &mut md,
            "api_key",
            "string?",
            "null",
            "API key (optional, uses default if not set)",
        );
        self.add_row(&mut md, "max_tokens", "usize", "200", "Maximum tokens for summary");
        self.add_row(&mut md, "temperature", "f32", "0.0", "Temperature for generation");
        md.push_str("\n");

        // LLM.retrieval section
        md.push_str("## `[llm.retrieval]`\n\n");
        md.push_str("Retrieval client - used for retrieval decisions and content evaluation.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "model",
            "string",
            "gpt-4o",
            "Model for retrieval (more capable model recommended)",
        );
        self.add_row(
            &mut md,
            "endpoint",
            "string",
            "https://api.openai.com/v1",
            "API endpoint",
        );
        self.add_row(
            &mut md,
            "api_key",
            "string?",
            "null",
            "API key (optional, uses default if not set)",
        );
        self.add_row(&mut md, "max_tokens", "usize", "100", "Maximum tokens for response");
        self.add_row(&mut md, "temperature", "f32", "0.0", "Temperature for generation");
        md.push_str("\n");

        // LLM.pilot section
        md.push_str("## `[llm.pilot]`\n\n");
        md.push_str("Pilot client - used for intelligent navigation guidance.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "model",
            "string",
            "gpt-4o-mini",
            "Model for pilot navigation (fast model recommended)",
        );
        self.add_row(
            &mut md,
            "endpoint",
            "string",
            "https://api.openai.com/v1",
            "API endpoint",
        );
        self.add_row(
            &mut md,
            "api_key",
            "string?",
            "null",
            "API key (optional, uses default if not set)",
        );
        self.add_row(&mut md, "max_tokens", "usize", "300", "Maximum tokens for response");
        self.add_row(&mut md, "temperature", "f32", "0.0", "Temperature for generation");
        md.push_str("\n");

        // LLM.retry section
        md.push_str("## `[llm.retry]`\n\n");
        md.push_str("Retry configuration for all LLM calls.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(&mut md, "max_attempts", "usize", "3", "Maximum retry attempts");
        self.add_row(
            &mut md,
            "initial_delay_ms",
            "u64",
            "500",
            "Initial delay before first retry (ms)",
        );
        self.add_row(
            &mut md,
            "max_delay_ms",
            "u64",
            "30000",
            "Maximum delay between retries (ms)",
        );
        self.add_row(
            &mut md,
            "multiplier",
            "f64",
            "2.0",
            "Multiplier for exponential backoff",
        );
        self.add_row(
            &mut md,
            "retry_on_rate_limit",
            "bool",
            "true",
            "Whether to retry on rate limit errors",
        );
        md.push_str("\n");

        // LLM.throttle section
        md.push_str("## `[llm.throttle]`\n\n");
        md.push_str("Throttle/rate limiting configuration for all LLM calls.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "max_concurrent_requests",
            "usize",
            "10",
            "Maximum concurrent LLM API calls",
        );
        self.add_row(
            &mut md,
            "requests_per_minute",
            "usize",
            "500",
            "Rate limit: requests per minute",
        );
        self.add_row(&mut md, "enabled", "bool", "true", "Enable rate limiting");
        self.add_row(
            &mut md,
            "semaphore_enabled",
            "bool",
            "true",
            "Enable semaphore-based concurrency",
        );
        md.push_str("\n");

        // LLM.fallback section
        md.push_str("## `[llm.fallback]`\n\n");
        md.push_str("Fallback configuration for all LLM calls.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "enabled",
            "bool",
            "true",
            "Enable fallback mechanism",
        );
        self.add_row(
            &mut md,
            "models",
            "[string]",
            "[\"gpt-4o-mini\", \"glm-4-flash\"]",
            "Fallback models in priority order",
        );
        self.add_row(
            &mut md,
            "endpoints",
            "[string]",
            "[]",
            "Fallback endpoints in priority order",
        );
        self.add_row(
            &mut md,
            "on_rate_limit",
            "string",
            "retry_then_fallback",
            "Behavior on rate limit (retry, fallback, retry_then_fallback, fail)",
        );
        self.add_row(
            &mut md,
            "on_timeout",
            "string",
            "retry_then_fallback",
            "Behavior on timeout",
        );
        self.add_row(
            &mut md,
            "on_all_failed",
            "string",
            "return_error",
            "Behavior when all attempts fail (return_error, return_cache)",
        );
        md.push_str("\n");

        // Metrics section
        md.push_str("## `[metrics]`\n\n");
        md.push_str("Unified metrics configuration for observability.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(&mut md, "enabled", "bool", "true", "Enable metrics collection");
        self.add_row(
            &mut md,
            "storage_path",
            "string",
            "./workspace/metrics",
            "Storage path for persisted metrics",
        );
        self.add_row(
            &mut md,
            "retention_days",
            "usize",
            "30",
            "Retention period in days",
        );
        md.push_str("\n");

        // Metrics.llm section
        md.push_str("## `[metrics.llm]`\n\n");
        md.push_str("LLM-specific metrics configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(&mut md, "track_tokens", "bool", "true", "Track token usage");
        self.add_row(&mut md, "track_latency", "bool", "true", "Track latency");
        self.add_row(&mut md, "track_cost", "bool", "true", "Track estimated cost");
        self.add_row(
            &mut md,
            "cost_per_1k_input_tokens",
            "f64",
            "0.00015",
            "Cost per 1K input tokens (gpt-4o-mini)",
        );
        self.add_row(
            &mut md,
            "cost_per_1k_output_tokens",
            "f64",
            "0.0006",
            "Cost per 1K output tokens (gpt-4o-mini)",
        );
        md.push_str("\n");

        // Metrics.pilot section
        md.push_str("## `[metrics.pilot]`\n\n");
        md.push_str("Pilot-specific metrics configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(&mut md, "track_decisions", "bool", "true", "Track Pilot decisions");
        self.add_row(
            &mut md,
            "track_accuracy",
            "bool",
            "true",
            "Track decision accuracy (requires feedback)",
        );
        self.add_row(&mut md, "track_feedback", "bool", "true", "Track user feedback");
        md.push_str("\n");

        // Metrics.retrieval section
        md.push_str("## `[metrics.retrieval]`\n\n");
        md.push_str("Retrieval-specific metrics configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(&mut md, "track_paths", "bool", "true", "Track search paths");
        self.add_row(&mut md, "track_scores", "bool", "true", "Track relevance scores");
        self.add_row(&mut md, "track_iterations", "bool", "true", "Track iterations");
        self.add_row(&mut md, "track_cache", "bool", "true", "Track cache hits/misses");
        md.push_str("\n");

        // Pilot section
        md.push_str("## `[pilot]`\n\n");
        md.push_str("Pilot navigation configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "mode",
            "string",
            "Balanced",
            "Operation mode (Aggressive, Balanced, Conservative, AlgorithmOnly)",
        );
        self.add_row(
            &mut md,
            "guide_at_start",
            "bool",
            "true",
            "Whether to provide guidance at search start",
        );
        self.add_row(
            &mut md,
            "guide_at_backtrack",
            "bool",
            "true",
            "Whether to provide guidance during backtracking",
        );
        md.push_str("\n");

        // Pilot.budget section
        md.push_str("## `[pilot.budget]`\n\n");
        md.push_str("Token and call budget constraints.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "max_tokens_per_query",
            "usize",
            "2000",
            "Maximum total tokens per query",
        );
        self.add_row(
            &mut md,
            "max_tokens_per_call",
            "usize",
            "500",
            "Maximum tokens per single LLM call",
        );
        self.add_row(
            &mut md,
            "max_calls_per_query",
            "usize",
            "5",
            "Maximum number of LLM calls per query",
        );
        self.add_row(
            &mut md,
            "max_calls_per_level",
            "usize",
            "2",
            "Maximum number of LLM calls per tree level",
        );
        self.add_row(
            &mut md,
            "hard_limit",
            "bool",
            "true",
            "Whether to enforce hard limits (true) or soft limits (false)",
        );
        md.push_str("\n");

        // Pilot.intervention section
        md.push_str("## `[pilot.intervention]`\n\n");
        md.push_str("Intervention threshold settings.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "fork_threshold",
            "usize",
            "3",
            "Minimum candidates to trigger fork intervention",
        );
        self.add_row(
            &mut md,
            "score_gap_threshold",
            "f32",
            "0.15",
            "Score gap threshold (intervene when scores are close)",
        );
        self.add_row(
            &mut md,
            "low_score_threshold",
            "f32",
            "0.3",
            "Low score threshold (intervene when best score is below this)",
        );
        self.add_row(
            &mut md,
            "max_interventions_per_level",
            "usize",
            "2",
            "Maximum interventions allowed per tree level",
        );
        md.push_str("\n");

        // Pilot.feedback section
        md.push_str("## `[pilot.feedback]`\n\n");
        md.push_str("Feedback and learning configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(&mut md, "enabled", "bool", "true", "Enable feedback collection");
        self.add_row(
            &mut md,
            "storage_path",
            "string",
            "./workspace/feedback",
            "Storage path for feedback data",
        );
        self.add_row(
            &mut md,
            "learning_rate",
            "f32",
            "0.1",
            "Learning rate for feedback-based improvements",
        );
        self.add_row(
            &mut md,
            "min_samples_for_learning",
            "usize",
            "10",
            "Minimum samples before applying learning",
        );
        md.push_str("\n");

        // Retrieval section
        md.push_str("## `[retrieval]`\n\n");
        md.push_str("Retrieval model and behavior configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "model",
            "string",
            "gpt-4o",
            "Model for retrieval navigation",
        );
        self.add_row(
            &mut md,
            "endpoint",
            "string",
            "https://api.openai.com/v1",
            "API endpoint",
        );
        self.add_row(&mut md, "top_k", "usize", "3", "Number of top results to return");
        self.add_row(
            &mut md,
            "max_tokens",
            "usize",
            "1000",
            "Maximum tokens for retrieval context",
        );
        self.add_row(&mut md, "temperature", "f32", "0.0", "Temperature for retrieval");
        md.push_str("\n");

        // Retrieval.search section
        md.push_str("## `[retrieval.search]`\n\n");
        md.push_str("Search algorithm configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(&mut md, "top_k", "usize", "5", "Number of top-k results to return");
        self.add_row(
            &mut md,
            "beam_width",
            "usize",
            "3",
            "Beam width for multi-path search",
        );
        self.add_row(
            &mut md,
            "max_iterations",
            "usize",
            "10",
            "Maximum iterations for search algorithms",
        );
        self.add_row(
            &mut md,
            "min_score",
            "f32",
            "0.1",
            "Minimum score to include a path",
        );
        md.push_str("\n");

        // Retrieval.sufficiency section
        md.push_str("## `[retrieval.sufficiency]`\n\n");
        md.push_str("Sufficiency checker configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "min_tokens",
            "usize",
            "500",
            "Minimum tokens for sufficiency",
        );
        self.add_row(
            &mut md,
            "target_tokens",
            "usize",
            "2000",
            "Target tokens for full sufficiency",
        );
        self.add_row(
            &mut md,
            "max_tokens",
            "usize",
            "4000",
            "Maximum tokens before stopping",
        );
        self.add_row(
            &mut md,
            "min_content_length",
            "usize",
            "200",
            "Minimum content length (characters)",
        );
        self.add_row(
            &mut md,
            "confidence_threshold",
            "f32",
            "0.7",
            "Confidence threshold for LLM judge",
        );
        md.push_str("\n");

        // Retrieval.cache section
        md.push_str("## `[retrieval.cache]`\n\n");
        md.push_str("Cache configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(&mut md, "max_entries", "usize", "1000", "Maximum cache entries");
        self.add_row(&mut md, "ttl_secs", "u64", "3600", "Time-to-live in seconds");
        md.push_str("\n");

        // Retrieval.strategy section
        md.push_str("## `[retrieval.strategy]`\n\n");
        md.push_str("Strategy-specific configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "exploration_weight",
            "f32",
            "1.414",
            "MCTS exploration weight (√2)",
        );
        self.add_row(
            &mut md,
            "similarity_threshold",
            "f32",
            "0.5",
            "Semantic similarity threshold",
        );
        self.add_row(
            &mut md,
            "high_similarity_threshold",
            "f32",
            "0.8",
            "High similarity for 'answer' decision",
        );
        self.add_row(
            &mut md,
            "low_similarity_threshold",
            "f32",
            "0.3",
            "Low similarity for 'explore' decision",
        );
        md.push_str("\n");

        // Retrieval.content section
        md.push_str("## `[retrieval.content]`\n\n");
        md.push_str("Content aggregator configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "enabled",
            "bool",
            "true",
            "Enable content aggregator",
        );
        self.add_row(
            &mut md,
            "token_budget",
            "usize",
            "4000",
            "Maximum tokens for aggregated content",
        );
        self.add_row(
            &mut md,
            "min_relevance_score",
            "f32",
            "0.2",
            "Minimum relevance score threshold (0.0-1.0)",
        );
        self.add_row(
            &mut md,
            "scoring_strategy",
            "string",
            "hybrid",
            "Scoring strategy (keyword, bm25, hybrid)",
        );
        self.add_row(
            &mut md,
            "output_format",
            "string",
            "markdown",
            "Output format (markdown, json, tree, flat)",
        );
        self.add_row(
            &mut md,
            "include_scores",
            "bool",
            "false",
            "Include relevance scores in output",
        );
        self.add_row(
            &mut md,
            "hierarchical_min_per_level",
            "f32",
            "0.1",
            "Minimum budget allocation per depth level",
        );
        self.add_row(
            &mut md,
            "deduplicate",
            "bool",
            "true",
            "Enable content deduplication",
        );
        self.add_row(
            &mut md,
            "dedup_threshold",
            "f32",
            "0.9",
            "Similarity threshold for deduplication",
        );
        md.push_str("\n");

        // Retrieval.multiturn section
        md.push_str("## `[retrieval.multiturn]`\n\n");
        md.push_str("Multi-turn retrieval configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "enabled",
            "bool",
            "true",
            "Enable multi-turn retrieval",
        );
        self.add_row(
            &mut md,
            "max_sub_queries",
            "usize",
            "3",
            "Maximum sub-queries per query",
        );
        self.add_row(
            &mut md,
            "decomposition_model",
            "string",
            "gpt-4o-mini",
            "Model for query decomposition",
        );
        self.add_row(
            &mut md,
            "aggregation_strategy",
            "string",
            "merge",
            "Aggregation strategy (merge, rank, synthesize)",
        );
        md.push_str("\n");

        // Retrieval.reference section
        md.push_str("## `[retrieval.reference]`\n\n");
        md.push_str("Reference following configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "enabled",
            "bool",
            "true",
            "Enable reference following",
        );
        self.add_row(&mut md, "max_depth", "usize", "3", "Maximum reference depth");
        self.add_row(
            &mut md,
            "max_references",
            "usize",
            "10",
            "Maximum references to follow",
        );
        self.add_row(
            &mut md,
            "follow_pages",
            "bool",
            "true",
            "Follow page references",
        );
        self.add_row(
            &mut md,
            "follow_tables_figures",
            "bool",
            "true",
            "Follow table/figure references",
        );
        self.add_row(
            &mut md,
            "min_confidence",
            "f32",
            "0.5",
            "Minimum confidence to follow reference",
        );
        md.push_str("\n");

        // Storage section
        md.push_str("## `[storage]`\n\n");
        md.push_str("Storage configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "workspace_dir",
            "string",
            "./workspace",
            "Workspace directory for persisted documents",
        );
        self.add_row(&mut md, "cache_size", "usize", "100", "Cache size");
        self.add_row(
            &mut md,
            "atomic_writes",
            "bool",
            "true",
            "Enable atomic file writes",
        );
        self.add_row(&mut md, "file_lock", "bool", "true", "Enable file locking");
        self.add_row(
            &mut md,
            "checksum_enabled",
            "bool",
            "true",
            "Enable checksum verification",
        );
        md.push_str("\n");

        // Storage.compression section
        md.push_str("## `[storage.compression]`\n\n");
        md.push_str("Compression configuration.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(&mut md, "enabled", "bool", "false", "Enable compression");
        self.add_row(
            &mut md,
            "algorithm",
            "string",
            "gzip",
            "Compression algorithm (gzip, zstd, lz4)",
        );
        self.add_row(&mut md, "level", "u32", "6", "Compression level");
        md.push_str("\n");

        // Indexer section
        md.push_str("## `[indexer]`\n\n");
        md.push_str("Controls document indexing behavior.\n\n");
        md.push_str("| Option | Type | Default | Description |\n");
        md.push_str("|--------|------|---------|-------------|\n");
        self.add_row(
            &mut md,
            "subsection_threshold",
            "usize",
            "300",
            "Word count threshold for splitting sections into subsections",
        );
        self.add_row(
            &mut md,
            "max_segment_tokens",
            "usize",
            "3000",
            "Maximum tokens to send in a single segmentation request",
        );
        self.add_row(
            &mut md,
            "max_summary_tokens",
            "usize",
            "200",
            "Maximum tokens for each summary",
        );
        self.add_row(
            &mut md,
            "min_summary_tokens",
            "usize",
            "20",
            "Minimum content tokens required to generate a summary",
        );
        md.push_str("\n");

        md
    }

    fn add_row(&self, md: &mut String, name: &str, ty: &str, default: &str, desc: &str) {
        md.push_str(&format!(
            "| `{}` | {} | {} | {} |\n",
            name, ty, default, desc
        ));
    }

    /// Generate an example TOML file with all options.
    pub fn to_example_toml(&self) -> String {
        toml::to_string_pretty(&self.config).unwrap_or_else(|e| {
            format!(
                "# Error generating TOML: {}\n\n# Using default config\n{}",
                e,
                Self::fallback_toml()
            )
        })
    }

    fn fallback_toml() -> String {
        r#"# Vectorless Configuration Example
# Copy this file to vectorless.toml and fill in your API keys
#
# All configuration is loaded from this file only.
# No environment variables are used - this ensures explicit, traceable configuration.

# ============================================================================
# LLM Configuration (Unified)
# ============================================================================
#
# The LLM pool allows configuring different models for different purposes:
# - summary: Used for generating document summaries during indexing
# - retrieval: Used for retrieval decisions and content evaluation
# - pilot: Used for intelligent navigation guidance
#
# Each client can have its own model, endpoint, and settings.

[llm]
# Default API key (used by all clients unless overridden per-client)
api_key = "sk-your-api-key-here"

# Summary client - generates document summaries during indexing
# Use a fast, cheap model for bulk processing
[llm.summary]
model = "gpt-4o-mini"
endpoint = "https://api.openai.com/v1"
max_tokens = 200
temperature = 0.0
# api_key = "sk-specific-key-for-summary"  # Optional: override default

# Retrieval client - used for retrieval decisions and content evaluation
# Can use a more capable model for better decisions
[llm.retrieval]
model = "gpt-4o"
endpoint = "https://api.openai.com/v1"
max_tokens = 100
temperature = 0.0
# api_key = "sk-specific-key-for-retrieval"  # Optional: override default

# Pilot client - used for intelligent navigation guidance
# Use a fast model for quick navigation decisions
[llm.pilot]
model = "gpt-4o-mini"
endpoint = "https://api.openai.com/v1"
max_tokens = 300
temperature = 0.0
# api_key = "sk-specific-key-for-pilot"  # Optional: override default

# Retry configuration (applies to all LLM calls)
[llm.retry]
max_attempts = 3
initial_delay_ms = 500
max_delay_ms = 30000
multiplier = 2.0
retry_on_rate_limit = true

# Throttle/rate limiting configuration (applies to all LLM calls)
[llm.throttle]
max_concurrent_requests = 10
requests_per_minute = 500
enabled = true
semaphore_enabled = true

# Fallback configuration (applies to all LLM calls)
[llm.fallback]
enabled = true
models = ["gpt-4o-mini", "glm-4-flash"]
on_rate_limit = "retry_then_fallback"
on_timeout = "retry_then_fallback"
on_all_failed = "return_error"

# ============================================================================
# Metrics Configuration (Unified)
# ============================================================================

[metrics]
enabled = true
storage_path = "./workspace/metrics"
retention_days = 30

[metrics.llm]
track_tokens = true
track_latency = true
track_cost = true
cost_per_1k_input_tokens = 0.00015   # gpt-4o-mini pricing
cost_per_1k_output_tokens = 0.0006

[metrics.pilot]
track_decisions = true
track_accuracy = true
track_feedback = true

[metrics.retrieval]
track_paths = true
track_scores = true
track_iterations = true
track_cache = true

# ============================================================================
# Pilot Configuration
# ============================================================================

[pilot]
mode = "Balanced"  # Aggressive | Balanced | Conservative | AlgorithmOnly
guide_at_start = true
guide_at_backtrack = true

[pilot.budget]
max_tokens_per_query = 2000
max_tokens_per_call = 500
max_calls_per_query = 5
max_calls_per_level = 2
hard_limit = true

[pilot.intervention]
fork_threshold = 3
score_gap_threshold = 0.15
low_score_threshold = 0.3
max_interventions_per_level = 2

[pilot.feedback]
enabled = true
storage_path = "./workspace/feedback"
learning_rate = 0.1
min_samples_for_learning = 10

# ============================================================================
# Retrieval Configuration
# ============================================================================

[retrieval]
model = "gpt-4o"
endpoint = "https://api.openai.com/v1"
top_k = 3
max_tokens = 1000
temperature = 0.0

[retrieval.search]
top_k = 5
beam_width = 3
max_iterations = 10
min_score = 0.1

[retrieval.sufficiency]
min_tokens = 500
target_tokens = 2000
max_tokens = 4000
min_content_length = 200
confidence_threshold = 0.7

[retrieval.cache]
max_entries = 1000
ttl_secs = 3600

[retrieval.strategy]
exploration_weight = 1.414
similarity_threshold = 0.5
high_similarity_threshold = 0.8
low_similarity_threshold = 0.3

[retrieval.content]
enabled = true
token_budget = 4000
min_relevance_score = 0.2
scoring_strategy = "hybrid"  # keyword | bm25 | hybrid
output_format = "markdown"
include_scores = false
hierarchical_min_per_level = 0.1
deduplicate = true
dedup_threshold = 0.9

# ============================================================================
# Multi-turn Retrieval Configuration
# ============================================================================

[retrieval.multiturn]
enabled = true
max_sub_queries = 3
decomposition_model = "gpt-4o-mini"
aggregation_strategy = "merge"  # merge | rank | synthesize

# ============================================================================
# Reference Following Configuration
# ============================================================================

[retrieval.reference]
enabled = true
max_depth = 3
max_references = 10
follow_pages = true
follow_tables_figures = true
min_confidence = 0.5

# ============================================================================
# Storage Configuration
# ============================================================================

[storage]
workspace_dir = "./workspace"
cache_size = 100
atomic_writes = true
file_lock = true
checksum_enabled = true

[storage.compression]
enabled = false
algorithm = "gzip"
level = 6

# ============================================================================
# Indexer Configuration
# ============================================================================

[indexer]
subsection_threshold = 300
max_segment_tokens = 3000
max_summary_tokens = 200
min_summary_tokens = 20
"#
        .to_string()
    }

    /// Generate a minimal example TOML file.
    pub fn to_minimal_toml(&self) -> String {
        r#"# Minimal Vectorless Configuration
# Most options have sensible defaults

[llm]
api_key = "your-api-key-here"

[retrieval]
top_k = 5
"#
        .to_string()
    }
}

impl Default for ConfigDocs {
    fn default() -> Self {
        Self::with_defaults()
    }
}

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

    #[test]
    fn test_config_docs_markdown() {
        let docs = ConfigDocs::with_defaults();
        let md = docs.to_markdown();

        assert!(md.contains("# Configuration Reference"));
        assert!(md.contains("## `[llm]`"));
        assert!(md.contains("## `[llm.summary]`"));
        assert!(md.contains("## `[metrics]`"));
        assert!(md.contains("## `[pilot]`"));
        assert!(md.contains("## `[retrieval]`"));
        assert!(md.contains("## `[retrieval.content]`"));
    }

    #[test]
    fn test_config_docs_toml() {
        let docs = ConfigDocs::with_defaults();
        let toml = docs.to_example_toml();

        assert!(toml.contains("[llm]") || toml.contains("[indexer]"));
    }

    #[test]
    fn test_config_docs_minimal_toml() {
        let docs = ConfigDocs::with_defaults();
        let toml = docs.to_minimal_toml();

        assert!(toml.contains("[llm]"));
        assert!(toml.len() < 200); // Should be minimal
    }
}