Skip to content

Capture root scope values in the atom cache so root overrides survive recompute - #213

Merged
ra1028 merged 2 commits into
mainfrom
capture-root-override-in-cache
Jun 17, 2026
Merged

ra1028 merged 2 commits into
mainfrom
capture-root-override-in-cache

Conversation

@ra1028

@ra1028 ra1028 commented Jun 17, 2026

Copy link
Copy Markdown
Owner

Summary

Scoped overrides were already captured in AtomCache (via scopeValues), but root overrides (applied at AtomRoot level) were not. As a result, recomputing an atom through a context that no longer carries the root override fell back to the atom's default value — which can be a fatalError for a required override.

This happens, for example, when an in-flight async value-creation transaction (or a transitive update) recomputes an atom and re-resolves an overridden dependency after AtomRoot has left the view tree. It does not happen in normal app usage, but can occur in setups such as UI snapshot tests.

Fix

Capture the root scope values in the cache symmetrically with scoped values, and restore them in switchContext(with:):

  • AtomCache now stores rootScopeValues: ScopeValues (non-optional — every atom is initialized under some root) alongside the optional scopeValues (an atom may not belong to a scope).
  • StoreContext.switchContext(with:) restores both from the cache, so recompute paths (reset, refresh, transitive update, release) resolve overrides through the scope captured at initialization, not the current context's.

reset/refresh keep resolving overrides via the current context's lookupAtomKeyAndOverride, so overrides added later through AtomTestContext.override are still picked up.

Trade-offs

  • Memory: the override containers are immutable after setup, so capturing the scope values shares their storage via copy-on-write — the overrides are not duplicated per cache (verified by a test comparing the captured arrays' buffer addresses).
  • Lifetime: a cache now retains the root override/observer closures until it is released — the same class of retention scoped overrides already incurred, now extended to root.
  • Observers: switchContext now sources root observers from the captured scope, consistent with how scoped observers already behave. This is invisible in the steady state (the captured root equals the current root).

Verification

Confirmed across all recompute paths (reset, refresh, transitive update, async transaction) that a root-overridden dependency resolves the override after the root no longer carries it; temporarily reverting the fix makes those paths fall back to the default, confirming the fix is responsible. Full library suite passes and was run 15× under parallel execution with no failures.

🤖 Generated with Claude Code

… recompute

Scoped overrides were already captured in `AtomCache` (via `scopeValues`),
but root overrides were not, so recomputing an atom through a context that no
longer carries the root override (e.g. an in-flight async transaction or a
transitive update after `AtomRoot` has left the view tree) fell back to the
atom's default value — which can be a `fatalError` for a required override.

Capture the root scope values in the cache symmetrically with scoped values
and restore them in `switchContext(with:)`:

- `AtomCache` now stores `rootScopeValues: ScopeValues` (non-optional, since
  every atom is initialized under some root) alongside the optional
  `scopeValues` (an atom may not belong to a scope).
- `StoreContext.switchContext(with:)` restores both from the cache, so
  recompute paths (reset, refresh, transitive update, release) resolve
  overrides through the scope captured at initialization.

The override containers are immutable after setup, so capturing the scope
values shares their storage via copy-on-write instead of duplicating the
overrides per cache.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 17, 2026 09:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Not ready to approve

A newly added test asserts Swift Array copy-on-write behavior via buffer address equality, which is brittle across toolchain/stdlib changes and should be made more robust.

Pull request overview

This PR fixes a subtle override-resolution bug by persisting root-level scope values in each atom cache so that recompute paths (reset/refresh/transitive update/async transactions) continue to resolve dependencies using the root overrides that were present at initialization time, even if the current context no longer carries them.

Changes:

  • Extend AtomCache / AtomCacheProtocol to store rootScopeValues alongside optional scopeValues.
  • Capture rootScopeValues when initializing caches and restore it in StoreContext.switchContext(with:).
  • Update/add tests to validate root override survival across recompute and to account for the new cache initializer shape.
File summaries
File Description
Sources/Atoms/Core/AtomCache.swift Adds rootScopeValues to the cache model/protocol and threads it through updates.
Sources/Atoms/Core/StoreContext.swift Captures root scope values into caches and restores them when switching contexts for recompute/update paths.
Tests/AtomsTests/Core/StoreContextTests.swift Adds coverage for root-override survival across recompute; updates cache construction in tests.
Tests/AtomsTests/Core/AtomCacheTests.swift Updates cache tests to include/validate rootScopeValues.
Tests/AtomsTests/Utilities/Utilities.swift Updates test-only AtomCache convenience init to supply a default rootScopeValues.

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 1

Note

Your feedback helps us improve the quality of this feature.
Please use 👍 or 👎 to tell us whether this assessment is correct.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

The test asserted Swift's Array copy-on-write behavior by comparing buffer
base addresses obtained from separate `withUnsafeBufferPointer` calls, which
relies on pointer stability outside the documented validity window and tests
a standard-library guarantee rather than this library's logic. Non-amplification
is already guaranteed structurally: the captured scope values are immutable and
never mutated after capture.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ra1028
ra1028 merged commit 545e1ac into main Jun 17, 2026
13 of 16 checks passed
@ra1028
ra1028 deleted the capture-root-override-in-cache branch June 17, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants