diff --git a/Cargo.lock b/Cargo.lock index d18e05460..2a6829f6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1844,7 +1844,6 @@ dependencies = [ "clap", "clap-markdown", "forgejo-api", - "hive-types", "libc", "reqwest", "serde", diff --git a/docs/swarm/README.md b/docs/swarm/README.md index 86055ea6b..f1ec240d8 100644 --- a/docs/swarm/README.md +++ b/docs/swarm/README.md @@ -32,8 +32,7 @@ grafana, the controller, authelia, …) → its repo's own yes/no rules and posts the result as a review. → [`review-classifier.md`](review-classifier.md) - **notification router** — forge events turned into todos on the agents - they concern: assigned, unassigned, cleared on close; and the classifier's - review requested on a push. → + they concern: assigned, unassigned, cleared on close. → [`notify-router.md`](notify-router.md) - **swarm UI** — the operator's day-to-day surface, on the swarm apex, `admins` only. → [`ui.md`](ui.md) @@ -416,7 +415,7 @@ itself, all under `https:///webhook/forge/`: - an instance-wide `pull_request_review_request` system hook, which starts the [review classifier](review-classifier.md#trigger-and-lifecycle) - instance-wide `issues` + `issue_assign` and `pull_request` + - `pull_request_assign` + `pull_request_sync` system hooks, whose deliveries it + `pull_request_assign` system hooks, whose deliveries it keeps in the `SWARM_EVENTS` stream for the [notification router](notify-router.md#the-path) diff --git a/docs/swarm/notify-router.md b/docs/swarm/notify-router.md index 8b2617101..6ce07563b 100644 --- a/docs/swarm/notify-router.md +++ b/docs/swarm/notify-router.md @@ -41,9 +41,8 @@ router notices an unassign only on an issue it has seen an assign event for. 1. swarm-controller registers two instance-wide hooks, one for the `issues` and `issue_assign` events at `/webhook/forge/issues` and one for - `pull_request`, `pull_request_assign` and `pull_request_sync` (a push to an - open PR's branch) at `/webhook/forge/pull-request`. At each start it adds - any of these events a registered hook lacks. + `pull_request` and `pull_request_assign` at `/webhook/forge/pull-request`. + At each start it adds any of these events a registered hook lacks. 2. On each delivery the controller checks the HMAC and publishes the body, unchanged, to the `SWARM_EVENTS` stream on `$SWARM.events.forge.`, such as @@ -69,19 +68,16 @@ The router holds no forge token and no webhook secret, and registers no hook. A flow is a graph of nodes joined by edges: one `source` node naming the forge event types it starts from, `match` nodes that pass when every condition -holds, `todo` nodes that emit one todo op per recipient, and `request_reviewer` -nodes that ask for a review. A match node can have more than one child, and -each one runs, so one event can reach several leaves. A flow must be a tree: -one source, one parent for every other node, every node reachable, no children -under a `todo` or a `request_reviewer`. The most is 64 nodes, 16 +holds, and `todo` nodes that emit one todo op per recipient. A match node can +have more than one child, and each one runs, so one event can reach several +todos. A flow must be a tree: one source, one parent for every other node, +every node reachable, no children under a `todo`. The most is 64 nodes, 16 levels deep, and 100 custom flows. A `todo` node sends to the agents an event field holds (`assignees_added`, `todo_holders`, …) or to one named agent. The router ships the three flows above, each a source → match on `action` → -todo, and `head-classifier`: source `pull_request_sync` → `request_reviewer` -for `review-classifier` ([below](#requesting-a-review)). The code defines them -(`swarm_wire::notify_flows::builtins`), so a build that changes one reaches -every swarm. Each switches off on `/notifications`. A custom flow has the same type and +todo. The code defines them (`swarm_wire::notify_flows::builtins`), so a +build that changes one reaches every swarm. A custom flow has the same type and the same evaluator, so it can do anything a built-in does. **The stored document.** swarm-controller keeps the rest in one document, the @@ -90,8 +86,8 @@ switched off, and the custom flows. Only the controller writes it, through `GET` and `PUT /api/notify/flows`. A `PUT` replaces the whole document in one compare-and-set. The controller answers 400 unless every flow passes the tree check above and reads only what exists: a source event from `issues`, -`issue_assign`, `pull_request`, `pull_request_assign` or `pull_request_sync`, -and condition and recipient fields from the event's field list. The same goes for the `{field}` +`issue_assign`, `pull_request` or `pull_request_assign`, and condition and +recipient fields from the event's field list. The same goes for the `{field}` placeholders in a todo's key and text. A `PUT` must carry the `revision` it read, `0` for no document, and gets 409 when the stored revision is another. Swarm UI's [`/notifications`](ui.md#notification-flows) page shows the document and @@ -104,8 +100,8 @@ accepts: - `source_events`: `[{name, description}]`, the source events. - `fields`: `[{name, description}]`, the condition and recipient fields, and the `{field}` placeholders. -- `node_kinds`: `[{kind, description, fields}]` for `source`, `match`, `todo` - and `request_reviewer`; each field is `{name, description, required, values}`, and `values` +- `node_kinds`: `[{kind, description, fields}]` for `source`, `match` and + `todo`; each field is `{name, description, required, values}`, and `values` lists the accepted `{name, description}` pairs (the `upsert` and `clear` of a todo's `op`), empty for other fields. - `limits`: `{max_nodes, max_depth, max_flows}`. @@ -113,39 +109,6 @@ accepts: A controller that predates `schema` leaves it out of the answer; a client reads it as optional. -**Requesting a review.** A `request_reviewer` node has one field, `reviewer`, -a forge login. For the PR the event concerns (`repo` and `number`; an event -about an issue sends nothing), the router sends a NATS request on -`$SWARM.router.sink.request-reviewer` with `{repo, number, reviewer}` and -swarm-controller answers with one line: - -| Reply | Meaning | -| --------------- | ---------------------------------------------------------------------------------- | -| `requested` | the forge accepted the request | -| `skipped:` | the reviewer's review of the PR's head already stands; the controller sent nothing | -| `error:` | the forge refused or didn't answer, or the request didn't decode | - -The controller asks the forge under its own token, which the router never -holds. It skips a reviewer whose latest review is neither stale (the PR's head -moved past the commit it reviewed) nor dismissed, because requesting a review -from someone who already submitted one makes the forge dismiss it, approval -included. The controller asks again once the review is older than the head. -`hive-forge pr assign-reviewer` applies the same rule (`hive_types::review`). - -The router sends the request once, after it queued the event's todo ops and -wrote the snapshot, and logs the answer. A controller that's down or answers -`error:` doesn't hold up the event or any agent's todos, and the request is -not retried: the next push asks again. A request repeated for the same head is -harmless: a reviewer with a pending request is a no-op for the forge, and one -who has reviewed gets `skipped`. - -`head-classifier` requests the review classifier's review on a push to any open -PR. The classifier then runs as it does for any requested review -([`review-classifier.md`](review-classifier.md#trigger-and-lifecycle)), through -its own request queue, which replaces a waiting request for the same PR. A -repo the classifier's account can't read, or a PR that account opened, answers -`error:`. - **What the router runs.** It reads the key at most every 10 seconds, when an event arrives, and runs the built-ins you left on, then the custom flows that are on and pass the tree check. It logs a custom flow that fails the check, @@ -164,25 +127,20 @@ document never leaves it with no flows. The controller refuses to overwrite a document of a version it doesn't know. **Deploying in any order.** The controller, the router and the UI can be on -different hosts. A router that predates `request_reviewer` reads a document -holding such a node (a custom flow with one) as one it can't read and keeps the -flows it had; it also has no `head-classifier`. A router that has it, with a -controller that predates it, gets no answer to the request and logs that. A controller that predates this leaves the key absent, and the +different hosts. A controller that predates this leaves the key absent, and the router runs the built-ins as before. A router that predates this ignores the document, so the page and the API show a state the router isn't yet running. ## Credentials and grants -| Principal | Grant | -| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `swarm-notify-router`, in its container | store: `read` on `swarm/services//oidc/client`. Queue: its own consumer on `SWARM_EVENTS` (create, pull, ack), publish on `$SWARM.router.todo.*` and on `$SWARM.router.sink.request-reviewer`, create `ROUTER_TODOS`, its own `notify-router-state` bucket, read on the `notify-flows` bucket, and `STREAM.INFO` on the agent-status bucket, which lists the agents | -| swarm-controller | publish on `$SWARM.events.forge.*`; subscribe to `$SWARM.router.sink.request-reviewer` and answer each request once, for 30 seconds; creates and writes the `notify-flows` bucket (every bucket, by its `$KV.*.>` grant) | -| an agent, under its own credential | create its own consumer `todo-` in the form whose filter is `$SWARM.router.todo.`, pull from it, and ack what it delivered | +| Principal | Grant | +| --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `swarm-notify-router`, in its container | store: `read` on `swarm/services//oidc/client`. Queue: its own consumer on `SWARM_EVENTS` (create, pull, ack), publish on `$SWARM.router.todo.*`, create `ROUTER_TODOS`, its own `notify-router-state` bucket, read on the `notify-flows` bucket, and `STREAM.INFO` on the agent-status bucket, which lists the agents | +| swarm-controller | publish on `$SWARM.events.forge.*`; creates and writes the `notify-flows` bucket (every bucket, by its `$KV.*.>` grant) | +| an agent, under its own credential | create its own consumer `todo-` in the form whose filter is `$SWARM.router.todo.`, pull from it, and ack what it delivered | -No other client may publish on `$SWARM.router.todo.*`, `$SWARM.router.sink.request-reviewer` or -`$SWARM.events.forge.*`, so each has one writer, and the controller alone -hears the review requests. The review request needs no secret of its own: the -controller asks the forge under the token it already holds. The agent's create subject +No other client may publish on `$SWARM.router.todo.*` or +`$SWARM.events.forge.*`, so each has one writer. The agent's create subject carries the consumer's filter, and the queue server checks it against the request, so an agent can't create a consumer that delivers another agent's todos. diff --git a/docs/swarm/review-classifier.md b/docs/swarm/review-classifier.md index ac9d97f42..5cc696341 100644 --- a/docs/swarm/review-classifier.md +++ b/docs/swarm/review-classifier.md @@ -18,7 +18,7 @@ that reviewer. ## Repo configuration -A repo defines its rules in a file at its root, in YAML as +A repo opts in with a rules file at its root, in YAML as `.hyperhive/review-classifier.yml` or in JSON as `.hyperhive/review-classifier.json`. It holds one of the two; with both, the rules don't load. `swarm-controller` always reads the file from the repo's @@ -37,14 +37,8 @@ with exactly these four keys: Any other key, at the top or in an entry, is an error: a misspelt key fails the load instead of vanishing. An empty list, more than ten entries, an empty -name or description, and two entries with one name are errors too. A file -that exists but fails these checks never falls back to a pass: the run posts -no verdict. -A repo whose default branch holds neither file has no rules. When someone -requests the classifier's review on its PR, the account approves at once. The -body reads `no review rules are configured for owner/name; approved without -checks`. The controller makes no model call. The instance has no per-repo -filter yet, so every repo without a rules file gets this approval. +name or description, and two entries with one name are errors too. Nothing +falls back to a pass: a repo whose file doesn't load has no rules. An example: @@ -92,10 +86,6 @@ as a reviewer on a PR starts a run: 4. The controller posts the result on the PR as the service account's review, on the commit it read. -A push starts no run by itself. The notification router's `head-classifier` -flow requests the account's review on every push to an open PR, unless its -review of the new head already stands ([`notify-router.md`](notify-router.md#requesting-a-review)). - When a post can't reach the secret store or the forge, because the connection fails or a try gets no answer within two minutes, the controller tries it up to six times over about five minutes, enough to outlast a @@ -427,8 +417,7 @@ Repos that rely on it typically raise their required approval count to account for it. A run with no clean verdict never approves. When the PR or the rules don't -load (a repo with no rules file gets an approval instead), the run fails, the classifier -answers a different number of +load, the run fails, the classifier answers a different number of classifications than the controller sent it, or **any** classification has no answer, the account posts a **comment** instead, starting with `**No verdict:**` and the reason, followed by the table when there is one. A comment neither approves nor diff --git a/docs/swarm/services.md b/docs/swarm/services.md index 6f908eedc..cea50f2e2 100644 --- a/docs/swarm/services.md +++ b/docs/swarm/services.md @@ -491,8 +491,7 @@ swarm-controller keeps every `issues` and `pull_request` delivery from its instance-wide hooks in the `SWARM_EVENTS` stream. The router reads that stream and queues todos for the agents an event concerns: an agent assigned to an issue or pull request gets a todo, one taken off it gets that todo rewritten -to say so, and closing it clears them. A push to an open pull request makes -it ask swarm-controller to request the review classifier's review. Each agent's harness takes its todos +to say so, and closing it clears them. Each agent's harness takes its todos from the `ROUTER_TODOS` stream, so an agent that's down gets them when it's back. diff --git a/docs/swarm/ui.md b/docs/swarm/ui.md index f2e356e02..a14270494 100644 --- a/docs/swarm/ui.md +++ b/docs/swarm/ui.md @@ -224,7 +224,7 @@ in two groups. Each **built-in** flow has an on/off switch; switching one off stops the router running it from its next event, and switching it on again restores it. Each **custom** flow shows its name and whether it's on. Every flow draws as an indented tree: the forge events it starts from, the -conditions under it, and the todo or review request each branch ends in. +conditions under it, and the todo each branch ends in. A built-in flow can be **duplicated** into a custom one, and **new flow** starts a custom flow with just a source; either opens the flow in the editor. A custom diff --git a/frontend/packages/swarm-ui/test/flow-types.test.mjs b/frontend/packages/swarm-ui/test/flow-types.test.mjs index 6bca99760..adb168f4b 100644 --- a/frontend/packages/swarm-ui/test/flow-types.test.mjs +++ b/frontend/packages/swarm-ui/test/flow-types.test.mjs @@ -73,14 +73,6 @@ test("describeNode: says what each kind does", () => { "clear todos forge:{repo}#{number}:* of {todo_holders}", ); assert.equal(describeNode(flow.nodes[4]), "todo to triage, key k: look"); - assert.equal( - describeNode({ - id: "r", - kind: "request_reviewer", - reviewer: "review-classifier", - }), - "request a review from review-classifier", - ); }); test("withBuiltin: switching off adds once, switching on removes", () => { diff --git a/hive-forge/Cargo.toml b/hive-forge/Cargo.toml index e264d25d1..1b6b60538 100644 --- a/hive-forge/Cargo.toml +++ b/hive-forge/Cargo.toml @@ -19,8 +19,6 @@ clap-markdown = "0.1" # not about the workspace. See the pin note on `forgejo-api` in the root # `Cargo.toml`. forgejo-api = { workspace = true, features = ["sync"] } -# The review rule `pr assign-reviewer` shares with swarm-controller. -hive-types.workspace = true url.workspace = true time.workspace = true reqwest = { workspace = true, features = [ diff --git a/hive-forge/src/verbs/mod.rs b/hive-forge/src/verbs/mod.rs index c44017f66..61b993faf 100644 --- a/hive-forge/src/verbs/mod.rs +++ b/hive-forge/src/verbs/mod.rs @@ -346,15 +346,66 @@ fn edit_distance(a: &str, b: &str) -> usize { prev[b.len()] } -pub(crate) use hive_types::review::ReviewInfo; +/// One reviewer's latest verdict on a PR, plus forgejo's `stale` / +/// `dismissed` bits. +/// +/// `stale` is set by forgejo when the PR head commit changed after this +/// review was submitted (branch protection then wants a fresh review); +/// `dismissed` is set when the review was explicitly dismissed. Either way +/// the review no longer applies to the current head even though its `state` +/// string still reads `APPROVED` / `REQUEST_CHANGES` — so surfacing them +/// stops the CLI from reporting a no-longer-valid review as still-good, and +/// [`ReviewInfo::superseded`] rolls both into one "doesn't count" check. +/// +/// ⚠️ **`stale` here is not forgejo's flag alone.** That flag is eventually +/// consistent: seconds after a push it still reports the pre-push answer, so +/// a verdict against the previous head reads as current in exactly the window +/// where someone runs the CLI right after pushing. [`latest_reviews`] therefore +/// ORs it with a direct comparison of the review's own `commit_id` against the +/// PR head — the flag is right *eventually*, the comparison is right +/// *immediately*, and either alone is worse than both. +pub(crate) struct ReviewInfo { + pub login: String, + pub state: String, + pub stale: bool, + pub dismissed: bool, +} + +impl ReviewInfo { + /// True when the review no longer applies to the current head — stale + /// (head moved) or dismissed. Such a verdict neither blocks a merge nor + /// counts as a fresh approval. + pub(crate) fn superseded(&self) -> bool { + self.stale || self.dismissed + } +} + +/// Whether this review was submitted against a commit that is no longer the +/// PR head — the race-free half of the staleness check. +/// +/// Fails **closed on unknowns**, i.e. "not stale": either side missing means +/// we cannot show the head moved, and the cost of guessing wrong in that +/// direction is one redundant re-review, where the other direction would +/// silently void every verdict on the PR (blocking nothing, but reporting a +/// ready PR as unreviewed and inviting a re-request that dismisses a real +/// approval). +fn reviewed_older_head(reviewed_sha: Option<&str>, head_sha: Option<&str>) -> bool { + let (Some(head), Some(reviewed)) = (head_sha, reviewed_sha) else { + return false; + }; + // Both emptiness checks matter, and for the same reason: a blank string + // is a value the forge sent, not a sha it has. Treating one as real would + // make every review compare unequal and mark the whole PR stale — the + // direction this whole function exists to avoid. + !head.is_empty() && !reviewed.is_empty() && reviewed != head +} /// Latest non-comment review per reviewer on a PR. Reviews come /// oldest-first, so a later verdict from the same user supersedes an /// earlier one; `COMMENT` / `PENDING` reviews carry no verdict and are -/// skipped. Shared by `pr status` (health view + readiness verdict), -/// `pr merge` (pre-merge changes-requested gate) and `pr assign-reviewer`, -/// and by swarm-controller's `request-reviewer` sink, through -/// [`hive_types::review`], so the verdict semantics stay in one place. +/// skipped. Shared by `pr status` (health view + readiness verdict) and +/// `pr merge` (pre-merge changes-requested gate) so the verdict semantics +/// stay in one place. /// /// # Errors /// @@ -388,16 +439,32 @@ pub(crate) fn latest_reviews(client: &Client, repo: &str, pr: u64) -> Result = Vec::new(); + for r in &reviews { + let Some(login) = r.user.as_ref().and_then(|u| u.login.as_deref()) else { + continue; + }; + let st = r.state.as_deref().unwrap_or(""); + if st == "COMMENT" || st == "PENDING" || st.is_empty() { + continue; + } + let stale = r.stale.unwrap_or(false) + || reviewed_older_head(r.commit_id.as_deref(), head_sha.as_deref()); + let dismissed = r.dismissed.unwrap_or(false); + if let Some(slot) = latest.iter_mut().find(|info| info.login == login) { + st.clone_into(&mut slot.state); + slot.stale = stale; + slot.dismissed = dismissed; + } else { + latest.push(ReviewInfo { + login: login.to_owned(), + state: st.to_owned(), + stale, + dismissed, + }); + } + } + Ok(latest) } /// The current dependency list for an issue or PR — each entry names @@ -617,7 +684,7 @@ pub(crate) fn attachment_json(assets: Option<&[Attachment]>) -> Vec { #[cfg(test)] mod tests { - use super::{NullableVec, pct_encode, refuse_json}; + use super::{NullableVec, pct_encode, refuse_json, reviewed_older_head}; #[test] fn refuse_json_bails_with_the_verb_and_reason() { @@ -648,6 +715,32 @@ mod tests { assert_eq!(v.0, vec![1, 2, 3]); } + #[test] + fn review_on_an_older_commit_is_stale() { + // The bug this exists for: forgejo still reports `stale: false` here + // in the seconds after a push, so the comparison has to catch it. + assert!(reviewed_older_head(Some("81292f14"), Some("f4c47088"))); + } + + #[test] + fn review_on_the_current_head_is_not_stale() { + assert!(!reviewed_older_head(Some("f4c47088"), Some("f4c47088"))); + } + + /// Every unknown fails *toward* keeping the verdict. Voiding every review + /// on a forge that stopped reporting one of these would be a far louder + /// wrong answer than one redundant re-review. + #[test] + fn unknown_commit_or_head_is_not_stale() { + assert!(!reviewed_older_head(None, Some("f4c47088"))); + assert!(!reviewed_older_head(Some("81292f14"), None)); + assert!(!reviewed_older_head(Some(""), Some("f4c47088"))); + // Absent and blank have to behave the same on BOTH sides — a blank + // head that counted as real would mark every review on the PR stale. + assert!(!reviewed_older_head(Some("81292f14"), Some(""))); + assert!(!reviewed_older_head(Some(""), Some(""))); + } + #[test] fn pct_encode_passes_unreserved_through() { // Plain artifact names round-trip verbatim — no performance diff --git a/hive-forge/src/verbs/pr_assign_reviewer.rs b/hive-forge/src/verbs/pr_assign_reviewer.rs index b36c8760c..a36d941b5 100644 --- a/hive-forge/src/verbs/pr_assign_reviewer.rs +++ b/hive-forge/src/verbs/pr_assign_reviewer.rs @@ -55,8 +55,10 @@ pub fn run(client: &Client, args: Args) -> Result<()> { // Guard the add path only — withdrawing a request is always safe. // Re-requesting from someone who already reviewed dismisses that review // instead of no-op'ing (see the module doc-comment above). - let reviews = super::latest_reviews(client, client.repo()?, args.number)?; - if let Some(existing) = hive_types::review::standing(&reviews, &args.user) { + if let Some(existing) = super::latest_reviews(client, client.repo()?, args.number)? + .into_iter() + .find(|r| r.login == args.user && !r.superseded()) + { println!( "not re-requesting: {} already has a standing {} review on #{} (re-requesting would dismiss it)", args.user, existing.state, args.number diff --git a/hive-types/README.md b/hive-types/README.md index 345140198..9a0579175 100644 --- a/hive-types/README.md +++ b/hive-types/README.md @@ -17,10 +17,6 @@ Foundational shared **newtypes** for the hyperhive workspace. A zero-dependency - **`term_msg`** — the icons and summary prefixes of a terminal row that sent a message or started a turn from one, and the parsers that read the recipient or sender back out. -- **`review`** — which of a PR's reviews still stand (latest verdict per - reviewer, stale or dismissed ones superseded). `hive-forge pr assign-reviewer` - and the notification router's `request-reviewer` sink share it, so both skip a - request on the same grounds. The crate is deliberately a leaf with the smallest possible dependency footprint so anything in the workspace can depend on it. See `docs/process/conventions.md` diff --git a/hive-types/src/lib.rs b/hive-types/src/lib.rs index 84d834d42..0ef2e23df 100644 --- a/hive-types/src/lib.rs +++ b/hive-types/src/lib.rs @@ -1,5 +1,5 @@ -//! Foundational shared types for the hyperhive workspace: [`Ident`], [`Permission`], -//! the [`term_msg`] row markers and the [`review`] rule. +//! Foundational shared types for the hyperhive workspace: [`Ident`], [`Permission`] +//! and the [`term_msg`] row markers. //! //! A zero-dependency (bar `serde`) leaf crate so every wire-type crate //! (`hive-sh4re`, `hive-host-sock`, `hive-core-agent-sock`) and both binaries @@ -11,7 +11,6 @@ mod permission; pub use permission::Permission; -pub mod review; pub mod term_msg; /// The environment variable nix uses to hand this process the blacklist of diff --git a/hive-types/src/review.rs b/hive-types/src/review.rs deleted file mode 100644 index d21360c6f..000000000 --- a/hive-types/src/review.rs +++ /dev/null @@ -1,231 +0,0 @@ -//! Which of a PR's reviews still stand: the rule `hive-forge pr -//! assign-reviewer` and the notification router's `request-reviewer` sink -//! share, so a request is skipped or made on the same grounds in both. -//! -//! The forge keeps a review's `state` (`APPROVED`, `REQUEST_CHANGES`, …) after -//! the head moves or the review is dismissed, so reading the state alone -//! reports a review that no longer applies as still good. [`latest_reviews`] -//! folds a PR's reviews to one verdict per reviewer, and -//! [`ReviewInfo::superseded`] says whether it still counts. -//! -//! Re-requesting a review from a reviewer whose review stands makes the forge -//! dismiss it, approval included. [`standing`] is the check a caller makes -//! before it requests. - -/// One review as the forge lists it, reduced to what the rule reads. Every -/// field is optional because the forge's listing is. -#[derive(Clone, Copy, Debug, Default)] -pub struct Listed<'a> { - pub login: Option<&'a str>, - pub state: Option<&'a str>, - /// The commit the review was submitted against. - pub commit_id: Option<&'a str>, - /// The forge's own `stale` flag. - pub stale: Option, - pub dismissed: Option, -} - -/// One reviewer's latest verdict on a PR, plus the `stale` / `dismissed` bits. -/// -/// `stale` is set by the forge when the PR head commit changed after this -/// review was submitted; `dismissed` is set when the review was explicitly -/// dismissed. Either way the review no longer applies to the current head even -/// though its `state` string still reads `APPROVED` / `REQUEST_CHANGES`. -/// -/// ⚠️ **`stale` here is not the forge's flag alone.** That flag is eventually -/// consistent: seconds after a push it still reports the pre-push answer, so a -/// verdict against the previous head reads as current in exactly the window -/// where a push has just happened. [`latest_reviews`] therefore ORs it with a -/// direct comparison of the review's own `commit_id` against the PR head: the -/// flag is right *eventually*, the comparison is right *immediately*. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct ReviewInfo { - pub login: String, - pub state: String, - pub stale: bool, - pub dismissed: bool, -} - -impl ReviewInfo { - /// True when the review no longer applies to the current head: stale - /// (head moved) or dismissed. Such a verdict neither blocks a merge nor - /// counts as a fresh approval. - #[must_use] - pub fn superseded(&self) -> bool { - self.stale || self.dismissed - } -} - -/// `login`'s review that still stands, if it has one. Its presence is the -/// reason not to request a review from `login` again. -#[must_use] -pub fn standing<'a>(reviews: &'a [ReviewInfo], login: &str) -> Option<&'a ReviewInfo> { - reviews.iter().find(|r| r.login == login && !r.superseded()) -} - -/// Whether this review was submitted against a commit that is no longer the PR -/// head: the race-free half of the staleness check. -/// -/// Fails **closed on unknowns**, i.e. "not stale": either side missing means -/// the head's move can't be shown, and guessing wrong in that direction costs -/// one redundant re-review, where the other direction would void every -/// verdict on the PR and invite a re-request that dismisses a real approval. -#[must_use] -pub fn reviewed_older_head(reviewed_sha: Option<&str>, head_sha: Option<&str>) -> bool { - let (Some(head), Some(reviewed)) = (head_sha, reviewed_sha) else { - return false; - }; - // Both emptiness checks matter, and for the same reason: a blank string is - // a value the forge sent, not a sha it has. Treating one as real would make - // every review compare unequal and mark the whole PR stale. - !head.is_empty() && !reviewed.is_empty() && reviewed != head -} - -/// Latest non-comment review per reviewer. `reviews` come oldest first, so a -/// later verdict from the same reviewer supersedes an earlier one; `COMMENT` -/// and `PENDING` reviews carry no verdict and are skipped. `head_sha` is the -/// PR's current head, or `None` when it couldn't be read, which leaves the -/// forge's `stale` flag alone to decide. -#[must_use] -pub fn latest_reviews<'a>( - reviews: impl IntoIterator>, - head_sha: Option<&str>, -) -> Vec { - let mut latest: Vec = Vec::new(); - for r in reviews { - let Some(login) = r.login else { continue }; - let verdict = r.state.unwrap_or(""); - if verdict == "COMMENT" || verdict == "PENDING" || verdict.is_empty() { - continue; - } - let stale = r.stale.unwrap_or(false) || reviewed_older_head(r.commit_id, head_sha); - let dismissed = r.dismissed.unwrap_or(false); - if let Some(slot) = latest.iter_mut().find(|info| info.login == login) { - verdict.clone_into(&mut slot.state); - slot.stale = stale; - slot.dismissed = dismissed; - } else { - latest.push(ReviewInfo { - login: login.to_owned(), - state: verdict.to_owned(), - stale, - dismissed, - }); - } - } - latest -} - -#[cfg(test)] -mod tests { - use super::*; - - fn review<'a>(login: &'a str, state: &'a str, commit: &'a str) -> Listed<'a> { - Listed { - login: Some(login), - state: Some(state), - commit_id: Some(commit), - ..Listed::default() - } - } - - #[test] - fn review_on_an_older_commit_is_stale() { - // The forge still reports `stale: false` here in the seconds after a - // push, so the comparison has to catch it. - assert!(reviewed_older_head(Some("81292f14"), Some("f4c47088"))); - } - - #[test] - fn review_on_the_current_head_is_not_stale() { - assert!(!reviewed_older_head(Some("f4c47088"), Some("f4c47088"))); - } - - /// Every unknown fails *toward* keeping the verdict. Voiding every review - /// on a forge that stopped reporting one of these would be a far louder - /// wrong answer than one redundant re-review. - #[test] - fn unknown_commit_or_head_is_not_stale() { - assert!(!reviewed_older_head(None, Some("f4c47088"))); - assert!(!reviewed_older_head(Some("81292f14"), None)); - assert!(!reviewed_older_head(Some(""), Some("f4c47088"))); - // Absent and blank have to behave the same on BOTH sides: a blank head - // that counted as real would mark every review on the PR stale. - assert!(!reviewed_older_head(Some("81292f14"), Some(""))); - assert!(!reviewed_older_head(Some(""), Some(""))); - } - - #[test] - fn a_review_of_the_current_head_stands_and_one_of_an_older_head_does_not() { - let reviews = latest_reviews( - [ - review("review-classifier", "APPROVED", "new"), - review("argus", "APPROVED", "old"), - ], - Some("new"), - ); - assert_eq!( - standing(&reviews, "review-classifier").map(|r| r.state.as_str()), - Some("APPROVED") - ); - // The forge's flag is still false for argus: only the commit says so. - assert!(standing(&reviews, "argus").is_none()); - // Control: a reviewer who never reviewed has nothing standing. - assert!(standing(&reviews, "iris").is_none()); - } - - #[test] - fn a_dismissed_or_flagged_review_does_not_stand() { - let reviews = latest_reviews( - [ - Listed { - dismissed: Some(true), - ..review("a", "APPROVED", "new") - }, - Listed { - stale: Some(true), - ..review("b", "REQUEST_CHANGES", "new") - }, - review("c", "APPROVED", "new"), - ], - Some("new"), - ); - assert!(standing(&reviews, "a").is_none()); - assert!(standing(&reviews, "b").is_none()); - assert!(standing(&reviews, "c").is_some(), "control"); - } - - #[test] - fn the_latest_verdict_per_reviewer_wins_and_comments_carry_none() { - let reviews = latest_reviews( - [ - review("a", "REQUEST_CHANGES", "old"), - review("a", "APPROVED", "new"), - review("a", "COMMENT", "new"), - review("b", "PENDING", "new"), - Listed { - login: None, - ..review("x", "APPROVED", "new") - }, - ], - Some("new"), - ); - assert_eq!( - reviews, - [ReviewInfo { - login: "a".to_owned(), - state: "APPROVED".to_owned(), - stale: false, - dismissed: false, - }] - ); - } - - /// With the head unread, the forge's flag decides alone, so a review of an - /// unknown commit stands. - #[test] - fn an_unread_head_leaves_the_forges_flag_to_decide() { - let reviews = latest_reviews([review("a", "APPROVED", "old")], None); - assert!(standing(&reviews, "a").is_some()); - } -} diff --git a/swarm-controller/src/forge.rs b/swarm-controller/src/forge.rs index e8689b925..e0e8d1086 100644 --- a/swarm-controller/src/forge.rs +++ b/swarm-controller/src/forge.rs @@ -55,7 +55,6 @@ pub mod legacy_tokens; pub mod objects; pub mod operator_profiles; pub mod pull_diff; -pub mod review_request; pub mod runner_token; pub mod site_admin; @@ -1761,12 +1760,12 @@ fn hook_registration(kind: DeliveryKind) -> (&'static [&'static str], HookScope< // classifier's account access can request its review. DeliveryKind::ReviewRequest => (&["pull_request_review_request"], HookScope::Instance), // Instance-wide, for the notification router, which reads the assign - // events and a push to a PR's branch. They are named: Forgejo's web - // form shows each as a box of its own, so a hook edited there can drop - // one while keeping `issues` or `pull_request`. + // events. They are named: Forgejo's web form shows assignment as a + // box of its own, so a hook edited there can drop it while keeping + // `issues` or `pull_request`. DeliveryKind::Issues => (&["issues", "issue_assign"], HookScope::Instance), DeliveryKind::PullRequest => ( - &["pull_request", "pull_request_assign", "pull_request_sync"], + &["pull_request", "pull_request_assign"], HookScope::Instance, ), } @@ -2112,7 +2111,7 @@ mod tests { assert_eq!(events(DeliveryKind::Issues), ["issues", "issue_assign"]); assert_eq!( events(DeliveryKind::PullRequest), - ["pull_request", "pull_request_assign", "pull_request_sync"] + ["pull_request", "pull_request_assign"] ); // The events a stored flow's source may name are the ones the hooks // deliver to the router. diff --git a/swarm-controller/src/forge/review_request.rs b/swarm-controller/src/forge/review_request.rs deleted file mode 100644 index 047e3c9e2..000000000 --- a/swarm-controller/src/forge/review_request.rs +++ /dev/null @@ -1,194 +0,0 @@ -//! A PR's reviews, and asking for one more, under the controller's token. -//! -//! The router's `request_reviewer` sink reaches the forge through here -//! ([`crate::request_reviewer`]). Which reviews still stand is -//! [`hive_types::review`], the rule `hive-forge pr assign-reviewer` applies -//! too. - -use anyhow::{Context, Result}; -use forgejo_api::structs::PullReviewRequestOptions; -use hive_types::review::{Listed, ReviewInfo, latest_reviews}; - -use super::Client; - -/// One review listing takes this many pages at most, 50 reviews a page. -const MAX_PAGES: u32 = 10; -const PAGE_SIZE: u32 = 50; - -impl Client { - /// The latest verdict of each reviewer of PR `number` of `owner/repo`, - /// judged against the PR's head as it is now. - /// - /// # Errors - /// A forge failure, with the [`forgejo_api::ForgejoError`] underneath. - pub async fn pull_reviews( - &self, - owner: &str, - repo: &str, - number: i64, - ) -> Result> { - let pull = self - .api - .call(|api| api.repo_get_pull_request(owner, repo, number).into_future()) - .await - .with_context(|| format!("read PR {number} of {owner}/{repo}"))?; - let head = pull.head.and_then(|h| h.sha); - let mut reviews = Vec::new(); - for page in 1..=MAX_PAGES { - let (_, batch) = self - .api - .call(|api| { - api.repo_list_pull_reviews(owner, repo, number) - .page(page) - .page_size(PAGE_SIZE) - .into_future() - }) - .await - .with_context(|| format!("list the reviews of PR {number} of {owner}/{repo}"))?; - let short = batch.len() < PAGE_SIZE as usize; - reviews.extend(batch); - if short { - break; - } - } - Ok(latest_reviews( - reviews.iter().map(|r| Listed { - login: r.user.as_ref().and_then(|u| u.login.as_deref()), - state: r.state.as_deref(), - commit_id: r.commit_id.as_deref(), - stale: r.stale, - dismissed: r.dismissed, - }), - head.as_deref(), - )) - } - - /// Ask `reviewer` to review PR `number` of `owner/repo`. From a reviewer - /// whose review stands, the forge dismisses it: check - /// [`hive_types::review::standing`] first. - /// - /// # Errors - /// The forge refused: no such reviewer, the PR's author, a reviewer with - /// no access to the repo. - pub async fn request_review( - &self, - owner: &str, - repo: &str, - number: i64, - reviewer: &str, - ) -> Result<()> { - let body = PullReviewRequestOptions { - reviewers: Some(vec![reviewer.to_owned()]), - team_reviewers: None, - }; - self.api - .call(|api| { - api.repo_create_pull_review_requests(owner, repo, number, body.clone()) - .into_future() - }) - .await - .with_context(|| { - format!("request {reviewer}'s review of PR {number} of {owner}/{repo}") - })?; - Ok(()) - } -} - -#[cfg(test)] -mod tests { - use std::sync::{Arc, Mutex}; - - use axum::http::{Method, StatusCode, Uri}; - - use super::*; - - /// A stand-in forge holding PR 7 of `o/r` at head `new`, with `reviews` - /// listed, answering the request post with `post`. Records each request as - /// method, path and body. - async fn forge( - reviews: &'static str, - post: u16, - ) -> (Client, Arc>>) { - let seen = Arc::new(Mutex::new(Vec::new())); - let recorded = Arc::clone(&seen); - let app = axum::Router::new().fallback( - move |method: Method, uri: Uri, body: axum::body::Bytes| { - let recorded = Arc::clone(&recorded); - async move { - recorded.lock().unwrap().push(( - method.to_string(), - uri.path().to_owned(), - String::from_utf8_lossy(&body).into_owned(), - )); - match (method.as_str(), uri.path()) { - ("GET", "/api/v1/repos/o/r/pulls/7") => ( - StatusCode::OK, - r#"{"number":7,"head":{"sha":"new"},"requested_reviewers":[]}"# - .to_owned(), - ), - ("GET", "/api/v1/repos/o/r/pulls/7/reviews") => { - (StatusCode::OK, reviews.to_owned()) - } - ("POST", "/api/v1/repos/o/r/pulls/7/requested_reviewers") => ( - StatusCode::from_u16(post).unwrap(), - if post == 201 { - "[]".to_owned() - } else { - r#"{"message":"refused"}"#.to_owned() - }, - ), - _ => (StatusCode::NOT_FOUND, r#"{"message":"no"}"#.to_owned()), - } - } - }, - ); - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); - let url = url::Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap(); - tokio::spawn(async move { axum::serve(listener, app).await }); - (Client::for_test_forge(url), seen) - } - - const REVIEWS: &str = r#"[ - {"id":1,"user":{"login":"review-classifier"},"state":"APPROVED","commit_id":"old","stale":false,"dismissed":false}, - {"id":2,"user":{"login":"argus"},"state":"APPROVED","commit_id":"new","stale":false,"dismissed":false} - ]"#; - - /// The head is read from the PR, so a review of an older commit is - /// superseded though the forge still reports `stale: false`. - #[tokio::test] - async fn reviews_are_judged_against_the_head_read_now() { - let (client, _) = forge(REVIEWS, 201).await; - let reviews = client.pull_reviews("o", "r", 7).await.unwrap(); - let standing = |login| hive_types::review::standing(&reviews, login).is_some(); - assert!( - !standing("review-classifier"), - "reviewed `old`, head is `new`" - ); - assert!(standing("argus"), "control: reviewed the head"); - } - - #[tokio::test] - async fn a_request_posts_the_reviewer_to_the_pull_requests_reviewer_list() { - let (client, seen) = forge("[]", 201).await; - client - .request_review("o", "r", 7, "review-classifier") - .await - .unwrap(); - let seen = seen.lock().unwrap(); - let (method, path, body) = seen.last().unwrap(); - assert_eq!(method, "POST"); - assert_eq!(path, "/api/v1/repos/o/r/pulls/7/requested_reviewers"); - let body: serde_json::Value = serde_json::from_str(body).unwrap(); - assert_eq!(body["reviewers"], serde_json::json!(["review-classifier"])); - } - - #[tokio::test] - async fn a_refused_request_is_an_error_naming_the_reviewer() { - let (client, _) = forge("[]", 422).await; - let err = client - .request_review("o", "r", 7, "review-classifier") - .await - .unwrap_err(); - assert!(format!("{err:#}").contains("review-classifier"), "{err:#}"); - } -} diff --git a/swarm-controller/src/main.rs b/swarm-controller/src/main.rs index b59030ebe..7439a410f 100644 --- a/swarm-controller/src/main.rs +++ b/swarm-controller/src/main.rs @@ -55,7 +55,6 @@ mod otel_http_client; mod pulls; mod queue_identity; mod read_policy; -mod request_reviewer; mod review_classifier; mod routes; mod schedules; @@ -240,11 +239,9 @@ async fn main() -> Result<()> { serve(listener, state).await } -/// Start taking the queued review requests and answering the notification -/// router's, then serve `state` on `listener`. +/// Start taking the queued review requests, then serve `state` on `listener`. async fn serve(listener: tokio::net::UnixListener, state: AppState) -> Result<()> { review_classifier::review::spawn_queue(&state); - request_reviewer::spawn(&state); axum::serve(listener, build_app(state)) .await .context("serving swarm-controller") diff --git a/swarm-controller/src/notify_flows.rs b/swarm-controller/src/notify_flows.rs index 96de238fc..343c6af78 100644 --- a/swarm-controller/src/notify_flows.rs +++ b/swarm-controller/src/notify_flows.rs @@ -294,8 +294,7 @@ mod tests { let Json(view) = get_flows(State(state.clone())).await.expect("get"); assert_eq!(view.revision, 0); assert!(view.disabled_builtins.is_empty() && view.flows.is_empty()); - assert_eq!(view.builtins.len(), 4); - assert!(view.builtins.iter().any(|f| f.id == "head-classifier")); + assert_eq!(view.builtins.len(), 3); let json = serde_json::to_value(&view).expect("serializes"); assert_eq!(json["schema"]["limits"]["max_nodes"], 64); assert_eq!(json["schema"]["node_kinds"][0]["kind"], "source"); @@ -322,7 +321,7 @@ mod tests { .expect("read") .expect("stored"); let doc = Document::decode(&bytes).expect("the router reads it"); - assert_eq!(doc.active().0.len(), 4, "3 built-ins and the custom flow"); + assert_eq!(doc.active().0.len(), 3, "2 built-ins and the custom flow"); // A refused save changes nothing: an orphan node, and a built-in that does not exist. let mut orphan = custom("orphan"); diff --git a/swarm-controller/src/request_reviewer.rs b/swarm-controller/src/request_reviewer.rs deleted file mode 100644 index 689b27daa..000000000 --- a/swarm-controller/src/request_reviewer.rs +++ /dev/null @@ -1,388 +0,0 @@ -//! Requesting a review for the notification router, which holds no forge -//! token. -//! -//! The router sends a [`RequestReviewer`] on -//! `swarm_wire::router_sink::REQUEST_REVIEWER_SUBJECT` and [`spawn`]'s -//! responder answers with an [`Outcome`] on the reply subject, having acted -//! under the controller's token. A reviewer whose review of the PR's head -//! already stands is skipped: requesting it again makes the forge dismiss it, -//! approval included. What stands is [`hive_types::review`], the rule -//! `hive-forge pr assign-reviewer` applies too. -//! -//! The request may be repeated (the router's event comes back after a failed -//! step): a reviewer with a request pending is a no-op for the forge, and one -//! who has reviewed since stands and is skipped. - -use std::future::Future; -use std::sync::Arc; -use std::time::Duration; - -use futures_util::StreamExt as _; -use hive_types::review::{ReviewInfo, standing}; -use swarm_wire::router_sink::{Outcome, REQUEST_REVIEWER_SUBJECT, RequestReviewer, WORK_DEADLINE}; - -use crate::forge; -use crate::jobq::Late; -use crate::review_classifier::split_repo; -use crate::routes::AppState; - -/// How long to wait before subscribing again. -const RETRY: Duration = Duration::from_secs(5); - -/// The forge as the responder uses it: [`forge::Client`] in production, a fake -/// in the tests. -pub(crate) trait ReviewForge { - fn reviews( - &self, - owner: &str, - repo: &str, - number: i64, - ) -> impl Future>> + Send; - - fn request( - &self, - owner: &str, - repo: &str, - number: i64, - reviewer: &str, - ) -> impl Future> + Send; -} - -impl ReviewForge for forge::Client { - async fn reviews( - &self, - owner: &str, - repo: &str, - number: i64, - ) -> anyhow::Result> { - self.pull_reviews(owner, repo, number).await - } - - async fn request( - &self, - owner: &str, - repo: &str, - number: i64, - reviewer: &str, - ) -> anyhow::Result<()> { - self.request_review(owner, repo, number, reviewer).await - } -} - -/// Answer the router's review requests for as long as the controller runs. -pub(crate) fn spawn(state: &AppState) { - let Some(client) = state.status.as_ref().map(|s| s.queue_client()) else { - tracing::warn!("no swarm queue is configured on this host; review requests go unanswered"); - return; - }; - let forge = Arc::clone(&state.forge); - tokio::spawn(async move { follow(&client, &forge).await }); -} - -/// Subscribe to the requests and answer each, and subscribe again if the -/// subscription ends. -async fn follow(client: &async_nats::Client, forge: &Late>) { - loop { - match client.subscribe(REQUEST_REVIEWER_SUBJECT).await { - Ok(mut requests) => { - tracing::info!( - subject = REQUEST_REVIEWER_SUBJECT, - "request reviewer: answering requests" - ); - while let Some(msg) = requests.next().await { - let Some(reply) = msg.reply.clone() else { - continue; - }; - let outcome = answer(forge.get().map(Arc::as_ref), &msg.payload).await; - match &outcome { - Outcome::Error(why) => { - tracing::warn!(%why, "request reviewer: not requested"); - } - other => { - tracing::info!(outcome = %other.encode(), "request reviewer: answered"); - } - } - if let Err(e) = client.publish(reply, outcome.encode().into()).await { - tracing::warn!(error = %e, "request reviewer: answering failed"); - } - } - tracing::warn!("request reviewer: subscription ended"); - } - Err(e) => tracing::warn!(error = %e, "request reviewer: subscribe failed"), - } - tokio::time::sleep(RETRY).await; - } -} - -/// What to answer to one request `payload`, `forge` being `None` while the -/// controller has no forge access. -async fn answer(forge: Option<&F>, payload: &[u8]) -> Outcome { - let request: RequestReviewer = match serde_json::from_slice(payload) { - Ok(request) => request, - Err(e) => return Outcome::Error(format!("the request does not decode: {e}")), - }; - let Some(forge) = forge else { - return Outcome::Error("this controller has no forge access yet".to_owned()); - }; - match tokio::time::timeout(WORK_DEADLINE, act(forge, &request)).await { - Ok(outcome) => outcome, - Err(_) => Outcome::Error(format!( - "the forge did not answer within {}s", - WORK_DEADLINE.as_secs() - )), - } -} - -async fn act(forge: &F, request: &RequestReviewer) -> Outcome { - let RequestReviewer { - repo, - number, - reviewer, - } = request; - let (owner, name) = match split_repo(repo) { - Ok(split) => split, - Err(why) => return Outcome::Error(why), - }; - let Ok(number_i64) = i64::try_from(*number) else { - return Outcome::Error(format!("PR number {number} is out of range")); - }; - if reviewer.trim().is_empty() { - return Outcome::Error("no reviewer named".to_owned()); - } - let reviews = match forge.reviews(owner, name, number_i64).await { - Ok(reviews) => reviews, - Err(e) => return Outcome::Error(format!("{e:#}")), - }; - if let Some(review) = standing(&reviews, reviewer) { - return Outcome::Skipped(format!( - "{reviewer} already has a standing {} review on {repo}#{number}", - review.state - )); - } - match forge.request(owner, name, number_i64, reviewer).await { - Ok(()) => Outcome::Requested, - Err(e) => Outcome::Error(format!("{e:#}")), - } -} - -#[cfg(test)] -mod tests { - use std::sync::Mutex; - - use super::*; - - #[derive(Default)] - struct Fake { - reviews: Vec, - reviews_fail: bool, - request_fail: bool, - asked: Mutex>, - } - - impl ReviewForge for Fake { - async fn reviews(&self, _: &str, _: &str, _: i64) -> anyhow::Result> { - if self.reviews_fail { - anyhow::bail!("the forge is down"); - } - Ok(self.reviews.clone()) - } - - async fn request( - &self, - owner: &str, - repo: &str, - number: i64, - reviewer: &str, - ) -> anyhow::Result<()> { - if self.request_fail { - anyhow::bail!("422: the reviewer is the PR's author"); - } - self.asked.lock().unwrap().push(( - format!("{owner}/{repo}"), - number, - reviewer.to_owned(), - )); - Ok(()) - } - } - - fn review(login: &str, verdict: &str, stale: bool, dismissed: bool) -> ReviewInfo { - ReviewInfo { - login: login.to_owned(), - state: verdict.to_owned(), - stale, - dismissed, - } - } - - fn payload(repo: &str, number: u64, reviewer: &str) -> Vec { - serde_json::to_vec(&RequestReviewer { - repo: repo.to_owned(), - number, - reviewer: reviewer.to_owned(), - }) - .unwrap() - } - - fn asked(fake: &Fake) -> Vec<(String, i64, String)> { - fake.asked.lock().unwrap().clone() - } - - #[tokio::test] - async fn a_reviewer_with_no_review_is_requested() { - let fake = Fake::default(); - let got = answer(Some(&fake), &payload("o/r", 7, "review-classifier")).await; - assert_eq!(got, Outcome::Requested); - assert_eq!( - asked(&fake), - [("o/r".to_owned(), 7, "review-classifier".to_owned())] - ); - } - - /// A review that still stands is skipped, because requesting from its - /// reviewer would make the forge dismiss it. - #[tokio::test] - async fn a_standing_review_is_skipped_and_a_stale_or_dismissed_one_is_requested_again() { - let reviewer = "review-classifier"; - let standing = Fake { - reviews: vec![review(reviewer, "APPROVED", false, false)], - ..Fake::default() - }; - let got = answer(Some(&standing), &payload("o/r", 7, reviewer)).await; - assert!( - matches!(&got, Outcome::Skipped(why) if why.contains("APPROVED") && why.contains("o/r#7")), - "{got:?}" - ); - assert!(asked(&standing).is_empty(), "nothing was requested"); - - for (stale, dismissed) in [(true, false), (false, true)] { - let fake = Fake { - reviews: vec![review(reviewer, "APPROVED", stale, dismissed)], - ..Fake::default() - }; - let got = answer(Some(&fake), &payload("o/r", 7, reviewer)).await; - assert_eq!( - got, - Outcome::Requested, - "stale {stale}, dismissed {dismissed}" - ); - assert_eq!(asked(&fake).len(), 1); - } - - // Another reviewer's standing review is no reason to skip this one. - let other = Fake { - reviews: vec![review("argus", "APPROVED", false, false)], - ..Fake::default() - }; - let got = answer(Some(&other), &payload("o/r", 7, reviewer)).await; - assert_eq!(got, Outcome::Requested); - } - - #[tokio::test] - async fn a_forge_failure_is_an_error_and_a_failed_read_requests_nothing() { - let down = Fake { - reviews_fail: true, - ..Fake::default() - }; - let got = answer(Some(&down), &payload("o/r", 7, "review-classifier")).await; - assert!( - matches!(&got, Outcome::Error(e) if e.contains("the forge is down")), - "{got:?}" - ); - assert!(asked(&down).is_empty()); - - let refused = Fake { - request_fail: true, - ..Fake::default() - }; - let got = answer(Some(&refused), &payload("o/r", 7, "review-classifier")).await; - assert!( - matches!(&got, Outcome::Error(e) if e.contains("422")), - "{got:?}" - ); - } - - #[tokio::test] - async fn a_request_that_is_malformed_or_has_no_forge_is_an_error() { - let fake = Fake::default(); - for bad in [ - b"not json".to_vec(), - payload("no-slash", 7, "review-classifier"), - payload("o/r", 7, " "), - payload("o/r", u64::MAX, "review-classifier"), - ] { - let got = answer(Some(&fake), &bad).await; - assert!( - matches!(got, Outcome::Error(_)), - "{:?}", - String::from_utf8_lossy(&bad) - ); - } - assert!(asked(&fake).is_empty()); - let none: Option<&Fake> = None; - let got = answer(none, &payload("o/r", 7, "review-classifier")).await; - assert!( - matches!(&got, Outcome::Error(e) if e.contains("no forge")), - "{got:?}" - ); - } - - /// The built-in flow's reviewer is the account the controller makes. - #[test] - fn the_built_in_flow_requests_the_classifiers_account() { - assert_eq!( - swarm_wire::notify_flows::CLASSIFIER_ACCOUNT, - forge::classifier_account::LOGIN - ); - } - - /// Against a real server: a request sent the way the router sends it comes - /// back with the outcome, and a subscriber before the controller is up - /// leaves the sender with no responders. - #[tokio::test] - async fn a_request_over_the_queue_is_answered_with_its_outcome() { - let nats = crate::test_nats::TestNats::start(); - let router = nats.client().await; - let ask = |body: Vec| { - let router = router.clone(); - async move { - let sent = async_nats::Request::new() - .payload(body.into()) - .timeout(Some(Duration::from_secs(5))); - router.send_request(REQUEST_REVIEWER_SUBJECT, sent).await - } - }; - assert!( - ask(payload("o/r", 7, "review-classifier")).await.is_err(), - "no controller listening" - ); - - let fake = Arc::new(Fake { - reviews: vec![review("argus", "APPROVED", false, false)], - ..Fake::default() - }); - let late: Late> = Arc::default(); - late.set(Arc::clone(&fake)).ok().unwrap(); - let controller = nats.client().await; - tokio::spawn(async move { follow(&controller, &late).await }); - // The subscription is processed once a request gets through. - let mut reply = None; - for _ in 0..50 { - if let Ok(msg) = ask(payload("o/r", 7, "review-classifier")).await { - reply = Some(msg); - break; - } - tokio::time::sleep(Duration::from_millis(100)).await; - } - let reply = reply.expect("the controller answers"); - assert_eq!( - Outcome::decode(&String::from_utf8_lossy(&reply.payload)), - Outcome::Requested - ); - let skipped = ask(payload("o/r", 7, "argus")).await.unwrap(); - assert!(matches!( - Outcome::decode(&String::from_utf8_lossy(&skipped.payload)), - Outcome::Skipped(_) - )); - } -} diff --git a/swarm-controller/src/review_classifier.rs b/swarm-controller/src/review_classifier.rs index d091d0050..b2a592997 100644 --- a/swarm-controller/src/review_classifier.rs +++ b/swarm-controller/src/review_classifier.rs @@ -468,34 +468,6 @@ async fn rules( owner: &str, name: &str, ) -> Result, ProblemDetails> { - match load_rules(files, owner, name).await? { - Loaded::Rules(rules) => Ok(Json(rules)), - Loaded::Absent { branch } => { - let [yml, json] = RULES_PATHS; - Err(error_problem( - StatusCode::NOT_FOUND, - &format!("{owner}/{name}'s default branch {branch} holds neither {yml} nor {json}"), - )) - } - } -} - -/// What the default branch holds of a repo's rules. -enum Loaded { - Rules(Rules), - /// Neither rules file exists on `branch`. - Absent { - branch: String, - }, -} - -/// Read the rules file of `owner/name` and parse it; a repo whose default -/// branch holds no rules file is [`Loaded::Absent`], not an error. -async fn load_rules( - files: &impl FileSource, - owner: &str, - name: &str, -) -> Result { let repo = format!("{owner}/{name}"); let read = forge::within_forge_deadline( "review rules", @@ -518,9 +490,13 @@ async fn load_rules( let [yml, json] = RULES_PATHS; let (path, bytes) = match read.files.as_slice() { [] => { - return Ok(Loaded::Absent { - branch: read.branch, - }); + return Err(error_problem( + StatusCode::NOT_FOUND, + &format!( + "{repo}'s default branch {} holds neither {yml} nor {json}", + read.branch + ), + )); } [file] => (&file.path, &file.bytes), _ => { @@ -536,7 +512,7 @@ async fn load_rules( let classifications = parse_rules(path, bytes) .map_err(|d| error_problem(StatusCode::UNPROCESSABLE_ENTITY, &d))?; tracing::info!(%repo, branch = %read.branch, commit = %read.commit, %path, "review rules read"); - Ok(Loaded::Rules(Rules { + Ok(Json(Rules { path: path.clone(), branch: read.branch, commit: read.commit, diff --git a/swarm-controller/src/review_classifier/review.rs b/swarm-controller/src/review_classifier/review.rs index 343e6ead8..7444d393f 100644 --- a/swarm-controller/src/review_classifier/review.rs +++ b/swarm-controller/src/review_classifier/review.rs @@ -2,11 +2,10 @@ //! ([`LOGIN`]) runs the repo's rules against the PR and posts the result as //! that account's review, on the commit the run read. //! -//! Only a review request starts a run. A push makes none itself: the -//! notification router's `head-classifier` flow requests the review. A fresh -//! request retires the account's earlier review: the forge clears the -//! `official` flag on a reviewer's previous reviews when they are requested -//! again, and only official reviews count toward approvals or block a merge. +//! Only a review request starts a run; a push does not. A fresh request +//! retires the account's earlier review: the forge clears the `official` flag +//! on a reviewer's previous reviews when they are requested again, and only +//! official reviews count toward approvals or block a merge. //! //! A request for a head whose newest recorded review approved or requested //! changes runs nothing: the account posts that verdict again, saying so. A @@ -825,8 +824,6 @@ impl SettingsSource for Queue { /// How far a run got. #[derive(Debug, Clone, PartialEq, Eq)] enum Outcome { - /// The repo has no rules file: nothing was asked of the classifier. - NoRules { repo: String }, /// Nothing reached the classifier, for this reason. NotRun(String), /// The classifier answered a run of `sent` classifications. @@ -861,7 +858,7 @@ async fn run( let sent = post(poster, req, head.as_deref(), ruling.review()).await; let answer = match &outcome { Outcome::Ran { reply, .. } => runs::answer(reply), - Outcome::NotRun(_) | Outcome::NoRules { .. } => (None, None), + Outcome::NotRun(_) => (None, None), }; record( req, @@ -1035,9 +1032,8 @@ async fn classify( }; let head = Some(pull.head_sha.clone()); let narrowed = pull.narrowed; - let rules = match super::load_rules(files, &req.owner, &req.name).await { - Ok(super::Loaded::Rules(rules)) => rules, - Ok(super::Loaded::Absent { .. }) => return (head, narrowed, Outcome::NoRules { repo }), + let rules = match super::rules(files, &req.owner, &req.name).await { + Ok(axum::Json(rules)) => rules, Err(problem) => { let detail = problem.detail.unwrap_or_default(); return ( @@ -1118,11 +1114,6 @@ impl Ruling { /// The ruling on a run's outcome. fn review_for(outcome: &Outcome) -> Ruling { match outcome { - Outcome::NoRules { repo } => Ruling { - event: Event::Approve, - body: format!("no review rules are configured for {repo}; approved without checks\n"), - reason: None, - }, Outcome::NotRun(reason) => Ruling::no_verdict(reason.clone(), &[]), Outcome::Ran { reply: Reply::Failed { error }, @@ -2318,8 +2309,7 @@ mod tests { } } - let [yml, _] = crate::review_classifier::RULES_PATHS; - let fake = Fake::new(failed, vec![rules_json(), (yml, String::new())]); + let fake = Fake::new(failed, Vec::new()); let run = run_read(&fake, &fake, &Posted::default(), &request("r")).await; assert_eq!(run.outcome, RunOutcome::NoVerdict); assert!( @@ -2740,65 +2730,13 @@ mod tests { assert_eq!(posted_commit.as_deref(), commit, "{repo}"); } - let [yml, _] = crate::review_classifier::RULES_PATHS; - let fake = Fake::new(approve(), vec![rules_json(), (yml, String::new())]); + let fake = Fake::new(approve(), Vec::new()); let poster = Posted::default(); run_read(&fake, &fake, &poster, &request("r")).await; let (_, _, review) = poster.0.into_inner().expect("unpoisoned").remove(0); assert_eq!(review.event, "COMMENT"); assert!( - review.body.contains("the rules did not load") && review.body.contains("holds both"), - "{}", - review.body - ); - } - - #[tokio::test] - async fn a_repo_without_rules_is_approved_without_a_model_call() { - let fake = Fake::new( - Reply::Failed { - error: "must not run".to_owned(), - }, - Vec::new(), - ); - let poster = Posted::default(); - let run = run_read(&fake, &fake, &poster, &request("r")).await; - assert_eq!(run.outcome, RunOutcome::Approved); - assert_eq!(run.detail, None); - assert_eq!(run.head_sha.as_deref(), Some("abc123")); - assert!(fake.sent.lock().expect("unpoisoned").is_empty()); - let reviews = poster.0.into_inner().expect("unpoisoned"); - assert_eq!(reviews.len(), 1); - let (pr, commit, review) = &reviews[0]; - assert_eq!(pr, "o/r#7"); - assert_eq!(commit.as_deref(), Some("abc123")); - assert_eq!(review.event, "APPROVED"); - assert_eq!( - review.body, - "no review rules are configured for o/r; approved without checks\n" - ); - - // A request for the same head approves again, still without a run. - let poster = Posted::default(); - let again = run_read(&fake, &fake, &poster, &request("r")).await; - assert_eq!(again.outcome, RunOutcome::Approved); - assert!(fake.sent.lock().expect("unpoisoned").is_empty()); - assert_eq!(poster.0.into_inner().expect("unpoisoned").len(), 1); - - // Control: a repo with rules still runs the classifier and posts its verdict. - let approve = Reply::Done { - rows: vec![row(None, true, true)], - overall: Overall::Approve, - }; - let with = Fake::new(approve, vec![rules_json()]); - let poster = Posted::default(); - let run = run_read(&with, &with, &poster, &request("r")).await; - assert_eq!(run.outcome, RunOutcome::Approved); - assert_eq!(with.sent.lock().expect("unpoisoned").len(), 1); - let (_, _, review) = poster.0.into_inner().expect("unpoisoned").remove(0); - assert_eq!(review.event, "APPROVED"); - assert!( - review.body.contains("Overall: ✅ approve"), + review.body.contains("the rules did not load") && review.body.contains("neither"), "{}", review.body ); @@ -2954,7 +2892,7 @@ mod tests { let body = narrowed(None, true).await; assert!(!body.contains("forge's diff"), "{body}"); let body = narrowed(reason(), false).await; - assert!(body.starts_with("no review rules are configured"), "{body}"); + assert!(body.starts_with("**No verdict:**"), "{body}"); assert!( !body.contains("forge's diff"), "a run that never ran read no diff: {body}" diff --git a/swarm-nats-auth/src/policy.rs b/swarm-nats-auth/src/policy.rs index 1a824a29c..e9ae32321 100644 --- a/swarm-nats-auth/src/policy.rs +++ b/swarm-nats-auth/src/policy.rs @@ -293,10 +293,6 @@ impl Policy { /// controller creates the bucket and is its one writer. /// - **The roster.** `STREAM.INFO` on the agent-status bucket, which lists /// its keys (`.`) and writes none. - /// - **Review requests.** Publish on the one sink subject - /// `swarm_wire::router_sink::REQUEST_REVIEWER_SUBJECT` and hear the answer - /// on its own inbox. The controller is the subject's one subscriber and - /// holds the forge token; no wildcard over `$SWARM.router.sink`. fn notify_router_permissions(client_id: &str) -> Permissions { use swarm_wire::{forge_events, notify_flows, notify_router, router_todo}; @@ -324,7 +320,6 @@ impl Policy { format!("$JS.API.STREAM.INFO.{flows}"), format!("$JS.API.DIRECT.GET.{flows}.>"), format!("$JS.API.STREAM.INFO.{}", Self::agent_status_stream()), - swarm_wire::router_sink::REQUEST_REVIEWER_SUBJECT.to_owned(), Self::inbox(client_id), ]); Permissions { @@ -352,8 +347,7 @@ impl Policy { /// What a reader may subscribe to: every agent's terminal, its /// subagents' terminals and its turn state, which the controller relays - /// to swarm-ui, every hive's permissions import, the notification router's - /// review requests, and its own reply inbox. + /// to swarm-ui, every hive's permissions import, and its own reply inbox. /// /// The three families are the publishers' (`hive-agent`'s `swarm_term` /// and `swarm_agent_state`, `swarm_wire::subagent_term`), spelled @@ -365,7 +359,6 @@ impl Policy { swarm_wire::subagent_term::subject_wildcard(), swarm_queue_client::agent_state_subject_wildcard(), swarm_wire::agent_permissions::import_subject_wildcard(), - swarm_wire::router_sink::REQUEST_REVIEWER_SUBJECT.to_owned(), Self::inbox(client_id), ] } @@ -1744,7 +1737,7 @@ mod tests { #[test] fn the_notify_router_drains_its_own_consumer_and_writes_only_its_own_state() { use swarm_queue_client::client_inbox_prefix; - use swarm_wire::{forge_events, notify_flows, notify_router, router_sink, router_todo}; + use swarm_wire::{forge_events, notify_flows, notify_router, router_todo}; let p = with_router() .permissions(NOTIFY_ROUTER) @@ -1765,7 +1758,6 @@ mod tests { router_todo::subject("atlas"), format!("$KV.{state}.42.5571"), format!("$JS.API.DIRECT.GET.KV_{state}.$KV.{state}.42.5571"), - router_sink::REQUEST_REVIEWER_SUBJECT.to_owned(), "$JS.API.STREAM.INFO.KV_agent-status".to_owned(), // The flows document: read it. format!("$JS.API.STREAM.INFO.KV_{flows}"), @@ -1791,8 +1783,6 @@ mod tests { forge_events::subject("issues"), "$KV.agent-status.alpha.atlas".to_owned(), "$KV.agent-schedules.atlas".to_owned(), - "$SWARM.router.sink.other".to_owned(), - "$SWARM.router.sink.>".to_owned(), // The flows document: not write it, nor make the bucket. format!("$KV.{flows}.{}", notify_flows::KEY), format!("$JS.API.STREAM.CREATE.KV_{flows}"), @@ -1808,48 +1798,6 @@ mod tests { assert_eq!(policy().permissions(NOTIFY_ROUTER), None); } - /// The router asks and the controller answers: nobody else may send the - /// request, and nobody else may hear it, since the answer acts under the - /// controller's forge token. - #[test] - fn only_the_router_may_send_a_review_request_and_only_the_reader_may_hear_it() { - use swarm_wire::router_sink::{REPLY_DEADLINE, REQUEST_REVIEWER_SUBJECT}; - - let p = with_router(); - let grants = [ - ( - "the reader", - p.permissions("swarm-controller").expect("reader"), - ), - ("a hive", p.permissions("hive-alpha").expect("a hive")), - ("the router", p.permissions(NOTIFY_ROUTER).expect("router")), - ("the minter", p.permissions(CLAUDE_LOGIN).expect("minter")), - ("the builder", p.permissions(BUILDER).expect("builder")), - ( - "the classifier", - p.permissions(CLASSIFIER).expect("classifier"), - ), - ("atlas", p.agent_token_permissions("atlas").expect("atlas")), - ]; - for (who, grant) in &grants { - let sends = grant - .publish - .iter() - .any(|g| covers(g, REQUEST_REVIEWER_SUBJECT)); - let hears = may_subscribe(grant, REQUEST_REVIEWER_SUBJECT); - assert_eq!(sends, *who == "the router", "{who} sends: {sends}"); - assert_eq!(hears, *who == "the reader", "{who} hears: {hears}"); - } - // The controller answers on the reply subject for as long as the - // router waits. - let reader = &grants[0].1; - assert!( - reader.responses >= Some(REPLY_DEADLINE), - "{:?}", - reader.responses - ); - } - #[test] fn only_the_router_may_queue_an_agents_todo_and_only_the_reader_may_keep_a_forge_event() { use swarm_wire::{forge_events, router_todo}; diff --git a/swarm-notify-router/src/flow.rs b/swarm-notify-router/src/flow.rs index 01a0fad84..bd64f67eb 100644 --- a/swarm-notify-router/src/flow.rs +++ b/swarm-notify-router/src/flow.rs @@ -5,9 +5,8 @@ //! type and this walk runs both. A `match` node passes when every one of its //! conditions holds, and then each of its children runs, so one event can //! reach several branches, in edge order, so a clear listed before an upsert -//! is queued before it. A `todo` node emits one op per recipient and a -//! `request_reviewer` node one request for a review. The router runs only -//! flows that pass [`Flow::validate`]. +//! is queued before it. A `todo` node emits one op per recipient. The router +//! runs only flows that pass [`Flow::validate`]. use std::collections::BTreeMap; @@ -33,27 +32,17 @@ pub struct Action { pub op: RouterTodoOp, } -/// One request for `reviewer`'s review of pull request `number` of `repo`. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct ReviewRequest { - pub flow: String, - pub node: String, - pub repo: String, - pub number: u64, - pub reviewer: String, -} - -/// Walk `flow` for one event of type `event_type`, calling `leaf` on each -/// `todo` and `request_reviewer` node the event reaches. Assumes -/// [`Flow::validate`] passed. -fn walk<'f>(flow: &'f Flow, event_type: &str, fields: &Fields, mut leaf: impl FnMut(&'f Node)) { +/// Walk `flow` for one event of type `event_type` and return the ops it +/// emits. Assumes [`Flow::validate`] passed. +pub fn evaluate(flow: &Flow, event_type: &str, fields: &Fields) -> Vec { let Some(root) = flow .nodes .iter() .find(|n| matches!(&n.kind, NodeKind::Source { events } if events.iter().any(|e| e == event_type))) else { - return; + return Vec::new(); }; + let mut actions = Vec::new(); let mut stack = vec![root.id.as_str()]; while let Some(id) = stack.pop() { let Some(node) = flow.node(id) else { continue }; @@ -64,55 +53,12 @@ fn walk<'f>(flow: &'f Flow, event_type: &str, fields: &Fields, mut leaf: impl Fn stack.extend(flow.children(id).into_iter().rev()); } } - NodeKind::Todo(_) | NodeKind::RequestReviewer { .. } => leaf(node), + NodeKind::Todo(sink) => actions.extend(emit(flow, node, sink, fields)), } } -} - -/// Walk `flow` for one event of type `event_type` and return the todo ops it -/// emits. Assumes [`Flow::validate`] passed. -pub fn evaluate(flow: &Flow, event_type: &str, fields: &Fields) -> Vec { - let mut actions = Vec::new(); - walk(flow, event_type, fields, |node| { - if let NodeKind::Todo(sink) = &node.kind { - actions.extend(emit(flow, node, sink, fields)); - } - }); actions } -/// Walk `flow` for one event of type `event_type` and return the review -/// requests it makes. An event that is not about a pull request makes none. -/// Assumes [`Flow::validate`] passed. -pub fn requests(flow: &Flow, event_type: &str, fields: &Fields) -> Vec { - let mut requests = Vec::new(); - walk(flow, event_type, fields, |node| { - if let NodeKind::RequestReviewer { reviewer } = &node.kind - && let Some(request) = request(flow, node, reviewer, fields) - { - requests.push(request); - } - }); - requests -} - -fn request(flow: &Flow, node: &Node, reviewer: &str, fields: &Fields) -> Option { - let text = |name: &str| match fields.get(name) { - Some(Value::Text(v)) => Some(v.as_str()), - _ => None, - }; - if text("kind") != Some("PR") { - return None; - } - Some(ReviewRequest { - flow: flow.id.clone(), - node: node.id.clone(), - repo: text("repo")?.to_owned(), - number: text("number")?.parse().ok()?, - reviewer: reviewer.to_owned(), - }) -} - fn emit(flow: &Flow, node: &Node, sink: &TodoSink, fields: &Fields) -> Vec { let recipients = match &sink.to { Recipients::Agent(agent) => vec![agent.clone()], @@ -387,41 +333,6 @@ mod tests { assert_eq!(got[0].op.source, "router:assigned"); } - /// A request sits under the same match gating as a todo, and names the - /// pull request the event concerns. - #[test] - fn a_reviewer_request_runs_under_its_match_and_only_for_a_pull_request() { - let mut f = flow("assigned"); - f.nodes[0].kind = NodeKind::Source { - events: vec!["pull_request_sync".to_owned()], - }; - f.nodes[2].kind = NodeKind::RequestReviewer { - reviewer: "argus".to_owned(), - }; - assert_eq!(f.validate(), Ok(())); - let pull = |action: &str, kind: &'static str| { - let mut fields = event(action, &[], &[], &[]); - fields.insert("kind", text(kind)); - fields - }; - let got = requests(&f, "pull_request_sync", &pull("assigned", "PR")); - assert_eq!( - got, - [ReviewRequest { - flow: "assigned".to_owned(), - node: "sink".to_owned(), - repo: "hyperhive/hyperhive".to_owned(), - number: 7, - reviewer: "argus".to_owned(), - }] - ); - assert!(requests(&f, "pull_request_sync", &pull("opened", "PR")).is_empty()); - assert!(requests(&f, "pull_request_sync", &pull("assigned", "issue")).is_empty()); - assert!(requests(&f, "pull_request", &pull("assigned", "PR")).is_empty()); - // A request is no todo. - assert!(evaluate(&f, "pull_request_sync", &pull("assigned", "PR")).is_empty()); - } - #[test] fn a_placeholder_expands_to_its_field_and_an_unknown_one_is_an_error() { let fields = event("assigned", &["a", "b"], &[], &[]); diff --git a/swarm-notify-router/src/main.rs b/swarm-notify-router/src/main.rs index 85c0c0693..b108b0d96 100644 --- a/swarm-notify-router/src/main.rs +++ b/swarm-notify-router/src/main.rs @@ -7,12 +7,6 @@ //! Each agent's harness applies them to its own todo list. The flows are the //! built-in ones, changed by the `notify-flows` document ([`stored`]). //! -//! A `request_reviewer` node is a request to swarm-controller, which holds the -//! forge token (`swarm_wire::router_sink`). It is sent once, after the event's -//! ops are queued and its snapshot is written, and the answer is only logged: -//! a controller that is down or slow must not hold up every agent's todos, and -//! the next push asks again. -//! //! Per issue it keeps a snapshot in its state bucket ([`event::Snapshot`]): //! the assignees it last saw and the agents it queued a todo for. An event is //! acked once its ops are queued and the snapshot is written, so a crash @@ -32,7 +26,6 @@ use async_nats::jetstream::{self, consumer::pull, kv, message::PublishMessage}; use futures_util::{StreamExt as _, TryStreamExt as _}; use swarm_queue_client::{ClientSecret, QueueConfig}; use swarm_secret_client::stored_secret::StoredSecret; -use swarm_wire::router_sink::{self, Outcome, RequestReviewer}; use swarm_wire::{forge_events, notify_router, router_todo}; mod event; @@ -110,7 +103,6 @@ async fn main() -> Result<()> { struct Router { js: jetstream::Context, - nats: async_nats::Client, events: jetstream::consumer::Consumer, state: kv::Store, flows: stored::Flows, @@ -152,7 +144,6 @@ impl Router { }; Ok(Self { js, - nats: queue.clone(), events, state, flows: stored::Flows::builtins(), @@ -209,52 +200,9 @@ impl Router { let bytes = serde_json::to_vec(&routed.next).expect("a snapshot encodes"); self.state.put(&key, bytes.into()).await.map_err(retry)?; } - for request in &routed.requests { - self.request_review(request).await; - } Ok(()) } - /// Ask swarm-controller for one review and log what it answered. One - /// attempt: nothing here fails the event. - async fn request_review(&self, request: &flow::ReviewRequest) { - let wire = RequestReviewer { - repo: request.repo.clone(), - number: request.number, - reviewer: request.reviewer.clone(), - }; - let body = serde_json::to_vec(&wire).expect("a review request encodes"); - let sent = async_nats::Request::new() - .payload(body.into()) - .timeout(Some(router_sink::REPLY_DEADLINE)); - let (flow, repo, number, reviewer) = ( - &request.flow, - &request.repo, - request.number, - &request.reviewer, - ); - match self - .nats - .send_request(router_sink::REQUEST_REVIEWER_SUBJECT, sent) - .await - { - Ok(reply) => match Outcome::decode(&String::from_utf8_lossy(&reply.payload)) { - Outcome::Requested => { - tracing::info!(%flow, %repo, number, %reviewer, "notify router: review requested"); - } - Outcome::Skipped(why) => { - tracing::info!(%flow, %repo, number, %reviewer, %why, "notify router: review not requested"); - } - Outcome::Error(why) => { - tracing::warn!(%flow, %repo, number, %reviewer, %why, "notify router: the controller could not request the review"); - } - }, - Err(e) => { - tracing::warn!(%flow, %repo, number, %reviewer, error = %e, "notify router: no answer to a review request; not retried"); - } - } - } - /// Queue one op on its agent's subject and wait for the stream's ack. async fn queue(&self, seq: u64, action: &flow::Action) -> Result<()> { let subject = router_todo::subject(&action.agent); diff --git a/swarm-notify-router/src/route.rs b/swarm-notify-router/src/route.rs index 83ae39ef6..db37448ff 100644 --- a/swarm-notify-router/src/route.rs +++ b/swarm-notify-router/src/route.rs @@ -8,16 +8,13 @@ use swarm_wire::router_todo::Op; use crate::event::{Delivery, Snapshot, fields, is_assign}; use swarm_wire::notify_flows::Flow; -use crate::flow::{Action, ReviewRequest, evaluate, requests}; +use crate::flow::{Action, evaluate}; /// What one event comes to. #[derive(Debug, PartialEq, Eq)] pub struct Routed { /// The ops to queue, one per agent that exists. pub actions: Vec, - /// The reviews to ask the forge for. Not limited to agents: a reviewer is - /// a forge account. - pub requests: Vec, /// Recipients that are not agents of this swarm, so got nothing. pub skipped: Vec, pub next: Snapshot, @@ -35,10 +32,6 @@ pub fn route( let fields = fields(event_type, delivery, snapshot); let mut actions = Vec::new(); let mut skipped = Vec::new(); - let mut review_requests = Vec::new(); - for flow in flows { - review_requests.extend(requests(flow, event_type, &fields)); - } for action in flows.iter().flat_map(|f| evaluate(f, event_type, &fields)) { if is_agent(&action.agent) { actions.push(action); @@ -69,7 +62,6 @@ pub fn route( next.holders.retain(|h| !cleared.contains(h)); Routed { actions, - requests: review_requests, skipped, next, } @@ -194,44 +186,6 @@ mod tests { ); } - /// A push to a PR makes one request for the classifier's review, whether - /// or not the classifier is an agent, and touches nothing else. - #[test] - fn a_push_to_a_pull_request_requests_the_classifiers_review_once() { - let flows = builtins(); - let pull = Delivery { - is_pull: true, - ..delivery("synchronized", &[]) - }; - let before = Snapshot { - assignees: Some(BTreeSet::from(["atlas".to_owned()])), - holders: BTreeSet::from(["atlas".to_owned()]), - }; - let r = route("pull_request_sync", &pull, &before, &flows, |_| false); - assert_eq!( - r.requests, - [ReviewRequest { - flow: "head-classifier".to_owned(), - node: "request".to_owned(), - repo: "o/r".to_owned(), - number: 9, - reviewer: "review-classifier".to_owned(), - }] - ); - assert!(r.actions.is_empty() && r.skipped.is_empty()); - assert_eq!(r.next, before); - - // The source gates: another PR event requests nothing. - for other in ["pull_request", "pull_request_assign", "issues"] { - let r = route(other, &pull, &before, &flows, |_| true); - assert!(r.requests.is_empty(), "{other}"); - } - // Only a PR can be asked to review. - let issue = delivery("synchronized", &[]); - let r = route("pull_request_sync", &issue, &before, &flows, |_| true); - assert!(r.requests.is_empty()); - } - #[test] fn an_event_no_flow_matches_changes_nothing() { let before = Snapshot { diff --git a/swarm-notify-router/src/stored.rs b/swarm-notify-router/src/stored.rs index af3993f56..765127830 100644 --- a/swarm-notify-router/src/stored.rs +++ b/swarm-notify-router/src/stored.rs @@ -188,28 +188,19 @@ mod tests { #[test] fn with_no_document_the_router_runs_the_builtins() { let mut f = Flows::builtins(); - assert_eq!( - ids(&f), - ["assigned", "unassigned", "closed", "head-classifier"] - ); + assert_eq!(ids(&f), ["assigned", "unassigned", "closed"]); f.apply(Read::Absent); - assert_eq!( - ids(&f), - ["assigned", "unassigned", "closed", "head-classifier"] - ); + assert_eq!(ids(&f), ["assigned", "unassigned", "closed"]); // An empty document is the same. f.apply(at(1, doc(&[], &[]))); - assert_eq!( - ids(&f), - ["assigned", "unassigned", "closed", "head-classifier"] - ); + assert_eq!(ids(&f), ["assigned", "unassigned", "closed"]); } #[test] fn a_document_switches_builtins_off_and_adds_custom_flows() { let mut f = Flows::builtins(); f.apply(at(1, doc(&["unassigned"], &["triage"]))); - assert_eq!(ids(&f), ["assigned", "closed", "head-classifier", "triage"]); + assert_eq!(ids(&f), ["assigned", "closed", "triage"]); } #[test] @@ -217,7 +208,7 @@ mod tests { let mut f = Flows::builtins(); f.apply(at(1, doc(&["closed"], &["triage"]))); let before = ids(&f).join(","); - assert_eq!(before, "assigned,unassigned,head-classifier,triage"); + assert_eq!(before, "assigned,unassigned,triage"); for bad in [ junk(2, b"not json"), junk(3, br#"{"version":2,"flows":[]}"#), @@ -230,20 +221,14 @@ mod tests { // The next good revision replaces them: the control that the bad // ones were kept out by being bad, not by the loader being stuck. f.apply(at(5, doc(&[], &[]))); - assert_eq!( - ids(&f), - ["assigned", "unassigned", "closed", "head-classifier"] - ); + assert_eq!(ids(&f), ["assigned", "unassigned", "closed"]); } #[test] fn a_bad_first_document_is_the_builtins_not_nothing() { let mut f = Flows::builtins(); f.apply(junk(1, br#"{"version":9}"#)); - assert_eq!( - ids(&f), - ["assigned", "unassigned", "closed", "head-classifier"] - ); + assert_eq!(ids(&f), ["assigned", "unassigned", "closed"]); } #[test] @@ -251,10 +236,7 @@ mod tests { let mut f = Flows::builtins(); f.apply(at(1, doc(&["closed"], &["triage"]))); f.apply(Read::Absent); - assert_eq!( - ids(&f), - ["assigned", "unassigned", "closed", "head-classifier"] - ); + assert_eq!(ids(&f), ["assigned", "unassigned", "closed"]); } #[test] @@ -272,9 +254,6 @@ mod tests { revision: 1, bytes: serde_json::to_vec(&d).unwrap(), }); - assert_eq!( - ids(&f), - ["assigned", "unassigned", "closed", "head-classifier"] - ); + assert_eq!(ids(&f), ["assigned", "unassigned", "closed"]); } } diff --git a/swarm-wire/src/lib.rs b/swarm-wire/src/lib.rs index 992e07fa0..3d0c9b467 100644 --- a/swarm-wire/src/lib.rs +++ b/swarm-wire/src/lib.rs @@ -21,7 +21,6 @@ pub mod notices; pub mod notify_flows; pub mod notify_router; pub mod review_classifier; -pub mod router_sink; pub mod router_todo; pub mod subagent_term; pub mod wanted; diff --git a/swarm-wire/src/notify_flows.rs b/swarm-wire/src/notify_flows.rs index af64870c1..c0504a1d2 100644 --- a/swarm-wire/src/notify_flows.rs +++ b/swarm-wire/src/notify_flows.rs @@ -4,10 +4,8 @@ //! A flow is a graph of nodes joined by edges, rooted at one `source` node. //! A `match` node passes when every one of its conditions holds, and then //! each of its children runs. A `todo` node is a leaf and emits one todo op -//! per recipient. A `request_reviewer` node is a leaf that asks the forge for -//! a review of the pull request the event concerns. Flows are trees: one -//! source, one parent for every other node, every node reachable from the -//! source ([`Flow::validate`]). +//! per recipient. Flows are trees: one source, one parent for every other +//! node, every node reachable from the source ([`Flow::validate`]). //! //! The document under [`KEY`] in [`BUCKET`] holds the ids of the built-in //! flows that are switched off and the custom flows. The built-ins @@ -58,10 +56,6 @@ pub const SOURCE_EVENTS: &[(&str, &str)] = &[ "pull_request_assign", "A pull request assigned or unassigned", ), - ( - "pull_request_sync", - "A push to the branch of an open pull request", - ), ]; /// The fields of an event a condition or a recipient can name, with what each @@ -87,10 +81,6 @@ pub const FIELDS: &[(&str, &str)] = &[ ), ]; -/// The forge account of the review classifier, whose review the built-in -/// `head-classifier` flow requests. -pub const CLASSIFIER_ACCOUNT: &str = "review-classifier"; - /// The todo ops a `todo` node's `op` takes, one per [`Op`] variant. const OPS: &[(&str, &str)] = &[ ( @@ -181,13 +171,6 @@ pub enum NodeKind { Match { when: Vec }, /// Emits a todo op to each recipient. Todo(TodoSink), - /// Asks the forge for `reviewer`'s review of the pull request the event - /// concerns, unless their review of its head already stands. Does nothing - /// for an issue. - RequestReviewer { - /// A forge login. - reviewer: String, - }, } /// `field` equals `value`. A list field holds when one of its items does. @@ -332,16 +315,6 @@ fn is_id(id: &str) -> bool { .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_') } -/// Whether `login` can name a forge account: Forgejo allows letters, digits, -/// `-`, `_` and `.`, and no more than 40 of them. -fn is_login(login: &str) -> bool { - !login.is_empty() - && login.len() <= 40 - && login - .bytes() - .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.')) -} - fn is_field(name: &str) -> bool { FIELDS.iter().any(|(n, _)| *n == name) } @@ -464,15 +437,6 @@ pub fn schema() -> Schema { ), ], }, - KindSchema { - kind: "request_reviewer".to_owned(), - description: "A leaf: asks the forge for a review of the pull request the event concerns, unless the reviewer's review of its head already stands; does nothing for an issue".to_owned(), - fields: vec![kind_field( - "reviewer", - "The forge login to request a review from", - true, - )], - }, ], limits: Limits { max_nodes: MAX_NODES, @@ -548,12 +512,8 @@ impl Flow { return Err(format!("{} source nodes, not one", roots.len())); }; for node in &self.nodes { - if matches!( - node.kind, - NodeKind::Todo(_) | NodeKind::RequestReviewer { .. } - ) && !self.children(&node.id).is_empty() - { - return Err(format!("{} has children, and is a leaf", node.id)); + if matches!(node.kind, NodeKind::Todo(_)) && !self.children(&node.id).is_empty() { + return Err(format!("todo {} has children", node.id)); } } // From the one root, every node reached exactly once: a node reached @@ -627,11 +587,6 @@ impl NodeKind { } } Self::Todo(sink) => sink.validate()?, - Self::RequestReviewer { reviewer } => { - if !is_login(reviewer) { - return Err(format!("reviewer {reviewer:?} is not a forge login")); - } - } } Ok(()) } @@ -677,7 +632,6 @@ impl TodoSink { /// then tells each agent that held one with a `closed` todo. The clear is the /// first child of the match and runs first: its prefix also covers the /// `closed` key. -/// - `head-classifier`: a push to a PR requests the review classifier's review. #[must_use] pub fn builtins() -> Vec { let assignment_key = "forge:{repo}#{number}:assignment"; @@ -708,7 +662,6 @@ pub fn builtins() -> Vec { }, ), closed(), - head_classifier(), ] } @@ -743,33 +696,6 @@ fn closed() -> Flow { flow } -/// source(`pull_request_sync`) → request the review classifier's review. -fn head_classifier() -> Flow { - Flow { - id: "head-classifier".to_owned(), - name: "head-classifier".to_owned(), - enabled: true, - nodes: vec![ - Node { - id: "source".to_owned(), - kind: NodeKind::Source { - events: vec!["pull_request_sync".to_owned()], - }, - }, - Node { - id: "request".to_owned(), - kind: NodeKind::RequestReviewer { - reviewer: CLASSIFIER_ACCOUNT.to_owned(), - }, - }, - ], - edges: vec![Edge { - from: "source".to_owned(), - to: "request".to_owned(), - }], - } -} - /// source(`events`) → match(`action` = `action`) → `sink`. fn chain(id: &str, events: &[&str], action: &str, sink: TodoSink) -> Flow { let node = |id: &str, kind| Node { @@ -882,7 +808,7 @@ mod tests { assert!(with_field("no_such_field").is_err(), "control"); let kinds: Vec<&str> = s.node_kinds.iter().map(|k| k.kind.as_str()).collect(); - assert_eq!(kinds, ["source", "match", "todo", "request_reviewer"]); + assert_eq!(kinds, ["source", "match", "todo"]); let op_names = |op: &str| -> Option { serde_json::from_value(serde_json::Value::String(op.to_owned())).ok() }; @@ -991,15 +917,10 @@ mod tests { #[test] fn a_node_that_reads_what_does_not_exist_is_refused() { let mut event = builtin("assigned"); - event.nodes[0].kind = NodeKind::Source { - events: vec!["pull_request_syn".to_owned()], - }; - assert!(event.validate().unwrap_err().contains("pull_request_syn")); - // Control: the spelling that exists passes. event.nodes[0].kind = NodeKind::Source { events: vec!["pull_request_sync".to_owned()], }; - assert_eq!(event.validate(), Ok(())); + assert!(event.validate().unwrap_err().contains("pull_request_sync")); let mut field = builtin("assigned"); field.nodes[1].kind = NodeKind::Match { @@ -1023,81 +944,6 @@ mod tests { assert_eq!(custom("c").validate(), Ok(())); } - /// A reviewer request is a leaf naming a forge login; the wire spells the - /// kind as the schema lists it. - #[test] - fn a_request_reviewer_node_is_a_leaf_naming_a_login() { - let with = |reviewer: &str| { - let mut f = custom("c"); - f.nodes[2].kind = NodeKind::RequestReviewer { - reviewer: reviewer.to_owned(), - }; - f.validate() - }; - assert_eq!(with("review-classifier"), Ok(())); - assert_eq!(with("a.b_c-1"), Ok(())); - for bad in ["", "two words", "a/b", "x@y", &"a".repeat(41)] { - assert!(with(bad).is_err(), "{bad:?}"); - } - - let mut parent = custom("c"); - parent.nodes[2].kind = NodeKind::RequestReviewer { - reviewer: "review-classifier".to_owned(), - }; - parent.nodes.push(Node { - id: "m2".to_owned(), - kind: NodeKind::Match { when: Vec::new() }, - }); - parent.edges.push(edge("sink", "m2")); - assert!( - parent.validate().unwrap_err().contains("leaf"), - "a reviewer request with a child" - ); - - let json = serde_json::to_value(Node { - id: "ask".to_owned(), - kind: NodeKind::RequestReviewer { - reviewer: "review-classifier".to_owned(), - }, - }) - .unwrap(); - assert_eq!( - json, - serde_json::json!({ - "id": "ask", - "kind": "request_reviewer", - "reviewer": "review-classifier", - }) - ); - let kind = json["kind"].as_str().unwrap(); - assert!( - schema().node_kinds.iter().any(|k| k.kind == kind), - "the schema lists the kind the wire uses" - ); - let node: Node = serde_json::from_value(json).unwrap(); - assert!(matches!(node.kind, NodeKind::RequestReviewer { .. })); - } - - /// The built-in that requests the classifier on every push. - #[test] - fn head_classifier_requests_the_classifier_on_a_push() { - let f = builtin("head-classifier"); - assert_eq!(f.validate(), Ok(())); - assert_eq!( - f.nodes[0].kind, - NodeKind::Source { - events: vec!["pull_request_sync".to_owned()] - } - ); - assert_eq!( - f.nodes[1].kind, - NodeKind::RequestReviewer { - reviewer: CLASSIFIER_ACCOUNT.to_owned() - } - ); - assert_eq!(f.children("source"), ["request"]); - } - #[test] fn a_placeholder_must_name_an_event_field() { let with = |key: &str, text: &str| { @@ -1184,7 +1030,7 @@ mod tests { let ids = |d: &Document| d.active().0.into_iter().map(|f| f.id).collect::>(); assert_eq!( ids(&Document::default()), - ["assigned", "unassigned", "closed", "head-classifier"], + ["assigned", "unassigned", "closed"], "an empty document is the built-ins" ); let mut off = custom("off"); @@ -1196,10 +1042,7 @@ mod tests { disabled_builtins: vec!["unassigned".to_owned(), "gone".to_owned()], flows: vec![custom("triage"), off, broken, custom("assigned")], }; - assert_eq!( - ids(&doc), - ["assigned", "closed", "head-classifier", "triage"] - ); + assert_eq!(ids(&doc), ["assigned", "closed", "triage"]); let (_, skipped) = doc.active(); assert_eq!(skipped.len(), 2, "{skipped:?}"); assert!(skipped.iter().any(|s| s.starts_with("flow broken:"))); diff --git a/swarm-wire/src/router_sink.rs b/swarm-wire/src/router_sink.rs deleted file mode 100644 index 72fc78064..000000000 --- a/swarm-wire/src/router_sink.rs +++ /dev/null @@ -1,105 +0,0 @@ -//! The notification router's sinks that act on the forge, which it asks -//! swarm-controller to run. -//! -//! The router has no forge token. For a `request_reviewer` node -//! ([`crate::notify_flows::NodeKind::RequestReviewer`]) it sends a request on -//! [`REQUEST_REVIEWER_SUBJECT`] and swarm-controller answers on the request's -//! reply subject with an [`Outcome`], having acted under its own token. -//! -//! Three crates name these: the router sends, the controller answers, and the -//! auth-callout responder grants each its side. The controller is the -//! subject's one subscriber and the router its one publisher. - -use std::time::Duration; - -use serde::{Deserialize, Serialize}; - -/// The subject a request for a review goes to. -pub const REQUEST_REVIEWER_SUBJECT: &str = "$SWARM.router.sink.request-reviewer"; - -/// How long the router waits for the answer. The controller's reply grant -/// lasts this long, so its own work stops sooner ([`WORK_DEADLINE`]). -pub const REPLY_DEADLINE: Duration = Duration::from_secs(30); - -/// How long the controller works on one request before it answers -/// [`Outcome::Error`]. -pub const WORK_DEADLINE: Duration = Duration::from_secs(20); - -/// Ask for `reviewer`'s review of pull request `number` of `repo`. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct RequestReviewer { - /// `owner/name`. - pub repo: String, - pub number: u64, - /// The reviewer's forge login. - pub reviewer: String, -} - -/// What the controller did with a [`RequestReviewer`], on the wire as one -/// line: `requested`, `skipped:` or `error:`. -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum Outcome { - /// The forge accepted the request. - Requested, - /// Nothing was sent, and why. - Skipped(String), - /// The request failed, and why. - Error(String), -} - -impl Outcome { - /// The wire line. - #[must_use] - pub fn encode(&self) -> String { - match self { - Self::Requested => "requested".to_owned(), - Self::Skipped(why) => format!("skipped:{why}"), - Self::Error(why) => format!("error:{why}"), - } - } - - /// The outcome a reply line says; a line that is none of the three is an - /// [`Outcome::Error`] saying so. - #[must_use] - pub fn decode(line: &str) -> Self { - if line == "requested" { - Self::Requested - } else if let Some(why) = line.strip_prefix("skipped:") { - Self::Skipped(why.to_owned()) - } else if let Some(why) = line.strip_prefix("error:") { - Self::Error(why.to_owned()) - } else { - Self::Error(format!("unreadable reply {line:?}")) - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn an_outcome_round_trips_its_line() { - for outcome in [ - Outcome::Requested, - Outcome::Skipped("a review stands".to_owned()), - Outcome::Error("the forge said: no: why".to_owned()), - ] { - assert_eq!(Outcome::decode(&outcome.encode()), outcome); - } - assert_eq!(Outcome::Requested.encode(), "requested"); - assert_eq!(Outcome::Skipped("x".to_owned()).encode(), "skipped:x"); - assert_eq!(Outcome::Error("x".to_owned()).encode(), "error:x"); - } - - #[test] - fn a_line_that_is_no_outcome_reads_as_an_error() { - assert!(matches!(Outcome::decode("ok"), Outcome::Error(e) if e.contains("ok"))); - } - - /// The reply grant covers the work and the router's wait covers the grant. - #[test] - fn the_controller_stops_working_before_the_router_stops_waiting() { - assert!(WORK_DEADLINE < REPLY_DEADLINE); - } -}