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
37 changes: 37 additions & 0 deletions rustcode/engine/src/acp/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// The thread the job was recorded from: an input, never written to.
pub(crate) session: String,
pub(crate) settings: Option<crate::sessions::SessionSettingsSnapshot>,
}

/// 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<String, ScheduledPromptError>>;
}

/// 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
}
47 changes: 14 additions & 33 deletions rustcode/engine/src/daemon/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
52 changes: 52 additions & 0 deletions rustcode/engine/src/raw_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, crate::acp::builtin::ScheduledPromptError>,
> {
Box::pin(run_scheduled_prompt(job, cancellation))
}
}

async fn run_scheduled_prompt(
job: crate::acp::builtin::ScheduledPrompt,
cancellation: tokio_util::sync::CancellationToken,
) -> Result<String, crate::acp::builtin::ScheduledPromptError> {
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),
Expand Down
2 changes: 1 addition & 1 deletion scripts/comment-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading