huskies: merge 1163 story Replace perm_rx lock-as-presence-signal with a permission router

This commit is contained in:
Huskies Agent
2026-07-16 13:19:20 +00:00
parent 6f8a8ffd87
commit 18b065f77a
26 changed files with 823 additions and 293 deletions
+14 -11
View File
@@ -7,12 +7,11 @@
use crate::agents::AgentPool;
use crate::chat::dispatcher::ChatDispatcher;
use crate::http::context::{PermissionDecision, PermissionForward};
use crate::service::permission_router::{PendingPermReplies, ResponderRegistry};
use crate::service::status::StatusBroadcaster;
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::{Mutex as TokioMutex, mpsc, oneshot};
/// Shared state bundle constructed once at startup and cloned (via `Arc`) into
/// every context that needs access to the project root, agent pool, bot
@@ -29,11 +28,16 @@ pub struct Services {
pub bot_user_id: String,
/// Set of room/channel IDs where ambient mode is active.
pub ambient_rooms: Arc<std::sync::Mutex<HashSet<String>>>,
/// Receiver for permission requests from the MCP `prompt_permission` tool.
pub perm_rx: Arc<TokioMutex<mpsc::UnboundedReceiver<PermissionForward>>>,
/// Per-room pending permission reply senders, keyed by room/channel ID
/// as a plain string.
pub pending_perm_replies: Arc<TokioMutex<HashMap<String, oneshot::Sender<PermissionDecision>>>>,
/// Registry of tasks currently registered to receive forwarded MCP
/// `prompt_permission` requests (Matrix bot, sled uplink, WS chat
/// sessions, per-message chat transports). Replaces holding `perm_rx`'s
/// mutex as a presence signal.
pub permission_registry: Arc<ResponderRegistry>,
/// Pending permission replies awaiting a plain-language (yes/no) chat
/// reply, keyed by `request_id` with a per-location FIFO index so two
/// concurrent requests for the same room/sender/channel don't drop each
/// other's oneshot sender.
pub pending_perm_replies: Arc<PendingPermReplies>,
/// Seconds to wait for a user to respond to a permission prompt before
/// auto-denying (fail-closed).
pub permission_timeout_secs: u64,
@@ -58,7 +62,6 @@ impl Services {
/// Build a minimal `Services` for testing with the given project root and
/// bot display name.
pub fn new_test(project_root: std::path::PathBuf, bot_name: String) -> std::sync::Arc<Self> {
let (_perm_tx, perm_rx) = mpsc::unbounded_channel();
let agents = std::sync::Arc::new(crate::agents::AgentPool::new_test(3000));
std::sync::Arc::new(Self {
project_root,
@@ -67,8 +70,8 @@ impl Services {
bot_name,
bot_user_id: String::new(),
ambient_rooms: std::sync::Arc::new(std::sync::Mutex::new(HashSet::new())),
perm_rx: std::sync::Arc::new(TokioMutex::new(perm_rx)),
pending_perm_replies: std::sync::Arc::new(TokioMutex::new(HashMap::new())),
permission_registry: ResponderRegistry::new(),
pending_perm_replies: PendingPermReplies::new(),
permission_timeout_secs: 120,
chat_dispatcher: std::sync::Arc::new(ChatDispatcher::new(1_500)),
})