behest 0.3.3

A Rust-native cloud agent runtime with typed tools, pluggable memory, queues, and observability.
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
//! Atomic reasoning operators and the operator trait.
//!
//! Operators are the atomic units of reasoning. Each operator receives
//! an [`OperatorContext`] and a [`TaskState`], and produces a new [`TaskState`].
//!
//! Every operator uses an [`LlmProvider`] to generate content. An optional
//! [`ChatLlmProvider`] wrapping the runtime's [`ChatProvider`] is the
//! canonical implementation.
//!
//! [`ChatLlmProvider`]: crate::reasoning::llm_provider::ChatLlmProvider
//! [`ChatProvider`]: crate::provider::ChatProvider

pub mod act;
pub mod analyze;
pub mod answer;
pub mod compose;
pub mod custom;
pub mod decompose;
pub mod diagnose;
pub mod evaluate;
pub mod explain;
pub mod generate;
pub mod hypothesize;
pub mod observe;
pub mod optimize;
pub mod plan;
pub mod prioritize;
pub mod reason;
pub mod reduce;
pub mod reflect;
pub mod revise;
pub mod search;
pub mod select;
pub mod simulate;
pub mod solve;
pub mod synthesize;
pub mod test_op;
pub mod verify;

pub use self::act::ActOperator;
pub use self::analyze::AnalyzeOperator;
pub use self::answer::AnswerOperator;
pub use self::compose::ComposeOperator;
pub use self::custom::CustomOperator;
pub use self::decompose::DecomposeOperator;
pub use self::diagnose::DiagnoseOperator;
pub use self::evaluate::EvaluateOperator;
pub use self::explain::ExplainOperator;
pub use self::generate::GenerateOperator;
pub use self::hypothesize::HypothesizeOperator;
pub use self::observe::ObserveOperator;
pub use self::optimize::OptimizeOperator;
pub use self::plan::PlanOperator;
pub use self::prioritize::PrioritizeOperator;
pub use self::reason::ReasonOperator;
pub use self::reduce::ReduceOperator;
pub use self::reflect::ReflectOperator;
pub use self::revise::ReviseOperator;
pub use self::search::SearchOperator;
pub use self::select::SelectOperator;
pub use self::simulate::SimulateOperator;
pub use self::solve::SolveOperator;
pub use self::synthesize::SynthesizeOperator;
pub use self::test_op::TestOperator;
pub use self::verify::VerifyOperator;

use std::fmt::Write;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use crate::reasoning::error::ReasoningError;
use crate::reasoning::state::{Step, StepStatus, TaskState};
use crate::runtime::invocation::{Control, RuntimeInvocation};

// ---------------------------------------------------------------------------
// OperatorKind
// ---------------------------------------------------------------------------

/// The kind of atomic reasoning operation.
///
/// Each variant corresponds to a fundamental reasoning primitive.
/// Strategies compose these primitives into reasoning graphs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum OperatorKind {
    /// Analyze the current state to extract structure or insights.
    Analyze,
    /// Decompose a complex goal into sub-goals.
    Decompose,
    /// Produce a plan for achieving the goal.
    Plan,
    /// Apply logical reasoning to the current state.
    Reason,
    /// Execute an action (typically a tool call).
    Act,
    /// Observe the result of an action.
    Observe,
    /// Solve a specific sub-problem.
    Solve,
    /// Verify that a result satisfies constraints.
    Verify,
    /// Reflect on progress and identify improvements.
    Reflect,
    /// Revise the current plan based on reflection.
    Revise,
    /// Select the best option from alternatives.
    Select,
    /// Compose partial results into a coherent whole.
    Compose,
    /// Reduce multiple results into a single result.
    Reduce,
    /// Explain reasoning process or results.
    Explain,
    /// Evaluate quality of options or results.
    Evaluate,
    /// Generate new options or ideas.
    Generate,
    /// Propose a hypothesis.
    Hypothesize,
    /// Test a hypothesis or solution.
    Test,
    /// Search for relevant information.
    Search,
    /// Synthesize information from multiple sources.
    Synthesize,
    /// Determine priority of tasks or options.
    Prioritize,
    /// Optimize an existing solution.
    Optimize,
    /// Simulate possible outcomes.
    Simulate,
    /// Diagnose problem causes.
    Diagnose,
    /// Produce the final answer.
    Answer,
}

