From bfbbf4463164f5585dcf7c407556fd1acd84d57f Mon Sep 17 00:00:00 2001 From: Lifei Zhou Date: Tue, 22 Sep 2026 08:13:49 +0000 Subject: [PATCH] fix(acp): prevent duplicate schedules from overwriting recipes (#12434) --- crates/goose/src/acp/server/schedule.rs | 97 +++++++++++++++++++------ crates/goose/src/scheduler.rs | 1 + 2 files changed, 75 insertions(+), 23 deletions(-) diff --git a/crates/goose/src/acp/server/schedule.rs b/crates/goose/src/acp/server/schedule.rs index c73c7d7f3..f4f614485 100644 --- a/crates/goose/src/acp/server/schedule.rs +++ b/crates/goose/src/acp/server/schedule.rs @@ -6,12 +6,11 @@ use goose_sdk_types::custom_requests::{ RunScheduleNowResponse, RunScheduleNowStatus, ScheduledJobDto, UnpauseScheduleRequest, UpdateScheduleRequest, UpdateScheduleResponse, }; -use tokio::fs; use super::{build_session_info, GooseAcpAgent, ResultExt}; use crate::recipe::validate_recipe::validate_recipe_template_from_content; use crate::recipe::Recipe; -use crate::scheduler::{get_default_scheduled_recipes_dir, ScheduledJob, SchedulerError}; +use crate::scheduler::{ScheduledJob, SchedulerError, ValidatedScheduleRecipe}; use crate::scheduler_trait::SchedulerTrait; use std::sync::Arc; @@ -30,7 +29,7 @@ fn validate_schedule_id(id: &str) -> Result<(), agent_client_protocol::Error> { Ok(()) } -fn validate_schedule_recipe(recipe: &Recipe) -> Result<(), agent_client_protocol::Error> { +fn validate_schedule_recipe(recipe: &Recipe) -> Result { let recipe_yaml = recipe .to_yaml() .map_err(|e| agent_client_protocol::Error::invalid_params().data(e.to_string()))?; @@ -38,7 +37,7 @@ fn validate_schedule_recipe(recipe: &Recipe) -> Result<(), agent_client_protocol validate_recipe_template_from_content(&recipe_yaml, None) .map_err(|e| agent_client_protocol::Error::invalid_params().data(e.to_string()))?; - Ok(()) + Ok(recipe_yaml) } fn schedule_not_found_or_internal(error: SchedulerError) -> agent_client_protocol::Error { @@ -180,26 +179,12 @@ impl GooseAcpAgent { "This recipe contains hidden characters that could be malicious. Please remove them before trying to save.", )); } - validate_schedule_recipe(&recipe)?; - - let scheduled_recipes_dir = get_default_scheduled_recipes_dir().map_err(|e| { - agent_client_protocol::Error::internal_error() - .data(format!("Failed to get scheduled recipes directory: {e}")) - })?; - - let recipe_path = scheduled_recipes_dir.join(format!("{id}.yaml")); - let yaml_content = recipe.to_yaml().map_err(|e| { - agent_client_protocol::Error::internal_error() - .data(format!("Failed to convert recipe to YAML: {e}")) - })?; - fs::write(&recipe_path, yaml_content).await.map_err(|e| { - agent_client_protocol::Error::internal_error() - .data(format!("Failed to save recipe file: {e}")) - })?; + let yaml_content = validate_schedule_recipe(&recipe)?; + let recipe_source = format!("{id}.yaml"); let job = ScheduledJob { - id, - source: recipe_path.to_string_lossy().into_owned(), + id: id.clone(), + source: recipe_source.clone(), cron: req.cron, last_run: None, currently_running: false, @@ -211,10 +196,23 @@ impl GooseAcpAgent { }; scheduler - .add_scheduled_job(job.clone(), false) + .add_scheduled_job_with_recipe( + job, + ValidatedScheduleRecipe::new(yaml_content.into_bytes(), recipe_source.into()), + ) .await .map_err(create_schedule_error)?; + let job = scheduler + .list_scheduled_jobs() + .await + .into_iter() + .find(|job| job.id == id) + .ok_or_else(|| { + agent_client_protocol::Error::internal_error() + .data("Schedule not found after creation") + })?; + Ok(CreateScheduleResponse { job: scheduled_job_to_dto(job), }) @@ -347,6 +345,7 @@ mod tests { use crate::acp::server::AcpBuiltinSelection; use crate::acp::server_factory::{AcpServer, AcpServerFactoryConfig}; use crate::agents::GoosePlatform; + use crate::scheduler::get_default_scheduled_recipes_dir; use goose_sdk_types::custom_requests::{ListRecipesRequest, ScheduleRecipeRequest}; use serial_test::serial; @@ -361,6 +360,58 @@ mod tests { ); } + fn create_schedule_request(id: &str, prompt: &str) -> CreateScheduleRequest { + let recipe = Recipe::builder() + .title(format!("{id} recipe")) + .description("Schedule overwrite regression test") + .prompt(prompt) + .build() + .unwrap(); + + CreateScheduleRequest { + id: id.to_string(), + recipe: recipe.try_into().unwrap(), + cron: "0 0 0 * * *".to_string(), + } + } + + #[tokio::test] + #[serial] + async fn duplicate_schedule_does_not_overwrite_existing_recipe() { + let root = tempfile::tempdir().unwrap(); + let _guard = env_lock::lock_env([ + ("GOOSE_DISABLE_KEYRING", Some("true")), + ("GOOSE_PATH_ROOT", root.path().to_str()), + ]); + let server = AcpServer::new(AcpServerFactoryConfig { + builtins: AcpBuiltinSelection::default(), + config_dir: root.path().join("config"), + goose_platform: GoosePlatform::GooseCli, + additional_source_roots: Vec::new(), + session_cwd: None, + enable_scheduler: true, + }); + let agent = server.create_agent().await.unwrap(); + + let created = agent + .on_create_schedule(create_schedule_request("nightly", "original prompt")) + .await + .unwrap(); + let recipe_path = created.job.source; + let original_recipe = std::fs::read(&recipe_path).unwrap(); + + let error = agent + .on_create_schedule(create_schedule_request("nightly", "replacement prompt")) + .await + .expect_err("duplicate schedule must be rejected"); + + assert_eq!( + error.code, + agent_client_protocol::Error::invalid_params().code + ); + assert_eq!(std::fs::read(recipe_path).unwrap(), original_recipe); + } + #[tokio::test] #[serial] async fn disabled_scheduler_rejects_schedule_operations_without_recipe_writes() { diff --git a/crates/goose/src/scheduler.rs b/crates/goose/src/scheduler.rs index 846e75c12..91cf0b6df 100644 --- a/crates/goose/src/scheduler.rs +++ b/crates/goose/src/scheduler.rs @@ -492,6 +492,7 @@ impl Scheduler { write_schedule_recipe_bytes(&destination_recipe_path, &recipe_bytes)?; stored_job.recipe_base_dir = original_recipe_path .parent() + .filter(|parent| !parent.as_os_str().is_empty()) .map(|p| p.to_string_lossy().into_owned()); stored_job.source = destination_recipe_path.to_string_lossy().into_owned(); stored_job.current_session_id = None;