diff --git a/CLAUDE.md b/CLAUDE.md index 973b160..9298702 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -339,13 +339,30 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li process group). **Reattach is `attach-session`, never `new-session -A`**: a session that ended while the app was away must not come back as a fresh `claude` (and the update prelude runs once, inside, never again on reattach). - - **Stop ends every tmux server** (`persistent::end_all`, bounded wait for the servers to go) - in the same `close_first` step that closes the tabs, **before** the stop guard's push - snapshot; the entrypoint's TERM hook does the same before its own push (`aws_end_terminals`). - - Probe (`command -v tmux` + the config) once per tunnel, cached by target id and forgotten in - `tunnels::register*`; without it the old launch runs and `open_terminal_session` returns a - one-time notice. Every tmux exec except the attach goes through `exec_oneshot_background` - (never idle-stop activity). + - **Stop ends every tmux server** (`persistent::END_ALL_SCRIPT`: the panes' pids are taken + before `kill-server`, then a bounded 5 s wait for them and the servers) in the same + `close_first` step that closes the tabs, **before** the stop guard's push snapshot. The + entrypoint's TERM hook (`aws_end_terminals`) and the idle watcher's lifetime-cap push run the + same script verbatim (a test pins all three). The store is cleared on Stop, Remove, a move to + Docker and a death-watch stop. + - **The probe** (`command -v tmux` + the config) runs once per tunnel, cached by target id and + forgotten in `tunnels::register*`, and is **three-state**: yes → persistent; a real no (an + image without tmux) → the old launch plus a one-time notice; **unanswered → `Err`**, never + "no" — read as no, a listing would prune every remembered tab and a reattach would end tabs + whose Claude still runs. Nothing is pruned or ended on an `Err`; the frontend asks again. + Every tmux exec except the attach goes through `exec_oneshot_background` (never idle-stop + activity). `TMUX`/`TMUX_TMPDIR` are dropped from the client env and reserved custom names. + - **A tab closed while disconnected** is queued (`terminal_store` `pending_end`) and ended at the + next connect (`list_workspace_terminals` runs it first); a reattach of a closed or queued id + is `Ended`, and a reattach that finishes after its tab closed does not remember it again. + - **The web terminal's exec ids are `web-`**, never the bare key the desktop uses: + sharing one `ExecSessionManager` id would let either front end's attach supersede the + other's exec (no exit callback, a frozen tab) or its close/disconnect end the other's. + - **Cost, by ruling:** a detached `claude` still counts for the idle watcher — that *is* the + feature (remote control keeps working with the app closed) — so a Claude left running keeps + the workspace up and billed until it ends, it is ended (Project Home → Sessions flags + detached Claude sessions, with End), Stop, or the lifetime cap. HostSection's notes say so + (`PERSISTENT_TERMINALS_NOTE`). A detached shell does not keep it up. - Frontend: a persistent tab reattaches on exit (retrying while the tunnel comes back) and shows "[Session ended]" only when the workspace says the session is gone or the project stopped; `usePersistentTerminals` reopens this app's sessions once per connection (`id|container_id`) diff --git a/app/src-tauri/src/commands/project_commands.rs b/app/src-tauri/src/commands/project_commands.rs index ce8f5aa..f93ac07 100644 --- a/app/src-tauri/src/commands/project_commands.rs +++ b/app/src-tauri/src/commands/project_commands.rs @@ -1153,12 +1153,17 @@ pub async fn update_project( // status change or a container id landing since `stored` was read is kept // rather than written over. let mappings_changed = stored.port_mappings != project.port_mappings; + let left_aws = stored.is_aws() && !project.is_aws(); let saved = state .projects_store .update_restoring(project, restore_store_owned_fields)?; // AWS (Q6): a connected workspace's forwards follow the saved mappings // now — there is no container to recreate. Nothing when not connected. + if left_aws { + // Moved to Docker: its workspace terminals went with the task. + crate::storage::terminal_store::clear(&saved.id); + } if saved.is_aws() && mappings_changed { let warn = ports_warner(&app_handle, &saved.id, &saved.name); if let Some(detail) = crate::host::port_forwards::open_for_project(&state.projects_store, &saved.id, Some(warn)).await { diff --git a/app/src-tauri/src/commands/terminal_commands.rs b/app/src-tauri/src/commands/terminal_commands.rs index 086322f..128e882 100644 --- a/app/src-tauri/src/commands/terminal_commands.rs +++ b/app/src-tauri/src/commands/terminal_commands.rs @@ -275,6 +275,8 @@ pub async fn close_terminal_session( state.exec_manager.close_session(&session_id).await; if let Some((project_id, _)) = terminal_store::find(&session_id) { + // A reattach of this tab still in flight must not remember it again. + mark_closed(&session_id); if let Err(e) = terminal_store::remove(&session_id) { log::warn!("Could not forget terminal {session_id}: {e}"); } @@ -285,21 +287,60 @@ pub async fn close_terminal_session( .get(&project_id) .and_then(|p| ContainerTarget::for_project(&p)) .or(session_target); - match target { - Some(t) => { - if let Err(e) = persistent::end(&t, &session_id).await { - log::warn!("Could not end terminal {session_id} in the workspace: {e}"); - } + let ended = match target { + Some(t) => persistent::end(&t, &session_id).await, + None => Err("not connected".to_string()), + }; + if let Err(e) = ended { + // Still running in the workspace (a leftover Claude would keep + // it up): end it at the next connect. + log::warn!("Terminal {session_id}: could not end it now ({e}); ending it at the next connect"); + if let Err(e) = terminal_store::add_pending_end(&project_id, &session_id) { + log::warn!("Could not remember to end terminal {session_id}: {e}"); } - None => log::warn!( - "Terminal {session_id}: not connected, so it keeps running in the workspace \ - (Project Home → Sessions lists it)" - ), } } Ok(()) } +/// Session ids whose tab was closed in this run of the app. +fn closed_ids() -> &'static std::sync::Mutex> { + static C: std::sync::OnceLock>> = + std::sync::OnceLock::new(); + C.get_or_init(Default::default) +} + +fn mark_closed(session_id: &str) { + closed_ids().lock().unwrap_or_else(|e| e.into_inner()).insert(session_id.to_string()); +} + +fn was_closed(session_id: &str) -> bool { + closed_ids().lock().unwrap_or_else(|e| e.into_inner()).contains(session_id) +} + +/// End the project's sessions whose tabs were closed while it could not be +/// reached; returns the ones ended (or found gone). +async fn end_pending(project_id: &str, target: &ContainerTarget, running: &[persistent::RunningSession]) -> Vec { + let pending = terminal_store::pending_end(project_id); + let mut done = Vec::new(); + for key in pending { + if !running.iter().any(|r| r.key == key) { + done.push(key); + continue; + } + match persistent::end(target, &key).await { + Ok(()) => done.push(key), + Err(e) => log::warn!("Could not end closed terminal {key}: {e}"), + } + } + if !done.is_empty() { + if let Err(e) = terminal_store::done_pending_end(project_id, &done) { + log::warn!("Could not update the terminals to end: {e}"); + } + } + done +} + /// A connected AWS project's target, waiting for a re-attach in progress. async fn aws_target(project: &Project) -> Result { if !project.is_aws() { @@ -330,7 +371,11 @@ pub async fn list_workspace_terminals( return Ok(Vec::new()); } let target = aws_target(&project).await?; - let running = persistent::list(&target).await?; + // An `Err` (an unanswered probe or listing) says nothing about what runs: + // nothing is pruned or ended on it, and the caller asks again. + let mut running = persistent::list(&target).await?; + let ended = end_pending(&project_id, &target, &running).await; + running.retain(|r| !ended.contains(&r.key)); let keys = running.iter().map(|r| r.key.clone()).collect(); if let Err(e) = terminal_store::retain(&project_id, &keys) { log::warn!("Could not prune remembered terminals: {e}"); @@ -384,11 +429,18 @@ pub async fn reattach_terminal_session( .projects_store .get(&project_id) .ok_or_else(|| format!("Project {} not found", project_id))?; + if was_closed(&session_id) { + return Ok(ReattachOutcome::Ended); + } let target = aws_target(&project).await?; - if !persistent::supported(&target).await { + // Unanswered → `Err` (the tab retries); only a real "no tmux" ends it. + if !persistent::supported(&target).await? { terminal_store::remove(&session_id).ok(); return Ok(ReattachOutcome::Ended); } + if terminal_store::pending_end(&project_id).contains(&session_id) { + return Ok(ReattachOutcome::Ended); + } let running = persistent::list(&target).await?; let Some(found) = running.into_iter().find(|r| r.key == session_id) else { if let Err(e) = terminal_store::remove(&session_id) { @@ -404,12 +456,14 @@ pub async fn reattach_terminal_session( let output_event = format!("terminal-output-{}", session_id); if let Some(rows) = rows { - if let Some(history) = persistent::history(&target, &session_id).await { - let bytes = persistent::replay_bytes(&history, rows); - if !bytes.is_empty() { - let _ = app_handle.emit(&output_event, bytes); - } - } + // Exactly one chunk, empty when there is no history: the tab treats + // the first chunk after asking for a replay as the replay, so live + // output after it gets its URL/OSC/SSO handling again. + let bytes = persistent::history(&target, &session_id) + .await + .map(|h| persistent::replay_bytes(&h, rows)) + .unwrap_or_default(); + let _ = app_handle.emit(&output_event, bytes); } let exit_event = format!("terminal-exit-{}", session_id); @@ -431,6 +485,12 @@ pub async fn reattach_terminal_session( ) .await?; + // Closed while this attach was under way: its close already ended (or + // queued the end of) the session; don't remember it again. + if was_closed(&session_id) { + state.exec_manager.close_session(&session_id).await; + return Ok(ReattachOutcome::Ended); + } let entry = PersistedTerminal { id: session_id.clone(), kind, name: name.clone() }; if let Err(e) = terminal_store::add(&project_id, entry) { log::warn!("Could not remember terminal {session_id}: {e}"); diff --git a/app/src-tauri/src/container_watch.rs b/app/src-tauri/src/container_watch.rs index 8b11d21..8266f4a 100644 --- a/app/src-tauri/src/container_watch.rs +++ b/app/src-tauri/src/container_watch.rs @@ -117,7 +117,14 @@ async fn settle(store: &ProjectsStore, container: &ContainerTarget) -> Option return Some(*p), + ExitMark::Flipped(p) => { + // An AWS task that stopped by itself (idle, lifetime cap, + // crash) took its persistent terminals with it. + if p.is_aws() { + crate::storage::terminal_store::clear(&p.id); + } + return Some(*p); + } ExitMark::NotApplicable => return None, // The store moved while the host was answering — possibly a stop and // start of this very container. The inspect is stale; ask again. diff --git a/app/src-tauri/src/docker/aws_mode_tests.rs b/app/src-tauri/src/docker/aws_mode_tests.rs index e678e0b..8fc4f95 100644 --- a/app/src-tauri/src/docker/aws_mode_tests.rs +++ b/app/src-tauri/src/docker/aws_mode_tests.rs @@ -1359,13 +1359,19 @@ fn aws_stop_ends_the_persistent_terminals_before_its_push() { let calls = fs::read_to_string(&log).unwrap(); assert_eq!( calls, - "tmux -L triple-c-aaa kill-server\ntmux -L triple-c-bbb kill-server\npush --reason sigterm\n" + "tmux -L triple-c-aaa list-panes -a -F #{pane_pid}\ntmux -L triple-c-aaa kill-server\n\ + tmux -L triple-c-bbb list-panes -a -F #{pane_pid}\ntmux -L triple-c-bbb kill-server\n\ + push --reason sigterm\n" ); drop(keep); - // The app's own Stop ends them with the same loop. - let app = crate::host::persistent::end_all_cmd().join(" "); - assert!(app.contains(r#"tmux -L "${s##*/}" kill-server"#) && app.contains("\"$d\"/triple-c-*"), "{app}"); - assert!(block(ENTRYPOINT, "aws_stop").contains(r#"tmux -L "${s##*/}" kill-server"#)); + // The app's own Stop and the idle watcher's lifetime stop run the very + // same script. + let script = crate::host::persistent::END_ALL_SCRIPT; + assert!(block(ENTRYPOINT, "aws_stop").contains(script)); + assert!(IDLE_WATCH.contains(script)); + assert_eq!(crate::host::persistent::end_all_cmd()[2], script); + let lifetime = &IDLE_WATCH[IDLE_WATCH.find("lifetime_stop() {").unwrap()..]; + assert!(lifetime.find("end_terminals").unwrap() < lifetime.find("--reason lifetime").unwrap()); } #[test] diff --git a/app/src-tauri/src/docker/container.rs b/app/src-tauri/src/docker/container.rs index 036ec67..220bd04 100644 --- a/app/src-tauri/src/docker/container.rs +++ b/app/src-tauri/src/docker/container.rs @@ -260,6 +260,10 @@ const RESERVED_ENV_PREFIXES: &[&str] = &["ANTHROPIC_", "AWS_", "GIT_", "HOST_", /// value would silently outrank the keychain-held shared token and be invisible /// to the auth UI. const RESERVED_ENV_EXACT: &[&str] = &[ + // Persistent AWS terminals (`host::persistent`): either would point a + // tab's tmux at another socket than the one the app lists and ends. + "TMUX", + "TMUX_TMPDIR", "CLAUDE_INSTRUCTIONS", "MCP_SERVERS_JSON", "CLAUDE_CODE_SETTINGS_JSON", diff --git a/app/src-tauri/src/host/persistent.rs b/app/src-tauri/src/host/persistent.rs index 92c339b..f81303e 100644 --- a/app/src-tauri/src/host/persistent.rs +++ b/app/src-tauri/src/host/persistent.rs @@ -226,26 +226,32 @@ exit 0"# } /// End every session (Stop, before the WIP push's snapshot), then wait — -/// bounded — until no tmux server of ours is left, so a pane's last writes -/// land before the snapshot. +/// bounded, 5 s — until the panes' processes (taken before the kill) and +/// the servers are gone, so a pane's last writes land before the snapshot. +/// The same script is `entrypoint.sh`'s `aws_end_terminals` and the idle +/// watcher's (a test pins them together). pub fn end_all_cmd() -> Vec { - vec![ - "sh".into(), - "-c".into(), - format!( - r#"{SOCKET_DIR} + vec!["sh".into(), "-c".into(), END_ALL_SCRIPT.into()] +} + +/// See [`end_all_cmd`]. +pub const END_ALL_SCRIPT: &str = r#"d="${TMUX_TMPDIR:-/tmp}/tmux-$(id -u)" command -v tmux >/dev/null 2>&1 || exit 0 -for s in "$d"/{SOCKET_PREFIX}*; do - [ -S "$s" ] && tmux -L "${{s##*/}}" kill-server 2>/dev/null +pids="" +for s in "$d"/triple-c-*; do + [ -S "$s" ] || continue + pids="$pids $(tmux -L "${s##*/}" list-panes -a -F '#{pane_pid}' 2>/dev/null)" + tmux -L "${s##*/}" kill-server 2>/dev/null done i=0 -while pgrep -u "$(id -u)" -f '^tmux -L {SOCKET_PREFIX}' >/dev/null 2>&1 && [ $i -lt 50 ]; do +while [ $i -lt 50 ]; do + alive="" + for p in $pids; do kill -0 "$p" 2>/dev/null && alive=1; done + pgrep -u "$(id -u)" -f '^tmux -L triple-c-' >/dev/null 2>&1 && alive=1 + [ -n "$alive" ] || break sleep 0.1; i=$((i + 1)) done -exit 0"# - ), - ] -} +exit 0"#; /// A session found running in the workspace. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -276,8 +282,13 @@ pub fn parse_list(out: &str) -> Vec { /// The exec environment of a tmux client: the caller's pairs (copy-at-connect /// credentials, which the server it starts inherits) plus [`OUTER_TERM`]. +/// `TMUX`/`TMUX_TMPDIR` are dropped (and reserved as custom variable +/// names): either would point the client at another socket than the one the +/// app lists and ends. pub fn client_env(mut env: Vec) -> Vec { - env.retain(|e| !e.starts_with("TERM=")); + env.retain(|e| { + !e.starts_with("TERM=") && !e.starts_with("TMUX=") && !e.starts_with("TMUX_TMPDIR=") + }); env.push(OUTER_TERM.to_string()); env } @@ -333,9 +344,13 @@ pub async fn probe(target: &ContainerTarget) -> Option { Some(supported) } -/// [`probe`], an unanswered probe counting as no. -pub async fn supported(target: &ContainerTarget) -> bool { - probe(target).await.unwrap_or(false) +/// [`probe`] for callers that must not guess: an unanswered probe is an +/// error (the caller retries), never "no tmux" — reading it as no would +/// prune remembered sessions and end tabs whose Claude still runs. +pub async fn supported(target: &ContainerTarget) -> Result { + probe(target) + .await + .ok_or_else(|| "The workspace did not answer whether it can keep terminals".to_string()) } /// The "no tmux" notice, the first time it is asked for on a tunnel whose @@ -404,11 +419,8 @@ pub async fn plan( return plan_with(false, false, None, key, kind, name, inner, env); } validate_key(key)?; - let ok = supported(target).await; + let ok = supported(target).await?; let notice = if ok { None } else { take_notice(target.id()) }; - if !ok && notice.is_none() { - log::info!("Terminal {key}: not persistent (no tmux in the image, or it could not be asked)"); - } plan_with(true, ok, notice, key, kind, name, inner, env) } @@ -421,8 +433,9 @@ async fn small(target: &ContainerTarget, cmd: Vec) -> Result<(String, i6 } /// The sessions running in the workspace (none on an image without tmux). +/// An `Err` (including an unanswered probe) says nothing about what runs. pub async fn list(target: &ContainerTarget) -> Result, String> { - if !supported(target).await { + if !supported(target).await? { return Ok(Vec::new()); } let (out, code) = small(target, list_cmd()).await?; diff --git a/app/src-tauri/src/host/persistent/tests.rs b/app/src-tauri/src/host/persistent/tests.rs index 818a4d7..bbce61e 100644 --- a/app/src-tauri/src/host/persistent/tests.rs +++ b/app/src-tauri/src/host/persistent/tests.rs @@ -161,3 +161,26 @@ fn the_image_ships_the_config_and_tmux() { assert!(df.contains("COPY triple-c-tmux.conf /etc/triple-c/tmux.conf")); assert!(df.lines().any(|l| l.trim() == "tmux \\"), "tmux in an apt layer"); } + +/// An unanswered probe (a tunnel reopening) is an error, never "no tmux": +/// read as no, the listing would prune every remembered tab and a reattach +/// would end tabs whose Claude still runs. +#[tokio::test] +async fn an_unanswered_probe_is_an_error_not_a_no() { + let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port(); + let target = ContainerTarget::Agent( + crate::host::AgentEndpoint::loopback(port, "x".repeat(64)), + ); + assert_eq!(probe(&target).await, None); + assert!(supported(&target).await.is_err()); + assert!(list(&target).await.is_err(), "no listing, so nothing to prune"); + let planned = plan(true, &target, KEY, TerminalKind::Claude, None, claude_inner(), vec![]).await; + assert!(planned.is_err(), "not silently a non-persistent tab"); + assert!(probes().lock().unwrap().get(target.id()).is_none(), "asked again next time"); +} + +#[test] +fn the_client_env_drops_what_would_point_tmux_elsewhere() { + let env = client_env(vec!["TMUX=/tmp/x,1,0".into(), "TMUX_TMPDIR=/elsewhere".into(), "A=1".into()]); + assert_eq!(env, vec!["A=1".to_string(), OUTER_TERM.to_string()]); +} diff --git a/app/src-tauri/src/storage/terminal_store.rs b/app/src-tauri/src/storage/terminal_store.rs index 19ed629..857053a 100644 --- a/app/src-tauri/src/storage/terminal_store.rs +++ b/app/src-tauri/src/storage/terminal_store.rs @@ -42,6 +42,11 @@ struct File { version: u32, #[serde(default)] projects: BTreeMap>, + /// Tabs closed while the workspace could not be reached: their tmux + /// servers still run (a leftover Claude would keep a billed workspace + /// up), so they are ended at the next connect. + #[serde(default)] + pending_end: BTreeMap>, } fn lock() -> &'static Mutex<()> { @@ -78,6 +83,7 @@ fn load_file(dir: &Path) -> File { fn save_file(dir: &Path, mut file: File) -> Result<(), String> { file.version = FORMAT_VERSION; file.projects.retain(|_, v| !v.is_empty()); + file.pending_end.retain(|_, v| !v.is_empty()); let path = path_in(dir); let tmp = path.with_extension("json.tmp"); let data = serde_json::to_string_pretty(&file).map_err(|e| e.to_string())?; @@ -146,10 +152,38 @@ pub fn retain_in(dir: &Path, project_id: &str, running: &HashSet) -> Res }) } -/// Forget all of a project's terminals (stopped, removed). +/// Forget all of a project's terminals (stopped, removed, moved off AWS): +/// remembered and pending-end alike. pub fn clear_in(dir: &Path, project_id: &str) -> Result<(), String> { edit_in(dir, |f| { f.projects.remove(project_id); + f.pending_end.remove(project_id); + }) +} + +/// Remember to end `session_id` at the project's next connect. +pub fn add_pending_end_in(dir: &Path, project_id: &str, session_id: &str) -> Result<(), String> { + validate_key(session_id)?; + edit_in(dir, |f| { + let list = f.pending_end.entry(project_id.to_string()).or_default(); + if !list.iter().any(|k| k == session_id) && list.len() < MAX_PER_PROJECT { + list.push(session_id.to_string()); + } + }) +} + +/// The project's sessions waiting to be ended. +pub fn pending_end_in(dir: &Path, project_id: &str) -> Vec { + let _g = lock().lock().unwrap_or_else(|e| e.into_inner()); + load_file(dir).pending_end.remove(project_id).unwrap_or_default() +} + +/// They were ended (or are gone): stop remembering them. +pub fn done_pending_end_in(dir: &Path, project_id: &str, ended: &[String]) -> Result<(), String> { + edit_in(dir, |f| { + if let Some(list) = f.pending_end.get_mut(project_id) { + list.retain(|k| !ended.contains(k)); + } }) } @@ -175,6 +209,18 @@ pub fn retain(project_id: &str, running: &HashSet) -> Result<(), String> retain_in(&data_dir()?, project_id, running) } +pub fn add_pending_end(project_id: &str, session_id: &str) -> Result<(), String> { + add_pending_end_in(&data_dir()?, project_id, session_id) +} + +pub fn pending_end(project_id: &str) -> Vec { + data_dir().map(|d| pending_end_in(&d, project_id)).unwrap_or_default() +} + +pub fn done_pending_end(project_id: &str, ended: &[String]) -> Result<(), String> { + done_pending_end_in(&data_dir()?, project_id, ended) +} + /// Best effort: a failure is logged (the next listing prunes anyway). pub fn clear(project_id: &str) { match data_dir().and_then(|d| clear_in(&d, project_id)) { @@ -227,6 +273,22 @@ mod tests { assert!(list_in(&d, "p").is_empty()); } + #[test] + fn a_tab_closed_while_disconnected_is_ended_at_the_next_connect() { + let d = dir("pending"); + add_pending_end_in(&d, "p", "a-1").unwrap(); + add_pending_end_in(&d, "p", "a-1").unwrap(); + add_pending_end_in(&d, "p", "b-2").unwrap(); + add_pending_end_in(&d, "q", "c-3").unwrap(); + assert!(add_pending_end_in(&d, "p", "../x").is_err()); + assert_eq!(pending_end_in(&d, "p"), ["a-1", "b-2"]); + done_pending_end_in(&d, "p", &["a-1".to_string()]).unwrap(); + assert_eq!(pending_end_in(&d, "p"), ["b-2"]); + clear_in(&d, "p").unwrap(); + assert!(pending_end_in(&d, "p").is_empty()); + assert_eq!(pending_end_in(&d, "q"), ["c-3"], "per project"); + } + #[test] fn a_bad_key_is_refused_and_a_corrupt_file_reads_as_empty() { let d = dir("bad"); diff --git a/app/src-tauri/src/web_terminal/ws_handler.rs b/app/src-tauri/src/web_terminal/ws_handler.rs index ca1abe8..5c9196f 100644 --- a/app/src-tauri/src/web_terminal/ws_handler.rs +++ b/app/src-tauri/src/web_terminal/ws_handler.rs @@ -292,13 +292,28 @@ async fn close_owned(state: &WebTerminalState, session_id: &str, persistent: boo let target = state.exec_manager.get_target(session_id).await.ok(); state.exec_manager.close_session(session_id).await; let Some(target) = target else { return }; + let Some(key) = tmux_key_of(session_id) else { return }; if persistent { - if let Err(e) = crate::host::persistent::end(&target, session_id).await { + if let Err(e) = crate::host::persistent::end(&target, key).await { log::warn!("Web terminal: could not end terminal {session_id}: {e}"); } } } +/// The web terminal's exec ids are `web-`, never the bare key: the +/// desktop app attaches to a workspace terminal under the bare key in the +/// same `ExecSessionManager`, and sharing the id would make either front +/// end's attach take over (or its close/disconnect end) the other's exec. +const WEB_EXEC_PREFIX: &str = "web-"; + +fn web_exec_id(key: &str) -> String { + format!("{WEB_EXEC_PREFIX}{key}") +} + +fn tmux_key_of(exec_id: &str) -> Option<&str> { + exec_id.strip_prefix(WEB_EXEC_PREFIX) +} + /// Open a new terminal session for a project. async fn handle_open( state: &WebTerminalState, @@ -329,7 +344,8 @@ async fn handle_open( // environment (never the agent's). Empty for everything else. let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()).await; - let session_id = uuid::Uuid::new_v4().to_string(); + let key = uuid::Uuid::new_v4().to_string(); + let session_id = web_exec_id(&key); let project_name = project.name.clone(); // AWS: in its own tmux server, like the desktop app's tabs @@ -344,7 +360,7 @@ async fn handle_open( let plan = crate::host::persistent::plan( project.is_aws(), &container, - &session_id, + &key, kind, Some("web terminal"), cmd, @@ -401,3 +417,22 @@ async fn handle_open( Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + /// A desktop reattach (exec id = the bare key) and the web terminal's own + /// exec of the same tmux session never share an `ExecSessionManager` id, + /// in either direction, and the web side maps back to the right key. + #[test] + fn web_and_desktop_execs_of_one_terminal_never_share_an_id() { + let key = "0b9c6f7e-1d2a-4c3b-9e8f-7a6b5c4d3e2f"; + let web = web_exec_id(key); + assert_ne!(web, key, "a desktop reattach must not supersede the web exec"); + assert_eq!(tmux_key_of(&web), Some(key)); + // A desktop exec id is not one the web side would end or detach. + assert_eq!(tmux_key_of(key), None); + crate::host::persistent::validate_key(&web).unwrap(); + } +} diff --git a/app/src/components/projects/home/SessionsTab.tsx b/app/src/components/projects/home/SessionsTab.tsx index 7d3fe42..b72914a 100644 --- a/app/src/components/projects/home/SessionsTab.tsx +++ b/app/src/components/projects/home/SessionsTab.tsx @@ -153,7 +153,8 @@ function WorkspaceTerminals({ project }: { project: Project }) {

Terminals keep running in the workspace when Triple-C closes or loses its - connection. Closing a tab ends its terminal. + connection. Closing a tab ends its terminal. A Claude session left running keeps + the workspace up until it ends, you end it here, you Stop, or the lifetime cap.