Skip to main content

subc_daemon/
operator_confirm.rs

1//! Operator confirmation policy and its blocking provider boundary.
2//!
3//! The forwarding lock precedes the state lock. Providers and deliveries never
4//! acquire the forwarding lock, and neither can hold up the policy timers.
5use std::{
6    collections::{HashMap, VecDeque},
7    path::PathBuf,
8    sync::{Arc, Mutex},
9    time::Duration,
10};
11
12use subc_protocol::{
13    error_codes,
14    session::{OperatorConfirmReply, OperatorConfirmRequest},
15    ErrorBody, Flags, FrameType, Principal, Priority,
16};
17use tokio::{
18    sync::{oneshot, Notify},
19    time::{timeout_at, Instant},
20};
21
22use crate::{
23    caller_identity::CallerIdentity,
24    forwarding::{ModuleEndpointId, ModuleRouteKey, RouteBinding},
25    router::RouteCtx,
26    ConnectionId, Frame,
27};
28
29mod shortcut;
30pub(crate) use shortcut::ShortcutContext;
31
32/// Person declines of privileged client requests that start a cooldown when
33/// they all fall within `DECLINE_WINDOW` of the newest one.
34const DECLINE_LIMIT: usize = 3;
35const DECLINE_WINDOW: Duration = Duration::from_secs(10 * 60);
36/// Measured from the decline that started the cooldown. Refusals during the
37/// cooldown are not declines by a person, so they never extend it.
38const DECLINE_COOLDOWN: Duration = Duration::from_secs(10 * 60);
39
40/// Monotonic time for the repeated-decline cooldown. Tests replace it so the
41/// ten-minute window and cooldown can be crossed without waiting.
42pub(crate) type CooldownClock = Arc<dyn Fn() -> Instant + Send + Sync>;
43
44/// Who repeated person declines of privileged requests are counted against.
45/// The executable path keeps a reconnecting program under its cooldown; a
46/// caller whose executable could not be resolved is counted per connection,
47/// because nothing else the daemon owns ties its connections together.
48#[derive(Clone, Debug, PartialEq, Eq, Hash)]
49pub(crate) enum DeclineKey {
50    Executable(PathBuf),
51    Connection(ConnectionId),
52}
53
54impl DeclineKey {
55    pub(crate) fn for_caller(caller: &CallerIdentity, connection: ConnectionId) -> Self {
56        match caller.executable() {
57            Some(executable) => Self::Executable(executable.to_path_buf()),
58            None => Self::Connection(connection),
59        }
60    }
61}
62
63impl std::fmt::Display for DeclineKey {
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        match self {
66            Self::Executable(path) => write!(f, "{}", path.display()),
67            Self::Connection(connection) => write!(
68                f,
69                "an unidentified local process on connection {}",
70                connection.get()
71            ),
72        }
73    }
74}
75
76/// A blocking provider publishes a withdraw handle as soon as one exists, then
77/// returns only when its prompt has ended. Publishing is allowed to be delayed:
78/// the daemon's timeout also covers setup and a delayed handle is still withdrawn.
79pub trait OperatorProvider: Send + Sync + 'static {
80    /// Whether this provider can attempt strict interactive configuration
81    /// presence. Readiness is not approval; the prompt result is authoritative.
82    fn config_gate_ready(&self) -> bool {
83        false
84    }
85    fn prompt(
86        &self,
87        text: &str,
88        publish: Box<dyn FnOnce(Arc<dyn OperatorWithdraw>) + Send>,
89    ) -> ProviderResult;
90
91    /// Configuration approval requires an interactive response. Providers that
92    /// distinguish policy grants override this instead of reusing legacy approval.
93    fn prompt_gate(
94        &self,
95        text: &str,
96        publish: Box<dyn FnOnce(Arc<dyn OperatorWithdraw>) + Send>,
97    ) -> ProviderResult {
98        self.prompt(text, publish)
99    }
100}
101
102/// Called on a blocking thread, never while a daemon lock is held.
103pub trait OperatorWithdraw: Send + Sync + 'static {
104    fn withdraw(&self, reason: &str);
105}
106
107#[derive(Debug, Clone, PartialEq, Eq)]
108pub enum ProviderResult {
109    Approved,
110    Declined,
111    Unavailable,
112    ProviderError {
113        reason: &'static str,
114    },
115    UnsupportedPlatform,
116    NoPresence,
117    SetupRequired,
118    LocalAuthenticationError {
119        failure: OperatorOsErrorKind,
120        domain: String,
121        code: isize,
122    },
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126pub enum OperatorOsErrorKind {
127    Declined,
128    NoPresence,
129    ProviderError,
130}
131
132#[derive(Debug, Clone, Copy)]
133pub struct OperatorLimits {
134    pub queue_wait: Duration,
135    pub prompt_timeout: Duration,
136    pub stuck_grace: Duration,
137}
138
139impl Default for OperatorLimits {
140    fn default() -> Self {
141        Self {
142            queue_wait: Duration::from_secs(150),
143            prompt_timeout: Duration::from_secs(120),
144            stuck_grace: Duration::from_secs(10),
145        }
146    }
147}
148
149impl OperatorLimits {
150    fn delivery_budget(self) -> Duration {
151        self.queue_wait + self.prompt_timeout + self.stuck_grace + Duration::from_secs(10)
152    }
153}
154
155struct OsProvider;
156
157#[cfg(any(target_os = "macos", windows, target_os = "linux"))]
158struct OsWithdraw(Arc<dyn subc_presence::Withdraw>);
159#[cfg(any(target_os = "macos", windows, target_os = "linux"))]
160impl OperatorWithdraw for OsWithdraw {
161    fn withdraw(&self, _: &str) {
162        self.0.withdraw();
163    }
164}
165
166impl OperatorProvider for OsProvider {
167    fn config_gate_ready(&self) -> bool {
168        cfg!(any(target_os = "macos", windows, target_os = "linux"))
169    }
170    fn prompt_gate(
171        &self,
172        text: &str,
173        publish: Box<dyn FnOnce(Arc<dyn OperatorWithdraw>) + Send>,
174    ) -> ProviderResult {
175        #[cfg(any(target_os = "macos", windows, target_os = "linux"))]
176        {
177            gate_os_outcome(subc_presence::prompt(
178                text,
179                Box::new(move |handle| publish(Arc::new(OsWithdraw(handle)))),
180            ))
181        }
182        #[cfg(not(any(target_os = "macos", windows, target_os = "linux")))]
183        {
184            let _ = (text, publish);
185            ProviderResult::Unavailable
186        }
187    }
188
189    fn prompt(
190        &self,
191        text: &str,
192        publish: Box<dyn FnOnce(Arc<dyn OperatorWithdraw>) + Send>,
193    ) -> ProviderResult {
194        #[cfg(any(target_os = "macos", windows, target_os = "linux"))]
195        {
196            os_outcome(subc_presence::prompt(
197                text,
198                Box::new(move |handle| publish(Arc::new(OsWithdraw(handle)))),
199            ))
200        }
201        #[cfg(not(any(target_os = "macos", windows, target_os = "linux")))]
202        {
203            let _ = (text, publish);
204            ProviderResult::Unavailable
205        }
206    }
207}
208
209/// Convert the operating-system prompt's result into the daemon's provider
210/// result. Legacy prompts accept an OS policy-rule grant even without an
211/// interactive prompt. Keeping this separate from the platform bindings lets
212/// tests on every host verify that errors and unavailable providers never
213/// grant a write.
214#[cfg(any(target_os = "macos", windows, target_os = "linux"))]
215fn os_outcome(outcome: subc_presence::Outcome) -> ProviderResult {
216    match outcome {
217        subc_presence::Outcome::Approved => ProviderResult::Approved,
218        subc_presence::Outcome::AuthorizedWithoutPrompt => ProviderResult::Approved,
219        subc_presence::Outcome::Declined => ProviderResult::Declined,
220        subc_presence::Outcome::ProviderError => ProviderResult::Unavailable,
221        subc_presence::Outcome::ProviderErrorWithReason { reason } => {
222            ProviderResult::ProviderError { reason }
223        }
224        subc_presence::Outcome::NoPresence => ProviderResult::NoPresence,
225        subc_presence::Outcome::SetupRequired => ProviderResult::SetupRequired,
226        subc_presence::Outcome::UnsupportedPlatform => ProviderResult::UnsupportedPlatform,
227        subc_presence::Outcome::LocalAuthenticationError {
228            failure,
229            domain,
230            code,
231        } => ProviderResult::LocalAuthenticationError {
232            failure: match failure {
233                subc_presence::LocalAuthenticationFailure::Declined => {
234                    OperatorOsErrorKind::Declined
235                }
236                subc_presence::LocalAuthenticationFailure::NoPresence => {
237                    OperatorOsErrorKind::NoPresence
238                }
239                subc_presence::LocalAuthenticationFailure::ProviderError => {
240                    OperatorOsErrorKind::ProviderError
241                }
242            },
243            domain,
244            code,
245        },
246        // Future presence providers must fail closed until explicitly mapped.
247        _ => ProviderResult::Unavailable,
248    }
249}
250
251#[cfg(any(target_os = "macos", windows, target_os = "linux"))]
252fn gate_os_outcome(outcome: subc_presence::Outcome) -> ProviderResult {
253    match outcome {
254        subc_presence::Outcome::AuthorizedWithoutPrompt => ProviderResult::Unavailable,
255        other => os_outcome(other),
256    }
257}
258
259#[cfg(all(test, any(target_os = "macos", windows, target_os = "linux")))]
260mod os_outcome_tests {
261    use super::*;
262
263    #[test]
264    fn config_gate_os_outcomes_require_interactive_confirmation() {
265        use crate::config_gate::PresenceDecision as Gate;
266        use subc_presence::{LocalAuthenticationFailure as Failure, Outcome as Os};
267        for (outcome, expected) in [
268            (Os::Approved, Gate::InteractiveApproval),
269            (Os::AuthorizedWithoutPrompt, Gate::Unavailable),
270            (Os::Declined, Gate::Declined),
271            (Os::NoPresence, Gate::Unavailable),
272            (Os::SetupRequired, Gate::Unavailable),
273            (Os::UnsupportedPlatform, Gate::Unavailable),
274            (Os::ProviderError, Gate::Unavailable),
275            (
276                Os::ProviderErrorWithReason { reason: "no_agent" },
277                Gate::Unavailable,
278            ),
279            (
280                Os::LocalAuthenticationError {
281                    failure: Failure::Declined,
282                    domain: "LA".into(),
283                    code: -2,
284                },
285                Gate::Declined,
286            ),
287            (
288                Os::LocalAuthenticationError {
289                    failure: Failure::NoPresence,
290                    domain: "LA".into(),
291                    code: -4,
292                },
293                Gate::Unavailable,
294            ),
295            (
296                Os::LocalAuthenticationError {
297                    failure: Failure::ProviderError,
298                    domain: "LA".into(),
299                    code: -1,
300                },
301                Gate::Unavailable,
302            ),
303        ] {
304            assert_eq!(
305                Gate::from_operator_outcome(&Outcome::from_provider(gate_os_outcome(outcome))),
306                expected
307            );
308        }
309        assert_eq!(
310            Gate::from_operator_outcome(&Outcome::declined("backoff")),
311            Gate::Declined
312        );
313        assert_eq!(
314            Gate::from_operator_outcome(&Outcome::unavailable("queue_full")),
315            Gate::Unavailable
316        );
317    }
318
319    #[test]
320    fn policy_authorization_still_approves_legacy_prompts() {
321        assert_eq!(
322            os_outcome(subc_presence::Outcome::AuthorizedWithoutPrompt),
323            ProviderResult::Approved
324        );
325    }
326
327    #[test]
328    fn only_an_os_approval_becomes_approved() {
329        use subc_presence::Outcome as Os;
330        assert_eq!(os_outcome(Os::Approved), ProviderResult::Approved);
331        assert_eq!(os_outcome(Os::Declined), ProviderResult::Declined);
332        assert_eq!(os_outcome(Os::ProviderError), ProviderResult::Unavailable);
333        let root_error = os_outcome(Os::ProviderErrorWithReason {
334            reason: "root_user",
335        });
336        assert_eq!(
337            root_error,
338            ProviderResult::ProviderError {
339                reason: "root_user"
340            }
341        );
342        let refusal = Outcome::from_provider(root_error);
343        assert_eq!(refusal.code, error_codes::OPERATOR_PRESENCE_UNAVAILABLE);
344        assert_eq!(refusal.reason, "root_user");
345        assert_eq!(os_outcome(Os::NoPresence), ProviderResult::NoPresence);
346        assert_eq!(os_outcome(Os::SetupRequired), ProviderResult::SetupRequired);
347        let refusal = Outcome::from_provider(ProviderResult::SetupRequired);
348        assert_eq!(refusal.code, error_codes::OPERATOR_PRESENCE_UNAVAILABLE);
349        assert_eq!(refusal.reason, "operator_prompt_setup_required");
350        assert_eq!(
351            os_outcome(Os::UnsupportedPlatform),
352            ProviderResult::UnsupportedPlatform
353        );
354        assert_eq!(
355            os_outcome(Os::LocalAuthenticationError {
356                failure: subc_presence::LocalAuthenticationFailure::NoPresence,
357                domain: "com.apple.LocalAuthentication".into(),
358                code: -4,
359            }),
360            ProviderResult::LocalAuthenticationError {
361                failure: OperatorOsErrorKind::NoPresence,
362                domain: "com.apple.LocalAuthentication".into(),
363                code: -4,
364            }
365        );
366    }
367}
368
369#[derive(Clone, Debug)]
370pub(crate) struct Outcome {
371    pub code: &'static str,
372    pub reason: &'static str,
373    retry_after_ms: Option<u64>,
374    os_error_domain: Option<String>,
375    os_error_code: Option<isize>,
376}
377impl Outcome {
378    pub(crate) fn unavailable(reason: &'static str) -> Self {
379        Self {
380            code: error_codes::OPERATOR_PRESENCE_UNAVAILABLE,
381            reason,
382            retry_after_ms: None,
383            os_error_domain: None,
384            os_error_code: None,
385        }
386    }
387    fn declined(reason: &'static str) -> Self {
388        Self {
389            code: error_codes::OPERATOR_DECLINED,
390            reason,
391            retry_after_ms: None,
392            os_error_domain: None,
393            os_error_code: None,
394        }
395    }
396    fn with_retry_after_ms(mut self, retry_after_ms: u64) -> Self {
397        self.retry_after_ms = Some(retry_after_ms);
398        self
399    }
400    pub(crate) fn refusal(code: &'static str) -> Self {
401        Self {
402            code,
403            reason: "",
404            retry_after_ms: None,
405            os_error_domain: None,
406            os_error_code: None,
407        }
408    }
409    fn from_provider(result: ProviderResult) -> Self {
410        match result {
411            ProviderResult::Approved => Self {
412                code: "confirmed",
413                reason: "",
414                retry_after_ms: None,
415                os_error_domain: None,
416                os_error_code: None,
417            },
418            ProviderResult::Declined => Self::declined("person"),
419            ProviderResult::Unavailable => Self::unavailable("provider_error"),
420            ProviderResult::ProviderError { reason } => Self::unavailable(reason),
421            ProviderResult::UnsupportedPlatform => Self::unavailable("unsupported_platform"),
422            ProviderResult::NoPresence => Self::unavailable("no_presence"),
423            ProviderResult::SetupRequired => Self::unavailable("operator_prompt_setup_required"),
424            ProviderResult::LocalAuthenticationError {
425                failure,
426                domain,
427                code,
428            } => {
429                let mut outcome = match failure {
430                    OperatorOsErrorKind::Declined => Self::declined("person"),
431                    OperatorOsErrorKind::NoPresence => Self::unavailable("no_presence"),
432                    OperatorOsErrorKind::ProviderError => Self::unavailable("provider_error"),
433                };
434                outcome.os_error_domain = Some(domain);
435                outcome.os_error_code = Some(code);
436                outcome
437            }
438        }
439    }
440    pub(crate) fn frame(self, request: &Frame) -> Frame {
441        // A refusal is the daemon's own answer to the client's request, so
442        // its ERROR carries the daemon-origin bit; a confirmation is a plain
443        // response and keeps the control flags.
444        let (ty, body, daemon_origin) = if self.code == "confirmed" {
445            (
446                FrameType::Response,
447                serde_json::to_vec(&OperatorConfirmReply::confirmed()).expect("serializable reply"),
448                false,
449            )
450        } else {
451            let mut error = ErrorBody::new(self.code, "operator confirmation refused");
452            if !self.reason.is_empty() {
453                let detail = match self.retry_after_ms {
454                    Some(retry_after_ms) => {
455                        serde_json::json!({"reason": self.reason, "retry_after_ms": retry_after_ms})
456                    }
457                    None => serde_json::json!({"reason": self.reason}),
458                };
459                error = error.with_detail(detail);
460            }
461            (
462                FrameType::Error,
463                serde_json::to_vec(&error).expect("serializable error"),
464                true,
465            )
466        };
467        let flags = if daemon_origin {
468            crate::router::daemon_error_flags()
469        } else {
470            Flags::new(false, Priority::Passive, false)
471        };
472        Frame::build_with_version(
473            request.header.ver,
474            ty,
475            flags,
476            0,
477            0,
478            request.header.corr,
479            body,
480        )
481        .expect("bounded control reply")
482    }
483}
484
485impl crate::config_gate::PresenceDecision {
486    pub(crate) fn from_operator_outcome(outcome: &Outcome) -> Self {
487        match outcome.code {
488            "confirmed" => Self::InteractiveApproval,
489            error_codes::OPERATOR_DECLINED => Self::Declined,
490            _ => Self::Unavailable,
491        }
492    }
493}
494
495fn retry_after_ms(remaining: Duration) -> u64 {
496    let whole_milliseconds = remaining.as_millis();
497    let rounded_milliseconds =
498        whole_milliseconds + u128::from(!remaining.subsec_nanos().is_multiple_of(1_000_000));
499    u64::try_from(rounded_milliseconds).expect("operator confirmation backoff fits in u64")
500}
501
502pub(crate) fn audit(
503    module_id: &str,
504    summary: &str,
505    principal: &str,
506    outcome: &Outcome,
507    wait: Duration,
508    prompt: Duration,
509    prompt_shown: bool,
510) {
511    tracing::info!(target: "subc_daemon::operator_confirm", module_id, summary = ?summary,
512        principal, outcome = outcome.code, reason = outcome.reason,
513        os_error_domain = ?outcome.os_error_domain, os_error_code = ?outcome.os_error_code,
514        wait_ms = wait.as_millis() as u64, prompt_ms = prompt.as_millis() as u64,
515        prompt_shown, "operator_confirm_audit");
516}
517
518fn principal_label(principal: &Principal) -> String {
519    match principal {
520        Principal::Direct => "direct".into(),
521        Principal::Reserved { module_id } => format!("reserved:{module_id}"),
522        Principal::Unverified => String::new(),
523    }
524}
525
526pub(crate) fn valid_summary(summary: &str) -> bool {
527    let count = summary.chars().count();
528    (1..=200).contains(&count)
529        && !summary.starts_with(char::is_whitespace)
530        && !summary.ends_with(char::is_whitespace)
531        && !summary.chars().any(|c| {
532            c.is_control()
533                || matches!(c,
534            '\u{200b}'..='\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2028}' | '\u{2029}' |
535            '\u{2060}'..='\u{2069}' | '\u{feff}')
536        })
537}
538
539enum RequestKind {
540    Module(ModuleRouteKey),
541    Privileged {
542        answer: Mutex<Option<oneshot::Sender<Outcome>>>,
543        key: DeclineKey,
544    },
545}
546
547struct Request {
548    gate_presence: bool,
549    id: u64,
550    module_id: String,
551    summary: String,
552    principal: String,
553    text: String,
554    kind: RequestKind,
555    ctx: RouteCtx,
556    frame: Frame,
557    arrived: Instant,
558    delivery_deadline: Instant,
559    // A handoff reserves its queue position before its outcome append finishes.
560    prompt_ready: std::sync::atomic::AtomicBool,
561    shortcut_audit: bool,
562}
563struct Active {
564    request: Arc<Request>,
565    started: Option<Instant>,
566    handle: Option<Arc<dyn OperatorWithdraw>>,
567    committed: bool,
568    withdrawn: Option<(Instant, &'static str)>,
569    withdraw_started: bool,
570}
571struct State {
572    provider: Arc<dyn OperatorProvider>,
573    limits: OperatorLimits,
574    queue: VecDeque<Arc<Request>>,
575    modules: HashMap<String, u64>,
576    backoff: HashMap<(String, String), Instant>,
577    cooldown_clock: CooldownClock,
578    /// Recent person declines of privileged requests, oldest first.
579    declines: HashMap<DeclineKey, VecDeque<Instant>>,
580    /// When each privileged caller's cooldown ends.
581    cooldowns: HashMap<DeclineKey, Instant>,
582    prompt: Option<Active>,
583    stuck: bool,
584    running: bool,
585    next_id: u64,
586    shortcuts: HashMap<u64, shortcut::Pending>,
587    #[cfg(test)]
588    deliveries: usize,
589}
590
591pub(crate) struct OperatorConfirms {
592    state: Mutex<State>,
593    wake: Notify,
594}
595
596struct WithdrawOnDrop {
597    confirms: Arc<OperatorConfirms>,
598    id: u64,
599}
600
601impl Drop for WithdrawOnDrop {
602    fn drop(&mut self) {
603        self.confirms
604            .withdraw_matching(|request| request.id == self.id, "caller_cancelled");
605    }
606}
607impl std::fmt::Debug for OperatorConfirms {
608    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
609        f.debug_struct("OperatorConfirms").finish_non_exhaustive()
610    }
611}
612impl Default for OperatorConfirms {
613    fn default() -> Self {
614        Self {
615            state: Mutex::new(State {
616                provider: Arc::new(OsProvider),
617                limits: OperatorLimits::default(),
618                queue: VecDeque::new(),
619                modules: HashMap::new(),
620                backoff: HashMap::new(),
621                cooldown_clock: Arc::new(Instant::now),
622                declines: HashMap::new(),
623                cooldowns: HashMap::new(),
624                prompt: None,
625                stuck: false,
626                running: false,
627                next_id: 0,
628                shortcuts: HashMap::new(),
629                #[cfg(test)]
630                deliveries: 0,
631            }),
632            wake: Notify::new(),
633        }
634    }
635}
636impl OperatorConfirms {
637    pub(crate) fn config_gate_ready(&self) -> bool {
638        let state = self.lock();
639        !state.stuck && state.provider.config_gate_ready()
640    }
641    fn lock(&self) -> std::sync::MutexGuard<'_, State> {
642        self.state.lock().unwrap_or_else(|p| p.into_inner())
643    }
644    pub(crate) fn configure(&self, provider: Arc<dyn OperatorProvider>, limits: OperatorLimits) {
645        let mut state = self.lock();
646        assert!(
647            !state.running && state.modules.is_empty(),
648            "configure before serving"
649        );
650        state.provider = provider;
651        state.limits = limits;
652    }
653    pub(crate) fn configure_bootstrap(&self, config: &crate::bootstrap::BootstrapConfig) {
654        let provider: Arc<dyn OperatorProvider> = Arc::new(OsProvider);
655        #[cfg(feature = "test-support")]
656        let provider = test_provider::select(
657            provider,
658            config.operator_script.as_deref(),
659            config.operator_events.as_deref(),
660            std::env::current_exe().ok().as_deref(),
661        );
662        self.configure(
663            provider,
664            OperatorLimits {
665                queue_wait: config.operator_queue_wait,
666                prompt_timeout: config.operator_timeout,
667                stuck_grace: config.operator_stuck_grace,
668            },
669        );
670    }
671
672    #[cfg(feature = "test-support")]
673    pub(crate) fn configure_script(
674        &self,
675        script: &std::path::Path,
676        events: Option<&std::path::Path>,
677        limits: Option<OperatorLimits>,
678    ) {
679        let provider = test_provider::select(
680            Arc::new(OsProvider),
681            Some(script),
682            events,
683            test_provider::current_executable().as_deref(),
684        );
685        self.configure(provider, limits.unwrap_or_default());
686    }
687
688    /// Called while the forwarding read lock still protects this binding. All
689    /// registration/nonce lookups must have finished before entering this method.
690    #[cfg(test)]
691    pub(crate) fn admit(
692        self: &Arc<Self>,
693        ctx: &RouteCtx,
694        frame: Frame,
695        module_id: String,
696        nonce_proven: bool,
697        request: OperatorConfirmRequest,
698        binding: Option<&RouteBinding>,
699    ) -> Vec<Frame> {
700        self.admit_with_shortcut(ctx, frame, module_id, nonce_proven, request, binding, None)
701    }
702
703    #[allow(clippy::too_many_arguments)]
704    pub(crate) fn admit_with_shortcut(
705        self: &Arc<Self>,
706        ctx: &RouteCtx,
707        frame: Frame,
708        module_id: String,
709        nonce_proven: bool,
710        request: OperatorConfirmRequest,
711        binding: Option<&RouteBinding>,
712        shortcut: Option<ShortcutContext>,
713    ) -> Vec<Frame> {
714        let principal = binding
715            .map(|b| principal_label(&b.principal))
716            .unwrap_or_default();
717        let mut state = self.lock();
718        let now = Instant::now();
719        state.backoff.retain(|_, until| *until > now);
720        let refusal = if !nonce_proven
721            || binding.is_none_or(|b| matches!(b.principal, Principal::Unverified))
722        {
723            Some(Outcome::refusal(
724                error_codes::OPERATOR_REQUEST_NOT_PERMITTED,
725            ))
726        } else if !valid_summary(&request.summary) {
727            Some(Outcome::refusal(error_codes::OPERATOR_SUMMARY_INVALID))
728        } else if let Some(until) = state
729            .backoff
730            .get(&(module_id.clone(), principal.clone()))
731            .copied()
732            .filter(|_| shortcut.is_none())
733        {
734            Some(Outcome::declined("backoff").with_retry_after_ms(retry_after_ms(until - now)))
735        } else if shortcut.is_none() && state.stuck {
736            Some(Outcome::unavailable("provider_stuck"))
737        } else if state.modules.contains_key(&module_id) {
738            Some(Outcome::unavailable("module_limit"))
739        } else if shortcut.is_none() && state.queue.len() >= 4 {
740            Some(Outcome::unavailable("queue_full"))
741        } else {
742            None
743        };
744        if let Some(outcome) = refusal {
745            audit(
746                &module_id,
747                &request.summary,
748                &principal,
749                &outcome,
750                Duration::ZERO,
751                Duration::ZERO,
752                false,
753            );
754            return vec![outcome.frame(&frame)];
755        }
756        let binding = binding.expect("permission checked");
757        let opener = match &binding.principal {
758            Principal::Direct => "a local program",
759            Principal::Reserved { module_id } => module_id,
760            Principal::Unverified => unreachable!(),
761        };
762        state.next_id += 1;
763        let pending = Arc::new(Request {
764            id: state.next_id,
765            text: format!(
766                "approve a request from {module_id}: {} (requested by {opener})",
767                request.summary
768            ),
769            module_id,
770            summary: request.summary,
771            principal,
772            gate_presence: false,
773            kind: RequestKind::Module(ModuleRouteKey {
774                endpoint: binding.module_endpoint,
775                channel: binding.module_channel,
776            }),
777            ctx: ctx.clone(),
778            frame,
779            arrived: now,
780            delivery_deadline: now + state.limits.delivery_budget(),
781            prompt_ready: true.into(),
782            shortcut_audit: shortcut.is_some(),
783        });
784        state.modules.insert(pending.module_id.clone(), pending.id);
785        if let Some(shortcut) = shortcut {
786            self.start_shortcut(&mut state, pending, shortcut);
787        } else {
788            self.enqueue(&mut state, pending);
789        }
790        Vec::new()
791    }
792
793    /// Client-control approval shares the module confirmation queue and timers,
794    /// but returns a decision to dispatch instead of sending an operator reply.
795    /// A caller in its repeated-decline cooldown is refused before anything else
796    /// is checked, without a prompt.
797    #[allow(clippy::too_many_arguments)]
798    pub(crate) async fn approve_privileged_with_shortcut(
799        self: &Arc<Self>,
800        ctx: &RouteCtx,
801        frame: &Frame,
802        operation: &str,
803        text: String,
804        caller: String,
805        key: DeclineKey,
806        shortcut: Option<ShortcutContext>,
807    ) -> Outcome {
808        self.approve_client(ctx, frame, operation, text, caller, key, shortcut, false)
809            .await
810    }
811
812    pub(crate) async fn approve_config_gate(
813        self: &Arc<Self>,
814        ctx: &RouteCtx,
815        frame: &Frame,
816        text: String,
817        caller: String,
818        key: DeclineKey,
819    ) -> Outcome {
820        // Never accept an operator-app shortcut as config review.
821        self.approve_client(ctx, frame, "config.apply", text, caller, key, None, true)
822            .await
823    }
824
825    #[cfg(target_os = "macos")]
826    pub(crate) async fn approve_app_trust(
827        self: &Arc<Self>,
828        ctx: &RouteCtx,
829        frame: &Frame,
830        text: String,
831        caller: String,
832        key: DeclineKey,
833    ) -> Outcome {
834        // A previously trusted app cannot approve a different build, nor can
835        // an OS authorization that did not prompt the person.
836        self.approve_client(
837            ctx,
838            frame,
839            subc_control::ops::APP_TRUST,
840            text,
841            caller,
842            key,
843            None,
844            true,
845        )
846        .await
847    }
848
849    #[allow(clippy::too_many_arguments)]
850    async fn approve_client(
851        self: &Arc<Self>,
852        ctx: &RouteCtx,
853        frame: &Frame,
854        operation: &str,
855        text: String,
856        caller: String,
857        key: DeclineKey,
858        shortcut: Option<ShortcutContext>,
859        gate_presence: bool,
860    ) -> Outcome {
861        let (answer, receive) = oneshot::channel();
862        let id = {
863            let mut state = self.lock();
864            let now = (state.cooldown_clock)();
865            state.cooldowns.retain(|_, until| *until > now);
866            state.declines.retain(|_, declines| {
867                declines.retain(|at| now.saturating_duration_since(*at) < DECLINE_WINDOW);
868                !declines.is_empty()
869            });
870            let refusal = if state.cooldowns.contains_key(&key) {
871                Some(Outcome::declined("backoff"))
872            } else if shortcut.is_none() && state.stuck {
873                Some(Outcome::unavailable("provider_stuck"))
874            } else if shortcut.is_none() && state.queue.len() >= 4 {
875                Some(Outcome::unavailable("queue_full"))
876            } else {
877                None
878            };
879            if let Some(outcome) = refusal {
880                audit(
881                    operation,
882                    &text,
883                    &caller,
884                    &outcome,
885                    Duration::ZERO,
886                    Duration::ZERO,
887                    false,
888                );
889                return outcome;
890            }
891            state.next_id += 1;
892            let id = state.next_id;
893            let now = Instant::now();
894            let pending = Arc::new(Request {
895                id,
896                module_id: operation.into(),
897                summary: text.clone(),
898                principal: caller,
899                text,
900                gate_presence,
901                kind: RequestKind::Privileged {
902                    answer: Mutex::new(Some(answer)),
903                    key,
904                },
905                ctx: ctx.clone(),
906                frame: frame.clone(),
907                arrived: now,
908                delivery_deadline: now + state.limits.delivery_budget(),
909                prompt_ready: true.into(),
910                shortcut_audit: shortcut.is_some(),
911            });
912            if let Some(shortcut) = shortcut {
913                self.start_shortcut(&mut state, pending, shortcut);
914            } else {
915                self.enqueue(&mut state, pending);
916            }
917            id
918        };
919        let _withdraw = WithdrawOnDrop {
920            confirms: Arc::clone(self),
921            id,
922        };
923        receive
924            .await
925            .unwrap_or_else(|_| Outcome::unavailable("provider_error"))
926    }
927
928    fn enqueue(self: &Arc<Self>, state: &mut State, pending: Arc<Request>) {
929        if state.prompt.is_none() && state.queue.is_empty() {
930            state.prompt = Some(Active {
931                request: pending,
932                started: None,
933                handle: None,
934                committed: false,
935                withdrawn: None,
936                withdraw_started: false,
937            });
938        } else {
939            state.queue.push_back(pending);
940        }
941        if !state.running {
942            state.running = true;
943            tokio::spawn(Arc::clone(self).run());
944        }
945        self.wake.notify_one();
946    }
947
948    fn commit(
949        self: &Arc<Self>,
950        state: &mut State,
951        request: Arc<Request>,
952        outcome: Outcome,
953        started: Option<Instant>,
954        shown: bool,
955    ) {
956        let now = Instant::now();
957        let wait = started.unwrap_or(now).duration_since(request.arrived);
958        let prompt = started.map(|at| now.duration_since(at)).unwrap_or_default();
959        if outcome.reason == "person" && matches!(request.kind, RequestKind::Module(_)) {
960            state.backoff.insert(
961                (request.module_id.clone(), request.principal.clone()),
962                now + Duration::from_secs(30),
963            );
964        }
965        audit(
966            &request.module_id,
967            if request.shortcut_audit {
968                "Operator shortcut request"
969            } else {
970                &request.summary
971            },
972            &request.principal,
973            &outcome,
974            wait,
975            prompt,
976            shown,
977        );
978        if let RequestKind::Privileged { answer, key } = &request.kind {
979            let person_declined = outcome.reason == "person";
980            if let Some(answer) = answer.lock().unwrap_or_else(|p| p.into_inner()).take() {
981                let _ = answer.send(outcome);
982            }
983            if person_declined {
984                self.count_person_decline(state, key);
985            }
986            return;
987        }
988        let this = Arc::clone(self);
989        #[cfg(test)]
990        {
991            state.deliveries += 1;
992        }
993        tokio::spawn(async move {
994            let answer = outcome.frame(&request.frame);
995            match timeout_at(request.delivery_deadline, request.ctx.egress.send(answer)).await {
996                Ok(Ok(())) => {}
997                result => {
998                    tracing::warn!(target: "subc_daemon::operator_confirm", module_id = request.module_id, ?result, "operator confirm answer dropped")
999                }
1000            }
1001            let mut state = this.lock();
1002            #[cfg(test)]
1003            {
1004                state.deliveries -= 1;
1005            }
1006            if state.modules.get(&request.module_id) == Some(&request.id) {
1007                state.modules.remove(&request.module_id);
1008            }
1009        });
1010    }
1011
1012    /// Only a person's decline at the prompt reaches here: withdrawals, timeouts
1013    /// and unavailability commit other reasons. The prompt that recorded the
1014    /// limit-reaching decline has already ended, so starting the cooldown only
1015    /// refuses this caller's requests still waiting in the queue.
1016    fn count_person_decline(self: &Arc<Self>, state: &mut State, key: &DeclineKey) {
1017        let now = (state.cooldown_clock)();
1018        let declines = state.declines.entry(key.clone()).or_default();
1019        declines.retain(|at| now.saturating_duration_since(*at) < DECLINE_WINDOW);
1020        declines.push_back(now);
1021        if declines.len() < DECLINE_LIMIT {
1022            return;
1023        }
1024        // Counting restarts after the cooldown instead of carrying old declines.
1025        state.declines.remove(key);
1026        state.cooldowns.insert(key.clone(), now + DECLINE_COOLDOWN);
1027        tracing::warn!(target: "subc_daemon::operator_confirm", caller = %key,
1028            cooldown_secs = DECLINE_COOLDOWN.as_secs(),
1029            "privileged requests refused without a prompt after repeated operator declines");
1030        let mut index = 0;
1031        while index < state.queue.len() {
1032            if matches!(&state.queue[index].kind,
1033                RequestKind::Privileged { key: queued, .. } if queued == key)
1034            {
1035                let request = state.queue.remove(index).expect("queue index");
1036                self.commit(state, request, Outcome::declined("backoff"), None, false);
1037            } else {
1038                index += 1;
1039            }
1040        }
1041    }
1042
1043    fn withdraw_matching(
1044        self: &Arc<Self>,
1045        predicate: impl Fn(&Request) -> bool,
1046        reason: &'static str,
1047    ) -> bool {
1048        let mut state = self.lock();
1049        let mut matched = false;
1050        let ids: Vec<_> = state
1051            .shortcuts
1052            .iter()
1053            .filter(|(_, pending)| predicate(&pending.request))
1054            .map(|(id, _)| *id)
1055            .collect();
1056        for id in ids {
1057            self.end_shortcut(&mut state, id, Outcome::declined(reason), "withdrawn");
1058            matched = true;
1059        }
1060        let mut index = 0;
1061        while index < state.queue.len() {
1062            if predicate(&state.queue[index]) {
1063                let request = state.queue.remove(index).expect("queue index");
1064                self.commit(&mut state, request, Outcome::declined(reason), None, false);
1065                matched = true;
1066            } else {
1067                index += 1;
1068            }
1069        }
1070        if let Some(active) = state
1071            .prompt
1072            .as_mut()
1073            .filter(|a| !a.committed && predicate(&a.request))
1074        {
1075            active.committed = true;
1076            active.withdrawn = Some((Instant::now(), reason));
1077            let (request, started, shown) = (
1078                Arc::clone(&active.request),
1079                active.started,
1080                active.handle.is_some(),
1081            );
1082            self.commit(
1083                &mut state,
1084                request,
1085                Outcome::declined(reason),
1086                started,
1087                shown,
1088            );
1089            matched = true;
1090        }
1091        self.wake.notify_one();
1092        matched
1093    }
1094    pub(crate) fn route_closed(self: &Arc<Self>, key: ModuleRouteKey) {
1095        self.withdraw_matching(
1096            |r| matches!(r.kind, RequestKind::Module(k) if k == key),
1097            "route_closed",
1098        );
1099    }
1100    pub(crate) fn module_closed(self: &Arc<Self>, endpoint: ModuleEndpointId) {
1101        self.withdraw_matching(
1102            |r| matches!(r.kind, RequestKind::Module(k) if k.endpoint == endpoint),
1103            "module_closed",
1104        );
1105    }
1106    pub(crate) fn connection_closed(self: &Arc<Self>, connection: crate::ConnectionId) {
1107        self.withdraw_matching(
1108            |r| {
1109                matches!(r.kind, RequestKind::Privileged { .. })
1110                    && r.ctx.connection_id == connection
1111            },
1112            "caller_cancelled",
1113        );
1114    }
1115    pub(crate) fn cancel(self: &Arc<Self>, connection: crate::ConnectionId, corr: u64) -> bool {
1116        self.withdraw_matching(
1117            |r| r.ctx.connection_id == connection && r.frame.header.corr == corr,
1118            "caller_cancelled",
1119        )
1120    }
1121
1122    fn publish(self: &Arc<Self>, id: u64, handle: Arc<dyn OperatorWithdraw>) {
1123        let mut state = self.lock();
1124        if let Some(active) = state.prompt.as_mut().filter(|a| a.request.id == id) {
1125            active.handle = Some(Arc::clone(&handle));
1126            if let Some((_, reason)) = active.withdrawn {
1127                active.withdraw_started = true;
1128                tokio::task::spawn_blocking(move || handle.withdraw(reason));
1129            }
1130        }
1131        self.wake.notify_one();
1132    }
1133    fn returned(self: &Arc<Self>, id: u64, result: ProviderResult) {
1134        let mut state = self.lock();
1135        let Some(active) = state.prompt.take() else {
1136            return;
1137        };
1138        assert_eq!(active.request.id, id);
1139        // A fast late return can beat the prompt task's wake-up. The decision
1140        // still requires a withdraw, even though the result is discarded.
1141        if !active.withdraw_started {
1142            if let (Some(handle), Some((_, reason))) = (active.handle.as_ref(), active.withdrawn) {
1143                let handle = Arc::clone(handle);
1144                tokio::task::spawn_blocking(move || handle.withdraw(reason));
1145            }
1146        }
1147        if !active.committed {
1148            let outcome = if Instant::now() >= active.request.delivery_deadline {
1149                Outcome::unavailable("timeout")
1150            } else {
1151                Outcome::from_provider(result)
1152            };
1153            self.commit(
1154                &mut state,
1155                active.request,
1156                outcome,
1157                active.started,
1158                active.handle.is_some(),
1159            );
1160        } else {
1161            tracing::debug!(target: "subc_daemon::operator_confirm", ?result, "late operator provider result discarded");
1162        }
1163        state.stuck = false;
1164        self.wake.notify_one();
1165    }
1166
1167    async fn run(self: Arc<Self>) {
1168        loop {
1169            let deadline = {
1170                let mut state = self.lock();
1171                let now = Instant::now();
1172                let mut index = 0;
1173                while index < state.queue.len() {
1174                    if state.queue[index].arrived + state.limits.queue_wait <= now {
1175                        let request = state.queue.remove(index).expect("queue index");
1176                        self.commit(
1177                            &mut state,
1178                            request,
1179                            Outcome::unavailable("queue_wait"),
1180                            None,
1181                            false,
1182                        );
1183                    } else {
1184                        index += 1;
1185                    }
1186                }
1187                let timeout = state.limits.prompt_timeout;
1188                if let Some(active) = state.prompt.as_mut().filter(|a| {
1189                    !a.committed
1190                        && (a.request.delivery_deadline <= now
1191                            || a.started.is_some_and(|at| at + timeout <= now))
1192                }) {
1193                    active.committed = true;
1194                    active.withdrawn = Some((now, "timeout"));
1195                    let (request, started, shown) = (
1196                        Arc::clone(&active.request),
1197                        active.started,
1198                        active.handle.is_some(),
1199                    );
1200                    self.commit(
1201                        &mut state,
1202                        request,
1203                        Outcome::unavailable("timeout"),
1204                        started,
1205                        shown,
1206                    );
1207                }
1208                let grace = state.limits.stuck_grace;
1209                if !state.stuck
1210                    && state.prompt.as_ref().is_some_and(|a| {
1211                        a.started.is_some() && a.withdrawn.is_some_and(|(at, _)| at + grace <= now)
1212                    })
1213                {
1214                    state.stuck = true;
1215                    tracing::error!(target: "subc_daemon::operator_confirm", "operator provider stuck after withdraw");
1216                    while let Some(request) = state.queue.pop_front() {
1217                        self.commit(
1218                            &mut state,
1219                            request,
1220                            Outcome::unavailable("provider_stuck"),
1221                            None,
1222                            false,
1223                        );
1224                    }
1225                }
1226                if let Some(active) = state
1227                    .prompt
1228                    .as_mut()
1229                    .filter(|a| !a.withdraw_started && a.withdrawn.is_some() && a.handle.is_some())
1230                {
1231                    active.withdraw_started = true;
1232                    let handle = Arc::clone(active.handle.as_ref().expect("handle checked"));
1233                    let reason = active.withdrawn.expect("withdraw checked").1;
1234                    tokio::task::spawn_blocking(move || handle.withdraw(reason));
1235                }
1236                // A reserved prompt cancelled before setup needs no provider call.
1237                if state
1238                    .prompt
1239                    .as_ref()
1240                    .is_some_and(|a| a.started.is_none() && a.committed)
1241                {
1242                    state.prompt = None;
1243                }
1244                if state.prompt.is_none() {
1245                    if let Some(request) = state.queue.pop_front() {
1246                        state.prompt = Some(Active {
1247                            request,
1248                            started: None,
1249                            handle: None,
1250                            committed: false,
1251                            withdrawn: None,
1252                            withdraw_started: false,
1253                        });
1254                    }
1255                }
1256                if let Some(active) = state.prompt.as_mut().filter(|a| {
1257                    a.started.is_none()
1258                        && !a.committed
1259                        && a.request
1260                            .prompt_ready
1261                            .load(std::sync::atomic::Ordering::Acquire)
1262                }) {
1263                    active.started = Some(now);
1264                    let request = Arc::clone(&active.request);
1265                    let provider = Arc::clone(&state.provider);
1266                    let this = Arc::clone(&self);
1267                    tokio::spawn(async move {
1268                        let publish_to = Arc::clone(&this);
1269                        let id = request.id;
1270                        let result = tokio::task::spawn_blocking(move || {
1271                            let publish = Box::new(move |handle| publish_to.publish(id, handle));
1272                            if request.gate_presence {
1273                                provider.prompt_gate(&request.text, publish)
1274                            } else {
1275                                provider.prompt(&request.text, publish)
1276                            }
1277                        })
1278                        .await
1279                        .unwrap_or(ProviderResult::Unavailable);
1280                        this.returned(id, result);
1281                    });
1282                }
1283                if state.prompt.is_none() && state.queue.is_empty() {
1284                    state.running = false;
1285                    return;
1286                }
1287                let mut deadline = state
1288                    .queue
1289                    .iter()
1290                    .map(|r| r.arrived + state.limits.queue_wait)
1291                    .min();
1292                if let Some(active) = &state.prompt {
1293                    let next = if !active.committed {
1294                        Some(
1295                            active
1296                                .started
1297                                .map_or(active.request.delivery_deadline, |at| {
1298                                    (at + timeout).min(active.request.delivery_deadline)
1299                                }),
1300                        )
1301                    } else if !state.stuck {
1302                        active.withdrawn.map(|(at, _)| at + grace)
1303                    } else {
1304                        None
1305                    };
1306                    if let Some(next) = next {
1307                        deadline = Some(deadline.map_or(next, |d| d.min(next)));
1308                    }
1309                }
1310                deadline
1311            };
1312            if let Some(at) = deadline {
1313                tokio::select! { _ = self.wake.notified() => {}, _ = tokio::time::sleep_until(at) => {} }
1314            } else {
1315                self.wake.notified().await;
1316            }
1317        }
1318    }
1319}
1320
1321#[cfg(feature = "test-support")]
1322pub(crate) mod test_provider;
1323#[cfg(test)]
1324mod tests;