Skip to main content

lean_ctx/core/patterns/
mod.rs

1/// Pattern engine version. Bump when output format changes to maintain
2/// shell-output determinism guarantees. Consumers (proofs, benchmarks)
3/// can embed this version to detect pattern-breaking updates.
4pub const PATTERN_ENGINE_VERSION: u32 = 1;
5
6pub mod ansible;
7pub mod artisan;
8pub mod aws;
9pub mod bazel;
10pub mod bun;
11pub mod cargo;
12pub mod clang;
13pub mod cmake;
14pub mod composer;
15pub mod curl;
16pub mod deno;
17pub mod deps_cmd;
18pub mod docker;
19pub mod dotnet;
20pub mod env_filter;
21pub mod eslint;
22pub mod fd;
23pub mod find;
24pub mod flutter;
25pub mod gh;
26pub mod git;
27pub mod glab;
28pub mod golang;
29pub mod grep;
30pub mod helm;
31pub mod json_schema;
32pub mod just;
33pub mod kubectl;
34pub mod log_dedup;
35pub mod ls;
36pub mod make;
37pub mod maven;
38pub mod mix;
39pub mod mypy;
40pub mod mysql;
41pub mod next_build;
42pub mod ninja;
43pub mod npm;
44pub mod php;
45pub mod pip;
46pub mod playwright;
47pub mod pnpm;
48pub mod poetry;
49pub mod prettier;
50pub mod prisma;
51pub mod psql;
52pub mod ruby;
53pub mod ruff;
54pub mod swift;
55pub mod sysinfo;
56pub mod systemd;
57pub mod terraform;
58pub mod test;
59pub mod typescript;
60pub mod wget;
61pub mod zig;
62
63use crate::core::tokens::count_tokens;
64
65pub fn compress_output(command: &str, output: &str) -> Option<String> {
66    // Policy gate: protected commands (Passthrough + Verbatim) must
67    // never be pattern-compressed. The caller (compress_if_beneficial
68    // or ctx_shell::handle) should have already checked, but we
69    // defend in depth here.
70    let policy = crate::shell::output_policy::classify(command, &[]);
71    if policy.is_protected() {
72        return None;
73    }
74
75    let cleaned = crate::core::compressor::strip_ansi(output);
76    let output = if cleaned.len() < output.len() {
77        &cleaned
78    } else {
79        output
80    };
81
82    if let Some(engine) = crate::core::filters::FilterEngine::load() {
83        if let Some(filtered) = engine.apply(command, output) {
84            return shorter_only(filtered, output);
85        }
86    }
87
88    if let Some(compressed) = try_specific_pattern(command, output) {
89        if let Some(r) = shorter_only(compressed, output) {
90            return Some(r);
91        }
92    }
93
94    if let Some(r) = json_schema::compress(output) {
95        if let Some(r) = shorter_only(r, output) {
96            return Some(r);
97        }
98    }
99
100    if let Some(r) = log_dedup::compress(output) {
101        if let Some(r) = shorter_only(r, output) {
102            return Some(r);
103        }
104    }
105
106    if let Some(r) = test::compress(output) {
107        if let Some(r) = shorter_only(r, output) {
108            return Some(r);
109        }
110    }
111
112    None
113}
114
115/// Collapse whitespace into single spaces so comparisons align with logical word tokens.
116fn normalize_shell_tokens(text: &str) -> String {
117    text.split_whitespace().collect::<Vec<_>>().join(" ")
118}
119
120fn shorter_only(compressed: String, original: &str) -> Option<String> {
121    let orig_n = normalize_shell_tokens(original);
122    let comp_n = normalize_shell_tokens(&compressed);
123    let ct_c = count_tokens(&comp_n);
124    let ct_o = count_tokens(&orig_n);
125    if ct_c < ct_o || (ct_c == ct_o && comp_n.len() < orig_n.len()) {
126        Some(compressed)
127    } else {
128        None
129    }
130}
131
132pub fn try_specific_pattern(cmd: &str, output: &str) -> Option<String> {
133    let cl = cmd.to_ascii_lowercase();
134    let c = cl.as_str();
135
136    if c.starts_with("git ") {
137        return git::compress(c, output);
138    }
139    if c.starts_with("gh ") {
140        return gh::compress(c, output);
141    }
142    if c.starts_with("glab ") {
143        return glab::try_glab_pattern(c, output);
144    }
145    if c == "terraform" || c.starts_with("terraform ") {
146        return terraform::compress(c, output);
147    }
148    if c == "make" || c.starts_with("make ") {
149        return make::compress(c, output);
150    }
151    if c == "just" || c.starts_with("just ") {
152        return just::compress(c, output);
153    }
154    if c.starts_with("mvn ")
155        || c.starts_with("./mvnw ")
156        || c.starts_with("mvnw ")
157        || c.starts_with("gradle ")
158        || c.starts_with("./gradlew ")
159        || c.starts_with("gradlew ")
160    {
161        return maven::compress(c, output);
162    }
163    if c.starts_with("kubectl ") || c.starts_with("k ") {
164        return kubectl::compress(c, output);
165    }
166    if c.starts_with("helm ") {
167        return helm::compress(c, output);
168    }
169    if c.starts_with("pnpm ") {
170        return pnpm::compress(c, output);
171    }
172    if c.starts_with("bun ") || c.starts_with("bunx ") {
173        return bun::compress(c, output);
174    }
175    if c.starts_with("deno ") {
176        return deno::compress(c, output);
177    }
178    if c.starts_with("npm ") || c.starts_with("yarn ") {
179        return npm::compress(c, output);
180    }
181    if c.starts_with("cargo ") {
182        return cargo::compress(c, output);
183    }
184    if c.starts_with("docker ") || c.starts_with("docker-compose ") {
185        return docker::compress(c, output);
186    }
187    if c.starts_with("pip ") || c.starts_with("pip3 ") || c.starts_with("python -m pip") {
188        return pip::compress(c, output);
189    }
190    if c.starts_with("mypy") || c.starts_with("python -m mypy") || c.starts_with("dmypy ") {
191        return mypy::compress(c, output);
192    }
193    if c.starts_with("pytest") || c.starts_with("python -m pytest") {
194        return test::compress(output);
195    }
196    if c.starts_with("ruff ") {
197        return ruff::compress(c, output);
198    }
199    if c.starts_with("eslint")
200        || c.starts_with("npx eslint")
201        || c.starts_with("biome ")
202        || c.starts_with("stylelint")
203    {
204        return eslint::compress(c, output);
205    }
206    if c.starts_with("prettier") || c.starts_with("npx prettier") {
207        return prettier::compress(output);
208    }
209    if c.starts_with("go ") || c.starts_with("golangci-lint") || c.starts_with("golint") {
210        return golang::compress(c, output);
211    }
212    if c.starts_with("playwright")
213        || c.starts_with("npx playwright")
214        || c.starts_with("cypress")
215        || c.starts_with("npx cypress")
216    {
217        return playwright::compress(c, output);
218    }
219    if c.starts_with("vitest") || c.starts_with("npx vitest") || c.starts_with("pnpm vitest") {
220        return test::compress(output);
221    }
222    if c.starts_with("next ")
223        || c.starts_with("npx next")
224        || c.starts_with("vite ")
225        || c.starts_with("npx vite")
226        || c.starts_with("vp ")
227        || c.starts_with("vite-plus ")
228    {
229        return next_build::compress(c, output);
230    }
231    if c.starts_with("tsc") || c.contains("typescript") {
232        return typescript::compress(output);
233    }
234    if c.starts_with("rubocop")
235        || c.starts_with("bundle ")
236        || c.starts_with("rake ")
237        || c.starts_with("rails test")
238        || c.starts_with("rspec")
239    {
240        return ruby::compress(c, output);
241    }
242    if c.starts_with("grep ") || c.starts_with("rg ") {
243        return grep::compress(output);
244    }
245    if c.starts_with("find ") {
246        return find::compress(output);
247    }
248    if c.starts_with("fd ") || c.starts_with("fdfind ") {
249        return fd::compress(output);
250    }
251    if c.starts_with("ls ") || c == "ls" {
252        return ls::compress(output);
253    }
254    if c.starts_with("curl ") {
255        return curl::compress_with_cmd(c, output);
256    }
257    if c.starts_with("wget ") {
258        return wget::compress(output);
259    }
260    if c == "env" || c.starts_with("env ") || c.starts_with("printenv") {
261        return env_filter::compress(output);
262    }
263    if c.starts_with("dotnet ") {
264        return dotnet::compress(c, output);
265    }
266    if c.starts_with("flutter ")
267        || (c.starts_with("dart ") && (c.contains(" analyze") || c.ends_with(" analyze")))
268    {
269        return flutter::compress(c, output);
270    }
271    if c.starts_with("poetry ")
272        || c.starts_with("uv sync")
273        || (c.starts_with("uv ") && c.contains("pip install"))
274        || c.starts_with("conda ")
275        || c.starts_with("mamba ")
276        || c.starts_with("pipx ")
277    {
278        return poetry::compress(c, output);
279    }
280    if c.starts_with("aws ") {
281        return aws::compress(c, output);
282    }
283    if c.starts_with("psql ") || c.starts_with("pg_") {
284        return psql::compress(c, output);
285    }
286    if c.starts_with("mysql ") || c.starts_with("mariadb ") {
287        return mysql::compress(c, output);
288    }
289    if c.starts_with("prisma ") || c.starts_with("npx prisma") {
290        return prisma::compress(c, output);
291    }
292    if c.starts_with("swift ") {
293        return swift::compress(c, output);
294    }
295    if c.starts_with("zig ") {
296        return zig::compress(c, output);
297    }
298    if c.starts_with("cmake ") || c.starts_with("ctest") {
299        return cmake::compress(c, output);
300    }
301    if c.starts_with("ninja") {
302        return ninja::compress(c, output);
303    }
304    if c.starts_with("ansible") || c.starts_with("ansible-playbook") {
305        return ansible::compress(c, output);
306    }
307    if c.starts_with("composer ") {
308        return composer::compress(c, output);
309    }
310    if c.starts_with("php artisan") || c.starts_with("artisan ") {
311        return artisan::compress(c, output);
312    }
313    if c.starts_with("./vendor/bin/pest") || c.starts_with("pest ") {
314        return artisan::compress("php artisan test", output);
315    }
316    if c.starts_with("mix ") || c.starts_with("iex ") {
317        return mix::compress(c, output);
318    }
319    if c.starts_with("bazel ") || c.starts_with("blaze ") {
320        return bazel::compress(c, output);
321    }
322    if c.starts_with("systemctl ") || c.starts_with("journalctl") {
323        return systemd::compress(c, output);
324    }
325    if c.starts_with("jest") || c.starts_with("npx jest") || c.starts_with("pnpm jest") {
326        return test::compress(output);
327    }
328    if c.starts_with("mocha") || c.starts_with("npx mocha") {
329        return test::compress(output);
330    }
331    if c.starts_with("tofu ") {
332        return terraform::compress(c, output);
333    }
334    if c.starts_with("ps ") || c == "ps" {
335        return sysinfo::compress_ps(output);
336    }
337    if c.starts_with("df ") || c == "df" {
338        return sysinfo::compress_df(output);
339    }
340    if c.starts_with("du ") || c == "du" {
341        return sysinfo::compress_du(output);
342    }
343    if c.starts_with("ping ") {
344        return sysinfo::compress_ping(output);
345    }
346    if c.starts_with("jq ") || c == "jq" {
347        return json_schema::compress(output);
348    }
349    if c.starts_with("hadolint") {
350        return eslint::compress(c, output);
351    }
352    if c.starts_with("yamllint") || c.starts_with("npx yamllint") {
353        return eslint::compress(c, output);
354    }
355    if c.starts_with("markdownlint") || c.starts_with("npx markdownlint") {
356        return eslint::compress(c, output);
357    }
358    if c.starts_with("oxlint") || c.starts_with("npx oxlint") {
359        return eslint::compress(c, output);
360    }
361    if c.starts_with("pyright") || c.starts_with("basedpyright") {
362        return mypy::compress(c, output);
363    }
364    if c.starts_with("turbo ") || c.starts_with("npx turbo") {
365        return npm::compress(c, output);
366    }
367    if c.starts_with("nx ") || c.starts_with("npx nx") {
368        return npm::compress(c, output);
369    }
370    if c.starts_with("clang++ ") || c.starts_with("clang ") {
371        return clang::compress(c, output);
372    }
373    if c.starts_with("gcc ")
374        || c.starts_with("g++ ")
375        || c.starts_with("cc ")
376        || c.starts_with("c++ ")
377    {
378        return cmake::compress(c, output);
379    }
380
381    None
382}
383
384#[cfg(test)]
385mod tests {
386    use super::*;
387
388    #[test]
389    fn routes_git_commands() {
390        let output = "On branch main\nnothing to commit";
391        assert!(compress_output("git status", output).is_some());
392    }
393
394    #[test]
395    fn routes_cargo_commands() {
396        let output = "   Compiling lean-ctx v2.1.1\n    Finished `release` profile [optimized] target(s) in 30.5s";
397        assert!(compress_output("cargo build --release", output).is_some());
398    }
399
400    #[test]
401    fn routes_npm_commands() {
402        let output = "added 150 packages, and audited 151 packages in 5s\n\n25 packages are looking for funding\n  run `npm fund` for details\n\nfound 0 vulnerabilities";
403        assert!(compress_output("npm install", output).is_some());
404    }
405
406    #[test]
407    fn routes_docker_commands() {
408        let output = "CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES";
409        // docker ps is Verbatim (via is_container_listing), so compress_output
410        // correctly returns None (policy gate). docker build should still compress.
411        assert!(compress_output("docker ps", output).is_none());
412        let build_output =
413            "Step 1/5 : FROM node:18\n ---> abc123\nStep 2/5 : COPY . .\nSuccessfully built def456";
414        assert!(compress_output("docker build .", build_output).is_some());
415    }
416
417    #[test]
418    fn routes_mypy_commands() {
419        let output = "src/main.py:10: error: Missing return  [return]\nFound 1 error in 1 file (checked 3 source files)";
420        assert!(compress_output("mypy .", output).is_some());
421        assert!(compress_output("python -m mypy src/", output).is_some());
422    }
423
424    #[test]
425    fn routes_pytest_commands() {
426        let output = "===== test session starts =====\ncollected 5 items\ntest_main.py ..... [100%]\n===== 5 passed in 0.5s =====";
427        assert!(compress_output("pytest", output).is_some());
428        assert!(compress_output("python -m pytest tests/", output).is_some());
429    }
430
431    #[test]
432    fn unknown_command_returns_none() {
433        assert!(compress_output("some-unknown-tool --version", "v1.0").is_none());
434    }
435
436    #[test]
437    fn case_insensitive_routing() {
438        let output = "On branch main\nnothing to commit";
439        assert!(compress_output("Git Status", output).is_some());
440        assert!(compress_output("GIT STATUS", output).is_some());
441    }
442
443    #[test]
444    fn routes_vp_and_vite_plus() {
445        let output = "  VITE v5.0.0  ready in 200 ms\n\n  -> Local:   http://localhost:5173/\n  -> Network: http://192.168.1.2:5173/";
446        assert!(compress_output("vp build", output).is_some());
447        assert!(compress_output("vite-plus build", output).is_some());
448    }
449
450    #[test]
451    fn routes_bunx_commands() {
452        let output = "1 pass tests\n0 fail tests\n3 skip tests\nDone 12ms\nsome extra line\nmore output here";
453        let result = compress_output("bunx test", output);
454        assert!(
455            result.is_some(),
456            "bunx should compress when output is large enough"
457        );
458        assert!(result.unwrap().contains("bun test: 1 passed"));
459    }
460
461    #[test]
462    fn routes_deno_task() {
463        let output = "Task dev deno run --allow-net server.ts\nListening on http://localhost:8000";
464        assert!(try_specific_pattern("deno task dev", output).is_some());
465    }
466
467    #[test]
468    fn routes_jest_commands() {
469        let output = "PASS  tests/main.test.js\nTest Suites: 1 passed, 1 total\nTests:       5 passed, 5 total\nTime:        2.5 s";
470        assert!(try_specific_pattern("jest", output).is_some());
471        assert!(try_specific_pattern("npx jest --coverage", output).is_some());
472    }
473
474    #[test]
475    fn routes_mocha_commands() {
476        let output = "  3 passing (50ms)\n  1 failing\n\n  1) Array #indexOf():\n     Error: expected -1 to equal 0";
477        assert!(try_specific_pattern("mocha", output).is_some());
478        assert!(try_specific_pattern("npx mocha tests/", output).is_some());
479    }
480
481    #[test]
482    fn routes_tofu_commands() {
483        let output = "Initializing the backend...\nInitializing provider plugins...\nTerraform has been successfully initialized!";
484        assert!(try_specific_pattern("tofu init", output).is_some());
485    }
486
487    #[test]
488    fn routes_ps_commands() {
489        let mut lines = vec!["USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND".to_string()];
490        for i in 0..20 {
491            lines.push(format!("user {i} 0.0 0.1 1234 123 ? S 10:00 0:00 proc_{i}"));
492        }
493        let output = lines.join("\n");
494        assert!(try_specific_pattern("ps aux", &output).is_some());
495    }
496
497    #[test]
498    fn routes_ping_commands() {
499        let output = "PING google.com (1.2.3.4): 56 data bytes\n64 bytes from 1.2.3.4: icmp_seq=0 ttl=116 time=12ms\n3 packets transmitted, 3 packets received, 0.0% packet loss\nrtt min/avg/max/stddev = 11/12/13/1 ms";
500        assert!(try_specific_pattern("ping -c 3 google.com", output).is_some());
501    }
502
503    #[test]
504    fn routes_jq_to_json_schema() {
505        let output = "{\"name\": \"test\", \"version\": \"1.0\", \"items\": [{\"id\": 1}, {\"id\": 2}, {\"id\": 3}, {\"id\": 4}, {\"id\": 5}, {\"id\": 6}, {\"id\": 7}, {\"id\": 8}, {\"id\": 9}, {\"id\": 10}]}";
506        assert!(try_specific_pattern("jq '.items' data.json", output).is_some());
507    }
508
509    #[test]
510    fn routes_linting_tools() {
511        let lint_output = "src/main.py:10: error: Missing return\nsrc/main.py:20: error: Unused var\nFound 2 errors";
512        assert!(try_specific_pattern("hadolint Dockerfile", lint_output).is_some());
513        assert!(try_specific_pattern("oxlint src/", lint_output).is_some());
514        assert!(try_specific_pattern("pyright src/", lint_output).is_some());
515        assert!(try_specific_pattern("basedpyright src/", lint_output).is_some());
516    }
517
518    #[test]
519    fn routes_fd_commands() {
520        let output = "src/main.rs\nsrc/lib.rs\nsrc/util/helpers.rs\nsrc/util/math.rs\ntests/integration.rs\n";
521        assert!(try_specific_pattern("fd --extension rs", output).is_some());
522        assert!(try_specific_pattern("fdfind .rs", output).is_some());
523    }
524
525    #[test]
526    fn routes_just_commands() {
527        let output = "Available recipes:\n    build\n    test\n    lint\n";
528        assert!(try_specific_pattern("just --list", output).is_some());
529        assert!(try_specific_pattern("just build", output).is_some());
530    }
531
532    #[test]
533    fn routes_ninja_commands() {
534        let output = "[1/10] Compiling foo.c\n[10/10] Linking app\n";
535        assert!(try_specific_pattern("ninja", output).is_some());
536        assert!(try_specific_pattern("ninja -j4", output).is_some());
537    }
538
539    #[test]
540    fn routes_clang_commands() {
541        let output =
542            "src/main.c:10:5: error: use of undeclared identifier 'foo'\n1 error generated.\n";
543        assert!(try_specific_pattern("clang src/main.c", output).is_some());
544        assert!(try_specific_pattern("clang++ -std=c++17 main.cpp", output).is_some());
545    }
546
547    #[test]
548    fn routes_cargo_run() {
549        let output = "   Compiling foo v0.1.0\n    Finished `dev` profile\nHello, world!";
550        assert!(try_specific_pattern("cargo run", output).is_some());
551    }
552
553    #[test]
554    fn routes_cargo_bench() {
555        let output = "   Compiling foo v0.1.0\ntest bench_parse ... bench: 1234 ns/iter";
556        assert!(try_specific_pattern("cargo bench", output).is_some());
557    }
558
559    #[test]
560    fn routes_build_tools() {
561        let build_output = "   Compiling foo v0.1.0\n    Finished release [optimized]";
562        assert!(try_specific_pattern("gcc -o main main.c", build_output).is_some());
563        assert!(try_specific_pattern("g++ -o main main.cpp", build_output).is_some());
564    }
565
566    #[test]
567    fn routes_monorepo_tools() {
568        let output = "npm warn deprecated inflight@1.0.6\nnpm warn deprecated rimraf@3.0.2\nadded 150 packages, and audited 151 packages in 5s\n\n25 packages are looking for funding\n  run `npm fund` for details\n\nfound 0 vulnerabilities";
569        assert!(try_specific_pattern("turbo install", output).is_some());
570        assert!(try_specific_pattern("nx install", output).is_some());
571    }
572
573    #[test]
574    fn gh_api_passthrough_never_compresses() {
575        let huge = "line\n".repeat(5000);
576        assert!(
577            compress_output("gh api repos/owner/repo/actions/jobs/123/logs", &huge).is_none(),
578            "gh api must never be compressed, even for large output"
579        );
580        assert!(compress_output("gh api repos/owner/repo/actions/runs/123/logs", &huge).is_none());
581    }
582
583    #[test]
584    fn gh_log_flags_passthrough() {
585        let huge = "line\n".repeat(5000);
586        assert!(compress_output("gh run view 123 --log-failed", &huge).is_none());
587        assert!(compress_output("gh run view 123 --log", &huge).is_none());
588    }
589
590    #[test]
591    fn gh_structured_commands_still_compress() {
592        let output = "On branch main\nnothing to commit";
593        assert!(try_specific_pattern("gh pr list", output).is_some());
594        assert!(try_specific_pattern("gh run list", output).is_some());
595    }
596}