// 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.importFoundationextensionCottonMethod {/// `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`.publicstaticlet 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.publicstaticlet terminalWrite = "terminal.write"/// `terminal.resize {session, cols, rows}` — the grid the far end paints/// into.publicstaticlet terminalResize = "terminal.resize"/// `terminal.detach {session}` — this device has stopped watching. Never/// refuses, and nothing happens to the run.publicstaticlet 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.publicenumTerminalEventKind {/// `{session, bytes}` — PTY output, base64.publicstaticlet data = "terminal.data"/// `{session, state}` — `attached`, `catchingUp` or `closed`.publicstaticlet 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.publicstructTerminalGrid: 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.publicstaticlet 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.publicstaticlet fallback = TerminalGrid(cols: 80, rows: 24)publiclet cols: Intpubliclet rows: Intprivateinit(cols: Int, rows: Int) {self.cols = colsself.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.publicinit?(measuredCols: Int, measuredRows: Int) {guard measuredCols > 0, measuredRows > 0else { returnnil }self.cols = min(measuredCols, TerminalGrid.maxDimension)self.rows = min(measuredRows, TerminalGrid.maxDimension) }}/// `terminal.attach`'s request.publicstructTerminalAttachRequest: Encodable, Sendable, Equatable {publiclet session: Stringpubliclet cols: Intpubliclet rows: Intpublicinit(session: String, grid: TerminalGrid) {self.session = sessionself.cols = grid.colsself.rows = grid.rows }}/// `terminal.write`'s request — the bytes a key produced, base64.publicstructTerminalWriteRequest: Encodable, Sendable, Equatable {publiclet session: Stringpubliclet bytes: Stringpublicinit(session: String, bytes: someSequence<UInt8>) {self.session = sessionself.bytes = Data(bytes).base64EncodedString() }}/// `terminal.resize`'s request.publicstructTerminalResizeRequest: Encodable, Sendable, Equatable {publiclet session: Stringpubliclet cols: Intpubliclet rows: Intpublicinit(session: String, grid: TerminalGrid) {self.session = sessionself.cols = grid.colsself.rows = grid.rows }}/// `terminal.detach`'s request — one session named, and nothing else.publicstructTerminalDetachRequest: Encodable, Sendable, Equatable {publiclet session: Stringpublicinit(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.publicstructTerminalAttached: Decodable, Sendable, Equatable {publiclet session: Stringpubliclet state: Stringpublicinit(session: String, state: String) {self.session = sessionself.state = state }}