aboutsummaryrefslogtreecommitdiff
path: root/crates/dinggerd/src/restore.rs
blob: 1dca9551ddffc620c732ba60bcf76d760bc7cd1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//! `dingger restore`: rebuild tmux sessions from a snapshot.
//!
//! Only adds what is missing. A session that exists is left alone, except a
//! bare one (one window, one pane, only a shell): that is what a terminal
//! creates on startup (Ghostty's `tmux new-session -As 0`), so the saved
//! session replaces it and its clients move over without detaching.
//!
//! Each session is rebuilt with its windows (index, name, automatic-rename),
//! panes (directory, in order), exact layout, zoom, and active window and
//! pane. Everything is addressed by tmux id, so names with `.` or `:` work.
//!
//! Then, once each new shell shows its prompt: agents with a session
//! reference are resumed (`pi --session <file>`, run), and every other
//! saved command line is typed but not run, for the user to press Enter.

use std::time::{Duration, Instant};

use crate::snapshot::{self, shell_quote, PaneSnap, SessionSnap, Snapshot, WindowSnap};
use crate::tmux;

thread_local! {
    static REPORT: std::cell::RefCell<Vec<String>> = const { std::cell::RefCell::new(Vec::new()) };
}

/// Print a report line, and keep it for `--report`.
fn say(line: String) {
    println!("{line}");
    REPORT.with(|report| report.borrow_mut().push(line));
}

/// What a restore did, for the summary notification.
#[derive(Debug, Default)]
pub struct Summary {
    pub sessions: usize,
    pub failed: usize,
    pub agents: usize,
    pub resumed: usize,
    pub interrupted: usize,
    pub typed: usize,
}

/// What restore types into a rebuilt pane.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Typed {
    /// Resume the agent's session: typed and run.
    Resume(String),
    /// The saved command line: typed, not run.
    Retype(String),
}

/// The command that resumes an agent's session.
fn resume_command(agent: &str, session: &str) -> Option<String> {
    match agent {
        "pi" => Some(format!("pi --session {}", shell_quote(session))),
        _ => None,
    }
}

pub fn typed_for(pane: &PaneSnap) -> Option<Typed> {
    let typeable = |text: &String| !text.trim().is_empty() && !text.chars().any(char::is_control);
    if let Some(agent) = &pane.agent {
        let resume = agent.session.as_deref().and_then(|session| resume_command(&agent.agent, session));
        if let Some(command) = resume.filter(typeable) {
            return Some(Typed::Resume(command));
        }
    }
    pane.command.clone().filter(typeable).map(Typed::Retype)
}

/// A rebuilt pane and what it held.
struct Rebuilt {
    pane_id: String,
    label: String,
    pane: PaneSnap,
}

/// What restore does with a saved session.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Plan {
    Create,
    /// Replace an existing bare session of the same name.
    Replace,
    /// It exists and has been used: leave it alone.
    Keep,
}

/// A session on the running server.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Existing {
    pub name: String,
    /// One window with one pane that runs only a shell.
    pub bare: bool,
}

pub fn plan(snapshot: &Snapshot, existing: &[Existing]) -> Vec<(String, Plan)> {
    snapshot
        .sessions
        .iter()
        .map(|session| {
            let plan = match existing.iter().find(|e| e.name == session.name) {
                None => Plan::Create,
                // A saved single shell has nothing to add over a bare one.
                Some(e) if e.bare && !saved_bare(session) => Plan::Replace,
                Some(_) => Plan::Keep,
            };
            (session.name.clone(), plan)
        })
        .collect()
}

/// Sessions restore would create or replace.
pub fn missing(snapshot: &Snapshot, existing: &[Existing]) -> Vec<String> {
    plan(snapshot, existing)
        .into_iter()
        .filter(|(_, plan)| *plan != Plan::Keep)
        .map(|(name, _)| name)
        .collect()
}

