// A knock from the share extension, owed until the file is open. // // The extension stages the bytes, writes the mailbox entry and opens // `tinmen-cotton:///messages/import?share=` (`CottonMailbox/ShareHandover.swift` // says why only a chat export knocks). **What arrives at the app is therefore // not navigation — it is a reason to DRAIN.** The mailbox is the fact; the URL // only says a new one is in it. // // ── Why this is not `PendingTaps` ──────────────────────────────────────────── // // `PendingTaps` holds a ROUTE until there is a stack to push it onto, and // replays it. A share cannot use it: pushing `/messages/import` from the URL // and ALSO draining would open the import screen twice — once bare from the // URL, once carrying the staged file from the drain — and the person would be // looking at the empty one. The two paths have to become one, and the one that // survives is the drain's, because it is the only one holding the file. // // So the URL's whole effect is `arrived()`, and the only question left is WHEN // the drain may run: every branch of it either writes a row or opens a screen, // and neither is possible before a file is open (`useReaderIntake.ts:71`'s // `if (!workspaceId) return`, which the gate answers here). A knock that lands // during the boot read — the ordinary case, because opening the app IS the cold // launch — is owed, and the gate's `.ready` pays it. // // ── ONE DRAIN PER FOREGROUND ───────────────────────────────────────────────── // // The mailbox is emptied whole by whoever drains it first // (`ShareMailbox.drain`), so for a share that FILES, a second drain in the same // breath finds nothing and costs nothing. That was the whole argument, and it // was wrong about the case that matters. // // **A share that is REFUSED is put back**, once, for one more try // (`ShareMailbox.reoffer`). A knock and the `.active` foreground both drained on // the one arrival, so the walk of 15 Sep 2026 read the two log lines back to // back — "kept for one more try", then "not imported twice, discarded" — and // the bytes were gone, in the same second, without one single thing having // changed between the two attempts. A retry that does not wait for anything is // not a retry. // // So this object is now the coalescer as well as the owed flag: **every drain // in the app goes through it, and a foreground gets exactly one.** Whichever // trigger asks first — the cold-launch task, the gate's `.ready`, the knock, the // `.active` edge — takes that foreground's drain, and the rest find it spent. // The retry then happens on a genuinely later foreground. // // **The count moves when the app LEAVES the foreground**, not when it arrives, // and that is deliberate: `onOpenURL` and the `.active` edge are not ordered // against each other by iOS, so a counter that moved on the way IN would let // whichever arrived first drain under the old number and the other under the // new one — which is the bug, rebuilt. Leaving is unambiguous and happens once. import CottonKit import Foundation @MainActor @Observable public final class ShareArrivals { private var owed = false /// Which foreground the app is in. It starts at 1 because a cold launch IS /// a foreground and never gets an `.active` EDGE to count. private var foreground = 1 /// The foreground whose drain has been taken, if any. private var drainedForeground: Int? public init() {} /// The share extension opened Cotton after staging a share. public func arrived() { owed = true } /// The app left the foreground. The next drain is a new one. public func wentAway() { foreground += 1 } /// Take THIS foreground's drain. `false` when it has already been taken. /// /// A knock that lands after the drain has run is not lost: it is simply not /// a second drain, because the mailbox it would read is the one that was /// just read, and the only entry that could still be in it is a refusal /// waiting for a later try. public func takeDrain() -> Bool { guard drainedForeground != foreground else { return false } drainedForeground = foreground owed = false return true } /// Take the drain a knock OWES — nothing when no knock has landed, and /// nothing when this foreground has already drained. /// /// Idempotent by construction: two knocks before the gate resolves are one /// drain, which is right, because one drain takes the whole mailbox. public func takeOwedDrain() -> Bool { guard owed else { return false } return takeDrain() } /// Whether a drain is still owed. For the test, and for nothing else. public var isOwed: Bool { owed } /// Whether this foreground has spent its drain. For the test, and for /// nothing else. public var hasDrained: Bool { drainedForeground == foreground } }