-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat(/new): add new session command #2235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -135,6 +135,73 @@ pub fn fork(app: &mut App) -> CommandResult { | |||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// Start a fresh saved session from the current TUI state. | ||||||||||||||||||||||||||||||||||||
| pub fn new_session(app: &mut App, arg: Option<&str>) -> CommandResult { | ||||||||||||||||||||||||||||||||||||
| let force = match arg.map(str::trim).filter(|s| !s.is_empty()) { | ||||||||||||||||||||||||||||||||||||
| None => false, | ||||||||||||||||||||||||||||||||||||
| Some("--force" | "force") => true, | ||||||||||||||||||||||||||||||||||||
| Some(other) => { | ||||||||||||||||||||||||||||||||||||
| return CommandResult::error(format!( | ||||||||||||||||||||||||||||||||||||
| "Usage: /new [--force]\n\nUnknown argument: {other}" | ||||||||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if !force { | ||||||||||||||||||||||||||||||||||||
| let blockers = new_session_blockers(app); | ||||||||||||||||||||||||||||||||||||
| if !blockers.is_empty() { | ||||||||||||||||||||||||||||||||||||
| return CommandResult::error(format!( | ||||||||||||||||||||||||||||||||||||
| "Cannot start a new session while {}. Run `/new --force` to discard pending work and start a fresh session.", | ||||||||||||||||||||||||||||||||||||
| blockers.join(", ") | ||||||||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let new_id = uuid::Uuid::new_v4().to_string(); | ||||||||||||||||||||||||||||||||||||
| super::core::reset_conversation_state(app); | ||||||||||||||||||||||||||||||||||||
| app.clear_input(); | ||||||||||||||||||||||||||||||||||||
| app.session_artifacts.clear(); | ||||||||||||||||||||||||||||||||||||
| app.session_context_references.clear(); | ||||||||||||||||||||||||||||||||||||
| app.tool_evidence.clear(); | ||||||||||||||||||||||||||||||||||||
| app.current_session_id = Some(new_id.clone()); | ||||||||||||||||||||||||||||||||||||
| app.session_title = Some("New Session".to_string()); | ||||||||||||||||||||||||||||||||||||
| app.scroll_to_bottom(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| CommandResult::with_message_and_action( | ||||||||||||||||||||||||||||||||||||
| format!( | ||||||||||||||||||||||||||||||||||||
| "Started new session {} (New Session). Previous sessions remain available via /resume.", | ||||||||||||||||||||||||||||||||||||
| crate::session_manager::truncate_id(&new_id) | ||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+166
to
+174
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
| AppAction::SyncSession { | ||||||||||||||||||||||||||||||||||||
| session_id: Some(new_id), | ||||||||||||||||||||||||||||||||||||
| messages: Vec::new(), | ||||||||||||||||||||||||||||||||||||
| system_prompt: None, | ||||||||||||||||||||||||||||||||||||
| model: app.model.clone(), | ||||||||||||||||||||||||||||||||||||
| workspace: app.workspace.clone(), | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| fn new_session_blockers(app: &App) -> Vec<&'static str> { | ||||||||||||||||||||||||||||||||||||
| let mut blockers = Vec::new(); | ||||||||||||||||||||||||||||||||||||
| if !app.input.trim().is_empty() { | ||||||||||||||||||||||||||||||||||||
| blockers.push("the composer has unsent text"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if !app.queued_messages.is_empty() || app.queued_draft.is_some() { | ||||||||||||||||||||||||||||||||||||
| blockers.push("queued messages are pending"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if app.is_loading || app.runtime_turn_status.as_deref() == Some("in_progress") { | ||||||||||||||||||||||||||||||||||||
| blockers.push("a turn is in progress"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if app.is_compacting { | ||||||||||||||||||||||||||||||||||||
| blockers.push("context compaction is running"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if app.task_panel.iter().any(|task| task.status == "running") { | ||||||||||||||||||||||||||||||||||||
| blockers.push("background tasks are running"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| blockers | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+150
to
+203
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical vs Discardable BlockersAllowing We should split the blockers into:
let critical_blockers = new_session_critical_blockers(app);
if !critical_blockers.is_empty() {
return CommandResult::error(format!(
"Cannot start a new session while {}. Please wait for these operations to complete.",
critical_blockers.join(", ")
));
}
if !force {
let discardable_blockers = new_session_discardable_blockers(app);
if !discardable_blockers.is_empty() {
return CommandResult::error(format!(
"Cannot start a new session while {}. Run `/new --force` to discard pending work and start a fresh session.",
discardable_blockers.join(", ")
));
}
}
let new_id = uuid::Uuid::new_v4().to_string();
super::core::reset_conversation_state(app);
app.clear_input();
app.session_artifacts.clear();
app.session_context_references.clear();
app.tool_evidence.clear();
app.current_session_id = Some(new_id.clone());
app.session_title = Some("New Session".to_string());
app.scroll_to_bottom();
CommandResult::with_message_and_action(
format!(
"Started new session {} (New Session). Previous sessions remain available via /resume.",
crate::session_manager::truncate_id(&new_id)
),
AppAction::SyncSession {
session_id: Some(new_id),
messages: Vec::new(),
system_prompt: None,
model: app.model.clone(),
workspace: app.workspace.clone(),
},
)
}
fn new_session_critical_blockers(app: &App) -> Vec<&'static str> {
let mut blockers = Vec::new();
if app.is_loading || app.runtime_turn_status.as_deref() == Some("in_progress") {
blockers.push("a turn is in progress");
}
if app.is_compacting {
blockers.push("context compaction is running");
}
if app.task_panel.iter().any(|task| task.status == "running") {
blockers.push("background tasks are running");
}
blockers
}
fn new_session_discardable_blockers(app: &App) -> Vec<&'static str> {
let mut blockers = Vec::new();
if !app.input.trim().is_empty() {
blockers.push("the composer has unsent text");
}
if !app.queued_messages.is_empty() || app.queued_draft.is_some() {
blockers.push("queued messages are pending");
}
blockers
} |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /// Load session from file | ||||||||||||||||||||||||||||||||||||
| pub fn load(app: &mut App, path: Option<&str>) -> CommandResult { | ||||||||||||||||||||||||||||||||||||
| let load_path = if let Some(p) = path { | ||||||||||||||||||||||||||||||||||||
|
|
@@ -489,6 +556,110 @@ mod tests { | |||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||
| fn new_session_from_resumed_state_creates_distinct_empty_session() { | ||||||||||||||||||||||||||||||||||||
| let tmpdir = TempDir::new().unwrap(); | ||||||||||||||||||||||||||||||||||||
| let mut app = create_test_app_with_tmpdir(&tmpdir); | ||||||||||||||||||||||||||||||||||||
| app.current_session_id = Some("old-session".to_string()); | ||||||||||||||||||||||||||||||||||||
| app.session_title = Some("Old Session".to_string()); | ||||||||||||||||||||||||||||||||||||
| app.api_messages.push(crate::models::Message { | ||||||||||||||||||||||||||||||||||||
| role: "user".to_string(), | ||||||||||||||||||||||||||||||||||||
| content: vec![crate::models::ContentBlock::Text { | ||||||||||||||||||||||||||||||||||||
| text: "continue this thread".to_string(), | ||||||||||||||||||||||||||||||||||||
| cache_control: None, | ||||||||||||||||||||||||||||||||||||
| }], | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
| app.add_message(HistoryCell::System { | ||||||||||||||||||||||||||||||||||||
| content: "old transcript".to_string(), | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
| app.system_prompt = Some(crate::models::SystemPrompt::Text("old prompt".to_string())); | ||||||||||||||||||||||||||||||||||||
| app.session.total_tokens = 123; | ||||||||||||||||||||||||||||||||||||
| app.session.session_cost = 1.25; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let result = new_session(&mut app, None); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| assert!(!result.is_error, "{:?}", result.message); | ||||||||||||||||||||||||||||||||||||
| let new_id = app.current_session_id.clone().expect("new session id"); | ||||||||||||||||||||||||||||||||||||
| assert_ne!(new_id, "old-session"); | ||||||||||||||||||||||||||||||||||||
| assert_eq!(app.session_title.as_deref(), Some("New Session")); | ||||||||||||||||||||||||||||||||||||
| assert!(app.api_messages.is_empty()); | ||||||||||||||||||||||||||||||||||||
| assert!(app.history.is_empty()); | ||||||||||||||||||||||||||||||||||||
| assert!(app.system_prompt.is_none()); | ||||||||||||||||||||||||||||||||||||
| assert_eq!(app.session.total_tokens, 0); | ||||||||||||||||||||||||||||||||||||
| assert_eq!(app.session.session_cost, 0.0); | ||||||||||||||||||||||||||||||||||||
| assert!( | ||||||||||||||||||||||||||||||||||||
| result | ||||||||||||||||||||||||||||||||||||
| .message | ||||||||||||||||||||||||||||||||||||
| .as_deref() | ||||||||||||||||||||||||||||||||||||
| .unwrap_or_default() | ||||||||||||||||||||||||||||||||||||
| .contains("/resume") | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| match result.action { | ||||||||||||||||||||||||||||||||||||
| Some(AppAction::SyncSession { | ||||||||||||||||||||||||||||||||||||
| session_id, | ||||||||||||||||||||||||||||||||||||
| messages, | ||||||||||||||||||||||||||||||||||||
| system_prompt, | ||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||
| assert_eq!(session_id.as_deref(), Some(new_id.as_str())); | ||||||||||||||||||||||||||||||||||||
| assert!(messages.is_empty()); | ||||||||||||||||||||||||||||||||||||
| assert!(system_prompt.is_none()); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| other => panic!("expected SyncSession action, got {other:?}"), | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||
| fn new_session_blocks_unsent_input_without_force() { | ||||||||||||||||||||||||||||||||||||
| let tmpdir = TempDir::new().unwrap(); | ||||||||||||||||||||||||||||||||||||
| let mut app = create_test_app_with_tmpdir(&tmpdir); | ||||||||||||||||||||||||||||||||||||
| app.current_session_id = Some("old-session".to_string()); | ||||||||||||||||||||||||||||||||||||
| app.input = "draft text".to_string(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let result = new_session(&mut app, None); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| assert!(result.is_error); | ||||||||||||||||||||||||||||||||||||
| assert_eq!(app.current_session_id.as_deref(), Some("old-session")); | ||||||||||||||||||||||||||||||||||||
| assert_eq!(app.input, "draft text"); | ||||||||||||||||||||||||||||||||||||
| assert!(result.action.is_none()); | ||||||||||||||||||||||||||||||||||||
| assert!( | ||||||||||||||||||||||||||||||||||||
| result | ||||||||||||||||||||||||||||||||||||
| .message | ||||||||||||||||||||||||||||||||||||
| .as_deref() | ||||||||||||||||||||||||||||||||||||
| .unwrap_or_default() | ||||||||||||||||||||||||||||||||||||
| .contains("/new --force") | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||
| fn new_session_force_discards_unsent_input() { | ||||||||||||||||||||||||||||||||||||
| let tmpdir = TempDir::new().unwrap(); | ||||||||||||||||||||||||||||||||||||
| let mut app = create_test_app_with_tmpdir(&tmpdir); | ||||||||||||||||||||||||||||||||||||
| app.current_session_id = Some("old-session".to_string()); | ||||||||||||||||||||||||||||||||||||
| app.input = "draft text".to_string(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let result = new_session(&mut app, Some("--force")); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| assert!(!result.is_error, "{:?}", result.message); | ||||||||||||||||||||||||||||||||||||
| assert_ne!(app.current_session_id.as_deref(), Some("old-session")); | ||||||||||||||||||||||||||||||||||||
| assert!(app.input.is_empty()); | ||||||||||||||||||||||||||||||||||||
| assert!(matches!(result.action, Some(AppAction::SyncSession { .. }))); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||
| fn new_session_blocks_in_flight_turn_without_force() { | ||||||||||||||||||||||||||||||||||||
| let tmpdir = TempDir::new().unwrap(); | ||||||||||||||||||||||||||||||||||||
| let mut app = create_test_app_with_tmpdir(&tmpdir); | ||||||||||||||||||||||||||||||||||||
| app.current_session_id = Some("old-session".to_string()); | ||||||||||||||||||||||||||||||||||||
| app.is_loading = true; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let result = new_session(&mut app, None); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| assert!(result.is_error); | ||||||||||||||||||||||||||||||||||||
| assert_eq!(app.current_session_id.as_deref(), Some("old-session")); | ||||||||||||||||||||||||||||||||||||
| assert!(result.action.is_none()); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||
| fn test_save_with_default_path_uses_managed_sessions_dir() { | ||||||||||||||||||||||||||||||||||||
| let tmpdir = TempDir::new().unwrap(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"force"(without the--prefix) as a valid flag, but the help text andusagestring only document--force. A user who accidentally types/new forceexpecting an error will silently discard pending work. Accepting only the documented form avoids surprising behaviour.