Skip to main content

switchyard_libsy/algorithms/
advisor_gate.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Executor gated by a once-per-session advisor review.
5//!
6//! The executor answers every client-visible turn. Turns with tool calls pass
7//! through unreviewed; the first *terminal* turn — no tool calls (or a text
8//! match under the `pattern` trigger) — is buffered and shown to a stronger
9//! advisor model together with the full transcript. `APPROVE` releases the
10//! buffered turn unchanged; `REDO` appends the discarded turn's text and the
11//! advisor's plan as feedback, then re-invokes the executor so it keeps
12//! working. Each budget scope (one benchmark evaluation, one session, or the
13//! whole instance — see [`budget_scope`]) is reviewed at most `max_reviews`
14//! times; afterwards every call is a pure passthrough.
15//!
16//! This design is a near-superset of solo executor behavior: identical until
17//! the executor first claims to be done, plus one quality gate that catches
18//! premature convergence. Front-loading advice was measured to suppress the
19//! executor's own test-and-iterate loop, so no advice is injected up front.
20//!
21//! Failure posture: executor errors always propagate (including
22//! `ContextWindowExceeded`, which hosts map to a client-visible 400 so agent
23//! harnesses can compact). Advisor errors honor `fail_open` — the buffered
24//! turn passes through as an implicit APPROVE — refund the consumed review,
25//! and count toward a per-scope failure cap that stops consulting a down
26//! advisor entirely.
27
28use std::collections::{HashMap, HashSet};
29use std::hash::{DefaultHasher, Hash, Hasher};
30use std::sync::Arc;
31use std::time::Instant;
32
33use parking_lot::Mutex;
34use switchyard_protocol::{
35    ContentBlock, InstructionBlock, LlmRequest, Message, ModelId, OutputParams, Request, Role,
36    SamplingParams,
37};
38
39use crate::core::algorithm::{Algorithm, Driver, RoutingOutcome};
40use crate::{LibsyError, Result};
41
42mod telemetry;
43#[cfg(test)]
44mod tests;
45mod transcript;
46mod turn;
47
48use telemetry::{
49    ReviewAudit, emit_discarded_audit, emit_review_audit, record_consult_failure, record_discarded,
50    record_review,
51};
52use transcript::{VERDICT_PATTERN, Verdict, advisor_reply_text, parse_verdict, review_transcript};
53use turn::{
54    GatedTurn, assistant_turns, buffer_turn, count_tool_results, has_tool_use, reasoning_text,
55    visible_text,
56};
57
58/// APPROVE/REDO reviewer contract sent as the advisor's system prompt.
59pub const REVIEWER_SYSTEM_PROMPT: &str =
60    include_str!("../prompts/advisor-gate/reviewer-system-prompt.md");
61
62/// Prepended to the advisor's REDO plan when it is fed back as a user turn,
63/// instructing the executor to continue rather than stop.
64pub const REDO_FEEDBACK_PREFIX: &str = concat!(
65    include_str!("../prompts/advisor-gate/redo-feedback-prefix.md"),
66    "\n"
67);
68
69/// Labels the executor's internal reasoning when a turn has no visible text,
70/// so the advisor still has evidence to review (reasoning models on vLLM/NIM
71/// can emit turns whose only output is reasoning).
72const REASONING_TAIL_LABEL: &str =
73    "(the executor produced no visible text this turn; its internal reasoning follows)\n";
74/// REDO echo when the discarded turn had neither text nor reasoning; strict
75/// endpoints (Anthropic) reject empty text blocks, so never echo "".
76const EMPTY_ECHO_PLACEHOLDER: &str = "(the executor produced no output this turn)";
77/// Failed consults tolerated per scope before the gate stops consulting.
78/// Failures refund the review budget — a transient advisor error must not
79/// silently exhaust `max_reviews` with zero real reviews — so this separate
80/// cap is what bounds per-turn consult latency against a down advisor.
81const MAX_FAILED_CONSULTS: u32 = 3;
82/// Bounds tracked budget scopes and stall keys; a scope dropped at the bound
83/// re-arms like a process restart (rare, harmless).
84const MAX_TRACKED_SCOPES: usize = 1_024;
85/// Benchmark harnesses stamp every request of one evaluation — sub-agents
86/// included — with this header, so it is the review budget's first-choice
87/// scope: "reviews for *this* task" survives gateways shared by many tasks.
88const BENCH_SESSION_HEADER: &str = "proxy_x_session_id";
89
90/// How the gate decides a buffered executor turn is terminal.
91#[derive(Clone, Debug, PartialEq)]
92pub enum GateTrigger {
93    /// First turn without tool calls (subject to `gate_min_tool_results`).
94    NoToolCall,
95    /// First turn whose visible text matches this regex (searched, not anchored) —
96    /// for text-protocol harnesses where every turn lacks tool calls and
97    /// completion is declared with a textual marker instead.
98    Pattern(String),
99}
100
101/// Gate knobs; defaults mirror the benchmarked Python advisor configuration.
102#[derive(Clone, Debug)]
103pub struct AdvisorGateConfig {
104    /// System prompt for the advisor's review call; states the APPROVE/REDO contract.
105    pub reviewer_system_prompt: String,
106    /// Prepended to the advisor's REDO plan when fed back to the executor.
107    pub redo_feedback_prefix: String,
108    /// What fires the review.
109    pub gate_trigger: GateTrigger,
110    /// Reviews allowed per budget scope. 1 keeps the original once-per-task
111    /// gate; higher values re-review later terminal turns, making the gate a
112    /// sequential best-of-(N+1) with the advisor as judge.
113    pub max_reviews: u32,
114    /// When > 0, additionally review (once per conversation, consuming budget)
115    /// the first request already carrying at least this many assistant turns —
116    /// a mid-task checkpoint for executors that grind without declaring
117    /// completion. 0 disables.
118    pub gate_stall_turns: u32,
119    /// For the `no_tool_call` trigger: only review once the conversation
120    /// carries at least this many tool results, skipping early commentary
121    /// turns on chatty harnesses. 0 reviews from the first terminal turn.
122    pub gate_min_tool_results: u32,
123    /// Cap on the advisor's output per consult.
124    pub advisor_max_tokens: u64,
125    /// Sampling temperature for the consult; `None` omits the field on the wire.
126    pub advisor_temperature: Option<f64>,
127    /// Cap on the serialized transcript handed to the advisor; the middle of
128    /// an over-cap conversation is dropped (task head + recent tail survive).
129    pub transcript_max_chars: usize,
130    /// When true (default), an advisor failure degrades to APPROVE; when
131    /// false, it propagates as the turn's error.
132    pub fail_open: bool,
133}
134
135impl Default for AdvisorGateConfig {
136    fn default() -> Self {
137        Self {
138            reviewer_system_prompt: REVIEWER_SYSTEM_PROMPT.to_string(),
139            redo_feedback_prefix: REDO_FEEDBACK_PREFIX.to_string(),
140            gate_trigger: GateTrigger::NoToolCall,
141            max_reviews: 1,
142            gate_stall_turns: 0,
143            gate_min_tool_results: 0,
144            advisor_max_tokens: 2048,
145            advisor_temperature: None,
146            transcript_max_chars: 200_000,
147            fail_open: true,
148        }
149    }
150}
151
152/// The trigger with its pattern compiled once at construction.
153enum CompiledTrigger {
154    NoToolCall,
155    Pattern(regex::Regex),
156}
157
158/// Review budget scope, in precedence order: the benchmark harness header
159/// (exact evaluation identity, sub-agents included), then the host-resolved
160/// session id, then one instance-wide scope for headerless clients.
161#[derive(Clone, Debug, Hash, PartialEq, Eq)]
162enum ScopeKey {
163    Instance,
164    Client(String),
165    Session(String),
166}
167
168/// Per-scope review ledger.
169#[derive(Default)]
170struct ScopeState {
171    reviews: u32,
172    failed_consults: u32,
173    exhaustion_logged: bool,
174}
175
176/// Shared mutable gate state; every access is a short critical section and
177/// the lock is never held across an await.
178#[derive(Default)]
179struct GateState {
180    scopes: HashMap<ScopeKey, ScopeState>,
181    stall_fired: HashSet<u64>,
182}
183
184/// Advisor review gate: executor turns pass through until the first terminal
185/// turn, which a stronger advisor reviews once per scope budget (APPROVE
186/// releases it, REDO feeds the plan back and re-invokes the executor).
187pub struct AdvisorGate {
188    executor: ModelId,
189    advisor: ModelId,
190    config: AdvisorGateConfig,
191    trigger: CompiledTrigger,
192    verdict_re: regex::Regex,
193    state: Mutex<GateState>,
194}
195
196impl AdvisorGate {
197    /// Validates ranges and compiles the trigger and verdict patterns.
198    pub fn new(executor: ModelId, advisor: ModelId, config: AdvisorGateConfig) -> Result<Self> {
199        if config.max_reviews < 1 {
200            return Err(algorithm_error("max_reviews must be at least 1"));
201        }
202        if config.advisor_max_tokens < 1 {
203            return Err(algorithm_error("advisor_max_tokens must be at least 1"));
204        }
205        if config.transcript_max_chars < 256 {
206            return Err(algorithm_error("transcript_max_chars must be at least 256"));
207        }
208        let trigger = match &config.gate_trigger {
209            GateTrigger::NoToolCall => CompiledTrigger::NoToolCall,
210            GateTrigger::Pattern(pattern) => {
211                if pattern.is_empty() {
212                    return Err(algorithm_error(
213                        "gate_trigger 'pattern' requires a non-empty gate_trigger_pattern",
214                    ));
215                }
216                CompiledTrigger::Pattern(regex::Regex::new(pattern).map_err(|error| {
217                    algorithm_error(format!(
218                        "gate_trigger_pattern is not a valid regex: {error}"
219                    ))
220                })?)
221            }
222        };
223        let verdict_re = regex::Regex::new(VERDICT_PATTERN).map_err(|error| {
224            algorithm_error(format!("verdict pattern failed to compile: {error}"))
225        })?;
226        Ok(Self {
227            executor,
228            advisor,
229            config,
230            trigger,
231            verdict_re,
232            state: Mutex::new(GateState::default()),
233        })
234    }
235
236    // ── Scope ledger ────────────────────────────────────────────────────────
237
238    /// Whether the scope's budget or failure cap is spent; logs once per scope.
239    fn check_exhausted(&self, scope: &ScopeKey) -> bool {
240        let mut state = self.state.lock();
241        let Some(entry) = state.scopes.get_mut(scope) else {
242            return false;
243        };
244        let exhausted = entry.reviews >= self.config.max_reviews
245            || entry.failed_consults >= MAX_FAILED_CONSULTS;
246        if exhausted && !entry.exhaustion_logged {
247            entry.exhaustion_logged = true;
248            tracing::info!(
249                target: "libsy",
250                scope = ?scope,
251                "advisor gate: review budget spent; passing through"
252            );
253        }
254        exhausted
255    }
256
257    /// Atomically re-checks exhaustion and reserves one review. Reserving
258    /// before the consult await means concurrent same-scope requests cannot
259    /// overdraw `max_reviews`; a loser returns its buffered turn unreviewed.
260    fn try_reserve(&self, scope: &ScopeKey) -> bool {
261        let mut state = self.state.lock();
262        if state.scopes.len() >= MAX_TRACKED_SCOPES && !state.scopes.contains_key(scope) {
263            let evict = state
264                .scopes
265                .keys()
266                .find(|key| **key != ScopeKey::Instance)
267                .cloned();
268            if let Some(key) = evict {
269                state.scopes.remove(&key);
270            }
271        }
272        let max_reviews = self.config.max_reviews;
273        let entry = state.scopes.entry(scope.clone()).or_default();
274        if entry.reviews >= max_reviews || entry.failed_consults >= MAX_FAILED_CONSULTS {
275            return false;
276        }
277        entry.reviews += 1;
278        true
279    }
280
281    /// Returns a reserved review after a failed consult and counts the
282    /// failure; applied on fail-open *and* fail-closed paths so the failure
283    /// cap bounds both.
284    fn refund_failure(&self, scope: &ScopeKey) {
285        let mut state = self.state.lock();
286        let entry = state.scopes.entry(scope.clone()).or_default();
287        entry.reviews = entry.reviews.saturating_sub(1);
288        entry.failed_consults += 1;
289    }
290
291    /// Drops a completed session's ledger entry; the instance scope persists.
292    fn evict_scope(&self, scope: &ScopeKey) {
293        if *scope == ScopeKey::Instance {
294            return;
295        }
296        self.state.lock().scopes.remove(scope);
297    }
298
299    fn stall_already_fired(&self, key: u64) -> bool {
300        self.state.lock().stall_fired.contains(&key)
301    }
302
303    fn mark_stall_fired(&self, key: u64) {
304        let mut state = self.state.lock();
305        if state.stall_fired.len() >= MAX_TRACKED_SCOPES {
306            let drop = state.stall_fired.iter().next().copied();
307            if let Some(key) = drop {
308                state.stall_fired.remove(&key);
309            }
310        }
311        state.stall_fired.insert(key);
312    }
313
314    // ── Gate flow ───────────────────────────────────────────────────────────
315
316    async fn route_inner(
317        &self,
318        driver: &Driver,
319        request: Request,
320        scope: &ScopeKey,
321    ) -> Result<RoutingOutcome> {
322        // Spent budget (or failure cap): pure passthrough — live stream,
323        // verbatim preserved-body replay, zero buffering. Executor errors
324        // (including ContextWindowExceeded) propagate for the host's
325        // client-visible mapping.
326        if self.check_exhausted(scope) {
327            return Ok(RoutingOutcome::route_to(
328                self.executor.clone(),
329                Vec::new(),
330                request,
331            ));
332        }
333
334        // Gated phase: generate the turn once, fully buffered, so the gate
335        // can inspect it before the client sees anything.
336        let response = driver
337            .call_model(request.clone(), vec![self.executor.clone()])
338            .await?;
339        let turn = buffer_turn(self.executor.as_str(), response).await?;
340
341        // The stall checkpoint fires once per conversation regardless of the
342        // turn's shape — even a tool-call turn — for executors that grind
343        // without ever declaring completion.
344        let stall_key = stall_key(&request);
345        let stall = self.config.gate_stall_turns > 0
346            && !self.stall_already_fired(stall_key)
347            && assistant_turns(&request.llm_request.messages) >= self.config.gate_stall_turns;
348        let triggered = match &self.trigger {
349            CompiledTrigger::Pattern(pattern) => {
350                pattern.is_match(visible_text(&turn.agg).as_deref().unwrap_or(""))
351            }
352            CompiledTrigger::NoToolCall => {
353                !has_tool_use(&turn.agg)
354                    && count_tool_results(&request.llm_request.messages)
355                        >= self.config.gate_min_tool_results
356            }
357        };
358        if !(triggered || stall) {
359            return Ok(RoutingOutcome::answered(
360                self.executor.clone(),
361                request,
362                turn.into_response(),
363            ));
364        }
365        // A stall consumed by a simultaneous trigger does not latch, so the
366        // checkpoint can still fire later if this review is refunded.
367        if stall && !triggered {
368            self.mark_stall_fired(stall_key);
369        }
370        if !self.try_reserve(scope) {
371            return Ok(RoutingOutcome::answered(
372                self.executor.clone(),
373                request,
374                turn.into_response(),
375            ));
376        }
377
378        let trigger_label = match (&self.trigger, triggered) {
379            (CompiledTrigger::Pattern(_), true) => "pattern",
380            (CompiledTrigger::NoToolCall, true) => "no_tool_call",
381            _ => "stall",
382        };
383        let review_tail = visible_text(&turn.agg).or_else(|| {
384            reasoning_text(&turn.agg).map(|reasoning| format!("{REASONING_TAIL_LABEL}{reasoning}"))
385        });
386        match self
387            .consult(driver, &request, review_tail.as_deref(), trigger_label)
388            .await
389        {
390            Ok(ConsultOutcome::Approve) => Ok(RoutingOutcome::answered(
391                self.executor.clone(),
392                request,
393                turn.into_response(),
394            )),
395            Ok(ConsultOutcome::Redo { plan }) => Ok(self.redo(request, turn, &plan)),
396            Ok(ConsultOutcome::Failed) => {
397                self.refund_failure(scope);
398                Ok(RoutingOutcome::answered(
399                    self.executor.clone(),
400                    request,
401                    turn.into_response(),
402                ))
403            }
404            Err(error) => {
405                self.refund_failure(scope);
406                Err(error)
407            }
408        }
409    }
410
411    /// REDO: the client never sees the gated turn. Its text (or reasoning) is
412    /// echoed as an assistant message, the advisor's plan follows as user
413    /// feedback, and the executor continues as a pure passthrough call.
414    fn redo(&self, request: Request, turn: GatedTurn, plan: &str) -> RoutingOutcome {
415        record_discarded(&turn.agg.usage);
416        emit_discarded_audit(self.executor.as_str(), &turn.agg.usage);
417        let echo = visible_text(&turn.agg)
418            .or_else(|| reasoning_text(&turn.agg))
419            .unwrap_or_else(|| EMPTY_ECHO_PLACEHOLDER.to_string());
420        let mut redo = request;
421        redo.llm_request
422            .messages
423            .push(Message::text(Role::Assistant, echo));
424        redo.llm_request.messages.push(Message::text(
425            Role::User,
426            format!("{}{}", self.config.redo_feedback_prefix, plan),
427        ));
428        // Mandatory after any message mutation: codecs otherwise replay the
429        // preserved pre-surgery body verbatim and the feedback never reaches
430        // the executor.
431        crate::algorithms::util::prompts::drop_exact_replay(&mut redo);
432        RoutingOutcome::route_to(self.executor.clone(), Vec::new(), redo)
433    }
434
435    /// Consults the advisor over the buffered transcript and parses the
436    /// verdict. `Ok(Failed)` covers fail-open errors and unparseable replies
437    /// (the caller refunds); fail-closed errors return `Err`.
438    async fn consult(
439        &self,
440        driver: &Driver,
441        base: &Request,
442        review_tail: Option<&str>,
443        trigger: &'static str,
444    ) -> Result<ConsultOutcome> {
445        // The advisor reviews the FULL transcript: system/developer content is
446        // normalized out of `messages` into `instructions`, so prepend it back
447        // as leading messages (identical {role, content} shape) — the task
448        // constraints the verdict must check against usually live there.
449        let transcript_messages: Vec<Message> = base
450            .llm_request
451            .instructions
452            .iter()
453            .map(|block| Message {
454                role: block.role,
455                content: block.content.clone(),
456            })
457            .chain(base.llm_request.messages.iter().cloned())
458            .collect();
459        let transcript = review_transcript(
460            &transcript_messages,
461            review_tail,
462            self.config.transcript_max_chars,
463        );
464        let consult_request = self.build_consult_request(base, transcript);
465        let started = Instant::now();
466        let reply = match driver
467            .call_model(consult_request, vec![self.advisor.clone()])
468            .await
469        {
470            Ok(response) => response
471                .llm_response
472                .into_agg()
473                .await
474                .map_err(|source| LibsyError::client_call(self.advisor.clone(), source)),
475            Err(error) => Err(error),
476        };
477        let latency_ms = started.elapsed().as_secs_f64() * 1000.0;
478        let agg = match reply {
479            Ok(agg) => agg,
480            Err(error) => {
481                record_consult_failure(crate::algorithms::util::llm_judge::libsy_error_reason(
482                    &error,
483                ));
484                if !self.config.fail_open {
485                    // Surface as an algorithm failure (5xx), never as the
486                    // advisor's own client error: a typed ContextWindowExceeded
487                    // from the consult would otherwise reach the client as 400
488                    // context_length_exceeded and trigger compaction of a
489                    // healthy conversation.
490                    return Err(algorithm_error(format!(
491                        "advisor consult failed (fail_open = false): {error}"
492                    )));
493                }
494                tracing::warn!(
495                    target: "libsy",
496                    error = %error,
497                    "advisor gate: consult failed; passing the turn through (fail open)"
498                );
499                emit_review_audit(ReviewAudit {
500                    verdict: "APPROVE",
501                    error: Some(error.to_string()),
502                    latency_ms,
503                    reply_head: None,
504                    usage: None,
505                });
506                return Ok(ConsultOutcome::Failed);
507            }
508        };
509        let reply_text = advisor_reply_text(&agg);
510        let reply_head: String = reply_text.chars().take(160).collect();
511        match parse_verdict(&self.verdict_re, &reply_text) {
512            Some(Verdict::Approve) => {
513                record_review("approve", trigger);
514                emit_review_audit(ReviewAudit {
515                    verdict: "APPROVE",
516                    error: None,
517                    latency_ms,
518                    reply_head: Some(reply_head),
519                    usage: Some(&agg.usage),
520                });
521                Ok(ConsultOutcome::Approve)
522            }
523            Some(Verdict::Redo { plan }) => {
524                record_review("redo", trigger);
525                emit_review_audit(ReviewAudit {
526                    verdict: "REDO",
527                    error: None,
528                    latency_ms,
529                    reply_head: Some(reply_head),
530                    usage: Some(&agg.usage),
531                });
532                Ok(ConsultOutcome::Redo { plan })
533            }
534            None => {
535                // The advisor spent real tokens on a reply the gate cannot
536                // act on; the observer already recorded them. Refunded by
537                // the caller so a flaky advisor cannot burn the budget.
538                record_review("unparseable", trigger);
539                emit_review_audit(ReviewAudit {
540                    verdict: "UNPARSEABLE",
541                    error: None,
542                    latency_ms,
543                    reply_head: Some(reply_head),
544                    usage: Some(&agg.usage),
545                });
546                Ok(ConsultOutcome::Failed)
547            }
548        }
549    }
550
551    /// A fresh, buffered, tool-free request carrying the reviewer contract and
552    /// the serialized transcript; metadata is kept for session correlation.
553    fn build_consult_request(&self, base: &Request, transcript: String) -> Request {
554        Request {
555            llm_request: LlmRequest {
556                model: base.llm_request.model.clone(),
557                instructions: vec![InstructionBlock {
558                    role: Role::System,
559                    content: vec![ContentBlock::Text {
560                        text: self.config.reviewer_system_prompt.clone(),
561                    }],
562                }],
563                messages: vec![Message::text(Role::User, transcript)],
564                sampling: SamplingParams {
565                    temperature: self.config.advisor_temperature,
566                    ..SamplingParams::default()
567                },
568                output: OutputParams {
569                    max_output_tokens: Some(self.config.advisor_max_tokens),
570                    response_format: None,
571                },
572                ..LlmRequest::default()
573            },
574            raw_request: None,
575            metadata: base.metadata.clone(),
576        }
577    }
578}
579
580#[async_trait::async_trait]
581impl Algorithm for AdvisorGate {
582    fn name(&self) -> &str {
583        "advisor_gate"
584    }
585
586    async fn route(self: Arc<Self>, driver: Driver, request: Request) -> Result<RoutingOutcome> {
587        let scope = budget_scope(&request);
588        let session_final = request
589            .metadata
590            .as_ref()
591            .and_then(|metadata| metadata.session_final)
592            == Some(true);
593        let result = self.route_inner(&driver, request, &scope).await;
594        if session_final {
595            self.evict_scope(&scope);
596        }
597        result
598    }
599}
600
601/// Outcome of one consult; `Failed` = fail-open error or unparseable reply.
602enum ConsultOutcome {
603    Approve,
604    Redo { plan: String },
605    Failed,
606}
607
608// ── Budget scope ────────────────────────────────────────────────────────────
609
610/// Resolves the review budget scope: the benchmark harness header wins (it is
611/// stamped on every request of one evaluation, sub-agents included), then the
612/// host-resolved session id, then one shared instance scope.
613fn budget_scope(request: &Request) -> ScopeKey {
614    let metadata = request.metadata.as_ref();
615    if let Some(value) = metadata
616        .and_then(|metadata| metadata.http_headers.as_ref())
617        .and_then(|headers| headers.get(BENCH_SESSION_HEADER))
618        .and_then(|value| value.to_str().ok())
619        && !value.is_empty()
620    {
621        return ScopeKey::Client(value.to_string());
622    }
623    if let Some(id) = metadata.and_then(|metadata| metadata.session_id.as_deref())
624        && !id.is_empty()
625    {
626        return ScopeKey::Session(id.to_string());
627    }
628    ScopeKey::Instance
629}
630
631/// Latches the stall checkpoint per conversation: hash of the first user
632/// message's text, which is constant across a session's turns.
633fn stall_key(request: &Request) -> u64 {
634    let text = request
635        .llm_request
636        .messages
637        .iter()
638        .find(|message| message.role == Role::User)
639        .and_then(|message| message.text_content("\n"))
640        .unwrap_or_default();
641    let mut hasher = DefaultHasher::new();
642    text.hash(&mut hasher);
643    hasher.finish()
644}
645
646fn algorithm_error(message: impl Into<String>) -> LibsyError {
647    LibsyError::AlgorithmError {
648        message: message.into(),
649    }
650}