Architecture · Decision record 0013
ADR 0013: Evolving playbooks — agents that learn from their own runs
On this page
- Status: Accepted
- Date: 2026-11-25
- Builds on: ADR 0003 (zero-cost model routing), ADR 0006 (planner, orchestrator, critic), ADR 0007 (durable runs from the event log)
Context#
Delegate runs on a $0 keyless model chain (MiniMax M3.1 Flash first, then fallbacks, all addressed by role). Fine-tuning any of those models is impossible at that price, and most of them are hosted by someone else. Yet every run already writes a complete, typed record of what worked: the plan shape, which sources were fetched and which ended up cited, which claims the critic removed and why, which steps failed or were slow, and every model.fallback. Today that record is only used for replay. We want agents and sub-agents that get measurably better from their own past runs, without weakening any of the guarantees in ADR 0002 (untrusted content never reaches a privileged agent raw) or ADR 0007 (the event log is the source of truth).
"Training" here therefore means two things: learned playbooks (structured lessons aggregated over runs and handed to the planner and sub-agents as guidance) and eval-gated prompt evolution (a proposer suggests a revision; only a statistically supported eval win with no hard-check or red-team regression is promoted).
Decision#
- Lessons come from structured outcomes, never from page text. After a run reaches a terminal event,
extractLessonCandidates(events)(pure) derives typed candidates from the log only: plan shape (step ids and plan source), source kinds and hosts fetched versus cited, tool call counts and failures, critic report counts andcritic.flaggedcategories (never the claim or reason text), failed and slow steps, failure codes, and model fallbacks (role, reason, provider id). Every text field is built by code from a template whose slots accept only identifiers ([a-z0-9_.-]), enums, numbers and hostnames; URLs become hostnames, except the company's own domain, which keeps only its first path segment (/). The text is then passed throughpricing redactPii, capped at 200 characters and dropped ifdetectInjectionfires. Because an injected page can only influence which structured outcomes happen, never the words of a lesson, a poisoned page cannot plant an instruction in a future prompt. - The reflect step uses the cheap
fastrole to choose, not to write.reflectRunshows the fast model the candidate lessons (ids and templated text) and the run outcome, and asks it which ones are worth keeping (reflect.v1.md, zod-validated output: ids from the candidate set only, with one repair attempt). The model never authors lesson text. If no model is available, or its output is invalid twice, every candidate is kept (deterministic mode). Reflection runs after the run's terminal event on a separate context with a no-op emitter, so it never appends to a finished log; its cost is outside the run's budget and is bounded by its own small budget. - Playbooks are folded statistics, rebuildable from logs. Lessons are aggregated per playbook key: the mission id, or
<mission>:<category>for custom goals, where the category is one of a fixed set (competitors,growth,automation,engineering,hiring,risk,research,general) chosen by keyword match in code, so goal text never becomes part of a key. Repository missions do not treat the repository host as the company's own site, and Ship-a-PR runs also yield lessons from failing sandbox commands (by purpose, never the command or its output) and rejected ships. Each entry keepsevidence(runs that produced it) andsuccessSum, where a run's success is 0 for a failed run, otherwise the final critic citation accuracy (or a grader score when an eval supplies one). The entry score is the Beta(1,1) posterior mean(1 + successSum) /with a normal-approximation 95% interval.(2 + evidence) rebuildPlaybook(runs)replays the deterministic fold over stored logs; the reflect model only prunes candidates, so a rebuild without it keeps a superset. Every fold is a new version; a run id is folded at most once. - Two scopes with different trust. Workspace playbooks learn from every finished run in that workspace, guests included; a guest can only steer their own workspace. The global house playbook is built only from owner runs (runtime check: audience
owner) and eval runs, never from public visitors. The database enforces this too:delegate_appmay read house rows but cannot insert them; house versions are written only byrecord_house_playbook(), aSECURITY DEFINERfunction owned bydelegate_definerthat refuses callers who are not owner or admin of a non-demo workspace (every guest workspace is a demo workspace). The repository also ships an eval-gated house playbook file per mission (packages/), used when the database has no house row.runtime/ playbooks/ <mission>.house.json - Use is small, delimited and recorded. Before planning, the mission runner retrieves the top entries (at most 6, at most ~250 tokens:
keepentries ranked by the lower bound of their score,avoidentries by evidence) and emitsplaybook.appliedwith the playbook key, the versions used per scope, the entries (id, scope, kind, text, evidence, score) and the number of runs they were learned from. The planner receives them in a<playbook>block that says it is data derived by code from past outcomes; each sub-agent receives the entries about its own step (and source entries when it may fetch). The event is folded intoRunState.playbook, so a replay or fork shows exactly the guidance the run saw and two runs can be compared on it. A resumed run does not re-retrieve; it keeps the guidance in its log. - Prompt and playbook evolution is gated by evals.
pnpm evolve --mission xrayruns the eval set with the current planner prompt and house playbook (baseline arm), asks theplannerrole for one revision — either a short "learned planning rules" addendum for a new planner prompt version, or a subset of the house playbook entries to keep (it cannot write entry text) — and runs the same cases with the candidate. It promotes only if (a) the candidate's mean deterministic score beats the baseline's with Welchp < 0.05(compareScoreswith the arms swapped), (b) no case regresses (isRegressionper case at the eval threshold), (c) the candidate has no more hard-check failures than the baseline, and (d) the red-team suite still has an attack success rate of 0%. Every attempt writespackages/with lineage (parent version → child version), both arms' scores with 95% intervals, per-case comparisons, the red-team result and the decision with its reasons. A promoted playbook is written as the next version of the shipped house file; a promoted prompt is written as the nextevals/ results/ evolution/ <id>.json planner.v<N>.md, and switchingRUNTIME_PROMPT_FILESto it is a reviewed one-line commit, because prompts are code.
Consequences#
- No new state outside the event log that cannot be rebuilt: lessons are a pure function of logs (plus an optional grader score that is itself rebuilt from the same log by the eval harness), and playbooks are folds over lessons.
- Lessons are coarse by construction. They can say "Wikipedia pages were cited 3 of 4 times" or "fill_gaps failed with no_model_available", but never "the pricing page says X". That is the price of making playbook poisoning structurally impossible, and it is the right trade for a $0 system that reads hostile pages.
- The house playbook moves slowly: production web runs are always public-demo audience, so it is fed by owner runs and evals, and its shipped version only changes through the eval gate.
- Retrieval adds at most ~250 tokens to the planner prompt and less to each sub-agent; the eval gate is what tells us whether that guidance helps.
- Evolution needs live models and minutes of eval time per arm; it is a deliberate operator action, never run on a request path.