Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: the review handlers take a review-only sub-state

This commit is contained in:
atlas 2026-11-30 10:48:55 +01:00
commit 1d19836cd6
5 changed files with 44 additions and 20 deletions

View file

@ -27,7 +27,7 @@ use std::time::{Duration, Instant};
use axum::Json;
use axum::extract::rejection::{JsonRejection, QueryRejection};
use axum::extract::{Path, Query, State};
use axum::extract::{FromRef, Path, Query, State};
use axum::http::{HeaderMap, StatusCode};
use problem_details::ProblemDetails;
use serde::{Deserialize, Serialize};
@ -165,6 +165,31 @@ impl TryOutStatus {
}
}
/// What the review routes may reach of [`AppState`].
///
/// Derived per request from [`AppState`] (`FromRef`), so each field is a clone
/// of the same `Arc` and nothing is held twice.
#[derive(Clone)]
pub(crate) struct ReviewState {
pub(crate) status: Option<Arc<crate::status::StatusReader>>,
pub(crate) forge: crate::jobq::Late<Arc<forge::Client>>,
pub(crate) try_outs: TryOuts,
pub(crate) reviews: Arc<review::Reviews>,
pub(crate) runs: runs::Runs,
}
impl FromRef<AppState> for ReviewState {
fn from_ref(state: &AppState) -> Self {
Self {
status: state.status.clone(),
forge: Arc::clone(&state.forge),
try_outs: state.try_outs.clone(),
reviews: Arc::clone(&state.reviews),
runs: state.runs.clone(),
}
}
}
/// The runs this daemon holds, by id.
#[derive(Clone, Default)]
pub(crate) struct TryOuts {
@ -338,7 +363,7 @@ impl RunSink for async_nats::Client {
tag = "review-classifier"
)]
pub(crate) async fn post_try_out(
State(state): State<AppState>,
State(state): State<ReviewState>,
headers: HeaderMap,
body: Result<Json<TryOutRequest>, JsonRejection>,
) -> Result<(StatusCode, Json<TryOutAccepted>), ProblemDetails> {
@ -414,7 +439,7 @@ pub(crate) async fn post_try_out(
tag = "review-classifier"
)]
pub(crate) async fn get_try_out(
State(state): State<AppState>,
State(state): State<ReviewState>,
Path(id): Path<String>,
) -> Result<Json<TryOutStatus>, ProblemDetails> {
state.try_outs.get(&id).map(Json).ok_or_else(|| {
@ -451,7 +476,7 @@ pub(crate) async fn get_try_out(
tag = "review-classifier"
)]
pub(crate) async fn get_rules(
State(state): State<AppState>,
State(state): State<ReviewState>,
query: Result<Query<RulesQuery>, QueryRejection>,
) -> Result<Json<Rules>, ProblemDetails> {
let Query(query) = query.map_err(|e| error_problem(StatusCode::BAD_REQUEST, &e.body_text()))?;

View file

@ -11,8 +11,8 @@
use std::future::Future;
use axum::Json;
use axum::extract::Path;
use axum::extract::rejection::JsonRejection;
use axum::extract::{Path, State};
use axum::http::{HeaderMap, StatusCode};
use problem_details::ProblemDetails;
use serde::{Deserialize, Serialize};
@ -23,7 +23,7 @@ use swarm_secret_client::{
};
use utoipa::ToSchema;
use crate::routes::{AppState, error_problem};
use crate::routes::error_problem;
/// The most bytes a key may have. Provider keys run to a few hundred.
const MAX_KEY_BYTES: usize = 4096;
@ -181,7 +181,7 @@ async fn connect() -> Result<SecretStore, ProblemDetails> {
),
tag = "review-classifier"
)]
pub(crate) async fn get_keys(State(_state): State<AppState>) -> Result<Json<Keys>, ProblemDetails> {
pub(crate) async fn get_keys() -> Result<Json<Keys>, ProblemDetails> {
let store = connect().await?;
both(&store).await.map(Json).map_err(|e| store_problem(&e))
}
@ -206,7 +206,6 @@ pub(crate) async fn get_keys(State(_state): State<AppState>) -> Result<Json<Keys
tag = "review-classifier"
)]
pub(crate) async fn put_key(
State(_state): State<AppState>,
headers: HeaderMap,
Path(runtime): Path<String>,
body: Result<Json<KeyBody>, JsonRejection>,

View file

@ -32,7 +32,7 @@ use utoipa::ToSchema;
use super::{FileSource, RULES_PATHS, check_classifications, operator, parse_rules, split_repo};
use crate::forge::classifier_account::{BranchCommit, CommitAuthor, LOGIN};
use crate::forge::{self, BranchFile};
use crate::routes::{AppState, error_problem};
use crate::routes::error_problem;
/// Where a branch's name starts, so the forge's branch list groups them.
const BRANCH_PREFIX: &str = "review-classifier/";
@ -118,7 +118,7 @@ impl BranchSink for forge::Client {
tag = "review-classifier"
)]
pub(crate) async fn post_open_pr(
State(state): State<AppState>,
State(state): State<super::ReviewState>,
headers: HeaderMap,
body: Result<Json<OpenPrRequest>, JsonRejection>,
) -> Result<(StatusCode, Json<OpenPrBranch>), ProblemDetails> {

View file

@ -23,7 +23,7 @@ use serde::{Deserialize, Serialize};
use swarm_wire::review_classifier::{Overall, Reply, Row};
use utoipa::ToSchema;
use crate::routes::{AppState, error_problem};
use crate::routes::error_problem;
use crate::schedules::{read_with, write};
/// The bucket holding the runs. Agents hold no grant on it.
@ -217,7 +217,7 @@ fn newest(run: &RunRecord, mut runs: Vec<Stored>) -> Vec<Stored> {
runs
}
/// The runs' store, as [`AppState`] holds it.
/// The runs' store, as [`crate::routes::AppState`] holds it.
#[derive(Clone)]
pub(crate) enum Runs {
/// [`BUCKET`] on the swarm queue, when this host has one.
@ -385,7 +385,7 @@ fn log(run: &RunRecord) {
tag = "review-classifier"
)]
pub(crate) async fn get_runs(
State(state): State<AppState>,
State(state): State<super::ReviewState>,
) -> Result<Json<Vec<RunRecord>>, ProblemDetails> {
state.runs.list().await.map(Json)
}

View file

@ -26,8 +26,8 @@ use swarm_wire::review_classifier::{
};
use utoipa::ToSchema;
use crate::routes::{AppState, error_problem, wanted_error_status};
use crate::schedules::{queue, read_with, refused, write};
use crate::routes::{error_problem, wanted_error_status};
use crate::schedules::{queue_of, read_with, refused, write};
/// The bucket holding the settings. No other service holds a grant on it.
pub const BUCKET: &str = "review-classifier-settings";
@ -351,9 +351,9 @@ fn queue_problem(e: &anyhow::Error) -> ProblemDetails {
tag = "review-classifier"
)]
pub(crate) async fn get_settings(
State(state): State<AppState>,
State(state): State<super::ReviewState>,
) -> Result<Json<Settings>, ProblemDetails> {
let client = queue(&state).map_err(refused)?;
let client = queue_of(state.status.as_ref()).map_err(refused)?;
client
.settings()
.await
@ -389,7 +389,7 @@ pub(crate) async fn get_settings(
tag = "review-classifier"
)]
pub(crate) async fn put_settings(
State(state): State<AppState>,
State(state): State<super::ReviewState>,
headers: HeaderMap,
body: Result<Json<SettingsUpdate>, JsonRejection>,
) -> Result<Json<Settings>, ProblemDetails> {
@ -401,7 +401,7 @@ pub(crate) async fn put_settings(
)
})?;
let Json(update) = body.map_err(|e| error_problem(StatusCode::BAD_REQUEST, &e.body_text()))?;
let client = queue(&state).map_err(refused)?;
let client = queue_of(state.status.as_ref()).map_err(refused)?;
let store = open(&client).await.map_err(|e| queue_problem(&e))?;
for _ in 0..MAX_ATTEMPTS {
let (current, revision) = stored(&store).await.map_err(|e| queue_problem(&e))?;
@ -677,7 +677,7 @@ mod tests {
}
/// An app whose queue is `nats`.
async fn app(nats: &TestNats) -> (axum::Router, AppState) {
async fn app(nats: &TestNats) -> (axum::Router, crate::routes::AppState) {
let (mut state, _) = crate::routes::tests::state_with_roster();
state.status = Some(std::sync::Arc::new(crate::status::StatusReader::new(
nats.client().await,