fn saved_bare(session: &SessionSnap) -> bool {
    matches!(&session.windows[..], [window] if matches!(&window.panes[..], [pane] if pane.agent.is_none()))
}

use snapshot::is_shell;

/// The running server's sessions; empty when tmux is not running.
pub fn existing_sessions() -> Vec<Existing> {
    let Ok(out) = tmux::run(&[
        "list-panes",
        "-a",
        "-F",
        "#{session_name}\t#{session_windows}\t#{window_panes}\t#{pane_current_command}",
    ]) else {
        return Vec::new();
    };
    let mut sessions: Vec<Existing> = Vec::new();
    for line in out.lines() {
        let f: Vec<&str> = line.split('\t').collect();
        if f.len() < 4 || sessions.iter().any(|s| s.name == f[0]) {
            continue;
        }
        sessions.push(Existing {
            name: f[0].to_string(),
            bare: f[1] == "1" && f[2] == "1" && is_shell(f[3]),
        });
    }
    sessions
}

fn first_line(out: String) -> String {
    out.lines().next().unwrap_or_default().trim().to_string()
}

/// Build one window's panes, layout, and options. `pane` is its first pane.
fn build_window(
    window_id: &str,
    first_pane: String,
    window: &WindowSnap,
    session: &str,
    rebuilt: &mut Vec<Rebuilt>,
) -> Result<(), String> {
    let width = window.width.max(10).to_string();
    let height = window.height.max(5).to_string();
    // The saved size, so the layout applies exactly; unset afterwards so the
    // window follows its clients again.
    tmux::run(&["resize-window", "-t", window_id, "-x", &width, "-y", &height])?;
    let mut panes = vec![first_pane];
    for pane in window.panes.iter().skip(1) {
        // Split the newest pane: tmux lists a split right after its source,
        // so panes keep the saved order. Rebalance to make room for the next.
        let last = panes.last().cloned().unwrap_or_default();
        let id = first_line(tmux::run(&[
            "split-window", "-d", "-t", &last, "-c", &pane.cwd, "-P", "-F", "#{pane_id}",
        ])?);
        panes.push(id);
        let _ = tmux::run(&["select-layout", "-t", window_id, "tiled"]);
    }
    if panes.len() > 1 {
        if let Err(err) = tmux::run(&["select-layout", "-t", window_id, &window.layout]) {
            tracing::warn!(%err, "saved layout did not apply; keeping a tiled layout");
        }
    }
    tmux::run(&["set-option", "-w", "-u", "-t", window_id, "window-size"])?;
    if window.automatic_rename {
        tmux::run(&["set-option", "-w", "-t", window_id, "automatic-rename", "on"])?;
    }
    if let Some(position) = window.panes.iter().position(|pane| pane.active) {
        if let Some(id) = panes.get(position) {
            tmux::run(&["select-pane", "-t", id])?;
            if window.zoomed && panes.len() > 1 {
                tmux::run(&["resize-pane", "-Z", "-t", id])?;
            }
        }
    }
    for (pane_id, pane) in panes.into_iter().zip(&window.panes) {
        let label = if window.panes.len() > 1 {
            format!("{session}:{}.{}", window.name, pane.index)
        } else {
            format!("{session}:{}", window.name)
        };
        rebuilt.push(Rebuilt { pane_id, label, pane: pane.clone() });
    }
    Ok(())
}

