diff --git a/CLAUDE.md b/CLAUDE.md index f929ec7..ca0ad90 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -264,8 +264,8 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li - **`docker/`** — Docker API layer using bollard: - `client.rs` — Singleton Docker connection via `OnceLock` - `container.rs` — Container lifecycle (create, start, stop, remove, inspect) - - `exec.rs` — Attached exec streaming. `create_attached_exec()` is the **single** place an - attached exec is opened; terminal sessions and the auth bridge both go through it. + - `exec.rs` — Docker's implementation of everything `host::ContainerTarget` does inside a + container. `create_attached_exec_spec()` is the **single** place an attached exec is opened. - `image.rs` — Image build/pull with progress streaming - `gateway.rs` — Optional LiteLLM sibling container giving Claude Code an Anthropic-format front end for providers that only speak OpenAI (see `gateway-container/`). Mirrors `stt.rs`. @@ -282,6 +282,15 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li - `legacy_cleanup.rs` — One-release migration shim removing leftovers from the deleted MCP feature (containers labelled `triple-c.mcp-server`, `triple-c-net-*` networks). Deletable once users have migrated. +- **`host/`** — Where a project's container runs, and the only way into it. `ContainerTarget` + (one `Docker` variant today; AWS planned — `docs/superpowers/specs/2026-11-13-aws-ecs-host-design.md`) + wraps execs, file transfer and `is_running`; `sessions.rs` holds `ExecSessionManager`. + **Container-interior code must go through `ContainerTarget`, never `docker::exec`, bollard or + `get_docker()`** — `host::tests::no_container_interior_bollard_outside_the_host_layer` fails + the build on it. Its allowlist is Docker-only code (migration) plus lifecycle/`die` events, + which phase 1b moves; shrink it, never grow it. Prefer `ContainerTarget::for_project` or a + target a session already holds (`ExecSessionManager::get_target`) over + `ContainerTarget::docker(id)`, which hard-codes the host. - **`web_terminal/`** — Remote terminal access via axum HTTP+WebSocket server: - `server.rs` — Axum server lifecycle (start/stop), serves embedded HTML and handles WS upgrades - `ws_handler.rs` — Per-connection WebSocket handler with JSON protocol, session management, cleanup on disconnect diff --git a/app/src-tauri/src/commands/file_commands.rs b/app/src-tauri/src/commands/file_commands.rs index f28f7a3..1a472df 100644 --- a/app/src-tauri/src/commands/file_commands.rs +++ b/app/src-tauri/src/commands/file_commands.rs @@ -1435,17 +1435,6 @@ pub async fn create_container_directory( Ok(dest) } -/// The underlying cause of a [`ContainerTarget::attach`] failure, without the -/// host layer's own "Failed to create exec: " / "Failed to start exec: " -/// prefix, so the transfers below can keep framing it in their own words as -/// they did when they opened the exec themselves. Anything else (an -/// unreachable daemon, a detached start) passes through whole. -fn strip_exec_error_prefix(e: &str) -> &str { - e.strip_prefix("Failed to create exec: ") - .or_else(|| e.strip_prefix("Failed to start exec: ")) - .unwrap_or(e) -} - /// Every "container is not running" refusal starts with this, so a caller (the file /// viewer's poll, `app/src/viewer/ipcMessages.ts`) can tell it apart from any other failure. pub(crate) const NOT_RUNNING_PREFIX: &str = "Start the project before"; @@ -1630,7 +1619,7 @@ dd iflag=nonblock bs=64k status=none if="$TC_SRC" || exit 5"#; .no_stdin(), ) .await - .map_err(|e| format!("Failed to start download: {}", strip_exec_error_prefix(&e)))?; + .map_err(|e| format!("Failed to start download: {}", e))?; let exec_id = exec.exec_id; let mut output = exec.output; @@ -2310,10 +2299,7 @@ tar czf - --ignore-failed-read \ .no_stdin(), ) .await - .map_err(|e| match e.strip_prefix("Failed to create exec: ") { - Some(rest) => format!("Failed to create backup exec: {}", rest), - None => format!("Failed to start backup exec: {}", strip_exec_error_prefix(&e)), - })?; + .map_err(|e| format!("Failed to start backup: {}", e))?; let exec_id = exec.exec_id; let mut output = exec.output; diff --git a/app/src-tauri/src/commands/inspect_commands.rs b/app/src-tauri/src/commands/inspect_commands.rs index a637fc6..be6c81c 100644 --- a/app/src-tauri/src/commands/inspect_commands.rs +++ b/app/src-tauri/src/commands/inspect_commands.rs @@ -1287,14 +1287,10 @@ pub async fn run_scheduled_task_now( ]) .env(home_env()) .no_stdin(); - let exec_id = target.exec_detached(spec).await.map_err(|e| { - // Keep this command's own wording for a start failure ("Failed to start - // task: …"); a create failure reads "Failed to create exec: …" either way. - match e.strip_prefix("Failed to start exec: ") { - Some(rest) => format!("Failed to start task: {}", rest), - None => e, - } - })?; + let exec_id = target + .exec_detached(spec) + .await + .map_err(|e| format!("Failed to start task: {}", e))?; log::info!( "Triggered scheduler task {} in project {} (detached exec {})", diff --git a/app/src-tauri/src/commands/terminal_commands.rs b/app/src-tauri/src/commands/terminal_commands.rs index 3cab350..a1ddfd7 100644 --- a/app/src-tauri/src/commands/terminal_commands.rs +++ b/app/src-tauri/src/commands/terminal_commands.rs @@ -239,7 +239,7 @@ pub async fn paste_image_to_terminal( image_data: Vec, state: State<'_, AppState>, ) -> Result { - let target = ContainerTarget::docker(state.exec_manager.get_container_id(&session_id).await?); + let target = state.exec_manager.get_target(&session_id).await?; let timestamp = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) @@ -282,7 +282,7 @@ pub async fn upload_host_file_to_terminal( let base = crate::commands::file_commands::host_upload_name(&host_path)?; let host_path = crate::commands::file_commands::resolve_host_read_path(&host_path).await?; - let target = ContainerTarget::docker(state.exec_manager.get_container_id(&session_id).await?); + let target = state.exec_manager.get_target(&session_id).await?; let meta = tokio::fs::metadata(&host_path) .await @@ -345,7 +345,7 @@ pub async fn start_audio_bridge( state: State<'_, AppState>, ) -> Result<(), String> { // Get the container from the terminal session - let target = ContainerTarget::docker(state.exec_manager.get_container_id(&session_id).await?); + let target = state.exec_manager.get_target(&session_id).await?; // Create audio bridge exec session with ID "audio-{session_id}" // The loop handles reconnection when the FIFO reader (fake rec) is killed and restarted diff --git a/app/src-tauri/src/docker/exec.rs b/app/src-tauri/src/docker/exec.rs index 0e4caac..3a7ab60 100644 --- a/app/src-tauri/src/docker/exec.rs +++ b/app/src-tauri/src/docker/exec.rs @@ -5,40 +5,20 @@ use futures_util::StreamExt; use super::client::get_docker; use crate::host::{ExecChunk, ExecSpec}; -/// A `docker exec` that has been created and started with stdin/stdout/stderr -/// attached — the raw duplex halves, before any policy about what to do with -/// them. -/// -/// This is the single place in the codebase that knows how to open an attached -/// exec. Both consumers are built on it: -/// * [`ExecSessionManager`] — interactive terminals and the audio bridge, -/// which pump bytes through mpsc channels and a callback. -/// * `auth_bridge` — per-connection `socat` tunnels, which pump bytes -/// straight between a host TCP socket and these halves. -/// -/// With `tty = false` the output stream is demultiplexed by Docker, so the -/// consumer can tell [`LogOutput::StdOut`] from [`LogOutput::StdErr`]. That -/// distinction matters for the auth bridge: `socat`'s diagnostics must not be -/// spliced into the proxied byte stream. +// `AttachedExec` lives in `host` now (host-neutral output chunks); this +// module's job is to produce one from Docker. Its consumers are +// `host::sessions::ExecSessionManager` (terminals, audio bridge) and the auth +// bridge's per-connection `socat` tunnels, both through `host::ContainerTarget`. +// With `tty = false` Docker demultiplexes stdout from stderr, which the auth +// bridge relies on: `socat`'s diagnostics must not be spliced into the proxied +// byte stream. pub use crate::host::AttachedExec; -/// Create and start an exec with stdin + stdout + stderr attached, returning the -/// raw duplex halves. Runs as `claude` in `/workspace`, like every other exec -/// this app opens. -pub async fn create_attached_exec( - container_id: &str, - cmd: Vec, - tty: bool, -) -> Result { - create_attached_exec_as(container_id, cmd, tty, "claude", "/workspace").await -} - -/// [`create_attached_exec`] with the user and working directory spelled out. +/// An attached exec with the user and working directory spelled out. /// -/// Only base-image migration needs this: replaying `apt` and unpacking a -/// payload tar at `/` have to run as **root**, and every other caller wants the -/// `claude` / `/workspace` defaults that [`create_attached_exec`] supplies. It -/// stays the single place an attached exec is opened. +/// Only base-image migration calls this directly: replaying `apt` and unpacking +/// a payload tar at `/` have to run as **root**. Everything else goes through +/// `host::ContainerTarget::attach`. pub async fn create_attached_exec_as( container_id: &str, cmd: Vec, @@ -54,8 +34,8 @@ pub async fn create_attached_exec_as( } /// The single place an attached exec is opened on Docker. Everything else — -/// [`create_attached_exec`], [`create_attached_exec_as`], and -/// `host::ContainerTarget::attach` — is a thin wrapper over this. +/// [`create_attached_exec_as`] and `host::ContainerTarget::attach` — is a thin +/// wrapper over this. pub async fn create_attached_exec_spec( container_id: &str, spec: &ExecSpec, diff --git a/app/src-tauri/src/host/mod.rs b/app/src-tauri/src/host/mod.rs index 5a1f4ab..1350610 100644 --- a/app/src-tauri/src/host/mod.rs +++ b/app/src-tauri/src/host/mod.rs @@ -29,7 +29,7 @@ pub mod sessions; pub use crate::docker::exec::{ - MAX_DROP_BYTES, MAX_ONESHOT_OUTPUT, OUTPUT_LIMIT_MARKER, PROC_NET_OUTPUT_LIMIT, + MAX_DROP_BYTES, OUTPUT_LIMIT_MARKER, PROC_NET_OUTPUT_LIMIT, }; pub use sessions::ExecSessionManager; @@ -434,22 +434,6 @@ mod tests { "commands/migration_commands.rs", // Phase 1b: lifecycle and the `die` event stream. "container_watch.rs", - // Phase 1a, not yet migrated. Each migration removes its own - // entries; the list must be empty before phase 1a merges. - "auth_bridge/mod.rs", - "auth_bridge/tunnel.rs", - "browser_view/detect.rs", - "browser_view/install.rs", - "browser_view/mod.rs", - "browser_view/page.rs", - "commands/auth_token_commands.rs", - "commands/file_commands.rs", - "commands/inspect_commands.rs", - "commands/terminal_commands.rs", - "file_viewer/poll.rs", - "file_viewer/resolve.rs", - "marketplace/gh_login.rs", - "marketplace/sync.rs", ]; let src = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); let mut offenders = Vec::new(); diff --git a/app/src-tauri/src/host/sessions.rs b/app/src-tauri/src/host/sessions.rs index acdcbba..38e977e 100644 --- a/app/src-tauri/src/host/sessions.rs +++ b/app/src-tauri/src/host/sessions.rs @@ -187,12 +187,15 @@ impl ExecSessionManager { } } - pub async fn get_container_id(&self, session_id: &str) -> Result { + /// The container a session was opened on — reuse it for side-channel work + /// on the same session (image paste, file drop, audio bridge) rather than + /// rebuilding a target from an id, which would assume the host. + pub async fn get_target(&self, session_id: &str) -> Result { let sessions = self.sessions.lock().await; let session = sessions .get(session_id) .ok_or_else(|| format!("Session {} not found", session_id))?; - Ok(session.target.id().to_string()) + Ok(session.target.clone()) } pub async fn write_file_to_container( diff --git a/app/src-tauri/src/web_terminal/ws_handler.rs b/app/src-tauri/src/web_terminal/ws_handler.rs index 1782381..8d60578 100644 --- a/app/src-tauri/src/web_terminal/ws_handler.rs +++ b/app/src-tauri/src/web_terminal/ws_handler.rs @@ -295,9 +295,7 @@ async fn handle_open( return Err(format!("Project '{}' is not running", project.name)); } - let container_id = project - .container_id - .as_ref() + let container = crate::host::ContainerTarget::for_project(&project) .ok_or_else(|| "Container not running".to_string())?; let cmd = match session_type { @@ -330,7 +328,7 @@ async fn handle_open( state .exec_manager .create_session( - &crate::host::ContainerTarget::docker(container_id.as_str()), + &container, &session_id, cmd, on_output,