aingle_minimal 0.6.2

Ultra-light AIngle node for IoT devices (<1MB RAM)
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
// Copyright 2019-2026 Apilium Technologies OÜ. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR Commercial

//! Smart Node - IoT+AI Pipeline Integration
//!
//! Combines `MinimalNode` with Kaneru to create intelligent IoT nodes
//! that can observe, decide, act, and learn.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                      SmartNode                               │
//! ├─────────────────────────────────────────────────────────────┤
//! │                                                              │
//! │  ┌──────────────┐     ┌──────────────┐     ┌─────────────┐ │
//! │  │   Sensors    │────>│   Kaneru     │────>│  Network    │ │
//! │  │   (IoT)      │     │   Agent      │     │  (CoAP)     │ │
//! │  └──────────────┘     └──────────────┘     └─────────────┘ │
//! │         │                    │                    │         │
//! │         v                    v                    v         │
//! │  ┌──────────────┐     ┌──────────────┐     ┌─────────────┐ │
//! │  │ Observations │     │   Entries    │     │   Records   │ │
//! │  │              │────>│   (DAG)      │<────│   (Gossip)  │ │
//! │  └──────────────┘     └──────────────┘     └─────────────┘ │
//! │                                                              │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use aingle_minimal::{SmartNode, SmartNodeConfig};
//! use kaneru::{Observation, Goal};
//!
//! // Create smart node with AI capabilities
//! let config = SmartNodeConfig::iot_mode();
//! let mut node = SmartNode::new(config).await?;
//!
//! // Add a goal
//! node.add_goal(Goal::maintain("temperature", 20.0..25.0));
//!
//! // Process sensor data
//! node.observe(Observation::sensor("temperature", 22.5));
//!
//! // Let the agent decide and act
//! if let Some(action) = node.step() {
//!     println!("Action taken: {:?}", action);
//! }
//! ```

use crate::config::Config;
use crate::error::Result;
use crate::node::MinimalNode;
use crate::types::{Entry, EntryType, Hash, NodeStats};

use kaneru::agent::AgentStats;
use kaneru::{
    Action, ActionResult, ActionType, Agent, AgentConfig, AgentState, Goal, Observation, Policy,
    Rule, SimpleAgent,
};

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Configuration for SmartNode
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SmartNodeConfig {
    /// Base node configuration
    pub node_config: Config,
    /// Agent configuration
    pub agent_config: AgentConfig,
    /// Auto-publish sensor observations
    pub auto_publish_observations: bool,
    /// Auto-publish action results
    pub auto_publish_actions: bool,
    /// Observation retention period in seconds
    pub observation_retention_secs: u64,
    /// Maximum pending actions
    pub max_pending_actions: usize,
}

impl Default for SmartNodeConfig {
    fn default() -> Self {
        Self {
            node_config: Config::default(),
            agent_config: AgentConfig::default(),
            auto_publish_observations: true,
            auto_publish_actions: true,
            observation_retention_secs: 300,
            max_pending_actions: 100,
        }
    }
}

impl SmartNodeConfig {
    /// Create IoT-optimized configuration
    pub fn iot_mode() -> Self {
        Self {
            node_config: Config::iot_mode(),
            agent_config: AgentConfig::iot_mode(),
            auto_publish_observations: true,
            auto_publish_actions: true,
            observation_retention_secs: 60,
            max_pending_actions: 20,
        }
    }

    /// Create low-power configuration
    pub fn low_power() -> Self {
        Self {
            node_config: Config::low_power(),
            agent_config: AgentConfig::iot_mode(), // Use iot_mode as base
            auto_publish_observations: false,      // Save energy
            auto_publish_actions: false,
            observation_retention_secs: 30,
            max_pending_actions: 10,
        }
    }
}

/// Smart Node combining MinimalNode with Kaneru Agent
pub struct SmartNode {
    /// Base AIngle node
    node: MinimalNode,
    /// Kaneru Agent
    agent: SimpleAgent,
    /// Configuration
    config: SmartNodeConfig,
    /// Pending actions to execute
    pending_actions: Vec<Action>,
    /// Action history
    action_history: Vec<(Action, ActionResult)>,
    /// Entry hash to observation mapping
    observation_entries: HashMap<Hash, Observation>,
}

