cano 0.8.0

High-performance orchestration engine for building resilient, self-healing systems in Rust. Uses Finite State Machines (FSM) for strict, type-safe transitions.
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
//! # Negotiation Workflow Example
//!
//! This example demonstrates a negotiation workflow between a seller and buyer using the Workflow structure:
//! 1. **SellerNode**: Starts with an initial price and decrements it on each round
//! 2. **BuyerNode**: Evaluates the offer against their budget and decides to accept or continue
//!
//! The workflow showcases:
//! - Inter-node communication through shared store
//! - Iterative negotiation logic with random price decrements
//! - Workflow control based on negotiation outcomes
//!
//! ## Testing Different Scenarios
//!
//! To test a "no deal" scenario, modify the buyer budget in the code:
//! - Change `buyer_budget = 1000` to `buyer_budget = 50` for a failed negotiation
//! - The buyer will walk away after 10 rounds if the offer is still too high
//!
//! Run with:
//! ```bash
//! cargo run --example workflow_negotiation
//! ```

use async_trait::async_trait;
use cano::prelude::*;
use rand::RngExt;

/// Action enum for controlling negotiation workflow
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum NegotiationAction {
    StartSelling,
    BuyerEvaluate,
    Deal,
    NoDeal,
    Error,
}

/// Negotiation state to track the current offer and round
#[derive(Debug, Clone)]
struct NegotiationState {
    current_offer: u32,
    buyer_budget: u32,
    round: u32,
    seller_initial_price: u32,
}

impl NegotiationState {
    fn new(initial_price: u32, buyer_budget: u32) -> Self {
        Self {
            current_offer: initial_price,
            buyer_budget,
            round: 1,
            seller_initial_price: initial_price,
        }
    }
}

/// Seller node: Manages pricing and decrements offers
#[derive(Clone)]
struct SellerNode;

impl SellerNode {
    fn new() -> Self {
        Self
    }

    /// Generate a random price reduction between 500 and 2000
    fn calculate_price_reduction() -> u32 {
        let mut rng = rand::rng();
        rng.random_range(500..=2000)
    }
}

#[async_trait]
impl Node<NegotiationAction> for SellerNode {
    type PrepResult = NegotiationState;
    type ExecResult = NegotiationState;

    fn config(&self) -> TaskConfig {
        TaskConfig::minimal()
    }

    /// Preparation phase: Load current negotiation state or initialize if first round
    async fn prep(&self, store: &MemoryStore) -> Result<Self::PrepResult, CanoError> {
        match store.get::<NegotiationState>("negotiation_state") {
            Ok(state) => {
                println!(
                    "🏷️  Seller: Round {} - Current offer on table: ${}",
                    state.round, state.current_offer
                );
                Ok(state)
            }
            Err(_) => {
                // First round - initialize negotiation
                let initial_price = 10000;
                let buyer_budget = 1000;
                let state = NegotiationState::new(initial_price, buyer_budget);

                println!("🏪 Seller: Starting negotiation!");
                println!("🏷️  Seller: Initial asking price: ${initial_price}");
                println!("💰 Buyer budget: ${buyer_budget}");
                println!("{}", "=".repeat(50));

                Ok(state)
            }
        }
    }

    /// Execution phase: Calculate new offer (unless it's the first round)
    async fn exec(&self, prep_res: Self::PrepResult) -> Self::ExecResult {
        let mut state = prep_res;

        // If this is not the first round, make a new offer
        if state.round > 1 {
            let reduction = Self::calculate_price_reduction();
            let new_offer = state.current_offer.saturating_sub(reduction);

            // Don't go below a minimum reasonable price
            let minimum_price = 100;
            state.current_offer = std::cmp::max(new_offer, minimum_price);

            println!(
                "🏷️  Seller: Round {} - Reducing price by ${}",
                state.round, reduction
            );
            println!("🏷️  Seller: New offer: ${}", state.current_offer);

            if state.current_offer == minimum_price {
                println!("🏷️  Seller: This is my final offer! Can't go any lower.");
            }
        } else {
            println!("🏷️  Seller: My asking price is ${}", state.current_offer);
        }

        state
    }

