gateway: the retry is announced in the chat, not into a screen queue

The engine's note callback pushes onto the screen event queue. A gateway has
no screen, so the queue was never read: five corrected tries five seconds
apart were twenty-five seconds of silence in Telegram, and the operator saw
nothing until the turn finally answered.

The gateway now installs its own note callback and sends the line into the
chat the turn came from. It is safe on the polling thread because the turn
runs there itself, not on a worker.

The two lines a broken call leaves behind were hardcoded English while the
transport retry was already translated; both keys are now in en/de/pt_BR.

Tests: test_gateway sends "kaputt", the fake answers with a tool call whose
arguments are not JSON, and the chat must carry
"kaputter Tool-Call, neuer Versuch in 0 s (1/5)" and still end with "fertig".
Counter-check: with the engine's own note restored the same run says
"FAIL a broken call is announced in the chat". make check green (273 s),
check_asan green over 45 binaries.
This commit is contained in:
lotherk committed 2026-11-22 17:31:36 +00:00
1 parent 082223af96
commit bc0b9f22f9
8 files changed
+107 -8

No files matched your search

+12
View File
@@ -379,6 +379,18 @@ a status row `retrying in 5 s: ...` joins the transcript below the call, so a
pause does not look like a hang and nothing is printed into the input line.
Ctrl-C during a wait ends it at once.
A tool call whose arguments are not JSON is a different failure with a
different answer. Sending the identical prompt asks for the identical broken
call, so the request is not repeated: the turn appends a user message that
quotes what came back and says what was wrong with it - a call cut off by the
output limit is told to be smaller, everything else to be one complete JSON
object. Five such corrections are allowed, five seconds apart, and each one is
announced (`broken tool call, trying again in 5 s (2/5)`), so a gateway that
shows no screen still tells the operator why it is quiet. When the fifth
correction is spent the turn stops asking for a call at all and asks for words:
the operator gets what the model wanted to do, step by step, instead of a turn
that died on "model sent malformed tool arguments".
A stream is never cut for being slow: a router may wait minutes for an upstream
without sending anything but keepalive lines, and a byte-per-second guard cut
exactly those with "Less than 1 bytes/sec". The overall HTTP timeout is the
+2
View File
@@ -18,6 +18,8 @@ ui.compacted = Kontext kompaktiert: %zu->%zu Nachrichten, %s->%s (%s->%s)
ui.compact_nohost = compact: kein Plugin-Host, nichts zu kompaktieren
ui.compact_pending = Kompaktierung eingereiht: laeuft am Ende des Turns (Ziel %zu Tokens)
ui.steer_queued_line = Steuerung eingereiht: %s
ui.badtool_note = kaputter Tool-Call, neuer Versuch in %d s (%d/%d)
agent.badcall_answer = Ich konnte keinen ausfuehrbaren Tool-Call senden: die Argumente waren kein gueltiges JSON. Nichts lief. Der rohe Aufruf steht im Log; sag mir, wie es weitergehen soll.
ui.steer_failed = Steuerung nicht angenommen (kein Turn laeuft)
ui.model_title = Modell
ui.open_busy = Sitzungswechsel geht nicht waehrend ein Turn laeuft; /open danach
+2
View File
@@ -17,6 +17,8 @@ ui.compacted = context compacted: %zu->%zu msgs, %s->%s (%s->%s)
ui.compact_nohost = compact: no plugin host, nothing to compact
ui.compact_pending = compaction queued: it runs when the turn ends (target %zu tokens)
ui.steer_queued_line = steering queued: %s
ui.badtool_note = broken tool call, trying again in %d s (%d/%d)
agent.badcall_answer = I could not send a runnable tool call: the arguments were not valid JSON. Nothing ran. The raw call is in the log; tell me how to continue.
ui.steer_failed = steering not accepted (no turn is running)
ui.model_title = model
ui.open_busy = cannot switch sessions while a turn runs; /open after the turn
+2
View File
@@ -18,6 +18,8 @@ ui.compacted = contexto compactado: %zu->%zu msgs, %s->%s (%s->%s)
ui.compact_nohost = compact: sem host de plugin, nada a compactar
ui.compact_pending = compactacao na fila: roda no fim do turno (alvo %zu tokens)
ui.steer_queued_line = direcionamento na fila: %s
ui.badtool_note = chamada de ferramenta quebrada, nova tentativa em %d s (%d/%d)
agent.badcall_answer = Nao consegui enviar uma chamada de ferramenta executavel: os argumentos nao eram JSON valido. Nada rodou. A chamada bruta esta no log; diga como continuar.
ui.steer_failed = direcionamento nao aceito (nenhum turno em andamento)
ui.model_title = modelo
ui.open_busy = nao da para trocar de sessao durante um turno; /open depois
+5 -4
View File
@@ -943,11 +943,11 @@ agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision,
reply.calls[0].args : "");
if (reply.content == NULL || reply.content[0] == '\0') {
free(reply.content);
reply.content = strdup(
reply.content = strdup(sp_td("agent.badcall_answer",
"I could not send a runnable tool call: the "
"arguments were not valid JSON. Nothing ran. "
"The raw call is in the log; tell me how to "
"continue.");
"continue."));
if (reply.content == NULL) {
sp_reply_free(&reply);
goto oom;
@@ -1067,8 +1067,9 @@ agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision,
char line[256];
snprintf(line, sizeof(line),
"tool call with broken JSON, "
"trying again in %d s (%d/%d)",
sp_td("ui.badtool_note",
"broken tool call, trying again "
"in %d s (%d/%d)"),
retry_wait_ms() / 1000, bad_tool_tries,
SP_BAD_TOOL_TRIES);
a->cfg->note(a->cfg->note_ctx, line);
+39 -4
View File
@@ -1024,6 +1024,31 @@ static struct {
const char *allow;
} tg_cur;
/*
* The model's note to the operator, in the chat the turn came from.
*
* The engine's note callback pushes onto the screen event queue, and a
* gateway has no screen: the queue is never read, so five retries five
* seconds apart were twenty-five seconds of silence. The note goes to the
* chat instead, because that is where the operator is waiting.
*
* Safe only because the turn runs on the polling thread itself (sp_agent_run
* in the update loop, not a worker): the next poll happens after the turn.
*/
static void
tg_note(void *ctx, const char *text)
{
struct sp_buf err;
(void)ctx;
if (text == NULL || *text == '\0' || tg_cur.chat_id == 0)
return;
if (sp_buf_init(&err) != 0)
return;
(void)send_message(tg_cur.token, tg_cur.chat_id, text, &err);
sp_buf_free(&err);
}
/*
* /interview: the model talks to the user, not a questionnaire. The system
* prompt is swapped for the interview prompt and the conversation restarted;
@@ -2158,11 +2183,21 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token,
* either denied every mutating call (no approval stream) or, with ack
* channels, waited for an answer that only it could have read.
*/
if (orig != NULL && !orig->autonomous && orig->approve == NULL) {
if (orig != NULL) {
gcfg = *orig;
gcfg.approve = tg_approve;
gcfg.approve_ctx = &tg_cur;
gcfg.approve_is_channel = 1;
/*
* The engine's note callback writes to the screen event queue,
* and a gateway has no screen. Without this the retry notes
* were pushed and never read: five retries, twenty-five seconds
* of silence.
*/
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;
}
ag->cfg = &gcfg;
}
+12
View File
@@ -17,6 +17,7 @@ sent = []
TARGET = sys.argv[2]
ANSWER = sys.argv[4] if len(sys.argv) > 4 else "ok"
FAILS = [2] # "wiederholen": the first two calls answer HTTP 500
KAPUTT = [1] # "kaputt": the first request sends a tool call with broken JSON
def add_update(text, frm=7):
@@ -218,6 +219,17 @@ class H(http.server.BaseHTTPRequestHandler):
answer = "| a | b |\n|---|---|\n| 1 | 22 |\n"
chunks = [{"choices": [{"delta": {"content": answer}}]},
{"choices": [{"delta": {}, "finish_reason": "stop"}]}]
elif "kaputt" in last_user and KAPUTT[0] > 0:
# a model that sends a tool call whose arguments are not JSON:
# the turn must correct it, and the operator must see that it
# is retrying instead of looking at a silent chat. Only the
# first request is broken; the correction note is not "kaputt",
# so the second one falls through to the normal tool call.
KAPUTT[0] -= 1
chunks = [{"choices": [{"delta": {"tool_calls": [{"index": 0,
"id": "c1", "type": "function", "function": {
"name": "run", "arguments": "{\"command\":"}}]}}]},
{"choices": [{"delta": {}, "finish_reason": "tool_calls"}]}]
elif has_tool:
chunks = [{"choices": [{"delta": {"content": "fertig"}}]},
{"choices": [{"delta": {}, "finish_reason": "stop"}]}]
+33
View File
@@ -243,6 +243,39 @@ check "the summary is in USER.txt" grep -q 'mag C99' "$D/home/.splinter/USER.txt
check "the marker is not in USER.txt" \
sh -c "! grep -q INTERVIEW_COMPLETE '$D/home/.splinter/USER.txt'"
# The model sends a tool call with arguments that are not JSON. The turn
# corrects it and asks again - and the operator must see that in the chat:
# the engine's note callback wrote to a screen queue that a gateway never
# reads, so five retries meant twenty-five seconds of silence.
P=$(free_port)
rm -rf "$D/home" "$D/out.txt" "$D/sent.log"
mkdir -p "$D/home/.splinter"
printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\n' \
"$P" > "$D/home/splinter.conf"
python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok "kaputt" \
> "$D/fake.out" 2>&1 &
S=$!
if ! wait_fake "$P" "$S"; then
echo "FAIL fake telegram did not start on $P"
fails=$((fails + 1))
else
HOME="$D/home" SPLINTER_LOCALE=de SPLINTER_TELEGRAM_TOKEN=tok \
SPLINTER_TELEGRAM_API="http://127.0.0.1:$P" \
SPLINTER_RETRY_WAIT_MS=200 \
./splinter --config "$D/home/splinter.conf" --daemon \
> "$D/gw.out" 2>&1 &
G=$!
for i in $(seq 1 120); do
grep -q '^fertig' "$D/sent.log" 2>/dev/null && break
sleep 0.5
done
kill "$G" "$S" 2>/dev/null
wait "$G" "$S" 2>/dev/null
check "a broken call is announced in the chat" \
grep -q '^kaputter Tool-Call, neuer Versuch in 0 s (1/5)' "$D/sent.log"
check "the turn still ends with an answer" grep -q '^fertig' "$D/sent.log"
fi
if [ "$fails" -eq 0 ]; then echo "test_gateway: ok"; exit 0; fi
cat "$D/gw.out"
exit 1