1use std::path::Path;
2
3pub fn shorten_path(path: &str) -> String {
4 let p = Path::new(path);
5 if let Some(name) = p.file_name() {
6 return name.to_string_lossy().to_string();
7 }
8 path.to_string()
9}
10
11#[allow(dead_code)]
12pub fn format_type_short(ty: &str) -> String {
13 match ty {
14 "string" | "String" => ":s".to_string(),
15 "number" | "i32" | "i64" | "u32" | "u64" | "usize" | "f32" | "f64" => ":n".to_string(),
16 "boolean" | "bool" => ":b".to_string(),
17 "void" | "()" => "".to_string(),
18 t if t.starts_with("Promise<") => format!("→{}", &t[8..t.len() - 1]),
19 t if t.starts_with("Option<") => format!(":?{}", &t[7..t.len() - 1]),
20 t if t.starts_with("Vec<") => format!(":[{}]", &t[4..t.len() - 1]),
21 t if t.starts_with("Result<") => format!("→!{}", &t[7..t.len() - 1]),
22 _ => format!(":{ty}"),
23 }
24}
25
26pub fn format_savings(original: usize, compressed: usize) -> String {
27 let saved = original.saturating_sub(compressed);
28 if original == 0 {
29 return "0 tok saved".to_string();
30 }
31 let pct = (saved as f64 / original as f64 * 100.0).round() as usize;
32 format!("[{saved} tok saved ({pct}%)]")
33}
34
35pub struct InstructionTemplate {
36 pub code: &'static str,
37 pub full: &'static str,
38}
39
40const TEMPLATES: &[InstructionTemplate] = &[
41 InstructionTemplate {
42 code: "ACT1",
43 full: "Act immediately, report result in one line",
44 },
45 InstructionTemplate {
46 code: "BRIEF",
47 full: "Summarize approach in 1-2 lines, then act",
48 },
49 InstructionTemplate {
50 code: "FULL",
51 full: "Outline approach, consider edge cases, then act",
52 },
53 InstructionTemplate {
54 code: "DELTA",
55 full: "Only show changed lines, not full files",
56 },
57 InstructionTemplate {
58 code: "NOREPEAT",
59 full: "Never repeat known context. Reference cached files by Fn ID",
60 },
61 InstructionTemplate {
62 code: "STRUCT",
63 full: "Use notation, not sentences. Changes: +line/-line/~line",
64 },
65 InstructionTemplate {
66 code: "1LINE",
67 full: "One line per action. Summarize, don't explain",
68 },
69 InstructionTemplate {
70 code: "NODOC",
71 full: "Don't add comments that narrate what code does",
72 },
73 InstructionTemplate {
74 code: "ACTFIRST",
75 full: "Execute tool calls immediately. Never narrate before acting",
76 },
77 InstructionTemplate {
78 code: "QUALITY",
79 full: "Never skip edge case analysis or error handling to save tokens",
80 },
81 InstructionTemplate {
82 code: "NOMOCK",
83 full: "Never use mock data, fake values, or placeholder code",
84 },
85 InstructionTemplate {
86 code: "FREF",
87 full: "Reference files by Fn refs only, never full paths",
88 },
89 InstructionTemplate {
90 code: "DIFF",
91 full: "For code changes: show only diff lines, not full files",
92 },
93 InstructionTemplate {
94 code: "ABBREV",
95 full: "Use abbreviations: fn, cfg, impl, deps, req, res, ctx, err",
96 },
97 InstructionTemplate {
98 code: "SYMBOLS",
99 full: "Use TDD symbols: ⊕=add ⊖=remove ∆=modify →=returns ✓=ok ✗=fail",
100 },
101];
102
103pub fn instruction_decoder_block() -> String {
105 let mut lines = vec!["INSTRUCTION CODES:".to_string()];
106 for t in TEMPLATES {
107 lines.push(format!(" {} = {}", t.code, t.full));
108 }
109 lines.join("\n")
110}
111
112pub fn encode_instructions(complexity: &str) -> String {
114 match complexity {
115 "mechanical" => "MODE: ACT1 DELTA 1LINE | BUDGET: ~50 out-tokens".to_string(),
116 "standard" => "MODE: BRIEF DELTA NOREPEAT STRUCT | BUDGET: ~150 out-tokens".to_string(),
117 "architectural" => {
118 "MODE: FULL QUALITY NOREPEAT STRUCT FREF | BUDGET: unlimited".to_string()
119 }
120 _ => "MODE: BRIEF | BUDGET: ~150 out-tokens".to_string(),
121 }
122}
123
124pub fn encode_instructions_with_snr(complexity: &str, compression_pct: f64) -> String {
126 let snr = if compression_pct > 0.0 {
127 1.0 - (compression_pct / 100.0)
128 } else {
129 1.0
130 };
131 let base = encode_instructions(complexity);
132 format!("{base} | SNR: {snr:.2}")
133}
134
135#[allow(dead_code)]
137pub fn instruction_encoding_savings() -> (usize, usize) {
138 use super::tokens::count_tokens;
139 let decoder = instruction_decoder_block();
140 let decoder_cost = count_tokens(&decoder);
141
142 let full_mechanical = "TASK COMPLEXITY: mechanical\nMinimal reasoning needed. Act immediately, report result in one line.";
143 let encoded_mechanical = "MODE: ACT1 DELTA 1LINE";
144
145 let saving_per_call = count_tokens(full_mechanical) - count_tokens(encoded_mechanical);
146 (decoder_cost, saving_per_call)
147}
148
149#[cfg(test)]
150mod tests {
151 use super::*;
152
153 #[test]
154 fn decoder_block_contains_all_codes() {
155 let block = instruction_decoder_block();
156 for t in TEMPLATES {
157 assert!(
158 block.contains(t.code),
159 "decoder should contain code {}",
160 t.code
161 );
162 }
163 }
164
165 #[test]
166 fn encoded_instructions_are_shorter() {
167 use super::super::tokens::count_tokens;
168 let full = "TASK COMPLEXITY: mechanical\nMinimal reasoning needed. Act immediately, report result in one line.";
169 let encoded = encode_instructions("mechanical");
170 assert!(
171 count_tokens(&encoded) < count_tokens(full),
172 "encoded ({}) should be shorter than full ({})",
173 count_tokens(&encoded),
174 count_tokens(full)
175 );
176 }
177
178 #[test]
179 fn encoding_has_positive_savings() {
180 let (decoder_cost, saving_per_call) = instruction_encoding_savings();
181 assert!(decoder_cost > 0);
182 assert!(saving_per_call > 0);
183 let break_even = (decoder_cost + saving_per_call - 1) / saving_per_call;
184 assert!(
185 break_even <= 30,
186 "break-even should be within 30 calls, got {break_even}"
187 );
188 }
189
190 #[test]
191 fn all_complexity_levels_encode() {
192 for level in &["mechanical", "standard", "architectural"] {
193 let encoded = encode_instructions(level);
194 assert!(encoded.starts_with("MODE:"), "should start with MODE:");
195 }
196 }
197}