lcm: never drop the newest user message

A long tool loop pushed the question out of the six-message tail; the model then
wrote 'The compaction dropped the user's actual question' and recovered it with
recall (session 1794952419). The newest user message is now pinned and put back
right after the summary, so the task survives any compaction. test_compactor
covers it.
This commit is contained in:
lotherk committed 2026-11-17 22:23:05 +00:00
1 parent 82bda09ed1
commit dc9a1c62ec
2 files changed
+68 -4

No files matched your search

+44 -4
View File
@@ -114,7 +114,9 @@ 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;
size_t pin_at = 0;
char *pin_content = NULL, *pin_name = NULL;
int trimmed, pinned = 0;
const char *oldest;
(void)turn;
@@ -149,6 +151,24 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
if (bytes <= budget)
return 1;
}
/*
* The newest user message is the task being answered. A long tool loop
* can push it out of the verbatim tail; dropping it made the model ask
* "what was the question?" and dig it out with recall. Pin it and put
* it back right after the summary.
*/
for (i = conv->n; i > first; i--)
if (is_role(&conv->v[i - 1], "user")) {
pin_at = i - 1;
break;
}
if (pin_at < keep_from && keep_from > first) {
pin_content = strdup(conv->v[pin_at].content != NULL ?
conv->v[pin_at].content : "");
if (conv->v[pin_at].name != NULL)
pin_name = strdup(conv->v[pin_at].name);
pinned = pin_content != NULL;
}
drop = keep_from - first;
oldest = conv->v[first].content; /* the oldest one that goes */
@@ -165,8 +185,11 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
"[lcm] %zu messages compacted out (user %zu, assistant %zu, tool %zu) "
"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,
trimmed > 0 ? " Older tool output was cut too (recall still has it)." : "",
drop - (pinned ? 1 : 0), n_user - (pinned ? 1 : 0), n_asst, n_tool,
budget, conv->n - keep_from,
pinned ? " The current question was kept." :
(trimmed > 0 ? " Older tool output was cut too (recall still has it)." :
""),
preview);
for (i = first; i < keep_from; i++) {
@@ -187,9 +210,26 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
m.role = "system";
m.content = summary;
if (sp_conv_insert(conv, first, &m) != 0)
return -1;
goto fail;
}
if (pinned) {
struct sp_msg m;
memset(&m, 0, sizeof(m));
m.role = "user";
m.name = pin_name;
m.content = pin_content;
if (sp_conv_insert(conv, first + 1, &m) != 0)
goto fail;
}
free(pin_content);
free(pin_name);
return 1;
fail:
free(pin_content);
free(pin_name);
return -1;
}
/* case-blind substring for the recall tool */
+24
View File
@@ -215,6 +215,30 @@ main(void)
sp_conv_free(&conv);
}
/*
* The newest user message is the task: a long tool loop must not push it
* out of the tail. It comes back right after the summary.
*/
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "system", NULL, "you are a harness") == 0);
CHECK(sp_conv_push(&conv, "user", NULL,
"the question that matters") == 0);
{
size_t i;
for (i = 0; i < 12; i++)
CHECK(sp_conv_push(&conv, "assistant", NULL,
"message number x") == 0);
}
CHECK(sp_host_compact(host, &conv, 300) == 1);
CHECK(strcmp(conv.v[0].role, "system") == 0);
CHECK(strcmp(conv.v[1].role, "system") == 0);
CHECK(strstr(conv.v[1].content, "[lcm]") != NULL);
CHECK(strstr(conv.v[1].content, "current question was kept") != NULL);
CHECK(strcmp(conv.v[2].role, "user") == 0);
CHECK(strstr(conv.v[2].content, "the question that matters") != NULL);
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(&reg, &ag, NULL);