148148 configuration_lifecycle ,
149149 encode_cursor ,
150150 issue_is_retirable ,
151+ provisional_observation_reuse_candidate ,
151152 reject_reserved_authoritative_party ,
152153 validate_adjustment_currency ,
153154 validate_managed_revision_rows ,
155+ validate_retained_provisional_observation ,
154156 validate_revision_currency ,
155157)
156158from adcp_reporting_storage .ledger .row_storage import (
@@ -1810,10 +1812,13 @@ async def commit_provisional_observation(
18101812 ) -> ReportingRevisionRecord :
18111813 acquisition = observation .acquisition
18121814 key = (acquisition .account_id , acquisition .obligation_id , acquisition .ordinal )
1815+ reuse_candidate = provisional_observation_reuse_candidate (
1816+ acquisition , observation , revision
1817+ )
18131818 if revision is not None and (
18141819 revision .account_id != acquisition .account_id
18151820 or revision .reporting_obligation_id != acquisition .obligation_id
1816- or revision .reporting_revision_id != observation .revision_id
1821+ or ( revision .reporting_revision_id != observation .revision_id and not reuse_candidate )
18171822 ):
18181823 raise LedgerConflictError ("OBSERVATION_CONFLICT" , "observation identity differs" )
18191824 async with self .transaction (), self ._connection () as connection :
@@ -1839,7 +1844,14 @@ async def commit_provisional_observation(
18391844 retained_observation = ProvisionalObservation .from_wire (existing [1 ])
18401845 if (
18411846 retained_observation .acquisition != acquisition
1842- or existing [0 ] != observation .revision_id
1847+ or (
1848+ existing [0 ] != observation .revision_id
1849+ and (
1850+ not reuse_candidate
1851+ or revision is None
1852+ or existing [0 ] != revision .reporting_revision_id
1853+ )
1854+ )
18431855 or retained_observation .manifest_json != observation .manifest_json
18441856 ):
18451857 raise LedgerConflictError ("OBSERVATION_CONFLICT" , "observation replay differs" )
@@ -1852,11 +1864,10 @@ async def commit_provisional_observation(
18521864 )
18531865 # Reuse the immutable publication replay checks while holding
18541866 # the observation's account lock and transaction.
1855- return (
1856- await self .commit_revision (revision , rows )
1857- if revision is not None
1858- else retained_revision
1859- )
1867+ if revision is None or (reuse_candidate and existing [0 ] == observation .revision_id ):
1868+ validate_retained_provisional_observation (retained_revision , observation , rows )
1869+ return retained_revision
1870+ return await self .commit_revision (revision , rows )
18601871 reserved = await (
18611872 await connection .execute (
18621873 "SELECT payload FROM reporting_provisional_acquisitions"
@@ -1872,7 +1883,7 @@ async def commit_provisional_observation(
18721883 )
18731884 if checkpoint is not None and checkpoint .next_observation != acquisition .ordinal :
18741885 raise LedgerConflictError ("OBSERVATION_CONFLICT" , "observation ordinal changed" )
1875- if revision is None :
1886+ if revision is None or reuse_candidate :
18761887 if acquisition .predecessor_revision_id != observation .revision_id :
18771888 raise LedgerConflictError (
18781889 "OBSERVATION_CONFLICT" , "snapshot predecessor differs"
@@ -1902,6 +1913,16 @@ async def commit_provisional_observation(
19021913 raise LedgerConflictError (
19031914 "OBSERVATION_CONFLICT" , "snapshot predecessor is no longer current"
19041915 )
1916+ validate_retained_provisional_observation (
1917+ committed , observation , rows , require_readable = False
1918+ )
1919+ if not committed .readable :
1920+ if revision is None :
1921+ raise LedgerConflictError (
1922+ "HISTORY_UNAVAILABLE" , "snapshot predecessor is not readable"
1923+ )
1924+ committed = await self .commit_revision (revision , rows )
1925+ observation = replace (observation , revision_id = committed .reporting_revision_id )
19051926 else :
19061927 committed = await self .commit_revision (revision , rows )
19071928 await self .record_restatement_checkpoint (
0 commit comments