impl SmartNode {
    /// Create a new smart node
    pub fn new(config: SmartNodeConfig) -> Result<Self> {
        let node = MinimalNode::new(config.node_config.clone())?;
        let agent =
            SimpleAgent::with_config(&config.agent_config.name, config.agent_config.clone());

        Ok(Self {
            node,
            agent,
            config,
            pending_actions: Vec::new(),
            action_history: Vec::new(),
            observation_entries: HashMap::new(),
        })
    }

    /// Create with a pre-configured agent
    pub fn with_agent(config: SmartNodeConfig, agent: SimpleAgent) -> Result<Self> {
        let node = MinimalNode::new(config.node_config.clone())?;

        Ok(Self {
            node,
            agent,
            config,
            pending_actions: Vec::new(),
            action_history: Vec::new(),
            observation_entries: HashMap::new(),
        })
    }

    /// Get the underlying node
    pub fn node(&self) -> &MinimalNode {
        &self.node
    }

    /// Get mutable node reference
    pub fn node_mut(&mut self) -> &mut MinimalNode {
        &mut self.node
    }

    /// Get the agent
    pub fn agent(&self) -> &SimpleAgent {
        &self.agent
    }

    /// Get mutable agent reference
    pub fn agent_mut(&mut self) -> &mut SimpleAgent {
        &mut self.agent
    }

    /// Process an observation from a sensor
    pub fn observe(&mut self, observation: Observation) -> Result<Option<Hash>> {
        // Feed observation to agent
        self.agent.observe(observation.clone());

        // Optionally publish to DAG
        if self.config.auto_publish_observations {
            let entry = self.observation_to_entry(&observation);
            let hash = self.node.create_entry(entry)?;
            self.observation_entries.insert(hash.clone(), observation);
            return Ok(Some(hash));
        }

        Ok(None)
    }

    /// Process a batch of observations
    pub fn observe_batch(&mut self, observations: Vec<Observation>) -> Result<Vec<Hash>> {
        let mut hashes = Vec::new();
        for obs in observations {
            if let Some(hash) = self.observe(obs)? {
                hashes.push(hash);
            }
        }
        Ok(hashes)
    }

    /// Let the agent decide on an action
    pub fn decide(&self) -> Action {
        self.agent.decide()
    }

    /// Execute a single step: decide and execute action
    pub fn step(&mut self) -> Result<Option<ActionResult>> {
        let action = self.decide();

        if action.is_noop() {
            return Ok(None);
        }

        let result = self.execute_action(action)?;
        Ok(Some(result))
    }

    /// Execute an action
    pub fn execute_action(&mut self, action: Action) -> Result<ActionResult> {
        let start = std::time::Instant::now();

        // Execute based on action type
        let result = match &action.action_type {
            ActionType::Publish(topic) => self.execute_publish(&action, topic)?,
            ActionType::StoreData(key) => self.execute_store(&action, key)?,
            ActionType::SendMessage(target) => self.execute_send(&action, target)?,
            ActionType::Alert(message) => self.execute_alert(&action, message)?,
            ActionType::UpdateState(state_name) => {
                self.execute_state_update(&action, state_name)?
            }
            ActionType::Query(key) => self.execute_query(&action, key)?,
            ActionType::RemoteCall(target) => self.execute_remote_call(&action, target)?,
            ActionType::Wait => ActionResult::success(&action.id),
            ActionType::NoOp => ActionResult::success(&action.id),
            ActionType::Custom(name) => {
                log::debug!("Custom action '{}' executed", name);
                ActionResult::success(&action.id)
            }
        };

        let result = result.with_duration(start.elapsed().as_micros() as u64);

        // Clone observation for learning (to avoid borrow issues)
        let obs_clone = self
            .agent
            .recent_observations(1)
            .first()
            .map(|o| (*o).clone());

        // Learn from the result
        if let Some(obs) = obs_clone {
            self.agent.learn(&obs, &action, &result);
        }

        // Store in history
        if self.action_history.len() >= self.config.max_pending_actions {
            self.action_history.remove(0);
        }
        self.action_history.push((action.clone(), result.clone()));

        // Optionally publish action result
        if self.config.auto_publish_actions {
            let entry = self.action_to_entry(&action, &result);
            let _ = self.node.create_entry(entry);
        }

        Ok(result)
    }

