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

175 lines
8.3 KiB
Markdown

# krang
A science experiment built on the [splinter](../splinter) engine. Its subject
acts on its own behalf: one persistent life, a world to act in, a goal that is
its own, and no user to serve. The design notes, the prior art and the
conscious-machine blueprint are in [docs/DESIGN.md](docs/DESIGN.md).
krang exists to test one hypothesis: what an assistant lacks is not
intelligence but a regular **impulse to continue**. It is born once, keeps one
session forever, wakes on its own cadence, works on a goal it owns, notices
when it is stuck, changes approach, and records what happened. There is no user
to ask.
It lives in a chroot. The process starts as root only to build that world, then
drops to an unprivileged uid and applies two one-way restrictions: Landlock for
paths and seccomp-bpf for syscalls on Linux (the design is pledge/unveil; the
OpenBSD backend is not implemented yet).
## Build
```sh
make -C ../splinter # libsplinter.so and the headers
make
make check # no network, no key, no model
```
`libsplinter.so` has no SONAME; the binary carries an rpath to `../splinter`.
Rebuild krang whenever splinter changes.
## Configure
```sh
sudo install -d /etc/krang
sudo install -m 0600 krang.conf.example /etc/krang/krang.conf
$EDITOR /etc/krang/krang.conf
```
The config's `goal` is an **initial condition**, not a task. It seeds the
newborn; after that the goal in the session's `.meta` is the subject's. It can
keep it, revise it, or replace it with `set_goal`; the change is logged, and
the next impulse reads the new goal back.
## Prepare and start
```sh
sudo krang --prepare-rootfs --config /etc/krang/krang.conf
krang --plan --config /etc/krang/krang.conf # read the plan, do not start
sudo krang --config /etc/krang/krang.conf # start
```
Do not start it casually: once it has a provider it will act on its own. The
service unit in `share/krang.service` restarts it after an unexpected exit; a
deliberate stop is `systemctl stop krang` (or SIGTERM).
## The instruments
- **Dashboard** `http://127.0.0.1:8096/` — state, goal, the conductor's drive
and reason, impulses, turns, tool calls, failures, tokens, cost, context,
session, compactions, stuck level, last outcome and error, working notes, and
a chat box.
- **HTTP API** — `/healthz`, `/api/state`, `/api/impulses`, `/api/log`,
`/api/history`, `/api/threads`, `/api/tools`, `/api/sandbox`, `/api/config`,
`/api/events`, `POST /api/say`, `GET /api/say/<id>`, `POST /mcp`.
- **MCP** — newline-delimited JSON-RPC on `127.0.0.1:8098`. Tools:
`krang_state`, `krang_impulses`, `krang_events`, `krang_session`,
`krang_history`, `krang_tools`, `krang_sandbox`, `krang_config`,
`krang_say`, `krang_say_status`, `krang_steer`, `krang_cancel`. No
`krang_approve`: krang never asks.
- Chat from either surface goes into the **same** session as the daemon's own
impulses. One turn at a time; a message that arrives mid-turn is queued.
The model also gets `journal_search` (read-only), which searches the full
session log including the history compaction dropped, and `set_goal`, which
writes the goal it is pursuing. The latter is the subject's own act, not an
operator setting; the session meta is the authority.
## The conductor
A timer alone is not a reason. Before every impulse the daemon measures its own
state - context against the window and the compaction threshold, cost against
the cap, stuck level, turn failures, impulse count, the age of `THREADS.md`,
session size - and lets five drives compete:
| drive | when it wins | what the impulse is for |
| --- | --- | --- |
| `survive` | context or cost is near its limit | the smallest step; write down what compaction must not lose |
| `reflect` | the stuck ladder fired | find the mistake, change the approach |
| `maintain` | notes are missing or stale | bring `THREADS.md` and `skills/` up to date |
| `explore` | twenty impulses without a completed goal, then every eighth | try one new capability, bounded |
| `goal` | nothing more urgent | the smallest useful step on its own goal |
The chosen drive, its reason and the vitals go into the impulse and onto the
dashboard. The model still chooses the action; the conductor only chooses what
the turn is about. This is the engineering version of the body/drives/
conductor loop: constraints produce drives, drives compete for attention, and
the consequences feed the next decision.
## Restart
The event log is the life, not a copy of it. At startup `kreplay_events()`
reads it once and rebuilds the impulse ledger (with ids), the token/cost/step
totals, the last outcome, the stuck ladder, the blocked clock and the
recent-start count. The engine's own counters are added on top, so the
dashboard never starts again at zero. A restored blocked goal keeps its hour
of quiet; three starts within five minutes back off instead of spinning. The
session is opened with `resume`, so there is no second birth.
Splinter plugins are opt-in: `plugin = /absolute/plugin.so` (max 8). The root
phase bind-mounts each plugin and `libsplinter.so` read-only into `/opt/krang`
and sets `LD_LIBRARY_PATH`; only the path is wired, not the plugin's own
`plugin NAME { }` config block yet. The root integration loads the clock plugin
inside the chroot as a proof.
If a token is set in the config, loopback is not required and every request
needs it (`Authorization: Bearer`, `?token=`, or the MCP `AUTH <token>` line).
Without a token krang refuses to bind to a non-loopback address.
A loopback dashboard without a token is still a local service, so the HTTP
surface checks the names a browser sends: a request whose `Host` is not a
loopback name is refused (DNS rebinding), and an `Origin` that does not match
the `Host` is refused (cross-site requests). There is no CORS grant; the
dashboard is same-origin.
## The experiment log
- `state/sessions/krang.jsonl` is the conversation and the model's memory.
- `state/events.jsonl` is the append-only experiment log. It is opened by the
root phase, owned by root, mode 0600, and written through the inherited
descriptor; the sandbox user can neither open nor truncate it. The fd is
`FD_CLOEXEC`, so the `run` tool's children do not inherit it.
- The dashboard, HTTP API and MCP all read the same snapshot and the same ring
of recent events. There is no second counter.
## Tests
```sh
make check
make check-asan
./krang --check --config krang.conf.example # config + plan + scripted turn
./krang --self-test # one scripted turn, no network
sudo tests/root-integration.sh # debootstrap + chroot + drop
```
`make check` covers config parsing and path guards, the birth prompt, stuck
detection, the snapshot/HTTP/MCP surfaces (including the Host/Origin refusal
and token check), a real scripted engine turn, the engine under Landlock +
seccomp, `kdaemon_run` itself (one birth impulse on the script transport, then
a clean stop), a turn cancelled by `max_turn_seconds`, restart replay (ledger,
totals, stuck and blocked) with a second daemon run that continues the first,
incremental session counting across appends, a torn tail and truncation, and
the conductor's goal/maintain/explore/survive decisions. The sandbox tests
prove Landlock denies a write outside the allowed set and seccomp denies
`mount(2)` while fork/execve/network still work.
`tests/root-integration.sh` is the root-only proof: it prepares a real rootfs
with debootstrap, enters it with `--sandbox-test`, loads the clock plugin from
the splinter tree, and checks uid 65534, mount denial, plugin execution,
event-log ownership/mode, session ownership, and that the sandbox uid cannot
read the event log. It never starts the autonomous daemon or calls a provider.
## Safety notes
- chroot is isolation, not a jail against root. krang runs as an unprivileged
uid after setup and has no path back.
- The provider key is readable by the daemon; assume the model can eventually
see anything in its environment. Use a throwaway, budget-capped key. The
`run` tool's environment is filtered with `sp_secret_env_add()`, and the
event log redacts the configured key.
- The network namespace is shared. `run` can reach the network; the chroot does
not change that.
- The session log is the model's memory. It is writable by the sandbox uid; the
experiment log is not.
- User namespaces and kernel bugs remain outside the model. The layers raise
the cost of an escape; they are not a proof.