38f8576c84
Port the browser's Needs-you list, dismissal bookkeeping and attention
transition ledger as widget-free Dart under lib/features/attention/domain/.
Includes the local-day window, export-wait exclusion, {runId}:{kind}
identities, sorted dismissal storage and focused regressions.
137 lines
5.0 KiB
Dart
137 lines
5.0 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
import 'attention_models.dart';
|
|
|
|
/// One newly-entered attention state: a run that has just become failed or has
|
|
/// just become a human decision. The entry carries the whole current summary, so
|
|
/// its wording and unknown fields match what the surface would show.
|
|
class AttentionTransition {
|
|
const AttentionTransition(this.entry);
|
|
|
|
final AttentionEntry entry;
|
|
|
|
int get runId => entry.runId;
|
|
AttentionKind get kind => entry.kind;
|
|
String get key => entry.key;
|
|
JsonMap get run => entry.run;
|
|
}
|
|
|
|
/// Tracks the last status seen for each run and emits only a genuine transition
|
|
/// *into* a qualifying state.
|
|
///
|
|
/// - [seed] records the authoritative list (the initial `list_runs` read or a
|
|
/// `subscribe` snapshot) without emitting anything, so a run that was already
|
|
/// failed/waiting before this client looked never announces itself.
|
|
/// - [beginSeed]/[endSeed] bracket a reconnect's pushed snapshot: frames seen
|
|
/// while seeding update the baseline but raise nothing, so a gap can never
|
|
/// manufacture a transition.
|
|
/// - [observe] emits only when a known run's status changes into `failed` or a
|
|
/// human `waiting`. A duplicate frame, an unchanged qualifying state, an
|
|
/// unknown/nonqualifying status and a first-seen run all return null.
|
|
/// - A run+kind key is forgotten when the run leaves that qualifying state (or
|
|
/// enters the other kind), so a genuinely new transition can fire once more;
|
|
/// the same transition can never repeat.
|
|
/// - [resetForReconnect] drops the status baseline but keeps the emitted keys.
|
|
/// The following [reseed] establishes the new baseline without emitting, so a
|
|
/// disconnect cannot produce a delayed notification for a state the operator
|
|
/// already saw.
|
|
class AttentionTransitionDetector {
|
|
final Map<int, String> _lastStatus = {};
|
|
final Set<String> _emitted = {};
|
|
bool _seeding = false;
|
|
|
|
/// Records [runs] as the baseline without emitting. Keys that still qualify
|
|
/// are kept so a reconnect cannot repeat them; keys whose run disappeared from
|
|
/// the read are kept too, because an absent run tells us nothing.
|
|
void seed(Iterable<JsonMap>? runs) {
|
|
final statuses = <int, String>{};
|
|
final kinds = <int, AttentionKind?>{};
|
|
for (final run in runs ?? const <JsonMap>[]) {
|
|
final runId = attentionRunId(run['run_id']) ?? attentionRunId(run['id']);
|
|
if (runId == null) continue;
|
|
statuses[runId] = _word(run['status']);
|
|
kinds[runId] = attentionKindForRun(run);
|
|
}
|
|
_lastStatus
|
|
..clear()
|
|
..addAll(statuses);
|
|
for (final key in [..._emitted]) {
|
|
final split = key.lastIndexOf(':');
|
|
if (split <= 0) continue;
|
|
final runId = attentionRunId(key.substring(0, split));
|
|
if (runId == null || !statuses.containsKey(runId)) continue;
|
|
final kind = kinds[runId];
|
|
if (kind == null || key != attentionKey(runId, kind)) {
|
|
_emitted.remove(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Replaces the baseline for a reconnect. Emitted keys survive: a transition
|
|
/// swallowed by the gap is not replayed after the socket returns.
|
|
void reseed(Iterable<JsonMap>? runs) {
|
|
_lastStatus.clear();
|
|
_seeding = false;
|
|
seed(runs);
|
|
}
|
|
|
|
/// Drops the status baseline but keeps this session's emitted keys. Call
|
|
/// before the next [seed] when the connection is lost.
|
|
void resetForReconnect() {
|
|
_lastStatus.clear();
|
|
_seeding = false;
|
|
}
|
|
|
|
void beginSeed() => _seeding = true;
|
|
|
|
void endSeed() => _seeding = false;
|
|
|
|
/// Observes one pushed `run_event` summary/frame. Returns the transition only
|
|
/// when this run enters a qualifying state; [dismissed] identities are marked
|
|
/// seen but do not emit.
|
|
AttentionTransition? observe(
|
|
JsonMap frame, {
|
|
Set<String> dismissed = const {},
|
|
}) {
|
|
final runId =
|
|
attentionRunId(frame['run_id']) ?? attentionRunId(frame['id']);
|
|
final status = _word(frame['status']);
|
|
if (runId == null || status.isEmpty) return null;
|
|
|
|
final previous = _lastStatus[runId];
|
|
_lastStatus[runId] = status;
|
|
|
|
final kind = attentionKindForRun(frame);
|
|
if (kind == null) {
|
|
_forget(runId);
|
|
return null;
|
|
}
|
|
for (final other in AttentionKind.values) {
|
|
if (other != kind) _emitted.remove(attentionKey(runId, other));
|
|
}
|
|
if (_seeding) return null;
|
|
|
|
final key = attentionKey(runId, kind);
|
|
if (previous == null || previous == status) return null;
|
|
|
|
final entry = attentionEntryFor(frame);
|
|
if (entry == null) return null;
|
|
_emitted.add(key);
|
|
if (dismissed.contains(key)) return null;
|
|
return AttentionTransition(entry);
|
|
}
|
|
|
|
/// A copy of the run+kind identities raised in this session.
|
|
Set<String> get emittedKeys => {..._emitted};
|
|
|
|
/// The last status recorded for [runId], or null for a run never seen.
|
|
String? statusOf(int runId) => _lastStatus[runId];
|
|
|
|
void _forget(int runId) {
|
|
for (final kind in AttentionKind.values) {
|
|
_emitted.remove(attentionKey(runId, kind));
|
|
}
|
|
}
|
|
}
|
|
|
|
String _word(dynamic value) => '${value ?? ''}'.trim().toLowerCase();
|