Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/cli-sub-agent/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ async fn execute_task(
extra_env_ref,
Some("batch"),
None, // batch does not use tier-based selection
csa_process::StreamMode::BufferOnly,
)
.await;

Expand Down
1 change: 1 addition & 0 deletions crates/cli-sub-agent/src/claude_sub_agent_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub(crate) async fn handle_claude_sub_agent(
extra_env,
Some("run"),
None, // claude-sub-agent does not use tier-based selection
csa_process::StreamMode::BufferOnly,
)
.await?;

Expand Down
4 changes: 4 additions & 0 deletions crates/cli-sub-agent/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ pub enum Commands {
/// Block-wait for a free slot instead of failing when all slots are occupied
#[arg(long)]
wait: bool,

/// Stream child stdout to stderr in real-time (prefix: [stdout])
#[arg(long)]
stream_stdout: bool,
},

/// Manage sessions
Expand Down
1 change: 1 addition & 0 deletions crates/cli-sub-agent/src/debate_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub(crate) async fn handle_debate(args: DebateArgs, current_depth: u32) -> Resul
extra_env,
Some("debate"),
None, // debate does not use tier-based selection
csa_process::StreamMode::BufferOnly,
)
.await?;

Expand Down
22 changes: 21 additions & 1 deletion crates/cli-sub-agent/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::IsTerminal;

use anyhow::Result;
use clap::Parser;
use tempfile::TempDir;
Expand Down Expand Up @@ -71,7 +73,17 @@ async fn main() -> Result<()> {
thinking,
no_failover,
wait,
stream_stdout,
} => {
// Determine stream mode: explicit flag > auto-detect (text format + stderr is TTY)
let stream_mode = if stream_stdout
|| (matches!(output_format, OutputFormat::Text) && std::io::stderr().is_terminal())
{
csa_process::StreamMode::TeeToStderr
} else {
csa_process::StreamMode::BufferOnly
};

let exit_code = handle_run(
tool,
prompt,
Expand All @@ -88,6 +100,7 @@ async fn main() -> Result<()> {
wait,
current_depth,
output_format,
stream_mode,
)
.await?;
std::process::exit(exit_code);
Expand Down Expand Up @@ -283,6 +296,7 @@ async fn handle_run(
wait: bool,
current_depth: u32,
output_format: OutputFormat,
stream_mode: csa_process::StreamMode,
) -> Result<i32> {
// 1. Determine project root
let project_root = pipeline::determine_project_root(cd.as_deref())?;
Expand Down Expand Up @@ -556,7 +570,12 @@ async fn handle_run(
let temp_dir = TempDir::new()?;
info!("Ephemeral session in: {:?}", temp_dir.path());
executor
.execute_in(&prompt_text, temp_dir.path(), extra_env.as_ref())
.execute_in(
&prompt_text,
temp_dir.path(),
extra_env.as_ref(),
stream_mode,
)
.await?
} else {
// Persistent session
Expand All @@ -572,6 +591,7 @@ async fn handle_run(
extra_env.as_ref(),
Some("run"),
resolved_tier_name.as_deref(),
stream_mode,
)
.await
{
Expand Down
8 changes: 7 additions & 1 deletion crates/cli-sub-agent/src/mcp_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,12 @@ async fn handle_run_tool(args: Value) -> Result<Value> {
// Ephemeral: use temp directory
let temp_dir = TempDir::new()?;
executor
.execute_in(prompt, temp_dir.path(), extra_env_ref)
.execute_in(
prompt,
temp_dir.path(),
extra_env_ref,
csa_process::StreamMode::BufferOnly,
)
.await?
} else {
// Persistent session
Expand All @@ -590,6 +595,7 @@ async fn handle_run_tool(args: Value) -> Result<Value> {
extra_env_ref,
Some("run"),
None, // MCP server does not use tier-based selection
csa_process::StreamMode::BufferOnly,
)
.await?
};
Expand Down
5 changes: 4 additions & 1 deletion crates/cli-sub-agent/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ pub(crate) async fn execute_with_session(
extra_env: Option<&std::collections::HashMap<String, String>>,
task_type: Option<&str>,
tier_name: Option<&str>,
stream_mode: csa_process::StreamMode,
) -> Result<ExecutionResult> {
let execution = execute_with_session_and_meta(
executor,
Expand All @@ -220,6 +221,7 @@ pub(crate) async fn execute_with_session(
extra_env,
task_type,
tier_name,
stream_mode,
)
.await?;

Expand All @@ -239,6 +241,7 @@ pub(crate) async fn execute_with_session_and_meta(
extra_env: Option<&std::collections::HashMap<String, String>>,
task_type: Option<&str>,
tier_name: Option<&str>,
stream_mode: csa_process::StreamMode,
) -> Result<SessionExecutionResult> {
// Check for parent session violation: a child process must not operate on its own session
if let Some(ref session_id) = session_arg {
Expand Down Expand Up @@ -395,7 +398,7 @@ pub(crate) async fn execute_with_session_and_meta(
let mut sigint = signal(SignalKind::interrupt()).context("Failed to install SIGINT handler")?;

// Wait for either child completion or signal
let wait_future = csa_process::wait_and_capture(child);
let wait_future = csa_process::wait_and_capture(child, stream_mode);
tokio::pin!(wait_future);

let result = tokio::select! {
Expand Down
1 change: 1 addition & 0 deletions crates/cli-sub-agent/src/review_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ async fn execute_review(
extra_env,
Some("review"),
None,
csa_process::StreamMode::BufferOnly,
)
.await
}
Expand Down
7 changes: 6 additions & 1 deletion crates/csa-executor/src/agent_backend_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ impl AgentSession for ExecutorAgentSession {

match self
.executor
.execute_in(&prompt, &self.cwd, extra_env)
.execute_in(
&prompt,
&self.cwd,
extra_env,
csa_process::StreamMode::BufferOnly,
)
.await
{
Ok(result) => {
Expand Down
6 changes: 4 additions & 2 deletions crates/csa-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ impl Executor {
tool_state: Option<&ToolState>,
session: &MetaSessionState,
extra_env: Option<&HashMap<String, String>>,
stream_mode: csa_process::StreamMode,
) -> Result<ExecutionResult> {
let (cmd, stdin_data) = self.build_command(prompt, tool_state, session, extra_env);
csa_process::run_and_capture_with_stdin(cmd, stdin_data).await
csa_process::run_and_capture_with_stdin(cmd, stdin_data, stream_mode).await
}

/// Execute in a specific directory (for ephemeral sessions).
Expand All @@ -220,6 +221,7 @@ impl Executor {
prompt: &str,
work_dir: &Path,
extra_env: Option<&HashMap<String, String>>,
stream_mode: csa_process::StreamMode,
) -> Result<ExecutionResult> {
let mut cmd = Command::new(self.executable_name());
cmd.current_dir(work_dir);
Expand All @@ -242,7 +244,7 @@ impl Executor {
} else {
self.append_prompt_args_with_transport(&mut cmd, prompt, prompt_transport);
}
csa_process::run_and_capture_with_stdin(cmd, stdin_data).await
csa_process::run_and_capture_with_stdin(cmd, stdin_data, stream_mode).await
}

/// Build base command with session environment variables.
Expand Down
Loading
Loading