env=NAME=VALUE on a delegate line (repeatable) is set in the child right before exec and beats what the parent exported, so two delegates can run the same cli with two accounts. Remotely it goes in front as `env 'NAME=VALUE'`, quoted like the rest. cli_env = NAME=VALUE does the same for the main cli transport. A bad name is an error with the line. Also in this commit, the work in progress from the tree: swarm and provider state, the provider/scheduler/planner/tmux plugins, the ui test, whose tmux session name is now per run so parallel runs do not collide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
3.6 KiB
C
124 lines
3.6 KiB
C
/*
|
|
* splinter -- lcm: lossy compression memory for the conversation.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* The rule this plugin follows: a conversation that does not fit the budget is
|
|
* cut from the front, but never silently. What is dropped is replaced by one
|
|
* system message that says how much went, of which roles, and quotes the head
|
|
* of the oldest dropped turn — so a model (and a human reading the session log)
|
|
* can tell the difference between "the user never said that" and "that was
|
|
* compacted away".
|
|
*
|
|
* Deterministic on purpose: no second model, no summary that costs another
|
|
* call, nothing that makes a run unreproducible. The harder variant (an LLM
|
|
* writes the summary) is a different plugin, not a flag on this one.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_plugin.h"
|
|
|
|
#define KEEP_TAIL 6 /* messages always kept verbatim */
|
|
#define PREVIEW 120 /* chars of the oldest dropped message */
|
|
|
|
static int
|
|
is_role(const struct sp_msg *m, const char *role)
|
|
{
|
|
return m->role != NULL && strcmp(m->role, role) == 0;
|
|
}
|
|
|
|
static int
|
|
lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
|
unsigned turn)
|
|
{
|
|
size_t bytes, i, drop, first, keep_from;
|
|
char preview[PREVIEW + 1];
|
|
char summary[512];
|
|
size_t n_user = 0, n_asst = 0, n_tool = 0;
|
|
const char *oldest;
|
|
|
|
(void)turn;
|
|
(void)h;
|
|
if (conv == NULL || conv->n <= KEEP_TAIL + 1)
|
|
return 0;
|
|
if (budget == 0)
|
|
return 0; /* no byte budget: nothing to do */
|
|
bytes = sp_conv_bytes(conv);
|
|
if (bytes <= budget)
|
|
return 0; /* fits: nothing to do */
|
|
|
|
/* a leading system prompt stays; everything else may go */
|
|
first = is_role(&conv->v[0], "system") ? 1 : 0;
|
|
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
|
|
* orphaned tool message with a 400. Such results go with their call.
|
|
*/
|
|
while (keep_from < conv->n && is_role(&conv->v[keep_from], "tool"))
|
|
keep_from++;
|
|
if (keep_from >= conv->n || keep_from <= first)
|
|
return 0;
|
|
drop = keep_from - first;
|
|
|
|
oldest = conv->v[first].content; /* the oldest one that goes */
|
|
for (i = first; i < keep_from; i++) {
|
|
if (is_role(&conv->v[i], "user"))
|
|
n_user++;
|
|
else if (is_role(&conv->v[i], "assistant"))
|
|
n_asst++;
|
|
else if (is_role(&conv->v[i], "tool"))
|
|
n_tool++;
|
|
}
|
|
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. "
|
|
"Oldest dropped turn began with: \"%s\"",
|
|
drop, n_user, n_asst, n_tool, budget, conv->n - keep_from, preview);
|
|
|
|
for (i = first; i < keep_from; i++) {
|
|
free(conv->v[i].role);
|
|
free(conv->v[i].name);
|
|
free(conv->v[i].call_id);
|
|
free(conv->v[i].content);
|
|
free(conv->v[i].calls);
|
|
}
|
|
memmove(&conv->v[first], &conv->v[keep_from],
|
|
(conv->n - keep_from) * sizeof(*conv->v));
|
|
conv->n = first + (conv->n - keep_from);
|
|
|
|
{
|
|
struct sp_msg m;
|
|
|
|
memset(&m, 0, sizeof(m));
|
|
m.role = "system";
|
|
m.content = summary;
|
|
if (sp_conv_insert(conv, first, &m) != 0)
|
|
return -1;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static const struct sp_compactor lcm = { "lcm", lcm_compact };
|
|
|
|
static int
|
|
lcm_init(struct sp_host *h)
|
|
{
|
|
return sp_host_add_compactor(h, &lcm);
|
|
}
|
|
|
|
static const struct sp_plugin lcm_plugin = {
|
|
SP_PLUGIN_ABI, "lcm", "1.0.0", lcm_init, NULL, NULL
|
|
};
|
|
|
|
const struct sp_plugin *
|
|
sp_plugin_describe(void)
|
|
{
|
|
return &lcm_plugin;
|
|
}
|