diff --git a/src/browser/PlaywrightExecutionBoundary.ts b/src/browser/PlaywrightExecutionBoundary.ts index 9fd21fcf3..f340d73d3 100644 --- a/src/browser/PlaywrightExecutionBoundary.ts +++ b/src/browser/PlaywrightExecutionBoundary.ts @@ -2,6 +2,14 @@ import { AnalysisCancelledError, AnalysisTimeoutError, } from "../domain/analysisErrorCore.js"; +import { WEB_RUNTIME_LIMITS } from "../domain/webRuntime.js"; + +export const PLAYWRIGHT_NETWORK_READ_TIMEOUT_MS = 5_000; + +// Cleanup drains bounded content reads before shutting down the browser and +// removing its profile. The outer deadline must cover both sequential stages. +export const PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS = + PLAYWRIGHT_NETWORK_READ_TIMEOUT_MS + WEB_RUNTIME_LIMITS.cleanupTimeoutMs; const OPERATION = "capture_browser_scenario" as const; diff --git a/src/browser/PlaywrightScenarioBrowser.test.ts b/src/browser/PlaywrightScenarioBrowser.test.ts index 79e903975..23a0add37 100644 --- a/src/browser/PlaywrightScenarioBrowser.test.ts +++ b/src/browser/PlaywrightScenarioBrowser.test.ts @@ -2,6 +2,7 @@ import { afterEach, expect, it, vi } from "vitest"; import type { Browser } from "playwright-core"; import { browserScenarioSchema } from "../domain/browserScenario.js"; +import { PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS } from "./PlaywrightExecutionBoundary.js"; import { attachedScenarioLeaseKey, openPlaywrightScenarioBrowser, @@ -144,6 +145,35 @@ it("keeps attached-target ownership until cancelled cleanup actually settles", a expect(settled).toEqual([true]); }); +it("waits for bounded event finalization and owned browser teardown", async () => { + vi.useFakeTimers(); + try { + const closeBrowser = vi.fn( + () => new Promise((resolve) => setTimeout(resolve, 1_500)), + ); + const removeProfile = vi.fn(async () => undefined); + const cleanup = new PlaywrightScenarioBrowserCleanupOwner({ + closeBrowser, + removeProfile, + }); + const finished = cleanup + .close(() => new Promise((resolve) => setTimeout(resolve, 4_500))) + .then( + () => ({ ok: true }), + (cause: unknown) => ({ ok: false, cause }), + ); + await vi.advanceTimersByTimeAsync(4_500); + expect(removeProfile).not.toHaveBeenCalled(); + await vi.advanceTimersByTimeAsync(1_500); + expect(await finished).toEqual({ ok: true }); + expect(cleanup.browserClosed).toBe(true); + expect(closeBrowser).toHaveBeenCalledOnce(); + expect(removeProfile).toHaveBeenCalledOnce(); + } finally { + vi.useRealTimers(); + } +}); + it("keeps attached-target ownership after the cleanup response timeout", async () => { vi.useFakeTimers(); try { @@ -164,7 +194,7 @@ it("keeps attached-target ownership after the cleanup response timeout", async ( cleanup: { reason: expect.stringContaining("timed out") }, }); await closeStarted.promise; - await vi.advanceTimersByTimeAsync(1_000); + await vi.advanceTimersByTimeAsync(PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS); await timeoutExpectation; expect(settled).toEqual([]); diff --git a/src/browser/PlaywrightScenarioBrowser.ts b/src/browser/PlaywrightScenarioBrowser.ts index 615ad055e..35f2ba275 100644 --- a/src/browser/PlaywrightScenarioBrowser.ts +++ b/src/browser/PlaywrightScenarioBrowser.ts @@ -8,7 +8,10 @@ import type { BrowserScenario } from "../domain/browserScenario.js"; import { AnalysisError } from "../domain/analysisErrorBase.js"; import { isLiteralLoopbackHostname } from "../domain/browserObservation.js"; import { BrowserObservationError } from "../domain/browserObservationError.js"; -import { withPlaywrightExecutionBoundary } from "./PlaywrightExecutionBoundary.js"; +import { + PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS, + withPlaywrightExecutionBoundary, +} from "./PlaywrightExecutionBoundary.js"; const OPERATION = "capture_browser_scenario" as const; @@ -71,7 +74,11 @@ export class PlaywrightScenarioBrowserCleanupOwner { signal?: AbortSignal, ): Promise { try { - await withPlaywrightExecutionBoundary(() => cleanup, 1_000, signal); + await withPlaywrightExecutionBoundary( + () => cleanup, + PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS, + signal, + ); } catch (cause: unknown) { throw new BrowserObservationError(OPERATION, "cleanup_failed", { cause, diff --git a/src/browser/PlaywrightScenarioSession.ts b/src/browser/PlaywrightScenarioSession.ts index 486f555ea..da2751f7d 100644 --- a/src/browser/PlaywrightScenarioSession.ts +++ b/src/browser/PlaywrightScenarioSession.ts @@ -14,7 +14,10 @@ import { performPlaywrightScenarioAction } from "./PlaywrightScenarioActions.js" import { capturePlaywrightStepArtifacts } from "./PlaywrightScenarioArtifacts.js"; import { PlaywrightScenarioEvents } from "./PlaywrightScenarioEvents.js"; import { PlaywrightScenarioStorage } from "./PlaywrightScenarioStorage.js"; -import { withPlaywrightExecutionBoundary } from "./PlaywrightExecutionBoundary.js"; +import { + PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS, + withPlaywrightExecutionBoundary, +} from "./PlaywrightExecutionBoundary.js"; import { browserScenarioCaptureData, browserScenarioOperationFailure, @@ -154,7 +157,10 @@ export class PlaywrightScenarioSession implements BrowserScenarioSessionPort { () => undefined, ); options.retainCleanup?.(() => - withPlaywrightExecutionBoundary(lateCleanup, 1_000), + withPlaywrightExecutionBoundary( + lateCleanup, + PLAYWRIGHT_SCENARIO_CLEANUP_TIMEOUT_MS, + ), ); void lateCleanup().catch((cleanupCause: unknown) => { // best-effort cleanup: retainCleanup keeps the retry capability. diff --git a/src/browser/network/PlaywrightScenarioNetwork.ts b/src/browser/network/PlaywrightScenarioNetwork.ts index 45129cb95..01493b3a3 100644 --- a/src/browser/network/PlaywrightScenarioNetwork.ts +++ b/src/browser/network/PlaywrightScenarioNetwork.ts @@ -4,6 +4,7 @@ import type { } from "../../domain/browserNetworkEvidence.js"; import type { BrowserScenarioEvent } from "../../domain/browserScenarioCaptureValues.js"; import type { BrowserScenarioSecrets } from "../BrowserScenarioSecrets.js"; +import { PLAYWRIGHT_NETWORK_READ_TIMEOUT_MS } from "../PlaywrightExecutionBoundary.js"; import { captureNetworkBody, captureNetworkHeaders, @@ -76,7 +77,7 @@ export class PlaywrightScenarioNetwork { constructor(private readonly options: NetworkCaptureOptions) { this.readOptions = { - timeoutMs: options.readTimeoutMs ?? 5_000, + timeoutMs: options.readTimeoutMs ?? PLAYWRIGHT_NETWORK_READ_TIMEOUT_MS, ...(options.signal === undefined ? {} : { signal: options.signal }), }; }