compact: a /compact typed mid-turn is queued, not refused
sp_engine_input answered ui.command_busy for every command while a turn ran, and /compact was one of them. That is the worst moment to refuse it: a turn is what makes you look at the context size. Measured on a session whose operator had typed /compact three times against a 253 KB request and never saw it run - the refusal reads as "already done" and nobody repeats the line once the turn is over. The conversation belongs to the turn worker, so compacting inline would race its appends. Keep the token target instead and apply it in turn_thread after the pending model switch, before the engine goes idle; a foreign turn (the Telegram daemon's) gets the same at sp_engine_turn_end. The new keys ui.compact_nohost and ui.compact_pending are in the .msg sources, which is what catalogs.c is generated from. tests/test_engine.c runs a real turn and types /compact while it runs; without the turn-end call it fails at `applied`.
This commit is contained in:
1 parent
678e2814e6
commit
9e2a2e6156
5 files changed
+207
-23
No files matched your search
@@ -15,6 +15,8 @@ ui.untitled = (ohne Titel)
|
|||||||
ui.session_opened = Sitzung geoeffnet
|
ui.session_opened = Sitzung geoeffnet
|
||||||
ui.session_gone = Sitzung %s ist weg - jetzt auf %s
|
ui.session_gone = Sitzung %s ist weg - jetzt auf %s
|
||||||
ui.compacted = Kontext kompaktiert: %zu->%zu Nachrichten, %s->%s (%s->%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_queued_line = Steuerung eingereiht: %s
|
||||||
ui.steer_failed = Steuerung nicht angenommen (kein Turn laeuft)
|
ui.steer_failed = Steuerung nicht angenommen (kein Turn laeuft)
|
||||||
ui.model_title = Modell
|
ui.model_title = Modell
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ ui.untitled = (untitled)
|
|||||||
ui.session_opened = session opened
|
ui.session_opened = session opened
|
||||||
ui.session_gone = session %s is gone - now on %s
|
ui.session_gone = session %s is gone - now on %s
|
||||||
ui.compacted = context compacted: %zu->%zu msgs, %s->%s (%s->%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_queued_line = steering queued: %s
|
||||||
ui.steer_failed = steering not accepted (no turn is running)
|
ui.steer_failed = steering not accepted (no turn is running)
|
||||||
ui.model_title = model
|
ui.model_title = model
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ ui.untitled = (sem titulo)
|
|||||||
ui.session_opened = sessao aberta
|
ui.session_opened = sessao aberta
|
||||||
ui.session_gone = sessao %s sumiu - agora em %s
|
ui.session_gone = sessao %s sumiu - agora em %s
|
||||||
ui.compacted = contexto compactado: %zu->%zu msgs, %s->%s (%s->%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_queued_line = direcionamento na fila: %s
|
||||||
ui.steer_failed = direcionamento nao aceito (nenhum turno em andamento)
|
ui.steer_failed = direcionamento nao aceito (nenhum turno em andamento)
|
||||||
ui.model_title = modelo
|
ui.model_title = modelo
|
||||||
|
|||||||
+118
-23
@@ -92,6 +92,18 @@ struct sp_engine {
|
|||||||
char pend_model[320];
|
char pend_model[320];
|
||||||
int has_pend_model;
|
int has_pend_model;
|
||||||
pthread_mutex_t model_lock;
|
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
|
* 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
|
* 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 *,
|
static void cmd_model_apply(struct sp_engine *, const char *,
|
||||||
struct sp_buf *, int);
|
struct sp_buf *, int);
|
||||||
static void engine_apply_pending_model(struct sp_engine *);
|
static void engine_apply_pending_model(struct sp_engine *);
|
||||||
|
static void engine_apply_pending_compact(struct sp_engine *);
|
||||||
|
|
||||||
/* --------------------------------------------------------------- events */
|
/* --------------------------------------------------------------- events */
|
||||||
|
|
||||||
@@ -1266,6 +1279,13 @@ turn_thread(void *arg)
|
|||||||
ev_push(e, SP_EV_ENTRY, 0, "splinter", note);
|
ev_push(e, SP_EV_ENTRY, 0, "splinter", note);
|
||||||
}
|
}
|
||||||
engine_apply_pending_model(e);
|
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,
|
ev_push(e, SP_EV_TURN_END, rc,
|
||||||
e->answer.p != NULL ? e->answer.p : "",
|
e->answer.p != NULL ? e->answer.p : "",
|
||||||
e->err.p != NULL ? e->err.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);
|
pthread_mutex_lock(&e->lock);
|
||||||
e->ext_busy = 0;
|
e->ext_busy = 0;
|
||||||
pthread_mutex_unlock(&e->lock);
|
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);
|
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);
|
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 */
|
/* apply the selector a /model left while the turn was running */
|
||||||
static void
|
static void
|
||||||
engine_apply_pending_model(struct sp_engine *e)
|
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) &&
|
if (sp_engine_busy(e) &&
|
||||||
(strcmp(name, "new") == 0 || strcmp(name, "reset") == 0 ||
|
(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, "agents") == 0 || strcmp(name, "connect") == 0 ||
|
||||||
strcmp(name, "self-check") == 0 ||
|
strcmp(name, "self-check") == 0 ||
|
||||||
strcmp(name, "selfcheck") == 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;
|
return SP_INPUT_HANDLED;
|
||||||
}
|
}
|
||||||
if (strcmp(name, "compact") == 0) {
|
if (strcmp(name, "compact") == 0) {
|
||||||
size_t before = sp_conv_bytes(&e->conv);
|
/*
|
||||||
size_t msgs = e->conv.n;
|
* While a turn runs the conversation belongs to the worker, so the
|
||||||
size_t tokens = e->conf != NULL &&
|
* request is remembered instead of applied - and instead of being
|
||||||
e->conf->compact_target > 0 ?
|
* refused, which is what made /compact look broken: the operator
|
||||||
e->conf->compact_target : 8000;
|
* types it while the model is writing, reads "not while a turn
|
||||||
size_t budget = tokens * 4;
|
* runs", and never types it again. The turn end applies it.
|
||||||
int changed;
|
*/
|
||||||
|
if (sp_engine_busy(e)) {
|
||||||
|
size_t tokens = compact_tokens_from(e, args);
|
||||||
|
|
||||||
if (args[0] != '\0') {
|
pthread_mutex_lock(&e->lock);
|
||||||
char *end;
|
e->pend_compact_tokens = tokens;
|
||||||
unsigned long n = strtoul(args, &end, 10);
|
e->has_pend_compact = 1;
|
||||||
|
pthread_mutex_unlock(&e->lock);
|
||||||
if (*end == '\0' && n > 0 && n < 1000000) {
|
sp_buf_printf(out, sp_td("ui.compact_pending",
|
||||||
tokens = (size_t)n;
|
"compaction queued: it runs when the turn ends "
|
||||||
budget = tokens * 4;
|
"(target %zu tokens)\n"), tokens);
|
||||||
}
|
return SP_INPUT_HANDLED;
|
||||||
}
|
}
|
||||||
changed = e->host != NULL ?
|
cmd_compact(e, args, out);
|
||||||
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);
|
|
||||||
return SP_INPUT_HANDLED;
|
return SP_INPUT_HANDLED;
|
||||||
}
|
}
|
||||||
if (strcmp(name, "cost") == 0) {
|
if (strcmp(name, "cost") == 0) {
|
||||||
|
|||||||
@@ -122,6 +122,14 @@ main(void)
|
|||||||
char tmpl[512], script[600], path[700], logpath[760];
|
char tmpl[512], script[600], path[700], logpath[760];
|
||||||
char *dir;
|
char *dir;
|
||||||
const char *tmp = getenv("TMPDIR");
|
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;
|
FILE *f;
|
||||||
char call[256];
|
char call[256];
|
||||||
int kind;
|
int kind;
|
||||||
@@ -148,6 +156,12 @@ main(void)
|
|||||||
/* a second turn whose tool fails: /self-check must name the failure */
|
/* a second turn whose tool fails: /self-check must name the failure */
|
||||||
fprintf(f, "tool read {\"path\":\"/nonexistent-splinter-test\"}\n");
|
fprintf(f, "tool read {\"path\":\"/nonexistent-splinter-test\"}\n");
|
||||||
fprintf(f, "answer done\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);
|
fclose(f);
|
||||||
(void)setenv("HOME", dir, 1);
|
(void)setenv("HOME", dir, 1);
|
||||||
(void)sp_i18n_init("en");
|
(void)sp_i18n_init("en");
|
||||||
@@ -468,6 +482,75 @@ main(void)
|
|||||||
CHECK(out.p != NULL && out.p[0] != '\0');
|
CHECK(out.p != NULL && out.p[0] != '\0');
|
||||||
sp_buf_clear(&out);
|
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 */
|
/* a shell line is an entry event, not a reply */
|
||||||
kind = sp_engine_input(eng, "!echo shell-out", &out);
|
kind = sp_engine_input(eng, "!echo shell-out", &out);
|
||||||
CHECK(kind == SP_INPUT_HANDLED);
|
CHECK(kind == SP_INPUT_HANDLED);
|
||||||
|
|||||||
Reference in new issue
Block a user