1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Regression tests for TOOLS.md template to prevent bloat and duplication.
//!
//! These tests enforce that:
//! 1. TOOLS.md stays concise (under 100 lines)
//! 2. No failure timestamps or log entries
//! 3. No content duplicated from BRAIN_PREAMBLE (system prompt)
//! 4. No raw HTML or stack traces
use std::fs;
const TEMPLATE_PATH: &str = "src/docs/reference/templates/TOOLS.md";
fn load_template() -> String {
fs::read_to_string(TEMPLATE_PATH).unwrap_or_else(|_| panic!("Failed to read {TEMPLATE_PATH}"))
}
/// TOOLS.md must stay under 100 lines.
/// If it grows beyond that, content likely belongs in a skill or on-demand file.
#[test]
fn test_tools_md_line_count() {
let content = load_template();
let lines = content.lines().count();
assert!(
lines <= 100,
"TOOLS.md template has {lines} lines (max 100). \
Move excess content to skills or on-demand .md files."
);
}
/// TOOLS.md must not contain failure timestamps.
/// These are diagnostic logs, not tool definitions.
/// Pattern: "May \d+", "Jun \d+", "\d+ failures", "Recurring \(" etc.
#[test]
fn test_no_failure_timestamps() {
let content = load_template();
let patterns = [
"failures on",
"failure:",
"failure cluster",
"recurring (",
"since 202",
"session id:",
"timestamp:",
];
for pattern in &patterns {
let lower = content.to_lowercase();
assert!(
!lower.contains(pattern),
"TOOLS.md contains failure log pattern '{pattern}'. \
Failure data belongs in feedback_record, not TOOLS.md."
);
}
}
/// TOOLS.md must not contain raw HTML or stack traces.
#[test]
fn test_no_raw_html_or_traces() {
let content = load_template();
let bad_patterns = ["<!doctype", "<html", "Traceback", "at line \\d+", "panic!("];
for pattern in &bad_patterns {
assert!(
!content.to_lowercase().contains(&pattern.to_lowercase()),
"TOOLS.md contains raw HTML or trace pattern '{pattern}'."
);
}
}
/// TOOLS.md must not duplicate BRAIN_PREAMBLE content.
/// The system prompt already has: search routing, GitHub routing, browser routing,
/// tool parameter list, RSI instructions, plan tool usage.
/// Allow mentions in the "What doesn't belong" boundary section.
#[test]
fn test_no_preamble_duplicates() {
let content = load_template();
// Check for actual content sections that duplicate the preamble.
// We check for section HEADERS, not inline mentions in boundary lists.
let bad_sections = [
"## Search Routing",
"## GitHub Routing",
"## Browser Routing",
"## RSI",
"## Tool Parameters",
"## Parameter Reference",
];
for section in &bad_sections {
assert!(
!content.contains(section),
"TOOLS.md has section '{section}' which duplicates BRAIN_PREAMBLE content."
);
}
}
/// TOOLS.md must not contain duplicate sections.
/// Each header should appear exactly once.
#[test]
fn test_no_duplicate_sections() {
let content = load_template();
let mut headers: Vec<&str> = Vec::new();
for line in content.lines() {
if line.starts_with("## ") {
let header = line.trim_start_matches("## ").trim();
assert!(
!headers.contains(&header),
"TOOLS.md has duplicate section: '## {header}'"
);
headers.push(header);
}
}
}
/// TOOLS.md must not contain full CLI references.
/// These belong in skills (loaded on demand).
#[test]
fn test_no_full_cli_references() {
let content = load_template();
let lower = content.to_lowercase();
// Full CLI references belong in skills, not TOOLS.md
let cli_ref_patterns = [
"gh pr list",
"gh issue list",
"gh api repos",
"gog gmail",
"gog calendar",
"socialcrabs post",
"socialcrabs schedule",
];
for pattern in &cli_ref_patterns {
assert!(
!lower.contains(pattern),
"TOOLS.md contains full CLI reference '{pattern}'. \
Move to a skill and load on demand."
);
}
}
/// TOOLS.md must not contain provider configuration guides.
/// These live in config.toml and the onboarding wizard.
/// Allow mentions in the "What doesn't belong" boundary section.
#[test]
fn test_no_provider_config_guides() {
let content = load_template();
let lower = content.to_lowercase();
let config_patterns = ["base_url", "default_model", "[provider]"];
for pattern in &config_patterns {
assert!(
!lower.contains(pattern),
"TOOLS.md contains provider config pattern '{pattern}'. \
Provider config lives in config.toml and onboarding."
);
}
}
/// Cross-reference TOOLS.md against the live BRAIN_PREAMBLE in prompt_builder.rs.
/// Extracts the actual preamble from source code and checks for significant phrase overlap.
/// This catches duplication in BOTH directions: preamble→TOOLS.md and TOOLS.md→preamble.
#[test]
fn test_no_brain_preamble_overlap() {
let tools = load_template();
// Read the actual BRAIN_PREAMBLE from source.
let prompt_builder = fs::read_to_string("src/brain/prompt_builder.rs")
.expect("Failed to read prompt_builder.rs");
// Extract the BRAIN_PREAMBLE constant value from the source.
let preamble = extract_const_string(&prompt_builder, "BRAIN_PREAMBLE")
.unwrap_or_else(|| panic!("Could not find BRAIN_PREAMBLE constant in prompt_builder.rs"));
// Extract 4+ word phrases from the preamble and check if they appear in TOOLS.md.
// We skip short phrases (common words) and focus on meaningful sequences.
let preamble_words: Vec<&str> = preamble.split_whitespace().collect();
let mut overlaps = Vec::new();
for window in preamble_words.windows(5) {
let phrase = window.join(" ").to_lowercase();
// Skip phrases that are just common words / formatting
if phrase.contains('|') || phrase.contains("```") || phrase.contains("///") {
continue;
}
// Only check phrases with at least 3 alphanumeric words
let alpha_count = window
.iter()
.filter(|w| w.chars().any(|c| c.is_alphanumeric()))
.count();
if alpha_count < 3 {
continue;
}
if tools.to_lowercase().contains(&phrase) {
overlaps.push(phrase);
}
}
// Deduplicate and report
overlaps.sort();
overlaps.dedup();
// Allow up to 3 accidental overlaps (common phrases like "use the", "for more details")
// but flag systematic duplication
assert!(
overlaps.len() <= 3,
"TOOLS.md and BRAIN_PREAMBLE have {} overlapping 5-word phrases (max 3 allowed). \
This indicates content duplication between the system prompt and TOOLS.md. \
Overlapping phrases:\n - {}\n\n\
Either remove from TOOLS.md (preamble already covers it) or remove from \
BRAIN_PREAMBLE (TOOLS.md is the authoritative source).",
overlaps.len(),
overlaps.join("\n - ")
);
}
/// Extract a Rust string constant value from source code.
/// Looks for `const NAME: &str = "...";` or `const NAME: &str = r#"..."#;`
fn extract_const_string(source: &str, const_name: &str) -> Option<String> {
let needle = format!("const {const_name}:");
let start = source.find(&needle)?;
let after = &source[start..];
// Find the first `=` after the const declaration
let eq_pos = after.find('=')?;
let after_eq = &after[eq_pos + 1..];
// Check for raw string literal r#"..."#
if let Some(raw_start) = after_eq.find("r#\"") {
let content_start = raw_start + 3;
let rest = &after_eq[content_start..];
let end = rest.find("\"#")?;
return Some(rest[..end].to_string());
}
// Check for regular string literal "..."
if let Some(quote_start) = after_eq.find('"') {
let rest = &after_eq[quote_start + 1..];
// Handle escaped quotes by accumulating
let mut result = String::new();
let mut chars = rest.chars().peekable();
while let Some(c) = chars.next() {
if c == '\\' {
if let Some(&next) = chars.peek() {
match next {
'"' => {
result.push('"');
chars.next();
}
'n' => {
result.push('\n');
chars.next();
}
't' => {
result.push('\t');
chars.next();
}
'\\' => {
result.push('\\');
chars.next();
}
_ => {
result.push(c);
}
}
}
} else if c == '"' {
return Some(result);
} else {
result.push(c);
}
}
}
None
}
/// TOOLS.md must not contain system commands (macOS/Win/Linux).
/// These are basic OS knowledge.
/// Allow mentions in the "What doesn't belong" boundary section.
#[test]
fn test_no_system_commands() {
let content = load_template();
// Only check for actual system command SECTIONS, not boundary mentions
let bad_headers = [
"## System Commands",
"## System",
"## OS Commands",
"## macOS",
"## Windows",
"## Linux",
];
for header in &bad_headers {
assert!(
!content.contains(header),
"TOOLS.md contains system command section '{header}'. \
System commands are basic OS knowledge."
);
}
}