a3s-code-core 3.2.0

A3S Code Core - Embeddable AI agent library with tool execution
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
// Prompt Registry
//
// Central registry for all system prompts and prompt templates used in A3S Code.
// Every LLM-facing prompt is externalized here as a compile-time `include_str!`
// so the full agentic design is visible in one place.
//
// Directory layout:
//   prompts/
//   ├── common/   — shared runtime prompts used by multiple subsystems
//   ├── analysis/ — intent classification and pre-analysis prompts
//   ├── planning/ — planner and plan execution prompts
//   └── agents/   — built-in delegated-agent role prompts

// ============================================================================
// Default System Prompt
// ============================================================================

use crate::llm::LlmClient;
use anyhow::Context;

/// Default agentic system prompt — injected when no system prompt is configured.
///
/// Instructs the LLM to behave as an autonomous coding agent: use tools to act,
/// verify results, and keep working until the task is fully complete.
pub const SYSTEM_DEFAULT: &str = include_str!("../prompts/common/system_default.md");

/// Continuation message — injected as a user turn when the LLM stops without
/// completing the task (i.e. stops calling tools mid-task).
pub const CONTINUATION: &str = include_str!("../prompts/common/continuation.md");

// ============================================================================
// Delegated Run Prompts
// ============================================================================

/// Explore delegated run — read-only codebase exploration
pub const AGENT_EXPLORE: &str = include_str!("../prompts/agents/explore.md");

/// Plan delegated run — read-only planning and analysis
pub const AGENT_PLAN: &str = include_str!("../prompts/agents/plan.md");

/// Code review delegated run — issue finding and review focus
pub const AGENT_CODE_REVIEW: &str = include_str!("../prompts/agents/code_review.md");

// ============================================================================
// Session — Context Compaction
// ============================================================================

/// User template for context compaction. Placeholder: `{conversation}`
pub const CONTEXT_COMPACT: &str = include_str!("../prompts/common/context_compact.md");

/// Prefix for compacted summary messages
pub const CONTEXT_SUMMARY_PREFIX: &str =
    include_str!("../prompts/common/context_summary_prefix.md");

// ============================================================================
// LLM Planner — JSON-structured prompts
// ============================================================================

/// System prompt for LLM planner: plan creation (JSON output)
pub const LLM_PLAN_SYSTEM: &str = include_str!("../prompts/planning/llm_plan_system.md");

/// System prompt for LLM planner: goal extraction (JSON output)
pub const LLM_GOAL_EXTRACT_SYSTEM: &str =
    include_str!("../prompts/planning/llm_goal_extract_system.md");

/// System prompt for LLM planner: goal achievement check (JSON output)
pub const LLM_GOAL_CHECK_SYSTEM: &str =
    include_str!("../prompts/planning/llm_goal_check_system.md");

/// System prompt for pre-analysis: combined intent + goal + plan + input optimization.
pub const PRE_ANALYSIS_SYSTEM: &str = include_str!("../prompts/analysis/pre_analysis_system.md");

// ============================================================================
// Plan Execution Templates
// ============================================================================

/// Template for initial plan execution message
pub const PLAN_EXECUTE_GOAL: &str = include_str!("../prompts/planning/plan_execute_goal.md");

/// Template for per-step execution prompt
pub const PLAN_EXECUTE_STEP: &str = include_str!("../prompts/planning/plan_execute_step.md");

/// Template for fallback plan step description
pub const PLAN_FALLBACK_STEP: &str = include_str!("../prompts/planning/plan_fallback_step.md");

/// Skill catalog header injected before listing available skill names/descriptions.
pub const SKILLS_CATALOG_HEADER: &str = include_str!("../prompts/common/skills_catalog_header.md");

// ============================================================================
// Verification Agent
// ============================================================================

/// Verification agent — adversarial specialist that tries to break code
pub const AGENT_VERIFICATION: &str = include_str!("../prompts/agents/verification.md");

// ============================================================================
// Intent Classification
// ============================================================================

/// System prompt for LLM-based intent classification
pub const INTENT_CLASSIFY_SYSTEM: &str =
    include_str!("../prompts/analysis/intent_classify_system.md");

// ============================================================================
// Planning Mode (Auto-Detection)
// ============================================================================

use serde::{Deserialize, Serialize};

