Files
splinter/src/swarm.c
T
lotherkandClaude Opus 5.5 b05bb34cc7 ui: a tool call reads like a call; a joined swarm session stays usable
The transcript said

  [23:12:05] <werkzeug> run  command: touch /tmp/.../made

which is the harness talking about itself. Hermes prints one row per call -
emoji, a capitalized verb, a one-line preview of the primary argument
(agent/display.py builds it per tool, 80 chars) and a duration; its long
outputs end in "... (N more lines)". opencode's TUI does the same shape: a
header line for the call, the output indented underneath.

So the row is ">> Run  touch /tmp/.../made" (verb capitalized, no
"command:" and no JSON for a shell call), and the observation hangs under it
as "| output", cut at 4 KiB with "... N lines". The log keeps the rest.

Also: the swarm master never sent "unlock", so a second splinter in the same
directory sat on "[swarm locked]" and swallowed every key - one of the
intermittent failures in the suite. Joining unlocks; the master locks a slave
only while it routes work to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 06:42:10 +00:00

444 lines
11 KiB
C

/*
* splinter -- swarm: fleet of instances in one working directory.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#include "swarm.h"
static void
set_nb(int fd)
{
int fl = fcntl(fd, F_GETFL, 0);
if (fl >= 0)
(void)fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}
static int
send_line(int fd, const char *s)
{
size_t n = strlen(s);
return write(fd, s, n) == (ssize_t)n && write(fd, "\n", 1) == 1 ? 0 : -1;
}
static int
json_get_int(const char *s, const char *key, int def)
{
char pat[64];
const char *p;
snprintf(pat, sizeof(pat), "\"%s\"", key);
p = strstr(s, pat);
if (!p) return def;
p = strchr(p, ':');
if (!p) return def;
return (int)strtol(p + 1, NULL, 10);
}
static const char *
json_get_str(const char *s, const char *key, char *buf, size_t cap)
{
char pat[64];
const char *p, *q;
size_t n;
buf[0] = '\0';
snprintf(pat, sizeof(pat), "\"%s\"", key);
p = strstr(s, pat);
if (!p) return buf;
p = strchr(p, ':');
if (!p) return buf;
p = strchr(p, '"');
if (!p) return buf;
p++;
q = strchr(p, '"');
if (!q) return buf;
n = (size_t)(q - p);
if (n >= cap) n = cap - 1;
memcpy(buf, p, n);
buf[n] = '\0';
return buf;
}
static void
path_for(struct swarm *s, const char *cwd)
{
snprintf(s->path, sizeof(s->path), "%s/%s", cwd && *cwd ? cwd : ".", SWARM_PATH);
}
/* No unlink before bind: if a master is alive the bind fails with EADDRINUSE
* and the caller connects as a slave instead; a stale socket is removed by
* swarm_init only after the connect failed. */
static int
try_become_master(struct swarm *s)
{
struct sockaddr_un a;
int fd;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) return -1;
memset(&a, 0, sizeof(a));
a.sun_family = AF_UNIX;
snprintf(a.sun_path, sizeof(a.sun_path), "%.107s", s->path);
if (bind(fd, (struct sockaddr *)&a, sizeof(a)) != 0) {
close(fd);
return -1;
}
(void)chmod(s->path, 0600);
if (listen(fd, 8) != 0) { close(fd); unlink(s->path); return -1; }
set_nb(fd);
s->listen_fd = fd;
s->role = SWARM_MASTER;
s->id = 0;
s->npeers = 0;
return 0;
}
static int
try_connect_master(struct swarm *s)
{
struct sockaddr_un a;
int fd;
char msg[128];
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) return -1;
memset(&a, 0, sizeof(a));
a.sun_family = AF_UNIX;
snprintf(a.sun_path, sizeof(a.sun_path), "%.107s", s->path);
if (connect(fd, (struct sockaddr *)&a, sizeof(a)) != 0) {
close(fd);
return -1;
}
set_nb(fd);
snprintf(msg, sizeof(msg), "{\"t\":\"hello\",\"pid\":%d}", (int)getpid());
send_line(fd, msg);
s->master_fd = fd;
s->role = SWARM_SLAVE;
s->id = -1;
s->locked = 1; /* taken over until master says otherwise / by default slave */
return 0;
}
int
swarm_init(struct swarm *s, const char *cwd)
{
memset(s, 0, sizeof(*s));
s->listen_fd = -1;
s->master_fd = -1;
s->role = SWARM_NONE;
s->id = -1;
signal(SIGPIPE, SIG_IGN);
path_for(s, cwd);
if (try_become_master(s) == 0)
return 0;
/* stale socket? try connect; if connect fails, remove and retry bind */
if (try_connect_master(s) == 0)
return 0;
unlink(s->path);
if (try_become_master(s) == 0)
return 0;
return -1; /* standalone */
}
static struct swarm_peer *
peer_by_id(struct swarm *s, int id)
{
int i;
for (i = 0; i < s->npeers; i++)
if (s->peers[i].alive && s->peers[i].id == id)
return &s->peers[i];
return NULL;
}
static void
master_accept(struct swarm *s)
{
int fd, i, id = 1;
struct swarm_peer *p;
char msg[128];
if (s->listen_fd < 0) return;
fd = accept(s->listen_fd, NULL, NULL);
if (fd < 0) return;
set_nb(fd);
/* find free id, 0 is master */
for (id = 1; id < 1000; id++) {
int used = 0;
for (i = 0; i < s->npeers; i++)
if (s->peers[i].alive && s->peers[i].id == id) used = 1;
if (!used) break;
}
if (s->npeers < SWARM_MAX_PEERS) p = &s->peers[s->npeers++];
else { /* reuse dead slot */
p = NULL;
for (i = 0; i < s->npeers; i++) if (!s->peers[i].alive) { p = &s->peers[i]; break; }
if (!p) { close(fd); return; }
}
memset(p, 0, sizeof(*p));
p->fd = fd; p->id = id; p->alive = 1; p->busy = 0;
snprintf(msg, sizeof(msg), "{\"t\":\"welcome\",\"id\":%d,\"master\":%d}", id, (int)getpid());
send_line(fd, msg);
/*
* And immediately let it go. A joined session used to stay locked for
* good - nothing ever sent "unlock" - so a second splinter in the same
* directory sat on "[swarm locked]" and swallowed every key. A session is
* only locked while the fleet actually runs work on it.
*/
send_line(fd, "{\"t\":\"unlock\"}");
}
static void
master_handle_line(struct swarm *s, struct swarm_peer *p, const char *line)
{
(void)s;
if (strstr(line, "\"hello\"")) {
p->pid = json_get_int(line, "pid", 0);
} else if (strstr(line, "\"status\"")) {
p->busy = json_get_int(line, "busy", p->busy);
} else if (strstr(line, "\"pong\"")) {
p->alive = 1;
}
}
static void
slave_handle_line(struct swarm *s, const char *line)
{
char text[3800];
if (strstr(line, "\"welcome\"")) {
s->id = json_get_int(line, "id", s->id);
s->locked = 1;
} else if (strstr(line, "\"exec\"")) {
json_get_str(line, "text", text, sizeof(text));
if (text[0]) {
snprintf(s->inbox, sizeof(s->inbox), "%s", text);
s->has_inbox = 1;
s->inbox_remote = 1;
}
} else if (strstr(line, "\"unlock\"")) {
s->locked = 0;
} else if (strstr(line, "\"lock\"")) {
s->locked = 1;
} else if (strstr(line, "\"ping\"")) {
send_line(s->master_fd, "{\"t\":\"pong\"}");
} else if (strstr(line, "\"result\"") || strstr(line, "\"event\"")) {
/* master receives results in its own inbox/handling path as needed */
}
}
static void
process_line(struct swarm *s, int is_master, struct swarm_peer *peer, char *line)
{
size_t l = strlen(line);
while (l > 0 && (line[l - 1] == '\r' || line[l - 1] == '\n'))
line[--l] = '\0';
if (l == 0)
return;
if (is_master && peer)
master_handle_line(s, peer, line);
else if (!is_master)
slave_handle_line(s, line);
}
static void
read_lines_fd(struct swarm *s, int fd, int is_master, struct swarm_peer *peer)
{
char buf[1024];
char *ibuf;
size_t *ilen, cap;
ssize_t n;
char *nl;
if (peer) { ibuf = peer->ibuf; ilen = &peer->ilen; cap = sizeof(peer->ibuf); }
else { ibuf = s->mibuf; ilen = &s->milen; cap = sizeof(s->mibuf); }
n = read(fd, buf, sizeof(buf));
if (n == 0) {
if (peer) { peer->alive = 0; if (peer->fd >= 0) close(peer->fd); peer->fd = -1; }
else if (s->master_fd >= 0) { close(s->master_fd); s->master_fd = -1; s->role = SWARM_NONE; }
return;
}
if (n < 0) return;
if (*ilen + (size_t)n >= cap) *ilen = 0; /* overflow: drop partial */
memcpy(ibuf + *ilen, buf, (size_t)n);
*ilen += (size_t)n;
ibuf[*ilen] = '\0';
while ((nl = memchr(ibuf, '\n', *ilen)) != NULL) {
*nl = '\0';
process_line(s, is_master, peer, ibuf);
{
size_t rest = *ilen - (size_t)(nl - ibuf) - 1;
memmove(ibuf, nl + 1, rest);
*ilen = rest;
ibuf[*ilen] = '\0';
}
}
}
void
swarm_poll(struct swarm *s)
{
int i;
if (!s || s->role == SWARM_NONE) return;
if (s->role == SWARM_MASTER) {
master_accept(s);
for (i = 0; i < s->npeers; i++) {
if (s->peers[i].alive && s->peers[i].fd >= 0)
read_lines_fd(s, s->peers[i].fd, 1, &s->peers[i]);
}
} else if (s->role == SWARM_SLAVE && s->master_fd >= 0) {
read_lines_fd(s, s->master_fd, 0, NULL);
}
}
static void
json_escape(char *dst, size_t cap, const char *src)
{
size_t i = 0, j = 0;
unsigned char c;
if (cap == 0) return;
while (src[i] != '\0' && j + 6 < cap) {
c = (unsigned char)src[i++];
if (c == '"' || c == '\\') { dst[j++]='\\'; dst[j++]=(char)c; }
else if (c == '\n') { dst[j++]='\\'; dst[j++]='n'; }
else if (c == '\r') { dst[j++]='\\'; dst[j++]='r'; }
else if (c == '\t') { dst[j++]='\\'; dst[j++]='t'; }
else if (c < 0x20) { j += snprintf(dst+j, cap-j, "\\u%04x", c); }
else dst[j++] = (char)c;
}
dst[j] = '\0';
}
int
swarm_route(struct swarm *s, int id, const char *text)
{
struct swarm_peer *p;
char msg[4096];
int best = -1, best_busy = 1<<30, i;
if (!s || s->role != SWARM_MASTER || !text) return -1;
if (id < 0) {
/* least busy slave */
for (i = 0; i < s->npeers; i++) {
if (!s->peers[i].alive) continue;
if (s->peers[i].busy < best_busy) { best_busy = s->peers[i].busy; best = s->peers[i].id; }
}
id = best;
}
if (id == 0) {
/* route to master self: put in inbox */
snprintf(s->inbox, sizeof(s->inbox), "%s", text);
s->has_inbox = 1;
s->inbox_remote = 1;
return 0;
}
p = peer_by_id(s, id);
if (!p || p->fd < 0) return -1;
{
char esc[3500];
json_escape(esc, sizeof(esc), text);
if (text[0] == '\0') {
snprintf(msg, sizeof(msg), "{\"t\":\"unlock\"}");
send_line(p->fd, msg);
return id;
}
/* the slave is busy with fleet work: no local input until it is done */
send_line(p->fd, "{\"t\":\"lock\"}");
snprintf(msg, sizeof(msg), "{\"t\":\"exec\",\"text\":\"%s\"}", esc);
send_line(p->fd, msg);
}
return id;
}
void
swarm_list(struct swarm *s, char *buf, size_t cap)
{
int i, off = 0;
if (!s || cap == 0) return;
buf[0] = '\0';
if (s->role == SWARM_NONE) { snprintf(buf, cap, "swarm: standalone (no fleet)"); return; }
off += snprintf(buf+off, cap-off, "0: master (self)%s\n", s->role==SWARM_MASTER?"":"");
if (s->role == SWARM_MASTER) {
for (i = 0; i < s->npeers && off+2 < (int)cap; i++) {
if (!s->peers[i].alive) continue;
off += snprintf(buf+off, cap-off, "%d: slave pid=%d busy=%d%s\n",
s->peers[i].id, s->peers[i].pid, s->peers[i].busy,
s->peers[i].fd>=0?"":" (dc)");
}
} else {
off += snprintf(buf+off, cap-off, "%d: this slave (locked=%d)\n", s->id, s->locked);
}
}
void
swarm_report_busy(struct swarm *s, int busy)
{
char msg[64];
if (!s || s->role != SWARM_SLAVE || s->master_fd < 0) return;
snprintf(msg, sizeof(msg), "{\"t\":\"status\",\"busy\":%d}", busy);
send_line(s->master_fd, msg);
}
int
swarm_take_inbox(struct swarm *s, char *buf, size_t cap)
{
if (!s || !s->has_inbox) return 0;
snprintf(buf, cap, "%s", s->inbox);
s->inbox[0] = '\0';
s->has_inbox = 0;
/* inbox_remote remains readable until next set; caller can check before/after? */
return 1;
}
int
swarm_inbox_remote(struct swarm *s)
{
int r;
if (!s) return 0;
r = s->inbox_remote;
s->inbox_remote = 0;
return r;
}
void
swarm_status(struct swarm *s, char *buf, size_t cap)
{
int alive = 0, i;
if (!s || s->role == SWARM_NONE) { if (cap) buf[0] = '\0'; return; }
if (s->role == SWARM_MASTER) {
for (i=0;i<s->npeers;i++) if (s->peers[i].alive) alive++;
/* nobody joined: nothing to say */
if (alive == 0 && cap > 0)
buf[0] = '\0';
else
snprintf(buf, cap, "swarm: master 0 (%d slaves)", alive);
} else {
snprintf(buf, cap, "swarm: slave %d%s", s->id, s->locked?" [locked]":"");
}
}
void
swarm_close(struct swarm *s)
{
int i;
if (!s) return;
if (s->role == SWARM_MASTER) {
for (i=0;i<s->npeers;i++) if (s->peers[i].fd>=0) close(s->peers[i].fd);
if (s->listen_fd>=0) { close(s->listen_fd); unlink(s->path); }
} else if (s->role == SWARM_SLAVE) {
if (s->master_fd>=0) close(s->master_fd);
}
s->role = SWARM_NONE;
s->listen_fd = s->master_fd = -1;
}