/// Create `session` under `name`; returns its session id.
fn build_session(session: &SessionSnap, name: &str, rebuilt: &mut Vec<Rebuilt>) -> Result<String, String> {
    let Some(first) = session.windows.first() else {
        return Err(format!("{}: no windows saved", session.name));
    };
    let cwd = first.panes.first().map(|pane| pane.cwd.as_str()).unwrap_or("~");
    let created = first_line(tmux::run(&[
        "new-session", "-d", "-s", name, "-n", &first.name, "-c", cwd,
        "-x", &first.width.max(10).to_string(), "-y", &first.height.max(5).to_string(),
        "-P", "-F", "#{session_id}\t#{window_id}\t#{window_index}\t#{pane_id}",
    ])?);
    let ids: Vec<&str> = created.split('\t').collect();
    let [session_id, first_window, first_index, first_pane] = ids[..] else {
        return Err(format!("unexpected new-session output: {created}"));
    };
    let session_id = session_id.to_string();
    if first_index != first.index.to_string() {
        tmux::run(&["move-window", "-s", first_window, "-t", &format!("{session_id}:{}", first.index)])?;
    }
    build_window(first_window, first_pane.to_string(), first, &session.name, rebuilt)?;
    for window in session.windows.iter().skip(1) {
        let cwd = window.panes.first().map(|pane| pane.cwd.as_str()).unwrap_or("~");
        let created = first_line(tmux::run(&[
            "new-window", "-d", "-t", &format!("{session_id}:{}", window.index), "-n", &window.name,
            "-c", cwd, "-P", "-F", "#{window_id}\t#{pane_id}",
        ])?);
        let Some((window_id, pane_id)) = created.split_once('\t') else {
            return Err(format!("unexpected new-window output: {created}"));
        };
        build_window(window_id, pane_id.to_string(), window, &session.name, rebuilt)?;
    }
    if let Some(active) = session.windows.iter().find(|window| window.active) {
        tmux::run(&["select-window", "-t", &format!("{session_id}:{}", active.index)])?;
    }
    Ok(session_id)
}

/// Replace a bare session: build the saved one beside it, move its clients
/// over, then remove it and take its name.
fn replace_session(session: &SessionSnap, rebuilt: &mut Vec<Rebuilt>) -> Result<(), String> {
    let temporary = format!("{}-restoring-{}", session.name, std::process::id());
    let new_id = build_session(session, &temporary, rebuilt)?;
    let clients = tmux::run(&["list-clients", "-F", "#{client_name}\t#{session_name}"]).unwrap_or_default();
    for line in clients.lines() {
        if let Some((client, attached)) = line.split_once('\t') {
            if attached == session.name {
                tmux::run(&["switch-client", "-c", client, "-t", &new_id])?;
            }
        }
    }
    tmux::run(&["kill-session", "-t", &format!("={}", session.name)])?;
    tmux::run(&["rename-session", "-t", &new_id, &session.name]).map(|_| ())
}

/// Wait for each new shell's prompt, then resume agents (spaced out) and
/// type the other command lines without running them; report which agents
/// did not start.
fn type_commands(rebuilt: &[Rebuilt], summary: &mut Summary) {
    let work: Vec<(&Rebuilt, Typed)> =
        rebuilt.iter().filter_map(|r| typed_for(&r.pane).map(|typed| (r, typed))).collect();
    if work.is_empty() {
        return;
    }
    // Text typed before the prompt is drawn is echoed twice; wait for it
    // (the cursor leaves the top-left corner).
    let deadline = Instant::now() + Duration::from_secs(10);
    loop {
        let cursors = tmux::run(&["list-panes", "-a", "-F", "#{pane_id} #{cursor_x} #{cursor_y}"]).unwrap_or_default();
        let waiting = work.iter().any(|(r, _)| {
            cursors.lines().any(|line| line == format!("{} 0 0", r.pane_id))
        });
        if !waiting || Instant::now() > deadline {
            break;
        }
        std::thread::sleep(Duration::from_millis(100));
    }
    std::thread::sleep(Duration::from_millis(200));

    let mut resumed = Vec::new();
    for (r, typed) in &work {
        match typed {
            Typed::Resume(command) => {
                let _ = tmux::run(&["send-keys", "-t", &r.pane_id, "-l", command]);
                let _ = tmux::run(&["send-keys", "-t", &r.pane_id, "Enter"]);
                say(format!("resume   {}  {command}", r.label));
                resumed.push(*r);
                // Agents start one at a time.
                std::thread::sleep(Duration::from_millis(500));
            }
            Typed::Retype(command) => {
                let _ = tmux::run(&["send-keys", "-t", &r.pane_id, "-l", command]);
                say(format!("type     {}  {command}  (press Enter to run)", r.label));
                summary.typed += 1;
            }
        }
    }
    for (r, _) in &work {
        if let Some(agent) = r.pane.agent.as_ref().filter(|agent| agent.state == "working") {
            let name = if agent.name.is_empty() { &agent.agent } else { &agent.name };
            say(format!("interrupted  {}  {name}: it was working, its turn was cut short", r.label));
            summary.interrupted += 1;
        }
    }
    if resumed.is_empty() {
        return;
    }
    // An agent that started has replaced the shell in the foreground.
    let deadline = Instant::now() + Duration::from_secs(30);
    let mut not_running: Vec<&Rebuilt> = resumed.clone();
    while !not_running.is_empty() && Instant::now() < deadline {
        std::thread::sleep(Duration::from_millis(500));
        not_running.retain(|r| {
            tmux::run(&["display-message", "-p", "-t", &r.pane_id, "#{pane_current_command}"])
                .map(|command| is_shell(command.trim()))
                .unwrap_or(true)
        });
    }
    summary.agents = resumed.len();
    summary.resumed = resumed.len() - not_running.len();
    say(format!("resumed {} of {} agents", summary.resumed, summary.agents));
    for r in not_running {
        say(format!("not running  {}: check the pane for the error", r.label));
    }
}

