// The one place a refused phone's writes are turned away.//// A server that has REFUSED this device — `device_not_approved`,// `device_revoked`, `device_bad_signature` — used to replace the whole app// with a refusal screen, and the person's full copy sat unreadable behind it.// The copy is theirs and it is whole, so the app now opens on it READ-ONLY:// every `read.*` is local and answers as it always did; what cannot happen is// a write, because a row filed on a phone the server no longer admits is a// row that reaches no other device and may never.//// **Seventy-five callers ask `write` directly**, so the refusal is not a flag// each screen checks — it is a wrapper over the core every model is handed,// and `write` is the one method it turns away. Everything else passes:// `join.*` (the way back in), `sync.*`, `read.*`, the media path, the// terminal frames. A refused write answers the same envelope the crate would,// so `ScreenWriter` shows the sentence in the place it shows every refusal// and nothing anywhere has to know the gate exists.//// The gate is a fact `BootModel` owns (`readOnly`) and `RootView` copies// down on every gate transition; the wrapper reads it on every call, from// whichever thread the call arrives on.importFoundation/// Why writes are refused right now, or `nil` when they are not.////// A class with a lock rather than an actor: `callRaw` is synchronous on the/// real core and the wrapper must answer on the same thread it was asked on.publicfinalclassWriteGate: @unchecked Sendable {privatelet lock = NSLock()privatevar sentence: String?publicinit() {}/// Hold writes with this sentence, or let them through with `nil`.publicfunc hold(_ sentence: String?) { lock.lock()defer { lock.unlock() }self.sentence = sentence }/// The sentence a write is refused with right now, or `nil`.publicvar refusal: String? { lock.lock()defer { lock.unlock() }return sentence }/// The methods the gate turns away. ONE: the tool path every screen/// writes through. The phone's own acts — leaving, erasing, joining/// again — must still work on a refused phone; they are the way out.publicstaticlet refusedMethods: Set<String> = [CottonMethod.write]/// The refusal code a held write answers with. A screen keys on/// `message`, which is the sentence; the code is for a log.publicstaticlet code = "read_only"}/// A `CottonCalling` over another, refusing writes while the gate is held.publicstructGatedCore: CottonCalling {privatelet core: any CottonCallingprivatelet gate: WriteGatepublicinit(over core: any CottonCalling, gate: WriteGate) {self.core = coreself.gate = gate }publicfunc callRaw(_ method: String, requestJSON: String) asyncthrows -> String {ifWriteGate.refusedMethods.contains(method), let sentence = gate.refusal {returnGatedCore.refusalEnvelope(sentence) }returntryawait core.callRaw(method, requestJSON: requestJSON) }publicfunc subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) async {await core.subscribe(handler) }publicfunc close() async {await core.close() }/// The crate's own envelope shape, so `CottonCalling.call` throws the/// same `CottonCallError` a refusal from below the boundary would./// The sentence is the gate's own (`gate.write_refused`, a line row);/// strings always encode, and an answer that somehow did not is the/// malformed answer `call` already says, not a second sentence here.staticfunc refusalEnvelope(_ sentence: String) -> String {structEnvelope: Encodable {var ok = falselet error: [String: String] }let envelope = Envelope(error: ["code": WriteGate.code, "message": sentence])let encoder = JSONEncoder() encoder.outputFormatting = .sortedKeysreturnString(decoding: (try? encoder.encode(envelope)) ?? Data(), as: UTF8.self) }}