impl OperatorKind {
    /// Returns `true` if this operator is a terminal operator (produces a final result).
    #[must_use]
    pub const fn is_terminal(&self) -> bool {
        matches!(self, Self::Answer)
    }

    /// Returns `true` if this operator typically requires tool access.
    #[must_use]
    pub const fn requires_tools(&self) -> bool {
        matches!(
            self,
            Self::Act | Self::Observe | Self::Search | Self::Test | Self::Simulate
        )
    }
}

// ---------------------------------------------------------------------------
// LlmProvider
// ---------------------------------------------------------------------------

/// LLM provider abstraction for reasoning operators.
///
/// This is a simplified interface designed for operator-level LLM calls.
/// The canonical implementation wraps [`ChatProvider`](crate::provider::ChatProvider).
#[async_trait]
pub trait LlmProvider: Send + Sync {
    /// Calls the LLM with a user prompt and optional system prompt.
    ///
    /// # Errors
    ///
    /// Returns [`ReasoningError`] on provider failure.
    async fn complete(&self, prompt: &str, system: Option<&str>) -> Result<String, ReasoningError>;
}

// ---------------------------------------------------------------------------
// OperatorContext
// ---------------------------------------------------------------------------

/// Read-only context provided to operators during execution.
///
/// Operators use this to gather information (invoke sub-sessions via the
/// runtime facade, check cancellation state, access memory) but do **not**
/// control the execution flow. The operator's output is always a new
/// `TaskState` — side effects are mediated by the runtime.
pub struct OperatorContext<'a> {
    /// The runtime invocation facade — operators use this to access providers,
    /// tools, and run sub-sessions.
    pub invocation: Option<&'a RuntimeInvocation>,
    /// Cooperative cancellation/control handle.
    pub control: Option<&'a Control>,
    /// A memory store for operators that need to read/write memory.
    pub memory: Option<&'a dyn MemoryStore>,
    /// An LLM provider for generating content during operator execution.
    pub llm: Option<&'a dyn LlmProvider>,
}

/// Minimal memory store interface for operator context.
///
/// This is a read-oriented view — operators can query memory but
/// write-back is mediated by the runtime.
#[async_trait]
pub trait MemoryStore: Send + Sync {
    /// Retrieves a value by key.
    async fn get(&self, key: &str) -> Option<String>;

    /// Lists all keys matching a prefix.
    async fn keys(&self, prefix: &str) -> Vec<String>;
}

// ---------------------------------------------------------------------------
// ReasoningOperator
// ---------------------------------------------------------------------------

/// An atomic reasoning operator.
///
/// Operators are the building blocks of reasoning strategies. Each operator
/// implements a single, well-defined transformation on a `TaskState`.
///
/// # Provider priority
///
/// If an operator has its own provider (via `provider` field),
/// it should use that. Otherwise fall back to [`OperatorContext::llm`].
///
/// # Prompt
///
/// Each operator has a mutable prompt template. The prompt can be customized
/// via [`set_prompt`](ReasoningOperator::set_prompt) before execution.
///
/// # Design contract
///
/// - Operators MUST be **idempotent with respect to side effects**: calling
///   `apply` with the same inputs should produce the same state transformation.
/// - Operators MUST NOT mutate external state directly. If an action is needed,
///   encode it as an [`Action`](crate::reasoning::state::Action) in the output state.
/// - Operators SHOULD be small and composable. Complex reasoning patterns
///   are built by composing operators in a `ReasoningGraph`.
#[async_trait]
pub trait ReasoningOperator: Send + Sync {
    /// Returns the kind of this operator.
    fn kind(&self) -> OperatorKind;

