# The bone protocol Everything that talks to the bone core (the TUI, scripts, editors) uses this API. It is JSON-RPC 2.0, one JSON message per line (newline-delimited JSON). The typed definitions in `crates/bone-proto/src/methods.rs` and `types.rs` are the source of truth. Exact wire examples for every method and event are in `crates/bone-proto/tests/golden/`; tests fail if the wire format changes. ## Connecting | Transport | How | |---|---| | stdio | start `bone --headless`, write requests to its stdin, read its stdout | | Windows named pipe | `bone --headless --listen [PATH]`, default `\\.\pipe\bone3-`; restricted to the current user and local clients | | Unix socket | `bone --headless --listen [PATH]`, connect to `PATH` (default `$XDG_RUNTIME_DIR/bone3/bone.sock`, mode 0600) | | in-process | Rust only: `Server::connect_in_process()` | One socket server serves many clients. Each one sees every event, so several UIs can watch and drive the same session. From the shell: ```sh printf '%s\n' \ '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocol_version":0,"client_name":"sh"}}' \ '{"jsonrpc":"2.0","id":2,"method":"session/list"}' \ '{"jsonrpc":"2.0","id":3,"method":"shutdown"}' | bone --headless ``` ## Handshake The first request must be `initialize`. Anything else gets `-32002`. The client sends its `protocol_version`, and a different version gets `-32003` with the server's version in `error.data`. The connection is usable after a failed handshake, so the client can retry. The protocol is version `0` until it is declared stable; until then, client and server must match exactly. Events are only sent to connections that have completed the handshake. ## Methods After `initialize`, the server runs each request on its own task: replies carry their request's `id` and can arrive in any order, and a request's events (for example `model/delta`) can arrive before its reply. | Method | Params | Result | |---|---|---| | `initialize` | `{ protocol_version, client_name }` | `{ protocol_version, server_name, server_version }` | | `shutdown` | `{}` | `null`; then the server closes this connection | | `echo` | `{ text }` | `{ text }`, plus an `echoed` event (for testing) | | `session/create` | `{ cwd? }` | `SessionInfo`. `cwd` defaults to the server's directory, so clients should send their own | | `session/list` | `{}` | `[SessionInfo]`, newest first | | `session/active` | `{}` | `[SessionId]` for currently running turns in the core (ordering unspecified); no transcripts are loaded | | `session/messages` | `{ session_id, tail? }` | `{ info, messages: [ChatMessage], active_turn?, omitted_turns?, tool_times?, turn_times? }`. With `tail`, about the last that many messages, from the start of their first turn (a turn much longer than that is cut inside it, never between a tool call and its result; `tail: 0` sends none); `omitted_turns` counts the user messages left out before them. `tool_times` is `{ : { started_at, duration_ms, generation_ms? } }` (Unix ms, ms, ms) for the tool calls in `messages` that were recorded with their times, as `tool/started` and `tool/finished` told them; calls from cores before it was kept have none. `turn_times` is `{ : { started_at, duration_ms } }` for the finished turns in `messages`, numbered as their last user message is (omitted ones counted), as `turn/finished` told them | | `session/set` | `{ session_id, title?, pinned?, archived? }` | `SessionInfo` as changed: a `title` replaces the one from its first message, `pinned` lists it first, `archived` keeps it out of the recent chats until its next turn starts (which clears it). Each change is a line in the session file; a `session/changed` event follows when anything changed | | `session/cd` | `{ session_id, cwd }` | `SessionInfo` with the new `cwd`: the session's tools run there from its next turn on. A relative `cwd` starts from the one it has. Kept in the session file; the model's first message from the new directory is sent with a note of the change. An error while a turn runs or when `cwd` is not a directory; a `session/changed` event follows, unless it was there already | | `session/delete` | `{ session_id }` | `null`; the session and its file are gone and `session/deleted` goes to every client. An error while a turn runs | | `session/compact` | `{ session_id, clear? }` | `{ session_id, messages, tokens_before, tokens_after, reason }`: the older part of the session is summarized by a model, and from then on model calls get the summary plus a handoff rebuilt from the transcript at the cut, in place of it. The handoff is not stored. The transcript, the session file and `session/messages` keep everything. `messages` is how many transcript messages were newly summarized; the token counts are estimates of what a model call sends, before and after. `clear` drops the summary instead (`reason: "clear"`). An error when there is nothing to compact yet. Also announced as `session/compacted` | | `attachment/upload` | `{ data, name? }` (base64 PNG/JPEG/WebP) | `ImageAttachment`: validates, normalizes and stores the image on the core machine | | `attachment/read` | `{ id }` | `{ data }`: base64 PNG for a saved attachment | | `turn/start` | `{ session_id, text, images? }` | `{ turn_id }`, returned at once; the turn runs in the background | | `turn/steer` | `{ session_id, text, images? }` | `null`; shorthand for `queue/add` with `mode: "steer"`, but an error when no turn is running | | `queue/add` | `{ session_id, text, mode?, images? }` | `{ id? , turn_id? }`. Idle session: the message starts a turn at once (`turn_id`). Running turn: it is queued (`id`); `mode: "steer"` (the default) joins that turn before its next model call (`turn/steered` says when; one arriving during the final answer gets another step), `"next"` starts its own turn after it. Queued turns go through `turn_start` hooks; steered messages do not. Core Lua `queue_add` hooks may rewrite or refuse it | | `queue/remove` | `{ session_id, id }` | `null`; an error for an unknown id | | `queue/update` | `{ session_id, id, text?, mode?, images? }` | `null` | | `queue/move` | `{ session_id, id, to }` | `null`; `to` is the new position, 0 first | | `queue/clear` | `{ session_id }` | `null` | | `queue/resume` | `{ session_id }` | `null`; a paused queue goes on (starting a turn if the session is idle) | | `turn/cancel` | `{ session_id }` | `null` (no-op if nothing is running) | | `processes/get` | `{ session_id }` | `{ version, processes: [ProcessSnapshot] }` for managed shell processes | | `processes/list` | `{}` | the same, for every session's processes | | `process/cancel` | `{ session_id, id }` | `null`; asks the process group to stop | | `process/read` | `{ session_id, id, from? }` | `{ offset, data, total }`: the output as written (escape codes kept) from byte `from` (default 0); `offset` is later than asked when older output is no longer kept (the last 1 MiB is), `total` is how many bytes it wrote | | `process/resize` | `{ session_id, id, cols, rows }` | `null`; sets a running terminal job's size (it gets `SIGWINCH`) | | `ask/respond` | `{ ask_id, answer }` | `null`; error if no question with that id is open, or if `answer` is `null` | | `ask/list` | `{}` | `[{ ask_id, session_id?, question }]`: the questions still waiting for an answer, oldest first, for a client that missed their `ask/requested` | | `health/check` | `{}` | `[{ name, status: "ok" \| "warn" \| "error", message }]`: the core's checks (provider, API key, reachability, sessions folder, Lua) and core Lua's `bone.health` checks | | `settings/get` | `{}` | the saved settings (`settings.json` in the config dir), a JSON object. The core keeps its own copy: a change made to the file from outside (an editor, the agent, another core) is taken up when the next turn starts, announced with `settings/changed` (empty `path`), and is never overwritten by a later `settings/set`, which changes the file as it is then | | `settings/set` | `{ path, value, session_id? }` | every saved setting after the change. `path` is dotted (`tui.tool_detail`, `providers.qwen.model`); `null` removes it. `provider` must name a configured provider; `providers.` adds a provider and `providers..` changes one field; these changes reload the configuration and are undone if loading fails. With `session_id`, changing `provider` or that session's provider model also saves its provider/model selection; other sessions keep theirs. Every client hears `settings/changed` | | `secrets/set` | `{ provider, key }` | the names of the providers with a key. Saves (or with `key: null` removes) a provider's API key in `secrets.json` (mode 0600) and reloads. Keys are never sent to clients | | `secrets/list` | `{}` | the names of the providers with a saved key | | `settings/reset` | `{ path }` | as `settings/set` with `null` | | `core/reload` | `{}` | `ReloadResult`. Loads the Lua configuration again (runtime, enabled plugins, `core.lua`) and switches to it; running turns finish on the previous one. An error (and no change) if loading fails | | `plugin/list` | `{}` | `[{ name, core, loaded }]`: the plugins folder; `core` if it has a `core.lua`, `loaded` if that runs | | `lua/call` | `{ name, args?, session_id?, cwd? }` | whatever the function core Lua registered as `name` (`bone.rpc.register`) returns; it gets `args` and `{ session_id, cwd }`. An error for an unknown name or a failing function | | `mcp/list` | `{}` | `[{ name, state, error?, tools, url?, command?, lazy?, signed_in?, tool_info? }]`: the MCP servers core Lua configured; `state` is `"idle"`, `"starting"`, `"ready"`, `"auth"` (needs a sign-in) or `"failed"`, `tools` their tools by the names the model sees, `tool_info` `[{ name, description, read_only? }]` | | `mcp/reconnect` | `{ name }` | `{}`: drop the server's connection and start it again now (also revives one that gave up) | | `mcp/auth` | `{ name }` | `{ url, redirect_uri }`: begin an OAuth sign-in to an HTTP server; open `url` in a browser. The core finishes when the browser is redirected to `redirect_uri` on this machine, then reconnects the server. Tokens are saved in `mcp-auth.json` (owner-only) and refreshed automatically | | `mcp/auth_code` | `{ name, code }` | `{}`: finish a sign-in from the address the browser was redirected to (or the bare code), for a browser on another machine | | `mcp/sign_out` | `{ name }` | `{}`: forget the saved sign-in and reconnect | | `store/query` | `{ sql, params? }` | `{ columns, rows, truncated }`: one read-only SQL statement against the session index (see architecture.md for its tables). `params` is a list (`?1`…) or an object (`:name`); at most 10,000 rows, 5 seconds | | `model/list` | `{ session_id? }` | `[ModelInfo]`: the configured providers, sorted by name. With a session, `current` and its `model` reflect that session's saved selection; otherwise they reflect the default. `models` is the provider's list of models to switch between (`providers..models`), with `model` and the provider's default always in it | | `model/use` | `{ session_id?, provider?, model }` | every saved setting after the change. Switches to `model` of `provider` (default: the session's) in one change and one reload: it becomes `providers..model` and `provider`, joins `providers..models` when it is not listed, and the session moves to it; other sessions keep theirs. Every client hears `settings/changed` for each path that changed | | `model/complete` | `{ provider?, messages, tools?, options?, stream? }` | `{ request_id }`, returned at once. One model call outside any session: `provider` is an entry name (default: the current one), `tools` are offered but never run, `options` override `model`, `reasoning_effort` or a Lua provider's options. With `stream`, `model/delta` events follow; `model/completed` always ends it | | `model/cancel` | `{ request_id }` | `null`; the call ends with `model/completed` and the error `"cancelled"` | | `plugin/load`, `plugin/unload`, `plugin/reload` | `{ name }` | `ReloadResult`: enable, disable, or keep the plugin, then reload as `core/reload` does. An error for a plugin that is not there or has no `core.lua` | `ImageAttachment` is `{ id, name, mime_type, width, height, bytes }`, where `id` is the SHA-256 of a canonical PNG, `mime_type` is `"image/png"`, and `bytes` is its stored size. First upload bytes, then put returned references in `images` on user or tool messages, turn or queue requests, or `model/complete` messages. Local client paths are never sent to the core. A message may have empty text when it has images. `queue/update` omits `images` to keep them or sends `images: []` to remove them; an empty message is rejected. Empty lists are omitted on the wire, preserving existing text-only messages. Limits are 8 images/message, 20 MiB/image input and canonical PNG, 40 megapixels/image, 40 MiB/message, and 32 MiB per NDJSON frame. Image bytes are rejected inside references: only providers receive an extra `data` field with hydrated base64 PNG. Caller-supplied metadata is replaced by verified stored metadata. Sessions and queue files save references, so attachment files must move with the data directory. Uploads deduplicate by hash and are retained indefinitely. `ReloadResult` is `{ plugins: [{ name, core, loaded }], warnings? }`; `warnings` lists settings that cannot change while running (`data_dir`) and errors from `bone.on_shutdown`. Disabling a plugin lasts until the server restarts; rename its folder to disable it for good. `ModelInfo` is `{ name, model, type?, current, supports_images?, compact_limit?, base_url?, reasoning_effort?, stream_usage, replay_reasoning?, api?, api_auto?, has_key, added }`. `type` names a Lua provider; built-in providers use `api: "completions" | "responses" | "anthropic"` (`"completions"` is omitted). `api_auto` is true when the API was inferred rather than configured. `has_key` reports whether a key is configured, never the key itself; `added` marks a provider defined only in `settings.json`. `replay_reasoning` forces Chat Completions reasoning replay on or off; absent, reasoning is replayed once the server streams it. `supports_images: false` rejects image calls; absent leaves support to the endpoint. `compact_limit` is an optional nonnegative integer: absent inherits global `compact.limit`, positive overrides it, and `0` disables proactive compaction without changing overflow retry. The session's selected provider supplies the limit, not the summary provider. Set it through `settings/set` at `providers..compact_limit`; `null` removes the saved override. `SessionInfo` is `{ session_id, cwd, created_at, title?, parent?, owner? }`. `parent` is legacy fork ancestry from older session files; new branching relationships belong to the Lua fork plugin; `owner` is `{ session_id, call_id?, name? }` for a session core Lua started on another's behalf (a sub-agent, via `bone.session.create`): the session and tool call that started it. Clients usually leave owned sessions out of session pickers and show them with their owner. The queue: each session keeps `[{ id, text, images?, mode: "steer" | "next", created_at }]`, saved next to its file (`.queue.json`, there only while the queue has messages) so it outlives a restart; ids are never reused. When a turn ends (including cancellation), any steer message that did not join it becomes `next`. After completed and failed turns, the first queued message starts the next turn. Cancellation normally pauses a nonempty queue, and so does loading a session that had a queue; `queue/resume` or a new `queue/add` lets it go on. Messages submitted after cancellation while the turn winds down become `next`, are placed ahead of older queued items in submission order, and prevent the queue from pausing. Accepted queue changes remain in memory and may run even if saving fails; `queue/changed.error` warns that they may be lost on restart. `session/messages` includes `queue` and `queue_paused`. `ChatMessage` is tagged by `role`: ```json { "role": "system", "content": "..." } { "role": "user", "content": "..." } { "role": "assistant", "content": "...", "reasoning": "...", "tool_calls": [{ "id": "call-1", "name": "read_file", "arguments": "{\"path\":\"README.md\"}" }], "reasoning_state": { "model": "...", "items": [] } } { "role": "tool", "call_id": "...", "content": "...", "is_error": true } ``` Empty assistant `content`/`reasoning`/`tool_calls`, absent `reasoning_state` and a false `is_error` are omitted. A tool call's `arguments` is the raw JSON string the model produced, which may be invalid. Assistant `reasoning_state` is saved with the transcript and returned to clients: `{ model, items, prefix?, field? }` holds opaque provider reasoning for replay on compatible later calls. Keep it intact when copying messages; `prefix` can bind it to an unchanged conversation, and `field` records the Chat Completions reasoning field. A user message has `source` when someone other than the user sent it: the `source` it was queued with (`bone.queue.add`), such as `"agent"`, `"job"` or `"result"`. Providers do not send it. User and tool messages may include `images: [ImageAttachment, ...]`. `read_file` image results retain their references on the tool message. `tool/finished` reports the text and dimensions; `session/messages` includes the image references. ## Events Every event carries `session_id` (except `echoed`, `ask/resolved`, `settings/changed`, `core/reloaded` and the `model/*` events) and, for turns, `turn_id`. | Event | Params | Meaning | |---|---|---| | `turn/started` | `{ text, images?, source? }` | a user message started a turn (from any client); `source` as on the message | | `message/delta` | `{ kind: "text" \| "reasoning", text }` | streamed model output | | `message/completed` | `{ message, usage? }` | the final assistant message as saved; replaces whatever streamed | | `tool/started` | `{ call, started_at?, generation_ms? }` | the core is handling a tool call (`tool_call` hooks run next); `started_at` is milliseconds since the Unix epoch. On the first call of a reply, `generation_ms` is how long the model spent writing that reply after its reasoning (from the last reasoning it streamed, or the start of the model call when none did, to the reply's end) | | `tool/output` | `{ call_id, text }` | output a running foreground tool produced, in order, as it comes; `tool/finished` still carries the whole result. Detached shell processes use `process/changed` after their start result | | `tool/finished` | `{ call_id, output, is_error, duration_ms? }` | its result, as the model will see it, and how long it ran | | `process/changed` | `{ session_id, version, process: ProcessSnapshot, chunk? }` | a managed shell process started, produced output, or changed state. `chunk` is `{ offset, data }`: new output as written, at byte `offset` (chunks follow on from each other; after a gap, `process/read` it) | | `ask/requested` | `{ ask_id, question }` | core Lua (a hook or tool) called `bone.ask(question)` and waits; answer with `ask/respond`. `question` is whatever the Lua passed, e.g. `{ kind: "confirm", text: "Write this file?" }` | | `ask/resolved` | `{ ask_id, answer }` | answered by some client, or `answer: null` if it was dropped (the turn was cancelled, or the core reloaded) first | | `turn/steered` | `{ text, images?, source? }` | a steer message from the queue joined the running turn's transcript (show it as a user message) | | `queue/changed` | `{ items, paused, error? }` | the session's queue after a change; a failed automatic start retains the item and pauses the queue. `error` also reports save failures, which do not reject accepted changes or stop the queue | | `turn/finished` | `{ outcome: { status: "completed" \| "cancelled" \| "failed", message? }, timing? }` | the turn is over; `timing: { started_at, duration_ms }` (Unix ms, ms) is when it ran, from saving its user message (none for a turn refused before that) | | `settings/changed` | `{ path, value, settings }` | a setting was saved (`value` null: removed) by some client; `settings` is all of them (no `session_id`) | | `core/reloaded` | `ReloadResult` | the core switched to a newly loaded Lua configuration (no `session_id`) | | `model/delta` | `{ request_id, kind, text }` | streamed output of a `model/complete` call (no `session_id`) | | `model/completed` | `{ request_id, message?, usage?, error? }` | a `model/complete` call ended: the assistant `message`, or an `error` (no `session_id`) | | `session/created` | `SessionInfo` | a session was created by a client or core Lua (a sub-agent's, with `owner`, or a seeded ordinary session) | | `session/deleted` | `{ session_id }` | a client deleted the session | | `session/changed` | `SessionInfo` | its title, folder, pinned or archived changed (`session/set`, `session/cd`, or a turn starting on an archived session) | | `session/compacted` | `{ messages, tokens_before, tokens_after, reason }` | the session was compacted (`reason`: `"manual"` from `session/compact`, `"limit"` over the effective compaction limit (`compact_limit` on the session's selected provider, else `compact.limit`), `"overflow"` after the model said the context is too long, or `"clear"`). The transcript did not change; there is nothing to reload | | `session/compact_failed` | `{ reason, error }` | automatic compaction failed (`reason`: `"limit"` or `"overflow"`); the context is unchanged. Future calls can attempt compaction again. Manual failures are returned by `session/compact` instead | | `session/updated` | `{ reason: "append" \| "compact" \| "lagged" \| "indexed" }` | core Lua changed the session's transcript (`bone.session.append`, or `bone.session.compact` replacing it); load it again with `session/messages`. With `session_id` empty, load every session shown and the list again: `"lagged"`, this client fell behind and missed events; `"indexed"`, the session index caught up with files it had not read, so `session/list` may say more | A typical turn: `turn/started`, then `message/delta`…, `message/completed` (with `tool_calls`), and for each call `tool/started`, then `ask/requested` → `ask/resolved` if a hook asks the user (for example, a custom approval hook), then `tool/finished`. That repeats until a `message/completed` arrives without tool calls, then `turn/finished`. Events are broadcast to all clients. A client that falls more than 8192 events behind loses the oldest ones, so treat `message/completed` and `session/messages` as authoritative over accumulated deltas. ## Multiple clients Any number of clients (a TUI, a GUI, scripts) can share one core and drive the same sessions. The core owns all session state; what holds between clients: - **Delivery.** Every event goes to every connection that completed the handshake, in the order the core emitted it. `turn/started` carries the user message, so other clients can show a turn they did not start. - **Questions.** The first `ask/respond` for an `ask_id` wins. Later answers, from any client, get `-32602` ("no open question"). Every client hears `ask/resolved` and should close its copy of the question. `ask/list` returns the questions still open, for a client that connected after `ask/requested`. - **Submitting.** `turn/start` on a session with a running turn fails with `-32004`. `queue/add` never conflicts: it starts a turn on an idle session and queues otherwise, so clients that may race should use it. - **Lag.** A client more than 8192 events behind gets `session/updated` with `reason: "lagged"` and an empty `session_id`. It has missed events and must reload every session it shows. - **Resync.** `session/messages` is a complete snapshot of a session: `messages`, `active_turn`, `queue`, and `queue_paused`. With `settings/get` and `ask/list`, it is all a client needs after connecting, reconnecting, or lagging. Fetch the snapshots before handling buffered events, so the events bring them up to date in order. - **Not shared.** Prompt drafts, scroll positions, open windows, and Lua items added with `bone.chat.add` belong to each client. `ProcessSnapshot` contains `id`, `command`, `state` (`running`, `exited`, `cancelled`, `timed_out` or `failed`), `running`, `pid`, start/finish times, `tail` (the last line of output, escape codes removed), `output_bytes`, `truncated`, exit status, and `terminal`. Its output comes as `process/changed` chunks and from `process/read`. Background shell processes are owned by the session and outlive the model turn; `process/changed` is the authoritative UI stream after the shell tool returns. They run in a pseudo-terminal (`terminal: true`, 120×32 until `process/resize`; `TERM=xterm-256color`, `PAGER=cat`), so their output is what a terminal shows: colors and `\r` progress lines. The model reads it with escape codes removed and progress lines settled. ## Errors | Code | Meaning | |---|---| | `-32700` | the line was not valid JSON-RPC (sent with `"id": null`) | | `-32601` | unknown method | | `-32602` | bad params (including unknown session or ask ids) | | `-32603` | internal error (e.g. session storage failed) | | `-32002` | request before `initialize` | | `-32003` | protocol version mismatch | | `-32004` | the session already has a turn running | ## Rust client ```rust let (client, mut events) = bone_proto::client::Client::new(bone_proto::client::connect(path).await?); client.initialize("my-tool").await?; let s = client.request::(SessionCreateParams { cwd: Some(cwd) }).await?; client.request::(TurnStartParams { session_id: s.session_id, text: "hi".into(), images: Vec::new() }).await?; while let Some(e) = events.recv().await { if let Some(Ok(d)) = e.parse::() { print!("{}", d.text) } if e.parse::().is_some() { break } } ``` `bone_proto::client::spawn` takes an owned `tokio::process::Command` configured with `--headless` and returns `(Connection, Child)` for a private server; keep the child handle alive while using the connection. `bone_proto::client::reconnect(path, client_name)` connects to a socket server again after losing it, retrying with backoff, and returns the new client with its settings and open questions already fetched. `crates/bone-server/examples/chat.rs` is a complete line-mode client.