providers: a 3s connect timeout, one model-list path, a tmux test of its own
The model-list probe hung on a host that accepted no connection and the delegate list had a second, worse way to ask: popen(curl) without the key and a line-by-line "id" scan that saw only the first model in one-line JSON. - http_get(): --connect-timeout 3, -m 8 for the answer. Every provider probe (/v1/models, usage/balance) uses it. - delegate_list probe=1 builds an sp_provider and calls sp_provider_served(), so it sends the key, reads the JSON and lists every model. - The route stays <endpoint>/models: /v1/models for opencode, ollama.com, api.anthropic.com and the junction gateway (verified against all four). - test_tmux unsets TMUX/TMUX_PANE and points TMUX_TMPDIR at its own temp dir: no session, window or keystroke on the user's server. The literal "x" probe text is gone. make check: all tests passed.
This commit is contained in:
1 parent
9af7695064
commit
42c11f95c7
4 files changed
+75
-46
No files matched your search
+42
-42
@@ -98,12 +98,6 @@ json_str(const char *args, const char *key, struct sp_buf *keep)
|
|||||||
return keep->p;
|
return keep->p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
models_url(const char *endpoint, char *out, size_t len)
|
|
||||||
{
|
|
||||||
sp_endpoint_url(endpoint, "models", out, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
tool_delegate_list(const char *args, struct sp_buf *out, void *ctx)
|
tool_delegate_list(const char *args, struct sp_buf *out, void *ctx)
|
||||||
{
|
{
|
||||||
@@ -148,49 +142,55 @@ tool_delegate_list(const char *args, struct sp_buf *out, void *ctx)
|
|||||||
d->host[0] != '\0' ? d->host : "");
|
d->host[0] != '\0' ? d->host : "");
|
||||||
sp_buf_printf(out, " wofuer: %s\n", d->about);
|
sp_buf_printf(out, " wofuer: %s\n", d->about);
|
||||||
if (probe && d->endpoint[0] != '\0') {
|
if (probe && d->endpoint[0] != '\0') {
|
||||||
char url[600], cmd[700];
|
struct sp_provider pv;
|
||||||
FILE *fp;
|
struct sp_buf ids = { 0 }, perr = { 0 };
|
||||||
char line[512];
|
char kenv[sizeof(pv.key_env)];
|
||||||
int seen = 0;
|
|
||||||
|
|
||||||
models_url(d->endpoint, url, sizeof(url));
|
/*
|
||||||
snprintf(cmd, sizeof(cmd), "curl -sS --max-time 8 '%s'",
|
* The same route and the same reader as every other
|
||||||
url);
|
* model list: /models under the endpoint's base, with
|
||||||
fp = popen(cmd, "r");
|
* the delegate's key and headers. The old popen(1)
|
||||||
|
* variant sent no key and saw only the first "id" of a
|
||||||
|
* one-line JSON answer.
|
||||||
|
*/
|
||||||
|
memset(&pv, 0, sizeof(pv));
|
||||||
|
snprintf(pv.id, sizeof(pv.id), "%s", d->id);
|
||||||
|
snprintf(pv.kind, sizeof(pv.kind), "%s", d->kind);
|
||||||
|
snprintf(pv.endpoint, sizeof(pv.endpoint), "%s",
|
||||||
|
d->endpoint);
|
||||||
|
snprintf(pv.model, sizeof(pv.model), "%s", d->model);
|
||||||
|
snprintf(pv.key_env, sizeof(pv.key_env), "%s",
|
||||||
|
d->key_env);
|
||||||
|
/* a literal key lives in 'key' until the child starts */
|
||||||
|
if (pv.key_env[0] == '\0' && d->key[0] != '\0') {
|
||||||
|
snprintf(kenv, sizeof(kenv),
|
||||||
|
"SPLINTER_PROBE_KEY_%ld", (long)getpid());
|
||||||
|
(void)setenv(kenv, d->key, 1);
|
||||||
|
snprintf(pv.key_env, sizeof(pv.key_env), "%s",
|
||||||
|
kenv);
|
||||||
|
}
|
||||||
sp_buf_puts(out, " models: ");
|
sp_buf_puts(out, " models: ");
|
||||||
if (fp == NULL) {
|
if (sp_buf_init(&ids) != 0 ||
|
||||||
sp_buf_puts(out, "nicht erreichbar\n");
|
sp_buf_init(&perr) != 0) {
|
||||||
|
sp_buf_puts(out, "(keine Modellliste)\n");
|
||||||
|
} else if (sp_provider_served(&pv, &ids, &perr) != 0) {
|
||||||
|
sp_buf_printf(out, "%s\n", perr.p != NULL ?
|
||||||
|
perr.p : "(keine Modellliste)");
|
||||||
} else {
|
} else {
|
||||||
while (fgets(line, sizeof(line), fp) != NULL) {
|
char *m, *save = NULL;
|
||||||
const char *m = strstr(line, "\"id\"");
|
int seen = 0;
|
||||||
|
|
||||||
if (m == NULL)
|
for (m = strtok_r(ids.p, "\n", &save);
|
||||||
continue;
|
m != NULL;
|
||||||
m = strchr(m, ':');
|
m = strtok_r(NULL, "\n", &save)) {
|
||||||
if (m == NULL)
|
if (seen++)
|
||||||
continue;
|
sp_buf_puts(out, ", ");
|
||||||
m = strchr(m, '"');
|
sp_buf_printf(out, "%s", m);
|
||||||
if (m == NULL)
|
|
||||||
continue;
|
|
||||||
{
|
|
||||||
const char *e = strchr(m + 1, '"');
|
|
||||||
size_t n = e != NULL ?
|
|
||||||
(size_t)(e - m - 1) : 0;
|
|
||||||
|
|
||||||
if (n > 0 && n < 80) {
|
|
||||||
if (seen++)
|
|
||||||
sp_buf_puts(out,
|
|
||||||
", ");
|
|
||||||
sp_buf_printf(out,
|
|
||||||
"%.*s", (int)n, m + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
(void)pclose(fp);
|
|
||||||
if (!seen)
|
|
||||||
sp_buf_puts(out, "(keine Modellliste)");
|
|
||||||
sp_buf_puts(out, "\n");
|
sp_buf_puts(out, "\n");
|
||||||
}
|
}
|
||||||
|
sp_buf_free(&ids);
|
||||||
|
sp_buf_free(&perr);
|
||||||
}
|
}
|
||||||
sp_buf_puts(out, "\n");
|
sp_buf_puts(out, "\n");
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-2
@@ -248,6 +248,11 @@ sp_provider_use(struct sp_agent *ag, const struct sp_config *cfg, size_t idx,
|
|||||||
* One GET, no body. curl writes to a pipe; the URL and the header go to curl
|
* One GET, no body. curl writes to a pipe; the URL and the header go to curl
|
||||||
* on stdin/argv respectively, so a token never shows up in ps.
|
* on stdin/argv respectively, so a token never shows up in ps.
|
||||||
*/
|
*/
|
||||||
|
/* A probe may not hang on a host that does not answer: 3s to connect, 8s
|
||||||
|
* for the whole answer. */
|
||||||
|
#define PROVIDER_CONNECT_TIMEOUT_S 3
|
||||||
|
#define PROVIDER_GET_TIMEOUT_S 8
|
||||||
|
|
||||||
static int
|
static int
|
||||||
http_get(const char *url, const char *const *headers, struct sp_buf *out,
|
http_get(const char *url, const char *const *headers, struct sp_buf *out,
|
||||||
struct sp_buf *err)
|
struct sp_buf *err)
|
||||||
@@ -256,10 +261,13 @@ http_get(const char *url, const char *const *headers, struct sp_buf *out,
|
|||||||
pid_t child;
|
pid_t child;
|
||||||
const char *args[8];
|
const char *args[8];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
char tbuf[16];
|
char tbuf[16], cbuf[16];
|
||||||
|
|
||||||
snprintf(tbuf, sizeof(tbuf), "%d", 8);
|
snprintf(tbuf, sizeof(tbuf), "%d", PROVIDER_GET_TIMEOUT_S);
|
||||||
|
snprintf(cbuf, sizeof(cbuf), "%d", PROVIDER_CONNECT_TIMEOUT_S);
|
||||||
args[n++] = "-sS";
|
args[n++] = "-sS";
|
||||||
|
args[n++] = "--connect-timeout";
|
||||||
|
args[n++] = cbuf;
|
||||||
args[n++] = "-m";
|
args[n++] = "-m";
|
||||||
args[n++] = tbuf;
|
args[n++] = tbuf;
|
||||||
args[n] = NULL;
|
args[n] = NULL;
|
||||||
|
|||||||
@@ -300,6 +300,16 @@ main(void)
|
|||||||
"https://api.anthropic.com/v1/models") == 0);
|
"https://api.anthropic.com/v1/models") == 0);
|
||||||
CHECK(strcmp(sp_endpoint_url("https://h/v1/messages", NULL, u,
|
CHECK(strcmp(sp_endpoint_url("https://h/v1/messages", NULL, u,
|
||||||
sizeof(u)), "https://h/v1") == 0);
|
sizeof(u)), "https://h/v1") == 0);
|
||||||
|
/* the model list of the three providers Konrad uses */
|
||||||
|
CHECK(strcmp(sp_endpoint_url("https://opencode.ai/zen/go/v1",
|
||||||
|
"models", u, sizeof(u)),
|
||||||
|
"https://opencode.ai/zen/go/v1/models") == 0);
|
||||||
|
CHECK(strcmp(sp_endpoint_url("https://ollama.com/v1",
|
||||||
|
"models", u, sizeof(u)),
|
||||||
|
"https://ollama.com/v1/models") == 0);
|
||||||
|
CHECK(strcmp(sp_endpoint_url("https://api.anthropic.com/v1",
|
||||||
|
"models", u, sizeof(u)),
|
||||||
|
"https://api.anthropic.com/v1/models") == 0);
|
||||||
}
|
}
|
||||||
CHECK(sp_provider_endpoint_ok("https://x/v1") == 1);
|
CHECK(sp_provider_endpoint_ok("https://x/v1") == 1);
|
||||||
CHECK(sp_provider_endpoint_ok("x/v1") == 0);
|
CHECK(sp_provider_endpoint_ok("x/v1") == 0);
|
||||||
|
|||||||
+13
-2
@@ -53,6 +53,15 @@ main(void)
|
|||||||
const char *s;
|
const char *s;
|
||||||
|
|
||||||
CHECK(mkdtemp(dir) != NULL);
|
CHECK(mkdtemp(dir) != NULL);
|
||||||
|
/*
|
||||||
|
* A tmux server of our own. $TMUX (set when this runs inside the
|
||||||
|
* user's tmux) wins over TMUX_TMPDIR, and without either the test
|
||||||
|
* would create its session on the user's server - a stray window, and
|
||||||
|
* keys meant for `cat` could land in a real pane.
|
||||||
|
*/
|
||||||
|
unsetenv("TMUX");
|
||||||
|
unsetenv("TMUX_PANE");
|
||||||
|
setenv("TMUX_TMPDIR", dir, 1);
|
||||||
memset(&sess, 0, sizeof(sess));
|
memset(&sess, 0, sizeof(sess));
|
||||||
snprintf(sess.dir, sizeof(sess.dir), "%s", dir);
|
snprintf(sess.dir, sizeof(sess.dir), "%s", dir);
|
||||||
memset(®, 0, sizeof(reg));
|
memset(®, 0, sizeof(reg));
|
||||||
@@ -68,8 +77,8 @@ main(void)
|
|||||||
}
|
}
|
||||||
CHECK(sp_registry_find(®, "tmux_watch") == NULL);
|
CHECK(sp_registry_find(®, "tmux_watch") == NULL);
|
||||||
|
|
||||||
/* nothing is targeted yet */
|
/* nothing is targeted yet: the text must not reach any terminal */
|
||||||
s = call(®, "tmux_ask", "{\"text\":\"x\"}", &out);
|
s = call(®, "tmux_ask", "{\"text\":\"probe\"}", &out);
|
||||||
CHECK(strstr(s, "error: no tmux target") != NULL);
|
CHECK(strstr(s, "error: no tmux target") != NULL);
|
||||||
|
|
||||||
/* a name that could be an option, a second target or a command */
|
/* a name that could be an option, a second target or a command */
|
||||||
@@ -115,6 +124,8 @@ done:
|
|||||||
sp_registry_free(®);
|
sp_registry_free(®);
|
||||||
sp_buf_free(&out);
|
sp_buf_free(&out);
|
||||||
sp_buf_free(&err);
|
sp_buf_free(&err);
|
||||||
|
/* only the private server: TMUX_TMPDIR points into dir */
|
||||||
|
(void)system("tmux kill-server 2>/dev/null");
|
||||||
snprintf(args, sizeof(args), "rm -rf '%s'", dir);
|
snprintf(args, sizeof(args), "rm -rf '%s'", dir);
|
||||||
(void)system(args);
|
(void)system(args);
|
||||||
if (failures == 0)
|
if (failures == 0)
|
||||||
|
|||||||
Reference in new issue
Block a user