weavatrix-rust 1.0.1

Read-only Rust repository intelligence and MCP server
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
//! Runtime-correctness review over local evidence.
//!
//! Every check here reads only repository bytes: no network, no package
//! manager, no execution. Each projection reports what it actually covered so
//! a caller can never mistake "nothing configured" for "nothing wrong".

use crate::RepositoryState;
use blazingly_json::{Value, json};
use std::collections::BTreeSet;
use std::fs;
use std::path::Path;
use weavatrix_graph::NodeKind;

/// One bounded runtime-correctness pattern.
struct Rule {
    id: &'static str,
    severity: &'static str,
    languages: &'static [&'static str],
    message: &'static str,
    /// Line-level trigger; the second argument is the enclosing block text.
    matches: fn(&str) -> bool,
}

const RULES: &[Rule] = &[
    Rule {
        id: "runtime.await_in_loop",
        severity: "medium",
        languages: &["javascript", "typescript", "python"],
        message: "await inside a loop serializes iterations; consider batching",
        matches: |line| {
            let trimmed = line.trim_start();
            (trimmed.starts_with("for ") || trimmed.starts_with("while "))
                && line.contains("await ")
        },
    },
    Rule {
        id: "runtime.floating_promise",
        severity: "high",
        languages: &["javascript", "typescript"],
        message: "promise-returning call is neither awaited nor chained; rejections are unobserved",
        matches: |line| {
            let trimmed = line.trim();
            trimmed.ends_with(");")
                && (trimmed.starts_with("fetch(")
                    || trimmed.contains(".then(")
                    || trimmed.starts_with("Promise.all("))
                && !trimmed.contains("await ")
                && !trimmed.contains(".catch(")
                && !trimmed.starts_with("return ")
        },
    },
    Rule {
        id: "runtime.empty_catch",
        severity: "high",
        languages: &[],
        message: "error is swallowed by an empty catch/except block",
        matches: |line| {
            let trimmed = line.trim().replace(' ', "");
            trimmed == "catch{}"
                || trimmed.ends_with("catch{}")
                || trimmed == "except:pass"
                || trimmed.ends_with("=>{}),")
        },
    },
    Rule {
        id: "runtime.blocking_call_in_async",
        severity: "high",
        languages: &["javascript", "typescript", "python", "rust"],
        message: "blocking or sleeping call on an async path stalls the executor",
        matches: |line| {
            line.contains("readFileSync")
                || line.contains("execSync")
                || line.contains("time.sleep(")
                || line.contains("std::thread::sleep")
        },
    },
    Rule {
        id: "runtime.unchecked_unwrap",
        severity: "medium",
        languages: &["rust"],
        message: "unwrap/expect on fallible values panics in production paths",
        matches: |line| {
            (line.contains(".unwrap()") || line.contains(".expect(")) && !line.contains("//")
        },
    },
    Rule {
        id: "runtime.shared_mutable_global",
        severity: "medium",
        languages: &["javascript", "typescript", "python", "go"],
        message: "mutable module-level state is shared across concurrent requests",
        matches: |line| {
            let trimmed = line.trim_start();
            line.starts_with(|c: char| !c.is_whitespace())
                && (trimmed.starts_with("let cache")
                    || trimmed.starts_with("var cache")
                    || trimmed.starts_with("let current")
                    || trimmed.starts_with("global "))
        },
    },
];

/// A finding identity that survives line shifts: rule, file, and a
/// fingerprint of the offending code rather than its position.
fn finding_id(rule: &str, path: &str, line: &str) -> String {
    let normalized = line.split_whitespace().collect::<Vec<_>>().join(" ");
    let mut hash = 0xcbf2_9ce4_8422_2325_u64;
    for byte in normalized.as_bytes() {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
    }
    format!("{rule}:{path}:{hash:016x}")
}

/// One reviewable source: repository path, language identifier, and body.
pub(super) type Source = (String, String, String);

/// Findings, files scanned, and whether the cap truncated the review.
pub(super) type RuntimeReview = (Vec<Value>, usize, bool);

/// Runs the runtime rule set over arbitrary source text, so the same review
/// applies to the worktree and to an immutable Git baseline.
pub(super) fn runtime_findings(
    sources: impl IntoIterator<Item = Source>,
    max: usize,
) -> RuntimeReview {
    let mut findings = Vec::new();
    let mut scanned = 0_usize;
    let mut truncated = false;
    for (path, language, text) in sources {
        scanned += 1;
        let ignored_lines = if language == "rust" {
            rust_cfg_test_lines(&text)
        } else {
            BTreeSet::new()
        };
        let code = runtime_code(&path, &text);
        let async_lines = async_context_lines(&path, &language, &code);
        for (offset, (line, code_line)) in text.lines().zip(code.lines()).enumerate() {
            if ignored_lines.contains(&(offset + 1)) {
                continue;
            }
            if line.len() > 400 {
                continue;
            }
            for rule in RULES {
                if !rule.languages.is_empty() && !rule.languages.contains(&language.as_str()) {
                    continue;
                }
                if rule.id == "runtime.blocking_call_in_async"
                    && !async_lines.contains(&(offset + 1))
                {
                    continue;
                }
                if (rule.matches)(code_line) {
                    if findings.len() >= max {
                        truncated = true;
                        break;
                    }
                    findings.push(json!({
                        "id": finding_id(rule.id, &path, line),
                        "rule": rule.id,
                        "category": "runtime",
                        "severity": rule.severity,
                        "file": path,
                        "line": offset + 1,
                        "language": language,
                        "message": rule.message,
                        "evidence": line.trim(),
                    }));
                }
            }
        }
    }
    findings.sort_by(|left, right| left["id"].as_str().cmp(&right["id"].as_str()));
    (findings, scanned, truncated)
}

fn runtime_code(path: &str, source: &str) -> String {
    let Some(extension) = Path::new(path).extension().and_then(|value| value.to_str()) else {
        return source.to_owned();
    };
    let Some(language) = weavatrix_parse::Language::from_extension(extension) else {
        return source.to_owned();
    };
    let mut code = source.as_bytes().to_vec();
    for token in weavatrix_parse::tokenize(source, language) {
        if matches!(
            token.kind,
            weavatrix_parse::TokenKind::String
                | weavatrix_parse::TokenKind::Regex
                | weavatrix_parse::TokenKind::LineComment
                | weavatrix_parse::TokenKind::BlockComment
                | weavatrix_parse::TokenKind::Unterminated
        ) {
            code[token.start..token.end].fill(b' ');
        }
    }
    String::from_utf8(code).unwrap_or_else(|_| source.to_owned())
}

fn async_context_lines(path: &str, language: &str, source: &str) -> BTreeSet<usize> {
    if language == "python" {
        return python_async_lines(source);
    }
    let Some(extension) = Path::new(path).extension().and_then(|value| value.to_str()) else {
        return BTreeSet::new();
    };
    let Some(language) = weavatrix_parse::Language::from_extension(extension) else {
        return BTreeSet::new();
    };
    if !matches!(
        language,
        weavatrix_parse::Language::JavaScript
            | weavatrix_parse::Language::TypeScript
            | weavatrix_parse::Language::Rust
    ) {
        return BTreeSet::new();
    }
    brace_async_lines(source, language)
}

fn brace_async_lines(source: &str, language: weavatrix_parse::Language) -> BTreeSet<usize> {
    let tokens = weavatrix_parse::tokenize_lite(source, language);
    let mut lines = BTreeSet::new();
    let mut index = 0_usize;
    while index < tokens.len() {
        if tokens[index].text(source) != "async" {
            index += 1;
            continue;
        }
        lines.insert(tokens[index].line as usize);
        let limit = (index + 128).min(tokens.len());
        let Some(open) = (index + 1..limit)
            .take_while(|candidate| tokens[*candidate].text(source) != ";")
            .find(|candidate| tokens[*candidate].text(source) == "{")
        else {
            index += 1;
            continue;
        };
        let mut depth = 0_usize;
        let mut close = None;
        for (candidate, token) in tokens.iter().enumerate().skip(open) {
            match token.text(source) {
                "{" => depth += 1,
                "}" => {
                    depth = depth.saturating_sub(1);
                    if depth == 0 {
                        close = Some(candidate);
                        break;
                    }
                }
                _ => {}
            }
        }
        let Some(close) = close else {
            index += 1;
            continue;
        };
        lines.extend(tokens[index].line as usize..=tokens[close].line as usize);
        index = close + 1;
    }
    lines
}