    /// Post-processing phase: Store the updated offer for buyer evaluation
    async fn post(
        &self,
        store: &MemoryStore,
        exec_res: Self::ExecResult,
    ) -> Result<NegotiationAction, CanoError> {
        // Store the current negotiation state
        store.put("negotiation_state", exec_res.clone())?;

        println!("🏷️  Seller: Waiting for buyer's response...");
        println!("{}", "-".repeat(30));

        Ok(NegotiationAction::BuyerEvaluate)
    }
}

/// Buyer node: Evaluates offers and makes decisions
#[derive(Clone)]
struct BuyerNode;

impl BuyerNode {
    fn new() -> Self {
        Self
    }

    /// Evaluate if the offer is acceptable based on budget and negotiation strategy
    fn evaluate_offer(state: &NegotiationState) -> bool {
        let offer_ratio = state.current_offer as f64 / state.buyer_budget as f64;

        // Accept if offer is at or below budget
        if state.current_offer <= state.buyer_budget {
            return true;
        }

        // Give up if the offer is still way too high after many rounds
        if state.round >= 10 && offer_ratio > 3.0 {
            return false; // Will result in NoDeal
        }

        false
    }
}

#[async_trait]
impl Node<NegotiationAction> for BuyerNode {
    type PrepResult = NegotiationState;
    type ExecResult = (NegotiationState, bool);

    fn config(&self) -> TaskConfig {
        TaskConfig::minimal()
    }

    /// Preparation phase: Load the current negotiation state
    async fn prep(&self, store: &MemoryStore) -> Result<Self::PrepResult, CanoError> {
        let state: NegotiationState = store.get("negotiation_state").map_err(|e| {
            CanoError::preparation(format!("Failed to load negotiation state: {e}"))
        })?;

        println!(
            "💰 Buyer: Evaluating seller's offer of ${}",
            state.current_offer
        );
        Ok(state)
    }

    /// Execution phase: Decide whether to accept the offer
    async fn exec(&self, prep_res: Self::PrepResult) -> Self::ExecResult {
        let state = prep_res;
        let acceptable = Self::evaluate_offer(&state);

        if acceptable {
            if state.current_offer <= state.buyer_budget {
                println!(
                    "💰 Buyer: Great! This offer (${}) fits my budget (${})",
                    state.current_offer, state.buyer_budget
                );
                println!("🤝 Buyer: I accept this deal!");
            }
        } else {
            let offer_ratio = state.current_offer as f64 / state.buyer_budget as f64;

            if state.round >= 10 && offer_ratio > 3.0 {
                println!(
                    "💰 Buyer: This is taking too long and the offer (${}) is still {}x my budget.",
                    state.current_offer,
                    offer_ratio.round() as u32
                );
                println!("😞 Buyer: I'm walking away from this negotiation.");
            } else {
                println!(
                    "💰 Buyer: ${} is still above my budget of ${}.",
                    state.current_offer, state.buyer_budget
                );
                println!("💰 Buyer: Can you do better?");
            }
        }

        (state, acceptable)
    }

    /// Post-processing phase: Update state and determine next action
    async fn post(
        &self,
        store: &MemoryStore,
        exec_res: Self::ExecResult,
    ) -> Result<NegotiationAction, CanoError> {
        let (mut state, acceptable) = exec_res;

        if acceptable && state.current_offer <= state.buyer_budget {
            // Store final deal details
            store.put("final_deal", state.clone())?;
            store.remove("negotiation_state")?;

            println!("✅ Deal reached in round {}!", state.round);
            return Ok(NegotiationAction::Deal);
        }

        // Check if we should give up
        let offer_ratio = state.current_offer as f64 / state.buyer_budget as f64;
        if state.round >= 10 && offer_ratio > 3.0 {
            store.put("failed_negotiation", state)?;
            store.remove("negotiation_state")?;
            return Ok(NegotiationAction::NoDeal);
        }

        // Continue negotiation - increment round and go back to seller
        state.round += 1;
        store.put("negotiation_state", state)?;

        println!("{}", "-".repeat(30));

        Ok(NegotiationAction::StartSelling)
    }
}

