Agent loop#
Covers the kernel's own required turn-by-turn control flow — not a plugin protocol. Nothing in this directory is optional plugin surface; every conforming kernel implementation MUST implement this directory's MUST-level behavior regardless of which providers happen to be loaded. The tone throughout is "here is what the kernel does," not "here is what a plugin author implements" — contrast with model/, tool/, context/, memory/, and frontend/, each of which a third-party plugin author actually builds against.
This design reflects patterns observed across roughly 16 agentic coding systems (Claude Code, Codex CLI, Gemini CLI, Aider, Cline, Kilo Code, opencode, Continue, Goose, OpenHands, SWE-agent, Zed, Plandex, Open Interpreter, Cursor, Windsurf/Cascade, Amp). Where a strong convergent pattern holds across independent implementations, this directory adopts it as a MUST. Where there's real divergence with no clear winner, a document here makes an explicit judgment call and says so, or carries the question into conformance.md. See architecture.md for the surrounding system (provider categories, Emit→Render→Paint, state backend, plan/apply terminology) — this directory only covers the kernel loop algorithm in detail.
Scope and definitions#
- Turn — one iteration of context-assemble → model call → tool resolution → apply → hook dispatch, corresponding to exactly one
StreamCompletioncall and its resultant tool executions. Seeturn-algorithm.md. - Session — one
RunSessioninvocation, spanningsession-starttosession-end, comprising one or more turns. Sessions form a tree viaparent_session_id(state-backend.md); the root session has no parent. Seesubagents.md. - Hook points, in the order they occur across the loop, per
architecture.md:session-start,context-assemble,pre-model-call,post-model-response,pre-tool-call,plan-ready,post-tool-call,post-apply,session-end.context-assemblethroughpost-applyrepeat every turn;session-startandsession-endfire exactly once per session. Seehook-dispatch.md.
Reading order#
turn-algorithm.md— the numberedRunTurnalgorithm, turn-level tool-call concurrency, loop termination and bounds (independent bound dimensions, cost accounting, limit-reached behavior, done detection, doom-loop detection).hook-dispatch.md— thepluggableharness.hook.v1wire contract, dispatch order and payload flow, subscriber error handling, timeout behavior, parallelism within one hook point, and theveto-mode subscription trust model.plan-apply-gate.md— plan construction and policy evaluation, decision semantics, the circuit breaker on repeated denials, and thedata_source/interactivepolicy precheck.subagents.md—RunSession's data types, context isolation, concurrency limits, session-hierarchy bookkeeping, structural depth limits, tool scoping at spawn, cancellation propagation, and the (deliberate) absence of inter-session communication.error-recovery.md— model-provider error handling and tool-provider (plugin) crash handling mid-turn.conformance.md— the MUST/SHOULD/MAY summary matrix and genuinely open questions.