Repository navigation
Description
Summary
restart save writes the snapshot unconditionally. If a snapshot is already saved and armed
(auto_restore_on_next_start: true), a later restart save silently replaces it, disarms it, and
resets created_at. The user's only record of what to restore after reboot is destroyed by an
unrelated save, and both commands report success.
Reproduce
# 1. a snapshot exists and is armed, holding real sessions
$ cat "$KCODE_HOME/restart-snapshot.json"
{"version":1,"created_at":"2026-01-01T00:00:00Z","auto_restore_on_next_start":true,
"sessions":[{"session_id":"session_SENTINEL_do_not_lose","display_name":"sentinel", ...}]}
# 2. an unrelated save runs (an autostart hook, a second desktop session, an agent, a script)
$ kcode restart save
Saved empty reboot snapshot to .../restart-snapshot.json
No active jcode windows were detected.
# 3. the armed snapshot is gone
$ cat "$KCODE_HOME/restart-snapshot.json"
{"version":1,"created_at":"2026-11-28T02:45:55.646366844Z","auto_restore_on_next_start":false,
"sessions":[]}Measured on master 1d42e78: the sentinel session id disappeared, auto_restore_on_next_start went
true -> false, created_at was replaced, sessions went to []. Exit status 0 and no warning.
This is worst exactly at the reboot boundary it exists to protect: a session saves, then anything that
runs restart save on the next login (before the restore happens) erases the snapshot. The restore
then finds nothing, and both sides reported success.
Cause
src/cli/commands/restart.rs:8
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()?
};
snapshot.auto_restore_on_next_start = auto_restore;
crate::restart_snapshot::write_snapshot(&snapshot)?; // no read of the existing fileThere is no read of the current snapshot, no comparison, and no confirmation. write_snapshot is
last-writer-wins.
Secondary: save --auto-restore with zero windows still arms
$ kcode restart save --auto-restore
Saved empty reboot snapshot to ...
Automatic restore is armed for the next plain `jcode` launch.
No active jcode windows were detected.
The file is written with auto_restore_on_next_start: true and sessions: []. A later plain start says
Restoring 0 jcode window(s)... then Saved reboot snapshot is empty. Nothing to restore. and
consumes it. Harmless end to end, but the "armed" message is untrue and the empty armed snapshot
suppresses a genuine restore the same file could have held.
Suggested fix
- In
run_restart_save_command, load the existing snapshot first. If
auto_restore_on_next_startis true andsessionsis non-empty, either refuse with a message
naming the existing snapshot'screated_at, or require an explicit--force. - Preserve the armed flag rather than overwriting it from the CLI default, so a plain
restart save
cannot silently disarm a pending restore. - Do not write an armed snapshot when
sessions.is_empty(); print the empty result and exit
non-zero, or leave any existing snapshot untouched.
Related
#389 covers the mirror defect: two concurrent restart restore runs both spawn every saved session
(check-then-act without a claim). Both are last-writer-wins / check-then-act around the same file.
A single atomic-claim primitive would address both.
Activity