switchyard-libsy 0.4.0

Provider-neutral multi-LLM routing and orchestration for Switchyard
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! The classifier that runs one verification-gated turn.
//!
//! A proposed tool call is judged against the session's trajectory; a final
//! answer is typed, judged by rungs that run cheapest-first and stop once the
//! decision is settled, and then committed or escalated. Bounded mode shares one
//! deadline across calls. A failed or timed-out call supplies no evidence.

use std::future::Future;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use switchyard_protocol::{
    AggLlmResponse, ContentBlock, Message, ModelId, Request, Response, StopReason,
};

use super::config::{ServingMode, VgrConfig};
use super::decide::{self, AgenticRun, Route, Signals, Tri};
use super::rungs::{self, Question};
use super::safety::{CircuitBreaker, endpoint_failure, fallback_eligible};
use super::{Branch, Capabilities, TaskType, derive_capabilities, readout, text};
use crate::algorithms::util::affinity::{AffinityRouter, has_new_user_turn, initiated_by_user};
use crate::algorithms::util::buffered_response::buffer_response;
use crate::algorithms::util::decisive;
use crate::algorithms::util::prompts::append_note;
use crate::core::algorithm::{AnswerSummary, Driver};
use crate::core::classifier::{Classification, Classifier};
use crate::core::state::{State, StateValue};
use crate::{LibsyError, Result};

/// Whether turn verification latched the capable tier; cleared when the user responds.
const TURN_LATCHED_KEY: &str = "vgr_turn_latched";
/// Holds a shadow turn only after a VGR escalation.
const SHADOW_HELD_KEY: &str = "vgr_shadow_held";
const LAST_ANSWER_FINAL_KEY: &str = "vgr_last_answer_final";
const FOLLOW_UP_QUIET_SECS: u32 = 2;
pub(super) const TURN_STREAK_KEY: &str = "vgr_turn_streak";
/// Unix seconds at the first request of the current user turn.
const TURN_STARTED_KEY: &str = "vgr_turn_started";
/// Set once the handoff note was sent in the current user turn.
const HANDOFF_SENT_KEY: &str = "vgr_handoff_sent";
/// Set once the efficient tier served a response since the last handoff.
pub(super) const EFFICIENT_SERVED_KEY: &str = "vgr_local_served";

fn count(state: &State, key: &str) -> Option<u32> {
    match state.extra.get(key) {
        Some(StateValue::Count(n)) => Some(*n),
        _ => None,
    }
}

fn set_count(state: &mut State, key: &str, value: u32) {
    state
        .extra
        .insert(key.to_string(), StateValue::Count(value));
}

pub(super) fn set_flag(state: &mut State, key: &str, on: bool) {
    state.extra.insert(key.to_string(), StateValue::Bool(on));
}

fn flag(state: &State, key: &str) -> bool {
    matches!(state.extra.get(key), Some(StateValue::Bool(true)))
}

pub(super) fn turn_streak(state: &State) -> u32 {
    count(state, TURN_STREAK_KEY).unwrap_or(0)
}

struct RequestContext<'a> {
    driver: &'a Driver,
    request: &'a Request,
    started: Instant,
}

/// Consecutive escalation votes that latch a session to the capable tier.
pub(super) const TURN_CONFIRMATIONS: u32 = 2;
const TURN_ESCALATE_AT: f64 = 0.5;

type Scored = (Classification, Option<Response>);

pub(super) struct VgrClassifier {
    pub(super) config: VgrConfig,
    pub(super) breaker: CircuitBreaker,
}

