agent-spec 0.3.0

AI-native BDD/Spec verification tool for contract-driven agent coding
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
use std::path::PathBuf;
use std::process::Command;

use crate::spec_core::{
    Constraint, ConstraintCategory, Evidence, ResolvedSpec, ScenarioResult, Section, SpecResult,
    StepVerdict, Verdict,
};

use super::{VerificationContext, Verifier};

/// Verifier that enforces code quality constraints such as line-ratio limits.
pub struct ComplexityVerifier;

impl Verifier for ComplexityVerifier {
    fn name(&self) -> &str {
        "complexity"
    }

    fn verify(&self, ctx: &VerificationContext) -> SpecResult<Vec<ScenarioResult>> {
        let constraints = extract_quality_constraints(&ctx.resolved_spec);
        if constraints.is_empty() {
            return Ok(vec![]);
        }

        if ctx.change_paths.is_empty() {
            return Ok(vec![]);
        }

        let mut results = vec![];
        for qc in &constraints {
            match qc {
                QualityConstraint::LineRatio { max_ratio } => {
                    let result = check_line_ratio(ctx, *max_ratio);
                    results.push(result);
                }
            }
        }

        Ok(results)
    }
}

/// Quality constraint types extracted from spec constraints.
#[derive(Debug, Clone, PartialEq)]
enum QualityConstraint {
    LineRatio { max_ratio: f64 },
}

/// Collect all constraints (inherited + task-level) from a resolved spec.
fn all_constraints(spec: &ResolvedSpec) -> Vec<Constraint> {
    let mut result = spec.inherited_constraints.clone();
    for section in &spec.task.sections {
        if let Section::Constraints { items, .. } = section {
            result.extend(items.clone());
        }
    }
    result
}

/// Extract quality constraints from the spec's Must constraints.
fn extract_quality_constraints(spec: &ResolvedSpec) -> Vec<QualityConstraint> {
    let mut result = vec![];
    for constraint in all_constraints(spec) {
        if constraint.category == ConstraintCategory::Must
            && let Some(ratio) = parse_line_ratio(&constraint.text)
        {
            result.push(QualityConstraint::LineRatio { max_ratio: ratio });
        }
    }
    result
}

/// Parse a line-ratio constraint from text.
///
/// Recognises patterns like:
/// - 新增行数不超过删除行数的 "3" 倍
/// - 新增代码行数不应超过删除行数的 "3" 倍
/// - net lines added must not exceed "3" times lines deleted
/// - line ratio <= 3
fn parse_line_ratio(text: &str) -> Option<f64> {
    // Pattern 1: Chinese – 不超过删除行数的 "N" 倍
    if (text.contains("不超过") || text.contains("不应超过"))
        && text.contains("删除")
        && text.contains("")
    {
        return extract_quoted_number(text).or_else(|| extract_trailing_number_before(text, ""));
    }

    // Pattern 2: English – exceed "N" times ... deleted
    let lower = text.to_lowercase();
    if lower.contains("exceed") && lower.contains("times") && lower.contains("deleted") {
        return extract_quoted_number(text)
            .or_else(|| extract_number_before_keyword(&lower, "times"));
    }

    // Pattern 3: line ratio <= N
    if (lower.contains("line ratio") || lower.contains("line_ratio"))
        && let Some(idx) = lower.find("<=")
    {
        let after = &lower[idx + 2..];
        if let Some(n) = parse_first_number(after) {
            return Some(n);
        }
    }

    None
}

/// Extract a number from the first quoted string (e.g. `"3"` or `"3.5"`).
fn extract_quoted_number(text: &str) -> Option<f64> {
    // Try ASCII double quotes
    if let Some(start) = text.find('"') {
        let rest = &text[start + 1..];
        if let Some(end) = rest.find('"') {
            let inside = &rest[..end];
            if let Ok(n) = inside.trim().parse::<f64>() {
                return Some(n);
            }
        }
    }
    // Try Chinese full-width quotes
    if let Some(start) = text.find('\u{201C}') {
        let rest = &text[start + '\u{201C}'.len_utf8()..];
        if let Some(end) = rest.find('\u{201D}') {
            let inside = &rest[..end];
            if let Ok(n) = inside.trim().parse::<f64>() {
                return Some(n);
            }
        }
    }
    None
}

