diff --git a/plugins/lcm/lcm.c b/plugins/lcm/lcm.c index 517c8f5..9e0a84e 100644 --- a/plugins/lcm/lcm.c +++ b/plugins/lcm/lcm.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "splinter.h" #include "splinter_plugin.h" @@ -28,6 +29,8 @@ #define KEEP_TAIL 6 /* default: messages always kept verbatim */ #define PREVIEW 120 /* chars of the oldest dropped message */ #define TOOL_TRIM 8192 /* default: older tool outputs are cut to this */ +#define RECALL_HIT 1600 /* bytes shown of one matching line */ +#define RECALL_TOTAL 12000 /* bytes of matches, so recall stays cheap */ static size_t keep_tail = KEEP_TAIL; static size_t tool_trim = TOOL_TRIM; @@ -361,7 +364,10 @@ has_ci(const char *hay, const char *needle) static int lcm_recall(const char *args, struct sp_buf *out, void *ctx) { - char query[256] = "", path[800], line[8192]; + char query[256] = "", path[800]; + char *line = NULL; + size_t linecap = 0; + ssize_t len; struct sp_host *h = ctx; const char *dir = sp_host_session_dir(h); const char *id = sp_host_session_id(h); @@ -390,15 +396,36 @@ lcm_recall(const char *args, struct sp_buf *out, void *ctx) f = fopen(path, "r"); if (f == NULL) return sp_buf_puts(out, "error: cannot read the session log"); - while (n < limit && fgets(line, sizeof(line), f) != NULL) { - char *text; - + /* + * getline, not fgets: one session line is one JSON object, and an + * observation can be tens of KiB. An 8 KiB buffer cut such a line into + * pieces, so a match in the middle came back without its context and a + * long hit was printed in full. A hit now shows head and tail and says + * how much was cut; the whole answer is bounded. + */ + while (n < limit && (len = getline(&line, &linecap, f)) != -1) { if (!has_ci(line, query)) continue; n++; - text = line; - sp_buf_printf(out, "%s", text); + if ((size_t)len > RECALL_HIT) { + size_t head = RECALL_HIT - 400, tail = 400; + + sp_buf_printf(out, "%.*s\n[lcm] %zd bytes cut (recall)\n", + (int)head, line, + len - (ssize_t)(head + tail)); + sp_buf_puts(out, line + len - (ssize_t)tail); + if (line[len - 1] != '\n') + sp_buf_puts(out, "\n"); + } else { + sp_buf_puts(out, line); + } + if (out->len >= RECALL_TOTAL) { + sp_buf_puts(out, "... more matches not shown (recall " + "limit)\n"); + break; + } } + free(line); fclose(f); if (n == 0) sp_buf_printf(out, "no line in the session log contains '%s'", diff --git a/share/locale/de.msg b/share/locale/de.msg index 4ea4c26..dee0969 100644 --- a/share/locale/de.msg +++ b/share/locale/de.msg @@ -15,7 +15,6 @@ 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.steering_queued = Steuerung eingereiht ui.steer_queued_line = Steuerung eingereiht: %s ui.steer_failed = Steuerung nicht angenommen (kein Turn laeuft) ui.model_title = Modell @@ -47,7 +46,6 @@ widgets.none = kein Plugin hat eine Widget-Zeile veröffentlicht err.oom = kein Speicher err.theme = Farbschema -err.session = Sitzung err.plugin_failed = Plugin fehlgeschlagen role.tool = werkzeug @@ -129,16 +127,8 @@ ack.yes = Ja ack.no = Nein tg.cmd.interview = Profil im Chat anlegen -tg.interview.start = Erstes Gespraech fuer '%s'. Ich stelle %zu Fragen; /skip ueberspringt eine, /cancel bricht ab. -tg.interview.question = Frage %zu/%zu: %s -tg.interview.how = Antworte einfach in den Chat. -tg.interview.saved = Profil geschrieben: %s -tg.interview.cancel = Interview abgebrochen. -tg.interview.busy = Das Interview laeuft; antworte, /skip oder /cancel. # interview questions (terminal and Telegram) -interview.intro = Erstes Gespraech fuer '%s'. Enter ueberspringt eine Frage, Ctrl-D bricht ab. -interview.header = _Geschrieben von `splinter --interview`. Von Hand aenderbar; splinter liest sie beim Start._ interview.q.language = In welcher Sprache soll ich mit dir reden? interview.h.language = z.B. deutsch, english, portugues interview.q.about = Wer bist du und woran arbeitest du gerade? @@ -151,10 +141,6 @@ interview.q.never = Was soll ich NIE tun? interview.h.never = z.B. keine Commits ohne Auftrag, nichts in /tmp interview.q.paths = Welche Pfade sind heilig oder gefaehrlich? interview.h.paths = z.B. /home/carol nie anfassen -interview.skipped = (nicht beantwortet) -interview.written = Geschrieben: %s -interview.error.nodir = interview: kein Sitzungsverzeichnis -interview.error.write = interview: kann %s nicht schreiben # /model und /connect (Gateway) session.new = Neue Konversation: %s session.failed = Konnte keine neue Sitzung anlegen: %s @@ -173,7 +159,6 @@ tg.cmd.connect = einen Provider hinzufuegen model.choose = Waehle ein Modell. Die Liste kommt aus der Konfiguration; die Wahl gilt fuer diese Sitzung. model.none = Kein Modell konfiguriert. Mit /connect kommt eins dazu. model.unknown = Kein solches Modell: %s -model.pool = Pool %s: %s (Anbieter %s, diese Sitzung). model.pools = Pools: model.pending = Modellwechsel auf %s nach der laufenden Anfrage ui.cancelling = breche ab ... (Ctrl-C nochmal beendet) @@ -181,7 +166,6 @@ model.failed = Umschalten fehlgeschlagen: %s model.switched = Modell: %s (Provider %s, diese Sitzung). model.no_key = , kein %s gesetzt -model.saved = Modell: %s (Provider %s). Fuer den naechsten Start gespeichert. model.save_failed = Modell: %s (Provider %s). Nicht gespeichert: %s model.probe = Endpoints nach Modellen fragen model.probe_start = Frage %zu Provider nach ihren Modellen ... @@ -235,8 +219,6 @@ usage.calls = %lu Aufrufe usage.room = , Platz in %s usage.of_calls = (%lu/%lu Aufrufe) usage.of_tokens = (%lu/%lu Tokens) -plan.widget = %d offen, %d aktiv, %d erledigt -plan.widget_active = #%d aktiv - %d offen, %d aktiv, %d erledigt ui.thinking = denkt status.title = Status (%s) status.session = Sitzung diff --git a/share/locale/en.msg b/share/locale/en.msg index 9558588..676d616 100644 --- a/share/locale/en.msg +++ b/share/locale/en.msg @@ -14,7 +14,6 @@ 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.steering_queued = steering queued ui.steer_queued_line = steering queued: %s ui.steer_failed = steering not accepted (no turn is running) ui.model_title = model @@ -46,7 +45,6 @@ widgets.none = no plugin published a widget line err.oom = out of memory err.theme = theme -err.session = session err.plugin_failed = plugin failed role.tool = tool @@ -128,16 +126,8 @@ ack.yes = Yes ack.no = No tg.cmd.interview = create the profile in the chat -tg.interview.start = First conversation for '%s'. I ask %zu questions; /skip skips one, /cancel stops. -tg.interview.question = Question %zu/%zu: %s -tg.interview.how = Just answer in the chat. -tg.interview.saved = profile written: %s -tg.interview.cancel = interview cancelled. -tg.interview.busy = The interview is running; answer, /skip or /cancel. # interview questions (terminal and Telegram) -interview.intro = First conversation for '%s'. Enter skips a question, Ctrl-D aborts. -interview.header = _Written by `splinter --interview`. Edit it by hand whenever you like; splinter reads it at start._ interview.q.language = Which language should I speak with you in? interview.h.language = e.g. deutsch, english, portugues interview.q.about = Who are you and what are you working on right now? @@ -150,10 +140,6 @@ interview.q.never = What should I NEVER do? interview.h.never = e.g. no commits without a request, nothing in /tmp interview.q.paths = Which paths are sacred or dangerous? interview.h.paths = e.g. never touch /home/carol -interview.skipped = (not answered) -interview.written = Written: %s -interview.error.nodir = interview: no session directory -interview.error.write = interview: cannot write %s # /model and /connect (gateway) session.new = New conversation: %s session.failed = Could not start a new session: %s @@ -172,7 +158,6 @@ tg.cmd.connect = add a provider model.choose = Choose a model. The list is the configuration; the choice lasts for this session. model.none = No model is configured. Add one with /connect. model.unknown = No such model: %s -model.pool = Pool %s: using %s (provider %s, this session). model.pools = pools: model.pending = model switch to %s after the current request ui.cancelling = cancelling ... (Ctrl-C again quits) @@ -180,7 +165,6 @@ model.failed = Could not switch: %s model.switched = Model: %s (provider %s, this session). model.no_key = , no %s set -model.saved = Model: %s (provider %s). Saved for the next start. model.save_failed = Model: %s (provider %s). Not saved: %s model.probe = Ask the endpoints for their models model.probe_start = Asking %zu provider(s) for their models... @@ -234,8 +218,6 @@ usage.calls = %lu calls usage.room = , room in %s usage.of_calls = (%lu/%lu calls) usage.of_tokens = (%lu/%lu tokens) -plan.widget = todo %d, doing %d, done %d -plan.widget_active = #%d active - todo %d, doing %d, done %d ui.thinking = thinking status.title = status (%s) status.session = session diff --git a/share/locale/pt_BR.msg b/share/locale/pt_BR.msg index d12b196..739e7a1 100644 --- a/share/locale/pt_BR.msg +++ b/share/locale/pt_BR.msg @@ -15,7 +15,6 @@ 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.steering_queued = direcionamento na fila ui.steer_queued_line = direcionamento na fila: %s ui.steer_failed = direcionamento nao aceito (nenhum turno em andamento) ui.model_title = modelo @@ -61,7 +60,6 @@ widgets.none = nenhum plugin publicou uma linha de widget err.oom = sem memoria err.theme = tema -err.session = sessao err.plugin_failed = plugin falhou err.thread = nao foi possivel iniciar a thread do turno err.turn_failed = turno falhou @@ -132,16 +130,8 @@ ack.yes = Sim ack.no = Nao tg.cmd.interview = criar o perfil no chat -tg.interview.start = Primeira conversa para '%s'. Sao %zu perguntas; /skip pula uma, /cancel cancela. -tg.interview.question = Pergunta %zu/%zu: %s -tg.interview.how = Responda no chat. -tg.interview.saved = perfil escrito: %s -tg.interview.cancel = entrevista cancelada. -tg.interview.busy = A entrevista esta em andamento; responda, /skip ou /cancel. # interview questions (terminal and Telegram) -interview.intro = Primeira conversa para '%s'. Enter pula uma pergunta, Ctrl-D cancela. -interview.header = _Escrito por `splinter --interview`. Edite a mao quando quiser; splinter le no inicio._ interview.q.language = Em que lingua devo falar com voce? interview.h.language = ex.: deutsch, english, portugues interview.q.about = Quem e voce e no que esta trabalhando agora? @@ -154,10 +144,6 @@ interview.q.never = O que eu NUNCA devo fazer? interview.h.never = ex.: nenhum commit sem pedido, nada em /tmp interview.q.paths = Quais caminhos sao sagrados ou perigosos? interview.h.paths = ex.: nunca tocar em /home/carol -interview.skipped = (nao respondido) -interview.written = Escrito: %s -interview.error.nodir = interview: sem diretorio de sessao -interview.error.write = interview: nao consigo escrever %s # /model e /connect (gateway) session.new = Nova conversa: %s session.failed = Nao consegui abrir uma sessao nova: %s @@ -176,7 +162,6 @@ tg.cmd.connect = adicionar um provedor model.choose = Escolha um modelo. A lista e a configuracao; a escolha vale para esta sessao. model.none = Nenhum modelo configurado. Adicione um com /connect. model.unknown = Modelo inexistente: %s -model.pool = Pool %s: usando %s (provedor %s, esta sessao). model.pools = Pools: model.pending = troca de modelo para %s depois da requisicao atual ui.cancelling = cancelando ... (Ctrl-C de novo encerra) @@ -184,7 +169,6 @@ model.failed = Nao consegui trocar: %s model.switched = Modelo: %s (provedor %s, esta sessao). model.no_key = , %s nao definida -model.saved = Modelo: %s (provedor %s). Salvo para a proxima sessao. model.save_failed = Modelo: %s (provedor %s). Nao salvo: %s model.probe = Perguntar aos endpoints quais modelos servem model.probe_start = Perguntando a %zu provedor(es) pelos modelos... @@ -238,8 +222,6 @@ usage.calls = %lu chamadas usage.room = , vaga em %s usage.of_calls = (%lu/%lu chamadas) usage.of_tokens = (%lu/%lu tokens) -plan.widget = %d a fazer, %d em curso, %d feitas -plan.widget_active = #%d ativa - %d a fazer, %d em curso, %d feitas ui.thinking = pensando status.title = status (%s) status.session = sessao diff --git a/tests/test_compactor.c b/tests/test_compactor.c index 7710acc..87e505a 100644 --- a/tests/test_compactor.c +++ b/tests/test_compactor.c @@ -274,6 +274,65 @@ main(void) CHECK(strstr(conv.v[2].content, "the question that matters") != NULL); sp_conv_free(&conv); + /* + * recall reads a whole session line. One line is one JSON object and an + * observation can be tens of KiB; with an 8 KiB fgets buffer a match in + * the middle came back without its context, and a long hit was printed + * in full. It now shows head and tail, says what was cut, and stays + * bounded. + */ + { + struct sp_registry r2; + struct sp_session_info sess; + struct sp_host *h2; + const struct sp_tool *rt; + struct sp_buf out; + char tmp[] = "/tmp/splinter-recall-XXXXXX"; + char path[700]; + char *big; + FILE *f; + + memset(&r2, 0, sizeof(r2)); + memset(&sess, 0, sizeof(sess)); + CHECK(mkdtemp(tmp) != NULL); + snprintf(sess.dir, sizeof(sess.dir), "%s", tmp); + snprintf(sess.id, sizeof(sess.id), "sess-recall"); + snprintf(path, sizeof(path), "%s/%s.jsonl", sess.dir, sess.id); + big = malloc(20001); + CHECK(big != NULL); + f = fopen(path, "w"); + CHECK(f != NULL); + if (big != NULL && f != NULL) { + memset(big, 'x', 20000); + big[20000] = '\0'; + memcpy(big, "PREFIX ", 7); + memcpy(big + 100, "NEEDLE-IN-A-LONG-LINE", 21); + fprintf(f, "{\"role\":\"observation\",\"text\":\"%s\"}\n", + big); + fclose(f); + } + free(big); + h2 = sp_host_new(&r2, &ag, &sess); + CHECK(h2 != NULL); + sp_buf_clear(&err); + CHECK(sp_plugin_load(h2, "plugins/lcm/lcm.so", &err) == 0); + rt = sp_registry_find(&r2, "recall"); + CHECK(rt != NULL); + CHECK(sp_buf_init(&out) == 0); + if (rt != NULL) + CHECK(rt->run("{\"query\":\"NEEDLE-IN-A-LONG-LINE\"}", + &out, rt->ctx) == 0); + CHECK(out.p != NULL && + strstr(out.p, "NEEDLE-IN-A-LONG-LINE") != NULL); + CHECK(out.p != NULL && strstr(out.p, "bytes cut") != NULL); + CHECK(out.len > 0 && out.len < 3000); + sp_buf_free(&out); + sp_host_free(h2); + sp_registry_free(&r2); + snprintf(path, sizeof(path), "rm -rf '%s'", tmp); + (void)system(path); + } + /* ---- cps: prepare on the worker, apply on a later call ---- */ /* its own host: loaded next to lcm, lcm would do the work first */ host2 = sp_host_new(®, &ag, NULL);