Cotton code
171 lines · 8.0 KB · SwiftRaw
// The contract for work and reference — sub-step 4f's half of `Boundary.swift`.//// A SEPARATE FILE for the reason `Boundary+4d.swift`, `Boundary+4e.swift` and// `Boundary+4i.swift` all give in their headers: several lanes are landing at// once, and a lane that appends to the middle of somebody else's file makes a// merge conflict out of what are really two independent additions. The rule// stated in `Boundary.swift` still binds every shape below — **where this file// and `crates/cotton-ffi/src/phone/reads.rs` disagree, the// crate wins** — and `BoundaryContractTests` reads that file by path so the two// cannot drift quietly.//// WHY SO FEW NAMES for a lane of fifty-eight routes. Almost every read this// lane makes goes through `ScreenRead(read:args:)`, which builds its method as// `"read.\(name)"` — one call site, one shape, and the name arrives as data. So// the names are held crate-side instead, where the whole set can be checked at// once: `READS ∪ DEFERRED ∪ READ_LESS = ROUTES` is exact and disjoint// (`reads_tests::every_screen_is_answered_or_deferred_by_name`), and// `reads_work_tests::refusal_is_about_the_record` holds each read's ARGUMENTS// against the screen that passes them.//// The document viewer's literal, `read.mediaPath`, is NOT here: 4i already// declares it in `Boundary+4i.swift` and the list in `BoundaryContractTests`// already carries it. One name, one declaration.extension CottonMethod {    /// `read.sessions` — the Macs, asked by the two places that are not the    /// sessions list: Settings › Code (the per-Mac cap and pause) and the    /// todo's hand-to-agent sheet (the Mac picker). `list_devices`' answer    /// reaches the phone through this read and nowhere else, and a second    /// spelling of "which Macs are there" is what naming it here avoids.    public static let readSessions = "read.sessions"    /// `read.session {id}` — one session however old, with its turns, its    /// messages, its task list and the decision waiting for the Mac: what the    /// session table's page and its conversation read    /// (`SessionTableSource`, `SessionMessageTableSource`).    public static let readSession = "read.session"    /// `read.projectSessions {slug, limit}` — one project's sessions of all    /// time, newest first: what the session table's list reads when it is    /// narrowed to one project (`SessionTableSource`).    public static let readProjectSessions = "read.projectSessions"}// ── Starting a session from the phone ───────────────────────────────────────//// TWO METHODS, not tools: a launch row is// the OWNER's own request to a machine, written through the phone's outbox// under the owner's byline. The MCP verbs on the other side can only claim a// launch and close it — no assistant can create one — so there is no tool to// call here and the phone writes it as a method.//// The shapes below are `crates/cotton-ffi/src/phone/session_queue.rs`'s. Both are// `Encodable` with Optional fields, and a synthesised `Encodable` omits a nil// Optional rather than writing `null`, so an argument the person did not choose// never reaches the method at all — which is what lets the crate read "no// account named" and "no project named" as absence rather than as a null it has// to spell a meaning for./// `session.create`'s request.////// EVERY CHOICE IS OPTIONAL BUT THE PROMPT. No `deviceId` means ANY MAC — the/// dispatcher picks one and writes the sentence on the row when it cannot; no/// `account` means whichever login on that Mac takes it; no `model` means the/// agent's own default; no `title` means the prompt's first line names the run./// No `permissionMode` means the HOUSEHOLD's own default, which is a setting/// rather than a word written here/// ([sessions-start-in-bypass-permissions-mode-by-default-it-s-a-sett]).////// `prompt` is required and MAY be empty: an empty first prompt is a session/// that comes up listening for an instruction.////// **ONE OBJECT (schema v23).** What this creates is the `cotton_sessions` row/// the session then runs and ends on — there is no launch to join it to.public struct SessionCreateRequest: Encodable, Sendable {    /// The DEVICE the work is placed on — a key the owner admitted. `nil` is    /// an ANSWER and not an absence: it means any Mac.    public var deviceId: String?    /// The todo this session was handed. Set by "Hand to an agent"; the session    /// carries the link and nothing changes on the todo row.    public var todoId: String?    /// Which agent: `claude-code` or `codex`.    public var tool: String?    /// `bypass` or `ask`. Absent means the household's own default.    public var permissionMode: String?    public var account: String?    /// The project's SLUG, which is what a picker holds — the crate resolves it    /// to the row and takes that project's first repository as the checkout.    public var projectSlug: String?    public var model: String?    public var title: String?    public let prompt: String    /// A worktree to create off the trunk before the session starts. The phone    /// has no control for it today — the form names a project, and a project's    /// repository is what the Mac checks out — but the column and the method    /// both take it, so it is declared rather than silently dropped.    public var branch: String?    public init(        deviceId: String? = nil,        todoId: String? = nil,        tool: String? = nil,        permissionMode: String? = nil,        account: String? = nil,        projectSlug: String? = nil,        model: String? = nil,        title: String? = nil,        prompt: String,        branch: String? = nil    ) {        self.deviceId = deviceId        self.todoId = todoId        self.tool = tool        self.permissionMode = permissionMode        self.account = account        self.projectSlug = projectSlug        self.model = model        self.title = title        self.prompt = prompt        self.branch = branch    }}/// `session.withdraw`'s request — the owner taking back a session no Mac has/// started. Refused with a sentence once an agent is behind it: by then the/// work is a running process on its Mac.public struct SessionWithdrawRequest: Encodable, Sendable {    public let id: String    public init(id: String) {        self.id = id    }}/// `session.resume`'s request — the owner carrying a session on where it/// stopped.////// It re-queues THE SAME ROW on the Mac it ran on and keeps the agent's own/// session id, so the agent is asked to continue rather than to start: the/// transcript is on that one machine and nothing syncs it. A row that FAILED/// before any agent ran (no folder, no clone, no login) is restarted instead —/// queued afresh for any Mac, nothing to continue and nothing lost. Refused/// with a sentence for a session that is still going, one Cotton did not/// start, an ended one that never reached an agent, or one whose Mac is not/// recorded.public struct SessionResumeRequest: Encodable, Sendable {    public let sessionId: String    public init(sessionId: String) {        self.sessionId = sessionId    }}/// `project.conversation`'s request — the project's one standing conversation.////// **It ENSURES rather than creates**: the method answers the existing row or/// opens one `queued` on any Mac, and the conversation's id is derived from/// the workspace and the project, so two devices asking in the same minute/// arrive at one row (Cotton note "The project conversation — one standing thread per project"). The project/// page therefore asks once, before its first message, and keeps the id.////// By SLUG, as every project method on this phone is addressed: the slug is/// the page's own route parameter and always in hand.public struct ProjectConversationRequest: Encodable, Sendable {    public let slug: String    public init(slug: String) {        self.slug = slug    }}