mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 08:18:10 +01:00
George filed two reports through FleetCrown's own feedback widget, watched nothing happen, and asked Loki "what was the most recent feedback sent". Not in your data. Both rows were in site_feedback, filed minutes earlier. Four runs spawned from them sat in `waiting`. Twenty-four runs had errored in three days. The answer was nevertheless honest: Loki had been handed no records, so the sentence was true of what it could see — which is worse than an error, because a truthful negative reads as a checked one. Three independent causes, each sufficient alone. 1. NO SOURCE READ THE TABLE. `grep -rn feedback src/lib/agent/` returned nothing. Nor did runs, agent sessions, alerts, or notes. Loki could see the "life" half of the product (people, projects, goals) and nothing of the fleet half the product is named after. 2. THE PROMPT BUDGET WAS ARITHMETICALLY IMPOSSIBLE. One module constant — Groq's 12000-token minute window x 0.8 / 3 rounds = 3200 — applied to every vendor. Groq has since cut the window to 8000, and the prompt's own fixed cost measures 2946 tokens (system prompt 1264 + native tool schema 977 + catalog), leaving ~254 tokens: one fact. With the operator's message it goes negative, the fit correctly returns zero, and production logged `[loki] round 1: 40 facts exceed the call budget — sending 0` on every turn and sent it anyway with an empty Records block. 3. THE RICHER SEED WAS ON THE FALLBACK PATH ONLY. buildGroundedTurn (approvals, knowledge, economy, cues) ran only inside askLokiViaGateway. The primary loop seeded people + projects. So the approval queue seeded in #262 *specifically to survive losing the tool loop* was, on the path that normally ran, reachable only by the tool call the loop could not afford. WHAT CHANGED Retrieve first, then reason. A pure keyword planner (src/lib/agent/plan.ts) reads the question and fetches the relevant sources in parallel BEFORE the model is called; tools become depth, not basic retrieval. A tool loop needs a healthy model and a fitting prompt before it can ask its first question, which is exactly what is missing when this class fires. A regex is dumber than a model and never rate-limited. This is how OrangeCat's Cat has always worked. Eleven new fact adapters: feedback, runs, live agent sessions, alerts, notes, plus a computed fleet pulse (runner state, runs waiting/errored, unread feedback, pending approvals, today's AI budget) and a buildFleetBrief of SQL-computed answers the model may only phrase. Six new tools over them. Budgets follow the vendor: linkPromptBudgetTokens() sizes Groq from its minute window and large-context vendors by usefulness; the prompt is sized against the largest usable link and the walker SKIPS links it does not fit (a preflight, not a refused round-trip). Env-tunable without a deploy. When overhead busts every budget the loop now throws instead of sending an empty Records block. Conversation history reaches the primary path, so continuity no longer appears and disappears depending on which brain served the turn. Provenance is rendered: model, what was read per source, tools used, elapsed time, and the grounding verdict. The verdict was always computed and always returned; the route dropped it on persist and the assistant never read it, so a flagged answer looked exactly like a clean one — the failure the harness exists to prevent, reintroduced at the last inch. THE GATE (this is the third time this class has fired) Every read tool's data must ALSO be reachable from the seed. Seed = survivability, tool = depth, one adapter called by both. scripts/test/loki-retrieval-plan.ts fails the build on a read tool with no seed source, and on a seed source no question can reach. It caught a real orphan on its first run: the OrangeCat economy feed, fetchable but unreachable. Also fixed while here: propose_action reported a failed DB write as a successfully queued draft; the repair pass accepted a repair that invented a NEW unsupported claim if it had fewer total; assignee matching used a bare substring (the dr-UZH-nikov pattern on the write side); pageContext over 2000 chars 400'd the whole request instead of being clamped; the native tool schema repeated each full description, paying for the same prose twice. Docs: docs/architecture/loki-answering.md. Tests: 4 files, 63 checks, all env-independent. Claude-Session: https://claude.ai/code/session_01CAfKwM1oUSA9tU9fgdhfdf Co-authored-by: Cato <georgy.butaev@revamp-it.ch> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>