Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

feat(core): a turn is a loop of rounds - #37

Merged
mkhellat merged 3 commits into
mainfrom
feat/turn-loop
Dec 1, 2026
Merged

mkhellat merged 3 commits into
mainfrom
feat/turn-loop

Conversation

@mkhellat

@mkhellat mkhellat commented Dec 1, 2026

Copy link
Copy Markdown
Owner

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

  1. Round 1: the planner returns a reply (the turn ends) or a plan.
  2. After a round's steps run, an assessment call with the answering prompt (identity facts, history, the turn's observations, the request) returns either {"status": "answer", …} (the turn ends) or {"status": "more", "missing": …}. The model's own "missing" text goes into the next round's observations.
  3. Refused or invalid plans are observations too. The model sees the reason in the next round, whatever the reason was.
  4. When rounds run out (default 3), a plan repeats, or the user stops after something ran, one answer-only final call is made.
  5. Approval: the user approves the turn's first plan. Later rounds run read-only steps (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.
  6. History keeps one compact entry per turn ("round k: tool(target) → status; …" from tool and target only, security review M1) plus the reply. Outputs never enter it.

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:

  • the separate answer-after-steps call (feat(core): read what the steps printed, then answer the request #28);
  • planner rule 10 ("never answer with echo") and the echo-only plan check;
  • the shell-syntax-only retry;
  • the identity phrases "never claim / never say" (identity is now facts only: Codebud, the model and backend, the project, no shell);
  • the planner's unused final-call path.

Commits

  1. 3a5d95c feat(llm): schema-constrained model output
  2. a36fe9c feat(core): a turn is a loop of rounds
  3. 5bc26c3 test: chat_eval plays a turn through the loop

Measured (scripts/chat_eval.py --repeat 2, qwen2.5-coder 3B; both runs alike, zero spread)

Scenario main this PR time, 2 runs
first (18 checks) 13, 13 15, 15 361 → 547 s
log scenario (13 checks) 9, 9 9, 9 218 → 248 s
  • Where it gains: turn 7, where main's refused plan ended the turn, and this PR's model saw the refusal and answered; and turn 9.
  • What fails in both: summarising files, counting, and choosing the right log file. These are the 3B's limits.
  • Cost: about 50% more time per turn on the first scenario.
  • Earlier single-run comparisons were within run-to-run noise (feat(llm): temperature and seed settings; chat_eval --repeat #36), hence --repeat.

Review and CI

  • The loop was implemented by Opus; structured output, the assessment and the clean-up into atomic commits by Sonnet. I reviewed all of it before pushing. The split tree is identical to the measured one apart from the removed dead code (verified with git diff).
  • Each commit passes the full suite (1943, 1955, 1955) and ruff; the manual builds without warnings.
  • CI runs once on this head: core change.

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.
@mkhellat

mkhellat commented Dec 1, 2026

Copy link
Copy Markdown
Owner Author

Review summary: correct

One sweep, locally, over the design as built and the split.

Conditions.

  • The turn ends ⇔ round 1 replies ∨ an assessment answers ∨ the final call answers. The final call happens ⇔ the rounds run out ∨ a plan repeats an earlier one (same tools and args) ∨ the user stops after something ran.
  • A refused or invalid plan uses up a round and becomes an observation, so a model producing them can't stall the loop.
  • A later round runs a step without asking ⇔ file_read ∨ (harmless-list command ∧ ¬powerful ∧ ¬network ∧ no registered notice). Whether it may run at all is still refusal_reason() at execution.

Verified:

  • run_turn is bounded on every exit;
  • the schema per call kind (round / assess / answer-only final);
  • history comes from tool and target only, with no outputs;
  • the split tree equals the measured tree minus the dead final path (git diff against the pre-split tip is empty);
  • each commit passes the suite and ruff;
  • the measurement in the PR body: 15/15 vs 13/13 with zero spread, and a tie on the log scenario.

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.

@mkhellat

mkhellat commented Dec 1, 2026

Copy link
Copy Markdown
Owner Author

Merging. CI is green on Fedora and both Ubuntu jobs at 5bc26c3, plus the docs build. The two macOS jobs did not run: they stayed queued without a runner, and the maintainer decided to merge without them. If a macOS-only problem appears later, it will show on the next PR's CI. CI on the merge is skipped: the branch sits on main (75656bd), which has not moved, so the merged tree is the tested one; the merge commit carries [skip ci]. I start the docs workflow by hand. A merge commit keeps the three commits as written.

@mkhellat
mkhellat merged commit b4cdcab into main Dec 1, 2026
5 of 7 checks passed
@mkhellat
mkhellat deleted the feat/turn-loop branch December 1, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant