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.
259 lines
7.7 KiB
C
259 lines
7.7 KiB
C
/*
|
|
* krang -- restarting without starting a new experiment.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* Part one replays a synthetic event log through kreplay_events(). Part two
|
|
* runs kdaemon_run() twice with a fresh struct each time and proves that the
|
|
* second life continues the first: same birth, same ledger ids, cumulative
|
|
* totals.
|
|
*/
|
|
#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, const char *dir)
|
|
{
|
|
char path[800];
|
|
|
|
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), "survive a restart");
|
|
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->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;
|
|
snprintf(k->session_dir, sizeof(k->session_dir), "%s/sessions", dir);
|
|
snprintf(k->script, sizeof(k->script), "%s/script.txt", dir);
|
|
(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);
|
|
}
|
|
|
|
static void
|
|
test_synthetic(void)
|
|
{
|
|
struct krang w, r;
|
|
char tmpl[] = "/tmp/krang-replay-XXXXXX";
|
|
char *dir;
|
|
struct sp_buf b;
|
|
|
|
dir = mkdtemp(tmpl);
|
|
CHECK(dir != NULL);
|
|
if (dir == NULL)
|
|
return;
|
|
init_krang(&w, dir);
|
|
klog_emit(&w.log, "start", "newborn", "0", "session", "krang", NULL);
|
|
klog_emit(&w.log, "impulse_start", "impulse", "1", "cause", "birth",
|
|
"prompt", "wake up", NULL);
|
|
klog_emit(&w.log, "tool", "name", "run",
|
|
"args", "{\"command\":\"true\"}", NULL);
|
|
klog_emit(&w.log, "tool_result", "name", "run", "ok", "1",
|
|
"obs", "", NULL);
|
|
klog_emit(&w.log, "turn_end", "rc", "0", "steps", "1", "calls", "1",
|
|
"tokens_in", "100", "tokens_out", "20", "cost", "0.010000",
|
|
"impulse", "1", "compactions", "0", "cause", "birth",
|
|
"answer", "first-answer", "error", "", NULL);
|
|
klog_emit(&w.log, "impulse_start", "impulse", "2", "cause", "timer",
|
|
"prompt", "continue", NULL);
|
|
klog_emit(&w.log, "tool", "name", "write",
|
|
"args", "{\"path\":\"/var/lib/krang/workspace/self/SYSTEM.md\"}",
|
|
NULL);
|
|
klog_emit(&w.log, "tool_result", "name", "write", "ok", "0",
|
|
"obs", "error: denied", NULL);
|
|
klog_emit(&w.log, "turn_end", "rc", "-1", "steps", "2", "calls", "1",
|
|
"tokens_in", "50", "tokens_out", "10", "cost", "0.005000",
|
|
"impulse", "2", "compactions", "1", "cause", "timer",
|
|
"answer", "", "error", "boom", NULL);
|
|
klog_emit(&w.log, "stuck", "level", "2", "attempts", "1", NULL);
|
|
klog_emit(&w.log, "blocked", "reason", "same approach", NULL);
|
|
klog_emit(&w.log, "chat_in", "id", "1", "text", "hello", NULL);
|
|
klog_emit(&w.log, "chat_out", "rc", "0", "answer", "hi", NULL);
|
|
klog_emit(&w.log, "goal_complete", "goal", "survive a restart", NULL);
|
|
klog_close(&w.log);
|
|
|
|
init_krang(&r, dir);
|
|
CHECK(kreplay_events(&r, r.last_error, sizeof(r.last_error)) == 0);
|
|
CHECK(r.restored.impulses == 2);
|
|
CHECK(r.restored.self_impulses == 2);
|
|
CHECK(r.restored.external_impulses == 0);
|
|
CHECK(r.restored.turns == 2);
|
|
CHECK(r.restored.steps == 3);
|
|
CHECK(r.restored.tool_calls == 2);
|
|
CHECK(r.restored.tool_failures == 1);
|
|
CHECK(r.restored.self_edits == 1);
|
|
CHECK(r.restored.tokens_in == 150);
|
|
CHECK(r.restored.tokens_out == 30);
|
|
CHECK(r.restored.cost > 0.0149 && r.restored.cost < 0.0151);
|
|
CHECK(r.restored.compactions == 1);
|
|
CHECK(r.restored.blocked_events == 1);
|
|
CHECK(r.restored.chat_in == 1);
|
|
CHECK(r.restored.chat_out == 1);
|
|
CHECK(r.restored.goal_completes == 1);
|
|
CHECK(r.restored.last_turn_rc == -1);
|
|
CHECK(contains(r.restored.last_outcome, "error: boom"));
|
|
CHECK(contains(r.restored.last_error, "boom"));
|
|
CHECK(r.impulse_seq == 2);
|
|
CHECK(r.impulses[0].id == 1);
|
|
CHECK(r.impulses[0].tokens_in == 100);
|
|
CHECK(r.impulses[0].tokens_out == 20);
|
|
CHECK(contains(r.impulses[0].answer, "first-answer"));
|
|
CHECK(r.impulses[1].id == 2);
|
|
CHECK(r.impulses[1].rc == -1);
|
|
CHECK(r.impulses[1].tool_calls == 1);
|
|
CHECK(r.stuck.level == 2);
|
|
CHECK(r.stuck.attempts == 1);
|
|
CHECK(r.st.stuck_level == 2);
|
|
CHECK(r.last_blocked_at > 0);
|
|
CHECK(r.recent_starts == 1);
|
|
CHECK(r.st.impulses == 2);
|
|
CHECK(r.st.tokens_in == 150);
|
|
sp_buf_init(&b);
|
|
kstate_impulses_json(&r, &b, 10);
|
|
CHECK(contains(b.p, "\"id\":1"));
|
|
CHECK(contains(b.p, "\"id\":2"));
|
|
sp_buf_free(&b);
|
|
klog_close(&r.log);
|
|
pthread_mutex_destroy(&r.lock);
|
|
pthread_cond_destroy(&r.cond);
|
|
sp_buf_free(&r.err);
|
|
pthread_mutex_destroy(&w.lock);
|
|
pthread_cond_destroy(&w.cond);
|
|
sp_buf_free(&w.err);
|
|
}
|
|
|
|
static void
|
|
test_end_to_end(void)
|
|
{
|
|
struct krang a, b;
|
|
char tmpl[] = "/tmp/krang-restart-XXXXXX";
|
|
char *dir;
|
|
char path[800];
|
|
struct sp_buf bjson;
|
|
long born1;
|
|
FILE *f;
|
|
|
|
dir = mkdtemp(tmpl);
|
|
CHECK(dir != NULL);
|
|
if (dir == NULL)
|
|
return;
|
|
snprintf(path, sizeof(path), "%s/script.txt", dir);
|
|
f = fopen(path, "w");
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fprintf(f, "tool echo {\"text\":\"restart\"}\n");
|
|
fprintf(f, "answer restart-ok\n");
|
|
fclose(f);
|
|
|
|
init_krang(&a, dir);
|
|
CHECK(ksession_meta(&a, NULL, NULL, NULL, 0, NULL, 0) == 0);
|
|
a.max_impulses = 1;
|
|
CHECK(kdaemon_run(&a) == 0);
|
|
CHECK(a.st.impulses == 1);
|
|
CHECK(a.st.turns == 1);
|
|
CHECK(a.st.tool_calls == 1);
|
|
born1 = a.st.born_at;
|
|
CHECK(born1 > 0);
|
|
CHECK(contains(a.st.last_outcome, "restart-ok"));
|
|
pthread_mutex_destroy(&a.lock);
|
|
pthread_cond_destroy(&a.cond);
|
|
sp_buf_free(&a.err);
|
|
|
|
init_krang(&b, dir);
|
|
b.max_impulses = 1;
|
|
CHECK(kdaemon_run(&b) == 0);
|
|
CHECK(b.st.impulses == 2);
|
|
CHECK(b.st.turns == 2);
|
|
CHECK(b.st.self_impulses == 2);
|
|
CHECK(b.st.tool_calls == 2);
|
|
/* one scripted turn is two engine steps (tool round + answer) */
|
|
CHECK(b.restored.steps == 2);
|
|
CHECK(b.st.steps == 4);
|
|
CHECK(b.st.born_at == born1);
|
|
CHECK(contains(b.st.last_outcome, "restart-ok"));
|
|
CHECK(b.impulse_seq == 2);
|
|
sp_buf_init(&bjson);
|
|
kstate_json(&b, &bjson);
|
|
CHECK(contains(bjson.p, "\"total\":2"));
|
|
sp_buf_free(&bjson);
|
|
sp_buf_init(&bjson);
|
|
kstate_impulses_json(&b, &bjson, 10);
|
|
CHECK(contains(bjson.p, "\"id\":1"));
|
|
CHECK(contains(bjson.p, "\"id\":2"));
|
|
CHECK(contains(bjson.p, "restart-ok"));
|
|
sp_buf_free(&bjson);
|
|
{
|
|
char logbuf[65536];
|
|
FILE *lf;
|
|
size_t n = 0;
|
|
|
|
snprintf(path, sizeof(path), "%s/state/events.jsonl", dir);
|
|
lf = fopen(path, "r");
|
|
CHECK(lf != NULL);
|
|
if (lf != NULL) {
|
|
n = fread(logbuf, 1, sizeof(logbuf) - 1, lf);
|
|
logbuf[n] = '\0';
|
|
fclose(lf);
|
|
} else {
|
|
logbuf[0] = '\0';
|
|
}
|
|
CHECK(contains(logbuf, "Drive: goal"));
|
|
CHECK(contains(logbuf, "Vitals:"));
|
|
CHECK(contains(logbuf, "\"drive\":\"goal\""));
|
|
}
|
|
pthread_mutex_destroy(&b.lock);
|
|
pthread_cond_destroy(&b.cond);
|
|
sp_buf_free(&b.err);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
test_synthetic();
|
|
test_end_to_end();
|
|
if (failures != 0) {
|
|
fprintf(stderr, "%d test(s) failed\n", failures);
|
|
return 1;
|
|
}
|
|
printf("test_replay: ok\n");
|
|
return 0;
|
|
}
|