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
//! `croft attach` and `croft ls`, local session persistence: run croft under the built-in session host
//! ([`crate::session_host`], the multiplayer mux) so a session launched on
//! this machine (its terminals, LSP, DAP, editor state) survives closing the
//! terminal window and can be reattached later, by one or several clients.
//! This mirrors the remote persistence in [`crate::remote`] (same socket
//! keying) but for the box croft already runs on.
//!
//! - `croft attach [path]` attaches to (or creates) the persistent session for
//! a workspace. Several attachers share the session (see
//! docs/MULTIPLAYER.md).
//! - Detach by closing the window: the client dies, the host keeps croft
//! alive.
//! - `croft ls` lists the live sessions and prunes dead sockets.
//! - Sessions created by an older croft under dtach keep reattaching through
//! dtach until they end; new sessions never need dtach.
//!
//! The session files these commands read and write (socket paths, sidecars,
//! the liveness probe) live in [`crate::session`], below the session host.
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use crate::session::{
collab_socket_path, is_alive, is_session_socket, meta_path, mux_socket_path, now_unix,
read_meta, sessions_dir, socket_path, write_meta_preserving_created,
};
/// The dtach argv that runs `inner` (the real croft TUI) under an
/// attach-or-create session on `socket`. Same flags as the remote wrapper:
/// `-A` attach-or-create, `-E`/`-z` keep dtach's hands off croft's Ctrl
/// chords, `-r winch` repaints on reattach. Pure so it can be unit-tested.
fn dtach_attach_argv(socket: &str, inner: &[String]) -> Vec<String> {
let mut argv = vec![
String::from("-A"),
socket.to_string(),
String::from("-E"),
String::from("-z"),
String::from("-r"),
String::from("winch"),
];
argv.extend(inner.iter().cloned());
argv
}
fn dtach_on_path() -> bool {
// dtach with no args prints usage and exits non-zero, but it still *ran* —
// Ok means the binary is on PATH; Err (ENOENT) means it isn't.
std::process::Command::new("dtach").output().is_ok()
}
fn humanize_age(secs: u64) -> String {
if secs < 60 {
format!("{secs}s")
} else if secs < 3_600 {
format!("{}m", secs / 60)
} else if secs < 86_400 {
format!("{}h", secs / 3_600)
} else {
format!("{}d", secs / 86_400)
}
}
#[cfg(unix)]
fn exec_dtach(socket: &Path, inner: &[String]) -> Result<()> {
use std::os::unix::process::CommandExt;
let argv = dtach_attach_argv(&socket.to_string_lossy(), inner);
// exec replaces this process with dtach, which forks the persistent server
// (running the inner croft) and attaches us as its client. Only returns on
// failure to exec. CROFT_SESSION_PERSISTENT switches on the WINCH mode-
// reassert so the mouse survives a reattach, exactly as on the remote path.
let err = std::process::Command::new("dtach")
.args(&argv)
.env("CROFT_SESSION_PERSISTENT", "1")
.exec();
Err(err).context("exec dtach")
}
#[cfg(not(unix))]
fn exec_dtach(_socket: &Path, _inner: &[String]) -> Result<()> {
anyhow::bail!("session persistence requires a Unix host")
}
/// `croft attach [path]`: attach to (or create) the persistent session for a
/// workspace via the built-in session host (multiplayer mux; see
/// docs/MULTIPLAYER.md), which needs no external binary. A workspace with a
/// live legacy dtach session keeps reattaching through dtach so sessions
/// started under an older croft are never orphaned.
///
/// With `solo`, open an independent viewport instead: this process runs its
/// own croft (no shared PTY) and replicates shared-file edits with the other
/// participants over the workspace's collab relay (Phase D).
pub fn attach(path: Option<PathBuf>, solo: bool) -> Result<()> {
let workspace = match path {
Some(p) => p,
None => std::env::current_dir().context("resolving workspace path")?,
}
.canonicalize()
.context("resolving workspace path")?;
if !workspace.is_dir() {
anyhow::bail!("{} is not a directory", workspace.display());
}
if solo {
let collab = collab_socket_path(&workspace);
crate::collab::ensure_relay(&collab)?;
// The app reads these through CollabChannel::env_config, exactly as
// the remote launch tail exports them over SSH. Startup here is
// still single-threaded; the app's threads spawn inside run().
unsafe {
std::env::set_var("CROFT_COLLAB_SOCKET", &collab);
std::env::set_var("CROFT_COLLAB_ROLE", "guest");
}
return crate::app::run(workspace, None, None, None, false, Vec::new());
}
let croft = std::env::current_exe().context("resolving croft binary path")?;
let inner = vec![
croft.to_string_lossy().into_owned(),
workspace.to_string_lossy().into_owned(),
];
let dir = sessions_dir();
std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?;
let legacy = socket_path(&workspace);
if is_alive(&legacy) && dtach_on_path() {
// Reattaching keeps the original creation time, so `ls` uptime
// reflects when the session started, not this reattach.
write_meta_preserving_created(&legacy, &workspace)?;
return exec_dtach(&legacy, &inner);
}
let mux = mux_socket_path(&workspace);
let code = crate::session_host::attach_or_create(&mux, Some(&workspace), &inner)?;
if code != 0 {
std::process::exit(code);
}
Ok(())
}
/// One live session in `croft ls`.
#[derive(Debug)]
struct SessionRow {
id: String,
workspace: String,
uptime: String,
/// The host survived a binary update (#238).
stale: bool,
/// Where `croft web` serves it, if it does (#343).
web: Option<String>,
}
/// The live persistent sessions under `dir`, pruning any dead session
/// sockets.
fn session_rows(dir: &Path) -> Vec<SessionRow> {
let mut rows: Vec<SessionRow> = Vec::new();
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let socket = entry.path();
// The folder also holds other listeners: the collab relay and
// agent hook socket share the hash keying, and pane hosts live
// here too. None is a session: listing them printed phantom
// `(unknown)` rows whose ids collided with the real session's,
// and probing them cost each a connect-and-drop. Their
// lifecycles belong to their own bind paths, not to `ls` pruning.
if !socket
.file_name()
.and_then(|n| n.to_str())
.is_some_and(is_session_socket)
{
continue;
}
if !is_alive(&socket) {
let _ = std::fs::remove_file(&socket);
let _ = std::fs::remove_file(meta_path(&socket));
let _ = std::fs::remove_file(crate::web::record_path(&socket));
continue;
}
let meta = read_meta(&socket);
let stem = socket
.file_stem()
.unwrap_or_default()
.to_string_lossy()
.to_string();
let id = stem.chars().take(8).collect::<String>();
let workspace = meta
.as_ref()
.map(|m| m.workspace.display().to_string())
.unwrap_or_else(|| String::from("(unknown)"));
let uptime = meta
.as_ref()
.map(|m| humanize_age(now_unix().saturating_sub(m.created_unix)))
.unwrap_or_else(|| String::from("?"));
// #238: the host wrote its stale-image marker — it survived a
// binary update and keeps running the old code until restarted.
let stale = crate::session_host::stale_marker_path(&socket).exists();
let web = crate::web::live_record(&crate::web::record_path(&socket)).map(|r| r.url);
rows.push(SessionRow {
id,
workspace,
uptime,
stale,
web,
});
}
}
rows
}
/// `croft ls`: print the live persistent sessions, pruning any dead sockets.
pub fn list() -> Result<()> {
let rows = session_rows(&sessions_dir());
if rows.is_empty() {
println!("No persistent croft sessions. Start one with `croft attach`.");
return Ok(());
}
println!("{:<10} {:<44} UPTIME", "SESSION", "WORKSPACE");
for row in rows {
let hint = if row.stale {
" (host pre-dates a binary update; reattach to refresh)"
} else {
""
};
let web = row
.web
.map(|url| format!(" web: {url}"))
.unwrap_or_default();
println!(
"{:<10} {:<44} {}{hint}{web}",
row.id, row.workspace, row.uptime
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::session::*;
/// #343: `croft ls` says which sessions `croft web` exposes, and where.
#[test]
fn ls_shows_where_a_session_is_served_on_the_web() {
let dir = tempfile::tempdir().unwrap();
let mux = dir.path().join("0123456789abcdef.mux.sock");
let _mux_listener = bind_socket_0600(&mux).unwrap();
write_meta(
&mux,
&SessionMeta {
workspace: PathBuf::from("/tmp/ws"),
created_unix: now_unix(),
},
)
.unwrap();
assert_eq!(session_rows(dir.path())[0].web, None);
std::fs::write(
crate::web::record_path(&mux),
format!(
r#"{{"pid":{},"url":"wss://127.0.0.1:7681/"}}"#,
std::process::id()
),
)
.unwrap();
let rows = session_rows(dir.path());
assert_eq!(rows.len(), 1, "the record is not a session: {rows:?}");
assert_eq!(rows[0].web.as_deref(), Some("wss://127.0.0.1:7681/"));
}
/// A workspace's collab relay socket (`<hash>.collab.sock`) shares the
/// sessions directory and hash keying with the real session socket, and
/// is a live listener whenever `croft pair` seated a navigator. It is not
/// a session: `croft ls` used to print it as a phantom `(unknown)` row
/// whose 8-char id collided with the real session's.
#[test]
fn ls_does_not_report_the_collab_relay_as_a_session() {
let dir = tempfile::tempdir().unwrap();
let hash = "deadbeefcafef00d";
// A live mux session with its workspace sidecar.
let mux = dir.path().join(format!("{hash}.mux.sock"));
let _mux_listener = bind_socket_0600(&mux).unwrap();
write_meta(
&mux,
&SessionMeta {
workspace: PathBuf::from("/tmp/ws"),
created_unix: now_unix(),
},
)
.unwrap();
// The same workspace's live collab relay.
let collab = dir.path().join(format!("{hash}.collab.sock"));
let _collab_listener = bind_socket_0600(&collab).unwrap();
let rows = session_rows(dir.path());
assert_eq!(
rows.len(),
1,
"only the session is a session; the relay must not be listed: {rows:?}"
);
assert_eq!(rows[0].workspace, "/tmp/ws");
assert!(
collab.exists(),
"ls must not prune the relay socket either — its lifecycle belongs to the relay"
);
}
/// #881: every croft binds `<hash>.hook.sock` for agent approvals, and
/// hosted panes bind `pane-<id>.sock`, in the same folder as the
/// sessions. Only a session's own socket is a session: `ls` lists the
/// mux and legacy dtach sockets and neither lists, probes nor prunes
/// anything else, live or dead.
#[test]
fn ls_reports_only_session_sockets_and_leaves_the_rest_alone() {
let dir = tempfile::tempdir().unwrap();
let hash = "0db75869317a960b";
let mux = dir.path().join(format!("{hash}.mux.sock"));
let _mux_listener = bind_socket_0600(&mux).unwrap();
write_meta(
&mux,
&SessionMeta {
workspace: PathBuf::from("/tmp/ws"),
created_unix: now_unix(),
},
)
.unwrap();
let hook = dir.path().join(format!("{hash}.hook.sock"));
let _hook_listener = bind_socket_0600(&hook).unwrap();
let pane = dir.path().join("pane-42-1a2b-0.sock");
let _pane_listener = bind_socket_0600(&pane).unwrap();
// A hook socket whose croft is gone: still not ls's to remove.
let stale_hook = dir.path().join("1111222233334444.hook.sock");
drop(bind_socket_0600(&stale_hook).unwrap());
// A legacy dtach session is still a session.
let legacy = dir.path().join("aaaabbbbccccdddd.sock");
let _legacy_listener = bind_socket_0600(&legacy).unwrap();
let mut ids: Vec<String> = session_rows(dir.path()).into_iter().map(|r| r.id).collect();
ids.sort();
assert_eq!(ids, ["0db75869", "aaaabbbb"]);
for kept in [&hook, &pane, &stale_hook] {
assert!(kept.exists(), "ls must not prune {}", kept.display());
}
}
#[test]
fn dtach_attach_argv_has_expected_flags_and_inner() {
let inner = vec![
String::from("/usr/local/bin/croft"),
String::from("/work/repo"),
];
let argv = dtach_attach_argv("/home/u/.cache/croft/sessions/abc.sock", &inner);
assert_eq!(
argv,
vec![
"-A",
"/home/u/.cache/croft/sessions/abc.sock",
"-E",
"-z",
"-r",
"winch",
"/usr/local/bin/croft",
"/work/repo",
]
);
}
#[test]
fn humanize_age_scales_units() {
assert_eq!(humanize_age(5), "5s");
assert_eq!(humanize_age(120), "2m");
assert_eq!(humanize_age(7_200), "2h");
assert_eq!(humanize_age(180_000), "2d");
}
}