From ac0bb456d348f82aab7918f6540a44fde1444108 Mon Sep 17 00:00:00 2001
From: Josh Knapp
Date: Tue, 17 Nov 2026 15:08:31 -0800
Subject: [PATCH] fix(aws): persistent terminals review fixes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- Probe is three-state: an unanswered probe is an Err (tab retries), never
"no tmux" — nothing is pruned or ended on it, opens fail rather than
silently losing persistence.
- Web terminal execs are web-: a desktop reattach no longer
supersedes the web exec, nor does a web close/disconnect touch the
desktop's.
- A tab closed while disconnected is queued (pending_end) and ended at
the next connect; a reattach of a closed/queued id is Ended and is not
remembered again.
- Cost ruling documented: a detached Claude keeps the workspace up
(HostSection note, Sessions flags detached Claude sessions).
- end_all waits for pane pids; the same script runs in the entrypoint's
TERM hook and before the idle watcher's lifetime-cap push.
- TMUX/TMUX_TMPDIR dropped from the client env and reserved.
- Store cleared on a move to Docker and on a death-watch stop.
- Frontend: no duplicate reopen of a tab still opening; quick-exit
backoff then stop; the replay is exactly one chunk, so live output
keeps its URL/OSC/SSO handling.
Co-Authored-By: Claude Opus 5.5
---
CLAUDE.md | 31 ++++--
.../src/commands/project_commands.rs | 5 +
.../src/commands/terminal_commands.rs | 94 +++++++++++++++----
app/src-tauri/src/container_watch.rs | 9 +-
app/src-tauri/src/docker/aws_mode_tests.rs | 16 +++-
app/src-tauri/src/docker/container.rs | 4 +
app/src-tauri/src/host/persistent.rs | 59 +++++++-----
app/src-tauri/src/host/persistent/tests.rs | 23 +++++
app/src-tauri/src/storage/terminal_store.rs | 64 ++++++++++++-
app/src-tauri/src/web_terminal/ws_handler.rs | 41 +++++++-
.../components/projects/home/SessionsTab.tsx | 11 ++-
.../projects/home/config/HostSection.tsx | 2 +
.../components/terminal/TerminalView.test.tsx | 44 +++++++++
app/src/components/terminal/TerminalView.tsx | 25 ++++-
app/src/hooks/usePersistentTerminals.test.ts | 23 ++++-
app/src/hooks/usePersistentTerminals.ts | 5 +-
app/src/hooks/useTerminal.ts | 18 +++-
app/src/lib/awsWorkspace.ts | 8 ++
app/src/lib/persistentTerminals.test.ts | 14 ++-
app/src/lib/persistentTerminals.ts | 21 +++++
container/entrypoint.sh | 34 +++++--
container/triple-c-idle-watch | 33 +++++++
22 files changed, 510 insertions(+), 74 deletions(-)
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.
)}
+
{PERSISTENT_TERMINALS_NOTE}
diff --git a/app/src/components/terminal/TerminalView.test.tsx b/app/src/components/terminal/TerminalView.test.tsx
index b0e2bfe..57d05e9 100644
--- a/app/src/components/terminal/TerminalView.test.tsx
+++ b/app/src/components/terminal/TerminalView.test.tsx
@@ -2191,11 +2191,55 @@ describe("persistent AWS terminals", () => {
expect(reattachTerminalSession).toHaveBeenCalledWith("p1", "s1", null);
expect(screenText()).not.toContain("[Session ended]");
+ // Long after that attach (a quick drop would back off first).
+ const later = Date.now() + 60_000;
+ const now = vi.spyOn(Date, "now").mockReturnValue(later);
reattachTerminalSession.mockResolvedValue({ outcome: "ended" });
await act(async () => exit({ payload: [] }));
await settle();
expect(screenText()).toContain("[Session ended]");
expect(useAppState.getState().sessions[0].persistent).toBe(false);
+ now.mockRestore();
+ });
+
+ it("stops reattaching a session that keeps dropping right after it attaches", async () => {
+ reattachTerminalSession.mockResolvedValue({ outcome: "reattached", kind: "claude", name: null });
+ vi.useFakeTimers({ shouldAdvanceTime: true });
+ mountPersistent(false);
+ await settle();
+ const exit = ptyOutput.listeners.get("terminal-exit-s1");
+ if (!exit) throw new Error("no exit listener");
+ for (let i = 0; i < 4; i++) {
+ await act(async () => exit({ payload: [] }));
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(i === 0 ? 100 : 3000 * i + 100);
+ });
+ await settle();
+ }
+ // First drop reattaches at once; the next two are quick and back off;
+ // the third quick one stops.
+ expect(reattachTerminalSession).toHaveBeenCalledTimes(3);
+ expect(screenText()).toContain("keeps dropping");
+ vi.useRealTimers();
+ });
+
+ it("only the first chunk after a replay request is the replay", async () => {
+ reattachTerminalSession.mockImplementation(async () => {
+ const emit = ptyOutput.listeners.get("terminal-output-s1")!;
+ emit({ payload: Array.from(new TextEncoder().encode("old history\r\n")) });
+ return { outcome: "reattached", kind: "claude", name: null };
+ });
+ mountPersistent(true);
+ await settle();
+ expect(screenText()).toContain("old history");
+ // Live output now gets the URL detector again.
+ const emit = ptyOutput.listeners.get("terminal-output-s1")!;
+ await act(async () => {
+ emit({ payload: Array.from(new TextEncoder().encode(`\x1b]7777;open;${btoa("https://example.com/live")}\x07`)) });
+ await new Promise((r) => setTimeout(r, 0));
+ });
+ await settle();
+ expect(document.querySelector(URL_TOAST_SELECTOR)).not.toBeNull();
});
it("an ordinary tab's exit just ends it", async () => {
diff --git a/app/src/components/terminal/TerminalView.tsx b/app/src/components/terminal/TerminalView.tsx
index 2f48e63..912e814 100644
--- a/app/src/components/terminal/TerminalView.tsx
+++ b/app/src/components/terminal/TerminalView.tsx
@@ -38,7 +38,7 @@ import { resolveTerminalGpuRendering } from "../../lib/terminalRenderer";
import TerminalContextMenu from "./TerminalContextMenu";
import { createFilePathLinkProvider } from "./filePathLinkProvider";
import { reattachTerminalSession } from "../../lib/tauri-commands";
-import { reattachWithRetry } from "../../lib/persistentTerminals";
+import { nextReconnect, reattachWithRetry } from "../../lib/persistentTerminals";
import type { FilePathMatch } from "../../lib/filePathLinks";
interface Props {
@@ -1364,6 +1364,9 @@ export default function TerminalView({ sessionId, active }: Props) {
const outputPromise = onOutput(sessionId, (data) => {
if (aborted) return;
if (replaying) {
+ // The backend sends a replay as exactly one chunk (empty when there
+ // is no history) before attaching: everything after it is live.
+ replaying = false;
term.write(data, syncMouseCapture);
return;
}
@@ -1430,6 +1433,7 @@ export default function TerminalView({ sessionId, active }: Props) {
const update = useAppState.getState().updateSession;
switch (result) {
case "reattached":
+ lastReattachAt = Date.now();
update(sessionId, { pendingReattach: false });
// tmux follows the newest client's size.
resize(sessionId, term.cols, term.rows).catch(() => {});
@@ -1448,10 +1452,27 @@ export default function TerminalView({ sessionId, active }: Props) {
}
};
+ // An attach that keeps dropping right after it succeeds (a client that
+ // cannot stay attached) must not loop: back off, then stop.
+ let lastReattachAt = 0;
+ let quickExits = 0;
const exitPromise = onExit(sessionId, () => {
if (aborted) return;
if (persistentRef.current) {
- void reconnect(null);
+ const plan = nextReconnect(lastReattachAt, Date.now(), quickExits);
+ quickExits = plan.quickExits;
+ if (plan.delayMs === null) {
+ term.write(
+ "\r\n\x1b[33m[Disconnected: the session keeps dropping. It may still be running in " +
+ "the workspace: Project Home → Sessions can reattach or end it.]\x1b[0m\r\n",
+ );
+ return;
+ }
+ if (plan.delayMs === 0) void reconnect(null);
+ else
+ setTimeout(() => {
+ if (!aborted) void reconnect(null);
+ }, plan.delayMs);
return;
}
ended();
diff --git a/app/src/hooks/usePersistentTerminals.test.ts b/app/src/hooks/usePersistentTerminals.test.ts
index 4cd1408..a37a9e9 100644
--- a/app/src/hooks/usePersistentTerminals.test.ts
+++ b/app/src/hooks/usePersistentTerminals.test.ts
@@ -1,13 +1,17 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
const listWorkspaceTerminals = vi.fn();
+const openTerminalSession = vi.fn();
vi.mock("../lib/tauri-commands", () => ({
listWorkspaceTerminals: (id: string) => listWorkspaceTerminals(id),
+ openTerminalSession: (...a: unknown[]) => openTerminalSession(...a),
}));
+vi.mock("@tauri-apps/api/event", () => ({ listen: vi.fn() }));
import { act, renderHook, waitFor } from "@testing-library/react";
import { useAppState } from "../store/appState";
-import { resetPersistentTerminalSync, usePersistentTerminals } from "./usePersistentTerminals";
+import { resetPersistentTerminalSync, syncProjectTerminals, usePersistentTerminals } from "./usePersistentTerminals";
+import { useTerminal } from "./useTerminal";
import type { Project, WorkspaceTerminal } from "../lib/types";
const aws = (container_id: string | null, status = "running") =>
@@ -68,3 +72,20 @@ describe("usePersistentTerminals", () => {
expect(listWorkspaceTerminals).not.toHaveBeenCalled();
});
});
+
+it("does not reopen a tab whose own open is still in flight", async () => {
+ let finishOpen: (v: unknown) => void = () => {};
+ openTerminalSession.mockImplementation(() => new Promise((r) => (finishOpen = r)));
+ const { result } = renderHook(() => useTerminal());
+ const opening = result.current.open("p1", "api");
+ const id = openTerminalSession.mock.calls[0][1] as string;
+ // The workspace already lists (and remembers) it.
+ listWorkspaceTerminals.mockResolvedValue([t(id, true)]);
+ expect(await syncProjectTerminals(aws("http://127.0.0.1:5000/"))).toBe(0);
+ finishOpen({ persistent: true, notice: null });
+ await act(async () => {
+ await opening;
+ });
+ expect(useAppState.getState().sessions.filter((s) => s.id === id)).toHaveLength(1);
+ expect(useAppState.getState().sessions[0].pendingReattach).toBeUndefined();
+});
diff --git a/app/src/hooks/usePersistentTerminals.ts b/app/src/hooks/usePersistentTerminals.ts
index caee2b7..f2fb1f7 100644
--- a/app/src/hooks/usePersistentTerminals.ts
+++ b/app/src/hooks/usePersistentTerminals.ts
@@ -4,6 +4,7 @@ import { isAwsProject } from "../lib/awsWorkspace";
import { listWorkspaceTerminals } from "../lib/tauri-commands";
import { reattachTab, terminalsToReopen } from "../lib/persistentTerminals";
import type { Project } from "../lib/types";
+import { isOpening } from "./useTerminal";
/** Connections already synced this run, `|`: an AWS
* project's `container_id` is its tunnel's endpoint, new on every connect. */
@@ -30,7 +31,9 @@ export async function syncProjectTerminals(project: Project): Promise {
const running = await listWorkspaceTerminals(project.id);
const state = useAppState.getState();
const open = new Set(state.sessions.map((s) => s.id));
- const reopen = terminalsToReopen(running, open);
+ // A tab being opened right now is listed (and remembered) before its
+ // own addSession lands: never a second copy of it.
+ const reopen = terminalsToReopen(running, open).filter((t) => !isOpening(t.session_id));
for (const t of reopen) {
state.addSession(reattachTab(project.id, project.name, t), { activate: false });
}
diff --git a/app/src/hooks/useTerminal.ts b/app/src/hooks/useTerminal.ts
index 49519fa..f9fbeed 100644
--- a/app/src/hooks/useTerminal.ts
+++ b/app/src/hooks/useTerminal.ts
@@ -84,6 +84,14 @@ function discardInputQueue(sessionId: string): void {
if (!q.draining) inputQueues.delete(sessionId);
}
+/** Session ids whose `open_terminal_session` is in flight: a reconnect sync
+ * listing the workspace meanwhile must not reopen them as a second tab. */
+const opening = new Set();
+
+export function isOpening(sessionId: string): boolean {
+ return opening.has(sessionId);
+}
+
export function useTerminal() {
const { sessions, activeSessionId, addSession, removeSession, setActiveSession } =
useAppState(
@@ -105,7 +113,15 @@ export function useTerminal() {
resumeSessionId?: string,
) => {
const sessionId = crypto.randomUUID();
- const opened = await commands.openTerminalSession(projectId, sessionId, sessionType, sessionName, resumeSessionId);
+ opening.add(sessionId);
+ let opened;
+ try {
+ opened = await commands.openTerminalSession(projectId, sessionId, sessionType, sessionName, resumeSessionId);
+ } catch (e) {
+ opening.delete(sessionId);
+ throw e;
+ }
+ opening.delete(sessionId);
addSession({
id: sessionId,
projectId,
diff --git a/app/src/lib/awsWorkspace.ts b/app/src/lib/awsWorkspace.ts
index 6914bd8..a2c1ca4 100644
--- a/app/src/lib/awsWorkspace.ts
+++ b/app/src/lib/awsWorkspace.ts
@@ -323,3 +323,11 @@ export const EC2_DOCKER_ACCESS_NOTE =
export const EC2_VPN_HINT =
"Grants NET_ADMIN, the /dev/net/tun device and src_valid_mark so a VPN client (PIA, WireGuard, OpenVPN) can build a tunnel inside the workspace, an ECS task on your own EC2 instance. Anything in the workspace can then reconfigure the task's own network stack; the instance's is untouched. Applies on the project's next start.";
export const EC2_NO_CAPACITY = "Ask an admin to add EC2 capacity to your onboarding";
+
+/** Persistent terminals (`host/persistent.rs`): the cost of the user's goal
+ * that a Claude session keeps working with the app closed. */
+export const PERSISTENT_TERMINALS_NOTE =
+ "Terminals keep running in the workspace when you close Triple-C. A Claude session left " +
+ "running keeps the workspace up (and billed) after you close the app — until it ends, you " +
+ "end it in Project Home → Sessions, you Stop, or the lifetime cap. Shells left open do not " +
+ "keep it up.";
diff --git a/app/src/lib/persistentTerminals.test.ts b/app/src/lib/persistentTerminals.test.ts
index 2e873c7..5cd8438 100644
--- a/app/src/lib/persistentTerminals.test.ts
+++ b/app/src/lib/persistentTerminals.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect, vi } from "vitest";
-import { reattachTab, reattachWithRetry, terminalLabel, terminalsToReopen } from "./persistentTerminals";
+import { nextReconnect, reattachTab, reattachWithRetry, terminalLabel, terminalsToReopen, RECONNECT_DELAY_MS } from "./persistentTerminals";
import type { ReattachOutcome, WorkspaceTerminal } from "./types";
const noSleep = () => Promise.resolve();
@@ -78,3 +78,15 @@ describe("which terminals come back as tabs", () => {
expect(terminalLabel(term("k", true, "bash"))).toBe("Shell");
});
});
+
+describe("nextReconnect", () => {
+ it("reattaches at once after a long-lived attach, backs off on quick drops, then stops", () => {
+ expect(nextReconnect(0, 10_000, 0)).toEqual({ delayMs: 0, quickExits: 0 });
+ expect(nextReconnect(1_000, 60_000, 2)).toEqual({ delayMs: 0, quickExits: 0 });
+ let r = nextReconnect(1_000, 2_000, 0);
+ expect(r).toEqual({ delayMs: RECONNECT_DELAY_MS, quickExits: 1 });
+ r = nextReconnect(1_000, 2_000, r.quickExits);
+ expect(r).toEqual({ delayMs: RECONNECT_DELAY_MS * 2, quickExits: 2 });
+ expect(nextReconnect(1_000, 2_000, r.quickExits).delayMs).toBeNull();
+ });
+});
diff --git a/app/src/lib/persistentTerminals.ts b/app/src/lib/persistentTerminals.ts
index d111d52..4d452c6 100644
--- a/app/src/lib/persistentTerminals.ts
+++ b/app/src/lib/persistentTerminals.ts
@@ -43,6 +43,27 @@ export async function reattachWithRetry(opts: {
return opts.stillWanted() ? "gave_up" : "cancelled";
}
+/** An exit this soon after a successful reattach counts as a quick one. */
+export const QUICK_EXIT_MS = 5000;
+/** Quick exits in a row before a tab stops reattaching on its own. */
+export const MAX_QUICK_EXITS = 3;
+
+/**
+ * What a persistent tab does when its exec ends: reattach now (`delayMs` 0),
+ * after a back-off, or stop (`null`) when attaches keep dropping right after
+ * they succeed. `quickExits` is the new count to keep.
+ */
+export function nextReconnect(
+ lastReattachAt: number,
+ now: number,
+ quickExits: number,
+): { delayMs: number | null; quickExits: number } {
+ const quick = lastReattachAt > 0 && now - lastReattachAt < QUICK_EXIT_MS;
+ const count = quick ? quickExits + 1 : 0;
+ if (count >= MAX_QUICK_EXITS) return { delayMs: null, quickExits: count };
+ return { delayMs: count === 0 ? 0 : RECONNECT_DELAY_MS * count, quickExits: count };
+}
+
/** The tab label a workspace terminal gets when nothing else names it. */
export function terminalLabel(t: Pick): string {
return t.name ?? (t.kind === "claude" ? "Claude Code" : "Shell");
diff --git a/container/entrypoint.sh b/container/entrypoint.sh
index 37239cc..0d8c721 100644
--- a/container/entrypoint.sh
+++ b/container/entrypoint.sh
@@ -888,16 +888,30 @@ triplec_lifetime_exit=76
# The app's own Stop does the same before its push.
aws_end_terminals() {
command -v tmux >/dev/null 2>&1 || return 0
- _triplec_as_user sh -c '
- d="${TMUX_TMPDIR:-/tmp}/tmux-$(id -u)"
- for s in "$d"/triple-c-*; do
- [ -S "$s" ] && tmux -L "${s##*/}" kill-server 2>/dev/null
- done
- i=0
- while pgrep -u "$(id -u)" -f "^tmux -L triple-c-" >/dev/null 2>&1 && [ $i -lt 50 ]; do
- sleep 0.1; i=$((i + 1))
- done
- exit 0'
+ local script
+ # >>> end_terminals (app/src-tauri/src/host/persistent.rs END_ALL_SCRIPT, verbatim)
+ script=$(cat <<'TRIPLEC_END_TERMINALS'
+d="${TMUX_TMPDIR:-/tmp}/tmux-$(id -u)"
+command -v tmux >/dev/null 2>&1 || exit 0
+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 [ $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
+TRIPLEC_END_TERMINALS
+)
+ # <<< end_terminals
+ _triplec_as_user sh -c "$script"
}
aws_before_stop() {
[ "${TRIPLEC_HOST:-}" = aws ] || return 0
diff --git a/container/triple-c-idle-watch b/container/triple-c-idle-watch
index bc6f13d..092ccb5 100755
--- a/container/triple-c-idle-watch
+++ b/container/triple-c-idle-watch
@@ -232,6 +232,38 @@ lifetime_reached() {
[ -n "$lifetime" ] && [ $(( $(date +%s) - started_at )) -ge $((lifetime * 60)) ]
}
+# Persistent terminals (the app's tmux servers, app/src-tauri/src/host/persistent.rs)
+# would keep writing after the push's snapshot: end them, as their user,
+# before a lifetime-cap push (the stop is certain then; an idle stop has no
+# live terminal output to lose). Same script as entrypoint.sh's.
+end_terminals() {
+ command -v tmux >/dev/null 2>&1 || return 0
+ local script
+ # >>> end_terminals (app/src-tauri/src/host/persistent.rs END_ALL_SCRIPT, verbatim)
+ script=$(cat <<'TRIPLEC_END_TERMINALS'
+d="${TMUX_TMPDIR:-/tmp}/tmux-$(id -u)"
+command -v tmux >/dev/null 2>&1 || exit 0
+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 [ $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
+TRIPLEC_END_TERMINALS
+)
+ # <<< end_terminals
+ as_user sh -c "$script"
+}
+
# The cap is reached: push (best effort) unless the app has decided already,
# record "lifetime", signal the entrypoint and end.
lifetime_stop() {
@@ -241,6 +273,7 @@ lifetime_stop() {
pushed|discard)
echo "triple-c-idle-watch: Triple-C is already stopping it — keeping its decision" ;;
*)
+ end_terminals
if [ "$wip_push" = 1 ]; then
echo "triple-c-idle-watch: pushing work in progress before the stop..."
as_user timeout -k 5 "$wip_timeout" "$wip_bin" --reason lifetime \