feat(04-04): Wave 0 spike — stopServiceWorker helper + 5-min SW idle empirical result

SPIKE OUTCOME: FAILED (offscreen DIED across 5-min SW idle + worker.close())

Per Plan 04-04 spike-first contract, Wave 0 empirically investigated whether
the offscreen document's RAM-only `segments: Blob[] = []` at
src/offscreen/recorder.ts:91 survives a 5-min SW idle followed by Puppeteer
CDP-driven `worker.close()`. RESEARCH Q2 hypothesis (MEDIUM confidence): yes,
the offscreen has its own lifecycle anchored by active MediaRecorder. Spike
result REFUTES that hypothesis.

Empirical measurement (HEADLESS=1; one full run; reproducible via the
committed spike script):

  - assertA2 priming: PASSED (badge=REC; offscreen + MediaRecorder live)
  - 5-min idle:        elapsed cleanly (308.7s total wall-clock)
  - stopServiceWorker: succeeded (worker.close() returned)
  - SAVE_ARCHIVE ack:  {success: true} (SW respawned + processed message)
  - video/last_30sec.webm size: 8505 bytes (well below 100 KB floor)
  - meta.urls: only chrome-extension://* origins; real-page URLs LOST
  - rrweb/session.json: []
  - logs/events.json: []
  - ffprobe on extracted webm: 'End of file' + 'Duplicate element' errors
    (corrupt/truncated; not a valid 30s segment cluster sequence)

Interpretation: offscreen-document lifecycle is NOT independent of the SW
under Puppeteer CDP-driven worker.close() conditions. The 8505 bytes are
likely stale/partial header bytes from a re-initialized empty offscreen
context after SW respawn, not a surviving 30s buffer. The plan's Task 2
GATING CONDITION (videoSize > 100_000) is NOT satisfied; Task 2 is BLOCKED.

Per saved memory `feedback-gsd-ceremony-for-fixes.md`: architectural changes
(moving segments from offscreen RAM to IndexedDB per RESEARCH Q2 sub-question
b Option C) MUST route through proper plan-fix ceremony, NOT improvised
inline inside Plan 04-04. Plan 04-04 SUMMARY flags the failure mode + cites
exact remediation path. ROADMAP SC #1 remains OPEN pending the persistence-
layer plan-fix.

