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
|
|
@ -1,109 +0,0 @@
|
|||
//! The capabilities the swarm grants this hive's agents, as last read from the
|
||||
//! `agent-permissions` bucket by `workers::swarm_permissions`. Stored at
|
||||
//! `/var/lib/hyperhive/swarm-agent-permissions.json`, outside the meta repo:
|
||||
//! the swarm is the record, this file is the hive's last read of it.
|
||||
//!
|
||||
//! Format: agent name → the stored
|
||||
//! [`swarm_queue_client::agent_permissions::AgentPermissions`]. An agent with
|
||||
//! no entry, or an entry with `capabilities` unset, holds its default
|
||||
//! ([`default_for`]); `"capabilities": []` holds none.
|
||||
//!
|
||||
//! Read path: `lifecycle::host_config::set_nspawn_flags`, on every container
|
||||
//! start, which is why a change takes a restart.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use hive_sh4re::permissions::Capability;
|
||||
use swarm_queue_client::agent_permissions::AgentPermissions;
|
||||
|
||||
const FILE: &str = "swarm-agent-permissions.json";
|
||||
|
||||
pub type Stored = BTreeMap<String, AgentPermissions>;
|
||||
|
||||
#[must_use]
|
||||
pub fn path() -> PathBuf {
|
||||
crate::paths::state_root().join(FILE)
|
||||
}
|
||||
|
||||
/// Read the stored permissions. An absent file is the empty map: every agent
|
||||
/// on its default. A file that exists but can't be read or parsed is an error.
|
||||
pub fn read_at(path: &Path) -> std::io::Result<Stored> {
|
||||
super::read_map(path)
|
||||
}
|
||||
|
||||
/// Persist the permissions atomically.
|
||||
pub fn write_at(path: &Path, stored: &Stored) -> std::io::Result<()> {
|
||||
super::write_map(path, stored)
|
||||
}
|
||||
|
||||
/// The capabilities an agent holds when the swarm sets none: `ruth` recovers
|
||||
/// the other agents, every other agent holds nothing.
|
||||
#[must_use]
|
||||
pub fn default_for(agent: &str) -> Vec<Capability> {
|
||||
if agent == crate::lifecycle::MANAGER_NAME {
|
||||
vec![Capability::ManageRootAgent]
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// The capabilities `agent` holds under `record`.
|
||||
#[must_use]
|
||||
pub fn effective(agent: &str, record: Option<&AgentPermissions>) -> Vec<Capability> {
|
||||
record
|
||||
.and_then(|r| r.capabilities.clone())
|
||||
.unwrap_or_else(|| default_for(agent))
|
||||
}
|
||||
|
||||
/// Whether `agent` holds `cap` per the file at `path` (normally [`path`]).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// As [`read_at`].
|
||||
pub fn holds(path: &Path, agent: &str, cap: Capability) -> std::io::Result<bool> {
|
||||
Ok(effective(agent, read_at(path)?.get(agent)).contains(&cap))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn record(json: &str) -> AgentPermissions {
|
||||
serde_json::from_str(json).expect("decodes")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unset_is_the_default_and_an_empty_list_is_none() {
|
||||
assert_eq!(effective("ruth", None), vec![Capability::ManageRootAgent]);
|
||||
assert_eq!(
|
||||
effective("ruth", Some(&record("{}"))),
|
||||
vec![Capability::ManageRootAgent]
|
||||
);
|
||||
assert!(effective("ruth", Some(&record(r#"{"capabilities":[]}"#))).is_empty());
|
||||
assert!(effective("atlas", None).is_empty());
|
||||
assert_eq!(
|
||||
effective(
|
||||
"atlas",
|
||||
Some(&record(r#"{"capabilities":["manage_root_agent"]}"#))
|
||||
),
|
||||
vec![Capability::ManageRootAgent]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_file_holds_the_defaults() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let path = dir.path().join(FILE);
|
||||
assert!(holds(&path, "ruth", Capability::ManageRootAgent).expect("missing is empty"));
|
||||
assert!(!holds(&path, "atlas", Capability::ManageRootAgent).expect("missing is empty"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_corrupt_file_is_an_error() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let path = dir.path().join(FILE);
|
||||
std::fs::write(&path, "{\n \"ruth\": {\"capabilities\": [\"manage_root_ag").expect("seed");
|
||||
holds(&path, "ruth", Capability::ManageRootAgent).expect_err("truncated JSON");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue