lcm: cut old tool outputs before dropping messages
Tool observations are the bulk of a long conversation. Before lcm drops any
message it now cuts every old tool output to a head and a tail with a marker
quoting its first line; the middle stays in the session log, where the recall
tool finds it. Only when that is still over budget are messages dropped, so a
conversation often keeps all its turns and only loses the middle of old
outputs.
- plugin lcm { keep = 6; tool_trim = 8192 } sets the verbatim tail and the cap
per old observation (0 turns the trim off).
- the compacted summary says when tool output was cut too.
- test_compactor: a 20 KB old observation is trimmed, nothing is dropped, and
the marker points at recall.
- docs/CONFIG and docs/PLUGINS updated; the user config uses 6/8192.
make check: all tests passed.
This commit is contained in:
1 parent
7e74e2a32a
commit
82bda09ed1
4 files changed
+137
-10
No files matched your search
+2
-1
@@ -336,7 +336,8 @@ apart from `path` and `enabled` is that plugin's own configuration:
|
||||
|
||||
```
|
||||
plugin lcm {
|
||||
keep = 40
|
||||
keep = 6 # messages kept verbatim
|
||||
tool_trim = 8192 # older tool outputs are cut to this
|
||||
}
|
||||
plugin junction { } # url/header from the mcp junction server
|
||||
plugin mine {
|
||||
|
||||
+8
-4
@@ -156,10 +156,14 @@ not take the conversation with it.
|
||||
Two come with the tree.
|
||||
|
||||
**lcm** — deterministic, lossy, honest. If the conversation does not fit the
|
||||
budget, it keeps the leading system message and the last six messages, and puts
|
||||
one system message in between that says how many messages went, of which roles,
|
||||
and quotes the head of the oldest dropped turn. No second model is called, so a
|
||||
run stays reproducible and costs nothing extra.
|
||||
budget, it first cuts **old tool outputs** to a head and a tail with a marker
|
||||
in between (their middle stays in the session log, where the `recall` tool
|
||||
finds it); only when that is not enough does it keep the leading system message
|
||||
and the last six messages and put one system message in between that says how
|
||||
many messages went, of which roles, and quotes the head of the oldest dropped
|
||||
turn. No second model is called, so a run stays reproducible and costs nothing
|
||||
extra. `plugin lcm { keep = 6; tool_trim = 8192 }` sets the verbatim tail and
|
||||
the byte cap per old observation; `tool_trim = 0` turns the trim off.
|
||||
|
||||
**cps** — continuous parallel compression. The expensive part (deciding what
|
||||
would go) is computed on the widget's worker thread from an *index* of the
|
||||
|
||||
+98
-5
@@ -24,8 +24,12 @@
|
||||
#include "splinter_plugin.h"
|
||||
#include "mcp.h"
|
||||
|
||||
#define KEEP_TAIL 6 /* messages always kept verbatim */
|
||||
#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 */
|
||||
|
||||
static size_t keep_tail = KEEP_TAIL;
|
||||
static size_t tool_trim = TOOL_TRIM;
|
||||
|
||||
static int
|
||||
is_role(const struct sp_msg *m, const char *role)
|
||||
@@ -33,6 +37,75 @@ is_role(const struct sp_msg *m, const char *role)
|
||||
return m->role != NULL && strcmp(m->role, role) == 0;
|
||||
}
|
||||
|
||||
/* the first line of a text, at most n bytes, for the marker */
|
||||
static void
|
||||
first_line(const char *s, char *out, size_t cap, size_t n)
|
||||
{
|
||||
size_t i = 0;
|
||||
|
||||
while (s[i] != '\0' && i < n && s[i] != '\n' && i + 1 < cap) {
|
||||
unsigned char c = (unsigned char)s[i];
|
||||
|
||||
out[i] = (c >= 32 && c != 127) ? (char)c : ' ';
|
||||
i++;
|
||||
}
|
||||
out[i] = '\0';
|
||||
}
|
||||
|
||||
/*
|
||||
* Tool outputs are the bulk of a long conversation. Before messages are
|
||||
* dropped, every old observation is cut to a head and a tail with a marker in
|
||||
* between; the middle is still in the session log, where the recall tool finds
|
||||
* it. The recent tail (the messages we keep verbatim) is never touched, so the
|
||||
* model always has the output it just asked for.
|
||||
*/
|
||||
static int
|
||||
trim_tools(struct sp_conv *conv, size_t first, size_t end, size_t cap)
|
||||
{
|
||||
size_t i;
|
||||
int n = 0;
|
||||
|
||||
if (cap == 0)
|
||||
return 0;
|
||||
for (i = first; i < end; i++) {
|
||||
struct sp_msg *m = &conv->v[i];
|
||||
size_t len, head, tail, off, room;
|
||||
char *hint = "", *nb;
|
||||
|
||||
if (!is_role(m, "tool") || m->content == NULL)
|
||||
continue;
|
||||
len = strlen(m->content);
|
||||
if (len <= cap)
|
||||
continue;
|
||||
head = cap / 2;
|
||||
tail = cap / 4;
|
||||
if (head + tail >= len)
|
||||
continue;
|
||||
{
|
||||
static char hbuf[72];
|
||||
|
||||
first_line(m->content, hbuf, sizeof(hbuf), 60);
|
||||
hint = hbuf;
|
||||
}
|
||||
nb = malloc(head + tail + 512);
|
||||
if (nb == NULL)
|
||||
continue;
|
||||
memcpy(nb, m->content, head);
|
||||
off = head;
|
||||
room = head + tail + 512 - off;
|
||||
off += (size_t)snprintf(nb + off, room,
|
||||
"\n[lcm] %zu bytes of tool output cut; recall \"%s\"\n",
|
||||
len - head - tail, hint);
|
||||
memcpy(nb + off, m->content + len - tail, tail);
|
||||
off += tail;
|
||||
nb[off] = '\0';
|
||||
free(m->content);
|
||||
m->content = nb;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static int
|
||||
lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
||||
unsigned turn)
|
||||
@@ -41,11 +114,12 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
||||
char preview[PREVIEW + 1];
|
||||
char summary[512];
|
||||
size_t n_user = 0, n_asst = 0, n_tool = 0;
|
||||
int trimmed;
|
||||
const char *oldest;
|
||||
|
||||
(void)turn;
|
||||
(void)h;
|
||||
if (conv == NULL || conv->n <= KEEP_TAIL + 1)
|
||||
if (conv == NULL || conv->n <= keep_tail + 1)
|
||||
return 0;
|
||||
if (budget == 0)
|
||||
return 0; /* no byte budget: nothing to do */
|
||||
@@ -55,7 +129,7 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
||||
|
||||
/* a leading system prompt stays; everything else may go */
|
||||
first = is_role(&conv->v[0], "system") ? 1 : 0;
|
||||
keep_from = conv->n - KEEP_TAIL;
|
||||
keep_from = conv->n - keep_tail;
|
||||
/*
|
||||
* The tail must not start with a tool result: its assistant turn, the
|
||||
* one carrying the tool_call, would be gone, and a provider answers an
|
||||
@@ -65,6 +139,16 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
||||
keep_from++;
|
||||
if (keep_from >= conv->n || keep_from <= first)
|
||||
return 0;
|
||||
/*
|
||||
* First the cheap cut: old tool outputs. If that is enough the messages
|
||||
* stay, and only the observations shrink.
|
||||
*/
|
||||
trimmed = trim_tools(conv, first, keep_from, tool_trim);
|
||||
if (trimmed > 0) {
|
||||
bytes = sp_conv_bytes(conv);
|
||||
if (bytes <= budget)
|
||||
return 1;
|
||||
}
|
||||
drop = keep_from - first;
|
||||
|
||||
oldest = conv->v[first].content; /* the oldest one that goes */
|
||||
@@ -79,9 +163,11 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
||||
snprintf(preview, sizeof(preview), "%s", oldest != NULL ? oldest : "");
|
||||
snprintf(summary, sizeof(summary),
|
||||
"[lcm] %zu messages compacted out (user %zu, assistant %zu, tool %zu) "
|
||||
"to fit %zu bytes; the verbatim tail of %zu messages follows. "
|
||||
"to fit %zu bytes; the verbatim tail of %zu messages follows.%s "
|
||||
"Oldest dropped turn began with: \"%s\"",
|
||||
drop, n_user, n_asst, n_tool, budget, conv->n - keep_from, preview);
|
||||
drop, n_user, n_asst, n_tool, budget, conv->n - keep_from,
|
||||
trimmed > 0 ? " Older tool output was cut too (recall still has it)." : "",
|
||||
preview);
|
||||
|
||||
for (i = first; i < keep_from; i++) {
|
||||
free(conv->v[i].role);
|
||||
@@ -197,7 +283,14 @@ static int
|
||||
lcm_init(struct sp_host *h)
|
||||
{
|
||||
static struct sp_tool rt = recall_tool;
|
||||
const char *v;
|
||||
|
||||
v = sp_host_plugin_config(h, "lcm", "keep");
|
||||
if (v != NULL && atoi(v) > 0 && atoi(v) < 1000)
|
||||
keep_tail = (size_t)atoi(v);
|
||||
v = sp_host_plugin_config(h, "lcm", "tool_trim");
|
||||
if (v != NULL && atoi(v) >= 0)
|
||||
tool_trim = (size_t)atoi(v);
|
||||
rt.ctx = h;
|
||||
if (sp_host_add_compactor(h, &lcm) != 0)
|
||||
return -1;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
* applies it. Both are exercised against the real .so files.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
@@ -186,6 +187,34 @@ main(void)
|
||||
CHECK(strstr(conv.v[conv.n - 1].content, "message number") != NULL);
|
||||
sp_conv_free(&conv);
|
||||
|
||||
/*
|
||||
* An old tool output is cut before any message is dropped: head and
|
||||
* tail survive, the middle is still in the session log for recall.
|
||||
*/
|
||||
{
|
||||
char *big = malloc(20001);
|
||||
size_t i;
|
||||
|
||||
CHECK(big != NULL);
|
||||
memset(big, 'x', 20000);
|
||||
big[20000] = '\0';
|
||||
sp_conv_init(&conv);
|
||||
CHECK(sp_conv_push(&conv, "system", NULL, "you are a harness") == 0);
|
||||
CHECK(sp_conv_push(&conv, "user", NULL, "run it") == 0);
|
||||
CHECK(sp_conv_push(&conv, "tool", NULL, big) == 0);
|
||||
for (i = 0; i < 8; i++)
|
||||
CHECK(sp_conv_push(&conv, i % 2 ? "assistant" : "user",
|
||||
NULL, "message number x") == 0);
|
||||
before = sp_conv_bytes(&conv);
|
||||
CHECK(sp_host_compact(host, &conv, before - 10000) == 1);
|
||||
CHECK(conv.n == 11); /* nothing dropped */
|
||||
CHECK(strstr(conv.v[2].content, "[lcm]") != NULL);
|
||||
CHECK(strstr(conv.v[2].content, "recall") != NULL);
|
||||
CHECK(sp_conv_bytes(&conv) < before);
|
||||
free(big);
|
||||
sp_conv_free(&conv);
|
||||
}
|
||||
|
||||
/* ---- 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);
|
||||
|
||||
Reference in new issue
Block a user