44"""
55
66from contextlib import contextmanager
7- from dataclasses import dataclass , is_dataclass
7+ from dataclasses import dataclass , is_dataclass , replace
88import logging
99from typing import (
1010 Any ,
@@ -1503,6 +1503,10 @@ def _prepare_time_travel_states(cls, snapshot: Snapshot) -> Dict[str, PreparedSt
15031503 scope_id = scope , metadata = state_snapshot .meta
15041504 )
15051505 schema .validate_metadata_contracts ("time_travel" )
1506+ state_snapshot = cls ._reconcile_snapshot_schema (
1507+ schema ,
1508+ state_snapshot ,
1509+ )
15061510 cls ._apply_time_travel_snapshot_state (schema , state_snapshot , materialize = False )
15071511 binding .validate_scope_membership (schema , values .keys ())
15081512 schema ._raw_dirty = schema .parameters != schema ._saved_parameters
@@ -1526,6 +1530,46 @@ def _prepare_time_travel_states(cls, snapshot: Snapshot) -> Dict[str, PreparedSt
15261530 raise ValueError (f"Cyclic historical ObjectState parents: { sorted (pending )!r} ." )
15271531 return prepared
15281532
1533+ @staticmethod
1534+ def _reconcile_snapshot_schema (
1535+ schema : "ObjectState" ,
1536+ snapshot : StateSnapshot ,
1537+ ) -> StateSnapshot :
1538+ """Project historical values onto the declaration's current field schema.
1539+
1540+ Construction owns the valid parameter surface. Historical snapshots own
1541+ values only for fields still declared on that surface. This gives newly
1542+ declared fields their construction-derived defaults and prevents removed
1543+ fields from surviving as detached historical parameters.
1544+ """
1545+
1546+ def reconcile (current : dict , historical : dict ) -> dict :
1547+ return {
1548+ key : historical [key ] if key in historical else value
1549+ for key , value in current .items ()
1550+ }
1551+
1552+ return replace (
1553+ snapshot ,
1554+ parameters = reconcile (schema .parameters , snapshot .parameters ),
1555+ saved_parameters = reconcile (
1556+ schema ._saved_parameters ,
1557+ snapshot .saved_parameters ,
1558+ ),
1559+ saved_resolved = reconcile (
1560+ schema ._saved_resolved ,
1561+ snapshot .saved_resolved ,
1562+ ),
1563+ live_resolved = reconcile (
1564+ schema ._live_resolved ,
1565+ snapshot .live_resolved ,
1566+ ),
1567+ provenance = reconcile (
1568+ schema ._live_provenance ,
1569+ snapshot .provenance ,
1570+ ),
1571+ )
1572+
15291573 @classmethod
15301574 def _restore_time_travel_states (
15311575 cls ,
0 commit comments