- openai transport sends x-opencode-session (opencode-go refused every request without it) and a splinter user agent - the message catalogues are compiled in: an installed binary no longer shows raw keys (ui.ready ...); LANG=de_DE now selects German (lang was emptied by an overlapping snprintf), pt_BR is matched - sidebar rewritten (agent, provider/model, usage, goal/plan, last turn); idle widgets say nothing; status line shows only what matters - tool calls shown readable instead of JSON with \u escapes - providers without their key variable are skipped by pools and refused by -m with a clear message; --doctor checks every provider Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
117 lines
3.1 KiB
C
117 lines
3.1 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);
|
|
CHECK(u.reset_at >= now + 3590 && u.reset_at <= now + 3601);
|
|
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;
|
|
}
|