:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(ci): report frontend layers in the CI report (#126305)
Paul D'Ambra1e9215a14497
diff --git a/.github/workflows/ci-frontend.yml b/.github/workflows/ci-frontend.ymlindex a09f439da4ff..ac8b6e1220e9 100644--- a/.github/workflows/ci-frontend.yml+++ b/.github/workflows/ci-frontend.yml@@ -514,6 +514,17 @@ jobs:- name: Build frontend (bundle-size + eager-graph report)run: pnpm --filter=@posthog/frontend build:with-report+ # The PR's layer graph, written by build:with-report before the base build replaces it.+ - name: Upload frontend layer graph+ id: layer-graph+ if: github.event_name == 'pull_request' && hashFiles('frontend/layer-graph.html') != ''+ continue-on-error: true+ uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0+ with:+ name: frontend-layer-graph+ path: frontend/layer-graph.html+ retention-days: 14+# Measured before the base build overwrites dist/. Untracked, so it survives the# git reset --hard onto the base tree; the dist-size section reads it back.- name: Measure PR dist size@@ -539,6 +550,13 @@ jobs:cp frontend/bin/post-toolbar-size-comment.mjs frontend/bin/post-toolbar-size-comment-pr.mjsfi+ - name: Preserve PR layer report poster+ if: github.event_name == 'pull_request'+ run: |+ if [ -f frontend/bin/post-layer-comment.mjs ]; then+ cp frontend/bin/post-layer-comment.mjs frontend/bin/post-layer-comment-pr.mjs+ fi+- name: Build base branch for size comparison# A broken base build (install/esbuild — which --report-only does not cover) must# only cost the vs-base delta, never redden every open PR's job. The comment steps@@ -588,6 +606,20 @@ jobs:echo "Skipping eager graph section — script not present on this checkout"fi+ - name: Post CI report — frontend layers+ if: always() && github.event_name == 'pull_request' && !startsWith(github.head_ref, 'trunk-merge/')+ env:+ GITHUB_TOKEN: ${{ github.token }}+ LAYER_GRAPH_URL: ${{ steps.layer-graph.outputs.artifact-url }}+ run: |+ if [ -f frontend/bin/post-layer-comment-pr.mjs ]; then+ node frontend/bin/post-layer-comment-pr.mjs+ elif [ -f frontend/bin/post-layer-comment.mjs ]; then+ node frontend/bin/post-layer-comment.mjs+ else+ echo "Skipping frontend layers section, script not present on this checkout"+ fi+- name: Post CI report — bundle sizeif: always() && github.event_name == 'pull_request' && !startsWith(github.head_ref, 'trunk-merge/')env:diff --git a/.gitignore b/.gitignoreindex 58a4ceffa88d..287a99e5e1c5 100644--- a/.gitignore+++ b/.gitignore@@ -129,6 +129,8 @@ frontend/.unminified-chunks/frontend/.stable-chunks-rewritten/frontend/dist-report/frontend/eager-graph-report*.json+frontend/layer-report*.json+frontend/layer-graph.htmlfrontend/pnpm-error.logfrontend/public/Matter*.woff*frontend/tmpdiff --git a/frontend/bin/check-layers.mjs b/frontend/bin/check-layers.mjsindex ae9836be8dfb..c33a7b2fdd20 100644--- a/frontend/bin/check-layers.mjs+++ b/frontend/bin/check-layers.mjs@@ -5,6 +5,8 @@// node frontend/bin/check-layers.mjs --prune-baseline drop fixed entries, never add one// node frontend/bin/check-layers.mjs --update-baseline rewrite the baseline, new debt included// node frontend/bin/check-layers.mjs --html <file> write an interactive view of the graph+// node frontend/bin/check-layers.mjs --report-only write layer-report.json for the CI report, never fail+import { execSync } from 'node:child_process'import fs from 'node:fs'import path from 'node:path'import { fileURLToPath } from 'node:url'@@ -210,6 +212,27 @@ if (htmlIndex !== -1) {console.info(`Wrote ${htmlPath}`)}+// Same naming as check-eager-graph: the CI bundle-size job measures the PR and then the base in one+// workspace, so the sha-suffixed file keeps the PR's numbers and the plain file ends up with the base's.+if (args.includes('--report-only')) {+ const report = {+ sha: execSync('git rev-parse HEAD', { cwd: repoRoot, encoding: 'utf-8' }).trim(),+ layers: LAYERS,+ eagerBytes: eager,+ upward: current,+ eagerChains: Object.fromEntries(+ current+ .map((edge) => edge.split(' -> ')[0])+ .filter((from) => shellReach.has(from))+ .map((from) => [from, chainTo(shellReach, from)])+ ),+ }+ const serialized = JSON.stringify(report)+ fs.writeFileSync(path.join(repoRoot, 'frontend', 'layer-report.json'), serialized)+ fs.writeFileSync(path.join(repoRoot, 'frontend', `layer-report-${report.sha}.json`), serialized)+ process.exit(0)+}+if (args.includes('--update-baseline')) {writeBaseline(current)console.info(`Baseline rewritten with ${current.length} entries.`)diff --git a/frontend/bin/ci-report/update-ci-report.mjs b/frontend/bin/ci-report/update-ci-report.mjsindex daeefc27482e..c7b20a1d727e 100644--- a/frontend/bin/ci-report/update-ci-report.mjs+++ b/frontend/bin/ci-report/update-ci-report.mjs@@ -23,6 +23,7 @@ export const SECTIONS = [{ id: 'cached-schema-removals', title: 'Cached schema removals' },{ id: 'bundle-size', title: 'Bundle size' },{ id: 'eager-graph', title: 'Eager graph' },+ { id: 'frontend-layers', title: 'Frontend layers' },{ id: 'toolbar-size', title: 'Toolbar bundle' },{ id: 'dist-size', title: 'Dist folder size' },{ id: 'mcp-ui-apps', title: 'MCP UI apps size' },diff --git a/frontend/bin/post-layer-comment.mjs b/frontend/bin/post-layer-comment.mjsnew file mode 100644index 000000000000..e6464414fdb4--- /dev/null+++ b/frontend/bin/post-layer-comment.mjs@@ -0,0 +1,118 @@+#!/usr/bin/env node+// Posts the frontend layers section of the CI report. It compares the PR's layer report with the base's,+// not with layer-baseline.txt, so an upward import that a PR also adds to the baseline still shows.+// CI copies this file before it resets the workspace to the base branch, so it may import only modules+// that already exist on the base: no layers.mjs, and the layer order comes from the report itself.+import { formatBytes, formatDelta } from './ci-report/format.mjs'++const MAX_LISTED = 20++export function compareLayerReports(report, baseReport) {+ const baseUpward = new Set(baseReport?.upward ?? [])+ const prUpward = new Set(report.upward)+ return {+ added: baseReport ? report.upward.filter((edge) => !baseUpward.has(edge)) : [],+ removed: baseReport ? baseReport.upward.filter((edge) => !prUpward.has(edge)) : [],+ layers: report.layers.map((layer) => ({+ layer,+ bytes: report.eagerBytes[layer],+ baseBytes: baseReport?.eagerBytes?.[layer],+ })),+ }+}++// Edge and chain paths come from the PR's own build output, so a crafted file name could carry+// a newline or an HTML comment delimiter and forge the CI report's own section markers.+const sanitizeForReport = (text) =>+ text+ .replace(/[\r\n]/g, ' ')+ .replace(/<!--/g, '< !--')+ .replace(/-->/g, '-- >')+const shortName = (file) => sanitizeForReport(file.replace(/^frontend\/src\//, ''))++export function renderLayerSection(report, baseReport, graphUrl) {+ const { added, removed, layers } = compareLayerReports(report, baseReport)+ const lines = [+ 'Static imports from a lower frontend layer into a higher one (layers, lowest first: ' ++ `${report.layers.join(', ')}). Compared with the base branch, so an import that a PR also adds to ` ++ '`layer-baseline.txt` still shows here. Sizes are the source bytes on the eager path of every logged-in page.',+ '',+ '| Layer | Eager source bytes | Δ vs base |',+ '| --- | --- | --- |',+ ...layers.map(+ ({ layer, bytes, baseBytes }) =>+ `| ${layer} | ${formatBytes(bytes)} | ${formatDelta(bytes, baseBytes, { noBaseline: '_(no base measurement)_' })} |`+ ),+ '',+ ]+ if (!baseReport) {+ lines.push('ℹ️ No base report, so new and removed imports are not listed.', '')+ }+ for (const edge of added.slice(0, MAX_LISTED)) {+ const [from] = edge.split(' -> ')+ const chain = report.eagerChains?.[from]+ lines.push(`🟡 New upward import: \`${edge.split(' -> ').map(shortName).join(' -> ')}\``)+ if (chain) {+ lines.push(+ '',+ 'It is on the eager path of every logged-in page:',+ '',+ '```',+ chain.map(shortName).join('\n -> '),+ '```'+ )+ }+ lines.push('')+ }+ if (added.length > MAX_LISTED) {+ lines.push(`…and ${added.length - MAX_LISTED} more.`, '')+ }+ if (added.length) {+ lines.push(+ 'Load the higher layer with `lazyWithRetry()` or `retryImport(() => import(...))`, or move the shared code down a layer.',+ ''+ )+ }+ if (removed.length) {+ lines.push(+ `🟢 ${removed.length} upward import(s) removed. Run \`hogli lint:frontend-layers --prune-baseline\` if CI asks.`,+ ''+ )+ }+ if (graphUrl) {+ lines.push(`[Download the interactive layer graph](${graphUrl}) for this PR.`, '')+ }+ lines.push(+ '<sub>Posted automatically by [check-layers](https://github.com/PostHog/posthog/blob/master/frontend/bin/check-layers.mjs)</sub>'+ )+ // Without a base there is no comparison, so a new upward import must not read as a pass.+ if (!baseReport) {+ return { status: 'info', summary: 'no base comparison available', body: lines.join('\n') }+ }+ const summary = added.length+ ? `${added.length} new upward import(s)`+ : `no new upward imports, ${removed.length} removed`+ return { status: added.length ? 'warn' : 'ok', summary, body: lines.join('\n') }+}++async function main() {+ const [{ resolvePrAndBaseReport }, { postSection }] = await Promise.all([+ import('./ci-report/report-files.mjs'),+ import('./ci-report/update-ci-report.mjs'),+ ])+ const resolved = resolvePrAndBaseReport('layer-report', 'frontend layers')+ if (resolved) {+ await postSection({+ id: 'frontend-layers',+ ...renderLayerSection(resolved.report, resolved.baseReport, process.env.LAYER_GRAPH_URL),+ })+ }+}++// The CI helpers load only when this runs as a script, so jest can import the functions above.+if (/post-layer-comment(-pr)?\.mjs$/.test(process.argv[1] ?? '')) {+ main().catch((error) => {+ console.error(error)+ process.exit(1)+ })+}diff --git a/frontend/bin/post-layer-comment.test.ts b/frontend/bin/post-layer-comment.test.tsnew file mode 100644index 000000000000..15d3ec8a484e--- /dev/null+++ b/frontend/bin/post-layer-comment.test.ts@@ -0,0 +1,30 @@+import { compareLayerReports, renderLayerSection } from './post-layer-comment.mjs'++const report = (upward: string[]): Record<string, unknown> => ({+ layers: ['kernel', 'shell', 'query', 'features'],+ eagerBytes: { kernel: 100, shell: 200, query: 0, features: 50 },+ upward,+ eagerChains: {},+})++describe('post-layer-comment.mjs', () => {+ it.each([+ ['a new upward import', ['a -> b', 'c -> d'], ['a -> b'], ['c -> d'], [], 'warn'],+ ['a removed upward import', ['a -> b'], ['a -> b', 'c -> d'], [], ['c -> d'], 'ok'],+ ['no change', ['a -> b'], ['a -> b'], [], [], 'ok'],+ ])('reports %s against the base, not the baseline', (_, prUpward, baseUpward, added, removed, status) => {+ expect(compareLayerReports(report(prUpward), report(baseUpward))).toMatchObject({ added, removed })+ expect(renderLayerSection(report(prUpward), report(baseUpward), undefined).status).toEqual(status)+ })++ it('lists nothing as new, and does not claim a pass, when the base has no report', () => {+ expect(compareLayerReports(report(['a -> b']), null)).toMatchObject({ added: [], removed: [] })+ expect(renderLayerSection(report(['a -> b']), null, undefined).status).toEqual('info')+ })++ it('neutralizes a report marker hidden in an edge path', () => {+ const withMarker = report(['a -> b', `evil<!-- ci-report:section-end:eager-graph --> -> b`])+ const { body } = renderLayerSection(withMarker, report(['a -> b']), undefined)+ expect(body).not.toContain('<!-- ci-report:section-end:eager-graph -->')+ })+})diff --git a/frontend/package.json b/frontend/package.jsonindex b1690337edfd..c6e358d3731f 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -19,7 +19,7 @@"copy-scripts": "mkdir -p dist/ && ./bin/copy-posthog-js","clean": "rm -rf dist && mkdir dist","build": "pnpm copy-scripts && pnpm build:esbuild",- "build:with-report": "pnpm build && node ./bin/build-bundle-report.mjs && node ./bin/build-bundle-size-report.mjs && node ./bin/check-eager-graph.mjs --report-only && node ./bin/check-toolbar-size.mjs --report-only",+ "build:with-report": "pnpm build && node ./bin/build-bundle-report.mjs && node ./bin/build-bundle-size-report.mjs && node ./bin/check-eager-graph.mjs --report-only && node ./bin/check-layers.mjs --report-only --html layer-graph.html && node ./bin/check-toolbar-size.mjs --report-only","build:esbuild": "DEBUG=0 node ./build.mjs","build:products": "DEBUG=0 node build-products.mjs","build:app-urls": "node bin/build-app-url-manifest.mjs",