Skip to content
Merged
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
287 changes: 284 additions & 3 deletions pkgs/fleet/crates/fleet-queue/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ use serde_json::{Value, json};
use std::time::{SystemTime, UNIX_EPOCH};

pub const PRIORITY_SCALE: [i64; 7] = [10, 20, 30, 40, 50, 70, 90];
pub const KINDS: [&str; 4] = ["pr_shepherd", "triage", "small_fix", "ci_investigation"];
pub const KINDS: [&str; 5] = [
"pr_shepherd",
"triage",
"small_fix",
"ci_investigation",
"second_pass",
];
// Both the old and the current names of the two repos that moved to the
// developing-today-forks org (2026-11-04) stay allowlisted: in-flight queue
// tasks carry dedupe keys with the old names and must keep validating,
Expand All @@ -75,7 +81,8 @@ triage: repo-health:v1:triage:<repo>:issue:<n>\n\
pr: repo-health:v1:pr:<repo>:pr:<n>\n\
pr-set: repo-health:v1:pr-set:<repo>:<n>-<n>[-...] (sorted)\n\
set: repo-health:v1:set:<origin issue>:owed:<repo>\n\
maintenance: repo-health:v1:maintenance:<repo>:<kind>:<YYYY-Www>";
maintenance: repo-health:v1:maintenance:<repo>:<kind>:<YYYY-Www>\n\
second-pass: repo-health:v1:second-pass:<repo>:<issue>:pass-<n>";

fn manual_schema_errors(p: &Value) -> Vec<String> {
let mut errs = Vec::new();
Expand Down Expand Up @@ -222,6 +229,92 @@ fn manual_schema_errors(p: &Value) -> Vec<String> {
.to_string(),
);
}
errs.extend(review_errors(p));
errs
}

/// The review payload block (design #2025 section 5.2; #2310): required
/// for kind second_pass, and shape-checked whenever a packet of any
/// kind carries one (the Stage B interim representation attaches it to
/// small_fix packets). gap_list and verdict_note_url are required, and
/// pass may not exceed pass_cap — the termination cap is enforced
/// here, in the enqueue code path, not by reviewer restraint.
fn review_errors(p: &Value) -> Vec<String> {
let mut errs = Vec::new();
let kind = p.get("kind").and_then(|k| k.as_str()).unwrap_or("");
let review = p.get("review");
if kind != "second_pass" && review.is_none() {
return errs;
}
let Some(review) = review.and_then(|r| r.as_object()) else {
errs.push(
"payload.review: required object for kind second_pass (design #2025 section 5.2)"
.to_string(),
);
return errs;
};
match review.get("verdict_note_url") {
Some(Value::String(u)) if !u.is_empty() => {}
_ => errs.push(
"payload.review.verdict_note_url: required non-empty string for second_pass packets"
.to_string(),
),
}
match review.get("gap_list") {
Some(Value::Array(a))
if !a.is_empty()
&& a.iter()
.all(|g| matches!(g, Value::String(s) if !s.is_empty())) => {}
_ => errs.push(
"payload.review.gap_list: required non-empty list of checkable gap strings for second_pass packets"
.to_string(),
),
}
let pass = review.get("pass").and_then(|v| v.as_i64());
let pass_cap = review.get("pass_cap").and_then(|v| v.as_i64());
match pass {
Some(n) if n >= 1 => {}
_ => errs.push(
"payload.review.pass: required positive integer (the pass being commissioned; the original work is pass 0)"
.to_string(),
),
}
match pass_cap {
Some(n) if n >= 1 => {}
_ => errs.push("payload.review.pass_cap: required positive integer".to_string()),
}
if let (Some(pass), Some(cap)) = (pass, pass_cap) {
if pass > cap {
errs.push(format!(
"payload.review.pass: pass {pass} exceeds pass_cap {cap} — the termination cap is enforced at enqueue (design #2025 section 5.1)"
));
}
}
if let Some(verdict) = review.get("verdict") {
if !matches!(verdict, Value::String(_)) {
errs.push("payload.review.verdict: must be a string".to_string());
}
}
if let Some(artifact) = review.get("artifact") {
if !artifact.is_object() {
errs.push("payload.review.artifact: must be an object {pr, issue, merged}".to_string());
}
}
if let Some(producer) = review.get("producer") {
if !producer.is_object() {
errs.push("payload.review.producer: must be an object {task_id, session, pr}".to_string());
}
}
if let Some(refs) = review.get("goal_context_refs") {
if !matches!(refs, Value::Array(a) if a.iter().all(|r| matches!(r, Value::String(_)))) {
errs.push("payload.review.goal_context_refs: must be a list of strings".to_string());
}
}
if let Some(floor) = review.get("route_floor") {
if !matches!(floor, Value::String(_)) {
errs.push("payload.review.route_floor: must be a string".to_string());
}
}
errs
}

