Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/reliable-retained-storybook-startup.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion packages/agentic/storybook-desktop/config/server.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions packages/agentic/storybook-desktop/config/timeouts.cjs
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 4 additions & 0 deletions packages/agentic/storybook-desktop/config/timeouts.d.cts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions packages/agentic/storybook-desktop/references/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions packages/agentic/storybook-desktop/references/readiness.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
151 changes: 140 additions & 11 deletions packages/agentic/storybook-desktop/src/cli/DesktopStorybookCli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,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'];
Expand Down Expand Up @@ -364,6 +373,12 @@ describe('DesktopStorybookCli', () => {
executablePath: '/Fixture/Host.app/Contents/MacOS/Host',
windowTitle: 'Agentic Components Storybook',
});
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' },
defaultRetainedReadyTimeoutMs,
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({
Expand Down Expand Up @@ -452,6 +467,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(String(DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS));
expect(f.runner.readiness).toHaveBeenCalledWith(
{ tier: 'service-ready', service: 'storybook-channel', descriptor: 'agent.json' },
defaultRetainedReadyTimeoutMs,
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);
Expand Down Expand Up @@ -605,6 +626,19 @@ 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 === String(DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS)),
).toBe(true);
expect(f.runner.readiness).toHaveBeenCalledWith(
{ tier: 'service-ready', service: 'storybook-channel-listener' },
DEFAULT_STORYBOOK_STARTUP_TIMEOUT_MS,
expect.any(AbortSignal),
);
expect(f.runner.readiness).toHaveBeenCalledWith(
{ tier: 'service-ready', service: 'storybook-channel' },
defaultRetainedReadyTimeoutMs,
expect.any(AbortSignal),
);
if (platform === 'macos') {
expect(JSON.parse(phases[0].env.STORYBOOK_OWNED_APPLICATION)).toEqual({
bundleIdentifier: cli.instance.bundleIdentifier,
Expand All @@ -628,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);
Expand All @@ -646,6 +680,94 @@ 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: fixtureStartupTimeoutMs, initialRenderTimeoutMs: fixtureInitialRenderTimeoutMs },
{},
fixtureInitialRenderTimeoutMs,
],
[
'channel environment override',
{ startupTimeoutMs: fixtureStartupTimeoutMs },
{ STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: String(fixtureEnvironmentRenderTimeoutMs) },
fixtureEnvironmentRenderTimeoutMs,
],
[
'explicit render budget over environment',
{ startupTimeoutMs: fixtureStartupTimeoutMs, initialRenderTimeoutMs: fixtureInitialRenderTimeoutMs },
{ STORYBOOK_INITIAL_RENDER_TIMEOUT_MS: String(fixtureEnvironmentRenderTimeoutMs) },
fixtureInitialRenderTimeoutMs,
],
['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({
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' },
fixtureStartupTimeoutMs,
expect.any(AbortSignal),
);
expect(f.runner.readiness).toHaveBeenCalledWith(
{ tier: 'service-ready', service: 'storybook-channel' },
fixtureStartupTimeoutMs + 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<void>((resolve) => (releaseRender = resolve));
const observing = new Promise<void>((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(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(DEFAULT_STORYBOOK_INITIAL_RENDER_TIMEOUT_MS);
await expect(smoke).rejects.toThrow(`Channel initial render exceeded ${defaultRetainedReadyTimeoutMs}ms`);
} 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'],
Expand Down Expand Up @@ -1360,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);
Expand Down Expand Up @@ -1800,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 });
}
Expand All @@ -1826,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);
Expand All @@ -1836,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 {
Expand Down Expand Up @@ -2026,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,
Expand Down
Loading
Loading