Skip to main content

lean_ctx/core/
attention_layout_driver.rs

1use crate::core::profiles::LayoutConfig;
2
3#[derive(Debug, Clone)]
4pub struct LayoutApplyResultV1 {
5    pub output: String,
6    pub changed: bool,
7    pub skipped: bool,
8    pub reason_code: String,
9}
10
11pub fn maybe_reorder_for_attention(
12    content: &str,
13    task_keywords: &[String],
14    cfg: &LayoutConfig,
15) -> LayoutApplyResultV1 {
16    if !cfg.enabled_effective() {
17        return LayoutApplyResultV1 {
18            output: content.to_string(),
19            changed: false,
20            skipped: true,
21            reason_code: "disabled".to_string(),
22        };
23    }
24
25    let line_count = content.lines().count();
26    if line_count < cfg.min_lines_effective() {
27        return LayoutApplyResultV1 {
28            output: content.to_string(),
29            changed: false,
30            skipped: true,
31            reason_code: "below_min_lines".to_string(),
32        };
33    }
34
35    if task_keywords.is_empty() {
36        return LayoutApplyResultV1 {
37            output: content.to_string(),
38            changed: false,
39            skipped: true,
40            reason_code: "no_keywords".to_string(),
41        };
42    }
43
44    LayoutApplyResultV1 {
45        output: content.to_string(),
46        changed: false,
47        skipped: true,
48        reason_code: "disabled".to_string(),
49    }
50}
51
52#[cfg(test)]
53pub mod tests {
54    use super::*;
55
56    #[test]
57    fn skips_when_disabled() {
58        let cfg = LayoutConfig::default();
59        let content = "use std::io;\nfn main() {}\nlet x = 1;\nlet y = 2;\nlet z = 3;\n";
60        let r = maybe_reorder_for_attention(content, &["main".to_string()], &cfg);
61        assert!(r.skipped);
62        assert!(!r.changed);
63        assert_eq!(r.output, content);
64    }
65
66    #[test]
67    fn skips_when_no_keywords() {
68        let cfg = LayoutConfig {
69            enabled: Some(true),
70            min_lines: Some(1),
71        };
72        let content = "use std::io;\nfn main() {}\n";
73        let r = maybe_reorder_for_attention(content, &[], &cfg);
74        assert!(r.skipped);
75        assert!(!r.changed);
76        assert_eq!(r.output, content);
77    }
78
79    #[test]
80    fn reorders_when_enabled_and_keywords_present() {
81        let cfg = LayoutConfig {
82            enabled: Some(true),
83            min_lines: Some(1),
84        };
85        let content = "let x = 1;\nuse std::io;\n}\nreturn Err(e);\npub struct Foo {\nfn main() {";
86        let r = maybe_reorder_for_attention(content, &["err".to_string()], &cfg);
87        assert!(r.skipped);
88        assert_eq!(r.reason_code, "disabled");
89    }
90}