/// An escalation hold, recorded as a VGR decision with its reason whenever it serves a call.
pub(super) struct Held(pub(super) Arc<AffinityRouter>, pub(super) &'static str);

#[async_trait]
impl Classifier for Held {
    async fn score(
        &self,
        state: &mut State,
        request: &mut Request,
        driver: &Driver,
    ) -> Result<Scored> {
        let scored = self.0.score(state, request, driver).await?;
        if matches!(&scored.0, Classification::Scores(scores) if !scores.is_empty()) {
            log(driver, self.1, None, Route::Capable);
        }
        Ok(scored)
    }
}

#[async_trait]
impl Classifier for VgrClassifier {
    async fn score(
        &self,
        state: &mut State,
        request: &mut Request,
        driver: &Driver,
    ) -> Result<Scored> {
        let started = Instant::now();
        let efficient = &self.config.targets.efficient;
        let messages = &request.llm_request.messages;
        let (new_turn, answered) = match initiated_by_user(request) {
            Some(by_user) => (by_user, by_user),
            None => {
                let answered = answers_final_reply(state, messages);
                let new_turn = if state.extra.contains_key(LAST_ANSWER_FINAL_KEY) {
                    answered
                } else {
                    has_new_user_turn(messages)
                };
                (new_turn, answered)
            }
        };
        if new_turn {
            state.extra.remove(TURN_STARTED_KEY);
            state.extra.remove(SHADOW_HELD_KEY);
            set_flag(state, HANDOFF_SENT_KEY, false);
        }
        if answered {
            set_flag(state, TURN_LATCHED_KEY, false);
            set_count(state, TURN_STREAK_KEY, 0);
        }
        let turn_budget_spent = self.turn_budget_spent(state);
        let short_circuit = if flag(state, SHADOW_HELD_KEY) {
            Some("turn_verification_latched")
        } else if self.config.mode == ServingMode::Off {
            Some("mode_off")
        } else if self
            .config
            .kill_switch
            .as_ref()
            .is_some_and(|switch| switch.is_engaged())
        {
            Some("kill_switch")
        } else if flag(state, TURN_LATCHED_KEY) || turn_streak(state) >= TURN_CONFIRMATIONS {
            Some("turn_verification_latched")
        } else if turn_budget_spent {
            Some("efficient_turn_budget")
        } else if !self.config.efficient_supports_attachments
            && !text::attachments(request).is_empty()
        {
            Some("efficient_attachment_unsupported")
        } else if text::newest_prompt_is_blank(request) {
            Some("empty_prompt")
        } else {
            None
        };
        if let Some(reason) = short_circuit {
            return Ok(self.escalate(driver, reason, None, request, state, None));
        }

        if self.config.bounded_efficient && self.remaining(started).is_none() {
            return Ok(self.escalate(driver, "efficient_timed_out", None, request, state, None));
        }
        let Some(trial) = self.breaker.attempt() else {
            return Ok(self.escalate(driver, "breaker_open", None, request, state, None));
        };
        let typed = if text::ToolRecord::from_request(request).errors > 0 {
            let context = RequestContext {
                driver,
                request,
                started,
            };
            Some(self.type_task(&context).await)
        } else {
            None
        };
        if typed == Some(Some(TaskType::Chat)) {
            let reason = "chat_after_tool_error";
            return Ok(self.escalate(driver, reason, Some(Branch::Chat), request, state, None));
        }
        if self.config.bounded_efficient && self.remaining(started).is_none() {
            return Ok(self.escalate(driver, "efficient_timed_out", None, request, state, None));
        }
        let attempted = self
            .within_budget(started, async {
                let response = driver
                    .call_model_with_error_recovery(request.clone(), vec![efficient.clone()], true)
                    .await?;
                buffer_response(efficient.as_str(), response).await
            })
            .await;
        let buffered = match attempted {
            Some(Ok(buffered)) => {
                trial.success();
                buffered
            }
            Some(Err(error)) => {
                driver.record_model_failure(efficient.clone());
                if endpoint_failure(&error) {
                    trial.failure();
                }
                if !fallback_eligible(&error, !text::attachments(request).is_empty()) {
                    return Err(error);
                }
                return Ok(self.escalate(
                    driver,
                    "efficient_unavailable",
                    None,
                    request,
                    state,
                    None,
                ));
            }
            None => {
                driver.record_model_failure(efficient.clone());
                trial.failure();
                return Ok(self.escalate(
                    driver,
                    "efficient_timed_out",
                    None,
                    request,
                    state,
                    None,
                ));
            }
        };

        let context = RequestContext {
            driver,
            request,
            started,
        };
        if rungs::has_tool_call(&buffered.agg) {
            if !tool_use_is_complete(&buffered.agg) {
                return Ok(self.escalate(
                    driver,
                    "malformed_tool_call",
                    None,
                    request,
                    state,
                    None,
                ));
            }
            if self.verify_turn(&context, &buffered.agg, state).await {
                return Ok(self.escalate(
                    driver,
                    "turn_verification_escalated",
                    None,
                    request,
                    state,
                    None,
                ));
            }
            if self.config.mode == ServingMode::Shadow {
                return Ok(self.serve_capable(
                    driver,
                    "turn_verification_complete",
                    None,
                    request,
                    state,
                    None,
                ));
            }
            log(driver, "turn_verification_complete", None, Route::Efficient);
            set_flag(state, EFFICIENT_SERVED_KEY, true);
            return Ok((decisive(efficient), Some(buffered.into_response())));
        }

        let attempt = rungs::response_text(&buffered.agg).unwrap_or_default();
        let task_type = match typed {
            Some(task_type) => task_type,
            None => self.type_task(&context).await,
        };
        let caps = derive_capabilities(
            request,
            &attempt,
            task_type,
            self.config.efficient_supports_attachments,
        );
        let signals = self.gather(&context, &caps).await;
        let route = decide::decide(&caps, &signals, self.config.confirmed_min_clean_tail());
        if route == Route::Capable {
            return Ok(self.escalate(
                driver,
                "decided",
                Some(caps.branch),
                request,
                state,
                Some(&attempt),
            ));
        }
        if self.config.mode == ServingMode::Shadow {
            return Ok(self.serve_capable(
                driver,
                "decided",
                Some(caps.branch),
                request,
                state,
                Some(&attempt),
            ));
        }
        log(driver, "decided", Some(caps.branch), Route::Efficient);
        set_flag(state, EFFICIENT_SERVED_KEY, true);
        Ok((decisive(efficient), Some(buffered.into_response())))
    }

    fn tracks_answers(&self) -> bool {
        true
    }

    fn observe_answer(&self, state: &mut State, answer: AnswerSummary) {
        let finished = if answer.final_reply {
            unix_seconds().max(1)
        } else {
            0
        };
        set_count(state, LAST_ANSWER_FINAL_KEY, finished);
    }
}

impl VgrClassifier {
    /// Whether the efficient tier may answer `request` when the capable tier fails it.
    ///
    /// That answer is unverified, so it is offered only while efficient attempts are allowed.
    pub(super) fn efficient_may_stand_in(&self, request: &Request) -> bool {
        matches!(self.config.mode, ServingMode::Active { .. })
            && !self
                .config
                .kill_switch
                .as_ref()
                .is_some_and(|switch| switch.is_engaged())
            && !self.breaker.is_open()
            && (self.config.efficient_supports_attachments || text::attachments(request).is_empty())
    }

    fn escalate(
        &self,
        driver: &Driver,
        reason: &'static str,
        branch: Option<Branch>,
        request: &mut Request,
        state: &mut State,
        unverified_final: Option<&str>,
    ) -> Scored {
        if self.config.mode == ServingMode::Shadow {
            set_flag(state, SHADOW_HELD_KEY, true);
        }
        self.serve_capable(driver, reason, branch, request, state, unverified_final)
    }

    fn serve_capable(
        &self,
        driver: &Driver,
        reason: &'static str,
        branch: Option<Branch>,
        request: &mut Request,
        state: &mut State,
        unverified_final: Option<&str>,
    ) -> Scored {
        if reason != "mode_off" {
            self.hand_off(request, state, unverified_final);
        }
        log(driver, reason, branch, Route::Capable);
        (decisive(&self.config.targets.capable), None)
    }

    /// Whether the current user turn has used up its efficient wall-clock budget.
    ///
    /// The clock starts at the first request of the user turn and includes the
    /// client's tool execution, since that is the time the client's own limit
    /// is spending.
    fn turn_budget_spent(&self, state: &mut State) -> bool {
        if !self.config.bounded_efficient {
            return false;
        }
        let Some(budget) = self.config.efficient_turn_budget else {
            return false;
        };
        let now = unix_seconds();
        let started = count(state, TURN_STARTED_KEY).unwrap_or(now);
        set_count(state, TURN_STARTED_KEY, started);
        u64::from(now.saturating_sub(started)) >= budget.as_secs()
    }

    /// Tells the capable tier it is inheriting unverified tool-using work.
    ///
    /// Sent at most once per user turn, on its first escalation.
    fn hand_off(&self, request: &mut Request, state: &mut State, unverified_final: Option<&str>) {
        if !self.config.agentic_handoff
            || flag(state, HANDOFF_SENT_KEY)
            || !flag(state, EFFICIENT_SERVED_KEY)
            || !text::turn_has_tool_trajectory(request)
        {
            return;
        }
        set_flag(state, HANDOFF_SENT_KEY, true);
        set_flag(state, EFFICIENT_SERVED_KEY, false);
        let note = handoff_note(unverified_final);
        if !(self.config.compact_handoff && super::compaction::compact(request, state, &note)) {
            append_note(&mut request.llm_request, &note);
        }
    }

    /// Judges one proposed tool call; `true` escalates the rest of the session.
    async fn verify_turn(
        &self,
        context: &RequestContext<'_>,
        proposed: &AggLlmResponse,
        state: &mut State,
    ) -> bool {
        let mut call = rungs::build_turn_request(
            rungs::turn_view(context.request, proposed),
            readout::MAX_OUTPUT_TOKENS,
            context.request.metadata.clone(),
        );
        readout::request_logprobs(&mut call);
        let judge = self.config.judge();
        let probability = match self.call(context, call, judge).await {
            None => return false,
            Some(Ok(agg)) => readout::p_yes(&agg),
            // Verification fails open, unless the efficient endpoint itself is down.
            Some(Err(error)) => {
                if *judge == self.config.targets.efficient && endpoint_failure(&error) {
                    self.breaker.failure();
                    set_flag(state, TURN_LATCHED_KEY, true);
                    return true;
                }
                return false;
            }
        };
        let Some(probability) = probability else {
            return false;
        };
        if probability < TURN_ESCALATE_AT {
            set_count(state, TURN_STREAK_KEY, 0);
            return false;
        }
        let streak = turn_streak(state).saturating_add(1);
        set_count(state, TURN_STREAK_KEY, streak);
        if streak >= TURN_CONFIRMATIONS {
            set_flag(state, TURN_LATCHED_KEY, true);
        }
        streak >= TURN_CONFIRMATIONS
    }

    /// Types the request; abstains, selecting the default regime, on any failure.
    async fn type_task(&self, context: &RequestContext<'_>) -> Option<TaskType> {
        if !self.config.task_typing {
            return None;
        }
        // Without a textual task, keep the default verification regime.
        if context
            .request
            .llm_request
            .messages
            .iter()
            .rev()
            .find(|message| message.role == switchyard_protocol::Role::User)
            .is_some_and(|message| {
                message.content.iter().all(|block| match block {
                    ContentBlock::Text { text } => text.trim().is_empty(),
                    ContentBlock::Image { .. } => true,
                    _ => false,
                })
            })
        {
            return None;
        }
        let (turns, _) = text::turns(context.request);
        let task = text::user_task_text(&turns);
        if task.trim().is_empty() {
            return None;
        }
        let call = rungs::build_typing_request(task, context.request.metadata.clone());
        let agg = self.call(context, call, self.config.judge()).await?.ok()?;
        rungs::parse_task_type(&agg)
    }

    /// Gathers evidence cheapest-first, stopping once the decision commits.
    async fn gather(&self, context: &RequestContext<'_>, caps: &Capabilities) -> Signals {
        let mut signals = Signals::default();
        let branch = caps.branch;
        let confirmed = self.config.confirmed_min_clean_tail();
        let run = (branch == Branch::Agentic).then(|| decide::agentic_run(caps, confirmed));
        let (Some(judged), false) = (
            caps.transcript.as_deref(),
            branch == Branch::Unknown
                || run == Some(AgenticRun::Vetoed)
                || (matches!(branch, Branch::Chat | Branch::DefaultVerified)
                    && caps.tools.is_some_and(|tools| tools.errors > 0)),
        ) else {
            return signals;
        };
        let settled =
            |signals: &Signals| decide::decide(caps, signals, confirmed) == Route::Efficient;
        let capable_confirms = run == Some(AgenticRun::ConfirmedRecovery);
        let efficient = self.config.judge();
        let ask = |target, question, max_output_tokens| {
            self.ask(context, target, question, judged, max_output_tokens)
        };

        // Coding here ran no tools, and a typed answer cites none, so neither record holds
        // evidence: judge the answer itself.
        let asked = match branch {
            Branch::Coding | Branch::Answer => Question::Answer,
            _ => Question::Evidence,
        };
        signals.readout = self.readout(context, judged, asked).await;
        if branch == Branch::Coding {
            if let Some(capable) = self.config.targets.capable_judge.as_ref()
                && decide::efficient_verified(branch, &signals)
                && !settled(&signals)
            {
                let tokens = rungs::DELIBERATION_MAX_OUTPUT_TOKENS;
                signals.capable_judge = Some(ask(capable, asked, tokens).await);
            }
            return signals;
        }
        if settled(&signals) {
            return signals;
        }
        // A deliberation never commits a typed answer, so the answer branch goes to the capable tier.
        if branch != Branch::Answer
            && !(capable_confirms && decide::efficient_verified(branch, &signals))
        {
            let metadata = || context.request.metadata.clone();
            let call = rungs::build_deliberation_request(judged, metadata());
            if let Some(Ok(deliberation)) = self.call(context, call, efficient).await {
                let call = rungs::build_verdict_request(judged, &deliberation, metadata());
                let verdict = self
                    .call(context, call, efficient)
                    .await
                    .and_then(Result::ok);
                signals.deliberation = verdict.and_then(|agg| readout::p_yes(&agg));
            }
            if settled(&signals) {
                return signals;
            }
        }
        let Some(capable) = self.config.targets.capable_judge.as_ref() else {
            return signals;
        };
        let capable_ask = |question| ask(capable, question, rungs::DELIBERATION_MAX_OUTPUT_TOKENS);
        if capable_confirms {
            if decide::efficient_verified(branch, &signals) {
                signals.capable_judge = Some(capable_ask(Question::Evidence).await);
            }
            return signals;
        }
        match branch {
            Branch::Answer => {
                signals.answer_verifier = Some(capable_ask(Question::Answer).await);
                if !settled(&signals) {
                    signals.evidence_verifier = Some(capable_ask(Question::Evidence).await);
                }
            }
            Branch::Chat | Branch::DefaultVerified => {
                signals.capable_judge = Some(capable_ask(Question::Evidence).await);
                // Asked only once the first affirms, so a refutation costs one call.
                if signals.capable_judge == Some(Tri::Yes) {
                    signals.evidence_confirm = Some(capable_ask(Question::Answer).await);
                }
            }
            _ => {}
        }
        signals
    }

    async fn readout(
        &self,
        context: &RequestContext<'_>,
        judged: &str,
        question: Question,
    ) -> Option<f64> {
        let mut call = rungs::build_request(
            question,
            judged,
            readout::MAX_OUTPUT_TOKENS,
            context.request.metadata.clone(),
        );
        readout::request_logprobs(&mut call);
        let agg = self.call(context, call, self.config.judge()).await?.ok()?;
        readout::p_yes(&agg)
    }

    async fn ask(
        &self,
        context: &RequestContext<'_>,
        target: &ModelId,
        question: Question,
        judged: &str,
        max_output_tokens: u64,
    ) -> Tri {
        let call = rungs::build_request(
            question,
            judged,
            max_output_tokens,
            context.request.metadata.clone(),
        );
        match self.call(context, call, target).await {
            Some(Ok(agg)) => rungs::parse_verdict(&agg),
            _ => Tri::Unknown,
        }
    }

    /// One verifier call inside the remaining budget; `None` once it is spent.
    async fn call(
        &self,
        context: &RequestContext<'_>,
        mut call: Request,
        target: &ModelId,
    ) -> Option<Result<AggLlmResponse>> {
        if self.config.efficient_supports_attachments
            && let Some(message) = call.llm_request.messages.last_mut()
        {
            let attachments = text::attachments(context.request);
            if !attachments.is_empty() {
                message.content.push(ContentBlock::Text {
                    text: "[request attachments, in conversation order]".into(),
                });
                message.content.extend(attachments.into_iter().cloned());
            }
        }
        self.within_budget(context.started, async {
            let response = context
                .driver
                .call_model_with_error_recovery(call, vec![target.clone()], true)
                .await?;
            response
                .llm_response
                .into_agg()
                .await
                .map_err(|source| LibsyError::client_call(target.clone(), source))
        })
        .await
    }

    /// Waits for a candidate or verifier, enforcing a shared deadline only in bounded mode.
    async fn within_budget<T>(
        &self,
        started: Instant,
        operation: impl Future<Output = T>,
    ) -> Option<T> {
        if !self.config.bounded_efficient {
            return Some(operation.await);
        }
        tokio::time::timeout(self.remaining(started)?, operation)
            .await
            .ok()
    }

    fn remaining(&self, started: Instant) -> Option<Duration> {
        self.config.deadline.checked_sub(started.elapsed())
    }
}

fn unix_seconds() -> u32 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |elapsed| {
            u32::try_from(elapsed.as_secs()).unwrap_or(u32::MAX)
        })
}

