diff --git a/oxa/oxa-cli/src/lead_page.rs b/oxa/oxa-cli/src/lead_page.rs index 80cbb8b8..21d56abf 100644 --- a/oxa/oxa-cli/src/lead_page.rs +++ b/oxa/oxa-cli/src/lead_page.rs @@ -32,11 +32,13 @@ fn action(value: Action) -> &'static str { Action::FixRequiredCi => "Fix required CI", Action::ResolveConflict => "Resolve conflict", Action::AddRemoves => "Add removal rationale", + Action::BuildIssue => "Build approved issue", Action::PromoteDraft => "Promote green draft", Action::Ready => "Ready to land", Action::WaitRequiredCi => "Wait for required CI", Action::WaitMergeability => "Wait for mergeability", Action::WaitReview => "Wait for review", + Action::WaitIssue => "Wait for issue intake", } } fn subject(value: &Subject) -> String { diff --git a/oxa/oxa-work/src/board.rs b/oxa/oxa-work/src/board.rs index 20650226..5279a154 100644 --- a/oxa/oxa-work/src/board.rs +++ b/oxa/oxa-work/src/board.rs @@ -5,6 +5,8 @@ use std::collections::{BTreeMap, BTreeSet}; mod claims; pub use claims::{Assignment, AssignmentBoard, AssignmentData, Claims, ReleaseRecord}; pub mod assignment; +mod intake; +pub use intake::{ImportState, Issue, Snapshot}; #[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)] #[serde(rename_all = "snake_case")] @@ -13,11 +15,13 @@ pub enum Action { FixRequiredCi, ResolveConflict, AddRemoves, + BuildIssue, PromoteDraft, Ready, WaitRequiredCi, WaitMergeability, WaitReview, + WaitIssue, } #[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] @@ -71,7 +75,16 @@ impl std::fmt::Debug for PullRequest { /// The adapter must return the complete open-PR snapshot or fail, never a partial page. pub trait GitHub { - fn open_pull_requests(&mut self) -> crate::Result>; + fn open_pull_requests(&mut self) -> crate::Result> { + Err("adapter must implement a complete board snapshot".into()) + } + /// One complete, caller-partitioned PR AND issue intake. Legacy PR adapters remain valid. + fn snapshot(&mut self) -> crate::Result { + Ok(Snapshot { + pull_requests: self.open_pull_requests()?, + issues: vec![], + }) + } } #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] @@ -89,7 +102,7 @@ pub fn valid_worker_id(id: &str) -> bool { !id.is_empty() && id.len() <= 128 && id.split([':', '/']).all(crate::valid_task_id) } -#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] pub struct AttemptState { pub utc_day: u64, pub attempts: u32, @@ -114,6 +127,7 @@ impl Default for Policy { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Entry { pub number: u64, + /// PR head, or frozen issue-source revision for BuildIssue/WaitIssue. Never a fabricated PR. pub head: String, pub release_pinned: bool, pub action: Action, @@ -145,7 +159,8 @@ impl Board { return Ok(board); } let mut seen = BTreeSet::new(); - for pr in github.open_pull_requests()? { + let snapshot = github.snapshot()?; + for pr in snapshot.pull_requests { if pr.number == 0 || pr.head.is_empty() || !seen.insert(pr.number) { return Err("invalid or duplicate pull request in board snapshot".into()); } @@ -174,25 +189,34 @@ impl Board { } else { Action::WaitReview }; - let saved = state.get(&pr.number); - let attempts_today = saved - .filter(|s| s.utc_day >= now / 86_400) - .map_or(0, |s| s.attempts); - let lease = saved.and_then(|s| s.lease.clone()); - let eligible = !matches!( + board.entries.push(intake::entry( + pr.number, + pr.head, + pr.release_pinned, action, - Action::WaitReview | Action::WaitRequiredCi | Action::WaitMergeability - ) && attempts_today < policy.max_attempts_per_day - && lease.as_ref().is_none_or(|l| l.expires_at <= now); - board.entries.push(Entry { - number: pr.number, - head: pr.head, - release_pinned: pr.release_pinned, + state.get(&pr.number), + policy, + now, + )); + } + for issue in snapshot.issues { + if issue.number == 0 || issue.revision.is_empty() || !seen.insert(issue.number) { + return Err("invalid or duplicate issue in board snapshot".into()); + } + let action = if issue.approved && !issue.has_open_pull_request { + Action::BuildIssue + } else { + Action::WaitIssue + }; + board.entries.push(intake::entry( + issue.number, + issue.revision, + issue.release_pinned, action, - lease, - attempts_today, - eligible, - }); + state.get(&issue.number), + policy, + now, + )); } board .entries diff --git a/oxa/oxa-work/src/board/assignment.rs b/oxa/oxa-work/src/board/assignment.rs index 515c8f4d..caeedec1 100644 --- a/oxa/oxa-work/src/board/assignment.rs +++ b/oxa/oxa-work/src/board/assignment.rs @@ -101,9 +101,11 @@ impl AssignmentPolicy { /// A manual pin narrows eligibility; it cannot bypass skills or action authority. pub fn allows(&self, worker: &Worker, action: Action) -> bool { let base_role = match action { - Action::FixFindings | Action::FixRequiredCi | Action::ResolveConflict | Action::AddRemoves => { - Role::Builder - } + Action::FixFindings + | Action::FixRequiredCi + | Action::ResolveConflict + | Action::AddRemoves + | Action::BuildIssue => Role::Builder, Action::PromoteDraft | Action::Ready => Role::Orchestrator, _ => return false, }; diff --git a/oxa/oxa-work/src/board/claims.rs b/oxa/oxa-work/src/board/claims.rs index dcc3795a..686395df 100644 --- a/oxa/oxa-work/src/board/claims.rs +++ b/oxa/oxa-work/src/board/claims.rs @@ -10,6 +10,7 @@ use std::path::{Path, PathBuf}; mod controls; pub use controls::{Assignment, AssignmentBoard, AssignmentData, ReleaseRecord}; +mod imports; #[derive(Deserialize, Serialize)] #[serde(deny_unknown_fields)] @@ -125,6 +126,7 @@ impl Claims { { return Err("invalid board claim identity or lease".into()); } + self.mark(number)?; Ok(saved) } @@ -196,7 +198,7 @@ impl Claims { ttl: u64, ) -> Result> { self.claim_checked(entry, policy, holder, (now, ttl), |row| { - Ok(row.data.policy.is_none()) + Ok(entry.action != Action::BuildIssue && row.data.policy.is_none()) }) } @@ -214,7 +216,7 @@ impl Claims { || !entry.eligible || matches!( entry.action, - Action::WaitReview | Action::WaitRequiredCi | Action::WaitMergeability + Action::WaitReview | Action::WaitRequiredCi | Action::WaitMergeability | Action::WaitIssue ) { return Ok(None); @@ -231,6 +233,9 @@ impl Claims { if self.established(entry.number)? { self.mark(entry.number)?; } + if row.data.import_hold { + return Ok(None); + } if !authorize(&mut row)? { return Ok(None); } diff --git a/oxa/oxa-work/src/board/claims/controls.rs b/oxa/oxa-work/src/board/claims/controls.rs index 157ef91a..eda2a454 100644 --- a/oxa/oxa-work/src/board/claims/controls.rs +++ b/oxa/oxa-work/src/board/claims/controls.rs @@ -12,6 +12,8 @@ pub struct AssignmentData { pub revision: u64, pub policy: Option, #[serde(default)] + pub import_hold: bool, + #[serde(default)] pub last_release: Option, } @@ -89,8 +91,17 @@ impl Claims { { return Err("invalid or duplicate worker catalog".into()); } + let mut board = Board::read(github, policy, &states, now)?; + for entry in &mut board.entries { + if assignments + .get(&entry.number) + .is_some_and(|row| row.data.import_hold) + { + entry.eligible = false; + } + } Ok(AssignmentBoard { - board: Board::read(github, policy, &states, now)?, + board, assignments, workers: workers.to_vec(), }) diff --git a/oxa/oxa-work/src/board/claims/imports.rs b/oxa/oxa-work/src/board/claims/imports.rs new file mode 100644 index 00000000..aae52441 --- /dev/null +++ b/oxa/oxa-work/src/board/claims/imports.rs @@ -0,0 +1,78 @@ +//! Bootstrap is restartable, refuses conflicting native facts, and never refunds attempts. +use super::*; +use crate::board::{assignment::ManualAuthority, ImportState}; +impl Claims { + /// Caller keeps activation OFF until the entire authoritative batch is imported and verified. + /// A partial I/O failure can be retried; an established, differing state is never overwritten. + pub fn import_snapshot( + &self, + authority: Option, + incoming: &BTreeMap, + now: u64, + ) -> Result<()> { + if authority.is_none() { + return Err("owner or lead import required".into()); + } + for (number, item) in incoming { + let state = &item.state; + if *number == 0 + || state.utc_day > now / 86_400 + || state.lease.as_ref().is_some_and(|l| { + state.attempts == 0 + || !valid_worker_id(&l.holder) + || l.generation != (state.utc_day, state.attempts) + || l.claimed_at.is_none_or(|at| at > now || at >= l.expires_at) + }) + { + return Err("invalid imported claim state".into()); + } + } + if Self::open(&self.root)?.uid != self.uid { + return Err("invalid import directory".into()); + } + let lock = io(File::open(&self.root))?; + io(lock.lock())?; + let mut prepared = vec![]; + for (number, item) in incoming { + let mut row = self.load(*number)?; + let exists = self.established(*number)?; + if exists && row.state != item.state { + return Err("import conflicts with established claims; reconciliation required".into()); + } + let held = item.excluded || item.state.lease.as_ref().is_some_and(|l| l.expires_at > now); + let changed = !exists || (held && !row.data.import_hold); + row.state = item.state.clone(); + row.data.import_hold |= held; + prepared.push((*number, row, exists, changed)); + } + for (number, row, exists, changed) in prepared { + if changed { + self.save(number, row, exists)?; + } + self.mark(number)?; + } + Ok(()) + } + /// Only after the trusted frontend verifies preserved queued/active work has ended. + pub fn clear_import_hold( + &self, + number: u64, + authority: Option, + expected_revision: u64, + ) -> Result<()> { + if authority.is_none() { + return Err("owner or lead reconciliation required".into()); + } + let _lock = self.lock(number)?; + let mut row = self.load(number)?; + if row.data.revision != expected_revision { + return Err("assignment changed; refresh it".into()); + } + if !row.data.import_hold { + return Ok(()); + } + row.data.import_hold = false; + self.save(number, row, true)?; + self.mark(number) + } +} diff --git a/oxa/oxa-work/src/board/intake.rs b/oxa/oxa-work/src/board/intake.rs new file mode 100644 index 00000000..6887ce82 --- /dev/null +++ b/oxa/oxa-work/src/board/intake.rs @@ -0,0 +1,51 @@ +//! Typed intake and conservative bootstrap data; no acquisition, partitioner or second store. +use super::*; +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct Issue { + pub number: u64, + pub revision: String, + pub release_pinned: bool, + /// Trusted intake approval, never inferred from arbitrary issue text. + pub approved: bool, + pub has_open_pull_request: bool, +} +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +pub struct Snapshot { + pub pull_requests: Vec, + pub issues: Vec, +} +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct ImportState { + pub state: AttemptState, + /// Queued OR active legacy work excludes native admission until explicitly reconciled. + pub excluded: bool, +} +pub(super) fn entry( + number: u64, + head: String, + release_pinned: bool, + action: Action, + saved: Option<&AttemptState>, + policy: &Policy, + now: u64, +) -> Entry { + let attempts_today = saved + .filter(|s| s.utc_day >= now / 86_400) + .map_or(0, |s| s.attempts); + let lease = saved.and_then(|s| s.lease.clone()); + let eligible = !matches!( + action, + Action::WaitReview | Action::WaitRequiredCi | Action::WaitMergeability | Action::WaitIssue + ) && attempts_today < policy.max_attempts_per_day + && lease.as_ref().is_none_or(|l| l.expires_at <= now); + Entry { + number, + head, + release_pinned, + action, + lease, + attempts_today, + eligible, + } +} diff --git a/oxa/oxa-work/tests/board_controls.rs b/oxa/oxa-work/tests/board_controls.rs index 73248499..0c52d6a7 100644 --- a/oxa/oxa-work/tests/board_controls.rs +++ b/oxa/oxa-work/tests/board_controls.rs @@ -1,5 +1,6 @@ use oxa_work::board::assignment::{Control, ManualAuthority, Pin, Role, Worker, WorkerKind}; use oxa_work::board::*; +use std::slice::from_ref; use std::{collections::BTreeMap, fs, os::unix::fs::DirBuilderExt, path::PathBuf}; struct Fixture(PathBuf); @@ -76,15 +77,21 @@ fn controls_require_authority_and_compare_revision_after_reopen() { .store() .control(3, Some(ManualAuthority::Lead), 0, Control::OwnerOnly(true)) .unwrap(); + let witness = fixture.0.join("3.board-claim.created"); + fs::remove_file(&witness).unwrap(); assert!(fixture .store() .control(3, Some(ManualAuthority::Owner), 0, Control::OwnerOnly(false)) .is_err()); + assert!(witness.exists()); let rows = fixture.store().assignments().unwrap(); assert_eq!(rows[&3].data.revision, 1); assert!(rows[&3].data.policy.as_ref().unwrap().owner_only); assert_eq!(rows[&3].state.attempts, 0); assert_eq!(rows[&3].assignee(10), None); + fs::remove_file(fixture.0.join("3.board-claim.json")).unwrap(); + fs::remove_file(fixture.0.join("3.board-claim.lock")).unwrap(); + assert!(fixture.store().assignments().is_err()); } #[test] @@ -166,6 +173,181 @@ fn one_snapshot_serves_all_workers_with_release_and_manual_pin_priority() { assert!(disabled.assignments.is_empty()); assert_eq!(github.0, 1); } +struct Intake(Snapshot, usize); +impl GitHub for Intake { + fn snapshot(&mut self) -> oxa_work::Result { + self.1 += 1; + Ok(self.0.clone()) + } +} +#[test] +fn typed_issue_intake_uses_one_snapshot_and_builder_authority() { + let fixture = Fixture::new(); + let mut worker = worker("slot-1"); + let mut source = Intake( + Snapshot { + pull_requests: FakeGitHub(0).open_pull_requests().unwrap(), + issues: (4..=6) + .map(|number| Issue { + number, + revision: "source-revision".into(), + release_pinned: true, + approved: number != 5, + has_open_pull_request: number == 6, + }) + .collect(), + }, + 0, + ); + let store = fixture.store(); + let disabled = store + .assignment_board(&mut source, &Policy::default(), from_ref(&worker), 10) + .unwrap(); + assert!(disabled.board.entries.is_empty()); + assert_eq!(source.1, 0); + let board = store + .assignment_board(&mut source, &policy(), from_ref(&worker), 10) + .unwrap(); + assert_eq!(source.1, 1); + assert_eq!(board.candidates(&worker.id).unwrap(), [3, 4, 1, 2]); + let issue = board.board.entries.iter().find(|e| e.number == 4).unwrap(); + assert_eq!(issue.action, Action::BuildIssue); + assert_eq!(issue.head, "source-revision"); + worker.roles = [Role::Reviewer].into(); + let denied = store.claim_for(issue, &policy(), &worker, 10, 10).unwrap(); + assert!(denied.is_none()); + let legacy = store.claim(issue, &policy(), &worker.id, 10, 10).unwrap(); + assert!(legacy.is_none()); + worker.roles = [Role::Builder].into(); + assert!(store + .claim_for(issue, &policy(), &worker, 10, 10) + .unwrap() + .is_some()); + for number in [5, 6] { + let waiting = board.board.entries.iter().find(|e| e.number == number).unwrap(); + assert_eq!(waiting.action, Action::WaitIssue); + assert!(!waiting.eligible); + assert!(store + .claim_for(waiting, &policy(), &worker, 10, 10) + .unwrap() + .is_none()); + } + source.0.issues[0].number = 3; + assert!(Board::read(&mut source, &policy(), &BTreeMap::new(), 10).is_err()); +} +#[test] +fn import_resumes_without_resetting_attempts_or_admitting_preserved_jobs() { + let fixture = Fixture::new(); + let store = fixture.store(); + let worker = worker("slot-1"); + let incoming: BTreeMap<_, _> = [ + ( + 3, + ImportState { + state: AttemptState { + utc_day: 0, + attempts: 1, + lease: Some(Lease { + holder: worker.id.clone(), + claimed_at: Some(10), + expires_at: 20, + generation: (0, 1), + }), + }, + excluded: false, + }, + ), + ( + 4, + ImportState { + state: AttemptState { + attempts: 2, + ..Default::default() + }, + excluded: true, + }, + ), + ] + .into(); + assert!(store.import_snapshot(None, &incoming, 11).is_err()); + assert_eq!(fs::read_dir(&fixture.0).unwrap().count(), 0); + let first = [(3, incoming[&3].clone())].into(); + store + .import_snapshot(Some(ManualAuthority::Owner), &first, 11) + .unwrap(); + store + .import_snapshot(Some(ManualAuthority::Owner), &incoming, 11) + .unwrap(); + store + .import_snapshot(Some(ManualAuthority::Owner), &incoming, 11) + .unwrap(); + let rows = store.assignments().unwrap(); + for number in [3, 4] { + assert_eq!(rows[&number].state, incoming[&number].state); + assert_eq!(rows[&number].data.revision, 1); + assert!(rows[&number].data.import_hold); + } + let mut issue = entry(); + issue.number = 4; + issue.action = Action::BuildIssue; + assert!(store + .claim_for(&issue, &policy(), &worker, 21, 10) + .unwrap() + .is_none()); + assert!(store + .claim(&entry(), &policy(), &worker.id, 21, 10) + .unwrap() + .is_none()); + let board = store + .assignment_board(&mut FakeGitHub(0), &policy(), from_ref(&worker), 21) + .unwrap(); + assert!(!board.candidates(&worker.id).unwrap().contains(&3)); + assert!(store.clear_import_hold(4, None, 1).is_err()); + assert!(store + .clear_import_hold(4, Some(ManualAuthority::Lead), 0) + .is_err()); + store + .clear_import_hold(4, Some(ManualAuthority::Lead), 1) + .unwrap(); + assert!(store + .claim_for(&issue, &policy(), &worker, 21, 10) + .unwrap() + .is_some()); + assert!(store + .import_snapshot(Some(ManualAuthority::Owner), &incoming, 21) + .is_err()); + assert_eq!(store.snapshot().unwrap()[&4].attempts, 3); +} +#[test] +fn invalid_or_conflicting_import_is_preflighted_before_any_state_write() { + let fixture = Fixture::new(); + let store = fixture.store(); + store + .claim(&entry(), &policy(), "codex:node/slot-1", 10, 10) + .unwrap(); + let mut incoming: BTreeMap<_, _> = [2, 3] + .map(|number| { + ( + number, + ImportState { + state: AttemptState::default(), + excluded: true, + }, + ) + }) + .into(); + assert!(store + .import_snapshot(Some(ManualAuthority::Owner), &incoming, 11) + .is_err()); + assert!(!fixture.0.join("2.board-claim.json").exists()); + incoming.remove(&3); + incoming.get_mut(&2).unwrap().state.utc_day = 1; + assert!(store + .import_snapshot(Some(ManualAuthority::Owner), &incoming, 11) + .is_err()); + assert!(!fixture.0.join("2.board-claim.json").exists()); + assert_eq!(store.snapshot().unwrap()[&3].attempts, 1); +} #[test] fn manual_assignment_and_unassignment_share_revision_fencing_and_attempts() {