diff --git a/examples/livesay-pack/README.md b/examples/livesay-pack/README.md index db81d03..ad4e4dd 100644 --- a/examples/livesay-pack/README.md +++ b/examples/livesay-pack/README.md @@ -90,3 +90,12 @@ maximum). Thus graceMs controls both observation time and the overall marker wait bound. Answers beginning after the quiet observation window are outside this bounded sample contract. Use an explicit final SceneSpec wait/expect for your app's readiness signal rather than arbitrary long asynchronous work. + +Cross-document navigation during a scene or observation is deliberately +UNVERIFIED: the previous document's probe transcript cannot be recovered and +might contain an unfinished answer. This example does not join transcripts +across documents or claim that a completed destination answer proves the prior +answer ended. SPA state changes without document replacement are supported. + +A cross-document scene is also blocked on the visual side: no destination +loading screenshot is presented as a successful before/after comparison. diff --git a/examples/livesay-pack/run.mjs b/examples/livesay-pack/run.mjs index c821f9e..88bd190 100644 --- a/examples/livesay-pack/run.mjs +++ b/examples/livesay-pack/run.mjs @@ -40,6 +40,16 @@ async function targetInfo(path, commit) { return { dir, commit, contentHash: hash(JSON.stringify(entries)) }; } +/** Serializable browser predicate; absolute elapsed time survives same-tab navigation. */ +export function observationComplete({ids, baselineOrigin, grace, observationStart}) { + const snapshot = window.__livesayProbe?.snapshot(); + // A document replacement loses the prior probe transcript. Never call it verified. + if (performance.timeOrigin !== baselineOrigin) return 'document-changed'; + const excluded = ids; + const answers = snapshot?.answers.filter(a => !excluded.includes(a.id)) ?? []; + return answers.length > 0 && answers.every(a => a.outcome !== 'running' && Number.isFinite(a.endedAt)) && performance.timeOrigin + performance.now() - observationStart >= grace && snapshot.capturedAt - Math.max(...answers.map(a => a.endedAt)) >= grace; +} + export async function runPack(config, livesay) { const scenes = normalizeScenes(config.scenes).map(scene => ({ ...scene, route: normalizeRoute(applyRouteParams(scene.route, config.params).route) })); if (scenes.some(s => /\[[^\]]+\]|:[A-Za-z_]/.test(s.route))) throw new Error('Resolve scene route parameters in manifest params'); @@ -73,27 +83,31 @@ export async function runPack(config, livesay) { try { await livesay.installProbe(page); } catch { failures.push({ side: capture.side, scene: capture.scene, reason: 'probe-install-failed' }); } }, async beforeScene(page, capture) { - try { const snapshot = await livesay.readProbeSnapshot(page); baselines.set(key(capture), snapshot.answers.map(a => a.id)); } catch {} + const timeOrigin = await page.evaluate(() => performance.timeOrigin); + baselines.set(key(capture), { ids: null, timeOrigin }); + try { const snapshot = await livesay.readProbeSnapshot(page); baselines.set(key(capture), { ids: snapshot.answers.map(a => a.id), timeOrigin }); } catch {} }, async beforeScreenshot(page, capture) { if (!capture.scene) return; try { const oldIds = baselines.get(key(capture)); - if (!oldIds) throw new Error('scene start snapshot missing'); - const observationStart = (await livesay.readProbeSnapshot(page)).capturedAt; - await page.waitForFunction(({ids, grace, observationStart}) => { - const snapshot = window.__livesayProbe?.snapshot(); - const answers = snapshot?.answers.filter(a => !ids.includes(a.id)) ?? []; - // Every newly observed answer gets its own full grace. A later answer - // resets the boundary; bounded timeout leaves sustained streams unverified. - return answers.length > 0 && answers.every(a => a.outcome !== 'running' && Number.isFinite(a.endedAt)) && snapshot.capturedAt - observationStart >= grace && snapshot.capturedAt - Math.max(...answers.map(a => a.endedAt)) >= grace; - }, {ids: oldIds, grace: graceMs, observationStart}, { timeout: 1500 + graceMs * 3, polling: 50 }); + if (!oldIds || !oldIds.ids) throw new Error('scene start snapshot missing'); + const observationStart = await page.evaluate(() => performance.timeOrigin + performance.now()); + await page.waitForFunction(observationComplete, {ids: oldIds.ids, baselineOrigin: oldIds.timeOrigin, grace: graceMs, observationStart}, { timeout: 1500 + graceMs * 3, polling: 50 }); const snapshot = await livesay.readProbeSnapshot(page); - if (!snapshot.events.length || !snapshot.answers.filter(a => !oldIds.includes(a.id)).length || snapshot.answers.some(a => a.outcome === 'running')) throw new Error('empty recording'); + const currentOrigin = await page.evaluate(() => performance.timeOrigin); + if (currentOrigin !== oldIds.timeOrigin) throw new Error('cross-document evidence lost'); + const ids = oldIds.ids; + if (!snapshot.events.length || !snapshot.answers.filter(a => !ids.includes(a.id)).length || snapshot.answers.some(a => a.outcome === 'running')) throw new Error('empty recording'); const report = livesay.audit(snapshot, { profile: 'nvda-like' }); const item = { binding: { ...binding }, scene: capture.scene, side: capture.side, report }; captures.set(key(capture), item); - } catch { failures.push({ side: capture.side, scene: capture.scene, reason: 'announcement-unverified' }); } + } catch { + failures.push({ side: capture.side, scene: capture.scene, reason: 'announcement-unverified' }); + const baseline = baselines.get(key(capture)); + const origin = await page.evaluate(() => performance.timeOrigin).catch(() => null); + if (baseline && origin !== baseline.timeOrigin) throw new Error('Cross-document scene is blocked; no destination screenshot comparison'); + } }, }; // Do not inherit an unrelated ambient .proofshot.json. diff --git a/examples/livesay-pack/test.mjs b/examples/livesay-pack/test.mjs index 6dc57b7..b59f7aa 100644 --- a/examples/livesay-pack/test.mjs +++ b/examples/livesay-pack/test.mjs @@ -5,7 +5,7 @@ import { tmpdir } from 'node:os'; import { join, resolve } from 'node:path'; import { pathToFileURL } from 'node:url'; import { execFileSync } from 'node:child_process'; -import { runPack, bindEvidence } from './run.mjs'; +import { runPack, bindEvidence, observationComplete } from './run.mjs'; const livesay = await import(pathToFileURL(resolve(process.argv[2])).href); const config = JSON.parse(await readFile(process.argv[3], 'utf8')); test('binding refuses scene, step/viewport, and either commit drift', () => { @@ -74,3 +74,35 @@ test('final action asynchronous answer is observed after earlier answer grace el c.scenes[0].steps=[{action:'click',selector:'#open'},{action:'wait',selector:'#last'},{action:'click',selector:'#last'}]; const pack=await runPack(c,livesay);assert.equal(pack.rows[0].status,'verified');assert.match(pack.rows[0].transcripts.head.join(' '),/Last completed/); }); + +test('observation predicate rejects old completed answers until final-action quiet time',()=>{ + const old=globalThis.window; + try { + globalThis.window={__livesayProbe:{snapshot:()=>({capturedAt:9000,answers:[{id:'x',endedAt:100,outcome:'done'}]})}}; + const now=performance.timeOrigin+performance.now(); + const args={ids:[],baselineOrigin:performance.timeOrigin,grace:1200,observationStart:now}; + assert.equal(observationComplete(args),false); + assert.equal(observationComplete({...args,observationStart:now-1300}),true); + } finally {globalThis.window=old;} +}); +test('same-tab document navigation is unverified because the old transcript is lost',async()=>{ + const c=await fixture(`if(location.pathname==='/second'){const p=window.__livesayProbe;p.mark('start',{id:'x'});document.querySelector('#result').textContent='Navigation completed.';p.mark('done',{id:'x'});}document.querySelector('#open').onclick=()=>{const p=window.__livesayProbe;p.mark('start',{id:'x'});p.mark('done',{id:'x'});setTimeout(()=>location.href='/second',700);};`); + const pack=await runPack(c,livesay);assert.equal(pack.rows[0].status,'unverified');assert.equal(pack.rows[0].announcement,null); +}); + +test('different document origin cannot validate even complete destination answers',()=>{ + const old=globalThis.window; + try { + globalThis.window={__livesayProbe:{snapshot:()=>({capturedAt:9000,answers:[{id:'x',endedAt:100,outcome:'done'}]})}}; + const now=performance.timeOrigin+performance.now(); + assert.equal(observationComplete({ids:['x'],baselineOrigin:performance.timeOrigin-100,grace:1200,observationStart:now-5000}),'document-changed'); + } finally {globalThis.window=old;} +}); + +test('missing initial probe still blocks a cross-document visual comparison',async()=>{ + const c=await fixture(`if(location.pathname==='/second'){document.querySelector('#result').id='arrived';document.querySelector('#arrived').textContent='Arrived';}document.querySelector('#open').onclick=()=>{setTimeout(()=>location.href='/second',700);};`); + c.scenes[0].steps.push({action:'wait',selector:'#arrived'}); + c.scenes[0].expect='#arrived'; + const broken={...livesay,readProbeSnapshot:async()=>{throw new Error('no probe');}}; + const pack=await runPack(c,broken);assert.equal(pack.rows[0].status,'unverified');assert.equal(pack.rows[0].visual.status,'error');assert.equal(pack.rows[0].visual.images.after,undefined);assert.match(pack.rows[0].visual.error,/Cross-document scene is blocked/); +});