omega-cognitive-kernel 0.15.2

Online strategy-patch runtime: training-free RuleEffect adaptation with audit and rollback
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
//! Body / sensor / actuator bus — edge-device channel layer.
//!
//! Minimal live implementation (no hardware I/O yet).
//!
//! Safety rules (non-negotiable):
//! - Never issues tokens or writes CAS.
//! - `write_authorized` is always `false` on every return packet from this module.
//! - Sensor `inject` is in-memory simulation only.
//! - Actuator `propose_command` returns a proposal payload only without mutating channel value.
//! - `apply_command` commits value only when called from a gated execute path.

use crate::prelude::*;
use alloc::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;

impl ChannelKind {
    pub fn parse(raw: &str) -> core::result::Result<Self, String> {
        let normalized = raw.trim().to_ascii_lowercase();
        if normalized.is_empty() || normalized == "sensor" {
            Ok(Self::Sensor)
        } else if normalized == "actuator" {
            Ok(Self::Actuator)
        } else if normalized == "bidirectional" {
            Ok(Self::Bidirectional)
        } else {
            Err(format!(
                "kind must be one of [actuator, bidirectional, sensor], got '{normalized}'"
            ))
        }
    }

    pub fn as_str(self) -> &'static str {
        match self {
            Self::Sensor => "sensor",
            Self::Actuator => "actuator",
            Self::Bidirectional => "bidirectional",
        }
    }
}

/// Lifecycle status returned in [`BodyPacket`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BodyPacketStatus {
    Registered,
    Ok,
    Injected,
    Proposed,
    Applied,
    Rejected,
    UnknownChannel,
}

/// Structured actuator command proposal emitted before token-gated execution.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BodyCommandProposal {
    pub action: String,
    pub channel: String,
    pub value: Value,
    pub reason: String,
    pub kind: ChannelKind,
    pub applied: bool,
    pub ts_ms: u64,
}

/// Packet returned by every [`BodyBus`] operation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BodyPacket {
    pub status: BodyPacketStatus,
    pub channel: String,
    pub kind: Option<ChannelKind>,
    pub value: Option<Value>,
    pub updated_at_ms: Option<u64>,
    pub write_authorized: bool,
    pub applied: bool,
    pub error: Option<String>,
    pub proposal: Option<BodyCommandProposal>,
}

/// Channel metadata snapshot returned by [`BodyBus::list_channels`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BodyChannelInfo {
    pub name: String,
    pub kind: ChannelKind,
    pub has_value: bool,
    pub updated_at_ms: Option<u64>,
}

/// Aggregate observability summary for [`BodyBus`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BodySummary {
    pub channel_count: usize,
    pub sensors: usize,
    pub actuators: usize,
    pub bidirectional: usize,
    pub channels: Vec<BodyChannelInfo>,
    pub write_authorized: bool,
    pub note: &'static str,
}

#[derive(Debug, Clone)]
struct ChannelState {
    kind: ChannelKind,
    value: Option<Value>,
    updated_at_ms: Option<u64>,
    meta: BTreeMap<String, Value>,
    last_command: Option<BodyCommandProposal>,
}

/// Named in-memory channels for sensors and actuators.
///
/// Channels hold the last known value in memory. Nothing here authorizes
/// world writes or CAS mutations.
#[derive(Debug, Clone, Default)]
pub struct BodyBus {
    channels: BTreeMap<String, ChannelState>,
}

impl BodyBus {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn register(
        &mut self,
        name: &str,
        kind: ChannelKind,
        meta: Option<BTreeMap<String, Value>>,
    ) -> core::result::Result<BodyPacket, String> {
        let trimmed = name.trim();
        if trimmed.is_empty() {
            return Err("channel name must be non-empty".to_string());
        }
        let key = trimmed.to_string();
        let existing = self.channels.get(&key);
        let value = existing.and_then(|c| c.value.clone());
        let updated_at_ms = existing.and_then(|c| c.updated_at_ms);
        let last_command = existing.and_then(|c| c.last_command.clone());

        self.channels.insert(
            key.clone(),
            ChannelState {
                kind,
                value,
                updated_at_ms,
                meta: meta.unwrap_or_default(),
                last_command,
            },
        );
        Ok(self.packet(&key, BodyPacketStatus::Registered, None))
    }

