cli: the main transport falls back at the limit, too
cli_fallback = claude1, claude0 names cli delegates whose cmd and env= take over when cli_cmd answers with its limit. The main command is benched as "cli", a fallback under its own id, on the same bench the delegation uses (sp_limit_parse, sp_cooldown_until, sp_cooldown_set are now exported from libsplinter). The session key stays, so claude resumes the same conversation under the next account. With everyone benched the turn fails with the first reset, as WAIT_QUOTA ready_at=. A fallback's env= goes through a 0600 file the shell sources, not a command line. A delegate's child is benched under the delegate's id (SPLINTER_CLI_ID), never as "cli". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ca8cdb6d13
commit
808039cd58
7 files changed
+302
-21
No files matched your search
+162
-6
@@ -20,6 +20,9 @@
|
||||
* message (the program keeps no history of its own)
|
||||
* SPLINTER_CLI_TIMEOUT seconds, default 1800
|
||||
* SPLINTER_CLI_SESSION the session key (set by splinter); empty: no sessions
|
||||
* SPLINTER_CLI_ID the name this command is benched under at its
|
||||
* limit; "cli" for the main one, the delegate's id
|
||||
* in a delegate's child
|
||||
* SPLINTER_CLI_STATE where the claude resume markers live,
|
||||
* default ~/.splinter/cli
|
||||
*
|
||||
@@ -30,6 +33,8 @@
|
||||
* must not do.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -42,6 +47,7 @@
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
#include "i18n.h"
|
||||
#include "config.h"
|
||||
|
||||
#define OUT_CAP (64u << 20)
|
||||
|
||||
@@ -49,6 +55,10 @@ struct cli_cfg {
|
||||
char cmd[512];
|
||||
char timeout[16];
|
||||
int full;
|
||||
struct sp_host *host;
|
||||
/* for a fallback: the delegate's env=, sourced from a 0600 file */
|
||||
const char (*env)[SP_CONFIG_ENV_LEN];
|
||||
size_t nenv;
|
||||
};
|
||||
|
||||
static void
|
||||
@@ -249,7 +259,7 @@ ci_contains(const char *hay, const char *needle)
|
||||
static int
|
||||
build_cmd(const struct cli_cfg *c, const char *path, const char *prompt,
|
||||
const char *key, const char *uuid, const char *extra, const char *errpath,
|
||||
struct sp_buf *cmd)
|
||||
const char *envfile, struct sp_buf *cmd)
|
||||
{
|
||||
int placeholder;
|
||||
const char *p = c->cmd;
|
||||
@@ -257,6 +267,10 @@ build_cmd(const struct cli_cfg *c, const char *path, const char *prompt,
|
||||
sp_buf_clear(cmd);
|
||||
placeholder = strstr(c->cmd, "{file}") != NULL ||
|
||||
strstr(c->cmd, "{prompt}") != NULL;
|
||||
if (envfile != NULL && envfile[0] != '\0' &&
|
||||
(sp_buf_puts(cmd, ". ") != 0 || sh_word(cmd, envfile) != 0 ||
|
||||
sp_buf_puts(cmd, " && ") != 0))
|
||||
return -1;
|
||||
if (sp_buf_printf(cmd, "timeout %s ", c->timeout) != 0)
|
||||
return -1;
|
||||
while (*p != '\0') {
|
||||
@@ -337,13 +351,48 @@ ok_status(int status)
|
||||
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The delegate's env= for a fallback, as `export NAME='value'` lines in a file
|
||||
* only this user can read, sourced by the shell. Not `env NAME=value cmd`: a
|
||||
* command line is visible to every user on the machine through ps.
|
||||
*/
|
||||
static int
|
||||
cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
||||
struct sp_buf *err)
|
||||
write_envfile(const struct cli_cfg *c, const char *path)
|
||||
{
|
||||
struct sp_buf b;
|
||||
size_t i;
|
||||
int fd, rc = -1;
|
||||
|
||||
if (sp_buf_init(&b) != 0)
|
||||
return -1;
|
||||
for (i = 0; i < c->nenv; i++) {
|
||||
const char *eq = strchr(c->env[i], '=');
|
||||
|
||||
if (eq == NULL)
|
||||
continue;
|
||||
if (sp_buf_puts(&b, "export ") != 0 ||
|
||||
sp_buf_add(&b, c->env[i], (size_t)(eq - c->env[i]) + 1) != 0 ||
|
||||
sh_word(&b, eq + 1) != 0 || sp_buf_puts(&b, "\n") != 0)
|
||||
goto out;
|
||||
}
|
||||
fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
||||
if (fd < 0)
|
||||
goto out;
|
||||
if (b.len == 0 || write(fd, b.p, b.len) == (ssize_t)b.len)
|
||||
rc = 0;
|
||||
close(fd);
|
||||
out:
|
||||
sp_buf_free(&b);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
cli_send_one(struct cli_cfg *c, const struct sp_conv *conv,
|
||||
struct sp_reply *out, struct sp_buf *err)
|
||||
{
|
||||
struct cli_cfg *c = ctx;
|
||||
struct sp_buf body, cmd, raw;
|
||||
char path[512], errpath[540] = "", errtext[512];
|
||||
char envfile[540] = "";
|
||||
char uuid[37] = "", ses[31] = "", extra[96] = "";
|
||||
const char *tmpdir = getenv("TMPDIR");
|
||||
const char *key = getenv("SPLINTER_CLI_SESSION");
|
||||
@@ -401,8 +450,17 @@ cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
||||
* the prompt so it never lands in the middle of a curses screen.
|
||||
*/
|
||||
snprintf(errpath, sizeof(errpath), "%s.err", path);
|
||||
if (c->nenv > 0) {
|
||||
snprintf(envfile, sizeof(envfile), "%s.env", path);
|
||||
if (write_envfile(c, envfile) != 0) {
|
||||
sp_buf_printf(err, "cli: env file: %s", strerror(errno));
|
||||
envfile[0] = '\0';
|
||||
unlink(path);
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
if (build_cmd(c, path, body.p != NULL ? body.p : "", key, uuid, extra,
|
||||
errpath, &cmd) != 0 ||
|
||||
errpath, envfile, &cmd) != 0 ||
|
||||
run_cmd(cmd.p, errpath, &raw, errtext, sizeof(errtext), &status,
|
||||
err) != 0) {
|
||||
unlink(path);
|
||||
@@ -420,7 +478,7 @@ cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
||||
snprintf(extra, sizeof(extra), "%s %s",
|
||||
resume ? "--resume" : "--session-id", uuid);
|
||||
if (build_cmd(c, path, body.p != NULL ? body.p : "", key, uuid,
|
||||
extra, errpath, &cmd) != 0 ||
|
||||
extra, errpath, envfile, &cmd) != 0 ||
|
||||
run_cmd(cmd.p, errpath, &raw, errtext, sizeof(errtext),
|
||||
&status, err) != 0) {
|
||||
unlink(path);
|
||||
@@ -446,6 +504,10 @@ cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
||||
sp_buf_printf(err, sp_td("cli.no_output", "cli: '%s' produced no output"), c->cmd);
|
||||
goto fail;
|
||||
}
|
||||
if (envfile[0] != '\0') {
|
||||
(void)unlink(envfile);
|
||||
envfile[0] = '\0';
|
||||
}
|
||||
out->status = 200;
|
||||
out->content = strdup(raw.p);
|
||||
if (out->content == NULL)
|
||||
@@ -458,12 +520,105 @@ cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
||||
fail:
|
||||
if (errpath[0] != '\0')
|
||||
(void)unlink(errpath);
|
||||
if (envfile[0] != '\0')
|
||||
(void)unlink(envfile);
|
||||
sp_buf_free(&body);
|
||||
sp_buf_free(&cmd);
|
||||
sp_buf_free(&raw);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* The main cli at its limit. cli_cmd is tried first, benched as "cli"; then
|
||||
* the cli delegates named in cli_fallback, in order, each with its own cmd and
|
||||
* env=, benched under its own id - the same bench the delegation uses, so an
|
||||
* account at its limit is skipped everywhere. The session key stays the same,
|
||||
* so claude resumes the same conversation under the next account.
|
||||
*/
|
||||
static int
|
||||
cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
struct cli_cfg *c = ctx;
|
||||
const struct sp_config *cfg;
|
||||
char list[300], *id, *save = NULL;
|
||||
long until, soonest = 0;
|
||||
int first = 1, tried = 0, rc = -1;
|
||||
|
||||
if (c == NULL)
|
||||
return -1;
|
||||
cfg = c->host != NULL ? sp_host_config(c->host) : NULL;
|
||||
/*
|
||||
* In a delegate's child the command is that delegate's, and so is the
|
||||
* bench: SPLINTER_CLI_ID names it. Benching "cli" from there would bench
|
||||
* the main program for an account it may not even use.
|
||||
*/
|
||||
{
|
||||
const char *self = getenv("SPLINTER_CLI_ID");
|
||||
|
||||
snprintf(list, sizeof(list), "%s,%s",
|
||||
self != NULL && *self != '\0' ? self : "cli",
|
||||
cfg != NULL ? cfg->cli_fallback : "");
|
||||
}
|
||||
for (id = strtok_r(list, ", ", &save); id != NULL;
|
||||
id = strtok_r(NULL, ", ", &save), first = 0) {
|
||||
struct cli_cfg alt = *c;
|
||||
const struct sp_delegate *d = NULL;
|
||||
size_t k;
|
||||
|
||||
if (!first) {
|
||||
for (k = 0; cfg != NULL && k < cfg->ndelegates; k++)
|
||||
if (strcmp(cfg->delegate[k].id, id) == 0)
|
||||
d = &cfg->delegate[k];
|
||||
if (d == NULL || strcmp(d->kind, "cli") != 0) {
|
||||
if (c->host != NULL)
|
||||
(void)sp_host_note(c->host, "cli: "
|
||||
"cli_fallback names no cli delegate");
|
||||
continue;
|
||||
}
|
||||
snprintf(alt.cmd, sizeof(alt.cmd), "%s",
|
||||
d->cmd[0] != '\0' ? d->cmd : "claude -p");
|
||||
alt.env = (const char (*)[SP_CONFIG_ENV_LEN])d->env;
|
||||
alt.nenv = d->nenv;
|
||||
}
|
||||
if ((until = sp_cooldown_until(id)) != 0) {
|
||||
if (soonest == 0 || until < soonest)
|
||||
soonest = until;
|
||||
continue;
|
||||
}
|
||||
if (tried > 0 && c->host != NULL) {
|
||||
char note[96];
|
||||
|
||||
snprintf(note, sizeof(note), "cli: falling back to %s",
|
||||
id);
|
||||
(void)sp_host_note(c->host, note);
|
||||
}
|
||||
tried++;
|
||||
sp_buf_clear(err);
|
||||
rc = cli_send_one(&alt, conv, out, err);
|
||||
if (rc == 0 || !sp_limit_parse(err->p, &until))
|
||||
return rc;
|
||||
sp_cooldown_set(id, until, err->p);
|
||||
if (soonest == 0 || until < soonest)
|
||||
soonest = until;
|
||||
}
|
||||
if (tried == 0 || rc != 0) {
|
||||
char when[32] = "?";
|
||||
time_t tt = (time_t)soonest;
|
||||
struct tm tm;
|
||||
|
||||
if (soonest > 0)
|
||||
strftime(when, sizeof(when), "%Y-%m-%d %H:%M",
|
||||
localtime_r(&tt, &tm));
|
||||
if (tried == 0)
|
||||
sp_buf_clear(err);
|
||||
sp_buf_printf(err, "%scli: every account at its usage limit, "
|
||||
"first back %s (WAIT_QUOTA ready_at=%ld)",
|
||||
err->len > 0 ? " -- " : "", when, soonest);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
cli_init(struct sp_host *h)
|
||||
{
|
||||
@@ -480,6 +635,7 @@ cli_init(struct sp_host *h)
|
||||
to != NULL && *to != '\0' ? to : "1800");
|
||||
cfg.full = full != NULL && strcmp(full, "0") != 0;
|
||||
|
||||
cfg.host = h;
|
||||
tr.send = cli_send;
|
||||
tr.ctx = &cfg;
|
||||
return sp_host_add_transport(h, "cli", tr);
|
||||
|
||||
Reference in new issue
Block a user