env=NAME=VALUE on a delegate line (repeatable) is set in the child right before exec and beats what the parent exported, so two delegates can run the same cli with two accounts. Remotely it goes in front as `env 'NAME=VALUE'`, quoted like the rest. cli_env = NAME=VALUE does the same for the main cli transport. A bad name is an error with the line. Also in this commit, the work in progress from the tree: swarm and provider state, the provider/scheduler/planner/tmux plugins, the ui test, whose tmux session name is now per run so parallel runs do not collide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
278 lines
9.5 KiB
C
278 lines
9.5 KiB
C
/*
|
|
* splinter -- provider adapters (OpenCode/Ollama/openai-compat), per-key quota/usage.
|
|
*
|
|
* Plugin-format adapters: dispatch by tr.provider ("opencode","ollama","openai-compat").
|
|
* Shared per-(provider,key_id) state in ~/.splinter/providers/ (flock+atomic), so
|
|
* multiple instances sharing a key see the same quota/burst view.
|
|
*
|
|
* - preflight: blocks on quota_ready_at; short backoff delays (<=30s) allowed by caller.
|
|
* - account: records status/tokens; 429/402/529 -> quota wait (Retry-After honored by
|
|
* transport/agent when known; defaults here). 503/408 -> short backoff.
|
|
* - Ollama (local): quota gating relaxed (transient/backoff only) unless explicit quota.
|
|
* - OpenCode/cloud: quota exhausted -> WAIT_QUOTA with ready_at; "quota back in N days"
|
|
* surfaced via reason/note and durable delegation jobs (waiting_quota+run_after) +
|
|
* scheduler auto-retry. Optional probe via SPLINTER_QUOTA_PROBE_URL (GET, bearer from
|
|
* SPLINTER_QUOTA_PROBE_KEY) when SPLINTER_PROVIDER_PROBE=1.
|
|
*/
|
|
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <dirent.h>
|
|
#include <fcntl.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/wait.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter_plugin.h"
|
|
|
|
static int is_ollama(const char *p) { return p && strcmp(p,"ollama")==0; }
|
|
static int is_opencode(const char *p){ return p && (strcmp(p,"opencode")==0 || strcmp(p,"open-code")==0); }
|
|
|
|
struct probe_apply { long ready; long now; };
|
|
static void probe_apply_fn(struct sp_prov_state *s, void *p){
|
|
struct probe_apply *c=p;
|
|
if (c->ready > s->quota_ready_at) s->quota_ready_at = c->ready;
|
|
if (c->ready > 0) snprintf(s->note,sizeof(s->note),"quota probe ready_at=%ld", c->ready);
|
|
s->last_update = c->now;
|
|
}
|
|
static void probe_apply(const char *provider, const char *key_id, long ready){
|
|
struct probe_apply c; c.ready=ready; c.now=(long)time(NULL);
|
|
sp_provstate_update(provider,key_id,probe_apply_fn,&c);
|
|
}
|
|
|
|
static void run_probe(const char *provider, const char *key_id)
|
|
{
|
|
const char *on = getenv("SPLINTER_PROVIDER_PROBE");
|
|
const char *url = getenv("SPLINTER_QUOTA_PROBE_URL");
|
|
const char *key = getenv("SPLINTER_QUOTA_PROBE_KEY");
|
|
const char *kenv = getenv("SPLINTER_QUOTA_PROBE_KEY_ENV");
|
|
char buf[4096];
|
|
size_t n = 0;
|
|
long ready = 0, ra = 0;
|
|
const char *q;
|
|
int in_pipe[2], out_pipe[2], devnull;
|
|
pid_t pid;
|
|
int st = 0;
|
|
char cfg[4096];
|
|
size_t off = 0, len;
|
|
ssize_t wr, rd;
|
|
|
|
if (!on || strcmp(on, "1") != 0 || !url || !*url) return;
|
|
if ((!key || !*key) && kenv && *kenv) key = getenv(kenv);
|
|
if (strncmp(url, "http://", 7) != 0 && strncmp(url, "https://", 8) != 0) return;
|
|
if (strpbrk(url, "\"\\\n\r") != NULL) return;
|
|
if (key && *key && strpbrk(key, "\"\\\n\r") != NULL) return;
|
|
|
|
if (pipe(in_pipe) != 0) return;
|
|
if (pipe(out_pipe) != 0) {
|
|
close(in_pipe[0]);
|
|
close(in_pipe[1]);
|
|
return;
|
|
}
|
|
pid = fork();
|
|
if (pid < 0) {
|
|
close(in_pipe[0]); close(in_pipe[1]);
|
|
close(out_pipe[0]); close(out_pipe[1]);
|
|
return;
|
|
}
|
|
if (pid == 0) {
|
|
char *const argv[] = {"curl", "-fsS", "-m", "10", "-K", "-", NULL};
|
|
dup2(in_pipe[0], 0);
|
|
dup2(out_pipe[1], 1);
|
|
devnull = open("/dev/null", O_WRONLY);
|
|
if (devnull >= 0) {
|
|
dup2(devnull, 2);
|
|
close(devnull);
|
|
}
|
|
close(in_pipe[0]); close(in_pipe[1]);
|
|
close(out_pipe[0]); close(out_pipe[1]);
|
|
execvp(argv[0], argv);
|
|
_exit(127);
|
|
}
|
|
|
|
close(in_pipe[0]);
|
|
close(out_pipe[1]);
|
|
len = (size_t)snprintf(cfg, sizeof(cfg), "url = \"%s\"\n", url);
|
|
if (len < sizeof(cfg) && key && *key) {
|
|
int m = snprintf(cfg + len, sizeof(cfg) - len,
|
|
"header = \"Authorization: Bearer %s\"\n", key);
|
|
len = (m < 0 || (size_t)m >= sizeof(cfg) - len) ? sizeof(cfg) : len + (size_t)m;
|
|
}
|
|
/* An over-long config is never sent: curl then reads an empty config and
|
|
* fails, and the exit status below discards the result. */
|
|
if (len >= sizeof(cfg))
|
|
len = 0;
|
|
{
|
|
void (*old_pipe)(int) = signal(SIGPIPE, SIG_IGN);
|
|
off = 0;
|
|
while (off < len) {
|
|
wr = write(in_pipe[1], cfg + off, len - off);
|
|
if (wr <= 0) break;
|
|
off += (size_t)wr;
|
|
}
|
|
close(in_pipe[1]);
|
|
signal(SIGPIPE, old_pipe);
|
|
}
|
|
|
|
n = 0;
|
|
for (;;) {
|
|
char rbuf[1024];
|
|
rd = read(out_pipe[0], rbuf, sizeof(rbuf));
|
|
if (rd <= 0) break;
|
|
if (n < sizeof(buf) - 1) {
|
|
size_t take = (size_t)rd;
|
|
if (take > sizeof(buf) - 1 - n) take = sizeof(buf) - 1 - n;
|
|
memcpy(buf + n, rbuf, take);
|
|
n += take;
|
|
}
|
|
}
|
|
buf[n] = '\0';
|
|
close(out_pipe[0]);
|
|
while (waitpid(pid, &st, 0) < 0 && errno == EINTR)
|
|
;
|
|
|
|
if (WIFEXITED(st) && WEXITSTATUS(st) == 0) {
|
|
q = strstr(buf, "\"quota_ready_at\""); if (q) { q = strchr(q, ':'); if (q) ready = strtol(q + 1, NULL, 10); }
|
|
q = strstr(buf, "\"retry_after\""); if (q) { q = strchr(q, ':'); if (q) ra = strtol(q + 1, NULL, 10); }
|
|
if (ready <= 0 && ra > 0) ready = (long)time(NULL) + ra;
|
|
if (ready > 0) probe_apply(provider, key_id, ready);
|
|
}
|
|
}
|
|
|
|
static int
|
|
prov_preflight(void *ctx, const char *provider, const char *key_id,
|
|
struct sp_prov_result *out)
|
|
{
|
|
long now = (long)time(NULL);
|
|
long ready = 0;
|
|
int r;
|
|
(void)ctx;
|
|
if (!out) return 0;
|
|
out->decision = SP_PROV_ADMIT;
|
|
if (!provider || !*provider || !key_id || !*key_id)
|
|
return 0;
|
|
/* ollama local: no cloud quota gating unless state explicitly says quota */
|
|
if (is_ollama(provider)) {
|
|
struct sp_prov_state st;
|
|
if (sp_provstate_load(provider,key_id,&st)==0 && st.quota_ready_at > now) {
|
|
out->decision = SP_PROV_WAIT_QUOTA;
|
|
out->ready_at = st.quota_ready_at;
|
|
out->retry_after_s = st.quota_ready_at - now;
|
|
snprintf(out->reason,sizeof(out->reason),"ollama quota/backoff until %ld", st.quota_ready_at);
|
|
}
|
|
return 0;
|
|
}
|
|
r = sp_provstate_preflight(provider, key_id, now, &ready,
|
|
out->reason, sizeof(out->reason));
|
|
if (r == 1) {
|
|
out->decision = SP_PROV_WAIT_QUOTA;
|
|
out->ready_at = ready;
|
|
out->retry_after_s = ready > now ? ready - now : 0;
|
|
if (out->retry_after_s > 3600)
|
|
snprintf(out->reason, sizeof(out->reason),
|
|
"%s quota exhausted: wait ~%ldh (ready_at=%ld), auto-scheduled when quota returns",
|
|
is_opencode(provider)?"opencode":"provider",
|
|
out->retry_after_s/3600, ready);
|
|
else if (!out->reason[0])
|
|
snprintf(out->reason, sizeof(out->reason),
|
|
"quota/backoff until %ld (auto-schedule)", ready);
|
|
/* optional probe to refine ready_at */
|
|
run_probe(provider, key_id);
|
|
} else if (r == 2) {
|
|
out->decision = SP_PROV_DELAY;
|
|
out->ready_at = ready;
|
|
out->retry_after_s = ready > now ? ready - now : 0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
prov_account(void *ctx, const char *provider, const char *key_id,
|
|
long http_status, size_t tokens_in, size_t tokens_out)
|
|
{
|
|
long ra = 0;
|
|
(void)ctx;
|
|
if (!provider || !*provider || !key_id || !*key_id)
|
|
return;
|
|
if (is_ollama(provider) && (http_status==429||http_status==402||http_status==529)) {
|
|
/* local ollama shouldn't quota like cloud, but handle gracefully */
|
|
ra = 30;
|
|
}
|
|
/* retry_after_s: transport/agent apply parsed Retry-After via quota_hint/provstate;
|
|
* here we record status/tokens and apply status-based defaults. */
|
|
sp_provstate_account(provider, key_id, http_status, tokens_in, tokens_out, ra);
|
|
/* on quota exhaustion, probe for accurate ready_at (opt-in) */
|
|
if (http_status==429 || http_status==402 || http_status==529)
|
|
run_probe(provider, key_id);
|
|
}
|
|
|
|
static const struct sp_provider_ops ops = { .ctx=NULL, .preflight=prov_preflight, .account=prov_account };
|
|
|
|
static void fmt_dur(long s, char *out, size_t cap){
|
|
if(s>=86400) snprintf(out,cap,"%ldd%ldh",s/86400,(s%86400)/3600);
|
|
else if(s>=3600) snprintf(out,cap,"%ldh%ldm",s/3600,(s%3600)/60);
|
|
else snprintf(out,cap,"%lds",s);
|
|
}
|
|
|
|
static int
|
|
cmd_quota(struct sp_host *h, const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
char dir[512]; DIR *dp; struct dirent *prov;
|
|
const char *home = getenv("HOME");
|
|
long now=(long)time(NULL);
|
|
(void)h;(void)args;(void)ctx;
|
|
if(!home) home="/tmp";
|
|
snprintf(dir,sizeof(dir),"%s/.splinter/providers", home);
|
|
sp_buf_puts(out,"per-key provider quota/usage (shared):\n");
|
|
dp=opendir(dir);
|
|
if(!dp){ sp_buf_puts(out," (no provider state yet)\n"); return 0; }
|
|
while((prov=readdir(dp))!=NULL){
|
|
char pdir[600]; DIR *dp2; struct dirent *kf;
|
|
if(prov->d_name[0]=='.') continue;
|
|
snprintf(pdir,sizeof(pdir),"%.300s/%.255s",dir,prov->d_name);
|
|
dp2=opendir(pdir); if(!dp2) continue;
|
|
while((kf=readdir(dp2))!=NULL){
|
|
char *dot, path[700], kid[64]; struct sp_prov_state st; char dur[32];
|
|
if(kf->d_name[0]=='.') continue;
|
|
dot=strstr(kf->d_name,".json"); if(!dot) continue;
|
|
{ size_t kl=(size_t)(dot-kf->d_name); if(kl>=sizeof(kid)) kl=sizeof(kid)-1; memcpy(kid,kf->d_name,kl); kid[kl]='\0'; }
|
|
snprintf(path,sizeof(path),"%.400s/%.255s",pdir,kf->d_name);
|
|
(void)path;
|
|
if(sp_provstate_load(prov->d_name,kid,&st)==0){
|
|
sp_buf_printf(out," %s/%s req=%u tok=%lu/%lu status=%ld",
|
|
prov->d_name,kid,st.req_count,st.tok_in,st.tok_out,st.last_status);
|
|
if(st.quota_ready_at>now){ fmt_dur(st.quota_ready_at-now,dur,sizeof(dur)); sp_buf_printf(out," WAIT_QUOTA %s (ready_at=%ld)",dur,st.quota_ready_at); }
|
|
else if(st.backoff_until>now){ fmt_dur(st.backoff_until-now,dur,sizeof(dur)); sp_buf_printf(out," backoff %s",dur); }
|
|
if(st.note[0]) sp_buf_printf(out," [%s]",st.note);
|
|
sp_buf_puts(out,"\n");
|
|
}
|
|
}
|
|
closedir(dp2);
|
|
}
|
|
closedir(dp);
|
|
sp_buf_puts(out,"adapters: opencode, ollama, openai-compat (per-key). probe: SPLINTER_PROVIDER_PROBE=1 + SPLINTER_QUOTA_PROBE_URL/KEY.\n");
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
provider_init(struct sp_host *h)
|
|
{
|
|
sp_host_set_provider(h, &ops);
|
|
sp_host_add_command(h, "quota", cmd_quota, NULL);
|
|
return 0;
|
|
}
|
|
|
|
static const struct sp_plugin plugin = {
|
|
SP_PLUGIN_ABI, "provider", "0.2", provider_init, NULL, NULL
|
|
};
|
|
|
|
const struct sp_plugin *
|
|
sp_plugin_describe(void)
|
|
{
|
|
return &plugin;
|
|
}
|