Unverified Commit f1e80752 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Add analyzer load scope evidence

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent be41d9bb
......@@ -13,6 +13,8 @@ const COMPRESSED_CAVEAT =
'Compressed source sizes are estimates because attributed parts are compressed independently.'
const ENTRY_HEURISTIC_CAVEAT =
'Route entries are detected heuristically from internal Next.js module identifiers.'
const LOAD_SCOPE_CAVEAT =
'Load scopes and worker detection are graph heuristics, not exact initial-transfer bytes or runtime trigger timing.'
type Environment = 'total' | 'client' | 'server'
type Metric = 'raw' | 'compressed'
......@@ -26,6 +28,24 @@ type OutputKind =
| 'media'
| 'wasm'
| 'other'
type LoadScope = 'initial' | 'async' | 'traced' | 'asset' | 'unknown'
const LOAD_SCOPE_ORDER: LoadScope[] = [
'initial',
'async',
'traced',
'asset',
'unknown',
]
type WorkerEvidence = { detected: boolean; heuristic: true }
type Reachability = {
modules: Map<
number,
{ scopes: Set<'initial' | 'async' | 'traced'>; worker: boolean }
>
truncated: boolean
}
interface Pagination {
offset: number
......@@ -46,6 +66,8 @@ interface SourceRow {
traced: boolean
chunkNames: string[]
chunkCount: number
loadScopes: LoadScope[]
worker: WorkerEvidence
}
interface DiffRow {
......@@ -61,6 +83,9 @@ interface DiffRow {
interface DiffInput extends Omit<DiffRow, 'status' | 'delta'> {
baselinePresent: boolean
comparisonPresent: boolean
baselineLoadScopes?: LoadScope[]
comparisonLoadScopes?: LoadScope[]
scopeTransitions?: Array<'initial-to-async' | 'async-to-initial'>
}
interface OverviewArgs {
......@@ -78,6 +103,7 @@ interface SourcesArgs {
search?: string
environment?: Environment
fileTypes?: FileType[]
loadScopes?: LoadScope[]
groupBy?: 'source' | 'package'
metric?: Metric
offset?: number
......@@ -148,6 +174,7 @@ interface CompareArgs {
environment?: Environment
metric?: Metric
search?: string
scopeTransition?: 'initial-to-async' | 'async-to-initial'
offset?: number
limit?: number
}
......@@ -161,14 +188,6 @@ class DetailedToolError extends Error {
}
}
export class AnalyzeQueryError extends Error {
constructor(readonly output: Record<string, unknown>) {
super(
typeof output.error === 'string' ? output.error : 'Analyzer query failed'
)
}
}
function publicError(error: unknown): Record<string, unknown> {
if (error instanceof DetailedToolError) {
return { error: error.message, ...error.details }
......@@ -180,8 +199,9 @@ function publicError(error: unknown): Record<string, unknown> {
}
if (error.message.startsWith('Unknown route'))
return { error: 'Unknown route' }
if (error.message.startsWith('Unknown source'))
if (error.message.startsWith('Unknown source')) {
return { error: 'Unknown source' }
}
if (
/^(?:Invalid |moduleIdent |route is required|Route comparisons)/.test(
error.message
......@@ -192,6 +212,14 @@ function publicError(error: unknown): Record<string, unknown> {
return { error: 'Unable to query analyzer data' }
}
export class AnalyzeQueryError extends Error {
constructor(readonly output: Record<string, unknown>) {
super(
typeof output.error === 'string' ? output.error : 'Analyzer query failed'
)
}
}
function safeTool<T, R>(fn: (args: T) => Promise<R>) {
return async (args: T) => {
try {
......@@ -293,6 +321,30 @@ function outputSizes(data: AnalyzeData, outputIndex: number) {
return { rawSize, compressedSize }
}
function estimatedInitialClientSizes(
data: AnalyzeData,
modules: ModulesData,
reachability: Reachability
) {
let rawSize = 0
let compressedSize = 0
for (let sourceIndex = 0; sourceIndex < data.sourceCount(); sourceIndex++) {
const flags = data.getSourceFlags(sourceIndex)
if (!flags.client) continue
const sourcePath = data.getFullSourcePath(sourceIndex)
const initiallyReachable = modules
.getModuleIndiciesFromPath(sourcePath)
.some((moduleIndex) =>
reachability.modules.get(moduleIndex)?.scopes.has('initial')
)
if (!initiallyReachable) continue
const sizes = sourceSizes(data, sourceIndex, 'client')
rawSize += sizes.rawSize
compressedSize += sizes.compressedSize
}
return { rawSize, compressedSize }
}
function packageNameFromPath(sourcePath: string): string | undefined {
const marker = 'node_modules/'
const start = sourcePath.lastIndexOf(marker)
......@@ -317,7 +369,10 @@ function collectSources(
search?: string
environment: Environment
fileTypes?: FileType[]
loadScopes?: LoadScope[]
groupBy: 'source' | 'package'
modules?: ModulesData
reachability?: Reachability
}
): SourceRow[] {
const search = options.search?.toLocaleLowerCase()
......@@ -339,6 +394,21 @@ function collectSources(
) {
continue
}
const loadEvidence =
options.modules && options.reachability
? sourceLoadEvidence(data, index, options.modules, options.reachability)
: {
loadScopes: ['unknown'] as LoadScope[],
worker: { detected: false, heuristic: true as const },
}
if (
options.loadScopes?.length &&
!options.loadScopes.some((scope) =>
loadEvidence.loadScopes.includes(scope)
)
) {
continue
}
const sizes = sourceSizes(data, index, options.environment)
if (sizes.rawSize === 0 && sizes.compressedSize === 0) continue
const packageName = packageNameFromPath(sourcePath)
......@@ -352,6 +422,12 @@ function collectSources(
existing.server ||= flags.server
existing.traced ||= flags.traced
existing.chunkNames.push(...data.sourceChunks(index))
existing.loadScopes = LOAD_SCOPE_ORDER.filter(
(scope) =>
existing.loadScopes.includes(scope) ||
loadEvidence.loadScopes.includes(scope)
)
existing.worker.detected ||= loadEvidence.worker.detected
} else {
rows.set(key, {
key,
......@@ -364,6 +440,7 @@ function collectSources(
traced: flags.traced,
chunkNames: data.sourceChunks(index),
chunkCount: 0,
...loadEvidence,
})
}
}
......@@ -487,29 +564,118 @@ function resolveRouteEntries(
}
}
function routeModules(
function looksLikeWorker(value: string): boolean {
return /(?:^|[/_.-])worker(?:[/_.-]|$)/i.test(value)
}
function routeReachability(
modules: ModulesData,
entries: Set<number>
): { modules: Set<number>; truncated: boolean } {
const reachable = new Set(entries)
const queue = [...entries]
while (queue.length) {
): Reachability {
type State = {
index: number
async: boolean
traced: boolean
worker: boolean
}
const result: Reachability = { modules: new Map(), truncated: false }
const queue: State[] = []
const seen = new Set<string>()
function add(state: State): boolean {
const key = `${state.index}:${+state.async}:${+state.traced}:${+state.worker}`
if (seen.has(key)) return true
if (seen.size >= MAX_GRAPH_NODES) {
result.truncated = true
return false
}
seen.add(key)
queue.push(state)
const evidence = result.modules.get(state.index) ?? {
scopes: new Set<'initial' | 'async' | 'traced'>(),
worker: false,
}
if (!state.async && !state.traced) evidence.scopes.add('initial')
if (state.async) evidence.scopes.add('async')
if (state.traced) evidence.scopes.add('traced')
evidence.worker ||= state.worker
result.modules.set(state.index, evidence)
return true
}
for (const index of [...entries].sort((a, b) => a - b)) {
const module = modules.module(index)
add({
index,
async: false,
traced: false,
worker: module
? looksLikeWorker(module.path) || looksLikeWorker(module.ident)
: false,
})
}
while (queue.length && !result.truncated) {
const current = queue.shift()!
const dependencies = [
...modules.moduleDependencies(current),
...modules.asyncModuleDependencies(current),
...modules.tracedModuleDependencies(current),
]
for (const dependency of dependencies) {
if (reachable.has(dependency)) continue
if (reachable.size >= MAX_GRAPH_NODES) {
return { modules: reachable, truncated: true }
const edges: Array<[number, 'sync' | 'async' | 'traced']> = [
...modules
.moduleDependencies(current.index)
.map((index) => [index, 'sync'] as [number, 'sync']),
...modules
.asyncModuleDependencies(current.index)
.map((index) => [index, 'async'] as [number, 'async']),
...modules
.tracedModuleDependencies(current.index)
.map((index) => [index, 'traced'] as [number, 'traced']),
].sort((a, b) => a[0] - b[0] || compareText(a[1], b[1]))
for (const [index, edgeKind] of edges) {
const module = modules.module(index)
if (!module) continue
if (
!add({
index,
async: current.async || edgeKind === 'async',
traced: current.traced || edgeKind === 'traced',
worker:
current.worker ||
looksLikeWorker(module.path) ||
looksLikeWorker(module.ident),
})
) {
break
}
reachable.add(dependency)
queue.push(dependency)
}
}
return { modules: reachable, truncated: false }
return result
}
function sourceLoadEvidence(
data: AnalyzeData,
sourceIndex: number,
modules: ModulesData,
reachability: Reachability
): { loadScopes: LoadScope[]; worker: WorkerEvidence } {
const scopes = new Set<LoadScope>()
const flags = data.getSourceFlags(sourceIndex)
if (flags.asset) scopes.add('asset')
let worker = false
const sourcePath = data.getFullSourcePath(sourceIndex)
if (sourcePath) {
const candidates = modules
.getModuleIndiciesFromPath(sourcePath)
.slice(0, MAX_MODULE_CANDIDATES)
for (const index of candidates) {
const evidence = reachability.modules.get(index)
if (!evidence) continue
for (const scope of evidence.scopes) scopes.add(scope)
worker ||= evidence.worker
}
}
if (scopes.size === 0) scopes.add('unknown')
return {
loadScopes: LOAD_SCOPE_ORDER.filter((scope) => scopes.has(scope)),
worker: { detected: worker, heuristic: true },
}
}
function moduleMatchesEnvironment(
......@@ -1075,6 +1241,43 @@ function selectModuleCandidate(
return selected
}
function provenance(metadata: SnapshotMetadata) {
return {
id: metadata.id,
createdAt: metadata.createdAt,
baselineName: metadata.baselineName,
nextVersion: metadata.nextVersion,
gitBranch: metadata.gitBranch,
gitSha: metadata.gitSha,
gitDirty: metadata.gitDirty,
worktreeFingerprint: metadata.worktreeFingerprint,
analysisFingerprint: metadata.analysisFingerprint,
routeCount: metadata.routeCount,
}
}
function loadScopeTransitions(
baseline: LoadScope[] | undefined,
comparison: LoadScope[] | undefined
): Array<'initial-to-async' | 'async-to-initial'> {
const result: Array<'initial-to-async' | 'async-to-initial'> = []
if (
baseline?.includes('initial') &&
!comparison?.includes('initial') &&
comparison?.includes('async')
) {
result.push('initial-to-async')
}
if (
baseline?.includes('async') &&
!baseline?.includes('initial') &&
comparison?.includes('initial')
) {
result.push('async-to-initial')
}
return result
}
function diffStatus(
inBaseline: boolean,
inComparison: boolean,
......@@ -1140,21 +1343,6 @@ function summarizeDiff(
}
}
function provenance(metadata: SnapshotMetadata) {
return {
id: metadata.id,
createdAt: metadata.createdAt,
baselineName: metadata.baselineName,
nextVersion: metadata.nextVersion,
gitBranch: metadata.gitBranch,
gitSha: metadata.gitSha,
gitDirty: metadata.gitDirty,
worktreeFingerprint: metadata.worktreeFingerprint,
analysisFingerprint: metadata.analysisFingerprint,
routeCount: metadata.routeCount,
}
}
const pagingSchema = {
offset: z
.number()
......@@ -1185,6 +1373,13 @@ const environmentSchema = z
.enum(['total', 'client', 'server'])
.optional()
.describe('Attribution environment. Default: `total`.')
const loadScopeSchema = z.enum([
'initial',
'async',
'traced',
'asset',
'unknown',
])
function jsonField(input: z.ZodTypeAny): {
schema: Record<string, unknown>
......@@ -1328,8 +1523,16 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
},
example: { environment: 'client', limit: 10 },
collection: 'routes',
selectableFields: ['route', 'rawSize', 'compressedSize'],
caveats: [COMPRESSED_CAVEAT],
selectableFields: [
'route',
'rawSize',
'compressedSize',
'estimatedInitialClientRawSize',
'estimatedInitialClientCompressedSize',
'routeEntryDetection',
'initialGraphTruncated',
],
caveats: [COMPRESSED_CAVEAT, LOAD_SCOPE_CAVEAT],
},
safeTool(async (args: OverviewArgs) => {
const snapshot = await repository.getSnapshot(args.snapshot)
......@@ -1338,13 +1541,22 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
const metric = args.metric ?? 'raw'
const filter = args.routeFilter?.toLocaleLowerCase()
const rows = []
const modules = await repository.loadModules(args.snapshot)
for (const route of snapshot.routes) {
if (filter && !route.toLocaleLowerCase().includes(filter)) continue
const sizes = routeSizes(
await repository.loadRoute(args.snapshot, route),
environment
)
rows.push({ route, ...sizes })
const data = await repository.loadRoute(args.snapshot, route)
const sizes = routeSizes(data, environment)
const entryResolution = resolveRouteEntries(modules, data)
const reachability = routeReachability(modules, entryResolution.entries)
const initial = estimatedInitialClientSizes(data, modules, reachability)
rows.push({
route,
...sizes,
estimatedInitialClientRawSize: initial.rawSize,
estimatedInitialClientCompressedSize: initial.compressedSize,
routeEntryDetection: { heuristic: entryResolution.heuristic },
initialGraphTruncated: reachability.truncated,
})
}
rows.sort(
(a, b) =>
......@@ -1366,7 +1578,7 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
},
routes: paged.values,
pagination: paged.pagination,
caveats: [COMPRESSED_CAVEAT],
caveats: [COMPRESSED_CAVEAT, LOAD_SCOPE_CAVEAT],
}
})
)
......@@ -1385,6 +1597,11 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
.array(z.enum(['js', 'css', 'json', 'asset']))
.max(4)
.optional(),
loadScopes: z
.array(loadScopeSchema)
.max(5)
.optional()
.describe('Keep sources matching at least one heuristic load scope.'),
groupBy: z
.enum(['source', 'package'])
.optional()
......@@ -1395,6 +1612,7 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
example: {
route: '/',
environment: 'client',
loadScopes: ['initial'],
groupBy: 'package',
limit: 20,
},
......@@ -1409,18 +1627,27 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
'server',
'traced',
'chunkCount',
'loadScopes',
'worker',
],
caveats: [COMPRESSED_CAVEAT],
caveats: [COMPRESSED_CAVEAT, LOAD_SCOPE_CAVEAT],
},
safeTool(async (args: SourcesArgs) => {
const environment = args.environment ?? 'total'
const metric = args.metric ?? 'raw'
const data = await repository.loadRoute(args.snapshot, args.route)
const modules = await repository.loadModules(args.snapshot)
const entryResolution = resolveRouteEntries(modules, data)
const reachability = routeReachability(modules, entryResolution.entries)
const rows = sortBySize(
collectSources(await repository.loadRoute(args.snapshot, args.route), {
collectSources(data, {
search: args.search,
environment,
fileTypes: args.fileTypes,
loadScopes: args.loadScopes,
groupBy: args.groupBy ?? 'source',
modules,
reachability,
}),
metric
)
......@@ -1435,7 +1662,12 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
groupBy: args.groupBy ?? 'source',
sources: paged.values.map(({ chunkNames: _chunkNames, ...row }) => row),
pagination: paged.pagination,
caveats: [COMPRESSED_CAVEAT],
routeEntryDetection: { heuristic: entryResolution.heuristic },
loadScopeDetection: {
heuristic: entryResolution.heuristic,
graphTruncated: reachability.truncated,
},
caveats: [COMPRESSED_CAVEAT, LOAD_SCOPE_CAVEAT],
}
})
)
......@@ -1471,9 +1703,8 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
safeTool(async (args: SourceChunksArgs) => {
const data = await repository.loadRoute(args.snapshot, args.route)
const sourceIndex = data.getSourceIndexFromPath(args.sourcePath)
if (sourceIndex === undefined) {
if (sourceIndex === undefined)
throw new Error(`Unknown source: ${args.sourcePath}`)
}
const byOutput = new Map<
number,
{ rawSize: number; compressedSize: number }
......@@ -1710,8 +1941,9 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
const asset = data.outputFile(assetIndex)
if (!asset) continue
const kind = outputKind(asset.filename)
if (kind !== 'font' && kind !== 'image' && kind !== 'media')
if (kind !== 'font' && kind !== 'image' && kind !== 'media') {
continue
}
if (!kinds.includes(kind)) continue
rows.push({
key: `${css.filename}\0${asset.filename}`,
......@@ -1719,9 +1951,9 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
assetFilename: asset.filename,
assetKind: kind,
...outputSizes(data, assetIndex),
relationshipEvidence: 'output-reference',
emissionEvidence: 'emitted',
requestEvidence: 'unknown',
relationshipEvidence: 'output-reference' as const,
emissionEvidence: 'emitted' as const,
requestEvidence: 'unknown' as const,
})
}
}
......@@ -1763,7 +1995,7 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
'explain_route_module',
{
description:
'Explain one source contribution and return a bounded importer chain toward an exact route entry.',
'Explain one source contribution and return a bounded importer chain toward a heuristic route entry.',
inputSchema: {
route: z.string().max(4096),
sourcePath: z.string().max(4096),
......@@ -1781,7 +2013,7 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
.describe('Maximum importer-chain depth. Default: 25.'),
},
example: { route: '/', sourcePath: '[project]/src/app/page.tsx' },
caveats: [COMPRESSED_CAVEAT, ENTRY_HEURISTIC_CAVEAT],
caveats: [COMPRESSED_CAVEAT, ENTRY_HEURISTIC_CAVEAT, LOAD_SCOPE_CAVEAT],
},
safeTool(async (args: ExplainArgs) => {
const environment = args.environment ?? 'total'
......@@ -1824,8 +2056,14 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
args.routeEntryId
)
const entries = entryResolution.entries
const routeGraph = routeModules(modules, entries)
const reachable = routeGraph.modules
const reachability = routeReachability(modules, entries)
const loadEvidence = sourceLoadEvidence(
data,
sourceIndex,
modules,
reachability
)
const reachable = new Set(reachability.modules.keys())
const importerCandidates = selected
? [
...modules.moduleDependents(selected.index),
......@@ -1864,7 +2102,7 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
entries,
reachable,
args.maxDepth ?? 25,
routeGraph.truncated,
reachability.truncated,
selectedImporter?.index
)
: undefined
......@@ -1894,12 +2132,17 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
selectedImporter: selectedImporter?.module,
importerChain,
...pathEvidence,
...loadEvidence,
loadScopeDetection: {
heuristic: entryResolution.heuristic,
graphTruncated: reachability.truncated,
},
routeEntries: entryResolution.candidates.map(
({ index: _index, ...entry }) => entry
),
selectedRouteEntryId: args.routeEntryId,
routeEntryDetection: { heuristic: entryResolution.heuristic },
caveats: [COMPRESSED_CAVEAT, ENTRY_HEURISTIC_CAVEAT],
caveats: [COMPRESSED_CAVEAT, ENTRY_HEURISTIC_CAVEAT, LOAD_SCOPE_CAVEAT],
}
return response
})
......@@ -1921,7 +2164,7 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
),
},
example: { route: '/', sourcePath: '[project]/src/app/page.tsx' },
caveats: [COMPRESSED_CAVEAT],
caveats: [COMPRESSED_CAVEAT, LOAD_SCOPE_CAVEAT],
},
safeTool(async (args: InitialGraphArgs) => {
const environment = args.environment ?? 'client'
......@@ -2006,7 +2249,7 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
sccs: analysis.sccs,
sccEvidence: analysis.sccEvidence,
},
caveats: [COMPRESSED_CAVEAT],
caveats: [COMPRESSED_CAVEAT, LOAD_SCOPE_CAVEAT],
}
})
)
......@@ -2050,7 +2293,7 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
'compressedSize',
'sourceCount',
],
caveats: [COMPRESSED_CAVEAT],
caveats: [COMPRESSED_CAVEAT, LOAD_SCOPE_CAVEAT],
},
safeTool(async (args: ImportEdgeArgs) => {
const environment = args.environment ?? 'client'
......@@ -2162,7 +2405,7 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
snapshot: provenance(
(await repository.getSnapshot(args.snapshot)).metadata
),
caveats: [COMPRESSED_CAVEAT],
caveats: [COMPRESSED_CAVEAT, LOAD_SCOPE_CAVEAT],
}
})
)
......@@ -2188,6 +2431,10 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
environment: environmentSchema,
metric: metricSchema,
search: z.string().max(1000).optional(),
scopeTransition: z
.enum(['initial-to-async', 'async-to-initial'])
.optional()
.describe('Source-only load-scope movement filter.'),
...pagingSchema,
},
example: {
......@@ -2202,10 +2449,13 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
'comparisonRawSize',
'baselineCompressedSize',
'comparisonCompressedSize',
'baselineLoadScopes',
'comparisonLoadScopes',
'scopeTransitions',
'status',
'delta',
],
caveats: [COMPRESSED_CAVEAT],
caveats: [COMPRESSED_CAVEAT, LOAD_SCOPE_CAVEAT],
},
safeTool(async (args: CompareArgs) => {
const granularity = args.granularity ?? 'route'
......@@ -2218,6 +2468,11 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
'Invalid route: route is only supported for source/package comparisons'
)
}
if (args.scopeTransition && granularity !== 'source') {
throw new Error(
'scopeTransition is only supported at source granularity'
)
}
const listed = await repository.listSnapshots()
const seen = new Set<string>()
......@@ -2300,21 +2555,53 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
if (!inBaseline && !inComparison) {
throw new Error(`Unknown route: ${args.route}`)
}
const baselineRows = new Map(
(inBaseline
? collectSources(
await repository.loadRoute(args.baselineSnapshot, args.route),
{ search: args.search, environment, groupBy }
const baselineData = inBaseline
? await repository.loadRoute(args.baselineSnapshot, args.route)
: undefined
const comparisonData = inComparison
? await repository.loadRoute(args.comparisonSnapshot, args.route)
: undefined
const baselineModules = inBaseline
? await repository.loadModules(args.baselineSnapshot)
: undefined
const comparisonModules = inComparison
? await repository.loadModules(args.comparisonSnapshot)
: undefined
const baselineReachability =
baselineData && baselineModules
? routeReachability(
baselineModules,
resolveRouteEntries(baselineModules, baselineData).entries
)
: undefined
const comparisonReachability =
comparisonData && comparisonModules
? routeReachability(
comparisonModules,
resolveRouteEntries(comparisonModules, comparisonData).entries
)
: undefined
const baselineRows = new Map(
(baselineData
? collectSources(baselineData, {
search: args.search,
environment,
groupBy,
modules: baselineModules,
reachability: baselineReachability,
})
: []
).map((row) => [row.key, row])
)
const comparisonRows = new Map(
(inComparison
? collectSources(
await repository.loadRoute(args.comparisonSnapshot, args.route),
{ search: args.search, environment, groupBy }
)
(comparisonData
? collectSources(comparisonData, {
search: args.search,
environment,
groupBy,
modules: comparisonModules,
reachability: comparisonReachability,
})
: []
).map((row) => [row.key, row])
)
......@@ -2331,8 +2618,23 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
comparisonRawSize: b?.rawSize ?? 0,
baselineCompressedSize: a?.compressedSize ?? 0,
comparisonCompressedSize: b?.compressedSize ?? 0,
...(granularity === 'source'
? {
baselineLoadScopes: a?.loadScopes ?? [],
comparisonLoadScopes: b?.loadScopes ?? [],
scopeTransitions: loadScopeTransitions(
a?.loadScopes,
b?.loadScopes
),
}
: {}),
}
})
if (args.scopeTransition) {
rows = rows.filter((row) =>
row.scopeTransitions?.includes(args.scopeTransition!)
)
}
}
const worktreeEvidenceAvailable =
baseline.metadata.worktreeFingerprint !== undefined &&
......@@ -2385,11 +2687,14 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
environment,
metric,
...(args.search ? { search: args.search } : {}),
...(args.scopeTransition
? { scopeTransition: args.scopeTransition }
: {}),
offset,
limit,
},
...summarizeDiff(rows, metric, offset, limit),
caveats: [COMPRESSED_CAVEAT],
caveats: [COMPRESSED_CAVEAT, LOAD_SCOPE_CAVEAT],
}
})
)
......
export const lazyValue = 'async'
'use client'
import { useState } from 'react'
import './styles.css'
import { syncValue } from './sync'
export default function Page() {
return <div>Hello World</div>
const [value, setValue] = useState(syncValue)
async function loadDeferred() {
const [{ lazyValue }, { workerValue }, { default: Image }] =
await Promise.all([
import('./lazy'),
import('./report.worker'),
import('next/image'),
])
setValue(`${lazyValue}:${workerValue}:${Image.displayName}`)
}
return <button onClick={loadDeferred}>{value}</button>
}
export const workerValue = 'worker'
export const syncValue = 'initial'
......@@ -92,7 +92,12 @@ describe('next experimental-analyze', () => {
for (const name of [
'get_app_overview',
'get_route_modules',
'get_source_chunks',
'get_route_outputs',
'get_css_assets',
'explain_route_module',
'get_initial_import_graph',
'analyze_import_edge',
'compare_bundles',
]) {
expect(help.stdout).toContain(
......@@ -106,6 +111,7 @@ describe('next experimental-analyze', () => {
expect(help.stdout).not.toContain('Example input:')
expect(help.stdout).not.toContain('"required": [')
expect(help.stdout).not.toContain('"route": "/"')
expect(help.stdout).not.toContain('"loadScopes"')
const noName = await next.runCommand(['experimental-analyze', 'query'])
expect(noName.exitCode).toBe(0)
......@@ -125,8 +131,13 @@ describe('next experimental-analyze', () => {
)
expect(queryHelp.stdout).toContain('Input schema:')
expect(queryHelp.stdout).toContain('Example input:')
expect(queryHelp.stdout).toContain('Evidence caveats:')
expect(queryHelp.stdout).toContain('Selectable row fields (--fields):')
expect(queryHelp.stdout).toContain('"required": [')
expect(queryHelp.stdout).toContain('"route": "/"')
expect(queryHelp.stdout).toContain('"loadScopes"')
expect(queryHelp.stdout).toContain('"loadScopes": [')
expect(queryHelp.stdout).toContain('default 500, max 2000')
expect(queryHelp.stdout).not.toContain('get_app_overview')
const directoryQueryHelp = await next.runCommand([
......@@ -205,8 +216,21 @@ describe('next experimental-analyze', () => {
expect(query.exitCode).toBe(0)
const overview = JSON.parse(query.stdout)
expect(overview).toMatchObject({
snapshot: {
analysisFingerprint: {
algorithm: 'sha256',
version: 1,
digest: expect.stringMatching(/^[0-9a-f]{64}$/),
},
},
pagination: { limit: 1, returned: 1 },
routes: [{ route: expect.any(String), rawSize: expect.any(Number) }],
routes: [
{
route: expect.any(String),
rawSize: expect.any(Number),
estimatedInitialClientRawSize: expect.any(Number),
},
],
})
for (const oldName of [
......@@ -236,6 +260,7 @@ describe('next experimental-analyze', () => {
const sourceResult = JSON.parse(sources.stdout)
expect(sourceResult).toMatchObject({
route: '/',
routeEntryDetection: { heuristic: false },
sources: [
{
sourcePath: expect.any(String),
......@@ -243,25 +268,50 @@ describe('next experimental-analyze', () => {
},
],
})
expect(JSON.parse(sources.stdout).sources[0]).not.toHaveProperty(
'chunks'
)
expect(sourceResult.sources[0]).not.toHaveProperty('chunks')
expect(sourceResult.sources[0]).not.toHaveProperty('chunksTruncated')
const sourceChunks = await next.runCommand([
'experimental-analyze',
'query',
'get_source_chunks',
'--input',
JSON.stringify({
route: '/',
sourcePath: sourceResult.sources[0].sourcePath,
}),
'--fields',
'filename,kind,rawSize',
])
expect(sourceChunks.exitCode).toBe(0)
expect(JSON.parse(sourceChunks.stdout)).toMatchObject({
chunks: [
{
filename: expect.any(String),
kind: expect.any(String),
rawSize: expect.any(Number),
},
],
})
const outputs = await next.runCommand([
'experimental-analyze',
'query',
'get_route_outputs',
'--input',
'{"route":"/","kinds":["css","font","image"]}',
JSON.stringify({ route: '/', kinds: ['css', 'font', 'image'] }),
'--all',
])
expect(outputs.exitCode).toBe(0)
expect(JSON.parse(outputs.stdout)).toMatchObject({
outputs: expect.arrayContaining([
const outputResult = JSON.parse(outputs.stdout)
expect(outputResult.outputs).toEqual(
expect.arrayContaining([
expect.objectContaining({ kind: 'css' }),
expect.objectContaining({ kind: 'font' }),
expect.objectContaining({ kind: 'image' }),
]),
})
])
)
expect(outputResult.pagination.truncated).toBe(false)
const cssAssets = await next.runCommand([
'experimental-analyze',
......@@ -271,13 +321,22 @@ describe('next experimental-analyze', () => {
'{"route":"/"}',
])
expect(cssAssets.exitCode).toBe(0)
expect(JSON.parse(cssAssets.stdout)).toMatchObject({
relationshipEvidence: 'output-reference',
assets: expect.arrayContaining([
expect.objectContaining({ assetKind: 'font' }),
expect.objectContaining({ assetKind: 'image' }),
]),
})
const cssAssetResult = JSON.parse(cssAssets.stdout)
expect(['output-reference', 'unavailable']).toContain(
cssAssetResult.relationshipEvidence
)
if (cssAssetResult.relationshipEvidence === 'output-reference') {
expect(cssAssetResult.assets).toEqual(
expect.arrayContaining([
expect.objectContaining({
cssFilename: expect.any(String),
assetFilename: expect.any(String),
emissionEvidence: 'emitted',
requestEvidence: 'unknown',
}),
])
)
}
const explanation = await next.runCommand([
'experimental-analyze',
......@@ -291,36 +350,207 @@ describe('next experimental-analyze', () => {
}),
])
expect(explanation.exitCode).toBe(0)
const explanationResult = JSON.parse(explanation.stdout)
expect(explanationResult).toMatchObject({
expect(JSON.parse(explanation.stdout)).toMatchObject({
route: '/',
sourcePath: sourceResult.sources[0].sourcePath,
routeEntryDetection: { heuristic: false },
selectedModule: { ident: expect.any(String) },
})
const graph = await next.runCommand([
async function sourcesFor(
search: string,
loadScopes?: string[],
groupBy = 'source'
) {
const result = await next.runCommand([
'experimental-analyze',
'query',
'get_route_modules',
'--input',
JSON.stringify({
route: '/',
environment: 'client',
search,
loadScopes,
groupBy,
limit: 100,
}),
])
expect(result.exitCode).toBe(0)
return JSON.parse(result.stdout)
}
const syncSources = await sourcesFor('app/sync.ts')
expect(syncSources.sources[0]).toMatchObject({
sourcePath: expect.stringContaining('app/sync.ts'),
loadScopes: ['initial'],
worker: { detected: false, heuristic: true },
})
const reExportedSources = await sourcesFor('app/leaf.ts')
expect(reExportedSources.sources[0]).toMatchObject({
sourcePath: expect.stringContaining('app/leaf.ts'),
loadScopes: ['initial'],
})
const lazySources = await sourcesFor('app/lazy.ts')
expect(lazySources.sources[0]).toMatchObject({
sourcePath: expect.stringContaining('app/lazy.ts'),
loadScopes: ['async'],
})
const workerSources = await sourcesFor('app/report.worker.ts')
expect(workerSources.sources[0]).toMatchObject({
loadScopes: ['async'],
worker: { detected: true, heuristic: true },
})
const asyncSources = await sourcesFor('app/', ['async'])
expect(
asyncSources.sources.map(
(source: { sourcePath: string }) => source.sourcePath
)
).toEqual(
expect.arrayContaining([
expect.stringContaining('app/lazy.ts'),
expect.stringContaining('app/report.worker.ts'),
])
)
expect(
asyncSources.sources.some((source: { sourcePath: string }) =>
source.sourcePath.includes('app/sync.ts')
)
).toBe(false)
const packages = await sourcesFor('node_modules/', undefined, 'package')
expect(
packages.sources.some(
(source: { loadScopes: string[] }) =>
source.loadScopes.includes('initial') &&
source.loadScopes.includes('async')
)
).toBe(true)
const scopeOrder = ['initial', 'async', 'traced', 'asset', 'unknown']
for (const source of packages.sources) {
expect(source.loadScopes).toEqual(
scopeOrder.filter((scope) => source.loadScopes.includes(scope))
)
}
const reExportedExplanation = await next.runCommand([
'experimental-analyze',
'query',
'explain_route_module',
'--input',
JSON.stringify({
route: '/',
environment: 'client',
sourcePath: reExportedSources.sources[0].sourcePath,
}),
])
expect(reExportedExplanation.exitCode).toBe(0)
const reExportedEvidence = JSON.parse(reExportedExplanation.stdout)
expect(reExportedEvidence).toMatchObject({
loadScopes: ['initial'],
nearestProjectImporter: {
path: expect.stringContaining('app/barrel.ts'),
},
})
const reExportedProjectChain =
reExportedEvidence.entryToSourceChain.chain.filter(
(entry: { module: { path: string } }) =>
/app\/(page\.tsx|barrel\.ts|leaf\.ts)$/.test(entry.module.path)
)
expect([
...new Set(
reExportedProjectChain.map(
(entry: { module: { path: string } }) => entry.module.path
)
),
]).toEqual([
expect.stringContaining('app/page.tsx'),
expect.stringContaining('app/barrel.ts'),
expect.stringContaining('app/leaf.ts'),
])
expect(
reExportedProjectChain
.slice(0, -1)
.every(
(entry: { edgeKindToNext?: string }) =>
entry.edgeKindToNext === 'sync'
)
).toBe(true)
const initialGraph = await next.runCommand([
'experimental-analyze',
'query',
'get_initial_import_graph',
'--input',
JSON.stringify({
route: '/',
sourcePath: sourceResult.sources[0].sourcePath,
moduleIdent: explanationResult.selectedModule.ident,
environment: 'total',
environment: 'client',
sourcePath: reExportedSources.sources[0].sourcePath,
moduleIdent: reExportedEvidence.selectedModule.ident,
}),
])
expect(graph.exitCode).toBe(0)
expect(JSON.parse(graph.stdout)).toMatchObject({
graph: {
complete: true,
nodes: expect.any(Array),
edges: expect.any(Array),
sccs: expect.any(Array),
sccEvidence: 'producer-petgraph',
expect(initialGraph.exitCode).toBe(0)
const graphEvidence = JSON.parse(initialGraph.stdout)
expect(graphEvidence.graph).toMatchObject({
complete: true,
nodes: expect.any(Array),
edges: expect.any(Array),
sccs: expect.any(Array),
sccEvidence: 'producer-petgraph',
})
expect(graphEvidence.graph.edges.length).toBeGreaterThan(0)
const graphEdge = graphEvidence.graph.edges[0]
const edgeDetail = await next.runCommand([
'experimental-analyze',
'query',
'analyze_import_edge',
'--input',
JSON.stringify({
route: '/',
environment: 'client',
sourcePath: reExportedSources.sources[0].sourcePath,
moduleIdent: reExportedEvidence.selectedModule.ident,
edgeId: graphEdge.edgeId,
granularity: 'source',
}),
])
expect(edgeDetail.exitCode).toBe(0)
const edgeEvidence = JSON.parse(edgeDetail.stdout)
expect(edgeEvidence.totals).toMatchObject({
moduleCount: graphEdge.leavingInitialModuleCount,
sourceCount: graphEdge.leavingInitialSourceCount,
rawSize: graphEdge.leavingInitialRawSize,
compressedSize: graphEdge.leavingInitialCompressedSize,
})
const lazyExplanation = await next.runCommand([
'experimental-analyze',
'query',
'explain_route_module',
'--input',
JSON.stringify({
route: '/',
environment: 'client',
sourcePath: lazySources.sources[0].sourcePath,
}),
])
expect(lazyExplanation.exitCode).toBe(0)
const lazyEvidence = JSON.parse(lazyExplanation.stdout)
expect(lazyEvidence).toMatchObject({
loadScopes: ['async'],
firstAsyncBoundary: {
importer: { path: expect.any(String) },
dependency: { path: expect.any(String) },
},
nearestProjectImporter: {
path: expect.stringContaining('app/page.tsx'),
},
nearestClientBoundary: {
path: expect.stringContaining('app/page.tsx'),
},
})
expect(
lazyEvidence.entryToSourceChain.chain.at(-1).module.path
).toContain('app/lazy.ts')
async function compare(input: Record<string, unknown>) {
return next.runCommand([
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment