// 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. import CottonKit import Foundation /// 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. private let materializeThrottle: TimeInterval = 5 * 60 @MainActor @Observable public final class RecurrenceMaterializer { private let 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. private let zone: () -> String private var 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. public private(set) var created: Int? public init(core: any CottonCalling, zone: @escaping () -> String = { TimeZone.current.identifier }) { self.core = core self.zone = zone } /// The new file has not had a pass, even when the old one just did. func copyReplaced() async { lastRunAt = nil created = nil await 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. public func run(now: Date = Date()) async { if let last = lastRunAt, now.timeIntervalSince(last) < materializeThrottle { return } lastRunAt = now let request = WriteRequest( tool: DayTool.materializeTodoRecurrences, args: .object(["timezone": .string(zone())]) ) guard let answer = try? await core.call(CottonMethod.write, request, as: WriteAnswer.self) else { return } guard let tree = answer.answer, case .object(let fields) = tree else { return } guard let minted = fields["created"], case .number(let count) = minted else { return } created = Int(count) } }