fix(orchestrator): la délégation simple vers UX renvoie son Final structuré (#207, QA verte)

Une demande idea_ask_agent vers UX échouait avec « finished without a structured Final response » alors qu'Architect répondait. Deux causes corrigées:

- session/process: un tour CLI terminé en code non nul SANS stdout (ex. erreur 401/auth) renvoyait une sortie vide au lieu du stderr réel; l'orchestrateur concluait alors à un « no Final ». On remonte désormais le stderr effectif (drain sync + async + helper process_exit_message + test dédié).

- orchestrator/service: la branche « ask waiting on background » masquait le premier Final structuré; la réponse synchrone redevient le premier Final (suppression de active_rendezvous_has_background_tasks / mark_rendezvous_task_waiting).

Nettoyage runtime corollaire (#204): arrêt du live runtime stale (PTY + structuré) d'un agent recréé afin que la délégation route vers l'agent courant (LiveSessions, stop_live_agent_runtime_in_project, wiring backend via DeleteAgent::with_runtime_cleanup, lifecycle, workstate cascade).
This commit is contained in:
2026-08-12 14:09:06 +02:00
parent ba176e3cf2
commit 646b9d9284
11 changed files with 673 additions and 203 deletions

View File

@ -32,7 +32,7 @@ use std::sync::mpsc::Sender;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::process::Command;
use domain::ports::AgentSessionError;
@ -219,7 +219,7 @@ fn drain_sandboxed(
killer_tx: tokio::sync::oneshot::Sender<Arc<std::sync::Mutex<std::process::Child>>>,
line_tap: Option<Sender<String>>,
) -> Result<Vec<String>, AgentSessionError> {
use std::io::{BufRead, BufReader as StdBufReader, Write as _};
use std::io::{BufRead, BufReader as StdBufReader, Read as _, Write as _};
use std::process::{Command as StdCommand, Stdio};
use std::sync::Mutex as StdMutex;
@ -266,6 +266,17 @@ fn drain_sandboxed(
.stdout
.take()
.ok_or_else(|| AgentSessionError::Io("stdout pipe indisponible".to_owned()))?;
let stderr = child
.stderr
.take()
.ok_or_else(|| AgentSessionError::Io("stderr pipe indisponible".to_owned()))?;
let stderr_handle = std::thread::spawn(move || {
let mut bytes = Vec::new();
StdBufReader::new(stderr)
.read_to_end(&mut bytes)
.map(|_| bytes)
.map_err(|e| AgentSessionError::Io(e.to_string()))
});
let child = Arc::new(StdMutex::new(child));
// Donne au côté async de quoi tuer l'enfant en cas de timeout.
let _ = killer_tx.send(Arc::clone(&child));
@ -285,8 +296,20 @@ fn drain_sandboxed(
}
// 6. Reap (pas de zombie). Lock tenu brièvement.
if let Ok(mut c) = child.lock() {
let _ = c.wait();
let status = child
.lock()
.map_err(|_| AgentSessionError::Io("child lock empoisonné".to_owned()))?
.wait()
.map_err(|e| AgentSessionError::Io(e.to_string()))?;
let stderr_bytes = stderr_handle
.join()
.map_err(|_| AgentSessionError::Io("stderr thread terminé sans résultat".to_owned()))??;
if !status.success() && collected.is_empty() {
return Err(AgentSessionError::Io(process_exit_message(
&command,
status.code(),
&stderr_bytes,
)));
}
Ok(collected)
}
@ -333,6 +356,14 @@ async fn drain(
.stdout
.take()
.ok_or_else(|| AgentSessionError::Io("stdout pipe indisponible".to_owned()))?;
let mut stderr = child
.stderr
.take()
.ok_or_else(|| AgentSessionError::Io("stderr pipe indisponible".to_owned()))?;
let stderr_task = tokio::spawn(async move {
let mut bytes = Vec::new();
stderr.read_to_end(&mut bytes).await.map(|_| bytes)
});
let mut lines = BufReader::new(stdout).lines();
let mut collected = Vec::new();
@ -350,9 +381,21 @@ async fn drain(
}
// Réifie le statut du process pour ne pas laisser de zombie ; un statut non nul
// n'est pas en soi une erreur de session (la CLI peut signaler une erreur
// *dans* sa sortie structurée, captée par le parser).
let _ = child.wait().await.map_err(map_io)?;
// n'est pas en soi une erreur de session si la CLI a imprimé une sortie
// structurée, captée ensuite par le parser. Sans stdout, il n'y a rien à parser :
// on remonte le stderr réel au lieu de laisser l'appelant conclure à un no-Final.
let status = child.wait().await.map_err(map_io)?;
let stderr_bytes = stderr_task
.await
.map_err(|e| AgentSessionError::Io(format!("lecture stderr: {e}")))?
.map_err(map_io)?;
if !status.success() && collected.is_empty() {
return Err(AgentSessionError::Io(process_exit_message(
&spec.command,
status.code(),
&stderr_bytes,
)));
}
Ok(collected)
}
@ -360,3 +403,14 @@ async fn drain(
fn map_io(e: io::Error) -> AgentSessionError {
AgentSessionError::Io(e.to_string())
}
fn process_exit_message(command: &str, code: Option<i32>, stderr: &[u8]) -> String {
let status = code.map_or_else(|| "signal".to_owned(), |code| format!("code {code}"));
let stderr = String::from_utf8_lossy(stderr);
let stderr = stderr.trim();
if stderr.is_empty() {
format!("{command} exited with {status} without structured output")
} else {
format!("{command} exited with {status}: {stderr}")
}
}