Skip to main content

lean_ctx/core/
rules_dedup.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Collapse duplicated lean-ctx guidance (#578): the planner and fixer behind
3//! `lean-ctx rules dedup`, `setup`, `doctor --fix` and the hook refresh.
4//!
5//! A client should pay for lean-ctx rules exactly once per session. Older
6//! installs (and parent-directory walks in monorepos) left full rule copies
7//! in several auto-loaded files; `doctor overhead` detects the duplication,
8//! this module repairs it:
9//!
10//!  1. lean-ctx-OWNED dedicated rule files outside the canonical global
11//!     location (project/parent `.cursor/rules/lean-ctx.mdc`,
12//!     `.claude/rules/lean-ctx.md`, …) → deleted.
13//!  2. `.cursorrules` lean-ctx blocks → removed when the canonical global
14//!     Cursor mdc exists (Cursor auto-loads both; pointer lives in AGENTS.md).
15//!  3. Stale compression blocks in `.cursorrules` → removed under the same
16//!     condition (the global mdc carries the block).
17//!  4. Compression block in a shared `AGENTS.md` (#684) → removed (pointer
18//!     kept) once every AGENTS.md reader is covered by its own canonical
19//!     carrier; otherwise reported and left as the carrier.
20//!
21//! Only lean-ctx-owned files and lean-ctx-marked blocks are ever touched.
22//! Unmarked user content is reported, never modified. Default is a dry-run
23//! report; `--apply` executes with `.bak` backups for partial edits.
24
25use std::path::{Path, PathBuf};
26
27use crate::core::rules_canonical::{END_MARK as BLOCK_END, START_MARK as BLOCK_START};
28use crate::core::rules_channel::{COMPRESSION_BLOCK_END, COMPRESSION_BLOCK_START};
29
30/// One planned dedup action.
31#[derive(Debug, PartialEq, Eq)]
32pub enum Action {
33    /// Delete a wholly lean-ctx-owned duplicate rules file.
34    DeleteFile { path: PathBuf, reason: String },
35    /// Strip lean-ctx-marked blocks from a shared file (keeps user content).
36    StripBlocks { path: PathBuf, reason: String },
37    /// Strip only the compression block from a shared file, keeping the
38    /// `<!-- lean-ctx -->` pointer and all user content (#684 — thins a shared
39    /// AGENTS.md whose readers are all covered by their own canonical carrier).
40    StripCompression { path: PathBuf, reason: String },
41    /// Informational only — lean-ctx guidance in user-maintained content.
42    Report { path: PathBuf, note: String },
43}
44
45/// A file is "lean-ctx-owned" when lean-ctx wrote the whole file: dedicated
46/// rule files start with the canonical header and carry a rules-version
47/// marker, project LEAN-CTX.md carries its ownership marker.
48fn is_owned_rules_file(content: &str) -> bool {
49    let starts_with_header = content
50        .trim_start()
51        .starts_with(crate::core::rules_canonical::START_MARK)
52        // CursorMdc has YAML frontmatter before the header.
53        || (content.trim_start().starts_with("---")
54            && content.contains(crate::core::rules_canonical::START_MARK));
55    if starts_with_header && content.contains("<!-- version:") {
56        return true;
57    }
58    is_owned_legacy_rules_file(content)
59}
60
61/// #1289: older installs wrote dedicated rule files with versioned markers
62/// (`<!-- lean-ctx-rules-v9 -->`) and no `<!-- version: N -->` line. Those
63/// files never matched the canonical ownership test above, so stale guidance
64/// (e.g. "Write, Delete, Glob → use normally" contradicting the shadow-mode
65/// Glob deny) survived every update and dedup run. A legacy file counts as
66/// owned when lean-ctx plausibly wrote all of it: a legacy rules marker sits
67/// in the first few lines and nothing but whitespace follows the last closing
68/// lean-ctx marker — user-extended files keep getting the Report instead.
69fn is_owned_legacy_rules_file(content: &str) -> bool {
70    let mut lines = content.trim_start().lines();
71    // Legacy files start with a `# lean-ctx …` heading (marker on the next
72    // line) or with the marker itself — a marker merely mentioned mid-text
73    // does not make a file lean-ctx-owned.
74    let first = lines.next().unwrap_or_default().trim();
75    let marker_line = if first.starts_with("# lean-ctx") {
76        lines.next().unwrap_or_default().trim()
77    } else {
78        first
79    };
80    if !marker_line.starts_with(crate::core::rules_canonical::RULES_MARKER_PREFIX) {
81        return false;
82    }
83    let Some(idx) = content.rfind("<!-- /lean-ctx") else {
84        return false;
85    };
86    let Some(end) = content[idx..].find("-->") else {
87        return false;
88    };
89    content[idx + end + 3..].trim().is_empty()
90}
91
92/// #1289/A3: the lean-ctx-written region of a legacy versioned rules file
93/// (`# lean-ctx …` heading + `<!-- lean-ctx-rules-vN -->` … `<!-- /lean-ctx -->`)
94/// that a user (or agent) extended below the closing marker. Returns the text
95/// after the region — the user's part — or `None` when the file does not start
96/// with such a region. Only the leading lean-ctx region is ever removed.
97fn strip_legacy_owned_prefix(content: &str) -> Option<String> {
98    let trimmed = content.trim_start();
99    let mut lines = trimmed.lines();
100    let first = lines.next()?.trim();
101    let marker_line = if first.starts_with("# lean-ctx") {
102        lines.next().unwrap_or_default().trim()
103    } else {
104        first
105    };
106    if !marker_line.starts_with(crate::core::rules_canonical::RULES_MARKER_PREFIX)
107        || marker_line.starts_with(BLOCK_START)
108    {
109        return None;
110    }
111    let mut offset = 0usize;
112    for line in trimmed.split_inclusive('\n') {
113        offset += line.len();
114        let t = line.trim();
115        if t == crate::core::rules_canonical::AGENTS_BLOCK_END
116            || t.starts_with("<!-- /lean-ctx-rules")
117        {
118            return Some(trimmed[offset..].to_string());
119        }
120    }
121    None
122}
123
124/// Line-based marker pair detection (GL #1158): prose mentions of a marker
125/// must not count as a block.
126fn has_marker_pair(content: &str, start: &str, end: &str) -> bool {
127    crate::marked_block::contains_marker_line(content, start)
128        && crate::marked_block::contains_marker_line(content, end)
129}
130
131fn has_marked_block(content: &str) -> bool {
132    has_marker_pair(content, BLOCK_START, BLOCK_END)
133        || has_marker_pair(content, COMPRESSION_BLOCK_START, COMPRESSION_BLOCK_END)
134}
135
136/// Strips every lean-ctx-marked block (rules + compression) from `content`.
137pub(crate) fn strip_lean_ctx_blocks(content: &str) -> String {
138    let mut out = strip_legacy_owned_prefix(content).unwrap_or_else(|| content.to_string());
139    // Repeat until stable — a file can contain both block kinds (and in
140    // pathological cases several of the same kind).
141    loop {
142        let next = if has_marker_pair(&out, BLOCK_START, BLOCK_END) {
143            crate::marked_block::remove_content(&out, BLOCK_START, BLOCK_END)
144        } else if has_marker_pair(&out, COMPRESSION_BLOCK_START, COMPRESSION_BLOCK_END) {
145            crate::marked_block::remove_content(
146                &out,
147                COMPRESSION_BLOCK_START,
148                COMPRESSION_BLOCK_END,
149            )
150        } else {
151            break;
152        };
153        if next == out {
154            break;
155        }
156        out = next;
157    }
158    let trimmed = out.trim();
159    if trimmed.is_empty() {
160        String::new()
161    } else {
162        format!("{}\n", out.trim_end().trim_start_matches('\n'))
163    }
164}
165
166/// A file that lean-ctx wrote whole: owned by [`is_owned_rules_file`], or one
167/// that opens with a lean-ctx rules marker and never closes it (a whole-file
168/// block, as `rules_overhead::lean_ctx_block_tokens` counts it) — nothing in
169/// it lies outside the lean-ctx region.
170fn is_whole_lean_ctx_file(content: &str) -> bool {
171    if is_owned_rules_file(content) {
172        return true;
173    }
174    content
175        .trim_start()
176        .starts_with(crate::core::rules_canonical::RULES_MARKER_PREFIX)
177        && !content
178            .lines()
179            .any(|l| l.trim_start().starts_with("<!-- /lean-ctx"))
180}
181
182/// True when `content` holds lean-ctx-written text that [`strip_lean_ctx_blocks`]
183/// can remove without touching anything outside the markers.
184fn has_strippable_region(content: &str) -> bool {
185    has_marked_block(content) || strip_legacy_owned_prefix(content).is_some()
186}
187
188/// Retire a lean-ctx rules file that duplicates a host's single rules channel
189/// (A3): a wholly lean-ctx-owned file is deleted; a file a user extended keeps
190/// every line outside the lean-ctx markers (backup at `<file>.bak`). Returns a
191/// result line when something changed. Shared by `setup`/`init` writers and
192/// `rules dedup`, so no path ever deletes user text.
193pub fn retire_rules_file(path: &Path) -> Option<String> {
194    let content = std::fs::read_to_string(path).ok()?;
195    let action = if is_whole_lean_ctx_file(&content) {
196        Action::DeleteFile {
197            path: path.to_path_buf(),
198            reason: String::new(),
199        }
200    } else if has_strippable_region(&content) {
201        Action::StripBlocks {
202            path: path.to_path_buf(),
203            reason: String::new(),
204        }
205    } else {
206        return None;
207    };
208    let line = apply(&action);
209    (!line.starts_with("unchanged")).then_some(line)
210}
211
212/// Strips only the compression block, keeping the `<!-- lean-ctx -->` pointer
213/// and every line of user content. Used to thin a shared AGENTS.md once each
214/// reader is covered by its own canonical carrier (#684).
215pub(crate) fn strip_compression_block(content: &str) -> String {
216    if !(content.contains(COMPRESSION_BLOCK_START) && content.contains(COMPRESSION_BLOCK_END)) {
217        return content.to_string();
218    }
219    let stripped = crate::marked_block::remove_content(
220        content,
221        COMPRESSION_BLOCK_START,
222        COMPRESSION_BLOCK_END,
223    );
224    let trimmed = stripped.trim_end();
225    if trimmed.is_empty() {
226        String::new()
227    } else {
228        format!("{trimmed}\n")
229    }
230}
231
232/// Dedicated lean-ctx rule files that may linger in a project / parent chain
233/// from older versions. Canonical copies live under `home` (global targets).
234fn project_owned_candidates(dir: &Path) -> Vec<PathBuf> {
235    vec![
236        dir.join(".cursor/rules/lean-ctx.mdc"),
237        dir.join(".claude/rules/lean-ctx.md"),
238        dir.join(".codebuddy/rules/lean-ctx.md"),
239        dir.join(".windsurf/rules/lean-ctx.md"),
240        dir.join(".cline/rules/lean-ctx.md"),
241        dir.join(".roo/rules/lean-ctx.md"),
242    ]
243}
244
245/// Plans one duplicate rules file: owned → delete, user-extended with a
246/// lean-ctx region → strip that region only, anything else → report.
247fn plan_duplicate_file(actions: &mut Vec<Action>, candidate: PathBuf, reason: &str) {
248    let Ok(content) = std::fs::read_to_string(&candidate) else {
249        return;
250    };
251    if is_whole_lean_ctx_file(&content) {
252        actions.push(Action::DeleteFile {
253            path: candidate,
254            reason: reason.into(),
255        });
256    } else if has_strippable_region(&content) {
257        actions.push(Action::StripBlocks {
258            path: candidate,
259            reason: format!("{reason}; text outside the lean-ctx markers is kept"),
260        });
261    } else if !content.trim().is_empty() {
262        actions.push(Action::Report {
263            path: candidate,
264            note: "contains custom edits — not lean-ctx-owned, left untouched".into(),
265        });
266    }
267}
268
269/// Global lean-ctx rule files that duplicate a host's single channel: Claude
270/// Code and CodeBuddy carry their rules in the `CLAUDE.md` / `CODEBUDDY.md`
271/// block and auto-load every `rules/*.md` on top (GL #555, A3).
272fn global_duplicate_candidates(home: &Path) -> Vec<PathBuf> {
273    vec![
274        crate::core::editor_registry::claude_rules_dir(home).join("lean-ctx.md"),
275        home.join(".codebuddy/rules/lean-ctx.md"),
276    ]
277}
278
279/// Plans the dedup for `project` (walking parents up to, excluding, `home`).
280pub fn plan(home: &Path, project: &Path) -> Vec<Action> {
281    let mut actions = Vec::new();
282    let canonical_cursor_mdc = home.join(".cursor/rules/lean-ctx.mdc");
283
284    // 0. Global always-loaded duplicates of the CLAUDE.md / CODEBUDDY.md block.
285    for candidate in global_duplicate_candidates(home) {
286        plan_duplicate_file(
287            &mut actions,
288            candidate,
289            "always-loaded duplicate of the host's CLAUDE.md/CODEBUDDY.md rules block",
290        );
291    }
292
293    // 1. Owned dedicated duplicates in the project + parent chain.
294    let mut dir = Some(project.to_path_buf());
295    while let Some(d) = dir {
296        if d == *home {
297            break;
298        }
299        for candidate in project_owned_candidates(&d) {
300            if candidate == canonical_cursor_mdc {
301                continue;
302            }
303            plan_duplicate_file(
304                &mut actions,
305                candidate,
306                "lean-ctx-owned duplicate of the global rules file",
307            );
308        }
309        // The parent walk stays inside `home`: a project outside it (a CI
310        // checkout, a test run with a temp HOME) must not reach directories
311        // that belong to a different home or user — only the project itself
312        // is checked there.
313        if !d.starts_with(home) {
314            break;
315        }
316        dir = d.parent().map(Path::to_path_buf);
317    }
318
319    // 2./3. `.cursorrules`: marked blocks are redundant once the canonical
320    // global mdc exists (Cursor loads both files every session).
321    let cursorrules = project.join(".cursorrules");
322    if let Ok(content) = std::fs::read_to_string(&cursorrules) {
323        if canonical_cursor_mdc.exists() && has_marked_block(&content) {
324            actions.push(Action::StripBlocks {
325                path: cursorrules,
326                reason: "global ~/.cursor/rules/lean-ctx.mdc already carries these blocks".into(),
327            });
328        } else if !canonical_cursor_mdc.exists() && content.contains("lean-ctx") {
329            actions.push(Action::Report {
330                path: cursorrules,
331                note: "no global Cursor mdc found — .cursorrules stays the carrier".into(),
332            });
333        } else if content.contains("lean-ctx") && !has_marked_block(&content) {
334            actions.push(Action::Report {
335                path: cursorrules,
336                note: "mentions lean-ctx without markers (manual rules) — review by hand".into(),
337            });
338        }
339    }
340
341    // 4. Shared project AGENTS.md (#684): Cursor, Codex and other agents all
342    // auto-load it, so a compression block here duplicates each reader's own
343    // canonical carrier. Strip only the compression block (the pointer stays)
344    // once every reader present on this machine is covered elsewhere.
345    let agents_md = project.join("AGENTS.md");
346    if let Ok(content) = std::fs::read_to_string(&agents_md) {
347        let has_compression =
348            content.contains(COMPRESSION_BLOCK_START) && content.contains(COMPRESSION_BLOCK_END);
349        if has_compression {
350            if crate::core::rules_channel::agents_md_can_thin(home) {
351                actions.push(Action::StripCompression {
352                    path: agents_md,
353                    reason: "every AGENTS.md reader already loads the compression block from its own canonical file".into(),
354                });
355            } else {
356                actions.push(Action::Report {
357                    path: agents_md,
358                    note: "an AGENTS.md reader has no other compression source — AGENTS.md stays the carrier".into(),
359                });
360            }
361        }
362    }
363
364    actions
365}
366
367/// Executes one action. Returns a human-readable result line.
368pub fn apply(action: &Action) -> String {
369    match action {
370        Action::DeleteFile { path, .. } => match std::fs::remove_file(path) {
371            Ok(()) => format!("deleted   {}", path.display()),
372            Err(e) => format!("FAILED    {} ({e})", path.display()),
373        },
374        Action::StripBlocks { path, .. } => {
375            let Ok(content) = std::fs::read_to_string(path) else {
376                return format!("FAILED    {} (unreadable)", path.display());
377            };
378            let stripped = strip_lean_ctx_blocks(&content);
379            if stripped == content {
380                return format!("unchanged {}", path.display());
381            }
382            let bak = path.with_extension("bak");
383            if let Err(e) = crate::config_io::write_backup_nofollow(path, &bak, content.as_bytes())
384            {
385                return format!("FAILED    {} (backup: {e})", path.display());
386            }
387            if stripped.is_empty() {
388                match std::fs::remove_file(path) {
389                    Ok(()) => format!(
390                        "deleted   {} (only lean-ctx blocks, backup: {})",
391                        path.display(),
392                        bak.display()
393                    ),
394                    Err(e) => format!("FAILED    {} ({e})", path.display()),
395                }
396            } else {
397                match std::fs::write(path, &stripped) {
398                    Ok(()) => format!("stripped  {} (backup: {})", path.display(), bak.display()),
399                    Err(e) => format!("FAILED    {} ({e})", path.display()),
400                }
401            }
402        }
403        Action::StripCompression { path, .. } => {
404            let Ok(content) = std::fs::read_to_string(path) else {
405                return format!("FAILED    {} (unreadable)", path.display());
406            };
407            let stripped = strip_compression_block(&content);
408            if stripped == content {
409                return format!("unchanged {}", path.display());
410            }
411            let bak = path.with_extension("bak");
412            if let Err(e) = crate::config_io::write_backup_nofollow(path, &bak, content.as_bytes())
413            {
414                return format!("FAILED    {} (backup: {e})", path.display());
415            }
416            match std::fs::write(path, &stripped) {
417                Ok(()) => format!("thinned   {} (backup: {})", path.display(), bak.display()),
418                Err(e) => format!("FAILED    {} ({e})", path.display()),
419            }
420        }
421        Action::Report { path, note } => format!("info      {} — {note}", path.display()),
422    }
423}
424
425/// Non-interactive auto-heal used by `lean-ctx setup` and `doctor --fix`
426/// (#578): applies every *fixable* dedup action (owned duplicates deleted,
427/// marked blocks stripped with `.bak` backups) and returns one result line per
428/// action. `Report`-only findings (user-maintained content) are never touched.
429pub fn auto_apply(home: &Path, project: &Path) -> Vec<String> {
430    plan(home, project)
431        .iter()
432        .filter(|a| !matches!(a, Action::Report { .. }))
433        .map(apply)
434        .collect()
435}
436
437#[cfg(test)]
438mod tests {
439    use super::*;
440
441    fn owned_mdc() -> String {
442        format!(
443            "---\ndescription: lean-ctx\n---\n{}\n<!-- version: 1 -->\nbody\n",
444            crate::core::rules_canonical::START_MARK
445        )
446    }
447
448    #[test]
449    fn detects_owned_dedicated_files() {
450        assert!(is_owned_rules_file(&owned_mdc()));
451        assert!(is_owned_rules_file(&format!(
452            "{}\n<!-- version: 1 -->\nbody\n",
453            crate::core::rules_canonical::START_MARK
454        )));
455        // User file mentioning lean-ctx is NOT owned.
456        assert!(!is_owned_rules_file("# My rules\nuse lean-ctx tools\n"));
457        // Marker buried mid-file (user prepended content) is NOT owned.
458        assert!(!is_owned_rules_file(&format!(
459            "# my header\n{}\n<!-- version: 1 -->\n",
460            crate::core::rules_canonical::START_MARK
461        )));
462    }
463
464    #[cfg(unix)]
465    #[test]
466    fn retire_does_not_follow_a_planted_backup_symlink() {
467        let dir = tempfile::tempdir().unwrap();
468        let rules = dir.path().join("project/.claude/rules");
469        std::fs::create_dir_all(&rules).unwrap();
470        let file = rules.join("lean-ctx.md");
471        std::fs::write(
472            &file,
473            format!("user line\n{BLOCK_START}\nour rules\n{BLOCK_END}\n"),
474        )
475        .unwrap();
476        let outside = dir.path().join("outside.txt");
477        std::os::unix::fs::symlink(&outside, rules.join("lean-ctx.bak")).unwrap();
478
479        let line = retire_rules_file(&file).unwrap();
480
481        assert!(line.starts_with("FAILED"), "{line}");
482        assert!(!outside.exists());
483        assert!(
484            std::fs::read_to_string(&file)
485                .unwrap()
486                .contains("our rules")
487        );
488    }
489
490    #[test]
491    fn strip_removes_rules_and_compression_blocks() {
492        let content = format!(
493            "user line\n{BLOCK_START}\nour rules\n{BLOCK_END}\nmore user\n{COMPRESSION_BLOCK_START}\nstyle\n{COMPRESSION_BLOCK_END}\n",
494        );
495        let out = strip_lean_ctx_blocks(&content);
496        assert!(out.contains("user line"));
497        assert!(out.contains("more user"));
498        assert!(!out.contains("our rules"));
499        assert!(!out.contains("style"));
500        assert!(!out.contains("lean-ctx-compression"));
501    }
502
503    #[test]
504    fn strip_of_pure_block_file_yields_empty() {
505        let content = format!("{BLOCK_START}\nonly ours\n{BLOCK_END}\n");
506        assert_eq!(strip_lean_ctx_blocks(&content), "");
507    }
508
509    /// #1289: legacy versioned-marker files (rules-v9 era, no `<!-- version:`)
510    /// are owned when wholly lean-ctx-written; user-extended copies are not.
511    #[test]
512    fn legacy_marker_files_owned_only_when_wholly_lean_ctx() {
513        let pristine = "# lean-ctx — Context Engineering Layer\n\
514             <!-- lean-ctx-rules-v9 -->\n\n\
515             PREFER lean-ctx MCP tools.\n\
516             Write, Delete, Glob → use normally.\n\
517             <!-- /lean-ctx -->\n";
518        assert!(is_owned_rules_file(pristine));
519
520        let user_extended = format!("{pristine}\n## My own precedence rules\nkeep me\n");
521        assert!(!is_owned_rules_file(&user_extended));
522
523        // A user file that merely mentions the marker mid-text is not owned.
524        assert!(!is_owned_rules_file(
525            "my notes\nabout <!-- lean-ctx-rules-v9 --> markers\n<!-- /lean-ctx -->\n"
526        ));
527    }
528
529    #[test]
530    fn plan_deletes_project_and_parent_owned_files_only() {
531        let tmp = tempfile::tempdir().unwrap();
532        let home = tmp.path();
533        let parent = home.join("projects");
534        let project = parent.join("app");
535
536        // Canonical global mdc (must never be planned for deletion).
537        std::fs::create_dir_all(home.join(".cursor/rules")).unwrap();
538        std::fs::write(home.join(".cursor/rules/lean-ctx.mdc"), owned_mdc()).unwrap();
539        // Stale project + parent copies.
540        std::fs::create_dir_all(project.join(".cursor/rules")).unwrap();
541        std::fs::write(project.join(".cursor/rules/lean-ctx.mdc"), owned_mdc()).unwrap();
542        std::fs::create_dir_all(parent.join(".cursor/rules")).unwrap();
543        std::fs::write(parent.join(".cursor/rules/lean-ctx.mdc"), owned_mdc()).unwrap();
544        // User-customized file — must only be reported.
545        std::fs::create_dir_all(project.join(".claude/rules")).unwrap();
546        std::fs::write(
547            project.join(".claude/rules/lean-ctx.md"),
548            "# customized by user\nkeep me\n",
549        )
550        .unwrap();
551
552        let actions = plan(home, &project);
553        let deletes: Vec<&PathBuf> = actions
554            .iter()
555            .filter_map(|a| match a {
556                Action::DeleteFile { path, .. } => Some(path),
557                _ => None,
558            })
559            .collect();
560        assert_eq!(deletes.len(), 2, "project + parent copies: {actions:?}");
561        assert!(deletes.iter().all(|p| !p.starts_with(home.join(".cursor"))));
562        assert!(actions.iter().any(|a| matches!(
563            a,
564            Action::Report { path, .. } if path.ends_with(".claude/rules/lean-ctx.md")
565        )));
566    }
567
568    /// A project outside `home` (a CI checkout, a test run with a temp HOME)
569    /// must not let the parent walk reach another user's directories: before,
570    /// it ran to the filesystem root and rewrote the real checkout's rules.
571    #[test]
572    fn plan_parent_walk_stays_inside_home() {
573        let tmp = tempfile::tempdir().unwrap();
574        let home = tmp.path().join("home");
575        let outside = tmp.path().join("elsewhere");
576        let project = outside.join("app");
577        std::fs::create_dir_all(&home).unwrap();
578        std::fs::create_dir_all(outside.join(".cursor/rules")).unwrap();
579        std::fs::create_dir_all(project.join(".cursor/rules")).unwrap();
580        std::fs::write(outside.join(".cursor/rules/lean-ctx.mdc"), owned_mdc()).unwrap();
581        std::fs::write(project.join(".cursor/rules/lean-ctx.mdc"), owned_mdc()).unwrap();
582
583        let paths: Vec<PathBuf> = plan(&home, &project)
584            .into_iter()
585            .filter_map(|a| match a {
586                Action::DeleteFile { path, .. } | Action::StripBlocks { path, .. } => Some(path),
587                _ => None,
588            })
589            .collect();
590        assert_eq!(paths, vec![project.join(".cursor/rules/lean-ctx.mdc")]);
591    }
592
593    #[test]
594    fn plan_strips_cursorrules_only_with_canonical_mdc() {
595        let tmp = tempfile::tempdir().unwrap();
596        let home = tmp.path();
597        let project = home.join("app");
598        std::fs::create_dir_all(&project).unwrap();
599        let rules = format!("{BLOCK_START}\npointer\n{BLOCK_END}\n");
600        std::fs::write(project.join(".cursorrules"), rules).unwrap();
601
602        // Without the global mdc, .cursorrules is the carrier — report only.
603        let actions = plan(home, &project);
604        assert!(
605            !actions
606                .iter()
607                .any(|a| matches!(a, Action::StripBlocks { .. })),
608            "{actions:?}"
609        );
610
611        // With the canonical mdc, the block is a duplicate — strip.
612        std::fs::create_dir_all(home.join(".cursor/rules")).unwrap();
613        std::fs::write(home.join(".cursor/rules/lean-ctx.mdc"), owned_mdc()).unwrap();
614        let actions = plan(home, &project);
615        assert!(
616            actions.iter().any(|a| matches!(
617                a,
618                Action::StripBlocks { path, .. } if path.ends_with(".cursorrules")
619            )),
620            "{actions:?}"
621        );
622    }
623
624    #[test]
625    fn apply_strip_writes_backup_and_keeps_user_content() {
626        let tmp = tempfile::tempdir().unwrap();
627        let path = tmp.path().join(".cursorrules");
628        std::fs::write(
629            &path,
630            format!("my custom rule\n{BLOCK_START}\nours\n{BLOCK_END}\n"),
631        )
632        .unwrap();
633
634        let msg = apply(&Action::StripBlocks {
635            path: path.clone(),
636            reason: String::new(),
637        });
638        assert!(msg.starts_with("stripped"), "{msg}");
639        let after = std::fs::read_to_string(&path).unwrap();
640        assert_eq!(after, "my custom rule\n");
641        let bak = std::fs::read_to_string(path.with_extension("bak")).unwrap();
642        assert!(bak.contains("ours"));
643    }
644
645    #[test]
646    fn strip_compression_keeps_pointer_and_user_content() {
647        let content = format!(
648            "# Agent Instructions\n\n{BLOCK_START}\npointer\n{BLOCK_END}\n\n{COMPRESSION_BLOCK_START}\nOUTPUT STYLE\n{COMPRESSION_BLOCK_END}\n",
649        );
650        let out = strip_compression_block(&content);
651        assert!(out.contains("# Agent Instructions"));
652        assert!(out.contains(BLOCK_START));
653        assert!(out.contains("pointer"));
654        assert!(!out.contains("lean-ctx-compression"));
655        assert!(!out.contains("OUTPUT STYLE"));
656    }
657
658    #[test]
659    fn plan_thins_agents_md_when_cursor_covered_and_no_codex() {
660        let _guard = crate::core::data_dir::test_env_lock();
661        let tmp = tempfile::tempdir().unwrap();
662        let home = tmp.path();
663        let project = home.join("app");
664        std::fs::create_dir_all(&project).unwrap();
665        // Isolate codex resolution to this empty home (no codex present).
666        crate::test_env::set_var("CODEX_HOME", home.join(".codex"));
667
668        // Canonical global mdc carries the compression block → cursor covered.
669        std::fs::create_dir_all(home.join(".cursor/rules")).unwrap();
670        std::fs::write(
671            home.join(".cursor/rules/lean-ctx.mdc"),
672            format!(
673                "{}\n<!-- version: 1 -->\n{COMPRESSION_BLOCK_START}\nstyle\n{COMPRESSION_BLOCK_END}\n",
674                crate::core::rules_canonical::START_MARK
675            ),
676        )
677        .unwrap();
678
679        // Project AGENTS.md still carries pointer + compression.
680        std::fs::write(
681            project.join("AGENTS.md"),
682            format!(
683                "# Agent Instructions\n\n{BLOCK_START}\npointer\n{BLOCK_END}\n\n{COMPRESSION_BLOCK_START}\nstyle\n{COMPRESSION_BLOCK_END}\n"
684            ),
685        )
686        .unwrap();
687
688        let actions = plan(home, &project);
689        assert!(
690            actions.iter().any(|a| matches!(
691                a,
692                Action::StripCompression { path, .. } if path.ends_with("AGENTS.md")
693            )),
694            "{actions:?}"
695        );
696        crate::test_env::remove_var("CODEX_HOME");
697    }
698
699    #[test]
700    fn plan_keeps_agents_md_carrier_when_cursor_uncovered() {
701        let _guard = crate::core::data_dir::test_env_lock();
702        let tmp = tempfile::tempdir().unwrap();
703        let home = tmp.path();
704        let project = home.join("app");
705        std::fs::create_dir_all(&project).unwrap();
706        crate::test_env::set_var("CODEX_HOME", home.join(".codex"));
707
708        // No global mdc → cursor not covered → AGENTS.md must stay the carrier.
709        std::fs::write(
710            project.join("AGENTS.md"),
711            format!(
712                "# Agent Instructions\n\n{BLOCK_START}\npointer\n{BLOCK_END}\n\n{COMPRESSION_BLOCK_START}\nstyle\n{COMPRESSION_BLOCK_END}\n"
713            ),
714        )
715        .unwrap();
716
717        let actions = plan(home, &project);
718        assert!(
719            !actions
720                .iter()
721                .any(|a| matches!(a, Action::StripCompression { .. })),
722            "{actions:?}"
723        );
724        // Reported instead, so the user understands why it was left alone.
725        assert!(
726            actions.iter().any(|a| matches!(
727                a,
728                Action::Report { path, .. } if path.ends_with("AGENTS.md")
729            )),
730            "{actions:?}"
731        );
732        crate::test_env::remove_var("CODEX_HOME");
733    }
734
735    #[test]
736    fn apply_strip_compression_thins_and_backs_up() {
737        let tmp = tempfile::tempdir().unwrap();
738        let path = tmp.path().join("AGENTS.md");
739        std::fs::write(
740            &path,
741            format!(
742                "# Agent Instructions\n\n{BLOCK_START}\npointer\n{BLOCK_END}\n\n{COMPRESSION_BLOCK_START}\nstyle\n{COMPRESSION_BLOCK_END}\n"
743            ),
744        )
745        .unwrap();
746
747        let msg = apply(&Action::StripCompression {
748            path: path.clone(),
749            reason: String::new(),
750        });
751        assert!(msg.starts_with("thinned"), "{msg}");
752        let after = std::fs::read_to_string(&path).unwrap();
753        assert!(after.contains(BLOCK_START));
754        assert!(!after.contains("lean-ctx-compression"));
755        let bak = std::fs::read_to_string(path.with_extension("bak")).unwrap();
756        assert!(bak.contains("lean-ctx-compression"));
757    }
758
759    #[test]
760    fn apply_strip_deletes_file_that_was_only_ours() {
761        let tmp = tempfile::tempdir().unwrap();
762        let path = tmp.path().join(".cursorrules");
763        std::fs::write(&path, format!("{BLOCK_START}\nours\n{BLOCK_END}\n")).unwrap();
764
765        let msg = apply(&Action::StripBlocks {
766            path: path.clone(),
767            reason: String::new(),
768        });
769        assert!(msg.starts_with("deleted"), "{msg}");
770        assert!(!path.exists());
771        assert!(path.with_extension("bak").exists());
772    }
773}