Files
splinter/tests/test_usage.c
T
lotherk a9f175c25b backend/frontend: an engine every front end can talk to
The harness was two halves that could not see each other: main.c built the
agent, ui.c ran it -- with the curses thread model, the approval condition
variable, the slash commands and the provider switching inside the drawing
code. A second front end had to copy all of it.

Now:

  src/backend/   engine.c owns the session, the turn thread, the event queue
                 and every command; agent, transports, tools, session, config,
                 providers, plugins and the protocol servers move here.
  src/frontend/  curses.c draws and reads keys through ui_backend.h;
                 ui_local.c is the engine in this process, ui_remote.c is a
                 served engine behind a socket. protocol.c serves the engine
                 as JSON lines (protocol 2: history, approvals, steering);
                 client.c is splinterctl, the reference client.

splinter --serve / --attach: the same screen runs as a client of a served
engine, so the socket carries everything the front end needs. docs/PROTOCOL.md
and docs/FRONTENDS.md describe the contract; docs/proto-example.py is a
60-line client. tests/test_engine, test_protocol and test_attach cover the
event order, the socket and the attach mode.

Review fixes in the same tree: dynamic line buffers (9 KB prompts and 8 KB
events were truncated), a turn event no longer ends the busy window (steer
leftovers), the screen finalises every turn, /new keeps the profile, reopens
the log and gets unique ids, the profile is remembered in <session>.agent so
--resume comes back in it, agent statistics are a locked snapshot (no data
race), !shell keeps 64 KiB with a marker, the socket probes for a live server,
compares its inode and sets CLOEXEC/SNDTIMEO, exit 2 means configuration,
BSD peer checks use getpeereid, and the i18n checker scans the new
subdirectories again.

Includes the parallel session's SSE fix: the parser reports what a line
produced, so a tool name no longer lands in the answer ("readread").
2026-11-16 09:06:51 +00:00

122 lines
3.3 KiB
C

/*
* splinter -- usage accounting: windows, limits, the log, the fraction.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "splinter.h"
#include "config.h"
#include "usage.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
int
main(void)
{
char dir[] = "/var/tmp/splinter-usage-XXXXXX";
char path[256];
unsigned long n = 0;
long w = 0, now = (long)time(NULL);
struct sp_usage u;
FILE *f;
int i;
CHECK(sp_usage_window("5h") == 18000);
CHECK(sp_usage_window("7d") == 604800);
CHECK(sp_usage_window("2w") == 1209600);
CHECK(sp_usage_window("90") == 90);
CHECK(sp_usage_window("5x") == 0);
CHECK(sp_usage_window("") == 0);
CHECK(sp_usage_limit_parse("45/5h", &n, &w) == 1 && n == 45 && w == 18000);
CHECK(sp_usage_limit_parse("45", &n, &w) == 0);
CHECK(sp_usage_limit_parse("0/5h", &n, &w) == 0);
CHECK(sp_usage_limit_parse("5/zz", &n, &w) == 0);
CHECK(mkdtemp(dir) != NULL);
setenv("SPLINTER_USAGE_DIR", dir, 1);
setenv("SPLINTER_COOLDOWN_DIR", dir, 1);
/* nothing recorded: zero used, a limit gives 0 %, none gives -1 */
sp_usage_get("a", 10, 3600, 0, 0, &u);
CHECK(u.req_used == 0 && u.frac == 0.0);
sp_usage_get("a", 0, 0, 0, 0, &u);
CHECK(u.frac < 0);
for (i = 0; i < 4; i++)
sp_usage_record("a", 100, 50);
sp_usage_get("a", 10, 3600, 0, 0, &u);
CHECK(u.req_used == 4);
CHECK(u.frac > 0.39 && u.frac < 0.41);
/*
* `now` was taken before the records. The assertion is "one window
* from now", not a stopwatch: on a loaded machine a few seconds pass
* between the two, and a one-second upper bound failed the suite.
*/
CHECK(u.reset_at >= now + 3590 && u.reset_at <= now + 3660);
sp_usage_get("a", 0, 0, 1000, 3600, &u);
CHECK(u.tok_used == 600);
CHECK(u.frac > 0.59 && u.frac < 0.61);
/* the fuller of the two limits decides */
sp_usage_get("a", 5, 3600, 1000, 3600, &u);
CHECK(u.frac > 0.79 && u.frac < 0.81);
/* an old line is outside the window */
snprintf(path, sizeof(path), "%s/b.log", dir);
f = fopen(path, "w");
CHECK(f != NULL);
fprintf(f, "%ld 0 0\n%ld 0 0\n", now - 7200, now - 10);
fclose(f);
sp_usage_get("b", 10, 3600, 0, 0, &u);
CHECK(u.req_used == 1);
/* a benched provider says so */
sp_cooldown_set("b", now + 600, "test");
sp_usage_get("b", 10, 3600, 0, 0, &u);
CHECK(u.benched_until > now);
/* an id cannot leave the directory */
sp_usage_record("../evil", 0, 0);
snprintf(path, sizeof(path), "%s/../evil.log", dir);
CHECK(access(path, F_OK) != 0);
{
char b[128];
sp_usage_get("a", 10, 3600, 0, 0, &u);
sp_usage_brief(&u, b, sizeof(b));
CHECK(strstr(b, "1h 40%") != NULL);
}
/* opencode-go wants the session id in a header; others get none */
{
char h[200];
sp_openai_set_session("sess-42");
CHECK(sp_openai_session_header(
"https://opencode.ai/zen/go/v1/chat/completions", h,
sizeof(h)) > 0);
CHECK(strcmp(h, "x-opencode-session: sess-42") == 0);
CHECK(sp_openai_session_header("https://ollama.com/v1/chat/completions",
h, sizeof(h)) == 0);
CHECK(sp_openai_session_header("http://127.0.0.1:8080/v1/chat", h,
sizeof(h)) == 0);
}
snprintf(path, sizeof(path), "rm -rf %s", dir);
if (system(path) != 0)
failures++;
if (failures == 0)
puts("test_usage: ok");
return failures != 0;
}