no approval gate: splinter runs what the operator's registry offers
Konrad, 24.11.: "v1 geht durch ohne irgendwas. ich will auch keine approvals generell. ich will nur das der user mit ask nach sachen gefragt werden kann." Removed the whole approval apparatus, not just the prompt: - agent.c: the decision point (sp_host_gate/approve callbacks), the pause/resume path (has_pending, pending_from, sp_agent_resume), ack_approval, ask_operator and the ack stdin thread are gone. A tool runs; its exit status is the observation. sp_tool_policy (allow/deny per profile) stays - that is the operator's own configuration, not a question to the operator. - engine.c: SP_EV_APPROVAL, sp_engine_approve, the approve_pending condition variable and the `approval`/`approve` protocol commands are gone. - openai_server.c: the v1 endpoint runs gw_* tools without a gate; the autonomous flag is no longer part of sp_gateway_opts. - a2aserver.c: INPUT_REQUIRED and the resume-by-taskId path are gone; CancelTask stays. An A2A caller has no operator sitting in front of a prompt. - mcpserver.c: --autonomous no longer gates mutating tools; the flag and the config key are accepted for compatibility and documented as changing nothing. - telegram.c: tg_approve and the approval callback are gone; ask is the only dialog, and /cancel inside it still ends the turn. - front ends: protocol.c, curses.c, ui_local.c, ui_remote.c, client.c lose the approval event and the waiting flag. - ack.c stays: it is a plugin API (plugins/kids asks its parent through it), not splinter's own gate. --ack is documented as such. Tests: test_pause.c guarded the pause and now guards the opposite (a mutate and a destructive tool run, nothing is asked). test_a2a_input.sh, test_protocol.sh, test_ui.sh, test_agents_ui.sh, test_gateway.sh, test_gateway_openai.sh, test_agent.c and test_mcpsession.sh were rewritten the same way; splinter_approve must now be absent from the MCP tool list. Every rewritten test was run red against the old behaviour where that was possible. make check: all tests passed (428 s) make check-asan: all 47 test binaries clean make check-tsan: all 47 test binaries free of data races
This commit is contained in:
1 parent
4af38ef91b
commit
74df75d438
41 files changed
+377
-1377
No files matched your search
@@ -61,7 +61,7 @@ splinter refuses to start rather than pretend it can talk to a model.
|
||||
| `--session DIR`, `--session-id ID` | session directory (default `~/.splinter`) and id (default `sess-<time>-<pid>`) |
|
||||
| `--steps N` | tool rounds per turn (default 150) |
|
||||
| `--max-bytes N` | request budget in bytes, 0 = no limit |
|
||||
| `--autonomous` | run mutating tools without asking |
|
||||
| `--autonomous` | accepted for compatibility; splinter does not gate its own tools, so it changes nothing |
|
||||
| `--no-stream` | wait for the whole reply instead of printing it as it arrives |
|
||||
| `--no-stdin` | ignore data on standard input |
|
||||
| `--choose TEXT`, `--delegate ID` | print which delegate fits TEXT; run that delegate with `-p TEXT` and print its observation |
|
||||
@@ -70,7 +70,7 @@ splinter refuses to start rather than pretend it can talk to a model.
|
||||
| `--mcp-server-listen IP`, `--mcp-listen-port N` | serve MCP with this registry's tools (port 8098; beyond loopback it needs `SPLINTER_MCP_TOKEN`) |
|
||||
| `--a2a-server-listen IP`, `--a2a-listen-port N` | serve A2A (JSON-RPC, agent card; port 8099; beyond loopback it needs `SPLINTER_A2A_SERVER_TOKEN`) |
|
||||
| `--openai-listen IP`, `--openai-listen-port N` | serve `/v1/chat/completions` with the local `gw_` tools (port 8097) |
|
||||
| `--ack ID yes|no`, `--ack-list` | answer a pending approval request, or list them (the files under `~/.splinter/ack`; see `docs/GATEWAY.md`) |
|
||||
| `--ack ID yes|no`, `--ack-list` | the ack files under `~/.splinter/ack` — a plugin API (the `kids` plugin asks its parent through it); splinter's own tools do not use it |
|
||||
| `-V, --version`, `-h, --help` | version and this text |
|
||||
|
||||
`--ack`, `--ack-list` and `--live` are read by the parser but missing from the
|
||||
@@ -89,9 +89,9 @@ is appended to `~/.splinter/<id>.jsonl` as one JSON object per line.
|
||||
- Send a request that exceeds the configured byte budget (nothing is sent; the
|
||||
turn fails instead of letting an endpoint truncate silently).
|
||||
- Run a tool that is not in the registry (the model is told, not guessed at).
|
||||
- Run a mutating or destructive tool without operator consent, unless
|
||||
`--autonomous` was given.
|
||||
- Skip a denial silently: the denial becomes the observation the model sees.
|
||||
- Run a tool the agent's policy does not allow: the denial is the observation
|
||||
the model sees, and it names the policy.
|
||||
- Skip a denial silently.
|
||||
|
||||
## Transport protocol
|
||||
|
||||
@@ -156,9 +156,9 @@ where the line runs and how to add another one.
|
||||
|
||||
The engine is front-end neutral: `sp_engine_submit()` starts a turn on its own
|
||||
thread and everything that happens — streamed text, a step's reasoning, a tool
|
||||
call, its observation, an approval question, the answer — becomes an event the
|
||||
front end drains. The curses interface is one front end; `splinter --serve`
|
||||
is another, and it is what a GUI would use.
|
||||
call, its observation, the model's question through `ask`, the answer —
|
||||
becomes an event the front end drains. The curses interface is one front end;
|
||||
`splinter --serve` is another, and it is what a GUI would use.
|
||||
|
||||
```sh
|
||||
splinter --serve # ~/.splinter/splinter.sock
|
||||
@@ -175,7 +175,7 @@ protocol carries everything the front end needs; if `--attach` works, a GUI
|
||||
has no hidden C dependency.
|
||||
|
||||
The protocol is one JSON object per line (`ready`, `state`, `stream`, `step`,
|
||||
`tool`, `tool_result`, `approval`, `turn`, ...; `input`, `say`, `approve`,
|
||||
`tool`, `tool_result`, `ask`, `turn`, ...; `input`, `say`, `answer`,
|
||||
`cancel`, `steer`, `state`, `quit`, `sessions`, `open`). `docs/PROTOCOL.md`
|
||||
has the field list and a sequence diagram; `docs/FRONTENDS.md` has the code
|
||||
layout and how to start a GUI on top of it. The socket is `0600` and the server
|
||||
@@ -211,8 +211,10 @@ harness. The model sees splinter's local tools as `gw_read`, `gw_write`,
|
||||
|
||||
That is how one conversation can act in both places, without a name collision
|
||||
between a local `read` and a caller's `read`. Beyond loopback a
|
||||
`SPLINTER_GATEWAY_TOKEN` is required, and local mutating tools need
|
||||
`--autonomous`. `docs/GATEWAY.md` has the details and examples.
|
||||
`SPLINTER_GATEWAY_TOKEN` is required. The endpoint does not approve anything:
|
||||
a caller who can reach it can run the tools the registry offers, and the
|
||||
operator decides what that is by what they register. `docs/GATEWAY.md` has the
|
||||
details and examples.
|
||||
|
||||
The gateway answers one request at a time, so a client that connects and sends
|
||||
half a request used to hold it for everyone; an unanswered read now ends after
|
||||
@@ -682,9 +684,9 @@ loaded plugin registers
|
||||
(`/goal`, `/quota`, ...) and every program in
|
||||
`~/.splinter/commands/` whose name is a valid Telegram command (lower-case,
|
||||
digits, underscore, at most 32). `/start` and `/help` answer with the short
|
||||
text plus that live list. An approval arrives as a message with **Yes/No
|
||||
buttons**; the callback answers the button, removes the buttons and writes the
|
||||
ack file. The text form `ok <id>` / `no <id>` still works.
|
||||
text plus that live list. The model's `ask` question arrives as a message with
|
||||
the suggested answers as **buttons**; the callback answers it, removes the
|
||||
buttons and hands the text back to the waiting turn.
|
||||
|
||||
The model's markdown is rendered as Telegram HTML: bold, italic, strike,
|
||||
inline code, fenced blocks, links (also `<https://…>`) and quotes become tags,
|
||||
@@ -866,8 +868,8 @@ pick is visible instead of mysterious:
|
||||
|
||||
`--choose "text"` prints that decision without running anything:
|
||||
`coder 7 task mentions 'code'`. The child is splinter in one-shot mode with its
|
||||
own context; the tool is classed destructive, so the operator approves the
|
||||
delegation once.
|
||||
own context. The tool is classed destructive; the registry decides whether
|
||||
the agent has it at all.
|
||||
|
||||
## Resuming
|
||||
|
||||
@@ -987,7 +989,7 @@ The one-server form from before still works:
|
||||
splinter --plugin plugins/mcp/mcp.so
|
||||
|
||||
The other direction, splinter as the server, is `--mcp-server-listen IP`:
|
||||
newline-delimited JSON-RPC over TCP, read tools only unless `--autonomous`.
|
||||
newline-delimited JSON-RPC over TCP, with the tools the registry offers.
|
||||
On 127.x it needs nothing; on any other address it refuses to start without
|
||||
`SPLINTER_MCP_TOKEN`, and every connection must begin with the line
|
||||
`AUTH <token>`. There is no TLS: across a network you do not own, tunnel it.
|
||||
@@ -1010,16 +1012,14 @@ work with splinter itself:
|
||||
splinter_agent switch the profile by name
|
||||
splinter_steer type into the running turn
|
||||
splinter_cancel cancel the running turn
|
||||
splinter_approve answer a pending approval (allow true|false)
|
||||
splinter_answer answer the question the `ask` tool asked
|
||||
|
||||
`say` starts a real turn, so the server needs a provider: give it `-m` (or run
|
||||
`--daemon --mcp-server-listen`, where the Telegram daemon and the MCP server
|
||||
share one engine). A turn that parks on an approval is returned as the
|
||||
question, not as a failure; `splinter_approve` answers it. A turn that parks
|
||||
on the `ask` tool comes back the same way ("the model asks: ..."), and
|
||||
`splinter_answer` delivers the text — both return instead of holding the call
|
||||
until the client gives up. The first line of `splinter_say` is the answer, then
|
||||
share one engine). A turn that parks on the `ask` tool comes back as the
|
||||
question, not as a failure ("the model asks: ..."), and `splinter_answer`
|
||||
delivers the text — it returns instead of holding the call until the client
|
||||
gives up. The first line of `splinter_say` is the answer, then
|
||||
the step and tool-call counts.
|
||||
|
||||
## Tools
|
||||
@@ -1062,13 +1062,11 @@ the prompt.
|
||||
`ask_timeout` or `SPLINTER_ASK_TIMEOUT` to make it shorter.
|
||||
- `echo` (read), `clock` (read).
|
||||
|
||||
Mutate and destructive tools ask first, unless `--autonomous` is set. In the
|
||||
chat the question sits where the prompt is, worded like the transcript row -
|
||||
`Run touch x (destructive)? y/N` - and the turn waits on it; `n` or Esc
|
||||
denies, and the model is told
|
||||
`error: denied by operator`. This used to be dead code in the TUI: the prompt
|
||||
read stdin behind curses' back, so with `--autonomous` off every such tool was
|
||||
silently denied.
|
||||
A tool the registry offers runs. splinter does not put its own question in
|
||||
front of a tool call - it is the operator's program, and what the agent may
|
||||
reach is what the operator registered (the agent's profile, a plugin's gate,
|
||||
the file tools' root). The one question splinter asks is the model's own,
|
||||
through `ask`.
|
||||
|
||||
## Shell escape
|
||||
|
||||
@@ -1098,7 +1096,8 @@ ABI in `docs/PLUGINS.md`.
|
||||
recursive `search` walks), so a link inside the root cannot lead out of it.
|
||||
`run` cannot be fenced at all — a shell opens any path — it only starts in the
|
||||
root.
|
||||
- MCP tools are all `mutate`, so they ask before they run.
|
||||
- MCP tools are all classed `mutate`: the class is metadata (a plugin gate and
|
||||
a profile's `tools:` read it), not a question in front of every call.
|
||||
- The ring only retries on rate limits; a 500 from a broken endpoint also counts
|
||||
as pressure, so a provider that fails hard gets skipped rather than fixed.
|
||||
- No retrieval tiers over old sessions. Compaction is a plugin (lcm); it cuts the
|
||||
|
||||
@@ -55,9 +55,9 @@ beobachten).
|
||||
`a2a_get_task`/`a2a_cancel_task` als Client-Werkzeuge und die Agent Card mit
|
||||
Bearer-Token; die Karte meldet `streaming:true` und
|
||||
`pushNotifications:true`. `GetTask` liefert das Task-Objekt, nicht
|
||||
`{"task":...}`. Ein mutierendes Werkzeug pausiert als
|
||||
`INPUT_REQUIRED`; `SendMessage` mit `taskId` und `yes`/`no` setzt fort,
|
||||
`CancelTask` bricht ab (`test_pause`, `test_a2a_input.sh`). `tests/test_a2a.sh` prueft Task, Message, Token, Fehler,
|
||||
`{"task":...}`. Werkzeuge laufen ohne Rueckfrage; es gibt kein
|
||||
`INPUT_REQUIRED` (an einem A2A-Aufrufer sitzt kein Operator, den man fragen
|
||||
koennte), `CancelTask` bricht ab (`test_pause`, `test_a2a_input.sh`). `tests/test_a2a.sh` prueft Task, Message, Token, Fehler,
|
||||
Streaming und Discovery, `tests/test_a2a_ui.sh` den Streaming-Pfad,
|
||||
`tests/test_a2a_server.sh` den Server und den Client gegen den Server
|
||||
(Discovery, blockierend und streamend), `tests/test_a2a_context.sh` die
|
||||
|
||||
+2
-4
@@ -25,7 +25,6 @@ harness will not start. An error names the line you wrote.
|
||||
core {
|
||||
default = provider/model # a provider, provider/model, or a pool
|
||||
theme = Xresources
|
||||
autonomous = true
|
||||
steps = 60
|
||||
on_limit = wait # a usage limit is waited out (or: fail)
|
||||
max_wait = 2d
|
||||
@@ -146,7 +145,7 @@ model: analyst # what -m takes: provider, pool, provider/model
|
||||
tools: read # all (default) | mutate | read
|
||||
allow: [delegate*] # names (or prefix*) allowed beyond that
|
||||
deny: [run] # never allowed, wins over everything
|
||||
autonomous: false # run mutating tools without asking, or not
|
||||
autonomous: false # kept for compatibility; changes nothing now
|
||||
---
|
||||
The prompt. It is added to the system prompt while this profile is active.
|
||||
```
|
||||
@@ -256,7 +255,7 @@ The `${NAME}` form inserts an environment variable.
|
||||
| `plugin` | a plugin to load, one line each: a shipped plugin's name (`lcm`) or a path; in the block form `plugin NAME { }`, see "Plugins" |
|
||||
| `theme` | `Xresources` (the default: `*foreground`, `*background`, `*color0..15` from `xrdb`), `light`/`dark` (white or black page, black or white text), or a CDE palette name (`splinter --list-themes`) |
|
||||
| `locale` | language code (`de`, `en`); catalogues live in `share/locale` |
|
||||
| `autonomous` | `1` runs mutating/destructive tools without asking |
|
||||
| `autonomous` | kept so an existing file still parses; splinter does not gate its own tools, so the value changes nothing |
|
||||
| `ask_timeout` | seconds an `ask` waits for the operator's answer (default 900); `SPLINTER_ASK_TIMEOUT` overrides it for one run. Shorten it for a run with no operator attached, so a turn does not park for the full wait |
|
||||
| `steps` | tool rounds per turn (default 150) |
|
||||
| `max_bytes` | conversation budget in bytes, `0` = no limit |
|
||||
@@ -464,7 +463,6 @@ single-server environment variable from before still works:
|
||||
endpoint = http://127.0.0.1:8080/v1
|
||||
model = /models/unsloth/Qwen3.6-35B-A3B-MTP-GGUF/Qwen3.6-35B-A3B-UD-Q8_K_XL.gguf
|
||||
theme = Xresources
|
||||
autonomous = 1
|
||||
plugin = lcm
|
||||
plugin = cps
|
||||
plugin = clock
|
||||
|
||||
+2
-3
@@ -52,7 +52,7 @@ onto the JSON `ev` names in `docs/PROTOCOL.md`.
|
||||
|
||||
`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
|
||||
a session is created or how an `ask` question 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
|
||||
@@ -109,7 +109,6 @@ The recommended shape for a GUI:
|
||||
* 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
|
||||
@@ -123,4 +122,4 @@ 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.
|
||||
because an `ask` question that nobody can answer would park the engine forever.
|
||||
+6
-4
@@ -36,7 +36,7 @@ The loop, inside one HTTP request:
|
||||
1. splinter asks its provider with the caller's conversation, the caller's
|
||||
tools **and** its own tools renamed `gw_*`.
|
||||
2. If the model calls `gw_*`, splinter runs it here (read tools always;
|
||||
mutating and destructive ones only with `--autonomous`), appends the
|
||||
mutating and destructive ones as the registry offers them), appends the
|
||||
observation and asks the provider again. The caller never sees these calls.
|
||||
3. If the model calls a caller tool, splinter returns a normal
|
||||
`chat.completion` with `tool_calls` and `finish_reason: "tool_calls"`. The
|
||||
@@ -103,9 +103,11 @@ A caller tool comes back instead:
|
||||
- Beyond loopback the server refuses to start without
|
||||
`SPLINTER_GATEWAY_TOKEN`; with a token set, every request needs
|
||||
`Authorization: Bearer <token>`.
|
||||
- `--autonomous` is the switch that lets the model use `gw_write`, `gw_edit`
|
||||
and `gw_run`. Without it those calls are denied with an observation the model
|
||||
reads, so it can still answer.
|
||||
- the endpoint has no approval step: whatever `gw_*` tool the registry offers
|
||||
is callable (`gw_write`, `gw_run`, ...). `--autonomous` is accepted and
|
||||
changes nothing. Who may reach the endpoint is decided by the token and by
|
||||
what the operator registers, not by a question the gateway could not put to
|
||||
anyone.
|
||||
- There is no TLS. Expose it through ssh, WireGuard or a TLS proxy when the
|
||||
network is not yours.
|
||||
- One request is served at a time; the endpoint is a gateway, not a load
|
||||
|
||||
+21
-12
@@ -138,9 +138,12 @@ t.run = clock_tool;
|
||||
sp_host_add_tool(host, &t);
|
||||
```
|
||||
|
||||
`SP_AUTH_READ` runs. `SP_AUTH_MUTATE` and `SP_AUTH_DESTRUCTIVE` ask the
|
||||
operator unless the harness was started with `--autonomous`. The loop decides
|
||||
this, not the plugin.
|
||||
`SP_AUTH_READ`, `SP_AUTH_MUTATE` and `SP_AUTH_DESTRUCTIVE` all run: splinter
|
||||
does not put its own question in front of a tool call. What an agent may reach
|
||||
is what the operator registered - the profile's allow/deny list, a plugin's
|
||||
own gate (`sp_host_add_gate`), the file tools' root. A plugin that wants a
|
||||
decision can ask for one through the ack files (`ack.h`, as `plugins/kids`
|
||||
does); that is the plugin's business, not the loop's.
|
||||
|
||||
## Compaction
|
||||
|
||||
@@ -376,10 +379,16 @@ same numbers with the pid and the child count.
|
||||
|
||||
## Zustimmung (ack)
|
||||
|
||||
Ein Werkzeug, das etwas aendert, braucht ein Ja von einem Menschen. Der Mensch
|
||||
kann am Terminal sitzen, am Telefon, oder hinter einem Skript - es koennen
|
||||
Ein Mechanismus, mit dem ein **Plugin** ein Ja von einem Menschen einholt. Der
|
||||
Mensch kann am Terminal sitzen, am Telefon, oder hinter einem Skript - es koennen
|
||||
mehrere Kanaele sein, und die erste Antwort gewinnt.
|
||||
|
||||
splinter selbst fragt nicht: seine eigenen Werkzeuge laufen, sobald die Registry
|
||||
sie anbietet (siehe `docs/TOOL-ROOT.md`). Wer eine Rueckfrage will, baut sie in
|
||||
sein Plugin - `plugins/kids` tut das und fragt sein Elternprogramm ueber diesen
|
||||
Weg. `--ack` und `--ack-list` sind entsprechend Plugin-Werkzeuge, keine
|
||||
Freigabe fuer splinters Tools.
|
||||
|
||||
Der Mechanismus ist zwei Dateien, damit nicht zwei Prozesse um dieselben
|
||||
Telegram-Updates streiten:
|
||||
|
||||
@@ -405,7 +414,9 @@ Antworten, aus jedem Prozess:
|
||||
splinter --ack 482915307 no # no, n, nein - jedes andere Wort ist ein Fehler
|
||||
splinter --ack-list
|
||||
|
||||
Ein Timeout ist ein Nein. Stille ist keine Zustimmung.
|
||||
Fuer ein Plugin, das diesen Weg nutzt, gilt: ein Timeout ist ein Nein, Stille
|
||||
ist keine Zustimmung. splinter erzwingt das nicht - es ist die Regel des
|
||||
Plugins, das fragt.
|
||||
|
||||
Das Telegram-Gateway registriert seine Befehle beim Start: die der geladenen
|
||||
Plugins, die Programme in `~/.splinter/commands/` mit gueltigem Telegram-Namen
|
||||
@@ -527,12 +538,10 @@ written to the session's `inbox/` (0600, 8 MiB cap) and the prompt says
|
||||
`[file <name> at <path> (mime, n bytes)]`, because a text model cannot read a
|
||||
PNG but the agent around it can. The same is true for a binary `url` part.
|
||||
|
||||
A mutating tool that needs approval pauses the task in
|
||||
`TASK_STATE_INPUT_REQUIRED`; the assistant call is already in the context, the
|
||||
tool has not run. Another `SendMessage` with the same `taskId` and a
|
||||
`yes`/`no` answer resumes it (`sp_agent_resume`); `CancelTask` cancels it.
|
||||
`SendStreamingMessage` has nowhere to resume, so it denies mutating tools
|
||||
instead of pausing.
|
||||
A turn runs to its end, tools included: there is no approval pause and no
|
||||
`TASK_STATE_INPUT_REQUIRED`. An A2A caller is not sitting in front of a
|
||||
prompt, so a question would only park a task nobody answers. `CancelTask`
|
||||
still cancels a task that is running.
|
||||
|
||||
`SubscribeToTask` sends the state the task has now (status update with its
|
||||
message) and ends the stream: one request at a time means a task cannot make
|
||||
|
||||
+9
-15
@@ -1,8 +1,8 @@
|
||||
# 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
|
||||
needs — the transcript, the reasoning as it streams, tool calls, the model's
|
||||
`ask` question, 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.
|
||||
@@ -43,8 +43,8 @@ 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.
|
||||
cancels the turn (an `ask` question 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
|
||||
@@ -73,8 +73,8 @@ client server
|
||||
|<-- {"ev":"stream","kind":"text",..} |
|
||||
|<-- {"ev":"step","think":..,"text":..}
|
||||
|<-- {"ev":"tool","name":"run",...} |
|
||||
|<-- {"ev":"approval","name":"run",..}| (a mutating tool)
|
||||
| {"cmd":"approve","allow":true} |
|
||||
|<-- {"ev":"ask","text":"which?",...}| (only when the model asks)
|
||||
| {"cmd":"answer","text":"blue"} |
|
||||
|<-- {"ev":"tool_result",...} |
|
||||
|<-- {"ev":"stream","kind":"text",..} |
|
||||
|<-- {"ev":"turn","ok":true,...} |
|
||||
@@ -120,11 +120,6 @@ decided this is a prompt (for example from its own input field).
|
||||
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
|
||||
@@ -244,7 +239,6 @@ with 0.
|
||||
| `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) |
|
||||
@@ -282,8 +276,8 @@ for line in f:
|
||||
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"] == "ask":
|
||||
send({"cmd": "answer", "text": "blue"})
|
||||
elif ev["ev"] == "turn":
|
||||
print()
|
||||
break
|
||||
@@ -307,7 +301,7 @@ 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_answer(eng, "blue"); /* answer an `ask` question */
|
||||
sp_engine_close(eng);
|
||||
```
|
||||
|
||||
|
||||
+3
-4
@@ -104,7 +104,6 @@ Stream.
|
||||
ohne Generator; Zahlen darin sind Stand 2026-11-11
|
||||
- **Letzter Delegate-Fehler**: die letzten 400 Bytes des Kindes stehen im Fehler,
|
||||
ein Limit bencht den Delegate und der nächste übernimmt (`test_delegate`)
|
||||
- **A2A INPUT_REQUIRED**: ein mutierendes Werkzeug pausiert (`sp_agent_run`
|
||||
liefert 2), `SendMessage` mit `taskId` und `yes`/`no` setzt per
|
||||
`sp_agent_resume` fort; `CancelTask` bricht ab (`test_pause`,
|
||||
`test_a2a_input.sh`)
|
||||
- **A2A ohne Rueckfrage**: Werkzeuge laufen, `CancelTask` bricht ab; den
|
||||
frueheren `INPUT_REQUIRED`-Pause-Pfad gibt es nicht mehr, `test_pause` und
|
||||
`test_a2a_input.sh` halten das neue Verhalten fest
|
||||
+10
-10
@@ -32,14 +32,14 @@ the request fits the configured byte budget; otherwise nothing is sent;
|
||||
.It
|
||||
the tool exists in the registry; otherwise the model is told it does not;
|
||||
.It
|
||||
the tool's authorization class allows the call; a mutating or destructive
|
||||
tool asks the operator unless
|
||||
.Fl -autonomous
|
||||
was given.
|
||||
the tool's authorization class allows the call; the class is metadata (a
|
||||
plugin gate and a profile's
|
||||
.Cm tools:
|
||||
read it), splinter asks nothing in front of a call.
|
||||
.El
|
||||
.Pp
|
||||
A denied tool is not skipped silently: the denial is the observation the model
|
||||
receives.
|
||||
A tool the agent's profile does not allow is not skipped silently: the denial
|
||||
is the observation the model receives.
|
||||
.Sh OPTIONS
|
||||
.Bl -tag -width Ds
|
||||
.It Fl p , -prompt Ar TEXT
|
||||
@@ -85,8 +85,8 @@ One-shot mode: wait for the whole reply instead of printing it as it arrives.
|
||||
A transport that can stream uses its streaming method by default; this asks for
|
||||
its plain send method.
|
||||
.It Fl -autonomous
|
||||
Run mutating tools without asking.
|
||||
Read-only tools never ask.
|
||||
Accepted for compatibility; splinter does not gate its own tools, so it
|
||||
changes nothing.
|
||||
.It Fl -steps Ar N
|
||||
Maximum tool rounds per turn (default 150).
|
||||
.It Fl -max-bytes Ar N
|
||||
@@ -193,8 +193,8 @@ exposes the same engine as JSON lines on a Unix socket, where
|
||||
.Pa splinterctl
|
||||
is the reference client and the place to start a GUI from.
|
||||
The engine reports a turn as events \(em streamed text, a step, a tool call, its
|
||||
observation, an approval question, the answer \(em and never calls into a front
|
||||
end.
|
||||
observation, the model's question and its answer \(em and never calls into a
|
||||
front end.
|
||||
.Pp
|
||||
Between the harness and its transport the protocol is simpler, and not a
|
||||
vendor API:
|
||||
|
||||
+7
-31
@@ -391,36 +391,25 @@ struct sp_agent_config {
|
||||
size_t compact_threshold;
|
||||
size_t compact_target;
|
||||
const char *system_prompt;
|
||||
int autonomous; /* 1: run mutate tools without asking */
|
||||
FILE *approval_in; /* NULL with !autonomous means deny */
|
||||
FILE *approval_out;
|
||||
/*
|
||||
* UI path for approval: when set, it is asked instead of the streams, so a
|
||||
* curses program can put the question on the screen instead of reading
|
||||
* stdin behind its own back. Returns 1 to allow, 0 to deny.
|
||||
* Kept so an existing config or profile still parses; splinter does not
|
||||
* gate its own tools, so the value changes nothing. A tool the registry
|
||||
* offers runs.
|
||||
*/
|
||||
int (*approve)(void *ctx, const char *name, int auth,
|
||||
const char *args);
|
||||
void *approve_ctx;
|
||||
int autonomous;
|
||||
/*
|
||||
* A question the `ask` tool puts to the operator. `opts` holds up to
|
||||
* `nopts` suggested answers (may be 0) and the callback writes the
|
||||
* answer into `answer` (cap bytes). Returns 0 on an answer, -1 when
|
||||
* there is nobody to ask -- the tool then says so in its observation
|
||||
* instead of hanging. NULL disables the tool's asking and is the one
|
||||
* difference from an approval, which fails closed.
|
||||
* instead of hanging. NULL disables the tool's asking; splinter does
|
||||
* not ask anything on its own, so a front end that cannot reach a
|
||||
* person simply leaves this unset.
|
||||
*/
|
||||
int (*ask)(void *ctx, const char *text,
|
||||
const char (*opts)[SP_ASK_OPT_LEN], size_t nopts,
|
||||
char *answer, size_t cap);
|
||||
void *ask_ctx;
|
||||
/*
|
||||
* Nonzero: 'approve' is itself the channel (the Telegram gateway asks in
|
||||
* its own chat) and is asked directly. Otherwise the ack mechanism goes
|
||||
* first when SPLINTER_ACK_CHANNELS is set - which in the gateway meant
|
||||
* waiting for an answer that only the blocked gateway could read.
|
||||
*/
|
||||
int approve_is_channel;
|
||||
|
||||
/*
|
||||
* Steering: while a turn runs, the operator may type. The UI cannot
|
||||
@@ -510,23 +499,10 @@ struct sp_agent {
|
||||
struct sp_session_info *sess;
|
||||
struct sp_conv *conv; /* optional: history lives here */
|
||||
struct sp_agent_stats stats;
|
||||
/*
|
||||
* A turn that stopped before a tool ran because a person has to decide.
|
||||
* The assistant message with the calls is already in the conversation;
|
||||
* the calls from pending_from on are still unobserved.
|
||||
*/
|
||||
struct sp_reply pending;
|
||||
size_t pending_from;
|
||||
int has_pending;
|
||||
};
|
||||
|
||||
#define SP_AGENT_INPUT_REQUIRED 2
|
||||
|
||||
int sp_agent_run(struct sp_agent *, const char *user_text,
|
||||
struct sp_buf *answer, struct sp_buf *err);
|
||||
/* Resume a paused turn: decision 1 allows the pending calls, 0 denies. */
|
||||
int sp_agent_resume(struct sp_agent *, int decision, struct sp_buf *answer,
|
||||
struct sp_buf *err);
|
||||
|
||||
/* builtin.c */
|
||||
int sp_builtin_register(struct sp_registry *, const char *root);
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
* The contract, in one paragraph: sp_engine_open() builds the engine from the
|
||||
* command line and the config; sp_engine_input() turns a typed line into
|
||||
* either a command reply or a prompt; sp_engine_submit() starts the turn;
|
||||
* sp_engine_poll() delivers its events; sp_engine_approve(), sp_engine_steer()
|
||||
* sp_engine_poll() delivers its events; sp_engine_answer(), sp_engine_steer()
|
||||
* and sp_engine_cancel() are what a front end may do while it runs.
|
||||
*
|
||||
* This is the C boundary. The same engine is served over a line-oriented JSON
|
||||
@@ -91,13 +91,11 @@ enum sp_ev_kind {
|
||||
SP_EV_TOOL_RESULT, /* a: name, b: observation, n: 1 joins the call */
|
||||
SP_EV_NOTE, /* a: the prompt's one line, "" clears it */
|
||||
SP_EV_ENTRY, /* a: role, b: text */
|
||||
SP_EV_APPROVAL, /* a: name, b: args, n: authorization class */
|
||||
/*
|
||||
* The model asked the operator a question (the `ask` tool). n: the
|
||||
* number of options, a: the text. The options are read with
|
||||
* sp_engine_ask() when the front end draws the dialog; the answer goes
|
||||
* back through sp_engine_answer(). Not an approval: an approval is a
|
||||
* yes/no about something that already happened, this is input.
|
||||
* back through sp_engine_answer().
|
||||
*/
|
||||
SP_EV_ASK,
|
||||
SP_EV_TURN_END, /* n: rc, a: answer, b: error */
|
||||
@@ -138,13 +136,7 @@ struct sp_engine_state {
|
||||
size_t last_ms, last_steps, last_calls; /* the last turn */
|
||||
int busy;
|
||||
int quit; /* /quit was typed */
|
||||
int waiting; /* the turn waits for an approval */
|
||||
/*
|
||||
* The turn waits for an answer to the `ask` tool. Separate from
|
||||
* `waiting` because the answer is text and the way out is different
|
||||
* (sp_engine_answer, not sp_engine_approve) - a caller that only knew
|
||||
* "waiting" would send a yes/no to a question that has options.
|
||||
*/
|
||||
/* the turn waits for an answer to the `ask` tool */
|
||||
int asking;
|
||||
int agents_n;
|
||||
int agent_cur;
|
||||
@@ -267,16 +259,11 @@ int sp_engine_steer(struct sp_engine *, const char *text);
|
||||
* still running (the loop will take it) or when nothing is pending.
|
||||
*/
|
||||
int sp_engine_take_steer(struct sp_engine *, struct sp_buf *out);
|
||||
/* 1 while a tool waits for the operator, with the call in the buffers. */
|
||||
int sp_engine_approval(struct sp_engine *, char *name, size_t namelen,
|
||||
int *auth, char *args, size_t argslen);
|
||||
/* Answer it. 0 when nothing was pending. */
|
||||
int sp_engine_approve(struct sp_engine *, int allow);
|
||||
|
||||
/*
|
||||
* The model asked the operator something (the `ask` tool). Same park/wake as
|
||||
* the approval above, so cancel and Ctrl-C behave the same, but the answer is
|
||||
* text: an option the operator picked, or words they typed.
|
||||
* The model asked the operator something (the `ask` tool). The turn thread
|
||||
* parks on the engine's condition variable and the front end wakes it, so
|
||||
* cancel and Ctrl-C behave the same; the answer is text: an option the
|
||||
* operator picked, or words they typed.
|
||||
*
|
||||
* 1 while a question is pending; `text` is what was asked and up to `max`
|
||||
* options are copied into `opts` (nopts may be NULL). Options are hints for a
|
||||
|
||||
@@ -19,8 +19,9 @@
|
||||
* process_result id -> output, exit status; fails while running
|
||||
* process_kill id -> stop it
|
||||
*
|
||||
* Tasks are shell commands, so starting one is destructive in the same sense that
|
||||
* `run` is: the gate and the approval path see it like any other change.
|
||||
* Tasks are shell commands, so starting one is destructive in the same sense
|
||||
* that `run` is: the authorization class says as much, and splinter runs it
|
||||
* like any other tool the registry offers.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
+1
-14
@@ -92,22 +92,12 @@ ui.status_cache = cache %d%%
|
||||
ui.status_cache_none = cache -
|
||||
ui.status_tps = %d tok/s
|
||||
ui.status_tps_none = - tok/s
|
||||
ui.approve = %s (%s)? y/N
|
||||
auth.mutate = aendert Dateien
|
||||
auth.destructive = zerstoerend
|
||||
role.diff = aenderung
|
||||
|
||||
# Zustimmung (ack): Kanalnachrichten, Antworten des Gateways, Terminal
|
||||
ack.request = Zustimmung %d: %s %s
|
||||
ack.how = Antwort: ok %d / no %d
|
||||
ack.prompt = Zustimmung %d: %s %s (%ds) - 'splinter --ack %d yes' oder hier y/N:
|
||||
ack.pending = Zustimmung %d: %s %s (%ds)
|
||||
ack.approved_by = splinter: zugestimmt von %s
|
||||
ack.none = keine offenen Anfragen
|
||||
ack.granted = Zustimmung erteilt.
|
||||
ack.refused = Abgelehnt.
|
||||
ack.gone = Diese Anfrage gibt es nicht (mehr).
|
||||
ack.waiting = Ich warte noch auf eine Zustimmung: ok %d / no %d
|
||||
|
||||
# openai transport
|
||||
openai.oom = openai: kein Speicher
|
||||
@@ -136,8 +126,6 @@ tg.cmd.tasks = Plandateien zeigen
|
||||
tg.cmd.status = Sitzung, Modell, Tokens, Limits
|
||||
tg.cmd.menu = Menue: Status, Modell, Neu, Hilfe
|
||||
tg.help = Ich bin splinter. Schreib eine Nachricht fuer einen Turn. /goal setzt das Ziel und startet einen Recap-Turn, /tasks zeigt den Plan. Aenderungen fragen hier mit Ja/Nein oder ok <id> / no <id>.
|
||||
ack.yes = Ja
|
||||
ack.no = Nein
|
||||
|
||||
tg.cmd.interview = Profil im Chat anlegen
|
||||
|
||||
@@ -257,8 +245,7 @@ status.compact = Kompaktierung
|
||||
status.compact_value = %zu angewandt, %zu fehlgeschlagen, %zu Runden (Agent %zu)
|
||||
status.embed = Index
|
||||
status.limits = Limits
|
||||
status.limits_value = max_bytes %zu, max_steps %zu%s
|
||||
status.autonomous = , autonom
|
||||
status.limits_value = max_bytes %zu, max_steps %zu
|
||||
status.meter = Messwert
|
||||
status.meter_value = ctx %zu/%zu, Cache %zu, Tok %zu/%zu
|
||||
status.bar = Leiste
|
||||
|
||||
+1
-14
@@ -92,22 +92,12 @@ ui.status_cache = cache %d%%
|
||||
ui.status_cache_none = cache -
|
||||
ui.status_tps = %d tok/s
|
||||
ui.status_tps_none = - tok/s
|
||||
ui.approve = %s (%s)? y/N
|
||||
auth.mutate = changes files
|
||||
auth.destructive = destructive
|
||||
role.diff = diff
|
||||
|
||||
# approvals (ack): channel messages, the gateway's replies, the terminal
|
||||
ack.request = Approval %d: %s %s
|
||||
ack.how = Answer: ok %d / no %d
|
||||
ack.prompt = approval %d: %s %s (%ds) - 'splinter --ack %d yes' or y/N here:
|
||||
ack.pending = approval %d: %s %s (%ds)
|
||||
ack.approved_by = splinter: approved by %s
|
||||
ack.none = no open requests
|
||||
ack.granted = Approved.
|
||||
ack.refused = Refused.
|
||||
ack.gone = There is no such request (any more).
|
||||
ack.waiting = Still waiting for an approval: ok %d / no %d
|
||||
|
||||
# openai transport
|
||||
openai.oom = openai: out of memory
|
||||
@@ -136,8 +126,6 @@ tg.cmd.tasks = Show the plan
|
||||
tg.cmd.status = session, model, tokens, limits
|
||||
tg.cmd.menu = menu: status, model, new, help
|
||||
tg.help = I am splinter. Send a message for a turn. /goal sets the goal and starts a recap turn, /tasks shows the plan. Changes ask here with Yes/No or ok <id> / no <id>.
|
||||
ack.yes = Yes
|
||||
ack.no = No
|
||||
|
||||
tg.cmd.interview = create the profile in the chat
|
||||
|
||||
@@ -258,8 +246,7 @@ status.compact = compact
|
||||
status.compact_value = %zu applied, %zu failed, %zu turns (agent %zu)
|
||||
status.embed = index
|
||||
status.limits = limits
|
||||
status.limits_value = max_bytes %zu, max_steps %zu%s
|
||||
status.autonomous = , autonomous
|
||||
status.limits_value = max_bytes %zu, max_steps %zu
|
||||
status.meter = meter
|
||||
status.meter_value = ctx %zu/%zu, cache %zu, tok %zu/%zu
|
||||
status.bar = bar
|
||||
|
||||
+1
-14
@@ -48,7 +48,6 @@ ui.status_cache = cache %d%%
|
||||
ui.status_cache_none = cache -
|
||||
ui.status_tps = %d tok/s
|
||||
ui.status_tps_none = - tok/s
|
||||
ui.approve = %s (%s)? y/N
|
||||
|
||||
sidebar.session = sessao
|
||||
sidebar.cwd = diretorio
|
||||
@@ -75,8 +74,6 @@ err.plugin_failed = plugin falhou
|
||||
err.thread = nao foi possivel iniciar a thread do turno
|
||||
err.turn_failed = turno falhou
|
||||
|
||||
auth.mutate = altera arquivos
|
||||
auth.destructive = destrutivo
|
||||
|
||||
focus.goal_prompt = Objetivo: %s. Trabalhe nisso. Quando o objetivo estiver totalmente alcancado, termine sua resposta final com a linha unica GOAL_COMPLETE para que o splinter possa marcar como succeeded.
|
||||
focus.reminder = Recap para lembrar: %s. Antes de responder, verifique se o ultimo passo aproximou do objetivo; se nao, diga isso e proponha o proximo passo. Quando o objetivo estiver totalmente alcancado, termine sua resposta final com a linha unica GOAL_COMPLETE.
|
||||
@@ -103,14 +100,7 @@ cli.failed = cli: '%s' falhou (exit %d): %.400s
|
||||
# aprovacoes (ack): mensagens dos canais, respostas do gateway, terminal
|
||||
ack.request = Aprovacao %d: %s %s
|
||||
ack.how = Resposta: ok %d / no %d
|
||||
ack.prompt = aprovacao %d: %s %s (%ds) - 'splinter --ack %d yes' ou y/N aqui:
|
||||
ack.pending = aprovacao %d: %s %s (%ds)
|
||||
ack.approved_by = splinter: aprovado por %s
|
||||
ack.none = nenhum pedido aberto
|
||||
ack.granted = Aprovado.
|
||||
ack.refused = Recusado.
|
||||
ack.gone = Esse pedido nao existe (mais).
|
||||
ack.waiting = Ainda aguardando uma aprovacao: ok %d / no %d
|
||||
|
||||
# openai transport
|
||||
openai.oom = openai: sem memoria
|
||||
@@ -139,8 +129,6 @@ tg.cmd.tasks = Mostrar o plano
|
||||
tg.cmd.status = sessao, modelo, tokens, limites
|
||||
tg.cmd.menu = menu: status, modelo, novo, ajuda
|
||||
tg.help = Eu sou o splinter. Envie uma mensagem para um turno. /goal define a meta e inicia um turno de recap, /tasks mostra o plano. Mudancas perguntam aqui com Sim/Nao ou ok <id> / no <id>.
|
||||
ack.yes = Sim
|
||||
ack.no = Nao
|
||||
|
||||
tg.cmd.interview = criar o perfil no chat
|
||||
|
||||
@@ -260,8 +248,7 @@ status.compact = compactacao
|
||||
status.compact_value = %zu aplicadas, %zu falharam, %zu turnos (agente %zu)
|
||||
status.embed = indice
|
||||
status.limits = limites
|
||||
status.limits_value = max_bytes %zu, max_steps %zu%s
|
||||
status.autonomous = , autonomo
|
||||
status.limits_value = max_bytes %zu, max_steps %zu
|
||||
status.meter = medidor
|
||||
status.meter_value = ctx %zu/%zu, cache %zu, tok %zu/%zu
|
||||
status.bar = barra
|
||||
|
||||
+15
-221
@@ -91,9 +91,6 @@ static struct a2a_ctx g_ctx[A2A_CONTEXTS];
|
||||
static unsigned g_seq;
|
||||
static unsigned g_ctx_seq;
|
||||
static unsigned long g_clock;
|
||||
static char g_ir_task[64]; /* task waiting for a decision */
|
||||
static char g_ir_context[128];
|
||||
static char g_ir_msg[512];
|
||||
static volatile int g_stop;
|
||||
|
||||
static void
|
||||
@@ -545,35 +542,6 @@ task_store(const char *context, const struct sp_buf *answer)
|
||||
return task_store_id(NULL, context, answer);
|
||||
}
|
||||
|
||||
static void
|
||||
task_update(struct a2a_task *t, const char *state, const struct sp_buf *answer)
|
||||
{
|
||||
snprintf(t->state, sizeof(t->state), "%s", state);
|
||||
sp_buf_clear(&t->answer);
|
||||
if (answer != NULL && answer->p != NULL)
|
||||
(void)sp_buf_add(&t->answer, answer->p, answer->len);
|
||||
}
|
||||
|
||||
static struct a2a_task *
|
||||
task_store_state(const char *id, const char *context, const char *state,
|
||||
const char *message)
|
||||
{
|
||||
struct sp_buf empty;
|
||||
struct a2a_task *t;
|
||||
|
||||
if (sp_buf_init(&empty) != 0)
|
||||
return NULL;
|
||||
t = task_store_id(id, context, &empty);
|
||||
sp_buf_free(&empty);
|
||||
if (t != NULL) {
|
||||
snprintf(t->state, sizeof(t->state), "%s", state);
|
||||
sp_buf_clear(&t->answer);
|
||||
if (message != NULL)
|
||||
(void)sp_buf_puts(&t->answer, message);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
static struct a2a_ctx *
|
||||
ctx_get(struct sp_agent *ag, const char *id, int create)
|
||||
{
|
||||
@@ -1054,35 +1022,13 @@ rpc_result(int fd, const char *id, const char *result)
|
||||
* One turn in its own conversation: the system prompt plus this caller's text.
|
||||
* The shared conversation from --chat would let one remote agent read another's
|
||||
* history.
|
||||
*/
|
||||
static int
|
||||
a2a_approve(void *ctx, const char *name, int auth, const char *args)
|
||||
{
|
||||
(void)ctx;
|
||||
(void)auth;
|
||||
snprintf(g_ir_msg, sizeof(g_ir_msg), "approval required: %s %s",
|
||||
name, args != NULL ? args : "");
|
||||
return 2; /* pause; the task becomes INPUT_REQUIRED */
|
||||
}
|
||||
|
||||
static int
|
||||
a2a_deny(void *ctx, const char *name, int auth, const char *args)
|
||||
{
|
||||
(void)ctx;
|
||||
(void)name;
|
||||
(void)auth;
|
||||
(void)args;
|
||||
return 0; /* streaming has nowhere to resume from */
|
||||
}
|
||||
|
||||
/*
|
||||
* A turn (text != NULL) or the resumption of a paused one (text == NULL, with
|
||||
* 'decision'): both under the same copy of the configuration, so that a second
|
||||
* approval inside a resumed task pauses again instead of asking the terminal.
|
||||
*
|
||||
* No approval callback: an A2A caller is not sitting in front of a prompt, and
|
||||
* splinter does not gate its own tools. The registry and the agent's policy say
|
||||
* what may run.
|
||||
*/
|
||||
static int
|
||||
turn_with(struct sp_agent *ag, struct sp_conv *conv, const char *text,
|
||||
int decision, int (*approve)(void *, const char *, int, const char *),
|
||||
struct sp_buf *answer, struct sp_buf *err)
|
||||
{
|
||||
struct sp_agent_config cfg;
|
||||
@@ -1095,17 +1041,11 @@ turn_with(struct sp_agent *ag, struct sp_conv *conv, const char *text,
|
||||
return -1;
|
||||
}
|
||||
cfg = *ag->cfg;
|
||||
cfg.approve = approve;
|
||||
cfg.approve_ctx = NULL;
|
||||
cfg.approve_is_channel = 1;
|
||||
savedcfg = ag->cfg;
|
||||
savedconv = ag->conv;
|
||||
ag->cfg = &cfg;
|
||||
ag->conv = conv;
|
||||
if (text != NULL)
|
||||
rc = sp_agent_run(ag, text, answer, err);
|
||||
else
|
||||
rc = sp_agent_resume(ag, decision, answer, err);
|
||||
rc = sp_agent_run(ag, text, answer, err);
|
||||
ag->conv = savedconv;
|
||||
ag->cfg = savedcfg;
|
||||
return rc;
|
||||
@@ -1113,41 +1053,9 @@ turn_with(struct sp_agent *ag, struct sp_conv *conv, const char *text,
|
||||
|
||||
static int
|
||||
run_turn(struct sp_agent *ag, struct sp_conv *conv, const char *text,
|
||||
int (*approve)(void *, const char *, int, const char *),
|
||||
struct sp_buf *answer, struct sp_buf *err)
|
||||
{
|
||||
return turn_with(ag, conv, text != NULL ? text : "", 0, approve,
|
||||
answer, err);
|
||||
}
|
||||
|
||||
static int
|
||||
resume_turn(struct sp_agent *ag, struct sp_conv *conv, int decision,
|
||||
struct sp_buf *answer, struct sp_buf *err)
|
||||
{
|
||||
g_ir_msg[0] = '\0'; /* the next pause writes its own */
|
||||
return turn_with(ag, conv, NULL, decision, a2a_approve, answer, err);
|
||||
}
|
||||
|
||||
/* yes/ok/ja/sim -> 1, no/nein/nao -> 0, anything else -> -1 */
|
||||
static int
|
||||
decision_of(const char *text)
|
||||
{
|
||||
if (text == NULL)
|
||||
return -1;
|
||||
while (*text == ' ' || *text == '\t')
|
||||
text++;
|
||||
if (strncasecmp(text, "yes", 3) == 0 ||
|
||||
strncasecmp(text, "ja", 2) == 0 ||
|
||||
strncasecmp(text, "ok", 2) == 0 ||
|
||||
strncasecmp(text, "sim", 3) == 0 ||
|
||||
(text[0] == 'y' && (text[1] == '\0' || text[1] == ' ')))
|
||||
return 1;
|
||||
if (strncasecmp(text, "nein", 4) == 0 ||
|
||||
strncasecmp(text, "no", 2) == 0 ||
|
||||
strncasecmp(text, "nao", 3) == 0 ||
|
||||
(text[0] == 'n' && (text[1] == '\0' || text[1] == ' ')))
|
||||
return 0;
|
||||
return -1;
|
||||
return turn_with(ag, conv, text != NULL ? text : "", answer, err);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1351,11 +1259,6 @@ handle_stream(int fd, struct sp_agent *ag, const char *id,
|
||||
return;
|
||||
}
|
||||
sse_start(fd);
|
||||
if (g_ir_task[0] != '\0') {
|
||||
sse_error(&o, -32004, "a task is waiting for input; use "
|
||||
"SendMessage with its taskId");
|
||||
goto out;
|
||||
}
|
||||
message = mcp_arg(params, "message");
|
||||
parts = message != NULL ? mcp_arg(message, "parts") : NULL;
|
||||
{
|
||||
@@ -1401,7 +1304,7 @@ handle_stream(int fd, struct sp_agent *ag, const char *id,
|
||||
cfg.stream_ctx = &o;
|
||||
ag->cfg = &cfg;
|
||||
rc = run_turn(ag, &cx->conv, text.p != NULL ? text.p : "",
|
||||
a2a_deny, &answer, &err);
|
||||
&answer, &err);
|
||||
ag->cfg = saved;
|
||||
if (rc != 0) {
|
||||
sse_error(&o, -32603, err.p != NULL && err.len > 0 ?
|
||||
@@ -1436,7 +1339,7 @@ handle_rpc(int fd, struct sp_agent *ag, const char *payload)
|
||||
{
|
||||
mcp_json *v;
|
||||
const mcp_json *params, *message, *parts;
|
||||
const char *method, *ctx, *task_id;
|
||||
const char *method, *ctx;
|
||||
char id[128], ctxbuf[128];
|
||||
struct sp_buf text, answer, err, out, perr;
|
||||
struct a2a_ctx *cx;
|
||||
@@ -1469,7 +1372,6 @@ handle_rpc(int fd, struct sp_agent *ag, const char *payload)
|
||||
goto send_out;
|
||||
}
|
||||
message = mcp_arg(params, "message");
|
||||
task_id = message != NULL ? mcp_arg_string(message, "taskId") : NULL;
|
||||
parts = message != NULL ? mcp_arg(message, "parts") : NULL;
|
||||
{
|
||||
char inbox[700];
|
||||
@@ -1489,63 +1391,6 @@ handle_rpc(int fd, struct sp_agent *ag, const char *payload)
|
||||
rpc_error(fd, id, rc == -2 ? -32003 : -32602, perr.p);
|
||||
goto send_out;
|
||||
}
|
||||
if (g_ir_task[0] != '\0') {
|
||||
struct a2a_task *t;
|
||||
int decision;
|
||||
|
||||
if (task_id == NULL ||
|
||||
strcmp(task_id, g_ir_task) != 0) {
|
||||
rpc_error(fd, id, -32004, "a task is waiting "
|
||||
"for input; send its taskId");
|
||||
goto send_out;
|
||||
}
|
||||
t = task_find(task_id);
|
||||
if (t == NULL) {
|
||||
rpc_error(fd, id, -32001, "task not found");
|
||||
goto send_out;
|
||||
}
|
||||
decision = decision_of(text.p);
|
||||
if (decision < 0) {
|
||||
rpc_error(fd, id, -32602, "expected yes or no");
|
||||
goto send_out;
|
||||
}
|
||||
cx = ctx_get(ag, t->context, 1);
|
||||
if (cx == NULL) {
|
||||
rpc_error(fd, id, -32603, "out of memory "
|
||||
"for a context");
|
||||
goto send_out;
|
||||
}
|
||||
rc = resume_turn(ag, &cx->conv, decision, &answer,
|
||||
&err);
|
||||
if (rc == SP_AGENT_INPUT_REQUIRED) {
|
||||
sp_buf_clear(&t->answer);
|
||||
(void)sp_buf_puts(&t->answer, g_ir_msg);
|
||||
task_json(&out, t);
|
||||
rpc_result(fd, id, out.p != NULL ? out.p : "{}");
|
||||
goto send_out;
|
||||
}
|
||||
if (rc != 0) {
|
||||
sp_reply_free(&ag->pending);
|
||||
ag->has_pending = 0;
|
||||
ag->pending_from = 0;
|
||||
g_ir_task[0] = '\0';
|
||||
g_ir_context[0] = '\0';
|
||||
g_ir_msg[0] = '\0';
|
||||
task_update(t, "TASK_STATE_FAILED", &err);
|
||||
rpc_error(fd, id, -32603,
|
||||
err.p != NULL && err.len > 0 ? err.p :
|
||||
"the turn failed");
|
||||
goto send_out;
|
||||
}
|
||||
task_update(t, "TASK_STATE_COMPLETED", &answer);
|
||||
g_ir_task[0] = '\0';
|
||||
g_ir_context[0] = '\0';
|
||||
g_ir_msg[0] = '\0';
|
||||
task_json(&out, t);
|
||||
rpc_result(fd, id, out.p != NULL ? out.p : "{}");
|
||||
push_task(&push, t);
|
||||
goto send_out;
|
||||
}
|
||||
ctx = mcp_arg_string(message, "contextId");
|
||||
if (ctx == NULL || *ctx == '\0') {
|
||||
snprintf(ctxbuf, sizeof(ctxbuf), "ctx-%d-%u",
|
||||
@@ -1561,29 +1406,8 @@ handle_rpc(int fd, struct sp_agent *ag, const char *payload)
|
||||
rpc_error(fd, id, -32603, "out of memory for a context");
|
||||
goto send_out;
|
||||
}
|
||||
g_ir_task[0] = '\0';
|
||||
g_ir_context[0] = '\0';
|
||||
g_ir_msg[0] = '\0';
|
||||
rc = run_turn(ag, &cx->conv, text.p != NULL ? text.p : "",
|
||||
a2a_approve, &answer, &err);
|
||||
if (rc == SP_AGENT_INPUT_REQUIRED) {
|
||||
char tid[64];
|
||||
struct a2a_task *t;
|
||||
|
||||
gen_task_id(tid, sizeof(tid));
|
||||
t = task_store_state(tid, ctx,
|
||||
"TASK_STATE_INPUT_REQUIRED", g_ir_msg);
|
||||
if (t == NULL) {
|
||||
rpc_error(fd, id, -32603, "out of memory");
|
||||
goto send_out;
|
||||
}
|
||||
snprintf(g_ir_task, sizeof(g_ir_task), "%s", t->id);
|
||||
snprintf(g_ir_context, sizeof(g_ir_context), "%s",
|
||||
t->context);
|
||||
task_json(&out, t);
|
||||
rpc_result(fd, id, out.p != NULL ? out.p : "{}");
|
||||
goto send_out;
|
||||
}
|
||||
&answer, &err);
|
||||
if (rc != 0) {
|
||||
rpc_error(fd, id, -32603,
|
||||
err.p != NULL && err.len > 0 ? err.p :
|
||||
@@ -1677,34 +1501,12 @@ send_out:
|
||||
mcp_json_free(v);
|
||||
return;
|
||||
}
|
||||
if (strcmp(t->state, "TASK_STATE_INPUT_REQUIRED") == 0) {
|
||||
struct sp_buf out, msg;
|
||||
|
||||
if (ag->has_pending) {
|
||||
sp_reply_free(&ag->pending);
|
||||
ag->has_pending = 0;
|
||||
ag->pending_from = 0;
|
||||
}
|
||||
if (strcmp(g_ir_task, t->id) == 0)
|
||||
g_ir_task[0] = '\0';
|
||||
g_ir_context[0] = '\0';
|
||||
g_ir_msg[0] = '\0';
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
sp_buf_init(&msg);
|
||||
sp_buf_puts(&msg, "task canceled");
|
||||
task_update(t, "TASK_STATE_CANCELED", &msg);
|
||||
sp_buf_free(&msg);
|
||||
memset(&out, 0, sizeof(out));
|
||||
if (sp_buf_init(&out) != 0) {
|
||||
rpc_error(fd, id, -32603, "out of memory");
|
||||
} else {
|
||||
task_json(&out, t);
|
||||
rpc_result(fd, id, out.p != NULL ? out.p : "{}");
|
||||
sp_buf_free(&out);
|
||||
}
|
||||
} else {
|
||||
rpc_error(fd, id, -32002, "task is already completed");
|
||||
}
|
||||
/*
|
||||
* A task no longer parks in INPUT_REQUIRED: a tool the registry
|
||||
* offers runs, so a task either completes or is cancelled while
|
||||
* it runs. Cancelling a finished task is an error.
|
||||
*/
|
||||
rpc_error(fd, id, -32002, "task is already completed");
|
||||
} else if (strcmp(method, "SubscribeToTask") == 0) {
|
||||
handle_subscribe(fd, id, params);
|
||||
} else if (strcmp(method, "CreateTaskPushNotificationConfig") == 0) {
|
||||
@@ -2031,14 +1833,6 @@ sp_a2a_serve(struct sp_agent *ag, const char *ip, int port, struct sp_buf *err)
|
||||
}
|
||||
for (i = 0; i < 32; i++)
|
||||
g_pcfg[i].used = 0;
|
||||
if (ag->has_pending) {
|
||||
sp_reply_free(&ag->pending);
|
||||
ag->has_pending = 0;
|
||||
ag->pending_from = 0;
|
||||
}
|
||||
g_ir_task[0] = '\0';
|
||||
g_ir_context[0] = '\0';
|
||||
g_ir_msg[0] = '\0';
|
||||
}
|
||||
fprintf(stderr, "splinter: A2A server stopped\n");
|
||||
return 0;
|
||||
|
||||
+16
-261
@@ -34,18 +34,6 @@
|
||||
#include "mcp.h"
|
||||
#include "usage.h"
|
||||
|
||||
/*
|
||||
* The terminal as one more approval channel. It must be stoppable: when the
|
||||
* phone answers first, or the request times out, the agent goes on, and a
|
||||
* thread still sitting in fgets() would hold it until somebody pressed Enter.
|
||||
* So it polls the terminal together with a pipe, and a byte on the pipe ends it.
|
||||
*/
|
||||
struct ack_thread {
|
||||
int id;
|
||||
FILE *in;
|
||||
int stop; /* read end of the stop pipe */
|
||||
};
|
||||
|
||||
|
||||
struct quota_set_ctx { long status; long now; long retry_after_s; };
|
||||
static void quota_set_fn(struct sp_prov_state *st, void *p) {
|
||||
@@ -88,149 +76,9 @@ sp_ask_wait_s(void)
|
||||
return SP_ASK_WAIT_S;
|
||||
}
|
||||
|
||||
static void *
|
||||
ack_stdin_thread(void *arg)
|
||||
{
|
||||
struct ack_thread *at = arg;
|
||||
char line[64];
|
||||
struct pollfd pfd[2];
|
||||
|
||||
pfd[0].fd = fileno(at->in);
|
||||
pfd[0].events = POLLIN;
|
||||
pfd[1].fd = at->stop;
|
||||
pfd[1].events = POLLIN;
|
||||
for (;;) {
|
||||
pfd[0].revents = pfd[1].revents = 0;
|
||||
if (poll(pfd, 2, -1) < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (pfd[1].revents != 0)
|
||||
break; /* answered elsewhere or expired */
|
||||
if (pfd[0].revents != 0) {
|
||||
if (fgets(line, sizeof(line), at->in) != NULL)
|
||||
(void)sp_ack_answer(at->id,
|
||||
line[0] == 'y' || line[0] == 'Y', "terminal");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Approval through the ack mechanism: the request becomes a file, the channels are
|
||||
* fired, and whoever answers first wins - the person at this terminal, a phone, or
|
||||
* a script. Without any channel configured this falls back to the plain prompt, so
|
||||
* nothing starts hanging that used to be synchronous.
|
||||
*/
|
||||
static int
|
||||
ack_approval(struct sp_agent *a, const struct sp_tool *t, const char *args)
|
||||
{
|
||||
const char *chans = getenv("SPLINTER_ACK_CHANNELS");
|
||||
const char *ttl_s = getenv("SPLINTER_ACK_TTL");
|
||||
struct sp_buf log;
|
||||
char who[64] = "";
|
||||
int id, ttl, yes;
|
||||
pthread_t th;
|
||||
int th_started = 0;
|
||||
int stop[2] = { -1, -1 };
|
||||
struct ack_thread at;
|
||||
|
||||
if (chans == NULL || *chans == '\0')
|
||||
return -1;
|
||||
ttl = ttl_s != NULL && *ttl_s != '\0' ? atoi(ttl_s) : 120;
|
||||
if (ttl <= 0)
|
||||
ttl = 120;
|
||||
id = sp_ack_request(t->name, args,
|
||||
t->auth == SP_AUTH_MUTATE ? "mutate" : "destructive", ttl);
|
||||
if (id < 0) {
|
||||
/* fail closed, but say why: a silent "denied" sends people hunting */
|
||||
fprintf(stderr, "splinter: cannot file an approval request in %s "
|
||||
"(missing, not yours, or writable by others): %s denied\n",
|
||||
sp_ack_dir() != NULL ? sp_ack_dir() : "(no ack dir)", t->name);
|
||||
return 0;
|
||||
}
|
||||
if (sp_buf_init(&log) == 0) {
|
||||
(void)sp_ack_notify(id, &log);
|
||||
if (log.p != NULL && *log.p != '\0')
|
||||
fprintf(stderr, "%s", log.p);
|
||||
sp_buf_free(&log);
|
||||
}
|
||||
if (a->cfg->approval_out != NULL)
|
||||
fprintf(a->cfg->approval_out, sp_td("ack.prompt",
|
||||
"approval %d: %s %s (%ds) - 'splinter --ack %d yes' or y/N "
|
||||
"here: "), id, t->name, args, ttl, id);
|
||||
else
|
||||
fprintf(stderr, sp_td("ack.pending", "approval %d: %s %s (%ds)\n"),
|
||||
id, t->name, args, ttl);
|
||||
if (a->cfg->approval_out != NULL)
|
||||
fflush(a->cfg->approval_out);
|
||||
|
||||
/* the terminal is one more channel, not a special case */
|
||||
if (a->cfg->approval_in != NULL && pipe(stop) == 0) {
|
||||
at.id = id;
|
||||
at.in = a->cfg->approval_in;
|
||||
at.stop = stop[0];
|
||||
if (pthread_create(&th, NULL, ack_stdin_thread, &at) == 0)
|
||||
th_started = 1;
|
||||
}
|
||||
yes = sp_ack_wait(id, ttl, who, sizeof(who));
|
||||
if (th_started) {
|
||||
(void)write(stop[1], "x", 1);
|
||||
(void)pthread_join(th, NULL);
|
||||
}
|
||||
/*
|
||||
* pipe(2) fills both ends or neither, and nothing between here and the
|
||||
* pipe() call can return, so one guard covers both. gcc's -fanalyzer
|
||||
* reports stop[1] as leaked here anyway (checked 2026-11-22): the
|
||||
* warning reproduces with a separate guard per fd too, so it is the
|
||||
* analyzer losing track of the descriptor through the call to
|
||||
* sp_ack_wait(), not a path that skips the close.
|
||||
*/
|
||||
if (stop[0] >= 0) {
|
||||
close(stop[0]);
|
||||
close(stop[1]);
|
||||
}
|
||||
if (yes && who[0] != '\0')
|
||||
fprintf(stderr, sp_td("ack.approved_by",
|
||||
"splinter: approved by %s\n"), who);
|
||||
return yes;
|
||||
}
|
||||
|
||||
static int
|
||||
ask_operator(struct sp_agent *a, const struct sp_tool *t, const char *args)
|
||||
{
|
||||
char line[64];
|
||||
int verdict;
|
||||
|
||||
if (a->cfg->approve != NULL && a->cfg->approve_is_channel)
|
||||
return a->cfg->approve(a->cfg->approve_ctx, t->name, (int)t->auth,
|
||||
args);
|
||||
verdict = ack_approval(a, t, args);
|
||||
if (verdict >= 0)
|
||||
return verdict;
|
||||
|
||||
if (a->cfg->approve != NULL)
|
||||
return a->cfg->approve(a->cfg->approve_ctx, t->name, (int)t->auth,
|
||||
args);
|
||||
if (a->cfg->approval_out != NULL) {
|
||||
fprintf(a->cfg->approval_out, "run %s (%s) %s? [y/N] ", t->name,
|
||||
t->auth == SP_AUTH_MUTATE ? "mutate" : "destructive", args);
|
||||
fflush(a->cfg->approval_out);
|
||||
}
|
||||
if (a->cfg->approval_in == NULL)
|
||||
return 0; /* fail closed */
|
||||
if (fgets(line, sizeof(line), a->cfg->approval_in) == NULL)
|
||||
return 0;
|
||||
return (line[0] == 'y' || line[0] == 'Y');
|
||||
}
|
||||
|
||||
/* The observation is always written: an error is an observation too. */
|
||||
/*
|
||||
* 0 = ran (obs is the observation), 1 = a person has to decide first (nothing
|
||||
* ran). have_decision/decision are used by a resume: the decision applies to
|
||||
* the pending calls instead of asking again.
|
||||
* 0 = ran (obs is the observation), -1 = the turn must end. There is no
|
||||
* "a person has to decide first": splinter does not gate its own tools.
|
||||
*/
|
||||
/* a name against a comma list of names; a trailing '*' is a prefix */
|
||||
static int
|
||||
@@ -266,7 +114,7 @@ policy_allows(const struct sp_tool_policy *p, const struct sp_tool *t)
|
||||
|
||||
static int
|
||||
run_one_call(struct sp_agent *a, const struct sp_toolcall *call,
|
||||
struct sp_buf *obs, int have_decision, int decision)
|
||||
struct sp_buf *obs)
|
||||
{
|
||||
const struct sp_tool *t;
|
||||
int verdict;
|
||||
@@ -310,29 +158,22 @@ run_one_call(struct sp_agent *a, const struct sp_toolcall *call,
|
||||
}
|
||||
}
|
||||
|
||||
if (t->auth != SP_AUTH_READ && !a->cfg->autonomous) {
|
||||
if (have_decision)
|
||||
verdict = decision ? 1 : 0;
|
||||
else {
|
||||
verdict = ask_operator(a, t, call->args);
|
||||
if (verdict == 2)
|
||||
return 1; /* pause; nothing has run */
|
||||
}
|
||||
if (!verdict) {
|
||||
a->stats.denied++;
|
||||
sp_buf_puts(obs, "error: denied by operator");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* No approval gate. A tool the registry offers runs -- the operator
|
||||
* decides what the agent may reach by what they put in the registry
|
||||
* (the agent's policy, a plugin's gate, the file tools' root), not by
|
||||
* answering a question the program invents. The one question splinter
|
||||
* still asks is the model's own, through `ask`.
|
||||
*/
|
||||
if (t->run(call->args, obs, t->ctx) != 0)
|
||||
sp_buf_printf(obs, "error: tool '%s' failed", call->name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* run one call and add its observation; 1 means approval is still required */
|
||||
/* run one call and add its observation; -1 ends the turn */
|
||||
static int
|
||||
run_one(struct sp_agent *a, struct sp_conv *conv,
|
||||
const struct sp_toolcall *call, int have_decision, int decision,
|
||||
const struct sp_toolcall *call,
|
||||
struct sp_buf *obs, struct sp_buf *err)
|
||||
{
|
||||
struct sp_msg tm;
|
||||
@@ -344,7 +185,7 @@ run_one(struct sp_agent *a, struct sp_conv *conv,
|
||||
if (a->cfg->tool_begin != NULL)
|
||||
a->cfg->tool_begin(a->cfg->tool_begin_ctx, call->name,
|
||||
call->args);
|
||||
rc = run_one_call(a, call, obs, have_decision, decision);
|
||||
rc = run_one_call(a, call, obs);
|
||||
if (rc != 0)
|
||||
return rc;
|
||||
if (a->cfg->tool_end != NULL)
|
||||
@@ -749,22 +590,8 @@ rollback:
|
||||
}
|
||||
}
|
||||
|
||||
/* the assistant message that opened the newest tool round, or conv->n */
|
||||
static size_t
|
||||
round_mark(const struct sp_conv *conv)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = conv->n; i > 0; i--)
|
||||
if (conv->v[i - 1].role != NULL &&
|
||||
strcmp(conv->v[i - 1].role, "assistant") == 0 &&
|
||||
conv->v[i - 1].calls != NULL)
|
||||
return i - 1;
|
||||
return conv->n;
|
||||
}
|
||||
|
||||
static int
|
||||
agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision,
|
||||
agent_loop(struct sp_agent *a, struct sp_conv *conv,
|
||||
struct sp_buf *answer, struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf obs;
|
||||
@@ -777,40 +604,6 @@ agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision,
|
||||
sp_buf_puts(err, "agent: out of memory");
|
||||
return -1;
|
||||
}
|
||||
if (resume) {
|
||||
if (!a->has_pending || a->pending_from > a->pending.ncalls) {
|
||||
sp_buf_puts(err, "agent: no turn is waiting for input");
|
||||
goto out;
|
||||
}
|
||||
for (i = a->pending_from; i < a->pending.ncalls; i++) {
|
||||
int r = run_one(a, conv, &a->pending.calls[i], 1,
|
||||
decision, &obs, err);
|
||||
|
||||
if (r > 0) {
|
||||
/* calls before i ran: never run them twice */
|
||||
a->pending_from = i;
|
||||
sp_buf_puts(err, "agent: approval needed again");
|
||||
goto out;
|
||||
}
|
||||
if (r != 0) {
|
||||
/*
|
||||
* The round cannot be resumed cleanly: answer
|
||||
* what is left and drop the pending turn, so a
|
||||
* retry does not run executed calls again.
|
||||
*/
|
||||
close_round(conv, &a->pending,
|
||||
round_mark(conv));
|
||||
sp_reply_free(&a->pending);
|
||||
a->has_pending = 0;
|
||||
a->pending_from = 0;
|
||||
goto out;
|
||||
}
|
||||
a->pending_from = i + 1;
|
||||
}
|
||||
sp_reply_free(&a->pending);
|
||||
a->has_pending = 0;
|
||||
a->pending_from = 0;
|
||||
}
|
||||
/*
|
||||
* max_steps counts tool rounds, not requests: a turn with N tool rounds
|
||||
* needs N+1 requests, because the last one carries the answer and runs
|
||||
@@ -1173,22 +966,8 @@ agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision,
|
||||
}
|
||||
}
|
||||
for (i = 0; i < reply.ncalls; i++) {
|
||||
int r = run_one(a, conv, &reply.calls[i], 0, 0, &obs,
|
||||
err);
|
||||
int r = run_one(a, conv, &reply.calls[i], &obs, err);
|
||||
|
||||
if (r == 1) {
|
||||
/*
|
||||
* Pause with the assistant message already in the
|
||||
* conversation; the calls from here on have no
|
||||
* observation yet. The next run resumes them.
|
||||
*/
|
||||
a->pending = reply;
|
||||
memset(&reply, 0, sizeof(reply));
|
||||
a->pending_from = i;
|
||||
a->has_pending = 1;
|
||||
rc = SP_AGENT_INPUT_REQUIRED;
|
||||
goto out;
|
||||
}
|
||||
if (r != 0) {
|
||||
close_round(conv, &reply, mark);
|
||||
sp_reply_free(&reply);
|
||||
@@ -1217,10 +996,6 @@ sp_agent_run(struct sp_agent *a, const char *user_text, struct sp_buf *answer,
|
||||
return -1;
|
||||
if (a->cfg->max_steps == 0)
|
||||
return -1;
|
||||
if (a->has_pending) {
|
||||
sp_buf_puts(err, "agent: a turn is waiting for input");
|
||||
return -1;
|
||||
}
|
||||
if (a->conv != NULL) {
|
||||
conv = a->conv;
|
||||
} else {
|
||||
@@ -1258,29 +1033,9 @@ sp_agent_run(struct sp_agent *a, const char *user_text, struct sp_buf *answer,
|
||||
sp_conv_free(&local);
|
||||
return -1;
|
||||
}
|
||||
rc = agent_loop(a, conv, 0, 0, answer, err);
|
||||
if (rc == SP_AGENT_INPUT_REQUIRED && conv == &local) {
|
||||
/* nowhere to resume from: do not leave a dangling pending turn */
|
||||
sp_reply_free(&a->pending);
|
||||
a->has_pending = 0;
|
||||
a->pending_from = 0;
|
||||
sp_buf_puts(err, "agent: input required without a persistent "
|
||||
"conversation");
|
||||
rc = -1;
|
||||
}
|
||||
rc = agent_loop(a, conv, answer, err);
|
||||
if (conv == &local)
|
||||
sp_conv_free(&local);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int
|
||||
sp_agent_resume(struct sp_agent *a, int decision, struct sp_buf *answer,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
if (a == NULL || a->cfg == NULL || a->conv == NULL || !a->has_pending) {
|
||||
sp_buf_puts(err, "agent: no turn is waiting for input");
|
||||
return -1;
|
||||
}
|
||||
sp_buf_clear(answer);
|
||||
return agent_loop(a, a->conv, 1, decision ? 1 : 0, answer, err);
|
||||
}
|
||||
+4
-87
@@ -147,18 +147,10 @@ struct sp_engine {
|
||||
size_t steers;
|
||||
long max_wait; /* seconds, 0: 12 h */
|
||||
|
||||
/* the approval a tool is parked on */
|
||||
int approve_pending;
|
||||
int approve_result;
|
||||
char approve_name[64];
|
||||
char approve_args[256];
|
||||
int approve_auth;
|
||||
/*
|
||||
* The question a tool is parked on (ask_operator). Same condition
|
||||
* variable and the same "the thread parks, the front end answers"
|
||||
* shape as the approval above, because a second wait mechanism would
|
||||
* be a second place to get cancel and Ctrl-C wrong. What differs is
|
||||
* the answer: free text (or a picked option) instead of yes/no.
|
||||
* The question a tool is parked on (ask_operator). The turn thread
|
||||
* parks on the condition variable and the front end answers; one wait
|
||||
* mechanism, so cancel and Ctrl-C are handled in one place.
|
||||
*/
|
||||
int ask_pending;
|
||||
char ask_text[SP_ASK_MAX];
|
||||
@@ -325,7 +317,6 @@ sp_engine_state(struct sp_engine *e, struct sp_engine_state *st)
|
||||
st->last_calls = e->last_calls;
|
||||
st->busy = e->running || e->ext_busy;
|
||||
st->quit = e->quit;
|
||||
st->waiting = e->approve_pending;
|
||||
st->asking = e->ask_pending;
|
||||
st->agents_n = (int)e->agents.n;
|
||||
st->agent_cur = e->agent_cur;
|
||||
@@ -1080,76 +1071,6 @@ engine_tool_end(void *ctx, const char *name, const char *obs)
|
||||
engine_apply_pending_model(e);
|
||||
}
|
||||
|
||||
/*
|
||||
* A tool needs permission. The turn thread parks on the engine's condition
|
||||
* variable; the front end asks the operator and calls sp_engine_approve().
|
||||
* Ctrl-C must work here too: the wait ends on cancel and the call is denied.
|
||||
*/
|
||||
static int
|
||||
engine_approve(void *ctx, const char *name, int auth, const char *args)
|
||||
{
|
||||
struct sp_engine *e = ctx;
|
||||
int answer;
|
||||
|
||||
pthread_mutex_lock(&e->lock);
|
||||
snprintf(e->approve_name, sizeof(e->approve_name), "%s",
|
||||
name != NULL ? name : "?");
|
||||
snprintf(e->approve_args, sizeof(e->approve_args), "%.200s",
|
||||
args != NULL ? args : "");
|
||||
e->approve_auth = auth;
|
||||
e->approve_result = 0;
|
||||
e->approve_pending = 1;
|
||||
pthread_mutex_unlock(&e->lock);
|
||||
ev_push(e, SP_EV_APPROVAL, auth, e->approve_name, e->approve_args);
|
||||
pthread_mutex_lock(&e->lock);
|
||||
while (e->approve_pending && !e->cancel)
|
||||
pthread_cond_wait(&e->cond, &e->lock);
|
||||
answer = e->cancel ? 0 : e->approve_result;
|
||||
e->approve_pending = 0;
|
||||
pthread_mutex_unlock(&e->lock);
|
||||
return answer;
|
||||
}
|
||||
|
||||
int
|
||||
sp_engine_approval(struct sp_engine *e, char *name, size_t namelen,
|
||||
int *auth, char *args, size_t argslen)
|
||||
{
|
||||
int pending;
|
||||
|
||||
if (e == NULL)
|
||||
return 0;
|
||||
pthread_mutex_lock(&e->lock);
|
||||
pending = e->approve_pending;
|
||||
if (pending) {
|
||||
if (name != NULL && namelen > 0)
|
||||
snprintf(name, namelen, "%s", e->approve_name);
|
||||
if (args != NULL && argslen > 0)
|
||||
snprintf(args, argslen, "%s", e->approve_args);
|
||||
if (auth != NULL)
|
||||
*auth = e->approve_auth;
|
||||
}
|
||||
pthread_mutex_unlock(&e->lock);
|
||||
return pending;
|
||||
}
|
||||
|
||||
int
|
||||
sp_engine_approve(struct sp_engine *e, int allow)
|
||||
{
|
||||
int pending;
|
||||
|
||||
if (e == NULL)
|
||||
return 0;
|
||||
pthread_mutex_lock(&e->lock);
|
||||
pending = e->approve_pending;
|
||||
if (pending) {
|
||||
e->approve_pending = 0;
|
||||
e->approve_result = allow != 0;
|
||||
pthread_cond_broadcast(&e->cond);
|
||||
}
|
||||
pthread_mutex_unlock(&e->lock);
|
||||
return pending;
|
||||
}
|
||||
|
||||
/*
|
||||
* The `ask` tool: the model puts a question to the operator. The channel is
|
||||
* the agent's `ask` callback, not a second event queue -- a tool that reaches
|
||||
@@ -1732,8 +1653,6 @@ engine_submit(struct sp_engine *e, const char *text)
|
||||
e->turn_cfg.steer_ctx = e;
|
||||
e->turn_cfg.stream = engine_stream_cb;
|
||||
e->turn_cfg.stream_ctx = e;
|
||||
e->turn_cfg.approve = engine_approve;
|
||||
e->turn_cfg.approve_ctx = e;
|
||||
e->turn_cfg.ask = engine_ask;
|
||||
e->turn_cfg.ask_ctx = e;
|
||||
e->turn_cfg.tool_begin = engine_tool_begin;
|
||||
@@ -1760,7 +1679,6 @@ engine_submit(struct sp_engine *e, const char *text)
|
||||
e->agents.v[e->agent_cur].autonomous;
|
||||
e->cancel = 0;
|
||||
e->steers = 0;
|
||||
e->approve_pending = 0;
|
||||
sp_buf_clear(&e->think);
|
||||
sp_buf_clear(&e->live);
|
||||
clock_gettime(CLOCK_MONOTONIC, &e->turn_t0);
|
||||
@@ -2862,8 +2780,7 @@ cmd_status(struct sp_engine *e, struct sp_buf *out)
|
||||
sp_buf_puts(out, "\n");
|
||||
sp_buf_printf(out, " %-8s ", sp_t("status.limits"));
|
||||
sp_buf_printf(out, sp_t("status.limits_value"), e->base.max_bytes,
|
||||
e->base.max_steps, e->base.autonomous ?
|
||||
sp_t("status.autonomous") : "");
|
||||
e->base.max_steps);
|
||||
sp_buf_puts(out, "\n");
|
||||
if (e->host != NULL) {
|
||||
struct sp_meter m;
|
||||
|
||||
+8
-26
@@ -235,8 +235,7 @@ reply_tools(int fd, int id, const struct srv *s)
|
||||
}
|
||||
|
||||
static void
|
||||
call_tool(int fd, int id, const struct srv *s, const char *line,
|
||||
int allow_mutate)
|
||||
call_tool(int fd, int id, const struct srv *s, const char *line)
|
||||
{
|
||||
const struct sp_tool *t;
|
||||
const char *name, *argstart = NULL;
|
||||
@@ -294,14 +293,6 @@ call_tool(int fd, int id, const struct srv *s, const char *line,
|
||||
return;
|
||||
}
|
||||
if (mine) {
|
||||
if (sp_mcpsession_auth(name) != SP_AUTH_READ && !allow_mutate) {
|
||||
sp_buf_free(&raw);
|
||||
sp_buf_free(&out);
|
||||
reply_error(fd, id, -32000, "tool needs authorization; "
|
||||
"start the server with --autonomous to allow "
|
||||
"mutating calls");
|
||||
return;
|
||||
}
|
||||
(void)sp_mcpsession_call(name, raw.p, &out);
|
||||
} else {
|
||||
t = srv_reg(s) != NULL ? sp_registry_find(srv_reg(s), name) : NULL;
|
||||
@@ -311,14 +302,6 @@ call_tool(int fd, int id, const struct srv *s, const char *line,
|
||||
reply_error(fd, id, -32601, "unknown tool");
|
||||
return;
|
||||
}
|
||||
if (t->auth != SP_AUTH_READ && !allow_mutate) {
|
||||
sp_buf_free(&raw);
|
||||
sp_buf_free(&out);
|
||||
reply_error(fd, id, -32000, "tool needs authorization; "
|
||||
"start the server with --autonomous to allow "
|
||||
"mutating calls");
|
||||
return;
|
||||
}
|
||||
if (t->run(raw.p, &out, t->ctx) != 0)
|
||||
sp_buf_puts(&out, "error: tool failed");
|
||||
}
|
||||
@@ -338,7 +321,7 @@ call_tool(int fd, int id, const struct srv *s, const char *line,
|
||||
}
|
||||
|
||||
static void
|
||||
handle_line(int fd, const struct srv *s, const char *line, int allow_mutate)
|
||||
handle_line(int fd, const struct srv *s, const char *line)
|
||||
{
|
||||
mcp_json *v;
|
||||
const char *method;
|
||||
@@ -373,7 +356,7 @@ handle_line(int fd, const struct srv *s, const char *line, int allow_mutate)
|
||||
}
|
||||
if (strcmp(method, "tools/call") == 0) {
|
||||
mcp_json_free(v);
|
||||
call_tool(fd, id, s, line, allow_mutate);
|
||||
call_tool(fd, id, s, line);
|
||||
return;
|
||||
}
|
||||
mcp_json_free(v);
|
||||
@@ -395,7 +378,7 @@ token_equal(const char *a, const char *b)
|
||||
}
|
||||
|
||||
static void
|
||||
serve_conn(int fd, const struct srv *s, int allow_mutate, const char *token)
|
||||
serve_conn(int fd, const struct srv *s, const char *token)
|
||||
{
|
||||
char line[16384];
|
||||
size_t used = 0;
|
||||
@@ -436,7 +419,7 @@ serve_conn(int fd, const struct srv *s, int allow_mutate, const char *token)
|
||||
}
|
||||
authed = 1;
|
||||
} else if (line[0] == '{')
|
||||
handle_line(fd, s, line, allow_mutate);
|
||||
handle_line(fd, s, line);
|
||||
len = (size_t)(nl - line) + 1;
|
||||
memmove(line, line + len, used - len + 1);
|
||||
used -= len;
|
||||
@@ -449,7 +432,7 @@ serve_conn(int fd, const struct srv *s, int allow_mutate, const char *token)
|
||||
int
|
||||
sp_mcp_serve(struct sp_engine **eng, const struct sp_engine_opts *opts,
|
||||
const struct sp_registry *reg, const char *ip, int port,
|
||||
int allow_mutate, int own_signals, struct sp_buf *err)
|
||||
int own_signals, struct sp_buf *err)
|
||||
{
|
||||
struct srv srv;
|
||||
int s, one = 1;
|
||||
@@ -510,8 +493,7 @@ sp_mcp_serve(struct sp_engine **eng, const struct sp_engine_opts *opts,
|
||||
/* a client that hangs up mid-reply is a closed connection, not our death */
|
||||
(void)signal(SIGPIPE, SIG_IGN);
|
||||
fprintf(stderr, "splinter: MCP server on %s:%d (%zu tools + %d "
|
||||
"session tools, %s, %s)\n", ip, port, ntools, 12,
|
||||
allow_mutate ? "mutating allowed" : "read only",
|
||||
"session tools, %s)\n", ip, port, ntools, 12,
|
||||
token != NULL ? "token required" : "loopback, no token");
|
||||
|
||||
while (!mcp_stop) {
|
||||
@@ -539,7 +521,7 @@ sp_mcp_serve(struct sp_engine **eng, const struct sp_engine_opts *opts,
|
||||
(void)setsockopt(c, SOL_SOCKET, SO_RCVTIMEO, &tv,
|
||||
sizeof(tv));
|
||||
}
|
||||
serve_conn(c, &srv, allow_mutate, token);
|
||||
serve_conn(c, &srv, token);
|
||||
close(c);
|
||||
}
|
||||
close(s);
|
||||
|
||||
@@ -19,7 +19,7 @@ struct sp_engine_opts;
|
||||
*/
|
||||
int sp_mcp_serve(struct sp_engine **eng, const struct sp_engine_opts *opts,
|
||||
const struct sp_registry *, const char *ip, int port,
|
||||
int allow_mutate, int own_signals, struct sp_buf *err);
|
||||
int own_signals, struct sp_buf *err);
|
||||
|
||||
/*
|
||||
* Ask a running sp_mcp_serve() to return: it stops accepting, closes the
|
||||
|
||||
+12
-64
@@ -20,15 +20,11 @@
|
||||
* splinter_agent switch the profile by name
|
||||
* splinter_steer type into the running turn
|
||||
* splinter_cancel cancel the running turn
|
||||
* splinter_approve answer a pending approval
|
||||
* splinter_answer answer the question the `ask` tool put to the operator
|
||||
*
|
||||
* A turn that parks on an approval is not an error: splinter_say returns the
|
||||
* question and leaves the turn waiting, and splinter_approve answers it. The
|
||||
* A turn that parks on `ask` is not an error: splinter_say returns the
|
||||
* question and leaves the turn waiting, and splinter_answer answers it. The
|
||||
* server is one connection at a time, so there is at most one such turn.
|
||||
*
|
||||
* Authorization goes through the same gate as the file tools: reads are
|
||||
* always served, anything that starts a turn or changes a session needs
|
||||
* --autonomous on the server.
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
@@ -93,22 +89,6 @@ arg_int(const char *args, const char *key, int dflt)
|
||||
return n;
|
||||
}
|
||||
|
||||
static int
|
||||
arg_bool(const char *args, const char *key, int dflt)
|
||||
{
|
||||
mcp_json *v;
|
||||
int n = dflt;
|
||||
|
||||
if (args == NULL || *args == '\0')
|
||||
return dflt;
|
||||
v = mcp_parse(args);
|
||||
if (v == NULL)
|
||||
return dflt;
|
||||
n = mcp_arg_bool(v, key, dflt);
|
||||
mcp_json_free(v);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* ---- the tools ---- */
|
||||
|
||||
static const struct {
|
||||
@@ -138,8 +118,8 @@ static const struct {
|
||||
"\"description\":\"how many messages, default 20, max 100\"}}}" },
|
||||
{ "splinter_say", SP_AUTH_MUTATE,
|
||||
"Send a prompt to the open session and wait for the answer. Returns "
|
||||
"the answer; if the turn waits for an approval, returns the question "
|
||||
"and leaves the turn parked (call splinter_approve).",
|
||||
"the answer; if the model asks a question, returns it and leaves the "
|
||||
"turn parked (call splinter_answer).",
|
||||
"{\"type\":\"object\",\"properties\":{\"text\":{\"type\":\"string\","
|
||||
"\"description\":\"the prompt\"}},\"required\":[\"text\"]}" },
|
||||
{ "splinter_open", SP_AUTH_MUTATE,
|
||||
@@ -164,11 +144,6 @@ static const struct {
|
||||
"\"description\":\"the steering text\"}},\"required\":[\"text\"]}" },
|
||||
{ "splinter_cancel", SP_AUTH_MUTATE,
|
||||
"Cancel the running turn.", "{\"type\":\"object\",\"properties\":{}}" },
|
||||
{ "splinter_approve", SP_AUTH_MUTATE,
|
||||
"Answer a pending approval: allow true runs the tool, false denies it "
|
||||
"(the denial becomes the observation the model sees).",
|
||||
"{\"type\":\"object\",\"properties\":{\"allow\":{\"type\":\"boolean\"}},"
|
||||
"\"required\":[\"allow\"]}" },
|
||||
{ "splinter_answer", SP_AUTH_MUTATE,
|
||||
"Answer the question the `ask` tool put to the operator: the text of "
|
||||
"the answer. Use it when splinter_say returns \"the model asks\".",
|
||||
@@ -264,7 +239,7 @@ tool_state(struct sp_buf *out)
|
||||
sp_buf_printf(out, "session: %s\n", st.session);
|
||||
sp_buf_printf(out, "dir: %s\n", st.dir);
|
||||
sp_buf_printf(out, "busy: %s%s\n", st.busy ? "yes" : "no",
|
||||
st.waiting ? " (waiting for an approval)" : "");
|
||||
st.asking ? " (the model is waiting for an answer)" : "");
|
||||
sp_buf_printf(out, "tokens: %zu in / %zu out\n", st.tokens_in,
|
||||
st.tokens_out);
|
||||
sp_buf_printf(out, "ctx: %zu/%zu", st.ctx_used, st.ctx_max);
|
||||
@@ -377,9 +352,8 @@ tool_say(const char *args, struct sp_buf *out)
|
||||
{
|
||||
struct sp_engine *e = *session_eng;
|
||||
char *text, *answer;
|
||||
char nm[64] = "", ar[512] = "";
|
||||
size_t steps = 0, calls = 0;
|
||||
int known = 0, auth = SP_AUTH_READ;
|
||||
int known = 0;
|
||||
time_t deadline;
|
||||
|
||||
/* an MCP thread's stack is small: the buffers live on the heap */
|
||||
@@ -414,23 +388,12 @@ tool_say(const char *args, struct sp_buf *out)
|
||||
collect(e, answer, SAY_ANSWER_MAX, &known, &steps, &calls);
|
||||
if (!sp_engine_busy(e))
|
||||
break;
|
||||
if (sp_engine_state(e, &st) == 0 && st.waiting &&
|
||||
sp_engine_approval(e, nm, sizeof(nm), &auth, ar,
|
||||
sizeof(ar))) {
|
||||
sp_buf_printf(out, "approval required: %s %s (%s)\n"
|
||||
"call splinter_approve {\"allow\":true} or "
|
||||
"{\"allow\":false}\n", nm, ar,
|
||||
auth == SP_AUTH_DESTRUCTIVE ? "destructive" :
|
||||
auth == SP_AUTH_MUTATE ? "mutate" : "read");
|
||||
goto done;
|
||||
}
|
||||
/*
|
||||
* The model asked the operator something. This is the same
|
||||
* park as an approval and must leave the same way: returned
|
||||
* to the caller as a question, with the tool that answers it
|
||||
* named. Waiting here instead held the MCP call for the full
|
||||
* ask wait (sp_ask_wait_s(), 900 s by default; the client timed
|
||||
* out at 15 s).
|
||||
* The model asked the operator something. The call returns
|
||||
* the question with the tool that answers it named. Waiting
|
||||
* here instead held the MCP call for the full ask wait
|
||||
* (sp_ask_wait_s(), 900 s by default; the client timed out at
|
||||
* 15 s).
|
||||
*/
|
||||
if (sp_engine_state(e, &st) == 0 && st.asking) {
|
||||
char qt[SP_ASK_MAX];
|
||||
@@ -612,19 +575,6 @@ tool_cancel(struct sp_buf *out)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_approve(const char *args, struct sp_buf *out)
|
||||
{
|
||||
int allow = arg_bool(args, "allow", 0);
|
||||
|
||||
if (sp_engine_approve(*session_eng, allow) != 0) {
|
||||
sp_buf_puts(out, "error: no approval is pending");
|
||||
return 0;
|
||||
}
|
||||
sp_buf_printf(out, "%s\n", allow ? "approved" : "denied");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_answer(const char *args, struct sp_buf *out)
|
||||
{
|
||||
@@ -672,8 +622,6 @@ sp_mcpsession_call(const char *name, const char *args, struct sp_buf *out)
|
||||
return tool_steer(args, out), 1;
|
||||
if (strcmp(name, "splinter_cancel") == 0)
|
||||
return tool_cancel(out), 1;
|
||||
if (strcmp(name, "splinter_approve") == 0)
|
||||
return tool_approve(args, out), 1;
|
||||
if (strcmp(name, "splinter_answer") == 0)
|
||||
return tool_answer(args, out), 1;
|
||||
return 0;
|
||||
|
||||
@@ -33,9 +33,10 @@
|
||||
* results, since the caller's results are still owed).
|
||||
*
|
||||
* Security: binding beyond loopback requires SPLINTER_GATEWAY_TOKEN, and the
|
||||
* token is checked on every request. Mutating and destructive local tools run
|
||||
* only with --autonomous; otherwise the model is told they are denied. There
|
||||
* is no TLS: put it behind ssh or a tunnel when the network is not yours.
|
||||
* token is checked on every request. What the local tools may do is decided by
|
||||
* what the operator registered and by the agent's policy -- the gateway asks
|
||||
* nobody, because the caller is not sitting in front of a prompt. There is no
|
||||
* TLS: put it behind ssh or a tunnel when the network is not yours.
|
||||
*/
|
||||
#include <arpa/inet.h>
|
||||
#include <ctype.h>
|
||||
@@ -524,11 +525,12 @@ run_local(const struct sp_gateway_opts *o, const char *gw_name,
|
||||
sp_buf_printf(out, "gateway: no local tool '%s'", name);
|
||||
return -1;
|
||||
}
|
||||
if (t->auth != SP_AUTH_READ && !o->autonomous) {
|
||||
sp_buf_puts(out, "denied: the gateway runs mutating and "
|
||||
"destructive tools only with --autonomous");
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* No approval, no --autonomous gate: a caller who can reach this endpoint
|
||||
* can run the tools the registry offers. What the operator allows is
|
||||
* expressed by what they register and by the agent's policy, not by a
|
||||
* question the gateway cannot even put to anyone.
|
||||
*/
|
||||
if (t->run == NULL) {
|
||||
sp_buf_printf(out, "gateway: '%s' has no runner", name);
|
||||
return -1;
|
||||
|
||||
@@ -19,7 +19,6 @@ struct sp_gateway_opts {
|
||||
const char *ip;
|
||||
int port;
|
||||
const char *token; /* SPLINTER_GATEWAY_TOKEN, may be NULL */
|
||||
int autonomous; /* local mutate/destructive tools */
|
||||
size_t max_steps; /* gateway tool rounds, 0: 16 */
|
||||
struct sp_registry *reg; /* splinter's own tools */
|
||||
struct sp_config *conf;
|
||||
|
||||
+15
-196
@@ -33,7 +33,6 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "ack.h"
|
||||
#include "curl.h"
|
||||
#include "splinter.h"
|
||||
#include "splinter_engine.h"
|
||||
@@ -73,7 +72,6 @@ static int tg_curl(const char *url, const char *post_body, struct sp_buf *out,
|
||||
static int tg_callback(const char *token, const char *allow,
|
||||
const mcp_json *cq, struct sp_agent *ag, struct sp_config *conf,
|
||||
struct sp_host *host, const char *sess_dir);
|
||||
static int tg_ack_reply(const char *token, long chat_id, const char *text);
|
||||
|
||||
/* Telegram shows "typing" for about five seconds; a turn takes longer. */
|
||||
struct typing {
|
||||
@@ -1033,13 +1031,13 @@ allowed(const char *allow, long id)
|
||||
}
|
||||
|
||||
/*
|
||||
* The update offset is shared by the poll loop and tg_approve: an approval
|
||||
* that waits keeps reading the chat, and the loop must not handle again what
|
||||
* the approval already consumed.
|
||||
* The update offset is shared by the poll loop and tg_ask: a question that
|
||||
* waits keeps reading the chat, and the loop must not handle again what the
|
||||
* question already consumed.
|
||||
*/
|
||||
static long tg_offset;
|
||||
|
||||
/* the chat the current turn came from, for tg_approve */
|
||||
/* the chat the current turn came from, for tg_ask */
|
||||
static struct {
|
||||
const char *token;
|
||||
long chat_id;
|
||||
@@ -1083,7 +1081,7 @@ tg_note(void *ctx, const char *text)
|
||||
/*
|
||||
* The `ask` tool's question, in the chat the turn came from.
|
||||
*
|
||||
* Same shape as tg_approve: the turn is parked, so this function reads the
|
||||
* The turn is parked while a question is open, so this function reads the
|
||||
* chat itself until the operator answers. The difference is the answer -- free
|
||||
* text, not yes/no -- so the reply is simply the next message from an allowed
|
||||
* user, and an option is taken by sending its text (a keyboard would be a
|
||||
@@ -1150,7 +1148,7 @@ tg_ask(void *ctx, const char *text, const char (*opts)[SP_ASK_OPT_LEN],
|
||||
tg_offset = uid + 1;
|
||||
cq = mcp_arg(update, "callback_query");
|
||||
if (cq != NULL) {
|
||||
/* a button from an approval; still not an answer */
|
||||
/* a button, not an answer to the question */
|
||||
(void)tg_callback(tg_cur.token, tg_cur.allow, cq,
|
||||
NULL, NULL, NULL, NULL);
|
||||
continue;
|
||||
@@ -1163,13 +1161,6 @@ tg_ask(void *ctx, const char *text, const char (*opts)[SP_ASK_OPT_LEN],
|
||||
from = tg_id(mcp_arg(m, "from"), "id");
|
||||
if (t == NULL || chat == 0 || !allowed(tg_cur.allow, from))
|
||||
continue;
|
||||
/*
|
||||
* A reply to some other approval is consumed by that
|
||||
* one, not taken as this question's answer: "ok 7" is
|
||||
* not an answer to "which colour?".
|
||||
*/
|
||||
if (tg_ack_reply(tg_cur.token, chat, t))
|
||||
continue;
|
||||
/*
|
||||
* /cancel is the way out of the question, and the hint
|
||||
* under it says so. Taking it as the answer instead
|
||||
@@ -1483,8 +1474,7 @@ tg_status_text(struct sp_agent *ag, struct sp_config *conf, struct sp_host *host
|
||||
if (ag->cfg != NULL) {
|
||||
sp_buf_printf(out, " %-10s ", sp_t("status.limits"));
|
||||
sp_buf_printf(out, sp_t("status.limits_value"),
|
||||
ag->cfg->max_bytes, ag->cfg->max_steps,
|
||||
ag->cfg->autonomous ? sp_t("status.autonomous") : "");
|
||||
ag->cfg->max_bytes, ag->cfg->max_steps);
|
||||
sp_buf_puts(out, "\n");
|
||||
}
|
||||
}
|
||||
@@ -1958,7 +1948,7 @@ tg_new_conversation(const char *token, long chat_id, struct sp_agent *ag,
|
||||
}
|
||||
|
||||
/*
|
||||
* An inline keyboard answer: ack:<id>:yes|no for an approval, model:<id> for a
|
||||
* An inline keyboard answer: model:<id> for a
|
||||
* model, mlater for "not now". The button travels as a callback_query, not a
|
||||
* message; the same allow list applies. The buttons are removed after the
|
||||
* answer so the chat does not keep a stale choice.
|
||||
@@ -1969,10 +1959,9 @@ tg_callback(const char *token, const char *allow, const mcp_json *cq,
|
||||
const char *sess_dir)
|
||||
{
|
||||
const mcp_json *from, *msg;
|
||||
const char *data, *cqid, *sep;
|
||||
const char *data, *cqid;
|
||||
long from_id, chat = 0, mid = 0;
|
||||
struct sp_buf sink;
|
||||
int id, yes;
|
||||
|
||||
if (cq == NULL)
|
||||
return 0;
|
||||
@@ -2056,7 +2045,7 @@ tg_callback(const char *token, const char *allow, const mcp_json *cq,
|
||||
tg_answer_callback(token, cqid, sp_t("model.toast"));
|
||||
if (mid != 0)
|
||||
tg_clear_buttons(token, chat, mid);
|
||||
/* during an approval only this process's poller runs: no agent */
|
||||
/* while a question waits only this process's poller runs */
|
||||
if (ag != NULL && conf != NULL && chat != 0) {
|
||||
if (sp_buf_init(&sink) == 0) {
|
||||
tg_model_use(token, chat, ag, conf, data + 6,
|
||||
@@ -2101,165 +2090,7 @@ tg_callback(const char *token, const char *allow, const mcp_json *cq,
|
||||
tg_clear_buttons(token, chat, mid);
|
||||
return 1;
|
||||
}
|
||||
if (strncmp(data, "ack:", 4) != 0)
|
||||
return 0;
|
||||
sep = strrchr(data, ':');
|
||||
if (sep == NULL)
|
||||
return 0;
|
||||
id = atoi(data + 4);
|
||||
yes = strcmp(sep + 1, "yes") == 0;
|
||||
if (id <= 0)
|
||||
return 0;
|
||||
if (sp_ack_answer(id, yes, "telegram") == 0) {
|
||||
tg_answer_callback(token, cqid, yes ?
|
||||
sp_td("ack.granted", "Approved.") :
|
||||
sp_td("ack.refused", "Refused."));
|
||||
if (chat != 0) {
|
||||
if (mid != 0)
|
||||
tg_clear_buttons(token, chat, mid);
|
||||
(void)send_message(token, chat, yes ?
|
||||
sp_td("ack.granted", "Approved.") :
|
||||
sp_td("ack.refused", "Refused."), NULL);
|
||||
}
|
||||
} else {
|
||||
tg_answer_callback(token, cqid,
|
||||
sp_td("ack.gone", "There is no such request (any more)."));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* "ok <id>", "ja <id>", "no <id>", "nein <id>": an answer to a pending
|
||||
* approval - this process's or another one's. Returns 1 when the text was one.
|
||||
*/
|
||||
static int
|
||||
tg_ack_reply(const char *token, long chat_id, const char *text)
|
||||
{
|
||||
const char *num;
|
||||
int id, yes;
|
||||
|
||||
if (strncmp(text, "ok ", 3) != 0 && strncmp(text, "no ", 3) != 0 &&
|
||||
strncmp(text, "ja ", 3) != 0 && strncmp(text, "nein ", 5) != 0)
|
||||
return 0;
|
||||
num = strchr(text, ' ');
|
||||
id = num != NULL ? atoi(num + 1) : 0;
|
||||
if (id <= 0)
|
||||
return 0;
|
||||
yes = text[0] == 'o' || text[0] == 'j';
|
||||
if (sp_ack_answer(id, yes, "telegram") == 0)
|
||||
(void)send_message(token, chat_id, yes ?
|
||||
sp_td("ack.granted", "Approved.") :
|
||||
sp_td("ack.refused", "Refused."), NULL);
|
||||
else
|
||||
(void)send_message(token, chat_id,
|
||||
sp_td("ack.gone", "There is no such request (any more)."),
|
||||
NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Approval for a tool call of the gateway's own turn, asked in the chat that
|
||||
* started the turn. The gateway is the only reader of that chat, so while it
|
||||
* waits it keeps reading: an answer resolves the request (also one from
|
||||
* 'splinter --ack' on the machine), anything else is told to wait. A timeout
|
||||
* is a no.
|
||||
*/
|
||||
static int
|
||||
tg_approve(void *ctx, const char *name, int auth, const char *args)
|
||||
{
|
||||
const char *ttl_s = getenv("SPLINTER_ACK_TTL");
|
||||
int ttl = ttl_s != NULL && *ttl_s != '\0' ? atoi(ttl_s) : 120;
|
||||
char shown[320], who[64] = "";
|
||||
struct sp_buf msg, url, reply, err, mark;
|
||||
time_t end;
|
||||
int id, yes;
|
||||
|
||||
(void)ctx;
|
||||
if (ttl <= 0)
|
||||
ttl = 120;
|
||||
id = sp_ack_request(name, args, auth == SP_AUTH_MUTATE ? "mutate" :
|
||||
"destructive", ttl);
|
||||
if (id < 0)
|
||||
return 0;
|
||||
snprintf(shown, sizeof(shown), "%s", args != NULL ? args : "");
|
||||
if (strlen(shown) >= 300)
|
||||
memcpy(shown + 296, "...", 4);
|
||||
if (sp_buf_init(&msg) != 0 || sp_buf_init(&url) != 0 ||
|
||||
sp_buf_init(&reply) != 0 || sp_buf_init(&err) != 0 ||
|
||||
sp_buf_init(&mark) != 0)
|
||||
return 0;
|
||||
sp_buf_printf(&msg, sp_td("ack.request", "Approval %d: %s %s"), id,
|
||||
name, shown);
|
||||
sp_buf_puts(&msg, "\n");
|
||||
sp_buf_printf(&msg, sp_td("ack.how", "Answer: ok %d / no %d"), id, id);
|
||||
sp_buf_puts(&mark, "{\"inline_keyboard\":[[{\"text\":\"");
|
||||
sp_json_escape(&mark, sp_t("ack.yes"));
|
||||
sp_buf_printf(&mark, "\",\"callback_data\":\"ack:%d:yes\"},"
|
||||
"{\"text\":\"", id);
|
||||
sp_json_escape(&mark, sp_t("ack.no"));
|
||||
sp_buf_printf(&mark, "\",\"callback_data\":\"ack:%d:no\"}]]}", id);
|
||||
(void)send_message_markup(tg_cur.token, tg_cur.chat_id, msg.p, mark.p,
|
||||
NULL);
|
||||
|
||||
end = time(NULL) + ttl;
|
||||
while (!tg_stop && time(NULL) < end && !sp_ack_answered(id)) {
|
||||
mcp_json *v;
|
||||
const mcp_json *result, *update;
|
||||
size_t i;
|
||||
|
||||
sp_buf_clear(&url);
|
||||
sp_buf_printf(&url, "%s/bot%s/getUpdates?timeout=5&offset=%ld",
|
||||
api_base(), tg_cur.token, tg_offset);
|
||||
sp_buf_clear(&reply);
|
||||
sp_buf_clear(&err);
|
||||
if (tg_curl(url.p, NULL, &reply, &err) != 0) {
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
v = mcp_parse(reply.p);
|
||||
result = v != NULL ? mcp_arg(v, "result") : NULL;
|
||||
for (i = 0; result != NULL &&
|
||||
(update = mcp_array_at(result, i)) != NULL; i++) {
|
||||
const mcp_json *m, *cq;
|
||||
long uid = tg_id(update, "update_id");
|
||||
const char *text;
|
||||
long chat, from;
|
||||
|
||||
if (uid < tg_offset)
|
||||
continue;
|
||||
tg_offset = uid + 1;
|
||||
cq = mcp_arg(update, "callback_query");
|
||||
if (cq != NULL) {
|
||||
(void)tg_callback(tg_cur.token, tg_cur.allow, cq,
|
||||
NULL, NULL, NULL, NULL);
|
||||
continue;
|
||||
}
|
||||
m = mcp_arg(update, "message");
|
||||
if (m == NULL)
|
||||
continue;
|
||||
text = mcp_arg_string(m, "text");
|
||||
chat = tg_id(mcp_arg(m, "chat"), "id");
|
||||
from = tg_id(mcp_arg(m, "from"), "id");
|
||||
if (text == NULL || chat == 0 || !allowed(tg_cur.allow, from))
|
||||
continue;
|
||||
if (tg_ack_reply(tg_cur.token, chat, text))
|
||||
continue;
|
||||
sp_buf_clear(&msg);
|
||||
sp_buf_printf(&msg, sp_td("ack.waiting",
|
||||
"Still waiting for an approval: ok %d / no %d"), id, id);
|
||||
(void)send_message(tg_cur.token, chat, msg.p, NULL);
|
||||
}
|
||||
if (v != NULL)
|
||||
mcp_json_free(v);
|
||||
}
|
||||
/* reads the answer if there is one, and closes the request either way */
|
||||
yes = sp_ack_wait(id, 1, who, sizeof(who));
|
||||
sp_buf_free(&msg);
|
||||
sp_buf_free(&url);
|
||||
sp_buf_free(&reply);
|
||||
sp_buf_free(&err);
|
||||
sp_buf_free(&mark);
|
||||
return yes;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
@@ -2340,9 +2171,10 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
}
|
||||
|
||||
/*
|
||||
* Approval in the chat the request came from. Without this the gateway
|
||||
* either denied every mutating call (no approval stream) or, with ack
|
||||
* channels, waited for an answer that only it could have read.
|
||||
* The turn's callbacks: notes go to the chat, and `ask` talks to the
|
||||
* operator in the chat the request came from. Without the ask callback
|
||||
* the model would be told "nobody can be asked" while a person is
|
||||
* sitting right there.
|
||||
*/
|
||||
if (orig != NULL) {
|
||||
gcfg = *orig;
|
||||
@@ -2354,11 +2186,6 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
*/
|
||||
gcfg.note = tg_note;
|
||||
gcfg.note_ctx = NULL;
|
||||
if (!orig->autonomous && orig->approve == NULL) {
|
||||
gcfg.approve = tg_approve;
|
||||
gcfg.approve_ctx = &tg_cur;
|
||||
gcfg.approve_is_channel = 1;
|
||||
}
|
||||
/*
|
||||
* The operator is in the chat, so `ask` must reach them there.
|
||||
* Set from the base config, not from the turn config: the
|
||||
@@ -2433,7 +2260,6 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
long chat_id, from, update_id;
|
||||
|
||||
update_id = tg_id(update, "update_id");
|
||||
/* tg_approve may have read past this batch already */
|
||||
if (update_id < tg_offset)
|
||||
continue;
|
||||
tg_offset = update_id + 1;
|
||||
@@ -2463,19 +2289,12 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
|
||||
* model: /goal comes from a plugin and may
|
||||
* start a prompt, not only answer.
|
||||
*/
|
||||
/*
|
||||
* An answer to a pending approval. The gateway is the one
|
||||
* reader of this chat, so it resolves here instead of letting a
|
||||
* second poller fight it for updates.
|
||||
*/
|
||||
if (tg_cc.active && tg_cc.chat_id == chat_id) {
|
||||
tg_connect_answer(token, chat_id, text, ag,
|
||||
conf, err);
|
||||
sp_buf_clear(err);
|
||||
continue;
|
||||
}
|
||||
if (tg_ack_reply(token, chat_id, text))
|
||||
continue;
|
||||
if (strcmp(text, "/interview") == 0) {
|
||||
tg_interview_begin(token, chat_id, ag,
|
||||
cmddir, err);
|
||||
|
||||
@@ -197,33 +197,6 @@ show_event(struct client *c, const mcp_json *v)
|
||||
fflush(stdout);
|
||||
}
|
||||
c->saw_reply = 1;
|
||||
} else if (strcmp(ev, "approval") == 0) {
|
||||
const char *call = mcp_arg_string(v, "call");
|
||||
char answer[32];
|
||||
|
||||
fprintf(stderr, "approve? %s [y/N] ",
|
||||
call != NULL ? call : "?");
|
||||
fflush(stderr);
|
||||
if (fgets(answer, sizeof(answer), stdin) != NULL &&
|
||||
(answer[0] == 'y' || answer[0] == 'Y')) {
|
||||
struct sp_buf b;
|
||||
|
||||
if (sp_buf_init(&b) == 0) {
|
||||
sp_buf_puts(&b, "{\"cmd\":\"approve\","
|
||||
"\"allow\":true}\n");
|
||||
(void)send_all(c->fd, b.p, b.len);
|
||||
sp_buf_free(&b);
|
||||
}
|
||||
} else {
|
||||
struct sp_buf b;
|
||||
|
||||
if (sp_buf_init(&b) == 0) {
|
||||
sp_buf_puts(&b, "{\"cmd\":\"approve\","
|
||||
"\"allow\":false}\n");
|
||||
(void)send_all(c->fd, b.p, b.len);
|
||||
sp_buf_free(&b);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(ev, "submitted") == 0) {
|
||||
c->saw_submitted = 1;
|
||||
} else if (strcmp(ev, "busy") == 0) {
|
||||
|
||||
+6
-60
@@ -3730,9 +3730,6 @@ drain_events(struct ui_state *s, struct turn_view *v)
|
||||
case SP_EV_ENTRY:
|
||||
add_entry(s, ev.a, ev.b);
|
||||
break;
|
||||
case SP_EV_APPROVAL:
|
||||
/* turn_approval() polls the engine for this */
|
||||
break;
|
||||
case SP_EV_TURN_END:
|
||||
v->rc = (int)ev.n;
|
||||
v->got_end = 1;
|
||||
@@ -3882,9 +3879,8 @@ turn_paint(struct ui_state *s, struct turn_view *v)
|
||||
}
|
||||
|
||||
/*
|
||||
* The `ask` tool put a question to the operator. Same park/wake as the approval
|
||||
* above, but the answer is text: the options are rows in the picker and a
|
||||
* typed line is the answer too. A question that arrives while the operator is
|
||||
* The `ask` tool put a question to the operator. The answer is text: the
|
||||
* options are rows in the picker and a typed line is the answer too. A question that arrives while the operator is
|
||||
* not looking must not hang the turn for its full timeout, so Esc and Ctrl-C
|
||||
* answer with nothing and the tool reports "no answer".
|
||||
*/
|
||||
@@ -3940,53 +3936,6 @@ turn_ask(struct ui_state *s)
|
||||
(void)rc;
|
||||
}
|
||||
|
||||
/*
|
||||
* A tool needs permission. The engine parked the turn; the question goes where
|
||||
* the prompt is, and y/N answers it. It names the call the way the transcript
|
||||
* does - "Run touch /tmp/x" - and then how far it reaches.
|
||||
*/
|
||||
static int
|
||||
turn_approval(struct ui_state *s, int c)
|
||||
{
|
||||
int auth = SP_AUTH_READ;
|
||||
char nm[64], ar[256];
|
||||
|
||||
if (!s->be->approval(s->be->ctx, nm, sizeof(nm), &auth, ar, sizeof(ar)))
|
||||
return 0;
|
||||
/*
|
||||
* Ctrl-C and Ctrl-D are not answers: they go on to turn_key, which
|
||||
* cancels and wakes the engine. Swallowing them here left the operator
|
||||
* no way out of a question but y or n.
|
||||
*/
|
||||
if (c == 3 || c == 4)
|
||||
return 0;
|
||||
{
|
||||
char call[640], label[64], line[768];
|
||||
size_t over, n;
|
||||
int w = getmaxx(ui_input) - 1;
|
||||
|
||||
sp_call_summary(nm, ar, call, sizeof(call));
|
||||
snprintf(label, sizeof(label), "%s",
|
||||
auth == SP_AUTH_MUTATE ? sp_t("auth.mutate") :
|
||||
sp_t("auth.destructive"));
|
||||
snprintf(line, sizeof(line), sp_t("ui.approve"), call, label);
|
||||
if ((int)strlen(line) > w) {
|
||||
/* the call gives way; the class and "y/N" never do */
|
||||
over = strlen(line) - (size_t)w;
|
||||
n = strlen(call);
|
||||
cut_line(call, n > over + 4 ? n - over - 4 : 0);
|
||||
snprintf(line, sizeof(line), sp_t("ui.approve"), call,
|
||||
label);
|
||||
}
|
||||
werase(ui_input);
|
||||
mvwaddnstr(ui_input, 0, 0, line, w);
|
||||
wrefresh(ui_input);
|
||||
}
|
||||
if (c == 'y' || c == 'Y' || c == 'n' || c == 'N' || c == 27)
|
||||
(void)s->be->approve(s->be->ctx, c == 'y' || c == 'Y');
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* A slash command typed while a turn runs. It is not a message to the model:
|
||||
* /model opens the picker at once (the switch is applied between requests),
|
||||
@@ -4098,8 +4047,9 @@ turn_key(struct ui_state *s, struct turn_view *v, int c)
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* Cancel at the engine: it wakes a turn that is asleep in an approval
|
||||
* and the transports kill their child or curl when the flag is set.
|
||||
* Cancel at the engine: it wakes a turn that is asleep on an `ask`
|
||||
* question and the transports kill their child or curl when the flag is
|
||||
* set.
|
||||
*/
|
||||
if (c == 3 || c == 4) {
|
||||
(void)s->be->cancel(s->be->ctx);
|
||||
@@ -4211,9 +4161,7 @@ ui_turn(struct ui_state *s, const char *text)
|
||||
continue;
|
||||
/*
|
||||
* A question is drawn in its own window and edits its own line;
|
||||
* the key that woke the loop is not an answer to it. Asking
|
||||
* before the approval keeps both channels apart: a turn that
|
||||
* parks on `ask` has no approval open.
|
||||
* the key that woke the loop is not an answer to it.
|
||||
*/
|
||||
if (s->be->ask != NULL && s->be->ask(s->be->ctx, probe,
|
||||
sizeof(probe), NULL, 0, NULL)) {
|
||||
@@ -4221,8 +4169,6 @@ ui_turn(struct ui_state *s, const char *text)
|
||||
turn_paint(s, &v);
|
||||
continue;
|
||||
}
|
||||
if (turn_approval(s, c))
|
||||
continue;
|
||||
if (c == ERR) {
|
||||
turn_paint(s, &v);
|
||||
continue;
|
||||
|
||||
+4
-19
@@ -196,7 +196,6 @@ send_state(struct client *c, struct sp_engine *eng, const char *name)
|
||||
ev_num(&b, "last_steps", (long)st.last_steps);
|
||||
ev_num(&b, "last_calls", (long)st.last_calls);
|
||||
ev_bool(&b, "busy", st.busy);
|
||||
ev_bool(&b, "waiting", st.waiting);
|
||||
ev_bool(&b, "asking", st.asking);
|
||||
ev_bool(&b, "quit", st.quit);
|
||||
sp_buf_puts(&b, ",\"agents\":[");
|
||||
@@ -267,19 +266,6 @@ send_event(struct client *c, struct sp_engine *eng, const struct sp_event *ev)
|
||||
ev_str(&b, "role", ev->a);
|
||||
ev_str(&b, "text", ev->b);
|
||||
break;
|
||||
case SP_EV_APPROVAL: {
|
||||
char call[640];
|
||||
|
||||
sp_call_summary(ev->a, ev->b, call, sizeof(call));
|
||||
ev_start(&b, "approval");
|
||||
ev_str(&b, "name", ev->a);
|
||||
ev_num(&b, "auth", ev->n);
|
||||
ev_str(&b, "args", ev->b);
|
||||
ev_str(&b, "call", call);
|
||||
ev_str(&b, "class", ev->n == SP_AUTH_MUTATE ? "mutate" :
|
||||
"destructive");
|
||||
break;
|
||||
}
|
||||
case SP_EV_ASK: {
|
||||
char opts[SP_ASK_OPTIONS][SP_ASK_OPT_LEN];
|
||||
char q[SP_ASK_MAX];
|
||||
@@ -837,8 +823,6 @@ handle_line(struct client *c, struct proto *p, const char *line)
|
||||
(void)send_reply(c, out.p != NULL ? out.p : "");
|
||||
}
|
||||
}
|
||||
} else if (strcmp(cmd, "approve") == 0) {
|
||||
(void)sp_engine_approve(eng, mcp_arg_bool(v, "allow", 0));
|
||||
} else if (strcmp(cmd, "answer") == 0) {
|
||||
text = mcp_arg_string(v, "text");
|
||||
/*
|
||||
@@ -928,9 +912,10 @@ done:
|
||||
}
|
||||
|
||||
/*
|
||||
* A client went away while a turn was running. The engine would park in an
|
||||
* approval nobody can answer, so cancel it and drain the events nobody will
|
||||
* read. The conversation keeps the answer the model produced up to there.
|
||||
* A client went away while a turn was running. A turn that is parked on an
|
||||
* `ask` question would wait for an answer nobody will send, so cancel it and
|
||||
* drain the events nobody will read. The conversation keeps the answer the
|
||||
* model produced up to there.
|
||||
*/
|
||||
static void
|
||||
abandon_turn(struct sp_engine *eng)
|
||||
|
||||
@@ -43,10 +43,6 @@ struct ui_backend {
|
||||
/* a steer that arrived after the turn's last step boundary */
|
||||
int (*take_steer)(void *ctx, struct sp_buf *out);
|
||||
|
||||
/* the approval a tool is parked on */
|
||||
int (*approval)(void *ctx, char *name, size_t namelen, int *auth,
|
||||
char *args, size_t argslen);
|
||||
int (*approve)(void *ctx, int allow);
|
||||
/*
|
||||
* The question the `ask` tool is parked on: the text and its options,
|
||||
* and the answer the front end gives back. A backend without a dialog
|
||||
|
||||
@@ -62,18 +62,6 @@ static int loc_steer(void *ctx, const char *text)
|
||||
return sp_engine_steer(ENG(ctx), text);
|
||||
}
|
||||
|
||||
static int loc_approval(void *ctx, char *name, size_t namelen, int *auth,
|
||||
char *args, size_t argslen)
|
||||
{
|
||||
return sp_engine_approval(ENG(ctx), name, namelen, auth, args,
|
||||
argslen);
|
||||
}
|
||||
|
||||
static int loc_approve(void *ctx, int allow)
|
||||
{
|
||||
return sp_engine_approve(ENG(ctx), allow);
|
||||
}
|
||||
|
||||
static int loc_ask(void *ctx, char *text, size_t textlen,
|
||||
char (*opts)[SP_ASK_OPT_LEN], size_t max, size_t *nopts)
|
||||
{
|
||||
@@ -289,8 +277,6 @@ static const struct ui_backend local = {
|
||||
.cancel = loc_cancel,
|
||||
.steer = loc_steer,
|
||||
.take_steer = loc_take_steer,
|
||||
.approval = loc_approval,
|
||||
.approve = loc_approve,
|
||||
.ask = loc_ask,
|
||||
.answer = loc_answer,
|
||||
.poll = loc_poll,
|
||||
|
||||
@@ -84,9 +84,6 @@ struct rem {
|
||||
int sess_gone; /* the restored session was not there */
|
||||
char gone_old[64], gone_new[64];
|
||||
int nagents, ncommands;
|
||||
int pending; /* an approval question is open */
|
||||
char a_name[64], a_args[256];
|
||||
int a_auth;
|
||||
/* the `ask` tool's question: text, options, and the answer to send back */
|
||||
int asking;
|
||||
char q_text[SP_ASK_MAX];
|
||||
@@ -321,7 +318,6 @@ fill_state(struct rem *r, const mcp_json *v)
|
||||
r->st.last_steps = (size_t)mcp_arg_int(v, "last_steps", 0);
|
||||
r->st.last_calls = (size_t)mcp_arg_int(v, "last_calls", 0);
|
||||
r->st.busy = mcp_arg_bool(v, "busy", 0);
|
||||
r->st.waiting = mcp_arg_bool(v, "waiting", 0);
|
||||
r->st.asking = mcp_arg_bool(v, "asking", 0);
|
||||
r->st.quit = mcp_arg_bool(v, "quit", 0);
|
||||
r->st.agent_cur = mcp_arg_int(v, "agent_cur", 0);
|
||||
@@ -524,16 +520,6 @@ dispatch(struct rem *r, const char *line)
|
||||
} else if (strcmp(ev, "entry") == 0) {
|
||||
q_push(r, SP_EV_ENTRY, 0, mcp_arg_string(v, "role"),
|
||||
mcp_arg_string(v, "text"));
|
||||
} else if (strcmp(ev, "approval") == 0) {
|
||||
r->pending = 1;
|
||||
r->a_auth = mcp_arg_int(v, "auth", SP_AUTH_MUTATE);
|
||||
snprintf(r->a_name, sizeof(r->a_name), "%s",
|
||||
mcp_arg_string(v, "name") != NULL ?
|
||||
mcp_arg_string(v, "name") : "?");
|
||||
snprintf(r->a_args, sizeof(r->a_args), "%s",
|
||||
mcp_arg_string(v, "args") != NULL ?
|
||||
mcp_arg_string(v, "args") : "");
|
||||
q_push(r, SP_EV_APPROVAL, r->a_auth, r->a_name, r->a_args);
|
||||
} else if (strcmp(ev, "ask") == 0) {
|
||||
const mcp_json *items = mcp_arg(v, "options");
|
||||
size_t i, n = items != NULL ? mcp_array_len(items) : 0;
|
||||
@@ -663,42 +649,6 @@ rem_steer(void *ctx, const char *text)
|
||||
return send_cmd(r, "steer", "text", text);
|
||||
}
|
||||
|
||||
static int
|
||||
rem_approval(void *ctx, char *name, size_t namelen, int *auth, char *args,
|
||||
size_t argslen)
|
||||
{
|
||||
struct rem *r = ctx;
|
||||
|
||||
if (!r->pending)
|
||||
return 0;
|
||||
if (name != NULL && namelen > 0)
|
||||
snprintf(name, namelen, "%s", r->a_name);
|
||||
if (args != NULL && argslen > 0)
|
||||
snprintf(args, argslen, "%s", r->a_args);
|
||||
if (auth != NULL)
|
||||
*auth = r->a_auth;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
rem_approve(void *ctx, int allow)
|
||||
{
|
||||
struct rem *r = ctx;
|
||||
struct sp_buf b;
|
||||
int rc;
|
||||
|
||||
if (!r->pending)
|
||||
return 0;
|
||||
r->pending = 0;
|
||||
if (r->fd < 0 || sp_buf_init(&b) != 0)
|
||||
return 0;
|
||||
sp_buf_printf(&b, "{\"cmd\":\"approve\",\"allow\":%s}\n",
|
||||
allow ? "true" : "false");
|
||||
rc = send_all(r->fd, b.p, b.len);
|
||||
sp_buf_free(&b);
|
||||
return rc == 0;
|
||||
}
|
||||
|
||||
static int
|
||||
rem_ask(void *ctx, char *text, size_t textlen, char (*opts)[SP_ASK_OPT_LEN],
|
||||
size_t max, size_t *nopts)
|
||||
@@ -780,7 +730,6 @@ rem_poll(void *ctx, struct sp_event *out)
|
||||
close(was);
|
||||
r->fd = -1;
|
||||
r->st.busy = 0;
|
||||
r->pending = 0;
|
||||
r->next_try = now_ms() + 1000;
|
||||
q_push(r, SP_EV_ENTRY, 0, "splinter",
|
||||
sp_t("ui.reconnecting"));
|
||||
@@ -1360,8 +1309,6 @@ sp_ui_backend_remote(const char *socket_path, struct sp_buf *err)
|
||||
be.wait = rem_wait;
|
||||
be.cancel = rem_cancel;
|
||||
be.steer = rem_steer;
|
||||
be.approval = rem_approval;
|
||||
be.approve = rem_approve;
|
||||
be.ask = rem_ask;
|
||||
be.answer = rem_answer;
|
||||
be.poll = rem_poll;
|
||||
|
||||
+8
-10
@@ -112,7 +112,8 @@ usage(FILE *f)
|
||||
" /v1/chat/completions with local gw_\n"
|
||||
" tools and caller tools passed through\n"
|
||||
" --openai-listen-port N gateway port, default 8097\n"
|
||||
" --autonomous run mutating tools without asking\n"
|
||||
" --autonomous accepted for compatibility; it no longer\n"
|
||||
" changes anything (no tool asks first)\n"
|
||||
" --steps N tool rounds per turn (default 150)\n"
|
||||
" --session DIR session directory (default ~/.splinter)\n"
|
||||
" --session-id ID session id (default sess-<time>-<pid>)\n"
|
||||
@@ -265,7 +266,7 @@ port_arg(const char *what, const char *s, int *out)
|
||||
static int
|
||||
ack_word(const char *w)
|
||||
{
|
||||
/* only a word that means yes is a yes: "nein" used to approve */
|
||||
/* only a word that means yes is a yes: "nein" used to count as one */
|
||||
if (!strcmp(w, "yes") || !strcmp(w, "y") || !strcmp(w, "ok") ||
|
||||
!strcmp(w, "ja"))
|
||||
return 1;
|
||||
@@ -1134,7 +1135,6 @@ struct mcp_bg {
|
||||
struct sp_engine **eng;
|
||||
const char *ip;
|
||||
int port;
|
||||
int allow_mutate;
|
||||
};
|
||||
|
||||
static void *
|
||||
@@ -1145,7 +1145,7 @@ mcp_bg_thread(void *arg)
|
||||
|
||||
if (sp_buf_init(&err) == 0) {
|
||||
(void)sp_mcp_serve(bg->eng, NULL, NULL, bg->ip, bg->port,
|
||||
bg->allow_mutate, 0, &err);
|
||||
0, &err);
|
||||
sp_buf_free(&err);
|
||||
}
|
||||
free(bg);
|
||||
@@ -1460,8 +1460,8 @@ main(int argc, char **argv)
|
||||
return rc;
|
||||
|
||||
/*
|
||||
* The modes that answer without an agent: a stored approval and the
|
||||
* first-run interview. Everything else is built by the engine.
|
||||
* The modes that answer without an agent: the ack files (a plugin API)
|
||||
* and the first-run interview. Everything else is built by the engine.
|
||||
*/
|
||||
if (o.acklist) {
|
||||
struct sp_buf out;
|
||||
@@ -1597,7 +1597,7 @@ main(int argc, char **argv)
|
||||
* beside it on its own thread. Session switching is not
|
||||
* available here (the daemon holds the engine), but the
|
||||
* session tools - list, history, say, model, steer,
|
||||
* cancel, approve - are.
|
||||
* cancel, answer - are.
|
||||
*/
|
||||
struct mcp_bg *bg = calloc(1, sizeof(*bg));
|
||||
pthread_t th;
|
||||
@@ -1610,7 +1610,6 @@ main(int argc, char **argv)
|
||||
bg->eng = ŋ
|
||||
bg->ip = o.mcp_ip;
|
||||
bg->port = o.mcp_port;
|
||||
bg->allow_mutate = cfg.autonomous == 1;
|
||||
if (pthread_create(&th, NULL, mcp_bg_thread, bg) != 0) {
|
||||
fprintf(stderr, "splinter: cannot start the MCP "
|
||||
"server thread\n");
|
||||
@@ -1627,7 +1626,7 @@ main(int argc, char **argv)
|
||||
(void)pthread_join(th, NULL);
|
||||
} else {
|
||||
rc = sp_mcp_serve(&eng, &eo, sp_engine_registry(eng),
|
||||
o.mcp_ip, o.mcp_port, cfg.autonomous == 1, 1, &err);
|
||||
o.mcp_ip, o.mcp_port, 1, &err);
|
||||
if (rc != 0)
|
||||
fprintf(stderr, "splinter: %s\n",
|
||||
err.len > 0 ? err.p : "MCP server failed");
|
||||
@@ -1648,7 +1647,6 @@ main(int argc, char **argv)
|
||||
go.ip = o.openai_ip;
|
||||
go.port = o.openai_port;
|
||||
go.token = getenv("SPLINTER_GATEWAY_TOKEN");
|
||||
go.autonomous = cfg.autonomous == 1;
|
||||
go.max_steps = 16;
|
||||
go.reg = sp_engine_registry(eng);
|
||||
go.conf = &conf;
|
||||
|
||||
+24
-41
@@ -1,29 +1,31 @@
|
||||
#!/bin/sh
|
||||
# splinter -- A2A INPUT_REQUIRED: a turn pauses before a mutating tool and
|
||||
# resumes with a task id and a yes/no answer.
|
||||
# splinter -- A2A runs a mutating tool without asking anyone.
|
||||
#
|
||||
# The script transport is the model: first a write call, then the answer. The
|
||||
# first SendMessage must return TASK_STATE_INPUT_REQUIRED without running the
|
||||
# tool; the follow-up with taskId "yes" runs it and completes; "no" completes
|
||||
# without writing.
|
||||
# This test used to guard the opposite: the first SendMessage returned
|
||||
# TASK_STATE_INPUT_REQUIRED before a write ran, and a follow-up with taskId
|
||||
# "yes"/"no" resumed it. That gate is gone -- an A2A caller is not sitting in
|
||||
# front of a prompt, and splinter does not decide for the operator. What is
|
||||
# guarded here:
|
||||
#
|
||||
# - a write call runs in the first SendMessage and the task completes;
|
||||
# - the answer the model gives after the tool round is the task's answer;
|
||||
# - no TASK_STATE_INPUT_REQUIRED is produced;
|
||||
# - CancelTask still works on a task that exists.
|
||||
set -u
|
||||
cd "$(dirname "$0")/.." || exit 1
|
||||
. "$(dirname "$0")/sh/port.sh"
|
||||
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-a2a-ir-XXXXXX") || exit 1
|
||||
P=$(free_port)
|
||||
P2=$((P + 1))
|
||||
P3=$((P + 2))
|
||||
S1=
|
||||
S2=
|
||||
S3=
|
||||
cleanup() {
|
||||
[ -n "$S1" ] && kill "$S1" 2>/dev/null
|
||||
[ -n "$S2" ] && kill "$S2" 2>/dev/null
|
||||
[ -n "$S3" ] && kill "$S3" 2>/dev/null
|
||||
rm -rf "$D"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
mkdir -p "$D/home"
|
||||
mkdir -p "$D/home" "$D/home2"
|
||||
|
||||
fails=0
|
||||
check() { if "$@"; then :; else echo "FAIL $*"; fails=$((fails + 1)); fi; }
|
||||
@@ -41,9 +43,6 @@ post() { # post <port> <json>
|
||||
first() { # first <port>
|
||||
post "$1" '{"jsonrpc":"2.0","id":"1","method":"SendMessage","params":{"message":{"messageId":"m","role":"ROLE_USER","parts":[{"text":"please write"}]}}}'
|
||||
}
|
||||
follow() { # follow <port> <task id> <text>
|
||||
post "$1" "{\"jsonrpc\":\"2.0\",\"id\":\"2\",\"method\":\"SendMessage\",\"params\":{\"message\":{\"messageId\":\"m2\",\"role\":\"ROLE_USER\",\"taskId\":\"$2\",\"parts\":[{\"text\":\"$3\"}]}}}"
|
||||
}
|
||||
state_of() {
|
||||
printf '%s' "$1" | python3 -c \
|
||||
'import json,sys; print(json.load(sys.stdin)["result"]["task"]["status"]["state"])'
|
||||
@@ -53,7 +52,7 @@ task_of() {
|
||||
'import json,sys; print(json.load(sys.stdin)["result"]["task"]["id"])'
|
||||
}
|
||||
|
||||
# ---- yes: pause, then run the write ----
|
||||
# ---- a write runs at once, no pause, no question ----
|
||||
printf 'tool write {"path":"%s/written.txt","content":"yes"}\nanswer done\n' \
|
||||
"$D" > "$D/model.txt"
|
||||
HOME="$D/home" ./splinter --no-config --script "$D/model.txt" \
|
||||
@@ -63,18 +62,20 @@ S1=$!
|
||||
check wait_port "$P"
|
||||
|
||||
r=$(first "$P")
|
||||
check test "$(state_of "$r")" = "TASK_STATE_INPUT_REQUIRED"
|
||||
tid=$(task_of "$r")
|
||||
check test -n "$tid"
|
||||
check test ! -e "$D/written.txt"
|
||||
|
||||
r=$(follow "$P" "$tid" "yes")
|
||||
check test "$(state_of "$r")" = "TASK_STATE_COMPLETED"
|
||||
check test -e "$D/written.txt"
|
||||
check grep -q 'yes' "$D/written.txt"
|
||||
check sh -c "! printf '%s' '$r' | grep -q INPUT_REQUIRED"
|
||||
check sh -c "printf '%s' '$r' | grep -q 'done'"
|
||||
|
||||
# ---- no: pause, then complete without the write ----
|
||||
printf 'tool write {"path":"%s/notwritten.txt","content":"no"}\nanswer done\n' \
|
||||
# ---- CancelTask on a finished task is refused, not silently accepted ----
|
||||
tid=$(task_of "$r")
|
||||
check test -n "$tid"
|
||||
r=$(post "$P" "{\"jsonrpc\":\"2.0\",\"id\":\"3\",\"method\":\"CancelTask\",\"params\":{\"id\":\"$tid\"}}")
|
||||
check sh -c "printf '%s' '$r' | grep -q 'error'"
|
||||
|
||||
# ---- a destructive call (run) also runs through ----
|
||||
printf 'tool run {"command":"touch %s/rm-me.txt"}\nanswer removed\n' \
|
||||
"$D" > "$D/model2.txt"
|
||||
HOME="$D/home2" ./splinter --no-config --script "$D/model2.txt" \
|
||||
--a2a-server-listen 127.0.0.1 --a2a-listen-port "$P2" \
|
||||
@@ -83,26 +84,8 @@ S2=$!
|
||||
check wait_port "$P2"
|
||||
|
||||
r=$(first "$P2")
|
||||
check test "$(state_of "$r")" = "TASK_STATE_INPUT_REQUIRED"
|
||||
tid=$(task_of "$r")
|
||||
r=$(follow "$P2" "$tid" "no")
|
||||
check test "$(state_of "$r")" = "TASK_STATE_COMPLETED"
|
||||
check test ! -e "$D/notwritten.txt"
|
||||
|
||||
# ---- cancel a task that is waiting for input ----
|
||||
printf 'tool write {"path":"%s/canceled.txt","content":"x"}\nanswer done\n' \
|
||||
"$D" > "$D/model3.txt"
|
||||
HOME="$D/home3" ./splinter --no-config --script "$D/model3.txt" \
|
||||
--a2a-server-listen 127.0.0.1 --a2a-listen-port "$P3" \
|
||||
> "$D/gw3.out" 2>&1 &
|
||||
S3=$!
|
||||
check wait_port "$P3"
|
||||
r=$(first "$P3")
|
||||
check test "$(state_of "$r")" = "TASK_STATE_INPUT_REQUIRED"
|
||||
tid=$(task_of "$r")
|
||||
r=$(post "$P3" "{\"jsonrpc\":\"2.0\",\"id\":\"3\",\"method\":\"CancelTask\",\"params\":{\"id\":\"$tid\"}}")
|
||||
check test "$(state_of "$r")" = "TASK_STATE_CANCELED"
|
||||
check test ! -e "$D/canceled.txt"
|
||||
check test -e "$D/rm-me.txt"
|
||||
|
||||
if [ "$fails" -eq 0 ]; then echo "test_a2a_input: ok"; exit 0; fi
|
||||
echo "--- gw1 ---"; tail -5 "$D/gw1.out" 2>/dev/null
|
||||
|
||||
+24
-5
@@ -64,6 +64,13 @@ setup(struct sp_registry *r)
|
||||
t.auth = SP_AUTH_READ;
|
||||
t.run = failing_tool;
|
||||
(void)sp_registry_add(r, &t);
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "destroy";
|
||||
t.description = "pretend to destroy";
|
||||
t.auth = SP_AUTH_DESTRUCTIVE;
|
||||
t.run = echo_tool;
|
||||
(void)sp_registry_add(r, &t);
|
||||
}
|
||||
|
||||
/* a stream that thinks, dies without an answer, then answers */
|
||||
@@ -407,18 +414,19 @@ main(void)
|
||||
CHECK(strstr(m.last_request.p, "unknown tool 'nope'") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 4. a mutating tool without consent is denied, and the denial is the
|
||||
* observation the model sees (fail closed: no approval input) */
|
||||
/* 4. a mutating tool runs without consent: splinter does not gate its
|
||||
* own tools, the registry decides what an agent may reach */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool write x") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer ok") == 0);
|
||||
CHECK(run_turn(&m, &r, "do it", 0, 8, 0, &answer, &err, &st) == 0);
|
||||
CHECK(st.denied == 1);
|
||||
CHECK(strstr(m.last_request.p, "denied by operator") != NULL);
|
||||
CHECK(st.denied == 0);
|
||||
CHECK(strstr(m.last_request.p, "observe write x") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 5. the same tool runs when the operator allowed autonomy */
|
||||
/* 5. and `autonomous` changes nothing: the flag is kept for config
|
||||
* compatibility only */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool write x") == 0);
|
||||
@@ -428,6 +436,17 @@ main(void)
|
||||
CHECK(strstr(m.last_request.p, "observe write x") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 5b. a destructive tool runs too */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool destroy x") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer ok") == 0);
|
||||
CHECK(run_turn(&m, &r, "do it", 0, 8, 0, &answer, &err, &st) == 0);
|
||||
CHECK(st.denied == 0);
|
||||
CHECK(st.tool_calls == 1);
|
||||
CHECK(strstr(m.last_request.p, "observe destroy x") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 6. a failing tool becomes an observation, not a crash */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
|
||||
+16
-3
@@ -1,6 +1,7 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# splinter -- Tab switches the agent profile; plan cannot run commands.
|
||||
# splinter -- Tab switches the agent profile; plan cannot run commands, and
|
||||
# build runs them without a question in front of the tool.
|
||||
set -u
|
||||
cd "$(dirname "$0")/.." || exit 1
|
||||
. "$(dirname "$0")/sh/port.sh"
|
||||
@@ -33,6 +34,16 @@ shows() {
|
||||
done
|
||||
return 1
|
||||
}
|
||||
# a condition that becomes true a moment later. A screen check is not enough
|
||||
# here: the text of the refused turn is still on the pane, so "obs: " appears
|
||||
# before the build turn has even started.
|
||||
waitfor() {
|
||||
for i in $(seq 1 40); do
|
||||
"$@" && return 0
|
||||
sleep 0.25
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
mkdir -p "$D/home/.splinter/agents" "$D/proj"
|
||||
printf -- '---\ndescription: only reads\ntools: read\n---\nBe careful.\n' \
|
||||
@@ -72,13 +83,15 @@ tmux send-keys -t $S Enter
|
||||
check "plan refuses the command" shows "not available in the 'plan' agent"
|
||||
check "nothing was created" test ! -e "$D/made"
|
||||
|
||||
# in build the same request goes through (autonomous is off, so it asks)
|
||||
# in build the same request goes through - no question in front of the tool,
|
||||
# the profile's policy is the only thing that decides what the agent may reach
|
||||
tmux send-keys -t $S Tab
|
||||
tmux send-keys -t $S Tab
|
||||
sleep 1
|
||||
tmux send-keys -t $S -l 'nochmal'
|
||||
tmux send-keys -t $S Enter
|
||||
check "build asks for consent" shows 'y/n\|allow\|approve\|Allow\|Approve'
|
||||
check "build runs the command" waitfor test -e "$D/made"
|
||||
check "and the model read the observation" shows 'exit status 0'
|
||||
|
||||
# The language is English unless a config or SPLINTER_LOCALE says otherwise;
|
||||
# the desktop's LANG must not change it behind the operator's back.
|
||||
|
||||
+7
-13
@@ -33,7 +33,6 @@ run() { # $1 = answer word; leaves $D/sent.log and maybe $D/out.txt
|
||||
fi
|
||||
HOME="$D/home" SPLINTER_LOCALE=de SPLINTER_TELEGRAM_TOKEN=tok \
|
||||
SPLINTER_TELEGRAM_API="http://127.0.0.1:$P" \
|
||||
SPLINTER_ACK_DIR="$D/home/ack" SPLINTER_ACK_TTL=20 \
|
||||
./splinter --config "$D/home/splinter.conf" --daemon \
|
||||
> "$D/gw.out" 2>&1 &
|
||||
G=$!
|
||||
@@ -56,20 +55,15 @@ check "the menu carries plugin commands" grep -q '^cmd: goal' "$D/sent.log"
|
||||
check "the menu carries command scripts" grep -q '^cmd: hello' "$D/sent.log"
|
||||
check "the menu carries /status" grep -q '^cmd: status' "$D/sent.log"
|
||||
check "the menu carries /menu" grep -q '^cmd: menu' "$D/sent.log"
|
||||
check "approval is asked in the chat, with the tool" \
|
||||
grep -q '^Zustimmung [0-9]*: write ' "$D/sent.log"
|
||||
check "approval carries inline buttons" \
|
||||
grep -q '"callback_data":"ack:' "$D/sent.log"
|
||||
check "a question meanwhile is told to wait" \
|
||||
grep -q '^Ich warte noch' "$D/sent.log"
|
||||
check "the yes is acknowledged" grep -q '^Zustimmung erteilt' "$D/sent.log"
|
||||
check "the tool ran after the yes" grep -q 'vom gateway' "$D/out.txt"
|
||||
# a mutating tool runs without a question in the chat: splinter does not gate
|
||||
# its own tools, and the gateway has no approval step to put to anyone
|
||||
check "the tool ran without an approval" grep -q 'vom gateway' "$D/out.txt"
|
||||
check "no approval question in the chat" \
|
||||
sh -c "! grep -q '^Zustimmung [0-9]*: write ' '$D/sent.log'"
|
||||
check "no approval buttons in the chat" \
|
||||
sh -c "! grep -q '\"callback_data\":\"ack:' '$D/sent.log'"
|
||||
check "the turn ends with the answer" grep -q '^fertig' "$D/sent.log"
|
||||
|
||||
run no 1
|
||||
check "the no is acknowledged" grep -q '^Abgelehnt' "$D/sent.log"
|
||||
check "the tool did not run after the no" test ! -e "$D/out.txt"
|
||||
|
||||
# a program in commands/ answers in the chat, without a model turn
|
||||
run_text() { # $1 = first message, $2 = line to wait for, $3 = port offset,
|
||||
# $4 = a line that must appear too before the fake is killed
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
# What it guards: a completion with a local gw_ tool is answered after the
|
||||
# gateway runs it here; a caller tool is returned to the client with its
|
||||
# arguments; the caller's follow-up reaches the upstream; gw_read reads locally;
|
||||
# gw_write needs --autonomous; a token is enforced; gw_ is reserved; the client
|
||||
# gw_write runs without --autonomous; a token is enforced; gw_ is reserved; the client
|
||||
# can ask for SSE; and beyond loopback the server refuses without a token.
|
||||
set -u
|
||||
cd "$(dirname "$0")/.." || exit 1
|
||||
@@ -138,12 +138,15 @@ curl -s -X POST "http://127.0.0.1:$P1/v1/chat/completions" \
|
||||
--data-binary "@$D/chunked.body" > "$D/chunked.json"
|
||||
checkgrep 'ERGEBNIS: hallo-vom-gw' "$D/chunked.json"
|
||||
|
||||
# mutating tools need --autonomous
|
||||
# a mutating tool runs on both gateways: no approval, no --autonomous gate.
|
||||
# The registry says what an agent may reach, not a prompt the endpoint cannot
|
||||
# put to anyone.
|
||||
rm -f "$D/written.txt"
|
||||
post "$P2" '{"model":"x","messages":[{"role":"user","content":"gw-write"}]}' \
|
||||
"$D/write-denied.json"
|
||||
checkgrep 'denied: the gateway runs mutating' "$D/write-denied.json"
|
||||
check test ! -e "$D/written.txt"
|
||||
"$D/write-noauto.json"
|
||||
check test -s "$D/written.txt"
|
||||
checkgrep 'geschrieben' "$D/written.txt"
|
||||
rm -f "$D/written.txt"
|
||||
post "$P1" '{"model":"x","messages":[{"role":"user","content":"gw-write"}]}' \
|
||||
"$D/write-ok.json"
|
||||
check test -s "$D/written.txt"
|
||||
|
||||
@@ -56,8 +56,10 @@ assert init["result"]["serverInfo"]["name"] == "splinter", init
|
||||
tools = [t["name"] for t in req(2, "tools/list")["result"]["tools"]]
|
||||
for want in ("splinter_sessions", "splinter_state", "splinter_models",
|
||||
"splinter_history", "splinter_say", "splinter_steer",
|
||||
"splinter_cancel", "splinter_approve", "splinter_answer"):
|
||||
"splinter_cancel", "splinter_answer"):
|
||||
assert want in tools, (want, tools)
|
||||
# there is no approval to answer: splinter does not gate its own tools
|
||||
assert "splinter_approve" not in tools, tools
|
||||
|
||||
state = call(3, "splinter_state", {})
|
||||
assert "model: m" in state, state
|
||||
|
||||
+63
-51
@@ -1,10 +1,17 @@
|
||||
/*
|
||||
* splinter -- a turn that pauses for approval and resumes.
|
||||
* splinter -- a mutating tool runs without asking anyone.
|
||||
*
|
||||
* The tool has not run when the turn returns SP_AGENT_INPUT_REQUIRED: the
|
||||
* assistant call is in the conversation, no observation is. Resuming with a
|
||||
* decision runs the pending call, adds its observation and continues the
|
||||
* model loop. A denial is an observation too, and the turn still ends.
|
||||
* This test used to guard the opposite: a turn that parked before a mutate
|
||||
* tool and waited for an approval, then resumed with a yes or a no. That
|
||||
* gate is gone, because it was the program deciding what the operator may
|
||||
* do. What stays true, and is guarded here:
|
||||
*
|
||||
* - a mutate tool the registry offers runs in the same turn, no pause;
|
||||
* - a destructive tool runs too;
|
||||
* - the turn never returns a "needs a person" code, so no front end has
|
||||
* to carry a resume path;
|
||||
* - a denial only happens where the operator configured one (the agent's
|
||||
* tool policy), never from a built-in prompt.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -31,12 +38,20 @@ write_tool(const char *args, struct sp_buf *out, void *ctx)
|
||||
}
|
||||
|
||||
static int
|
||||
approve(void *ctx, const char *name, int auth, const char *args)
|
||||
run_turn(struct sp_agent *a, struct sp_conv *conv, struct sp_mock *m,
|
||||
struct sp_buf *answer, struct sp_buf *err)
|
||||
{
|
||||
(void)name;
|
||||
(void)auth;
|
||||
(void)args;
|
||||
return *(int *)ctx;
|
||||
int rc;
|
||||
|
||||
sp_conv_init(conv);
|
||||
a->conv = conv;
|
||||
CHECK(sp_mock_init(m) == 0);
|
||||
CHECK(sp_mock_push(m, 200, "tool write x") == 0);
|
||||
CHECK(sp_mock_push(m, 200, "answer done") == 0);
|
||||
a->tr = sp_mock_transport(m);
|
||||
sp_buf_clear(answer);
|
||||
rc = sp_agent_run(a, "go", answer, err);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int
|
||||
@@ -49,7 +64,7 @@ main(void)
|
||||
struct sp_conv conv;
|
||||
struct sp_mock m;
|
||||
struct sp_buf answer, err;
|
||||
int verdict;
|
||||
int rc;
|
||||
|
||||
memset(&r, 0, sizeof(r));
|
||||
memset(&t, 0, sizeof(t));
|
||||
@@ -59,66 +74,63 @@ main(void)
|
||||
t.run = write_tool;
|
||||
CHECK(sp_registry_add(&r, &t) == 0);
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "rm";
|
||||
t.description = "destructive";
|
||||
t.auth = SP_AUTH_DESTRUCTIVE;
|
||||
t.run = write_tool;
|
||||
CHECK(sp_registry_add(&r, &t) == 0);
|
||||
|
||||
memset(&a, 0, sizeof(a));
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
cfg.max_steps = 5;
|
||||
cfg.autonomous = 0;
|
||||
cfg.approve = approve;
|
||||
cfg.approve_ctx = &verdict;
|
||||
cfg.approve_is_channel = 1;
|
||||
sp_conv_init(&conv);
|
||||
a.reg = &r;
|
||||
a.cfg = &cfg;
|
||||
a.conv = &conv;
|
||||
CHECK(sp_buf_init(&answer) == 0);
|
||||
CHECK(sp_buf_init(&err) == 0);
|
||||
|
||||
/* a call that needs a person: the turn stops before it runs */
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool write x") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer done") == 0);
|
||||
a.tr = sp_mock_transport(&m);
|
||||
verdict = 2;
|
||||
CHECK(sp_agent_run(&a, "go", &answer, &err) == SP_AGENT_INPUT_REQUIRED);
|
||||
CHECK(ran == 0);
|
||||
CHECK(a.has_pending == 1);
|
||||
CHECK(conv.n >= 2);
|
||||
CHECK(strcmp(conv.v[conv.n - 1].role, "assistant") == 0);
|
||||
|
||||
/* yes: the pending call runs, its observation goes back, the turn ends */
|
||||
CHECK(sp_agent_resume(&a, 1, &answer, &err) == 0);
|
||||
/* a mutate tool runs in the same turn: no pause, no question, and the
|
||||
* model sees its observation before it answers */
|
||||
ran = 0;
|
||||
rc = run_turn(&a, &conv, &m, &answer, &err);
|
||||
CHECK(rc == 0);
|
||||
CHECK(ran == 1);
|
||||
CHECK(a.has_pending == 0);
|
||||
CHECK(strcmp(answer.p, "done") == 0);
|
||||
CHECK(strstr(m.last_request.p, "observe write written") != NULL);
|
||||
CHECK(strstr(m.last_request.p, "denied") == NULL);
|
||||
sp_mock_free(&m);
|
||||
sp_conv_free(&conv);
|
||||
|
||||
/* no: the call does not run, but it is an observation and the turn ends */
|
||||
/* a destructive tool runs too: the operator decides what the agent may
|
||||
* reach by what is in the registry, not by answering a prompt */
|
||||
ran = 0;
|
||||
CHECK(run_turn(&a, &conv, &m, &answer, &err) == 0);
|
||||
CHECK(ran == 1);
|
||||
sp_mock_free(&m);
|
||||
sp_conv_free(&conv);
|
||||
|
||||
/*
|
||||
* The one denial that stays is the operator's own tool policy: a
|
||||
* profile that says "read only" is a configuration, not a prompt.
|
||||
*/
|
||||
memset(&a, 0, sizeof(a));
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
cfg.max_steps = 5;
|
||||
cfg.approve = approve;
|
||||
cfg.approve_ctx = &verdict;
|
||||
cfg.approve_is_channel = 1;
|
||||
sp_conv_init(&conv);
|
||||
a.reg = &r;
|
||||
a.cfg = &cfg;
|
||||
a.conv = &conv;
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool write x") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer done") == 0);
|
||||
a.tr = sp_mock_transport(&m);
|
||||
verdict = 2;
|
||||
CHECK(sp_agent_run(&a, "go", &answer, &err) == SP_AGENT_INPUT_REQUIRED);
|
||||
CHECK(sp_agent_resume(&a, 0, &answer, &err) == 0);
|
||||
CHECK(ran == 0);
|
||||
CHECK(a.has_pending == 0);
|
||||
CHECK(strstr(m.last_request.p, "observe write") != NULL);
|
||||
CHECK(strstr(m.last_request.p, "denied by operator") != NULL);
|
||||
sp_mock_free(&m);
|
||||
sp_conv_free(&conv);
|
||||
ran = 0;
|
||||
{
|
||||
struct sp_tool_policy pol;
|
||||
|
||||
memset(&pol, 0, sizeof(pol));
|
||||
pol.max_auth = SP_AUTH_READ;
|
||||
a.policy = &pol;
|
||||
CHECK(run_turn(&a, &conv, &m, &answer, &err) == 0);
|
||||
CHECK(ran == 0);
|
||||
CHECK(strstr(m.last_request.p, "denied") != NULL);
|
||||
sp_mock_free(&m);
|
||||
sp_conv_free(&conv);
|
||||
}
|
||||
|
||||
sp_buf_free(&answer);
|
||||
sp_buf_free(&err);
|
||||
|
||||
+13
-10
@@ -329,13 +329,13 @@ HOME=$D/home SPLINTER_LOCALE=en ./splinterctl --socket "$D/sock" \
|
||||
--cmd /model > "$D/model.out" 2>/dev/null
|
||||
checkgrep 'local/m' "$D/model.out"
|
||||
|
||||
# ------------------------------------------------- an approval is a question
|
||||
# --------------------------------------- a mutating tool runs without asking
|
||||
[ -n "$S1" ] && kill "$S1" 2>/dev/null
|
||||
S1=
|
||||
sleep 0.3
|
||||
rm -f "$D/sock"
|
||||
mkdir -p "$D/home2"
|
||||
python3 -I tests/fake_telegram.py "$P2" "$D/denied.txt" "$D/sent2.log" ok \
|
||||
python3 -I tests/fake_telegram.py "$P2" "$D/written2.txt" "$D/sent2.log" ok \
|
||||
> "$D/fake2.out" 2>&1 &
|
||||
F2=$!
|
||||
if ! wait_listen "$P2" "$F2"; then
|
||||
@@ -352,7 +352,7 @@ if ! wait_for "$D/sock2" 20; then
|
||||
cat "$D/serve2.out"
|
||||
exit 1
|
||||
fi
|
||||
python3 - "$D/sock2" > "$D/deny.out" 2> "$D/deny.err" <<'PY'
|
||||
python3 - "$D/sock2" > "$D/run.out" 2> "$D/run.err" <<'PYEOF'
|
||||
import json, socket, sys
|
||||
|
||||
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
@@ -365,21 +365,24 @@ while True:
|
||||
f.write(json.dumps({"cmd": "say", "text": "schreib"}) + "\n")
|
||||
f.flush()
|
||||
asked = False
|
||||
ran = False
|
||||
while True:
|
||||
ev = json.loads(f.readline())
|
||||
if ev.get("ev") == "approval":
|
||||
asked = True
|
||||
f.write(json.dumps({"cmd": "approve", "allow": False}) + "\n")
|
||||
f.flush()
|
||||
if ev.get("ev") == "tool_result":
|
||||
ran = True
|
||||
if ev.get("ev") == "turn":
|
||||
print("asked" if asked else "no question")
|
||||
print("ran" if ran else "did not run")
|
||||
print("ok" if ev.get("ok") else "failed")
|
||||
break
|
||||
PY
|
||||
checkgrep 'asked' "$D/deny.out"
|
||||
checkgrep 'ok' "$D/deny.out"
|
||||
# the denial is the observation: the tool never ran
|
||||
check test ! -e "$D/denied.txt"
|
||||
PYEOF
|
||||
checkgrep 'no question' "$D/run.out"
|
||||
checkgrep 'ran' "$D/run.out"
|
||||
checkgrep 'ok' "$D/run.out"
|
||||
# the tool really ran: the file the model asked for is there
|
||||
check test -e "$D/written2.txt"
|
||||
|
||||
# ------------------------------------------------------------------- quit
|
||||
python3 - "$D/sock2" <<'PY'
|
||||
|
||||
@@ -229,7 +229,7 @@ test_mcp(void)
|
||||
CHECK(sp_buf_init(&err) == 0);
|
||||
|
||||
unsetenv("SPLINTER_MCP_TOKEN");
|
||||
CHECK(sp_mcp_serve(NULL, NULL, &r, "0.0.0.0", port, 0, 1, &err) != 0);
|
||||
CHECK(sp_mcp_serve(NULL, NULL, &r, "0.0.0.0", port, 0, &err) != 0);
|
||||
CHECK(strstr(err.p, "SPLINTER_MCP_TOKEN") != NULL);
|
||||
|
||||
setenv("SPLINTER_MCP_TOKEN", "richtig", 1);
|
||||
@@ -238,7 +238,7 @@ test_mcp(void)
|
||||
struct sp_buf e;
|
||||
|
||||
(void)sp_buf_init(&e);
|
||||
_exit(sp_mcp_serve(NULL, NULL, &r, "127.0.0.1", port, 0, 1, &e) == 0 ? 0 : 1);
|
||||
_exit(sp_mcp_serve(NULL, NULL, &r, "127.0.0.1", port, 0, &e) == 0 ? 0 : 1);
|
||||
}
|
||||
CHECK(srv > 0);
|
||||
|
||||
|
||||
+12
-13
@@ -2,11 +2,10 @@
|
||||
#
|
||||
# splinter -- the curses UI, driven through tmux against a fake model.
|
||||
#
|
||||
# What it guards, as found: a tool call is asked on the screen and runs after
|
||||
# 'y'; Ctrl-C during that question cancels the turn (it used to be swallowed,
|
||||
# leaving y or n as the only way out); Ctrl-L repaints instead of sending the
|
||||
# half-typed line; Backspace edits the buffer, not only the screen; '!' and '/'
|
||||
# lines are handled; a resize does not end the program; Ctrl-D quits with 0.
|
||||
# What it guards, as found: a tool call shows its row and runs, no question in
|
||||
# front of it; Ctrl-L repaints instead of sending the half-typed line; Backspace
|
||||
# edits the buffer, not only the screen; '!' and '/' lines are handled; a resize
|
||||
# does not end the program; Ctrl-D quits with 0.
|
||||
# Skipped, and says so, where tmux is not installed.
|
||||
set -u
|
||||
cd "$(dirname "$0")/.." || exit 1
|
||||
@@ -125,15 +124,14 @@ typed '/new'; key Enter
|
||||
check shows 'New conversation: sess-'
|
||||
typed '/connect'; key Enter
|
||||
check shows 'No configuration directory to write to.'
|
||||
# a mutating tool runs without a question - no prompt, no 'y'. The row names
|
||||
# the call (verb, path) and never the arguments as the model sent them.
|
||||
typed 'schreib die datei'; key Enter
|
||||
check shows 'changes files'
|
||||
# the question names the call - verb, path, class - and never the arguments
|
||||
# as the model sent them ("write /tmp/x/out.txt", not write {"path": ...})
|
||||
check shows 'write '
|
||||
check not_shows '{"path"'
|
||||
key y
|
||||
check shows 'fertig'
|
||||
check shows '>> write '
|
||||
check shows 'fertig'
|
||||
check test -e "$D/out.txt"
|
||||
# the status bar above the prompt: context gauge, cache share, speed
|
||||
check shows 'ctx 1290/131072'
|
||||
check shows 'cache 62%'
|
||||
@@ -169,11 +167,12 @@ check shows 'retrying in'
|
||||
check shows 'antwort nach dem retry'
|
||||
check test "$(quit_code)" = 0
|
||||
|
||||
# nothing is asked, so the call cannot be cancelled beforehand: the tool has
|
||||
# already run when the row appears. What still holds is that the chat stays
|
||||
# usable afterwards.
|
||||
start 1
|
||||
typed 'schreib die datei'; key Enter
|
||||
check shows 'changes files'
|
||||
key C-c; sleep 2
|
||||
check test ! -e "$D/out.txt"
|
||||
check shows 'fertig'
|
||||
typed '!echo noch-da'; key Enter
|
||||
check shows 'noch-da'
|
||||
check test "$(quit_code)" = 0
|
||||
|
||||
Reference in new issue
Block a user