Files
pipelines/app/lib/features/artifacts/domain/artifact_document.dart
T

154 lines
6.0 KiB
Dart

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);
}