fn windows_and_panes(session: &SessionSnap) -> String {
    let panes: usize = session.windows.iter().map(|window| window.panes.len()).sum();
    format!("{} windows, {panes} panes", session.windows.len())
}

/// The snapshot to restore: `from`, or the restore candidate.
fn source(from: Option<&str>) -> Result<Snapshot, String> {
    match from {
        Some(path) => snapshot::load(std::path::Path::new(path)).ok_or_else(|| format!("cannot read {path}")),
        None => snapshot::candidate()
            .map(|candidate| candidate.snapshot)
            .ok_or_else(|| "nothing to restore: no saved layout from an earlier tmux server".to_string()),
    }
}

pub fn list() -> Result<(), String> {
    match snapshot::candidate() {
        Some(candidate) => {
            let missing = missing(&candidate.snapshot, &existing_sessions());
            println!(
                "restore candidate ({}), saved {}: {}; missing now: {}",
                candidate.status,
                format_time(candidate.snapshot.saved_at),
                candidate.snapshot.summary(),
                if missing.is_empty() { "none".to_string() } else { missing.join(", ") }
            );
        }
        None => println!("no restore candidate"),
    }
    for path in snapshot::history().iter().rev() {
        if let Some(snapshot) = snapshot::load(path) {
            println!("{}  {}  {}", path.display(), format_time(snapshot.saved_at), snapshot.summary());
        }
    }
    Ok(())
}

fn format_time(ms: u64) -> String {
    std::process::Command::new("date")
        .args(["-r", &(ms / 1000).to_string(), "+%Y-%m-%d %H:%M"])
        .output()
        .map(|out| String::from_utf8_lossy(&out.stdout).trim().to_string())
        .unwrap_or_else(|_| ms.to_string())
}

/// Restore missing sessions (all, or only `only`). With `report`, also
/// write the report to `restore.log` and notify a summary.
pub fn run(from: Option<&str>, only: &[String], dry_run: bool, report: bool) -> Result<(), String> {
    let result = restore(from, only, dry_run);
    if report && !dry_run {
        let lines = REPORT.with(|report| report.borrow().join("\n"));
        let _ = std::fs::write(snapshot::state_dir().join("restore.log"), format!("{lines}\n"));
        let (title, body) = match &result {
            Ok(summary) => summary_message(summary),
            Err(err) => ("tmux restore failed".to_string(), err.clone()),
        };
        crate::notify::send_message_blocking(&title, &body);
    }
    let _ = crate::hooks::poke();
    result.map(|_| ())
}

