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
//! Narrow authority for empirical provider-contract probes.
//!
//! Production dispatch lets the capability catalog reject or rewrite options
//! that a route declares unsupported. A contract probe must send exactly one
//! selected option through those local guards so the provider, rather than the
//! catalog, supplies the observation. Selection is typed and task-local so
//! concurrent calls cannot inherit it; extraction then carries the selected
//! option in the resolved call across spawned transport work.
use super::capabilities::PortableOption;
tokio::task_local! {
static PORTABLE_OPTION: PortableOption;
}
/// Run one provider-contract probe with catalog shaping suspended for exactly
/// the selected portable option.
pub async fn with_portable_option_probe<F>(option: PortableOption, future: F) -> F::Output
where
F: std::future::Future,
{
PORTABLE_OPTION.scope(option, future).await
}
/// Return the typed probe selection while call options are being extracted.
pub(crate) fn current_portable_option() -> Option<PortableOption> {
PORTABLE_OPTION.try_with(|selected| *selected).ok()
}
/// Whether catalog policy may reject, omit, or rewrite an explicit portable
/// option before the provider sees it.
///
/// Production calls always return true. A probe returns false only for its
/// selected option; every unrelated guard remains active.
pub(crate) fn catalog_may_shape_requested_portable_option(
selected: Option<PortableOption>,
option: PortableOption,
) -> bool {
selected != Some(option)
}
/// Whether the current call must stop after its first physical provider request.
///
/// Ordinary calls keep the runtime's bounded empty-output and transport
/// recoveries. A contract probe needs one request to equal one observation so
/// its budget and request-count receipt cannot under-report provider traffic.
pub(crate) fn requires_single_request(selected: Option<PortableOption>) -> bool {
selected.is_some()
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn probe_authority_is_typed_exact_and_task_local() {
use PortableOption::{Temperature, TopP};
assert!(catalog_may_shape_requested_portable_option(
None,
Temperature
));
assert!(!requires_single_request(None));
let (temperature_task, top_p_task) = tokio::join!(
with_portable_option_probe(Temperature, async {
tokio::task::yield_now().await;
let opts = crate::llm::api::LlmCallOptions {
provider_contract_probe: current_portable_option(),
..Default::default()
};
let payload = crate::llm::api::LlmRequestPayload::from(&opts);
assert_eq!(payload.provider_contract_probe, Some(Temperature));
(
catalog_may_shape_requested_portable_option(
current_portable_option(),
Temperature,
),
catalog_may_shape_requested_portable_option(current_portable_option(), TopP),
requires_single_request(current_portable_option()),
)
}),
with_portable_option_probe(TopP, async {
tokio::task::yield_now().await;
(
catalog_may_shape_requested_portable_option(
current_portable_option(),
Temperature,
),
catalog_may_shape_requested_portable_option(current_portable_option(), TopP),
requires_single_request(current_portable_option()),
)
}),
);
assert_eq!(temperature_task, (false, true, true));
assert_eq!(top_p_task, (true, false, true));
assert!(catalog_may_shape_requested_portable_option(
None,
Temperature
));
assert!(!requires_single_request(None));
}
}