Repository navigation
feat(core): a turn is a loop of rounds - #37
Conversation
The planner asks a small local model for JSON and, when the model answers with prose or a malformed object, has to retry. Ollama can enforce a JSON schema while it generates (the request's "format" field), so the output is valid JSON of the expected shape whatever the model would have said. call_llm now takes an optional schema and sends it to Ollama; the OpenAI backend ignores it, since structured outputs there need a model that supports them and a stricter schema dialect (the default gpt-3.5-turbo does not), so its requests stay as they were. The planner sends round_schema() with every planning call and with both of its retries: a reply, a plan (steps need id, description, tool and args; the tool is one of the registered ones) or a plan error. A test checks that the schema and the planner's own validation agree on a sample of objects. * NEWS: Mention the constrained output. * docs/chapters/architecture.texi (LLMPlanner): Describe the schema. * docs/chapters/llm-execution.texi (call lifecycle): Likewise. * docs/chapters/development.texi (test layout): List test_structured_output.py. * src/codebud/llm_stub.py (call_llm, _call_ollama): New argument schema, sent as the request's "format". * src/codebud/planner.py (round_schema): New. (LLMPlanner.generate_plan, LLMPlanner._attempt): Send it. * tests/test_structured_output.py: New. * tests/test_backend.py, tests/test_llm_stub.py, tests/test_planner.py, tests/test_plan_args.py, tests/test_rag_context.py, tests/test_remote_ollama.py, tests/test_reply_or_plan.py, tests/test_safety_policy.py: Model stubs accept the schema argument.
A request used to get one plan, one run of its steps and one answer call. A small model that read the wrong file, used a pipe or forgot an argument had no way to correct itself. A turn is now a bounded sequence of rounds (codebud/loop.py): the model plans, the steps run, and it sees their output (masked and bounded) or why a step or plan was refused, then plans further steps or replies, up to three rounds that run steps. Every step still goes through the executor and so the safety engine, the scope guard, integrity and the evidence recorder. In ask mode the first plan is approved as before; later rounds run read-only steps unasked and ask for the rest. Refused or invalid plans are not run: their reason becomes an observation for the next round. After each round whose steps ran, the model is asked with an answer-shaped prompt (identity, history, observations, the request; not the planner prompt, which makes a small model plan again instead of answering) whether it can answer now or what is still missing. An answer ends the turn; the "missing" note is added to the observations of the next round. When the rounds run out, a plan repeats an earlier one or the user stops the turn after something ran, one final call asks for an answer only (a schema that allows nothing else) and says what could not be found out. Both calls are schema-constrained with Ollama. The loop replaces answer_after_steps and its one answer call, rule 10 of the planner prompt, the check that refused echo-only plans and the shell-only retry: the model now sees such a refusal and corrects itself. The prompts state identity facts only (codebud, the model and backend, the project, that steps run without a shell); nothing in them steers what to answer. codebud plan and plan mode still plan round one only (handle_user_message). * NEWS: Describe rounds, the assessment and the final call. * docs/chapters/architecture.texi (The Agent Loop, AgentCore) (LLMPlanner): Rewrite for the loop; add the assessment. * docs/chapters/llm-execution.texi (call chain): Likewise. * docs/chapters/usage.texi (How a Turn Works): Describe rounds, approval, the assessment and history. * docs/chapters/privacy.texi (Where Data Can Go): Step output goes to the model each round. * docs/chapters/troubleshooting.texi: A refused plan is shown to the model, not an error. * docs/chapters/development.texi (test layout): List test_agent_loop.py; drop test_answer_after_steps.py. * src/Makefile.am (codebud_PYTHON): Add loop.py. * src/codebud/loop.py: New: run_turn, LoopBudget, observations, the history entry, the assessment and the final call. * src/codebud/core.py (AgentCore.run_turn, begin_turn, plan_round) (assess_round, end_turn, is_harmless): New. (AgentCore.handle_user_message): Plan round one only. (AgentCore.answer_after_steps, step_summary): Remove; step_summary moves to loop.py. * src/codebud/llm_stub.py (call_llm_assess, ASSESS_SCHEMA) (ANSWER_SCHEMA): New. (call_llm_answer, format_step_results): Remove. * src/codebud/planner.py (generate_plan): Take the turn's observations; no corrective retry for an invalid plan, the loop shows it to the model. Drop rule 10, the echo-only and shell-only checks. * src/codebud/persona.py (identity_block): Facts only. * src/codebud/safety.py (SafetyEngine.is_harmless): New. * src/codebud/cli/steps.py (RoundRunner): New. * src/codebud/cli/run.py, src/codebud/cli/chat.py (cmd_run, cmd_chat): Play the turn with run_turn. * src/codebud/cli/common.py (thinker): New. * src/codebud/cli/display.py: Show rounds. * tests/test_agent_loop.py: New. * tests/test_answer_after_steps.py: Remove. * tests/test_cli.py, tests/test_plan_args.py, tests/test_reply_or_plan.py, tests/test_structured_output.py: Follow the loop.
chat_eval planned a request with handle_user_message, ran the first plan in full-auto and, for the answer, called AgentCore.answer_after_steps, which the turn loop replaced. It now plays each request the way chat does: AgentCore.run_turn with a RoundRunner in full-auto, every round. A turn's text is its reply (or its error), and the steps recorded are those of every round, so the checks and the A-versus-B comparison see what the user would. * scripts/chat_eval.py (play_turn): Use run_turn and RoundRunner; report the number of rounds. (_final_text): Read the turn's reply or error.
Review summary: correctOne sweep, locally, over the design as built and the split. Conditions.
Verified:
Known, accepted: about 50% more time per turn on the 3B. The remaining failures (reading and choosing files, counting) are the model's limits, not the loop's. Verdict: correct. I'll merge when CI passes. |
|
Merging. CI is green on Fedora and both Ubuntu jobs at |
A turn becomes a loop of rounds. This implements the design the maintainer approved after the failed release-gate retests (agent-loop design, decisions of 2026-11-26).
Until now a turn was one shot: plan → run → answer from whatever came back. If the first plan was refused or looked at the wrong thing, nothing let the model try again.
How a turn works now
{"status": "answer", …}(the turn ends) or{"status": "more", "missing": …}. The model's own "missing" text goes into the next round's observations.file_read, harmless-list commands that are not powerful, need no network, and carry no notice from a registered layer) without asking; anything else is still asked. Safety, the scope guard and fence, integrity and the evidence recorder are unchanged for every step of every round.Model output is schema-constrained (Ollama
format): a round's call can only return a reply, a plan or an error, the assessment only answer or more, and the final call only an answer.Removed, because they were failure-specific or replaced by the loop:
Commits
3a5d95cfeat(llm): schema-constrained model outputa36fe9cfeat(core): a turn is a loop of rounds5bc26c3test: chat_eval plays a turn through the loopMeasured (
scripts/chat_eval.py --repeat 2, qwen2.5-coder 3B; both runs alike, zero spread)mainmain's refused plan ended the turn, and this PR's model saw the refusal and answered; and turn 9.--repeat.Review and CI
git diff).