    /// Returns a human-readable name for this operator (for logging/debugging).
    fn name(&self) -> &'static str;

    /// Returns the prompt template for this operator.
    fn prompt(&self) -> &str;

    /// Replaces the prompt template.
    fn set_prompt(&mut self, prompt: String);

    /// Applies this operator to the given state, producing a new state.
    ///
    /// # Errors
    ///
    /// Returns [`ReasoningError`] if the operator cannot produce a valid
    /// next state from the given input.
    async fn apply(
        &self,
        ctx: &OperatorContext<'_>,
        state: TaskState,
    ) -> Result<TaskState, ReasoningError>;
}

// ---------------------------------------------------------------------------
// BaseOperator
// ---------------------------------------------------------------------------

/// Shared state for all built-in operators.
///
/// Provides default implementations for prompt, provider, and name storage.
/// Concrete operator structs embed a `BaseOperator` and delegate trait methods.
pub struct BaseOperator {
    /// The operator kind discriminator.
    pub kind: OperatorKind,
    /// Human-readable operator name.
    pub name: &'static str,
    /// Mutable prompt template.
    pub prompt: String,
}

impl BaseOperator {
    /// Creates a new `BaseOperator` with the given kind and name.
    #[must_use]
    pub fn new(kind: OperatorKind, name: &'static str) -> Self {
        Self {
            kind,
            name,
            prompt: String::new(),
        }
    }

    /// Sets the prompt template, consuming self.
    #[must_use]
    pub fn with_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.prompt = prompt.into();
        self
    }
}

// ---------------------------------------------------------------------------
// Formatting & LLM call helpers
// ---------------------------------------------------------------------------

/// Formats the current reasoning state as a structured prompt string.
pub(crate) fn format_state(prompt: &str, state: &TaskState) -> String {
    let mut buf = String::new();

    let _ = writeln!(buf, "Goal: {}", state.goal.description);
    if !state.constraints.is_empty() {
        let _ = writeln!(buf);
        let _ = writeln!(buf, "Constraints:");
        for c in &state.constraints {
            let _ = writeln!(buf, "- {} ({:?})", c.description, c.kind);
        }
    }
    let _ = writeln!(buf);
    let _ = writeln!(buf, "Context: {}", state.context.input);
    if !state.context.facts.is_empty() {
        let _ = writeln!(buf, "Facts:");
        for f in &state.context.facts {
            let _ = writeln!(buf, "- {f}");
        }
    }
    if let Some(ref plan) = state.plan {
        let _ = writeln!(buf);
        let _ = writeln!(buf, "Plan: {}", plan.rationale);
        for step in &plan.steps {
            let _ = writeln!(
                buf,
                "- Step {}: {} ({:?})",
                step.id, step.description, step.status
            );
        }
    }
    if !state.observations.is_empty() {
        let _ = writeln!(buf);
        let _ = writeln!(buf, "Observations:");
        for o in &state.observations {
            let _ = writeln!(
                buf,
                "- [{}] {} (from {:?})",
                o.sequence, o.content, o.source
            );
        }
    }
    if !state.actions.is_empty() {
        let _ = writeln!(buf);
        let _ = writeln!(buf, "Actions:");
        for a in &state.actions {
            let tool_str = a.tool.as_deref().unwrap_or("reasoning");
            let _ = writeln!(buf, "- [{}] {} -> {:?}", tool_str, a.input, a.output);
        }
    }
    if !state.reflections.is_empty() {
        let _ = writeln!(buf);
        let _ = writeln!(buf, "Reflections:");
        for r in &state.reflections {
            let _ = writeln!(buf, "- {}", r.content);
        }
    }
    if !state.artifacts.is_empty() {
        let _ = writeln!(buf);
        let _ = writeln!(buf, "Artifacts:");
        for a in &state.artifacts {
            let _ = writeln!(buf, "- [{:?}] {}", a.kind, a.content);
        }
    }
    let _ = writeln!(buf);
    let _ = write!(buf, "---\n{prompt}");

    buf
}