fn plural(n: usize, one: &str, many: &str) -> String {
    format!("{n} {}", if n == 1 { one } else { many })
}

pub fn summary_message(summary: &Summary) -> (String, String) {
    let title = format!("Restored {}", plural(summary.sessions, "tmux session", "tmux sessions"));
    let mut parts = Vec::new();
    if summary.agents > 0 {
        parts.push(format!("resumed {} of {}", summary.resumed, plural(summary.agents, "agent", "agents")));
    }
    if summary.interrupted > 0 {
        parts.push(format!("{} mid-turn", plural(summary.interrupted, "was", "were")));
    }
    if summary.typed > 0 {
        parts.push(format!("{} typed, not run", plural(summary.typed, "command", "commands")));
    }
    if summary.failed > 0 {
        parts.push(format!("{} failed", plural(summary.failed, "session", "sessions")));
    }
    if parts.is_empty() {
        parts.push("nothing else to resume".into());
    }
    (title, parts.join(" · "))
}

/// Mark the restore candidate dismissed: no more offers for it.
pub fn dismiss() -> Result<(), String> {
    snapshot::set_candidate_status("dismissed")?;
    let _ = crate::hooks::poke();
    println!("dismissed; `dingger restore` still restores it");
    Ok(())
}

fn restore(from: Option<&str>, only: &[String], dry_run: bool) -> Result<Summary, String> {
    let snapshot = source(from)?;
    let mut summary = Summary::default();
    let existing = existing_sessions();
    let mut failures = 0;
    let mut rebuilt = Vec::new();
    for (name, plan) in plan(&snapshot, &existing) {
        if !only.is_empty() && !only.contains(&name) {
            continue;
        }
        let Some(session) = snapshot.sessions.iter().find(|s| s.name == name) else {
            continue;
        };
        let what = windows_and_panes(session);
        let verb = match plan {
            Plan::Keep => {
                say(format!("keep     {name}: it exists"));
                continue;
            }
            Plan::Create => "create",
            Plan::Replace => "replace",
        };
        if dry_run {
            say(format!("{verb:<8} {name}: {what}"));
            for window in &session.windows {
                for pane in &window.panes {
                    match typed_for(pane) {
                        Some(Typed::Resume(command)) => say(format!("  resume  {}  {command}", window.name)),
                        Some(Typed::Retype(command)) => say(format!("  type    {}  {command}", window.name)),
                        None => {}
                    }
                }
            }
            continue;
        }
        let result = match plan {
            Plan::Replace => replace_session(session, &mut rebuilt),
            _ => build_session(session, &name, &mut rebuilt).map(|_| ()),
        };
        match result {
            Ok(()) => {
                summary.sessions += 1;
                say(format!("{verb:<8} {name}: {what}"));
            }
            Err(err) => {
                failures += 1;
                say(format!("FAILED   {name}: {err}"));
            }
        }
    }
    if !rebuilt.is_empty() {
        type_commands(&rebuilt, &mut summary);
    }
    summary.failed = failures;
    if !dry_run && from.is_none() && missing(&snapshot, &existing_sessions()).is_empty() {
        let _ = snapshot::set_candidate_status("restored");
    }
    if failures == 0 { Ok(summary) } else { Err(format!("{failures} sessions failed")) }
}

/// One session restore would bring back, as offered to the user.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct OfferedSession {
    pub name: String,
    pub windows: usize,
    pub panes: usize,
    pub agents: usize,
}

/// The restore offer the menu bar app shows (`offer.json`).
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct Offer {
    pub saved_at: u64,
    pub sessions: Vec<OfferedSession>,
    pub agents: usize,
    /// Agents that were mid-turn when tmux stopped.
    pub interrupted: usize,
}

