Cotton code
166 lines · 7.8 KB · SwiftRaw
// The two lists the composer's sigils complete against.//// Spec 2026-08-29 §D4 — and the plan chips, which name the router's project// proposal from the same list.//// SIX screens used to write this out by hand: Home, the captures list,// a capture's own page, the person page, the tag page and the project page. Six// copies of "active only, id + name" is six chances for one of them to start// offering an archived project, and the bug would look like the composer// arguing with the project list. So the shaping lives in ONE place, and that// part of the hook ports unchanged:////   • **Only ACTIVE projects.** An archived project is a place you have stopped//     filing into; completing to one would invite a link nobody wants.//   • **People are offered whole** — a person is not archived out of a//     conversation.//   • **Never a missing list.** Both are `[]` before anything has landed: the//     matcher and the chip resolver read them on the first frame.//// ## THE SEAM: where the rows come from, and why it is not a read yet//// The hook that came before owned its own reads, because a query cache makes// one entry shared by every caller. There is no such cache here, and the// crate's own read table cannot answer this question yet. The model therefore// takes both lists FROM THE HOST, and this is the reason — stated here rather// than left as a shape somebody has to reverse-engineer:////   • `read.projects` exists (`crates/cotton-ffi/src/phone///     reads.rs`, `ReadSource::Tool("list_projects")`) but its answer carries//     **no `id`** — `project_out` emits `slug`, `name`, `status` and the//     summary provenance, and nothing else. A `#` chip needs the ID: the//     capture write says so in as many words ("Ids, never slugs — a slug is a//     name the person can change"), so a vocabulary built from that read would//     draw chips this app cannot then send. It also returns ARCHIVED projects,//     which is this file's rule to apply rather than the tool's.//   • `read.people` exists (`ReadSource::Tool("list_people")`) and does answer//     `id` + `name` — but it PAGES, at a default limit of 50, so a directory//     larger than that would silently stop completing names partway through the//     alphabet. That is the exact failure the hook's own `error` field was//     written to make visible.//   • The one read that DOES answer `#`'s vocabulary correctly today is//     `read.home`: its `projects` list is active-only, in activity order, with//     the id and the name. `supply(homeProjects:people:)` below is the bridge//     for a host that already holds it.//// So: the shaping is here and the reading is the host's, until either the// crate's `list_projects` answers an id or a `read.dockVocabulary` is declared// beside the others. Inventing a read this app could call and nothing answers// would be an `unknown_method` at runtime — the boundary's own header names// that as the failure mode, and it reads on screen as a composer that quietly// stops completing names.import CottonKitimport Foundation/// A project as a host holds it, before this file has decided anything about/// it. The `status` is the row's own string, so the active rule is applied HERE/// and not by six callers.public struct VocabularyProject: Sendable, Equatable {    public let id: String    public let name: String    public let status: String    /// The address a project page is opened at — how the Mac's open page is    /// told apart from the others, since a route names a SLUG and a chip an id.    public let slug: String?    public init(id: String, name: String, status: String, slug: String? = nil) {        self.id = id        self.name = name        self.status = status        self.slug = slug    }    public var isActive: Bool { status == VocabularyProject.activeStatus }    /// The status a project must wear to be completable.    ///    /// Declared on the VALUE rather than on the model: the model is    /// `@MainActor`, and the rule has to be decidable from anywhere — including    /// from a `Sendable` value like this one, which is not on any actor.    public static let activeStatus = "active"}/// A person as a host holds them. `name` is the FULL name — that is what `@`/// completes against, and `fullName` maps onto it for the same reason.public struct VocabularyPerson: Sendable, Equatable {    public let id: String    public let name: String    public init(id: String, name: String) {        self.id = id        self.name = name    }}/// The `#` vocabulary: ACTIVE projects, in the order the host handed them.////// A free function so the rule is decidable without a model, an actor or a/// frame — it is the one line of the hook that a wrong copy would have broken.public func dockProjectVocabulary(_ rows: [VocabularyProject]) -> [SigilCandidate] {    rows.filter(\.isActive).map { SigilCandidate(id: $0.id, name: $0.name) }}/// The `@` vocabulary: everyone, whole.public func dockPeopleVocabulary(_ rows: [VocabularyPerson]) -> [SigilCandidate] {    rows.map { SigilCandidate(id: $0.id, name: $0.name) }}/// The composer's two vocabularies, held for as long as the dock is on screen.////// `@Observable` rather than a struct a host passes down every draw: the dock/// re-reads these on every keystroke, and a list that is a new array each time/// would defeat the matcher's own work. The identity of both lists is held/// stable for exactly this reason.@MainActor@Observablepublic final class DockVocabulary {    /// The status a project must wear to be completable — the value's own, so    /// there is one spelling of it.    public static let activeStatus = VocabularyProject.activeStatus    /// Active projects — what `#` reaches.    public private(set) var projects: [SigilCandidate] = []    /// Everyone in the directory — what `@` reaches.    public private(set) var people: [SigilCandidate] = []    /// The active projects' slugs, by id: the project pill's cap colour, and    /// the way an open project page (a route, which names a slug) is found.    public private(set) var projectSlugs: [String: String] = [:]    public init() {}    /// Hand the model what a host has read.    public func supply(projects: [VocabularyProject], people: [VocabularyPerson]) {        self.projects = dockProjectVocabulary(projects)        projectSlugs = Dictionary(            projects.filter(\.isActive).compactMap { row in row.slug.map { (row.id, $0) } },            uniquingKeysWith: { first, _ in first }        )        self.people = dockPeopleVocabulary(people)    }    /// The bridge for a host that already holds `read.home`'s answer.    ///    /// `read.home`'s `projects` is ACTIVE-only by the time the crate answers it, so    /// there is nothing left here to filter — which is why this takes the    /// candidates straight rather than routing them through    /// `dockProjectVocabulary`, whose whole job is the filter.    public func supply(homeProjects: [JSONValue], people: [VocabularyPerson]) {        projects = homeProjects.map { SigilCandidate(id: $0.text("id") ?? "", name: $0.text("name") ?? "") }        projectSlugs = Dictionary(            homeProjects.compactMap { row in                guard let id = row.text("id"), let slug = row.text("slug") else { return nil }                return (id, slug)            },            uniquingKeysWith: { first, _ in first }        )        self.people = dockPeopleVocabulary(people)    }    /// The active project a page's slug names — `nil` for an archived one or    /// one this vocabulary has not heard of, which then arms nothing.    public func project(slug: String) -> SigilCandidate? {        guard let id = projectSlugs.first(where: { $0.value == slug })?.key else { return nil }        return projects.first { $0.id == id }    }}