    pub fn unregister(&mut self, name: &str) -> bool {
        self.channels.remove(name).is_some()
    }

    pub fn channel_meta(&self, name: &str) -> Option<&BTreeMap<String, Value>> {
        self.channels.get(name).map(|c| &c.meta)
    }

    pub fn list_channels(&self) -> Vec<BodyChannelInfo> {
        self.channels
            .iter()
            .map(|(name, ch)| BodyChannelInfo {
                name: name.clone(),
                kind: ch.kind,
                has_value: ch.value.is_some(),
                updated_at_ms: ch.updated_at_ms,
            })
            .collect()
    }

    /// Read last known value for a channel (observation only).
    pub fn read(&self, name: &str) -> BodyPacket {
        if !self.channels.contains_key(name) {
            return self.packet(
                name,
                BodyPacketStatus::UnknownChannel,
                Some("not_registered"),
            );
        }
        self.packet(name, BodyPacketStatus::Ok, None)
    }

    /// Simulate a sensor reading (in-memory only).
    ///
    /// Allowed for `Sensor` and `Bidirectional` channels. Does not touch the world.
    pub fn inject(&mut self, name: &str, value: Value, timestamp_ms: u64) -> BodyPacket {
        let Some(ch) = self.channels.get_mut(name) else {
            return self.packet(
                name,
                BodyPacketStatus::UnknownChannel,
                Some("not_registered"),
            );
        };
        if ch.kind == ChannelKind::Actuator {
            return self.packet(
                name,
                BodyPacketStatus::Rejected,
                Some("inject_not_allowed_on_actuator"),
            );
        }
        ch.value = Some(value);
        ch.updated_at_ms = Some(timestamp_ms);
        self.packet(name, BodyPacketStatus::Injected, None)
    }

    /// Build an actuator command proposal without mutating the channel value.
    pub fn propose_command(
        &mut self,
        name: &str,
        value: Value,
        reason: &str,
        timestamp_ms: u64,
    ) -> BodyPacket {
        let Some(ch) = self.channels.get_mut(name) else {
            return self.packet(
                name,
                BodyPacketStatus::UnknownChannel,
                Some("not_registered"),
            );
        };
        if ch.kind == ChannelKind::Sensor {
            return self.packet(
                name,
                BodyPacketStatus::Rejected,
                Some("command_not_allowed_on_sensor"),
            );
        }
        let proposal = BodyCommandProposal {
            action: "body_command".to_string(),
            channel: name.to_string(),
            value,
            reason: reason.to_string(),
            kind: ch.kind,
            applied: false,
            ts_ms: timestamp_ms,
        };
        ch.last_command = Some(proposal.clone());
        let mut pkt = self.packet(name, BodyPacketStatus::Proposed, None);
        pkt.proposal = Some(proposal);
        pkt
    }

    /// Commit actuator value after a token-gated execute step.
    ///
    /// Still never issues tokens or writes CAS; `write_authorized` stays `false`.
    pub fn apply_command(&mut self, name: &str, value: Value, timestamp_ms: u64) -> BodyPacket {
        let Some(ch) = self.channels.get_mut(name) else {
            return self.packet(
                name,
                BodyPacketStatus::UnknownChannel,
                Some("not_registered"),
            );
        };
        if ch.kind == ChannelKind::Sensor {
            return self.packet(
                name,
                BodyPacketStatus::Rejected,
                Some("command_not_allowed_on_sensor"),
            );
        }
        let proposal = BodyCommandProposal {
            action: "body_command".to_string(),
            channel: name.to_string(),
            value: value.clone(),
            reason: String::new(),
            kind: ch.kind,
            applied: true,
            ts_ms: timestamp_ms,
        };
        ch.value = Some(value);
        ch.updated_at_ms = Some(timestamp_ms);
        ch.last_command = Some(proposal);
        let mut pkt = self.packet(name, BodyPacketStatus::Applied, None);
        pkt.write_authorized = false;
        pkt.applied = true;
        pkt
    }

