From ebb3501e881854574054af82b1e09e99077a11bf Mon Sep 17 00:00:00 2001 From: Jason Morse <35609178+JasonVMo@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:45:23 -0800 Subject: [PATCH 1/2] Align retained Storybook startup readiness budgets Allow cold CI bundles to use the channel's initial-render budget instead of timing out at the listener deadline. Keep authenticated readiness, bounded failure, and ownership-safe cleanup. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../reliable-retained-storybook-startup.md | 5 + .../references/integration.md | 2 +- .../storybook-desktop/references/readiness.md | 10 ++ .../src/cli/DesktopStorybookCli.test.ts | 99 +++++++++++++++++++ .../src/cli/DesktopStorybookCli.ts | 50 +++++++--- .../storybook-desktop/src/config/commands.ts | 4 +- 6 files changed, 154 insertions(+), 16 deletions(-) create mode 100644 .changeset/reliable-retained-storybook-startup.md diff --git a/.changeset/reliable-retained-storybook-startup.md b/.changeset/reliable-retained-storybook-startup.md new file mode 100644 index 0000000000..c5b1d0471d --- /dev/null +++ b/.changeset/reliable-retained-storybook-startup.md @@ -0,0 +1,5 @@ +--- +'@fluentui-react-native/storybook-desktop': patch +--- + +Align retained native smoke and hosted-driver readiness deadlines with the channel's initial-render budget, allowing cold CI Metro bundles to finish without weakening authenticated readiness. diff --git a/packages/agentic/storybook-desktop/references/integration.md b/packages/agentic/storybook-desktop/references/integration.md index 9226b11a65..07ac421670 100644 --- a/packages/agentic/storybook-desktop/references/integration.md +++ b/packages/agentic/storybook-desktop/references/integration.md @@ -100,7 +100,7 @@ command incarnation, and producer PID. The neutral resource scope validates and retains that receipt on the owned channel's resource generation before the CLI reports readiness. The generated `agent-ready.*.json` file is a compatibility breadcrumb only, never readiness authority. For a -supervisor-retained Windows or Win32 application, the channel writes that +supervisor-retained macOS, Windows, or Win32 application, the channel writes that breadcrumb and publishes `service-ready` only after the retained app's authenticated runtime has said hello and rendered its initial story, bounded by `STORYBOOK_INITIAL_RENDER_TIMEOUT_MS` (default 300 s). A cold debug bundle can diff --git a/packages/agentic/storybook-desktop/references/readiness.md b/packages/agentic/storybook-desktop/references/readiness.md index 70100a6d4a..c978d439e3 100644 --- a/packages/agentic/storybook-desktop/references/readiness.md +++ b/packages/agentic/storybook-desktop/references/readiness.md @@ -29,6 +29,16 @@ no open bridge after a close, still reports `relaunch-required`. Startup render readiness does not replace request-correlated readiness for each requested story. +Retained native smoke and hosted drivers check listener startup separately +(`startupTimeoutMs`, default 120 seconds). Correlated channel readiness allows +that budget plus the initial-render budget (`initialRenderTimeoutMs`, then +`STORYBOOK_INITIAL_RENDER_TIMEOUT_MS` from the server command or inherited +environment, then an explicit `startupTimeoutMs`, otherwise 300 seconds). +The CLI forwards the same render budget to the channel so it cannot cancel a +healthy cold Metro bundle at the listener deadline. An HTTP-ready listener +alone never qualifies the runtime; failed processes and cancellation still +abort readiness immediately. + Each socket receives a fresh connection epoch in a version-2 hello challenge. The hello must echo that epoch and the manifest identity. Ready/error events bind the epoch, request, run, and story; version-1 native markers additionally diff --git a/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.test.ts b/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.test.ts index 78a588e3a7..dc7074778a 100644 --- a/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.test.ts +++ b/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.test.ts @@ -5,6 +5,7 @@ import path from 'node:path'; import type { DesktopStoryManifestEntry, NativeDriverArtifact } from '@fluentui-react-native/desktop-driver'; import { validateDesktopCaseOptions } from '@fluentui-react-native/desktop-driver/authoring'; import { DesktopServiceScope, DesktopSupervisor } from '@fluentui-react-native/desktop-test'; +import { deadline } from '@fluentui-react-native/desktop-test/commands'; import type { ResolvedDesktopConfig, ResolvedDesktopTarget } from '@fluentui-react-native/desktop-test'; import { selectDesktopRenderer } from '@fluentui-react-native/desktop-test/config'; import { DesktopRunReporter } from '@fluentui-react-native/desktop-test/reporting'; @@ -364,6 +365,12 @@ describe('DesktopStorybookCli', () => { executablePath: '/Fixture/Host.app/Contents/MacOS/Host', windowTitle: 'Agentic Components Storybook', }); + expect(channel.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS).toBe('300000'); + expect(f.runner.readiness).toHaveBeenCalledWith( + { tier: 'service-ready', service: 'storybook-channel', descriptor: 'agent.json' }, + 420_000, + expect.any(AbortSignal), + ); expect(JSON.parse(fs.readFileSync(channel.env.STORYBOOK_DRIVER_MANIFEST, 'utf8')).agent.descriptor).toBe('agent.json'); expect(f.options.resolveDesktopApplication).toHaveBeenCalledWith( expect.objectContaining({ @@ -452,6 +459,12 @@ describe('DesktopStorybookCli', () => { expect(ready).toMatchObject({ targetId: f.target.id, agentDescriptor: 'agent.json', serviceReady: { service: 'storybook-channel' } }); expect(ready.integration.details.services).toEqual(platform === 'windows' ? ['metro', 'storybook-channel'] : ['storybook-channel']); expect(JSON.parse(channel.env.STORYBOOK_OWNED_APPLICATION)).toEqual(owned); + expect(channel.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS).toBe('300000'); + expect(f.runner.readiness).toHaveBeenCalledWith( + { tier: 'service-ready', service: 'storybook-channel', descriptor: 'agent.json' }, + 420_000, + expect.any(AbortSignal), + ); const { arguments: _arguments, ...identity } = owned as typeof owned & { arguments?: string[] }; expect(JSON.parse(fs.readFileSync(channel.env.STORYBOOK_DRIVER_MANIFEST, 'utf8')).agent.application).toEqual(identity); expect(f.options.resolveDesktopApplication).toHaveBeenCalledTimes(1); @@ -605,6 +618,17 @@ describe('DesktopStorybookCli', () => { const phases = f.runner.background.filter((command) => command.env.STORYBOOK_OWNED_APPLICATION); expect(phases).toHaveLength(platform === 'windows' ? 2 : 1); expect(phases.every((command) => command.env.STORYBOOK_DRIVER_HANDOFF && command.env.STORYBOOK_DRIVER_MANIFEST)).toBe(true); + expect(phases.every((command) => command.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS === '300000')).toBe(true); + expect(f.runner.readiness).toHaveBeenCalledWith( + { tier: 'service-ready', service: 'storybook-channel-listener' }, + 120_000, + expect.any(AbortSignal), + ); + expect(f.runner.readiness).toHaveBeenCalledWith( + { tier: 'service-ready', service: 'storybook-channel' }, + 420_000, + expect.any(AbortSignal), + ); if (platform === 'macos') { expect(JSON.parse(phases[0].env.STORYBOOK_OWNED_APPLICATION)).toEqual({ bundleIdentifier: cli.instance.bundleIdentifier, @@ -646,6 +670,81 @@ describe('DesktopStorybookCli', () => { expect(f.events.at(-1)).toBe('lease-withdrawn'); }); + test.each([ + ['explicit render budget', { startupTimeoutMs: 1_000, initialRenderTimeoutMs: 45_000 }, {}, 45_000], + ['channel environment override', { startupTimeoutMs: 1_000 }, { STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: '65000' }, 65_000], + [ + 'explicit render budget over environment', + { startupTimeoutMs: 1_000, initialRenderTimeoutMs: 45_000 }, + { STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: '65000' }, + 45_000, + ], + ['legacy startup fallback', { startupTimeoutMs: 1_000 }, {}, 1_000], + ] as const)('aligns native smoke listener and channel deadlines: %s', async (_label, timeouts, env, renderMs) => { + const f = nativeSmokeFixture('macos'); + const config = makeConfig({ + macos: { + build: { command: 'build-native' }, + smoke: { ...timeouts, native: { kind: 'macos' }, server: { command: 'channel-fixture', env } }, + }, + }); + await new DesktopStorybookCli(config, f.options).smoke('macos', { mode: 'stories' }); + expect(f.runner.background.find((command) => command.context?.service === 'storybook-stories')?.env).toMatchObject({ + STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: String(renderMs), + }); + expect(f.runner.readiness).toHaveBeenCalledWith( + { tier: 'service-ready', service: 'storybook-channel-listener' }, + 1_000, + expect.any(AbortSignal), + ); + expect(f.runner.readiness).toHaveBeenCalledWith( + { tier: 'service-ready', service: 'storybook-channel' }, + 1_000 + renderMs, + expect.any(AbortSignal), + ); + }); + + test.each([false, true])('waits past listener startup for a cold render without becoming unbounded (expire=%s)', async (expire) => { + const f = nativeSmokeFixture('macos'); + let releaseRender!: () => void; + let observingRender!: () => void; + const render = new Promise((resolve) => (releaseRender = resolve)); + const observing = new Promise((resolve) => (observingRender = resolve)); + const readiness = f.runner.readiness.getMockImplementation()!; + f.runner.readiness.mockImplementation(async (expected, timeoutMs?: number) => { + if (expected.service !== 'storybook-channel') return readiness(expected); + jest.useFakeTimers(); + const waiting = deadline( + render.then(() => readiness(expected)), + timeoutMs!, + 'Channel initial render', + ); + observingRender(); + return waiting; + }); + const smoke = new DesktopStorybookCli(f.config, f.options).smoke('macos', { mode: 'stories' }); + void smoke.catch(() => undefined); + try { + await observing; + await jest.advanceTimersByTimeAsync(120_001); + expect(f.events).not.toContain('attach'); + expect(f.events).not.toContain('stop:storybook-stories'); + if (expire) { + await jest.advanceTimersByTimeAsync(300_000); + await expect(smoke).rejects.toThrow('Channel initial render exceeded 420000ms'); + } else { + releaseRender(); + await smoke; + expect(f.events).toContain('select:first--story'); + } + expect(f.events.slice(-3)).toEqual(['stop:storybook-stories', 'stop:metro', 'lease-withdrawn']); + } finally { + releaseRender(); + await Promise.allSettled([smoke]); + jest.useRealTimers(); + } + }); + test.each([ [false, 'accessible'], [true, 'physical'], diff --git a/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.ts b/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.ts index 63c1bc0551..c63d2dd8de 100644 --- a/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.ts +++ b/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.ts @@ -87,6 +87,16 @@ type OwnedMacOSApplication = { type OwnedWindowsSmokeOptions = Exclude, { kind: 'macos' }>; +function retainedStartupTimeouts(smoke: DesktopSmokeOptions | undefined, server: DesktopCommand) { + const configuredRenderTimeout = Number( + server.env?.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS ?? process.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS, + ); + return { + startupTimeoutMs: smoke?.startupTimeoutMs ?? 120_000, + initialRenderTimeoutMs: smoke?.initialRenderTimeoutMs ?? (configuredRenderTimeout || (smoke?.startupTimeoutMs ?? 300_000)), + }; +} + export type DesktopStorybookBuildDriverOptions = { force?: boolean; }; @@ -344,9 +354,6 @@ export class DesktopStorybookCli { } const macApplication = retained && platform === 'macos' ? this.ownedMacOSApplication(this.instance) : undefined; const windowsNative = retained && platform !== 'macos' ? this.ownedWindowsSmokeOptions(platform) : undefined; - const smokeOptions = windowsNative ? this.config.getPlatformOptions(platform).smoke : undefined; - // The retained channel withholds readiness until the app's initial render, which a cold development bundle delays. - const initialRenderTimeoutMs = smokeOptions ? (smokeOptions.initialRenderTimeoutMs ?? smokeOptions.startupTimeoutMs ?? 300_000) : 0; const wdio = resolveWdioOptions({ ...this.config.wdio, ...(options.input ? { input: options.input } : {}) }); const agent = options.agent && { descriptor: options.agent.descriptor ?? defaultAgentDescriptor, @@ -365,6 +372,10 @@ export class DesktopStorybookCli { if (command === false || command === undefined) { throw unsupportedAction('server', platform); } + const timeouts = + macApplication || windowsNative + ? retainedStartupTimeouts(this.config.getPlatformOptions(platform).smoke || undefined, command) + : undefined; const nativeDriver = await this.resolveDriver(platform); const storybookPort = options.port ?? (await findAvailablePort(this.instance.storybookPort, this.isPortAvailable)); const metroPort = await this.resolveDriverMetroPort(platform, storybookPort); @@ -430,7 +441,7 @@ export class DesktopStorybookCli { env: { ...command.env, ...(options.host ? { STORYBOOK_WS_HOST: options.host } : {}), - ...(initialRenderTimeoutMs ? { STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: String(initialRenderTimeoutMs) } : {}), + ...(timeouts ? { STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: String(timeouts.initialRenderTimeoutMs) } : {}), }, }, platform, @@ -440,14 +451,20 @@ export class DesktopStorybookCli { listener: { service: 'storybook-channel-listener', port: storybookPort }, }); const [, channelListener] = await Promise.all([ - metro && this.waitForOwnedService('metro', 'metro-listener', loopbackUrl(metroPort, '/status'), metro), - this.waitForOwnedService('storybook-channel', 'storybook-channel-listener', loopbackUrl(storybookPort, '/index.json'), channel), + metro && this.waitForOwnedService('metro', 'metro-listener', loopbackUrl(metroPort, '/status'), metro, timeouts?.startupTimeoutMs), + this.waitForOwnedService( + 'storybook-channel', + 'storybook-channel-listener', + loopbackUrl(storybookPort, '/index.json'), + channel, + timeouts?.startupTimeoutMs, + ), ]); const agentManifest = driverManifest.manifest.agent; const channelReady = await this.services.readyProcess( 'storybook-channel', { tier: 'service-ready', service: 'storybook-channel', ...(agentManifest ? { descriptor: agentManifest.descriptor } : {}) }, - 120_000 + initialRenderTimeoutMs, + timeouts ? timeouts.startupTimeoutMs + timeouts.initialRenderTimeoutMs : 120_000, ); assertSameReadinessProducer(channelListener, channelReady); // Windows and Win32 publish the retained app's lease inside the channel before it reports service readiness. @@ -1025,6 +1042,7 @@ export class DesktopStorybookCli { const configuredServer = this.config.getPlatformOptions(platform).server; const server = smoke.server ?? configuredServer; if (!server) throw new Error('Native smoke requires its owned Storybook channel.'); + const { startupTimeoutMs, initialRenderTimeoutMs } = retainedStartupTimeouts(smoke, server); const serverUrl = smoke.serverUrl ?? loopbackUrl(instance.storybookPort); const index = await this.createStoryManifest(this.config, platform); if (!index.entries.length) throw new Error('Native smoke selected no stories.'); @@ -1046,7 +1064,7 @@ export class DesktopStorybookCli { 'metro-listener', smoke.metroUrl ?? loopbackUrl(instance.metroPort, '/status'), metro, - smoke.startupTimeoutMs, + startupTimeoutMs, ); } const startPhase = async (phase: string) => { @@ -1067,7 +1085,12 @@ export class DesktopStorybookCli { identity: `storybook-smoke:${instance.id}:${phase}`, acquire: async () => { running = this.runner.start({ - ...this.prepareCommand(server, platform, instance, application), + ...this.prepareCommand( + { ...server, env: { ...server.env, STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: String(initialRenderTimeoutMs) } }, + platform, + instance, + application, + ), label: `Storybook ${phase}`, category: 'service', context: { service: `storybook-${phase}` }, @@ -1082,16 +1105,17 @@ export class DesktopStorybookCli { if (!running.waitForReadiness) throw new Error('Native smoke requires correlated channel readiness.'); const listener = await running.waitForReadiness( { tier: 'service-ready', service: 'storybook-channel-listener' }, - smoke.startupTimeoutMs ?? 120_000, + startupTimeoutMs, this.services.controller.signal, ); - await this.waitForUrl(new URL('/index.json', serverUrl).href, running, smoke.startupTimeoutMs, { + await this.waitForUrl(new URL('/index.json', serverUrl).href, running, startupTimeoutMs, { headers: this.listenerProbeHeaders(running), }); - await this.confirmListenerProbe(running, 'storybook-channel-listener', listener, smoke.startupTimeoutMs); + await this.confirmListenerProbe(running, 'storybook-channel-listener', listener, startupTimeoutMs); + // Retained launch and cold bundling precede the channel's authenticated initial-render receipt. const ready = await running.waitForReadiness( { tier: 'service-ready', service: 'storybook-channel' }, - smoke.startupTimeoutMs ?? 120_000, + startupTimeoutMs + initialRenderTimeoutMs, this.services.controller.signal, ); if (ready.service !== 'storybook-channel' || ready.descriptor !== undefined) diff --git a/packages/agentic/storybook-desktop/src/config/commands.ts b/packages/agentic/storybook-desktop/src/config/commands.ts index 88234dd8ba..11aa86c869 100644 --- a/packages/agentic/storybook-desktop/src/config/commands.ts +++ b/packages/agentic/storybook-desktop/src/config/commands.ts @@ -113,14 +113,14 @@ export type DesktopSmokeOptions = { metroUrl?: string; /** - * Maximum time to wait for the channel server and Metro. + * Maximum time to wait for the channel listener and Metro, excluding the retained app's initial render. * @default 120000 */ startupTimeoutMs?: number; /** * Maximum time to wait for the first story, including cold development bundling and runtime startup. - * Falls back to an explicitly configured startupTimeoutMs. + * Retained channels fall back to STORYBOOK_INITIAL_RENDER_TIMEOUT_MS, then an explicitly configured startupTimeoutMs. * @default 300000 */ initialRenderTimeoutMs?: number; From 2f916d7c23956c27dfaa963801170ad9779de854 Mon Sep 17 00:00:00 2001 From: Jason Morse <35609178+JasonVMo@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:59:49 -0800 Subject: [PATCH 2/2] Share Storybook runtime timeout constants with tests Derive default deadline expectations from the production constants and name shared test overrides without changing runtime timeout values. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../storybook-desktop/config/server.cjs | 3 +- .../config/storybook-control.cjs | 4 +- .../storybook-desktop/config/timeouts.cjs | 11 +++ .../storybook-desktop/config/timeouts.d.cts | 4 + .../references/contributing.md | 6 ++ .../src/cli/DesktopStorybookCli.test.ts | 88 +++++++++++++------ .../src/cli/DesktopStorybookCli.ts | 50 ++++++++--- 7 files changed, 121 insertions(+), 45 deletions(-) create mode 100644 packages/agentic/storybook-desktop/config/timeouts.cjs create mode 100644 packages/agentic/storybook-desktop/config/timeouts.d.cts diff --git a/packages/agentic/storybook-desktop/config/server.cjs b/packages/agentic/storybook-desktop/config/server.cjs index c7ca3a97a6..9c79cd5d18 100644 --- a/packages/agentic/storybook-desktop/config/server.cjs +++ b/packages/agentic/storybook-desktop/config/server.cjs @@ -2,6 +2,7 @@ const { createRequire } = require('node:module'); const fs = require('node:fs'); const path = require('node:path'); const { createControlledChannelServer } = require('./controlled-channel.cjs'); +const { DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS } = require('./timeouts.cjs'); async function startDesktopStorybookServer({ configPath, @@ -280,7 +281,7 @@ async function startDesktopStorybookServer({ if (retainedOrchestrator) { // A retained debug app may still be loading a cold Metro bundle; agents and runners must not observe readiness // until its authenticated runtime has rendered, or every story operation fails as bridge-unavailable. - const timeoutMs = Number(process.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS) || 300_000; + const timeoutMs = Number(process.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS) || DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS; const timeout = AbortSignal.timeout(timeoutMs); try { await retainedOrchestrator.waitForInitialBridge(AbortSignal.any([controller.signal, timeout])); diff --git a/packages/agentic/storybook-desktop/config/storybook-control.cjs b/packages/agentic/storybook-desktop/config/storybook-control.cjs index 795cf3c33a..3446069059 100644 --- a/packages/agentic/storybook-desktop/config/storybook-control.cjs +++ b/packages/agentic/storybook-desktop/config/storybook-control.cjs @@ -3,13 +3,15 @@ const path = require('node:path'); const { pathToFileURL } = require('node:url'); const { writeDesktopStorybookFailure } = require('./diagnostics.cjs'); const { parseDriverManifest } = require('./generated-contract.cjs'); +const { DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS } = require('./timeouts.cjs'); const host = process.env.STORYBOOK_WS_HOST || '127.0.0.1'; const port = Number(process.env.STORYBOOK_WS_PORT) || 7007; const smokeMode = process.env.STORYBOOK_SMOKE_MODE || 'stories'; const smokePhase = parseSmokePhase(process.argv.slice(2)); // The runtime connects only after Metro serves a cold development bundle, so the first story gets a startup budget. -const initialRenderMilliseconds = Number(process.env.STORYBOOK_SMOKE_INITIAL_RENDER_TIMEOUT_MS) || 300_000; +const initialRenderMilliseconds = + Number(process.env.STORYBOOK_SMOKE_INITIAL_RENDER_TIMEOUT_MS) || DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS; const controller = new AbortController(); let report; let runner; diff --git a/packages/agentic/storybook-desktop/config/timeouts.cjs b/packages/agentic/storybook-desktop/config/timeouts.cjs new file mode 100644 index 0000000000..741f1945a3 --- /dev/null +++ b/packages/agentic/storybook-desktop/config/timeouts.cjs @@ -0,0 +1,11 @@ +const DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS = 120_000; +const DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS = 300_000; +const DEFAULT_STORYBOOK_STORY_RENDER_TIMEOUT_MS = 15_000; +const STORYBOOK_NATIVE_UI_TIMEOUT_MS = 30_000; + +module.exports = { + DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, + DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS, + DEFAULT_STORYBOOK_STORY_RENDER_TIMEOUT_MS, + STORYBOOK_NATIVE_UI_TIMEOUT_MS, +}; diff --git a/packages/agentic/storybook-desktop/config/timeouts.d.cts b/packages/agentic/storybook-desktop/config/timeouts.d.cts new file mode 100644 index 0000000000..a684ca068c --- /dev/null +++ b/packages/agentic/storybook-desktop/config/timeouts.d.cts @@ -0,0 +1,4 @@ +export const DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS: number; +export const DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: number; +export const DEFAULT_STORYBOOK_STORY_RENDER_TIMEOUT_MS: number; +export const STORYBOOK_NATIVE_UI_TIMEOUT_MS: number; diff --git a/packages/agentic/storybook-desktop/references/contributing.md b/packages/agentic/storybook-desktop/references/contributing.md index b01a620731..9e77f28ae5 100644 --- a/packages/agentic/storybook-desktop/references/contributing.md +++ b/packages/agentic/storybook-desktop/references/contributing.md @@ -126,6 +126,12 @@ Keep story callbacks statically extractable and authentication checks fail-closed. Update references when commands, supported config, outputs, or readiness contracts change. +Shared lifecycle timeout values live in [`config/timeouts.cjs`](../config/timeouts.cjs), +with TypeScript declarations in `config/timeouts.d.cts`. The CLI, channel and +tests import those production values rather than duplicating numeric defaults. +Keep overrides local to a test, or name fixture constants when several tests +share them. + Authenticated bridge epochs are recorded by `BridgeEpochJournal` in the service factory's private endpoint/target namespace, separately from settlement and quarantine. The record binds renderer and epoch to a native-verified PID diff --git a/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.test.ts b/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.test.ts index dc7074778a..ce476d9958 100644 --- a/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.test.ts +++ b/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.test.ts @@ -22,6 +22,14 @@ import * as wdioTests from '../testing/runWdioTests'; import type { DesktopCommandRunner, PreparedDesktopCommand, RunningDesktopCommand } from './commandRunner'; import { createDesktopStorybookCommand } from './createDesktopStorybookCommand'; import { DesktopStorybookCli } from './DesktopStorybookCli'; +import { + DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, + DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS, + DEFAULT_STORYBOOK_STORY_RENDER_TIMEOUT_MS, + STORYBOOK_NATIVE_UI_TIMEOUT_MS, +} from '../../config/timeouts.cjs'; + +const defaultRetainedReadyTimeoutMs = DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS + DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS; const appRoot = path.resolve(__dirname, '../../../../../apps/storybook'); const linkedAppDirectories = ['node_modules', 'src']; @@ -365,10 +373,10 @@ describe('DesktopStorybookCli', () => { executablePath: '/Fixture/Host.app/Contents/MacOS/Host', windowTitle: 'Agentic Components Storybook', }); - expect(channel.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS).toBe('300000'); + expect(channel.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS).toBe(String(DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS)); expect(f.runner.readiness).toHaveBeenCalledWith( { tier: 'service-ready', service: 'storybook-channel', descriptor: 'agent.json' }, - 420_000, + defaultRetainedReadyTimeoutMs, expect.any(AbortSignal), ); expect(JSON.parse(fs.readFileSync(channel.env.STORYBOOK_DRIVER_MANIFEST, 'utf8')).agent.descriptor).toBe('agent.json'); @@ -459,10 +467,10 @@ describe('DesktopStorybookCli', () => { expect(ready).toMatchObject({ targetId: f.target.id, agentDescriptor: 'agent.json', serviceReady: { service: 'storybook-channel' } }); expect(ready.integration.details.services).toEqual(platform === 'windows' ? ['metro', 'storybook-channel'] : ['storybook-channel']); expect(JSON.parse(channel.env.STORYBOOK_OWNED_APPLICATION)).toEqual(owned); - expect(channel.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS).toBe('300000'); + expect(channel.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS).toBe(String(DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS)); expect(f.runner.readiness).toHaveBeenCalledWith( { tier: 'service-ready', service: 'storybook-channel', descriptor: 'agent.json' }, - 420_000, + defaultRetainedReadyTimeoutMs, expect.any(AbortSignal), ); const { arguments: _arguments, ...identity } = owned as typeof owned & { arguments?: string[] }; @@ -618,15 +626,17 @@ describe('DesktopStorybookCli', () => { const phases = f.runner.background.filter((command) => command.env.STORYBOOK_OWNED_APPLICATION); expect(phases).toHaveLength(platform === 'windows' ? 2 : 1); expect(phases.every((command) => command.env.STORYBOOK_DRIVER_HANDOFF && command.env.STORYBOOK_DRIVER_MANIFEST)).toBe(true); - expect(phases.every((command) => command.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS === '300000')).toBe(true); + expect( + phases.every((command) => command.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS === String(DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS)), + ).toBe(true); expect(f.runner.readiness).toHaveBeenCalledWith( { tier: 'service-ready', service: 'storybook-channel-listener' }, - 120_000, + DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, expect.any(AbortSignal), ); expect(f.runner.readiness).toHaveBeenCalledWith( { tier: 'service-ready', service: 'storybook-channel' }, - 420_000, + defaultRetainedReadyTimeoutMs, expect.any(AbortSignal), ); if (platform === 'macos') { @@ -652,7 +662,7 @@ describe('DesktopStorybookCli', () => { ['sidebar-header', 'sidebar-resize', 'addons-panel-header', 'addons-resize', 'addon-storybook-actions-panel'].map( (suffix) => `fixture-win32-${suffix}`, ), - 30_000, + STORYBOOK_NATIVE_UI_TIMEOUT_MS, ); expect(JSON.parse(phases[0].env.STORYBOOK_OWNED_APPLICATION).arguments).toContain('crashOnRedBox'); expect(f.runner.foreground.some((command) => command.command === 'pwsh')).toBe(false); @@ -670,16 +680,29 @@ describe('DesktopStorybookCli', () => { expect(f.events.at(-1)).toBe('lease-withdrawn'); }); + const fixtureStartupTimeoutMs = 1_000; + const fixtureInitialRenderTimeoutMs = 45_000; + const fixtureEnvironmentRenderTimeoutMs = 65_000; test.each([ - ['explicit render budget', { startupTimeoutMs: 1_000, initialRenderTimeoutMs: 45_000 }, {}, 45_000], - ['channel environment override', { startupTimeoutMs: 1_000 }, { STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: '65000' }, 65_000], + [ + 'explicit render budget', + { startupTimeoutMs: fixtureStartupTimeoutMs, initialRenderTimeoutMs: fixtureInitialRenderTimeoutMs }, + {}, + fixtureInitialRenderTimeoutMs, + ], + [ + 'channel environment override', + { startupTimeoutMs: fixtureStartupTimeoutMs }, + { STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: String(fixtureEnvironmentRenderTimeoutMs) }, + fixtureEnvironmentRenderTimeoutMs, + ], [ 'explicit render budget over environment', - { startupTimeoutMs: 1_000, initialRenderTimeoutMs: 45_000 }, - { STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: '65000' }, - 45_000, + { startupTimeoutMs: fixtureStartupTimeoutMs, initialRenderTimeoutMs: fixtureInitialRenderTimeoutMs }, + { STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: String(fixtureEnvironmentRenderTimeoutMs) }, + fixtureInitialRenderTimeoutMs, ], - ['legacy startup fallback', { startupTimeoutMs: 1_000 }, {}, 1_000], + ['legacy startup fallback', { startupTimeoutMs: fixtureStartupTimeoutMs }, {}, fixtureStartupTimeoutMs], ] as const)('aligns native smoke listener and channel deadlines: %s', async (_label, timeouts, env, renderMs) => { const f = nativeSmokeFixture('macos'); const config = makeConfig({ @@ -694,12 +717,12 @@ describe('DesktopStorybookCli', () => { }); expect(f.runner.readiness).toHaveBeenCalledWith( { tier: 'service-ready', service: 'storybook-channel-listener' }, - 1_000, + fixtureStartupTimeoutMs, expect.any(AbortSignal), ); expect(f.runner.readiness).toHaveBeenCalledWith( { tier: 'service-ready', service: 'storybook-channel' }, - 1_000 + renderMs, + fixtureStartupTimeoutMs + renderMs, expect.any(AbortSignal), ); }); @@ -726,12 +749,12 @@ describe('DesktopStorybookCli', () => { void smoke.catch(() => undefined); try { await observing; - await jest.advanceTimersByTimeAsync(120_001); + await jest.advanceTimersByTimeAsync(DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS + 1); expect(f.events).not.toContain('attach'); expect(f.events).not.toContain('stop:storybook-stories'); if (expire) { - await jest.advanceTimersByTimeAsync(300_000); - await expect(smoke).rejects.toThrow('Channel initial render exceeded 420000ms'); + await jest.advanceTimersByTimeAsync(DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS); + await expect(smoke).rejects.toThrow(`Channel initial render exceeded ${defaultRetainedReadyTimeoutMs}ms`); } else { releaseRender(); await smoke; @@ -1459,7 +1482,7 @@ describe('DesktopStorybookCli', () => { expect(maintained).toEqual([manifestPath]); expect(runner.readiness).toHaveBeenCalledWith( { tier: 'service-ready', service: 'storybook-channel', descriptor: 'custom.json' }, - 120_000, + DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, expect.any(AbortSignal), ); expect(fs.existsSync(readyPath)).toBe(false); @@ -1899,17 +1922,19 @@ describe('DesktopStorybookCli', () => { } }); - test.each([undefined, 150_000])( + test.each([undefined, DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS + 30_000])( 'allows a cold development bundle to take longer than the service startup timeout (initialRenderTimeoutMs: %s)', async (initialRenderTimeoutMs) => { jest.useFakeTimers(); try { const runner = new RecordingRunner(); - const readyAt = Date.now() + 130_000; + const coldRenderDelayMs = DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS + 10_000; + const retryDelayMs = 1000; + const readyAt = Date.now() + coldRenderDelayMs; const output = { write: jest.fn() }; const fetch: typeof globalThis.fetch = async (input) => { if (String(input).includes('select-story-sync')) { - await new Promise((resolve) => setTimeout(resolve, 1000)); + await new Promise((resolve) => setTimeout(resolve, retryDelayMs)); if (Date.now() < readyAt) { return new Response(JSON.stringify({ error: 'Storybook runtime is not connected yet.' }), { status: 408 }); } @@ -1925,7 +1950,7 @@ describe('DesktopStorybookCli', () => { ); const result = cli.smoke('macos').catch((error: unknown) => error); - await jest.advanceTimersByTimeAsync(131_000); + await jest.advanceTimersByTimeAsync(coldRenderDelayMs + retryDelayMs); expect(await result).toBeUndefined(); expect(output.write).toHaveBeenCalledWith('1 story rendered\n'); expect(runner.stopped).toBe(2); @@ -1935,11 +1960,16 @@ describe('DesktopStorybookCli', () => { }, ); + const shortNavigationTimeoutMs = 2000; test.each([ - { smokeOptions: {}, firstRenders: false, timeoutMs: 300_000 }, - { smokeOptions: { startupTimeoutMs: 2000 }, firstRenders: false, timeoutMs: 2000 }, - { smokeOptions: { startupTimeoutMs: 10_000, initialRenderTimeoutMs: 2000 }, firstRenders: false, timeoutMs: 2000 }, - { smokeOptions: { initialRenderTimeoutMs: 60_000 }, firstRenders: true, timeoutMs: 15_000 }, + { smokeOptions: {}, firstRenders: false, timeoutMs: DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS }, + { smokeOptions: { startupTimeoutMs: shortNavigationTimeoutMs }, firstRenders: false, timeoutMs: shortNavigationTimeoutMs }, + { + smokeOptions: { startupTimeoutMs: 10_000, initialRenderTimeoutMs: shortNavigationTimeoutMs }, + firstRenders: false, + timeoutMs: shortNavigationTimeoutMs, + }, + { smokeOptions: { initialRenderTimeoutMs: 60_000 }, firstRenders: true, timeoutMs: DEFAULT_STORYBOOK_STORY_RENDER_TIMEOUT_MS }, ])('bounds navigation and preserves the cause (%j)', async ({ smokeOptions, firstRenders, timeoutMs }) => { jest.useFakeTimers(); try { @@ -2125,7 +2155,7 @@ describe('DesktopStorybookCli', () => { expect(fetch.mock.calls.some(([input]) => input.toString().includes('select-story-sync'))).toBe(false); expect(waitForMacOSApplicationLease).toHaveBeenCalledWith( path.join(storybookRoot, 'storybook-desktop.generated', 'driver-manifest.macos.json'), - { timeoutMs: 120_000, signal: expect.any(AbortSignal), createStorage: createFixturePrivateStorage }, + { timeoutMs: DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, signal: expect.any(AbortSignal), createStorage: createFixturePrivateStorage }, ); expect(runSmokeTests).toHaveBeenCalledWith({ config: cli.config, diff --git a/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.ts b/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.ts index c63d2dd8de..ce76f04293 100644 --- a/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.ts +++ b/packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.ts @@ -22,6 +22,12 @@ import { formatDesktopReporterValue } from '@fluentui-react-native/desktop-drive import type { ResolvedDesktopReporterMode } from '@fluentui-react-native/desktop-driver/cli/reporter'; import { writeDesktopStorybookFailure, type DesktopStorybookErrorOutput } from '../../config/diagnostics.cjs'; +import { + DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, + DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS, + DEFAULT_STORYBOOK_STORY_RENDER_TIMEOUT_MS, + STORYBOOK_NATIVE_UI_TIMEOUT_MS, +} from '../../config/timeouts.cjs'; import { buildNativeDesktopDriver, resolveNativeDesktopDriver, @@ -92,8 +98,10 @@ function retainedStartupTimeouts(smoke: DesktopSmokeOptions | undefined, server: server.env?.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS ?? process.env.STORYBOOK_INITIAL_RENDER_TIMEOUT_MS, ); return { - startupTimeoutMs: smoke?.startupTimeoutMs ?? 120_000, - initialRenderTimeoutMs: smoke?.initialRenderTimeoutMs ?? (configuredRenderTimeout || (smoke?.startupTimeoutMs ?? 300_000)), + startupTimeoutMs: smoke?.startupTimeoutMs ?? DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, + initialRenderTimeoutMs: + smoke?.initialRenderTimeoutMs ?? + (configuredRenderTimeout || (smoke?.startupTimeoutMs ?? DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS)), }; } @@ -282,7 +290,7 @@ export class DesktopStorybookCli { platform, ), ); - await this.services.ready('storybook-channel', loopbackUrl(port, '/index.json'), 120_000, this.fetch); + await this.services.ready('storybook-channel', loopbackUrl(port, '/index.json'), DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, this.fetch); if (this.reporterMode === 'json' || this.reporterMode === 'ndjson') { this.writeStructured({ type: 'ready', host, port, runId: this.reporter?.runId }); } else { @@ -464,13 +472,13 @@ export class DesktopStorybookCli { const channelReady = await this.services.readyProcess( 'storybook-channel', { tier: 'service-ready', service: 'storybook-channel', ...(agentManifest ? { descriptor: agentManifest.descriptor } : {}) }, - timeouts ? timeouts.startupTimeoutMs + timeouts.initialRenderTimeoutMs : 120_000, + timeouts ? timeouts.startupTimeoutMs + timeouts.initialRenderTimeoutMs : DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, ); assertSameReadinessProducer(channelListener, channelReady); // Windows and Win32 publish the retained app's lease inside the channel before it reports service readiness. if (ownedApplication && platform === 'macos') { await this.waitForMacOSApplicationLease(driverManifest.path, { - timeoutMs: 120_000, + timeoutMs: DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, signal: this.services.controller.signal, createStorage: this.createPrivateStorage, }); @@ -1157,7 +1165,7 @@ export class DesktopStorybookCli { const stopStories = await startPhase('stories'); if (native.kind === 'macos') { await this.waitForMacOSApplicationLease(instance.driverManifestPath!, { - timeoutMs: smoke.startupTimeoutMs ?? 120_000, + timeoutMs: smoke.startupTimeoutMs ?? DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, signal: this.services.controller.signal, createStorage: this.createPrivateStorage, }); @@ -1169,7 +1177,7 @@ export class DesktopStorybookCli { ['sidebar-header', 'sidebar-resize', 'addons-panel-header', 'addons-resize', 'addon-storybook-actions-panel'].map( (suffix) => `${native.testIDPrefix}-win32-${suffix}`, ), - 30_000, + STORYBOOK_NATIVE_UI_TIMEOUT_MS, ), ); } @@ -1191,7 +1199,10 @@ export class DesktopStorybookCli { await stopStories(); await startPhase('tests'); await traverse((traversal) => - traversal.select(index.entries[0].id, smoke.initialRenderTimeoutMs ?? smoke.startupTimeoutMs ?? 300_000), + traversal.select( + index.entries[0].id, + smoke.initialRenderTimeoutMs ?? smoke.startupTimeoutMs ?? DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS, + ), ); } await this.services.guard( @@ -1303,7 +1314,7 @@ export class DesktopStorybookCli { throw new Error('The macOS authored-test lifecycle did not create a Desktop Driver manifest.'); } await this.waitForMacOSApplicationLease(instance.driverManifestPath, { - timeoutMs: smoke.startupTimeoutMs ?? 120_000, + timeoutMs: smoke.startupTimeoutMs ?? DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, signal: this.services.controller.signal, createStorage: this.createPrivateStorage, }); @@ -1590,7 +1601,7 @@ export class DesktopStorybookCli { operation: DesktopOperation, serverUrl: string, settleMs: number, - initialRenderTimeoutMs = 300_000, + initialRenderTimeoutMs = DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS, select = (storyId: string, timeoutMs: number) => this.selectStory(serverUrl, storyId, timeoutMs), requiredStoryIds: readonly string[] = [], failFast = false, @@ -1618,7 +1629,7 @@ export class DesktopStorybookCli { this.reporter?.emit({ kind: 'scope-start', payload: { label: `Render ${id}`, category: 'worker' } }, testId, { story: id }, testId); this.reporter?.emit({ kind: 'test-start', payload: { testId, title: `Render ${id}`, attempt: 0 } }, testId, { story: id }); try { - await select(id!, entryIndex === 0 ? initialRenderTimeoutMs : 15_000); + await select(id!, entryIndex === 0 ? initialRenderTimeoutMs : DEFAULT_STORYBOOK_STORY_RENDER_TIMEOUT_MS); consecutiveFailures = 0; if (settleMs > 0) { await delay(settleMs); @@ -1693,7 +1704,12 @@ export class DesktopStorybookCli { throw new Error(`Timed out after ${timeoutMs} ms waiting for Storybook story "${storyId}" to render.`, { cause: lastError }); } - private async waitForUrl(url: string, process: RunningDesktopCommand, timeoutMs = 120_000, init?: RequestInit): Promise { + private async waitForUrl( + url: string, + process: RunningDesktopCommand, + timeoutMs = DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, + init?: RequestInit, + ): Promise { const deadline = Date.now() + timeoutMs; let lastError: unknown; @@ -1732,14 +1748,20 @@ export class DesktopStorybookCli { process: RunningDesktopCommand, service: string, listener: { processId: number; incarnation: string }, - timeoutMs = 120_000, + timeoutMs = DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, ): Promise { if (!process.waitForListenerProbe) throw new Error('Owned HTTP readiness requires a correlated listener probe.'); const probed = await process.waitForListenerProbe(service, timeoutMs, this.services.controller.signal); assertSameReadinessProducer(listener, probed); } - private async waitForOwnedService(id: string, service: string, url: string, process: RunningDesktopCommand, timeoutMs = 120_000) { + private async waitForOwnedService( + id: string, + service: string, + url: string, + process: RunningDesktopCommand, + timeoutMs = DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS, + ) { const headers = this.listenerProbeHeaders(process); const listener = await this.services.readyProcess(id, { tier: 'service-ready', service }, timeoutMs); await this.services.ready(