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
16 changes: 16 additions & 0 deletions oxa/docs/work.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ and verified merge-group rejection evidence in `queue_rejections`, including the
removal event time.
Use `--queue-evidence` to record only the rejection evidence alongside the board.

To verify the provenance of test-report archives, pass `--queue-report-policy FILE` with an owner-only
JSON file in a private directory. Set the trusted workflow path, the exact names
of required checks that consume those reports, and every JUnit producer and artifact.

```json
{
"workflow_path": ".github/workflows/checks.yml",
"required_check_names": ["workspace test gate"],
"reports": [{"producer_name": "Workspace shard 1", "artifact_name": "test-report-1"}]
}
```

That option also enables queue evidence. The shadow journal records the checked
run, attempt, required check IDs, and SHA-256 digests after verifying every
configured archive for that attempt.

Limit: 100 PRs/items per connection. Reported checks are matched against the base's
classic protection and active repository/organization rulesets; missing required
contexts and unknown/no required checks stay pending. Review
Expand Down
65 changes: 41 additions & 24 deletions oxa/oxa-cli/src/work_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,32 +226,14 @@ impl<'a> Reader<'a> {
let invalid = QueryError::Other;
let (owner, name) = self.repo.split_once('/').ok_or(invalid("expected OWNER/REPO"))?;
let query = format!("query{{repository(owner:{owner:?},name:{name:?}){{{fields}}}}}");
let mut command = Command::new(self.gh);
command
.env("PATH", self.path)
.env("GH_TOKEN", self.token)
.env_remove("GH_DEBUG")
.args([
"api",
"--include",
"--hostname",
"github.com",
"graphql",
"-f",
&format!("query={query}"),
]);
let remaining = MAX_RESPONSE
.checked_sub(self.bytes.get())
.filter(|n| *n > 0)
.ok_or(invalid("GitHub snapshot exceeds byte limit"))?;
let duration = oxa_work::COMMAND_DEADLINE.min(self.time_left()?);
let mut command = self.api_command();
command.args(["--include", "graphql", "-f", &format!("query={query}")]);
let (remaining, duration) = self.read_budget()?;
let (code, text) = oxa_work::command_runner(command, Some(remaining), duration).ok_or(invalid(
"GitHub read failed: snapshot exceeds byte limit, capture refused or deadline reached",
))?;
// Charge captured bytes, including headers and non-2xx bodies, before interpreting them.
let mut used = self.bytes.get();
charge(&mut used, text.len()).map_err(|_| invalid("GitHub snapshot exceeds byte limit"))?;
self.bytes.set(used);
self.charge_capture(text.len())?;
let (headers, body) = text
.split_once("\r\n\r\n")
.or_else(|| text.split_once("\n\n"))
Expand Down Expand Up @@ -286,6 +268,31 @@ impl<'a> Reader<'a> {
.ok_or(invalid("missing GitHub repository"))?;
Ok((repository, text.len()))
}

fn read_budget(&self) -> Result<(usize, Duration), QueryError> {
let remaining = MAX_RESPONSE
.checked_sub(self.bytes.get())
.filter(|n| *n > 0)
.ok_or(QueryError::Other("GitHub snapshot exceeds byte limit"))?;
Ok((remaining, oxa_work::COMMAND_DEADLINE.min(self.time_left()?)))
}

fn charge_capture(&self, bytes: usize) -> Result<(), QueryError> {
let mut used = self.bytes.get();
charge(&mut used, bytes).map_err(|_| QueryError::Other("GitHub snapshot exceeds byte limit"))?;
self.bytes.set(used);
Ok(())
}

fn api_command(&self) -> Command {
let mut command = Command::new(self.gh);
command
.env("PATH", self.path)
.env("GH_TOKEN", self.token)
.env_remove("GH_DEBUG")
.args(["api", "--hostname", "github.com"]);
command
}
}

fn nodes(value: &Value) -> Result<&Vec<Value>, String> {
Expand Down Expand Up @@ -530,17 +537,21 @@ pub(crate) fn run(
// Existing parser declaration; migrate command/flag metadata together in #1062.
let (repo, options) = match args {
[shadow, flag, repo, options @ ..] if shadow == "--shadow" && flag == "--repo" => (repo, options),
_ => return Err("usage: oxa work board --shadow --repo OWNER/REPO [--once] [--token-stdin] [--review-policy FILE] [--queue-evidence] [--landing-shadow]".into()),
_ => return Err("usage: oxa work board --shadow --repo OWNER/REPO [--once] [--token-stdin] [--review-policy FILE] [--queue-evidence] [--landing-shadow] [--queue-report-policy FILE]".into()),
};
let (mut once, mut streamed, mut review_file) = (false, false, None);
let (mut queue_evidence, mut landing_shadow) = (false, false);
let mut report_file = None;
let mut options = options.iter();
while let Some(option) = options.next() {
match option.as_str() {
"--once" => once = true,
"--token-stdin" => streamed = true,
"--queue-evidence" => queue_evidence = true,
"--landing-shadow" => landing_shadow = true,
"--queue-report-policy" if report_file.is_none() => {
report_file = Some(options.next().ok_or("missing queue report policy file")?);
}
"--review-policy" if review_file.is_none() => {
review_file = Some(options.next().ok_or("missing board review policy file")?);
}
Expand Down Expand Up @@ -570,6 +581,9 @@ pub(crate) fn run(
let review_settings = review_file
.map(|path| review::Settings::read(Path::new(path)))
.transpose()?;
let mut report_settings = report_file
.map(|path| landing::report::Settings::read(Path::new(path)))
.transpose()?;
let inherited = env.get("GH_TOKEN").filter(|s| !s.is_empty());
if !streamed && inherited.is_none() {
return Err("shadow board requires an explicit GitHub App token".into());
Expand Down Expand Up @@ -616,11 +630,14 @@ pub(crate) fn run(
Board::read(&mut reader, &board_policy, &BTreeMap::new(), now)
.map(|board| (board, BTreeMap::new()))
}?;
let queue_rejections = if queue_evidence || landing_shadow {
let mut queue_rejections = if queue_evidence || landing_shadow || report_settings.is_some() {
oxa_work::board::queue::read(&board, |fields| reader.repository_read(fields))?
} else {
Vec::new()
};
if let Some(settings) = &mut report_settings {
reader.sample_test_reports(&mut queue_rejections, settings)?;
}
let (landing, landing_main) = if landing_shadow {
landing::review_decisions(&reader, &board)?
} else {
Expand Down
2 changes: 2 additions & 0 deletions oxa/oxa-cli/src/work_board/landing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use oxa_work::shadow::{LandingDecision, QueueObservation};
use serde_json::Value;
use std::{collections::BTreeMap, fmt::Write};

pub(super) mod report;

const BATCH: usize = 8;

pub(super) fn review_decisions(
Expand Down
231 changes: 231 additions & 0 deletions oxa/oxa-cli/src/work_board/landing/report.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
//! Download an exact Actions report through the existing Board Reader.
use crate::work_board::{QueryError, Reader};
use oxa_work::board::queue::{
report::{
attempt_for, select, ActionMetadata, ReportArchive, ReportSource, VerifiedArchive, VerifiedReports,
},
RejectionEvidence,
};
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::{
collections::{btree_map::Entry, BTreeMap, BTreeSet},
path::Path,
process::Command,
};

struct AttemptRead {
workflow: Value,
jobs: Value,
artifacts: Value,
verified_archives: BTreeSet<(u64, u64, String)>,
}

pub(in crate::work_board) struct Settings {
workflow_path: String,
required_check_names: BTreeSet<String>,
reports: Vec<(String, String)>,
verified: BTreeMap<(String, u64, u64), VerifiedReports>,
}

impl Settings {
pub(in crate::work_board) fn read(path: &Path) -> Result<Self, String> {
let input = oxa_work::shadow::read_queue_report_policy(path)?;
let invalid = "invalid queue report policy";
let policy: Value = serde_json::from_slice(&input).map_err(|_| invalid)?;
if policy.as_object().is_none_or(|map| map.len() != 3) {
return Err(invalid.into());
}
let workflow_path = policy["workflow_path"]
.as_str()
.filter(|path| path.starts_with(".github/workflows/") && !path.chars().any(char::is_control))
.ok_or(invalid)?
.to_owned();
let reports = policy["reports"]
.as_array()
.filter(|rows| !rows.is_empty())
.ok_or(invalid)?;
let label = |value: &Value| {
value
.as_str()
.filter(|name| !name.trim().is_empty() && !name.chars().any(char::is_control))
.map(str::to_owned)
.ok_or(invalid)
};
let checks = policy["required_check_names"]
.as_array()
.filter(|rows| !rows.is_empty())
.ok_or(invalid)?;
let required_check_names = checks.iter().map(label).collect::<Result<BTreeSet<_>, _>>()?;
if required_check_names.len() != checks.len() {
return Err(invalid.into());
}
let mut names = BTreeSet::new();
let mut sources = Vec::new();
for row in reports {
if row.as_object().is_none_or(|map| map.len() != 2) {
return Err(invalid.into());
}
let producer = label(&row["producer_name"])?;
let artifact = label(&row["artifact_name"])?;
if !names.insert(artifact.clone()) {
return Err(invalid.into());
}
sources.push((producer, artifact));
}
Ok(Self {
workflow_path,
required_check_names,
reports: sources,
verified: BTreeMap::new(),
})
}
}

impl Reader<'_> {
fn actions_get(&self, path: &str) -> Command {
let mut command = self.api_command();
command.args(["--method", "GET", &format!("repos/{}/actions/{path}", self.repo)]);
command
}

fn actions_json(&self, path: &str) -> Result<Value, String> {
let (remaining, duration) = self.read_budget().map_err(QueryError::message)?;
let (code, text) = oxa_work::command_runner(self.actions_get(path), Some(remaining), duration)
.ok_or("GitHub Actions response exceeds its limit or timed out")?;
self.charge_capture(text.len()).map_err(QueryError::message)?;
if code != 0 {
return Err("GitHub Actions metadata unavailable".into());
}
serde_json::from_str(&text).map_err(|_| "invalid GitHub Actions metadata".into())
}

pub(in crate::work_board) fn sample_test_reports(
&self,
rejections: &mut [RejectionEvidence],
settings: &mut Settings,
) -> Result<(), String> {
let mut previous = std::mem::take(&mut settings.verified);
let mut attempts_read = BTreeMap::new();
for rejection in rejections {
let mut attempts = BTreeMap::<_, Vec<_>>::new();
for id in rejection.workflow_checks() {
if let Some(attempt) = attempt_for(rejection, id) {
attempts.entry(attempt).or_default().push(id);
}
}
for ((run, attempt), checks) in attempts {
let key = (rejection.removal_event().to_owned(), run, attempt);
let proof = match previous.remove(&key) {
Some(saved) if saved.failed_check_runs == checks => Some(saved),
Comment thread
oxa-agent[bot] marked this conversation as resolved.
Resolved
_ => {
let sources = match attempts_read.entry((run, attempt)) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
entry.insert(self.read_attempt(run, attempt, &settings.workflow_path)?)
}
};
let Some(sources) = sources else { continue };
self.verify_attempt_reports(rejection, checks, (run, attempt), settings, sources)?
}
};
if let Some(proof) = proof {
rejection.record_verified_reports(proof.clone());
settings.verified.insert(key, proof);
}
}
}
Ok(())
}

fn read_attempt(
&self,
run: u64,
attempt: u64,
workflow_path: &str,
) -> Result<Option<AttemptRead>, String> {
let workflow = self.actions_json(&format!("runs/{run}/attempts/{attempt}"))?;
if workflow["path"].as_str() != Some(workflow_path) {
return Ok(None);
}
Ok(Some(AttemptRead {
workflow,
jobs: self.actions_json(&format!("runs/{run}/attempts/{attempt}/jobs?per_page=100"))?,
artifacts: self.actions_json(&format!("runs/{run}/artifacts?per_page=100"))?,
verified_archives: BTreeSet::new(),
}))
}

fn verify_attempt_reports(
&self,
rejection: &RejectionEvidence,
checks: Vec<u64>,
(run, attempt): (u64, u64),
settings: &Settings,
sources: &mut AttemptRead,
) -> Result<Option<VerifiedReports>, String> {
let metadata = ActionMetadata {
attempt: &sources.workflow,
jobs: &sources.jobs,
artifact_listing: &sources.artifacts,
};
let mut archives = Vec::new();
for (producer_name, artifact_name) in &settings.reports {
let source = ReportSource {
workflow_path: &settings.workflow_path,
required_check_names: &settings.required_check_names,
producer_name,
artifact_name,
max_archive_bytes: crate::work_board::MAX_RESPONSE as u64,
};
let mut selected = checks.iter().map(|id| select(rejection, *id, &source, metadata));
let Some(Some(first)) = selected.next() else {
return Ok(None);
};
if selected.any(|archive| archive.as_ref() != Some(&first)) {
return Ok(None);
}
archives.push(first);
}
let mut verified = Vec::new();
for report in archives {
let key = (report.artifact_id, report.size_bytes, report.sha256.to_owned());
if !sources.verified_archives.contains(&key) {
self.verify_archive(&report)?;
sources.verified_archives.insert(key);
}
verified.push(VerifiedArchive {
id: report.artifact_id,
sha256: report.sha256.to_owned(),
});
}
Ok(Some(VerifiedReports {
failed_check_runs: checks,
workflow_run: run,
run_attempt: attempt,
archives: verified,
}))
}

fn verify_archive(&self, selected: &ReportArchive<'_>) -> Result<(), String> {
let size = usize::try_from(selected.size_bytes).map_err(|_| "invalid GitHub archive size")?;
let (remaining, duration) = self.read_budget().map_err(QueryError::message)?;
if size > remaining {
return Err("GitHub test archive exceeds remaining snapshot byte limit".into());
}
let (code, bytes) = oxa_work::command_output_bytes(
self.actions_get(&format!("artifacts/{}/zip", selected.artifact_id)),
Some(size),
duration,
)
.ok_or("GitHub test archive exceeds its limit or timed out")?;
self.charge_capture(bytes.len()).map_err(QueryError::message)?;
if code != 0 || bytes.len() != size {
return Err("GitHub test archive unavailable or incomplete".into());
}
if format!("{:x}", Sha256::digest(&bytes)) != selected.sha256 {
return Err("GitHub test archive digest mismatch".into());
}
Ok(())
}
}
Loading
Loading