- lcm: the sidebar widget and /status segment show the context, the number of
compactions, messages and bytes of the last cut, and how many old tool
outputs were trimmed. The counters are the compactor's own, not an estimate.
- cost: a provider-reported USD cost (usage.cost/cost_usd) is carried through
sse/openai, the agent and the engine. /cost reports it with the cache share
and splits judged from plain turns; the notes say that an upstream judge is
not visible in the provider's usage. The status plugin and junction prefer
the reported number over list-price arithmetic.
- curses: the running turn's seconds moved from the input line to the status
bar. Notes and errors are transcript rows below the cause, never text in the
input line or the status bar; a failed backend request is a status row and is
retried by the agent. The steer prompt is only "steer> " and what was typed.
- model: /model probe works in the chat. The picker lists the configuration at
once, then asks the endpoint the session is on what it serves and adds those
models, so a router's whole menu is selectable. provider/model selectors now
apply the right provider instead of becoming a model name. The choice is
written to <session>.model and restored when that session is reopened; /new
carries the current model into the new session.
- sidebar: plugin titles are bold and underlined (this curses has no italic
attribute), with a rule between the plugin blocks.
- run/cli: a command timeout is an idle window. Every chunk on stdout or
stderr resets it, and the process is asked with waitpid(WNOHANG) whether it
is still there before the window is extended. A long build that keeps
reporting progress is no longer killed for being long; a silent hang is.
- config: ';' separates settings on one line, so
plugin lcm { keep = 6; tool_trim = 8192 } parses as two keys.
- tests: lcm widget/status counters, one-line config blocks, session model
restore and /model probe, cli idle timeout with a talkative and a silent
command, run output reset, and the UI status/retry rows.
make check: all tests passed.
366 lines
10 KiB
C
366 lines
10 KiB
C
/*
|
|
* splinter -- compactor plugin tests.
|
|
*
|
|
* lcm is deterministic: give it a conversation over budget and it must cut the
|
|
* front, keep the tail, and leave a system message that says what went.
|
|
* cps is a pipeline: the first call prepares a plan on its worker, a later call
|
|
* applies it. Both are exercised against the real .so files.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_plugin.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static void
|
|
nap_ms(long ms)
|
|
{
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = ms / 1000;
|
|
ts.tv_nsec = (ms % 1000) * 1000000L;
|
|
nanosleep(&ts, NULL);
|
|
}
|
|
|
|
static void
|
|
fill(struct sp_conv *c, int n)
|
|
{
|
|
int i;
|
|
|
|
sp_conv_init(c);
|
|
sp_conv_push(c, "system", NULL, "you are a harness");
|
|
for (i = 0; i < n; i++) {
|
|
char text[128];
|
|
|
|
snprintf(text, sizeof(text), "message number %d with some padding "
|
|
"so the conversation grows past a small budget", i);
|
|
sp_conv_push(c, (i % 2) ? "assistant" : "user", NULL, text);
|
|
}
|
|
}
|
|
|
|
static int
|
|
echo_tool(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
(void)ctx;
|
|
return sp_buf_puts(out, args);
|
|
}
|
|
|
|
/*
|
|
* Drive the real turn loop with lcm loaded and a byte budget. The mock echoes a
|
|
* fixed payload, so the conversation grows by a known amount per tool round;
|
|
* the only way the turn can finish is that the compactor kept every request
|
|
* under 'budget'. Returns the agent's rc, fills *stats.
|
|
*/
|
|
static int
|
|
run_budget(size_t budget, size_t calls, size_t payload,
|
|
struct sp_agent_stats *stats)
|
|
{
|
|
struct sp_registry r;
|
|
struct sp_tool t;
|
|
struct sp_agent ag;
|
|
struct sp_agent_config cfg;
|
|
struct sp_mock m;
|
|
struct sp_host *host = NULL;
|
|
struct sp_buf answer, err, reply, chunk;
|
|
size_t i;
|
|
int have_mock = 0, rc = -1;
|
|
|
|
memset(&r, 0, sizeof(r));
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "echo";
|
|
t.description = "echo the argument";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = echo_tool;
|
|
(void)sp_registry_add(&r, &t);
|
|
|
|
memset(&ag, 0, sizeof(ag));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
cfg.max_steps = calls + 2;
|
|
cfg.max_bytes = budget;
|
|
cfg.autonomous = 1;
|
|
ag.reg = &r;
|
|
ag.cfg = &cfg;
|
|
|
|
memset(&answer, 0, sizeof(answer));
|
|
memset(&err, 0, sizeof(err));
|
|
memset(&reply, 0, sizeof(reply));
|
|
memset(&chunk, 0, sizeof(chunk));
|
|
if (sp_buf_init(&answer) != 0 || sp_buf_init(&err) != 0 ||
|
|
sp_buf_init(&reply) != 0 || sp_buf_init(&chunk) != 0)
|
|
goto out;
|
|
host = sp_host_new(&r, &ag, NULL);
|
|
if (host == NULL || sp_plugin_load(host, "plugins/lcm/lcm.so", &err) != 0)
|
|
goto out;
|
|
ag.host = host;
|
|
if (sp_mock_init(&m) != 0)
|
|
goto out;
|
|
have_mock = 1;
|
|
for (i = 0; i < payload; i++)
|
|
if (sp_buf_puts(&chunk, "x") != 0)
|
|
goto out;
|
|
for (i = 0; i < calls; i++) {
|
|
sp_buf_clear(&reply);
|
|
if (sp_buf_printf(&reply, "tool echo %s", chunk.p) != 0 ||
|
|
sp_mock_push(&m, 200, reply.p) != 0)
|
|
goto out;
|
|
}
|
|
if (sp_mock_push(&m, 200, "answer done") != 0)
|
|
goto out;
|
|
ag.tr = sp_mock_transport(&m);
|
|
rc = sp_agent_run(&ag, "start", &answer, &err);
|
|
if (stats != NULL)
|
|
*stats = ag.stats;
|
|
if (rc == 0) {
|
|
CHECK(answer.p != NULL && strcmp(answer.p, "done") == 0);
|
|
} else {
|
|
fprintf(stderr, "FAIL budget %zu: rc %d: %s\n", budget, rc,
|
|
err.p != NULL ? err.p : "(no error)");
|
|
failures++;
|
|
}
|
|
|
|
out:
|
|
if (have_mock)
|
|
sp_mock_free(&m);
|
|
sp_host_free(host);
|
|
sp_buf_free(&answer);
|
|
sp_buf_free(&err);
|
|
sp_buf_free(&reply);
|
|
sp_buf_free(&chunk);
|
|
sp_registry_free(&r);
|
|
return rc;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct sp_registry reg;
|
|
struct sp_agent ag;
|
|
struct sp_host *host, *host2;
|
|
struct sp_buf err;
|
|
struct sp_conv conv;
|
|
size_t before, after, compactions, failed;
|
|
unsigned turns;
|
|
|
|
memset(®, 0, sizeof(reg));
|
|
memset(&ag, 0, sizeof(ag));
|
|
if (sp_buf_init(&err) != 0)
|
|
return 1;
|
|
|
|
host = sp_host_new(®, &ag, NULL);
|
|
CHECK(host != NULL);
|
|
|
|
/* ---- lcm ---- */
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_plugin_load(host, "plugins/lcm/lcm.so", &err) == 0);
|
|
|
|
/* under budget: untouched */
|
|
fill(&conv, 4);
|
|
before = sp_conv_bytes(&conv);
|
|
CHECK(sp_host_compact(host, &conv, 1000000) == 0);
|
|
CHECK(sp_conv_bytes(&conv) == before);
|
|
CHECK(conv.n == 5);
|
|
sp_conv_free(&conv);
|
|
|
|
/* over budget: cut from the front, system prompt stays, summary inserted */
|
|
fill(&conv, 20);
|
|
before = sp_conv_bytes(&conv);
|
|
CHECK(sp_host_compact(host, &conv, 400) == 1);
|
|
after = sp_conv_bytes(&conv);
|
|
CHECK(after < before);
|
|
CHECK(strcmp(conv.v[0].role, "system") == 0);
|
|
CHECK(strcmp(conv.v[0].content, "you are a harness") == 0);
|
|
CHECK(strcmp(conv.v[1].role, "system") == 0);
|
|
CHECK(strstr(conv.v[1].content, "[lcm]") != NULL);
|
|
CHECK(strstr(conv.v[1].content, "compacted out") != NULL);
|
|
/* the tail survived verbatim */
|
|
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);
|
|
}
|
|
|
|
/*
|
|
* 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);
|
|
CHECK(host2 != NULL);
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_plugin_load(host2, "plugins/cps/cps.so", &err) == 0);
|
|
|
|
fill(&conv, 20);
|
|
before = sp_conv_bytes(&conv);
|
|
/* first call only hands the index over */
|
|
CHECK(sp_host_compact(host2, &conv, 400) == 0);
|
|
CHECK(sp_conv_bytes(&conv) == before);
|
|
nap_ms(350); /* its worker ticks every 100 ms */
|
|
/* the plan is ready now: the next call applies it */
|
|
{
|
|
int rc = sp_host_compact(host2, &conv, 400);
|
|
|
|
CHECK(rc == 1);
|
|
if (rc == 1) {
|
|
CHECK(sp_conv_bytes(&conv) < before);
|
|
CHECK(strstr(conv.v[0].content, "you are a harness") != NULL);
|
|
CHECK(strstr(conv.v[1].content, "[cps]") != NULL);
|
|
}
|
|
}
|
|
sp_conv_free(&conv);
|
|
|
|
/* ---- stats are counted, not claimed ---- */
|
|
sp_host_compaction_stats(host, &compactions, &failed, &turns);
|
|
CHECK(compactions >= 1);
|
|
CHECK(failed == 0);
|
|
|
|
/*
|
|
* The sidebar shows those numbers, not an estimate: the widget is the
|
|
* compactor's own counter, and the status line carries one segment.
|
|
*/
|
|
nap_ms(2200); /* the widget ticks every two seconds */
|
|
{
|
|
char snap[2048];
|
|
|
|
CHECK(sp_host_widget_snapshot(host, snap, sizeof(snap)) > 0);
|
|
CHECK(strstr(snap, "lcm: ctx ") != NULL);
|
|
CHECK(strstr(snap, "compacted ") != NULL);
|
|
CHECK(strstr(snap, "cut ") != NULL);
|
|
CHECK(strstr(snap, "tools trimmed ") != NULL);
|
|
snap[0] = '\0';
|
|
CHECK(sp_host_status_text(host, snap, sizeof(snap)) > 0);
|
|
CHECK(strstr(snap, "compact ") != NULL);
|
|
}
|
|
|
|
/*
|
|
* ---- lcm never leaves a tool result without its call ----
|
|
* system, user, then [assistant+tool_calls, tool, tool] x 4, then a user:
|
|
* fifteen messages, and the verbatim tail of six would begin with a tool
|
|
* result whose assistant turn is cut away. A provider answers that with
|
|
* a 400.
|
|
*/
|
|
{
|
|
struct sp_conv tc;
|
|
struct sp_msg m;
|
|
char big[300];
|
|
size_t k;
|
|
int orphan = 0;
|
|
|
|
sp_conv_init(&tc);
|
|
memset(big, 'x', sizeof(big) - 1);
|
|
big[sizeof(big) - 1] = '\0';
|
|
sp_conv_push(&tc, "system", NULL, "you are a harness");
|
|
sp_conv_push(&tc, "user", NULL, big);
|
|
for (k = 0; k < 4; k++) {
|
|
memset(&m, 0, sizeof(m));
|
|
m.role = "assistant";
|
|
m.content = "";
|
|
m.calls = "[{\"id\":\"c\",\"type\":\"function\"}]";
|
|
CHECK(sp_conv_add(&tc, &m) == 0);
|
|
sp_conv_push(&tc, "tool", "read", big);
|
|
sp_conv_push(&tc, "tool", "read", big);
|
|
}
|
|
sp_conv_push(&tc, "user", NULL, "and now?");
|
|
CHECK(tc.n == 15);
|
|
CHECK(strcmp(tc.v[tc.n - 6].role, "tool") == 0);
|
|
CHECK(sp_host_compact(host, &tc, 400) == 1);
|
|
for (k = 1; k < tc.n; k++)
|
|
if (strcmp(tc.v[k].role, "tool") == 0 &&
|
|
strcmp(tc.v[k - 1].role, "assistant") != 0 &&
|
|
strcmp(tc.v[k - 1].role, "tool") != 0)
|
|
orphan = 1;
|
|
CHECK(!orphan);
|
|
CHECK(strcmp(tc.v[0].role, "system") == 0);
|
|
CHECK(strstr(tc.v[1].content, "[lcm]") != NULL);
|
|
CHECK(strcmp(tc.v[tc.n - 1].content, "and now?") == 0);
|
|
sp_conv_free(&tc);
|
|
}
|
|
|
|
/* ---- a plugin that is not a compactor cannot register one ---- */
|
|
CHECK(sp_host_add_compactor(host, NULL) == -1);
|
|
|
|
/*
|
|
* ---- the loop compacts at a budget, not only the plugin in isolation ----
|
|
* 8 KiB and 32 KiB (a rough 8k-token window). Each request has to fit
|
|
* after compaction or the turn fails; the counter is the measurement.
|
|
*/
|
|
{
|
|
struct sp_agent_stats st;
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
CHECK(run_budget(8192, 40, 400, &st) == 0);
|
|
CHECK(st.compactions >= 1);
|
|
CHECK(st.steps == 41);
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
CHECK(run_budget(32768, 120, 400, &st) == 0);
|
|
CHECK(st.compactions >= 1);
|
|
CHECK(st.steps == 121);
|
|
}
|
|
|
|
sp_host_free(host2);
|
|
sp_host_free(host);
|
|
sp_buf_free(&err);
|
|
sp_registry_free(®);
|
|
|
|
if (failures == 0)
|
|
printf("test_compactor: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|