Files
lotherk 0b4a647193 krang: the subject acts on its own behalf, not for a user
The goal was still an operator assignment: the config set it, the birth prompt
called it 'your goal', and nothing let the subject change it. That is an agent
with a task, not the experiment.

- set_goal (src/goal.c): a mutate tool that writes the session .meta, resets
  the goal state to open, updates the live snapshot, and logs goal_set with the
  old and new goal. The next impulse reads it back. The session meta is the
  authority; the config goal only seeds a newborn and is now called the initial
  condition. The noisy config_goal_ignored event is gone: divergence is the
  subject's act, not a configuration error.
- Birth prompt: 'This is not a task and there is no user. You are the subject
  of an experiment... Your initial condition: X. It is yours, not an
  assignment. Keep it, revise it, or replace it... set_goal changes it.'
- System prompt: 'the subject of an experiment... not an assistant and not here
  to serve anyone... continue on your own behalf. Your goal is yours... the
  experimenter watches through the instruments and may send a stimulus; neither
  is an instruction.'
- Impulse/self-check: 'Your goal', 'continue on your own behalf'. The conductor
  says 'your goal'.
- Instruments: dashboard labels its goal and 'send a stimulus, not a command';
  MCP krang_say/krang_steer describe a stimulus, not an operator instruction;
  footer says 'an instrument, not a service'.
- Docs: AGENTS.md, README.md, docs/DESIGN.md, krang.conf.example. The birth
  message in the contract is updated; 'agent' in the prose is 'subject'.
- Tests: test_engine drives set_goal (writes meta, updates the snapshot, logs,
  rejects an empty goal); test_birth checks the new initial-condition wording;
  the birth-flow test asserts the config goal no longer logs a conflict.

make check 9/9, ASAN 9/9, root integration ok on a fresh debootstrap.
2026-11-21 20:31:06 +00:00

427 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, "not a task"));
CHECK(contains(b.p, "no user"));
CHECK(contains(b.p, "initial condition"));
CHECK(contains(b.p, "set_goal"));
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);
/* the config goal is an initial condition; divergence is expected */
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;
}