Defend against invalid sessions (#3229)
Co-authored-by: Douwe Osinga <douwe@squareup.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use crate::session::message_to_markdown;
|
||||
use anyhow::{Context, Result};
|
||||
use cliclack::{confirm, multiselect, select};
|
||||
use goose::session::info::{get_session_info, SessionInfo, SortOrder};
|
||||
use goose::session::info::{get_valid_sorted_sessions, SessionInfo, SortOrder};
|
||||
use goose::session::{self, Identifier};
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
@@ -75,7 +75,7 @@ fn prompt_interactive_session_removal(sessions: &[SessionInfo]) -> Result<Vec<Se
|
||||
}
|
||||
|
||||
pub fn handle_session_remove(id: Option<String>, regex_string: Option<String>) -> Result<()> {
|
||||
let all_sessions = match get_session_info(SortOrder::Descending) {
|
||||
let all_sessions = match get_valid_sorted_sessions(SortOrder::Descending) {
|
||||
Ok(sessions) => sessions,
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to retrieve sessions: {:?}", e);
|
||||
@@ -125,7 +125,7 @@ pub fn handle_session_list(verbose: bool, format: String, ascending: bool) -> Re
|
||||
SortOrder::Descending
|
||||
};
|
||||
|
||||
let sessions = match get_session_info(sort_order) {
|
||||
let sessions = match get_valid_sorted_sessions(sort_order) {
|
||||
Ok(sessions) => sessions,
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to list sessions: {:?}", e);
|
||||
@@ -294,7 +294,7 @@ fn export_session_to_markdown(
|
||||
/// Shows a list of available sessions and lets the user select one
|
||||
pub fn prompt_interactive_session_selection() -> Result<session::Identifier> {
|
||||
// Get sessions sorted by modification date (newest first)
|
||||
let sessions = match get_session_info(SortOrder::Descending) {
|
||||
let sessions = match get_valid_sorted_sessions(SortOrder::Descending) {
|
||||
Ok(sessions) => sessions,
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to list sessions: {:?}", e);
|
||||
|
||||
@@ -225,14 +225,15 @@ async fn list_sessions() -> Json<serde_json::Value> {
|
||||
Ok(sessions) => {
|
||||
let session_info: Vec<serde_json::Value> = sessions
|
||||
.into_iter()
|
||||
.map(|(name, path)| {
|
||||
let metadata = session::read_metadata(&path).unwrap_or_default();
|
||||
serde_json::json!({
|
||||
"name": name,
|
||||
"path": path,
|
||||
"description": metadata.description,
|
||||
"message_count": metadata.message_count,
|
||||
"working_dir": metadata.working_dir
|
||||
.filter_map(|(name, path)| {
|
||||
session::read_metadata(&path).ok().map(|metadata| {
|
||||
serde_json::json!({
|
||||
"name": name,
|
||||
"path": path,
|
||||
"description": metadata.description,
|
||||
"message_count": metadata.message_count,
|
||||
"working_dir": metadata.working_dir
|
||||
})
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
@@ -246,7 +247,6 @@ async fn list_sessions() -> Json<serde_json::Value> {
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_session(
|
||||
axum::extract::Path(session_id): axum::extract::Path<String>,
|
||||
) -> Json<serde_json::Value> {
|
||||
@@ -259,17 +259,21 @@ async fn get_session(
|
||||
}
|
||||
};
|
||||
|
||||
let error_response = |e: Box<dyn std::error::Error>| {
|
||||
Json(serde_json::json!({
|
||||
"error": e.to_string()
|
||||
}))
|
||||
};
|
||||
|
||||
match session::read_messages(&session_file) {
|
||||
Ok(messages) => {
|
||||
let metadata = session::read_metadata(&session_file).unwrap_or_default();
|
||||
Json(serde_json::json!({
|
||||
Ok(messages) => match session::read_metadata(&session_file) {
|
||||
Ok(metadata) => Json(serde_json::json!({
|
||||
"metadata": metadata,
|
||||
"messages": messages
|
||||
}))
|
||||
}
|
||||
Err(e) => Json(serde_json::json!({
|
||||
"error": e.to_string()
|
||||
})),
|
||||
})),
|
||||
Err(e) => error_response(e.into()),
|
||||
},
|
||||
Err(e) => error_response(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1259,7 +1259,6 @@ impl Session {
|
||||
);
|
||||
}
|
||||
|
||||
/// Get the session metadata
|
||||
pub fn get_metadata(&self) -> Result<session::SessionMetadata> {
|
||||
if !self.session_file.as_ref().is_some_and(|f| f.exists()) {
|
||||
return Err(anyhow::anyhow!("Session file does not exist"));
|
||||
|
||||
Reference in New Issue
Block a user