merge(app): land run-session-audit micro-slice

This commit is contained in:
2026-10-22 09:50:21 +01:00
9 changed files with 1075 additions and 191 deletions
@@ -0,0 +1,352 @@
import '../../../core/api_gateway.dart';
import '../../artifacts/domain/artifact_record.dart';
import '../../artifacts/domain/artifact_selection.dart';
import 'run_detail_visit_recorded.dart';
/// The export state the pane reports. A status this client does not know is
/// [unknown] and is shown verbatim; it is never rounded to complete.
enum VisitSessionExportState {
complete,
partial,
missing,
skipped,
repair,
unknown,
}
/// The one state line for a visit's recorded export, with the server's own
/// diagnostic as [reason] when it recorded one.
class VisitSessionExportFact {
const VisitSessionExportFact({
required this.state,
required this.word,
this.reason,
});
final VisitSessionExportState state;
/// The state word shown to the operator. An unknown status keeps the
/// server's exact word here instead of being mapped to a known state.
final String word;
/// The server-recorded diagnostic, verbatim, or null when there was none.
final String? reason;
}
/// One recorded repair attempt and the read-only labels the pane shows for it.
class VisitSessionRepair {
const VisitSessionRepair({
required this.status,
required this.messages,
this.diagnostic,
this.option,
});
final String status;
final int messages;
final String? diagnostic;
/// The web pane's open label, shown read-only, or null when this repair is
/// the document the main open action already points at.
final String? option;
/// The web audit pane's repair label, without its `Repair: ` prefix.
String get label =>
'$status · $messages messages.'
'${diagnostic == null ? '' : ' $diagnostic'}';
}
/// A recorded session export document and the evidence the server recorded for
/// it.
class VisitSessionExport {
const VisitSessionExport({
required this.artifact,
this.status,
this.diagnostic,
this.messages = 0,
this.htmlBytes = 0,
this.truncated = false,
this.repaired = false,
});
final ArtifactRecord artifact;
final String? status;
final String? diagnostic;
final int messages;
final int htmlBytes;
final bool truncated;
/// True when the document is one of the visit's recorded repair attempts.
final bool repaired;
/// Completeness needs the server's complete status plus positive message and
/// byte counts, an untruncated document and no diagnostic. An absent or
/// partial record is never promoted to complete.
bool get isComplete => _evidenceComplete(
status: status,
messages: messages,
htmlBytes: htmlBytes,
truncated: truncated,
diagnostic: diagnostic,
);
/// The evidence sentence beside a present but incomplete export, or null
/// when the export is complete or no evidence was recorded.
String? get statusNote {
if (isComplete) return null;
if (status == null && messages <= 0 && !truncated) return null;
final word = status == null || status == 'complete' ? 'partial' : status;
return 'This export is $word; full session evidence is not available.';
}
}
/// The recorded harness session identity, from the step or its export evidence.
String? detailVisitSessionIdentity(JsonMap step) {
final direct = recordedText(step['session_id']);
if (direct != null) return direct;
final evidence = _firstEvidence(step);
return evidence == null ? null : recordedText(evidence['session_id']);
}
/// The visit's recorded export document, or null when no recorded export name
/// is present in the inventory.
///
/// The step's own `session_export` is preferred; a repair document is used
/// when that is the file the inventory carries. A visit with no export evidence
/// falls back to its `session.html`, exactly as the web audit pane does.
VisitSessionExport? detailVisitSessionExport(JsonMap step) {
final evidence = _exportEvidence(step);
for (final entry in evidence) {
final artifact = recordedArtifact(
step,
recordedText(entry.evidence['html']),
);
if (artifact == null) continue;
return VisitSessionExport(
artifact: artifact,
status: recordedText(entry.evidence['status']),
diagnostic: recordedText(entry.evidence['diagnostic']),
messages: recordedCount(entry.evidence['messages']),
htmlBytes: recordedCount(entry.evidence['html_bytes']),
truncated: entry.evidence['truncated'] == true,
repaired: entry.repaired,
);
}
if (evidence.isNotEmpty) return null;
for (final artifact in systemArtifacts(step)) {
if (artifact.name == 'session.html') {
return VisitSessionExport(artifact: artifact);
}
}
return null;
}
/// The recorded sentence for a visit whose export evidence names no file, in
/// the web audit pane's words: the recovery status while it is pending or
/// running, the evidence status otherwise, and the recorded diagnostic.
String missingExportMessage(JsonMap step, String sessionId) {
final evidence = _firstEvidence(step);
final recovery = step['session_export_recovery'];
final recoveryStatus = recovery is Map
? recordedText(recovery['status'])
: null;
final evidenceStatus = evidence == null
? null
: recordedText(evidence['status']);
final String status;
if (recoveryStatus == 'pending' || recoveryStatus == 'running') {
status = 'pending recovery';
} else if (evidenceStatus == 'complete') {
status = 'not available in the artifact inventory';
} else {
status = evidenceStatus ?? 'not yet available';
}
final diagnostic = evidence == null
? null
: recordedText(evidence['diagnostic']);
final suffix = diagnostic == null ? '' : ' $diagnostic';
return 'Session $sessionId is recorded. Its export is $status.$suffix';
}
/// Every recorded repair attempt, oldest first, read-only. The open label is
/// kept only for an attempt the main open action does not already point at.
List<VisitSessionRepair> visitSessionRepairs(
JsonMap step,
VisitSessionExport? export,
) {
final recorded = step['session_export_repairs'];
if (recorded is! List) return const [];
final selectedName = export?.artifact.name;
final repairs = <VisitSessionRepair>[];
for (final entry in recorded) {
if (entry is! Map) continue;
final artifact = recordedArtifact(step, recordedText(entry['html']));
final selected = artifact != null && artifact.name == selectedName;
repairs.add(
VisitSessionRepair(
status: recordedText(entry['status']) ?? 'partial',
messages: recordedCount(entry['messages']),
diagnostic: recordedText(entry['diagnostic']),
option: artifact == null || selected ? null : 'Open repaired export',
),
);
}
return repairs;
}
/// The Export row for a present export document.
VisitSessionExportFact exportFactFor(VisitSessionExport export) {
if (export.repaired) {
return VisitSessionExportFact(
state: VisitSessionExportState.repair,
word: 'repair',
reason: export.diagnostic,
);
}
final state = _exportState(export.status, evidence: export.isComplete);
if (state == VisitSessionExportState.complete) {
return const VisitSessionExportFact(
state: VisitSessionExportState.complete,
word: 'complete',
);
}
if (state == VisitSessionExportState.partial) return _partialFact(export);
return VisitSessionExportFact(
state: VisitSessionExportState.unknown,
word: export.status ?? 'unknown',
reason: export.diagnostic,
);
}
/// The Export row for a visit whose evidence names no file: missing, unless the
/// evidence carries a status this client does not know, which is kept verbatim.
VisitSessionExportFact missingExportFact(JsonMap step) {
final evidence = _firstEvidence(step);
final status = evidence == null ? null : recordedText(evidence['status']);
final diagnostic = evidence == null
? null
: recordedText(evidence['diagnostic']);
if (status != null &&
_exportState(status, evidence: false) ==
VisitSessionExportState.unknown) {
return VisitSessionExportFact(
state: VisitSessionExportState.unknown,
word: status,
reason: diagnostic,
);
}
return VisitSessionExportFact(
state: VisitSessionExportState.missing,
word: 'missing',
reason: diagnostic,
);
}
VisitSessionExportFact _partialFact(VisitSessionExport export) =>
VisitSessionExportFact(
state: VisitSessionExportState.partial,
word: 'partial',
reason: export.diagnostic,
);
const Set<String> _partialStatuses = <String>{
'partial',
'empty',
'failed',
'truncated',
'incomplete',
'unavailable',
};
/// The export state a recorded status means, with [evidence] reporting whether
/// the server's complete evidence was actually recorded for the document.
///
/// A `complete` word without verified evidence is partial, never complete. A
/// status this client does not know, or none at all, is unknown; the caller
/// keeps the server's own word in the row.
VisitSessionExportState _exportState(String? status, {required bool evidence}) {
if (status == 'complete') {
return evidence
? VisitSessionExportState.complete
: VisitSessionExportState.partial;
}
if (status != null && _partialStatuses.contains(status)) {
return VisitSessionExportState.partial;
}
return VisitSessionExportState.unknown;
}
/// The one completeness rule for recorded export evidence: the server's
/// complete status, positive message and byte counts, an untruncated document
/// and no diagnostic. An incomplete record is never promoted to complete.
bool _evidenceComplete({
required String? status,
required int messages,
required int htmlBytes,
required bool truncated,
required String? diagnostic,
}) =>
status == 'complete' &&
messages > 0 &&
htmlBytes > 0 &&
!truncated &&
diagnostic == null;
/// The recorded export evidence, most current first: a complete repair, then a
/// complete original, then every remaining candidate. A failed later attempt
/// never hides an earlier verified document; an older original never hides the
/// repair that replaced it.
List<_RecordedExport> _exportEvidence(JsonMap step) {
final repairs = <Map>[];
final recordedRepairs = step['session_export_repairs'];
if (recordedRepairs is List) {
for (final entry in recordedRepairs.reversed) {
if (entry is Map) repairs.add(entry);
}
}
final original = step['session_export'];
final ordered = <_RecordedExport>[];
void add(Map entry, bool repaired) {
for (final candidate in ordered) {
if (identical(candidate.evidence, entry)) return;
}
ordered.add(_RecordedExport(entry, repaired));
}
for (final repair in repairs) {
if (_isComplete(repair)) add(repair, true);
}
if (original is Map && _isComplete(original)) add(original, false);
for (final repair in repairs) {
add(repair, true);
}
if (original is Map) add(original, false);
return ordered;
}
class _RecordedExport {
const _RecordedExport(this.evidence, this.repaired);
final Map evidence;
final bool repaired;
}
Map? _firstEvidence(JsonMap step) {
final original = step['session_export'];
if (original is Map) return original;
final repairs = step['session_export_repairs'];
if (repairs is List) {
for (final entry in repairs.reversed) {
if (entry is Map) return entry;
}
}
return null;
}
bool _isComplete(Map evidence) => _evidenceComplete(
status: recordedText(evidence['status']),
messages: recordedCount(evidence['messages']),
htmlBytes: recordedCount(evidence['html_bytes']),
truncated: evidence['truncated'] == true,
diagnostic: recordedText(evidence['diagnostic']),
);
@@ -0,0 +1,31 @@
import '../../../core/api_gateway.dart';
import '../../artifacts/domain/artifact_record.dart';
import '../../artifacts/domain/artifact_selection.dart';
/// Reading one visit's recorded fields, shared by the session pointer and its
/// export evidence so both interpret the same reply the same way.
/// The trimmed, non-empty text of a recorded value, or null when the reply
/// carried none. A blank string is absence, never a value.
String? recordedText(dynamic value) {
if (value == null) return null;
final text = '$value'.trim();
return text.isEmpty ? null : text;
}
/// A recorded count from a number or numeric string; 0 when the reply carried
/// none or something unparsable.
int recordedCount(dynamic value) {
if (value is num) return value.toInt();
return int.tryParse('${value ?? ''}'.trim()) ?? 0;
}
/// The inventory artifact with exactly [name], or null when [name] is absent or
/// the visit's recorded inventory carries no such file.
ArtifactRecord? recordedArtifact(JsonMap step, String? name) {
if (name == null) return null;
for (final artifact in artifactsOf(step)) {
if (artifact.name == name) return artifact;
}
return null;
}
@@ -8,12 +8,15 @@ import '../../artifacts/domain/artifact_url.dart';
import '../../artifacts/ui/artifact_external_open.dart';
import 'run_detail_visit_session_state.dart';
/// The visit's recorded session pointer and, when the visit exported one, a
/// read-only external open of that document.
/// The visit's recorded session pointer, its recorded export state and, when
/// the visit exported one, a read-only external open of that document.
///
/// The state comes from [detailVisitSession], so a legacy `session.html`, a
/// repair, a still-running session and a failed one each report what the server
/// recorded. The open goes through the landed safe-link helper: the recorded url
/// recorded. Complete, partial, missing, skipped, repair and unrecognised
/// states each keep the server's own word and diagnostic; recorded repair
/// attempts and their open-option labels are shown read-only and make no
/// request. The open goes through the landed safe-link helper: the recorded url
/// is resolved by [resolveArtifactUrl] and passed to [openArtifactExternally]
/// without being fetched, rewritten or previewed. A refusal is reported.
class RunVisitSession extends StatefulWidget {
@@ -53,17 +56,34 @@ class _RunVisitSessionState extends State<RunVisitSession> {
? null
: resolveArtifactUrl(widget.configuredEndpoint, export.artifact.url);
final note = export?.statusNote;
final fact = session.exportFact;
final showReason =
export != null &&
fact?.reason != null &&
fact!.state != VisitSessionExportState.repair;
final children = <Widget>[
if (session.sessionId != null)
_SessionFact(label: 'Session', value: session.sessionId!),
if (export != null) ...[
if (note != null) Text(note),
if (resolved == null)
const Text(visitSessionEndpointMissing)
else
_openButton(export.artifact, resolved),
] else
Text(session.message),
if (fact != null)
_SessionFact(
key: const Key('run-visit-session-export'),
label: 'Export',
value: fact.word,
),
if (showReason)
_SessionFact(
key: const Key('run-visit-session-export-reason'),
label: 'Reason',
value: fact.reason!,
),
if (export != null && note != null) Text(note),
..._repairFacts(session.repairs),
if (export == null)
Text(session.message)
else if (resolved == null)
const Text(visitSessionEndpointMissing)
else
_openButton(export.artifact, resolved),
if (_status != null)
Padding(
padding: const EdgeInsets.only(top: 8),
@@ -110,6 +130,33 @@ class _RunVisitSessionState extends State<RunVisitSession> {
);
}
/// Recorded repair attempts, in the pane's own fact rows. Nothing here is a
/// control: the repair request belongs to its own action and is not wired.
List<Widget> _repairFacts(List<VisitSessionRepair> repairs) {
final facts = <Widget>[];
for (var index = 0; index < repairs.length; index += 1) {
final repair = repairs[index];
facts.add(
_SessionFact(
key: ValueKey('run-visit-session-repair-$index'),
label: 'Repair',
value: repair.label,
),
);
final option = repair.option;
if (option != null) {
facts.add(
_SessionFact(
key: ValueKey('run-visit-session-repair-option-$index'),
label: 'Repair option',
value: option,
),
);
}
}
return facts;
}
List<Widget> _spaced(List<Widget> children) => [
for (var index = 0; index < children.length; index += 1) ...[
if (index > 0) const SizedBox(height: 8),
@@ -119,7 +166,7 @@ class _RunVisitSessionState extends State<RunVisitSession> {
}
class _SessionFact extends StatelessWidget {
const _SessionFact({required this.label, required this.value});
const _SessionFact({super.key, required this.label, required this.value});
final String label;
final String value;
@@ -1,11 +1,12 @@
import '../../../core/api_gateway.dart';
import '../../artifacts/domain/artifact_record.dart';
import '../../artifacts/domain/artifact_selection.dart';
import 'run_detail_visit_export.dart';
import 'run_detail_visit_recorded.dart';
export 'run_detail_visit_export.dart';
/// The exact wording for a visit that never started a harness session, in the
/// web audit pane's order: a skipped visit, a stopped one, a model visit whose
/// session is still being created, a failed one, and a model visit with no
/// identity at all. Only a non-model visit with nothing recorded gets the
/// web audit pane's order: skipped, stopped, being created, failed, and a model
/// visit with no identity. Only a non-model visit with nothing recorded gets the
/// generic sentence.
const String visitSessionSkipped =
'This visit was skipped; no harness session was started.';
@@ -21,210 +22,88 @@ const String visitSessionNone = 'This visit has no harness session.';
const String visitSessionEndpointMissing =
'Set the server URL in Settings to open the session.';
/// A recorded session export document and the evidence the server recorded for
/// it.
class VisitSessionExport {
const VisitSessionExport({
required this.artifact,
this.status,
this.diagnostic,
this.truncated = false,
});
final ArtifactRecord artifact;
final String? status;
final String? diagnostic;
final bool truncated;
/// A partial or failed export is still the recorded document; it stays
/// openable and the pane says what the evidence reports.
bool get isComplete => status == 'complete' && !truncated;
/// The evidence sentence shown beside a present but incomplete export, or
/// null when the export is complete or the visit recorded no evidence.
String? get statusNote {
if (isComplete) return null;
if (status == null && !truncated) return null;
return 'This export is ${status ?? 'partial'}; full session evidence is '
'not available.';
}
}
/// A visit's recorded session: its identity, an openable export document and,
/// when neither exists, the one sentence describing the recorded state.
/// A visit's recorded session: identity, openable export, recorded export
/// state, recorded repairs and, without a document, the recorded sentence.
class VisitSessionState {
const VisitSessionState({this.sessionId, this.export, this.message = ''});
const VisitSessionState({
this.sessionId,
this.export,
this.message = '',
this.exportFact,
this.repairs = const [],
});
final String? sessionId;
final VisitSessionExport? export;
/// Empty exactly when [export] is present.
final String message;
/// Null when the visit recorded no session at all.
final VisitSessionExportFact? exportFact;
/// Every recorded repair attempt, oldest first, read-only.
final List<VisitSessionRepair> repairs;
}
/// Resolves the recorded session state without inventing a session.
///
/// The export is looked up through the recorded export evidence (including a
/// repair that replaced it) and then, for a historical visit that recorded no
/// evidence at all, through the `session.html` its inventory carries. When the
/// evidence names no file, its own status and diagnostic are reported instead
/// of claiming the inventory is empty.
/// repair that replaced it) and then, for a historical visit with no evidence,
/// through its `session.html`. When the evidence names no file, its own status
/// and diagnostic are reported instead of claiming the inventory is empty.
VisitSessionState detailVisitSession(JsonMap step) {
final sessionId = detailVisitSessionIdentity(step);
final export = detailVisitSessionExport(step);
final repairs = visitSessionRepairs(step, export);
if (export != null) {
return VisitSessionState(sessionId: sessionId, export: export);
return VisitSessionState(
sessionId: sessionId,
export: export,
exportFact: exportFactFor(export),
repairs: repairs,
);
}
if (sessionId != null) {
return VisitSessionState(
sessionId: sessionId,
message: _missingExportMessage(step, sessionId),
message: missingExportMessage(step, sessionId),
exportFact: missingExportFact(step),
repairs: repairs,
);
}
return VisitSessionState(message: _noSessionMessage(step));
return VisitSessionState(
message: _noSessionMessage(step),
exportFact: _isSkipped(step)
? const VisitSessionExportFact(
state: VisitSessionExportState.skipped,
word: 'skipped',
)
: null,
repairs: repairs,
);
}
/// The recorded harness session identity, from the step or its export evidence.
String? detailVisitSessionIdentity(JsonMap step) {
final direct = _text(step['session_id']);
if (direct != null) return direct;
final evidence = _firstEvidence(step);
return evidence == null ? null : _text(evidence['session_id']);
}
/// The visit's recorded export document, or null when no recorded export name
/// is present in the inventory.
///
/// The step's own `session_export` is preferred; a repair document is used
/// when that is the file the inventory actually carries. A visit with no export
/// evidence at all falls back to its `session.html` system artifact, exactly as
/// the web audit pane does for runs recorded before the evidence field existed.
VisitSessionExport? detailVisitSessionExport(JsonMap step) {
final evidence = _exportEvidence(step);
for (final entry in evidence) {
final artifact = _artifactNamed(step, _text(entry['html']));
if (artifact == null) continue;
return VisitSessionExport(
artifact: artifact,
status: _text(entry['status']),
diagnostic: _text(entry['diagnostic']),
truncated: entry['truncated'] == true,
);
}
if (evidence.isNotEmpty) return null;
for (final artifact in systemArtifacts(step)) {
if (artifact.name == 'session.html') {
return VisitSessionExport(artifact: artifact);
}
}
return null;
}
String _missingExportMessage(JsonMap step, String sessionId) {
final evidence = _firstEvidence(step);
final recovery = step['session_export_recovery'];
final recoveryStatus = recovery is Map ? _text(recovery['status']) : null;
final evidenceStatus = evidence == null ? null : _text(evidence['status']);
final String status;
if (recoveryStatus == 'pending' || recoveryStatus == 'running') {
status = 'pending recovery';
} else if (evidenceStatus == 'complete') {
status = 'not available in the artifact inventory';
} else {
status = evidenceStatus ?? 'not yet available';
}
final diagnostic = evidence == null ? null : _text(evidence['diagnostic']);
final suffix = diagnostic == null ? '' : ' $diagnostic';
return 'Session $sessionId is recorded. Its export is $status.$suffix';
}
bool _isSkipped(JsonMap step) =>
recordedText(step['status']) == 'skipped' ||
recordedText(step['outcome']) == 'skipped';
String _noSessionMessage(JsonMap step) {
final status = _text(step['status']);
if (status == 'skipped' || _text(step['outcome']) == 'skipped') {
return visitSessionSkipped;
}
final status = recordedText(step['status']);
if (_isSkipped(step)) return visitSessionSkipped;
if (status == 'canceled' || status == 'interrupted') {
return visitSessionStopped;
}
final model = _text(step['provider']) != null || _text(step['model']) != null;
final model =
recordedText(step['provider']) != null ||
recordedText(step['model']) != null;
if (!model) return visitSessionNone;
if (status == 'running') return visitSessionStarting;
if (status == 'failed') {
final diagnostic = _text(step['diagnostic']);
final diagnostic = recordedText(step['diagnostic']);
return diagnostic == null
? visitSessionFailed
: '$visitSessionFailed $diagnostic';
}
return visitSessionNoIdentity;
}
/// The recorded export evidence, most current first: a complete repair, then a
/// complete original, then every remaining candidate. A failed later attempt
/// never hides an earlier verified document; an older original never hides the
/// repair that replaced it.
List<Map> _exportEvidence(JsonMap step) {
final repairs = <Map>[];
final recordedRepairs = step['session_export_repairs'];
if (recordedRepairs is List) {
for (final entry in recordedRepairs.reversed) {
if (entry is Map) repairs.add(entry);
}
}
final original = step['session_export'];
final ordered = <Map>[];
void add(Map entry) {
for (final candidate in ordered) {
if (identical(candidate, entry)) return;
}
ordered.add(entry);
}
for (final repair in repairs) {
if (_isComplete(repair)) add(repair);
}
if (original is Map && _isComplete(original)) add(original);
for (final repair in repairs) {
add(repair);
}
if (original is Map) add(original);
return ordered;
}
Map? _firstEvidence(JsonMap step) {
final original = step['session_export'];
if (original is Map) return original;
final repairs = step['session_export_repairs'];
if (repairs is List) {
for (final entry in repairs.reversed) {
if (entry is Map) return entry;
}
}
return null;
}
bool _isComplete(Map evidence) =>
_text(evidence['status']) == 'complete' &&
_positive(evidence['messages']) &&
_positive(evidence['html_bytes']) &&
evidence['truncated'] != true &&
_text(evidence['diagnostic']) == null;
bool _positive(dynamic value) {
if (value is num) return value > 0;
if (value is String) return (int.tryParse(value.trim()) ?? 0) > 0;
return false;
}
ArtifactRecord? _artifactNamed(JsonMap step, String? name) {
if (name == null) return null;
for (final artifact in artifactsOf(step)) {
if (artifact.name == name) return artifact;
}
return null;
}
String? _text(dynamic value) {
if (value == null) return null;
final text = '$value'.trim();
return text.isEmpty ? null : text;
}