llm-coding-tools-core 0.1.0

Lightweight, high-performance core types and utilities for coding tools - framework agnostic
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
//! System prompt generation for LLM agents.
//!
//! Provides [`SystemPromptBuilder`] for tracking tools and generating formatted
//! system prompts containing tool usage context.

use crate::context::ToolContext;
use crate::path::AllowedPathResolver;

/// Entry storing tool name and context string.
struct ContextEntry {
    name: &'static str,
    context: &'static str,
}

/// Builder that tracks tools and generates formatted system prompts.
///
/// The environment section is always included and appears before tool listings.
///
/// # Example
///
/// ```no_run
/// use llm_coding_tools_core::context::{ToolContext, READ_ABSOLUTE};
/// use llm_coding_tools_core::SystemPromptBuilder;
///
/// struct ReadTool;
///
/// impl ToolContext for ReadTool {
///     const NAME: &'static str = "read";
///
///     fn context(&self) -> &'static str {
///         READ_ABSOLUTE
///     }
/// }
///
/// let mut pb = SystemPromptBuilder::new()
///     .working_directory(std::env::current_dir().unwrap().display().to_string());
///
/// pb.track(ReadTool);
///
/// let _prompt = pb.build();
/// ```
///
/// # Output
///
/// The generated system prompt is Markdown. For example, with two tools:
///
/// ```text
/// # Environment
///
/// Working directory: /home/user/project
///
/// # Tool Usage Guidelines
///
/// ## `Read` Tool
/// Reads files from disk.
/// ## `Bash` Tool
/// Executes shell commands.
/// ```
#[derive(Default)]
pub struct SystemPromptBuilder {
    entries: Vec<ContextEntry>,
    working_directory: Option<String>,
    allowed_paths: Option<Vec<String>>,
    supplemental: Vec<(&'static str, &'static str)>,
    system_prompt: Option<String>,
}

impl SystemPromptBuilder {
    /// Creates a new system prompt builder.
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Records context and returns tool unchanged.
    ///
    /// Use this to wrap tools before registering them with your tool collection:
    /// ```no_run
    /// use llm_coding_tools_core::context::{ToolContext, READ_ABSOLUTE};
    /// use llm_coding_tools_core::SystemPromptBuilder;
    ///
    /// struct MyTool;
    ///
    /// impl ToolContext for MyTool {
    ///     const NAME: &'static str = "read";
    ///
    ///     fn context(&self) -> &'static str {
    ///         READ_ABSOLUTE
    ///     }
    /// }
    ///
    /// let mut pb = SystemPromptBuilder::new();
    /// let _my_tool = pb.track(MyTool);
    /// // register _my_tool with your tool collection
    /// ```
    ///
    /// For example, if working with rig's agent builder:
    /// ```text
    /// let mut pb = SystemPromptBuilder::new();
    /// let agent = client
    ///     .agent("gpt-4o")
    ///     .tool(pb.track(ReadTool::new()))
    ///     .system prompt(&pb.build())
    ///     .build();
    /// ```
    pub fn track<T: ToolContext>(&mut self, tool: T) -> T {
        self.entries.push(ContextEntry {
            name: T::NAME,
            context: tool.context(),
        });
        tool
    }

    /// Adds supplemental context to the system prompt.
    ///
    /// Supplemental context appears in a separate "Supplemental Context" section
    /// after tool usage guidelines. Use this for guidance that isn't inherent
    /// to a specific tool, such as git workflows or GitHub CLI patterns.
    ///
    /// # Arguments
    ///
    /// * `name` - Section header (e.g., "Git Workflow", "GitHub CLI")
    /// * `context` - Context string content (e.g., [`GIT_WORKFLOW`](crate::context::GIT_WORKFLOW))
    ///
    /// # Examples
    ///
    /// Adding both git and GitHub CLI context:
    ///
    /// ```rust
    /// use llm_coding_tools_core::{SystemPromptBuilder, context};
    ///
    /// let pb = SystemPromptBuilder::new()
    ///     .add_context("Git Workflow", context::GIT_WORKFLOW)
    ///     .add_context("GitHub CLI", context::GITHUB_CLI);
    ///
    /// let prompt = pb.build();
    /// assert!(prompt.contains("# Supplemental Context"));
    /// assert!(prompt.contains("## Git Workflow"));
    /// ```
    ///
    /// Selective inclusion - adding only Git Workflow when not using GitHub features:
    ///
    /// ```rust
    /// use llm_coding_tools_core::{SystemPromptBuilder, context};
    ///
    /// // Only include git workflow for agents that use git but not GitHub
    /// let pb = SystemPromptBuilder::new()
    ///     .add_context("Git Workflow", context::GIT_WORKFLOW);
    ///
    /// let prompt = pb.build();
    /// assert!(prompt.contains("## Git Workflow"));
    /// assert!(!prompt.contains("## GitHub CLI"));
    /// ```
    #[inline]
    pub fn add_context(mut self, name: &'static str, context: &'static str) -> Self {
        self.supplemental.push((name, context));
        self
    }

