Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
67fbece
Evaluate anchored recursions in full and drop values a cut reaches
MaxGhenis Oct 2, 2026
2ed6c34
Merge master into fix-spiral-anchored-recursion
MaxGhenis Oct 6, 2026
416852c
Fix the round-2 review's eight regressions in deferred spiral recursion
MaxGhenis Oct 6, 2026
8fed34a
Merge current master and preserve invalidation and trace contracts
MaxGhenis Oct 7, 2026
e0a11a2
Add bounded scheduler and exact-deletion differential coverage
MaxGhenis Oct 7, 2026
661926c
Clarify exact storage deletion documentation
MaxGhenis Oct 8, 2026
8d7ea9c
Ignore deleted and derived input anchors during recursion scheduling
MaxGhenis Oct 8, 2026
a644f93
Merge current master documentation deployment changes
MaxGhenis Oct 8, 2026
0b241a5
Keep bounded recurrence properties independent of generation timing
MaxGhenis Oct 8, 2026
f34b54c
Merge current master copy and pickle repairs
MaxGhenis Oct 8, 2026
e5455b8
Merge master into fix-spiral-anchored-recursion
MaxGhenis Oct 19, 2026
eac7ec5
Keep descendant explicit invalidations owned by their branch
MaxGhenis Oct 19, 2026
1d8e8e2
Preserve input and cut cache invariants across the master merge
MaxGhenis Oct 19, 2026
7bc9a28
Scope deferred invalidation to its input dependencies
MaxGhenis Oct 19, 2026
94c517a
Merge master into the anchored recursion branch
MaxGhenis Nov 30, 2026
c0616f8
Check recursion anchors on the frame owner
MaxGhenis Nov 30, 2026
6b20b99
Invalidate deferred results after exact input deletion
MaxGhenis Nov 30, 2026
71226a9
Keep cut results out of the macro cache
MaxGhenis Nov 30, 2026
20e33f2
Document exact input deletion
MaxGhenis Nov 30, 2026
3a07e8c
Preserve anchored recurrences across exact input deletion
MaxGhenis Dec 1, 2026
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
1 change: 1 addition & 0 deletions changelog.d/fix-spiral-anchored-recursion.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Evaluate finite anchored period recursions in full across tracing modes and cache policies, discard cut-derived values while preserving inputs and branch-owned invalidations, and refresh deferred results after an exact deletion removes an input.
19 changes: 19 additions & 0 deletions docs/python_api/data_storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

The `policyengine_core.data_storage` module contains the classes that are used to handle the storage of data in simulations.

Both storage classes provide `delete_exact(period, branch_name, derived_only)`
to remove a single stored period, leaving contained periods in place.
The scheduler uses `derived_only=True` to remove cut-derived results and keep
inputs. `Holder.delete_array` also clears the matching fast-cache entry and
storage sequence metadata. Removing a stored input clears the simulation's
fast cache and invalidates deferred results. Deleting an absent period leaves
the input state unchanged.

Each simulation keeps its own explicit invalidations. A parent can clear a
descendant's cut-derived values while the descendant's explicit invalidation
stays pending, even when both refer to the same key.

The period-recursion scheduler's temporary results are discarded after input
writes or computed-value invalidation. Retried calculations retain foreign
input dependencies, and repeated input changes use the existing calculation
retry limit; stable anchored chains have no scheduling count limit. ADD and
DIVIDE aggregates derived from a recursion cut are returned without being
cached, while inputs supplied during the calculation still take precedence.

## InMemoryStorage

```{eval-rst}
Expand Down
6 changes: 6 additions & 0 deletions docs/python_api/tracers.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ The `policyengine_core.tracers` module contains classes used to represent tracer

## FlatTrace

Flat traces retain the first completed calculation's dependencies and parameters
when a later request reads its cache. When the period-recursion scheduler retries
a calculation, a completed accepted result replaces an abandoned or provisional
cut result for the same variable, period, and branch. The full computation and
performance logs still contain the retry attempts.

```{eval-rst}
.. autoclass:: policyengine_core.tracers.flat_trace.FlatTrace
:members:
Expand Down
21 changes: 21 additions & 0 deletions policyengine_core/data_storage/in_memory_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,27 @@ def delete(self, period: Period = None, branch_name: str = "default") -> None:
self._unmark_dropped_keys()
self._forget_dropped_numbers()