/// Negotiation orchestrator using Workflow
async fn run_negotiation_workflow() -> Result<(), CanoError> {
    println!("🤝 Starting Negotiation Workflow");
    println!("================================");
    println!("Seller starts at $10,000");
    println!("Buyer has a budget of $1,000");
    println!("Let's see if they can make a deal!");
    println!();

    let store = MemoryStore::new();

    // Create a Workflow that handles the negotiation process
    let workflow = Workflow::new(store.clone())
        .register(NegotiationAction::StartSelling, SellerNode::new())
        .register(NegotiationAction::BuyerEvaluate, BuyerNode::new())
        .add_exit_states(vec![
            NegotiationAction::Deal,
            NegotiationAction::NoDeal,
            NegotiationAction::Error,
        ]);

    // Execute the negotiation workflow
    match workflow.orchestrate(NegotiationAction::StartSelling).await {
        Ok(final_state) => {
            println!("{}", "=".repeat(50));

            match final_state {
                NegotiationAction::Deal => {
                    println!("🎉 NEGOTIATION SUCCESSFUL!");

                    if let Ok(deal) = store.get::<NegotiationState>("final_deal") {
                        println!("📋 Final Deal Summary:");
                        println!("  • Final price: ${}", deal.current_offer);
                        println!("  • Buyer budget: ${}", deal.buyer_budget);
                        println!("  • Rounds of negotiation: {}", deal.round);
                        println!(
                            "  • Savings from initial price: ${}",
                            deal.seller_initial_price - deal.current_offer
                        );

                        let savings_percent = ((deal.seller_initial_price - deal.current_offer)
                            as f64
                            / deal.seller_initial_price as f64)
                            * 100.0;
                        println!("  • Discount achieved: {savings_percent:.1}%");
                    }
                }
                NegotiationAction::NoDeal => {
                    println!("💔 NEGOTIATION FAILED!");

                    if let Ok(failed) = store.get::<NegotiationState>("failed_negotiation") {
                        println!("📋 Negotiation Summary:");
                        println!("  • Final offer: ${}", failed.current_offer);
                        println!("  • Buyer budget: ${}", failed.buyer_budget);
                        println!("  • Rounds attempted: {}", failed.round);
                        println!(
                            "  • Gap remaining: ${}",
                            failed.current_offer - failed.buyer_budget
                        );

                        let gap_ratio = failed.current_offer as f64 / failed.buyer_budget as f64;
                        println!("  • Offer was {gap_ratio:.1}x the buyer's budget");
                    }

                    println!("The buyer walked away - no deal was reached.");
                }
                NegotiationAction::Error => {
                    eprintln!("❌ Negotiation terminated due to an error");
                    return Err(CanoError::workflow(
                        "Negotiation terminated with error state",
                    ));
                }
                other => {
                    eprintln!("⚠️  Negotiation ended in unexpected state: {other:?}");
                    return Err(CanoError::workflow(format!(
                        "Negotiation ended in unexpected state: {other:?}"
                    )));
                }
            }
        }
        Err(e) => {
            eprintln!("❌ Negotiation workflow failed: {e}");
            return Err(e);
        }
    }

    Ok(())
}

