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    let output = crate::core::neural::context_reorder::reorder_for_lcurve(content, task_keywords);
45    LayoutApplyResultV1 {
46        changed: output != content,
47        output,
48        skipped: false,
49        reason_code: "reordered".to_string(),
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn skips_when_disabled() {
59        let cfg = LayoutConfig::default();
60        let content = "use std::io;\nfn main() {}\nlet x = 1;\nlet y = 2;\nlet z = 3;\n";
61        let r = maybe_reorder_for_attention(content, &["main".to_string()], &cfg);
62        assert!(r.skipped);
63        assert!(!r.changed);
64        assert_eq!(r.output, content);
65    }
66
67    #[test]
68    fn skips_when_no_keywords() {
69        let cfg = LayoutConfig {
70            enabled: Some(true),
71            min_lines: Some(1),
72        };
73        let content = "use std::io;\nfn main() {}\n";
74        let r = maybe_reorder_for_attention(content, &[], &cfg);
75        assert!(r.skipped);
76        assert!(!r.changed);
77        assert_eq!(r.output, content);
78    }
79
80    #[test]
81    fn reorders_when_enabled_and_keywords_present() {
82        let cfg = LayoutConfig {
83            enabled: Some(true),
84            min_lines: Some(1),
85        };
86        let content = "let x = 1;\nuse std::io;\n}\nreturn Err(e);\npub struct Foo {\nfn main() {";
87        let r = maybe_reorder_for_attention(content, &["err".to_string()], &cfg);
88        assert!(!r.skipped);
89        assert!(r.output.starts_with("return Err(") || r.output.starts_with("use "));
90    }
91}