/// Planning mode — controls when planning phase is used.
///
/// When set to `Auto` (the default), the system detects from the user's
/// message whether planning should be enabled. When explicitly `Enabled`,
/// planning runs on every execution. When `Disabled`, planning is skipped.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum PlanningMode {
    /// Automatically detect from message content — enables planning when the
    /// message benefits from structured pre-analysis. Local keyword detection is
    /// only a fallback when pre-analysis is unavailable.
    #[default]
    Auto,
    /// Explicitly disabled — never use planning phase.
    Disabled,
    /// Explicitly enabled — always use planning phase.
    Enabled,
}

impl PlanningMode {
    /// Returns true for the local no-LLM fallback path.
    ///
    /// Normal agent execution runs pre-analysis in `Auto` mode and uses its
    /// structured `requires_planning` decision instead of this heuristic.
    pub fn should_plan(&self, message: &str) -> bool {
        match self {
            PlanningMode::Auto => AgentStyle::detect_from_message(message).requires_planning(),
            PlanningMode::Enabled => true,
            PlanningMode::Disabled => false,
        }
    }
}

// ============================================================================
// Agent Style (Intent-Based Prompt Selection)
// ============================================================================

/// Agent style — determines which system prompt template is used.
///
/// Each style has a different focus and behavior, selected based on the user's
/// apparent intent from their message.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AgentStyle {
    /// Default — general purpose coding agent for research and multi-step tasks.
    #[default]
    GeneralPurpose,
    /// Read-only planning and architecture analysis.
    /// Prohibited from modifying files, focuses on design and planning.
    Plan,
    /// Adversarial verification specialist — tries to break code, not confirm it works.
    Verification,
    /// Fast file search and codebase exploration.
    /// Read-only, optimized for finding files and patterns quickly.
    Explore,
    /// Code review focused — analyzes code quality, best practices, potential issues.
    CodeReview,
}

/// Detection confidence level for style detection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DetectionConfidence {
    /// High confidence — very specific keywords, skip LLM classification.
    High,
    /// Medium confidence — some indicators present, LLM classification helpful.
    Medium,
    /// Low confidence — no clear indicators, LLM classification recommended.
    Low,
}

