merge(app): land notification capability after a retained deadline

This commit is contained in:
2026-10-22 02:05:36 +01:00
19 changed files with 2882 additions and 0 deletions
+291
View File
@@ -0,0 +1,291 @@
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pipelines_app/features/attention/domain/attention_models.dart';
import 'package:pipelines_app/features/attention/notify/notification_bridge.dart';
import 'package:pipelines_app/features/attention/notify/notification_controller.dart';
import 'package:pipelines_app/features/attention/notify/notification_ledger.dart';
import 'package:pipelines_app/features/attention/notify/notification_models.dart';
import 'package:pipelines_app/features/attention/notify/notification_store.dart';
import 'package:pipelines_app/features/attention/runtime/attention_controller.dart';
import 'package:pipelines_app/features/attention/runtime/attention_store.dart';
import 'package:pipelines_app/features/attention/ui/notification_permission_tile.dart';
/// Browser capture entrypoint for the notification capability.
///
/// It drives the real [NotificationController] over the real web bridge, so a
/// browser drive can prove the seed is silent, one live transition raises
/// exactly one notification with the entry's own words and run+kind identity, a
/// dismissed or replayed transition raises none, a second decision gate of one
/// run raises a fresh notification, and the Settings tile reads honestly in
/// every state on both hosts. Nothing here opens a socket or touches real
/// operator data; the store is in memory.
void main() {
final params = Uri.base.queryParameters;
final mode = params['mode'] ?? 'transition';
final cupertino = params['host'] == 'cupertino';
final scale = double.tryParse(params['scale'] ?? '') ?? 1;
final brightness = params['theme'] == 'light'
? Brightness.light
: Brightness.dark;
Widget scaled(BuildContext context, Widget? app) => MediaQuery(
data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(scale)),
child: app!,
);
final home = switch (mode) {
'tiles' => const _TileGallery(),
'tile' => _SingleTile(state: _stateOf(params['state'])),
_ => _NotificationProbe(mode: mode),
};
if (cupertino) {
runApp(
CupertinoApp(
theme: CupertinoThemeData(brightness: brightness),
builder: scaled,
home: home,
),
);
return;
}
runApp(
MaterialApp(
theme: ThemeData(
platform: TargetPlatform.android,
brightness: brightness,
),
builder: scaled,
home: home,
),
);
}
/// The report the browser drive reads, as a plain JS string.
void _publishReport(String value) =>
globalContext.setProperty('__probeReport'.toJS, value.toJS);
/// The permission state named by a `state` query parameter, defaulting to the
/// undecided one.
NotificationPermissionState _stateOf(String? name) {
for (final state in NotificationPermissionState.values) {
if (state.wireName == name) return state;
}
return NotificationPermissionState.notRequested;
}
String _at(Duration before) =>
DateTime.now().toUtc().subtract(before).toIso8601String();
Map<String, dynamic> _summary(
int id,
String status, {
String name = 'release',
String? title,
String diagnostic = '',
}) => {
'id': id,
'name': name,
'title': title ?? 'Run $id',
'status': status,
'diagnostic': diagnostic,
'created_at': _at(const Duration(hours: 1)),
// The server leaves finished_at empty while a run is in flight or waiting.
'finished_at': status == 'waiting' || status == 'running'
? ''
: _at(const Duration(minutes: 55)),
};
Map<String, dynamic> _frame(
int id,
String status, {
String name = 'release',
String? title,
String diagnostic = '',
}) => {
'type': 'run_event',
'run_id': id,
'name': name,
'title': title ?? 'Run $id',
'status': status,
'diagnostic': diagnostic,
'finished_at': status == 'waiting' || status == 'running'
? ''
: _at(const Duration(minutes: 1)),
};
/// Publish exactly as the manager wires it: the accumulated transitions and the
/// current qualifying keys from the same attention snapshot.
int _publish(NotificationController notify, AttentionController attention) =>
notify.publishTransitions(
attention.transitions,
qualifying: notificationQualifyingKeys(attention.rows),
dismissed: attention.dismissed,
);
/// Runs one notification scenario against the real bridge, then reports it and
/// draws the live permission tile underneath.
class _NotificationProbe extends StatefulWidget {
const _NotificationProbe({required this.mode});
final String mode;
@override
State<_NotificationProbe> createState() => _NotificationProbeState();
}
class _NotificationProbeState extends State<_NotificationProbe> {
String _report = 'running';
NotificationPermissionState _permission =
NotificationPermissionState.unsupported;
bool _asked = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _run());
}
Future<void> _run() async {
final bridge = createNotificationBridge();
final notify = NotificationController(
store: MemoryNotificationStore(),
bridge: bridge,
);
await notify.load();
final attention = AttentionController(store: MemoryAttentionStore());
await attention.load();
switch (widget.mode) {
case 'seed':
attention.seedRows([
_summary(1, 'failed', diagnostic: 'Stage 3 stopped'),
_summary(2, 'waiting'),
]);
_publish(notify, attention);
case 'dismissed':
attention.seedRows([_summary(1, 'running')]);
await attention.dismiss(attentionEntryFor(_summary(1, 'failed'))!);
attention.observeFrame(_frame(1, 'failed'));
_publish(notify, attention);
case 'reconnect':
attention.seedRows([_summary(1, 'running')]);
attention.observeFrame(_frame(1, 'failed'));
_publish(notify, attention);
attention.resetForReconnect();
attention.seedRows([_summary(1, 'failed')], reconnect: true);
_publish(notify, attention);
attention.observeFrame(_frame(1, 'failed'));
_publish(notify, attention);
case 'secondgate':
// A waiting run has no finished_at, so two gates of one run carry the
// same timestamps; leaving the state is what makes the second new.
attention.seedRows([_summary(7, 'running')]);
attention.observeFrame(_frame(7, 'waiting'));
_publish(notify, attention);
attention.observeFrame(_frame(7, 'running'));
_publish(notify, attention);
attention.observeFrame(_frame(7, 'waiting'));
_publish(notify, attention);
case 'denied':
case 'unsupported':
case 'default':
attention.seedRows([_summary(1, 'running')]);
attention.observeFrame(_frame(1, 'failed'));
_publish(notify, attention);
default:
attention.seedRows([_summary(1, 'running', name: 'nightly')]);
attention.observeFrame(
_frame(
1,
'failed',
name: 'nightly',
title: 'Nightly audit',
diagnostic: 'Stage 3 stopped',
),
);
_publish(notify, attention);
}
if (!mounted) return;
final report =
'mode=${widget.mode} posted=${notify.postedCount} '
'state=${notify.permission.wireName} asked=${notify.asked} '
'notified=${notify.notified.length}';
_publishReport(report);
setState(() {
_report = report;
_permission = notify.permission;
_asked = notify.asked;
});
notify.dispose();
attention.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ListView(
padding: const EdgeInsets.all(16),
children: [
Text(_report),
const SizedBox(height: 16),
NotificationPermissionTile(
state: _permission,
asked: _asked,
onRequest: () {},
),
],
),
),
);
}
}
/// One permission state on its own, so each can be captured and read at the
/// hardest width and text scale.
class _SingleTile extends StatelessWidget {
const _SingleTile({required this.state});
final NotificationPermissionState state;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: NotificationPermissionTile(state: state, onRequest: () {}),
),
),
);
}
}
/// Every permission state at once, so both hosts can be captured and read in
/// one page without touching a real permission.
class _TileGallery extends StatelessWidget {
const _TileGallery();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (final state in NotificationPermissionState.values) ...[
Text(state.wireName),
NotificationPermissionTile(state: state, onRequest: () {}),
],
],
),
),
),
);
}
}
+314
View File
@@ -0,0 +1,314 @@
// Real-browser acceptance for the attention notification capability.
//
// flutter build web -t tool/notification_probe.dart -o build/notification_probe_web
// OPENCODE_ARTIFACT_DIR=<dir> node tool/notification_probe.mjs
//
// The page is served from an isolated loopback server. `window.Notification` is
// replaced with a recording spy before the app loads, and the real browser
// permission is granted (or denied) through CDP, so the drive proves the real
// bridge's behaviour: the initial seed raises nothing, one live transition
// raises exactly one notification with the entry's own words and run+kind
// identity, a dismissed transition and a reconnect replay raise none, a second
// decision gate of one run raises a fresh notification (the waiting shape has no
// finished_at), and the Settings tile reads honestly in every state on both
// hosts at 320 logical px and 2x text. One PNG is written per captured state.
import { createServer } from 'node:http';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { existsSync } from 'node:fs';
import { setTimeout as delay } from 'node:timers/promises';
import { join, extname, resolve, sep } from 'node:path';
import { tmpdir } from 'node:os';
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
const root = resolve(process.env.PROBE_WEB_ROOT ?? 'build/notification_probe_web');
const artifactDir = process.env.OPENCODE_ARTIFACT_DIR;
if (!artifactDir) throw new Error('Set OPENCODE_ARTIFACT_DIR for the probe captures');
await mkdir(artifactDir, { recursive: true });
const defaultChrome = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
const chromePath = process.env.CHROME_PATH ?? defaultChrome;
if (!existsSync(chromePath)) throw new Error(`Chrome not found at ${chromePath}; set CHROME_PATH`);
const mime = {
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.wasm': 'application/wasm',
'.png': 'image/png',
'.ico': 'image/x-icon',
};
const server = createServer(async (req, res) => {
try {
const path = resolve(root, '.' + decodeURIComponent(new URL(req.url, 'http://localhost').pathname));
if (path !== root && !path.startsWith(root + sep)) return res.writeHead(403).end();
const filePath = path === root || path.endsWith('/') ? join(path, 'index.html') : path;
const file = await readFile(filePath);
res.writeHead(200, { 'content-type': mime[extname(filePath)] ?? 'application/octet-stream' }).end(file);
} catch {
res.writeHead(404).end('not found');
}
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
const port = server.address().port;
const origin = `http://127.0.0.1:${port}`;
// The spy is installed before every document, so the app only ever sees it. It
// delegates `permission` to the real constructor (which CDP granted or denied)
// and records each `new Notification(...)` call.
const spy = `(() => {
const mode = new URLSearchParams(location.search).get('mode') || '';
window.__notifications = [];
window.__probeReport = '';
if (mode === 'unsupported') { window.Notification = undefined; return; }
const Real = window.Notification;
function Spy(title, options) {
window.__notifications.push({
title: String(title ?? ''),
body: String((options && options.body) || ''),
tag: String((options && options.tag) || ''),
});
return { onclick: null, close() {} };
}
Object.defineProperty(Spy, 'permission', {
get: () => (Real ? Real.permission : 'granted'),
});
Spy.requestPermission = () => Promise.resolve(Spy.permission);
window.Notification = Spy;
})();`;
const profile = await mkdtemp(join(tmpdir(), 'notification-chrome-'));
const chrome = spawn(
chromePath,
[
'--headless=new',
'--no-first-run',
'--no-default-browser-check',
'--disable-gpu',
'--remote-debugging-port=0',
`--user-data-dir=${profile}`,
'about:blank',
],
{ stdio: ['ignore', 'ignore', 'pipe'] },
);
let stderr = '';
chrome.stderr.on('data', (data) => { stderr += data; });
const waitFor = async (check) => {
for (let i = 0; i < 120; i++) {
const value = await check();
if (value) return value;
await delay(100);
}
throw new Error(`Timed out waiting for Chrome DevTools\n${stderr.slice(-1200)}`);
};
let ws;
try {
const endpoint = await waitFor(() => stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/)?.[1]);
ws = new WebSocket(endpoint);
await once(ws, 'open');
let id = 0;
const pending = new Map();
ws.addEventListener('message', (event) => {
const message = JSON.parse(event.data);
if (message.id && pending.has(message.id)) {
pending.get(message.id)(message);
pending.delete(message.id);
}
});
const send = (method, params = {}, sessionId) =>
new Promise((resolveCall, rejectCall) => {
const callId = ++id;
pending.set(callId, (message) => {
if (message.error) rejectCall(new Error(`${method}: ${message.error.message}`));
else resolveCall(message.result);
});
ws.send(JSON.stringify({ id: callId, method, params, ...(sessionId ? { sessionId } : {}) }));
});
const target = (await send('Target.createTarget', { url: 'about:blank' })).targetId;
const session = (await send('Target.attachToTarget', { targetId: target, flatten: true })).sessionId;
await send('Page.enable', {}, session);
await send('Runtime.enable', {}, session);
await send('Page.addScriptToEvaluateOnNewDocument', { source: spy }, session);
const evaluate = async (expression) => {
const response = await send(
'Runtime.evaluate',
{ expression, returnByValue: true, awaitPromise: true },
session,
);
if (response.exceptionDetails) {
throw new Error(`Runtime.evaluate failed: ${JSON.stringify(response.exceptionDetails)}`);
}
return response.result?.value;
};
const semantics = () => evaluate(
"[...document.querySelectorAll('flt-semantics')].map(x=>{const r=x.getBoundingClientRect();return{label:(x.getAttribute('aria-label')||x.innerText||'').trim(),role:x.getAttribute('role'),x:r.x,y:r.y,w:r.width,h:r.height}})",
);
const requireLabels = (all, needles, state) => {
for (const needle of needles) {
if (!all.some((label) => label.includes(needle))) {
throw new Error(`Missing ${JSON.stringify(needle)} in ${state}: ${JSON.stringify(all)}`);
}
}
};
let viewport = { width: 320, height: 640 };
const navigate = async ({ mode, host = 'material', scale = 1, theme = 'dark', width = 320, height = 640, semantics: enableSemantics = false, state } = {}) => {
viewport = { width, height };
await send('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: theme }] }, session);
const query = new URLSearchParams({ mode, host, scale: String(scale), theme });
if (state) query.set('state', state);
await send('Page.navigate', { url: `${origin}/?${query}` }, session);
await delay(2500);
await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 2, mobile: true }, session);
await delay(1000);
if (enableSemantics) {
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
await delay(1200);
}
};
const waitForReport = async (mode) => {
for (let attempt = 0; attempt < 80; attempt++) {
const report = await evaluate('window.__probeReport');
if (report && report.includes(`mode=${mode}`)) return report;
await delay(150);
}
throw new Error(`The ${mode} scenario never reported`);
};
const notifications = () => evaluate('JSON.stringify(window.__notifications)');
const capture = async (name) => {
const data = (await send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }, session)).data;
await writeFile(join(artifactDir, name), Buffer.from(data, 'base64'));
};
const grant = () => send('Browser.grantPermissions', { origin, permissions: ['notifications'] });
const deny = () => send('Browser.setPermission', {
permission: { name: 'notifications' },
setting: 'denied',
origin,
});
const prompt = () => send('Browser.setPermission', {
permission: { name: 'notifications' },
setting: 'prompt',
origin,
});
const checks = [];
const captures = [];
const check = (name, condition, detail) => {
if (!condition) throw new Error(`${name}: ${detail}`);
checks.push(name);
};
await prompt();
// 1. The initial list seed never notifies.
await navigate({ mode: 'seed' });
const seedReport = await waitForReport('seed');
check('seed report', seedReport.includes('posted=0'), seedReport);
check('seed silent', (await notifications()) === '[]', await notifications());
// 2. One live transition notifies exactly once, with the entry's own words.
await grant();
await navigate({ mode: 'transition' });
const transitionReport = await waitForReport('transition');
check('transition posted once', transitionReport.includes('posted=1'), transitionReport);
const posted = JSON.parse(await notifications());
check('transition count', posted.length === 1, JSON.stringify(posted));
check('transition title', posted[0].title === 'A run failed', JSON.stringify(posted[0]));
check('transition body', posted[0].body === 'Nightly audit · nightly', JSON.stringify(posted[0]));
check('transition identity', posted[0].tag === '1:Failed', JSON.stringify(posted[0]));
await capture('notification-transition-material-dark-320-2x.png');
captures.push('notification-transition-material-dark-320-2x.png');
// 3. A dismissed transition never notifies.
await navigate({ mode: 'dismissed' });
const dismissedReport = await waitForReport('dismissed');
check('dismissed report', dismissedReport.includes('posted=0'), dismissedReport);
check('dismissed silent', (await notifications()) === '[]', await notifications());
// 4. A reconnect replay never notifies again.
await navigate({ mode: 'reconnect' });
const reconnectReport = await waitForReport('reconnect');
check('reconnect posted once', reconnectReport.includes('posted=1'), reconnectReport);
check('reconnect count', JSON.parse(await notifications()).length === 1, await notifications());
// 4b. A second decision gate of one run notifies again. The waiting frames
// carry no finished_at, so the run's timestamps cannot tell the gates apart.
await navigate({ mode: 'secondgate' });
const secondGateReport = await waitForReport('secondgate');
check('second gate posted twice', secondGateReport.includes('posted=2'), secondGateReport);
const gates = JSON.parse(await notifications());
check('second gate count', gates.length === 2, JSON.stringify(gates));
check(
'second gate identity',
gates.every((item) => item.tag === '7:Decision'),
JSON.stringify(gates),
);
check(
'second gate title',
gates.every((item) => item.title === 'A run needs your decision'),
JSON.stringify(gates),
);
// 5. A denied page is honest and posts nothing.
await deny();
await navigate({ mode: 'denied', semantics: true });
const deniedReport = await waitForReport('denied');
check('denied report', deniedReport.includes('state=denied') && deniedReport.includes('posted=0'), deniedReport);
check('denied silent', (await notifications()) === '[]', await notifications());
requireLabels(await semantics().then((rows) => rows.map((r) => r.label)), [
'Notifications are blocked for this site.',
], 'denied tile');
await capture('notification-denied-material-dark-320-2x.png');
captures.push('notification-denied-material-dark-320-2x.png');
// 6. An unsupported browser is honest and posts nothing.
await grant();
await navigate({ mode: 'unsupported', semantics: true });
const unsupportedReport = await waitForReport('unsupported');
check('unsupported report', unsupportedReport.includes('state=unsupported'), unsupportedReport);
check('unsupported silent', (await notifications()) === '[]', await notifications());
requireLabels(await semantics().then((rows) => rows.map((r) => r.label)), [
'This browser does not support notifications.',
], 'unsupported tile');
await capture('notification-unsupported-material-dark-320-2x.png');
captures.push('notification-unsupported-material-dark-320-2x.png');
// 7. Each tile state reads honestly on both hosts, at the hardest case:
// 320 logical px and 2x text.
await prompt();
const states = {
unsupported: 'This browser does not support notifications.',
insecure: 'Notifications need a secure context (https or localhost).',
default: 'Notifications are off. Enable them to be told when a run fails',
granted: 'Notifications are on. Failed and waiting runs will raise a browser notification.',
denied: 'Notifications are blocked for this site.',
};
for (const host of ['material', 'cupertino']) {
for (const [state, copy] of Object.entries(states)) {
await navigate({ mode: 'tile', host, scale: 2, semantics: true, state });
const labels = (await semantics()).map((row) => row.label);
requireLabels(labels, [
'Pipelines can notify you when a run is waiting',
copy,
], `${host} ${state} tile`);
if (state === 'default') {
requireLabels(labels, ['Enable notifications'], `${host} ${state} action`);
}
checks.push(`${host} ${state} tile`);
const name = `notification-tile-${state}-${host}-dark-320-2x.png`;
await capture(name);
captures.push(name);
}
}
console.log(JSON.stringify({ checks, captures }, null, 2));
} finally {
ws?.close();
chrome.kill('SIGTERM');
await Promise.race([once(chrome, 'exit'), delay(3000)]);
server.close();
await rm(profile, { recursive: true, force: true });
}