pub fn offer(snapshot: &Snapshot, existing: &[Existing]) -> Option<Offer> {
    let missing = missing(snapshot, existing);
    let sessions: Vec<&SessionSnap> =
        snapshot.sessions.iter().filter(|session| missing.contains(&session.name)).collect();
    if sessions.is_empty() {
        return None;
    }
    let panes = |session: &&SessionSnap| session.windows.iter().flat_map(|w| &w.panes).cloned().collect::<Vec<_>>();
    let agents = sessions.iter().flat_map(panes).filter(|pane| pane.agent.is_some()).count();
    let interrupted = sessions
        .iter()
        .flat_map(panes)
        .filter(|pane| pane.agent.as_ref().is_some_and(|agent| agent.state == "working"))
        .count();
    Some(Offer {
        saved_at: snapshot.saved_at,
        sessions: sessions
            .iter()
            .map(|session| OfferedSession {
                name: session.name.clone(),
                windows: session.windows.len(),
                panes: session.windows.iter().map(|w| w.panes.len()).sum(),
                agents: panes(session).iter().filter(|pane| pane.agent.is_some()).count(),
            })
            .collect(),
        agents,
        interrupted,
    })
}

/// Keeps `offer.json` and the `@dingger_restore` option current; called by
/// the watcher every loop.
#[derive(Default)]
pub struct OfferPublisher {
    candidate: Option<(std::time::SystemTime, Option<snapshot::Candidate>)>,
    published: Option<Option<Offer>>,
}

