feat(engine): inject mode-change runtime message and include mode in turn metadata
What
When the user switches modes mid-session (Agent → YOLO → Plan), the agent now receives a <codewhale:runtime_event kind="mode_change"> message in the conversation. This lets the agent re-evaluate any operations that were blocked by the previous mode approval policy.
Additionally, the current AppMode is now included in every turn <turn_meta> block so mid-turn messages (steer, REPL, LSP diagnostics) correctly report the active mode instead of always showing "Agent".
Why
Closes #2515. The core problem: users switch from Agent → YOLO mode with Alt+y, but the agent still says "I cannot do shell operations, you need to run them manually" — because it never received any signal that the mode changed.
Changes
engine.rs
- New
current_mode: AppModefield onEnginestruct — tracks the active mode Op::ChangeModehandling — injects amode_change_runtime_messageintosession.messages, which uses<codewhale:runtime_event kind="mode_change">XML (same pattern as sub-agent completion events). The message includes:- Policy description for the new mode
- Explicit re-evaluation hint ("Re-evaluate any previously blocked operations")
turn_metadata_block— now includesCurrent mode: {mode_label}lineuser_text_message_with_turn_metadata— usesself.current_modeinstead of hardcodedAppMode::Agentuser_text_message_with_turn_metadata_for_route— acceptsmode: AppModeparameterhandle_send_message— storesself.current_mode = modeearly
engine/tests.rs
turn_metadata_includes_current_mode— verifies mode appears in turn_metaturn_metadata_mode_updates_with_change_mode_op— verifies mid-turn messages pick up mode changesmode_change_runtime_message_format— verifies runtime event format- Updated
turn_metadata_includes_auto_model_routeto passAppMode::Agent
Verification
cargo test -p codewhale-tui # 3356 passed, 0 failed
cargo fmt --all -- --check # clean
Greptile Summary
This PR injects a <codewhale:runtime_event kind=\"mode_change\"> message into the conversation when the user switches modes mid-session, and adds the active AppMode to every turn's <turn_meta> block so the agent always has accurate context about its operating policy.
Enginegains acurrent_mode: AppModefield updated on bothOp::ChangeModeandOp::SendMessage;mode_change_runtime_messageconstructs the runtime event using the same XML pattern as sub-agent completion events.turn_metadata_blocknow accepts amode: AppModeparameter and emits aCurrent mode:line;user_text_message_with_turn_metadatathreadsself.current_modethrough automatically.turn_loop.rsupgrades the prefix-freeze diagnostic from Phase 2 (warn-only) to Phase 3 (emitting a one-shotPrefixCacheChangeevent on the first turn freeze).
Confidence Score: 4/5
Safe to merge with one fix: Op::EditLastTurn hardcodes AppMode::Agent instead of self.current_mode, silently overwriting the engine's tracked mode and re-dispatching the edited turn under the wrong approval policy.
The Op::EditLastTurn handler passes a hardcoded AppMode::Agent to handle_send_message, which immediately writes it back to self.current_mode. A YOLO or Plan user who runs /edit will have their re-sent message processed with Agent-level approval restrictions, and the engine's mode state will be permanently overwritten to Agent for all subsequent turns until the next SendMessage or ChangeMode — directly contradicting the purpose of this PR.
crates/tui/src/core/engine.rs — the Op::EditLastTurn arm at line 1158
Important Files Changed
| Filename | Overview |
|---|---|
| crates/tui/src/core/engine.rs | Adds current_mode tracking, Op::ChangeMode runtime message injection, and mode in turn metadata — but Op::EditLastTurn hardcodes AppMode::Agent instead of self.current_mode, silently overwriting the active mode on /edit and causing wrong approval policy for YOLO/Plan users. |
| crates/tui/src/core/engine/tests.rs | Adds integration test change_mode_op_injects_runtime_event_into_session_messages (exercises the full Op dispatch path) plus three unit tests covering mode in turn metadata and message format; turn_metadata_mode_updates_with_change_mode_op still sets engine.current_mode directly rather than via Op dispatch, but the new async test compensates for the most critical path. |
| crates/tui/src/core/engine/turn_loop.rs | Upgrades prefix-freeze from Phase 2 (warn-only) to Phase 3 (emit one-shot PrefixCacheChange event on first freeze); straightforward and self-contained. |
Sequence Diagram
sequenceDiagram
participant UI
participant Engine
participant Session
participant Agent
UI->>Engine: "Op::ChangeMode { mode: Yolo }"
Engine->>Engine: "previous_mode = current_mode (Agent)"
Engine->>Engine: "current_mode = Yolo"
Engine->>Session: refresh_system_prompt(Yolo)
Engine->>UI: emit_session_updated()
Engine->>Session: add_message(mode_change_runtime_event)
Engine->>UI: emit_session_updated()
Engine->>UI: Event::status("Mode changed to: YOLO mode")
UI->>Engine: "Op::SendMessage { mode: Yolo, ... }"
Engine->>Engine: "current_mode = Yolo"
Engine->>Session: add user_msg with turn_meta (Current mode: YOLO mode)
Engine->>Agent: MessageRequest (messages including mode_change event + turn_meta)
Agent-->>Engine: response (re-evaluates blocked operations)
Note over Engine: Op::EditLastTurn bypasses current_mode
UI->>Engine: "Op::EditLastTurn { new_message }"
Engine->>Session: truncate to last user message
Engine->>Engine: "mode = AppMode::Agent (hardcoded!)"
Engine->>Engine: "handle_send_message(mode=Agent) overwriting current_mode"
Engine->>Agent: MessageRequest (turn_meta shows Agent, YOLO ops blocked)
Comments Outside Diff (1)
-
crates/tui/src/core/engine.rs, line 1158-1161 (link)EditLastTurnsilently resets active mode to AgentOp::EditLastTurnpassesAppMode::Agenttohandle_send_message, which immediately overwritesself.current_mode = mode. A user who has switched to YOLO or Plan mode and then uses/editwill have their turn re-dispatched under Agent restrictions — and the engine's tracked mode will be permanently downgraded to Agent for all subsequent mid-turn messages too. The comment on the same line says "reusing the engine's stored mode/model config", but the mode is not reused at all. Usingself.current_modein place of the hardcodedAppMode::Agentwould match the intent.
Reviews (2): Last reviewed commit: "fix(engine): address review feedback — i..." | Re-trigger Greptile
Thanks @encyc for taking the time to contribute.
This repository is currently observing a maintainer-managed contribution gate in dry-run mode, so this pull request is staying open. When enforcement is enabled, pull requests from contributors who are not listed in .github/APPROVED_CONTRIBUTORS will be closed automatically.
Please read CONTRIBUTING.md for the expected contribution shape. A maintainer can grant PR access by commenting /lgtm on a pull request.
[!WARNING] You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!