// The terminal's four calls and two events, spelled ONCE, on the wire the core // actually decodes. // // **This file exists because the two halves of an FFI method were written from // the design page separately and never met.** `crates/cotton-ffi/src/phone/ // terminal.rs` decodes `{session, bytes, cols, rows}`; the pane sent // `{sessionId, data}` with no size at all, so every `terminal.attach` came back // `bad_request` and the Terminal tab drew "No terminal for this session" on a // session that was running perfectly well. Both sides were tested. Neither test // could see the other (Cotton note "Repo landmines — why the build's rules exist"). // // So the shape lives here, beside the method name, in the one module // `BoundaryContractTests` sweeps — and `TerminalWireTests` holds these types to // `crates/cotton-ffi/src/phone/terminal-wire.json`, which the crate's own test // feeds to the core. A field renamed on either side reddens one of the two. // // **The method NAMES are `CottonMethod`'s**, not a screen's enum: a name // declared beside the view that sends it is invisible to the sweep that holds // the app's vocabulary against `crates/cotton-ffi/src/methods.rs`, which is how // a crate-side rename reaches a person as an `unknown_method` instead of as a // red test. import Foundation extension CottonMethod { /// `terminal.attach {session, cols, rows}` — start watching one session's /// terminal. Idempotent: a tab reopened means "show me this one". /// /// The SIZE is required, and is not a convenience: the far end sets the /// pseudo-terminal's window size from it before the first byte is replayed, /// and an attach with no size is a `bad_request`. public static let terminalAttach = "terminal.attach" /// `terminal.write {session, bytes}` — keys, base64. /// /// A terminal's input is not text: a cursor key is `ESC [ A` and a paste may /// be any encoding at all, so what travels is bytes and the decoding is /// nobody's on this side. public static let terminalWrite = "terminal.write" /// `terminal.resize {session, cols, rows}` — the grid the far end paints /// into. public static let terminalResize = "terminal.resize" /// `terminal.detach {session}` — this device has stopped watching. Never /// refuses, and nothing happens to the run. public static let terminalDetach = "terminal.detach" } /// The two event kinds the terminal arrives on. /// /// Dotted, where every other kind on that bus is one lowercase word — the names /// Cotton note "The Code tool — why a Mac holds the sessions" fixed, and `EventKind`'s own `serde` renames. public enum TerminalEventKind { /// `{session, bytes}` — PTY output, base64. public static let data = "terminal.data" /// `{session, state}` — `attached`, `catchingUp` or `closed`. public static let state = "terminal.state" } /// How big the terminal is on screen, in character cells. /// /// A TYPE rather than two loose `Int`s because the band is the core's and not a /// view's: `dimension()` in `phone/terminal.rs` refuses anything outside /// 1…1000, and a view reporting its size mid-layout reports zero. Both of those /// are decided here, once, so no caller can send a size the far end will refuse. public struct TerminalGrid: Sendable, Equatable { /// The widest and tallest the core will accept — `MAX_DIMENSION`. A /// `TIOCSWINSZ` takes a `u16` and a phone is nowhere near it; clamping is /// what keeps a runaway measurement from becoming a refused resize on a /// terminal a person is reading. public static let maxDimension = 1_000 /// What an attach asks for before the emulator has been laid out. /// /// Eighty by twenty-four, the terminal's own default. The pane cannot know /// the real grid before the view exists — the emulator is only mounted once /// the attach has succeeded — so the first size is this one and the view's /// first `sizeChanged` sends the real one a frame later. public static let fallback = TerminalGrid(cols: 80, rows: 24) public let cols: Int public let rows: Int private init(cols: Int, rows: Int) { self.cols = cols self.rows = rows } /// A grid from what a view measured, or `nil` when it measured nothing. /// /// **A zero is not clamped to one, it is refused.** A view mid-layout /// reports zero columns; sending a 1×1 terminal would make the far end /// reflow the agent's output into a single column, and the next layout pass /// would reflow it back. Nothing is the honest answer, and the caller keeps /// the size it had. public init?(measuredCols: Int, measuredRows: Int) { guard measuredCols > 0, measuredRows > 0 else { return nil } self.cols = min(measuredCols, TerminalGrid.maxDimension) self.rows = min(measuredRows, TerminalGrid.maxDimension) } } /// `terminal.attach`'s request. public struct TerminalAttachRequest: Encodable, Sendable, Equatable { public let session: String public let cols: Int public let rows: Int public init(session: String, grid: TerminalGrid) { self.session = session self.cols = grid.cols self.rows = grid.rows } } /// `terminal.write`'s request — the bytes a key produced, base64. public struct TerminalWriteRequest: Encodable, Sendable, Equatable { public let session: String public let bytes: String public init(session: String, bytes: some Sequence) { self.session = session self.bytes = Data(bytes).base64EncodedString() } } /// `terminal.resize`'s request. public struct TerminalResizeRequest: Encodable, Sendable, Equatable { public let session: String public let cols: Int public let rows: Int public init(session: String, grid: TerminalGrid) { self.session = session self.cols = grid.cols self.rows = grid.rows } } /// `terminal.detach`'s request — one session named, and nothing else. public struct TerminalDetachRequest: Encodable, Sendable, Equatable { public let session: String public init(session: String) { self.session = session } } /// What `terminal.attach` and `terminal.detach` ANSWER: `{session, state}`. /// /// Decoded rather than ignored, because the answer is the first thing that says /// the terminal is live — the stream's own `terminal.state` follows, and a pane /// that waited for it would draw "Opening the terminal" over a terminal that had /// already replayed its scrollback. public struct TerminalAttached: Decodable, Sendable, Equatable { public let session: String public let state: String public init(session: String, state: String) { self.session = session self.state = state } }