Skip to main content

codetether_agent/session/context/
policy.rs

1//! Policy dispatcher — routes to the chosen [`DerivePolicy`] implementation.
2
3use std::sync::Arc;
4
5use anyhow::Result;
6use tokio::sync::mpsc;
7
8use crate::provider::ToolDefinition;
9use crate::session::Session;
10use crate::session::SessionEvent;
11use crate::session::derive_policy::DerivePolicy;
12
13use super::derive::derive_context;
14use super::helpers::DerivedContext;
15use super::reset::derive_reset;
16
17/// Derive an ephemeral [`DerivedContext`] under a chosen [`DerivePolicy`].
18///
19/// Generalisation of [`derive_context`] that accepts a policy selector.
20/// [`DerivePolicy::Legacy`] delegates back to `derive_context` so the
21/// two signatures co-exist without behaviour drift during rollout.
22///
23/// # Arguments
24///
25/// Same as [`derive_context`], plus:
26///
27/// * `policy` — Which derivation strategy to run. See [`DerivePolicy`].
28///
29/// # Errors
30///
31/// Propagates any error from the underlying pipeline.
32pub async fn derive_with_policy(
33    session: &Session,
34    provider: Arc<dyn crate::provider::Provider>,
35    model: &str,
36    system_prompt: &str,
37    tools: &[ToolDefinition],
38    event_tx: Option<&mpsc::Sender<SessionEvent>>,
39    policy: DerivePolicy,
40) -> Result<DerivedContext> {
41    match policy {
42        DerivePolicy::Legacy => {
43            derive_context(
44                session,
45                provider,
46                model,
47                system_prompt,
48                tools,
49                event_tx,
50                None,
51            )
52            .await
53        }
54        DerivePolicy::Reset { threshold_tokens } => {
55            derive_reset(
56                session,
57                provider,
58                model,
59                system_prompt,
60                tools,
61                threshold_tokens,
62            )
63            .await
64        }
65    }
66}