[Roadmap 3/5] Agent: split loop.py along its existing seams

Author: he-yufengCreated Sep 28, 2026Updated Sep 28, 2026

Part of the maintainer roadmap (#1621), item 3.

Problem

agent/src/agent/loop.py is 3,468 lines and one of the most-touched files (ten edits in the last two weeks alone). It owns tool dispatch, streaming/event emission, grounding plumbing, compaction triggers, and provenance recording in one module; each recent fix (#1599, #1601) required reading a lot of unrelated state.

Proposal

Split along the seams that already exist informally, into focused modules under agent/src/agent/ (tool-call lifecycle, streaming/events, grounding plumbing), keeping loop.py as the orchestrator. No behavior change; every extraction lands with the existing suites green and py_compile smoke intact.

Sequencing: one extraction per PR, smallest first, so each diff stays reviewable.