(
repo: &Repository,
store: Option<&SessionStore>,
session_id: &str,
)
| 237 | } |
| 238 | |
| 239 | fn current_plan( |
| 240 | repo: &Repository, |
| 241 | store: Option<&SessionStore>, |
| 242 | session_id: &str, |
| 243 | ) -> CliResult<Option<String>> { |
| 244 | let runtime = runtime_session(store, session_id)?; |
| 245 | let ledger = repo |
| 246 | .get_session_ledger(session_id) |
| 247 | .map_err(CliError::Repository)?; |
| 248 | if runtime.is_none() && ledger.is_none() { |
| 249 | return Err(invalid(format!( |
| 250 | "Unknown explicit session context: {session_id}" |
| 251 | ))); |
| 252 | } |
| 253 | let mut keys = BTreeSet::new(); |
| 254 | let runtime_active = runtime.as_ref().is_some_and(AgentSession::is_turn_active); |
| 255 | if let Some(runtime) = runtime { |
| 256 | if runtime.view_name != repo.current_view() { |
| 257 | return Err(invalid( |
| 258 | "Explicit session context belongs to a different view", |
| 259 | )); |
| 260 | } |
| 261 | if runtime.is_turn_active() { |
| 262 | let turn = runtime |
| 263 | .turn_count |
| 264 | .checked_add(1) |
| 265 | .ok_or_else(|| invalid("Turn overflow"))?; |
| 266 | if let Some(binding) = store |
| 267 | .map(|s| s.turn_intent_binding(session_id, turn)) |
| 268 | .transpose() |
| 269 | .map_err(|e| CliError::Internal(e.into()))? |
| 270 | .flatten() |
| 271 | { |
| 272 | keys.insert(intent_key(repo, &binding.intent_uid)?); |
| 273 | } |
| 274 | if let Some(work_item) = runtime.managed_run.and_then(|run| run.work_item_id) { |
| 275 | keys.insert(intent_key(repo, &work_item)?); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | if let Some((record, turns)) = ledger.filter(|_| !runtime_active) { |
| 280 | if record |
| 281 | .view_name |
| 282 | .as_deref() |
| 283 | .is_some_and(|view| view != repo.current_view()) |
| 284 | { |
| 285 | return Err(invalid( |
| 286 | "Explicit session ledger belongs to a different view", |
| 287 | )); |
| 288 | } |
| 289 | for plan in turns.iter().filter_map(|turn| turn.plan_id.as_deref()) { |
| 290 | keys.insert(intent_key(repo, plan)?); |
| 291 | } |
| 292 | } |
| 293 | if keys.len() > 1 { |
| 294 | return Err(invalid( |
| 295 | "Ambiguous governing intent in explicit session context", |
| 296 | )); |
no test coverage detected