feat(app): turn documents in the artifact gallery

This commit is contained in:
2026-10-22 03:24:40 +01:00
parent 4f684ba467
commit 3fa4657be7
18 changed files with 1365 additions and 168 deletions
@@ -0,0 +1,153 @@
import '../../../core/api_gateway.dart';
import 'artifact_capture.dart';
import 'artifact_kind.dart';
import 'artifact_record.dart';
import 'artifact_selection.dart';
/// The exact server-recorded name of a visit's session export. It is the one
/// turn document that only ever opens externally.
const String sessionExportDocumentName = 'session.html';
/// The turn-protocol documents a visit records, in the order the audit surface
/// presents them. These are the same files the stage-artifact filter treats as
/// turn-owned ([systemArtifactNames]), aliased so a rename cannot make the
/// gallery and the filter disagree.
const List<String> turnDocumentNames = systemArtifactNames;
/// The exact absence wording for a missing turn document. Session absence is
/// contextual and comes from [sessionDocumentAbsence].
const String noInputDocument = 'No input recorded.';
const String noOutputDocument = 'No output recorded.';
const String noMemoryDocument = 'No memory recorded for this visit.';
/// One presented turn document of a visit.
///
/// [record] is the server's exact recorded artifact, or null when the visit did
/// not record that document. A null record is never completed from a step field
/// or another document: the entry carries [absence] instead, so the gallery can
/// show why the document is missing.
class ArtifactDocument {
const ArtifactDocument({
required this.name,
required this.record,
required this.absence,
});
final String name;
final ArtifactRecord? record;
/// The exact wording shown when [record] is null; empty when recorded.
final String absence;
/// The session export is read-only evidence: it only ever opens externally.
bool get externalOnly => name == sessionExportDocumentName;
/// Whether selecting this document opens the existing in-app renderer. A
/// session export is excluded even if the server sniffed it as text.
bool get opensInViewer {
final recorded = record;
if (recorded == null || externalOnly) return false;
return isViewerArtifact(recorded.contentType, recorded.name);
}
}
String? _text(dynamic value) {
if (value == null) return null;
final text = '$value';
return text.isEmpty ? null : text;
}
/// The web client's contextual session wording, used only when no exact
/// `session.html` artifact was recorded. A recorded session id and a missing
/// export are different facts and are never collapsed into "not recorded".
String sessionDocumentAbsence(JsonMap step) {
final export = step['session_export'];
final evidence = export is Map ? Map<String, dynamic>.from(export) : null;
final evidenceSessionId = _text(evidence?['session_id']);
final sessionId = _text(step['session_id']) ?? evidenceSessionId ?? '';
if (sessionId.isNotEmpty) {
final recoveryValue = step['session_export_recovery'];
final recovery = recoveryValue is Map
? Map<String, dynamic>.from(recoveryValue)
: null;
final recoveryStatus = _text(recovery?['status']);
final evidenceStatus = _text(evidence?['status']);
final status = recoveryStatus == 'pending' || recoveryStatus == 'running'
? 'pending recovery'
: evidenceStatus == 'complete'
? 'not available in the artifact inventory'
: evidenceStatus ?? 'not yet available';
final diagnostic = _text(evidence?['diagnostic']);
return 'Session $sessionId is recorded. Its export is $status.'
'${diagnostic == null ? '' : ' $diagnostic'}';
}
final model = _text(step['model']) != null || _text(step['provider']) != null;
final status = _text(step['status']);
final outcome = _text(step['outcome']);
if (status == 'skipped' || outcome == 'skipped') {
return 'This visit was skipped; no harness session was started.';
}
if (status == 'canceled' || status == 'interrupted') {
return 'The stage stopped before a harness session was recorded.';
}
if (model) {
if (status == 'running') return 'The stage session is being created.';
if (status == 'failed') {
final diagnostic = _text(step['diagnostic']);
return diagnostic == null
? 'The stage did not create a harness session.'
: 'The stage did not create a harness session. $diagnostic';
}
return 'Runtime error: this model stage has no recorded session identity.';
}
return 'This visit has no harness session.';
}
String _absenceFor(String name, JsonMap step) => switch (name) {
'input.md' => noInputDocument,
'output.md' => noOutputDocument,
'memory.md' => noMemoryDocument,
_ => sessionDocumentAbsence(step),
};
/// Every turn document slot of one visit, recorded or absent.
///
/// A capture whose `artifacts` field is absent yields no slots at all: a missing
/// capture is not evidence that the visit recorded no document. A visit that
/// recorded none of the four turn documents yields no slots either, so a stage
/// outside the turn protocol is never presented as four missing documents. Only
/// exact recorded names are presented; nothing is synthesized from other step
/// fields.
List<ArtifactDocument> visitDocuments(JsonMap step) {
final capture = ArtifactCapture.fromStep(step);
if (!capture.isAvailable) return const <ArtifactDocument>[];
final recorded = presentedArtifacts(capture.artifacts);
ArtifactRecord? firstNamed(String name) {
for (final artifact in recorded) {
if (artifact.name == name) return artifact;
}
return null;
}
final records = <String, ArtifactRecord>{};
for (final name in turnDocumentNames) {
final record = firstNamed(name);
if (record != null) records[name] = record;
}
if (records.isEmpty) return const <ArtifactDocument>[];
final documents = <ArtifactDocument>[];
for (final name in turnDocumentNames) {
final record = records[name];
documents.add(
record == null
? ArtifactDocument(
name: name,
record: null,
absence: _absenceFor(name, step),
)
: ArtifactDocument(name: name, record: record, absence: ''),
);
}
return List<ArtifactDocument>.unmodifiable(documents);
}
@@ -1,14 +1,17 @@
import '../../../core/api_gateway.dart';
import '../../graph/domain/stage_visits.dart';
import 'artifact_document.dart';
import 'artifact_record.dart';
import 'artifact_selection.dart';
/// One gallery group: the stage-produced artifacts of a recorded stage visit.
/// One gallery group: the stage-produced artifacts of a recorded stage visit
/// plus that visit's turn documents, recorded or explicitly absent.
class ArtifactGroup {
const ArtifactGroup({
required this.stageKey,
required this.visit,
required this.artifacts,
this.documents = const <ArtifactDocument>[],
});
final String stageKey;
@@ -18,6 +21,15 @@ class ArtifactGroup {
final int visit;
final List<ArtifactRecord> artifacts;
/// The visit's turn-document slots. Empty when the capture was unavailable,
/// so an unknown capture is never presented as four missing documents.
final List<ArtifactDocument> documents;
/// The count the gallery header reports: stage artifacts plus the turn
/// documents the server actually recorded, never an absent slot.
int get recordedCount =>
artifacts.length + documents.where((doc) => doc.record != null).length;
}
int _sequenceOf(JsonMap step) {
@@ -86,6 +98,45 @@ List<ArtifactGroup> artifactGroups(List<JsonMap> steps) {
return List<ArtifactGroup>.unmodifiable(groups);
}
String _galleryKey(String stageKey, int visit) => '$stageKey\u0000$visit';
/// The gallery's per-visit groups: stage artifacts and turn documents in one
/// section per recorded visit.
///
/// Stage-only grouping is unchanged ([artifactGroups]); this function adds the
/// turn-document slots a visit recorded and keeps a visit whose only recorded
/// files are turn documents. A visit with neither stage artifacts nor a
/// recorded document, or whose capture is unavailable, produces no group.
List<ArtifactGroup> galleryGroups(List<JsonMap> steps) {
if (steps.isEmpty) return const <ArtifactGroup>[];
final stageGroups = <String, ArtifactGroup>{
for (final group in artifactGroups(steps))
_galleryKey(group.stageKey, group.visit): group,
};
final groups = <ArtifactGroup>[];
for (final entry in visitsByStage(steps).entries) {
for (var index = 0; index < entry.value.length; index += 1) {
final step = entry.value[index];
final visit = displayVisit(step, index);
final base = stageGroups[_galleryKey(entry.key, visit)];
final documents = visitDocuments(step);
final recordedDocuments = documents.where((doc) => doc.record != null);
if ((base?.artifacts.isEmpty ?? true) && recordedDocuments.isEmpty) {
continue;
}
groups.add(
ArtifactGroup(
stageKey: entry.key,
visit: visit,
artifacts: base?.artifacts ?? const <ArtifactRecord>[],
documents: documents,
),
);
}
}
return List<ArtifactGroup>.unmodifiable(groups);
}
/// One visit's stage artifacts, excluding shared public files that an earlier
/// visit already recorded. A repeated non-public name in a later visit stays
/// because it is a different recorded artifact, not the same shared file.
@@ -6,8 +6,8 @@ import '../../../core/api_gateway.dart';
const String legacySessionMarkdownArtifact = 'session.md';
/// The files the turn protocol itself writes for every visit. They are audit
/// evidence, not stage output, so the artifact gallery lists stage files first
/// and keeps these for an audit/debug surface.
/// evidence, not stage output: the artifact gallery presents them as the visit's
/// turn documents, and the stage-file list still excludes them.
const List<String> systemArtifactNames = <String>[
'input.md',
'memory.md',
@@ -16,14 +16,15 @@ String artifactsEmptyMessage(bool executing) => executing
/// The run summary's Artifacts section header.
///
/// The recorded count is exactly the number of tiles [artifactGroups] yields;
/// the title takes its singular form for one. A run whose capture is unavailable
/// says so instead of claiming that nothing was recorded. Partial capture is
/// flagged when any visit excluded or omitted files.
/// The recorded count is the number of recorded entries [galleryGroups] yields:
/// stage files plus the turn documents the server actually recorded, never an
/// absent slot. The title takes its singular form for one. A run whose capture is
/// unavailable says so instead of claiming that nothing was recorded. Partial
/// capture is flagged when any visit excluded or omitted files.
String artifactsHeader(List<JsonMap> steps, {required bool executing}) {
final recorded = artifactGroups(
final recorded = galleryGroups(
steps,
).fold<int>(0, (sum, group) => sum + group.artifacts.length);
).fold<int>(0, (sum, group) => sum + group.recordedCount);
if (recorded == 0) {
if (executing) return artifactsEmptyMessage(true);
final unavailable = steps.any(
@@ -44,8 +45,10 @@ String artifactsHeader(List<JsonMap> steps, {required bool executing}) {
///
/// A running visit says capture pending. A finished visit whose capture field was
/// absent says capture unavailable rather than "not recorded"; only an explicitly
/// empty capture says "not recorded for this visit". Recorded counts exclude the
/// turn-owned files and the retired session markdown, so the words match tiles.
/// empty capture says "not recorded for this visit". Without [count] the recorded
/// number excludes the turn-owned files and the retired session markdown, so the
/// stage-artifact words match the stage tiles; a caller that presents the turn
/// documents too passes its own recorded entry count.
String artifactsSummary(JsonMap step, {int? count}) {
final capture = ArtifactCapture.fromStep(step);
final recorded = count ?? stageArtifacts(step).length;
@@ -36,21 +36,27 @@ String artifactVisitIdentityOf(ArtifactGroup group, ArtifactRecord artifact) {
}
/// The artifacts the viewer can page through, in gallery order: group by group,
/// each group's images, videos and markdown/text documents in recorded order.
/// Other artifacts open externally and are not part of this list. Pure: it only
/// filters and orders records the gallery already holds.
/// each group's images, videos and markdown/text documents in recorded order,
/// then the visit's recorded turn documents that open in the renderer. A session
/// export is external-only and never enters the page list. Pure: it only filters
/// and orders records the gallery already holds.
List<PagedArtifact> pagedArtifacts(List<ArtifactGroup> groups) {
final pages = <PagedArtifact>[];
void add(ArtifactGroup group, ArtifactRecord artifact) {
pages.add(
PagedArtifact(group: group, artifact: artifact, pageIndex: pages.length),
);
}
for (final group in groups) {
for (final artifact in group.artifacts) {
if (!isViewerArtifact(artifact.contentType, artifact.name)) continue;
pages.add(
PagedArtifact(
group: group,
artifact: artifact,
pageIndex: pages.length,
),
);
add(group, artifact);
}
for (final document in group.documents) {
final artifact = document.record;
if (artifact == null || !document.opensInViewer) continue;
add(group, artifact);
}
}
return List<PagedArtifact>.unmodifiable(pages);