Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/adcp/reporting/ledger/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,9 @@ def _require_covered_rows(
)


def _zero_buy_snapshot(obligation: ReportingObligationRecord) -> bool:
"""A proven zero-buy period whose generation requires only snapshot reporting."""
return (
not obligation.media_buy_ids
and obligation.coverage_status == "full"
and obligation.required_finality == "snapshot"
)
def _zero_buy_obligation(obligation: ReportingObligationRecord) -> bool:
"""A period whose frozen, complete scope proves that it has no buys."""
return not obligation.media_buy_ids and obligation.coverage_status == "full"


@dataclass(frozen=True)
Expand Down Expand Up @@ -1373,12 +1369,14 @@ async def _close_without_source(
revision, unless its ``allow_partial`` generation can publish the
covered subset: the status projection reports a withheld one as
``REPORTING_COVERAGE_INCOMPLETE``. A proven zero-buy snapshot obligation
closes with a zero-row snapshot.
closes with a zero-row snapshot. Other zero-buy obligations have no
source work and remain without a revision.
"""
if _withheld(configuration, obligation):
return True
if _zero_buy_snapshot(obligation):
await self._commit_zero_buy_snapshot(obligation, turn=turn, now=now)
if _zero_buy_obligation(obligation):
if obligation.required_finality == "snapshot":
await self._commit_zero_buy_snapshot(obligation, turn=turn, now=now)
return True
return False

Expand Down Expand Up @@ -3169,8 +3167,10 @@ async def _acquire_obligation(
now = now or self._clock()
if _withheld(configuration, obligation):
return None
if _zero_buy_snapshot(obligation):
return await self._commit_zero_buy_snapshot(obligation, turn=turn, now=now)
if _zero_buy_obligation(obligation):
if obligation.required_finality == "snapshot":
return await self._commit_zero_buy_snapshot(obligation, turn=turn, now=now)
return None
finality = target_finality or obligation.required_finality
selected_policy = obligation.definition.finality_policy if obligation.definition else None
if finality == "official" and isinstance(selected_policy, ReportingStabilizedPolicy):
Expand Down
111 changes: 106 additions & 5 deletions tests/test_reporting_all_media_buys_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

from dataclasses import replace
from datetime import datetime, timezone
from typing import Any

import pytest

from adcp.reporting.fixtures import OFFICIAL_OFFERING_ID, SNAPSHOT_OFFERING_ID
from adcp.reporting.fixtures import (
OFFICIAL_OFFERING_ID,
SNAPSHOT_OFFERING_ID,
redacted_completed_result,
)
from adcp.reporting.ledger import (
CompleteMediaBuyScope,
CompletePackageRoster,
Expand All @@ -21,15 +26,29 @@
ReportingPeriodBoundary,
ReportingProducer,
ReportingScopeBuy,
ReportingSourceFinalPolicy,
ReportingStatusCaller,
ReportingStatusHandler,
UnsupportedMediaBuyRoster,
WorkerTurn,
freeze_all_media_buys,
)
from adcp.reporting.ledger.store import _config_payload
from adcp.reporting.source import (
ReportingSourceExecutorResult,
ReportingSourceSliceRequestV1,
ReportingSourceStagedObjectReader,
)
from tests.test_reporting_ledger import _configuration
from tests.test_reporting_producer_retry import FailingSource
from tests.test_reporting_producer_retry import (
FailingSource,
)
from tests.test_reporting_producer_retry import (
configuration as _source_configuration,
)
from tests.test_reporting_producer_retry import (
producer as _source_producer,
)

UTC = timezone.utc
CALLER = ReportingStatusCaller(account_id="acct_1", consumer_id="buyer_1")
Expand Down Expand Up @@ -89,6 +108,33 @@ async def __call__(self, configuration, period):
return self.result


class _SuccessfulSource(FailingSource):
"""An authoritative source that serves one generation-pinned row."""

def __init__(self) -> None:
super().__init__()
self.reader: ReportingSourceStagedObjectReader | None = None

async def execute(
self, request: ReportingSourceSliceRequestV1, *, cancel: Any
) -> ReportingSourceExecutorResult:
self.calls += 1
self.requests.append(request)
result, _, self.reader = redacted_completed_result(
request,
page_bodies=[
'{"media_buy_id":"media-buy-1","campaign_id":"campaign-redacted-1",'
'"impressions":10}\n'
],
observed_at=request.period.end,
)
return result

async def read(self, **kwargs: Any) -> bytes:
assert self.reader is not None
return await self.reader.read(**kwargs)


# -- freeze_all_media_buys ------------------------------------------------------


Expand Down Expand Up @@ -316,7 +362,7 @@ async def test_an_explicit_empty_snapshot_scope_also_closes_with_zero_rows() ->
assert len(turn.revisions_committed) == 1 and turn.slices_failed == []


async def test_a_zero_buy_official_period_is_never_closed_from_roster_proof() -> None:
async def test_a_zero_buy_official_period_skips_source_without_fabricating_finality() -> None:
store = InMemoryReportingLedgerStore(clock=lambda: FIRST_CLOSE)
configuration = _configuration(media_buy_ids=(), required_finality="official")
await store.put_configuration(configuration)
Expand All @@ -329,9 +375,10 @@ async def test_a_zero_buy_official_period_is_never_closed_from_roster_proof() ->
)
(obligation,) = await producer.close_elapsed_periods(configuration, now=FIRST_CLOSE)
turn = WorkerTurn()
with pytest.raises(LedgerConflictError) as refused:
assert (
await producer.acquire_obligation(configuration, obligation, turn=turn, now=FIRST_CLOSE)
assert refused.value.code == "EMPTY_DENOMINATOR"
is None
)
assert source.calls == 0 and turn.revisions_committed == []
assert (
await store.list_revisions(
Expand All @@ -342,6 +389,60 @@ async def test_a_zero_buy_official_period_is_never_closed_from_roster_proof() ->
)


async def test_a_zero_buy_official_period_does_not_block_later_source_work() -> None:
now = datetime(2026, 11, 1, 3, 10, tzinfo=UTC)
store = InMemoryReportingLedgerStore(clock=lambda: now)
source = _SuccessfulSource()
configuration = _source_configuration()
assert configuration.definition is not None
configuration = replace(
configuration,
media_buy_ids=(),
media_buy_scope="all_media_buys",
definition=replace(
configuration.definition,
finality_policy=ReportingSourceFinalPolicy("fixture", "source-evidence-redacted"),
),
)
await store.put_configuration(configuration)

async def scope(
_configuration: ReportingConfiguration, period: ReportingPeriodBoundary
) -> CompleteMediaBuyScope:
if period.end.hour == 2:
return CompleteMediaBuyScope(())
return CompleteMediaBuyScope(("media-buy-1",))

producer = _source_producer(
source,
store,
clock=lambda: now,
media_buy_scope_resolver=scope,
object_reader=source,
)
turn = await producer.run_configuration(configuration, now=now)

obligations = await store.list_generation_obligations(configuration.generation_key)
assert [obligation.media_buy_ids for obligation in obligations] == [
(),
("media-buy-1",),
]
assert source.calls == 1
assert turn.slices_failed == []
assert (
await store.list_revisions(
account_id=configuration.account_id,
reporting_obligation_id=obligations[0].reporting_obligation_id,
)
== ()
)
(revision,) = await store.list_revisions(
account_id=configuration.account_id,
reporting_obligation_id=obligations[1].reporting_obligation_id,
)
assert revision.finality == "official"


async def test_without_a_resolver_an_all_scope_generation_never_closes() -> None:
store = InMemoryReportingLedgerStore()
configuration = _all_scope()
Expand Down