//! `session.create` / `session.withdraw` / `session.resume` — the OWNER asks//! for a session.//!//! ## What a queued session is//!//! A row in `cotton_sessions` at `state = 'queued'`: which project, which Mac//! (or none, meaning any), which tool and model, a title and the first prompt.//! **It is the same row the session runs and ends on** — since schema v23 there//! is no second table. The phone writes it over sync like any other row; the//! `dispatch` worker on the serving Mac places a row with no device, and the//! chosen Mac's own client claims it (`queued → starting → running`) in its own//! file rather than over MCP.//!//! ## Where the row is actually written//!//! **`cotton_tools::session_birth`**, which is the ONE birth: the phone's//! `session.create` and the MCP `create_session` a project's conversation uses//! to open a thread are two callers of one function. This module is the//! METHOD — it parses what the phone sent, wraps the refusal in a//! `MethodError`, and hands the write to the client's `write_rows`.//!//! ## Why these are METHODS and not tools//!//! The same line `session.send` draws: the OWNER's own request is a method//! through the same `Ctx`, the same writer and the same outbox as every other//! phone write. What changed at v31 is not that: an assistant may now open a//! session over MCP, because a project's CONVERSATION is itself a session on a//! Mac and opening threads is what it is for. It goes through the same//! `write_session`, so the queue has one shape however it was asked for.use serde_json::Value;usecrate::handle::CottonHandle;usecrate::methods::MethodError;// The birth, and everything a caller of it needs. Re-exported rather than// re-declared: a second `CreateArgs` would be a second queue.pubuse cotton_tools::session_birth::{ can_carry_on, can_restart, can_resume, title_from_prompt, write_resume, write_session, write_withdraw, CreateArgs,SessionKind, PERMISSION_MODES, SESSION_PROMPT_MAX, SESSION_TITLE_MAX, SESSION_TOOLS, TITLE_FROM_PROMPT_MAX,};/// The phone's arguments, refused as a `MethodError` — the sentence is/// `session_birth`'s, and the shape is this layer's.pubfn validate_create(request: &Value) -> Result<CreateArgs, MethodError> { cotton_tools::session_birth::validate_create(request) .map_err(|reason| MethodError::new("bad_request", reason))}fn required(value: &Value, field: &str, what: &str) -> Result<String, MethodError> { value .get(field) .and_then(Value::as_str) .map(str::trim) .filter(|id| !id.is_empty()) .map(str::to_string) .ok_or_else(|| MethodError::new("bad_request", format!("{what} needs its `{field}`")))}/// `session.create` — one queued session, from the owner.pubfn create(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {let request = crate::phone::code_preferences::with_default(&handle.client(), request)?;let args = validate_create(&request)?; handle .client() .write_rows("session.create", &mut |ctx| write_session(ctx, &args)) .map(crate::authoring::own_answer) .map_err(|reason| MethodError::new("write_refused", reason))}pubfn validate_withdraw(request: &Value) -> Result<String, MethodError> { required(request, "id", "a withdrawal")}/// `session.withdraw` — take back a session no Mac has started yet.pubfn withdraw(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {let id = validate_withdraw(request)?; handle .client() .write_rows("session.withdraw", &mut |ctx| write_withdraw(ctx, &id)) .map(crate::authoring::own_answer) .map_err(|reason| MethodError::new("write_refused", reason))}pubfn validate_resume(request: &Value) -> Result<String, MethodError> { required(request, "sessionId", "a resume")}/// `session.resume` — carry on a session that ended.////// **It re-queues THE SAME ROW.** Not a copy: the id, the project, the prompt,/// the turns and the messages are the ones the owner is already looking at, and/// `external_id` is kept because it is what the agent is asked to continue and/// what every button on the card addresses over the helper's socket/// (`cotton-client/src/session_host/verbs.rs`).pubfn resume(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {let id = validate_resume(request)?; handle .client() .write_rows("session.resume", &mut |ctx| write_resume(ctx, &id)) .map(crate::authoring::own_answer) .map_err(|reason| MethodError::new("write_refused", reason))}