Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions crucible-broker/src/decide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ impl DecisionApi {
match (&question.kind, self) {
(QuestionKind::Score { .. }, DecisionApi::OpenAi) => Some(AskedAs::Score),
(QuestionKind::Score { .. }, DecisionApi::SystemOne) => Some(AskedAs::Choice),
(QuestionKind::Noul | QuestionKind::Choice { .. }, _) => None,
(QuestionKind::Noul | QuestionKind::Choice { .. } | QuestionKind::Pick { .. }, _) => {
None
}
}
}

Expand Down Expand Up @@ -161,7 +163,8 @@ fn system_one_body(
let mut body = json!({ "instructions": q.instructions });
match &q.kind {
QuestionKind::Noul => body["type"] = json!("noul"),
QuestionKind::Choice { options } | QuestionKind::Score { levels: options } => {
QuestionKind::Pick { .. } => body["type"] = json!("pick"),
QuestionKind::Choice { options, .. } | QuestionKind::Score { levels: options } => {
body["type"] = json!("choice");
body["criteria"] = Value::Object(
options
Expand Down Expand Up @@ -221,7 +224,12 @@ fn openai_body(model: &str, questions: &BTreeMap<QuestionId, Question>, state: &
"name": id.as_str(),
"instructions": q.instructions,
}),
QuestionKind::Choice { options } => {
QuestionKind::Pick { .. } => json!({
"type": "pick",
"name": id.as_str(),
"instructions": q.instructions,
}),
QuestionKind::Choice { options, .. } => {
let choices: Vec<Value> = options
.iter()
.map(|o| {
Expand Down Expand Up @@ -380,6 +388,7 @@ pub fn parse_response(
probabilities: BTreeMap::new(),
score: None,
asked_as,
labels: Vec::new(),
},
Reply::Answered(answer) => Answer {
asked_as,
Expand Down Expand Up @@ -443,12 +452,16 @@ fn probabilities(
{
distribution(probabilities)
}
(QuestionKind::Pick { .. }, _) => Err(invalid(format!(
"question {id:?} is a pick, which only a person answers"
))),
(kind, answer) => {
let asked = match (kind, asked_as) {
(QuestionKind::Noul, _) => "a noul",
(QuestionKind::Choice { .. }, _) => "a choice",
(QuestionKind::Score { .. }, Some(AskedAs::Choice)) => "a score asked as a choice",
(QuestionKind::Score { .. }, _) => "a score",
(QuestionKind::Pick { .. }, _) => "a pick",
};
let answered = match answer {
WireAnswer::Noul(_) => "a noul",
Expand Down Expand Up @@ -550,6 +563,7 @@ mod tests {
description: None,
},
],
multiple: false,
},
drop: vec![],
},
Expand Down
2 changes: 1 addition & 1 deletion crucible-broker/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ fn slack_escape(value: &str) -> String {
.replace('>', "&gt;")
}

fn template_value(value: &serde_json::Value) -> minijinja::Value {
pub fn template_value(value: &serde_json::Value) -> minijinja::Value {
match value {
serde_json::Value::Null => minijinja::Value::from(()),
serde_json::Value::Bool(value) => minijinja::Value::from(*value),
Expand Down
116 changes: 113 additions & 3 deletions crucible-contract/src/decision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,39 @@ pub struct ChoiceOption {
pub description: Option<String>,
}

fn is_false(b: &bool) -> bool {
!*b
}

/// Where a pick question's options come from: a list field of one of the route's dependencies,
/// read when the route opens its request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PickSource {
pub task: String,
pub field: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum QuestionKind {
Noul,
/// One declared label, or with `multiple` one or more.
Choice {
options: Vec<ChoiceOption>,
#[serde(default, skip_serializing_if = "is_false")]
multiple: bool,
},
/// Ordered levels, lowest first.
Score {
levels: Vec<ChoiceOption>,
},
/// One value, or with `multiple` one or more, from a list a dependency produced. A pick has
/// no labels, so no `when` may name it.
Pick {
source: PickSource,
#[serde(default, skip_serializing_if = "is_false")]
multiple: bool,
},
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -180,9 +202,28 @@ impl Question {
pub fn labels(&self) -> Vec<Label> {
match &self.kind {
QuestionKind::Noul => vec![Label(NOUL_YES.to_owned()), Label(NOUL_NO.to_owned())],
QuestionKind::Choice { options } | QuestionKind::Score { levels: options } => {
QuestionKind::Choice { options, .. } | QuestionKind::Score { levels: options } => {
options.iter().map(|o| o.label.clone()).collect()
}
QuestionKind::Pick { .. } => Vec::new(),
}
}

/// Whether an answer may hold more than one value.
pub fn multiple(&self) -> bool {
match &self.kind {
QuestionKind::Noul | QuestionKind::Score { .. } => false,
QuestionKind::Choice { multiple, .. } | QuestionKind::Pick { multiple, .. } => {
*multiple
}
}
}

/// The source of a pick question's options.
pub fn pick_source(&self) -> Option<&PickSource> {
match &self.kind {
QuestionKind::Pick { source, .. } => Some(source),
_ => None,
}
}

Expand All @@ -195,8 +236,8 @@ impl Question {
return Err(QuestionError::EmptyInstructions);
}
match &self.kind {
QuestionKind::Noul => {}
QuestionKind::Choice { options } => {
QuestionKind::Noul | QuestionKind::Pick { .. } => {}
QuestionKind::Choice { options, .. } => {
distinct(options).map_err(|fault| match fault {
OptionFault::TooFew { got } => QuestionError::TooFewOptions { got },
OptionFault::Duplicate { label } => QuestionError::DuplicateOption { label },
Expand Down Expand Up @@ -282,6 +323,7 @@ impl Question {
probabilities,
score: None,
asked_as: None,
labels: Vec::new(),
})
}

Expand Down Expand Up @@ -325,6 +367,7 @@ impl Question {
probabilities,
score: Some(score),
asked_as: None,
labels: Vec::new(),
})
}
}
Expand Down Expand Up @@ -400,6 +443,20 @@ pub struct Answer {
/// The form a model-decided score question was put to the decision API in.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub asked_as: Option<AskedAs>,
/// Every label chosen, for a question that takes more than one; `label` is then the first.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub labels: Vec<Label>,
}

impl Answer {
/// Whether the answer chose `label`: one of a multiple answer's labels, else its one label.
pub fn chose(&self, label: &Label) -> bool {
if self.labels.is_empty() {
&self.label == label
} else {
self.labels.contains(label)
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -433,6 +490,7 @@ mod tests {
description: None,
})
.collect(),
multiple: false,
},
drop: vec![],
}
Expand Down Expand Up @@ -618,6 +676,58 @@ mod tests {
assert_eq!(v["urgent"]["probabilities"]["no"], 0.1);
}

#[test]
fn a_multiple_choice_and_a_pick_round_trip_and_a_single_choice_omits_multiple() {
let multiple: Question = serde_json::from_value(serde_json::json!({
"instructions": "which checks?",
"type": "choice",
"options": [{"label": "lint"}, {"label": "smoke"}],
"multiple": true,
}))
.unwrap();
assert!(multiple.multiple());
assert_eq!(multiple.labels(), vec![label("lint"), label("smoke")]);
let pick: Question = serde_json::from_value(serde_json::json!({
"instructions": "which regions?",
"type": "pick",
"source": {"task": "plan", "field": "regions"},
}))
.unwrap();
assert!(!pick.multiple());
assert!(pick.labels().is_empty());
assert_eq!(
pick.pick_source().map(|s| s.field.as_str()),
Some("regions")
);
pick.validate().unwrap();
for q in [&multiple, &pick] {
let text = serde_json::to_string(q).unwrap();
assert_eq!(&serde_json::from_str::<Question>(&text).unwrap(), q);
}
let single = serde_json::to_value(choice(&["a", "b"])).unwrap();
assert!(single.get("multiple").is_none(), "{single}");
}

#[test]
fn an_answer_chose_its_label_or_any_of_its_labels() {
let one = Answer {
label: label("a"),
confidence: 1.0,
probabilities: BTreeMap::new(),
score: None,
asked_as: None,
labels: Vec::new(),
};
assert!(one.chose(&label("a")) && !one.chose(&label("b")));
let many = Answer {
labels: vec![label("a"), label("c")],
..one
};
assert!(many.chose(&label("a")) && many.chose(&label("c")) && !many.chose(&label("b")));
let wire = serde_json::to_value(&many).unwrap();
assert_eq!(wire["labels"], serde_json::json!(["a", "c"]));
}

#[test]
fn a_score_answers_its_levels_in_declared_order() {
assert_eq!(
Expand Down
Loading
Loading