From a73c754fd2c4ab8d76e85d1b1cb1a2ba141ea39c Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 05:49:43 +0100 Subject: [PATCH 1/5] Add typed landing operations to the existing publisher for #307 --- oxa/oxa-work/src/board/queue.rs | 2 +- oxa/oxa-work/src/lib.rs | 4 + oxa/oxa-work/src/publish.rs | 5 + oxa/oxa-work/src/publish/landing.rs | 379 ++++++++++++++++++++++++++++ 4 files changed, 389 insertions(+), 1 deletion(-) create mode 100644 oxa/oxa-work/src/publish/landing.rs diff --git a/oxa/oxa-work/src/board/queue.rs b/oxa/oxa-work/src/board/queue.rs index ee36321a6..c877ed3dd 100644 --- a/oxa/oxa-work/src/board/queue.rs +++ b/oxa/oxa-work/src/board/queue.rs @@ -183,7 +183,7 @@ fn node_id(value: &Value) -> Option<&str> { value.as_str().filter(|id| is_node_id(id)) } -pub(super) fn is_node_id(id: &str) -> bool { +pub(crate) fn is_node_id(id: &str) -> bool { (1..=128).contains(&id.len()) && id .bytes() diff --git a/oxa/oxa-work/src/lib.rs b/oxa/oxa-work/src/lib.rs index 7d99bb5c5..37ee8ffb2 100644 --- a/oxa/oxa-work/src/lib.rs +++ b/oxa/oxa-work/src/lib.rs @@ -55,6 +55,10 @@ pub use github::pull_request_task; pub mod incidents; pub mod situation; pub mod standing; +pub use publish::{ + Admission, AdmissionAcknowledgment, AdmissionReceipt, DeliveryReceipt, LandingPublisher, NoticeIntent, + RejectedLanding, RetryIntent, +}; #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] #[serde(deny_unknown_fields)] diff --git a/oxa/oxa-work/src/publish.rs b/oxa/oxa-work/src/publish.rs index 37888e9ea..d7aa53157 100644 --- a/oxa/oxa-work/src/publish.rs +++ b/oxa/oxa-work/src/publish.rs @@ -1,6 +1,11 @@ //! Explicit publishing of the accepted commit; existing queued PRs are never pushed. use super::{checked, state, tree, CheckpointObserver, LockedTask, Outcome, Record, Result, RunPolicy, Task}; use state::Step; +mod landing; +pub use landing::{ + Admission, AdmissionAcknowledgment, AdmissionReceipt, DeliveryReceipt, LandingPublisher, NoticeIntent, + RejectedLanding, RetryIntent, +}; use std::os::{fd::AsRawFd, unix::process::CommandExt}; use std::{ fs::File, diff --git a/oxa/oxa-work/src/publish/landing.rs b/oxa/oxa-work/src/publish/landing.rs new file mode 100644 index 000000000..9a19592ef --- /dev/null +++ b/oxa/oxa-work/src/publish/landing.rs @@ -0,0 +1,379 @@ +//! Typed landing operations over the canonical publisher's GitHub transport. +use super::{gh, json, repository}; +use crate::{ + board::queue::{is_node_id, is_object_id}, + checked, git, Result, RunPolicy, +}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::{fs::File, path::Path}; + +const ADD_COMMENT: &str = "mutation($pr:ID!,$body:String!){addComment(input:{subjectId:$pr,body:$body}){ + commentEdge{node{id repository{id} pullRequest{id} author{__typename + ... on User{id} ... on Bot{id}} createdAt}}}}"; +const ENQUEUE: &str = "mutation($pr:ID!,$head:GitObjectID!){enqueuePullRequest(input:{ + pullRequestId:$pr,expectedHeadOid:$head}){mergeQueueEntry{id enqueuedAt + mergeQueue{id repository{id}} pullRequest{id headRefOid} headCommit{oid}}}}"; + +/// Identity supplied by the board Reader and matched to the owner journal. +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +pub struct RejectedLanding { + pub repository_id: String, + pub queue_id: String, + pub pull_request_id: String, + pub pr_head: String, + pub rejected_group: String, + pub removal_id: String, + pub removed_at: u64, +} + +impl RejectedLanding { + fn check(&self) -> Result<()> { + if ![ + &self.repository_id, + &self.queue_id, + &self.pull_request_id, + &self.removal_id, + ] + .into_iter() + .all(|id| is_node_id(id)) + || !is_object_id(&self.pr_head) + || !is_object_id(&self.rejected_group) + || self.rejected_group == self.pr_head + || self.removed_at == 0 + { + return Err("invalid rejected landing identity".into()); + } + Ok(()) + } +} + +/// A journal reservation for a single notification of the rejected PR author. +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +pub struct NoticeIntent { + pub rejection: RejectedLanding, + pub recipient_id: String, + pub recipient_login: String, + pub publisher_id: String, +} + +impl NoticeIntent { + fn body(&self, failed_tests: &[&str]) -> Result { + self.rejection.check()?; + let login = self + .recipient_login + .strip_suffix("[bot]") + .unwrap_or(&self.recipient_login); + if !is_node_id(&self.recipient_id) + || !is_node_id(&self.publisher_id) + || login.len() > 100 + || !login.as_bytes().first().is_some_and(u8::is_ascii_alphanumeric) + || !login + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"-_".contains(&b)) + || failed_tests.is_empty() + || failed_tests.len() > 128 + { + return Err("invalid landing notice".into()); + } + let mut body = format!( + "@{login} required checks rejected merge-group commit `{}` for PR head `{}`. Failed tests:", + self.rejection.rejected_group, self.rejection.pr_head + ); + for name in failed_tests { + if name.is_empty() || name.len() > 512 || name.chars().any(char::is_control) { + return Err("invalid reported test name".into()); + } + body.push_str("\n- `"); + body.push_str(&name.replace('`', "’")); + body.push('`'); + } + Ok(body) + } +} + +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +pub struct DeliveryReceipt { + pub intent: NoticeIntent, + pub comment_id: String, + pub created_at: u64, +} + +/// A journal reservation authorized by the landing policy after confirmed delivery. +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +pub struct RetryIntent { + pub delivery: DeliveryReceipt, + pub observed_main: String, + pub observed_at: u64, + pub last_admission_at: u64, +} + +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +pub struct AdmissionReceipt { + pub acknowledgment: AdmissionAcknowledgment, + pub admitted_group: String, +} + +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +pub struct AdmissionAcknowledgment { + pub intent: RetryIntent, + pub queue_entry_id: String, + pub enqueued_at: u64, +} + +pub enum Admission { + Pending(AdmissionAcknowledgment), + Confirmed(AdmissionReceipt), +} + +impl AdmissionAcknowledgment { + /// Reconcile only the entry acknowledged by the mutation. Its group may + /// have been assigned after dispatch and read through the board Reader. + pub fn reconcile(&self, entry: &Value) -> Result { + let result = admission(&self.intent, entry)?; + let observed = match &result { + Admission::Pending(observed) => observed, + Admission::Confirmed(observed) => &observed.acknowledgment, + }; + if observed != self { + return Err("queue entry changed since publication acknowledgment".into()); + } + Ok(result) + } +} + +/// Only the canonical publisher may dispatch these operations. The caller must +/// hold the owner journal lock, reserve the intent, and revalidate current +/// Reader evidence before calling. An ambiguous outcome is never a retry signal. +pub struct LandingPublisher<'a> { + repo: &'a Path, + policy: &'a RunPolicy, + gh_binary: &'a Path, + journal: &'a File, +} + +impl<'a> LandingPublisher<'a> { + pub fn new(repo: &'a Path, policy: &'a RunPolicy, gh_binary: &'a Path, journal: &'a File) -> Self { + Self { + repo, + policy, + gh_binary, + journal, + } + } + + fn call(&self, args: &[&str]) -> Result { + json(&gh(self.gh_binary, self.policy, self.repo, self.journal, args)?) + .map_err(|_| "invalid GitHub landing publication response".into()) + } + + fn graphql(&self, host: &str, query: &str, fields: &[&str]) -> Result { + let query = format!("query={query}"); + let mut args = vec!["api", "graphql", "--hostname", host, "-f", &query]; + for field in fields { + args.extend(["-f", field]); + } + data(self.call(&args)?) + } + + fn host(&self, intent: &NoticeIntent) -> Result { + if self.policy.offline { + return Err("publishing is unavailable in offline mode".into()); + } + let remote = checked(git( + self.policy, + self.repo, + &["remote", "get-url", "--push", "--all", "origin"], + ))?; + if remote.lines().count() != 1 { + return Err("publishing requires one origin push URL".into()); + } + let origin = self.call(&["repo", "view", &remote, "--json", "id,nameWithOwner,url"])?; + if origin["id"].as_str() != Some(&intent.rejection.repository_id) { + return Err("landing reservation does not match publication origin".into()); + } + let host = repository(&origin)?.host; + let viewer = self.graphql(&host, "query{viewer{id}}", &[])?; + if viewer["viewer"]["id"].as_str() != Some(&intent.publisher_id) { + return Err("landing reservation does not match the publisher identity".into()); + } + Ok(host) + } + + pub fn notice(&self, intent: &NoticeIntent, failed_tests: &[&str]) -> Result { + let body = intent.body(failed_tests)?; + let host = self.host(intent)?; + let result = self.graphql( + &host, + ADD_COMMENT, + &[ + &format!("pr={}", intent.rejection.pull_request_id), + &format!("body={body}"), + ], + )?; + delivery(intent, &result["addComment"]["commentEdge"]["node"]) + } + + /// A queue acknowledgment may precede assignment of a merge group. In + /// that case the owner journal retains the pending reservation. + pub fn enqueue(&self, intent: &RetryIntent) -> Result { + let rejection = &intent.delivery.intent.rejection; + rejection.check()?; + if !is_node_id(&intent.delivery.comment_id) + || intent.delivery.created_at <= rejection.removed_at + || intent.observed_at < intent.delivery.created_at + || intent.observed_at.saturating_sub(intent.last_admission_at) < 1800 + || intent.last_admission_at >= rejection.removed_at + || !is_object_id(&intent.observed_main) + { + return Err("invalid landing retry reservation".into()); + } + let host = self.host(&intent.delivery.intent)?; + let result = self.graphql( + &host, + ENQUEUE, + &[ + &format!("pr={}", rejection.pull_request_id), + &format!("head={}", rejection.pr_head), + ], + )?; + admission(intent, &result["enqueuePullRequest"]["mergeQueueEntry"]) + } +} + +fn data(response: Value) -> Result { + if response.get("errors").is_some() { + return Err("GitHub landing publication response contained errors".into()); + } + match response { + Value::Object(mut fields) => fields.remove("data").filter(Value::is_object), + _ => None, + } + .ok_or_else(|| "missing GitHub landing publication data".into()) +} + +fn receipt_id(value: &Value) -> Result { + value + .as_str() + .filter(|id| is_node_id(id)) + .map(str::to_owned) + .ok_or_else(|| "missing GitHub landing receipt identity".into()) +} + +fn timestamp(value: &Value) -> Option { + oxa_session::parse_utc_seconds(value.as_str()?) +} + +fn delivery(intent: &NoticeIntent, node: &Value) -> Result { + if node["repository"]["id"].as_str() != Some(&intent.rejection.repository_id) + || node["pullRequest"]["id"].as_str() != Some(&intent.rejection.pull_request_id) + || !matches!(node["author"]["__typename"].as_str(), Some("User" | "Bot")) + || node["author"]["id"].as_str() != Some(&intent.publisher_id) + { + return Err("delivery receipt does not match the landing reservation".into()); + } + Ok(DeliveryReceipt { + intent: intent.clone(), + comment_id: receipt_id(&node["id"])?, + created_at: timestamp(&node["createdAt"]) + .filter(|time| *time > intent.rejection.removed_at) + .ok_or("invalid delivery receipt timestamp")?, + }) +} + +fn admission(intent: &RetryIntent, entry: &Value) -> Result { + let rejection = &intent.delivery.intent.rejection; + if entry["mergeQueue"]["repository"]["id"].as_str() != Some(&rejection.repository_id) + || entry["mergeQueue"]["id"].as_str() != Some(&rejection.queue_id) + || entry["pullRequest"]["id"].as_str() != Some(&rejection.pull_request_id) + || entry["pullRequest"]["headRefOid"].as_str() != Some(&rejection.pr_head) + { + return Err("admission acknowledgment does not match the landing reservation".into()); + } + let queue_entry_id = receipt_id(&entry["id"])?; + let enqueued_at = timestamp(&entry["enqueuedAt"]) + .filter(|time| *time >= intent.observed_at && *time > intent.delivery.created_at) + .ok_or("invalid landing admission timestamp")?; + let acknowledgment = AdmissionAcknowledgment { + intent: intent.clone(), + queue_entry_id, + enqueued_at, + }; + if entry.get("headCommit") == Some(&Value::Null) { + return Ok(Admission::Pending(acknowledgment)); + } + let group = entry["headCommit"]["oid"] + .as_str() + .filter(|group| { + is_object_id(group) && *group != rejection.pr_head && *group != rejection.rejected_group + }) + .ok_or("invalid newly admitted merge-group commit")?; + Ok(Admission::Confirmed(AdmissionReceipt { + acknowledgment, + admitted_group: group.to_owned(), + })) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn typed_landing_receipts_refuse_partial_and_mismatched_evidence() { + let intent = NoticeIntent { + rejection: RejectedLanding { + repository_id: "R_repo".into(), + queue_id: "MQ_main".into(), + pull_request_id: "PR_pull".into(), + pr_head: "a".repeat(40), + rejected_group: "b".repeat(40), + removal_id: "E_removed".into(), + removed_at: 3_500, + }, + recipient_id: "U_author".into(), + recipient_login: "synthetic-author".into(), + publisher_id: "U_publisher".into(), + }; + let body = intent.body(&["suite::test`@unrelated"]).unwrap(); + assert!(body.starts_with("@synthetic-author ")); + assert!(body.ends_with("`suite::test’@unrelated`")); + assert!(intent + .body(&["bad +name"]) + .is_err()); + let mut node = json!({ + "id":"IC_comment","repository":{"id":"R_repo"},"pullRequest":{"id":"PR_pull"}, + "author":{"__typename":"User","id":"U_publisher"},"createdAt":"1970-01-01T01:00:00Z" + }); + let delivered = delivery(&intent, &node).unwrap(); + node["author"]["id"] = json!("U_unrelated"); + assert!(delivery(&intent, &node).is_err()); + assert!(data(json!({"data":{"addComment": node},"errors":[{}]})).is_err()); + + let retry = RetryIntent { + delivery: delivered, + observed_main: "c".repeat(40), + observed_at: 3_800, + last_admission_at: 1_000, + }; + let mut entry = json!({ + "id":"MQE_entry","enqueuedAt":"1970-01-01T01:04:00Z", + "mergeQueue":{"id":"MQ_main","repository":{"id":"R_repo"}}, + "pullRequest":{"id":"PR_pull","headRefOid":intent.rejection.pr_head}, + "headCommit":null + }); + let Admission::Pending(pending) = admission(&retry, &entry).unwrap() else { + panic!("a missing group cannot certify admission"); + }; + entry["headCommit"] = json!({"oid": "d".repeat(40)}); + let Admission::Confirmed(admitted) = pending.reconcile(&entry).unwrap() else { + panic!("a matching acknowledged group must certify admission"); + }; + assert_eq!(admitted.admitted_group, "d".repeat(40)); + entry["id"] = json!("MQE_unrelated"); + assert!(pending.reconcile(&entry).is_err()); + entry["mergeQueue"]["id"] = json!("MQ_other"); + assert!(admission(&retry, &entry).is_err()); + } +} From 11bc1ad7dc20a435de4b5b7a64f7fc7043ec7f3f Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 05:55:35 +0100 Subject: [PATCH 2/5] Reserve landing publications in the existing owner journal for #307 --- oxa/oxa-work/src/shadow.rs | 25 +- oxa/oxa-work/src/shadow/landing_history.rs | 10 +- oxa/oxa-work/src/shadow/publication.rs | 348 +++++++++++++++++++++ 3 files changed, 374 insertions(+), 9 deletions(-) create mode 100644 oxa/oxa-work/src/shadow/publication.rs diff --git a/oxa/oxa-work/src/shadow.rs b/oxa/oxa-work/src/shadow.rs index 05c71d540..5a4af9fa9 100644 --- a/oxa/oxa-work/src/shadow.rs +++ b/oxa/oxa-work/src/shadow.rs @@ -15,6 +15,7 @@ use std::path::Path; use std::time::{Duration, Instant}; mod landing_history; +pub mod publication; use landing_history::{AdmissionEvidence, Admissions}; pub const INTERVAL: Duration = Duration::from_secs(300); @@ -49,6 +50,8 @@ struct Sample<'a> { landing: &'a BTreeMap, #[serde(skip_serializing_if = "Option::is_none")] landing_main: Option<&'a str>, + #[serde(skip_serializing_if = "Option::is_none")] + landing_publication: Option<&'a publication::Publication>, } pub(crate) const OWNER_FILE_FLAGS: libc::c_int = libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC; @@ -385,15 +388,9 @@ pub fn run_with_landing( matched_queue_admissions: &matched_queue_admissions, landing: &landing, landing_main: landing_main.as_deref(), + landing_publication: None, }; - let mut line = serde_json::to_vec(&sample).map_err(|_| "cannot encode shadow board")?; - line.push(b'\n'); - let length = log.metadata().map_err(|_| "cannot inspect shadow log")?.len(); - if length.saturating_add(line.len() as u64) > MAX_LOG_BYTES { - return Err("shadow log reached its size limit; rotate it while stopped".into()); - } - log.write_all(&line).map_err(|_| "cannot append shadow board")?; - log.sync_data().map_err(|_| "cannot sync shadow board")?; + append(log, &sample)?; if let Some(history) = &mut history { history.observe( &board, @@ -473,3 +470,15 @@ fn plan_matched_rejections( } } } + +fn append(log: &mut File, sample: &Sample<'_>) -> Result<()> { + let mut line = serde_json::to_vec(sample).map_err(|_| "cannot encode shadow board")?; + line.push(b'\n'); + let length = log.metadata().map_err(|_| "cannot inspect shadow log")?.len(); + if length.saturating_add(line.len() as u64) > MAX_LOG_BYTES { + return Err("shadow log reached its size limit; rotate it while stopped".into()); + } + log.write_all(&line).map_err(|_| "cannot append shadow board")?; + log.sync_data().map_err(|_| "cannot sync shadow board")?; + Ok(()) +} diff --git a/oxa/oxa-work/src/shadow/landing_history.rs b/oxa/oxa-work/src/shadow/landing_history.rs index d163f3490..39cf46edb 100644 --- a/oxa/oxa-work/src/shadow/landing_history.rs +++ b/oxa/oxa-work/src/shadow/landing_history.rs @@ -1,5 +1,8 @@ //! Correlate a verified rejection with an earlier owner-observed queue admission. -use super::{Board, File, QueueObservation, Read, Seek, SeekFrom, MAX_LOG_BYTES}; +use super::{ + publication::{Publication, Publications}, + Board, File, QueueObservation, Read, Seek, SeekFrom, MAX_LOG_BYTES, +}; use crate::board::queue::{is_object_id, Removal}; use crate::Result; use serde::{Deserialize, Serialize}; @@ -18,6 +21,7 @@ struct PriorSample { #[serde(default)] queue_rejections: Vec, landing_main: Option, + landing_publication: Option, } #[derive(Deserialize)] @@ -68,6 +72,7 @@ impl AdmissionEvidence { #[derive(Default)] pub(super) struct Admissions { + pub(super) publications: Publications, queued: BTreeMap, matched: BTreeMap, } @@ -97,6 +102,9 @@ impl Admissions { if prior.schema_version != 1 || prior.observed_at != prior.board.observed_at { return Err("invalid owner shadow journal sample".into()); } + if let Some(publication) = prior.landing_publication { + history.publications.observe(publication)?; + } if prior.landing.is_empty() { continue; } diff --git a/oxa/oxa-work/src/shadow/publication.rs b/oxa/oxa-work/src/shadow/publication.rs new file mode 100644 index 000000000..cc7cafb11 --- /dev/null +++ b/oxa/oxa-work/src/shadow/publication.rs @@ -0,0 +1,348 @@ +//! Opt-in landing dispatch through the canonical publisher under the owner journal lock. +use super::{append, open_log, Admissions, Board, File, Sample}; +use crate::{ + Admission, AdmissionAcknowledgment, AdmissionReceipt, DeliveryReceipt, LandingPublisher, NoticeIntent, + RejectedLanding, Result, RetryIntent, RunPolicy, +}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::{collections::BTreeMap, path::Path}; + +#[derive(Default)] +pub struct Policy { + pub enabled: bool, +} + +pub enum Approved { + Notice { + intent: NoticeIntent, + failed_tests: Vec, + }, + Retry { + intent: RetryIntent, + /// Count established by the existing Reader and owner journal. + prior_admissions: u8, + }, + Reconcile(AdmissionAcknowledgment), +} + +pub struct Authorization { + pub number: u64, + pub operation: Approved, +} + +#[derive(Clone, Deserialize, Serialize)] +#[serde(tag = "step", content = "evidence", rename_all = "snake_case")] +pub(super) enum Publication { + NoticeReserved(NoticeIntent), + Delivered(DeliveryReceipt), + RetryReserved(RetryIntent), + Acknowledged(AdmissionAcknowledgment), + Admitted(AdmissionReceipt), +} + +impl Publication { + fn rejection(&self) -> &RejectedLanding { + match self { + Self::NoticeReserved(intent) => &intent.rejection, + Self::Delivered(receipt) => &receipt.intent.rejection, + Self::RetryReserved(intent) => &intent.delivery.intent.rejection, + Self::Acknowledged(receipt) => &receipt.intent.delivery.intent.rejection, + Self::Admitted(receipt) => &receipt.acknowledgment.intent.delivery.intent.rejection, + } + } + + fn retry(&self) -> bool { + matches!( + self, + Self::RetryReserved(_) | Self::Acknowledged(_) | Self::Admitted(_) + ) + } + + fn follows(&self, previous: Option<&Self>) -> bool { + match (previous, self) { + (None, Self::NoticeReserved(_) | Self::RetryReserved(_)) => true, + (Some(Self::NoticeReserved(intent)), Self::Delivered(receipt)) => intent == &receipt.intent, + (Some(Self::RetryReserved(intent)), Self::Acknowledged(receipt)) => intent == &receipt.intent, + (Some(Self::RetryReserved(intent)), Self::Admitted(receipt)) => { + intent == &receipt.acknowledgment.intent + } + (Some(Self::Acknowledged(ack)), Self::Admitted(receipt)) => ack == &receipt.acknowledgment, + _ => false, + } + } +} + +#[derive(PartialEq, Eq, PartialOrd, Ord)] +struct PublicationKey { + repository: String, + pull_request: String, + head: String, + removal: Option, +} + +#[derive(Default)] +pub struct Publications { + latest: BTreeMap, +} + +impl Publications { + fn key(rejection: &RejectedLanding, retry: bool) -> PublicationKey { + PublicationKey { + repository: rejection.repository_id.clone(), + pull_request: rejection.pull_request_id.clone(), + head: rejection.pr_head.clone(), + removal: (!retry).then(|| rejection.removal_id.clone()), + } + } + + pub(super) fn observe(&mut self, record: Publication) -> Result<()> { + let rejection = record.rejection(); + let key = Self::key(rejection, record.retry()); + if !record.follows(self.latest.get(&key)) { + return Err("invalid landing publication journal transition".into()); + } + if let Publication::RetryReserved(intent) = &record { + if !matches!( + self.latest.get(&Self::key(rejection, false)), + Some(Publication::Delivered(receipt)) if *receipt == intent.delivery + ) { + return Err("landing retry requires a journaled delivery receipt".into()); + } + } + self.latest.insert(key, record); + Ok(()) + } + + pub fn delivered(&self, rejection: &RejectedLanding) -> Option<&DeliveryReceipt> { + match self.prior(rejection, false) { + Some(Publication::Delivered(receipt)) if &receipt.intent.rejection == rejection => Some(receipt), + _ => None, + } + } + + pub fn pending_admission(&self, rejection: &RejectedLanding) -> Option<&AdmissionAcknowledgment> { + match self.prior(rejection, true) { + Some(Publication::Acknowledged(receipt)) + if &receipt.intent.delivery.intent.rejection == rejection => + { + Some(receipt) + } + _ => None, + } + } + + pub fn attempted_retry(&self, rejection: &RejectedLanding) -> bool { + self.prior(rejection, true).is_some() + } + + pub fn admitted(&self, rejection: &RejectedLanding) -> Option<&AdmissionReceipt> { + match self.prior(rejection, true) { + Some(Publication::Admitted(receipt)) => Some(receipt), + _ => None, + } + } + + fn prior(&self, rejection: &RejectedLanding, retry: bool) -> Option<&Publication> { + self.latest.get(&Self::key(rejection, retry)) + } +} + +fn record(log: &mut File, board: &Board, publication: &Publication) -> Result<()> { + append( + log, + &Sample { + schema_version: 1, + observed_at: board.observed_at, + board, + would_take: Vec::new(), + reviews: &BTreeMap::new(), + queue_rejections: &[], + matched_queue_admissions: &BTreeMap::new(), + landing: &BTreeMap::new(), + landing_main: None, + landing_publication: Some(publication), + }, + ) +} + +/// The existing Reader supplies the fresh board and authorization after the +/// journal lock is acquired. Reconciliation reads the precise acknowledged +/// entry with that Reader. With the default policy neither callback is used. +pub fn run_once( + policy: &Policy, + journal_path: &Path, + repo: &Path, + run: &RunPolicy, + gh_binary: &Path, + read: impl FnOnce(&Publications) -> Result<(Board, Option)>, + read_acknowledged: impl FnOnce(&AdmissionAcknowledgment) -> Result, +) -> Result<()> { + if !policy.enabled || run.offline { + return Ok(()); + } + let mut log = open_log(journal_path)?; + let history = Admissions::load(&mut log)?; + let (board, action) = read(&history.publications)?; + advance(&mut log, &history.publications, &board, action, |file, action| { + let publisher = LandingPublisher::new(repo, run, gh_binary, file); + match action { + Approved::Notice { intent, failed_tests } => { + let names: Vec<_> = failed_tests.iter().map(String::as_str).collect(); + Ok(Publication::Delivered(publisher.notice(&intent, &names)?)) + } + Approved::Retry { intent, .. } => match publisher.enqueue(&intent)? { + Admission::Pending(ack) => Ok(Publication::Acknowledged(ack)), + Admission::Confirmed(receipt) => Ok(Publication::Admitted(receipt)), + }, + Approved::Reconcile(ack) => match ack.reconcile(&read_acknowledged(&ack)?)? { + Admission::Pending(ack) => Ok(Publication::Acknowledged(ack)), + Admission::Confirmed(receipt) => Ok(Publication::Admitted(receipt)), + }, + } + }) +} + +fn advance( + log: &mut File, + history: &Publications, + board: &Board, + action: Option, + publish: impl FnOnce(&File, Approved) -> Result, +) -> Result<()> { + let Some(Authorization { + number, + operation: action, + }) = action + else { + return Ok(()); + }; + let (rejection, retry) = match &action { + Approved::Notice { intent, .. } => (&intent.rejection, false), + Approved::Retry { intent, .. } => (&intent.delivery.intent.rejection, true), + Approved::Reconcile(ack) => (&ack.intent.delivery.intent.rejection, true), + }; + let Some(entry) = board + .entries + .iter() + .find(|entry| entry.number == number && entry.head == rejection.pr_head) + else { + return Ok(()); + }; + let previous = history.prior(rejection, retry); + let reservation = match &action { + Approved::Notice { intent, .. } if previous.is_none() => Publication::NoticeReserved(intent.clone()), + Approved::Retry { + intent, + prior_admissions, + } if previous.is_none() + && (1..3).contains(prior_admissions) + && entry.action == crate::board::Action::Ready + && entry.eligible + && board.observed_at == intent.observed_at + && matches!( + history.prior(rejection, false), + Some(Publication::Delivered(receipt)) if *receipt == intent.delivery + ) => + { + Publication::RetryReserved(intent.clone()) + } + Approved::Reconcile(ack) if matches!(previous, Some(Publication::Acknowledged(previous)) if ack == previous) => + { + let receipt = publish(log, action)?; + return match receipt { + Publication::Acknowledged(_) => Ok(()), + Publication::Admitted(_) if receipt.follows(previous) => record(log, board, &receipt), + _ => Err("invalid landing reconciliation result".into()), + }; + } + _ => return Ok(()), + }; + record(log, board, &reservation)?; + let receipt = publish(log, action)?; + if !receipt.follows(Some(&reservation)) { + return Err("invalid landing publication result".into()); + } + record(log, board, &receipt) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::board::{Action, Entry}; + + #[test] + fn ambiguous_delivery_reservation_survives_reopening_and_refuses_redispatch() { + let temp = crate::test_dir::PrivateDir::new("landing-publication"); + let path = temp.path().join("owner.jsonl"); + let intent = NoticeIntent { + rejection: RejectedLanding { + repository_id: "R_repo".into(), + queue_id: "MQ_main".into(), + pull_request_id: "PR_pull".into(), + pr_head: "a".repeat(40), + rejected_group: "b".repeat(40), + removal_id: "E_removed".into(), + removed_at: 2, + }, + recipient_id: "U_author".into(), + recipient_login: "synthetic-author".into(), + publisher_id: "U_publisher".into(), + }; + let mut board = Board { + observed_at: 3, + entries: vec![Entry { + number: 42, + head: "c".repeat(40), + release_pinned: false, + action: Action::Ready, + lease: None, + attempts_today: 0, + eligible: true, + }], + }; + let authorization = |intent: NoticeIntent| { + Some(Authorization { + number: 42, + operation: Approved::Notice { + intent, + failed_tests: vec!["synthetic test".into()], + }, + }) + }; + { + let mut file = open_log(&path).unwrap(); + let history = Admissions::load(&mut file).unwrap(); + advance( + &mut file, + &history.publications, + &board, + authorization(intent.clone()), + |_, _| panic!("wrong head must not dispatch"), + ) + .unwrap(); + board.entries[0].head = intent.rejection.pr_head.clone(); + assert!(advance( + &mut file, + &history.publications, + &board, + authorization(intent.clone()), + |_, _| Err("transport disconnected after dispatch".into()) + ) + .is_err()); + } + let mut file = open_log(&path).unwrap(); + let history = Admissions::load(&mut file).unwrap(); + assert!(history.publications.delivered(&intent.rejection).is_none()); + let mut moved_recipient = intent; + moved_recipient.recipient_id = "U_replacement".into(); + advance( + &mut file, + &history.publications, + &board, + authorization(moved_recipient), + |_, _| panic!("ambiguous delivery must not dispatch again"), + ) + .unwrap(); + } +} From 7920fc2edf5659f2ddf77035b6fe853d537151f4 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 06:13:07 +0100 Subject: [PATCH 3/5] Share recipient validation and accept ordered same-second receipts for #307 --- oxa/oxa-work/src/board/queue.rs | 29 ++++++++++++++++++----------- oxa/oxa-work/src/publish/landing.rs | 19 ++++++------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/oxa/oxa-work/src/board/queue.rs b/oxa/oxa-work/src/board/queue.rs index c877ed3dd..dd2769af0 100644 --- a/oxa/oxa-work/src/board/queue.rs +++ b/oxa/oxa-work/src/board/queue.rs @@ -37,19 +37,12 @@ impl NotificationTarget { fn read(row: &Value) -> Option { let author = &row["author"]; let login = author["login"].as_str()?; - let account = match author["__typename"].as_str()? { - "User" => login, - "Bot" => login.strip_suffix("[bot]").unwrap_or(login), + let is_bot = match author["__typename"].as_str()? { + "User" => false, + "Bot" => true, _ => return None, }; - if account.len() > 100 - || !account.as_bytes().first().is_some_and(u8::is_ascii_alphanumeric) - || !account - .bytes() - .all(|b| b.is_ascii_alphanumeric() || b"-_".contains(&b)) - { - return None; - } + recipient_mention(login, is_bot)?; Some(Self { pull_request_id: node_id(&row["id"])?.to_owned(), author_id: node_id(&author["id"])?.to_owned(), @@ -58,6 +51,20 @@ impl NotificationTarget { } } +pub(crate) fn recipient_mention(login: &str, is_bot: bool) -> Option<&str> { + let account = if is_bot { + login.strip_suffix("[bot]").unwrap_or(login) + } else { + login + }; + (account.len() <= 100 + && account.as_bytes().first().is_some_and(u8::is_ascii_alphanumeric) + && account + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"-_".contains(&b))) + .then_some(account) +} + pub(crate) struct Removal<'a> { pub number: u64, pub event: &'a str, diff --git a/oxa/oxa-work/src/publish/landing.rs b/oxa/oxa-work/src/publish/landing.rs index 9a19592ef..b8ce610bf 100644 --- a/oxa/oxa-work/src/publish/landing.rs +++ b/oxa/oxa-work/src/publish/landing.rs @@ -1,7 +1,7 @@ //! Typed landing operations over the canonical publisher's GitHub transport. use super::{gh, json, repository}; use crate::{ - board::queue::{is_node_id, is_object_id}, + board::queue::{is_node_id, is_object_id, recipient_mention}, checked, git, Result, RunPolicy, }; use serde::{Deserialize, Serialize}; @@ -60,17 +60,10 @@ pub struct NoticeIntent { impl NoticeIntent { fn body(&self, failed_tests: &[&str]) -> Result { self.rejection.check()?; - let login = self - .recipient_login - .strip_suffix("[bot]") - .unwrap_or(&self.recipient_login); + let login = recipient_mention(&self.recipient_login, self.recipient_login.ends_with("[bot]")) + .ok_or("invalid landing notice recipient")?; if !is_node_id(&self.recipient_id) || !is_node_id(&self.publisher_id) - || login.len() > 100 - || !login.as_bytes().first().is_some_and(u8::is_ascii_alphanumeric) - || !login - .bytes() - .all(|b| b.is_ascii_alphanumeric() || b"-_".contains(&b)) || failed_tests.is_empty() || failed_tests.len() > 128 { @@ -292,7 +285,7 @@ fn admission(intent: &RetryIntent, entry: &Value) -> Result { } let queue_entry_id = receipt_id(&entry["id"])?; let enqueued_at = timestamp(&entry["enqueuedAt"]) - .filter(|time| *time >= intent.observed_at && *time > intent.delivery.created_at) + .filter(|time| *time >= intent.observed_at && *time >= intent.delivery.created_at) .ok_or("invalid landing admission timestamp")?; let acknowledgment = AdmissionAcknowledgment { intent: intent.clone(), @@ -354,11 +347,11 @@ name"]) let retry = RetryIntent { delivery: delivered, observed_main: "c".repeat(40), - observed_at: 3_800, + observed_at: 3_600, last_admission_at: 1_000, }; let mut entry = json!({ - "id":"MQE_entry","enqueuedAt":"1970-01-01T01:04:00Z", + "id":"MQE_entry","enqueuedAt":"1970-01-01T01:00:00Z", "mergeQueue":{"id":"MQ_main","repository":{"id":"R_repo"}}, "pullRequest":{"id":"PR_pull","headRefOid":intent.rejection.pr_head}, "headCommit":null From d68f6036bd93ab811257f2726d4dd496d55438ec Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:16:49 +0100 Subject: [PATCH 4/5] Reproduce same-second removal and delivery receipt rejection (#1303) --- oxa/oxa-work/src/publish/landing.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/oxa/oxa-work/src/publish/landing.rs b/oxa/oxa-work/src/publish/landing.rs index b8ce610bf..e1515b0d5 100644 --- a/oxa/oxa-work/src/publish/landing.rs +++ b/oxa/oxa-work/src/publish/landing.rs @@ -322,7 +322,7 @@ mod tests { pr_head: "a".repeat(40), rejected_group: "b".repeat(40), removal_id: "E_removed".into(), - removed_at: 3_500, + removed_at: 3_600, }, recipient_id: "U_author".into(), recipient_login: "synthetic-author".into(), @@ -337,8 +337,10 @@ name"]) .is_err()); let mut node = json!({ "id":"IC_comment","repository":{"id":"R_repo"},"pullRequest":{"id":"PR_pull"}, - "author":{"__typename":"User","id":"U_publisher"},"createdAt":"1970-01-01T01:00:00Z" + "author":{"__typename":"User","id":"U_publisher"},"createdAt":"1970-01-01T00:59:59Z" }); + assert!(delivery(&intent, &node).is_err()); + node["createdAt"] = json!("1970-01-01T01:00:00Z"); let delivered = delivery(&intent, &node).unwrap(); node["author"]["id"] = json!("U_unrelated"); assert!(delivery(&intent, &node).is_err()); From 04c60db26a74500be6c315cb7c2aed8213bf6545 Mon Sep 17 00:00:00 2001 From: Oscar Nordstrom <13300531+n0rpan@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:32:29 +0100 Subject: [PATCH 5/5] Accept ordered same-second queue removal and notice receipts (#1303) --- oxa/oxa-work/src/publish/landing.rs | 36 ++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/oxa/oxa-work/src/publish/landing.rs b/oxa/oxa-work/src/publish/landing.rs index e1515b0d5..73564257b 100644 --- a/oxa/oxa-work/src/publish/landing.rs +++ b/oxa/oxa-work/src/publish/landing.rs @@ -101,6 +101,23 @@ pub struct RetryIntent { pub last_admission_at: u64, } +impl RetryIntent { + fn check(&self) -> Result<()> { + let rejection = &self.delivery.intent.rejection; + rejection.check()?; + if !is_node_id(&self.delivery.comment_id) + || self.delivery.created_at < rejection.removed_at + || self.observed_at < self.delivery.created_at + || self.observed_at.saturating_sub(self.last_admission_at) < 1800 + || self.last_admission_at >= rejection.removed_at + || !is_object_id(&self.observed_main) + { + return Err("invalid landing retry reservation".into()); + } + Ok(()) + } +} + #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub struct AdmissionReceipt { pub acknowledgment: AdmissionAcknowledgment, @@ -210,17 +227,8 @@ impl<'a> LandingPublisher<'a> { /// A queue acknowledgment may precede assignment of a merge group. In /// that case the owner journal retains the pending reservation. pub fn enqueue(&self, intent: &RetryIntent) -> Result { + intent.check()?; let rejection = &intent.delivery.intent.rejection; - rejection.check()?; - if !is_node_id(&intent.delivery.comment_id) - || intent.delivery.created_at <= rejection.removed_at - || intent.observed_at < intent.delivery.created_at - || intent.observed_at.saturating_sub(intent.last_admission_at) < 1800 - || intent.last_admission_at >= rejection.removed_at - || !is_object_id(&intent.observed_main) - { - return Err("invalid landing retry reservation".into()); - } let host = self.host(&intent.delivery.intent)?; let result = self.graphql( &host, @@ -269,7 +277,9 @@ fn delivery(intent: &NoticeIntent, node: &Value) -> Result { intent: intent.clone(), comment_id: receipt_id(&node["id"])?, created_at: timestamp(&node["createdAt"]) - .filter(|time| *time > intent.rejection.removed_at) + // The mutation is sent after observing the removal. Both events + // can be reported in the same UTC second. + .filter(|time| *time >= intent.rejection.removed_at) .ok_or("invalid delivery receipt timestamp")?, }) } @@ -352,6 +362,10 @@ name"]) observed_at: 3_600, last_admission_at: 1_000, }; + assert!(retry.check().is_ok()); + let mut stale = retry.clone(); + stale.delivery.created_at -= 1; + assert!(stale.check().is_err()); let mut entry = json!({ "id":"MQE_entry","enqueuedAt":"1970-01-01T01:00:00Z", "mergeQueue":{"id":"MQ_main","repository":{"id":"R_repo"}},