Files
krang/tests/test_daemon.c
T
lotherk fb6f97ba8e krang: autonomous agent daemon on libsplinter
One fixed session, born once, kept and compacted forever. The daemon wakes on
its own impulse, works on a persistent goal, detects repeated failures,
reflects, changes approach, and records everything. It starts as root, builds
a debootstrap chroot, then drops to an unprivileged uid and applies Landlock
and seccomp. Dashboard, HTTP API and MCP expose the same snapshot; the event
log is root-owned and append-only. Offline suites cover the engine, the daemon,
both sandbox layers and the dashboard/MCP sockets; a root-only script covers
debootstrap, chroot, uid drop and plugin loading.
2026-11-21 17:00:12 +00:00

131 lines
3.5 KiB
C

/*
* krang -- offline end-to-end daemon test.
*
* Copyright (c) 2026 Konrad Lother
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Runs kdaemon_run itself: temp state/workspace, script transport, exactly one
* impulse (the birth), no provider, no network, no root. The dashboard and MCP
* listeners are bound to ephemeral loopback ports and stopped by the daemon.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "krang.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
#cond); \
failures++; \
} \
} while (0)
static int
contains(const char *hay, const char *needle)
{
return hay != NULL && strstr(hay, needle) != NULL;
}
static void
read_file(const char *path, char *buf, size_t cap)
{
FILE *f;
size_t n;
buf[0] = '\0';
f = fopen(path, "r");
if (f == NULL)
return;
n = fread(buf, 1, cap - 1, f);
buf[n] = '\0';
fclose(f);
}
int
main(void)
{
struct krang k;
char tmpl[] = "/tmp/krang-daemon-XXXXXX";
char *dir;
char path[800], logbuf[65536];
size_t records = 0;
unsigned long long bytes = 0;
int rc;
dir = mkdtemp(tmpl);
CHECK(dir != NULL);
if (dir == NULL)
return 1;
memset(&k, 0, sizeof(k));
k.log.fd = -1;
pthread_mutex_init(&k.lock, NULL);
pthread_cond_init(&k.cond, NULL);
sp_buf_init(&k.err);
kconfig_defaults(&k.cfg);
snprintf(k.cfg.goal, sizeof(k.cfg.goal), "be born once");
snprintf(k.cfg.endpoint, sizeof(k.cfg.endpoint),
"http://127.0.0.1:1/v1");
snprintf(k.cfg.model, sizeof(k.cfg.model), "test-model");
snprintf(k.cfg.state_dir, sizeof(k.cfg.state_dir), "%s/state", dir);
snprintf(k.cfg.workspace, sizeof(k.cfg.workspace), "%s/workspace", dir);
snprintf(k.cfg.rootfs, sizeof(k.cfg.rootfs), "%s/rootfs", dir);
snprintf(k.session_dir, sizeof(k.session_dir), "%s/sessions", dir);
snprintf(k.script, sizeof(k.script), "%s/script.txt", dir);
snprintf(k.cfg.dashboard_host, sizeof(k.cfg.dashboard_host),
"127.0.0.1");
snprintf(k.cfg.mcp_host, sizeof(k.cfg.mcp_host), "127.0.0.1");
k.cfg.dashboard_port = 0;
k.cfg.mcp_port = 0;
k.max_impulses = 1;
(void)mkdir(k.cfg.state_dir, 0755);
(void)mkdir(k.cfg.workspace, 0700);
(void)mkdir(k.session_dir, 0700);
snprintf(path, sizeof(path), "%s/events.jsonl", k.cfg.state_dir);
CHECK(klog_open(&k.log, path, k.last_error,
sizeof(k.last_error)) == 0);
{
FILE *f = fopen(k.script, "w");
CHECK(f != NULL);
if (f == NULL)
return 1;
fprintf(f, "tool echo {\"text\":\"daemon\"}\n");
fprintf(f, "answer daemon-birth-ok\n");
fclose(f);
}
rc = kdaemon_run(&k);
CHECK(rc == 0);
CHECK(k.st.impulses == 1);
CHECK(k.st.turns == 1);
CHECK(k.st.born_at > 0);
CHECK(contains(k.st.last_outcome, "daemon-birth-ok"));
CHECK(k.st.session_records >= 3);
CHECK(k.log.fd == -1); /* kdaemon_run closed it */
(void)ksession_meta(&k, &records, &bytes, NULL, 0, NULL, 0);
CHECK(records >= 3);
CHECK(bytes > 0);
snprintf(path, sizeof(path), "%s/events.jsonl", k.cfg.state_dir);
read_file(path, logbuf, sizeof(logbuf));
CHECK(contains(logbuf, "\"kind\":\"birth\""));
CHECK(contains(logbuf, "\"kind\":\"turn_end\""));
CHECK(contains(logbuf, "\"kind\":\"stop\""));
CHECK(contains(logbuf, "daemon-birth-ok"));
pthread_mutex_destroy(&k.lock);
pthread_cond_destroy(&k.cond);
sp_buf_free(&k.err);
if (failures != 0) {
fprintf(stderr, "%d test(s) failed\n", failures);
return 1;
}
printf("test_daemon: ok\n");
return 0;
}