Repository navigation
Capture root scope values in the atom cache so root overrides survive recompute - #213
Conversation
… 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>
There was a problem hiding this comment.
⚠️ 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/AtomCacheProtocolto storerootScopeValuesalongside optionalscopeValues. - Capture
rootScopeValueswhen initializing caches and restore it inStoreContext.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>
Summary
Scoped overrides were already captured in
AtomCache(viascopeValues), but root overrides (applied atAtomRootlevel) 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 afatalErrorfor 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
AtomRoothas 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:):AtomCachenow storesrootScopeValues: ScopeValues(non-optional — every atom is initialized under some root) alongside the optionalscopeValues(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/refreshkeep resolving overrides via the current context'slookupAtomKeyAndOverride, so overrides added later throughAtomTestContext.overrideare still picked up.Trade-offs
switchContextnow 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