Restart persistence. A daemon that forgets its own history on every restart is not one experiment. kreplay_events() reads the append-only event log once at startup and rebuilds the impulse ledger (with ids), token/cost/step totals, the last outcome, the stuck ladder, the blocked clock and the recent-start count. The engine's counters start at zero on each open; the surfaces add them to the restored base, so the dashboard does not reset. A restored blocked goal keeps its hour of quiet, three starts within five minutes back off, and an empty session with a non-empty event log is recorded as session_reset instead of silently looking new. The conductor. Before an impulse the daemon measures its own vitals (context against the window and the compaction threshold, cost, stuck level, turn failures, impulses since the last goal, the age of THREADS.md, session size) and lets five drives compete: survive, reflect, maintain, explore, goal. The winning drive, its reason and the vitals are injected into the prompt and shown on the dashboard. The model still chooses the action; the conductor chooses what the turn is about. This is the control architecture from the body/drives/conductor argument, borrowed without the consciousness claim. Session counting is now incremental: only bytes appended since the last look are scanned, the offset stops at the last newline so a torn tail is recounted, and a truncated file resets the count. Tests: test_replay (synthetic log + two kdaemon_run lifetimes, same birth, ledger ids 1 and 2, cumulative totals), test_mind (goal/maintain/explore/ survive/reflect, prompt and snapshot), test_krang's session counter (append, torn tail, truncation). make check is nine suites; ASAN is green; the root integration still passes on a fresh debootstrap. Docs: AGENTS.md, README.md and docs/DESIGN.md; the latter maps the video's blueprint (body, drives, self-model, conductor, continuous feedback) to krang.
423 lines
11 KiB
C
423 lines
11 KiB
C
/*
|
|
* krang -- offline unit tests: config, birth, stuck, snapshot, HTTP, MCP.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*/
|
|
#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
|
|
init_krang(struct krang *k)
|
|
{
|
|
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), "test the apparatus");
|
|
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->st.state, sizeof(k->st.state), "sleeping");
|
|
snprintf(k->st.goal, sizeof(k->st.goal), "test the apparatus");
|
|
snprintf(k->st.session, sizeof(k->st.session), "krang");
|
|
snprintf(k->st.sandbox_uid, sizeof(k->st.sandbox_uid), "1000");
|
|
snprintf(k->st.sandbox_gid, sizeof(k->st.sandbox_gid), "1000");
|
|
snprintf(k->st.landlock, sizeof(k->st.landlock), "applied");
|
|
snprintf(k->st.seccomp, sizeof(k->st.seccomp), "applied");
|
|
k->st.started_at = k_now();
|
|
k->st.born_at = k->st.started_at;
|
|
}
|
|
|
|
static void
|
|
test_config(void)
|
|
{
|
|
struct kconfig c;
|
|
char path[] = "/tmp/krang-test-conf-XXXXXX";
|
|
char err[512];
|
|
int fd;
|
|
FILE *f;
|
|
|
|
fd = mkstemp(path);
|
|
CHECK(fd >= 0);
|
|
if (fd < 0)
|
|
return;
|
|
f = fdopen(fd, "w");
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fprintf(f, "# a test\n");
|
|
fprintf(f, "goal = keep the apparatus honest # inline comment\n");
|
|
fprintf(f, "endpoint = http://127.0.0.1:1/v1\n");
|
|
fprintf(f, "model = test\n");
|
|
fprintf(f, "impulse_seconds = 7\n");
|
|
fprintf(f, "max_steps = 9\n");
|
|
fprintf(f, "dashboard_port = 18096\n");
|
|
fprintf(f, "max_turn_seconds = 42\n");
|
|
fprintf(f, "seccomp = no\n");
|
|
fclose(f);
|
|
CHECK(kconfig_load(&c, path, err, sizeof(err)) == 0);
|
|
CHECK(strcmp(c.goal, "keep the apparatus honest") == 0);
|
|
CHECK(c.impulse_seconds == 7);
|
|
CHECK(c.max_steps == 9);
|
|
CHECK(c.dashboard_port == 18096);
|
|
CHECK(c.max_turn_seconds == 42);
|
|
CHECK(c.seccomp == 0);
|
|
unlink(path);
|
|
}
|
|
|
|
static void
|
|
test_config_guards(void)
|
|
{
|
|
struct kconfig c;
|
|
char path[] = "/tmp/krang-guard-XXXXXX";
|
|
char err[512];
|
|
int fd;
|
|
FILE *f;
|
|
|
|
fd = mkstemp(path);
|
|
CHECK(fd >= 0);
|
|
if (fd < 0)
|
|
return;
|
|
f = fdopen(fd, "w");
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fprintf(f, "rootfs = /\n");
|
|
fclose(f);
|
|
CHECK(kconfig_load(&c, path, err, sizeof(err)) != 0);
|
|
CHECK(contains(err, "must not be /"));
|
|
|
|
f = fopen(path, "w");
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fprintf(f, "rootfs = /tmp/krang-guard-rootfs\n");
|
|
fprintf(f, "state_dir = /tmp/krang-guard-rootfs/state\n");
|
|
fprintf(f, "workspace = /tmp/krang-guard-workspace\n");
|
|
fclose(f);
|
|
CHECK(kconfig_load(&c, path, err, sizeof(err)) != 0);
|
|
CHECK(contains(err, "separate trees"));
|
|
|
|
f = fopen(path, "w");
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fprintf(f, "rootfs = /tmp/../krang-guard-rootfs\n");
|
|
fclose(f);
|
|
CHECK(kconfig_load(&c, path, err, sizeof(err)) != 0);
|
|
CHECK(contains(err, ".."));
|
|
unlink(path);
|
|
}
|
|
|
|
static void
|
|
test_birth(void)
|
|
{
|
|
struct kconfig c;
|
|
struct sp_buf b;
|
|
|
|
kconfig_defaults(&c);
|
|
snprintf(c.goal, sizeof(c.goal), "map the sandbox");
|
|
sp_buf_init(&b);
|
|
k_birth_prompt(&c, &b);
|
|
CHECK(contains(b.p, "You are krang. You are awake."));
|
|
CHECK(contains(b.p, "no user"));
|
|
CHECK(contains(b.p, "map the sandbox"));
|
|
CHECK(contains(b.p, "Good luck."));
|
|
CHECK(contains(b.p, KRANG_IN_WORKSPACE));
|
|
sp_buf_free(&b);
|
|
}
|
|
|
|
static void
|
|
test_stuck(void)
|
|
{
|
|
struct kstuck s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
kstuck_call(&s, "run", "{\"command\":\"false\"}");
|
|
kstuck_call(&s, "run", "{\"command\":\"false\"}");
|
|
CHECK(s.repeated_calls >= 1);
|
|
kstuck_result(&s, "run", "error: nope", 1);
|
|
kstuck_result(&s, "run", "error: nope", 1);
|
|
CHECK(s.same_error >= 2);
|
|
CHECK(kstuck_level(&s) >= 1);
|
|
CHECK(kstuck_should_check(&s, k_now()) >= 1);
|
|
CHECK(s.attempts == 1);
|
|
|
|
/* a successful tool turn ends the episode and resets the ladder */
|
|
kstuck_result(&s, "run", "[exit status 0]", 0);
|
|
kstuck_turn(&s, 0, 1, 0);
|
|
CHECK(s.attempts == 0);
|
|
CHECK(kstuck_level(&s) == 0);
|
|
|
|
/* a turn that ends on a failing tool is not progress, even if the
|
|
* tool is classed as mutating (run/write/edit) */
|
|
kstuck_result(&s, "run", "error: nope", 1);
|
|
kstuck_result(&s, "run", "error: nope", 1);
|
|
CHECK(kstuck_should_check(&s, k_now()) >= 1);
|
|
kstuck_result(&s, "run", "error: nope", 1);
|
|
kstuck_turn(&s, 0, 1, 1);
|
|
CHECK(s.attempts >= 1);
|
|
CHECK(kstuck_level(&s) >= 1);
|
|
kstuck_reset(&s);
|
|
CHECK(s.level == 0);
|
|
CHECK(s.attempts == 0);
|
|
}
|
|
|
|
static void
|
|
test_state(void)
|
|
{
|
|
struct krang k;
|
|
struct sp_buf b;
|
|
|
|
init_krang(&k);
|
|
sp_buf_init(&b);
|
|
kstate_json(&k, &b);
|
|
CHECK(contains(b.p, "\"state\":\"sleeping\""));
|
|
CHECK(contains(b.p, "\"goal\":\"test the apparatus\""));
|
|
CHECK(contains(b.p, "\"version\":\"0.1.0\""));
|
|
CHECK(contains(b.p, "\"sandbox\":"));
|
|
CHECK(contains(b.p, "\"config\":"));
|
|
sp_buf_free(&b);
|
|
|
|
sp_buf_init(&b);
|
|
kstate_impulses_json(&k, &b, 10);
|
|
CHECK(strcmp(b.p, "[]") == 0);
|
|
sp_buf_free(&b);
|
|
|
|
sp_buf_init(&b);
|
|
kstate_tools_json(&k, &b);
|
|
CHECK(strcmp(b.p, "[]") == 0);
|
|
sp_buf_free(&b);
|
|
pthread_mutex_destroy(&k.lock);
|
|
pthread_cond_destroy(&k.cond);
|
|
sp_buf_free(&k.err);
|
|
}
|
|
|
|
static void
|
|
test_birth_flow(void)
|
|
{
|
|
struct krang k;
|
|
struct sp_session_info si;
|
|
struct sp_buf err, logjson;
|
|
char tmpl[] = "/tmp/krang-birth-XXXXXX";
|
|
char *dir;
|
|
char goal[KRANG_GOAL];
|
|
int newborn = -1;
|
|
|
|
dir = mkdtemp(tmpl);
|
|
CHECK(dir != NULL);
|
|
if (dir == NULL)
|
|
return;
|
|
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);
|
|
sp_buf_init(&err);
|
|
kconfig_defaults(&k.cfg);
|
|
snprintf(k.cfg.goal, sizeof(k.cfg.goal), "be born");
|
|
snprintf(k.session_dir, sizeof(k.session_dir), "%s/sessions", dir);
|
|
(void)mkdir(k.session_dir, 0700);
|
|
{
|
|
char lp[700];
|
|
|
|
snprintf(lp, sizeof(lp), "%s/events.jsonl", dir);
|
|
CHECK(klog_open(&k.log, lp, k.last_error,
|
|
sizeof(k.last_error)) == 0);
|
|
}
|
|
CHECK(kdaemon_session_open(&k, &newborn, goal, sizeof(goal),
|
|
k.last_error, sizeof(k.last_error)) == 0);
|
|
CHECK(newborn == 1);
|
|
CHECK(strcmp(goal, "be born") == 0);
|
|
CHECK(k.st.born_at > 0);
|
|
CHECK(sp_session_open(&si, k.session_dir, "krang", &err) == 0);
|
|
CHECK(sp_session_append(&si, "user", "first breath", &err) == 0);
|
|
sp_buf_free(&err);
|
|
|
|
newborn = -1;
|
|
CHECK(kdaemon_session_open(&k, &newborn, goal, sizeof(goal),
|
|
k.last_error, sizeof(k.last_error)) == 0);
|
|
CHECK(newborn == 0);
|
|
CHECK(strcmp(goal, "be born") == 0);
|
|
|
|
snprintf(k.cfg.goal, sizeof(k.cfg.goal), "a different goal");
|
|
CHECK(kdaemon_session_open(&k, &newborn, goal, sizeof(goal),
|
|
k.last_error, sizeof(k.last_error)) == 0);
|
|
CHECK(strcmp(goal, "be born") == 0);
|
|
sp_buf_init(&logjson);
|
|
klog_json(&k.log, &logjson, 20);
|
|
CHECK(contains(logjson.p, "config_goal_ignored"));
|
|
sp_buf_free(&logjson);
|
|
klog_close(&k.log);
|
|
pthread_mutex_destroy(&k.lock);
|
|
pthread_cond_destroy(&k.cond);
|
|
sp_buf_free(&k.err);
|
|
}
|
|
|
|
static void
|
|
test_session_count(void)
|
|
{
|
|
struct krang k;
|
|
char tmpl[] = "/tmp/krang-count-XXXXXX";
|
|
char *dir, path[800];
|
|
size_t records = 0;
|
|
unsigned long long bytes = 0;
|
|
FILE *f;
|
|
|
|
dir = mkdtemp(tmpl);
|
|
CHECK(dir != NULL);
|
|
if (dir == NULL)
|
|
return;
|
|
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.session_dir, sizeof(k.session_dir), "%s/sessions", dir);
|
|
(void)mkdir(k.session_dir, 0700);
|
|
snprintf(path, sizeof(path), "%s/krang.jsonl", k.session_dir);
|
|
f = fopen(path, "w");
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fprintf(f, "{\"role\":\"user\",\"text\":\"a\"}\n");
|
|
fprintf(f, "{\"role\":\"assistant\",\"text\":\"b\"}\n");
|
|
fprintf(f, "{\"role\":\"user\",\"text\":\"c\"}\n");
|
|
fclose(f);
|
|
CHECK(ksession_meta(&k, &records, &bytes, NULL, 0, NULL, 0) == 0);
|
|
CHECK(records == 3);
|
|
CHECK(bytes > 0);
|
|
|
|
f = fopen(path, "a");
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fprintf(f, "{\"role\":\"assistant\",\"text\":\"d\"}\n");
|
|
fprintf(f, "{\"role\":\"user\",\"text\":\"e\"}\n");
|
|
fclose(f);
|
|
CHECK(ksession_meta(&k, &records, &bytes, NULL, 0, NULL, 0) == 0);
|
|
CHECK(records == 5);
|
|
|
|
/* a torn tail is not a record until its newline arrives */
|
|
f = fopen(path, "a");
|
|
fprintf(f, "{\"role\":\"assistant\"");
|
|
fclose(f);
|
|
CHECK(ksession_meta(&k, &records, &bytes, NULL, 0, NULL, 0) == 0);
|
|
CHECK(records == 5);
|
|
f = fopen(path, "a");
|
|
fprintf(f, ",\"text\":\"f\"}\n");
|
|
fclose(f);
|
|
CHECK(ksession_meta(&k, &records, &bytes, NULL, 0, NULL, 0) == 0);
|
|
CHECK(records == 6);
|
|
|
|
/* a truncated file drops the old count, it does not keep it */
|
|
f = fopen(path, "w");
|
|
fprintf(f, "{\"role\":\"user\",\"text\":\"only\"}\n");
|
|
fclose(f);
|
|
CHECK(ksession_meta(&k, &records, &bytes, NULL, 0, NULL, 0) == 0);
|
|
CHECK(records == 1);
|
|
pthread_mutex_destroy(&k.lock);
|
|
pthread_cond_destroy(&k.cond);
|
|
sp_buf_free(&k.err);
|
|
}
|
|
|
|
static void
|
|
test_http(void)
|
|
{
|
|
struct krang k;
|
|
struct sp_buf b;
|
|
|
|
init_krang(&k);
|
|
sp_buf_init(&b);
|
|
khttp_handle_buffer(&k, "GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n",
|
|
&b);
|
|
CHECK(contains(b.p, "200 OK"));
|
|
CHECK(contains(b.p, "\"ok\":true"));
|
|
sp_buf_free(&b);
|
|
|
|
sp_buf_init(&b);
|
|
khttp_handle_buffer(&k, "GET /api/state HTTP/1.1\r\nHost: localhost\r\n\r\n",
|
|
&b);
|
|
CHECK(contains(b.p, "200 OK"));
|
|
CHECK(contains(b.p, "\"state\":\"sleeping\""));
|
|
sp_buf_free(&b);
|
|
|
|
sp_buf_init(&b);
|
|
khttp_handle_buffer(&k, "GET /nope HTTP/1.1\r\nHost: localhost\r\n\r\n", &b);
|
|
CHECK(contains(b.p, "404 Not Found"));
|
|
sp_buf_free(&b);
|
|
pthread_mutex_destroy(&k.lock);
|
|
pthread_cond_destroy(&k.cond);
|
|
sp_buf_free(&k.err);
|
|
}
|
|
|
|
static void
|
|
test_mcp(void)
|
|
{
|
|
struct krang k;
|
|
char *reply;
|
|
|
|
init_krang(&k);
|
|
kmcp_prepare(&k);
|
|
reply = kmcp_handle(&k,
|
|
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\"}");
|
|
CHECK(reply != NULL);
|
|
CHECK(contains(reply, "krang_state"));
|
|
CHECK(contains(reply, "krang_say"));
|
|
free(reply);
|
|
reply = kmcp_handle(&k,
|
|
"{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\","
|
|
"\"params\":{\"name\":\"krang_state\",\"arguments\":{}}}");
|
|
CHECK(reply != NULL);
|
|
CHECK(contains(reply, "sleeping"));
|
|
free(reply);
|
|
pthread_mutex_destroy(&k.lock);
|
|
pthread_cond_destroy(&k.cond);
|
|
sp_buf_free(&k.err);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
test_config();
|
|
test_config_guards();
|
|
test_birth();
|
|
test_stuck();
|
|
test_birth_flow();
|
|
test_state();
|
|
test_session_count();
|
|
test_http();
|
|
test_mcp();
|
|
if (failures != 0) {
|
|
fprintf(stderr, "%d test(s) failed\n", failures);
|
|
return 1;
|
|
}
|
|
printf("test_krang: ok\n");
|
|
return 0;
|
|
}
|