/// The note a capable tier receives when it takes over a tool-using session.
fn handoff_note(unverified_final: Option<&str>) -> String {
    let mut note = String::from(
        "\n\n[Routing notice from the serving infrastructure, not from the user] Until now the \
         work on the user's current request was carried out by a smaller, faster model. That work \
         has NOT been verified: it may be incomplete or incorrect, and it may have left the \
         environment in a broken state. You are taking over from here. Do not assume that any \
         step taken on that request succeeded or that the task is finished. Inspect the actual \
         current state of the environment against every requirement of the request, fix \
         whatever is missing or wrong, and give your final answer only after you have confirmed \
         that the requirements are met. Earlier steps may already have done part of the work. \
         Keep what they did and build on it: confirm a step's effect by inspecting the \
         environment rather than by running the step again, and repeat or reverse a step only \
         when the request cannot be met otherwise. Replies to the user's earlier requests are \
         not in question; do not revisit or correct them unless the user asks.",
    );
    if let Some(claim) = unverified_final.filter(|claim| !claim.trim().is_empty()) {
        const CLIPPING_MARKER_RESERVE: usize = 64;
        let claim = text::clip_mid(
            claim,
            super::render::ATTEMPT_BUDGET.saturating_sub(CLIPPING_MARKER_RESERVE),
            1.0 / 3.0,
        );
        note.push_str(
            "\n\nThe smaller model was about to finish with the following unchecked message:\n\
             <unverified_final_message>\n",
        );
        note.push_str(&claim);
        note.push_str("\n</unverified_final_message>");
    }
    note
}

