tokf 0.1.4

Config-driven CLI tool that compresses command output before it reaches an LLM context
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
pub mod types;

pub(crate) mod compound;
pub(crate) mod rules;
pub(crate) mod user_config;

use std::path::PathBuf;

use crate::config;
use compound::split_compound;
use rules::{apply_rules, should_skip};
use types::{RewriteConfig, RewriteRule};

pub use user_config::load_user_config;

/// Build rewrite rules by discovering installed filters (recursive walk).
///
/// For each filter pattern, generates a rule:
/// `^{command_pattern}(\s.*)?$` → `tokf run {0}`
///
/// Handles `CommandPattern::Multiple` (one rule per pattern string) and
/// wildcards (`*` → `\S+` in the regex).
pub(crate) fn build_rules_from_filters(search_dirs: &[PathBuf]) -> Vec<RewriteRule> {
    let mut rules = Vec::new();
    let mut seen_patterns: std::collections::HashSet<String> = std::collections::HashSet::new();

    let Ok(filters) = config::cache::discover_with_cache(search_dirs) else {
        return rules;
    };

    for filter in filters {
        for pattern in filter.config.command.patterns() {
            if !seen_patterns.insert(pattern.clone()) {
                continue;
            }

            let regex_str = config::command_pattern_to_regex(pattern);
            rules.push(RewriteRule {
                match_pattern: regex_str,
                replace: "tokf run {0}".to_string(),
            });
        }
    }

    rules
}

/// Top-level rewrite function. Orchestrates skip check, user rules, and filter rules.
pub fn rewrite(command: &str) -> String {
    let user_config = load_user_config().unwrap_or_default();
    rewrite_with_config(command, &user_config, &config::default_search_dirs())
}

/// Testable version with explicit config and search dirs.
pub(crate) fn rewrite_with_config(
    command: &str,
    user_config: &RewriteConfig,
    search_dirs: &[PathBuf],
) -> String {
    let user_skip_patterns = user_config
        .skip
        .as_ref()
        .map_or(&[] as &[String], |s| &s.patterns);

    if should_skip(command, user_skip_patterns) {
        return command.to_string();
    }

    let user_result = apply_rules(&user_config.rewrite, command);
    if user_result != command {
        return user_result;
    }

    let filter_rules = build_rules_from_filters(search_dirs);
    let segments = split_compound(command);
    if segments.len() == 1 {
        return apply_rules(&filter_rules, command);
    }

    // Compound command: rewrite each segment independently so every sub-command
    // that has a matching filter is wrapped, not just the first one.
    let mut changed = false;
    let mut out = String::with_capacity(command.len() + segments.len() * 9);
    for (seg, sep) in &segments {
        let trimmed = seg.trim();
        let rewritten = if trimmed.is_empty() || should_skip(trimmed, user_skip_patterns) {
            trimmed.to_string()
        } else {
            let r = apply_rules(&filter_rules, trimmed);
            if r != trimmed {
                changed = true;
            }
            r
        };
        out.push_str(&rewritten);
        out.push_str(sep);
    }
    if changed { out } else { command.to_string() }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use std::fs;

    use tempfile::TempDir;

    use super::*;

    // --- build_rules_from_filters ---

    #[test]
    fn build_rules_from_empty_dir() {
        let dir = TempDir::new().unwrap();
        let rules = build_rules_from_filters(&[dir.path().to_path_buf()]);
        // Empty disk dir — embedded stdlib is always present
        assert!(
            !rules.is_empty(),
            "embedded stdlib should provide built-in rules"
        );
    }

    #[test]
    fn build_rules_from_filter_files() {
        let dir = TempDir::new().unwrap();
        fs::write(
            dir.path().join("git-status.toml"),
            "command = \"git status\"",
        )
        .unwrap();
        fs::write(
            dir.path().join("cargo-test.toml"),
            "command = \"cargo test\"",
        )
        .unwrap();

        let rules = build_rules_from_filters(&[dir.path().to_path_buf()]);
        let patterns: Vec<&str> = rules.iter().map(|r| r.match_pattern.as_str()).collect();

        let has_cargo = patterns
            .iter()
            .any(|p| p.contains("cargo") && p.contains("test"));
        let has_git = patterns
            .iter()
            .any(|p| p.contains("git") && p.contains("status"));
        assert!(has_cargo, "expected cargo test pattern in {:?}", patterns);
        assert!(has_git, "expected git status pattern in {:?}", patterns);

        let cargo_rule = rules
            .iter()
            .find(|r| r.match_pattern.contains("cargo"))
            .unwrap();
        let git_rule = rules
            .iter()
            .find(|r| r.match_pattern.contains("status"))
            .unwrap();
        let re_cargo = regex::Regex::new(&cargo_rule.match_pattern).unwrap();
        let re_git = regex::Regex::new(&git_rule.match_pattern).unwrap();
        assert!(re_cargo.is_match("cargo test"));
        assert!(re_cargo.is_match("cargo test --lib"));
        assert!(re_git.is_match("git status"));
        assert!(re_git.is_match("git status --short"));
    }

    #[test]
    fn build_rules_dedup_across_dirs() {
        let dir1 = TempDir::new().unwrap();
        let dir2 = TempDir::new().unwrap();

        fs::write(
            dir1.path().join("git-status.toml"),
            "command = \"git status\"",
        )
        .unwrap();
        fs::write(
            dir2.path().join("git-status.toml"),
            "command = \"git status\"",
        )
        .unwrap();

        let rules =
            build_rules_from_filters(&[dir1.path().to_path_buf(), dir2.path().to_path_buf()]);
        let git_status_count = rules
            .iter()
            .filter(|r| r.match_pattern.contains("git") && r.match_pattern.contains("status"))
            .count();
        assert_eq!(
            git_status_count, 1,
            "git status should be deduped to one rule"
        );
    }

    #[test]
    fn build_rules_skips_invalid_filters() {
        let dir = TempDir::new().unwrap();
        fs::write(dir.path().join("bad.toml"), "not valid [[[").unwrap();
        fs::write(dir.path().join("good.toml"), "command = \"my-tool\"").unwrap();

        let rules = build_rules_from_filters(&[dir.path().to_path_buf()]);
        assert!(
            rules.iter().any(|r| r.match_pattern.contains("my\\-tool")),
            "expected my-tool rule in {:?}",
            rules.iter().map(|r| &r.match_pattern).collect::<Vec<_>>()
        );
    }

    #[test]
    fn build_rules_from_nested_dirs() {
        let dir = TempDir::new().unwrap();
        let git_dir = dir.path().join("git");
        fs::create_dir_all(&git_dir).unwrap();
        fs::write(git_dir.join("push.toml"), "command = \"git push\"").unwrap();
        fs::write(git_dir.join("status.toml"), "command = \"git status\"").unwrap();

        let rules = build_rules_from_filters(&[dir.path().to_path_buf()]);
        let patterns: Vec<&str> = rules.iter().map(|r| r.match_pattern.as_str()).collect();
        assert!(patterns.iter().any(|p| p.contains("push")));
        assert!(patterns.iter().any(|p| p.contains("status")));
    }

    #[test]
    fn build_rules_multiple_command_patterns() {
        let dir = TempDir::new().unwrap();
        fs::write(
            dir.path().join("test-runner.toml"),
            r#"command = ["pnpm test", "npm test"]"#,
        )
        .unwrap();

        let rules = build_rules_from_filters(&[dir.path().to_path_buf()]);
        let patterns: Vec<&str> = rules.iter().map(|r| r.match_pattern.as_str()).collect();
        assert!(patterns.iter().any(|p| p.contains("pnpm")));
        assert!(
            patterns
                .iter()
                .any(|p| p.contains("npm") && !p.contains("pnpm"))
        );
    }

    #[test]
    fn build_rules_wildcard_pattern() {
        let dir = TempDir::new().unwrap();
        fs::write(dir.path().join("npm-run.toml"), r#"command = "npm run *""#).unwrap();

        let rules = build_rules_from_filters(&[dir.path().to_path_buf()]);
        let npm_run_rule = rules
            .iter()
            .find(|r| r.match_pattern.contains("npm") && r.match_pattern.contains("run"))
            .expect("expected npm run rule");
        let re = regex::Regex::new(&npm_run_rule.match_pattern).unwrap();
        assert!(re.is_match("npm run build"));
        assert!(re.is_match("npm run test"));
        assert!(!re.is_match("npm install"));
    }

    // --- rewrite_with_config (single command) ---

    #[test]
    fn rewrite_with_filter_match() {
        let dir = TempDir::new().unwrap();
        fs::write(
            dir.path().join("git-status.toml"),
            "command = \"git status\"",
        )
        .unwrap();

        let config = RewriteConfig::default();
        let result = rewrite_with_config("git status", &config, &[dir.path().to_path_buf()]);
        assert_eq!(result, "tokf run git status");
    }

    #[test]
    fn rewrite_with_filter_match_with_args() {
        let dir = TempDir::new().unwrap();
        fs::write(
            dir.path().join("git-status.toml"),
            "command = \"git status\"",
        )
        .unwrap();

        let config = RewriteConfig::default();
        let result =
            rewrite_with_config("git status --short", &config, &[dir.path().to_path_buf()]);
        assert_eq!(result, "tokf run git status --short");
    }

    #[test]
    fn rewrite_builtin_skip_tokf() {
        let dir = TempDir::new().unwrap();
        let config = RewriteConfig::default();
        let result =
            rewrite_with_config("tokf run git status", &config, &[dir.path().to_path_buf()]);
        assert_eq!(result, "tokf run git status");
    }

    #[test]
    fn rewrite_no_match_passthrough() {
        let dir = TempDir::new().unwrap();
        let config = RewriteConfig::default();
        let result = rewrite_with_config("unknown-cmd foo", &config, &[dir.path().to_path_buf()]);
        assert_eq!(result, "unknown-cmd foo");
    }

    #[test]
    fn rewrite_user_rule_takes_priority() {
        let dir = TempDir::new().unwrap();
        fs::write(
            dir.path().join("git-status.toml"),
            "command = \"git status\"",
        )
        .unwrap();

        let config = RewriteConfig {
            skip: None,
            rewrite: vec![RewriteRule {
                match_pattern: "^git status".to_string(),
                replace: "custom-wrapper {0}".to_string(),
            }],
        };
        let result = rewrite_with_config("git status", &config, &[dir.path().to_path_buf()]);
        assert_eq!(result, "custom-wrapper git status");
    }

    #[test]
    fn rewrite_user_skip_prevents_rewrite() {
        let dir = TempDir::new().unwrap();
        fs::write(
            dir.path().join("git-status.toml"),
            "command = \"git status\"",
        )
        .unwrap();

        let config = RewriteConfig {
            skip: Some(types::SkipConfig {
                patterns: vec!["^git status".to_string()],
            }),
            rewrite: vec![],
        };
        let result = rewrite_with_config("git status", &config, &[dir.path().to_path_buf()]);
        assert_eq!(result, "git status");
    }

    // --- rewrite_with_config (compound commands) ---

    #[test]
    fn rewrite_compound_both_segments_match() {
        let dir = TempDir::new().unwrap();
        fs::write(dir.path().join("git-add.toml"), "command = \"git add\"").unwrap();
        fs::write(
            dir.path().join("git-status.toml"),
            "command = \"git status\"",
        )
        .unwrap();

        let config = RewriteConfig::default();
        let r = rewrite_with_config(
            "git add foo && git status",
            &config,
            &[dir.path().to_path_buf()],
        );
        assert_eq!(r, "tokf run git add foo && tokf run git status");
    }

    #[test]
    fn rewrite_compound_partial_match() {
        let dir = TempDir::new().unwrap();
        fs::write(
            dir.path().join("git-status.toml"),
            "command = \"git status\"",
        )
        .unwrap();

        let config = RewriteConfig::default();
        let r = rewrite_with_config(
            "unknown-cmd && git status",
            &config,
            &[dir.path().to_path_buf()],
        );
        assert_eq!(r, "unknown-cmd && tokf run git status");
    }

    #[test]
    fn rewrite_compound_pipe_not_split() {
        let dir = TempDir::new().unwrap();
        fs::write(dir.path().join("git-diff.toml"), "command = \"git diff\"").unwrap();

        let config = RewriteConfig::default();
        let r = rewrite_with_config(
            "git diff HEAD | head -5",
            &config,
            &[dir.path().to_path_buf()],
        );
        // Pipe is NOT a chain separator — the whole string is one segment.
        assert_eq!(r, "tokf run git diff HEAD | head -5");
    }

    #[test]
    fn rewrite_compound_no_match_passthrough() {
        let dir = TempDir::new().unwrap();
        let config = RewriteConfig::default();
        let r = rewrite_with_config(
            "unknown-a && unknown-b",
            &config,
            &[dir.path().to_path_buf()],
        );
        assert_eq!(r, "unknown-a && unknown-b");
    }
}