// The client half of recurring-todo materialization.//// One pass mounted at the root: on sign-in and on every foreground, throttled,// give each template what the planner says it is owed. The core mints only// where this device is the file's writer of record — a phone holding a// database no Mac serves yet; a copy a Mac serves gets its occurrences from// that Mac's hourly tick and the sync, and this pass answers `mints: false`// (`recurrence::mints_occurrences`; coordinator's decision (b), 295 C).//// The two can only ever race toward the same rows, because the occurrence's id// is DERIVED from (recurrence, occurrence_at): both sides compute the same id// and the second write is a plain last-write-wins upsert. It is NOT "the unique// constraint absorbs it" — on a file the replication unit is a row id, and two// minted ids for one occurrence would be two INSERTs, the second of which the// constraint refuses.//// It also CLAIMS THE WORKSPACE ZONE on its first run if nothing has. Without a// zone the server rests rather than guess (the host runs `TZ=UTC`), so this// pass is what turns the background half back on — and it sends the DEVICE's// zone, which `materialize_todo_recurrences` writes only when there is no// settings row. A recorded zone is never overwritten by a phone that travelled.importCottonKitimportFoundation/// The throttle, in seconds. Five minutes: a foreground is cheap, but a person/// switching apps every few seconds should not make the planner run every few/// seconds.privatelet materializeThrottle: TimeInterval = 5 * 60@MainActor @ObservablepublicfinalclassRecurrenceMaterializer {privatelet core: any CottonCalling/// The device's own zone, read once per pass rather than stored: a phone/// that lands somewhere else is a phone whose next foreground says so.privatelet zone: () -> Stringprivatevar lastRunAt: Date?/// What the last pass minted, so a screen could say so. Nothing draws it/// today — the write's own `tables` invalidates the todo reads — but a pass/// that answered nothing and a pass that never ran are different states and/// the model keeps them apart.publicprivate(set) var created: Int?publicinit(core: any CottonCalling, zone: @escaping () -> String = { TimeZone.current.identifier }) {self.core = coreself.zone = zone }/// The new file has not had a pass, even when the old one just did.func copyReplaced() async { lastRunAt = nil created = nilawait run() }/// Run a pass unless one ran inside the throttle window.////// A failure is SWALLOWED on purpose and the throttle still moves: this is/// a background repair, the person did not ask for it, and a materialiser/// that could not reach the file must not put an error in front of somebody/// who opened the app to read their day. The tool's own answer is the/// record of what happened.publicfunc run(now: Date = Date()) async {iflet last = lastRunAt, now.timeIntervalSince(last) < materializeThrottle { return } lastRunAt = nowlet request = WriteRequest( tool: DayTool.materializeTodoRecurrences, args: .object(["timezone": .string(zone())]) )guardlet answer = try? await core.call(CottonMethod.write, request, as: WriteAnswer.self) else { return }guardlet tree = answer.answer, case .object(let fields) = tree else { return }guardlet minted = fields["created"], case .number(let count) = minted else { return } created = Int(count) }}