Skip to main content

lean_ctx/core/
solution_rules.rs

1//! Solution-selection guidance for injected instructions.
2
3const START_MARKER: &str = "<!-- lean-ctx-solution -->";
4const END_MARKER: &str = "<!-- /lean-ctx-solution -->";
5
6const MINIMAL_RULES: &str =
7    "Prefer the standard library and platform-native capabilities before adding dependencies.";
8const BALANCED_RULES: &str = "Solution ladder: skip → reuse → stdlib → native → dependency → one-line → minimum.\nSafety: choose the smallest safe solution; validate inputs and preserve existing behavior.";
9const AGGRESSIVE_RULES: &str = "Challenge first: can this be deleted or skipped? Prefer deletion.\nSolution ladder: skip → reuse → stdlib → native → dependency → one-line → minimum.\nSafety: choose the smallest safe solution; validate inputs and preserve existing behavior.";
10
11/// Build the solution-selection rules block for an instruction intensity.
12pub fn solution_rules_block(intensity: &str) -> String {
13    let rules = match intensity {
14        "minimal" => MINIMAL_RULES,
15        "aggressive" => AGGRESSIVE_RULES,
16        _ => BALANCED_RULES,
17    };
18
19    format!("{START_MARKER}\n{rules}\n{END_MARKER}")
20}
21
22/// The policy supplied to every solution-focused subagent.
23pub fn solution_subagent_instructions(_intensity: &str) -> &'static str {
24    "Challenge the need, reuse existing code, prefer stdlib or native capabilities, and add dependencies only when justified."
25}
26
27/// Build optional compose hints from project configuration and dependencies.
28pub fn solution_compose_hints(config_enabled: bool, project_deps: &[String]) -> String {
29    if !config_enabled {
30        return String::new();
31    }
32
33    let mut lines = vec![START_MARKER.to_string()];
34    if !project_deps.is_empty() {
35        lines.push(format!("Installed deps: {}", project_deps.join(", ")));
36    }
37    lines.push(
38        "Prefer the standard library and platform-native capabilities before adding dependencies."
39            .to_string(),
40    );
41    lines.push(END_MARKER.to_string());
42    lines.join("\n")
43}
44
45/// Solution rules are injected only when configuration and request both allow it.
46pub fn should_inject(config_enabled: bool, inject_flag: bool) -> bool {
47    config_enabled && inject_flag
48}
49
50#[cfg(test)]
51mod tests {
52    use super::solution_compose_hints;
53
54    #[test]
55    fn compose_hints_include_detected_dependencies() {
56        let dependencies = ["tokio".to_string(), "serde".to_string()];
57
58        let hints = solution_compose_hints(true, &dependencies);
59
60        assert!(hints.contains("Installed deps: tokio, serde"));
61    }
62}