The gateway entry point accepted an empty token and then polled with it; the guard belongs there so a daemon that cannot listen says so instead of looking alive. Honest limit of this change: it is NOT the explanation for the running daemon. That one had a token (resolved from the token file) and failed with 'curl: (35) Recv failure: Connection reset by peer' thousands of times, and with the env variable removed it now fails with 'curl failed (-1)' and stops - a different error, not the missing token. The guard only catches the case where neither the environment nor the file provide one. What this commit does prove: with a config that has endpoint, model and telegram_allow, the daemon starts, reports 'gateway listening (allow: 8300935)', and stops on the failing poll instead of looping. What it does not prove: that a valid token makes it work - that needs a real message.
184 lines
5.1 KiB
C
184 lines
5.1 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 <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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
/* ---- 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);
|
|
|
|
/*
|
|
* ---- 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);
|
|
|
|
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;
|
|
}
|