// The routing story, as data. // // Pure, and for a reason: a row's whole reading is settled by a table rather // than by rendering. // // Moved beside the capture's context when Thoughts became its table's list // and page: `CaptureContext` reads `routedRefs`, and the rest is the reading // the engine's read-only thought columns (`became`, `story`, `why`) answer in // the same words. // // FOUR DISTINCTIONS THIS FILE EXISTS TO KEEP, each of which a shorter reading // erased once: // // • **filed vs corrected.** Every human filing lands as `corrected`, because // that is the status the router reads back as training signal. Two very // different acts share it: a capture the person MOVED off a confident // mis-filing was corrected; one they placed by hand because the router // abstained was never filed before. `routeConfidence` separates them // exactly — the router only ever writes the column at or above the // threshold, and no human path touches it. // • **filing vs a proposal.** `pending` with no reason is still being read; // `pending` WITH one is a proposal waiting for an answer. // • **routed-into-nothing vs waiting.** A routed capture with no record under // it stayed a thought (v29). Before that it fell through to "still waiting // to be filed", which was a lie about a capture nothing more was going to // happen to. // • **the sweep's sentence vs the filed caption.** A sweep refile's own // plain-English note REPLACES the caption outright rather than sitting // beside it: the records listed already show the new outcome. import CottonKit import Foundation /// The line the router files at or above, and no human path ever writes. /// `crates/cotton-core/src/rules/routing.rs`'s own constant; there is deliberately /// no second number. public let confidenceThreshold = 0.7 /// Whose decision put this capture where it is. public enum FilingHand: String, Equatable, Sendable { case router case userMoved case userFiled } /// What happened to one capture — the whole outcome, as one state a screen /// draws without re-deriving anything. public enum ThoughtOutcome: Equatable, Sendable { /// An optimistic row the outbox has not confirmed — its id is still the /// client-minted one. case sending /// `pending`, and the classifier has not answered yet. case filing /// `pending` with a verdict: the below-confidence proposal. case proposal /// `routed` / `corrected`: one chip per routed record, plus the caption. case filed(records: [RoutedRef], caption: String) /// `routed` with NOTHING under it — the router looked and confidently found /// no record in it, so the thought stands alone. Not `filed` with an empty /// list, which would draw a "became" that names nothing. case thought /// `dismissed`. case kept } /// One record a capture became. public struct RoutedRef: Equatable, Sendable { public let kind: String public let id: String public init(kind: String, id: String) { self.kind = kind self.id = id } /// The badge kind, where the string names one this app can draw. public var badge: BadgeKind? { BadgeKind(rawValue: kind) } } public enum ThoughtStory { /// An optimistic row's id, minted on this device before the outbox confirms /// it — the app's own convention, not the file's. static let tempIdPrefix = "temp-" // ── Whose hand filed it ─────────────────────────────────────────────── public static func filingHand(_ thought: JSONValue) -> FilingHand { guard thought.text("routeStatus") == "corrected" else { return .router } guard let confidence = thought.decimal("routeConfidence") else { return .userFiled } return confidence >= confidenceThreshold ? .userMoved : .userFiled } // ── What it became ──────────────────────────────────────────────────── /// `becameOf`'s records-preferred, pointer-fallback merge — the same one /// the read's own `routed_refs` makes, so a bubble and the facts keyed /// beside it can never disagree about which records exist. public static func routedRefs(_ thought: JSONValue) -> [RoutedRef] { let listed: [RoutedRef] = thought.list("routedRecords").compactMap { entry in guard let kind = entry["kind"]?.string, let id = entry["id"]?.string else { return nil } return RoutedRef(kind: kind, id: id) } if !listed.isEmpty { return listed } guard let kind = thought.text("routedKind"), let id = thought.text("routedId") else { return [] } return [RoutedRef(kind: kind, id: id)] } // ── The plan a proposal is holding ──────────────────────────────────── /// A target's kind, where it names one this app can draw. public static func targetKind(_ target: JSONValue) -> BadgeKind? { guard let kind = target["kind"]?.string else { return nil } return BadgeKind(rawValue: kind) } // ── The caption under a filed bubble ────────────────────────────────── /// The glue between the facts a filed row states. A separator, not a /// sentence. static let summarySeparator = " \u{00B7} " /// ONE line under a filed capture, stating every fact the ledger knows. public static func filedSummary( kind: BadgeKind?, destinationName: String? = nil, dismissed: Bool = false, corrected: Bool = false, spoken: Bool = false, calendar: Calendar = .current ) -> String { var parts: [String] = [] if spoken { parts.append("Spoken") } if dismissed { parts.append("dismissed") return parts.joined(separator: summarySeparator) } if let kind { let name = destinationName?.trimmingCharacters(in: .whitespacesAndNewlines) if kind == .personNote, let name, !name.isEmpty { parts.append(CottonStrings.noteOnPerson(name)) } else { parts.append(kind.label) } } // Only where the person MOVED a confident mis-filing. Filing something // the router abstained on corrected nothing, so it never says this. if corrected { parts.append("corrected") } return parts.joined(separator: summarySeparator) } // ── Why the router stopped ──────────────────────────────────────────── // ── The outcome ─────────────────────────────────────────────────────── public static func outcome(_ thought: JSONValue) -> ThoughtOutcome { if id(thought).hasPrefix(tempIdPrefix) { return .sending } let status = thought.text("routeStatus") if status == "dismissed" { return .kept } if status == "pending" { return thought.text("routeReason") == nil ? .filing : .proposal } let records = routedRefs(thought) if status == "routed" && records.isEmpty { return .thought } // The sweep's own sentence REPLACES the filed caption; otherwise the // caption carries only the correction story — the kind, the destination // and the date all live on the chips. let caption = thought.text("sweepNote") ?? filedSummary(kind: nil, corrected: filingHand(thought) == .userMoved) return .filed(records: records, caption: caption) } /// A capture's own id, which every ledger row carries. A row that somehow /// arrived without one is keyed as the empty string rather than dropped, /// so the page still draws the words the person wrote. public static func id(_ thought: JSONValue) -> String { thought.text("id") ?? "" } /// The capture's words: its own, untrimmed. A spoken capture whose /// transcript is empty says so rather than drawing a blank slip. public static func words(_ thought: JSONValue) -> String { if let text = thought.untrimmed("rawText"), !text.isEmpty { return text } if thought.untrimmed("audioUrl")?.isEmpty == false { return ShellLines.line("capture", "story.spoken") } return "" } }