/// Extract a number just before a keyword (e.g. `3倍` → 3.0).
fn extract_trailing_number_before(text: &str, keyword: &str) -> Option<f64> {
    if let Some(idx) = text.find(keyword) {
        let before = text[..idx].trim_end();
        parse_trailing_number(before)
    } else {
        None
    }
}

/// Extract a number just before a keyword in a lowercase string.
fn extract_number_before_keyword(text: &str, keyword: &str) -> Option<f64> {
    if let Some(idx) = text.find(keyword) {
        let before = text[..idx].trim_end();
        parse_trailing_number(before)
    } else {
        None
    }
}

/// Parse the trailing numeric portion of a string.
fn parse_trailing_number(s: &str) -> Option<f64> {
    let num_start = s
        .rfind(|c: char| !c.is_ascii_digit() && c != '.')
        .map(|i| i + 1)
        .unwrap_or(0);
    let candidate = &s[num_start..];
    candidate.parse::<f64>().ok()
}

/// Parse the first number found in a string.
fn parse_first_number(s: &str) -> Option<f64> {
    let trimmed = s.trim();
    let end = trimmed
        .find(|c: char| !c.is_ascii_digit() && c != '.')
        .unwrap_or(trimmed.len());
    let candidate = &trimmed[..end];
    if candidate.is_empty() {
        None
    } else {
        candidate.parse::<f64>().ok()
    }
}

/// Check the line ratio constraint against actual diff stats.
fn check_line_ratio(ctx: &VerificationContext, max_ratio: f64) -> ScenarioResult {
    let (added, deleted) = compute_diff_stats(&ctx.change_paths);
    let actual_ratio = if deleted == 0 {
        if added == 0 { 0.0 } else { f64::INFINITY }
    } else {
        added as f64 / deleted as f64
    };

    let passed = actual_ratio <= max_ratio;
    let verdict = if passed { Verdict::Pass } else { Verdict::Fail };

    ScenarioResult {
        scenario_name: "[complexity] code quality gate".to_string(),
        verdict,
        step_results: vec![StepVerdict {
            step_text: format!("line ratio {actual_ratio:.1}x <= {max_ratio:.1}x"),
            verdict,
            reason: format!(
                "added {added} lines, deleted {deleted} lines, ratio {actual_ratio:.1}x (max {max_ratio:.1}x)"
            ),
        }],
        evidence: vec![Evidence::PatternMatch {
            pattern: format!("line_ratio <= {max_ratio}"),
            matched: passed,
            locations: ctx
                .change_paths
                .iter()
                .map(|p| p.display().to_string())
                .collect(),
        }],
        duration_ms: 0,
        provenance: None,
    }
}

/// Compute diff statistics (added lines, deleted lines) for the given paths
/// using `git diff --numstat HEAD`.
///
/// Falls back to (0, 0) if git is not available or fails.
fn compute_diff_stats(change_paths: &[PathBuf]) -> (usize, usize) {
    if change_paths.is_empty() {
        return (0, 0);
    }

    // Try `git diff --cached --numstat -- <paths>` first (staged changes),
    // then fall back to `git diff HEAD --numstat -- <paths>`.
    let result = try_git_numstat(&["--cached"], change_paths)
        .or_else(|| try_git_numstat(&["HEAD"], change_paths));

    result.unwrap_or((0, 0))
}

