From 5f5951098d71fc9141b188faa0f9fd1589b0f395 Mon Sep 17 00:00:00 2001 From: rachitm022 Date: Sat, 5 Dec 2026 20:47:01 +0530 Subject: [PATCH] fix(reporting): cap status issues at the schema limit Co-Authored-By: Claude Opus 5.5 --- docs/reporting-release-notes.md | 10 +++ src/adcp/reporting/feed/projection.py | 3 +- src/adcp/reporting/ledger/status.py | 13 ++- .../reporting/test_reporting_feed_contract.py | 50 ++++++++++- tests/test_reporting_ledger.py | 87 +++++++++++++++++++ 5 files changed, 157 insertions(+), 6 deletions(-) diff --git a/docs/reporting-release-notes.md b/docs/reporting-release-notes.md index 088be43984..3129da9798 100644 --- a/docs/reporting-release-notes.md +++ b/docs/reporting-release-notes.md @@ -1,5 +1,15 @@ # Reliable Reporting upgrade and release notes +## Reporting status issues stay within the protocol limit + +`ReportingStatusHandler` returns at most 100 `issues` in summary and periods +responses and in each period projection, matching the AdCP response schema. +`action_required` issues are retained before `delayed` issues, with the existing +deterministic issue order preserved within each severity. Obligation counts still +cover the complete selected scope and are not reduced when the issue projection is +truncated. Adopters using the seller-side Reliable Reporting handler should upgrade +before serving scopes that can contain more than 100 concurrent reporting issues. + ## Protocol-shape control totals New Core revisions bind `{name, value, value_type[, unit]}` control totals in diff --git a/src/adcp/reporting/feed/projection.py b/src/adcp/reporting/feed/projection.py index 6083ff2fd0..8f2127d794 100644 --- a/src/adcp/reporting/feed/projection.py +++ b/src/adcp/reporting/feed/projection.py @@ -45,6 +45,7 @@ _adjustment_to_wire, _consumer_status_to_wire, _filters, + _issues_to_wire, _obligation_to_wire, _parse, _revision_to_wire, @@ -538,7 +539,7 @@ def capture_feed( obligations=tuple(p.obligation for p in scope_result.obligations), ), "health": scope_result.health, - "issues": [issue.to_wire() for issue in scope_result.issues], + "issues": _issues_to_wire(scope_result.issues), } if representation_version == 2 and not ( scope_result.health == "complete" and "adcp_version" in filters diff --git a/src/adcp/reporting/ledger/status.py b/src/adcp/reporting/ledger/status.py index bb3b564c75..4ec0de798d 100644 --- a/src/adcp/reporting/ledger/status.py +++ b/src/adcp/reporting/ledger/status.py @@ -80,6 +80,8 @@ ReportingStatusView = Literal["summary", "periods", "revision"] _DEFAULT_PAGE_SIZE = 100 +_STATUS_ISSUE_LIMIT = 100 +_ISSUE_SEVERITY_PRIORITY = {"action_required": 0, "delayed": 1, "info": 2} def _utc(value: datetime) -> datetime: @@ -90,6 +92,11 @@ def _iso(value: datetime) -> str: return _utc(value).isoformat().replace("+00:00", "Z") +def _issues_to_wire(issues: Sequence[ReportingIssue]) -> list[dict[str, Any]]: + prioritized = sorted(issues, key=lambda issue: _ISSUE_SEVERITY_PRIORITY[issue.severity]) + return [issue.to_wire() for issue in prioritized[:_STATUS_ISSUE_LIMIT]] + + @dataclass(frozen=True) class ReportingStatusCaller: """The authenticated caller, resolved from transport, never from the body. @@ -446,7 +453,7 @@ def render_snapshot( "data_through": _iso(watermark) if watermark else None, **({"next_expected_at": _iso(next_expected)} if next_expected is not None else {}), "obligation_counts": counts, - "issues": [i.to_wire() for i in result.issues], + "issues": _issues_to_wire(result.issues), } owners = {o.reporting_obligation_id: o for o in obligations} records: dict[tuple[str, str], Any] = { @@ -495,7 +502,7 @@ def render_snapshot( payload = { **common, "health": result.health, - "issues": [issue.to_wire() for issue in result.issues], + "issues": _issues_to_wire(result.issues), "changes_checkpoint": _encode_checkpoint(snapshot.max_sequence, caller=caller), "periods": periods, "revisions": [ @@ -823,7 +830,7 @@ def _obligation_to_wire( "production_status": production_status, "revision_count": len(revisions), "adjustment_count": 0, - "issues": [issue.to_wire() for issue in issues], + "issues": _issues_to_wire(issues), } if statuses: current = next((item for item in statuses if not item.superseded), None) diff --git a/tests/conformance/reporting/test_reporting_feed_contract.py b/tests/conformance/reporting/test_reporting_feed_contract.py index 6120945a01..b2066e2123 100644 --- a/tests/conformance/reporting/test_reporting_feed_contract.py +++ b/tests/conformance/reporting/test_reporting_feed_contract.py @@ -6,12 +6,13 @@ import pytest -from adcp.reporting.ledger import ReportingMaterializationCheck +from adcp.reporting.ledger import ReportingMaterializationCheck, derive_period from adcp.reporting.ledger.status import ReportingStatusCaller, ReportingStatusHandler from adcp.types import GetReportingStatusResponse from ._feed_support import ( ARRAYS, + feed_harness, feed_request, feeds, mixed_case, @@ -21,12 +22,57 @@ without_feed, ) from ._generation_support import configuration, revision_for -from ._receipt_support import extra_materialization, request_for +from ._receipt_support import extra_materialization, receipt_case, request_for from .test_reporting_notification_outbox import statement __all__ = ["feeds"] +async def test_frozen_feed_caps_and_prioritizes_status_issues() -> None: + async with feed_harness("memory") as h: + case = await receipt_case(h) + ledger_as_of = h.clock() + timedelta(days=10) + for ordinal in range(1, 102): + period = derive_period( + case.obligation.schedule, account_timezone="UTC", ordinal=ordinal + ) + await h.store.commit_obligation( + replace( + case.obligation, + reporting_obligation_id=f"delayed-obligation-{ordinal:03d}", + period=period, + scope_resolved_at=period.end, + automated_recovery_deadline_at=ledger_as_of + timedelta(days=1), + created_at=period.end, + ) + ) + period = derive_period(case.obligation.schedule, account_timezone="UTC", ordinal=102) + await h.store.commit_obligation( + replace( + case.obligation, + reporting_obligation_id="action-required-obligation", + period=period, + scope_resolved_at=period.end, + automated_recovery_deadline_at=ledger_as_of, + created_at=period.end, + ) + ) + h.clock.now = ledger_as_of + + page = await h.store.read_reporting_feed( + feed_request(case, limit=100), caller=case.binding.principal + ) + + GetReportingStatusResponse.model_validate(page) + assert len(page["issues"]) == 100 + assert page["issues"][0]["severity"] == "action_required" + delayed_issue_ids = [ + issue["issue_id"] for issue in page["issues"] if issue["severity"] == "delayed" + ] + assert delayed_issue_ids == sorted(delayed_issue_ids) + assert page["pagination"]["total_count"] == 105 + + @pytest.mark.parametrize("limit", [1, 5, 6, 7]) async def test_allowlist_exact_totals_dependency_order_and_final_only_checkpoint(feeds, limit): h = feeds diff --git a/tests/test_reporting_ledger.py b/tests/test_reporting_ledger.py index e6dc209e4c..7be1cc9103 100644 --- a/tests/test_reporting_ledger.py +++ b/tests/test_reporting_ledger.py @@ -637,6 +637,93 @@ async def test_summary_escalates_after_the_automated_recovery_window() -> None: assert payload["issues"][0]["recommended_action"] == "contact_seller" +async def test_status_issue_limit_keeps_actionable_issues_and_complete_counts() -> None: + from adcp.validation.schema_loader import get_named_validator + + ledger_as_of = datetime(2026, 9, 30, tzinfo=timezone.utc) + store = InMemoryReportingLedgerStore(clock=lambda: ledger_as_of) + configuration = _configuration() + await store.put_configuration(configuration) + for ordinal in range(1, 102): + await store.commit_obligation( + _obligation( + configuration, + ordinal=ordinal, + automated_recovery_deadline_at=ledger_as_of + timedelta(days=1), + ) + ) + await store.commit_obligation( + _obligation( + configuration, + ordinal=102, + automated_recovery_deadline_at=ledger_as_of - timedelta(days=1), + ) + ) + + handler = ReportingStatusHandler(store, page_size=1_000) + validator = get_named_validator("media-buy/get-reporting-status-response.json") + assert validator is not None + for view in ("summary", "periods"): + payload = await handler.handle({"view": view}, caller=CALLER) + + errors = sorted(validator.iter_errors(payload), key=str) + assert errors == [] + assert len(payload["issues"]) == 100 + assert payload["issues"][0]["severity"] == "action_required" + delayed_issue_ids = [ + issue["issue_id"] for issue in payload["issues"] if issue["severity"] == "delayed" + ] + assert delayed_issue_ids == sorted(delayed_issue_ids) + if view == "summary": + assert payload["obligation_counts"] == { + "total": 102, + "waiting": 0, + "healthy": 0, + "delayed": 101, + "action_required": 1, + "complete": 0, + } + else: + assert len(payload["periods"]) == 102 + assert all(len(period["issues"]) == 1 for period in payload["periods"]) + + +def test_period_projection_applies_the_status_issue_limit() -> None: + from adcp.reporting.ledger.models import ReportingIssue + from adcp.reporting.ledger.status import _obligation_to_wire + + issues = tuple( + ReportingIssue( + issue_id=f"issue_{index:03d}", + code="REPORT_OVERDUE", + severity="delayed", + responsible_party="seller", + recommended_action="wait_for_retry", + ) + for index in range(101) + ) + ( + ReportingIssue( + issue_id="issue_action_required", + code="REPORT_OVERDUE", + severity="action_required", + responsible_party="seller", + recommended_action="contact_seller", + ), + ) + + payload = _obligation_to_wire( + _obligation(_configuration()), + revisions=(), + health="action_required", + production_status="pending", + issues=issues, + statuses=(), + ) + + assert len(payload["issues"]) == 100 + assert payload["issues"][0]["issue_id"] == "issue_action_required" + + async def test_periods_view_paginates_one_snapshot() -> None: store, obligation = await _seeded() revision, rows = _revision(obligation)