impl AgentStyle {
    /// Returns the base system prompt for this style.
    pub fn base_prompt(&self) -> &'static str {
        match self {
            AgentStyle::GeneralPurpose => SYSTEM_DEFAULT,
            AgentStyle::Plan => AGENT_PLAN,
            AgentStyle::Verification => AGENT_VERIFICATION,
            AgentStyle::Explore => AGENT_EXPLORE,
            AgentStyle::CodeReview => AGENT_CODE_REVIEW,
        }
    }

    /// Returns style-specific guidelines if any.
    pub fn guidelines(&self) -> Option<&'static str> {
        match self {
            AgentStyle::GeneralPurpose => None,
            AgentStyle::Plan => None, // Already embedded in agents/plan.md
            AgentStyle::Verification => None, // Already embedded in agents/verification.md
            AgentStyle::Explore => None, // Already embedded in agents/explore.md
            AgentStyle::CodeReview => None,
        }
    }

    /// Returns a one-line description of this style.
    pub fn description(&self) -> &'static str {
        match self {
            AgentStyle::GeneralPurpose => {
                "General purpose coding agent for research and multi-step tasks"
            }
            AgentStyle::Plan => "Read-only planning and architecture analysis agent",
            AgentStyle::Verification => "Adversarial verification specialist — tries to break code",
            AgentStyle::Explore => "Fast read-only file search and codebase exploration agent",
            AgentStyle::CodeReview => "Code review focused — analyzes quality and best practices",
        }
    }

    /// Returns the canonical built-in delegated-agent name for this style.
    pub fn builtin_agent_name(&self) -> &'static str {
        match self {
            AgentStyle::GeneralPurpose => "general",
            AgentStyle::Plan => "plan",
            AgentStyle::Verification => "verification",
            AgentStyle::Explore => "explore",
            AgentStyle::CodeReview => "review",
        }
    }

    /// Returns the stable runtime mode label for UI/event consumers.
    pub fn runtime_mode(&self) -> &'static str {
        match self {
            AgentStyle::GeneralPurpose => "general",
            AgentStyle::Plan => "planning",
            AgentStyle::Verification => "verification",
            AgentStyle::Explore => "explore",
            AgentStyle::CodeReview => "code_review",
        }
    }

    /// Returns true if this style benefits from a planning phase.
    ///
    /// Planning is beneficial for styles that involve multi-step execution
    /// or where a structured approach improves outcomes.
    pub fn requires_planning(&self) -> bool {
        matches!(self, AgentStyle::Plan)
    }

    /// Detects the most appropriate agent style based on user message content,
    /// along with a confidence level.
    ///
    /// This is a local fallback for environments where LLM pre-analysis is
    /// unavailable. Normal execution uses structured pre-analysis first.
    pub fn detect_with_confidence(message: &str) -> (Self, DetectionConfidence) {
        // Chinese text has high ambiguity in intent classification due to
        // compound verb structures and context-dependent meaning.
        // Bypass keyword matching entirely and route to LLM classification.
        if message
            .chars()
            .any(|c| ('\u{4e00}'..='\u{9fff}').contains(&c))
        {
            return (AgentStyle::GeneralPurpose, DetectionConfidence::Low);
        }

        let lower = message.to_lowercase();

        // === HIGH CONFIDENCE: Very specific patterns ===

        // Strong verification indicators
        if lower.contains("try to break")
            || lower.contains("find vulnerabilities")
            || lower.contains("adversarial")
            || lower.contains("security audit")
        {
            return (AgentStyle::Verification, DetectionConfidence::High);
        }

        // Strong plan indicators
        if lower.contains("help me plan")
            || lower.contains("help me design")
            || lower.contains("create a plan")
            || lower.contains("implementation plan")
            || lower.contains("step-by-step plan")
        {
            return (AgentStyle::Plan, DetectionConfidence::High);
        }

        // Strong exploration indicators
        if lower.contains("find all files")
            || lower.contains("search for all")
            || lower.contains("locate all")
        {
            return (AgentStyle::Explore, DetectionConfidence::High);
        }

        // === MEDIUM CONFIDENCE: Specific but less definitive ===

        // Verification keywords
        if lower.contains("verify")
            || lower.contains("verification")
            || lower.contains("break")
            || lower.contains("debug")
            || lower.contains("test")
            || lower.contains("check if")
        {
            return (AgentStyle::Verification, DetectionConfidence::Medium);
        }

        // Plan keywords
        if lower.contains("plan")
            || lower.contains("design")
            || lower.contains("architecture")
            || lower.contains("approach")
        {
            return (AgentStyle::Plan, DetectionConfidence::Medium);
        }

        // Explore keywords
        if lower.contains("find")
            || lower.contains("search")
            || lower.contains("where is")
            || lower.contains("where's")
            || lower.contains("locate")
            || lower.contains("explore")
            || lower.contains("look for")
        {
            return (AgentStyle::Explore, DetectionConfidence::Medium);
        }

        // Code review keywords
        if lower.contains("review")
            || lower.contains("code review")
            || lower.contains("analyze")
            || lower.contains("assess")
            || lower.contains("quality")
            || lower.contains("best practice")
        {
            return (AgentStyle::CodeReview, DetectionConfidence::Medium);
        }

        // No clear indicators
        (AgentStyle::GeneralPurpose, DetectionConfidence::Low)
    }

    /// Detects the most appropriate agent style based on user message content.
    ///
    /// This is a local fallback heuristic. Normal execution uses structured
    /// pre-analysis first; users can also explicitly set the style via
    /// `SystemPromptSlots::with_style()`.
    pub fn detect_from_message(message: &str) -> Self {
        Self::detect_with_confidence(message).0
    }

    /// Classifies user intent using LLM when keyword confidence is low.
    ///
    /// This helper is available to callers that want explicit one-shot intent
    /// classification outside the main pre-analysis path.
    ///
    /// Uses a lightweight classification prompt that returns a single word.
    pub async fn detect_with_llm(llm: &dyn LlmClient, message: &str) -> anyhow::Result<Self> {
        use crate::llm::Message;

        let system = INTENT_CLASSIFY_SYSTEM;
        let messages = vec![Message::user(message)];

        let response = llm
            .complete(&messages, Some(system), &[])
            .await
            .context("LLM intent classification failed")?;

        let text = response.text().trim().to_lowercase();

        let style = match text.as_str() {
            "plan" => AgentStyle::Plan,
            "explore" => AgentStyle::Explore,
            "verification" => AgentStyle::Verification,
            "codereview" | "code review" => AgentStyle::CodeReview,
            _ => AgentStyle::GeneralPurpose,
        };

        Ok(style)
    }
}

// ============================================================================
// System Prompt Slots
// ============================================================================

/// Slot-based system prompt customization with intent-based style selection.
///
/// Users can customize specific parts of the system prompt without overriding
/// the core agentic capabilities (tool usage, autonomous behavior, completion
/// criteria). The default agentic core is ALWAYS included.
///
/// ## Assembly Order
///
/// ```text
/// [role]            ← Custom identity/role (e.g. "You are a Python expert")
/// [CORE]            ← Always present: Core Behaviour + Tool Usage Strategy + Completion Criteria
/// [guidelines]      ← Custom coding rules / constraints
/// [response_style]  ← Custom response format (replaces default Response Format section)
/// [extra]           ← Freeform additional instructions
/// ```
///
/// ## Intent-Based Selection
///
/// When `style` is left as `AgentStyle::GeneralPurpose` (the default), the
/// system will attempt to detect the user's intent from their first message and
/// automatically select an appropriate style. To override this behavior, explicitly
/// set the `style` field.
#[derive(Debug, Clone, Default)]
pub struct SystemPromptSlots {
    /// Agent style — determines which base prompt template is used.
    ///
    /// When `None` (default), the style is auto-detected from the user's message.
    /// Explicitly set this to force a particular style regardless of message content.
    pub style: Option<AgentStyle>,

    /// Custom role/identity prepended before the core prompt.
    ///
    /// Example: "You are a senior Python developer specializing in FastAPI."
    /// When set, replaces the default "You are A3S Code, an expert AI coding agent" line.
    pub role: Option<String>,

    /// Custom coding guidelines appended after the core prompt sections.
    ///
    /// Example: "Always use type hints. Follow PEP 8. Prefer dataclasses over dicts."
    pub guidelines: Option<String>,

    /// Custom response style that replaces the default "Response Format" section.
    ///
    /// When `None`, the default response format is used.
    pub response_style: Option<String>,

    /// Freeform extra instructions appended at the very end.
    pub extra: Option<String>,
}

/// The default role line in SYSTEM_DEFAULT that gets replaced when `role` slot is set.
const DEFAULT_ROLE_LINE: &str = include_str!("../prompts/common/system_default_role_line.md");

/// The default response format section.
const DEFAULT_RESPONSE_FORMAT: &str =
    include_str!("../prompts/common/system_default_response_format.md");

impl SystemPromptSlots {
    /// Build the final system prompt by assembling slots around the core prompt.
    ///
    /// The core agentic behavior (Core Behaviour, Tool Usage Strategy, Completion
    /// Criteria) is always preserved. Users can only customize the edges.
    ///
    /// Note: This uses `AgentStyle::GeneralPurpose` as the base. Use
    /// `build_with_message()` to enable automatic intent-based style detection.
    pub fn build(&self) -> String {
        self.build_with_style(self.style.unwrap_or_default())
    }

    /// Build the final system prompt, auto-detecting style from the initial message.
    ///
    /// If `self.style` is explicitly set, that style is used regardless of message content.
    /// Otherwise, the style is detected from `initial_message` using keyword analysis.
    pub fn build_with_message(&self, initial_message: &str) -> String {
        let style = self
            .style
            .unwrap_or_else(|| AgentStyle::detect_from_message(initial_message));
        self.build_with_style(style)
    }

