Files
splinter/tests/test_usage.c
T
lotherk bff47eede8 ui: palette roles, a working unlimited usage count, /goal as the recap
Three things the screen and the goal were getting wrong.

Colours. The theme had one accent for the user and grey for everything
else. theme.h now carries agent, think, tool and notice colours. A CDE
palette maps them to sets 7 (answer), 3 (reasoning), 6 (tool) and 8
(status); Xresources uses colour10/11/14/13 on dark and 2/3/6/5 on
light. Role colours keep their hue and are pushed from the surface only
when they would disappear; each role has a slightly different minimum,
so even a monochrome palette yields distinguishable steps. The 256-colour
mapping now takes the nearest cube/ramp entry instead of rounding each
channel down, which turned a dark orange into grey 240.

Usage. sp_usage_get() only counted requests inside a declared window. A
provider without limit= therefore always read "0 calls" in the sidebar,
although every call was in its log. Calls are now counted when no window
is declared; test_usage covers it.

Goal. /goal <text> set a variable and answered "goal set" without starting
a turn. The engine now starts the recap prompt: the model writes a short
recap, the focus plugin remembers that answer and injects it every seventh
turn (SPLINTER_FOCUS_EVERY still overrides). /recap is gone as a command;
its old behaviour is what /goal does now. A palette and a wiki page are
not enough to make that visible: the prompt, the confirmation entry and
the sidebar goal all show up in the chat.

Tests: palette role separation and indexed roles in test_theme, four
unlimited calls in test_usage, recap capture/injection and the missing
/recap in test_focus, and a settle delay so the UI tests type after
ui_read_line() owns the keyboard. make check: all tests passed.
2026-11-16 17:48:22 +00:00

130 lines
3.6 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);
/*
* Without a declared limit the sidebar still says how many calls the
* log holds; a fresh provider no longer reads "0 calls" forever.
*/
sp_usage_get("a", 0, 0, 0, 0, &u);
CHECK(u.req_used == 4);
CHECK(u.frac < 0);
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;
}