Skip to content

refactor(assistant): split run_session_turn into sibling API and CLI runners - #223

Merged
juacker merged 9 commits into
mainfrom
refactor/turn-runner
Sep 28, 2026
Merged

juacker merged 9 commits into
mainfrom
refactor/turn-runner

Conversation

@juacker

@juacker juacker commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Why

engine::run_session_turn was ~630 lines that ran the whole API turn inline (stream consumption, tool execution, context-limit recovery, persistence) while delegating CLI providers to local_agent. The two runners were not at the same level.

What

Pure refactor, no intended behaviour change.

  • engine::run_session_turn (now 37 lines) loads the session and connection once, validates (including a missing agent_workspace_id), resolves the workspace root once, then dispatches with a plain if to api_turn::run_session_turn or local_agent::run_session_turn. No trait. Callers are unchanged.
  • New assistant/api_turn.rs holds the API loop, split into prepare_api_request, try_context_limit_recovery, consume_api_stream, finalize_api_message, execute_api_tool_calls and ApiTurnState. The orchestrator is about 130 lines.
  • New assistant/turn_common.rs holds the helpers both runners use: trigger marker, empty-content rule, image preservation, and cleanup of unanswered input.

Tests

Characterisation tests, each checked by mutation:

  • partial API output persists across a reload;
  • tool calls are saved in order and replayed to the next request;
  • partial text is kept on cancel or stream error;
  • unanswered input is cleaned up while images are kept.

Known gap: there are no runner-level tests for these cases:

  • a tool failure being returned to the model;
  • forced compaction running only once;
  • the final run status;
  • UI event order.

The blocker is that AssistantDeps requires AppHandle<Wry>, while Tauri's mock_app gives MockRuntime. A follow-up PR will add a runtime seam.

Review

Two independent review rounds. Round 1 found a double workspace-root lookup, an unused adapter-injection seam, visibility that was too wide, and a stale comment; all were fixed. Round 2 returned production_quality, with a function-by-function mapping against main.

Manual test requested

API-provider chat with tool calls, plus a cancel partway through a reply.

@juacker
juacker marked this pull request as ready for review September 28, 2026 19:21
@juacker
juacker merged commit 70702fc into main Sep 28, 2026
2 checks passed
@juacker
juacker deleted the refactor/turn-runner branch September 28, 2026 19:21
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.

1 participant