Files
krang/tests/test_engine.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

141 lines
3.6 KiB
C

/*
* krang -- offline integration test: one scripted turn through the engine.
*
* Copyright (c) 2026 Konrad Lother
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* No network, no key, no model. The mock transport replays the script; the
* session and the event log are real.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "krang.h"
#include "i18n.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;
}
int
main(void)
{
struct krang k;
char tmpl[512], *dir, script[700], logpath[800], answer[KRANG_GOAL];
size_t records = 0;
unsigned long long bytes = 0;
FILE *f;
dir = mkdtemp(strcpy(tmpl, "/tmp/krang-engine-XXXXXX"));
CHECK(dir != NULL);
if (dir == NULL)
return 1;
(void)chdir(dir);
(void)setenv("HOME", dir, 1);
(void)sp_i18n_init("en");
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), "prove the engine");
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.plugin[0], sizeof(k.cfg.plugin[0]), "%s",
KRANG_TEST_PLUGIN);
k.cfg.nplugins = 1;
snprintf(k.session_dir, sizeof(k.session_dir), "%s/sessions", dir);
(void)mkdir(k.session_dir, 0700);
snprintf(logpath, sizeof(logpath), "%s/events.jsonl", dir);
CHECK(klog_open(&k.log, logpath, k.last_error,
sizeof(k.last_error)) == 0);
{
struct sp_buf sp;
sp_buf_init(&sp);
k_system_prompt(&k, &sp);
snprintf(k.system_prompt, sizeof(k.system_prompt), "%s",
sp.p != NULL ? sp.p : "");
sp_buf_free(&sp);
}
snprintf(script, sizeof(script), "%s/script.txt", dir);
f = fopen(script, "w");
CHECK(f != NULL);
if (f == NULL)
return 1;
fprintf(f, "tool echo {\"text\":\"from-script\"}\n");
fprintf(f, "answer hello-from-script\n");
fclose(f);
CHECK(kengine_open(&k, script, k.last_error,
sizeof(k.last_error)) == 0);
CHECK(k.eng != NULL);
if (k.eng != NULL) {
CHECK(sp_registry_find(sp_engine_registry(k.eng),
"clock") != NULL);
int rc = krun_turn(&k, "prove the apparatus", "test", NULL);
CHECK(rc == 0);
snprintf(answer, sizeof(answer), "%s", k.st.last_outcome);
CHECK(contains(answer, "hello-from-script"));
CHECK(contains(k.st.last_tool, "echo"));
CHECK(k.st.tool_calls >= 1);
(void)ksession_meta(&k, &records, &bytes, NULL, 0, NULL, 0);
CHECK(records >= 3);
CHECK(bytes > 0);
{
const struct sp_tool *jt = sp_registry_find(
sp_engine_registry(k.eng), "journal_search");
struct sp_buf jb;
CHECK(jt != NULL);
if (jt != NULL) {
sp_buf_init(&jb);
CHECK(jt->run("{\"query\":"
"\"hello-from-script\"}", &jb,
jt->ctx) == 0);
CHECK(contains(jb.p, "hello-from-script"));
sp_buf_free(&jb);
}
}
}
{
struct sp_buf b;
sp_buf_init(&b);
kstate_json(&k, &b);
CHECK(contains(b.p, "\"session\":"));
CHECK(contains(b.p, "\"tool_calls\":"));
sp_buf_free(&b);
}
kengine_close(&k);
klog_close(&k.log);
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_engine: ok\n");
return 0;
}