test(scheduler): guard recipe-before-inference ordering on scheduled runs (#11502)
Co-authored-by: Douwe Osinga <douwe.osinga@gmail.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use anyhow::Result;
|
||||
use serde_json::json;
|
||||
|
||||
use super::dummy_api::ProviderFeatures;
|
||||
use super::dummy_api::{DummyApi, ProviderFeatures};
|
||||
use super::pipeline::{
|
||||
test_pipeline, test_pipeline_with, test_pipeline_with_scheduler, MessageKind::Agent,
|
||||
MessageKind::Error, MessageKind::ToolResponse,
|
||||
@@ -490,3 +490,100 @@ async fn boolean_final_output_schema_stops_before_inference() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn scheduled_run_attaches_recipe_to_session_before_inference() -> Result<()> {
|
||||
let api = DummyApi::start(ProviderFeatures::default()).await;
|
||||
let gate = api
|
||||
.on("Reply while the response gate is held.")
|
||||
.hold_reply("done");
|
||||
let host = api.uri();
|
||||
let _guard = env_lock::lock_env([
|
||||
("GOOSE_PROVIDER", Some("openai")),
|
||||
("GOOSE_MODEL", Some("gpt-4o")),
|
||||
("OPENAI_API_KEY", Some("fake-openai-no-keyring")),
|
||||
("OPENAI_HOST", Some(host.as_str())),
|
||||
("OPENAI_CUSTOM_HEADERS", Some("")),
|
||||
]);
|
||||
|
||||
let temp_dir = tempfile::tempdir()?;
|
||||
let recipe_path = temp_dir.path().join("scheduled.yaml");
|
||||
std::fs::write(
|
||||
&recipe_path,
|
||||
r#"version: 1.0.0
|
||||
title: Ordering guard
|
||||
description: Scheduled recipe with a sub-recipe
|
||||
prompt: Reply while the response gate is held.
|
||||
extensions: []
|
||||
sub_recipes:
|
||||
- name: check_calendar
|
||||
path: ./check_calendar.yaml
|
||||
"#,
|
||||
)?;
|
||||
std::fs::write(
|
||||
temp_dir.path().join("check_calendar.yaml"),
|
||||
r#"version: 1.0.0
|
||||
title: Check calendar
|
||||
description: Sub-recipe
|
||||
prompt: check
|
||||
"#,
|
||||
)?;
|
||||
|
||||
let session_manager = std::sync::Arc::new(crate::session::SessionManager::new(
|
||||
temp_dir.path().to_path_buf(),
|
||||
));
|
||||
let scheduler = crate::scheduler::Scheduler::new(
|
||||
temp_dir.path().join("schedule.json"),
|
||||
session_manager.clone(),
|
||||
)
|
||||
.await?;
|
||||
let job = crate::scheduler::ScheduledJob {
|
||||
id: "ordering_guard".to_string(),
|
||||
source: recipe_path.to_string_lossy().into_owned(),
|
||||
cron: "0 0 0 1 1 *".to_string(),
|
||||
last_run: None,
|
||||
currently_running: false,
|
||||
paused: false,
|
||||
current_session_id: None,
|
||||
process_start_time: None,
|
||||
parameters: vec![],
|
||||
recipe_base_dir: None,
|
||||
};
|
||||
scheduler.add_scheduled_job(job, true).await?;
|
||||
|
||||
let mut run = tokio::spawn({
|
||||
let scheduler = scheduler.clone();
|
||||
async move { scheduler.run_now("ordering_guard").await }
|
||||
});
|
||||
|
||||
tokio::select! {
|
||||
() = gate.entered() => {}
|
||||
result = &mut run => panic!("run ended before inference began: {result:?}"),
|
||||
() = tokio::time::sleep(std::time::Duration::from_secs(60)) => {
|
||||
panic!("inference request never arrived")
|
||||
}
|
||||
}
|
||||
let sampled = async {
|
||||
let sessions = session_manager
|
||||
.list_sessions_by_types(&[crate::session::SessionType::Scheduled])
|
||||
.await?;
|
||||
session_manager.get_session(&sessions[0].id, false).await
|
||||
}
|
||||
.await;
|
||||
gate.release();
|
||||
let session = sampled?;
|
||||
|
||||
let recipe = session
|
||||
.recipe
|
||||
.expect("recipe must be attached to the session before inference begins");
|
||||
assert_eq!(recipe.title, "Ordering guard");
|
||||
let sub_recipes = recipe
|
||||
.sub_recipes
|
||||
.expect("attached recipe must keep its sub_recipes block");
|
||||
assert_eq!(sub_recipes.len(), 1);
|
||||
assert_eq!(sub_recipes[0].name, "check_calendar");
|
||||
|
||||
run.await??;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user