tokf 0.2.21

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
#![allow(clippy::unwrap_used, clippy::expect_used)]

use std::process::Command;

fn tokf() -> Command {
    Command::new(env!("CARGO_BIN_EXE_tokf"))
}

/// Helper: produce deterministic 10-line output for baseline tests.
const TEN_LINE_CMD: &str = "for i in 1 2 3 4 5 6 7 8 9 10; do echo line$i; done";

/// Helper: query the last event from the tracking DB.
fn last_event(db_path: &std::path::Path) -> (i64, i64) {
    let conn = rusqlite::Connection::open(db_path).unwrap();
    conn.query_row(
        "SELECT input_bytes, output_bytes FROM events ORDER BY rowid DESC LIMIT 1",
        [],
        |row| Ok((row.get(0)?, row.get(1)?)),
    )
    .unwrap()
}

/// Helper: query the last event including `pipe_override`.
fn last_event_full(db_path: &std::path::Path) -> (i64, i64, bool) {
    let conn = rusqlite::Connection::open(db_path).unwrap();
    conn.query_row(
        "SELECT input_bytes, output_bytes, pipe_override FROM events ORDER BY rowid DESC LIMIT 1",
        [],
        |row| {
            let po: i64 = row.get(2)?;
            Ok((row.get(0)?, row.get(1)?, po != 0))
        },
    )
    .unwrap()
}

/// Helper: set up a temp dir with a filter that matches `echo` and returns a fixed output.
fn setup_filter_dir(filter_output: &str) -> tempfile::TempDir {
    let dir = tempfile::TempDir::new().unwrap();
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    std::fs::write(
        filters_dir.join("echo.toml"),
        format!("command = \"echo\"\n[on_success]\noutput = \"{filter_output}\""),
    )
    .unwrap();
    dir
}

// --- Core baseline-pipe tracking ---

/// With --baseline-pipe 'tail -3', `input_bytes` should reflect ~3 lines, not all 10.
#[test]
fn baseline_pipe_records_piped_byte_count() {
    let dir = tempfile::TempDir::new().unwrap();
    let db_path = dir.path().join("test-fair.db");

    let output = tokf()
        .args([
            "run",
            "--no-filter",
            "--baseline-pipe",
            "tail -3",
            "sh",
            "-c",
            TEN_LINE_CMD,
        ])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "tokf run failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let (input_bytes, _) = last_event(&db_path);

    // `tail -3` of 10 lines → ~18-20 bytes. Must be much less than full (~61 bytes).
    assert!(
        input_bytes > 0,
        "input_bytes should be positive, got {input_bytes}"
    );
    assert!(
        input_bytes < 30,
        "input_bytes should reflect ~3 lines (~18 bytes), got {input_bytes}"
    );
}

/// Without --baseline-pipe, `input_bytes` should be the full output length.
#[test]
fn no_baseline_pipe_records_full_byte_count() {
    let dir = tempfile::TempDir::new().unwrap();
    let db_path = dir.path().join("test-full.db");

    let output = tokf()
        .args(["run", "--no-filter", "sh", "-c", TEN_LINE_CMD])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let (input_bytes, _) = last_event(&db_path);

    // Full output: "line1\nline2\n...line10\n" ≈ 61 bytes
    assert!(
        input_bytes > 50,
        "input_bytes should reflect full output (~61 bytes), got {input_bytes}"
    );
}

// --- Passthrough output_bytes fix (remediation #2) ---

/// In the no-filter passthrough path, `output_bytes` should be the full raw output
/// (what tokf actually printed), not the piped baseline.
#[test]
fn passthrough_output_bytes_is_full_output() {
    let dir = tempfile::TempDir::new().unwrap();
    let db_path = dir.path().join("test-passthrough.db");

    let output = tokf()
        .args([
            "run",
            "--no-filter",
            "--baseline-pipe",
            "tail -3",
            "sh",
            "-c",
            TEN_LINE_CMD,
        ])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let (input_bytes, output_bytes) = last_event(&db_path);

    // input_bytes = piped baseline (~18 bytes), output_bytes = full raw (~61 bytes)
    assert!(
        input_bytes < 30,
        "input_bytes should be piped baseline, got {input_bytes}"
    );
    assert!(
        output_bytes > 50,
        "output_bytes should be full output, got {output_bytes}"
    );
    assert!(
        output_bytes > input_bytes,
        "output_bytes ({output_bytes}) should exceed input_bytes ({input_bytes})"
    );
}

// --- Pipe command validation (remediation #5) ---

/// An unrecognised pipe command is rejected and falls back to full output.
#[test]
fn baseline_pipe_rejects_unknown_command() {
    let dir = tempfile::TempDir::new().unwrap();
    let db_path = dir.path().join("test-reject.db");

    let output = tokf()
        .args([
            "run",
            "--no-filter",
            "--baseline-pipe",
            "wc -l",
            "sh",
            "-c",
            TEN_LINE_CMD,
        ])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    // Should have warned on stderr
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("not allowed"),
        "expected rejection warning, got: {stderr}"
    );

    // Should fall back to full output size
    let (input_bytes, _) = last_event(&db_path);
    assert!(
        input_bytes > 50,
        "should fall back to full output, got {input_bytes}"
    );
}

// --- Pipe command failure (remediation #3) ---

/// When the pipe command fails (e.g. grep with bad args producing no output),
/// tokf does not crash and still records a tracking event.
#[test]
fn baseline_pipe_does_not_crash_on_pipe_failure() {
    let dir = tempfile::TempDir::new().unwrap();
    let db_path = dir.path().join("test-fail.db");

    // grep with --nonexistent-flag exits with error and produces no stdout.
    // compute_baseline will return 0 (stdout was empty). The important thing
    // is that tokf doesn't crash and the event is recorded.
    let output = tokf()
        .args([
            "run",
            "--no-filter",
            "--baseline-pipe",
            "grep --nonexistent-flag-xyz",
            "sh",
            "-c",
            TEN_LINE_CMD,
        ])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "tokf should not crash on pipe failure: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Tracking should still have recorded an event (input_bytes may be 0
    // since the failing grep produced no stdout).
    let conn = rusqlite::Connection::open(&db_path).unwrap();
    let count: i64 = conn
        .query_row("SELECT COUNT(*) FROM events", [], |row| row.get(0))
        .unwrap();
    assert_eq!(count, 1, "should record exactly one event");
}

// --- Grep baseline ---

/// baseline-pipe with grep records byte count of matching lines only.
#[test]
fn baseline_pipe_grep_records_matching_lines() {
    let dir = tempfile::TempDir::new().unwrap();
    let db_path = dir.path().join("test-grep.db");

    // Only "line1" and "line10" match "line1" → ~12 bytes (line1\nline10\n)
    let output = tokf()
        .args([
            "run",
            "--no-filter",
            "--baseline-pipe",
            "grep line1",
            "sh",
            "-c",
            TEN_LINE_CMD,
        ])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let (input_bytes, _) = last_event(&db_path);

    // "line1\nline10\n" = 12 bytes. Must be much less than full (~61 bytes).
    assert!(
        input_bytes > 0,
        "input_bytes should be positive, got {input_bytes}"
    );
    assert!(
        input_bytes < 20,
        "input_bytes should reflect matching lines (~12 bytes), got {input_bytes}"
    );
}

// --- Empty output ---

/// baseline-pipe with a command that produces no output records `input_bytes` = 0.
#[test]
fn baseline_pipe_empty_output() {
    let dir = tempfile::TempDir::new().unwrap();
    let db_path = dir.path().join("test-empty.db");

    let output = tokf()
        .args([
            "run",
            "--no-filter",
            "--baseline-pipe",
            "tail -3",
            "sh",
            "-c",
            "exit 0",
        ])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let (input_bytes, output_bytes) = last_event(&db_path);
    assert_eq!(input_bytes, 0, "empty command → input_bytes = 0");
    assert_eq!(output_bytes, 0, "empty command → output_bytes = 0");
}

// --- Head baseline ---

/// baseline-pipe with head records byte count of first N lines.
#[test]
fn baseline_pipe_head_records_first_lines() {
    let dir = tempfile::TempDir::new().unwrap();
    let db_path = dir.path().join("test-head.db");

    let output = tokf()
        .args([
            "run",
            "--no-filter",
            "--baseline-pipe",
            "head -2",
            "sh",
            "-c",
            TEN_LINE_CMD,
        ])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let (input_bytes, _) = last_event(&db_path);

    // "line1\nline2\n" = 12 bytes
    assert!(
        input_bytes > 0 && input_bytes < 20,
        "input_bytes should reflect ~2 lines (~12 bytes), got {input_bytes}"
    );
}

// --- prefer-less runtime tests ---

