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,17 +1,16 @@
|
|||
//! Per-agent configuration registries: tool groups, resource limits,
|
||||
//! topology (JSON files under `/var/lib/hyperhive/meta/`), the capabilities
|
||||
//! the swarm grants (outside the meta repo) and the shared wire-protocol size
|
||||
//! limits. Each submodule is re-exported at the crate root, so
|
||||
//! `crate::topology::…` etc. keep working unchanged.
|
||||
//! Per-agent configuration registries: resource limits, topology (JSON files
|
||||
//! under `/var/lib/hyperhive/meta/`), the permissions the swarm grants
|
||||
//! (outside the meta repo) and the shared wire-protocol size limits. Each
|
||||
//! submodule is re-exported at the crate root, so `crate::topology::…` etc.
|
||||
//! keep working unchanged.
|
||||
//!
|
||||
//! Note the two similarly-named modules: [`limits`] caps inline
|
||||
//! *message body* sizes on the sockets, while [`resource_limits`] holds
|
||||
//! per-agent CPU/memory caps for the container drop-in.
|
||||
|
||||
pub mod capabilities;
|
||||
pub mod limits;
|
||||
pub mod permissions;
|
||||
pub mod resource_limits;
|
||||
pub mod tool_groups;
|
||||
pub mod topology;
|
||||
|
||||
use std::io::{self, Write as _};
|
||||
|
|
|
|||
|
|
@ -1,20 +1,22 @@
|
|||
//! The capabilities the swarm grants this hive's agents, as last read from the
|
||||
//! 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 `capabilities` unset, holds its default
|
||||
//! ([`default_for`]); `"capabilities": []` holds none.
|
||||
//! no entry, or an entry with `permissions` unset, holds
|
||||
//! [`Permission::default_for`]; `"permissions": []` holds none.
|
||||
//!
|
||||
//! Read path: `lifecycle::host_config::set_nspawn_flags`, on every container
|
||||
//! start, which is why a change takes a restart.
|
||||
//! 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::Capability;
|
||||
use hive_sh4re::permissions::Permission;
|
||||
use swarm_queue_client::agent_permissions::AgentPermissions;
|
||||
|
||||
const FILE: &str = "swarm-agent-permissions.json";
|
||||
|
|
@ -37,32 +39,21 @@ 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.
|
||||
/// The permissions `agent` holds under `record`.
|
||||
#[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> {
|
||||
pub fn effective(agent: &str, record: Option<&AgentPermissions>) -> Vec<Permission> {
|
||||
record
|
||||
.and_then(|r| r.capabilities.clone())
|
||||
.unwrap_or_else(|| default_for(agent))
|
||||
.and_then(|r| r.permissions.clone())
|
||||
.unwrap_or_else(|| Permission::default_for(agent))
|
||||
}
|
||||
|
||||
/// Whether `agent` holds `cap` per the file at `path` (normally [`path`]).
|
||||
/// Whether `agent` holds `perm` 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))
|
||||
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)]
|
||||
|
|
@ -75,19 +66,16 @@ mod tests {
|
|||
|
||||
#[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!(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#"{"capabilities":["manage_root_agent"]}"#))
|
||||
Some(&record(r#"{"permissions":["manage_root_agent"]}"#))
|
||||
),
|
||||
vec![Capability::ManageRootAgent]
|
||||
vec![Permission::ManageRootAgent]
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -95,15 +83,15 @@ mod tests {
|
|||
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"));
|
||||
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\": {\"capabilities\": [\"manage_root_ag").expect("seed");
|
||||
holds(&path, "ruth", Capability::ManageRootAgent).expect_err("truncated JSON");
|
||||
std::fs::write(&path, "{\n \"ruth\": {\"permissions\": [\"manage_root_ag").expect("seed");
|
||||
holds(&path, "ruth", Permission::ManageRootAgent).expect_err("truncated JSON");
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
//!
|
||||
//! The swarm caps are stored at
|
||||
//! `/var/lib/hyperhive/meta/swarm-resource-limits.json` alongside
|
||||
//! `topology.json` and `tool-groups.json`.
|
||||
//! `topology.json`.
|
||||
//!
|
||||
//! Format: a JSON object mapping agent name to an object with optional
|
||||
//! `cpu_quota` / `memory_max` strings, passed verbatim to systemd's
|
||||
|
|
|
|||
|
|
@ -1,245 +0,0 @@
|
|||
//! Per-agent tool-group configuration. Stored at
|
||||
//! `/var/lib/hyperhive/meta/tool-groups.json` alongside `topology.json`
|
||||
//! and the meta `flake.nix`.
|
||||
//!
|
||||
//! Format: a JSON object mapping agent name to an array of
|
||||
//! `hive_sh4re::permissions::ToolGroup` `snake_case` strings:
|
||||
//!
|
||||
//! ```json
|
||||
//! {
|
||||
//! "alice": ["messaging", "meta", "inbox", "lifecycle"],
|
||||
//! "bob": ["messaging", "meta", "inbox"]
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! An absent entry (or an absent file) means "use the harness default"
|
||||
//! (`AGENT_DEFAULT`: `messaging + meta + inbox + execution`). `render_flake`
|
||||
//! in `meta.rs` reads this file and injects `HIVE_TOOL_GROUPS` into each
|
||||
//! agent's systemd service env; agents with no entry get no env var and the
|
||||
//! harness falls back to `AGENT_DEFAULT`.
|
||||
//!
|
||||
//! Write path: `set_groups` is called from the dashboard action handler
|
||||
//! that the operator uses to grant/revoke tool groups per agent.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::Context as _;
|
||||
|
||||
const TOOL_GROUPS_FILE: &str = "tool-groups.json";
|
||||
|
||||
#[must_use]
|
||||
pub fn tool_groups_path() -> PathBuf {
|
||||
crate::paths::meta_root().join(TOOL_GROUPS_FILE)
|
||||
}
|
||||
|
||||
/// Read the per-agent tool-group map. An absent file is the empty map —
|
||||
/// callers treat a missing entry as "use role default". A file that
|
||||
/// exists but can't be read or parsed is an error. Any entry that isn't a
|
||||
/// recognised [`hive_sh4re::permissions::ToolGroup`] is dropped (with a
|
||||
/// `warn!`) from what's returned — a stale or renamed name is never
|
||||
/// honoured — but `read` itself never writes; unknown names stay on disk
|
||||
/// until the next `set_groups` rewrites the file.
|
||||
pub fn read() -> std::io::Result<BTreeMap<String, Vec<String>>> {
|
||||
read_from(&tool_groups_path())
|
||||
}
|
||||
|
||||
fn read_from(path: &Path) -> std::io::Result<BTreeMap<String, Vec<String>>> {
|
||||
let mut map = super::read_map(path)?;
|
||||
prune_unknown(&mut map);
|
||||
Ok(map)
|
||||
}
|
||||
|
||||
/// True if `name` matches a recognised [`hive_sh4re::permissions::ToolGroup`],
|
||||
/// case-insensitive (same comparison [`validate_groups`] uses, and the same
|
||||
/// comparison `capabilities::is_known` uses — a hand-edited `"Meta"` in the
|
||||
/// file should round-trip, not get pruned).
|
||||
fn is_known(name: &str) -> bool {
|
||||
hive_sh4re::permissions::ToolGroup::ALL
|
||||
.iter()
|
||||
.any(|g| name.eq_ignore_ascii_case(<&str>::from(*g)))
|
||||
}
|
||||
|
||||
/// Drop any tool-group string in `map` that isn't a recognised
|
||||
/// [`hive_sh4re::permissions::ToolGroup`], warning per dropped string so a
|
||||
/// stale or renamed grant doesn't look like it's still in effect. An agent
|
||||
/// left with no groups is removed entirely, matching [`set_groups`]'s
|
||||
/// empty-vec behaviour (reverts to the harness role default). Returns
|
||||
/// whether anything was dropped.
|
||||
fn prune_unknown(map: &mut BTreeMap<String, Vec<String>>) -> bool {
|
||||
let mut dropped_any = false;
|
||||
map.retain(|agent, groups| {
|
||||
groups.retain(|g| {
|
||||
let known = is_known(g);
|
||||
if !known {
|
||||
dropped_any = true;
|
||||
tracing::warn!(agent, tool_group = %g, "dropping unrecognised tool group");
|
||||
}
|
||||
known
|
||||
});
|
||||
!groups.is_empty()
|
||||
});
|
||||
dropped_any
|
||||
}
|
||||
|
||||
/// Look up the configured tool groups for one agent. Returns an empty
|
||||
/// vec when the agent has no entry — callers should treat this as
|
||||
/// "use the harness role default." Errors as [`read`] does.
|
||||
pub fn groups_for(name: &str) -> std::io::Result<Vec<String>> {
|
||||
Ok(read()?.get(name).cloned().unwrap_or_default())
|
||||
}
|
||||
|
||||
/// Validate a slice of group name strings against `ToolGroup::ALL`.
|
||||
/// Returns `Ok(())` when all names are known, or `Err` listing the
|
||||
/// unrecognised names so callers can surface a useful error message.
|
||||
pub fn validate_groups(groups: &[String]) -> anyhow::Result<()> {
|
||||
let unknown: Vec<&str> = groups
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.filter(|s| !is_known(s))
|
||||
.collect();
|
||||
if unknown.is_empty() {
|
||||
Ok(())
|
||||
} else {
|
||||
anyhow::bail!(
|
||||
"unknown tool group(s): {}; valid names are: {}",
|
||||
unknown.join(", "),
|
||||
hive_sh4re::permissions::ToolGroup::ALL
|
||||
.iter()
|
||||
.map(|g| <&str>::from(*g))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the tool groups for one agent and persist the map. An empty
|
||||
/// `groups` vec removes the entry (agent reverts to role default).
|
||||
/// Returns an error if any name is not in `ToolGroup::ALL`, or if the
|
||||
/// existing file can't be read or parsed — the file is then left
|
||||
/// untouched.
|
||||
pub fn set_groups(name: &str, groups: &[String]) -> anyhow::Result<()> {
|
||||
set_groups_at(&tool_groups_path(), name, groups)
|
||||
}
|
||||
|
||||
fn set_groups_at(path: &Path, name: &str, groups: &[String]) -> anyhow::Result<()> {
|
||||
if !groups.is_empty() {
|
||||
validate_groups(groups)?;
|
||||
}
|
||||
let mut current: BTreeMap<String, Vec<String>> = super::read_map(path)?;
|
||||
if groups.is_empty() {
|
||||
current.remove(name);
|
||||
} else {
|
||||
current.insert(name.to_owned(), groups.to_vec());
|
||||
}
|
||||
super::write_map(path, ¤t).with_context(|| format!("write tool-groups for {name}"))
|
||||
}
|
||||
|
||||
/// Drop the entry for an agent that is being destroyed. Idempotent.
|
||||
/// Fails without writing if the existing file can't be read or parsed.
|
||||
pub fn remove_agent(name: &str) -> std::io::Result<()> {
|
||||
remove_agent_at(&tool_groups_path(), name)
|
||||
}
|
||||
|
||||
fn remove_agent_at(path: &Path, name: &str) -> std::io::Result<()> {
|
||||
let mut current: BTreeMap<String, Vec<String>> = super::read_map(path)?;
|
||||
if current.remove(name).is_some() {
|
||||
super::write_map(path, ¤t)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TRUNCATED: &str = "{\n \"alice\": [\"messaging\", \"meta\"],\n \"bob\": [\"mess";
|
||||
|
||||
fn corrupt_file() -> (tempfile::TempDir, PathBuf) {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let path = dir.path().join(TOOL_GROUPS_FILE);
|
||||
std::fs::write(&path, TRUNCATED).expect("seed");
|
||||
(dir, path)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_groups_leaves_a_corrupt_file_untouched() {
|
||||
let (_dir, path) = corrupt_file();
|
||||
let err = set_groups_at(&path, "ruth", &["messaging".to_owned()])
|
||||
.expect_err("a corrupt file must not be overwritten");
|
||||
let kind = err
|
||||
.downcast_ref::<std::io::Error>()
|
||||
.map(std::io::Error::kind);
|
||||
assert_eq!(kind, Some(std::io::ErrorKind::InvalidData));
|
||||
assert_eq!(std::fs::read(&path).expect("read"), TRUNCATED.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_agent_leaves_a_corrupt_file_untouched() {
|
||||
let (_dir, path) = corrupt_file();
|
||||
let err =
|
||||
remove_agent_at(&path, "alice").expect_err("a corrupt file must not be overwritten");
|
||||
assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
|
||||
assert_eq!(std::fs::read(&path).expect("read"), TRUNCATED.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_groups_keeps_other_agents_entries() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let path = dir.path().join(TOOL_GROUPS_FILE);
|
||||
set_groups_at(&path, "alice", &["inbox".to_owned()]).expect("set on missing file");
|
||||
set_groups_at(&path, "ruth", &["messaging".to_owned()]).expect("set");
|
||||
let map: BTreeMap<String, Vec<String>> =
|
||||
crate::agent_config::read_map(&path).expect("read");
|
||||
assert_eq!(map["alice"], vec!["inbox".to_owned()]);
|
||||
assert_eq!(map["ruth"], vec!["messaging".to_owned()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prune_unknown_keeps_known_name() {
|
||||
let mut map = BTreeMap::from([("alice".to_owned(), vec!["messaging".to_owned()])]);
|
||||
assert!(!prune_unknown(&mut map));
|
||||
assert_eq!(map["alice"], vec!["messaging".to_owned()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prune_unknown_keeps_a_hand_edited_differently_cased_known_name() {
|
||||
let mut map = BTreeMap::from([("alice".to_owned(), vec!["Messaging".to_owned()])]);
|
||||
assert!(!prune_unknown(&mut map));
|
||||
assert_eq!(map["alice"], vec!["Messaging".to_owned()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prune_unknown_drops_unrecognised_name() {
|
||||
let mut map = BTreeMap::from([("alice".to_owned(), vec!["diagnostics".to_owned()])]);
|
||||
assert!(prune_unknown(&mut map));
|
||||
assert!(!map.contains_key("alice"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prune_unknown_keeps_known_and_drops_unknown_in_same_entry() {
|
||||
let mut map = BTreeMap::from([(
|
||||
"alice".to_owned(),
|
||||
vec!["messaging".to_owned(), "scheduling".to_owned()],
|
||||
)]);
|
||||
assert!(prune_unknown(&mut map));
|
||||
assert_eq!(map["alice"], vec!["messaging".to_owned()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_from_drops_an_unknown_name_still_on_disk_without_rewriting_the_file() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let path = dir.path().join(TOOL_GROUPS_FILE);
|
||||
std::fs::write(&path, r#"{"alice": ["messaging", "diagnostics"]}"#).expect("seed");
|
||||
|
||||
// Exercises `read()`'s own wiring, not a hand-rolled equivalent —
|
||||
// deleting the `prune_unknown` call inside `read_from` must fail
|
||||
// this assertion.
|
||||
let map = read_from(&path).expect("read_from");
|
||||
assert_eq!(map["alice"], vec!["messaging".to_owned()]);
|
||||
|
||||
// `read()` itself never writes — the stale name is still on disk.
|
||||
let on_disk = std::fs::read_to_string(&path).expect("read file");
|
||||
assert!(on_disk.contains("diagnostics"), "{on_disk}");
|
||||
}
|
||||
}
|
||||
|
|
@ -9,11 +9,11 @@
|
|||
//! dashboard's tree view. All of that is gone; what the file is
|
||||
//! *for* now is the one thing that survived the removal — naming every
|
||||
//! agent, which is the set a
|
||||
//! [`hive_sh4re::permissions::Capability::ManageRootAgent`] holder gets
|
||||
//! [`hive_sh4re::permissions::Permission::ManageRootAgent`] holder gets
|
||||
//! bind-mounted ([`all_agents`]).
|
||||
//!
|
||||
//! Broader-than-your-own bind-mount grants are **not** stored here: they
|
||||
//! hang off the `ManageRootAgent` capability the swarm grants
|
||||
//! hang off the `ManageRootAgent` permission the swarm grants
|
||||
//! (`swarm_permissions`). See
|
||||
//! `lifecycle::set_nspawn_flags`.
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ fn read_from(path: &Path) -> std::io::Result<BTreeSet<String>> {
|
|||
}
|
||||
|
||||
/// Every agent the roster knows about, in name order. This is the set
|
||||
/// a [`hive_sh4re::permissions::Capability::ManageRootAgent`] holder gets
|
||||
/// a [`hive_sh4re::permissions::Permission::ManageRootAgent`] holder gets
|
||||
/// bind-mounted, and it is deliberately unfiltered: that capability means
|
||||
/// "may manage any agent", so the set is all of them.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -552,52 +552,6 @@ impl Coordinator {
|
|||
});
|
||||
}
|
||||
|
||||
/// Best-effort synchronous snapshot of live container names (the
|
||||
/// logical agent names from the last `nixos-container list` scan).
|
||||
/// Returns `None` when the roster cache lock is momentarily
|
||||
/// contended. The async `containers_snapshot` is the reliable path
|
||||
/// for request handlers; this exists for the sync SSE snapshot emits.
|
||||
#[must_use]
|
||||
pub fn live_container_names_blocking(&self) -> Option<HashSet<String>> {
|
||||
self.last_containers
|
||||
.try_lock()
|
||||
.ok()
|
||||
.map(|m| m.keys().cloned().collect())
|
||||
}
|
||||
|
||||
/// Emit a `ToolGroupsChanged` snapshot event. Called from the
|
||||
/// rebuild-queue worker after a `PermChange` / `ToolGroups` entry
|
||||
/// commits the JSON file, so the P3RM1SS10NS tab updates live.
|
||||
pub fn emit_tool_groups_snapshot(self: &Arc<Self>) {
|
||||
use hive_sh4re::permissions::ToolGroup;
|
||||
let groups = ToolGroup::ALL.iter().map(|g| <&str>::from(*g)).collect();
|
||||
let descriptions = ToolGroup::ALL
|
||||
.iter()
|
||||
.map(|g| (<&str>::from(*g), g.description()))
|
||||
.collect();
|
||||
let assignments = match crate::tool_groups::read() {
|
||||
Ok(map) => map,
|
||||
Err(e) => {
|
||||
tracing::warn!(error = ?e, "tool-groups snapshot skipped");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let roster = self.live_container_names_blocking().unwrap_or_default();
|
||||
let (agents, effective) = crate::dashboard::permissions::roster_and_effective(
|
||||
roster,
|
||||
&assignments,
|
||||
&crate::dashboard::permissions::tool_group_default_names(),
|
||||
);
|
||||
self.emit_dashboard_event(DashboardEvent::ToolGroupsChanged {
|
||||
seq: self.next_seq(),
|
||||
groups,
|
||||
descriptions,
|
||||
assignments,
|
||||
agents,
|
||||
effective,
|
||||
});
|
||||
}
|
||||
|
||||
/// Subscribe to the shutdown watch channel. Background tasks call
|
||||
/// this at spawn time and break their loop when the receiver
|
||||
/// transitions to `true` (via `Coordinator::request_shutdown`).
|
||||
|
|
|
|||
|
|
@ -37,7 +37,6 @@ use crate::lifecycle;
|
|||
(name = "lifecycle_ops", description = "agent container lifecycle: rebuild/restart/start/stop/pause/limits"),
|
||||
(name = "meta_inputs", description = "bulk flake-input update for the meta flake"),
|
||||
(name = "misc_api", description = "mark-all-read, compose, spawn-request, hive stats"),
|
||||
(name = "permissions", description = "tool-group assignment for agents"),
|
||||
(name = "rebuild_queue", description = "rebuild-queue cancel"),
|
||||
(name = "state_files", description = "proxied reads of allow-listed per-agent state files"),
|
||||
(name = "state_snapshot", description = "cold-load dashboard snapshot"),
|
||||
|
|
@ -56,7 +55,6 @@ mod health;
|
|||
mod lifecycle_ops;
|
||||
mod meta_inputs;
|
||||
mod misc_api;
|
||||
pub(crate) mod permissions;
|
||||
mod rebuild_queue;
|
||||
mod state_files;
|
||||
mod state_snapshot;
|
||||
|
|
@ -124,11 +122,6 @@ fn app(coord: Arc<Coordinator>) -> axum::Router {
|
|||
.routes(routes!(build_logs::get_build_logs_agent))
|
||||
.routes(routes!(build_logs::get_build_log_full))
|
||||
.routes(routes!(build_logs::get_build_log_raw))
|
||||
.routes(routes!(permissions::get_tool_groups))
|
||||
.routes(routes!(permissions::post_tool_groups))
|
||||
.routes(routes!(permissions::post_permissions))
|
||||
.routes(routes!(permissions::get_stale_permissions))
|
||||
.routes(routes!(permissions::delete_agent_permissions))
|
||||
.routes(routes!(rebuild_queue::post_rebuild_queue_cancel))
|
||||
.routes(routes!(lifecycle_ops::post_destroy))
|
||||
.routes(routes!(lifecycle_ops::post_kill))
|
||||
|
|
@ -376,11 +369,6 @@ mod router_build_probe {
|
|||
.routes(routes!(build_logs::get_build_logs_agent))
|
||||
.routes(routes!(build_logs::get_build_log_full))
|
||||
.routes(routes!(build_logs::get_build_log_raw))
|
||||
.routes(routes!(permissions::get_tool_groups))
|
||||
.routes(routes!(permissions::post_tool_groups))
|
||||
.routes(routes!(permissions::post_permissions))
|
||||
.routes(routes!(permissions::get_stale_permissions))
|
||||
.routes(routes!(permissions::delete_agent_permissions))
|
||||
.routes(routes!(rebuild_queue::post_rebuild_queue_cancel))
|
||||
.routes(routes!(lifecycle_ops::post_destroy))
|
||||
.routes(routes!(lifecycle_ops::post_kill))
|
||||
|
|
|
|||
|
|
@ -1,403 +0,0 @@
|
|||
//! Tool-group permission endpoints for the dashboard.
|
||||
//!
|
||||
//! Read endpoints return the full set of known groups plus
|
||||
//! descriptions and the per-agent assignment map (the UI never hard-codes
|
||||
//! the lists). Write endpoints validate, then enqueue a `PermChange` so the
|
||||
//! JSON file write is serialised through the FIFO rebuild worker.
|
||||
|
||||
use axum::{
|
||||
extract::{Path as AxumPath, State},
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
use problem_details::ProblemDetails;
|
||||
|
||||
use super::{AppState, Ident, guard_agent_name, strip_container_prefix};
|
||||
|
||||
#[derive(Serialize, ToSchema)]
|
||||
pub(super) struct ToolGroupsSnapshot {
|
||||
/// Ordered list of all known tool-group names. Drives the column
|
||||
/// headers in the tool-groups table — the UI does not hard-code them.
|
||||
groups: Vec<&'static str>,
|
||||
/// Short description for each group name. Keys match `groups` entries.
|
||||
descriptions: std::collections::BTreeMap<&'static str, &'static str>,
|
||||
/// Per-agent *explicit* assignment map. Absent agents use the role
|
||||
/// default — the UI uses presence here to badge an agent "(default)".
|
||||
assignments: std::collections::BTreeMap<String, Vec<String>>,
|
||||
/// Every agent the operator can manage (live roster ∪ explicit keys),
|
||||
/// sorted. The UI lists rows from this so default-perms agents always
|
||||
/// appear without depending on a separately-loaded container list.
|
||||
agents: Vec<String>,
|
||||
/// Per-agent *effective* groups: the explicit entry when present, else
|
||||
/// the role default the harness actually applies. Drives the checkbox
|
||||
/// state so default agents show their real groups, not blank.
|
||||
effective: std::collections::BTreeMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
/// Every known tool-group name + description,
|
||||
/// plus the per-agent explicit/effective assignment maps.
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/api/tool-groups",
|
||||
responses(
|
||||
(status = 200, description = "tool-group catalogue + assignments", body = ToolGroupsSnapshot),
|
||||
(status = 500, description = "tool-groups file unreadable"),
|
||||
),
|
||||
tag = "permissions"
|
||||
)]
|
||||
pub(super) async fn get_tool_groups(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<axum::Json<ToolGroupsSnapshot>, ProblemDetails> {
|
||||
let groups = hive_sh4re::permissions::ToolGroup::ALL
|
||||
.iter()
|
||||
.map(|g| <&str>::from(*g))
|
||||
.collect();
|
||||
let descriptions = hive_sh4re::permissions::ToolGroup::ALL
|
||||
.iter()
|
||||
.map(|g| (<&str>::from(*g), g.description()))
|
||||
.collect();
|
||||
let assignments = crate::tool_groups::read().map_err(|e| unreadable(&e))?;
|
||||
let roster = state
|
||||
.coord
|
||||
.containers_snapshot()
|
||||
.await
|
||||
.into_iter()
|
||||
.map(|c| c.name);
|
||||
let (agents, effective) =
|
||||
roster_and_effective(roster, &assignments, &tool_group_default_names());
|
||||
Ok(axum::Json(ToolGroupsSnapshot {
|
||||
groups,
|
||||
descriptions,
|
||||
assignments,
|
||||
agents,
|
||||
effective,
|
||||
}))
|
||||
}
|
||||
|
||||
/// A permission file that exists but can't be read or parsed. Surfaced
|
||||
/// as a 500 rather than an empty table, which would read as "nobody has
|
||||
/// any grants".
|
||||
fn unreadable(e: &std::io::Error) -> ProblemDetails {
|
||||
ProblemDetails::from_status_code(StatusCode::INTERNAL_SERVER_ERROR).with_detail(e.to_string())
|
||||
}
|
||||
|
||||
/// The role-default tool-group names the harness falls back to for an
|
||||
/// agent with no explicit entry (`ToolGroup::AGENT_DEFAULT`). Shared by
|
||||
/// the HTTP snapshot and the SSE emit so the effective values match what
|
||||
/// the container actually runs with.
|
||||
#[must_use]
|
||||
pub(crate) fn tool_group_default_names() -> Vec<&'static str> {
|
||||
hive_sh4re::permissions::ToolGroup::AGENT_DEFAULT
|
||||
.iter()
|
||||
.map(|g| <&str>::from(*g))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Build the `(agents, effective)` pair for a permissions snapshot.
|
||||
/// `agents` is the sorted union of the live roster and any agent that
|
||||
/// already has an explicit entry; `effective[agent]` is the explicit
|
||||
/// assignment when present, else `default` (the role fallback). Shared by
|
||||
/// the tool-groups snapshots on both the HTTP and SSE paths so the two
|
||||
/// surfaces never drift.
|
||||
#[must_use]
|
||||
pub(crate) fn roster_and_effective(
|
||||
roster: impl IntoIterator<Item = String>,
|
||||
explicit: &std::collections::BTreeMap<String, Vec<String>>,
|
||||
default: &[&'static str],
|
||||
) -> (Vec<String>, std::collections::BTreeMap<String, Vec<String>>) {
|
||||
let mut names: std::collections::BTreeSet<String> = roster.into_iter().collect();
|
||||
names.extend(explicit.keys().cloned());
|
||||
let agents: Vec<String> = names.into_iter().collect();
|
||||
let effective = agents
|
||||
.iter()
|
||||
.map(|a| {
|
||||
let v = explicit
|
||||
.get(a)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| default.iter().map(|s| (*s).to_owned()).collect::<Vec<_>>());
|
||||
(a.clone(), v)
|
||||
})
|
||||
.collect();
|
||||
(agents, effective)
|
||||
}
|
||||
|
||||
#[derive(Deserialize, ToSchema)]
|
||||
pub(super) struct SetToolGroupsBody {
|
||||
groups: Vec<String>,
|
||||
}
|
||||
|
||||
/// Replace `agent`'s explicit tool-group
|
||||
/// assignment (JSON body `{"groups": [...]}`).
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/tool-groups/{agent}",
|
||||
params(("agent" = String, Path, description = "agent name")),
|
||||
request_body = SetToolGroupsBody,
|
||||
responses(
|
||||
(status = 200, description = "tool-groups queued for write", body = String),
|
||||
(status = 400, description = "unknown group name, or no such agent"),
|
||||
(status = 404, description = "no such agent"),
|
||||
),
|
||||
tag = "permissions"
|
||||
)]
|
||||
pub(super) async fn post_tool_groups(
|
||||
State(state): State<AppState>,
|
||||
AxumPath(name): AxumPath<String>,
|
||||
axum::Json(body): axum::Json<SetToolGroupsBody>,
|
||||
) -> Result<Response, ProblemDetails> {
|
||||
let logical = strip_container_prefix(&name);
|
||||
// `guard_agent_name` yields a ready-made rejection `Response`; pass it
|
||||
// through as `Ok` (axum sends it verbatim) rather than re-deriving a
|
||||
// `ProblemDetails` — the guard is shared with `-> Response` handlers.
|
||||
if let Some(reject) = guard_agent_name(&state, &logical).await {
|
||||
return Ok(reject);
|
||||
}
|
||||
// Validate group names before queuing — fail fast so the operator
|
||||
// sees the error immediately rather than waiting for the worker.
|
||||
if let Err(e) = crate::tool_groups::validate_groups(&body.groups) {
|
||||
return Err(ProblemDetails::from_status_code(StatusCode::BAD_REQUEST)
|
||||
.with_detail(format!("invalid tool-groups for {logical}: {e}")));
|
||||
}
|
||||
// Submit a PermChange DAG: the JSON file write commits under
|
||||
// META_LOCK inside the WritePermFile node, so concurrent
|
||||
// batch-apply actions for different agents never race on the
|
||||
// shared tool-groups.json.
|
||||
state
|
||||
.coord
|
||||
.job_queue
|
||||
.insert_job(|b| {
|
||||
crate::job_queue::templates::perm_change(
|
||||
b,
|
||||
&logical,
|
||||
crate::job_queue::PermPayload::ToolGroups {
|
||||
groups: body.groups.clone(),
|
||||
},
|
||||
);
|
||||
Vec::new()
|
||||
})
|
||||
.expect("template-declared shapes are acyclic");
|
||||
state.coord.emit_rebuild_queue_snapshot();
|
||||
tracing::info!(agent = %logical, groups = ?body.groups, "operator: set tool-groups via dashboard");
|
||||
Ok((StatusCode::OK, "ok").into_response())
|
||||
}
|
||||
|
||||
/// One agent's slice of a batch permission change. An omitted
|
||||
/// `tool_groups` leaves the agent untouched, an empty array clears it, a
|
||||
/// populated array fully replaces it (same replace semantics as the
|
||||
/// per-agent endpoint).
|
||||
#[derive(Deserialize, ToSchema)]
|
||||
pub(super) struct PermChangeBody {
|
||||
agent: String,
|
||||
#[serde(default)]
|
||||
tool_groups: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, ToSchema)]
|
||||
pub(super) struct BatchPermsBody {
|
||||
changes: Vec<PermChangeBody>,
|
||||
}
|
||||
|
||||
/// Batch permission apply — `POST /api/permissions`.
|
||||
///
|
||||
/// The save-all permissions UI sends only the agents whose groups
|
||||
/// changed; each gets one `PermChange`. The whole batch is atomic: every
|
||||
/// change is validated up front and on any validation error nothing is
|
||||
/// written or enqueued.
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/permissions",
|
||||
request_body = BatchPermsBody,
|
||||
responses(
|
||||
(status = 200, description = "batch permission change queued", body = String),
|
||||
(status = 400, description = "unknown tool-group name, or no such agent"),
|
||||
(status = 404, description = "no such agent"),
|
||||
),
|
||||
tag = "permissions"
|
||||
)]
|
||||
pub(super) async fn post_permissions(
|
||||
State(state): State<AppState>,
|
||||
axum::Json(body): axum::Json<BatchPermsBody>,
|
||||
) -> Result<Response, ProblemDetails> {
|
||||
// Phase 1 — validate everything before touching any file or the
|
||||
// queue, so a bad entry fails the whole POST with zero side effects.
|
||||
// No-op rows (field omitted) are skipped, not errors.
|
||||
let mut staged: Vec<(String, Vec<String>)> = Vec::new();
|
||||
for change in &body.changes {
|
||||
let logical = strip_container_prefix(&change.agent);
|
||||
if let Some(reject) = guard_agent_name(&state, &logical).await {
|
||||
return Ok(reject);
|
||||
}
|
||||
if let Some(groups) = &change.tool_groups {
|
||||
if let Err(e) = crate::tool_groups::validate_groups(groups) {
|
||||
return Err(ProblemDetails::from_status_code(StatusCode::BAD_REQUEST)
|
||||
.with_detail(format!("invalid tool-groups for {logical}: {e}")));
|
||||
}
|
||||
staged.push((logical, groups.clone()));
|
||||
}
|
||||
}
|
||||
// Phase 2 — submit one PermChange DAG per affected agent.
|
||||
for (logical, groups) in staged {
|
||||
state
|
||||
.coord
|
||||
.job_queue
|
||||
.insert_job(|b| {
|
||||
crate::job_queue::templates::perm_change(
|
||||
b,
|
||||
&logical,
|
||||
crate::job_queue::PermPayload::ToolGroups { groups },
|
||||
);
|
||||
Vec::new()
|
||||
})
|
||||
.expect("template-declared shapes are acyclic");
|
||||
state.coord.emit_rebuild_queue_snapshot();
|
||||
tracing::info!(agent = %logical, "operator: batch perm change via dashboard");
|
||||
}
|
||||
Ok((StatusCode::OK, "ok").into_response())
|
||||
}
|
||||
|
||||
/// Agent names that have explicit tool-group entries but are
|
||||
/// not in the live container roster AND not in the kept-state directory
|
||||
/// list (i.e. truly gone — renamed or destroyed agents whose JSON entries
|
||||
/// persisted). The client uses this to drive the "stale permission entries"
|
||||
/// sub-section in K3PT ST4T3 without having to fetch three separate
|
||||
/// endpoints and perform set arithmetic on the client side.
|
||||
#[derive(Serialize, ToSchema)]
|
||||
pub(super) struct StalePermsResponse {
|
||||
/// Ghost agent names, sorted. Empty list → no stale entries.
|
||||
stale: Vec<String>,
|
||||
}
|
||||
|
||||
/// Agent names with explicit permission
|
||||
/// entries but no matching live container or kept-state dir.
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/api/permissions/stale",
|
||||
responses(
|
||||
(status = 200, description = "ghost agent names with stale permission entries", body = StalePermsResponse),
|
||||
(status = 500, description = "tool-groups file unreadable"),
|
||||
),
|
||||
tag = "permissions"
|
||||
)]
|
||||
pub(super) async fn get_stale_permissions(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<axum::Json<StalePermsResponse>, ProblemDetails> {
|
||||
// Live container names — includes stopped-but-configured containers.
|
||||
let live: std::collections::HashSet<String> = state
|
||||
.coord
|
||||
.containers_snapshot()
|
||||
.await
|
||||
.into_iter()
|
||||
.map(|c| c.name)
|
||||
.collect();
|
||||
// Kept-state directories: every agent that ever had a state dir on disk,
|
||||
// including both live containers (already in `live`) and soft-deleted
|
||||
// tombstones (destroyed but kept). A name present here is either live
|
||||
// or a properly-removed tombstone — neither is a ghost.
|
||||
let kept: std::collections::HashSet<String> =
|
||||
crate::coordinator::Coordinator::kept_state_names()
|
||||
.into_iter()
|
||||
.map(hive_types::Ident::into_string)
|
||||
.collect();
|
||||
// Known = live roster ∪ kept-state names.
|
||||
let known: std::collections::HashSet<&String> = live.iter().chain(kept.iter()).collect();
|
||||
let tgs = crate::tool_groups::read().map_err(|e| unreadable(&e))?;
|
||||
// `tgs` is a `BTreeMap`, so its keys are already sorted.
|
||||
let ghost_names: Vec<String> = tgs.keys().filter(|n| !known.contains(n)).cloned().collect();
|
||||
Ok(axum::Json(StalePermsResponse { stale: ghost_names }))
|
||||
}
|
||||
|
||||
/// Clear all explicit permission entries for a named agent without
|
||||
/// requiring it to exist in the live roster.
|
||||
///
|
||||
/// Used by the P3RM1SS10NS tab's "remove" button for agents that have
|
||||
/// stale explicit entries in `tool-groups.json` but are no longer
|
||||
/// running (e.g. an agent that was renamed or destroyed while its JSON
|
||||
/// entries persisted).
|
||||
///
|
||||
/// Bypasses `guard_agent_name`'s live-roster check intentionally —
|
||||
/// the whole point is to remove entries for non-roster agents. Only
|
||||
/// the format check ([`Ident::parse`]) is applied. No rebuild is
|
||||
/// enqueued (the agent doesn't exist to rebuild); the SSE snapshot
|
||||
/// updates the P3RM1SS10NS tab live.
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
path = "/api/permissions/{agent}",
|
||||
params(("agent" = String, Path, description = "agent name (need not be live)")),
|
||||
responses(
|
||||
(status = 200, description = "stale permission entries cleared", body = String),
|
||||
(status = 400, description = "bad agent name"),
|
||||
(status = 500, description = "tool-groups file write failed"),
|
||||
),
|
||||
tag = "permissions"
|
||||
)]
|
||||
pub(super) async fn delete_agent_permissions(
|
||||
State(state): State<AppState>,
|
||||
AxumPath(name): AxumPath<String>,
|
||||
) -> Response {
|
||||
let logical = match Ident::parse(&strip_container_prefix(&name)) {
|
||||
Ok(n) => n,
|
||||
Err(reason) => {
|
||||
return (StatusCode::BAD_REQUEST, format!("bad agent name: {reason}")).into_response();
|
||||
}
|
||||
};
|
||||
let removed = crate::tool_groups::remove_agent(logical.as_str());
|
||||
state.coord.emit_tool_groups_snapshot();
|
||||
// Surface an I/O error as 500 so the frontend's `!resp.ok` path
|
||||
// fires and the operator sees a meaningful message rather than a
|
||||
// silent "success" followed by the row reappearing unchanged.
|
||||
if let Err(e) = removed {
|
||||
tracing::warn!(agent = %logical, error = ?e, "failed to remove tool-groups entry");
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("failed to remove permission entries for {logical}: {e}"),
|
||||
)
|
||||
.into_response();
|
||||
}
|
||||
tracing::info!(agent = %logical, "operator: cleared stale permission entries via dashboard");
|
||||
(StatusCode::OK, "ok").into_response()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::roster_and_effective;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[test]
|
||||
fn effective_unions_roster_and_explicit_and_applies_default() {
|
||||
let mut explicit: BTreeMap<String, Vec<String>> = BTreeMap::new();
|
||||
explicit.insert("iris".to_owned(), vec!["messaging".to_owned()]);
|
||||
// `ruth` has an explicit entry but isn't in the live roster (e.g.
|
||||
// momentarily not listed) — it must still appear and keep its
|
||||
// explicit value.
|
||||
explicit.insert(
|
||||
"ruth".to_owned(),
|
||||
vec!["messaging".to_owned(), "lifecycle".to_owned()],
|
||||
);
|
||||
let roster = ["damocles".to_owned(), "iris".to_owned()];
|
||||
let default = ["messaging", "meta", "inbox", "execution"];
|
||||
|
||||
let (agents, effective) = roster_and_effective(roster, &explicit, &default);
|
||||
|
||||
// Sorted union of roster + explicit keys.
|
||||
assert_eq!(agents, vec!["damocles", "iris", "ruth"]);
|
||||
// Entry-less agent (damocles) → role default.
|
||||
assert_eq!(
|
||||
effective["damocles"],
|
||||
vec!["messaging", "meta", "inbox", "execution"]
|
||||
);
|
||||
// Explicit entries kept verbatim.
|
||||
assert_eq!(effective["iris"], vec!["messaging"]);
|
||||
assert_eq!(effective["ruth"], vec!["messaging", "lifecycle"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_default_yields_empty_effective_for_entryless() {
|
||||
let explicit: BTreeMap<String, Vec<String>> = BTreeMap::new();
|
||||
let (agents, effective) = roster_and_effective(["atlas".to_owned()], &explicit, &[]);
|
||||
assert_eq!(agents, vec!["atlas"]);
|
||||
assert!(effective["atlas"].is_empty());
|
||||
}
|
||||
}
|
||||
|
|
@ -141,24 +141,6 @@ pub enum DashboardEvent {
|
|||
/// (transients, container state), and a poll loop would be
|
||||
/// slower for the same information.
|
||||
RebuildQueueChanged { seq: u64 },
|
||||
/// Full snapshot of tool-group assignments (per-agent `Vec<group_name>`).
|
||||
/// Emitted from the rebuild-queue worker after a `PermChange`
|
||||
/// `ToolGroups` entry commits the JSON file. Lets the P3RM1SS10NS
|
||||
/// tab update live when the worker applies a queued change.
|
||||
ToolGroupsChanged {
|
||||
seq: u64,
|
||||
/// Ordered list of all known tool-group names (column headers).
|
||||
groups: Vec<&'static str>,
|
||||
/// Short description for each group name (tooltip).
|
||||
descriptions: std::collections::BTreeMap<&'static str, &'static str>,
|
||||
/// Per-agent *explicit* assignment map; absent agents use the role
|
||||
/// default (the UI badges those "(default)").
|
||||
assignments: std::collections::BTreeMap<String, Vec<String>>,
|
||||
/// Sorted roster the operator can manage (live ∪ explicit keys).
|
||||
agents: Vec<String>,
|
||||
/// Per-agent *effective* groups (explicit-or-`AGENT_DEFAULT`).
|
||||
effective: std::collections::BTreeMap<String, Vec<String>>,
|
||||
},
|
||||
}
|
||||
|
||||
impl DashboardEvent {
|
||||
|
|
@ -185,7 +167,6 @@ impl DashboardEvent {
|
|||
DashboardEvent::MetaInputsChanged { .. } => "meta_inputs_changed",
|
||||
DashboardEvent::MetaUpdateRunning { .. } => "meta_update_running",
|
||||
DashboardEvent::RebuildQueueChanged { .. } => "rebuild_queue_changed",
|
||||
DashboardEvent::ToolGroupsChanged { .. } => "tool_groups_changed",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -260,14 +241,6 @@ mod tests {
|
|||
running: false,
|
||||
},
|
||||
DashboardEvent::RebuildQueueChanged { seq: 1 },
|
||||
DashboardEvent::ToolGroupsChanged {
|
||||
seq: 1,
|
||||
groups: Vec::new(),
|
||||
descriptions: std::collections::BTreeMap::new(),
|
||||
assignments: std::collections::BTreeMap::new(),
|
||||
agents: Vec::new(),
|
||||
effective: std::collections::BTreeMap::new(),
|
||||
},
|
||||
];
|
||||
for ev in samples {
|
||||
let v: serde_json::Value = serde_json::to_value(&ev).expect("serialise");
|
||||
|
|
|
|||
|
|
@ -120,7 +120,6 @@ pub(super) async fn run_node(
|
|||
// The payload rides the node and is destructured here, so the executor
|
||||
// takes it directly instead of re-matching the kind behind a `bail!`
|
||||
// that could never fire.
|
||||
NodeKind::WritePermFile { payload, .. } => run_write_perm_file(coord, agent, payload).await,
|
||||
NodeKind::EmitRebuilt { ok, .. } => {
|
||||
run_emit_rebuilt(coord, agent, coord.job_queue.root_of(id), *ok).await;
|
||||
Ok(())
|
||||
|
|
@ -790,30 +789,6 @@ async fn run_write_dropin(coord: &Arc<Coordinator>, name: &str) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Write + commit the perm file(s) (fused under `META_LOCK` so the
|
||||
/// working tree is never left dirty), then emit the P3RM1SS10NS-tab
|
||||
/// snapshots so the dashboard reflects the new assignment.
|
||||
async fn run_write_perm_file(
|
||||
coord: &Arc<Coordinator>,
|
||||
name: &str,
|
||||
payload: &super::model::PermPayload,
|
||||
) -> Result<()> {
|
||||
use super::model::PermPayload;
|
||||
// Runs under the deploy window (it declares `Resource::MetaWindow`): a
|
||||
// perm commit landing inside another node's staged prepare→finalize
|
||||
// window would sweep the staged deploy lock into its commit (the
|
||||
// commits are also path-limited in meta.rs — belt and braces).
|
||||
match payload {
|
||||
PermPayload::ToolGroups { groups } => {
|
||||
crate::meta::commit_tool_groups(name, groups)
|
||||
.await
|
||||
.with_context(|| format!("commit tool-groups for {name}"))?;
|
||||
coord.emit_tool_groups_snapshot();
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Compute which agents a `nix flake update <inputs>` on the meta
|
||||
/// flake affects — the fan-out set for `MetaUpdate` DAGs. Empty
|
||||
/// `inputs` or any input under `hyperhive` → every container;
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ use hive_jobq::{Graph, NodeId};
|
|||
use hive_jobq_wire::{GraphNode, GraphWire};
|
||||
use tokio::sync::Notify;
|
||||
|
||||
pub use model::{NodeKind, PermPayload, State};
|
||||
pub use model::{NodeKind, State};
|
||||
use resource::Resource;
|
||||
|
||||
/// A job under construction: `hive_jobq`'s builder over this queue's payload
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
//! Data model for the generic job-DAG queue: the node kinds — the primitive
|
||||
//! operations — and what each one carries. The `State` / `PermPayload` wire
|
||||
//! enums live in `hive_host_sock::jobs` (they travel on the host admin
|
||||
//! socket) and are re-exported here for the queue's internal use. The graph
|
||||
//! operations — and what each one carries. The `State` wire
|
||||
//! enum lives in `hive_host_sock::jobs` (it travels on the host admin
|
||||
//! socket) and is re-exported here for the queue's internal use. The graph
|
||||
//! itself is served through `hive_jobq_wire`'s generic projection — there is
|
||||
//! no second, typed view of it any more.
|
||||
//!
|
||||
|
|
@ -12,7 +12,7 @@
|
|||
//! job is exactly the nodes it declared. See `docs/scheduler/coordinator.md::Job queue`
|
||||
//! for the full design.
|
||||
|
||||
pub use hive_host_sock::jobs::{PermPayload, State};
|
||||
pub use hive_host_sock::jobs::State;
|
||||
use serde::Serialize;
|
||||
|
||||
/// The primitive operations — each kind maps to one executor fn in
|
||||
|
|
@ -98,12 +98,10 @@ pub enum NodeKind {
|
|||
PauseDrain { agent: String },
|
||||
/// `set_nspawn_flags` + `set_resource_limits` + daemon-reload.
|
||||
WriteDropin { agent: String },
|
||||
/// Commit `tool-groups.json` per its `payload`.
|
||||
WritePermFile { agent: String, payload: PermPayload },
|
||||
/// Group root of a rebuild subtree — the **brace**, scoped to one
|
||||
/// agent. Why braces exist and what they cost: _Braces_.
|
||||
AgentWindow { agent: String },
|
||||
/// Tail node of a rebuild / perm-change: emit this agent's `Rebuilt`
|
||||
/// Tail node of a rebuild: emit this agent's `Rebuilt`
|
||||
/// manager event. One node per agent *and* per outcome; no cancel
|
||||
/// variant (a cancelled DAG never ran, so there is no rebuild to
|
||||
/// report).
|
||||
|
|
@ -185,7 +183,6 @@ impl NodeKind {
|
|||
| NodeKind::PauseSignal { agent }
|
||||
| NodeKind::PauseDrain { agent }
|
||||
| NodeKind::WriteDropin { agent }
|
||||
| NodeKind::WritePermFile { agent, .. }
|
||||
| NodeKind::AgentWindow { agent }
|
||||
| NodeKind::EmitRebuilt { agent, .. }
|
||||
| NodeKind::SetWanted { agent, .. } => agent,
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@
|
|||
//! `lifecycle::is_running` read), so [`super::power`] assembles them out of
|
||||
//! the primitives this module exports ([`rebuild_nodes`]).
|
||||
|
||||
use super::model::{NodeKind, PermPayload};
|
||||
use super::model::NodeKind;
|
||||
use super::resource::Resource;
|
||||
use super::{Handle, JobBuilder};
|
||||
|
||||
|
|
@ -403,26 +403,6 @@ pub fn destroy(builder: &JobBuilder, agent: &str, purge: bool) {
|
|||
.after_ok(last);
|
||||
}
|
||||
|
||||
/// Perm change: commit the JSON file, then the rebuild subgraph so
|
||||
/// the updated `HIVE_TOOL_GROUPS` env var takes
|
||||
/// effect in the container. Group-roots are `WritePermFile` plus the rebuild
|
||||
/// subgraph's `MetaSync` / `Prebuild` / `Reconcile`, so the `EmitRebuilt` tail
|
||||
/// edges all four.
|
||||
pub fn perm_change(builder: &JobBuilder, agent: &str, payload: PermPayload) {
|
||||
let write = builder
|
||||
.node(NodeKind::WritePermFile {
|
||||
agent: agent.to_owned(),
|
||||
payload,
|
||||
})
|
||||
.needs(Resource::MetaWindow);
|
||||
let roots = rebuild_nodes(builder, agent, true, Some(write));
|
||||
emit_rebuilt_tails(
|
||||
builder,
|
||||
agent,
|
||||
&[write, roots.meta_sync, roots.agent_window, roots.reconcile],
|
||||
);
|
||||
}
|
||||
|
||||
/// Meta-input lock bump. The `MetaLock` executor grows one rebuild subgraph
|
||||
/// per affected agent into *this same* DAG on completion (declared onto the
|
||||
/// builder it was handed) — appended *after* the bump lands so their prebuilds
|
||||
|
|
|
|||
|
|
@ -1515,38 +1515,6 @@ fn destroy_container_must_not_suppress_crash_watch() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn perm_change_shape_prefixes_rebuild_chain() {
|
||||
let q = JobQueue::new(1);
|
||||
insert(&q, |builder| {
|
||||
templates::perm_change(
|
||||
builder,
|
||||
"agent-a",
|
||||
PermPayload::ToolGroups { groups: vec![] },
|
||||
);
|
||||
});
|
||||
assert_eq!(
|
||||
declared_shape(&q)
|
||||
.iter()
|
||||
.map(|d| d.kind)
|
||||
.collect::<Vec<_>>(),
|
||||
vec![
|
||||
"write_perm_file",
|
||||
"meta_sync",
|
||||
"agent_window",
|
||||
"prebuild",
|
||||
"stop_for_update",
|
||||
"swap",
|
||||
"rebuild_bookkeeping",
|
||||
"reconcile",
|
||||
// the ok / !ok tail pair
|
||||
"emit_rebuilt",
|
||||
"emit_rebuilt",
|
||||
],
|
||||
"the perm write prefixes an otherwise ordinary rebuild chain"
|
||||
);
|
||||
}
|
||||
|
||||
// ---- standalone sweeps ----
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use std::path::{Path, PathBuf};
|
|||
|
||||
use anyhow::{Context, Result};
|
||||
use hive_priv_sock::{BindMount, CredentialMount};
|
||||
use hive_sh4re::permissions::Capability;
|
||||
use hive_sh4re::permissions::Permission;
|
||||
|
||||
use crate::coordinator::{AgentPaths, HiveEnv};
|
||||
|
||||
|
|
@ -386,7 +386,7 @@ fn bind_own_agent_dirs(agent_name: &str, notes_dir: &Path, binds: &mut Vec<BindM
|
|||
/// agent starts without the cross-agent mounts, so one corrupt file never
|
||||
/// blocks any agent's start, spawn or swap.
|
||||
fn holds_manage_root_agent(agent_name: &str, path: &Path) -> bool {
|
||||
crate::capabilities::holds(path, agent_name, Capability::ManageRootAgent).unwrap_or_else(|e| {
|
||||
crate::permissions::holds(path, agent_name, Permission::ManageRootAgent).unwrap_or_else(|e| {
|
||||
tracing::error!(
|
||||
agent = %agent_name,
|
||||
path = %path.display(),
|
||||
|
|
@ -567,25 +567,25 @@ async fn set_nspawn_flags(
|
|||
read_only: true,
|
||||
});
|
||||
|
||||
// `ManageRootAgent` capability: mount *every* agent in the hive as a
|
||||
// `ManageRootAgent` permission: mount *every* agent in the hive as a
|
||||
// virtual child. Enables recovery — the holder can update another
|
||||
// agent's config even when that agent is down. Also grants RO access
|
||||
// to /applied and /meta.
|
||||
//
|
||||
// ⚠️ "every agent" is the definition of this capability, not an
|
||||
// ⚠️ "every agent" is the definition of this permission, not an
|
||||
// accident of how the set is computed. The grant used to hang off a
|
||||
// `can_manage_top_level_agents` role and cover `top_level_agents()`
|
||||
// — i.e. `parent.is_none()` — which was "everything outside the
|
||||
// hierarchy". With that hierarchy removed, every agent is
|
||||
// parentless, so that set *was* every agent anyway; the capability
|
||||
// parentless, so that set *was* every agent anyway; the permission
|
||||
// now says so out loud instead of deriving it from a field that no
|
||||
// longer discriminates.
|
||||
//
|
||||
// This is the *only* cross-agent mount left. Dropping the topology
|
||||
// parent field took with it the unconditional grant every
|
||||
// agent used to get over its own direct children — so an agent with
|
||||
// no capability now sees its own dirs and nothing else.
|
||||
if holds_manage_root_agent(agent_name, &crate::capabilities::path()) {
|
||||
// no permission now sees its own dirs and nothing else.
|
||||
if holds_manage_root_agent(agent_name, &crate::permissions::path()) {
|
||||
// Skipping self is a no-op, not a narrowing: `agent_notes_dir` is
|
||||
// `agent_state_dir/state` and `config_bind_source` is shared, so
|
||||
// binding the holder as its own virtual child reproduced the two
|
||||
|
|
@ -819,7 +819,7 @@ mod tests {
|
|||
fn corrupt_swarm_permissions_deny_without_failing() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let path = dir.path().join("swarm-agent-permissions.json");
|
||||
let truncated = "{\n \"ruth\": {\"capabilities\": [\"manage_root_ag";
|
||||
let truncated = "{\n \"ruth\": {\"permissions\": [\"manage_root_ag";
|
||||
std::fs::write(&path, truncated).expect("seed");
|
||||
assert!(!holds_manage_root_agent("ruth", &path));
|
||||
assert_eq!(std::fs::read(&path).expect("read"), truncated.as_bytes());
|
||||
|
|
@ -835,7 +835,7 @@ mod tests {
|
|||
assert!(!holds_manage_root_agent("alice", &path));
|
||||
std::fs::write(
|
||||
&path,
|
||||
r#"{"ruth": {"capabilities": []}, "alice": {"capabilities": ["manage_root_agent"]}}"#,
|
||||
r#"{"ruth": {"permissions": []}, "alice": {"permissions": ["manage_root_agent"]}}"#,
|
||||
)
|
||||
.expect("seed");
|
||||
assert!(!holds_manage_root_agent("ruth", &path));
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ mod swarm_status;
|
|||
mod test_env;
|
||||
mod workers;
|
||||
|
||||
pub(crate) use agent_config::{capabilities, limits, resource_limits, tool_groups, topology};
|
||||
pub(crate) use agent_config::{limits, permissions, resource_limits, topology};
|
||||
pub(crate) use stats::{container_stats, hive_stats, host_stats, otel_metrics, warnings};
|
||||
pub(crate) use stores::{broker, build_logs, db, power};
|
||||
pub(crate) use workers::{agent_sockets, auto_update, crash_watch, knowledge, mcp_sockets};
|
||||
|
|
|
|||
|
|
@ -45,22 +45,14 @@ pub struct AgentSpec {
|
|||
pub port: u16,
|
||||
}
|
||||
|
||||
/// Stage every generated meta JSON file that exists: topology.json is
|
||||
/// regenerated by `reconcile` whenever the agent set changed;
|
||||
/// tool-groups is created lazily on
|
||||
/// first write (`set_groups`) — absent means
|
||||
/// every agent is on defaults, no file needed. Without staging, an
|
||||
/// Stage topology.json if it exists: it is regenerated by `reconcile`
|
||||
/// whenever the agent set changed. Without staging, an
|
||||
/// existing-but-untracked file shows up as untracked in the meta repo,
|
||||
/// which can confuse nix's dirty-tree fetch. `git add` is a no-op when
|
||||
/// content is unchanged.
|
||||
async fn stage_generated_meta_files(dir: &std::path::Path) -> Result<()> {
|
||||
for (path, name) in [
|
||||
(crate::topology::topology_path(), "topology.json"),
|
||||
(crate::tool_groups::tool_groups_path(), "tool-groups.json"),
|
||||
] {
|
||||
if path.exists() {
|
||||
git(dir, &["add", name]).await?;
|
||||
}
|
||||
if crate::topology::topology_path().exists() {
|
||||
git(dir, &["add", "topology.json"]).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -80,7 +72,7 @@ pub async fn sync_agents(hive: &HiveEnv, agents: &[AgentSpec]) -> Result<()> {
|
|||
let dir = crate::paths::meta_root();
|
||||
std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
|
||||
|
||||
let new_flake = render_hive_flake(hive, agents)?;
|
||||
let new_flake = render_hive_flake(hive, agents);
|
||||
let flake_path = dir.join("flake.nix");
|
||||
let on_disk = std::fs::read_to_string(&flake_path).unwrap_or_default();
|
||||
let initial = !dir.join(".git").exists();
|
||||
|
|
@ -193,7 +185,6 @@ pub async fn sync_agents(hive: &HiveEnv, agents: &[AgentSpec]) -> Result<()> {
|
|||
// Only ever a REMOVAL now; see `is_embedded_ca_name`.
|
||||
f if f.starts_with("peer-ca-") && has_pem_ext(f) => Some("peer-ca"),
|
||||
"topology.json" => Some("topology"),
|
||||
"tool-groups.json" => Some("tool-groups"),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
|
|
@ -299,50 +290,35 @@ pub async fn lock_update_hyperhive() -> Result<()> {
|
|||
git_commit_paths(&dir, "bump hyperhive", &["flake.lock"]).await
|
||||
}
|
||||
|
||||
/// Write the tool-groups file for `agent` and commit it atomically
|
||||
/// under `META_LOCK`. Ensures the JSON change is staged + committed
|
||||
/// before the next `sync_agents` runs, so the
|
||||
/// working tree is never left dirty by an untimely `PermChange` write.
|
||||
pub async fn commit_tool_groups(agent: &str, groups: &[String]) -> Result<()> {
|
||||
let _guard = META_LOCK.lock().await;
|
||||
crate::tool_groups::set_groups(agent, groups)?;
|
||||
let dir = crate::paths::meta_root();
|
||||
if crate::tool_groups::tool_groups_path().exists() {
|
||||
git(&dir, &["add", "tool-groups.json"]).await?;
|
||||
}
|
||||
if paths_dirty(&dir, &["tool-groups.json"]).await? {
|
||||
git_commit_paths(
|
||||
&dir,
|
||||
&format!("set tool-groups for {agent}"),
|
||||
&["tool-groups.json"],
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Delete `capabilities.json` from the meta repo and commit the removal under
|
||||
/// `META_LOCK`. Its grants live in the swarm's `agent-permissions` bucket
|
||||
/// Delete the meta repo's permission files and commit the removal under
|
||||
/// `META_LOCK`. Their grants live in the swarm's `agent-permissions` bucket
|
||||
/// once `workers::permissions_import` has sent them.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Removing the file or a git stage/commit step fails.
|
||||
pub async fn remove_capabilities_file() -> Result<()> {
|
||||
const FILE: &str = crate::workers::permissions_import::META_FILE;
|
||||
/// Removing a file or a git stage/commit step fails.
|
||||
pub async fn remove_permission_files() -> Result<()> {
|
||||
let _guard = META_LOCK.lock().await;
|
||||
let dir = crate::paths::meta_root();
|
||||
match std::fs::remove_file(dir.join(FILE)) {
|
||||
Ok(()) => {}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
|
||||
Err(e) => return Err(e).with_context(|| format!("removing {FILE}")),
|
||||
let mut removed = Vec::new();
|
||||
for file in crate::workers::permissions_import::META_FILES {
|
||||
match std::fs::remove_file(dir.join(file)) {
|
||||
Ok(()) => {}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
|
||||
Err(e) => return Err(e).with_context(|| format!("removing {file}")),
|
||||
}
|
||||
git(&dir, &["rm", "--cached", "--ignore-unmatch", "-q", file]).await?;
|
||||
// Only a tracked file has a removal to commit; naming an untracked
|
||||
// one makes `git commit -- <path>` fail.
|
||||
if paths_dirty(&dir, &[file]).await? {
|
||||
removed.push(*file);
|
||||
}
|
||||
}
|
||||
git(&dir, &["rm", "--cached", "--ignore-unmatch", "-q", FILE]).await?;
|
||||
if paths_dirty(&dir, &[FILE]).await? {
|
||||
if !removed.is_empty() {
|
||||
git_commit_paths(
|
||||
&dir,
|
||||
"capabilities moved to the swarm's agent-permissions",
|
||||
&[FILE],
|
||||
"permissions moved to the swarm's agent-permissions",
|
||||
&removed,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
|
@ -351,7 +327,7 @@ pub async fn remove_capabilities_file() -> Result<()> {
|
|||
|
||||
/// [`render_flake`] with its inputs taken from `hive`, as [`sync_agents`]
|
||||
/// renders it.
|
||||
fn render_hive_flake(hive: &HiveEnv, agents: &[AgentSpec]) -> Result<String> {
|
||||
fn render_hive_flake(hive: &HiveEnv, agents: &[AgentSpec]) -> String {
|
||||
render_flake(
|
||||
&hive.hyperhive_flake,
|
||||
&hive.hyperhive_docs_flake,
|
||||
|
|
@ -378,7 +354,7 @@ fn render_flake(
|
|||
operator_pronouns: &str,
|
||||
context_window_tokens: &std::collections::HashMap<String, u64>,
|
||||
agents: &[AgentSpec],
|
||||
) -> Result<String> {
|
||||
) -> String {
|
||||
render_flake_with_lookup(
|
||||
hyperhive_flake,
|
||||
docs_flake,
|
||||
|
|
@ -781,7 +757,7 @@ fn render_flake_with_lookup<F>(
|
|||
context_window_tokens: &std::collections::HashMap<String, u64>,
|
||||
agents: &[AgentSpec],
|
||||
lookup: F,
|
||||
) -> Result<String>
|
||||
) -> String
|
||||
where
|
||||
F: Fn(&str) -> Vec<&'static str>,
|
||||
{
|
||||
|
|
@ -860,7 +836,7 @@ where
|
|||
let pronouns_escaped = operator_pronouns.replace('\\', "\\\\").replace('"', "\\\"");
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" dashboardPort = {dashboard_port};\n operatorPronouns = \"{pronouns_escaped}\";\n mkAgent = {{ name, isManager, port, toolGroups ? null }}:"
|
||||
" dashboardPort = {dashboard_port};\n operatorPronouns = \"{pronouns_escaped}\";\n mkAgent = {{ name, isManager, port }}:"
|
||||
);
|
||||
out.push_str(
|
||||
r#" let
|
||||
|
|
@ -869,7 +845,6 @@ where
|
|||
else hyperhive.nixosConfigurations.agent-base;
|
||||
input = inputs."agent-${name}";
|
||||
service = "hive-agent";
|
||||
toolGroupsEnv = if toolGroups == null then {} else { HIVE_TOOL_GROUPS = toolGroups; };
|
||||
in
|
||||
base.extendModules {
|
||||
modules = [
|
||||
|
|
@ -1046,7 +1021,7 @@ where
|
|||
}
|
||||
out.push_str(
|
||||
r" };
|
||||
systemd.services.${service}.environment = toolGroupsEnv // {
|
||||
systemd.services.${service}.environment = {
|
||||
HIVE_PORT = toString port;
|
||||
HIVE_LABEL = name;
|
||||
HIVE_DASHBOARD_PORT = toString dashboardPort;
|
||||
|
|
@ -1090,34 +1065,18 @@ where
|
|||
nixosConfigurations = {
|
||||
"#,
|
||||
);
|
||||
// Propagated, never read as empty: an agent with no tool-groups entry
|
||||
// renders `toolGroups = null` and gets `AGENT_DEFAULT`, which fails
|
||||
// open for any agent whose explicit entry is narrower.
|
||||
let tool_groups_map = crate::tool_groups::read()?;
|
||||
for spec in agents {
|
||||
// Emit `toolGroups = "group1,group2"` when the operator has
|
||||
// explicitly configured groups for this agent. Absent entry = null
|
||||
// = harness falls back to AGENT_DEFAULT (no env var emitted,
|
||||
// no rebuild cascade for agents whose groups haven't changed).
|
||||
let groups = tool_groups_map.get(&spec.name).cloned().unwrap_or_default();
|
||||
let tool_groups_attr = if groups.is_empty() {
|
||||
"null".to_owned()
|
||||
} else {
|
||||
let joined = groups.join(",");
|
||||
format!("\"{joined}\"")
|
||||
};
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" {} = mkAgent {{ name = \"{}\"; isManager = {}; port = {}; toolGroups = {}; }};",
|
||||
" {} = mkAgent {{ name = \"{}\"; isManager = {}; port = {}; }};",
|
||||
spec.name,
|
||||
spec.name,
|
||||
if spec.is_manager { "true" } else { "false" },
|
||||
spec.port,
|
||||
tool_groups_attr,
|
||||
);
|
||||
}
|
||||
out.push_str(" };\n };\n}\n");
|
||||
Ok(out)
|
||||
out
|
||||
}
|
||||
|
||||
/// Return the list of file names that are currently staged (index differs
|
||||
|
|
@ -1202,7 +1161,7 @@ async fn git_commit(dir: &Path, message: &str) -> Result<()> {
|
|||
|
||||
/// Path-limited commit: commits ONLY the given paths, so unrelated
|
||||
/// staged content can never be swept into someone else's commit. Every targeted
|
||||
/// meta commit (perm files, topology, lock bumps) goes through this;
|
||||
/// meta commit (limits, topology, lock bumps) goes through this;
|
||||
/// only `sync_agents` uses the bare [`git_commit`], because its
|
||||
/// staged set *is* its intentional commit set.
|
||||
async fn git_commit_paths(dir: &Path, message: &str, paths: &[&str]) -> Result<()> {
|
||||
|
|
@ -1293,29 +1252,35 @@ mod tests {
|
|||
git(dir, &["init", "--initial-branch=main"])
|
||||
.await
|
||||
.expect("git init");
|
||||
std::fs::write(dir.join("tool-groups.json"), "{}").expect("write");
|
||||
std::fs::write(dir.join("resource-limits.json"), "{}").expect("write");
|
||||
std::fs::write(dir.join("flake.lock"), "v1").expect("write");
|
||||
git(dir, &["add", "-A"]).await.expect("add");
|
||||
git_commit(dir, "seed").await.expect("seed commit");
|
||||
// Something stages a new lock (uncommitted)…
|
||||
std::fs::write(dir.join("flake.lock"), "v2-staged-by-deploy").expect("write");
|
||||
git(dir, &["add", "flake.lock"]).await.expect("stage lock");
|
||||
// …and a perm change commits, path-limited.
|
||||
std::fs::write(dir.join("tool-groups.json"), r#"{"alice":[]}"#).expect("write");
|
||||
git(dir, &["add", "tool-groups.json"]).await.expect("add");
|
||||
git_commit_paths(dir, "set tool-groups for alice", &["tool-groups.json"])
|
||||
// …and a limits change commits, path-limited.
|
||||
std::fs::write(dir.join("resource-limits.json"), r#"{"alice":[]}"#).expect("write");
|
||||
git(dir, &["add", "resource-limits.json"])
|
||||
.await
|
||||
.expect("path-limited commit");
|
||||
// The perm file is committed; the staged lock is not.
|
||||
.expect("add");
|
||||
git_commit_paths(
|
||||
dir,
|
||||
"set resource limits for alice",
|
||||
&["resource-limits.json"],
|
||||
)
|
||||
.await
|
||||
.expect("path-limited commit");
|
||||
// The limits file is committed; the staged lock is not.
|
||||
assert!(
|
||||
!paths_dirty(dir, &["tool-groups.json"])
|
||||
!paths_dirty(dir, &["resource-limits.json"])
|
||||
.await
|
||||
.expect("check"),
|
||||
"perm file must be committed"
|
||||
"limits file must be committed"
|
||||
);
|
||||
assert!(
|
||||
paths_dirty(dir, &["flake.lock"]).await.expect("check"),
|
||||
"staged deploy lock must survive the perm commit"
|
||||
"staged deploy lock must survive the limits commit"
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -1352,8 +1317,7 @@ mod tests {
|
|||
"she/her",
|
||||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
// nixpkgs is a top-level input with an explicit URL; hyperhive
|
||||
// follows it.
|
||||
assert!(
|
||||
|
|
@ -1399,8 +1363,7 @@ mod tests {
|
|||
"she/her",
|
||||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
assert!(
|
||||
!out.contains("hyperhive-docs"),
|
||||
"no docs input/source when docs_flake is empty:\n{out}"
|
||||
|
|
@ -1418,8 +1381,7 @@ mod tests {
|
|||
"she/her",
|
||||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
assert!(
|
||||
out.contains(r#"networking.hostName = "h-${name}";"#),
|
||||
"an agent container with no hostname of its own is called `nixos`, \
|
||||
|
|
@ -1459,8 +1421,7 @@ mod tests {
|
|||
"she/her",
|
||||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
assert!(
|
||||
out.contains(
|
||||
"services.hyperhive.agent.claudeCodePath = \"/nix/store/cccc-claude-code-2.1.220\";"
|
||||
|
|
@ -1488,8 +1449,7 @@ mod tests {
|
|||
"she/her",
|
||||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
assert!(
|
||||
!out.contains("claudeCodePath"),
|
||||
"no claude assignment when unpinned:\n{out}"
|
||||
|
|
@ -1509,8 +1469,7 @@ mod tests {
|
|||
"she/her",
|
||||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
assert!(
|
||||
out.contains("nixpkgs.follows = \"hyperhive/nixpkgs\""),
|
||||
"expected fallback follows:\n{out}"
|
||||
|
|
@ -1545,8 +1504,7 @@ mod tests {
|
|||
sample_spec("dmatrix", false, 9003),
|
||||
],
|
||||
lookup,
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
// bitburner declares nixpkgs → follows emitted.
|
||||
assert!(
|
||||
out.contains("agent-bitburner.inputs.nixpkgs.follows = \"nixpkgs\""),
|
||||
|
|
@ -1575,8 +1533,7 @@ mod tests {
|
|||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
|_| Vec::new(),
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
// No agent-side follows when the lookup reports nothing
|
||||
// declared — protects agents whose flake.lock can't be read
|
||||
// (missing / unparsable) from being broken by a follows on a
|
||||
|
|
@ -1613,8 +1570,7 @@ mod tests {
|
|||
"she/her",
|
||||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
unsafe {
|
||||
std::env::remove_var("HIVE_FORGE_URL");
|
||||
}
|
||||
|
|
@ -1660,7 +1616,6 @@ mod tests {
|
|||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake")
|
||||
};
|
||||
let alpha = render_on("alpha-hive", "alpha-hive.example.test");
|
||||
let beta = render_on("beta-hive", "beta-hive.example.test");
|
||||
|
|
@ -1840,8 +1795,7 @@ mod tests {
|
|||
"she/her",
|
||||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
unsafe {
|
||||
std::env::remove_var("HIVE_FORGE_URL");
|
||||
std::env::remove_var("HIVE_MATRIX_URL");
|
||||
|
|
@ -1877,8 +1831,7 @@ mod tests {
|
|||
"she/her",
|
||||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake");
|
||||
);
|
||||
let want = format!(
|
||||
"agent-alice.url = \"git+file://{}\"",
|
||||
crate::paths::applied_dir("alice").display()
|
||||
|
|
@ -1923,7 +1876,6 @@ mod tests {
|
|||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake")
|
||||
};
|
||||
|
||||
// A leftover peer-CA file + the env var that names it. Both must be
|
||||
|
|
@ -2016,7 +1968,6 @@ mod tests {
|
|||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake")
|
||||
};
|
||||
unsafe {
|
||||
std::env::remove_var("HYPERHIVE_OTEL_EXTRA_RESOURCE_ATTRIBUTES");
|
||||
|
|
@ -2091,7 +2042,6 @@ mod tests {
|
|||
&std::collections::HashMap::new(),
|
||||
&[sample_spec("alice", false, 9001)],
|
||||
)
|
||||
.expect("render flake")
|
||||
};
|
||||
unsafe {
|
||||
std::env::remove_var("HYPERHIVE_GITHUB_DISABLED");
|
||||
|
|
@ -2123,8 +2073,8 @@ mod tests {
|
|||
..HiveEnv::default()
|
||||
};
|
||||
let agents = [sample_spec("alice", false, 9001)];
|
||||
let small = render_hive_flake(&hive("4G"), &agents).expect("render 4G");
|
||||
let large = render_hive_flake(&hive("16G"), &agents).expect("render 16G");
|
||||
let small = render_hive_flake(&hive("4G"), &agents);
|
||||
let large = render_hive_flake(&hive("16G"), &agents);
|
||||
assert_eq!(
|
||||
small, large,
|
||||
"the rendered flake must not depend on the memory cap"
|
||||
|
|
|
|||
|
|
@ -79,8 +79,8 @@ pub fn start(agent: &str, socket_path: &Path, coord: Arc<Coordinator>) -> Result
|
|||
|
||||
/// Bind + serve the manager socket. This is now **pure transport**: it grants
|
||||
/// no authority of its own — it just serves requests as `agent = MANAGER_AGENT`
|
||||
/// ("ruth"), and ruth's reach comes entirely from the capabilities /
|
||||
/// tool-groups it holds, identical to connecting on a per-agent socket — ruth
|
||||
/// ("ruth"), and ruth's reach comes entirely from the permissions it
|
||||
/// holds, identical to connecting on a per-agent socket — ruth
|
||||
/// uses the standard per-agent runtime dir + socket, with no dedicated helpers.
|
||||
pub fn start_manager(coord: Arc<Coordinator>) -> Result<()> {
|
||||
use std::os::unix::fs::PermissionsExt as _;
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ pub fn spawn(
|
|||
shutdown.clone(),
|
||||
));
|
||||
|
||||
// The capabilities the swarm grants, and the import of the meta repo's.
|
||||
// The permissions the swarm grants, and the import of the meta repo's.
|
||||
tokio::spawn(crate::workers::swarm_permissions::follow(
|
||||
client.clone(),
|
||||
std::sync::Arc::clone(&coord),
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ use anyhow::Result;
|
|||
|
||||
use crate::coordinator::Coordinator;
|
||||
use crate::lifecycle::{self, AGENT_PREFIX, MANAGER_NAME};
|
||||
use crate::tool_groups;
|
||||
|
||||
/// Resolve the current rev of `hyperhive_flake`. For a path on disk we
|
||||
/// canonicalize (following symlinks) so a /etc/hyperhive → /nix/store/...
|
||||
|
|
@ -200,7 +199,6 @@ pub async fn ensure_root_agent(coord: &Arc<Coordinator>) -> Result<()> {
|
|||
let hive = coord.hive_env();
|
||||
let paths = Coordinator::agent_paths(MANAGER_NAME, runtime)?;
|
||||
lifecycle::spawn(MANAGER_NAME, &hive, &paths).await?;
|
||||
seed_manager_tool_groups();
|
||||
if let Err(e) = coord.power.set(MANAGER_NAME, crate::power::Wanted::Up) {
|
||||
tracing::warn!(error = ?e, "agent_power: set manager wanted=up failed");
|
||||
}
|
||||
|
|
@ -212,47 +210,6 @@ pub async fn ensure_root_agent(coord: &Arc<Coordinator>) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Give ruth her privileged tool groups on the one path that creates her.
|
||||
///
|
||||
/// `effective_tool_groups()` has no manager-flavour fallback, so an agent
|
||||
/// with no entry in `tool-groups.json` is an agent with no privileged
|
||||
/// tools. Ruth needs hers from her first turn, and this is the only place
|
||||
/// she is brought into existence — so it is written once, here, rather
|
||||
/// than re-checked on every hive-c0re boot.
|
||||
///
|
||||
/// Skips a name that already has an entry: a destroy+recreate under the
|
||||
/// same name must not silently reset an operator's chosen group set back
|
||||
/// to the default. An unreadable file is logged and left alone: ruth has
|
||||
/// already been spawned, and seeding stays best-effort like the write
|
||||
/// below.
|
||||
fn seed_manager_tool_groups() {
|
||||
match tool_groups::groups_for(MANAGER_NAME) {
|
||||
Ok(groups) if groups.is_empty() => {}
|
||||
Ok(_) => {
|
||||
tracing::debug!("manager tool groups already set — leaving as-is");
|
||||
return;
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
error = ?e,
|
||||
"tool-groups file unreadable — not seeding ruth's tool groups"
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
let all_groups: Vec<String> = hive_sh4re::permissions::ToolGroup::MANAGER_DEFAULT
|
||||
.iter()
|
||||
.map(|g| <&str>::from(*g).to_owned())
|
||||
.collect();
|
||||
match tool_groups::set_groups(MANAGER_NAME, &all_groups) {
|
||||
Ok(()) => tracing::info!("seeded ruth's tool groups to MANAGER_DEFAULT (all groups)"),
|
||||
Err(e) => tracing::warn!(
|
||||
error = ?e,
|
||||
"failed to seed ruth's tool groups — she will start without privileged tools"
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Boot reconcile (see the module doc): classify every agent by rev
|
||||
/// freshness + persisted `wanted` intent, submit one `Boot` DAG
|
||||
/// (hyperhive lock bump growing an in-DAG rebuild subgraph per stale
|
||||
|
|
|
|||
|
|
@ -1,75 +1,146 @@
|
|||
//! The one-shot move of the capabilities this hive's meta repo held
|
||||
//! (`meta/capabilities.json`) into the swarm's `agent-permissions` bucket.
|
||||
//! The one-shot move of the permissions this hive's meta repo held
|
||||
//! (`meta/capabilities.json` and `meta/tool-groups.json`) into the swarm's
|
||||
//! `agent-permissions` bucket, as one list per agent.
|
||||
//!
|
||||
//! At boot, before any agent starts, [`seed_local`] copies the file's grants
|
||||
//! into [`crate::capabilities`] when nothing is stored there yet, so agents
|
||||
//! At boot, before any agent starts, [`seed_local`] copies the files' grants
|
||||
//! into [`crate::permissions`] when nothing is stored there yet, so agents
|
||||
//! keep their mounts until the swarm answers. [`send_pending`] then sends the
|
||||
//! grants to swarm-controller, which writes each agent's key only where there
|
||||
//! is none, and on its answer deletes the file from the meta repo. Nothing
|
||||
//! reads the file after that.
|
||||
//! is none, and on its answer deletes both files from the meta repo. Nothing
|
||||
//! reads them after that.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use hive_sh4re::permissions::Capability;
|
||||
use hive_sh4re::permissions::Permission;
|
||||
use swarm_queue_client::agent_permissions::{
|
||||
self, AgentPermissions, IMPORT_REPLY_DEADLINE, Import, ImportReply,
|
||||
};
|
||||
|
||||
/// The meta repo file the grants are moved out of.
|
||||
pub const META_FILE: &str = "capabilities.json";
|
||||
const CAPABILITIES_FILE: &str = "capabilities.json";
|
||||
const TOOL_GROUPS_FILE: &str = "tool-groups.json";
|
||||
|
||||
fn meta_path() -> PathBuf {
|
||||
crate::paths::meta_root().join(META_FILE)
|
||||
/// The meta repo files the grants are moved out of.
|
||||
pub const META_FILES: &[&str] = &[CAPABILITIES_FILE, TOOL_GROUPS_FILE];
|
||||
|
||||
/// Tool groups the files may name that give nothing and have no
|
||||
/// [`Permission`]: dropped.
|
||||
const EMPTY_TOOL_GROUPS: &[&str] = &["lifecycle", "forge"];
|
||||
|
||||
type Grants = BTreeMap<String, Vec<Permission>>;
|
||||
type FileMap = BTreeMap<String, Vec<String>>;
|
||||
|
||||
fn parse(agent: &str, name: &str) -> Option<Permission> {
|
||||
let found = Permission::ALL
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|p| name.eq_ignore_ascii_case(<&str>::from(*p)));
|
||||
if found.is_none()
|
||||
&& !EMPTY_TOOL_GROUPS
|
||||
.iter()
|
||||
.any(|g| name.eq_ignore_ascii_case(g))
|
||||
{
|
||||
tracing::warn!(%agent, permission = %name, "import: dropping unrecognised name");
|
||||
}
|
||||
found
|
||||
}
|
||||
|
||||
/// The grants the meta file holds, by agent, as the swarm stores them. A name
|
||||
/// that is not a [`Capability`] is dropped, with a warning.
|
||||
/// The one list per agent the two files add up to, as the swarm stores it.
|
||||
/// `None` for either file means it does not exist.
|
||||
///
|
||||
/// `ruth` with no entry in a file that exists had her grant revoked: the file
|
||||
/// was created holding it, and a revoke removed the entry. She is sent an
|
||||
/// empty list, since unset would give her the default back.
|
||||
fn grants(file: BTreeMap<String, Vec<String>>) -> BTreeMap<String, Vec<Capability>> {
|
||||
let mut out: BTreeMap<String, Vec<Capability>> = file
|
||||
.into_iter()
|
||||
.map(|(agent, names)| {
|
||||
let caps = names
|
||||
/// Each half is what the agent held under the old readers:
|
||||
/// - tool groups: the entry's known names; an absent entry, or one naming
|
||||
/// only unknown names, was the default groups. `lifecycle`/`forge` count as
|
||||
/// known (they parsed) and are then dropped, so an entry of only those
|
||||
/// held no tools and imports as none.
|
||||
/// - capabilities: the entry's names. With the file present, `ruth` having no
|
||||
/// entry means her grant was revoked: the file was created holding it, and
|
||||
/// a revoke removed the entry. With no file, the default.
|
||||
///
|
||||
/// An agent whose list equals [`Permission::default_for`] is left out, so its
|
||||
/// key stays unset and follows the default.
|
||||
fn grants(caps: Option<&FileMap>, groups: Option<&FileMap>) -> Grants {
|
||||
let mut agents: BTreeSet<&str> = BTreeSet::new();
|
||||
agents.extend(caps.iter().flat_map(|m| m.keys().map(String::as_str)));
|
||||
agents.extend(groups.iter().flat_map(|m| m.keys().map(String::as_str)));
|
||||
if caps.is_some() {
|
||||
agents.insert(crate::lifecycle::MANAGER_NAME);
|
||||
}
|
||||
let mut out = Grants::new();
|
||||
for agent in agents {
|
||||
let default = Permission::default_for(agent);
|
||||
let tool_default: Vec<Permission> = default
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|p| *p != Permission::ManageRootAgent)
|
||||
.collect();
|
||||
let tool_part = match groups.and_then(|m| m.get(agent)) {
|
||||
Some(names)
|
||||
if names.iter().any(|n| {
|
||||
parse(agent, n).is_some()
|
||||
|| EMPTY_TOOL_GROUPS.iter().any(|g| n.eq_ignore_ascii_case(g))
|
||||
}) =>
|
||||
{
|
||||
names.iter().filter_map(|n| parse(agent, n)).collect()
|
||||
}
|
||||
_ => tool_default,
|
||||
};
|
||||
let cap_part: Vec<Permission> = match caps {
|
||||
Some(m) => m
|
||||
.get(agent)
|
||||
.map(|names| names.iter().filter_map(|n| parse(agent, n)).collect())
|
||||
.unwrap_or_default(),
|
||||
None => default
|
||||
.iter()
|
||||
.filter_map(|name| {
|
||||
let cap = Capability::ALL
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|c| name.eq_ignore_ascii_case(<&str>::from(*c)));
|
||||
if cap.is_none() {
|
||||
tracing::warn!(%agent, capability = %name, "import: dropping unrecognised capability");
|
||||
}
|
||||
cap
|
||||
})
|
||||
.collect();
|
||||
(agent, caps)
|
||||
})
|
||||
.collect();
|
||||
out.entry(crate::lifecycle::MANAGER_NAME.to_owned())
|
||||
.or_default();
|
||||
.copied()
|
||||
.filter(|p| *p == Permission::ManageRootAgent)
|
||||
.collect(),
|
||||
};
|
||||
let mut list: Vec<Permission> = Vec::new();
|
||||
for perm in tool_part.into_iter().chain(cap_part) {
|
||||
if !list.contains(&perm) {
|
||||
list.push(perm);
|
||||
}
|
||||
}
|
||||
let same_as_default =
|
||||
list.len() == default.len() && default.iter().all(|p| list.contains(p));
|
||||
if !same_as_default {
|
||||
out.insert(agent.to_owned(), list);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn read_grants(path: &Path) -> Result<Option<BTreeMap<String, Vec<Capability>>>> {
|
||||
fn read_file(path: &Path) -> Result<Option<FileMap>> {
|
||||
if !path.exists() {
|
||||
return Ok(None);
|
||||
}
|
||||
let file: BTreeMap<String, Vec<String>> = serde_json::from_slice(
|
||||
let map = serde_json::from_slice(
|
||||
&std::fs::read(path).with_context(|| format!("reading {}", path.display()))?,
|
||||
)
|
||||
.with_context(|| format!("parsing {}", path.display()))?;
|
||||
Ok(Some(grants(file)))
|
||||
Ok(Some(map))
|
||||
}
|
||||
|
||||
/// Store the meta file's grants as the last-read swarm permissions, when the
|
||||
/// file is still there and nothing has been stored yet.
|
||||
/// The grants the files in `meta` hold, or `None` when neither exists.
|
||||
fn read_grants(meta: &Path) -> Result<Option<Grants>> {
|
||||
let caps = read_file(&meta.join(CAPABILITIES_FILE))?;
|
||||
let groups = read_file(&meta.join(TOOL_GROUPS_FILE))?;
|
||||
if caps.is_none() && groups.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(Some(grants(caps.as_ref(), groups.as_ref())))
|
||||
}
|
||||
|
||||
fn meta_dir() -> PathBuf {
|
||||
crate::paths::meta_root()
|
||||
}
|
||||
|
||||
/// Store the files' grants as the last-read swarm permissions, when a file
|
||||
/// is still there and nothing has been stored yet.
|
||||
pub fn seed_local() {
|
||||
if let Err(e) = seed_local_at(&meta_path(), &crate::capabilities::path()) {
|
||||
if let Err(e) = seed_local_at(&meta_dir(), &crate::permissions::path()) {
|
||||
tracing::warn!(error = ?e, "import: seeding the stored permissions from the meta repo failed");
|
||||
}
|
||||
}
|
||||
|
|
@ -83,39 +154,71 @@ fn seed_local_at(meta: &Path, stored: &Path) -> Result<()> {
|
|||
};
|
||||
let records = grants
|
||||
.into_iter()
|
||||
.map(|(agent, caps)| {
|
||||
.map(|(agent, perms)| {
|
||||
(
|
||||
agent,
|
||||
AgentPermissions {
|
||||
capabilities: Some(caps),
|
||||
permissions: Some(perms),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
crate::capabilities::write_at(stored, &records).context("storing the seeded permissions")?;
|
||||
tracing::info!("import: seeded the stored permissions from the meta repo's capabilities");
|
||||
crate::permissions::write_at(stored, &records).context("storing the seeded permissions")?;
|
||||
tracing::info!("import: seeded the stored permissions from the meta repo");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Send the meta file's grants to swarm-controller while the file is there,
|
||||
/// and delete it once the controller has answered that it handled them.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// The file is still there: the import failed, or deleting the file did. The
|
||||
/// next pass sends again; the controller keeps a key it already has.
|
||||
pub async fn send_pending(client: &async_nats::Client, hive: &str) -> Result<()> {
|
||||
send_at(client, hive, &meta_path())
|
||||
.await
|
||||
.context("importing the meta repo's capabilities")
|
||||
/// What one attempt to send the import came to.
|
||||
#[derive(Debug)]
|
||||
pub enum ImportOutcome {
|
||||
/// No meta file is left: nothing to import.
|
||||
NothingPending,
|
||||
/// The controller took the grants and the files are deleted.
|
||||
Imported,
|
||||
/// The controller answered with a refusal; the files stay.
|
||||
Refused(String),
|
||||
/// No answer (the controller or the queue is down), or a file could not
|
||||
/// be read or deleted; the files stay.
|
||||
Unsettled(anyhow::Error),
|
||||
}
|
||||
|
||||
async fn send_at(client: &async_nats::Client, hive: &str, path: &Path) -> Result<()> {
|
||||
let Some(capabilities) = read_grants(path)? else {
|
||||
return Ok(());
|
||||
impl ImportOutcome {
|
||||
/// Whether the swarm's keys may be read after this outcome. While a file
|
||||
/// is left, an unset key may only mean its grants are not imported yet,
|
||||
/// and reading it as the default would undo them.
|
||||
#[must_use]
|
||||
pub fn read_allowed(&self) -> bool {
|
||||
matches!(self, Self::NothingPending | Self::Imported)
|
||||
}
|
||||
|
||||
/// Why reading is not allowed, for the caller's log and banner.
|
||||
#[must_use]
|
||||
pub fn into_error(self) -> Option<anyhow::Error> {
|
||||
match self {
|
||||
Self::NothingPending | Self::Imported => None,
|
||||
Self::Refused(why) => Some(anyhow::anyhow!("the controller refused the import: {why}")),
|
||||
Self::Unsettled(e) => Some(e.context("importing the meta repo's permissions")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Send the files' grants to swarm-controller while a file is there, and
|
||||
/// delete both once the controller has answered that it handled them. The
|
||||
/// next pass sends again after anything but [`ImportOutcome::Imported`]; the
|
||||
/// controller keeps a key it already has.
|
||||
pub async fn send_pending(client: &async_nats::Client, hive: &str) -> ImportOutcome {
|
||||
match send_at(client, hive, &meta_dir()).await {
|
||||
Ok(outcome) => outcome,
|
||||
Err(e) => ImportOutcome::Unsettled(e),
|
||||
}
|
||||
}
|
||||
|
||||
async fn send_at(client: &async_nats::Client, hive: &str, meta: &Path) -> Result<ImportOutcome> {
|
||||
let Some(permissions) = read_grants(meta)? else {
|
||||
return Ok(ImportOutcome::NothingPending);
|
||||
};
|
||||
swarm_queue_client::ensure_connected(client)?;
|
||||
let payload = serde_json::to_vec(&Import { capabilities })?;
|
||||
let payload = serde_json::to_vec(&Import { permissions })?;
|
||||
let reply = tokio::time::timeout(
|
||||
IMPORT_REPLY_DEADLINE,
|
||||
client.request(agent_permissions::import_subject(hive), payload.into()),
|
||||
|
|
@ -133,65 +236,96 @@ async fn send_at(client: &async_nats::Client, hive: &str, path: &Path) -> Result
|
|||
?written,
|
||||
?kept,
|
||||
?skipped,
|
||||
"import: the controller took the meta repo's capabilities"
|
||||
"import: the controller took the meta repo's permissions"
|
||||
);
|
||||
crate::meta::remove_capabilities_file()
|
||||
crate::meta::remove_permission_files()
|
||||
.await
|
||||
.context("deleting the imported capabilities file")
|
||||
}
|
||||
ImportReply::Failed { error } => {
|
||||
anyhow::bail!("the controller refused the import: {error}")
|
||||
.context("deleting the imported permission files")?;
|
||||
Ok(ImportOutcome::Imported)
|
||||
}
|
||||
ImportReply::Failed { error } => Ok(ImportOutcome::Refused(error)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
use super::{FileMap, grants, seed_local_at};
|
||||
use hive_sh4re::permissions::Permission::{self, *};
|
||||
|
||||
use super::{grants, seed_local_at};
|
||||
use hive_sh4re::permissions::Capability;
|
||||
|
||||
fn file(json: &str) -> BTreeMap<String, Vec<String>> {
|
||||
fn file(json: &str) -> FileMap {
|
||||
serde_json::from_str(json).expect("decodes")
|
||||
}
|
||||
|
||||
/// Both files add up per agent; the empty groups and unknown names drop;
|
||||
/// an agent left on the default is not sent.
|
||||
#[test]
|
||||
fn grants_keep_known_names_and_a_revoked_ruth_stays_revoked() {
|
||||
let out = grants(file(
|
||||
r#"{"atlas": ["manage_root_agent", "fly_to_the_moon"], "iris": []}"#,
|
||||
));
|
||||
assert_eq!(out["atlas"], vec![Capability::ManageRootAgent]);
|
||||
assert_eq!(out["iris"], Vec::<Capability>::new());
|
||||
assert_eq!(out["ruth"], Vec::<Capability>::new());
|
||||
|
||||
let out = grants(file(r#"{"ruth": ["manage_root_agent"]}"#));
|
||||
assert_eq!(out["ruth"], vec![Capability::ManageRootAgent]);
|
||||
fn the_two_files_merge_into_one_list() {
|
||||
let caps = file(r#"{"ruth": ["manage_root_agent"], "atlas": ["manage_root_agent"]}"#);
|
||||
let groups = file(
|
||||
r#"{"ruth": ["messaging", "meta", "inbox", "lifecycle", "execution"],
|
||||
"atlas": ["messaging", "web_tools", "forge"],
|
||||
"iris": ["messaging", "fly_to_the_moon"],
|
||||
"sock": ["lifecycle"]}"#,
|
||||
);
|
||||
let out = grants(Some(&caps), Some(&groups));
|
||||
assert!(!out.contains_key("ruth"), "ruth's seed is her default");
|
||||
assert_eq!(out["atlas"], vec![Messaging, WebTools, ManageRootAgent]);
|
||||
assert_eq!(out["iris"], vec![Messaging]);
|
||||
assert_eq!(out["sock"], Vec::<Permission>::new());
|
||||
}
|
||||
|
||||
/// The seed writes once, from the file, and never over a stored read.
|
||||
/// Without a capabilities entry, ruth's existing file means a revoke; with
|
||||
/// no capabilities file at all, her default stands.
|
||||
#[test]
|
||||
fn the_seed_copies_the_file_only_when_nothing_is_stored() {
|
||||
fn a_revoked_ruth_stays_revoked() {
|
||||
let out = grants(Some(&file("{}")), None);
|
||||
assert_eq!(out["ruth"], vec![Messaging, Meta, Inbox, Execution]);
|
||||
|
||||
let out = grants(None, Some(&file(r#"{"atlas": ["messaging"]}"#)));
|
||||
assert!(!out.contains_key("ruth"));
|
||||
assert_eq!(out["atlas"], vec![Messaging]);
|
||||
}
|
||||
|
||||
/// The seed writes once, from the files, and never over a stored read.
|
||||
#[test]
|
||||
fn the_seed_copies_the_files_only_when_nothing_is_stored() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let meta = dir.path().join("capabilities.json");
|
||||
let meta = dir.path();
|
||||
let stored = dir.path().join("stored.json");
|
||||
|
||||
seed_local_at(&meta, &stored).expect("no file is no seed");
|
||||
seed_local_at(meta, &stored).expect("no file is no seed");
|
||||
assert!(!stored.exists());
|
||||
|
||||
std::fs::write(&meta, r#"{"atlas": ["manage_root_agent"]}"#).expect("seed");
|
||||
seed_local_at(&meta, &stored).expect("seeded");
|
||||
assert!(
|
||||
crate::capabilities::holds(&stored, "atlas", Capability::ManageRootAgent)
|
||||
.expect("read")
|
||||
);
|
||||
assert!(
|
||||
!crate::capabilities::holds(&stored, "ruth", Capability::ManageRootAgent)
|
||||
.expect("read")
|
||||
);
|
||||
std::fs::write(
|
||||
meta.join("capabilities.json"),
|
||||
r#"{"atlas": ["manage_root_agent"]}"#,
|
||||
)
|
||||
.expect("seed");
|
||||
seed_local_at(meta, &stored).expect("seeded");
|
||||
assert!(crate::permissions::holds(&stored, "atlas", ManageRootAgent).expect("read"));
|
||||
assert!(!crate::permissions::holds(&stored, "ruth", ManageRootAgent).expect("read"));
|
||||
|
||||
std::fs::write(&stored, "{}").expect("a later read");
|
||||
seed_local_at(&meta, &stored).expect("left alone");
|
||||
seed_local_at(meta, &stored).expect("left alone");
|
||||
assert_eq!(std::fs::read_to_string(&stored).expect("read"), "{}");
|
||||
}
|
||||
|
||||
/// The read gate, one row per outcome: only a settled import lets the
|
||||
/// swarm's keys be read.
|
||||
#[test]
|
||||
fn keys_are_read_only_once_the_import_is_settled() {
|
||||
use super::ImportOutcome;
|
||||
for (outcome, allowed) in [
|
||||
(ImportOutcome::NothingPending, true),
|
||||
(ImportOutcome::Imported, true),
|
||||
(ImportOutcome::Refused("not a hive".to_owned()), false),
|
||||
(
|
||||
ImportOutcome::Unsettled(anyhow::anyhow!("no responders")),
|
||||
false,
|
||||
),
|
||||
] {
|
||||
assert_eq!(outcome.read_allowed(), allowed, "{outcome:?}");
|
||||
assert_eq!(outcome.into_error().is_none(), allowed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,26 @@
|
|||
//! Reading the capabilities the swarm grants this hive's agents.
|
||||
//! Reading the permissions the swarm grants this hive's agents.
|
||||
//!
|
||||
//! swarm-controller keeps one key per agent in the `agent-permissions` bucket
|
||||
//! ([`swarm_queue_client::agent_permissions`]). Every [`READ_INTERVAL`] this
|
||||
//! reads the key of each agent this hive has and stores what it finds with
|
||||
//! [`crate::capabilities`], where the container's bind mounts are worked out.
|
||||
//! Those are fixed when the container starts, so an agent whose capabilities
|
||||
//! changed is restarted, after its current turn.
|
||||
//! [`crate::permissions`], where the container's bind mounts are worked out.
|
||||
//! Those are fixed when the container starts, so an agent whose
|
||||
//! `manage_root_agent` changed is restarted, after its current turn. The
|
||||
//! agent applies the rest of its permissions itself.
|
||||
//!
|
||||
//! A failed read, or a bucket that does not answer, leaves the stored
|
||||
//! permissions as they are and restarts nothing: dropping them would put an
|
||||
//! agent back on its default, which can be wider than what the swarm set.
|
||||
//! permissions as they are and restarts nothing.
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use hive_sh4re::permissions::Permission;
|
||||
use swarm_queue_client::agent_permissions::{self, AgentPermissions, Read};
|
||||
|
||||
use crate::capabilities::{self, Stored};
|
||||
use crate::coordinator::Coordinator;
|
||||
use crate::permissions::{self, Stored};
|
||||
use crate::stats::sweep_health::{self, SweepHealth};
|
||||
|
||||
/// How often the permissions are read.
|
||||
|
|
@ -38,11 +39,13 @@ pub async fn follow(
|
|||
) {
|
||||
let mut health = SweepHealth::new("swarm_permissions_read", "warn", FAILURES_BEFORE_BANNER);
|
||||
loop {
|
||||
// While the meta repo's grants are not in the swarm yet, an unset key
|
||||
// is not the swarm's answer, so nothing is read until they are.
|
||||
let pass = match super::permissions_import::send_pending(&client, &hive).await {
|
||||
Ok(()) => sync(&client).await,
|
||||
Err(e) => Err(e),
|
||||
let import = super::permissions_import::send_pending(&client, &hive).await;
|
||||
let pass = if import.read_allowed() {
|
||||
sync(&client).await
|
||||
} else {
|
||||
Err(import
|
||||
.into_error()
|
||||
.unwrap_or_else(|| anyhow::anyhow!("import unsettled")))
|
||||
};
|
||||
match pass {
|
||||
Ok(changed) => {
|
||||
|
|
@ -76,7 +79,7 @@ pub async fn follow(
|
|||
}
|
||||
|
||||
/// Read every agent's key and store the result. Returns the agents whose
|
||||
/// capabilities changed.
|
||||
/// `manage_root_agent` changed.
|
||||
async fn sync(client: &async_nats::Client) -> Result<Vec<String>> {
|
||||
// An unconnected client does not fail a JetStream request, it hangs on it.
|
||||
swarm_queue_client::ensure_connected(client)?;
|
||||
|
|
@ -86,7 +89,7 @@ async fn sync(client: &async_nats::Client) -> Result<Vec<String>> {
|
|||
.into_iter()
|
||||
.map(|spec| spec.name)
|
||||
.collect();
|
||||
sync_with(&agents, &capabilities::path(), |agent| async move {
|
||||
sync_with(&agents, &permissions::path(), |agent| async move {
|
||||
agent_permissions::read(client, &agent)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!(e))
|
||||
|
|
@ -116,24 +119,26 @@ where
|
|||
}
|
||||
}
|
||||
}
|
||||
let stored = capabilities::read_at(path).context("reading the stored swarm permissions")?;
|
||||
let stored = permissions::read_at(path).context("reading the stored swarm permissions")?;
|
||||
if read_now == stored {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
capabilities::write_at(path, &read_now).context("storing the swarm permissions")?;
|
||||
permissions::write_at(path, &read_now).context("storing the swarm permissions")?;
|
||||
tracing::info!(permissions = ?read_now, "swarm permissions: stored the swarm's permissions");
|
||||
Ok(agents
|
||||
.iter()
|
||||
.filter(|a| {
|
||||
capabilities::effective(a, stored.get(*a))
|
||||
!= capabilities::effective(a, read_now.get(*a))
|
||||
})
|
||||
.filter(|a| manages(a, &stored) != manages(a, &read_now))
|
||||
.cloned()
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Whether `agent` holds the one permission hive-c0re enforces under `stored`.
|
||||
fn manages(agent: &str, stored: &Stored) -> bool {
|
||||
permissions::effective(agent, stored.get(agent)).contains(&Permission::ManageRootAgent)
|
||||
}
|
||||
|
||||
/// Restart each running agent in `changed` once its current turn is done, so
|
||||
/// its container starts with the bind mounts its new capabilities give. A
|
||||
/// its container starts with the bind mounts its new permissions give. A
|
||||
/// stopped agent gets them at its next start.
|
||||
async fn restart(coord: &Arc<Coordinator>, changed: Vec<String>) {
|
||||
let mut running = Vec::new();
|
||||
|
|
@ -145,7 +150,7 @@ async fn restart(coord: &Arc<Coordinator>, changed: Vec<String>) {
|
|||
if running.is_empty() {
|
||||
return;
|
||||
}
|
||||
tracing::info!(agents = ?running, "swarm permissions: capabilities changed, restarting");
|
||||
tracing::info!(agents = ?running, "swarm permissions: manage_root_agent changed, restarting");
|
||||
if let Err(e) = crate::job_queue::power::restart_many(coord, &running, true).await {
|
||||
tracing::error!(agents = ?running, error = ?e, "swarm permissions: restart insert failed");
|
||||
}
|
||||
|
|
@ -188,16 +193,12 @@ mod tests {
|
|||
.await
|
||||
.expect("read");
|
||||
assert!(changed.is_empty());
|
||||
assert!(crate::permissions::read_at(&path).expect("read").is_empty());
|
||||
assert!(
|
||||
crate::capabilities::read_at(&path)
|
||||
.expect("read")
|
||||
.is_empty()
|
||||
);
|
||||
assert!(
|
||||
crate::capabilities::holds(
|
||||
crate::permissions::holds(
|
||||
&path,
|
||||
"ruth",
|
||||
hive_sh4re::permissions::Capability::ManageRootAgent
|
||||
hive_sh4re::permissions::Permission::ManageRootAgent
|
||||
)
|
||||
.expect("read")
|
||||
);
|
||||
|
|
@ -206,14 +207,14 @@ mod tests {
|
|||
/// Revoking ruth's default, and granting atlas, restarts both; clearing
|
||||
/// them again restarts both back.
|
||||
#[tokio::test]
|
||||
async fn a_changed_capability_asks_for_a_restart() {
|
||||
async fn a_changed_manage_root_agent_asks_for_a_restart() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let path = dir.path().join("p.json");
|
||||
let changed = sync(&path, |agent| {
|
||||
Ok(Read::Current(record(if agent == "ruth" {
|
||||
r#"{"capabilities":[]}"#
|
||||
r#"{"permissions":[]}"#
|
||||
} else {
|
||||
r#"{"capabilities":["manage_root_agent"]}"#
|
||||
r#"{"permissions":["manage_root_agent"]}"#
|
||||
})))
|
||||
})
|
||||
.await
|
||||
|
|
@ -223,9 +224,9 @@ mod tests {
|
|||
// The same answer again changes nothing.
|
||||
let unchanged = sync(&path, |agent| {
|
||||
Ok(Read::Current(record(if agent == "ruth" {
|
||||
r#"{"capabilities":[]}"#
|
||||
r#"{"permissions":[]}"#
|
||||
} else {
|
||||
r#"{"capabilities":["manage_root_agent"]}"#
|
||||
r#"{"permissions":["manage_root_agent"]}"#
|
||||
})))
|
||||
})
|
||||
.await
|
||||
|
|
@ -238,37 +239,37 @@ mod tests {
|
|||
assert_eq!(cleared, agents());
|
||||
}
|
||||
|
||||
/// Setting ruth's default explicitly is a different record but the same
|
||||
/// mounts, so it is stored and restarts nothing.
|
||||
/// A change that leaves `manage_root_agent` where it was is stored and
|
||||
/// restarts nothing: the agent applies tool permissions itself.
|
||||
#[tokio::test]
|
||||
async fn an_explicit_default_is_stored_without_a_restart() {
|
||||
async fn a_tool_only_change_is_stored_without_a_restart() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let path = dir.path().join("p.json");
|
||||
let changed = sync(&path, |agent| {
|
||||
Ok(Read::Current(if agent == "ruth" {
|
||||
record(r#"{"capabilities":["manage_root_agent"]}"#)
|
||||
record(r#"{"permissions":["manage_root_agent"]}"#)
|
||||
} else {
|
||||
AgentPermissions::default()
|
||||
record(r#"{"permissions":["messaging","web_tools"]}"#)
|
||||
}))
|
||||
})
|
||||
.await
|
||||
.expect("read");
|
||||
assert!(changed.is_empty());
|
||||
assert!(
|
||||
crate::capabilities::read_at(&path)
|
||||
crate::permissions::read_at(&path)
|
||||
.expect("read")
|
||||
.contains_key("ruth")
|
||||
.contains_key("atlas")
|
||||
);
|
||||
}
|
||||
|
||||
/// One failed read, or a bucket that does not answer, keeps what was
|
||||
/// stored and restarts nothing: never a fall back to the default.
|
||||
/// stored and restarts nothing.
|
||||
#[tokio::test]
|
||||
async fn a_failed_read_keeps_the_last_value() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let path = dir.path().join("p.json");
|
||||
let revoked = || Ok(Read::Current(record(r#"{"capabilities":[]}"#)));
|
||||
// An empty list is atlas's default already, so only ruth changes.
|
||||
let revoked = || Ok(Read::Current(record(r#"{"permissions":[]}"#)));
|
||||
// atlas holds no manage_root_agent either way, so only ruth changes.
|
||||
assert_eq!(
|
||||
sync(&path, |_| revoked()).await.expect("read"),
|
||||
vec!["ruth".to_owned()]
|
||||
|
|
|
|||
Loading…
Reference in a new issue