From df6bfa9cfbcc40ac93ccf6ee85ed79ff384fe695 Mon Sep 17 00:00:00 2001 From: lotherk Date: Tue, 24 Nov 2026 09:00:59 +0000 Subject: [PATCH] telegram: /cancel ends an open ask question instead of answering it The hint under the question promised "/cancel cancels the turn", but the gateway knew the command nowhere -- a /cancel was handed to the model as the operator's answer and the turn went on with that as its decision. The gateway runs its turns on sp_agent, so there was no cancel flag the chat could set. tg_ask now takes /cancel (and "cancel", case-blind) as the way out: it sets tg_cancelled, says so in the chat (new key ask.cancelled, de/en/pt_BR) and returns -2, which the tool reports as "no answer: the operator cancelled". gcfg.cancel points at that flag -- the agent already reads cfg->cancel between steps -- and it is cleared at the start of every turn so a cancel ends the turn it was meant for. Test evidence: tests/test_gateway.sh grew a case where the fake operator replies /cancel to the question. Proven red against the old code (2 FAILs: "/cancel is acknowledged in the chat", "/cancel does not reach the model as the answer") and green on the fix. The existing gateway case is unchanged (FAKE_ANSWER defaults to ok). test_gateway: ok, make check green (336 s). --- src/backend/telegram.c | 44 +++++++++++++++++++++++++++++++++++++++++- tests/fake_telegram.py | 3 +++ tests/test_gateway.sh | 17 +++++++++++++++- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/backend/telegram.c b/src/backend/telegram.c index 005104e..b5d06ff 100644 --- a/src/backend/telegram.c +++ b/src/backend/telegram.c @@ -1046,6 +1046,15 @@ static struct { const char *allow; } tg_cur; +/* + * Set by /cancel while an `ask` question is open, read by the agent between + * steps. The gateway runs its turns itself (sp_agent_run), so there is no + * engine whose cancel flag the chat could set; this is that flag. It is + * cleared at the start of every turn, so a cancel ends the turn it was meant + * for and not the next one. + */ +static volatile int tg_cancelled; + /* * The model's note to the operator, in the chat the turn came from. * @@ -1112,7 +1121,7 @@ tg_ask(void *ctx, const char *text, const char (*opts)[SP_ASK_OPT_LEN], } (void)send_message(tg_cur.token, tg_cur.chat_id, msg.p, NULL); - end = time(NULL) + SP_ASK_WAIT_S; + end = time(NULL) + sp_ask_wait_s(); while (!tg_stop && time(NULL) < end) { mcp_json *v; const mcp_json *result, *update; @@ -1161,6 +1170,30 @@ tg_ask(void *ctx, const char *text, const char (*opts)[SP_ASK_OPT_LEN], */ 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 + * would hand the word "/cancel" to the model as if the + * operator had answered, and the turn would go on with + * that as its decision. The turn is cancelled through + * the agent's own cancel flag, which is what the chat + * loop's Ctrl-C path sets. + */ + if (strcmp(t, "/cancel") == 0 || + strcasecmp(t, "cancel") == 0) { + struct sp_buf cmsg; + + tg_cancelled = 1; + if (sp_buf_init(&cmsg) == 0) { + sp_buf_puts(&cmsg, sp_td("ask.cancelled", + "cancelled - the question is gone.")); + (void)send_message(tg_cur.token, chat, + cmsg.p, NULL); + sp_buf_free(&cmsg); + } + got = -2; /* cancelled, not answered */ + break; + } snprintf(answer, cap, "%s", t); got = 0; break; @@ -2336,6 +2369,13 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token, gcfg.ask = tg_ask; gcfg.ask_ctx = &tg_cur; } + /* + * The chat has no Ctrl-C: /cancel is the only way out of a turn, + * and it sets this flag. The agent already reads cfg->cancel + * between steps, so a cancelled turn ends with + * "cancelled by operator" instead of running on. + */ + gcfg.cancel = &tg_cancelled; ag->cfg = &gcfg; } @@ -2622,6 +2662,8 @@ sp_telegram_run(struct sp_agent *ag, struct sp_host *host, const char *token, tg_cur.token = token; tg_cur.chat_id = chat_id; tg_cur.allow = allow; + /* a cancel ends the turn it was meant for */ + tg_cancelled = 0; if (sp_engine_agent_turn_begin(ag) != 0) { (void)send_message(token, chat_id, sp_td("tg.busy", "busy - a turn is running"), diff --git a/tests/fake_telegram.py b/tests/fake_telegram.py index 2077526..b0d5389 100644 --- a/tests/fake_telegram.py +++ b/tests/fake_telegram.py @@ -147,6 +147,9 @@ class H(http.server.BaseHTTPRequestHandler): # on the question, not on the hint -- the hint is translated and # this fake runs under SPLINTER_LOCALE=de in the gateway test. if "Which colour should the login page be?" in text: + # ANSWER is the operator's reply. "/cancel" is a real answer + # word here too: the gateway must treat it as the way out of + # the question, not hand it to the model as a decision. add_update(ANSWER) m = re.match(r"Zustimmung (\d+):", text) if m: diff --git a/tests/test_gateway.sh b/tests/test_gateway.sh index bf65eff..a600d0d 100755 --- a/tests/test_gateway.sh +++ b/tests/test_gateway.sh @@ -81,7 +81,10 @@ run_text() { # $1 = first message, $2 = line to wait for, $3 = port offset, chmod +x "$D/home/.splinter/commands/hello" printf 'endpoint = http://127.0.0.1:%s/v1/chat/completions\nmodel = m\ntelegram_allow = 7\ndelegate = far | test | endpoint=http://127.0.0.1:9/v1/chat/completions | model=other\n' \ "$P" > "$D/home/splinter.conf" - python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" ok "$1" \ + # $FAKE_ANSWER lets one case reply with something other than "ok" -- + # the /cancel case below needs the operator's message to be /cancel. + python3 -I tests/fake_telegram.py "$P" "$D/out.txt" "$D/sent.log" \ + "${FAKE_ANSWER:-ok}" "$1" \ > "$D/fake.out" 2>&1 & S=$! if ! wait_listen "$P" "$S"; then @@ -235,6 +238,18 @@ check "the operator's answer reaches the model" \ check "the model is not told nobody is there" \ sh -c "! grep -q 'nobody can be asked' '$D/sent.log'" +# /cancel while the question is open: the hint under the question promises it, +# so it has to work. Before this it was handed to the model as the operator's +# answer -- the turn went on with "/cancel" as its decision. +FAKE_ANSWER=/cancel run_text "frag den operator" 'abgebrochen' 106 +check "the question is asked" \ + grep -q 'Which colour should the login page be?' "$D/sent.log" +check "/cancel is acknowledged in the chat" grep -q 'abgebrochen' "$D/sent.log" +check "/cancel does not reach the model as the answer" \ + sh -c "! grep -q 'antwort war: /cancel' '$D/sent.log'" +check "the turn ended instead of going on" \ + sh -c "! grep -q 'antwort war: ok' '$D/sent.log'" + # 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