/// When filtered output is smaller than piped output, prefer-less uses filtered.
#[test]
fn prefer_less_uses_filtered_when_smaller() {
    // Filter produces "filtered" (8 bytes). Raw = "hello\n" (6 bytes).
    // Pipe = tail -1 of "hello\n" ≈ 6 bytes. Filter (8) > pipe (6) → uses filter?
    // Actually we need filtered < piped. Let's make raw output large:
    //   echo produces many words → raw is large → tail -1 returns last line (small)
    //   but filter returns "filtered" which is also small.
    // Simpler: use a filter that returns very short output, a command that produces
    // large output, and a pipe that returns most of it.
    //
    // echo "aaa...long..." → raw = long string → grep "a" → returns all of it (long)
    // filter returns "filtered" (8 bytes) → 8 < long → uses filtered, pipe_override=false.
    let dir = setup_filter_dir("filtered");
    let db_path = dir.path().join("test-pl-filtered.db");

    let output = tokf()
        .args([
            "run",
            "--baseline-pipe",
            "grep a",
            "--prefer-less",
            "echo",
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
        ])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("filtered"),
        "should print filtered output, got: {stdout}"
    );

    let (_, _, pipe_override) = last_event_full(&db_path);
    assert!(!pipe_override, "pipe_override should be false");
}

/// When piped output is smaller than filtered output, prefer-less uses piped.
#[test]
fn prefer_less_uses_piped_when_smaller() {
    // Filter returns a long string. Raw = 10 lines. tail -1 = 1 line (~7 bytes).
    // Filter output > 7 bytes → uses piped, pipe_override=true.
    let dir = tempfile::TempDir::new().unwrap();
    let filters_dir = dir.path().join(".tokf/filters");
    std::fs::create_dir_all(&filters_dir).unwrap();
    // Filter that produces output much larger than `tail -1` of 10 lines.
    let long_output = "x".repeat(200);
    std::fs::write(
        filters_dir.join("sh.toml"),
        format!("command = \"sh\"\n[on_success]\noutput = \"{long_output}\""),
    )
    .unwrap();
    let db_path = dir.path().join("test-pl-piped.db");

    let output = tokf()
        .args([
            "run",
            "--baseline-pipe",
            "tail -1",
            "--prefer-less",
            "sh",
            "-c",
            TEN_LINE_CMD,
        ])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Should print the piped output (tail -1 = "line10"), not the long filter output.
    assert!(
        stdout.contains("line10"),
        "should print piped output (tail -1), got: {stdout}"
    );
    assert!(
        !stdout.contains(&long_output),
        "should NOT print filter output"
    );

    let (_, _, pipe_override) = last_event_full(&db_path);
    assert!(pipe_override, "pipe_override should be true");
}

/// --prefer-less without --baseline-pipe is a no-op (runs normally).
#[test]
fn prefer_less_without_baseline_pipe_is_noop() {
    let output = tokf()
        .args(["run", "--prefer-less", "echo", "hello"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "should succeed as a no-op, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("hello"),
        "should print normal output, got: {stdout}"
    );
}

/// When `compute_output` returns `None` (disallowed command), prefer-less falls back to filtered.
#[test]
fn prefer_less_fallback_on_disallowed_pipe() {
    let dir = setup_filter_dir("filtered-ok");
    let db_path = dir.path().join("test-pl-fail.db");

    // "wc" is not in the allowed commands whitelist, so compute_output returns None.
    let output = tokf()
        .args([
            "run",
            "--baseline-pipe",
            "wc -l",
            "--prefer-less",
            "echo",
            "hello",
        ])
        .env("TOKF_DB_PATH", &db_path)
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "tokf should not crash, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("filtered-ok"),
        "should fall back to filtered output, got: {stdout}"
    );

    let (_, _, pipe_override) = last_event_full(&db_path);
    assert!(!pipe_override, "pipe_override should be false on failure");
}

/// When --prefer-less is used on the passthrough path (no filter), verbose warns.
#[test]
fn prefer_less_passthrough_verbose_warns() {
    let dir = tempfile::TempDir::new().unwrap();
    // No filters set up — passthrough path.
    let output = tokf()
        .args([
            "--verbose",
            "run",
            "--baseline-pipe",
            "tail -1",
            "--prefer-less",
            "echo",
            "hello",
        ])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("--prefer-less has no effect"),
        "should warn about no effect, got: {stderr}"
    );
}