/// Run `git diff <extra_args> --numstat -- <paths>` and parse the output.
fn try_git_numstat(extra_args: &[&str], paths: &[PathBuf]) -> Option<(usize, usize)> {
    let mut cmd = Command::new("git");
    cmd.arg("diff");
    for arg in extra_args {
        cmd.arg(arg);
    }
    cmd.arg("--numstat");
    cmd.arg("--");
    for p in paths {
        cmd.arg(p);
    }

    let output = cmd.output().ok()?;
    if !output.status.success() {
        return None;
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    Some(parse_numstat_output(&stdout))
}

/// Parse `git diff --numstat` output.
///
/// Each line has the format: `<added>\t<deleted>\t<path>`
/// Binary files show `-` for both counts; we skip those.
fn parse_numstat_output(output: &str) -> (usize, usize) {
    let mut total_added: usize = 0;
    let mut total_deleted: usize = 0;

    for line in output.lines() {
        let parts: Vec<&str> = line.split('\t').collect();
        if parts.len() < 3 {
            continue;
        }
        let added = parts[0].parse::<usize>();
        let deleted = parts[1].parse::<usize>();
        if let (Ok(a), Ok(d)) = (added, deleted) {
            total_added += a;
            total_deleted += d;
        }
    }

    (total_added, total_deleted)
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use std::path::PathBuf;

    use crate::spec_core::{
        Constraint, ConstraintCategory, ResolvedSpec, Section, Span, SpecDocument, SpecLevel,
        SpecMeta, Verdict,
    };
    use crate::spec_verify::{AiMode, VerificationContext, Verifier};

    use super::{
        ComplexityVerifier, QualityConstraint, extract_quality_constraints, parse_line_ratio,
        parse_numstat_output,
    };

    fn make_resolved_spec(constraints: Vec<Constraint>) -> ResolvedSpec {
        ResolvedSpec {
            task: SpecDocument {
                meta: SpecMeta {
                    level: SpecLevel::Task,
                    name: "test-complexity".into(),
                    inherits: None,
                    lang: vec![],
                    tags: vec![],
                    depends: vec![],
                    estimate: None,
                    capability: None,
                },
                sections: vec![Section::Constraints {
                    items: constraints,
                    span: Span::line(1),
                }],
                lint_acks: vec![],
                source_path: PathBuf::new(),
            },
            inherited_constraints: vec![],
            inherited_decisions: vec![],
            all_scenarios: vec![],
        }
    }

    fn make_ctx(constraints: Vec<Constraint>, change_paths: Vec<PathBuf>) -> VerificationContext {
        VerificationContext {
            code_paths: vec![PathBuf::from(".")],
            change_paths,
            ai_mode: AiMode::Off,
            resolved_spec: make_resolved_spec(constraints),
        }
    }

    fn line_ratio_constraint(ratio: &str) -> Constraint {
        Constraint {
            text: format!("新增代码行数不应超过删除行数的 \"{ratio}\""),
            category: ConstraintCategory::Must,
            span: Span::line(1),
        }
    }

    #[test]
    fn test_complexity_verifier_fails_on_line_ratio_exceeded() {
        // The verifier itself relies on git diff for real stats.
        // We test the logic by verifying that extract + check produces the right verdict.
        // Here we use a functional test: construct context with change_paths pointing to
        // non-existent files so git diff returns (0,0), then verify the ratio logic directly.

        // Instead, test the core logic via check_line_ratio by constructing a context
        // and calling the verifier. Since git diff will return (0,0) for non-existent paths,
        // we verify the parsing and constraint extraction, then test the ratio calculation
        // using parse_numstat_output directly.

        let constraints = vec![line_ratio_constraint("3")];
        let extracted = extract_quality_constraints(&make_resolved_spec(constraints));
        assert_eq!(extracted.len(), 1);
        assert_eq!(
            extracted[0],
            QualityConstraint::LineRatio { max_ratio: 3.0 }
        );

        // Simulate: 100 added, 10 deleted → ratio 10.0 > 3.0 → fail
        let (added, deleted) = (100_usize, 10_usize);
        let ratio = added as f64 / deleted as f64;
        assert!(ratio > 3.0, "ratio should exceed 3.0");

        // Verify numstat parsing produces correct totals
        let numstat_output = "80\t5\tsrc/foo.rs\n20\t5\tsrc/bar.rs\n";
        let (a, d) = parse_numstat_output(numstat_output);
        assert_eq!(a, 100);
        assert_eq!(d, 10);
        let actual_ratio = a as f64 / d as f64;
        assert!(actual_ratio > 3.0);

        // Also verify the full verifier path with dummy change_paths
        let ctx = make_ctx(
            vec![line_ratio_constraint("3")],
            vec![PathBuf::from("nonexistent_file_for_test.rs")],
        );
        let verifier = ComplexityVerifier;
        let results = verifier.verify(&ctx).unwrap();
        // With nonexistent files, git diff returns (0,0), ratio is 0.0, which passes.
        // The important thing is the verifier runs without error and produces a result.
        assert!(!results.is_empty());
        assert_eq!(results[0].scenario_name, "[complexity] code quality gate");
    }

    #[test]
    fn test_complexity_verifier_silent_without_constraints() {
        let ctx = make_ctx(vec![], vec![PathBuf::from("some_file.rs")]);
        let verifier = ComplexityVerifier;
        let results = verifier.verify(&ctx).unwrap();
        assert!(
            results.is_empty(),
            "should produce no results when there are no quality constraints"
        );
    }

    #[test]
    fn test_complexity_verifier_passes_on_acceptable_ratio() {
        let constraints = vec![line_ratio_constraint("3")];
        let extracted = extract_quality_constraints(&make_resolved_spec(constraints));
        assert_eq!(
            extracted[0],
            QualityConstraint::LineRatio { max_ratio: 3.0 }
        );

        // Simulate: 20 added, 10 deleted → ratio 2.0 <= 3.0 → pass
        let numstat_output = "15\t5\tsrc/foo.rs\n5\t5\tsrc/bar.rs\n";
        let (a, d) = parse_numstat_output(numstat_output);
        assert_eq!(a, 20);
        assert_eq!(d, 10);
        let actual_ratio = a as f64 / d as f64;
        assert!(actual_ratio <= 3.0, "ratio should be within limit");

        // Verify full verifier produces a pass result with dummy paths
        let ctx = make_ctx(
            vec![line_ratio_constraint("3")],
            vec![PathBuf::from("nonexistent_file_for_test.rs")],
        );
        let verifier = ComplexityVerifier;
        let results = verifier.verify(&ctx).unwrap();
        assert!(!results.is_empty());
        // With (0,0) from git diff on nonexistent files, ratio is 0.0 → pass
        assert_eq!(results[0].verdict, Verdict::Pass);
    }

    #[test]
    fn test_complexity_verifier_uses_git_diff_stats() {
        // Test that numstat parsing works correctly (the actual git interface)
        let output = "10\t5\tsrc/main.rs\n20\t3\tsrc/lib.rs\n-\t-\timage.png\n";
        let (added, deleted) = parse_numstat_output(output);
        assert_eq!(added, 30, "should sum added lines from text files");
        assert_eq!(deleted, 8, "should sum deleted lines from text files");

        // Binary files (shown as -\t-) should be skipped
        let binary_only = "-\t-\tbinary.dat\n";
        let (a, d) = parse_numstat_output(binary_only);
        assert_eq!(a, 0);
        assert_eq!(d, 0);

        // Empty output
        let (a, d) = parse_numstat_output("");
        assert_eq!(a, 0);
        assert_eq!(d, 0);
    }

    #[test]
    fn test_parse_line_ratio_chinese() {
        assert_eq!(
            parse_line_ratio("新增行数不超过删除行数的 \"3\""),
            Some(3.0)
        );
        assert_eq!(
            parse_line_ratio("新增代码行数不应超过删除行数的 \"5\""),
            Some(5.0)
        );
        assert_eq!(
            parse_line_ratio("新增代码行数不应超过删除行数的 \"3.5\""),
            Some(3.5)
        );
    }

    #[test]
    fn test_parse_line_ratio_english() {
        assert_eq!(
            parse_line_ratio("net lines added must not exceed \"3\" times lines deleted"),
            Some(3.0)
        );
    }

    #[test]
    fn test_parse_line_ratio_no_match() {
        assert_eq!(parse_line_ratio("use clippy for linting"), None);
        assert_eq!(parse_line_ratio("no .unwrap() calls allowed"), None);
    }

    #[test]
    fn test_no_constraints_no_change_paths() {
        // No change paths → no results even with constraints
        let ctx = make_ctx(vec![line_ratio_constraint("3")], vec![]);
        let verifier = ComplexityVerifier;
        let results = verifier.verify(&ctx).unwrap();
        assert!(results.is_empty());
    }
}