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
32 changes: 30 additions & 2 deletions src/objectstate/object_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def __init__(
#
# If we used copy.deepcopy(_live_resolved), we'd bake in ancestor's unsaved
# edits, causing inverted dirty state when ancestor is saved/reset.
self._saved_resolved = self._compute_resolved_snapshot(use_saved=True)
self._saved_resolved = self._saved_resolved_snapshot()

# DEBUG: Log live vs saved at registration
logger.debug(f"🔵 INIT_RESOLVED: scope={self.scope_id!r} obj_type={type(self.object_instance).__name__}")
Expand Down Expand Up @@ -2051,6 +2051,34 @@ def prepare_for_snapshot_capture(self) -> None:

# ==================== SAVED STATE / DIRTY TRACKING ====================

def _saved_resolved_snapshot(self) -> dict[str, Any]:
"""The saved epoch's resolved values: the live ones while the two epochs read the
same values, resolved on their own otherwise.

The saved epoch reads this state's saved parameters and each registered ancestor's
saved object; the live epoch reads the current ones. While this state and every
ancestor hold exactly their saved parameters, both epochs read the same values and
resolve alike, so a state opened or saved under clean ancestors (every view opened
over saved settings) resolves its fields once instead of once per epoch.
"""
if self._saved_epoch_is_live():
# The saved sources first: bringing the live snapshot up to date resolves the
# saved one again when its sources are not yet the ones recorded.
self._resolved_from[True] = self._resolution_sources(sync_delegates=False)[True]
self._ensure_live_resolved(notify_flash=False)
# Copied as the saved parameters are: a live value changed in place stays dirty.
return self._copy_parameters_for_saved_baseline(self._live_resolved)
return self._compute_resolved_snapshot(use_saved=True)

def _saved_epoch_is_live(self) -> bool:
if self.is_raw_dirty:
return False
return not any(
state.is_raw_dirty
for _, state in ObjectStateRegistry.registered_ancestors(self.scope_id)
if state is not self
)

def _compute_resolved_snapshot(self, use_saved: bool = False) -> dict[str, Any]:
"""Resolve editor fields using the selected live or saved ancestor epoch."""
if not isinstance(self.parameters, dict):
Expand Down Expand Up @@ -2243,7 +2271,7 @@ def mark_saved(self) -> None:
# Compute new saved resolved using SAVED ancestor baselines (use_saved=True)
# This ensures saved baseline is computed relative to other saved baselines
logger.debug(f"🔧 mark_saved: Computing new saved_resolved for scope={self.scope_id!r}")
new_saved_resolved = self._compute_resolved_snapshot(use_saved=True)
new_saved_resolved = self._saved_resolved_snapshot()
logger.debug(f"🔧 mark_saved: New saved_resolved computed, keys={list(new_saved_resolved.keys())[:5]}...")

# Update saved resolved baseline
Expand Down
85 changes: 65 additions & 20 deletions src/objectstate/object_state_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@
logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class SnapshotReach:
"""The objects one scope's values reached when ``capture`` was taken of them.

``held`` keeps those objects alive while this is kept, so an identity in
``identities`` stays theirs and is never another object's reused id.
"""

identities: frozenset
held: list
capture: object = None

def of(self, capture: object) -> "SnapshotReach":
return SnapshotReach(self.identities, self.held, capture)


class ObjectStateRegistrySubscription:
"""Idempotent ownership handle for one registry callback registration."""

Expand Down Expand Up @@ -215,6 +231,11 @@ class attributes, so two independent registries cannot exist.
_on_unregister_callbacks: List[Callable[[str, 'ObjectState'], None]] = []
_deferred_invalidations: Set[DeferredFieldInvalidation] = set()
_snapshot_dirty_scopes: Set[str] = set()
# The objects each scope's values reached when it was last captured, with the capture
# they belong to. A scope left clean since keeps that capture in the next snapshot and
# reaches the same objects, so a snapshot walks only the scopes it captures to find
# which clean ones share an object with them (`_record_snapshot_internal`).
_snapshot_reach: dict[str, "SnapshotReach"] = {}

# Time-travel completion callbacks - UI subscribes to reopen windows for dirty states
# Callbacks receive (dirty_states, triggering_scope) where:
Expand Down Expand Up @@ -585,6 +606,7 @@ def clear(cls) -> None:
cls._deferred_invalidations.clear()
_live_invalidation_deferrals.set(0)
cls._snapshot_dirty_scopes.clear()
cls._snapshot_reach.clear()
cls._snapshots.clear()
cls._timelines.clear()
cls._current_head = None
Expand Down Expand Up @@ -1248,8 +1270,10 @@ def _record_snapshot_internal(

for state in cls._states.values():
state.validate_metadata_contracts("snapshot")
capture_values = {
key: StateSnapshot(

def capture_values(key: str) -> StateSnapshot:
state = cls._states[key]
return StateSnapshot(
saved_resolved=state._saved_resolved,
live_resolved=state._live_resolved,
parameters=state.parameters,
Expand All @@ -1258,27 +1282,46 @@ def _record_snapshot_internal(
construction=state.construction_binding(),
meta=state.copy_metadata_for_snapshot(),
)
for key, state in cls._states.items()
}

def walked(values: StateSnapshot) -> SnapshotReach:
memo: dict[int, Any] = {}
copy.deepcopy(values, memo)
return SnapshotReach(
frozenset(memo) - {id(memo), id(values), id(values.construction)},
memo.get(id(memo), []),
)

# A changed scope also invalidates immutable snapshots that share a
# copied object with it. Independent clean scopes retain structural
# sharing; connected scopes are copied together with one graph memo.
alias_scopes: Dict[int, Set[str]] = {}
for key, values in capture_values.items():
memo: Dict[int, Any] = {}
copy.deepcopy(values, memo)
for identity in memo:
if identity not in (id(memo), id(values), id(values.construction)):
alias_scopes.setdefault(identity, set()).add(key)
pending = list(snapshot_candidate_keys)
while pending:
key = pending.pop()
for scopes in alias_scopes.values():
if key in scopes:
additional = scopes - snapshot_candidate_keys
snapshot_candidate_keys.update(additional)
pending.extend(additional)
captured = copy.deepcopy({key: capture_values[key] for key in snapshot_candidate_keys})
# Only the scopes being captured are walked: a clean scope reaches what
# it reached when its kept capture was taken.
values_of = {key: capture_values(key) for key in snapshot_candidate_keys}
reach = {key: walked(values) for key, values in values_of.items()}
for key in current_state_keys - snapshot_candidate_keys:
kept = cls._snapshot_reach.get(key)
if kept is not None and kept.capture is all_states.get(key):
reach[key] = kept
else:
values_of[key] = capture_values(key)
reach[key] = cls._snapshot_reach[key] = walked(values_of[key]).of(all_states[key])
shared = set().union(*(reach[key].identities for key in snapshot_candidate_keys))
clean = current_state_keys - snapshot_candidate_keys
joined = True
while joined:
joined = {key for key in clean if not reach[key].identities.isdisjoint(shared)}
clean -= joined
snapshot_candidate_keys |= joined
for key in joined:
shared |= reach[key].identities
captured = copy.deepcopy({
key: values_of[key] if key in values_of else capture_values(key)
for key in snapshot_candidate_keys
})
for key in snapshot_candidate_keys:
cls._snapshot_reach[key] = reach[key].of(captured[key])
for key in set(cls._snapshot_reach) - current_state_keys:
del cls._snapshot_reach[key]

with TimeTravelProfiler.phase(
"objectstate.snapshot_capture_states",
Expand Down Expand Up @@ -1967,6 +2010,8 @@ def time_travel_to_snapshot(cls, snapshot_id: str) -> bool:
}
cls._current_head = snapshot_id
cls._in_time_travel = True
# Restored scopes hold copies of the snapshot's values, not what they reached before.
cls._snapshot_reach.clear()
try:
with TimeTravelProfiler.phase(
"objectstate.reconcile_scopes",
Expand Down
39 changes: 39 additions & 0 deletions tests/test_construction_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,42 @@ def test_failed_attachment_rolls_back_complete_graph_and_loaded_history(tmp_path
assert path.read_bytes() == file_bytes




def test_a_snapshot_walks_only_the_scopes_it_captures():
"""An edit walks and copies the edited scope; an independent clean scope keeps its
capture and the objects it reached then, unwalked, while one that shares an object
with the edited scope is captured with it. Time travel forgets what scopes reached."""
shared = [1, 2]
edited = register(Config(items=shared), "edited")
register(Config(items=shared), "sharing")
independent = [register(Config(value=number), f"independent-{number}") for number in range(5)]
baseline = ObjectStateRegistry._snapshots[record("baseline")]
reached = {
state.scope_id: ObjectStateRegistry._snapshot_reach[state.scope_id] for state in independent
}
edited.update_parameter("value", 3)
after = ObjectStateRegistry.get_branch_history()[-1]
for state in independent:
assert after.all_states[state.scope_id] is baseline.all_states[state.scope_id]
assert ObjectStateRegistry._snapshot_reach[state.scope_id] is reached[state.scope_id]
assert after.all_states["sharing"] is not baseline.all_states["sharing"]
assert after.all_states["edited"].parameters["value"] == 3
assert ObjectStateRegistry.time_travel_to_snapshot(baseline.id)
assert ObjectStateRegistry._snapshot_reach == {}


def test_a_state_opened_under_saved_ancestors_resolves_its_saved_epoch_as_its_live_one():
"""Under ancestors holding their saved values, the saved epoch reads what the live one
reads: it is the live snapshot, copied, so a live value changed in place is dirty. An
ancestor with unsaved values gives the saved epoch its own resolution."""
parent = register(Config(value=4, items=[1]), "clean-parent")
child = register(LazyDataclassFactory.make_lazy_simple(Config)(), "clean-parent::child")
assert child._saved_resolved == child._live_resolved
assert child._saved_resolved["items"] is not child._live_resolved["items"]
parent.update_parameter("value", 5)
late = ObjectState(
LazyDataclassFactory.make_lazy_simple(Config)(), scope_id="clean-parent::late"
)
assert late._live_resolved["value"] == 5
assert late._saved_resolved["value"] == 4
Loading