    /// Execute publish action
    fn execute_publish(&mut self, action: &Action, topic: &str) -> Result<ActionResult> {
        // Get value from params
        let value = action.params.get("value").cloned();
        let value_str = match &value {
            Some(v) => serde_json::to_string(v).unwrap_or_default(),
            None => "null".to_string(),
        };

        // Create entry
        let content = format!("{{\"topic\":\"{}\",\"value\":{}}}", topic, value_str);
        let entry = Entry {
            entry_type: EntryType::App,
            content: content.into_bytes(),
        };

        match self.node.create_entry(entry) {
            Ok(hash) => Ok(ActionResult::success_with_value(
                &action.id,
                hash.to_string(),
            )),
            Err(e) => Ok(ActionResult::failure(&action.id, &e.to_string())),
        }
    }

    /// Execute store action
    fn execute_store(&mut self, action: &Action, key: &str) -> Result<ActionResult> {
        let value = action.params.get("value").cloned();
        let value_str = match &value {
            Some(v) => serde_json::to_string(v).unwrap_or_default(),
            None => "null".to_string(),
        };

        let content = format!("{{\"key\":\"{}\",\"value\":{}}}", key, value_str);
        let entry = Entry {
            entry_type: EntryType::App,
            content: content.into_bytes(),
        };

        match self.node.create_entry(entry) {
            Ok(hash) => Ok(ActionResult::success_with_value(
                &action.id,
                hash.to_string(),
            )),
            Err(e) => Ok(ActionResult::failure(&action.id, &e.to_string())),
        }
    }

    /// Execute send message action
    fn execute_send(&mut self, action: &Action, target: &str) -> Result<ActionResult> {
        log::info!(
            "Sending message to {}: {:?}",
            target,
            action.params.get("content")
        );
        // Network send would go here (via CoAP or gossip)
        Ok(ActionResult::success(&action.id))
    }

    /// Execute alert action
    fn execute_alert(&mut self, action: &Action, message: &str) -> Result<ActionResult> {
        log::warn!("ALERT: {}", message);

        // Create alert entry
        let content = format!(
            "{{\"alert\":\"{}\",\"timestamp\":{}}}",
            message,
            chrono::Utc::now().timestamp()
        );
        let entry = Entry {
            entry_type: EntryType::App,
            content: content.into_bytes(),
        };

        let _ = self.node.create_entry(entry);
        Ok(ActionResult::success(&action.id))
    }

    /// Execute state update action
    fn execute_state_update(&mut self, action: &Action, state_name: &str) -> Result<ActionResult> {
        let value = action.params.get("value").cloned();
        log::debug!("State update: {} = {:?}", state_name, value);
        Ok(ActionResult::success(&action.id))
    }

    /// Execute query action - retrieve data from the DAG by hash
    fn execute_query(&self, action: &Action, key: &str) -> Result<ActionResult> {
        log::debug!("Querying data for key: {}", key);

        // Create hash from key (will hash the key string)
        let hash = Hash::from_bytes(key.as_bytes());

        match self.node.get_entry(&hash) {
            Ok(Some(entry)) => {
                let content_str = String::from_utf8_lossy(&entry.content);
                Ok(ActionResult::success_with_value(
                    &action.id,
                    content_str.to_string(),
                ))
            }
            Ok(None) => {
                // Entry not found - return structured response
                Ok(ActionResult::failure(&action.id, "Entry not found"))
            }
            Err(e) => Ok(ActionResult::failure(&action.id, &e.to_string())),
        }
    }

    /// Execute remote call action - send message to another node
    fn execute_remote_call(&self, action: &Action, target: &str) -> Result<ActionResult> {
        log::info!("Remote call to target: {}", target);

        // Get method from params (kaneru::Value)
        let method = action
            .params
            .get("method")
            .and_then(|v| match v {
                kaneru::Value::String(s) => Some(s.as_str()),
                _ => None,
            })
            .unwrap_or("ping");

        // Get payload from params (convert kaneru::Value to bytes)
        let payload = action
            .params
            .get("payload")
            .map(|v| match v {
                kaneru::Value::Bytes(b) => b.clone(),
                kaneru::Value::String(s) => s.as_bytes().to_vec(),
                kaneru::Value::Json(j) => serde_json::to_vec(j).unwrap_or_default(),
                _ => Vec::new(),
            })
            .unwrap_or_default();

        // Build remote call message
        let from_key = self.node.public_key();
        let message = serde_json::json!({
            "type": "remote_call",
            "target": target,
            "method": method,
            "payload": payload,
            "from": format!("{:?}", from_key),
        });

        log::debug!("Remote call message: {:?}", message);

        // In a full implementation, this would:
        // 1. Resolve target node address (via DHT or direct)
        // 2. Send CoAP/QUIC request to target
        // 3. Wait for response with timeout
        // 4. Return response or error

        // For now, return success with pending status
        Ok(ActionResult::success_with_value(
            &action.id,
            format!(
                "{{\"status\":\"pending\",\"target\":\"{}\",\"method\":\"{}\"}}",
                target, method
            ),
        ))
    }