    /// Build the prompt with an explicitly specified style.
    fn build_with_style(&self, style: AgentStyle) -> String {
        let mut parts: Vec<String> = Vec::new();

        // Normalize line endings: strip \r so string matching works on Windows
        // where include_str! may produce \r\n if the file has CRLF endings.
        let base_prompt = style.base_prompt().replace('\r', "");
        let default_role_line = DEFAULT_ROLE_LINE.replace('\r', "");
        let default_response_format = DEFAULT_RESPONSE_FORMAT.replace('\r', "");

        // 1. Role: for GeneralPurpose, replace the default role line.
        // For other styles (Plan, Explore, Verification), prepend custom role since
        // those prompts have their own identity embedded.
        let core = if let Some(ref role) = self.role {
            if style == AgentStyle::GeneralPurpose {
                let custom_role = format!(
                    "{}. You operate in an agentic loop: inspect, act with tools, observe results, and continue until the user's request is genuinely complete.",
                    role.trim_end_matches('.')
                );
                base_prompt.replace(&default_role_line, &custom_role)
            } else {
                // Prepend custom role for other styles
                format!("{}\n\n{}", role, base_prompt)
            }
        } else {
            base_prompt
        };

        // 2. Core: strip the default response format section if custom one is provided
        let core = if self.response_style.is_some() {
            core.replace(&default_response_format, "")
                .trim_end()
                .to_string()
        } else {
            core.trim_end().to_string()
        };

        parts.push(core);

        // 3. Custom response style (replaces default Response Format)
        if let Some(ref style) = self.response_style {
            parts.push(format!("## Response Format\n\n{}", style));
        }

        // 4. Guidelines: style-specific + custom
        let style_guidelines = style.guidelines();
        if style_guidelines.is_some() || self.guidelines.is_some() {
            let mut guidelines_parts = Vec::new();
            if let Some(sg) = style_guidelines {
                guidelines_parts.push(sg.to_string());
            }
            if let Some(ref g) = self.guidelines {
                guidelines_parts.push(g.clone());
            }
            parts.push(format!(
                "## Guidelines\n\n{}",
                guidelines_parts.join("\n\n")
            ));
        }

        // 5. Extra freeform instructions.
        if let Some(ref extra) = self.extra {
            parts.push(extra.clone());
        }

        parts.join("\n\n")
    }

    /// Returns true if all slots are empty (use pure default prompt).
    pub fn is_empty(&self) -> bool {
        self.style.is_none()
            && self.role.is_none()
            && self.guidelines.is_none()
            && self.response_style.is_none()
            && self.extra.is_none()
    }

    /// Set the agent style explicitly.
    pub fn with_style(mut self, style: AgentStyle) -> Self {
        self.style = Some(style);
        self
    }

    /// Set the role/identity.
    pub fn with_role(mut self, role: impl Into<String>) -> Self {
        self.role = Some(role.into());
        self
    }

    /// Set custom guidelines.
    pub fn with_guidelines(mut self, guidelines: impl Into<String>) -> Self {
        self.guidelines = Some(guidelines.into());
        self
    }

    /// Set custom response style.
    pub fn with_response_style(mut self, style: impl Into<String>) -> Self {
        self.response_style = Some(style.into());
        self
    }

