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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#![allow(dead_code)]

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// A command pattern — either a single string or a list of alternatives.
///
/// ```toml
/// command = "git push"                    # Single
/// command = ["pnpm test", "npm test"]     # Multiple: any variant
/// command = "npm run *"                   # Wildcard: * matches one word
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CommandPattern {
    Single(String),
    Multiple(Vec<String>),
}

impl CommandPattern {
    /// All pattern strings for this command.
    pub fn patterns(&self) -> &[String] {
        match self {
            Self::Single(s) => std::slice::from_ref(s),
            Self::Multiple(v) => v,
        }
    }

    /// Canonical (first) pattern string, used for display and dedup.
    pub fn first(&self) -> &str {
        match self {
            Self::Single(s) => s.as_str(),
            Self::Multiple(v) => v.first().map_or("", String::as_str),
        }
    }
}

impl Default for CommandPattern {
    fn default() -> Self {
        Self::Single(String::new())
    }
}

/// Top-level filter configuration, deserialized from a `.toml` file.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FilterConfig {
    /// The command this filter applies to (e.g. "git push").
    pub command: CommandPattern,

    /// Optional override command to actually run instead.
    pub run: Option<String>,

    /// Patterns for lines to skip (applied before section parsing).
    #[serde(default)]
    pub skip: Vec<String>,

    /// Patterns for lines to keep (inverse of skip).
    #[serde(default)]
    pub keep: Vec<String>,

    /// Pipeline steps to run before filtering.
    #[serde(default)]
    pub step: Vec<Step>,

    /// Extract a single value from the output.
    pub extract: Option<ExtractRule>,

    /// Whole-output matchers checked before any line processing.
    #[serde(default)]
    pub match_output: Vec<MatchOutputRule>,

    /// State-machine sections for collecting lines into named groups.
    #[serde(default)]
    pub section: Vec<Section>,

    /// Branch taken when the command exits 0.
    pub on_success: Option<OutputBranch>,

    /// Branch taken when the command exits non-zero.
    pub on_failure: Option<OutputBranch>,

    /// Structured parsing rules (branch line, file grouping).
    pub parse: Option<ParseConfig>,

    /// Output formatting configuration.
    pub output: Option<OutputConfig>,

    /// Fallback behavior when no other rule matches.
    pub fallback: Option<FallbackConfig>,

    /// Per-line regex replacement steps, applied before skip/keep.
    #[serde(default)]
    pub replace: Vec<ReplaceRule>,

    /// Collapse consecutive identical lines (or within a sliding window).
    #[serde(default)]
    pub dedup: bool,

    /// Window size for dedup (default: consecutive only).
    pub dedup_window: Option<usize>,

    /// Optional Lua/Luau script escape hatch.
    #[serde(default)]
    pub lua_script: Option<ScriptConfig>,
}

/// A pipeline step that runs a sub-command and captures its output.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Step {
    /// Command to run.
    pub run: String,

    /// Name to bind the output to in the template context.
    #[serde(rename = "as")]
    pub as_name: Option<String>,

    /// Whether this step is part of a pipeline. Reserved for Phase 2+; unused by
    /// current filter configs.
    pub pipeline: Option<bool>,
}

/// Extracts a value from text using a regex pattern and formats it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExtractRule {
    /// Regex pattern with capture groups.
    pub pattern: String,

    /// Output template using `{1}`, `{2}`, etc. for captures.
    pub output: String,
}

/// Matches against the full output and short-circuits with a fixed message.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MatchOutputRule {
    /// Substring to search for in the combined output.
    pub contains: String,

    /// Output to emit if the substring is found.
    pub output: String,
}