    /// Sets a custom system prompt that appears first in the generated system prompt.
    ///
    /// The provided prompt is prepended before all other sections (environment,
    /// tools, supplemental context). User provides exactly what they want,
    /// including any markdown headers - no auto-modification is applied.
    ///
    /// # Example
    ///
    /// ```rust
    /// use llm_coding_tools_core::SystemPromptBuilder;
    ///
    /// let pb = SystemPromptBuilder::new()
    ///     .system_prompt("# System Instructions\n\nYou are a helpful assistant.");
    ///
    /// let prompt = pb.build();
    /// assert!(prompt.starts_with("# System Instructions"));
    /// ```
    #[inline]
    pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.system_prompt = Some(prompt.into());
        self
    }
}

impl SystemPromptBuilder {
    /// Sets the working directory to display in the environment section.
    ///
    /// Accepts any type that can be converted to String, including:
    /// - `&str`
    /// - `String`
    /// - `PathBuf` or `&Path` (via `.display().to_string()`)
    ///
    /// # Example
    ///
    /// ```no_run
    /// use llm_coding_tools_core::SystemPromptBuilder;
    ///
    /// let _pb = SystemPromptBuilder::new()
    ///     .working_directory("/home/user/project");
    ///
    /// // With runtime-computed path
    /// let _pb = SystemPromptBuilder::new()
    ///     .working_directory(std::env::current_dir().unwrap().display().to_string());
    /// ```
    #[inline]
    pub fn working_directory(mut self, path: impl Into<String>) -> Self {
        self.working_directory = Some(path.into());
        self
    }

    /// Sets the allowed directories to display in the environment section.
    ///
    /// Takes an [`AllowedPathResolver`] reference and extracts its allowed paths
    /// for display. Paths are already canonicalized (absolute, symlinks resolved)
    /// by the resolver during construction.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use llm_coding_tools_core::{AllowedPathResolver, SystemPromptBuilder};
    ///
    /// let resolver = AllowedPathResolver::new(vec!["/home/user/project", "/tmp"]).unwrap();
    /// let _pb = SystemPromptBuilder::new()
    ///     .working_directory("/home/user/project")
    ///     .allowed_paths(&resolver);
    /// ```
    #[inline]
    pub fn allowed_paths(mut self, resolver: &AllowedPathResolver) -> Self {
        // AllowedPathResolver::allowed_paths() returns &[PathBuf] where paths
        // are already canonicalized (absolute, symlinks resolved) during
        // AllowedPathResolver::new() construction.
        self.allowed_paths = Some(
            resolver
                .allowed_paths()
                .iter()
                .map(|p| p.display().to_string())
                .collect(),
        );
        self
    }
}

/// Returns the separator needed to ensure exactly `\n\n` between content and next section.
///
/// Given a string, determines how many newlines to append so that the result
/// ends with exactly `\n\n` (one blank line). Does not modify the user's content.
#[inline]
fn section_separator(s: &str) -> &'static str {
    if s.ends_with("\n\n") {
        ""
    } else if s.ends_with('\n') {
        "\n"
    } else {
        "\n\n"
    }
}

