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.
196 lines
4.7 KiB
C
196 lines
4.7 KiB
C
/*
|
|
* krang -- the conductor's drives.
|
|
*
|
|
* 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 <sys/time.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
|
|
reset_krang(struct krang *k, const char *ws)
|
|
{
|
|
memset(&k->st, 0, sizeof(k->st));
|
|
k->st.impulses = 2;
|
|
k->st.turns = 2;
|
|
k->st.ctx_max = 100000;
|
|
k->st.ctx_used = 10000;
|
|
k->st.session_bytes = 2 * 1024 * 1024;
|
|
k->hour_count = 0;
|
|
k->turn_failures = 0;
|
|
k->last_goal_impulse = 0;
|
|
k->cfg.max_cost = 0.0;
|
|
k->cfg.max_impulses_per_hour = 30;
|
|
k->cfg.compact_threshold = 60000;
|
|
snprintf(k->cfg.workspace, sizeof(k->cfg.workspace), "%s", ws);
|
|
snprintf(k->st.goal, sizeof(k->st.goal), "test the conductor");
|
|
}
|
|
|
|
static void
|
|
touch_notes(const char *ws, long age)
|
|
{
|
|
char path[700];
|
|
FILE *f;
|
|
|
|
snprintf(path, sizeof(path), "%s/THREADS.md", ws);
|
|
f = fopen(path, "w");
|
|
if (f != NULL) {
|
|
fprintf(f, "# notes\n");
|
|
fclose(f);
|
|
}
|
|
if (age > 0) {
|
|
struct timeval tv[2];
|
|
|
|
tv[0].tv_sec = k_now() - age;
|
|
tv[0].tv_usec = 0;
|
|
tv[1] = tv[0];
|
|
(void)utimes(path, tv);
|
|
}
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct krang k;
|
|
struct kvitals v;
|
|
struct kdecision d;
|
|
char tmpl[] = "/tmp/krang-mind-XXXXXX";
|
|
char *ws;
|
|
struct sp_buf b;
|
|
|
|
ws = mkdtemp(tmpl);
|
|
CHECK(ws != NULL);
|
|
if (ws == 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);
|
|
k.cfg.no_sandbox = 1;
|
|
(void)unlink("/tmp/krang-mind-notes-does-not-exist");
|
|
|
|
/* a young session with no special deficit pursues the goal */
|
|
reset_krang(&k, ws);
|
|
kconductor_measure(&k, &v);
|
|
kconductor_decide(&k, &v, &d);
|
|
CHECK(strcmp(d.drive, "goal") == 0);
|
|
CHECK(v.threads_age < 0);
|
|
CHECK(d.urgency == 30);
|
|
|
|
/* after a few impulses, missing notes become a drive */
|
|
k.st.impulses = 5;
|
|
kconductor_measure(&k, &v);
|
|
kconductor_decide(&k, &v, &d);
|
|
CHECK(strcmp(d.drive, "maintain") == 0);
|
|
CHECK(d.urgency >= 50);
|
|
CHECK(contains(d.reason, "missing"));
|
|
|
|
/* fresh notes release it */
|
|
touch_notes(ws, 0);
|
|
kconductor_measure(&k, &v);
|
|
kconductor_decide(&k, &v, &d);
|
|
CHECK(strcmp(d.drive, "goal") == 0);
|
|
CHECK(v.threads_age >= 0 && v.threads_age < 120);
|
|
|
|
/* stale notes become maintenance again */
|
|
touch_notes(ws, 3600);
|
|
kconductor_measure(&k, &v);
|
|
kconductor_decide(&k, &v, &d);
|
|
CHECK(strcmp(d.drive, "maintain") == 0);
|
|
CHECK(contains(d.reason, "minutes"));
|
|
|
|
/* context pressure outranks the goal */
|
|
touch_notes(ws, 0);
|
|
k.st.ctx_used = 90000;
|
|
kconductor_measure(&k, &v);
|
|
kconductor_decide(&k, &v, &d);
|
|
CHECK(strcmp(d.drive, "survive") == 0);
|
|
CHECK(contains(d.reason, "context"));
|
|
|
|
/* and the cost cap outranks context */
|
|
k.cfg.max_cost = 1.0;
|
|
k.st.cost = 0.95;
|
|
kconductor_measure(&k, &v);
|
|
kconductor_decide(&k, &v, &d);
|
|
CHECK(strcmp(d.drive, "survive") == 0);
|
|
CHECK(contains(d.reason, "cost"));
|
|
CHECK(d.urgency >= 80);
|
|
k.cfg.max_cost = 0.0;
|
|
k.st.cost = 0.0;
|
|
k.st.ctx_used = 10000;
|
|
touch_notes(ws, 0);
|
|
|
|
/* a long open goal gets an exploration impulse on the eighth turn */
|
|
k.st.impulses = 20;
|
|
k.last_goal_impulse = 0;
|
|
kconductor_measure(&k, &v);
|
|
kconductor_decide(&k, &v, &d);
|
|
CHECK(strcmp(d.drive, "explore") == 0);
|
|
CHECK(contains(d.reason, "since the last completed goal"));
|
|
|
|
/* the prompt carries the drive, the reason and the measured vitals */
|
|
k.st.goal_state[0] = '\0';
|
|
sp_buf_init(&b);
|
|
k_impulse_prompt(&k, "timer", &b);
|
|
CHECK(contains(b.p, "[conductor]"));
|
|
CHECK(contains(b.p, "Drive: explore"));
|
|
CHECK(contains(b.p, "Why:"));
|
|
CHECK(contains(b.p, "Vitals:"));
|
|
CHECK(contains(b.p, "Explore."));
|
|
CHECK(contains(b.p, "session 2.0 MB"));
|
|
CHECK(strcmp(k.st.drive, "explore") == 0);
|
|
sp_buf_free(&b);
|
|
|
|
/* the self-check path claims the reflect drive */
|
|
k.st.stuck_level = 2;
|
|
k.st.stuck_attempts = 2;
|
|
sp_buf_init(&b);
|
|
k_selfcheck_prompt(&k, &b);
|
|
CHECK(contains(b.p, "Drive: reflect"));
|
|
CHECK(contains(b.p, "stuck level 2"));
|
|
CHECK(strcmp(k.st.drive, "reflect") == 0);
|
|
CHECK(k.st.drive_urgency >= 80);
|
|
sp_buf_free(&b);
|
|
|
|
/* the snapshot carries it to the dashboard */
|
|
sp_buf_init(&b);
|
|
kstate_json(&k, &b);
|
|
CHECK(contains(b.p, "\"drive\":{\"name\":\"reflect\""));
|
|
CHECK(contains(b.p, "\"urgency\":"));
|
|
sp_buf_free(&b);
|
|
|
|
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_mind: ok\n");
|
|
return 0;
|
|
}
|