impl OfferPublisher {
    pub fn tick(&mut self) {
        let path = snapshot::state_dir().join("restore.json");
        let modified = std::fs::metadata(&path).and_then(|m| m.modified()).ok();
        let stale = match (&self.candidate, modified) {
            (Some((at, _)), Some(modified)) => *at != modified,
            (None, _) => true,
            (Some(_), None) => true,
        };
        if stale {
            self.candidate = Some((modified.unwrap_or(std::time::UNIX_EPOCH), snapshot::candidate()));
        }
        let offered = self
            .candidate
            .as_ref()
            .and_then(|(_, candidate)| candidate.as_ref())
            .filter(|candidate| candidate.status == "offered");
        let offer = offered.and_then(|candidate| offer(&candidate.snapshot, &existing_sessions()));
        if self.published.as_ref() == Some(&offer) {
            return;
        }
        let file = snapshot::state_dir().join("offer.json");
        match &offer {
            Some(offer) => {
                if let Ok(json) = serde_json::to_string_pretty(offer) {
                    let tmp = file.with_extension("tmp");
                    if std::fs::write(&tmp, json).is_ok() {
                        let _ = std::fs::rename(&tmp, &file);
                    }
                }
                let set = tmux::run(&["set-option", "-g", "@dingger_restore", &offer.sessions.len().to_string()]);
                tracing::info!(sessions = offer.sessions.len(), agents = offer.agents, ok = set.is_ok(), "restore offered");
            }
            None => {
                let _ = std::fs::remove_file(&file);
                let _ = tmux::run(&["set-option", "-g", "-u", "@dingger_restore"]);
                if matches!(self.published, Some(Some(_))) {
                    tracing::info!("restore offer withdrawn");
                }
            }
        }
        self.published = Some(offer);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::snapshot::{AgentSnap, PaneSnap, ServerId, SessionSnap};

    fn window(panes: usize) -> WindowSnap {
        WindowSnap {
            index: 0,
            name: "w".into(),
            layout: String::new(),
            width: 80,
            height: 24,
            active: true,
            zoomed: false,
            automatic_rename: false,
            panes: (0..panes)
                .map(|i| PaneSnap {
                    id: String::new(),
                    index: i as u32,
                    cwd: "/tmp".into(),
                    active: i == 0,
                    agent: None,
                    command: None,
                })
                .collect(),
        }
    }

    fn snapshot(names: &[&str]) -> Snapshot {
        Snapshot {
            version: 1,
            saved_at: 1,
            server: ServerId { pid: 1, start_time: 1 },
            sessions: names
                .iter()
                .map(|name| SessionSnap { name: name.to_string(), windows: vec![window(2)] })
                .collect(),
        }
    }

    #[test]
    fn creates_missing_replaces_bare_and_keeps_used_sessions() {
        let existing = [
            Existing { name: "0".into(), bare: true },
            Existing { name: "keep".into(), bare: false },
            Existing { name: "new".into(), bare: true },
        ];
        assert_eq!(
            plan(&snapshot(&["0", "keep", "work"]), &existing),
            vec![("0".into(), Plan::Replace), ("keep".into(), Plan::Keep), ("work".into(), Plan::Create)]
        );
        assert_eq!(missing(&snapshot(&["0", "keep", "work"]), &existing), vec!["0", "work"]);
        assert!(missing(&snapshot(&["keep"]), &existing).is_empty());
    }

    #[test]
    fn a_saved_single_shell_does_not_replace_a_bare_session() {
        let mut saved = snapshot(&["0"]);
        saved.sessions[0].windows = vec![window(1)];
        let existing = [Existing { name: "0".into(), bare: true }];
        assert_eq!(plan(&saved, &existing), vec![("0".into(), Plan::Keep)]);
        assert!(missing(&saved, &existing).is_empty(), "or the offer would never go away");
    }

    #[test]
    fn resumes_agents_by_session_and_retypes_the_rest() {
        let mut pane = window(1).panes.remove(0);
        assert_eq!(typed_for(&pane), None, "a shell prompt types nothing");
        pane.command = Some("nvim 'my file.sh'".into());
        assert_eq!(typed_for(&pane), Some(Typed::Retype("nvim 'my file.sh'".into())));
        pane.command = Some("echo one\necho two".into());
        assert_eq!(typed_for(&pane), None, "a newline would run it");
        pane.agent = Some(AgentSnap {
            agent: "pi".into(),
            name: "x".into(),
            state: "idle".into(),
            session: Some("/s/a b.jsonl".into()),
        });
        assert_eq!(typed_for(&pane), Some(Typed::Resume("pi --session '/s/a b.jsonl'".into())));
        if let Some(agent) = pane.agent.as_mut() {
            agent.session = None;
        }
        pane.command = Some("pi --model x".into());
        assert_eq!(typed_for(&pane), Some(Typed::Retype("pi --model x".into())), "no session: retyped");
    }

    #[test]
    fn offers_only_missing_sessions_with_their_agents() {
        let mut saved = snapshot(&["0", "work"]);
        saved.sessions[1].windows[0].panes[0].agent = Some(AgentSnap {
            agent: "pi".into(),
            name: "x".into(),
            state: "working".into(),
            session: None,
        });
        let offer = offer(&saved, &[Existing { name: "0".into(), bare: false }]).expect("work is missing");
        assert_eq!(offer.sessions.iter().map(|s| s.name.as_str()).collect::<Vec<_>>(), ["work"]);
        assert_eq!((offer.agents, offer.interrupted, offer.sessions[0].panes), (1, 1, 2));
        assert_eq!(super::offer(&saved, &[
            Existing { name: "0".into(), bare: false },
            Existing { name: "work".into(), bare: false },
        ]), None, "nothing missing, no offer");
    }

    #[test]
    fn summarizes_a_restore() {
        let summary = Summary { sessions: 9, agents: 22, resumed: 21, interrupted: 1, typed: 3, failed: 0 };
        assert_eq!(
            summary_message(&summary),
            ("Restored 9 tmux sessions".into(), "resumed 21 of 22 agents · 1 was mid-turn · 3 commands typed, not run".into())
        );
    }

    #[test]
    fn shells_are_recognized() {
        assert!(is_shell("zsh") && is_shell("-zsh") && is_shell("bash") && is_shell("fish"));
        assert!(!is_shell("nvim") && !is_shell("node") && !is_shell("tmux"));
    }
}