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
|
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use ratatui::layout::Direction;
use tokio::sync::{mpsc, Notify};
use tracing::{error, warn};
use crate::events::AppEvent;
use crate::layout::{Node, PaneId, TileLayout};
use crate::pane::PaneState;
use crate::terminal::{TerminalId, TerminalRuntime, TerminalState};
use crate::workspace::Workspace;
use super::{DirectionSnapshot, LayoutSnapshot, SessionSnapshot, TabSnapshot, WorkspaceSnapshot};
/// Restore workspaces from a snapshot. Each pane gets a fresh shell in its saved cwd.
pub fn restore(
snapshot: &SessionSnapshot,
rows: u16,
cols: u16,
scrollback_limit_bytes: usize,
default_shell: &str,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<AtomicBool>,
) -> (
Vec<Workspace>,
HashMap<TerminalId, TerminalState>,
HashMap<TerminalId, TerminalRuntime>,
) {
let mut workspaces = Vec::new();
let mut terminals = HashMap::new();
let mut terminal_runtimes = HashMap::new();
for ws_snap in &snapshot.workspaces {
if let Some((workspace, restored_terminals, restored_runtimes)) = restore_workspace(
ws_snap,
rows,
cols,
scrollback_limit_bytes,
default_shell,
events.clone(),
render_notify.clone(),
render_dirty.clone(),
) {
for terminal in restored_terminals {
terminals.insert(terminal.id.clone(), terminal);
}
terminal_runtimes.extend(restored_runtimes);
workspaces.push(workspace);
}
}
(workspaces, terminals, terminal_runtimes)
}
fn restore_workspace(
snap: &WorkspaceSnapshot,
rows: u16,
cols: u16,
scrollback_limit_bytes: usize,
default_shell: &str,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<AtomicBool>,
) -> Option<(
Workspace,
Vec<TerminalState>,
HashMap<TerminalId, TerminalRuntime>,
)> {
let mut tabs = Vec::new();
let mut terminals = Vec::new();
let mut terminal_runtimes = HashMap::new();
let mut public_pane_numbers = HashMap::new();
let mut next_public_pane_number = 1;
for (idx, tab_snap) in snap.tabs.iter().enumerate() {
let (tab, restored_terminals, restored_runtimes) = restore_tab(
tab_snap,
idx + 1,
rows,
cols,
scrollback_limit_bytes,
default_shell,
events.clone(),
render_notify.clone(),
render_dirty.clone(),
)?;
for pane_id in tab.layout.pane_ids() {
public_pane_numbers.insert(pane_id, next_public_pane_number);
next_public_pane_number += 1;
}
terminals.extend(restored_terminals);
terminal_runtimes.extend(restored_runtimes);
tabs.push(tab);
}
if tabs.is_empty() {
return None;
}
Some((
Workspace {
id: snap
.id
.clone()
.unwrap_or_else(crate::workspace::generate_workspace_id),
custom_name: snap.custom_name.clone(),
identity_cwd: snap.identity_cwd.clone(),
cached_git_branch: None,
cached_git_ahead_behind: None,
public_pane_numbers,
next_public_pane_number,
active_tab: snap.active_tab.min(tabs.len().saturating_sub(1)),
tabs,
#[cfg(test)]
test_runtimes: HashMap::new(),
},
terminals,
terminal_runtimes,
))
}
fn restore_tab(
snap: &TabSnapshot,
number: usize,
rows: u16,
cols: u16,
scrollback_limit_bytes: usize,
default_shell: &str,
events: mpsc::Sender<AppEvent>,
render_notify: Arc<Notify>,
render_dirty: Arc<AtomicBool>,
) -> Option<(
crate::workspace::Tab,
Vec<TerminalState>,
HashMap<TerminalId, TerminalRuntime>,
)> {
let (node, id_map) = restore_node_remapped(&snap.layout);
let reverse_id_map: HashMap<PaneId, u32> = id_map
.iter()
.map(|(&old_id, &new_id)| (new_id, old_id))
.collect();
let pane_ids = collect_pane_ids(&node);
let mut panes = HashMap::new();
let mut terminals = Vec::new();
let mut terminal_runtimes = HashMap::new();
for id in &pane_ids {
let saved_cwd = reverse_id_map
.get(id)
.and_then(|old_id| snap.panes.get(old_id))
.map(|p| p.cwd.clone())
.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| "/".into()));
let cwd = if saved_cwd.exists() {
saved_cwd
} else {
warn!(
cwd = %saved_cwd.display(),
"saved pane cwd does not exist, falling back to HOME"
);
let home = std::env::var("HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| PathBuf::from("/"));
if home.exists() {
home
} else {
PathBuf::from("/")
}
};
let saved_label = reverse_id_map
.get(id)
.and_then(|old_id| snap.panes.get(old_id))
.and_then(|p| p.label.clone());
let saved_agent_name = reverse_id_map
.get(id)
.and_then(|old_id| snap.panes.get(old_id))
.and_then(|p| p.agent_name.clone());
match TerminalRuntime::spawn(
*id,
rows,
cols,
cwd.clone(),
scrollback_limit_bytes,
crate::terminal_theme::TerminalTheme::default(),
default_shell,
events.clone(),
render_notify.clone(),
render_dirty.clone(),
) {
Ok(runtime) => {
let terminal_id = TerminalId::alloc();
let mut terminal = TerminalState::new(terminal_id.clone(), cwd.clone());
if let Some(label) = saved_label {
terminal.set_manual_label(label);
}
if let Some(agent_name) = saved_agent_name {
terminal.set_agent_name(agent_name);
}
panes.insert(*id, PaneState::new(terminal_id.clone()));
terminal_runtimes.insert(terminal_id, runtime);
terminals.push(terminal);
}
Err(e) => {
error!(
tab = ?snap.custom_name,
pane_id = id.raw(),
err = %e,
"failed to restore pane, skipping"
);
}
}
}
if panes.is_empty() {
warn!(
tab = ?snap.custom_name,
"no panes could be restored for tab, dropping it"
);
return None;
}
let surviving: HashSet<PaneId> = panes.keys().copied().collect();
let Some(node) = prune_restored_node(node, &surviving) else {
warn!(
tab = ?snap.custom_name,
"restored tab lost all panes after pruning missing layout nodes"
);
return None;
};
let pane_ids = collect_pane_ids(&node);
let focus = resolve_restored_pane(snap.focused, &id_map, &surviving, &pane_ids)?;
let root_pane = resolve_restored_pane(snap.root_pane, &id_map, &surviving, &pane_ids)?;
let layout = TileLayout::from_saved(node, focus);
Some((
crate::workspace::Tab {
custom_name: snap.custom_name.clone(),
number,
root_pane,
layout,
panes,
#[cfg(test)]
runtimes: HashMap::new(),
zoomed: snap.zoomed,
events,
render_notify,
render_dirty,
},
terminals,
terminal_runtimes,
))
}
pub(super) fn prune_restored_node(node: Node, surviving: &HashSet<PaneId>) -> Option<Node> {
match node {
Node::Pane(id) => surviving.contains(&id).then_some(Node::Pane(id)),
Node::Split {
direction,
ratio,
first,
second,
} => {
let first = prune_restored_node(*first, surviving);
let second = prune_restored_node(*second, surviving);
match (first, second) {
(Some(first), Some(second)) => Some(Node::Split {
direction,
ratio,
first: Box::new(first),
second: Box::new(second),
}),
(Some(remaining), None) | (None, Some(remaining)) => Some(remaining),
(None, None) => None,
}
}
}
}
pub(super) fn resolve_restored_pane(
saved_old_id: Option<u32>,
id_map: &HashMap<u32, PaneId>,
surviving: &HashSet<PaneId>,
pane_ids: &[PaneId],
) -> Option<PaneId> {
saved_old_id
.and_then(|old_id| id_map.get(&old_id).copied())
.filter(|pane_id| surviving.contains(pane_id))
.or_else(|| pane_ids.first().copied())
}
/// Restore a layout tree, remapping every pane ID to a fresh globally unique one.
/// Returns the new tree and a map of old_raw_id → new PaneId.
pub(super) fn restore_node_remapped(snap: &LayoutSnapshot) -> (Node, HashMap<u32, PaneId>) {
let mut id_map = HashMap::new();
let node = remap_inner(snap, &mut id_map);
(node, id_map)
}
fn remap_inner(snap: &LayoutSnapshot, id_map: &mut HashMap<u32, PaneId>) -> Node {
match snap {
LayoutSnapshot::Pane(old_id) => {
let new_id = PaneId::alloc();
id_map.insert(*old_id, new_id);
Node::Pane(new_id)
}
LayoutSnapshot::Split {
direction,
ratio,
first,
second,
} => {
let first_node = remap_inner(first, id_map);
let second_node = remap_inner(second, id_map);
let dir = match direction {
DirectionSnapshot::Horizontal => Direction::Horizontal,
DirectionSnapshot::Vertical => Direction::Vertical,
};
Node::Split {
direction: dir,
ratio: *ratio,
first: Box::new(first_node),
second: Box::new(second_node),
}
}
}
}
pub(super) fn collect_pane_ids(node: &Node) -> Vec<PaneId> {
let mut ids = Vec::new();
collect_ids_inner(node, &mut ids);
ids
}
fn collect_ids_inner(node: &Node, ids: &mut Vec<PaneId>) {
match node {
Node::Pane(id) => ids.push(*id),
Node::Split { first, second, .. } => {
collect_ids_inner(first, ids);
collect_ids_inner(second, ids);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn capture_and_restore_node_round_trip() {
let node = Node::Split {
direction: Direction::Horizontal,
ratio: 0.5,
first: Box::new(Node::Pane(PaneId::from_raw(0))),
second: Box::new(Node::Split {
direction: Direction::Vertical,
ratio: 0.3,
first: Box::new(Node::Pane(PaneId::from_raw(1))),
second: Box::new(Node::Pane(PaneId::from_raw(2))),
}),
};
let snap = super::super::snapshot::capture_node(&node);
let (restored, id_map) = restore_node_remapped(&snap);
assert_eq!(id_map.len(), 3);
let ids = collect_pane_ids(&restored);
assert_eq!(ids.len(), 3);
let unique: std::collections::HashSet<u32> = ids.iter().map(|id| id.raw()).collect();
assert_eq!(unique.len(), 3);
}
#[test]
fn prune_restored_node_collapses_missing_branch() {
let keep = PaneId::from_raw(11);
let missing = PaneId::from_raw(12);
let node = Node::Split {
direction: Direction::Horizontal,
ratio: 0.5,
first: Box::new(Node::Pane(keep)),
second: Box::new(Node::Pane(missing)),
};
let surviving = std::collections::HashSet::from([keep]);
let pruned = prune_restored_node(node, &surviving).expect("remaining pane should survive");
assert!(matches!(pruned, Node::Pane(id) if id == keep));
}
#[test]
fn resolve_restored_pane_prefers_surviving_saved_id_and_falls_back_to_first_remaining() {
let first = PaneId::from_raw(21);
let second = PaneId::from_raw(22);
let id_map = HashMap::from([(0_u32, first), (1_u32, second)]);
let surviving = std::collections::HashSet::from([first]);
let pane_ids = vec![first];
assert_eq!(
resolve_restored_pane(Some(0), &id_map, &surviving, &pane_ids),
Some(first)
);
assert_eq!(
resolve_restored_pane(Some(1), &id_map, &surviving, &pane_ids),
Some(first)
);
}
}
|