# ALGORITHM.md — the machine in words How floop serves any goal: the general loop, tool action, the generic expert, recursion, stopping, failure, and how every LLM call gets its context composed. ## 0. The whole machine in one paragraph A goal arrives (a question to answer, a tool to call, a chat to hold, a maintenance task to run). floop never "thinks a goal through" in one pass: the goal reduces to open questions until one is small enough for a single expert call; answers flow back up and are assembled into the response. Three kinds of parts do the work. **The LLMs** propose: read the request, type the response expected, decompose the goal into subquestions, translate between language and formal formats, compose the final prose. They never decide, never compute, never assert facts. **The engines** — solvers, provers, retrieval, the knowledge graph — do the deciding: deterministic, typed inputs in, typed payloads and traces out. **The machine** — plain fixed code — owns the state: the open-question stack, the dependency order, the budget, the retries, the stopping rule, and the per-call context composition. One law runs through everything: a claim enters the state only as a typed row with provenance — never as free-form model prose. ## 1. RECEPTION — typing the goal A request arrives with (possibly) a tool list. One LLM call reads it and declares the **response type expected by the requester**: `answer` (a question to answer), `tool_call` (an action to take), `chat` (conversation to hold), `meta` (a floop-maintenance task), `plan_only` (a plan without execution). This typing annotates the goal — like `answer_type`, it may be tightened later by evidence. It is NOT a path switch: whatever the type, the goal is served by the same loop below. The typing costs one cheap call and buys a contract: the output is checked against the declared type on arrival, and a mismatch is a refusal-replan, not a silent guess. ## 2. The main loop The machine keeps the **stack of open questions**, each declaring its answer type and its dependencies (which questions it waits on). The loop, in words: 1. **Stop check.** No open questions? Ask the prover: is the goal derivable from the evidence table? Yes → synthesize the response. No → the decomposition was incomplete: one more LLM call asks "what is still missing?" and new questions open. Stopping is a formal condition, never a model's feeling. 2. **Pick.** Take the first question whose dependencies are all answered. If none can be picked, a question is waiting on a failed answer: handle the failure (re-open, re-decompose, escalate to the user). There is no search: dependency order IS execution order. 3. **Route.** One cheap LLM judgement, with the expert catalogue in view, picks who attends the question: a base expert, floop itself (recurse — too big for one call), or the user (expert of last resort). 4. **Execute.** The attendant runs. Its result is checked on arrival: type must match, claim must not contradict the table. A mismatch is refused — bounded reformulation attempts; a contradiction triggers dependency-directed backtracking (§6). Nothing untyped or unprovenanced is ever appended. 5. **Tick the budget.** Every LLM call and engine run costs. When the budget dies, the loop stops and the LLM answers directly from what the table holds — explicitly marked `Unverified`. ## 3. Tool action — an output of the loop, not an alternative to it When the goal type is `tool_call`, the loop does not change: the harness's tool schemas are evidence rows; "which tool satisfies the goal?", "what arguments does it need?", "which of the previous observations are relevant?" are **ordinary subquestions**, routed to experts like any other — logic reasons over schemas, analytic/numeric compute argument values, knowledge supplies world facts. The loop's output is a `tool_call` row (tool, arguments) whose provenance cites the schema rows and evidence that justify it; fixed code validates it against the schema before emission. The harness executes the tool and returns the observation, which enters the table as a `tool_result` row (a user-side fact, never "verified", never silently retracted). Then the loop iterates on the new observation — the next decision may be another tool, an expert, or stop. This is the ReAct pattern with every step typed, evidenced and auditable; for the harness it looks exactly like an LLM deciding tool calls. The prover's stopping predicate for tool goals: "the evidence supports a concrete (tool, arguments)". ## 4. The generic expert Every expert is the same shape: **translate → check → solve → return**. - An LLM call (a dspy Signature) turns the question plus its referenced evidence into the expert's formal format — the call receives a purpose-built context (§7), never the whole state. - A cheap formal check runs (well-typed, consistent units, well-formed query, trusted source); failures go back to the translating LLM for bounded retries (≤3). - The deterministic engine solves — no LLM inside. Out comes a typed payload with a trace: a proof, a derivation, a verbatim quote with URL and access date. - If nothing passes after the retries, the expert returns `Unverified` — the honest fallback; the loop decides what to do with the hole. Fleet specializations (formal formats, checks, engines, payloads) live in ARCHITECTURE.md §7. Two laws bear repeating: the LLM never computes — not even a multiplication (`numeric` exists to enforce this); and `knowledge` never paraphrases — its payload is the verbatim quote with provenance. ## 5. Recursion — floop inside floop A question too big for one expert call recurses: a child floop run on that question, knowing its parent goal. Same loop, same discipline, two differences: **context shapes adequacy** (the child knows why it is asked, so it knows when its answer is good enough) and **budget is partitioned** (a slice of the parent's, plus a depth cap; a child that cannot finish returns Unverified upward). The recursion bottoms out at the expert catalogue — base case: one expert call; recursive case: one more level of the same machine. There is no separate planner component: the plan is the open-question stack, planning is proposing reductions of it, execution is answering from it. ## 6. Failure and backtracking Failure is information. A contradiction in the table walks the provenance graph to the culprit assumption, marks it retracted, and retracts exactly its downstream rows — dependency-directed, not chronological; work that never depended on the dead assumption survives; the corpse stays for inspection and learning (append-only). Factual hypotheses (refutable by evidence) open branches; parameter assumptions (no evidence could refute them) are dimensions of the answer function, never branches. And one thing can never be retracted: the user. A user-supplied fact colliding with a source is presented verbatim to the user — never silently resolved. ## 7. Per-call context composition Every LLM call gets its own purpose-built context, assembled by fixed code within the model's window: - The machine state (evidence table, question stack, knowledge graph) is machine-sized — disk, database, RAM — and can exceed any window. **No call ever receives the whole state.** - For each call the composer assembles: the task (the Signature's inputs), the referenced rows (by id, compact summaries), the orientation the task needs (parent goal, section headers, document chunk). What went in is logged. - When the needed background exceeds the window, the machine **synthesizes** rather than truncates: compact row summaries, structured KG subgraphs, chunked documents (KNOWLEDGE.md). The callee gets what its function needs — not everything floop knows. - Different calls to the same Signature may carry different context sizes: the contract is the fields, not the window. ## 8. Stopping and responding The stop condition is formal (prover over the table; for tool goals: a concrete justified (tool, args) is derivable). Then — and only then — one final LLM pass composes the response in natural language from the evidence rows and traces: every number carries its source, every derivation its steps, every assumption its mark. If the budget died first, the response is composed anyway from what the table holds — explicitly marked `Unverified`. The product is the same in both cases: a reply that tells you exactly which parts are proven and which are, honestly, a guess. ## 9. Who does what - **LLM, always**: reading and typing requests; proposing subquestions; translating between language and formal formats; the routing judgement; composing responses. - **Fixed code, always**: the question stack and dependency ordering; budget partition; bounded retries; arrival checks; contradiction detection and retraction; refusing to stop; per-call context composition. - **The prover (a solver, not a model), always**: the stop condition; "answered" for any question; the final derivability check. - **Never the LLM**: computing values; deciding when it is finished; writing free-form rows into the state; silently picking between conflicting facts.