    pub fn summary(&self) -> BodySummary {
        let mut sensors = 0usize;
        let mut actuators = 0usize;
        let mut bidirectional = 0usize;
        for ch in self.channels.values() {
            match ch.kind {
                ChannelKind::Sensor => sensors += 1,
                ChannelKind::Actuator => actuators += 1,
                ChannelKind::Bidirectional => bidirectional += 1,
            }
        }
        BodySummary {
            channel_count: self.channels.len(),
            sensors,
            actuators,
            bidirectional,
            channels: self.list_channels(),
            write_authorized: false,
            note: "body_bus_gated_via_suggestion",
        }
    }

    fn packet(&self, name: &str, status: BodyPacketStatus, error: Option<&str>) -> BodyPacket {
        let ch = self.channels.get(name);
        BodyPacket {
            status,
            channel: name.to_string(),
            kind: ch.map(|c| c.kind),
            value: ch.and_then(|c| c.value.clone()),
            updated_at_ms: ch.and_then(|c| c.updated_at_ms),
            write_authorized: false,
            applied: false,
            error: error.map(str::to_string),
            proposal: None,
        }
    }
}

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

    #[test]
    fn register_list_and_unregister_channels() {
        let mut bus = BodyBus::new();
        assert!(bus.register("  ", ChannelKind::Sensor, None).is_err());

        let pkt = bus
            .register("temp", ChannelKind::Sensor, None)
            .expect("register sensor");
        assert_eq!(pkt.status, BodyPacketStatus::Registered);
        assert!(!pkt.write_authorized);

        bus.register("valve", ChannelKind::Actuator, None)
            .expect("register actuator");
        let list = bus.list_channels();
        assert_eq!(list.len(), 2);
        assert_eq!(list[0].name, "temp");
        assert_eq!(list[1].name, "valve");

        assert!(bus.unregister("temp"));
        assert!(!bus.unregister("temp"));
    }

    #[test]
    fn sensor_inject_and_reject_actuator() {
        let mut bus = BodyBus::new();
        bus.register("temp", ChannelKind::Sensor, None).unwrap();
        bus.register("motor", ChannelKind::Actuator, None).unwrap();

        let injected = bus.inject("temp", json!(23.5), 1_000);
        assert_eq!(injected.status, BodyPacketStatus::Injected);
        assert_eq!(injected.value, Some(json!(23.5)));
        assert!(!injected.write_authorized);

        let rejected = bus.inject("motor", json!(1.0), 1_001);
        assert_eq!(rejected.status, BodyPacketStatus::Rejected);
        assert_eq!(
            rejected.error.as_deref(),
            Some("inject_not_allowed_on_actuator")
        );
        assert!(!rejected.write_authorized);
    }

    #[test]
    fn propose_does_not_mutate_until_apply_and_never_authorizes_writes() {
        let mut bus = BodyBus::new();
        bus.register("temp", ChannelKind::Sensor, None).unwrap();
        bus.register("servo", ChannelKind::Bidirectional, None)
            .unwrap();

        let sensor_cmd = bus.propose_command("temp", json!(10), "test", 2_000);
        assert_eq!(sensor_cmd.status, BodyPacketStatus::Rejected);
        assert_eq!(
            sensor_cmd.error.as_deref(),
            Some("command_not_allowed_on_sensor")
        );

        let proposed = bus.propose_command("servo", json!(45), "align", 2_001);
        assert_eq!(proposed.status, BodyPacketStatus::Proposed);
        assert!(proposed.value.is_none());
        assert!(proposed.proposal.is_some());
        assert!(!proposed.write_authorized);

        let applied = bus.apply_command("servo", json!(45), 2_002);
        assert_eq!(applied.status, BodyPacketStatus::Applied);
        assert!(applied.applied);
        assert_eq!(applied.value, Some(json!(45)));
        assert!(!applied.write_authorized);

        let summary = bus.summary();
        assert_eq!(summary.sensors, 1);
        assert_eq!(summary.bidirectional, 1);
        assert!(!summary.write_authorized);
    }
}
/// Supported channel directions on [`BodyBus`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChannelKind {
    Sensor,
    Actuator,
    Bidirectional,
}