/* * 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 #include #include #include #include #include #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; }