Files
splinter/docs/FRONTENDS.md
T
lotherk 181b2c0638 ask: the model can put a question to the operator, in every front end
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.
2026-11-24 07:57:35 +00:00

6.6 KiB

Backend and front ends

splinter is split in two so that the screen is not the harness.

                     +-----------------------+
   a keyboard  --->  |  front end (curses)   |  ---\
                     +-----------------------+      \
                                                     \   +----------------+
   a socket    --->  |  front end (protocol) |  --------> |  the engine    |
                     +-----------------------+      /      +----------------+
                                                     /       agent, tools,
   -p / --daemon --> |  direct library use   |  ---/        transports,
                     +-----------------------+               session, config,
                                                             plugins

The backend

libsplinter.so is the whole harness minus the screen:

file what it owns
src/backend/engine.c struct sp_engine: session, conversation, registry, plugins, transport, agent, turn thread, event queue, slash commands
src/backend/agent.c the loop: request, tool calls, observations, answer
src/backend/transport*.c, openai.c, anthropic.c, sse.c a conversation in, a reply out
src/backend/builtin.c, tool.c the tools and the registry
src/backend/session.c, conv.c append-only JSONL, owned messages
src/backend/config.c, providers.c, usage.c configuration and providers
src/backend/plugin.c dlopen, widgets, commands, gates, compactors
src/backend/telegram.c, a2aserver.c, mcpserver.c outbound protocol adapters

The engine never calls into a front end. A turn runs on a thread of the engine's making and appends struct sp_event records to a queue; the front end drains it whenever it draws. That is the whole threading rule, and it is why a slow terminal cannot stall a model, and why a GUI cannot corrupt a turn.

include/splinter_engine.h is the C boundary. Its event kinds map one to one onto the JSON ev names in docs/PROTOCOL.md.

The front ends

file kind
src/frontend/curses.c the screen: transcript, sidebar, prompt, keys
src/frontend/ui_backend.h what the screen needs from a backend
src/frontend/ui_local.c that backend as the engine in this process
src/frontend/ui_remote.c that backend as a protocol client (--attach)
src/frontend/protocol.c splinter --serve: JSON lines, sessions, open
src/frontend/client.c splinterctl, the reference protocol client
src/main.c the launcher: parse, config, engine, pick a front end

curses.c owns the screen, the themes, the markdown-to-ASCII pass, the input history and the key bindings. It does not know how a provider is switched, how a session is created or how an approval is waited for — those are /model, /new and the engine's condition variable. It talks to one of two backends through ui_backend.h: the engine linked into the process (--chat), or a served engine over the socket (--attach). The screen cannot tell them apart, which is the test the protocol has to pass.

splinter --serve --socket /tmp/s.sock   # one process: engine + protocol
splinter --attach --socket /tmp/s.sock  # another: the screen as a client

The server owns the engine: sessions lists the session directory and open switches to another id (or new/last), sending the state and history of that session. The curses screen exposes that as /sessions and /open ID, in both modes: --serve switches its engine, and the in-process --chat closes its engine and opens the next one from the same options (plugins keep process globals, so the old one goes first). /sessions without an argument is a two-step picker over the same ops (sources, then source_sessions): the foreign stores appear next to this folder's sessions, and picking one of their rows runs the same /open <source>:<id> that imports and continues it. One session is active at a time; two clients still take turns.

--attach keeps running when the backend disappears: it says so, retries every second, and on the next server asks for the session it was on by id. The history it already had is shown again, and turns continue; if that session is gone, the screen says which one and stays on the session the server has. A GUI gets the same with sessions, open and its own retry loop.

Writing a GUI client

Two ways, and the second one is the interesting one.

  1. Link the library. Include splinter_engine.h, call sp_engine_open(), then sp_engine_input() / sp_engine_submit() / sp_engine_poll() as src/frontend/curses.c does. This only works from C or a C-binding language.

  2. Speak the socket. Run splinter --serve and connect to the Unix socket from anything that can open one and write JSON: Python/Tk, Qt, GTK, a web front end through a small bridge. docs/PROTOCOL.md is the contract; docs/proto-example.py is a complete 60-line client, and splinter --attach is the full curses front end running this way. Use sessions/open for tabs and retry the connect for a server restart.

  3. Let another harness use splinter as a model. splinter --openai-listen serves /v1/chat/completions: the caller's tools stay with the caller, splinter's own run locally under gw_*. See docs/GATEWAY.md.

The recommended shape for a GUI:

  • one window per connection; the state event fills the sidebar (session, model, agent, tokens);
  • on connect, send history once and draw the returned items - a GUI that joins a session should not look like it invented it;
  • append stream deltas to the open bubble; close it on step or turn;
  • draw tool as a call row and tool_result under it (or on the same row when join is true);
  • when approval arrives, show the question and send approve;
  • when ask arrives, show text and options and send answer with the text the operator gave; the turn is parked until it arrives, so a client that ignores the event leaves the model waiting (the engine's timeout ends it with "nobody answered", but slowly);
  • busy drives the spinner; cancel and steer are the two buttons that must remain live during a turn;
  • nothing in the client has to know a provider, a session file or a tool's authorization class — the engine already decided.

There is one conversation and one client at a time; a second connection waits until the first is gone. An attaching client asks for history (the last 100 messages, up to 256 KiB) and shows the session it joins; live events continue from that point. If a client disconnects, the server cancels a running turn, because an approval that nobody can answer would park the engine forever.