JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
id: agentserver-plan-mode-exit name: AgentServer Plan Mode Exit status: partial confidence: confirmed last_updated: 2026-07-14 user_visible_surface: "TUI plan approval dialog, plan-mode indicator, and the return to code.normal after approval." source_of_truth: - "session checkpointer: DeepAgentState.plan_mode" - "workspace plan file resolved from plan_mode.plan_slug" modules: - agentserver-runtime - agent-harness - gateway-and-channels directories: - jiuwenswarm/server - jiuwenswarm/server/runtime/agent_adapter - jiuwenswarm/agents/harness/code/rails - jiuwenswarm/agents/harness/common/rails/interrupt - jiuwenswarm/channels/tui/frontend/src - tests/unit_tests/agentserver code_symbols: - AgentWebSocketServer._ensure_code_mode_state - AgentWebSocketServer._check_post_process_plan_exit - AgentWebSocketServer._push_plan_mode_exited - PlanApprovalInterruptRail.resolve_interrupt - CodeAgentModeRail.after_tool_call - convert_interactions_to_ask_user_question entrypoints: - "TUI /plan [request] -> chat.send(mode=code.plan)" - "LLM exit_plan_mode tool call -> approval interrupt" - "TUI approval answer -> chat.send interrupt resume"
AgentServer Plan Mode Exit
Outcome
A code-mode user can inspect a generated plan, approve or reject it, and—only after approval—resume in code.normal. Durable agent state, stale-mode guards, and plan.mode_exited keep the agent and TUI from re-entering the completed plan.
Causal Path
/plansets TUI mode tocode.planand attaches one-shotplan_entry_source: slash_commandtochat.send._prepare_code_mode_chat_turnselects the project-scoped code agent;_ensure_code_mode_statehydrates the session, switches durable mode toplan, clears stale slug state, and adds the activation reminder.- The model calls
enter_plan_mode, which assignsplan_slug; plan-mode rails restrict writes to the resolved plan file. After writing it, the model callsexit_plan_mode. PlanApprovalInterruptRail(priority 78) intercepts that tool before execution. It reads the file and emits a confirm interrupt with path/slug and a 3,000-character preview. Normalization converts it tochat.ask_user_questionwithrequest_id, approve/reject options, and plan metadata.- TUI stores
pendingQuestionand currentresumeMode. Its answer is a new empty-querychat.sendcarrying the samerequest_id, answers, source, resume mode, and plan metadata; rejection may add revision feedback. is_interrupt_resume_payloadrecognizes this request, so_ensure_code_mode_stateskips synchronization: stale clientcode.plancannot overwrite a just-restored checkpoint. The adapter suppliesInteractiveInputto the suspended confirm rail. Approval executes the tool; rejection yieldsRejectResult, sets_plan_rejected, skips it, and stays in plan mode.- Approved
ExitPlanModeTool.invokereads the full plan and immediately callsrestore_mode_after_plan_exit: mode becomespre_plan_modeornormal,pre_plan_modeis cleared,plan_slugis retained, andsave_stateupdates checkpoint-backed session state. For an empty plan,CodeAgentModeRail.after_tool_callrestores once a real result exists. The exit notification tells the model writes are allowed. - Unary and streaming
finallyblocks run_check_post_process_plan_exit, which creates a fresh session view, runspre_run, and reloads persisted state. If a request that began ascode.planis now normal, it records_plan_exited_sessionsand pushesplan.mode_exitedwith channel/session identity. TUI changes a currentcode.*mode tocode.normal.
State Classification
- Durable source of truth: checkpointer-backed
DeepAgentState.plan_modeand the workspace plan file. Request params and TUI mode are projections. - Process-local coordination:
_session_mode_sync_locksserializes request-start mode synchronization per session._plan_exited_sessionsis a one-shot stale-reentry guard, not durable truth. - Client/transient state: TUI
mode, pending entry/question/resume state; interrupt correlation; the exit push. - Derived output: the approval preview and plan metadata, the model-facing exit notification, and the mode-exited event.
Replay, Restore, Or Reconstruction
pre_run reconstructs state from the checkpointer. A missed exit push does not roll back the durable exit. On the next ordinary code.plan chat, _ensure_code_mode_state rewrites to code.normal when either the session is in _plan_exited_sessions or normal persisted state still has a plan_slug; the latter is cleared and persisted. Both paths repush the event.
Explicit /plan is the exception: its source marker discards the flag and permits fresh entry, which clears the old slug. Interrupt resumes skip repair to continue the suspended tool. Background methods do not synchronize mode.
Contract
- Plan request:
chat.sendcarriesmode: code.plan; explicit entry additionally carries one-shotplan_entry_source: slash_command. - Approval request:
chat.ask_user_questioncarries non-emptyrequest_id,source: confirm_interrupt, options, and plan metadata. - Approval response:
chat.sendcarries the same ID, non-empty answers, source, empty query, and captured resume mode. - Exit notification: server push payload is
{event_type: "plan.mode_exited", mode: "code.normal"}and is routed with channel/session identity. It has no checkpoint revision or approval request ID.
Consumer State And Output
The TUI renders the interrupt preview; the executing model receives the full plan from ExitPlanModeTool. Approve continues the run in normal mode. Reject keeps code.plan, returns feedback, and emits no exit event. TUI mode is not the commit point.
Failure, Ordering, And Identity
- The per-session lock covers only
_ensure_code_mode_state; restore, post-process reload, push, and answer correlation run outside it. Concurrent same-session requests can observe different exit-boundary snapshots. - The lock map is unpruned.
_plan_exited_sessionsis process-local and lost on restart; persisted slug is the durable fallback. - Post-process checking runs from
finallyand reads state, not tool output. Replayedcode.plantraffic already in normal mode may duplicate an exit event and guard entry. The event lacks an idempotency key, though TUI assignment is idempotent. - A push can be dropped when no Gateway socket is active. Recovery occurs only when a later chat hits a stale-mode guard; no reconnect-time mode hydration was found.
- Plan-file read failure yields an empty approval body. Empty-plan approval is restored by the after-tool fallback. Restore failure in that fallback is logged and swallowed, leaving persisted plan mode unchanged and suppressing the server's exit detection.
request_idcorrelates approval. Suspended-tool matching and duplicate/stale answers are delegated to the harness and lack end-to-end replay coverage.
Verification
Evidence was checked in AgentServer, rails, interrupt helpers, pinned openjiuwen==0.1.15.post3, and TUI state/handlers. Tests cover mode resolution/sync, slash re-entry, sync exclusions, interrupt routing, approval conversion/mounting, plan write restrictions, and TUI approval/reject UI.
Focused checks are test_plan_mode_orchestration.py, test_code_agent_mode_rail.py, test_agentserver_modes.py, test_structured_ask_user.py, test_swarm_assembly.py, and TUI run-tests.mjs.
Known Gaps
No focused test covers _check_post_process_plan_exit, exit-push delivery into TUI state, missed/duplicate push, plan_slug restart fallback, restore failure, stale or duplicate approval answers, or concurrent same-session plan exit versus a new chat. A live Gateway-WebSocket-TUI test is still needed to confirm ordering across approval, checkpoint persistence, resumed model output, and the final mode indicator.