mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-04 10:48:19 +00:00
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:
@@ -203,19 +203,39 @@ def _user_cache_dir() -> Path:
|
||||
return Path.home() / ".cache"
|
||||
|
||||
|
||||
def _runs_dir_override() -> str:
|
||||
"""The `OPENULTRASAST_RUNS_DIR` set in the environment; empty when runs go to the default location."""
|
||||
return os.environ.get("OPENULTRASAST_RUNS_DIR", "")
|
||||
|
||||
|
||||
def runs_dir_is_overridden() -> bool:
|
||||
"""Whether `OPENULTRASAST_RUNS_DIR` names where runs go. Wherever it points, that location is never pruned."""
|
||||
return bool(_runs_dir_override())
|
||||
|
||||
|
||||
def _runs_dir() -> str:
|
||||
"""Where a scan creates its run: the user's cache, unless `OPENULTRASAST_RUNS_DIR` says otherwise.
|
||||
|
||||
Always absolute, and taken from the working directory when the override is relative, never from the
|
||||
target: a run inside the analysed tree is what this default exists to prevent.
|
||||
"""
|
||||
override = os.environ.get("OPENULTRASAST_RUNS_DIR")
|
||||
override = _runs_dir_override()
|
||||
if not override:
|
||||
return str(_user_cache_dir() / "openultrasast" / "runs")
|
||||
path = Path(override)
|
||||
return str(path if path.is_absolute() else Path.cwd() / path)
|
||||
|
||||
|
||||
def _runs_retention_days() -> int:
|
||||
"""How long a run stays in the default cache before a later scan removes it; 0 keeps every run."""
|
||||
value = os.environ.get("OPENULTRASAST_RUNS_RETENTION_DAYS", "").strip()
|
||||
if not value:
|
||||
return 30
|
||||
if not value.isdecimal():
|
||||
raise ValueError(f"OPENULTRASAST_RUNS_RETENTION_DAYS must be a whole number of days, 0 or more, not {value!r}")
|
||||
return int(value)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ResolvedConfig:
|
||||
models: ModelConfig = ModelConfig()
|
||||
@@ -232,6 +252,7 @@ class ResolvedConfig:
|
||||
# Read from the environment each time a config is built, so one made without `load_config` lands in the
|
||||
# same place.
|
||||
runs_dir: str = field(default_factory=_runs_dir)
|
||||
runs_retention_days: int = field(default_factory=_runs_retention_days)
|
||||
|
||||
|
||||
def load_config(config_path: Path | None = None, *, dotenv: bool = True) -> ResolvedConfig:
|
||||
|
||||
Reference in New Issue
Block a user