    /// Set extra instructions.
    pub fn with_extra(mut self, extra: impl Into<String>) -> Self {
        self.extra = Some(extra.into());
        self
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Render a template by replacing `{key}` placeholders with values
pub fn render(template: &str, vars: &[(&str, &str)]) -> String {
    let mut result = template.to_string();
    for (key, value) in vars {
        result = result.replace(&format!("{{{}}}", key), value);
    }
    result
}

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

    #[test]
    fn test_all_prompts_loaded() {
        // Verify all prompts are non-empty at compile time
        assert!(!SYSTEM_DEFAULT.is_empty());
        assert!(!CONTINUATION.is_empty());
        assert!(!AGENT_EXPLORE.is_empty());
        assert!(!AGENT_PLAN.is_empty());
        assert!(!AGENT_CODE_REVIEW.is_empty());
        assert!(!CONTEXT_COMPACT.is_empty());
        assert!(!LLM_PLAN_SYSTEM.is_empty());
        assert!(!LLM_GOAL_EXTRACT_SYSTEM.is_empty());
        assert!(!LLM_GOAL_CHECK_SYSTEM.is_empty());
        assert!(!SKILLS_CATALOG_HEADER.is_empty());
        assert!(!PLAN_EXECUTE_GOAL.is_empty());
        assert!(!PLAN_EXECUTE_STEP.is_empty());
        assert!(!PLAN_FALLBACK_STEP.is_empty());
    }

    #[test]
    fn test_render_template() {
        let result = render(
            PLAN_EXECUTE_GOAL,
            &[("goal", "Build app"), ("steps", "1. Init")],
        );
        assert!(result.contains("Build app"));
        assert!(!result.contains("{goal}"));
    }

    #[test]
    fn test_render_multiple_placeholders() {
        let template = "Goal: {goal}\nCriteria: {criteria}\nState: {current_state}";
        let result = render(
            template,
            &[
                ("goal", "Build a REST API"),
                ("criteria", "- Endpoint works\n- Tests pass"),
                ("current_state", "API is deployed"),
            ],
        );
        assert!(result.contains("Build a REST API"));
        assert!(result.contains("Endpoint works"));
        assert!(result.contains("API is deployed"));
    }

    #[test]
    fn test_delegated_agent_prompts_contain_guidelines() {
        assert!(AGENT_EXPLORE.contains("Guidelines"));
        assert!(AGENT_EXPLORE.contains("read-only"));
        assert!(AGENT_PLAN.contains("Guidelines"));
        assert!(AGENT_PLAN.contains("read-only"));
    }

    #[test]
    fn test_context_summary_prefix() {
        assert!(CONTEXT_SUMMARY_PREFIX.contains("Context Summary"));
    }

    // ── SystemPromptSlots tests ──

    #[test]
    fn test_slots_default_builds_system_default() {
        let slots = SystemPromptSlots::default();
        let built = slots.build();
        assert!(built.contains("Core Behaviour"));
        assert!(built.contains("Tool Usage Strategy"));
        assert!(built.contains("Completion Criteria"));
        assert!(built.contains("Response Format"));
        assert!(built.contains("A3S Code"));
    }

    #[test]
    fn test_slots_custom_role_replaces_default() {
        let slots = SystemPromptSlots {
            role: Some("You are a senior Python developer".to_string()),
            ..Default::default()
        };
        let built = slots.build();
        assert!(built.contains("You are a senior Python developer"));
        assert!(!built.contains("You are A3S Code"));
        // Core sections still present
        assert!(built.contains("Core Behaviour"));
        assert!(built.contains("Tool Usage Strategy"));
    }

    #[test]
    fn test_slots_custom_guidelines_appended() {
        let slots = SystemPromptSlots {
            guidelines: Some("Always use type hints. Follow PEP 8.".to_string()),
            ..Default::default()
        };
        let built = slots.build();
        assert!(built.contains("## Guidelines"));
        assert!(built.contains("Always use type hints"));
        assert!(built.contains("Core Behaviour"));
    }

    #[test]
    fn test_slots_custom_response_style_replaces_default() {
        let slots = SystemPromptSlots {
            response_style: Some("Be concise. Use bullet points.".to_string()),
            ..Default::default()
        };
        let built = slots.build();
        assert!(built.contains("Be concise. Use bullet points."));
        // Default response format content should be gone
        assert!(!built.contains("keep progress notes brief and useful"));
        // But core is still there
        assert!(built.contains("Core Behaviour"));
    }

    #[test]
    fn test_slots_extra_appended() {
        let slots = SystemPromptSlots {
            extra: Some("Remember: always write tests first.".to_string()),
            ..Default::default()
        };
        let built = slots.build();
        assert!(built.contains("Remember: always write tests first."));
        assert!(built.contains("Core Behaviour"));
    }

    #[test]
    fn test_slots_with_extra() {
        let slots = SystemPromptSlots::default().with_extra("You are a helpful assistant.");
        let built = slots.build();
        assert!(built.contains("You are a helpful assistant."));
        assert!(built.contains("Core Behaviour"));
        assert!(built.contains("Tool Usage Strategy"));
    }

    #[test]
    fn test_slots_all_slots_combined() {
        let slots = SystemPromptSlots {
            style: None,
            role: Some("You are a Rust expert".to_string()),
            guidelines: Some("Use clippy. No unwrap.".to_string()),
            response_style: Some("Short answers only.".to_string()),
            extra: Some("Project uses tokio.".to_string()),
        };
        let built = slots.build();
        assert!(built.contains("You are a Rust expert"));
        assert!(built.contains("Core Behaviour"));
        assert!(built.contains("## Guidelines"));
        assert!(built.contains("Use clippy"));
        assert!(built.contains("Short answers only"));
        assert!(built.contains("Project uses tokio"));
        // Default response format replaced
        assert!(!built.contains("keep progress notes brief and useful"));
    }

    #[test]
    fn test_slots_is_empty() {
        assert!(SystemPromptSlots::default().is_empty());
        assert!(!SystemPromptSlots {
            role: Some("test".to_string()),
            ..Default::default()
        }
        .is_empty());
        assert!(!SystemPromptSlots {
            style: Some(AgentStyle::Plan),
            ..Default::default()
        }
        .is_empty());
    }

    // ── AgentStyle tests ──

    #[test]
    fn test_agent_style_default_is_general_purpose() {
        assert_eq!(AgentStyle::default(), AgentStyle::GeneralPurpose);
    }

    #[test]
    fn test_agent_style_base_prompt() {
        assert_eq!(AgentStyle::GeneralPurpose.base_prompt(), SYSTEM_DEFAULT);
        assert_eq!(AgentStyle::Plan.base_prompt(), AGENT_PLAN);
        assert_eq!(AgentStyle::Explore.base_prompt(), AGENT_EXPLORE);
        assert_eq!(AgentStyle::Verification.base_prompt(), AGENT_VERIFICATION);
        assert_eq!(AgentStyle::CodeReview.base_prompt(), AGENT_CODE_REVIEW);
    }

    #[test]
    fn test_agent_style_guidelines() {
        assert!(AgentStyle::GeneralPurpose.guidelines().is_none());
        assert!(AgentStyle::Plan.guidelines().is_none()); // embedded in prompt
        assert!(AgentStyle::Explore.guidelines().is_none());
        assert!(AgentStyle::Verification.guidelines().is_none());
        assert!(AgentStyle::CodeReview.guidelines().is_none());
    }

    #[test]
    fn test_agent_style_builtin_agent_name_mapping() {
        assert_eq!(AgentStyle::GeneralPurpose.builtin_agent_name(), "general");
        assert_eq!(AgentStyle::Plan.builtin_agent_name(), "plan");
        assert_eq!(AgentStyle::Explore.builtin_agent_name(), "explore");
        assert_eq!(
            AgentStyle::Verification.builtin_agent_name(),
            "verification"
        );
        assert_eq!(AgentStyle::CodeReview.builtin_agent_name(), "review");
    }

    #[test]
    fn test_agent_style_runtime_mode_mapping() {
        assert_eq!(AgentStyle::GeneralPurpose.runtime_mode(), "general");
        assert_eq!(AgentStyle::Plan.runtime_mode(), "planning");
        assert_eq!(AgentStyle::Explore.runtime_mode(), "explore");
        assert_eq!(AgentStyle::Verification.runtime_mode(), "verification");
        assert_eq!(AgentStyle::CodeReview.runtime_mode(), "code_review");
    }

    #[test]
    fn test_agent_style_detect_plan() {
        assert_eq!(
            AgentStyle::detect_from_message("Help me plan a new feature"),
            AgentStyle::Plan
        );
        assert_eq!(
            AgentStyle::detect_from_message("Design the architecture for this"),
            AgentStyle::Plan
        );
        assert_eq!(
            AgentStyle::detect_from_message("What's the implementation approach?"),
            AgentStyle::Plan
        );
    }

    #[test]
    fn test_agent_style_detect_verification() {
        assert_eq!(
            AgentStyle::detect_from_message("Verify that this works correctly"),
            AgentStyle::Verification
        );
        assert_eq!(
            AgentStyle::detect_from_message("Test the login flow"),
            AgentStyle::Verification
        );
        assert_eq!(
            AgentStyle::detect_from_message("Check if the API handles edge cases"),
            AgentStyle::Verification
        );
    }

    #[test]
    fn test_agent_style_detect_explore() {
        assert_eq!(
            AgentStyle::detect_from_message("Find all files related to auth"),
            AgentStyle::Explore
        );
        assert_eq!(
            AgentStyle::detect_from_message("Where is the user model defined?"),
            AgentStyle::Explore
        );
        assert_eq!(
            AgentStyle::detect_from_message("Search for password hashing code"),
            AgentStyle::Explore
        );
    }

    #[test]
    fn test_agent_style_detect_code_review() {
        assert_eq!(
            AgentStyle::detect_from_message("Review the PR changes"),
            AgentStyle::CodeReview
        );
        assert_eq!(
            AgentStyle::detect_from_message("Analyze this code for best practices"),
            AgentStyle::CodeReview
        );
        assert_eq!(
            AgentStyle::detect_from_message("Assess code quality"),
            AgentStyle::CodeReview
        );
    }

    #[test]
    fn test_agent_style_detect_default_is_general_purpose() {
        // "Implement" was removed from Plan keywords as too generic
        assert_eq!(
            AgentStyle::detect_from_message("Implement the new feature"),
            AgentStyle::GeneralPurpose
        );
        // "Write tests" contains "test" so it's detected as Verification
        assert_eq!(
            AgentStyle::detect_from_message("Write code for the API"),
            AgentStyle::GeneralPurpose
        );
    }

    #[test]
    fn test_build_with_message_auto_detects_style() {
        let slots = SystemPromptSlots::default();
        let built = slots.build_with_message("Help me plan a new feature");
        // Should use Plan style
        assert!(built.contains("planning agent") || built.contains("READ-ONLY"));
    }

    #[test]
    fn test_build_with_message_explicit_style_overrides() {
        let slots = SystemPromptSlots {
            style: Some(AgentStyle::Verification),
            ..Default::default()
        };
        let built = slots.build_with_message("Help me plan a new feature");
        // Should use Verification style, not Plan
        assert!(built.contains("adversarial verification specialist"));
    }

    #[test]
    fn test_build_with_message_plan_style() {
        let slots = SystemPromptSlots::default();
        let built = slots.build_with_message("Design the system architecture");
        assert!(built.contains("planning agent") || built.contains("READ-ONLY"));
    }

    #[test]
    fn test_build_with_message_explore_style() {
        let slots = SystemPromptSlots::default();
        let built = slots.build_with_message("Find all authentication files");
        assert!(built.contains("exploration agent") || built.contains("explore"));
    }

    #[test]
    fn test_build_with_message_code_review_style() {
        let slots = SystemPromptSlots::default();
        let built = slots.build_with_message("Review this code");
        assert!(built.contains("code review agent"));
        assert!(built.contains("regressions"));
    }

    #[test]
    fn test_builder_methods() {
        let slots = SystemPromptSlots::default()
            .with_style(AgentStyle::Plan)
            .with_role("You are a Python expert")
            .with_guidelines("Use type hints")
            .with_response_style("Be brief")
            .with_extra("Additional instructions");

        assert_eq!(slots.style, Some(AgentStyle::Plan));
        assert_eq!(slots.role, Some("You are a Python expert".to_string()));
        assert_eq!(slots.guidelines, Some("Use type hints".to_string()));
        assert_eq!(slots.response_style, Some("Be brief".to_string()));
        assert_eq!(slots.extra, Some("Additional instructions".to_string()));

        let built = slots.build();
        assert!(built.contains("Python expert"));
        assert!(built.contains("Use type hints"));
        assert!(built.contains("Be brief"));
        assert!(built.contains("Additional instructions"));
    }

    #[test]
    fn test_code_review_guidelines_appended() {
        let slots = SystemPromptSlots {
            style: Some(AgentStyle::CodeReview),
            ..Default::default()
        };
        let built = slots.build();
        assert!(built.contains("code review agent"));
        assert!(built.contains("correctness"));
        assert!(built.contains("regressions"));
        assert!(built.contains("security"));
    }

    #[test]
    fn test_prompts_do_not_reference_removed_surfaces() {
        let prompts = [
            SYSTEM_DEFAULT,
            AGENT_VERIFICATION,
            PRE_ANALYSIS_SYSTEM,
            AGENT_EXPLORE,
            AGENT_PLAN,
            AGENT_CODE_REVIEW,
            SKILLS_CATALOG_HEADER,
            CONTINUATION,
        ]
        .join("\n")
        .to_lowercase();

        for removed in [
            "orchestrator",
            "plugin",
            "agentic_search",
            "agentic_parse",
            "agentic-search",
            "agentic-parse",
            "manage_skill",
            "claude.md",
        ] {
            assert!(
                !prompts.contains(removed),
                "prompt still references removed surface: {removed}"
            );
        }

        assert!(SYSTEM_DEFAULT.contains("program"));
        assert!(SYSTEM_DEFAULT.contains("task"));
        assert!(SYSTEM_DEFAULT.contains("parallel_task"));
        assert!(SYSTEM_DEFAULT.contains("AHP"));
    }
}