The `ask` tool asks and the answer -- or "nobody answered" -- becomes the observation. Read class: it changes nothing on the machine, and the authority is the operator, not a gate. Front ends: the chat draws a dialog (options to pick, typed words still count), the protocol carries it as an `ask` event and an `answer` command, MCP returns it from splinter_say and resumes with splinter_answer, the Telegram gateway sends it as a message and takes the next message from an allowed user. What the review of the WIP found, and what this fixes: - -p hung for the full 900 s ask wait: sp_engine_prompt installed engine_ask, but nobody polls events on the synchronous path. cfg.ask now stays NULL there, so the tool answers at once with "nobody can be asked here". Measured: 3 s instead of 900 s (was rc=124 after 20 s). - splinter_say over MCP hung the same way, because tool_say only looked at st.waiting. It now looks at st.asking and returns the question; the client timed out at 15 s before, and the turn was lost. - protocol.c read the question buffer uninitialized when sp_engine_ask returned 0. - turn_ask answered nothing when the window could not be opened, which parked the turn for the full wait. - pick_run computed the list height without the question's rows, so the highlighted row scrolled out of the window (reproduced: after nine Downs no ">" row was on the screen). pick_draw and pick_run now share pick_listtop(). - the Telegram gateway was dead for this tool: it runs its turns on a copy of e->base, where ask was never set, so the model was told "nobody can be asked" while the operator sat in the chat. New tg_ask, verified against a running daemon and covered in test_gateway.sh. - tests/test_ask_ui.sh was untracked and not in make check, so it never ran. A2A keeps no ask channel on purpose: there an approval pauses the task into INPUT_REQUIRED, and the ask signature cannot express a pause -- saying "nobody can be asked" is the honest answer there. Tests: make check green (327 s), including the new test_ask_mcp.sh and test_ask_ui.sh, both wired into make check. test_ask_mcp.sh was verified to fail against the unfixed code (client times out, no result written). make check-asan: 46 binaries clean. make check-tsan: 46 binaries, no races.
326 lines
14 KiB
Markdown
326 lines
14 KiB
Markdown
# The front-end protocol
|
|
|
|
`splinter --serve` runs the engine on a Unix socket. Everything a front end
|
|
needs — the transcript, the reasoning as it streams, tool calls, approval
|
|
questions, slash commands, model and profile switches — arrives as one JSON
|
|
object per line. The curses interface uses the same engine in-process through
|
|
`include/splinter_engine.h`; this socket is the same boundary for programs that
|
|
are not C.
|
|
|
|
```
|
|
splinter --serve [--socket PATH]
|
|
```
|
|
|
|
The default socket is `$SPLINTER_SOCKET`, else `~/.splinter/splinter.sock`.
|
|
The file is created mode `0600` and the server refuses a peer whose uid is not
|
|
the owner's: this conversation may run `run` with the owner's rights.
|
|
|
|
The reference client is `splinterctl`; `docs/proto-example.py` is a 60-line
|
|
Python client that shows the whole loop. `splinter --attach` is the complete
|
|
curses front end speaking this protocol, so anything the screen can do is
|
|
reachable over the socket by design. If its connection drops, `--attach`
|
|
reconnects and asks for the session it was on with `open`; a GUI can do the
|
|
same.
|
|
|
|
## Framing
|
|
|
|
* One JSON object per line, UTF-8, `\n` terminated. No arrays, no envelope.
|
|
* Server → client objects have `"ev"`; client → server objects have `"cmd"`.
|
|
* Unknown fields are ignored. Unknown `cmd` answers with an `error` event.
|
|
* `"protocol": 2` in the greeting is the format version. It changes only when
|
|
a field changes meaning; new fields are added without a bump.
|
|
|
|
### Why this stays one JSON object per line
|
|
|
|
It was considered for replacement and kept. The two failures that made the
|
|
framing look guilty were not on this socket: a model sent two tool-argument
|
|
objects in one string and `mcp_parse()` silently took the first (fixed with
|
|
`mcp_parse_exact()`), and a gateway put HTTP chunk framing inside an SSE body
|
|
(fixed at the gateway). A second, non-JSON protocol would have hidden neither.
|
|
The line format is also what lets `splinterctl`, `docs/proto-example.py` and a
|
|
GUI be a client in a few dozen lines.
|
|
|
|
One client at a time. The server holds one active engine, so one conversation
|
|
runs; `open` switches it, it does not run two. A second writer on the same
|
|
engine would be a second history. When a client disconnects in the middle of a turn, the server
|
|
cancels the turn (an approval nobody can answer would otherwise park the
|
|
engine forever) and accepts the next client.
|
|
|
|
A client that arrives while another is attached is told so at once: it is
|
|
accepted only to be sent
|
|
`{"ev":"error","reason":"attached","text":"another client is attached: ..."}`
|
|
and then closed, and `splinterctl` exits non-zero. The server answers the door
|
|
even in the middle of a turn, so a second `splinterctl --cmd /status` gets that
|
|
error instead of hanging in the listen backlog with no output at all - which
|
|
reads as a broken engine. A client that hears no greeting at all (an older
|
|
server) gives up after five seconds with a message rather than waiting for an
|
|
outer `timeout`.
|
|
|
|
## Sequence
|
|
|
|
```
|
|
client server
|
|
| connect |
|
|
|<-- {"ev":"ready","protocol":2,...} |
|
|
| {"cmd":"commands"} / {"cmd":"history"} |
|
|
|<-- {"ev":"state",...} |
|
|
|<-- {"ev":"commands","names":[...]} |
|
|
|<-- {"ev":"history","items":[...]} |
|
|
| {"cmd":"input","line":"hallo"} |
|
|
|<-- {"ev":"submitted","text":"hallo"}|
|
|
|<-- {"ev":"busy","on":true} |
|
|
|<-- {"ev":"stream","kind":"think",..}| (only with a reasoning model)
|
|
|<-- {"ev":"stream","kind":"text",..} |
|
|
|<-- {"ev":"step","think":..,"text":..}
|
|
|<-- {"ev":"tool","name":"run",...} |
|
|
|<-- {"ev":"approval","name":"run",..}| (a mutating tool)
|
|
| {"cmd":"approve","allow":true} |
|
|
|<-- {"ev":"tool_result",...} |
|
|
|<-- {"ev":"stream","kind":"text",..} |
|
|
|<-- {"ev":"turn","ok":true,...} |
|
|
|<-- {"ev":"state",...} |
|
|
|<-- {"ev":"busy","on":false} |
|
|
```
|
|
|
|
`stream` chunks are deltas: append them. `step` marks the end of one model step
|
|
at a tool call; it carries the step's whole reasoning and answer, so a client
|
|
that lost a chunk can repair from it. The final answer of the turn is in
|
|
`turn.answer` (the last step's text, which is also the last `stream` text).
|
|
|
|
A `turn` event does not by itself mean the engine is idle. A steer that was
|
|
typed too late for a step boundary becomes a second turn in the same busy
|
|
period: another `entry` (`you`), more `stream`/`turn` events, and only then
|
|
`busy:false`. A client finishes its work when `busy.on` is `false`, not at the
|
|
first `turn`; each `turn` is one answer to show.
|
|
|
|
## Client → server
|
|
|
|
### `{"cmd":"input","line":"..."}`
|
|
|
|
The universal entry point: exactly what the operator typed.
|
|
|
|
* A slash command (`/model`, `/status`, `/agent plan`, …) answers with a
|
|
`reply` event and does not start a turn.
|
|
* A prompt starts a turn: `submitted`, then the turn events.
|
|
* `!command` runs a shell; its output arrives as an `entry` with
|
|
`"role":"shell"`.
|
|
* A program in `~/.splinter/commands/` replaces the line with its output and
|
|
starts a turn with it (`submitted` carries the text).
|
|
|
|
The server refuses this command with an `error` while a turn is running; the
|
|
transcript is the engine's, not a client's scratchpad.
|
|
|
|
### `{"cmd":"say","text":"..."}`
|
|
|
|
Start a turn directly, without command interpretation. The client has already
|
|
decided this is a prompt (for example from its own input field).
|
|
|
|
### `{"cmd":"command","line":"/..."}`
|
|
|
|
Alias of `input`; kept because a client that has a command palette knows what
|
|
it is sending.
|
|
|
|
### `{"cmd":"approve","allow":true}`
|
|
|
|
Answer the open approval question. `false` denies; the denial becomes the
|
|
observation the model sees (it is not silent).
|
|
|
|
### `{"cmd":"answer","text":"..."}`
|
|
|
|
Answer the open question from the `ask` tool. The text is the answer; an empty
|
|
string counts as "no answer" and the model is told so. A client that answers
|
|
nothing leaves the turn parked for the engine's wait, then the tool reports
|
|
that nobody answered — it does not hang forever.
|
|
|
|
### `{"cmd":"cancel"}`
|
|
|
|
Cancel the running turn. The engine sets its flag; every transport kills the
|
|
child process or the `curl` it started. The turn then ends with
|
|
`"ev":"turn","ok":false`.
|
|
|
|
### `{"cmd":"steer","text":"..."}`
|
|
|
|
Text typed while the turn runs. The loop picks it up at the next step boundary
|
|
and puts it into the conversation; a step that has already ended cannot be
|
|
interrupted. If no turn runs, the answer is an `error`.
|
|
|
|
### `{"cmd":"state"}`
|
|
|
|
Ask for a `state` event. Useful after a reconnect.
|
|
|
|
### `{"cmd":"history"}`
|
|
|
|
Ask for the transcript so far: `"ev":"history"`, `"truncated":bool`,
|
|
`"items":[{"role":"user","text":"..."}, {"role":"assistant",...}]`. The last
|
|
100 messages and 256 KiB are sent; a client that attaches shows the session it
|
|
joins instead of an empty pane. The system prompt and tool observations stay
|
|
out — this is what the person and the model said.
|
|
|
|
### `"tag"`: which answer belongs to which request
|
|
|
|
A request may carry `"tag":N` (any positive number). The server echoes it in
|
|
the event that answers it — `sessions`, `sources`, `models` — and
|
|
the client drops an event whose tag is not the one it is waiting for. Without
|
|
it a listing was matched by kind alone, so a server slower than the client's
|
|
5 s timeout left its answer in the socket and the next request of that kind
|
|
read it as its own. A request without a tag gets an answer without one, as
|
|
before.
|
|
|
|
### `{"cmd":"sessions"}`
|
|
|
|
List the sessions in the server's session directory, newest first:
|
|
`"ev":"sessions"`, `"items":[{"id","title","mtime","bytes","current"}]`.
|
|
`title` is the first user line or what `/rename` set (empty when there is no
|
|
message yet), `current` marks the session the connection is on.
|
|
|
|
### `{"cmd":"sources"}`
|
|
|
|
What the `/sessions` picker offers as a first step: `"ev":"sources"`,
|
|
`"items":[{"name","count"}]` — `splinter` first (this session directory), then
|
|
every foreign store that has sessions. A store the plugin cannot read is left
|
|
out instead of shown empty.
|
|
|
|
### `{"cmd":"sessions","source":"NAME"}`
|
|
|
|
The second step: the sessions of one source, same `"ev":"sessions"` shape as
|
|
above. For `splinter` the rows are plain session ids. For `claude`, `hermes`
|
|
and `opencode` a row's `id` is `"<source>:<id>"`, which is exactly what
|
|
`{"cmd":"open","session":"claude:<id>"}` takes to import that session and
|
|
continue it here.
|
|
|
|
### `{"cmd":"open","session":"ID"}`
|
|
|
|
Switch this connection to another session. `"new"` starts a fresh one,
|
|
`"last"` the newest, an id continues that file. The server sends `ready`,
|
|
`state`, `commands`, `history` and finally `"ev":"opened"` with the new
|
|
`session` and `dir`; the old engine is closed first, so plugins are not doubled.
|
|
While a turn runs the command is refused. An unknown id is an error and the
|
|
connection stays on its session.
|
|
|
|
`{"cmd":"input","line":"/rename [ID] TITLE"}` names the current session (or the
|
|
one named by ID), and `{"cmd":"input","line":"/rm ID"}` removes a session that is
|
|
not the open one: its JSONL, sidecars and `<dir>/logs/<id>` directory. Both
|
|
answer with a `reply` event.
|
|
|
|
### `{"cmd":"commands"}`
|
|
|
|
Ask what a command palette may offer: `"ev":"commands"`,
|
|
`"names":["agent","connect","model",...]` — the same list Tab completes in the
|
|
curses front end, plugin commands and `commands/` programs included.
|
|
|
|
### `{"cmd":"models"}`
|
|
|
|
The list `/model` takes, for a picker: `"ev":"models"` with
|
|
`"items":[{"sel":"junction/qwen3.6-35b","label":"junction/qwen3.6-35b","current":true},...]`.
|
|
`sel` is what goes after `/model `, `label` is what the picker shows, and
|
|
`current` marks the model the run is on. Without an argument it is the
|
|
configuration (providers, then pools), so it answers at once. With
|
|
`{"cmd":"models","probe":"true"}` the endpoint this session is on is also asked
|
|
`/v1/models` and its models are appended, as `provider/model` selectors; that
|
|
is a network call and may take a few seconds. `/model <selector>` through
|
|
`input` does the switch and stores the choice in the session.
|
|
|
|
The answer carries the request's `tag` like the other listings, so a probe that
|
|
took longer than the client's timeout cannot be read as the answer to the next
|
|
`models` request: `/model` would then draw the endpoint's models where the
|
|
configuration was asked for.
|
|
|
|
### `{"cmd":"ping"}` / `{"cmd":"quit"}`
|
|
|
|
`pong`; and a clean shutdown: `quit` event, server closes the socket and exits
|
|
with 0.
|
|
|
|
## Server → client
|
|
|
|
| `ev` | fields | meaning |
|
|
| --- | --- | --- |
|
|
| `ready` | `protocol`, `version` | the greeting, always first |
|
|
| `state` | `session`, `dir`, `model`, `endpoint`, `agent`, `agents[]`, `agent_cur`, `tokens_in`, `tokens_out`, `ctx_used`, `ctx_max`, `cached`, `turn_tokens_out`, `steps`, `calls`, `compactions`, `last_ms`, `last_steps`, `last_calls`, `busy`, `waiting`, `asking`, `quit` | everything a sidebar or the status bar shows |
|
|
| `submitted` | `text` | the prompt actually run (a command may have rewritten it) |
|
|
| `busy` | `on` | a turn started or ended |
|
|
| `stream` | `kind` (`text` or `think`), `text` | a delta of the answer or the reasoning |
|
|
| `step` | `think`, `text` | one model step ended at a tool call |
|
|
| `tool` | `name`, `args`, `call` | a tool call; `call` is the readable line (`Run ls -l`) |
|
|
| `tool_result` | `name`, `text`, `join` | what the tool returned; `join` true means "append to the call row" |
|
|
| `entry` | `role`, `text` | a finished transcript entry |
|
|
| `note` | `text` | a one-line prompt note, e.g. a usage-limit wait; `""` clears it |
|
|
| `approval` | `name`, `args`, `call`, `auth`, `class` | a tool needs consent; answer with `approve` |
|
|
| `ask` | `text`, `options[]` | the `ask` tool's question; answer with `answer` |
|
|
| `turn` | `ok`, `rc`, `answer`, `error` | one turn ended; `busy:false` ends the work |
|
|
| `clear` | — | `/new`: drop the transcript pane (the session is new) |
|
|
| `reply` | `text` | the answer to a command; may be empty |
|
|
| `error` | `text` | the command was refused |
|
|
| `commands` | `names[]` | the slash commands, for a palette |
|
|
| `sessions` | `items[]` | the sessions, with id/title/mtime/bytes/current |
|
|
| `opened` | `session`, `dir` | the switch is complete |
|
|
| `history` | `truncated`, `items[]` | the transcript so far (role/text) |
|
|
| `pong` | — | answer to `ping` |
|
|
| `quit` | — | the engine is shutting down |
|
|
|
|
`entry.role` is one of `you`, `agent`, `thinking`, `tool`, `observation`,
|
|
`splinter`, `shell`, `error`. A client renders them in its own way; the
|
|
sidecar values (`state`, `tool.call`, `tool_result.join`) exist so it does not
|
|
have to re-implement the harness' own arithmetic.
|
|
|
|
## A minimal client
|
|
|
|
```python
|
|
import json, socket, sys
|
|
|
|
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
s.connect(sys.argv[1])
|
|
f = s.makefile("rw", encoding="utf-8")
|
|
|
|
def send(obj):
|
|
f.write(json.dumps(obj) + "\n")
|
|
f.flush()
|
|
|
|
send({"cmd": "input", "line": "list the C files"})
|
|
for line in f:
|
|
ev = json.loads(line)
|
|
if ev["ev"] == "stream" and ev["kind"] == "text":
|
|
print(ev["text"], end="", flush=True)
|
|
elif ev["ev"] == "tool":
|
|
print("\n>>", ev["call"])
|
|
elif ev["ev"] == "approval":
|
|
send({"cmd": "approve", "allow": True})
|
|
elif ev["ev"] == "turn":
|
|
print()
|
|
break
|
|
```
|
|
|
|
Run it against `splinter --serve --socket /tmp/s.sock` with the socket path as
|
|
the argument; `docs/proto-example.py` is the same program with argument
|
|
handling.
|
|
|
|
## The same boundary in C
|
|
|
|
`include/splinter_engine.h` is what `src/frontend/curses.c` and
|
|
`src/frontend/protocol.c` use:
|
|
|
|
```c
|
|
struct sp_engine *eng;
|
|
struct sp_engine_opts o = { .conf = &conf, .base = &cfg, .endpoint = url,
|
|
.model = name, .key_env = "SPLINTER_API_KEY" };
|
|
sp_engine_open(&eng, &o, &err);
|
|
sp_engine_input(eng, "/status", &reply); /* or a prompt ... */
|
|
sp_engine_submit(eng, prompt); /* ... then a turn */
|
|
while (sp_engine_poll(eng, &ev)) /* the same events as above */
|
|
handle(ev);
|
|
sp_engine_approve(eng, 1);
|
|
sp_engine_close(eng);
|
|
```
|
|
|
|
The event kinds map one to one onto the JSON `ev` names (`SP_EV_TOOL` ↔
|
|
`tool`, `SP_EV_TURN_END` ↔ `turn`). A front end cannot drift from the socket
|
|
protocol without drifting from the curses one, because both read the same
|
|
queue.
|
|
|
|
## Versioning
|
|
|
|
`"protocol": 2` is the current version. Additive fields keep the number;
|
|
removing a field or changing its meaning bumps it, and the curses front end
|
|
moves in the same commit. Version 2 added the `history` command, which
|
|
`--attach` uses during its greeting: a client that needs it refuses a version
|
|
1 server instead of waiting for an answer that never comes.
|