Cotton code
131 lines · 6.6 KB · SwiftRaw
// A read the core cut (architect's rulings 21:00 (4) and 22:03 IST 16 Oct// 2026): the read pool was busy, or the read ran past its deadline. The core// answers the code `could_not_read` with `details.why` (`busy`, `too_long`);// the app draws the database definition's typed line for it// (`words.could_not_read`), never the core's sentence. A read whose transport// never reached the serving Mac answers `mac_away` the same way, so no// transport text — an address, a resolver's words — reaches a page// (architect's offline-truth ruling, 17 Oct 2026). Any other read that// failed — another code, or an error that is no refusal at all — says the// definition's `other` line (walker 294 row G): this file is the one way a// caught read error becomes visible text, and the error's own words// (`message`, `localizedDescription`) go only to the log. A read whose facts// live only on the serving Mac (AI usage) answers `on_the_mac` at once while// the Mac is not heard, with the Mac's `heard_at` and the table: the line// says the Mac's last-heard age (the `ago` format's spelling, as Copies says// "Mac heard …") and the table's plural (295 rule 15, walker294). A read// that met a table mid-move — its computed index being rebuilt in the// background after the open — answers `moving` with the table and the// database: the line says the table's plural and no version, offers no Try// again, and the screen reads again on the move's done signal (the `data`// event of origin `ClientEvent.movesDoneOrigin`), never on a timer// (passivemove rule 3).import CottonKitimport Foundationenum CouldNotRead: Equatable, Sendable {    case busy    case tooLong    case macAway    /// The read's facts live only on the serving Mac and it is not heard    /// now: when it was last heard (an ISO instant; nil, never) and the table.    case onTheMac(heardAt: String?, table: String?)    /// The read met `table` mid-move (its background move is not done): it    /// shows by itself on the move's done signal.    case moving(table: String?)    /// Any other read that failed: its own words never reach the page.    case other    /// Why the core cut the read; nil for any other error.    static func of(_ error: Error) -> CouldNotRead? {        guard let error = error as? CottonCallError, error.code == "could_not_read" else { return nil }        switch error.details?["why"]?.string {        case "too_long": return .tooLong        case "mac_away": return .macAway        case "on_the_mac":            // How long ago, as the core said it (`@day_parts`).            return .onTheMac(heardAt: error.details?["@day_parts"]?["heard_at"]?["ago"]?.string, table: error.details?["table"]?.string)        case "moving": return .moving(table: error.details?["table"]?.string)        default: return .busy        }    }    /// What any caught read error says: the core's cut, else `other`.    static func reading(_ error: Error) -> CouldNotRead { of(error) ?? .other }    /// The one way a caught read error becomes visible text: the definition's    /// line for it, filled with the database's name. Never the error's own    /// words — those are logged here and nowhere drawn.    static func line(for error: Error, database: String = CouldNotReadWords.unnamed,                     definitions: TableDefinitions = TableDefinitions([]), now: Date = Date()) -> String {        let why = reading(error)        if why == .other { Log.write("read failed — \(error)") }        return why.line(database: database, definitions: definitions, now: now)    }    /// Whether Home and the lists keep trying by themselves: a read the core    /// cut does; any other failure offers Try again.    var keepsTrying: Bool { self != .other }    /// Whether the read waits on a background move's done signal rather    /// than a backoff: a table mid-move draws itself when the move is done.    var waitsForMove: Bool {        if case .moving = self { return true }        return false    }    /// The line, filled with the database's name: a line of the shell's    /// `reading` set (`could_not_read.*`). These words lived on the    /// `database` definition until that definition was deleted (13e1775e7),    /// and every failed read then said nothing at all.    func line(database: String, definitions: TableDefinitions, now: Date = Date()) -> String {        let said = { (path: String, fills: [String: String]) in            ShellLines.line("reading", "could_not_read.\(path)", fills.merging(["database": database]) { mine, _ in mine })        }        switch self {        case .busy: return said("busy", [:])        case .tooLong: return said("too_long", [:])        case .macAway: return said("mac_away", [:])        case .onTheMac(let heardAt, let table):            // A Mac never heard, or heard past what the age says, says            // `.macAway`'s line: the age is the line's one fact.            guard let heard = heardAt else {                return CouldNotRead.macAway.line(database: database, definitions: definitions, now: now)            }            return said("on_the_mac", ["heard": heard, "what": definitions[table]?.plural ?? database])        case .moving(let table):            return said("moving", ["what": table.flatMap { definitions[$0] }?.plural ?? database])        case .other: return said("other", [:])        }    }}extension CouldNotRead {    /// Whether a refusal is said over a list holding `drawn` records. A busy    /// file over rows already drawn is a re-read that will land on its own:    /// the rows stand and nothing is said (walker294 row 116: "Personal is    /// busy on this phone" over a Copies list that was there). A list with    /// nothing drawn, and every other refusal, says it.    static func isSaid(_ stall: CouldNotRead?, over drawn: Int) -> Bool {        !(stall == .busy && drawn > 0)    }    /// What a read refusal says where it is drawn: the typed line for a read    /// the core cut, filled with the shown database's name; else the refusal's    /// own words.    @MainActor    static func said(        _ stall: CouldNotRead?, otherwise problem: String?, scene: DatabaseScene?, databases: DatabasesModel?,        definitions: TableDefinitions    ) -> String? {        guard let stall else { return problem }        let name = databases?.database(scene?.shownId)?.name ?? CouldNotReadWords.unnamed        return stall.line(database: name, definitions: definitions)    }}enum CouldNotReadWords {    /// The subject when the shown database is not known yet.    static var unnamed: String { ShellLines.line("record", "could_not_read.unnamed") }}