Task 1 persisting artifacts (this commit):
  - tests/uat/lib/harness-page-driver.ts:
    + Browser type import (puppeteer)
    + stopServiceWorker(browser, extensionId) helper (verbatim from Chrome
      devrel canonical pattern — Puppeteer >=22.1.0; project pin ^25 OK)
    + findLatestZip exported (was module-internal) so the spike script can
      reuse the canonical mtime-sort selection logic without duplication
  - tests/uat/spike-a33-sw-persistence.ts (NEW):
    + One-shot empirical investigation script; reusable for future SW-
      lifecycle regression testing (e.g., verifying the eventual IndexedDB
      persistence layer actually closes ROADMAP SC #1)
    + Step 1 reuses __mokoshHarness.assertA2 (canonical fresh-recording
      prime; not the non-existent dispatchSaveArchive that REVISION iter-2
      explicitly forbids)
    + Step 5 dispatches SAVE_ARCHIVE via chrome.runtime.sendMessage inline
      from harness-page realm (Option B per plan-checker BLOCKER 2;
      matches A5/A11/A12/A13/A26/A28/A29/A30/A31 pattern)

Verification (Task 1 acceptance criteria):
  - npx tsc --noEmit: exits 0
  - HEADLESS=1 tsx tests/uat/spike-a33-sw-persistence.ts: ran to completion
    (no Puppeteer throw); SPIKE RESULT line emitted with explicit
    videoSize=8505 bytes; SAVE_ARCHIVE ack received
  - grep -c 'dispatchSaveArchive' tests/uat/spike-a33-sw-persistence.ts: 0
  - grep -c "type: 'SAVE_ARCHIVE'" tests/uat/spike-a33-sw-persistence.ts: 1
  - Total spike wall-clock: 308.7s (~5min idle + ~8s orchestration)

References:
  - Plan 04-04 PLAN.md spike contract (lines 64-72)
  - 04-RESEARCH.md Q2 sub-question (b) — Chrome MV3 offscreen lifecycle
  - https://developer.chrome.com/docs/extensions/how-to/test/test-serviceworker-termination-with-puppeteer
  - Saved memory: feedback-gsd-ceremony-for-fixes.md (no inline architectural
    fixes; route through plan-fix ceremony)
This commit is contained in:
2026-05-21 18:44:45 +02:00
parent 303644f8cc
commit 3726eee39f
2 changed files with 244 additions and 6 deletions

View File

@@ -40,12 +40,45 @@ import { join, resolve as resolvePath } from 'node:path';
import JSZip from 'jszip';
import { EventType, IncrementalSource } from '@rrweb/types';
import type { Page } from 'puppeteer';
import type { Browser, Page } from 'puppeteer';
import type { AssertionRecord, CheckRecord } from './assertions';
import { assertArchiveShape, extractEntryToFile } from './zip';
import type { UserEvent } from '../../../src/shared/types';
/**
* Force-terminate the MV3 service worker via Puppeteer CDP. Required
* because Puppeteer's persistent CDP attach keeps SWs alive indefinitely;
* natural 30s idle eviction does NOT fire under test conditions per
* Chrome docs.
*
* Plan 04-04 — Wave 0 SPIKE persisting artifact + Wave 1 driveA33
* helper. The Puppeteer ≥ 22.1.0 `WebWorker.close()` surface is the
* canonical Chrome devrel pattern for simulating MV3 SW idle eviction
* under Puppeteer-driven UAT (CDP attach prevents natural eviction).
*
* References:
* - https://developer.chrome.com/docs/extensions/how-to/test/test-serviceworker-termination-with-puppeteer
* - https://developer.chrome.com/docs/extensions/develop/concepts/service-workers/lifecycle
* - https://developer.chrome.com/blog/eyeos-journey-to-testing-mv3-service%20worker-suspension
*
* @param browser - Puppeteer Browser handle from `launchHarnessBrowser`.
* @param extensionId - The runtime extension ID (from `handles.extensionId`).
*/
export async function stopServiceWorker(
browser: Browser,
extensionId: string,
): Promise<void> {
const host = `chrome-extension://${extensionId}`;
const target = await browser.waitForTarget(
(t) => t.type() === 'service_worker' && t.url().startsWith(host),
);
const worker = await target.worker();
if (worker !== null) {
await worker.close();
}
}
/**
* Extended assertion-record shape for A5/A12/A13 which return
* host-side-required binary payloads:
@@ -1384,17 +1417,21 @@ const A27_HOST_POLL_TIMEOUT_MS = 8_000;
const A27_HOST_POLL_INTERVAL_MS = 100;
/**
* Internal: pick the most-recently-modified .zip file in `downloadsDir`,
* or null if no .zip files exist. Used by driveA26 + driveA28 to chain
* off A25/A27 without re-dispatching SAVE. The "latest mtime wins"
* pattern works because the A25/A27 driver returns AFTER its zip lands
* Pick the most-recently-modified .zip file in `downloadsDir`, or null
* if no .zip files exist. Used by driveA26 + driveA28 to chain off
* A25/A27 without re-dispatching SAVE. The "latest mtime wins" pattern
* works because the A25/A27 driver returns AFTER its zip lands
* (await-pattern via the stable-size poll); no race with mid-write
* partial zips at the read instant.
*
* Plan 04-04 — exported so the Wave 0 spike script
* (`tests/uat/spike-a33-sw-persistence.ts`) can reuse the canonical
* mtime-sort archive-selection logic without duplicating it.
*
* @param downloadsDir - Absolute path to the per-run downloads dir.
* @returns Absolute path of the latest .zip, or null on empty dir.
*/
function findLatestZip(downloadsDir: string): string | null {
export function findLatestZip(downloadsDir: string): string | null {
const candidates = readdirSync(downloadsDir).filter(isZipFilename);
if (candidates.length === 0) {
return null;