fix(schedule): validate recipes consistently (#11561)
Co-authored-by: Alex Z <20266945+A1exZabr@users.noreply.github.com> Co-authored-by: Douwe Osinga <douwe.osinga@gmail.com>
This commit is contained in:
@@ -9,8 +9,7 @@ use chrono::Utc;
|
||||
use rmcp::model::{Annotations, ContentBlock, ErrorCode, ErrorData, Role, TextContent};
|
||||
|
||||
use crate::conversation::Conversation;
|
||||
use crate::recipe::template_recipe::parse_recipe_content;
|
||||
use crate::recipe::validate_recipe::validate_recipe_template_from_content;
|
||||
use crate::recipe::validate_recipe::{recipe_file_format, validate_recipe_for_scheduling};
|
||||
use crate::scheduler::{
|
||||
open_regular_schedule_recipe, ValidatedScheduleRecipe, MAX_SCHEDULE_RECIPE_BYTES,
|
||||
};
|
||||
@@ -181,18 +180,12 @@ impl ScheduleTool {
|
||||
let recipe_dir = canonical_recipe_path
|
||||
.parent()
|
||||
.map(|path| path.to_string_lossy().into_owned());
|
||||
// Parse failures echo the file back in the serde error, so they only ever
|
||||
// get the generic message; the checks that follow describe the recipe's
|
||||
// own shape and are safe to report.
|
||||
parse_recipe_content(&content, recipe_dir.clone()).map_err(|_| {
|
||||
if recipe_path.ends_with(".json") {
|
||||
recipe_file_error("Invalid JSON recipe")
|
||||
} else {
|
||||
recipe_file_error("Invalid YAML recipe")
|
||||
}
|
||||
})?;
|
||||
validate_recipe_template_from_content(&content, recipe_dir)
|
||||
.map_err(|error| recipe_file_error(&error.to_string()))?;
|
||||
validate_recipe_for_scheduling(
|
||||
&content,
|
||||
recipe_dir,
|
||||
recipe_file_format(&canonical_recipe_path),
|
||||
)
|
||||
.map_err(|error| recipe_file_error(&error.to_string()))?;
|
||||
|
||||
// Generate unique job ID
|
||||
let job_id = format!("agent_created_{}", uuid::Uuid::new_v4());
|
||||
|
||||
@@ -19,6 +19,7 @@ pub mod read_recipe_file_content;
|
||||
mod recipe_extension_adapter;
|
||||
pub mod template_recipe;
|
||||
pub mod validate_recipe;
|
||||
mod value_deserializer;
|
||||
pub mod yaml_format_utils;
|
||||
|
||||
pub const BUILT_IN_RECIPE_DIR_PARAM: &str = "recipe_dir";
|
||||
|
||||
@@ -243,35 +243,47 @@ pub fn parse_recipe_content(
|
||||
Ok((parsed.recipe, parsed.template_variables))
|
||||
}
|
||||
|
||||
pub(crate) fn parse_recipe_template(
|
||||
fn prepare_recipe_template_with_environment(
|
||||
content: &str,
|
||||
recipe_dir: Option<String>,
|
||||
) -> Result<ParsedRecipeTemplate> {
|
||||
) -> Result<(String, HashSet<String>, Environment<'static>)> {
|
||||
let preprocessed_content = preprocess_template_variables(content)?;
|
||||
|
||||
let (env, template_variables) = get_env_with_template_variables(
|
||||
&preprocessed_content,
|
||||
recipe_dir,
|
||||
UndefinedBehavior::Lenient,
|
||||
)?;
|
||||
let template = env.get_template(CURRENT_TEMPLATE_NAME).unwrap();
|
||||
|
||||
// Detect if template uses inheritance or includes
|
||||
let recipe_content = if uses_template_inheritance(&preprocessed_content) {
|
||||
// Must render to resolve inheritance
|
||||
template
|
||||
.render(())
|
||||
.map_err(|e| anyhow::anyhow!("Failed to parse the recipe {}", e))?
|
||||
} else {
|
||||
// Preserve conditionals and variables as-is
|
||||
preprocessed_content
|
||||
};
|
||||
Ok((recipe_content, template_variables, env))
|
||||
}
|
||||
|
||||
pub(crate) fn prepare_recipe_template(
|
||||
content: &str,
|
||||
recipe_dir: Option<String>,
|
||||
) -> Result<(String, HashSet<String>)> {
|
||||
let (recipe_content, template_variables, _) =
|
||||
prepare_recipe_template_with_environment(content, recipe_dir)?;
|
||||
Ok((recipe_content, template_variables))
|
||||
}
|
||||
|
||||
pub(crate) fn parse_recipe_template(
|
||||
content: &str,
|
||||
recipe_dir: Option<String>,
|
||||
) -> Result<ParsedRecipeTemplate> {
|
||||
let (recipe_content, template_variables, environment) =
|
||||
prepare_recipe_template_with_environment(content, recipe_dir)?;
|
||||
let recipe = Recipe::from_content(&recipe_content)?;
|
||||
Ok(ParsedRecipeTemplate {
|
||||
recipe,
|
||||
template_variables,
|
||||
environment: env,
|
||||
environment,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,412 @@
|
||||
use crate::recipe::read_recipe_file_content::RecipeFile;
|
||||
use crate::recipe::template_recipe::{
|
||||
parse_recipe_content, parse_recipe_template, ParsedRecipeTemplate,
|
||||
parse_recipe_content, parse_recipe_template, prepare_recipe_template, ParsedRecipeTemplate,
|
||||
};
|
||||
use crate::recipe::value_deserializer::RecipeValueDeserializer;
|
||||
use crate::recipe::{
|
||||
Recipe, RecipeParameter, RecipeParameterInputType, RecipeParameterRequirement,
|
||||
BUILT_IN_RECIPE_DIR_PARAM,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use serde_path_to_error::Segment;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum RecipeFileFormat {
|
||||
Json,
|
||||
Yaml,
|
||||
}
|
||||
|
||||
pub fn recipe_file_format(path: &Path) -> RecipeFileFormat {
|
||||
if path
|
||||
.extension()
|
||||
.is_some_and(|extension| extension.eq_ignore_ascii_case("json"))
|
||||
{
|
||||
RecipeFileFormat::Json
|
||||
} else {
|
||||
RecipeFileFormat::Yaml
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SchedulerRecipeError {
|
||||
GenericParse(RecipeFileFormat),
|
||||
InvalidSchema(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for SchedulerRecipeError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
SchedulerRecipeError::GenericParse(RecipeFileFormat::Json) => {
|
||||
write!(formatter, "Invalid JSON recipe")
|
||||
}
|
||||
SchedulerRecipeError::GenericParse(RecipeFileFormat::Yaml) => {
|
||||
write!(formatter, "Invalid YAML recipe")
|
||||
}
|
||||
SchedulerRecipeError::InvalidSchema(message) => {
|
||||
write!(formatter, "Invalid recipe: {message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for SchedulerRecipeError {}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum SchemaNode {
|
||||
Recipe,
|
||||
Scalar,
|
||||
StringList,
|
||||
Settings,
|
||||
Author,
|
||||
ParameterList,
|
||||
Parameter,
|
||||
ExtensionList,
|
||||
Extension,
|
||||
Response,
|
||||
SubRecipeList,
|
||||
SubRecipe,
|
||||
Retry,
|
||||
CheckList,
|
||||
Check,
|
||||
Opaque,
|
||||
}
|
||||
|
||||
struct SafeSchemaPath {
|
||||
display: String,
|
||||
node: SchemaNode,
|
||||
truncated: bool,
|
||||
}
|
||||
|
||||
fn schema_field(node: SchemaNode, field: &str) -> Option<SchemaNode> {
|
||||
match node {
|
||||
SchemaNode::Recipe => match field {
|
||||
"version" | "title" | "description" | "instructions" | "prompt" => {
|
||||
Some(SchemaNode::Scalar)
|
||||
}
|
||||
"extensions" => Some(SchemaNode::ExtensionList),
|
||||
"settings" => Some(SchemaNode::Settings),
|
||||
"activities" => Some(SchemaNode::StringList),
|
||||
"author" => Some(SchemaNode::Author),
|
||||
"parameters" => Some(SchemaNode::ParameterList),
|
||||
"response" => Some(SchemaNode::Response),
|
||||
"sub_recipes" => Some(SchemaNode::SubRecipeList),
|
||||
"retry" => Some(SchemaNode::Retry),
|
||||
_ => None,
|
||||
},
|
||||
SchemaNode::Settings => match field {
|
||||
"goose_provider" | "goose_model" | "temperature" | "max_turns" => {
|
||||
Some(SchemaNode::Scalar)
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
SchemaNode::Author => match field {
|
||||
"contact" | "metadata" => Some(SchemaNode::Scalar),
|
||||
_ => None,
|
||||
},
|
||||
SchemaNode::Parameter => match field {
|
||||
"key" | "input_type" | "requirement" | "description" | "default" => {
|
||||
Some(SchemaNode::Scalar)
|
||||
}
|
||||
"options" => Some(SchemaNode::StringList),
|
||||
_ => None,
|
||||
},
|
||||
SchemaNode::Extension => match field {
|
||||
"type" | "name" | "description" | "cmd" | "timeout" | "cwd" | "bundled"
|
||||
| "display_name" | "uri" | "socket" | "client_id" | "client_secret_key"
|
||||
| "instructions" | "code" => Some(SchemaNode::Scalar),
|
||||
"args" | "env_keys" | "available_tools" | "scopes" | "dependencies" => {
|
||||
Some(SchemaNode::StringList)
|
||||
}
|
||||
"envs" | "headers" | "tools" => Some(SchemaNode::Opaque),
|
||||
_ => None,
|
||||
},
|
||||
SchemaNode::Response => match field {
|
||||
"json_schema" => Some(SchemaNode::Opaque),
|
||||
_ => None,
|
||||
},
|
||||
SchemaNode::SubRecipe => match field {
|
||||
"name" | "path" | "sequential_when_repeated" | "description" => {
|
||||
Some(SchemaNode::Scalar)
|
||||
}
|
||||
"values" => Some(SchemaNode::Opaque),
|
||||
_ => None,
|
||||
},
|
||||
SchemaNode::Retry => match field {
|
||||
"max_retries" | "on_failure" | "timeout_seconds" | "on_failure_timeout_seconds" => {
|
||||
Some(SchemaNode::Scalar)
|
||||
}
|
||||
"checks" => Some(SchemaNode::CheckList),
|
||||
_ => None,
|
||||
},
|
||||
SchemaNode::Check => match field {
|
||||
"type" | "command" => Some(SchemaNode::Scalar),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn schema_sequence_item(node: SchemaNode) -> Option<SchemaNode> {
|
||||
match node {
|
||||
SchemaNode::StringList => Some(SchemaNode::Scalar),
|
||||
SchemaNode::ParameterList => Some(SchemaNode::Parameter),
|
||||
SchemaNode::ExtensionList => Some(SchemaNode::Extension),
|
||||
SchemaNode::SubRecipeList => Some(SchemaNode::SubRecipe),
|
||||
SchemaNode::CheckList => Some(SchemaNode::Check),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn safe_schema_path(path: &serde_path_to_error::Path) -> Option<SafeSchemaPath> {
|
||||
let mut node = SchemaNode::Recipe;
|
||||
let mut display = String::new();
|
||||
|
||||
for segment in path {
|
||||
if matches!(node, SchemaNode::Opaque) {
|
||||
return Some(SafeSchemaPath {
|
||||
display,
|
||||
node,
|
||||
truncated: true,
|
||||
});
|
||||
}
|
||||
|
||||
match segment {
|
||||
Segment::Map { key } => {
|
||||
node = schema_field(node, key)?;
|
||||
if !display.is_empty() {
|
||||
display.push('.');
|
||||
}
|
||||
display.push_str(key);
|
||||
}
|
||||
Segment::Seq { index } => {
|
||||
node = schema_sequence_item(node)?;
|
||||
display.push_str(&format!("[{index}]"));
|
||||
}
|
||||
Segment::Enum { .. } | Segment::Unknown => return None,
|
||||
}
|
||||
}
|
||||
|
||||
Some(SafeSchemaPath {
|
||||
display,
|
||||
node,
|
||||
truncated: false,
|
||||
})
|
||||
}
|
||||
|
||||
fn strip_path_prefix(message: &str, path: &str) -> String {
|
||||
if path.is_empty() {
|
||||
return message.to_string();
|
||||
}
|
||||
message
|
||||
.strip_prefix(path)
|
||||
.and_then(|rest| rest.strip_prefix(": "))
|
||||
.unwrap_or(message)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn schema_field_from_message<'a>(message: &'a str, prefix: &str) -> Option<&'a str> {
|
||||
message.strip_prefix(prefix)?.strip_suffix('`')
|
||||
}
|
||||
|
||||
fn classify_conversion_error(
|
||||
error: &serde_path_to_error::Error<serde_yaml::Error>,
|
||||
format: RecipeFileFormat,
|
||||
) -> SchedulerRecipeError {
|
||||
let Some(safe_path) = safe_schema_path(error.path()) else {
|
||||
return SchedulerRecipeError::GenericParse(format);
|
||||
};
|
||||
let raw_path = match error.path().to_string().as_str() {
|
||||
"." => String::new(),
|
||||
path => path.to_string(),
|
||||
};
|
||||
let message = strip_path_prefix(
|
||||
&crate::recipe::strip_error_location(&error.inner().to_string()),
|
||||
&raw_path,
|
||||
);
|
||||
|
||||
if !safe_path.truncated {
|
||||
for (prefix, label) in [
|
||||
("missing field `", "missing field"),
|
||||
("duplicate field `", "duplicate field"),
|
||||
] {
|
||||
if let Some(field) = schema_field_from_message(&message, prefix) {
|
||||
if schema_field(safe_path.node, field).is_some() {
|
||||
let diagnostic = if safe_path.display.is_empty() {
|
||||
format!("{label} `{field}`")
|
||||
} else {
|
||||
format!("{}: {label} `{field}`", safe_path.display)
|
||||
};
|
||||
return SchedulerRecipeError::InvalidSchema(diagnostic);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if safe_path.display.is_empty() {
|
||||
SchedulerRecipeError::GenericParse(format)
|
||||
} else {
|
||||
SchedulerRecipeError::InvalidSchema(format!("{} is invalid", safe_path.display))
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_recipe_from_value(
|
||||
value: &serde_yaml::Value,
|
||||
format: RecipeFileFormat,
|
||||
coerce_scalars: bool,
|
||||
) -> Result<Recipe, SchedulerRecipeError> {
|
||||
let result = if coerce_scalars {
|
||||
serde_path_to_error::deserialize(RecipeValueDeserializer::new(value))
|
||||
} else {
|
||||
serde_path_to_error::deserialize(value)
|
||||
};
|
||||
result.map_err(|error| classify_conversion_error(&error, format))
|
||||
}
|
||||
|
||||
fn format_parameter_paths(indices: &[usize]) -> String {
|
||||
indices
|
||||
.iter()
|
||||
.map(|index| format!("parameters[{index}]"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
}
|
||||
|
||||
fn normalize_scheduling_parameter_key(key: &str) -> String {
|
||||
serde_yaml::from_str::<serde_yaml::Value>(key)
|
||||
.ok()
|
||||
.and_then(|value| match value {
|
||||
serde_yaml::Value::Bool(value) => Some(value.to_string()),
|
||||
serde_yaml::Value::Number(value) => Some(value.to_string()),
|
||||
serde_yaml::Value::Null => Some("null".to_string()),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or_else(|| key.to_string())
|
||||
}
|
||||
|
||||
fn validate_scheduling_parameters(
|
||||
parameters: &Option<Vec<RecipeParameter>>,
|
||||
template_variables: &HashSet<String>,
|
||||
) -> Result<(), String> {
|
||||
let parameters = parameters.as_deref().unwrap_or_default();
|
||||
|
||||
let file_defaults = parameters
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, parameter)| {
|
||||
matches!(parameter.input_type, RecipeParameterInputType::File)
|
||||
&& parameter.default.is_some()
|
||||
})
|
||||
.map(|(index, _)| index)
|
||||
.collect::<Vec<_>>();
|
||||
if !file_defaults.is_empty() {
|
||||
return Err(format!(
|
||||
"file parameters cannot have default values at {}",
|
||||
format_parameter_paths(&file_defaults)
|
||||
));
|
||||
}
|
||||
|
||||
let optional_without_defaults = parameters
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, parameter)| {
|
||||
matches!(parameter.requirement, RecipeParameterRequirement::Optional)
|
||||
&& parameter.default.is_none()
|
||||
})
|
||||
.map(|(index, _)| index)
|
||||
.collect::<Vec<_>>();
|
||||
if !optional_without_defaults.is_empty() {
|
||||
return Err(format!(
|
||||
"optional parameters require default values at {}",
|
||||
format_parameter_paths(&optional_without_defaults)
|
||||
));
|
||||
}
|
||||
|
||||
let mut defined_keys = HashSet::new();
|
||||
for (index, parameter) in parameters.iter().enumerate() {
|
||||
if !defined_keys.insert(parameter.key.clone()) {
|
||||
return Err(format!(
|
||||
"duplicate parameter definition at parameters[{index}]"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let mut referenced_keys = template_variables.clone();
|
||||
referenced_keys.remove(BUILT_IN_RECIPE_DIR_PARAM);
|
||||
let keys_match = |defined: &str, referenced: &str| {
|
||||
defined == referenced || defined == normalize_scheduling_parameter_key(referenced)
|
||||
};
|
||||
|
||||
let missing_count = referenced_keys
|
||||
.iter()
|
||||
.filter(|referenced| {
|
||||
!defined_keys
|
||||
.iter()
|
||||
.any(|defined| keys_match(defined, referenced))
|
||||
})
|
||||
.count();
|
||||
if missing_count > 0 {
|
||||
return Err(format!(
|
||||
"missing parameter definitions for {missing_count} template variables"
|
||||
));
|
||||
}
|
||||
|
||||
let unnecessary = parameters
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, parameter)| {
|
||||
!referenced_keys
|
||||
.iter()
|
||||
.any(|referenced| keys_match(¶meter.key, referenced))
|
||||
})
|
||||
.map(|(index, _)| index)
|
||||
.collect::<Vec<_>>();
|
||||
if !unnecessary.is_empty() {
|
||||
return Err(format!(
|
||||
"unnecessary parameter definitions at {}",
|
||||
format_parameter_paths(&unnecessary)
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn validate_recipe_for_scheduling(
|
||||
content: &str,
|
||||
recipe_dir: Option<String>,
|
||||
format: RecipeFileFormat,
|
||||
) -> Result<Recipe, SchedulerRecipeError> {
|
||||
let (prepared_content, template_variables) = prepare_recipe_template(content, recipe_dir)
|
||||
.map_err(|_| SchedulerRecipeError::GenericParse(format))?;
|
||||
let document = serde_yaml::from_str::<serde_yaml::Value>(&prepared_content)
|
||||
.map_err(|_| SchedulerRecipeError::GenericParse(format))?;
|
||||
let nested_recipe = document.get("recipe");
|
||||
let recipe_value = nested_recipe.unwrap_or(&document);
|
||||
let mut recipe = convert_recipe_from_value(recipe_value, format, nested_recipe.is_none())?;
|
||||
recipe.ensure_analyze_for_developer();
|
||||
recipe.ensure_summon_for_subrecipes();
|
||||
|
||||
validate_prompt_or_instructions(&recipe)
|
||||
.map_err(|error| SchedulerRecipeError::InvalidSchema(error.to_string()))?;
|
||||
validate_retry_config(&recipe)
|
||||
.map_err(|error| SchedulerRecipeError::InvalidSchema(error.to_string()))?;
|
||||
validate_scheduling_parameters(&recipe.parameters, &template_variables)
|
||||
.map_err(SchedulerRecipeError::InvalidSchema)?;
|
||||
if let Some(schema) = recipe
|
||||
.response
|
||||
.as_ref()
|
||||
.and_then(|response| response.json_schema.as_ref())
|
||||
{
|
||||
validate_json_schema(schema).map_err(|error| match error.to_string().as_str() {
|
||||
"JSON schema must be an object" | "Empty JSON schema is not allowed" => {
|
||||
SchedulerRecipeError::InvalidSchema(error.to_string())
|
||||
}
|
||||
_ => SchedulerRecipeError::InvalidSchema("response.json_schema is invalid".to_string()),
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(recipe)
|
||||
}
|
||||
|
||||
pub(crate) struct ValidatedRecipeTemplate {
|
||||
parsed: ParsedRecipeTemplate,
|
||||
@@ -37,7 +436,9 @@ pub fn parse_and_validate_parameters(
|
||||
) -> Result<Recipe> {
|
||||
let (recipe_template, template_variables) =
|
||||
parse_recipe_content(recipe_file_content, recipe_dir_str)?;
|
||||
validate_recipe_parameters(&recipe_template, &template_variables)?;
|
||||
let recipe_parameters = &recipe_template.parameters;
|
||||
validate_optional_parameters(recipe_parameters)?;
|
||||
validate_parameters_in_template(recipe_parameters, &template_variables)?;
|
||||
Ok(recipe_template)
|
||||
}
|
||||
|
||||
@@ -82,7 +483,6 @@ pub(crate) fn validate_recipe_template(
|
||||
let parsed = parse_recipe_template(recipe_content, recipe_dir)?;
|
||||
validate_recipe_parameters(parsed.recipe(), parsed.template_variables())?;
|
||||
validate_recipe_non_parameter_invariants(parsed.recipe())?;
|
||||
|
||||
Ok(ValidatedRecipeTemplate { parsed })
|
||||
}
|
||||
|
||||
@@ -94,7 +494,6 @@ pub(crate) fn validate_recipe_non_parameter_invariants(recipe: &Recipe) -> Resul
|
||||
validate_json_schema(json_schema)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -309,6 +708,301 @@ parameters:
|
||||
println!("Recipe: {:?}", recipe.prompt);
|
||||
}
|
||||
|
||||
fn scheduling_error(content: &str) -> SchedulerRecipeError {
|
||||
validate_recipe_for_scheduling(content, None, RecipeFileFormat::Yaml).unwrap_err()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_reports_safe_schema_paths_without_values() {
|
||||
let cases = [
|
||||
(
|
||||
"description: Missing title\nprompt: Run safely\n",
|
||||
"Invalid recipe: missing field `title`",
|
||||
"Missing title",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run {{ item }}\nparameters:\n - key: item\n input_type: string\n requirement: required\n",
|
||||
"Invalid recipe: parameters[0]: missing field `description`",
|
||||
"item",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run {{ item }}\nparameters:\n - key: item\n input_type: yaml-secret-242\n requirement: required\n description: hi\n",
|
||||
"Invalid recipe: parameters[0].input_type is invalid",
|
||||
"yaml-secret-242",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run\nsettings:\n temperature: yaml-secret-242\n",
|
||||
"Invalid recipe: settings.temperature is invalid",
|
||||
"yaml-secret-242",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run\nauthor:\n contact: [yaml-secret-242]\n",
|
||||
"Invalid recipe: author.contact is invalid",
|
||||
"yaml-secret-242",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run\nsub_recipes:\n - path: yaml-secret-242.yaml\n",
|
||||
"Invalid recipe: sub_recipes[0]: missing field `name`",
|
||||
"yaml-secret-242",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run\nextensions:\n - type: yaml-secret-242\n name: ext\n",
|
||||
"Invalid recipe: extensions[0].type is invalid",
|
||||
"yaml-secret-242",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run\nretry:\n max_retries: yaml-secret-242\n checks: []\n",
|
||||
"Invalid recipe: retry.max_retries is invalid",
|
||||
"yaml-secret-242",
|
||||
),
|
||||
];
|
||||
|
||||
for (content, expected, secret) in cases {
|
||||
let message = scheduling_error(content).to_string();
|
||||
assert_eq!(message, expected);
|
||||
assert!(!message.contains(secret));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_uses_generic_errors_for_untrusted_parse_details() {
|
||||
let yaml_cases = [
|
||||
"yaml-secret-242",
|
||||
"title: Test: yaml-secret-242\n",
|
||||
"{% if %}\nyaml-secret-242\n{% endif %}\n",
|
||||
];
|
||||
for content in yaml_cases {
|
||||
let message = scheduling_error(content).to_string();
|
||||
assert_eq!(message, "Invalid YAML recipe");
|
||||
assert!(!message.contains("yaml-secret-242"));
|
||||
}
|
||||
|
||||
let message = validate_recipe_for_scheduling(
|
||||
"{\"title\": \"json-secret-242\"",
|
||||
None,
|
||||
RecipeFileFormat::Json,
|
||||
)
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert_eq!(message, "Invalid JSON recipe");
|
||||
assert!(!message.contains("json-secret-242"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_rejects_duplicate_fields_like_runtime_load() {
|
||||
let content = "title: First\ntitle: yaml-secret-242\ndescription: hi\nprompt: Run safely\n";
|
||||
|
||||
assert!(Recipe::from_content(content).is_err());
|
||||
let message = scheduling_error(content).to_string();
|
||||
assert_eq!(message, "Invalid YAML recipe");
|
||||
assert!(!message.contains("yaml-secret-242"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_preserves_runtime_scalar_coercion() {
|
||||
let recipe = validate_recipe_for_scheduling(
|
||||
"title: 12345\ndescription: hi\nprompt: Run {{ count }}\nsettings:\n temperature: 0.3\n max_turns: 7\nparameters:\n - key: count\n input_type: string\n requirement: optional\n default: 0.85\n description: hi\n",
|
||||
None,
|
||||
RecipeFileFormat::Yaml,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(recipe.title, "12345");
|
||||
assert_eq!(
|
||||
recipe.parameters.as_ref().unwrap()[0].default.as_deref(),
|
||||
Some("0.85")
|
||||
);
|
||||
let settings = recipe.settings.unwrap();
|
||||
assert_eq!(settings.temperature, Some(0.3));
|
||||
assert_eq!(settings.max_turns, Some(7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_scalar_spellings_match_cli_acceptance() {
|
||||
for scalar in [
|
||||
"15",
|
||||
"0.85",
|
||||
"1e3",
|
||||
"0x10",
|
||||
"true",
|
||||
"null",
|
||||
"~",
|
||||
"!!null null",
|
||||
"!!str 15",
|
||||
"!!str null",
|
||||
"!custom yaml-secret-242",
|
||||
] {
|
||||
let content = format!(
|
||||
"title: 12345\ndescription: hi\nprompt: Run {{{{ count }}}}\nparameters:\n - key: count\n input_type: string\n requirement: optional\n default: {scalar}\n description: hi\n"
|
||||
);
|
||||
let cli = validate_recipe_template_from_content(&content, None);
|
||||
let scheduling = validate_recipe_for_scheduling(&content, None, RecipeFileFormat::Yaml);
|
||||
|
||||
assert_eq!(
|
||||
scheduling.is_ok(),
|
||||
cli.is_ok(),
|
||||
"validation verdict differed for {scalar}: cli={cli:?}, scheduling={scheduling:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_matches_parameter_keys_after_yaml_scalar_normalization() {
|
||||
for key in ["TRUE", "NULL"] {
|
||||
let template_key = key;
|
||||
let content = format!(
|
||||
"title: Test\ndescription: hi\nprompt: Run {{{{ {template_key} }}}}\nparameters:\n - key: {key}\n input_type: string\n requirement: required\n description: hi\n"
|
||||
);
|
||||
let cli = validate_recipe_template_from_content(&content, None);
|
||||
let scheduling = validate_recipe_for_scheduling(&content, None, RecipeFileFormat::Yaml);
|
||||
|
||||
assert_eq!(
|
||||
scheduling.is_ok(),
|
||||
cli.is_ok(),
|
||||
"validation verdict differed for {key}: cli={cli:?}, scheduling={scheduling:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_required_string_scalars_match_cli_acceptance() {
|
||||
for scalar in [
|
||||
"12345",
|
||||
"1e3",
|
||||
"0x10",
|
||||
"true",
|
||||
"null",
|
||||
"~",
|
||||
"!!null null",
|
||||
"!!str null",
|
||||
] {
|
||||
let content = format!("title: {scalar}\ndescription: hi\nprompt: Run safely\n");
|
||||
let cli = validate_recipe_template_from_content(&content, None);
|
||||
let scheduling = validate_recipe_for_scheduling(&content, None, RecipeFileFormat::Yaml);
|
||||
|
||||
assert_eq!(
|
||||
scheduling.is_ok(),
|
||||
cli.is_ok(),
|
||||
"validation verdict differed for {scalar}: cli={cli:?}, scheduling={scheduling:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_extension_scalars_match_cli_acceptance() {
|
||||
let content = "title: Test\ndescription: hi\nprompt: Run safely\nextensions:\n - type: stdio\n name: 12345\n cmd: 67890\n args: [13579]\n";
|
||||
|
||||
let cli = validate_recipe_template_from_content(content, None);
|
||||
let scheduling = validate_recipe_for_scheduling(content, None, RecipeFileFormat::Yaml);
|
||||
|
||||
assert_eq!(
|
||||
scheduling.is_ok(),
|
||||
cli.is_ok(),
|
||||
"validation verdict differed: cli={cli:?}, scheduling={scheduling:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_accepts_nested_recipe_documents() {
|
||||
let recipe = validate_recipe_for_scheduling(
|
||||
"recipe:\n title: Nested\n description: hi\n prompt: Run safely\n",
|
||||
None,
|
||||
RecipeFileFormat::Yaml,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(recipe.title, "Nested");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_preserves_nested_recipe_scalar_rules() {
|
||||
let content = "recipe:\n title: Nested\n description: hi\n prompt: Run {{ count }}\n parameters:\n - key: count\n input_type: string\n requirement: optional\n default: 15\n description: hi\n";
|
||||
|
||||
assert!(validate_recipe_template_from_content(content, None).is_err());
|
||||
let message = scheduling_error(content).to_string();
|
||||
assert_eq!(message, "Invalid recipe: parameters[0].default is invalid");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_parameter_diagnostics_do_not_reflect_keys() {
|
||||
let cases = [
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run\nparameters:\n - key: yaml-secret-242\n input_type: string\n requirement: required\n description: hi\n",
|
||||
"Invalid recipe: unnecessary parameter definitions at parameters[0]",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run {{ yaml_secret_242 }}\n",
|
||||
"Invalid recipe: missing parameter definitions for 1 template variables",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run {{ yaml_secret_242 }}\nparameters:\n - key: yaml_secret_242\n input_type: string\n requirement: required\n description: first\n - key: yaml_secret_242\n input_type: string\n requirement: required\n description: second\n",
|
||||
"Invalid recipe: duplicate parameter definition at parameters[1]",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run {{ yaml_secret_242 }}\nparameters:\n - key: yaml_secret_242\n input_type: file\n requirement: required\n default: secret.txt\n description: hi\n",
|
||||
"Invalid recipe: file parameters cannot have default values at parameters[0]",
|
||||
),
|
||||
(
|
||||
"title: Test\ndescription: hi\nprompt: Run {{ yaml_secret_242 }}\nparameters:\n - key: yaml_secret_242\n input_type: string\n requirement: optional\n description: hi\n",
|
||||
"Invalid recipe: optional parameters require default values at parameters[0]",
|
||||
),
|
||||
];
|
||||
|
||||
for (content, expected) in cases {
|
||||
let message = scheduling_error(content).to_string();
|
||||
assert_eq!(message, expected);
|
||||
assert!(!message.contains("yaml_secret_242"));
|
||||
assert!(!message.contains("yaml-secret-242"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_redacts_dynamic_map_keys() {
|
||||
let cases = [
|
||||
"title: Test\ndescription: hi\nprompt: Run\nextensions:\n - type: stdio\n name: ext\n cmd: run\n args: []\n envs:\n yaml-secret-242: []\n",
|
||||
"title: Test\ndescription: hi\nprompt: Run\nextensions:\n - type: streamable_http\n name: ext\n uri: https://example.com\n headers:\n yaml-secret-242: []\n",
|
||||
"title: Test\ndescription: hi\nprompt: Run\nsub_recipes:\n - name: child\n path: child.yaml\n values:\n yaml-secret-242:\n ? [complex, key]\n : value\n",
|
||||
];
|
||||
|
||||
for content in cases {
|
||||
let message = scheduling_error(content).to_string();
|
||||
assert!(!message.contains("yaml-secret-242"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_sanitizes_json_schema_compile_errors() {
|
||||
let content = r#"
|
||||
title: Test
|
||||
description: hi
|
||||
prompt: Run
|
||||
response:
|
||||
json_schema:
|
||||
type: object
|
||||
properties:
|
||||
result:
|
||||
type: string
|
||||
pattern: "[yaml-secret-242"
|
||||
"#;
|
||||
|
||||
let message = scheduling_error(content).to_string();
|
||||
assert_eq!(message, "Invalid recipe: response.json_schema is invalid");
|
||||
assert!(!message.contains("yaml-secret-242"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scheduling_uses_json_format_without_losing_schema_details() {
|
||||
let message = validate_recipe_for_scheduling(
|
||||
"{\"description\": \"no title\", \"prompt\": \"Run\"}",
|
||||
None,
|
||||
RecipeFileFormat::Json,
|
||||
)
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(message, "Invalid recipe: missing field `title`");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn response_json_schema_must_be_an_object() {
|
||||
let recipe_content = r#"
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
use serde::de::{DeserializeSeed, Deserializer, MapAccess, SeqAccess, Visitor};
|
||||
use serde::forward_to_deserialize_any;
|
||||
use serde_yaml::mapping::Iter as MappingIter;
|
||||
use serde_yaml::Value;
|
||||
use std::slice;
|
||||
|
||||
pub(super) struct RecipeValueDeserializer<'de> {
|
||||
value: &'de Value,
|
||||
}
|
||||
|
||||
impl<'de> RecipeValueDeserializer<'de> {
|
||||
pub(super) fn new(value: &'de Value) -> Self {
|
||||
Self { value }
|
||||
}
|
||||
|
||||
fn deserialize_string_value<V>(self, visitor: V) -> Result<V::Value, serde_yaml::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.value {
|
||||
Value::String(value) => visitor.visit_borrowed_str(value),
|
||||
Value::Number(value) => visitor.visit_string(value.to_string()),
|
||||
Value::Bool(value) => visitor.visit_string(value.to_string()),
|
||||
Value::Null => visitor.visit_string("null".to_string()),
|
||||
Value::Tagged(tagged) => Self::new(&tagged.value).deserialize_string_value(visitor),
|
||||
other => serde::Deserializer::deserialize_string(other, visitor),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserializer<'de> for RecipeValueDeserializer<'de> {
|
||||
type Error = serde_yaml::Error;
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.value {
|
||||
Value::Sequence(sequence) => visitor.visit_seq(RecipeSeqAccess {
|
||||
iter: sequence.iter(),
|
||||
}),
|
||||
Value::Mapping(mapping) => visitor.visit_map(RecipeMapAccess {
|
||||
iter: mapping.iter(),
|
||||
value: None,
|
||||
}),
|
||||
Value::Tagged(tagged) => Self::new(&tagged.value).deserialize_any(visitor),
|
||||
value => serde::Deserializer::deserialize_any(value, visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_string_value(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_string_value(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_string_value(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.value {
|
||||
Value::Null => visitor.visit_none(),
|
||||
_ => visitor.visit_some(self),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.value {
|
||||
Value::Sequence(sequence) => visitor.visit_seq(RecipeSeqAccess {
|
||||
iter: sequence.iter(),
|
||||
}),
|
||||
Value::Tagged(tagged) => Self::new(&tagged.value).deserialize_seq(visitor),
|
||||
value => serde::Deserializer::deserialize_seq(value, visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_seq(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_tuple_struct<V>(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_len: usize,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_seq(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.value {
|
||||
Value::Mapping(mapping) => visitor.visit_map(RecipeMapAccess {
|
||||
iter: mapping.iter(),
|
||||
value: None,
|
||||
}),
|
||||
Value::Tagged(tagged) => Self::new(&tagged.value).deserialize_map(visitor),
|
||||
value => serde::Deserializer::deserialize_map(value, visitor),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_struct<V>(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_fields: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_map(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
serde::Deserializer::deserialize_enum(self.value, name, variants, visitor)
|
||||
}
|
||||
|
||||
fn deserialize_newtype_struct<V>(
|
||||
self,
|
||||
_name: &'static str,
|
||||
visitor: V,
|
||||
) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 bytes byte_buf
|
||||
unit unit_struct identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
struct RecipeSeqAccess<'de> {
|
||||
iter: slice::Iter<'de, Value>,
|
||||
}
|
||||
|
||||
impl<'de> SeqAccess<'de> for RecipeSeqAccess<'de> {
|
||||
type Error = serde_yaml::Error;
|
||||
|
||||
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
||||
where
|
||||
T: DeserializeSeed<'de>,
|
||||
{
|
||||
self.iter
|
||||
.next()
|
||||
.map(|value| seed.deserialize(RecipeValueDeserializer::new(value)))
|
||||
.transpose()
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> Option<usize> {
|
||||
Some(self.iter.len())
|
||||
}
|
||||
}
|
||||
|
||||
struct RecipeMapAccess<'de> {
|
||||
iter: MappingIter<'de>,
|
||||
value: Option<&'de Value>,
|
||||
}
|
||||
|
||||
impl<'de> MapAccess<'de> for RecipeMapAccess<'de> {
|
||||
type Error = serde_yaml::Error;
|
||||
|
||||
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
{
|
||||
let Some((key, value)) = self.iter.next() else {
|
||||
return Ok(None);
|
||||
};
|
||||
self.value = Some(value);
|
||||
seed.deserialize(RecipeValueDeserializer::new(key))
|
||||
.map(Some)
|
||||
}
|
||||
|
||||
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
let value = self.value.take().expect("value requested before map key");
|
||||
seed.deserialize(RecipeValueDeserializer::new(value))
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> Option<usize> {
|
||||
Some(self.iter.len())
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,9 @@ use crate::conversation::Conversation;
|
||||
use crate::posthog;
|
||||
use crate::providers::create;
|
||||
use crate::recipe::build_recipe::build_recipe_from_template;
|
||||
use crate::recipe::validate_recipe::{
|
||||
recipe_file_format, validate_recipe_for_scheduling, SchedulerRecipeError,
|
||||
};
|
||||
use crate::recipe::Recipe;
|
||||
use crate::scheduler_trait::SchedulerTrait;
|
||||
use crate::session::session_manager::SessionType;
|
||||
@@ -73,8 +76,8 @@ pub(crate) fn open_regular_schedule_recipe(path: &Path) -> io::Result<File> {
|
||||
Ok(file)
|
||||
}
|
||||
|
||||
fn copy_bounded_schedule_recipe(source: &Path, destination: &Path) -> Result<(), SchedulerError> {
|
||||
let source = open_regular_schedule_recipe(source).map_err(|error| {
|
||||
fn read_validated_schedule_recipe(source_path: &Path) -> Result<Vec<u8>, SchedulerError> {
|
||||
let source = open_regular_schedule_recipe(source_path).map_err(|error| {
|
||||
SchedulerError::RecipeLoadError(format!("Cannot read recipe file: {error}"))
|
||||
})?;
|
||||
let metadata = source.metadata().map_err(|error| {
|
||||
@@ -104,7 +107,17 @@ fn copy_bounded_schedule_recipe(source: &Path, destination: &Path) -> Result<(),
|
||||
)));
|
||||
}
|
||||
|
||||
write_schedule_recipe_bytes(destination, &bytes)
|
||||
let format = recipe_file_format(source_path);
|
||||
let content = std::str::from_utf8(&bytes).map_err(|_| {
|
||||
SchedulerError::RecipeLoadError(SchedulerRecipeError::GenericParse(format).to_string())
|
||||
})?;
|
||||
let recipe_dir = source_path
|
||||
.parent()
|
||||
.map(|path| path.to_string_lossy().into_owned());
|
||||
validate_recipe_for_scheduling(content, recipe_dir, format)
|
||||
.map_err(|error| SchedulerError::RecipeLoadError(error.to_string()))?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
fn write_schedule_recipe_bytes(destination: &Path, bytes: &[u8]) -> Result<(), SchedulerError> {
|
||||
@@ -441,9 +454,9 @@ impl Scheduler {
|
||||
|
||||
let mut stored_job = original_job_spec;
|
||||
if make_copy {
|
||||
let (original_recipe_path, validated_recipe) =
|
||||
let (original_recipe_path, recipe_bytes) =
|
||||
if let Some(validated_recipe) = validated_recipe {
|
||||
(validated_recipe.source, Some(validated_recipe.bytes))
|
||||
(validated_recipe.source, validated_recipe.bytes)
|
||||
} else {
|
||||
let original_recipe_path =
|
||||
Path::new(&stored_job.source).canonicalize().map_err(|e| {
|
||||
@@ -458,7 +471,8 @@ impl Scheduler {
|
||||
stored_job.source
|
||||
)));
|
||||
}
|
||||
(original_recipe_path, None)
|
||||
let recipe_bytes = read_validated_schedule_recipe(&original_recipe_path)?;
|
||||
(original_recipe_path, recipe_bytes)
|
||||
};
|
||||
|
||||
let scheduled_recipes_dir = self
|
||||
@@ -475,11 +489,7 @@ impl Scheduler {
|
||||
let destination_filename = format!("{}.{}", stored_job.id, original_extension);
|
||||
let destination_recipe_path = scheduled_recipes_dir.join(destination_filename);
|
||||
|
||||
if let Some(recipe) = validated_recipe.as_deref() {
|
||||
write_schedule_recipe_bytes(&destination_recipe_path, recipe)?;
|
||||
} else {
|
||||
copy_bounded_schedule_recipe(&original_recipe_path, &destination_recipe_path)?;
|
||||
}
|
||||
write_schedule_recipe_bytes(&destination_recipe_path, &recipe_bytes)?;
|
||||
stored_job.recipe_base_dir = original_recipe_path
|
||||
.parent()
|
||||
.map(|p| p.to_string_lossy().into_owned());
|
||||
@@ -1336,10 +1346,9 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bounded_recipe_copy_rejects_source_that_grew_after_validation() {
|
||||
fn bounded_recipe_read_rejects_source_that_grew_after_validation() {
|
||||
let temp_dir = tempdir().unwrap();
|
||||
let source = temp_dir.path().join("source.yaml");
|
||||
let destination = temp_dir.path().join("destination.yaml");
|
||||
fs::write(
|
||||
&source,
|
||||
"title: Valid\ndescription: Initially valid\nprompt: Run safely\n",
|
||||
@@ -1354,10 +1363,9 @@ mod tests {
|
||||
.set_len(MAX_SCHEDULE_RECIPE_BYTES + 1)
|
||||
.unwrap();
|
||||
|
||||
let error = copy_bounded_schedule_recipe(&source, &destination).unwrap_err();
|
||||
let error = read_validated_schedule_recipe(&source).unwrap_err();
|
||||
|
||||
assert!(error.to_string().contains("exceeds the 1048576 byte limit"));
|
||||
assert!(!destination.exists());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -1428,6 +1436,70 @@ mod tests {
|
||||
assert!(!destination.exists());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn schedule_add_rejects_invalid_recipe_before_copying_or_persisting() {
|
||||
let temp_dir = tempdir().unwrap();
|
||||
let recipe_path = temp_dir.path().join("missing-title.yaml");
|
||||
fs::write(
|
||||
&recipe_path,
|
||||
"description: Missing title\nprompt: Run safely\n",
|
||||
)
|
||||
.unwrap();
|
||||
let storage_path = temp_dir.path().join("schedule.json");
|
||||
let session_manager = Arc::new(SessionManager::new(temp_dir.path().to_path_buf()));
|
||||
let scheduler = Scheduler::new(storage_path, session_manager).await.unwrap();
|
||||
let job = ScheduledJob {
|
||||
id: "invalid_recipe".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,
|
||||
};
|
||||
|
||||
let error = scheduler.add_scheduled_job(job, true).await.unwrap_err();
|
||||
|
||||
assert_eq!(
|
||||
error.to_string(),
|
||||
"Recipe load error: Invalid recipe: missing field `title`"
|
||||
);
|
||||
assert!(scheduler.list_scheduled_jobs().await.is_empty());
|
||||
assert!(!temp_dir.path().join("scheduled_recipes").exists());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn schedule_add_preserves_validated_source_bytes() {
|
||||
let temp_dir = tempdir().unwrap();
|
||||
let recipe_path = temp_dir.path().join("scalar-spelling.yaml");
|
||||
let source = b"title: 12345\ndescription: Preserve source bytes\nprompt: Run {{ count }}\nparameters:\n - key: count\n input_type: string\n requirement: optional\n default: 1e3\n description: hi\n";
|
||||
fs::write(&recipe_path, source).unwrap();
|
||||
let storage_path = temp_dir.path().join("schedule.json");
|
||||
let session_manager = Arc::new(SessionManager::new(temp_dir.path().to_path_buf()));
|
||||
let scheduler = Scheduler::new(storage_path, session_manager).await.unwrap();
|
||||
let job = ScheduledJob {
|
||||
id: "preserve_source".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.unwrap();
|
||||
|
||||
let jobs = scheduler.list_scheduled_jobs().await;
|
||||
assert_eq!(jobs.len(), 1);
|
||||
assert_eq!(fs::read(&jobs[0].source).unwrap(), source);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn validated_recipe_copy_makes_existing_destination_owner_private() {
|
||||
@@ -1677,13 +1749,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_job_with_no_prompt_does_not_panic() {
|
||||
let _guard = env_lock::lock_env([
|
||||
("GOOSE_PROVIDER", Some("openai")),
|
||||
("GOOSE_MODEL", Some("gpt-4o")),
|
||||
("OPENAI_API_KEY", Some("fake-openai-no-keyring")),
|
||||
("OPENAI_CUSTOM_HEADERS", Some("")),
|
||||
]);
|
||||
async fn test_job_with_no_prompt_is_rejected_at_add_time() {
|
||||
let temp_dir = tempdir().unwrap();
|
||||
let recipe_path = temp_dir.path().join("no_prompt.yaml");
|
||||
fs::write(
|
||||
@@ -1709,15 +1775,13 @@ mod tests {
|
||||
recipe_base_dir: None,
|
||||
};
|
||||
|
||||
// Schedule the job and let it run — should not panic
|
||||
scheduler.add_scheduled_job(job, true).await.unwrap();
|
||||
sleep(Duration::from_millis(1500)).await;
|
||||
let error = scheduler.add_scheduled_job(job, true).await.unwrap_err();
|
||||
|
||||
// The job should have attempted to run (last_run set) but not crashed the scheduler
|
||||
let jobs = scheduler.list_scheduled_jobs().await;
|
||||
assert!(
|
||||
jobs[0].last_run.is_some(),
|
||||
"Job should have attempted to run without panicking"
|
||||
assert_eq!(
|
||||
error.to_string(),
|
||||
"Recipe load error: Invalid recipe: Recipe must specify at least one of `instructions` or `prompt`."
|
||||
);
|
||||
assert!(scheduler.list_scheduled_jobs().await.is_empty());
|
||||
assert!(!temp_dir.path().join("scheduled_recipes").exists());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use goose::recipe::validate_recipe::{
|
||||
validate_recipe_for_scheduling, validate_recipe_template_from_content, RecipeFileFormat,
|
||||
};
|
||||
|
||||
fn workspace_root() -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.parent()
|
||||
.and_then(Path::parent)
|
||||
.expect("goose crate must be inside the workspace crates directory")
|
||||
.to_path_buf()
|
||||
}
|
||||
|
||||
fn collect_yaml_files(directory: &Path, paths: &mut Vec<PathBuf>) {
|
||||
for entry in std::fs::read_dir(directory)
|
||||
.unwrap_or_else(|error| panic!("failed to read {}: {error}", directory.display()))
|
||||
{
|
||||
let path = entry.expect("failed to read directory entry").path();
|
||||
if path.is_dir() {
|
||||
collect_yaml_files(&path, paths);
|
||||
} else if path
|
||||
.extension()
|
||||
.is_some_and(|extension| extension == "yaml")
|
||||
{
|
||||
paths.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checked_in_recipes_match_cli_validation_for_scheduling() {
|
||||
let root = workspace_root();
|
||||
let recipes_root = root.join("documentation/src/pages/recipes/data/recipes");
|
||||
let mut paths = Vec::new();
|
||||
collect_yaml_files(&recipes_root, &mut paths);
|
||||
paths.sort();
|
||||
assert!(!paths.is_empty(), "expected checked-in recipe fixtures");
|
||||
|
||||
for path in paths {
|
||||
let content = std::fs::read_to_string(&path)
|
||||
.unwrap_or_else(|error| panic!("failed to read {}: {error}", path.display()));
|
||||
let recipe_dir = path
|
||||
.parent()
|
||||
.expect("recipe path must have a parent")
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
|
||||
let cli_result = validate_recipe_template_from_content(&content, Some(recipe_dir.clone()));
|
||||
let scheduling_result =
|
||||
validate_recipe_for_scheduling(&content, Some(recipe_dir), RecipeFileFormat::Yaml);
|
||||
|
||||
assert_eq!(
|
||||
scheduling_result.is_ok(),
|
||||
cli_result.is_ok(),
|
||||
"validation verdict differed for {}: CLI={:?}, scheduling={:?}",
|
||||
path.display(),
|
||||
cli_result.err(),
|
||||
scheduling_result.err()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -226,6 +226,41 @@ async fn parse_errors_do_not_reflect_recipe_contents() {
|
||||
assert!(scheduler.jobs.lock().await.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reports_actionable_schema_errors_without_reflecting_values() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let scheduler = Arc::new(MockScheduler::new());
|
||||
let tool = schedule_tool(&temp_dir, scheduler.clone());
|
||||
let cases = [
|
||||
(
|
||||
"missing-title.yaml",
|
||||
"description: Missing title\nprompt: Run safely\n".to_string(),
|
||||
"Invalid recipe: missing field `title`",
|
||||
"Missing title",
|
||||
),
|
||||
(
|
||||
"invalid-input-type.yaml",
|
||||
"title: Test\ndescription: hi\nprompt: Run {{ item }}\nparameters:\n - key: item\n input_type: yaml-secret-242\n requirement: required\n description: hi\n"
|
||||
.to_string(),
|
||||
"Invalid recipe: parameters[0].input_type is invalid",
|
||||
"yaml-secret-242",
|
||||
),
|
||||
];
|
||||
|
||||
for (name, content, expected, secret) in cases {
|
||||
let path = temp_dir.path().join(name);
|
||||
std::fs::write(&path, content).unwrap();
|
||||
|
||||
let message = create_schedule(&tool, &path).await.unwrap_err();
|
||||
|
||||
assert_eq!(message, expected);
|
||||
assert!(!message.contains(secret));
|
||||
}
|
||||
|
||||
assert!(scheduler.jobs.lock().await.is_empty());
|
||||
assert!(scheduler.validated_recipes.lock().await.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_non_regular_recipe_path() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user