Load a session by ID. Returns `Ok(None)` if the session file doesn't exist (not an error). # Errors Returns `AgentError::SessionIdInvalid` if the ID contains path traversal characters. Returns `AgentError::SessionLoadFailed` if the file exists but can't be parsed.
(&self, session_id: &str)
| 458 | /// Returns `AgentError::SessionIdInvalid` if the ID contains path traversal characters. |
| 459 | /// Returns `AgentError::SessionLoadFailed` if the file exists but can't be parsed. |
| 460 | pub fn load(&self, session_id: &str) -> AgentResult<Option<AgentSession>> { |
| 461 | validate_session_id(session_id)?; |
| 462 | |
| 463 | let path = self.session_path(session_id); |
| 464 | |
| 465 | let data = match std::fs::read(&path) { |
| 466 | Ok(data) => data, |
| 467 | Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), |
| 468 | Err(e) => { |
| 469 | return Err(AgentError::SessionLoadFailed { |
| 470 | session_id: session_id.to_string(), |
| 471 | reason: e.to_string(), |
| 472 | }); |
| 473 | } |
| 474 | }; |
| 475 | |
| 476 | let session: AgentSession = |
| 477 | serde_json::from_slice(&data).map_err(|e| AgentError::SessionLoadFailed { |
| 478 | session_id: session_id.to_string(), |
| 479 | reason: format!("JSON parse error: {}", e), |
| 480 | })?; |
| 481 | |
| 482 | Ok(Some(session)) |
| 483 | } |
| 484 | |
| 485 | /// Save a session to disk atomically. |
| 486 | /// |