Skip to main content

lean_ctx/core/contextops/
drift.rs

1use std::path::Path;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub enum DriftStatus {
7    InSync,
8    Drifted,
9    Missing,
10    NoMarkers,
11    ReadError,
12    NotDetected,
13}
14
15impl std::fmt::Display for DriftStatus {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        match self {
18            Self::InSync => write!(f, "IN_SYNC"),
19            Self::Drifted => write!(f, "DRIFTED"),
20            Self::Missing => write!(f, "MISSING"),
21            Self::NoMarkers => write!(f, "NO_MARKERS"),
22            Self::ReadError => write!(f, "READ_ERROR"),
23            Self::NotDetected => write!(f, "NOT_DETECTED"),
24        }
25    }
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct DriftReport {
30    pub target: String,
31    pub path: String,
32    pub status: DriftStatus,
33    pub diff: Option<String>,
34}
35
36/// Compare each detected agent's on-disk `<!-- lean-ctx-rules -->` block against
37/// the **canonical** rule source (`rules_canonical` via
38/// `rules_inject::rules_shared_content` / `rules_dedicated_markdown`).
39///
40/// Drift is measured purely against the canonical single-source-of-truth — it
41/// deliberately does **not** read `.lean-ctx/rules.toml`. `rules.toml` is a
42/// `rules lint` input and a user-facing export from `rules init`; it never
43/// overrides the canonical rule body (see [`super::config::RulesConfig`]). This
44/// is also why `rules diff` works without running `rules init` first (#548).
45pub(crate) fn detect_drift(home: &Path) -> Vec<DriftReport> {
46    let statuses = crate::rules_inject::collect_rules_status(home);
47    // The canonical block lean-ctx would write for each target, keyed by name and
48    // chosen by the target's real `RulesFormat` — see the heuristic note below.
49    let expected_by_target = crate::rules_inject::expected_blocks_by_target(home);
50
51    let marker = crate::core::rules_canonical::START_MARK;
52    let end_marker = crate::core::rules_canonical::END_MARK;
53
54    statuses
55        .into_iter()
56        .map(|status| {
57            if !status.detected {
58                return DriftReport {
59                    target: status.name,
60                    path: status.path,
61                    status: DriftStatus::NotDetected,
62                    diff: None,
63                };
64            }
65
66            let path = Path::new(&status.path);
67            if !path.exists() {
68                return DriftReport {
69                    target: status.name,
70                    path: status.path,
71                    status: DriftStatus::Missing,
72                    diff: None,
73                };
74            }
75
76            let Ok(content) = std::fs::read_to_string(path) else {
77                return DriftReport {
78                    target: status.name,
79                    path: status.path,
80                    status: DriftStatus::ReadError,
81                    diff: None,
82                };
83            };
84
85            if !crate::marked_block::contains_marker_line(&content, marker) {
86                return DriftReport {
87                    target: status.name,
88                    path: status.path,
89                    status: DriftStatus::NoMarkers,
90                    diff: None,
91                };
92            }
93
94            let section = extract_section(&content, marker, end_marker);
95
96            // Compare against the canonical block for THIS target's format. The
97            // previous content heuristic ("up_to_date and no 'existing user
98            // rules'") misclassified freshly synced shared files (Copilot/Codex
99            // CLI) as dedicated and reported phantom drift after every sync (#548).
100            let expected_section = expected_by_target
101                .get(&status.name)
102                .map(|expected| extract_section(expected, marker, end_marker))
103                .unwrap_or_default();
104
105            let section_trimmed = section.trim();
106            let expected_trimmed = expected_section.trim();
107
108            if section_trimmed == expected_trimmed {
109                DriftReport {
110                    target: status.name,
111                    path: status.path,
112                    status: DriftStatus::InSync,
113                    diff: None,
114                }
115            } else {
116                // The comparison needs the file as it is; what is REPORTED is
117                // a rendering of its content, so the diff passes admission as
118                // carrier text before it leaves this function (an agent rule
119                // file lives outside any project and has no rooted reader).
120                // Nothing read here is written anywhere.
121                let diff = compute_diff(expected_trimmed, section_trimmed);
122                let diff = match crate::core::context_admission::carrier::admit_carrier_text(
123                    &diff,
124                    // The user's own agent rule file and the rules lean-ctx
125                    // installs: no outside origin.
126                    &crate::core::context_admission::carrier::INSTANCE,
127                ) {
128                    Ok(admitted) => admitted.text,
129                    Err(withheld) => withheld.notice(),
130                };
131                DriftReport {
132                    target: status.name,
133                    path: status.path,
134                    status: DriftStatus::Drifted,
135                    diff: Some(diff),
136                }
137            }
138        })
139        .collect()
140}
141
142/// The block from the `marker` line to the first `end_marker` line after it
143/// (to the end of the file when there is none), with LF line endings. Only
144/// whole marker lines count, as for the writers: a marker quoted in prose is
145/// user text (GL #1158).
146fn extract_section(content: &str, marker: &str, end_marker: &str) -> String {
147    use crate::marked_block::marker_line_span;
148    let Some((start, after)) = marker_line_span(content, marker) else {
149        return String::new();
150    };
151    let end = marker_line_span(&content[after..], end_marker)
152        .map_or(content.len(), |(_, end)| after + end);
153
154    content[start..end].replace("\r\n", "\n")
155}
156
157fn compute_diff(expected: &str, actual: &str) -> String {
158    let expected_lines: Vec<&str> = expected.lines().collect();
159    let actual_lines: Vec<&str> = actual.lines().collect();
160
161    let mut diff_lines = Vec::new();
162    let max_len = expected_lines.len().max(actual_lines.len());
163
164    for i in 0..max_len {
165        match (expected_lines.get(i), actual_lines.get(i)) {
166            (Some(exp), Some(act)) if exp != act => {
167                diff_lines.push(format!("- {exp}"));
168                diff_lines.push(format!("+ {act}"));
169            }
170            (Some(exp), None) => {
171                diff_lines.push(format!("- {exp}"));
172            }
173            (None, Some(act)) => {
174                diff_lines.push(format!("+ {act}"));
175            }
176            _ => {}
177        }
178    }
179
180    diff_lines.join("\n")
181}
182
183#[cfg(test)]
184mod tests {
185    use super::*;
186    use crate::core::rules_canonical::{END_MARK, START_MARK};
187
188    #[test]
189    fn drift_status_display() {
190        assert_eq!(DriftStatus::InSync.to_string(), "IN_SYNC");
191        assert_eq!(DriftStatus::Drifted.to_string(), "DRIFTED");
192        assert_eq!(DriftStatus::Missing.to_string(), "MISSING");
193        assert_eq!(DriftStatus::NoMarkers.to_string(), "NO_MARKERS");
194        assert_eq!(DriftStatus::ReadError.to_string(), "READ_ERROR");
195        assert_eq!(DriftStatus::NotDetected.to_string(), "NOT_DETECTED");
196    }
197
198    #[test]
199    fn extract_section_with_markers() {
200        // A marker quoted in prose is not the block.
201        let content = format!(
202            "before `{START_MARK}` and `{END_MARK}`\n{START_MARK}\n<!-- version: 1 -->\n\nrules\n{END_MARK}\nafter"
203        );
204        let section = extract_section(&content, START_MARK, END_MARK);
205        assert!(section.contains("rules"));
206        assert!(section.contains(START_MARK));
207        assert!(section.contains(END_MARK));
208        assert!(section.starts_with(START_MARK), "{section:?}");
209        assert!(!section.contains("before"));
210        assert!(!section.contains("after"));
211    }
212
213    #[test]
214    fn extract_section_no_marker() {
215        let section = extract_section("no markers here", "MARKER", "END");
216        assert!(section.is_empty(), "{section:?}");
217    }
218
219    #[test]
220    fn compute_diff_identical() {
221        let diff = compute_diff("line1\nline2", "line1\nline2");
222        assert!(diff.is_empty(), "{diff:?}");
223    }
224
225    #[test]
226    fn compute_diff_changed() {
227        let diff = compute_diff("line1\nline2", "line1\nline3");
228        assert!(diff.contains("- line2"));
229        assert!(diff.contains("+ line3"));
230    }
231
232    #[test]
233    fn compute_diff_added_line() {
234        let diff = compute_diff("line1", "line1\nline2");
235        assert!(diff.contains("+ line2"));
236    }
237
238    #[test]
239    fn compute_diff_removed_line() {
240        let diff = compute_diff("line1\nline2", "line1");
241        assert!(diff.contains("- line2"));
242    }
243}