fn python_async_lines(source: &str) -> BTreeSet<usize> {
    let mut lines = BTreeSet::new();
    let mut scopes = Vec::<usize>::new();
    for (offset, line) in source.lines().enumerate() {
        let trimmed = line.trim_start();
        if trimmed.is_empty() {
            continue;
        }
        let indent = line.len().saturating_sub(trimmed.len());
        while scopes.last().is_some_and(|scope| indent <= *scope) {
            scopes.pop();
        }
        if !scopes.is_empty() {
            lines.insert(offset + 1);
        }
        if trimmed.starts_with("async def ") {
            lines.insert(offset + 1);
            scopes.push(indent);
        }
    }
    lines
}

/// Lines compiled only under `cfg(test)`. Token positions make brace matching
/// insensitive to braces written inside strings or comments.
pub(super) fn rust_cfg_test_lines(source: &str) -> BTreeSet<usize> {
    let tokens = weavatrix_parse::tokenize_lite(source, weavatrix_parse::Language::Rust);
    let mut ignored = BTreeSet::new();
    let mut index = 0_usize;
    while index + 2 < tokens.len() {
        if tokens[index].text(source) != "#"
            || tokens[index + 1].text(source) != "["
            || tokens[index + 2].text(source) != "cfg"
        {
            index += 1;
            continue;
        }
        let Some(attribute_end) =
            (index + 3..tokens.len()).find(|candidate| tokens[*candidate].text(source) == "]")
        else {
            break;
        };
        if !(index + 3..attribute_end).any(|candidate| tokens[candidate].text(source) == "test") {
            index = attribute_end + 1;
            continue;
        }
        let Some(open) = (attribute_end + 1..tokens.len())
            .find(|candidate| tokens[*candidate].text(source) == "{")
        else {
            break;
        };
        let mut depth = 0_usize;
        let mut close = None;
        for (candidate, token) in tokens.iter().enumerate().skip(open) {
            match token.text(source) {
                "{" => depth += 1,
                "}" => {
                    depth = depth.saturating_sub(1);
                    if depth == 0 {
                        close = Some(candidate);
                        break;
                    }
                }
                _ => {}
            }
        }
        let Some(close) = close else {
            break;
        };
        ignored.extend(tokens[index].line as usize..=tokens[close].line as usize);
        index = close + 1;
    }
    ignored
}

/// Production source text of the analyzed worktree.
pub(super) fn product_sources(state: &RepositoryState) -> Vec<Source> {
    state
        .graph()
        .nodes()
        .iter()
        .filter(|node| node.kind == NodeKind::File)
        .filter_map(|node| {
            let language = node.language.clone()?;
            if super::health::is_non_product(&node.label) {
                return None;
            }
            let text = fs::read_to_string(state.root().join(&node.label)).ok()?;
            Some((node.label.clone(), language, text))
        })
        .collect()
}

/// Bounded runtime-correctness and concurrency review over production source.
pub(super) fn runtime(state: &RepositoryState, max: usize) -> Value {
    let (findings, scanned, truncated) = runtime_findings(product_sources(state), max);
    json!({
        "status": if findings.is_empty() {"PASS"} else {"REVIEW"},
        "execution": {"status": "COMPLETE"},
        "static_analysis": {
            "present": true,
            "scope": "bounded line-level rules over production source"
        },
        "runtime_evidence": {
            "present": false,
            "reason": "run_audit does not execute the repository or ingest profiler and telemetry data"
        },
        "rules": RULES.iter().map(|rule| rule.id).collect::<Vec<_>>(),
        "files_scanned": scanned,
        "findings_total": findings.len(),
        "truncated": truncated,
        "findings": findings,
        "caveat": "bounded syntax-context patterns over production source; no data-flow or execution",
    })
}

