feat(app): add the pure native attention model

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.
This commit is contained in:
2026-10-21 22:14:04 +01:00
parent 6a0f1c345e
commit 38f8576c84
6 changed files with 1113 additions and 0 deletions
@@ -0,0 +1,113 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/domain/attention_models.dart';
Map<String, dynamic> run([Map<String, dynamic> overrides = const {}]) => {
'id': 1,
'name': 'Plan and land',
'title': null,
'status': 'failed',
'created_at': '2026-10-19T10:00:00Z',
'finished_at': '2026-10-19T10:05:00Z',
'diagnostic': '',
...overrides,
};
void main() {
group('dismissal and clear-all snapshot', () {
test('a dismissed identity is filtered out and never returns', () {
final rows = [
run({'id': 7, 'status': 'waiting', 'title': 'Wait'}),
];
final dismissed = attentionDismiss(const {}, '7:Decision');
expect(attentionEntries(rows, dismissed: dismissed), isEmpty);
expect(attentionEntries(rows, dismissed: dismissed), isEmpty);
});
test('a different kind for the same run is a different identity', () {
final dismissed = attentionDismiss(const {}, '7:Decision');
final failed = run({'id': 7, 'status': 'failed', 'diagnostic': 'later'});
final entries = attentionEntries([failed], dismissed: dismissed);
expect(entries.single.key, '7:Failed');
expect(entries.single.detail, 'later');
});
test('dismiss ignores a blank key and adds once', () {
final one = attentionDismiss(const {}, '1:Failed');
expect(one, {'1:Failed'});
expect(attentionDismiss(one, '1:Failed'), {'1:Failed'});
expect(attentionDismiss(one, ' '), {'1:Failed'});
expect(one, {'1:Failed'}, reason: 'input set is not mutated');
});
test('clear all applies only the identities in the open snapshot', () {
final a = attentionEntryFor(run({'id': 1}))!;
final snapshot = attentionDismissalSnapshot([a]);
final b = attentionEntryFor(run({'id': 2}))!;
final after = attentionDismissAll(const {}, snapshot);
expect(after, {'1:Failed'});
expect(after.contains(b.key), isFalse);
expect(
attentionEntries([
run({'id': 1}),
run({'id': 2}),
], dismissed: after).map((entry) => entry.runId),
[2],
);
});
test('the snapshot is a defensive copy', () {
final a = attentionEntryFor(run({'id': 1}))!;
final b = attentionEntryFor(run({'id': 2}))!;
final live = [a, b];
final snapshot = attentionDismissalSnapshot(live);
live.add(attentionEntryFor(run({'id': 3}))!);
expect(snapshot.map((entry) => entry.runId), [1, 2]);
expect(snapshot, isNot(same(live)));
});
});
group('persistence', () {
test('the key is the documented one', () {
expect(attentionStorageKey, 'pipelines.web.attention.v1');
});
test('encoding is one sorted JSON array and round-trips', () {
final encoded = encodeAttentionDismissals({
'9:Failed',
'1:Decision',
'4:Failed',
});
expect(encoded, '["1:Decision","4:Failed","9:Failed"]');
expect(decodeAttentionDismissals(encoded), {
'1:Decision',
'4:Failed',
'9:Failed',
});
expect(encodeAttentionDismissals(const {}), '[]');
});
test('blank or unreadable storage decodes to empty and never throws', () {
for (final stored in [
null,
'',
' ',
'not json',
'{',
'"a string"',
'{}',
'42',
]) {
expect(decodeAttentionDismissals(stored), isEmpty);
}
});
test('a JSON array keeps only non-empty strings', () {
expect(
decodeAttentionDismissals(
'["1:Failed", 2, null, {"a":1}, "3:Decision", ""]',
),
{'1:Failed', '3:Decision'},
);
});
});
}
@@ -0,0 +1,334 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/domain/attention_models.dart';
Map<String, dynamic> run([Map<String, dynamic> overrides = const {}]) => {
'id': 1,
'name': 'Plan and land',
'title': null,
'status': 'failed',
'created_at': '2026-10-19T10:00:00Z',
'finished_at': '2026-10-19T10:05:00Z',
'diagnostic': '',
...overrides,
};
void main() {
group('qualification', () {
test('only waiting and failed qualify; every other status asks nobody', () {
expect(attentionKindOf('waiting'), AttentionKind.decision);
expect(attentionKindOf('failed'), AttentionKind.failed);
for (final status in [
'queued',
'running',
'succeeded',
'canceled',
'cancelled',
'interrupted',
'active',
'blocked',
'skipped',
'unknown',
'',
' ',
null,
'FAILED?',
]) {
expect(
attentionKindOf(status),
isNull,
reason: 'status ${status ?? 'null'} must not qualify',
);
}
});
test('trims and lowercases the server words', () {
expect(attentionKindOf(' Waiting '), AttentionKind.decision);
expect(attentionKindOf('Failed\n'), AttentionKind.failed);
expect(attentionKindOf('WAITING'), AttentionKind.decision);
});
test('ranks are Decision 0 then Failed 1', () {
expect(AttentionKind.decision.rank, 0);
expect(AttentionKind.failed.rank, 1);
});
test('waiting excludes audit/export handoffs, not resource waits', () {
expect(
attentionKindForRun(run({'status': 'waiting'})),
AttentionKind.decision,
);
expect(
attentionKindForRun(
run({'status': 'waiting', 'export_pending_sequence': 4}),
),
isNull,
);
expect(
attentionKindForRun(
run({'status': 'waiting', 'export_pending_sequence': ' 2 '}),
),
isNull,
);
expect(
attentionKindForRun(
run({'status': 'waiting', 'export_pending_sequence': 0}),
),
AttentionKind.decision,
);
// web/src/attention.js and Attention.kt never consult resource_wait; the
// server retired it, so a stale value must not hide a real decision.
for (final stale in [
{'resource': 'device-1'},
'device-1',
true,
1,
]) {
expect(
attentionKindForRun(
run({'status': 'waiting', 'resource_wait': stale}),
),
AttentionKind.decision,
reason: 'resource_wait $stale must not suppress a decision',
);
}
// A failed run stays failed even if a stale export field survived.
expect(
attentionKindForRun(
run({'status': 'failed', 'export_pending_sequence': 4}),
),
AttentionKind.failed,
);
});
test(
'nonqualifying runs are dropped entirely, including malformed ids',
() {
final entries = attentionEntries([
run({'id': 1, 'status': 'succeeded'}),
run({'id': 2, 'status': 'running'}),
run({'id': 3, 'status': 'failed'}),
run({'id': 4, 'status': 'queued'}),
run({'id': 'not-a-number', 'status': 'failed'}),
run({'id': 0, 'status': 'failed'}),
]);
expect(entries.map((entry) => entry.runId), [3]);
expect(attentionEntries(null), isEmpty);
},
);
});
group('identity and wording', () {
test('identity is exactly {runId}:{kind}', () {
expect(attentionKey(440, AttentionKind.failed), '440:Failed');
expect(attentionKey(12, AttentionKind.decision), '12:Decision');
});
test('label is the trimmed title else the pipeline name', () {
expect(attentionEntryFor(run({'title': ' My run '}))!.label, 'My run');
expect(attentionEntryFor(run({'title': ' '}))!.label, 'Plan and land');
expect(attentionEntryFor(run({'title': null}))!.label, 'Plan and land');
});
test(
'detail is the decision line or the trimmed diagnostic else Run failed',
() {
expect(
attentionDetail(AttentionKind.decision, run({'status': 'waiting'})),
'Waiting for your decision',
);
expect(
attentionDetail(
AttentionKind.failed,
run({'diagnostic': ' boom '}),
),
'boom',
);
expect(
attentionDetail(AttentionKind.failed, run({'diagnostic': ' '})),
'Run failed',
);
expect(
attentionDetail(AttentionKind.failed, run({'diagnostic': null})),
'Run failed',
);
},
);
test('row description is exactly {pipeline} · {detail}', () {
final entry = attentionEntryFor(
run({'title': 'T', 'diagnostic': 'failed hard'}),
)!;
expect(attentionRowDescription(entry), 'Plan and land · failed hard');
});
test('badge label is bare Notifications when empty, else the count', () {
expect(attentionBadgeLabel(0), 'Notifications');
expect(attentionBadgeLabel(-3), 'Notifications');
expect(attentionBadgeLabel(1), 'Notifications, 1 needing attention');
expect(attentionBadgeLabel(171), 'Notifications, 171 needing attention');
});
test('the entry keeps the whole summary, including unknown fields', () {
final source = run({
'unknown_future_field': {'keep': true},
'id': 7,
});
final entry = attentionEntryFor(source)!;
expect(entry.run['unknown_future_field'], {'keep': true});
expect(entry.run, same(source));
});
});
group('ordering', () {
test('kind rank, then recency descending, then run id descending', () {
final entries = attentionEntries([
run({
'id': 10,
'status': 'failed',
'finished_at': '2026-10-19T10:00:00Z',
}),
run({
'id': 11,
'status': 'waiting',
'finished_at': '2026-10-19T09:00:00Z',
}),
run({
'id': 12,
'status': 'failed',
'finished_at': '2026-10-19T11:00:00Z',
}),
run({
'id': 13,
'status': 'waiting',
'finished_at': '2026-10-19T08:00:00Z',
}),
]);
expect(entries.map((entry) => entry.runId), [11, 13, 12, 10]);
});
test('recency falls back to created_at when finished_at is blank', () {
final entries = attentionEntries([
run({
'id': 1,
'status': 'failed',
'finished_at': '',
'created_at': '2026-10-19T12:00:00Z',
}),
run({
'id': 2,
'status': 'failed',
'finished_at': '2026-10-19T11:00:00Z',
}),
]);
expect(entries.map((entry) => entry.runId), [1, 2]);
});
test('equal kind and recency tie by run id descending and stay stable', () {
final rows = [
run({'id': 3, 'finished_at': '2026-10-19T10:00:00Z'}),
run({'id': 9, 'finished_at': '2026-10-19T10:00:00Z'}),
run({'id': 5, 'finished_at': '2026-10-19T10:00:00Z'}),
];
expect(attentionEntries(rows).map((entry) => entry.runId), [9, 5, 3]);
expect(attentionEntries(rows.reversed).map((entry) => entry.runId), [
9,
5,
3,
]);
});
});
group('local-day window', () {
final since = DateTime.utc(2026, 10, 20);
final rows = [
run({'id': 1, 'finished_at': '2026-10-19T21:33:27Z'}),
run({'id': 2, 'finished_at': '2026-10-20T10:00:00Z'}),
];
test('startOfLocalDay is local midnight, injectable for tests', () {
final now = DateTime(2026, 10, 20, 15, 4, 5, 6);
final start = startOfLocalDay(now: now);
expect(start, DateTime(2026, 10, 20));
expect(start.hour, 0);
expect(start.minute, 0);
expect(start.second, 0);
expect(start.millisecond, 0);
expect(start.isBefore(now), isTrue);
expect(start.toLocal(), start);
});
test('a cutoff drops older work and keeps newer work', () {
expect(attentionEntries(rows, since: since).map((entry) => entry.runId), [
2,
]);
});
test('no cutoff, and an epoch-zero cutoff, show everything', () {
expect(attentionEntries(rows).map((entry) => entry.runId), [2, 1]);
expect(
attentionEntries(
rows,
since: DateTime.fromMillisecondsSinceEpoch(0),
).map((entry) => entry.runId),
[2, 1],
);
});
test('the cutoff applies to waiting decisions as well as failures', () {
expect(
attentionEntries([
run({
'id': 3,
'status': 'waiting',
'finished_at': '2026-10-19T09:00:00Z',
}),
], since: since),
isEmpty,
);
});
test('unparseable or blank recency is kept, as in the browser', () {
for (final recency in ['', ' ', 'not a timestamp']) {
final kept = attentionEntries([
run({'id': 4, 'finished_at': recency, 'created_at': recency}),
], since: since);
expect(
kept.map((entry) => entry.runId),
[4],
reason: 'recency ${recency.isEmpty ? '<blank>' : recency} is kept',
);
}
});
test('finished_at is the recency the cutoff reads, else created_at', () {
expect(
attentionEntries([
run({
'id': 5,
'created_at': '2026-10-18T09:00:00Z',
'finished_at': '2026-10-20T09:00:00Z',
}),
], since: since).map((entry) => entry.runId),
[5],
);
expect(
attentionEntries([
run({
'id': 6,
'created_at': '2026-10-18T09:00:00Z',
'finished_at': '',
}),
], since: since),
isEmpty,
);
});
test('dismissal and the cutoff compose', () {
final dismissed = attentionDismiss(const {}, '2:Failed');
expect(
attentionEntries(rows, dismissed: dismissed, since: since),
isEmpty,
);
});
});
}
@@ -0,0 +1,224 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/domain/attention_models.dart';
import 'package:pipelines_app/features/attention/domain/attention_transitions.dart';
Map<String, dynamic> run([Map<String, dynamic> overrides = const {}]) => {
'id': 1,
'name': 'Plan and land',
'title': null,
'status': 'failed',
'created_at': '2026-10-19T10:00:00Z',
'finished_at': '2026-10-19T10:05:00Z',
'diagnostic': '',
...overrides,
};
void main() {
test('a seeded failed/waiting run raises nothing', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 1, 'status': 'failed'}),
run({'id': 2, 'status': 'waiting'}),
]);
expect(detector.observe(run({'id': 1, 'status': 'failed'})), isNull);
expect(detector.observe(run({'id': 2, 'status': 'waiting'})), isNull);
expect(detector.emittedKeys, isEmpty);
});
test('a first-seen run raises nothing and becomes the baseline', () {
final detector = AttentionTransitionDetector();
expect(detector.observe(run({'id': 5, 'status': 'failed'})), isNull);
expect(detector.statusOf(5), 'failed');
expect(detector.observe(run({'id': 5, 'status': 'failed'})), isNull);
expect(detector.emittedKeys, isEmpty);
});
test('entering failed from a known running run emits exactly once', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 5, 'status': 'running'}),
]);
final first = detector.observe(run({'id': 5, 'status': 'failed'}));
expect(first, isNotNull);
expect(first!.runId, 5);
expect(first.kind, AttentionKind.failed);
expect(first.key, '5:Failed');
expect(first.entry.label, 'Plan and land');
expect(first.entry.detail, 'Run failed');
expect(detector.observe(run({'id': 5, 'status': 'failed'})), isNull);
expect(detector.emittedKeys, {'5:Failed'});
});
test('entering a human waiting state emits a Decision', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 6, 'status': 'running'}),
]);
final transition = detector.observe(
run({'id': 6, 'status': 'waiting', 'title': ' Decide '}),
);
expect(transition, isNotNull);
expect(transition!.kind, AttentionKind.decision);
expect(transition.key, '6:Decision');
expect(transition.entry.label, 'Decide');
expect(transition.entry.detail, 'Waiting for your decision');
});
test('waiting to failed is a new kind and emits once', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 7, 'status': 'waiting'}),
]);
final transition = detector.observe(run({'id': 7, 'status': 'failed'}));
expect(transition!.key, '7:Failed');
expect(detector.emittedKeys, {'7:Failed'});
});
test('a run that leaves the state and returns may emit again', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 8, 'status': 'running'}),
]);
expect(detector.observe(run({'id': 8, 'status': 'failed'})), isNotNull);
expect(detector.observe(run({'id': 8, 'status': 'running'})), isNull);
expect(detector.emittedKeys, isEmpty);
final again = detector.observe(run({'id': 8, 'status': 'failed'}));
expect(again, isNotNull);
expect(detector.observe(run({'id': 8, 'status': 'failed'})), isNull);
});
test('nonqualifying statuses never emit', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 9, 'status': 'queued'}),
]);
for (final status in [
'running',
'succeeded',
'canceled',
'interrupted',
'unknown',
]) {
expect(
detector.observe(run({'id': 9, 'status': status})),
isNull,
reason: 'status $status must not emit',
);
}
expect(detector.emittedKeys, isEmpty);
});
test(
'an export wait never emits a Decision even though status is waiting',
() {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 10, 'status': 'running'}),
]);
expect(
detector.observe(
run({'id': 10, 'status': 'waiting', 'export_pending_sequence': 2}),
),
isNull,
);
expect(detector.emittedKeys, isEmpty);
},
);
test('dismissed identities are marked seen but do not emit', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 11, 'status': 'running'}),
]);
expect(
detector.observe(
run({'id': 11, 'status': 'failed'}),
dismissed: {'11:Failed'},
),
isNull,
);
expect(detector.emittedKeys, {'11:Failed'});
expect(detector.observe(run({'id': 11, 'status': 'failed'})), isNull);
});
test('a reconnect reseed produces no transition and no replay', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 12, 'status': 'failed'}),
]);
detector.resetForReconnect();
detector.reseed([
run({'id': 12, 'status': 'failed'}),
]);
expect(detector.observe(run({'id': 12, 'status': 'failed'})), isNull);
expect(detector.emittedKeys, isEmpty);
});
test('a reseed keeps still-qualifying keys and prunes the rest', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 20, 'status': 'running'}),
run({'id': 21, 'status': 'running'}),
]);
expect(detector.observe(run({'id': 20, 'status': 'failed'})), isNotNull);
expect(detector.observe(run({'id': 21, 'status': 'waiting'})), isNotNull);
expect(detector.emittedKeys, {'20:Failed', '21:Decision'});
detector.reseed([
run({'id': 20, 'status': 'failed'}),
run({'id': 21, 'status': 'succeeded'}),
]);
// 20 still qualifies, so its key survives; 21 no longer asks, so its key is
// pruned and a genuine later transition can be raised once more.
expect(detector.emittedKeys, {'20:Failed'});
expect(detector.observe(run({'id': 20, 'status': 'failed'})), isNull);
expect(detector.observe(run({'id': 21, 'status': 'waiting'})), isNotNull);
expect(detector.observe(run({'id': 21, 'status': 'waiting'})), isNull);
});
test('a reseed keeps the key of a run absent from the read', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 22, 'status': 'running'}),
]);
expect(detector.observe(run({'id': 22, 'status': 'failed'})), isNotNull);
detector.reseed([
run({'id': 23, 'status': 'running'}),
]);
expect(detector.emittedKeys, {'22:Failed'});
// Absence says nothing, so seeing the same state again is not a transition.
expect(detector.observe(run({'id': 22, 'status': 'failed'})), isNull);
});
test('a snapshot bracketed as a seed does not emit or replay', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 13, 'status': 'running'}),
])
..beginSeed();
expect(detector.observe(run({'id': 13, 'status': 'waiting'})), isNull);
detector.endSeed();
expect(detector.observe(run({'id': 13, 'status': 'waiting'})), isNull);
expect(detector.emittedKeys, isEmpty);
});
test('the transition keeps the whole frame, including unknown fields', () {
final detector = AttentionTransitionDetector()
..seed([
run({'id': 14, 'status': 'running'}),
]);
final frame = run({
'id': 14,
'run_id': 14,
'status': 'failed',
'diagnostic': ' boom ',
'unknown_future_field': ['keep'],
});
final transition = detector.observe(frame);
expect(transition!.entry.detail, 'boom');
expect(transition.run, same(frame));
expect(transition.run['unknown_future_field'], ['keep']);
});
}