|
| 1 | +"""Offline finding-identity contracts through the installed SDK's real graph.""" |
| 2 | + |
| 3 | +import importlib |
| 4 | +import json |
| 5 | +from dataclasses import replace |
| 6 | + |
| 7 | +import pytest |
| 8 | +from skillspector.inspection_ledger import ( |
| 9 | + LedgerOutcome, |
| 10 | + LedgerReason, |
| 11 | + LedgerRecordType, |
| 12 | + analyzer_status_for_events, |
| 13 | + ledger_event, |
| 14 | +) |
| 15 | +from skillspector.models import OCCURRENCE_FINDING_ID_KEY, AnalyzerFinding, Location, Severity |
| 16 | +from skillspector.nodes.analyzers.static_runner import analyzer_finding_to_finding |
| 17 | +from skillspector.suppression import Baseline, SuppressionRule |
| 18 | + |
| 19 | + |
| 20 | +def findings(): |
| 21 | + original = AnalyzerFinding( |
| 22 | + rule_id="PI1", message="Generated inert match", severity=Severity.HIGH, |
| 23 | + location=Location(file="generated.md", start_line=1), confidence=0.9, |
| 24 | + matched_text="INERT_MARKER", context="INERT_MARKER", |
| 25 | + ) |
| 26 | + first = replace(analyzer_finding_to_finding(original), finding_id="generated-a") |
| 27 | + second = replace(analyzer_finding_to_finding(original), finding_id="generated-b") |
| 28 | + return first, second |
| 29 | + |
| 30 | + |
| 31 | +def run_graph(monkeypatch, produced, *, extra_events=(), **state): |
| 32 | + workflow = importlib.import_module("skillspector.graph") |
| 33 | + event = ledger_event( |
| 34 | + outcome=LedgerOutcome.COMPLETED, phase="behavioral", analyzer_id="behavioral_ast", |
| 35 | + path="generated.md", emitted_finding_ids=[finding.finding_id for finding in produced], |
| 36 | + ) |
| 37 | + |
| 38 | + def producer(current): |
| 39 | + return { |
| 40 | + "findings": produced, "inspection_ledger": [event, *extra_events], |
| 41 | + "analyzer_status_events": [analyzer_status_for_events("behavioral_ast", [event])], |
| 42 | + } |
| 43 | + |
| 44 | + monkeypatch.setattr(workflow, "resolve_input", lambda current: {}) |
| 45 | + monkeypatch.setattr(workflow, "build_context", lambda current: {}) |
| 46 | + monkeypatch.setattr(workflow, "ANALYZER_NODE_IDS", ["behavioral_ast"]) |
| 47 | + monkeypatch.setattr(workflow, "ANALYZER_MODULES", {}) |
| 48 | + monkeypatch.setattr(workflow, "ANALYZER_NODES", {"behavioral_ast": producer}) |
| 49 | + return workflow.create_graph().invoke({ |
| 50 | + "components": ["generated.md"], |
| 51 | + "component_metadata": [{"path": "generated.md", "type": "markdown"}], |
| 52 | + "file_cache": {"generated.md": "INERT_MARKER INERT_MARKER\nINERT_MARKER\n"}, |
| 53 | + "manifest": {"name": "generated", "description": "Generated inert documentation"}, |
| 54 | + "skill_path": "generated-fixture", "use_llm": False, |
| 55 | + "output_format": "json", "findings": [], **state, |
| 56 | + }) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize("difference", [ |
| 60 | + "none", "line", "columns", "evidence", "classification", "snippet", "source", |
| 61 | +]) |
| 62 | +def test_distinct_canonical_ids_survive_report_compaction(monkeypatch, difference): |
| 63 | + first, second = findings() |
| 64 | + if difference == "line": |
| 65 | + second = replace(second, start_line=2) |
| 66 | + elif difference == "columns": |
| 67 | + first = replace(first, start_column=0, end_column=12) |
| 68 | + second = replace(second, start_column=13, end_column=25) |
| 69 | + elif difference == "evidence": |
| 70 | + second = replace(second, evidence={"generated": "different"}) |
| 71 | + elif difference == "classification": |
| 72 | + second = replace(second, severity="CRITICAL") |
| 73 | + elif difference == "snippet": |
| 74 | + second = replace(second, code_snippet="INERT_MARKER second generated context") |
| 75 | + elif difference == "source": |
| 76 | + second = replace(second, source_identity="generated/source", source_digest="sha256:inert") |
| 77 | + result = run_graph(monkeypatch, [first, second]) |
| 78 | + report = json.loads(result["report_body"]) |
| 79 | + expected = {finding.finding_id: finding for finding in [first, second]} |
| 80 | + assert result["analysis_completeness"]["findings_before_filtering"] == 2 |
| 81 | + assert result["analysis_completeness"]["findings_after_filtering"] == 2 |
| 82 | + assert result["analysis_completeness"]["is_complete"] is True |
| 83 | + assert set(result["effective_finding_ids"]) == set(expected) |
| 84 | + assert {finding.finding_id for finding in result["active_findings"]} == set(expected) |
| 85 | + assert len(report["issues"]) == 2 |
| 86 | + for issue in report["issues"]: |
| 87 | + original = expected.pop(issue["finding_id"]) |
| 88 | + assert issue["severity"] == original.severity |
| 89 | + assert issue["code_snippet"] == original.code_snippet |
| 90 | + assert issue["evidence"] == original.evidence |
| 91 | + assert issue["location"] == original.to_dict()["location"] |
| 92 | + assert not expected |
| 93 | + assert report["suppressed"] == [] |
| 94 | + assert {item["properties"]["findingId"] |
| 95 | + for item in result["sarif_report"]["runs"][0]["results"]} == { |
| 96 | + first.finding_id, second.finding_id} |
| 97 | + |
| 98 | + |
| 99 | +def test_repeated_id_is_an_update_not_an_extra_finding(monkeypatch): |
| 100 | + first, second = findings() |
| 101 | + result = run_graph(monkeypatch, [first, replace(second, finding_id=first.finding_id)]) |
| 102 | + report = json.loads(result["report_body"]) |
| 103 | + assert report["analysis_completeness"]["findings_before_filtering"] == 1 |
| 104 | + assert report["analysis_completeness"]["findings_after_filtering"] == 1 |
| 105 | + assert [issue["finding_id"] for issue in report["issues"]] == [first.finding_id] |
| 106 | + |
| 107 | + |
| 108 | +def test_suppression_is_explicit_and_retains_both_ids(monkeypatch): |
| 109 | + result = run_graph(monkeypatch, list(findings()), baseline=Baseline( |
| 110 | + rules=[SuppressionRule(rule_id="PI1", reason="Generated explicit suppression")])) |
| 111 | + report = json.loads(result["report_body"]) |
| 112 | + assert report["issues"] == [] |
| 113 | + assert report["suppressed_count"] == 2 |
| 114 | + assert {issue["finding_id"] for issue in report["suppressed"]} == {"generated-a", "generated-b"} |
| 115 | + assert report["analysis_completeness"]["findings_after_filtering"] == 2 |
| 116 | + |
| 117 | + |
| 118 | +def test_multiple_locations_are_occurrences_not_extra_canonical_findings(monkeypatch): |
| 119 | + first, _ = findings() |
| 120 | + first = replace(first, occurrences=[ |
| 121 | + {"file": "generated.md", "start_line": 1, "end_line": None}, |
| 122 | + {"file": "generated.md", "start_line": 2, "end_line": None}, |
| 123 | + ]) |
| 124 | + result = run_graph(monkeypatch, [first]) |
| 125 | + report = json.loads(result["report_body"]) |
| 126 | + assert report["analysis_completeness"]["findings_after_filtering"] == 1 |
| 127 | + assert [issue["finding_id"] for issue in report["issues"]] == [first.finding_id] * 2 |
| 128 | + |
| 129 | + |
| 130 | +def test_repeated_same_id_location_does_not_invent_an_occurrence(monkeypatch): |
| 131 | + first, _ = findings() |
| 132 | + occurrence = {"file": "generated.md", "start_line": 1, "end_line": None} |
| 133 | + first = replace(first, occurrences=[occurrence, dict(occurrence)]) |
| 134 | + result = run_graph(monkeypatch, [first]) |
| 135 | + report = json.loads(result["report_body"]) |
| 136 | + assert report["analysis_completeness"]["findings_after_filtering"] == 1 |
| 137 | + assert [issue["finding_id"] for issue in report["issues"]] == [first.finding_id] |
| 138 | + |
| 139 | + |
| 140 | +@pytest.mark.parametrize("invalid_identity", [None, "", [], {}]) |
| 141 | +def test_invalid_private_occurrence_identity_uses_canonical_id(monkeypatch, invalid_identity): |
| 142 | + produced = [replace(finding, occurrences=[{ |
| 143 | + "file": finding.file, "start_line": finding.start_line, |
| 144 | + OCCURRENCE_FINDING_ID_KEY: invalid_identity, |
| 145 | + }]) for finding in findings()] |
| 146 | + result = run_graph(monkeypatch, produced) |
| 147 | + report = json.loads(result["report_body"]) |
| 148 | + assert {issue["finding_id"] for issue in report["issues"]} == {"generated-a", "generated-b"} |
| 149 | + assert report["analysis_completeness"]["findings_after_filtering"] == 2 |
| 150 | + |
| 151 | + |
| 152 | +def test_actual_output_limit_is_still_incomplete(monkeypatch): |
| 153 | + finalizer = importlib.import_module("skillspector.nodes.finalize_inspection_ledger") |
| 154 | + reporter = importlib.import_module("skillspector.nodes.report") |
| 155 | + monkeypatch.setattr(finalizer, "MAX_FINDING_OUTPUT_RECORDS", 1) |
| 156 | + monkeypatch.setattr(reporter, "MAX_FINDING_OUTPUT_RECORDS", 1) |
| 157 | + result = run_graph(monkeypatch, list(findings())) |
| 158 | + report = json.loads(result["report_body"]) |
| 159 | + assert len(result["findings"]) == 2 |
| 160 | + assert len(report["issues"]) == 1 |
| 161 | + assert report["analysis_completeness"]["is_complete"] is False |
| 162 | + assert any(row["reason_code"] == "output_limit" |
| 163 | + for row in report["analysis_completeness"]["ledger_exceptions"]) |
| 164 | + |
| 165 | + |
| 166 | +@pytest.mark.parametrize("reason", [LedgerReason.REFERENCE_MISSING, LedgerReason.STATIC_PARSE_LIMIT]) |
| 167 | +def test_reference_notifications_and_real_limits_are_retained(monkeypatch, reason): |
| 168 | + event = ledger_event( |
| 169 | + outcome=LedgerOutcome.PARTIAL, record_type=LedgerRecordType.SYSTEM, |
| 170 | + phase="reference_resolution" if reason == LedgerReason.REFERENCE_MISSING else "static", |
| 171 | + path="generated.md", start_line=1, end_line=1, reason=reason, |
| 172 | + ) |
| 173 | + result = run_graph(monkeypatch, list(findings()), extra_events=[event]) |
| 174 | + report = json.loads(result["report_body"]) |
| 175 | + assert len(report["issues"]) == 2 |
| 176 | + assert report["analysis_completeness"]["is_complete"] is False |
| 177 | + assert any(row["reason_code"] == reason |
| 178 | + for row in report["analysis_completeness"]["ledger_exceptions"]) |
| 179 | + |
| 180 | + |
| 181 | +def test_unknown_semantic_telemetry_stays_incomplete_without_provider_calls(monkeypatch): |
| 182 | + result = run_graph(monkeypatch, list(findings()), llm_requested=True, |
| 183 | + llm_preflight_error="Generated unavailable runtime") |
| 184 | + report = json.loads(result["report_body"]) |
| 185 | + assert len(report["issues"]) == 2 |
| 186 | + assert report["analysis_completeness"]["is_complete"] is False |
| 187 | + assert report["metadata"]["llm_requested"] is True |
| 188 | + assert report["metadata"]["llm_available"] is False |
0 commit comments