Skip to content

Commit ddabd19

Browse files
fix: preserve distinct scanner finding identities (#90)
1 parent fd5f18a commit ddabd19

4 files changed

Lines changed: 258 additions & 1 deletion

File tree

‎docker/clawscan-runtime/Dockerfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ RUN git init /tmp/skillspector \
4141
RUN python -m pip install --no-cache-dir --upgrade pip uv \
4242
&& python -m pip install --no-cache-dir /tmp/skillspector pytest \
4343
"cisco-ai-skill-scanner==$CISCO_AI_SKILL_SCANNER_VERSION" \
44-
&& python -m pytest -q /tmp/clawscan-adapter-tests/test_skillspector_responses.py /tmp/clawscan-adapter-tests/test_skillspector_applicability.py \
44+
&& python -m pytest -q /tmp/clawscan-adapter-tests/test_skillspector_responses.py /tmp/clawscan-adapter-tests/test_skillspector_applicability.py /tmp/clawscan-adapter-tests/test_skillspector_accounting.py \
4545
&& uv tool install /tmp/aig/skill-scan --with pytest \
4646
&& /root/.local/share/uv/tools/aig-skill-scan/bin/python -m pytest -q /tmp/clawscan-adapter-tests/test_aig_responses.py \
4747
&& uv tool install "snyk-agent-scan==$SNYK_AGENT_SCAN_VERSION" \

‎docker/clawscan-runtime/patches/skillspector-responses.patch‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,68 @@ diff --git a/tests/test_mcp_tool_poisoning.py b/tests/test_mcp_tool_poisoning.py
323323
assert partial[0]["reason_code"] is LedgerReason.OUTPUT_LIMIT
324324

325325
def test_markdown_deadline_stops_before_prefix_and_fence_extraction(
326+
diff --git a/src/skillspector/nodes/deduplicate.py b/src/skillspector/nodes/deduplicate.py
327+
--- a/src/skillspector/nodes/deduplicate.py
328+
+++ b/src/skillspector/nodes/deduplicate.py
329+
@@ -25,7 +25,9 @@ def _occurrences(finding: Finding) -> list[dict[str, object]]:
330+
if finding.occurrences:
331+
occurrences = [dict(item) for item in finding.occurrences]
332+
for occurrence in occurrences:
333+
- occurrence.setdefault(OCCURRENCE_FINDING_ID_KEY, finding.finding_id)
334+
+ report_finding_id = occurrence.get(OCCURRENCE_FINDING_ID_KEY)
335+
+ if not isinstance(report_finding_id, str) or not report_finding_id:
336+
+ occurrence[OCCURRENCE_FINDING_ID_KEY] = finding.finding_id
337+
occurrence.setdefault(
338+
OCCURRENCE_CODE_SNIPPET_KEY,
339+
finding.code_snippet or finding.context,
340+
@@ -210,6 +212,7 @@ def deduplicate(findings: list[Finding]) -> list[Finding]:
341+
str(occurrence.get("source_digest") or finding.source_digest or ""),
342+
str(occurrence.get("source_url") or finding.source_url or ""),
343+
_line(occurrence.get("transitive_depth"), finding.transitive_depth),
344+
+ occurrence.get(OCCURRENCE_FINDING_ID_KEY, finding.finding_id),
345+
)
346+
occurrences.setdefault(
347+
location,
348+
@@ -246,6 +249,7 @@ def deduplicate(findings: list[Finding]) -> list[Finding]:
349+
source_digest,
350+
source_url,
351+
transitive_depth,
352+
+ _finding_id,
353+
),
354+
(report_finding_id, report_code_snippet),
355+
) in sorted(
356+
@@ -260,6 +264,7 @@ def deduplicate(findings: list[Finding]) -> list[Finding]:
357+
_line(item[0][2], item[0][1]),
358+
_line(item[0][3], -1),
359+
_line(item[0][4], -1),
360+
+ str(item[0][9]),
361+
),
362+
)
363+
]
364+
diff --git a/tests/nodes/test_report_occurrence_columns.py b/tests/nodes/test_report_occurrence_columns.py
365+
--- a/tests/nodes/test_report_occurrence_columns.py
366+
+++ b/tests/nodes/test_report_occurrence_columns.py
367+
@@ -193,7 +193,7 @@ def test_cross_file_occurrences_keep_their_report_identity_and_snippet() -> None:
368+
compacted = deduplicate([*findings, replace(findings[0], finding_id="finding-first-duplicate")])
369+
370+
assert len(compacted) == 1
371+
- assert len(compacted[0].occurrences) == 2
372+
+ assert len(compacted[0].occurrences) == 3
373+
assert deduplicate(compacted)[0].to_dict() == compacted[0].to_dict()
374+
assert all(
375+
set(occurrence)
376+
@@ -216,6 +216,7 @@ def test_cross_file_occurrences_keep_their_report_identity_and_snippet() -> None:
377+
(issue["location"]["file"], issue["finding_id"], issue["code_snippet"]) for issue in issues
378+
] == [
379+
("first.py", "finding-first", "first_context\nsubprocess.run(command)"),
380+
+ ("first.py", "finding-first-duplicate", "first_context\nsubprocess.run(command)"),
381+
("second.py", "finding-second", "second_context\nsubprocess.run(command)"),
382+
]
383+
assert "_skillspector_report_" not in result["report_body"]
384+
@@ -229,5 +230,6 @@ def test_cross_file_occurrences_keep_their_report_identity_and_snippet() -> None:
385+
for item in result["sarif_report"]["runs"][0]["results"]
386+
] == [
387+
("first.py", "finding-first", "first_context\nsubprocess.run(command)"),
388+
+ ("first.py", "finding-first-duplicate", "first_context\nsubprocess.run(command)"),
389+
("second.py", "finding-second", "second_context\nsubprocess.run(command)"),
390+
]
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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

‎docs/scanner-runtime.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ fixes, not a new ClawScan model-provider framework:
8888
make subsequent fully examined prose incomplete. Additional applicable code
8989
still produces an `output_limit`; file, byte, artifact and runtime limits
9090
remain enforced. This is not evidence that production inspection gaps closed.
91+
- SkillSpector's report compaction preserves distinct canonical finding IDs even
92+
when their rule, match and location coincide. Ledger counts are not lowered to
93+
match a compacted projection; explicit suppression and output limits remain
94+
separate. This does not attribute or resolve any production count discrepancy.
9195
- A.I.G reuses the official OpenAI SDK's Responses streaming API for that exact
9296
model. A.I.G tools are XML in text, so complete text history is replayed, not
9397
converted to synthetic OpenAI tools. Reasoning effort and usage are mapped;

0 commit comments

Comments
 (0)