-
Notifications
You must be signed in to change notification settings - Fork 329
feat(skip): add layout safety observation foundations #1672
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
Merged
james-elicx
merged 7 commits into
cloudflare:main
from
NathanDrake2406:nathan/726-skip-01-observation
May 31, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8f61b49
feat(skip): add layout safety observation foundations
NathanDrake2406 d9981ae
fix(skip): address review feedback on observation foundations
NathanDrake2406 c9de116
fix(skip): record cacheable-fetch observation synchronously before fi…
NathanDrake2406 0024089
fix(test): remove polluting fetch restore and catch background promise
NathanDrake2406 5d13b4d
Merge branch 'main' into nathan/726-skip-01-observation
NathanDrake2406 cae0cad
chore: empty commit to trigger CI
NathanDrake2406 159923c
chore: empty commit to trigger CI
NathanDrake2406 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
239 changes: 239 additions & 0 deletions
239
packages/vinext/src/server/app-layout-param-observation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,239 @@ | ||||||||||||||||||
| import type { ThenableParamsObserver } from "vinext/shims/thenable-params"; | ||||||||||||||||||
| import { | ||||||||||||||||||
| _peekRequestScopedCacheLife, | ||||||||||||||||||
| _peekUnstableCacheObservations, | ||||||||||||||||||
| type UnstableCacheObservation, | ||||||||||||||||||
| } from "vinext/shims/cache"; | ||||||||||||||||||
| import { | ||||||||||||||||||
| getCollectedFetchTags, | ||||||||||||||||||
| peekCacheableFetchObservations, | ||||||||||||||||||
| peekDynamicFetchObservations, | ||||||||||||||||||
| } from "vinext/shims/fetch-cache"; | ||||||||||||||||||
| import { peekRenderRequestApiUsage } from "vinext/shims/headers"; | ||||||||||||||||||
| import { | ||||||||||||||||||
| isInsideUnifiedScope, | ||||||||||||||||||
| runWithUnifiedStateMutation, | ||||||||||||||||||
| } from "vinext/shims/unified-request-context"; | ||||||||||||||||||
| import type { RenderRequestApiKind } from "./cache-proof.js"; | ||||||||||||||||||
|
|
||||||||||||||||||
| export type AppLayoutParamAccessObservation = Readonly<{ | ||||||||||||||||||
| cacheLifeObserved: boolean; | ||||||||||||||||||
| cacheTags: readonly string[]; | ||||||||||||||||||
| cacheableFetchCount: number; | ||||||||||||||||||
| completeness: "complete" | "unknown"; | ||||||||||||||||||
| dynamicFetchCount: number; | ||||||||||||||||||
| finiteRevalidateSeconds: number | null; | ||||||||||||||||||
| keys: readonly string[]; | ||||||||||||||||||
| observed: boolean; | ||||||||||||||||||
| paramScopeKeys: readonly string[]; | ||||||||||||||||||
| requestApis: readonly RenderRequestApiKind[]; | ||||||||||||||||||
| unstableCaches: readonly UnstableCacheObservation[]; | ||||||||||||||||||
| }>; | ||||||||||||||||||
|
|
||||||||||||||||||
| export type AppLayoutParamAccessTracker = Readonly<{ | ||||||||||||||||||
| createThenableParamsObserver: (layoutId: string) => ThenableParamsObserver; | ||||||||||||||||||
| getLayoutObservation: (layoutId: string) => AppLayoutParamAccessObservation; | ||||||||||||||||||
| recordLayoutFiniteRevalidate: (layoutId: string, revalidateSeconds: number) => void; | ||||||||||||||||||
| recordLayoutParamScope: (layoutId: string, paramScopeKeys: readonly string[]) => void; | ||||||||||||||||||
| runLayoutProbe: (layoutId: string, probe: () => unknown) => unknown; | ||||||||||||||||||
| }>; | ||||||||||||||||||
|
|
||||||||||||||||||
| export function isAppLayoutObservationUnsafeForStaticReuse( | ||||||||||||||||||
| observation: AppLayoutParamAccessObservation, | ||||||||||||||||||
| ): boolean { | ||||||||||||||||||
| return ( | ||||||||||||||||||
| observation.completeness !== "complete" || | ||||||||||||||||||
| observation.paramScopeKeys.length > 0 || | ||||||||||||||||||
| observation.observed || | ||||||||||||||||||
| observation.requestApis.length > 0 || | ||||||||||||||||||
| observation.finiteRevalidateSeconds !== null || | ||||||||||||||||||
| observation.cacheLifeObserved || | ||||||||||||||||||
| observation.cacheTags.length > 0 || | ||||||||||||||||||
| observation.cacheableFetchCount > 0 || | ||||||||||||||||||
| observation.dynamicFetchCount > 0 || | ||||||||||||||||||
| observation.unstableCaches.length > 0 | ||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| type MutableLayoutParamAccessObservation = { | ||||||||||||||||||
| cacheLifeObserved: boolean; | ||||||||||||||||||
| cacheTags: Set<string>; | ||||||||||||||||||
| cacheableFetches: Set<string>; | ||||||||||||||||||
| dynamicFetches: Set<string>; | ||||||||||||||||||
| finiteRevalidateSeconds: number | null; | ||||||||||||||||||
| keys: Set<string>; | ||||||||||||||||||
| observed: boolean; | ||||||||||||||||||
| paramScopeKeys: Set<string>; | ||||||||||||||||||
| probeComplete: boolean; | ||||||||||||||||||
| requestApis: Set<RenderRequestApiKind>; | ||||||||||||||||||
| unstableCaches: Map<string, UnstableCacheObservation>; | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| function isPromiseLike(value: unknown): value is PromiseLike<unknown> { | ||||||||||||||||||
| return Boolean( | ||||||||||||||||||
| value && | ||||||||||||||||||
| (typeof value === "object" || typeof value === "function") && | ||||||||||||||||||
| "then" in value && | ||||||||||||||||||
| typeof value.then === "function", | ||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| export function createAppLayoutParamAccessTracker(): AppLayoutParamAccessTracker { | ||||||||||||||||||
| const observations = new Map<string, MutableLayoutParamAccessObservation>(); | ||||||||||||||||||
|
|
||||||||||||||||||
| const ensureObservation = (layoutId: string): MutableLayoutParamAccessObservation => { | ||||||||||||||||||
| const existing = observations.get(layoutId); | ||||||||||||||||||
| if (existing) return existing; | ||||||||||||||||||
|
|
||||||||||||||||||
| const created: MutableLayoutParamAccessObservation = { | ||||||||||||||||||
| cacheLifeObserved: false, | ||||||||||||||||||
| cacheTags: new Set(), | ||||||||||||||||||
| cacheableFetches: new Set(), | ||||||||||||||||||
| dynamicFetches: new Set(), | ||||||||||||||||||
| finiteRevalidateSeconds: null, | ||||||||||||||||||
| keys: new Set(), | ||||||||||||||||||
| observed: false, | ||||||||||||||||||
| paramScopeKeys: new Set(), | ||||||||||||||||||
| probeComplete: false, | ||||||||||||||||||
| requestApis: new Set(), | ||||||||||||||||||
| unstableCaches: new Map(), | ||||||||||||||||||
| }; | ||||||||||||||||||
| observations.set(layoutId, created); | ||||||||||||||||||
| return created; | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| const markObserved = (layoutId: string, keys: readonly string[]) => { | ||||||||||||||||||
| const observation = ensureObservation(layoutId); | ||||||||||||||||||
| observation.observed = true; | ||||||||||||||||||
| for (const key of keys) { | ||||||||||||||||||
| observation.keys.add(key); | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| const markProbeComplete = (layoutId: string) => { | ||||||||||||||||||
| ensureObservation(layoutId).probeComplete = true; | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| const runWithIsolatedProbeDependencies = (probe: () => unknown): unknown => { | ||||||||||||||||||
| if (!isInsideUnifiedScope()) { | ||||||||||||||||||
| return probe(); | ||||||||||||||||||
| } | ||||||||||||||||||
| return runWithUnifiedStateMutation((ctx) => { | ||||||||||||||||||
| ctx.cacheableFetchUrls = new Set(); | ||||||||||||||||||
| ctx.currentRequestTags = []; | ||||||||||||||||||
| ctx.currentFetchSoftTags = []; | ||||||||||||||||||
| ctx.dynamicFetchUrls = new Set(); | ||||||||||||||||||
| ctx.dynamicUsageDetected = false; | ||||||||||||||||||
| ctx.renderRequestApiUsage = new Set(); | ||||||||||||||||||
| ctx.requestScopedCacheLife = null; | ||||||||||||||||||
| ctx.unstableCacheObservations = new Map(); | ||||||||||||||||||
| }, probe); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| const recordProbeDependencies = (layoutId: string) => { | ||||||||||||||||||
| const observation = ensureObservation(layoutId); | ||||||||||||||||||
| if (_peekRequestScopedCacheLife() !== null) { | ||||||||||||||||||
| observation.cacheLifeObserved = true; | ||||||||||||||||||
| } | ||||||||||||||||||
| for (const tag of getCollectedFetchTags()) { | ||||||||||||||||||
| observation.cacheTags.add(tag); | ||||||||||||||||||
| } | ||||||||||||||||||
| for (const url of peekCacheableFetchObservations()) { | ||||||||||||||||||
| observation.cacheableFetches.add(url); | ||||||||||||||||||
| } | ||||||||||||||||||
| for (const url of peekDynamicFetchObservations()) { | ||||||||||||||||||
| observation.dynamicFetches.add(url); | ||||||||||||||||||
| } | ||||||||||||||||||
| for (const requestApi of peekRenderRequestApiUsage()) { | ||||||||||||||||||
| observation.requestApis.add(requestApi); | ||||||||||||||||||
| } | ||||||||||||||||||
| for (const unstableCache of _peekUnstableCacheObservations()) { | ||||||||||||||||||
| observation.unstableCaches.set(unstableCache.keyHash, unstableCache); | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| return { | ||||||||||||||||||
| createThenableParamsObserver(layoutId) { | ||||||||||||||||||
| return { | ||||||||||||||||||
| observeParamAccess(keys) { | ||||||||||||||||||
| markObserved(layoutId, keys); | ||||||||||||||||||
| }, | ||||||||||||||||||
| }; | ||||||||||||||||||
| }, | ||||||||||||||||||
| getLayoutObservation(layoutId) { | ||||||||||||||||||
| const observation = observations.get(layoutId); | ||||||||||||||||||
| if (!observation) { | ||||||||||||||||||
| return { | ||||||||||||||||||
| cacheLifeObserved: false, | ||||||||||||||||||
| cacheTags: [], | ||||||||||||||||||
| cacheableFetchCount: 0, | ||||||||||||||||||
| completeness: "unknown", | ||||||||||||||||||
| dynamicFetchCount: 0, | ||||||||||||||||||
| finiteRevalidateSeconds: null, | ||||||||||||||||||
| keys: [], | ||||||||||||||||||
| observed: false, | ||||||||||||||||||
| paramScopeKeys: [], | ||||||||||||||||||
| requestApis: [], | ||||||||||||||||||
| unstableCaches: [], | ||||||||||||||||||
| }; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return { | ||||||||||||||||||
| cacheLifeObserved: observation.cacheLifeObserved, | ||||||||||||||||||
| cacheTags: [...observation.cacheTags].sort(), | ||||||||||||||||||
| cacheableFetchCount: observation.cacheableFetches.size, | ||||||||||||||||||
| completeness: observation.probeComplete ? "complete" : "unknown", | ||||||||||||||||||
| dynamicFetchCount: observation.dynamicFetches.size, | ||||||||||||||||||
| finiteRevalidateSeconds: observation.finiteRevalidateSeconds, | ||||||||||||||||||
| keys: [...observation.keys].sort(), | ||||||||||||||||||
| observed: observation.observed, | ||||||||||||||||||
| paramScopeKeys: [...observation.paramScopeKeys].sort(), | ||||||||||||||||||
| requestApis: [...observation.requestApis].sort(), | ||||||||||||||||||
| unstableCaches: [...observation.unstableCaches.values()].sort((a, b) => | ||||||||||||||||||
| a.keyHash.localeCompare(b.keyHash), | ||||||||||||||||||
| ), | ||||||||||||||||||
| }; | ||||||||||||||||||
| }, | ||||||||||||||||||
| recordLayoutFiniteRevalidate(layoutId, revalidateSeconds) { | ||||||||||||||||||
| if (!Number.isFinite(revalidateSeconds) || revalidateSeconds <= 0) return; | ||||||||||||||||||
| const observation = ensureObservation(layoutId); | ||||||||||||||||||
| observation.finiteRevalidateSeconds = | ||||||||||||||||||
| observation.finiteRevalidateSeconds === null | ||||||||||||||||||
| ? revalidateSeconds | ||||||||||||||||||
| : Math.min(observation.finiteRevalidateSeconds, revalidateSeconds); | ||||||||||||||||||
| }, | ||||||||||||||||||
| recordLayoutParamScope(layoutId, paramScopeKeys) { | ||||||||||||||||||
| const observation = ensureObservation(layoutId); | ||||||||||||||||||
| for (const key of paramScopeKeys) { | ||||||||||||||||||
| observation.paramScopeKeys.add(key); | ||||||||||||||||||
| } | ||||||||||||||||||
| }, | ||||||||||||||||||
| runLayoutProbe(layoutId, probe) { | ||||||||||||||||||
| return runWithIsolatedProbeDependencies(() => { | ||||||||||||||||||
| const result = probe(); | ||||||||||||||||||
| if (!isPromiseLike(result)) { | ||||||||||||||||||
| recordProbeDependencies(layoutId); | ||||||||||||||||||
| markProbeComplete(layoutId); | ||||||||||||||||||
| return result; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return Promise.resolve(result).then( | ||||||||||||||||||
| (resolved) => { | ||||||||||||||||||
| recordProbeDependencies(layoutId); | ||||||||||||||||||
| markProbeComplete(layoutId); | ||||||||||||||||||
| return resolved; | ||||||||||||||||||
| }, | ||||||||||||||||||
| (error: unknown) => { | ||||||||||||||||||
| // Record whatever dependencies we observed before the failure | ||||||||||||||||||
| // so the layout's dependency snapshot is as complete as possible. | ||||||||||||||||||
| // Deliberately do NOT call markProbeComplete here: a failed probe | ||||||||||||||||||
| // leaves completeness as "unknown", which makes the planner fall | ||||||||||||||||||
| // back to render-and-send — the safe default for any probe error. | ||||||||||||||||||
| recordProbeDependencies(layoutId); | ||||||||||||||||||
| throw error; | ||||||||||||||||||
|
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. Intentional that
Suggested change
|
||||||||||||||||||
| }, | ||||||||||||||||||
| ); | ||||||||||||||||||
| }); | ||||||||||||||||||
| }, | ||||||||||||||||||
| }; | ||||||||||||||||||
| } | ||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currentFetchSoftTagsis not reset here, unlikerunWithFetchCachewhich resets it alongsidecurrentRequestTagsanddynamicFetchUrls. This means soft tags from an outer scope leak into the isolated probe. Probably harmless now since probes don't issue real fetches, but it's an inconsistency with the other reset sites.