// The serving Mac's off-Mac backup, as one notice on the board.//// WHY: the owner works from the phone, and the Mac's copy to its bucket was// dead for two days with nothing on the phone saying so. So a Mac whose backup// FAILED, or fell BEHIND past its own lag allowance, is one notice in the// board's notices part — on Home and on the database page — until the Mac says// it caught up.//// What is judged where:// * failing or not is the MAC's judgement: the relay's status is `failed`, or// `behind` once it has not caught up for longer than the Mac's// `COTTON_BACKUP_LAG_MINUTES` (`cotton_client::backup::lag`), and the core// maps both to `failing` (`methods/host_settings.rs` `backup_section`). No// second lag constant lives on this side to drift from the Mac's.// * since when, the same: the Mac's `since` (when it fell behind, or the// last sync it saw before a failure).// * here: ONE notice per Mac however many of its databases fail, its sentence,// and that silence changes nothing — a phone that cannot reach the Mac// keeps what the Mac last said rather than inventing "not backed up".// * ON HOME ONLY PAST A DAY (owner, 11 Oct 2026): a Mac a few minutes// behind is the stream catching up, not news — the card said "not backed// up since 1:48 am" half an hour later and pushed the board down. So it is// raised only when the last copy that got through (`lastAt`, the backup's// last-applied time) is more than `staleAfter` old, or none ever did; the// Mac's own state lives on its page either way.// * the rule is "the owner's databases are not being backed up off the Mac",// so HALF A CONFIGURATION (a bucket named, its settings not adding up —// the relay's `invalid`) raises it too, with its own sentence. Starting is// not raised (the first copy is on its way), nor is no bucket named at all// (`off`: a Mac set up to hold the only copy).importCottonKitimportCottonValuesimportFoundation/// One Mac's backup that is not getting through.publicstructBackupNotice: Equatable, Sendable {/// The Mac that said it, by device id — its page is where the notice opens.publiclet deviceId: String/// The Mac's own name off the device rows; nil when the row is not here.publiclet macName: String?/// Since when nothing got through (ISO-8601); nil when the Mac does not know.publiclet since: String?/// The Mac's own sentence for why — kept for the Mac's page, never said/// on Home: a script path or a relay's error is no news to a person.publiclet reason: String?/// The last copy that got through (ISO-8601); nil when none ever did.publicvar lastAt: String? = nil/// Why nothing is copied: the copy stopped getting through, or the/// backup is only half set up.publicvar kind: Kind = .failing/// The backup's one line, resolved by the core (`records::backup`) — the/// same Feedback and the Mac's page say; nil when the core said none.publicvar line: String? = nilpublicenumKind: Equatable, Sendable {case failingcase halfSetUp }/// What one `read.servingBackup` answer means for the board.publicenumReading: Equatable, Sendable {/// A Mac answered: its notice, or nil when its backup is getting through.case answered(BackupNotice?)/// No Mac answered — nothing new to say either way.case unanswered }/// Read one answer. Only an answer a Mac gave (`this_mac`, `serving_mac`)/// says anything; a database of that Mac's failing is enough, and however/// many fail it is still the one Mac's notice.publicstaticfunc read(_ answer: JSONValue, now: Date = Date()) -> Reading {guardlet answered = DeviceBackup.answered(answer),let deviceId = answer.untrimmed("deviceId"), !deviceId.isEmptyelse { return .unanswered }let whole = answered.backupiflet last = whole.lastAt.flatMap({ CottonDate.parse($0) }), now.timeIntervalSince(last) < staleAfter {return .answered(nil) }let databases = (answer["backup"]?.list("databases") ?? []).compactMap { DeviceBackup(line: $0) }let name = answer.text("deviceName")let failing = databases.filter { $0.state == .failing }guard whole.state == .failing || !failing.isEmpty else {guard whole.isHalfSetUp else { return .answered(nil) }return .answered(BackupNotice(deviceId: deviceId, macName: name, since: nil, reason: whole.lastError, kind: .halfSetUp, line: answer.text("backupLine"))) }let since = whole.since ?? failing.compactMap(\.since).min { earlier($0, $1) }let reason = (whole.state == .failing ? whole.lastError : nil) ?? failing.lazy.compactMap(\.lastError).firstreturn .answered(BackupNotice(deviceId: deviceId, macName: name, since: since, reason: reason, lastAt: whole.lastAt, line: answer.text("backupLine"))) }/// How old the last copy that got through must be before Home says so.publicstaticlet staleAfter: TimeInterval = 24 * 60 * 60privatestaticfunc earlier(_ one: String, _ other: String) -> Bool {switch (CottonDate.parse(one), CottonDate.parse(other)) {caselet (a?, b?): return a < bcase (.some, nil): returntruedefault: returnfalse } }}/// The notice's label over its sentence.enumBackupNoticeWords {staticvar label: String { ShellLines.line("board", "backup.label") }}/// The backup receipt's words, as the board's `notices` part sets them/// (`backup_behind` with `{device}` and `{behind}`, `backup_unset`,/// `backup_open`; resolved by the core, `board::default_of`). The core fills/// the line (`records::backup`); the app draws it and the button's word.publicstructBackupWords: Hashable, Sendable, Codable {publiclet behind: Stringpubliclet unset: Stringpublicletopen: String}