def delete_exact(
self,
period: Period,
branch_name: str = "default",
derived_only: bool = False,
) -> None:
"""Drop the array stored for exactly ``period``.

Unlike ``delete``, this leaves the periods ``period`` contains. With
``derived_only``, an input stored there stays.
"""
if self.is_eternal:
period = periods.period(periods.ETERNITY)
key = f"{branch_name}:{periods.period(period)}"
if derived_only and key in self._inputs:
return
self._arrays.pop(key, None)
self._stop_sharing(key)
self._unmark_input(key)
self._sequence_numbers.pop(key, None)

def get_known_periods(self) -> list:
# Split on the first colon only: an anchored period's string form
# itself contains colons (e.g. "default:year:2027-11").
Expand Down
17 changes: 17 additions & 0 deletions policyengine_core/data_storage/on_disk_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,23 @@ def delete(self, period: Period = None, branch_name: str = "default") -> None:
}
self._forget_dropped_keys()

def delete_exact(
self,
period: Period,
branch_name: str = "default",
derived_only: bool = False,
) -> None:
"""Drop the file recorded for exactly ``period``. With
``derived_only``, an input stored there stays."""
if self.is_eternal:
period = periods.period(periods.ETERNITY)
key = f"{branch_name}_{periods.period(period)}"
if derived_only and key in self._files and key not in self._derived:
return
self._files.pop(key, None)
self._derived.discard(key)
self._sequence_numbers.pop(key, None)

def get_known_periods(self) -> list:
return [period for _, period in self.get_known_branch_periods()]

Expand Down
52 changes: 51 additions & 1 deletion policyengine_core/holders/holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def _stored_keys(self) -> Tuple[set, set]:
def _forget_deleted_inputs(
self, user_input_keys: set, stored_before: Tuple[set, set]
) -> None:
"""Drop the simulation's record of the inputs ``delete_arrays`` deleted.
"""Drop the simulation's record of the inputs removed from storage.

``_user_input_keys`` records each (variable, branch, period) stored
through ``set_input``, with the period as storage keys it (see
Expand Down Expand Up @@ -464,6 +464,55 @@ def _has_unnumbered_values(self) -> bool:
and self._disk_storage.has_unnumbered_values()
)

def delete_array(
self,
period: Period,
branch_name: str = "default",
derived_only: bool = False,
) -> None:
"""Remove the value stored for exactly ``period`` under ``branch_name``.

Leave contained periods in storage. Removing a stored input clears
this simulation's fast cache and invalidates deferred results.
Deleting an absent period leaves the input state unchanged. With
``derived_only=True``, clear only calculated values and keep inputs.
"""
simulation = getattr(self, "simulation", None)
user_input_keys = (
None if derived_only else getattr(simulation, "_user_input_keys", None)
)
stored_before = self._stored_keys() if user_input_keys else None
removes_input = (
simulation is not None
and not derived_only
and any(
storage is not None
and storage.has(period, branch_name)
and not storage.is_derived(period, branch_name)
for storage in (self._memory_storage, self._disk_storage)
)
)

self._memory_storage.delete_exact(period, branch_name, derived_only)
if self._disk_storage:
self._disk_storage.delete_exact(period, branch_name, derived_only)
self._evict_fast_cache(period, branch_name)

if user_input_keys:
self._forget_deleted_inputs(user_input_keys, stored_before)

if removes_input:
from policyengine_core.simulations.simulation import _note_input_change

# A fast-cache hit can bypass the deferred-result refresh, even
# for a result the holder never stored.
fast_cache = getattr(simulation, "_fast_cache", None)
if fast_cache is not None:
fast_cache.clear()
_note_input_change(
simulation, "delete", self.variable.name, period, branch_name
)

def _get_array_from_storage(
self, period: Period, branch_name: str = "default"
) -> ArrayLike:
Expand Down Expand Up @@ -882,6 +931,7 @@ def put_in_cache(
return

self._set(period, value, branch_name, is_input=False, derived=derived)
self.simulation._note_cached_value(self.variable.name, period)

def _record_store(self, period: Period, sequence_number: int) -> None:
simulation = getattr(self, "simulation", None)
Expand Down
Loading