impl SystemPromptBuilder {
    /// Generates the system prompt string with environment section.
    pub fn build(self) -> String {
        // Environment section size: ~50 bytes header + path length
        // "# Environment\n\nWorking directory: \n\n" = ~38 bytes
        const ENV_HEADER_SIZE: usize = 50;
        // "Allowed directories:\n- " per path + path length
        const ALLOWED_DIR_PER_ITEM: usize = 25;

        let system_prompt_size = self.system_prompt.as_ref().map_or(0, |p| p.len() + 2);

        let env_size = if self.working_directory.is_some() || self.allowed_paths.is_some() {
            ENV_HEADER_SIZE + self.working_directory.as_ref().map_or(0, |d| d.len())
        } else if self.system_prompt.is_some()
            || !self.entries.is_empty()
            || !self.supplemental.is_empty()
        {
            ENV_HEADER_SIZE
        } else {
            0
        };

        let allowed_size = self.allowed_paths.as_ref().map_or(0, |paths| {
            paths.iter().map(|p| p.len() + ALLOWED_DIR_PER_ITEM).sum()
        });

        let tools_size: usize = self
            .entries
            .iter()
            .map(|e| e.context.len() + e.name.len() + 20)
            .sum();

        let supplemental_size: usize = self
            .supplemental
            .iter()
            .map(|(n, c)| c.len() + n.len() + 20)
            .sum();

        let has_tools = !self.entries.is_empty();
        let has_supplemental = !self.supplemental.is_empty();
        let has_system_prompt = self.system_prompt.is_some();
        let has_env_content = self.working_directory.is_some() || self.allowed_paths.is_some();

        let total_size =
            system_prompt_size + env_size + allowed_size + tools_size + supplemental_size + 90;
        let mut output = String::with_capacity(total_size);

        // Return empty if nothing to output
        if !has_tools && !has_supplemental && !has_system_prompt && !has_env_content {
            return String::new();
        }

        // System prompt (first)
        if let Some(ref prompt) = self.system_prompt {
            output.push_str(prompt);
            // Smart separator: ensure exactly one blank line before next section
            output.push_str(section_separator(prompt));
        }

        // Environment section
        if has_env_content || has_system_prompt || has_tools || has_supplemental {
            output.push_str("# Environment\n\n");

            if let Some(ref dir) = self.working_directory {
                output.push_str("Working directory: ");
                output.push_str(dir);
                output.push('\n');
            }

            if let Some(ref paths) = self.allowed_paths {
                output.push_str("Allowed directories:\n");
                for path in paths {
                    output.push_str("- ");
                    output.push_str(path);
                    output.push('\n');
                }
            }

            if (has_tools || has_supplemental) && has_env_content {
                if !output.ends_with('\n') {
                    output.push('\n');
                }
                output.push('\n');
            }
        }

        // Tool section
        if has_tools {
            output.push_str("# Tool Usage Guidelines\n\n");

            for entry in self.entries {
                output.push_str("## `");
                let mut chars = entry.name.chars();
                if let Some(first) = chars.next() {
                    output.push(first.to_ascii_uppercase());
                    output.push_str(chars.as_str());
                } else {
                    output.push_str(entry.name);
                }
                output.push_str("` Tool\n");
                output.push_str(entry.context);
                if !entry.context.ends_with('\n') {
                    output.push('\n');
                }
            }
        }

        // Supplemental context section
        if has_supplemental {
            output.push_str("\n# Supplemental Context\n");

            for (name, context) in self.supplemental {
                output.push_str("## ");
                output.push_str(name);
                output.push('\n');
                output.push_str(context);
                if !context.ends_with('\n') {
                    output.push('\n');
                }
            }
        }

        output.truncate(output.trim_end().len());
        output
    }
}

/// Extension trait for placeholder substitution on system prompt strings.
///
/// Provides simple `{key}` placeholder replacement after building a system prompt.
/// Unmatched placeholders are left as-is.
///
/// # Example
///
/// ```rust
/// use llm_coding_tools_core::Substitute;
///
/// let text = "Available agents: {agents}".to_string();
/// let result = text
///     .substitute("agents", "code-review, research")
///     .substitute("missing", "ignored");
///
/// assert_eq!(result, "Available agents: code-review, research");
/// ```
pub trait Substitute {
    /// Replaces `{key}` placeholder with the given value.
    ///
    /// Returns a new String with the substitution applied.
    /// If the placeholder is not found, returns the string unchanged.
    fn substitute(self, key: &str, value: &str) -> String;

    /// Replaces multiple `{key}` placeholders with their values.
    ///
    /// Accepts an iterator of (key, value) pairs.
    fn substitute_all<'a>(
        self,
        substitutions: impl IntoIterator<Item = (&'a str, &'a str)>,
    ) -> String;
}

impl Substitute for String {
    #[inline]
    fn substitute(self, key: &str, value: &str) -> String {
        let placeholder = format!("{{{}}}", key);
        self.replace(&placeholder, value)
    }

