From ca78b24f475684353417ea90e358b59cb755a168 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Tue, 20 Oct 2026 21:20:06 +0100 Subject: [PATCH] fix(audit): expose verified visit exports and export-only recovery --- internal/protocol/runevents.go | 3 + internal/server/artifacts.go | 69 ++++++++++------- internal/server/artifacts_test.go | 28 +++++++ internal/server/session_export_repair.go | 46 ++++++++---- internal/server/session_export_repair_test.go | 6 +- web/src/app.js | 3 + web/src/artifacts.js | 39 +++++++++- web/src/protocol.js | 1 + web/src/repository.js | 8 ++ web/src/views/dialogs.js | 21 +++++- web/src/views/runDetail.js | 74 ++++++++++++++++--- web/test/session-audit-browser.mjs | 53 +++++++++++++ web/test/sessionCapture.test.mjs | 51 +++++++++++++ web/test/sessionExportRepository.test.mjs | 16 ++++ 14 files changed, 359 insertions(+), 59 deletions(-) create mode 100644 web/test/session-audit-browser.mjs create mode 100644 web/test/sessionCapture.test.mjs create mode 100644 web/test/sessionExportRepository.test.mjs diff --git a/internal/protocol/runevents.go b/internal/protocol/runevents.go index 32921b8b..48c5d858 100644 --- a/internal/protocol/runevents.go +++ b/internal/protocol/runevents.go @@ -25,6 +25,9 @@ const ( // from the frame instead of re-reading the whole list. They repeat on every // frame (pipeline identity never changes mid-run). type RunEventFrame struct { + // SessionExportUpdated requests a detail refresh even when the stage/run + // outcome is unchanged. It never fabricates a second stage completion. + SessionExportUpdated bool `json:"session_export_updated,omitempty"` // GraphEvidence seeds a complete compact snapshot; GraphVisits preserves // changed visits even when Step is replaced during frame coalescing. GraphEvidence *RunGraphEvidence `json:"graph_evidence,omitempty"` diff --git a/internal/server/artifacts.go b/internal/server/artifacts.go index 041b3bd2..88f152aa 100644 --- a/internal/server/artifacts.go +++ b/internal/server/artifacts.go @@ -54,8 +54,46 @@ func (r *runner) scanArtifacts(runID int64, sequence int, directory string, publ return nil, 0, 0 } files := []protocol.StageArtifact{} + seen := map[string]bool{} skipped, truncated := 0, 0 used := int64(0) + add := func(full, name string, info fs.FileInfo) bool { + if seen[full] { + return true + } + if len(files) >= maxArtifactFiles { + skipped++ + return false + } + if info.Size() > maxArtifactFileBytes { + skipped++ + return true + } + if used+info.Size() > maxArtifactTotalBytes { + truncated++ + return false + } + contentType := mime.TypeByExtension(filepath.Ext(name)) + if contentType == "" { + contentType = sniffArtifact(root, full) + if contentType == "" { + skipped++ + return true + } + } + used += info.Size() + seen[full] = true + files = append(files, protocol.StageArtifact{Name: name, SizeBytes: info.Size(), ContentType: contentType, URL: artifactURL(runID, sequence, name)}) + return true + } + // Mandatory runtime evidence must not disappear behind hundreds of private + // captures/public deliverables in the bounded inventory. + for _, name := range []string{"input.md", "memory.md", "output.md", "session.html"} { + full := path.Join(rel, name) + if info, err := root.Lstat(full); err == nil && info.Mode().IsRegular() && info.Mode()&fs.ModeSymlink == 0 && info.Size() <= maxArtifactFileBytes { + add(full, name, info) + } + } scan := func(relative, prefix string) { _ = fs.WalkDir(root.FS(), relative, func(name string, entry fs.DirEntry, walkErr error) error { if walkErr != nil { @@ -65,6 +103,9 @@ func (r *runner) scanArtifacts(runID int64, sequence int, directory string, publ if name == relative || entry.IsDir() { return nil } + if seen[name] { + return nil + } if entry.Type()&fs.ModeSymlink != 0 || !entry.Type().IsRegular() { skipped++ return nil @@ -74,36 +115,12 @@ func (r *runner) scanArtifacts(runID int64, sequence int, directory string, publ skipped++ return nil } - if len(files) >= maxArtifactFiles { - skipped++ - return fs.SkipAll - } - if info.Size() > maxArtifactFileBytes { - skipped++ - return nil - } - if used+info.Size() > maxArtifactTotalBytes { - truncated++ - return fs.SkipAll - } artifactName := strings.TrimPrefix(name, relative+"/") artifactName = prefix + artifactName full := path.Join(relative, strings.TrimPrefix(artifactName, prefix)) - contentType := mime.TypeByExtension(filepath.Ext(artifactName)) - if contentType == "" { - contentType = sniffArtifact(root, full) - if contentType == "" { - skipped++ - return nil - } + if !add(full, artifactName, info) { + return fs.SkipAll } - used += info.Size() - files = append(files, protocol.StageArtifact{ - Name: artifactName, - SizeBytes: info.Size(), - ContentType: contentType, - URL: artifactURL(runID, sequence, artifactName), - }) return nil }) } diff --git a/internal/server/artifacts_test.go b/internal/server/artifacts_test.go index 55903dc5..da5e8bb9 100644 --- a/internal/server/artifacts_test.go +++ b/internal/server/artifacts_test.go @@ -29,6 +29,34 @@ func artifactNamed(files []protocol.StageArtifact, name string) (protocol.StageA return protocol.StageArtifact{}, false } +func TestSessionEvidenceIsInventoriedBeforeLargeCaptureDirectories(t *testing.T) { + root := t.TempDir() + dir := filepath.Join(root, "1", "stages", "01-test") + if err := os.MkdirAll(filepath.Join(dir, "private"), 0700); err != nil { + t.Fatal(err) + } + for i := 0; i < maxArtifactFiles+10; i++ { + if err := os.WriteFile(filepath.Join(dir, "private", fmt.Sprintf("capture-%03d.txt", i)), []byte("fixture"), 0600); err != nil { + t.Fatal(err) + } + } + for _, name := range []string{"input.md", "memory.md", "output.md", "session.html"} { + if err := os.WriteFile(filepath.Join(dir, name), []byte("runtime evidence"), 0600); err != nil { + t.Fatal(err) + } + } + r := &runner{artifactsRoot: root} + files, skipped, _ := r.scanArtifacts(1, 1, dir) + if len(files) > maxArtifactFiles || skipped == 0 { + t.Fatalf("inventory bound lost: %d %d", len(files), skipped) + } + for _, name := range []string{"input.md", "memory.md", "output.md", "session.html"} { + if _, ok := artifactNamed(files, name); !ok { + t.Fatalf("mandatory runtime file %s hidden by user captures", name) + } + } +} + func TestArtifactsScanRecordsSizesTypesAndSkips(t *testing.T) { root := t.TempDir() stepDir := filepath.Join(root, "4", "2") diff --git a/internal/server/session_export_repair.go b/internal/server/session_export_repair.go index 8f30f91b..9c5265b7 100644 --- a/internal/server/session_export_repair.go +++ b/internal/server/session_export_repair.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "strconv" + "strings" "time" "git.a-wan.net/alan/pipelines/internal/pipeline" @@ -37,7 +38,11 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc if err != nil { return nil, err } - if d.Status == "running" || d.Status == "queued" || d.Status == "waiting" { + pendingGate, gateErr := r.store.PendingSessionExportGate(ctx, id, sequence) + if gateErr != nil { + return nil, gateErr + } + if d.Status == "running" || d.Status == "queued" || (d.Status == "waiting" && !pendingGate) { return nil, fmt.Errorf("session export repair requires a settled run") } var step *protocol.RunStep @@ -80,22 +85,28 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc if err := r.store.RecordSessionExportRepair(recordCtx, id, sequence, evidence); err != nil { return nil, err } - return r.store.GetRun(recordCtx, id) + return r.sessionExportUpdatedDetail(recordCtx, id) } - if owner.Backend != "v1" && owner.Backend != "v2" { + if owner.Backend != "v1" && owner.Backend != "v2" && owner.Backend != "pi" { return recordFailure("native export unavailable for original harness") } - if len(step.Artifacts) >= maxArtifactFiles { - return recordFailure("artifact inventory full; repair not attempted") - } root, err := os.OpenRoot(r.artifactsRoot) if err != nil { return recordFailure(err.Error()) } defer root.Close() run := strconv.FormatInt(id, 10) - public := filepath.Join(run, "public") - for _, dir := range []string{run, public} { + if step.Stage == "" || step.Stage == "." || step.Stage == ".." || strings.ContainsAny(step.Stage, "/\\") { + return recordFailure("invalid recorded stage key") + } + visit := filepath.Join(run, "stages", fmt.Sprintf("%02d-%s", sequence, step.Stage)) + legacy := filepath.Join(run, strconv.Itoa(sequence)) + if !containedDirectory(root, filepath.ToSlash(visit)) && containedDirectory(root, filepath.ToSlash(legacy)) { + visit = legacy + } + // Audit records belong to this visit, never shared public/: the next stage + // must not receive the previous agent's private tool/transcript contents. + for _, dir := range []string{run, filepath.Dir(visit), visit} { info, err := root.Lstat(dir) if os.IsNotExist(err) { if err := root.Mkdir(dir, 0700); err != nil { @@ -123,7 +134,7 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc if export == nil { export = stage.ExportSession } - result, err := export(exportCtx, stage.SessionExportRequest{SessionID: owner.ID, Backend: owner.Backend, Directory: owner.Directory, ArtifactsDir: filepath.Join(r.artifactsRoot, public), FileName: name, RunID: id, Sequence: sequence, Stage: step.Stage, Provider: step.Provider, Model: step.Model}) + result, err := export(exportCtx, stage.SessionExportRequest{SessionID: owner.ID, Backend: owner.Backend, Reference: owner.Reference, Directory: owner.Directory, ArtifactsDir: filepath.Join(r.artifactsRoot, visit), FileName: name, RunID: id, Sequence: sequence, Stage: step.Stage, Provider: step.Provider, Model: step.Model}) persistCtx, persistCancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second) defer persistCancel() evidence.Attempts = result.Attempts @@ -131,6 +142,7 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc return recordFailure(err.Error()) } evidence.Status = result.Status + evidence.Source = result.Source evidence.Messages = result.Messages evidence.Truncated = result.Truncated evidence.Diagnostic = result.Diagnostic @@ -138,11 +150,11 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc if result.HTML != name { return recordFailure("repair exporter returned an unexpected filename") } - info, err := root.Lstat(filepath.Join(public, name)) + info, err := root.Lstat(filepath.Join(visit, name)) if err != nil { return recordFailure(err.Error()) } - if !info.Mode().IsRegular() || info.Size() <= 0 || info.Size() > stage.SessionExportFileLimit || result.Messages <= 0 { + if !info.Mode().IsRegular() || info.Size() <= 0 || info.Size() > stage.SessionExportFileLimit || result.Messages <= 0 || info.Size() != result.HTMLBytes { return recordFailure("repair export missing, empty, non-normal or oversized") } var used int64 @@ -152,7 +164,7 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc if used+info.Size() > maxArtifactTotalBytes { return recordFailure("repaired export exceeds artifact inventory byte bounds") } - evidence.HTML = "public/" + name + evidence.HTML = name evidence.HTMLBytes = info.Size() if err := r.store.RecordDeliverable(persistCtx, id, sequence, protocol.StageArtifact{Name: evidence.HTML, SizeBytes: info.Size(), ContentType: "text/html; charset=utf-8", URL: artifactURL(id, sequence, evidence.HTML)}); err != nil { return recordFailure(err.Error()) @@ -162,5 +174,13 @@ func (r *runner) repairSessionExportOwned(ctx context.Context, id int64, sequenc if err := r.store.RecordSessionExportRepair(persistCtx, id, sequence, evidence); err != nil { return nil, err } - return r.store.GetRun(persistCtx, id) + return r.sessionExportUpdatedDetail(persistCtx, id) +} + +func (r *runner) sessionExportUpdatedDetail(ctx context.Context, id int64) (*protocol.RunDetail, error) { + detail, err := r.store.GetRun(ctx, id) + if err == nil { + r.emitRun(protocol.RunEventFrame{RunID: id, PipelineID: detail.PipelineID, Name: detail.Name, Title: detail.Title, Version: detail.Version, Workspace: detail.Workspace, Status: detail.Status, CurrentStage: detail.CurrentStage, CompletedSteps: detail.CompletedSteps, SessionExportUpdated: true}, true) + } + return detail, err } diff --git a/internal/server/session_export_repair_test.go b/internal/server/session_export_repair_test.go index fca0fccd..21a2c992 100644 --- a/internal/server/session_export_repair_test.go +++ b/internal/server/session_export_repair_test.go @@ -68,7 +68,7 @@ func TestRuntimeSessionExportRepairPreservesOriginalAndUsesRecordedOwner(t *test t.Fatalf("repair changed run outcome: %+v", repaired) } latest := repaired.Steps[0].SessionExportRepairs[1] - if latest.Status != "complete" || latest.Messages != 2 || latest.SessionID != owner.ID || latest.Backend != owner.Backend || !strings.HasPrefix(latest.HTML, "public/session-repair-1-") { + if latest.Status != "complete" || latest.Messages != 2 || latest.SessionID != owner.ID || latest.Backend != owner.Backend || !strings.HasPrefix(latest.HTML, "session-repair-1-") { t.Fatalf("repair not recorded: %+v", latest) } after, _ := json.Marshal(repaired.Steps[0].SessionExport) @@ -79,7 +79,7 @@ func TestRuntimeSessionExportRepairPreservesOriginalAndUsesRecordedOwner(t *test if string(body) != "historical evidence" { t.Fatal("historical export overwritten") } - body, err = os.ReadFile(filepath.Join(h.runner.artifactsRoot, "1", latest.HTML)) + body, err = os.ReadFile(filepath.Join(h.runner.artifactsRoot, "1", "stages", "01-"+repaired.Steps[0].Stage, latest.HTML)) if err != nil || len(body) == 0 { t.Fatalf("repair file missing: %v", err) } @@ -141,7 +141,7 @@ func TestCLIRepairSessionExportUsesRuntimeProtocol(t *testing.T) { SessionID string `json:"session_id"` HTML string `json:"html"` } - if err := json.Unmarshal(out.Bytes(), &evidence); err != nil || evidence.Status != "complete" || evidence.SessionID != "ses_run" || !strings.HasPrefix(evidence.HTML, "public/session-repair-1-") { + if err := json.Unmarshal(out.Bytes(), &evidence); err != nil || evidence.Status != "complete" || evidence.SessionID != "ses_run" || !strings.HasPrefix(evidence.HTML, "session-repair-1-") { t.Fatalf("wrong repair reply: %+v %v %s", evidence, err, out.String()) } } diff --git a/web/src/app.js b/web/src/app.js index 2ef8c051..f5350861 100644 --- a/web/src/app.js +++ b/web/src/app.js @@ -1846,6 +1846,7 @@ function refreshVisibleListOnForeground() { /** Render whatever a run-state change touched, without re-reading anything. */ function renderRunState() { if (state.view === "runs" || state.view === "pipelines") renderContent(); + views.dialogs.refreshStageAudit?.(); refreshAttentionBell(); } @@ -2101,6 +2102,7 @@ function applyRunEvent(frame) { } const detail = state.runDetail; if (detail && Number(detail.id) === runId) { + if (frame.session_export_updated === true) scheduleRunDetailSync(runId); runWatch.detailDeltaCount += 1; const folded = foldRunEventDetail(detail, frame); const detailChanged = folded !== detail; @@ -2381,6 +2383,7 @@ const runtime = { onReconnectClicked, selectPipeline, selectRun, + refreshRunDetail: scheduleRunDetailSync, setError, loadCurrentView, }; diff --git a/web/src/artifacts.js b/web/src/artifacts.js index 2b394175..fcc521d3 100644 --- a/web/src/artifacts.js +++ b/web/src/artifacts.js @@ -468,12 +468,49 @@ export function artifactViewerPositionLabel(index, count, contentType) { * that recorded no export, whose export name is blank, or whose file is missing * from a truncated listing yields null. Mirrors SessionExport.kt sessionExportFile. */ +export function effectiveSessionExport(step) { + const recorded = step?.session_export ?? null; + const id = String(step?.session_id || recorded?.session_id || ""); + const original = recorded?.session_id && id && recorded.session_id !== id ? null : recorded; + const repairs = [...(step?.session_export_repairs ?? [])].reverse() + .filter((entry) => id !== "" && entry?.session_id === id); + const hasFile = (entry) => Boolean(entry?.html) && (step?.artifacts ?? []).some((file) => file?.name === entry.html && file.size_bytes > 0); + // A failed later attempt must not hide an earlier, verified full repair. + const isComplete = (entry) => entry?.status === "complete" && entry.messages > 0 && + entry.html_bytes > 0 && !entry.truncated && !entry.diagnostic && hasFile(entry); + return repairs.find(isComplete) ?? (isComplete(original) ? original : null) ?? repairs.find(hasFile) ?? (hasFile(original) ? original : null) ?? repairs[0] ?? original ?? null; +} + export function sessionExportArtifact(step) { - const html = step?.session_export?.html; + const html = effectiveSessionExport(step)?.html; if (typeof html !== "string" || html.trim() === "") return null; return (step?.artifacts ?? []).find((artifact) => artifact?.name === html) ?? null; } +/** A recorded ID and an exported document are different facts. Never call a + * recorded session "not recorded" just because its export is missing. */ +export function sessionCaptureState(step) { + const evidence = effectiveSessionExport(step); + const sessionId = String(step?.session_id || evidence?.session_id || ""); + const artifact = sessionExportArtifact(step); + if (artifact) return { sessionId, evidence, artifact, message: "" }; + if (sessionId) { + const recovery = step?.session_export_recovery; + const status = recovery?.status === "pending" || recovery?.status === "running" + ? "pending recovery" : evidence?.status === "complete" ? "not available in the artifact inventory" : evidence?.status || "not yet available"; + return { sessionId, evidence, artifact: null, + message: `Session ${sessionId} is recorded. Its export is ${status}.${evidence?.diagnostic ? ` ${evidence.diagnostic}` : ""}` }; + } + const model = Boolean(step?.model || step?.provider); + const skipped=step?.status==="skipped" || step?.outcome==="skipped"; + const stopped=["canceled","interrupted"].includes(step?.status); + return { sessionId, evidence, artifact: null, message: skipped ? "This visit was skipped; no harness session was started." : stopped ? "The stage stopped before a harness session was recorded." : model + ? step?.status === "running" ? "The stage session is being created." + : step?.status === "failed" ? `The stage did not create a harness session.${step?.diagnostic ? ` ${step.diagnostic}` : ""}` + : "Runtime error: this model stage has no recorded session identity." + : "This visit has no harness session." }; +} + /** * The absolute url a gallery artifact can be opened at, or null when it cannot * be resolved against the configured server (blank or invalid). The one place a diff --git a/web/src/protocol.js b/web/src/protocol.js index 34e8f794..4264dfb9 100644 --- a/web/src/protocol.js +++ b/web/src/protocol.js @@ -25,6 +25,7 @@ export const Protocol = Object.freeze({ LIST_RUNS: "list_runs", RUNS: "runs", GET_RUN: "get_run", + REPAIR_SESSION_EXPORT: "repair_session_export", RUN: "run", // Live run state (internal/protocol/runevents.go, Message.kt; R10). `subscribe` diff --git a/web/src/repository.js b/web/src/repository.js index e154e091..b6634054 100644 --- a/web/src/repository.js +++ b/web/src/repository.js @@ -115,6 +115,14 @@ export class PipelinesRepository { return response.run_detail; } + /** Export the exact recorded visit again; never invokes its model or tools. */ + async repairSessionExport(id, sequence) { + if (!Number.isSafeInteger(id) || id<1 || !Number.isSafeInteger(sequence) || sequence<1) throw new ProtocolError("An exact run and visit are required."); + const response=await this.#client.call({type:Protocol.REPAIR_SESSION_EXPORT,run_id:id,export_sequence:sequence}); + if (!response.run_detail) throw new ProtocolError("The server returned no run detail."); + return response.run_detail; + } + /** * Asks the server to push this connection the live run state it displays * (R10, also the source of L5's notifications). Send-only, mirroring diff --git a/web/src/views/dialogs.js b/web/src/views/dialogs.js index da4d2292..411d01db 100644 --- a/web/src/views/dialogs.js +++ b/web/src/views/dialogs.js @@ -59,6 +59,9 @@ export function createDialogsView(rt) { let changeViewerOverlay = null; let changeViewerIndex = null; +let refreshAudit = null; + +function refreshStageAudit() { refreshAudit?.(); } /** Close the diff viewer; the controller returns focus to the viewed row. */ function closeChangeViewer() { @@ -840,10 +843,10 @@ function openStageDefinition(detail, stageKey, subtitle = "") { * on the right. It is the only place the system files are shown, so the artifact * surfaces stay focused on what the stage actually produced. */ -function openStageAudit({ title = "Audit", subtitle = "", tabs = [], trigger = null } = {}) { +function openStageAudit({ title = "Audit", subtitle = "", tabs = [], trigger = null, revision = null } = {}) { const available = (tabs ?? []).filter((tab) => typeof tab?.body === "function"); if (!available.length) return; - const close = () => modal.close("stage-audit"); + const close = () => { refreshAudit = null; modal.close("stage-audit"); }; const overlay = h("div", { class: "audit-viewer", role: "dialog", @@ -890,6 +893,12 @@ function openStageAudit({ title = "Audit", subtitle = "", tabs = [], trigger = n overlay.append(panel); let active = available[0].key; + let renderedRevision = null; + const renderBody = () => { + const current = available.find((tab) => tab.key === active) ?? available[0]; + body.replaceChildren(current.body()); + renderedRevision = typeof revision === "function" ? revision(active) : null; + }; const renderTabs = () => { nav.replaceChildren( ...available.map((tab) => { @@ -923,8 +932,7 @@ function openStageAudit({ title = "Audit", subtitle = "", tabs = [], trigger = n }); }), ); - const current = available.find((tab) => tab.key === active) ?? available[0]; - body.replaceChildren(current.body()); + renderBody(); }; renderTabs(); modal.open("stage-audit", overlay, { @@ -932,9 +940,14 @@ function openStageAudit({ title = "Audit", subtitle = "", tabs = [], trigger = n trigger, fallbackSelectors: ["#detailTitle", "#pane-main"], }); + refreshAudit = () => { + if (!overlay.isConnected || typeof revision !== "function") return; + if (revision(active) !== renderedRevision) renderBody(); + }; } return { + refreshStageAudit, openStageDefinition, closeChangeViewer, openChangeViewer, diff --git a/web/src/views/runDetail.js b/web/src/views/runDetail.js index 57a22d60..f299a209 100644 --- a/web/src/views/runDetail.js +++ b/web/src/views/runDetail.js @@ -50,6 +50,8 @@ import { presentedArtifacts, resolvedArtifactUrl, sessionExportArtifact, + effectiveSessionExport, + sessionCaptureState, stageArtifacts, systemArtifacts, visitArtifacts, @@ -111,6 +113,31 @@ export function createRunDetailView(rt) { const openArtifactViewer = (...args) => rt.views.dialogs.openArtifactViewer(...args); const showStageAudit = (...args) => rt.views.dialogs.openStageAudit(...args); const selectPipeline = (...args) => rt.selectPipeline(...args); + const sessionRepairs = new Set(); + +function sessionExportRetry(step) { + const run=state.runDetail; + if (!run?.id || !step?.sequence || !(step.session_id || step.session_export?.session_id)) return null; + const key=`${run.id}:${step.sequence}`; + const running=step.agent_active || ["running","queued"].includes(run.status) || + (run.status==="waiting" && run.export_pending_sequence!==step.sequence); + const button=h("button",{class:"ghost session-export-retry",type:"button",disabled:running || sessionRepairs.has(key), + text:sessionRepairs.has(key)?"Exporting…":"Retry export only", + title:"Read the exact recorded session again. Does not rerun the stage or its tools."}); + button.addEventListener("click",async()=>{ + if (sessionRepairs.has(key)) return; + sessionRepairs.add(key);button.disabled=true;button.textContent="Exporting…"; + try { + const detail=await repo.repairSessionExport(run.id,step.sequence); + if (state.runDetail?.id===run.id) { + if (rt.refreshRunDetail) rt.refreshRunDetail(run.id); + else {state.runDetail=detail;renderContent();} + } + } catch(error) {state.actionError=describeError(error);renderContent();} + finally {sessionRepairs.delete(key);button.disabled=Boolean(running);button.textContent="Retry export only";rt.views.dialogs.refreshStageAudit?.();} + }); + return button; +} /** * The run detail: a tab strip (Summary / Graph / Input / Output / Timeline) over @@ -213,18 +240,30 @@ function latestVisitIndex(d, key) { /** Open the audit dialog for one visit: its turn files, session and definition. */ function openAuditFor(d, step, stageKey, visitIndex) { if (!step) return; + const currentStep = () => state.runDetail?.id === d.id + ? state.runDetail.steps?.find((entry) => entry.sequence === step.sequence) ?? step : step; showStageAudit({ title: `${nodeLabel(stageKey)} audit`, subtitle: `Visit ${visitIndex + 1} · ${runLabelFor(d)}`, tabs: [ - { key: "input", label: "Input", body: () => auditArtifact(step, "input.md", EMPTY_INPUT_MESSAGE) }, - { key: "output", label: "Output", body: () => auditArtifact(step, "output.md", EMPTY_OUTPUT_MESSAGE) }, - { key: "memory", label: "Memory", body: () => auditArtifact(step, "memory.md", "No memory recorded for this visit.") }, - { key: "session", label: "Session", body: () => auditSession(step) }, + { key: "input", label: "Input", body: () => auditArtifact(currentStep(), "input.md", EMPTY_INPUT_MESSAGE) }, + { key: "output", label: "Output", body: () => auditArtifact(currentStep(), "output.md", EMPTY_OUTPUT_MESSAGE) }, + { key: "memory", label: "Memory", body: () => auditArtifact(currentStep(), "memory.md", "No memory recorded for this visit.") }, + { key: "session", label: "Session", body: () => auditSession(currentStep()) }, { key: "definition", label: "Definition", body: () => definitionDocument( runDefinition(d), stageKey, (text) => rt.views.chat.renderMarkdown(text), ) }, ], + revision: (tab) => { + const current = currentStep(); + if (tab === "session") { + const capture = sessionCaptureState(current); + const retrying = capture.artifact && capture.evidence?.status === "complete" ? false : sessionRepairs.has(`${d.id}:${step.sequence}`); + return JSON.stringify([capture.sessionId,capture.artifact,capture.evidence?.status,capture.evidence?.messages,capture.evidence?.truncated,capture.evidence?.diagnostic,capture.message,retrying]); + } + if (["input","output","memory"].includes(tab)) return JSON.stringify(systemArtifacts(current).find((entry)=>entry.name===`${tab}.md`) ?? null); + return `${d.pipeline_id}:${d.version}:${stageKey}`; + }, }); } @@ -303,15 +342,25 @@ function auditArtifact(step, name, emptyMessage) { /** The exported session, rendered in place, with the raw file one click away. */ function auditSession(step) { - const artifact = systemArtifacts(step).find((entry) => entry.name === "session.html"); + const capture = sessionCaptureState(step); + const legacy = !step?.session_export && !(step?.session_export_repairs?.length); + const artifact = capture.artifact ?? (legacy ? systemArtifacts(step).find((entry) => entry.name === "session.html") : null); if (!artifact) { - return h("div", { - class: "muted", - text: "Session not recorded for this visit. Sessions are exported by model stages.", + const body = h("div", { + class: "audit-session-state muted", + "data-session-id": capture.sessionId, }); + body.append(h("p",{text:capture.message})); + const retry=sessionExportRetry(step);if(retry)body.append(retry); + return body; } const resolved = resolvedArtifactUrl(artifact, state.url); const wrap = h("div", { class: "audit-session" }); + if (capture.sessionId) wrap.append(h("p", {class:"audit-session-state muted",text:`Recorded session: ${capture.sessionId}`})); + if (capture.evidence && (capture.evidence.status && capture.evidence.status!=="complete" || capture.evidence.truncated)) { + wrap.append(h("p",{class:"audit-session-state muted",text:`This export is ${capture.evidence.status || "partial"}; full session evidence is not available.`})); + const retry=sessionExportRetry(step);if(retry)wrap.append(retry); + } if (!resolved) { wrap.append(h("div", { class: "muted", text: ARTIFACT_SERVER_HINT })); return wrap; @@ -1273,23 +1322,24 @@ function artifactAction(kind, label, disabled, onclick) { function renderSessionExport(step) { const file = sessionExportArtifact(step); const wrap = h("div", { class: "session-export" }); + const evidence = effectiveSessionExport(step); + const complete = evidence?.status === "complete" && evidence.messages>0 && evidence.html_bytes>0 && file && !evidence.truncated && !evidence.diagnostic; const recovery = step?.session_export_recovery; - if (recovery) wrap.append(h("div", { class: "muted", text: `Automatic export recovery: ${recovery.status} · ${recovery.attempts}/3 attempts. ${recovery.diagnostic || ""}` })); + if (recovery && !complete) wrap.append(h("div", { class: "muted", text: `Automatic export recovery: ${recovery.status} · ${recovery.attempts}/3 attempts. ${recovery.diagnostic || ""}` })); for (const repair of step?.session_export_repairs || []) { const row = h("div", { class: "session-export-repair" }); row.append(h("span", { text: `Repair: ${repair.status || "partial"} · ${repair.messages || 0} messages. ${repair.diagnostic || ""}` })); - const repaired = sessionExportArtifact({ ...step, session_export: repair }); + const repaired = sessionExportArtifact({ ...step, session_export: repair, session_export_repairs: [] }); const repairedHref = repaired && resolvedArtifactUrl(repaired, state.url); if (repairedHref) row.append(h("a", { ...artifactBrowserLinkProps(repairedHref), text: "Open repaired export" })); wrap.append(row); } - const evidence = step?.session_export; const incomplete = evidence && (evidence.status && evidence.status !== "complete" || !evidence.messages || evidence.truncated || evidence.diagnostic); if (incomplete) { wrap.append(h("span", { class: "muted", text: `Session recorded, but export ${evidence.status || (!evidence.messages ? "empty" : "partial")}. Full session evidence was not captured.${evidence.diagnostic ? ` ${evidence.diagnostic}` : ""}` })); } if (!file) { - if (!incomplete) wrap.append(h("span", { class: "muted", text: step?.session_id ? "Session recorded, but export failed or was not captured." : "No session was exported for this visit." })); + wrap.append(h("span", { class: "muted", text: sessionCaptureState(step).message })); return wrap; } const href = resolvedArtifactUrl(file, state.url); diff --git a/web/test/session-audit-browser.mjs b/web/test/session-audit-browser.mjs new file mode 100644 index 00000000..dc10974b --- /dev/null +++ b/web/test/session-audit-browser.mjs @@ -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=` +${["design/tokens/tokens","base","shell","lists","run-detail","chat","dialogs"].map(n=>``).join("")} +
`; +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('

Recorded session ses_exact

');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));} diff --git a/web/test/sessionCapture.test.mjs b/web/test/sessionCapture.test.mjs new file mode 100644 index 00000000..a4198454 --- /dev/null +++ b/web/test/sessionCapture.test.mjs @@ -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/); +}); diff --git a/web/test/sessionExportRepository.test.mjs b/web/test/sessionExportRepository.test.mjs new file mode 100644 index 00000000..745346a2 --- /dev/null +++ b/web/test/sessionExportRepository.test.mjs @@ -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); +});