Skip to content

refactor: split the agent turn loop and the TUI app into modules - #251

Merged
hakula139 merged 2 commits into
mainfrom
refactor/split-turn-and-app
Oct 16, 2026
Merged

hakula139 merged 2 commits into
mainfrom
refactor/split-turn-and-app

Conversation

@hakula139

@hakula139 hakula139 commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Summary

As the first part of #148, this splits the two largest files by concern without changing behavior. The slash-command move into a core command module will follow in a separate PR.

  • Engine turn modules. agent.rs moves under the engine. The turn loop and stream collection move to engine/turn.rs, while the tool round, its permission gate, and approvals move to engine/dispatch.rs. The auto-compaction trigger moves to engine/compaction.rs with the summarization request. TurnAbort and await_unless_aborted are shared by all three modules, so they move to engine/abort.rs to keep the dispatch module from importing the turn module that calls it. Since the moved items are used only inside the engine, their visibility narrows to pub(super).
  • Post-compact message in the session. SUMMARY_PREFIX and the helpers that write and strip the synthetic post-compact message move to session/post_compact.rs. On resume, session history strips the prefix. This move also removes its import from the engine layer above it.
  • App modules. tui/app.rs retains the App state and its event loop. Terminal input, along with the actions and ops it produces, moves to app/dispatch.rs. Engine and git probe events, together with the turn lifecycle they drive, move to app/events.rs. Frame drawing moves to app/render.rs. Methods called across these modules have their visibility widened to pub(super).
  • Tests beside their code. Each test moves alongside the function it covers. The turn fixtures shared by the turn, dispatch, and compaction tests join the engine fixtures in engine/testing.rs. The app fixtures live in tui/app/testing.rs, and the app's render snapshots move with their test module. Two existing tests change: the approval-event check now asserts the exact event list, and the duplicate in-test writer is replaced with the shared crossterm_test_terminal fixture.
  • Docs. The module map drops the Agent layer, and the design-doc Sources point to the new files.

Part of #148.

Test plan

  • cargo fmt --all --check, cargo clippy --all-targets -- -D warnings, and cargo test pass. The test count matches main, with one new test.
  • The new test, modal_gate_lets_mouse_scrolls_reach_chat, verifies that the modal gate passes mouse scrolls through to chat. It fails if the gate swallows them.
  • pnpm format, pnpm lint, and pnpm spellcheck pass.
  • Codecov counts code in the new files as added lines, so moved lines that lacked coverage on main appear as patch misses. Most are test assertion arms that run only when a test fails: the overflow-retry check in the compaction tests, the reply_of fixture's panic, and the text-block check in the post-compact tests. The test_app fixture's browser stub runs only if a test opens a browser. The jump overlay's guard for a pill wider than the chat area cannot trigger because the label is truncated to fit.

@hakula139 hakula139 added this to the v0.1.0 milestone Oct 16, 2026
@hakula139 hakula139 added refactor Restructured code that preserves behavior (refactor) area: agent Touches the agent turn loop, compaction, or agent events area: tui Touches terminal UI rendering, input, or theming area: engine Touches the engine, its protocol, or the drivers that run it labels Oct 16, 2026
@hakula139 hakula139 self-assigned this Oct 16, 2026
@codecov

codecov Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

@hakula139
hakula139 force-pushed the refactor/split-turn-and-app branch from b438c42 to 528a0f8 Compare October 16, 2026 07:30
Move `agent.rs` under the engine and split it by concern:
`engine/turn.rs` holds the turn loop and stream collection,
`engine/dispatch.rs` holds the tool round with its permission gate and
approvals, and `engine/compaction.rs` absorbs the auto-compaction
trigger beside the summarization request. `engine/abort.rs` holds
`TurnAbort` and `await_unless_aborted`, which the turn loop, the tool
round, and compaction share, so the dispatch module no longer imports
from the turn module that calls it. The turn fixtures join the engine
fixtures in `engine/testing.rs`, and each test sits beside the code it
covers.

The post-compact message helpers move to `session/post_compact.rs`,
since the session history strips the prefix on resume and must not
import from the engine above it.

The moved items are only used inside the engine, so they narrow to
`pub(super)`. The module map drops the Agent layer, and the design-doc
Sources point at the new files.
`tui/app.rs` keeps the `App` state and its event loop. Terminal input
and the actions and ops it produces move to `app/dispatch.rs`, engine
and git probe events with the turn lifecycle they drive move to
`app/events.rs`, and frame drawing moves to `app/render.rs`. Each test
moves beside the code it covers, and the fixtures that several modules
share live in `app/testing.rs`, which also replaces a duplicate
in-test writer with `crossterm_test_terminal`.

Methods called across the new modules widen to `pub(super)`, the
render snapshots follow their test module, and the design-doc Sources
point at the new files. A new test covers the modal gate passing mouse
scrolls through to chat.
@hakula139
hakula139 force-pushed the refactor/split-turn-and-app branch from 528a0f8 to 89cc0e4 Compare October 16, 2026 13:45
@hakula139
hakula139 enabled auto-merge (squash) October 16, 2026 13:45
@hakula139
hakula139 merged commit e04dedb into main Oct 16, 2026
10 checks passed
@hakula139
hakula139 deleted the refactor/split-turn-and-app branch October 16, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: agent Touches the agent turn loop, compaction, or agent events area: engine Touches the engine, its protocol, or the drivers that run it area: tui Touches terminal UI rendering, input, or theming refactor Restructured code that preserves behavior (refactor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant