agent permissions: one set, tool groups included, set at agent creation
mara on #5170: "dont split caps and perms. its all perms. / dont include the now empty tool groups / nothing set -> default / also add perms to agent creation screen". - hive-sh4re: one `Permission` enum (messaging, meta, inbox, execution, web_tools, manage_root_agent) replaces `ToolGroup` and `Capability`; `lifecycle`/`forge` are gone. `Permission::default_for` is the default for an agent the swarm sets nothing for. - The record is `{ "permissions": [...] }`. Missing key, unset field or no successful read yet = default; `[]` = none; a failed read after a good one keeps the last value. - hive-agent reads its own key every 10s and builds each turn's --tools, --allowedTools and MCP config (rewritten before every turn) from it; the subagent daemon reads it on each start/continue. Agents get a read-only grant on their own key. - hive-c0re restarts an agent only when its manage_root_agent changes. The import now merges meta/capabilities.json and meta/tool-groups.json into one list per agent and deletes both; the "import pending, don't read" decision is `ImportOutcome::read_allowed`, table-tested. - swarm-controller: `GET /api/permissions` (names + default) and an optional `permissions` on `POST /api/agents`, stored before the job is queued. swarm-ui's create-agent form gets a permissions field. - Deleted: HIVE_TOOL_GROUPS and its forward to the subagent daemon, tool_groups.rs, ruth's tool-group seed, the permissions rebuild job (PermPayload/WritePermFile/perm_change), the dashboard permissions tab and its stale-entries panel. Part of #5170.
This commit is contained in:
parent
f862354e43
commit
f0844f6da3
73 changed files with 1578 additions and 2742 deletions
|
|
@ -1742,6 +1742,11 @@ struct CreateAgentRequest {
|
|||
/// /api/agents/{name}/inference` takes them. Absent as `limits`.
|
||||
#[serde(default)]
|
||||
inference: Option<inference::InferenceSettings>,
|
||||
/// The agent's permissions, stored before the job is queued. Left out, the
|
||||
/// agent holds the default set.
|
||||
#[serde(default)]
|
||||
#[schema(value_type = Option<Vec<String>>)]
|
||||
permissions: Option<Vec<hive_sh4re::permissions::Permission>>,
|
||||
}
|
||||
|
||||
/// Where the queued job landed — a caller polls `/api/jobq/graph` (or
|
||||
|
|
@ -1795,10 +1800,10 @@ struct CreateAgentResponse {
|
|||
request_body = CreateAgentRequest,
|
||||
responses(
|
||||
(status = 200, description = "job chain queued", body = CreateAgentResponse),
|
||||
(status = 400, description = "`name` or `hive` is not a valid identifier, `name` is new and reserved or refused by the forge, `hive` is not in this swarm, or `limits` or `inference` holds a value the matching `PUT` refuses (problem+json)", body = String),
|
||||
(status = 400, description = "`name` or `hive` is not a valid identifier, `name` is new and reserved or refused by the forge, `hive` is not in this swarm, `limits` or `inference` holds a value the matching `PUT` refuses, or a permission is listed twice (problem+json)", body = String),
|
||||
(status = 409, description = "the swarm has already placed `name` on a different hive (problem+json)", body = String),
|
||||
(status = 422, description = "an unknown field in the body, in `limits`, or in `inference`, or an `effort` that is not one of the five levels"),
|
||||
(status = 503, description = "the swarm queue is not connected, so whether `name` is placed on another hive is unknown, or `limits` or `inference` is set and cannot be stored (problem+json)", body = String),
|
||||
(status = 422, description = "an unknown field in the body, in `limits`, or in `inference`, an `effort` that is not one of the five levels, or a `permissions` entry that is not a permission name"),
|
||||
(status = 503, description = "the swarm queue is not connected, so whether `name` is placed on another hive is unknown, or `limits`, `inference` or `permissions` is set and cannot be stored (problem+json)", body = String),
|
||||
(status = 500, description = "the job chain could not be queued, or another hive's wanted state could not be read (problem+json)", body = String),
|
||||
),
|
||||
tag = "agents"
|
||||
|
|
@ -1813,15 +1818,7 @@ async fn create_agent(
|
|||
|
||||
// Settings are checked before anything is created, so a bad value stops
|
||||
// the request rather than leaving an agent without them.
|
||||
let limits = req
|
||||
.limits
|
||||
.map(agent_limits::Caps::into_stored)
|
||||
.transpose()
|
||||
.map_err(|why| error_problem(axum::http::StatusCode::BAD_REQUEST, &why))?;
|
||||
let inference = req
|
||||
.inference
|
||||
.map(inference::InferenceSettings::into_stored)
|
||||
.transpose()
|
||||
let settings = CreateSettings::parse(req.limits, req.inference, req.permissions)
|
||||
.map_err(|why| error_problem(axum::http::StatusCode::BAD_REQUEST, &why))?;
|
||||
|
||||
// Two checks, and the second is the one that makes the field worth
|
||||
|
|
@ -1885,7 +1882,7 @@ async fn create_agent(
|
|||
}
|
||||
// Checked after the roster, so a mistyped hive stays a 400 while the queue
|
||||
// is down.
|
||||
let settings_queue = if limits.is_some() || inference.is_some() {
|
||||
let settings_queue = if settings.any() {
|
||||
Some(schedules::queue(&state).map_err(schedules::refused)?)
|
||||
} else {
|
||||
None
|
||||
|
|
@ -1916,7 +1913,14 @@ async fn create_agent(
|
|||
// settings behind for the name.
|
||||
let stored = match &settings_queue {
|
||||
Some(client) => {
|
||||
StoredSettings::store(client, &agent, limits.as_ref(), inference.as_ref()).await?
|
||||
StoredSettings::store(
|
||||
client,
|
||||
&agent,
|
||||
settings.limits.as_ref(),
|
||||
settings.inference.as_ref(),
|
||||
settings.permissions.as_ref(),
|
||||
)
|
||||
.await?
|
||||
}
|
||||
None => StoredSettings::default(),
|
||||
};
|
||||
|
|
@ -1948,27 +1952,67 @@ async fn create_agent(
|
|||
}))
|
||||
}
|
||||
|
||||
/// The settings a `POST /api/agents` body gives, as they are stored.
|
||||
struct CreateSettings {
|
||||
limits: Option<swarm_queue_client::agent_limits::AgentCaps>,
|
||||
inference: Option<swarm_queue_client::agent_inference::AgentInference>,
|
||||
permissions: Option<swarm_queue_client::agent_permissions::AgentPermissions>,
|
||||
}
|
||||
|
||||
impl CreateSettings {
|
||||
/// Each setting given, or why its `PUT` would refuse the first bad one.
|
||||
fn parse(
|
||||
limits: Option<agent_limits::Caps>,
|
||||
inference: Option<inference::InferenceSettings>,
|
||||
permissions: Option<Vec<hive_sh4re::permissions::Permission>>,
|
||||
) -> Result<Self, String> {
|
||||
Ok(Self {
|
||||
limits: limits.map(agent_limits::Caps::into_stored).transpose()?,
|
||||
inference: inference
|
||||
.map(inference::InferenceSettings::into_stored)
|
||||
.transpose()?,
|
||||
permissions: permissions.map(agent_permissions::at_create).transpose()?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether any setting is given.
|
||||
fn any(&self) -> bool {
|
||||
self.limits.is_some() || self.inference.is_some() || self.permissions.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
/// What `create_agent` replaced in the settings stores, to put back if the
|
||||
/// creation is refused after the write.
|
||||
#[derive(Default)]
|
||||
struct StoredSettings {
|
||||
limits: agent_limits::Replaced,
|
||||
inference: inference::Replaced,
|
||||
permissions: agent_permissions::Replaced,
|
||||
}
|
||||
|
||||
impl StoredSettings {
|
||||
/// Store whichever of `limits` and `inference` are given. A failure on
|
||||
/// the second puts back the first.
|
||||
/// Store whichever of `limits`, `permissions` and `inference` are given, in
|
||||
/// that order. A failure on one puts back the ones stored before it.
|
||||
async fn store(
|
||||
client: &async_nats::Client,
|
||||
agent: &str,
|
||||
limits: Option<&swarm_queue_client::agent_limits::AgentCaps>,
|
||||
inference: Option<&swarm_queue_client::agent_inference::AgentInference>,
|
||||
permissions: Option<&swarm_queue_client::agent_permissions::AgentPermissions>,
|
||||
) -> Result<Self, problem_details::ProblemDetails> {
|
||||
let mut stored = Self::default();
|
||||
if let Some(limits) = limits {
|
||||
stored.limits = agent_limits::store(client, agent, limits).await?;
|
||||
}
|
||||
if let Some(permissions) = permissions {
|
||||
match agent_permissions::store(client, agent, permissions).await {
|
||||
Ok(replaced) => stored.permissions = replaced,
|
||||
Err(e) => {
|
||||
stored.roll_back(client, agent).await;
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(inference) = inference {
|
||||
match inference::store(client, agent, inference).await {
|
||||
Ok(replaced) => stored.inference = replaced,
|
||||
|
|
@ -1984,6 +2028,7 @@ impl StoredSettings {
|
|||
async fn roll_back(self, client: &async_nats::Client, agent: &str) {
|
||||
agent_limits::restore(client, agent, self.limits).await;
|
||||
inference::restore(client, agent, self.inference).await;
|
||||
agent_permissions::restore(client, agent, self.permissions).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -3510,6 +3555,7 @@ fn build_app(state: AppState) -> axum::Router {
|
|||
agent_permissions::put_agent_permissions,
|
||||
agent_permissions::delete_agent_permissions
|
||||
))
|
||||
.routes(routes!(agent_permissions::get_permission_catalogue))
|
||||
.routes(routes!(get_config_prs))
|
||||
.routes(routes!(create_agent))
|
||||
.routes(routes!(mint_agent_forge_token))
|
||||
|
|
@ -3771,6 +3817,7 @@ mod tests {
|
|||
hive: "pr1maa".to_owned(),
|
||||
limits: None,
|
||||
inference: None,
|
||||
permissions: None,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
|
|
@ -3811,6 +3858,7 @@ mod tests {
|
|||
hive: "pr1ma".to_owned(),
|
||||
limits: None,
|
||||
inference: None,
|
||||
permissions: None,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
|
|
@ -3973,6 +4021,7 @@ mod tests {
|
|||
agent,
|
||||
Some(&caps(r#"{"cpu_quota":"400%"}"#)),
|
||||
Some(&inference),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
assert!(stored.is_err(), "{agent}: the inference write must fail");
|
||||
|
|
@ -3991,6 +4040,60 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
/// Through the real handler: when the inference store fails after the
|
||||
/// permissions are stored, the creation is refused and the permissions go
|
||||
/// back to what they were, the earlier value or no key. The inference
|
||||
/// bucket refuses any value, as above.
|
||||
#[tokio::test]
|
||||
async fn a_failed_creation_leaves_no_permissions_behind() {
|
||||
let nats = crate::test_nats::TestNats::start();
|
||||
let client = nats.client().await;
|
||||
async_nats::jetstream::new(client.clone())
|
||||
.create_key_value(async_nats::jetstream::kv::Config {
|
||||
bucket: swarm_queue_client::agent_inference::BUCKET.to_owned(),
|
||||
history: 1,
|
||||
max_value_size: 1,
|
||||
..Default::default()
|
||||
})
|
||||
.await
|
||||
.expect("a bucket that refuses any value");
|
||||
let state = crate::agent_limits::tests::state(&client);
|
||||
let earlier = crate::agent_permissions::at_create(vec![
|
||||
hive_sh4re::permissions::Permission::Messaging,
|
||||
])
|
||||
.expect("valid");
|
||||
crate::agent_permissions::store(&client, "atlas", &earlier)
|
||||
.await
|
||||
.expect("an earlier grant");
|
||||
|
||||
for agent in ["atlas", "iris"] {
|
||||
let req: super::CreateAgentRequest = serde_json::from_str(&format!(
|
||||
r#"{{"name":"{agent}","hive":"pr1ma","permissions":["web_tools"],
|
||||
"inference":{{"model":"opus"}}}}"#
|
||||
))
|
||||
.expect("body parses");
|
||||
super::create_agent(axum::extract::State(state.clone()), axum::Json(req))
|
||||
.await
|
||||
.expect_err("the inference write must fail");
|
||||
}
|
||||
let bucket = swarm_queue_client::agent_permissions::open_or_create(&client)
|
||||
.await
|
||||
.expect("bucket");
|
||||
let held =
|
||||
|value: Option<bytes::Bytes>| value.map(|v| String::from_utf8_lossy(&v).into_owned());
|
||||
assert_eq!(
|
||||
held(bucket.get("atlas").await.expect("read")).as_deref(),
|
||||
Some(r#"{"permissions":["messaging"]}"#),
|
||||
"the earlier grant comes back"
|
||||
);
|
||||
assert_eq!(
|
||||
held(bucket.get("iris").await.expect("read")),
|
||||
None,
|
||||
"a grant stored for a name with none is removed"
|
||||
);
|
||||
assert_eq!(node_count(&state.jobq), 0, "nothing may be queued");
|
||||
}
|
||||
|
||||
/// A valid cap beside an invalid model stores neither and queues nothing,
|
||||
/// on a server that could have stored the cap.
|
||||
#[tokio::test]
|
||||
|
|
@ -4044,6 +4147,7 @@ mod tests {
|
|||
hive: hive.to_owned(),
|
||||
limits: None,
|
||||
inference: None,
|
||||
permissions: None,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
|
|
@ -5166,6 +5270,7 @@ mod tests {
|
|||
hive: "pr1ma".to_owned(),
|
||||
limits: None,
|
||||
inference: None,
|
||||
permissions: None,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
|
|
@ -5219,6 +5324,7 @@ mod tests {
|
|||
hive: "pr1ma".to_owned(),
|
||||
limits: None,
|
||||
inference: None,
|
||||
permissions: None,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
|
|
@ -5268,6 +5374,7 @@ mod tests {
|
|||
hive: "pr1ma".to_owned(),
|
||||
limits: None,
|
||||
inference: None,
|
||||
permissions: None,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
|
|
@ -5325,6 +5432,7 @@ mod tests {
|
|||
hive: "pr1ma".to_owned(),
|
||||
limits: None,
|
||||
inference: None,
|
||||
permissions: None,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
|
|
@ -5529,6 +5637,7 @@ mod tests {
|
|||
hive: "pr1ma".to_owned(),
|
||||
limits: None,
|
||||
inference: None,
|
||||
permissions: None,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
|
|
|
|||
Loading…
Reference in a new issue