Skip to main content

causa_runtime/driver/
runner.rs

1//! Public run entry points and ownership of the supplied context.
2
3use super::TurnOutcome;
4use crate::{ToolExecutor, TurnRunOptions};
5use causa_kernel::{CallControl, Context, ModelGateway, TurnId};
6use std::sync::Arc;
7
8/// Drives logical model rounds over caller-owned materials.
9///
10/// Only fresh declarations from each accepted model output are executed.
11/// Preparation and observation are supplied per run; tool policy belongs to
12/// the executor. Controlled stops return materials, including the complete
13/// current batch when its results have not committed.
14pub struct TurnRunner {
15    pub(super) gateway: Arc<dyn ModelGateway>,
16    pub(super) executor: Arc<ToolExecutor>,
17}
18impl TurnRunner {
19    /// Assemble the two execution dependencies.
20    pub fn new(gateway: Arc<dyn ModelGateway>, executor: Arc<ToolExecutor>) -> Self {
21        Self { gateway, executor }
22    }
23
24    /// Run using complete logical gateway calls.
25    pub async fn run(
26        &self,
27        turn_id: TurnId,
28        context: Context,
29        options: TurnRunOptions,
30        control: CallControl,
31    ) -> TurnOutcome {
32        self.execute(turn_id, context, options, control, false)
33            .await
34    }
35    /// Run using streaming gateway calls, accepting only complete `Done` output.
36    pub async fn run_streaming(
37        &self,
38        turn_id: TurnId,
39        context: Context,
40        options: TurnRunOptions,
41        control: CallControl,
42    ) -> TurnOutcome {
43        self.execute(turn_id, context, options, control, true).await
44    }
45    #[tracing::instrument(name = "agent.turn", skip_all, fields(turn_id = %turn_id.0, model = %options.model.0))]
46    async fn execute(
47        &self,
48        turn_id: TurnId,
49        mut context: Context,
50        options: TurnRunOptions,
51        control: CallControl,
52        streaming: bool,
53    ) -> TurnOutcome {
54        let (result, uncommitted_tool_batch) = self
55            .run_loop(&turn_id, &mut context, &options, &control, streaming)
56            .await;
57        TurnOutcome {
58            turn_id,
59            context,
60            result,
61            uncommitted_tool_batch,
62        }
63    }
64}