diff --git a/benchmarks/valid/cohort_selection.py b/benchmarks/valid/cohort_selection.py index 11b00405..e40aa7d0 100644 --- a/benchmarks/valid/cohort_selection.py +++ b/benchmarks/valid/cohort_selection.py @@ -273,6 +273,7 @@ def _ledger_facts(batches: Sequence[CohortBatchOutcomeV1]) -> _LedgerFacts: eco_policy = _closed_digest(batch.ecosystem_policy_digest) gt_policy = _closed_digest(batch.ground_truth_policy_digest) digests.add(_closed_digest(batch.batch_digest)) + _closed_vocabulary(batch.pending_entries, _DIGEST) batch_families: set[str] = set() for positive in batch.accepted_positives: for scope in positive.verified_scopes: @@ -501,13 +502,21 @@ def _pending_by_ecosystem( state: SourcingQueueStateV1, batches: Sequence[CohortBatchOutcomeV1], ) -> dict[str, tuple[ReviewQueueEntryV1, ...]]: - """Pending entries per ecosystem from every batch's resolved records. + """Pending entries per ecosystem, derived from every batch's record outcomes. - A record resolves its entry only through the committed review machinery: - accepted positives and review-level exclusions answer the entry, while - unreadable or evidence-invalid records leave it pending. A narrowed - selection batch therefore surrenders exactly the entries its records - resolved, never the whole shared queue. + A record resolves its entry only when it is an accepted record or carries + a review-level exclusion reason; unreadable or evidence-invalid records + leave it pending. Reading records instead of a batch's ``pending_entries`` + keeps a narrowed selection batch from answering the whole shared queue: + only entries whose records resolved are marked answered here. + + This view is not equivalent to the third round's ``pending_entries`` view + in one corner. Ingestion marks an entry answered as soon as its review + pair validates, so a record then excluded as a conflicting accepted review + is absent from ``pending_entries`` and the third round counts it answered, + while here it stays pending because that exclusion reason is not + review-level. The fourth round therefore offers such an entry again rather + than dropping it. """ queued = { ecosystem: {entry.entry_digest for entry in _queue_for(state, ecosystem).entries} diff --git a/tests/test_valid_benchmark_cohort_selection.py b/tests/test_valid_benchmark_cohort_selection.py index fa34b944..b2cfd178 100644 --- a/tests/test_valid_benchmark_cohort_selection.py +++ b/tests/test_valid_benchmark_cohort_selection.py @@ -39,6 +39,7 @@ from benchmarks.valid.cohort_selection import ( # noqa: E402 CohortSelectionStatus, cohort_selection, cohort_selection_bytes, + select_fourth_cohort_batch, select_third_cohort_batch, ) from benchmarks.valid.cohort_selection_store import ( # noqa: E402 @@ -306,6 +307,43 @@ def test_malformed_ledgers_and_states_fail_closed(): _select({"python_queue": ()}, ledger) +@pytest.mark.parametrize( + "select", + (select_third_cohort_batch, select_fourth_cohort_batch), + ids=("third", "fourth"), +) +def test_forged_pending_entries_outside_the_durable_shape_fail_closed(select) -> None: + state, _, ledger = _prepared() + batch = next(item for item in ledger if item.pending_entries) + intact = replace(batch, pending_entries=tuple(batch.pending_entries)) + rest = tuple(item for item in ledger if item is not batch) + + def decided(pending_entries): + return select( + state=state, + accepted_batches=(replace(batch, pending_entries=pending_entries), *rest), + expected_ecosystem_policy_digest=ECOSYSTEM_POLICY, + expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY, + ) + + baseline = decided(intact.pending_entries) + assert baseline.status is CohortSelectionStatus.SELECTED + assert intact.pending_entries and all(len(item) == 64 for item in intact.pending_entries) + + for forged in ( + list(intact.pending_entries), + set(intact.pending_entries), + iter(intact.pending_entries), + intact.pending_entries[0], + (*intact.pending_entries[:-1], "not-a-digest"), + (*intact.pending_entries, intact.pending_entries[0]), + None, + ): + with pytest.raises(CohortSelectionStateError) as error: + decided(forged) + assert error.value.reason is CohortSelectionStateReason.STATE_MISMATCH + + def test_selection_module_and_outputs_avoid_out_of_scope_machinery(): root = Path(__file__).resolve().parents[1] for name in ("benchmarks/valid/cohort_selection.py", "benchmarks/valid/cohort_selection_store.py"):