lean_ctx/core/
instruction_compiler.rs1use crate::core::client_constraints;
2use crate::core::profiles;
3use crate::core::protocol::CrpMode;
4
5#[derive(Debug, Clone, serde::Serialize)]
6#[serde(rename_all = "camelCase")]
7pub struct CompiledRuleFile {
8 pub path: String,
9 pub content: String,
10}
11
12#[derive(Debug, Clone, serde::Serialize)]
13#[serde(rename_all = "camelCase")]
14pub struct CompiledInstructions {
15 pub schema_version: u32,
16 pub client: String,
17 pub profile: String,
18 pub crp_mode: String,
19 pub unified_tool_mode: bool,
20 pub mcp_instructions: String,
21 pub rules_files: Vec<CompiledRuleFile>,
22}
23
24#[derive(Debug, Clone, Copy, Default)]
25pub struct CompileOptions {
26 pub unified: bool,
27 pub include_rules_files: bool,
28 pub crp_mode_override: Option<CrpMode>,
29}
30
31pub fn compile(
32 client_id: &str,
33 profile_name: &str,
34 opts: CompileOptions,
35) -> Result<CompiledInstructions, String> {
36 let client_id = client_id.trim();
37 let profile_name = profile_name.trim();
38 if client_id.is_empty() {
39 return Err("missing client id".to_string());
40 }
41 if profile_name.is_empty() {
42 return Err("missing profile name".to_string());
43 }
44
45 let constraints = client_constraints::by_client_id(client_id);
46 if constraints.is_none() {
47 return Err(format!(
48 "unknown client '{client_id}' (use 'lean-ctx instructions --list-clients')"
49 ));
50 }
51
52 let profile = profiles::load_profile(profile_name)
53 .ok_or_else(|| format!("unknown profile '{profile_name}'"))?;
54
55 let crp_mode = opts
56 .crp_mode_override
57 .or_else(|| CrpMode::parse(profile.compression.crp_mode_effective()))
58 .unwrap_or(CrpMode::Tdd);
59
60 let mcp_instructions = crate::instructions::build_instructions_with_client_for_compiler(
61 crp_mode,
62 client_id,
63 opts.unified,
64 );
65
66 if let Some(cap) = constraints.and_then(|c| c.mcp_instructions_max_chars) {
67 if mcp_instructions.len() > cap {
68 return Err(format!(
69 "compiled MCP instructions exceed cap for {client_id}: {} > {cap}",
70 mcp_instructions.len()
71 ));
72 }
73 }
74
75 let mut rules_files = Vec::new();
76 if opts.include_rules_files && client_id == "claude-code" {
77 rules_files.push(CompiledRuleFile {
78 path: "~/.claude/rules/lean-ctx.md".to_string(),
79 content: crate::rules_inject::rules_dedicated_markdown().to_string(),
80 });
81 }
82
83 Ok(CompiledInstructions {
84 schema_version: 1,
85 client: client_id.to_string(),
86 profile: profile.profile.name,
87 crp_mode: match crp_mode {
88 CrpMode::Off => "off",
89 CrpMode::Compact => "compact",
90 CrpMode::Tdd => "tdd",
91 }
92 .to_string(),
93 unified_tool_mode: opts.unified,
94 mcp_instructions,
95 rules_files,
96 })
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn compiled_instructions_are_deterministic() {
105 let a = compile(
106 "cursor",
107 "exploration",
108 CompileOptions {
109 unified: false,
110 include_rules_files: false,
111 crp_mode_override: Some(CrpMode::Tdd),
112 },
113 )
114 .unwrap();
115 let b = compile(
116 "cursor",
117 "exploration",
118 CompileOptions {
119 unified: false,
120 include_rules_files: false,
121 crp_mode_override: Some(CrpMode::Tdd),
122 },
123 )
124 .unwrap();
125 assert_eq!(a.mcp_instructions, b.mcp_instructions);
126 }
127
128 #[test]
129 fn compiles_for_all_known_clients() {
130 for c in crate::core::client_constraints::ALL_CLIENTS {
131 let out = compile(
132 c.id,
133 "exploration",
134 CompileOptions {
135 unified: false,
136 include_rules_files: false,
137 crp_mode_override: Some(CrpMode::Tdd),
138 },
139 )
140 .unwrap();
141 assert!(
142 !out.mcp_instructions.trim().is_empty(),
143 "empty instructions for client {}",
144 c.id
145 );
146 }
147 }
148
149 #[test]
150 fn claude_mcp_instructions_respect_cap() {
151 let out = compile(
152 "claude-code",
153 "exploration",
154 CompileOptions {
155 unified: false,
156 include_rules_files: false,
157 crp_mode_override: Some(CrpMode::Tdd),
158 },
159 )
160 .unwrap();
161 assert!(out.mcp_instructions.len() <= 2048);
162 }
163}