#[tokio::main]
async fn main() {
    println!("🤝 Negotiation Workflow Example");
    println!("===============================");

    match run_negotiation_workflow().await {
        Ok(()) => {
            println!("\n✅ Negotiation workflow completed!");
        }
        Err(e) => {
            eprintln!("\n❌ Negotiation workflow failed: {e}");
            std::process::exit(1);
        }
    }

    println!("\n🎭 This example demonstrates:");
    println!("  • Inter-node communication via shared store");
    println!("  • Iterative workflow with conditional routing");
    println!("  • Random business logic (price reductions)");
    println!("  • Multiple exit conditions (deal/no deal)");
}

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

    #[tokio::test]
    async fn test_seller_node_initialization() {
        let seller = SellerNode::new();
        let store = MemoryStore::new();

        // First run should initialize the negotiation
        let result = seller.run(&store).await.unwrap();
        assert_eq!(result, NegotiationAction::BuyerEvaluate);

        // Verify state was stored
        let state: NegotiationState = store.get("negotiation_state").unwrap();
        assert_eq!(state.current_offer, 10000);
        assert_eq!(state.buyer_budget, 1000);
        assert_eq!(state.round, 1);
    }

    #[tokio::test]
    async fn test_buyer_node_evaluation() {
        let store = MemoryStore::new();

        // Setup: affordable offer
        let affordable_state = NegotiationState::new(10000, 1000);
        let mut affordable_state_modified = affordable_state.clone();
        affordable_state_modified.current_offer = 800; // Within budget
        store
            .put("negotiation_state", affordable_state_modified.clone())
            .unwrap();

        let buyer = BuyerNode::new();
        let result = buyer.run(&store).await.unwrap();

        // Should accept the deal
        assert_eq!(result, NegotiationAction::Deal);
    }

    #[tokio::test]
    async fn test_buyer_node_rejection() {
        let store = MemoryStore::new();

        // Setup: expensive offer, early round
        let expensive_state = NegotiationState::new(10000, 1000);
        let mut expensive_state_modified = expensive_state.clone();
        expensive_state_modified.current_offer = 5000; // Way above budget
        expensive_state_modified.round = 2; // Early round
        store
            .put("negotiation_state", expensive_state_modified)
            .unwrap();

        let buyer = BuyerNode::new();
        let result = buyer.run(&store).await.unwrap();

        // Should continue negotiating
        assert_eq!(result, NegotiationAction::StartSelling);
    }

    #[tokio::test]
    async fn test_buyer_node_gives_up() {
        let store = MemoryStore::new();

        // Setup: expensive offer, many rounds
        let expensive_state = NegotiationState::new(10000, 1000);
        let mut expensive_state_modified = expensive_state.clone();
        expensive_state_modified.current_offer = 5000; // Still way above budget
        expensive_state_modified.round = 10; // Many rounds
        store
            .put("negotiation_state", expensive_state_modified)
            .unwrap();

        let buyer = BuyerNode::new();
        let result = buyer.run(&store).await.unwrap();

        // Should give up
        assert_eq!(result, NegotiationAction::NoDeal);
    }

    #[tokio::test]
    async fn test_seller_price_reduction() {
        let store = MemoryStore::new();

        // Setup: existing negotiation state
        let initial_state = NegotiationState::new(10000, 1000);
        let mut ongoing_state = initial_state.clone();
        ongoing_state.round = 2; // Not first round
        ongoing_state.current_offer = 8000;
        store
            .put("negotiation_state", ongoing_state.clone())
            .unwrap();

        let seller = SellerNode::new();
        let result = seller.run(&store).await.unwrap();

        assert_eq!(result, NegotiationAction::BuyerEvaluate);

        // Verify price was reduced
        let updated_state: NegotiationState = store.get("negotiation_state").unwrap();
        assert!(updated_state.current_offer < ongoing_state.current_offer);
        assert!(updated_state.current_offer >= 100); // Minimum price
    }

    #[tokio::test]
    async fn test_negotiation_state_structure() {
        let state = NegotiationState::new(5000, 2000);

        assert_eq!(state.current_offer, 5000);
        assert_eq!(state.buyer_budget, 2000);
        assert_eq!(state.round, 1);
        assert_eq!(state.seller_initial_price, 5000);
    }

    #[tokio::test]
    async fn test_price_reduction_range() {
        // Test the price reduction range multiple times
        for _ in 0..10 {
            let reduction = SellerNode::calculate_price_reduction();
            assert!(reduction >= 500);
            assert!(reduction <= 2000);
        }
    }

    #[tokio::test]
    async fn test_full_negotiation_workflow() {
        // This will run the full workflow - may result in either deal or no deal
        let result = run_negotiation_workflow().await;

        // The workflow should complete without errors, regardless of outcome
        assert!(result.is_ok());
    }
}