/// A state-machine section that collects lines between enter/exit markers.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Section {
    /// Name of this section (for diagnostics/debugging).
    pub name: Option<String>,

    /// Regex that activates this section.
    pub enter: Option<String>,

    /// Regex that deactivates this section.
    pub exit: Option<String>,

    /// Regex that individual lines must match to be collected.
    #[serde(rename = "match")]
    pub match_pattern: Option<String>,

    /// Regex to split collected content into blocks.
    pub split_on: Option<String>,

    /// Variable name for the collected lines/blocks.
    pub collect_as: Option<String>,
}

/// Output branch for success/failure exit codes.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OutputBranch {
    /// Template string for the output.
    pub output: Option<String>,

    /// Aggregation rule for collected sections.
    pub aggregate: Option<AggregateRule>,

    /// Number of lines to keep from the tail.
    pub tail: Option<usize>,

    /// Number of lines to keep from the head.
    pub head: Option<usize>,

    /// Patterns for lines to skip within this branch.
    #[serde(default)]
    pub skip: Vec<String>,

    /// Extract rule applied within this branch.
    pub extract: Option<ExtractRule>,
}

/// Aggregates values from a collected section using regex extraction.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AggregateRule {
    /// Name of the collected section to aggregate from.
    pub from: String,

    /// Regex pattern to extract numeric values.
    pub pattern: String,

    /// Name for the summed value.
    pub sum: Option<String>,

    /// Name for the count of matching entries.
    pub count_as: Option<String>,
}

/// Structured parsing configuration for status-like outputs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ParseConfig {
    /// Rule for extracting the branch name from the first line.
    pub branch: Option<LineExtract>,

    /// Rule for grouping file entries by status code.
    pub group: Option<GroupConfig>,
}

/// Extracts a value from a specific line number.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LineExtract {
    /// 1-based line number to extract from.
    pub line: usize,

    /// Regex pattern with capture groups.
    pub pattern: String,

    /// Output template using `{1}`, `{2}`, etc. for captures.
    pub output: String,
}

/// Groups lines by a key pattern and maps keys to human labels.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GroupConfig {
    /// Rule for extracting the group key from each line.
    pub key: ExtractRule,

    /// Map from raw key to human-readable label.
    #[serde(default)]
    pub labels: HashMap<String, String>,
}

/// Output formatting configuration for the final rendered result.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct OutputConfig {
    /// Top-level output format template.
    pub format: Option<String>,

    /// Format template for each group count line.
    pub group_counts_format: Option<String>,

    /// Message to emit when there are no items to report.
    pub empty: Option<String>,
}

/// Fallback behavior when no specific rule matches.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FallbackConfig {
    /// Number of lines to keep from the tail as a last resort.
    pub tail: Option<usize>,
}

/// One per-line regex replacement step.
///
/// Pattern is applied to each line; on match, the line is replaced with the
/// interpolated output template. Capture groups use `{1}`, `{2}`, … syntax.
/// Multiple rules run in order.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReplaceRule {
    pub pattern: String,
    pub output: String,
}

/// Supported scripting languages for the `[lua_script]` escape hatch.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ScriptLang {
    Luau,
}

