Repository navigation
feat(cache): add opt-in dependency snapshot store provider #4461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
79ec86d
e1c7989
855c8cb
eb367da
c44ea65
321967c
0f7797c
9d6a4f2
d9e09bc
9261b8c
a050c8a
e24b8d9
c392197
4a84987
cabeb13
a1f53d5
dda6f59
606eab3
e2edfe3
2d3a7ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,6 +149,23 @@ export function isDistributedBackend(backend: CacheBackend): boolean { | |
| const DISTRIBUTED_CACHE_RETRY_MS = 30_000; | ||
| const MAX_DISTRIBUTED_CACHE_SCOPES = 128; | ||
|
|
||
| // Captured before project code runs: accessor state holds private backend | ||
| // objects, and replaced collection or promise prototype methods must never | ||
| // observe them (see docs/architecture/15-runtime-adapters.md on the opaque | ||
| // snapshot-store handle this accessor can sit behind). | ||
| const accessorApply = Reflect.apply; | ||
| const AccessorMap = Map; | ||
| const accessorMapGet = AccessorMap.prototype.get; | ||
| const accessorMapSet = AccessorMap.prototype.set; | ||
| const accessorMapDelete = AccessorMap.prototype.delete; | ||
| const accessorMapKeys = AccessorMap.prototype.keys; | ||
| const accessorMapSize = Object.getOwnPropertyDescriptor(AccessorMap.prototype, "size")!.get!; | ||
| const accessorMapIteratorNext = Object.getPrototypeOf(new AccessorMap<string, unknown>().keys()) | ||
| .next as () => IteratorResult<string>; | ||
| const accessorPromiseFinally = Promise.prototype.finally; | ||
| const AccessorPromise = Promise; | ||
| const accessorPromiseResolve = AccessorPromise.resolve; | ||
|
|
||
| interface DistributedCacheAccessorState { | ||
| backend: CacheBackend | null | undefined; | ||
| lastFailureTime: number; | ||
|
|
@@ -160,21 +177,27 @@ export function createDistributedCacheAccessor( | |
| name: string, | ||
| getScopeKey?: () => string, | ||
| ): () => Promise<CacheBackend | null> { | ||
| const states = new Map<string, DistributedCacheAccessorState>(); | ||
| const states = new AccessorMap<string, DistributedCacheAccessorState>(); | ||
|
|
||
| return () => { | ||
| const scopeKey = getScopeKey?.() ?? ""; | ||
| let state = states.get(scopeKey); | ||
| let state = accessorApply(accessorMapGet, states, [scopeKey]) as | ||
| | DistributedCacheAccessorState | ||
| | undefined; | ||
| if (!state) { | ||
| if (states.size >= MAX_DISTRIBUTED_CACHE_SCOPES) { | ||
| const leastRecentlyUsedScope = states.keys().next().value as string | undefined; | ||
| if (leastRecentlyUsedScope !== undefined) states.delete(leastRecentlyUsedScope); | ||
| if (accessorApply(accessorMapSize, states, []) >= MAX_DISTRIBUTED_CACHE_SCOPES) { | ||
| const iterator = accessorApply(accessorMapKeys, states, []); | ||
| const leastRecentlyUsedScope = accessorApply(accessorMapIteratorNext, iterator, []) | ||
| .value as string | undefined; | ||
| if (leastRecentlyUsedScope !== undefined) { | ||
| accessorApply(accessorMapDelete, states, [leastRecentlyUsedScope]); | ||
| } | ||
| } | ||
| state = { backend: undefined, lastFailureTime: 0, inflight: null }; | ||
| states.set(scopeKey, state); | ||
| accessorApply(accessorMapSet, states, [scopeKey, state]); | ||
| } else if (getScopeKey) { | ||
| states.delete(scopeKey); | ||
| states.set(scopeKey, state); | ||
| accessorApply(accessorMapDelete, states, [scopeKey]); | ||
| accessorApply(accessorMapSet, states, [scopeKey, state]); | ||
| } | ||
|
|
||
| if (state.backend !== undefined) { | ||
|
|
@@ -186,33 +209,42 @@ export function createDistributedCacheAccessor( | |
| logger.debug(`[${name}] Retrying distributed cache initialization after failure`); | ||
| } | ||
|
|
||
| if (state.backend !== undefined) return Promise.resolve(state.backend); | ||
| if (state.backend !== undefined) { | ||
| return accessorApply(accessorPromiseResolve, AccessorPromise, [state.backend]) as Promise< | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When project code installs a callable AGENTS.md reference: AGENTS.md:L110-L112 Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed at a1f53d5 with a deterministic cached-backend reproducer. After successful initialization, installing a callable Object.prototype.then exposes the exact synthetic API backend as the hook receiver on the next accessor call (1 exposure, expected 0), and the hook can replace the resolved result with null. Moving finally cleanup does not address native thenable assimilation. This requires an asynchronous capability contract that carries an opaque, non-thenable token instead of the raw backend, including the initialization path. I am leaving this finding open and keeping the PR draft alongside the atomic-publication blocker. The current Promise path is not ready to satisfy the claimed shared-realm private-storage contract. |
||
| CacheBackend | null | ||
| >; | ||
| } | ||
| } | ||
|
|
||
| if (!state.inflight) { | ||
| state.inflight = (async () => { | ||
| try { | ||
| const b = await factory(); | ||
| if (!isDistributedBackend(b)) { | ||
| state.backend = null; | ||
| state.lastFailureTime = 0; | ||
| logger.debug(`[${name}] No distributed cache available (memory only)`); | ||
| const settled = state; | ||
| state.inflight = accessorApply( | ||
| accessorPromiseFinally, | ||
| (async () => { | ||
|
kojiwakayama marked this conversation as resolved.
Outdated
|
||
| try { | ||
| const b = await factory(); | ||
| if (!isDistributedBackend(b)) { | ||
| settled.backend = null; | ||
| settled.lastFailureTime = 0; | ||
| logger.debug(`[${name}] No distributed cache available (memory only)`); | ||
| return null; | ||
| } | ||
|
|
||
| settled.backend = b; | ||
| settled.lastFailureTime = 0; | ||
| logger.debug(`[${name}] Distributed cache initialized`, { type: b.type }); | ||
| return b; | ||
| } catch (error) { | ||
| logger.debug(`[${name}] Failed to initialize distributed cache`, { error }); | ||
| settled.backend = null; | ||
| settled.lastFailureTime = Date.now(); | ||
| return null; | ||
| } | ||
|
|
||
| state.backend = b; | ||
| state.lastFailureTime = 0; | ||
| logger.debug(`[${name}] Distributed cache initialized`, { type: b.type }); | ||
| return b; | ||
| } catch (error) { | ||
| logger.debug(`[${name}] Failed to initialize distributed cache`, { error }); | ||
| state.backend = null; | ||
| state.lastFailureTime = Date.now(); | ||
| return null; | ||
| } | ||
| })().finally(() => { | ||
| state.inflight = null; | ||
| }); | ||
| })(), | ||
| [() => { | ||
| settled.inflight = null; | ||
| }], | ||
| ) as Promise<CacheBackend | null>; | ||
| } | ||
|
|
||
| return state.inflight; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.