diff --git a/plugins/lcm/lcm.c b/plugins/lcm/lcm.c index 9b7e604..9515cc2 100644 --- a/plugins/lcm/lcm.c +++ b/plugins/lcm/lcm.c @@ -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 */ diff --git a/tests/test_compactor.c b/tests/test_compactor.c index 338df0a..fa669f3 100644 --- a/tests/test_compactor.c +++ b/tests/test_compactor.c @@ -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(®, &ag, NULL);