Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,10 @@ SkillSpector is built to be driven by other tools (CI pipelines, install gates,
| Code | Meaning |
|------|---------|
| `0` | Scan completed, `risk_score` ≤ 50 (recommendation `SAFE` or `CAUTION`), and no enabled strict gate fired |
| `1` | Scan completed and either `risk_score` > 50, `--fail-on-findings` found an active finding, or `--fail-on-incomplete` found partial/incomplete analysis |
| `1` | Scan completed and either `risk_score` > 50, `--fail-on-findings` found an active finding, `--fail-on-incomplete` found partial/incomplete analysis, or `--min-coverage` found coverage below its threshold |
| `2` | Error (bad input, unreadable source, internal failure) |

> By default, the exit code collapses `SAFE` and `CAUTION` into `0`. Use `--fail-on-findings` to gate on any active finding, `--fail-on-incomplete` to gate on incomplete coverage, or read the JSON `recommendation` field for custom policy.
> By default, the exit code collapses `SAFE` and `CAUTION` into `0`. Use `--fail-on-findings` to gate on any active finding, `--fail-on-incomplete` to gate on incomplete coverage, `--min-coverage PERCENT` to gate on a coverage floor, or read the JSON `recommendation` field for custom policy.

### Machine-readable output

Expand Down
5 changes: 5 additions & 0 deletions docs/ANALYSIS_RESOURCE_BOUNDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ When relevant analysis is incomplete:
- `skillspector scan --fail-on-incomplete` exits with status 1. Without this option, the CLI retains
its compatibility behavior and still applies its ordinary risk-score exit policy. Execution
failures exit with status 2.
- `skillspector scan --min-coverage PERCENT` exits with status 1 when canonical coverage is below
`PERCENT`; equality passes. Recursive scans check each scanned child's own coverage, not the
aggregate `analysis_completeness.coverage_percent`, and any omitted or unscanned skill fails a
positive threshold. Skills that discovery never found because it stopped early are not counted;
add `--fail-on-incomplete` to catch that case.
- MCP responses set `safe_to_install` to `false` when analysis is incomplete, any relevant file is
entirely uninspected, execution failed, or the risk score exceeds the installation threshold.

Expand Down
60 changes: 58 additions & 2 deletions src/skillspector/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from __future__ import annotations

import json
import math
import os
import sys
import warnings
Expand Down Expand Up @@ -368,6 +369,31 @@ def _write_result(
print(report_body)


def _validate_min_coverage(value: float | None) -> float | None:
"""Accept only finite percentage thresholds in the CLI's supported range."""
if value is not None and (not math.isfinite(value) or not 0 <= value <= 100):
raise typer.BadParameter("must be a finite number between 0 and 100")
return value


def _coverage_below_threshold(result: dict[str, object], threshold: float | None) -> bool:
"""Compare finalized canonical coverage with the requested threshold."""
if threshold is None:
return False
completeness = result.get("analysis_completeness")
coverage = completeness.get("coverage_percent") if isinstance(completeness, dict) else None
if isinstance(coverage, bool) or not isinstance(coverage, (int, float)):
return True
if not math.isfinite(coverage):
return True
return coverage < threshold


def _omissions_fail_threshold(omitted_skills: int, threshold: float | None) -> bool:
"""Known omitted skills leave coverage below any positive threshold."""
return bool(omitted_skills) and threshold is not None and threshold > 0


def _recursive_json_payload(result: dict[str, object]) -> dict[str, object] | None:
"""Return parsed report_body when it is valid JSON object text."""
raw_report_body = result.get("report_body")
Expand Down Expand Up @@ -538,6 +564,14 @@ def scan(
help="Exit 1 when relevant analysis is partial or incomplete.",
),
] = False,
min_coverage: Annotated[
float | None,
typer.Option(
"--min-coverage",
help="Exit 1 when canonical analysis coverage is below this percentage (0-100).",
callback=_validate_min_coverage,
),
] = None,
fail_on_findings: Annotated[
bool,
typer.Option(
Expand Down Expand Up @@ -617,10 +651,17 @@ def scan(
raise typer.Exit(code=2)

if mcp_registry:
if recursive or baseline is not None or show_suppressed or yara_rules_dir is not None:
if (
recursive
or baseline is not None
or show_suppressed
or yara_rules_dir is not None
or min_coverage is not None
):
err_console.print(
"[red]Error:[/red] --mcp-registry cannot be combined with "
"--recursive, --baseline, --show-suppressed, or --yara-rules-dir"
"--recursive, --baseline, --show-suppressed, --yara-rules-dir, "
"or --min-coverage"
)
raise typer.Exit(code=2)
if format != FormatChoice.json:
Expand Down Expand Up @@ -669,8 +710,10 @@ def scan(
discovery_console = (
err_console if output is None and format is not FormatChoice.terminal else console
)
recursive_omitted_skills = 0
if recursive and resolved_path.is_dir():
detection = detect_skills(resolved_path)
recursive_omitted_skills = detection.omitted_symlink_entries
if not detection.complete:
pre_scan_ledger_events = _multi_skill_limitation_events(detection)
err_console.print(
Expand Down Expand Up @@ -703,6 +746,7 @@ def scan(
verbose=verbose,
fail_on_incomplete=fail_on_incomplete,
fail_on_findings=fail_on_findings,
min_coverage=min_coverage,
)
except typer.Exit:
raise
Expand Down Expand Up @@ -794,6 +838,10 @@ def scan(
raise typer.Exit(code=1)
if fail_on_findings and effective_findings(result):
raise typer.Exit(code=1)
if _coverage_below_threshold(result, min_coverage) or _omissions_fail_threshold(
recursive_omitted_skills, min_coverage
):
raise typer.Exit(code=1)
if (result.get("risk_score") or 0) > RISK_THRESHOLD:
raise typer.Exit(code=1)
except typer.Exit:
Expand Down Expand Up @@ -2735,6 +2783,7 @@ def _scan_multi_skill(
yara_dir: str | None = None,
verbose: bool = False,
fail_on_incomplete: bool = False,
min_coverage: float | None = None,
fail_on_findings: bool = False,
**legacy_kwargs: object,
) -> None:
Expand Down Expand Up @@ -2777,6 +2826,7 @@ def _scan_multi_skill(
complete_skill_count = 0
partial_skill_count = 0
failed_skill_count = 0
coverage_failed = False

for i, skill in enumerate(skills, 1):
if i > _MULTI_SKILL_MAX_SKILLS:
Expand Down Expand Up @@ -2823,6 +2873,8 @@ def _scan_multi_skill(
source_local_only=skill.local_only,
)
child_failed = result.get("execution_successful") is False
if not child_failed and _coverage_below_threshold(result, min_coverage):
coverage_failed = True
completeness_value = result.get("analysis_completeness")
child_partial = (
not child_failed
Expand Down Expand Up @@ -2928,6 +2980,8 @@ def _scan_multi_skill(
aggregate_limitations.append(
f"{unscanned_skill_count} recursive skill(s) unscanned after an aggregate limit"
)
if _omissions_fail_threshold(skills_omitted_total, min_coverage):
coverage_failed = True
aggregate_limitations = list(dict.fromkeys(aggregate_limitations))[:256]
aggregate_completeness = _multi_skill_analysis_completeness(
total_skills=len(skills) + omitted_symlink_entry_count,
Expand Down Expand Up @@ -3164,6 +3218,8 @@ def _scan_multi_skill(
raise typer.Exit(code=1)
if fail_on_findings and has_findings:
raise typer.Exit(code=1)
if coverage_failed:
raise typer.Exit(code=1)
if max_score > RISK_THRESHOLD:
raise typer.Exit(code=1)

Expand Down
Loading
Loading