Skip to content

tui: centralize agent rebuild in one rebuild_agent helper - #201

Merged
gi-dellav merged 1 commit into
gi-dellav:mainfrom
wowi42:refactor/centralize-agent-rebuild
Jul 19, 2026
Merged

gi-dellav merged 1 commit into
gi-dellav:mainfrom
wowi42:refactor/centralize-agent-rebuild

Conversation

@wowi42

@wowi42 wowi42 commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Implements item 2 of Control flow and duplication from #186:

Centralize agent rebuild in one rebuild_agent(session, client, context, ...) helper used by start_main_run, slash commands, and mid-turn compaction.

What changed

Every main-agent rebuild path hand-rolled the same completion_model / resolve_temperature / resolve_extra_body + build_agent preamble (~15 copies across app, event_handler, and slash commands).

Introduced AgentBuildCtx (src/ui/state.rs): a borrow bundle of the build inputs with a single rebuild_agent(model_id, reasoning_enabled) method, obtainable from UiContext::agent_build_ctx() (main loop, mid-turn compaction) and SlashCtx::agent_build_ctx() (slash commands), or built from owned clones in the startup prebuild task.

Converted call sites:

  • ensure_agent — used by start_main_run, mid-turn compaction, worktree-merge run
  • handle_compress — /compress and auto/mid-turn compaction
  • SlashCtx::rebuild_agent / rebuild_agent_with_client / switch_to_prompt_model, apply_prompt_model — all slash-command rebuilds (/model, /provider, /add, /mode, prompt-model switches, ...)
  • /loop respawn and worktree-return respawn (event_handler.rs)
  • worktree startup (x2), worktree exit, MCP reconnect, background prebuild (app.rs)
  • apply_model / handle_model (slash/providers.rs)

Net: −312 / +149 lines. Out of scope and untouched: subagent and /btw builders, non-TUI one-shot/ACP modes.

Behavior preserved

  • advisor global-state syncs keep their original order at slash sites
  • /loop and worktree respawns keep hardcoded reasoning = true
  • model switches still build against the not-yet-committed model id
  • MCP lazy-connect stays at the startup sites

Verification

  • cargo fmt
  • cargo test — 608 passed (default features)
  • cargo test --features advisor,hooks,memory — 784 passed
  • cargo test --no-default-features — 510 passed
  • cargo install --path . --debug OK

Refs #186

Item 2 of "Control flow and duplication" from gi-dellav#186. Every main-agent
rebuild path used to hand-roll the same completion_model /
resolve_temperature / resolve_extra_body + build_agent preamble
(~15 copies across app, event_handler, slash commands).

Introduce AgentBuildCtx, a borrow bundle of the build inputs with a
single rebuild_agent(model_id, reasoning_enabled) method, obtainable
from UiContext (main loop, mid-turn compaction) and SlashCtx (slash
commands), or built from owned clones in the startup prebuild task.

Converted: ensure_agent (start_main_run, mid-turn compaction), /loop
and worktree respawns, worktree startup/exit, MCP reconnect, prebuild,
SlashCtx::rebuild_agent(_with_client), switch_to_prompt_model,
apply_prompt_model, handle_compress, /model switching.

Behavior preserved: advisor syncs keep their order, /loop and worktree
respawns keep hardcoded reasoning=true, model switches build against
the not-yet-committed model id, MCP lazy-connect stays at startup.

Refs gi-dellav#186
@gi-dellav
gi-dellav merged commit 7e1c3b9 into gi-dellav:main Jul 19, 2026
12 checks passed
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.

2 participants