Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Derive the smoke window-wait bound and share it across terminal specs (#4951)
Nick Gomeze675c6de4864
diff --git a/packages/desktop/playwright.config.ts b/packages/desktop/playwright.config.tsindex 1327543ec..ab16aec2c 100644--- a/packages/desktop/playwright.config.ts+++ b/packages/desktop/playwright.config.ts@@ -1,4 +1,5 @@import { defineConfig } from '@playwright/test';+import { ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS } from './tests/smoke/_helpers/launch-desktop';/*** Desktop-package Playwright config.@@ -29,17 +30,7 @@ export default defineConfig({// iteration. Excluding them at the matcher level codifies the convention:// CI never runs them even if the smoke env var leaks in.testIgnore: ['**/_*.e2e.ts'],- // CI gets 150s per test to accommodate cumulative inner timeouts on the- // assertion path (helper waits + window-show + poll budgets can sum to- // 80-140s on a slow macos-latest runner. 150s gives 10s headroom over the heaviest standard-- // pattern test (create-new-project.e2e.ts at 140s). Tests that- // structurally exceed this (e.g. qa-create-new-extended.e2e.ts which- // launches Electron twice for cross-restart state checks) opt into a- // larger budget with `test.setTimeout`, so the structural reason is- // local to the test that needs it. Local dev keeps- // 60s so real regressions surface immediately. Same CI-vs-local- // divergence shape as `retries: process.env.CI ? 2 : 0` below.- timeout: process.env.CI ? 150_000 : 60_000,+ timeout: process.env.CI ? 150_000 : ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS,// Retries for CI macOS runner flake. Tests that hang or time out from// runner-load contention (Electron Helper XPC delays, slow loadFile// resolution, slow window show under vibrancy + transparent: true) getdiff --git a/packages/desktop/playwright.packaged.config.ts b/packages/desktop/playwright.packaged.config.tsindex 20864c869..6845a9d0e 100644--- a/packages/desktop/playwright.packaged.config.ts+++ b/packages/desktop/playwright.packaged.config.ts@@ -1,4 +1,5 @@import { defineConfig } from '@playwright/test';+import { ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS } from './tests/smoke/_helpers/launch-desktop';/*** Packaged-app Playwright config — the release-gate tier.@@ -69,7 +70,7 @@ export default defineConfig({// Matches the unpackaged config's CI budget. A packaged launch is if anything// slower than `electron out/main/index.js` (Gatekeeper assessment, first-run// dyld cache warm-up), so a tighter budget would only add flake.- timeout: process.env.CI ? 150_000 : 60_000,+ timeout: process.env.CI ? 150_000 : ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS,retries: process.env.CI ? 2 : 0,failOnFlakyTests: false,workers: 1,diff --git a/packages/desktop/tests/smoke/_helpers/launch-desktop.ts b/packages/desktop/tests/smoke/_helpers/launch-desktop.tsindex 0cb77547b..4f0894088 100644--- a/packages/desktop/tests/smoke/_helpers/launch-desktop.ts+++ b/packages/desktop/tests/smoke/_helpers/launch-desktop.ts@@ -2,6 +2,7 @@ import { existsSync } from 'node:fs';import { basename, dirname, join, resolve } from 'node:path';import { fileURLToPath } from 'node:url';import type { _electron as electron } from '@playwright/test';+import { readinessGiveUpBoundMs } from './launch-readiness';type ElectronLaunchOptions = NonNullable<Parameters<typeof electron.launch>[0]>;export type SmokeLaunchEnv = NonNullable<ElectronLaunchOptions['env']>;@@ -85,6 +86,13 @@ export interface DesktopLaunchOptions {export const DEFAULT_LAUNCH_TIMEOUT_MS = 30_000;+export const SETUP_BEFORE_FIRST_READINESS_WAIT_MS = 0;++export const ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS =+ DEFAULT_LAUNCH_TIMEOUT_MS ++ readinessGiveUpBoundMs({ path: 'fork' }) ++ SETUP_BEFORE_FIRST_READINESS_WAIT_MS;+export function desktopLaunchOptions(input: DesktopLaunchOptionsInput = {}): DesktopLaunchOptions {const target = input.target ?? resolveDesktopTarget();const extraArgs = input.args ?? [];diff --git a/packages/desktop/tests/smoke/_helpers/launch-readiness.test.ts b/packages/desktop/tests/smoke/_helpers/launch-readiness.test.tsindex 6de3c46b5..8c0a72715 100644--- a/packages/desktop/tests/smoke/_helpers/launch-readiness.test.ts+++ b/packages/desktop/tests/smoke/_helpers/launch-readiness.test.ts@@ -53,6 +53,7 @@ import {type ReadyWaitRecord,readBootLog,readBootLogLines,+ readinessWorstCaseMs,readyWaitsFor,rememberLaunchHome,sinceLastAdvancementMs,@@ -63,6 +64,21 @@ import {waitForReadySignal,waitForWindowByMode,} from './launch-readiness.ts';+import {+ type GiveUp,+ giveUpOf,+ LAUNCH_REACHING_THE_CEILING_OF,+ launchBootingAfterTheWaitsFirstRead,+ launchBootingAfterTheWaitsFirstReadBesideTheDayBeforesLog,+ pollIntervalOf,+ READINESS_CALLS,+ READINESS_PATHS,+ reachOf,+ utilityForkBootingAfterTheWaitsFirstRead,+ utilityForkTheLastStageKeptAlive,+ utilityForkTheLastStageKeptAliveBesideTheDayBeforesLog,+ utilityForkWhoseFirstBeatLandsOnTheLastStagePoll,+} from './readiness-reach.test-helper.ts';function markLine(phase: WaterfallPhase, elapsedMs: number, time: string): string {return JSON.stringify({ time, ...startupMarkLine(phase, elapsedMs) });@@ -3079,3 +3095,166 @@ describe('the longest wait startup stages can buy is the static cap plus one sta);});});++describe("a launch the app narrates inside its own contract still reaches the app's own verdict", () => {+ for (const { label, options } of READINESS_CALLS) {+ it(label, async () => {+ const launch = utilityForkTheLastStageKeptAlive(options);+ const reach = await reachOf(launch, 2 * launch.appVerdictAtMs);+ expect(reach).toMatchObject({ gaveUpAtMs: expect.any(Number) });+ expect('gaveUpAtMs' in reach ? reach.gaveUpAtMs : Number.NaN).toBeGreaterThan(+ launch.appVerdictAtMs,+ );+ expect('message' in reach ? reach.message : '').toContain(DESKTOP_OPEN_PROJECT_FAILED_EVENT);+ });+ }+});++describe('the latest deadline the helper arms is the shared worst case of the path the launch takes', () => {+ for (const { label, options } of READINESS_CALLS) {+ for (const path of READINESS_PATHS) {+ it(`${label}, on the ${path} path`, async () => {+ const worstCaseMs = readinessWorstCaseMs({ path, ...options });+ const launch = LAUNCH_REACHING_THE_CEILING_OF[path](options);+ const giveUp = giveUpOf(await reachOf(launch, 2 * worstCaseMs), launch);+ expect(giveUp.decidingCapMs, launch.name).toBe(worstCaseMs);+ expect(giveUp.gaveUpAtMs).toBeGreaterThanOrEqual(worstCaseMs);+ expect(giveUp.gaveUpAtMs).toBeLessThan(worstCaseMs + pollIntervalOf(options));+ });+ }+ }+});++describe("a launch inside the app's own contract, read on a poll out of phase with the app's heartbeat, runs to the app's own last grant", () => {+ const launch = utilityForkWhoseFirstBeatLandsOnTheLastStagePoll({ pollMs: BOOT_LOG_POLL_MS + 3 });+ const forkWorstCaseMs = readinessWorstCaseMs({ path: 'fork', ...launch.options });++ it("arms the grant the app's last beat bought, and gives up naming the app's verdict", async () => {+ const giveUp = giveUpOf(await reachOf(launch, 2 * forkWorstCaseMs), launch);+ expect(giveUp.declaredGrantMs).toBeDefined();+ expect(giveUp.decidingCapMs).toBe(giveUp.declaredGrantMs);+ expect(giveUp.gaveUpAtMs).toBeGreaterThan(launch.appVerdictAtMs);+ expect(giveUp.message).toContain(DESKTOP_OPEN_PROJECT_FAILED_EVENT);+ });++ it("finds that grant inside the fork path's worst case", async () => {+ const giveUp = giveUpOf(await reachOf(launch, 2 * forkWorstCaseMs), launch);+ expect(giveUp.declaredGrantMs).toBeDefined();+ expect(giveUp.declaredGrantMs).toBeLessThanOrEqual(forkWorstCaseMs);+ });+});++describe("a launch inside the app's own contract, read on polls further out of phase with the app's heartbeat, still runs to the app's own last grant", () => {+ for (const pollMs of [+ BOOT_LOG_POLL_MS + 20,+ BOOT_LOG_POLL_MS + 60,+ BOOT_LOG_POLL_MS + 83,+ BOOT_LOG_POLL_MS + 150,+ 2 * BOOT_LOG_POLL_MS - 1,+ ]) {+ it(`read every ${pollMs}ms, arms the grant the app's last beat bought, gives up naming the app's verdict, and finds that grant inside the fork path's worst case`, async () => {+ const launch = utilityForkWhoseFirstBeatLandsOnTheLastStagePoll({ pollMs });+ const forkWorstCaseMs = readinessWorstCaseMs({ path: 'fork', ...launch.options });+ const giveUp = giveUpOf(await reachOf(launch, 2 * forkWorstCaseMs), launch);+ expect(giveUp.declaredGrantMs, `read every ${pollMs}ms`).toBeDefined();+ expect(giveUp.decidingCapMs, `read every ${pollMs}ms`).toBe(giveUp.declaredGrantMs);+ expect(giveUp.gaveUpAtMs).toBeGreaterThan(launch.appVerdictAtMs);+ expect(giveUp.message).toContain(DESKTOP_OPEN_PROJECT_FAILED_EVENT);+ expect(giveUp.declaredGrantMs).toBeLessThanOrEqual(forkWorstCaseMs);+ });+ }+});++describe('a launch that boots after the wait first reads a home an earlier launch left its log in', () => {+ const verdictOf = ({ gaveUpAtMs, decidingCapMs, advancements }: GiveUp) => ({+ gaveUpAtMs,+ decidingCapMs,+ advancements,+ });++ it('decides as it would alone in that home, and records only its own stages', async () => {+ const alone = launchBootingAfterTheWaitsFirstRead({}, { sharedWithAnEarlierLaunch: false });+ const shared = launchBootingAfterTheWaitsFirstRead({}, { sharedWithAnEarlierLaunch: true });+ const aloneGiveUp = giveUpOf(await reachOf(alone, REPLAY_WATCHDOG_MS), alone);+ const sharedGiveUp = giveUpOf(await reachOf(shared, REPLAY_WATCHDOG_MS), shared);+ expect(verdictOf(sharedGiveUp)).toEqual(verdictOf(aloneGiveUp));+ expect(sharedGiveUp.advancements.map((advancement) => advancement.event)).toEqual(+ shared.ownStageEvents,+ );+ expect(aloneGiveUp.decidingCapMs).toBe(+ readinessWorstCaseMs({ path: 'packaged', ...alone.options }),+ );+ });++ it("decides as it would alone when the earlier launch stopped inside its utility wait, and reaches the app's own verdict", async () => {+ const alone = utilityForkBootingAfterTheWaitsFirstRead(+ {},+ { sharedWithALaunchThatStoppedInItsUtilityWait: false },+ );+ const shared = utilityForkBootingAfterTheWaitsFirstRead(+ {},+ { sharedWithALaunchThatStoppedInItsUtilityWait: true },+ );+ const aloneGiveUp = giveUpOf(await reachOf(alone, REPLAY_WATCHDOG_MS), alone);+ const sharedGiveUp = giveUpOf(await reachOf(shared, REPLAY_WATCHDOG_MS), shared);+ expect(verdictOf(sharedGiveUp)).toEqual(verdictOf(aloneGiveUp));+ expect(sharedGiveUp.gaveUpAtMs).toBeGreaterThan(shared.appVerdictAtMs);+ expect(sharedGiveUp.message).toContain(DESKTOP_OPEN_PROJECT_FAILED_EVENT);+ });+});++describe('a wait over a home that also holds the log a launch the day before left decides as it would had every poll read both logs', () => {+ const recordOf = ({+ gaveUpAtMs,+ reason,+ decidingCapMs,+ declaredGrantMs,+ advancements,+ }: GiveUp) => ({+ gaveUpAtMs,+ reason,+ decidingCapMs,+ declaredGrantMs,+ advancements,+ });++ it("reads past a poll that could not read the launch's own log, and still reaches the app's own verdict", async () => {+ const alone = utilityForkTheLastStageKeptAlive({});+ const beside = utilityForkTheLastStageKeptAliveBesideTheDayBeforesLog(+ {},+ { unreadableOnOnePollOfTheDeclaredGrant: false },+ );+ const glitched = utilityForkTheLastStageKeptAliveBesideTheDayBeforesLog(+ {},+ { unreadableOnOnePollOfTheDeclaredGrant: true },+ );+ const watchMs = 2 * alone.appVerdictAtMs;+ const aloneGiveUp = giveUpOf(await reachOf(alone, watchMs), alone);+ const besideGiveUp = giveUpOf(await reachOf(beside, watchMs), beside);+ const glitchedGiveUp = giveUpOf(await reachOf(glitched, watchMs), glitched);+ expect(recordOf(besideGiveUp)).toEqual(recordOf(aloneGiveUp));+ expect(recordOf(glitchedGiveUp)).toEqual(recordOf(besideGiveUp));+ expect(glitchedGiveUp.gaveUpAtMs).toBeGreaterThan(glitched.appVerdictAtMs);+ expect(glitchedGiveUp.message).toContain(DESKTOP_OPEN_PROJECT_FAILED_EVENT);+ });++ it('adopts a launch that boots while the earlier log cannot be read, and records only its own stages', async () => {+ const alone = launchBootingAfterTheWaitsFirstRead({}, { sharedWithAnEarlierLaunch: false });+ const beside = launchBootingAfterTheWaitsFirstReadBesideTheDayBeforesLog(+ {},+ { unreadableFromTheBootUntilMainsFirstHeartbeat: false },+ );+ const glitched = launchBootingAfterTheWaitsFirstReadBesideTheDayBeforesLog(+ {},+ { unreadableFromTheBootUntilMainsFirstHeartbeat: true },+ );+ const aloneGiveUp = giveUpOf(await reachOf(alone, REPLAY_WATCHDOG_MS), alone);+ const besideGiveUp = giveUpOf(await reachOf(beside, REPLAY_WATCHDOG_MS), beside);+ const glitchedGiveUp = giveUpOf(await reachOf(glitched, REPLAY_WATCHDOG_MS), glitched);+ expect(recordOf(besideGiveUp)).toEqual(recordOf(aloneGiveUp));+ expect(recordOf(glitchedGiveUp)).toEqual(recordOf(besideGiveUp));+ expect(glitchedGiveUp.advancements.map((advancement) => advancement.event)).toEqual(+ glitched.ownStageEvents,+ );+ });+});diff --git a/packages/desktop/tests/smoke/_helpers/launch-readiness.ts b/packages/desktop/tests/smoke/_helpers/launch-readiness.tsindex b411c3715..88f6180e4 100644--- a/packages/desktop/tests/smoke/_helpers/launch-readiness.ts+++ b/packages/desktop/tests/smoke/_helpers/launch-readiness.ts@@ -11,6 +11,7 @@ import {startupMarkLine,UTILITY_INIT_TIMEOUT_MS,} from '../../../src/shared/boot-narration.ts';+import type { DesktopLaunchMode } from './launch-desktop';export const BOOT_LOG_HEARTBEAT_MS = SPAWN_WAIT_HEARTBEAT_MS;@@ -24,6 +25,43 @@ export const UTILITY_TIMEOUT_OBSERVATION_MARGIN_MS = 5_000;const BOOT_LOG_TAIL_LINES = 12;+export type ReadinessPath = 'fork' | 'packaged';++export function readinessWorstCaseMs({+ path,+ capMs = BOOT_LOG_CAP_MS,+ stallMs = BOOT_LOG_STALL_MS,+}: {+ path: ReadinessPath;+ capMs?: number;+ stallMs?: number;+}): number {+ switch (path) {+ case 'packaged':+ return capMs + stallMs;+ case 'fork':+ return capMs + stallMs + UTILITY_INIT_TIMEOUT_MS + UTILITY_TIMEOUT_OBSERVATION_MARGIN_MS;+ }+}++export function readinessGiveUpBoundMs(options: {+ path: ReadinessPath;+ capMs?: number;+ stallMs?: number;+ pollMs?: number;+}): number {+ return readinessWorstCaseMs(options) + (options.pollMs ?? BOOT_LOG_POLL_MS);+}++const READINESS_PATH_BY_MODE = {+ unpackaged: 'fork',+ packaged: 'packaged',+} as const satisfies Record<DesktopLaunchMode, ReadinessPath>;++export function readinessPathOf(mode: DesktopLaunchMode): ReadinessPath {+ return READINESS_PATH_BY_MODE[mode];+}+export interface BootLogSnapshot {dir: string;exists: boolean;@@ -491,7 +529,9 @@ export interface ReadySignalOptions<T> {startDeadline?: (ms: number) => ReadyDeadline;isProbePending?: () => boolean;onCapExtended?: (capMs: number) => void;+ onDeclaredGrant?: (grantMs: number) => void;onAdvancement?: (advancement: LaunchAdvancement) => void;+ onNewLaunch?: () => void;}export interface ReadyDeadline {@@ -622,6 +662,8 @@ export async function waitForReadySignal<T>(options: ReadySignalOptions<T>): Prolet lastProgressAt = startedAt;let cursor = -1;const advancementSeen = new Set<string>();+ const bootLinesRead = new Set<string>();+ let launchBootLine: string | undefined;let lastLegibleReadAt: number | undefined;let lastStageRenewalAt: number | undefined;let snapshot = emptyBootLog(bootLogDirFor(options.home));@@ -667,7 +709,23 @@ export async function waitForReadySignal<T>(options: ReadySignalOptions<T>): ProlastProgressAt = readAt;}- for (const event of launchAdvancementPhases(snapshot.lines)) {+ const bootLinesNow = snapshot.lines.filter((line) => parseEvent(line) === DESKTOP_BOOT_EVENT);+ const bootLineNow = bootLinesNow.at(-1);+ const readsAnEarlierLaunch =+ bootLineNow !== undefined &&+ bootLineNow !== launchBootLine &&+ bootLinesRead.has(bootLineNow);+ for (const line of bootLinesNow) bootLinesRead.add(line);+ if (bootLineNow !== undefined && !readsAnEarlierLaunch) {+ if (launchBootLine !== undefined && bootLineNow !== launchBootLine) {+ advancementSeen.clear();+ grantedElapsedMs = Number.NEGATIVE_INFINITY;+ derivedDeadlineAt = undefined;+ options.onNewLaunch?.();+ }+ launchBootLine = bootLineNow;+ }+ for (const event of readsAnEarlierLaunch ? [] : launchAdvancementPhases(snapshot.lines)) {if (advancementSeen.has(event)) continue;advancementSeen.add(event);options.onAdvancement?.({ event, atMs: readAt - startedAt });@@ -678,7 +736,7 @@ export async function waitForReadySignal<T>(options: ReadySignalOptions<T>): Proif (classifyBootLog(snapshot) !== 'unreadable') lastLegibleReadAt = readAt;const declaredPhaseOpen = hasOpenDeclaredPhase(snapshot.lines);- if (declaredPhaseOpen) {+ if (declaredPhaseOpen && !readsAnEarlierLaunch) {const budget = newestDeclaredPhaseBudget(snapshot.lines);if (budget !== undefined && budget.elapsedMs > grantedElapsedMs) {grantedElapsedMs = budget.elapsedMs;@@ -687,14 +745,21 @@ export async function waitForReadySignal<T>(options: ReadySignalOptions<T>): ProUTILITY_TIMEOUT_OBSERVATION_MARGIN_MS,);derivedDeadlineAt = Math.max(derivedDeadlineAt ?? 0, now() + remaining);+ options.onDeclaredGrant?.(derivedDeadlineAt - startedAt);}}- const effectiveCapMs = Math.max(- capMs,- derivedDeadlineAt === undefined ? capMs : derivedDeadlineAt - startedAt,- lastStageRenewalAt === undefined- ? capMs- : Math.min(lastStageRenewalAt - startedAt + stallMs, capMs + stallMs),+ const effectiveCapMs = Math.min(+ readinessWorstCaseMs({ path: 'fork', capMs, stallMs }),+ Math.max(+ capMs,+ derivedDeadlineAt === undefined ? capMs : derivedDeadlineAt - startedAt,+ lastStageRenewalAt === undefined+ ? capMs+ : Math.min(+ lastStageRenewalAt - startedAt + stallMs,+ readinessWorstCaseMs({ path: 'packaged', capMs, stallMs }),+ ),+ ),);if (effectiveCapMs > armedCapMs) {deadline.cancel();@@ -790,6 +855,7 @@ export interface ReadyWaitRecord {what: string;elapsedMs: number;capMs: number;+ declaredGrantMs?: number;requestedCapMs: number;gaveUp: boolean;reason: ReadyWaitGiveUpReason;@@ -874,6 +940,7 @@ export async function waitForWindowByMode<TPage extends ModeProbePage>(const what = `${mode} window`;const capMs = options.capMs ?? BOOT_LOG_CAP_MS;let decidingCapMs = capMs;+ let declaredGrantMs: number | undefined;const startedAt = Date.now();const probeStates = new WeakMap<TPage, ModeProbeState>();let pendingProbeCount = 0;@@ -892,9 +959,16 @@ export async function waitForWindowByMode<TPage extends ModeProbePage>(onCapExtended: (extended) => {decidingCapMs = extended;},+ onDeclaredGrant: (grantMs) => {+ declaredGrantMs = grantMs;+ },onAdvancement: (advancement) => {advancements.push(advancement);},+ onNewLaunch: () => {+ advancements.length = 0;+ declaredGrantMs = undefined;+ },probe: async () => {const pages = app.windows();for (const page of pages) {@@ -962,6 +1036,7 @@ export async function waitForWindowByMode<TPage extends ModeProbePage>(what,elapsedMs: Date.now() - startedAt,capMs: decidingCapMs,+ ...(declaredGrantMs === undefined ? {} : { declaredGrantMs }),requestedCapMs: capMs,gaveUp: !succeeded,reason,diff --git a/packages/desktop/tests/smoke/_helpers/parse-timeouts.test.ts b/packages/desktop/tests/smoke/_helpers/parse-timeouts.test.tsindex 5209c1743..a9da0a290 100644--- a/packages/desktop/tests/smoke/_helpers/parse-timeouts.test.ts+++ b/packages/desktop/tests/smoke/_helpers/parse-timeouts.test.ts@@ -1,17 +1,112 @@-import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';-import { join } from 'node:path';-import { afterAll, beforeAll, describe, expect, test } from 'vitest';-import { BOOT_LOG_CAP_MS } from './launch-readiness';+import { basename, join } from 'node:path';+import { afterAll, afterEach, assert, beforeAll, describe, expect, test, vi } from 'vitest';+import { ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS, PACKAGED_APP_ENV } from './launch-desktop';+import { BOOT_LOG_POLL_MS, readinessGiveUpBoundMs } from './launch-readiness';import {+ type ChargeTier,extractHelperBudgets,extractReadinessBudgets,extractTestEntries,+ type FileAnalysis,parseNumericLiteral,parsePlaywrightConfigTimeout,parseTestFile,+ perTestBudgetMs,stripCommentsAndStrings,+ sumOfDeclaredBoundsMs,+ type TestEntry,} from './parse-timeouts';+import {+ giveUpOf,+ LAUNCH_REACHING_THE_CEILING_OF,+ pollIntervalOf,+ READINESS_CALLS,+ READINESS_PATHS,+ type ReadinessCallOptions,+ reachOf,+ SCRIPTED_LAUNCHES,+ type ScriptedLaunch,+} from './readiness-reach.test-helper';++function onlyCharge(chargesMs: readonly number[]): number {+ expect(chargesMs).toHaveLength(1);+ return chargesMs[0] ?? Number.NaN;+}++async function helperGiveUpMs(launch: ScriptedLaunch, chargedMs: number): Promise<number> {+ const reach = await reachOf(launch, 2 * chargedMs);+ expect(+ reach,+ `the readiness wait on ${launch.name}, watched for twice the ${chargedMs}ms charged`,+ ).toMatchObject({ gaveUpAtMs: expect.any(Number) });+ return 'gaveUpAtMs' in reach ? reach.gaveUpAtMs : Number.NaN;+}++function pinChargeToWhatTheHelperWaits(+ options: ReadinessCallOptions,+ chargedMs: () => number,+): void {+ for (const launch of SCRIPTED_LAUNCHES.map((script) => script(options))) {+ test(`the helper gives up no later than the charge on ${launch.name}`, async () => {+ const charge = chargedMs();+ expect(await helperGiveUpMs(launch, charge), launch.name).toBeLessThanOrEqual(charge);+ });+ }++ test('the charge covers the latest the helper waits, by at most one poll interval', async () => {+ const charge = chargedMs();+ const giveUpsMs: number[] = [];+ for (const script of SCRIPTED_LAUNCHES) {+ giveUpsMs.push(await helperGiveUpMs(script(options), charge));+ }+ const overchargeMs = charge - Math.max(...giveUpsMs);+ expect(overchargeMs).toBeGreaterThanOrEqual(0);+ expect(overchargeMs).toBeLessThanOrEqual(pollIntervalOf(options));+ });+}++type DeclaredTest = Parameters<typeof sumOfDeclaredBoundsMs>[0];++type PerTestScoring = Parameters<typeof perTestBudgetMs>[1];++const STEP_TIMEOUT_MS = 15_000;++const CONFIG_CI_OUTER_MS = 150_000;++const SMOKE_SPEC_DIR = join(__dirname, '..');++const CONSENT_DIALOG_SPEC = join(SMOKE_SPEC_DIR, 'consent-dialog.e2e.ts');++const DESKTOP_PLAYWRIGHT_CONFIG = join(SMOKE_SPEC_DIR, '..', '..', 'playwright.config.ts');++const fixtureRoot = mkdtempSync(join(tmpdir(), 'parse-timeouts-fixture-'));++let fixtureCount = 0;++afterAll(() => {+ rmSync(fixtureRoot, { recursive: true, force: true });+});++function writeFixture(suffix: string, lines: readonly string[]): string {+ fixtureCount += 1;+ const file = join(fixtureRoot, `fixture-${fixtureCount}${suffix}`);+ writeFileSync(file, `${lines.join('\n')}\n`);+ return file;+}++function lineHolding(lines: readonly string[], fragment: string): number {+ const index = lines.findIndex((line) => line.includes(fragment));+ if (index === -1) throw new Error(`the fixture has no line holding ${fragment}`);+ return index + 1;+}++function entryDeclaredAt(analysis: FileAnalysis, line: number): TestEntry {+ const entry = analysis.tests.find((candidate) => candidate.lineNumber === line);+ assert.isDefined(entry, `a test is declared at ${analysis.filePath}:${line}`);+ return entry;+}describe('parseNumericLiteral', () => {test('plain digits', () => {@@ -405,34 +500,654 @@ export default defineConfig({});describe('parseTestFile (real file, sanity)', () => {+ const consentDialog = join(__dirname, '..', 'consent-dialog.e2e.ts');+test('consent-dialog.e2e.ts yields helpers + tests', () => {- const fa = parseTestFile(join(__dirname, '..', 'consent-dialog.e2e.ts'));+ const fa = parseTestFile(consentDialog);expect(fa.helpers.length).toBeGreaterThan(0);expect(fa.tests.length).toBeGreaterThanOrEqual(3);const byName = new Map(fa.helpers.map((h) => [h.name, h.maxTimeoutMs]));expect(byName.get('launchApp') ?? 0).toBeGreaterThan(0);expect(byName.get('expandAdvancedSettings') ?? 0).toBeGreaterThan(0);- expect(byName.get('findWindowByMode') ?? 0).toBe(BOOT_LOG_CAP_MS);+ expect(byName.has('findWindowByMode')).toBe(true);+ });++ describe('consent-dialog.e2e.ts findWindowByMode, which wraps a plain waitForWindowByMode call', () => {+ pinChargeToWhatTheHelperWaits({}, () =>+ onlyCharge(+ parseTestFile(consentDialog)+ .helpers.filter((helper) => helper.name === 'findWindowByMode')+ .map((helper) => helper.maxTimeoutMs),+ ),+ );});});describe('readiness-helper attribution', () => {- test('scores the default cap for a plain waitForWindowByMode call', () => {- expect(extractReadinessBudgets('return waitForWindowByMode(app, mode);')).toEqual([- BOOT_LOG_CAP_MS,+ for (const { label, source, options } of READINESS_CALLS) {+ describe(label, () => {+ pinChargeToWhatTheHelperWaits(options, () => onlyCharge(extractReadinessBudgets(source)));+ });+ }++ describe('a waitForWindowByMode called straight from a test body', () => {+ const src = `test('x', async () => {\n await waitForWindowByMode(app, 'editor');\n});\n`;++ test('is counted once, and that one charge is the whole of the test cumulative', () => {+ const entry = extractTestEntries(src, [])[0];+ expect(entry?.directTimeoutsMs).toHaveLength(1);+ expect(entry?.cumulativeMs).toBe(entry?.directTimeoutsMs[0]);+ });++ pinChargeToWhatTheHelperWaits({}, () =>+ onlyCharge(extractTestEntries(src, [])[0]?.directTimeoutsMs ?? []),+ );+ });+});++describe('the charge on each path is the deadline the helper arms there, plus one poll', () => {+ for (const { label, source, options } of READINESS_CALLS) {+ describe(label, () => {+ for (const path of READINESS_PATHS) {+ test(`on the ${path} path, one poll short of the charge is the deadline the helper armed`, async () => {+ const charge = onlyCharge(extractReadinessBudgets(source, { path }));+ const launch = LAUNCH_REACHING_THE_CEILING_OF[path](options);+ const giveUp = giveUpOf(await reachOf(launch, 2 * charge), launch);+ expect(giveUp.decidingCapMs, launch.name).toBe(charge - pollIntervalOf(options));+ });+ }++ test('on the packaged path, the charge covers the stage-only give-up by at most one poll, and is the packaged give-up bound', async () => {+ const charge = onlyCharge(extractReadinessBudgets(source, { path: 'packaged' }));+ const overchargeMs =+ charge - (await helperGiveUpMs(LAUNCH_REACHING_THE_CEILING_OF.packaged(options), charge));+ expect(overchargeMs).toBeGreaterThanOrEqual(0);+ expect(overchargeMs).toBeLessThanOrEqual(pollIntervalOf(options));+ expect(charge).toBe(readinessGiveUpBoundMs({ path: 'packaged', ...options }));+ });++ test("a charge that names no path is the fork path's give-up bound", () => {+ expect(onlyCharge(extractReadinessBudgets(source))).toBe(+ readinessGiveUpBoundMs({ path: 'fork', ...options }),+ );+ });+ });+ }++ describe('consent-dialog.e2e.ts findWindowByMode', () => {+ const findWindowByModeCharge = (tier?: ChargeTier): number =>+ onlyCharge(+ parseTestFile(CONSENT_DIALOG_SPEC, tier)+ .helpers.filter((helper) => helper.name === 'findWindowByMode')+ .map((helper) => helper.maxTimeoutMs),+ );++ test('on the packaged path, covers the stage-only give-up by at most one poll, and is the packaged give-up bound', async () => {+ const charge = findWindowByModeCharge({ path: 'packaged' });+ const overchargeMs =+ charge - (await helperGiveUpMs(LAUNCH_REACHING_THE_CEILING_OF.packaged({}), charge));+ expect(overchargeMs).toBeGreaterThanOrEqual(0);+ expect(overchargeMs).toBeLessThanOrEqual(pollIntervalOf({}));+ expect(charge).toBe(readinessGiveUpBoundMs({ path: 'packaged' }));+ });++ test("with no path named, is the fork path's give-up bound", () => {+ expect(findWindowByModeCharge()).toBe(readinessGiveUpBoundMs({ path: 'fork' }));+ });+ });+});++describe("a call that names its own pollMs is charged the deadline the helper arms plus one of the call's own polls", () => {+ const options: ReadinessCallOptions = { pollMs: 2 * BOOT_LOG_POLL_MS };+ const source = `waitForWindowByMode(app, 'editor', { pollMs: ${2 * BOOT_LOG_POLL_MS} })`;++ for (const path of READINESS_PATHS) {+ test(`on the ${path} path, one of the call's own polls short of the charge is the deadline the helper armed, and the helper gives up before the charge`, async () => {+ const charge = onlyCharge(extractReadinessBudgets(source, { path }));+ const launch = LAUNCH_REACHING_THE_CEILING_OF[path](options);+ const giveUp = giveUpOf(await reachOf(launch, 2 * charge), launch);+ expect(giveUp.decidingCapMs, launch.name).toBe(charge - pollIntervalOf(options));+ expect(giveUp.gaveUpAtMs).toBeLessThan(charge);+ });+ }+});++describe('an options argument the charge cannot read is refused, never defaulted', () => {+ for (const unreadable of ['{ capMs: CAP }', '{ ...opts }', 'opts', '{ capMs }']) {+ test(`throws on ${unreadable}, naming it`, () => {+ expect(() =>+ extractReadinessBudgets(`waitForWindowByMode(app, 'editor', ${unreadable})`),+ ).toThrow(unreadable);+ });+ }++ for (const readable of ['{ home: homedir() }', "{ liveness: 'none' }"]) {+ test(`charges ${readable} the plain call's fork give-up bound`, () => {+ expect(extractReadinessBudgets(`waitForWindowByMode(app, 'editor', ${readable})`)).toEqual([+ readinessGiveUpBoundMs({ path: 'fork' }),+ ]);+ });+ }++ test("charges a liveness choice inside a helper, whose string the parser has blanked, the plain call's fork give-up bound", () => {+ const src = [+ 'async function findTerminalWindow(app: ElectronApplication): Promise<Page> {',+ " return waitForWindowByMode(app, 'terminal', { liveness: 'none' });",+ '}',+ ].join('\n');+ expect(extractHelperBudgets(src)).toEqual([+ { name: 'findTerminalWindow', maxTimeoutMs: readinessGiveUpBoundMs({ path: 'fork' }) },]);});+});++describe('a stallMs or pollMs the charge cannot read is refused as well, never defaulted', () => {+ for (const unreadable of [+ '{ stallMs: STALL }',+ '{ pollMs: POLL }',+ '{ stallMs }',+ '{ pollMs }',+ ]) {+ test(`throws on ${unreadable}, naming it`, () => {+ expect(() =>+ extractReadinessBudgets(`waitForWindowByMode(app, 'editor', ${unreadable})`),+ ).toThrow(unreadable);+ });+ }+});++describe('a test title holding another quote kind is audited once, and whole', () => {+ const TITLES = [+ {+ holding: 'an embedded backtick',+ header: 'test("runs `ok start` against a fresh home", async () => {',+ endsWith: /against a fresh home$/,+ },+ {+ holding: 'a conditional between single quotes',+ header: `test(\`renders the \${success ? 'success' : 'failure'} result\`, async () => {`,+ endsWith: /'failure'\} result$/,+ },+ {+ holding: 'an escaped quote of its own kind',+ header: "test('keeps the app\\'s own verdict', async () => {",+ endsWith: /s own verdict$/,+ },+ {+ holding: 'an escaped quote of its own kind against its closing quote',+ header: "test('quotes \\'ok\\'', async () => {",+ endsWith: /ok\\?'$/,+ },+ ];++ for (const { holding, header, endsWith } of TITLES) {+ test(`a title holding ${holding}`, () => {+ const entries = extractTestEntries(+ [header, ` await expect(page).toBeVisible({ timeout: ${STEP_TIMEOUT_MS} });`, '});'].join(+ '\n',+ ),+ [],+ );+ expect(entries).toHaveLength(1);+ expect(entries[0]?.cumulativeMs).toBe(STEP_TIMEOUT_MS);+ expect(entries[0]?.testName).toMatch(endsWith);+ });+ }- test('prefers an inline capMs over the default', () => {- expect(- extractReadinessBudgets("waitForWindowByMode(app, 'navigator', { capMs: 60_000 })"),- ).toEqual([60_000]);+ test('every smoke test whose title holds another quote kind is audited', () => {+ const quoteTitled: DeclaredTest[] = [];+ for (const name of readdirSync(SMOKE_SPEC_DIR).filter((file) => file.endsWith('.e2e.ts'))) {+ const file = join(SMOKE_SPEC_DIR, name);+ const src = readFileSync(file, 'utf8');+ const stripped = stripCommentsAndStrings(src);+ for (const header of stripped.matchAll(/(?:^|\n)\s*test\(\s*(['"`])/g)) {+ const declaredAt = (header.index ?? 0) + header[0].indexOf('test(');+ const opensAt = (header.index ?? 0) + header[0].length - 1;+ const title = src.slice(opensAt + 1, stripped.indexOf(header[1] ?? '', opensAt + 1));+ if (/['"`]/.test(title)) {+ quoteTitled.push({ file, line: src.slice(0, declaredAt).split('\n').length });+ }+ }+ }+ expect(quoteTitled.length).toBeGreaterThan(0);+ const unaudited = quoteTitled.filter(+ ({ file, line }) => !parseTestFile(file).tests.some((entry) => entry.lineNumber === line),+ );+ expect(unaudited).toEqual([]);});+});- test('scores a waitForWindowByMode called straight from a test body', () => {- const src = `test('x', async () => {\n await waitForWindowByMode(app, 'editor');\n});\n`;- const entry = extractTestEntries(src, [])[0];- expect(entry?.directTimeoutsMs).toEqual([BOOT_LOG_CAP_MS]);- expect(entry?.cumulativeMs).toBe(BOOT_LOG_CAP_MS);+describe('a per-test outer is read in full: a number, the derived sum, or a refusal', () => {+ const derivedOuterTest = [+ "test('launches and finds the editor', async () => {",+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ " await waitForWindowByMode(app, 'editor');",+ ` await expect(page).toBeVisible({ timeout: ${STEP_TIMEOUT_MS} });`,+ '});',+ ];++ for (const path of READINESS_PATHS) {+ test(`reads sumOfDeclaredBoundsMs(test.info()) as the test's own sum on the ${path} path`, () => {+ const [entry] = extractTestEntries(derivedOuterTest.join('\n'), [], { path });+ expect(entry?.perTestTimeoutMs).toBe(entry?.cumulativeMs);+ expect(entry?.cumulativeMs).toBe(readinessGiveUpBoundMs({ path }) + STEP_TIMEOUT_MS);+ });+ }++ for (const [spelled, valueMs] of [+ ['15 * 60_000', 15 * 60_000],+ ['(2 + 3) * 60_000 + 30_000', (2 + 3) * 60_000 + 30_000],+ ] as const) {+ test(`reads test.setTimeout(${spelled}) as the number it spells`, () => {+ const [entry] = extractTestEntries(+ [+ "test('declares an outer in arithmetic', async () => {",+ ` test.setTimeout(${spelled});`,+ ` await expect(page).toBeVisible({ timeout: ${STEP_TIMEOUT_MS} });`,+ '});',+ ].join('\n'),+ [],+ );+ expect(entry?.perTestTimeoutMs).toBe(valueMs);+ });+ }++ const UNREADABLE_OUTERS = [+ { shape: 'a name', statements: ['test.setTimeout(budgetMs);'] },+ { shape: 'another call', statements: ['test.setTimeout(computeBudgetMs(test.info()));'] },+ {+ shape: 'the derived sum with arithmetic added',+ statements: ['test.setTimeout(sumOfDeclaredBoundsMs(test.info()) - 1_000);'],+ },+ {+ shape: 'the derived sum given a second argument',+ statements: ["test.setTimeout(sumOfDeclaredBoundsMs(test.info(), 'fork'));"],+ },+ {+ shape: 'the derived sum beside a literal outer in the same body',+ statements: [+ 'test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ 'test.setTimeout(200_000);',+ ],+ },+ ];++ for (const { shape, statements } of UNREADABLE_OUTERS) {+ test(`throws on ${shape}, naming its line`, () => {+ const lines = [+ "test('declares an outer the guard cannot read', async () => {",+ ...statements.map((statement) => ` ${statement}`),+ ` await expect(page).toBeVisible({ timeout: ${STEP_TIMEOUT_MS} });`,+ '});',+ ];+ const outerLines = statements.map((statement) => lineHolding(lines, statement));+ expect(() => extractTestEntries(lines.join('\n'), [])).toThrow(+ new RegExp(`(?:line\\W*|:)(?:${outerLines.join('|')})\\b`),+ );+ });+ }++ test('names the spec file of an outer it cannot read', () => {+ const file = writeFixture('.e2e.ts', [+ "test('declares an outer the guard cannot read', async () => {",+ ' test.setTimeout(budgetMs);',+ '});',+ ]);+ expect(() => parseTestFile(file)).toThrow(basename(file));+ });++ test('reports an outer of any shape at describe scope as loose', () => {+ const lines = [+ "test.describe('a suite', () => {",+ ' test.setTimeout(200_000);',+ ' test.setTimeout(2 * 30_000);',+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ ' test.setTimeout(budgetMs);',+ " test('declares its own outer', async () => {",+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ ` await expect(page).toBeVisible({ timeout: ${STEP_TIMEOUT_MS} });`,+ ' });',+ '});',+ ];+ const suiteScope = lines.slice(0, lineHolding(lines, "test('declares its own outer'"));+ const expectedLoose = suiteScope.flatMap((line, index) =>+ line.includes('test.setTimeout(') ? [index + 1] : [],+ );+ expect(parseTestFile(writeFixture('.e2e.ts', lines)).looseSetTimeoutLines).toEqual(+ expectedLoose,+ );+ });+});++describe('sumOfDeclaredBoundsMs reads the running test out of its own spec', () => {+ const lines = [+ "import { expect, test } from './_helpers/smoke-test';",+ '',+ "test('launches and finds the editor', async () => {",+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ " await waitForWindowByMode(app, 'editor');",+ ` await expect(page).toBeVisible({ timeout: ${STEP_TIMEOUT_MS} });`,+ '});',+ ];+ const file = writeFixture('.e2e.ts', lines);+ const declared: DeclaredTest = { file, line: lineHolding(lines, "test('launches") };++ afterEach(() => {+ vi.unstubAllEnvs();+ });++ for (const path of READINESS_PATHS) {+ test(`returns the ${path} path's sum for the test declared at that line`, () => {+ const sumMs = sumOfDeclaredBoundsMs(declared, path);+ expect(sumMs).toBe(readinessGiveUpBoundMs({ path }) + STEP_TIMEOUT_MS);+ expect(sumMs).toBe(+ entryDeclaredAt(parseTestFile(file, { path }), declared.line).cumulativeMs,+ );+ });+ }++ test('throws, rather than defaulting, when no test is declared at that line', () => {+ const undeclaredLine = lineHolding(lines, 'waitForWindowByMode');+ expect(() => sumOfDeclaredBoundsMs({ file, line: undeclaredLine }, 'fork')).toThrow(+ new RegExp(`(?:line\\W*|:)${undeclaredLine}\\b`),+ );+ });++ test('takes the packaged path when OK_DESKTOP_PACKAGED_APP names a bundle', () => {+ vi.stubEnv(PACKAGED_APP_ENV, join(fixtureRoot, 'OpenKnowledge.app'));+ expect(sumOfDeclaredBoundsMs(declared)).toBe(+ readinessGiveUpBoundMs({ path: 'packaged' }) + STEP_TIMEOUT_MS,+ );+ });++ test('takes the fork path when OK_DESKTOP_PACKAGED_APP names none', () => {+ vi.stubEnv(PACKAGED_APP_ENV, '');+ expect(sumOfDeclaredBoundsMs(declared)).toBe(+ readinessGiveUpBoundMs({ path: 'fork' }) + STEP_TIMEOUT_MS,+ );+ });+});++describe('the per-test budget the guard scores a test against', () => {+ const lines = [+ "test('declares an outer below its own sum', async () => {",+ ` test.setTimeout(${STEP_TIMEOUT_MS});`,+ " await waitForWindowByMode(app, 'editor');",+ ` await expect(page).toBeVisible({ timeout: ${STEP_TIMEOUT_MS} });`,+ '});',+ '',+ "test('derives its outer from its own sum', async () => {",+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ " await waitForWindowByMode(app, 'editor');",+ ` await expect(page).toBeVisible({ timeout: ${STEP_TIMEOUT_MS} });`,+ '});',+ '',+ "test('declares no outer', async () => {",+ ` await expect(page).toBeVisible({ timeout: ${STEP_TIMEOUT_MS} });`,+ '});',+ ];+ const file = writeFixture('.e2e.ts', lines);+ const scoring: PerTestScoring = { filePath: file, path: 'fork', configCiMs: CONFIG_CI_OUTER_MS };+ const declaredBy = (title: string): TestEntry =>+ entryDeclaredAt(parseTestFile(file, { path: scoring.path }), lineHolding(lines, title));++ test('is a literal outer as written, so one below its sum scores over budget', () => {+ const entry = declaredBy("test('declares an outer below its own sum'");+ const { budgetMs } = perTestBudgetMs(entry, scoring);+ expect(budgetMs).toBe(STEP_TIMEOUT_MS);+ expect(entry.cumulativeMs).toBeGreaterThan(budgetMs);+ });++ test("is a derived outer's own sum, read through sumOfDeclaredBoundsMs", () => {+ const entry = declaredBy("test('derives its outer from its own sum'");+ const { budgetMs } = perTestBudgetMs(entry, scoring);+ expect(budgetMs).toBe(sumOfDeclaredBoundsMs({ file, line: entry.lineNumber }, scoring.path));+ expect(budgetMs).toBe(entry.cumulativeMs);+ });++ test("is the config's CI outer for a test that declares none", () => {+ expect(perTestBudgetMs(declaredBy("test('declares no outer'"), scoring).budgetMs).toBe(+ CONFIG_CI_OUTER_MS,+ );+ });+});++describe('the path a charge is scored on never comes from the environment the guard runs in', () => {+ afterEach(() => {+ vi.unstubAllEnvs();+ });++ const helperWaiting = [+ 'async function findEditorWindow(app: ElectronApplication): Promise<Page> {',+ " return waitForWindowByMode(app, 'editor');",+ '}',+ ];++ const UNTIERED_CHARGES: readonly { through: string; chargesMs: () => readonly number[] }[] = [+ {+ through: 'extractReadinessBudgets',+ chargesMs: () => extractReadinessBudgets("return waitForWindowByMode(app, 'editor');"),+ },+ {+ through: 'extractHelperBudgets',+ chargesMs: () =>+ extractHelperBudgets(helperWaiting.join('\n')).map((helper) => helper.maxTimeoutMs),+ },+ {+ through: 'extractTestEntries',+ chargesMs: () =>+ extractTestEntries(+ "test('x', async () => {\n await waitForWindowByMode(app, 'editor');\n});\n",+ [],+ )[0]?.directTimeoutsMs ?? [],+ },+ {+ through: 'parseTestFile, traced through a helper',+ chargesMs: () =>+ parseTestFile(+ writeFixture('.e2e.ts', [+ ...helperWaiting,+ '',+ "test('finds the editor through a helper', async () => {",+ ' await findEditorWindow(app);',+ '});',+ ]),+ ).tests[0]?.tracedHelperBudgetsMs ?? [],+ },+ ];++ for (const { through, chargesMs } of UNTIERED_CHARGES) {+ test(`${through} charges a wait on no named path the fork path's give-up bound when OK_DESKTOP_PACKAGED_APP names a bundle`, () => {+ vi.stubEnv(PACKAGED_APP_ENV, join(fixtureRoot, 'OpenKnowledge.app'));+ expect(chargesMs()).toEqual([readinessGiveUpBoundMs({ path: 'fork' })]);+ });+ }++ test('the per-test budget of a derived outer is its sum on the path it is scored on, when OK_DESKTOP_PACKAGED_APP names none', () => {+ vi.stubEnv(PACKAGED_APP_ENV, '');+ const lines = [+ "test('derives its outer from its own sum', async () => {",+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ " await waitForWindowByMode(app, 'editor');",+ ` await expect(page).toBeVisible({ timeout: ${STEP_TIMEOUT_MS} });`,+ '});',+ ];+ const file = writeFixture('.e2e.ts', lines);+ const entry = entryDeclaredAt(+ parseTestFile(file, { path: 'packaged' }),+ lineHolding(lines, "test('derives"),+ );+ const { budgetMs } = perTestBudgetMs(entry, {+ filePath: file,+ path: 'packaged',+ configCiMs: CONFIG_CI_OUTER_MS,+ });+ expect(budgetMs).toBe(sumOfDeclaredBoundsMs({ file, line: entry.lineNumber }, 'packaged'));+ expect(budgetMs).not.toBe(sumOfDeclaredBoundsMs({ file, line: entry.lineNumber }, 'fork'));+ });+});++describe('the config outer a local run takes can be the derived one-launch outer', () => {+ const configWithLocalBranch = (name: string): string =>+ writeFixture('.config.ts', [+ "import { defineConfig } from '@playwright/test';",+ `import { ${name} } from './tests/smoke/_helpers/launch-desktop';`,+ '',+ 'export default defineConfig({',+ ` timeout: process.env.CI ? ${CONFIG_CI_OUTER_MS} : ${name},`,+ '});',+ ]);++ test('resolves that local branch to the value launch-desktop exports under the name', () => {+ const config = configWithLocalBranch('ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS');+ expect(() => parsePlaywrightConfigTimeout(config)).not.toThrow();+ expect(parsePlaywrightConfigTimeout(config)).toMatchObject({+ ci: CONFIG_CI_OUTER_MS,+ local: ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS,+ });+ });++ test("refuses any other name in that branch, where the derived outer's name parses", () => {+ expect(() =>+ parsePlaywrightConfigTimeout(+ configWithLocalBranch('ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS'),+ ),+ ).not.toThrow();+ expect(() => parsePlaywrightConfigTimeout(configWithLocalBranch('OTHER_NAME'))).toThrow(+ /OTHER_NAME/,+ );+ });+});++describe('the outer a test derives from its parsed charge', () => {+ test('keeps a test whose own sum exceeds the timeout in force at its own sum', () => {+ const lines = [+ "test('finds its editor', async () => {",+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ " await waitForWindowByMode(app, 'editor');",+ '});',+ ];+ const declared = {+ file: writeFixture('.e2e.ts', lines),+ line: lineHolding(lines, "test('finds"),+ };+ const sumMs = readinessGiveUpBoundMs({ path: 'fork' });+ expect(STEP_TIMEOUT_MS).toBeLessThan(sumMs);+ expect(sumOfDeclaredBoundsMs({ ...declared, timeout: STEP_TIMEOUT_MS }, 'fork')).toBe(sumMs);+ });++ test('keeps a test whose waits it cannot charge under the timeout in force, never under none', () => {+ const lines = [+ "test('finds its editor through a helper another module declares', async () => {",+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ ' await findEditorWindowElsewhere(app);',+ '});',+ ];+ const declared = {+ file: writeFixture('.e2e.ts', lines),+ line: lineHolding(lines, "test('finds"),+ };+ const inForceMs = parsePlaywrightConfigTimeout(DESKTOP_PLAYWRIGHT_CONFIG).ci;+ expect(sumOfDeclaredBoundsMs(declared, 'fork')).toBe(0);+ expect(sumOfDeclaredBoundsMs({ ...declared, timeout: inForceMs }, 'fork')).toBe(inForceMs);+ });++ test('leaves a test that runs with no timeout, as --debug runs it, with none', () => {+ const lines = [+ "test('finds its editor', async () => {",+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ " await waitForWindowByMode(app, 'editor');",+ '});',+ ];+ const declared = {+ file: writeFixture('.e2e.ts', lines),+ line: lineHolding(lines, "test('finds"),+ };+ expect(sumOfDeclaredBoundsMs(declared, 'fork')).toBe(readinessGiveUpBoundMs({ path: 'fork' }));+ expect(sumOfDeclaredBoundsMs({ ...declared, timeout: 0 }, 'fork')).toBe(0);+ });++ describe('resolves a test declared through a modifier whose body Playwright runs', () => {+ const headers = ['test.only(', 'test.fail(', 'test.fail.only('];+ const lines = headers.flatMap((header) => [+ `${header}'is declared through ${header.slice(0, -1)}', async () => {`,+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ " await waitForWindowByMode(app, 'editor');",+ '});',+ '',+ ]);+ const file = writeFixture('.e2e.ts', lines);++ for (const header of headers) {+ test(`${header} resolves to the sum of the test it declares`, () => {+ expect(sumOfDeclaredBoundsMs({ file, line: lineHolding(lines, header) }, 'fork')).toBe(+ readinessGiveUpBoundMs({ path: 'fork' }),+ );+ });+ }++ test('the guard audits every one of them', () => {+ expect(parseTestFile(file).tests.map((t) => t.lineNumber)).toEqual(+ headers.map((header) => lineHolding(lines, header)),+ );+ });+ });++ describe('reads only the declarations the running test makes', () => {+ const lines = [+ 'async function findWindowTheGuardCannotCharge(app: ElectronApplication): Promise<Page> {',+ " return waitForWindowByMode(app, 'editor', { capMs: CAP });",+ '}',+ '',+ "test('declares an outer the guard cannot read', async () => {",+ ' test.setTimeout(budgetMs);',+ '});',+ '',+ "test('derives its outer beside them', async () => {",+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ " await waitForWindowByMode(app, 'editor');",+ '});',+ '',+ "test('derives its outer through the helper the guard cannot charge', async () => {",+ ' test.setTimeout(sumOfDeclaredBoundsMs(test.info()));',+ ' await findWindowTheGuardCannotCharge(app);',+ '});',+ ];+ const file = writeFixture('.e2e.ts', lines);+ const unreadableCallAt = `${file}:${lineHolding(lines, '{ capMs: CAP }')}`;++ test('a test that calls none of them resolves to its own sum', () => {+ expect(+ sumOfDeclaredBoundsMs(+ { file, line: lineHolding(lines, "test('derives its outer beside") },+ 'fork',+ ),+ ).toBe(readinessGiveUpBoundMs({ path: 'fork' }));+ });++ test('a test that calls the helper it cannot charge is refused, naming that call', () => {+ expect(() =>+ sumOfDeclaredBoundsMs(+ { file, line: lineHolding(lines, "test('derives its outer through") },+ 'fork',+ ),+ ).toThrow(unreadableCallAt);+ });++ test('the guard still refuses the spec, naming the call it cannot read', () => {+ expect(() => parseTestFile(file)).toThrow(unreadableCallAt);+ });+ });++ test('the guard names where a test body calls the readiness wait with options it cannot read', () => {+ const lines = [+ "test('stalls on a bound the guard cannot read', async () => {",+ " await waitForWindowByMode(app, 'editor', { stallMs: STALL });",+ '});',+ ];+ const file = writeFixture('.e2e.ts', lines);+ expect(() => parseTestFile(file)).toThrow(+ `${file}:${lineHolding(lines, '{ stallMs: STALL }')}`,+ );});});diff --git a/packages/desktop/tests/smoke/_helpers/parse-timeouts.ts b/packages/desktop/tests/smoke/_helpers/parse-timeouts.tsindex 4848742f1..18e72d415 100644--- a/packages/desktop/tests/smoke/_helpers/parse-timeouts.ts+++ b/packages/desktop/tests/smoke/_helpers/parse-timeouts.ts@@ -1,28 +1,62 @@import { readFileSync } from 'node:fs';-import { DEFAULT_LAUNCH_TIMEOUT_MS } from './launch-desktop';-import { BOOT_LOG_CAP_MS } from './launch-readiness';+import {+ DEFAULT_LAUNCH_TIMEOUT_MS,+ ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS,+ resolveDesktopTarget,+} from './launch-desktop';+import { type ReadinessPath, readinessGiveUpBoundMs, readinessPathOf } from './launch-readiness';const TIMEOUT_LITERAL_RE = /\btimeout:\s*(\d+(?:_\d+)*)/g;const LAUNCH_HELPER_CALL_RE = /\bdesktopLaunchOptions\(/;const READINESS_HELPER_CALL_RE = /\bwaitForWindowByMode\s*\(/g;-const READINESS_CAPMS_RE = /\bcapMs:\s*(\d+(?:_\d+)*)/;+const READINESS_OPTION_PROPERTY_RE = /^([A-Za-z_$][\w$]*)\s*:\s*([\s\S]+)$/;+const NUMERIC_LITERAL_RE = /^(\d+(?:_\d+)*)$/;const TOPASS_TIMEOUT_RE = /\.toPass\(\s*\{[^}]*timeout:\s*(\d+(?:_\d+)*)/g;const DEFAULT_TIMEOUT_ARG_RE = /\btimeoutMs\s*=\s*(\d+(?:_\d+)*)/g;const FUNCTION_HEADER_RE = /(?:^|\n)(?:export\s+)?(?:async\s+)?function\s+(\w+)\s*\(/g;-const TEST_HEADER_RE = /(?:^|\n)\s*test\(\s*(['"`])([^'"`]+)\1/g;-const TEST_SET_TIMEOUT_RE = /\btest\.setTimeout\(\s*(\d+(?:_\d+)*)\s*\)/g;+const TEST_HEADER_RE =+ /(?:^|\n)\s*test(?:\.only|\.fail(?:\.only)?)?\(\s*(['"`])((?:\\.|(?!\1)[^\\])*)\1/g;+const TEST_SET_TIMEOUT_RE = /\btest\.setTimeout\(/g;+const DERIVED_OUTER_RE = /^sumOfDeclaredBoundsMs\(\s*test\.info\(\s*\)\s*\)$/;+const ARITHMETIC_TOKEN_RE = /\d+(?:_\d+)*|[()+*]|\S/g;++export interface ChargeTier {+ path: ReadinessPath;+}++const UNPACKAGED_CHARGE_TIER: ChargeTier = { path: readinessPathOf('unpackaged') };++type ReadinessBudgetKey = 'capMs' | 'stallMs' | 'pollMs';++type ReadinessBudgets = Partial<Record<ReadinessBudgetKey, number>>;++const READINESS_BUDGET_KEYS: ReadonlySet<string> = new Set<ReadinessBudgetKey>([+ 'capMs',+ 'stallMs',+ 'pollMs',+]);++function isReadinessBudgetKey(key: string): key is ReadinessBudgetKey {+ return READINESS_BUDGET_KEYS.has(key);+}export interface HelperBudget {name: string;maxTimeoutMs: number;}-export interface TestEntry {+type PerTestOuter =+ | { perTestTimeoutSource: 'literal'; perTestTimeoutMs: number }+ | { perTestTimeoutSource: 'sum-of-declared-bounds'; perTestTimeoutMs: number }+ | { perTestTimeoutSource: null; perTestTimeoutMs: null };++export type TestEntry = PerTestOuter & TestEntryBudgets;++interface TestEntryBudgets {testName: string;lineNumber: number;- perTestTimeoutMs: number | null;directTimeoutsMs: number[];helperCallNames: string[];tracedHelperBudgetsMs: number[];@@ -100,6 +134,10 @@ function lineNumberAt(src: string, idx: number): number {return n;}+function locatorFor(filePath: string | undefined): (line: number) => string {+ return (line) => (filePath === undefined ? `line ${line}` : `${filePath}:${line}`);+}+export function stripCommentsAndStrings(src: string): string {const out: string[] = new Array(src.length);let i = 0;@@ -161,10 +199,75 @@ export function stripCommentsAndStrings(src: string): string {return out.join('');}-export function extractReadinessBudgets(src: string): number[] {+function topLevelParts(text: string): string[] {+ const parts: string[] = [];+ let depth = 0;+ let start = 0;+ for (let i = 0; i < text.length; i += 1) {+ const c = text[i];+ if (c === '"' || c === "'" || c === '`') {+ const quote = c;+ i += 1;+ while (i < text.length && text[i] !== quote) i += text[i] === '\\' ? 2 : 1;+ continue;+ }+ if (c === '(' || c === '{' || c === '[') depth += 1;+ else if (c === ')' || c === '}' || c === ']') depth -= 1;+ else if (c === ',' && depth === 0) {+ parts.push(text.slice(start, i));+ start = i + 1;+ }+ }+ parts.push(text.slice(start));+ return parts.map((part) => part.trim()).filter((part) => part.length > 0);+}++function unreadableReadinessOptions(callText: string, where: string): Error {+ return new Error(+ `extractReadinessBudgets: cannot read the options of ${callText} at ${where}; the third ` ++ 'argument must be an object literal of key: value pairs whose capMs, stallMs and pollMs ' ++ 'are numeric literals',+ );+}++function readReadinessBudgets(+ options: string | undefined,+ callText: string,+ where: string,+): ReadinessBudgets {+ const read: ReadinessBudgets = {};+ if (options === undefined) return read;+ if (!options.startsWith('{') || findMatchingClose(options, 0) !== options.length - 1) {+ throw unreadableReadinessOptions(callText, where);+ }+ for (const property of topLevelParts(options.slice(1, -1))) {+ const pair = property.match(READINESS_OPTION_PROPERTY_RE);+ if (pair === null) throw unreadableReadinessOptions(callText, where);+ const [, key, value] = pair;+ if (!isReadinessBudgetKey(key)) continue;+ const literal = value.trim().match(NUMERIC_LITERAL_RE);+ if (literal === null) throw unreadableReadinessOptions(callText, where);+ read[key] = parseNumericLiteral(literal[1]);+ }+ return read;+}++export function extractReadinessBudgets(+ src: string,+ tier: ChargeTier = UNPACKAGED_CHARGE_TIER,+): number[] {+ return readinessBudgetsIn(src, tier, (at) => locatorFor(undefined)(lineNumberAt(src, at)));+}++function readinessBudgetsIn(+ src: string,+ tier: ChargeTier,+ whereAt: (at: number) => string,+): number[] {const budgets: number[] = [];for (const m of src.matchAll(READINESS_HELPER_CALL_RE)) {- const openIdx = src.indexOf('(', m.index ?? 0);+ const callStart = m.index ?? 0;+ const openIdx = src.indexOf('(', callStart);if (openIdx === -1) continue;let depth = 1;let i = openIdx + 1;@@ -175,15 +278,31 @@ export function extractReadinessBudgets(src: string): number[] {i += 1;}const args = src.slice(openIdx + 1, Math.max(openIdx + 1, i - 1));- const cap = args.match(READINESS_CAPMS_RE);- budgets.push(cap ? parseNumericLiteral(cap[1]) : BOOT_LOG_CAP_MS);+ const read = readReadinessBudgets(+ topLevelParts(args)[2],+ src.slice(callStart, i),+ whereAt(callStart),+ );+ budgets.push(readinessGiveUpBoundMs({ path: tier.path, ...read }));}return budgets;}-export function extractHelperBudgets(src: string): HelperBudget[] {+export function extractHelperBudgets(+ src: string,+ tier: ChargeTier = UNPACKAGED_CHARGE_TIER,+): HelperBudget[] {+ return helperBudgetsIn(src, tier, src.matchAll(FUNCTION_HEADER_RE), locatorFor(undefined));+}++function helperBudgetsIn(+ src: string,+ tier: ChargeTier,+ headers: Iterable<RegExpMatchArray>,+ locate: (line: number) => string,+): HelperBudget[] {const helpers: HelperBudget[] = [];- for (const m of src.matchAll(FUNCTION_HEADER_RE)) {+ for (const m of headers) {const name = m[1];if (name === 'test' || name === 'describe') continue;const headerStart = m.index ?? 0;@@ -215,7 +334,9 @@ export function extractHelperBudgets(src: string): HelperBudget[] {if (LAUNCH_HELPER_CALL_RE.test(body)) {budgets.push(DEFAULT_LAUNCH_TIMEOUT_MS);}- budgets.push(...extractReadinessBudgets(body));+ budgets.push(+ ...readinessBudgetsIn(body, tier, (at) => locate(lineNumberAt(src, bodyOpenIdx + 1 + at))),+ );const maxTimeoutMs = budgets.length > 0 ? Math.max(...budgets) : 0;if (maxTimeoutMs > 0) {helpers.push({ name, maxTimeoutMs });@@ -224,13 +345,77 @@ export function extractHelperBudgets(src: string): HelperBudget[] {return helpers;}-export function extractTestEntries(src: string, helpers: HelperBudget[]): TestEntry[] {+function evaluateArithmetic(text: string): number | undefined {+ const tokens = text.match(ARITHMETIC_TOKEN_RE) ?? [];+ let at = 0;+ function factor(): number | undefined {+ const token: string | undefined = tokens[at];+ if (token === '(') {+ at += 1;+ const value = sum();+ if (value === undefined || tokens[at] !== ')') return undefined;+ at += 1;+ return value;+ }+ if (token === undefined || !NUMERIC_LITERAL_RE.test(token)) return undefined;+ at += 1;+ return parseNumericLiteral(token);+ }+ function product(): number | undefined {+ let value = factor();+ while (value !== undefined && tokens[at] === '*') {+ at += 1;+ const next = factor();+ value = next === undefined ? undefined : value * next;+ }+ return value;+ }+ function sum(): number | undefined {+ let value = product();+ while (value !== undefined && tokens[at] === '+') {+ at += 1;+ const next = product();+ value = next === undefined ? undefined : value + next;+ }+ return value;+ }+ const value = sum();+ return value !== undefined && at === tokens.length ? value : undefined;+}++export function extractTestEntries(+ src: string,+ helpers: HelperBudget[],+ tier: ChargeTier = UNPACKAGED_CHARGE_TIER,+): TestEntry[] {+ return testEntriesOf(+ src,+ src.matchAll(TEST_HEADER_RE),+ () => helpers,+ tier,+ locatorFor(undefined),+ );+}++function declarationStartOf(header: RegExpMatchArray): number {+ return (header.index ?? 0) + header[0].indexOf('test');+}++function callsTo(name: string): RegExp {+ return new RegExp(`\\b${name}\\s*\\(`, 'g');+}++function testEntriesOf(+ src: string,+ headers: Iterable<RegExpMatchArray>,+ helpersCalledIn: (body: string) => readonly HelperBudget[],+ tier: ChargeTier,+ locate: (line: number) => string,+): TestEntry[] {const entries: TestEntry[] = [];- for (const m of src.matchAll(TEST_HEADER_RE)) {+ for (const m of headers) {const testName = m[2];- const matchStart = m.index ?? 0;- const testKwOffset = m[0].indexOf('test(');- const headerStart = matchStart + (testKwOffset >= 0 ? testKwOffset : 0);+ const headerStart = declarationStartOf(m);const lineNumber = lineNumberAt(src, headerStart);const arrowIdx = src.indexOf('=>', headerStart);if (arrowIdx === -1) continue;@@ -245,22 +430,56 @@ export function extractTestEntries(src: string, helpers: HelperBudget[]): TestEnfor (const tm of strippedForTimeouts.matchAll(TIMEOUT_LITERAL_RE)) {directTimeoutsMs.push(parseNumericLiteral(tm[1]));}- directTimeoutsMs.push(...extractReadinessBudgets(strippedForTimeouts));+ directTimeoutsMs.push(+ ...readinessBudgetsIn(strippedForTimeouts, tier, (at) =>+ locate(lineNumberAt(src, bodyOpenIdx + 1 + at)),+ ),+ );const toPassBudgetsMs: number[] = [];for (const tm of strippedForTimeouts.matchAll(TOPASS_TIMEOUT_RE)) {toPassBudgetsMs.push(parseNumericLiteral(tm[1]));}- const setTimeoutMs: number[] = [];- for (const tm of strippedForTimeouts.matchAll(TEST_SET_TIMEOUT_RE)) {- setTimeoutMs.push(parseNumericLiteral(tm[1]));+ const literalOutersMs: number[] = [];+ const derivedOuterLines: number[] = [];+ for (const sm of strippedForTimeouts.matchAll(TEST_SET_TIMEOUT_RE)) {+ const outerAt = sm.index ?? 0;+ const line = lineNumberAt(src, bodyOpenIdx + 1 + outerAt);+ const argumentStart = outerAt + sm[0].length;+ let depth = 1;+ let argumentEnd = argumentStart;+ while (argumentEnd < strippedForTimeouts.length && depth > 0) {+ const c = strippedForTimeouts[argumentEnd];+ if (c === '(') depth += 1;+ else if (c === ')') depth -= 1;+ argumentEnd += 1;+ }+ const argument = strippedForTimeouts.slice(argumentStart, argumentEnd - 1).trim();+ if (DERIVED_OUTER_RE.test(argument)) {+ derivedOuterLines.push(line);+ continue;+ }+ const outerMs = evaluateArithmetic(argument);+ if (outerMs === undefined) {+ throw new Error(+ `extractTestEntries: cannot read test.setTimeout(${argument}) at ${locate(line)}; a ` ++ 'per-test outer must be numeric-literal arithmetic or exactly ' ++ 'sumOfDeclaredBoundsMs(test.info())',+ );+ }+ literalOutersMs.push(outerMs);+ }+ const derivedOuterLine = derivedOuterLines[0];+ if (derivedOuterLine !== undefined && derivedOuterLines.length + literalOutersMs.length > 1) {+ throw new Error(+ `extractTestEntries: the sumOfDeclaredBoundsMs(test.info()) outer at ` ++ `${locate(derivedOuterLine)} must be the only test.setTimeout in its test`,+ );}- const perTestTimeoutMs = setTimeoutMs.length > 0 ? Math.max(...setTimeoutMs) : null;const strippedBody = stripCommentsAndStrings(body);const helperCallNames: string[] = [];const tracedHelperBudgetsMs: number[] = [];- for (const helper of helpers) {- const callRe = new RegExp(`\\b${helper.name}\\s*\\(`, 'g');- for (const _cm of strippedBody.matchAll(callRe)) {+ for (const helper of helpersCalledIn(strippedBody)) {+ for (const _cm of strippedBody.matchAll(callsTo(helper.name))) {helperCallNames.push(helper.name);tracedHelperBudgetsMs.push(helper.maxTimeoutMs);}@@ -268,10 +487,16 @@ export function extractTestEntries(src: string, helpers: HelperBudget[]): TestEnconst cumulativeMs =directTimeoutsMs.reduce((a, b) => a + b, 0) +tracedHelperBudgetsMs.reduce((a, b) => a + b, 0);+ const outer: PerTestOuter =+ derivedOuterLine !== undefined+ ? { perTestTimeoutMs: cumulativeMs, perTestTimeoutSource: 'sum-of-declared-bounds' }+ : literalOutersMs.length > 0+ ? { perTestTimeoutMs: Math.max(...literalOutersMs), perTestTimeoutSource: 'literal' }+ : { perTestTimeoutMs: null, perTestTimeoutSource: null };entries.push({testName,lineNumber,- perTestTimeoutMs,+ ...outer,directTimeoutsMs,helperCallNames,tracedHelperBudgetsMs,@@ -283,10 +508,14 @@ export function extractTestEntries(src: string, helpers: HelperBudget[]): TestEnreturn entries;}-export function parseTestFile(filePath: string): FileAnalysis {+export function parseTestFile(+ filePath: string,+ tier: ChargeTier = UNPACKAGED_CHARGE_TIER,+): FileAnalysis {const src = readFileSync(filePath, 'utf8');- const helpers = extractHelperBudgets(src);- const tests = extractTestEntries(src, helpers);+ const locate = locatorFor(filePath);+ const helpers = helperBudgetsIn(src, tier, src.matchAll(FUNCTION_HEADER_RE), locate);+ const tests = testEntriesOf(src, src.matchAll(TEST_HEADER_RE), () => helpers, tier, locate);return { filePath, helpers, tests, looseSetTimeoutLines: findLooseSetTimeouts(src, tests) };}@@ -321,7 +550,66 @@ export function parsePlaywrightConfigTimeout(configPath: string): PlaywrightConfraw,};}+ const derivedLocal = raw.match(+ /^process\.env\.CI\s*\?\s*(\d+(?:_\d+)*)\s*:\s*ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS$/,+ );+ if (derivedLocal) {+ return {+ ci: parseNumericLiteral(derivedLocal[1]),+ local: ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS,+ raw,+ };+ }throw new Error(`parsePlaywrightConfigTimeout: unsupported \`timeout:\` shape in ${configPath} — got "${raw}"`,);}++export function sumOfDeclaredBoundsMs(+ test: { file: string; line: number; timeout?: number },+ path: ReadinessPath = readinessPathOf(resolveDesktopTarget().mode),+): number {+ const src = readFileSync(test.file, 'utf8');+ const tier: ChargeTier = { path };+ const locate = locatorFor(test.file);+ const entry = testEntriesOf(+ src,+ [...src.matchAll(TEST_HEADER_RE)].filter(+ (header) => lineNumberAt(src, declarationStartOf(header)) === test.line,+ ),+ (body) =>+ helperBudgetsIn(+ src,+ tier,+ [...src.matchAll(FUNCTION_HEADER_RE)].filter((header) => callsTo(header[1]).test(body)),+ locate,+ ),+ tier,+ locate,+ ).at(0);+ if (entry === undefined) {+ throw new Error(`sumOfDeclaredBoundsMs: no test is declared at ${test.file}:${test.line}`);+ }+ if (test.timeout === undefined) return entry.cumulativeMs;+ return test.timeout === 0 ? 0 : Math.max(test.timeout, entry.cumulativeMs);+}++export function perTestBudgetMs(+ entry: TestEntry,+ scoring: { filePath: string; path: ReadinessPath; configCiMs: number },+): { budgetMs: number; source: 'literal' | 'sum-of-declared-bounds' | 'config-ci' } {+ switch (entry.perTestTimeoutSource) {+ case 'sum-of-declared-bounds':+ return {+ budgetMs: sumOfDeclaredBoundsMs(+ { file: scoring.filePath, line: entry.lineNumber },+ scoring.path,+ ),+ source: 'sum-of-declared-bounds',+ };+ case 'literal':+ return { budgetMs: entry.perTestTimeoutMs, source: 'literal' };+ case null:+ return { budgetMs: scoring.configCiMs, source: 'config-ci' };+ }+}diff --git a/packages/desktop/tests/smoke/_helpers/readiness-reach.test-helper.ts b/packages/desktop/tests/smoke/_helpers/readiness-reach.test-helper.tsnew file mode 100644index 000000000..d8ae68175--- /dev/null+++ b/packages/desktop/tests/smoke/_helpers/readiness-reach.test-helper.ts@@ -0,0 +1,497 @@+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { assert, vi } from 'vitest';+import type { WaterfallPhase } from '../../../src/main/startup-waterfall.ts';+import {+ BOOT_HEARTBEAT_EVENTS,+ DESKTOP_BOOT_EVENT,+ DESKTOP_OPEN_PROJECT_FAILED_EVENT,+ SPAWN_WAIT_HEARTBEAT_MS,+ startupMarkLine,+ UTILITY_INIT_TIMEOUT_MS,+} from '../../../src/shared/boot-narration.ts';+import {+ BOOT_LOG_CAP_MS,+ BOOT_LOG_POLL_MS,+ BOOT_LOG_STALL_MS,+ bootLogDirFor,+ EVERY_STARTUP_PHASE,+ type LaunchAdvancement,+ type ReadinessPath,+ type ReadyWaitGiveUpReason,+ readinessWorstCaseMs,+ readyWaitsFor,+ UTILITY_TIMEOUT_OBSERVATION_MARGIN_MS,+ type WaitForWindowOptions,+ waitForWindowByMode,+} from './launch-readiness.ts';++export type ReadinessCallOptions = Pick<WaitForWindowOptions, 'capMs' | 'stallMs' | 'pollMs'>;++export const READINESS_PATHS: readonly ReadinessPath[] = ['fork', 'packaged'];++interface ReadinessCall {+ label: string;+ source: string;+ options: ReadinessCallOptions;+}++export const READINESS_CALLS: readonly ReadinessCall[] = [+ {+ label: 'a plain call',+ source: 'return waitForWindowByMode(app, mode);',+ options: {},+ },+ {+ label: 'a call with an inline capMs',+ source: "waitForWindowByMode(app, 'navigator', { capMs: 60_000 })",+ options: { capMs: 60_000 },+ },+ {+ label: 'a call whose capMs falls between two polls',+ source: `waitForWindowByMode(app, 'editor', { capMs: ${BOOT_LOG_CAP_MS + 1} })`,+ options: { capMs: BOOT_LOG_CAP_MS + 1 },+ },+ {+ label: 'a call with an inline stallMs',+ source: `waitForWindowByMode(app, 'editor', { stallMs: ${2 * BOOT_LOG_STALL_MS} })`,+ options: { stallMs: 2 * BOOT_LOG_STALL_MS },+ },+];++export function pollIntervalOf(options: ReadinessCallOptions): number {+ return options.pollMs ?? BOOT_LOG_POLL_MS;+}++function firstPollAtOrAfter(atMs: number, pollMs: number): number {+ return Math.ceil(atMs / pollMs) * pollMs;+}++interface NarrationLine {+ atMs: number;+ text: string;+}++type HomeLog = 'own' | 'earlier';++interface UnreadableStretch {+ log: HomeLog;+ fromMs: number;+ untilMs: number;+}++export interface ScriptedLaunch {+ name: string;+ options: ReadinessCallOptions;+ narrationUntil: (untilMs: number) => NarrationLine[];+ earlierLog?: readonly NarrationLine[];+ unreadable?: UnreadableStretch;+}++const WAIT_STARTED_AT = Date.UTC(2026, 8, 24);++function lineAt(atMs: number, body: Record<string, unknown>): NarrationLine {+ return {+ atMs,+ text: JSON.stringify({ time: new Date(WAIT_STARTED_AT + atMs).toISOString(), ...body }),+ };+}++function stageAt(phase: WaterfallPhase, atMs: number): NarrationLine {+ return lineAt(atMs, startupMarkLine(phase, atMs));+}++function utilityBeatAt(atMs: number, elapsedMs: number): NarrationLine {+ return lineAt(atMs, {+ event: BOOT_HEARTBEAT_EVENTS.utilityWait,+ elapsedMs,+ initTimeoutMs: UTILITY_INIT_TIMEOUT_MS,+ });+}++function utilityBeatsOnTheAppsCadenceFrom(forkAtMs: number): NarrationLine[] {+ const beats: NarrationLine[] = [];+ for (+ let elapsedMs = SPAWN_WAIT_HEARTBEAT_MS;+ elapsedMs <= UTILITY_INIT_TIMEOUT_MS;+ elapsedMs += SPAWN_WAIT_HEARTBEAT_MS+ ) {+ beats.push(utilityBeatAt(forkAtMs + elapsedMs, elapsedMs));+ }+ return beats;+}++function launchOpenedBeforeTheWait(pollMs: number): NarrationLine[] {+ return [lineAt(-pollMs, { event: DESKTOP_BOOT_EVENT }), stageAt('appReady', -pollMs)];+}++function mainNarratesUntil(untilMs: number): NarrationLine[] {+ const beats: NarrationLine[] = [];+ for (let atMs = SPAWN_WAIT_HEARTBEAT_MS; atMs <= untilMs; atMs += SPAWN_WAIT_HEARTBEAT_MS) {+ beats.push(lineAt(atMs, { event: BOOT_HEARTBEAT_EVENTS.boot, elapsedMs: atMs }));+ }+ return beats;+}++function waitBudgets(options: ReadinessCallOptions) {+ return {+ capMs: options.capMs ?? BOOT_LOG_CAP_MS,+ stallMs: options.stallMs ?? BOOT_LOG_STALL_MS,+ pollMs: pollIntervalOf(options),+ };+}++interface ScriptedStage {+ phase: WaterfallPhase;+ atMs: number;+}++function stagesLandingAsLateAsTheyCanRenew(options: ReadinessCallOptions): ScriptedStage[] {+ const { capMs, stallMs, pollMs } = waitBudgets(options);+ const lastRenewingStageAtMs = capMs + stallMs - pollMs;+ return [+ { phase: 'bootstrapDone', atMs: capMs - pollMs },+ { phase: 'serverSpawned', atMs: lastRenewingStageAtMs - pollMs },+ { phase: 'serverLockReady', atMs: lastRenewingStageAtMs - pollMs },+ { phase: 'windowCreated', atMs: lastRenewingStageAtMs },+ { phase: 'loadUrlResolved', atMs: lastRenewingStageAtMs },+ ];+}++function stagesRenewingAsLateAsTheyCan(options: ReadinessCallOptions): ScriptedLaunch {+ const { pollMs } = waitBudgets(options);+ return {+ name: 'startup stages that each arrive as late as they can still renew the wait',+ options,+ narrationUntil: (untilMs) => [+ ...launchOpenedBeforeTheWait(pollMs),+ ...stagesLandingAsLateAsTheyCanRenew(options).map(({ phase, atMs }) => stageAt(phase, atMs)),+ ...mainNarratesUntil(untilMs),+ ],+ };+}++export function utilityForkTheLastStageKeptAlive(+ options: ReadinessCallOptions,+): ScriptedLaunch & { appVerdictAtMs: number } {+ const { capMs, stallMs, pollMs } = waitBudgets(options);+ const forkAtMs = capMs + stallMs - SPAWN_WAIT_HEARTBEAT_MS;+ const appVerdictAtMs = forkAtMs + UTILITY_INIT_TIMEOUT_MS + pollMs;+ return {+ name: "a utility fork the last startup stage kept alive, beating on the app's own cadence until its budget runs out",+ options,+ appVerdictAtMs,+ narrationUntil: (untilMs) => [+ ...launchOpenedBeforeTheWait(pollMs),+ stageAt('bootstrapDone', capMs - pollMs),+ stageAt('serverSpawned', forkAtMs),+ ...utilityBeatsOnTheAppsCadenceFrom(forkAtMs),+ lineAt(appVerdictAtMs, { event: DESKTOP_OPEN_PROJECT_FAILED_EVENT }),+ ...mainNarratesUntil(untilMs),+ ],+ };+}++export function utilityForkWhoseFirstBeatLandsOnTheLastStagePoll(+ options: ReadinessCallOptions,+): ScriptedLaunch & { appVerdictAtMs: number } {+ const { capMs, stallMs, pollMs } = waitBudgets(options);+ const forkAtMs = firstPollAtOrAfter(capMs + stallMs, pollMs) - SPAWN_WAIT_HEARTBEAT_MS;+ const appVerdictAtMs = forkAtMs + UTILITY_INIT_TIMEOUT_MS + pollMs;+ return {+ name: 'a utility fork whose first beat lands on the last poll the stage window allows',+ options,+ appVerdictAtMs,+ narrationUntil: (untilMs) => [+ ...launchOpenedBeforeTheWait(pollMs),+ stageAt('bootstrapDone', capMs - pollMs),+ stageAt('serverSpawned', forkAtMs),+ ...utilityBeatsOnTheAppsCadenceFrom(forkAtMs),+ lineAt(appVerdictAtMs, { event: DESKTOP_OPEN_PROJECT_FAILED_EVENT }),+ ...mainNarratesUntil(untilMs),+ ],+ };+}++function budgetDeclaredButNeverSpent(options: ReadinessCallOptions): ScriptedLaunch {+ const { pollMs } = waitBudgets(options);+ const everyMs = UTILITY_TIMEOUT_OBSERVATION_MARGIN_MS - pollMs;+ return {+ name: 'utility beats the parser accepts, whose elapsed time never keeps up with the clock',+ options,+ narrationUntil: (untilMs) => {+ const beats: NarrationLine[] = [];+ let elapsedMs = UTILITY_INIT_TIMEOUT_MS - UTILITY_TIMEOUT_OBSERVATION_MARGIN_MS;+ for (let atMs = everyMs; atMs <= untilMs; atMs += everyMs) {+ elapsedMs += 1;+ beats.push(utilityBeatAt(atMs, elapsedMs));+ }+ return [+ ...launchOpenedBeforeTheWait(pollMs),+ stageAt('bootstrapDone', -pollMs),+ stageAt('serverSpawned', -pollMs),+ ...beats,+ ];+ },+ };+}++export const SCRIPTED_LAUNCHES = [+ stagesRenewingAsLateAsTheyCan,+ utilityForkTheLastStageKeptAlive,+ budgetDeclaredButNeverSpent,+] as const;++export const LAUNCH_REACHING_THE_CEILING_OF: Readonly<+ Record<ReadinessPath, (options: ReadinessCallOptions) => ScriptedLaunch>+> = {+ fork: budgetDeclaredButNeverSpent,+ packaged: stagesRenewingAsLateAsTheyCan,+};++function anEarlierLaunchThatFinished(pollMs: number, finishedAtMs = 0): NarrationLine[] {+ return [+ lineAt(finishedAtMs - (EVERY_STARTUP_PHASE.length + 1) * pollMs, { event: DESKTOP_BOOT_EVENT }),+ ...EVERY_STARTUP_PHASE.map((phase, index) =>+ stageAt(phase, finishedAtMs + (index - EVERY_STARTUP_PHASE.length) * pollMs),+ ),+ ];+}++const ONE_DAY_MS = 24 * 60 * 60 * 1000;++function aLaunchThatFinishedTheDayBefore(pollMs: number): NarrationLine[] {+ return anEarlierLaunchThatFinished(pollMs, -ONE_DAY_MS);+}++export function launchBootingAfterTheWaitsFirstRead(+ options: ReadinessCallOptions,+ home: { sharedWithAnEarlierLaunch: boolean },+): ScriptedLaunch & { bootAtMs: number; ownStageEvents: readonly string[] } {+ const { pollMs } = waitBudgets(options);+ const bootAtMs = pollMs / 2;+ const ownStages: ScriptedStage[] = [+ { phase: 'appReady', atMs: bootAtMs },+ ...stagesLandingAsLateAsTheyCanRenew(options),+ ];+ return {+ name: home.sharedWithAnEarlierLaunch+ ? "a launch booting after the wait's first read, into a home whose log an earlier launch already filled"+ : "a launch booting after the wait's first read, alone in its home",+ options,+ bootAtMs,+ ownStageEvents: ownStages.map(({ phase }) => startupMarkLine(phase, 0).event),+ narrationUntil: (untilMs) => [+ ...(home.sharedWithAnEarlierLaunch ? anEarlierLaunchThatFinished(pollMs) : []),+ lineAt(bootAtMs, { event: DESKTOP_BOOT_EVENT }),+ ...ownStages.map(({ phase, atMs }) => stageAt(phase, atMs)),+ ...mainNarratesUntil(untilMs),+ ],+ };+}++function anEarlierLaunchThatStoppedInItsUtilityWait(pollMs: number): NarrationLine[] {+ const forkAtMs = -(UTILITY_INIT_TIMEOUT_MS + pollMs);+ return [+ lineAt(forkAtMs - 2 * pollMs, { event: DESKTOP_BOOT_EVENT }),+ stageAt('appReady', forkAtMs - 2 * pollMs),+ stageAt('bootstrapDone', forkAtMs - pollMs),+ stageAt('serverSpawned', forkAtMs),+ ...utilityBeatsOnTheAppsCadenceFrom(forkAtMs),+ ];+}++export function utilityForkBootingAfterTheWaitsFirstRead(+ options: ReadinessCallOptions,+ home: { sharedWithALaunchThatStoppedInItsUtilityWait: boolean },+): ScriptedLaunch & { appVerdictAtMs: number } {+ const { capMs, pollMs } = waitBudgets(options);+ const bootAtMs = pollMs / 2;+ const forkAtMs = capMs - SPAWN_WAIT_HEARTBEAT_MS;+ const appVerdictAtMs = forkAtMs + UTILITY_INIT_TIMEOUT_MS + pollMs;+ return {+ name: home.sharedWithALaunchThatStoppedInItsUtilityWait+ ? "a utility fork booting after the wait's first read, into a home an earlier launch left inside its utility wait"+ : "a utility fork booting after the wait's first read, alone in its home",+ options,+ appVerdictAtMs,+ narrationUntil: (untilMs) => [+ ...(home.sharedWithALaunchThatStoppedInItsUtilityWait+ ? anEarlierLaunchThatStoppedInItsUtilityWait(pollMs)+ : []),+ lineAt(bootAtMs, { event: DESKTOP_BOOT_EVENT }),+ stageAt('appReady', bootAtMs),+ stageAt('bootstrapDone', forkAtMs - pollMs),+ stageAt('serverSpawned', forkAtMs),+ ...utilityBeatsOnTheAppsCadenceFrom(forkAtMs),+ lineAt(appVerdictAtMs, { event: DESKTOP_OPEN_PROJECT_FAILED_EVENT }),+ ...mainNarratesUntil(untilMs),+ ],+ };+}++export function utilityForkTheLastStageKeptAliveBesideTheDayBeforesLog(+ options: ReadinessCallOptions,+ ownLog: { unreadableOnOnePollOfTheDeclaredGrant: boolean },+): ScriptedLaunch & { appVerdictAtMs: number } {+ const launch = utilityForkTheLastStageKeptAlive(options);+ const { pollMs } = waitBudgets(options);+ const unreadableAtMs =+ firstPollAtOrAfter(readinessWorstCaseMs({ path: 'packaged', ...options }), pollMs) + pollMs;+ const unreadable: UnreadableStretch = {+ log: 'own',+ fromMs: unreadableAtMs,+ untilMs: unreadableAtMs + pollMs,+ };+ return {+ ...launch,+ name: ownLog.unreadableOnOnePollOfTheDeclaredGrant+ ? `${launch.name}, beside the log a launch the day before left, its own log unreadable on one poll of the grant it declared`+ : `${launch.name}, beside the log a launch the day before left`,+ earlierLog: aLaunchThatFinishedTheDayBefore(pollMs),+ ...(ownLog.unreadableOnOnePollOfTheDeclaredGrant ? { unreadable } : {}),+ };+}++export function launchBootingAfterTheWaitsFirstReadBesideTheDayBeforesLog(+ options: ReadinessCallOptions,+ earlierLog: { unreadableFromTheBootUntilMainsFirstHeartbeat: boolean },+): ScriptedLaunch & { ownStageEvents: readonly string[] } {+ const launch = launchBootingAfterTheWaitsFirstRead(options, { sharedWithAnEarlierLaunch: false });+ const { pollMs } = waitBudgets(options);+ const unreadable: UnreadableStretch = {+ log: 'earlier',+ fromMs: launch.bootAtMs,+ untilMs: SPAWN_WAIT_HEARTBEAT_MS,+ };+ return {+ ...launch,+ name: earlierLog.unreadableFromTheBootUntilMainsFirstHeartbeat+ ? "a launch booting after the wait's first read, beside the log a launch the day before left, which cannot be read from the boot until main's first heartbeat"+ : "a launch booting after the wait's first read, beside the log a launch the day before left",+ earlierLog: aLaunchThatFinishedTheDayBefore(pollMs),+ ...(earlierLog.unreadableFromTheBootUntilMainsFirstHeartbeat ? { unreadable } : {}),+ };+}++export interface GiveUp {+ gaveUpAtMs: number;+ message: string;+ reason: ReadyWaitGiveUpReason;+ decidingCapMs: number | undefined;+ declaredGrantMs: number | undefined;+ advancements: readonly LaunchAdvancement[];+}++type Reach = GiveUp | { admittedAtMs: number } | { stillWaitingAtMs: number };++function isGiveUp(reach: Reach): reach is GiveUp {+ return 'gaveUpAtMs' in reach;+}++export function giveUpOf(reach: Reach, launch: ScriptedLaunch): GiveUp {+ assert(+ isGiveUp(reach),+ `the readiness wait on ${launch.name} gave up, but it reached ${JSON.stringify(reach)}`,+ );+ return reach;+}++const reachCache = new Map<string, Reach>();++function byTime(lines: readonly NarrationLine[]): NarrationLine[] {+ return [...lines].sort((a, b) => a.atMs - b.atMs);+}++function dailyLogFileOf(launchLines: readonly NarrationLine[]): string {+ const startedAt = new Date(WAIT_STARTED_AT + (launchLines[0]?.atMs ?? 0));+ return `desktop.${startedAt.toISOString().slice(0, 10)}.log`;+}++async function waitOutLaunch(launch: ScriptedLaunch, watchMs: number): Promise<Reach> {+ const home = mkdtempSync(join(tmpdir(), 'ok-readiness-reach-'));+ vi.useFakeTimers({ now: WAIT_STARTED_AT, toFake: ['setTimeout', 'clearTimeout', 'Date'] });+ try {+ mkdirSync(bootLogDirFor(home), { recursive: true });+ const logs: { which: HomeLog; lines: NarrationLine[] }[] = [+ { which: 'own', lines: byTime(launch.narrationUntil(watchMs)) },+ ...(launch.earlierLog === undefined+ ? []+ : [{ which: 'earlier' as const, lines: byTime(launch.earlierLog) }]),+ ];+ const { unreadable } = launch;+ const publish = (atMs: number): void => {+ for (const log of logs) {+ const file = join(bootLogDirFor(home), dailyLogFileOf(log.lines));+ rmSync(file, { recursive: true, force: true });+ if (+ unreadable?.log === log.which &&+ atMs >= unreadable.fromMs &&+ atMs < unreadable.untilMs+ ) {+ mkdirSync(file);+ continue;+ }+ writeFileSync(+ file,+ log.lines+ .filter((line) => line.atMs <= atMs)+ .map((line) => `${line.text}\n`)+ .join(''),+ 'utf8',+ );+ }+ };+ publish(0);+ const changesAtMs = new Set([+ ...logs.flatMap((log) => log.lines.map((line) => line.atMs)),+ ...(unreadable === undefined ? [] : [unreadable.fromMs, unreadable.untilMs]),+ ]);+ for (const atMs of [...changesAtMs].sort((a, b) => a - b)) {+ if (atMs > 0) setTimeout(() => publish(atMs), atMs);+ }+ const neverAnswers = { evaluate: () => new Promise<string | undefined>(() => {}) };+ const app = { windows: () => [neverAnswers] };+ let reach: Reach | undefined;+ let failure: { error: unknown } | undefined;+ void waitForWindowByMode(app, 'editor', {+ home,+ ...launch.options,+ }).then(+ () => {+ reach = { admittedAtMs: Date.now() - WAIT_STARTED_AT };+ },+ (error: unknown) => {+ const recorded = readyWaitsFor(app)?.[0];+ if (recorded === undefined || recorded.reason === 'none') {+ failure = { error };+ return;+ }+ reach = {+ gaveUpAtMs: Date.now() - WAIT_STARTED_AT,+ message: error instanceof Error ? error.message : String(error),+ reason: recorded.reason,+ decidingCapMs: recorded.capMs,+ declaredGrantMs: recorded.declaredGrantMs,+ advancements: recorded.advancements,+ };+ },+ );+ await vi.advanceTimersByTimeAsync(watchMs);+ if (failure !== undefined) throw failure.error;+ return reach ?? { stillWaitingAtMs: Date.now() - WAIT_STARTED_AT };+ } finally {+ vi.useRealTimers();+ rmSync(home, { recursive: true, force: true });+ }+}++export async function reachOf(launch: ScriptedLaunch, watchMs: number): Promise<Reach> {+ const key = JSON.stringify([launch.name, launch.options, watchMs]);+ const cached = reachCache.get(key);+ if (cached !== undefined) return cached;+ const reach = await waitOutLaunch(launch, watchMs);+ reachCache.set(key, reach);+ return reach;+}diff --git a/packages/desktop/tests/smoke/consent-dialog.e2e.ts b/packages/desktop/tests/smoke/consent-dialog.e2e.tsindex 7eb53e0a7..18cddb8bd 100644--- a/packages/desktop/tests/smoke/consent-dialog.e2e.ts+++ b/packages/desktop/tests/smoke/consent-dialog.e2e.ts@@ -17,6 +17,7 @@ import { desktopLaunchOptions, resolveDesktopTarget } from './_helpers/launch-deimport { launchDesktopApp, waitForWindowByMode } from './_helpers/launch-readiness';import { seedMcpConsentComplete } from './_helpers/mcp-consent';import { clickNavOpen } from './_helpers/navigator-actions';+import { sumOfDeclaredBoundsMs } from './_helpers/parse-timeouts';import {homeEnv,PLATFORM_SKIP_REASON,@@ -126,6 +127,7 @@ test.describe('Consent-dialog smoke', () => {});test('Enter on a focused dialog input fires Start', async ({ captureStderrFor }) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const tmpHome = seedTmpHome('enter-to-start');const projectDir = seedFreshNonGitProject('enter-to-start');trackForCleanup(tmpHome, projectDir);@@ -179,6 +181,7 @@ test.describe('Consent-dialog smoke', () => {test('Pick Existing on a sub-folder of a git repo lands .ok/ at the git root', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const tmpHome = seedTmpHome('git-root-promote');const { repoRoot, subFolder } = seedGitRepoWithSubFolder(tmpHome, 'git-root-promote');trackForCleanup(tmpHome);diff --git a/packages/desktop/tests/smoke/terminal-dock-state.e2e.ts b/packages/desktop/tests/smoke/terminal-dock-state.e2e.tsindex a4de02f83..5565f45a9 100644--- a/packages/desktop/tests/smoke/terminal-dock-state.e2e.ts+++ b/packages/desktop/tests/smoke/terminal-dock-state.e2e.ts@@ -1,11 +1,16 @@import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';-import type { Page } from '@playwright/test';import { _electron as electron } from '@playwright/test';import { waitForAgentsDockPublish } from './_helpers/dock-state-ready';import { desktopLaunchOptions, resolveDesktopTarget } from './_helpers/launch-desktop';-import { PTY_PLATFORM_SKIP_REASON, PTY_PLATFORM_SUPPORTED } from './_helpers/platform-gate';+import { waitForWindowByMode } from './_helpers/launch-readiness';+import { sumOfDeclaredBoundsMs } from './_helpers/parse-timeouts';+import {+ homeEnv,+ PTY_PLATFORM_SKIP_REASON,+ PTY_PLATFORM_SUPPORTED,+} from './_helpers/platform-gate';import { expect, test } from './_helpers/smoke-test';const TARGET = resolveDesktopTarget();@@ -17,8 +22,10 @@ test.describe('terminal dock-state IPC', () => {test.skip(!TARGET.exists, TARGET.missingReason);test('terminal tab snapshot survives renderer reload together', async ({ captureStderrFor }) => {- test.setTimeout(200_000);+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const testRoot = mkdtempSync(join(tmpdir(), 'ok-terminal-dock-state-'));+ const tmpHome = join(testRoot, 'home');+ mkdirSync(tmpHome, { recursive: true });const projectDir = join(testRoot, 'project');mkdirSync(join(projectDir, '.ok'), { recursive: true });writeFileSync(join(projectDir, '.ok', 'config.yml'), "content:\n dir: '.'\n");@@ -43,25 +50,11 @@ test.describe('terminal dock-state IPC', () => {target: TARGET,args: [`--user-data-dir=${userDataDir}`],timeout: 30_000,- env: { ...process.env, OK_RECLAIM_DISABLE: '1' },+ env: { ...process.env, ...homeEnv(tmpHome), OK_RECLAIM_DISABLE: '1' },}),);- captureStderrFor(app, { cleanupDirs: [testRoot] });- let page: Page | undefined;- await expect(async () => {- for (const candidate of app.windows()) {- const mode = await candidate- .evaluate(() => window.okDesktop?.config?.mode)- .catch(() => undefined);- if (mode === 'editor') {- page = candidate;- return;- }- }- throw new Error('no editor window yet');- }).toPass({ timeout: 25_000 });- const editorPage = page;- if (editorPage == null) throw new Error('editor window vanished after readiness poll');+ captureStderrFor(app, { home: tmpHome, cleanupDirs: [testRoot] });+ const editorPage = await waitForWindowByMode(app, 'editor');await waitForAgentsDockPublish(() => editorPage.evaluate(() => window.okDesktop?.terminal.getDockState()),diff --git a/packages/desktop/tests/smoke/terminal-dock.e2e.ts b/packages/desktop/tests/smoke/terminal-dock.e2e.tsindex c808b1ab5..64ff7d305 100644--- a/packages/desktop/tests/smoke/terminal-dock.e2e.ts+++ b/packages/desktop/tests/smoke/terminal-dock.e2e.ts@@ -20,6 +20,7 @@ import {} from './_helpers/dock-reveal-latency';import { desktopLaunchOptions, resolveDesktopTarget } from './_helpers/launch-desktop';import { launchDesktopApp, waitForWindowByMode } from './_helpers/launch-readiness';+import { sumOfDeclaredBoundsMs } from './_helpers/parse-timeouts';import {PTY_PLATFORM_SKIP_REASON,PTY_PLATFORM_SUPPORTED,@@ -161,19 +162,17 @@ async function findEditorWindow(app: ElectronApplication): Promise<Page> {}async function dispatchRendererMenuAction(- app: ElectronApplication,action: 'move-terminal' | 'toggle-agent-panel' | 'toggle-terminal',- editorPage?: Page,+ editorPage: Page,): Promise<void> {- const page = editorPage ?? (await findEditorWindow(app));- await page.evaluate(async (menuAction) => {+ await editorPage.evaluate(async (menuAction) => {const menu = window.okDesktop?.menu;if (!menu) throw new Error('renderer menu bridge is unavailable');await menu.dispatch({ kind: 'menu-action', action: menuAction });}, action);}-async function clickViewTerminalItem(app: ElectronApplication, editorPage?: Page): Promise<string> {+async function clickViewTerminalItem(app: ElectronApplication, editorPage: Page): Promise<string> {const label = await app.evaluate(async ({ Menu }) => {const menu = Menu.getApplicationMenu();if (!menu) throw new Error('application menu is unavailable');@@ -187,7 +186,7 @@ async function clickViewTerminalItem(app: ElectronApplication, editorPage?: Pagereturn label;});if (process.platform !== 'darwin')- await dispatchRendererMenuAction(app, 'toggle-terminal', editorPage);+ await dispatchRendererMenuAction('toggle-terminal', editorPage);return label;}@@ -230,9 +229,12 @@ async function terminalPlacementLabel(app: ElectronApplication): Promise<string>});}-async function clickTerminalPlacementItem(app: ElectronApplication): Promise<void> {+async function clickTerminalPlacementItem(+ app: ElectronApplication,+ editorPage: Page,+): Promise<void> {if (process.platform !== 'darwin') {- await dispatchRendererMenuAction(app, 'move-terminal');+ await dispatchRendererMenuAction('move-terminal', editorPage);return;}await app.evaluate(async ({ Menu }) => {@@ -245,9 +247,9 @@ async function clickTerminalPlacementItem(app: ElectronApplication): Promise<voi});}-async function clickViewAgentsItem(app: ElectronApplication): Promise<void> {+async function clickViewAgentsItem(app: ElectronApplication, editorPage: Page): Promise<void> {if (process.platform !== 'darwin') {- await dispatchRendererMenuAction(app, 'toggle-agent-panel');+ await dispatchRendererMenuAction('toggle-agent-panel', editorPage);return;}await app.evaluate(async ({ Menu }) => {@@ -325,7 +327,7 @@ async function widenEditorWindow(async function revealTerminalSurface(app: ElectronApplication, target: Locator): Promise<void> {await expect(async () => {if (await target.isVisible()) return;- await clickViewTerminalItem(app);+ await clickViewTerminalItem(app, target.page());await expect(target).toBeVisible({ timeout: 5_000 });}).toPass({ timeout: 15_000 });}@@ -461,7 +463,7 @@ test.describe('Docked terminal — live Electron', () => {await revealTerminalSurface(app, terminalSection(page));await expect.poll(() => viewTerminalLabel(app), { timeout: 8_000 }).toBe('Hide Terminal');- await clickViewTerminalItem(app);+ await clickViewTerminalItem(app, page);await expect.poll(() => viewTerminalLabel(app), { timeout: 8_000 }).toBe('Show Terminal');});@@ -472,15 +474,15 @@ test.describe('Docked terminal — live Electron', () => {track(s.tmpHome, s.projectDir);const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });- await findEditorWindow(app);+ const page = await findEditorWindow(app);expect(await terminalPlacementLabel(app)).toBe('Move Terminal to right');- await clickTerminalPlacementItem(app);+ await clickTerminalPlacementItem(app, page);await expect.poll(() => terminalPlacementLabel(app), { timeout: 8_000 }).toBe('Move Terminal to bottom');- await clickTerminalPlacementItem(app);+ await clickTerminalPlacementItem(app, page);await expect.poll(() => terminalPlacementLabel(app), { timeout: 8_000 }).toBe('Move Terminal to right');@@ -594,6 +596,7 @@ test.describe('Docked terminal — live Electron', () => {test('right Terminal and Agents exclude each other only when the window is infeasible', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('rail-admission', { consent: true, skipRestoreState: true });track(s.tmpHome, s.projectDir);const app = await launchApp(s);@@ -602,12 +605,12 @@ test.describe('Docked terminal — live Electron', () => {await widenEditorWindow(app, page, 1900, 900);await openTerminal(app, page);- await clickTerminalPlacementItem(app);+ await clickTerminalPlacementItem(app, page);await expect(page.locator('#terminal-column section[aria-label="Terminal"]')).toBeVisible({timeout: 10_000,});await waitForStatus(page, 'running', 25_000);- await clickViewAgentsItem(app);+ await clickViewAgentsItem(app, page);await expect(page.locator('#agents-column')).toBeVisible({ timeout: 10_000 });await expect.poll(@@ -654,14 +657,14 @@ test.describe('Docked terminal — live Electron', () => {'Terminal closed to make room for the agent panel.',transientNoticeObservation(page, {document: 'current',- trigger: () => clickViewAgentsItem(app),+ trigger: () => clickViewAgentsItem(app, page),}),{ timeout: 10_000 },);await expect(page.locator('#agents-column')).toBeVisible({ timeout: 10_000 });await expectCollapsedRailColumn(page, '#terminal-column');- await clickViewAgentsItem(app);+ await clickViewAgentsItem(app, page);await expectCollapsedRailColumn(page, '#agents-column');await expectCollapsedRailColumn(page, '#terminal-column');});@@ -722,6 +725,7 @@ test.describe('Docked terminal — live Electron', () => {test('a window-resize storm keeps the shell responsive and settles the PTY at the fitted grid', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('resize-storm', { consent: true });track(s.tmpHome, s.projectDir);const app = await launchApp(s, { restrictPath: true });@@ -837,7 +841,7 @@ test.describe('Docked terminal — live Electron', () => {await page.keyboard.press('Escape');await expect.poll(focusInTerminal).toBe(true);- await clickViewTerminalItem(app);+ await clickViewTerminalItem(app, page);await expect.poll(focusInTerminal).toBe(false);});@@ -881,7 +885,7 @@ test.describe('Docked terminal — live Electron', () => {await ensureBottomDock(page);await page.locator('section[aria-label="Terminal"] .xterm').click();- await clickViewTerminalItem(app);+ await clickViewTerminalItem(app, page);await expect(page.locator('#terminal-dock-panel')).toHaveAttribute('inert', '', {timeout: 10_000,});@@ -927,9 +931,9 @@ test.describe('Docked terminal — live Electron', () => {.poll(() => page.evaluate(() => Number(localStorage.getItem('ok-terminal-height-v1') ?? 0))).toBeGreaterThan(heightBefore);- await clickViewTerminalItem(app);+ await clickViewTerminalItem(app, page);await expect(panel).toHaveAttribute('inert', '', { timeout: 10_000 });- await clickViewTerminalItem(app);+ await clickViewTerminalItem(app, page);await expect(terminalSection(page)).toBeVisible({ timeout: 10_000 });await expect.poll(async () => {@@ -942,6 +946,7 @@ test.describe('Docked terminal — live Electron', () => {test('QA-015/032 shell exit shows restart; banner hidden on exit', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('exit', { consent: true });track(s.tmpHome, s.projectDir);const app = await launchApp(s, { restrictPath: true });@@ -1157,6 +1162,7 @@ test.describe('Docked terminal — live Electron', () => {test('a renderer reload preserves the open terminal and its live session', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('reload-survival', { consent: true });track(s.tmpHome, s.projectDir);const app = await launchApp(s, { restrictPath: true });@@ -1188,7 +1194,7 @@ test.describe('Docked terminal — live Electron', () => {test('the primary-modifier J shortcuts stage a new CLI tab and toggle visibility', async ({captureStderrFor,}) => {- test.setTimeout(200_000);+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('stage', { consent: true, fakeClaudeTui: true, skipRestoreState: true });track(s.tmpHome, s.projectDir);const app = await launchApp(s, { restrictPath: true });@@ -1227,11 +1233,11 @@ test.describe('Docked terminal — live Electron', () => {.toContain('OKSTAGE_REUSE_742');await waitForMenuSelectionState(page, true);- expect(await clickViewTerminalItem(app)).toBe('Hide Terminal');+ expect(await clickViewTerminalItem(app, page)).toBe('Hide Terminal');await expect(terminalSection(page)).toBeHidden();await expect.poll(() => viewTerminalLabel(app), { timeout: 8_000 }).toBe('Show Terminal');- expect(await clickViewTerminalItem(app)).toBe('Show Terminal');+ expect(await clickViewTerminalItem(app, page)).toBe('Show Terminal');await expect(terminalSection(page)).toBeVisible();await waitForStatus(page, 'running', 25_000, { foreground: 'program' });expect(await terminalTabs().count()).toBe(tabsAfterLaunch);diff --git a/packages/desktop/tests/smoke/terminal-launch-argv.e2e.ts b/packages/desktop/tests/smoke/terminal-launch-argv.e2e.tsindex ca43b7eb1..8afe7896d 100644--- a/packages/desktop/tests/smoke/terminal-launch-argv.e2e.ts+++ b/packages/desktop/tests/smoke/terminal-launch-argv.e2e.ts@@ -12,7 +12,9 @@ import { join } from 'node:path';import type { ElectronApplication, Locator, Page } from '@playwright/test';import { _electron as electron } from '@playwright/test';import { desktopLaunchOptions, resolveDesktopTarget } from './_helpers/launch-desktop';+import { waitForWindowByMode } from './_helpers/launch-readiness';import { seedMcpConsentComplete } from './_helpers/mcp-consent';+import { sumOfDeclaredBoundsMs } from './_helpers/parse-timeouts';import {PTY_PLATFORM_SKIP_REASON,PTY_PLATFORM_SUPPORTED,@@ -114,23 +116,11 @@ async function launchApp(s: Seed): Promise<ElectronApplication> {);}-async function findEditorWindow(app: ElectronApplication, timeoutMs = 25_000): Promise<Page> {- let page: Page | undefined;- await expect(async () => {- for (const p of app.windows()) {- const mode = await p.evaluate(() => window.okDesktop?.config?.mode).catch(() => undefined);- if (mode === 'editor') {- page = p;- return;- }- }- throw new Error('no editor window yet');- }).toPass({ timeout: timeoutMs });- if (!page) throw new Error('editor window vanished after readiness poll');- return page;+async function findEditorWindow(app: ElectronApplication): Promise<Page> {+ return waitForWindowByMode(app, 'editor');}-async function clickViewTerminalItem(app: ElectronApplication): Promise<void> {+async function clickViewTerminalItem(app: ElectronApplication, editorPage: Page): Promise<void> {await app.evaluate(async ({ Menu }) => {const menu = Menu.getApplicationMenu();if (!menu) throw new Error('application menu is unavailable');@@ -142,8 +132,7 @@ async function clickViewTerminalItem(app: ElectronApplication): Promise<void> {if (process.platform === 'darwin') item.click();});if (process.platform !== 'darwin') {- const page = await findEditorWindow(app);- await page.evaluate(async () => {+ await editorPage.evaluate(async () => {const menu = window.okDesktop?.menu;if (!menu) throw new Error('renderer menu bridge is unavailable');await menu.dispatch({ kind: 'menu-action', action: 'toggle-terminal' });@@ -166,7 +155,7 @@ async function readTerminalText(page: Page): Promise<string> {async function openTerminal(app: ElectronApplication, page: Page): Promise<void> {await expect(async () => {if (await terminalSection(page).isVisible()) return;- await clickViewTerminalItem(app);+ await clickViewTerminalItem(app, page);await expect(terminalSection(page)).toBeVisible({ timeout: 1_000 });}).toPass({ timeout: 15_000 });await expect(page.locator('[data-terminal-status]')).toHaveAttribute(@@ -206,9 +195,10 @@ test.describe('Docked terminal launch — composed argv', () => {test('a Claude launch hands the PTY the settings payload and the prompt verbatim', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed();const app = await launchApp(s);- captureStderrFor(app, { cleanupDirs: [s.tmpHome, s.projectDir] });+ captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });const page = await findEditorWindow(app);await openTerminal(app, page);diff --git a/packages/desktop/tests/smoke/terminal-links.e2e.ts b/packages/desktop/tests/smoke/terminal-links.e2e.tsindex d5f8a57c4..22b3b97dd 100644--- a/packages/desktop/tests/smoke/terminal-links.e2e.ts+++ b/packages/desktop/tests/smoke/terminal-links.e2e.ts@@ -5,6 +5,7 @@ import type { ElectronApplication, Page } from '@playwright/test';import { _electron as electron } from '@playwright/test';import { removeTempDirBestEffort } from '../support/temp-dir-cleanup.test-helper';import { desktopLaunchOptions, resolveDesktopTarget } from './_helpers/launch-desktop';+import { waitForWindowByMode } from './_helpers/launch-readiness';import {PTY_PLATFORM_SKIP_REASON,PTY_PLATFORM_SUPPORTED,@@ -80,20 +81,8 @@ async function launchApp(s: Seed): Promise<ElectronApplication> {);}-async function findEditorWindow(app: ElectronApplication, timeoutMs = 25_000): Promise<Page> {- let page: Page | undefined;- await expect(async () => {- for (const p of app.windows()) {- const mode = await p.evaluate(() => window.okDesktop?.config?.mode).catch(() => undefined);- if (mode === 'editor') {- page = p;- return;- }- }- throw new Error('no editor window yet');- }).toPass({ timeout: timeoutMs });- if (!page) throw new Error('editor window vanished after readiness poll');- return page;+async function findEditorWindow(app: ElectronApplication): Promise<Page> {+ return waitForWindowByMode(app, 'editor');}async function openRunningTerminal(app: ElectronApplication, page: Page): Promise<void> {diff --git a/packages/desktop/tests/smoke/terminal-movement.e2e.ts b/packages/desktop/tests/smoke/terminal-movement.e2e.tsindex 0aa155eb7..a5d500ac1 100644--- a/packages/desktop/tests/smoke/terminal-movement.e2e.ts+++ b/packages/desktop/tests/smoke/terminal-movement.e2e.ts@@ -5,6 +5,8 @@ import { join } from 'node:path';import type { ElectronApplication, ElementHandle, JSHandle, Page } from '@playwright/test';import { _electron as electron } from '@playwright/test';import { desktopLaunchOptions, resolveDesktopTarget } from './_helpers/launch-desktop';+import { waitForWindowByMode } from './_helpers/launch-readiness';+import { sumOfDeclaredBoundsMs } from './_helpers/parse-timeouts';import {PTY_PLATFORM_SKIP_REASON,PTY_PLATFORM_SUPPORTED,@@ -108,40 +110,24 @@ async function launchApp(s: Seed): Promise<ElectronApplication> {);}-async function findEditorWindow(app: ElectronApplication, timeoutMs = 25_000): Promise<Page> {- let page: Page | undefined;- await expect(async () => {- for (const candidate of app.windows()) {- const mode = await candidate- .evaluate(() => window.okDesktop?.config?.mode)- .catch(() => undefined);- if (mode === 'editor') {- page = candidate;- return;- }- }- throw new Error('no editor window yet');- }).toPass({ timeout: timeoutMs });- if (!page) throw new Error('editor window vanished after readiness poll');- return page;+async function findEditorWindow(app: ElectronApplication): Promise<Page> {+ return waitForWindowByMode(app, 'editor');}async function dispatchRendererMenuAction(- app: ElectronApplication,action: 'move-terminal' | 'toggle-agent-panel' | 'toggle-terminal',- editorPage?: Page,+ editorPage: Page,): Promise<void> {- const page = editorPage ?? (await findEditorWindow(app));- await page.evaluate(async (menuAction) => {+ await editorPage.evaluate(async (menuAction) => {const menu = window.okDesktop?.menu;if (!menu) throw new Error('renderer menu bridge is unavailable');await menu.dispatch({ kind: 'menu-action', action: menuAction });}, action);}-async function clickViewTerminalItem(app: ElectronApplication): Promise<void> {+async function clickViewTerminalItem(app: ElectronApplication, editorPage: Page): Promise<void> {if (process.platform !== 'darwin') {- await dispatchRendererMenuAction(app, 'toggle-terminal');+ await dispatchRendererMenuAction('toggle-terminal', editorPage);return;}await app.evaluate(async ({ Menu }) => {@@ -154,9 +140,9 @@ async function clickViewTerminalItem(app: ElectronApplication): Promise<void> {});}-async function clickViewAgentsItem(app: ElectronApplication): Promise<void> {+async function clickViewAgentsItem(app: ElectronApplication, editorPage: Page): Promise<void> {if (process.platform !== 'darwin') {- await dispatchRendererMenuAction(app, 'toggle-agent-panel');+ await dispatchRendererMenuAction('toggle-agent-panel', editorPage);return;}await app.evaluate(async ({ Menu }) => {@@ -171,10 +157,10 @@ async function clickViewAgentsItem(app: ElectronApplication): Promise<void> {async function clickTerminalPlacementItem(app: ElectronApplication,- editorPage?: Page,+ editorPage: Page,): Promise<void> {if (process.platform !== 'darwin') {- await dispatchRendererMenuAction(app, 'move-terminal', editorPage);+ await dispatchRendererMenuAction('move-terminal', editorPage);return;}await app.evaluate(async ({ Menu }) => {@@ -190,11 +176,11 @@ async function clickTerminalPlacementItem(async function clickTerminalPlacementItemRapidly(app: ElectronApplication,count: number,- editorPage?: Page,+ editorPage: Page,): Promise<void> {if (process.platform !== 'darwin') {for (let index = 0; index < count; index += 1) {- await dispatchRendererMenuAction(app, 'move-terminal', editorPage);+ await dispatchRendererMenuAction('move-terminal', editorPage);}return;}@@ -255,7 +241,7 @@ async function openTerminal(app: ElectronApplication, page: Page): Promise<void>const terminal = visibleTerminal(page);await expect(async () => {if (await terminal.isVisible()) return;- await clickViewTerminalItem(app);+ await clickViewTerminalItem(app, page);await expect(terminal).toBeVisible({ timeout: 5_000 });}).toPass({ timeout: 15_000 });await expect(terminal.locator('[data-terminal-status]')).toHaveAttribute(@@ -512,7 +498,7 @@ test.describe('Terminal placement continuity — live Electron', () => {test.skip(!TARGET.exists, TARGET.missingReason);test('moving a populated terminal preserves every live session', async ({ captureStderrFor }) => {- test.setTimeout(260_000);+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed();const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });@@ -601,7 +587,7 @@ test.describe('Terminal placement continuity — live Electron', () => {test('renderer restart restores the right layout and its live active terminal', async ({captureStderrFor,}) => {- test.setTimeout(300_000);+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed({ skipRestoreState: true });const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });@@ -670,7 +656,7 @@ test.describe('Terminal placement continuity — live Electron', () => {await typeInActiveTerminal(page, `${SHELL_COMMANDS.output(afterRestart)}\r`);await expect.poll(() => readActiveTerminal(page), { timeout: 15_000 }).toContain(afterRestart);- await clickViewAgentsItem(app);+ await clickViewAgentsItem(app, page);await expect(page.locator('#agents-column')).toBeVisible({ timeout: 10_000 });const editorWindow = await app.browserWindow(page);await editorWindow.evaluate((windowHandle: unknown) => {@@ -725,6 +711,7 @@ test.describe('Terminal placement continuity — live Electron', () => {test('fresh and malformed layout state recover to a usable bottom terminal', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed();const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });diff --git a/packages/desktop/tests/smoke/terminal-process-restart.e2e.ts b/packages/desktop/tests/smoke/terminal-process-restart.e2e.tsindex 92db69a98..21e371b78 100644--- a/packages/desktop/tests/smoke/terminal-process-restart.e2e.ts+++ b/packages/desktop/tests/smoke/terminal-process-restart.e2e.ts@@ -5,6 +5,8 @@ import { join } from 'node:path';import type { ElectronApplication, Page } from '@playwright/test';import { _electron as electron } from '@playwright/test';import { desktopLaunchOptions, resolveDesktopTarget } from './_helpers/launch-desktop';+import { waitForWindowByMode } from './_helpers/launch-readiness';+import { sumOfDeclaredBoundsMs } from './_helpers/parse-timeouts';import {PTY_PLATFORM_SKIP_REASON,PTY_PLATFORM_SUPPORTED,@@ -86,21 +88,7 @@ async function launchRestartProfile(seed: RestartSeed): Promise<ElectronApplicat}async function findEditorWindow(app: ElectronApplication): Promise<Page> {- let page: Page | undefined;- await expect(async () => {- for (const candidate of app.windows()) {- const mode = await candidate- .evaluate(() => window.okDesktop?.config?.mode)- .catch(() => undefined);- if (mode === 'editor') {- page = candidate;- return;- }- }- throw new Error('editor window unavailable');- }).toPass({ timeout: 25_000 });- if (!page) throw new Error('editor window vanished');- return page;+ return waitForWindowByMode(app, 'editor');}async function setWindowSize(@@ -198,7 +186,7 @@ test.describe('terminal process restart', () => {test('restores placement, width, tab order, and active tab in a separate Electron process', async ({captureStderrFor,}) => {- test.setTimeout(240_000);+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const seed = seedRestartProfile();const firstApp = await launchRestartProfile(seed);captureStderrFor(firstApp, { home: seed.tmpHome });diff --git a/packages/desktop/tests/smoke/terminal-tabs.e2e.ts b/packages/desktop/tests/smoke/terminal-tabs.e2e.tsindex 54f14509b..ef01bb36c 100644--- a/packages/desktop/tests/smoke/terminal-tabs.e2e.ts+++ b/packages/desktop/tests/smoke/terminal-tabs.e2e.ts@@ -5,6 +5,7 @@ import type { ElectronApplication, Locator, Page } from '@playwright/test';import { _electron as electron } from '@playwright/test';import { desktopLaunchOptions, resolveDesktopTarget } from './_helpers/launch-desktop';import { launchDesktopApp, waitForWindowByMode } from './_helpers/launch-readiness';+import { sumOfDeclaredBoundsMs } from './_helpers/parse-timeouts';import {PTY_PLATFORM_SKIP_REASON,PTY_PLATFORM_SUPPORTED,@@ -276,6 +277,7 @@ test.describe('Terminal tabs — live Electron', () => {test('a second tab spawns its own live shell (independent sessions)', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('two-shells');const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });@@ -321,6 +323,7 @@ test.describe('Terminal tabs — live Electron', () => {test('closing a tab reaps only that shell; the survivor stays interactive', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('close-one');const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });@@ -344,6 +347,7 @@ test.describe('Terminal tabs — live Electron', () => {});test('a manual rename pins over the program’s OSC title', async ({ captureStderrFor }) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('rename-pin');const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });@@ -424,6 +428,7 @@ test.describe('Terminal tabs — live Electron', () => {test('keyboard reorder changes order, keeps sticky numbers, and preserves the live shell', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('reorder-survive');const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });@@ -471,6 +476,7 @@ test.describe('Terminal tabs — live Electron', () => {test('pointer-drag reorder changes order and preserves the live shell', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('drag-survive');const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });@@ -513,6 +519,7 @@ test.describe('Terminal tabs — live Electron', () => {});test('a renderer reload preserves tab labels and order', async ({ captureStderrFor }) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('reload-preserve');const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });diff --git a/packages/desktop/tests/smoke/terminal-window.e2e.ts b/packages/desktop/tests/smoke/terminal-window.e2e.tsindex caaa8c719..ba71a68ff 100644--- a/packages/desktop/tests/smoke/terminal-window.e2e.ts+++ b/packages/desktop/tests/smoke/terminal-window.e2e.ts@@ -4,6 +4,8 @@ import { basename, join } from 'node:path';import type { ElectronApplication, Page } from '@playwright/test';import { _electron as electron } from '@playwright/test';import { desktopLaunchOptions, resolveDesktopTarget } from './_helpers/launch-desktop';+import { waitForWindowByMode } from './_helpers/launch-readiness';+import { sumOfDeclaredBoundsMs } from './_helpers/parse-timeouts';import {PTY_PLATFORM_SKIP_REASON,PTY_PLATFORM_SUPPORTED,@@ -71,24 +73,12 @@ async function launchApp(s: Seed): Promise<ElectronApplication> {);}-async function findWindowByMode(- app: ElectronApplication,- mode: 'editor' | 'terminal',- timeoutMs = 25_000,-): Promise<Page> {- let page: Page | undefined;- await expect(async () => {- for (const p of app.windows()) {- const m = await p.evaluate(() => window.okDesktop?.config?.mode).catch(() => undefined);- if (m === mode) {- page = p;- return;- }- }- throw new Error(`no ${mode} window yet`);- }).toPass({ timeout: timeoutMs });- if (!page) throw new Error(`${mode} window vanished after readiness poll`);- return page;+async function findEditorWindow(app: ElectronApplication): Promise<Page> {+ return waitForWindowByMode(app, 'editor');+}++async function findTerminalWindow(app: ElectronApplication): Promise<Page> {+ return waitForWindowByMode(app, 'terminal', { liveness: 'none' });}async function waitForRendererResponsive(page: Page): Promise<void> {@@ -158,11 +148,12 @@ test.describe('Standalone terminal window — live Electron', () => {test('New Terminal Window opens a window with a live shell at the project root; close-last closes it', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('open-close');track(s.tmpHome, s.projectDir);const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });- const editor = await findWindowByMode(app, 'editor');+ const editor = await findEditorWindow(app);await waitForRendererResponsive(editor);const editorWindow = await app.browserWindow(editor);const editorWebContentsId = await editorWindow.evaluate(@@ -170,7 +161,7 @@ test.describe('Standalone terminal window — live Electron', () => {);expect(await clickNewTerminalWindow(app, editorWebContentsId)).toBe(true);- const term = await findWindowByMode(app, 'terminal');+ const term = await findTerminalWindow(app);expect(await term.evaluate(() => window.okDesktop?.config.collabUrl)).not.toBe('');await expect(term.locator('[data-terminal-status]').first()).toHaveAttribute(@@ -203,14 +194,15 @@ test.describe('Standalone terminal window — live Electron', () => {test('opening New Terminal Window twice yields two independent terminal windows', async ({captureStderrFor,}) => {+ test.setTimeout(sumOfDeclaredBoundsMs(test.info()));const s = seed('multi');track(s.tmpHome, s.projectDir);const app = await launchApp(s);captureStderrFor(app, { home: s.tmpHome, cleanupDirs: [s.tmpHome, s.projectDir] });- await waitForRendererResponsive(await findWindowByMode(app, 'editor'));+ await waitForRendererResponsive(await findEditorWindow(app));expect(await clickNewTerminalWindow(app)).toBe(true);- await findWindowByMode(app, 'terminal');+ await findTerminalWindow(app);expect(await clickNewTerminalWindow(app)).toBe(true);await expect.poll(() => terminalWindowCount(app), { timeout: 20_000 }).toBe(2);