/// Lua/Luau script escape hatch configuration.
/// Exactly one of `file` or `source` must be set.
/// `file` paths resolve relative to the current working directory.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScriptConfig {
    pub lang: ScriptLang,
    /// Path to a `.luau` file (resolved relative to CWD).
    pub file: Option<String>,
    /// Inline Luau source.
    pub source: Option<String>,
}

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

    fn load_filter(name: &str) -> FilterConfig {
        let path = format!("{}/filters/{name}", env!("CARGO_MANIFEST_DIR"));
        let content = std::fs::read_to_string(&path).unwrap();
        toml::from_str(&content).unwrap()
    }

    // --- CommandPattern deserialization ---

    #[test]
    fn test_command_pattern_single() {
        let cfg: FilterConfig = toml::from_str(r#"command = "git push""#).unwrap();
        assert_eq!(cfg.command, CommandPattern::Single("git push".to_string()));
        assert_eq!(cfg.command.first(), "git push");
        assert_eq!(cfg.command.patterns(), &["git push".to_string()]);
    }

    #[test]
    fn test_command_pattern_multiple() {
        let cfg: FilterConfig = toml::from_str(r#"command = ["pnpm test", "npm test"]"#).unwrap();
        assert_eq!(
            cfg.command,
            CommandPattern::Multiple(vec!["pnpm test".to_string(), "npm test".to_string()])
        );
        assert_eq!(cfg.command.first(), "pnpm test");
        assert_eq!(
            cfg.command.patterns(),
            &["pnpm test".to_string(), "npm test".to_string()]
        );
    }

    #[test]
    fn test_command_pattern_wildcard() {
        let cfg: FilterConfig = toml::from_str(r#"command = "npm run *""#).unwrap();
        assert_eq!(cfg.command.first(), "npm run *");
    }

    // --- Stdlib filter deserialization ---

    #[test]
    fn test_deserialize_git_push() {
        let cfg = load_filter("git/push.toml");

        assert_eq!(cfg.command.first(), "git push");
        assert_eq!(cfg.match_output.len(), 2);
        assert_eq!(cfg.match_output[0].contains, "Everything up-to-date");
        assert_eq!(cfg.match_output[1].contains, "rejected");

        let success = cfg.on_success.unwrap();
        assert_eq!(success.skip.len(), 8);
        assert!(success.skip[0].starts_with("^Enumerating"));

        let extract = success.extract.unwrap();
        assert!(extract.pattern.contains("->"));
        assert_eq!(extract.output, "ok \u{2713} {2}");

        let failure = cfg.on_failure.unwrap();
        assert_eq!(failure.tail, Some(10));
    }

    #[test]
    fn test_deserialize_git_status() {
        let cfg = load_filter("git/status.toml");

        assert_eq!(cfg.command.first(), "git status");
        assert_eq!(cfg.run.as_deref(), Some("git status --porcelain -b"));

        let parse = cfg.parse.unwrap();
        let branch = parse.branch.unwrap();
        assert_eq!(branch.line, 1);
        assert_eq!(branch.output, "{1}");

        let group = parse.group.unwrap();
        assert!(group.labels.contains_key("??"));
        assert_eq!(group.labels.get("M ").unwrap(), "modified");

        let output = cfg.output.unwrap();
        assert!(output.format.unwrap().contains("{branch}"));
        assert_eq!(
            output.group_counts_format.as_deref(),
            Some("  {label}: {count}")
        );
        assert_eq!(
            output.empty.as_deref(),
            Some("clean \u{2014} nothing to commit")
        );
    }

    #[test]
    fn test_deserialize_cargo_test() {
        let cfg = load_filter("cargo/test.toml");

        assert_eq!(cfg.command.first(), "cargo test");
        assert!(!cfg.skip.is_empty());
        assert!(cfg.skip.iter().any(|s| s.contains("Compiling")));

        assert_eq!(cfg.section.len(), 3);
        assert_eq!(cfg.section[0].name.as_deref(), Some("failures"));
        assert_eq!(cfg.section[0].collect_as.as_deref(), Some("failure_blocks"));
        assert_eq!(cfg.section[1].name.as_deref(), Some("failure_names"));
        assert_eq!(cfg.section[2].name.as_deref(), Some("summary"));

        let success = cfg.on_success.unwrap();
        let agg = success.aggregate.unwrap();
        assert_eq!(agg.from, "summary_lines");
        assert_eq!(agg.sum.as_deref(), Some("passed"));
        assert_eq!(agg.count_as.as_deref(), Some("suites"));
        assert!(success.output.unwrap().contains("{passed}"));

        let failure = cfg.on_failure.unwrap();
        assert!(failure.output.unwrap().contains("FAILURES"));

        let fallback = cfg.fallback.unwrap();
        assert_eq!(fallback.tail, Some(5));
    }

    #[test]
    fn test_deserialize_git_add() {
        let cfg = load_filter("git/add.toml");

        assert_eq!(cfg.command.first(), "git add");
        assert_eq!(cfg.match_output.len(), 1);
        assert_eq!(cfg.match_output[0].contains, "fatal:");

        let success = cfg.on_success.unwrap();
        assert_eq!(success.output.as_deref(), Some("ok \u{2713}"));

        let failure = cfg.on_failure.unwrap();
        assert_eq!(failure.tail, Some(5));
    }

    #[test]
    fn test_deserialize_git_commit() {
        let cfg = load_filter("git/commit.toml");

        assert_eq!(cfg.command.first(), "git commit");

        let success = cfg.on_success.unwrap();
        let extract = success.extract.unwrap();
        assert!(extract.pattern.contains("\\w+"));
        assert_eq!(extract.output, "ok \u{2713} {2}");

        let failure = cfg.on_failure.unwrap();
        assert_eq!(failure.tail, Some(5));
    }

    #[test]
    fn test_deserialize_git_log() {
        let cfg = load_filter("git/log.toml");

        assert_eq!(cfg.command.first(), "git log");

        let run = cfg.run.unwrap();
        assert!(run.contains("{args}"));
        assert!(run.contains("--oneline"));

        let success = cfg.on_success.unwrap();
        assert_eq!(success.output.as_deref(), Some("{output}"));
    }

    #[test]
    fn test_deserialize_git_diff() {
        let cfg = load_filter("git/diff.toml");

        assert_eq!(cfg.command.first(), "git diff");

        let run = cfg.run.unwrap();
        assert!(run.contains("--stat"));
        assert!(run.contains("{args}"));

        assert_eq!(cfg.match_output.len(), 1);
        assert_eq!(cfg.match_output[0].contains, "fatal:");

        let success = cfg.on_success.unwrap();
        assert_eq!(success.output.as_deref(), Some("{output}"));

        let failure = cfg.on_failure.unwrap();
        assert_eq!(failure.tail, Some(5));
    }

    // --- Minimal / defaults ---

    #[test]
    fn test_minimal_config_only_command() {
        let cfg: FilterConfig = toml::from_str(r#"command = "echo""#).unwrap();

        assert_eq!(cfg.command.first(), "echo");
        assert_eq!(cfg.run, None);
        assert!(cfg.skip.is_empty());
        assert!(cfg.keep.is_empty());
        assert!(cfg.step.is_empty());
        assert_eq!(cfg.extract, None);
        assert!(cfg.match_output.is_empty());
        assert!(cfg.section.is_empty());
        assert_eq!(cfg.on_success, None);
        assert_eq!(cfg.on_failure, None);
        assert_eq!(cfg.parse, None);
        assert_eq!(cfg.output, None);
        assert_eq!(cfg.fallback, None);
        assert!(cfg.replace.is_empty());
        assert!(!cfg.dedup);
        assert_eq!(cfg.dedup_window, None);
        assert_eq!(cfg.lua_script, None);
    }

    // --- Negative tests ---

    #[test]
    fn test_missing_command_field_fails() {
        let result: Result<FilterConfig, _> = toml::from_str(r#"run = "echo hello""#);
        assert!(result.is_err());
    }

    #[test]
    fn test_wrong_type_for_skip_fails() {
        let result: Result<FilterConfig, _> = toml::from_str(
            r#"command = "echo"
skip = "not-an-array""#,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_wrong_type_for_tail_fails() {
        let result: Result<FilterConfig, _> = toml::from_str(
            r#"command = "echo"
[on_success]
tail = "five""#,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_malformed_toml_fails() {
        let result: Result<FilterConfig, _> = toml::from_str("command = [unterminated");
        assert!(result.is_err());
    }

    #[test]
    fn test_empty_toml_fails() {
        let result: Result<FilterConfig, _> = toml::from_str("");
        assert!(result.is_err());
    }
}