    /// Convert observation to DAG entry
    fn observation_to_entry(&self, obs: &Observation) -> Entry {
        let content = serde_json::json!({
            "type": "observation",
            "obs_type": format!("{:?}", obs.obs_type),
            "value": obs.value,
            "timestamp": obs.timestamp.0,
            "confidence": obs.confidence.0,
            "metadata": obs.metadata,
        });

        Entry {
            entry_type: EntryType::App,
            content: content.to_string().into_bytes(),
        }
    }

    /// Convert action result to DAG entry
    fn action_to_entry(&self, action: &Action, result: &ActionResult) -> Entry {
        let content = serde_json::json!({
            "type": "action_result",
            "action_type": format!("{:?}", action.action_type),
            "success": result.success,
            "value": result.value,
            "error": result.error,
            "executed_at": result.executed_at.0,
            "duration_us": result.duration_us,
        });

        Entry {
            entry_type: EntryType::App,
            content: content.to_string().into_bytes(),
        }
    }

    /// Add a policy to the agent
    pub fn add_policy(&mut self, policy: Policy) {
        self.agent.add_policy(policy);
    }

    /// Add a rule to the agent
    pub fn add_rule(&mut self, rule: Rule) {
        self.agent.add_rule(rule);
    }

    /// Add a goal to the agent
    pub fn add_goal(&mut self, goal: Goal) {
        self.agent.add_goal(goal);
    }

    /// Get agent state
    pub fn agent_state(&self) -> AgentState {
        self.agent.state()
    }

    /// Get agent statistics
    pub fn agent_stats(&self) -> &AgentStats {
        self.agent.stats()
    }

    /// Get node statistics
    pub fn node_stats(&self) -> Result<NodeStats> {
        self.node.stats()
    }

    /// Get combined statistics
    pub fn stats(&self) -> Result<SmartNodeStats> {
        Ok(SmartNodeStats {
            node_stats: self.node.stats()?,
            agent_stats: self.agent.stats().clone(),
            pending_actions: self.pending_actions.len(),
            action_history_len: self.action_history.len(),
            observation_entries: self.observation_entries.len(),
        })
    }

    /// Pause the agent
    pub fn pause(&mut self) {
        self.agent.pause();
    }

    /// Resume the agent
    pub fn resume(&mut self) {
        self.agent.resume();
    }

    /// Stop the smart node
    pub fn stop(&mut self) {
        self.agent.stop();
    }

    /// Check if running
    pub fn is_running(&self) -> bool {
        self.agent.is_running()
    }

    /// Get active goals
    pub fn active_goals(&self) -> Vec<&Goal> {
        self.agent.active_goals()
    }

    /// Get recent observations
    pub fn recent_observations(&self, count: usize) -> Vec<&Observation> {
        self.agent.recent_observations(count)
    }

    /// Get action history
    pub fn action_history(&self) -> &[(Action, ActionResult)] {
        &self.action_history
    }

    /// Clear action history
    pub fn clear_history(&mut self) {
        self.action_history.clear();
    }
}

/// Combined statistics for SmartNode
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SmartNodeStats {
    /// Node statistics
    pub node_stats: NodeStats,
    /// Agent statistics
    pub agent_stats: AgentStats,
    /// Pending actions count
    pub pending_actions: usize,
    /// Action history length
    pub action_history_len: usize,
    /// Observation entries count
    pub observation_entries: usize,
}

/// Sensor adapter for converting sensor readings to Kaneru observations
pub struct SensorAdapter {
    name: String,
    scale: f64,
    offset: f64,
}

impl SensorAdapter {
    /// Create a new sensor adapter
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            scale: 1.0,
            offset: 0.0,
        }
    }

    /// Create with scaling
    pub fn with_scaling(name: &str, scale: f64, offset: f64) -> Self {
        Self {
            name: name.to_string(),
            scale,
            offset,
        }
    }

    /// Convert raw reading to observation
    pub fn reading(&self, raw_value: f64) -> Observation {
        let scaled = raw_value * self.scale + self.offset;
        Observation::sensor(&self.name, scaled)
    }

    /// Create boolean observation (on/off, true/false)
    pub fn boolean(&self, value: bool) -> Observation {
        Observation::sensor(&self.name, if value { 1.0 } else { 0.0 })
    }

    /// Create event observation
    pub fn event(&self) -> Observation {
        Observation::event(&self.name)
    }
}

