diff --git a/rustcode/engine/src/acp/builtin.rs b/rustcode/engine/src/acp/builtin.rs index a7b13f6db..835caee96 100644 --- a/rustcode/engine/src/acp/builtin.rs +++ b/rustcode/engine/src/acp/builtin.rs @@ -237,3 +237,40 @@ pub(crate) struct Published { /// A prompt is queued and no turn runs, so the turn loop should start. pub(crate) queued_while_idle: bool, } + +/// A scheduled job's prompt, which the agent runs headless in a daemon +/// session of its own. +pub(crate) struct ScheduledPrompt { + pub(crate) prompt: String, + pub(crate) workspace: PathBuf, + pub(crate) model_profile: Option, + /// The thread the job was recorded from: an input, never written to. + pub(crate) session: String, + pub(crate) settings: Option, +} + +/// Why a scheduled prompt did not finish, by whether running it again can +/// help. +pub(crate) enum ScheduledPromptError { + Permanent(String), + Transient(String), + /// The turn may have had effects before it failed. + Ambiguous(String), +} + +/// What the engine runs on the built-in agent outside an ACP thread. +pub(crate) trait BuiltinRuntime: Send + Sync { + /// Run a scheduled job's prompt to its end and return the reply. + fn run_scheduled( + &self, + prompt: ScheduledPrompt, + cancellation: tokio_util::sync::CancellationToken, + ) -> AgentFuture<'static, Result>; +} + +/// The built-in agent's runtime. The agent lives in this crate until ADR +/// 0008's last step, so this is its headless runner; after that the binary +/// registers it at startup. +pub(crate) fn runtime() -> &'static dyn BuiltinRuntime { + &crate::raw_cli::HeadlessRuntime +} diff --git a/rustcode/engine/src/daemon/executor.rs b/rustcode/engine/src/daemon/executor.rs index 6fcb34b0c..4ae940f3e 100644 --- a/rustcode/engine/src/daemon/executor.rs +++ b/rustcode/engine/src/daemon/executor.rs @@ -436,47 +436,28 @@ impl ActionBackend for ProductionActions { settings, .. } => { - let mut state = match crate::raw_cli::build_scheduled_state( - &prompt, - std::path::Path::new(&workspace), - model_profile.as_deref(), - &session, - settings.as_ref(), - ) { - Ok(state) => state, - Err(error) - if error.starts_with("unknown model profile:") - || error.starts_with("recorded MCP credential unavailable:") => - { - return transient(error); - } - Err(error) => return permanent(error), + let job = crate::acp::builtin::ScheduledPrompt { + prompt, + workspace: std::path::PathBuf::from(workspace), + model_profile, + session, + settings, }; - // The recorded session is an input. Persist daemon output in - // a separate session, never over the interactive transcript. - static NEXT_SESSION: std::sync::atomic::AtomicU64 = - std::sync::atomic::AtomicU64::new(0); - state.active_session_id = format!( - "daemon-{}-{}-{}", - std::process::id(), - chrono::Utc::now().timestamp_micros(), - NEXT_SESSION.fetch_add(1, std::sync::atomic::Ordering::Relaxed) - ); - match crate::raw_cli::run_scheduled_turn( - state, - std::path::Path::new(&workspace), - cancellation, - ) - .await + match crate::acp::builtin::runtime() + .run_scheduled(job, cancellation) + .await { Ok(output) => RunOutcome::Succeeded { summary: "Headless turn completed".into(), output: Some(output), }, - Err(crate::raw_cli::ScheduledTurnError::Transient(error)) => { + Err(crate::acp::builtin::ScheduledPromptError::Permanent(error)) => { + permanent(error) + } + Err(crate::acp::builtin::ScheduledPromptError::Transient(error)) => { transient(error) } - Err(crate::raw_cli::ScheduledTurnError::Ambiguous(error)) => { + Err(crate::acp::builtin::ScheduledPromptError::Ambiguous(error)) => { ambiguous(error, None) } } diff --git a/rustcode/engine/src/raw_cli.rs b/rustcode/engine/src/raw_cli.rs index 71e28f82a..2f368c4d1 100644 --- a/rustcode/engine/src/raw_cli.rs +++ b/rustcode/engine/src/raw_cli.rs @@ -274,6 +274,58 @@ pub(crate) async fn run_scheduled_turn( .await } +/// The built-in agent's headless runner, as [`crate::acp::builtin::BuiltinRuntime`]. +pub(crate) struct HeadlessRuntime; + +impl crate::acp::builtin::BuiltinRuntime for HeadlessRuntime { + fn run_scheduled( + &self, + job: crate::acp::builtin::ScheduledPrompt, + cancellation: tokio_util::sync::CancellationToken, + ) -> crate::acp::builtin::AgentFuture< + 'static, + Result, + > { + Box::pin(run_scheduled_prompt(job, cancellation)) + } +} + +async fn run_scheduled_prompt( + job: crate::acp::builtin::ScheduledPrompt, + cancellation: tokio_util::sync::CancellationToken, +) -> Result { + use crate::acp::builtin::ScheduledPromptError; + let mut state = build_scheduled_state( + &job.prompt, + &job.workspace, + job.model_profile.as_deref(), + &job.session, + job.settings.as_ref(), + ) + .map_err(|error| { + if error.starts_with("unknown model profile:") + || error.starts_with("recorded MCP credential unavailable:") + { + ScheduledPromptError::Transient(error) + } else { + ScheduledPromptError::Permanent(error) + } + })?; + static NEXT_SESSION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + state.active_session_id = format!( + "daemon-{}-{}-{}", + std::process::id(), + chrono::Utc::now().timestamp_micros(), + NEXT_SESSION.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + ); + run_scheduled_turn(state, &job.workspace, cancellation) + .await + .map_err(|error| match error { + ScheduledTurnError::Transient(error) => ScheduledPromptError::Transient(error), + ScheduledTurnError::Ambiguous(error) => ScheduledPromptError::Ambiguous(error), + }) +} + pub(crate) enum ScheduledTurnError { Transient(String), Ambiguous(String), diff --git a/scripts/comment-baseline.txt b/scripts/comment-baseline.txt index e35b676cd..ab6d66c27 100644 --- a/scripts/comment-baseline.txt +++ b/scripts/comment-baseline.txt @@ -792,7 +792,7 @@ 21 rustcode/engine/src/controller/tests.rs 12 rustcode/engine/src/controller/worker.rs 7 rustcode/engine/src/daemon/action_tests.rs -24 rustcode/engine/src/daemon/executor.rs +22 rustcode/engine/src/daemon/executor.rs 1 rustcode/engine/src/daemon/hub_run.rs 12 rustcode/engine/src/daemon/lifecycle.rs 1 rustcode/engine/src/daemon/mod.rs