Files
splinter/tests/test_cli.c
T
Konrad 50bc6751b2 splinter: real-model fixes, steering, cli transport
First run against a real model (Qwen3.6-35B-A3B on kontron, strata-poly)
exposed two things a stub never would:

- our tool schema sent "properties":{}, which tells the model that no
  arguments exist. Tools now carry a real JSON schema (echo/command/clock), and
  the loop completes a tool round against the 35B: request 1 -> tool_call,
  request 2 -> "Das Ergebnis ist: ping".
- an answer longer than the screen was not drawn at all: the entry did not
  'fit', so the viewport skipped it and the chat looked empty. draw_history is
  now a real viewport (total height, bottom-anchored, entries taller than the
  screen start at the offset that puts their end on screen).

Steering: a turn runs on its own thread while the UI keeps reading keys. Enter
queues text the loop picks up at the next step boundary; Ctrl-C cancels; in a
single-step turn the text becomes the next message instead of disappearing.
Curses remains UI-thread-only. A dangling ag->cfg after the turn made a steer
run twice - the caller's config is now restored, and the session log confirms
one user line per steer.

Transports are plugins now (sp_host_add_transport / --transport). plugins/cli
makes any program the model: SPLINTER_CLI_CMD, run through /bin/sh, prompt on
stdin, stdout as the answer, 4 MiB cap, non-zero exit with the program's stderr,
empty output is an error. Verified with sed, with claude -p (2.9 s, answer OK)
and with cat.

Eleven test binaries green, test_cli included.
2026-11-07 15:43:47 +00:00

107 lines
2.9 KiB
C

/*
* splinter -- the cli transport: any program as the model.
*
* Deterministic: the "model" here is /bin/sh running sed, so a red test means
* the transport is wrong, not an endpoint. What is checked: the prompt reaches
* the program's stdin, its stdout becomes the answer, a program that prints
* nothing is an error, and a non-zero exit carries the program's own words.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static int
one_turn(struct sp_host *host, const char *text, struct sp_reply *out,
struct sp_buf *err)
{
struct sp_transport *tr;
struct sp_conv conv;
tr = sp_host_transport(host, "cli");
CHECK(tr != NULL);
if (tr == NULL)
return -1;
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "user", NULL, text) == 0);
{
int rc = tr->send(tr->ctx, &conv, out, err);
sp_conv_free(&conv);
return rc;
}
}
int
main(void)
{
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host, *host2, *host3;
struct sp_buf err;
struct sp_reply rep;
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
if (sp_buf_init(&err) != 0)
return 1;
setenv("SPLINTER_CLI_CMD", "sed 's/^/X: /'", 1);
host = sp_host_new(&reg, &ag, NULL);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/cli/cli.so", &err) == 0);
CHECK(sp_host_transport(host, "cli") != NULL);
CHECK(sp_host_transport(host, "nope") == NULL);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(host, "hallo", &rep, &err) == 0);
CHECK(rep.status == 200);
CHECK(rep.content != NULL && strncmp(rep.content, "X: hallo", 8) == 0);
sp_reply_free(&rep);
/* a program that prints nothing must not look like an empty answer */
/* a fresh host: the transport name is taken once per host, on purpose */
setenv("SPLINTER_CLI_CMD", "true", 1);
host2 = sp_host_new(&reg, &ag, NULL);
CHECK(host2 != NULL);
CHECK(sp_plugin_load(host2, "plugins/cli/cli.so", &err) == 0);
sp_buf_clear(&err);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(host2, "hallo", &rep, &err) == -1);
CHECK(strstr(err.p, "no output") != NULL);
sp_reply_free(&rep);
/* a failing program says so, with its own output attached */
setenv("SPLINTER_CLI_CMD", "sh -c 'echo kaputt >&2; exit 3'", 1);
host3 = sp_host_new(&reg, &ag, NULL);
CHECK(host3 != NULL);
CHECK(sp_plugin_load(host3, "plugins/cli/cli.so", &err) == 0);
sp_buf_clear(&err);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(host3, "hallo", &rep, &err) == -1);
CHECK(strstr(err.p, "exit 3") != NULL);
CHECK(strstr(err.p, "kaputt") != NULL);
sp_reply_free(&rep);
sp_host_free(host3);
sp_host_free(host2);
sp_host_free(host);
sp_buf_free(&err);
sp_registry_free(&reg);
if (failures == 0)
printf("test_cli: ok\n");
return failures == 0 ? 0 : 1;
}