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
97
hive-c0re/src/agent_config/permissions.rs
Normal file
97
hive-c0re/src/agent_config/permissions.rs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
//! The permissions 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 `permissions` unset, holds
|
||||
//! [`Permission::default_for`]; `"permissions": []` holds none.
|
||||
//!
|
||||
//! hive-c0re enforces one member, [`Permission::ManageRootAgent`]: read by
|
||||
//! `lifecycle::host_config::set_nspawn_flags` on every container start, which
|
||||
//! is why a change to it takes a restart. The agent reads the tool members
|
||||
//! itself.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use hive_sh4re::permissions::Permission;
|
||||
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 permissions `agent` holds under `record`.
|
||||
#[must_use]
|
||||
pub fn effective(agent: &str, record: Option<&AgentPermissions>) -> Vec<Permission> {
|
||||
record
|
||||
.and_then(|r| r.permissions.clone())
|
||||
.unwrap_or_else(|| Permission::default_for(agent))
|
||||
}
|
||||
|
||||
/// Whether `agent` holds `perm` per the file at `path` (normally [`path`]).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// As [`read_at`].
|
||||
pub fn holds(path: &Path, agent: &str, perm: Permission) -> std::io::Result<bool> {
|
||||
Ok(effective(agent, read_at(path)?.get(agent)).contains(&perm))
|
||||
}
|
||||
|
||||
#[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!(effective("ruth", None).contains(&Permission::ManageRootAgent));
|
||||
assert!(effective("ruth", Some(&record("{}"))).contains(&Permission::ManageRootAgent));
|
||||
assert!(effective("ruth", Some(&record(r#"{"permissions":[]}"#))).is_empty());
|
||||
assert_eq!(effective("atlas", None), Permission::DEFAULT.to_vec());
|
||||
assert_eq!(
|
||||
effective(
|
||||
"atlas",
|
||||
Some(&record(r#"{"permissions":["manage_root_agent"]}"#))
|
||||
),
|
||||
vec![Permission::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", Permission::ManageRootAgent).expect("missing is empty"));
|
||||
assert!(!holds(&path, "atlas", Permission::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\": {\"permissions\": [\"manage_root_ag").expect("seed");
|
||||
holds(&path, "ruth", Permission::ManageRootAgent).expect_err("truncated JSON");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue