feat(contributor-scan): remove scan runs older than 30 days from the default cache (3.1, run retention)

By maintainer decision. Since 6edae3a a native scan leaves a run directory
under ~/.cache/openultrasast/runs/ and nothing removed them. After a scan's
manifest is written, with no OPENULTRASAST_RUNS_DIR set, the runs in that
default location that are older than 30 days are now removed. The age comes
from the UTC timestamp in the run id, never from file times, so a copied or
touched directory is not misjudged, and a run exactly 30 days old is kept.

Never touched: the scan's own run; anything not named exactly like a run
(only direct children of the runs root whose name is a run id); symlinks,
which are neither followed nor removed; and any location chosen with
OPENULTRASAST_RUNS_DIR, even when it resolves to the default.

OPENULTRASAST_RUNS_RETENTION_DAYS sets the number of days and 0 keeps every
run. A failed prune never fails the scan: it is one notice on the command
line, and the MCP scan and benchmark results now carry a notices key. A
retention longer than the calendar and a directory tree too deep to remove
are both turned into that same behaviour.

Known limits, on record: a removal that fails part-way leaves the run half
deleted and it is retried on every later scan; the prune stops at the first
failure; a clock set far ahead removes earlier runs; a filesystem mounted at
or under an expired run is emptied before the removal fails.

Tests: tests/test_run_pruning.py drives the function and the real command
line (34 tests); tests/test_mcp.py pins the notices. The reviewer attacked
the deletion path with about sixty hostile inputs and twenty-three mutations;
all are killed or kept safe.
This commit is contained in:
Marius Ciepluch
2026-11-24 17:51:37 +01:00
parent efa2b62bff
commit 17d46c0f93
9 changed files with 478 additions and 7 deletions
+41
View File
@@ -1,9 +1,13 @@
"""Narrow MCP server: tool listing, scan→status flow, and surface safety (Phase 12)."""
import errno
import io
import json
from pathlib import Path
import pytest
from test_run_pruning import _default_cache, _earlier_run, _refuse_removal
from openultrasast.mcp import McpServer, serve
EXPECTED_TOOLS = {
@@ -101,6 +105,7 @@ def test_scan_then_status_and_findings(tmp_path: Path, monkeypatch) -> None: #
scan = _call(server, "openultrasast.scan", {"path": str(_repo(tmp_path)), "mode": "quick"})
assert not scan["isError"] and scan["scan_id"]
assert scan["notices"] == [] # a run root of its own is never pruned, so the scan has nothing to say
run_dir = scan["run_dir"]
status = _call(server, "openultrasast.status", {"run_dir": run_dir})
@@ -137,6 +142,42 @@ def test_export_report_returns_markdown(tmp_path: Path, monkeypatch) -> None: #
assert report["format"] == "markdown" and report["content"]
def test_scan_result_carries_the_notice_of_the_runs_the_scan_removed(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
runs = _default_cache(tmp_path, monkeypatch)
expired = _earlier_run(runs, 40)
result = _call(McpServer(), "openultrasast.scan", {"path": str(_repo(tmp_path)), "mode": "quick"})
assert not result["isError"]
assert result["notices"] == [f"removed 1 run older than 30 days from {runs}"]
assert not expired.exists()
def test_scan_result_carries_the_notice_of_a_removal_that_was_refused(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
runs = _default_cache(tmp_path, monkeypatch)
expired = _earlier_run(runs, 40)
refusal = PermissionError(errno.EACCES, "Permission denied", str(expired))
_refuse_removal(monkeypatch, expired, refusal)
result = _call(McpServer(), "openultrasast.scan", {"path": str(_repo(tmp_path)), "mode": "quick"})
assert result["exit_code"] == 0
assert result["notices"] == [f"old runs could not be removed from {runs}: {refusal}"]
def test_benchmark_result_carries_the_notice_of_the_runs_the_scan_removed(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
runs = _default_cache(tmp_path, monkeypatch)
_repo(tmp_path)
_earlier_run(runs, 40)
manifest = tmp_path / "benchmark.toml"
manifest.write_text('name = "py-smoke"\nlanguage = "python"\n\n[source]\npath = "repo"\n')
result = _call(McpServer(), "openultrasast.benchmark", {"manifest": str(manifest), "mode": "quick"})
assert not result["isError"]
assert result["notices"] == [f"removed 1 run older than 30 days from {runs}"]
# ---- stdio loop -------------------------------------------------------------