/// Policy builder for common IoT scenarios
pub struct IoTPolicyBuilder;

impl IoTPolicyBuilder {
    /// Create a threshold alert policy
    pub fn threshold_alert(sensor_name: &str, threshold: f64, alert_message: &str) -> Rule {
        use kaneru::policy::Condition;

        Rule::new(
            &format!("{}_threshold", sensor_name),
            Condition::above(sensor_name, threshold),
            Action::alert(alert_message),
        )
    }

    /// Create a range maintenance policy
    pub fn maintain_range(
        sensor_name: &str,
        min: f64,
        max: f64,
        action_low: Action,
        action_high: Action,
    ) -> Vec<Rule> {
        use kaneru::policy::Condition;

        vec![
            Rule::new(
                &format!("{}_below_min", sensor_name),
                Condition::below(sensor_name, min),
                action_low,
            ),
            Rule::new(
                &format!("{}_above_max", sensor_name),
                Condition::above(sensor_name, max),
                action_high,
            ),
        ]
    }

    /// Create a binary control policy (on/off based on threshold)
    pub fn binary_control(
        sensor_name: &str,
        threshold: f64,
        on_action: Action,
        off_action: Action,
    ) -> Vec<Rule> {
        use kaneru::policy::Condition;

        vec![
            Rule::new(
                &format!("{}_turn_on", sensor_name),
                Condition::below(sensor_name, threshold),
                on_action,
            ),
            Rule::new(
                &format!("{}_turn_off", sensor_name),
                Condition::above(sensor_name, threshold),
                off_action,
            ),
        ]
    }
}

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

    fn test_config() -> SmartNodeConfig {
        SmartNodeConfig {
            node_config: Config::test_mode(),
            agent_config: AgentConfig::default(),
            auto_publish_observations: false,
            auto_publish_actions: false,
            observation_retention_secs: 60,
            max_pending_actions: 10,
        }
    }

    #[test]
    fn test_smart_node_creation() {
        let config = test_config();
        let node = SmartNode::new(config).unwrap();
        assert!(node.is_running());
    }

    #[test]
    fn test_smart_node_observe() {
        let config = test_config();
        let mut node = SmartNode::new(config).unwrap();

        let obs = Observation::sensor("temperature", 25.0);
        let result = node.observe(obs);
        assert!(result.is_ok());

        assert_eq!(node.agent_stats().observations_received, 1);
    }

    #[test]
    fn test_smart_node_step() {
        let config = test_config();
        let mut node = SmartNode::new(config).unwrap();

        // Without any rules or observations, step should return None
        let result = node.step().unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_sensor_adapter() {
        let adapter = SensorAdapter::with_scaling("temperature", 0.1, -40.0);
        let obs = adapter.reading(650.0); // Raw ADC value

        assert_eq!(obs.value.as_f64().unwrap(), 25.0); // (650 * 0.1) - 40 = 25
    }

    #[test]
    fn test_policy_builder() {
        let rule = IoTPolicyBuilder::threshold_alert("temperature", 30.0, "High temperature!");

        assert!(matches!(rule.action.action_type, ActionType::Alert(_)));
    }

    #[test]
    fn test_smart_node_with_rule() {
        use kaneru::policy::Condition;

        let config = test_config();
        let mut node = SmartNode::new(config).unwrap();

        // Add a rule: if temp > 30, alert
        let rule = Rule::new(
            "high_temp",
            Condition::above("temperature", 30.0),
            Action::alert("Temperature too high!"),
        );
        node.add_rule(rule);

        // Observe high temperature
        let obs = Observation::sensor("temperature", 35.0);
        node.observe(obs).unwrap();

        // Step should trigger the alert
        let result = node.step().unwrap();
        assert!(result.is_some());

        let action_result = result.unwrap();
        assert!(action_result.success);
    }

    #[test]
    fn test_smart_node_stats() {
        let config = test_config();
        let node = SmartNode::new(config).unwrap();

        let stats = node.stats().unwrap();
        assert_eq!(stats.pending_actions, 0);
        assert_eq!(stats.observation_entries, 0);
    }
}