fn log(driver: &Driver, reason: &'static str, branch: Option<Branch>, served: Route) {
    driver.set_evidence(serde_json::json!({
        "source": "vgr",
        "reason_code": reason,
        "verdict": if served == Route::Efficient { "efficient" } else { "capable" },
        "branch": branch.map(|branch| format!("{branch:?}")),
        "policy_version": decide::POLICY_VERSION,
    }));
    tracing::info!(
        target: "libsy",
        reason,
        branch = ?branch,
        served = ?served,
        policy_version = decide::POLICY_VERSION,
        "vgr decision"
    );
}

/// Whether the user is answering a finished assistant reply.
fn answers_final_reply(state: &State, messages: &[Message]) -> bool {
    if let Some(finished) = count(state, LAST_ANSWER_FINAL_KEY)
        && (finished == 0 || unix_seconds().saturating_sub(finished) < FOLLOW_UP_QUIET_SECS)
    {
        return false;
    }
    messages
        .len()
        .checked_sub(1)
        .is_some_and(|last| text::starts_user_turn(messages, last))
}

fn tool_use_is_complete(response: &AggLlmResponse) -> bool {
    response.outputs.iter().all(|output| {
        output.stop_reason != Some(StopReason::MaxTokens)
            && output.content.iter().all(|block| {
                !matches!(block, ContentBlock::ToolCall(call) if call.arguments.is_string())
            })
    })
}

#[cfg(test)]
mod tests;