Watch
0
0
Fork
You've already forked hyperhive
0

agents get a per-agent repo limit in their permissions record, orgs are locked

max_repos (default 0, 0..=100) lives in the agent's agent-permissions record and is edited on its permissions panel. The controller applies each agent's own value as max_repo_creation at CreateForgeUser and to existing accounts on change, restart and every five minutes; agent accounts may not create organizations.
This commit is contained in:
atlas 2026-11-15 20:47:21 +01:00 • committed by mara
commit 1edeb5c55d
8 changed files with 763 additions and 58 deletions

View file

@ -25,7 +25,8 @@ use swarm_controller_api::permissions::Permissions;
use swarm_queue_client::agent_permissions;
use swarm_secret_client::observability::Grant;
use swarm_wire::agent_permissions::{
AgentPermissions, Import, ImportReply, hive_from_import_subject, import_subject_wildcard,
AgentPermissions, DEFAULT_MAX_REPOS, Import, ImportReply, MAX_REPOS, hive_from_import_subject,
import_subject_wildcard,
};
use utoipa::ToSchema;
@ -41,8 +42,8 @@ use crate::wanted::WantedWriter;
const MAX_ATTEMPTS: usize = 5;
/// The permissions to store for `req`, whose list is already checked. The list
/// replaces the stored one; `unrestricted_internet` left out keeps the one in
/// `current`. `internet_log` is stored only as `true`, and a request that sets
/// replaces the stored one; `unrestricted_internet` and `max_repos` left out
/// keep the ones in `current`. `internet_log` is stored only as `true`, and a request that sets
/// `unrestricted_internet` without it turns it off: an agent that moves to
/// unrestricted and back to restricted does not come back logging.
fn into_stored(req: Permissions, current: &AgentPermissions) -> AgentPermissions {
@ -55,6 +56,7 @@ fn into_stored(req: Permissions, current: &AgentPermissions) -> AgentPermissions
permissions: req.permissions,
unrestricted_internet: req.unrestricted_internet.or(current.unrestricted_internet),
internet_log,
max_repos: req.max_repos.or(current.max_repos),
}
}
@ -78,12 +80,49 @@ pub(crate) fn unique(perms: &[Permission]) -> Result<(), String> {
Ok(())
}
/// Woken when an agent's `max_repos` changes, so the forge pass applies it at
/// once instead of at its next tick. A wake with no pass waiting is kept for
/// the next one.
pub(crate) static MAX_REPOS_CHANGED: tokio::sync::Notify = tokio::sync::Notify::const_new();
/// The repository limit `record` sets: its `max_repos`, or
/// [`DEFAULT_MAX_REPOS`] when it has none. A value above [`MAX_REPOS`], as a
/// hand edit could leave, reads as the default with a warning: the stricter
/// reading.
fn max_repos_of(agent: &str, record: &AgentPermissions) -> u32 {
match record.max_repos {
None => DEFAULT_MAX_REPOS,
Some(n) if n <= MAX_REPOS => n,
Some(n) => {
tracing::warn!(
agent,
n,
"the stored repository limit is out of range; using the default"
);
DEFAULT_MAX_REPOS
}
}
}
/// `agent`'s repository limit: what its record sets, or [`DEFAULT_MAX_REPOS`]
/// when it has no record. A record that does not decode reads as none. Opens
/// the bucket, which the controller creates at start anyway.
///
/// # Errors
/// The queue could not be read.
pub(crate) async fn max_repos(client: &async_nats::Client, agent: &str) -> anyhow::Result<u32> {
let store = agent_permissions::open_or_create(client).await?;
let (record, _, _) = read_or_default::<AgentPermissions>(&store, agent).await?;
Ok(max_repos_of(agent, &record))
}
/// The stored permissions as the API shows them.
fn view(v: AgentPermissions) -> Permissions {
Permissions {
permissions: v.permissions,
unrestricted_internet: v.unrestricted_internet,
internet_log: v.internet_log,
max_repos: v.max_repos,
}
}
@ -249,6 +288,12 @@ async fn put_with(
if let Some(perms) = &req.permissions {
unique(perms).map_err(|why| refuse(error_problem(StatusCode::BAD_REQUEST, &why)))?;
}
if let Some(n) = req.max_repos.filter(|n| *n > MAX_REPOS) {
return Err(refuse(error_problem(
StatusCode::BAD_REQUEST,
&format!("max_repos {n} is more than the {MAX_REPOS} an agent may be given"),
)));
}
let client = queue(&state).map_err(|r| refuse(refused(r)))?;
if !live(&state).await.map_err(refuse)?.contains(&agent) {
return Err(refuse(error_problem(
@ -283,6 +328,9 @@ async fn put_with(
.map_err(|e| queue_problem(&e))?
{
tracing::info!(%agent, ?value, "set agent permissions");
if value.max_repos != current.max_repos {
MAX_REPOS_CHANGED.notify_one();
}
if !revoking {
mirror(leaves, &agent, &before, &after)
.await
@ -333,12 +381,15 @@ async fn delete_with(
let (current, revision, _) = read_or_default::<AgentPermissions>(&store, &agent)
.await
.map_err(|e| queue_problem(&e))?;
// Only the flags outlive a clear: dropping `unrestricted_internet`
// would turn an agent created with `false` on, and dropping
// `internet_log` would turn a logging agent into an enforced one.
// Only the flags and the repository limit outlive a clear: dropping
// `unrestricted_internet` would turn an agent created with `false` on,
// dropping `internet_log` would turn a logging agent into an enforced
// one, and a limit is an operator's decision about repositories, not
// part of the permission list.
let kept = AgentPermissions {
unrestricted_internet: current.unrestricted_internet,
internet_log: current.internet_log,
max_repos: current.max_repos,
..AgentPermissions::default()
};
mirror(leaves, &agent, &held(&current), &held(&kept))
@ -438,6 +489,7 @@ pub(crate) fn at_create(
permissions: perms,
unrestricted_internet,
internet_log: None,
max_repos: None,
})
}
@ -467,6 +519,7 @@ fn merged_at_create(
} else {
current.internet_log
},
max_repos: value.max_repos.or(current.max_repos),
}
}
@ -810,7 +863,7 @@ pub(crate) mod tests {
use axum::http::StatusCode;
use hive_types::Permission;
use swarm_secret_client::observability::Grant;
use swarm_wire::agent_permissions::{Import, ImportReply};
use swarm_wire::agent_permissions::{Import, ImportReply, MAX_REPOS};
fn stored(json: &str) -> swarm_wire::agent_permissions::AgentPermissions {
serde_json::from_str(json).expect("decodes")
@ -821,6 +874,7 @@ pub(crate) mod tests {
permissions: None,
unrestricted_internet,
internet_log,
max_repos: None,
}
}
@ -886,6 +940,99 @@ pub(crate) mod tests {
);
}
/// An agent's repository limit is its own: a change to one agent's leaves
/// every other agent at its limit, a `PUT` that leaves the field out and a
/// `DELETE` keep it, and a limit above the range is refused.
#[tokio::test]
async fn an_agents_repo_limit_is_its_own_and_survives_other_changes() {
use crate::wanted::WantedWriter;
use swarm_wire::wanted::AgentState;
let nats = TestNats::start();
let client = nats.client().await;
let state = crate::agent_limits::tests::declared(&client).await;
WantedWriter::new(client.clone())
.set("pr1ma", "iris", AgentState::Up)
.await
.expect("declare iris");
let limit = |n| Permissions {
max_repos: Some(n),
..Permissions::default()
};
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), 0);
// A change wakes the forge pass; drain stale wakes first.
while tokio::time::timeout(
std::time::Duration::from_millis(1),
super::MAX_REPOS_CHANGED.notified(),
)
.await
.is_ok()
{}
assert_eq!(
put(&state, "atlas", limit(3))
.await
.expect("set")
.0
.max_repos,
Some(3)
);
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), 3);
tokio::time::timeout(
std::time::Duration::from_secs(5),
super::MAX_REPOS_CHANGED.notified(),
)
.await
.expect("the change wakes the forge pass");
assert_eq!(
super::max_repos(&client, "iris").await.unwrap(),
0,
"another agent's limit is not changed"
);
let _ = put(&state, "iris", limit(7)).await.expect("set");
assert_eq!(super::max_repos(&client, "iris").await.unwrap(), 7);
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), 3);
// A PUT without the field, and a DELETE, keep the limit.
let _ = put(&state, "atlas", granted()).await.expect("set");
assert_eq!(get(&state, "atlas").await.max_repos, Some(3));
delete_agent_permissions(State(state.clone()), Path("atlas".to_owned()))
.await
.expect("cleared");
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), 3);
let problem = put(&state, "atlas", limit(MAX_REPOS + 1))
.await
.expect_err("over the range");
assert_eq!(problem.status, Some(StatusCode::BAD_REQUEST), "{problem:?}");
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), 3);
let _ = put(&state, "atlas", limit(MAX_REPOS))
.await
.expect("control: the top of the range is stored");
assert_eq!(super::max_repos(&client, "atlas").await.unwrap(), MAX_REPOS);
}
/// A stored limit above the range, as a hand edit could leave it, reads as
/// none rather than as that many repositories.
#[test]
fn a_stored_limit_above_the_range_reads_as_none() {
let read = |json: &str| super::max_repos_of("atlas", &stored(json));
assert_eq!(read(r#"{"max_repos":5000}"#), 0);
assert_eq!(read("{}"), 0);
assert_eq!(read(r#"{"max_repos":7}"#), 7, "control: in range");
}
/// A create keeps the stored limit and does not set one.
#[test]
fn a_create_keeps_the_repo_limit() {
let current = stored(r#"{"max_repos":4}"#);
assert_eq!(
super::merged_at_create(&stored("{}"), &current, true).max_repos,
Some(4)
);
assert_eq!(super::at_create(None, None).expect("valid").max_repos, None);
}
pub(crate) async fn published(client: &async_nats::Client, agent: &str) -> Option<String> {
let store = swarm_queue_client::agent_permissions::open_or_create(client)
.await
@ -902,6 +1049,7 @@ pub(crate) mod tests {
permissions: Some(vec![Permission::ManageRootAgent]),
unrestricted_internet: None,
internet_log: None,
max_repos: None,
}
}
@ -947,6 +1095,7 @@ pub(crate) mod tests {
permissions: Some(Vec::new()),
unrestricted_internet: None,
internet_log: None,
max_repos: None,
};
let _ = put(&state, "atlas", none.clone()).await.expect("set");
assert_eq!(
@ -975,6 +1124,7 @@ pub(crate) mod tests {
permissions: None,
unrestricted_internet: Some(false),
internet_log: Some(true),
max_repos: None,
};
let stored = put(&state, "atlas", log.clone()).await.expect("set").0;
assert_eq!(stored, log);
@ -997,6 +1147,7 @@ pub(crate) mod tests {
permissions: None,
unrestricted_internet: Some(true),
internet_log: None,
max_repos: None,
};
let _ = put(&state, "atlas", on.clone()).await.expect("set");
let off = Permissions {
@ -1144,6 +1295,7 @@ pub(crate) mod tests {
permissions: Some(perms.to_vec()),
unrestricted_internet: None,
internet_log: None,
max_repos: None,
}
}

View file

@ -44,6 +44,7 @@ use crate::webhook::DeliveryKind;
use swarm_secret_client::stored_secret::StoredSecret;
use swarm_wire::review_classifier::Commit;
pub mod agent_max_repos;
pub mod agent_token;
mod api;
mod base_pulls;
@ -749,27 +750,29 @@ impl Client {
/// unconditionally, 422 only after a follow-up `user_get` confirms the
/// user (see [`folds_into_success`] — not every 422 means it exists).
///
/// Then locks the account out of creating repos
/// ([`Self::disable_repo_creation`]), whether it was just created or
/// Then limits the account ([`Self::limit_agent_account`]) to `max_repos`
/// repositories and no organizations, whether it was just created or
/// already existed. Nothing else does that for a swarm-created agent, so
/// a failure fails the node. Leaves an existing account's email as it
/// finds it.
pub async fn ensure_agent_user(&self, agent: &str) -> Result<()> {
pub async fn ensure_agent_user(&self, agent: &str, max_repos: u32) -> Result<()> {
self.create_agent_user(agent).await?;
self.disable_repo_creation(agent).await
self.limit_agent_account(agent, max_repos).await
}
/// Set `max_repo_creation = 0` on `agent`'s account. Agents create repos
/// through the hive, never with their own token — a write-scoped token
/// can otherwise create and own a repo and self-merge in it, bypassing
/// the operator-only-merge policy. Existing repos are untouched; push,
/// PRs and clone keep working.
/// Set `max_repo_creation = max_repos` and `allow_create_organization =
/// false` on `agent`'s account. At 0 an agent creates repos only through
/// the hive, never with its own token; above 0 a write-scoped token can
/// create and own that many repos and self-merge in them, bypassing the
/// operator-only-merge policy. Existing repos are untouched; push, PRs and
/// clone keep working. An account that may not create organizations cannot
/// get around the limit by making an organization to own repos in.
///
/// The PATCH itself is unconditional rather than verify-then-patch:
/// Forgejo's API never returns `max_repo_creation` (it is a field of
/// `EditUserOption` only, not of `User`), so there is no current value
/// to compare against, and setting 0 on an account already at 0 is a
/// no-op. But `create_agent_user`'s idempotent fold only proves *some*
/// to compare against, and setting the same value again is a no-op. But
/// `create_agent_user`'s idempotent fold only proves *some*
/// account named `agent` exists — a 409 folds with no read at all, and
/// a 422's follow-up `user_get` only confirms the username, not that
/// swarm-controller created it — so before that PATCH this reads the
@ -781,31 +784,35 @@ impl Client {
/// name collided with an account this node didn't create, so it fails
/// the node rather than sending the PATCH (or silently skipping) to an
/// account nobody verified is agent-controlled.
async fn disable_repo_creation(&self, agent: &str) -> Result<()> {
pub(crate) async fn limit_agent_account(&self, agent: &str, max_repos: u32) -> Result<()> {
let user = self
.api
.call(|api| api.user_get(agent).into_future())
.await
.with_context(|| format!("read forge user {agent} before locking it down"))?;
.with_context(|| format!("read forge user {agent} before limiting it"))?;
let expected = agent_email(agent);
let legacy = legacy_agent_email(agent);
if !matches!(user.email.as_deref(), Some(e) if e == expected || e == legacy) {
anyhow::bail!(
"forge account `{agent}` exists but is not this agent's (email {:?}, expected \
`{expected}` or the legacy `{legacy}`); refusing to lock it down",
`{expected}` or the legacy `{legacy}`); refusing to limit it",
user.email
);
}
self.api
.call(|api| {
api.admin_edit_user(agent, repo_creation_lockdown(agent))
api.admin_edit_user(agent, agent_account_lockdown(agent, max_repos))
.into_future()
})
.await
.with_context(|| {
format!("disable repo creation for forge user {agent} (max_repo_creation = 0)")
format!("limit forge user {agent} (max_repo_creation = {max_repos})")
})?;
tracing::info!(%agent, "swarm forge: disabled repo creation (max_repo_creation = 0)");
tracing::info!(
%agent,
max_repos,
"swarm forge: limited the account (max_repo_creation, no organizations)"
);
Ok(())
}
@ -2167,7 +2174,7 @@ fn initial_flake_nix() -> &'static str {
/// Canonical email for an agent's Forgejo account, set by
/// [`Client::create_agent_user`] at creation and checked by
/// [`Client::disable_repo_creation`] before it sends the account's PATCH. Same
/// [`Client::limit_agent_account`] before it sends the account's PATCH. Same
/// format string as `hive-c0re::forge::users::agent_email` — not called
/// across the crate boundary (see this module's doc comment), but both
/// creators use this exact value, so it doubles as the
@ -2179,7 +2186,7 @@ fn agent_email(agent: &str) -> String {
/// The email older hive-created agent accounts carry, and keep: nothing
/// rewrites it to [`agent_email`]. Accepted by
/// [`Client::disable_repo_creation`]'s guard alongside [`agent_email`] so
/// [`Client::limit_agent_account`]'s guard alongside [`agent_email`] so
/// such an account is still recognised as the agent's own.
fn legacy_agent_email(agent: &str) -> String {
format!("{agent}@hive.local")
@ -2215,6 +2222,17 @@ fn repo_creation_lockdown(agent: &str) -> EditUserOption {
}
}
/// [`repo_creation_lockdown`] for an agent's account: `max_repos` repositories
/// instead of none, and no organizations. The service accounts that share the
/// lockdown stay at none and are not touched on organizations.
fn agent_account_lockdown(agent: &str, max_repos: u32) -> EditUserOption {
EditUserOption {
allow_create_organization: Some(false),
max_repo_creation: Some(i64::from(max_repos)),
..repo_creation_lockdown(agent)
}
}
/// Whether a create-style call failed because the object already
/// exists. Forgejo signals this as HTTP 409 (conflict) or 422
/// (validation) — match both defensively, same as
@ -2842,10 +2860,11 @@ mod tests {
async fn a_created_agent_user_gets_repo_creation_disabled() {
let (client, seen) = stub_forge((201, "{}"), own_account_get("alice"), (200, "{}")).await;
client.ensure_agent_user("alice").await.unwrap();
client.ensure_agent_user("alice", 0).await.unwrap();
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
assert_eq!(body["max_repo_creation"], 0);
assert_eq!(body["allow_create_organization"], false);
let set: Vec<&str> = body
.as_object()
.unwrap()
@ -2853,11 +2872,35 @@ mod tests {
.filter(|(_, v)| !v.is_null())
.map(|(k, _)| k.as_str())
.collect();
assert_eq!(set, ["login_name", "max_repo_creation", "source_id"]);
assert_eq!(
set,
[
"allow_create_organization",
"login_name",
"max_repo_creation",
"source_id"
]
);
assert_eq!(body["login_name"], "alice");
assert_eq!(body["source_id"], 0);
}
/// The agent's limit is what goes out as `max_repo_creation`, on a created
/// account and an existing one alike, and organizations stay refused at any
/// limit.
#[tokio::test]
async fn the_agents_limit_is_what_the_account_is_set_to() {
for create in [(201, "{}"), (409, r#"{"message":"user already exists"}"#)] {
let (client, seen) = stub_forge(create, own_account_get("alice"), (200, "{}")).await;
client.ensure_agent_user("alice", 3).await.unwrap();
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
assert_eq!(body["max_repo_creation"], 3, "{create:?}");
assert_eq!(body["allow_create_organization"], false, "{create:?}");
}
}
/// An account that already existed is locked down too: the node is
/// re-run, and an account created before this lockdown existed must
/// not keep repo creation just because the create step folded to
@ -2872,7 +2915,7 @@ mod tests {
)
.await;
client.ensure_agent_user("alice").await.unwrap();
client.ensure_agent_user("alice", 0).await.unwrap();
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
assert_eq!(body["max_repo_creation"], 0);
@ -2891,7 +2934,7 @@ mod tests {
)
.await;
let err = client.ensure_agent_user("alice").await.unwrap_err();
let err = client.ensure_agent_user("alice", 0).await.unwrap_err();
let msg = format!("{err:#}");
assert!(msg.contains("max_repo_creation"), "{msg}");
@ -2915,7 +2958,7 @@ mod tests {
)
.await;
let err = client.ensure_agent_user("alice").await.unwrap_err();
let err = client.ensure_agent_user("alice", 0).await.unwrap_err();
let msg = format!("{err:#}");
assert!(msg.contains("alice"), "{msg}");
@ -2945,7 +2988,7 @@ mod tests {
)
.await;
client.ensure_agent_user("alice").await.unwrap();
client.ensure_agent_user("alice", 0).await.unwrap();
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
assert_eq!(body["max_repo_creation"], 0);
@ -2963,7 +3006,7 @@ mod tests {
)
.await;
client.ensure_agent_user("alice").await.unwrap();
client.ensure_agent_user("alice", 0).await.unwrap();
let body = lockdown_patch(&seen, "alice").expect("no lockdown PATCH sent");
assert_eq!(body["max_repo_creation"], 0);

View file

@ -0,0 +1,223 @@
//! Each existing agent's forge account at its own repository limit.
//!
//! The limit is the `max_repos` of the agent's record in `agent-permissions`
//! (`swarm_wire::agent_permissions`). A new account gets it from its
//! `CreateForgeUser` node. This pass reaches the others: at start, when a
//! change of an agent's `max_repos` wakes it
//! ([`crate::agent_permissions::MAX_REPOS_CHANGED`]), and every
//! [`RECONCILE_INTERVAL`] over every agent that holds a store identity.
//!
//! Forgejo never returns an account's `max_repo_creation`, so nothing can be
//! compared against the forge. The pass remembers what it applied to each agent
//! in this process and applies again only what differs, so a restart applies
//! every agent's limit once, and an agent whose edit failed is retried at the
//! next tick. The decision is pure ([`plan`]), so the tests pin it.
use std::collections::BTreeMap;
use std::sync::Arc;
use super::Client;
/// How often [`spawn`] re-checks every agent.
const RECONCILE_INTERVAL: std::time::Duration = std::time::Duration::from_mins(5);
/// The agents of `desired` whose account is not known to be at their limit,
/// with that limit.
pub fn plan(
desired: &BTreeMap<String, u32>,
applied: &BTreeMap<String, u32>,
) -> Vec<(String, u32)> {
desired
.iter()
.filter(|(agent, limit)| applied.get(*agent) != Some(limit))
.map(|(agent, limit)| (agent.clone(), *limit))
.collect()
}
/// One pass: set each limit of `todo` on its agent's account, recording the
/// ones the forge took in `applied`. An agent the forge refuses is logged and
/// left out, so the next pass plans it again.
async fn apply(client: &Client, todo: &[(String, u32)], applied: &mut BTreeMap<String, u32>) {
for (agent, limit) in todo {
match client.limit_agent_account(agent, *limit).await {
Ok(()) => {
applied.insert(agent.clone(), *limit);
}
Err(e) => tracing::warn!(
%agent,
limit,
error = %format!("{e:#}"),
"agent max repos: the forge account was not updated; retrying next pass"
),
}
}
}
/// Every roster agent's limit. An agent whose record can't be read is left
/// out, so nothing is applied for it this pass.
async fn desired(queue: &async_nats::Client, roster: &[String]) -> BTreeMap<String, u32> {
let mut limits = BTreeMap::new();
for agent in roster {
match crate::agent_permissions::max_repos(queue, agent).await {
Ok(limit) => {
limits.insert(agent.clone(), limit);
}
Err(e) => tracing::warn!(
%agent,
error = %format!("{e:#}"),
"agent max repos: the agent's limit was not read; skipping it this pass"
),
}
}
limits
}
/// Apply every agent's limit now and again on each wake and tick. A pass that
/// fails is logged and retried on the next tick; it never stops the daemon.
pub fn spawn(client: Arc<Client>, queue: async_nats::Client) {
tokio::spawn(async move {
let mut ticker = tokio::time::interval(RECONCILE_INTERVAL);
let mut applied = BTreeMap::new();
loop {
tokio::select! {
_ = ticker.tick() => {}
() = crate::agent_permissions::MAX_REPOS_CHANGED.notified() => {}
}
let roster = match super::docs_grant::read_roster().await {
Ok(roster) => roster,
Err(e) => {
tracing::warn!(error = %format!("{e:#}"), "agent max repos: no roster; retrying next tick");
continue;
}
};
applied.retain(|agent, _| roster.contains(agent));
let todo = plan(&desired(&queue, &roster).await, &applied);
if todo.is_empty() {
continue;
}
tracing::info!(agents = todo.len(), "agent max repos: applying the limits");
super::within_forge_deadline("agent max repos", apply(&client, &todo, &mut applied))
.await;
}
});
}
#[cfg(test)]
mod tests {
use super::*;
fn limits(of: &[(&str, u32)]) -> BTreeMap<String, u32> {
of.iter().map(|(a, n)| ((*a).to_owned(), *n)).collect()
}
/// An agent at another value, or not yet applied, is planned with its own
/// limit; one already at its limit is not.
#[test]
fn plan_names_the_agents_not_at_their_limit() {
let desired = limits(&[("atlas", 2), ("iris", 0), ("sock", 5)]);
let applied = limits(&[("atlas", 2), ("iris", 1)]);
assert_eq!(
plan(&desired, &applied),
[("iris".to_owned(), 0), ("sock".to_owned(), 5)]
);
// Control: everything applied at its limit leaves nothing to do.
assert!(plan(&desired, &desired).is_empty());
}
/// A stand-in forge on a loopback port that answers each agent's own
/// email and records every admin edit as (login, body).
async fn stub_agents() -> (
Client,
Arc<std::sync::Mutex<Vec<(String, serde_json::Value)>>>,
) {
use axum::http::{Method, StatusCode, Uri, header::CONTENT_TYPE};
let edits: Arc<std::sync::Mutex<Vec<(String, serde_json::Value)>>> = Arc::default();
let recorded = Arc::clone(&edits);
let app = axum::Router::new().fallback(
move |method: Method, uri: Uri, body: axum::body::Bytes| {
let recorded = Arc::clone(&recorded);
async move {
let login = uri.path().rsplit('/').next().unwrap_or_default().to_owned();
let body = match method {
Method::GET => format!(r#"{{"email":"{login}@hyperhive.local"}}"#),
Method::PATCH => {
let json = serde_json::from_slice(&body).unwrap_or_default();
recorded.lock().unwrap().push((login, json));
"{}".to_owned()
}
_ => "{}".to_owned(),
};
(StatusCode::OK, [(CONTENT_TYPE, "application/json")], body)
}
},
);
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 });
let api = crate::forge::api::tests::fixed(url.clone(), "stub-token");
(Client { api, url }, edits)
}
/// Each agent gets its own limit, none is skipped and none takes another's;
/// each is recorded as applied, and a second plan has nothing left.
#[tokio::test]
async fn the_pass_sets_each_agent_its_own_limit() {
let (client, edits) = stub_agents().await;
let desired = limits(&[("atlas", 4), ("iris", 0), ("sock", 9)]);
let mut applied = BTreeMap::new();
apply(&client, &plan(&desired, &applied), &mut applied).await;
let mut seen: Vec<(String, i64, bool)> = edits
.lock()
.unwrap()
.iter()
.map(|(login, body)| {
(
login.clone(),
body["max_repo_creation"].as_i64().unwrap(),
body["allow_create_organization"] == false,
)
})
.collect();
seen.sort();
assert_eq!(
seen,
[
("atlas".to_owned(), 4, true),
("iris".to_owned(), 0, true),
("sock".to_owned(), 9, true)
]
);
assert_eq!(applied, desired);
assert!(plan(&desired, &applied).is_empty());
// One agent's changed limit plans that agent alone.
let changed = limits(&[("atlas", 4), ("iris", 1), ("sock", 9)]);
assert_eq!(plan(&changed, &applied), [("iris".to_owned(), 1)]);
}
/// An agent whose edit the forge refuses is not recorded, so the next pass
/// plans it again.
#[tokio::test]
async fn a_refused_agent_is_planned_again() {
let (client, edits) = stub_agents().await;
let mut applied = BTreeMap::new();
apply(&client, &[("atlas".to_owned(), 2)], &mut applied).await;
// Control: an account that is the agent's own is recorded.
assert_eq!(applied.get("atlas"), Some(&2));
assert_eq!(edits.lock().unwrap().len(), 1);
let (broken, _seen) = super::super::tests::stub_forge(
(200, "{}"),
(200, r#"{"email":"someone@example.com"}"#),
(200, "{}"),
)
.await;
let mut applied = BTreeMap::new();
apply(&broken, &[("atlas".to_owned(), 2)], &mut applied).await;
assert!(applied.is_empty(), "a refused agent must not be recorded");
let desired = limits(&[("atlas", 2)]);
assert_eq!(plan(&desired, &applied), [("atlas".to_owned(), 2)]);
}
}

View file

@ -134,7 +134,7 @@ impl Client {
/// The store's `hive-agent-*` cert-auth roles: exactly the agents that hold a
/// store identity.
async fn read_roster() -> Result<Vec<String>> {
pub(super) async fn read_roster() -> Result<Vec<String>> {
let store = crate::store::connect()
.await
.context("logging in to the swarm secret store")?;

View file

@ -591,7 +591,7 @@ mod tests {
#[test]
fn an_agent_called_hive_x_is_not_hive_xs_account() {
// Both ways round: the hive job refuses the agent's account, and the
// agent's own guard (`disable_repo_creation`) refuses the hive's,
// agent's own guard (`limit_agent_account`) refuses the hive's,
// because neither accepts the other's email.
let login = forge::hive_login("alpha");
for agents in [
@ -638,7 +638,7 @@ mod tests {
)
.await;
client
.ensure_agent_user("hive-alpha")
.ensure_agent_user("hive-alpha", 0)
.await
.expect_err("the hive's account is not the agent's");
assert!(lockdown_patch(&seen, "hive-alpha").is_none());

View file

@ -38,9 +38,10 @@ use super::{
folds_into_success, is_ambiguous_validation_failure, is_confirmed_conflict,
};
/// Forgejo org that owns agent-created repos. Agents can't create repos with
/// their own token (`max_repo_creation = 0`); a repo an agent asks for is
/// created here and the agent added as a **write** member (not owner/admin).
/// Forgejo org that owns agent-created repos. An agent can create repos with
/// its own token only up to its own `max_repos` (default 0, so none); a repo
/// an agent asks for is created here and the agent added as a **write**
/// member (not owner/admin).
/// Because the org, not the agent, owns the repo, perms stay centrally
/// managed and branch protection (referencing [`OPERATORS_TEAM`]) can block
/// the author from merging their own PR.

View file

@ -393,7 +393,13 @@ async fn run_node_work(
.to_owned(),
),
Some(client) => {
match forge_node("create_forge_user", client.ensure_agent_user(&agent)).await {
let max_repos = agent_max_repos_limit(deps.queue.as_ref(), &agent).await;
match forge_node(
"create_forge_user",
client.ensure_agent_user(&agent, max_repos),
)
.await
{
Ok(()) => Outcome::Done,
Err(e) => Outcome::Failed(format!("{e:#}")),
}
@ -554,6 +560,26 @@ async fn mint_forge_token(
}
}
/// The repository limit a new agent account is created at: the `max_repos` of
/// the agent's permissions record, or none (0) when there is no queue or it
/// cannot be read. The forge pass in `forge::agent_max_repos` raises the
/// account to its limit once the queue answers.
async fn agent_max_repos_limit(queue: Option<&async_nats::Client>, agent: &str) -> u32 {
let Some(queue) = queue else {
return swarm_wire::agent_permissions::DEFAULT_MAX_REPOS;
};
crate::agent_permissions::max_repos(queue, agent)
.await
.unwrap_or_else(|e| {
tracing::warn!(
agent,
error = %format!("{e:#}"),
"swarm jobq: the agent's repository limit is unreadable; creating the account at none"
);
swarm_wire::agent_permissions::DEFAULT_MAX_REPOS
})
}
/// The `GrantDocsRead` arm, lifted out for the same reason.
pub(crate) async fn grant_docs_read(
forge: Option<Arc<forge::Client>>,
@ -2031,6 +2057,29 @@ mod tests {
}
}
/// A new agent's account is created at the limit of its own record, not
/// another agent's, and at none when it has no record or there is no queue.
#[tokio::test]
async fn a_new_account_is_created_at_its_agents_limit() {
let nats = crate::test_nats::TestNats::start();
let client = nats.client().await;
assert_eq!(agent_max_repos_limit(Some(&client), "iris").await, 0);
let store = swarm_queue_client::agent_permissions::open_or_create(&client)
.await
.expect("bucket");
store
.put("iris", br#"{"max_repos":4}"#.to_vec().into())
.await
.expect("written");
store
.put("atlas", br#"{"max_repos":9}"#.to_vec().into())
.await
.expect("written");
assert_eq!(agent_max_repos_limit(Some(&client), "iris").await, 4);
assert_eq!(agent_max_repos_limit(Some(&client), "sock").await, 0);
assert_eq!(agent_max_repos_limit(None, "iris").await, 0);
}
fn build_state(
sched: &hive_jobq::scheduler::Scheduler<SwarmNodeKind, super::SwarmResourceKind>,
rev: &str,

View file

@ -277,6 +277,7 @@ fn spawn_forge_workers(
}
forge::runner_token::spawn(Arc::clone(&client));
if let Some(queue) = queue {
forge::agent_max_repos::spawn(Arc::clone(&client), queue.clone());
crate::cache_miss::spawn(
queue.clone(),
Arc::new(wanted::WantedWriter::new(queue)),