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

166 lines
4.3 KiB
C

/*
* krang -- offline integration test: one scripted turn through the engine.
*
* Copyright (c) 2026 Konrad Lother
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* No network, no key, no model. The mock transport replays the script; the
* session and the event log are real.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "krang.h"
#include "i18n.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;
}
int
main(void)
{
struct krang k;
char tmpl[512], *dir, script[700], logpath[800], answer[KRANG_GOAL];
size_t records = 0;
unsigned long long bytes = 0;
FILE *f;
dir = mkdtemp(strcpy(tmpl, "/tmp/krang-engine-XXXXXX"));
CHECK(dir != NULL);
if (dir == NULL)
return 1;
(void)chdir(dir);
(void)setenv("HOME", dir, 1);
(void)sp_i18n_init("en");
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), "prove the engine");
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.plugin[0], sizeof(k.cfg.plugin[0]), "%s",
KRANG_TEST_PLUGIN);
k.cfg.nplugins = 1;
snprintf(k.session_dir, sizeof(k.session_dir), "%s/sessions", dir);
(void)mkdir(k.session_dir, 0700);
snprintf(logpath, sizeof(logpath), "%s/events.jsonl", dir);
CHECK(klog_open(&k.log, logpath, k.last_error,
sizeof(k.last_error)) == 0);
{
struct sp_buf sp;
sp_buf_init(&sp);
k_system_prompt(&k, &sp);
snprintf(k.system_prompt, sizeof(k.system_prompt), "%s",
sp.p != NULL ? sp.p : "");
sp_buf_free(&sp);
}
snprintf(script, sizeof(script), "%s/script.txt", dir);
f = fopen(script, "w");
CHECK(f != NULL);
if (f == NULL)
return 1;
fprintf(f, "tool echo {\"text\":\"from-script\"}\n");
fprintf(f, "answer hello-from-script\n");
fclose(f);
CHECK(kengine_open(&k, script, k.last_error,
sizeof(k.last_error)) == 0);
CHECK(k.eng != NULL);
if (k.eng != NULL) {
const struct sp_tool *gt;
CHECK(sp_registry_find(sp_engine_registry(k.eng),
"clock") != NULL);
gt = sp_registry_find(sp_engine_registry(k.eng), "set_goal");
CHECK(gt != NULL);
if (gt != NULL) {
struct sp_buf gb;
char g[KRANG_GOAL];
sp_buf_init(&gb);
CHECK(gt->run("{\"goal\":\"a goal of my own\"}", &gb,
gt->ctx) == 0);
CHECK(contains(gb.p, "a goal of my own"));
CHECK(sp_session_get_goal(k.session_dir, "krang", g,
sizeof(g)) == 0);
CHECK(strcmp(g, "a goal of my own") == 0);
CHECK(strcmp(k.st.goal, "a goal of my own") == 0);
sp_buf_clear(&gb);
CHECK(gt->run("{\"goal\":\"\"}", &gb, gt->ctx) == 0);
CHECK(contains(gb.p, "error:"));
sp_buf_free(&gb);
sp_buf_init(&gb);
klog_json(&k.log, &gb, 50);
CHECK(contains(gb.p, "goal_set"));
sp_buf_free(&gb);
}
int rc = krun_turn(&k, "prove the apparatus", "test", NULL);
CHECK(rc == 0);
snprintf(answer, sizeof(answer), "%s", k.st.last_outcome);
CHECK(contains(answer, "hello-from-script"));
CHECK(contains(k.st.last_tool, "echo"));
CHECK(k.st.tool_calls >= 1);
(void)ksession_meta(&k, &records, &bytes, NULL, 0, NULL, 0);
CHECK(records >= 3);
CHECK(bytes > 0);
{
const struct sp_tool *jt = sp_registry_find(
sp_engine_registry(k.eng), "journal_search");
struct sp_buf jb;
CHECK(jt != NULL);
if (jt != NULL) {
sp_buf_init(&jb);
CHECK(jt->run("{\"query\":"
"\"hello-from-script\"}", &jb,
jt->ctx) == 0);
CHECK(contains(jb.p, "hello-from-script"));
sp_buf_free(&jb);
}
}
}
{
struct sp_buf b;
sp_buf_init(&b);
kstate_json(&k, &b);
CHECK(contains(b.p, "\"session\":"));
CHECK(contains(b.p, "\"tool_calls\":"));
sp_buf_free(&b);
}
kengine_close(&k);
klog_close(&k.log);
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_engine: ok\n");
return 0;
}