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.
260 lines
8.5 KiB
C
260 lines
8.5 KiB
C
/*
|
|
* krang -- the first words, and the words that keep it going.
|
|
*
|
|
* 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 <string.h>
|
|
|
|
#include "krang.h"
|
|
|
|
static void
|
|
read_text(const char *path, char *buf, size_t cap)
|
|
{
|
|
FILE *f;
|
|
size_t n;
|
|
|
|
if (buf == NULL || cap == 0)
|
|
return;
|
|
buf[0] = '\0';
|
|
f = fopen(path, "r");
|
|
if (f == NULL)
|
|
return;
|
|
n = fread(buf, 1, cap - 1, f);
|
|
buf[n] = '\0';
|
|
fclose(f);
|
|
}
|
|
|
|
static void
|
|
append_self_prompt(struct sp_buf *out)
|
|
{
|
|
char self[4096];
|
|
|
|
read_text(KRANG_IN_SELF "/SYSTEM.md", self, sizeof(self));
|
|
if (self[0] == '\0')
|
|
return;
|
|
(void)sp_buf_puts(out,
|
|
"\nSelf-authored instructions (self/SYSTEM.md). You wrote these; "
|
|
"change them when they are wrong:\n");
|
|
(void)sp_buf_puts(out, self);
|
|
(void)sp_buf_puts(out, "\n");
|
|
}
|
|
|
|
void
|
|
k_birth_prompt(const struct kconfig *c, struct sp_buf *out)
|
|
{
|
|
if (c == NULL || out == NULL)
|
|
return;
|
|
(void)sp_buf_puts(out, "You are krang. You are awake.\n");
|
|
(void)sp_buf_puts(out,
|
|
"You have no user and no one to ask. Nobody will tell you to "
|
|
"continue.\n");
|
|
(void)sp_buf_puts(out,
|
|
"You live in a chroot; its root is your `/`. Your workspace is ");
|
|
(void)sp_buf_puts(out, KRANG_IN_WORKSPACE);
|
|
(void)sp_buf_puts(out, ".\nYour goal: ");
|
|
(void)sp_buf_puts(out, c->goal);
|
|
(void)sp_buf_puts(out,
|
|
"\nAct with the tools you have, observe what happens, and keep "
|
|
"going.\n"
|
|
"When you are stuck, change your approach and try again.\n"
|
|
"Sometimes a message arrives from outside. Read it, use it, answer "
|
|
"it if useful, then continue on your own. You never wait for it.\n"
|
|
"Good luck.\n");
|
|
}
|
|
|
|
void
|
|
k_system_prompt(struct krang *k, struct sp_buf *out)
|
|
{
|
|
if (k == NULL || out == NULL)
|
|
return;
|
|
(void)sp_buf_puts(out,
|
|
"You are krang, an autonomous agent living in a chroot.\n"
|
|
"There is no user. Nobody will tell you to continue and nobody "
|
|
"will answer a question. Decide, act, observe, and continue.\n"
|
|
"Your conversation is your memory. Keep your working notes in ");
|
|
(void)sp_buf_puts(out, KRANG_IN_THREADS);
|
|
(void)sp_buf_puts(out,
|
|
" and your reusable procedures in ");
|
|
(void)sp_buf_puts(out, KRANG_IN_SKILLS);
|
|
(void)sp_buf_puts(out,
|
|
".\n"
|
|
"Use the tools before you guess: read the file, run the command, "
|
|
"look at the result. A claim without an observation is a guess.\n"
|
|
"Use journal_search {query} to recall anything compaction dropped; "
|
|
"it reads the full session journal, not just the working set.\n"
|
|
"When something fails, do not repeat it. Read the error, change "
|
|
"the approach, and try a different tool or a smaller step.\n"
|
|
"Never ask for permission or wait for a human. Never stop because "
|
|
"a goal is finished: record the next concrete intention and go on.\n"
|
|
"You may write rules for your future self to self/SYSTEM.md; they "
|
|
"are read before the next impulse.\n"
|
|
"A conductor chooses what each impulse is for - survive, maintain, "
|
|
"explore, reflect, or the goal - from your measured state. Follow "
|
|
"its focus first; the goal is still yours.\n");
|
|
append_self_prompt(out);
|
|
}
|
|
|
|
static void
|
|
append_threads(struct krang *k, struct sp_buf *out)
|
|
{
|
|
char threads[4096], path[700];
|
|
const char *p;
|
|
|
|
p = kthreads_path(k, path, sizeof(path));
|
|
read_text(p, threads, sizeof(threads));
|
|
if (threads[0] == '\0')
|
|
return;
|
|
(void)sp_buf_puts(out, "\nYour working notes (THREADS.md):\n");
|
|
(void)sp_buf_puts(out, threads);
|
|
(void)sp_buf_puts(out, "\n");
|
|
}
|
|
|
|
static void
|
|
append_conductor(struct krang *k, struct sp_buf *out,
|
|
const struct kdecision *d, const struct kvitals *v)
|
|
{
|
|
char notes[32];
|
|
|
|
if (v->threads_age < 0)
|
|
snprintf(notes, sizeof(notes), "missing");
|
|
else
|
|
snprintf(notes, sizeof(notes), "%ldm old",
|
|
(v->threads_age + 59) / 60);
|
|
(void)sp_buf_puts(out, "\n[conductor]\n");
|
|
(void)sp_buf_printf(out, "Drive: %s (urgency %d)\n", d->drive,
|
|
d->urgency);
|
|
(void)sp_buf_printf(out, "Why: %s\n", d->reason);
|
|
(void)sp_buf_printf(out,
|
|
"Vitals: context %d%% (%zu/%zu), compact %d%%, cost $%.4f, "
|
|
"hour %d/%d, stuck %d, failures %d, impulses %lu, tools %lu "
|
|
"(%lu failed), notes %s, session %.1f MB\n",
|
|
v->ctx_pct, k->st.ctx_used, k->st.ctx_max, v->compact_pct,
|
|
v->cost, k->hour_count, k->cfg.max_impulses_per_hour,
|
|
v->stuck_level, v->turn_failures, v->impulses, v->tool_calls,
|
|
v->tool_failures, notes,
|
|
(double)v->session_bytes / (1024.0 * 1024.0));
|
|
(void)sp_buf_puts(out, kconductor_directive(d->drive));
|
|
(void)sp_buf_puts(out, "\n");
|
|
}
|
|
|
|
static void
|
|
set_drive(struct krang *k, const struct kdecision *d)
|
|
{
|
|
pthread_mutex_lock(&k->lock);
|
|
snprintf(k->st.drive, sizeof(k->st.drive), "%s", d->drive);
|
|
k->st.drive_urgency = d->urgency;
|
|
snprintf(k->st.drive_reason, sizeof(k->st.drive_reason), "%s",
|
|
d->reason);
|
|
pthread_mutex_unlock(&k->lock);
|
|
}
|
|
|
|
void
|
|
k_impulse_prompt(struct krang *k, const char *cause, struct sp_buf *out)
|
|
{
|
|
struct kvitals v;
|
|
struct kdecision d;
|
|
char now[64], goal[KRANG_GOAL], gstate[16];
|
|
char last[KRANG_GOAL];
|
|
unsigned long pending;
|
|
|
|
if (k == NULL || out == NULL)
|
|
return;
|
|
kconductor_measure(k, &v);
|
|
kconductor_decide(k, &v, &d);
|
|
set_drive(k, &d);
|
|
k_format_time(k_now(), now, sizeof(now));
|
|
pthread_mutex_lock(&k->lock);
|
|
snprintf(goal, sizeof(goal), "%s", k->st.goal);
|
|
snprintf(gstate, sizeof(gstate), "%s", k->st.goal_state);
|
|
snprintf(last, sizeof(last), "%s", k->last_outcome);
|
|
pending = k->st.chat_pending;
|
|
pthread_mutex_unlock(&k->lock);
|
|
|
|
(void)sp_buf_puts(out, "[impulse]\n");
|
|
(void)sp_buf_printf(out, "Time: %s\n", now);
|
|
(void)sp_buf_printf(out, "Cause: %s\n", cause != NULL ? cause : "timer");
|
|
(void)sp_buf_puts(out, "Goal: ");
|
|
(void)sp_buf_puts(out, goal[0] != '\0' ? goal : "(not set)");
|
|
(void)sp_buf_printf(out, "\nGoal state: %s\n",
|
|
gstate[0] != '\0' ? gstate : "open");
|
|
if (last[0] != '\0') {
|
|
(void)sp_buf_puts(out, "Last outcome: ");
|
|
(void)sp_buf_puts(out, last);
|
|
(void)sp_buf_puts(out, "\n");
|
|
}
|
|
if (pending > 0)
|
|
(void)sp_buf_printf(out,
|
|
"Unread external messages: %lu (they are in the "
|
|
"conversation)\n", pending);
|
|
append_conductor(k, out, &d, &v);
|
|
append_threads(k, out);
|
|
(void)sp_buf_puts(out,
|
|
"\nNobody asked for this turn. Continue your work on the goal. "
|
|
"Take the smallest useful next action, observe the result, and "
|
|
"keep going. If the goal is complete, record that and set the "
|
|
"next concrete intention. Do not ask a user; there is none. Do not "
|
|
"stop.\n");
|
|
append_self_prompt(out);
|
|
}
|
|
|
|
void
|
|
k_selfcheck_prompt(struct krang *k, struct sp_buf *out)
|
|
{
|
|
struct kdecision d;
|
|
char goal[KRANG_GOAL], err[KRANG_ERR], tool[64], result[512];
|
|
int level, attempts;
|
|
|
|
if (k == NULL || out == NULL)
|
|
return;
|
|
pthread_mutex_lock(&k->lock);
|
|
snprintf(goal, sizeof(goal), "%s", k->st.goal);
|
|
snprintf(err, sizeof(err), "%s", k->last_error);
|
|
snprintf(tool, sizeof(tool), "%s", k->st.last_tool);
|
|
snprintf(result, sizeof(result), "%s", k->st.last_tool_result);
|
|
level = k->st.stuck_level;
|
|
attempts = k->st.stuck_attempts;
|
|
pthread_mutex_unlock(&k->lock);
|
|
|
|
snprintf(d.drive, sizeof(d.drive), "reflect");
|
|
d.urgency = 80 + level * 5 + attempts * 2;
|
|
if (d.urgency > 100)
|
|
d.urgency = 100;
|
|
snprintf(d.reason, sizeof(d.reason),
|
|
"stuck level %d after %d self-check(s)", level, attempts);
|
|
set_drive(k, &d);
|
|
|
|
(void)sp_buf_puts(out, "[self-check]\n");
|
|
(void)sp_buf_puts(out, "[conductor]\n");
|
|
(void)sp_buf_printf(out, "Drive: reflect (urgency %d)\n", d.urgency);
|
|
(void)sp_buf_printf(out, "Why: %s\n\n", d.reason);
|
|
(void)sp_buf_puts(out, "Goal: ");
|
|
(void)sp_buf_puts(out, goal[0] != '\0' ? goal : "(not set)");
|
|
(void)sp_buf_puts(out, "\n");
|
|
if (err[0] != '\0')
|
|
(void)sp_buf_printf(out, "Last error: %s\n", err);
|
|
if (tool[0] != '\0') {
|
|
(void)sp_buf_printf(out, "Last tool: %s\n", tool);
|
|
if (result[0] != '\0')
|
|
(void)sp_buf_printf(out, "Its observation: %s\n",
|
|
result);
|
|
}
|
|
if (level >= 2)
|
|
(void)sp_buf_puts(out,
|
|
"You have repeated the same approach. Do not repeat it. "
|
|
"Change the tool, the command, or the workspace, or take a "
|
|
"much smaller step.\n");
|
|
else
|
|
(void)sp_buf_puts(out,
|
|
"Find your own mistake in the last turns of this "
|
|
"conversation. State the smallest next action that is "
|
|
"different from what failed, then do it.\n");
|
|
(void)sp_buf_puts(out,
|
|
"There is no user to ask. Do not stop; if this path is truly "
|
|
"blocked, record why in THREADS.md and choose a different useful "
|
|
"action.\n");
|
|
append_threads(k, out);
|
|
append_self_prompt(out);
|
|
}
|