fix(audit): expose verified visit exports and export-only recovery

This commit is contained in:
2026-10-20 21:20:06 +01:00
parent f87e1ed488
commit ca78b24f47
14 changed files with 359 additions and 59 deletions
+53
View File
@@ -0,0 +1,53 @@
// Isolated audit regression: session identity, verified repair selection and
// live modal refresh. Provider/session data is fixture-only; no production DB.
import assert from "node:assert/strict";
import { createServer } from "node:http";
import { readFile, mkdir } from "node:fs/promises";
import { resolve, extname } from "node:path";
import { launchChrome } from "./browser.mjs";
const root=resolve(import.meta.dirname,"..");
const out=resolve(process.argv[2]??"captures-session-audit");await mkdir(out,{recursive:true});
const html=`<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
${["design/tokens/tokens","base","shell","lists","run-detail","chat","dialogs"].map(n=>`<link rel="stylesheet" href="/${n}.css">`).join("")}
<div class="app-root"><main></main></div><script type="module">
import {createModalController} from '/src/modal.js';
import {createChatView} from '/src/views/chat.js';
import {createDialogsView} from '/src/views/dialogs.js';
import {createRunDetailView} from '/src/views/runDetail.js';
const step={sequence:1,stage:'Survey',status:'completed',outcome:'success',model:'fixture',session_id:'ses_exact',session_export:{session_id:'ses_exact',status:'failed',diagnostic:'native readback failed'},artifacts:[]};
const run={id:19,pipeline_id:25,version:6,status:'succeeded',name:'Audit fixture',graph:{stages:[{key:'Survey',type:'model',edges:{success:'#success'}}]},steps:[step]};
const state={url:'ws://'+location.host+'/connect',runDetail:run,runStageSections:new Set(),runStagePane:{stageKey:'Survey',visitIndex:0}};
window.repairRequests=[];
const rt={state,modal:createModalController(),views:{},repo:{repairSessionExport:async(id,sequence)=>{repairRequests.push({id,sequence});window.recordRepair();return state.runDetail;}},renderContent:()=>{}};
rt.views.chat=createChatView(rt);rt.views.dialogs=createDialogsView(rt);rt.views.runDetail=createRunDetailView(rt);
document.querySelector('main').append(rt.views.runDetail.renderStagePane(run));rt.modal.reconcile();
window.recordRepair=()=>{const updated={...step,session_export_repairs:[{session_id:'ses_exact',status:'complete',messages:14,html:'session-repair-1-fixed.html',html_bytes:100}],artifacts:[{name:'session-repair-1-fixed.html',url:'/artifacts/19/1/session-repair-1-fixed.html',content_type:'text/html',size_bytes:100}]};state.runDetail={...run,steps:[updated]};rt.views.dialogs.refreshStageAudit();};
</script>`;
const server=createServer(async(req,res)=>{try{
const pathname=new URL(req.url,"http://fixture").pathname;
if(pathname==="/"){res.setHeader('Content-Type','text/html');res.end(html);return;}
if(pathname.startsWith('/artifacts/')){res.setHeader('Content-Type','text/html');res.setHeader('Content-Security-Policy',"sandbox; default-src 'none'");res.end('<h1>Recorded session ses_exact</h1>');return;}
const file=resolve(root,`.${pathname}`);if(!file.startsWith(root+'/'))throw Error('outside fixture');
res.setHeader('Content-Type',extname(file)==='.css'?'text/css':'text/javascript');res.end(await readFile(file));
}catch{res.writeHead(404);res.end();}});
await new Promise(r=>server.listen(0,'127.0.0.1',r));
let browser;
try{
browser=await launchChrome({width:390,height:844});
const page=await browser.openPage(`http://127.0.0.1:${server.address().port}/`);
await page.waitForSelector('.audit-open');await page.click('.audit-open');await page.click('[data-audit-tab=session]');
assert.match(await page.evaluate("document.querySelector('.audit-body').textContent"),/ses_exact is recorded/);
assert.doesNotMatch(await page.evaluate("document.querySelector('.audit-body').textContent"),/Session not recorded|exported by model stages/);
await page.screenshot(resolve(out,'01-recorded-identity-export-failed.png'));
await page.click('.session-export-retry');await page.waitForSelector('.audit-session-frame');
assert.deepEqual(await page.evaluate('repairRequests'),[{id:19,sequence:1}]);
assert.match(await page.evaluate("document.querySelector('.audit-session-frame').src"),/session-repair-1-fixed.html$/);
assert.match(await page.evaluate("document.querySelector('.audit-body').textContent"),/Recorded session: ses_exact/);
assert.doesNotMatch(await page.evaluate("document.querySelector('.audit-body').textContent"),/native readback failed/);
assert.equal(await page.evaluate("document.querySelector('.audit-session-frame').getAttribute('sandbox')"),'');
const same=await page.evaluate("window.savedFrame=document.querySelector('.audit-session-frame');true");
await page.evaluate('recordRepair()');assert.equal(await page.evaluate("savedFrame===document.querySelector('.audit-session-frame')"),same);
await page.screenshot(resolve(out,'02-complete-repair-refreshes-open-audit.png'));
console.log('PASS: recorded identity remains visible; a verified repair refreshes the open Session tab without reopening or resetting an unchanged iframe.');
}finally{await browser?.close();await new Promise(r=>server.close(r));}
+51
View File
@@ -0,0 +1,51 @@
import test from "node:test";
import assert from "node:assert/strict";
import { effectiveSessionExport, sessionExportArtifact, sessionCaptureState } from "../src/artifacts.js";
const file = (name) => ({name,size_bytes:100,content_type:"text/html",url:`/artifacts/19/1/${name}`});
const complete = (html, id="ses_stage") => ({session_id:id,backend:"v2",status:"complete",messages:14,html,html_bytes:100});
test("a missing export never hides a recorded session identity", () => {
const state=sessionCaptureState({session_id:"ses_stage",model:"fixture",status:"completed",session_export:{session_id:"ses_stage",status:"failed",diagnostic:"readback failed"}});
assert.equal(state.sessionId,"ses_stage");assert.equal(state.artifact,null);
assert.match(state.message,/ses_stage is recorded/);assert.match(state.message,/failed/);
assert.doesNotMatch(state.message,/not recorded|exported by model stages/);
});
test("the audit viewer selects a verified complete repair without changing original evidence", () => {
const original={session_id:"ses_stage",status:"failed",diagnostic:"original capture failed"};
const repaired=complete("session-repair-1-good.html");
const step={session_id:"ses_stage",session_export:original,session_export_repairs:[repaired,{session_id:"ses_stage",status:"failed"}],artifacts:[file(repaired.html)]};
assert.equal(effectiveSessionExport(step),repaired);
assert.equal(sessionExportArtifact(step).name,repaired.html);
assert.equal(sessionCaptureState(step).artifact.url,"/artifacts/19/1/session-repair-1-good.html");
assert.equal(step.session_export,original);assert.equal(original.status,"failed");
});
test("a wrong-session repair or an unlisted file cannot masquerade as the export", () => {
const original={session_id:"ses_stage",status:"failed"};
const wrong=complete("session-repair-wrong.html","ses_other");
const missing=complete("session-repair-missing.html");
const step={session_id:"ses_stage",session_export:original,session_export_repairs:[wrong,missing],artifacts:[file(wrong.html)]};
assert.equal(effectiveSessionExport({...step,session_export_repairs:[wrong]}),original);
assert.equal(sessionExportArtifact(step),null);
});
test("a partial repair does not replace a complete original export", () => {
const original=complete("session.html");
const partial={...complete("session-repair-partial.html"),status:"partial",truncated:true};
assert.equal(effectiveSessionExport({session_id:"ses_stage",session_export:original,session_export_repairs:[partial],artifacts:[file(original.html),file(partial.html)]}),original);
});
test("an original export naming another session is not selected", () => {
const original=complete("session.html","ses_wrong");
assert.equal(sessionExportArtifact({session_id:"ses_stage",session_export:original,artifacts:[file("session.html")]}),null);
});
test("pre-admission failure, starting session and export recovery have distinct states", () => {
assert.match(sessionCaptureState({model:"fixture",status:"running"}).message,/being created/);
assert.match(sessionCaptureState({model:"fixture",status:"failed",diagnostic:"provider unreachable"}).message,/did not create.*provider unreachable/);
assert.match(sessionCaptureState({model:"fixture",status:"completed"}).message,/Runtime error/);
assert.match(sessionCaptureState({model:"fixture",status:"completed",outcome:"skipped"}).message,/was skipped/);
assert.match(sessionCaptureState({session_id:"ses_stage",session_export_recovery:{status:"pending"}}).message,/pending recovery/);
});
+16
View File
@@ -0,0 +1,16 @@
import test from "node:test";
import assert from "node:assert/strict";
import { PipelinesRepository } from "../src/repository.js";
test("web export retry names the exact recorded visit and sends no execution request", async () => {
const sent=[];const detail={id:19,status:"succeeded"};
const repo=new PipelinesRepository({call:async frame=>{sent.push(frame);return {run_detail:detail};}});
assert.equal(await repo.repairSessionExport(19,4),detail);
assert.deepEqual(sent,[{type:"repair_session_export",run_id:19,export_sequence:4}]);
});
test("web export retry refuses an unknown run or visit before any request", async () => {
let calls=0;const repo=new PipelinesRepository({call:async()=>{calls++;}});
for(const [id,sequence] of [[0,1],[19,0],[19,-1],[19,1.5]]) await assert.rejects(repo.repairSessionExport(id,sequence));
assert.equal(calls,0);
});