Expand All @@ -240,6 +333,9 @@ pub struct DedupeMatch {
pub num: Option<i64>,
pub nums: Option<Vec<i64>>,
pub origin: Option<i64>,
/// The commissioned pass for the second-pass family (#2310);
/// None for every other family.
pub pass: Option<i64>,
}

fn all_digits(s: &str) -> bool {
Expand Down Expand Up @@ -277,12 +373,13 @@ fn match_dedupe(key: &str) -> Option<DedupeMatch> {
num: None,
nums: None,
origin: Some(origin),
pass: None,
});
}
}
return None;
}
for fam in ["fix", "triage", "pr-set", "pr", "maintenance"] {
for fam in ["fix", "triage", "pr-set", "pr", "maintenance", "second-pass"] {
let Some(after) = rest.strip_prefix(&format!("{fam}:")) else {
continue;
};
Expand All @@ -300,6 +397,27 @@ fn match_dedupe(key: &str) -> Option<DedupeMatch> {
num: Some(num),
nums: None,
origin: None,
pass: None,
});
}
}
}
"second-pass" => {
// repo-health:v1:second-pass:<repo>:<issue>:pass-<n>
// (design #2025 section 5.2): one live packet per
// (artifact, pass) by construction.
if let Some((issue_s, pass_s)) = tail.split_once(":pass-") {
if let (Some(num), Some(pass)) = (
canonical_number(issue_s),
canonical_number(pass_s),
) {
return Some(DedupeMatch {
family: fam.into(),
repo: repo.to_string(),
num: Some(num),
nums: None,
origin: None,
pass: Some(pass),
});
}
}
Expand All @@ -313,6 +431,7 @@ fn match_dedupe(key: &str) -> Option<DedupeMatch> {
num: Some(num),
nums: None,
origin: None,
pass: None,
});
}
}
Expand All @@ -335,6 +454,7 @@ fn match_dedupe(key: &str) -> Option<DedupeMatch> {
num: None,
nums: Some(nums),
origin: None,
pass: None,
});
}
}
Expand Down Expand Up @@ -372,6 +492,7 @@ fn match_dedupe(key: &str) -> Option<DedupeMatch> {
num: None,
nums: None,
origin: None,
pass: None,
});
}
}
Expand All @@ -392,6 +513,7 @@ fn kind_families(kind: &str) -> Option<&'static [&'static str]> {
"triage" => Some(&["triage", "maintenance"]),
"pr_shepherd" => Some(&["pr", "pr-set", "set"]),
"ci_investigation" => Some(&["fix", "triage", "pr"]),
"second_pass" => Some(&["second-pass"]),
_ => None,
}
}
Expand Down Expand Up @@ -478,6 +600,40 @@ fn dedupe_errors(packet: &Value, payload: &Value) -> Vec<String> {
}
}
}
"second-pass" => {
let n = m.num.unwrap_or(0);
match issue_no {
Some(ino) if n != ino => {
errors.push(format!(
"packet.dedupe_key: issue {n} in key does not match payload.issue.number {ino}"
));
}
Some(_) => {}
None => {
errors.push(format!(
"packet.dedupe_key: family 'second-pass' requires payload.issue.number {n} matching the key issue, but the payload carries no issue.number (#6446)"
));
}
}
let key_pass = m.pass.unwrap_or(0);
let review_pass = payload
.get("review")
.and_then(|r| r.get("pass"))
.and_then(|v| v.as_i64());
match review_pass {
Some(rp) if rp != key_pass => {
errors.push(format!(
"packet.dedupe_key: pass {key_pass} in key does not match payload.review.pass {rp}"
));
}
Some(_) => {}
None => {
errors.push(format!(
"packet.dedupe_key: family 'second-pass' requires payload.review.pass {key_pass} matching the key pass, but the payload carries no review.pass"
));
}
}
}
"pr" => {
let n = m.num.unwrap_or(0);
if !pr_numbers.is_empty() {
Expand Down Expand Up @@ -1542,4 +1698,129 @@ mod tests {
let _ = validate_not_before_at(&json!(s), "packet.not_before", 1_762_700_000);
}
}

fn second_pass_payload(review: serde_json::Value) -> Value {
let mut payload = payload_with(json!({"issue": {"number": 2310}}));
let obj = payload.as_object_mut().expect("payload object");
obj.insert("kind".to_string(), json!("second_pass"));
obj.insert("review".to_string(), review);
payload
}

fn valid_review() -> serde_json::Value {
json!({
"verdict": "second-pass",
"verdict_note_url": "https://github.com/developing-today/code/issues/2025#issuecomment-1",
"artifact": {"pr": 9001, "issue": 2310, "merged": false},
"pass": 1,
"pass_cap": 2,
"gap_list": ["ring-2 gap: no test pins the cap"],
"goal_context_refs": ["https://github.com/developing-today/code/issues/2025"],
"producer": {"task_id": 7325, "session": "ses_x", "pr": 9001},
"route_floor": "specialist",
})
}

#[test]
fn second_pass_packet_validates() {
let packet = json!({
"title": "Second pass 1 on #2310",
"priority": 20,
"dedupe_key": "repo-health:v1:second-pass:developing-today/code:2310:pass-1",
"payload": second_pass_payload(valid_review()),
});
let (errors, _warnings) = validate_packet(&packet);
assert!(errors.is_empty(), "well-formed second_pass packet must validate; got {errors:?}");
}

#[test]
fn second_pass_rejects_missing_gap_list() {
let mut review = valid_review();
review.as_object_mut().unwrap().remove("gap_list");
let errors = manual_schema_errors(&second_pass_payload(review));
assert!(
errors.iter().any(|e| e.contains("review.gap_list")),
"missing gap_list must be rejected; got {errors:?}"
);
}

#[test]
fn second_pass_rejects_missing_verdict_note_url() {
let mut review = valid_review();
review.as_object_mut().unwrap().remove("verdict_note_url");
let errors = manual_schema_errors(&second_pass_payload(review));
assert!(
errors.iter().any(|e| e.contains("review.verdict_note_url")),
"missing verdict_note_url must be rejected; got {errors:?}"
);
}

#[test]
fn second_pass_rejects_pass_above_cap() {
let mut review = valid_review();
review["pass"] = json!(3);
let errors = manual_schema_errors(&second_pass_payload(review));
assert!(
errors.iter().any(|e| e.contains("exceeds pass_cap")),
"pass > pass_cap must be rejected at enqueue; got {errors:?}"
);
}

#[test]
fn second_pass_requires_the_review_block() {
let mut payload = payload_with(json!({"issue": {"number": 2310}}));
payload
.as_object_mut()
.unwrap()
.insert("kind".to_string(), json!("second_pass"));
let errors = manual_schema_errors(&payload);
assert!(
errors.iter().any(|e| e.contains("payload.review")),
"second_pass without a review block must be rejected; got {errors:?}"
);
}

#[test]
fn second_pass_dedupe_family_matches_issue_and_pass() {
let identity = json!({"issue": {"number": 2310}, "review": {"pass": 1}});
assert!(
dedupe_errs(
"repo-health:v1:second-pass:developing-today/code:2310:pass-1",
"second_pass",
identity.clone(),
)
.is_empty()
);
// Same (issue, pass) is the same key by construction: a second
// commissioning collides on it instead of double-commissioning.
let m = match_dedupe("repo-health:v1:second-pass:developing-today/code:2310:pass-1")
.expect("second-pass key parses");
assert_eq!(m.family, "second-pass");
assert_eq!(m.num, Some(2310));
assert_eq!(m.pass, Some(1));
// Pass disagreement between key and payload is an error.
let errors = dedupe_errs(
"repo-health:v1:second-pass:developing-today/code:2310:pass-2",
"second_pass",
identity,
);
assert!(
errors.iter().any(|e| e.contains("pass 2 in key")),
"key pass must match review.pass; got {errors:?}"
);
// The second-pass family is not valid for other kinds, and
// other families are not valid for second_pass.
assert!(!dedupe_errs(
"repo-health:v1:second-pass:developing-today/code:2310:pass-1",
"small_fix",
json!({"issue": {"number": 2310}}),
)
.is_empty());
assert!(!dedupe_errs(
"repo-health:v1:fix:developing-today/code:issue:2310",
"second_pass",
json!({"issue": {"number": 2310}}),
)
.is_empty());
}
}