diff --git a/.agents/skills/openultrasast-scan/SKILL.md b/.agents/skills/openultrasast-scan/SKILL.md index 37ede3c9..12fce781 100644 --- a/.agents/skills/openultrasast-scan/SKILL.md +++ b/.agents/skills/openultrasast-scan/SKILL.md @@ -20,7 +20,9 @@ it wrote. (`$XDG_CACHE_HOME/openultrasast/runs/` when that variable is set to an absolute path; `OPENULTRASAST_RUNS_DIR` overrides it). After a scan, with no `OPENULTRASAST_RUNS_DIR` set, the runs in that default location older than 30 days are removed (`OPENULTRASAST_RUNS_RETENTION_DAYS` sets the number of days and `0` keeps every run), and - nothing is removed from a location chosen with `OPENULTRASAST_RUNS_DIR`. + nothing is removed from a location chosen with `OPENULTRASAST_RUNS_DIR`, except through the shell wrapper: it + mounts `~/.cache/openultrasast/runs` at `/reports` in the image, and a scan through it removes the expired runs + there as well. 5. Report the finding count, how many the verifier accepted, and **every** recorded degradation (`manifest.json`, key `degradations`). No findings plus a degradation is not a clean scan: the scan answered a smaller question than it was asked. diff --git a/README.md b/README.md index 096e5921..6f489bb1 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,9 @@ one is resolved against the current working directory, never against the directo After a scan writes its run to the default location, with no `OPENULTRASAST_RUNS_DIR` set, the runs there older than 30 days are removed, and `ousast` prints one `notice:` line when it removes any; `OPENULTRASAST_RUNS_RETENTION_DAYS` sets the number of days (`0` keeps every run), and nothing is -ever removed from a location chosen with `OPENULTRASAST_RUNS_DIR`. A scan does not read the files +ever removed from a location chosen with `OPENULTRASAST_RUNS_DIR`, except through the shell wrapper: +it mounts `~/.cache/openultrasast/runs` at `/reports` in the image, and a scan through it removes the +expired runs there as well. A scan does not read the files under its runs directory when that directory lies inside the scanned path; when this leaves source files out of the scan, `ousast` prints one `notice:` line naming the directory and how many. diff --git a/docs/examples.md b/docs/examples.md index 350daa54..dd9fb3f9 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -8,7 +8,9 @@ the current working directory, never against the target. Once a scan has written default location, with no `OPENULTRASAST_RUNS_DIR` set, the runs there that are older than 30 days are removed, and `ousast` prints one `notice:` line when it removes any; `OPENULTRASAST_RUNS_RETENTION_DAYS` sets the number of days (`0` keeps every run), and nothing is -ever removed from a location chosen with `OPENULTRASAST_RUNS_DIR`. A scan does not read the files +ever removed from a location chosen with `OPENULTRASAST_RUNS_DIR`, except through the shell wrapper: +it mounts `~/.cache/openultrasast/runs` at `/reports` in the image, and a scan through it removes the +expired runs there as well. A scan does not read the files under its runs directory when that directory lies inside the scanned path; when this leaves source files out of the scan, `ousast` prints one `notice:` line naming the directory and how many. `ousast benchmark` writes its run to the same directory, as `//` beside diff --git a/src/openultrasast/config.py b/src/openultrasast/config.py index 43636418..4d819341 100644 --- a/src/openultrasast/config.py +++ b/src/openultrasast/config.py @@ -208,8 +208,24 @@ def _runs_dir_override() -> str: return os.environ.get("OPENULTRASAST_RUNS_DIR", "") +WRAPPER_RUNS_MOUNT = "/reports" + + +def runs_dir_is_the_wrappers() -> bool: + """Whether `OPENULTRASAST_RUNS_DIR` is the shell wrapper's: the runs directory, mounted in the image. + + The wrappers pass `/reports` (`ops/shell/ousast.sh`), where the compose file mounts the directory a native + scan writes to by default, so a scan prunes it as it prunes the default location. + """ + return Path(_runs_dir_override()) == Path(WRAPPER_RUNS_MOUNT) + + def runs_dir_is_overridden() -> bool: - """Whether `OPENULTRASAST_RUNS_DIR` names where runs go. Wherever it points, that location is never pruned.""" + """Whether `OPENULTRASAST_RUNS_DIR` names where runs go. + + Wherever it points, that location is not pruned; the wrapper's mount is the one exception + (`runs_dir_is_the_wrappers`). + """ return bool(_runs_dir_override()) diff --git a/src/openultrasast/run.py b/src/openultrasast/run.py index 2be79475..ea1bed3c 100644 --- a/src/openultrasast/run.py +++ b/src/openultrasast/run.py @@ -7,7 +7,7 @@ from datetime import UTC, datetime, timedelta from pathlib import Path from uuid import uuid4 -from .config import ResolvedConfig, runs_dir_is_overridden, write_resolved_config +from .config import ResolvedConfig, runs_dir_is_overridden, runs_dir_is_the_wrappers, write_resolved_config _RUN_ID = re.compile(r"(\d{8}T\d{6}Z)-[0-9a-f]{8}") @@ -70,11 +70,12 @@ def prune_old_runs(runs_root: Path, *, keep: Path | None = None, retention_days: def prune_scan_runs(config: ResolvedConfig, *, keep: Path) -> tuple[str, ...]: """Remove the expired runs once a scan has written its own; returns what the scan is to say about it. - Only the default location is pruned: while `OPENULTRASAST_RUNS_DIR` is set, wherever it points, every run - stays. Nothing is said when nothing was removed, and a removal that fails is said once and costs the scan - nothing. + Two locations are pruned: the default one, and the shell wrapper's runs directory, which is the user's runs + directory mounted in the image (`runs_dir_is_the_wrappers`). While `OPENULTRASAST_RUNS_DIR` points anywhere + else, wherever it points, every run stays. Nothing is said when nothing was removed, and a removal that + fails is said once and costs the scan nothing. """ - if runs_dir_is_overridden(): + if runs_dir_is_overridden() and not runs_dir_is_the_wrappers(): return () root = Path(config.runs_dir) try: diff --git a/tests/test_run_pruning.py b/tests/test_run_pruning.py index 6844955b..6f7fdf12 100644 --- a/tests/test_run_pruning.py +++ b/tests/test_run_pruning.py @@ -1,5 +1,6 @@ -"""What a scan removes from its cache (contributor-scan Requirement 5.3, decision of 2026-11-24): once its run -is written, the expired runs go from the default location, and nothing else does.""" +"""What a scan removes from its cache (contributor-scan Requirement 5.3, decisions of 2026-11-24 and 2026-11-27): +once its run is written, the expired runs go from the default location and from the shell wrapper's mount of it, +and nothing else does.""" import errno import os @@ -13,7 +14,7 @@ import pytest from test_scan_output_location import _scan, _target, _user_cache from openultrasast import run -from openultrasast.config import ResolvedConfig, load_config +from openultrasast.config import WRAPPER_RUNS_MOUNT, ResolvedConfig, load_config from openultrasast.run import prune_old_runs, prune_scan_runs NOW = datetime(2026, 11, 24, 12, tzinfo=UTC) @@ -268,6 +269,42 @@ def test_a_scan_with_the_runs_dir_override_pointing_at_the_default_location_remo assert _notices(out) == [] +def test_a_scan_through_the_wrapper_removes_the_expired_runs_from_the_mounted_runs_directory( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """What a scan in the image sees through the shell wrapper. The runs directory is a stand-in for `/reports`, + which only root can make on the host.""" + monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", "/reports") + monkeypatch.delenv(RETENTION, raising=False) + mounted = tmp_path / "reports" + _earlier_run(mounted, 40) + last_week = _earlier_run(mounted, 5) + current = _run(mounted, _run_id(datetime.now(UTC), "badc0de0")) + + notices = prune_scan_runs(ResolvedConfig(runs_dir=str(mounted)), keep=current) + + assert notices == (f"removed 1 run older than 30 days from {mounted}",) + assert sorted(mounted.iterdir()) == [last_week, current] + + +def test_a_scan_that_compose_runs_without_the_wrapper_removes_nothing(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + """The compose file's own `OPENULTRASAST_RUNS_DIR`, `/reports/runs`: a location chosen, where nothing is removed.""" + monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", "/reports/runs") + monkeypatch.delenv(RETENTION, raising=False) + runs = tmp_path / "reports" / "runs" + expired = _earlier_run(runs, 40) + current = _run(runs, _run_id(datetime.now(UTC), "badc0de0")) + + assert prune_scan_runs(ResolvedConfig(runs_dir=str(runs)), keep=current) == () + assert sorted(runs.iterdir()) == [expired, current] + + +def test_the_wrapper_passes_the_override_that_a_scan_prunes() -> None: + """The wrapper's word and the scan's mount point must not drift apart: the scan prunes `/reports` by that name.""" + wrapper = Path(__file__).resolve().parents[1] / "ops" / "shell" / "ousast.sh" + assert f"-e OPENULTRASAST_RUNS_DIR={WRAPPER_RUNS_MOUNT} ousast" in wrapper.read_text() + + def test_a_scan_with_an_empty_runs_dir_override_removes_the_expired_runs_from_the_default_location( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] ) -> None: