Open Code Review (OCR) — LLM loop (Runner)
The one shared tool-use loop underneath both internal/agent and internal/scan — sends messages, executes returned tool calls, manages context size. Package: internal/llmloop/.
What flows through it
Technical details
Package: internal/llmloop/
Depends on: llm, tool, session, model
Depended on by: Review agent, Scan agent
What actually happens here
Runner.RunPerFile (loop.go) is the literal for-loop: send ChatRequest, get tool calls back, executeToolCall each one, append results, repeat until task_done, MaxToolRequestTimes rounds exhausted (triggers a final 'grace round' offering only code_comment/task_done), 3 consecutive rounds with no usable result, or context compression fails to bring the conversation back under budget. compression.go runs three-zone compression (soft 60% / warn 80% of Template.MaxTokens) synchronously or as a background job per conversation. pool.go is the CommentWorkerPool that can run code_comment's line-resolution off the critical path.
How the LLM / model version is chosen
Model/provider/template are all passed in via Deps from the calling package (agent or scan) — llmloop itself makes no routing decision; it just calls Deps.LLMClient.CompletionsWithCtx(Model, Messages, Tools). The optional re-location call (diff.ReLocateComment, when a comment's line number can't be resolved deterministically) uses the same Deps.Model.
Deterministic (non-LLM) work in this module
Round/budget bookkeeping, empty-round detection, three-zone token-threshold compression triggers, and tool-call dispatch routing (built-in vs. dynamic MCP) are deterministic; only the model's own responses (what to call, what to say) are AI-driven.
Part of the module docs index. Full interactive version, with every module
side by side: architecture.html §7. Cross-cutting mechanisms
this module participates in (retry, compression, manifest, resume, hashing): LLD.