    fn substitute_all<'a>(
        mut self,
        substitutions: impl IntoIterator<Item = (&'a str, &'a str)>,
    ) -> String {
        for (key, value) in substitutions {
            let placeholder = format!("{{{}}}", key);
            self = self.replace(&placeholder, value);
        }
        self
    }
}

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

    struct MockTool {
        id: u32,
    }

    impl ToolContext for MockTool {
        const NAME: &'static str = "mock";
        fn context(&self) -> &'static str {
            "Mock tool context."
        }
    }

    struct OtherTool;

    impl ToolContext for OtherTool {
        const NAME: &'static str = "other";
        fn context(&self) -> &'static str {
            "Other context."
        }
    }

    #[test]
    fn empty_builder_returns_empty_string() {
        let preamble = SystemPromptBuilder::new().build();
        assert!(preamble.is_empty());
    }

    #[test]
    fn track_returns_tool_unchanged() {
        let mut pb = SystemPromptBuilder::new();
        let tool = MockTool { id: 42 };
        let returned = pb.track(tool);
        assert_eq!(returned.id, 42);
    }

    #[test]
    fn single_tool_formats_correctly() {
        let mut pb = SystemPromptBuilder::new().working_directory("/home/user");
        let _ = pb.track(MockTool { id: 1 });
        let preamble = pb.build();

        assert!(preamble.contains("# Environment"));
        assert!(preamble.contains("Working directory: /home/user"));
        assert!(preamble.contains("# Tool Usage Guidelines"));
        assert!(preamble.contains("## `Mock` Tool"));
        assert!(preamble.contains("Mock tool context."));
    }

    #[test]
    fn multiple_tools_preserve_order() {
        let mut pb = SystemPromptBuilder::new().working_directory("/home/user");
        let _ = pb.track(MockTool { id: 1 });
        let _ = pb.track(OtherTool);
        let preamble = pb.build();

        let mock_pos = preamble.find("## `Mock` Tool").unwrap();
        let other_pos = preamble.find("## `Other` Tool").unwrap();
        assert!(
            mock_pos < other_pos,
            "Tools should appear in insertion order"
        );
    }

    #[test]
    fn multiple_tools_have_single_newline_between() {
        let mut pb = SystemPromptBuilder::new().working_directory("/home/user");
        let _ = pb.track(MockTool { id: 1 });
        let _ = pb.track(OtherTool);
        let preamble = pb.build();

        // Verify exact transition: context ends, then next tool header
        assert!(
            preamble.contains("Mock tool context.\n## `Other` Tool"),
            "Expected single newline between tool sections.\nGot:\n{preamble}"
        );

        // Verify single newline after tool header
        assert!(
            preamble.contains("## `Mock` Tool\nMock tool context."),
            "Expected single newline after tool header.\nGot:\n{preamble}"
        );

        // Verify blank line after section header
        assert!(
            preamble.contains("# Tool Usage Guidelines\n\n## `Mock` Tool"),
            "Expected blank line after section header.\nGot:\n{preamble}"
        );

        // Verify no trailing whitespace at end of preamble
        assert_eq!(
            preamble,
            preamble.trim_end(),
            "Preamble has trailing whitespace"
        );
    }

    #[test]
    fn multiple_tools_with_working_dir_have_single_newline_between() {
        let mut pb = SystemPromptBuilder::new().working_directory("/test");
        let _ = pb.track(MockTool { id: 1 });
        let _ = pb.track(OtherTool);
        let preamble = pb.build();

        // Verify exact transition: context ends, then next tool header
        assert!(
            preamble.contains("Mock tool context.\n## `Other` Tool"),
            "Expected single newline between tool sections.\nGot:\n{preamble}"
        );

        // Verify single newline after tool header
        assert!(
            preamble.contains("## `Mock` Tool\nMock tool context."),
            "Expected single newline after tool header.\nGot:\n{preamble}"
        );

        // Verify blank line after Environment header
        assert!(
            preamble.contains("# Environment\n\nWorking directory:"),
            "Expected blank line after Environment header.\nGot:\n{preamble}"
        );

        // Verify blank line after Tool Usage Guidelines header
        assert!(
            preamble.contains("# Tool Usage Guidelines\n\n## `Mock` Tool"),
            "Expected blank line after section header.\nGot:\n{preamble}"
        );

        // Verify no trailing whitespace at end of preamble
        assert_eq!(
            preamble,
            preamble.trim_end(),
            "Preamble has trailing whitespace"
        );
    }

    #[test]
    fn builder_includes_environment_section() {
        let mut pb = SystemPromptBuilder::new().working_directory("/home/user/project");
        let _ = pb.track(MockTool { id: 1 });
        let preamble = pb.build();

        assert!(preamble.contains("# Environment"));
        assert!(preamble.contains("Working directory: /home/user/project"));
        // Environment should come before tools
        let env_pos = preamble.find("# Environment").unwrap();
        let tools_pos = preamble.find("# Tool Usage Guidelines").unwrap();
        assert!(env_pos < tools_pos);
    }

    #[test]
    fn builder_without_env_data_and_tools_returns_empty() {
        let pb = SystemPromptBuilder::new();
        let preamble = pb.build();
        assert!(preamble.is_empty());
    }

    #[test]
    fn builder_with_working_dir_but_no_tools() {
        // Environment section should render even without tools tracked
        let pb = SystemPromptBuilder::new().working_directory("/home/user/project");
        let preamble = pb.build();

        assert!(preamble.contains("# Environment"));
        assert!(preamble.contains("Working directory: /home/user/project"));
        assert!(!preamble.contains("# Tool Usage Guidelines"));
    }

    #[test]
    fn working_directory_accepts_runtime_string() {
        // Simulates std::env::current_dir().unwrap().display().to_string()
        let runtime_path = String::from("/runtime/computed/path");
        let pb = SystemPromptBuilder::new().working_directory(runtime_path);
        let preamble = pb.build();

        assert!(preamble.contains("Working directory: /runtime/computed/path"));
    }

    #[test]
    fn working_directory_accepts_str() {
        let pb = SystemPromptBuilder::new().working_directory("/static/path");
        let preamble = pb.build();

        assert!(preamble.contains("Working directory: /static/path"));
    }

    #[test]
    fn substitute_replaces_single_placeholder() {
        use super::Substitute;

        let text = "Hello {name}!".to_string();
        let result = text.substitute("name", "World");
        assert_eq!(result, "Hello World!");
    }

    #[test]
    fn substitute_leaves_unmatched_placeholders() {
        use super::Substitute;

        let text = "Hello {name}, welcome to {place}!".to_string();
        let result = text.substitute("name", "Alice");
        assert_eq!(result, "Hello Alice, welcome to {place}!");
    }

    #[test]
    fn substitute_handles_empty_value() {
        use super::Substitute;

        let text = "Prefix{middle}Suffix".to_string();
        let result = text.substitute("middle", "");
        assert_eq!(result, "PrefixSuffix");
    }

    #[test]
    fn substitute_all_replaces_multiple() {
        use super::Substitute;

        let text = "Hello {name}, welcome to {place}!".to_string();
        let result = text.substitute_all([("name", "Alice"), ("place", "Wonderland")]);
        assert_eq!(result, "Hello Alice, welcome to Wonderland!");
    }

    #[test]
    fn substitute_no_placeholder_returns_unchanged() {
        use super::Substitute;

        let text = "No placeholders here".to_string();
        let result = text.substitute("missing", "value");
        assert_eq!(result, "No placeholders here");
    }

    #[test]
    fn default_builder_compiles() {
        let _pb_default: SystemPromptBuilder = SystemPromptBuilder::new();
    }

    #[test]
    fn backwards_compatibility_existing_api() {
        // Existing code should work unchanged
        let mut pb = SystemPromptBuilder::new();
        let _ = pb.track(MockTool { id: 1 });
        let preamble = pb.build();

        assert!(preamble.contains("# Tool Usage Guidelines"));
        assert!(preamble.contains("## `Mock` Tool"));
    }

    #[test]
    fn builder_with_allowed_paths_shows_paths() {
        use tempfile::TempDir;

        let dir = TempDir::new().unwrap();
        let resolver = AllowedPathResolver::new(vec![dir.path()]).unwrap();

        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .allowed_paths(&resolver);
        let preamble = pb.build();

        assert!(preamble.contains("# Environment"));
        assert!(preamble.contains("Working directory: /home/user"));
        assert!(preamble.contains("Allowed directories:"));
        // Check that the temp dir path appears (canonicalized)
        assert!(preamble.contains(&dir.path().canonicalize().unwrap().display().to_string()));
    }

    #[test]
    fn builder_with_only_allowed_paths_no_working_dir() {
        use tempfile::TempDir;

        let dir = TempDir::new().unwrap();
        let resolver = AllowedPathResolver::new(vec![dir.path()]).unwrap();

        let pb = SystemPromptBuilder::new().allowed_paths(&resolver);
        let preamble = pb.build();

        assert!(preamble.contains("# Environment"));
        assert!(!preamble.contains("Working directory:"));
        assert!(preamble.contains("Allowed directories:"));
    }

    #[test]
    fn allowed_paths_format_is_bulleted_absolute_paths() {
        use std::path::Path;
        use tempfile::TempDir;

        let dir1 = TempDir::new().unwrap();
        let dir2 = TempDir::new().unwrap();
        let resolver = AllowedPathResolver::new(vec![dir1.path(), dir2.path()]).unwrap();

        let pb = SystemPromptBuilder::new().allowed_paths(&resolver);
        let preamble = pb.build();

        // Check format: "- <absolute_path>" (cross-platform)
        let lines: Vec<&str> = preamble.lines().collect();
        let allowed_idx = lines
            .iter()
            .position(|l| l.contains("Allowed directories"))
            .unwrap();

        for i in 1..=2 {
            let line = lines[allowed_idx + i];
            assert!(
                line.starts_with("- "),
                "Line should start with '- ': {}",
                line
            );
            let path_str = line.strip_prefix("- ").unwrap();
            assert!(
                Path::new(path_str).is_absolute(),
                "Path should be absolute: {}",
                path_str
            );
        }
    }

    #[test]
    fn allowed_paths_appears_after_working_directory() {
        use tempfile::TempDir;

        let dir = TempDir::new().unwrap();
        let resolver = AllowedPathResolver::new(vec![dir.path()]).unwrap();

        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .allowed_paths(&resolver);
        let preamble = pb.build();

        let working_dir_pos = preamble.find("Working directory:").unwrap();
        let allowed_pos = preamble.find("Allowed directories:").unwrap();
        assert!(
            working_dir_pos < allowed_pos,
            "Working directory should appear before allowed paths"
        );
    }

    #[test]
    fn builder_with_only_working_dir_no_allowed_paths() {
        // Only working_directory() should not render "Allowed directories:" section
        let pb = SystemPromptBuilder::new().working_directory("/home/user/project");
        let preamble = pb.build();

        assert!(preamble.contains("# Environment"));
        assert!(preamble.contains("Working directory: /home/user/project"));
        assert!(
            !preamble.contains("Allowed directories:"),
            "Should not render Allowed directories when not explicitly set"
        );
    }

    #[test]
    fn add_context_includes_supplemental_section() {
        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .add_context("Git Workflow", "Git guidance content.");

        let preamble = pb.build();

        assert!(preamble.contains("# Supplemental Context"));
        assert!(preamble.contains("## Git Workflow"));
        assert!(preamble.contains("Git guidance content."));
    }

    #[test]
    fn add_context_appears_after_tools() {
        let mut pb = SystemPromptBuilder::new().add_context("Git Workflow", "Git guidance.");
        let _ = pb.track(MockTool { id: 1 });

        let preamble = pb.build();

        let tools_pos = preamble.find("# Tool Usage Guidelines").unwrap();
        let supplemental_pos = preamble.find("# Supplemental Context").unwrap();
        assert!(
            tools_pos < supplemental_pos,
            "Tools should appear before supplemental context"
        );
    }

    #[test]
    fn add_context_multiple_sections_preserve_order() {
        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .add_context("Git Workflow", "Git content.")
            .add_context("GitHub CLI", "GitHub content.");

        let preamble = pb.build();

        let git_pos = preamble.find("## Git Workflow").unwrap();
        let github_pos = preamble.find("## GitHub CLI").unwrap();
        assert!(
            git_pos < github_pos,
            "Contexts should appear in insertion order"
        );
    }

    #[test]
    fn add_context_only_no_tools() {
        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .add_context("Git Workflow", "Git guidance.");

        let preamble = pb.build();

        assert!(!preamble.contains("# Tool Usage Guidelines"));
        assert!(preamble.contains("# Supplemental Context"));
        assert!(preamble.contains("## Git Workflow"));
    }

    #[test]
    fn add_context_with_env_section() {
        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .add_context("Git Workflow", "Git guidance.");

        let preamble = pb.build();

        let env_pos = preamble.find("# Environment").unwrap();
        let supplemental_pos = preamble.find("# Supplemental Context").unwrap();
        assert!(env_pos < supplemental_pos);
    }

    #[test]
    fn add_context_with_env_and_tools() {
        let mut pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .add_context("Git Workflow", "Git guidance.");
        let _ = pb.track(MockTool { id: 1 });

        let preamble = pb.build();

        let env_pos = preamble.find("# Environment").unwrap();
        let tools_pos = preamble.find("# Tool Usage Guidelines").unwrap();
        let supplemental_pos = preamble.find("# Supplemental Context").unwrap();

        assert!(env_pos < tools_pos);
        assert!(tools_pos < supplemental_pos);
    }

    #[test]
    fn add_context_no_triple_newlines() {
        let mut pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .add_context("Git Workflow", "Git guidance.\n");
        let _ = pb.track(MockTool { id: 1 });

        let preamble = pb.build();

        assert!(
            !preamble.contains("\n\n\n"),
            "Found triple newline in preamble.\nGot:\n{preamble}"
        );
    }

    #[test]
    fn add_context_chains_fluently() {
        // Verify fluent chaining works
        let pb = SystemPromptBuilder::new()
            .add_context("A", "a")
            .add_context("B", "b")
            .add_context("C", "c");

        let preamble = pb.build();

        assert!(preamble.contains("## A"));
        assert!(preamble.contains("## B"));
        assert!(preamble.contains("## C"));
    }

    #[test]
    fn add_context_with_actual_git_workflow_constant() {
        use crate::context::GIT_WORKFLOW;

        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .add_context("Git Workflow", GIT_WORKFLOW);

        let preamble = pb.build();

        assert!(preamble.contains("# Supplemental Context"));
        assert!(preamble.contains("## Git Workflow"));
        // Verify actual content from git_workflow.txt is included
        assert!(
            preamble.contains("Only create commits when requested"),
            "Should contain git commit workflow content"
        );
        assert!(
            preamble.contains("Git Safety Protocol"),
            "Should contain safety protocol section"
        );
    }

    #[test]
    fn add_context_with_actual_github_cli_constant() {
        use crate::context::GITHUB_CLI;

        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .add_context("GitHub CLI", GITHUB_CLI);

        let preamble = pb.build();

        assert!(preamble.contains("# Supplemental Context"));
        assert!(preamble.contains("## GitHub CLI"));
        // Verify actual content from github_cli.txt is included
        assert!(
            preamble.contains("gh pr create"),
            "Should contain gh pr create example"
        );
    }

    #[test]
    fn add_context_selective_inclusion_git_only() {
        use crate::context::{GITHUB_CLI, GIT_WORKFLOW};

        // Only include git workflow (not GitHub CLI)
        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .add_context("Git Workflow", GIT_WORKFLOW);

        let preamble = pb.build();

        assert!(preamble.contains("## Git Workflow"));
        assert!(!preamble.contains("## GitHub CLI"));
        assert!(!preamble.contains(GITHUB_CLI));
    }

    #[test]
    fn add_context_both_git_and_github() {
        use crate::context::{GITHUB_CLI, GIT_WORKFLOW};

        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user")
            .add_context("Git Workflow", GIT_WORKFLOW)
            .add_context("GitHub CLI", GITHUB_CLI);

        let preamble = pb.build();

        assert!(preamble.contains("## Git Workflow"));
        assert!(preamble.contains("## GitHub CLI"));
        // Verify order preserved
        let git_pos = preamble.find("## Git Workflow").unwrap();
        let github_pos = preamble.find("## GitHub CLI").unwrap();
        assert!(
            git_pos < github_pos,
            "Git Workflow should appear before GitHub CLI"
        );
    }

    #[test]
    fn system_prompt_appears_first() {
        let pb = SystemPromptBuilder::new()
            .system_prompt("# System Instructions\n\nYou are a helpful assistant.")
            .working_directory("/home/user");

        let preamble = pb.build();

        assert!(
            preamble.starts_with("# System Instructions"),
            "System prompt should appear first.\nGot:\n{preamble}"
        );

        let system_pos = preamble.find("# System Instructions").unwrap();
        let env_pos = preamble.find("# Environment").unwrap();
        assert!(
            system_pos < env_pos,
            "System prompt should appear before environment section"
        );
    }

    #[test]
    fn system_prompt_appears_before_tools() {
        let mut pb =
            SystemPromptBuilder::new().system_prompt("# Custom Header\n\nMy custom instructions.");
        let _ = pb.track(MockTool { id: 1 });

        let preamble = pb.build();

        let system_pos = preamble.find("# Custom Header").unwrap();
        let tools_pos = preamble.find("# Tool Usage Guidelines").unwrap();
        assert!(
            system_pos < tools_pos,
            "System prompt should appear before tools section"
        );
    }

    #[test]
    fn system_prompt_no_modification() {
        // User provides exact content, no auto-header added
        let custom = "My custom content without header";
        let pb = SystemPromptBuilder::new().system_prompt(custom);

        let preamble = pb.build();

        assert!(
            preamble.starts_with("My custom content without header"),
            "System prompt should not be modified.\nGot:\n{preamble}"
        );
    }

    #[test]
    fn system_prompt_optional_default_behavior() {
        // Without system_prompt, existing behavior preserved
        let mut pb = SystemPromptBuilder::new();
        let _ = pb.track(MockTool { id: 1 });

        let preamble = pb.build();

        assert!(
            preamble.starts_with("# Environment"),
            "Without system prompt, should start with Environment.\nGot:\n{preamble}"
        );
    }

    #[test]
    fn system_prompt_only_produces_output() {
        let pb = SystemPromptBuilder::new()
            .system_prompt("# Just Instructions\n\nOnly system prompt, no tools.");

        let preamble = pb.build();

        assert!(!preamble.is_empty());
        assert!(preamble.contains("# Just Instructions"));
        assert!(!preamble.contains("# Tool Usage Guidelines"));
    }

    #[test]
    fn system_prompt_with_env_and_tools_and_supplemental() {
        let mut pb = SystemPromptBuilder::new()
            .system_prompt("# System\n\nInstructions.")
            .working_directory("/home/user")
            .add_context("Git Workflow", "Git guidance.");
        let _ = pb.track(MockTool { id: 1 });

        let preamble = pb.build();

        let system_pos = preamble.find("# System").unwrap();
        let env_pos = preamble.find("# Environment").unwrap();
        let tools_pos = preamble.find("# Tool Usage Guidelines").unwrap();
        let supplemental_pos = preamble.find("# Supplemental Context").unwrap();

        assert!(system_pos < env_pos);
        assert!(env_pos < tools_pos);
        assert!(tools_pos < supplemental_pos);
    }

    #[test]
    fn system_prompt_no_trailing_newline_gets_separator() {
        // System prompt without trailing newline should get "\n\n" separator
        let mut pb = SystemPromptBuilder::new().system_prompt("# System\n\nNo trailing newline");
        let _ = pb.track(MockTool { id: 1 });

        let preamble = pb.build();

        // Should have exactly one blank line between system prompt and environment
        assert!(
            preamble.contains("No trailing newline\n\n# Environment"),
            "Expected one blank line after system prompt.\nGot:\n{preamble}"
        );
        assert!(
            !preamble.contains("\n\n\n"),
            "Found triple newline in preamble.\nGot:\n{preamble}"
        );
    }

    #[test]
    fn system_prompt_single_trailing_newline_gets_one_more() {
        // System prompt ending with \n should get "\n" to make "\n\n"
        let mut pb =
            SystemPromptBuilder::new().system_prompt("# System\n\nEnds with single newline\n");
        let _ = pb.track(MockTool { id: 1 });

        let preamble = pb.build();

        // Should have exactly one blank line between system prompt and environment
        assert!(
            preamble.contains("Ends with single newline\n\n# Environment"),
            "Expected one blank line after system prompt.\nGot:\n{preamble}"
        );
        assert!(
            !preamble.contains("\n\n\n"),
            "Found triple newline in preamble.\nGot:\n{preamble}"
        );
    }

    #[test]
    fn system_prompt_double_trailing_newline_no_extra() {
        // System prompt ending with \n\n should get no extra separator
        let mut pb =
            SystemPromptBuilder::new().system_prompt("# System\n\nEnds with double newline\n\n");
        let _ = pb.track(MockTool { id: 1 });

        let preamble = pb.build();

        // Should have exactly one blank line between system prompt and environment
        assert!(
            preamble.contains("Ends with double newline\n\n# Environment"),
            "Expected one blank line after system prompt.\nGot:\n{preamble}"
        );
        assert!(
            !preamble.contains("\n\n\n"),
            "Found triple newline in preamble.\nGot:\n{preamble}"
        );
    }

    #[test]
    fn system_prompt_trailing_newlines_with_environment() {
        let pb = SystemPromptBuilder::new()
            .system_prompt("# System\n\nEnds with single newline\n")
            .working_directory("/home/user");

        let preamble = pb.build();

        assert!(
            preamble.contains("Ends with single newline\n\n# Environment"),
            "Expected one blank line after system prompt.\nGot:\n{preamble}"
        );
        assert!(
            !preamble.contains("\n\n\n"),
            "Found triple newline in preamble.\nGot:\n{preamble}"
        );
    }

    #[test]
    fn system_prompt_chains_fluently() {
        // Verify fluent chaining with other methods
        let pb = SystemPromptBuilder::new()
            .system_prompt("# System\n\nContent.")
            .working_directory("/home/user")
            .add_context("A", "a");

        let preamble = pb.build();

        assert!(preamble.contains("# System"));
        assert!(preamble.contains("# Environment"));
        assert!(preamble.contains("# Supplemental Context"));
    }

    #[test]
    fn section_separator_returns_correct_suffix() {
        // Direct unit test for section_separator helper
        assert_eq!(section_separator("no newline"), "\n\n");
        assert_eq!(section_separator("single newline\n"), "\n");
        assert_eq!(section_separator("double newline\n\n"), "");
        assert_eq!(section_separator("triple newline\n\n\n"), "");
        assert_eq!(section_separator(""), "\n\n");
    }

    #[test]
    fn preamble_preview_structure_has_correct_section_order() {
        // Mirrors the example binary to verify structure
        let resolver = AllowedPathResolver::from_canonical(["/home/user/project", "/tmp"]);

        let mut pb = SystemPromptBuilder::new()
            .system_prompt("# System Instructions\n\nYou are helpful.")
            .working_directory("/home/user/project")
            .allowed_paths(&resolver)
            .add_context("Git Workflow", "Git guidance content.")
            .add_context("GitHub CLI", "GitHub guidance content.");

        let _ = pb.track(MockTool { id: 1 });
        let _ = pb.track(OtherTool);

        let preamble = pb.build();

        // Verify all sections present
        assert!(
            preamble.contains("# System Instructions"),
            "Missing system prompt"
        );
        assert!(
            preamble.contains("# Environment"),
            "Missing environment section"
        );
        assert!(
            preamble.contains("Working directory:"),
            "Missing working directory"
        );
        assert!(
            preamble.contains("Allowed directories:"),
            "Missing allowed directories"
        );
        assert!(
            preamble.contains("# Tool Usage Guidelines"),
            "Missing tools section"
        );
        assert!(
            preamble.contains("# Supplemental Context"),
            "Missing supplemental section"
        );

        // Verify section order: system -> env -> tools -> supplemental
        let system_pos = preamble.find("# System Instructions").unwrap();
        let env_pos = preamble.find("# Environment").unwrap();
        let tools_pos = preamble.find("# Tool Usage Guidelines").unwrap();
        let supplemental_pos = preamble.find("# Supplemental Context").unwrap();

        assert!(
            system_pos < env_pos,
            "System prompt should come before environment"
        );
        assert!(env_pos < tools_pos, "Environment should come before tools");
        assert!(
            tools_pos < supplemental_pos,
            "Tools should come before supplemental"
        );

        // Verify no formatting issues
        assert!(
            !preamble.contains("\n\n\n"),
            "Found triple newline (double blank line)"
        );
        assert_eq!(
            preamble,
            preamble.trim_end(),
            "Preamble has trailing whitespace"
        );
    }

    #[test]
    fn preamble_preview_allowed_paths_rendered_correctly() {
        let resolver = AllowedPathResolver::from_canonical(["/home/user/project", "/tmp"]);

        let pb = SystemPromptBuilder::new()
            .working_directory("/home/user/project")
            .allowed_paths(&resolver);

        let preamble = pb.build();

        // Verify both paths appear as bullet points
        assert!(
            preamble.contains("- /home/user/project"),
            "Missing project path"
        );
        assert!(preamble.contains("- /tmp"), "Missing tmp path");
    }
}