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
104 changes: 104 additions & 0 deletions services/common/src/run_questions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const MAX_QUESTION_CHARS: usize = 600;
const MAX_WHY_CHARS: usize = 600;
const MAX_SUGGESTIONS: usize = 4;
const MAX_SUGGESTION_CHARS: usize = 200;
const MAX_CHECKED: usize = 8;

/// One thing a run needs a person to tell it.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
Expand All @@ -48,6 +49,20 @@ pub struct Question {
/// the code or the data makes it a question (see `stop_rules::screen`).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub evidence: Vec<String>,
/// The facts the run already checked before asking (each lookup, file
/// or tool result, in plain words): a person is never asked to repeat
/// what the run could see (see `stop_rules::screen`).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub checked: Vec<String>,
/// How the fix differs depending on the answer. A question whose answer
/// changes nothing is never asked.
#[serde(default, skip_serializing_if = "String::is_empty")]
pub changes: String,
/// For a question that rests on what a field means: where that field's
/// value is written (`owner/repo:path:line`, or a lookup id), read in
/// this run. A field's meaning is read from its writer, not its name.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub written_at: Vec<String>,
}

/// What a run asked when it stopped, and where it asked it.
Expand Down Expand Up @@ -160,6 +175,8 @@ pub fn validate(
/// - question: Which environments get the new limit?
/// why: Each environment has its own value in config/limits.yml.
/// options: Production only | Every environment
/// checked: config/limits.yml holds one value per environment
/// changes: production only edits one line; every environment edits three
/// ```
///
/// Older or looser answers still read: bullet or numbered lines become one
Expand Down Expand Up @@ -189,6 +206,12 @@ pub fn parse(detail: &str) -> Vec<Question> {
.filter(|s| !s.is_empty())
.map(str::to_string)
.collect();
} else if let (Some(c), Some(last)) = (field(item, "checked"), out.last_mut()) {
last.checked = split_list(c);
} else if let (Some(c), Some(last)) = (field(item, "changes"), out.last_mut()) {
last.changes = c.to_string();
} else if let (Some(w), Some(last)) = (field(item, "written_at"), out.last_mut()) {
last.written_at = split_list(w);
} else if let (Some(opts), Some(last)) = (
field(item, "options").or_else(|| field(item, "suggestions")),
out.last_mut(),
Expand Down Expand Up @@ -268,10 +291,34 @@ fn tidy(questions: Vec<Question>) -> Vec<Question> {
.filter(|s| !s.is_empty())
.take(MAX_SUGGESTIONS)
.collect(),
checked: q
.checked
.iter()
.map(|s| clip(s.trim(), MAX_SUGGESTION_CHARS))
.filter(|s| !s.is_empty())
.take(MAX_CHECKED)
.collect(),
changes: clip(q.changes.trim(), MAX_WHY_CHARS),
written_at: q
.written_at
.iter()
.map(|s| clip(s.trim(), MAX_SUGGESTION_CHARS))
.filter(|s| !s.is_empty())
.take(MAX_SUGGESTIONS)
.collect(),
})
.collect()
}

/// A `|`-separated list, trimmed, blanks dropped.
fn split_list(s: &str) -> Vec<String> {
s.split('|')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string)
.collect()
}

/// The questions as the ticket comment lists them (Markdown).
pub fn markdown(questions: &[Question]) -> String {
let mut out = String::new();
Expand All @@ -290,6 +337,12 @@ pub fn markdown(questions: &[Question]) -> String {
.join(", ")
));
}
if !q.checked.is_empty() {
out.push_str(&format!(" Already checked: {}\n", q.checked.join("; ")));
}
if !q.changes.is_empty() {
out.push_str(&format!(" What the answer changes: {}\n", q.changes));
}
if !q.evidence.is_empty() {
out.push_str(&format!(
" Evidence: {}\n",
Expand Down Expand Up @@ -317,6 +370,12 @@ pub fn plain(questions: &[Question]) -> String {
if !q.suggestions.is_empty() {
s.push_str(&format!(" Possible answers: {}.", q.suggestions.join("; ")));
}
if !q.checked.is_empty() {
s.push_str(&format!(" Already checked: {}.", q.checked.join("; ")));
}
if !q.changes.is_empty() {
s.push_str(&format!(" What the answer changes: {}", q.changes));
}
if !q.evidence.is_empty() {
s.push_str(&format!(" Evidence: {}.", q.evidence.join("; ")));
}
Expand Down Expand Up @@ -553,6 +612,51 @@ fn clip(s: &str, max_chars: usize) -> String {
mod tests {
use super::*;

#[test]
fn a_question_carries_what_was_checked_and_what_its_answer_changes() {
let qs = parse(
"- question: Should records missing from the index be rebuilt in full?\n \
why: the incremental sync skipped them\n \
checked: L1 the record is active in the app database | L2 it is missing from the index\n \
changes: yes: run the rebuild; no: change the inclusion rule\n \
written_at: acme/pipelines:indexer/build.py:88 | L3",
);
assert_eq!(qs.len(), 1);
assert_eq!(
qs[0].checked,
vec![
"L1 the record is active in the app database",
"L2 it is missing from the index"
]
);
assert_eq!(
qs[0].changes,
"yes: run the rebuild; no: change the inclusion rule"
);
assert_eq!(
qs[0].written_at,
vec!["acme/pipelines:indexer/build.py:88", "L3"]
);
let md = markdown(&qs);
assert!(
md.contains("Already checked: L1 the record is active"),
"{md}"
);
assert!(
md.contains("What the answer changes: yes: run the rebuild"),
"{md}"
);
let text = plain(&qs);
assert!(text.contains("Already checked: L1"), "{text}");
// Stored and read back with them.
let json = serde_json::to_string(&qs[0]).unwrap();
let back: Question = serde_json::from_str(&json).unwrap();
assert_eq!(back, qs[0]);
// An older stored question reads without them.
let old: Question = serde_json::from_str(r#"{"id":"q1","question":"A?"}"#).unwrap();
assert!(old.checked.is_empty() && old.changes.is_empty());
}

fn answer(q: &str, a: &str) -> Answer {
Answer {
question_id: "q1".into(),
Expand Down
8 changes: 7 additions & 1 deletion services/common/src/stop_rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! When a run may stop, and how it says why, for every team.
//!
//! A run stops for a person only when a person is what it needs. Four rules,
//! A run stops for a person only when a person is what it needs. Five rules,
//! each enforced here and called where a run would stop:
//!
//! 1. [`tooling`]: the run's own tools failing (a missing dependency, a
Expand All @@ -20,6 +20,12 @@
//! the same failing test or ticket ends the run as covered by it, linked
//! in the run summary and on the ticket, unless the ticket asks for a
//! separate change.
//! 5. [`screen::asking_rule`]: a question to a person says what the run
//! already checked and how the answer changes the fix, can't be answered
//! by the run's own tools, data sources or code, rests on no field whose
//! writer the run never read, and waits until conflicting findings about
//! the same record are settled. Asked too early twice, the run reports
//! its findings and the one next step a person takes instead.

pub mod environment;
pub mod existing_work;
Expand Down
Loading
Loading