diff --git a/share/locale/de.msg b/share/locale/de.msg index d911bb0..b9008ab 100644 --- a/share/locale/de.msg +++ b/share/locale/de.msg @@ -15,6 +15,8 @@ ui.untitled = (ohne Titel) ui.session_opened = Sitzung geoeffnet ui.session_gone = Sitzung %s ist weg - jetzt auf %s 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.steer_failed = Steuerung nicht angenommen (kein Turn laeuft) ui.model_title = Modell diff --git a/share/locale/en.msg b/share/locale/en.msg index 6a326ba..6ebf893 100644 --- a/share/locale/en.msg +++ b/share/locale/en.msg @@ -14,6 +14,8 @@ ui.untitled = (untitled) ui.session_opened = session opened ui.session_gone = session %s is gone - now on %s 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.steer_failed = steering not accepted (no turn is running) ui.model_title = model diff --git a/share/locale/pt_BR.msg b/share/locale/pt_BR.msg index 2a770e4..2d1dd15 100644 --- a/share/locale/pt_BR.msg +++ b/share/locale/pt_BR.msg @@ -15,6 +15,8 @@ ui.untitled = (sem titulo) ui.session_opened = sessao aberta ui.session_gone = sessao %s sumiu - agora em %s 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.steer_failed = direcionamento nao aceito (nenhum turno em andamento) ui.model_title = modelo diff --git a/src/backend/engine.c b/src/backend/engine.c index 9546034..acf9e94 100644 --- a/src/backend/engine.c +++ b/src/backend/engine.c @@ -92,6 +92,18 @@ struct sp_engine { char pend_model[320]; int has_pend_model; pthread_mutex_t model_lock; + /* + * A /compact typed while a turn runs. The conversation belongs to the + * worker, so compacting now would race its appends and its realloc; and + * refusing the command outright was worse than it sounds: the operator + * sees "not while a turn runs" for a command that reads as instant, and + * by the time the turn is over nobody repeats it. Measured on a 253 KB + * request whose operator had run /compact three times (2026-11-22). + * So the request is kept - with the token target it was asked for - and + * applied where the turn thread no longer touches the conversation. + */ + size_t pend_compact_tokens; + int has_pend_compact; /* * Held by whoever works on the agent outside the turn thread: a * daemon's Telegram turn for its whole length, an MCP session tool for @@ -164,6 +176,7 @@ struct sp_engine { static void cmd_model_apply(struct sp_engine *, const char *, struct sp_buf *, int); static void engine_apply_pending_model(struct sp_engine *); +static void engine_apply_pending_compact(struct sp_engine *); /* --------------------------------------------------------------- events */ @@ -1266,6 +1279,13 @@ turn_thread(void *arg) ev_push(e, SP_EV_ENTRY, 0, "splinter", note); } engine_apply_pending_model(e); + /* + * A /compact typed while this turn ran. After the model switch, so + * the budget runs against the compactor of the provider that is + * about to send, and before the turn is marked idle, so the + * operator's next keystroke finds a compacted conversation. + */ + engine_apply_pending_compact(e); ev_push(e, SP_EV_TURN_END, rc, e->answer.p != NULL ? e->answer.p : "", e->err.p != NULL ? e->err.p : ""); @@ -1557,6 +1577,11 @@ sp_engine_turn_end(struct sp_engine *e) pthread_mutex_lock(&e->lock); e->ext_busy = 0; pthread_mutex_unlock(&e->lock); + /* + * A /compact typed during a foreign turn (the Telegram daemon's) lands + * here instead of in the turn thread, which never ran one. + */ + engine_apply_pending_compact(e); pthread_mutex_unlock(&e->turn_mx); } @@ -2207,6 +2232,80 @@ cmd_model(struct sp_engine *e, const char *args, struct sp_buf *out) cmd_model_apply(e, args, out, 1); } +/* + * The token target of a /compact line: the argument when it parses, else the + * configured compact_target, else the documented 8000. + */ +static size_t +compact_tokens_from(struct sp_engine *e, const char *args) +{ + size_t tokens = e->conf != NULL && e->conf->compact_target > 0 ? + e->conf->compact_target : 8000; + + if (args != NULL && args[0] != '\0') { + char *end; + unsigned long n = strtoul(args, &end, 10); + + if (*end == '\0' && n > 0 && n < 1000000) + tokens = (size_t)n; + } + return tokens; +} + +/* /compact, applied now: the conversation is nobody else's at this point */ +static void +cmd_compact(struct sp_engine *e, const char *args, struct sp_buf *out) +{ + size_t before = sp_conv_bytes(&e->conv); + size_t msgs = e->conv.n; + size_t tokens = compact_tokens_from(e, args); + size_t budget = tokens * 4; + int changed; + + if (e->host == NULL) { + sp_buf_puts(out, sp_td("ui.compact_nohost", + "compact: no plugin host, nothing to compact\n")); + return; + } + changed = sp_host_compact(e->host, &e->conv, budget); + sp_buf_printf(out, "compact: %zu->%zu msgs, %zu->%zu bytes " + "(target %zu tokens)%s", msgs, e->conv.n, before, + sp_conv_bytes(&e->conv), tokens, + changed ? "" : " - nothing to do"); + ev_push(e, SP_EV_STATE, 0, NULL, NULL); +} + +/* + * A /compact typed while a turn ran. Called from the turn thread with no turn + * in flight, so the conversation has one owner again. + */ +static void +engine_apply_pending_compact(struct sp_engine *e) +{ + size_t tokens; + struct sp_buf out; + + pthread_mutex_lock(&e->lock); + if (!e->has_pend_compact) { + pthread_mutex_unlock(&e->lock); + return; + } + tokens = e->pend_compact_tokens; + e->has_pend_compact = 0; + pthread_mutex_unlock(&e->lock); + if (sp_buf_init(&out) != 0) + return; + { + char arg[32]; + + snprintf(arg, sizeof(arg), "%zu", tokens); + cmd_compact(e, arg, &out); + } + if (out.p != NULL && out.len > 0) + ev_push(e, SP_EV_ENTRY, 0, "splinter", out.p); + sp_buf_free(&out); +} + /* apply the selector a /model left while the turn was running */ static void engine_apply_pending_model(struct sp_engine *e) @@ -2842,7 +2941,7 @@ sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out) */ if (sp_engine_busy(e) && (strcmp(name, "new") == 0 || strcmp(name, "reset") == 0 || - strcmp(name, "compact") == 0 || strcmp(name, "agent") == 0 || + strcmp(name, "agent") == 0 || strcmp(name, "agents") == 0 || strcmp(name, "connect") == 0 || strcmp(name, "self-check") == 0 || strcmp(name, "selfcheck") == 0)) { @@ -2881,30 +2980,26 @@ sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out) return SP_INPUT_HANDLED; } if (strcmp(name, "compact") == 0) { - size_t before = sp_conv_bytes(&e->conv); - size_t msgs = e->conv.n; - size_t tokens = e->conf != NULL && - e->conf->compact_target > 0 ? - e->conf->compact_target : 8000; - size_t budget = tokens * 4; - int changed; + /* + * While a turn runs the conversation belongs to the worker, so the + * request is remembered instead of applied - and instead of being + * refused, which is what made /compact look broken: the operator + * types it while the model is writing, reads "not while a turn + * runs", and never types it again. The turn end applies it. + */ + if (sp_engine_busy(e)) { + size_t tokens = compact_tokens_from(e, args); - if (args[0] != '\0') { - char *end; - unsigned long n = strtoul(args, &end, 10); - - if (*end == '\0' && n > 0 && n < 1000000) { - tokens = (size_t)n; - budget = tokens * 4; - } + pthread_mutex_lock(&e->lock); + e->pend_compact_tokens = tokens; + e->has_pend_compact = 1; + pthread_mutex_unlock(&e->lock); + sp_buf_printf(out, sp_td("ui.compact_pending", + "compaction queued: it runs when the turn ends " + "(target %zu tokens)\n"), tokens); + return SP_INPUT_HANDLED; } - changed = e->host != NULL ? - sp_host_compact(e->host, &e->conv, budget) : 0; - sp_buf_printf(out, "compact: %zu->%zu msgs, %zu->%zu bytes " - "(target %zu tokens)%s", msgs, e->conv.n, before, - sp_conv_bytes(&e->conv), tokens, - changed ? "" : " - nothing to do"); - ev_push(e, SP_EV_STATE, 0, NULL, NULL); + cmd_compact(e, args, out); return SP_INPUT_HANDLED; } if (strcmp(name, "cost") == 0) { diff --git a/tests/test_engine.c b/tests/test_engine.c index 9424c3a..8018553 100644 --- a/tests/test_engine.c +++ b/tests/test_engine.c @@ -122,6 +122,14 @@ main(void) char tmpl[512], script[600], path[700], logpath[760]; char *dir; const char *tmp = getenv("TMPDIR"); + + /* + * A turn whose mock runs out of replies is retried three times, five + * seconds apart. That is the right policy against a real provider and + * the wrong one inside a test suite: it turns a missing line in a + * script into a hang. Same lever as test_agent.c. + */ + (void)setenv("SPLINTER_RETRY_WAIT_MS", "0", 1); FILE *f; char call[256]; int kind; @@ -148,6 +156,12 @@ main(void) /* a second turn whose tool fails: /self-check must name the failure */ fprintf(f, "tool read {\"path\":\"/nonexistent-splinter-test\"}\n"); fprintf(f, "answer done\n"); + /* the queued-/compact turn and the one after it */ + { + int k; + for (k = 0; k < 20; k++) + fprintf(f, "answer fertig\n"); + } fclose(f); (void)setenv("HOME", dir, 1); (void)sp_i18n_init("en"); @@ -468,6 +482,75 @@ main(void) CHECK(out.p != NULL && out.p[0] != '\0'); sp_buf_clear(&out); + /* + * A /compact typed while a turn runs. The conversation belongs to the + * worker, so the command used to be refused with "not while a turn + * runs" - and an operator who reads that while the model is writing + * never repeats it: three /compact in one session, none of them ran + * (2026-11-22, 253 KB requests). It is queued instead, with the target + * it was asked for, and applied when the turn ends. + */ + { + struct sp_buf reply; + struct sp_event ev; + int applied = 0, guard = 0; + + CHECK(sp_buf_init(&reply) == 0); + CHECK(sp_engine_submit(eng, "sag hallo") == 0); + /* the turn owns the conversation now; the command must not race it */ + CHECK(sp_engine_busy(eng) == 1); + kind = sp_engine_input(eng, "/compact 500", &reply); + CHECK(kind == SP_INPUT_HANDLED); + CHECK(contains(reply.p, "queued")); + CHECK(contains(reply.p, "500")); + while (guard++ < 500) { + while (sp_engine_poll(eng, &ev)) { + if (ev.kind == SP_EV_ENTRY && ev.b != NULL && + contains(ev.b, "compact:")) + applied = 1; + sp_engine_event_free(&ev); + } + if (!sp_engine_busy(eng)) + break; + { + struct timespec ts = { 0, 10000000L }; + + nanosleep(&ts, NULL); + } + } + (void)sp_engine_wait(eng); + /* drain what the turn end queued after the last poll */ + while (sp_engine_poll(eng, &ev)) { + if (ev.kind == SP_EV_ENTRY && ev.b != NULL && + contains(ev.b, "compact:")) + applied = 1; + sp_engine_event_free(&ev); + } + CHECK(applied); + /* the queue is a one-shot: it must not fire again on the next turn */ + applied = 0; + CHECK(sp_engine_submit(eng, "sag hallo") == 0); + while (guard++ < 1000) { + while (sp_engine_poll(eng, &ev)) { + if (ev.kind == SP_EV_ENTRY && ev.b != NULL && + contains(ev.b, "compact:")) + applied = 1; + sp_engine_event_free(&ev); + } + if (!sp_engine_busy(eng)) + break; + { + struct timespec ts = { 0, 10000000L }; + + nanosleep(&ts, NULL); + } + } + (void)sp_engine_wait(eng); + CHECK(applied == 0); + sp_buf_clear(&reply); + sp_buf_free(&reply); + } + /* a shell line is an entry event, not a reply */ kind = sp_engine_input(eng, "!echo shell-out", &out); CHECK(kind == SP_INPUT_HANDLED);