Repository navigation
fix(restart): refuse to clobber an armed reboot snapshot #409
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,51 +2,130 @@ use anyhow::Result; | |||||||||||
| use chrono::Utc; | ||||||||||||
| use serde::Deserialize; | ||||||||||||
| use std::io::IsTerminal; | ||||||||||||
| use std::path::PathBuf; | ||||||||||||
| use std::path::{Path, PathBuf}; | ||||||||||||
|
|
||||||||||||
| pub async fn run_restart_save_command(auto_restore: bool, replace_armed: bool) -> Result<()> { | ||||||||||||
| run_restart_save_command_with_writer(auto_restore, replace_armed, &mut std::io::stdout()).await | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| async fn run_restart_save_command_with_writer<W: std::io::Write>( | ||||||||||||
| auto_restore: bool, | ||||||||||||
| replace_armed: bool, | ||||||||||||
| output: &mut W, | ||||||||||||
| ) -> Result<()> { | ||||||||||||
| let path = crate::restart_snapshot::snapshot_path()?; | ||||||||||||
| let existing = existing_snapshot_for_save(&path)?; | ||||||||||||
| if existing | ||||||||||||
| .as_ref() | ||||||||||||
| .is_some_and(|snapshot| snapshot.auto_restore_on_next_start) | ||||||||||||
| && !replace_armed | ||||||||||||
| { | ||||||||||||
| // The restore-side claim that serializes competing restores belongs to #389. | ||||||||||||
| let snapshot = existing.as_ref().expect("armed snapshot present"); | ||||||||||||
| anyhow::bail!( | ||||||||||||
| "Refusing to overwrite armed reboot snapshot at {}: created {}, {} session(s), auto-restore armed. Run `jcode restart restore` to consume it, or use `jcode restart save --replace-armed` to replace it.", | ||||||||||||
| path.display(), | ||||||||||||
| snapshot.created_at, | ||||||||||||
| snapshot.sessions.len() | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| pub async fn run_restart_save_command(auto_restore: bool) -> Result<()> { | ||||||||||||
| let mut snapshot = if let Some(snapshot) = capture_connected_restart_snapshot().await? { | ||||||||||||
| snapshot | ||||||||||||
| } else { | ||||||||||||
| crate::restart_snapshot::save_current_snapshot()? | ||||||||||||
| crate::restart_snapshot::capture_current_snapshot()? | ||||||||||||
| }; | ||||||||||||
| snapshot.auto_restore_on_next_start = auto_restore; | ||||||||||||
| snapshot.auto_restore_on_next_start = auto_restore && !snapshot.sessions.is_empty(); | ||||||||||||
|
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. WARNING:
@Kilo fix this |
||||||||||||
| crate::restart_snapshot::write_snapshot(&snapshot)?; | ||||||||||||
| let path = crate::restart_snapshot::snapshot_path()?; | ||||||||||||
|
|
||||||||||||
| if snapshot.sessions.is_empty() { | ||||||||||||
| println!("Saved empty reboot snapshot to {}", path.display()); | ||||||||||||
| writeln!(output, "Saved empty reboot snapshot to {}", path.display())?; | ||||||||||||
| if auto_restore { | ||||||||||||
| println!("Automatic restore is armed for the next plain `jcode` launch."); | ||||||||||||
| writeln!( | ||||||||||||
| output, | ||||||||||||
| "Automatic restore was not armed because no sessions were captured." | ||||||||||||
| )?; | ||||||||||||
| } | ||||||||||||
| println!("\nNo active jcode windows were detected."); | ||||||||||||
| writeln!(output, "\nNo active jcode windows were detected.")?; | ||||||||||||
| return Ok(()); | ||||||||||||
|
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. WARNING: The replaced-snapshot report is skipped on the empty-capture early return. When @Kilo fix this |
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| println!( | ||||||||||||
| "Saved reboot snapshot with {} session(s) to {}\n", | ||||||||||||
| writeln!( | ||||||||||||
| output, | ||||||||||||
| "Saved reboot snapshot with {} session(s) to {}{}\n", | ||||||||||||
| snapshot.sessions.len(), | ||||||||||||
| path.display() | ||||||||||||
| ); | ||||||||||||
| path.display(), | ||||||||||||
| if let Some(existing) = &existing { | ||||||||||||
| format!( | ||||||||||||
| " (replaced unarmed snapshot created {} with {} session(s))", | ||||||||||||
|
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. WARNING: Misleading message: the replaced snapshot is always described as "unarmed". This branch is reached when an unarmed snapshot was replaced without
Suggested change
@Kilo fix this |
||||||||||||
| existing.created_at, | ||||||||||||
| existing.sessions.len() | ||||||||||||
| ) | ||||||||||||
| } else { | ||||||||||||
| String::new() | ||||||||||||
| } | ||||||||||||
| )?; | ||||||||||||
| for session in &snapshot.sessions { | ||||||||||||
| let suffix = if session.is_selfdev { | ||||||||||||
| " [self-dev]" | ||||||||||||
| } else { | ||||||||||||
| "" | ||||||||||||
| }; | ||||||||||||
| println!( | ||||||||||||
| writeln!( | ||||||||||||
| output, | ||||||||||||
| "- {} ({}){}", | ||||||||||||
| session.display_name, session.session_id, suffix | ||||||||||||
| ); | ||||||||||||
| )?; | ||||||||||||
| } | ||||||||||||
| if auto_restore { | ||||||||||||
| println!("\nAutomatic restore is armed for the next plain `jcode` launch."); | ||||||||||||
| writeln!( | ||||||||||||
| output, | ||||||||||||
| "\nAutomatic restore is armed for the next plain `jcode` launch." | ||||||||||||
| )?; | ||||||||||||
| } | ||||||||||||
| println!("\nAfter reboot, restore them with:\n jcode restart restore"); | ||||||||||||
| writeln!( | ||||||||||||
| output, | ||||||||||||
| "\nAfter reboot, restore them with:\n jcode restart restore" | ||||||||||||
| )?; | ||||||||||||
|
|
||||||||||||
| Ok(()) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn existing_snapshot_for_save( | ||||||||||||
| path: &Path, | ||||||||||||
| ) -> Result<Option<crate::restart_snapshot::RestartSnapshot>> { | ||||||||||||
| match crate::restart_snapshot::load_snapshot() { | ||||||||||||
| Ok(snapshot) => Ok(Some(snapshot)), | ||||||||||||
| Err(error) => { | ||||||||||||
| let missing = | ||||||||||||
| path.try_exists().map_err(|error| { | ||||||||||||
| anyhow::anyhow!( | ||||||||||||
| "Failed to inspect reboot snapshot at {}: {}", | ||||||||||||
| path.display(), | ||||||||||||
| error | ||||||||||||
| ) | ||||||||||||
| })? || matches!(nested_io_error(&error), Some(std::io::ErrorKind::NotFound)); | ||||||||||||
|
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. WARNING: Corrupt/unreadable existing snapshot is silently treated as "missing".
@Kilo fix this |
||||||||||||
| if missing { | ||||||||||||
| Ok(None) | ||||||||||||
| } else { | ||||||||||||
| Err(anyhow::anyhow!( | ||||||||||||
| "Failed to read existing reboot snapshot at {}: {}", | ||||||||||||
| path.display(), | ||||||||||||
| error | ||||||||||||
| )) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn nested_io_error(error: &anyhow::Error) -> Option<std::io::ErrorKind> { | ||||||||||||
| error.chain().find_map(|cause| { | ||||||||||||
| cause | ||||||||||||
| .downcast_ref::<std::io::Error>() | ||||||||||||
| .map(|error| error.kind()) | ||||||||||||
| }) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| pub fn run_restart_status_command() -> Result<()> { | ||||||||||||
| let path = crate::restart_snapshot::snapshot_path()?; | ||||||||||||
| let snapshot = match crate::restart_snapshot::load_snapshot() { | ||||||||||||
|
|
||||||||||||
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.
SUGGESTION: Redundant
expectafteris_some_andguard.existing.as_ref().expect("armed snapshot present")is unreachable-as-None because the enclosingifalready provedexistingisSomeand armed. It is safe, but the double-handling is easy to misread. Considerif let Some(snapshot) = existing.as_ref().filter(|s| s.auto_restore_on_next_start) { ... }to make the invariant structural.@Kilo fix this
Fix these issues in Kilo Cloud