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
63pub fn compress_output(command: &str, output: &str) -> Option<String> {
64    let cleaned = crate::core::compressor::strip_ansi(output);
65    let output = if cleaned.len() < output.len() {
66        &cleaned
67    } else {
68        output
69    };
70
71    if let Some(engine) = crate::core::filters::FilterEngine::load() {
72        if let Some(filtered) = engine.apply(command, output) {
73            return Some(filtered);
74        }
75    }
76
77    let specific = try_specific_pattern(command, output);
78    if specific.is_some() {
79        return specific;
80    }
81
82    if let Some(r) = json_schema::compress(output) {
83        return Some(r);
84    }
85
86    if let Some(r) = log_dedup::compress(output) {
87        return Some(r);
88    }
89
90    if let Some(r) = test::compress(output) {
91        return Some(r);
92    }
93
94    if output.len() > 8000 {
95        return Some(truncate_large_output(command, output));
96    }
97
98    None
99}
100
101fn truncate_large_output(command: &str, output: &str) -> String {
102    let lines: Vec<&str> = output.lines().collect();
103    let total = lines.len();
104    let size = output.len();
105    let head = 30.min(total);
106    let tail = 15.min(total.saturating_sub(head));
107
108    let mut result = String::with_capacity(4096);
109    let cmd_short = if command.len() > 60 {
110        &command[..60]
111    } else {
112        command
113    };
114    result.push_str(&format!("{cmd_short} ({size} bytes, {total} lines):\n"));
115    for line in lines.iter().take(head) {
116        if line.len() > 300 {
117            result.push_str(&line[..300]);
118            result.push_str("…\n");
119        } else {
120            result.push_str(line);
121            result.push('\n');
122        }
123    }
124    if total > head + tail {
125        result.push_str(&format!(
126            "\n[… {} lines omitted …]\n\n",
127            total - head - tail
128        ));
129        for line in lines.iter().skip(total - tail) {
130            if line.len() > 300 {
131                result.push_str(&line[..300]);
132                result.push_str("…\n");
133            } else {
134                result.push_str(line);
135                result.push('\n');
136            }
137        }
138    }
139    result
140}
141
142fn try_specific_pattern(cmd: &str, output: &str) -> Option<String> {
143    let cl = cmd.to_ascii_lowercase();
144    let c = cl.as_str();
145
146    if c.starts_with("git ") {
147        return git::compress(c, output);
148    }
149    if c.starts_with("gh ") {
150        return gh::compress(c, output);
151    }
152    if c.starts_with("glab ") {
153        return glab::try_glab_pattern(c, output);
154    }
155    if c == "terraform" || c.starts_with("terraform ") {
156        return terraform::compress(c, output);
157    }
158    if c == "make" || c.starts_with("make ") {
159        return make::compress(c, output);
160    }
161    if c == "just" || c.starts_with("just ") {
162        return just::compress(c, output);
163    }
164    if c.starts_with("mvn ")
165        || c.starts_with("./mvnw ")
166        || c.starts_with("mvnw ")
167        || c.starts_with("gradle ")
168        || c.starts_with("./gradlew ")
169        || c.starts_with("gradlew ")
170    {
171        return maven::compress(c, output);
172    }
173    if c.starts_with("kubectl ") || c.starts_with("k ") {
174        return kubectl::compress(c, output);
175    }
176    if c.starts_with("helm ") {
177        return helm::compress(c, output);
178    }
179    if c.starts_with("pnpm ") {
180        return pnpm::compress(c, output);
181    }
182    if c.starts_with("bun ") || c.starts_with("bunx ") {
183        return bun::compress(c, output);
184    }
185    if c.starts_with("deno ") {
186        return deno::compress(c, output);
187    }
188    if c.starts_with("npm ") || c.starts_with("yarn ") {
189        return npm::compress(c, output);
190    }
191    if c.starts_with("cargo ") {
192        return cargo::compress(c, output);
193    }
194    if c.starts_with("docker ") || c.starts_with("docker-compose ") {
195        return docker::compress(c, output);
196    }
197    if c.starts_with("pip ") || c.starts_with("pip3 ") || c.starts_with("python -m pip") {
198        return pip::compress(c, output);
199    }
200    if c.starts_with("mypy") || c.starts_with("python -m mypy") || c.starts_with("dmypy ") {
201        return mypy::compress(c, output);
202    }
203    if c.starts_with("pytest") || c.starts_with("python -m pytest") {
204        return test::compress(output);
205    }
206    if c.starts_with("ruff ") {
207        return ruff::compress(c, output);
208    }
209    if c.starts_with("eslint")
210        || c.starts_with("npx eslint")
211        || c.starts_with("biome ")
212        || c.starts_with("stylelint")
213    {
214        return eslint::compress(c, output);
215    }
216    if c.starts_with("prettier") || c.starts_with("npx prettier") {
217        return prettier::compress(output);
218    }
219    if c.starts_with("go ") || c.starts_with("golangci-lint") || c.starts_with("golint") {
220        return golang::compress(c, output);
221    }
222    if c.starts_with("playwright")
223        || c.starts_with("npx playwright")
224        || c.starts_with("cypress")
225        || c.starts_with("npx cypress")
226    {
227        return playwright::compress(c, output);
228    }
229    if c.starts_with("vitest") || c.starts_with("npx vitest") || c.starts_with("pnpm vitest") {
230        return test::compress(output);
231    }
232    if c.starts_with("next ")
233        || c.starts_with("npx next")
234        || c.starts_with("vite ")
235        || c.starts_with("npx vite")
236        || c.starts_with("vp ")
237        || c.starts_with("vite-plus ")
238    {
239        return next_build::compress(c, output);
240    }
241    if c.starts_with("tsc") || c.contains("typescript") {
242        return typescript::compress(output);
243    }
244    if c.starts_with("rubocop")
245        || c.starts_with("bundle ")
246        || c.starts_with("rake ")
247        || c.starts_with("rails test")
248        || c.starts_with("rspec")
249    {
250        return ruby::compress(c, output);
251    }
252    if c.starts_with("grep ") || c.starts_with("rg ") {
253        return grep::compress(output);
254    }
255    if c.starts_with("find ") {
256        return find::compress(output);
257    }
258    if c.starts_with("fd ") || c.starts_with("fdfind ") {
259        return fd::compress(output);
260    }
261    if c.starts_with("ls ") || c == "ls" {
262        return ls::compress(output);
263    }
264    if c.starts_with("curl ") {
265        return curl::compress_with_cmd(c, output);
266    }
267    if c.starts_with("wget ") {
268        return wget::compress(output);
269    }
270    if c == "env" || c.starts_with("env ") || c.starts_with("printenv") {
271        return env_filter::compress(output);
272    }
273    if c.starts_with("dotnet ") {
274        return dotnet::compress(c, output);
275    }
276    if c.starts_with("flutter ")
277        || (c.starts_with("dart ") && (c.contains(" analyze") || c.ends_with(" analyze")))
278    {
279        return flutter::compress(c, output);
280    }
281    if c.starts_with("poetry ")
282        || c.starts_with("uv sync")
283        || (c.starts_with("uv ") && c.contains("pip install"))
284        || c.starts_with("conda ")
285        || c.starts_with("mamba ")
286        || c.starts_with("pipx ")
287    {
288        return poetry::compress(c, output);
289    }
290    if c.starts_with("aws ") {
291        return aws::compress(c, output);
292    }
293    if c.starts_with("psql ") || c.starts_with("pg_") {
294        return psql::compress(c, output);
295    }
296    if c.starts_with("mysql ") || c.starts_with("mariadb ") {
297        return mysql::compress(c, output);
298    }
299    if c.starts_with("prisma ") || c.starts_with("npx prisma") {
300        return prisma::compress(c, output);
301    }
302    if c.starts_with("swift ") {
303        return swift::compress(c, output);
304    }
305    if c.starts_with("zig ") {
306        return zig::compress(c, output);
307    }
308    if c.starts_with("cmake ") || c.starts_with("ctest") {
309        return cmake::compress(c, output);
310    }
311    if c.starts_with("ninja") {
312        return ninja::compress(c, output);
313    }
314    if c.starts_with("ansible") || c.starts_with("ansible-playbook") {
315        return ansible::compress(c, output);
316    }
317    if c.starts_with("composer ") {
318        return composer::compress(c, output);
319    }
320    if c.starts_with("php artisan") || c.starts_with("artisan ") {
321        return artisan::compress(c, output);
322    }
323    if c.starts_with("./vendor/bin/pest") || c.starts_with("pest ") {
324        return artisan::compress("php artisan test", output);
325    }
326    if c.starts_with("mix ") || c.starts_with("iex ") {
327        return mix::compress(c, output);
328    }
329    if c.starts_with("bazel ") || c.starts_with("blaze ") {
330        return bazel::compress(c, output);
331    }
332    if c.starts_with("systemctl ") || c.starts_with("journalctl") {
333        return systemd::compress(c, output);
334    }
335    if c.starts_with("jest") || c.starts_with("npx jest") || c.starts_with("pnpm jest") {
336        return test::compress(output);
337    }
338    if c.starts_with("mocha") || c.starts_with("npx mocha") {
339        return test::compress(output);
340    }
341    if c.starts_with("tofu ") {
342        return terraform::compress(c, output);
343    }
344    if c.starts_with("ps ") || c == "ps" {
345        return sysinfo::compress_ps(output);
346    }
347    if c.starts_with("df ") || c == "df" {
348        return sysinfo::compress_df(output);
349    }
350    if c.starts_with("du ") || c == "du" {
351        return sysinfo::compress_du(output);
352    }
353    if c.starts_with("ping ") {
354        return sysinfo::compress_ping(output);
355    }
356    if c.starts_with("jq ") || c == "jq" {
357        return json_schema::compress(output);
358    }
359    if c.starts_with("hadolint") {
360        return eslint::compress(c, output);
361    }
362    if c.starts_with("yamllint") || c.starts_with("npx yamllint") {
363        return eslint::compress(c, output);
364    }
365    if c.starts_with("markdownlint") || c.starts_with("npx markdownlint") {
366        return eslint::compress(c, output);
367    }
368    if c.starts_with("oxlint") || c.starts_with("npx oxlint") {
369        return eslint::compress(c, output);
370    }
371    if c.starts_with("pyright") || c.starts_with("basedpyright") {
372        return mypy::compress(c, output);
373    }
374    if c.starts_with("turbo ") || c.starts_with("npx turbo") {
375        return npm::compress(c, output);
376    }
377    if c.starts_with("nx ") || c.starts_with("npx nx") {
378        return npm::compress(c, output);
379    }
380    if c.starts_with("clang++ ") || c.starts_with("clang ") {
381        return clang::compress(c, output);
382    }
383    if c.starts_with("gcc ")
384        || c.starts_with("g++ ")
385        || c.starts_with("cc ")
386        || c.starts_with("c++ ")
387    {
388        return cmake::compress(c, output);
389    }
390
391    None
392}
393
394#[cfg(test)]
395mod tests {
396    use super::*;
397
398    #[test]
399    fn routes_git_commands() {
400        let output = "On branch main\nnothing to commit";
401        assert!(compress_output("git status", output).is_some());
402    }
403
404    #[test]
405    fn routes_cargo_commands() {
406        let output = "   Compiling lean-ctx v2.1.1\n    Finished `release` profile [optimized] target(s) in 30.5s";
407        assert!(compress_output("cargo build --release", output).is_some());
408    }
409
410    #[test]
411    fn routes_npm_commands() {
412        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";
413        assert!(compress_output("npm install", output).is_some());
414    }
415
416    #[test]
417    fn routes_docker_commands() {
418        let output = "CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES";
419        assert!(compress_output("docker ps", output).is_some());
420    }
421
422    #[test]
423    fn routes_mypy_commands() {
424        let output = "src/main.py:10: error: Missing return  [return]\nFound 1 error in 1 file (checked 3 source files)";
425        assert!(compress_output("mypy .", output).is_some());
426        assert!(compress_output("python -m mypy src/", output).is_some());
427    }
428
429    #[test]
430    fn routes_pytest_commands() {
431        let output = "===== test session starts =====\ncollected 5 items\ntest_main.py ..... [100%]\n===== 5 passed in 0.5s =====";
432        assert!(compress_output("pytest", output).is_some());
433        assert!(compress_output("python -m pytest tests/", output).is_some());
434    }
435
436    #[test]
437    fn unknown_command_returns_none() {
438        assert!(compress_output("some-unknown-tool --version", "v1.0").is_none());
439    }
440
441    #[test]
442    fn case_insensitive_routing() {
443        let output = "On branch main\nnothing to commit";
444        assert!(compress_output("Git Status", output).is_some());
445        assert!(compress_output("GIT STATUS", output).is_some());
446    }
447
448    #[test]
449    fn routes_vp_and_vite_plus() {
450        let output = "  VITE v5.0.0  ready in 200 ms\n\n  -> Local:   http://localhost:5173/\n  -> Network: http://192.168.1.2:5173/";
451        assert!(compress_output("vp build", output).is_some());
452        assert!(compress_output("vite-plus build", output).is_some());
453    }
454
455    #[test]
456    fn routes_bunx_commands() {
457        let output = "1 pass tests\nDone 12ms";
458        let compressed = compress_output("bunx test", output).unwrap();
459        assert!(compressed.contains("bun test: 1 passed"));
460    }
461
462    #[test]
463    fn routes_deno_task() {
464        let output = "Task dev deno run --allow-net server.ts\nListening on http://localhost:8000";
465        assert!(try_specific_pattern("deno task dev", output).is_some());
466    }
467
468    #[test]
469    fn routes_jest_commands() {
470        let output = "PASS  tests/main.test.js\nTest Suites: 1 passed, 1 total\nTests:       5 passed, 5 total\nTime:        2.5 s";
471        assert!(try_specific_pattern("jest", output).is_some());
472        assert!(try_specific_pattern("npx jest --coverage", output).is_some());
473    }
474
475    #[test]
476    fn routes_mocha_commands() {
477        let output = "  3 passing (50ms)\n  1 failing\n\n  1) Array #indexOf():\n     Error: expected -1 to equal 0";
478        assert!(try_specific_pattern("mocha", output).is_some());
479        assert!(try_specific_pattern("npx mocha tests/", output).is_some());
480    }
481
482    #[test]
483    fn routes_tofu_commands() {
484        let output = "Initializing the backend...\nInitializing provider plugins...\nTerraform has been successfully initialized!";
485        assert!(try_specific_pattern("tofu init", output).is_some());
486    }
487
488    #[test]
489    fn routes_ps_commands() {
490        let mut lines = vec!["USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND".to_string()];
491        for i in 0..20 {
492            lines.push(format!("user {i} 0.0 0.1 1234 123 ? S 10:00 0:00 proc_{i}"));
493        }
494        let output = lines.join("\n");
495        assert!(try_specific_pattern("ps aux", &output).is_some());
496    }
497
498    #[test]
499    fn routes_ping_commands() {
500        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";
501        assert!(try_specific_pattern("ping -c 3 google.com", output).is_some());
502    }
503
504    #[test]
505    fn routes_jq_to_json_schema() {
506        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}]}";
507        assert!(try_specific_pattern("jq '.items' data.json", output).is_some());
508    }
509
510    #[test]
511    fn routes_linting_tools() {
512        let lint_output = "src/main.py:10: error: Missing return\nsrc/main.py:20: error: Unused var\nFound 2 errors";
513        assert!(try_specific_pattern("hadolint Dockerfile", lint_output).is_some());
514        assert!(try_specific_pattern("oxlint src/", lint_output).is_some());
515        assert!(try_specific_pattern("pyright src/", lint_output).is_some());
516        assert!(try_specific_pattern("basedpyright src/", lint_output).is_some());
517    }
518
519    #[test]
520    fn routes_fd_commands() {
521        let output = "src/main.rs\nsrc/lib.rs\nsrc/util/helpers.rs\nsrc/util/math.rs\ntests/integration.rs\n";
522        assert!(try_specific_pattern("fd --extension rs", output).is_some());
523        assert!(try_specific_pattern("fdfind .rs", output).is_some());
524    }
525
526    #[test]
527    fn routes_just_commands() {
528        let output = "Available recipes:\n    build\n    test\n    lint\n";
529        assert!(try_specific_pattern("just --list", output).is_some());
530        assert!(try_specific_pattern("just build", output).is_some());
531    }
532
533    #[test]
534    fn routes_ninja_commands() {
535        let output = "[1/10] Compiling foo.c\n[10/10] Linking app\n";
536        assert!(try_specific_pattern("ninja", output).is_some());
537        assert!(try_specific_pattern("ninja -j4", output).is_some());
538    }
539
540    #[test]
541    fn routes_clang_commands() {
542        let output =
543            "src/main.c:10:5: error: use of undeclared identifier 'foo'\n1 error generated.\n";
544        assert!(try_specific_pattern("clang src/main.c", output).is_some());
545        assert!(try_specific_pattern("clang++ -std=c++17 main.cpp", output).is_some());
546    }
547
548    #[test]
549    fn routes_cargo_run() {
550        let output = "   Compiling foo v0.1.0\n    Finished `dev` profile\nHello, world!";
551        assert!(try_specific_pattern("cargo run", output).is_some());
552    }
553
554    #[test]
555    fn routes_cargo_bench() {
556        let output = "   Compiling foo v0.1.0\ntest bench_parse ... bench: 1234 ns/iter";
557        assert!(try_specific_pattern("cargo bench", output).is_some());
558    }
559
560    #[test]
561    fn routes_build_tools() {
562        let build_output = "   Compiling foo v0.1.0\n    Finished release [optimized]";
563        assert!(try_specific_pattern("gcc -o main main.c", build_output).is_some());
564        assert!(try_specific_pattern("g++ -o main main.cpp", build_output).is_some());
565    }
566
567    #[test]
568    fn routes_monorepo_tools() {
569        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";
570        assert!(try_specific_pattern("turbo install", output).is_some());
571        assert!(try_specific_pattern("nx install", output).is_some());
572    }
573}