junction plugin, light/dark themes, goal completion
- plugins/junction: evaluates the X-Junction-Mcp header and /status; the sidebar widget, the "junction" status segment, junction_status and /junction show uptime, MCP-server health, queue and request counts. The url/header come from the plugin block, the mcp junction server or SPLINTER_JUNCTION_URL/TOKEN; the key is never printed. - theme: two flat themes. light is a white page with black text, dark the same inverted; chat, sidebar and input share the page, the status line is the one separate surface. Each CDE palette keeps its own rules. - curses: the sidebar uses the theme's own text on its own background (white/black) instead of white on a darkened surface; the one column between chat and sidebar is painted, so a white theme has no black seam. - focus: the goal goes into the first prompt (config, resume, planner), and every goal-carrying text asks the model to end with GOAL_COMPLETE. The marker is read from the conversation and the session log and recorded as goal_state=succeeded in the session meta; /goal and the sidebar say [done], reminders stop, a new goal opens a new one. - tests: test_junction (fake server, header identity, no colon in widget continuation lines, no key in the output), test_focus (announcement, completion in the compactor and from the tick, persisted state), test_theme (flat themes, list order). make check: all tests passed.
This commit is contained in:
1 parent
be3d0ee415
commit
614988aa68
20 files changed
+1563
-98
No files matched your search
@@ -44,7 +44,8 @@ PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
|
||||
plugins/planner/planner.so \
|
||||
plugins/a2a/a2a.so \
|
||||
plugins/sysinfo/sysinfo.so \
|
||||
plugins/tmux/tmux.so
|
||||
plugins/tmux/tmux.so \
|
||||
plugins/junction/junction.so
|
||||
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \
|
||||
tests/test_transport tests/test_agent tests/test_openai \
|
||||
tests/test_theme tests/test_plugin tests/test_memory tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_anthropic tests/test_cli tests/test_compactor tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_providers tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_planner tests/test_tmux tests/test_usage tests/test_agents tests/test_confblock tests/test_jobs tests/test_engine
|
||||
@@ -258,6 +259,8 @@ tests/test_confblock: tests/test_confblock.c $(LIB)
|
||||
|
||||
tests/test_engine: tests/test_engine.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_engine.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_junction: tests/test_junction.c $(LIB) plugins/junction/junction.so
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_junction.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_usage: tests/test_usage.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_usage.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_tmux: tests/test_tmux.c $(LIB) $(PLUGIN)
|
||||
@@ -289,7 +292,7 @@ tests/test_lifecycle: tests/test_lifecycle.c $(LIB) tests/plugin_lifecycle.so
|
||||
tests/test_plugin: tests/test_plugin.c $(LIB) $(PLUGIN)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_plugin.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
|
||||
check: $(LIB) $(BIN) $(CLIENT) $(PLUGIN) $(TESTS)
|
||||
check: $(LIB) $(BIN) $(CLIENT) $(PLUGIN) $(TESTS) tests/test_junction
|
||||
@set -e; python3 tools/check_i18n.py; \
|
||||
export LD_LIBRARY_PATH=.; \
|
||||
for t in $(TESTS); do echo "== $$t"; ./$$t; done; \
|
||||
@@ -310,6 +313,7 @@ check: $(LIB) $(BIN) $(CLIENT) $(PLUGIN) $(TESTS)
|
||||
echo "== tests/test_attach.sh"; sh tests/test_attach.sh; \
|
||||
echo "== tests/test_gateway_openai.sh"; sh tests/test_gateway_openai.sh; \
|
||||
echo "== tests/test_reconnect.sh"; sh tests/test_reconnect.sh; \
|
||||
echo "== tests/test_junction.sh"; sh tests/test_junction.sh; \
|
||||
echo "all tests passed"
|
||||
|
||||
# The installed binary finds its own library through an rpath relative to itself
|
||||
@@ -344,7 +348,8 @@ install: all
|
||||
|
||||
clean:
|
||||
rm -f src/backend/catalogs.c src/backend/buildinfo.c $(BIN) $(CLIENT) $(LIB) $(LIBOBJ_ALL) $(BINOBJ) \
|
||||
$(CLIENTOBJ) $(PLUGIN) $(TESTS) tests/plugin_lifecycle.so
|
||||
$(CLIENTOBJ) $(PLUGIN) $(TESTS) tests/plugin_lifecycle.so \
|
||||
tests/test_junction
|
||||
|
||||
.PHONY: all check curses install clean
|
||||
|
||||
@@ -369,3 +374,7 @@ plugins/a2a/a2a.so: plugins/a2a/a2a.c $(LIB)
|
||||
plugins/sysinfo/sysinfo.so: plugins/sysinfo/sysinfo.c $(LIB)
|
||||
$(CC) $(CFLAGS) -shared -o $@ plugins/sysinfo/sysinfo.c -L. -lsplinter \
|
||||
$(PLUGIN_RPATH) $(LIBS)
|
||||
|
||||
plugins/junction/junction.so: plugins/junction/junction.c $(LIB)
|
||||
$(CC) $(CFLAGS) -shared -o $@ plugins/junction/junction.c -L. -lsplinter \
|
||||
$(PLUGIN_RPATH) $(LIBS)
|
||||
@@ -171,7 +171,14 @@ The default theme is `Xresources`: it takes `*foreground`, `*background` and
|
||||
paints the UI with them, so the harness looks like the terminal it runs in.
|
||||
Without any of those resources it keeps the terminal's own colour indices.
|
||||
|
||||
Every CDE palette from the desktop's `.dp` files is a theme as well, 43 of them
|
||||
Two flat themes are the readable ones: `light` is a white page with black text,
|
||||
`dark` the same inverted. The chat, the sidebar and the input line share the
|
||||
page colour; only the status line above the prompt is a separate surface (light
|
||||
grey on `light`, dark grey on `dark`). That is the shape to reach for when a
|
||||
palette leaves the conversation unreadable: `theme = light` in the config or
|
||||
`--theme light`.
|
||||
|
||||
Every CDE palette from the desktop's `.dp` files is a theme as well, 42 of them
|
||||
including `Default` and `MakroSlate`. A palette is used the way the desktop
|
||||
uses it: set 5 is the chat background, set 4 the input area, set 1 the user's
|
||||
lines, set 7 the model's answer, set 3 its reasoning, set 6 tool calls, set 8
|
||||
|
||||
+20
-1
@@ -238,7 +238,7 @@ lies about what it does.
|
||||
| `transport` | a transport a plugin provides, e.g. `cli` |
|
||||
| `cli_cmd` | command for the `cli` transport, e.g. `claude -p` |
|
||||
| `plugin` | a plugin to load, one line each: a shipped plugin's name (`lcm`) or a path; in the block form `plugin NAME { }`, see "Plugins" |
|
||||
| `theme` | `Xresources` (the default: `*foreground`, `*background`, `*color0..15` from `xrdb`) or a CDE palette name (`splinter --list-themes`) |
|
||||
| `theme` | `Xresources` (the default: `*foreground`, `*background`, `*color0..15` from `xrdb`), `light`/`dark` (white or black page, black or white text), or a CDE palette name (`splinter --list-themes`) |
|
||||
| `locale` | language code (`de`, `en`); catalogues live in `share/locale` |
|
||||
| `autonomous` | `1` runs mutating/destructive tools without asking |
|
||||
| `steps` | tool rounds per turn (default 150) |
|
||||
@@ -339,6 +339,7 @@ plugin lcm {
|
||||
keep = 40
|
||||
}
|
||||
plugin tmux { }
|
||||
plugin junction { } # url/header from the mcp junction server
|
||||
plugin mine {
|
||||
path = ~/my/plugin.so # not shipped: say where it is
|
||||
foo = bar
|
||||
@@ -363,6 +364,24 @@ The old forms still load, so an existing file keeps working:
|
||||
`plugins = [plugins/lcm/lcm.so, ...]` in `core` (one path each) and the flat
|
||||
`plugin = /path/to/x.so`. They carry no plugin settings.
|
||||
|
||||
`plugin junction { }` reads the address and the key from the `mcp junction`
|
||||
server it already uses; its own block may override them:
|
||||
|
||||
```
|
||||
plugin junction {
|
||||
url = http://127.0.0.1:8090/mcp
|
||||
header = "Authorization: Bearer ${JUNCTION_TOKEN}"
|
||||
interval = 5000 # ms between checks, default 5000
|
||||
}
|
||||
```
|
||||
|
||||
It evaluates the `X-Junction-Mcp` response header (that is how junction
|
||||
identifies itself; a 200 without it is reported as "not junction") and reads
|
||||
uptime, MCP-server health, queue and request counts from `/status`. The sidebar
|
||||
widget, the `junction` status segment, `junction_status` and `/junction` all
|
||||
show that snapshot. Without either block, `SPLINTER_JUNCTION_URL` and
|
||||
`SPLINTER_JUNCTION_TOKEN` are read.
|
||||
|
||||
## MCP servers
|
||||
|
||||
An MCP server contributes tools. The servers are the configuration of the
|
||||
|
||||
+46
-1
@@ -227,14 +227,59 @@ stopped. That works in the TUI and in a one-shot run
|
||||
the conversation in front of you. `SPLINTER_FOCUS_EVERY` sets the interval
|
||||
(default 7 turns).
|
||||
|
||||
The goal is in the prompt from the first request, not from the seventh turn:
|
||||
when a goal exists and has not been announced in this session yet (config,
|
||||
resume, planner), the compactor inserts it once, before the newest user
|
||||
message.
|
||||
|
||||
A goal ends. Every text that carries the goal — the first announcement, the
|
||||
recap prompt, `/continue`, the periodic reminder — tells the model to end its
|
||||
final answer with the single line `GOAL_COMPLETE` when the goal is fully
|
||||
achieved. The plugin then records `goal_state=succeeded` beside the goal in the
|
||||
session `.meta` (it also watches the session log from its tick, so the state
|
||||
changes without waiting for the next turn), stops reminding, writes one
|
||||
`goal succeeded: …` note into the transcript, and shows `[done]` in the sidebar.
|
||||
Only an answer written after the goal went into a prompt can close it, so an
|
||||
old `GOAL_COMPLETE` cannot mark a goal that was set later. `/goal` reports the
|
||||
state; a new goal opens a new one.
|
||||
|
||||
It registers as a compactor and inserts one system message — *before* the newest
|
||||
user message, so the model reads recap then request and answers the request. If
|
||||
the model has not answered the recap prompt yet, the goal text stands in. Its
|
||||
widget line shows the goal and how many recaps went out.
|
||||
widget line shows the goal, `[done]` when it is reached, and the warning is
|
||||
never printed twice in one turn.
|
||||
|
||||
It is a reminder, not an instruction: the user can change direction at any
|
||||
time, and the message says so on its face.
|
||||
|
||||
## junction
|
||||
|
||||
The fleet router, evaluated from the outside. junction answers `/mcp` with
|
||||
|
||||
```
|
||||
X-Junction-Mcp: 1
|
||||
```
|
||||
|
||||
and that header is the identity: a 200 with the header is junction, a 200
|
||||
without it is "not junction, or a proxy in front of it" — a health check that
|
||||
trusts any answer is wrong exactly when it matters. The plugin checks `/mcp`
|
||||
for the header and reads `/status` for junction's own counters (uptime, how
|
||||
many MCP servers answer, queue, requests, endpoints). It does not count a
|
||||
second time.
|
||||
|
||||
```
|
||||
junction: 20m up mcp 5/5
|
||||
q 1/32 req 11 ok 0 fail ep 25
|
||||
hdr X-Junction-Mcp seen
|
||||
```
|
||||
|
||||
That is a widget, a status segment (`junction 20m up`), the tool
|
||||
`junction_status` and `/junction`. The address and the key come from the
|
||||
plugin's own block, else from the `mcp junction` server, else from
|
||||
`SPLINTER_JUNCTION_URL` / `SPLINTER_JUNCTION_TOKEN`. The header is never
|
||||
printed: it carries the key. `interval` (milliseconds, default 5000) sets how
|
||||
often the tick asks.
|
||||
|
||||
## sysinfo
|
||||
|
||||
RAM, CPU, swap and disk in one sidebar line:
|
||||
|
||||
@@ -255,6 +255,15 @@ int sp_session_set_goal(const char *dir, const char *id,
|
||||
const char *goal, struct sp_buf *err);
|
||||
int sp_session_get_goal(const char *dir, const char *id,
|
||||
char *buf, size_t cap);
|
||||
/*
|
||||
* "open" or "succeeded". A model that reached the goal answers GOAL_COMPLETE;
|
||||
* the state is what the sidebar and the status line show afterwards. Setting a
|
||||
* new goal resets it to open.
|
||||
*/
|
||||
int sp_session_set_goal_state(const char *dir, const char *id,
|
||||
const char *state, struct sp_buf *err);
|
||||
int sp_session_get_goal_state(const char *dir, const char *id,
|
||||
char *buf, size_t cap);
|
||||
/*
|
||||
* Remove a session: its log, meta, saved profile and the logs/<id> directory.
|
||||
* The caller must not delete the session its engine has open.
|
||||
|
||||
+338
-50
@@ -15,6 +15,17 @@
|
||||
* recap every seventh turn. There is no separate /recap command: recapping is
|
||||
* what a goal does.
|
||||
*
|
||||
* The goal is also the first thing the prompt carries. A session that starts
|
||||
* with a goal (config, resume, planner) gets one system message on its first
|
||||
* request, before the model has answered anything. A goal that is only
|
||||
* remembered internally is a goal the model never saw.
|
||||
*
|
||||
* A goal ends, and the model says so: the instructions ask it to end its final
|
||||
* answer with the single line GOAL_COMPLETE. The plugin looks for that line at
|
||||
* the end of the conversation and in the session log, records the state in the
|
||||
* session meta (sp_session_set_goal_state), and stops reminding. The sidebar
|
||||
* says [done] and /goal reports it; a new goal opens a new one.
|
||||
*
|
||||
* SPLINTER_FOCUS_GOAL text, exported from the config key `goal`
|
||||
* SPLINTER_FOCUS_EVERY turns between recaps, default 7
|
||||
*
|
||||
@@ -25,10 +36,13 @@
|
||||
* not appended after it: the model then reads goal -> request and answers the
|
||||
* request, instead of reading the request and treating the goal as a reply.
|
||||
*/
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
@@ -36,6 +50,8 @@
|
||||
|
||||
#define GOAL_MAX 1000
|
||||
#define RECAP_MAX 800
|
||||
#define GOAL_DONE "GOAL_COMPLETE"
|
||||
#define SESSION_TAIL (16 * 1024)
|
||||
|
||||
/*
|
||||
* goal, recap and reminders are shared by three threads: /goal runs on the UI
|
||||
@@ -46,10 +62,15 @@ static pthread_mutex_t focus_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static char goal[GOAL_MAX + 1];
|
||||
static char recap[RECAP_MAX + 1]; /* the model's short recap of the goal */
|
||||
static int recap_pending; /* goal set, waiting for that answer */
|
||||
static char goal_state[16] = "open"; /* open | succeeded */
|
||||
static unsigned announced; /* the goal is already in a prompt */
|
||||
static unsigned armed; /* a GOAL_COMPLETE may now be an answer */
|
||||
static size_t armed_assistants; /* assistant messages when it was armed */
|
||||
static struct timespec armed_at; /* and when, for the session file */
|
||||
static unsigned every = 7;
|
||||
static unsigned last_reminder;
|
||||
static unsigned reminders;
|
||||
static char widget_text[128];
|
||||
static char widget_text[192];
|
||||
static char session_id[64]; /* whose goal the statics belong to */
|
||||
static char env_goal[GOAL_MAX + 1]; /* SPLINTER_FOCUS_GOAL fallback */
|
||||
|
||||
@@ -105,77 +126,327 @@ recap_capture(struct sp_conv *conv)
|
||||
|
||||
/*
|
||||
* A plugin is not unloaded between sessions: the same focus statics serve the
|
||||
* next /open. Read the goal from the session meta whenever the session id
|
||||
* changes, so a resumed session and a switched session see their own goal.
|
||||
* The caller holds focus_lock.
|
||||
* next /open. Read the goal and its state from the session meta whenever the
|
||||
* session id changes, so a resumed session and a switched session see their
|
||||
* own goal. The caller holds focus_lock.
|
||||
*/
|
||||
static void
|
||||
focus_session_sync(struct sp_host *h)
|
||||
{
|
||||
const char *dir = sp_host_session_dir(h);
|
||||
const char *id = sp_host_session_id(h);
|
||||
char g[GOAL_MAX + 1];
|
||||
char g[GOAL_MAX + 1], st[16];
|
||||
|
||||
if (id == NULL || *id == '\0' || strcmp(session_id, id) == 0)
|
||||
return;
|
||||
snprintf(session_id, sizeof(session_id), "%s", id);
|
||||
goal[0] = '\0';
|
||||
recap[0] = '\0';
|
||||
snprintf(goal_state, sizeof(goal_state), "open");
|
||||
recap_pending = 0;
|
||||
announced = 0;
|
||||
armed = 0;
|
||||
reminders = 0;
|
||||
last_reminder = 0;
|
||||
if (dir != NULL && *dir != '\0' &&
|
||||
sp_session_get_goal(dir, id, g, sizeof(g)) == 0)
|
||||
sp_session_get_goal(dir, id, g, sizeof(g)) == 0) {
|
||||
snprintf(goal, sizeof(goal), "%s", g);
|
||||
else if (env_goal[0] != '\0')
|
||||
if (sp_session_get_goal_state(dir, id, st, sizeof(st)) == 0 &&
|
||||
strcmp(st, "succeeded") == 0)
|
||||
snprintf(goal_state, sizeof(goal_state), "%s", st);
|
||||
} else if (env_goal[0] != '\0')
|
||||
snprintf(goal, sizeof(goal), "%s", env_goal);
|
||||
}
|
||||
|
||||
/*
|
||||
* The model declared the goal reached. Persist that in the session (a resumed
|
||||
* session must know), stop reminding, and say it in the sidebar and the
|
||||
* transcript once.
|
||||
*/
|
||||
static void
|
||||
focus_publish(struct sp_host *h)
|
||||
{
|
||||
char line[sizeof(widget_text)];
|
||||
int done;
|
||||
|
||||
pthread_mutex_lock(&focus_lock);
|
||||
done = strcmp(goal_state, "succeeded") == 0;
|
||||
if (goal[0] == '\0')
|
||||
line[0] = '\0'; /* no goal: the sidebar says nothing */
|
||||
else
|
||||
snprintf(line, sizeof(line), "%s%.160s%s",
|
||||
done ? "[done] " : "", goal,
|
||||
strlen(goal) > 160 ? "..." : "");
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
(void)sp_host_widget_text(h, "focus", line);
|
||||
}
|
||||
|
||||
static void
|
||||
focus_mark_succeeded(struct sp_host *h)
|
||||
{
|
||||
const char *dir = sp_host_session_dir(h);
|
||||
const char *id = sp_host_session_id(h);
|
||||
char g[GOAL_MAX + 1];
|
||||
int changed = 0;
|
||||
|
||||
pthread_mutex_lock(&focus_lock);
|
||||
if (goal[0] != '\0' && strcmp(goal_state, "succeeded") != 0) {
|
||||
snprintf(goal_state, sizeof(goal_state), "succeeded");
|
||||
snprintf(g, sizeof(g), "%s", goal);
|
||||
changed = 1;
|
||||
}
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
if (!changed)
|
||||
return;
|
||||
if (dir != NULL && *dir != '\0' && id != NULL && *id != '\0') {
|
||||
struct sp_buf serr;
|
||||
|
||||
if (sp_buf_init(&serr) == 0) {
|
||||
(void)sp_session_set_goal_state(dir, id, "succeeded",
|
||||
&serr);
|
||||
sp_buf_free(&serr);
|
||||
}
|
||||
}
|
||||
focus_publish(h);
|
||||
{
|
||||
char msg[GOAL_MAX + 128];
|
||||
|
||||
snprintf(msg, sizeof(msg), sp_td("focus.succeeded",
|
||||
"goal succeeded: %s"), g);
|
||||
(void)sp_host_status_msg(h, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The newest assistant answer, and only that: a marker in an old turn does not
|
||||
* close a goal that was set after it. Tool rounds are skipped, they carry no
|
||||
* final text - and `after` is how many answers existed when the goal went into
|
||||
* the prompt, so an answer from before that point does not count.
|
||||
*/
|
||||
static size_t
|
||||
count_assistants(const struct sp_conv *conv)
|
||||
{
|
||||
size_t i, n = 0;
|
||||
|
||||
for (i = 0; conv != NULL && i < conv->n; i++)
|
||||
if (conv->v[i].role != NULL &&
|
||||
strcmp(conv->v[i].role, "assistant") == 0 &&
|
||||
conv->v[i].content != NULL && conv->v[i].content[0] != '\0')
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
static int
|
||||
conv_goal_complete(const struct sp_conv *conv, size_t after)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (conv == NULL || count_assistants(conv) <= after)
|
||||
return 0;
|
||||
for (i = conv->n; i > 0; i--) {
|
||||
const struct sp_msg *m = &conv->v[i - 1];
|
||||
|
||||
if (m->role == NULL || strcmp(m->role, "assistant") != 0)
|
||||
continue;
|
||||
if (m->content == NULL || m->content[0] == '\0')
|
||||
continue;
|
||||
if (m->calls != NULL && m->calls[0] != '\0')
|
||||
continue;
|
||||
return strstr(m->content, GOAL_DONE) != NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The tick has no conversation in hand; it reads the session log. Only the tail
|
||||
* is read: the answer that closed the goal is the newest thing in the file, and
|
||||
* a session that has grown to megabytes must not be read every second.
|
||||
*/
|
||||
static int
|
||||
session_goal_complete(struct sp_host *h)
|
||||
{
|
||||
const char *dir = sp_host_session_dir(h);
|
||||
const char *id = sp_host_session_id(h);
|
||||
char path[800], tail[SESSION_TAIL + 1];
|
||||
struct stat st;
|
||||
off_t size, off;
|
||||
ssize_t n;
|
||||
int fd;
|
||||
char *line, *save = NULL, *last = NULL;
|
||||
|
||||
if (dir == NULL || *dir == '\0' || id == NULL || *id == '\0')
|
||||
return 0;
|
||||
snprintf(path, sizeof(path), "%s/%s.jsonl", dir, id);
|
||||
/*
|
||||
* A file that has not been written since the goal was armed holds the
|
||||
* answer to an older goal, whatever its last line says.
|
||||
*/
|
||||
if (stat(path, &st) != 0)
|
||||
return 0;
|
||||
if (st.st_mtim.tv_sec < armed_at.tv_sec ||
|
||||
(st.st_mtim.tv_sec == armed_at.tv_sec &&
|
||||
st.st_mtim.tv_nsec <= armed_at.tv_nsec))
|
||||
return 0;
|
||||
fd = open(path, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return 0;
|
||||
size = lseek(fd, 0, SEEK_END);
|
||||
if (size <= 0) {
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
off = size > (off_t)SESSION_TAIL ? size - (off_t)SESSION_TAIL : 0;
|
||||
if (lseek(fd, off, SEEK_SET) < 0) {
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
n = read(fd, tail, sizeof(tail) - 1);
|
||||
close(fd);
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
tail[n] = '\0';
|
||||
/*
|
||||
* Only the newest final answer counts: an old GOAL_COMPLETE belongs to
|
||||
* the goal that was open then, and a new goal has been set since.
|
||||
*/
|
||||
for (line = strtok_r(tail, "\n", &save); line != NULL;
|
||||
line = strtok_r(NULL, "\n", &save)) {
|
||||
if (strstr(line, "\"role\":\"assistant\"") != NULL)
|
||||
last = line;
|
||||
}
|
||||
return last != NULL && strstr(last, GOAL_DONE) != NULL;
|
||||
}
|
||||
|
||||
/* a system message right before the newest user message, never after it */
|
||||
static int
|
||||
insert_before_user(struct sp_conv *conv, const char *text)
|
||||
{
|
||||
struct sp_msg m;
|
||||
size_t at;
|
||||
|
||||
if (conv == NULL || conv->n == 0)
|
||||
return -1;
|
||||
memset(&m, 0, sizeof(m));
|
||||
m.role = "system";
|
||||
m.content = (char *)text;
|
||||
at = conv->n - 1;
|
||||
while (at > 0 && conv->v[at].role != NULL &&
|
||||
strcmp(conv->v[at].role, "user") != 0)
|
||||
at--;
|
||||
return sp_conv_insert(conv, at, &m);
|
||||
}
|
||||
|
||||
/*
|
||||
* The goal is in a prompt now. From here on an answer may close it - but only
|
||||
* one written after this point: the count of answers and the log's mtime are
|
||||
* the barrier that keeps an older GOAL_COMPLETE out.
|
||||
*/
|
||||
static void
|
||||
focus_arm(const struct sp_conv *conv)
|
||||
{
|
||||
armed = 1;
|
||||
armed_assistants = count_assistants(conv);
|
||||
(void)clock_gettime(CLOCK_REALTIME, &armed_at);
|
||||
}
|
||||
|
||||
/* /goal's own prompt already carries the goal: do not say it twice */
|
||||
static int
|
||||
newest_user_is_goal_prompt(const struct sp_conv *conv)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (conv == NULL)
|
||||
return 0;
|
||||
for (i = conv->n; i > 0; i--) {
|
||||
const struct sp_msg *m = &conv->v[i - 1];
|
||||
|
||||
if (m->role != NULL && strcmp(m->role, "user") == 0)
|
||||
return m->content != NULL &&
|
||||
strncmp(m->content, "Goal: ", 6) == 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
focus_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
||||
unsigned turn)
|
||||
{
|
||||
char text[GOAL_MAX + 512];
|
||||
struct sp_msg m;
|
||||
size_t at;
|
||||
|
||||
char r[GOAL_MAX + 1];
|
||||
char text[GOAL_MAX + 800];
|
||||
char rem[GOAL_MAX + 768];
|
||||
char r[GOAL_MAX + 1];
|
||||
int complete;
|
||||
|
||||
(void)budget;
|
||||
(void)h;
|
||||
if (conv == NULL || conv->n == 0)
|
||||
return 0;
|
||||
pthread_mutex_lock(&focus_lock);
|
||||
focus_session_sync(h);
|
||||
recap_capture(conv);
|
||||
if (goal[0] == '\0' || turn == 0 || (turn % every) != 0 ||
|
||||
turn == last_reminder) { /* one recap per turn, always */
|
||||
/*
|
||||
* Only an answer to a prompt that carried this goal can close it: armed
|
||||
* is set when the goal goes into the request, so an old GOAL_COMPLETE
|
||||
* lying in the conversation cannot close a goal that was set after it.
|
||||
*/
|
||||
complete = armed && goal[0] != '\0' &&
|
||||
strcmp(goal_state, "succeeded") != 0 &&
|
||||
conv_goal_complete(conv, armed_assistants);
|
||||
if (complete) {
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
focus_mark_succeeded(h);
|
||||
return 0;
|
||||
}
|
||||
if (goal[0] == '\0' || strcmp(goal_state, "succeeded") == 0 ||
|
||||
turn == 0 || turn == last_reminder) { /* never twice in a turn */
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The first request carries the goal itself. Recap first, goal text
|
||||
* until a recap exists - the same rule the periodic reminder follows.
|
||||
*/
|
||||
if (!announced) {
|
||||
if (newest_user_is_goal_prompt(conv)) {
|
||||
/* /goal or /continue already wrote it into the prompt */
|
||||
announced = 1;
|
||||
focus_arm(conv);
|
||||
last_reminder = turn;
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
focus_publish(h);
|
||||
return 0;
|
||||
}
|
||||
snprintf(r, sizeof(r), "%s", recap[0] != '\0' ? recap : goal);
|
||||
announced = 1;
|
||||
focus_arm(conv);
|
||||
last_reminder = turn;
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
snprintf(rem, sizeof(rem), sp_td("focus.goal_prompt",
|
||||
"Goal: %s. Work toward it. When the goal is fully "
|
||||
"achieved, end your final answer with the single "
|
||||
"line GOAL_COMPLETE so splinter can mark it as "
|
||||
"succeeded."), r);
|
||||
snprintf(text, sizeof(text), "[focus] %s", rem);
|
||||
if (insert_before_user(conv, text) != 0)
|
||||
return -1;
|
||||
focus_publish(h);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((turn % every) != 0) {
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
return 0;
|
||||
}
|
||||
snprintf(r, sizeof(r), "%s", recap[0] != '\0' ? recap : goal);
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
|
||||
{
|
||||
char rem[GOAL_MAX + 256];
|
||||
|
||||
snprintf(rem, sizeof(rem), sp_td("focus.reminder",
|
||||
"Recap to remember: %s. Before answering, check whether the "
|
||||
"last step moved closer to the goal; if not, say so and name "
|
||||
"the next step."), r);
|
||||
snprintf(text, sizeof(text), "[focus] %s", rem);
|
||||
}
|
||||
|
||||
memset(&m, 0, sizeof(m));
|
||||
m.role = "system";
|
||||
m.content = text;
|
||||
|
||||
/* before the newest message, so the goal is read first */
|
||||
at = conv->n - 1;
|
||||
while (at > 0 && conv->v[at].role != NULL &&
|
||||
strcmp(conv->v[at].role, "user") != 0)
|
||||
at--;
|
||||
if (sp_conv_insert(conv, at, &m) != 0)
|
||||
snprintf(rem, sizeof(rem), sp_td("focus.reminder",
|
||||
"Recap to remember: %s. Before answering, check whether "
|
||||
"the last step moved closer to the goal; if not, say so "
|
||||
"and name the next step. When the goal is fully achieved, "
|
||||
"end your final answer with the single line "
|
||||
"GOAL_COMPLETE."), r);
|
||||
snprintf(text, sizeof(text), "[focus] %s", rem);
|
||||
if (insert_before_user(conv, text) != 0)
|
||||
return -1;
|
||||
|
||||
pthread_mutex_lock(&focus_lock);
|
||||
@@ -184,6 +455,7 @@ focus_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
||||
snprintf(widget_text, sizeof(widget_text), "%u reminders, every %u turns",
|
||||
reminders, every);
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
focus_publish(h);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -197,6 +469,7 @@ focus_command(struct sp_host *h, const char *args, struct sp_buf *out,
|
||||
{
|
||||
char g[GOAL_MAX + 1], saveerr[256] = "";
|
||||
unsigned n;
|
||||
int done;
|
||||
|
||||
(void)ctx;
|
||||
pthread_mutex_lock(&focus_lock);
|
||||
@@ -204,10 +477,13 @@ focus_command(struct sp_host *h, const char *args, struct sp_buf *out,
|
||||
if (args == NULL || *args == '\0') {
|
||||
snprintf(g, sizeof(g), "%s", goal);
|
||||
n = reminders;
|
||||
done = strcmp(goal_state, "succeeded") == 0;
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
if (g[0] == '\0')
|
||||
return sp_buf_puts(out, sp_td("focus.none", "no goal set (/goal <text>)"));
|
||||
return sp_buf_printf(out, sp_td("focus.status",
|
||||
return sp_buf_printf(out, sp_td(done ? "focus.status_done" :
|
||||
"focus.status", done ?
|
||||
"goal: %s [succeeded] (%u recaps, every %u turns)" :
|
||||
"goal: %s (%u recaps, every %u turns)"), g, n, every);
|
||||
}
|
||||
snprintf(goal, sizeof(goal), "%.*s", GOAL_MAX, args);
|
||||
@@ -215,6 +491,9 @@ focus_command(struct sp_host *h, const char *args, struct sp_buf *out,
|
||||
snprintf(goal + GOAL_MAX - 24, 25, "...[gekuerzt]");
|
||||
recap[0] = '\0';
|
||||
recap_pending = 1;
|
||||
snprintf(goal_state, sizeof(goal_state), "open");
|
||||
announced = 0;
|
||||
armed = 0;
|
||||
reminders = 0;
|
||||
last_reminder = 0;
|
||||
snprintf(g, sizeof(g), "%s", goal);
|
||||
@@ -236,13 +515,7 @@ focus_command(struct sp_host *h, const char *args, struct sp_buf *out,
|
||||
}
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
/* publish now, so the sidebar does not wait for the next tick */
|
||||
{
|
||||
char line[192];
|
||||
|
||||
snprintf(line, sizeof(line), "%.160s%s", g,
|
||||
strlen(g) > 160 ? "..." : "");
|
||||
(void)sp_host_widget_text(h, "focus", line);
|
||||
}
|
||||
focus_publish(h);
|
||||
(void)sp_buf_printf(out, sp_td("focus.set", "goal set: %s"), g);
|
||||
if (saveerr[0] != '\0')
|
||||
sp_buf_printf(out, " (%s)", saveerr);
|
||||
@@ -252,17 +525,29 @@ focus_command(struct sp_host *h, const char *args, struct sp_buf *out,
|
||||
static int
|
||||
focus_tick(struct sp_host *h, const char *name, void *ctx)
|
||||
{
|
||||
char line[192];
|
||||
char line[sizeof(widget_text)];
|
||||
int check = 0;
|
||||
|
||||
(void)ctx;
|
||||
pthread_mutex_lock(&focus_lock);
|
||||
focus_session_sync(h);
|
||||
if (goal[0] == '\0')
|
||||
line[0] = '\0'; /* no goal: the sidebar says nothing */
|
||||
else
|
||||
snprintf(line, sizeof(line), "%.160s%s",
|
||||
goal, strlen(goal) > 160 ? "..." : "");
|
||||
check = armed && goal[0] != '\0' &&
|
||||
strcmp(goal_state, "succeeded") != 0 &&
|
||||
session_goal_complete(h);
|
||||
if (!check) {
|
||||
if (goal[0] == '\0')
|
||||
line[0] = '\0'; /* no goal: the sidebar says nothing */
|
||||
else
|
||||
snprintf(line, sizeof(line), "%s%.160s%s",
|
||||
strcmp(goal_state, "succeeded") == 0 ?
|
||||
"[done] " : "", goal,
|
||||
strlen(goal) > 160 ? "..." : "");
|
||||
}
|
||||
pthread_mutex_unlock(&focus_lock);
|
||||
if (check) {
|
||||
focus_mark_succeeded(h);
|
||||
return 0;
|
||||
}
|
||||
return sp_host_widget_text(h, name, line);
|
||||
}
|
||||
|
||||
@@ -280,6 +565,9 @@ focus_init(struct sp_host *h)
|
||||
memset(env_goal, 0, sizeof(env_goal));
|
||||
session_id[0] = '\0';
|
||||
recap_pending = 0;
|
||||
snprintf(goal_state, sizeof(goal_state), "open");
|
||||
announced = 0;
|
||||
armed = 0;
|
||||
if (g != NULL) {
|
||||
snprintf(env_goal, sizeof(env_goal), "%.*s", GOAL_MAX, g);
|
||||
if (strlen(g) > GOAL_MAX)
|
||||
@@ -300,7 +588,7 @@ focus_init(struct sp_host *h)
|
||||
}
|
||||
|
||||
static const struct sp_plugin focus_plugin = {
|
||||
SP_PLUGIN_ABI, "focus", "1.0.0", focus_init, NULL, NULL
|
||||
SP_PLUGIN_ABI, "focus", "1.1.0", focus_init, NULL, NULL
|
||||
};
|
||||
|
||||
const struct sp_plugin *
|
||||
|
||||
@@ -0,0 +1,549 @@
|
||||
/*
|
||||
* splinter -- junction: is the fleet router there, and what does it say?
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* junction is the one endpoint that holds the fleet's tools (tasksd, skilld,
|
||||
* memoryd, wikid, koder) and routes the models. This plugin keeps its health
|
||||
* on screen: a sidebar widget, a status segment, a tool and /junction.
|
||||
*
|
||||
* The header is the identity. junction answers with
|
||||
*
|
||||
* X-Junction-Mcp: 1
|
||||
*
|
||||
* on /mcp, and that is how this plugin tells junction from a proxy that happens
|
||||
* to return JSON on the same port. A 200 with no such header is reported as
|
||||
* "not junction", not as "up" - a health check that trusts any answer is a
|
||||
* health check that is wrong exactly when it matters.
|
||||
*
|
||||
* The numbers come from /status (uptime, the MCP servers it aggregates, the
|
||||
* queue, request counts, endpoints). They are junction's own counters; this
|
||||
* plugin does not count requests a second time.
|
||||
*
|
||||
* Configuration, in the plugin's own block or the MCP server it already has:
|
||||
*
|
||||
* plugin junction {
|
||||
* url = http://127.0.0.1:8090/mcp
|
||||
* header = "Authorization: Bearer ${JUNCTION_TOKEN}"
|
||||
* interval = 5000 # ms, default 5000
|
||||
* }
|
||||
*
|
||||
* Without a block the `mcp junction { url = ... header = ... }` server is used,
|
||||
* so there is one place for the address and the key. Without either,
|
||||
* SPLINTER_JUNCTION_URL / SPLINTER_JUNCTION_TOKEN are read. The header is never
|
||||
* printed: it carries the key.
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
#include "config.h"
|
||||
#include "curl.h"
|
||||
#include "mcp.h"
|
||||
|
||||
#define J_URL_MAX 512
|
||||
#define J_HDR_MAX 512
|
||||
#define J_DEFAULT_MS 5000
|
||||
#define J_TIMEOUT_S "4"
|
||||
|
||||
/* Everything the tick thread writes and the UI thread reads lives here. */
|
||||
static struct {
|
||||
pthread_mutex_t lock;
|
||||
char url[J_URL_MAX];
|
||||
char header[J_HDR_MAX];
|
||||
unsigned interval_ms;
|
||||
|
||||
char widget[1024]; /* the sidebar block, no colon in line 2+ */
|
||||
char status[160]; /* the status segment */
|
||||
int identity; /* X-Junction-Mcp was seen */
|
||||
int body_ok; /* /status answered JSON */
|
||||
long http_mcp; /* HTTP status of GET /mcp */
|
||||
long http_status; /* HTTP status of GET /status */
|
||||
unsigned long uptime_s;
|
||||
unsigned mcp_ok, mcp_total;
|
||||
unsigned queue_inflight, queue_max;
|
||||
unsigned req_ok, req_fail;
|
||||
unsigned endpoints;
|
||||
time_t checked;
|
||||
} j = { .lock = PTHREAD_MUTEX_INITIALIZER };
|
||||
|
||||
/* "1d4h", "2h07m", "20m", "41s" */
|
||||
static void
|
||||
human_duration(unsigned long s, char *buf, size_t len)
|
||||
{
|
||||
if (s >= 86400)
|
||||
snprintf(buf, len, "%lud%02luh", s / 86400,
|
||||
(s % 86400) / 3600);
|
||||
else if (s >= 3600)
|
||||
snprintf(buf, len, "%luh%02lum", s / 3600, (s % 3600) / 60);
|
||||
else if (s >= 60)
|
||||
snprintf(buf, len, "%lum", s / 60);
|
||||
else
|
||||
snprintf(buf, len, "%lus", s);
|
||||
}
|
||||
|
||||
/* the address without its /mcp tail: /status and /mcp are two routes on it */
|
||||
static void
|
||||
base_url(char *buf, size_t len)
|
||||
{
|
||||
size_t n;
|
||||
|
||||
pthread_mutex_lock(&j.lock);
|
||||
snprintf(buf, len, "%s", j.url);
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
n = strlen(buf);
|
||||
while (n > 0 && buf[n - 1] == '/')
|
||||
buf[--n] = '\0';
|
||||
if (n > 4 && strcmp(buf + n - 4, "/mcp") == 0)
|
||||
buf[n - 4] = '\0';
|
||||
}
|
||||
|
||||
/* "HTTP/1.1 405 Method Not Allowed" -> 405 */
|
||||
static long
|
||||
header_status(const char *headers)
|
||||
{
|
||||
const char *sp;
|
||||
|
||||
if (headers == NULL || strncmp(headers, "HTTP/", 5) != 0)
|
||||
return 0;
|
||||
sp = strchr(headers, ' ');
|
||||
return sp != NULL ? strtol(sp + 1, NULL, 10) : 0;
|
||||
}
|
||||
|
||||
/* one response header, case-blind; 1 when it is there */
|
||||
static int
|
||||
header_value(const char *headers, const char *name, char *out, size_t cap)
|
||||
{
|
||||
const char *p = headers;
|
||||
size_t n = strlen(name);
|
||||
|
||||
while (p != NULL && *p != '\0') {
|
||||
const char *nl = strchr(p, '\n');
|
||||
size_t len = nl != NULL ? (size_t)(nl - p) : strlen(p);
|
||||
|
||||
if (len > n && strncasecmp(p, name, n) == 0 && p[n] == ':') {
|
||||
const char *v = p + n + 1;
|
||||
size_t vl;
|
||||
|
||||
while (*v == ' ' || *v == '\t')
|
||||
v++;
|
||||
vl = len - (size_t)(v - p);
|
||||
while (vl > 0 && (v[vl - 1] == '\r' || v[vl - 1] == ' ' ||
|
||||
v[vl - 1] == '\t'))
|
||||
vl--;
|
||||
snprintf(out, cap, "%.*s", (int)vl, v);
|
||||
return 1;
|
||||
}
|
||||
p = nl != NULL ? nl + 1 : NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* One GET. headers_only returns the response headers instead of the body;
|
||||
* otherwise the body is returned and the HTTP status is appended by curl's -w
|
||||
* on its own last line, the same shape the MCP client uses.
|
||||
*/
|
||||
static char *
|
||||
junction_get(const char *url, int headers_only, long *http)
|
||||
{
|
||||
static const char *const args_headers[] = {
|
||||
"-s", "-S", "-m", J_TIMEOUT_S, "-o", "/dev/null", "-D", "-",
|
||||
NULL
|
||||
};
|
||||
static const char *const args_body[] = {
|
||||
"-s", "-S", "-m", J_TIMEOUT_S, "-w", "\n%{http_code}", NULL
|
||||
};
|
||||
const char *hdrs[2];
|
||||
char hdr[J_HDR_MAX];
|
||||
struct sp_buf out, err;
|
||||
char chunk[4096];
|
||||
ssize_t got;
|
||||
pid_t pid;
|
||||
int fd, st = 0;
|
||||
char *ret;
|
||||
|
||||
*http = 0;
|
||||
if (sp_buf_init(&out) != 0)
|
||||
return NULL;
|
||||
(void)sp_buf_init(&err);
|
||||
pthread_mutex_lock(&j.lock);
|
||||
snprintf(hdr, sizeof(hdr), "%s", j.header);
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
hdrs[0] = hdr[0] != '\0' ? hdr : NULL;
|
||||
hdrs[1] = NULL;
|
||||
|
||||
fd = sp_curl_start(headers_only ? args_headers : args_body, url, hdrs,
|
||||
1, &pid, &err);
|
||||
if (fd < 0) {
|
||||
sp_buf_free(&err);
|
||||
sp_buf_free(&out);
|
||||
return NULL;
|
||||
}
|
||||
while ((got = read(fd, chunk, sizeof(chunk))) > 0) {
|
||||
if (sp_buf_add(&out, chunk, (size_t)got) != 0)
|
||||
break;
|
||||
}
|
||||
close(fd);
|
||||
(void)waitpid(pid, &st, 0);
|
||||
if (!WIFEXITED(st) || WEXITSTATUS(st) != 0 || out.p == NULL) {
|
||||
sp_buf_free(&err);
|
||||
sp_buf_free(&out);
|
||||
return NULL;
|
||||
}
|
||||
if (headers_only) {
|
||||
*http = header_status(out.p);
|
||||
} else {
|
||||
char *nl = strrchr(out.p, '\n');
|
||||
|
||||
if (nl != NULL) {
|
||||
*http = strtol(nl + 1, NULL, 10);
|
||||
*nl = '\0';
|
||||
out.len = (size_t)(nl - out.p);
|
||||
}
|
||||
}
|
||||
sp_buf_free(&err);
|
||||
ret = out.p != NULL ? out.p : calloc(1, 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* junction's /status: the counters it publishes, read once per tick */
|
||||
static void
|
||||
read_status(const char *body)
|
||||
{
|
||||
mcp_json *root = mcp_parse(body);
|
||||
const mcp_json *m;
|
||||
unsigned ok = 0, total = 0;
|
||||
size_t i;
|
||||
|
||||
if (root == NULL)
|
||||
return;
|
||||
pthread_mutex_lock(&j.lock);
|
||||
j.uptime_s = (unsigned long)mcp_arg_int(root, "uptime_s", 0);
|
||||
j.req_ok = j.req_fail = 0;
|
||||
j.endpoints = 0;
|
||||
j.mcp_ok = j.mcp_total = 0;
|
||||
j.queue_inflight = j.queue_max = 0;
|
||||
|
||||
m = mcp_arg(root, "requests");
|
||||
if (m != NULL) {
|
||||
j.req_ok = (unsigned)mcp_arg_int(m, "ok", 0);
|
||||
j.req_fail = (unsigned)mcp_arg_int(m, "fail", 0);
|
||||
}
|
||||
m = mcp_arg(root, "queue");
|
||||
if (m != NULL) {
|
||||
j.queue_inflight = (unsigned)mcp_arg_int(m, "inflight", 0);
|
||||
j.queue_max = (unsigned)mcp_arg_int(m, "max_inflight", 0);
|
||||
}
|
||||
m = mcp_arg(root, "mcp");
|
||||
if (m != NULL) {
|
||||
total = (unsigned)mcp_array_len(m);
|
||||
for (i = 0; i < total; i++) {
|
||||
const mcp_json *e = mcp_array_at(m, i);
|
||||
|
||||
if (e != NULL && mcp_arg_int(e, "ok", 0) == 1)
|
||||
ok++;
|
||||
}
|
||||
}
|
||||
m = mcp_arg(root, "endpoints");
|
||||
if (m != NULL)
|
||||
j.endpoints = (unsigned)mcp_array_len(m);
|
||||
j.mcp_ok = ok;
|
||||
j.mcp_total = total;
|
||||
j.body_ok = 1;
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
mcp_json_free(root);
|
||||
}
|
||||
|
||||
/* the text the sidebar, the status segment and /junction show; lock held */
|
||||
static void
|
||||
compose(void)
|
||||
{
|
||||
char d[32];
|
||||
|
||||
human_duration(j.uptime_s, d, sizeof(d));
|
||||
if (!j.identity) {
|
||||
/*
|
||||
* Continuation lines carry no colon: the sidebar reads a line
|
||||
* with one as a new widget, not as part of this block.
|
||||
*/
|
||||
snprintf(j.widget, sizeof(j.widget),
|
||||
"no X-Junction-Mcp header\n"
|
||||
"mcp http %ld status http %ld\n"
|
||||
"not junction, or a proxy in front of it",
|
||||
j.http_mcp, j.http_status);
|
||||
snprintf(j.status, sizeof(j.status), "junction not recognised");
|
||||
return;
|
||||
}
|
||||
if (!j.body_ok) {
|
||||
snprintf(j.widget, sizeof(j.widget),
|
||||
"identified, but /status did not answer\n"
|
||||
"mcp http %ld status http %ld",
|
||||
j.http_mcp, j.http_status);
|
||||
snprintf(j.status, sizeof(j.status), "junction up, status http %ld",
|
||||
j.http_status);
|
||||
return;
|
||||
}
|
||||
snprintf(j.widget, sizeof(j.widget),
|
||||
"%s up mcp %u/%u\n"
|
||||
"q %u/%u req %u ok %u fail ep %u\n"
|
||||
"hdr X-Junction-Mcp %s",
|
||||
d, j.mcp_ok, j.mcp_total, j.queue_inflight, j.queue_max,
|
||||
j.req_ok, j.req_fail, j.endpoints, j.identity ? "seen" : "missing");
|
||||
if (j.mcp_total > 0 && j.mcp_ok < j.mcp_total)
|
||||
snprintf(j.status, sizeof(j.status), "junction %s up, mcp %u/%u",
|
||||
d, j.mcp_ok, j.mcp_total);
|
||||
else
|
||||
snprintf(j.status, sizeof(j.status), "junction %s up", d);
|
||||
}
|
||||
|
||||
static void
|
||||
probe(void)
|
||||
{
|
||||
char base[J_URL_MAX], url[J_URL_MAX + 32];
|
||||
char hval[64] = "";
|
||||
char *headers = NULL, *body = NULL;
|
||||
long http_mcp = 0, http_status = 0;
|
||||
int identity = 0, body_ok = 0;
|
||||
|
||||
base_url(base, sizeof(base));
|
||||
if (base[0] == '\0') {
|
||||
pthread_mutex_lock(&j.lock);
|
||||
snprintf(j.widget, sizeof(j.widget),
|
||||
"no url configured\n"
|
||||
"plugin junction { url = ... } or mcp junction { }");
|
||||
snprintf(j.status, sizeof(j.status), "junction not configured");
|
||||
j.identity = 0;
|
||||
j.body_ok = 0;
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
return;
|
||||
}
|
||||
snprintf(url, sizeof(url), "%s/mcp", base);
|
||||
headers = junction_get(url, 1, &http_mcp);
|
||||
if (headers != NULL) {
|
||||
if (header_value(headers, "X-Junction-Mcp", hval, sizeof(hval)))
|
||||
identity = 1;
|
||||
free(headers);
|
||||
}
|
||||
snprintf(url, sizeof(url), "%s/status", base);
|
||||
body = junction_get(url, 0, &http_status);
|
||||
if (body != NULL) {
|
||||
read_status(body);
|
||||
body_ok = 1;
|
||||
free(body);
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&j.lock);
|
||||
j.http_mcp = http_mcp;
|
||||
j.http_status = http_status;
|
||||
j.identity = identity;
|
||||
j.body_ok = body_ok;
|
||||
j.checked = time(NULL);
|
||||
compose();
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
}
|
||||
|
||||
/* a text for the tool and the command: every field, the URL, never the key */
|
||||
static void
|
||||
snapshot_text(struct sp_buf *out)
|
||||
{
|
||||
char d[32], url[J_URL_MAX], hdr[8];
|
||||
int identity, body_ok;
|
||||
long hm, hs;
|
||||
unsigned ok, total, inflight, qmax, rok, rfail, eps;
|
||||
|
||||
pthread_mutex_lock(&j.lock);
|
||||
snprintf(url, sizeof(url), "%s", j.url);
|
||||
identity = j.identity;
|
||||
body_ok = j.body_ok;
|
||||
hm = j.http_mcp;
|
||||
hs = j.http_status;
|
||||
ok = j.mcp_ok;
|
||||
total = j.mcp_total;
|
||||
inflight = j.queue_inflight;
|
||||
qmax = j.queue_max;
|
||||
rok = j.req_ok;
|
||||
rfail = j.req_fail;
|
||||
eps = j.endpoints;
|
||||
snprintf(hdr, sizeof(hdr), "%s", j.header[0] != '\0' ? "yes" : "no");
|
||||
human_duration(j.uptime_s, d, sizeof(d));
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
|
||||
sp_buf_printf(out, "junction: %s\n", identity && body_ok ? "up" :
|
||||
identity ? "identified, status unreadable" : "not recognised");
|
||||
sp_buf_printf(out, " url: %s\n", url[0] != '\0' ? url : "(none)");
|
||||
sp_buf_printf(out, " header: %s (X-Junction-Mcp %s)\n", hdr,
|
||||
identity ? "seen" : "missing");
|
||||
sp_buf_printf(out, " http: /mcp %ld, /status %ld\n", hm, hs);
|
||||
sp_buf_printf(out, " uptime: %s\n", d);
|
||||
sp_buf_printf(out, " mcp: %u/%u answering\n", ok, total);
|
||||
sp_buf_printf(out, " queue: %u/%u\n", inflight, qmax);
|
||||
sp_buf_printf(out, " requests: %u ok, %u fail endpoints: %u\n",
|
||||
rok, rfail, eps);
|
||||
}
|
||||
|
||||
static void
|
||||
junction_status(struct sp_host *h, char *buf, size_t len)
|
||||
{
|
||||
(void)h;
|
||||
pthread_mutex_lock(&j.lock);
|
||||
snprintf(buf, len, "%s", j.status[0] != '\0' ? j.status : "junction ?");
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
}
|
||||
|
||||
static int
|
||||
junction_tick(struct sp_host *h, const char *name, void *ctx)
|
||||
{
|
||||
char text[sizeof(j.widget)];
|
||||
|
||||
(void)ctx;
|
||||
probe();
|
||||
pthread_mutex_lock(&j.lock);
|
||||
snprintf(text, sizeof(text), "%s", j.widget);
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
return sp_host_widget_text(h, name, text);
|
||||
}
|
||||
|
||||
static int
|
||||
junction_tool(const char *args_json, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
(void)args_json;
|
||||
(void)ctx;
|
||||
snapshot_text(out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
junction_command(struct sp_host *h, const char *args, struct sp_buf *out,
|
||||
void *ctx)
|
||||
{
|
||||
(void)h;
|
||||
(void)ctx;
|
||||
if (args != NULL && *args != '\0')
|
||||
return sp_buf_puts(out, "usage: /junction");
|
||||
snapshot_text(out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The address and the key: the plugin's own block first, then the MCP server
|
||||
* called junction (one place for both), then the environment. The key is never
|
||||
* copied anywhere it could be printed.
|
||||
*/
|
||||
static void
|
||||
resolve_config(struct sp_host *h)
|
||||
{
|
||||
const struct sp_config *cfg = sp_host_config(h);
|
||||
const char *v;
|
||||
const char *tok;
|
||||
size_t i;
|
||||
|
||||
v = sp_host_plugin_config(h, "junction", "url");
|
||||
if (v != NULL && *v != '\0') {
|
||||
pthread_mutex_lock(&j.lock);
|
||||
snprintf(j.url, sizeof(j.url), "%s", v);
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
}
|
||||
v = sp_host_plugin_config(h, "junction", "header");
|
||||
if (v != NULL && *v != '\0') {
|
||||
pthread_mutex_lock(&j.lock);
|
||||
snprintf(j.header, sizeof(j.header), "%s", v);
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
}
|
||||
if (cfg != NULL && cfg->nmcp > 0) {
|
||||
for (i = 0; i < cfg->nmcp; i++) {
|
||||
if (strcmp(cfg->mcp[i].id, "junction") != 0)
|
||||
continue;
|
||||
pthread_mutex_lock(&j.lock);
|
||||
if (j.url[0] == '\0' && cfg->mcp[i].url[0] != '\0')
|
||||
snprintf(j.url, sizeof(j.url), "%s",
|
||||
cfg->mcp[i].url);
|
||||
if (j.header[0] == '\0' &&
|
||||
cfg->mcp[i].header[0] != '\0')
|
||||
snprintf(j.header, sizeof(j.header), "%s",
|
||||
cfg->mcp[i].header);
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j.url[0] == '\0') {
|
||||
v = getenv("SPLINTER_JUNCTION_URL");
|
||||
if (v != NULL && *v != '\0') {
|
||||
pthread_mutex_lock(&j.lock);
|
||||
snprintf(j.url, sizeof(j.url), "%s", v);
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
}
|
||||
}
|
||||
if (j.header[0] == '\0') {
|
||||
tok = getenv("SPLINTER_JUNCTION_TOKEN");
|
||||
if (tok == NULL || *tok == '\0')
|
||||
tok = getenv("JUNCTION_TOKEN");
|
||||
if (tok != NULL && *tok != '\0') {
|
||||
pthread_mutex_lock(&j.lock);
|
||||
snprintf(j.header, sizeof(j.header),
|
||||
"Authorization: Bearer %s", tok);
|
||||
pthread_mutex_unlock(&j.lock);
|
||||
}
|
||||
}
|
||||
v = sp_host_plugin_config(h, "junction", "interval");
|
||||
if (v != NULL && atoi(v) > 0)
|
||||
j.interval_ms = (unsigned)atoi(v);
|
||||
else if ((v = getenv("SPLINTER_JUNCTION_INTERVAL")) != NULL &&
|
||||
atoi(v) > 0)
|
||||
j.interval_ms = (unsigned)atoi(v);
|
||||
if (j.interval_ms < 1000)
|
||||
j.interval_ms = 1000;
|
||||
if (j.interval_ms > 600000)
|
||||
j.interval_ms = 600000;
|
||||
}
|
||||
|
||||
static int
|
||||
junction_init(struct sp_host *h)
|
||||
{
|
||||
struct sp_widget w;
|
||||
static const struct sp_status seg = { "junction", junction_status };
|
||||
static const struct sp_tool tool = {
|
||||
"junction_status",
|
||||
"The fleet router's health: whether it answers, the X-Junction-Mcp "
|
||||
"identity header, uptime, how many MCP servers answer, the queue "
|
||||
"and the request counts from junction's own /status.",
|
||||
"{\"type\":\"object\",\"properties\":{}}",
|
||||
SP_AUTH_READ,
|
||||
junction_tool,
|
||||
NULL
|
||||
};
|
||||
|
||||
j.interval_ms = J_DEFAULT_MS;
|
||||
resolve_config(h);
|
||||
memset(&w, 0, sizeof(w));
|
||||
w.name = "junction";
|
||||
w.tick = junction_tick;
|
||||
w.interval_ms = j.interval_ms;
|
||||
if (sp_host_add_widget(h, &w) != 0)
|
||||
return -1;
|
||||
if (sp_host_add_status(h, &seg) != 0)
|
||||
return -1;
|
||||
if (sp_host_add_tool(h, &tool) != 0)
|
||||
return -1;
|
||||
if (sp_host_add_command(h, "junction", junction_command, NULL) != 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sp_plugin junction_plugin = {
|
||||
SP_PLUGIN_ABI, "junction", "1.0.0", junction_init, NULL, NULL
|
||||
};
|
||||
|
||||
const struct sp_plugin *
|
||||
sp_plugin_describe(void)
|
||||
{
|
||||
return &junction_plugin;
|
||||
}
|
||||
+6
-3
@@ -43,12 +43,15 @@ role.tool = werkzeug
|
||||
role.observation = ausgabe
|
||||
err.thread = Turn-Thread laesst sich nicht starten
|
||||
err.turn_failed = Turn fehlgeschlagen
|
||||
focus.reminder = Recap zum Merken: %s. Pruefe vor der Antwort, ob der letzte Schritt naeher am Ziel war; wenn nicht, sage das und nenne den naechsten Schritt.
|
||||
focus.recap_prompt = Ziel: %s. Schreibe einen kurzen Recap zum Merken: 3-5 Punkte, was am Ende stimmen muss, die ersten konkreten Schritte und offene Fragen.
|
||||
focus.continue_prompt = Arbeite an diesem Ziel weiter: %s. Mach dort weiter, wo die Sitzung aufgehoert hat: pruefe den Stand, tue den naechsten konkreten Schritt und berichte, was sich geaendert hat.
|
||||
focus.goal_prompt = Ziel: %s. Arbeite darauf hin. Wenn das Ziel vollstaendig erreicht ist, beende deine letzte Antwort mit der einen Zeile GOAL_COMPLETE, damit splinter es als succeeded markieren kann.
|
||||
focus.reminder = Recap zum Merken: %s. Pruefe vor der Antwort, ob der letzte Schritt naeher am Ziel war; wenn nicht, sage das und nenne den naechsten Schritt. Wenn das Ziel vollstaendig erreicht ist, beende deine letzte Antwort mit der einen Zeile GOAL_COMPLETE.
|
||||
focus.recap_prompt = Ziel: %s. Schreibe einen kurzen Recap zum Merken: 3-5 Punkte, was am Ende stimmen muss, die ersten konkreten Schritte und offene Fragen. Wenn das Ziel vollstaendig erreicht ist, beende deine letzte Antwort mit der einen Zeile GOAL_COMPLETE.
|
||||
focus.continue_prompt = Arbeite an diesem Ziel weiter: %s. Mach dort weiter, wo die Sitzung aufgehoert hat: pruefe den Stand, tue den naechsten konkreten Schritt und berichte, was sich geaendert hat. Wenn das Ziel vollstaendig erreicht ist, beende deine letzte Antwort mit der einen Zeile GOAL_COMPLETE.
|
||||
focus.set = Ziel gesetzt: %s
|
||||
focus.none = kein Ziel gesetzt (/goal <text>)
|
||||
focus.status = Ziel: %s (%u Recaps, alle %u Turns)
|
||||
focus.status_done = Ziel: %s [succeeded] (%u Recaps, alle %u Turns)
|
||||
focus.succeeded = Ziel erreicht: %s
|
||||
cps.idle = Leerlauf (angewandt %lu, vorbereitet %lu)
|
||||
cps.plan = Plan bereit: schnitt %zu/%zu, %zu -> %zu B
|
||||
cli.no_output = cli: '%s' lieferte keine Ausgabe
|
||||
|
||||
+6
-3
@@ -42,12 +42,15 @@ role.tool = tool
|
||||
role.observation = output
|
||||
err.thread = cannot start the turn thread
|
||||
err.turn_failed = turn failed
|
||||
focus.reminder = Recap to remember: %s. Before answering, check whether the last step moved closer to the goal; if not, say so and name the next step.
|
||||
focus.recap_prompt = Goal: %s. Write a short recap to remember: 3-5 bullets, what must be true at the end, the first concrete steps, and open questions.
|
||||
focus.continue_prompt = Continue working toward this goal: %s. Pick up where the session stopped: check the current state, do the next concrete step, and report what changed.
|
||||
focus.goal_prompt = Goal: %s. Work toward it. When the goal is fully achieved, end your final answer with the single line GOAL_COMPLETE so splinter can mark it as succeeded.
|
||||
focus.reminder = Recap to remember: %s. Before answering, check whether the last step moved closer to the goal; if not, say so and name the next step. When the goal is fully achieved, end your final answer with the single line GOAL_COMPLETE.
|
||||
focus.recap_prompt = Goal: %s. Write a short recap to remember: 3-5 bullets, what must be true at the end, the first concrete steps, and open questions. When the goal is fully achieved, end your final answer with the single line GOAL_COMPLETE.
|
||||
focus.continue_prompt = Continue working toward this goal: %s. Pick up where the session stopped: check the current state, do the next concrete step, and report what changed. When the goal is fully achieved, end your final answer with the single line GOAL_COMPLETE.
|
||||
focus.set = goal set: %s
|
||||
focus.none = no goal set (/goal <text>)
|
||||
focus.status = goal: %s (%u recaps, every %u turns)
|
||||
focus.status_done = goal: %s [succeeded] (%u recaps, every %u turns)
|
||||
focus.succeeded = goal succeeded: %s
|
||||
cps.idle = idle (applied %lu, prepared %lu)
|
||||
cps.plan = plan ready: cut %zu/%zu, %zu -> %zu B
|
||||
cli.no_output = cli: '%s' produced no output
|
||||
|
||||
@@ -57,12 +57,15 @@ err.turn_failed = turno falhou
|
||||
auth.mutate = altera arquivos
|
||||
auth.destructive = destrutivo
|
||||
|
||||
focus.reminder = Recap para lembrar: %s. Antes de responder, verifique se o ultimo passo aproximou do objetivo; se nao, diga isso e proponha o proximo passo.
|
||||
focus.recap_prompt = Objetivo: %s. Escreva um recap curto para lembrar: 3-5 pontos, o que precisa estar certo no final, os primeiros passos concretos e as perguntas abertas.
|
||||
focus.continue_prompt = Continue trabalhando neste objetivo: %s. Retome de onde a sessao parou: verifique o estado, faca o proximo passo concreto e relate o que mudou.
|
||||
focus.goal_prompt = Objetivo: %s. Trabalhe nisso. Quando o objetivo estiver totalmente alcancado, termine sua resposta final com a linha unica GOAL_COMPLETE para que o splinter possa marcar como succeeded.
|
||||
focus.reminder = Recap para lembrar: %s. Antes de responder, verifique se o ultimo passo aproximou do objetivo; se nao, diga isso e proponha o proximo passo. Quando o objetivo estiver totalmente alcancado, termine sua resposta final com a linha unica GOAL_COMPLETE.
|
||||
focus.recap_prompt = Objetivo: %s. Escreva um recap curto para lembrar: 3-5 pontos, o que precisa estar certo no final, os primeiros passos concretos e as perguntas abertas. Quando o objetivo estiver totalmente alcancado, termine sua resposta final com a linha unica GOAL_COMPLETE.
|
||||
focus.continue_prompt = Continue trabalhando neste objetivo: %s. Retome de onde a sessao parou: verifique o estado, faca o proximo passo concreto e relate o que mudou. Quando o objetivo estiver totalmente alcancado, termine sua resposta final com a linha unica GOAL_COMPLETE.
|
||||
focus.set = objetivo definido: %s
|
||||
focus.none = nenhum objetivo definido (/goal <texto>)
|
||||
focus.status = objetivo: %s (%u recaps, a cada %u turnos)
|
||||
focus.status_done = objetivo: %s [succeeded] (%u recaps, a cada %u turnos)
|
||||
focus.succeeded = objetivo alcancado: %s
|
||||
|
||||
cps.idle = ocioso (aplicado %lu, preparado %lu)
|
||||
cps.plan = plano pronto: corte %zu/%zu, %zu -> %zu B
|
||||
|
||||
@@ -2003,7 +2003,9 @@ sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out)
|
||||
sp_buf_printf(out, sp_td("focus.continue_prompt",
|
||||
"Continue working toward this goal: %s. Pick up where the "
|
||||
"session stopped: check the current state, do the next "
|
||||
"concrete step, and report what changed."), goal);
|
||||
"concrete step, and report what changed. When the goal is "
|
||||
"fully achieved, end your final answer with the single "
|
||||
"line GOAL_COMPLETE."), goal);
|
||||
return SP_INPUT_PROMPT;
|
||||
}
|
||||
if (strcmp(name, "connect") == 0) {
|
||||
@@ -2039,7 +2041,9 @@ sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out)
|
||||
"Goal: %s. Write a short recap to remember: "
|
||||
"3-5 bullets, what must be true at the end, "
|
||||
"the first concrete steps, and open "
|
||||
"questions."), args);
|
||||
"questions. When the goal is fully achieved, "
|
||||
"end your final answer with the single line "
|
||||
"GOAL_COMPLETE."), args);
|
||||
sp_buf_free(&confirm);
|
||||
return SP_INPUT_PROMPT;
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ struct session_meta {
|
||||
long opened;
|
||||
char title[96];
|
||||
char goal[1001]; /* the session's /goal, one line */
|
||||
char goal_state[16]; /* open | succeeded: the model said GOAL_COMPLETE */
|
||||
};
|
||||
|
||||
/* a title is one line, no control bytes, at most 95 bytes of UTF-8 */
|
||||
@@ -141,6 +142,8 @@ meta_read(const char *dir, const char *id, struct session_meta *m)
|
||||
snprintf(m->title, sizeof(m->title), "%s", v);
|
||||
else if (strcmp(line, "goal") == 0)
|
||||
snprintf(m->goal, sizeof(m->goal), "%s", v);
|
||||
else if (strcmp(line, "goal_state") == 0)
|
||||
snprintf(m->goal_state, sizeof(m->goal_state), "%s", v);
|
||||
}
|
||||
fclose(f);
|
||||
return 0;
|
||||
@@ -163,6 +166,8 @@ meta_write(const char *dir, const char *id, const struct session_meta *m)
|
||||
fprintf(f, "title=%s\n", m->title);
|
||||
if (m->goal[0] != '\0')
|
||||
fprintf(f, "goal=%s\n", m->goal);
|
||||
if (m->goal_state[0] != '\0')
|
||||
fprintf(f, "goal_state=%s\n", m->goal_state);
|
||||
if (fclose(f) != 0) {
|
||||
(void)unlink(tmp);
|
||||
return -1;
|
||||
@@ -819,6 +824,8 @@ sp_session_set_goal(const char *dir, const char *id, const char *goal,
|
||||
if (meta_read(dir, id, &m) != 0)
|
||||
memset(&m, 0, sizeof(m));
|
||||
snprintf(m.goal, sizeof(m.goal), "%s", clean);
|
||||
/* a new goal is open, whatever the last one reached */
|
||||
snprintf(m.goal_state, sizeof(m.goal_state), "open");
|
||||
if (meta_write(dir, id, &m) != 0) {
|
||||
sp_buf_printf(err, "session: cannot write the meta file: %s",
|
||||
strerror(errno));
|
||||
@@ -841,6 +848,62 @@ sp_session_get_goal(const char *dir, const char *id, char *buf, size_t cap)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Whether the goal is still open or the model has declared it done. The state
|
||||
* lives beside the goal in the session, not in the plugin: a resumed session
|
||||
* must not forget that its goal was already reached, and the plugin is not
|
||||
* unloaded between sessions.
|
||||
*/
|
||||
int
|
||||
sp_session_set_goal_state(const char *dir, const char *id, const char *state,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
struct session_meta m;
|
||||
char path[700];
|
||||
|
||||
if (dir == NULL || id == NULL || state == NULL || !valid_id(id)) {
|
||||
sp_buf_puts(err, "session: invalid id");
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(state, "open") != 0 && strcmp(state, "succeeded") != 0) {
|
||||
sp_buf_puts(err, "session: goal state must be open or succeeded");
|
||||
return -1;
|
||||
}
|
||||
snprintf(path, sizeof(path), "%s/%s.jsonl", dir, id);
|
||||
if (access(path, F_OK) != 0) {
|
||||
sp_buf_puts(err, "session: no such session");
|
||||
return -1;
|
||||
}
|
||||
if (meta_read(dir, id, &m) != 0)
|
||||
memset(&m, 0, sizeof(m));
|
||||
if (m.goal[0] == '\0') {
|
||||
sp_buf_puts(err, "session: no goal set");
|
||||
return -1;
|
||||
}
|
||||
snprintf(m.goal_state, sizeof(m.goal_state), "%s", state);
|
||||
if (meta_write(dir, id, &m) != 0) {
|
||||
sp_buf_printf(err, "session: cannot write the meta file: %s",
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_session_get_goal_state(const char *dir, const char *id, char *buf,
|
||||
size_t cap)
|
||||
{
|
||||
struct session_meta m;
|
||||
|
||||
if (dir == NULL || id == NULL || buf == NULL || cap == 0 ||
|
||||
!valid_id(id))
|
||||
return -1;
|
||||
if (meta_read(dir, id, &m) != 0 || m.goal_state[0] == '\0')
|
||||
return -1;
|
||||
snprintf(buf, cap, "%s", m.goal_state);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_session_delete(const char *dir, const char *id, struct sp_buf *err)
|
||||
{
|
||||
|
||||
+60
-3
@@ -301,6 +301,48 @@ sp_theme_from_xrdb(const char *text, struct sp_theme *out)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The simple thing, asked for after a palette made the chat unreadable: one
|
||||
* surface for the chat, the sidebar and the input line, and text that stands
|
||||
* on it. Role colours keep their hue and are dark on white, bright on black;
|
||||
* the status line is the one surface that may differ, so it remains a bar.
|
||||
*/
|
||||
static void
|
||||
load_flat(int dark, struct sp_theme *out)
|
||||
{
|
||||
memset(out, 0, sizeof(*out));
|
||||
out->name = dark ? SP_THEME_DARK : SP_THEME_LIGHT;
|
||||
if (!dark) {
|
||||
memcpy(out->bg, (const unsigned char[]){255, 255, 255}, 3);
|
||||
memcpy(out->fg, (const unsigned char[]){0, 0, 0}, 3);
|
||||
memcpy(out->border, (const unsigned char[]){96, 96, 96}, 3);
|
||||
memcpy(out->accent, (const unsigned char[]){0, 0, 176}, 3);
|
||||
memcpy(out->agent, (const unsigned char[]){0, 112, 0}, 3);
|
||||
memcpy(out->think, (const unsigned char[]){104, 0, 148}, 3);
|
||||
memcpy(out->tool, (const unsigned char[]){0, 104, 116}, 3);
|
||||
memcpy(out->notice, (const unsigned char[]){148, 0, 120}, 3);
|
||||
memcpy(out->error, (const unsigned char[]){176, 0, 0}, 3);
|
||||
memcpy(out->input_bg, out->bg, 3);
|
||||
memcpy(out->input_fg, out->fg, 3);
|
||||
memcpy(out->status, (const unsigned char[]){230, 230, 230}, 3);
|
||||
memcpy(out->status_fg, (const unsigned char[]){0, 0, 0}, 3);
|
||||
} else {
|
||||
memcpy(out->bg, (const unsigned char[]){0, 0, 0}, 3);
|
||||
memcpy(out->fg, (const unsigned char[]){235, 235, 235}, 3);
|
||||
memcpy(out->border, (const unsigned char[]){160, 160, 160}, 3);
|
||||
memcpy(out->accent, (const unsigned char[]){120, 170, 255}, 3);
|
||||
memcpy(out->agent, (const unsigned char[]){120, 230, 140}, 3);
|
||||
memcpy(out->think, (const unsigned char[]){200, 160, 255}, 3);
|
||||
memcpy(out->tool, (const unsigned char[]){90, 220, 230}, 3);
|
||||
memcpy(out->notice, (const unsigned char[]){240, 140, 220}, 3);
|
||||
memcpy(out->error, (const unsigned char[]){255, 110, 110}, 3);
|
||||
memcpy(out->input_bg, out->bg, 3);
|
||||
memcpy(out->input_fg, out->fg, 3);
|
||||
memcpy(out->status, (const unsigned char[]){40, 40, 40}, 3);
|
||||
memcpy(out->status_fg, (const unsigned char[]){235, 235, 235}, 3);
|
||||
}
|
||||
}
|
||||
|
||||
/* the resource text: the X server's database, or the file it is loaded from */
|
||||
static char *
|
||||
xrdb_text(void)
|
||||
@@ -352,10 +394,18 @@ sp_theme_load(const char *name, struct sp_theme *out, struct sp_buf *err)
|
||||
free(text);
|
||||
return rc;
|
||||
}
|
||||
if (strcasecmp(name, SP_THEME_LIGHT) == 0) {
|
||||
load_flat(0, out);
|
||||
return 0;
|
||||
}
|
||||
if (strcasecmp(name, SP_THEME_DARK) == 0) {
|
||||
load_flat(1, out);
|
||||
return 0;
|
||||
}
|
||||
p = find_palette(name);
|
||||
if (p == NULL) {
|
||||
sp_buf_printf(err, "theme: unknown theme '%s' (Xresources or "
|
||||
"one of %zu CDE palettes; --list-themes)", name,
|
||||
sp_buf_printf(err, "theme: unknown theme '%s' (Xresources, light, "
|
||||
"dark or one of %zu CDE palettes; --list-themes)", name,
|
||||
sp_palette_count);
|
||||
return -1;
|
||||
}
|
||||
@@ -370,8 +420,15 @@ sp_theme_list(const char ***names, size_t *count)
|
||||
size_t i, n = 0;
|
||||
|
||||
list[n++] = SP_THEME_XRESOURCES;
|
||||
for (i = 0; i < sp_palette_count && n < 257; i++)
|
||||
for (i = 0; i < sp_palette_count && n < 255; i++)
|
||||
list[n++] = sp_palettes[i].name;
|
||||
/*
|
||||
* The flat themes come last: the CDE names are capitalised, so this
|
||||
* keeps the list sorted after the fixed first entry and --list-themes
|
||||
* reads as one alphabetical column behind "Xresources".
|
||||
*/
|
||||
list[n++] = SP_THEME_DARK;
|
||||
list[n++] = SP_THEME_LIGHT;
|
||||
*names = list;
|
||||
*count = n;
|
||||
return n;
|
||||
|
||||
@@ -57,6 +57,15 @@ struct sp_theme {
|
||||
|
||||
#define SP_THEME_DEFAULT "Xresources"
|
||||
#define SP_THEME_XRESOURCES "Xresources"
|
||||
/*
|
||||
* Two flat themes, because a palette can be beautiful and still leave the
|
||||
* conversation unreadable. One page colour for the chat, the sidebar and the
|
||||
* input line; the status line is the only surface that is allowed to differ.
|
||||
* light: white page, black text, white input, grey status
|
||||
* dark: black page, white text, black input, dark grey status
|
||||
*/
|
||||
#define SP_THEME_LIGHT "light"
|
||||
#define SP_THEME_DARK "dark"
|
||||
|
||||
/* Load a theme by name; NULL selects the default. Returns 0 or -1. */
|
||||
int sp_theme_load(const char *name, struct sp_theme *out, struct sp_buf *err);
|
||||
|
||||
+34
-17
@@ -130,6 +130,7 @@ struct ui_state {
|
||||
|
||||
static WINDOW *ui_hist;
|
||||
static WINDOW *ui_side;
|
||||
static WINDOW *ui_gap; /* the one column between chat and sidebar */
|
||||
static WINDOW *ui_status;
|
||||
static WINDOW *ui_input;
|
||||
/*
|
||||
@@ -149,7 +150,7 @@ enum {
|
||||
PAIR_THINK, /* the model's reasoning */
|
||||
PAIR_TOOL, /* a tool call */
|
||||
PAIR_NOTICE, /* status and session notes */
|
||||
PAIR_SIDE /* sidebar: white on one dark surface */
|
||||
PAIR_SIDE /* sidebar: the theme's text on the theme's page */
|
||||
};
|
||||
|
||||
/* the session id and directory the sidebar shows */
|
||||
@@ -268,7 +269,8 @@ init_colors(const struct sp_theme *t)
|
||||
init_pair(PAIR_THINK, think, t->idx_bg);
|
||||
init_pair(PAIR_TOOL, tool, t->idx_bg);
|
||||
init_pair(PAIR_NOTICE, notice, t->idx_bg);
|
||||
init_pair(PAIR_SIDE, COLOR_WHITE, t->idx_bg);
|
||||
/* the sidebar is the page: its own text on its own surface */
|
||||
init_pair(PAIR_SIDE, t->idx_fg, t->idx_bg);
|
||||
return;
|
||||
}
|
||||
init_pair(PAIR_MAIN, rgb(t->fg, n), rgb(t->bg, n));
|
||||
@@ -281,20 +283,13 @@ init_colors(const struct sp_theme *t)
|
||||
init_pair(PAIR_THINK, rgb(t->think, n), rgb(t->bg, n));
|
||||
init_pair(PAIR_TOOL, rgb(t->tool, n), rgb(t->bg, n));
|
||||
init_pair(PAIR_NOTICE, rgb(t->notice, n), rgb(t->bg, n));
|
||||
{
|
||||
/*
|
||||
* The sidebar is one surface, not a patchwork: a darkened theme
|
||||
* background with white text, so every heading, label and widget
|
||||
* sits on the same colour. On a dark theme it is simply the
|
||||
* background.
|
||||
*/
|
||||
unsigned char side_bg[3];
|
||||
|
||||
memcpy(side_bg, t->bg, 3);
|
||||
sp_color_darken(side_bg, 45000);
|
||||
init_pair(PAIR_SIDE, rgb((const unsigned char[]){255, 255, 255}, n),
|
||||
rgb(side_bg, n));
|
||||
}
|
||||
/*
|
||||
* The sidebar is the page itself: the theme's own text on the theme's
|
||||
* own background. A darkened surface made a white theme paint black
|
||||
* behind white text; the operator asked for the simple thing instead --
|
||||
* white page, black text; black page, white text.
|
||||
*/
|
||||
init_pair(PAIR_SIDE, rgb(t->fg, n), rgb(t->bg, n));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1109,6 +1104,15 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir,
|
||||
char key[64];
|
||||
const char *act = sp_usage_active();
|
||||
|
||||
if (ui_gap != NULL) {
|
||||
/*
|
||||
* Repaint the seam every frame: clearok() after a resize and the
|
||||
* first draw both leave holes that would show the terminal's own
|
||||
* background in the middle of a white page.
|
||||
*/
|
||||
werase(ui_gap);
|
||||
wnoutrefresh(ui_gap);
|
||||
}
|
||||
if (ui_side == NULL)
|
||||
return; /* one text field: no sidebar */
|
||||
|
||||
@@ -1559,10 +1563,21 @@ layout(void)
|
||||
ui_hist = newwin(rows - 2, chat_w, 0, 0);
|
||||
ui_side = side_w > 0 ?
|
||||
newwin(rows - 2, side_w, 0, chat_w + 1) : NULL;
|
||||
/*
|
||||
* The column between the two panes belongs to nobody. Left
|
||||
* alone it keeps the terminal's default colour, which on a
|
||||
* white theme is a black stripe down the middle. It is a
|
||||
* window with the page colour instead.
|
||||
*/
|
||||
ui_gap = side_w > 0 ? newwin(rows - 2, 1, 0, chat_w) : NULL;
|
||||
}
|
||||
wbkgd(ui_hist, COLOR_PAIR(PAIR_MAIN));
|
||||
if (ui_side != NULL)
|
||||
wbkgd(ui_side, COLOR_PAIR(PAIR_SIDE));
|
||||
if (ui_gap != NULL) {
|
||||
wbkgd(ui_gap, COLOR_PAIR(PAIR_MAIN));
|
||||
werase(ui_gap);
|
||||
}
|
||||
/* the meter line sits above the input, like a status bar */
|
||||
ui_status = newwin(1, cols, rows - 2, 0);
|
||||
wbkgd(ui_status, COLOR_PAIR(PAIR_STATUS));
|
||||
@@ -1585,11 +1600,13 @@ relayout(int input_timeout_ms)
|
||||
delwin(ui_hist);
|
||||
if (ui_side != NULL)
|
||||
delwin(ui_side);
|
||||
if (ui_gap != NULL)
|
||||
delwin(ui_gap);
|
||||
if (ui_status != NULL)
|
||||
delwin(ui_status);
|
||||
if (ui_input != NULL)
|
||||
delwin(ui_input);
|
||||
ui_hist = ui_side = ui_status = ui_input = NULL;
|
||||
ui_hist = ui_side = ui_gap = ui_status = ui_input = NULL;
|
||||
layout();
|
||||
wtimeout(ui_input, input_timeout_ms);
|
||||
clearok(curscr, TRUE);
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python3
|
||||
"""A stand-in for junction, for tests/test_junction.
|
||||
|
||||
It answers exactly what the plugin reads: the X-Junction-Mcp identity header on
|
||||
/mcp (with 405, as the real one does for a GET) and a /status body with the
|
||||
counters the plugin publishes. No token is required; the test is about reading
|
||||
the header and the JSON, not about authentication.
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
STATUS = {
|
||||
"uptime_s": 1211,
|
||||
"requests": {"ok": 11, "fail": 0},
|
||||
"queue": {"inflight": 1, "waiting": 0, "max_inflight": 32},
|
||||
"endpoints": [{"name": "a"}, {"name": "b"}, {"name": "c"}],
|
||||
"mcp": [
|
||||
{"name": "tasksd", "ok": 1, "fail": 0, "tools": 9},
|
||||
{"name": "skilld", "ok": 1, "fail": 0, "tools": 10},
|
||||
{"name": "memoryd", "ok": 1, "fail": 0, "tools": 7},
|
||||
{"name": "wikid", "ok": 0, "fail": 1, "tools": 0},
|
||||
{"name": "koder", "ok": 1, "fail": 0, "tools": 12},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def log_message(self, *args):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
if self.path == "/mcp":
|
||||
self.send_response(405)
|
||||
self.send_header("X-Junction-Mcp", "1")
|
||||
self.send_header("Content-Length", "0")
|
||||
self.end_headers()
|
||||
elif self.path == "/status":
|
||||
body = json.dumps(STATUS).encode()
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.send_header("Content-Length", "0")
|
||||
self.end_headers()
|
||||
|
||||
|
||||
def main():
|
||||
port = int(sys.argv[1]) if len(sys.argv) > 1 else 18099
|
||||
HTTPServer(("127.0.0.1", port), Handler).serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+138
-7
@@ -8,6 +8,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
@@ -82,14 +84,18 @@ main(void)
|
||||
CHECK(sp_conv_push(&conv, "system", NULL, "du bist knapp") == 0);
|
||||
CHECK(sp_conv_push(&conv, "user", NULL, "erster turn") == 0);
|
||||
sp_host_turn(host);
|
||||
CHECK(sp_host_compact(host, &conv, 0) == 0);
|
||||
CHECK(count_focus(&conv) == 0);
|
||||
/* the first request carries the goal itself, before the user's line */
|
||||
CHECK(sp_host_compact(host, &conv, 0) == 1);
|
||||
CHECK(count_focus(&conv) == 1);
|
||||
CHECK(strstr(conv.v[conv.n - 1].content, "erster turn") != NULL);
|
||||
CHECK(strstr(conv.v[conv.n - 2].content, "Ship the config file") != NULL);
|
||||
CHECK(strstr(conv.v[conv.n - 2].content, "GOAL_COMPLETE") != NULL);
|
||||
|
||||
/* turn 2: the reminder appears, once */
|
||||
CHECK(sp_conv_push(&conv, "user", NULL, "zweiter turn") == 0);
|
||||
sp_host_turn(host);
|
||||
CHECK(sp_host_compact(host, &conv, 1) == 1);
|
||||
CHECK(count_focus(&conv) == 1);
|
||||
CHECK(count_focus(&conv) == 2);
|
||||
CHECK(strstr(conv.v[conv.n - 1].content, "zweiter turn") != NULL);
|
||||
{
|
||||
size_t i;
|
||||
@@ -105,19 +111,20 @@ main(void)
|
||||
|
||||
/* compacting again in the same turn must not add a second one */
|
||||
CHECK(sp_host_compact(host, &conv, 1) == 0);
|
||||
CHECK(count_focus(&conv) == 1);
|
||||
CHECK(count_focus(&conv) == 2);
|
||||
|
||||
/* turn 3: silent */
|
||||
CHECK(sp_conv_push(&conv, "user", NULL, "dritter turn") == 0);
|
||||
sp_host_turn(host);
|
||||
CHECK(sp_host_compact(host, &conv, 1) == 0);
|
||||
CHECK(count_focus(&conv) == 1);
|
||||
CHECK(count_focus(&conv) == 2);
|
||||
|
||||
/* turn 4: second reminder */
|
||||
/* turn 4: second reminder, with the completion rule in it */
|
||||
CHECK(sp_conv_push(&conv, "user", NULL, "vierter turn") == 0);
|
||||
sp_host_turn(host);
|
||||
CHECK(sp_host_compact(host, &conv, 1) == 1);
|
||||
CHECK(count_focus(&conv) == 2);
|
||||
CHECK(count_focus(&conv) == 3);
|
||||
CHECK(strstr(conv.v[conv.n - 2].content, "GOAL_COMPLETE") != NULL);
|
||||
|
||||
/* ---- /goal is the recap feature; /recap is not a command ---- */
|
||||
{
|
||||
@@ -170,11 +177,135 @@ main(void)
|
||||
CHECK(sp_host_command(host, "gibtsnicht", "", &out) == -1);
|
||||
CHECK(strstr(out.p, "unknown command") != NULL);
|
||||
|
||||
/*
|
||||
* The goal ends with GOAL_COMPLETE. The next compaction sees the
|
||||
* newest assistant answer, marks the goal succeeded and stops
|
||||
* reminding; /goal and the sidebar say so.
|
||||
*/
|
||||
CHECK(sp_conv_push(&conv, "assistant", NULL,
|
||||
"alles erledigt\nGOAL_COMPLETE") == 0);
|
||||
CHECK(sp_conv_push(&conv, "user", NULL, "und jetzt?") == 0);
|
||||
sp_host_turn(host);
|
||||
{
|
||||
int before = count_focus(&conv);
|
||||
char snap[1024];
|
||||
|
||||
CHECK(sp_host_compact(host, &conv, 1) == 0);
|
||||
CHECK(count_focus(&conv) == before);
|
||||
snap[0] = '\0';
|
||||
sp_host_widget_snapshot(host, snap, sizeof(snap));
|
||||
CHECK(strstr(snap, "[done]") != NULL);
|
||||
CHECK(strstr(snap, "neues ziel") != NULL);
|
||||
}
|
||||
sp_buf_clear(&out);
|
||||
CHECK(sp_host_command(host, "goal", "", &out) == 0);
|
||||
CHECK(strstr(out.p, "succeeded") != NULL);
|
||||
|
||||
/* succeeded is final for this goal: no more reminders */
|
||||
sp_buf_clear(&out);
|
||||
CHECK(sp_host_command(host, "goal", "", &out) == 0);
|
||||
CHECK(strstr(out.p, "succeeded") != NULL);
|
||||
CHECK(sp_conv_push(&conv, "user", NULL, "noch ein turn") == 0);
|
||||
sp_host_turn(host);
|
||||
CHECK(sp_host_compact(host, &conv, 1) == 0);
|
||||
|
||||
/* a new goal opens a new one, and is announced again */
|
||||
sp_buf_clear(&out);
|
||||
CHECK(sp_host_command(host, "goal", "zweites ziel", &out) == 0);
|
||||
CHECK(sp_conv_push(&conv, "user", NULL, "los") == 0);
|
||||
sp_host_turn(host);
|
||||
CHECK(sp_host_compact(host, &conv, 1) == 1);
|
||||
{
|
||||
char snap[1024];
|
||||
|
||||
snap[0] = '\0';
|
||||
sp_host_widget_snapshot(host, snap, sizeof(snap));
|
||||
CHECK(strstr(snap, "[done]") == NULL);
|
||||
CHECK(strstr(snap, "zweites ziel") != NULL);
|
||||
}
|
||||
|
||||
sp_buf_free(&out);
|
||||
}
|
||||
|
||||
sp_conv_free(&conv);
|
||||
sp_host_free(host);
|
||||
|
||||
/*
|
||||
* ---- the tick reads the answer from the session log ----
|
||||
*
|
||||
* The compactor sees the conversation; a turn that ends has no next
|
||||
* compactor call. The tick watches the session file, so the goal turns
|
||||
* "[done]" without waiting for another turn.
|
||||
*/
|
||||
{
|
||||
static struct sp_session_info si;
|
||||
char dir[] = "/tmp/opencode/focus-XXXXXX";
|
||||
char snap[1024], st[16];
|
||||
struct sp_conv c2;
|
||||
|
||||
CHECK(mkdtemp(dir) != NULL);
|
||||
snprintf(si.dir, sizeof(si.dir), "%s", dir);
|
||||
snprintf(si.id, sizeof(si.id), "sess-goaltest");
|
||||
CHECK(sp_session_append(&si, "user", "start", &err) == 0);
|
||||
CHECK(sp_session_set_goal(dir, si.id, "tick goal", &err) == 0);
|
||||
|
||||
host = sp_host_new(®, &ag, &si);
|
||||
CHECK(host != NULL);
|
||||
CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0);
|
||||
sp_conv_init(&c2);
|
||||
CHECK(sp_conv_push(&c2, "user", NULL, "erste anfrage") == 0);
|
||||
sp_host_turn(host);
|
||||
CHECK(sp_host_compact(host, &c2, 0) == 1); /* announced, armed */
|
||||
snap[0] = '\0';
|
||||
sp_host_widget_snapshot(host, snap, sizeof(snap));
|
||||
CHECK(strstr(snap, "tick goal") != NULL);
|
||||
CHECK(strstr(snap, "[done]") == NULL);
|
||||
|
||||
/* the model answers after the goal went into the prompt */
|
||||
{
|
||||
struct timespec ts = { 1, 100 * 1000 * 1000L };
|
||||
|
||||
nanosleep(&ts, NULL);
|
||||
}
|
||||
CHECK(sp_session_append(&si, "assistant",
|
||||
"fertig\nGOAL_COMPLETE", &err) == 0);
|
||||
{
|
||||
struct timespec ts = { 1, 600 * 1000 * 1000L };
|
||||
|
||||
nanosleep(&ts, NULL); /* let the tick run */
|
||||
}
|
||||
snap[0] = '\0';
|
||||
sp_host_widget_snapshot(host, snap, sizeof(snap));
|
||||
CHECK(strstr(snap, "[done]") != NULL);
|
||||
CHECK(sp_session_get_goal_state(dir, si.id, st, sizeof(st)) == 0);
|
||||
CHECK(strcmp(st, "succeeded") == 0);
|
||||
|
||||
/* and the state survives a fresh host: the meta is the truth */
|
||||
sp_conv_free(&c2);
|
||||
sp_host_free(host);
|
||||
host = sp_host_new(®, &ag, &si);
|
||||
CHECK(host != NULL);
|
||||
CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0);
|
||||
{
|
||||
struct sp_buf out;
|
||||
|
||||
CHECK(sp_buf_init(&out) == 0);
|
||||
CHECK(sp_host_command(host, "goal", "", &out) == 0);
|
||||
CHECK(strstr(out.p, "succeeded") != NULL);
|
||||
sp_buf_free(&out);
|
||||
}
|
||||
sp_host_free(host);
|
||||
{
|
||||
char path[700];
|
||||
|
||||
snprintf(path, sizeof(path), "%s/%s.jsonl", dir, si.id);
|
||||
(void)unlink(path);
|
||||
snprintf(path, sizeof(path), "%s/%s.meta", dir, si.id);
|
||||
(void)unlink(path);
|
||||
}
|
||||
(void)rmdir(dir);
|
||||
}
|
||||
|
||||
sp_buf_free(&err);
|
||||
sp_registry_free(®);
|
||||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* splinter -- junction plugin tests.
|
||||
*
|
||||
* The plugin is loaded for real and talks HTTP to tests/fake_junction.py. What
|
||||
* is proven: the X-Junction-Mcp header is what makes the endpoint "junction",
|
||||
* the counters come from /status (not from the plugin counting), the sidebar
|
||||
* text carries no colon in a continuation line (the sidebar would read it as a
|
||||
* new widget), and neither the tool nor /junction prints the key.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.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 void
|
||||
nap_ms(long ms)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
ts.tv_sec = ms / 1000;
|
||||
ts.tv_nsec = (ms % 1000) * 1000000L;
|
||||
nanosleep(&ts, NULL);
|
||||
}
|
||||
|
||||
/* the sidebar reads "name: text"; line 2 and later must not look like a name */
|
||||
static int
|
||||
continuation_has_colon(const char *text)
|
||||
{
|
||||
const char *p = strchr(text, '\n');
|
||||
|
||||
while (p != NULL && *p != '\0') {
|
||||
const char *start = p + 1;
|
||||
const char *nl = strchr(start, '\n');
|
||||
|
||||
p = nl;
|
||||
if (strchr(start, ':') != NULL &&
|
||||
(nl == NULL || strchr(start, ':') < nl))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
struct sp_registry reg;
|
||||
struct sp_agent ag;
|
||||
struct sp_host *host;
|
||||
struct sp_buf err, out;
|
||||
char snap[4096], url[256];
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s PORT\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
memset(®, 0, sizeof(reg));
|
||||
memset(&ag, 0, sizeof(ag));
|
||||
if (sp_buf_init(&err) != 0 || sp_buf_init(&out) != 0)
|
||||
return 1;
|
||||
|
||||
snprintf(url, sizeof(url), "http://127.0.0.1:%s/mcp", argv[1]);
|
||||
setenv("SPLINTER_JUNCTION_URL", url, 1);
|
||||
unsetenv("SPLINTER_JUNCTION_TOKEN");
|
||||
unsetenv("JUNCTION_TOKEN");
|
||||
|
||||
host = sp_host_new(®, &ag, NULL);
|
||||
CHECK(host != NULL);
|
||||
CHECK(sp_plugin_load(host, "plugins/junction/junction.so", &err) == 0);
|
||||
/* the loader reports a failed init; a CHECK here would have caught it */
|
||||
|
||||
/* the first tick runs immediately, the HTTP round trips take a moment */
|
||||
nap_ms(2500);
|
||||
snap[0] = '\0';
|
||||
CHECK(sp_host_widget_snapshot(host, snap, sizeof(snap)) > 0);
|
||||
CHECK(strstr(snap, "junction: ") != NULL);
|
||||
CHECK(strstr(snap, "up") != NULL);
|
||||
CHECK(strstr(snap, "X-Junction-Mcp") != NULL);
|
||||
CHECK(strstr(snap, "mcp 4/5") != NULL);
|
||||
CHECK(!continuation_has_colon(snap));
|
||||
|
||||
/* the status segment the status line and /status show */
|
||||
{
|
||||
char buf[160];
|
||||
|
||||
buf[0] = '\0';
|
||||
(void)sp_host_status_text(host, buf, sizeof(buf));
|
||||
CHECK(strstr(buf, "junction") != NULL);
|
||||
CHECK(strstr(buf, "up") != NULL);
|
||||
}
|
||||
|
||||
/* /junction: every field, no key */
|
||||
sp_buf_clear(&out);
|
||||
CHECK(sp_host_command(host, "junction", "", &out) == 0);
|
||||
CHECK(strstr(out.p, "junction: up") != NULL);
|
||||
CHECK(strstr(out.p, "mcp: 4/5 answering") != NULL);
|
||||
CHECK(strstr(out.p, "header: no") != NULL); /* no token was set */
|
||||
CHECK(strstr(out.p, "Authorization") == NULL);
|
||||
|
||||
/* the tool is registered and returns the same snapshot */
|
||||
CHECK(sp_registry_find(®, "junction_status") != NULL);
|
||||
|
||||
sp_host_free(host);
|
||||
sp_buf_free(&err);
|
||||
sp_buf_free(&out);
|
||||
sp_registry_free(®);
|
||||
|
||||
if (failures == 0)
|
||||
printf("test_junction: ok\n");
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#!/bin/sh
|
||||
# tests/test_junction.sh -- the junction plugin against a stand-in server.
|
||||
#
|
||||
# The C test needs a live HTTP endpoint; this starts tests/fake_junction.py on a
|
||||
# free-ish port, waits for it, and hands the port to the test. Nothing leaves
|
||||
# the machine.
|
||||
set -e
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
port=${JUNCTION_TEST_PORT:-18099}
|
||||
python3 tests/fake_junction.py "$port" &
|
||||
pid=$!
|
||||
trap 'kill $pid 2>/dev/null || true' EXIT INT TERM
|
||||
|
||||
# wait for the port, not for a fixed sleep: a loaded machine starts python late
|
||||
i=0
|
||||
while [ $i -lt 50 ]; do
|
||||
if python3 - "$port" <<'EOF'
|
||||
import socket, sys
|
||||
s = socket.socket()
|
||||
s.settimeout(0.2)
|
||||
try:
|
||||
s.connect(("127.0.0.1", int(sys.argv[1])))
|
||||
sys.exit(0)
|
||||
except OSError:
|
||||
sys.exit(1)
|
||||
finally:
|
||||
s.close()
|
||||
EOF
|
||||
then
|
||||
break
|
||||
fi
|
||||
i=$((i + 1))
|
||||
sleep 0.1
|
||||
done
|
||||
|
||||
./tests/test_junction "$port"
|
||||
+37
-4
@@ -32,13 +32,15 @@ main(void)
|
||||
if (sp_buf_init(&err) != 0)
|
||||
return 1;
|
||||
|
||||
/* Xresources first, then every palette of the CDE tree, by name */
|
||||
/* Xresources first, then every palette of the CDE tree, then the flat two */
|
||||
CHECK(sp_theme_list(&names, &count) == count);
|
||||
CHECK(count == sp_palette_count + 1);
|
||||
CHECK(count >= 41);
|
||||
CHECK(count == sp_palette_count + 3);
|
||||
CHECK(count >= 43);
|
||||
CHECK(strcmp(names[0], "Xresources") == 0);
|
||||
for (i = 2; i < count; i++)
|
||||
CHECK(strcmp(names[i - 1], names[i]) < 0);
|
||||
CHECK(strcmp(names[count - 2], "dark") == 0);
|
||||
CHECK(strcmp(names[count - 1], "light") == 0);
|
||||
|
||||
/* the default is the X resource theme, and NULL means the default */
|
||||
CHECK(sp_theme_load(NULL, &t, &err) == 0);
|
||||
@@ -57,7 +59,7 @@ main(void)
|
||||
CHECK(sp_theme_load("Delphinium", &t2, &err) == 0);
|
||||
|
||||
/* in every palette the text stands off its surface (CDE's black/white) */
|
||||
for (i = 1; i < count; i++) {
|
||||
for (i = 1; i < count - 2; i++) {
|
||||
CHECK(sp_theme_load(names[i], &t, &err) == 0);
|
||||
CHECK(abs(luma(t.fg) - luma(t.bg)) >= 100000 ||
|
||||
luma(t.bg) == luma(t.fg));
|
||||
@@ -70,6 +72,37 @@ main(void)
|
||||
CHECK(abs(luma(t.tool) - luma(t.bg)) >= 34000);
|
||||
CHECK(abs(luma(t.notice) - luma(t.bg)) >= 36000);
|
||||
}
|
||||
|
||||
/*
|
||||
* The flat themes: the operator asked for the simple thing. The chat
|
||||
* and the sidebar share one page colour (white/black), the text is the
|
||||
* opposite, the input keeps the same page, and only the status line is
|
||||
* allowed to be a different surface.
|
||||
*/
|
||||
CHECK(sp_theme_load("light", &t, &err) == 0);
|
||||
CHECK(strcmp(t.name, SP_THEME_LIGHT) == 0);
|
||||
CHECK(memcmp(t.bg, "\xff\xff\xff", 3) == 0);
|
||||
CHECK(memcmp(t.fg, "\x00\x00\x00", 3) == 0);
|
||||
CHECK(memcmp(t.input_bg, t.bg, 3) == 0);
|
||||
CHECK(memcmp(t.input_fg, t.fg, 3) == 0);
|
||||
CHECK(memcmp(t.status, t.bg, 3) != 0);
|
||||
CHECK(memcmp(t.status_fg, t.fg, 3) == 0);
|
||||
CHECK(t.indexed == 0);
|
||||
CHECK(sp_theme_load("Light", &t2, &err) == 0);
|
||||
CHECK(strcmp(t2.name, SP_THEME_LIGHT) == 0);
|
||||
CHECK(sp_theme_load("dark", &t, &err) == 0);
|
||||
CHECK(strcmp(t.name, SP_THEME_DARK) == 0);
|
||||
CHECK(memcmp(t.bg, "\x00\x00\x00", 3) == 0);
|
||||
CHECK(memcmp(t.fg, "\xeb\xeb\xeb", 3) == 0);
|
||||
CHECK(memcmp(t.input_bg, t.bg, 3) == 0);
|
||||
CHECK(memcmp(t.input_fg, t.fg, 3) == 0);
|
||||
CHECK(memcmp(t.status, t.bg, 3) != 0);
|
||||
/* role text is legible on the page in both directions */
|
||||
CHECK(luma(t.accent) - luma(t.bg) >= 50000);
|
||||
CHECK(luma(t.agent) - luma(t.bg) >= 50000);
|
||||
CHECK(luma(t.tool) - luma(t.bg) >= 50000);
|
||||
CHECK(sp_theme_load("LIGHT", &t2, &err) == 0);
|
||||
CHECK(memcmp(t2.bg, "\xff\xff\xff", 3) == 0);
|
||||
/* X resources: the tight binding wins, 48-bit colours are read */
|
||||
CHECK(sp_theme_from_xrdb("*.background:\t#6e1e7d\n"
|
||||
"*.foreground:\t#acdcb4\n*.color12:\t#7f6c6c\n"
|
||||
|
||||
Reference in new issue
Block a user