From 1d19836cd60bababef09e39bb1bed0e5e1e042a3 Mon Sep 17 00:00:00 2001 From: atlas Date: Mon, 30 Nov 2026 10:48:55 +0100 Subject: [PATCH] swarm-controller: the review handlers take a review-only sub-state --- .../src/review/review_classifier.rs | 33 ++++++++++++++++--- .../src/review/review_classifier/keys.rs | 7 ++-- .../src/review/review_classifier/open_pr.rs | 4 +-- .../src/review/review_classifier/runs.rs | 6 ++-- .../src/review/review_classifier/settings.rs | 14 ++++---- 5 files changed, 44 insertions(+), 20 deletions(-) diff --git a/swarm-controller/src/review/review_classifier.rs b/swarm-controller/src/review/review_classifier.rs index ae925cf31..5b0c2fe02 100644 --- a/swarm-controller/src/review/review_classifier.rs +++ b/swarm-controller/src/review/review_classifier.rs @@ -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>, + pub(crate) forge: crate::jobq::Late>, + pub(crate) try_outs: TryOuts, + pub(crate) reviews: Arc, + pub(crate) runs: runs::Runs, +} + +impl FromRef 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, + State(state): State, headers: HeaderMap, body: Result, JsonRejection>, ) -> Result<(StatusCode, Json), 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, + State(state): State, Path(id): Path, ) -> Result, 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, + State(state): State, query: Result, QueryRejection>, ) -> Result, ProblemDetails> { let Query(query) = query.map_err(|e| error_problem(StatusCode::BAD_REQUEST, &e.body_text()))?; diff --git a/swarm-controller/src/review/review_classifier/keys.rs b/swarm-controller/src/review/review_classifier/keys.rs index ea5c47423..b680d832f 100644 --- a/swarm-controller/src/review/review_classifier/keys.rs +++ b/swarm-controller/src/review/review_classifier/keys.rs @@ -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 { ), tag = "review-classifier" )] -pub(crate) async fn get_keys(State(_state): State) -> Result, ProblemDetails> { +pub(crate) async fn get_keys() -> Result, 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) -> Result, headers: HeaderMap, Path(runtime): Path, body: Result, JsonRejection>, diff --git a/swarm-controller/src/review/review_classifier/open_pr.rs b/swarm-controller/src/review/review_classifier/open_pr.rs index a96eb2f63..59af1532b 100644 --- a/swarm-controller/src/review/review_classifier/open_pr.rs +++ b/swarm-controller/src/review/review_classifier/open_pr.rs @@ -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, + State(state): State, headers: HeaderMap, body: Result, JsonRejection>, ) -> Result<(StatusCode, Json), ProblemDetails> { diff --git a/swarm-controller/src/review/review_classifier/runs.rs b/swarm-controller/src/review/review_classifier/runs.rs index 6140acf2f..17aa2ddae 100644 --- a/swarm-controller/src/review/review_classifier/runs.rs +++ b/swarm-controller/src/review/review_classifier/runs.rs @@ -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) -> Vec { 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, + State(state): State, ) -> Result>, ProblemDetails> { state.runs.list().await.map(Json) } diff --git a/swarm-controller/src/review/review_classifier/settings.rs b/swarm-controller/src/review/review_classifier/settings.rs index 462e96198..56ae08152 100644 --- a/swarm-controller/src/review/review_classifier/settings.rs +++ b/swarm-controller/src/review/review_classifier/settings.rs @@ -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, + State(state): State, ) -> Result, 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, + State(state): State, headers: HeaderMap, body: Result, JsonRejection>, ) -> Result, 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,