/// Calls the LLM via [`OperatorContext`] and records a [`Step`].
///
/// Returns the LLM response text and the new state with the step recorded.
///
/// # Errors
///
/// Returns [`ReasoningError::OperatorFailed`] if no LLM is available or the
/// underlying provider fails.
pub(crate) async fn call_llm_and_record_step(
    ctx: &OperatorContext<'_>,
    kind: OperatorKind,
    name: &str,
    prompt: &str,
    state: TaskState,
    system: &str,
) -> Result<(String, TaskState), ReasoningError> {
    let llm = ctx.llm.ok_or_else(|| ReasoningError::OperatorFailed {
        operator: name.into(),
        message: "no LLM provider configured in operator context".into(),
    })?;
    let user = format_state(prompt, &state);
    let response =
        llm.complete(&user, Some(system))
            .await
            .map_err(|e| ReasoningError::OperatorFailed {
                operator: name.into(),
                message: e.to_string(),
            })?;
    let new_state = state.record_step(Step {
        operator: kind,
        input: user,
        output: Some(response.clone()),
        status: StepStatus::Completed,
    });
    Ok((response, new_state))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn operator_kind_is_terminal() {
        assert!(OperatorKind::Answer.is_terminal());
        assert!(!OperatorKind::Reason.is_terminal());
        assert!(!OperatorKind::Act.is_terminal());
        assert!(!OperatorKind::Explain.is_terminal());
        assert!(!OperatorKind::Evaluate.is_terminal());
        assert!(!OperatorKind::Generate.is_terminal());
    }

    #[test]
    fn operator_kind_requires_tools() {
        assert!(OperatorKind::Act.requires_tools());
        assert!(OperatorKind::Observe.requires_tools());
        assert!(OperatorKind::Search.requires_tools());
        assert!(OperatorKind::Test.requires_tools());
        assert!(OperatorKind::Simulate.requires_tools());
        assert!(!OperatorKind::Reason.requires_tools());
        assert!(!OperatorKind::Plan.requires_tools());
        assert!(!OperatorKind::Explain.requires_tools());
        assert!(!OperatorKind::Evaluate.requires_tools());
    }

    #[test]
    fn operator_kind_should_serialize() {
        let kind = OperatorKind::Analyze;
        let json = serde_json::to_string(&kind).unwrap();
        assert_eq!(json, "\"Analyze\"");
        let deserialized: OperatorKind = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, OperatorKind::Analyze);
    }

    #[test]
    fn all_operator_kinds_should_serialize_roundtrip() {
        let kinds = [
            OperatorKind::Analyze,
            OperatorKind::Decompose,
            OperatorKind::Plan,
            OperatorKind::Reason,
            OperatorKind::Act,
            OperatorKind::Observe,
            OperatorKind::Solve,
            OperatorKind::Verify,
            OperatorKind::Reflect,
            OperatorKind::Revise,
            OperatorKind::Select,
            OperatorKind::Compose,
            OperatorKind::Reduce,
            OperatorKind::Explain,
            OperatorKind::Evaluate,
            OperatorKind::Generate,
            OperatorKind::Hypothesize,
            OperatorKind::Test,
            OperatorKind::Search,
            OperatorKind::Synthesize,
            OperatorKind::Prioritize,
            OperatorKind::Optimize,
            OperatorKind::Simulate,
            OperatorKind::Diagnose,
            OperatorKind::Answer,
        ];

        for kind in &kinds {
            let json = serde_json::to_string(kind).unwrap();
            let deserialized: OperatorKind = serde_json::from_str(&json).unwrap();
            assert_eq!(deserialized, *kind);
        }
    }

    #[test]
    fn base_operator_new() {
        let base = BaseOperator::new(OperatorKind::Analyze, "Analyze");
        assert_eq!(base.kind, OperatorKind::Analyze);
        assert_eq!(base.name, "Analyze");
        assert!(base.prompt.is_empty());
    }
}