From e4622b01aa8145be52637db9612934bf8e0e129b Mon Sep 17 00:00:00 2001 From: Daniel Petty Date: Sun, 1 Nov 2026 12:10:17 +0000 Subject: [PATCH 1/2] docs(openspec): fix plan #2130 osv-scanner database digest The first plan named files that do not exist, added ToolRun fields that exist, and used a Ruff command that fails. The new plan names the database files as the ruleset of osv-scanner, digests them as a stream, and leaves Ruff as it is: its config digest already follows the config files. Plans #2130. --- .../design.md | 15 ++++-- .../proposal.md | 52 +++++++++++-------- .../specs/review-tool-runs/spec.md | 42 +++++++++------ .../tasks.md | 25 ++++----- 4 files changed, 79 insertions(+), 55 deletions(-) diff --git a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/design.md b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/design.md index 3446f968ab..ff91724d3d 100644 --- a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/design.md +++ b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/design.md @@ -2,11 +2,16 @@ ## Decisions -- **Use a streamed SHA-256 over the database files in sorted filename order for the osv-scanner digest.**: The 36 MB database exceeds the file-size cap of a plain file hash. A streamed hash avoids loading the file into memory. Sorting by filename makes the digest deterministic across runs on the same database. -- **Use the SHA-256 of `ruff --show-settings` JSON output for the Ruff digest.**: The issue asks for the resolved configuration, not the raw file. `--show-settings` outputs the final merged configuration as JSON, which is the exact input Ruff uses for the scan. -- **Store both digests as optional string fields on the existing ToolRun record.**: The ToolRun record already lives in the SARIF run property bag (#1983). Adding a field is the minimal change. Optional keeps the record valid for tools that do not produce a digest. +- **Name the database files as the ruleset files of osv-scanner.**: The request already has this seam (`ruleset_files`), the record already turns it into `ruleset_digest`, and Trivy uses it for its database metadata. The database of osv-scanner is the file `all.zip` in one directory for each ecosystem (`osv-scalibr/PyPI/all.zip`), so the hook returns `sorted(read.glob("*/all.zip"))` of the `ToolDataDir` that `data_dirs()` gives. The digest covers the content and not the paths, so it is the same for the same database in another cache directory. +- **Digest a ruleset file below a read-only bind as a stream, with no size limit.**: The npm database is 210 MB, over the 50 MiB limit, and the sandbox executor refuses a file over the limit. A file below a bind is data that devops-cli or the user put in the cache of the tool. It is not a file of the reviewed tree. A stream needs no memory, so the limit has no reason there. +- **Keep the limit for every other file.**: A config file or a ruleset file of the reviewed tree is not trusted. `ARCHITECTURE.md` asks for a size check before a read, and the contract test pins the refusal. Only the ruleset files below a bind of the request skip the limit. A config file never does. +- **Make `file_digest` read as a stream.**: It is the one function that digests a file for the record and for the benchmarks. `hashlib.file_digest` gives the same SHA-256, so every digest stays the same. +- **No change to the Ruff scanner.**: The Ruff digest already follows the config files that Ruff finds. The one gap, a base file named by `extend`, is written into `docs/SELF_IMPROVEMENT.md`. ## Rejected alternatives -- **Use the database's modified time combined with a digest of its index.**: The modified time is not content-addressed: a file can be rewritten with the same mtime, or the mtime can change without a content change. A streamed content hash is more robust and matches the acceptance criterion that the digest changes when the database changes. -- **Hash the raw `ruff.toml` or `pyproject.toml` file for the Ruff digest.**: The issue and the proposed design both state the digest covers the resolved configuration, not the raw file. Ruff merges defaults with the project config, so the raw file does not capture the full input. +- **The modified time of the database with a digest of its index.**: The scratch download of osv-scanner 2.6.0 wrote one file, `osv-scalibr/PyPI/all.zip`, and no index or metadata file. The modified time changes when the same content is fetched again, so the digest would change with no change of the database. A digest of the zip directory entries would need a new reader of the zip format in devops-cli. A stream of the file is simpler and exact. +- **Lift the limit for all files of a record.**: A config file or a ruleset file of a reviewed tree comes from the code under review. A limit on it is a threshold that only the owner may lower, and the contract test pins it. +- **A new field on `ToolRun` (`database_digest`, `config_digest: str | None`).**: `ToolRun` already has `config_digest: str` and `ruleset_digest: str`, and the Tool Runs table and the SARIF export show them. A second field would duplicate them. +- **The digest of `ruff check --show-settings`.**: The output is plain text, it holds absolute paths of the checkout, and `--isolated` ignores the config file (see Feasibility). Stripping the paths would be a new hand-written parser of a text format that Ruff does not promise to keep. +- **`get_or_compute_snapshot_digest` for the record.**: It reads `data.cache_dir/osv_db`, which the scan does not read, and it uses modified times. diff --git a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/proposal.md b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/proposal.md index f01a127578..cec8d37cc5 100644 --- a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/proposal.md +++ b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/proposal.md @@ -2,37 +2,45 @@ ## Why -Issue #2130: the ToolRun of an osv-scanner run records no digest of its advisory database, and the ToolRun of a Ruff run records no digest of the Ruff configuration it read. Without these digests, a user cannot verify which database version or configuration produced a given scan result. The 36 MB PyPI database exceeds the file-size cap of the record's digest step, so a plain file hash is not possible. +Issue #2130: the ToolRun of an osv-scanner run records no digest of its advisory database, and the ToolRun of a Ruff run is said to record no digest of the Ruff configuration. The issue asks for two results: -## What Changes - -- The ToolRun of an osv-scanner run records a `database_digest` field: a streamed SHA-256 over the advisory database files in sorted filename order. -- The ToolRun of a Ruff run records a `config_digest` field: the SHA-256 of the JSON output of `ruff --show-settings` for the target. -- The `review-tool-runs` capability gains two requirements that pin the digest behavior. -- The change fits one pull request. - -## Capabilities +- The ToolRun of an osv-scanner run changes when its database changes, and stays the same when it does not. +- The ToolRun of a Ruff run changes when the Ruff configuration of the target changes. -### New Capabilities +The check on `origin/main` shows that only the first result is missing. The second result exists (see Acceptance). -None. - -### Modified Capabilities +## What Changes -- `review-tool-runs`: osv-scanner toolrun records database digest, ruff toolrun records configuration digest +- `OsvScanner` names the `all.zip` file of each ecosystem in its offline database as ruleset files, as `TrivyScanner` names its metadata file. The `ruleset_digest` of the ToolRun then follows the content of the database. +- The record digests a ruleset file below a read-only bind of the request (the data directory of the tool) as a stream, whatever its size. A file that is not below a bind, and every config file, keep the 50 MiB limit. `file_digest` in `src/devops_cli/ai/run_store.py` reads a file as a stream, with the same result. +- `docs/SELF_IMPROVEMENT.md` says what the osv-scanner ruleset digest covers. +- Ruff gets no change: its `config_digest` already follows the config files. +- The change has a spec delta in `review-tool-runs`: the ToolRun record in `review.md` and in the SARIF export is observable. +- The change fits one pull request. -## Impact +## Acceptance -- Code: `src/devops_cli/review/scanners/osv_scanner.py`, `src/devops_cli/review/scanners/ruff.py` -- Tests: `tests/test_review_tool_runs.py` +- Criterion 1, the osv-scanner digest follows the database: group 1 lets the record digest a large database file, and group 2 names the database files in the request. +- Criterion 2, the Ruff digest follows the Ruff configuration: already met on `main`. `RuffScanner.config_names` lists `.ruff.toml`, `ruff.toml` and `pyproject.toml`, and the test `test_a_changed_config_file_of_the_tree_changes_the_config_digest` pins it. Group 3 runs that test again and writes the one gap into the document: a Ruff config that uses `extend` has a base file that the digest does not cover. No criterion asks for that file, so the plan adds no code for it. ## Feasibility -- The issue states the ToolRun records no digest today and cites #2096 for the 36 MB database and the file-size cap. The capability `review-tool-runs` exists in the spec list, confirming the ToolRun record is a defined concept. -- Python's `hashlib` supports streaming SHA-256 over file objects, so the 36 MB database can be hashed without loading it into memory. This satisfies the file-size cap constraint. -- Ruff's `--show-settings` flag outputs the resolved configuration as JSON to stdout. A test can invoke it on a fixture directory and hash the output offline. -- First checks the implementation runs against the real system: `uv run ruff --show-settings --isolated` to confirm the flag exists and outputs JSON; `uv run devops review scan --help` to confirm the scan command and its flags; `ls` of the osv-scanner database directory to confirm the file layout for the streamed hash. +All commands ran on 2026-11-01 on `origin/main`. + +- The osv-scanner record has no database digest today. `git grep -n ruleset_files -- src/devops_cli/security/osv_scanner.py` prints nothing, so `OsvScanner` keeps the default hook of `BaseSecurityScanner` (`ruleset_files` returns no file). `docs/SELF_IMPROVEMENT.md` says the same: "none: the database has no metadata file yet". +- The database is one file. `OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY= osv-scanner scan source --offline --offline-vulnerabilities --download-offline-databases --lockfile uv.lock` (osv-scanner 2.6.0, a `uv.lock` that has one PyPI package) wrote `find -type f` = `osv-scalibr/PyPI/all.zip`, 37604846 bytes. There is no index or metadata file next to it. +- The size claim of the issue is half right. The limit of the digest step is `DEFAULT_REPOMAP_MAX_FILE_SIZE_BYTES` = 52428800 (50 MiB, `src/devops_cli/config/defaults.py`). A HEAD request for `https://osv-vulnerabilities.storage.googleapis.com//all.zip` gave `Content-Length` 37604846 for PyPI (under the limit) and 219697992 for npm (over it). Go, Maven, crates.io, RubyGems, Packagist and NuGet are between 2.6 MB and 12.7 MB. +- Naming the database file as a ruleset file today stops the scan. A scratch script built a `ToolRequest` with a sparse `all.zip` of 52428801 bytes as `ruleset_files` and called `record_tool_run`. It raised `SandboxValidationError: The file .../npm/all.zip cannot be read as a regular file of at most 52428800 bytes`. The sandbox executor builds its record with `require_files=True`, so a review with an npm database would fail. This is why group 1 comes first. +- A stream needs no memory limit. `hashlib.file_digest` (standard library, Python 3.11 and later) digested the same 52428801-byte file in 0.07 s on Python 3.14.10 (`uv run python`). +- The Ruff digest exists. `git grep -n config_names -- src/devops_cli/security/ruff.py` shows `config_names = CONST_RUFF_CONFIG_FILE_NAMES` and `config_search_upward = True`, and `record_tool_run` digests `request.config_files`. `uv run devops ci test tests/security/test_review_tool_runs.py -k "changed_config_file_of_the_tree or names_every_config_file_it_may_read"` ran 3 tests, 3 passed. The `ruff` case edits `pyproject.toml` and checks that the digest changes, and that it stays the same for an unchanged file. +- The Ruff command of the first plan does not work. `ruff --show-settings a.py` (ruff 0.16.8) printed `error: unexpected argument '--show-settings' found`. `ruff check --show-settings a.py` prints plain text, not JSON, with absolute paths (`cache_dir`, `file_resolver.project_root`), and `--isolated` ignores `ruff.toml` (`linter.line_length = 88` where the file sets 100). So a digest of that output would depend on the checkout path. The plan does not use it. +- A Ruff `extend` is not covered. With `ruff.toml` = `extend = "base.toml"` and `base.toml` = `line-length = 77`, `ruff check --show-settings a.py` printed `linter.line_length = 77`. The digest covers `ruff.toml` but not `base.toml`. +- Nothing newer did this work. `git log origin/main --since=2026-10-24 -- src/devops_cli/security/osv_scanner.py src/devops_cli/security/tool_executor.py src/devops_cli/ai/run_store.py` shows no commit about a database digest. The active changes under `openspec/changes/` that name osv-scanner or `file_digest` are this one and 2116, which adds only a test fixture requirement. An open-issue search for "osv digest" finds only #2130. +- `get_or_compute_snapshot_digest` in `src/devops_cli/security/osv_scanner.py` is not the answer. It hashes the names and modified times of `data.cache_dir/osv_db`, a directory that the scan does not read (the scan reads `osv-scalibr` below `OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY`). It feeds the `AdvisoryAnchor` of findings. `openspec/specs/config-commands/spec.md` pins it, so the plan keeps it as it is. ## Existing Tool Considered -Python's `hashlib` (standard library) provides the streamed SHA-256. Ruff's `--show-settings` flag outputs the resolved configuration. No external library is needed. The change adds two small functions in devops-cli's own scanner adapter code and one field to the ToolRun record. The written cost is roughly 80 lines of code and tests, well within one step's budget. No internal code is deleted. +- `hashlib.file_digest` from the Python standard library streams a file through SHA-256. No new dependency and no hand-written chunk loop. +- The record already has the right place: `BaseSecurityScanner.ruleset_files`, `ToolDataDir`, and the `ruleset_digest` of `ToolRun`. `TrivyScanner.ruleset_files` is the model for the osv-scanner hook. +- The change must keep passing: `tests/ai/test_tool_executor_contract.py::test_a_file_that_cannot_be_digested_is_refused` (a file over the limit that is not below a bind stays refused), `tests/security/test_review_tool_runs.py::test_each_review_scan_names_the_config_files_and_the_ruleset_files_its_tool_reads`, and `tests/security/test_scanner_tool_executor.py::test_a_tool_that_reads_a_fetched_database_gets_it_read_only_by_its_variable`. +- Code removed: `_is_small_regular_file` in `src/devops_cli/security/tool_executor.py` becomes a check for a regular file with an optional limit. `file_digest` no longer reads a whole file into memory. diff --git a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/specs/review-tool-runs/spec.md b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/specs/review-tool-runs/spec.md index 0c0a89b4a6..d87913be3c 100644 --- a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/specs/review-tool-runs/spec.md +++ b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/specs/review-tool-runs/spec.md @@ -1,23 +1,33 @@ ## ADDED Requirements -### Requirement: osv-scanner toolrun records database digest -The ToolRun of an osv-scanner run SHALL record a `database_digest` field that is a SHA-256 hex digest computed by streaming the advisory database files in sorted filename order. The digest SHALL change when the database content changes and SHALL stay the same when it does not. +### Requirement: The ruleset digest of an osv-scanner run follows its offline database +The record of an osv-scanner run SHALL name the file `all.zip` of each ecosystem in the `osv-scalibr` directory of its offline database as ruleset files. So the `ruleset_digest` of the record SHALL change when the content of a database file changes, and SHALL stay the same when it does not. A run with no database file SHALL name no ruleset file. -#### Scenario: digest changes when database changes -- **WHEN** the advisory database files differ between two osv-scanner runs -- **THEN** the `database_digest` in the two ToolRun records is different +#### Scenario: The same database +- **WHEN** osv-scanner scans a tree two times and its database holds `PyPI/all.zip` and `npm/all.zip` both times +- **THEN** the two records have the same `ruleset_digest` +- **AND** the request names both files as ruleset files -#### Scenario: digest stable for unchanged database -- **WHEN** the advisory database files are identical between two osv-scanner runs -- **THEN** the `database_digest` in the two ToolRun records is the same +#### Scenario: A changed database +- **WHEN** the bytes of `PyPI/all.zip` change between two scans +- **THEN** the `ruleset_digest` of the second record differs from the first -### Requirement: ruff toolrun records configuration digest -The ToolRun of a Ruff run SHALL record a `config_digest` field that is a SHA-256 hex digest of the resolved configuration that Ruff used for the target. The digest SHALL change when the resolved configuration changes and SHALL stay the same when it does not. +#### Scenario: No database +- **WHEN** the offline database directory of osv-scanner holds no `all.zip` +- **THEN** the request names no ruleset file +- **AND** the `ruleset_digest` is the digest of no files -#### Scenario: digest changes when configuration changes -- **WHEN** the Ruff configuration of the target differs between two Ruff runs -- **THEN** the `config_digest` in the two ToolRun records is different +### Requirement: A ruleset file of a data directory is digested at any size +The record SHALL digest a ruleset file that lies below a read-only bind of the request by reading it as a stream, whatever its size. A ruleset file that lies below no bind, and every config file, SHALL still be refused when it is larger than 50 MiB (`DEFAULT_REPOMAP_MAX_FILE_SIZE_BYTES`). -#### Scenario: digest stable for unchanged configuration -- **WHEN** the Ruff configuration of the target is identical between two Ruff runs -- **THEN** the `config_digest` in the two ToolRun records is the same +#### Scenario: A large database file below a bind +- **WHEN** a request names a ruleset file of 50 MiB and one byte below one of its read-only binds +- **THEN** the run starts and the `ruleset_digest` is the digest of the content of that file + +#### Scenario: A large ruleset file below no bind +- **WHEN** a request names a ruleset file of 50 MiB and one byte that lies below no read-only bind +- **THEN** the executor refuses the request with a `SandboxValidationError` and the tool does not start + +#### Scenario: A large config file below a bind +- **WHEN** a request names a config file of 50 MiB and one byte that lies below a read-only bind +- **THEN** the executor refuses the request with a `SandboxValidationError` and the tool does not start diff --git a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/tasks.md b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/tasks.md index 6c292e073e..4e00c1a3a6 100644 --- a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/tasks.md +++ b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/tasks.md @@ -1,21 +1,22 @@ # Tasks -## 1. Add osv-scanner database digest to the ToolRun +## 1. Digest a ruleset file of a data directory as a stream -Reads: none. Changes: `src/devops_cli/review/scanners/osv_scanner.py`, `tests/test_review_tool_runs.py`. +Reads: `src/devops_cli/security/tool_executor.py` (lines 180-240, 353-383, 490-530), `src/devops_cli/ai/run_store.py` (lines 64-76), `tests/ai/test_tool_executor_contract.py` (lines 210-255, 525-565). Changes: `src/devops_cli/security/tool_executor.py`, `src/devops_cli/ai/run_store.py`, `tests/ai/test_tool_executor_contract.py`. -- [ ] 1.1 Write a test that creates two temporary directories with different file contents, calls the new `database_digest` function on each, and asserts the digests differ. Write a second test that calls the function twice on the same directory and asserts the digests are equal. The test fails because `database_digest` does not exist yet, and verify that `uv run devops ci test tests/test_review_tool_runs.py -k osv_scanner` fails on the base -- [ ] 1.2 Add a `database_digest(db_path: Path) -> str` function to the osv-scanner adapter. It lists the files under `db_path` in sorted order by relative path, opens each file in binary mode, and feeds its bytes into a single `hashlib.sha256` object in 64 KiB chunks. It returns the hex digest. Add a `database_digest: str | None` field to the ToolRun record. In the osv-scanner scan path, call `database_digest` on the database directory and store the result in the ToolRun before the SARIF export, and verify that `uv run devops ci test tests/test_review_tool_runs.py -k osv_scanner` passes -- [ ] 1.3 Change no document, run `uv run devops docs generate --sync-readme`, and verify that `uv run devops ci docs` passes +- [ ] 1.1 Add two cases to `tests/ai/test_tool_executor_contract.py`, `test_a_ruleset_file_below_a_bind_is_digested_at_any_size` and `test_a_config_file_below_a_bind_is_refused_when_it_is_too_large`, over the `executor` fixture and the `scenario` fixture, next to `test_a_file_that_cannot_be_digested_is_refused`. The first names a sparse file of `DEFAULT_REPOMAP_MAX_FILE_SIZE_BYTES + 1` bytes as a ruleset file, in a directory that the request also names in `read_only_binds`, and asserts that the run starts and that `ruleset_digest` is `digest([file_digest(that file)])`. The second names the same kind of file as a config file below a bind, and asserts a `SandboxValidationError` that matches `regular file`. Verify that `uv run devops ci test tests/ai/test_tool_executor_contract.py -k "below_a_bind"` fails on the base for the first case, with `SandboxValidationError ... cannot be read as a regular file of at most 52428800 bytes`, and that the second case already passes +- [ ] 1.2 Make `file_digest` in `src/devops_cli/ai/run_store.py` read the file as a stream with `hashlib.file_digest` and keep its first 16 hex digits and its `None` on `OSError`. Replace `_is_small_regular_file` in `src/devops_cli/security/tool_executor.py` by a check that takes an optional size limit. Give `_files_digest` the paths below which a file has no limit, and let `record_tool_run` pass `request.read_only_binds` for `ruleset_files` only, and no path for `config_files`. Update the docstrings of `_files_digest` and `ToolRequest` (a ruleset file below a bind). Verify that `uv run devops ci test tests/ai/test_tool_executor_contract.py tests/ai/test_run_store.py tests/security/test_review_tool_runs.py tests/security/test_scanner_tool_executor.py` passes -## 2. Add Ruff configuration digest to the ToolRun +## 2. Name the offline database as the ruleset of osv-scanner -Reads: none. Changes: `src/devops_cli/review/scanners/ruff.py`, `tests/test_review_tool_runs.py`. +Reads: `src/devops_cli/security/osv_scanner.py` (lines 240-300), `src/devops_cli/security/trivy.py` (lines 138-170), `src/devops_cli/config/constants.py` (lines 4590-4598), `tests/security/test_review_tool_runs.py` (lines 98-160, 200-260, 440-470). Changes: `src/devops_cli/security/osv_scanner.py`, `src/devops_cli/config/constants.py`, `tests/security/test_review_tool_runs.py`. -- [ ] 2.1 Write a test that creates a temporary project with a `ruff.toml`, calls the new `config_digest` function, and asserts it returns a 64-character hex string. Write a second test that modifies the `ruff.toml` and calls the function again, asserting the digest differs. The test fails because `config_digest` does not exist yet, and verify that `uv run devops ci test tests/test_review_tool_runs.py -k ruff` fails on the base -- [ ] 2.2 Add a `config_digest(target: Path) -> str` function to the Ruff adapter. It runs `ruff --show-settings --isolated ` via `subprocess.run`, captures stdout, and returns the SHA-256 hex digest of the JSON bytes. Add a `config_digest: str | None` field to the ToolRun record. In the Ruff scan path, call `config_digest` on the target and store the result in the ToolRun before the SARIF export, and verify that `uv run devops ci test tests/test_review_tool_runs.py -k ruff` passes -- [ ] 2.3 Change no document, run `uv run devops docs generate --sync-readme`, and verify that `uv run devops ci docs` passes +- [ ] 2.1 Add to `tests/security/test_review_tool_runs.py` a fixture `osv_database` that sets `OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY` to a temporary directory and writes `osv-scalibr/PyPI/all.zip` and `osv-scalibr/npm/all.zip` there. Add four cases that use it with the `osv-scanner` entry of `_REVIEW_SCANS`: the request names both files, sorted, as ruleset files; two runs give the same `ruleset_digest` and a changed `PyPI/all.zip` gives another; a database directory with no `all.zip` gives a request with no ruleset file and `digest([])`; a sparse `npm/all.zip` of `DEFAULT_REPOMAP_MAX_FILE_SIZE_BYTES + 1` bytes still gives a run with a `ruleset_digest`. Verify that `uv run devops ci test tests/security/test_review_tool_runs.py -k osv` fails on the base because the request names no ruleset file +- [ ] 2.2 Add `CONST_OSV_DB_FILE_NAME` (`all.zip`) beside `CONST_OSV_DB_DIR_NAME` in `src/devops_cli/config/constants.py`. Add `OsvScanner.ruleset_files`, which returns the sorted `*/` files of the `read` directory of `data_dirs()`. Verify that `uv run devops ci test tests/security/test_review_tool_runs.py tests/security/test_osv_scanner.py tests/security/test_scanner_tool_executor.py tests/security/test_scanner_contract.py` passes -## Workflow follow-up +## 3. Document the digests and confirm the Ruff criterion -- Pending a person: review the diff in tuicr (tuicr -r ..HEAD). +Reads: `docs/SELF_IMPROVEMENT.md` (lines 636-700). Changes: `docs/SELF_IMPROVEMENT.md`. + +- [ ] 3.1 In the table of `docs/SELF_IMPROVEMENT.md`, change the osv-scanner ruleset cell from "none: the database has no metadata file yet" to the `all.zip` file of each ecosystem in the `osv-scalibr` directory. Add one sentence that a ruleset file in a data directory of the tool is read as a stream, and any other file above 50 MiB stops the run. In the note on the Ruff search, add that a base file named by `extend` is not covered. Verify that `git grep -n "no metadata file yet" -- docs` prints nothing, then run `uv run devops docs generate --sync-readme` and verify that `uv run devops ci docs` passes +- [ ] 3.2 Confirm criterion 2 with no change: verify that `uv run devops ci test tests/security/test_review_tool_runs.py -k "changed_config_file_of_the_tree"` passes, with the `ruff` case in the run From 9f1e9782fb802c44cfef2b89a55470c31ed6947a Mon Sep 17 00:00:00 2001 From: Daniel Petty Date: Sun, 1 Nov 2026 12:16:55 +0000 Subject: [PATCH 2/2] docs(openspec): fix plan #2130 after verification Isolate the osv cache in the tests, fix the Reads lines of group 1, and state the Ruff extend limit as pending a person. --- .../changes/2130-the-toolrun-of-osv-scanner-and/design.md | 2 +- .../2130-the-toolrun-of-osv-scanner-and/proposal.md | 8 +++++--- .../changes/2130-the-toolrun-of-osv-scanner-and/tasks.md | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/design.md b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/design.md index ff91724d3d..a5bd96fb81 100644 --- a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/design.md +++ b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/design.md @@ -6,7 +6,7 @@ - **Digest a ruleset file below a read-only bind as a stream, with no size limit.**: The npm database is 210 MB, over the 50 MiB limit, and the sandbox executor refuses a file over the limit. A file below a bind is data that devops-cli or the user put in the cache of the tool. It is not a file of the reviewed tree. A stream needs no memory, so the limit has no reason there. - **Keep the limit for every other file.**: A config file or a ruleset file of the reviewed tree is not trusted. `ARCHITECTURE.md` asks for a size check before a read, and the contract test pins the refusal. Only the ruleset files below a bind of the request skip the limit. A config file never does. - **Make `file_digest` read as a stream.**: It is the one function that digests a file for the record and for the benchmarks. `hashlib.file_digest` gives the same SHA-256, so every digest stays the same. -- **No change to the Ruff scanner.**: The Ruff digest already follows the config files that Ruff finds. The one gap, a base file named by `extend`, is written into `docs/SELF_IMPROVEMENT.md`. +- **No change to the Ruff scanner, and a limit that a person decides.**: The Ruff digest follows the config files that Ruff finds by name (`ruff.toml`, `.ruff.toml`, `pyproject.toml`). Criterion 2 is met for them. A base file named by `extend` is part of the resolved configuration, and the digest does not cover it, so criterion 2 is not met for a config that uses `extend`. The plan does not close this gap, for two reasons. Ruff has no command that lists its config files, and no output of Ruff is stable enough to digest (see Feasibility). To follow `extend`, devops-cli would need its own reader of the chain: parse each file with `tomllib`, resolve a relative path against the file that names it, and guard against a loop. That is a new hand-written helper, and it is more than the issue asks. The plan writes the limit into `docs/SELF_IMPROVEMENT.md`. A person decides whether a follow-up issue must cover the `extend` chain. ## Rejected alternatives diff --git a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/proposal.md b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/proposal.md index cec8d37cc5..d53414f112 100644 --- a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/proposal.md +++ b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/proposal.md @@ -7,21 +7,23 @@ Issue #2130: the ToolRun of an osv-scanner run records no digest of its advisory - The ToolRun of an osv-scanner run changes when its database changes, and stays the same when it does not. - The ToolRun of a Ruff run changes when the Ruff configuration of the target changes. -The check on `origin/main` shows that only the first result is missing. The second result exists (see Acceptance). +The check on `origin/main` shows that the first result is missing. The second result exists for the config files that Ruff finds by name, and not for a base file named by `extend` (see Acceptance). ## What Changes - `OsvScanner` names the `all.zip` file of each ecosystem in its offline database as ruleset files, as `TrivyScanner` names its metadata file. The `ruleset_digest` of the ToolRun then follows the content of the database. - The record digests a ruleset file below a read-only bind of the request (the data directory of the tool) as a stream, whatever its size. A file that is not below a bind, and every config file, keep the 50 MiB limit. `file_digest` in `src/devops_cli/ai/run_store.py` reads a file as a stream, with the same result. - `docs/SELF_IMPROVEMENT.md` says what the osv-scanner ruleset digest covers. -- Ruff gets no change: its `config_digest` already follows the config files. +- Ruff gets no code change: its `config_digest` already follows the config files that Ruff finds by name. The `extend` limit is written down and left to a person. - The change has a spec delta in `review-tool-runs`: the ToolRun record in `review.md` and in the SARIF export is observable. - The change fits one pull request. ## Acceptance - Criterion 1, the osv-scanner digest follows the database: group 1 lets the record digest a large database file, and group 2 names the database files in the request. -- Criterion 2, the Ruff digest follows the Ruff configuration: already met on `main`. `RuffScanner.config_names` lists `.ruff.toml`, `ruff.toml` and `pyproject.toml`, and the test `test_a_changed_config_file_of_the_tree_changes_the_config_digest` pins it. Group 3 runs that test again and writes the one gap into the document: a Ruff config that uses `extend` has a base file that the digest does not cover. No criterion asks for that file, so the plan adds no code for it. +- Criterion 2, the Ruff digest follows the Ruff configuration: met on `main` for the files that Ruff finds by name, and pending a person for a base file named by `extend`. + - Met: `RuffScanner.config_names` lists `.ruff.toml`, `ruff.toml` and `pyproject.toml`, and the test `test_a_changed_config_file_of_the_tree_changes_the_config_digest` pins it. Group 3 runs that test again. + - Pending a person: a Ruff config that uses `extend` has a base file that is part of the resolved configuration, and the digest does not cover it. A change of that base file leaves the `config_digest` the same. The plan adds no code for it, because it needs a reader of the `extend` chain in devops-cli, and the choice is a decision of a person (see `design.md`). Group 3 writes the limit into `docs/SELF_IMPROVEMENT.md`. ## Feasibility diff --git a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/tasks.md b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/tasks.md index 4e00c1a3a6..1790fe0367 100644 --- a/openspec/changes/2130-the-toolrun-of-osv-scanner-and/tasks.md +++ b/openspec/changes/2130-the-toolrun-of-osv-scanner-and/tasks.md @@ -2,7 +2,7 @@ ## 1. Digest a ruleset file of a data directory as a stream -Reads: `src/devops_cli/security/tool_executor.py` (lines 180-240, 353-383, 490-530), `src/devops_cli/ai/run_store.py` (lines 64-76), `tests/ai/test_tool_executor_contract.py` (lines 210-255, 525-565). Changes: `src/devops_cli/security/tool_executor.py`, `src/devops_cli/ai/run_store.py`, `tests/ai/test_tool_executor_contract.py`. +Reads: `src/devops_cli/security/tool_executor.py` (lines 180-240, 353-383, 490-530), `src/devops_cli/ai/run_store.py` (lines 64-76), `tests/ai/test_tool_executor_contract.py` (lines 118-175 for `Scenario` and its `request()`, 267-312 for the `scenario` and `executor` fixtures, 527-565 for the file helpers and the refusal test). Changes: `src/devops_cli/security/tool_executor.py`, `src/devops_cli/ai/run_store.py`, `tests/ai/test_tool_executor_contract.py`. - [ ] 1.1 Add two cases to `tests/ai/test_tool_executor_contract.py`, `test_a_ruleset_file_below_a_bind_is_digested_at_any_size` and `test_a_config_file_below_a_bind_is_refused_when_it_is_too_large`, over the `executor` fixture and the `scenario` fixture, next to `test_a_file_that_cannot_be_digested_is_refused`. The first names a sparse file of `DEFAULT_REPOMAP_MAX_FILE_SIZE_BYTES + 1` bytes as a ruleset file, in a directory that the request also names in `read_only_binds`, and asserts that the run starts and that `ruleset_digest` is `digest([file_digest(that file)])`. The second names the same kind of file as a config file below a bind, and asserts a `SandboxValidationError` that matches `regular file`. Verify that `uv run devops ci test tests/ai/test_tool_executor_contract.py -k "below_a_bind"` fails on the base for the first case, with `SandboxValidationError ... cannot be read as a regular file of at most 52428800 bytes`, and that the second case already passes - [ ] 1.2 Make `file_digest` in `src/devops_cli/ai/run_store.py` read the file as a stream with `hashlib.file_digest` and keep its first 16 hex digits and its `None` on `OSError`. Replace `_is_small_regular_file` in `src/devops_cli/security/tool_executor.py` by a check that takes an optional size limit. Give `_files_digest` the paths below which a file has no limit, and let `record_tool_run` pass `request.read_only_binds` for `ruleset_files` only, and no path for `config_files`. Update the docstrings of `_files_digest` and `ToolRequest` (a ruleset file below a bind). Verify that `uv run devops ci test tests/ai/test_tool_executor_contract.py tests/ai/test_run_store.py tests/security/test_review_tool_runs.py tests/security/test_scanner_tool_executor.py` passes @@ -11,12 +11,12 @@ Reads: `src/devops_cli/security/tool_executor.py` (lines 180-240, 353-383, 490-5 Reads: `src/devops_cli/security/osv_scanner.py` (lines 240-300), `src/devops_cli/security/trivy.py` (lines 138-170), `src/devops_cli/config/constants.py` (lines 4590-4598), `tests/security/test_review_tool_runs.py` (lines 98-160, 200-260, 440-470). Changes: `src/devops_cli/security/osv_scanner.py`, `src/devops_cli/config/constants.py`, `tests/security/test_review_tool_runs.py`. -- [ ] 2.1 Add to `tests/security/test_review_tool_runs.py` a fixture `osv_database` that sets `OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY` to a temporary directory and writes `osv-scalibr/PyPI/all.zip` and `osv-scalibr/npm/all.zip` there. Add four cases that use it with the `osv-scanner` entry of `_REVIEW_SCANS`: the request names both files, sorted, as ruleset files; two runs give the same `ruleset_digest` and a changed `PyPI/all.zip` gives another; a database directory with no `all.zip` gives a request with no ruleset file and `digest([])`; a sparse `npm/all.zip` of `DEFAULT_REPOMAP_MAX_FILE_SIZE_BYTES + 1` bytes still gives a run with a `ruleset_digest`. Verify that `uv run devops ci test tests/security/test_review_tool_runs.py -k osv` fails on the base because the request names no ruleset file +- [ ] 2.1 Add to `tests/security/test_review_tool_runs.py` an autouse fixture `osv_cache` that sets `OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY` to an empty temporary directory for every test of the file, as `tree` isolates `TRIVY_CACHE_DIR` through `trivy_cache`. Without it, `_osv_db_base()` falls back to the user cache directory, and a database that the host fetched earlier would give the existing `osv-scanner` case of `_READS` a ruleset file. Leave that case unchanged: it must stay green, with `(["osv-scanner.toml"], [])`. Add a fixture `osv_database` that uses `osv_cache` and writes `osv-scalibr/PyPI/all.zip` and `osv-scalibr/npm/all.zip` there. Add four cases that use `osv_database` with the `osv-scanner` entry of `_REVIEW_SCANS`: the request names both files, sorted, as ruleset files; two runs give the same `ruleset_digest` and a changed `PyPI/all.zip` gives another; a database directory with no `all.zip` gives a request with no ruleset file and `digest([])`; a sparse `npm/all.zip` of `DEFAULT_REPOMAP_MAX_FILE_SIZE_BYTES + 1` bytes still gives a run with a `ruleset_digest`. Verify that `uv run devops ci test tests/security/test_review_tool_runs.py -k osv` fails on the base because the request names no ruleset file, and that the whole file passes with `OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY` unset - [ ] 2.2 Add `CONST_OSV_DB_FILE_NAME` (`all.zip`) beside `CONST_OSV_DB_DIR_NAME` in `src/devops_cli/config/constants.py`. Add `OsvScanner.ruleset_files`, which returns the sorted `*/` files of the `read` directory of `data_dirs()`. Verify that `uv run devops ci test tests/security/test_review_tool_runs.py tests/security/test_osv_scanner.py tests/security/test_scanner_tool_executor.py tests/security/test_scanner_contract.py` passes -## 3. Document the digests and confirm the Ruff criterion +## 3. Document the digests and confirm the Ruff criterion where it is met Reads: `docs/SELF_IMPROVEMENT.md` (lines 636-700). Changes: `docs/SELF_IMPROVEMENT.md`. - [ ] 3.1 In the table of `docs/SELF_IMPROVEMENT.md`, change the osv-scanner ruleset cell from "none: the database has no metadata file yet" to the `all.zip` file of each ecosystem in the `osv-scalibr` directory. Add one sentence that a ruleset file in a data directory of the tool is read as a stream, and any other file above 50 MiB stops the run. In the note on the Ruff search, add that a base file named by `extend` is not covered. Verify that `git grep -n "no metadata file yet" -- docs` prints nothing, then run `uv run devops docs generate --sync-readme` and verify that `uv run devops ci docs` passes -- [ ] 3.2 Confirm criterion 2 with no change: verify that `uv run devops ci test tests/security/test_review_tool_runs.py -k "changed_config_file_of_the_tree"` passes, with the `ruff` case in the run +- [ ] 3.2 Confirm the met part of criterion 2 with no change (the `extend` limit stays pending a person): verify that `uv run devops ci test tests/security/test_review_tool_runs.py -k "changed_config_file_of_the_tree"` passes, with the `ruff` case in the run