#[cfg(test)]
mod tests {
    use super::{finding_id, runtime_findings};

    #[test]
    fn finding_identity_survives_line_shifts_and_reindentation() {
        let original = "    for (const item of items) { await save(item); }";
        let shifted = "        for (const item of items) {  await save(item); }";
        assert_eq!(
            finding_id("runtime.await_in_loop", "src/a.js", original),
            finding_id("runtime.await_in_loop", "src/a.js", shifted),
            "indentation and inner spacing must not change a finding identity"
        );
        assert_ne!(
            finding_id("runtime.await_in_loop", "src/a.js", original),
            finding_id("runtime.await_in_loop", "src/b.js", original),
            "the same code in another file is another finding"
        );
    }

    #[test]
    fn runtime_rules_report_stable_findings_for_moved_code() {
        let body = "const items = [];\nfor (const item of items) { await save(item); }\n";
        let moved = format!("// header\n// header\n{body}");
        let (first, scanned, truncated) = runtime_findings(
            [(
                "src/a.js".to_owned(),
                "javascript".to_owned(),
                body.to_owned(),
            )],
            10,
        );
        let (second, _, _) = runtime_findings(
            [("src/a.js".to_owned(), "javascript".to_owned(), moved)],
            10,
        );
        assert_eq!(scanned, 1);
        assert!(!truncated);
        assert_eq!(first.len(), 1, "await inside a loop is reported once");
        assert_eq!(
            first[0]["id"], second[0]["id"],
            "moving code down the file keeps its identity so debt stays comparable"
        );
        assert_ne!(
            first[0]["line"], second[0]["line"],
            "the reported line still follows the code"
        );
    }

    #[test]
    fn runtime_rules_ignore_rust_code_compiled_only_for_tests() {
        let source = r#"
pub fn production() {
    risky().unwrap();
}

#[cfg(test)]
mod tests {
    const JSON: &str = "{ braces in strings do not close the module }";

    #[test]
    fn smoke() {
        risky().expect("tests may assert");
    }
}
"#;
        let (findings, scanned, truncated) = runtime_findings(
            [(
                "src/lib.rs".to_owned(),
                "rust".to_owned(),
                source.to_owned(),
            )],
            10,
        );

        assert_eq!(scanned, 1);
        assert!(!truncated);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0]["line"], 3);
    }

    #[test]
    fn blocking_calls_are_reported_only_inside_async_contexts() {
        let rust = r"
fn background_worker() {
    std::thread::sleep(std::time::Duration::from_millis(10));
}

async fn request_handler() {
    std::thread::sleep(std::time::Duration::from_millis(10));
}
";
        let python = r"
def background_worker():
    time.sleep(1)

async def request_handler():
    time.sleep(1)
";
        let javascript = r#"
function backgroundWorker() {
    readFileSync("state.json");
}

async function requestHandler() {
    readFileSync("state.json");
}
"#;
        let (findings, scanned, truncated) = runtime_findings(
            [
                ("src/lib.rs".to_owned(), "rust".to_owned(), rust.to_owned()),
                (
                    "worker.py".to_owned(),
                    "python".to_owned(),
                    python.to_owned(),
                ),
                (
                    "worker.js".to_owned(),
                    "javascript".to_owned(),
                    javascript.to_owned(),
                ),
            ],
            10,
        );
        assert_eq!(scanned, 3);
        assert!(!truncated);
        assert_eq!(findings.len(), 3);
        assert!(
            findings.iter().all(|finding| {
                let expected = if finding["file"] == "worker.py" { 6 } else { 7 };
                finding["line"] == expected
            }),
            "{findings:#?}"
        );
    }
}