//! What a loop needs to start an agent session and read its console state: the //! shared context, `start_session` and `read_session_state`. Both the //! Implementation Loop and the Review Loop call it. use std::path::PathBuf; use std::sync::Arc; use crate::agent_manager::AgentManager; use crate::loop_driver::{self, Hooks, Launch, Limits, LoopSlot, Strength}; use crate::models::StrengthRow; use crate::session_reader; /// What the watch thread needs, shared by every step of one loop. #[derive(Clone)] pub struct Ctx { pub slot: LoopSlot, pub agent: Arc, pub console_dir: PathBuf, pub home: PathBuf, pub launch: Launch, pub run_id: u64, pub hooks: Arc, pub limits: Limits, } /// What a started session reports: the agent's generation and the model and /// effort it started on. #[derive(Clone, Debug, PartialEq, Eq)] pub struct SessionStart { pub generation: u64, pub model: Option, pub effort: Option, } /// Start a fresh agent on `prompt`. With `strength` (the word and its row) the /// agent starts on that row's model and effort with `PLAN2CODE_LOOP_STRENGTH` /// set; without it, on the agent's launch choice. pub fn start_session( ctx: &Ctx, prompt: &str, strength: Option<(Strength, &StrengthRow)>, ) -> Result { match strength { Some((word, row)) => { loop_driver::start_phase_with(&ctx.agent, &ctx.launch, prompt.to_string(), row, word)? } None => loop_driver::start_with(&ctx.agent, &ctx.launch, prompt.to_string())?, } let snapshot = ctx.agent.snapshot(); Ok(SessionStart { generation: snapshot.generation, model: snapshot.model, effort: snapshot.effort, }) } /// The console state of session `sid`, or `None` when it cannot be read now. pub fn read_session_state(ctx: &Ctx, sid: &str) -> Option { session_reader::load(&ctx.home, sid) .ok() .map(|session| session.state) }