{"record":{"id":"1068b32cbb44f43d","repo":"sigoden/aichat","slug":"already-in-a-agent-please-run-exit-agent-first","errorCode":null,"errorMessage":"Already in a agent, please run '.exit agent' first to exit the current agent.","messagePattern":"Already in a agent, please run '\\.exit agent' first to exit the current agent\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/config/mod.rs","lineNumber":1498,"sourceCode":"        }\n        self.rag_template\n            .as_deref()\n            .unwrap_or(RAG_TEMPLATE)\n            .replace(\"__CONTEXT__\", embeddings)\n            .replace(\"__INPUT__\", text)\n    }\n\n    pub async fn use_agent(\n        config: &GlobalConfig,\n        agent_name: &str,\n        session_name: Option<&str>,\n        abort_signal: AbortSignal,\n    ) -> Result<()> {\n        if !config.read().function_calling {\n            bail!(\"Please enable function calling before using the agent.\");\n        }\n        if config.read().agent.is_some() {\n            bail!(\"Already in a agent, please run '.exit agent' first to exit the current agent.\");\n        }\n        let agent = Agent::init(config, agent_name, abort_signal).await?;\n        let session = session_name.map(|v| v.to_string()).or_else(|| {\n            if config.read().macro_flag {\n                None\n            } else {\n                agent.agent_prelude().map(|v| v.to_string())\n            }\n        });\n        config.write().rag = agent.rag();\n        config.write().agent = Some(agent);\n        if let Some(session) = session {\n            config.write().use_session(Some(&session))?;\n        } else {\n            config.write().init_agent_shared_variables()?;\n        }\n        Ok(())\n    }","sourceCodeStart":1480,"sourceCodeEnd":1516,"githubUrl":"https://github.com/sigoden/aichat/blob/82976d349ad97ac9aae0655ad631dace5e2a6385/src/config/mod.rs#L1480-L1516","documentation":"Thrown by the agent-entry function in src/config/mod.rs:1498 when an agent session is already active (`config.read().agent.is_some()`). Only one agent may be active per config at a time, so starting another requires explicitly leaving the current one first; the message tells the user the exact command to do so.","triggerScenarios":"Running `.agent <other-name>` (or calling the entry function again) while already inside an agent session; scripts that invoke the agent command twice without exiting; nested agent switching without `.exit agent` in between.","commonSituations":"Users switching between agents interactively and forgetting the exit step; automation that loops over agent names reusing one session; confusion after a failed agent init left state partially set.","solutions":["Run `.exit agent` (or call `config.exit_agent()`) then start the new agent","Check `config.read().agent.is_some()` before entry and exit programmatically first","Refactor scripts to create a fresh session per agent instead of reusing one"],"exampleFix":"// before\nconfig.use_agent(\"other-agent\", None, signal).await?;\n// after\nif config.read().agent.is_some() {\n    config.exit_agent()?;\n}\nconfig.use_agent(\"other-agent\", None, signal).await?;","handlingStrategy":"validation","validationCode":"if config.read().agent.is_some() {\n    bail!(\"Already in an agent; run '.exit agent' before starting another\");\n}","typeGuard":null,"tryCatchPattern":"if let Err(e) = config.use_agent(name, session, signal).await {\n    if e.to_string().contains(\"Already in a agent\") {\n        config.exit_agent()?;\n        config.use_agent(name, session, signal).await?;\n    } else {\n        return Err(e.into());\n    }\n}","preventionTips":["Always exit the current agent before entering a new one","Track agent-session state in your tooling and mirror exit calls","Use one session per agent in scripts rather than reusing a session"],"tags":["agent","state-conflict","session","cli"],"backgroundTag":"invalid-state-transition","analyzedSha":"82976d349ad97ac9aae0655ad631dace5e2a6385","analyzedAt":"2026-09-09T18:33:06.139Z","contentChangedAt":"2026-09-09T18:33:06.139Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}